stream-protocol-toolkit 0.1.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/LICENSE +21 -0
- package/README.md +6 -0
- package/dist/Application/Application.d.ts +39 -0
- package/dist/Application/Application.js +156 -0
- package/dist/Application/ApplicationEventEnum.d.ts +6 -0
- package/dist/Application/ApplicationEventEnum.js +10 -0
- package/dist/Application/ApplicationEventMap.d.ts +15 -0
- package/dist/Application/ApplicationEventMap.js +2 -0
- package/dist/Application/ApplicationOptions.d.ts +12 -0
- package/dist/Application/ApplicationOptions.js +2 -0
- package/dist/Application/ApplicationState.d.ts +1 -0
- package/dist/Application/ApplicationState.js +2 -0
- package/dist/Application/ApplicationUnknownProtocolHandler.d.ts +5 -0
- package/dist/Application/ApplicationUnknownProtocolHandler.js +2 -0
- package/dist/Application/create-stream-context/CreateStreamContextInputDTO.d.ts +3 -0
- package/dist/Application/create-stream-context/CreateStreamContextInputDTO.js +2 -0
- package/dist/Application/create-stream-context/CreateStreamContextOutputDTO.d.ts +5 -0
- package/dist/Application/create-stream-context/CreateStreamContextOutputDTO.js +2 -0
- package/dist/Application/create-stream-context/index.d.ts +2 -0
- package/dist/Application/create-stream-context/index.js +2 -0
- package/dist/Application/factories/application-state-factory/ApplicationStateFactory.d.ts +5 -0
- package/dist/Application/factories/application-state-factory/ApplicationStateFactory.js +2 -0
- package/dist/Application/factories/application-state-factory/ApplicationStateFactoryInputDTO.d.ts +2 -0
- package/dist/Application/factories/application-state-factory/ApplicationStateFactoryInputDTO.js +2 -0
- package/dist/Application/factories/application-state-factory/ApplicationStateFactoryOutputDTO.d.ts +4 -0
- package/dist/Application/factories/application-state-factory/ApplicationStateFactoryOutputDTO.js +2 -0
- package/dist/Application/factories/application-state-factory/application-state-factory.d.ts +5 -0
- package/dist/Application/factories/application-state-factory/application-state-factory.js +8 -0
- package/dist/Application/factories/application-state-factory/index.d.ts +4 -0
- package/dist/Application/factories/application-state-factory/index.js +5 -0
- package/dist/Application/factories/index.d.ts +6 -0
- package/dist/Application/factories/index.js +22 -0
- package/dist/Application/factories/stream-buffer-factory/StreamBufferFactory.d.ts +5 -0
- package/dist/Application/factories/stream-buffer-factory/StreamBufferFactory.js +2 -0
- package/dist/Application/factories/stream-buffer-factory/StreamBufferFactoryInputDTO.d.ts +5 -0
- package/dist/Application/factories/stream-buffer-factory/StreamBufferFactoryInputDTO.js +2 -0
- package/dist/Application/factories/stream-buffer-factory/StreamBufferFactoryOutputDTO.d.ts +5 -0
- package/dist/Application/factories/stream-buffer-factory/StreamBufferFactoryOutputDTO.js +2 -0
- package/dist/Application/factories/stream-buffer-factory/index.d.ts +4 -0
- package/dist/Application/factories/stream-buffer-factory/index.js +5 -0
- package/dist/Application/factories/stream-buffer-factory/stream-buffer-factory.d.ts +5 -0
- package/dist/Application/factories/stream-buffer-factory/stream-buffer-factory.js +10 -0
- package/dist/Application/factories/stream-context-factory/StreamContextFactory.d.ts +5 -0
- package/dist/Application/factories/stream-context-factory/StreamContextFactory.js +2 -0
- package/dist/Application/factories/stream-context-factory/StreamContextFactoryInputDTO.d.ts +3 -0
- package/dist/Application/factories/stream-context-factory/StreamContextFactoryInputDTO.js +2 -0
- package/dist/Application/factories/stream-context-factory/StreamContextFactoryOutputDTO.d.ts +5 -0
- package/dist/Application/factories/stream-context-factory/StreamContextFactoryOutputDTO.js +2 -0
- package/dist/Application/factories/stream-context-factory/index.d.ts +4 -0
- package/dist/Application/factories/stream-context-factory/index.js +5 -0
- package/dist/Application/factories/stream-context-factory/stream-context-factory.d.ts +5 -0
- package/dist/Application/factories/stream-context-factory/stream-context-factory.js +12 -0
- package/dist/Application/factories/stream-context-state-factory/StreamContextStateFactory.d.ts +5 -0
- package/dist/Application/factories/stream-context-state-factory/StreamContextStateFactory.js +2 -0
- package/dist/Application/factories/stream-context-state-factory/StreamContextStateFactoryInputDTO.d.ts +2 -0
- package/dist/Application/factories/stream-context-state-factory/StreamContextStateFactoryInputDTO.js +2 -0
- package/dist/Application/factories/stream-context-state-factory/StreamContextStateFactoryOutputDTO.d.ts +4 -0
- package/dist/Application/factories/stream-context-state-factory/StreamContextStateFactoryOutputDTO.js +2 -0
- package/dist/Application/factories/stream-context-state-factory/index.d.ts +4 -0
- package/dist/Application/factories/stream-context-state-factory/index.js +5 -0
- package/dist/Application/factories/stream-context-state-factory/stream-context-state-factory.d.ts +5 -0
- package/dist/Application/factories/stream-context-state-factory/stream-context-state-factory.js +8 -0
- package/dist/Application/factories/stream-protocol-processor-factory/StreamProtocolProcessorFactory.d.ts +5 -0
- package/dist/Application/factories/stream-protocol-processor-factory/StreamProtocolProcessorFactory.js +2 -0
- package/dist/Application/factories/stream-protocol-processor-factory/StreamProtocolProcessorFactoryInputDTO.d.ts +2 -0
- package/dist/Application/factories/stream-protocol-processor-factory/StreamProtocolProcessorFactoryInputDTO.js +2 -0
- package/dist/Application/factories/stream-protocol-processor-factory/StreamProtocolProcessorFactoryOutputDTO.d.ts +5 -0
- package/dist/Application/factories/stream-protocol-processor-factory/StreamProtocolProcessorFactoryOutputDTO.js +2 -0
- package/dist/Application/factories/stream-protocol-processor-factory/index.d.ts +4 -0
- package/dist/Application/factories/stream-protocol-processor-factory/index.js +5 -0
- package/dist/Application/factories/stream-protocol-processor-factory/stream-protocol-processor-factory.d.ts +5 -0
- package/dist/Application/factories/stream-protocol-processor-factory/stream-protocol-processor-factory.js +13 -0
- package/dist/Application/factories/transform-stream-handler-factory/TransformStreamHandlerFactory.d.ts +5 -0
- package/dist/Application/factories/transform-stream-handler-factory/TransformStreamHandlerFactory.js +2 -0
- package/dist/Application/factories/transform-stream-handler-factory/TransformStreamHandlerFactoryInputDTO.d.ts +5 -0
- package/dist/Application/factories/transform-stream-handler-factory/TransformStreamHandlerFactoryInputDTO.js +2 -0
- package/dist/Application/factories/transform-stream-handler-factory/TransformStreamHandlerFactoryOutputDTO.d.ts +4 -0
- package/dist/Application/factories/transform-stream-handler-factory/TransformStreamHandlerFactoryOutputDTO.js +2 -0
- package/dist/Application/factories/transform-stream-handler-factory/index.d.ts +4 -0
- package/dist/Application/factories/transform-stream-handler-factory/index.js +5 -0
- package/dist/Application/factories/transform-stream-handler-factory/transform-stream-handler-factory.d.ts +5 -0
- package/dist/Application/factories/transform-stream-handler-factory/transform-stream-handler-factory.js +65 -0
- package/dist/Application/index.d.ts +9 -0
- package/dist/Application/index.js +24 -0
- package/dist/Application/start-stream/StartStreamInputDTO.d.ts +5 -0
- package/dist/Application/start-stream/StartStreamInputDTO.js +2 -0
- package/dist/Application/start-stream/StartStreamOutputDTO.d.ts +2 -0
- package/dist/Application/start-stream/StartStreamOutputDTO.js +2 -0
- package/dist/Application/start-stream/index.d.ts +2 -0
- package/dist/Application/start-stream/index.js +2 -0
- package/dist/Protocol/Protocol.d.ts +12 -0
- package/dist/Protocol/Protocol.js +84 -0
- package/dist/Protocol/ProtocolInitValue.d.ts +4 -0
- package/dist/Protocol/ProtocolInitValue.js +2 -0
- package/dist/Protocol/ProtocolOptions.d.ts +1 -0
- package/dist/Protocol/ProtocolOptions.js +2 -0
- package/dist/Protocol/index.d.ts +3 -0
- package/dist/Protocol/index.js +5 -0
- package/dist/ProtocolHandler/ProtocolHandler.d.ts +6 -0
- package/dist/ProtocolHandler/ProtocolHandler.js +6 -0
- package/dist/ProtocolHandler/ProtocolHandlerOptions.d.ts +1 -0
- package/dist/ProtocolHandler/ProtocolHandlerOptions.js +2 -0
- package/dist/ProtocolHandler/handle/HandleInputDTO.d.ts +7 -0
- package/dist/ProtocolHandler/handle/HandleInputDTO.js +2 -0
- package/dist/ProtocolHandler/handle/HandleOutputDTO.d.ts +3 -0
- package/dist/ProtocolHandler/handle/HandleOutputDTO.js +2 -0
- package/dist/ProtocolHandler/handle/index.d.ts +2 -0
- package/dist/ProtocolHandler/handle/index.js +2 -0
- package/dist/ProtocolHandler/index.d.ts +3 -0
- package/dist/ProtocolHandler/index.js +20 -0
- package/dist/RawPacket/RawPacket.d.ts +5 -0
- package/dist/RawPacket/RawPacket.js +2 -0
- package/dist/RawPacket/RawPacketHeader.d.ts +4 -0
- package/dist/RawPacket/RawPacketHeader.js +2 -0
- package/dist/RawPacket/index.d.ts +2 -0
- package/dist/RawPacket/index.js +2 -0
- package/dist/StreamBuffer/StreamBuffer.d.ts +14 -0
- package/dist/StreamBuffer/StreamBuffer.js +59 -0
- package/dist/StreamBuffer/StreamBufferOptions.d.ts +5 -0
- package/dist/StreamBuffer/StreamBufferOptions.js +2 -0
- package/dist/StreamBuffer/index.d.ts +2 -0
- package/dist/StreamBuffer/index.js +5 -0
- package/dist/StreamContext/StreamContext.d.ts +17 -0
- package/dist/StreamContext/StreamContext.js +18 -0
- package/dist/StreamContext/StreamContextOptions.d.ts +12 -0
- package/dist/StreamContext/StreamContextOptions.js +2 -0
- package/dist/StreamContext/index.d.ts +2 -0
- package/dist/StreamContext/index.js +5 -0
- package/dist/StreamProtocolProcessor/StreamProtocolProcessor.d.ts +28 -0
- package/dist/StreamProtocolProcessor/StreamProtocolProcessor.js +137 -0
- package/dist/StreamProtocolProcessor/StreamProtocolProcessorOptions.d.ts +10 -0
- package/dist/StreamProtocolProcessor/StreamProtocolProcessorOptions.js +2 -0
- package/dist/StreamProtocolProcessor/index.d.ts +2 -0
- package/dist/StreamProtocolProcessor/index.js +5 -0
- package/dist/errors/StreamProtocolToolkitAlreadyExistsError.d.ts +3 -0
- package/dist/errors/StreamProtocolToolkitAlreadyExistsError.js +7 -0
- package/dist/errors/StreamProtocolToolkitError.d.ts +3 -0
- package/dist/errors/StreamProtocolToolkitError.js +13 -0
- package/dist/errors/StreamProtocolToolkitNotImplementedError.d.ts +3 -0
- package/dist/errors/StreamProtocolToolkitNotImplementedError.js +7 -0
- package/dist/errors/StreamProtocolToolkitTypeError.d.ts +3 -0
- package/dist/errors/StreamProtocolToolkitTypeError.js +7 -0
- package/dist/errors/index.d.ts +4 -0
- package/dist/errors/index.js +11 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +25 -0
- package/dist/utils/any-signal.d.ts +1 -0
- package/dist/utils/any-signal.js +34 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +13 -0
- package/dist/utils/is-plain-object.d.ts +1 -0
- package/dist/utils/is-plain-object.js +13 -0
- package/dist/utils/is-readable-stream-alive.d.ts +2 -0
- package/dist/utils/is-readable-stream-alive.js +21 -0
- package/dist/utils/is-writable-stream-alive.d.ts +2 -0
- package/dist/utils/is-writable-stream-alive.js +24 -0
- package/dist/utils/read-paused-stream-chunk.d.ts +3 -0
- package/dist/utils/read-paused-stream-chunk.js +51 -0
- package/package.json +48 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamContext = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
5
|
+
class StreamContext {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.input = options.input;
|
|
8
|
+
this.output = options.output;
|
|
9
|
+
this.eventBus = new events_1.EventEmitter();
|
|
10
|
+
this.application = options.application;
|
|
11
|
+
this.abortController = new AbortController();
|
|
12
|
+
this.inputTransform = options.inputTransform;
|
|
13
|
+
this.outputTransform = options.outputTransform;
|
|
14
|
+
this.streamContextState = options.streamContextState;
|
|
15
|
+
this.streamProtocolProcessor = options.streamProtocolProcessor;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.StreamContext = StreamContext;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Application } from '../Application';
|
|
2
|
+
import type { Readable, Writable, Transform } from 'stream';
|
|
3
|
+
import type { StreamProtocolProcessor } from '../StreamProtocolProcessor';
|
|
4
|
+
export type StreamContextOptions<ISTREAM extends Readable, OSTREAM extends Writable, AS extends object, SCS extends object> = {
|
|
5
|
+
input: ISTREAM;
|
|
6
|
+
output?: OSTREAM;
|
|
7
|
+
streamContextState: SCS;
|
|
8
|
+
inputTransform?: Transform[];
|
|
9
|
+
outputTransform?: Transform[];
|
|
10
|
+
application: Application<ISTREAM, OSTREAM, AS, SCS>;
|
|
11
|
+
streamProtocolProcessor: StreamProtocolProcessor<ISTREAM, OSTREAM, AS, SCS>;
|
|
12
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamContext = void 0;
|
|
4
|
+
var StreamContext_1 = require("./StreamContext");
|
|
5
|
+
Object.defineProperty(exports, "StreamContext", { enumerable: true, get: function () { return StreamContext_1.StreamContext; } });
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import { StreamBuffer } from '../StreamBuffer';
|
|
3
|
+
import type { Application } from '../Application';
|
|
4
|
+
import type { Readable, Writable } from 'stream';
|
|
5
|
+
import type { RawPacket, RawPacketHeader } from '../RawPacket';
|
|
6
|
+
import type { StreamProtocolProcessorOptions } from './StreamProtocolProcessorOptions';
|
|
7
|
+
export declare class StreamProtocolProcessor<ISTREAM extends Readable, OSTREAM extends Writable, AS extends object, SCS extends object> {
|
|
8
|
+
readonly packetMaxLength: number;
|
|
9
|
+
readonly packetOpcodeSize: number;
|
|
10
|
+
readonly packetLengthSize: number;
|
|
11
|
+
readonly streamBuffer: StreamBuffer;
|
|
12
|
+
readonly application: Application<ISTREAM, OSTREAM, AS, SCS>;
|
|
13
|
+
constructor(options: StreamProtocolProcessorOptions<ISTREAM, OSTREAM, AS, SCS>);
|
|
14
|
+
pushChunk(chunk: Buffer): this;
|
|
15
|
+
assertState(): void;
|
|
16
|
+
readPacketOpcode(buffer: StreamBuffer): number;
|
|
17
|
+
writePacketOpcode(buffer: StreamBuffer, value: number, unshift?: boolean): this;
|
|
18
|
+
packetOpcodeIsReadable(buffer: StreamBuffer): boolean;
|
|
19
|
+
readPacketLength(buffer: StreamBuffer): number;
|
|
20
|
+
writePacketLength(buffer: StreamBuffer, value: number, unshift?: boolean): this;
|
|
21
|
+
packetLengthIsReadable(buffer: StreamBuffer): boolean;
|
|
22
|
+
readPacketHeader(buffer: StreamBuffer): RawPacketHeader;
|
|
23
|
+
writePacketHeader(buffer: StreamBuffer, packetHeader: RawPacketHeader, unshift?: boolean): this;
|
|
24
|
+
packetHeaderIsReadable(buffer: StreamBuffer): boolean;
|
|
25
|
+
readPacket(buffer: StreamBuffer): RawPacket;
|
|
26
|
+
writePacket(buffer: StreamBuffer, packet: RawPacket, unshift?: boolean): this;
|
|
27
|
+
packetIsReadable(buffer: StreamBuffer): boolean;
|
|
28
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamProtocolProcessor = void 0;
|
|
4
|
+
const StreamBuffer_1 = require("../StreamBuffer");
|
|
5
|
+
const extended_buffer_1 = require("extended-buffer");
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
|
+
const DEFAULT_PACKET_OPCODE_SIZE = 1;
|
|
8
|
+
const DEFAULT_PACKET_LENGTH_SIZE = 1;
|
|
9
|
+
const DEFAULT_PACKET_MAX_LENGTH = 16 * 1024;
|
|
10
|
+
class StreamProtocolProcessor {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
var _a, _b, _c;
|
|
13
|
+
this.application = options.application;
|
|
14
|
+
this.streamBuffer = options.streamBuffer;
|
|
15
|
+
this.packetMaxLength = (_a = options.packetMaxLength) !== null && _a !== void 0 ? _a : DEFAULT_PACKET_MAX_LENGTH;
|
|
16
|
+
this.packetOpcodeSize = (_b = options.packetOpcodeSize) !== null && _b !== void 0 ? _b : DEFAULT_PACKET_OPCODE_SIZE;
|
|
17
|
+
this.packetLengthSize = (_c = options.packetLengthSize) !== null && _c !== void 0 ? _c : DEFAULT_PACKET_LENGTH_SIZE;
|
|
18
|
+
}
|
|
19
|
+
pushChunk(chunk) {
|
|
20
|
+
this.streamBuffer.writeNativeBuffer(chunk);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
assertState() {
|
|
24
|
+
if (!this.packetHeaderIsReadable(this.streamBuffer)) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const savedPointer = this.streamBuffer.getPointer();
|
|
28
|
+
const packetHeader = this.readPacketHeader(this.streamBuffer);
|
|
29
|
+
this.streamBuffer.setPointer(savedPointer);
|
|
30
|
+
if (packetHeader.length > this.packetMaxLength) {
|
|
31
|
+
throw new errors_1.StreamProtocolToolkitError('MAX_PACKET_SIZE_EXCEEDED');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
readPacketOpcode(buffer) {
|
|
35
|
+
return buffer.readUIntBE(this.packetOpcodeSize);
|
|
36
|
+
}
|
|
37
|
+
writePacketOpcode(buffer, value, unshift) {
|
|
38
|
+
buffer.writeUIntBE(value, this.packetOpcodeSize, unshift);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
packetOpcodeIsReadable(buffer) {
|
|
42
|
+
return buffer.isReadable(this.packetOpcodeSize);
|
|
43
|
+
}
|
|
44
|
+
readPacketLength(buffer) {
|
|
45
|
+
return buffer.readUIntBE(this.packetLengthSize);
|
|
46
|
+
}
|
|
47
|
+
writePacketLength(buffer, value, unshift) {
|
|
48
|
+
buffer.writeUIntBE(value, this.packetLengthSize, unshift);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
packetLengthIsReadable(buffer) {
|
|
52
|
+
return buffer.isReadable(this.packetLengthSize);
|
|
53
|
+
}
|
|
54
|
+
readPacketHeader(buffer) {
|
|
55
|
+
const savedPointer = buffer.getPointer();
|
|
56
|
+
try {
|
|
57
|
+
const opcode = this.readPacketOpcode(buffer);
|
|
58
|
+
const length = this.readPacketLength(buffer);
|
|
59
|
+
return {
|
|
60
|
+
opcode,
|
|
61
|
+
length
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
buffer.setPointer(savedPointer);
|
|
66
|
+
throw e;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
writePacketHeader(buffer, packetHeader, unshift) {
|
|
70
|
+
if (unshift) {
|
|
71
|
+
const headerSize = this.packetOpcodeSize + this.packetLengthSize;
|
|
72
|
+
buffer.allocStart(headerSize);
|
|
73
|
+
this.writePacketLength(buffer, packetHeader.length, true).writePacketOpcode(buffer, packetHeader.opcode, true);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
this.writePacketOpcode(buffer, packetHeader.opcode).writePacketLength(buffer, packetHeader.length);
|
|
77
|
+
}
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
packetHeaderIsReadable(buffer) {
|
|
81
|
+
const headerSize = this.packetOpcodeSize + this.packetLengthSize;
|
|
82
|
+
return buffer.isReadable(headerSize);
|
|
83
|
+
}
|
|
84
|
+
readPacket(buffer) {
|
|
85
|
+
const savedPointer = buffer.getPointer();
|
|
86
|
+
try {
|
|
87
|
+
const packetHeader = this.readPacketHeader(buffer);
|
|
88
|
+
const currentPointer = buffer.getPointer();
|
|
89
|
+
const payload = new StreamBuffer_1.StreamBuffer({
|
|
90
|
+
initNativeBuffer: (0, extended_buffer_1.nativeBufferSubarray)(buffer.nativeBufferView, currentPointer, currentPointer + packetHeader.length)
|
|
91
|
+
});
|
|
92
|
+
buffer.offset(packetHeader.length);
|
|
93
|
+
return {
|
|
94
|
+
opcode: packetHeader.opcode,
|
|
95
|
+
length: packetHeader.length,
|
|
96
|
+
payload
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
buffer.setPointer(savedPointer);
|
|
101
|
+
throw e;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
writePacket(buffer, packet, unshift) {
|
|
105
|
+
if (unshift) {
|
|
106
|
+
const headerSize = this.packetOpcodeSize + this.packetLengthSize;
|
|
107
|
+
buffer.allocStart(headerSize + packet.payload.length);
|
|
108
|
+
buffer.writeNativeBuffer(packet.payload.nativeBufferView, true);
|
|
109
|
+
this.writePacketHeader(buffer, {
|
|
110
|
+
opcode: packet.opcode,
|
|
111
|
+
length: packet.payload.length
|
|
112
|
+
}, true);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
this.writePacketHeader(buffer, {
|
|
116
|
+
opcode: packet.opcode,
|
|
117
|
+
length: packet.payload.length
|
|
118
|
+
});
|
|
119
|
+
buffer.writeNativeBuffer(packet.payload.nativeBufferView);
|
|
120
|
+
}
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
packetIsReadable(buffer) {
|
|
124
|
+
const savedPointer = buffer.getPointer();
|
|
125
|
+
try {
|
|
126
|
+
if (!this.packetHeaderIsReadable(buffer)) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
const packetHeader = this.readPacketHeader(buffer);
|
|
130
|
+
return buffer.isReadable(packetHeader.length);
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
buffer.setPointer(savedPointer);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.StreamProtocolProcessor = StreamProtocolProcessor;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Readable, Writable } from 'stream';
|
|
2
|
+
import type { Application } from '../Application';
|
|
3
|
+
import type { StreamBuffer } from '../StreamBuffer';
|
|
4
|
+
export type StreamProtocolProcessorOptions<ISTREAM extends Readable, OSTREAM extends Writable, AS extends object, SCS extends object> = {
|
|
5
|
+
packetMaxLength?: number;
|
|
6
|
+
packetOpcodeSize?: number;
|
|
7
|
+
packetLengthSize?: number;
|
|
8
|
+
streamBuffer: StreamBuffer;
|
|
9
|
+
application: Application<ISTREAM, OSTREAM, AS, SCS>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamProtocolProcessor = void 0;
|
|
4
|
+
var StreamProtocolProcessor_1 = require("./StreamProtocolProcessor");
|
|
5
|
+
Object.defineProperty(exports, "StreamProtocolProcessor", { enumerable: true, get: function () { return StreamProtocolProcessor_1.StreamProtocolProcessor; } });
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamProtocolToolkitAlreadyExistsError = void 0;
|
|
4
|
+
const StreamProtocolToolkitError_1 = require("./StreamProtocolToolkitError");
|
|
5
|
+
class StreamProtocolToolkitAlreadyExistsError extends StreamProtocolToolkitError_1.StreamProtocolToolkitError {
|
|
6
|
+
}
|
|
7
|
+
exports.StreamProtocolToolkitAlreadyExistsError = StreamProtocolToolkitAlreadyExistsError;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamProtocolToolkitError = void 0;
|
|
4
|
+
class StreamProtocolToolkitError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = new.target.name;
|
|
8
|
+
if (Error.captureStackTrace) {
|
|
9
|
+
Error.captureStackTrace(this, new.target);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.StreamProtocolToolkitError = StreamProtocolToolkitError;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamProtocolToolkitNotImplementedError = void 0;
|
|
4
|
+
const StreamProtocolToolkitError_1 = require("./StreamProtocolToolkitError");
|
|
5
|
+
class StreamProtocolToolkitNotImplementedError extends StreamProtocolToolkitError_1.StreamProtocolToolkitError {
|
|
6
|
+
}
|
|
7
|
+
exports.StreamProtocolToolkitNotImplementedError = StreamProtocolToolkitNotImplementedError;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamProtocolToolkitTypeError = void 0;
|
|
4
|
+
const StreamProtocolToolkitError_1 = require("./StreamProtocolToolkitError");
|
|
5
|
+
class StreamProtocolToolkitTypeError extends StreamProtocolToolkitError_1.StreamProtocolToolkitError {
|
|
6
|
+
}
|
|
7
|
+
exports.StreamProtocolToolkitTypeError = StreamProtocolToolkitTypeError;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { StreamProtocolToolkitError } from './StreamProtocolToolkitError';
|
|
2
|
+
export { StreamProtocolToolkitTypeError } from './StreamProtocolToolkitTypeError';
|
|
3
|
+
export { StreamProtocolToolkitAlreadyExistsError } from './StreamProtocolToolkitAlreadyExistsError';
|
|
4
|
+
export { StreamProtocolToolkitNotImplementedError } from './StreamProtocolToolkitNotImplementedError';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamProtocolToolkitNotImplementedError = exports.StreamProtocolToolkitAlreadyExistsError = exports.StreamProtocolToolkitTypeError = exports.StreamProtocolToolkitError = void 0;
|
|
4
|
+
var StreamProtocolToolkitError_1 = require("./StreamProtocolToolkitError");
|
|
5
|
+
Object.defineProperty(exports, "StreamProtocolToolkitError", { enumerable: true, get: function () { return StreamProtocolToolkitError_1.StreamProtocolToolkitError; } });
|
|
6
|
+
var StreamProtocolToolkitTypeError_1 = require("./StreamProtocolToolkitTypeError");
|
|
7
|
+
Object.defineProperty(exports, "StreamProtocolToolkitTypeError", { enumerable: true, get: function () { return StreamProtocolToolkitTypeError_1.StreamProtocolToolkitTypeError; } });
|
|
8
|
+
var StreamProtocolToolkitAlreadyExistsError_1 = require("./StreamProtocolToolkitAlreadyExistsError");
|
|
9
|
+
Object.defineProperty(exports, "StreamProtocolToolkitAlreadyExistsError", { enumerable: true, get: function () { return StreamProtocolToolkitAlreadyExistsError_1.StreamProtocolToolkitAlreadyExistsError; } });
|
|
10
|
+
var StreamProtocolToolkitNotImplementedError_1 = require("./StreamProtocolToolkitNotImplementedError");
|
|
11
|
+
Object.defineProperty(exports, "StreamProtocolToolkitNotImplementedError", { enumerable: true, get: function () { return StreamProtocolToolkitNotImplementedError_1.StreamProtocolToolkitNotImplementedError; } });
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './utils';
|
|
2
|
+
export * from './errors';
|
|
3
|
+
export * from './Protocol';
|
|
4
|
+
export * from './RawPacket';
|
|
5
|
+
export * from './Application';
|
|
6
|
+
export * from './StreamBuffer';
|
|
7
|
+
export * from './StreamContext';
|
|
8
|
+
export * from './ProtocolHandler';
|
|
9
|
+
export * from './StreamProtocolProcessor';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./utils"), exports);
|
|
18
|
+
__exportStar(require("./errors"), exports);
|
|
19
|
+
__exportStar(require("./Protocol"), exports);
|
|
20
|
+
__exportStar(require("./RawPacket"), exports);
|
|
21
|
+
__exportStar(require("./Application"), exports);
|
|
22
|
+
__exportStar(require("./StreamBuffer"), exports);
|
|
23
|
+
__exportStar(require("./StreamContext"), exports);
|
|
24
|
+
__exportStar(require("./ProtocolHandler"), exports);
|
|
25
|
+
__exportStar(require("./StreamProtocolProcessor"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function anySignal(signals: AbortSignal[]): AbortSignal;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.anySignal = anySignal;
|
|
4
|
+
function anySignal(signals) {
|
|
5
|
+
const handlers = new Map();
|
|
6
|
+
const controller = new AbortController();
|
|
7
|
+
const cleanup = function () {
|
|
8
|
+
for (const [signal, handler] of handlers) {
|
|
9
|
+
signal.removeEventListener('abort', handler);
|
|
10
|
+
}
|
|
11
|
+
handlers.clear();
|
|
12
|
+
};
|
|
13
|
+
for (const signal of signals) {
|
|
14
|
+
if (!signal) {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (signal.aborted) {
|
|
18
|
+
controller.abort(signal.reason);
|
|
19
|
+
cleanup();
|
|
20
|
+
return controller.signal;
|
|
21
|
+
}
|
|
22
|
+
const handler = function () {
|
|
23
|
+
controller.abort(signal.reason);
|
|
24
|
+
};
|
|
25
|
+
handlers.set(signal, handler);
|
|
26
|
+
signal.addEventListener('abort', handler, {
|
|
27
|
+
once: true
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
controller.signal.addEventListener('abort', cleanup, {
|
|
31
|
+
once: true
|
|
32
|
+
});
|
|
33
|
+
return controller.signal;
|
|
34
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { anySignal } from './any-signal';
|
|
2
|
+
export { isPlainObject } from './is-plain-object';
|
|
3
|
+
export { isReadableStreamAlive } from './is-readable-stream-alive';
|
|
4
|
+
export { isWritableStreamAlive } from './is-writable-stream-alive';
|
|
5
|
+
export { readPausedStreamChunk } from './read-paused-stream-chunk';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readPausedStreamChunk = exports.isWritableStreamAlive = exports.isReadableStreamAlive = exports.isPlainObject = exports.anySignal = void 0;
|
|
4
|
+
var any_signal_1 = require("./any-signal");
|
|
5
|
+
Object.defineProperty(exports, "anySignal", { enumerable: true, get: function () { return any_signal_1.anySignal; } });
|
|
6
|
+
var is_plain_object_1 = require("./is-plain-object");
|
|
7
|
+
Object.defineProperty(exports, "isPlainObject", { enumerable: true, get: function () { return is_plain_object_1.isPlainObject; } });
|
|
8
|
+
var is_readable_stream_alive_1 = require("./is-readable-stream-alive");
|
|
9
|
+
Object.defineProperty(exports, "isReadableStreamAlive", { enumerable: true, get: function () { return is_readable_stream_alive_1.isReadableStreamAlive; } });
|
|
10
|
+
var is_writable_stream_alive_1 = require("./is-writable-stream-alive");
|
|
11
|
+
Object.defineProperty(exports, "isWritableStreamAlive", { enumerable: true, get: function () { return is_writable_stream_alive_1.isWritableStreamAlive; } });
|
|
12
|
+
var read_paused_stream_chunk_1 = require("./read-paused-stream-chunk");
|
|
13
|
+
Object.defineProperty(exports, "readPausedStreamChunk", { enumerable: true, get: function () { return read_paused_stream_chunk_1.readPausedStreamChunk; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isPlainObject<T extends object>(value: T): boolean;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isPlainObject = isPlainObject;
|
|
4
|
+
function isPlainObject(value) {
|
|
5
|
+
if (value === null || typeof value !== 'object') {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
const proto = Object.getPrototypeOf(value);
|
|
9
|
+
if (proto === null) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
return Object.getPrototypeOf(proto) === null;
|
|
13
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isReadableStreamAlive = isReadableStreamAlive;
|
|
4
|
+
function isReadableStreamAlive(stream) {
|
|
5
|
+
if (!stream ||
|
|
6
|
+
stream.readable === false ||
|
|
7
|
+
stream.errored ||
|
|
8
|
+
stream.closed === true ||
|
|
9
|
+
stream.destroyed === true ||
|
|
10
|
+
stream.readableEnded === true) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const readableState = stream._readableState;
|
|
14
|
+
if (readableState) {
|
|
15
|
+
if (readableState.destroyed ||
|
|
16
|
+
readableState.ended && readableState.endEmitted) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isWritableStreamAlive = isWritableStreamAlive;
|
|
4
|
+
function isWritableStreamAlive(stream) {
|
|
5
|
+
if (!stream ||
|
|
6
|
+
stream.writable === false ||
|
|
7
|
+
stream.errored ||
|
|
8
|
+
stream.closed === true ||
|
|
9
|
+
stream.destroyed === true ||
|
|
10
|
+
stream.writableEnded === true ||
|
|
11
|
+
stream.writableFinished === true) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const writableState = stream._writableState;
|
|
15
|
+
if (writableState) {
|
|
16
|
+
if (writableState.destroyed ||
|
|
17
|
+
writableState.errored ||
|
|
18
|
+
writableState.errorEmitted === true ||
|
|
19
|
+
(writableState.ending === true || writableState.ended === true || writableState.finished === true)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readPausedStreamChunk = readPausedStreamChunk;
|
|
4
|
+
const buffer_1 = require("buffer");
|
|
5
|
+
const errors_1 = require("../errors");
|
|
6
|
+
const is_readable_stream_alive_1 = require("./is-readable-stream-alive");
|
|
7
|
+
function waitReadable(stream) {
|
|
8
|
+
return new Promise(function (resolve, reject) {
|
|
9
|
+
const onReadable = function () {
|
|
10
|
+
cleanup();
|
|
11
|
+
resolve();
|
|
12
|
+
};
|
|
13
|
+
const onEnd = function () {
|
|
14
|
+
cleanup();
|
|
15
|
+
reject(new errors_1.StreamProtocolToolkitError('STREAM_ENDED'));
|
|
16
|
+
};
|
|
17
|
+
const onClose = function () {
|
|
18
|
+
cleanup();
|
|
19
|
+
reject(new errors_1.StreamProtocolToolkitError('STREAM_CLOSED'));
|
|
20
|
+
};
|
|
21
|
+
const onError = function (err) {
|
|
22
|
+
cleanup();
|
|
23
|
+
reject(err);
|
|
24
|
+
};
|
|
25
|
+
const cleanup = function () {
|
|
26
|
+
stream.removeListener('readable', onReadable);
|
|
27
|
+
stream.removeListener('end', onEnd);
|
|
28
|
+
stream.removeListener('close', onClose);
|
|
29
|
+
stream.removeListener('error', onError);
|
|
30
|
+
};
|
|
31
|
+
stream.once('readable', onReadable);
|
|
32
|
+
stream.once('end', onEnd);
|
|
33
|
+
stream.once('close', onClose);
|
|
34
|
+
stream.once('error', onError);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
async function readPausedStreamChunk(stream) {
|
|
38
|
+
if (!stream.isPaused()) {
|
|
39
|
+
throw new errors_1.StreamProtocolToolkitError('STREAM_NOT_PAUSED');
|
|
40
|
+
}
|
|
41
|
+
while (true) {
|
|
42
|
+
if (!(0, is_readable_stream_alive_1.isReadableStreamAlive)(stream)) {
|
|
43
|
+
throw new errors_1.StreamProtocolToolkitError('INVALID_READABLE_STREAM');
|
|
44
|
+
}
|
|
45
|
+
const chunk = stream.read();
|
|
46
|
+
if (chunk !== null) {
|
|
47
|
+
return typeof chunk === 'string' ? buffer_1.Buffer.from(chunk) : chunk;
|
|
48
|
+
}
|
|
49
|
+
await waitReadable(stream);
|
|
50
|
+
}
|
|
51
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "stream-protocol-toolkit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node.js stream protocol toolkit",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "npm run clean && node ./node_modules/.bin/tsc",
|
|
18
|
+
"clean": "node -e \"['./dist'].forEach(item => require('fs').rmSync(item, {recursive:true,force:true}));\"",
|
|
19
|
+
"prepack": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/mvcbox/node-stream-protocol-toolkit.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"Buffer",
|
|
27
|
+
"Binary data"
|
|
28
|
+
],
|
|
29
|
+
"author": "mvcbox.org@gmail.com",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/mvcbox/node-stream-protocol-toolkit/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/mvcbox/node-stream-protocol-toolkit#readme",
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"LICENSE",
|
|
38
|
+
"README.md"
|
|
39
|
+
],
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"extended-buffer": "^7.4.0",
|
|
42
|
+
"utility-types": "^3.11.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^16.18.126",
|
|
46
|
+
"typescript": "^5.9.3"
|
|
47
|
+
}
|
|
48
|
+
}
|