@telperion/messenger 8.3.0 → 8.4.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/README.md +35 -4
- package/package.json +2 -2
- package/rtc/channel-controller.cjs +7 -3
- package/rtc/channel-controller.d.ts +1 -1
- package/rtc/channel-controller.js +7 -3
- package/rtc/channel-controller.js.map +1 -1
- package/rtc/initializer.cjs +47 -0
- package/rtc/initializer.d.ts +16 -0
- package/rtc/initializer.js +46 -0
- package/rtc/initializer.js.map +1 -0
- package/rtc/message-client.cjs +47 -54
- package/rtc/message-client.d.ts +26 -11
- package/rtc/message-client.js +46 -53
- package/rtc/message-client.js.map +1 -1
- package/rtc/message-host.cjs +50 -55
- package/rtc/message-host.d.ts +26 -10
- package/rtc/message-host.js +49 -54
- package/rtc/message-host.js.map +1 -1
- package/rtc/message-service.cjs +16 -6
- package/rtc/message-service.d.ts +7 -5
- package/rtc/message-service.js +18 -8
- package/rtc/message-service.js.map +1 -1
package/rtc/message-client.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import { uniqueId } from '@telperion/js-utils/unique-id';
|
|
2
2
|
import { noop } from '@telperion/js-utils/function/noop';
|
|
3
|
-
import { promisify } from '@telperion/js-utils/promise/promisify';
|
|
4
|
-
import { timeout } from '@telperion/js-utils/promise/timeout';
|
|
5
3
|
import { NEVER } from '@telperion/js-utils/promise/never';
|
|
6
4
|
import { Subject } from 'rxjs';
|
|
7
5
|
import { MessageClient } from '../message-client';
|
|
8
6
|
import { GET_NEW_ID, RESPONSES$, SEND } from '../selectors';
|
|
9
7
|
import { DEFAULT_CHANNEL_NAME } from './default-channel-name';
|
|
10
|
-
import {
|
|
11
|
-
const CONNECTION = Symbol('RTCMessageClient Connection');
|
|
8
|
+
import { initialize } from './initializer';
|
|
12
9
|
const CHANNEL_NAME = Symbol('RTCMessageClient Channel Name');
|
|
10
|
+
const CHANNEL = Symbol('RTCMessageClient Channel');
|
|
11
|
+
const CHANNEL_OPEN = Symbol('RTCMessageClient Channel Open');
|
|
12
|
+
const DEINITIALIZE = Symbol('RTCMessageClient Deinitialize');
|
|
13
13
|
export const READY = Symbol('RTCMessageClient Ready');
|
|
14
|
+
export const CREATED = Symbol('RTCMessageClient Created');
|
|
14
15
|
/**
|
|
15
|
-
*
|
|
16
|
+
* WebRTC DataChannel message client that sends request messages and receives
|
|
17
|
+
* responses over a negotiated channel.
|
|
16
18
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
19
|
+
* Both peers must create the channel with an identical name and negotiated ID.
|
|
20
|
+
* The ID is derived automatically from the channel name via an FNV-1a hash
|
|
21
|
+
* unless an explicit `channelId` is supplied.
|
|
22
|
+
*
|
|
23
|
+
* Use `@Request` decorators to define outgoing RPC methods. The client can be
|
|
24
|
+
* constructed without a connection and initialized later via {@link initialize}.
|
|
20
25
|
*
|
|
21
26
|
* @example
|
|
22
27
|
* ```typescript
|
|
@@ -33,71 +38,59 @@ export const READY = Symbol('RTCMessageClient Ready');
|
|
|
33
38
|
*/
|
|
34
39
|
export class RTCMessageClient extends MessageClient {
|
|
35
40
|
[RESPONSES$] = new Subject();
|
|
36
|
-
[CONNECTION] = Promise.resolve([undefined, noop]);
|
|
37
41
|
[CHANNEL_NAME];
|
|
42
|
+
[CHANNEL] = NEVER;
|
|
43
|
+
[CHANNEL_OPEN] = NEVER;
|
|
44
|
+
[DEINITIALIZE] = Promise.resolve(noop);
|
|
45
|
+
get [CREATED]() {
|
|
46
|
+
return this[CHANNEL];
|
|
47
|
+
}
|
|
48
|
+
/** Resolves with the `RTCDataChannel` once it has been created (may not yet be open). */
|
|
49
|
+
get created() {
|
|
50
|
+
return this[CREATED];
|
|
51
|
+
}
|
|
38
52
|
get [READY]() {
|
|
39
|
-
return this[
|
|
53
|
+
return this[CHANNEL_OPEN];
|
|
40
54
|
}
|
|
55
|
+
/** Resolves with the `RTCDataChannel` once it is open and ready to transmit. */
|
|
41
56
|
get ready() {
|
|
42
57
|
return this[READY];
|
|
43
58
|
}
|
|
44
59
|
/**
|
|
45
|
-
* @param connection
|
|
60
|
+
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
46
61
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
62
|
+
* @param channelId - Explicit negotiated channel ID. Overrides the hash-derived default.
|
|
47
63
|
*/
|
|
48
|
-
constructor(connection, channelName = DEFAULT_CHANNEL_NAME) {
|
|
64
|
+
constructor(connection, channelName = DEFAULT_CHANNEL_NAME, channelId) {
|
|
49
65
|
super();
|
|
50
66
|
this[CHANNEL_NAME] = channelName;
|
|
51
|
-
RTCMessageClient.prototype.initialize.call(this, connection);
|
|
67
|
+
RTCMessageClient.prototype.initialize.call(this, connection, channelId);
|
|
52
68
|
}
|
|
53
69
|
/**
|
|
54
70
|
* (Re)initializes the data channel with a new connection.
|
|
55
|
-
*
|
|
71
|
+
* Waits for the previous channel to be cleaned up (with a 5 s timeout)
|
|
72
|
+
* before establishing the new one.
|
|
56
73
|
*
|
|
57
74
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
58
|
-
* @param
|
|
75
|
+
* @param channelId - Optional negotiated channel ID. Defaults to the hash-derived ID.
|
|
59
76
|
*/
|
|
60
|
-
initialize(connection) {
|
|
61
|
-
|
|
62
|
-
this
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return;
|
|
74
|
-
const dataChannel = CHANNEL_CONTROLLER.getChannel(conn, this[CHANNEL_NAME]);
|
|
75
|
-
if (dataChannel.readyState === 'open')
|
|
76
|
-
return dataChannel;
|
|
77
|
-
return new Promise((resolve) => {
|
|
78
|
-
dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });
|
|
79
|
-
});
|
|
80
|
-
})
|
|
81
|
-
.then(dataChannel => {
|
|
82
|
-
if (!dataChannel)
|
|
83
|
-
return [undefined, noop];
|
|
84
|
-
const listener = (event) => {
|
|
85
|
-
const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
86
|
-
if (data.type !== 'response')
|
|
87
|
-
return;
|
|
88
|
-
this[RESPONSES$].next(data);
|
|
89
|
-
};
|
|
90
|
-
dataChannel.addEventListener('message', listener);
|
|
91
|
-
return [dataChannel, () => {
|
|
92
|
-
dataChannel.removeEventListener('message', listener);
|
|
93
|
-
CHANNEL_CONTROLLER.close(dataChannel);
|
|
94
|
-
}];
|
|
77
|
+
initialize(connection, channelId) {
|
|
78
|
+
initialize({
|
|
79
|
+
context: this,
|
|
80
|
+
selectors: {
|
|
81
|
+
channel: CHANNEL,
|
|
82
|
+
channelOpen: CHANNEL_OPEN,
|
|
83
|
+
deinit: DEINITIALIZE,
|
|
84
|
+
emitter: RESPONSES$
|
|
85
|
+
},
|
|
86
|
+
connection,
|
|
87
|
+
channelName: this[CHANNEL_NAME],
|
|
88
|
+
channelId,
|
|
89
|
+
messageFilterType: 'response'
|
|
95
90
|
});
|
|
96
91
|
}
|
|
97
92
|
async [SEND](message) {
|
|
98
|
-
const
|
|
99
|
-
if (dataChannel?.readyState !== 'open')
|
|
100
|
-
return;
|
|
93
|
+
const dataChannel = await this[CHANNEL_OPEN];
|
|
101
94
|
dataChannel.send(JSON.stringify({ ...message, type: 'request' }));
|
|
102
95
|
}
|
|
103
96
|
[GET_NEW_ID]() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/message-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACzD,OAAO,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AACzD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/message-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QA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O,IAAI,CAAC,OAAO,CAAC,CAAC;IACvB,CAAC;IAED,IAAY,CAAC,KAAK,CAAC;QACjB,OAAO,IAAI,CAAC,YAAY,CAAC,CAAC;IAC5B,CAAC;IAED,gFAAgF;IAChF,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,YACE,UAA6B,EAC7B,WAAW,GAAG,oBAAoB,EAClC,SAAkB;QAElB,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,YAAY,CAAC,GAAG,WAAW,CAAC;QACjC,gBAAgB,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,EAAE,SAAS,CAAC,CAAC;IAC1E,CAAC;IAED;;;;;;;OAOG;IACH,UAAU,CAAC,UAA6B,EAAE,SAAkB;QAC1D,UAAU,CAAC;YACT,OAAO,EAAE,IAAI;YACb,SAAS,EAAE;gBACT,OAAO,EAAE,OAAO;gBAChB,WAAW,EAAE,YAAY;gBACzB,MAAM,EAAE,YAAY;gBACpB,OAAO,EAAE,UAAU;aACpB;YACD,UAAU;YACV,WAAW,EAAE,IAAI,CAAC,YAAY,CAAC;YAC/B,SAAS;YACT,iBAAiB,EAAE,UAAU;SAC9B,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,CAAC,IAAI,CAAC,CAAI,OAAmB;QACxC,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,CAAC;QAE7C,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAC,GAAG,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;IACnE,CAAC;IAES,CAAC,UAAU,CAAC;QACpB,OAAO,QAAQ,CAAC,uBAAuB,CAAC,CAAC;IAC3C,CAAC;CACF","file":"message-client.js","sourcesContent":["import { uniqueId } from '@telperion/js-utils/unique-id';\r\nimport { noop } from '@telperion/js-utils/function/noop';\r\nimport { NEVER } from '@telperion/js-utils/promise/never';\r\nimport { Subject } from 'rxjs';\r\n\r\nimport { MessageClient } from '../message-client';\r\nimport { MessageResponse } from '../message-response.type';\r\nimport { Message } from '../message.interface';\r\nimport { GET_NEW_ID, RESPONSES$, SEND } from '../selectors';\r\nimport { RTCConnectionArg } from './rtc.type';\r\nimport { DEFAULT_CHANNEL_NAME } from './default-channel-name';\r\nimport { initialize } from './initializer';\r\n\r\nconst CHANNEL_NAME = Symbol('RTCMessageClient Channel Name');\r\nconst CHANNEL = Symbol('RTCMessageClient Channel');\r\nconst CHANNEL_OPEN = Symbol('RTCMessageClient Channel Open');\r\nconst DEINITIALIZE = Symbol('RTCMessageClient Deinitialize');\r\nexport const READY = Symbol('RTCMessageClient Ready');\r\nexport const CREATED = Symbol('RTCMessageClient Created');\r\n\r\n/**\r\n * WebRTC DataChannel message client that sends request messages and receives\r\n * responses over a negotiated channel.\r\n *\r\n * Both peers must create the channel with an identical name and negotiated ID.\r\n * The ID is derived automatically from the channel name via an FNV-1a hash\r\n * unless an explicit `channelId` is supplied.\r\n *\r\n * Use `@Request` decorators to define outgoing RPC methods. The client can be\r\n * constructed without a connection and initialized later via {@link initialize}.\r\n *\r\n * @example\r\n * ```typescript\r\n * class GameClient extends RTCMessageClient {\r\n * @Request('move')\r\n * sendMove(position: [number, number]): Observable<boolean> {\r\n * return null!;\r\n * }\r\n * }\r\n *\r\n * const client = new GameClient(peerConnection, 'game-events');\r\n * client.sendMove([3, 4]).subscribe(ok => console.log('Accepted:', ok));\r\n * ```\r\n */\r\nexport class RTCMessageClient extends MessageClient {\r\n public [RESPONSES$] = new Subject<MessageResponse>();\r\n private [CHANNEL_NAME]: string;\r\n private [CHANNEL]: Promise<RTCDataChannel> = NEVER;\r\n private [CHANNEL_OPEN]: Promise<RTCDataChannel> = NEVER;\r\n private [DEINITIALIZE]: Promise<() => void> = Promise.resolve(noop);\r\n\r\n private get [CREATED]() {\r\n return this[CHANNEL];\r\n }\r\n\r\n /** Resolves with the `RTCDataChannel` once it has been created (may not yet be open). */\r\n get created(): Promise<RTCDataChannel> {\r\n return this[CREATED];\r\n }\r\n\r\n private get [READY](): Promise<RTCDataChannel> {\r\n return this[CHANNEL_OPEN];\r\n }\r\n\r\n /** Resolves with the `RTCDataChannel` once it is open and ready to transmit. */\r\n get ready(): Promise<RTCDataChannel> {\r\n return this[READY];\r\n }\r\n\r\n /**\r\n * @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.\r\n * @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.\r\n * @param channelId - Explicit negotiated channel ID. Overrides the hash-derived default.\r\n */\r\n constructor(\r\n connection?: RTCConnectionArg,\r\n channelName = DEFAULT_CHANNEL_NAME,\r\n channelId?: number\r\n ) {\r\n super();\r\n\r\n this[CHANNEL_NAME] = channelName;\r\n RTCMessageClient.prototype.initialize.call(this, connection, channelId);\r\n }\r\n\r\n /**\r\n * (Re)initializes the data channel with a new connection.\r\n * Waits for the previous channel to be cleaned up (with a 5 s timeout)\r\n * before establishing the new one.\r\n *\r\n * @param connection - RTCPeerConnection instance, promise, or factory function.\r\n * @param channelId - Optional negotiated channel ID. Defaults to the hash-derived ID.\r\n */\r\n initialize(connection?: RTCConnectionArg, channelId?: number): void {\r\n initialize({\r\n context: this,\r\n selectors: {\r\n channel: CHANNEL,\r\n channelOpen: CHANNEL_OPEN,\r\n deinit: DEINITIALIZE,\r\n emitter: RESPONSES$\r\n },\r\n connection,\r\n channelName: this[CHANNEL_NAME],\r\n channelId,\r\n messageFilterType: 'response'\r\n });\r\n }\r\n\r\n public async [SEND]<T>(message: Message<T>) {\r\n const dataChannel = await this[CHANNEL_OPEN];\r\n\r\n dataChannel.send(JSON.stringify({...message, type: 'request' }));\r\n }\r\n\r\n protected [GET_NEW_ID](): string {\r\n return uniqueId('messenger-rtc-message');\r\n }\r\n}\r\n"]}
|
package/rtc/message-host.cjs
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RTCMessageHost = exports.READY = void 0;
|
|
3
|
+
exports.RTCMessageHost = exports.CREATED = exports.READY = void 0;
|
|
4
4
|
const noop_1 = require("@telperion/js-utils/function/noop");
|
|
5
|
-
const
|
|
6
|
-
const timeout_1 = require("@telperion/js-utils/promise/timeout");
|
|
5
|
+
const call_1 = require("@telperion/js-utils/function/call");
|
|
7
6
|
const never_1 = require("@telperion/js-utils/promise/never");
|
|
8
7
|
const rxjs_1 = require("rxjs");
|
|
9
8
|
const message_host_1 = require("../message-host");
|
|
10
9
|
const selectors_1 = require("../selectors");
|
|
11
10
|
const default_channel_name_1 = require("./default-channel-name");
|
|
12
|
-
const
|
|
13
|
-
const CONNECTION = Symbol('RTCMessageHost Connection');
|
|
14
|
-
const REQUESTS = Symbol('RTCMessageHost Requests');
|
|
11
|
+
const initializer_1 = require("./initializer");
|
|
15
12
|
const CHANNEL_NAME = Symbol('RTCMessageHost Channel Name');
|
|
13
|
+
const CHANNEL = Symbol('RTCMessageHost Channel');
|
|
14
|
+
const CHANNEL_OPEN = Symbol('RTCMessageHost Channel Open');
|
|
15
|
+
const DEINITIALIZE = Symbol('RTCMessageHost Deinitialize');
|
|
16
|
+
const REQUESTS = Symbol('RTCMessageHost Requests');
|
|
16
17
|
exports.READY = Symbol('RTCMessageHost Ready');
|
|
18
|
+
exports.CREATED = Symbol('RTCMessageHost Created');
|
|
17
19
|
/**
|
|
18
|
-
*
|
|
20
|
+
* WebRTC DataChannel message host that listens for incoming request messages
|
|
21
|
+
* and dispatches responses over the same negotiated channel.
|
|
22
|
+
*
|
|
23
|
+
* Both peers must create the channel with an identical name and negotiated ID.
|
|
24
|
+
* The ID is derived automatically from the channel name via an FNV-1a hash
|
|
25
|
+
* unless an explicit `channelId` is supplied.
|
|
19
26
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
27
|
+
* Use `@Listen` decorators to register request handlers. The host begins
|
|
28
|
+
* listening as soon as it is constructed; pass a connection later via
|
|
29
|
+
* {@link initialize} if one is not available at construction time.
|
|
23
30
|
*
|
|
24
31
|
* @example
|
|
25
32
|
* ```typescript
|
|
@@ -35,22 +42,33 @@ exports.READY = Symbol('RTCMessageHost Ready');
|
|
|
35
42
|
*/
|
|
36
43
|
class RTCMessageHost extends message_host_1.MessageHost {
|
|
37
44
|
[REQUESTS] = new rxjs_1.Subject();
|
|
38
|
-
[CONNECTION] = Promise.resolve([undefined, noop_1.noop]);
|
|
39
45
|
[CHANNEL_NAME];
|
|
46
|
+
[CHANNEL] = never_1.NEVER;
|
|
47
|
+
[CHANNEL_OPEN] = never_1.NEVER;
|
|
48
|
+
[DEINITIALIZE] = Promise.resolve(noop_1.noop);
|
|
49
|
+
get [exports.CREATED]() {
|
|
50
|
+
return this[CHANNEL];
|
|
51
|
+
}
|
|
52
|
+
/** Resolves with the `RTCDataChannel` once it has been created (may not yet be open). */
|
|
53
|
+
get created() {
|
|
54
|
+
return this[exports.CREATED];
|
|
55
|
+
}
|
|
40
56
|
get [exports.READY]() {
|
|
41
|
-
return this[
|
|
57
|
+
return this[CHANNEL_OPEN];
|
|
42
58
|
}
|
|
59
|
+
/** Resolves with the `RTCDataChannel` once it is open and ready to transmit. */
|
|
43
60
|
get ready() {
|
|
44
61
|
return this[exports.READY];
|
|
45
62
|
}
|
|
46
63
|
/**
|
|
47
|
-
* @param connection
|
|
64
|
+
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
48
65
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
66
|
+
* @param channelId - Explicit negotiated channel ID. Overrides the hash-derived default.
|
|
49
67
|
*/
|
|
50
|
-
constructor(connection, channelName = default_channel_name_1.DEFAULT_CHANNEL_NAME) {
|
|
68
|
+
constructor(connection, channelName = default_channel_name_1.DEFAULT_CHANNEL_NAME, channelId) {
|
|
51
69
|
super();
|
|
52
70
|
this[CHANNEL_NAME] = channelName;
|
|
53
|
-
RTCMessageHost.prototype.initialize.call(this, connection);
|
|
71
|
+
RTCMessageHost.prototype.initialize.call(this, connection, channelId);
|
|
54
72
|
this[selectors_1.LISTEN](this[REQUESTS]);
|
|
55
73
|
}
|
|
56
74
|
/**
|
|
@@ -58,53 +76,30 @@ class RTCMessageHost extends message_host_1.MessageHost {
|
|
|
58
76
|
* Cleans up the previous channel before establishing a new one.
|
|
59
77
|
*
|
|
60
78
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
79
|
+
* @param channelId - Optional new channel ID. Defaults to the current channel ID.
|
|
61
80
|
*/
|
|
62
|
-
initialize(connection) {
|
|
63
|
-
|
|
64
|
-
this
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return;
|
|
76
|
-
const dataChannel = channel_controller_1.CHANNEL_CONTROLLER.getChannel(conn, this[CHANNEL_NAME]);
|
|
77
|
-
if (dataChannel.readyState === 'open')
|
|
78
|
-
return dataChannel;
|
|
79
|
-
return new Promise((resolve) => {
|
|
80
|
-
dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });
|
|
81
|
-
});
|
|
82
|
-
})
|
|
83
|
-
.then(dataChannel => {
|
|
84
|
-
if (!dataChannel)
|
|
85
|
-
return [undefined, noop_1.noop];
|
|
86
|
-
const handler = (event) => {
|
|
87
|
-
const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
88
|
-
if (data.type !== 'request')
|
|
89
|
-
return;
|
|
90
|
-
this[REQUESTS].next(data);
|
|
91
|
-
};
|
|
92
|
-
dataChannel.addEventListener('message', handler);
|
|
93
|
-
return [dataChannel, () => {
|
|
94
|
-
dataChannel.removeEventListener('message', handler);
|
|
95
|
-
channel_controller_1.CHANNEL_CONTROLLER.close(dataChannel);
|
|
96
|
-
}];
|
|
81
|
+
initialize(connection, channelId) {
|
|
82
|
+
(0, initializer_1.initialize)({
|
|
83
|
+
context: this,
|
|
84
|
+
selectors: {
|
|
85
|
+
channel: CHANNEL,
|
|
86
|
+
channelOpen: CHANNEL_OPEN,
|
|
87
|
+
deinit: DEINITIALIZE,
|
|
88
|
+
emitter: REQUESTS
|
|
89
|
+
},
|
|
90
|
+
connection,
|
|
91
|
+
channelName: this[CHANNEL_NAME],
|
|
92
|
+
channelId,
|
|
93
|
+
messageFilterType: 'request'
|
|
97
94
|
});
|
|
98
95
|
}
|
|
99
96
|
/** Closes the data channel and removes event listeners. */
|
|
100
97
|
terminate() {
|
|
101
|
-
this[
|
|
102
|
-
.then(([_, deinitialize]) => deinitialize())
|
|
103
|
-
.catch(noop_1.noop);
|
|
98
|
+
this[DEINITIALIZE].then(call_1.call);
|
|
104
99
|
}
|
|
105
100
|
async [selectors_1.RESPONSE](message) {
|
|
106
|
-
const
|
|
107
|
-
dataChannel
|
|
101
|
+
const dataChannel = await this[CHANNEL_OPEN];
|
|
102
|
+
dataChannel.send(JSON.stringify({ ...message, type: 'response' }));
|
|
108
103
|
}
|
|
109
104
|
}
|
|
110
105
|
exports.RTCMessageHost = RTCMessageHost;
|
package/rtc/message-host.d.ts
CHANGED
|
@@ -2,16 +2,24 @@ import { MessageHost } from '../message-host';
|
|
|
2
2
|
import { MessageResponse } from '../message-response.type';
|
|
3
3
|
import { RESPONSE } from '../selectors';
|
|
4
4
|
import { RTCConnectionArg } from './rtc.type';
|
|
5
|
-
declare const CONNECTION: unique symbol;
|
|
6
|
-
declare const REQUESTS: unique symbol;
|
|
7
5
|
declare const CHANNEL_NAME: unique symbol;
|
|
6
|
+
declare const CHANNEL: unique symbol;
|
|
7
|
+
declare const CHANNEL_OPEN: unique symbol;
|
|
8
|
+
declare const DEINITIALIZE: unique symbol;
|
|
9
|
+
declare const REQUESTS: unique symbol;
|
|
8
10
|
export declare const READY: unique symbol;
|
|
11
|
+
export declare const CREATED: unique symbol;
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* WebRTC DataChannel message host that listens for incoming request messages
|
|
14
|
+
* and dispatches responses over the same negotiated channel.
|
|
15
|
+
*
|
|
16
|
+
* Both peers must create the channel with an identical name and negotiated ID.
|
|
17
|
+
* The ID is derived automatically from the channel name via an FNV-1a hash
|
|
18
|
+
* unless an explicit `channelId` is supplied.
|
|
11
19
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
20
|
+
* Use `@Listen` decorators to register request handlers. The host begins
|
|
21
|
+
* listening as soon as it is constructed; pass a connection later via
|
|
22
|
+
* {@link initialize} if one is not available at construction time.
|
|
15
23
|
*
|
|
16
24
|
* @example
|
|
17
25
|
* ```typescript
|
|
@@ -27,22 +35,30 @@ export declare const READY: unique symbol;
|
|
|
27
35
|
*/
|
|
28
36
|
export declare class RTCMessageHost extends MessageHost {
|
|
29
37
|
private [REQUESTS];
|
|
30
|
-
private [CONNECTION];
|
|
31
38
|
private [CHANNEL_NAME];
|
|
39
|
+
private [CHANNEL];
|
|
40
|
+
private [CHANNEL_OPEN];
|
|
41
|
+
private [DEINITIALIZE];
|
|
42
|
+
private get [CREATED]();
|
|
43
|
+
/** Resolves with the `RTCDataChannel` once it has been created (may not yet be open). */
|
|
44
|
+
get created(): Promise<RTCDataChannel>;
|
|
32
45
|
private get [READY]();
|
|
46
|
+
/** Resolves with the `RTCDataChannel` once it is open and ready to transmit. */
|
|
33
47
|
get ready(): Promise<RTCDataChannel>;
|
|
34
48
|
/**
|
|
35
|
-
* @param connection
|
|
49
|
+
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
36
50
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
51
|
+
* @param channelId - Explicit negotiated channel ID. Overrides the hash-derived default.
|
|
37
52
|
*/
|
|
38
|
-
constructor(connection?: RTCConnectionArg, channelName?: string);
|
|
53
|
+
constructor(connection?: RTCConnectionArg, channelName?: string, channelId?: number);
|
|
39
54
|
/**
|
|
40
55
|
* (Re)initializes the data channel with a new connection.
|
|
41
56
|
* Cleans up the previous channel before establishing a new one.
|
|
42
57
|
*
|
|
43
58
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
59
|
+
* @param channelId - Optional new channel ID. Defaults to the current channel ID.
|
|
44
60
|
*/
|
|
45
|
-
initialize(connection
|
|
61
|
+
initialize(connection?: RTCConnectionArg, channelId?: number): void;
|
|
46
62
|
/** Closes the data channel and removes event listeners. */
|
|
47
63
|
terminate(): void;
|
|
48
64
|
protected [RESPONSE](message: MessageResponse): Promise<void>;
|
package/rtc/message-host.js
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
1
|
import { noop } from '@telperion/js-utils/function/noop';
|
|
2
|
-
import {
|
|
3
|
-
import { timeout } from '@telperion/js-utils/promise/timeout';
|
|
2
|
+
import { call } from '@telperion/js-utils/function/call';
|
|
4
3
|
import { NEVER } from '@telperion/js-utils/promise/never';
|
|
5
4
|
import { Subject } from 'rxjs';
|
|
6
5
|
import { MessageHost } from '../message-host';
|
|
7
6
|
import { LISTEN, RESPONSE } from '../selectors';
|
|
8
7
|
import { DEFAULT_CHANNEL_NAME } from './default-channel-name';
|
|
9
|
-
import {
|
|
10
|
-
const CONNECTION = Symbol('RTCMessageHost Connection');
|
|
11
|
-
const REQUESTS = Symbol('RTCMessageHost Requests');
|
|
8
|
+
import { initialize } from './initializer';
|
|
12
9
|
const CHANNEL_NAME = Symbol('RTCMessageHost Channel Name');
|
|
10
|
+
const CHANNEL = Symbol('RTCMessageHost Channel');
|
|
11
|
+
const CHANNEL_OPEN = Symbol('RTCMessageHost Channel Open');
|
|
12
|
+
const DEINITIALIZE = Symbol('RTCMessageHost Deinitialize');
|
|
13
|
+
const REQUESTS = Symbol('RTCMessageHost Requests');
|
|
13
14
|
export const READY = Symbol('RTCMessageHost Ready');
|
|
15
|
+
export const CREATED = Symbol('RTCMessageHost Created');
|
|
14
16
|
/**
|
|
15
|
-
*
|
|
17
|
+
* WebRTC DataChannel message host that listens for incoming request messages
|
|
18
|
+
* and dispatches responses over the same negotiated channel.
|
|
19
|
+
*
|
|
20
|
+
* Both peers must create the channel with an identical name and negotiated ID.
|
|
21
|
+
* The ID is derived automatically from the channel name via an FNV-1a hash
|
|
22
|
+
* unless an explicit `channelId` is supplied.
|
|
16
23
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
24
|
+
* Use `@Listen` decorators to register request handlers. The host begins
|
|
25
|
+
* listening as soon as it is constructed; pass a connection later via
|
|
26
|
+
* {@link initialize} if one is not available at construction time.
|
|
20
27
|
*
|
|
21
28
|
* @example
|
|
22
29
|
* ```typescript
|
|
@@ -32,22 +39,33 @@ export const READY = Symbol('RTCMessageHost Ready');
|
|
|
32
39
|
*/
|
|
33
40
|
export class RTCMessageHost extends MessageHost {
|
|
34
41
|
[REQUESTS] = new Subject();
|
|
35
|
-
[CONNECTION] = Promise.resolve([undefined, noop]);
|
|
36
42
|
[CHANNEL_NAME];
|
|
43
|
+
[CHANNEL] = NEVER;
|
|
44
|
+
[CHANNEL_OPEN] = NEVER;
|
|
45
|
+
[DEINITIALIZE] = Promise.resolve(noop);
|
|
46
|
+
get [CREATED]() {
|
|
47
|
+
return this[CHANNEL];
|
|
48
|
+
}
|
|
49
|
+
/** Resolves with the `RTCDataChannel` once it has been created (may not yet be open). */
|
|
50
|
+
get created() {
|
|
51
|
+
return this[CREATED];
|
|
52
|
+
}
|
|
37
53
|
get [READY]() {
|
|
38
|
-
return this[
|
|
54
|
+
return this[CHANNEL_OPEN];
|
|
39
55
|
}
|
|
56
|
+
/** Resolves with the `RTCDataChannel` once it is open and ready to transmit. */
|
|
40
57
|
get ready() {
|
|
41
58
|
return this[READY];
|
|
42
59
|
}
|
|
43
60
|
/**
|
|
44
|
-
* @param connection
|
|
61
|
+
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
45
62
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
63
|
+
* @param channelId - Explicit negotiated channel ID. Overrides the hash-derived default.
|
|
46
64
|
*/
|
|
47
|
-
constructor(connection, channelName = DEFAULT_CHANNEL_NAME) {
|
|
65
|
+
constructor(connection, channelName = DEFAULT_CHANNEL_NAME, channelId) {
|
|
48
66
|
super();
|
|
49
67
|
this[CHANNEL_NAME] = channelName;
|
|
50
|
-
RTCMessageHost.prototype.initialize.call(this, connection);
|
|
68
|
+
RTCMessageHost.prototype.initialize.call(this, connection, channelId);
|
|
51
69
|
this[LISTEN](this[REQUESTS]);
|
|
52
70
|
}
|
|
53
71
|
/**
|
|
@@ -55,53 +73,30 @@ export class RTCMessageHost extends MessageHost {
|
|
|
55
73
|
* Cleans up the previous channel before establishing a new one.
|
|
56
74
|
*
|
|
57
75
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
76
|
+
* @param channelId - Optional new channel ID. Defaults to the current channel ID.
|
|
58
77
|
*/
|
|
59
|
-
initialize(connection) {
|
|
60
|
-
|
|
61
|
-
this
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return;
|
|
73
|
-
const dataChannel = CHANNEL_CONTROLLER.getChannel(conn, this[CHANNEL_NAME]);
|
|
74
|
-
if (dataChannel.readyState === 'open')
|
|
75
|
-
return dataChannel;
|
|
76
|
-
return new Promise((resolve) => {
|
|
77
|
-
dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });
|
|
78
|
-
});
|
|
79
|
-
})
|
|
80
|
-
.then(dataChannel => {
|
|
81
|
-
if (!dataChannel)
|
|
82
|
-
return [undefined, noop];
|
|
83
|
-
const handler = (event) => {
|
|
84
|
-
const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
85
|
-
if (data.type !== 'request')
|
|
86
|
-
return;
|
|
87
|
-
this[REQUESTS].next(data);
|
|
88
|
-
};
|
|
89
|
-
dataChannel.addEventListener('message', handler);
|
|
90
|
-
return [dataChannel, () => {
|
|
91
|
-
dataChannel.removeEventListener('message', handler);
|
|
92
|
-
CHANNEL_CONTROLLER.close(dataChannel);
|
|
93
|
-
}];
|
|
78
|
+
initialize(connection, channelId) {
|
|
79
|
+
initialize({
|
|
80
|
+
context: this,
|
|
81
|
+
selectors: {
|
|
82
|
+
channel: CHANNEL,
|
|
83
|
+
channelOpen: CHANNEL_OPEN,
|
|
84
|
+
deinit: DEINITIALIZE,
|
|
85
|
+
emitter: REQUESTS
|
|
86
|
+
},
|
|
87
|
+
connection,
|
|
88
|
+
channelName: this[CHANNEL_NAME],
|
|
89
|
+
channelId,
|
|
90
|
+
messageFilterType: 'request'
|
|
94
91
|
});
|
|
95
92
|
}
|
|
96
93
|
/** Closes the data channel and removes event listeners. */
|
|
97
94
|
terminate() {
|
|
98
|
-
this[
|
|
99
|
-
.then(([_, deinitialize]) => deinitialize())
|
|
100
|
-
.catch(noop);
|
|
95
|
+
this[DEINITIALIZE].then(call);
|
|
101
96
|
}
|
|
102
97
|
async [RESPONSE](message) {
|
|
103
|
-
const
|
|
104
|
-
dataChannel
|
|
98
|
+
const dataChannel = await this[CHANNEL_OPEN];
|
|
99
|
+
dataChannel.send(JSON.stringify({ ...message, type: 'response' }));
|
|
105
100
|
}
|
|
106
101
|
}
|
|
107
102
|
|
package/rtc/message-host.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/message-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AACzD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/message-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AACzD,OAAO,EAAE,IA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cesContent":["import { noop } from '@telperion/js-utils/function/noop';\r\nimport { call } from '@telperion/js-utils/function/call';\r\nimport { NEVER } from '@telperion/js-utils/promise/never';\r\nimport { Subject } from 'rxjs';\r\n\r\nimport { MessageHost } from '../message-host';\r\nimport { MessageResponse } from '../message-response.type';\r\nimport { Message } from '../message.interface';\r\nimport { LISTEN, RESPONSE } from '../selectors';\r\nimport { RTCConnectionArg } from './rtc.type';\r\nimport { DEFAULT_CHANNEL_NAME } from './default-channel-name';\r\nimport { initialize } from './initializer';\r\n\r\nconst CHANNEL_NAME = Symbol('RTCMessageHost Channel Name');\r\nconst CHANNEL = Symbol('RTCMessageHost Channel');\r\nconst CHANNEL_OPEN = Symbol('RTCMessageHost Channel Open');\r\nconst DEINITIALIZE = Symbol('RTCMessageHost Deinitialize');\r\nconst REQUESTS = Symbol('RTCMessageHost Requests');\r\nexport const READY = Symbol('RTCMessageHost Ready');\r\nexport const CREATED = Symbol('RTCMessageHost Created');\r\n\r\n/**\r\n * WebRTC DataChannel message host that listens for incoming request messages\r\n * and dispatches responses over the same negotiated channel.\r\n *\r\n * Both peers must create the channel with an identical name and negotiated ID.\r\n * The ID is derived automatically from the channel name via an FNV-1a hash\r\n * unless an explicit `channelId` is supplied.\r\n *\r\n * Use `@Listen` decorators to register request handlers. The host begins\r\n * listening as soon as it is constructed; pass a connection later via\r\n * {@link initialize} if one is not available at construction time.\r\n *\r\n * @example\r\n * ```typescript\r\n * class GameHost extends RTCMessageHost {\r\n * @Listen('move')\r\n * handleMove({ data }: { data: [number, number] }): Observable<boolean> {\r\n * return of(applyMove(data));\r\n * }\r\n * }\r\n *\r\n * const host = new GameHost(peerConnection, 'game-events');\r\n * ```\r\n */\r\nexport class RTCMessageHost extends MessageHost {\r\n private [REQUESTS] = new Subject<Message>();\r\n private [CHANNEL_NAME]: string;\r\n private [CHANNEL]: Promise<RTCDataChannel> = NEVER;\r\n private [CHANNEL_OPEN]: Promise<RTCDataChannel> = NEVER;\r\n private [DEINITIALIZE]: Promise<() => void> = Promise.resolve(noop);\r\n\r\n private get [CREATED]() {\r\n return this[CHANNEL];\r\n }\r\n\r\n /** Resolves with the `RTCDataChannel` once it has been created (may not yet be open). */\r\n get created(): Promise<RTCDataChannel> {\r\n return this[CREATED];\r\n }\r\n\r\n private get [READY](): Promise<RTCDataChannel> {\r\n return this[CHANNEL_OPEN];\r\n }\r\n\r\n /** Resolves with the `RTCDataChannel` once it is open and ready to transmit. */\r\n get ready(): Promise<RTCDataChannel> {\r\n return this[READY];\r\n }\r\n\r\n /**\r\n * @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.\r\n * @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.\r\n * @param channelId - Explicit negotiated channel ID. Overrides the hash-derived default.\r\n */\r\n constructor(\r\n connection?: RTCConnectionArg,\r\n channelName = DEFAULT_CHANNEL_NAME,\r\n channelId?: number\r\n ) {\r\n super();\r\n\r\n this[CHANNEL_NAME] = channelName;\r\n RTCMessageHost.prototype.initialize.call(this, connection, channelId);\r\n this[LISTEN](this[REQUESTS]);\r\n }\r\n\r\n /**\r\n * (Re)initializes the data channel with a new connection.\r\n * Cleans up the previous channel before establishing a new one.\r\n *\r\n * @param connection - RTCPeerConnection instance, promise, or factory function.\r\n * @param channelId - Optional new channel ID. Defaults to the current channel ID.\r\n */\r\n initialize(connection?: RTCConnectionArg, channelId?: number): void {\r\n initialize({\r\n context: this,\r\n selectors: {\r\n channel: CHANNEL,\r\n channelOpen: CHANNEL_OPEN,\r\n deinit: DEINITIALIZE,\r\n emitter: REQUESTS\r\n },\r\n connection,\r\n channelName: this[CHANNEL_NAME],\r\n channelId,\r\n messageFilterType: 'request'\r\n });\r\n }\r\n\r\n /** Closes the data channel and removes event listeners. */\r\n public terminate(): void {\r\n this[DEINITIALIZE].then(call);\r\n }\r\n\r\n protected async [RESPONSE](message: MessageResponse) {\r\n const dataChannel = await this[CHANNEL_OPEN];\r\n\r\n dataChannel.send(JSON.stringify({...message, type: 'response' }));\r\n }\r\n}\r\n"]}
|