stream-protocol-toolkit 0.1.0 → 0.2.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/dist/Application/ApplicationOptions.d.ts +1 -1
- package/dist/examples/ping-pong/client.d.ts +1 -0
- package/dist/examples/ping-pong/client.js +51 -0
- package/dist/examples/ping-pong/handlers/PingHandler.d.ts +7 -0
- package/dist/examples/ping-pong/handlers/PingHandler.js +16 -0
- package/dist/examples/ping-pong/handlers/PongHandler.d.ts +8 -0
- package/dist/examples/ping-pong/handlers/PongHandler.js +24 -0
- package/dist/examples/ping-pong/handlers/index.d.ts +2 -0
- package/dist/examples/ping-pong/handlers/index.js +7 -0
- package/dist/examples/ping-pong/protocol/PingProtocol.d.ts +9 -0
- package/dist/examples/ping-pong/protocol/PingProtocol.js +22 -0
- package/dist/examples/ping-pong/protocol/PongProtocol.d.ts +9 -0
- package/dist/examples/ping-pong/protocol/PongProtocol.js +22 -0
- package/dist/examples/ping-pong/protocol/index.d.ts +2 -0
- package/dist/examples/ping-pong/protocol/index.js +7 -0
- package/dist/examples/ping-pong/server.d.ts +1 -0
- package/dist/examples/ping-pong/server.js +31 -0
- package/dist/examples/test.d.ts +0 -0
- package/dist/examples/test.js +1 -0
- package/package.json +3 -3
|
@@ -7,6 +7,6 @@ export type ApplicationOptions<ISTREAM extends Readable, OSTREAM extends Writabl
|
|
|
7
7
|
streamContextFactory?: StreamContextFactory<ISTREAM, OSTREAM, AS, SCS>;
|
|
8
8
|
applicationStateFactory?: ApplicationStateFactory<ISTREAM, OSTREAM, AS, SCS>;
|
|
9
9
|
streamContextStateFactory?: StreamContextStateFactory<ISTREAM, OSTREAM, AS, SCS>;
|
|
10
|
-
transformStreamHandlerFactory
|
|
10
|
+
transformStreamHandlerFactory?: TransformStreamHandlerFactory<ISTREAM, OSTREAM, AS, SCS>;
|
|
11
11
|
streamProtocolProcessorFactory?: StreamProtocolProcessorFactory<ISTREAM, OSTREAM, AS, SCS>;
|
|
12
12
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const net_1 = __importDefault(require("net"));
|
|
7
|
+
const handlers_1 = require("./handlers");
|
|
8
|
+
const protocol_1 = require("./protocol");
|
|
9
|
+
const Application_1 = require("../../Application");
|
|
10
|
+
const clientApp = new Application_1.Application();
|
|
11
|
+
clientApp.addProtocol(protocol_1.PongProtocol, new handlers_1.PongHandler());
|
|
12
|
+
clientApp.eventBus.on(1, function (streamContext) {
|
|
13
|
+
console.log('ApplicationEventEnum.STREAM_CONTEXT_CREATED');
|
|
14
|
+
});
|
|
15
|
+
clientApp.eventBus.on(3, function (streamContext) {
|
|
16
|
+
console.log('ApplicationEventEnum.STREAM_CONTEXT_DETACHED');
|
|
17
|
+
});
|
|
18
|
+
clientApp.eventBus.on(0, function (err) {
|
|
19
|
+
console.log('ApplicationEventEnum.STREAM_CONTEXT_ERROR:', err);
|
|
20
|
+
});
|
|
21
|
+
clientApp.eventBus.on(2, function (streamContext) {
|
|
22
|
+
console.log('ApplicationEventEnum.STREAM_CONTEXT_ATTACHED');
|
|
23
|
+
const packet = (new protocol_1.PingProtocol({
|
|
24
|
+
someData: 0
|
|
25
|
+
}))._marshal();
|
|
26
|
+
streamContext.streamProtocolProcessor.writePacketHeader(packet, {
|
|
27
|
+
opcode: protocol_1.PingProtocol._opcode,
|
|
28
|
+
length: packet.length
|
|
29
|
+
}, true);
|
|
30
|
+
client.write(packet.nativeBufferView);
|
|
31
|
+
streamContext.streamContextState.time = Date.now();
|
|
32
|
+
streamContext.streamContextState.maxTime = Date.now() + 10000;
|
|
33
|
+
});
|
|
34
|
+
const client = net_1.default.createConnection({
|
|
35
|
+
host: '127.0.0.1',
|
|
36
|
+
port: 12345,
|
|
37
|
+
}, function () {
|
|
38
|
+
console.log('Connection open');
|
|
39
|
+
client.setNoDelay(true);
|
|
40
|
+
const { streamContext } = clientApp.createStreamContext({
|
|
41
|
+
input: client,
|
|
42
|
+
output: client
|
|
43
|
+
});
|
|
44
|
+
clientApp.startStream({
|
|
45
|
+
streamContext
|
|
46
|
+
}).then(function () {
|
|
47
|
+
console.log('Connection close');
|
|
48
|
+
}).catch(function (err) {
|
|
49
|
+
console.error('Connection error:', err);
|
|
50
|
+
});
|
|
51
|
+
}).on('error', console.error);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import net from 'net';
|
|
2
|
+
import { PingProtocol } from '../protocol';
|
|
3
|
+
import { HandleInputDTO, HandleOutputDTO, ProtocolHandler } from '../../../ProtocolHandler';
|
|
4
|
+
export declare class PingHandler extends ProtocolHandler<PingProtocol, net.Socket, net.Socket, any, any> {
|
|
5
|
+
constructor();
|
|
6
|
+
handle(input: HandleInputDTO<PingProtocol, net.Socket, net.Socket, any, any>): Promise<HandleOutputDTO>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PingHandler = void 0;
|
|
4
|
+
const protocol_1 = require("../protocol");
|
|
5
|
+
const ProtocolHandler_1 = require("../../../ProtocolHandler");
|
|
6
|
+
class PingHandler extends ProtocolHandler_1.ProtocolHandler {
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
}
|
|
10
|
+
async handle(input) {
|
|
11
|
+
return new protocol_1.PongProtocol({
|
|
12
|
+
someData: input.packet.someData
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.PingHandler = PingHandler;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import net from 'net';
|
|
2
|
+
import { PongProtocol } from '../protocol';
|
|
3
|
+
import { HandleInputDTO, HandleOutputDTO, ProtocolHandler } from '../../../ProtocolHandler';
|
|
4
|
+
export declare class PongHandler extends ProtocolHandler<PongProtocol, net.Socket, net.Socket, any, any> {
|
|
5
|
+
protected counter: number;
|
|
6
|
+
constructor();
|
|
7
|
+
handle(input: HandleInputDTO<PongProtocol, net.Socket, net.Socket, any, any>): Promise<HandleOutputDTO>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PongHandler = void 0;
|
|
4
|
+
const protocol_1 = require("../protocol");
|
|
5
|
+
const ProtocolHandler_1 = require("../../../ProtocolHandler");
|
|
6
|
+
class PongHandler extends ProtocolHandler_1.ProtocolHandler {
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
this.counter = 0;
|
|
10
|
+
}
|
|
11
|
+
async handle(input) {
|
|
12
|
+
var _a;
|
|
13
|
+
if (Date.now() > input.streamContext.streamContextState.maxTime) {
|
|
14
|
+
console.log('>>> Done:', this.counter);
|
|
15
|
+
(_a = input.streamContext.output) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
input.streamContext.streamContextState.time = Date.now();
|
|
19
|
+
return new protocol_1.PingProtocol({
|
|
20
|
+
someData: this.counter++
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.PongHandler = PongHandler;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PongHandler = exports.PingHandler = void 0;
|
|
4
|
+
var PingHandler_1 = require("./PingHandler");
|
|
5
|
+
Object.defineProperty(exports, "PingHandler", { enumerable: true, get: function () { return PingHandler_1.PingHandler; } });
|
|
6
|
+
var PongHandler_1 = require("./PongHandler");
|
|
7
|
+
Object.defineProperty(exports, "PongHandler", { enumerable: true, get: function () { return PongHandler_1.PongHandler; } });
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { StreamBuffer } from '../../../StreamBuffer';
|
|
2
|
+
import { Protocol, ProtocolInitValue } from '../../../Protocol';
|
|
3
|
+
export declare class PingProtocol extends Protocol {
|
|
4
|
+
someData: number;
|
|
5
|
+
constructor(initValue: ProtocolInitValue<PingProtocol>);
|
|
6
|
+
static get _opcode(): number;
|
|
7
|
+
_marshal(): StreamBuffer;
|
|
8
|
+
_unmarshal(buffer: StreamBuffer): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PingProtocol = void 0;
|
|
4
|
+
const Protocol_1 = require("../../../Protocol");
|
|
5
|
+
class PingProtocol extends Protocol_1.Protocol {
|
|
6
|
+
constructor(initValue) {
|
|
7
|
+
super();
|
|
8
|
+
this._initialize(initValue);
|
|
9
|
+
}
|
|
10
|
+
static get _opcode() {
|
|
11
|
+
return 1;
|
|
12
|
+
}
|
|
13
|
+
_marshal() {
|
|
14
|
+
const buffer = this._createBuffer();
|
|
15
|
+
buffer.writeUInt32BE(this.someData);
|
|
16
|
+
return buffer;
|
|
17
|
+
}
|
|
18
|
+
_unmarshal(buffer) {
|
|
19
|
+
this.someData = buffer.readUInt32BE();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.PingProtocol = PingProtocol;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { StreamBuffer } from '../../../StreamBuffer';
|
|
2
|
+
import { Protocol, ProtocolInitValue } from '../../../Protocol';
|
|
3
|
+
export declare class PongProtocol extends Protocol {
|
|
4
|
+
someData: number;
|
|
5
|
+
constructor(initValue: ProtocolInitValue<PongProtocol>);
|
|
6
|
+
static get _opcode(): number;
|
|
7
|
+
_marshal(): StreamBuffer;
|
|
8
|
+
_unmarshal(buffer: StreamBuffer): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PongProtocol = void 0;
|
|
4
|
+
const Protocol_1 = require("../../../Protocol");
|
|
5
|
+
class PongProtocol extends Protocol_1.Protocol {
|
|
6
|
+
constructor(initValue) {
|
|
7
|
+
super();
|
|
8
|
+
this._initialize(initValue);
|
|
9
|
+
}
|
|
10
|
+
static get _opcode() {
|
|
11
|
+
return 2;
|
|
12
|
+
}
|
|
13
|
+
_marshal() {
|
|
14
|
+
const buffer = this._createBuffer();
|
|
15
|
+
buffer.writeUInt32BE(this.someData);
|
|
16
|
+
return buffer;
|
|
17
|
+
}
|
|
18
|
+
_unmarshal(buffer) {
|
|
19
|
+
this.someData = buffer.readUInt32BE();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.PongProtocol = PongProtocol;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PongProtocol = exports.PingProtocol = void 0;
|
|
4
|
+
var PingProtocol_1 = require("./PingProtocol");
|
|
5
|
+
Object.defineProperty(exports, "PingProtocol", { enumerable: true, get: function () { return PingProtocol_1.PingProtocol; } });
|
|
6
|
+
var PongProtocol_1 = require("./PongProtocol");
|
|
7
|
+
Object.defineProperty(exports, "PongProtocol", { enumerable: true, get: function () { return PongProtocol_1.PongProtocol; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const net_1 = __importDefault(require("net"));
|
|
7
|
+
const Application_1 = require("../../Application");
|
|
8
|
+
const handlers_1 = require("./handlers");
|
|
9
|
+
const protocol_1 = require("./protocol");
|
|
10
|
+
const serverApp = new Application_1.Application();
|
|
11
|
+
serverApp.addProtocol(protocol_1.PingProtocol, new handlers_1.PingHandler());
|
|
12
|
+
net_1.default.createServer(function (client) {
|
|
13
|
+
console.log('Connection open');
|
|
14
|
+
client.setNoDelay(true);
|
|
15
|
+
const { streamContext } = serverApp.createStreamContext({
|
|
16
|
+
input: client,
|
|
17
|
+
output: client
|
|
18
|
+
});
|
|
19
|
+
serverApp.startStream({
|
|
20
|
+
streamContext
|
|
21
|
+
}).then(function () {
|
|
22
|
+
console.log('Connection close');
|
|
23
|
+
}).catch(function (err) {
|
|
24
|
+
console.error('Connection error:', err);
|
|
25
|
+
});
|
|
26
|
+
}).listen({
|
|
27
|
+
host: '127.0.0.1',
|
|
28
|
+
port: 12345
|
|
29
|
+
}, function () {
|
|
30
|
+
console.log('------- Server started -------');
|
|
31
|
+
}).on('error', console.error);
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stream-protocol-toolkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Node.js stream protocol toolkit",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"url": "git+https://github.com/mvcbox/node-stream-protocol-toolkit.git"
|
|
24
24
|
},
|
|
25
25
|
"keywords": [
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"Stream",
|
|
27
|
+
"Stream protocol"
|
|
28
28
|
],
|
|
29
29
|
"author": "mvcbox.org@gmail.com",
|
|
30
30
|
"license": "MIT",
|