@nmtjs/ws-client 0.12.5 → 0.12.7
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.d.ts +30 -0
- package/dist/index.js +68 -64
- package/package.json +11 -11
- package/dist/index.js.map +0 -1
- package/src/index.ts +0 -134
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ClientMessageType } from '@nmtjs/protocol';
|
|
2
|
+
import { type Protocol, type ProtocolBaseClientCallOptions, type ProtocolBaseTransformer, ProtocolTransport } from '@nmtjs/protocol/client';
|
|
3
|
+
export type WebSocketClientTransportOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* The origin of the server
|
|
6
|
+
* @example 'http://localhost:3000'
|
|
7
|
+
*/
|
|
8
|
+
origin: string;
|
|
9
|
+
/**
|
|
10
|
+
* Custom WebSocket class
|
|
11
|
+
* @default globalThis.WebSocket
|
|
12
|
+
*/
|
|
13
|
+
wsFactory?: (url: URL) => WebSocket;
|
|
14
|
+
debug?: boolean;
|
|
15
|
+
};
|
|
16
|
+
export declare class WebSocketClientTransport extends ProtocolTransport {
|
|
17
|
+
protected readonly protocol: Protocol;
|
|
18
|
+
protected webSocket: WebSocket | null;
|
|
19
|
+
protected connecting: Promise<void> | null;
|
|
20
|
+
protected options: WebSocketClientTransportOptions;
|
|
21
|
+
constructor(protocol: Protocol, options: WebSocketClientTransportOptions);
|
|
22
|
+
connect(auth: any, transformer: ProtocolBaseTransformer): Promise<void>;
|
|
23
|
+
disconnect(): Promise<void>;
|
|
24
|
+
call(namespace: string, procedure: string, payload: any, options: ProtocolBaseClientCallOptions, transformer: ProtocolBaseTransformer): Promise<import("@nmtjs/common").InteractivePromise<unknown> & {
|
|
25
|
+
namespace: string;
|
|
26
|
+
procedure: string;
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
}>;
|
|
29
|
+
send(messageType: ClientMessageType, buffer: ArrayBuffer): Promise<void>;
|
|
30
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,68 +1,72 @@
|
|
|
1
|
-
import { ClientMessageType, concat, encodeNumber } from
|
|
2
|
-
import { ProtocolTransport, ProtocolTransportStatus } from
|
|
1
|
+
import { ClientMessageType, concat, encodeNumber } from '@nmtjs/protocol';
|
|
2
|
+
import { ProtocolTransport, ProtocolTransportStatus, } from '@nmtjs/protocol/client';
|
|
3
3
|
export class WebSocketClientTransport extends ProtocolTransport {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
4
|
+
protocol;
|
|
5
|
+
webSocket = null;
|
|
6
|
+
connecting = null;
|
|
7
|
+
options;
|
|
8
|
+
constructor(protocol, options) {
|
|
9
|
+
super();
|
|
10
|
+
this.protocol = protocol;
|
|
11
|
+
this.options = {
|
|
12
|
+
debug: false,
|
|
13
|
+
...options,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
connect(auth, transformer) {
|
|
17
|
+
// this.auth = auth
|
|
18
|
+
const wsUrl = new URL('/api', this.options.origin);
|
|
19
|
+
if (this.protocol.contentType) {
|
|
20
|
+
wsUrl.searchParams.set('content-type', this.protocol.contentType);
|
|
21
|
+
wsUrl.searchParams.set('accept', this.protocol.contentType);
|
|
22
|
+
}
|
|
23
|
+
if (auth)
|
|
24
|
+
wsUrl.searchParams.set('auth', auth);
|
|
25
|
+
const ws = this.options.wsFactory?.(wsUrl) ?? new WebSocket(wsUrl.toString());
|
|
26
|
+
ws.binaryType = 'arraybuffer';
|
|
27
|
+
this.status = ProtocolTransportStatus.CONNECTING;
|
|
28
|
+
ws.addEventListener('message', ({ data }) => {
|
|
29
|
+
this.protocol.handleServerMessage(data, this, transformer);
|
|
30
|
+
});
|
|
31
|
+
ws.addEventListener('close', (event) => {
|
|
32
|
+
console.dir(event);
|
|
33
|
+
this.status = ProtocolTransportStatus.DISCONNECTED;
|
|
34
|
+
if (event.code !== 1000)
|
|
35
|
+
this.emit('disconnected');
|
|
36
|
+
this.webSocket = null;
|
|
37
|
+
}, { once: true });
|
|
38
|
+
this.webSocket = ws;
|
|
39
|
+
this.connecting = new Promise((resolve, reject) => {
|
|
40
|
+
ws.addEventListener('open', () => {
|
|
41
|
+
this.status = ProtocolTransportStatus.CONNECTED;
|
|
42
|
+
this.emit('connected');
|
|
43
|
+
resolve();
|
|
44
|
+
}, { once: true });
|
|
45
|
+
ws.addEventListener('error', (event) => {
|
|
46
|
+
reject(new Error('WebSocket error', { cause: event }));
|
|
47
|
+
}, { once: true });
|
|
48
|
+
});
|
|
49
|
+
return this.connecting;
|
|
50
|
+
}
|
|
51
|
+
async disconnect() {
|
|
52
|
+
if (this.webSocket === null)
|
|
53
|
+
return;
|
|
54
|
+
this.webSocket.close(1000, 'user');
|
|
55
|
+
return _once(this.webSocket, 'close');
|
|
56
|
+
}
|
|
57
|
+
async call(namespace, procedure, payload, options, transformer) {
|
|
58
|
+
const { call, buffer } = this.protocol.createRpc(namespace, procedure, payload, options, transformer);
|
|
59
|
+
await this.send(ClientMessageType.Rpc, buffer);
|
|
60
|
+
return call;
|
|
61
|
+
}
|
|
62
|
+
async send(messageType, buffer) {
|
|
63
|
+
if (this.connecting)
|
|
64
|
+
await this.connecting;
|
|
65
|
+
this.webSocket.send(concat(encodeNumber(messageType, 'Uint8'), buffer));
|
|
66
|
+
}
|
|
61
67
|
}
|
|
62
68
|
function _once(target, event) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
return new Promise((resolve) => {
|
|
70
|
+
target.addEventListener(event, () => resolve(), { once: true });
|
|
71
|
+
});
|
|
66
72
|
}
|
|
67
|
-
|
|
68
|
-
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -3,29 +3,29 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": {
|
|
6
|
-
"types": "./
|
|
7
|
-
"import": "./dist/index.js"
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"import": "./dist/index.js",
|
|
8
|
+
"module-sync": "./dist/index.js"
|
|
8
9
|
}
|
|
9
10
|
},
|
|
10
11
|
"dependencies": {
|
|
11
|
-
"@nmtjs/
|
|
12
|
-
"@nmtjs/
|
|
13
|
-
"@nmtjs/
|
|
12
|
+
"@nmtjs/common": "0.12.7",
|
|
13
|
+
"@nmtjs/protocol": "0.12.7",
|
|
14
|
+
"@nmtjs/client": "0.12.7"
|
|
14
15
|
},
|
|
15
16
|
"peerDependencies": {
|
|
16
|
-
"@nmtjs/client": "0.12.
|
|
17
|
-
"@nmtjs/
|
|
18
|
-
"@nmtjs/
|
|
17
|
+
"@nmtjs/client": "0.12.7",
|
|
18
|
+
"@nmtjs/protocol": "0.12.7",
|
|
19
|
+
"@nmtjs/common": "0.12.7"
|
|
19
20
|
},
|
|
20
21
|
"files": [
|
|
21
|
-
"src",
|
|
22
22
|
"dist",
|
|
23
23
|
"LICENSE.md",
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
|
-
"version": "0.12.
|
|
26
|
+
"version": "0.12.7",
|
|
27
27
|
"scripts": {
|
|
28
|
-
"build": "
|
|
28
|
+
"build": "tsc",
|
|
29
29
|
"type-check": "tsc --noEmit"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAS,mBAAmB,QAAQ,oBAAoB,iBAAiB;AACzE,SAIE,mBACA,+BACK,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,OAAK,SAAS,wBAAwB;AAEtC,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,QAAK,SAAS,wBAAwB;AACtC,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,SAAS,wBAAwB;AACtC,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 ProtocolTransportStatus,\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 this.status = ProtocolTransportStatus.CONNECTING\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 this.status = ProtocolTransportStatus.DISCONNECTED\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.status = ProtocolTransportStatus.CONNECTED\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/src/index.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { ClientMessageType, concat, encodeNumber } from '@nmtjs/protocol'
|
|
2
|
-
import {
|
|
3
|
-
type Protocol,
|
|
4
|
-
type ProtocolBaseClientCallOptions,
|
|
5
|
-
type ProtocolBaseTransformer,
|
|
6
|
-
ProtocolTransport,
|
|
7
|
-
ProtocolTransportStatus,
|
|
8
|
-
} from '@nmtjs/protocol/client'
|
|
9
|
-
|
|
10
|
-
export type WebSocketClientTransportOptions = {
|
|
11
|
-
/**
|
|
12
|
-
* The origin of the server
|
|
13
|
-
* @example 'http://localhost:3000'
|
|
14
|
-
*/
|
|
15
|
-
origin: string
|
|
16
|
-
/**
|
|
17
|
-
* Custom WebSocket class
|
|
18
|
-
* @default globalThis.WebSocket
|
|
19
|
-
*/
|
|
20
|
-
wsFactory?: (url: URL) => WebSocket
|
|
21
|
-
|
|
22
|
-
debug?: boolean
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class WebSocketClientTransport extends ProtocolTransport {
|
|
26
|
-
protected webSocket: WebSocket | null = null
|
|
27
|
-
protected connecting: Promise<void> | null = null
|
|
28
|
-
protected options: WebSocketClientTransportOptions
|
|
29
|
-
|
|
30
|
-
constructor(
|
|
31
|
-
protected readonly protocol: Protocol,
|
|
32
|
-
options: WebSocketClientTransportOptions,
|
|
33
|
-
) {
|
|
34
|
-
super()
|
|
35
|
-
this.options = {
|
|
36
|
-
debug: false,
|
|
37
|
-
...options,
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
connect(auth: any, transformer: ProtocolBaseTransformer): Promise<void> {
|
|
42
|
-
// this.auth = auth
|
|
43
|
-
const wsUrl = new URL('/api', this.options.origin)
|
|
44
|
-
if (this.protocol.contentType) {
|
|
45
|
-
wsUrl.searchParams.set('content-type', this.protocol.contentType)
|
|
46
|
-
wsUrl.searchParams.set('accept', this.protocol.contentType)
|
|
47
|
-
}
|
|
48
|
-
if (auth) wsUrl.searchParams.set('auth', auth)
|
|
49
|
-
|
|
50
|
-
const ws =
|
|
51
|
-
this.options.wsFactory?.(wsUrl) ?? new WebSocket(wsUrl.toString())
|
|
52
|
-
|
|
53
|
-
ws.binaryType = 'arraybuffer'
|
|
54
|
-
|
|
55
|
-
this.status = ProtocolTransportStatus.CONNECTING
|
|
56
|
-
|
|
57
|
-
ws.addEventListener('message', ({ data }) => {
|
|
58
|
-
this.protocol.handleServerMessage(data as ArrayBuffer, this, transformer)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
ws.addEventListener(
|
|
62
|
-
'close',
|
|
63
|
-
(event) => {
|
|
64
|
-
console.dir(event)
|
|
65
|
-
this.status = ProtocolTransportStatus.DISCONNECTED
|
|
66
|
-
if (event.code !== 1000) this.emit('disconnected')
|
|
67
|
-
this.webSocket = null
|
|
68
|
-
},
|
|
69
|
-
{ once: true },
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
this.webSocket = ws
|
|
73
|
-
|
|
74
|
-
this.connecting = new Promise((resolve, reject) => {
|
|
75
|
-
ws.addEventListener(
|
|
76
|
-
'open',
|
|
77
|
-
() => {
|
|
78
|
-
this.status = ProtocolTransportStatus.CONNECTED
|
|
79
|
-
this.emit('connected')
|
|
80
|
-
resolve()
|
|
81
|
-
},
|
|
82
|
-
{ once: true },
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
ws.addEventListener(
|
|
86
|
-
'error',
|
|
87
|
-
(event) => {
|
|
88
|
-
reject(new Error('WebSocket error', { cause: event }))
|
|
89
|
-
},
|
|
90
|
-
{ once: true },
|
|
91
|
-
)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
return this.connecting
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async disconnect(): Promise<void> {
|
|
98
|
-
if (this.webSocket === null) return
|
|
99
|
-
this.webSocket!.close(1000, 'user')
|
|
100
|
-
return _once(this.webSocket, 'close')
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async call(
|
|
104
|
-
namespace: string,
|
|
105
|
-
procedure: string,
|
|
106
|
-
payload: any,
|
|
107
|
-
options: ProtocolBaseClientCallOptions,
|
|
108
|
-
transformer: ProtocolBaseTransformer,
|
|
109
|
-
) {
|
|
110
|
-
const { call, buffer } = this.protocol.createRpc(
|
|
111
|
-
namespace,
|
|
112
|
-
procedure,
|
|
113
|
-
payload,
|
|
114
|
-
options,
|
|
115
|
-
transformer,
|
|
116
|
-
)
|
|
117
|
-
await this.send(ClientMessageType.Rpc, buffer)
|
|
118
|
-
return call
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async send(
|
|
122
|
-
messageType: ClientMessageType,
|
|
123
|
-
buffer: ArrayBuffer,
|
|
124
|
-
): Promise<void> {
|
|
125
|
-
if (this.connecting) await this.connecting
|
|
126
|
-
this.webSocket!.send(concat(encodeNumber(messageType, 'Uint8'), buffer))
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function _once(target: EventTarget, event: string) {
|
|
131
|
-
return new Promise<void>((resolve) => {
|
|
132
|
-
target.addEventListener(event, () => resolve(), { once: true })
|
|
133
|
-
})
|
|
134
|
-
}
|