mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-18 17:14:20 +01:00
With napi v3 we can compile our javascript binding to wasm, which can reduce a lot of maintenance overhead and complexity
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
import {
|
|
createOnMessage as __wasmCreateOnMessageForFsProxy,
|
|
getDefaultContext as __emnapiGetDefaultContext,
|
|
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
|
|
WASI as __WASI,
|
|
} from '@napi-rs/wasm-runtime'
|
|
|
|
|
|
|
|
const __wasi = new __WASI({
|
|
version: 'preview1',
|
|
})
|
|
|
|
const __wasmUrl = new URL('./turso.wasm32-wasi.wasm', import.meta.url).href
|
|
const __emnapiContext = __emnapiGetDefaultContext()
|
|
|
|
|
|
const __sharedMemory = new WebAssembly.Memory({
|
|
initial: 4000,
|
|
maximum: 65536,
|
|
shared: true,
|
|
})
|
|
|
|
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
|
|
|
|
const {
|
|
instance: __napiInstance,
|
|
module: __wasiModule,
|
|
napiModule: __napiModule,
|
|
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
|
|
context: __emnapiContext,
|
|
asyncWorkPoolSize: 4,
|
|
wasi: __wasi,
|
|
onCreateWorker() {
|
|
const worker = new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
|
|
type: 'module',
|
|
})
|
|
|
|
return worker
|
|
},
|
|
overwriteImports(importObject) {
|
|
importObject.env = {
|
|
...importObject.env,
|
|
...importObject.napi,
|
|
...importObject.emnapi,
|
|
memory: __sharedMemory,
|
|
}
|
|
return importObject
|
|
},
|
|
beforeInit({ instance }) {
|
|
for (const name of Object.keys(instance.exports)) {
|
|
if (name.startsWith('__napi_register__')) {
|
|
instance.exports[name]()
|
|
}
|
|
}
|
|
},
|
|
})
|
|
export default __napiModule.exports
|
|
export const Database = __napiModule.exports.Database
|
|
export const Statement = __napiModule.exports.Statement
|