envlock-core 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Benjamin Davies
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,82 @@
1
+ # envlock-core
2
+
3
+ Framework-agnostic 1Password + dotenvx secret injection logic.
4
+
5
+ > Most users should install [`envlock`](https://www.npmjs.com/package/envlock) instead. This package is for integrating envlock with frameworks other than Next.js.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add envlock-core
11
+ ```
12
+
13
+ ## API
14
+
15
+ ### `runWithSecrets(options)`
16
+
17
+ Runs a command with secrets injected from 1Password via dotenvx. If `DOTENV_PRIVATE_KEY_<ENV>` is already set (e.g. in CI), it skips `op run` and calls `dotenvx run` directly.
18
+
19
+ ```ts
20
+ import { runWithSecrets } from 'envlock-core';
21
+
22
+ runWithSecrets({
23
+ envFile: '.env.production',
24
+ environment: 'production',
25
+ onePasswordEnvId: 'ca6uypwvab5mevel44gqdc2zae',
26
+ command: 'node',
27
+ args: ['server.js'],
28
+ });
29
+ ```
30
+
31
+ **Options:**
32
+
33
+ | Option | Type | Description |
34
+ |--------|------|-------------|
35
+ | `envFile` | `string` | Path to the encrypted dotenvx env file |
36
+ | `environment` | `string` | Environment name (`development`, `staging`, `production`) |
37
+ | `onePasswordEnvId` | `string` | Your 1Password Environment ID |
38
+ | `command` | `string` | The command to run |
39
+ | `args` | `string[]` | Arguments to pass to the command |
40
+
41
+ ### `validateOnePasswordEnvId(id)`
42
+
43
+ Throws if `id` is not a valid 1Password Environment ID (lowercase alphanumeric + hyphens). Protects against CLI flag injection and shell metacharacters.
44
+
45
+ ### `validateEnvFilePath(envFile, cwd)`
46
+
47
+ Throws if `envFile` resolves outside `cwd`. Protects against path traversal.
48
+
49
+ ### `hasBinary(name)`
50
+
51
+ Returns `true` if `name` is found in `PATH`.
52
+
53
+ ### `checkBinary(name, installHint)`
54
+
55
+ Calls `process.exit(1)` with a helpful message if `name` is not in `PATH`.
56
+
57
+ ## Types
58
+
59
+ ```ts
60
+ type Environment = 'development' | 'staging' | 'production';
61
+
62
+ interface EnvlockOptions {
63
+ onePasswordEnvId: string;
64
+ envFiles?: {
65
+ development?: string;
66
+ staging?: string;
67
+ production?: string;
68
+ };
69
+ }
70
+
71
+ interface RunWithSecretsOptions {
72
+ envFile: string;
73
+ environment: string;
74
+ onePasswordEnvId: string;
75
+ command: string;
76
+ args: string[];
77
+ }
78
+ ```
79
+
80
+ ## License
81
+
82
+ MIT — [Benjamin Davies](https://github.com/BenDavies1218)
@@ -0,0 +1,26 @@
1
+ interface RunWithSecretsOptions {
2
+ envFile: string;
3
+ environment: string;
4
+ onePasswordEnvId: string;
5
+ command: string;
6
+ args: string[];
7
+ }
8
+ declare function runWithSecrets(options: RunWithSecretsOptions): void;
9
+
10
+ declare function hasBinary(name: string): boolean;
11
+ declare function checkBinary(name: string, installHint: string): void;
12
+
13
+ declare function validateOnePasswordEnvId(id: string): void;
14
+ declare function validateEnvFilePath(envFile: string, cwd: string): void;
15
+
16
+ type Environment = "development" | "staging" | "production";
17
+ interface EnvlockOptions {
18
+ onePasswordEnvId: string;
19
+ envFiles?: {
20
+ development?: string;
21
+ staging?: string;
22
+ production?: string;
23
+ };
24
+ }
25
+
26
+ export { type Environment, type EnvlockOptions, type RunWithSecretsOptions, checkBinary, hasBinary, runWithSecrets, validateEnvFilePath, validateOnePasswordEnvId };
package/dist/index.js ADDED
@@ -0,0 +1,94 @@
1
+ // src/invoke.ts
2
+ import { spawnSync } from "child_process";
3
+
4
+ // src/detect.ts
5
+ import { execFileSync } from "child_process";
6
+ var WHICH = process.platform === "win32" ? "where" : "which";
7
+ function hasBinary(name) {
8
+ try {
9
+ execFileSync(WHICH, [name], { stdio: "pipe" });
10
+ return true;
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
15
+ function checkBinary(name, installHint) {
16
+ if (!hasBinary(name)) {
17
+ console.error(`[envlock] '${name}' not found in PATH.
18
+ ${installHint}`);
19
+ process.exit(1);
20
+ }
21
+ }
22
+
23
+ // src/invoke.ts
24
+ function runWithSecrets(options) {
25
+ const { envFile, environment, onePasswordEnvId, command, args } = options;
26
+ checkBinary(
27
+ "dotenvx",
28
+ "Install dotenvx: npm install -g @dotenvx/dotenvx\nOr add it as a dev dependency."
29
+ );
30
+ const privateKeyVar = `DOTENV_PRIVATE_KEY_${environment.toUpperCase()}`;
31
+ const keyAlreadyInjected = !!process.env[privateKeyVar];
32
+ let result;
33
+ if (keyAlreadyInjected) {
34
+ result = spawnSync(
35
+ "dotenvx",
36
+ ["run", "-f", envFile, "--", command, ...args],
37
+ { stdio: "inherit" }
38
+ );
39
+ } else {
40
+ checkBinary(
41
+ "op",
42
+ "Install 1Password CLI: brew install --cask 1password-cli@beta\nThen sign in: op signin"
43
+ );
44
+ result = spawnSync(
45
+ "op",
46
+ [
47
+ "run",
48
+ "--environment",
49
+ onePasswordEnvId,
50
+ "--",
51
+ "dotenvx",
52
+ "run",
53
+ "-f",
54
+ envFile,
55
+ "--",
56
+ command,
57
+ ...args
58
+ ],
59
+ { stdio: "inherit" }
60
+ );
61
+ }
62
+ process.exit(result.status ?? 1);
63
+ }
64
+
65
+ // src/validate.ts
66
+ import { isAbsolute, relative, resolve } from "path";
67
+ var OP_ENV_ID_PATTERN = /^[a-z0-9][a-z0-9-]*$/;
68
+ function validateOnePasswordEnvId(id) {
69
+ if (!id || !OP_ENV_ID_PATTERN.test(id)) {
70
+ throw new Error(
71
+ `[envlock] Invalid onePasswordEnvId: "${id}". Must be a lowercase alphanumeric string (hyphens allowed), e.g. 'ca6uypwvab5mevel44gqdc2zae'.`
72
+ );
73
+ }
74
+ }
75
+ function validateEnvFilePath(envFile, cwd) {
76
+ if (envFile.includes("\0")) {
77
+ throw new Error(`[envlock] Invalid env file path: null bytes are not allowed.`);
78
+ }
79
+ const resolved = resolve(cwd, envFile);
80
+ const base = resolve(cwd);
81
+ const rel = relative(base, resolved);
82
+ if (rel.startsWith("..") || isAbsolute(rel)) {
83
+ throw new Error(
84
+ `[envlock] Invalid env file path: "${envFile}" resolves outside the project directory.`
85
+ );
86
+ }
87
+ }
88
+ export {
89
+ checkBinary,
90
+ hasBinary,
91
+ runWithSecrets,
92
+ validateEnvFilePath,
93
+ validateOnePasswordEnvId
94
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "envlock-core",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Core 1Password + dotenvx secret injection logic for envlock",
6
+ "license": "MIT",
7
+ "author": "Benjamin Davies",
8
+ "homepage": "https://github.com/BenDavies1218/envlock#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/BenDavies1218/envlock.git",
12
+ "directory": "packages/core"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/BenDavies1218/envlock/issues"
16
+ },
17
+ "engines": {
18
+ "node": ">=18"
19
+ },
20
+ "exports": {
21
+ ".": {
22
+ "import": "./dist/index.js",
23
+ "types": "./dist/index.d.ts"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "devDependencies": {
30
+ "@types/node": "^20.14.10",
31
+ "tsup": "^8.0.0",
32
+ "typescript": "^5.8.2",
33
+ "vitest": "^3.0.0"
34
+ },
35
+ "scripts": {
36
+ "build": "tsup",
37
+ "dev": "tsup --watch",
38
+ "test": "vitest run",
39
+ "test:watch": "vitest"
40
+ }
41
+ }