@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
package/src/omu.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { ExtensionRegistry } from './api';
|
|
2
|
-
import { ASSET_EXTENSION_TYPE, type AssetExtension } from './api/asset/extension';
|
|
3
|
-
import { DASHBOARD_EXTENSION_TYPE, type DashboardExtension } from './api/dashboard/extension';
|
|
4
|
-
import { ENDPOINT_EXTENSION_TYPE, type EndpointExtension } from './api/endpoint/extension';
|
|
5
|
-
import { HTTP_EXTENSION_TYPE, HttpExtension } from './api/http/extension';
|
|
6
|
-
import { I18N_EXTENSION_TYPE, type I18nExtension } from './api/i18n/extension';
|
|
7
|
-
import { LOGGER_EXTENSION_TYPE, type LoggerExtension } from './api/logger/extension.js';
|
|
8
|
-
import { PERMISSION_EXTENSION_TYPE, type PermissionExtension } from './api/permission/extension';
|
|
9
|
-
import { PLUGIN_EXTENSION_TYPE, type PluginExtension } from './api/plugin/extension';
|
|
10
|
-
import { REGISTRY_EXTENSION_TYPE, type RegistryExtension } from './api/registry/extension';
|
|
11
|
-
import { SERVER_EXTENSION_TYPE, type ServerExtension } from './api/server/extension';
|
|
12
|
-
import { SESSION_EXTENSION_TYPE, SessionExtension } from './api/session';
|
|
13
|
-
import { SIGNAL_EXTENSION_TYPE, type SignalExtension } from './api/signal/extension';
|
|
14
|
-
import { TABLE_EXTENSION_TYPE, type TableExtension } from './api/table/extension';
|
|
15
|
-
import type { App } from './app.js';
|
|
16
|
-
import type { Unlisten } from './event';
|
|
17
|
-
import { EventEmitter } from './event';
|
|
18
|
-
import type { Address, Connection } from './network';
|
|
19
|
-
import { Network } from './network';
|
|
20
|
-
import { Transport } from './network/connection.js';
|
|
21
|
-
import { PACKET_TYPES } from './network/packet/packet-types.js';
|
|
22
|
-
import type { PacketType } from './network/packet/packet.js';
|
|
23
|
-
import { WebsocketTransport } from './network/websocket-transport';
|
|
24
|
-
import { BrowserSession, type SessioTokenProvider } from './token.js';
|
|
25
|
-
|
|
26
|
-
export type ClientEvents = {
|
|
27
|
-
started: EventEmitter<[]>;
|
|
28
|
-
stopped: EventEmitter<[]>;
|
|
29
|
-
ready: EventEmitter<[]>;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export class Omu {
|
|
33
|
-
public ready: boolean;
|
|
34
|
-
public running: boolean;
|
|
35
|
-
readonly event: ClientEvents = {
|
|
36
|
-
started: new EventEmitter(),
|
|
37
|
-
stopped: new EventEmitter(),
|
|
38
|
-
ready: new EventEmitter(),
|
|
39
|
-
};
|
|
40
|
-
readonly token: SessioTokenProvider;
|
|
41
|
-
readonly address: Address;
|
|
42
|
-
readonly sessions: SessionExtension;
|
|
43
|
-
readonly network: Network;
|
|
44
|
-
readonly endpoints: EndpointExtension;
|
|
45
|
-
readonly permissions: PermissionExtension;
|
|
46
|
-
readonly plugins: PluginExtension;
|
|
47
|
-
readonly dashboard: DashboardExtension;
|
|
48
|
-
readonly extensions: ExtensionRegistry;
|
|
49
|
-
readonly tables: TableExtension;
|
|
50
|
-
readonly registries: RegistryExtension;
|
|
51
|
-
readonly signals: SignalExtension;
|
|
52
|
-
readonly assets: AssetExtension;
|
|
53
|
-
readonly i18n: I18nExtension;
|
|
54
|
-
readonly server: ServerExtension;
|
|
55
|
-
readonly logger: LoggerExtension;
|
|
56
|
-
readonly http: HttpExtension;
|
|
57
|
-
|
|
58
|
-
constructor(
|
|
59
|
-
public readonly app: App,
|
|
60
|
-
options?: {
|
|
61
|
-
address?: Address;
|
|
62
|
-
token?: SessioTokenProvider | string;
|
|
63
|
-
transport?: Transport;
|
|
64
|
-
connection?: Connection;
|
|
65
|
-
},
|
|
66
|
-
) {
|
|
67
|
-
this.ready = false;
|
|
68
|
-
this.running = false;
|
|
69
|
-
if (typeof options?.token === 'string') {
|
|
70
|
-
const token = options?.token;
|
|
71
|
-
this.token = {
|
|
72
|
-
get: () => Promise.resolve(token),
|
|
73
|
-
};
|
|
74
|
-
} else {
|
|
75
|
-
this.token = options?.token ?? new BrowserSession();
|
|
76
|
-
}
|
|
77
|
-
this.address = options?.address ?? {
|
|
78
|
-
host: '127.0.0.1',
|
|
79
|
-
port: 26423,
|
|
80
|
-
secure: false,
|
|
81
|
-
};
|
|
82
|
-
this.network = new Network(
|
|
83
|
-
this,
|
|
84
|
-
this.address,
|
|
85
|
-
this.token,
|
|
86
|
-
options?.transport ?? new WebsocketTransport(this.address),
|
|
87
|
-
options?.connection,
|
|
88
|
-
);
|
|
89
|
-
this.network.event.disconnected.listen(() => {
|
|
90
|
-
this.ready = false;
|
|
91
|
-
});
|
|
92
|
-
this.network.addPacketHandler(PACKET_TYPES.READY, () => {
|
|
93
|
-
if (this.ready) {
|
|
94
|
-
throw new Error('Received READY packet when already ready');
|
|
95
|
-
}
|
|
96
|
-
this.ready = true;
|
|
97
|
-
this.event.ready.emit();
|
|
98
|
-
});
|
|
99
|
-
this.extensions = new ExtensionRegistry(this);
|
|
100
|
-
this.endpoints = this.extensions.register(ENDPOINT_EXTENSION_TYPE);
|
|
101
|
-
this.plugins = this.extensions.register(PLUGIN_EXTENSION_TYPE);
|
|
102
|
-
this.permissions = this.extensions.register(PERMISSION_EXTENSION_TYPE);
|
|
103
|
-
this.tables = this.extensions.register(TABLE_EXTENSION_TYPE);
|
|
104
|
-
this.sessions = this.extensions.register(SESSION_EXTENSION_TYPE);
|
|
105
|
-
this.registries = this.extensions.register(REGISTRY_EXTENSION_TYPE);
|
|
106
|
-
this.dashboard = this.extensions.register(DASHBOARD_EXTENSION_TYPE);
|
|
107
|
-
this.signals = this.extensions.register(SIGNAL_EXTENSION_TYPE);
|
|
108
|
-
this.assets = this.extensions.register(ASSET_EXTENSION_TYPE);
|
|
109
|
-
this.i18n = this.extensions.register(I18N_EXTENSION_TYPE);
|
|
110
|
-
this.server = this.extensions.register(SERVER_EXTENSION_TYPE);
|
|
111
|
-
this.logger = this.extensions.register(LOGGER_EXTENSION_TYPE);
|
|
112
|
-
this.http = this.extensions.register(HTTP_EXTENSION_TYPE);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
public send<T>(packetType: PacketType<T>, data: T): void {
|
|
116
|
-
this.network.send({
|
|
117
|
-
type: packetType,
|
|
118
|
-
data,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public async start(): Promise<void> {
|
|
123
|
-
if (this.running) {
|
|
124
|
-
throw new Error('Client already running');
|
|
125
|
-
}
|
|
126
|
-
this.running = true;
|
|
127
|
-
const connect = this.network.connect();
|
|
128
|
-
this.event.started.emit();
|
|
129
|
-
await connect;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
public stop(): void {
|
|
133
|
-
this.running = false;
|
|
134
|
-
this.ready = false;
|
|
135
|
-
this.network.close();
|
|
136
|
-
this.event.stopped.emit();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
public onReady(callback: () => void): Unlisten {
|
|
140
|
-
if (this.ready) {
|
|
141
|
-
callback();
|
|
142
|
-
}
|
|
143
|
-
return this.event.ready.listen(callback);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
public async waitForReady(): Promise<void> {
|
|
147
|
-
if (this.ready) return;
|
|
148
|
-
return new Promise<void>((resolve) => {
|
|
149
|
-
let unlisten = () => {};
|
|
150
|
-
unlisten = this.event.ready.listen(() => {
|
|
151
|
-
unlisten();
|
|
152
|
-
resolve();
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
}
|
package/src/result.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
export type Success<T> = {
|
|
2
|
-
data: T;
|
|
3
|
-
error: null;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export type Failure<E> = {
|
|
7
|
-
data: null;
|
|
8
|
-
error: E;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export type Result<T, E = Error> = Success<T> | Failure<E>;
|
|
12
|
-
|
|
13
|
-
export async function tryCatch<T, E = Error>(
|
|
14
|
-
promise: Promise<T>,
|
|
15
|
-
): Promise<Result<T, E>> {
|
|
16
|
-
try {
|
|
17
|
-
const data = await promise;
|
|
18
|
-
return { data, error: null };
|
|
19
|
-
} catch (error) {
|
|
20
|
-
return { data: null, error: error as E };
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function success<T>(data: T): Result<T> {
|
|
25
|
-
return { data, error: null };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function failure<E>(error: E): Result<null, E> {
|
|
29
|
-
return { data: null, error };
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class PromiseResult<T, E = Error> implements Promise<T> {
|
|
33
|
-
private constructor(
|
|
34
|
-
private readonly promise: Promise<T>,
|
|
35
|
-
) {}
|
|
36
|
-
|
|
37
|
-
public static create<T, E = Error>(): { promise: PromiseResult<T, E>; resolve: (value: T) => void; reject: (error: E) => void } {
|
|
38
|
-
let resolve: (value: T) => void = () => {};
|
|
39
|
-
let reject: (error: E) => void = () => {};
|
|
40
|
-
const promise = new Promise<T>((res, rej) => {
|
|
41
|
-
resolve = res;
|
|
42
|
-
reject = rej;
|
|
43
|
-
});
|
|
44
|
-
return {
|
|
45
|
-
promise: new PromiseResult<T, E>(promise),
|
|
46
|
-
resolve,
|
|
47
|
-
reject,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: E) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2> {
|
|
52
|
-
return this.promise.then(onfulfilled, onrejected);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
catch<TResult = never>(onrejected?: ((reason: E) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult> {
|
|
56
|
-
return this.promise.catch(onrejected);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
finally(onfinally?: (() => void) | null | undefined): Promise<T> {
|
|
60
|
-
return this.promise.finally(onfinally);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
[Symbol.toStringTag]: string = 'ResultPromise';
|
|
64
|
-
|
|
65
|
-
async toResult(): Promise<Result<T, E>> {
|
|
66
|
-
try {
|
|
67
|
-
return {
|
|
68
|
-
data: await this.promise,
|
|
69
|
-
error: null,
|
|
70
|
-
};
|
|
71
|
-
} catch (err) {
|
|
72
|
-
return {
|
|
73
|
-
data: null,
|
|
74
|
-
error: err as E,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
@@ -1,432 +0,0 @@
|
|
|
1
|
-
import { textDecoder, textEncoder } from '../const.js';
|
|
2
|
-
import { JsonType } from './serializer.js';
|
|
3
|
-
|
|
4
|
-
export class Flags {
|
|
5
|
-
private value: number;
|
|
6
|
-
private length: number;
|
|
7
|
-
|
|
8
|
-
constructor(options: { value?: number; length?: number }) {
|
|
9
|
-
this.value = options.value ?? 0;
|
|
10
|
-
this.length = options.length ?? 32;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public has(position: number): boolean {
|
|
14
|
-
return (this.value & (1 << position)) !== 0;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public get(position: number): boolean {
|
|
18
|
-
return (this.value & (1 << position)) !== 0;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public ifSet<T>(position: number, callback: () => T): T | undefined {
|
|
22
|
-
if (this.has(position)) {
|
|
23
|
-
return callback();
|
|
24
|
-
}
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public set(position: number, value: boolean): void {
|
|
29
|
-
if (value) {
|
|
30
|
-
this.value |= 1 << position;
|
|
31
|
-
} else {
|
|
32
|
-
this.value &= ~(1 << position);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public write(writer: ByteWriter): void {
|
|
37
|
-
const bits = new Uint8Array((this.length + 7) / 8);
|
|
38
|
-
for (let i = 0; i < bits.length; i++) {
|
|
39
|
-
bits[i] = (this.value >> (i * 8)) & 0xff;
|
|
40
|
-
}
|
|
41
|
-
writer.write(bits);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public static read(reader: ByteReader, length: number): Flags {
|
|
45
|
-
let value = 0;
|
|
46
|
-
for (let i = 0; i < (length + 7) / 8; i++) {
|
|
47
|
-
value |= reader.readUint8() << (i * 8);
|
|
48
|
-
}
|
|
49
|
-
return new Flags({ value, length });
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function readFloat16(dataView: DataView, offset: number): number {
|
|
54
|
-
const sign = (dataView.getUint16(offset) & 0x8000) ? -1 : 1;
|
|
55
|
-
const exponent = (dataView.getUint16(offset) & 0x7c00) >> 10;
|
|
56
|
-
const mantissa = (dataView.getUint16(offset) & 0x03ff);
|
|
57
|
-
if (exponent === 0 && mantissa === 0) {
|
|
58
|
-
return 0;
|
|
59
|
-
} else if (exponent === 0x1f) {
|
|
60
|
-
return mantissa === 0 ? Infinity * sign : NaN;
|
|
61
|
-
} else {
|
|
62
|
-
const normalizedExponent = exponent - 15;
|
|
63
|
-
const normalizedMantissa = mantissa / 1024;
|
|
64
|
-
return sign * Math.pow(2, normalizedExponent) * (1 + normalizedMantissa);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function writeFloat16(dataView: DataView, offset: number, value: number): void {
|
|
69
|
-
if (value === 0) {
|
|
70
|
-
dataView.setUint16(offset, 0);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
const sign = value < 0 ? 1 : 0;
|
|
74
|
-
value = Math.abs(value);
|
|
75
|
-
if (!isFinite(value)) {
|
|
76
|
-
dataView.setUint16(offset, 0x7c00 | (sign << 15));
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const exponent = Math.floor(Math.log2(value));
|
|
80
|
-
const mantissa = value / Math.pow(2, exponent) - 1;
|
|
81
|
-
const normalizedExponent = exponent + 15;
|
|
82
|
-
const normalizedMantissa = Math.round(mantissa * 1024);
|
|
83
|
-
if (normalizedExponent <= 0) {
|
|
84
|
-
dataView.setUint16(offset, sign << 15);
|
|
85
|
-
} else if (normalizedExponent >= 0x1f) {
|
|
86
|
-
dataView.setUint16(offset, 0x7c00 | (sign << 15));
|
|
87
|
-
} else {
|
|
88
|
-
dataView.setUint16(offset, (sign << 15) | (normalizedExponent << 10) | normalizedMantissa);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export class ByteWriter {
|
|
93
|
-
private dataView: DataView;
|
|
94
|
-
private buffer: ArrayBuffer;
|
|
95
|
-
private offset = 0;
|
|
96
|
-
private finished = false;
|
|
97
|
-
|
|
98
|
-
constructor(init?: ArrayBuffer, offset?: number) {
|
|
99
|
-
this.offset = offset ?? 0;
|
|
100
|
-
this.buffer = init ?? new ArrayBuffer(1024);
|
|
101
|
-
this.dataView = new DataView(this.buffer);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public static fromUint8Array(buffer: Uint8Array): ByteWriter {
|
|
105
|
-
const arrayBuffer = new ArrayBuffer(buffer.byteLength);
|
|
106
|
-
const uint8Array = new Uint8Array(arrayBuffer);
|
|
107
|
-
uint8Array.set(new Uint8Array(buffer));
|
|
108
|
-
return new ByteWriter(arrayBuffer, buffer.byteLength);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private allocate(length: number): void {
|
|
112
|
-
if (this.offset + length > this.buffer.byteLength) {
|
|
113
|
-
const newByteLength = Math.max(this.buffer.byteLength * 2, this.offset + length);
|
|
114
|
-
const newBuffer = new ArrayBuffer(newByteLength);
|
|
115
|
-
new Uint8Array(newBuffer).set(new Uint8Array(this.buffer));
|
|
116
|
-
this.buffer = newBuffer;
|
|
117
|
-
this.dataView = new DataView(this.buffer);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public write(data: Uint8Array): ByteWriter {
|
|
122
|
-
if (this.finished) {
|
|
123
|
-
throw new Error('Buffer already finished');
|
|
124
|
-
}
|
|
125
|
-
this.allocate(data.length);
|
|
126
|
-
new Uint8Array(this.buffer).set(data, this.offset);
|
|
127
|
-
this.offset += data.length;
|
|
128
|
-
return this;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
public writeBoolean(value: boolean): ByteWriter {
|
|
132
|
-
this.allocate(1);
|
|
133
|
-
this.dataView.setInt8(this.offset, value ? 1 : 0);
|
|
134
|
-
this.offset += 1;
|
|
135
|
-
return this;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
public writeInt8(value: number): ByteWriter {
|
|
139
|
-
this.allocate(1);
|
|
140
|
-
this.dataView.setInt8(this.offset, value);
|
|
141
|
-
this.offset += 1;
|
|
142
|
-
return this;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
public writeUint8(value: number): ByteWriter {
|
|
146
|
-
this.allocate(1);
|
|
147
|
-
this.dataView.setUint8(this.offset, value);
|
|
148
|
-
this.offset += 1;
|
|
149
|
-
return this;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
public writeInt16(value: number): ByteWriter {
|
|
153
|
-
this.allocate(2);
|
|
154
|
-
this.dataView.setInt16(this.offset, value);
|
|
155
|
-
this.offset += 2;
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
public writeUint16(value: number): ByteWriter {
|
|
160
|
-
this.allocate(2);
|
|
161
|
-
this.dataView.setUint16(this.offset, value);
|
|
162
|
-
this.offset += 2;
|
|
163
|
-
return this;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
public writeInt32(value: number): ByteWriter {
|
|
167
|
-
this.allocate(4);
|
|
168
|
-
this.dataView.setInt32(this.offset, value);
|
|
169
|
-
this.offset += 4;
|
|
170
|
-
return this;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
public writeUint32(value: number): ByteWriter {
|
|
174
|
-
this.allocate(4);
|
|
175
|
-
this.dataView.setUint32(this.offset, value);
|
|
176
|
-
this.offset += 4;
|
|
177
|
-
return this;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
public writeInt64(value: bigint): ByteWriter {
|
|
181
|
-
this.allocate(8);
|
|
182
|
-
this.dataView.setBigInt64(this.offset, value);
|
|
183
|
-
this.offset += 8;
|
|
184
|
-
return this;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
public writeUint64(value: bigint): ByteWriter {
|
|
188
|
-
this.allocate(8);
|
|
189
|
-
this.dataView.setBigUint64(this.offset, value);
|
|
190
|
-
this.offset += 8;
|
|
191
|
-
return this;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
public writeULEB128(value: number): ByteWriter {
|
|
195
|
-
// Unsigned LEB128
|
|
196
|
-
if (value < 0) {
|
|
197
|
-
throw new Error('Value must be non-negative');
|
|
198
|
-
}
|
|
199
|
-
while (value > 0x7f) {
|
|
200
|
-
this.allocate(1);
|
|
201
|
-
this.dataView.setUint8(this.offset, (value & 0x7f) | 0x80);
|
|
202
|
-
this.offset += 1;
|
|
203
|
-
value >>= 7;
|
|
204
|
-
}
|
|
205
|
-
this.allocate(1);
|
|
206
|
-
this.dataView.setUint8(this.offset, value & 0x7f);
|
|
207
|
-
this.offset += 1;
|
|
208
|
-
return this;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
public writeFloat16(value: number): ByteWriter {
|
|
212
|
-
this.allocate(2);
|
|
213
|
-
writeFloat16(this.dataView, this.offset, value);
|
|
214
|
-
this.offset += 2;
|
|
215
|
-
return this;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
public writeFloat32(value: number): ByteWriter {
|
|
219
|
-
this.allocate(4);
|
|
220
|
-
this.dataView.setFloat32(this.offset, value);
|
|
221
|
-
this.offset += 4;
|
|
222
|
-
return this;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
public writeFloat64(value: number): ByteWriter {
|
|
226
|
-
this.allocate(4);
|
|
227
|
-
this.dataView.setFloat64(this.offset, value);
|
|
228
|
-
this.offset += 4;
|
|
229
|
-
return this;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
public writeUint8Array(value: Uint8Array): ByteWriter {
|
|
233
|
-
this.writeULEB128(value.byteLength);
|
|
234
|
-
this.write(value);
|
|
235
|
-
return this;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
public writeString(value: string): ByteWriter {
|
|
239
|
-
this.writeUint8Array(textEncoder.encode(value));
|
|
240
|
-
return this;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
public writeJSON(value: JsonType): ByteWriter {
|
|
244
|
-
this.writeString(JSON.stringify(value));
|
|
245
|
-
return this;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
public writeFlags(flags: Flags): ByteWriter {
|
|
249
|
-
flags.write(this);
|
|
250
|
-
return this;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
public finish(): Uint8Array {
|
|
254
|
-
if (this.finished) {
|
|
255
|
-
throw new Error('Buffer already finished');
|
|
256
|
-
}
|
|
257
|
-
this.finished = true;
|
|
258
|
-
return new Uint8Array(this.buffer, 0, this.offset);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
public toUint8Array(): Uint8Array {
|
|
262
|
-
return new Uint8Array(this.buffer, 0, this.offset);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
public toArrayBuffer(): ArrayBuffer {
|
|
266
|
-
return this.buffer.slice(0, this.offset);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
export class ByteReader {
|
|
271
|
-
private dataView: DataView;
|
|
272
|
-
private offset: number;
|
|
273
|
-
private finished: boolean;
|
|
274
|
-
|
|
275
|
-
private constructor(data: DataView) {
|
|
276
|
-
this.dataView = data;
|
|
277
|
-
this.offset = 0;
|
|
278
|
-
this.finished = false;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
public static fromUint8Array(buffer: Uint8Array): ByteReader {
|
|
282
|
-
const arrayBuffer = new ArrayBuffer(buffer.byteLength);
|
|
283
|
-
const uint8Array = new Uint8Array(arrayBuffer);
|
|
284
|
-
uint8Array.set(new Uint8Array(buffer));
|
|
285
|
-
const dataView = new DataView(arrayBuffer);
|
|
286
|
-
return new ByteReader(dataView);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
public static fromArrayBuffer(buffer: ArrayBuffer): ByteReader {
|
|
290
|
-
const dataView = new DataView(buffer);
|
|
291
|
-
return new ByteReader(dataView);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
public static async fromBlob(blob: Blob): Promise<ByteReader> {
|
|
295
|
-
const arrayBuffer = await blob.arrayBuffer();
|
|
296
|
-
const dataView = new DataView(arrayBuffer);
|
|
297
|
-
return new ByteReader(dataView);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
public read(size: number): Uint8Array {
|
|
301
|
-
if (this.finished) {
|
|
302
|
-
throw new Error('Buffer already finished');
|
|
303
|
-
}
|
|
304
|
-
if (size < 0) {
|
|
305
|
-
throw new Error('Size must be positive');
|
|
306
|
-
}
|
|
307
|
-
if (this.offset + size > this.dataView.byteLength) {
|
|
308
|
-
throw new Error('Buffer not fully read');
|
|
309
|
-
}
|
|
310
|
-
const value = new Uint8Array(this.dataView.buffer, this.offset, size);
|
|
311
|
-
this.offset += size;
|
|
312
|
-
return value;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
public readBoolean(): boolean {
|
|
316
|
-
const value = this.dataView.getInt8(this.offset);
|
|
317
|
-
this.offset += 1;
|
|
318
|
-
return value !== 0;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
public readInt8(): number {
|
|
322
|
-
const value = this.dataView.getInt8(this.offset);
|
|
323
|
-
this.offset += 1;
|
|
324
|
-
return value;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
public readUint8(): number {
|
|
328
|
-
const value = this.dataView.getUint8(this.offset);
|
|
329
|
-
this.offset += 1;
|
|
330
|
-
return value;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
public readInt16(): number {
|
|
334
|
-
const value = this.dataView.getInt16(this.offset);
|
|
335
|
-
this.offset += 2;
|
|
336
|
-
return value;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
public readUint16(): number {
|
|
340
|
-
const value = this.dataView.getUint16(this.offset);
|
|
341
|
-
this.offset += 2;
|
|
342
|
-
return value;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
public readInt32(): number {
|
|
346
|
-
const value = this.dataView.getInt32(this.offset);
|
|
347
|
-
this.offset += 4;
|
|
348
|
-
return value;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
public readUint32(): number {
|
|
352
|
-
const value = this.dataView.getUint32(this.offset);
|
|
353
|
-
this.offset += 4;
|
|
354
|
-
return value;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
public readInt64(): bigint {
|
|
358
|
-
const value = this.dataView.getBigInt64(this.offset);
|
|
359
|
-
this.offset += 8;
|
|
360
|
-
return value;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
public readUint64(): bigint {
|
|
364
|
-
const value = this.dataView.getBigUint64(this.offset);
|
|
365
|
-
this.offset += 8;
|
|
366
|
-
return value;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
public readULEB128(): number {
|
|
370
|
-
// Unsigned LEB128
|
|
371
|
-
let value = 0;
|
|
372
|
-
let shift = 0;
|
|
373
|
-
let byte: number;
|
|
374
|
-
do {
|
|
375
|
-
if (this.offset >= this.dataView.byteLength) {
|
|
376
|
-
throw new Error('Buffer not fully read');
|
|
377
|
-
}
|
|
378
|
-
byte = this.dataView.getUint8(this.offset);
|
|
379
|
-
this.offset += 1;
|
|
380
|
-
value |= (byte & 0b01111111) << shift;
|
|
381
|
-
shift += 7;
|
|
382
|
-
}
|
|
383
|
-
while ((byte & 0b10000000) !== 0);
|
|
384
|
-
return value;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
public readFloat16(): number {
|
|
388
|
-
const value = readFloat16(this.dataView, this.offset);
|
|
389
|
-
this.offset += 2;
|
|
390
|
-
return value;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
public readFloat32(): number {
|
|
394
|
-
const value = this.dataView.getFloat32(this.offset);
|
|
395
|
-
this.offset += 4;
|
|
396
|
-
return value;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
public readFloat64(): number {
|
|
400
|
-
const value = this.dataView.getFloat64(this.offset);
|
|
401
|
-
this.offset += 8;
|
|
402
|
-
return value;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
public readUint8Array(): Uint8Array {
|
|
406
|
-
const length = this.readULEB128();
|
|
407
|
-
return this.read(length);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
public readString(): string {
|
|
411
|
-
const byteArray = this.readUint8Array();
|
|
412
|
-
return textDecoder.decode(byteArray);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
public readJSON<T extends JsonType>(): T {
|
|
416
|
-
return JSON.parse(this.readString());
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
public readFlags(length: number): Flags {
|
|
420
|
-
return Flags.read(this, length);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
public finish(): void {
|
|
424
|
-
if (this.finished) {
|
|
425
|
-
throw new Error('Buffer already finished');
|
|
426
|
-
}
|
|
427
|
-
this.finished = true;
|
|
428
|
-
if (this.offset !== this.dataView.byteLength) {
|
|
429
|
-
throw new Error('Buffer not fully read');
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
}
|