-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmod.ts
107 lines (94 loc) · 3.61 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { CAPABILITIES } from './polyfills/deno-capabilities.ts';
import type { ManifestLike, InternalConfig, ExtismPluginOptions, Plugin } from './interfaces.ts';
import { toWasmModuleData as _toWasmModuleData } from './manifest.ts';
import { createForegroundPlugin as _createForegroundPlugin } from './foreground-plugin.ts';
import { createBackgroundPlugin as _createBackgroundPlugin } from './background-plugin.ts';
export { CAPABILITIES } from './polyfills/deno-capabilities.ts';
export type {
Capabilities,
ExtismPluginOptions,
ManifestLike,
ManifestWasmResponse,
ManifestWasmModule,
ManifestWasmData,
ManifestWasmUrl,
ManifestWasmPath,
ManifestWasm,
Manifest,
Plugin,
PluginOutput,
} from './interfaces.ts';
export type { CallContext, CallContext as CurrentPlugin } from './call-context.ts';
/**
* Create a {@link Plugin} given a {@link ManifestLike} and {@link ExtismPluginOptions}.
*
* Plugins wrap Wasm modules, exposing rich access to exported functions.
*
* ```ts
* const plugin = await createPlugin(
* 'https://github.com/extism/plugins/releases/download/v0.3.0/count_vowels.wasm',
* { useWasi: true }
* );
*
* try {
* const result = await plugin.call('count_vowels', 'hello world');
* const parsed = result.json();
*
* console.log(parsed); // { count: 3, total: 3, vowels: "aeiouAEIOU" }
* } finally {
* await plugin.close();
* }
* ```
*
* {@link Plugin | `Plugin`} can run on a background thread when the
* environment supports it. You can see if the current environment supports
* background plugins by checking the {@link Capabilities#hasWorkerCapability |
* `hasWorkerCapability`} property of {@link CAPABILITIES}.
*
* @param manifest A {@link ManifestLike | `ManifestLike`}. May be a `string`
* representing a URL, JSON, a path to a wasm file ({@link
* Capabilities#manifestSupportsPaths | in environments} where paths are
* supported); an [ArrayBuffer](https://mdn.io/ArrayBuffer); or a {@link
* Manifest}.
*
* @param opts {@link ExtismPluginOptions | options} for controlling the behavior
* of the plugin.
*
* @returns a promise for a {@link Plugin}.
*/
export async function createPlugin(
manifest: ManifestLike | PromiseLike<ManifestLike>,
opts: ExtismPluginOptions = {},
): Promise<Plugin> {
opts = { ...opts };
opts.useWasi ??= false;
opts.enableWasiOutput ??= opts.useWasi ? CAPABILITIES.extismStdoutEnvVarSet : false;
opts.functions = opts.functions || {};
opts.allowedPaths ??= {};
opts.allowedHosts ??= <any>[].concat(opts.allowedHosts || []);
opts.logger ??= console;
opts.config ??= {};
opts.fetch ??= fetch;
// TODO(chrisdickinson): reset this to `CAPABILITIES.hasWorkerCapability` once we've fixed https://github.com/extism/js-sdk/issues/46.
opts.runInWorker ??= false;
if (opts.runInWorker && !CAPABILITIES.hasWorkerCapability) {
throw new Error(
'Cannot enable off-thread wasm; current context is not `crossOriginIsolated` (see https://mdn.io/crossOriginIsolated)',
);
}
const [names, moduleData] = await _toWasmModuleData(await Promise.resolve(manifest), opts.fetch ?? fetch);
const ic: InternalConfig = {
allowedHosts: opts.allowedHosts as [],
allowedPaths: opts.allowedPaths,
functions: opts.functions,
fetch: opts.fetch || fetch,
wasiEnabled: opts.useWasi,
logger: opts.logger,
config: opts.config,
enableWasiOutput: opts.enableWasiOutput,
sharedArrayBufferSize: Number(opts.sharedArrayBufferSize) || 1 << 16,
};
return (opts.runInWorker ? _createBackgroundPlugin : _createForegroundPlugin)(ic, names, moduleData);
}
export { createPlugin as newPlugin };
export default createPlugin;