@omujs/omu 0.9.47 → 0.9.49
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/dts/api/asset/extension.d.ts +25 -0
- package/dist/dts/api/asset/index.d.ts +2 -0
- package/dist/dts/api/dashboard/extension.d.ts +183 -0
- package/{src/api/dashboard/handler.ts → dist/dts/api/dashboard/handler.d.ts} +0 -2
- package/dist/dts/api/dashboard/index.d.ts +3 -0
- package/dist/dts/api/dashboard/packets.d.ts +73 -0
- package/dist/dts/api/endpoint/endpoint.d.ts +21 -0
- package/dist/dts/api/endpoint/extension.d.ts +17 -0
- package/{src/api/endpoint/index.ts → dist/dts/api/endpoint/index.d.ts} +0 -1
- package/dist/dts/api/endpoint/packets.d.ts +51 -0
- package/dist/dts/api/extension-registry.d.ts +8 -0
- package/dist/dts/api/extension.d.ts +11 -0
- package/dist/dts/api/http/extension.d.ts +41 -0
- package/dist/dts/api/http/http.d.ts +36 -0
- package/{src/api/http/index.ts → dist/dts/api/http/index.d.ts} +0 -1
- package/dist/dts/api/http/websocket.d.ts +125 -0
- package/dist/dts/api/i18n/extension.d.ts +19 -0
- package/{src/api/i18n/index.ts → dist/dts/api/i18n/index.d.ts} +0 -1
- package/{src/api/index.ts → dist/dts/api/index.d.ts} +0 -1
- package/dist/dts/api/logger/extension.d.ts +25 -0
- package/dist/dts/api/logger/index.d.ts +1 -0
- package/dist/dts/api/logger/packets.d.ts +18 -0
- package/dist/dts/api/permission/extension.d.ts +19 -0
- package/{src/api/permission/index.ts → dist/dts/api/permission/index.d.ts} +0 -1
- package/dist/dts/api/permission/permission.d.ts +25 -0
- package/dist/dts/api/plugin/extension.d.ts +43 -0
- package/{src/api/plugin/index.ts → dist/dts/api/plugin/index.d.ts} +0 -1
- package/{src/api/plugin/package-info.ts → dist/dts/api/plugin/package-info.d.ts} +0 -7
- package/dist/dts/api/registry/extension.d.ts +23 -0
- package/{src/api/registry/index.ts → dist/dts/api/registry/index.d.ts} +0 -1
- package/dist/dts/api/registry/packets.d.ts +20 -0
- package/{src/api/registry/registry.ts → dist/dts/api/registry/registry.d.ts} +11 -53
- package/dist/dts/api/server/extension.d.ts +61 -0
- package/dist/dts/api/server/index.d.ts +1 -0
- package/dist/dts/api/session/extension.d.ts +52 -0
- package/dist/dts/api/session/index.d.ts +1 -0
- package/dist/dts/api/signal/extension.d.ts +19 -0
- package/dist/dts/api/signal/packets.d.ts +21 -0
- package/dist/dts/api/signal/signal.d.ts +33 -0
- package/dist/dts/api/table/extension.d.ts +26 -0
- package/{src/api/table/index.ts → dist/dts/api/table/index.d.ts} +0 -1
- package/dist/dts/api/table/packets.d.ts +65 -0
- package/dist/dts/api/table/table.d.ts +75 -0
- package/dist/dts/app.d.ts +49 -0
- package/dist/dts/const.d.ts +2 -0
- package/dist/dts/errors.d.ts +36 -0
- package/dist/dts/event/index.d.ts +6 -0
- package/dist/dts/helper.d.ts +10 -0
- package/dist/dts/identifier.d.ts +51 -0
- package/dist/dts/index.d.ts +36 -0
- package/{src/localization/index.ts → dist/dts/localization/index.d.ts} +0 -1
- package/dist/dts/localization/locale.d.ts +735 -0
- package/{src/localization/localization.ts → dist/dts/localization/localization.d.ts} +0 -2
- package/dist/dts/lock.d.ts +6 -0
- package/dist/dts/network/connection.d.ts +17 -0
- package/dist/dts/network/encryption.d.ts +38 -0
- package/dist/dts/network/frame-transport.d.ts +22 -0
- package/{src/network/index.ts → dist/dts/network/index.d.ts} +0 -1
- package/dist/dts/network/network.d.ts +59 -0
- package/{src/network/packet/index.ts → dist/dts/network/packet/index.d.ts} +0 -1
- package/dist/dts/network/packet/packet-types.d.ts +84 -0
- package/dist/dts/network/packet/packet.d.ts +25 -0
- package/dist/dts/network/websocket-transport.d.ts +24 -0
- package/dist/dts/omu.d.ts +61 -0
- package/dist/dts/result.d.ts +26 -0
- package/dist/dts/serialize/bytebuffer.d.ts +73 -0
- package/dist/dts/serialize/serializer.d.ts +26 -0
- package/dist/dts/token.d.ts +14 -0
- package/dist/dts/version.d.ts +2 -0
- package/dist/esm/address.js +4 -0
- package/dist/esm/api/asset/extension.js +911 -0
- package/dist/esm/api/asset/index.js +909 -0
- package/dist/esm/api/dashboard/extension.js +1362 -0
- package/dist/esm/api/dashboard/handler.js +4 -0
- package/dist/esm/api/dashboard/index.js +1361 -0
- package/dist/esm/api/dashboard/packets.js +419 -0
- package/dist/esm/api/endpoint/endpoint.js +383 -0
- package/dist/esm/api/endpoint/extension.js +1096 -0
- package/dist/esm/api/endpoint/index.js +383 -0
- package/dist/esm/api/endpoint/packets.js +799 -0
- package/dist/esm/api/extension-registry.js +23 -0
- package/dist/esm/api/extension.js +278 -0
- package/dist/esm/api/http/extension.js +1420 -0
- package/dist/esm/api/http/http.js +448 -0
- package/dist/esm/api/http/index.js +1419 -0
- package/dist/esm/api/http/websocket.js +220 -0
- package/dist/esm/api/i18n/extension.js +1339 -0
- package/dist/esm/api/i18n/index.js +1337 -0
- package/dist/esm/api/index.js +295 -0
- package/dist/esm/api/logger/extension.js +856 -0
- package/dist/esm/api/logger/index.js +852 -0
- package/dist/esm/api/logger/packets.js +673 -0
- package/dist/esm/api/permission/extension.js +535 -0
- package/dist/esm/api/permission/index.js +294 -0
- package/dist/esm/api/permission/permission.js +294 -0
- package/dist/esm/api/plugin/extension.js +531 -0
- package/dist/esm/api/plugin/index.js +529 -0
- package/dist/esm/api/plugin/package-info.js +4 -0
- package/dist/esm/api/registry/extension.js +1286 -0
- package/dist/esm/api/registry/index.js +1285 -0
- package/dist/esm/api/registry/packets.js +694 -0
- package/dist/esm/api/registry/registry.js +383 -0
- package/dist/esm/api/server/extension.js +2140 -0
- package/dist/esm/api/server/index.js +2118 -0
- package/dist/esm/api/session/extension.js +2235 -0
- package/dist/esm/api/session/index.js +2235 -0
- package/dist/esm/api/signal/extension.js +1031 -0
- package/dist/esm/api/signal/index.js +379 -0
- package/dist/esm/api/signal/packets.js +690 -0
- package/dist/esm/api/signal/signal.js +379 -0
- package/dist/esm/api/table/extension.js +1553 -0
- package/dist/esm/api/table/index.js +1552 -0
- package/dist/esm/api/table/packets.js +904 -0
- package/dist/esm/api/table/table.js +383 -0
- package/dist/esm/app.js +264 -0
- package/dist/esm/const.js +11 -0
- package/dist/esm/errors.js +548 -0
- package/dist/esm/event/index.js +22 -0
- package/dist/esm/helper.js +15 -0
- package/dist/esm/identifier.js +266 -0
- package/dist/esm/index.js +5186 -0
- package/dist/esm/localization/index.js +742 -0
- package/dist/esm/localization/locale.js +742 -0
- package/dist/esm/localization/localization.js +4 -0
- package/dist/esm/lock.js +46 -0
- package/dist/esm/network/connection.js +293 -0
- package/dist/esm/network/encryption.js +986 -0
- package/dist/esm/network/frame-transport.js +531 -0
- package/dist/esm/network/index.js +1627 -0
- package/dist/esm/network/network.js +1368 -0
- package/dist/esm/network/packet/index.js +485 -0
- package/dist/esm/network/packet/packet-types.js +486 -0
- package/dist/esm/network/packet/packet.js +126 -0
- package/dist/esm/network/websocket-transport.js +493 -0
- package/dist/esm/omu.js +4945 -0
- package/dist/esm/result.js +68 -0
- package/dist/esm/serialize/bytebuffer.js +384 -0
- package/dist/esm/serialize/index.js +471 -0
- package/dist/esm/serialize/serializer.js +97 -0
- package/dist/esm/token.js +42 -0
- package/dist/esm/version.js +11 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +4 -1
- package/CHANGELOG.md +0 -1837
- package/src/api/asset/extension.ts +0 -193
- package/src/api/asset/index.ts +0 -5
- package/src/api/dashboard/extension.ts +0 -651
- package/src/api/dashboard/index.ts +0 -38
- package/src/api/dashboard/packets.ts +0 -126
- package/src/api/endpoint/endpoint.ts +0 -56
- package/src/api/endpoint/extension.ts +0 -133
- package/src/api/endpoint/packets.ts +0 -176
- package/src/api/extension-registry.ts +0 -17
- package/src/api/extension.ts +0 -19
- package/src/api/http/extension.ts +0 -401
- package/src/api/http/http.ts +0 -79
- package/src/api/http/websocket.ts +0 -283
- package/src/api/i18n/extension.ts +0 -65
- package/src/api/logger/extension.ts +0 -75
- package/src/api/logger/index.ts +0 -4
- package/src/api/logger/packets.ts +0 -38
- package/src/api/permission/extension.ts +0 -107
- package/src/api/permission/permission.ts +0 -53
- package/src/api/plugin/extension.ts +0 -100
- package/src/api/registry/extension.ts +0 -326
- package/src/api/registry/packets.ts +0 -64
- package/src/api/server/extension.ts +0 -155
- package/src/api/server/index.ts +0 -11
- package/src/api/session/extension.ts +0 -205
- package/src/api/session/index.ts +0 -7
- package/src/api/signal/extension.ts +0 -125
- package/src/api/signal/packets.ts +0 -61
- package/src/api/signal/signal.ts +0 -67
- package/src/api/table/extension.ts +0 -554
- package/src/api/table/packets.ts +0 -252
- package/src/api/table/table.ts +0 -113
- package/src/app.ts +0 -95
- package/src/const.ts +0 -2
- package/src/errors.ts +0 -70
- package/src/event/index.ts +0 -18
- package/src/helper.ts +0 -16
- package/src/identifier.ts +0 -256
- package/src/index.ts +0 -87
- package/src/localization/locale.ts +0 -737
- package/src/lock.ts +0 -43
- package/src/network/connection.ts +0 -45
- package/src/network/encryption.ts +0 -221
- package/src/network/frame-transport.ts +0 -186
- package/src/network/network.ts +0 -322
- package/src/network/packet/packet-types.ts +0 -164
- package/src/network/packet/packet.ts +0 -55
- package/src/network/websocket-transport.ts +0 -123
- package/src/omu.ts +0 -156
- package/src/result.ts +0 -78
- package/src/serialize/bytebuffer.ts +0 -432
- package/src/serialize/serializer.ts +0 -149
- package/src/token.ts +0 -39
- package/src/version.ts +0 -2
- package/tsconfig.json +0 -18
- /package/{src/address.ts → dist/dts/address.d.ts} +0 -0
- /package/{src/api/signal/index.ts → dist/dts/api/signal/index.d.ts} +0 -0
- /package/{src/serialize/index.ts → dist/dts/serialize/index.d.ts} +0 -0
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { App, AppJson } from '../../app.js';
|
|
2
|
-
import { ByteReader, ByteWriter } from '../../serialize';
|
|
3
|
-
|
|
4
|
-
export type AppInstallResponse = {
|
|
5
|
-
accepted: boolean;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export type DragDropFile = {
|
|
9
|
-
type: 'file' | 'directory';
|
|
10
|
-
size: number;
|
|
11
|
-
name: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type DragDropPosition = {
|
|
15
|
-
x: number;
|
|
16
|
-
y: number;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export type DragEnter = {
|
|
20
|
-
type: 'enter';
|
|
21
|
-
drag_id: string;
|
|
22
|
-
files: DragDropFile[];
|
|
23
|
-
position: DragDropPosition;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export type DragOver = {
|
|
27
|
-
type: 'over';
|
|
28
|
-
drag_id: string;
|
|
29
|
-
position: DragDropPosition;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type DragDrop = {
|
|
33
|
-
type: 'drop';
|
|
34
|
-
drag_id: string;
|
|
35
|
-
position: DragDropPosition;
|
|
36
|
-
files: DragDropFile[];
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export type DragLeave = {
|
|
40
|
-
type: 'leave';
|
|
41
|
-
drag_id: string;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export type DragState = DragEnter | DragOver | DragDrop | DragLeave;
|
|
45
|
-
|
|
46
|
-
export type FileDragPacket = {
|
|
47
|
-
drag_id: string;
|
|
48
|
-
app: App;
|
|
49
|
-
state: DragState;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export type DragDropReadRequest = {
|
|
53
|
-
drag_id: string;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export type DragDropReadRequestDashboard = {
|
|
57
|
-
request_id: string;
|
|
58
|
-
drag_id: string;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type DragDropReadMeta = {
|
|
62
|
-
request_id: string;
|
|
63
|
-
drag_id: string;
|
|
64
|
-
files: readonly DragDropFile[];
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export type FileData = {
|
|
68
|
-
file: DragDropFile;
|
|
69
|
-
buffer: Uint8Array;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export class DragDropReadResponse {
|
|
73
|
-
constructor(
|
|
74
|
-
public readonly meta: DragDropReadMeta,
|
|
75
|
-
public readonly files: Record<string, FileData>,
|
|
76
|
-
public readonly version: number = 1,
|
|
77
|
-
) {}
|
|
78
|
-
|
|
79
|
-
public static serialize(data: DragDropReadResponse): Uint8Array {
|
|
80
|
-
const writer = new ByteWriter();
|
|
81
|
-
writer.writeULEB128(data.version);
|
|
82
|
-
writer.writeString(JSON.stringify(data.meta));
|
|
83
|
-
writer.writeULEB128(Object.keys(data.files).length);
|
|
84
|
-
|
|
85
|
-
for (const [key, { file, buffer }] of Object.entries(data.files)) {
|
|
86
|
-
writer.writeString(key);
|
|
87
|
-
writer.writeString(JSON.stringify(file));
|
|
88
|
-
writer.writeUint8Array(buffer);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return writer.finish();
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public static deserialize(data: Uint8Array): DragDropReadResponse {
|
|
95
|
-
const reader = ByteReader.fromUint8Array(data);
|
|
96
|
-
const version = reader.readULEB128();
|
|
97
|
-
const meta: DragDropReadMeta = JSON.parse(reader.readString());
|
|
98
|
-
const length = reader.readULEB128();
|
|
99
|
-
|
|
100
|
-
const files: Record<string, FileData> = {};
|
|
101
|
-
for (let i = 0; i < length; i++) {
|
|
102
|
-
const key = reader.readString();
|
|
103
|
-
const file: DragDropFile = JSON.parse(reader.readString());
|
|
104
|
-
const buffer = reader.readUint8Array();
|
|
105
|
-
files[key] = { file, buffer };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return new DragDropReadResponse(
|
|
109
|
-
meta,
|
|
110
|
-
files,
|
|
111
|
-
version,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export type DragDropRequest = object;
|
|
117
|
-
|
|
118
|
-
export type DragDropRequestDashboard = {
|
|
119
|
-
request_id: string;
|
|
120
|
-
app: AppJson;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
export type DragDropRequestResponse = {
|
|
124
|
-
request_id: string;
|
|
125
|
-
ok: boolean;
|
|
126
|
-
};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { Identifier, IntoId } from '../../identifier';
|
|
2
|
-
import { JsonType, type Serializable, Serializer } from '../../serialize';
|
|
3
|
-
|
|
4
|
-
export class EndpointType<Req = unknown, Res = unknown> {
|
|
5
|
-
constructor(
|
|
6
|
-
public readonly id: Identifier,
|
|
7
|
-
public readonly requestSerializer: Serializable<Req, Uint8Array>,
|
|
8
|
-
public readonly responseSerializer: Serializable<Res, Uint8Array>,
|
|
9
|
-
public readonly permissionId?: Identifier | undefined,
|
|
10
|
-
) { }
|
|
11
|
-
|
|
12
|
-
static createJson<Req, Res, ReqData extends JsonType = JsonType, ResData extends JsonType = JsonType>(
|
|
13
|
-
identifier: IntoId,
|
|
14
|
-
{
|
|
15
|
-
name,
|
|
16
|
-
requestSerializer,
|
|
17
|
-
responseSerializer,
|
|
18
|
-
permissionId,
|
|
19
|
-
}: {
|
|
20
|
-
name: string;
|
|
21
|
-
requestSerializer?: Serializable<Req, ReqData>;
|
|
22
|
-
responseSerializer?: Serializable<Res, ResData>;
|
|
23
|
-
permissionId?: IntoId;
|
|
24
|
-
},
|
|
25
|
-
): EndpointType<Req, Res> {
|
|
26
|
-
return new EndpointType<Req, Res>(
|
|
27
|
-
Identifier.from(identifier).join(name),
|
|
28
|
-
Serializer.pipe<Req, JsonType, Uint8Array>(requestSerializer ?? Serializer.noop(), Serializer.json()),
|
|
29
|
-
Serializer.pipe<Res, JsonType, Uint8Array>(responseSerializer ?? Serializer.noop(), Serializer.json()),
|
|
30
|
-
permissionId ? Identifier.from(permissionId) : undefined,
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
static createSerialized<Req, Res>(
|
|
35
|
-
identifier: IntoId,
|
|
36
|
-
{
|
|
37
|
-
name,
|
|
38
|
-
requestSerializer,
|
|
39
|
-
responseSerializer,
|
|
40
|
-
permissionId,
|
|
41
|
-
}: {
|
|
42
|
-
name: string;
|
|
43
|
-
requestSerializer: Serializable<Req, Uint8Array>;
|
|
44
|
-
responseSerializer: Serializable<Res, Uint8Array>;
|
|
45
|
-
permissionId?: IntoId;
|
|
46
|
-
},
|
|
47
|
-
): EndpointType<Req, Res> {
|
|
48
|
-
return new EndpointType<Req, Res>(
|
|
49
|
-
Identifier.from(identifier).join(name),
|
|
50
|
-
requestSerializer,
|
|
51
|
-
responseSerializer,
|
|
52
|
-
permissionId ? Identifier.from(permissionId) : undefined,
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import type { Identifier } from '../../identifier';
|
|
2
|
-
import { IdentifierMap } from '../../identifier';
|
|
3
|
-
import { PacketType } from '../../network/packet';
|
|
4
|
-
import { Omu } from '../../omu';
|
|
5
|
-
import { ExtensionType } from '../extension.js';
|
|
6
|
-
|
|
7
|
-
import { EndpointType } from './endpoint.js';
|
|
8
|
-
import { EndpointInvokedPacket, EndpointInvokePacket, EndpointRegisterPacket, EndpointResponsePacket, InvokedParams, InvokeParams, ResponseParams } from './packets.js';
|
|
9
|
-
|
|
10
|
-
export const ENDPOINT_EXTENSION_TYPE: ExtensionType<EndpointExtension> = new ExtensionType(
|
|
11
|
-
'endpoint',
|
|
12
|
-
(omu: Omu) => new EndpointExtension(omu),
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
type CallPromise = {
|
|
16
|
-
resolve: (data: Uint8Array) => void;
|
|
17
|
-
reject: (error: Error) => void;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
type EndpointHandler = {
|
|
21
|
-
type: EndpointType;
|
|
22
|
-
handler: (packet: EndpointInvokedPacket) => Promise<Uint8Array>;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export class EndpointExtension {
|
|
26
|
-
public readonly type: ExtensionType<EndpointExtension> = ENDPOINT_EXTENSION_TYPE;
|
|
27
|
-
private readonly boundEndpoints = new IdentifierMap<EndpointHandler>();
|
|
28
|
-
private readonly responsePromises: Map<number, CallPromise> = new Map();
|
|
29
|
-
private callId: number;
|
|
30
|
-
|
|
31
|
-
constructor(private readonly omu: Omu) {
|
|
32
|
-
this.callId = Math.floor((performance.timeOrigin + performance.now()) * 1000);
|
|
33
|
-
omu.network.registerPacket(
|
|
34
|
-
ENDPOINT_REGISTER_PACKET,
|
|
35
|
-
ENDPOINT_INVOKE_PACKET,
|
|
36
|
-
ENDPOINT_INVOKED_PACKET,
|
|
37
|
-
ENDPOINT_RESPONSE_PACKET,
|
|
38
|
-
);
|
|
39
|
-
omu.network.addPacketHandler(ENDPOINT_INVOKED_PACKET, (packet) => {
|
|
40
|
-
this.handleInvoked(packet);
|
|
41
|
-
});
|
|
42
|
-
omu.network.addPacketHandler(ENDPOINT_RESPONSE_PACKET, (packet) => {
|
|
43
|
-
const promise = this.responsePromises.get(packet.params.key);
|
|
44
|
-
if (!promise) {
|
|
45
|
-
throw new Error(
|
|
46
|
-
`Received response for unknown key ${packet.params.key} (${packet.params.id.key()})`,
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
this.responsePromises.delete(packet.params.key);
|
|
50
|
-
if (packet.params.error) {
|
|
51
|
-
promise.reject(new Error(packet.params.error));
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
promise.resolve(packet.buffer);
|
|
55
|
-
});
|
|
56
|
-
omu.network.addTask(() => this.onTask());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
private async handleInvoked(packet: EndpointInvokedPacket) {
|
|
60
|
-
const endpoint = this.boundEndpoints.get(packet.params.id);
|
|
61
|
-
if (!endpoint) {
|
|
62
|
-
throw new Error(`Received invocation for unknown endpoint ${packet.params.id.key()} (${packet.params.key})`);
|
|
63
|
-
}
|
|
64
|
-
try {
|
|
65
|
-
const result = await endpoint.handler(packet);
|
|
66
|
-
this.omu.send(ENDPOINT_RESPONSE_PACKET, new EndpointResponsePacket(
|
|
67
|
-
new ResponseParams(packet.params.id, packet.params.key, null),
|
|
68
|
-
result,
|
|
69
|
-
));
|
|
70
|
-
} catch (error) {
|
|
71
|
-
this.omu.send(ENDPOINT_RESPONSE_PACKET, new EndpointResponsePacket(
|
|
72
|
-
new ResponseParams(packet.params.id, packet.params.key, JSON.stringify(error)),
|
|
73
|
-
new Uint8Array(),
|
|
74
|
-
));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private onTask(): void {
|
|
79
|
-
const endpoints = new IdentifierMap<Identifier | undefined>();
|
|
80
|
-
for (const [key, endpoint] of this.boundEndpoints) {
|
|
81
|
-
endpoints.set(key, endpoint.type.permissionId);
|
|
82
|
-
}
|
|
83
|
-
const packet = new EndpointRegisterPacket(endpoints);
|
|
84
|
-
this.omu.send(ENDPOINT_REGISTER_PACKET, packet);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public bind<Req, Res>(
|
|
88
|
-
type: EndpointType<Req, Res>,
|
|
89
|
-
handler: (data: Req, params: InvokedParams) => Promise<Res>,
|
|
90
|
-
): void {
|
|
91
|
-
if (this.omu.running) {
|
|
92
|
-
throw new Error('Cannot register endpoints after the client has started');
|
|
93
|
-
}
|
|
94
|
-
if (this.boundEndpoints.has(type.id)) {
|
|
95
|
-
throw new Error(`Endpoint with id ${type.id.key()} is already registered`);
|
|
96
|
-
}
|
|
97
|
-
this.boundEndpoints.set(type.id, { type, handler: async ({ buffer, params }) => {
|
|
98
|
-
const req = type.requestSerializer.deserialize(buffer);
|
|
99
|
-
const result = await handler(req, params);
|
|
100
|
-
return type.responseSerializer.serialize(result);
|
|
101
|
-
} });
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public async call<Req, Res>(endpoint: EndpointType<Req, Res>, data: Req): Promise<Res> {
|
|
105
|
-
const key = this.callId++;
|
|
106
|
-
const promise = new Promise<Uint8Array>((resolve, reject) => {
|
|
107
|
-
this.responsePromises.set(key, { resolve, reject });
|
|
108
|
-
});
|
|
109
|
-
this.omu.send(ENDPOINT_INVOKE_PACKET, new EndpointInvokePacket(
|
|
110
|
-
new InvokeParams(endpoint.id, key),
|
|
111
|
-
endpoint.requestSerializer.serialize(data),
|
|
112
|
-
));
|
|
113
|
-
const response = await promise;
|
|
114
|
-
return endpoint.responseSerializer.deserialize(response);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const ENDPOINT_REGISTER_PACKET = PacketType.createSerialized<EndpointRegisterPacket>(ENDPOINT_EXTENSION_TYPE, {
|
|
119
|
-
name: 'register',
|
|
120
|
-
serializer: EndpointRegisterPacket,
|
|
121
|
-
});
|
|
122
|
-
const ENDPOINT_INVOKE_PACKET = PacketType.createSerialized<EndpointInvokePacket>(ENDPOINT_EXTENSION_TYPE, {
|
|
123
|
-
name: 'invoke',
|
|
124
|
-
serializer: EndpointInvokePacket,
|
|
125
|
-
});
|
|
126
|
-
const ENDPOINT_INVOKED_PACKET = PacketType.createSerialized<EndpointInvokedPacket>(ENDPOINT_EXTENSION_TYPE, {
|
|
127
|
-
name: 'invoked',
|
|
128
|
-
serializer: EndpointInvokedPacket,
|
|
129
|
-
});
|
|
130
|
-
const ENDPOINT_RESPONSE_PACKET = PacketType.createSerialized<EndpointResponsePacket>(ENDPOINT_EXTENSION_TYPE, {
|
|
131
|
-
name: 'response',
|
|
132
|
-
serializer: EndpointResponsePacket,
|
|
133
|
-
});
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { Identifier, IdentifierMap } from '../../identifier';
|
|
2
|
-
import { ByteReader, ByteWriter } from '../../serialize';
|
|
3
|
-
|
|
4
|
-
export class EndpointRegisterPacket {
|
|
5
|
-
constructor(
|
|
6
|
-
public endpoints: IdentifierMap<Identifier | undefined>,
|
|
7
|
-
) { }
|
|
8
|
-
|
|
9
|
-
public static serialize(item: EndpointRegisterPacket): Uint8Array {
|
|
10
|
-
const writer = new ByteWriter();
|
|
11
|
-
writer.writeULEB128(item.endpoints.size);
|
|
12
|
-
for (const [key, value] of item.endpoints) {
|
|
13
|
-
writer.writeString(key.key());
|
|
14
|
-
writer.writeString(value?.key() ?? '');
|
|
15
|
-
}
|
|
16
|
-
return writer.finish();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public static deserialize(item: Uint8Array): EndpointRegisterPacket {
|
|
20
|
-
const reader = ByteReader.fromUint8Array(item);
|
|
21
|
-
const count = reader.readULEB128();
|
|
22
|
-
const endpoints = new IdentifierMap<Identifier | undefined>();
|
|
23
|
-
for (let i = 0; i < count; i++) {
|
|
24
|
-
const key = Identifier.fromKey(reader.readString());
|
|
25
|
-
const value = Identifier.fromKey(reader.readString());
|
|
26
|
-
endpoints.set(key, value);
|
|
27
|
-
}
|
|
28
|
-
return new EndpointRegisterPacket(endpoints);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class InvokedParams {
|
|
33
|
-
constructor(
|
|
34
|
-
public readonly id: Identifier,
|
|
35
|
-
public readonly caller: Identifier,
|
|
36
|
-
public readonly key: number,
|
|
37
|
-
) { }
|
|
38
|
-
|
|
39
|
-
public static serialize(item: InvokedParams): string {
|
|
40
|
-
return JSON.stringify({
|
|
41
|
-
id: item.id.key(),
|
|
42
|
-
caller: item.caller.key(),
|
|
43
|
-
key: item.key,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public static deserialize(item: string): InvokedParams {
|
|
48
|
-
const data: {
|
|
49
|
-
id: string;
|
|
50
|
-
caller: string;
|
|
51
|
-
key: number;
|
|
52
|
-
} = JSON.parse(item);
|
|
53
|
-
return new InvokedParams(
|
|
54
|
-
Identifier.fromKey(data.id),
|
|
55
|
-
Identifier.fromKey(data.caller),
|
|
56
|
-
data.key,
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export class EndpointInvokedPacket {
|
|
62
|
-
constructor(
|
|
63
|
-
public params: InvokedParams,
|
|
64
|
-
public buffer: Uint8Array,
|
|
65
|
-
) { }
|
|
66
|
-
|
|
67
|
-
public static serialize(item: EndpointInvokedPacket): Uint8Array {
|
|
68
|
-
const writer = new ByteWriter();
|
|
69
|
-
writer.writeString(InvokedParams.serialize(item.params));
|
|
70
|
-
writer.writeUint8Array(item.buffer);
|
|
71
|
-
return writer.finish();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
public static deserialize(item: Uint8Array): EndpointInvokedPacket {
|
|
75
|
-
const reader = ByteReader.fromUint8Array(item);
|
|
76
|
-
const params = InvokedParams.deserialize(reader.readString());
|
|
77
|
-
const buffer = reader.readUint8Array();
|
|
78
|
-
return new EndpointInvokedPacket(params, buffer);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export class InvokeParams {
|
|
83
|
-
constructor(
|
|
84
|
-
public readonly id: Identifier,
|
|
85
|
-
public readonly key: number,
|
|
86
|
-
) { }
|
|
87
|
-
|
|
88
|
-
public static serialize(item: InvokeParams): string {
|
|
89
|
-
return JSON.stringify({
|
|
90
|
-
id: item.id.key(),
|
|
91
|
-
key: item.key,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
public static deserialize(item: string): InvokeParams {
|
|
96
|
-
const data: {
|
|
97
|
-
id: string;
|
|
98
|
-
key: number;
|
|
99
|
-
} = JSON.parse(item);
|
|
100
|
-
return new InvokeParams(
|
|
101
|
-
Identifier.fromKey(data.id),
|
|
102
|
-
data.key,
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
export class EndpointInvokePacket {
|
|
108
|
-
constructor(
|
|
109
|
-
public params: InvokeParams,
|
|
110
|
-
public buffer: Uint8Array,
|
|
111
|
-
) { }
|
|
112
|
-
|
|
113
|
-
public static serialize(item: EndpointInvokePacket): Uint8Array {
|
|
114
|
-
const writer = new ByteWriter();
|
|
115
|
-
writer.writeString(InvokeParams.serialize(item.params));
|
|
116
|
-
writer.writeUint8Array(item.buffer);
|
|
117
|
-
return writer.finish();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
public static deserialize(item: Uint8Array): EndpointInvokePacket {
|
|
121
|
-
const reader = ByteReader.fromUint8Array(item);
|
|
122
|
-
const params = InvokeParams.deserialize(reader.readString());
|
|
123
|
-
const buffer = reader.readUint8Array();
|
|
124
|
-
return new EndpointInvokePacket(params, buffer);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export class ResponseParams {
|
|
129
|
-
constructor(
|
|
130
|
-
public readonly id: Identifier,
|
|
131
|
-
public readonly key: number,
|
|
132
|
-
public readonly error: string | null,
|
|
133
|
-
) { }
|
|
134
|
-
|
|
135
|
-
public static serialize(item: ResponseParams): string {
|
|
136
|
-
return JSON.stringify({
|
|
137
|
-
id: item.id.key(),
|
|
138
|
-
key: item.key,
|
|
139
|
-
error: item.error,
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public static deserialize(item: string): ResponseParams {
|
|
144
|
-
const data: {
|
|
145
|
-
id: string;
|
|
146
|
-
key: number;
|
|
147
|
-
error: string | null;
|
|
148
|
-
} = JSON.parse(item);
|
|
149
|
-
return new ResponseParams(
|
|
150
|
-
Identifier.fromKey(data.id),
|
|
151
|
-
data.key,
|
|
152
|
-
data.error,
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
export class EndpointResponsePacket {
|
|
158
|
-
constructor(
|
|
159
|
-
public params: ResponseParams,
|
|
160
|
-
public buffer: Uint8Array,
|
|
161
|
-
) { }
|
|
162
|
-
|
|
163
|
-
public static serialize(item: EndpointResponsePacket): Uint8Array {
|
|
164
|
-
const writer = new ByteWriter();
|
|
165
|
-
writer.writeString(ResponseParams.serialize(item.params));
|
|
166
|
-
writer.writeUint8Array(item.buffer);
|
|
167
|
-
return writer.finish();
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
public static deserialize(item: Uint8Array): EndpointResponsePacket {
|
|
171
|
-
const reader = ByteReader.fromUint8Array(item);
|
|
172
|
-
const params = ResponseParams.deserialize(reader.readString());
|
|
173
|
-
const buffer = reader.readUint8Array();
|
|
174
|
-
return new EndpointResponsePacket(params, buffer);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Omu } from '../omu.js';
|
|
2
|
-
import type { Extension, ExtensionType } from './extension.js';
|
|
3
|
-
|
|
4
|
-
export class ExtensionRegistry {
|
|
5
|
-
private readonly extensionMap: Map<string, Extension> = new Map();
|
|
6
|
-
|
|
7
|
-
constructor(private readonly omu: Omu) {}
|
|
8
|
-
|
|
9
|
-
register<T extends Extension>(type: ExtensionType<T>): T {
|
|
10
|
-
if (this.extensionMap.has(type.name)) {
|
|
11
|
-
throw new Error(`Extension type ${type.name} already registered`);
|
|
12
|
-
}
|
|
13
|
-
const extension = type.create(this.omu);
|
|
14
|
-
this.extensionMap.set(type.name, extension);
|
|
15
|
-
return extension;
|
|
16
|
-
}
|
|
17
|
-
}
|
package/src/api/extension.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Identifier } from '../identifier';
|
|
2
|
-
import { Omu } from '../omu';
|
|
3
|
-
|
|
4
|
-
export interface Extension {
|
|
5
|
-
readonly type: ExtensionType;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export class ExtensionType<T extends Extension = Extension> extends Identifier {
|
|
9
|
-
constructor(
|
|
10
|
-
public readonly name: string,
|
|
11
|
-
public readonly create: (omu: Omu) => T,
|
|
12
|
-
) {
|
|
13
|
-
super('ext', name);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public key(): string {
|
|
17
|
-
return new Identifier('ext', this.name).key();
|
|
18
|
-
}
|
|
19
|
-
}
|