ownscope
v0.0.4
Published
Framework-agnostic async scope transformer with optional framework runtime adapters
Readme
ownscope
Framework-agnostic async scope transformation for JavaScript and TypeScript. Mark an async function with "use scope" to restore a captured context around synchronous work after await. Choose a runtime adapter for your framework, or provide your own.
Install
npm install ownscopeInstall the relevant framework separately (for example solid-js, @angular/core, or vue). Vite users also install vite.
Transform
import { transform } from 'ownscope';
const source = `async function load() {
"use scope";
const data = await fetchData();
update(data);
}`;
const result = await transform(source, {
filename: 'app.js',
runtime: { from: 'ownscope/runtime/solid' }
});
console.log(result.code);Runtime selection is required. Use an adapter from the table below or a custom createScope() module. The captured context is restored only during synchronous callbacks. This does not preserve a framework's component lifecycle or reactive dependency listener across arbitrary awaits.
Public imports
| Import | API |
| --- | --- |
| ownscope | Async transform, transformTypescript, transformAcorn, Vite ownerScope |
| ownscope/typescript | Synchronous transform(code, options?) |
| ownscope/typescript/injected | createTransformer(ts) (reuses a provided TypeScript compiler) |
| ownscope/acorn | Async transform(code, options?) |
| ownscope/vite | ownerScope(options?) |
| ownscope/runtime | Generic createScopeFactory |
| ownscope/runtime/core | Generic createScopeFactory alias |
Runtime adapters
| Import | API | Purpose |
| --- | --- | --- |
| ownscope/runtime/solid | createScope() | Solid owner restoration |
| ownscope/runtime/angular | createScope() | Angular injection context |
| ownscope/runtime/vue | createScope() | Vue active effect scope (not component instance) |
| ownscope/runtime/vue/app | createVueAppScope(app) | Vue app-provided injection context |
| ownscope/runtime/nuxt | createScope() | Nuxt app context |
| ownscope/runtime/effector | createScope() | Effector fork callback binding |
| ownscope/runtime/unctx | createUnctxScopeFactory(context) | Existing unctx context |
| ownscope/runtime/mobx | createScope() | Experimental: actions, not context restoration |
| ownscope/runtime/node | createScope() | Node async-context snapshot |
Adapters only load when their subpath is imported. For Vue app or unctx, expose an application-specific module exporting the configured factory as createScope and use its path in runtime.from.
Vite
import { defineConfig } from 'vite';
import { ownerScope } from 'ownscope/vite';
export default defineConfig({
plugins: [ownerScope({
engine: 'typescript',
runtime: { from: 'ownscope/runtime/angular' }
})]
});Custom scope runtime
// ./my-scope.js
import { createScopeFactory } from 'ownscope/runtime';
export const createScope = createScopeFactory({
capture: () => getActiveContext(),
run: (context, callback) => runInContext(context, callback)
});Your createScope() must synchronously capture the context and return a callback runner that re-enters it, preserving callback return values and exceptions.
Inline factory (without importing a scope module)
const result = await transform(source, {
runtime: {
mode: 'inline',
factory: `function createScope() {
const context = getCurrentContext();
return callback => executeWithContext(context, callback);
}`,
imports: {
from: 'my-context-library',
names: ['getCurrentContext', 'executeWithContext']
}
}
});factory is required in inline mode. It must be a JavaScript function expression that returns a synchronous callback runner. imports is optional and may be a single {from, names} object or an array. runtime.provider no longer selects a built-in Solid inline helper.
Options and result
filename?: string, strict?: boolean, sourceMap?: boolean | 'traced' | 'hybrid', and runtime?: { mode?: 'import' | 'inline'; from?: string; name?: string; factory?: string; imports?: ... }.
Import mode uses runtime.from and defaults to the named export createScope (override with runtime.name). Source maps use exact trace provenance by default; sourceMap: false disables them.
Transforms return TransformResult: { code, changed, transformed, diagnostics, map }. The root and Acorn transform APIs are asynchronous; direct TypeScript transforms are synchronous.
Inject TypeScript
import ts from 'typescript';
import { createTransformer } from 'ownscope/typescript/injected';
const { transform } = createTransformer(ts);
const result = transform(source, { runtime: { from: './my-scope.js' } });The injected entry does not import TypeScript itself.
