@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/lock.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
type Task = () => Promise<void>;
|
|
3
|
-
|
|
4
|
-
export class Lock {
|
|
5
|
-
private tasks: (Task)[] = [];
|
|
6
|
-
private waiter: Promise<void> | null = null;
|
|
7
|
-
|
|
8
|
-
private wait(): Promise<void> {
|
|
9
|
-
if (this.waiter) {
|
|
10
|
-
return this.waiter;
|
|
11
|
-
}
|
|
12
|
-
this.waiter = new Promise<void>((resolve) => {
|
|
13
|
-
const runTasks = async () => {
|
|
14
|
-
while (this.tasks.length > 0) {
|
|
15
|
-
const task = this.tasks.shift();
|
|
16
|
-
if (task) {
|
|
17
|
-
await task();
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
this.waiter = null;
|
|
21
|
-
resolve();
|
|
22
|
-
};
|
|
23
|
-
runTasks();
|
|
24
|
-
});
|
|
25
|
-
return this.waiter;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public async use<T>(callback: () => Promise<T>): Promise<T> {
|
|
29
|
-
const promise = new Promise<T>((resolve, reject) => {
|
|
30
|
-
const task = async () => {
|
|
31
|
-
const promise = callback();
|
|
32
|
-
try {
|
|
33
|
-
resolve(await promise);
|
|
34
|
-
} catch (reason) {
|
|
35
|
-
reject(reason);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
this.tasks.push(task);
|
|
39
|
-
});
|
|
40
|
-
await this.wait();
|
|
41
|
-
return promise;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { Identifier, IdentifierMap } from '../identifier';
|
|
2
|
-
import type { Serializable } from '../serialize';
|
|
3
|
-
|
|
4
|
-
import type { Packet, PacketData, PacketType } from './packet';
|
|
5
|
-
|
|
6
|
-
export class PacketMapper implements Serializable<Packet, PacketData> {
|
|
7
|
-
private readonly packetMap = new IdentifierMap<PacketType<unknown>>();
|
|
8
|
-
|
|
9
|
-
public register(...packetTypes: PacketType<unknown>[]): void {
|
|
10
|
-
for (const type of packetTypes) {
|
|
11
|
-
if (this.packetMap.has(type.id)) {
|
|
12
|
-
throw new Error(`Packet id ${type.id.key()} already registered`);
|
|
13
|
-
}
|
|
14
|
-
this.packetMap.set(type.id, type);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
serialize(packet: Packet): PacketData {
|
|
19
|
-
return {
|
|
20
|
-
type: packet.type.id.key(),
|
|
21
|
-
data: packet.type.serializer.serialize(packet.data),
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
deserialize(data: PacketData): Packet {
|
|
26
|
-
const identifier = Identifier.fromKey(data.type);
|
|
27
|
-
const type = this.packetMap.get(identifier);
|
|
28
|
-
if (!type) throw new Error(`Packet type ${data.type} not registered`);
|
|
29
|
-
return {
|
|
30
|
-
type,
|
|
31
|
-
data: type.serializer.deserialize(data.data),
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface Transport {
|
|
37
|
-
connect(): Promise<Connection>;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface Connection {
|
|
41
|
-
send(packet: Packet, serializer: Serializable<Packet, PacketData>): void;
|
|
42
|
-
receive(serializer: Serializable<Packet, PacketData>): Promise<Packet | null>;
|
|
43
|
-
close(): void;
|
|
44
|
-
closed: boolean;
|
|
45
|
-
}
|
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import { textDecoder, textEncoder } from '../const';
|
|
2
|
-
import { ByteReader, ByteWriter } from '../serialize';
|
|
3
|
-
import { Packet, PacketData } from './packet';
|
|
4
|
-
import { PACKET_TYPES } from './packet/packet-types';
|
|
5
|
-
|
|
6
|
-
function base64UrlEncode(buffer: Uint8Array): string {
|
|
7
|
-
return buffer
|
|
8
|
-
.toBase64()
|
|
9
|
-
.replace(/\+/g, '-')
|
|
10
|
-
.replace(/\//g, '_')
|
|
11
|
-
.replace(/=/g, '');
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function base64UrlDecode(data: string): Uint8Array {
|
|
15
|
-
data = data.replace(/-/g, '+').replace(/_/g, '/');
|
|
16
|
-
while (data.length % 4) {
|
|
17
|
-
data += '=';
|
|
18
|
-
}
|
|
19
|
-
return Uint8Array.fromBase64(data);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function convertUint8ArrayToArrayBuffer(u8: Uint8Array): ArrayBuffer {
|
|
23
|
-
const buf = new ArrayBuffer(u8.length);
|
|
24
|
-
const view = new Uint8Array(buf);
|
|
25
|
-
view.set(u8);
|
|
26
|
-
return buf;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
type RSANumbers = {
|
|
30
|
-
e: string;
|
|
31
|
-
n: string;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type EncryptionResponse = {
|
|
35
|
-
kind: 'v1';
|
|
36
|
-
rsa: RSANumbers;
|
|
37
|
-
aes: string;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export const ENCRYPTION_AVAILABLE: boolean = typeof crypto !== 'undefined' && !!crypto.subtle;
|
|
41
|
-
|
|
42
|
-
export class Decryptor {
|
|
43
|
-
private constructor(
|
|
44
|
-
private readonly privateKey: CryptoKey,
|
|
45
|
-
private readonly publicKey: CryptoKey,
|
|
46
|
-
) {}
|
|
47
|
-
|
|
48
|
-
public static async new(): Promise<Decryptor> {
|
|
49
|
-
const { privateKey, publicKey } = await crypto.subtle.generateKey(
|
|
50
|
-
{
|
|
51
|
-
name: 'RSA-OAEP',
|
|
52
|
-
modulusLength: 4096,
|
|
53
|
-
publicExponent: new Uint8Array([1, 0, 1]),
|
|
54
|
-
hash: 'SHA-256',
|
|
55
|
-
},
|
|
56
|
-
true,
|
|
57
|
-
['encrypt', 'decrypt'],
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
return new Decryptor(privateKey, publicKey);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public async toRequest(): Promise<RSANumbers> {
|
|
64
|
-
const key = await crypto.subtle.exportKey('jwk', this.publicKey);
|
|
65
|
-
if (!key.e) throw new Error('Key export failed: missing exponent');
|
|
66
|
-
if (!key.n) throw new Error('Key export failed: missing modulus');
|
|
67
|
-
return {
|
|
68
|
-
e: key.e,
|
|
69
|
-
n: key.n,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public async decrypt(data: ArrayBuffer): Promise<ArrayBuffer> {
|
|
74
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
75
|
-
{ name: 'RSA-OAEP' },
|
|
76
|
-
this.privateKey,
|
|
77
|
-
data,
|
|
78
|
-
);
|
|
79
|
-
return decrypted;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public async decryptString(data: string): Promise<string> {
|
|
83
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
84
|
-
{ name: 'RSA-OAEP' },
|
|
85
|
-
this.privateKey,
|
|
86
|
-
convertUint8ArrayToArrayBuffer(base64UrlDecode(data)),
|
|
87
|
-
);
|
|
88
|
-
return textDecoder.decode(decrypted);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export class Encryptor {
|
|
93
|
-
private constructor(
|
|
94
|
-
private readonly publicKey: CryptoKey,
|
|
95
|
-
) {}
|
|
96
|
-
|
|
97
|
-
public static async new(request: RSANumbers): Promise<Encryptor> {
|
|
98
|
-
const publicKey = await crypto.subtle.importKey(
|
|
99
|
-
'jwk',
|
|
100
|
-
{
|
|
101
|
-
alg: 'RSA-OAEP-256',
|
|
102
|
-
ext: true,
|
|
103
|
-
key_ops: ['encrypt'],
|
|
104
|
-
kty: 'RSA',
|
|
105
|
-
e: request.e.replace(/=+$/, ''),
|
|
106
|
-
n: request.n.replace(/=+$/, ''),
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
name: 'RSA-OAEP',
|
|
110
|
-
hash: 'SHA-256',
|
|
111
|
-
},
|
|
112
|
-
true,
|
|
113
|
-
['encrypt'],
|
|
114
|
-
);
|
|
115
|
-
return new Encryptor(publicKey);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public async encrypt(data: ArrayBuffer): Promise<ArrayBuffer> {
|
|
119
|
-
return await crypto.subtle.encrypt(
|
|
120
|
-
{
|
|
121
|
-
name: 'RSA-OAEP',
|
|
122
|
-
},
|
|
123
|
-
this.publicKey,
|
|
124
|
-
data,
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
public async encryptString(data: string): Promise<string> {
|
|
129
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
130
|
-
{
|
|
131
|
-
name: 'RSA-OAEP',
|
|
132
|
-
},
|
|
133
|
-
this.publicKey,
|
|
134
|
-
textEncoder.encode(data),
|
|
135
|
-
);
|
|
136
|
-
return base64UrlEncode(new Uint8Array(encrypted));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export class AES {
|
|
141
|
-
private constructor(
|
|
142
|
-
private readonly key: CryptoKey,
|
|
143
|
-
private readonly iv: ArrayBuffer,
|
|
144
|
-
) {}
|
|
145
|
-
|
|
146
|
-
public static async new(): Promise<AES> {
|
|
147
|
-
const key = await crypto.subtle.generateKey(
|
|
148
|
-
{
|
|
149
|
-
name: 'AES-CBC',
|
|
150
|
-
length: 128,
|
|
151
|
-
},
|
|
152
|
-
true,
|
|
153
|
-
['encrypt', 'decrypt'],
|
|
154
|
-
);
|
|
155
|
-
const iv = crypto.getRandomValues(new Uint8Array(16));
|
|
156
|
-
return new AES(key, iv.buffer);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
public static async deserialize(aes: string, decryptor: Decryptor): Promise<AES> {
|
|
160
|
-
const decrypted = await decryptor.decrypt(convertUint8ArrayToArrayBuffer(base64UrlDecode(aes)));
|
|
161
|
-
const reader = ByteReader.fromArrayBuffer(decrypted);
|
|
162
|
-
const key = reader.readUint8Array();
|
|
163
|
-
const iv = reader.readUint8Array();
|
|
164
|
-
|
|
165
|
-
const key_encoded = await crypto.subtle.importKey(
|
|
166
|
-
'raw',
|
|
167
|
-
convertUint8ArrayToArrayBuffer(key),
|
|
168
|
-
'AES-CBC',
|
|
169
|
-
false,
|
|
170
|
-
['encrypt', 'decrypt'],
|
|
171
|
-
);
|
|
172
|
-
return new AES(
|
|
173
|
-
key_encoded,
|
|
174
|
-
convertUint8ArrayToArrayBuffer(iv),
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
public async serialize(encryptor: Encryptor): Promise<string> {
|
|
179
|
-
const writer = new ByteWriter();
|
|
180
|
-
const rawKey = await crypto.subtle.exportKey('raw', this.key);
|
|
181
|
-
writer.writeUint8Array(new Uint8Array(rawKey));
|
|
182
|
-
writer.writeUint8Array(new Uint8Array(this.iv));
|
|
183
|
-
const encrypted = await encryptor.encrypt(convertUint8ArrayToArrayBuffer(writer.finish()));
|
|
184
|
-
return base64UrlEncode(new Uint8Array(encrypted));
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
public async encrypt(data: PacketData): Promise<Packet> {
|
|
188
|
-
const writer = new ByteWriter();
|
|
189
|
-
writer.writeString(data.type);
|
|
190
|
-
writer.writeUint8Array(data.data);
|
|
191
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
192
|
-
{
|
|
193
|
-
name: 'AES-CBC',
|
|
194
|
-
iv: this.iv,
|
|
195
|
-
length: 16,
|
|
196
|
-
},
|
|
197
|
-
this.key,
|
|
198
|
-
convertUint8ArrayToArrayBuffer(writer.finish()),
|
|
199
|
-
);
|
|
200
|
-
return PACKET_TYPES.ENCRYPTED_PACKET.new(new Uint8Array(encrypted));
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
public async decrypt(packet: Packet<Uint8Array>): Promise<PacketData> {
|
|
204
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
205
|
-
{
|
|
206
|
-
name: 'AES-CBC',
|
|
207
|
-
iv: this.iv,
|
|
208
|
-
length: 16,
|
|
209
|
-
},
|
|
210
|
-
this.key,
|
|
211
|
-
convertUint8ArrayToArrayBuffer(packet.data),
|
|
212
|
-
);
|
|
213
|
-
const reader = ByteReader.fromArrayBuffer(decrypted);
|
|
214
|
-
const type = reader.readString();
|
|
215
|
-
const data = reader.readUint8Array();
|
|
216
|
-
return {
|
|
217
|
-
type,
|
|
218
|
-
data,
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
}
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
|
|
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
|
-
const FRAME_TYPE_KEY = 'omuapps-frame';
|
|
9
|
-
|
|
10
|
-
type Command = ({
|
|
11
|
-
type: 'syn';
|
|
12
|
-
payload: {
|
|
13
|
-
origin: string;
|
|
14
|
-
};
|
|
15
|
-
} | {
|
|
16
|
-
type: 'connect';
|
|
17
|
-
payload: object;
|
|
18
|
-
} | {
|
|
19
|
-
type: 'disconnect';
|
|
20
|
-
payload: object;
|
|
21
|
-
} | {
|
|
22
|
-
type: 'send';
|
|
23
|
-
payload: Uint8Array;
|
|
24
|
-
} | {
|
|
25
|
-
type: 'receive';
|
|
26
|
-
payload: Uint8Array;
|
|
27
|
-
} | {
|
|
28
|
-
type: 'error';
|
|
29
|
-
payload: string;
|
|
30
|
-
}) & {
|
|
31
|
-
type_key?: string;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export class FrameTransport implements Transport {
|
|
35
|
-
public connect(): Promise<FrameConnection> {
|
|
36
|
-
return FrameConnection.create();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export class FrameConnection implements Connection {
|
|
41
|
-
public connected: boolean;
|
|
42
|
-
private origin: string | null = null;
|
|
43
|
-
private readonly packetQueue: Array<PacketData> = [];
|
|
44
|
-
private readonly sendQueue: Array<Command> = [];
|
|
45
|
-
private receiveWaiter: (() => void) | null = null;
|
|
46
|
-
|
|
47
|
-
constructor(
|
|
48
|
-
private connectedWaiter: (() => void) | null,
|
|
49
|
-
) {
|
|
50
|
-
this.connected = false;
|
|
51
|
-
window.addEventListener('message', (event) => {
|
|
52
|
-
if (event.data === null || typeof event.data !== 'object') {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
if (!('type_key' in event.data) || event.data.type_key !== FRAME_TYPE_KEY) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const command = event.data as Command;
|
|
59
|
-
if (command.type === 'syn') {
|
|
60
|
-
this.origin = event.origin;
|
|
61
|
-
}
|
|
62
|
-
if (event.origin !== this.origin) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
this.handleCommand(command);
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
public static create(): Promise<FrameConnection> {
|
|
70
|
-
return new Promise<FrameConnection>((resolve) => {
|
|
71
|
-
const connection = new FrameConnection((): void => resolve(connection));
|
|
72
|
-
connection.postCommand({
|
|
73
|
-
type: 'connect',
|
|
74
|
-
payload: {},
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
private handleCommand(command: Command): void {
|
|
80
|
-
const { type, payload } = command;
|
|
81
|
-
switch (type) {
|
|
82
|
-
case 'syn':
|
|
83
|
-
this.origin = payload.origin;
|
|
84
|
-
this.postCommand({
|
|
85
|
-
type: 'syn',
|
|
86
|
-
payload: {
|
|
87
|
-
origin: window.origin,
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
break;
|
|
91
|
-
case 'connect':
|
|
92
|
-
this.connected = true;
|
|
93
|
-
if (this.connectedWaiter) {
|
|
94
|
-
this.connectedWaiter();
|
|
95
|
-
this.connectedWaiter = null;
|
|
96
|
-
}
|
|
97
|
-
break;
|
|
98
|
-
case 'disconnect':
|
|
99
|
-
this.connected = false;
|
|
100
|
-
break;
|
|
101
|
-
case 'receive': {
|
|
102
|
-
const reader = ByteReader.fromUint8Array(payload);
|
|
103
|
-
this.packetQueue.push({
|
|
104
|
-
type: reader.readString(),
|
|
105
|
-
data: reader.readUint8Array(),
|
|
106
|
-
});
|
|
107
|
-
reader.finish();
|
|
108
|
-
if (this.receiveWaiter) {
|
|
109
|
-
this.receiveWaiter();
|
|
110
|
-
}
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
case 'error':
|
|
114
|
-
throw new Error(payload);
|
|
115
|
-
case 'send':
|
|
116
|
-
break;
|
|
117
|
-
default:
|
|
118
|
-
throw new Error(`Unknown command type: ${type}`);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private postCommand(command: Command): void {
|
|
123
|
-
command.type_key = FRAME_TYPE_KEY;
|
|
124
|
-
if (!this.origin) {
|
|
125
|
-
this.sendQueue.push(command);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
for (const queuedCommand of this.sendQueue) {
|
|
129
|
-
window.parent.postMessage(queuedCommand, this.origin);
|
|
130
|
-
}
|
|
131
|
-
this.sendQueue.length = 0;
|
|
132
|
-
window.parent.postMessage(command, this.origin);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
public async receive(serializer: Serializable<Packet<unknown>, PacketData>): Promise<Packet | null> {
|
|
136
|
-
if (this.receiveWaiter) {
|
|
137
|
-
throw new Error('Already receiving');
|
|
138
|
-
}
|
|
139
|
-
if (!this.connected) {
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
while (this.packetQueue.length === 0 && this.connected) {
|
|
143
|
-
await new Promise<void>((resolve) => {
|
|
144
|
-
this.receiveWaiter = (): void => resolve();
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
if (!this.connected) {
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
const packet = serializer.deserialize(this.packetQueue.shift()!);
|
|
151
|
-
this.receiveWaiter = null;
|
|
152
|
-
return packet;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
get closed(): boolean {
|
|
156
|
-
return !this.connected;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
close(): void {
|
|
160
|
-
if (this.connected) {
|
|
161
|
-
this.postCommand({
|
|
162
|
-
type: 'disconnect',
|
|
163
|
-
payload: {},
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
this.connected = false;
|
|
167
|
-
if (this.receiveWaiter) {
|
|
168
|
-
this.receiveWaiter();
|
|
169
|
-
this.receiveWaiter = null;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
send(packet: Packet, serializer: Serializable<Packet<unknown>, PacketData>): void {
|
|
174
|
-
if (!this.connected) {
|
|
175
|
-
throw new Error('Not connected');
|
|
176
|
-
}
|
|
177
|
-
const packetData = serializer.serialize(packet);
|
|
178
|
-
const writer = new ByteWriter();
|
|
179
|
-
writer.writeString(packetData.type);
|
|
180
|
-
writer.writeUint8Array(packetData.data);
|
|
181
|
-
this.postCommand({
|
|
182
|
-
type: 'send',
|
|
183
|
-
payload: writer.finish(),
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
}
|