@omujs/omu 0.9.48 → 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 -1845
- 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,283 +0,0 @@
|
|
|
1
|
-
import { textDecoder } from '../../const';
|
|
2
|
-
import { DataChunk, RequestHandle } from './http';
|
|
3
|
-
|
|
4
|
-
export type WSData = string | ArrayBufferLike | Blob | ArrayBufferView;
|
|
5
|
-
|
|
6
|
-
export const WSMsgType = {
|
|
7
|
-
CONTINUATION: 0x0,
|
|
8
|
-
TEXT: 0x1,
|
|
9
|
-
BINARY: 0x2,
|
|
10
|
-
PING: 0x9,
|
|
11
|
-
PONG: 0xA,
|
|
12
|
-
CLOSE: 0x8,
|
|
13
|
-
} as const;
|
|
14
|
-
|
|
15
|
-
export const WSCloseCode = {
|
|
16
|
-
OK: 1000,
|
|
17
|
-
GOING_AWAY: 1001,
|
|
18
|
-
PROTOCOL_ERROR: 1002,
|
|
19
|
-
UNSUPPORTED_DATA: 1003,
|
|
20
|
-
ABNORMAL_CLOSURE: 1006,
|
|
21
|
-
INVALID_TEXT: 1007,
|
|
22
|
-
POLICY_VIOLATION: 1008,
|
|
23
|
-
MESSAGE_TOO_BIG: 1009,
|
|
24
|
-
MANDATORY_EXTENSION: 1010,
|
|
25
|
-
INTERNAL_ERROR: 1011,
|
|
26
|
-
SERVICE_RESTART: 1012,
|
|
27
|
-
TRY_AGAIN_LATER: 1013,
|
|
28
|
-
BAD_GATEWAY: 1014,
|
|
29
|
-
} as const;
|
|
30
|
-
|
|
31
|
-
export type WebSocketOpen = RequestHandle & {
|
|
32
|
-
url: string;
|
|
33
|
-
protocol?: string | null;
|
|
34
|
-
};
|
|
35
|
-
export type WebSocketClose = RequestHandle & {
|
|
36
|
-
code?: number;
|
|
37
|
-
reason?: string | null;
|
|
38
|
-
};
|
|
39
|
-
export type WebSocketError = RequestHandle & {
|
|
40
|
-
type: 'ConnectionRefused';
|
|
41
|
-
reason?: string | null;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export type EventListenerMap<T> = {
|
|
45
|
-
[K in keyof T]: ((event: T[K]) => unknown)[]
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export type WSMsg = {
|
|
49
|
-
type: 'open';
|
|
50
|
-
data: WebSocketOpen;
|
|
51
|
-
} | {
|
|
52
|
-
type: 'error';
|
|
53
|
-
data: WebSocketError;
|
|
54
|
-
} | {
|
|
55
|
-
type: 'text';
|
|
56
|
-
data: string;
|
|
57
|
-
} | {
|
|
58
|
-
type: 'binary';
|
|
59
|
-
data: Uint8Array;
|
|
60
|
-
} | {
|
|
61
|
-
type: 'close';
|
|
62
|
-
data: WebSocketClose;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export type WebSocketHandle = {
|
|
66
|
-
ws: OmuWS;
|
|
67
|
-
open: (response: WebSocketOpen) => void;
|
|
68
|
-
dispatch: (data: DataChunk<WSDataMeta>) => void;
|
|
69
|
-
close: (response: WebSocketClose) => void;
|
|
70
|
-
error: (response: WebSocketError) => void;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export type WSDataMeta = {
|
|
74
|
-
id: string;
|
|
75
|
-
type:
|
|
76
|
-
| typeof WSMsgType.TEXT
|
|
77
|
-
| typeof WSMsgType.BINARY
|
|
78
|
-
| typeof WSMsgType.PING
|
|
79
|
-
| typeof WSMsgType.PONG;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export type WSHandleState = {
|
|
83
|
-
type: 'created' | 'receiving';
|
|
84
|
-
handle: WebSocketHandle;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
type WSOptions = {
|
|
88
|
-
addQueue: (data: WSData) => void;
|
|
89
|
-
dispose(options: {
|
|
90
|
-
code?: number;
|
|
91
|
-
reason?: string;
|
|
92
|
-
}): void;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export class OmuWS {
|
|
96
|
-
constructor(
|
|
97
|
-
protected readonly addQueue: (data: WSData) => void,
|
|
98
|
-
protected readonly dispose: (options: { code?: number; reason?: string }) => void,
|
|
99
|
-
protected readonly receiveQueue: WSMsg[] = [],
|
|
100
|
-
private receiveResolve: (() => void) | undefined = undefined,
|
|
101
|
-
private status: 'connecting' | 'open' | 'closed' = 'connecting',
|
|
102
|
-
) { }
|
|
103
|
-
|
|
104
|
-
public toWebSocket(): OmuWebSocket {
|
|
105
|
-
return new OmuWebSocket(this);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public static create(options: WSOptions): WebSocketHandle {
|
|
109
|
-
const ws = new OmuWS(options.addQueue, options.dispose);
|
|
110
|
-
return {
|
|
111
|
-
ws,
|
|
112
|
-
open: (response) => {
|
|
113
|
-
ws.receiveQueue.push({
|
|
114
|
-
type: 'open',
|
|
115
|
-
data: response,
|
|
116
|
-
});
|
|
117
|
-
ws.receiveResolve?.();
|
|
118
|
-
},
|
|
119
|
-
dispatch: (data: DataChunk<WSDataMeta>): void => {
|
|
120
|
-
if (data.meta.type === WSMsgType.BINARY) {
|
|
121
|
-
ws.receiveQueue.push({
|
|
122
|
-
type: 'binary',
|
|
123
|
-
data: data.body,
|
|
124
|
-
});
|
|
125
|
-
} else {
|
|
126
|
-
ws.receiveQueue.push({
|
|
127
|
-
type: 'text',
|
|
128
|
-
data: textDecoder.decode(data.body),
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
ws.receiveResolve?.();
|
|
132
|
-
},
|
|
133
|
-
close: (response) => {
|
|
134
|
-
ws.receiveQueue.push({
|
|
135
|
-
type: 'close',
|
|
136
|
-
data: response,
|
|
137
|
-
});
|
|
138
|
-
ws.receiveResolve?.();
|
|
139
|
-
},
|
|
140
|
-
error: (response) => {
|
|
141
|
-
ws.receiveQueue.push({
|
|
142
|
-
type: 'error',
|
|
143
|
-
data: response,
|
|
144
|
-
});
|
|
145
|
-
ws.receiveResolve?.();
|
|
146
|
-
},
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
public send(data: WSData): void {
|
|
151
|
-
this.addQueue(data);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
public close(code?: number, reason?: string): void {
|
|
155
|
-
this.dispose({ code, reason });
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
public async receive(): Promise<WSMsg> {
|
|
159
|
-
if (this.receiveResolve) {
|
|
160
|
-
throw new Error('Already receiving from another async call');
|
|
161
|
-
}
|
|
162
|
-
while (this.status !== 'closed') {
|
|
163
|
-
const data = this.receiveQueue.shift();
|
|
164
|
-
if (!data) {
|
|
165
|
-
await new Promise<void>((resolve) => this.receiveResolve = resolve);
|
|
166
|
-
continue;
|
|
167
|
-
}
|
|
168
|
-
if (data.type === 'close') {
|
|
169
|
-
this.status = 'closed';
|
|
170
|
-
}
|
|
171
|
-
if (data.type === 'open') {
|
|
172
|
-
this.status = 'open';
|
|
173
|
-
}
|
|
174
|
-
this.receiveResolve = undefined;
|
|
175
|
-
return data;
|
|
176
|
-
}
|
|
177
|
-
throw new Error('Receiving on already closed socket');
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
protocol: string = '';
|
|
181
|
-
url: string = '';
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export class OmuWebSocket implements WebSocket {
|
|
185
|
-
public CONNECTING = 0 as const;
|
|
186
|
-
public OPEN = 1 as const;
|
|
187
|
-
public CLOSING = 2 as const;
|
|
188
|
-
public CLOSED = 3 as const;
|
|
189
|
-
|
|
190
|
-
binaryType: BinaryType = 'blob';
|
|
191
|
-
bufferedAmount: number = 0;
|
|
192
|
-
extensions: string = '';
|
|
193
|
-
onclose: ((this: WebSocket, ev: CloseEvent) => unknown) | null = null;
|
|
194
|
-
onerror: ((this: WebSocket, ev: Event) => unknown) | null = null;
|
|
195
|
-
onmessage: ((this: WebSocket, ev: MessageEvent) => unknown) | null = null;
|
|
196
|
-
onopen: ((this: WebSocket, ev: Event) => unknown) | null = null;
|
|
197
|
-
protocol: string = '';
|
|
198
|
-
readyState: 0 | 2 | 1 | 3 = 0;
|
|
199
|
-
url: string = '';
|
|
200
|
-
|
|
201
|
-
constructor(
|
|
202
|
-
protected readonly ws: OmuWS,
|
|
203
|
-
private readonly eventListeners: EventListenerMap<WebSocketEventMap> = {
|
|
204
|
-
close: [],
|
|
205
|
-
error: [],
|
|
206
|
-
message: [],
|
|
207
|
-
open: [],
|
|
208
|
-
},
|
|
209
|
-
) {
|
|
210
|
-
this.receiveLoop();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
private async receiveLoop() {
|
|
214
|
-
while (this.readyState === WebSocket.CLOSED) {
|
|
215
|
-
const { data, type } = await this.ws.receive();
|
|
216
|
-
switch (type) {
|
|
217
|
-
case 'close':
|
|
218
|
-
this.dispatchEvent(new CloseEvent('close', {
|
|
219
|
-
code: data.code,
|
|
220
|
-
reason: data.reason ?? undefined,
|
|
221
|
-
}));
|
|
222
|
-
break;
|
|
223
|
-
case 'open':
|
|
224
|
-
this.url = data.url;
|
|
225
|
-
this.protocol = data.protocol ?? '';
|
|
226
|
-
this.dispatchEvent(new Event('open', {}));
|
|
227
|
-
break;
|
|
228
|
-
case 'error':
|
|
229
|
-
this.dispatchEvent(new ErrorEvent('error', {
|
|
230
|
-
error: data.type,
|
|
231
|
-
message: data.reason ?? undefined,
|
|
232
|
-
}));
|
|
233
|
-
break;
|
|
234
|
-
case 'text':
|
|
235
|
-
case 'binary':
|
|
236
|
-
this.dispatchEvent(new MessageEvent('message', {
|
|
237
|
-
data,
|
|
238
|
-
}));
|
|
239
|
-
break;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
dispatchEvent(event: Event): boolean {
|
|
245
|
-
const listeners = this.eventListeners[event.type];
|
|
246
|
-
for (const listener of listeners) {
|
|
247
|
-
listener(event);
|
|
248
|
-
}
|
|
249
|
-
if (event.type === 'open') {
|
|
250
|
-
this.onopen?.(event as MessageEvent);
|
|
251
|
-
}
|
|
252
|
-
if (event.type === 'error') {
|
|
253
|
-
this.onerror?.(event);
|
|
254
|
-
}
|
|
255
|
-
if (event.type === 'message') {
|
|
256
|
-
this.onmessage?.(event as MessageEvent);
|
|
257
|
-
}
|
|
258
|
-
if (event.type === 'close') {
|
|
259
|
-
this.onclose?.(event as CloseEvent);
|
|
260
|
-
}
|
|
261
|
-
return true;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {
|
|
265
|
-
this.ws.send(data);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
close(code?: number, reason?: string): void {
|
|
269
|
-
this.ws.close(code, reason);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
addEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => unknown): void {
|
|
273
|
-
this.eventListeners[type].push(listener);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
removeEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => unknown): void {
|
|
277
|
-
const listeners = this.eventListeners[type];
|
|
278
|
-
const index = listeners.indexOf(listener);
|
|
279
|
-
if (index !== -1) {
|
|
280
|
-
listeners.splice(index, 1);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { Identifier } from '../../identifier';
|
|
2
|
-
import type { Locale } from '../../localization/locale.js';
|
|
3
|
-
import type { LocalizedText } from '../../localization/localization.js';
|
|
4
|
-
import { Omu } from '../../omu';
|
|
5
|
-
import type { Extension } from '../extension.js';
|
|
6
|
-
import { ExtensionType } from '../extension.js';
|
|
7
|
-
import { RegistryType, type Registry } from '../registry';
|
|
8
|
-
|
|
9
|
-
export const I18N_EXTENSION_TYPE: ExtensionType<I18nExtension> = new ExtensionType('i18n', (client) => new I18nExtension(client));
|
|
10
|
-
|
|
11
|
-
export const I18N_SET_LOCALES_PERMISSION_ID: Identifier = I18N_EXTENSION_TYPE.join('locales', 'set');
|
|
12
|
-
export const I18N_GET_LOCALES_PERMISSION_ID: Identifier = I18N_EXTENSION_TYPE.join('locales', 'get');
|
|
13
|
-
const I18N_LOCALES_REGISTRY_TYPE = RegistryType.createJson<Locale[]>(I18N_EXTENSION_TYPE, {
|
|
14
|
-
name: 'locales',
|
|
15
|
-
defaultValue: typeof window === 'undefined' ? [] : window.navigator.languages as Locale[],
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
export class I18nExtension implements Extension {
|
|
19
|
-
public readonly type: ExtensionType<I18nExtension> = I18N_EXTENSION_TYPE;
|
|
20
|
-
public readonly localesRegistry: Registry<Locale[]>;
|
|
21
|
-
|
|
22
|
-
constructor(private readonly omu: Omu) {
|
|
23
|
-
this.localesRegistry = omu.registries.get(I18N_LOCALES_REGISTRY_TYPE);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public translate(localizedText: LocalizedText): string {
|
|
27
|
-
const locales = this.localesRegistry.value;
|
|
28
|
-
if (typeof localizedText === 'string') {
|
|
29
|
-
return localizedText;
|
|
30
|
-
}
|
|
31
|
-
const translation = this.selectBestTranslation(locales, localizedText);
|
|
32
|
-
if (!translation) {
|
|
33
|
-
return Object.values(localizedText)[0];
|
|
34
|
-
}
|
|
35
|
-
return translation;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public selectBestTranslation(
|
|
39
|
-
locales: readonly Locale[],
|
|
40
|
-
localizedText: LocalizedText,
|
|
41
|
-
): string | undefined {
|
|
42
|
-
if (typeof localizedText === 'string') {
|
|
43
|
-
return localizedText;
|
|
44
|
-
}
|
|
45
|
-
const translations = localizedText;
|
|
46
|
-
const transformedLocales = locales.reduce((acc, l) => {
|
|
47
|
-
const parts = l.split('-');
|
|
48
|
-
for (let i = parts.length; i > 0; i--) {
|
|
49
|
-
acc.push(parts.slice(0, i).join('-'));
|
|
50
|
-
}
|
|
51
|
-
return acc;
|
|
52
|
-
}, [] as string[]);
|
|
53
|
-
for (const l of transformedLocales) {
|
|
54
|
-
const translation = translations[l];
|
|
55
|
-
if (translation) {
|
|
56
|
-
return translation;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return undefined;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public setLocale(locale: Locale[]): Promise<void> {
|
|
63
|
-
return this.localesRegistry.set(locale);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type { Unlisten } from '../../event';
|
|
2
|
-
import { Identifier, IdentifierMap } from '../../identifier';
|
|
3
|
-
import { PacketType } from '../../network/packet/packet.js';
|
|
4
|
-
import { Omu } from '../../omu';
|
|
5
|
-
import { ExtensionType } from '../extension.js';
|
|
6
|
-
|
|
7
|
-
import type { LogMessage } from './packets.js';
|
|
8
|
-
import { LogPacket, LogType } from './packets.js';
|
|
9
|
-
|
|
10
|
-
export const LOGGER_EXTENSION_TYPE: ExtensionType<LoggerExtension> = new ExtensionType(
|
|
11
|
-
'logger',
|
|
12
|
-
(client) => new LoggerExtension(client),
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
export const LOGGER_LOG_PERMISSION_ID: Identifier = LOGGER_EXTENSION_TYPE.join('log');
|
|
16
|
-
|
|
17
|
-
export const LOGGER_LOG_PACKET: PacketType<LogPacket> = PacketType.createSerialized<LogPacket>(LOGGER_EXTENSION_TYPE, {
|
|
18
|
-
name: 'log',
|
|
19
|
-
serializer: LogPacket,
|
|
20
|
-
});
|
|
21
|
-
export const LOGGER_LISTEN_PACKET: PacketType<Identifier> = PacketType.createJson<Identifier>(LOGGER_EXTENSION_TYPE, {
|
|
22
|
-
name: 'listen',
|
|
23
|
-
serializer: Identifier,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
type LogListener = (message: LogMessage) => void;
|
|
27
|
-
export class LoggerExtension {
|
|
28
|
-
public readonly type: ExtensionType<LoggerExtension> = LOGGER_EXTENSION_TYPE;
|
|
29
|
-
private readonly listeners: IdentifierMap<LogListener[]> = new IdentifierMap();
|
|
30
|
-
|
|
31
|
-
constructor(private readonly omu: Omu) {
|
|
32
|
-
omu.network.registerPacket(LOGGER_LOG_PACKET, LOGGER_LISTEN_PACKET);
|
|
33
|
-
omu.network.addPacketHandler(LOGGER_LOG_PACKET, (packet) => {
|
|
34
|
-
const listener = this.listeners.get(packet.id);
|
|
35
|
-
if (listener) {
|
|
36
|
-
listener.forEach((it) => it(packet.message));
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public log(message: LogMessage): void {
|
|
42
|
-
const packet = new LogPacket(this.omu.app.id, message);
|
|
43
|
-
this.omu.send(LOGGER_LOG_PACKET, packet);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public error(text: string): void {
|
|
47
|
-
this.log({ type: LogType.ERROR, text });
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public warning(text: string): void {
|
|
51
|
-
this.log({ type: LogType.WARNING, text });
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public info(text: string): void {
|
|
55
|
-
this.log({ type: LogType.INFO, text });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public debug(text: string): void {
|
|
59
|
-
this.log({ type: LogType.DEBUG, text });
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public listen(id: Identifier, listener: LogListener): Unlisten {
|
|
63
|
-
if (!this.listeners.has(id)) {
|
|
64
|
-
this.omu.onReady(() => {
|
|
65
|
-
this.omu.send(LOGGER_LISTEN_PACKET, id);
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
const list = this.listeners.get(id) ?? [];
|
|
69
|
-
list.push(listener);
|
|
70
|
-
this.listeners.set(id, list);
|
|
71
|
-
return () => {
|
|
72
|
-
list.splice(list.indexOf(listener), 1);
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
}
|
package/src/api/logger/index.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Identifier } from '../../identifier';
|
|
2
|
-
import { ByteReader, ByteWriter } from '../../serialize';
|
|
3
|
-
|
|
4
|
-
export enum LogType {
|
|
5
|
-
ERROR = 'error',
|
|
6
|
-
WARNING = 'warning',
|
|
7
|
-
INFO = 'info',
|
|
8
|
-
DEBUG = 'debug',
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type LogMessage = {
|
|
12
|
-
type: LogType;
|
|
13
|
-
text: string;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export class LogPacket {
|
|
17
|
-
constructor(
|
|
18
|
-
public readonly id: Identifier,
|
|
19
|
-
public readonly message: LogMessage,
|
|
20
|
-
) {}
|
|
21
|
-
|
|
22
|
-
public static serialize(item: LogPacket): Uint8Array {
|
|
23
|
-
const writer = new ByteWriter();
|
|
24
|
-
writer.writeString(item.id.key());
|
|
25
|
-
writer.writeString(item.message.type);
|
|
26
|
-
writer.writeString(item.message.text);
|
|
27
|
-
return writer.finish();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public static deserialize(item: Uint8Array): LogPacket {
|
|
31
|
-
const reader = ByteReader.fromUint8Array(item);
|
|
32
|
-
const id = reader.readString();
|
|
33
|
-
const type = reader.readString() as LogType;
|
|
34
|
-
const text = reader.readString();
|
|
35
|
-
const message = { type, text };
|
|
36
|
-
return new LogPacket(Identifier.fromKey(id), message);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { Identifier, IdentifierMap, IdentifierSet, IntoId } from '../../identifier';
|
|
2
|
-
import { PacketType } from '../../network/packet/packet.js';
|
|
3
|
-
import { Omu } from '../../omu';
|
|
4
|
-
import { Serializer } from '../../serialize';
|
|
5
|
-
import { EndpointType } from '../endpoint/endpoint.js';
|
|
6
|
-
import { ExtensionType } from '../extension.js';
|
|
7
|
-
|
|
8
|
-
import { PermissionType } from './permission.js';
|
|
9
|
-
|
|
10
|
-
export const PERMISSION_EXTENSION_TYPE: ExtensionType<PermissionExtension> = new ExtensionType(
|
|
11
|
-
'permission',
|
|
12
|
-
(client) => new PermissionExtension(client),
|
|
13
|
-
);
|
|
14
|
-
const PERMISSION_REGISTER_PACKET = PacketType.createJson<PermissionType[]>(
|
|
15
|
-
PERMISSION_EXTENSION_TYPE,
|
|
16
|
-
{
|
|
17
|
-
name: 'register',
|
|
18
|
-
serializer: Serializer.of(PermissionType).toArray(),
|
|
19
|
-
},
|
|
20
|
-
);
|
|
21
|
-
const PERMISSION_REQUIRE_PACKET = PacketType.createJson<Identifier[]>(PERMISSION_EXTENSION_TYPE, {
|
|
22
|
-
name: 'require',
|
|
23
|
-
serializer: Serializer.of(Identifier).toArray(),
|
|
24
|
-
});
|
|
25
|
-
const PERMISSION_REQUEST_ENDPOINT = EndpointType.createJson<Identifier[], void>(
|
|
26
|
-
PERMISSION_EXTENSION_TYPE,
|
|
27
|
-
{
|
|
28
|
-
name: 'request',
|
|
29
|
-
requestSerializer: Serializer.of(Identifier).toArray(),
|
|
30
|
-
},
|
|
31
|
-
);
|
|
32
|
-
const PERMISSION_GRANT_PACKET = PacketType.createJson<PermissionType[]>(PERMISSION_EXTENSION_TYPE, {
|
|
33
|
-
name: 'grant',
|
|
34
|
-
serializer: Serializer.of(PermissionType).toArray(),
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
export class PermissionExtension {
|
|
38
|
-
public readonly type: ExtensionType<PermissionExtension> = PERMISSION_EXTENSION_TYPE;
|
|
39
|
-
private permissions = new IdentifierMap<PermissionType>();
|
|
40
|
-
private readonly registeredPermissions = new IdentifierMap<PermissionType>();
|
|
41
|
-
private readonly requiredPermissions = new IdentifierSet();
|
|
42
|
-
|
|
43
|
-
constructor(private readonly omu: Omu) {
|
|
44
|
-
omu.network.registerPacket(
|
|
45
|
-
PERMISSION_REGISTER_PACKET,
|
|
46
|
-
PERMISSION_REQUIRE_PACKET,
|
|
47
|
-
PERMISSION_GRANT_PACKET,
|
|
48
|
-
);
|
|
49
|
-
omu.network.addPacketHandler(PERMISSION_GRANT_PACKET, (permissions) => {
|
|
50
|
-
for (const permission of permissions) {
|
|
51
|
-
this.permissions.set(permission.id, permission);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
omu.network.addTask(() => this.onTask());
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private onTask(): void {
|
|
58
|
-
if (this.omu.app.type === 'remote') {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (this.registeredPermissions.size > 0) {
|
|
62
|
-
this.omu.send(
|
|
63
|
-
PERMISSION_REGISTER_PACKET,
|
|
64
|
-
Array.from(this.registeredPermissions.values()),
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
if (this.requiredPermissions.size > 0) {
|
|
68
|
-
this.omu.send(
|
|
69
|
-
PERMISSION_REQUIRE_PACKET,
|
|
70
|
-
Array.from(this.requiredPermissions.values()),
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public register(permission: PermissionType): void {
|
|
76
|
-
if (this.omu.running) {
|
|
77
|
-
this.omu.send(PERMISSION_REGISTER_PACKET, [permission]);
|
|
78
|
-
}
|
|
79
|
-
this.registeredPermissions.set(permission.id, permission);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public require(...permissionIds: IntoId[]): void {
|
|
83
|
-
const ids = permissionIds.map(Identifier.from);
|
|
84
|
-
const missing = ids.filter((id) => !this.permissions.has(id));
|
|
85
|
-
if (this.omu.running) {
|
|
86
|
-
if (missing.length > 0) {
|
|
87
|
-
throw new Error('Permissions must be registered before the client starts');
|
|
88
|
-
}
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
for (const id of ids) {
|
|
92
|
-
this.requiredPermissions.add(id);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
public async request(...permissionIds: Identifier[]): Promise<void> {
|
|
97
|
-
await this.omu.endpoints.call(PERMISSION_REQUEST_ENDPOINT, permissionIds);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
public has(permissionIdentifier: Identifier): boolean {
|
|
101
|
-
return this.permissions.has(permissionIdentifier);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public required(permissionIdentifier: Identifier): boolean {
|
|
105
|
-
return this.requiredPermissions.has(permissionIdentifier);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { Identifier, IntoId } from '../../identifier';
|
|
2
|
-
import type { LocalizedText } from '../../localization/localization.js';
|
|
3
|
-
|
|
4
|
-
export type PermissionLevel = 'low' | 'medium' | 'high';
|
|
5
|
-
|
|
6
|
-
export type PermissionMetadata = {
|
|
7
|
-
name: LocalizedText;
|
|
8
|
-
note?: LocalizedText;
|
|
9
|
-
level: PermissionLevel;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export type PermissionTypeJson = {
|
|
13
|
-
id: string;
|
|
14
|
-
metadata: PermissionMetadata;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export class PermissionType {
|
|
18
|
-
public readonly id: Identifier;
|
|
19
|
-
public readonly metadata: PermissionMetadata;
|
|
20
|
-
|
|
21
|
-
constructor(options: { id: Identifier; metadata: PermissionMetadata }) {
|
|
22
|
-
this.id = options.id;
|
|
23
|
-
this.metadata = options.metadata;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public static create(
|
|
27
|
-
identifier: IntoId,
|
|
28
|
-
{
|
|
29
|
-
metadata,
|
|
30
|
-
}: {
|
|
31
|
-
metadata: PermissionMetadata;
|
|
32
|
-
},
|
|
33
|
-
): PermissionType {
|
|
34
|
-
return new PermissionType({
|
|
35
|
-
id: Identifier.from(identifier),
|
|
36
|
-
metadata: metadata,
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public static serialize(data: PermissionType): PermissionTypeJson {
|
|
41
|
-
return {
|
|
42
|
-
id: data.id.key(),
|
|
43
|
-
metadata: data.metadata,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public static deserialize(json: PermissionTypeJson): PermissionType {
|
|
48
|
-
return new PermissionType({
|
|
49
|
-
id: Identifier.fromKey(json.id),
|
|
50
|
-
metadata: json.metadata,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|