-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.ts
More file actions
68 lines (60 loc) · 1.72 KB
/
message.ts
File metadata and controls
68 lines (60 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os from "os";
import { EventType, MsgType, RawMessage } from "./types";
const magic = Buffer.from("i3-ipc");
const end = os.endianness();
const writeInt32 = (buf: Buffer, value: number, offset?: number) => {
if (end === "BE") {
return buf.writeUInt32BE(value, offset);
}
return buf.writeUInt32LE(value, offset);
};
const readInt32 = (buf: Buffer, offset?: number) => {
if (end === "BE") {
return buf.readUInt32BE(offset);
}
return buf.readUInt32LE(offset);
};
//<magic><i32 len><i32 type><msg>
export const writeMessage = (type: MsgType, value: string) => {
const msg = Buffer.from(value ?? "");
const len = msg.length;
const buf = Buffer.alloc(8);
writeInt32(buf, len);
writeInt32(buf, type, 4);
return Buffer.concat([magic, buf, msg]);
};
export const readMessage = (buf: Buffer, offset:number = 0 ): RawMessage => {
if (magic.compare(buf, offset, offset+magic.length) === 0) {
let lenOffset = offset + magic.length;
const len = readInt32(buf, lenOffset);
lenOffset+=4
const typ = readInt32(buf, lenOffset);
lenOffset+=4
const msg = buf.subarray(lenOffset, len + lenOffset);
let data: Record<string, any>;
try {
data = JSON.parse(msg.toString());
} catch (e) {
data = { unparsable: true };
}
return {
type: typ,
value: msg.toString(),
data,
start:offset,
end:len + lenOffset
};
} else {
throw new Error("message is missing magic string:" + buf.toString());
}
};
export const readMessages = (buff: Buffer): RawMessage[] => {
let offset = 0
let messages : RawMessage[] = []
while(offset < buff.length) {
const msg = readMessage(buff, offset)
offset = msg.end
messages.push(msg)
}
return messages
}