@voxgig/system 1.11.0 → 1.12.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/dist/lib/context.d.ts +10 -0
- package/dist/lib/context.js +53 -0
- package/dist/lib/context.js.map +1 -0
- package/dist/system.d.ts +2 -1
- package/dist/system.js +3 -1
- package/dist/system.js.map +1 -1
- package/lib/context.ts +87 -0
- package/package.json +1 -1
- package/system.ts +2 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type ContextSpec = {
|
|
2
|
+
env: string;
|
|
3
|
+
stage?: string;
|
|
4
|
+
srvname?: string;
|
|
5
|
+
};
|
|
6
|
+
declare const ALL_SRV = "all";
|
|
7
|
+
declare function resolveStage(model: any, spec: ContextSpec): string;
|
|
8
|
+
declare function context(seneca: any, model: any, pkg: any, spec: ContextSpec): any;
|
|
9
|
+
export type { ContextSpec, };
|
|
10
|
+
export { context, resolveStage, ALL_SRV, };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright © 2026 Voxgig Ltd, MIT License. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ALL_SRV = void 0;
|
|
5
|
+
exports.context = context;
|
|
6
|
+
exports.resolveStage = resolveStage;
|
|
7
|
+
// The service name for a process running every service in-tree. AWS Lambda
|
|
8
|
+
// is the one deployment that overrides it: one function per service.
|
|
9
|
+
const ALL_SRV = 'all';
|
|
10
|
+
exports.ALL_SRV = ALL_SRV;
|
|
11
|
+
// stage resolution, most specific first:
|
|
12
|
+
// 1. an explicit spec.stage - the caller knows better (Lambda's STAGE)
|
|
13
|
+
// 2. process.env.STAGE - deploy-time override
|
|
14
|
+
// 3. the model's env entry stage - main.env.<env>.stage, e.g. aws.stage
|
|
15
|
+
// 4. the env name - a sensible last resort ('local', 'docker')
|
|
16
|
+
//
|
|
17
|
+
// (3) is the one that was being thrown away: a model could say
|
|
18
|
+
// `aws: { stage: 'dev' }` and every entry would still report 'aws'.
|
|
19
|
+
function resolveStage(model, spec) {
|
|
20
|
+
if (null != spec.stage && '' !== spec.stage) {
|
|
21
|
+
return spec.stage;
|
|
22
|
+
}
|
|
23
|
+
const fromEnvVar = 'undefined' === typeof process ? undefined :
|
|
24
|
+
process.env && process.env.STAGE;
|
|
25
|
+
if (null != fromEnvVar && '' !== fromEnvVar) {
|
|
26
|
+
return fromEnvVar;
|
|
27
|
+
}
|
|
28
|
+
const envdef = model && model.main && model.main.env &&
|
|
29
|
+
model.main.env[spec.env];
|
|
30
|
+
if (envdef && null != envdef.stage && '' !== envdef.stage) {
|
|
31
|
+
return envdef.stage;
|
|
32
|
+
}
|
|
33
|
+
return spec.env;
|
|
34
|
+
}
|
|
35
|
+
// context(seneca, model, pkg, { env }): set the runtime context an entry
|
|
36
|
+
// needs. Returns seneca so it can be chained.
|
|
37
|
+
//
|
|
38
|
+
// `env` is required and NOT derived: it is the one thing the model cannot
|
|
39
|
+
// tell us, because it names which entry is running. It need not be a model
|
|
40
|
+
// env name - 'lambda' and 'test' are entries without a matching main.env
|
|
41
|
+
// key - so it is deliberately not validated against the model.
|
|
42
|
+
function context(seneca, model, pkg, spec) {
|
|
43
|
+
if (null == spec || 'string' !== typeof spec.env || '' === spec.env) {
|
|
44
|
+
throw new Error('voxgig-system: context requires a non-empty `env` string');
|
|
45
|
+
}
|
|
46
|
+
seneca.context.model = model;
|
|
47
|
+
seneca.context.pkg = pkg;
|
|
48
|
+
seneca.context.env = spec.env;
|
|
49
|
+
seneca.context.stage = resolveStage(model, spec);
|
|
50
|
+
seneca.context.srvname = spec.srvname || ALL_SRV;
|
|
51
|
+
return seneca;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../lib/context.ts"],"names":[],"mappings":";AAAA,+CAA+C;;;QAmF7C,OAAO;QACP,YAAY;A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AC,GAAG,IAAI,EAAE,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC;QACpE,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAA;IAC7E,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;IAC5B,MAAM,CAAC,OAAO,CAAC,GAAG,GAAG,GAAG,CAAA;IACxB,MAAM,CAAC,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAA;IAC7B,MAAM,CAAC,OAAO,CAAC,KAAK,GAAG,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IAChD,MAAM,CAAC,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,OAAO,CAAA;IAEhD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/dist/system.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Utility } from './lib/utility';
|
|
|
3
3
|
import { MakeSrv } from './srv/make';
|
|
4
4
|
import { addEntity, addSrv, addMsg, addFields, addEnv, resolveModelFiles } from './lib/add';
|
|
5
5
|
import { listTemplates, ejectFragment, ejectCode, diffTemplates } from './lib/template';
|
|
6
|
+
import { context } from './lib/context';
|
|
6
7
|
declare function messages(seneca: any, options: any, reload: any): void;
|
|
7
8
|
declare function gubuify(params: any, Gubu: any): any;
|
|
8
9
|
declare function prepare(seneca: any, require: any): void;
|
|
@@ -34,4 +35,4 @@ declare const Template: {
|
|
|
34
35
|
ejectCode: typeof ejectCode;
|
|
35
36
|
diff: typeof diffTemplates;
|
|
36
37
|
};
|
|
37
|
-
export { Add, Template, gubuify, System, MakeSrv, Utility, Local, Live, };
|
|
38
|
+
export { Add, Template, context, gubuify, System, MakeSrv, Utility, Local, Live, };
|
package/dist/system.js
CHANGED
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.Utility = exports.MakeSrv = exports.System = exports.Template = exports.Add = void 0;
|
|
7
|
+
exports.Utility = exports.MakeSrv = exports.System = exports.context = exports.Template = exports.Add = void 0;
|
|
8
8
|
exports.gubuify = gubuify;
|
|
9
9
|
exports.Local = Local;
|
|
10
10
|
exports.Live = Live;
|
|
@@ -16,6 +16,8 @@ const make_1 = require("./srv/make");
|
|
|
16
16
|
Object.defineProperty(exports, "MakeSrv", { enumerable: true, get: function () { return make_1.MakeSrv; } });
|
|
17
17
|
const add_1 = require("./lib/add");
|
|
18
18
|
const template_1 = require("./lib/template");
|
|
19
|
+
const context_1 = require("./lib/context");
|
|
20
|
+
Object.defineProperty(exports, "context", { enumerable: true, get: function () { return context_1.context; } });
|
|
19
21
|
const { srvmsgs, deep } = utility_1.Utility;
|
|
20
22
|
function messages(seneca, options, reload) {
|
|
21
23
|
let srvname = seneca.fixedargs.plugin$.name.replace(/^srv_/, '');
|
package/dist/system.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"system.js","sourceRoot":"","sources":["../system.ts"],"names":[],"mappings":";AAAA,+CAA+C;;;;;;
|
|
1
|
+
{"version":3,"file":"system.js","sourceRoot":"","sources":["../system.ts"],"names":[],"mappings":";AAAA,+CAA+C;;;;;;QAgP7C,OAAO;QAIP,KAAK;QACL,IAAI;AAlPN,gDAAuB;AACvB,4CAAmB;AAOnB,2CAAuC;wFAA9B,iBAAO;AAEhB,qCAAoC;wFAA3B,cAAO;AAEhB,mCAA2F;AAC3F,6CAAuF;AACvF,2CAAuC;wFAA9B,iBAAO;AAIhB,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,iBAAO,CAAA;AAGjC,SAAS,QAAQ,CAAC,MAAW,EAAE,OAAY,EAAE,MAAW;IACtD,IAAI,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,EAA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AAE,wBAAa;IACnB,KAAK,EAAE,wBAAa;IACpB,SAAS,EAAT,oBAAS;IACT,IAAI,EAAE,wBAAa;CACpB,CAAA;QAIC,QAAQ"}
|
package/lib/context.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/* Copyright © 2026 Voxgig Ltd, MIT License. */
|
|
2
|
+
|
|
3
|
+
// seneca.context: the runtime facts every environment entry has to put on
|
|
4
|
+
// the Seneca instance before services load.
|
|
5
|
+
//
|
|
6
|
+
// This used to be five hand-written assignments at the top of every
|
|
7
|
+
// generated entry (local, docker, vm, aws, azure, cloudflare, web, and the
|
|
8
|
+
// test setups). Copy-paste let them drift: azure never set `stage` or
|
|
9
|
+
// `srvname`, cloudflare set none of them, and the web runner reported
|
|
10
|
+
// `env: 'local'` when it was the web environment. Nothing read those
|
|
11
|
+
// fields yet, so the drift was silent - which is exactly why it spread.
|
|
12
|
+
//
|
|
13
|
+
// One call now sets all five, and everything that CAN be derived is.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
type ContextSpec = {
|
|
17
|
+
env: string // which entry this is - the only fact not in the model
|
|
18
|
+
stage?: string // deploy stage; see the resolution order below
|
|
19
|
+
srvname?: string // 'all' for a single process, the service for one-per-srv
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
// The service name for a process running every service in-tree. AWS Lambda
|
|
24
|
+
// is the one deployment that overrides it: one function per service.
|
|
25
|
+
const ALL_SRV = 'all'
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// stage resolution, most specific first:
|
|
29
|
+
// 1. an explicit spec.stage - the caller knows better (Lambda's STAGE)
|
|
30
|
+
// 2. process.env.STAGE - deploy-time override
|
|
31
|
+
// 3. the model's env entry stage - main.env.<env>.stage, e.g. aws.stage
|
|
32
|
+
// 4. the env name - a sensible last resort ('local', 'docker')
|
|
33
|
+
//
|
|
34
|
+
// (3) is the one that was being thrown away: a model could say
|
|
35
|
+
// `aws: { stage: 'dev' }` and every entry would still report 'aws'.
|
|
36
|
+
function resolveStage(model: any, spec: ContextSpec): string {
|
|
37
|
+
if (null != spec.stage && '' !== spec.stage) {
|
|
38
|
+
return spec.stage
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const fromEnvVar = 'undefined' === typeof process ? undefined :
|
|
42
|
+
process.env && process.env.STAGE
|
|
43
|
+
if (null != fromEnvVar && '' !== fromEnvVar) {
|
|
44
|
+
return fromEnvVar
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const envdef = model && model.main && model.main.env &&
|
|
48
|
+
model.main.env[spec.env]
|
|
49
|
+
if (envdef && null != envdef.stage && '' !== envdef.stage) {
|
|
50
|
+
return envdef.stage
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return spec.env
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
// context(seneca, model, pkg, { env }): set the runtime context an entry
|
|
58
|
+
// needs. Returns seneca so it can be chained.
|
|
59
|
+
//
|
|
60
|
+
// `env` is required and NOT derived: it is the one thing the model cannot
|
|
61
|
+
// tell us, because it names which entry is running. It need not be a model
|
|
62
|
+
// env name - 'lambda' and 'test' are entries without a matching main.env
|
|
63
|
+
// key - so it is deliberately not validated against the model.
|
|
64
|
+
function context(seneca: any, model: any, pkg: any, spec: ContextSpec): any {
|
|
65
|
+
if (null == spec || 'string' !== typeof spec.env || '' === spec.env) {
|
|
66
|
+
throw new Error('voxgig-system: context requires a non-empty `env` string')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
seneca.context.model = model
|
|
70
|
+
seneca.context.pkg = pkg
|
|
71
|
+
seneca.context.env = spec.env
|
|
72
|
+
seneca.context.stage = resolveStage(model, spec)
|
|
73
|
+
seneca.context.srvname = spec.srvname || ALL_SRV
|
|
74
|
+
|
|
75
|
+
return seneca
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
export type {
|
|
80
|
+
ContextSpec,
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export {
|
|
84
|
+
context,
|
|
85
|
+
resolveStage,
|
|
86
|
+
ALL_SRV,
|
|
87
|
+
}
|
package/package.json
CHANGED
package/system.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { MakeSrv } from './srv/make'
|
|
|
15
15
|
|
|
16
16
|
import { addEntity, addSrv, addMsg, addFields, addEnv, resolveModelFiles } from './lib/add'
|
|
17
17
|
import { listTemplates, ejectFragment, ejectCode, diffTemplates } from './lib/template'
|
|
18
|
+
import { context } from './lib/context'
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
|
|
@@ -236,6 +237,7 @@ const Template = {
|
|
|
236
237
|
export {
|
|
237
238
|
Add,
|
|
238
239
|
Template,
|
|
240
|
+
context,
|
|
239
241
|
gubuify,
|
|
240
242
|
System,
|
|
241
243
|
MakeSrv,
|