Skip to content

Commit ece1809

Browse files
author
Zachary Whitley
committed
feat(jco): let createJcoPolyfill accept a caller-supplied policy
Previously createJcoPolyfill hard-coded `policy: new AllowAllPolicy()` via `Omit<PolyfillConfig, 'policy'>` on its signature. That blocked the natural way to inject env vars / args / preopens into a jco-mode polyfill — composing with createCliPolicy({env, args}). This is a small API completion, not a behaviour change: when no policy is supplied the default is still AllowAllPolicy. The jco resource bridge (buildJcoImports — already producing InputStream / OutputStream class constructors with prototype methods) is untouched. Enables: const polyfill = createJcoPolyfill({ policy: createCliPolicy({ env: process.env, args: ... }) }) Use case: downstream wrappers (e.g. datafission's tools/browser-demo/wasi-polyfill-shim) that need to expose a preview2-shim-shaped surface (camelCase methods + resource classes on streams) backed by wasi-polyfill, with env captured at init.
1 parent 0531cbb commit ece1809

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

src/wasip2/core/polyfill.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -333,24 +333,29 @@ export function createDevPolyfill(): Polyfill {
333333
* Create a Polyfill pre-configured for jco-transpiled components
334334
*
335335
* This is a convenience function that:
336-
* - Creates a polyfill with the AllowAllPolicy (for development)
337-
* - Sets up jcoCompat mode by default
336+
* - Creates a polyfill with the AllowAllPolicy (for development) by default,
337+
* or a caller-supplied policy when one is provided
338+
* - Sets up jcoCompat mode by default (override via `jcoCompat: false`)
338339
*
339-
* Usage:
340-
* ```typescript
341-
* import { createJcoPolyfill, registerCorePlugins } from '@tegmentum/wasi-polyfill'
340+
* Composing with a configured policy is the supported way to inject
341+
* environment variables, args, preopens, etc. into a jco-mode polyfill:
342342
*
343-
* // Register plugins first
344-
* registerCorePlugins()
343+
* ```typescript
344+
* import { createJcoPolyfill, createCliPolicy } from '@tegmentum/wasi-polyfill'
345+
* import { environmentPlugin } from '@tegmentum/wasi-polyfill/wasip2/plugins/cli'
345346
*
346-
* const polyfill = createJcoPolyfill()
347-
* const { imports } = await polyfill.getImports(interfaces)
347+
* const polyfill = createJcoPolyfill({
348+
* policy: createCliPolicy({ env: process.env, args: process.argv.slice(2) })
349+
* })
350+
* polyfill.registerPlugin(environmentPlugin)
351+
* const { imports } = await polyfill.forInterfaces(['wasi:cli/environment@0.2.0'])
352+
* // imports['wasi:cli/environment'].getEnvironment() → process.env entries
348353
* ```
349354
*/
350-
export function createJcoPolyfill(config?: Omit<PolyfillConfig, 'policy'>): Polyfill {
355+
export function createJcoPolyfill(config?: PolyfillConfig): Polyfill {
351356
return new Polyfill({
352357
...config,
353-
policy: new AllowAllPolicy(),
358+
policy: config?.policy ?? new AllowAllPolicy(),
354359
jcoCompat: config?.jcoCompat ?? true,
355360
})
356361
}

0 commit comments

Comments
 (0)