From db98751f67931f3fdd83faed60804b7bb5e5628d Mon Sep 17 00:00:00 2001 From: Alina Sireneva Date: Fri, 23 Feb 2024 04:08:21 +0300 Subject: [PATCH] chore: removed platform-specific code in wasm --- packages/wasm/package.json | 3 -- packages/wasm/src/index.ts | 16 +--------- packages/wasm/src/init.ts | 26 ----------------- packages/wasm/src/init.web.ts | 42 --------------------------- packages/wasm/src/types.ts | 1 - packages/wasm/tests/allocator.test.ts | 5 ++-- packages/wasm/tests/init.ts | 18 ++++++++++++ 7 files changed, 22 insertions(+), 89 deletions(-) delete mode 100644 packages/wasm/src/init.ts delete mode 100644 packages/wasm/src/init.web.ts create mode 100644 packages/wasm/tests/init.ts diff --git a/packages/wasm/package.json b/packages/wasm/package.json index 8ab61ff6..83493722 100644 --- a/packages/wasm/package.json +++ b/packages/wasm/package.json @@ -12,9 +12,6 @@ "build": "pnpm run -w build-package wasm", "build:wasm": "docker build --output=lib --target=binaries lib" }, - "browser": { - "./src/init.js": "./src/init.web.js" - }, "distOnlyFields": { "exports": { ".": { diff --git a/packages/wasm/src/index.ts b/packages/wasm/src/index.ts index a2c516e0..f7fa32d5 100644 --- a/packages/wasm/src/index.ts +++ b/packages/wasm/src/index.ts @@ -1,5 +1,4 @@ -import { loadWasmBinary } from './init.js' -import { InitInput, MtcuteWasmModule, SyncInitInput } from './types.js' +import { MtcuteWasmModule, SyncInitInput } from './types.js' export * from './types.js' @@ -47,19 +46,6 @@ export function initSync(module: SyncInitInput): void { /* c8 ignore end */ -/** - * Init the WASM blob asynchronously (e.g. by passing a URL to the WASM file) - * - * By default, will try to determine the best way to load the WASM file automatically. - */ -export async function initAsync(input?: InitInput): Promise { - if (wasm !== undefined) return - const instance = await loadWasmBinary(input) - - wasm = instance.exports as unknown as MtcuteWasmModule - initCommon() -} - /** * Deflate some data with zlib headers and max output size * diff --git a/packages/wasm/src/init.ts b/packages/wasm/src/init.ts deleted file mode 100644 index 4d274881..00000000 --- a/packages/wasm/src/init.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* eslint-disable no-restricted-imports */ -import { readFile } from 'fs/promises' -import { join } from 'path' - -import { InitInput } from './types.js' - -// @only-if-esm -const url = await import('url') -const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) -// @/only-if-esm - -export async function loadWasmBinary(input?: InitInput): Promise { - if (typeof input === 'undefined') { - input = join(__dirname, '../lib/mtcute.wasm') - } - - /* c8 ignore next 3 */ - if (typeof input !== 'string') { - throw new Error('Invalid input, for Node.js pass path to wasm blob') - } - - const module = new WebAssembly.Module(await readFile(input)) - const instance = new WebAssembly.Instance(module) - - return instance -} diff --git a/packages/wasm/src/init.web.ts b/packages/wasm/src/init.web.ts deleted file mode 100644 index 6e0cc1e1..00000000 --- a/packages/wasm/src/init.web.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { InitInput } from './types.js' - -export async function loadWasmBinary(input?: InitInput): Promise { - if (typeof input === 'undefined') { - input = new URL('../lib/mtcute.wasm', import.meta.url) - } - - if ( - typeof input === 'string' || - (typeof Request === 'function' && input instanceof Request) || - (typeof URL === 'function' && input instanceof URL) - ) { - input = await fetch(input) - } - - if (typeof Response === 'function' && input instanceof Response) { - if (typeof WebAssembly.instantiateStreaming === 'function') { - try { - const { instance } = await WebAssembly.instantiateStreaming(input) - - return instance - } catch (e) { - if (input.headers.get('Content-Type') !== 'application/wasm') { - console.warn( - '`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n', - e, - ) - } else { - throw e - } - } - } - - const bytes = await input.arrayBuffer() - - const { instance } = await WebAssembly.instantiate(bytes) - - return instance - } - - return await WebAssembly.instantiate(input) -} diff --git a/packages/wasm/src/types.ts b/packages/wasm/src/types.ts index 55ce1b60..0170c3e2 100644 --- a/packages/wasm/src/types.ts +++ b/packages/wasm/src/types.ts @@ -29,4 +29,3 @@ export interface MtcuteWasmModule { } export type SyncInitInput = BufferSource | WebAssembly.Module -export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module diff --git a/packages/wasm/tests/allocator.test.ts b/packages/wasm/tests/allocator.test.ts index c9afc617..1a0cb70b 100644 --- a/packages/wasm/tests/allocator.test.ts +++ b/packages/wasm/tests/allocator.test.ts @@ -1,9 +1,10 @@ import { beforeAll, describe, expect, it } from 'vitest' -import { __getWasm, initAsync } from '../src/index.js' +import { __getWasm } from '../src/index.js' +import { initWasm } from './init.js' beforeAll(async () => { - await initAsync() + await initWasm() }) describe('allocator', () => { diff --git a/packages/wasm/tests/init.ts b/packages/wasm/tests/init.ts new file mode 100644 index 00000000..07cd12e7 --- /dev/null +++ b/packages/wasm/tests/init.ts @@ -0,0 +1,18 @@ +import { initSync } from '../src/index.js' + +// todo: use platform-specific packages +export async function initWasm() { + const url = new URL('../lib/mtcute.wasm', import.meta.url) + + if (import.meta.env.TEST_ENV === 'node' || import.meta.env.TEST_ENV === 'bun') { + const fs = await import('fs/promises') + const blob = await fs.readFile(url) + initSync(blob) + + return + } + + const blob = await fetch(url) + const buffer = await blob.arrayBuffer() + initSync(buffer) +}