@dotenvx/primitives 0.8.0 → 0.9.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.
Files changed (2) hide show
  1. package/dist/index.cjs +95 -55
  2. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -6031,9 +6031,9 @@ var require_evaluate = __commonJS({
6031
6031
  function chomp(value) {
6032
6032
  return value.replace(/[\r\n]+$/, "");
6033
6033
  }
6034
- function evaluate2(value, opts) {
6035
- const processEnv = opts.processEnv || process.env;
6036
- const runningParsed = opts.runningParsed || {};
6034
+ function evaluate2(value, options) {
6035
+ const processEnv = options.processEnv || process.env;
6036
+ const runningParsed = options.runningParsed || {};
6037
6037
  const matches = value.match(/\$\(([^)]+(?:\)[^(]*)*)\)/g) || [];
6038
6038
  return matches.reduce((newValue, match) => {
6039
6039
  const command = match.slice(2, -1);
@@ -6054,11 +6054,11 @@ var require_evaluate = __commonJS({
6054
6054
  // src/expand.js
6055
6055
  var require_expand = __commonJS({
6056
6056
  "src/expand.js"(exports2, module2) {
6057
- function expand2(value, opts) {
6058
- const overload = opts.overload;
6059
- const processEnv = opts.processEnv || process.env;
6060
- const runningParsed = opts.runningParsed || {};
6061
- const literals = opts.literals || {};
6057
+ function expand2(value, options) {
6058
+ const overload = options.overload;
6059
+ const processEnv = options.processEnv || process.env;
6060
+ const runningParsed = options.runningParsed || {};
6061
+ const literals = options.literals || {};
6062
6062
  let env = { ...runningParsed, ...processEnv };
6063
6063
  if (overload) {
6064
6064
  env = { ...processEnv, ...runningParsed };
@@ -6146,25 +6146,25 @@ var require_scan = __commonJS({
6146
6146
  }
6147
6147
  return q;
6148
6148
  }
6149
- function clean(value, quote, opts) {
6149
+ function clean(value, quote, options) {
6150
6150
  let v = trimmer(value);
6151
6151
  v = v.replace(/^(['"`])([\s\S]*)\1$/mg, "$2");
6152
- if (quote === '"' && opts.expandDoubleQuotedNewlines !== false) {
6152
+ if (quote === '"' && options.expandDoubleQuotedNewlines !== false) {
6153
6153
  v = v.replace(/\\n/g, "\n");
6154
6154
  v = v.replace(/\\r/g, "\r");
6155
6155
  v = v.replace(/\\t/g, " ");
6156
6156
  }
6157
6157
  return v;
6158
6158
  }
6159
- function scan2(src, opts = {}, transform) {
6160
- if (typeof opts === "function") {
6161
- transform = opts;
6162
- opts = {};
6159
+ function scan2(src, options = {}, transform) {
6160
+ if (typeof options === "function") {
6161
+ transform = options;
6162
+ options = {};
6163
6163
  }
6164
6164
  const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
6165
6165
  const parsed = {};
6166
6166
  let lines = (src || "").toString();
6167
- if (opts.convertWindowsNewlines !== false) {
6167
+ if (options.convertWindowsNewlines !== false) {
6168
6168
  lines = lines.replace(/\r\n?/mg, "\n");
6169
6169
  }
6170
6170
  let match;
@@ -6172,7 +6172,7 @@ var require_scan = __commonJS({
6172
6172
  const name = match[1];
6173
6173
  const raw = match[2];
6174
6174
  const quote = getQuote(raw);
6175
- const value = clean(raw, quote, opts);
6175
+ const value = clean(raw, quote, options);
6176
6176
  let parsedValue = value;
6177
6177
  if (typeof transform === "function") {
6178
6178
  parsedValue = transform({
@@ -6202,40 +6202,60 @@ var require_keyring = __commonJS({
6202
6202
  try {
6203
6203
  const src = fs.readFileSync(filepath, "utf8");
6204
6204
  const { parsed } = scan2(src);
6205
- const result = {};
6205
+ const result = [];
6206
6206
  for (const name in parsed) {
6207
- result[name] = parsed[name][parsed[name].length - 1];
6207
+ if (name.startsWith("DOTENV_PRIVATE_KEY")) {
6208
+ result.push(parsed[name][parsed[name].length - 1]);
6209
+ }
6208
6210
  }
6209
6211
  return result;
6210
6212
  } catch (_e) {
6211
- return {};
6213
+ return [];
6212
6214
  }
6213
6215
  }
6214
- function keyring2() {
6215
- const mem = {};
6216
- const processEnv = process.env;
6217
- const keysFilepath = ".env.keys";
6218
- const sources = {
6219
- ...keysFromFile(keysFilepath),
6220
- ...processEnv
6221
- // process.env wins
6222
- };
6223
- for (const name in sources) {
6216
+ function keyring2(options = {}) {
6217
+ const ring = options.ring || {};
6218
+ const processEnv = options.processEnv || process.env;
6219
+ const keysFilepaths = Array.isArray(options.fk) ? options.fk : [options.fk || ".env.keys"];
6220
+ const privateKeyValues = keysFilepaths.reduce((acc, filepath) => {
6221
+ return acc.concat(keysFromFile(filepath));
6222
+ }, []);
6223
+ for (const name in processEnv) {
6224
6224
  if (name.startsWith("DOTENV_PRIVATE_KEY")) {
6225
- try {
6226
- const privateKeyHex = sources[name];
6227
- const publicKeyHex = derive2(privateKeyHex);
6228
- mem[publicKeyHex] = privateKeyHex;
6229
- } catch (_e) {
6230
- }
6225
+ privateKeyValues.push(processEnv[name]);
6231
6226
  }
6232
6227
  }
6233
- return mem;
6228
+ for (const privateKeyHex of privateKeyValues) {
6229
+ try {
6230
+ const publicKeyHex = derive2(privateKeyHex);
6231
+ ring[publicKeyHex] = privateKeyHex;
6232
+ } catch (_e) {
6233
+ }
6234
+ }
6235
+ return ring;
6234
6236
  }
6235
6237
  module2.exports = keyring2;
6236
6238
  }
6237
6239
  });
6238
6240
 
6241
+ // src/publickeys.js
6242
+ var require_publickeys = __commonJS({
6243
+ "src/publickeys.js"(exports2, module2) {
6244
+ var scan2 = require_scan();
6245
+ function publickeys2(src) {
6246
+ const { parsed } = scan2(src);
6247
+ const result = [];
6248
+ for (const name in parsed) {
6249
+ if (name.startsWith("DOTENV_PUBLIC_KEY")) {
6250
+ result.push(parsed[name][parsed[name].length - 1]);
6251
+ }
6252
+ }
6253
+ return result;
6254
+ }
6255
+ module2.exports = publickeys2;
6256
+ }
6257
+ });
6258
+
6239
6259
  // src/parse.js
6240
6260
  var require_parse = __commonJS({
6241
6261
  "src/parse.js"(exports2, module2) {
@@ -6244,39 +6264,56 @@ var require_parse = __commonJS({
6244
6264
  var encrypted2 = require_encrypted();
6245
6265
  var evaluate2 = require_evaluate();
6246
6266
  var expand2 = require_expand();
6267
+ var publickeys2 = require_publickeys();
6247
6268
  var scan2 = require_scan();
6248
6269
  function resolveEscapeSequences(value) {
6249
6270
  return value.replace(/\\\$/g, "$");
6250
6271
  }
6251
- function collapse(parsed) {
6252
- const result = {};
6253
- for (const name in parsed) {
6254
- result[name] = parsed[name][parsed[name].length - 1];
6272
+ function decryptWithKeyring(ring, value, publicKeyHexes) {
6273
+ const tried = {};
6274
+ for (const publicKeyHex of publicKeyHexes) {
6275
+ const privateKeyHex = ring[publicKeyHex];
6276
+ if (!privateKeyHex) {
6277
+ continue;
6278
+ }
6279
+ tried[privateKeyHex] = true;
6280
+ try {
6281
+ return decrypt2(privateKeyHex, value);
6282
+ } catch (_e) {
6283
+ }
6255
6284
  }
6256
- return result;
6285
+ for (const privateKeyHex of Object.values(ring)) {
6286
+ if (!privateKeyHex || tried[privateKeyHex]) {
6287
+ continue;
6288
+ }
6289
+ try {
6290
+ return decrypt2(privateKeyHex, value);
6291
+ } catch (_e) {
6292
+ }
6293
+ }
6294
+ return value;
6257
6295
  }
6258
- function parse2(src, opts = {}) {
6259
- const overload = opts.overload;
6260
- const processEnv = opts.processEnv || process.env;
6261
- const ring = keyring2();
6296
+ function parse2(src, options = {}) {
6297
+ const overload = options.overload;
6298
+ const processEnv = options.processEnv || process.env;
6299
+ const publicKeyHexes = publickeys2(src);
6300
+ let ring = {};
6301
+ for (const publicKeyHex of publicKeyHexes) {
6302
+ ring[publicKeyHex] = "";
6303
+ }
6304
+ ring = keyring2({ processEnv, ring });
6262
6305
  function inProcessEnv(name) {
6263
6306
  return Object.prototype.hasOwnProperty.call(processEnv, name);
6264
6307
  }
6265
- let publicKeyHex;
6266
6308
  const runningParsed = {};
6267
6309
  const literals = {};
6268
- const { parsed } = scan2(src, ({ name, value, quote }) => {
6310
+ const parsed = {};
6311
+ scan2(src, ({ name, value, quote }) => {
6269
6312
  let parsedValue = value;
6270
- if (name.startsWith("DOTENV_PUBLIC_KEY")) {
6271
- publicKeyHex = parsedValue;
6272
- }
6273
6313
  if (!overload && inProcessEnv(name)) {
6274
6314
  parsedValue = processEnv[name];
6275
6315
  }
6276
- try {
6277
- parsedValue = decrypt2(ring[publicKeyHex], parsedValue);
6278
- } catch (_e) {
6279
- }
6316
+ parsedValue = decryptWithKeyring(ring, parsedValue, publicKeyHexes);
6280
6317
  const encryptedPrefixed = encrypted2(parsedValue);
6281
6318
  let evaled = false;
6282
6319
  if (!encryptedPrefixed && quote !== "'" && (!inProcessEnv(name) || processEnv[name] === parsedValue)) {
@@ -6296,13 +6333,14 @@ var require_parse = __commonJS({
6296
6333
  literals[name] = parsedValue;
6297
6334
  }
6298
6335
  runningParsed[name] = parsedValue;
6336
+ parsed[name] = parsedValue;
6299
6337
  if (Object.prototype.hasOwnProperty.call(processEnv, name) && !overload) {
6300
6338
  } else {
6301
6339
  }
6302
6340
  return parsedValue;
6303
6341
  });
6304
6342
  return {
6305
- parsed: collapse(parsed)
6343
+ parsed
6306
6344
  };
6307
6345
  }
6308
6346
  module2.exports = parse2;
@@ -6342,6 +6380,7 @@ var expand = require_expand();
6342
6380
  var keypair = require_keypair();
6343
6381
  var keyring = require_keyring();
6344
6382
  var parse = require_parse();
6383
+ var publickeys = require_publickeys();
6345
6384
  var scan = require_scan();
6346
6385
  var sealed = require_sealed();
6347
6386
  module.exports = {
@@ -6354,6 +6393,7 @@ module.exports = {
6354
6393
  keypair,
6355
6394
  keyring,
6356
6395
  parse,
6396
+ publickeys,
6357
6397
  scan,
6358
6398
  sealed
6359
6399
  };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.8.0",
2
+ "version": "0.9.0",
3
3
  "name": "@dotenvx/primitives",
4
4
  "description": "a secure dotenv–from the creator of `dotenv`",
5
5
  "author": "@motdotla",