@4players/odin-common 1.0.6 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -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 +40 -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 +20 -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/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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export function isAudioCapable() {
|
|
2
|
+
return typeof AudioContext !== 'undefined' && typeof Worker !== 'undefined';
|
|
3
|
+
}
|
|
4
|
+
export function isBlinkBrowser() {
|
|
5
|
+
const pattern = /(apple)?webkit\/537\.36/i;
|
|
6
|
+
return (typeof window !== 'undefined' && pattern.test(window.navigator.userAgent));
|
|
7
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/* eslint-disable no-redeclare */
|
|
2
|
+
export function assert(condition, message) {
|
|
3
|
+
if (!condition) {
|
|
4
|
+
fail(message);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export function fail(message) {
|
|
8
|
+
throw new Error(message);
|
|
9
|
+
}
|
|
10
|
+
export function success(value) {
|
|
11
|
+
return { type: 'Success', value };
|
|
12
|
+
}
|
|
13
|
+
export function failure(reason) {
|
|
14
|
+
return { type: 'Failure', reason };
|
|
15
|
+
}
|
|
16
|
+
export function unwrap(result) {
|
|
17
|
+
if (result.type === 'Failure') {
|
|
18
|
+
fail(result.reason);
|
|
19
|
+
}
|
|
20
|
+
return result.value;
|
|
21
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as msgpack from '@msgpack/msgpack';
|
|
2
|
+
import { MessagePackRpcSchema } from '../schema/serialization';
|
|
3
|
+
import { failure, success } from './result';
|
|
4
|
+
export function msgpackEncode(value) {
|
|
5
|
+
return msgpack.encode(value);
|
|
6
|
+
}
|
|
7
|
+
export function msgpackDecode(buffer) {
|
|
8
|
+
return msgpack.decode(buffer);
|
|
9
|
+
}
|
|
10
|
+
export function parseRpc(buffer) {
|
|
11
|
+
try {
|
|
12
|
+
return success(MessagePackRpcSchema.parse(msgpackDecode(buffer)));
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
return failure(String(error));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function parseRpcMessage(events, rpc) {
|
|
19
|
+
if (!isKnownRpcMessage(events, rpc.name))
|
|
20
|
+
return undefined;
|
|
21
|
+
const parsed = events[rpc.name].safeParse(rpc.properties);
|
|
22
|
+
if (!parsed.success) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
name: rpc.name,
|
|
27
|
+
properties: parsed.data,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function isKnownRpcMessage(events, name) {
|
|
31
|
+
return name in events;
|
|
32
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export class Selector {
|
|
11
|
+
constructor(generators) {
|
|
12
|
+
this.generators = generators;
|
|
13
|
+
this.futures = generators.map(Selector.addIndex);
|
|
14
|
+
}
|
|
15
|
+
next() {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const [result, index] = yield Promise.race(this.futures);
|
|
18
|
+
this.futures[index] = Selector.addIndex(this.generators[index], index);
|
|
19
|
+
return result;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
static addIndex(generator, index) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
return [yield generator(), index];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* eslint-disable no-redeclare */
|
|
2
|
+
export function sleep(ms, value) {
|
|
3
|
+
if (ms <= 0) {
|
|
4
|
+
return Promise.resolve(value);
|
|
5
|
+
}
|
|
6
|
+
return new Promise((resolve) => setTimeout(() => resolve(value), ms));
|
|
7
|
+
}
|
|
8
|
+
export function abortableSleep(ms, signal) {
|
|
9
|
+
if (signal.aborted) {
|
|
10
|
+
return Promise.resolve('aborted');
|
|
11
|
+
}
|
|
12
|
+
return new Promise((resolve) => {
|
|
13
|
+
const onAbort = () => resolve('aborted');
|
|
14
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
15
|
+
setTimeout(() => {
|
|
16
|
+
signal.removeEventListener('abort', onAbort);
|
|
17
|
+
resolve(undefined);
|
|
18
|
+
}, ms);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export function nextTick() {
|
|
22
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
23
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export class Strand {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.tasks = [];
|
|
13
|
+
this.running = false;
|
|
14
|
+
}
|
|
15
|
+
enqueue(task) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const wrapped = () => __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
try {
|
|
19
|
+
const result = yield task();
|
|
20
|
+
resolve(result);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
reject(error);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
this.tasks.push(wrapped);
|
|
27
|
+
if (!this.running) {
|
|
28
|
+
this.execute();
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
execute() {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
this.running = true;
|
|
35
|
+
while (true) {
|
|
36
|
+
const task = this.tasks.shift();
|
|
37
|
+
if (task === undefined)
|
|
38
|
+
break;
|
|
39
|
+
yield task();
|
|
40
|
+
}
|
|
41
|
+
this.running = false;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { failure, success } from './result';
|
|
2
|
+
export function normalizeUrl(url) {
|
|
3
|
+
let normalized = url.trim();
|
|
4
|
+
if (url.indexOf('://') === -1)
|
|
5
|
+
normalized = `https://${normalized}`;
|
|
6
|
+
try {
|
|
7
|
+
return success(new URL(normalized));
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
return failure(String(error));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function extendUrl(base, path) {
|
|
14
|
+
let pathname = base.pathname;
|
|
15
|
+
if (pathname.endsWith('/') === false)
|
|
16
|
+
pathname += '/';
|
|
17
|
+
try {
|
|
18
|
+
return success(new URL(pathname + path, base));
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
return failure(String(error));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function isProperty(object, name) {
|
|
2
|
+
return name in object;
|
|
3
|
+
}
|
|
4
|
+
export function isFunction(value) {
|
|
5
|
+
return typeof value === 'function';
|
|
6
|
+
}
|
|
7
|
+
export function isNull(value) {
|
|
8
|
+
return typeof value === 'object' && value === null;
|
|
9
|
+
}
|
|
10
|
+
export function isNumber(value) {
|
|
11
|
+
return typeof value === 'number';
|
|
12
|
+
}
|
|
13
|
+
export function isObject(value) {
|
|
14
|
+
return typeof value === 'object';
|
|
15
|
+
}
|
|
16
|
+
export function isString(value) {
|
|
17
|
+
return typeof value === 'string';
|
|
18
|
+
}
|
|
19
|
+
export function isUndefined(value) {
|
|
20
|
+
return typeof value === 'undefined';
|
|
21
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { WebSocket } from 'ws';
|
|
11
|
+
import { failure, success } from './result';
|
|
12
|
+
import { sleep } from './sleep';
|
|
13
|
+
export function webSocketConnect(url, connectIntervals, clientOptions, onFailure) {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const start = performance.now();
|
|
16
|
+
let connection = failure('no connection was attempted');
|
|
17
|
+
let cancelled = false;
|
|
18
|
+
const cancel = () => {
|
|
19
|
+
cancelled = true;
|
|
20
|
+
};
|
|
21
|
+
const connect = () => __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
for (let failures = 0; failures < connectIntervals.length && !cancelled; failures++) {
|
|
23
|
+
const nextReconnectAt = start + whenToConnect(failures, connectIntervals);
|
|
24
|
+
yield sleep(nextReconnectAt - performance.now(), 'interval');
|
|
25
|
+
if (cancelled) {
|
|
26
|
+
return failure('cancelled');
|
|
27
|
+
}
|
|
28
|
+
connection = yield webSocketConnectOnce(url, clientOptions);
|
|
29
|
+
if (connection.type === 'Success') {
|
|
30
|
+
return connection;
|
|
31
|
+
}
|
|
32
|
+
else if (onFailure !== undefined) {
|
|
33
|
+
onFailure(failures + 1, cancel);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return connection;
|
|
37
|
+
});
|
|
38
|
+
return { connect, cancel };
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export function webSocketConnectOnce(url, clientOptions) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
let webSocket;
|
|
44
|
+
try {
|
|
45
|
+
webSocket = new WebSocket(url, clientOptions);
|
|
46
|
+
}
|
|
47
|
+
catch (reason) {
|
|
48
|
+
return failure(`unable to create websocket; ${reason}`);
|
|
49
|
+
}
|
|
50
|
+
return new Promise((resolve) => {
|
|
51
|
+
const onOpen = () => {
|
|
52
|
+
cleanup();
|
|
53
|
+
resolve(success(webSocket));
|
|
54
|
+
};
|
|
55
|
+
const onError = (event) => {
|
|
56
|
+
cleanup();
|
|
57
|
+
resolve(failure(event.message));
|
|
58
|
+
};
|
|
59
|
+
const onClose = (event) => {
|
|
60
|
+
cleanup();
|
|
61
|
+
resolve(failure(event.reason));
|
|
62
|
+
};
|
|
63
|
+
const cleanup = () => {
|
|
64
|
+
webSocket.removeEventListener('open', onOpen);
|
|
65
|
+
webSocket.removeEventListener('error', onError);
|
|
66
|
+
webSocket.removeEventListener('close', onClose);
|
|
67
|
+
};
|
|
68
|
+
webSocket.addEventListener('open', onOpen);
|
|
69
|
+
webSocket.addEventListener('error', onError);
|
|
70
|
+
webSocket.addEventListener('close', onClose);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function whenToConnect(failures, connectIntervals) {
|
|
75
|
+
const past_connects = connectIntervals
|
|
76
|
+
.slice(0, failures)
|
|
77
|
+
.reduce((a, b) => a + b, 0);
|
|
78
|
+
const interval = connectIntervals[failures] * Math.random();
|
|
79
|
+
return past_connects + interval;
|
|
80
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
export type Commands = Record<string, {
|
|
3
|
+
request: z.ZodTypeAny;
|
|
4
|
+
response: z.ZodTypeAny;
|
|
5
|
+
}>;
|
|
6
|
+
export declare const MainCommandsRpc: Commands;
|
|
7
|
+
export type MainCommands = typeof MainCommandsRpc;
|
|
8
|
+
export declare const RoomCommandsRpc: Commands;
|
|
9
|
+
export type RoomCommands = typeof RoomCommandsRpc;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
export type Notifications = Record<string, z.ZodTypeAny>;
|
|
3
|
+
export declare const MainNotificationsRpc: Notifications;
|
|
4
|
+
export type MainNotifications = typeof MainNotificationsRpc;
|
|
5
|
+
export declare const RoomNotificationsRpc: Notifications;
|
|
6
|
+
export type RoomNotifications = typeof RoomNotificationsRpc;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function find<T>(entries: Iterable<T>, predicate: (entry: T) => boolean): T | undefined;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare function assert(condition: unknown, message: string): asserts condition;
|
|
2
|
+
export declare function fail(message: string): never;
|
|
3
|
+
export type Failure = {
|
|
4
|
+
type: 'Failure';
|
|
5
|
+
reason: string;
|
|
6
|
+
};
|
|
7
|
+
export type Success<T> = {
|
|
8
|
+
type: 'Success';
|
|
9
|
+
value: T;
|
|
10
|
+
};
|
|
11
|
+
export type Result<T> = Success<T> | Failure;
|
|
12
|
+
export declare function success<T>(value: T): Success<T>;
|
|
13
|
+
export declare function success(value: void): Success<void>;
|
|
14
|
+
export declare function failure(reason: string): Failure;
|
|
15
|
+
export declare function unwrap<T>(result: Result<T>): T;
|
|
16
|
+
export type Accept<T> = (result: T) => void;
|
|
17
|
+
export type Reject = (reason: Error) => void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
import { MessagePackRpc } from '../schema/serialization';
|
|
3
|
+
import { Result } from './result';
|
|
4
|
+
import { Notifications } from '../rpc/notifications';
|
|
5
|
+
export declare function msgpackEncode(value: unknown): ArrayBuffer;
|
|
6
|
+
export declare function msgpackDecode(buffer: ArrayBuffer): unknown;
|
|
7
|
+
export interface RpcMessage {
|
|
8
|
+
name: string;
|
|
9
|
+
properties: unknown;
|
|
10
|
+
}
|
|
11
|
+
export interface TypedRpcMessage<Name extends string, Properties> extends RpcMessage {
|
|
12
|
+
name: Name;
|
|
13
|
+
properties: Properties;
|
|
14
|
+
}
|
|
15
|
+
export type RealizedEvents<E extends Notifications> = {
|
|
16
|
+
[Name in Extract<keyof E, string>]: TypedRpcMessage<Name, z.infer<E[Name]>>;
|
|
17
|
+
}[Extract<keyof E, string>];
|
|
18
|
+
export declare function parseRpc(buffer: ArrayBuffer): Result<MessagePackRpc>;
|
|
19
|
+
export declare function parseRpcMessage<E extends Notifications>(events: E, rpc: RpcMessage): RealizedEvents<E> | undefined;
|
|
20
|
+
export declare function isKnownRpcMessage<E extends Notifications>(events: E, name: string): name is Extract<keyof E, string>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class Selector<T extends Array<() => PromiseLike<unknown>>> {
|
|
2
|
+
private readonly generators;
|
|
3
|
+
private readonly futures;
|
|
4
|
+
constructor(generators: T);
|
|
5
|
+
next(): Promise<Awaited<ReturnType<T[number]>>>;
|
|
6
|
+
static addIndex(generator: () => PromiseLike<unknown>, index: number): Promise<[unknown, number]>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function sleep(ms: number, value: void): Promise<void>;
|
|
2
|
+
export declare function sleep<T extends string>(ms: number, value: T): Promise<T>;
|
|
3
|
+
export declare function abortableSleep(ms: number, signal: AbortSignal): Promise<void | 'aborted'>;
|
|
4
|
+
export declare function nextTick(): Promise<void>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function isProperty<T extends object>(object: T, name: string): name is Extract<keyof T, string>;
|
|
2
|
+
export declare function isFunction(value: any): value is Function;
|
|
3
|
+
export declare function isNull(value: any): boolean;
|
|
4
|
+
export declare function isNumber(value: any): value is Number;
|
|
5
|
+
export declare function isObject(value: any): value is Object;
|
|
6
|
+
export declare function isString(value: any): value is String;
|
|
7
|
+
export declare function isUndefined(value: any): value is undefined;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ClientOptions, WebSocket } from 'ws';
|
|
2
|
+
import { Result } from './result';
|
|
3
|
+
export declare function webSocketConnect(url: URL, connectIntervals: number[], clientOptions?: ClientOptions, onFailure?: (failures: number, cancel: () => void) => void): Promise<{
|
|
4
|
+
connect: () => Promise<Result<WebSocket>>;
|
|
5
|
+
cancel: () => void;
|
|
6
|
+
}>;
|
|
7
|
+
export declare function webSocketConnectOnce(url: URL, clientOptions?: ClientOptions): Promise<Result<WebSocket>>;
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@4players/odin-common",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "A collection of commonly used type definitions and
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"description": "A collection of commonly used type definitions and utility functions across ODIN web projects",
|
|
5
5
|
"author": "Josho Bleicker <josho.bleicker@4players.io> (https://www.4players.io)",
|
|
6
6
|
"homepage": "https://www.4players.io",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"odin",
|
|
9
9
|
"sdk",
|
|
10
|
+
"utility",
|
|
10
11
|
"schemas",
|
|
11
12
|
"types",
|
|
12
13
|
"zod"
|
|
@@ -15,26 +16,33 @@
|
|
|
15
16
|
"license": "ISC",
|
|
16
17
|
"main": "./lib/cjs/index.js",
|
|
17
18
|
"module": "./lib/esm/index.js",
|
|
18
|
-
"types": "./lib/
|
|
19
|
+
"types": "./lib/index.d.ts",
|
|
19
20
|
"scripts": {
|
|
21
|
+
"clean": "rm -rf lib",
|
|
20
22
|
"build": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json",
|
|
21
|
-
"
|
|
23
|
+
"format": "prettier 'src/**/*.ts' --write",
|
|
24
|
+
"lint": "eslint . --ext .ts",
|
|
25
|
+
"test": "testyts"
|
|
22
26
|
},
|
|
23
27
|
"engines": {
|
|
24
28
|
"node": ">=14"
|
|
25
29
|
},
|
|
26
30
|
"dependencies": {
|
|
31
|
+
"@msgpack/msgpack": "~3.0.0-beta2",
|
|
32
|
+
"ws": "~8.15.0",
|
|
27
33
|
"zod": "~3.22.0"
|
|
28
34
|
},
|
|
29
35
|
"devDependencies": {
|
|
30
|
-
"@typescript-eslint/eslint-plugin": "~6.
|
|
31
|
-
"@typescript-eslint/parser": "~6.
|
|
32
|
-
"
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "~6.14.0",
|
|
37
|
+
"@typescript-eslint/parser": "~6.14.0",
|
|
38
|
+
"@types/ws": "~8.5.0",
|
|
39
|
+
"eslint": "~8.55.0",
|
|
33
40
|
"eslint-config-semistandard": "~17.0.0",
|
|
34
41
|
"eslint-plugin-import": "~2.29.0",
|
|
35
42
|
"eslint-plugin-node": "~11.1.0",
|
|
36
43
|
"eslint-plugin-promise": "~6.1.0",
|
|
37
|
-
"prettier": "3.1.0",
|
|
44
|
+
"prettier": "~3.1.0",
|
|
45
|
+
"testyts": "~1.5.0",
|
|
38
46
|
"typescript": "~5.2.0"
|
|
39
47
|
}
|
|
40
48
|
}
|
package/lib/types/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from './schemas/serialization';
|
|
2
|
-
export * from './schemas/token';
|
|
3
|
-
export * from './schemas/room';
|
|
4
|
-
export * from './schemas/peer';
|
|
5
|
-
export * from './schemas/media';
|
|
6
|
-
export * from './schemas/message';
|
|
7
|
-
export * from './schemas/webrtc';
|
|
8
|
-
export * from './streams/commands';
|
|
9
|
-
export * from './streams/notifications';
|
|
10
|
-
export * from './plugins/api';
|