-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support wasi target on browser (#757)
- Loading branch information
1 parent
ed4e9aa
commit f39eec0
Showing
37 changed files
with
827 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync } from '@emnapi/core' | ||
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime' | ||
import { WASI as __WASI } from '@tybys/wasm-util' | ||
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from 'memfs-browser' | ||
|
||
import __wasmUrl from './argon2.wasm32-wasi.wasm?url' | ||
|
||
const __fs = __createFsFromVolume( | ||
__Volume.fromJSON({ | ||
'/': null, | ||
}), | ||
) | ||
|
||
const __wasi = new __WASI({ | ||
version: 'preview1', | ||
fs: __fs, | ||
}) | ||
|
||
const __emnapiContext = __emnapiGetDefaultContext() | ||
|
||
const __sharedMemory = new WebAssembly.Memory({ | ||
initial: 1024, | ||
maximum: 10240, | ||
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() { | ||
return new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), { | ||
type: 'module', | ||
}) | ||
}, | ||
overwriteImports(importObject) { | ||
importObject.env = { | ||
...importObject.env, | ||
...importObject.napi, | ||
...importObject.emnapi, | ||
memory: __sharedMemory, | ||
} | ||
return importObject | ||
}, | ||
beforeInit({ instance }) { | ||
__napi_rs_initialize_modules(instance) | ||
}, | ||
}) | ||
|
||
function __napi_rs_initialize_modules(__napiInstance) { | ||
__napiInstance.exports['__napi_register__Algorithm_0']?.() | ||
__napiInstance.exports['__napi_register__Version_1']?.() | ||
__napiInstance.exports['__napi_register__Options_struct_2']?.() | ||
__napiInstance.exports['__napi_register__HashTask_impl_3']?.() | ||
__napiInstance.exports['__napi_register__hash_4']?.() | ||
__napiInstance.exports['__napi_register__hash_sync_5']?.() | ||
__napiInstance.exports['__napi_register__RawHashTask_impl_6']?.() | ||
__napiInstance.exports['__napi_register__hash_raw_7']?.() | ||
__napiInstance.exports['__napi_register__hash_raw_sync_8']?.() | ||
__napiInstance.exports['__napi_register__VerifyTask_impl_9']?.() | ||
__napiInstance.exports['__napi_register__verify_10']?.() | ||
__napiInstance.exports['__napi_register__verify_sync_11']?.() | ||
} | ||
export const Algorithm = __napiModule.exports.Algorithm | ||
export const hash = __napiModule.exports.hash | ||
export const hashRaw = __napiModule.exports.hashRaw | ||
export const hashRawSync = __napiModule.exports.hashRawSync | ||
export const hashSync = __napiModule.exports.hashSync | ||
export const verify = __napiModule.exports.verify | ||
export const verifySync = __napiModule.exports.verifySync | ||
export const Version = __napiModule.exports.Version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core' | ||
import { WASI } from '@tybys/wasm-util' | ||
import { Volume, createFsFromVolume } from 'memfs-browser' | ||
|
||
const fs = createFsFromVolume( | ||
Volume.fromJSON({ | ||
'/': null, | ||
}), | ||
) | ||
|
||
const handler = new MessageHandler({ | ||
onLoad({ wasmModule, wasmMemory }) { | ||
const wasi = new WASI({ | ||
fs, | ||
print: function () { | ||
// eslint-disable-next-line no-console | ||
console.log.apply(console, arguments) | ||
}, | ||
}) | ||
return instantiateNapiModuleSync(wasmModule, { | ||
childThread: true, | ||
wasi, | ||
overwriteImports(importObject) { | ||
importObject.env = { | ||
...importObject.env, | ||
...importObject.napi, | ||
...importObject.emnapi, | ||
memory: wasmMemory, | ||
} | ||
}, | ||
}) | ||
}, | ||
}) | ||
|
||
globalThis.onmessage = function (e) { | ||
handler.handle(e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync } from '@emnapi/core' | ||
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime' | ||
import { WASI as __WASI } from '@tybys/wasm-util' | ||
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from 'memfs-browser' | ||
|
||
import __wasmUrl from './bcrypt.wasm32-wasi.wasm?url' | ||
|
||
const __fs = __createFsFromVolume( | ||
__Volume.fromJSON({ | ||
'/': null, | ||
}), | ||
) | ||
|
||
const __wasi = new __WASI({ | ||
version: 'preview1', | ||
fs: __fs, | ||
}) | ||
|
||
const __emnapiContext = __emnapiGetDefaultContext() | ||
|
||
const __sharedMemory = new WebAssembly.Memory({ | ||
initial: 1024, | ||
maximum: 10240, | ||
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() { | ||
return new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), { | ||
type: 'module', | ||
}) | ||
}, | ||
overwriteImports(importObject) { | ||
importObject.env = { | ||
...importObject.env, | ||
...importObject.napi, | ||
...importObject.emnapi, | ||
memory: __sharedMemory, | ||
} | ||
return importObject | ||
}, | ||
beforeInit({ instance }) { | ||
__napi_rs_initialize_modules(instance) | ||
}, | ||
}) | ||
|
||
function __napi_rs_initialize_modules(__napiInstance) { | ||
__napiInstance.exports['__napi_register__HashTask_impl_0']?.() | ||
__napiInstance.exports['__napi_register__SaltTask_impl_1']?.() | ||
__napiInstance.exports['__napi_register__VerifyTask_impl_2']?.() | ||
__napiInstance.exports['__napi_register__DEFAULT_COST_3']?.() | ||
__napiInstance.exports['__napi_register__gen_salt_sync_4']?.() | ||
__napiInstance.exports['__napi_register__gen_salt_js_5']?.() | ||
__napiInstance.exports['__napi_register__hash_sync_6']?.() | ||
__napiInstance.exports['__napi_register__hash_7']?.() | ||
__napiInstance.exports['__napi_register__verify_sync_8']?.() | ||
__napiInstance.exports['__napi_register__verify_9']?.() | ||
} | ||
export const DEFAULT_COST = __napiModule.exports.DEFAULT_COST | ||
export const genSalt = __napiModule.exports.genSalt | ||
export const genSaltSync = __napiModule.exports.genSaltSync | ||
export const hash = __napiModule.exports.hash | ||
export const hashSync = __napiModule.exports.hashSync | ||
export const verify = __napiModule.exports.verify | ||
export const verifySync = __napiModule.exports.verifySync |
Oops, something went wrong.