@nmtjs/client 0.15.2 → 0.16.0-beta.1
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/client.d.ts +64 -0
- package/dist/client.js +97 -0
- package/dist/client.js.map +1 -0
- package/dist/clients/runtime.d.ts +6 -12
- package/dist/clients/runtime.js +58 -57
- package/dist/clients/runtime.js.map +1 -1
- package/dist/clients/static.d.ts +4 -9
- package/dist/clients/static.js +20 -20
- package/dist/clients/static.js.map +1 -1
- package/dist/core.d.ts +33 -83
- package/dist/core.js +305 -690
- package/dist/core.js.map +1 -1
- package/dist/events.d.ts +0 -1
- package/dist/events.js +74 -11
- package/dist/events.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/layers/ping.d.ts +6 -0
- package/dist/layers/ping.js +65 -0
- package/dist/layers/ping.js.map +1 -0
- package/dist/layers/rpc.d.ts +19 -0
- package/dist/layers/rpc.js +521 -0
- package/dist/layers/rpc.js.map +1 -0
- package/dist/layers/streams.d.ts +20 -0
- package/dist/layers/streams.js +194 -0
- package/dist/layers/streams.js.map +1 -0
- package/dist/plugins/browser.js +28 -9
- package/dist/plugins/browser.js.map +1 -1
- package/dist/plugins/heartbeat.js +10 -10
- package/dist/plugins/heartbeat.js.map +1 -1
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/index.js +0 -1
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/reconnect.js +11 -94
- package/dist/plugins/reconnect.js.map +1 -1
- package/dist/plugins/types.d.ts +27 -11
- package/dist/transport.d.ts +49 -31
- package/dist/types.d.ts +21 -5
- package/package.json +10 -10
- package/src/client.ts +216 -0
- package/src/clients/runtime.ts +93 -79
- package/src/clients/static.ts +46 -38
- package/src/core.ts +394 -901
- package/src/events.ts +113 -14
- package/src/index.ts +4 -0
- package/src/layers/ping.ts +99 -0
- package/src/layers/rpc.ts +725 -0
- package/src/layers/streams.ts +277 -0
- package/src/plugins/browser.ts +39 -9
- package/src/plugins/heartbeat.ts +10 -10
- package/src/plugins/index.ts +8 -1
- package/src/plugins/reconnect.ts +12 -119
- package/src/plugins/types.ts +30 -13
- package/src/transport.ts +75 -46
- package/src/types.ts +33 -8
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { MAX_UINT32, noopFn } from '@nmtjs/common';
|
|
2
|
+
import { ClientMessageType, kBlobKey, ServerMessageType } from '@nmtjs/protocol';
|
|
3
|
+
import { ProtocolServerBlobStream } from '@nmtjs/protocol/client';
|
|
4
|
+
import { ClientStreams, ServerStreams } from '../streams.js';
|
|
5
|
+
const DEFAULT_PULL_SIZE = 65535;
|
|
6
|
+
const toReasonString = (reason) => {
|
|
7
|
+
if (typeof reason === 'string')
|
|
8
|
+
return reason;
|
|
9
|
+
if (reason === undefined || reason === null)
|
|
10
|
+
return undefined;
|
|
11
|
+
return String(reason);
|
|
12
|
+
};
|
|
13
|
+
export const createServerBlobConsumer = (metadata, subscribe) => {
|
|
14
|
+
const consumer = ((options) => subscribe(options));
|
|
15
|
+
Object.defineProperties(consumer, {
|
|
16
|
+
metadata: {
|
|
17
|
+
configurable: false,
|
|
18
|
+
enumerable: true,
|
|
19
|
+
writable: false,
|
|
20
|
+
value: metadata,
|
|
21
|
+
},
|
|
22
|
+
[kBlobKey]: {
|
|
23
|
+
configurable: false,
|
|
24
|
+
enumerable: false,
|
|
25
|
+
writable: false,
|
|
26
|
+
value: true,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
return consumer;
|
|
30
|
+
};
|
|
31
|
+
export const createStreamLayer = (core) => {
|
|
32
|
+
const clientStreams = new ClientStreams();
|
|
33
|
+
const serverStreams = new ServerStreams();
|
|
34
|
+
let streamId = 0;
|
|
35
|
+
const getStreamId = () => {
|
|
36
|
+
if (streamId >= MAX_UINT32) {
|
|
37
|
+
streamId = 0;
|
|
38
|
+
}
|
|
39
|
+
return streamId++;
|
|
40
|
+
};
|
|
41
|
+
const addClientStream = (blob) => {
|
|
42
|
+
const id = getStreamId();
|
|
43
|
+
return clientStreams.add(blob.source, id, blob.metadata);
|
|
44
|
+
};
|
|
45
|
+
const createServerBlobStream = (id, metadata) => {
|
|
46
|
+
const stream = new ProtocolServerBlobStream(metadata, {
|
|
47
|
+
pull: () => {
|
|
48
|
+
if (!core.messageContext)
|
|
49
|
+
return;
|
|
50
|
+
core.emitStreamEvent({
|
|
51
|
+
direction: 'outgoing',
|
|
52
|
+
streamType: 'server_blob',
|
|
53
|
+
action: 'pull',
|
|
54
|
+
streamId: id,
|
|
55
|
+
byteLength: DEFAULT_PULL_SIZE,
|
|
56
|
+
});
|
|
57
|
+
const buffer = core.protocol.encodeMessage(core.messageContext, ClientMessageType.ServerStreamPull, { streamId: id, size: DEFAULT_PULL_SIZE });
|
|
58
|
+
core.send(buffer).catch(noopFn);
|
|
59
|
+
},
|
|
60
|
+
close: () => {
|
|
61
|
+
serverStreams.remove(id);
|
|
62
|
+
},
|
|
63
|
+
readableStrategy: { highWaterMark: 0 },
|
|
64
|
+
});
|
|
65
|
+
serverStreams.add(id, stream);
|
|
66
|
+
return createServerBlobConsumer(metadata, ({ signal } = {}) => {
|
|
67
|
+
if (signal) {
|
|
68
|
+
signal.addEventListener('abort', () => {
|
|
69
|
+
if (!core.messageContext)
|
|
70
|
+
return;
|
|
71
|
+
core.emitStreamEvent({
|
|
72
|
+
direction: 'outgoing',
|
|
73
|
+
streamType: 'server_blob',
|
|
74
|
+
action: 'abort',
|
|
75
|
+
streamId: id,
|
|
76
|
+
reason: toReasonString(signal.reason),
|
|
77
|
+
});
|
|
78
|
+
const buffer = core.protocol.encodeMessage(core.messageContext, ClientMessageType.ServerStreamAbort, { streamId: id, reason: toReasonString(signal.reason) });
|
|
79
|
+
core.send(buffer).catch(noopFn);
|
|
80
|
+
void serverStreams.abort(id).catch(noopFn);
|
|
81
|
+
}, { once: true });
|
|
82
|
+
}
|
|
83
|
+
return stream;
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
const addServerBlobStream = (metadata) => {
|
|
87
|
+
const id = getStreamId();
|
|
88
|
+
const stream = new ProtocolServerBlobStream(metadata);
|
|
89
|
+
serverStreams.add(id, stream);
|
|
90
|
+
return { streamId: id, stream };
|
|
91
|
+
};
|
|
92
|
+
core.on('message', (message) => {
|
|
93
|
+
switch (message.type) {
|
|
94
|
+
case ServerMessageType.ServerStreamPush:
|
|
95
|
+
core.emitStreamEvent({
|
|
96
|
+
direction: 'incoming',
|
|
97
|
+
streamType: 'server_blob',
|
|
98
|
+
action: 'push',
|
|
99
|
+
streamId: message.streamId,
|
|
100
|
+
byteLength: message.chunk.byteLength,
|
|
101
|
+
});
|
|
102
|
+
void serverStreams.push(message.streamId, message.chunk);
|
|
103
|
+
break;
|
|
104
|
+
case ServerMessageType.ServerStreamEnd:
|
|
105
|
+
core.emitStreamEvent({
|
|
106
|
+
direction: 'incoming',
|
|
107
|
+
streamType: 'server_blob',
|
|
108
|
+
action: 'end',
|
|
109
|
+
streamId: message.streamId,
|
|
110
|
+
});
|
|
111
|
+
void serverStreams.end(message.streamId);
|
|
112
|
+
break;
|
|
113
|
+
case ServerMessageType.ServerStreamAbort:
|
|
114
|
+
core.emitStreamEvent({
|
|
115
|
+
direction: 'incoming',
|
|
116
|
+
streamType: 'server_blob',
|
|
117
|
+
action: 'abort',
|
|
118
|
+
streamId: message.streamId,
|
|
119
|
+
reason: message.reason,
|
|
120
|
+
});
|
|
121
|
+
void serverStreams.abort(message.streamId);
|
|
122
|
+
break;
|
|
123
|
+
case ServerMessageType.ClientStreamPull:
|
|
124
|
+
core.emitStreamEvent({
|
|
125
|
+
direction: 'incoming',
|
|
126
|
+
streamType: 'client_blob',
|
|
127
|
+
action: 'pull',
|
|
128
|
+
streamId: message.streamId,
|
|
129
|
+
byteLength: message.size,
|
|
130
|
+
});
|
|
131
|
+
void clientStreams.pull(message.streamId, message.size).then((chunk) => {
|
|
132
|
+
if (!core.messageContext)
|
|
133
|
+
return;
|
|
134
|
+
if (chunk) {
|
|
135
|
+
core.emitStreamEvent({
|
|
136
|
+
direction: 'outgoing',
|
|
137
|
+
streamType: 'client_blob',
|
|
138
|
+
action: 'push',
|
|
139
|
+
streamId: message.streamId,
|
|
140
|
+
byteLength: chunk.byteLength,
|
|
141
|
+
});
|
|
142
|
+
const buffer = core.protocol.encodeMessage(core.messageContext, ClientMessageType.ClientStreamPush, { streamId: message.streamId, chunk });
|
|
143
|
+
core.send(buffer).catch(noopFn);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
core.emitStreamEvent({
|
|
147
|
+
direction: 'outgoing',
|
|
148
|
+
streamType: 'client_blob',
|
|
149
|
+
action: 'end',
|
|
150
|
+
streamId: message.streamId,
|
|
151
|
+
});
|
|
152
|
+
const buffer = core.protocol.encodeMessage(core.messageContext, ClientMessageType.ClientStreamEnd, { streamId: message.streamId });
|
|
153
|
+
core.send(buffer).catch(noopFn);
|
|
154
|
+
void clientStreams.end(message.streamId).catch(noopFn);
|
|
155
|
+
}, () => {
|
|
156
|
+
if (!core.messageContext)
|
|
157
|
+
return;
|
|
158
|
+
core.emitStreamEvent({
|
|
159
|
+
direction: 'outgoing',
|
|
160
|
+
streamType: 'client_blob',
|
|
161
|
+
action: 'abort',
|
|
162
|
+
streamId: message.streamId,
|
|
163
|
+
});
|
|
164
|
+
const buffer = core.protocol.encodeMessage(core.messageContext, ClientMessageType.ClientStreamAbort, { streamId: message.streamId });
|
|
165
|
+
core.send(buffer).catch(noopFn);
|
|
166
|
+
clientStreams.remove(message.streamId);
|
|
167
|
+
});
|
|
168
|
+
break;
|
|
169
|
+
case ServerMessageType.ClientStreamAbort:
|
|
170
|
+
core.emitStreamEvent({
|
|
171
|
+
direction: 'incoming',
|
|
172
|
+
streamType: 'client_blob',
|
|
173
|
+
action: 'abort',
|
|
174
|
+
streamId: message.streamId,
|
|
175
|
+
reason: message.reason,
|
|
176
|
+
});
|
|
177
|
+
void clientStreams.abort(message.streamId, message.reason).catch(noopFn);
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
core.on('disconnected', (reason) => {
|
|
182
|
+
void clientStreams.clear(reason).catch(noopFn);
|
|
183
|
+
void serverStreams.clear(reason).catch(noopFn);
|
|
184
|
+
});
|
|
185
|
+
return {
|
|
186
|
+
clientStreams,
|
|
187
|
+
serverStreams,
|
|
188
|
+
getStreamId,
|
|
189
|
+
addClientStream,
|
|
190
|
+
createServerBlobStream,
|
|
191
|
+
addServerBlobStream,
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
//# sourceMappingURL=streams.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streams.js","sourceRoot":"","sources":["../../src/layers/streams.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAChF,OAAO,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAA;AAGjE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAE5D,MAAM,iBAAiB,GAAG,KAAK,CAAA;AAE/B,MAAM,cAAc,GAAG,CAAC,MAAe,EAAE,EAAE,CAAC;IAC1C,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAA;IAC7C,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO,SAAS,CAAA;IAC7D,OAAO,MAAM,CAAC,MAAM,CAAC,CAAA;AAAA,CACtB,CAAA;AAiBD,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,QAA8B,EAC9B,SAA2E,EAC/C,EAAE,CAAC;IAC/B,MAAM,QAAQ,GAAG,CAAC,CAAC,OAAkC,EAAE,EAAE,CACvD,SAAS,CAAC,OAAO,CAAC,CAA+B,CA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aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;gBAAA,CACvC,CACF,CAAA;gBACD,MAAK;YACP,KAAK,iBAAiB,CAAC,iBAAiB;gBACtC,IAAI,CAAC,eAAe,CAAC;oBACnB,SAAS,EAAE,UAAU;oBACrB,UAAU,EAAE,aAAa;oBACzB,MAAM,EAAE,OAAO;oBACf,QAAQ,EAAE,OAAO,CAAC,QAAQ;oBAC1B,MAAM,EAAE,OAAO,CAAC,MAAM;iBACvB,CAAC,CAAA;gBACF,KAAK,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;gBACxE,MAAK;QACT,CAAC;IAAA,CACF,CAAC,CAAA;IAEF,IAAI,CAAC,EAAE,CAAC,cAAc,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC;QAClC,KAAK,aAAa,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;QAC9C,KAAK,aAAa,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;IAAA,CAC/C,CAAC,CAAA;IAEF,OAAO;QACL,aAAa;QACb,aAAa;QACb,WAAW;QACX,eAAe;QACf,sBAAsB;QACtB,mBAAmB;KACpB,CAAA;AAAA,CACF,CAAA"}
|
package/dist/plugins/browser.js
CHANGED
|
@@ -1,29 +1,48 @@
|
|
|
1
|
+
const syncPauseReasons = (setPauseReason) => {
|
|
2
|
+
if (globalThis.window && 'navigator' in globalThis.window) {
|
|
3
|
+
setPauseReason('offline', globalThis.window.navigator?.onLine === false);
|
|
4
|
+
}
|
|
5
|
+
if (globalThis.document) {
|
|
6
|
+
setPauseReason('tab_hidden', globalThis.document.visibilityState === 'hidden');
|
|
7
|
+
}
|
|
8
|
+
};
|
|
1
9
|
export const browserConnectivityPlugin = () => {
|
|
2
|
-
return (
|
|
10
|
+
return ({ core }) => {
|
|
3
11
|
const cleanup = [];
|
|
4
|
-
const
|
|
5
|
-
if (
|
|
6
|
-
|
|
12
|
+
const triggerReconnect = () => {
|
|
13
|
+
if (!core.isDisposed()) {
|
|
14
|
+
core.triggerReconnect();
|
|
7
15
|
}
|
|
8
16
|
};
|
|
9
17
|
return {
|
|
10
18
|
name: 'browser-connectivity',
|
|
11
19
|
onInit: () => {
|
|
20
|
+
syncPauseReasons(core.setReconnectPauseReason.bind(core));
|
|
12
21
|
if (globalThis.window) {
|
|
13
|
-
const onPageShow = () =>
|
|
22
|
+
const onPageShow = () => triggerReconnect();
|
|
14
23
|
globalThis.window.addEventListener('pageshow', onPageShow);
|
|
15
24
|
cleanup.push(() => globalThis.window?.removeEventListener('pageshow', onPageShow));
|
|
16
|
-
const onOnline = () =>
|
|
25
|
+
const onOnline = () => {
|
|
26
|
+
core.setReconnectPauseReason('offline', false);
|
|
27
|
+
triggerReconnect();
|
|
28
|
+
};
|
|
17
29
|
globalThis.window.addEventListener('online', onOnline);
|
|
18
30
|
cleanup.push(() => globalThis.window?.removeEventListener('online', onOnline));
|
|
19
|
-
const
|
|
31
|
+
const onOffline = () => {
|
|
32
|
+
core.setReconnectPauseReason('offline', true);
|
|
33
|
+
};
|
|
34
|
+
globalThis.window.addEventListener('offline', onOffline);
|
|
35
|
+
cleanup.push(() => globalThis.window?.removeEventListener('offline', onOffline));
|
|
36
|
+
const onFocus = () => triggerReconnect();
|
|
20
37
|
globalThis.window.addEventListener('focus', onFocus);
|
|
21
38
|
cleanup.push(() => globalThis.window?.removeEventListener('focus', onFocus));
|
|
22
39
|
}
|
|
23
40
|
if (globalThis.document) {
|
|
24
41
|
const onVisibilityChange = () => {
|
|
25
|
-
|
|
26
|
-
|
|
42
|
+
const hidden = globalThis.document?.visibilityState === 'hidden';
|
|
43
|
+
core.setReconnectPauseReason('tab_hidden', hidden);
|
|
44
|
+
if (!hidden) {
|
|
45
|
+
triggerReconnect();
|
|
27
46
|
}
|
|
28
47
|
};
|
|
29
48
|
globalThis.document.addEventListener('visibilitychange', onVisibilityChange);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/plugins/browser.ts"],"names":[],"mappings":"AAEA,MAAM,
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/plugins/browser.ts"],"names":[],"mappings":"AAEA,MAAM,gBAAgB,GAAG,CACvB,cAAyD,EACzD,EAAE,CAAC;IACH,IAAI,UAAU,CAAC,MAAM,IAAI,WAAW,IAAI,UAAU,CAAC,MAAM,EAAE,CAAC;QAC1D,cAAc,CAAC,SAAS,EAAE,UAAU,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,KAAK,KAAK,CAAC,CAAA;IAC1E,CAAC;IAED,IAAI,UAAU,CAAC,QAAQ,EAAE,CAAC;QACxB,cAAc,CACZ,YAAY,EACZ,UAAU,CAAC,QAAQ,CAAC,eAAe,KAAK,QAAQ,CACjD,CAAA;IACH,CAAC;AAAA,CACF,CAAA;AAED,MAAM,CAAC,MAAM,yBAAyB,GAAG,GAAiB,EAAE,CAAC;IAC3D,OAAO,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;QACnB,MAAM,OAAO,GAAsB,EAAE,CAAA;QAErC,MAAM,gBAAgB,GAAG,GAAG,EAAE,CAAC;YAC7B,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;gBACvB,IAAI,CAAC,gBAAgB,EAAE,CAAA;YACzB,CAAC;QAAA,CACF,CAAA;QAED,OAAO;YACL,IAAI,EAAE,sBAAsB;YAC5B,MAAM,EAAE,GAAG,EAAE,CAAC;gBACZ,gBAAgB,CAAC,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;gBAEzD,IAAI,UAAU,CAAC,MAAM,EAAE,CAAC;oBACtB,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC,gBAAgB,EAAE,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B,GAAG,GAAG,EAAE,CAAC;wBAC/B,MAAM,MAAM,GAAG,UAAU,CAAC,QAAQ,EAAE,eAAe,KAAK,QAAQ,CAAA;wBAChE,IAAI,CAAC,uBAAuB,CAAC,YAAY,EAAE,MAAM,CAAC,CAAA;wBAClD,IAAI,CAAC,MAAM,EAAE,CAAC;4BACZ,gBAAgB,EAAE,CAAA;wBACpB,CAAC;oBAAA,CACF,CAAA;oBAED,UAAU,CAAC,QAAQ,CAAC,gBAAgB,CAClC,kBAAkB,EAClB,kBAAkB,CACnB,CAAA;oBACD,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAChB,UAAU,CAAC,QAAQ,EAAE,mBAAmB,CACtC,kBAAkB,EAClB,kBAAkB,CACnB,CACF,CAAA;gBACH,CAAC;YAAA,CACF;YACD,OAAO,EAAE,GAAG,EAAE,CAAC;gBACb,KAAK,MAAM,IAAI,IAAI,OAAO;oBAAE,IAAI,EAAE,CAAA;gBAClC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAA;YAAA,CACnB;SACF,CAAA;IAAA,CACF,CAAA;AAAA,CACF,CAAA"}
|
|
@@ -26,7 +26,7 @@ const isPaused = () => {
|
|
|
26
26
|
return false;
|
|
27
27
|
};
|
|
28
28
|
export const heartbeatPlugin = (options = {}) => {
|
|
29
|
-
return (
|
|
29
|
+
return ({ core, ping }) => {
|
|
30
30
|
const interval = options.interval ?? DEFAULT_HEARTBEAT_INTERVAL;
|
|
31
31
|
const timeout = options.timeout ?? DEFAULT_HEARTBEAT_TIMEOUT;
|
|
32
32
|
let heartbeatAbortController = null;
|
|
@@ -39,32 +39,32 @@ export const heartbeatPlugin = (options = {}) => {
|
|
|
39
39
|
const startHeartbeat = () => {
|
|
40
40
|
if (heartbeatTask)
|
|
41
41
|
return;
|
|
42
|
-
if (
|
|
42
|
+
if (core.transportType !== ConnectionType.Bidirectional)
|
|
43
43
|
return;
|
|
44
44
|
heartbeatAbortController = new AbortController();
|
|
45
45
|
const signal = heartbeatAbortController.signal;
|
|
46
46
|
heartbeatTask = (async () => {
|
|
47
47
|
while (!signal.aborted &&
|
|
48
|
-
!
|
|
49
|
-
|
|
48
|
+
!core.isDisposed() &&
|
|
49
|
+
core.state === 'connected') {
|
|
50
50
|
if (isPaused()) {
|
|
51
51
|
await sleep(1000, signal);
|
|
52
52
|
continue;
|
|
53
53
|
}
|
|
54
54
|
await sleep(interval, signal);
|
|
55
55
|
if (signal.aborted ||
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
core.isDisposed() ||
|
|
57
|
+
core.state !== 'connected') {
|
|
58
58
|
continue;
|
|
59
59
|
}
|
|
60
60
|
try {
|
|
61
|
-
await
|
|
61
|
+
await ping.ping(timeout, signal);
|
|
62
62
|
}
|
|
63
63
|
catch {
|
|
64
64
|
if (!signal.aborted &&
|
|
65
|
-
!
|
|
66
|
-
|
|
67
|
-
await
|
|
65
|
+
!core.isDisposed() &&
|
|
66
|
+
core.state === 'connected') {
|
|
67
|
+
await core
|
|
68
68
|
.requestReconnect('heartbeat_timeout')
|
|
69
69
|
.catch(() => void 0);
|
|
70
70
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"heartbeat.js","sourceRoot":"","sources":["../../src/plugins/heartbeat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAIhD,MAAM,0BAA0B,GAAG,KAAK,CAAA;AACxC,MAAM,yBAAyB,GAAG,IAAI,CAAA;AAEtC,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,MAAoB,EAAE,EAAE,CAAC;IAClD,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC;QACpC,IAAI,MAAM,EAAE,OAAO;YAAE,OAAO,OAAO,EAAE,CAAA;QACrC,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;QACrC,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,CAAC,gBAAgB,C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E,GAAG,EAAE,CAAC,aAAa,EAAE;YACnC,OAAO,EAAE,aAAa;SACvB,CAAA;IAAA,CACF,CAAA;AAAA,CACF,CAAA"}
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
export type { ClientPlugin, ClientPluginContext, ClientPluginEvent, ClientPluginInstance, ReconnectConfig, StreamEvent, } from './types.ts';
|
|
1
2
|
export * from './browser.ts';
|
|
2
3
|
export * from './heartbeat.ts';
|
|
3
4
|
export * from './logging.ts';
|
|
4
5
|
export * from './reconnect.ts';
|
|
5
|
-
export * from './types.ts';
|
package/dist/plugins/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/plugins/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/plugins/index.ts"],"names":[],"mappings":"AAQA,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA"}
|
|
@@ -1,98 +1,15 @@
|
|
|
1
|
-
import { ConnectionType } from '@nmtjs/protocol';
|
|
2
|
-
const DEFAULT_RECONNECT_TIMEOUT = 1000;
|
|
3
|
-
const DEFAULT_MAX_RECONNECT_TIMEOUT = 60000;
|
|
4
|
-
const sleep = (ms, signal) => {
|
|
5
|
-
return new Promise((resolve) => {
|
|
6
|
-
if (signal?.aborted)
|
|
7
|
-
return resolve();
|
|
8
|
-
const timer = setTimeout(resolve, ms);
|
|
9
|
-
if (signal) {
|
|
10
|
-
signal.addEventListener('abort', () => {
|
|
11
|
-
clearTimeout(timer);
|
|
12
|
-
resolve();
|
|
13
|
-
}, { once: true });
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
};
|
|
17
|
-
const computeReconnectDelay = (ms) => {
|
|
18
|
-
if (globalThis.window) {
|
|
19
|
-
const jitter = Math.floor(ms * 0.2 * Math.random());
|
|
20
|
-
return ms + jitter;
|
|
21
|
-
}
|
|
22
|
-
return ms;
|
|
23
|
-
};
|
|
24
|
-
const isReconnectPaused = () => {
|
|
25
|
-
if (globalThis.window && 'navigator' in globalThis.window) {
|
|
26
|
-
if (globalThis.window.navigator?.onLine === false)
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
if (globalThis.document) {
|
|
30
|
-
if (globalThis.document.visibilityState === 'hidden')
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
return false;
|
|
34
|
-
};
|
|
35
1
|
export const reconnectPlugin = (options = {}) => {
|
|
36
|
-
return (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
reconnectAbortController = null;
|
|
43
|
-
reconnecting = null;
|
|
44
|
-
};
|
|
45
|
-
const ensureReconnectLoop = () => {
|
|
46
|
-
if (reconnecting)
|
|
47
|
-
return;
|
|
48
|
-
reconnectAbortController = new AbortController();
|
|
49
|
-
const signal = reconnectAbortController.signal;
|
|
50
|
-
reconnecting = (async () => {
|
|
51
|
-
while (!signal.aborted &&
|
|
52
|
-
!client.isDisposed() &&
|
|
53
|
-
client.state === 'disconnected' &&
|
|
54
|
-
client.lastDisconnectReason !== 'client') {
|
|
55
|
-
if (isReconnectPaused()) {
|
|
56
|
-
await sleep(1000, signal);
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
const delay = computeReconnectDelay(reconnectTimeout);
|
|
60
|
-
await sleep(delay, signal);
|
|
61
|
-
if (signal.aborted ||
|
|
62
|
-
client.isDisposed() ||
|
|
63
|
-
client.state !== 'disconnected' ||
|
|
64
|
-
client.lastDisconnectReason === 'client') {
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
const previousTimeout = reconnectTimeout;
|
|
68
|
-
await client.connect().catch(() => void 0);
|
|
69
|
-
if (client.state === 'disconnected') {
|
|
70
|
-
reconnectTimeout = Math.min(previousTimeout * 2, options.maxTimeout ?? DEFAULT_MAX_RECONNECT_TIMEOUT);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
})().finally(() => {
|
|
74
|
-
reconnecting = null;
|
|
75
|
-
reconnectAbortController = null;
|
|
2
|
+
return ({ core }) => ({
|
|
3
|
+
name: 'reconnect',
|
|
4
|
+
onInit: () => {
|
|
5
|
+
core.configureReconnect({
|
|
6
|
+
initialTimeout: options.initialTimeout,
|
|
7
|
+
maxTimeout: options.maxTimeout,
|
|
76
8
|
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
cancelReconnect();
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
ensureReconnectLoop();
|
|
86
|
-
};
|
|
87
|
-
return {
|
|
88
|
-
name: 'reconnect',
|
|
89
|
-
onConnect: () => {
|
|
90
|
-
reconnectTimeout = options.initialTimeout ?? DEFAULT_RECONNECT_TIMEOUT;
|
|
91
|
-
cancelReconnect();
|
|
92
|
-
},
|
|
93
|
-
onDisconnect,
|
|
94
|
-
dispose: cancelReconnect,
|
|
95
|
-
};
|
|
96
|
-
};
|
|
9
|
+
},
|
|
10
|
+
dispose: () => {
|
|
11
|
+
core.configureReconnect(null);
|
|
12
|
+
},
|
|
13
|
+
});
|
|
97
14
|
};
|
|
98
15
|
//# sourceMappingURL=reconnect.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reconnect.js","sourceRoot":"","sources":["../../src/plugins/reconnect.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"reconnect.js","sourceRoot":"","sources":["../../src/plugins/reconnect.ts"],"names":[],"mappings":"AAOA,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,OAAO,GAA2B,EAAE,EACtB,EAAE,CAAC;IACjB,OAAO,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QACpB,IAAI,EAAE,WAAW;QACjB,MAAM,EAAE,GAAG,EAAE,CAAC;YACZ,IAAI,CAAC,kBAAkB,CAAC;gBACtB,cAAc,EAAE,OAAO,CAAC,cAAc;gBACtC,UAAU,EAAE,OAAO,CAAC,UAAU;aAC/B,CAAC,CAAA;QAAA,CACH;QACD,OAAO,EAAE,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAA;QAAA,CAC9B;KACF,CAAC,CAAA;AAAA,CACH,CAAA"}
|
package/dist/plugins/types.d.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ClientCore, ConnectionState } from '../core.ts';
|
|
2
|
+
import type { PingLayerApi } from '../layers/ping.ts';
|
|
2
3
|
export type ClientDisconnectReason = 'client' | 'server' | (string & {});
|
|
4
|
+
export interface ReconnectConfig {
|
|
5
|
+
initialTimeout?: number;
|
|
6
|
+
maxTimeout?: number;
|
|
7
|
+
}
|
|
8
|
+
export type StreamEvent = {
|
|
9
|
+
direction: 'incoming' | 'outgoing';
|
|
10
|
+
streamType: 'rpc' | 'client_blob' | 'server_blob';
|
|
11
|
+
action: 'response' | 'pull' | 'push' | 'end' | 'abort';
|
|
12
|
+
callId?: number;
|
|
13
|
+
streamId?: number;
|
|
14
|
+
byteLength?: number;
|
|
15
|
+
reason?: string;
|
|
16
|
+
};
|
|
3
17
|
export type ClientPluginEvent = {
|
|
18
|
+
kind: 'state_changed';
|
|
19
|
+
timestamp: number;
|
|
20
|
+
state: ConnectionState;
|
|
21
|
+
previous: ConnectionState;
|
|
22
|
+
} | {
|
|
4
23
|
kind: 'connected';
|
|
5
24
|
timestamp: number;
|
|
6
25
|
transportType: 'bidirectional' | 'unidirectional';
|
|
@@ -33,17 +52,10 @@ export type ClientPluginEvent = {
|
|
|
33
52
|
callId: number;
|
|
34
53
|
procedure: string;
|
|
35
54
|
error: unknown;
|
|
36
|
-
} | {
|
|
55
|
+
} | ({
|
|
37
56
|
kind: 'stream_event';
|
|
38
57
|
timestamp: number;
|
|
39
|
-
|
|
40
|
-
streamType: 'rpc' | 'client_blob' | 'server_blob';
|
|
41
|
-
action: 'response' | 'pull' | 'push' | 'end' | 'abort';
|
|
42
|
-
callId?: number;
|
|
43
|
-
streamId?: number;
|
|
44
|
-
byteLength?: number;
|
|
45
|
-
reason?: string;
|
|
46
|
-
};
|
|
58
|
+
} & StreamEvent);
|
|
47
59
|
/**
|
|
48
60
|
* Client plugin lifecycle contract.
|
|
49
61
|
*
|
|
@@ -60,4 +72,8 @@ export interface ClientPluginInstance {
|
|
|
60
72
|
onClientEvent?(event: ClientPluginEvent): void | Promise<void>;
|
|
61
73
|
dispose?(): void;
|
|
62
74
|
}
|
|
63
|
-
export
|
|
75
|
+
export interface ClientPluginContext {
|
|
76
|
+
core: ClientCore;
|
|
77
|
+
ping: PingLayerApi;
|
|
78
|
+
}
|
|
79
|
+
export type ClientPlugin = (context: ClientPluginContext) => ClientPluginInstance;
|
package/dist/transport.d.ts
CHANGED
|
@@ -1,53 +1,71 @@
|
|
|
1
1
|
import type { ConnectionType, ProtocolBlobMetadata, ProtocolVersion } from '@nmtjs/protocol';
|
|
2
2
|
import type { BaseClientFormat } from '@nmtjs/protocol/client';
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
_stream_response?: boolean;
|
|
6
|
-
};
|
|
7
|
-
export interface ClientTransportStartParams {
|
|
3
|
+
export type ClientDisconnectReason = 'client' | 'server' | (string & {});
|
|
4
|
+
export interface TransportConnectParams {
|
|
8
5
|
auth?: string;
|
|
9
6
|
application?: string;
|
|
10
|
-
onMessage: (message: ArrayBufferView) =>
|
|
11
|
-
onConnect: () =>
|
|
12
|
-
onDisconnect: (reason:
|
|
7
|
+
onMessage: (message: ArrayBufferView) => void;
|
|
8
|
+
onConnect: () => void;
|
|
9
|
+
onDisconnect: (reason: ClientDisconnectReason) => void;
|
|
13
10
|
}
|
|
14
|
-
export interface
|
|
15
|
-
|
|
11
|
+
export interface TransportSendOptions {
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
}
|
|
14
|
+
export interface TransportCallContext {
|
|
15
|
+
contentType: string;
|
|
16
16
|
auth?: string;
|
|
17
17
|
application?: string;
|
|
18
18
|
}
|
|
19
|
-
export
|
|
19
|
+
export interface TransportRpcParams {
|
|
20
|
+
callId: number;
|
|
21
|
+
procedure: string;
|
|
22
|
+
payload: ArrayBufferView;
|
|
23
|
+
blob?: {
|
|
24
|
+
source: ReadableStream;
|
|
25
|
+
metadata: ProtocolBlobMetadata;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export interface TransportCallOptions {
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
streamResponse?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface TransportRpcResponse {
|
|
20
33
|
type: 'rpc';
|
|
21
34
|
result: ArrayBufferView;
|
|
22
|
-
}
|
|
35
|
+
}
|
|
36
|
+
export interface TransportRpcStreamResponse {
|
|
23
37
|
type: 'rpc_stream';
|
|
24
38
|
stream: ReadableStream<ArrayBufferView>;
|
|
25
|
-
}
|
|
39
|
+
}
|
|
40
|
+
export interface TransportBlobResponse {
|
|
26
41
|
type: 'blob';
|
|
27
42
|
metadata: ProtocolBlobMetadata;
|
|
28
43
|
source: ReadableStream<ArrayBufferView>;
|
|
29
|
-
}
|
|
30
|
-
export
|
|
44
|
+
}
|
|
45
|
+
export interface TransportErrorResponse {
|
|
46
|
+
type: 'error';
|
|
47
|
+
error: ArrayBufferView;
|
|
48
|
+
status?: number;
|
|
49
|
+
statusText?: string;
|
|
50
|
+
}
|
|
51
|
+
export type TransportCallResponse = TransportRpcResponse | TransportRpcStreamResponse | TransportBlobResponse | TransportErrorResponse;
|
|
52
|
+
export interface BidirectionalTransport {
|
|
31
53
|
type: ConnectionType.Bidirectional;
|
|
32
|
-
connect(params:
|
|
54
|
+
connect(params: TransportConnectParams): Promise<void>;
|
|
33
55
|
disconnect(): Promise<void>;
|
|
34
|
-
send(message: ArrayBufferView, options:
|
|
35
|
-
}
|
|
56
|
+
send(message: ArrayBufferView, options: TransportSendOptions): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
export interface UnidirectionalTransport {
|
|
36
59
|
type: ConnectionType.Unidirectional;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
format: BaseClientFormat;
|
|
41
|
-
auth?: string;
|
|
42
|
-
application?: string;
|
|
43
|
-
}, rpc: {
|
|
44
|
-
callId: number;
|
|
45
|
-
procedure: string;
|
|
46
|
-
payload: any;
|
|
47
|
-
}, options: ClientTransportMessageOptions): Promise<ClientCallResponse>;
|
|
48
|
-
};
|
|
60
|
+
call(context: TransportCallContext, rpc: TransportRpcParams, options: TransportCallOptions): Promise<TransportCallResponse>;
|
|
61
|
+
}
|
|
62
|
+
export type ClientTransport = BidirectionalTransport | UnidirectionalTransport;
|
|
49
63
|
export interface ClientTransportParams {
|
|
50
64
|
protocol: ProtocolVersion;
|
|
51
65
|
format: BaseClientFormat;
|
|
52
66
|
}
|
|
53
|
-
export type ClientTransportFactory<
|
|
67
|
+
export type ClientTransportFactory<Transport extends ClientTransport = ClientTransport, Options = unknown> = (params: ClientTransportParams, options: Options) => Transport;
|
|
68
|
+
export type ClientTransportMessageOptions = TransportSendOptions;
|
|
69
|
+
export type ClientTransportStartParams = TransportConnectParams;
|
|
70
|
+
export type ClientTransportRpcParams = TransportCallContext;
|
|
71
|
+
export type ClientCallResponse = TransportCallResponse;
|