@omujs/omu 0.9.48 → 0.9.50
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/address.js +1 -0
- package/dist/address.js.map +7 -0
- package/dist/api/asset/extension.d.ts +25 -0
- package/dist/api/asset/extension.js +2 -0
- package/dist/api/asset/extension.js.map +7 -0
- package/dist/api/asset/index.d.ts +2 -0
- package/dist/api/asset/index.js +2 -0
- package/dist/api/asset/index.js.map +7 -0
- package/dist/api/dashboard/extension.d.ts +181 -0
- package/dist/api/dashboard/extension.js +2 -0
- package/dist/api/dashboard/extension.js.map +7 -0
- package/{src/api/dashboard/handler.ts → dist/api/dashboard/handler.d.ts} +3 -5
- package/dist/api/dashboard/handler.js +1 -0
- package/dist/api/dashboard/handler.js.map +7 -0
- package/dist/api/dashboard/index.d.ts +3 -0
- package/dist/api/dashboard/index.js +2 -0
- package/dist/api/dashboard/index.js.map +7 -0
- package/dist/api/dashboard/packets.d.ts +63 -0
- package/dist/api/dashboard/packets.js +2 -0
- package/dist/api/dashboard/packets.js.map +7 -0
- package/dist/api/endpoint/endpoint.d.ts +21 -0
- package/dist/api/endpoint/endpoint.js +2 -0
- package/dist/api/endpoint/endpoint.js.map +7 -0
- package/dist/api/endpoint/extension.d.ts +17 -0
- package/dist/api/endpoint/extension.js +2 -0
- package/dist/api/endpoint/extension.js.map +7 -0
- package/{src/api/endpoint/index.ts → dist/api/endpoint/index.d.ts} +0 -1
- package/dist/api/endpoint/index.js +2 -0
- package/dist/api/endpoint/index.js.map +7 -0
- package/dist/api/endpoint/packets.d.ts +51 -0
- package/dist/api/endpoint/packets.js +2 -0
- package/dist/api/endpoint/packets.js.map +7 -0
- package/dist/api/extension-registry.d.ts +8 -0
- package/dist/api/extension-registry.js +2 -0
- package/dist/api/extension-registry.js.map +7 -0
- package/dist/api/extension.d.ts +11 -0
- package/dist/api/extension.js +2 -0
- package/dist/api/extension.js.map +7 -0
- package/dist/api/http/extension.d.ts +41 -0
- package/dist/api/http/extension.js +2 -0
- package/dist/api/http/extension.js.map +7 -0
- package/dist/api/http/http.d.ts +36 -0
- package/dist/api/http/http.js +2 -0
- package/dist/api/http/http.js.map +7 -0
- package/{src/api/http/index.ts → dist/api/http/index.d.ts} +0 -1
- package/dist/api/http/index.js +2 -0
- package/dist/api/http/index.js.map +7 -0
- package/dist/api/http/websocket.d.ts +125 -0
- package/dist/api/http/websocket.js +2 -0
- package/dist/api/http/websocket.js.map +7 -0
- package/dist/api/i18n/extension.d.ts +19 -0
- package/dist/api/i18n/extension.js +2 -0
- package/dist/api/i18n/extension.js.map +7 -0
- package/{src/api/i18n/index.ts → dist/api/i18n/index.d.ts} +0 -1
- package/dist/api/i18n/index.js +2 -0
- package/dist/api/i18n/index.js.map +7 -0
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.js +2 -0
- package/dist/api/index.js.map +7 -0
- package/dist/api/logger/extension.d.ts +25 -0
- package/dist/api/logger/extension.js +2 -0
- package/dist/api/logger/extension.js.map +7 -0
- package/dist/api/logger/index.d.ts +1 -0
- package/dist/api/logger/index.js +2 -0
- package/dist/api/logger/index.js.map +7 -0
- package/dist/api/logger/packets.d.ts +18 -0
- package/dist/api/logger/packets.js +2 -0
- package/dist/api/logger/packets.js.map +7 -0
- package/dist/api/permission/extension.d.ts +19 -0
- package/dist/api/permission/extension.js +2 -0
- package/dist/api/permission/extension.js.map +7 -0
- package/{src/api/permission/index.ts → dist/api/permission/index.d.ts} +0 -1
- package/dist/api/permission/index.js +2 -0
- package/dist/api/permission/index.js.map +7 -0
- package/dist/api/permission/permission.d.ts +25 -0
- package/dist/api/permission/permission.js +2 -0
- package/dist/api/permission/permission.js.map +7 -0
- package/dist/api/plugin/extension.d.ts +43 -0
- package/dist/api/plugin/extension.js +2 -0
- package/dist/api/plugin/extension.js.map +7 -0
- package/{src/api/plugin/index.ts → dist/api/plugin/index.d.ts} +0 -1
- package/dist/api/plugin/index.js +2 -0
- package/dist/api/plugin/index.js.map +7 -0
- package/{src/api/plugin/package-info.ts → dist/api/plugin/package-info.d.ts} +0 -7
- package/dist/api/plugin/package-info.js +1 -0
- package/dist/api/plugin/package-info.js.map +7 -0
- package/dist/api/registry/extension.d.ts +23 -0
- package/dist/api/registry/extension.js +2 -0
- package/dist/api/registry/extension.js.map +7 -0
- package/{src/api/registry/index.ts → dist/api/registry/index.d.ts} +0 -1
- package/dist/api/registry/index.js +2 -0
- package/dist/api/registry/index.js.map +7 -0
- package/dist/api/registry/packets.d.ts +20 -0
- package/dist/api/registry/packets.js +2 -0
- package/dist/api/registry/packets.js.map +7 -0
- package/{src/api/registry/registry.ts → dist/api/registry/registry.d.ts} +11 -53
- package/dist/api/registry/registry.js +2 -0
- package/dist/api/registry/registry.js.map +7 -0
- package/dist/api/server/extension.d.ts +61 -0
- package/dist/api/server/extension.js +2 -0
- package/dist/api/server/extension.js.map +7 -0
- package/dist/api/server/index.d.ts +1 -0
- package/dist/api/server/index.js +2 -0
- package/dist/api/server/index.js.map +7 -0
- package/dist/api/session/extension.d.ts +52 -0
- package/dist/api/session/extension.js +2 -0
- package/dist/api/session/extension.js.map +7 -0
- package/dist/api/session/index.d.ts +1 -0
- package/dist/api/session/index.js +2 -0
- package/dist/api/session/index.js.map +7 -0
- package/dist/api/signal/extension.d.ts +19 -0
- package/dist/api/signal/extension.js +2 -0
- package/dist/api/signal/extension.js.map +7 -0
- package/dist/api/signal/index.d.ts +1 -0
- package/dist/api/signal/index.js +2 -0
- package/dist/api/signal/index.js.map +7 -0
- package/dist/api/signal/packets.d.ts +21 -0
- package/dist/api/signal/packets.js +2 -0
- package/dist/api/signal/packets.js.map +7 -0
- package/dist/api/signal/signal.d.ts +33 -0
- package/dist/api/signal/signal.js +2 -0
- package/dist/api/signal/signal.js.map +7 -0
- package/dist/api/table/extension.d.ts +26 -0
- package/dist/api/table/extension.js +2 -0
- package/dist/api/table/extension.js.map +7 -0
- package/{src/api/table/index.ts → dist/api/table/index.d.ts} +0 -1
- package/dist/api/table/index.js +2 -0
- package/dist/api/table/index.js.map +7 -0
- package/dist/api/table/packets.d.ts +65 -0
- package/dist/api/table/packets.js +2 -0
- package/dist/api/table/packets.js.map +7 -0
- package/dist/api/table/table.d.ts +75 -0
- package/dist/api/table/table.js +2 -0
- package/dist/api/table/table.js.map +7 -0
- package/dist/app.d.ts +49 -0
- package/dist/app.js +2 -0
- package/dist/app.js.map +7 -0
- package/dist/const.d.ts +2 -0
- package/dist/const.js +2 -0
- package/dist/const.js.map +7 -0
- package/dist/errors.d.ts +36 -0
- package/dist/errors.js +2 -0
- package/dist/errors.js.map +7 -0
- package/dist/event/index.d.ts +6 -0
- package/dist/event/index.js +2 -0
- package/dist/event/index.js.map +7 -0
- package/dist/helper.d.ts +10 -0
- package/dist/helper.js +2 -0
- package/dist/helper.js.map +7 -0
- package/dist/identifier.d.ts +51 -0
- package/dist/identifier.js +2 -0
- package/dist/identifier.js.map +7 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +7 -0
- package/{src/localization/index.ts → dist/localization/index.d.ts} +0 -1
- package/dist/localization/index.js +2 -0
- package/dist/localization/index.js.map +7 -0
- package/dist/localization/locale.d.ts +735 -0
- package/dist/localization/locale.js +2 -0
- package/dist/localization/locale.js.map +7 -0
- package/{src/localization/localization.ts → dist/localization/localization.d.ts} +0 -2
- package/dist/localization/localization.js +1 -0
- package/dist/localization/localization.js.map +7 -0
- package/dist/lock.d.ts +6 -0
- package/dist/lock.js +2 -0
- package/dist/lock.js.map +7 -0
- package/dist/network/connection.d.ts +17 -0
- package/dist/network/connection.js +2 -0
- package/dist/network/connection.js.map +7 -0
- package/dist/network/encryption.d.ts +38 -0
- package/dist/network/encryption.js +2 -0
- package/dist/network/encryption.js.map +7 -0
- package/dist/network/frame-transport.d.ts +22 -0
- package/dist/network/frame-transport.js +2 -0
- package/dist/network/frame-transport.js.map +7 -0
- package/{src/network/index.ts → dist/network/index.d.ts} +0 -1
- package/dist/network/index.js +2 -0
- package/dist/network/index.js.map +7 -0
- package/dist/network/network.d.ts +59 -0
- package/dist/network/network.js +2 -0
- package/dist/network/network.js.map +7 -0
- package/{src/network/packet/index.ts → dist/network/packet/index.d.ts} +0 -1
- package/dist/network/packet/index.js +2 -0
- package/dist/network/packet/index.js.map +7 -0
- package/dist/network/packet/packet-types.d.ts +84 -0
- package/dist/network/packet/packet-types.js +2 -0
- package/dist/network/packet/packet-types.js.map +7 -0
- package/dist/network/packet/packet.d.ts +25 -0
- package/dist/network/packet/packet.js +2 -0
- package/dist/network/packet/packet.js.map +7 -0
- package/dist/network/websocket-transport.d.ts +24 -0
- package/dist/network/websocket-transport.js +2 -0
- package/dist/network/websocket-transport.js.map +7 -0
- package/dist/omu.d.ts +61 -0
- package/dist/omu.js +2 -0
- package/dist/omu.js.map +7 -0
- package/dist/result.d.ts +26 -0
- package/dist/result.js +2 -0
- package/dist/result.js.map +7 -0
- package/dist/serialize/bytebuffer.d.ts +73 -0
- package/dist/serialize/bytebuffer.js +2 -0
- package/dist/serialize/bytebuffer.js.map +7 -0
- package/dist/serialize/index.d.ts +2 -0
- package/dist/serialize/index.js +2 -0
- package/dist/serialize/index.js.map +7 -0
- package/dist/serialize/serializer.d.ts +26 -0
- package/dist/serialize/serializer.js +2 -0
- package/dist/serialize/serializer.js.map +7 -0
- package/dist/token.d.ts +14 -0
- package/dist/token.js +2 -0
- package/dist/token.js.map +7 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +7 -0
- package/package.json +12 -9
- 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/index.ts +0 -3
- 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/index.ts +0 -1
- 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/index.ts +0 -2
- 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/address.d.ts} +0 -0
package/src/network/network.ts
DELETED
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
import type { Address } from '../address.js';
|
|
2
|
-
import {
|
|
3
|
-
AnotherConnection, AppRemoved, DisconnectReason, InternalError,
|
|
4
|
-
InvalidOrigin,
|
|
5
|
-
InvalidPacket,
|
|
6
|
-
InvalidToken,
|
|
7
|
-
InvalidVersion,
|
|
8
|
-
PermissionDenied,
|
|
9
|
-
ServerRestart,
|
|
10
|
-
} from '../errors.js';
|
|
11
|
-
import { EventEmitter } from '../event';
|
|
12
|
-
import { IdentifierMap } from '../identifier';
|
|
13
|
-
import { Omu } from '../omu.js';
|
|
14
|
-
import type { SessioTokenProvider } from '../token.js';
|
|
15
|
-
import { VERSION } from '../version.js';
|
|
16
|
-
|
|
17
|
-
import type { Connection, Transport } from './connection.js';
|
|
18
|
-
import { PacketMapper } from './connection.js';
|
|
19
|
-
import { AES, Decryptor, ENCRYPTION_AVAILABLE, EncryptionResponse, Encryptor } from './encryption.js';
|
|
20
|
-
import { ConnectPacket, DisconnectType, PACKET_TYPES } from './packet/packet-types.js';
|
|
21
|
-
import type { Packet, PacketType } from './packet/packet.js';
|
|
22
|
-
|
|
23
|
-
type PacketHandler<T> = {
|
|
24
|
-
readonly type: PacketType<T>;
|
|
25
|
-
event: EventEmitter<[T]>;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
type StatusType<T> = {
|
|
29
|
-
type: T;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type NetworkStatus = StatusType<'connecting'> | StatusType<'connected'> | StatusType<'ready'> | {
|
|
33
|
-
type: 'disconnected';
|
|
34
|
-
attempt?: number;
|
|
35
|
-
reason?: DisconnectReason;
|
|
36
|
-
} | {
|
|
37
|
-
type: 'reconnecting';
|
|
38
|
-
attempt: number;
|
|
39
|
-
timeout: number;
|
|
40
|
-
date: Date;
|
|
41
|
-
cancel: () => void;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export class Network {
|
|
45
|
-
public status: NetworkStatus = { type: 'disconnected' };
|
|
46
|
-
public readonly event: { connected: EventEmitter<[]>; disconnected: EventEmitter<[DisconnectReason | undefined]>; packet: EventEmitter<[Packet<unknown>]>; status: EventEmitter<[NetworkStatus]> } = {
|
|
47
|
-
connected: new EventEmitter(),
|
|
48
|
-
disconnected: new EventEmitter(),
|
|
49
|
-
packet: new EventEmitter(),
|
|
50
|
-
status: new EventEmitter(),
|
|
51
|
-
};
|
|
52
|
-
private readonly tasks: Array<() => Promise<void> | void> = [];
|
|
53
|
-
private readonly packetMapper = new PacketMapper();
|
|
54
|
-
private readonly packetHandlers = new IdentifierMap<PacketHandler<unknown>>();
|
|
55
|
-
private listenTask: Promise<void> | undefined = undefined;
|
|
56
|
-
public reason: DisconnectReason | undefined = undefined;
|
|
57
|
-
private attempt = 0;
|
|
58
|
-
|
|
59
|
-
constructor(
|
|
60
|
-
private readonly omu: Omu,
|
|
61
|
-
public address: Address,
|
|
62
|
-
private readonly tokenProvider: SessioTokenProvider,
|
|
63
|
-
private transport: Transport,
|
|
64
|
-
private connection?: Connection | undefined,
|
|
65
|
-
private aes?: AES | undefined,
|
|
66
|
-
) {
|
|
67
|
-
this.registerPacket(
|
|
68
|
-
PACKET_TYPES.SERVER_META,
|
|
69
|
-
PACKET_TYPES.CONNECT,
|
|
70
|
-
PACKET_TYPES.DISCONNECT,
|
|
71
|
-
PACKET_TYPES.TOKEN,
|
|
72
|
-
PACKET_TYPES.READY,
|
|
73
|
-
PACKET_TYPES.ENCRYPTED_PACKET,
|
|
74
|
-
);
|
|
75
|
-
this.addPacketHandler(PACKET_TYPES.DISCONNECT, async (reason) => {
|
|
76
|
-
if (reason.type === DisconnectType.SHUTDOWN
|
|
77
|
-
|| reason.type === DisconnectType.CLOSE) {
|
|
78
|
-
this.setStatus({ type: 'disconnected' });
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const ERROR_MAP = {
|
|
82
|
-
[DisconnectType.ANOTHER_CONNECTION]: AnotherConnection,
|
|
83
|
-
[DisconnectType.PERMISSION_DENIED]: PermissionDenied,
|
|
84
|
-
[DisconnectType.APP_REMOVED]: AppRemoved,
|
|
85
|
-
[DisconnectType.INVALID_TOKEN]: InvalidToken,
|
|
86
|
-
[DisconnectType.INVALID_ORIGIN]: InvalidOrigin,
|
|
87
|
-
[DisconnectType.INVALID_VERSION]: InvalidVersion,
|
|
88
|
-
[DisconnectType.INTERNAL_ERROR]: InternalError,
|
|
89
|
-
[DisconnectType.INVALID_PACKET]: InvalidPacket,
|
|
90
|
-
[DisconnectType.INVALID_PACKET_TYPE]: InvalidPacket,
|
|
91
|
-
[DisconnectType.INVALID_PACKET_DATA]: InvalidPacket,
|
|
92
|
-
[DisconnectType.SERVER_RESTART]: ServerRestart,
|
|
93
|
-
[DisconnectType.CLOSE]: undefined,
|
|
94
|
-
[DisconnectType.SHUTDOWN]: undefined,
|
|
95
|
-
};
|
|
96
|
-
const error = ERROR_MAP[reason.type];
|
|
97
|
-
if (error) {
|
|
98
|
-
this.reason = new error(reason.message ?? '');
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
this.addPacketHandler(PACKET_TYPES.READY, () => {
|
|
102
|
-
if (this.status.type === 'ready') {
|
|
103
|
-
throw new Error('Received READY packet when already ready');
|
|
104
|
-
}
|
|
105
|
-
this.setStatus({ type: 'ready' });
|
|
106
|
-
this.attempt = 0;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public setConnection(connection: Connection): void {
|
|
111
|
-
if (this.status.type !== 'disconnected') {
|
|
112
|
-
throw new Error('Cannot change connection while connected');
|
|
113
|
-
}
|
|
114
|
-
this.connection = connection;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public registerPacket(...packetTypes: PacketType<unknown>[]): void {
|
|
118
|
-
this.packetMapper.register(...packetTypes);
|
|
119
|
-
for (const type of packetTypes) {
|
|
120
|
-
this.packetHandlers.set(type.id, {
|
|
121
|
-
type,
|
|
122
|
-
event: new EventEmitter(),
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public addPacketHandler<T>(type: PacketType<T>, handler: (packet: T) => void): void {
|
|
128
|
-
const listeners = this.packetHandlers.get(type.id) as PacketHandler<unknown> | undefined;
|
|
129
|
-
if (!listeners) {
|
|
130
|
-
throw new Error(`Packet type ${type.id.key()} not registered`);
|
|
131
|
-
}
|
|
132
|
-
listeners.event.listen(handler as (packet: unknown) => void);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private async scheduleReconnect(attempt: number): Promise<boolean> {
|
|
136
|
-
const timeout = Math.min(30000, 2000 * (Math.pow(1.5, attempt) - 1) + 2000);
|
|
137
|
-
const date = new Date();
|
|
138
|
-
let cancelled = false;
|
|
139
|
-
const status: Extract<NetworkStatus, { type: 'reconnecting' }> = {
|
|
140
|
-
type: 'reconnecting',
|
|
141
|
-
attempt,
|
|
142
|
-
timeout,
|
|
143
|
-
date,
|
|
144
|
-
cancel: () => {cancelled = true;},
|
|
145
|
-
};
|
|
146
|
-
await this.setStatus(status);
|
|
147
|
-
if (cancelled) {
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
const remaining = status.timeout - (new Date().getTime() - date.getTime());
|
|
151
|
-
await new Promise((resolve) => setTimeout(resolve, remaining));
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
public async connect(reconnect = true): Promise<void> {
|
|
156
|
-
if (!this.isState('disconnected')) {
|
|
157
|
-
throw new Error(`Cannot connect while ${this.status.type}`);
|
|
158
|
-
}
|
|
159
|
-
if (this.listenTask) {
|
|
160
|
-
throw new Error('Cannot connect while already connecting');
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
this.attempt = 0;
|
|
164
|
-
while (this.omu.running) {
|
|
165
|
-
this.attempt++;
|
|
166
|
-
try {
|
|
167
|
-
this.aes = undefined;
|
|
168
|
-
this.connection = await this.transport.connect();
|
|
169
|
-
await this.setStatus({ type: 'connecting' });
|
|
170
|
-
const metaReceived = await this.connection.receive(this.packetMapper);
|
|
171
|
-
if (!metaReceived) {
|
|
172
|
-
throw new Error('Connection closed before receiving server meta');
|
|
173
|
-
}
|
|
174
|
-
if (!PACKET_TYPES.SERVER_META.match(metaReceived)) {
|
|
175
|
-
throw new Error('First packet received was not server meta');
|
|
176
|
-
}
|
|
177
|
-
const { data: meta } = metaReceived;
|
|
178
|
-
this.address.hash = meta.hash;
|
|
179
|
-
let token = await this.tokenProvider.get(this.address, this.omu.app);
|
|
180
|
-
if (!token) {
|
|
181
|
-
throw new InvalidToken(`No token available for app ${this.omu.app.id.key()} at ${this.address.host}:${this.address.port}:${this.address.hash}`);
|
|
182
|
-
}
|
|
183
|
-
let encryptionResp: EncryptionResponse | undefined = undefined;
|
|
184
|
-
if (this.omu.app.type === 'remote' && ENCRYPTION_AVAILABLE && meta.encryption) {
|
|
185
|
-
const decryptor = await Decryptor.new();
|
|
186
|
-
const encryptor = await Encryptor.new(meta.encryption.rsa);
|
|
187
|
-
if (token) {
|
|
188
|
-
token = await encryptor.encryptString(token);
|
|
189
|
-
}
|
|
190
|
-
this.aes = await AES.new();
|
|
191
|
-
encryptionResp = {
|
|
192
|
-
kind: 'v1',
|
|
193
|
-
rsa: await decryptor.toRequest(),
|
|
194
|
-
aes: await this.aes.serialize(encryptor),
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
this.connection.send({
|
|
198
|
-
type: PACKET_TYPES.CONNECT,
|
|
199
|
-
data: new ConnectPacket({
|
|
200
|
-
app: this.omu.app,
|
|
201
|
-
protocol: { version: VERSION },
|
|
202
|
-
encryption: encryptionResp,
|
|
203
|
-
token,
|
|
204
|
-
}),
|
|
205
|
-
}, this.packetMapper);
|
|
206
|
-
this.listenTask = this.listen();
|
|
207
|
-
await this.setStatus({ type: 'connected' });
|
|
208
|
-
await this.event.connected.emit();
|
|
209
|
-
this.dispatchTasks().then(() => {
|
|
210
|
-
this.send({
|
|
211
|
-
type: PACKET_TYPES.READY,
|
|
212
|
-
data: null,
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
await this.listenTask;
|
|
216
|
-
} catch (error) {
|
|
217
|
-
if (error instanceof DisconnectReason) {
|
|
218
|
-
this.reason = error;
|
|
219
|
-
}
|
|
220
|
-
if (!reconnect) {
|
|
221
|
-
throw error;
|
|
222
|
-
}
|
|
223
|
-
} finally {
|
|
224
|
-
this.event.disconnected.emit(this.reason);
|
|
225
|
-
if (this.status.type !== 'disconnected' && this.status.type !== 'reconnecting') {
|
|
226
|
-
this.setStatus({ type: 'disconnected', attempt: this.attempt, reason: this.reason });
|
|
227
|
-
}
|
|
228
|
-
this.connection?.close();
|
|
229
|
-
this.connection = undefined;
|
|
230
|
-
this.listenTask = undefined;
|
|
231
|
-
}
|
|
232
|
-
if (this.reason && ![
|
|
233
|
-
DisconnectType.SHUTDOWN,
|
|
234
|
-
DisconnectType.SERVER_RESTART,
|
|
235
|
-
].includes(this.reason.type)) {
|
|
236
|
-
break;
|
|
237
|
-
}
|
|
238
|
-
this.reason = undefined;
|
|
239
|
-
if (!reconnect) {
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
const shouldReconnect = await this.scheduleReconnect(this.attempt);
|
|
243
|
-
if (!shouldReconnect) {
|
|
244
|
-
break;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
await this.setStatus({ type: 'disconnected', reason: this.reason });
|
|
248
|
-
this.connection?.close();
|
|
249
|
-
this.connection = undefined;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
private isState(state: NetworkStatus['type']): boolean {
|
|
253
|
-
return this.status.type === state;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
public async send(packet: Packet): Promise<void> {
|
|
257
|
-
if (!this.connection) {
|
|
258
|
-
throw new Error('No connection established');
|
|
259
|
-
}
|
|
260
|
-
if (this.aes) {
|
|
261
|
-
const serialized = this.packetMapper.serialize(packet);
|
|
262
|
-
packet = await this.aes.encrypt(serialized);
|
|
263
|
-
}
|
|
264
|
-
this.connection.send(packet, this.packetMapper);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
private async listen(): Promise<void> {
|
|
268
|
-
while (this.connection) {
|
|
269
|
-
const packet = await this.connection.receive(this.packetMapper);
|
|
270
|
-
if (!packet) {
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
await this.dispatchPacket(packet);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
private async dispatchPacket(packet: Packet): Promise<void> {
|
|
278
|
-
if (PACKET_TYPES.ENCRYPTED_PACKET.match(packet)) {
|
|
279
|
-
if (!this.aes) {
|
|
280
|
-
throw new Error('Received encrypted packet before encryption was established');
|
|
281
|
-
}
|
|
282
|
-
const decrypted = await this.aes.decrypt(packet);
|
|
283
|
-
packet = this.packetMapper.deserialize(decrypted);
|
|
284
|
-
}
|
|
285
|
-
await this.event.packet.emit(packet);
|
|
286
|
-
const packetHandler = this.packetHandlers.get(packet.type.id);
|
|
287
|
-
if (!packetHandler) {
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
await packetHandler.event.emit(packet.data);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
public addTask(task: () => Promise<void> | void): void {
|
|
294
|
-
if (this.omu.running) {
|
|
295
|
-
throw new Error('Cannot add task after client is ready');
|
|
296
|
-
}
|
|
297
|
-
this.tasks.push(task);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
public removeTask(task: () => Promise<void> | void): void {
|
|
301
|
-
this.tasks.splice(this.tasks.indexOf(task), 1);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
private async dispatchTasks(): Promise<void> {
|
|
305
|
-
for (const task of this.tasks) {
|
|
306
|
-
await task();
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
private async setStatus(status: NetworkStatus): Promise<void> {
|
|
311
|
-
if (this.status === status) {
|
|
312
|
-
throw new Error(`Cannot set status to ${status} when already ${status}`);
|
|
313
|
-
}
|
|
314
|
-
this.status = status;
|
|
315
|
-
await this.event.status.emit(status);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
public close(): void {
|
|
319
|
-
this.setStatus({ type: 'disconnected' });
|
|
320
|
-
this.attempt = 0;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import type { AppJson } from '../../app.js';
|
|
2
|
-
import { App } from '../../app.js';
|
|
3
|
-
import { Identifier } from '../../identifier';
|
|
4
|
-
import { Serializer } from '../../serialize/serializer.js';
|
|
5
|
-
import { PacketType } from './packet.js';
|
|
6
|
-
|
|
7
|
-
const IDENTIFIER = new Identifier('core', 'packet');
|
|
8
|
-
|
|
9
|
-
type ProtocolInfo = {
|
|
10
|
-
version: string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
type RSANumbers = {
|
|
14
|
-
e: string;
|
|
15
|
-
n: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
type EncryptionRequest = {
|
|
19
|
-
kind: 'v1';
|
|
20
|
-
rsa: RSANumbers;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
type EncryptionResponse = {
|
|
24
|
-
kind: 'v1';
|
|
25
|
-
rsa: RSANumbers;
|
|
26
|
-
aes: string;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export type ServerMeta = {
|
|
30
|
-
protocol: ProtocolInfo;
|
|
31
|
-
encryption: EncryptionRequest | null;
|
|
32
|
-
hash: string | undefined;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
type ConnectPacketJson = {
|
|
36
|
-
protocol: ProtocolInfo;
|
|
37
|
-
app: AppJson;
|
|
38
|
-
token: string;
|
|
39
|
-
encryption: EncryptionResponse | null;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export class ConnectPacket {
|
|
43
|
-
public readonly protocol: ProtocolInfo;
|
|
44
|
-
public readonly app: App;
|
|
45
|
-
public readonly token: string;
|
|
46
|
-
public readonly encryption: EncryptionResponse | null;
|
|
47
|
-
|
|
48
|
-
constructor(options: {
|
|
49
|
-
protocol: ProtocolInfo;
|
|
50
|
-
app: App;
|
|
51
|
-
token: string;
|
|
52
|
-
encryption?: EncryptionResponse | null;
|
|
53
|
-
}) {
|
|
54
|
-
this.protocol = options.protocol;
|
|
55
|
-
this.app = options.app;
|
|
56
|
-
this.token = options.token;
|
|
57
|
-
this.encryption = options.encryption ?? null;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public static deserialize(data: ConnectPacketJson): ConnectPacket {
|
|
61
|
-
return new ConnectPacket({
|
|
62
|
-
protocol: data.protocol,
|
|
63
|
-
app: App.deserialize(data.app),
|
|
64
|
-
encryption: data.encryption,
|
|
65
|
-
token: data.token,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
public static serialize(data: ConnectPacket): ConnectPacketJson {
|
|
70
|
-
return {
|
|
71
|
-
protocol: data.protocol,
|
|
72
|
-
app: App.serialize(data.app),
|
|
73
|
-
encryption: data.encryption,
|
|
74
|
-
token: data.token,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export enum DisconnectType {
|
|
80
|
-
INVALID_TOKEN = 'invalid_token',
|
|
81
|
-
INVALID_ORIGIN = 'invalid_origin',
|
|
82
|
-
INVALID_VERSION = 'invalid_version',
|
|
83
|
-
INVALID_PACKET_TYPE = 'invalid_packet_type',
|
|
84
|
-
INVALID_PACKET_DATA = 'invalid_packet_data',
|
|
85
|
-
INVALID_PACKET = 'invalid_packet',
|
|
86
|
-
INTERNAL_ERROR = 'internal_error',
|
|
87
|
-
ANOTHER_CONNECTION = 'another_connection',
|
|
88
|
-
PERMISSION_DENIED = 'permission_denied',
|
|
89
|
-
APP_REMOVED = 'app_removed',
|
|
90
|
-
SERVER_RESTART = 'server_restart',
|
|
91
|
-
SHUTDOWN = 'shutdown',
|
|
92
|
-
CLOSE = 'close',
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
type DisconnectPacketJson = {
|
|
96
|
-
type: DisconnectType;
|
|
97
|
-
message: string | null;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export class DisconnectPacket {
|
|
101
|
-
public readonly type: DisconnectType;
|
|
102
|
-
public readonly message: string | null;
|
|
103
|
-
|
|
104
|
-
constructor(options: {
|
|
105
|
-
type: DisconnectType;
|
|
106
|
-
message?: string | null;
|
|
107
|
-
}) {
|
|
108
|
-
this.type = options.type;
|
|
109
|
-
this.message = options.message ?? null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
public static serialize(data: DisconnectPacket): DisconnectPacketJson {
|
|
113
|
-
return data.toJson();
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public static deserialize(data: DisconnectPacketJson): DisconnectPacket {
|
|
117
|
-
return DisconnectPacket.fromJson(data);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
static fromJson(data: DisconnectPacketJson): DisconnectPacket {
|
|
121
|
-
return new DisconnectPacket({
|
|
122
|
-
type: data.type,
|
|
123
|
-
message: data.message,
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
toJson(): DisconnectPacketJson {
|
|
128
|
-
return {
|
|
129
|
-
type: this.type,
|
|
130
|
-
message: this.message,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export const PACKET_TYPES: {
|
|
136
|
-
SERVER_META: PacketType<ServerMeta>;
|
|
137
|
-
CONNECT: PacketType<ConnectPacket>;
|
|
138
|
-
DISCONNECT: PacketType<DisconnectPacket>;
|
|
139
|
-
TOKEN: PacketType<string>;
|
|
140
|
-
READY: PacketType<undefined>;
|
|
141
|
-
ENCRYPTED_PACKET: PacketType<Uint8Array>;
|
|
142
|
-
} = {
|
|
143
|
-
SERVER_META: PacketType.createJson<ServerMeta>(IDENTIFIER, {
|
|
144
|
-
name: 'server_meta',
|
|
145
|
-
}),
|
|
146
|
-
CONNECT: PacketType.createJson<ConnectPacket>(IDENTIFIER, {
|
|
147
|
-
name: 'connect',
|
|
148
|
-
serializer: ConnectPacket,
|
|
149
|
-
}),
|
|
150
|
-
DISCONNECT: PacketType.createJson<DisconnectPacket>(IDENTIFIER, {
|
|
151
|
-
name: 'disconnect',
|
|
152
|
-
serializer: DisconnectPacket,
|
|
153
|
-
}),
|
|
154
|
-
TOKEN: PacketType.createJson<string>(IDENTIFIER, {
|
|
155
|
-
name: 'token',
|
|
156
|
-
}),
|
|
157
|
-
READY: PacketType.createJson<undefined>(IDENTIFIER, {
|
|
158
|
-
name: 'ready',
|
|
159
|
-
}),
|
|
160
|
-
ENCRYPTED_PACKET: PacketType.createSerialized<Uint8Array>(IDENTIFIER, {
|
|
161
|
-
name: 'e',
|
|
162
|
-
serializer: Serializer.noop(),
|
|
163
|
-
}),
|
|
164
|
-
};
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { Identifier } from '../../identifier';
|
|
2
|
-
import { JsonType, type Serializable, Serializer } from '../../serialize';
|
|
3
|
-
|
|
4
|
-
export type PacketData = {
|
|
5
|
-
readonly type: string;
|
|
6
|
-
readonly data: Uint8Array;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export type Packet<T = unknown> = {
|
|
10
|
-
readonly type: PacketType<T>;
|
|
11
|
-
readonly data: T;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export class PacketType<T> {
|
|
15
|
-
constructor(
|
|
16
|
-
public readonly id: Identifier,
|
|
17
|
-
public readonly serializer: Serializable<T, Uint8Array>,
|
|
18
|
-
) { }
|
|
19
|
-
|
|
20
|
-
public new(data: T): Packet<T> {
|
|
21
|
-
return {
|
|
22
|
-
type: this,
|
|
23
|
-
data,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
public match(packet: Packet): packet is Packet<T> {
|
|
27
|
-
return packet.type === this;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static createJson<T, D extends JsonType = JsonType>(identifier: Identifier, {
|
|
31
|
-
name,
|
|
32
|
-
serializer,
|
|
33
|
-
}: {
|
|
34
|
-
name: string;
|
|
35
|
-
serializer?: Serializable<T, D>;
|
|
36
|
-
}): PacketType<T> {
|
|
37
|
-
return new PacketType<T>(
|
|
38
|
-
identifier.join(name),
|
|
39
|
-
Serializer.pipe<T, D, Uint8Array>(serializer ?? Serializer.noop(), Serializer.json()),
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
static createSerialized<T>(identifier: Identifier, {
|
|
44
|
-
name,
|
|
45
|
-
serializer,
|
|
46
|
-
}: {
|
|
47
|
-
name: string;
|
|
48
|
-
serializer: Serializable<T, Uint8Array>;
|
|
49
|
-
}): PacketType<T> {
|
|
50
|
-
return new PacketType(
|
|
51
|
-
identifier.join(name),
|
|
52
|
-
serializer,
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import type { Address } from '../address.js';
|
|
2
|
-
import type { Serializable } from '../serialize';
|
|
3
|
-
import { ByteReader, ByteWriter } from '../serialize';
|
|
4
|
-
|
|
5
|
-
import type { Connection, Transport } from './connection.js';
|
|
6
|
-
import type { Packet, PacketData } from './packet/packet.js';
|
|
7
|
-
|
|
8
|
-
export class WebsocketTransport implements Transport {
|
|
9
|
-
constructor(
|
|
10
|
-
private readonly address: Address,
|
|
11
|
-
) { }
|
|
12
|
-
|
|
13
|
-
private get wsEndpoint(): string {
|
|
14
|
-
const protocol = this.address.secure ? 'wss' : 'ws';
|
|
15
|
-
const { host, port } = this.address;
|
|
16
|
-
return `${protocol}://${host}:${port}/ws`;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public connect(): Promise<Connection> {
|
|
20
|
-
const socket = new WebSocket(this.wsEndpoint);
|
|
21
|
-
return WebsocketConnection.fromSocket(socket);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class WebsocketConnection implements Connection {
|
|
26
|
-
private readonly packetQueue: Array<PacketData> = [];
|
|
27
|
-
private receiveWaiter: (() => void) | null = null;
|
|
28
|
-
private readingPromise: Promise<void> | null = null;
|
|
29
|
-
private beingClosed: boolean = false;
|
|
30
|
-
|
|
31
|
-
constructor(
|
|
32
|
-
private socket: WebSocket,
|
|
33
|
-
) { }
|
|
34
|
-
|
|
35
|
-
public static fromSocket(socket: WebSocket): Promise<WebsocketConnection> {
|
|
36
|
-
const connection = new WebsocketConnection(socket);
|
|
37
|
-
return new Promise((resolve, reject) => {
|
|
38
|
-
socket.onerror = reject;
|
|
39
|
-
socket.onclose = () => {
|
|
40
|
-
connection.beingClosed = true;
|
|
41
|
-
if (connection.receiveWaiter) {
|
|
42
|
-
connection.receiveWaiter();
|
|
43
|
-
connection.receiveWaiter = null;
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
socket.onmessage = (event): void => {
|
|
47
|
-
connection.readingPromise = connection.onMessage(event).finally(() => {
|
|
48
|
-
connection.readingPromise = null;
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
socket.onopen = (): void => {
|
|
52
|
-
resolve(connection);
|
|
53
|
-
};
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private async onMessage(event: MessageEvent<string | Blob>): Promise<void> {
|
|
58
|
-
if (typeof event.data === 'string') {
|
|
59
|
-
throw new Error('Received string data');
|
|
60
|
-
}
|
|
61
|
-
const reader = await ByteReader.fromBlob(event.data);
|
|
62
|
-
const data = {
|
|
63
|
-
type: reader.readString(),
|
|
64
|
-
data: reader.readUint8Array(),
|
|
65
|
-
};
|
|
66
|
-
this.packetQueue.push(data);
|
|
67
|
-
reader.finish();
|
|
68
|
-
if (this.receiveWaiter) {
|
|
69
|
-
this.receiveWaiter();
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public async receive(serializer: Serializable<Packet<unknown>, PacketData>): Promise<Packet | null> {
|
|
74
|
-
if (this.receiveWaiter) {
|
|
75
|
-
throw new Error('Already receiving');
|
|
76
|
-
}
|
|
77
|
-
while (this.packetQueue.length === 0 && !this.beingClosed) {
|
|
78
|
-
await new Promise<void>((resolve) => {
|
|
79
|
-
this.receiveWaiter = resolve;
|
|
80
|
-
});
|
|
81
|
-
this.receiveWaiter = null;
|
|
82
|
-
}
|
|
83
|
-
if (this.packetQueue.length === 0 && this.beingClosed) {
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
const packetData = this.packetQueue.shift()!;
|
|
87
|
-
return serializer.deserialize(packetData);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
get closed(): boolean {
|
|
91
|
-
if (this.readingPromise) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
if (this.packetQueue.length > 0) {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
if (this.receiveWaiter) {
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
if (this.socket.readyState !== WebSocket.CLOSED && this.socket.readyState !== WebSocket.CLOSING) {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
close(): void {
|
|
107
|
-
if (this.socket) {
|
|
108
|
-
this.socket.close();
|
|
109
|
-
}
|
|
110
|
-
if (this.receiveWaiter) {
|
|
111
|
-
this.receiveWaiter();
|
|
112
|
-
this.receiveWaiter = null;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
send(packet: Packet, serializer: Serializable<Packet<unknown>, PacketData>): void {
|
|
117
|
-
const packetData = serializer.serialize(packet);
|
|
118
|
-
const writer = new ByteWriter();
|
|
119
|
-
writer.writeString(packetData.type);
|
|
120
|
-
writer.writeUint8Array(packetData.data);
|
|
121
|
-
this.socket.send(writer.finish());
|
|
122
|
-
}
|
|
123
|
-
}
|