@mtcute/web 0.8.0
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/LICENSE +8 -0
- package/README.md +53 -0
- package/cjs/client.d.ts +30 -0
- package/cjs/client.js +40 -0
- package/cjs/client.js.map +1 -0
- package/cjs/crypto.d.ts +21 -0
- package/cjs/crypto.js +71 -0
- package/cjs/crypto.js.map +1 -0
- package/cjs/encodings/base64.d.ts +2 -0
- package/cjs/encodings/base64.js +112 -0
- package/cjs/encodings/base64.js.map +1 -0
- package/cjs/encodings/hex.d.ts +2 -0
- package/cjs/encodings/hex.js +69 -0
- package/cjs/encodings/hex.js.map +1 -0
- package/cjs/encodings/utf8.d.ts +3 -0
- package/cjs/encodings/utf8.js +29 -0
- package/cjs/encodings/utf8.js.map +1 -0
- package/cjs/exit-hook.d.ts +1 -0
- package/cjs/exit-hook.js +24 -0
- package/cjs/exit-hook.js.map +1 -0
- package/cjs/idb/driver.d.ts +18 -0
- package/cjs/idb/driver.js +137 -0
- package/cjs/idb/driver.js.map +1 -0
- package/cjs/idb/index.d.ts +22 -0
- package/cjs/idb/index.js +28 -0
- package/cjs/idb/index.js.map +1 -0
- package/cjs/idb/repository/auth-keys.d.ts +14 -0
- package/cjs/idb/repository/auth-keys.js +70 -0
- package/cjs/idb/repository/auth-keys.js.map +1 -0
- package/cjs/idb/repository/kv.d.ts +11 -0
- package/cjs/idb/repository/kv.js +34 -0
- package/cjs/idb/repository/kv.js.map +1 -0
- package/cjs/idb/repository/peers.d.ts +12 -0
- package/cjs/idb/repository/peers.js +38 -0
- package/cjs/idb/repository/peers.js.map +1 -0
- package/cjs/idb/repository/ref-messages.d.ts +12 -0
- package/cjs/idb/repository/ref-messages.js +57 -0
- package/cjs/idb/repository/ref-messages.js.map +1 -0
- package/cjs/idb/utils.d.ts +3 -0
- package/cjs/idb/utils.js +27 -0
- package/cjs/idb/utils.js.map +1 -0
- package/cjs/index.d.ts +7 -0
- package/cjs/index.js +24 -0
- package/cjs/index.js.map +1 -0
- package/cjs/logging.d.ts +1 -0
- package/cjs/logging.js +35 -0
- package/cjs/logging.js.map +1 -0
- package/cjs/package.json +3 -0
- package/cjs/platform.d.ts +21 -0
- package/cjs/platform.js +49 -0
- package/cjs/platform.js.map +1 -0
- package/cjs/wasm.d.ts +2 -0
- package/cjs/wasm.js +36 -0
- package/cjs/wasm.js.map +1 -0
- package/cjs/websocket.d.ts +45 -0
- package/cjs/websocket.js +121 -0
- package/cjs/websocket.js.map +1 -0
- package/cjs/worker.d.ts +10 -0
- package/cjs/worker.js +128 -0
- package/cjs/worker.js.map +1 -0
- package/esm/client.d.ts +30 -0
- package/esm/client.js +35 -0
- package/esm/client.js.map +1 -0
- package/esm/crypto.d.ts +21 -0
- package/esm/crypto.js +67 -0
- package/esm/crypto.js.map +1 -0
- package/esm/encodings/base64.d.ts +2 -0
- package/esm/encodings/base64.js +107 -0
- package/esm/encodings/base64.js.map +1 -0
- package/esm/encodings/hex.d.ts +2 -0
- package/esm/encodings/hex.js +64 -0
- package/esm/encodings/hex.js.map +1 -0
- package/esm/encodings/utf8.d.ts +3 -0
- package/esm/encodings/utf8.js +23 -0
- package/esm/encodings/utf8.js.map +1 -0
- package/esm/exit-hook.d.ts +1 -0
- package/esm/exit-hook.js +20 -0
- package/esm/exit-hook.js.map +1 -0
- package/esm/idb/driver.d.ts +18 -0
- package/esm/idb/driver.js +133 -0
- package/esm/idb/driver.js.map +1 -0
- package/esm/idb/index.d.ts +22 -0
- package/esm/idb/index.js +23 -0
- package/esm/idb/index.js.map +1 -0
- package/esm/idb/repository/auth-keys.d.ts +14 -0
- package/esm/idb/repository/auth-keys.js +66 -0
- package/esm/idb/repository/auth-keys.js.map +1 -0
- package/esm/idb/repository/kv.d.ts +11 -0
- package/esm/idb/repository/kv.js +30 -0
- package/esm/idb/repository/kv.js.map +1 -0
- package/esm/idb/repository/peers.d.ts +12 -0
- package/esm/idb/repository/peers.js +34 -0
- package/esm/idb/repository/peers.js.map +1 -0
- package/esm/idb/repository/ref-messages.d.ts +12 -0
- package/esm/idb/repository/ref-messages.js +53 -0
- package/esm/idb/repository/ref-messages.js.map +1 -0
- package/esm/idb/utils.d.ts +3 -0
- package/esm/idb/utils.js +21 -0
- package/esm/idb/utils.js.map +1 -0
- package/esm/index.d.ts +7 -0
- package/esm/index.js +8 -0
- package/esm/index.js.map +1 -0
- package/esm/logging.d.ts +1 -0
- package/esm/logging.js +31 -0
- package/esm/logging.js.map +1 -0
- package/esm/platform.d.ts +21 -0
- package/esm/platform.js +45 -0
- package/esm/platform.js.map +1 -0
- package/esm/wasm.d.ts +2 -0
- package/esm/wasm.js +32 -0
- package/esm/wasm.js.map +1 -0
- package/esm/websocket.d.ts +45 -0
- package/esm/websocket.js +113 -0
- package/esm/websocket.js.map +1 -0
- package/esm/worker.d.ts +10 -0
- package/esm/worker.js +123 -0
- package/esm/worker.js.map +1 -0
- package/package.json +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2023 Alina Sireneva
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# @mtcute/web
|
|
2
|
+
|
|
3
|
+
📖 [API Reference](https://ref.mtcute.dev/modules/_mtcute_web.html)
|
|
4
|
+
|
|
5
|
+
Web support package for mtcute. Includes:
|
|
6
|
+
- WASM crypto provider
|
|
7
|
+
- Websocket transport
|
|
8
|
+
- IndexedDB storage
|
|
9
|
+
- `TelegramClient` implementation using the above
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { TelegramClient } from '@mtcute/web'
|
|
15
|
+
|
|
16
|
+
const tg = new TelegramClient({
|
|
17
|
+
apiId: 12345,
|
|
18
|
+
apiHash: 'abcdef',
|
|
19
|
+
storage: 'my-account'
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
tg.run(async (user) => {
|
|
23
|
+
console.log(`✨ logged in as ${user.displayName}`)
|
|
24
|
+
})
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage with workers
|
|
28
|
+
|
|
29
|
+
You can also use this package with web workers to offload most of the heavy lifting to a separate thread:
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// worker.ts
|
|
33
|
+
import { BaseTelegramClient, TelegramWorker } from '@mtcute/web'
|
|
34
|
+
|
|
35
|
+
const client = new BaseTelegramClient({
|
|
36
|
+
apiId: 12345,
|
|
37
|
+
apiHash: 'abcdef',
|
|
38
|
+
storage: 'my-account'
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
new TelegramWorker({ client })
|
|
42
|
+
|
|
43
|
+
// main.ts
|
|
44
|
+
import { TelegramClient, TelegramWorkerPort } from '@mtcute/web'
|
|
45
|
+
|
|
46
|
+
const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }) // or SharedWorker
|
|
47
|
+
const port = new TelegramWorkerPort({ worker })
|
|
48
|
+
const tg = new TelegramClient({ client: port })
|
|
49
|
+
|
|
50
|
+
tg.run({}, async (user) => {
|
|
51
|
+
console.log(`✨ logged in as ${user.displayName}`)
|
|
52
|
+
})
|
|
53
|
+
```
|
package/cjs/client.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ITelegramStorageProvider, PartialOnly } from '@mtcute/core';
|
|
2
|
+
import { BaseTelegramClient as BaseTelegramClientBase, BaseTelegramClientOptions as BaseTelegramClientOptionsBase, TelegramClient as TelegramClientBase, TelegramClientOptions } from '@mtcute/core/client.js';
|
|
3
|
+
export type { TelegramClientOptions };
|
|
4
|
+
export interface BaseTelegramClientOptions extends PartialOnly<Omit<BaseTelegramClientOptionsBase, 'storage'>, 'transport' | 'crypto'> {
|
|
5
|
+
/**
|
|
6
|
+
* Storage to use for this client.
|
|
7
|
+
*
|
|
8
|
+
* If a string is passed, it will be used as
|
|
9
|
+
* a name for an IndexedDB database.
|
|
10
|
+
*
|
|
11
|
+
* @default `"client.session"`
|
|
12
|
+
*/
|
|
13
|
+
storage?: string | ITelegramStorageProvider;
|
|
14
|
+
/**
|
|
15
|
+
* **ADVANCED USE ONLY**
|
|
16
|
+
*
|
|
17
|
+
* Whether to not set up the platform.
|
|
18
|
+
* This is useful if you call `setPlatform` yourself.
|
|
19
|
+
*/
|
|
20
|
+
platformless?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare class BaseTelegramClient extends BaseTelegramClientBase {
|
|
23
|
+
constructor(opts: BaseTelegramClientOptions);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Telegram client for use in Node.js
|
|
27
|
+
*/
|
|
28
|
+
export declare class TelegramClient extends TelegramClientBase {
|
|
29
|
+
constructor(opts: TelegramClientOptions);
|
|
30
|
+
}
|
package/cjs/client.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TelegramClient = exports.BaseTelegramClient = void 0;
|
|
4
|
+
const client_js_1 = require("@mtcute/core/client.js");
|
|
5
|
+
const platform_js_1 = require("@mtcute/core/platform.js");
|
|
6
|
+
const crypto_js_1 = require("./crypto.js");
|
|
7
|
+
const index_js_1 = require("./idb/index.js");
|
|
8
|
+
const platform_js_2 = require("./platform.js");
|
|
9
|
+
const websocket_js_1 = require("./websocket.js");
|
|
10
|
+
class BaseTelegramClient extends client_js_1.BaseTelegramClient {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
if (!opts.platformless)
|
|
13
|
+
(0, platform_js_1.setPlatform)(new platform_js_2.WebPlatform());
|
|
14
|
+
super({
|
|
15
|
+
crypto: new crypto_js_1.WebCryptoProvider(),
|
|
16
|
+
transport: () => new websocket_js_1.WebSocketTransport(),
|
|
17
|
+
...opts,
|
|
18
|
+
storage: typeof opts.storage === 'string' ?
|
|
19
|
+
new index_js_1.IdbStorage(opts.storage) :
|
|
20
|
+
opts.storage ?? new index_js_1.IdbStorage('client.session'),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.BaseTelegramClient = BaseTelegramClient;
|
|
25
|
+
/**
|
|
26
|
+
* Telegram client for use in Node.js
|
|
27
|
+
*/
|
|
28
|
+
class TelegramClient extends client_js_1.TelegramClient {
|
|
29
|
+
constructor(opts) {
|
|
30
|
+
if ('client' in opts) {
|
|
31
|
+
super(opts);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
super({
|
|
35
|
+
client: new BaseTelegramClient(opts),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.TelegramClient = TelegramClient;
|
|
40
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":";;;AACA,sDAK+B;AAC/B,0DAAsD;AAEtD,2CAA+C;AAC/C,6CAA2C;AAC3C,+CAA2C;AAC3C,iDAAmD;AAyBnD,MAAa,kBAAmB,SAAQ,8BAAsB;IAC1D,YAAY,IAA+B;QACvC,IAAI,CAAC,IAAI,CAAC,YAAY;YAAE,IAAA,yBAAW,EAAC,IAAI,yBAAW,EAAE,CAAC,CAAA;QAEtD,KAAK,CAAC;YACF,MAAM,EAAE,IAAI,6BAAiB,EAAE;YAC/B,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI,iCAAkB,EAAE;YACzC,GAAG,IAAI;YACP,OAAO,EACH,OAAO,IAAI,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC;gBAC9B,IAAI,qBAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;gBAC9B,IAAI,CAAC,OAAO,IAAI,IAAI,qBAAU,CAAC,gBAAgB,CAAC;SAC3D,CAAC,CAAA;IACN,CAAC;CACJ;AAdD,gDAcC;AAED;;GAEG;AACH,MAAa,cAAe,SAAQ,0BAAkB;IAClD,YAAY,IAA2B;QACnC,IAAI,QAAQ,IAAI,IAAI,EAAE;YAClB,KAAK,CAAC,IAAI,CAAC,CAAA;YAEX,OAAM;SACT;QAED,KAAK,CAAC;YACF,MAAM,EAAE,IAAI,kBAAkB,CAAC,IAAI,CAAC;SACvC,CAAC,CAAA;IACN,CAAC;CACJ;AAZD,wCAYC","sourcesContent":["import { ITelegramStorageProvider, PartialOnly } from '@mtcute/core'\nimport {\n BaseTelegramClient as BaseTelegramClientBase,\n BaseTelegramClientOptions as BaseTelegramClientOptionsBase,\n TelegramClient as TelegramClientBase,\n TelegramClientOptions,\n} from '@mtcute/core/client.js'\nimport { setPlatform } from '@mtcute/core/platform.js'\n\nimport { WebCryptoProvider } from './crypto.js'\nimport { IdbStorage } from './idb/index.js'\nimport { WebPlatform } from './platform.js'\nimport { WebSocketTransport } from './websocket.js'\n\nexport type { TelegramClientOptions }\n\nexport interface BaseTelegramClientOptions\n extends PartialOnly<Omit<BaseTelegramClientOptionsBase, 'storage'>, 'transport' | 'crypto'> {\n /**\n * Storage to use for this client.\n *\n * If a string is passed, it will be used as\n * a name for an IndexedDB database.\n *\n * @default `\"client.session\"`\n */\n storage?: string | ITelegramStorageProvider\n\n /**\n * **ADVANCED USE ONLY**\n *\n * Whether to not set up the platform.\n * This is useful if you call `setPlatform` yourself.\n */\n platformless?: boolean\n}\n\nexport class BaseTelegramClient extends BaseTelegramClientBase {\n constructor(opts: BaseTelegramClientOptions) {\n if (!opts.platformless) setPlatform(new WebPlatform())\n\n super({\n crypto: new WebCryptoProvider(),\n transport: () => new WebSocketTransport(),\n ...opts,\n storage:\n typeof opts.storage === 'string' ?\n new IdbStorage(opts.storage) :\n opts.storage ?? new IdbStorage('client.session'),\n })\n }\n}\n\n/**\n * Telegram client for use in Node.js\n */\nexport class TelegramClient extends TelegramClientBase {\n constructor(opts: TelegramClientOptions) {\n if ('client' in opts) {\n super(opts)\n\n return\n }\n\n super({\n client: new BaseTelegramClient(opts),\n })\n }\n}\n"]}
|
package/cjs/crypto.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BaseCryptoProvider, IAesCtr, ICryptoProvider, IEncryptionScheme } from '@mtcute/core/utils.js';
|
|
2
|
+
import { WasmInitInput } from './wasm.js';
|
|
3
|
+
export interface WebCryptoProviderOptions {
|
|
4
|
+
crypto?: Crypto;
|
|
5
|
+
wasmInput?: WasmInitInput;
|
|
6
|
+
}
|
|
7
|
+
export declare class WebCryptoProvider extends BaseCryptoProvider implements ICryptoProvider {
|
|
8
|
+
readonly crypto: Crypto;
|
|
9
|
+
private _wasmInput?;
|
|
10
|
+
sha1(data: Uint8Array): Uint8Array;
|
|
11
|
+
sha256(data: Uint8Array): Uint8Array;
|
|
12
|
+
createAesCtr(key: Uint8Array, iv: Uint8Array): IAesCtr;
|
|
13
|
+
createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme;
|
|
14
|
+
gzip(data: Uint8Array, maxSize: number): Uint8Array | null;
|
|
15
|
+
gunzip(data: Uint8Array): Uint8Array;
|
|
16
|
+
constructor(params?: WebCryptoProviderOptions);
|
|
17
|
+
initialize(): Promise<void>;
|
|
18
|
+
pbkdf2(password: Uint8Array, salt: Uint8Array, iterations: number, keylen?: number | undefined, algo?: string | undefined): Promise<Uint8Array>;
|
|
19
|
+
hmacSha256(data: Uint8Array, key: Uint8Array): Promise<Uint8Array>;
|
|
20
|
+
randomFill(buf: Uint8Array): void;
|
|
21
|
+
}
|
package/cjs/crypto.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebCryptoProvider = void 0;
|
|
4
|
+
const utils_js_1 = require("@mtcute/core/utils.js");
|
|
5
|
+
const wasm_1 = require("@mtcute/wasm");
|
|
6
|
+
const wasm_js_1 = require("./wasm.js");
|
|
7
|
+
const ALGO_TO_SUBTLE = {
|
|
8
|
+
sha256: 'SHA-256',
|
|
9
|
+
sha1: 'SHA-1',
|
|
10
|
+
sha512: 'SHA-512',
|
|
11
|
+
};
|
|
12
|
+
class WebCryptoProvider extends utils_js_1.BaseCryptoProvider {
|
|
13
|
+
sha1(data) {
|
|
14
|
+
return (0, wasm_1.sha1)(data);
|
|
15
|
+
}
|
|
16
|
+
sha256(data) {
|
|
17
|
+
return (0, wasm_1.sha256)(data);
|
|
18
|
+
}
|
|
19
|
+
createAesCtr(key, iv) {
|
|
20
|
+
const ctx = (0, wasm_1.createCtr256)(key, iv);
|
|
21
|
+
return {
|
|
22
|
+
process: (data) => (0, wasm_1.ctr256)(ctx, data),
|
|
23
|
+
close: () => (0, wasm_1.freeCtr256)(ctx),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
createAesIge(key, iv) {
|
|
27
|
+
return {
|
|
28
|
+
encrypt: (data) => (0, wasm_1.ige256Encrypt)(data, key, iv),
|
|
29
|
+
decrypt: (data) => (0, wasm_1.ige256Decrypt)(data, key, iv),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
gzip(data, maxSize) {
|
|
33
|
+
return (0, wasm_1.deflateMaxSize)(data, maxSize);
|
|
34
|
+
}
|
|
35
|
+
gunzip(data) {
|
|
36
|
+
return (0, wasm_1.gunzip)(data);
|
|
37
|
+
}
|
|
38
|
+
constructor(params) {
|
|
39
|
+
super();
|
|
40
|
+
const crypto = params?.crypto ?? globalThis.crypto;
|
|
41
|
+
if (!crypto || !crypto.subtle) {
|
|
42
|
+
throw new Error('WebCrypto is not available');
|
|
43
|
+
}
|
|
44
|
+
this.crypto = crypto;
|
|
45
|
+
this._wasmInput = params?.wasmInput;
|
|
46
|
+
}
|
|
47
|
+
async initialize() {
|
|
48
|
+
(0, wasm_1.initSync)(await (0, wasm_js_1.loadWasmBinary)(this._wasmInput));
|
|
49
|
+
}
|
|
50
|
+
async pbkdf2(password, salt, iterations, keylen, algo) {
|
|
51
|
+
const keyMaterial = await this.crypto.subtle.importKey('raw', password, 'PBKDF2', false, ['deriveBits']);
|
|
52
|
+
return this.crypto.subtle
|
|
53
|
+
.deriveBits({
|
|
54
|
+
name: 'PBKDF2',
|
|
55
|
+
salt,
|
|
56
|
+
iterations,
|
|
57
|
+
hash: algo ? ALGO_TO_SUBTLE[algo] : 'SHA-512',
|
|
58
|
+
}, keyMaterial, (keylen || 64) * 8)
|
|
59
|
+
.then((result) => new Uint8Array(result));
|
|
60
|
+
}
|
|
61
|
+
async hmacSha256(data, key) {
|
|
62
|
+
const keyMaterial = await this.crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: { name: 'SHA-256' } }, false, ['sign']);
|
|
63
|
+
const res = await this.crypto.subtle.sign({ name: 'HMAC' }, keyMaterial, data);
|
|
64
|
+
return new Uint8Array(res);
|
|
65
|
+
}
|
|
66
|
+
randomFill(buf) {
|
|
67
|
+
this.crypto.getRandomValues(buf);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.WebCryptoProvider = WebCrypt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C,SAAS,CAClD,KAAK,EACL,GAAG,EACH,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAC3C,KAAK,EACL,CAAC,MAAM,CAAC,CACX,CAAA;QAED,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,IAAI,CAAC,CAAA;QAE9E,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;IAC9B,CAAC;IAED,UAAU,CAAC,GAAe;QACtB,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAA;IACpC,CAAC;CACJ;AA3FD,8CA2FC","sourcesContent":["import { BaseCryptoProvider, IAesCtr, ICryptoProvider, IEncryptionScheme } from '@mtcute/core/utils.js'\nimport {\n createCtr256,\n ctr256,\n deflateMaxSize,\n freeCtr256,\n gunzip,\n ige256Decrypt,\n ige256Encrypt,\n initSync,\n sha1,\n sha256,\n} from '@mtcute/wasm'\n\nimport { loadWasmBinary, WasmInitInput } from './wasm.js'\n\nconst ALGO_TO_SUBTLE: Record<string, string> = {\n sha256: 'SHA-256',\n sha1: 'SHA-1',\n sha512: 'SHA-512',\n}\n\nexport interface WebCryptoProviderOptions {\n crypto?: Crypto\n wasmInput?: WasmInitInput\n}\n\nexport class WebCryptoProvider extends BaseCryptoProvider implements ICryptoProvider {\n readonly crypto: Crypto\n private _wasmInput?: WasmInitInput\n\n sha1(data: Uint8Array): Uint8Array {\n return sha1(data)\n }\n\n sha256(data: Uint8Array): Uint8Array {\n return sha256(data)\n }\n\n createAesCtr(key: Uint8Array, iv: Uint8Array): IAesCtr {\n const ctx = createCtr256(key, iv)\n\n return {\n process: (data) => ctr256(ctx, data),\n close: () => freeCtr256(ctx),\n }\n }\n\n createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme {\n return {\n encrypt: (data) => ige256Encrypt(data, key, iv),\n decrypt: (data) => ige256Decrypt(data, key, iv),\n }\n }\n\n gzip(data: Uint8Array, maxSize: number): Uint8Array | null {\n return deflateMaxSize(data, maxSize)\n }\n\n gunzip(data: Uint8Array): Uint8Array {\n return gunzip(data)\n }\n\n constructor(params?: WebCryptoProviderOptions) {\n super()\n const crypto = params?.crypto ?? globalThis.crypto\n\n if (!crypto || !crypto.subtle) {\n throw new Error('WebCrypto is not available')\n }\n this.crypto = crypto\n this._wasmInput = params?.wasmInput\n }\n\n async initialize(): Promise<void> {\n initSync(await loadWasmBinary(this._wasmInput))\n }\n\n async pbkdf2(\n password: Uint8Array,\n salt: Uint8Array,\n iterations: number,\n keylen?: number | undefined,\n algo?: string | undefined,\n ): Promise<Uint8Array> {\n const keyMaterial = await this.crypto.subtle.importKey('raw', password, 'PBKDF2', false, ['deriveBits'])\n\n return this.crypto.subtle\n .deriveBits(\n {\n name: 'PBKDF2',\n salt,\n iterations,\n hash: algo ? ALGO_TO_SUBTLE[algo] : 'SHA-512',\n },\n keyMaterial,\n (keylen || 64) * 8,\n )\n .then((result) => new Uint8Array(result))\n }\n\n async hmacSha256(data: Uint8Array, key: Uint8Array): Promise<Uint8Array> {\n const keyMaterial = await this.crypto.subtle.importKey(\n 'raw',\n key,\n { name: 'HMAC', hash: { name: 'SHA-256' } },\n false,\n ['sign'],\n )\n\n const res = await this.crypto.subtle.sign({ name: 'HMAC' }, keyMaterial, data)\n\n return new Uint8Array(res)\n }\n\n randomFill(buf: Uint8Array): void {\n this.crypto.getRandomValues(buf)\n }\n}\n"]}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.base64Decode = exports.base64Encode = void 0;
|
|
4
|
+
/// Based on https://github.com/beatgammit/base64-js, MIT license
|
|
5
|
+
const lookup = [];
|
|
6
|
+
const revLookup = [];
|
|
7
|
+
const code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
8
|
+
for (let i = 0, len = code.length; i < len; ++i) {
|
|
9
|
+
lookup[i] = code[i];
|
|
10
|
+
revLookup[code.charCodeAt(i)] = i;
|
|
11
|
+
}
|
|
12
|
+
function getLens(b64) {
|
|
13
|
+
const len = b64.length;
|
|
14
|
+
if (len % 4 > 0) {
|
|
15
|
+
throw new Error('Invalid string. Length must be a multiple of 4');
|
|
16
|
+
}
|
|
17
|
+
// Trim off extra bytes after placeholder bytes are found
|
|
18
|
+
// See: https://github.com/beatgammit/base64-js/issues/42
|
|
19
|
+
let validLen = b64.indexOf('=');
|
|
20
|
+
if (validLen === -1)
|
|
21
|
+
validLen = len;
|
|
22
|
+
const placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);
|
|
23
|
+
return [validLen, placeHoldersLen];
|
|
24
|
+
}
|
|
25
|
+
function _byteLength(b64, validLen, placeHoldersLen) {
|
|
26
|
+
return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;
|
|
27
|
+
}
|
|
28
|
+
function toByteArray(b64, arr) {
|
|
29
|
+
let tmp;
|
|
30
|
+
const lens = getLens(b64);
|
|
31
|
+
const validLen = lens[0];
|
|
32
|
+
const placeHoldersLen = lens[1];
|
|
33
|
+
let curByte = 0;
|
|
34
|
+
// if there are placeholders, only get up to the last complete 4 chars
|
|
35
|
+
const len = placeHoldersLen > 0 ? validLen - 4 : validLen;
|
|
36
|
+
let i;
|
|
37
|
+
for (i = 0; i < len; i += 4) {
|
|
38
|
+
tmp =
|
|
39
|
+
(revLookup[b64.charCodeAt(i)] << 18) |
|
|
40
|
+
(revLookup[b64.charCodeAt(i + 1)] << 12) |
|
|
41
|
+
(revLookup[b64.charCodeAt(i + 2)] << 6) |
|
|
42
|
+
revLookup[b64.charCodeAt(i + 3)];
|
|
43
|
+
arr[curByte++] = (tmp >> 16) & 0xff;
|
|
44
|
+
arr[curByte++] = (tmp >> 8) & 0xff;
|
|
45
|
+
arr[curByte++] = tmp & 0xff;
|
|
46
|
+
}
|
|
47
|
+
if (placeHoldersLen === 2) {
|
|
48
|
+
tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4);
|
|
49
|
+
arr[curByte++] = tmp & 0xff;
|
|
50
|
+
}
|
|
51
|
+
if (placeHoldersLen === 1) {
|
|
52
|
+
tmp =
|
|
53
|
+
(revLookup[b64.charCodeAt(i)] << 10) |
|
|
54
|
+
(revLookup[b64.charCodeAt(i + 1)] << 4) |
|
|
55
|
+
(revLookup[b64.charCodeAt(i + 2)] >> 2);
|
|
56
|
+
arr[curByte++] = (tmp >> 8) & 0xff;
|
|
57
|
+
arr[curByte++] = tmp & 0xff;
|
|
58
|
+
}
|
|
59
|
+
return arr;
|
|
60
|
+
}
|
|
61
|
+
function tripletToBase64(num) {
|
|
62
|
+
return lookup[(num >> 18) & 0x3f] + lookup[(num >> 12) & 0x3f] + lookup[(num >> 6) & 0x3f] + lookup[num & 0x3f];
|
|
63
|
+
}
|
|
64
|
+
function encodeChunk(uint8, start, end) {
|
|
65
|
+
let tmp;
|
|
66
|
+
const output = [];
|
|
67
|
+
for (let i = start; i < end; i += 3) {
|
|
68
|
+
tmp = ((uint8[i] << 16) & 0xff0000) + ((uint8[i + 1] << 8) & 0xff00) + (uint8[i + 2] & 0xff);
|
|
69
|
+
output.push(tripletToBase64(tmp));
|
|
70
|
+
}
|
|
71
|
+
return output.join('');
|
|
72
|
+
}
|
|
73
|
+
function fromByteArray(uint8) {
|
|
74
|
+
let tmp;
|
|
75
|
+
const len = uint8.length;
|
|
76
|
+
const extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
|
|
77
|
+
const parts = [];
|
|
78
|
+
const maxChunkLength = 16383; // must be multiple of 3
|
|
79
|
+
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
80
|
+
for (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
|
81
|
+
parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength));
|
|
82
|
+
}
|
|
83
|
+
// pad the end with zeros, but make sure to not forget the extra bytes
|
|
84
|
+
if (extraBytes === 1) {
|
|
85
|
+
tmp = uint8[len - 1];
|
|
86
|
+
parts.push(lookup[tmp >> 2] + lookup[(tmp << 4) & 0x3f] + '==');
|
|
87
|
+
}
|
|
88
|
+
else if (extraBytes === 2) {
|
|
89
|
+
tmp = (uint8[len - 2] << 8) + uint8[len - 1];
|
|
90
|
+
parts.push(lookup[tmp >> 10] + lookup[(tmp >> 4) & 0x3f] + lookup[(tmp << 2) & 0x3f] + '=');
|
|
91
|
+
}
|
|
92
|
+
return parts.join('');
|
|
93
|
+
}
|
|
94
|
+
function base64Encode(buf, url = false) {
|
|
95
|
+
const str = fromByteArray(buf);
|
|
96
|
+
if (url)
|
|
97
|
+
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
98
|
+
return str;
|
|
99
|
+
}
|
|
100
|
+
exports.base64Encode = base64Encode;
|
|
101
|
+
function base64Decode(string, url = false) {
|
|
102
|
+
if (url) {
|
|
103
|
+
string = string.replace(/-/g, '+').replace(/_/g, '/');
|
|
104
|
+
while (string.length % 4)
|
|
105
|
+
string += '=';
|
|
106
|
+
}
|
|
107
|
+
const buf = new Uint8Array(_byteLength(string, ...getLens(string)));
|
|
108
|
+
toByteArray(string, buf);
|
|
109
|
+
return buf;
|
|
110
|
+
}
|
|
111
|
+
exports.base64Decode = base64Decode;
|
|
112
|
+
//# sourceMappingURL=base64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.js","sourceRoot":"","sources":["../../../src/encodings/base64.ts"],"names":[],"mappings":";;;AAAA,iEAAiE;AACjE,MAAM,MAAM,GAAa,EAAE,CAAA;AAC3B,MAAM,SAAS,GAAa,EAAE,CAAA;AAE9B,MAAM,IAAI,GAAG,kEAAkE,CAAA;AAE/E,KAAK,IAAI,CAAC,G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AAC,GAAG,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,CAAA;KAC9F;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;AACzB,CAAC;AAED,SAAgB,YAAY,CAAC,GAAe,EAAE,GAAG,GAAG,KAAK;IACrD,MAAM,GAAG,GAAG,aAAa,CAAC,GAAG,CAAC,CAAA;IAC9B,IAAI,GAAG;QAAE,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;IAE7E,OAAO,GAAG,CAAA;AACd,CAAC;AALD,oCAKC;AAED,SAAgB,YAAY,CAAC,MAAc,EAAE,GAAG,GAAG,KAAK;IACpD,IAAI,GAAG,EAAE;QACL,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;QACrD,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC;YAAE,MAAM,IAAI,GAAG,CAAA;KAC1C;IAED,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IAEnE,WAAW,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAExB,OAAO,GAAG,CAAA;AACd,CAAC;AAXD,oCAWC","sourcesContent":["/// Based on https://github.com/beatgammit/base64-js, MIT license\nconst lookup: string[] = []\nconst revLookup: number[] = []\n\nconst code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'\n\nfor (let i = 0, len = code.length; i < len; ++i) {\n lookup[i] = code[i]\n revLookup[code.charCodeAt(i)] = i\n}\n\nfunction getLens(b64: string): [number, number] {\n const len = b64.length\n\n if (len % 4 > 0) {\n throw new Error('Invalid string. Length must be a multiple of 4')\n }\n\n // Trim off extra bytes after placeholder bytes are found\n // See: https://github.com/beatgammit/base64-js/issues/42\n let validLen = b64.indexOf('=')\n if (validLen === -1) validLen = len\n\n const placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4)\n\n return [validLen, placeHoldersLen]\n}\n\nfunction _byteLength(b64: string, validLen: number, placeHoldersLen: number) {\n return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen\n}\n\nfunction toByteArray(b64: string, arr: Uint8Array) {\n let tmp\n const lens = getLens(b64)\n const validLen = lens[0]\n const placeHoldersLen = lens[1]\n\n let curByte = 0\n\n // if there are placeholders, only get up to the last complete 4 chars\n const len = placeHoldersLen > 0 ? validLen - 4 : validLen\n\n let i\n\n for (i = 0; i < len; i += 4) {\n tmp =\n (revLookup[b64.charCodeAt(i)] << 18) |\n (revLookup[b64.charCodeAt(i + 1)] << 12) |\n (revLookup[b64.charCodeAt(i + 2)] << 6) |\n revLookup[b64.charCodeAt(i + 3)]\n arr[curByte++] = (tmp >> 16) & 0xff\n arr[curByte++] = (tmp >> 8) & 0xff\n arr[curByte++] = tmp & 0xff\n }\n\n if (placeHoldersLen === 2) {\n tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)\n arr[curByte++] = tmp & 0xff\n }\n\n if (placeHoldersLen === 1) {\n tmp =\n (revLookup[b64.charCodeAt(i)] << 10) |\n (revLookup[b64.charCodeAt(i + 1)] << 4) |\n (revLookup[b64.charCodeAt(i + 2)] >> 2)\n arr[curByte++] = (tmp >> 8) & 0xff\n arr[curByte++] = tmp & 0xff\n }\n\n return arr\n}\n\nfunction tripletToBase64(num: number) {\n return lookup[(num >> 18) & 0x3f] + lookup[(num >> 12) & 0x3f] + lookup[(num >> 6) & 0x3f] + lookup[num & 0x3f]\n}\n\nfunction encodeChunk(uint8: Uint8Array, start: number, end: number) {\n let tmp\n const output = []\n\n for (let i = start; i < end; i += 3) {\n tmp = ((uint8[i] << 16) & 0xff0000) + ((uint8[i + 1] << 8) & 0xff00) + (uint8[i + 2] & 0xff)\n output.push(tripletToBase64(tmp))\n }\n\n return output.join('')\n}\n\nfunction fromByteArray(uint8: Uint8Array) {\n let tmp\n const len = uint8.length\n const extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes\n const parts = []\n const maxChunkLength = 16383 // must be multiple of 3\n\n // go through the array every three bytes, we'll deal with trailing stuff later\n for (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {\n parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength))\n }\n\n // pad the end with zeros, but make sure to not forget the extra bytes\n if (extraBytes === 1) {\n tmp = uint8[len - 1]\n parts.push(lookup[tmp >> 2] + lookup[(tmp << 4) & 0x3f] + '==')\n } else if (extraBytes === 2) {\n tmp = (uint8[len - 2] << 8) + uint8[len - 1]\n parts.push(lookup[tmp >> 10] + lookup[(tmp >> 4) & 0x3f] + lookup[(tmp << 2) & 0x3f] + '=')\n }\n\n return parts.join('')\n}\n\nexport function base64Encode(buf: Uint8Array, url = false): string {\n const str = fromByteArray(buf)\n if (url) return str.replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '')\n\n return str\n}\n\nexport function base64Decode(string: string, url = false): Uint8Array {\n if (url) {\n string = string.replace(/-/g, '+').replace(/_/g, '/')\n while (string.length % 4) string += '='\n }\n\n const buf = new Uint8Array(_byteLength(string, ...getLens(string)))\n\n toByteArray(string, buf)\n\n return buf\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// Based on https://github.com/feross/buffer, MIT license
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.hexDecode = exports.hexEncode = void 0;
|
|
5
|
+
const hexSliceLookupTable = (function () {
|
|
6
|
+
const alphabet = '0123456789abcdef';
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
8
|
+
const table = new Array(256);
|
|
9
|
+
for (let i = 0; i < 16; ++i) {
|
|
10
|
+
const i16 = i * 16;
|
|
11
|
+
for (let j = 0; j < 16; ++j) {
|
|
12
|
+
table[i16 + j] = alphabet[i] + alphabet[j];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return table;
|
|
16
|
+
})();
|
|
17
|
+
const hexCharValueTable = {
|
|
18
|
+
'0': 0,
|
|
19
|
+
'1': 1,
|
|
20
|
+
'2': 2,
|
|
21
|
+
'3': 3,
|
|
22
|
+
'4': 4,
|
|
23
|
+
'5': 5,
|
|
24
|
+
'6': 6,
|
|
25
|
+
'7': 7,
|
|
26
|
+
'8': 8,
|
|
27
|
+
'9': 9,
|
|
28
|
+
a: 10,
|
|
29
|
+
b: 11,
|
|
30
|
+
c: 12,
|
|
31
|
+
d: 13,
|
|
32
|
+
e: 14,
|
|
33
|
+
f: 15,
|
|
34
|
+
A: 10,
|
|
35
|
+
B: 11,
|
|
36
|
+
C: 12,
|
|
37
|
+
D: 13,
|
|
38
|
+
E: 14,
|
|
39
|
+
F: 15,
|
|
40
|
+
};
|
|
41
|
+
function hexEncode(buf) {
|
|
42
|
+
let out = '';
|
|
43
|
+
for (let i = 0; i < buf.byteLength; ++i) {
|
|
44
|
+
out += hexSliceLookupTable[buf[i]];
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
exports.hexEncode = hexEncode;
|
|
49
|
+
function hexDecodeInner(buf, string) {
|
|
50
|
+
const strLen = string.length;
|
|
51
|
+
const length = Math.min(buf.length, strLen / 2);
|
|
52
|
+
let i;
|
|
53
|
+
for (i = 0; i < length; ++i) {
|
|
54
|
+
const a = hexCharValueTable[string[i * 2]];
|
|
55
|
+
const b = hexCharValueTable[string[i * 2 + 1]];
|
|
56
|
+
if (a === undefined || b === undefined) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
buf[i] = (a << 4) | b;
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
function hexDecode(string) {
|
|
64
|
+
const buf = new Uint8Array(Math.ceil(string.length / 2));
|
|
65
|
+
hexDecodeInner(buf, string);
|
|
66
|
+
return buf;
|
|
67
|
+
}
|
|
68
|
+
exports.hexDecode = hexDecode;
|
|
69
|
+
//# sourceMappingURL=hex.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.js","sourceRoot":"","sources":["../../../src/encodings/hex.ts"],"names":[],"mappings":";AAAA,0DAA0D;;;AAE1D,MAAM,mBAAmB,GAAG,CAAC;IACzB,MAAM,QAAQ,GAAG,kBAAkB,CAAA;IACnC,mEAAmE;IACnE,MAAM,KAAK,GAAa,IAAI,KAAK,CAAC,GAAG,CAAC,CAAA;IAEtC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE;QACzB,MAAM,GAAG,GAAG,CAAC,GAAG,EAAE,CAAA;QAElB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE;YACzB,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;SAC7C;KACJ;IAED,OAAO,KAAK,CAAA;AAChB,CAAC,CAAC,EAAE,CAAA;AAEJ,MAAM,iBAAiB,GAA2B;IAC9C,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,CAAC;IACN,CAAC,EAAE,EAAE;IACL,CAAC,EAA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;IACpC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAA;IACxD,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;IAE3B,OAAO,GAAG,CAAA;AACd,CAAC;AALD,8BAKC","sourcesContent":["/// Based on https://github.com/feross/buffer, MIT license\n\nconst hexSliceLookupTable = (function () {\n const alphabet = '0123456789abcdef'\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n const table: string[] = new Array(256)\n\n for (let i = 0; i < 16; ++i) {\n const i16 = i * 16\n\n for (let j = 0; j < 16; ++j) {\n table[i16 + j] = alphabet[i] + alphabet[j]\n }\n }\n\n return table\n})()\n\nconst hexCharValueTable: Record<string, number> = {\n '0': 0,\n '1': 1,\n '2': 2,\n '3': 3,\n '4': 4,\n '5': 5,\n '6': 6,\n '7': 7,\n '8': 8,\n '9': 9,\n a: 10,\n b: 11,\n c: 12,\n d: 13,\n e: 14,\n f: 15,\n A: 10,\n B: 11,\n C: 12,\n D: 13,\n E: 14,\n F: 15,\n}\n\nexport function hexEncode(buf: Uint8Array): string {\n let out = ''\n\n for (let i = 0; i < buf.byteLength; ++i) {\n out += hexSliceLookupTable[buf[i]]\n }\n\n return out\n}\n\nfunction hexDecodeInner(buf: Uint8Array, string: string): void {\n const strLen = string.length\n const length = Math.min(buf.length, strLen / 2)\n\n let i\n\n for (i = 0; i < length; ++i) {\n const a = hexCharValueTable[string[i * 2]]\n const b = hexCharValueTable[string[i * 2 + 1]]\n\n if (a === undefined || b === undefined) {\n return\n }\n buf[i] = (a << 4) | b\n }\n\n return\n}\n\nexport function hexDecode(string: string): Uint8Array {\n const buf = new Uint8Array(Math.ceil(string.length / 2))\n hexDecodeInner(buf, string)\n\n return buf\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.utf8Encode = exports.utf8Decode = exports.utf8ByteLength = void 0;
|
|
4
|
+
const sharedEncoder = new TextEncoder();
|
|
5
|
+
const sharedDecoder = new TextDecoder('utf8');
|
|
6
|
+
function utf8ByteLength(str) {
|
|
7
|
+
// https://stackoverflow.com/a/23329386
|
|
8
|
+
let s = str.length;
|
|
9
|
+
for (let i = str.length - 1; i >= 0; i--) {
|
|
10
|
+
const code = str.charCodeAt(i);
|
|
11
|
+
if (code > 0x7f && code <= 0x7ff)
|
|
12
|
+
s++;
|
|
13
|
+
else if (code > 0x7ff && code <= 0xffff)
|
|
14
|
+
s += 2;
|
|
15
|
+
if (code >= 0xDC00 && code <= 0xDFFF)
|
|
16
|
+
i--; //trail surrogate
|
|
17
|
+
}
|
|
18
|
+
return s;
|
|
19
|
+
}
|
|
20
|
+
exports.utf8ByteLength = utf8ByteLength;
|
|
21
|
+
function utf8Decode(buf) {
|
|
22
|
+
return sharedDecoder.decode(buf);
|
|
23
|
+
}
|
|
24
|
+
exports.utf8Decode = utf8Decode;
|
|
25
|
+
function utf8Encode(str) {
|
|
26
|
+
return sharedEncoder.encode(str);
|
|
27
|
+
}
|
|
28
|
+
exports.utf8Encode = utf8Encode;
|
|
29
|
+
//# sourceMappingURL=utf8.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utf8.js","sourceRoot":"","sources":["../../../src/encodings/utf8.ts"],"names":[],"mappings":";;;AAAA,MAAM,aAAa,GAAG,IAAI,WAAW,EAAE,CAAA;AACvC,MAAM,aAAa,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,CAAA;AAE7C,SAAgB,cAAc,CAAC,GAAW;IACtC,uCAAuC;IACvC,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,CAAA;IAElB,KAAK,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;QACtC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QAC9B,IAAI,IAAI,GAAG,IAAI,IAAI,IAAI,IAAI,KAAK;YAAE,CAAC,EAAE,CAAA;aAChC,IAAI,IAAI,GAAG,KAAK,IAAI,IAAI,IAAI,MAAM;YAAE,CAAC,IAAI,CAAC,CAAA;QAC/C,IAAI,IAAI,IAAI,MAAM,IAAI,IAAI,IAAI,MAAM;YAAE,CAAC,EAAE,CAAA,CAAC,iBAAiB;KAC9D;IAED,OAAO,CAAC,CAAA;AACZ,CAAC;AAZD,wCAYC;AAED,SAAgB,UAAU,CAAC,GAAe;IACtC,OAAO,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AACpC,CAAC;AAFD,gCAEC;AAED,SAAgB,UAAU,CAAC,GAAW;IAClC,OAAO,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AACpC,CAAC;AAFD,gCAEC","sourcesContent":["const sharedEncoder = new TextEncoder()\nconst sharedDecoder = new TextDecoder('utf8')\n\nexport function utf8ByteLength(str: string) {\n // https://stackoverflow.com/a/23329386\n let s = str.length\n\n for (let i = str.length - 1; i >= 0; i--) {\n const code = str.charCodeAt(i)\n if (code > 0x7f && code <= 0x7ff) s++\n else if (code > 0x7ff && code <= 0xffff) s += 2\n if (code >= 0xDC00 && code <= 0xDFFF) i-- //trail surrogate\n }\n\n return s\n}\n\nexport function utf8Decode(buf: Uint8Array): string {\n return sharedDecoder.decode(buf)\n}\n\nexport function utf8Encode(str: string): Uint8Array {\n return sharedEncoder.encode(str)\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function beforeExit(fn: () => void): () => void;
|
package/cjs/exit-hook.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.beforeExit = void 0;
|
|
4
|
+
const callbacks = new Set();
|
|
5
|
+
let registered = false;
|
|
6
|
+
function beforeExit(fn) {
|
|
7
|
+
if (typeof window === 'undefined') {
|
|
8
|
+
return () => { };
|
|
9
|
+
}
|
|
10
|
+
if (!registered) {
|
|
11
|
+
registered = true;
|
|
12
|
+
window.addEventListener('beforeunload', () => {
|
|
13
|
+
for (const callback of callbacks) {
|
|
14
|
+
callback();
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
callbacks.add(fn);
|
|
19
|
+
return () => {
|
|
20
|
+
callbacks.delete(fn);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.beforeExit = beforeExit;
|
|
24
|
+
//# sourceMappingURL=exit-hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exit-hook.js","sourceRoot":"","sources":["../../src/exit-hook.ts"],"names":[],"mappings":";;;AAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAc,CAAA;AAEvC,IAAI,UAAU,GAAG,KAAK,CAAA;AAEtB,SAAgB,UAAU,CAAC,EAAc;IACrC,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QAC/B,OAAO,GAAG,EAAE,GAAE,CAAC,CAAA;KAClB;IACD,IAAI,CAAC,UAAU,EAAE;QACb,UAAU,GAAG,IAAI,CAAA;QAEjB,MAAM,CAAC,gBAAgB,CAAC,cAAc,EAAE,GAAG,EAAE;YACzC,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE;gBAC9B,QAAQ,EAAE,CAAA;aACb;QACL,CAAC,CAAC,CAAA;KACL;IAED,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IAEjB,OAAO,GAAG,EAAE;QACR,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;IACxB,CAAC,CAAA;AACL,CAAC;AAnBD,gCAmBC","sourcesContent":["const callbacks = new Set<() => void>()\n\nlet registered = false\n\nexport function beforeExit(fn: () => void): () => void {\n if (typeof window === 'undefined') {\n return () => {}\n }\n if (!registered) {\n registered = true\n\n window.addEventListener('beforeunload', () => {\n for (const callback of callbacks) {\n callback()\n }\n })\n }\n\n callbacks.add(fn)\n\n return () => {\n callbacks.delete(fn)\n }\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BaseStorageDriver } from '@mtcute/core';
|
|
2
|
+
export type PostMigrationFunction = (db: IDBDatabase) => Promise<void>;
|
|
3
|
+
type MigrationFunction = (db: IDBDatabase) => void | PostMigrationFunction;
|
|
4
|
+
export declare class IdbStorageDriver extends BaseStorageDriver {
|
|
5
|
+
readonly _dbName: string;
|
|
6
|
+
db: IDBDatabase;
|
|
7
|
+
constructor(_dbName: string);
|
|
8
|
+
private _pendingWrites;
|
|
9
|
+
private _pendingWritesOses;
|
|
10
|
+
private _migrations;
|
|
11
|
+
private _maxVersion;
|
|
12
|
+
registerMigration(repo: string, version: number, migration: MigrationFunction): void;
|
|
13
|
+
writeLater(os: string, obj: unknown): void;
|
|
14
|
+
_load(): Promise<void>;
|
|
15
|
+
_save(): Promise<void>;
|
|
16
|
+
_destroy(): void;
|
|
17
|
+
}
|
|
18
|
+
export {};
|