@jahands/dagger-helpers 0.2.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/README.md +3 -0
- package/dist/constants.d.ts +9 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +23 -0
- package/dist/constants.js.map +1 -0
- package/dist/env.d.ts +26 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +95 -0
- package/dist/env.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jacob Hands
|
|
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/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** various constants for dagger modules in my repos */
|
|
2
|
+
export declare const constants: {
|
|
3
|
+
/** constants for workers-monorepo */
|
|
4
|
+
readonly workersMonorepo: {
|
|
5
|
+
/** default source ignore list */
|
|
6
|
+
readonly ignore: string[];
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,eAAO,MAAM,SAAS;IACrB,qCAAqC;;QAEpC,iCAAiC;;;CAiBzB,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** various constants for dagger modules in my repos */
|
|
2
|
+
export const constants = {
|
|
3
|
+
/** constants for workers-monorepo */
|
|
4
|
+
workersMonorepo: {
|
|
5
|
+
/** default source ignore list */
|
|
6
|
+
ignore: [
|
|
7
|
+
'**/node_modules/',
|
|
8
|
+
'**/.env',
|
|
9
|
+
'**/.secret',
|
|
10
|
+
'**/Earthfile',
|
|
11
|
+
'**/.wrangler',
|
|
12
|
+
'**/.dev.vars',
|
|
13
|
+
'**/.turbo/',
|
|
14
|
+
'**/dist/',
|
|
15
|
+
'**/dist2/',
|
|
16
|
+
'**/.DS_Store',
|
|
17
|
+
'**/.astro/',
|
|
18
|
+
'**/.next/',
|
|
19
|
+
'*.env',
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,MAAM,CAAC,MAAM,SAAS,GAAG;IACxB,qCAAqC;IACrC,eAAe,EAAE;QAChB,iCAAiC;QACjC,MAAM,EAAE;YACP,kBAAkB;YAClB,SAAS;YACT,YAAY;YACZ,cAAc;YACd,cAAc;YACd,cAAc;YACd,YAAY;YACZ,UAAU;YACV,WAAW;YACX,cAAc;YACd,YAAY;YACZ,WAAW;YACX,OAAO;SACY;KACpB;CACQ,CAAA"}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Secret } from '@dagger.io/dagger';
|
|
2
|
+
/**
|
|
3
|
+
* Structure stored in AsyncLocalStorage, containing both the fully merged
|
|
4
|
+
* environment variables and the set of variables explicitly passed to the
|
|
5
|
+
* current decorated function call.
|
|
6
|
+
*/
|
|
7
|
+
export interface EnvContext {
|
|
8
|
+
currentParams: Set<string>;
|
|
9
|
+
mergedEnv: Record<string, Secret | string | undefined>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Store for environment variables accessible via AsyncLocalStorage
|
|
13
|
+
*/
|
|
14
|
+
export declare const envStorage: any;
|
|
15
|
+
/**
|
|
16
|
+
* Helper to extract parameter names from a function's source code.
|
|
17
|
+
* Note: This approach using Function.toString() can be fragile and might
|
|
18
|
+
* break with code minification or complex function definitions.
|
|
19
|
+
*/
|
|
20
|
+
export declare function extractParamNames(func: Function): string[];
|
|
21
|
+
/**
|
|
22
|
+
* Decorator factory that wraps a method to capture its arguments based on
|
|
23
|
+
* extracted parameter names and store them in AsyncLocalStorage.
|
|
24
|
+
*/
|
|
25
|
+
export declare function ParamsToEnv(): MethodDecorator;
|
|
26
|
+
//# sourceMappingURL=env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE/C;;;;GAIG;AACH,MAAM,WAAW,UAAU;IAC1B,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC1B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;CACtD;AAED;;GAEG;AACH,eAAO,MAAM,UAAU,KAAsC,CAAA;AAE7D;;;;GAIG;AAEH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,EAAE,CA8B1D;AAED;;;GAGG;AAEH,wBAAgB,WAAW,IAAI,eAAe,CA6D7C"}
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
/**
|
|
4
|
+
* Store for environment variables accessible via AsyncLocalStorage
|
|
5
|
+
*/
|
|
6
|
+
export const envStorage = new AsyncLocalStorage();
|
|
7
|
+
/**
|
|
8
|
+
* Helper to extract parameter names from a function's source code.
|
|
9
|
+
* Note: This approach using Function.toString() can be fragile and might
|
|
10
|
+
* break with code minification or complex function definitions.
|
|
11
|
+
*/
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
13
|
+
export function extractParamNames(func) {
|
|
14
|
+
const funcStr = func.toString().replace(/(\r\n|\n|\r)/gm, '');
|
|
15
|
+
// Regex to find parameter list within parentheses, handles various function definition styles
|
|
16
|
+
const paramsMatch = funcStr.match(/(?:async\s*)?(?:function\s*[\w\s]*)?\(([^)]*)\)/) ??
|
|
17
|
+
funcStr.match(/^\s*(?:async\s*)?\(([^)]*)\)/); // Arrow function support
|
|
18
|
+
if (!paramsMatch || typeof paramsMatch[1] === 'undefined') {
|
|
19
|
+
console.warn('Could not extract parameter names from function string:', funcStr);
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
const paramsStr = paramsMatch[1];
|
|
23
|
+
// Remove comments, default values, and type annotations to isolate names
|
|
24
|
+
const cleanedParamsStr = paramsStr
|
|
25
|
+
.replace(/\/\*.*?\*\//g, '') // Remove block comments
|
|
26
|
+
.replace(/\/\/.*?$/gm, ''); // Remove line comments
|
|
27
|
+
if (!cleanedParamsStr.trim()) {
|
|
28
|
+
return []; // No parameters
|
|
29
|
+
}
|
|
30
|
+
return cleanedParamsStr
|
|
31
|
+
.split(',')
|
|
32
|
+
.map((param) => {
|
|
33
|
+
// Remove type annotations (e.g., ": Secret") and default initializers (e.g., "= 'default'")
|
|
34
|
+
const namePart = param.split(/[:=]/)[0];
|
|
35
|
+
return namePart?.trim() ?? '';
|
|
36
|
+
})
|
|
37
|
+
.filter((name) => name.length > 0); // Filter out empty strings from trailing commas etc.
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Decorator factory that wraps a method to capture its arguments based on
|
|
41
|
+
* extracted parameter names and store them in AsyncLocalStorage.
|
|
42
|
+
*/
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
44
|
+
export function ParamsToEnv() {
|
|
45
|
+
return function (_target, propertyKey, descriptor) {
|
|
46
|
+
if (typeof descriptor === 'undefined' || typeof descriptor.value !== 'function') {
|
|
47
|
+
throw new Error(`@ParamsToEnv decorator can only be applied to methods, not: ${String(propertyKey)}`);
|
|
48
|
+
}
|
|
49
|
+
const originalMethod = descriptor.value;
|
|
50
|
+
let paramNames = null; // Cache parameter names lazily
|
|
51
|
+
descriptor.value = function (...args) {
|
|
52
|
+
const parentContext = envStorage.getStore();
|
|
53
|
+
if (paramNames === null) {
|
|
54
|
+
paramNames = extractParamNames(originalMethod);
|
|
55
|
+
// Basic validation: Check if the number of extracted names matches the number of arguments received.
|
|
56
|
+
// This might be inaccurate if optional parameters aren't passed.
|
|
57
|
+
if (paramNames.length !== args.length && typeof propertyKey === 'string') {
|
|
58
|
+
// Log a warning but attempt to proceed. The context might be incomplete.
|
|
59
|
+
console.warn(`Parameter name extraction/argument count mismatch for ${propertyKey}: ` +
|
|
60
|
+
`Extracted ${paramNames.length} names (${paramNames.join(', ')}), received ${args.length} args. ` +
|
|
61
|
+
`Context in AsyncLocalStorage may be incomplete or incorrect.`);
|
|
62
|
+
// Optionally, you could try to pad paramNames or truncate args, but it's risky.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const newEnv = {};
|
|
66
|
+
const currentParams = new Set(); // Track params for current method
|
|
67
|
+
paramNames?.forEach((name, index) => {
|
|
68
|
+
// Check if the param name matches the ENV_VAR pattern
|
|
69
|
+
if (/^[A-Z0-9_]+$/.test(name)) {
|
|
70
|
+
// Add all matching defined parameter names to currentParams
|
|
71
|
+
currentParams.add(name);
|
|
72
|
+
// Only add to newEnv if argument was actually passed
|
|
73
|
+
if (index < args.length) {
|
|
74
|
+
newEnv[name] = args[index];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// If paramNames.length > args.length, some names won't get a value, which is expected for optional params.
|
|
78
|
+
});
|
|
79
|
+
// merge parent context with new context (new takes precedence)
|
|
80
|
+
const parentMergedEnv = parentContext?.mergedEnv ?? {};
|
|
81
|
+
const mergedEnv = { ...parentMergedEnv, ...newEnv };
|
|
82
|
+
// Prepare context object for storage
|
|
83
|
+
const contextToStore = {
|
|
84
|
+
currentParams,
|
|
85
|
+
mergedEnv,
|
|
86
|
+
};
|
|
87
|
+
// run the original method within the AsyncLocalStorage context using the merged env
|
|
88
|
+
return envStorage.run(contextToStore, () => {
|
|
89
|
+
// The original method is called with its original arguments
|
|
90
|
+
return originalMethod.apply(this, args);
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=env.js.map
|
package/dist/env.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAcpD;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,iBAA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G,CAAC,IAAI,CAAC,CAAA;oBACvB,qDAAqD;oBACrD,IAAI,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;wBACzB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAA;oBAC3B,CAAC;gBACF,CAAC;gBACD,2GAA2G;YAC5G,CAAC,CAAC,CAAA;YAEF,+DAA+D;YAC/D,MAAM,eAAe,GAAG,aAAa,EAAE,SAAS,IAAI,EAAE,CAAA;YACtD,MAAM,SAAS,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,MAAM,EAAE,CAAA;YAEnD,qCAAqC;YACrC,MAAM,cAAc,GAAe;gBAClC,aAAa;gBACb,SAAS;aACT,CAAA;YAED,oFAAoF;YACpF,OAAO,UAAU,CAAC,GAAG,CAAC,cAAc,EAAE,GAAG,EAAE;gBAC1C,4DAA4D;gBAC5D,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;YACxC,CAAC,CAAC,CAAA;QACH,CAAC,CAAA;IACF,CAAC,CAAA;AACF,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAClD,YAAY,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAE1C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAGlD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jahands/dagger-helpers",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "dagger.io helpers for my own projects - not meant for public use",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"dagger",
|
|
8
|
+
"dagger.io"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/jahands/workers-packages/tree/main/packages/dagger-helpers",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/jahands/workers-packages.git",
|
|
14
|
+
"directory": "packages/dagger-helpers"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Jacob Hands",
|
|
19
|
+
"url": "https://github.com/jahands"
|
|
20
|
+
},
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
|
+
"module": "./dist/index.js",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"typescript": "5.5.4",
|
|
38
|
+
"vitest": "2.1.1",
|
|
39
|
+
"@repo/tools": "0.8.1",
|
|
40
|
+
"@repo/typescript-config": "0.3.4"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@dagger.io/dagger": "^0.18.4"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "runx build tsc ./src/index.ts",
|
|
47
|
+
"check:lint": "run-eslint-workers",
|
|
48
|
+
"check:types": "runx check --exports",
|
|
49
|
+
"test": "run-vitest"
|
|
50
|
+
}
|
|
51
|
+
}
|