@pluv/platform-node 0.0.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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +17 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +157 -0
- package/dist/index.mjs +125 -0
- package/package.json +28 -0
- package/src/NodePlatform.ts +42 -0
- package/src/NodeWebSocket.ts +101 -0
- package/src/index.ts +3 -0
- package/src/platformNode.ts +8 -0
- package/tsconfig.json +8 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
> @pluv/platform-node@0.0.0 build /home/runner/work/pluv/pluv/packages/platform-node
|
|
3
|
+
> tsup src/index.ts --format esm,cjs --dts
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v6.5.0
|
|
8
|
+
[34mCLI[39m Target: es6
|
|
9
|
+
[34mESM[39m Build start
|
|
10
|
+
[34mCJS[39m Build start
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m3.06 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 89ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m4.41 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 94ms
|
|
15
|
+
[34mDTS[39m Build start
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 5602ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m1.35 KB[39m
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { WebSocket } from 'ws';
|
|
2
|
+
export { WebSocket } from 'ws';
|
|
3
|
+
import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPersistance, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig } from '@pluv/io';
|
|
4
|
+
|
|
5
|
+
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
6
|
+
declare class NodeWebSocket extends AbstractWebSocket {
|
|
7
|
+
webSocket: WebSocket;
|
|
8
|
+
get readyState(): 0 | 1 | 2 | 3;
|
|
9
|
+
constructor(webSocket: WebSocket, config: NodeWebSocketConfig);
|
|
10
|
+
addEventListener<TType extends keyof AbstractEventMap>(type: TType, handler: AbstractListener<TType>): void;
|
|
11
|
+
close(code?: number | undefined, reason?: string | undefined): void;
|
|
12
|
+
initialize(): Promise<() => void>;
|
|
13
|
+
send(message: string | ArrayBuffer | ArrayBufferView): void;
|
|
14
|
+
terminate(): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type NodePlatformOptions = {
|
|
18
|
+
persistance?: undefined;
|
|
19
|
+
pubSub?: undefined;
|
|
20
|
+
} | {
|
|
21
|
+
persistance: AbstractPersistance;
|
|
22
|
+
pubSub: AbstractPubSub;
|
|
23
|
+
};
|
|
24
|
+
declare class NodePlatform extends AbstractPlatform<WebSocket> {
|
|
25
|
+
constructor(options?: NodePlatformOptions);
|
|
26
|
+
convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket;
|
|
27
|
+
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
28
|
+
randomUUID(): string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare const platformNode: (options?: NodePlatformOptions) => NodePlatform;
|
|
32
|
+
|
|
33
|
+
export { NodePlatformOptions, platformNode };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
var __async = (__this, __arguments, generator) => {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
var fulfilled = (value) => {
|
|
28
|
+
try {
|
|
29
|
+
step(generator.next(value));
|
|
30
|
+
} catch (e) {
|
|
31
|
+
reject(e);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var rejected = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.throw(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
42
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/index.ts
|
|
47
|
+
var src_exports = {};
|
|
48
|
+
__export(src_exports, {
|
|
49
|
+
platformNode: () => platformNode
|
|
50
|
+
});
|
|
51
|
+
module.exports = __toCommonJS(src_exports);
|
|
52
|
+
|
|
53
|
+
// src/NodePlatform.ts
|
|
54
|
+
var import_io2 = require("@pluv/io");
|
|
55
|
+
var import_crypto = __toESM(require("crypto"));
|
|
56
|
+
var import_util = require("util");
|
|
57
|
+
|
|
58
|
+
// src/NodeWebSocket.ts
|
|
59
|
+
var import_io = require("@pluv/io");
|
|
60
|
+
var NodeWebSocket = class extends import_io.AbstractWebSocket {
|
|
61
|
+
constructor(webSocket, config) {
|
|
62
|
+
const { room } = config;
|
|
63
|
+
super({ room });
|
|
64
|
+
this.webSocket = webSocket;
|
|
65
|
+
}
|
|
66
|
+
get readyState() {
|
|
67
|
+
return this.webSocket.readyState;
|
|
68
|
+
}
|
|
69
|
+
addEventListener(type, handler) {
|
|
70
|
+
switch (type) {
|
|
71
|
+
case "close":
|
|
72
|
+
this.webSocket.on("close", (code, reason) => __async(this, null, function* () {
|
|
73
|
+
yield Promise.resolve(
|
|
74
|
+
handler({
|
|
75
|
+
code,
|
|
76
|
+
reason: reason.toString("utf-8")
|
|
77
|
+
})
|
|
78
|
+
);
|
|
79
|
+
}));
|
|
80
|
+
return;
|
|
81
|
+
case "error":
|
|
82
|
+
this.webSocket.on("error", (error) => __async(this, null, function* () {
|
|
83
|
+
yield Promise.resolve(
|
|
84
|
+
handler({
|
|
85
|
+
error,
|
|
86
|
+
message: error.message
|
|
87
|
+
})
|
|
88
|
+
);
|
|
89
|
+
}));
|
|
90
|
+
return;
|
|
91
|
+
case "message":
|
|
92
|
+
this.webSocket.on("message", (data) => __async(this, null, function* () {
|
|
93
|
+
yield Promise.resolve(
|
|
94
|
+
handler({
|
|
95
|
+
data
|
|
96
|
+
})
|
|
97
|
+
);
|
|
98
|
+
}));
|
|
99
|
+
return;
|
|
100
|
+
default:
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
close(code, reason) {
|
|
104
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
105
|
+
(readyState) => readyState === this.readyState
|
|
106
|
+
);
|
|
107
|
+
if (!canClose)
|
|
108
|
+
return;
|
|
109
|
+
this.webSocket.close(code, reason);
|
|
110
|
+
}
|
|
111
|
+
initialize() {
|
|
112
|
+
return __async(this, null, function* () {
|
|
113
|
+
return Promise.resolve(() => void 0);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
send(message) {
|
|
117
|
+
if (this.readyState !== this.OPEN)
|
|
118
|
+
return;
|
|
119
|
+
this.webSocket.send(message, (error) => {
|
|
120
|
+
if (error)
|
|
121
|
+
throw error;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
terminate() {
|
|
125
|
+
this.webSocket.terminate();
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// src/NodePlatform.ts
|
|
130
|
+
var NodePlatform = class extends import_io2.AbstractPlatform {
|
|
131
|
+
constructor(options = {}) {
|
|
132
|
+
const { persistance, pubSub } = options;
|
|
133
|
+
super(persistance && pubSub ? { persistance, pubSub } : {});
|
|
134
|
+
}
|
|
135
|
+
convertWebSocket(webSocket, config) {
|
|
136
|
+
const { room } = config;
|
|
137
|
+
return new NodeWebSocket(webSocket, { room });
|
|
138
|
+
}
|
|
139
|
+
parseData(data) {
|
|
140
|
+
if (typeof data === "string")
|
|
141
|
+
return JSON.parse(data);
|
|
142
|
+
const decoder = new import_util.TextDecoder("utf8");
|
|
143
|
+
return JSON.parse(decoder.decode(data));
|
|
144
|
+
}
|
|
145
|
+
randomUUID() {
|
|
146
|
+
return import_crypto.default.randomUUID();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// src/platformNode.ts
|
|
151
|
+
var platformNode = (options = {}) => {
|
|
152
|
+
return new NodePlatform(options);
|
|
153
|
+
};
|
|
154
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
155
|
+
0 && (module.exports = {
|
|
156
|
+
platformNode
|
|
157
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/NodePlatform.ts
|
|
23
|
+
import { AbstractPlatform } from "@pluv/io";
|
|
24
|
+
import crypto from "crypto";
|
|
25
|
+
import { TextDecoder } from "util";
|
|
26
|
+
|
|
27
|
+
// src/NodeWebSocket.ts
|
|
28
|
+
import { AbstractWebSocket } from "@pluv/io";
|
|
29
|
+
var NodeWebSocket = class extends AbstractWebSocket {
|
|
30
|
+
constructor(webSocket, config) {
|
|
31
|
+
const { room } = config;
|
|
32
|
+
super({ room });
|
|
33
|
+
this.webSocket = webSocket;
|
|
34
|
+
}
|
|
35
|
+
get readyState() {
|
|
36
|
+
return this.webSocket.readyState;
|
|
37
|
+
}
|
|
38
|
+
addEventListener(type, handler) {
|
|
39
|
+
switch (type) {
|
|
40
|
+
case "close":
|
|
41
|
+
this.webSocket.on("close", (code, reason) => __async(this, null, function* () {
|
|
42
|
+
yield Promise.resolve(
|
|
43
|
+
handler({
|
|
44
|
+
code,
|
|
45
|
+
reason: reason.toString("utf-8")
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
}));
|
|
49
|
+
return;
|
|
50
|
+
case "error":
|
|
51
|
+
this.webSocket.on("error", (error) => __async(this, null, function* () {
|
|
52
|
+
yield Promise.resolve(
|
|
53
|
+
handler({
|
|
54
|
+
error,
|
|
55
|
+
message: error.message
|
|
56
|
+
})
|
|
57
|
+
);
|
|
58
|
+
}));
|
|
59
|
+
return;
|
|
60
|
+
case "message":
|
|
61
|
+
this.webSocket.on("message", (data) => __async(this, null, function* () {
|
|
62
|
+
yield Promise.resolve(
|
|
63
|
+
handler({
|
|
64
|
+
data
|
|
65
|
+
})
|
|
66
|
+
);
|
|
67
|
+
}));
|
|
68
|
+
return;
|
|
69
|
+
default:
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
close(code, reason) {
|
|
73
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
74
|
+
(readyState) => readyState === this.readyState
|
|
75
|
+
);
|
|
76
|
+
if (!canClose)
|
|
77
|
+
return;
|
|
78
|
+
this.webSocket.close(code, reason);
|
|
79
|
+
}
|
|
80
|
+
initialize() {
|
|
81
|
+
return __async(this, null, function* () {
|
|
82
|
+
return Promise.resolve(() => void 0);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
send(message) {
|
|
86
|
+
if (this.readyState !== this.OPEN)
|
|
87
|
+
return;
|
|
88
|
+
this.webSocket.send(message, (error) => {
|
|
89
|
+
if (error)
|
|
90
|
+
throw error;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
terminate() {
|
|
94
|
+
this.webSocket.terminate();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/NodePlatform.ts
|
|
99
|
+
var NodePlatform = class extends AbstractPlatform {
|
|
100
|
+
constructor(options = {}) {
|
|
101
|
+
const { persistance, pubSub } = options;
|
|
102
|
+
super(persistance && pubSub ? { persistance, pubSub } : {});
|
|
103
|
+
}
|
|
104
|
+
convertWebSocket(webSocket, config) {
|
|
105
|
+
const { room } = config;
|
|
106
|
+
return new NodeWebSocket(webSocket, { room });
|
|
107
|
+
}
|
|
108
|
+
parseData(data) {
|
|
109
|
+
if (typeof data === "string")
|
|
110
|
+
return JSON.parse(data);
|
|
111
|
+
const decoder = new TextDecoder("utf8");
|
|
112
|
+
return JSON.parse(decoder.decode(data));
|
|
113
|
+
}
|
|
114
|
+
randomUUID() {
|
|
115
|
+
return crypto.randomUUID();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// src/platformNode.ts
|
|
120
|
+
var platformNode = (options = {}) => {
|
|
121
|
+
return new NodePlatform(options);
|
|
122
|
+
};
|
|
123
|
+
export {
|
|
124
|
+
platformNode
|
|
125
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/platform-node",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@pluv/io": "^0.0.0",
|
|
12
|
+
"@pluv/types": "^0.0.0",
|
|
13
|
+
"@types/node": "^18.11.18",
|
|
14
|
+
"@types/ws": "^8.5.4"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@pluv/tsconfig": "^0.0.0",
|
|
18
|
+
"eslint-config-pluv": "^0.0.0",
|
|
19
|
+
"tsup": "^6.5.0",
|
|
20
|
+
"typescript": "^4.9.4"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
24
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
25
|
+
"lint": "eslint src/**/*.ts* --fix",
|
|
26
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AbstractPersistance, ConvertWebSocketConfig } from "@pluv/io";
|
|
2
|
+
import { AbstractPlatform, AbstractPubSub } from "@pluv/io";
|
|
3
|
+
import crypto from "crypto";
|
|
4
|
+
import { TextDecoder } from "util";
|
|
5
|
+
import type { WebSocket } from "ws";
|
|
6
|
+
import { NodeWebSocket } from "./NodeWebSocket";
|
|
7
|
+
|
|
8
|
+
export type NodePlatformOptions =
|
|
9
|
+
| { persistance?: undefined; pubSub?: undefined }
|
|
10
|
+
| {
|
|
11
|
+
persistance: AbstractPersistance;
|
|
12
|
+
pubSub: AbstractPubSub;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export class NodePlatform extends AbstractPlatform<WebSocket> {
|
|
16
|
+
constructor(options: NodePlatformOptions = {}) {
|
|
17
|
+
const { persistance, pubSub } = options;
|
|
18
|
+
|
|
19
|
+
super(persistance && pubSub ? { persistance, pubSub } : {});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public convertWebSocket(
|
|
23
|
+
webSocket: WebSocket,
|
|
24
|
+
config: ConvertWebSocketConfig
|
|
25
|
+
): NodeWebSocket {
|
|
26
|
+
const { room } = config;
|
|
27
|
+
|
|
28
|
+
return new NodeWebSocket(webSocket, { room });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public parseData(data: string | ArrayBuffer): Record<string, any> {
|
|
32
|
+
if (typeof data === "string") return JSON.parse(data);
|
|
33
|
+
|
|
34
|
+
const decoder = new TextDecoder("utf8");
|
|
35
|
+
|
|
36
|
+
return JSON.parse(decoder.decode(data));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public randomUUID(): string {
|
|
40
|
+
return crypto.randomUUID();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AbstractCloseEvent,
|
|
3
|
+
AbstractErrorEvent,
|
|
4
|
+
AbstractEventMap,
|
|
5
|
+
AbstractListener,
|
|
6
|
+
AbstractMessageEvent,
|
|
7
|
+
AbstractWebSocketConfig,
|
|
8
|
+
} from "@pluv/io";
|
|
9
|
+
import { AbstractWebSocket } from "@pluv/io";
|
|
10
|
+
import type { WebSocket } from "ws";
|
|
11
|
+
|
|
12
|
+
export interface NodeWebSocketEventMap {
|
|
13
|
+
close: AbstractCloseEvent;
|
|
14
|
+
message: AbstractMessageEvent;
|
|
15
|
+
error: AbstractErrorEvent;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
19
|
+
|
|
20
|
+
export class NodeWebSocket extends AbstractWebSocket {
|
|
21
|
+
public webSocket: WebSocket;
|
|
22
|
+
|
|
23
|
+
public get readyState(): 0 | 1 | 2 | 3 {
|
|
24
|
+
return this.webSocket.readyState;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
constructor(webSocket: WebSocket, config: NodeWebSocketConfig) {
|
|
28
|
+
const { room } = config;
|
|
29
|
+
|
|
30
|
+
super({ room });
|
|
31
|
+
|
|
32
|
+
this.webSocket = webSocket;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public addEventListener<TType extends keyof AbstractEventMap>(
|
|
36
|
+
type: TType,
|
|
37
|
+
handler: AbstractListener<TType>
|
|
38
|
+
): void {
|
|
39
|
+
switch (type) {
|
|
40
|
+
case "close":
|
|
41
|
+
this.webSocket.on("close", async (code, reason) => {
|
|
42
|
+
await Promise.resolve(
|
|
43
|
+
(handler as AbstractListener<"close">)({
|
|
44
|
+
code,
|
|
45
|
+
reason: reason.toString("utf-8"),
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return;
|
|
51
|
+
case "error":
|
|
52
|
+
this.webSocket.on("error", async (error) => {
|
|
53
|
+
await Promise.resolve(
|
|
54
|
+
(handler as AbstractListener<"error">)({
|
|
55
|
+
error,
|
|
56
|
+
message: error.message,
|
|
57
|
+
})
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return;
|
|
62
|
+
case "message":
|
|
63
|
+
this.webSocket.on("message", async (data) => {
|
|
64
|
+
await Promise.resolve(
|
|
65
|
+
(handler as AbstractListener<"message">)({
|
|
66
|
+
data,
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return;
|
|
72
|
+
default:
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public close(code?: number | undefined, reason?: string | undefined): void {
|
|
77
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
78
|
+
(readyState) => readyState === this.readyState
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
if (!canClose) return;
|
|
82
|
+
|
|
83
|
+
this.webSocket.close(code, reason);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public async initialize(): Promise<() => void> {
|
|
87
|
+
return Promise.resolve(() => undefined);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public send(message: string | ArrayBuffer | ArrayBufferView): void {
|
|
91
|
+
if (this.readyState !== this.OPEN) return;
|
|
92
|
+
|
|
93
|
+
this.webSocket.send(message, (error) => {
|
|
94
|
+
if (error) throw error;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public terminate(): void {
|
|
99
|
+
this.webSocket.terminate();
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/index.ts
ADDED