@tonyclaw/agent-inspector 4.0.8 → 4.0.10
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/.output/backend/nitro.json +1 -1
- package/.output/cli.js +915 -25970
- package/.output/server/_chunks/ssr-renderer.mjs +1 -38
- package/.output/server/_libs/@opentelemetry/semantic-conventions+[...].mjs +1 -2658
- package/.output/server/_libs/agent-base.mjs +2 -234
- package/.output/server/_libs/ajv-formats.mjs +1 -330
- package/.output/server/_libs/ajv.mjs +8 -6080
- package/.output/server/_libs/anthropic-ai__sdk.mjs +19 -6021
- package/.output/server/_libs/atomically.mjs +1 -155
- package/.output/server/_libs/base64-js.mjs +1 -102
- package/.output/server/_libs/bignumber.js.mjs +1 -1353
- package/.output/server/_libs/buffer-equal-constant-time.mjs +1 -38
- package/.output/server/_libs/conf.mjs +1 -635
- package/.output/server/_libs/cookie-es.mjs +1 -44
- package/.output/server/_libs/core-util-is.mjs +1 -75
- package/.output/server/_libs/data-uri-to-buffer.mjs +1 -39
- package/.output/server/_libs/debounce-fn.mjs +1 -69
- package/.output/server/_libs/debug.mjs +5 -547
- package/.output/server/_libs/dot-prop.mjs +1 -265
- package/.output/server/_libs/ecdsa-sig-formatter.mjs +1 -162
- package/.output/server/_libs/env-paths.mjs +1 -57
- package/.output/server/_libs/extend.mjs +1 -94
- package/.output/server/_libs/fast-deep-equal.mjs +1 -38
- package/.output/server/_libs/fast-uri.mjs +1 -812
- package/.output/server/_libs/fetch-blob.mjs +1 -287
- package/.output/server/_libs/formdata-polyfill.mjs +9 -86
- package/.output/server/_libs/gaxios.mjs +5 -926
- package/.output/server/_libs/gcp-metadata.mjs +1 -322
- package/.output/server/_libs/google-auth-library.mjs +16 -6339
- package/.output/server/_libs/google-logging-utils.mjs +1 -385
- package/.output/server/_libs/google__genai.mjs +13 -18034
- package/.output/server/_libs/h3-v2.mjs +2 -287
- package/.output/server/_libs/h3.mjs +2 -408
- package/.output/server/_libs/has-flag.mjs +1 -16
- package/.output/server/_libs/https-proxy-agent.mjs +7 -280
- package/.output/server/_libs/immediate.mjs +1 -74
- package/.output/server/_libs/inherits.mjs +1 -50
- package/.output/server/_libs/isarray.mjs +1 -14
- package/.output/server/_libs/isbot.mjs +1 -20
- package/.output/server/_libs/json-bigint.mjs +8 -413
- package/.output/server/_libs/json-schema-traverse.mjs +1 -91
- package/.output/server/_libs/jszip.mjs +2 -3051
- package/.output/server/_libs/jwa.mjs +3 -233
- package/.output/server/_libs/jws.mjs +1 -298
- package/.output/server/_libs/lie.mjs +1 -273
- package/.output/server/_libs/mariozechner__pi-agent-core.mjs +1 -824
- package/.output/server/_libs/mariozechner__pi-ai.mjs +34 -22887
- package/.output/server/_libs/mimic-function.mjs +2 -47
- package/.output/server/_libs/mistralai__mistralai.mjs +7 -32045
- package/.output/server/_libs/modelcontextprotocol__core.mjs +1 -1121
- package/.output/server/_libs/modelcontextprotocol__server.mjs +13 -13205
- package/.output/server/_libs/ms.mjs +1 -120
- package/.output/server/_libs/node-domexception.mjs +1 -17
- package/.output/server/_libs/node-fetch.mjs +4 -1578
- package/.output/server/_libs/ohash.mjs +1 -1
- package/.output/server/_libs/openai.mjs +23 -6766
- package/.output/server/_libs/opentelemetry__api.mjs +1 -1678
- package/.output/server/_libs/p-retry.mjs +1 -87
- package/.output/server/_libs/pako.mjs +1 -4223
- package/.output/server/_libs/partial-json.mjs +2 -244
- package/.output/server/_libs/process-nextick-args.mjs +1 -48
- package/.output/server/_libs/react-dom.mjs +62 -10778
- package/.output/server/_libs/react.mjs +1 -519
- package/.output/server/_libs/readable-stream.mjs +1 -1518
- package/.output/server/_libs/retry.mjs +1 -236
- package/.output/server/_libs/rou3.mjs +1 -14
- package/.output/server/_libs/safe-buffer.mjs +1 -126
- package/.output/server/_libs/semver.mjs +1 -1938
- package/.output/server/_libs/seroval-plugins.mjs +1 -58
- package/.output/server/_libs/seroval.mjs +3 -1775
- package/.output/server/_libs/setimmediate.mjs +1 -152
- package/.output/server/_libs/srvx.mjs +4 -1390
- package/.output/server/_libs/stubborn-fs.mjs +1 -91
- package/.output/server/_libs/stubborn-utils.mjs +1 -66
- package/.output/server/_libs/supports-color.mjs +1 -108
- package/.output/server/_libs/tanstack__history.mjs +1 -230
- package/.output/server/_libs/tanstack__react-router.mjs +1 -848
- package/.output/server/_libs/tanstack__react-store.mjs +1 -1
- package/.output/server/_libs/tanstack__router-core.mjs +1 -4839
- package/.output/server/_libs/typebox.mjs +3 -6596
- package/.output/server/_libs/ufo.mjs +1 -54
- package/.output/server/_libs/uint8array-extras.mjs +1 -69
- package/.output/server/_libs/util-deprecate.mjs +1 -12
- package/.output/server/_libs/web-streams-polyfill.mjs +1 -4282
- package/.output/server/_libs/when-exit.mjs +1 -53
- package/.output/server/_libs/ws.mjs +7 -3715
- package/.output/server/_libs/zod-to-json-schema.mjs +5 -1207
- package/.output/server/_libs/zod.mjs +59 -9958
- package/.output/server/_ssr/empty-plugin-adapters-C3Q1xu4u.mjs +1 -0
- package/.output/server/_ssr/index.mjs +2 -1512
- package/.output/server/_ssr/publicBasePathContext--TAQS2_5.mjs +1 -0
- package/.output/server/_ssr/router-B3jax4sN.mjs +341 -0
- package/.output/server/_ssr/start-PDVGOqUN.mjs +1 -0
- package/.output/server/_tanstack-start-manifest_v-D7cKwKzM.mjs +1 -0
- package/.output/server/index.mjs +1 -320
- package/.output/ui/assets/{CompareDrawer-DafpfgaT.js → CompareDrawer-D8fg0prL.js} +1 -1
- package/.output/ui/assets/{InspectorPet-Brsmmfzx.js → InspectorPet-BAY7SAee.js} +1 -1
- package/.output/ui/assets/{ProxyViewerContainer-D2pq1fUr.js → ProxyViewerContainer-BPITPKYp.js} +4 -4
- package/.output/ui/assets/{ReplayDialog-D4mXXgT9.js → ReplayDialog-NLGOmBsc.js} +1 -1
- package/.output/ui/assets/{RequestAnatomy-Ccnsq12U.js → RequestAnatomy-DBhRhis7.js} +1 -1
- package/.output/ui/assets/{ResponseView-CJv3vB5X.js → ResponseView-DNXX-dzj.js} +1 -1
- package/.output/ui/assets/{StreamingChunkSequence-CEkLnOF6.js → StreamingChunkSequence-CLiT5Xun.js} +1 -1
- package/.output/ui/assets/{_sessionId-C7s68UH3.js → _sessionId-CY0Jj2Xv.js} +1 -1
- package/.output/ui/assets/{_sessionId-DlG-yHoI.js → _sessionId-Cf0So2vy.js} +1 -1
- package/.output/ui/assets/{index-ChZZrVHa.js → index-BIMJaHOR.js} +1 -1
- package/.output/ui/assets/{index-CFqnSirZ.js → index-DLG3AIXx.js} +1 -1
- package/.output/ui/assets/{index-B0NCZQ93.js → index-DTj5ErVh.js} +2 -2
- package/.output/ui/assets/{index-Cqh47jeU.js → index-ujyZ2AyC.js} +1 -1
- package/.output/ui/assets/{json-viewer-DvpI0vbY.js → json-viewer-BwPQ5x7s.js} +1 -1
- package/.output/ui/assets/{jszip.min-5AG8hW2c.js → jszip.min-CDwYiHmS.js} +1 -1
- package/.output/ui/index.html +1 -1
- package/.output/workers/logFinalizer.worker.js +41 -17739
- package/.output/workers/sessionWorkerEntry.js +41 -17735
- package/package.json +7 -7
- package/.output/server/_ssr/empty-plugin-adapters-BFgPZ6_d.mjs +0 -6
- package/.output/server/_ssr/publicBasePathContext-IH2ilOfu.mjs +0 -176
- package/.output/server/_ssr/router-BDyF-OMB.mjs +0 -27187
- package/.output/server/_ssr/start-COQu_AgD.mjs +0 -57
- package/.output/server/_tanstack-start-manifest_v-ClTaMkEj.mjs +0 -4
|
@@ -1,38 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
var bufferEqualConstantTime;
|
|
3
|
-
var hasRequiredBufferEqualConstantTime;
|
|
4
|
-
function requireBufferEqualConstantTime() {
|
|
5
|
-
if (hasRequiredBufferEqualConstantTime) return bufferEqualConstantTime;
|
|
6
|
-
hasRequiredBufferEqualConstantTime = 1;
|
|
7
|
-
var Buffer = require$$0.Buffer;
|
|
8
|
-
var SlowBuffer = require$$0.SlowBuffer;
|
|
9
|
-
bufferEqualConstantTime = bufferEq;
|
|
10
|
-
function bufferEq(a, b) {
|
|
11
|
-
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
if (a.length !== b.length) {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
var c = 0;
|
|
18
|
-
for (var i = 0; i < a.length; i++) {
|
|
19
|
-
c |= a[i] ^ b[i];
|
|
20
|
-
}
|
|
21
|
-
return c === 0;
|
|
22
|
-
}
|
|
23
|
-
bufferEq.install = function() {
|
|
24
|
-
Buffer.prototype.equal = SlowBuffer.prototype.equal = function equal(that) {
|
|
25
|
-
return bufferEq(this, that);
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
var origBufEqual = Buffer.prototype.equal;
|
|
29
|
-
var origSlowBufEqual = SlowBuffer.prototype.equal;
|
|
30
|
-
bufferEq.restore = function() {
|
|
31
|
-
Buffer.prototype.equal = origBufEqual;
|
|
32
|
-
SlowBuffer.prototype.equal = origSlowBufEqual;
|
|
33
|
-
};
|
|
34
|
-
return bufferEqualConstantTime;
|
|
35
|
-
}
|
|
36
|
-
export {
|
|
37
|
-
requireBufferEqualConstantTime as r
|
|
38
|
-
};
|
|
1
|
+
import l from"buffer";var n,i;function B(){if(i)return n;i=1;var r=l.Buffer,o=l.SlowBuffer;n=t;function t(e,u){if(!r.isBuffer(e)||!r.isBuffer(u)||e.length!==u.length)return!1;for(var a=0,f=0;f<e.length;f++)a|=e[f]^u[f];return a===0}t.install=function(){r.prototype.equal=o.prototype.equal=function(u){return t(this,u)}};var q=r.prototype.equal,p=o.prototype.equal;return t.restore=function(){r.prototype.equal=q,o.prototype.equal=p},n}export{B as r};
|
|
@@ -1,635 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import process from "node:process";
|
|
3
|
-
import fs from "node:fs";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import crypto from "node:crypto";
|
|
6
|
-
import assert from "node:assert";
|
|
7
|
-
import { h as hasProperty, d as deleteProperty, s as setProperty, g as getProperty } from "./dot-prop.mjs";
|
|
8
|
-
import { e as envPaths } from "./env-paths.mjs";
|
|
9
|
-
import { w as writeFileSync } from "./atomically.mjs";
|
|
10
|
-
import { _ as _2020Exports } from "./ajv.mjs";
|
|
11
|
-
import { a as ajvFormatsModule } from "./ajv-formats.mjs";
|
|
12
|
-
import { d as debounceFunction } from "./debounce-fn.mjs";
|
|
13
|
-
import { s as semver } from "./semver.mjs";
|
|
14
|
-
import { u as uint8ArrayToString, s as stringToUint8Array, c as concatUint8Arrays } from "./uint8array-extras.mjs";
|
|
15
|
-
const defaultEncryptionAlgorithm = "aes-256-cbc";
|
|
16
|
-
const supportedEncryptionAlgorithms = /* @__PURE__ */ new Set([
|
|
17
|
-
"aes-256-cbc",
|
|
18
|
-
"aes-256-gcm",
|
|
19
|
-
"aes-256-ctr"
|
|
20
|
-
]);
|
|
21
|
-
const isSupportedEncryptionAlgorithm = (value) => typeof value === "string" && supportedEncryptionAlgorithms.has(value);
|
|
22
|
-
const createPlainObject = () => /* @__PURE__ */ Object.create(null);
|
|
23
|
-
const isExist = (data) => data !== void 0;
|
|
24
|
-
const checkValueType = (key, value) => {
|
|
25
|
-
const nonJsonTypes = /* @__PURE__ */ new Set([
|
|
26
|
-
"undefined",
|
|
27
|
-
"symbol",
|
|
28
|
-
"function"
|
|
29
|
-
]);
|
|
30
|
-
const type = typeof value;
|
|
31
|
-
if (nonJsonTypes.has(type)) {
|
|
32
|
-
throw new TypeError(`Setting a value of type \`${type}\` for key \`${key}\` is not allowed as it's not supported by JSON`);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
const INTERNAL_KEY = "__internal__";
|
|
36
|
-
const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`;
|
|
37
|
-
class Conf {
|
|
38
|
-
path;
|
|
39
|
-
events;
|
|
40
|
-
#validator;
|
|
41
|
-
#encryptionKey;
|
|
42
|
-
#encryptionAlgorithm;
|
|
43
|
-
#options;
|
|
44
|
-
#defaultValues = {};
|
|
45
|
-
#isInMigration = false;
|
|
46
|
-
#watcher;
|
|
47
|
-
#watchFile;
|
|
48
|
-
#debouncedChangeHandler;
|
|
49
|
-
constructor(partialOptions = {}) {
|
|
50
|
-
const options = this.#prepareOptions(partialOptions);
|
|
51
|
-
this.#options = options;
|
|
52
|
-
this.#setupValidator(options);
|
|
53
|
-
this.#applyDefaultValues(options);
|
|
54
|
-
this.#configureSerialization(options);
|
|
55
|
-
this.events = new EventTarget();
|
|
56
|
-
this.#encryptionKey = options.encryptionKey;
|
|
57
|
-
this.#encryptionAlgorithm = options.encryptionAlgorithm ?? defaultEncryptionAlgorithm;
|
|
58
|
-
this.path = this.#resolvePath(options);
|
|
59
|
-
this.#initializeStore(options);
|
|
60
|
-
if (options.watch) {
|
|
61
|
-
this._watch();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
get(key, defaultValue) {
|
|
65
|
-
if (this.#options.accessPropertiesByDotNotation) {
|
|
66
|
-
return this._get(key, defaultValue);
|
|
67
|
-
}
|
|
68
|
-
const { store } = this;
|
|
69
|
-
return key in store ? store[key] : defaultValue;
|
|
70
|
-
}
|
|
71
|
-
set(key, value) {
|
|
72
|
-
if (typeof key !== "string" && typeof key !== "object") {
|
|
73
|
-
throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`);
|
|
74
|
-
}
|
|
75
|
-
if (typeof key !== "object" && value === void 0) {
|
|
76
|
-
throw new TypeError("Use `delete()` to clear values");
|
|
77
|
-
}
|
|
78
|
-
if (this._containsReservedKey(key)) {
|
|
79
|
-
throw new TypeError(`Please don't use the ${INTERNAL_KEY} key, as it's used to manage this module internal operations.`);
|
|
80
|
-
}
|
|
81
|
-
const { store } = this;
|
|
82
|
-
const set = (key2, value2) => {
|
|
83
|
-
checkValueType(key2, value2);
|
|
84
|
-
if (this.#options.accessPropertiesByDotNotation) {
|
|
85
|
-
setProperty(store, key2, value2);
|
|
86
|
-
} else {
|
|
87
|
-
if (key2 === "__proto__" || key2 === "constructor" || key2 === "prototype") {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
store[key2] = value2;
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
if (typeof key === "object") {
|
|
94
|
-
const object = key;
|
|
95
|
-
for (const [key2, value2] of Object.entries(object)) {
|
|
96
|
-
set(key2, value2);
|
|
97
|
-
}
|
|
98
|
-
} else {
|
|
99
|
-
set(key, value);
|
|
100
|
-
}
|
|
101
|
-
this.store = store;
|
|
102
|
-
}
|
|
103
|
-
has(key) {
|
|
104
|
-
if (this.#options.accessPropertiesByDotNotation) {
|
|
105
|
-
return hasProperty(this.store, key);
|
|
106
|
-
}
|
|
107
|
-
return key in this.store;
|
|
108
|
-
}
|
|
109
|
-
appendToArray(key, value) {
|
|
110
|
-
checkValueType(key, value);
|
|
111
|
-
const array = this.#options.accessPropertiesByDotNotation ? this._get(key, []) : key in this.store ? this.store[key] : [];
|
|
112
|
-
if (!Array.isArray(array)) {
|
|
113
|
-
throw new TypeError(`The key \`${key}\` is already set to a non-array value`);
|
|
114
|
-
}
|
|
115
|
-
this.set(key, [...array, value]);
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
Reset items to their default values, as defined by the `defaults` or `schema` option.
|
|
119
|
-
|
|
120
|
-
@see `clear()` to reset all items.
|
|
121
|
-
|
|
122
|
-
@param keys - The keys of the items to reset.
|
|
123
|
-
*/
|
|
124
|
-
reset(...keys) {
|
|
125
|
-
for (const key of keys) {
|
|
126
|
-
if (isExist(this.#defaultValues[key])) {
|
|
127
|
-
this.set(key, this.#defaultValues[key]);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
delete(key) {
|
|
132
|
-
const { store } = this;
|
|
133
|
-
if (this.#options.accessPropertiesByDotNotation) {
|
|
134
|
-
deleteProperty(store, key);
|
|
135
|
-
} else {
|
|
136
|
-
delete store[key];
|
|
137
|
-
}
|
|
138
|
-
this.store = store;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
Delete all items.
|
|
142
|
-
|
|
143
|
-
This resets known items to their default values, if defined by the `defaults` or `schema` option.
|
|
144
|
-
*/
|
|
145
|
-
clear() {
|
|
146
|
-
const newStore = createPlainObject();
|
|
147
|
-
for (const key of Object.keys(this.#defaultValues)) {
|
|
148
|
-
if (isExist(this.#defaultValues[key])) {
|
|
149
|
-
checkValueType(key, this.#defaultValues[key]);
|
|
150
|
-
if (this.#options.accessPropertiesByDotNotation) {
|
|
151
|
-
setProperty(newStore, key, this.#defaultValues[key]);
|
|
152
|
-
} else {
|
|
153
|
-
newStore[key] = this.#defaultValues[key];
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
this.store = newStore;
|
|
158
|
-
}
|
|
159
|
-
onDidChange(key, callback) {
|
|
160
|
-
if (typeof key !== "string") {
|
|
161
|
-
throw new TypeError(`Expected \`key\` to be of type \`string\`, got ${typeof key}`);
|
|
162
|
-
}
|
|
163
|
-
if (typeof callback !== "function") {
|
|
164
|
-
throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`);
|
|
165
|
-
}
|
|
166
|
-
return this._handleValueChange(() => this.get(key), callback);
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
Watches the whole config object, calling `callback` on any changes.
|
|
170
|
-
|
|
171
|
-
@param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`.
|
|
172
|
-
@returns A function, that when called, will unsubscribe.
|
|
173
|
-
*/
|
|
174
|
-
onDidAnyChange(callback) {
|
|
175
|
-
if (typeof callback !== "function") {
|
|
176
|
-
throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`);
|
|
177
|
-
}
|
|
178
|
-
return this._handleStoreChange(callback);
|
|
179
|
-
}
|
|
180
|
-
get size() {
|
|
181
|
-
const entries = Object.keys(this.store);
|
|
182
|
-
return entries.filter((key) => !this._isReservedKeyPath(key)).length;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
Get all the config as an object or replace the current config with an object.
|
|
186
|
-
|
|
187
|
-
@example
|
|
188
|
-
```
|
|
189
|
-
console.log(config.store);
|
|
190
|
-
//=> {name: 'John', age: 30}
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
@example
|
|
194
|
-
```
|
|
195
|
-
config.store = {
|
|
196
|
-
hello: 'world'
|
|
197
|
-
};
|
|
198
|
-
```
|
|
199
|
-
*/
|
|
200
|
-
get store() {
|
|
201
|
-
try {
|
|
202
|
-
const data = fs.readFileSync(this.path, this.#encryptionKey ? null : "utf8");
|
|
203
|
-
const dataString = this._decryptData(data);
|
|
204
|
-
const parseStore = (value) => {
|
|
205
|
-
const deserializedData = this._deserialize(value);
|
|
206
|
-
if (!this.#isInMigration) {
|
|
207
|
-
this._validate(deserializedData);
|
|
208
|
-
}
|
|
209
|
-
return Object.assign(createPlainObject(), deserializedData);
|
|
210
|
-
};
|
|
211
|
-
return parseStore(dataString);
|
|
212
|
-
} catch (error) {
|
|
213
|
-
if (error?.code === "ENOENT") {
|
|
214
|
-
this._ensureDirectory();
|
|
215
|
-
return createPlainObject();
|
|
216
|
-
}
|
|
217
|
-
if (this.#options.clearInvalidConfig) {
|
|
218
|
-
const errorInstance = error;
|
|
219
|
-
if (errorInstance.name === "SyntaxError") {
|
|
220
|
-
return createPlainObject();
|
|
221
|
-
}
|
|
222
|
-
if (errorInstance.message?.startsWith("Config schema violation:")) {
|
|
223
|
-
return createPlainObject();
|
|
224
|
-
}
|
|
225
|
-
if (errorInstance.message === "Failed to decrypt config data.") {
|
|
226
|
-
return createPlainObject();
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
throw error;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
set store(value) {
|
|
233
|
-
this._ensureDirectory();
|
|
234
|
-
if (!hasProperty(value, INTERNAL_KEY)) {
|
|
235
|
-
try {
|
|
236
|
-
const data = fs.readFileSync(this.path, this.#encryptionKey ? null : "utf8");
|
|
237
|
-
const dataString = this._decryptData(data);
|
|
238
|
-
const currentStore = this._deserialize(dataString);
|
|
239
|
-
if (hasProperty(currentStore, INTERNAL_KEY)) {
|
|
240
|
-
setProperty(value, INTERNAL_KEY, getProperty(currentStore, INTERNAL_KEY));
|
|
241
|
-
}
|
|
242
|
-
} catch {
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
if (!this.#isInMigration) {
|
|
246
|
-
this._validate(value);
|
|
247
|
-
}
|
|
248
|
-
this._write(value);
|
|
249
|
-
this.events.dispatchEvent(new Event("change"));
|
|
250
|
-
}
|
|
251
|
-
*[Symbol.iterator]() {
|
|
252
|
-
for (const [key, value] of Object.entries(this.store)) {
|
|
253
|
-
if (!this._isReservedKeyPath(key)) {
|
|
254
|
-
yield [key, value];
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
Close the file watcher if one exists. This is useful in tests to prevent the process from hanging.
|
|
260
|
-
*/
|
|
261
|
-
_closeWatcher() {
|
|
262
|
-
if (this.#watcher) {
|
|
263
|
-
this.#watcher.close();
|
|
264
|
-
this.#watcher = void 0;
|
|
265
|
-
}
|
|
266
|
-
if (this.#watchFile) {
|
|
267
|
-
fs.unwatchFile(this.path);
|
|
268
|
-
this.#watchFile = false;
|
|
269
|
-
}
|
|
270
|
-
this.#debouncedChangeHandler = void 0;
|
|
271
|
-
}
|
|
272
|
-
_decryptData(data) {
|
|
273
|
-
const encryptionKey = this.#encryptionKey;
|
|
274
|
-
if (!encryptionKey) {
|
|
275
|
-
return typeof data === "string" ? data : uint8ArrayToString(data);
|
|
276
|
-
}
|
|
277
|
-
const encryptionAlgorithm = this.#encryptionAlgorithm;
|
|
278
|
-
const authenticationTagLength = encryptionAlgorithm === "aes-256-gcm" ? 16 : 0;
|
|
279
|
-
const separatorCodePoint = ":".codePointAt(0);
|
|
280
|
-
const separatorByte = typeof data === "string" ? data.codePointAt(16) : data[16];
|
|
281
|
-
const hasSeparator = separatorCodePoint !== void 0 && separatorByte === separatorCodePoint;
|
|
282
|
-
if (!hasSeparator) {
|
|
283
|
-
if (encryptionAlgorithm === "aes-256-cbc") {
|
|
284
|
-
return typeof data === "string" ? data : uint8ArrayToString(data);
|
|
285
|
-
}
|
|
286
|
-
throw new Error("Failed to decrypt config data.");
|
|
287
|
-
}
|
|
288
|
-
const getEncryptedPayload = (dataUpdate2) => {
|
|
289
|
-
if (authenticationTagLength === 0) {
|
|
290
|
-
return { ciphertext: dataUpdate2 };
|
|
291
|
-
}
|
|
292
|
-
const authenticationTagStart = dataUpdate2.length - authenticationTagLength;
|
|
293
|
-
if (authenticationTagStart < 0) {
|
|
294
|
-
throw new Error("Invalid authentication tag length.");
|
|
295
|
-
}
|
|
296
|
-
return {
|
|
297
|
-
ciphertext: dataUpdate2.slice(0, authenticationTagStart),
|
|
298
|
-
authenticationTag: dataUpdate2.slice(authenticationTagStart)
|
|
299
|
-
};
|
|
300
|
-
};
|
|
301
|
-
const initializationVector = data.slice(0, 16);
|
|
302
|
-
const slice = data.slice(17);
|
|
303
|
-
const dataUpdate = typeof slice === "string" ? stringToUint8Array(slice) : slice;
|
|
304
|
-
const decrypt = (salt) => {
|
|
305
|
-
const { ciphertext, authenticationTag } = getEncryptedPayload(dataUpdate);
|
|
306
|
-
const password = crypto.pbkdf2Sync(encryptionKey, salt, 1e4, 32, "sha512");
|
|
307
|
-
const decipher = crypto.createDecipheriv(encryptionAlgorithm, password, initializationVector);
|
|
308
|
-
if (authenticationTag) {
|
|
309
|
-
decipher.setAuthTag(authenticationTag);
|
|
310
|
-
}
|
|
311
|
-
return uint8ArrayToString(concatUint8Arrays([decipher.update(ciphertext), decipher.final()]));
|
|
312
|
-
};
|
|
313
|
-
try {
|
|
314
|
-
return decrypt(initializationVector);
|
|
315
|
-
} catch {
|
|
316
|
-
try {
|
|
317
|
-
return decrypt(initializationVector.toString());
|
|
318
|
-
} catch {
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
if (encryptionAlgorithm === "aes-256-cbc") {
|
|
322
|
-
return typeof data === "string" ? data : uint8ArrayToString(data);
|
|
323
|
-
}
|
|
324
|
-
throw new Error("Failed to decrypt config data.");
|
|
325
|
-
}
|
|
326
|
-
_handleStoreChange(callback) {
|
|
327
|
-
let currentValue = this.store;
|
|
328
|
-
const onChange = () => {
|
|
329
|
-
const oldValue = currentValue;
|
|
330
|
-
const newValue = this.store;
|
|
331
|
-
if (isDeepStrictEqual(newValue, oldValue)) {
|
|
332
|
-
return;
|
|
333
|
-
}
|
|
334
|
-
currentValue = newValue;
|
|
335
|
-
callback.call(this, newValue, oldValue);
|
|
336
|
-
};
|
|
337
|
-
this.events.addEventListener("change", onChange);
|
|
338
|
-
return () => {
|
|
339
|
-
this.events.removeEventListener("change", onChange);
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
_handleValueChange(getter, callback) {
|
|
343
|
-
let currentValue = getter();
|
|
344
|
-
const onChange = () => {
|
|
345
|
-
const oldValue = currentValue;
|
|
346
|
-
const newValue = getter();
|
|
347
|
-
if (isDeepStrictEqual(newValue, oldValue)) {
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
currentValue = newValue;
|
|
351
|
-
callback.call(this, newValue, oldValue);
|
|
352
|
-
};
|
|
353
|
-
this.events.addEventListener("change", onChange);
|
|
354
|
-
return () => {
|
|
355
|
-
this.events.removeEventListener("change", onChange);
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
_deserialize = (value) => JSON.parse(value);
|
|
359
|
-
_serialize = (value) => JSON.stringify(value, void 0, " ");
|
|
360
|
-
_validate(data) {
|
|
361
|
-
if (!this.#validator) {
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
const valid = this.#validator(data);
|
|
365
|
-
if (valid || !this.#validator.errors) {
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
const errors = this.#validator.errors.map(({ instancePath, message = "" }) => `\`${instancePath.slice(1)}\` ${message}`);
|
|
369
|
-
throw new Error("Config schema violation: " + errors.join("; "));
|
|
370
|
-
}
|
|
371
|
-
_ensureDirectory() {
|
|
372
|
-
fs.mkdirSync(path.dirname(this.path), { recursive: true });
|
|
373
|
-
}
|
|
374
|
-
_write(value) {
|
|
375
|
-
let data = this._serialize(value);
|
|
376
|
-
const encryptionKey = this.#encryptionKey;
|
|
377
|
-
if (encryptionKey) {
|
|
378
|
-
const initializationVector = crypto.randomBytes(16);
|
|
379
|
-
const password = crypto.pbkdf2Sync(encryptionKey, initializationVector, 1e4, 32, "sha512");
|
|
380
|
-
const cipher = crypto.createCipheriv(this.#encryptionAlgorithm, password, initializationVector);
|
|
381
|
-
const encryptedData = concatUint8Arrays([cipher.update(stringToUint8Array(data)), cipher.final()]);
|
|
382
|
-
const encryptedParts = [initializationVector, stringToUint8Array(":"), encryptedData];
|
|
383
|
-
if (this.#encryptionAlgorithm === "aes-256-gcm") {
|
|
384
|
-
encryptedParts.push(cipher.getAuthTag());
|
|
385
|
-
}
|
|
386
|
-
data = concatUint8Arrays(encryptedParts);
|
|
387
|
-
}
|
|
388
|
-
if (process.env.SNAP) {
|
|
389
|
-
fs.writeFileSync(this.path, data, { mode: this.#options.configFileMode });
|
|
390
|
-
} else {
|
|
391
|
-
try {
|
|
392
|
-
writeFileSync(this.path, data, { mode: this.#options.configFileMode });
|
|
393
|
-
} catch (error) {
|
|
394
|
-
if (error?.code === "EXDEV") {
|
|
395
|
-
fs.writeFileSync(this.path, data, { mode: this.#options.configFileMode });
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
throw error;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
_watch() {
|
|
403
|
-
this._ensureDirectory();
|
|
404
|
-
if (!fs.existsSync(this.path)) {
|
|
405
|
-
this._write(createPlainObject());
|
|
406
|
-
}
|
|
407
|
-
if (process.platform === "win32" || process.platform === "darwin") {
|
|
408
|
-
this.#debouncedChangeHandler ??= debounceFunction(() => {
|
|
409
|
-
this.events.dispatchEvent(new Event("change"));
|
|
410
|
-
}, { wait: 100 });
|
|
411
|
-
const directory = path.dirname(this.path);
|
|
412
|
-
const basename = path.basename(this.path);
|
|
413
|
-
this.#watcher = fs.watch(directory, { persistent: false, encoding: "utf8" }, (_eventType, filename) => {
|
|
414
|
-
if (filename && filename !== basename) {
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
if (typeof this.#debouncedChangeHandler === "function") {
|
|
418
|
-
this.#debouncedChangeHandler();
|
|
419
|
-
}
|
|
420
|
-
});
|
|
421
|
-
} else {
|
|
422
|
-
this.#debouncedChangeHandler ??= debounceFunction(() => {
|
|
423
|
-
this.events.dispatchEvent(new Event("change"));
|
|
424
|
-
}, { wait: 1e3 });
|
|
425
|
-
fs.watchFile(this.path, { persistent: false }, (_current, _previous) => {
|
|
426
|
-
if (typeof this.#debouncedChangeHandler === "function") {
|
|
427
|
-
this.#debouncedChangeHandler();
|
|
428
|
-
}
|
|
429
|
-
});
|
|
430
|
-
this.#watchFile = true;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
_migrate(migrations, versionToMigrate, beforeEachMigration) {
|
|
434
|
-
let previousMigratedVersion = this._get(MIGRATION_KEY, "0.0.0");
|
|
435
|
-
const newerVersions = Object.keys(migrations).filter((candidateVersion) => this._shouldPerformMigration(candidateVersion, previousMigratedVersion, versionToMigrate));
|
|
436
|
-
let storeBackup = structuredClone(this.store);
|
|
437
|
-
for (const version of newerVersions) {
|
|
438
|
-
try {
|
|
439
|
-
if (beforeEachMigration) {
|
|
440
|
-
beforeEachMigration(this, {
|
|
441
|
-
fromVersion: previousMigratedVersion,
|
|
442
|
-
toVersion: version,
|
|
443
|
-
finalVersion: versionToMigrate,
|
|
444
|
-
versions: newerVersions
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
const migration = migrations[version];
|
|
448
|
-
migration?.(this);
|
|
449
|
-
this._set(MIGRATION_KEY, version);
|
|
450
|
-
previousMigratedVersion = version;
|
|
451
|
-
storeBackup = structuredClone(this.store);
|
|
452
|
-
} catch (error) {
|
|
453
|
-
this.store = storeBackup;
|
|
454
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
455
|
-
throw new Error(`Something went wrong during the migration! Changes applied to the store until this failed migration will be restored. ${errorMessage}`);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
if (this._isVersionInRangeFormat(previousMigratedVersion) || !semver.eq(previousMigratedVersion, versionToMigrate)) {
|
|
459
|
-
this._set(MIGRATION_KEY, versionToMigrate);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
_containsReservedKey(key) {
|
|
463
|
-
if (typeof key === "string") {
|
|
464
|
-
return this._isReservedKeyPath(key);
|
|
465
|
-
}
|
|
466
|
-
if (!key || typeof key !== "object") {
|
|
467
|
-
return false;
|
|
468
|
-
}
|
|
469
|
-
return this._objectContainsReservedKey(key);
|
|
470
|
-
}
|
|
471
|
-
_objectContainsReservedKey(value) {
|
|
472
|
-
if (!value || typeof value !== "object") {
|
|
473
|
-
return false;
|
|
474
|
-
}
|
|
475
|
-
for (const [candidateKey, candidateValue] of Object.entries(value)) {
|
|
476
|
-
if (this._isReservedKeyPath(candidateKey)) {
|
|
477
|
-
return true;
|
|
478
|
-
}
|
|
479
|
-
if (this._objectContainsReservedKey(candidateValue)) {
|
|
480
|
-
return true;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
return false;
|
|
484
|
-
}
|
|
485
|
-
_isReservedKeyPath(candidate) {
|
|
486
|
-
return candidate === INTERNAL_KEY || candidate.startsWith(`${INTERNAL_KEY}.`);
|
|
487
|
-
}
|
|
488
|
-
_isVersionInRangeFormat(version) {
|
|
489
|
-
return semver.clean(version) === null;
|
|
490
|
-
}
|
|
491
|
-
_shouldPerformMigration(candidateVersion, previousMigratedVersion, versionToMigrate) {
|
|
492
|
-
if (this._isVersionInRangeFormat(candidateVersion)) {
|
|
493
|
-
if (previousMigratedVersion !== "0.0.0" && semver.satisfies(previousMigratedVersion, candidateVersion)) {
|
|
494
|
-
return false;
|
|
495
|
-
}
|
|
496
|
-
return semver.satisfies(versionToMigrate, candidateVersion);
|
|
497
|
-
}
|
|
498
|
-
if (semver.lte(candidateVersion, previousMigratedVersion)) {
|
|
499
|
-
return false;
|
|
500
|
-
}
|
|
501
|
-
if (semver.gt(candidateVersion, versionToMigrate)) {
|
|
502
|
-
return false;
|
|
503
|
-
}
|
|
504
|
-
return true;
|
|
505
|
-
}
|
|
506
|
-
_get(key, defaultValue) {
|
|
507
|
-
return getProperty(this.store, key, defaultValue);
|
|
508
|
-
}
|
|
509
|
-
_set(key, value) {
|
|
510
|
-
const { store } = this;
|
|
511
|
-
setProperty(store, key, value);
|
|
512
|
-
this.store = store;
|
|
513
|
-
}
|
|
514
|
-
#prepareOptions(partialOptions) {
|
|
515
|
-
const options = {
|
|
516
|
-
configName: "config",
|
|
517
|
-
fileExtension: "json",
|
|
518
|
-
projectSuffix: "nodejs",
|
|
519
|
-
clearInvalidConfig: false,
|
|
520
|
-
accessPropertiesByDotNotation: true,
|
|
521
|
-
configFileMode: 438,
|
|
522
|
-
...partialOptions
|
|
523
|
-
};
|
|
524
|
-
options.encryptionAlgorithm ??= defaultEncryptionAlgorithm;
|
|
525
|
-
if (!isSupportedEncryptionAlgorithm(options.encryptionAlgorithm)) {
|
|
526
|
-
throw new TypeError(`The \`encryptionAlgorithm\` option must be one of: ${[...supportedEncryptionAlgorithms].join(", ")}`);
|
|
527
|
-
}
|
|
528
|
-
if (!options.cwd) {
|
|
529
|
-
if (!options.projectName) {
|
|
530
|
-
throw new Error("Please specify the `projectName` option.");
|
|
531
|
-
}
|
|
532
|
-
options.cwd = envPaths(options.projectName, { suffix: options.projectSuffix }).config;
|
|
533
|
-
}
|
|
534
|
-
if (typeof options.fileExtension === "string") {
|
|
535
|
-
options.fileExtension = options.fileExtension.replace(/^\.+/, "");
|
|
536
|
-
}
|
|
537
|
-
return options;
|
|
538
|
-
}
|
|
539
|
-
#setupValidator(options) {
|
|
540
|
-
if (!(options.schema ?? options.ajvOptions ?? options.rootSchema)) {
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
if (options.schema && typeof options.schema !== "object") {
|
|
544
|
-
throw new TypeError("The `schema` option must be an object.");
|
|
545
|
-
}
|
|
546
|
-
const ajvFormats = ajvFormatsModule.default;
|
|
547
|
-
const ajv = new _2020Exports.Ajv2020({
|
|
548
|
-
allErrors: true,
|
|
549
|
-
useDefaults: true,
|
|
550
|
-
...options.ajvOptions
|
|
551
|
-
});
|
|
552
|
-
ajvFormats(ajv);
|
|
553
|
-
const schema = {
|
|
554
|
-
...options.rootSchema,
|
|
555
|
-
type: "object",
|
|
556
|
-
properties: options.schema
|
|
557
|
-
};
|
|
558
|
-
this.#validator = ajv.compile(schema);
|
|
559
|
-
this.#captureSchemaDefaults(options.schema);
|
|
560
|
-
}
|
|
561
|
-
#captureSchemaDefaults(schemaConfig) {
|
|
562
|
-
const schemaEntries = Object.entries(schemaConfig ?? {});
|
|
563
|
-
for (const [key, schemaDefinition] of schemaEntries) {
|
|
564
|
-
if (!schemaDefinition || typeof schemaDefinition !== "object") {
|
|
565
|
-
continue;
|
|
566
|
-
}
|
|
567
|
-
if (!Object.hasOwn(schemaDefinition, "default")) {
|
|
568
|
-
continue;
|
|
569
|
-
}
|
|
570
|
-
const { default: defaultValue } = schemaDefinition;
|
|
571
|
-
if (defaultValue === void 0) {
|
|
572
|
-
continue;
|
|
573
|
-
}
|
|
574
|
-
this.#defaultValues[key] = defaultValue;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
#applyDefaultValues(options) {
|
|
578
|
-
if (options.defaults) {
|
|
579
|
-
Object.assign(this.#defaultValues, options.defaults);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
#configureSerialization(options) {
|
|
583
|
-
if (options.serialize) {
|
|
584
|
-
this._serialize = options.serialize;
|
|
585
|
-
}
|
|
586
|
-
if (options.deserialize) {
|
|
587
|
-
this._deserialize = options.deserialize;
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
#resolvePath(options) {
|
|
591
|
-
const normalizedFileExtension = typeof options.fileExtension === "string" ? options.fileExtension : void 0;
|
|
592
|
-
const fileExtension = normalizedFileExtension ? `.${normalizedFileExtension}` : "";
|
|
593
|
-
return path.resolve(options.cwd, `${options.configName ?? "config"}${fileExtension}`);
|
|
594
|
-
}
|
|
595
|
-
#initializeStore(options) {
|
|
596
|
-
if (options.migrations) {
|
|
597
|
-
this.#runMigrations(options);
|
|
598
|
-
this._validate(this.store);
|
|
599
|
-
return;
|
|
600
|
-
}
|
|
601
|
-
const fileStore = this.store;
|
|
602
|
-
const storeWithDefaults = Object.assign(createPlainObject(), options.defaults ?? {}, fileStore);
|
|
603
|
-
this._validate(storeWithDefaults);
|
|
604
|
-
try {
|
|
605
|
-
assert.deepEqual(fileStore, storeWithDefaults);
|
|
606
|
-
} catch {
|
|
607
|
-
this.store = storeWithDefaults;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
#runMigrations(options) {
|
|
611
|
-
const { migrations, projectVersion } = options;
|
|
612
|
-
if (!migrations) {
|
|
613
|
-
return;
|
|
614
|
-
}
|
|
615
|
-
if (!projectVersion) {
|
|
616
|
-
throw new Error("Please specify the `projectVersion` option.");
|
|
617
|
-
}
|
|
618
|
-
this.#isInMigration = true;
|
|
619
|
-
try {
|
|
620
|
-
const fileStore = this.store;
|
|
621
|
-
const storeWithDefaults = Object.assign(createPlainObject(), options.defaults ?? {}, fileStore);
|
|
622
|
-
try {
|
|
623
|
-
assert.deepEqual(fileStore, storeWithDefaults);
|
|
624
|
-
} catch {
|
|
625
|
-
this._write(storeWithDefaults);
|
|
626
|
-
}
|
|
627
|
-
this._migrate(migrations, projectVersion, options.beforeEachMigration);
|
|
628
|
-
} finally {
|
|
629
|
-
this.#isInMigration = false;
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
export {
|
|
634
|
-
Conf as C
|
|
635
|
-
};
|
|
1
|
+
import{isDeepStrictEqual as C}from"node:util";import b from"node:process";import c from"node:fs";import m from"node:path";import g from"node:crypto";import O from"node:assert";import{h as j,d as I,s as w,g as F}from"./dot-prop.mjs";import{e as B}from"./env-paths.mjs";import{w as L}from"./atomically.mjs";import{_ as M}from"./ajv.mjs";import{a as W}from"./ajv-formats.mjs";import{d as N}from"./debounce-fn.mjs";import{s as u}from"./semver.mjs";import{u as _,s as S,c as P}from"./uint8array-extras.mjs";const V="aes-256-cbc",K=new Set(["aes-256-cbc","aes-256-gcm","aes-256-ctr"]),q=l=>typeof l=="string"&&K.has(l),h=()=>Object.create(null),z=l=>l!==void 0,D=(l,t)=>{const e=new Set(["undefined","symbol","function"]),r=typeof t;if(e.has(r))throw new TypeError(`Setting a value of type \`${r}\` for key \`${l}\` is not allowed as it's not supported by JSON`)},f="__internal__",T=`${f}.migrations.version`;class ot{path;events;#i;#s;#o;#t;#e={};#n=!1;#a;#c;#r;constructor(t={}){const e=this.#h(t);this.#t=e,this.#f(e),this.#p(e),this.#u(e),this.events=new EventTarget,this.#s=e.encryptionKey,this.#o=e.encryptionAlgorithm??V,this.path=this.#d(e),this.#y(e),e.watch&&this._watch()}get(t,e){if(this.#t.accessPropertiesByDotNotation)return this._get(t,e);const{store:r}=this;return t in r?r[t]:e}set(t,e){if(typeof t!="string"&&typeof t!="object")throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof t}`);if(typeof t!="object"&&e===void 0)throw new TypeError("Use `delete()` to clear values");if(this._containsReservedKey(t))throw new TypeError(`Please don't use the ${f} key, as it's used to manage this module internal operations.`);const{store:r}=this,i=(s,o)=>{if(D(s,o),this.#t.accessPropertiesByDotNotation)w(r,s,o);else{if(s==="__proto__"||s==="constructor"||s==="prototype")return;r[s]=o}};if(typeof t=="object"){const s=t;for(const[o,n]of Object.entries(s))i(o,n)}else i(t,e);this.store=r}has(t){return this.#t.accessPropertiesByDotNotation?j(this.store,t):t in this.store}appendToArray(t,e){D(t,e);const r=this.#t.accessPropertiesByDotNotation?this._get(t,[]):t in this.store?this.store[t]:[];if(!Array.isArray(r))throw new TypeError(`The key \`${t}\` is already set to a non-array value`);this.set(t,[...r,e])}reset(...t){for(const e of t)z(this.#e[e])&&this.set(e,this.#e[e])}delete(t){const{store:e}=this;this.#t.accessPropertiesByDotNotation?I(e,t):delete e[t],this.store=e}clear(){const t=h();for(const e of Object.keys(this.#e))z(this.#e[e])&&(D(e,this.#e[e]),this.#t.accessPropertiesByDotNotation?w(t,e,this.#e[e]):t[e]=this.#e[e]);this.store=t}onDidChange(t,e){if(typeof t!="string")throw new TypeError(`Expected \`key\` to be of type \`string\`, got ${typeof t}`);if(typeof e!="function")throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof e}`);return this._handleValueChange(()=>this.get(t),e)}onDidAnyChange(t){if(typeof t!="function")throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof t}`);return this._handleStoreChange(t)}get size(){return Object.keys(this.store).filter(e=>!this._isReservedKeyPath(e)).length}get store(){try{const t=c.readFileSync(this.path,this.#s?null:"utf8"),e=this._decryptData(t);return(i=>{const s=this._deserialize(i);return this.#n||this._validate(s),Object.assign(h(),s)})(e)}catch(t){if(t?.code==="ENOENT")return this._ensureDirectory(),h();if(this.#t.clearInvalidConfig){const e=t;if(e.name==="SyntaxError"||e.message?.startsWith("Config schema violation:")||e.message==="Failed to decrypt config data.")return h()}throw t}}set store(t){if(this._ensureDirectory(),!j(t,f))try{const e=c.readFileSync(this.path,this.#s?null:"utf8"),r=this._decryptData(e),i=this._deserialize(r);j(i,f)&&w(t,f,F(i,f))}catch{}this.#n||this._validate(t),this._write(t),this.events.dispatchEvent(new Event("change"))}*[Symbol.iterator](){for(const[t,e]of Object.entries(this.store))this._isReservedKeyPath(t)||(yield[t,e])}_closeWatcher(){this.#a&&(this.#a.close(),this.#a=void 0),this.#c&&(c.unwatchFile(this.path),this.#c=!1),this.#r=void 0}_decryptData(t){const e=this.#s;if(!e)return typeof t=="string"?t:_(t);const r=this.#o,i=r==="aes-256-gcm"?16:0,s=":".codePointAt(0),o=typeof t=="string"?t.codePointAt(16):t[16];if(!(s!==void 0&&o===s)){if(r==="aes-256-cbc")return typeof t=="string"?t:_(t);throw new Error("Failed to decrypt config data.")}const a=p=>{if(i===0)return{ciphertext:p};const y=p.length-i;if(y<0)throw new Error("Invalid authentication tag length.");return{ciphertext:p.slice(0,y),authenticationTag:p.slice(y)}},d=t.slice(0,16),E=t.slice(17),$=typeof E=="string"?S(E):E,A=p=>{const{ciphertext:y,authenticationTag:x}=a($),R=g.pbkdf2Sync(e,p,1e4,32,"sha512"),v=g.createDecipheriv(r,R,d);return x&&v.setAuthTag(x),_(P([v.update(y),v.final()]))};try{return A(d)}catch{try{return A(d.toString())}catch{}}if(r==="aes-256-cbc")return typeof t=="string"?t:_(t);throw new Error("Failed to decrypt config data.")}_handleStoreChange(t){let e=this.store;const r=()=>{const i=e,s=this.store;C(s,i)||(e=s,t.call(this,s,i))};return this.events.addEventListener("change",r),()=>{this.events.removeEventListener("change",r)}}_handleValueChange(t,e){let r=t();const i=()=>{const s=r,o=t();C(o,s)||(r=o,e.call(this,o,s))};return this.events.addEventListener("change",i),()=>{this.events.removeEventListener("change",i)}}_deserialize=t=>JSON.parse(t);_serialize=t=>JSON.stringify(t,void 0," ");_validate(t){if(!this.#i||this.#i(t)||!this.#i.errors)return;const r=this.#i.errors.map(({instancePath:i,message:s=""})=>`\`${i.slice(1)}\` ${s}`);throw new Error("Config schema violation: "+r.join("; "))}_ensureDirectory(){c.mkdirSync(m.dirname(this.path),{recursive:!0})}_write(t){let e=this._serialize(t);const r=this.#s;if(r){const i=g.randomBytes(16),s=g.pbkdf2Sync(r,i,1e4,32,"sha512"),o=g.createCipheriv(this.#o,s,i),n=P([o.update(S(e)),o.final()]),a=[i,S(":"),n];this.#o==="aes-256-gcm"&&a.push(o.getAuthTag()),e=P(a)}if(b.env.SNAP)c.writeFileSync(this.path,e,{mode:this.#t.configFileMode});else try{L(this.path,e,{mode:this.#t.configFileMode})}catch(i){if(i?.code==="EXDEV"){c.writeFileSync(this.path,e,{mode:this.#t.configFileMode});return}throw i}}_watch(){if(this._ensureDirectory(),c.existsSync(this.path)||this._write(h()),b.platform==="win32"||b.platform==="darwin"){this.#r??=N(()=>{this.events.dispatchEvent(new Event("change"))},{wait:100});const t=m.dirname(this.path),e=m.basename(this.path);this.#a=c.watch(t,{persistent:!1,encoding:"utf8"},(r,i)=>{i&&i!==e||typeof this.#r=="function"&&this.#r()})}else this.#r??=N(()=>{this.events.dispatchEvent(new Event("change"))},{wait:1e3}),c.watchFile(this.path,{persistent:!1},(t,e)=>{typeof this.#r=="function"&&this.#r()}),this.#c=!0}_migrate(t,e,r){let i=this._get(T,"0.0.0");const s=Object.keys(t).filter(n=>this._shouldPerformMigration(n,i,e));let o=structuredClone(this.store);for(const n of s)try{r&&r(this,{fromVersion:i,toVersion:n,finalVersion:e,versions:s});const a=t[n];a?.(this),this._set(T,n),i=n,o=structuredClone(this.store)}catch(a){this.store=o;const d=a instanceof Error?a.message:String(a);throw new Error(`Something went wrong during the migration! Changes applied to the store until this failed migration will be restored. ${d}`)}(this._isVersionInRangeFormat(i)||!u.eq(i,e))&&this._set(T,e)}_containsReservedKey(t){return typeof t=="string"?this._isReservedKeyPath(t):!t||typeof t!="object"?!1:this._objectContainsReservedKey(t)}_objectContainsReservedKey(t){if(!t||typeof t!="object")return!1;for(const[e,r]of Object.entries(t))if(this._isReservedKeyPath(e)||this._objectContainsReservedKey(r))return!0;return!1}_isReservedKeyPath(t){return t===f||t.startsWith(`${f}.`)}_isVersionInRangeFormat(t){return u.clean(t)===null}_shouldPerformMigration(t,e,r){return this._isVersionInRangeFormat(t)?e!=="0.0.0"&&u.satisfies(e,t)?!1:u.satisfies(r,t):!(u.lte(t,e)||u.gt(t,r))}_get(t,e){return F(this.store,t,e)}_set(t,e){const{store:r}=this;w(r,t,e),this.store=r}#h(t){const e={configName:"config",fileExtension:"json",projectSuffix:"nodejs",clearInvalidConfig:!1,accessPropertiesByDotNotation:!0,configFileMode:438,...t};if(e.encryptionAlgorithm??=V,!q(e.encryptionAlgorithm))throw new TypeError(`The \`encryptionAlgorithm\` option must be one of: ${[...K].join(", ")}`);if(!e.cwd){if(!e.projectName)throw new Error("Please specify the `projectName` option.");e.cwd=B(e.projectName,{suffix:e.projectSuffix}).config}return typeof e.fileExtension=="string"&&(e.fileExtension=e.fileExtension.replace(/^\.+/,"")),e}#f(t){if(!(t.schema??t.ajvOptions??t.rootSchema))return;if(t.schema&&typeof t.schema!="object")throw new TypeError("The `schema` option must be an object.");const e=W.default,r=new M.Ajv2020({allErrors:!0,useDefaults:!0,...t.ajvOptions});e(r);const i={...t.rootSchema,type:"object",properties:t.schema};this.#i=r.compile(i),this.#l(t.schema)}#l(t){const e=Object.entries(t??{});for(const[r,i]of e){if(!i||typeof i!="object"||!Object.hasOwn(i,"default"))continue;const{default:s}=i;s!==void 0&&(this.#e[r]=s)}}#p(t){t.defaults&&Object.assign(this.#e,t.defaults)}#u(t){t.serialize&&(this._serialize=t.serialize),t.deserialize&&(this._deserialize=t.deserialize)}#d(t){const e=typeof t.fileExtension=="string"?t.fileExtension:void 0,r=e?`.${e}`:"";return m.resolve(t.cwd,`${t.configName??"config"}${r}`)}#y(t){if(t.migrations){this.#g(t),this._validate(this.store);return}const e=this.store,r=Object.assign(h(),t.defaults??{},e);this._validate(r);try{O.deepEqual(e,r)}catch{this.store=r}}#g(t){const{migrations:e,projectVersion:r}=t;if(e){if(!r)throw new Error("Please specify the `projectVersion` option.");this.#n=!0;try{const i=this.store,s=Object.assign(h(),t.defaults??{},i);try{O.deepEqual(i,s)}catch{this._write(s)}this._migrate(e,r,t.beforeEachMigration)}finally{this.#n=!1}}}}export{ot as C};
|