@dotenvx/primitives 1.0.1 → 1.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.
Files changed (2) hide show
  1. package/dist/index.cjs +90 -9
  2. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -6357,17 +6357,18 @@ var require_parse = __commonJS({
6357
6357
  return value;
6358
6358
  }
6359
6359
  async function parse2(src, options = {}) {
6360
- const { overload, processEnv, fk, provider, publicKeyHexes, ring: initialRing } = keyringOptions(src, options);
6360
+ const { overload, array, processEnv, fk, provider, publicKeyHexes, ring: initialRing } = keyringOptions(src, options);
6361
6361
  const ring = await keyring2({ processEnv, ring: initialRing, fk, provider });
6362
- return parseWithRing(src, { overload, processEnv, ring, publicKeyHexes });
6362
+ return parseWithRing(src, { overload, array, processEnv, ring, publicKeyHexes });
6363
6363
  }
6364
6364
  function parseSync2(src, options = {}) {
6365
- const { overload, processEnv, fk, provider, publicKeyHexes, ring: initialRing } = keyringOptions(src, options);
6365
+ const { overload, array, processEnv, fk, provider, publicKeyHexes, ring: initialRing } = keyringOptions(src, options);
6366
6366
  const ring = keyringSync2({ processEnv, ring: initialRing, fk, provider });
6367
- return parseWithRing(src, { overload, processEnv, ring, publicKeyHexes });
6367
+ return parseWithRing(src, { overload, array, processEnv, ring, publicKeyHexes });
6368
6368
  }
6369
6369
  function keyringOptions(src, options = {}) {
6370
6370
  const overload = options.overload;
6371
+ const array = options.array;
6371
6372
  const processEnv = options.processEnv || process.env;
6372
6373
  const fk = options.fk;
6373
6374
  const provider = options.provider;
@@ -6376,10 +6377,11 @@ var require_parse = __commonJS({
6376
6377
  for (const publicKeyHex of publicKeyHexes) {
6377
6378
  ring[publicKeyHex] = "";
6378
6379
  }
6379
- return { overload, processEnv, fk, provider, publicKeyHexes, ring };
6380
+ return { overload, array, processEnv, fk, provider, publicKeyHexes, ring };
6380
6381
  }
6381
6382
  function parseWithRing(src, options = {}) {
6382
6383
  const overload = options.overload;
6384
+ const array = options.array;
6383
6385
  const processEnv = options.processEnv || process.env;
6384
6386
  const ring = options.ring || {};
6385
6387
  const publicKeyHexes = options.publicKeyHexes || [];
@@ -6416,11 +6418,26 @@ var require_parse = __commonJS({
6416
6418
  literals[name] = parsedValue;
6417
6419
  }
6418
6420
  runningParsed[name] = parsedValue;
6419
- parsed[name] = parsedValue;
6421
+ if (array) {
6422
+ parsed[name] = parsed[name] || [];
6423
+ parsed[name].push(parsedValue);
6424
+ } else {
6425
+ parsed[name] = parsedValue;
6426
+ }
6420
6427
  if (Object.prototype.hasOwnProperty.call(processEnv, name) && !overload) {
6421
- existed[name] = processEnv[name];
6428
+ if (array) {
6429
+ existed[name] = existed[name] || [];
6430
+ existed[name].push(processEnv[name]);
6431
+ } else {
6432
+ existed[name] = processEnv[name];
6433
+ }
6422
6434
  } else {
6423
- injected[name] = parsed[name];
6435
+ if (array) {
6436
+ injected[name] = injected[name] || [];
6437
+ injected[name].push(parsedValue);
6438
+ } else {
6439
+ injected[name] = parsed[name];
6440
+ }
6424
6441
  }
6425
6442
  return parsedValue;
6426
6443
  });
@@ -6458,6 +6475,68 @@ var require_sealed = __commonJS({
6458
6475
  }
6459
6476
  });
6460
6477
 
6478
+ // src/upsert.js
6479
+ var require_upsert = __commonJS({
6480
+ "src/upsert.js"(exports2, module2) {
6481
+ var scan2 = require_scan();
6482
+ function escapeForRegex(str) {
6483
+ return str.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
6484
+ }
6485
+ function replaceExistingValue(src, key, originalValue, replaceValue) {
6486
+ const escapedKey = escapeForRegex(key);
6487
+ const escapedOriginalValue = escapeForRegex(originalValue);
6488
+ let enforceEndOfLine = "";
6489
+ if (escapedOriginalValue === "") {
6490
+ enforceEndOfLine = "$";
6491
+ }
6492
+ const currentPart = new RegExp(
6493
+ "^(\\s*)?(export\\s+)?" + escapedKey + "\\s*=\\s*([\"'`]?)" + escapedOriginalValue + "\\3" + enforceEndOfLine,
6494
+ "m"
6495
+ );
6496
+ return src.replace(currentPart, function(match, spaces = "", exportPart = "", quote = "") {
6497
+ let newPart = `${key}=${quote}${replaceValue}${quote}`;
6498
+ const newlineMatch = src.match(new RegExp(`${escapedKey}\\s*=\\s*
6499
+
6500
+ `, "m"));
6501
+ if (escapedOriginalValue === "" && quote === "" && newlineMatch) {
6502
+ const newlineCount = newlineMatch[0].match(/\n/g).length - 1;
6503
+ for (let i = 0; i < newlineCount; i++) {
6504
+ newPart += "\n";
6505
+ }
6506
+ }
6507
+ return `${spaces}${exportPart}${newPart}`;
6508
+ });
6509
+ }
6510
+ function upsert2(src, key, replaceValue) {
6511
+ let output;
6512
+ let newPart = "";
6513
+ const { parsed } = scan2(src, { expandDoubleQuotedNewlines: false, convertWindowsNewlines: false });
6514
+ if (Object.prototype.hasOwnProperty.call(parsed, key)) {
6515
+ const allValues = parsed[key];
6516
+ let duplicateOutput = src;
6517
+ const replacements = Array.isArray(replaceValue) ? replaceValue : allValues.map(() => replaceValue);
6518
+ allValues.forEach((value, index) => {
6519
+ duplicateOutput = replaceExistingValue(duplicateOutput, key, value, `\0DOTENVX_UPSERT_${index}\0`);
6520
+ });
6521
+ replacements.forEach((replacement, index) => {
6522
+ duplicateOutput = duplicateOutput.split(`\0DOTENVX_UPSERT_${index}\0`).join(replacement);
6523
+ });
6524
+ return duplicateOutput;
6525
+ } else {
6526
+ newPart += `${key}="${replaceValue}"`;
6527
+ if (src.endsWith("\n")) {
6528
+ newPart = newPart + "\n";
6529
+ } else {
6530
+ newPart = "\n" + newPart;
6531
+ }
6532
+ output = src + newPart;
6533
+ }
6534
+ return output;
6535
+ }
6536
+ module2.exports = upsert2;
6537
+ }
6538
+ });
6539
+
6461
6540
  // src/index.js
6462
6541
  var decrypt = require_decrypt();
6463
6542
  var derive = require_derive();
@@ -6473,6 +6552,7 @@ var parseSync = require_parse().sync;
6473
6552
  var publickeys = require_publickeys();
6474
6553
  var scan = require_scan();
6475
6554
  var sealed = require_sealed();
6555
+ var upsert = require_upsert();
6476
6556
  module.exports = {
6477
6557
  decrypt,
6478
6558
  derive,
@@ -6487,5 +6567,6 @@ module.exports = {
6487
6567
  parseSync,
6488
6568
  publickeys,
6489
6569
  scan,
6490
- sealed
6570
+ sealed,
6571
+ upsert
6491
6572
  };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.1",
2
+ "version": "1.2.0",
3
3
  "name": "@dotenvx/primitives",
4
4
  "description": "a secure dotenv–from the creator of `dotenv`",
5
5
  "author": "@motdotla",