@4players/odin-common 1.0.6 → 1.1.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/CHANGELOG.md +6 -2
- package/lib/cjs/index.js +23 -10
- package/lib/cjs/{streams → rpc}/commands.js +8 -8
- package/lib/cjs/{streams → rpc}/notifications.js +7 -7
- package/lib/cjs/utility/base64.js +30 -0
- package/lib/cjs/utility/bytearray.js +29 -0
- package/lib/cjs/utility/environment.js +12 -0
- package/lib/cjs/utility/iterable.js +11 -0
- package/lib/cjs/utility/json.js +7 -0
- package/lib/cjs/utility/result.js +29 -0
- package/lib/cjs/utility/rpc.js +39 -0
- package/lib/cjs/utility/selector.js +31 -0
- package/lib/cjs/utility/sleep.js +29 -0
- package/lib/cjs/utility/strand.js +48 -0
- package/lib/cjs/utility/url.js +28 -0
- package/lib/cjs/utility/validation.js +31 -0
- package/lib/cjs/utility/websocket.js +85 -0
- package/lib/esm/index.js +23 -10
- package/lib/esm/{streams → rpc}/commands.js +7 -7
- package/lib/esm/{streams → rpc}/notifications.js +6 -6
- package/lib/esm/utility/base64.js +25 -0
- package/lib/esm/utility/bytearray.js +24 -0
- package/lib/esm/utility/environment.js +7 -0
- package/lib/esm/utility/iterable.js +7 -0
- package/lib/esm/utility/json.js +3 -0
- package/lib/esm/utility/result.js +21 -0
- package/lib/esm/utility/rpc.js +32 -0
- package/lib/esm/utility/selector.js +27 -0
- package/lib/esm/utility/sleep.js +23 -0
- package/lib/esm/utility/strand.js +44 -0
- package/lib/esm/utility/url.js +23 -0
- package/lib/esm/utility/validation.js +21 -0
- package/lib/esm/utility/websocket.js +80 -0
- package/lib/index.d.ts +23 -0
- package/lib/rpc/commands.d.ts +9 -0
- package/lib/rpc/notifications.d.ts +6 -0
- package/lib/utility/base64.d.ts +3 -0
- package/lib/utility/base64.spec.d.ts +4 -0
- package/lib/utility/bytearray.d.ts +4 -0
- package/lib/utility/bytearray.spec.d.ts +4 -0
- package/lib/utility/environment.d.ts +2 -0
- package/lib/utility/environment.spec.d.ts +4 -0
- package/lib/utility/iterable.d.ts +1 -0
- package/lib/utility/iterable.spec.d.ts +4 -0
- package/lib/utility/json.d.ts +4 -0
- package/lib/utility/json.spec.d.ts +7 -0
- package/lib/utility/result.d.ts +17 -0
- package/lib/utility/rpc.d.ts +19 -0
- package/lib/utility/selector.d.ts +7 -0
- package/lib/utility/sleep.d.ts +4 -0
- package/lib/utility/strand.d.ts +6 -0
- package/lib/utility/url.d.ts +3 -0
- package/lib/utility/validation.d.ts +7 -0
- package/lib/utility/websocket.d.ts +7 -0
- package/lib/utility/websocket.spec.d.ts +6 -0
- package/package.json +16 -8
- package/testy.json +4 -0
- package/lib/types/index.d.ts +0 -10
- package/lib/types/streams/commands.d.ts +0 -218
- package/lib/types/streams/notifications.d.ts +0 -779
- /package/lib/cjs/{plugins → plugin}/api.js +0 -0
- /package/lib/cjs/{schemas → schema}/media.js +0 -0
- /package/lib/cjs/{schemas → schema}/message.js +0 -0
- /package/lib/cjs/{schemas → schema}/peer.js +0 -0
- /package/lib/cjs/{schemas → schema}/room.js +0 -0
- /package/lib/cjs/{schemas → schema}/serialization.js +0 -0
- /package/lib/cjs/{schemas → schema}/token.js +0 -0
- /package/lib/cjs/{schemas → schema}/webrtc.js +0 -0
- /package/lib/esm/{plugins → plugin}/api.js +0 -0
- /package/lib/esm/{schemas → schema}/media.js +0 -0
- /package/lib/esm/{schemas → schema}/message.js +0 -0
- /package/lib/esm/{schemas → schema}/peer.js +0 -0
- /package/lib/esm/{schemas → schema}/room.js +0 -0
- /package/lib/esm/{schemas → schema}/serialization.js +0 -0
- /package/lib/esm/{schemas → schema}/token.js +0 -0
- /package/lib/esm/{schemas → schema}/webrtc.js +0 -0
- /package/lib/{types/plugins → plugin}/api.d.ts +0 -0
- /package/lib/{types/schemas → schema}/media.d.ts +0 -0
- /package/lib/{types/schemas → schema}/message.d.ts +0 -0
- /package/lib/{types/schemas → schema}/peer.d.ts +0 -0
- /package/lib/{types/schemas → schema}/room.d.ts +0 -0
- /package/lib/{types/schemas → schema}/serialization.d.ts +0 -0
- /package/lib/{types/schemas → schema}/token.d.ts +0 -0
- /package/lib/{types/schemas → schema}/webrtc.d.ts +0 -0
package/CHANGELOG.md
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -14,13 +14,26 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
18
|
-
__exportStar(require("./
|
|
19
|
-
__exportStar(require("./
|
|
20
|
-
__exportStar(require("./
|
|
21
|
-
__exportStar(require("./
|
|
22
|
-
__exportStar(require("./
|
|
23
|
-
__exportStar(require("./
|
|
24
|
-
__exportStar(require("./
|
|
25
|
-
__exportStar(require("./
|
|
26
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./schema/serialization"), exports);
|
|
18
|
+
__exportStar(require("./schema/token"), exports);
|
|
19
|
+
__exportStar(require("./schema/room"), exports);
|
|
20
|
+
__exportStar(require("./schema/peer"), exports);
|
|
21
|
+
__exportStar(require("./schema/media"), exports);
|
|
22
|
+
__exportStar(require("./schema/message"), exports);
|
|
23
|
+
__exportStar(require("./schema/webrtc"), exports);
|
|
24
|
+
__exportStar(require("./rpc/commands"), exports);
|
|
25
|
+
__exportStar(require("./rpc/notifications"), exports);
|
|
26
|
+
__exportStar(require("./utility/base64"), exports);
|
|
27
|
+
__exportStar(require("./utility/bytearray"), exports);
|
|
28
|
+
__exportStar(require("./utility/iterable"), exports);
|
|
29
|
+
__exportStar(require("./utility/environment"), exports);
|
|
30
|
+
__exportStar(require("./utility/json"), exports);
|
|
31
|
+
__exportStar(require("./utility/result"), exports);
|
|
32
|
+
__exportStar(require("./utility/rpc"), exports);
|
|
33
|
+
__exportStar(require("./utility/selector"), exports);
|
|
34
|
+
__exportStar(require("./utility/sleep"), exports);
|
|
35
|
+
__exportStar(require("./utility/strand"), exports);
|
|
36
|
+
__exportStar(require("./utility/url"), exports);
|
|
37
|
+
__exportStar(require("./utility/validation"), exports);
|
|
38
|
+
__exportStar(require("./utility/websocket"), exports);
|
|
39
|
+
__exportStar(require("./plugin/api"), exports);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.RoomCommandsRpc = exports.MainCommandsRpc = void 0;
|
|
4
4
|
const z = require("zod");
|
|
5
|
-
const serialization_1 = require("../
|
|
6
|
-
const room_1 = require("../
|
|
7
|
-
const peer_1 = require("../
|
|
8
|
-
const media_1 = require("../
|
|
9
|
-
const webrtc_1 = require("../
|
|
10
|
-
exports.
|
|
5
|
+
const serialization_1 = require("../schema/serialization");
|
|
6
|
+
const room_1 = require("../schema/room");
|
|
7
|
+
const peer_1 = require("../schema/peer");
|
|
8
|
+
const media_1 = require("../schema/media");
|
|
9
|
+
const webrtc_1 = require("../schema/webrtc");
|
|
10
|
+
exports.MainCommandsRpc = {
|
|
11
11
|
JoinRoom: {
|
|
12
12
|
request: z.object({
|
|
13
13
|
token: z.string(),
|
|
@@ -34,7 +34,7 @@ exports.MainCommands = {
|
|
|
34
34
|
response: z.null(),
|
|
35
35
|
},
|
|
36
36
|
};
|
|
37
|
-
exports.
|
|
37
|
+
exports.RoomCommandsRpc = {
|
|
38
38
|
UpdatePeer: {
|
|
39
39
|
request: z.object({ user_data: serialization_1.ByteArraySchema }),
|
|
40
40
|
response: z.null(),
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const room_1 = require("../
|
|
5
|
-
const peer_1 = require("../
|
|
6
|
-
const message_1 = require("../
|
|
7
|
-
const webrtc_1 = require("../
|
|
8
|
-
exports.
|
|
3
|
+
exports.RoomNotificationsRpc = exports.MainNotificationsRpc = void 0;
|
|
4
|
+
const room_1 = require("../schema/room");
|
|
5
|
+
const peer_1 = require("../schema/peer");
|
|
6
|
+
const message_1 = require("../schema/message");
|
|
7
|
+
const webrtc_1 = require("../schema/webrtc");
|
|
8
|
+
exports.MainNotificationsRpc = {
|
|
9
9
|
WebRtcUpdate: webrtc_1.WebRtcUpdateSchema,
|
|
10
10
|
};
|
|
11
|
-
exports.
|
|
11
|
+
exports.RoomNotificationsRpc = {
|
|
12
12
|
RoomStatusChanged: room_1.RoomStatusChangedSchema,
|
|
13
13
|
RoomUpdated: room_1.RoomUpdatesSchema,
|
|
14
14
|
PeerUpdated: peer_1.PeerUpdateSchema,
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fromBase64Url = exports.fromBase64 = void 0;
|
|
4
|
+
const result_1 = require("./result");
|
|
5
|
+
function fromBase64(base64) {
|
|
6
|
+
try {
|
|
7
|
+
const binString = atob(base64);
|
|
8
|
+
/* @ts-ignore: strings are array-like */
|
|
9
|
+
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
10
|
+
const decoder = new TextDecoder('utf8');
|
|
11
|
+
return (0, result_1.success)(decoder.decode(bytes));
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
return (0, result_1.failure)(String(error));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.fromBase64 = fromBase64;
|
|
18
|
+
function fromBase64Url(base64uri) {
|
|
19
|
+
try {
|
|
20
|
+
const binString = atob(base64uri.replace(/-/g, '+').replace(/_/g, '/'));
|
|
21
|
+
/* @ts-ignore: strings are array-like */
|
|
22
|
+
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
23
|
+
const decoder = new TextDecoder('utf8');
|
|
24
|
+
return (0, result_1.success)(decoder.decode(bytes));
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
return (0, result_1.failure)(String(error));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.fromBase64Url = fromBase64Url;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fromBytes = exports.toBytes = void 0;
|
|
4
|
+
const result_1 = require("./result");
|
|
5
|
+
function toBytes(value) {
|
|
6
|
+
try {
|
|
7
|
+
(0, result_1.assert)(value !== undefined, 'undefined cannot be converted to byte array');
|
|
8
|
+
(0, result_1.assert)(value !== null, 'null cannot be converted to byte array');
|
|
9
|
+
const json = JSON.stringify(value);
|
|
10
|
+
const decoder = new TextEncoder();
|
|
11
|
+
return (0, result_1.success)(decoder.encode(json));
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
return (0, result_1.failure)(String(error));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.toBytes = toBytes;
|
|
18
|
+
function fromBytes(bytes) {
|
|
19
|
+
try {
|
|
20
|
+
(0, result_1.assert)(bytes.length > 0, 'empty byte array cannot be converted to value');
|
|
21
|
+
const json = new TextDecoder().decode(bytes);
|
|
22
|
+
const text = JSON.parse(json);
|
|
23
|
+
return (0, result_1.success)(text);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
return (0, result_1.failure)(String(error));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.fromBytes = fromBytes;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBlinkBrowser = exports.isAudioCapable = void 0;
|
|
4
|
+
function isAudioCapable() {
|
|
5
|
+
return typeof AudioContext !== 'undefined' && typeof Worker !== 'undefined';
|
|
6
|
+
}
|
|
7
|
+
exports.isAudioCapable = isAudioCapable;
|
|
8
|
+
function isBlinkBrowser() {
|
|
9
|
+
const pattern = /(apple)?webkit\/537\.36/i;
|
|
10
|
+
return (typeof window !== 'undefined' && pattern.test(window.navigator.userAgent));
|
|
11
|
+
}
|
|
12
|
+
exports.isBlinkBrowser = isBlinkBrowser;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.find = void 0;
|
|
4
|
+
function find(entries, predicate) {
|
|
5
|
+
for (const entry of entries) {
|
|
6
|
+
if (predicate(entry))
|
|
7
|
+
return entry;
|
|
8
|
+
}
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
exports.find = find;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-redeclare */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.unwrap = exports.failure = exports.success = exports.fail = exports.assert = void 0;
|
|
5
|
+
function assert(condition, message) {
|
|
6
|
+
if (!condition) {
|
|
7
|
+
fail(message);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.assert = assert;
|
|
11
|
+
function fail(message) {
|
|
12
|
+
throw new Error(message);
|
|
13
|
+
}
|
|
14
|
+
exports.fail = fail;
|
|
15
|
+
function success(value) {
|
|
16
|
+
return { type: 'Success', value };
|
|
17
|
+
}
|
|
18
|
+
exports.success = success;
|
|
19
|
+
function failure(reason) {
|
|
20
|
+
return { type: 'Failure', reason };
|
|
21
|
+
}
|
|
22
|
+
exports.failure = failure;
|
|
23
|
+
function unwrap(result) {
|
|
24
|
+
if (result.type === 'Failure') {
|
|
25
|
+
fail(result.reason);
|
|
26
|
+
}
|
|
27
|
+
return result.value;
|
|
28
|
+
}
|
|
29
|
+
exports.unwrap = unwrap;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseRpcMessage = exports.parseRpc = exports.msgpackDecode = exports.msgpackEncode = void 0;
|
|
4
|
+
const msgpack = require("@msgpack/msgpack");
|
|
5
|
+
const serialization_1 = require("../schema/serialization");
|
|
6
|
+
const result_1 = require("./result");
|
|
7
|
+
function msgpackEncode(value) {
|
|
8
|
+
return msgpack.encode(value);
|
|
9
|
+
}
|
|
10
|
+
exports.msgpackEncode = msgpackEncode;
|
|
11
|
+
function msgpackDecode(buffer) {
|
|
12
|
+
return msgpack.decode(buffer);
|
|
13
|
+
}
|
|
14
|
+
exports.msgpackDecode = msgpackDecode;
|
|
15
|
+
function parseRpc(buffer) {
|
|
16
|
+
try {
|
|
17
|
+
return (0, result_1.success)(serialization_1.MessagePackRpcSchema.parse(msgpackDecode(buffer)));
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
return (0, result_1.failure)(String(error));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.parseRpc = parseRpc;
|
|
24
|
+
function parseRpcMessage(events, rpc) {
|
|
25
|
+
if (!isKnownRpcMessage(events, rpc.name))
|
|
26
|
+
return undefined;
|
|
27
|
+
const parsed = events[rpc.name].safeParse(rpc.properties);
|
|
28
|
+
if (!parsed.success) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
name: rpc.name,
|
|
33
|
+
properties: parsed.data,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.parseRpcMessage = parseRpcMessage;
|
|
37
|
+
function isKnownRpcMessage(events, name) {
|
|
38
|
+
return name in events;
|
|
39
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Selector = void 0;
|
|
13
|
+
class Selector {
|
|
14
|
+
constructor(generators) {
|
|
15
|
+
this.generators = generators;
|
|
16
|
+
this.futures = generators.map(Selector.addIndex);
|
|
17
|
+
}
|
|
18
|
+
next() {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
const [result, index] = yield Promise.race(this.futures);
|
|
21
|
+
this.futures[index] = Selector.addIndex(this.generators[index], index);
|
|
22
|
+
return result;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
static addIndex(generator, index) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
return [yield generator(), index];
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.Selector = Selector;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-redeclare */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.nextTick = exports.abortableSleep = exports.sleep = void 0;
|
|
5
|
+
function sleep(ms, value) {
|
|
6
|
+
if (ms <= 0) {
|
|
7
|
+
return Promise.resolve(value);
|
|
8
|
+
}
|
|
9
|
+
return new Promise((resolve) => setTimeout(() => resolve(value), ms));
|
|
10
|
+
}
|
|
11
|
+
exports.sleep = sleep;
|
|
12
|
+
function abortableSleep(ms, signal) {
|
|
13
|
+
if (signal.aborted) {
|
|
14
|
+
return Promise.resolve('aborted');
|
|
15
|
+
}
|
|
16
|
+
return new Promise((resolve) => {
|
|
17
|
+
const onAbort = () => resolve('aborted');
|
|
18
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
19
|
+
setTimeout(() => {
|
|
20
|
+
signal.removeEventListener('abort', onAbort);
|
|
21
|
+
resolve(undefined);
|
|
22
|
+
}, ms);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
exports.abortableSleep = abortableSleep;
|
|
26
|
+
function nextTick() {
|
|
27
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
28
|
+
}
|
|
29
|
+
exports.nextTick = nextTick;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Strand = void 0;
|
|
13
|
+
class Strand {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.tasks = [];
|
|
16
|
+
this.running = false;
|
|
17
|
+
}
|
|
18
|
+
enqueue(task) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const wrapped = () => __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
try {
|
|
22
|
+
const result = yield task();
|
|
23
|
+
resolve(result);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
reject(error);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
this.tasks.push(wrapped);
|
|
30
|
+
if (!this.running) {
|
|
31
|
+
this.execute();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
execute() {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
this.running = true;
|
|
38
|
+
while (true) {
|
|
39
|
+
const task = this.tasks.shift();
|
|
40
|
+
if (task === undefined)
|
|
41
|
+
break;
|
|
42
|
+
yield task();
|
|
43
|
+
}
|
|
44
|
+
this.running = false;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.Strand = Strand;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendUrl = exports.normalizeUrl = void 0;
|
|
4
|
+
const result_1 = require("./result");
|
|
5
|
+
function normalizeUrl(url) {
|
|
6
|
+
let normalized = url.trim();
|
|
7
|
+
if (url.indexOf('://') === -1)
|
|
8
|
+
normalized = `https://${normalized}`;
|
|
9
|
+
try {
|
|
10
|
+
return (0, result_1.success)(new URL(normalized));
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
return (0, result_1.failure)(String(error));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.normalizeUrl = normalizeUrl;
|
|
17
|
+
function extendUrl(base, path) {
|
|
18
|
+
let pathname = base.pathname;
|
|
19
|
+
if (pathname.endsWith('/') === false)
|
|
20
|
+
pathname += '/';
|
|
21
|
+
try {
|
|
22
|
+
return (0, result_1.success)(new URL(pathname + path, base));
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
return (0, result_1.failure)(String(error));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.extendUrl = extendUrl;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isUndefined = exports.isString = exports.isObject = exports.isNumber = exports.isNull = exports.isFunction = exports.isProperty = void 0;
|
|
4
|
+
function isProperty(object, name) {
|
|
5
|
+
return name in object;
|
|
6
|
+
}
|
|
7
|
+
exports.isProperty = isProperty;
|
|
8
|
+
function isFunction(value) {
|
|
9
|
+
return typeof value === 'function';
|
|
10
|
+
}
|
|
11
|
+
exports.isFunction = isFunction;
|
|
12
|
+
function isNull(value) {
|
|
13
|
+
return typeof value === 'object' && value === null;
|
|
14
|
+
}
|
|
15
|
+
exports.isNull = isNull;
|
|
16
|
+
function isNumber(value) {
|
|
17
|
+
return typeof value === 'number';
|
|
18
|
+
}
|
|
19
|
+
exports.isNumber = isNumber;
|
|
20
|
+
function isObject(value) {
|
|
21
|
+
return typeof value === 'object';
|
|
22
|
+
}
|
|
23
|
+
exports.isObject = isObject;
|
|
24
|
+
function isString(value) {
|
|
25
|
+
return typeof value === 'string';
|
|
26
|
+
}
|
|
27
|
+
exports.isString = isString;
|
|
28
|
+
function isUndefined(value) {
|
|
29
|
+
return typeof value === 'undefined';
|
|
30
|
+
}
|
|
31
|
+
exports.isUndefined = isUndefined;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.webSocketConnectOnce = exports.webSocketConnect = void 0;
|
|
13
|
+
const ws_1 = require("ws");
|
|
14
|
+
const result_1 = require("./result");
|
|
15
|
+
const sleep_1 = require("./sleep");
|
|
16
|
+
function webSocketConnect(url, connectIntervals, clientOptions, onFailure) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
const start = performance.now();
|
|
19
|
+
let connection = (0, result_1.failure)('no connection was attempted');
|
|
20
|
+
let cancelled = false;
|
|
21
|
+
const cancel = () => {
|
|
22
|
+
cancelled = true;
|
|
23
|
+
};
|
|
24
|
+
const connect = () => __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
for (let failures = 0; failures < connectIntervals.length && !cancelled; failures++) {
|
|
26
|
+
const nextReconnectAt = start + whenToConnect(failures, connectIntervals);
|
|
27
|
+
yield (0, sleep_1.sleep)(nextReconnectAt - performance.now(), 'interval');
|
|
28
|
+
if (cancelled) {
|
|
29
|
+
return (0, result_1.failure)('cancelled');
|
|
30
|
+
}
|
|
31
|
+
connection = yield webSocketConnectOnce(url, clientOptions);
|
|
32
|
+
if (connection.type === 'Success') {
|
|
33
|
+
return connection;
|
|
34
|
+
}
|
|
35
|
+
else if (onFailure !== undefined) {
|
|
36
|
+
onFailure(failures + 1, cancel);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return connection;
|
|
40
|
+
});
|
|
41
|
+
return { connect, cancel };
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.webSocketConnect = webSocketConnect;
|
|
45
|
+
function webSocketConnectOnce(url, clientOptions) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
let webSocket;
|
|
48
|
+
try {
|
|
49
|
+
webSocket = new ws_1.WebSocket(url, clientOptions);
|
|
50
|
+
}
|
|
51
|
+
catch (reason) {
|
|
52
|
+
return (0, result_1.failure)(`unable to create websocket; ${reason}`);
|
|
53
|
+
}
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
const onOpen = () => {
|
|
56
|
+
cleanup();
|
|
57
|
+
resolve((0, result_1.success)(webSocket));
|
|
58
|
+
};
|
|
59
|
+
const onError = (event) => {
|
|
60
|
+
cleanup();
|
|
61
|
+
resolve((0, result_1.failure)(event.message));
|
|
62
|
+
};
|
|
63
|
+
const onClose = (event) => {
|
|
64
|
+
cleanup();
|
|
65
|
+
resolve((0, result_1.failure)(event.reason));
|
|
66
|
+
};
|
|
67
|
+
const cleanup = () => {
|
|
68
|
+
webSocket.removeEventListener('open', onOpen);
|
|
69
|
+
webSocket.removeEventListener('error', onError);
|
|
70
|
+
webSocket.removeEventListener('close', onClose);
|
|
71
|
+
};
|
|
72
|
+
webSocket.addEventListener('open', onOpen);
|
|
73
|
+
webSocket.addEventListener('error', onError);
|
|
74
|
+
webSocket.addEventListener('close', onClose);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
exports.webSocketConnectOnce = webSocketConnectOnce;
|
|
79
|
+
function whenToConnect(failures, connectIntervals) {
|
|
80
|
+
const past_connects = connectIntervals
|
|
81
|
+
.slice(0, failures)
|
|
82
|
+
.reduce((a, b) => a + b, 0);
|
|
83
|
+
const interval = connectIntervals[failures] * Math.random();
|
|
84
|
+
return past_connects + interval;
|
|
85
|
+
}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
8
|
-
export * from './
|
|
9
|
-
export * from './
|
|
10
|
-
export * from './
|
|
1
|
+
export * from './schema/serialization';
|
|
2
|
+
export * from './schema/token';
|
|
3
|
+
export * from './schema/room';
|
|
4
|
+
export * from './schema/peer';
|
|
5
|
+
export * from './schema/media';
|
|
6
|
+
export * from './schema/message';
|
|
7
|
+
export * from './schema/webrtc';
|
|
8
|
+
export * from './rpc/commands';
|
|
9
|
+
export * from './rpc/notifications';
|
|
10
|
+
export * from './utility/base64';
|
|
11
|
+
export * from './utility/bytearray';
|
|
12
|
+
export * from './utility/iterable';
|
|
13
|
+
export * from './utility/environment';
|
|
14
|
+
export * from './utility/json';
|
|
15
|
+
export * from './utility/result';
|
|
16
|
+
export * from './utility/rpc';
|
|
17
|
+
export * from './utility/selector';
|
|
18
|
+
export * from './utility/sleep';
|
|
19
|
+
export * from './utility/strand';
|
|
20
|
+
export * from './utility/url';
|
|
21
|
+
export * from './utility/validation';
|
|
22
|
+
export * from './utility/websocket';
|
|
23
|
+
export * from './plugin/api';
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as z from 'zod';
|
|
2
|
-
import { ByteArraySchema } from '../
|
|
3
|
-
import { RoomIdSchema } from '../
|
|
4
|
-
import { PeerIdSchema, PeerPositionSchema } from '../
|
|
5
|
-
import { MediaIdSchema, MediaPropertiesSchema } from '../
|
|
6
|
-
import { WebRtcUpdateSchema } from '../
|
|
7
|
-
export const
|
|
2
|
+
import { ByteArraySchema } from '../schema/serialization';
|
|
3
|
+
import { RoomIdSchema } from '../schema/room';
|
|
4
|
+
import { PeerIdSchema, PeerPositionSchema } from '../schema/peer';
|
|
5
|
+
import { MediaIdSchema, MediaPropertiesSchema } from '../schema/media';
|
|
6
|
+
import { WebRtcUpdateSchema } from '../schema/webrtc';
|
|
7
|
+
export const MainCommandsRpc = {
|
|
8
8
|
JoinRoom: {
|
|
9
9
|
request: z.object({
|
|
10
10
|
token: z.string(),
|
|
@@ -31,7 +31,7 @@ export const MainCommands = {
|
|
|
31
31
|
response: z.null(),
|
|
32
32
|
},
|
|
33
33
|
};
|
|
34
|
-
export const
|
|
34
|
+
export const RoomCommandsRpc = {
|
|
35
35
|
UpdatePeer: {
|
|
36
36
|
request: z.object({ user_data: ByteArraySchema }),
|
|
37
37
|
response: z.null(),
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { RoomStatusChangedSchema, RoomUpdatesSchema } from '../
|
|
2
|
-
import { PeerUpdateSchema } from '../
|
|
3
|
-
import { MessageReceivedSchema } from '../
|
|
4
|
-
import { WebRtcUpdateSchema } from '../
|
|
5
|
-
export const
|
|
1
|
+
import { RoomStatusChangedSchema, RoomUpdatesSchema } from '../schema/room';
|
|
2
|
+
import { PeerUpdateSchema } from '../schema/peer';
|
|
3
|
+
import { MessageReceivedSchema } from '../schema/message';
|
|
4
|
+
import { WebRtcUpdateSchema } from '../schema/webrtc';
|
|
5
|
+
export const MainNotificationsRpc = {
|
|
6
6
|
WebRtcUpdate: WebRtcUpdateSchema,
|
|
7
7
|
};
|
|
8
|
-
export const
|
|
8
|
+
export const RoomNotificationsRpc = {
|
|
9
9
|
RoomStatusChanged: RoomStatusChangedSchema,
|
|
10
10
|
RoomUpdated: RoomUpdatesSchema,
|
|
11
11
|
PeerUpdated: PeerUpdateSchema,
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { failure, success } from './result';
|
|
2
|
+
export function fromBase64(base64) {
|
|
3
|
+
try {
|
|
4
|
+
const binString = atob(base64);
|
|
5
|
+
/* @ts-ignore: strings are array-like */
|
|
6
|
+
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
7
|
+
const decoder = new TextDecoder('utf8');
|
|
8
|
+
return success(decoder.decode(bytes));
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
return failure(String(error));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function fromBase64Url(base64uri) {
|
|
15
|
+
try {
|
|
16
|
+
const binString = atob(base64uri.replace(/-/g, '+').replace(/_/g, '/'));
|
|
17
|
+
/* @ts-ignore: strings are array-like */
|
|
18
|
+
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
19
|
+
const decoder = new TextDecoder('utf8');
|
|
20
|
+
return success(decoder.decode(bytes));
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
return failure(String(error));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { assert, failure, success } from './result';
|
|
2
|
+
export function toBytes(value) {
|
|
3
|
+
try {
|
|
4
|
+
assert(value !== undefined, 'undefined cannot be converted to byte array');
|
|
5
|
+
assert(value !== null, 'null cannot be converted to byte array');
|
|
6
|
+
const json = JSON.stringify(value);
|
|
7
|
+
const decoder = new TextEncoder();
|
|
8
|
+
return success(decoder.encode(json));
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
return failure(String(error));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function fromBytes(bytes) {
|
|
15
|
+
try {
|
|
16
|
+
assert(bytes.length > 0, 'empty byte array cannot be converted to value');
|
|
17
|
+
const json = new TextDecoder().decode(bytes);
|
|
18
|
+
const text = JSON.parse(json);
|
|
19
|
+
return success(text);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
return failure(String(error));
|
|
23
|
+
}
|
|
24
|
+
}
|