@cqrs-ddd/pipeline-job-context 0.5.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/COMMERCIAL_LICENSE.txt +34 -0
- package/LICENSE +661 -0
- package/README.md +42 -0
- package/dist/decorators/as-system.decorator.d.ts +36 -0
- package/dist/decorators/as-system.decorator.d.ts.map +1 -0
- package/dist/decorators/as-system.decorator.js +53 -0
- package/dist/decorators/as-system.decorator.js.map +1 -0
- package/dist/decorators/in-job-context.decorator.d.ts +35 -0
- package/dist/decorators/in-job-context.decorator.d.ts.map +1 -0
- package/dist/decorators/in-job-context.decorator.js +47 -0
- package/dist/decorators/in-job-context.decorator.js.map +1 -0
- package/dist/errors/invalid-job-context.error.d.ts +15 -0
- package/dist/errors/invalid-job-context.error.d.ts.map +1 -0
- package/dist/errors/invalid-job-context.error.js +20 -0
- package/dist/errors/invalid-job-context.error.js.map +1 -0
- package/dist/errors/missing-job-context.error.d.ts +14 -0
- package/dist/errors/missing-job-context.error.d.ts.map +1 -0
- package/dist/errors/missing-job-context.error.js +19 -0
- package/dist/errors/missing-job-context.error.js.map +1 -0
- package/dist/helpers/method-decorator.d.ts +13 -0
- package/dist/helpers/method-decorator.d.ts.map +1 -0
- package/dist/helpers/method-decorator.js +24 -0
- package/dist/helpers/method-decorator.js.map +1 -0
- package/dist/helpers/parse-job-context.d.ts +20 -0
- package/dist/helpers/parse-job-context.d.ts.map +1 -0
- package/dist/helpers/parse-job-context.js +68 -0
- package/dist/helpers/parse-job-context.js.map +1 -0
- package/dist/helpers/principal-reference.d.ts +12 -0
- package/dist/helpers/principal-reference.d.ts.map +1 -0
- package/dist/helpers/principal-reference.js +15 -0
- package/dist/helpers/principal-reference.js.map +1 -0
- package/dist/helpers/registration.d.ts +39 -0
- package/dist/helpers/registration.d.ts.map +1 -0
- package/dist/helpers/registration.js +44 -0
- package/dist/helpers/registration.js.map +1 -0
- package/dist/helpers/with-job-context.d.ts +21 -0
- package/dist/helpers/with-job-context.d.ts.map +1 -0
- package/dist/helpers/with-job-context.js +46 -0
- package/dist/helpers/with-job-context.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/context-source.interface.d.ts +23 -0
- package/dist/interfaces/context-source.interface.d.ts.map +1 -0
- package/dist/interfaces/context-source.interface.js +3 -0
- package/dist/interfaces/context-source.interface.js.map +1 -0
- package/dist/interfaces/job-context.interface.d.ts +12 -0
- package/dist/interfaces/job-context.interface.d.ts.map +1 -0
- package/dist/interfaces/job-context.interface.js +3 -0
- package/dist/interfaces/job-context.interface.js.map +1 -0
- package/dist/interfaces/job-principal.interface.d.ts +28 -0
- package/dist/interfaces/job-principal.interface.d.ts.map +1 -0
- package/dist/interfaces/job-principal.interface.js +3 -0
- package/dist/interfaces/job-principal.interface.js.map +1 -0
- package/dist/interfaces/principal-reference.interface.d.ts +12 -0
- package/dist/interfaces/principal-reference.interface.d.ts.map +1 -0
- package/dist/interfaces/principal-reference.interface.js +3 -0
- package/dist/interfaces/principal-reference.interface.js.map +1 -0
- package/dist/register-job-context.d.ts +34 -0
- package/dist/register-job-context.d.ts.map +1 -0
- package/dist/register-job-context.js +32 -0
- package/dist/register-job-context.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* Copyright (C) 2026-present Aristotelis — see repository license. */
|
|
2
|
+
import { MissingJobContextError } from '../errors/missing-job-context.error.js';
|
|
3
|
+
import { toReference } from './principal-reference.js';
|
|
4
|
+
import { activeRegistration } from './registration.js';
|
|
5
|
+
/**
|
|
6
|
+
* Stamps the current execution context onto a job payload: the tenant and
|
|
7
|
+
* correlation id of the registered sources (a new one from the correlation
|
|
8
|
+
* source's `create()` when none is active), and the principal the registered `IJobPrincipal`
|
|
9
|
+
* captures, reduced to its identity. The processor restores it with
|
|
10
|
+
* `@InJobContext`.
|
|
11
|
+
*
|
|
12
|
+
* @param data - The payload. Must be a plain object; a shallow copy is returned.
|
|
13
|
+
* @returns A copy of `data` with `jobContext` added.
|
|
14
|
+
* @throws {TypeError} When `data` is not a plain object.
|
|
15
|
+
* @throws {MissingJobContextError} Without a `registerJobContext` registration, a
|
|
16
|
+
* current tenant, or a current principal.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* await queue.add('send', withJobContext({ userId, email }));
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function withJobContext(data) {
|
|
24
|
+
const prototype = data !== null && typeof data === 'object'
|
|
25
|
+
? Object.getPrototypeOf(data)
|
|
26
|
+
: undefined;
|
|
27
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
28
|
+
throw new TypeError('withJobContext(data) requires a plain object payload. Wrap arrays and class instances in a plain object first.');
|
|
29
|
+
}
|
|
30
|
+
const { principal, sources } = activeRegistration();
|
|
31
|
+
const tenantId = sources.tenantId.current();
|
|
32
|
+
if (tenantId === undefined)
|
|
33
|
+
throw new MissingJobContextError('tenant');
|
|
34
|
+
const captured = principal.capture();
|
|
35
|
+
if (captured === undefined)
|
|
36
|
+
throw new MissingJobContextError('principal');
|
|
37
|
+
return {
|
|
38
|
+
...data,
|
|
39
|
+
jobContext: {
|
|
40
|
+
tenantId,
|
|
41
|
+
correlationId: sources.correlationId.current() ?? sources.correlationId.create(),
|
|
42
|
+
principal: toReference(captured),
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=with-job-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-job-context.js","sourceRoot":"","sources":["../../src/helpers/with-job-context.ts"],"names":[],"mappings":"AAAA,sEAAsE;AAEtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAC;AAEhF,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,cAAc,CAC5B,IAAO;IAEP,MAAM,SAAS,GACb,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;QACvC,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC;QAC7B,CAAC,CAAC,SAAS,CAAC;IAChB,IAAI,SAAS,KAAK,MAAM,CAAC,SAAS,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACzD,MAAM,IAAI,SAAS,CACjB,gHAAgH,CACjH,CAAC;IACJ,CAAC;IACD,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,kBAAkB,EAAE,CAAC;IACpD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;IAC5C,IAAI,QAAQ,KAAK,SAAS;QAAE,MAAM,IAAI,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IACvE,MAAM,QAAQ,GAAG,SAAS,CAAC,OAAO,EAAE,CAAC;IACrC,IAAI,QAAQ,KAAK,SAAS;QAAE,MAAM,IAAI,sBAAsB,CAAC,WAAW,CAAC,CAAC;IAC1E,OAAO;QACL,GAAG,IAAI;QACP,UAAU,EAAE;YACV,QAAQ;YACR,aAAa,EACX,OAAO,CAAC,aAAa,CAAC,OAAO,EAAE,IAAI,OAAO,CAAC,aAAa,CAAC,MAAM,EAAE;YACnE,SAAS,EAAE,WAAW,CAAC,QAAQ,CAAC;SACjC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { AsSystem, type AsSystemOptions, } from './decorators/as-system.decorator.js';
|
|
2
|
+
export { InJobContext, type InJobContextOptions, } from './decorators/in-job-context.decorator.js';
|
|
3
|
+
export { InvalidJobContextError } from './errors/invalid-job-context.error.js';
|
|
4
|
+
export { MissingJobContextError } from './errors/missing-job-context.error.js';
|
|
5
|
+
export { withJobContext } from './helpers/with-job-context.js';
|
|
6
|
+
export type { ContextSource, CorrelationSource, JobContextSources, } from './interfaces/context-source.interface.js';
|
|
7
|
+
export type { JobContext, WithJobContext, } from './interfaces/job-context.interface.js';
|
|
8
|
+
export type { IJobPrincipal } from './interfaces/job-principal.interface.js';
|
|
9
|
+
export type { PrincipalReference } from './interfaces/principal-reference.interface.js';
|
|
10
|
+
export { type JobContextOptions, registerJobContext, } from './register-job-context.js';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,QAAQ,EACR,KAAK,eAAe,GACrB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EACL,YAAY,EACZ,KAAK,mBAAmB,GACzB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAC;AAC/E,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAC/D,YAAY,EACV,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,0CAA0C,CAAC;AAClD,YAAY,EACV,UAAU,EACV,cAAc,GACf,MAAM,uCAAuC,CAAC;AAC/C,YAAY,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAC7E,YAAY,EAAE,kBAAkB,EAAE,MAAM,+CAA+C,CAAC;AACxF,OAAO,EACL,KAAK,iBAAiB,EACtB,kBAAkB,GACnB,MAAM,2BAA2B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* Copyright (C) 2026-present Aristotelis — see repository license. */
|
|
2
|
+
export { AsSystem, } from './decorators/as-system.decorator.js';
|
|
3
|
+
export { InJobContext, } from './decorators/in-job-context.decorator.js';
|
|
4
|
+
export { InvalidJobContextError } from './errors/invalid-job-context.error.js';
|
|
5
|
+
export { MissingJobContextError } from './errors/missing-job-context.error.js';
|
|
6
|
+
export { withJobContext } from './helpers/with-job-context.js';
|
|
7
|
+
export { registerJobContext, } from './register-job-context.js';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sEAAsE;AAEtE,OAAO,EACL,QAAQ,GAET,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EACL,YAAY,GAEb,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAC;AAC/E,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAY/D,OAAO,EAEL,kBAAkB,GACnB,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value kept for the current execution by the package that owns it, such as
|
|
3
|
+
* `tenantSource` of `@cqrs-ddd/pipeline-tenant`.
|
|
4
|
+
*/
|
|
5
|
+
export interface ContextSource {
|
|
6
|
+
/** The value of the current execution, or `undefined` for none. */
|
|
7
|
+
current(): string | undefined;
|
|
8
|
+
/** Runs `fn` with `value` as the current value for everything it calls. */
|
|
9
|
+
run<T>(value: string | undefined, fn: () => T): T;
|
|
10
|
+
}
|
|
11
|
+
/** A correlation id source that also makes new ids and judges ids received from outside. */
|
|
12
|
+
export interface CorrelationSource extends ContextSource {
|
|
13
|
+
/** A new correlation id. */
|
|
14
|
+
create(): string;
|
|
15
|
+
/** Whether `id`, read from a job payload, is a well-formed correlation id. */
|
|
16
|
+
accepts(id: string): boolean;
|
|
17
|
+
}
|
|
18
|
+
/** The tenant and correlation id a job carries and restores. */
|
|
19
|
+
export interface JobContextSources {
|
|
20
|
+
readonly tenantId: ContextSource;
|
|
21
|
+
readonly correlationId: CorrelationSource;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=context-source.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-source.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/context-source.interface.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,mEAAmE;IACnE,OAAO,IAAI,MAAM,GAAG,SAAS,CAAC;IAC9B,2EAA2E;IAC3E,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;CACnD;AAED,4FAA4F;AAC5F,MAAM,WAAW,iBAAkB,SAAQ,aAAa;IACtD,4BAA4B;IAC5B,MAAM,IAAI,MAAM,CAAC;IACjB,8EAA8E;IAC9E,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC;CAC9B;AAED,gEAAgE;AAChE,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAC;IACjC,QAAQ,CAAC,aAAa,EAAE,iBAAiB,CAAC;CAC3C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-source.interface.js","sourceRoot":"","sources":["../../src/interfaces/context-source.interface.ts"],"names":[],"mappings":"AAAA,sEAAsE"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PrincipalReference } from './principal-reference.interface.js';
|
|
2
|
+
/** Execution context a job payload carries from the request that enqueued it. */
|
|
3
|
+
export interface JobContext {
|
|
4
|
+
readonly tenantId: string;
|
|
5
|
+
readonly correlationId: string;
|
|
6
|
+
readonly principal: PrincipalReference;
|
|
7
|
+
}
|
|
8
|
+
/** A job payload stamped by `withJobContext`. */
|
|
9
|
+
export type WithJobContext<T extends Record<string, unknown>> = T & {
|
|
10
|
+
jobContext: JobContext;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=job-context.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job-context.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/job-context.interface.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAE7E,iFAAiF;AACjF,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,SAAS,EAAE,kBAAkB,CAAC;CACxC;AAED,iDAAiD;AACjD,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG;IAClE,UAAU,EAAE,UAAU,CAAC;CACxB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job-context.interface.js","sourceRoot":"","sources":["../../src/interfaces/job-context.interface.ts"],"names":[],"mappings":"AAAA,sEAAsE"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PrincipalReference } from './principal-reference.interface.js';
|
|
2
|
+
/**
|
|
3
|
+
* Application port that reads the current principal when a job is enqueued and
|
|
4
|
+
* binds it again when the job runs. The application implements it over its own
|
|
5
|
+
* authentication state; register the implementation with
|
|
6
|
+
* `registerJobContext`.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam TGrant - The application's grant type, as declared in `@AsSystem`.
|
|
9
|
+
*/
|
|
10
|
+
export interface IJobPrincipal<TGrant = unknown> {
|
|
11
|
+
/**
|
|
12
|
+
* The principal of the current execution, or `undefined` when there is none.
|
|
13
|
+
* Only `id`, `type` and `sessionId` of the result are kept.
|
|
14
|
+
*/
|
|
15
|
+
capture(): PrincipalReference | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Re-checks `principal` against current state and runs `work` with it as the
|
|
18
|
+
* current principal. It runs inside the job's tenant and correlation id.
|
|
19
|
+
* Throw to refuse the job, for example for a revoked session or a deleted
|
|
20
|
+
* principal; the job then fails without running.
|
|
21
|
+
*
|
|
22
|
+
* @param grants - Set only for `@AsSystem` work: the complete grants its code
|
|
23
|
+
* declares. For a job payload it is `undefined`, and the principal's
|
|
24
|
+
* authorization must be resolved from current state.
|
|
25
|
+
*/
|
|
26
|
+
restore<T>(principal: PrincipalReference, work: () => Promise<T>, grants?: readonly TGrant[]): Promise<T>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=job-principal.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job-principal.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/job-principal.interface.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAE7E;;;;;;;GAOG;AACH,MAAM,WAAW,aAAa,CAAC,MAAM,GAAG,OAAO;IAC7C;;;OAGG;IACH,OAAO,IAAI,kBAAkB,GAAG,SAAS,CAAC;IAE1C;;;;;;;;;OASG;IACH,OAAO,CAAC,CAAC,EACP,SAAS,EAAE,kBAAkB,EAC7B,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACtB,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,GACzB,OAAO,CAAC,CAAC,CAAC,CAAC;CACf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job-principal.interface.js","sourceRoot":"","sources":["../../src/interfaces/job-principal.interface.ts"],"names":[],"mappings":"AAAA,sEAAsE"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identity of the principal a job acts for. It carries no grants: what the
|
|
3
|
+
* principal may do is resolved again when the job runs.
|
|
4
|
+
*/
|
|
5
|
+
export interface PrincipalReference {
|
|
6
|
+
readonly id: string;
|
|
7
|
+
/** Application-defined classification, such as `'user'` or `'service'`. */
|
|
8
|
+
readonly type: string;
|
|
9
|
+
/** Login session the principal authenticated with; absent when it has none. */
|
|
10
|
+
readonly sessionId?: string;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=principal-reference.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principal-reference.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/principal-reference.interface.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,2EAA2E;IAC3E,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,+EAA+E;IAC/E,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principal-reference.interface.js","sourceRoot":"","sources":["../../src/interfaces/principal-reference.interface.ts"],"names":[],"mappings":"AAAA,sEAAsE"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { JobContextSources } from './interfaces/context-source.interface.js';
|
|
2
|
+
import type { IJobPrincipal } from './interfaces/job-principal.interface.js';
|
|
3
|
+
/** What {@link registerJobContext} makes the job context of the process. */
|
|
4
|
+
export interface JobContextOptions {
|
|
5
|
+
/** Restores and re-checks the principal of a job, and declares system principals. */
|
|
6
|
+
principal: IJobPrincipal;
|
|
7
|
+
/** The tenants a job may run in; a job that names another is refused. */
|
|
8
|
+
tenants: readonly string[];
|
|
9
|
+
/**
|
|
10
|
+
* Where the tenant and the correlation id are stamped from and restored into, such as
|
|
11
|
+
* `tenantSource` of `@cqrs-ddd/pipeline-tenant` and `correlationSource` of
|
|
12
|
+
* `@cqrs-ddd/pipeline-correlation`.
|
|
13
|
+
*/
|
|
14
|
+
sources: JobContextSources;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Makes `options` the job context of this process: `withJobContext` stamps payloads
|
|
18
|
+
* with it, `@InJobContext()` restores it, and `@AsSystem` declares system principals
|
|
19
|
+
* through it. One application registers it once, at startup.
|
|
20
|
+
*
|
|
21
|
+
* @returns A function that removes this registration, for shutdown.
|
|
22
|
+
* @throws TypeError when `tenants` is empty.
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const unregister = registerJobContext({
|
|
26
|
+
* principal: new SessionJobPrincipal(sessions),
|
|
27
|
+
* tenants: ['acme', 'globex'],
|
|
28
|
+
* sources: { tenantId: tenantSource, correlationId: correlationSource },
|
|
29
|
+
* });
|
|
30
|
+
* process.on('SIGTERM', unregister);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function registerJobContext(options: JobContextOptions): () => void;
|
|
34
|
+
//# sourceMappingURL=register-job-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register-job-context.d.ts","sourceRoot":"","sources":["../src/register-job-context.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAClF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAE7E,4EAA4E;AAC5E,MAAM,WAAW,iBAAiB;IAChC,qFAAqF;IACrF,SAAS,EAAE,aAAa,CAAC;IACzB,yEAAyE;IACzE,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3B;;;;OAIG;IACH,OAAO,EAAE,iBAAiB,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,iBAAiB,GAAG,MAAM,IAAI,CAWzE"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* Copyright (C) 2026-present Aristotelis — see repository license. */
|
|
2
|
+
import { register, unregister } from './helpers/registration.js';
|
|
3
|
+
/**
|
|
4
|
+
* Makes `options` the job context of this process: `withJobContext` stamps payloads
|
|
5
|
+
* with it, `@InJobContext()` restores it, and `@AsSystem` declares system principals
|
|
6
|
+
* through it. One application registers it once, at startup.
|
|
7
|
+
*
|
|
8
|
+
* @returns A function that removes this registration, for shutdown.
|
|
9
|
+
* @throws TypeError when `tenants` is empty.
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const unregister = registerJobContext({
|
|
13
|
+
* principal: new SessionJobPrincipal(sessions),
|
|
14
|
+
* tenants: ['acme', 'globex'],
|
|
15
|
+
* sources: { tenantId: tenantSource, correlationId: correlationSource },
|
|
16
|
+
* });
|
|
17
|
+
* process.on('SIGTERM', unregister);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function registerJobContext(options) {
|
|
21
|
+
if (options.tenants.length === 0) {
|
|
22
|
+
throw new TypeError('registerJobContext needs at least one tenant.');
|
|
23
|
+
}
|
|
24
|
+
const registration = {
|
|
25
|
+
principal: options.principal,
|
|
26
|
+
tenants: [...options.tenants],
|
|
27
|
+
sources: options.sources,
|
|
28
|
+
};
|
|
29
|
+
register(registration);
|
|
30
|
+
return () => unregister(registration);
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=register-job-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register-job-context.js","sourceRoot":"","sources":["../src/register-job-context.ts"],"names":[],"mappings":"AAAA,sEAAsE;AAEtE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAkBjE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAA0B;IAC3D,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,SAAS,CAAC,+CAA+C,CAAC,CAAC;IACvE,CAAC;IACD,MAAM,YAAY,GAAG;QACnB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;QAC7B,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC;IACF,QAAQ,CAAC,YAAY,CAAC,CAAC;IACvB,OAAO,GAAG,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;AACxC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cqrs-ddd/pipeline-job-context",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Carries the tenant, correlation id and principal of a request into the queue jobs it enqueues, and gives system-started work an explicit context",
|
|
5
|
+
"author": "Aristotelis",
|
|
6
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=22.12.0"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/aristoteliss/ddd-cqrs.git",
|
|
14
|
+
"directory": "packages/pipeline-job-context"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://aristoteliss.github.io/ddd-cqrs/packages/pipeline-job-context/",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/aristoteliss/ddd-cqrs/issues"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"nestjs",
|
|
22
|
+
"cqrs",
|
|
23
|
+
"pipeline",
|
|
24
|
+
"multi-tenancy",
|
|
25
|
+
"queue",
|
|
26
|
+
"jobs",
|
|
27
|
+
"authorization",
|
|
28
|
+
"correlation-id"
|
|
29
|
+
],
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"main": "dist/index.js",
|
|
38
|
+
"types": "dist/index.d.ts",
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"LICENSE",
|
|
42
|
+
"COMMERCIAL_LICENSE.txt"
|
|
43
|
+
],
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
49
|
+
"vitest": "^5.0.3"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsc -p tsconfig.build.json",
|
|
53
|
+
"build:watch": "tsc -p tsconfig.build.json --watch",
|
|
54
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
55
|
+
"rebuild": "pnpm run clean && pnpm run build",
|
|
56
|
+
"lint": "tsc --noEmit",
|
|
57
|
+
"test": "vitest run",
|
|
58
|
+
"test:watch": "vitest"
|
|
59
|
+
}
|
|
60
|
+
}
|