@dotenvx/primitives 0.7.1 → 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 +131 -47
  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,26 +6146,33 @@ var require_scan = __commonJS({
6146
6146
  }
6147
6147
  return q;
6148
6148
  }
6149
- function clean(value, quote) {
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 === '"') {
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, transform) {
6159
+ function scan2(src, options = {}, transform) {
6160
+ if (typeof options === "function") {
6161
+ transform = options;
6162
+ options = {};
6163
+ }
6160
6164
  const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
6161
6165
  const parsed = {};
6162
- const lines = (src || "").toString().replace(/\r\n?/mg, "\n");
6166
+ let lines = (src || "").toString();
6167
+ if (options.convertWindowsNewlines !== false) {
6168
+ lines = lines.replace(/\r\n?/mg, "\n");
6169
+ }
6163
6170
  let match;
6164
6171
  while ((match = LINE.exec(lines)) !== null) {
6165
6172
  const name = match[1];
6166
6173
  const raw = match[2];
6167
6174
  const quote = getQuote(raw);
6168
- const value = clean(raw, quote);
6175
+ const value = clean(raw, quote, options);
6169
6176
  let parsedValue = value;
6170
6177
  if (typeof transform === "function") {
6171
6178
  parsedValue = transform({
@@ -6174,7 +6181,8 @@ var require_scan = __commonJS({
6174
6181
  quote
6175
6182
  });
6176
6183
  }
6177
- parsed[name] = parsedValue;
6184
+ parsed[name] = parsed[name] || [];
6185
+ parsed[name].push(parsedValue);
6178
6186
  }
6179
6187
  return {
6180
6188
  parsed
@@ -6194,36 +6202,60 @@ var require_keyring = __commonJS({
6194
6202
  try {
6195
6203
  const src = fs.readFileSync(filepath, "utf8");
6196
6204
  const { parsed } = scan2(src);
6197
- return parsed;
6205
+ const result = [];
6206
+ for (const name in parsed) {
6207
+ if (name.startsWith("DOTENV_PRIVATE_KEY")) {
6208
+ result.push(parsed[name][parsed[name].length - 1]);
6209
+ }
6210
+ }
6211
+ return result;
6198
6212
  } catch (_e) {
6199
- return {};
6213
+ return [];
6200
6214
  }
6201
6215
  }
6202
- function keyring2() {
6203
- const mem = {};
6204
- const processEnv = process.env;
6205
- const keysFilepath = ".env.keys";
6206
- const sources = {
6207
- ...keysFromFile(keysFilepath),
6208
- ...processEnv
6209
- // process.env wins
6210
- };
6211
- 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) {
6212
6224
  if (name.startsWith("DOTENV_PRIVATE_KEY")) {
6213
- try {
6214
- const privateKeyHex = sources[name];
6215
- const publicKeyHex = derive2(privateKeyHex);
6216
- mem[publicKeyHex] = privateKeyHex;
6217
- } catch (_e) {
6218
- }
6225
+ privateKeyValues.push(processEnv[name]);
6219
6226
  }
6220
6227
  }
6221
- 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;
6222
6236
  }
6223
6237
  module2.exports = keyring2;
6224
6238
  }
6225
6239
  });
6226
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
+
6227
6259
  // src/parse.js
6228
6260
  var require_parse = __commonJS({
6229
6261
  "src/parse.js"(exports2, module2) {
@@ -6232,32 +6264,56 @@ var require_parse = __commonJS({
6232
6264
  var encrypted2 = require_encrypted();
6233
6265
  var evaluate2 = require_evaluate();
6234
6266
  var expand2 = require_expand();
6267
+ var publickeys2 = require_publickeys();
6235
6268
  var scan2 = require_scan();
6236
6269
  function resolveEscapeSequences(value) {
6237
6270
  return value.replace(/\\\$/g, "$");
6238
6271
  }
6239
- function parse2(src, opts = {}) {
6240
- const overload = opts.overload;
6241
- const processEnv = opts.processEnv || process.env;
6242
- const ring = keyring2();
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
+ }
6284
+ }
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;
6295
+ }
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 });
6243
6305
  function inProcessEnv(name) {
6244
6306
  return Object.prototype.hasOwnProperty.call(processEnv, name);
6245
6307
  }
6246
- let publicKeyHex;
6247
6308
  const runningParsed = {};
6248
6309
  const literals = {};
6249
- const { parsed } = scan2(src, ({ name, value, quote }) => {
6310
+ const parsed = {};
6311
+ scan2(src, ({ name, value, quote }) => {
6250
6312
  let parsedValue = value;
6251
- if (name.startsWith("DOTENV_PUBLIC_KEY")) {
6252
- publicKeyHex = parsedValue;
6253
- }
6254
6313
  if (!overload && inProcessEnv(name)) {
6255
6314
  parsedValue = processEnv[name];
6256
6315
  }
6257
- try {
6258
- parsedValue = decrypt2(ring[publicKeyHex], parsedValue);
6259
- } catch (_e) {
6260
- }
6316
+ parsedValue = decryptWithKeyring(ring, parsedValue, publicKeyHexes);
6261
6317
  const encryptedPrefixed = encrypted2(parsedValue);
6262
6318
  let evaled = false;
6263
6319
  if (!encryptedPrefixed && quote !== "'" && (!inProcessEnv(name) || processEnv[name] === parsedValue)) {
@@ -6277,6 +6333,7 @@ var require_parse = __commonJS({
6277
6333
  literals[name] = parsedValue;
6278
6334
  }
6279
6335
  runningParsed[name] = parsedValue;
6336
+ parsed[name] = parsedValue;
6280
6337
  if (Object.prototype.hasOwnProperty.call(processEnv, name) && !overload) {
6281
6338
  } else {
6282
6339
  }
@@ -6290,6 +6347,29 @@ var require_parse = __commonJS({
6290
6347
  }
6291
6348
  });
6292
6349
 
6350
+ // src/sealed.js
6351
+ var require_sealed = __commonJS({
6352
+ "src/sealed.js"(exports2, module2) {
6353
+ var encrypted2 = require_encrypted();
6354
+ var scan2 = require_scan();
6355
+ function publicKey(name) {
6356
+ return name.startsWith("DOTENV_PUBLIC_KEY");
6357
+ }
6358
+ function sealed2(src) {
6359
+ const { parsed } = scan2(src);
6360
+ for (const [name, values] of Object.entries(parsed)) {
6361
+ for (const value of values) {
6362
+ if (!encrypted2(value) && !publicKey(name)) {
6363
+ return false;
6364
+ }
6365
+ }
6366
+ }
6367
+ return true;
6368
+ }
6369
+ module2.exports = sealed2;
6370
+ }
6371
+ });
6372
+
6293
6373
  // src/index.js
6294
6374
  var decrypt = require_decrypt();
6295
6375
  var derive = require_derive();
@@ -6300,7 +6380,9 @@ var expand = require_expand();
6300
6380
  var keypair = require_keypair();
6301
6381
  var keyring = require_keyring();
6302
6382
  var parse = require_parse();
6383
+ var publickeys = require_publickeys();
6303
6384
  var scan = require_scan();
6385
+ var sealed = require_sealed();
6304
6386
  module.exports = {
6305
6387
  decrypt,
6306
6388
  derive,
@@ -6311,5 +6393,7 @@ module.exports = {
6311
6393
  keypair,
6312
6394
  keyring,
6313
6395
  parse,
6314
- scan
6396
+ publickeys,
6397
+ scan,
6398
+ sealed
6315
6399
  };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.7.1",
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",