@cibule/wiring 0.1.1
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/README.md +108 -0
- package/dist/index.cjs +197 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +167 -0
- package/dist/index.js.map +7 -0
- package/dist/lib/create-request-middleware.d.ts +15 -0
- package/dist/lib/create-request-middleware.d.ts.map +1 -0
- package/dist/lib/create-static-injector.d.ts +8 -0
- package/dist/lib/create-static-injector.d.ts.map +1 -0
- package/dist/lib/run-with-stores.d.ts +4 -0
- package/dist/lib/run-with-stores.d.ts.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# @cibule/wiring
|
|
2
|
+
|
|
3
|
+
Server wiring helpers for `@cibule/di`. Provides framework-agnostic middleware, static injector creation, and AsyncLocalStorage utilities for per-request dependency injection.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @cibule/wiring
|
|
9
|
+
# or
|
|
10
|
+
bun add @cibule/wiring
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @cibule/wiring
|
|
13
|
+
# or
|
|
14
|
+
yarn add @cibule/wiring
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
### `createRequestMiddleware<Env>(options)`
|
|
20
|
+
|
|
21
|
+
Creates a framework-agnostic middleware handler that sets up a per-request DI injector. Each request gets its own `Injector` instance, ensuring complete isolation between requests.
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { InjectionToken } from '@cibule/di';
|
|
25
|
+
import { createRequestMiddleware, INJECTOR_KEY } from '@cibule/wiring';
|
|
26
|
+
|
|
27
|
+
const DB = new InjectionToken<Database>('DB');
|
|
28
|
+
|
|
29
|
+
const middleware = createRequestMiddleware<Env>({
|
|
30
|
+
getProviders: env => [{ provide: DB, useFactory: () => createDb(env.DB_URL) }],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Hono example
|
|
34
|
+
app.use('*', middleware);
|
|
35
|
+
|
|
36
|
+
// Access the injector in handlers
|
|
37
|
+
app.get('/users', c => {
|
|
38
|
+
const injector = c.get(INJECTOR_KEY);
|
|
39
|
+
const db = injector.get(DB);
|
|
40
|
+
// ...
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**Options:**
|
|
45
|
+
|
|
46
|
+
| Option | Type | Description |
|
|
47
|
+
| ---------------- | -------------------------- | ---------------------------------------------- |
|
|
48
|
+
| `getProviders` | `(env: Env) => Provider[]` | Returns providers for the per-request injector |
|
|
49
|
+
| `parentInjector` | `Injector` (optional) | Parent injector for hierarchical DI |
|
|
50
|
+
|
|
51
|
+
### `createStaticInjector(options)`
|
|
52
|
+
|
|
53
|
+
Creates a static `Injector` instance. Useful for app-level singletons that live outside the request lifecycle.
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { InjectionToken } from '@cibule/di';
|
|
57
|
+
import { createStaticInjector } from '@cibule/wiring';
|
|
58
|
+
|
|
59
|
+
const CONFIG = new InjectionToken<AppConfig>('CONFIG');
|
|
60
|
+
|
|
61
|
+
const appInjector = createStaticInjector({
|
|
62
|
+
providers: [{ provide: CONFIG, useValue: { port: 3000 } }],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Use as parent for request-scoped injectors
|
|
66
|
+
const middleware = createRequestMiddleware<Env>({
|
|
67
|
+
parentInjector: appInjector,
|
|
68
|
+
getProviders: env => [
|
|
69
|
+
/* request-scoped providers */
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `runWithStores(stores, fn)`
|
|
75
|
+
|
|
76
|
+
Runs a function within multiple `AsyncLocalStorage` contexts. Nests stores recursively so all values are accessible inside `fn`.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
80
|
+
import { runWithStores } from '@cibule/wiring';
|
|
81
|
+
|
|
82
|
+
const requestId = new AsyncLocalStorage<string>();
|
|
83
|
+
const userId = new AsyncLocalStorage<number>();
|
|
84
|
+
|
|
85
|
+
const result = runWithStores(
|
|
86
|
+
[
|
|
87
|
+
[requestId, 'req-123'],
|
|
88
|
+
[userId, 42],
|
|
89
|
+
],
|
|
90
|
+
() => {
|
|
91
|
+
console.log(requestId.getStore()); // 'req-123'
|
|
92
|
+
console.log(userId.getStore()); // 42
|
|
93
|
+
return 'done';
|
|
94
|
+
},
|
|
95
|
+
);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Runtime Requirements
|
|
99
|
+
|
|
100
|
+
Requires a runtime with `AsyncLocalStorage` support (Node.js, Bun, or Cloudflare Workers with `nodejs_compat`). The `createRequestMiddleware` and `createStaticInjector` functions work on any runtime.
|
|
101
|
+
|
|
102
|
+
## Building
|
|
103
|
+
|
|
104
|
+
Run `nx build wiring` to build the library.
|
|
105
|
+
|
|
106
|
+
## Running unit tests
|
|
107
|
+
|
|
108
|
+
Run `nx test wiring` to execute the unit tests via [Vitest](https://vitest.dev/).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
INJECTOR_KEY: () => INJECTOR_KEY,
|
|
24
|
+
createRequestMiddleware: () => createRequestMiddleware,
|
|
25
|
+
createStaticInjector: () => createStaticInjector,
|
|
26
|
+
runWithStores: () => runWithStores
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// ../di/src/lib/context.ts
|
|
31
|
+
var currentInjector = null;
|
|
32
|
+
function setCurrentInjector(injector) {
|
|
33
|
+
const previous = currentInjector;
|
|
34
|
+
currentInjector = injector;
|
|
35
|
+
return previous;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ../di/src/lib/injection-token.ts
|
|
39
|
+
var InjectionToken = class {
|
|
40
|
+
constructor(description) {
|
|
41
|
+
this.description = description;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// ../di/src/lib/injector.ts
|
|
46
|
+
var Injector = class _Injector {
|
|
47
|
+
constructor(providers, parent) {
|
|
48
|
+
this.parent = parent;
|
|
49
|
+
this.register({ provide: _Injector, useValue: this });
|
|
50
|
+
for (const provider of this.flatten(providers)) {
|
|
51
|
+
this.register(provider);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
records = /* @__PURE__ */ new Map();
|
|
55
|
+
static create(options) {
|
|
56
|
+
return new _Injector(options.providers, options.parent);
|
|
57
|
+
}
|
|
58
|
+
get(token, options) {
|
|
59
|
+
const record = this.records.get(token);
|
|
60
|
+
if (record) {
|
|
61
|
+
return this.resolve(record);
|
|
62
|
+
}
|
|
63
|
+
if (this.parent) {
|
|
64
|
+
return options ? this.parent.get(token, options) : this.parent.get(token);
|
|
65
|
+
}
|
|
66
|
+
if (options?.optional) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`No provider for ${this.tokenName(token)}`);
|
|
70
|
+
}
|
|
71
|
+
runInContext(fn) {
|
|
72
|
+
const previous = setCurrentInjector(this);
|
|
73
|
+
try {
|
|
74
|
+
return fn();
|
|
75
|
+
} finally {
|
|
76
|
+
setCurrentInjector(previous);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
resolve(record) {
|
|
80
|
+
if (!record.resolved) {
|
|
81
|
+
record.value = this.runInContext(() => this.invokeFactories(record));
|
|
82
|
+
record.resolved = true;
|
|
83
|
+
}
|
|
84
|
+
return record.value;
|
|
85
|
+
}
|
|
86
|
+
invokeFactories(record) {
|
|
87
|
+
if (!record.multi) {
|
|
88
|
+
const factory = record.factories[0];
|
|
89
|
+
if (!factory) {
|
|
90
|
+
throw new Error("Record has no factory");
|
|
91
|
+
}
|
|
92
|
+
return factory();
|
|
93
|
+
}
|
|
94
|
+
const parentValues = this.resolveParentMulti(record);
|
|
95
|
+
return [...parentValues, ...record.factories.map((f) => f())];
|
|
96
|
+
}
|
|
97
|
+
register(provider) {
|
|
98
|
+
if (typeof provider === "function") {
|
|
99
|
+
this.addRecord(provider, {
|
|
100
|
+
token: provider,
|
|
101
|
+
factories: [() => new provider()],
|
|
102
|
+
resolved: false,
|
|
103
|
+
multi: false
|
|
104
|
+
});
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const token = provider.provide;
|
|
108
|
+
const multi = provider.multi === true;
|
|
109
|
+
const factory = this.createFactory(provider);
|
|
110
|
+
this.addRecord(token, { token, factories: [factory], resolved: false, multi });
|
|
111
|
+
}
|
|
112
|
+
createFactory(provider) {
|
|
113
|
+
if ("useValue" in provider) {
|
|
114
|
+
return () => provider.useValue;
|
|
115
|
+
}
|
|
116
|
+
if ("useClass" in provider) {
|
|
117
|
+
return () => new provider.useClass();
|
|
118
|
+
}
|
|
119
|
+
return provider.useFactory;
|
|
120
|
+
}
|
|
121
|
+
addRecord(token, record) {
|
|
122
|
+
if (!record.multi) {
|
|
123
|
+
this.records.set(token, record);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const existing = this.records.get(token);
|
|
127
|
+
if (existing?.multi) {
|
|
128
|
+
existing.factories.push(...record.factories);
|
|
129
|
+
existing.resolved = false;
|
|
130
|
+
existing.value = void 0;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
this.records.set(token, record);
|
|
134
|
+
}
|
|
135
|
+
resolveParentMulti(record) {
|
|
136
|
+
if (!this.parent) {
|
|
137
|
+
return [];
|
|
138
|
+
}
|
|
139
|
+
const parentRecord = this.parent.records.get(record.token);
|
|
140
|
+
if (parentRecord?.multi) {
|
|
141
|
+
return this.parent.resolve(parentRecord);
|
|
142
|
+
}
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
flatten(providers) {
|
|
146
|
+
return providers.flatMap((p) => Array.isArray(p) ? this.flatten(p) : [p]);
|
|
147
|
+
}
|
|
148
|
+
tokenName(token) {
|
|
149
|
+
if (typeof token === "function") {
|
|
150
|
+
return token.name;
|
|
151
|
+
}
|
|
152
|
+
if (token instanceof InjectionToken) {
|
|
153
|
+
return token.description;
|
|
154
|
+
}
|
|
155
|
+
return token.name;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// src/lib/create-request-middleware.ts
|
|
160
|
+
var INJECTOR_KEY = "injector";
|
|
161
|
+
function createRequestMiddleware(options) {
|
|
162
|
+
return async (c, next) => {
|
|
163
|
+
const providers = options.getProviders(c.env);
|
|
164
|
+
const injector = Injector.create({
|
|
165
|
+
providers,
|
|
166
|
+
parent: options.parentInjector
|
|
167
|
+
});
|
|
168
|
+
c.set(INJECTOR_KEY, injector);
|
|
169
|
+
await injector.runInContext(next);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/lib/create-static-injector.ts
|
|
174
|
+
function createStaticInjector(options) {
|
|
175
|
+
return Injector.create({
|
|
176
|
+
providers: options.providers,
|
|
177
|
+
parent: options.parentInjector
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// src/lib/run-with-stores.ts
|
|
182
|
+
function runWithStores(stores, fn) {
|
|
183
|
+
const [first, ...rest] = stores;
|
|
184
|
+
if (!first) {
|
|
185
|
+
return fn();
|
|
186
|
+
}
|
|
187
|
+
const [storage, value] = first;
|
|
188
|
+
return storage.run(value, () => runWithStores(rest, fn));
|
|
189
|
+
}
|
|
190
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
191
|
+
0 && (module.exports = {
|
|
192
|
+
INJECTOR_KEY,
|
|
193
|
+
createRequestMiddleware,
|
|
194
|
+
createStaticInjector,
|
|
195
|
+
runWithStores
|
|
196
|
+
});
|
|
197
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts", "../../di/src/lib/context.ts", "../../di/src/lib/injection-token.ts", "../../di/src/lib/injector.ts", "../src/lib/create-request-middleware.ts", "../src/lib/create-static-injector.ts", "../src/lib/run-with-stores.ts"],
|
|
4
|
+
"sourcesContent": ["export type {\n MiddlewareContext,\n MiddlewareHandler,\n NextFunction,\n RequestMiddlewareOptions,\n} from './lib/create-request-middleware';\nexport { createRequestMiddleware, INJECTOR_KEY } from './lib/create-request-middleware';\nexport type { StaticInjectorOptions } from './lib/create-static-injector';\nexport { createStaticInjector } from './lib/create-static-injector';\nexport type { StoreEntry } from './lib/run-with-stores';\nexport { runWithStores } from './lib/run-with-stores';\n", "import type { Injector } from './injector';\n\nlet currentInjector: Injector | null = null;\n\nexport function getCurrentInjector(): Injector | null {\n return currentInjector;\n}\n\nexport function setCurrentInjector(injector: Injector | null): Injector | null {\n const previous = currentInjector;\n currentInjector = injector;\n return previous;\n}\n", "export class InjectionToken<T> {\n declare readonly _brand: T;\n constructor(public readonly description: string) {}\n}\n", "import { setCurrentInjector } from './context';\nimport { InjectionToken } from './injection-token';\nimport type { InjectOptions, Provider, SingleProvider } from './provider';\nimport type { Token } from './token';\n\ninterface InjectorRecord<T = unknown> {\n readonly token: Token<T>;\n factories: (() => T)[];\n value?: T;\n resolved: boolean;\n multi: boolean;\n}\n\nexport class Injector {\n private readonly records = new Map<Token<unknown>, InjectorRecord>();\n\n private constructor(\n providers: Provider[],\n private readonly parent?: Injector,\n ) {\n this.register({ provide: Injector as Token<Injector>, useValue: this });\n for (const provider of this.flatten(providers)) {\n this.register(provider);\n }\n }\n\n public static create(options: { providers: Provider[]; parent?: Injector }): Injector {\n return new Injector(options.providers, options.parent);\n }\n\n public get<T>(token: Token<T>): T;\n public get<T>(token: Token<T>, options: InjectOptions): T | null;\n public get<T>(token: Token<T>, options?: InjectOptions): T | null {\n const record = this.records.get(token) as InjectorRecord<T> | undefined;\n\n if (record) {\n return this.resolve(record);\n }\n\n if (this.parent) {\n return options ? this.parent.get(token, options) : this.parent.get(token);\n }\n\n if (options?.optional) {\n return null;\n }\n\n throw new Error(`No provider for ${this.tokenName(token)}`);\n }\n\n public runInContext<T>(fn: () => T): T {\n const previous = setCurrentInjector(this);\n try {\n return fn();\n } finally {\n setCurrentInjector(previous);\n }\n }\n\n private resolve<T>(record: InjectorRecord<T>): T {\n if (!record.resolved) {\n record.value = this.runInContext(() => this.invokeFactories(record));\n record.resolved = true;\n }\n return record.value as T;\n }\n\n private invokeFactories<T>(record: InjectorRecord<T>): T {\n if (!record.multi) {\n const factory = record.factories[0];\n if (!factory) {\n throw new Error('Record has no factory');\n }\n return factory();\n }\n const parentValues = this.resolveParentMulti(record);\n return [...parentValues, ...record.factories.map(f => f())] as T;\n }\n\n private register(provider: SingleProvider): void {\n if (typeof provider === 'function') {\n this.addRecord(provider, {\n token: provider,\n factories: [() => new provider()],\n resolved: false,\n multi: false,\n });\n return;\n }\n\n const token = provider.provide;\n const multi = provider.multi === true;\n const factory = this.createFactory(provider);\n\n this.addRecord(token, { token, factories: [factory], resolved: false, multi });\n }\n\n private createFactory(\n provider: Exclude<SingleProvider, new (...args: unknown[]) => unknown>,\n ): () => unknown {\n if ('useValue' in provider) {\n return () => provider.useValue;\n }\n if ('useClass' in provider) {\n return () => new provider.useClass();\n }\n return provider.useFactory;\n }\n\n private addRecord(token: Token<unknown>, record: InjectorRecord): void {\n if (!record.multi) {\n this.records.set(token, record);\n return;\n }\n\n const existing = this.records.get(token);\n\n if (existing?.multi) {\n existing.factories.push(...record.factories);\n existing.resolved = false;\n existing.value = undefined;\n return;\n }\n\n this.records.set(token, record);\n }\n\n private resolveParentMulti(record: InjectorRecord): unknown[] {\n if (!this.parent) {\n return [];\n }\n\n const parentRecord = this.parent.records.get(record.token);\n\n if (parentRecord?.multi) {\n return this.parent.resolve(parentRecord) as unknown[];\n }\n\n return [];\n }\n\n private flatten(providers: Provider[]): SingleProvider[] {\n return providers.flatMap(p => (Array.isArray(p) ? this.flatten(p) : [p]));\n }\n\n private tokenName(token: Token<unknown>): string {\n if (typeof token === 'function') {\n return token.name;\n }\n if (token instanceof InjectionToken) {\n return token.description;\n }\n return token.name;\n }\n}\n", "import type { Provider } from '@cibule/di';\nimport { Injector } from '@cibule/di';\n\nexport interface RequestMiddlewareOptions<Env = unknown> {\n readonly parentInjector?: Injector;\n readonly getProviders: (env: Env) => Provider[];\n}\n\nexport interface MiddlewareContext<Env = unknown> {\n readonly env: Env;\n set(key: string, value: unknown): void;\n}\n\nexport const INJECTOR_KEY = 'injector' as const;\n\nexport type NextFunction = () => Promise<void>;\n\nexport type MiddlewareHandler<Env = unknown> = (\n c: MiddlewareContext<Env>,\n next: NextFunction,\n) => Promise<void>;\n\nexport function createRequestMiddleware<Env>(\n options: RequestMiddlewareOptions<Env>,\n): MiddlewareHandler<Env> {\n return async (c: MiddlewareContext<Env>, next: NextFunction): Promise<void> => {\n const providers = options.getProviders(c.env);\n const injector = Injector.create({\n providers,\n parent: options.parentInjector,\n });\n\n c.set(INJECTOR_KEY, injector);\n\n await injector.runInContext(next);\n };\n}\n", "import type { Provider } from '@cibule/di';\nimport { Injector } from '@cibule/di';\n\nexport interface StaticInjectorOptions {\n readonly providers: Provider[];\n readonly parentInjector?: Injector;\n}\n\nexport function createStaticInjector(options: StaticInjectorOptions): Injector {\n return Injector.create({\n providers: options.providers,\n parent: options.parentInjector,\n });\n}\n", "import type { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type StoreEntry<T = unknown> = readonly [AsyncLocalStorage<T>, T];\n\nexport function runWithStores<R>(stores: ReadonlyArray<StoreEntry>, fn: () => R): R {\n const [first, ...rest] = stores;\n\n if (!first) {\n return fn();\n }\n\n const [storage, value] = first;\n\n return storage.run(value, () => runWithStores(rest, fn));\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,IAAI,kBAAmC;AAMhC,SAAS,mBAAmB,UAA4C;AAC7E,QAAM,WAAW;AACjB,oBAAkB;AAClB,SAAO;AACT;;;ACZO,IAAM,iBAAN,MAAwB;AAAA,EAE7B,YAA4B,aAAqB;AAArB;AAAA,EAAsB;AACpD;;;ACUO,IAAM,WAAN,MAAM,UAAS;AAAA,EAGZ,YACN,WACiB,QACjB;AADiB;AAEjB,SAAK,SAAS,EAAE,SAAS,WAA6B,UAAU,KAAK,CAAC;AACtE,eAAW,YAAY,KAAK,QAAQ,SAAS,GAAG;AAC9C,WAAK,SAAS,QAAQ;AAAA,IACxB;AAAA,EACF;AAAA,EAViB,UAAU,oBAAI,IAAoC;AAAA,EAYnE,OAAc,OAAO,SAAiE;AACpF,WAAO,IAAI,UAAS,QAAQ,WAAW,QAAQ,MAAM;AAAA,EACvD;AAAA,EAIO,IAA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AAA,IACf;AACA,WAAO,MAAM;AAAA,EACf;AACF;;;AC7IO,IAAM,eAAe;AASrB,SAAS,wBACd,SACwB;AACxB,SAAO,OAAO,GAA2B,SAAsC;AAC7E,UAAM,YAAY,QAAQ,aAAa,EAAE,GAAG;AAC5C,UAAM,WAAW,SAAS,OAAO;AAAA,MAC/B;AAAA,MACA,QAAQ,QAAQ;AAAA,IAClB,CAAC;AAED,MAAE,IAAI,cAAc,QAAQ;AAE5B,UAAM,SAAS,aAAa,IAAI;AAAA,EAClC;AACF;;;AC5BO,SAAS,qBAAqB,SAA0C;AAC7E,SAAO,SAAS,OAAO;AAAA,IACrB,WAAW,QAAQ;AAAA,IACnB,QAAQ,QAAQ;AAAA,EAClB,CAAC;AACH;;;ACTO,SAAS,cAAiB,QAAmC,IAAgB;AAClF,QAAM,CAAC,OAAO,GAAG,IAAI,IAAI;AAEzB,MAAI,CAAC,OAAO;AACV,WAAO,GAAG;AAAA,EACZ;AAEA,QAAM,CAAC,SAAS,KAAK,IAAI;AAEzB,SAAO,QAAQ,IAAI,OAAO,MAAM,cAAc,MAAM,EAAE,CAAC;AACzD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type { MiddlewareContext, MiddlewareHandler, NextFunction, RequestMiddlewareOptions, } from './lib/create-request-middleware';
|
|
2
|
+
export { createRequestMiddleware, INJECTOR_KEY } from './lib/create-request-middleware';
|
|
3
|
+
export type { StaticInjectorOptions } from './lib/create-static-injector';
|
|
4
|
+
export { createStaticInjector } from './lib/create-static-injector';
|
|
5
|
+
export type { StoreEntry } from './lib/run-with-stores';
|
|
6
|
+
export { runWithStores } from './lib/run-with-stores';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,YAAY,EACZ,wBAAwB,GACzB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,uBAAuB,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AACxF,YAAY,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,YAAY,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// ../di/src/lib/context.ts
|
|
2
|
+
var currentInjector = null;
|
|
3
|
+
function setCurrentInjector(injector) {
|
|
4
|
+
const previous = currentInjector;
|
|
5
|
+
currentInjector = injector;
|
|
6
|
+
return previous;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// ../di/src/lib/injection-token.ts
|
|
10
|
+
var InjectionToken = class {
|
|
11
|
+
constructor(description) {
|
|
12
|
+
this.description = description;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// ../di/src/lib/injector.ts
|
|
17
|
+
var Injector = class _Injector {
|
|
18
|
+
constructor(providers, parent) {
|
|
19
|
+
this.parent = parent;
|
|
20
|
+
this.register({ provide: _Injector, useValue: this });
|
|
21
|
+
for (const provider of this.flatten(providers)) {
|
|
22
|
+
this.register(provider);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
records = /* @__PURE__ */ new Map();
|
|
26
|
+
static create(options) {
|
|
27
|
+
return new _Injector(options.providers, options.parent);
|
|
28
|
+
}
|
|
29
|
+
get(token, options) {
|
|
30
|
+
const record = this.records.get(token);
|
|
31
|
+
if (record) {
|
|
32
|
+
return this.resolve(record);
|
|
33
|
+
}
|
|
34
|
+
if (this.parent) {
|
|
35
|
+
return options ? this.parent.get(token, options) : this.parent.get(token);
|
|
36
|
+
}
|
|
37
|
+
if (options?.optional) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`No provider for ${this.tokenName(token)}`);
|
|
41
|
+
}
|
|
42
|
+
runInContext(fn) {
|
|
43
|
+
const previous = setCurrentInjector(this);
|
|
44
|
+
try {
|
|
45
|
+
return fn();
|
|
46
|
+
} finally {
|
|
47
|
+
setCurrentInjector(previous);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
resolve(record) {
|
|
51
|
+
if (!record.resolved) {
|
|
52
|
+
record.value = this.runInContext(() => this.invokeFactories(record));
|
|
53
|
+
record.resolved = true;
|
|
54
|
+
}
|
|
55
|
+
return record.value;
|
|
56
|
+
}
|
|
57
|
+
invokeFactories(record) {
|
|
58
|
+
if (!record.multi) {
|
|
59
|
+
const factory = record.factories[0];
|
|
60
|
+
if (!factory) {
|
|
61
|
+
throw new Error("Record has no factory");
|
|
62
|
+
}
|
|
63
|
+
return factory();
|
|
64
|
+
}
|
|
65
|
+
const parentValues = this.resolveParentMulti(record);
|
|
66
|
+
return [...parentValues, ...record.factories.map((f) => f())];
|
|
67
|
+
}
|
|
68
|
+
register(provider) {
|
|
69
|
+
if (typeof provider === "function") {
|
|
70
|
+
this.addRecord(provider, {
|
|
71
|
+
token: provider,
|
|
72
|
+
factories: [() => new provider()],
|
|
73
|
+
resolved: false,
|
|
74
|
+
multi: false
|
|
75
|
+
});
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const token = provider.provide;
|
|
79
|
+
const multi = provider.multi === true;
|
|
80
|
+
const factory = this.createFactory(provider);
|
|
81
|
+
this.addRecord(token, { token, factories: [factory], resolved: false, multi });
|
|
82
|
+
}
|
|
83
|
+
createFactory(provider) {
|
|
84
|
+
if ("useValue" in provider) {
|
|
85
|
+
return () => provider.useValue;
|
|
86
|
+
}
|
|
87
|
+
if ("useClass" in provider) {
|
|
88
|
+
return () => new provider.useClass();
|
|
89
|
+
}
|
|
90
|
+
return provider.useFactory;
|
|
91
|
+
}
|
|
92
|
+
addRecord(token, record) {
|
|
93
|
+
if (!record.multi) {
|
|
94
|
+
this.records.set(token, record);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const existing = this.records.get(token);
|
|
98
|
+
if (existing?.multi) {
|
|
99
|
+
existing.factories.push(...record.factories);
|
|
100
|
+
existing.resolved = false;
|
|
101
|
+
existing.value = void 0;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.records.set(token, record);
|
|
105
|
+
}
|
|
106
|
+
resolveParentMulti(record) {
|
|
107
|
+
if (!this.parent) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
const parentRecord = this.parent.records.get(record.token);
|
|
111
|
+
if (parentRecord?.multi) {
|
|
112
|
+
return this.parent.resolve(parentRecord);
|
|
113
|
+
}
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
flatten(providers) {
|
|
117
|
+
return providers.flatMap((p) => Array.isArray(p) ? this.flatten(p) : [p]);
|
|
118
|
+
}
|
|
119
|
+
tokenName(token) {
|
|
120
|
+
if (typeof token === "function") {
|
|
121
|
+
return token.name;
|
|
122
|
+
}
|
|
123
|
+
if (token instanceof InjectionToken) {
|
|
124
|
+
return token.description;
|
|
125
|
+
}
|
|
126
|
+
return token.name;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// src/lib/create-request-middleware.ts
|
|
131
|
+
var INJECTOR_KEY = "injector";
|
|
132
|
+
function createRequestMiddleware(options) {
|
|
133
|
+
return async (c, next) => {
|
|
134
|
+
const providers = options.getProviders(c.env);
|
|
135
|
+
const injector = Injector.create({
|
|
136
|
+
providers,
|
|
137
|
+
parent: options.parentInjector
|
|
138
|
+
});
|
|
139
|
+
c.set(INJECTOR_KEY, injector);
|
|
140
|
+
await injector.runInContext(next);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/lib/create-static-injector.ts
|
|
145
|
+
function createStaticInjector(options) {
|
|
146
|
+
return Injector.create({
|
|
147
|
+
providers: options.providers,
|
|
148
|
+
parent: options.parentInjector
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/lib/run-with-stores.ts
|
|
153
|
+
function runWithStores(stores, fn) {
|
|
154
|
+
const [first, ...rest] = stores;
|
|
155
|
+
if (!first) {
|
|
156
|
+
return fn();
|
|
157
|
+
}
|
|
158
|
+
const [storage, value] = first;
|
|
159
|
+
return storage.run(value, () => runWithStores(rest, fn));
|
|
160
|
+
}
|
|
161
|
+
export {
|
|
162
|
+
INJECTOR_KEY,
|
|
163
|
+
createRequestMiddleware,
|
|
164
|
+
createStaticInjector,
|
|
165
|
+
runWithStores
|
|
166
|
+
};
|
|
167
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../di/src/lib/context.ts", "../../di/src/lib/injection-token.ts", "../../di/src/lib/injector.ts", "../src/lib/create-request-middleware.ts", "../src/lib/create-static-injector.ts", "../src/lib/run-with-stores.ts"],
|
|
4
|
+
"sourcesContent": ["import type { Injector } from './injector';\n\nlet currentInjector: Injector | null = null;\n\nexport function getCurrentInjector(): Injector | null {\n return currentInjector;\n}\n\nexport function setCurrentInjector(injector: Injector | null): Injector | null {\n const previous = currentInjector;\n currentInjector = injector;\n return previous;\n}\n", "export class InjectionToken<T> {\n declare readonly _brand: T;\n constructor(public readonly description: string) {}\n}\n", "import { setCurrentInjector } from './context';\nimport { InjectionToken } from './injection-token';\nimport type { InjectOptions, Provider, SingleProvider } from './provider';\nimport type { Token } from './token';\n\ninterface InjectorRecord<T = unknown> {\n readonly token: Token<T>;\n factories: (() => T)[];\n value?: T;\n resolved: boolean;\n multi: boolean;\n}\n\nexport class Injector {\n private readonly records = new Map<Token<unknown>, InjectorRecord>();\n\n private constructor(\n providers: Provider[],\n private readonly parent?: Injector,\n ) {\n this.register({ provide: Injector as Token<Injector>, useValue: this });\n for (const provider of this.flatten(providers)) {\n this.register(provider);\n }\n }\n\n public static create(options: { providers: Provider[]; parent?: Injector }): Injector {\n return new Injector(options.providers, options.parent);\n }\n\n public get<T>(token: Token<T>): T;\n public get<T>(token: Token<T>, options: InjectOptions): T | null;\n public get<T>(token: Token<T>, options?: InjectOptions): T | null {\n const record = this.records.get(token) as InjectorRecord<T> | undefined;\n\n if (record) {\n return this.resolve(record);\n }\n\n if (this.parent) {\n return options ? this.parent.get(token, options) : this.parent.get(token);\n }\n\n if (options?.optional) {\n return null;\n }\n\n throw new Error(`No provider for ${this.tokenName(token)}`);\n }\n\n public runInContext<T>(fn: () => T): T {\n const previous = setCurrentInjector(this);\n try {\n return fn();\n } finally {\n setCurrentInjector(previous);\n }\n }\n\n private resolve<T>(record: InjectorRecord<T>): T {\n if (!record.resolved) {\n record.value = this.runInContext(() => this.invokeFactories(record));\n record.resolved = true;\n }\n return record.value as T;\n }\n\n private invokeFactories<T>(record: InjectorRecord<T>): T {\n if (!record.multi) {\n const factory = record.factories[0];\n if (!factory) {\n throw new Error('Record has no factory');\n }\n return factory();\n }\n const parentValues = this.resolveParentMulti(record);\n return [...parentValues, ...record.factories.map(f => f())] as T;\n }\n\n private register(provider: SingleProvider): void {\n if (typeof provider === 'function') {\n this.addRecord(provider, {\n token: provider,\n factories: [() => new provider()],\n resolved: false,\n multi: false,\n });\n return;\n }\n\n const token = provider.provide;\n const multi = provider.multi === true;\n const factory = this.createFactory(provider);\n\n this.addRecord(token, { token, factories: [factory], resolved: false, multi });\n }\n\n private createFactory(\n provider: Exclude<SingleProvider, new (...args: unknown[]) => unknown>,\n ): () => unknown {\n if ('useValue' in provider) {\n return () => provider.useValue;\n }\n if ('useClass' in provider) {\n return () => new provider.useClass();\n }\n return provider.useFactory;\n }\n\n private addRecord(token: Token<unknown>, record: InjectorRecord): void {\n if (!record.multi) {\n this.records.set(token, record);\n return;\n }\n\n const existing = this.records.get(token);\n\n if (existing?.multi) {\n existing.factories.push(...record.factories);\n existing.resolved = false;\n existing.value = undefined;\n return;\n }\n\n this.records.set(token, record);\n }\n\n private resolveParentMulti(record: InjectorRecord): unknown[] {\n if (!this.parent) {\n return [];\n }\n\n const parentRecord = this.parent.records.get(record.token);\n\n if (parentRecord?.multi) {\n return this.parent.resolve(parentRecord) as unknown[];\n }\n\n return [];\n }\n\n private flatten(providers: Provider[]): SingleProvider[] {\n return providers.flatMap(p => (Array.isArray(p) ? this.flatten(p) : [p]));\n }\n\n private tokenName(token: Token<unknown>): string {\n if (typeof token === 'function') {\n return token.name;\n }\n if (token instanceof InjectionToken) {\n return token.description;\n }\n return token.name;\n }\n}\n", "import type { Provider } from '@cibule/di';\nimport { Injector } from '@cibule/di';\n\nexport interface RequestMiddlewareOptions<Env = unknown> {\n readonly parentInjector?: Injector;\n readonly getProviders: (env: Env) => Provider[];\n}\n\nexport interface MiddlewareContext<Env = unknown> {\n readonly env: Env;\n set(key: string, value: unknown): void;\n}\n\nexport const INJECTOR_KEY = 'injector' as const;\n\nexport type NextFunction = () => Promise<void>;\n\nexport type MiddlewareHandler<Env = unknown> = (\n c: MiddlewareContext<Env>,\n next: NextFunction,\n) => Promise<void>;\n\nexport function createRequestMiddleware<Env>(\n options: RequestMiddlewareOptions<Env>,\n): MiddlewareHandler<Env> {\n return async (c: MiddlewareContext<Env>, next: NextFunction): Promise<void> => {\n const providers = options.getProviders(c.env);\n const injector = Injector.create({\n providers,\n parent: options.parentInjector,\n });\n\n c.set(INJECTOR_KEY, injector);\n\n await injector.runInContext(next);\n };\n}\n", "import type { Provider } from '@cibule/di';\nimport { Injector } from '@cibule/di';\n\nexport interface StaticInjectorOptions {\n readonly providers: Provider[];\n readonly parentInjector?: Injector;\n}\n\nexport function createStaticInjector(options: StaticInjectorOptions): Injector {\n return Injector.create({\n providers: options.providers,\n parent: options.parentInjector,\n });\n}\n", "import type { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type StoreEntry<T = unknown> = readonly [AsyncLocalStorage<T>, T];\n\nexport function runWithStores<R>(stores: ReadonlyArray<StoreEntry>, fn: () => R): R {\n const [first, ...rest] = stores;\n\n if (!first) {\n return fn();\n }\n\n const [storage, value] = first;\n\n return storage.run(value, () => runWithStores(rest, fn));\n}\n"],
|
|
5
|
+
"mappings": ";AAEA,IAAI,kBAAmC;AAMhC,SAAS,mBAAmB,UAA4C;AAC7E,QAAM,WAAW;AACjB,oBAAkB;AAClB,SAAO;AACT;;;ACZO,IAAM,iBAAN,MAAwB;AAAA,EAE7B,YAA4B,aAAqB;AAArB;AAAA,EAAsB;AACpD;;;ACUO,IAAM,WAAN,MAAM,UAAS;AAAA,EAGZ,YACN,WACiB,QACjB;AADiB;AAEjB,SAAK,SAAS,EAAE,SAAS,WAA6B,UAAU,KAAK,CAAC;AACtE,eAAW,YAAY,KAAK,QAAQ,SAAS,GAAG;AAC9C,WAAK,SAAS,QAAQ;AAAA,IACxB;AAAA,EACF;AAAA,EAViB,UAAU,oBAAI,IAAoC;AAAA,EAYnE,OAAc,OAAO,SAAiE;AACpF,WAAO,IAAI,UAAS,QAAQ,WAAW,QAAQ,MAAM;AAAA,EACvD;AAAA,EAIO,IAAO,OAAiB,SAAmC;AAChE,UAAM,SAAS,KAAK,QAAQ,IAAI,KAAK;AAErC,QAAI,QAAQ;AACV,aAAO,KAAK,QAAQ,MAAM;AAAA,IAC5B;AAEA,QAAI,KAAK,QAAQ;AACf,aAAO,UAAU,KAAK,OAAO,IAAI,OAAO,OAAO,IAAI,KAAK,OAAO,IAAI,KAAK;AAAA,IAC1E;AAEA,QAAI,SAAS,UAAU;AACrB,aAAO;AAAA,IACT;AAEA,UAAM,IAAI,MAAM,mBAAmB,KAAK,UAAU,KAAK,CAAC,EAAE;AAAA,EAC5D;AAAA,EAEO,aAAgB,IAAgB;AACrC,UAAM,WAAW,mBAAmB,IAAI;AACxC,QAAI;AACF,aAAO,GAAG;AAAA,IACZ,UAAE;AACA,yBAAmB,QAAQ;AAAA,IAC7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AAClF,QAAM,CAAC,OAAO,GAAG,IAAI,IAAI;AAEzB,MAAI,CAAC,OAAO;AACV,WAAO,GAAG;AAAA,EACZ;AAEA,QAAM,CAAC,SAAS,KAAK,IAAI;AAEzB,SAAO,QAAQ,IAAI,OAAO,MAAM,cAAc,MAAM,EAAE,CAAC;AACzD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Provider } from '@cibule/di';
|
|
2
|
+
import { Injector } from '@cibule/di';
|
|
3
|
+
export interface RequestMiddlewareOptions<Env = unknown> {
|
|
4
|
+
readonly parentInjector?: Injector;
|
|
5
|
+
readonly getProviders: (env: Env) => Provider[];
|
|
6
|
+
}
|
|
7
|
+
export interface MiddlewareContext<Env = unknown> {
|
|
8
|
+
readonly env: Env;
|
|
9
|
+
set(key: string, value: unknown): void;
|
|
10
|
+
}
|
|
11
|
+
export declare const INJECTOR_KEY: "injector";
|
|
12
|
+
export type NextFunction = () => Promise<void>;
|
|
13
|
+
export type MiddlewareHandler<Env = unknown> = (c: MiddlewareContext<Env>, next: NextFunction) => Promise<void>;
|
|
14
|
+
export declare function createRequestMiddleware<Env>(options: RequestMiddlewareOptions<Env>): MiddlewareHandler<Env>;
|
|
15
|
+
//# sourceMappingURL=create-request-middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-request-middleware.d.ts","sourceRoot":"","sources":["../../../../../../src/lib/create-request-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,WAAW,wBAAwB,CAAC,GAAG,GAAG,OAAO;IACrD,QAAQ,CAAC,cAAc,CAAC,EAAE,QAAQ,CAAC;IACnC,QAAQ,CAAC,YAAY,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,QAAQ,EAAE,CAAC;CACjD;AAED,MAAM,WAAW,iBAAiB,CAAC,GAAG,GAAG,OAAO;IAC9C,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;CACxC;AAED,eAAO,MAAM,YAAY,EAAG,UAAmB,CAAC;AAEhD,MAAM,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;AAE/C,MAAM,MAAM,iBAAiB,CAAC,GAAG,GAAG,OAAO,IAAI,CAC7C,CAAC,EAAE,iBAAiB,CAAC,GAAG,CAAC,EACzB,IAAI,EAAE,YAAY,KACf,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,wBAAgB,uBAAuB,CAAC,GAAG,EACzC,OAAO,EAAE,wBAAwB,CAAC,GAAG,CAAC,GACrC,iBAAiB,CAAC,GAAG,CAAC,CAYxB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Provider } from '@cibule/di';
|
|
2
|
+
import { Injector } from '@cibule/di';
|
|
3
|
+
export interface StaticInjectorOptions {
|
|
4
|
+
readonly providers: Provider[];
|
|
5
|
+
readonly parentInjector?: Injector;
|
|
6
|
+
}
|
|
7
|
+
export declare function createStaticInjector(options: StaticInjectorOptions): Injector;
|
|
8
|
+
//# sourceMappingURL=create-static-injector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-static-injector.d.ts","sourceRoot":"","sources":["../../../../../../src/lib/create-static-injector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,SAAS,EAAE,QAAQ,EAAE,CAAC;IAC/B,QAAQ,CAAC,cAAc,CAAC,EAAE,QAAQ,CAAC;CACpC;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,QAAQ,CAK7E"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
export type StoreEntry<T = unknown> = readonly [AsyncLocalStorage<T>, T];
|
|
3
|
+
export declare function runWithStores<R>(stores: ReadonlyArray<StoreEntry>, fn: () => R): R;
|
|
4
|
+
//# sourceMappingURL=run-with-stores.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-with-stores.d.ts","sourceRoot":"","sources":["../../../../../../src/lib/run-with-stores.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE1D,MAAM,MAAM,UAAU,CAAC,CAAC,GAAG,OAAO,IAAI,SAAS,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAEzE,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,UAAU,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAUlF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cibule/wiring",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://gitlab.com/LadaBr/cibule",
|
|
28
|
+
"directory": "packages/wiring"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@cibule/di": "workspace:*"
|
|
35
|
+
}
|
|
36
|
+
}
|