@spaceteams/warp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.mts +71 -0
- package/dist/index.mjs +168 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Spaceteams
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
//#region src/middleware.d.ts
|
|
2
|
+
type NoRunOptions = NonNullable<unknown>;
|
|
3
|
+
type NoScopeContext = NonNullable<unknown>;
|
|
4
|
+
type Middleware<AmbientContext, RunOptions = NoRunOptions, ScopeContext = NoScopeContext> = <T>(ctx: AmbientContext, options: Partial<RunOptions>, next: (ctx: AmbientContext & ScopeContext) => Promise<T> | T) => Promise<T> | T;
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/run.d.ts
|
|
7
|
+
type Run<AmbientContext, RunOptions, ScopeContext = NoScopeContext> = AmbientContext & {
|
|
8
|
+
run: <T>(options: RunOptions, inner: (app: Run<AmbientContext & Partial<ScopeContext>, RunOptions, ScopeContext>) => Promise<T> | T) => Promise<T> | T;
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/component/index.d.ts
|
|
12
|
+
type NoDeps = NonNullable<unknown>;
|
|
13
|
+
declare const COMPONENT: unique symbol;
|
|
14
|
+
type ComponentRef<Ctx, RunOptions, Out> = {
|
|
15
|
+
readonly [COMPONENT]: true;
|
|
16
|
+
readonly __ctx?: Ctx;
|
|
17
|
+
readonly __runOptions?: RunOptions;
|
|
18
|
+
readonly __out?: Out;
|
|
19
|
+
};
|
|
20
|
+
type ComponentFactory<Ctx, RunOptions, Deps, Out> = (ctx: Run<Ctx & Deps, RunOptions>) => Out;
|
|
21
|
+
type ComponentInput<Ctx, RunOptions, Out> = ComponentRef<Ctx, RunOptions, Out> | Out;
|
|
22
|
+
type Component<Ctx, RunOptions, Deps, Out> = ComponentRef<Ctx, RunOptions, Out> & {
|
|
23
|
+
factory: ComponentFactory<Ctx, RunOptions, Deps, Out>;
|
|
24
|
+
deps?: { [K in keyof Deps]: ComponentRef<Ctx, RunOptions, Deps[K]> };
|
|
25
|
+
name?: string;
|
|
26
|
+
};
|
|
27
|
+
type ComponentDefinition<Ctx, RunOptions, Deps, Out> = {
|
|
28
|
+
factory: ComponentFactory<Ctx, RunOptions, Deps, Out>;
|
|
29
|
+
deps?: { [K in keyof Deps]: ComponentInput<Ctx, RunOptions, Deps[K]> };
|
|
30
|
+
name?: string;
|
|
31
|
+
};
|
|
32
|
+
type InferComponentParams<T> = T extends Component<infer Ctx, infer RunOptions, infer Deps, infer Out> ? [Ctx, RunOptions, Deps, Out] : never;
|
|
33
|
+
type InferComponentCtx<T> = InferComponentParams<T>[0];
|
|
34
|
+
type InferComponentRunOptions<T> = InferComponentParams<T>[1];
|
|
35
|
+
type InferComponentDeps<T> = InferComponentParams<T>[2];
|
|
36
|
+
type InferComponentOut<T> = InferComponentParams<T>[3];
|
|
37
|
+
declare function brandComponent<T extends object, Ctx, RunOptions, Out>(obj: T): T & ComponentRef<Ctx, RunOptions, Out>;
|
|
38
|
+
declare function isComponent(value: unknown): value is Component<unknown, unknown, unknown, unknown>;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/runtime/runtime.d.ts
|
|
41
|
+
type SafeIntersect<A, B> = A extends undefined ? B : A & B;
|
|
42
|
+
type OptionalArg<A> = A extends undefined ? [] : [A];
|
|
43
|
+
declare class Runtime<Ctx, ActualContext extends Ctx, ScopeContext, RunOptions, Requirements = undefined> {
|
|
44
|
+
private readonly middleware;
|
|
45
|
+
private readonly ctx;
|
|
46
|
+
private readonly resolveFn;
|
|
47
|
+
constructor(middleware: Middleware<Ctx, RunOptions, ScopeContext>, ctx: ActualContext);
|
|
48
|
+
provide<Extension>(ext: Extension): Runtime<Ctx, ActualContext & Extension, ScopeContext, RunOptions, Requirements>;
|
|
49
|
+
require<Extension>(): Runtime<Ctx, ActualContext, ScopeContext, RunOptions, SafeIntersect<Requirements, Extension>>;
|
|
50
|
+
resolve: <Deps, Out>(component: Component<SafeIntersect<Requirements, ActualContext>, RunOptions, Deps, Out>, ...requirements: OptionalArg<Requirements>) => Out | Promise<Out>;
|
|
51
|
+
get component(): <Deps, F extends ComponentFactory<SafeIntersect<Requirements, ActualContext>, RunOptions, Deps, ReturnType<F>>>(factory: F, deps?: { [T in keyof Deps]: ComponentInput<SafeIntersect<Requirements, ActualContext>, RunOptions, Deps[T]> } | undefined, opts?: {
|
|
52
|
+
name?: string | undefined;
|
|
53
|
+
} | undefined) => Component<SafeIntersect<Requirements, ActualContext>, RunOptions, Deps, ReturnType<F>>;
|
|
54
|
+
get classComponent(): <Deps, Ctor extends new (deps: Run<SafeIntersect<Requirements, ActualContext> & Deps, RunOptions>) => InstanceType<Ctor>>(ctor: Ctor, deps?: { [K in keyof Deps]: ComponentInput<SafeIntersect<Requirements, ActualContext>, RunOptions, Deps[K]> } | undefined, opts?: {
|
|
55
|
+
name?: string | undefined;
|
|
56
|
+
} | undefined) => Component<SafeIntersect<Requirements, ActualContext>, RunOptions, Deps, InstanceType<Ctor>>;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/runtime/runtime-builder.d.ts
|
|
60
|
+
declare class RuntimeBuilder<AmbientContext, Options, ScopeContext = NoScopeContext> {
|
|
61
|
+
private readonly middlewares;
|
|
62
|
+
constructor(middlewares?: Middleware<AmbientContext, Options, ScopeContext>[]);
|
|
63
|
+
use<A, H, S>(mw: Middleware<A, H, S>): RuntimeBuilder<AmbientContext & A, Options & H, ScopeContext & S>;
|
|
64
|
+
provide<ActualCtx extends AmbientContext>(ctx: ActualCtx): Runtime<AmbientContext, ActualCtx, ScopeContext, Options, undefined>;
|
|
65
|
+
private buildMiddleware;
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/runtime/index.d.ts
|
|
69
|
+
declare function buildRuntime(): RuntimeBuilder<Record<string, unknown>, unknown>;
|
|
70
|
+
//#endregion
|
|
71
|
+
export { Component, ComponentDefinition, ComponentFactory, ComponentInput, ComponentRef, InferComponentCtx, InferComponentDeps, InferComponentOut, InferComponentParams, InferComponentRunOptions, Middleware, NoDeps, NoRunOptions, NoScopeContext, Run, brandComponent, buildRuntime, isComponent };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
//#region src/component/index.ts
|
|
2
|
+
const COMPONENT = Symbol("component");
|
|
3
|
+
function brandComponent(obj) {
|
|
4
|
+
Object.defineProperty(obj, COMPONENT, {
|
|
5
|
+
value: true,
|
|
6
|
+
enumerable: false,
|
|
7
|
+
configurable: false,
|
|
8
|
+
writable: false
|
|
9
|
+
});
|
|
10
|
+
return obj;
|
|
11
|
+
}
|
|
12
|
+
function isComponent(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && value[COMPONENT] === true;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/component/class-component.ts
|
|
17
|
+
function defineClassComponent() {
|
|
18
|
+
return (ctor, deps, opts) => {
|
|
19
|
+
const factory = (ctx) => {
|
|
20
|
+
return new ctor(ctx);
|
|
21
|
+
};
|
|
22
|
+
return brandComponent({
|
|
23
|
+
factory,
|
|
24
|
+
deps,
|
|
25
|
+
name: opts?.name ?? ctor.name
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/component/functional-component.ts
|
|
31
|
+
function defineFunctionalComponent() {
|
|
32
|
+
return (factory, deps, opts) => {
|
|
33
|
+
return brandComponent({
|
|
34
|
+
factory,
|
|
35
|
+
deps,
|
|
36
|
+
name: opts?.name
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/runtime/create-resolver.ts
|
|
42
|
+
function createResolver(mw) {
|
|
43
|
+
return (root, ctx) => {
|
|
44
|
+
const stack = [];
|
|
45
|
+
const checkCyclicDependency = (depPath) => {
|
|
46
|
+
const depName = depPath.split(".").at(-1);
|
|
47
|
+
if (!depName) return;
|
|
48
|
+
for (const stackPath of stack) if (stackPath.split(".").includes(depName)) throw new Error(`Cyclic dependency: ${[...stack, depPath].join(" -> ")}`);
|
|
49
|
+
};
|
|
50
|
+
const withStackTracking = (depPath, fn) => {
|
|
51
|
+
stack.push(depPath);
|
|
52
|
+
try {
|
|
53
|
+
return fn();
|
|
54
|
+
} finally {
|
|
55
|
+
stack.pop();
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const defineDependencyProperty = (target, depName, depPath, depComp, scopeCtx, cache) => {
|
|
59
|
+
Object.defineProperty(target, depName, {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: false,
|
|
62
|
+
get() {
|
|
63
|
+
if (cache.has(depName)) return cache.get(depName);
|
|
64
|
+
checkCyclicDependency(depPath);
|
|
65
|
+
const out = withStackTracking(depPath, () => bindInScope(depComp, scopeCtx, depPath));
|
|
66
|
+
cache.set(depName, out);
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
const attachDependencies = (runCtx, deps, scopeCtx, pathPrefix, cache) => {
|
|
72
|
+
for (const [depName, depComp] of Object.entries(deps)) defineDependencyProperty(runCtx, depName, pathPrefix ? `${pathPrefix}.${depName}` : depName, depComp, scopeCtx, cache);
|
|
73
|
+
};
|
|
74
|
+
const createBaseRunContext = (scopeCtx) => {
|
|
75
|
+
return {
|
|
76
|
+
...scopeCtx,
|
|
77
|
+
run: (nestedOptions, nestedInner) => runWithContext(scopeCtx, nestedOptions, nestedInner)
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const bindInScope = (comp, scopeCtx, path) => {
|
|
81
|
+
const localDepCache = /* @__PURE__ */ new Map();
|
|
82
|
+
const runCtx = createBaseRunContext(scopeCtx);
|
|
83
|
+
if (!isComponent(comp)) return comp;
|
|
84
|
+
attachDependencies(runCtx, comp.deps ?? {}, scopeCtx, path, localDepCache);
|
|
85
|
+
return comp.factory(runCtx);
|
|
86
|
+
};
|
|
87
|
+
const makeRootRunContext = (scopeCtx) => {
|
|
88
|
+
const rootCache = /* @__PURE__ */ new Map();
|
|
89
|
+
const runCtx = createBaseRunContext(scopeCtx);
|
|
90
|
+
attachDependencies(runCtx, root.deps ?? {}, scopeCtx, "", rootCache);
|
|
91
|
+
return runCtx;
|
|
92
|
+
};
|
|
93
|
+
const runWithContext = async (currentCtx, options, inner) => {
|
|
94
|
+
return await mw(currentCtx, options, (scopedCtx) => {
|
|
95
|
+
return inner(makeRootRunContext(scopedCtx));
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
return root.factory(makeRootRunContext(ctx));
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/runtime/runtime.ts
|
|
103
|
+
var Runtime = class Runtime {
|
|
104
|
+
resolveFn;
|
|
105
|
+
constructor(middleware, ctx) {
|
|
106
|
+
this.middleware = middleware;
|
|
107
|
+
this.ctx = ctx;
|
|
108
|
+
this.resolveFn = createResolver(middleware);
|
|
109
|
+
}
|
|
110
|
+
provide(ext) {
|
|
111
|
+
return new Runtime(this.middleware, {
|
|
112
|
+
...this.ctx,
|
|
113
|
+
...ext
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
require() {
|
|
117
|
+
return new Runtime(this.middleware, this.ctx);
|
|
118
|
+
}
|
|
119
|
+
resolve = (component, ...requirements) => {
|
|
120
|
+
const req = requirements[0];
|
|
121
|
+
const ctx = req ? {
|
|
122
|
+
...this.ctx,
|
|
123
|
+
...req
|
|
124
|
+
} : { ...this.ctx };
|
|
125
|
+
return this.resolveFn(component, ctx);
|
|
126
|
+
};
|
|
127
|
+
get component() {
|
|
128
|
+
return defineFunctionalComponent();
|
|
129
|
+
}
|
|
130
|
+
get classComponent() {
|
|
131
|
+
return defineClassComponent();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/runtime/runtime-builder.ts
|
|
136
|
+
var RuntimeBuilder = class RuntimeBuilder {
|
|
137
|
+
middlewares;
|
|
138
|
+
constructor(middlewares = []) {
|
|
139
|
+
this.middlewares = middlewares;
|
|
140
|
+
}
|
|
141
|
+
use(mw) {
|
|
142
|
+
return new RuntimeBuilder([...this.middlewares, mw]);
|
|
143
|
+
}
|
|
144
|
+
provide(ctx) {
|
|
145
|
+
return new Runtime(this.buildMiddleware(), ctx);
|
|
146
|
+
}
|
|
147
|
+
buildMiddleware() {
|
|
148
|
+
const middlewares = this.middlewares;
|
|
149
|
+
return function composed(ctx, options, next) {
|
|
150
|
+
let index = -1;
|
|
151
|
+
const dispatch = (i, currentCtx) => {
|
|
152
|
+
if (i <= index) throw new Error("next() called multiple times");
|
|
153
|
+
index = i;
|
|
154
|
+
const mw = middlewares[i];
|
|
155
|
+
if (!mw) return next(currentCtx);
|
|
156
|
+
return mw(currentCtx, options, (nextCtx) => dispatch(i + 1, nextCtx));
|
|
157
|
+
};
|
|
158
|
+
return dispatch(0, ctx);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/runtime/index.ts
|
|
164
|
+
function buildRuntime() {
|
|
165
|
+
return new RuntimeBuilder();
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
export { brandComponent, buildRuntime, isComponent };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spaceteams/warp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Type-safe composition with execution scopes ",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/index.mjs",
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/spaceteams/warp.git",
|
|
19
|
+
"directory": "warp"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"dependency-injection",
|
|
23
|
+
"composition",
|
|
24
|
+
"context",
|
|
25
|
+
"runtime",
|
|
26
|
+
"middleware",
|
|
27
|
+
"typescript",
|
|
28
|
+
"request-scoped",
|
|
29
|
+
"dependency-graph"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsdown",
|
|
34
|
+
"dev": "tsdown --watch",
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:watch": "vitest",
|
|
38
|
+
"lint": "biome check .",
|
|
39
|
+
"lint:fix": "biome check --write .",
|
|
40
|
+
"format": "biome format --write ."
|
|
41
|
+
}
|
|
42
|
+
}
|