-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (123 loc) · 3.96 KB
/
index.js
File metadata and controls
140 lines (123 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* @fileoverview Vault - A secure interface to OS-native keychains.
*/
import VaultService from './src/domain/services/VaultService.js';
import VaultError from './src/domain/errors/VaultError.js';
import PlatformNotSupportedError from './src/domain/errors/PlatformNotSupportedError.js';
import SecretNotFoundError from './src/domain/errors/SecretNotFoundError.js';
import { createBunKeychainAdapter } from './src/infrastructure/adapters/bun/index.js';
import { createDenoKeychainAdapter } from './src/infrastructure/adapters/deno/index.js';
import { defaultRuntime } from './src/runtime/index.js';
const isNodeRuntime =
typeof process !== 'undefined' &&
typeof process.versions?.node === 'string' &&
process.release?.name === 'node';
let nodeRequire;
if (isNodeRuntime) {
const { createRequire } = await import('module');
nodeRequire = createRequire(import.meta.url);
}
const loadNodeAdapterModule = () => {
if (!nodeRequire) {
throw new Error('Node runtime is required for the Node keychain adapter');
}
return nodeRequire('./src/infrastructure/adapters/node/index.js');
};
/**
* Create a keychain adapter backed by the Node runtime.
* @param {Object} [options]
* @param {string} [options.account] - Account/scope for the secrets.
* @returns {KeychainAdapter}
*/
export function createNodeKeychainAdapter(options) {
return loadNodeAdapterModule().createNodeKeychainAdapter(options);
}
/**
* Detect the environment and instantiate the matching keychain adapter.
* @param {Object} params
* @param {string} [params.account]
* @returns {KeychainAdapter}
*/
const detectAdapter = ({ account }) => {
if (typeof Bun !== 'undefined' && typeof Bun.spawnSync === 'function') {
return createBunKeychainAdapter({ account });
}
if (typeof Deno !== 'undefined' && typeof Deno.Command === 'function') {
return createDenoKeychainAdapter({ account });
}
return createNodeKeychainAdapter({ account });
};
export {
VaultService,
VaultError,
PlatformNotSupportedError,
SecretNotFoundError,
createBunKeychainAdapter,
createDenoKeychainAdapter
};
/**
* Facade class for the Vault library.
* Maintains backward compatibility with v1.0.0.
*/
export default class Vault {
/**
* @param {Object} options
* @param {string} [options.account='git-stunts']
* @param {Function} [options.adapterFactory]
*/
constructor({ account = 'git-stunts', adapterFactory } = {}) {
if (adapterFactory != null && typeof adapterFactory !== 'function') {
throw new TypeError('adapterFactory must be a function');
}
const adapter =
adapterFactory ? adapterFactory({ account }) : detectAdapter({ account });
this.service = new VaultService({ account, adapter });
}
get account() {
return this.service.account;
}
get isMac() {
return defaultRuntime.getPlatform() === 'darwin';
}
get isLinux() {
return defaultRuntime.getPlatform() === 'linux';
}
get isWindows() {
return defaultRuntime.getPlatform() === 'win32';
}
getSecret({ target }) {
return this.service.getSecret(target);
}
setSecret({ target, value }) {
this.service.setSecret(target, value);
}
/**
* Remove the secret for the requested target.
* @param {Object} params
* @param {string} params.target
* @returns {boolean}
*/
deleteSecret({ target }) {
return this.service.deleteSecret(target);
}
/**
* Resolve a value from the vault or environment variables.
* @param {Object} params
* @param {string} params.envKey
* @param {string} params.vaultTarget
* @returns {string|undefined}
*/
resolveSecret({ envKey, vaultTarget }) {
return this.service.resolveSecret({ envKey, vaultTarget });
}
/**
* Ensure a secret exists, prompting if configured.
* @param {Object} params
* @param {string} params.target
* @param {string} [params.promptMessage]
* @returns {Promise<string>}
*/
async ensureSecret({ target, promptMessage }) {
return this.service.ensureSecret({ target, promptMessage });
}
}