@uniformdev/redirect 19.68.1-alpha.27 → 19.71.1-alpha.103

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/dist/index.d.mts CHANGED
@@ -111,8 +111,6 @@ declare class RedirectClient extends ApiClient<RedirectClientOptions> {
111
111
  */
112
112
  private static processDefinitionToResults;
113
113
  static validateRedirect(url: string, redirectDefinition: RedirectDefinition['redirect']): boolean;
114
- static getTargetVariableExpandedUrl(url: string, redirectDefinition: RedirectDefinition['redirect'], isVariable?: (pathSegment: string) => boolean): string;
115
- private static mergeQueryStrings;
116
114
  private static getSourceVariables;
117
115
  }
118
116
  declare class UncachedRedirectClient extends RedirectClient {
@@ -402,6 +400,12 @@ declare class WithMemoryCache extends RedirectClientCache<RedirectClientCacheOpt
402
400
  refresh(): Promise<void[]>;
403
401
  }
404
402
 
403
+ declare function getTargetVariableExpandedUrl(url: string, redirectDefinition: RedirectDefinition['redirect'], isVariable?: (pathSegment: string) => boolean): string;
404
+ declare function getSourceVariables(path: string, source: string, isVariable?: (pathSegment: string, isLast: boolean) => boolean): {
405
+ [variable: string]: string;
406
+ };
407
+ declare function mergeQueryStrings(qs1: string, qs2: string): string;
408
+
405
409
  type Wildcard = {
406
410
  pathSegment: string;
407
411
  index: number;
@@ -448,4 +452,4 @@ type ProcessedUrl = {
448
452
  */
449
453
  declare function processUrl(url: string): ProcessedUrl;
450
454
 
451
- export { DirectionAwareRedirectDefinition, ExtractWildcards, PathTrie, PathTrieData, ProcessedUrl, RedirectClient, RedirectClientGetRedirect, RedirectClientGetRedirects, RedirectClientGetRequest, RedirectClientOptions, RedirectDataCache, RedirectDefinition, RedirectDefinitions, RedirectDeleteRequest, RedirectDeleteResponse, RedirectFileConverter, RedirectFileConverterParams, RedirectGetRequest, RedirectGetResponse, RedirectOptions, RedirectResult, RedirectUpsertRequest, RedirectUpsertResponse, SourceAndTarget, SourceTargetAndWildcards, UncachedRedirectClient, Wildcard, WithMemoryCache, pathTrieReturn, processUrl };
455
+ export { type DirectionAwareRedirectDefinition, ExtractWildcards, PathTrie, PathTrieData, type ProcessedUrl, RedirectClient, type RedirectClientGetRedirect, type RedirectClientGetRedirects, type RedirectClientGetRequest, type RedirectClientOptions, type RedirectDataCache, type RedirectDefinition, type RedirectDefinitions, type RedirectDeleteRequest, type RedirectDeleteResponse, RedirectFileConverter, type RedirectFileConverterParams, type RedirectGetRequest, type RedirectGetResponse, type RedirectOptions, type RedirectResult, type RedirectUpsertRequest, type RedirectUpsertResponse, type SourceAndTarget, type SourceTargetAndWildcards, UncachedRedirectClient, type Wildcard, WithMemoryCache, getSourceVariables, getTargetVariableExpandedUrl, mergeQueryStrings, type pathTrieReturn, processUrl };
package/dist/index.d.ts CHANGED
@@ -111,8 +111,6 @@ declare class RedirectClient extends ApiClient<RedirectClientOptions> {
111
111
  */
112
112
  private static processDefinitionToResults;
113
113
  static validateRedirect(url: string, redirectDefinition: RedirectDefinition['redirect']): boolean;
114
- static getTargetVariableExpandedUrl(url: string, redirectDefinition: RedirectDefinition['redirect'], isVariable?: (pathSegment: string) => boolean): string;
115
- private static mergeQueryStrings;
116
114
  private static getSourceVariables;
117
115
  }
118
116
  declare class UncachedRedirectClient extends RedirectClient {
@@ -402,6 +400,12 @@ declare class WithMemoryCache extends RedirectClientCache<RedirectClientCacheOpt
402
400
  refresh(): Promise<void[]>;
403
401
  }
404
402
 
403
+ declare function getTargetVariableExpandedUrl(url: string, redirectDefinition: RedirectDefinition['redirect'], isVariable?: (pathSegment: string) => boolean): string;
404
+ declare function getSourceVariables(path: string, source: string, isVariable?: (pathSegment: string, isLast: boolean) => boolean): {
405
+ [variable: string]: string;
406
+ };
407
+ declare function mergeQueryStrings(qs1: string, qs2: string): string;
408
+
405
409
  type Wildcard = {
406
410
  pathSegment: string;
407
411
  index: number;
@@ -448,4 +452,4 @@ type ProcessedUrl = {
448
452
  */
449
453
  declare function processUrl(url: string): ProcessedUrl;
450
454
 
451
- export { DirectionAwareRedirectDefinition, ExtractWildcards, PathTrie, PathTrieData, ProcessedUrl, RedirectClient, RedirectClientGetRedirect, RedirectClientGetRedirects, RedirectClientGetRequest, RedirectClientOptions, RedirectDataCache, RedirectDefinition, RedirectDefinitions, RedirectDeleteRequest, RedirectDeleteResponse, RedirectFileConverter, RedirectFileConverterParams, RedirectGetRequest, RedirectGetResponse, RedirectOptions, RedirectResult, RedirectUpsertRequest, RedirectUpsertResponse, SourceAndTarget, SourceTargetAndWildcards, UncachedRedirectClient, Wildcard, WithMemoryCache, pathTrieReturn, processUrl };
455
+ export { type DirectionAwareRedirectDefinition, ExtractWildcards, PathTrie, PathTrieData, type ProcessedUrl, RedirectClient, type RedirectClientGetRedirect, type RedirectClientGetRedirects, type RedirectClientGetRequest, type RedirectClientOptions, type RedirectDataCache, type RedirectDefinition, type RedirectDefinitions, type RedirectDeleteRequest, type RedirectDeleteResponse, RedirectFileConverter, type RedirectFileConverterParams, type RedirectGetRequest, type RedirectGetResponse, type RedirectOptions, type RedirectResult, type RedirectUpsertRequest, type RedirectUpsertResponse, type SourceAndTarget, type SourceTargetAndWildcards, UncachedRedirectClient, type Wildcard, WithMemoryCache, getSourceVariables, getTargetVariableExpandedUrl, mergeQueryStrings, type pathTrieReturn, processUrl };
package/dist/index.esm.js CHANGED
@@ -1,5 +1,3 @@
1
- import "./chunk-TLYAXJ7N.mjs";
2
-
3
1
  // src/cache/data/refresher.ts
4
2
  var Refresher = class {
5
3
  /**
@@ -274,6 +272,58 @@ function processUrl(url) {
274
272
  };
275
273
  }
276
274
 
275
+ // src/util/process.ts
276
+ function getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
277
+ const processedTarget = processUrl(redirectDefinition.targetUrl);
278
+ const processedSource = processUrl(redirectDefinition.sourceUrl);
279
+ let finalUrlPath = processedTarget.path;
280
+ const processedUrl = processUrl(url);
281
+ const variables = getSourceVariables(processedUrl.path, processedSource.path, isVariable);
282
+ for (const variable in variables) {
283
+ finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
284
+ }
285
+ let protocol = redirectDefinition.targetPreserveIncomingProtocol || processedTarget.protocol === "" ? processedUrl.protocol : processedTarget.protocol;
286
+ const domain = protocol !== "" ? redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.domain : processedTarget.domain : "";
287
+ if (domain === "" && protocol !== "") {
288
+ protocol = "";
289
+ }
290
+ const port = domain === "" ? "" : redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.port : processedTarget.port;
291
+ const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
292
+ const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
293
+ return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
294
+ }
295
+ function getSourceVariables(path, source, isVariable = (pathSegment, isLast) => pathSegment.startsWith(":") || pathSegment === "*" && isLast) {
296
+ const variables = {};
297
+ const pathSegments = path.split("/");
298
+ const sourceSegments = source.split("/");
299
+ if (pathSegments.length !== sourceSegments.length && !source.endsWith("/*")) {
300
+ throw new Error("Path and source have different numbers of path segments, must be the same");
301
+ }
302
+ sourceSegments.forEach((sourceSegment, i) => {
303
+ if (isVariable(sourceSegment, i === sourceSegments.length - 1)) {
304
+ variables[sourceSegment] = pathSegments[i];
305
+ }
306
+ });
307
+ return variables;
308
+ }
309
+ function mergeQueryStrings(qs1, qs2) {
310
+ let fragment = false;
311
+ if (qs1.startsWith("#")) {
312
+ fragment = true;
313
+ qs1 = qs1.substring(1);
314
+ }
315
+ if (qs2.startsWith("#")) {
316
+ fragment = true;
317
+ qs2 = qs2.substring(1);
318
+ }
319
+ const params1 = new URLSearchParams(qs1);
320
+ const params2 = new URLSearchParams(qs2);
321
+ const merged = new URLSearchParams([...params1, ...params2]).toString();
322
+ if (merged.length > 0)
323
+ return (fragment ? "#" : "?") + merged;
324
+ return "";
325
+ }
326
+
277
327
  // src/redirectClient.ts
278
328
  var _RedirectClient = class _RedirectClient extends ApiClient {
279
329
  constructor(options) {
@@ -530,7 +580,7 @@ var _RedirectClient = class _RedirectClient extends ApiClient {
530
580
  const redirect = definition == null ? void 0 : definition.redirect;
531
581
  if (redirect.sourceMustMatchDomain && processedUrl.domain !== processedResult.domain)
532
582
  return void 0;
533
- const finalUrl = (options == null ? void 0 : options.reverse) ? "n/a" : _RedirectClient.getTargetVariableExpandedUrl(processedUrl.url, redirect);
583
+ const finalUrl = (options == null ? void 0 : options.reverse) ? "n/a" : getTargetVariableExpandedUrl(processedUrl.url, redirect);
534
584
  return {
535
585
  url: finalUrl,
536
586
  definition,
@@ -556,42 +606,6 @@ var _RedirectClient = class _RedirectClient extends ApiClient {
556
606
  }
557
607
  return true;
558
608
  }
559
- static getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
560
- const processedTarget = processUrl(redirectDefinition.targetUrl);
561
- const processedSource = processUrl(redirectDefinition.sourceUrl);
562
- let finalUrlPath = processedTarget.path;
563
- const processedUrl = processUrl(url);
564
- const variables = this.getSourceVariables(processedUrl.path, processedSource.path, isVariable);
565
- for (const variable in variables) {
566
- finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
567
- }
568
- let protocol = redirectDefinition.targetPreserveIncomingProtocol || processedTarget.protocol === "" ? processedUrl.protocol : processedTarget.protocol;
569
- const domain = protocol !== "" ? redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.domain : processedTarget.domain : "";
570
- if (domain === "" && protocol !== "") {
571
- protocol = "";
572
- }
573
- const port = domain === "" ? "" : redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.port : processedTarget.port;
574
- const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
575
- const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
576
- return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
577
- }
578
- static mergeQueryStrings(qs1, qs2) {
579
- let fragment = false;
580
- if (qs1.startsWith("#")) {
581
- fragment = true;
582
- qs1 = qs1.substring(1);
583
- }
584
- if (qs2.startsWith("#")) {
585
- fragment = true;
586
- qs2 = qs2.substring(1);
587
- }
588
- const params1 = new URLSearchParams(qs1);
589
- const params2 = new URLSearchParams(qs2);
590
- const merged = new URLSearchParams([...params1, ...params2]).toString();
591
- if (merged.length > 0)
592
- return (fragment ? "#" : "?") + merged;
593
- return "";
594
- }
595
609
  static getSourceVariables(path, source, isVariable = (pathSegment, isLast) => pathSegment.startsWith(":") || pathSegment === "*" && isLast) {
596
610
  const variables = {};
597
611
  const pathSegments = path.split("/");
@@ -624,8 +638,6 @@ var UncachedRedirectClient = class extends RedirectClient {
624
638
 
625
639
  // src/util/RedirectFileConverter.ts
626
640
  var getDefaultClient = async () => {
627
- const dotenv = await import("./main-4Y7KG46W.mjs");
628
- dotenv.config();
629
641
  return new RedirectClient({
630
642
  apiKey: process.env.UNIFORM_API_KEY,
631
643
  apiHost: process.env.UNIFORM_BASE_URL,
@@ -702,5 +714,8 @@ export {
702
714
  RedirectFileConverter,
703
715
  UncachedRedirectClient,
704
716
  WithMemoryCache,
717
+ getSourceVariables,
718
+ getTargetVariableExpandedUrl,
719
+ mergeQueryStrings,
705
720
  processUrl
706
721
  };
package/dist/index.js CHANGED
@@ -5,9 +5,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __commonJS = (cb, mod) => function __require() {
9
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
- };
11
8
  var __export = (target, all) => {
12
9
  for (var name in all)
13
10
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -30,300 +27,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
27
  ));
31
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
29
 
33
- // ../../node_modules/.pnpm/dotenv@16.3.1/node_modules/dotenv/package.json
34
- var require_package = __commonJS({
35
- "../../node_modules/.pnpm/dotenv@16.3.1/node_modules/dotenv/package.json"(exports, module2) {
36
- module2.exports = {
37
- name: "dotenv",
38
- version: "16.3.1",
39
- description: "Loads environment variables from .env file",
40
- main: "lib/main.js",
41
- types: "lib/main.d.ts",
42
- exports: {
43
- ".": {
44
- types: "./lib/main.d.ts",
45
- require: "./lib/main.js",
46
- default: "./lib/main.js"
47
- },
48
- "./config": "./config.js",
49
- "./config.js": "./config.js",
50
- "./lib/env-options": "./lib/env-options.js",
51
- "./lib/env-options.js": "./lib/env-options.js",
52
- "./lib/cli-options": "./lib/cli-options.js",
53
- "./lib/cli-options.js": "./lib/cli-options.js",
54
- "./package.json": "./package.json"
55
- },
56
- scripts: {
57
- "dts-check": "tsc --project tests/types/tsconfig.json",
58
- lint: "standard",
59
- "lint-readme": "standard-markdown",
60
- pretest: "npm run lint && npm run dts-check",
61
- test: "tap tests/*.js --100 -Rspec",
62
- prerelease: "npm test",
63
- release: "standard-version"
64
- },
65
- repository: {
66
- type: "git",
67
- url: "git://github.com/motdotla/dotenv.git"
68
- },
69
- funding: "https://github.com/motdotla/dotenv?sponsor=1",
70
- keywords: [
71
- "dotenv",
72
- "env",
73
- ".env",
74
- "environment",
75
- "variables",
76
- "config",
77
- "settings"
78
- ],
79
- readmeFilename: "README.md",
80
- license: "BSD-2-Clause",
81
- devDependencies: {
82
- "@definitelytyped/dtslint": "^0.0.133",
83
- "@types/node": "^18.11.3",
84
- decache: "^4.6.1",
85
- sinon: "^14.0.1",
86
- standard: "^17.0.0",
87
- "standard-markdown": "^7.1.0",
88
- "standard-version": "^9.5.0",
89
- tap: "^16.3.0",
90
- tar: "^6.1.11",
91
- typescript: "^4.8.4"
92
- },
93
- engines: {
94
- node: ">=12"
95
- },
96
- browser: {
97
- fs: false
98
- }
99
- };
100
- }
101
- });
102
-
103
- // ../../node_modules/.pnpm/dotenv@16.3.1/node_modules/dotenv/lib/main.js
104
- var require_main = __commonJS({
105
- "../../node_modules/.pnpm/dotenv@16.3.1/node_modules/dotenv/lib/main.js"(exports, module2) {
106
- "use strict";
107
- var fs = require("fs");
108
- var path = require("path");
109
- var os = require("os");
110
- var crypto = require("crypto");
111
- var packageJson = require_package();
112
- var version = packageJson.version;
113
- var LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
114
- function parse(src) {
115
- const obj = {};
116
- let lines = src.toString();
117
- lines = lines.replace(/\r\n?/mg, "\n");
118
- let match;
119
- while ((match = LINE.exec(lines)) != null) {
120
- const key = match[1];
121
- let value = match[2] || "";
122
- value = value.trim();
123
- const maybeQuote = value[0];
124
- value = value.replace(/^(['"`])([\s\S]*)\1$/mg, "$2");
125
- if (maybeQuote === '"') {
126
- value = value.replace(/\\n/g, "\n");
127
- value = value.replace(/\\r/g, "\r");
128
- }
129
- obj[key] = value;
130
- }
131
- return obj;
132
- }
133
- function _parseVault(options) {
134
- const vaultPath = _vaultPath(options);
135
- const result = DotenvModule.configDotenv({ path: vaultPath });
136
- if (!result.parsed) {
137
- throw new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`);
138
- }
139
- const keys = _dotenvKey(options).split(",");
140
- const length = keys.length;
141
- let decrypted;
142
- for (let i = 0; i < length; i++) {
143
- try {
144
- const key = keys[i].trim();
145
- const attrs = _instructions(result, key);
146
- decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key);
147
- break;
148
- } catch (error) {
149
- if (i + 1 >= length) {
150
- throw error;
151
- }
152
- }
153
- }
154
- return DotenvModule.parse(decrypted);
155
- }
156
- function _log(message) {
157
- console.log(`[dotenv@${version}][INFO] ${message}`);
158
- }
159
- function _warn(message) {
160
- console.log(`[dotenv@${version}][WARN] ${message}`);
161
- }
162
- function _debug(message) {
163
- console.log(`[dotenv@${version}][DEBUG] ${message}`);
164
- }
165
- function _dotenvKey(options) {
166
- if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
167
- return options.DOTENV_KEY;
168
- }
169
- if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
170
- return process.env.DOTENV_KEY;
171
- }
172
- return "";
173
- }
174
- function _instructions(result, dotenvKey) {
175
- let uri;
176
- try {
177
- uri = new URL(dotenvKey);
178
- } catch (error) {
179
- if (error.code === "ERR_INVALID_URL") {
180
- throw new Error("INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=development");
181
- }
182
- throw error;
183
- }
184
- const key = uri.password;
185
- if (!key) {
186
- throw new Error("INVALID_DOTENV_KEY: Missing key part");
187
- }
188
- const environment = uri.searchParams.get("environment");
189
- if (!environment) {
190
- throw new Error("INVALID_DOTENV_KEY: Missing environment part");
191
- }
192
- const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`;
193
- const ciphertext = result.parsed[environmentKey];
194
- if (!ciphertext) {
195
- throw new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`);
196
- }
197
- return { ciphertext, key };
198
- }
199
- function _vaultPath(options) {
200
- let dotenvPath = path.resolve(process.cwd(), ".env");
201
- if (options && options.path && options.path.length > 0) {
202
- dotenvPath = options.path;
203
- }
204
- return dotenvPath.endsWith(".vault") ? dotenvPath : `${dotenvPath}.vault`;
205
- }
206
- function _resolveHome(envPath) {
207
- return envPath[0] === "~" ? path.join(os.homedir(), envPath.slice(1)) : envPath;
208
- }
209
- function _configVault(options) {
210
- _log("Loading env from encrypted .env.vault");
211
- const parsed = DotenvModule._parseVault(options);
212
- let processEnv = process.env;
213
- if (options && options.processEnv != null) {
214
- processEnv = options.processEnv;
215
- }
216
- DotenvModule.populate(processEnv, parsed, options);
217
- return { parsed };
218
- }
219
- function configDotenv(options) {
220
- let dotenvPath = path.resolve(process.cwd(), ".env");
221
- let encoding = "utf8";
222
- const debug = Boolean(options && options.debug);
223
- if (options) {
224
- if (options.path != null) {
225
- dotenvPath = _resolveHome(options.path);
226
- }
227
- if (options.encoding != null) {
228
- encoding = options.encoding;
229
- }
230
- }
231
- try {
232
- const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }));
233
- let processEnv = process.env;
234
- if (options && options.processEnv != null) {
235
- processEnv = options.processEnv;
236
- }
237
- DotenvModule.populate(processEnv, parsed, options);
238
- return { parsed };
239
- } catch (e) {
240
- if (debug) {
241
- _debug(`Failed to load ${dotenvPath} ${e.message}`);
242
- }
243
- return { error: e };
244
- }
245
- }
246
- function config(options) {
247
- const vaultPath = _vaultPath(options);
248
- if (_dotenvKey(options).length === 0) {
249
- return DotenvModule.configDotenv(options);
250
- }
251
- if (!fs.existsSync(vaultPath)) {
252
- _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`);
253
- return DotenvModule.configDotenv(options);
254
- }
255
- return DotenvModule._configVault(options);
256
- }
257
- function decrypt(encrypted, keyStr) {
258
- const key = Buffer.from(keyStr.slice(-64), "hex");
259
- let ciphertext = Buffer.from(encrypted, "base64");
260
- const nonce = ciphertext.slice(0, 12);
261
- const authTag = ciphertext.slice(-16);
262
- ciphertext = ciphertext.slice(12, -16);
263
- try {
264
- const aesgcm = crypto.createDecipheriv("aes-256-gcm", key, nonce);
265
- aesgcm.setAuthTag(authTag);
266
- return `${aesgcm.update(ciphertext)}${aesgcm.final()}`;
267
- } catch (error) {
268
- const isRange = error instanceof RangeError;
269
- const invalidKeyLength = error.message === "Invalid key length";
270
- const decryptionFailed = error.message === "Unsupported state or unable to authenticate data";
271
- if (isRange || invalidKeyLength) {
272
- const msg = "INVALID_DOTENV_KEY: It must be 64 characters long (or more)";
273
- throw new Error(msg);
274
- } else if (decryptionFailed) {
275
- const msg = "DECRYPTION_FAILED: Please check your DOTENV_KEY";
276
- throw new Error(msg);
277
- } else {
278
- console.error("Error: ", error.code);
279
- console.error("Error: ", error.message);
280
- throw error;
281
- }
282
- }
283
- }
284
- function populate(processEnv, parsed, options = {}) {
285
- const debug = Boolean(options && options.debug);
286
- const override = Boolean(options && options.override);
287
- if (typeof parsed !== "object") {
288
- throw new Error("OBJECT_REQUIRED: Please check the processEnv argument being passed to populate");
289
- }
290
- for (const key of Object.keys(parsed)) {
291
- if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
292
- if (override === true) {
293
- processEnv[key] = parsed[key];
294
- }
295
- if (debug) {
296
- if (override === true) {
297
- _debug(`"${key}" is already defined and WAS overwritten`);
298
- } else {
299
- _debug(`"${key}" is already defined and was NOT overwritten`);
300
- }
301
- }
302
- } else {
303
- processEnv[key] = parsed[key];
304
- }
305
- }
306
- }
307
- var DotenvModule = {
308
- configDotenv,
309
- _configVault,
310
- _parseVault,
311
- config,
312
- decrypt,
313
- parse,
314
- populate
315
- };
316
- module2.exports.configDotenv = DotenvModule.configDotenv;
317
- module2.exports._configVault = DotenvModule._configVault;
318
- module2.exports._parseVault = DotenvModule._parseVault;
319
- module2.exports.config = DotenvModule.config;
320
- module2.exports.decrypt = DotenvModule.decrypt;
321
- module2.exports.parse = DotenvModule.parse;
322
- module2.exports.populate = DotenvModule.populate;
323
- module2.exports = DotenvModule;
324
- }
325
- });
326
-
327
30
  // src/index.ts
328
31
  var src_exports = {};
329
32
  __export(src_exports, {
@@ -334,6 +37,9 @@ __export(src_exports, {
334
37
  RedirectFileConverter: () => RedirectFileConverter,
335
38
  UncachedRedirectClient: () => UncachedRedirectClient,
336
39
  WithMemoryCache: () => WithMemoryCache,
40
+ getSourceVariables: () => getSourceVariables,
41
+ getTargetVariableExpandedUrl: () => getTargetVariableExpandedUrl,
42
+ mergeQueryStrings: () => mergeQueryStrings,
337
43
  processUrl: () => processUrl
338
44
  });
339
45
  module.exports = __toCommonJS(src_exports);
@@ -612,6 +318,58 @@ function processUrl(url) {
612
318
  };
613
319
  }
614
320
 
321
+ // src/util/process.ts
322
+ function getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
323
+ const processedTarget = processUrl(redirectDefinition.targetUrl);
324
+ const processedSource = processUrl(redirectDefinition.sourceUrl);
325
+ let finalUrlPath = processedTarget.path;
326
+ const processedUrl = processUrl(url);
327
+ const variables = getSourceVariables(processedUrl.path, processedSource.path, isVariable);
328
+ for (const variable in variables) {
329
+ finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
330
+ }
331
+ let protocol = redirectDefinition.targetPreserveIncomingProtocol || processedTarget.protocol === "" ? processedUrl.protocol : processedTarget.protocol;
332
+ const domain = protocol !== "" ? redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.domain : processedTarget.domain : "";
333
+ if (domain === "" && protocol !== "") {
334
+ protocol = "";
335
+ }
336
+ const port = domain === "" ? "" : redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.port : processedTarget.port;
337
+ const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
338
+ const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
339
+ return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
340
+ }
341
+ function getSourceVariables(path, source, isVariable = (pathSegment, isLast) => pathSegment.startsWith(":") || pathSegment === "*" && isLast) {
342
+ const variables = {};
343
+ const pathSegments = path.split("/");
344
+ const sourceSegments = source.split("/");
345
+ if (pathSegments.length !== sourceSegments.length && !source.endsWith("/*")) {
346
+ throw new Error("Path and source have different numbers of path segments, must be the same");
347
+ }
348
+ sourceSegments.forEach((sourceSegment, i) => {
349
+ if (isVariable(sourceSegment, i === sourceSegments.length - 1)) {
350
+ variables[sourceSegment] = pathSegments[i];
351
+ }
352
+ });
353
+ return variables;
354
+ }
355
+ function mergeQueryStrings(qs1, qs2) {
356
+ let fragment = false;
357
+ if (qs1.startsWith("#")) {
358
+ fragment = true;
359
+ qs1 = qs1.substring(1);
360
+ }
361
+ if (qs2.startsWith("#")) {
362
+ fragment = true;
363
+ qs2 = qs2.substring(1);
364
+ }
365
+ const params1 = new URLSearchParams(qs1);
366
+ const params2 = new URLSearchParams(qs2);
367
+ const merged = new URLSearchParams([...params1, ...params2]).toString();
368
+ if (merged.length > 0)
369
+ return (fragment ? "#" : "?") + merged;
370
+ return "";
371
+ }
372
+
615
373
  // src/redirectClient.ts
616
374
  var _RedirectClient = class _RedirectClient extends import_api.ApiClient {
617
375
  constructor(options) {
@@ -868,7 +626,7 @@ var _RedirectClient = class _RedirectClient extends import_api.ApiClient {
868
626
  const redirect = definition == null ? void 0 : definition.redirect;
869
627
  if (redirect.sourceMustMatchDomain && processedUrl.domain !== processedResult.domain)
870
628
  return void 0;
871
- const finalUrl = (options == null ? void 0 : options.reverse) ? "n/a" : _RedirectClient.getTargetVariableExpandedUrl(processedUrl.url, redirect);
629
+ const finalUrl = (options == null ? void 0 : options.reverse) ? "n/a" : getTargetVariableExpandedUrl(processedUrl.url, redirect);
872
630
  return {
873
631
  url: finalUrl,
874
632
  definition,
@@ -894,42 +652,6 @@ var _RedirectClient = class _RedirectClient extends import_api.ApiClient {
894
652
  }
895
653
  return true;
896
654
  }
897
- static getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
898
- const processedTarget = processUrl(redirectDefinition.targetUrl);
899
- const processedSource = processUrl(redirectDefinition.sourceUrl);
900
- let finalUrlPath = processedTarget.path;
901
- const processedUrl = processUrl(url);
902
- const variables = this.getSourceVariables(processedUrl.path, processedSource.path, isVariable);
903
- for (const variable in variables) {
904
- finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
905
- }
906
- let protocol = redirectDefinition.targetPreserveIncomingProtocol || processedTarget.protocol === "" ? processedUrl.protocol : processedTarget.protocol;
907
- const domain = protocol !== "" ? redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.domain : processedTarget.domain : "";
908
- if (domain === "" && protocol !== "") {
909
- protocol = "";
910
- }
911
- const port = domain === "" ? "" : redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.port : processedTarget.port;
912
- const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
913
- const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
914
- return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
915
- }
916
- static mergeQueryStrings(qs1, qs2) {
917
- let fragment = false;
918
- if (qs1.startsWith("#")) {
919
- fragment = true;
920
- qs1 = qs1.substring(1);
921
- }
922
- if (qs2.startsWith("#")) {
923
- fragment = true;
924
- qs2 = qs2.substring(1);
925
- }
926
- const params1 = new URLSearchParams(qs1);
927
- const params2 = new URLSearchParams(qs2);
928
- const merged = new URLSearchParams([...params1, ...params2]).toString();
929
- if (merged.length > 0)
930
- return (fragment ? "#" : "?") + merged;
931
- return "";
932
- }
933
655
  static getSourceVariables(path, source, isVariable = (pathSegment, isLast) => pathSegment.startsWith(":") || pathSegment === "*" && isLast) {
934
656
  const variables = {};
935
657
  const pathSegments = path.split("/");
@@ -962,8 +684,6 @@ var UncachedRedirectClient = class extends RedirectClient {
962
684
 
963
685
  // src/util/RedirectFileConverter.ts
964
686
  var getDefaultClient = async () => {
965
- const dotenv = await Promise.resolve().then(() => __toESM(require_main()));
966
- dotenv.config();
967
687
  return new RedirectClient({
968
688
  apiKey: process.env.UNIFORM_API_KEY,
969
689
  apiHost: process.env.UNIFORM_BASE_URL,
@@ -1041,5 +761,8 @@ async function RedirectFileConverter({
1041
761
  RedirectFileConverter,
1042
762
  UncachedRedirectClient,
1043
763
  WithMemoryCache,
764
+ getSourceVariables,
765
+ getTargetVariableExpandedUrl,
766
+ mergeQueryStrings,
1044
767
  processUrl
1045
768
  });
package/dist/index.mjs CHANGED
@@ -1,5 +1,3 @@
1
- import "./chunk-TLYAXJ7N.mjs";
2
-
3
1
  // src/cache/data/refresher.ts
4
2
  var Refresher = class {
5
3
  /**
@@ -274,6 +272,58 @@ function processUrl(url) {
274
272
  };
275
273
  }
276
274
 
275
+ // src/util/process.ts
276
+ function getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
277
+ const processedTarget = processUrl(redirectDefinition.targetUrl);
278
+ const processedSource = processUrl(redirectDefinition.sourceUrl);
279
+ let finalUrlPath = processedTarget.path;
280
+ const processedUrl = processUrl(url);
281
+ const variables = getSourceVariables(processedUrl.path, processedSource.path, isVariable);
282
+ for (const variable in variables) {
283
+ finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
284
+ }
285
+ let protocol = redirectDefinition.targetPreserveIncomingProtocol || processedTarget.protocol === "" ? processedUrl.protocol : processedTarget.protocol;
286
+ const domain = protocol !== "" ? redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.domain : processedTarget.domain : "";
287
+ if (domain === "" && protocol !== "") {
288
+ protocol = "";
289
+ }
290
+ const port = domain === "" ? "" : redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.port : processedTarget.port;
291
+ const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
292
+ const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
293
+ return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
294
+ }
295
+ function getSourceVariables(path, source, isVariable = (pathSegment, isLast) => pathSegment.startsWith(":") || pathSegment === "*" && isLast) {
296
+ const variables = {};
297
+ const pathSegments = path.split("/");
298
+ const sourceSegments = source.split("/");
299
+ if (pathSegments.length !== sourceSegments.length && !source.endsWith("/*")) {
300
+ throw new Error("Path and source have different numbers of path segments, must be the same");
301
+ }
302
+ sourceSegments.forEach((sourceSegment, i) => {
303
+ if (isVariable(sourceSegment, i === sourceSegments.length - 1)) {
304
+ variables[sourceSegment] = pathSegments[i];
305
+ }
306
+ });
307
+ return variables;
308
+ }
309
+ function mergeQueryStrings(qs1, qs2) {
310
+ let fragment = false;
311
+ if (qs1.startsWith("#")) {
312
+ fragment = true;
313
+ qs1 = qs1.substring(1);
314
+ }
315
+ if (qs2.startsWith("#")) {
316
+ fragment = true;
317
+ qs2 = qs2.substring(1);
318
+ }
319
+ const params1 = new URLSearchParams(qs1);
320
+ const params2 = new URLSearchParams(qs2);
321
+ const merged = new URLSearchParams([...params1, ...params2]).toString();
322
+ if (merged.length > 0)
323
+ return (fragment ? "#" : "?") + merged;
324
+ return "";
325
+ }
326
+
277
327
  // src/redirectClient.ts
278
328
  var _RedirectClient = class _RedirectClient extends ApiClient {
279
329
  constructor(options) {
@@ -530,7 +580,7 @@ var _RedirectClient = class _RedirectClient extends ApiClient {
530
580
  const redirect = definition == null ? void 0 : definition.redirect;
531
581
  if (redirect.sourceMustMatchDomain && processedUrl.domain !== processedResult.domain)
532
582
  return void 0;
533
- const finalUrl = (options == null ? void 0 : options.reverse) ? "n/a" : _RedirectClient.getTargetVariableExpandedUrl(processedUrl.url, redirect);
583
+ const finalUrl = (options == null ? void 0 : options.reverse) ? "n/a" : getTargetVariableExpandedUrl(processedUrl.url, redirect);
534
584
  return {
535
585
  url: finalUrl,
536
586
  definition,
@@ -556,42 +606,6 @@ var _RedirectClient = class _RedirectClient extends ApiClient {
556
606
  }
557
607
  return true;
558
608
  }
559
- static getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
560
- const processedTarget = processUrl(redirectDefinition.targetUrl);
561
- const processedSource = processUrl(redirectDefinition.sourceUrl);
562
- let finalUrlPath = processedTarget.path;
563
- const processedUrl = processUrl(url);
564
- const variables = this.getSourceVariables(processedUrl.path, processedSource.path, isVariable);
565
- for (const variable in variables) {
566
- finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
567
- }
568
- let protocol = redirectDefinition.targetPreserveIncomingProtocol || processedTarget.protocol === "" ? processedUrl.protocol : processedTarget.protocol;
569
- const domain = protocol !== "" ? redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.domain : processedTarget.domain : "";
570
- if (domain === "" && protocol !== "") {
571
- protocol = "";
572
- }
573
- const port = domain === "" ? "" : redirectDefinition.targetPreserveIncomingDomain || processedTarget.domain === "" ? processedUrl.port : processedTarget.port;
574
- const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
575
- const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
576
- return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
577
- }
578
- static mergeQueryStrings(qs1, qs2) {
579
- let fragment = false;
580
- if (qs1.startsWith("#")) {
581
- fragment = true;
582
- qs1 = qs1.substring(1);
583
- }
584
- if (qs2.startsWith("#")) {
585
- fragment = true;
586
- qs2 = qs2.substring(1);
587
- }
588
- const params1 = new URLSearchParams(qs1);
589
- const params2 = new URLSearchParams(qs2);
590
- const merged = new URLSearchParams([...params1, ...params2]).toString();
591
- if (merged.length > 0)
592
- return (fragment ? "#" : "?") + merged;
593
- return "";
594
- }
595
609
  static getSourceVariables(path, source, isVariable = (pathSegment, isLast) => pathSegment.startsWith(":") || pathSegment === "*" && isLast) {
596
610
  const variables = {};
597
611
  const pathSegments = path.split("/");
@@ -624,8 +638,6 @@ var UncachedRedirectClient = class extends RedirectClient {
624
638
 
625
639
  // src/util/RedirectFileConverter.ts
626
640
  var getDefaultClient = async () => {
627
- const dotenv = await import("./main-4Y7KG46W.mjs");
628
- dotenv.config();
629
641
  return new RedirectClient({
630
642
  apiKey: process.env.UNIFORM_API_KEY,
631
643
  apiHost: process.env.UNIFORM_BASE_URL,
@@ -702,5 +714,8 @@ export {
702
714
  RedirectFileConverter,
703
715
  UncachedRedirectClient,
704
716
  WithMemoryCache,
717
+ getSourceVariables,
718
+ getTargetVariableExpandedUrl,
719
+ mergeQueryStrings,
705
720
  processUrl
706
721
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/redirect",
3
- "version": "19.68.1-alpha.27+9da5c6b31",
3
+ "version": "19.71.1-alpha.103+2e172bb17",
4
4
  "description": "Uniform redirect client",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "main": "./dist/index.js",
@@ -33,12 +33,12 @@
33
33
  "/dist"
34
34
  ],
35
35
  "dependencies": {
36
- "@uniformdev/context": "19.68.1-alpha.27+9da5c6b31",
36
+ "@uniformdev/context": "19.71.1-alpha.103+2e172bb17",
37
37
  "p-limit": "^3.1.0",
38
38
  "rfdc": "^1.3.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"
42
42
  },
43
- "gitHead": "9da5c6b318807a05aa003ceb52317b27f4e1d8cf"
43
+ "gitHead": "2e172bb17f644b34be818605979a1436b8621c90"
44
44
  }
@@ -1,16 +0,0 @@
1
- var __getOwnPropNames = Object.getOwnPropertyNames;
2
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
3
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
4
- }) : x)(function(x) {
5
- if (typeof require !== "undefined")
6
- return require.apply(this, arguments);
7
- throw Error('Dynamic require of "' + x + '" is not supported');
8
- });
9
- var __commonJS = (cb, mod) => function __require2() {
10
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
11
- };
12
-
13
- export {
14
- __require,
15
- __commonJS
16
- };
@@ -1,298 +0,0 @@
1
- import {
2
- __commonJS,
3
- __require
4
- } from "./chunk-TLYAXJ7N.mjs";
5
-
6
- // ../../node_modules/.pnpm/dotenv@16.3.1/node_modules/dotenv/package.json
7
- var require_package = __commonJS({
8
- "../../node_modules/.pnpm/dotenv@16.3.1/node_modules/dotenv/package.json"(exports, module) {
9
- module.exports = {
10
- name: "dotenv",
11
- version: "16.3.1",
12
- description: "Loads environment variables from .env file",
13
- main: "lib/main.js",
14
- types: "lib/main.d.ts",
15
- exports: {
16
- ".": {
17
- types: "./lib/main.d.ts",
18
- require: "./lib/main.js",
19
- default: "./lib/main.js"
20
- },
21
- "./config": "./config.js",
22
- "./config.js": "./config.js",
23
- "./lib/env-options": "./lib/env-options.js",
24
- "./lib/env-options.js": "./lib/env-options.js",
25
- "./lib/cli-options": "./lib/cli-options.js",
26
- "./lib/cli-options.js": "./lib/cli-options.js",
27
- "./package.json": "./package.json"
28
- },
29
- scripts: {
30
- "dts-check": "tsc --project tests/types/tsconfig.json",
31
- lint: "standard",
32
- "lint-readme": "standard-markdown",
33
- pretest: "npm run lint && npm run dts-check",
34
- test: "tap tests/*.js --100 -Rspec",
35
- prerelease: "npm test",
36
- release: "standard-version"
37
- },
38
- repository: {
39
- type: "git",
40
- url: "git://github.com/motdotla/dotenv.git"
41
- },
42
- funding: "https://github.com/motdotla/dotenv?sponsor=1",
43
- keywords: [
44
- "dotenv",
45
- "env",
46
- ".env",
47
- "environment",
48
- "variables",
49
- "config",
50
- "settings"
51
- ],
52
- readmeFilename: "README.md",
53
- license: "BSD-2-Clause",
54
- devDependencies: {
55
- "@definitelytyped/dtslint": "^0.0.133",
56
- "@types/node": "^18.11.3",
57
- decache: "^4.6.1",
58
- sinon: "^14.0.1",
59
- standard: "^17.0.0",
60
- "standard-markdown": "^7.1.0",
61
- "standard-version": "^9.5.0",
62
- tap: "^16.3.0",
63
- tar: "^6.1.11",
64
- typescript: "^4.8.4"
65
- },
66
- engines: {
67
- node: ">=12"
68
- },
69
- browser: {
70
- fs: false
71
- }
72
- };
73
- }
74
- });
75
-
76
- // ../../node_modules/.pnpm/dotenv@16.3.1/node_modules/dotenv/lib/main.js
77
- var require_main = __commonJS({
78
- "../../node_modules/.pnpm/dotenv@16.3.1/node_modules/dotenv/lib/main.js"(exports, module) {
79
- var fs = __require("fs");
80
- var path = __require("path");
81
- var os = __require("os");
82
- var crypto = __require("crypto");
83
- var packageJson = require_package();
84
- var version = packageJson.version;
85
- var LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
86
- function parse(src) {
87
- const obj = {};
88
- let lines = src.toString();
89
- lines = lines.replace(/\r\n?/mg, "\n");
90
- let match;
91
- while ((match = LINE.exec(lines)) != null) {
92
- const key = match[1];
93
- let value = match[2] || "";
94
- value = value.trim();
95
- const maybeQuote = value[0];
96
- value = value.replace(/^(['"`])([\s\S]*)\1$/mg, "$2");
97
- if (maybeQuote === '"') {
98
- value = value.replace(/\\n/g, "\n");
99
- value = value.replace(/\\r/g, "\r");
100
- }
101
- obj[key] = value;
102
- }
103
- return obj;
104
- }
105
- function _parseVault(options) {
106
- const vaultPath = _vaultPath(options);
107
- const result = DotenvModule.configDotenv({ path: vaultPath });
108
- if (!result.parsed) {
109
- throw new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`);
110
- }
111
- const keys = _dotenvKey(options).split(",");
112
- const length = keys.length;
113
- let decrypted;
114
- for (let i = 0; i < length; i++) {
115
- try {
116
- const key = keys[i].trim();
117
- const attrs = _instructions(result, key);
118
- decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key);
119
- break;
120
- } catch (error) {
121
- if (i + 1 >= length) {
122
- throw error;
123
- }
124
- }
125
- }
126
- return DotenvModule.parse(decrypted);
127
- }
128
- function _log(message) {
129
- console.log(`[dotenv@${version}][INFO] ${message}`);
130
- }
131
- function _warn(message) {
132
- console.log(`[dotenv@${version}][WARN] ${message}`);
133
- }
134
- function _debug(message) {
135
- console.log(`[dotenv@${version}][DEBUG] ${message}`);
136
- }
137
- function _dotenvKey(options) {
138
- if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
139
- return options.DOTENV_KEY;
140
- }
141
- if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
142
- return process.env.DOTENV_KEY;
143
- }
144
- return "";
145
- }
146
- function _instructions(result, dotenvKey) {
147
- let uri;
148
- try {
149
- uri = new URL(dotenvKey);
150
- } catch (error) {
151
- if (error.code === "ERR_INVALID_URL") {
152
- throw new Error("INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=development");
153
- }
154
- throw error;
155
- }
156
- const key = uri.password;
157
- if (!key) {
158
- throw new Error("INVALID_DOTENV_KEY: Missing key part");
159
- }
160
- const environment = uri.searchParams.get("environment");
161
- if (!environment) {
162
- throw new Error("INVALID_DOTENV_KEY: Missing environment part");
163
- }
164
- const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`;
165
- const ciphertext = result.parsed[environmentKey];
166
- if (!ciphertext) {
167
- throw new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`);
168
- }
169
- return { ciphertext, key };
170
- }
171
- function _vaultPath(options) {
172
- let dotenvPath = path.resolve(process.cwd(), ".env");
173
- if (options && options.path && options.path.length > 0) {
174
- dotenvPath = options.path;
175
- }
176
- return dotenvPath.endsWith(".vault") ? dotenvPath : `${dotenvPath}.vault`;
177
- }
178
- function _resolveHome(envPath) {
179
- return envPath[0] === "~" ? path.join(os.homedir(), envPath.slice(1)) : envPath;
180
- }
181
- function _configVault(options) {
182
- _log("Loading env from encrypted .env.vault");
183
- const parsed = DotenvModule._parseVault(options);
184
- let processEnv = process.env;
185
- if (options && options.processEnv != null) {
186
- processEnv = options.processEnv;
187
- }
188
- DotenvModule.populate(processEnv, parsed, options);
189
- return { parsed };
190
- }
191
- function configDotenv(options) {
192
- let dotenvPath = path.resolve(process.cwd(), ".env");
193
- let encoding = "utf8";
194
- const debug = Boolean(options && options.debug);
195
- if (options) {
196
- if (options.path != null) {
197
- dotenvPath = _resolveHome(options.path);
198
- }
199
- if (options.encoding != null) {
200
- encoding = options.encoding;
201
- }
202
- }
203
- try {
204
- const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }));
205
- let processEnv = process.env;
206
- if (options && options.processEnv != null) {
207
- processEnv = options.processEnv;
208
- }
209
- DotenvModule.populate(processEnv, parsed, options);
210
- return { parsed };
211
- } catch (e) {
212
- if (debug) {
213
- _debug(`Failed to load ${dotenvPath} ${e.message}`);
214
- }
215
- return { error: e };
216
- }
217
- }
218
- function config(options) {
219
- const vaultPath = _vaultPath(options);
220
- if (_dotenvKey(options).length === 0) {
221
- return DotenvModule.configDotenv(options);
222
- }
223
- if (!fs.existsSync(vaultPath)) {
224
- _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`);
225
- return DotenvModule.configDotenv(options);
226
- }
227
- return DotenvModule._configVault(options);
228
- }
229
- function decrypt(encrypted, keyStr) {
230
- const key = Buffer.from(keyStr.slice(-64), "hex");
231
- let ciphertext = Buffer.from(encrypted, "base64");
232
- const nonce = ciphertext.slice(0, 12);
233
- const authTag = ciphertext.slice(-16);
234
- ciphertext = ciphertext.slice(12, -16);
235
- try {
236
- const aesgcm = crypto.createDecipheriv("aes-256-gcm", key, nonce);
237
- aesgcm.setAuthTag(authTag);
238
- return `${aesgcm.update(ciphertext)}${aesgcm.final()}`;
239
- } catch (error) {
240
- const isRange = error instanceof RangeError;
241
- const invalidKeyLength = error.message === "Invalid key length";
242
- const decryptionFailed = error.message === "Unsupported state or unable to authenticate data";
243
- if (isRange || invalidKeyLength) {
244
- const msg = "INVALID_DOTENV_KEY: It must be 64 characters long (or more)";
245
- throw new Error(msg);
246
- } else if (decryptionFailed) {
247
- const msg = "DECRYPTION_FAILED: Please check your DOTENV_KEY";
248
- throw new Error(msg);
249
- } else {
250
- console.error("Error: ", error.code);
251
- console.error("Error: ", error.message);
252
- throw error;
253
- }
254
- }
255
- }
256
- function populate(processEnv, parsed, options = {}) {
257
- const debug = Boolean(options && options.debug);
258
- const override = Boolean(options && options.override);
259
- if (typeof parsed !== "object") {
260
- throw new Error("OBJECT_REQUIRED: Please check the processEnv argument being passed to populate");
261
- }
262
- for (const key of Object.keys(parsed)) {
263
- if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
264
- if (override === true) {
265
- processEnv[key] = parsed[key];
266
- }
267
- if (debug) {
268
- if (override === true) {
269
- _debug(`"${key}" is already defined and WAS overwritten`);
270
- } else {
271
- _debug(`"${key}" is already defined and was NOT overwritten`);
272
- }
273
- }
274
- } else {
275
- processEnv[key] = parsed[key];
276
- }
277
- }
278
- }
279
- var DotenvModule = {
280
- configDotenv,
281
- _configVault,
282
- _parseVault,
283
- config,
284
- decrypt,
285
- parse,
286
- populate
287
- };
288
- module.exports.configDotenv = DotenvModule.configDotenv;
289
- module.exports._configVault = DotenvModule._configVault;
290
- module.exports._parseVault = DotenvModule._parseVault;
291
- module.exports.config = DotenvModule.config;
292
- module.exports.decrypt = DotenvModule.decrypt;
293
- module.exports.parse = DotenvModule.parse;
294
- module.exports.populate = DotenvModule.populate;
295
- module.exports = DotenvModule;
296
- }
297
- });
298
- export default require_main();