chore: removed platform-specific code in wasm
This commit is contained in:
parent
806c62bda8
commit
db98751f67
7 changed files with 22 additions and 89 deletions
|
@ -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": {
|
||||
".": {
|
||||
|
|
|
@ -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<void> {
|
||||
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
|
||||
*
|
||||
|
|
|
@ -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<WebAssembly.Instance> {
|
||||
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
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
import { InitInput } from './types.js'
|
||||
|
||||
export async function loadWasmBinary(input?: InitInput): Promise<WebAssembly.Instance> {
|
||||
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)
|
||||
}
|
|
@ -29,4 +29,3 @@ export interface MtcuteWasmModule {
|
|||
}
|
||||
|
||||
export type SyncInitInput = BufferSource | WebAssembly.Module
|
||||
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
18
packages/wasm/tests/init.ts
Normal file
18
packages/wasm/tests/init.ts
Normal file
|
@ -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)
|
||||
}
|
Loading…
Reference in a new issue