Skip to content

Commit

Permalink
lint, format
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdickinson committed Nov 19, 2024
1 parent d349c3e commit 17b8647
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 116 deletions.
1 change: 1 addition & 0 deletions examples/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const plugin = await createPlugin(filename, {

console.log('calling', { filename, funcname, input });
const res = await plugin.call(funcname, new TextEncoder().encode(input));
console.log(res)
// const s = new TextDecoder().decode(res.buffer);
// console.log(s);

Expand Down
1 change: 1 addition & 0 deletions examples/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function main() {

console.log('calling', { filename, funcname, input });
const res = await plugin.call(funcname, new TextEncoder().encode(input));
console.log(res)
// const s = new TextDecoder().decode(res.buffer);
// console.log(s);

Expand Down
24 changes: 12 additions & 12 deletions src/background-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BackgroundPlugin {
async #handleTimeout() {
// block new requests from coming in & the current request from settling
const request = this.#request;
this.#request = [() => {}, () => {}];
this.#request = [() => { }, () => { }];

const timedOut = {};
const failed = {};
Expand Down Expand Up @@ -210,7 +210,7 @@ class BackgroundPlugin {
await this.#handleTimeout();
}
},
() => {},
() => { },
);

this.worker.postMessage({
Expand Down Expand Up @@ -300,7 +300,7 @@ class BackgroundPlugin {
//
// - https://github.com/nodejs/node/pull/44409
// - https://github.com/denoland/deno/issues/14786
const timer = setInterval(() => {}, 0);
const timer = setInterval(() => { }, 0);
try {
if (!func) {
throw Error(`Plugin error: host function "${ev.namespace}" "${ev.func}" does not exist`);
Expand Down Expand Up @@ -425,7 +425,7 @@ class RingBufferWriter {

signal() {
const old = Atomics.load(this.flag, 0);
while (Atomics.compareExchange(this.flag, 0, old, this.outputOffset) === old) {}
while (Atomics.compareExchange(this.flag, 0, old, this.outputOffset) === old) { }
Atomics.notify(this.flag, 0, 1);
}

Expand Down Expand Up @@ -522,10 +522,10 @@ class HttpContext {
this.makeRequest(callContext, reqaddr, bodyaddr);
functions[EXTISM_ENV].http_status_code = () => this.lastStatusCode;
functions[EXTISM_ENV].http_headers = (callContext: CallContext) => {
if (this.lastHeaders === null){
if (this.lastHeaders === null) {
return 0n;
}
return callContext.store(JSON.stringify(this.lastHeaders));
return callContext.store(JSON.stringify(this.lastHeaders));
};
}

Expand All @@ -534,7 +534,7 @@ class HttpContext {
this.lastHeaders = {};
}
this.lastStatusCode = 0;

const req = callContext.read(reqaddr);
if (req === null) {
return 0n;
Expand Down Expand Up @@ -562,12 +562,12 @@ class HttpContext {

this.lastStatusCode = response.status;

if (this.lastHeaders !== null){
this.lastHeaders = Object.fromEntries(response.headers);
if (this.lastHeaders !== null) {
this.lastHeaders = Object.fromEntries(response.headers);
}

try {
let bytes = this.memoryOptions.maxHttpResponseBytes
const bytes = this.memoryOptions.maxHttpResponseBytes
? await readBodyUpTo(response, this.memoryOptions.maxHttpResponseBytes)
: new Uint8Array(await response.arrayBuffer());

Expand Down Expand Up @@ -637,7 +637,7 @@ async function createWorker(
names: string[],
modules: WebAssembly.Module[],
sharedData: SharedArrayBuffer,
onworker: (_w: Worker) => void = (_w: Worker) => {},
onworker: (_w: Worker) => void = (_w: Worker) => { },
): Promise<Worker> {
const worker = new Worker(WORKER_URL);
onworker(worker);
Expand Down Expand Up @@ -686,7 +686,7 @@ function timeout(ms: number | null, sentinel: any) {

async function terminateWorker(w: Worker) {
if (typeof (globalThis as any).Bun !== 'undefined') {
const timer = setTimeout(() => {}, 10);
const timer = setTimeout(() => { }, 10);
await w.terminate();
clearTimeout(timer);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/call-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export class CallContext {
},

http_headers: (): bigint => {
this.#logger.error("http_headers is not enabled");
this.#logger.error('http_headers is not enabled');
return 0n;
},

Expand Down
38 changes: 20 additions & 18 deletions src/foreground-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export const EXTISM_ENV = 'extism:host/env';
type InstantiatedModule = [WebAssembly.Module, WebAssembly.Instance];

interface SuspendingCtor {
new(fn: CallableFunction): any
new (fn: CallableFunction): any;
}

const AsyncFunction = (async () => { }).constructor
const Suspending: SuspendingCtor | undefined = (WebAssembly as any).Suspending
const promising: CallableFunction | undefined = (WebAssembly as any).promising
const AsyncFunction = (async () => {}).constructor;
const Suspending: SuspendingCtor | undefined = (WebAssembly as any).Suspending;
const promising: CallableFunction | undefined = (WebAssembly as any).promising;

export class ForegroundPlugin {
#context: CallContext;
Expand All @@ -28,7 +28,7 @@ export class ForegroundPlugin {
context: CallContext,
instancePair: InstantiatedModule,
wasi: InternalWasi[],
suspendsOnInvoke: boolean
suspendsOnInvoke: boolean,
) {
this.#context = context;
this.#instancePair = instancePair;
Expand Down Expand Up @@ -133,19 +133,21 @@ export async function createForegroundPlugin(
env: {},
};

let suspendsOnInvoke = false
let suspendsOnInvoke = false;
for (const namespace in opts.functions) {
imports[namespace] = imports[namespace] || {};
for (const [name, func] of Object.entries(opts.functions[namespace])) {
const isAsync = func.constructor === AsyncFunction
suspendsOnInvoke ||= isAsync
const wrapped = func.bind(null, context)
imports[namespace][name] = isAsync ? new (WebAssembly as any).Suspending(wrapped) : wrapped
const isAsync = func.constructor === AsyncFunction;
suspendsOnInvoke ||= isAsync;
const wrapped = func.bind(null, context);
imports[namespace][name] = isAsync ? new (WebAssembly as any).Suspending(wrapped) : wrapped;
}
}

if (suspendsOnInvoke && (!Suspending || !promising)) {
throw new TypeError('This platform does not support async function imports on the main thread; consider using `runInWorker`.')
throw new TypeError(
'This platform does not support async function imports on the main thread; consider using `runInWorker`.',
);
}

// find the "main" module and try to instantiate it.
Expand Down Expand Up @@ -246,9 +248,9 @@ async function instantiateModule(
const instance = providerExports.find((xs) => xs.name === '_start')
? await instantiateModule([...current, module], provider, imports, opts, wasiList, names, modules, new Map())
: !linked.has(provider)
? (await instantiateModule([...current, module], provider, imports, opts, wasiList, names, modules, linked),
linked.get(provider))
: linked.get(provider);
? (await instantiateModule([...current, module], provider, imports, opts, wasiList, names, modules, linked),
linked.get(provider))
: linked.get(provider);

if (!instance) {
// circular import, either make a trampoline or bail
Expand Down Expand Up @@ -289,10 +291,10 @@ async function instantiateModule(
const guestType = instance.exports.hs_init
? 'haskell'
: instance.exports._initialize
? 'reactor'
: instance.exports._start
? 'command'
: 'none';
? 'reactor'
: instance.exports._start
? 'command'
: 'none';

if (wasi) {
await wasi?.initialize(instance);
Expand Down
19 changes: 9 additions & 10 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ export interface ExtismPluginOptions {
* ```
*/
functions?:
| {
[key: string]: {
[key: string]: (callContext: CallContext, ...args: any[]) => any;
};
}
| undefined;
| {
[key: string]: {
[key: string]: (callContext: CallContext, ...args: any[]) => any;
};
}
| undefined;
allowedPaths?: { [key: string]: string } | undefined;

/**
Expand Down Expand Up @@ -208,9 +208,9 @@ export interface ExtismPluginOptions {
fetch?: typeof fetch;
sharedArrayBufferSize?: number;

/**
/**
* Determines whether or not HTTP response headers should be exposed to plugins,
* when set to `true`, `extism:host/env::http_headers` will return the response
* when set to `true`, `extism:host/env::http_headers` will return the response
* headers for HTTP requests made using `extism:host/env::http_request`
*/
allowHttpResponseHeaders?: boolean;
Expand Down Expand Up @@ -242,7 +242,7 @@ type SnakeCase<T extends Record<string, any>> = {
};

export interface NativeManifestOptions
extends Pick<ExtismPluginOptions, 'allowedPaths' | 'allowedHosts' | 'memory' | 'config' | 'timeoutMs'> { }
extends Pick<ExtismPluginOptions, 'allowedPaths' | 'allowedHosts' | 'memory' | 'config' | 'timeoutMs'> {}
/**
* The subset of {@link ExtismPluginOptions} attributes available for configuration via
* a {@link Manifest}. If an attribute is specified at both the {@link ExtismPluginOptions} and
Expand Down Expand Up @@ -392,7 +392,6 @@ export interface Capabilities {
*/
supportsJSPromiseInterface: boolean;


/**
* Whether or not the environment allows SharedArrayBuffers to be passed to `TextDecoder.decode` and `TextEncoder.encodeInto` directly
*
Expand Down
9 changes: 4 additions & 5 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
ManifestWasmResponse,
ManifestWasmModule,
ManifestLike,
PluginConfigLike,
ManifestOptions,
} from './interfaces.ts';
import { readFile } from './polyfills/node-fs.ts';
Expand Down Expand Up @@ -87,10 +86,10 @@ function parseManifestFromJson(json: string): Manifest {
config: parsed.config,
...(parsed.memory
? {
maxHttpResponseBytes: parsed.memory.maxHttpResponseBytes ?? parsed.memory.max_http_response_bytes,
maxPages: parsed.memory.maxPages ?? parsed.memory.max_pages,
maxVarBytes: parsed.memory.maxVarBytes ?? parsed.memory.max_var_bytes,
}
maxHttpResponseBytes: parsed.memory.maxHttpResponseBytes ?? parsed.memory.max_http_response_bytes,
maxPages: parsed.memory.maxPages ?? parsed.memory.max_pages,
maxVarBytes: parsed.memory.maxVarBytes ?? parsed.memory.max_var_bytes,
}
: {}),
};
}
Expand Down
Loading

0 comments on commit 17b8647

Please sign in to comment.