@jsonjoy.com/json-pack 1.14.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ws/WsFrameDecoder.d.ts +10 -0
- package/lib/ws/WsFrameDecoder.js +117 -0
- package/lib/ws/WsFrameDecoder.js.map +1 -0
- package/lib/ws/WsFrameEncoder.d.ts +17 -0
- package/lib/ws/WsFrameEncoder.js +125 -0
- package/lib/ws/WsFrameEncoder.js.map +1 -0
- package/lib/ws/constants.d.ts +9 -0
- package/lib/ws/constants.js +14 -0
- package/lib/ws/constants.js.map +1 -0
- package/lib/ws/errors.d.ts +6 -0
- package/lib/ws/errors.js +16 -0
- package/lib/ws/errors.js.map +1 -0
- package/lib/ws/frames.d.ts +20 -0
- package/lib/ws/frames.js +35 -0
- package/lib/ws/frames.js.map +1 -0
- package/lib/ws/index.d.ts +4 -0
- package/lib/ws/index.js +8 -0
- package/lib/ws/index.js.map +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StreamingOctetReader } from '@jsonjoy.com/util/lib/buffers/StreamingOctetReader';
|
|
2
|
+
import { WsCloseFrame, WsFrameHeader } from './frames';
|
|
3
|
+
export declare class WsFrameDecoder {
|
|
4
|
+
readonly reader: StreamingOctetReader;
|
|
5
|
+
push(uint8: Uint8Array): void;
|
|
6
|
+
readFrameHeader(): WsFrameHeader | undefined;
|
|
7
|
+
readFrameData(frame: WsFrameHeader, remaining: number, dst: Uint8Array, pos: number): number;
|
|
8
|
+
copyFrameData(frame: WsFrameHeader, dst: Uint8Array, pos: number): void;
|
|
9
|
+
readCloseFrameData(frame: WsCloseFrame): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WsFrameDecoder = void 0;
|
|
4
|
+
const StreamingOctetReader_1 = require("@jsonjoy.com/util/lib/buffers/StreamingOctetReader");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const frames_1 = require("./frames");
|
|
8
|
+
class WsFrameDecoder {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.reader = new StreamingOctetReader_1.StreamingOctetReader();
|
|
11
|
+
}
|
|
12
|
+
push(uint8) {
|
|
13
|
+
this.reader.push(uint8);
|
|
14
|
+
}
|
|
15
|
+
readFrameHeader() {
|
|
16
|
+
try {
|
|
17
|
+
const reader = this.reader;
|
|
18
|
+
if (reader.size() < 2)
|
|
19
|
+
return undefined;
|
|
20
|
+
const b0 = reader.u8();
|
|
21
|
+
const b1 = reader.u8();
|
|
22
|
+
const fin = (b0 >>> 7);
|
|
23
|
+
const opcode = b0 & 0b1111;
|
|
24
|
+
const maskBit = b1 >>> 7;
|
|
25
|
+
let length = b1 & 0b01111111;
|
|
26
|
+
if (length === 126) {
|
|
27
|
+
if (reader.size() < 2)
|
|
28
|
+
return undefined;
|
|
29
|
+
length = (reader.u8() << 8) | reader.u8();
|
|
30
|
+
}
|
|
31
|
+
else if (length === 127) {
|
|
32
|
+
if (reader.size() < 8)
|
|
33
|
+
return undefined;
|
|
34
|
+
reader.skip(4);
|
|
35
|
+
length = reader.u32();
|
|
36
|
+
}
|
|
37
|
+
let mask;
|
|
38
|
+
if (maskBit) {
|
|
39
|
+
if (reader.size() < 4)
|
|
40
|
+
return undefined;
|
|
41
|
+
mask = [reader.u8(), reader.u8(), reader.u8(), reader.u8()];
|
|
42
|
+
}
|
|
43
|
+
if (opcode >= constants_1.WsFrameOpcode.MIN_CONTROL_OPCODE) {
|
|
44
|
+
switch (opcode) {
|
|
45
|
+
case constants_1.WsFrameOpcode.CLOSE: {
|
|
46
|
+
return new frames_1.WsCloseFrame(fin, opcode, length, mask, 0, '');
|
|
47
|
+
}
|
|
48
|
+
case constants_1.WsFrameOpcode.PING: {
|
|
49
|
+
if (length > 125)
|
|
50
|
+
throw new errors_1.WsFrameDecodingError();
|
|
51
|
+
const data = mask ? reader.bufXor(length, mask, 0) : reader.buf(length);
|
|
52
|
+
return new frames_1.WsPingFrame(fin, opcode, length, mask, data);
|
|
53
|
+
}
|
|
54
|
+
case constants_1.WsFrameOpcode.PONG: {
|
|
55
|
+
if (length > 125)
|
|
56
|
+
throw new errors_1.WsFrameDecodingError();
|
|
57
|
+
const data = mask ? reader.bufXor(length, mask, 0) : reader.buf(length);
|
|
58
|
+
return new frames_1.WsPongFrame(fin, opcode, length, mask, data);
|
|
59
|
+
}
|
|
60
|
+
default: {
|
|
61
|
+
throw new errors_1.WsFrameDecodingError();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return new frames_1.WsFrameHeader(fin, opcode, length, mask);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
if (err instanceof RangeError)
|
|
69
|
+
return undefined;
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
readFrameData(frame, remaining, dst, pos) {
|
|
74
|
+
const reader = this.reader;
|
|
75
|
+
const mask = frame.mask;
|
|
76
|
+
const readSize = Math.min(reader.size(), remaining);
|
|
77
|
+
if (!mask)
|
|
78
|
+
reader.copy(readSize, dst, pos);
|
|
79
|
+
else {
|
|
80
|
+
const alreadyRead = frame.length - remaining;
|
|
81
|
+
reader.copyXor(readSize, dst, pos, mask, alreadyRead);
|
|
82
|
+
}
|
|
83
|
+
return remaining - readSize;
|
|
84
|
+
}
|
|
85
|
+
copyFrameData(frame, dst, pos) {
|
|
86
|
+
const reader = this.reader;
|
|
87
|
+
const mask = frame.mask;
|
|
88
|
+
const readSize = frame.length;
|
|
89
|
+
if (!mask)
|
|
90
|
+
reader.copy(readSize, dst, pos);
|
|
91
|
+
else
|
|
92
|
+
reader.copyXor(readSize, dst, pos, mask, 0);
|
|
93
|
+
}
|
|
94
|
+
readCloseFrameData(frame) {
|
|
95
|
+
let length = frame.length;
|
|
96
|
+
if (length > 125)
|
|
97
|
+
throw new errors_1.WsFrameDecodingError();
|
|
98
|
+
let code = 0;
|
|
99
|
+
let reason = '';
|
|
100
|
+
if (length > 0) {
|
|
101
|
+
if (length < 2)
|
|
102
|
+
throw new errors_1.WsFrameDecodingError();
|
|
103
|
+
const reader = this.reader;
|
|
104
|
+
const mask = frame.mask;
|
|
105
|
+
const octet1 = reader.u8() ^ (mask ? mask[0] : 0);
|
|
106
|
+
const octet2 = reader.u8() ^ (mask ? mask[1] : 0);
|
|
107
|
+
code = (octet1 << 8) | octet2;
|
|
108
|
+
length -= 2;
|
|
109
|
+
if (length)
|
|
110
|
+
reason = reader.utf8(length, mask ?? [0, 0, 0, 0], 2);
|
|
111
|
+
}
|
|
112
|
+
frame.code = code;
|
|
113
|
+
frame.reason = reason;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.WsFrameDecoder = WsFrameDecoder;
|
|
117
|
+
//# sourceMappingURL=WsFrameDecoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WsFrameDecoder.js","sourceRoot":"","sources":["../../src/ws/WsFrameDecoder.ts"],"names":[],"mappings":";;;AAAA,6FAAwF;AACxF,2CAA0C;AAC1C,qCAA8C;AAC9C,qCAA+E;AAE/E,MAAa,cAAc;IAA3B;QACkB,WAAM,GAAG,IAAI,2CAAoB,EAAE,CAAC;IAgHtD,CAAC;IA9GQ,IAAI,CAAC,KAAiB;QAC3B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IAEM,eAAe;QACpB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,IAAI,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC;gBAAE,OAAO,SAAS,CAAC;YACxC,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,EAAE,CAAC;YACvB,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,E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C;YACjD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;YACxB,MAAM,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAClD,MAAM,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAClD,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC;YAC9B,MAAM,IAAI,CAAC,CAAC;YACZ,IAAI,MAAM;gBAAE,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACpE,CAAC;QACD,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC;QAClB,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;IACxB,CAAC;CACF;AAjHD,wCAiHC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { WsFrameOpcode } from './constants';
|
|
2
|
+
import type { IWriter, IWriterGrowable } from '@jsonjoy.com/util/lib/buffers';
|
|
3
|
+
export declare class WsFrameEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable> {
|
|
4
|
+
readonly writer: W;
|
|
5
|
+
constructor(writer?: W);
|
|
6
|
+
encodePing(data: Uint8Array | null): Uint8Array;
|
|
7
|
+
encodePong(data: Uint8Array | null): Uint8Array;
|
|
8
|
+
encodeClose(reason: string, code?: number): Uint8Array;
|
|
9
|
+
encodeHdr(fin: 0 | 1, opcode: WsFrameOpcode, length: number, mask: number): Uint8Array;
|
|
10
|
+
encodeDataMsgHdrFast(length: number): Uint8Array;
|
|
11
|
+
writePing(data: Uint8Array | null): void;
|
|
12
|
+
writePong(data: Uint8Array | null): void;
|
|
13
|
+
writeClose(reason: string, code?: number): void;
|
|
14
|
+
writeHdr(fin: 0 | 1, opcode: WsFrameOpcode, length: number, mask: number): void;
|
|
15
|
+
writeDataMsgHdrFast(length: number): void;
|
|
16
|
+
writeBufXor(buf: Uint8Array, mask: number): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WsFrameEncoder = void 0;
|
|
4
|
+
const Writer_1 = require("@jsonjoy.com/util/lib/buffers/Writer");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const maskBuf = new Uint8Array(4);
|
|
8
|
+
const maskBufView = new DataView(maskBuf.buffer, maskBuf.byteOffset, maskBuf.byteLength);
|
|
9
|
+
class WsFrameEncoder {
|
|
10
|
+
constructor(writer = new Writer_1.Writer()) {
|
|
11
|
+
this.writer = writer;
|
|
12
|
+
}
|
|
13
|
+
encodePing(data) {
|
|
14
|
+
this.writePing(data);
|
|
15
|
+
return this.writer.flush();
|
|
16
|
+
}
|
|
17
|
+
encodePong(data) {
|
|
18
|
+
this.writePong(data);
|
|
19
|
+
return this.writer.flush();
|
|
20
|
+
}
|
|
21
|
+
encodeClose(reason, code = 0) {
|
|
22
|
+
this.writeClose(reason, code);
|
|
23
|
+
return this.writer.flush();
|
|
24
|
+
}
|
|
25
|
+
encodeHdr(fin, opcode, length, mask) {
|
|
26
|
+
this.writeHdr(fin, opcode, length, mask);
|
|
27
|
+
return this.writer.flush();
|
|
28
|
+
}
|
|
29
|
+
encodeDataMsgHdrFast(length) {
|
|
30
|
+
this.writeDataMsgHdrFast(length);
|
|
31
|
+
return this.writer.flush();
|
|
32
|
+
}
|
|
33
|
+
writePing(data) {
|
|
34
|
+
let length = 0;
|
|
35
|
+
if (data && (length = data.length)) {
|
|
36
|
+
this.writeHdr(1, constants_1.WsFrameOpcode.PING, length, 0);
|
|
37
|
+
this.writer.buf(data, length);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this.writeHdr(1, constants_1.WsFrameOpcode.PING, 0, 0);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
writePong(data) {
|
|
44
|
+
let length = 0;
|
|
45
|
+
if (data && (length = data.length)) {
|
|
46
|
+
this.writeHdr(1, constants_1.WsFrameOpcode.PONG, length, 0);
|
|
47
|
+
this.writer.buf(data, length);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.writeHdr(1, constants_1.WsFrameOpcode.PONG, 0, 0);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
writeClose(reason, code = 0) {
|
|
54
|
+
if (reason || code) {
|
|
55
|
+
const reasonLength = reason.length;
|
|
56
|
+
const length = 2 + reasonLength;
|
|
57
|
+
const writer = this.writer;
|
|
58
|
+
writer.ensureCapacity(2 +
|
|
59
|
+
2 +
|
|
60
|
+
reasonLength * 4);
|
|
61
|
+
const lengthX = writer.x + 1;
|
|
62
|
+
this.writeHdr(1, constants_1.WsFrameOpcode.CLOSE, length, 0);
|
|
63
|
+
writer.u16(code);
|
|
64
|
+
if (reasonLength) {
|
|
65
|
+
const utf8Length = writer.utf8(reason);
|
|
66
|
+
if (utf8Length !== reasonLength) {
|
|
67
|
+
if (utf8Length > 126 - 2)
|
|
68
|
+
throw new errors_1.WsFrameEncodingError();
|
|
69
|
+
writer.uint8[lengthX] = (writer.uint8[lengthX] & 0b10000000) | (utf8Length + 2);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
this.writeHdr(1, constants_1.WsFrameOpcode.CLOSE, 0, 0);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
writeHdr(fin, opcode, length, mask) {
|
|
78
|
+
const octet1 = (fin << 7) | opcode;
|
|
79
|
+
const maskBit = mask ? 0b10000000 : 0b00000000;
|
|
80
|
+
const writer = this.writer;
|
|
81
|
+
if (length < 126) {
|
|
82
|
+
const octet2 = maskBit | length;
|
|
83
|
+
writer.u16((octet1 << 8) | octet2);
|
|
84
|
+
}
|
|
85
|
+
else if (length < 0x10000) {
|
|
86
|
+
const octet2 = maskBit | 126;
|
|
87
|
+
writer.u32(((octet1 << 8) | octet2) * 0x10000 + length);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const octet2 = maskBit | 127;
|
|
91
|
+
writer.u16((octet1 << 8) | octet2);
|
|
92
|
+
writer.u32(0);
|
|
93
|
+
writer.u32(length);
|
|
94
|
+
}
|
|
95
|
+
if (mask)
|
|
96
|
+
writer.u32(mask);
|
|
97
|
+
}
|
|
98
|
+
writeDataMsgHdrFast(length) {
|
|
99
|
+
const writer = this.writer;
|
|
100
|
+
if (length < 126) {
|
|
101
|
+
writer.u16(33280 + length);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (length < 0x10000) {
|
|
105
|
+
writer.u32(2189295616 + length);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
writer.u16(33407);
|
|
109
|
+
writer.u32(0);
|
|
110
|
+
writer.u32(length);
|
|
111
|
+
}
|
|
112
|
+
writeBufXor(buf, mask) {
|
|
113
|
+
maskBufView.setUint32(0, mask, false);
|
|
114
|
+
const writer = this.writer;
|
|
115
|
+
const length = buf.length;
|
|
116
|
+
writer.ensureCapacity(length);
|
|
117
|
+
let x = writer.x;
|
|
118
|
+
const uint8 = writer.uint8;
|
|
119
|
+
for (let i = 0; i < length; i++)
|
|
120
|
+
uint8[x++] = buf[i] ^ maskBuf[i & 3];
|
|
121
|
+
writer.x = x;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
exports.WsFrameEncoder = WsFrameEncoder;
|
|
125
|
+
//# sourceMappingURL=WsFrameEncoder.js.map
|
|
@@ -0,0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AAM,CAAC;QAC1B,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QAC9B,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;QACjB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE;YAAE,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACtE,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC;IACf,CAAC;CACF;AArHD,wCAqHC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WsFrameOpcode = void 0;
|
|
4
|
+
var WsFrameOpcode;
|
|
5
|
+
(function (WsFrameOpcode) {
|
|
6
|
+
WsFrameOpcode[WsFrameOpcode["CONTINUE"] = 0] = "CONTINUE";
|
|
7
|
+
WsFrameOpcode[WsFrameOpcode["TEXT"] = 1] = "TEXT";
|
|
8
|
+
WsFrameOpcode[WsFrameOpcode["BINARY"] = 2] = "BINARY";
|
|
9
|
+
WsFrameOpcode[WsFrameOpcode["MIN_CONTROL_OPCODE"] = 8] = "MIN_CONTROL_OPCODE";
|
|
10
|
+
WsFrameOpcode[WsFrameOpcode["CLOSE"] = 8] = "CLOSE";
|
|
11
|
+
WsFrameOpcode[WsFrameOpcode["PING"] = 9] = "PING";
|
|
12
|
+
WsFrameOpcode[WsFrameOpcode["PONG"] = 10] = "PONG";
|
|
13
|
+
})(WsFrameOpcode || (exports.WsFrameOpcode = WsFrameOpcode = {}));
|
|
14
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/ws/constants.ts"],"names":[],"mappings":";;;AAAA,IAAY,aAeX;AAfD,WAAY,aAAa;IAEvB,yDAAY,CAAA;IAGZ,iDAAQ,CAAA;IACR,qDAAU,CAAA;IAIV,6EAAsB,CAAA;IAEtB,mDAAS,CAAA;IACT,iDAAQ,CAAA;IACR,kDAAS,CAAA;AACX,CAAC,EAfW,aAAa,6BAAb,aAAa,QAexB"}
|
package/lib/ws/errors.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WsFrameEncodingError = exports.WsFrameDecodingError = void 0;
|
|
4
|
+
class WsFrameDecodingError extends Error {
|
|
5
|
+
constructor() {
|
|
6
|
+
super('WS_FRAME_DECODING');
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.WsFrameDecodingError = WsFrameDecodingError;
|
|
10
|
+
class WsFrameEncodingError extends Error {
|
|
11
|
+
constructor() {
|
|
12
|
+
super('WS_FRAME_ENCODING');
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.WsFrameEncodingError = WsFrameEncodingError;
|
|
16
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/ws/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,oBAAqB,SAAQ,KAAK;IAC7C;QACE,KAAK,CAAC,mBAAmB,CAAC,CAAC;IAC7B,CAAC;CACF;AAJD,oDAIC;AAED,MAAa,oBAAqB,SAAQ,KAAK;IAC7C;QACE,KAAK,CAAC,mBAAmB,CAAC,CAAC;IAC7B,CAAC;CACF;AAJD,oDAIC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare class WsFrameHeader {
|
|
2
|
+
readonly fin: 0 | 1;
|
|
3
|
+
readonly opcode: number;
|
|
4
|
+
readonly length: number;
|
|
5
|
+
readonly mask: undefined | [number, number, number, number];
|
|
6
|
+
constructor(fin: 0 | 1, opcode: number, length: number, mask: undefined | [number, number, number, number]);
|
|
7
|
+
}
|
|
8
|
+
export declare class WsPingFrame extends WsFrameHeader {
|
|
9
|
+
readonly data: Uint8Array;
|
|
10
|
+
constructor(fin: 0 | 1, opcode: number, length: number, mask: undefined | [number, number, number, number], data: Uint8Array);
|
|
11
|
+
}
|
|
12
|
+
export declare class WsPongFrame extends WsFrameHeader {
|
|
13
|
+
readonly data: Uint8Array;
|
|
14
|
+
constructor(fin: 0 | 1, opcode: number, length: number, mask: undefined | [number, number, number, number], data: Uint8Array);
|
|
15
|
+
}
|
|
16
|
+
export declare class WsCloseFrame extends WsFrameHeader {
|
|
17
|
+
code: number;
|
|
18
|
+
reason: string;
|
|
19
|
+
constructor(fin: 0 | 1, opcode: number, length: number, mask: undefined | [number, number, number, number], code: number, reason: string);
|
|
20
|
+
}
|
package/lib/ws/frames.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WsCloseFrame = exports.WsPongFrame = exports.WsPingFrame = exports.WsFrameHeader = void 0;
|
|
4
|
+
class WsFrameHeader {
|
|
5
|
+
constructor(fin, opcode, length, mask) {
|
|
6
|
+
this.fin = fin;
|
|
7
|
+
this.opcode = opcode;
|
|
8
|
+
this.length = length;
|
|
9
|
+
this.mask = mask;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.WsFrameHeader = WsFrameHeader;
|
|
13
|
+
class WsPingFrame extends WsFrameHeader {
|
|
14
|
+
constructor(fin, opcode, length, mask, data) {
|
|
15
|
+
super(fin, opcode, length, mask);
|
|
16
|
+
this.data = data;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.WsPingFrame = WsPingFrame;
|
|
20
|
+
class WsPongFrame extends WsFrameHeader {
|
|
21
|
+
constructor(fin, opcode, length, mask, data) {
|
|
22
|
+
super(fin, opcode, length, mask);
|
|
23
|
+
this.data = data;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.WsPongFrame = WsPongFrame;
|
|
27
|
+
class WsCloseFrame extends WsFrameHeader {
|
|
28
|
+
constructor(fin, opcode, length, mask, code, reason) {
|
|
29
|
+
super(fin, opcode, length, mask);
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.reason = reason;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.WsCloseFrame = WsCloseFrame;
|
|
35
|
+
//# sourceMappingURL=frames.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frames.js","sourceRoot":"","sources":["../../src/ws/frames.ts"],"names":[],"mappings":";;;AAAA,MAAa,aAAa;IACxB,YACkB,GAAU,EACV,MAAc,EACd,MAAc,EACd,IAAkD;QAHlD,QAAG,GAAH,GAAG,CAAO;QACV,WAAM,GAAN,MAAM,CAAQ;QACd,WAAM,GAAN,MAAM,CAAQ;QACd,SAAI,GAAJ,IAAI,CAA8C;IACjE,CAAC;CACL;AAPD,sCAOC;AAED,MAAa,WAAY,SAAQ,aAAa;IAC5C,YACE,GAAU,EACV,MAAc,EACd,MAAc,EACd,IAAkD,EAClC,IAAgB;QAEhC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QAFjB,SAAI,GAAJ,IAAI,CAAY;IAGlC,CAAC;CACF;AAVD,kCAUC;AAED,MAAa,WAAY,SAAQ,aAAa;IAC5C,YACE,GAAU,EACV,MAAc,EACd,MAAc,EACd,IAAkD,EAClC,IAAgB;QAEhC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QAFjB,SAAI,GAAJ,IAAI,CAAY;IAGlC,CAAC;CACF;AAVD,kCAUC;AAED,MAAa,YAAa,SAAQ,aAAa;IAC7C,YACE,GAAU,EACV,MAAc,EACd,MAAc,EACd,IAAkD,EAC3C,IAAY,EACZ,MAAc;QAErB,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QAH1B,SAAI,GAAJ,IAAI,CAAQ;QACZ,WAAM,GAAN,MAAM,CAAQ;IAGvB,CAAC;CACF;AAXD,oCAWC"}
|
package/lib/ws/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./constants"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./errors"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./frames"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./WsFrameDecoder"), exports);
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ws/index.ts"],"names":[],"mappings":";;;AAAA,sDAA4B;AAC5B,mDAAyB;AACzB,mDAAyB;AACzB,2DAAiC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsonjoy.com/json-pack",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.0",
|
|
4
4
|
"description": "High-performance JSON serialization library",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "streamich",
|
|
@@ -115,7 +115,8 @@
|
|
|
115
115
|
"tslint": "^6.1.3",
|
|
116
116
|
"tslint-config-common": "^1.6.2",
|
|
117
117
|
"typedoc": "^0.25.12",
|
|
118
|
-
"typescript": "^5.3.3"
|
|
118
|
+
"typescript": "^5.3.3",
|
|
119
|
+
"websocket": "^1.0.35"
|
|
119
120
|
},
|
|
120
121
|
"jest": {
|
|
121
122
|
"verbose": true,
|