@nmtjs/ws-client 0.11.0 → 0.11.2
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/index.js +12 -10
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +19 -20
package/dist/index.js
CHANGED
|
@@ -3,10 +3,14 @@ import { ProtocolTransport } from "@nmtjs/protocol/client";
|
|
|
3
3
|
export class WebSocketClientTransport extends ProtocolTransport {
|
|
4
4
|
webSocket = null;
|
|
5
5
|
connecting = null;
|
|
6
|
+
options;
|
|
6
7
|
constructor(protocol, options) {
|
|
7
8
|
super();
|
|
8
9
|
this.protocol = protocol;
|
|
9
|
-
this.options =
|
|
10
|
+
this.options = {
|
|
11
|
+
debug: false,
|
|
12
|
+
...options
|
|
13
|
+
};
|
|
10
14
|
}
|
|
11
15
|
connect(auth, transformer) {
|
|
12
16
|
const wsUrl = new URL("/api", this.options.origin);
|
|
@@ -20,28 +24,26 @@ export class WebSocketClientTransport extends ProtocolTransport {
|
|
|
20
24
|
ws.addEventListener("message", ({ data }) => {
|
|
21
25
|
this.protocol.handleServerMessage(data, this, transformer);
|
|
22
26
|
});
|
|
27
|
+
ws.addEventListener("close", (event) => {
|
|
28
|
+
console.dir(event);
|
|
29
|
+
if (event.code !== 1e3) this.emit("disconnected");
|
|
30
|
+
this.webSocket = null;
|
|
31
|
+
}, { once: true });
|
|
23
32
|
this.webSocket = ws;
|
|
24
33
|
this.connecting = new Promise((resolve, reject) => {
|
|
25
34
|
ws.addEventListener("open", () => {
|
|
26
|
-
this.
|
|
35
|
+
this.emit("connected");
|
|
27
36
|
resolve();
|
|
28
37
|
}, { once: true });
|
|
29
38
|
ws.addEventListener("error", (event) => {
|
|
30
39
|
reject(new Error("WebSocket error", { cause: event }));
|
|
31
40
|
}, { once: true });
|
|
32
|
-
ws.addEventListener("close", () => {
|
|
33
|
-
this.protocol.emit("disconnected");
|
|
34
|
-
this.webSocket = null;
|
|
35
|
-
if (this.options.autoreconnect === true) {
|
|
36
|
-
setTimeout(this.connect.bind(this), 1e3);
|
|
37
|
-
}
|
|
38
|
-
}, { once: true });
|
|
39
41
|
});
|
|
40
42
|
return this.connecting;
|
|
41
43
|
}
|
|
42
44
|
async disconnect() {
|
|
43
45
|
if (this.webSocket === null) return;
|
|
44
|
-
this.webSocket.close();
|
|
46
|
+
this.webSocket.close(1e3, "user");
|
|
45
47
|
return _once(this.webSocket, "close");
|
|
46
48
|
}
|
|
47
49
|
async call(namespace, procedure, payload, options, transformer) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAS,mBAAmB,QAAQ,oBAAoB,iBAAiB;AACzE,SAIE,yBACK,wBAAwB;
|
|
1
|
+
{"mappings":"AAAA,SAAS,mBAAmB,QAAQ,oBAAoB,iBAAiB;AACzE,SAIE,yBACK,wBAAwB;AAiB/B,OAAO,MAAM,iCAAiC,kBAAkB;CAC9D,AAAU,YAA8B;CACxC,AAAU,aAAmC;CAC7C,AAAU;CAEV,YACqBA,UACnBC,SACA;AACA,SAAO;OAHY;AAInB,OAAK,UAAU;GACb,OAAO;GACP,GAAG;EACJ;CACF;CAED,QAAQC,MAAWC,aAAqD;EAEtE,MAAM,QAAQ,IAAI,IAAI,QAAQ,KAAK,QAAQ;AAC3C,MAAI,KAAK,SAAS,aAAa;AAC7B,SAAM,aAAa,IAAI,gBAAgB,KAAK,SAAS,YAAY;AACjE,SAAM,aAAa,IAAI,UAAU,KAAK,SAAS,YAAY;EAC5D;AACD,MAAI,KAAM,OAAM,aAAa,IAAI,QAAQ,KAAK;EAE9C,MAAM,KACJ,KAAK,QAAQ,YAAY,MAAM,IAAI,IAAI,UAAU,MAAM,UAAU;AAEnE,KAAG,aAAa;AAEhB,KAAG,iBAAiB,WAAW,CAAC,EAAE,MAAM,KAAK;AAC3C,QAAK,SAAS,oBAAoB,MAAqB,MAAM,YAAY;EAC1E,EAAC;AAEF,KAAG,iBACD,SACA,CAAC,UAAU;AACT,WAAQ,IAAI,MAAM;AAClB,OAAI,MAAM,SAAS,IAAM,MAAK,KAAK,eAAe;AAClD,QAAK,YAAY;EAClB,GACD,EAAE,MAAM,KAAM,EACf;AAED,OAAK,YAAY;AAEjB,OAAK,aAAa,IAAI,QAAQ,CAAC,SAAS,WAAW;AACjD,MAAG,iBACD,QACA,MAAM;AACJ,SAAK,KAAK,YAAY;AACtB,aAAS;GACV,GACD,EAAE,MAAM,KAAM,EACf;AAED,MAAG,iBACD,SACA,CAAC,UAAU;AACT,WAAO,IAAI,MAAM,mBAAmB,EAAE,OAAO,MAAO,GAAE;GACvD,GACD,EAAE,MAAM,KAAM,EACf;EACF;AAED,SAAO,KAAK;CACb;CAED,MAAM,aAA4B;AAChC,MAAI,KAAK,cAAc,KAAM;AAC7B,OAAK,UAAW,MAAM,KAAM,OAAO;AACnC,SAAO,MAAM,KAAK,WAAW,QAAQ;CACtC;CAED,MAAM,KACJC,WACAC,WACAC,SACAC,SACAJ,aACA;EACA,MAAM,EAAE,MAAM,QAAQ,GAAG,KAAK,SAAS,UACrC,WACA,WACA,SACA,SACA,YACD;AACD,QAAM,KAAK,KAAK,kBAAkB,KAAK,OAAO;AAC9C,SAAO;CACR;CAED,MAAM,KACJK,aACAC,QACe;AACf,MAAI,KAAK,WAAY,OAAM,KAAK;AAChC,OAAK,UAAW,KAAK,OAAO,aAAa,aAAa,QAAQ,EAAE,OAAO,CAAC;CACzE;AACF;AAED,SAAS,MAAMC,QAAqBC,OAAe;AACjD,QAAO,IAAI,QAAc,CAAC,YAAY;AACpC,SAAO,iBAAiB,OAAO,MAAM,SAAS,EAAE,EAAE,MAAM,KAAM,EAAC;CAChE;AACF","names":["protocol: Protocol","options: WebSocketClientTransportOptions","auth: any","transformer: ProtocolBaseTransformer","namespace: string","procedure: string","payload: any","options: ProtocolBaseClientCallOptions","messageType: ClientMessageType","buffer: ArrayBuffer","target: EventTarget","event: string"],"sources":["../src/index.ts"],"sourcesContent":["import { ClientMessageType, concat, encodeNumber } from '@nmtjs/protocol'\nimport {\n type Protocol,\n type ProtocolBaseClientCallOptions,\n type ProtocolBaseTransformer,\n ProtocolTransport,\n} from '@nmtjs/protocol/client'\n\nexport type WebSocketClientTransportOptions = {\n /**\n * The origin of the server\n * @example 'http://localhost:3000'\n */\n origin: string\n /**\n * Custom WebSocket class\n * @default globalThis.WebSocket\n */\n wsFactory?: (url: URL) => WebSocket\n\n debug?: boolean\n}\n\nexport class WebSocketClientTransport extends ProtocolTransport {\n protected webSocket: WebSocket | null = null\n protected connecting: Promise<void> | null = null\n protected options: WebSocketClientTransportOptions\n\n constructor(\n protected readonly protocol: Protocol,\n options: WebSocketClientTransportOptions,\n ) {\n super()\n this.options = {\n debug: false,\n ...options,\n }\n }\n\n connect(auth: any, transformer: ProtocolBaseTransformer): Promise<void> {\n // this.auth = auth\n const wsUrl = new URL('/api', this.options.origin)\n if (this.protocol.contentType) {\n wsUrl.searchParams.set('content-type', this.protocol.contentType)\n wsUrl.searchParams.set('accept', this.protocol.contentType)\n }\n if (auth) wsUrl.searchParams.set('auth', auth)\n\n const ws =\n this.options.wsFactory?.(wsUrl) ?? new WebSocket(wsUrl.toString())\n\n ws.binaryType = 'arraybuffer'\n\n ws.addEventListener('message', ({ data }) => {\n this.protocol.handleServerMessage(data as ArrayBuffer, this, transformer)\n })\n\n ws.addEventListener(\n 'close',\n (event) => {\n console.dir(event)\n if (event.code !== 1000) this.emit('disconnected')\n this.webSocket = null\n },\n { once: true },\n )\n\n this.webSocket = ws\n\n this.connecting = new Promise((resolve, reject) => {\n ws.addEventListener(\n 'open',\n () => {\n this.emit('connected')\n resolve()\n },\n { once: true },\n )\n\n ws.addEventListener(\n 'error',\n (event) => {\n reject(new Error('WebSocket error', { cause: event }))\n },\n { once: true },\n )\n })\n\n return this.connecting\n }\n\n async disconnect(): Promise<void> {\n if (this.webSocket === null) return\n this.webSocket!.close(1000, 'user')\n return _once(this.webSocket, 'close')\n }\n\n async call(\n namespace: string,\n procedure: string,\n payload: any,\n options: ProtocolBaseClientCallOptions,\n transformer: ProtocolBaseTransformer,\n ) {\n const { call, buffer } = this.protocol.createRpc(\n namespace,\n procedure,\n payload,\n options,\n transformer,\n )\n await this.send(ClientMessageType.Rpc, buffer)\n return call\n }\n\n async send(\n messageType: ClientMessageType,\n buffer: ArrayBuffer,\n ): Promise<void> {\n if (this.connecting) await this.connecting\n this.webSocket!.send(concat(encodeNumber(messageType, 'Uint8'), buffer))\n }\n}\n\nfunction _once(target: EventTarget, event: string) {\n return new Promise<void>((resolve) => {\n target.addEventListener(event, () => resolve(), { once: true })\n })\n}\n"],"version":3,"file":"index.js"}
|
package/package.json
CHANGED
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
}
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@nmtjs/client": "0.11.
|
|
12
|
-
"@nmtjs/common": "0.11.
|
|
13
|
-
"@nmtjs/protocol": "0.11.
|
|
11
|
+
"@nmtjs/client": "0.11.2",
|
|
12
|
+
"@nmtjs/common": "0.11.2",
|
|
13
|
+
"@nmtjs/protocol": "0.11.2"
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
|
-
"@nmtjs/client": "0.11.
|
|
17
|
-
"@nmtjs/common": "0.11.
|
|
18
|
-
"@nmtjs/protocol": "0.11.
|
|
16
|
+
"@nmtjs/client": "0.11.2",
|
|
17
|
+
"@nmtjs/common": "0.11.2",
|
|
18
|
+
"@nmtjs/protocol": "0.11.2"
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
21
|
"src",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"LICENSE.md",
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
|
-
"version": "0.11.
|
|
26
|
+
"version": "0.11.2",
|
|
27
27
|
"scripts": {
|
|
28
28
|
"build": "neemata-build --root=./src './*.ts'",
|
|
29
29
|
"type-check": "tsc --noEmit"
|
package/src/index.ts
CHANGED
|
@@ -12,11 +12,6 @@ export type WebSocketClientTransportOptions = {
|
|
|
12
12
|
* @example 'http://localhost:3000'
|
|
13
13
|
*/
|
|
14
14
|
origin: string
|
|
15
|
-
/**
|
|
16
|
-
* Whether to autoreconnect on close
|
|
17
|
-
* @default true
|
|
18
|
-
*/
|
|
19
|
-
autoreconnect?: boolean
|
|
20
15
|
/**
|
|
21
16
|
* Custom WebSocket class
|
|
22
17
|
* @default globalThis.WebSocket
|
|
@@ -29,15 +24,21 @@ export type WebSocketClientTransportOptions = {
|
|
|
29
24
|
export class WebSocketClientTransport extends ProtocolTransport {
|
|
30
25
|
protected webSocket: WebSocket | null = null
|
|
31
26
|
protected connecting: Promise<void> | null = null
|
|
27
|
+
protected options: WebSocketClientTransportOptions
|
|
32
28
|
|
|
33
29
|
constructor(
|
|
34
30
|
protected readonly protocol: Protocol,
|
|
35
|
-
|
|
31
|
+
options: WebSocketClientTransportOptions,
|
|
36
32
|
) {
|
|
37
33
|
super()
|
|
34
|
+
this.options = {
|
|
35
|
+
debug: false,
|
|
36
|
+
...options,
|
|
37
|
+
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
connect(auth: any, transformer: ProtocolBaseTransformer): Promise<void> {
|
|
41
|
+
// this.auth = auth
|
|
41
42
|
const wsUrl = new URL('/api', this.options.origin)
|
|
42
43
|
if (this.protocol.contentType) {
|
|
43
44
|
wsUrl.searchParams.set('content-type', this.protocol.contentType)
|
|
@@ -54,13 +55,23 @@ export class WebSocketClientTransport extends ProtocolTransport {
|
|
|
54
55
|
this.protocol.handleServerMessage(data as ArrayBuffer, this, transformer)
|
|
55
56
|
})
|
|
56
57
|
|
|
58
|
+
ws.addEventListener(
|
|
59
|
+
'close',
|
|
60
|
+
(event) => {
|
|
61
|
+
console.dir(event)
|
|
62
|
+
if (event.code !== 1000) this.emit('disconnected')
|
|
63
|
+
this.webSocket = null
|
|
64
|
+
},
|
|
65
|
+
{ once: true },
|
|
66
|
+
)
|
|
67
|
+
|
|
57
68
|
this.webSocket = ws
|
|
58
69
|
|
|
59
70
|
this.connecting = new Promise((resolve, reject) => {
|
|
60
71
|
ws.addEventListener(
|
|
61
72
|
'open',
|
|
62
73
|
() => {
|
|
63
|
-
this.
|
|
74
|
+
this.emit('connected')
|
|
64
75
|
resolve()
|
|
65
76
|
},
|
|
66
77
|
{ once: true },
|
|
@@ -73,18 +84,6 @@ export class WebSocketClientTransport extends ProtocolTransport {
|
|
|
73
84
|
},
|
|
74
85
|
{ once: true },
|
|
75
86
|
)
|
|
76
|
-
|
|
77
|
-
ws.addEventListener(
|
|
78
|
-
'close',
|
|
79
|
-
() => {
|
|
80
|
-
this.protocol.emit('disconnected')
|
|
81
|
-
this.webSocket = null
|
|
82
|
-
if (this.options.autoreconnect === true) {
|
|
83
|
-
setTimeout(this.connect.bind(this), 1000)
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
{ once: true },
|
|
87
|
-
)
|
|
88
87
|
})
|
|
89
88
|
|
|
90
89
|
return this.connecting
|
|
@@ -92,7 +91,7 @@ export class WebSocketClientTransport extends ProtocolTransport {
|
|
|
92
91
|
|
|
93
92
|
async disconnect(): Promise<void> {
|
|
94
93
|
if (this.webSocket === null) return
|
|
95
|
-
this.webSocket!.close()
|
|
94
|
+
this.webSocket!.close(1000, 'user')
|
|
96
95
|
return _once(this.webSocket, 'close')
|
|
97
96
|
}
|
|
98
97
|
|