prettier 3.6.1 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/THIRD-PARTY-NOTICES.md +297 -82
- package/doc.js +599 -363
- package/doc.mjs +600 -367
- package/index.cjs +95 -77
- package/index.d.ts +8 -2
- package/index.mjs +5172 -5502
- package/internal/experimental-cli-worker.mjs +466 -259
- package/internal/experimental-cli.mjs +1148 -1967
- package/internal/legacy-cli.mjs +2063 -541
- package/package.json +2 -1
- package/plugins/acorn.js +13 -12
- package/plugins/acorn.mjs +13 -12
- package/plugins/angular.js +4 -2
- package/plugins/angular.mjs +4 -2
- package/plugins/babel.js +13 -13
- package/plugins/babel.mjs +13 -13
- package/plugins/estree.js +40 -32
- package/plugins/estree.mjs +40 -32
- package/plugins/flow.js +16 -15
- package/plugins/flow.mjs +16 -15
- package/plugins/glimmer.js +28 -20
- package/plugins/glimmer.mjs +30 -22
- package/plugins/graphql.js +15 -16
- package/plugins/graphql.mjs +15 -16
- package/plugins/html.js +23 -21
- package/plugins/html.mjs +23 -21
- package/plugins/markdown.js +53 -54
- package/plugins/markdown.mjs +52 -53
- package/plugins/meriyah.js +5 -4
- package/plugins/meriyah.mjs +5 -4
- package/plugins/postcss.js +41 -34
- package/plugins/postcss.mjs +41 -34
- package/plugins/typescript.js +16 -15
- package/plugins/typescript.mjs +16 -15
- package/plugins/yaml.js +90 -91
- package/plugins/yaml.mjs +90 -91
- package/standalone.js +29 -30
- package/standalone.mjs +29 -30
package/internal/legacy-cli.mjs
CHANGED
|
@@ -11,10 +11,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
11
11
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
12
12
|
var __getProtoOf = Object.getPrototypeOf;
|
|
13
13
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __typeError = (msg) => {
|
|
15
|
-
throw TypeError(msg);
|
|
16
|
-
};
|
|
17
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
18
14
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
19
15
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
20
16
|
}) : x)(function(x) {
|
|
@@ -40,12 +36,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
40
36
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
41
37
|
mod
|
|
42
38
|
));
|
|
43
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
44
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
45
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
46
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
47
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
48
|
-
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
49
39
|
|
|
50
40
|
// node_modules/dashify/index.js
|
|
51
41
|
var require_dashify = __commonJS({
|
|
@@ -288,7 +278,7 @@ var require_fast_json_stable_stringify = __commonJS({
|
|
|
288
278
|
if (!opts) opts = {};
|
|
289
279
|
if (typeof opts === "function") opts = { cmp: opts };
|
|
290
280
|
var cycles = typeof opts.cycles === "boolean" ? opts.cycles : false;
|
|
291
|
-
var cmp = opts.cmp && /* @__PURE__ */ function(f) {
|
|
281
|
+
var cmp = opts.cmp && /* @__PURE__ */ (function(f) {
|
|
292
282
|
return function(node) {
|
|
293
283
|
return function(a, b) {
|
|
294
284
|
var aobj = { key: a, value: node[a] };
|
|
@@ -296,9 +286,9 @@ var require_fast_json_stable_stringify = __commonJS({
|
|
|
296
286
|
return f(aobj, bobj);
|
|
297
287
|
};
|
|
298
288
|
};
|
|
299
|
-
}(opts.cmp);
|
|
289
|
+
})(opts.cmp);
|
|
300
290
|
var seen = [];
|
|
301
|
-
return function stringify5(node) {
|
|
291
|
+
return (function stringify5(node) {
|
|
302
292
|
if (node && node.toJSON && typeof node.toJSON === "function") {
|
|
303
293
|
node = node.toJSON();
|
|
304
294
|
}
|
|
@@ -331,7 +321,7 @@ var require_fast_json_stable_stringify = __commonJS({
|
|
|
331
321
|
}
|
|
332
322
|
seen.splice(seenIndex, 1);
|
|
333
323
|
return "{" + out + "}";
|
|
334
|
-
}(data);
|
|
324
|
+
})(data);
|
|
335
325
|
};
|
|
336
326
|
}
|
|
337
327
|
});
|
|
@@ -463,6 +453,11 @@ var require_vendors = __commonJS({
|
|
|
463
453
|
constant: "CLOUDFLARE_PAGES",
|
|
464
454
|
env: "CF_PAGES"
|
|
465
455
|
},
|
|
456
|
+
{
|
|
457
|
+
name: "Cloudflare Workers",
|
|
458
|
+
constant: "CLOUDFLARE_WORKERS",
|
|
459
|
+
env: "WORKERS_CI"
|
|
460
|
+
},
|
|
466
461
|
{
|
|
467
462
|
name: "Codefresh",
|
|
468
463
|
constant: "CODEFRESH",
|
|
@@ -741,23 +736,25 @@ var require_ci_info = __commonJS({
|
|
|
741
736
|
exports.name = null;
|
|
742
737
|
exports.isPR = null;
|
|
743
738
|
exports.id = null;
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
739
|
+
if (env3.CI !== "false") {
|
|
740
|
+
vendors.forEach(function(vendor) {
|
|
741
|
+
const envs = Array.isArray(vendor.env) ? vendor.env : [vendor.env];
|
|
742
|
+
const isCI2 = envs.every(function(obj) {
|
|
743
|
+
return checkEnv(obj);
|
|
744
|
+
});
|
|
745
|
+
exports[vendor.constant] = isCI2;
|
|
746
|
+
if (!isCI2) {
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
exports.name = vendor.name;
|
|
750
|
+
exports.isPR = checkPR(vendor);
|
|
751
|
+
exports.id = vendor.constant;
|
|
748
752
|
});
|
|
749
|
-
|
|
750
|
-
if (!isCI2) {
|
|
751
|
-
return;
|
|
752
|
-
}
|
|
753
|
-
exports.name = vendor.name;
|
|
754
|
-
exports.isPR = checkPR(vendor);
|
|
755
|
-
exports.id = vendor.constant;
|
|
756
|
-
});
|
|
753
|
+
}
|
|
757
754
|
exports.isCI = !!(env3.CI !== "false" && // Bypass all checks if CI env is explicitly set to 'false'
|
|
758
755
|
(env3.BUILD_ID || // Jenkins, Cloudbees
|
|
759
756
|
env3.BUILD_NUMBER || // Jenkins, TeamCity
|
|
760
|
-
env3.CI || // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari, Cloudflare Pages
|
|
757
|
+
env3.CI || // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari, Cloudflare Pages/Workers
|
|
761
758
|
env3.CI_APP_ID || // Appflow
|
|
762
759
|
env3.CI_BUILD_ID || // Appflow
|
|
763
760
|
env3.CI_BUILD_NUMBER || // Appflow
|
|
@@ -809,17 +806,26 @@ var require_ci_info = __commonJS({
|
|
|
809
806
|
// src/cli/index.js
|
|
810
807
|
import * as prettier2 from "../index.mjs";
|
|
811
808
|
|
|
812
|
-
// scripts/build/shims/
|
|
813
|
-
var
|
|
814
|
-
|
|
809
|
+
// scripts/build/shims/shared.js
|
|
810
|
+
var OPTIONAL_OBJECT = 1;
|
|
811
|
+
var createMethodShim = (methodName, getImplementation) => (flags, object2, ...arguments_) => {
|
|
812
|
+
if (flags | OPTIONAL_OBJECT && (object2 === void 0 || object2 === null)) {
|
|
815
813
|
return;
|
|
816
814
|
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
}
|
|
820
|
-
return object2.at(index);
|
|
815
|
+
const implementation = getImplementation.call(object2) ?? object2[methodName];
|
|
816
|
+
return implementation.apply(object2, arguments_);
|
|
821
817
|
};
|
|
822
|
-
|
|
818
|
+
|
|
819
|
+
// scripts/build/shims/method-at.js
|
|
820
|
+
function stringOrArrayAt(index) {
|
|
821
|
+
return this[index < 0 ? this.length + index : index];
|
|
822
|
+
}
|
|
823
|
+
var at = createMethodShim("at", function() {
|
|
824
|
+
if (Array.isArray(this) || typeof this === "string") {
|
|
825
|
+
return stringOrArrayAt;
|
|
826
|
+
}
|
|
827
|
+
});
|
|
828
|
+
var method_at_default = at;
|
|
823
829
|
|
|
824
830
|
// src/cli/options/get-context-options.js
|
|
825
831
|
var import_dashify = __toESM(require_dashify(), 1);
|
|
@@ -1007,7 +1013,7 @@ var {
|
|
|
1007
1013
|
fastGlob,
|
|
1008
1014
|
createTwoFilesPatch,
|
|
1009
1015
|
picocolors,
|
|
1010
|
-
|
|
1016
|
+
closetLevenshteinMatch
|
|
1011
1017
|
} = sharedWithCli;
|
|
1012
1018
|
|
|
1013
1019
|
// src/cli/options/get-context-options.js
|
|
@@ -1030,12 +1036,11 @@ function apiOptionToCliOption(apiOption) {
|
|
|
1030
1036
|
return normalizeDetailedOption(cliOption);
|
|
1031
1037
|
}
|
|
1032
1038
|
function normalizeDetailedOption(option) {
|
|
1033
|
-
var _a;
|
|
1034
1039
|
return {
|
|
1035
1040
|
category: optionCategories.CATEGORY_OTHER,
|
|
1036
1041
|
...option,
|
|
1037
1042
|
name: option.cliName ?? (0, import_dashify.default)(option.name),
|
|
1038
|
-
choices:
|
|
1043
|
+
choices: option.choices?.map((choice) => {
|
|
1039
1044
|
const newChoice = {
|
|
1040
1045
|
description: "",
|
|
1041
1046
|
deprecated: false,
|
|
@@ -1071,27 +1076,26 @@ function getContextOptionsWithoutPlugins() {
|
|
|
1071
1076
|
return supportInfoToContextOptions(supportInfo);
|
|
1072
1077
|
}
|
|
1073
1078
|
|
|
1074
|
-
// scripts/build/shims/
|
|
1075
|
-
var stringReplaceAll =
|
|
1076
|
-
if (isOptionalObject && (original === void 0 || original === null)) {
|
|
1077
|
-
return;
|
|
1078
|
-
}
|
|
1079
|
-
if (original.replaceAll) {
|
|
1080
|
-
return original.replaceAll(pattern, replacement);
|
|
1081
|
-
}
|
|
1079
|
+
// scripts/build/shims/method-replace-all.js
|
|
1080
|
+
var stringReplaceAll = String.prototype.replaceAll ?? function(pattern, replacement) {
|
|
1082
1081
|
if (pattern.global) {
|
|
1083
|
-
return
|
|
1082
|
+
return this.replace(pattern, replacement);
|
|
1084
1083
|
}
|
|
1085
|
-
return
|
|
1084
|
+
return this.split(pattern).join(replacement);
|
|
1086
1085
|
};
|
|
1087
|
-
var
|
|
1086
|
+
var replaceAll = createMethodShim("replaceAll", function() {
|
|
1087
|
+
if (typeof this === "string") {
|
|
1088
|
+
return stringReplaceAll;
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
var method_replace_all_default = replaceAll;
|
|
1088
1092
|
|
|
1089
1093
|
// node_modules/camelcase/index.js
|
|
1090
1094
|
var UPPERCASE = /[\p{Lu}]/u;
|
|
1091
1095
|
var LOWERCASE = /[\p{Ll}]/u;
|
|
1092
|
-
var LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/
|
|
1093
|
-
var IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
|
|
1096
|
+
var LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/u;
|
|
1094
1097
|
var SEPARATORS = /[_.\- ]+/;
|
|
1098
|
+
var IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
|
|
1095
1099
|
var LEADING_SEPARATORS = new RegExp("^" + SEPARATORS.source);
|
|
1096
1100
|
var SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, "gu");
|
|
1097
1101
|
var NUMBERS_AND_IDENTIFIER = new RegExp("\\d+" + IDENTIFIER.source, "gu");
|
|
@@ -1122,28 +1126,57 @@ var preserveCamelCase = (string, toLowerCase, toUpperCase, preserveConsecutiveUp
|
|
|
1122
1126
|
}
|
|
1123
1127
|
return string;
|
|
1124
1128
|
};
|
|
1125
|
-
var preserveConsecutiveUppercase = (input, toLowerCase) =>
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1129
|
+
var preserveConsecutiveUppercase = (input, toLowerCase) => input.replace(LEADING_CAPITAL, (match) => toLowerCase(match));
|
|
1130
|
+
var processWithCasePreservation = (input, toLowerCase, preserveConsecutiveUppercase2) => {
|
|
1131
|
+
let result = "";
|
|
1132
|
+
let previousWasNumber = false;
|
|
1133
|
+
let previousWasUppercase = false;
|
|
1134
|
+
const characters = [...input];
|
|
1135
|
+
for (let index = 0; index < characters.length; index++) {
|
|
1136
|
+
const character = characters[index];
|
|
1137
|
+
const isUpperCase = UPPERCASE.test(character);
|
|
1138
|
+
const nextCharIsUpperCase = index + 1 < characters.length && UPPERCASE.test(characters[index + 1]);
|
|
1139
|
+
if (previousWasNumber && /[\p{Alpha}]/u.test(character)) {
|
|
1140
|
+
result += character;
|
|
1141
|
+
previousWasNumber = false;
|
|
1142
|
+
previousWasUppercase = isUpperCase;
|
|
1143
|
+
} else if (preserveConsecutiveUppercase2 && isUpperCase && (previousWasUppercase || nextCharIsUpperCase)) {
|
|
1144
|
+
result += character;
|
|
1145
|
+
previousWasUppercase = true;
|
|
1146
|
+
} else if (/\d/.test(character)) {
|
|
1147
|
+
result += character;
|
|
1148
|
+
previousWasNumber = true;
|
|
1149
|
+
previousWasUppercase = false;
|
|
1150
|
+
} else if (SEPARATORS.test(character)) {
|
|
1151
|
+
result += character;
|
|
1152
|
+
previousWasUppercase = false;
|
|
1153
|
+
} else {
|
|
1154
|
+
result += toLowerCase(character);
|
|
1155
|
+
previousWasNumber = false;
|
|
1156
|
+
previousWasUppercase = false;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
return result;
|
|
1134
1160
|
};
|
|
1135
|
-
var postProcess = (input, toUpperCase
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1161
|
+
var postProcess = (input, toUpperCase, {
|
|
1162
|
+
capitalizeAfterNumber
|
|
1163
|
+
}) => {
|
|
1164
|
+
const transformNumericIdentifier = capitalizeAfterNumber ? (match, identifier, offset, string) => {
|
|
1165
|
+
const nextCharacter = string.charAt(offset + match.length);
|
|
1166
|
+
if (SEPARATORS.test(nextCharacter)) {
|
|
1167
|
+
return match;
|
|
1168
|
+
}
|
|
1169
|
+
return identifier ? match.slice(0, -identifier.length) + toUpperCase(identifier) : match;
|
|
1170
|
+
} : (match) => match;
|
|
1171
|
+
return method_replace_all_default(
|
|
1172
|
+
/* OPTIONAL_OBJECT: false */
|
|
1173
|
+
0,
|
|
1174
|
+
method_replace_all_default(
|
|
1175
|
+
/* OPTIONAL_OBJECT: false */
|
|
1176
|
+
0,
|
|
1144
1177
|
input,
|
|
1145
1178
|
NUMBERS_AND_IDENTIFIER,
|
|
1146
|
-
|
|
1179
|
+
transformNumericIdentifier
|
|
1147
1180
|
),
|
|
1148
1181
|
SEPARATORS_AND_IDENTIFIER,
|
|
1149
1182
|
(_2, identifier) => toUpperCase(identifier)
|
|
@@ -1156,34 +1189,44 @@ function camelCase(input, options) {
|
|
|
1156
1189
|
options = {
|
|
1157
1190
|
pascalCase: false,
|
|
1158
1191
|
preserveConsecutiveUppercase: false,
|
|
1192
|
+
capitalizeAfterNumber: true,
|
|
1159
1193
|
...options
|
|
1160
1194
|
};
|
|
1161
1195
|
if (Array.isArray(input)) {
|
|
1162
|
-
input = input.map((
|
|
1196
|
+
input = input.map((element) => element.trim()).filter((element) => element.length > 0).join("-");
|
|
1163
1197
|
} else {
|
|
1164
1198
|
input = input.trim();
|
|
1165
1199
|
}
|
|
1166
1200
|
if (input.length === 0) {
|
|
1167
1201
|
return "";
|
|
1168
1202
|
}
|
|
1203
|
+
const leadingPrefix = input.match(/^[_$]*/)[0];
|
|
1204
|
+
input = input.slice(leadingPrefix.length);
|
|
1205
|
+
if (input.length === 0) {
|
|
1206
|
+
return leadingPrefix;
|
|
1207
|
+
}
|
|
1169
1208
|
const toLowerCase = options.locale === false ? (string) => string.toLowerCase() : (string) => string.toLocaleLowerCase(options.locale);
|
|
1170
1209
|
const toUpperCase = options.locale === false ? (string) => string.toUpperCase() : (string) => string.toLocaleUpperCase(options.locale);
|
|
1171
1210
|
if (input.length === 1) {
|
|
1172
1211
|
if (SEPARATORS.test(input)) {
|
|
1173
|
-
return
|
|
1212
|
+
return leadingPrefix;
|
|
1174
1213
|
}
|
|
1175
|
-
return options.pascalCase ? toUpperCase(input) : toLowerCase(input);
|
|
1214
|
+
return leadingPrefix + (options.pascalCase ? toUpperCase(input) : toLowerCase(input));
|
|
1176
1215
|
}
|
|
1177
1216
|
const hasUpperCase = input !== toLowerCase(input);
|
|
1178
1217
|
if (hasUpperCase) {
|
|
1179
1218
|
input = preserveCamelCase(input, toLowerCase, toUpperCase, options.preserveConsecutiveUppercase);
|
|
1180
1219
|
}
|
|
1181
1220
|
input = input.replace(LEADING_SEPARATORS, "");
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1221
|
+
if (options.capitalizeAfterNumber) {
|
|
1222
|
+
input = options.preserveConsecutiveUppercase ? preserveConsecutiveUppercase(input, toLowerCase) : toLowerCase(input);
|
|
1223
|
+
} else {
|
|
1224
|
+
input = processWithCasePreservation(input, toLowerCase, options.preserveConsecutiveUppercase);
|
|
1225
|
+
}
|
|
1226
|
+
if (options.pascalCase && input.length > 0) {
|
|
1227
|
+
input = toUpperCase(input[0]) + input.slice(1);
|
|
1185
1228
|
}
|
|
1186
|
-
return postProcess(input, toUpperCase);
|
|
1229
|
+
return leadingPrefix + postProcess(input, toUpperCase, options);
|
|
1187
1230
|
}
|
|
1188
1231
|
|
|
1189
1232
|
// src/cli/utils.js
|
|
@@ -1191,13 +1234,33 @@ import fs from "fs/promises";
|
|
|
1191
1234
|
import path from "path";
|
|
1192
1235
|
|
|
1193
1236
|
// node_modules/sdbm/index.js
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1237
|
+
var textEncoder = new TextEncoder();
|
|
1238
|
+
function sdbmHash(input, options) {
|
|
1239
|
+
if (typeof input === "string") {
|
|
1240
|
+
if (options?.bytes) {
|
|
1241
|
+
input = textEncoder.encode(input);
|
|
1242
|
+
} else {
|
|
1243
|
+
let hash3 = 0n;
|
|
1244
|
+
for (let index = 0; index < input.length; index++) {
|
|
1245
|
+
hash3 = BigInt(input.charCodeAt(index)) + (hash3 << 6n) + (hash3 << 16n) - hash3;
|
|
1246
|
+
}
|
|
1247
|
+
return hash3;
|
|
1248
|
+
}
|
|
1249
|
+
} else if (!(input instanceof Uint8Array)) {
|
|
1250
|
+
throw new TypeError("Expected a string or Uint8Array");
|
|
1251
|
+
}
|
|
1252
|
+
let hash2 = 0n;
|
|
1253
|
+
for (const byte of input) {
|
|
1254
|
+
hash2 = BigInt(byte) + (hash2 << 6n) + (hash2 << 16n) - hash2;
|
|
1198
1255
|
}
|
|
1199
|
-
return hash2
|
|
1256
|
+
return hash2;
|
|
1257
|
+
}
|
|
1258
|
+
function sdbm(input, options) {
|
|
1259
|
+
return Number(BigInt.asUintN(32, sdbmHash(input, options)));
|
|
1200
1260
|
}
|
|
1261
|
+
sdbm.bigint = function(input, options) {
|
|
1262
|
+
return BigInt.asUintN(64, sdbmHash(input, options));
|
|
1263
|
+
};
|
|
1201
1264
|
|
|
1202
1265
|
// src/cli/utils.js
|
|
1203
1266
|
import { __internal as sharedWithCli2 } from "../index.mjs";
|
|
@@ -1247,14 +1310,16 @@ function isJson(value) {
|
|
|
1247
1310
|
return false;
|
|
1248
1311
|
}
|
|
1249
1312
|
}
|
|
1250
|
-
var normalizeToPosix = path.sep === "\\" ? (filepath) =>
|
|
1251
|
-
/*
|
|
1252
|
-
|
|
1313
|
+
var normalizeToPosix = path.sep === "\\" ? (filepath) => method_replace_all_default(
|
|
1314
|
+
/* OPTIONAL_OBJECT: false */
|
|
1315
|
+
0,
|
|
1253
1316
|
filepath,
|
|
1254
1317
|
"\\",
|
|
1255
1318
|
"/"
|
|
1256
1319
|
) : (filepath) => filepath;
|
|
1257
|
-
var {
|
|
1320
|
+
var {
|
|
1321
|
+
omit
|
|
1322
|
+
} = sharedWithCli2.utils;
|
|
1258
1323
|
|
|
1259
1324
|
// src/cli/options/create-minimist-options.js
|
|
1260
1325
|
function createMinimistOptions(detailedOptions) {
|
|
@@ -1306,16 +1371,17 @@ var descriptor = {
|
|
|
1306
1371
|
value: (value) => vnopts.apiDescriptor.value(value),
|
|
1307
1372
|
pair: ({ key, value }) => value === false ? `--no-${key}` : value === true ? descriptor.key(key) : value === "" ? `${descriptor.key(key)} without an argument` : `${descriptor.key(key)}=${value}`
|
|
1308
1373
|
};
|
|
1309
|
-
var _flags;
|
|
1310
1374
|
var FlagSchema = class extends vnopts.ChoiceSchema {
|
|
1375
|
+
#flags = [];
|
|
1311
1376
|
constructor({ name, flags }) {
|
|
1312
1377
|
super({ name, choices: flags });
|
|
1313
|
-
|
|
1314
|
-
__privateSet(this, _flags, [...flags].sort());
|
|
1378
|
+
this.#flags = [...flags].sort();
|
|
1315
1379
|
}
|
|
1316
1380
|
preprocess(value, utils) {
|
|
1317
|
-
if (typeof value === "string" && value.length > 0 && !
|
|
1318
|
-
const suggestion =
|
|
1381
|
+
if (typeof value === "string" && value.length > 0 && !this.#flags.includes(value)) {
|
|
1382
|
+
const suggestion = closetLevenshteinMatch(value, this.#flags, {
|
|
1383
|
+
maxDistance: 3
|
|
1384
|
+
});
|
|
1319
1385
|
if (suggestion) {
|
|
1320
1386
|
utils.logger.warn(
|
|
1321
1387
|
[
|
|
@@ -1332,7 +1398,6 @@ var FlagSchema = class extends vnopts.ChoiceSchema {
|
|
|
1332
1398
|
return "a flag";
|
|
1333
1399
|
}
|
|
1334
1400
|
};
|
|
1335
|
-
_flags = new WeakMap();
|
|
1336
1401
|
function normalizeCliOptions(options, optionInfos, opts) {
|
|
1337
1402
|
return normalizeOptions(options, optionInfos, {
|
|
1338
1403
|
...opts,
|
|
@@ -1345,7 +1410,6 @@ var normalize_cli_options_default = normalizeCliOptions;
|
|
|
1345
1410
|
|
|
1346
1411
|
// src/cli/options/parse-cli-arguments.js
|
|
1347
1412
|
function parseArgv(rawArguments, detailedOptions, logger, keys2) {
|
|
1348
|
-
var _a;
|
|
1349
1413
|
const minimistOptions = createMinimistOptions(detailedOptions);
|
|
1350
1414
|
let argv2 = minimistParse(rawArguments, minimistOptions);
|
|
1351
1415
|
if (keys2) {
|
|
@@ -1362,7 +1426,7 @@ function parseArgv(rawArguments, detailedOptions, logger, keys2) {
|
|
|
1362
1426
|
return [option.forwardToApi || camelCase(key), value];
|
|
1363
1427
|
})
|
|
1364
1428
|
),
|
|
1365
|
-
_:
|
|
1429
|
+
_: normalized._?.map(String),
|
|
1366
1430
|
get __raw() {
|
|
1367
1431
|
return argv2;
|
|
1368
1432
|
}
|
|
@@ -1379,18 +1443,23 @@ function parseArgvWithoutPlugins(rawArguments, logger, keys2) {
|
|
|
1379
1443
|
}
|
|
1380
1444
|
|
|
1381
1445
|
// src/cli/context.js
|
|
1382
|
-
var _stack;
|
|
1383
1446
|
var Context = class {
|
|
1384
|
-
|
|
1385
|
-
|
|
1447
|
+
#stack = [];
|
|
1448
|
+
constructor({
|
|
1449
|
+
rawArguments,
|
|
1450
|
+
logger
|
|
1451
|
+
}) {
|
|
1386
1452
|
this.rawArguments = rawArguments;
|
|
1387
1453
|
this.logger = logger;
|
|
1388
1454
|
}
|
|
1389
1455
|
async init() {
|
|
1390
|
-
const {
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1456
|
+
const {
|
|
1457
|
+
rawArguments,
|
|
1458
|
+
logger
|
|
1459
|
+
} = this;
|
|
1460
|
+
const {
|
|
1461
|
+
plugins
|
|
1462
|
+
} = parseArgvWithoutPlugins(rawArguments, logger, ["plugin"]);
|
|
1394
1463
|
await this.pushContextPlugins(plugins);
|
|
1395
1464
|
const argv2 = parseArgv(rawArguments, this.detailedOptions, logger);
|
|
1396
1465
|
this.argv = argv2;
|
|
@@ -1401,21 +1470,24 @@ var Context = class {
|
|
|
1401
1470
|
*/
|
|
1402
1471
|
async pushContextPlugins(plugins) {
|
|
1403
1472
|
const options = await getContextOptions(plugins);
|
|
1404
|
-
|
|
1473
|
+
this.#stack.push(options);
|
|
1405
1474
|
Object.assign(this, options);
|
|
1406
1475
|
}
|
|
1407
1476
|
popContextPlugins() {
|
|
1408
|
-
|
|
1409
|
-
Object.assign(this,
|
|
1410
|
-
/*
|
|
1411
|
-
|
|
1412
|
-
|
|
1477
|
+
this.#stack.pop();
|
|
1478
|
+
Object.assign(this, method_at_default(
|
|
1479
|
+
/* OPTIONAL_OBJECT: false */
|
|
1480
|
+
0,
|
|
1481
|
+
this.#stack,
|
|
1413
1482
|
-1
|
|
1414
1483
|
));
|
|
1415
1484
|
}
|
|
1416
1485
|
// eslint-disable-next-line getter-return
|
|
1417
1486
|
get performanceTestFlag() {
|
|
1418
|
-
const {
|
|
1487
|
+
const {
|
|
1488
|
+
debugBenchmark,
|
|
1489
|
+
debugRepeat
|
|
1490
|
+
} = this.argv;
|
|
1419
1491
|
if (debugBenchmark) {
|
|
1420
1492
|
return {
|
|
1421
1493
|
name: "--debug-benchmark",
|
|
@@ -1428,7 +1500,9 @@ var Context = class {
|
|
|
1428
1500
|
debugRepeat
|
|
1429
1501
|
};
|
|
1430
1502
|
}
|
|
1431
|
-
const {
|
|
1503
|
+
const {
|
|
1504
|
+
PRETTIER_PERF_REPEAT
|
|
1505
|
+
} = process.env;
|
|
1432
1506
|
if (PRETTIER_PERF_REPEAT && /^\d+$/u.test(PRETTIER_PERF_REPEAT)) {
|
|
1433
1507
|
return {
|
|
1434
1508
|
name: "PRETTIER_PERF_REPEAT (environment variable)",
|
|
@@ -1437,7 +1511,6 @@ var Context = class {
|
|
|
1437
1511
|
}
|
|
1438
1512
|
}
|
|
1439
1513
|
};
|
|
1440
|
-
_stack = new WeakMap();
|
|
1441
1514
|
var context_default = Context;
|
|
1442
1515
|
|
|
1443
1516
|
// src/cli/file-info.js
|
|
@@ -1455,7 +1528,7 @@ async function logFileInfoOrDie(context) {
|
|
|
1455
1528
|
const fileInfo = await getFileInfo(path2.resolve(file), {
|
|
1456
1529
|
ignorePath,
|
|
1457
1530
|
withNodeModules: withNodeModules2,
|
|
1458
|
-
plugins
|
|
1531
|
+
plugins,
|
|
1459
1532
|
resolveConfig: config !== false
|
|
1460
1533
|
});
|
|
1461
1534
|
const result = await format((0, import_fast_json_stable_stringify.default)(fileInfo), { parser: "json" });
|
|
@@ -1518,14 +1591,13 @@ import path4 from "path";
|
|
|
1518
1591
|
var alwaysIgnoredDirectories = [".git", ".sl", ".svn", ".hg", ".jj"];
|
|
1519
1592
|
var withNodeModules = [...alwaysIgnoredDirectories, "node_modules"];
|
|
1520
1593
|
var cwd = process.cwd();
|
|
1521
|
-
var _directories;
|
|
1522
1594
|
var DirectoryIgnorer = class {
|
|
1595
|
+
#directories;
|
|
1596
|
+
ignorePatterns;
|
|
1523
1597
|
constructor(shouldIgnoreNodeModules) {
|
|
1524
|
-
__privateAdd(this, _directories);
|
|
1525
|
-
__publicField(this, "ignorePatterns");
|
|
1526
1598
|
const directories = shouldIgnoreNodeModules ? withNodeModules : alwaysIgnoredDirectories;
|
|
1527
1599
|
const patterns = directories.map((directory) => `**/${directory}`);
|
|
1528
|
-
|
|
1600
|
+
this.#directories = new Set(directories);
|
|
1529
1601
|
this.ignorePatterns = patterns;
|
|
1530
1602
|
}
|
|
1531
1603
|
/**
|
|
@@ -1534,11 +1606,10 @@ var DirectoryIgnorer = class {
|
|
|
1534
1606
|
shouldIgnore(absolutePathOrPattern) {
|
|
1535
1607
|
const directoryNames = path4.relative(cwd, absolutePathOrPattern).split(path4.sep);
|
|
1536
1608
|
return directoryNames.some(
|
|
1537
|
-
(directoryName) =>
|
|
1609
|
+
(directoryName) => this.#directories.has(directoryName)
|
|
1538
1610
|
);
|
|
1539
1611
|
}
|
|
1540
1612
|
};
|
|
1541
|
-
_directories = new WeakMap();
|
|
1542
1613
|
var directoryIgnorerWithNodeModules = new DirectoryIgnorer(
|
|
1543
1614
|
/* shouldIgnoreNodeModules */
|
|
1544
1615
|
true
|
|
@@ -1552,12 +1623,16 @@ var directoryIgnorerWithoutNodeModules = new DirectoryIgnorer(
|
|
|
1552
1623
|
async function* expandPatterns(context) {
|
|
1553
1624
|
const seen = /* @__PURE__ */ new Set();
|
|
1554
1625
|
let noResults = true;
|
|
1555
|
-
for await (const {
|
|
1556
|
-
|
|
1557
|
-
|
|
1626
|
+
for await (const {
|
|
1627
|
+
filePath,
|
|
1628
|
+
ignoreUnknown,
|
|
1629
|
+
error
|
|
1630
|
+
} of expandPatternsInternal(context)) {
|
|
1558
1631
|
noResults = false;
|
|
1559
1632
|
if (error) {
|
|
1560
|
-
yield {
|
|
1633
|
+
yield {
|
|
1634
|
+
error
|
|
1635
|
+
};
|
|
1561
1636
|
continue;
|
|
1562
1637
|
}
|
|
1563
1638
|
const filename = path5.resolve(filePath);
|
|
@@ -1565,7 +1640,10 @@ async function* expandPatterns(context) {
|
|
|
1565
1640
|
continue;
|
|
1566
1641
|
}
|
|
1567
1642
|
seen.add(filename);
|
|
1568
|
-
yield {
|
|
1643
|
+
yield {
|
|
1644
|
+
filename,
|
|
1645
|
+
ignoreUnknown
|
|
1646
|
+
};
|
|
1569
1647
|
}
|
|
1570
1648
|
if (noResults && context.argv.errorOnUnmatchedPattern !== false) {
|
|
1571
1649
|
yield {
|
|
@@ -1595,9 +1673,7 @@ async function* expandPatternsInternal(context) {
|
|
|
1595
1673
|
error: `Explicitly specified pattern "${pattern}" is a symbolic link.`
|
|
1596
1674
|
};
|
|
1597
1675
|
} else {
|
|
1598
|
-
context.logger.debug(
|
|
1599
|
-
`Skipping pattern "${pattern}", as it is a symbolic link.`
|
|
1600
|
-
);
|
|
1676
|
+
context.logger.debug(`Skipping pattern "${pattern}", as it is a symbolic link.`);
|
|
1601
1677
|
}
|
|
1602
1678
|
} else if (stat.isFile()) {
|
|
1603
1679
|
entries.push({
|
|
@@ -1625,11 +1701,18 @@ async function* expandPatternsInternal(context) {
|
|
|
1625
1701
|
});
|
|
1626
1702
|
}
|
|
1627
1703
|
}
|
|
1628
|
-
for (const {
|
|
1704
|
+
for (const {
|
|
1705
|
+
type,
|
|
1706
|
+
glob,
|
|
1707
|
+
input,
|
|
1708
|
+
ignoreUnknown
|
|
1709
|
+
} of entries) {
|
|
1629
1710
|
let result;
|
|
1630
1711
|
try {
|
|
1631
1712
|
result = await fastGlob(glob, globOptions);
|
|
1632
|
-
} catch ({
|
|
1713
|
+
} catch ({
|
|
1714
|
+
message
|
|
1715
|
+
}) {
|
|
1633
1716
|
yield {
|
|
1634
1717
|
error: `${errorMessages.globError[type]}: "${input}".
|
|
1635
1718
|
${message}`
|
|
@@ -1638,10 +1721,15 @@ ${message}`
|
|
|
1638
1721
|
}
|
|
1639
1722
|
if (result.length === 0) {
|
|
1640
1723
|
if (context.argv.errorOnUnmatchedPattern !== false) {
|
|
1641
|
-
yield {
|
|
1724
|
+
yield {
|
|
1725
|
+
error: `${errorMessages.emptyResults[type]}: "${input}".`
|
|
1726
|
+
};
|
|
1642
1727
|
}
|
|
1643
1728
|
} else {
|
|
1644
|
-
yield* sortPaths(result).map((filePath) => ({
|
|
1729
|
+
yield* sortPaths(result).map((filePath) => ({
|
|
1730
|
+
filePath,
|
|
1731
|
+
ignoreUnknown
|
|
1732
|
+
}));
|
|
1645
1733
|
}
|
|
1646
1734
|
}
|
|
1647
1735
|
}
|
|
@@ -1661,27 +1749,27 @@ function sortPaths(paths) {
|
|
|
1661
1749
|
return paths.sort((a, b) => a.localeCompare(b));
|
|
1662
1750
|
}
|
|
1663
1751
|
function escapePathForGlob(path13) {
|
|
1664
|
-
return
|
|
1665
|
-
/*
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
/*
|
|
1669
|
-
|
|
1752
|
+
return method_replace_all_default(
|
|
1753
|
+
/* OPTIONAL_OBJECT: false */
|
|
1754
|
+
0,
|
|
1755
|
+
method_replace_all_default(
|
|
1756
|
+
/* OPTIONAL_OBJECT: false */
|
|
1757
|
+
0,
|
|
1670
1758
|
fastGlob.escapePath(
|
|
1671
|
-
|
|
1672
|
-
/*
|
|
1673
|
-
|
|
1759
|
+
method_replace_all_default(
|
|
1760
|
+
/* OPTIONAL_OBJECT: false */
|
|
1761
|
+
0,
|
|
1674
1762
|
path13,
|
|
1675
1763
|
"\\",
|
|
1676
1764
|
"\0"
|
|
1677
1765
|
)
|
|
1678
1766
|
// Workaround for fast-glob#262 (part 1)
|
|
1679
1767
|
),
|
|
1680
|
-
|
|
1768
|
+
"\\!",
|
|
1681
1769
|
"@(!)"
|
|
1682
1770
|
),
|
|
1683
1771
|
"\0",
|
|
1684
|
-
|
|
1772
|
+
"@(\\\\)"
|
|
1685
1773
|
);
|
|
1686
1774
|
}
|
|
1687
1775
|
var fixWindowsSlashes = normalizeToPosix;
|
|
@@ -1719,7 +1807,7 @@ function findUpSync(name, {
|
|
|
1719
1807
|
const filePath = isAbsoluteName ? name : path6.join(directory, name);
|
|
1720
1808
|
try {
|
|
1721
1809
|
const stats = fs2.statSync(filePath, { throwIfNoEntry: false });
|
|
1722
|
-
if (type === "file" &&
|
|
1810
|
+
if (type === "file" && stats?.isFile() || type === "directory" && stats?.isDirectory()) {
|
|
1723
1811
|
return filePath;
|
|
1724
1812
|
}
|
|
1725
1813
|
} catch {
|
|
@@ -1784,7 +1872,7 @@ function findCacheDirectory(options = {}) {
|
|
|
1784
1872
|
|
|
1785
1873
|
// src/cli/find-cache-file.js
|
|
1786
1874
|
function findDefaultCacheFile() {
|
|
1787
|
-
const cacheDir = findCacheDirectory({ name: "prettier"
|
|
1875
|
+
const cacheDir = findCacheDirectory({ name: "prettier" }) ?? os.tmpdir();
|
|
1788
1876
|
const cacheFilePath = path9.join(cacheDir, ".prettier-cache");
|
|
1789
1877
|
return cacheFilePath;
|
|
1790
1878
|
}
|
|
@@ -1823,153 +1911,1392 @@ import fs6 from "fs";
|
|
|
1823
1911
|
import path11 from "path";
|
|
1824
1912
|
|
|
1825
1913
|
// node_modules/file-entry-cache/node_modules/flat-cache/dist/index.js
|
|
1826
|
-
import path10 from "path";
|
|
1827
1914
|
import fs5 from "fs";
|
|
1915
|
+
import path10 from "path";
|
|
1828
1916
|
|
|
1829
1917
|
// node_modules/hookified/dist/node/index.js
|
|
1830
|
-
var
|
|
1918
|
+
var Eventified = class {
|
|
1831
1919
|
_eventListeners;
|
|
1832
1920
|
_maxListeners;
|
|
1833
1921
|
_logger;
|
|
1834
|
-
|
|
1835
|
-
|
|
1922
|
+
_throwOnEmitError = false;
|
|
1923
|
+
_throwOnEmptyListeners = false;
|
|
1924
|
+
_errorEvent = "error";
|
|
1925
|
+
constructor(options) {
|
|
1926
|
+
this._eventListeners = /* @__PURE__ */ new Map();
|
|
1927
|
+
this._maxListeners = 100;
|
|
1928
|
+
this._logger = options?.logger;
|
|
1929
|
+
if (options?.throwOnEmitError !== void 0) {
|
|
1930
|
+
this._throwOnEmitError = options.throwOnEmitError;
|
|
1931
|
+
}
|
|
1932
|
+
if (options?.throwOnEmptyListeners !== void 0) {
|
|
1933
|
+
this._throwOnEmptyListeners = options.throwOnEmptyListeners;
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
/**
|
|
1937
|
+
* Gets the logger
|
|
1938
|
+
* @returns {Logger}
|
|
1939
|
+
*/
|
|
1940
|
+
get logger() {
|
|
1941
|
+
return this._logger;
|
|
1942
|
+
}
|
|
1943
|
+
/**
|
|
1944
|
+
* Sets the logger
|
|
1945
|
+
* @param {Logger} logger
|
|
1946
|
+
*/
|
|
1947
|
+
set logger(logger) {
|
|
1948
|
+
this._logger = logger;
|
|
1949
|
+
}
|
|
1950
|
+
/**
|
|
1951
|
+
* Gets whether an error should be thrown when an emit throws an error. Default is false and only emits an error event.
|
|
1952
|
+
* @returns {boolean}
|
|
1953
|
+
*/
|
|
1954
|
+
get throwOnEmitError() {
|
|
1955
|
+
return this._throwOnEmitError;
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Sets whether an error should be thrown when an emit throws an error. Default is false and only emits an error event.
|
|
1959
|
+
* @param {boolean} value
|
|
1960
|
+
*/
|
|
1961
|
+
set throwOnEmitError(value) {
|
|
1962
|
+
this._throwOnEmitError = value;
|
|
1963
|
+
}
|
|
1964
|
+
/**
|
|
1965
|
+
* Gets whether an error should be thrown when emitting 'error' event with no listeners. Default is false.
|
|
1966
|
+
* @returns {boolean}
|
|
1967
|
+
*/
|
|
1968
|
+
get throwOnEmptyListeners() {
|
|
1969
|
+
return this._throwOnEmptyListeners;
|
|
1970
|
+
}
|
|
1971
|
+
/**
|
|
1972
|
+
* Sets whether an error should be thrown when emitting 'error' event with no listeners. Default is false.
|
|
1973
|
+
* @param {boolean} value
|
|
1974
|
+
*/
|
|
1975
|
+
set throwOnEmptyListeners(value) {
|
|
1976
|
+
this._throwOnEmptyListeners = value;
|
|
1836
1977
|
}
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1978
|
+
/**
|
|
1979
|
+
* Adds a handler function for a specific event that will run only once
|
|
1980
|
+
* @param {string | symbol} eventName
|
|
1981
|
+
* @param {EventListener} listener
|
|
1982
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
1983
|
+
*/
|
|
1984
|
+
once(eventName, listener) {
|
|
1985
|
+
const onceListener = (...arguments_) => {
|
|
1986
|
+
this.off(eventName, onceListener);
|
|
1987
|
+
listener(...arguments_);
|
|
1840
1988
|
};
|
|
1841
|
-
|
|
1989
|
+
this.on(eventName, onceListener);
|
|
1990
|
+
return this;
|
|
1842
1991
|
}
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1992
|
+
/**
|
|
1993
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
1994
|
+
* @param {string} eventName The event name. Not required
|
|
1995
|
+
* @returns {number} The number of listeners
|
|
1996
|
+
*/
|
|
1997
|
+
listenerCount(eventName) {
|
|
1998
|
+
if (eventName === void 0) {
|
|
1999
|
+
return this.getAllListeners().length;
|
|
2000
|
+
}
|
|
2001
|
+
const listeners = this._eventListeners.get(eventName);
|
|
2002
|
+
return listeners ? listeners.length : 0;
|
|
1847
2003
|
}
|
|
2004
|
+
/**
|
|
2005
|
+
* Gets an array of event names
|
|
2006
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
2007
|
+
*/
|
|
1848
2008
|
eventNames() {
|
|
1849
2009
|
return [...this._eventListeners.keys()];
|
|
1850
2010
|
}
|
|
1851
|
-
|
|
1852
|
-
|
|
2011
|
+
/**
|
|
2012
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
2013
|
+
* @param {string} [event] (Optional) The event name
|
|
2014
|
+
* @returns {EventListener[]} An array of listeners
|
|
2015
|
+
*/
|
|
2016
|
+
rawListeners(event) {
|
|
2017
|
+
if (event === void 0) {
|
|
2018
|
+
return this.getAllListeners();
|
|
2019
|
+
}
|
|
2020
|
+
return this._eventListeners.get(event) ?? [];
|
|
1853
2021
|
}
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
2022
|
+
/**
|
|
2023
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
2024
|
+
* @param {string | symbol} eventName
|
|
2025
|
+
* @param {EventListener} listener
|
|
2026
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
2027
|
+
*/
|
|
2028
|
+
prependListener(eventName, listener) {
|
|
2029
|
+
const listeners = this._eventListeners.get(eventName) ?? [];
|
|
2030
|
+
listeners.unshift(listener);
|
|
2031
|
+
this._eventListeners.set(eventName, listeners);
|
|
2032
|
+
return this;
|
|
1857
2033
|
}
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
2034
|
+
/**
|
|
2035
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
2036
|
+
* @param {string | symbol} eventName
|
|
2037
|
+
* @param {EventListener} listener
|
|
2038
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
2039
|
+
*/
|
|
2040
|
+
prependOnceListener(eventName, listener) {
|
|
2041
|
+
const onceListener = (...arguments_) => {
|
|
2042
|
+
this.off(eventName, onceListener);
|
|
2043
|
+
listener(...arguments_);
|
|
1861
2044
|
};
|
|
1862
|
-
|
|
2045
|
+
this.prependListener(eventName, onceListener);
|
|
2046
|
+
return this;
|
|
1863
2047
|
}
|
|
2048
|
+
/**
|
|
2049
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
2050
|
+
* @returns {number} The maximum number of listeners
|
|
2051
|
+
*/
|
|
1864
2052
|
maxListeners() {
|
|
1865
2053
|
return this._maxListeners;
|
|
1866
2054
|
}
|
|
1867
|
-
|
|
1868
|
-
|
|
2055
|
+
/**
|
|
2056
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
2057
|
+
* @param {string | symbol} event
|
|
2058
|
+
* @param {EventListener} listener
|
|
2059
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
2060
|
+
*/
|
|
2061
|
+
addListener(event, listener) {
|
|
2062
|
+
this.on(event, listener);
|
|
2063
|
+
return this;
|
|
2064
|
+
}
|
|
2065
|
+
/**
|
|
2066
|
+
* Adds a listener for a specific event
|
|
2067
|
+
* @param {string | symbol} event
|
|
2068
|
+
* @param {EventListener} listener
|
|
2069
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
2070
|
+
*/
|
|
2071
|
+
on(event, listener) {
|
|
2072
|
+
if (!this._eventListeners.has(event)) {
|
|
2073
|
+
this._eventListeners.set(event, []);
|
|
2074
|
+
}
|
|
2075
|
+
const listeners = this._eventListeners.get(event);
|
|
2076
|
+
if (listeners) {
|
|
2077
|
+
if (listeners.length >= this._maxListeners) {
|
|
2078
|
+
console.warn(
|
|
2079
|
+
`MaxListenersExceededWarning: Possible event memory leak detected. ${listeners.length + 1} ${event} listeners added. Use setMaxListeners() to increase limit.`
|
|
2080
|
+
);
|
|
2081
|
+
}
|
|
2082
|
+
listeners.push(listener);
|
|
2083
|
+
}
|
|
2084
|
+
return this;
|
|
2085
|
+
}
|
|
2086
|
+
/**
|
|
2087
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
2088
|
+
* @param {string | symbol} event
|
|
2089
|
+
* @param {EventListener} listener
|
|
2090
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
2091
|
+
*/
|
|
2092
|
+
removeListener(event, listener) {
|
|
2093
|
+
this.off(event, listener);
|
|
2094
|
+
return this;
|
|
2095
|
+
}
|
|
2096
|
+
/**
|
|
2097
|
+
* Removes a listener for a specific event
|
|
2098
|
+
* @param {string | symbol} event
|
|
2099
|
+
* @param {EventListener} listener
|
|
2100
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
2101
|
+
*/
|
|
2102
|
+
off(event, listener) {
|
|
2103
|
+
const listeners = this._eventListeners.get(event) ?? [];
|
|
2104
|
+
const index = listeners.indexOf(listener);
|
|
2105
|
+
if (index !== -1) {
|
|
2106
|
+
listeners.splice(index, 1);
|
|
2107
|
+
}
|
|
2108
|
+
if (listeners.length === 0) {
|
|
2109
|
+
this._eventListeners.delete(event);
|
|
2110
|
+
}
|
|
2111
|
+
return this;
|
|
2112
|
+
}
|
|
2113
|
+
/**
|
|
2114
|
+
* Calls all listeners for a specific event
|
|
2115
|
+
* @param {string | symbol} event
|
|
2116
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
2117
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
2118
|
+
*/
|
|
2119
|
+
emit(event, ...arguments_) {
|
|
2120
|
+
let result = false;
|
|
2121
|
+
const listeners = this._eventListeners.get(event);
|
|
2122
|
+
if (listeners && listeners.length > 0) {
|
|
2123
|
+
for (const listener of listeners) {
|
|
2124
|
+
listener(...arguments_);
|
|
2125
|
+
result = true;
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
if (event === this._errorEvent) {
|
|
2129
|
+
const error = arguments_[0] instanceof Error ? arguments_[0] : new Error(`${arguments_[0]}`);
|
|
2130
|
+
if (this._throwOnEmitError && !result) {
|
|
2131
|
+
throw error;
|
|
2132
|
+
} else {
|
|
2133
|
+
if (this.listeners(this._errorEvent).length === 0 && this._throwOnEmptyListeners === true) {
|
|
2134
|
+
throw error;
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
return result;
|
|
2139
|
+
}
|
|
2140
|
+
/**
|
|
2141
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
2142
|
+
* @param {string} [event] (Optional) The event name
|
|
2143
|
+
* @returns {EventListener[]} An array of listeners
|
|
2144
|
+
*/
|
|
2145
|
+
listeners(event) {
|
|
2146
|
+
return this._eventListeners.get(event) ?? [];
|
|
2147
|
+
}
|
|
2148
|
+
/**
|
|
2149
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
2150
|
+
* @param {string} [event] (Optional) The event name
|
|
2151
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
2152
|
+
*/
|
|
2153
|
+
removeAllListeners(event) {
|
|
2154
|
+
if (event !== void 0) {
|
|
2155
|
+
this._eventListeners.delete(event);
|
|
2156
|
+
} else {
|
|
2157
|
+
this._eventListeners.clear();
|
|
2158
|
+
}
|
|
2159
|
+
return this;
|
|
2160
|
+
}
|
|
2161
|
+
/**
|
|
2162
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
2163
|
+
* @param {number} n The maximum number of listeners
|
|
2164
|
+
* @returns {void}
|
|
2165
|
+
*/
|
|
2166
|
+
setMaxListeners(n) {
|
|
2167
|
+
this._maxListeners = n;
|
|
2168
|
+
for (const listeners of this._eventListeners.values()) {
|
|
2169
|
+
if (listeners.length > n) {
|
|
2170
|
+
listeners.splice(n);
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
/**
|
|
2175
|
+
* Gets all listeners
|
|
2176
|
+
* @returns {EventListener[]} An array of listeners
|
|
2177
|
+
*/
|
|
2178
|
+
getAllListeners() {
|
|
2179
|
+
let result = [];
|
|
2180
|
+
for (const listeners of this._eventListeners.values()) {
|
|
2181
|
+
result = [...result, ...listeners];
|
|
2182
|
+
}
|
|
2183
|
+
return result;
|
|
2184
|
+
}
|
|
2185
|
+
};
|
|
2186
|
+
var Hookified = class extends Eventified {
|
|
2187
|
+
_hooks;
|
|
2188
|
+
_throwOnHookError = false;
|
|
2189
|
+
_enforceBeforeAfter = false;
|
|
2190
|
+
_deprecatedHooks;
|
|
2191
|
+
_allowDeprecated = true;
|
|
2192
|
+
constructor(options) {
|
|
2193
|
+
super({
|
|
2194
|
+
logger: options?.logger,
|
|
2195
|
+
throwOnEmitError: options?.throwOnEmitError,
|
|
2196
|
+
throwOnEmptyListeners: options?.throwOnEmptyListeners
|
|
2197
|
+
});
|
|
2198
|
+
this._hooks = /* @__PURE__ */ new Map();
|
|
2199
|
+
this._deprecatedHooks = options?.deprecatedHooks ? new Map(options.deprecatedHooks) : /* @__PURE__ */ new Map();
|
|
2200
|
+
if (options?.throwOnHookError !== void 0) {
|
|
2201
|
+
this._throwOnHookError = options.throwOnHookError;
|
|
2202
|
+
} else if (options?.throwHookErrors !== void 0) {
|
|
2203
|
+
this._throwOnHookError = options.throwHookErrors;
|
|
2204
|
+
}
|
|
2205
|
+
if (options?.enforceBeforeAfter !== void 0) {
|
|
2206
|
+
this._enforceBeforeAfter = options.enforceBeforeAfter;
|
|
2207
|
+
}
|
|
2208
|
+
if (options?.allowDeprecated !== void 0) {
|
|
2209
|
+
this._allowDeprecated = options.allowDeprecated;
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
/**
|
|
2213
|
+
* Gets all hooks
|
|
2214
|
+
* @returns {Map<string, Hook[]>}
|
|
2215
|
+
*/
|
|
2216
|
+
get hooks() {
|
|
2217
|
+
return this._hooks;
|
|
2218
|
+
}
|
|
2219
|
+
/**
|
|
2220
|
+
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
2221
|
+
* @returns {boolean}
|
|
2222
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
2223
|
+
*/
|
|
2224
|
+
get throwHookErrors() {
|
|
2225
|
+
return this._throwOnHookError;
|
|
2226
|
+
}
|
|
2227
|
+
/**
|
|
2228
|
+
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
2229
|
+
* @param {boolean} value
|
|
2230
|
+
* @deprecated - this will be deprecated in version 2. Please use throwOnHookError.
|
|
2231
|
+
*/
|
|
2232
|
+
set throwHookErrors(value) {
|
|
2233
|
+
this._throwOnHookError = value;
|
|
2234
|
+
}
|
|
2235
|
+
/**
|
|
2236
|
+
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
2237
|
+
* @returns {boolean}
|
|
2238
|
+
*/
|
|
2239
|
+
get throwOnHookError() {
|
|
2240
|
+
return this._throwOnHookError;
|
|
2241
|
+
}
|
|
2242
|
+
/**
|
|
2243
|
+
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
2244
|
+
* @param {boolean} value
|
|
2245
|
+
*/
|
|
2246
|
+
set throwOnHookError(value) {
|
|
2247
|
+
this._throwOnHookError = value;
|
|
2248
|
+
}
|
|
2249
|
+
/**
|
|
2250
|
+
* Gets whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
2251
|
+
* @returns {boolean}
|
|
2252
|
+
* @default false
|
|
2253
|
+
*/
|
|
2254
|
+
get enforceBeforeAfter() {
|
|
2255
|
+
return this._enforceBeforeAfter;
|
|
2256
|
+
}
|
|
2257
|
+
/**
|
|
2258
|
+
* Sets whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
2259
|
+
* @param {boolean} value
|
|
2260
|
+
*/
|
|
2261
|
+
set enforceBeforeAfter(value) {
|
|
2262
|
+
this._enforceBeforeAfter = value;
|
|
2263
|
+
}
|
|
2264
|
+
/**
|
|
2265
|
+
* Gets the map of deprecated hook names to deprecation messages.
|
|
2266
|
+
* @returns {Map<string, string>}
|
|
2267
|
+
*/
|
|
2268
|
+
get deprecatedHooks() {
|
|
2269
|
+
return this._deprecatedHooks;
|
|
2270
|
+
}
|
|
2271
|
+
/**
|
|
2272
|
+
* Sets the map of deprecated hook names to deprecation messages.
|
|
2273
|
+
* @param {Map<string, string>} value
|
|
2274
|
+
*/
|
|
2275
|
+
set deprecatedHooks(value) {
|
|
2276
|
+
this._deprecatedHooks = value;
|
|
2277
|
+
}
|
|
2278
|
+
/**
|
|
2279
|
+
* Gets whether deprecated hooks are allowed to be registered and executed. Default is true.
|
|
2280
|
+
* @returns {boolean}
|
|
2281
|
+
*/
|
|
2282
|
+
get allowDeprecated() {
|
|
2283
|
+
return this._allowDeprecated;
|
|
2284
|
+
}
|
|
2285
|
+
/**
|
|
2286
|
+
* Sets whether deprecated hooks are allowed to be registered and executed. Default is true.
|
|
2287
|
+
* @param {boolean} value
|
|
2288
|
+
*/
|
|
2289
|
+
set allowDeprecated(value) {
|
|
2290
|
+
this._allowDeprecated = value;
|
|
2291
|
+
}
|
|
2292
|
+
/**
|
|
2293
|
+
* Validates hook event name if enforceBeforeAfter is enabled
|
|
2294
|
+
* @param {string} event - The event name to validate
|
|
2295
|
+
* @throws {Error} If enforceBeforeAfter is true and event doesn't start with 'before' or 'after'
|
|
2296
|
+
*/
|
|
2297
|
+
validateHookName(event) {
|
|
2298
|
+
if (this._enforceBeforeAfter) {
|
|
2299
|
+
const eventValue = event.trim().toLocaleLowerCase();
|
|
2300
|
+
if (!eventValue.startsWith("before") && !eventValue.startsWith("after")) {
|
|
2301
|
+
throw new Error(
|
|
2302
|
+
`Hook event "${event}" must start with "before" or "after" when enforceBeforeAfter is enabled`
|
|
2303
|
+
);
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
/**
|
|
2308
|
+
* Checks if a hook is deprecated and emits a warning if it is
|
|
2309
|
+
* @param {string} event - The event name to check
|
|
2310
|
+
* @returns {boolean} - Returns true if the hook should proceed, false if it should be blocked
|
|
2311
|
+
*/
|
|
2312
|
+
checkDeprecatedHook(event) {
|
|
2313
|
+
if (this._deprecatedHooks.has(event)) {
|
|
2314
|
+
const message = this._deprecatedHooks.get(event);
|
|
2315
|
+
const warningMessage = `Hook "${event}" is deprecated${message ? `: ${message}` : ""}`;
|
|
2316
|
+
this.emit("warn", { hook: event, message: warningMessage });
|
|
2317
|
+
if (this.logger?.warn) {
|
|
2318
|
+
this.logger.warn(warningMessage);
|
|
2319
|
+
}
|
|
2320
|
+
return this._allowDeprecated;
|
|
2321
|
+
}
|
|
2322
|
+
return true;
|
|
1869
2323
|
}
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
2324
|
+
/**
|
|
2325
|
+
* Adds a handler function for a specific event
|
|
2326
|
+
* @param {string} event
|
|
2327
|
+
* @param {Hook} handler - this can be async or sync
|
|
2328
|
+
* @returns {void}
|
|
2329
|
+
*/
|
|
2330
|
+
onHook(event, handler) {
|
|
2331
|
+
this.validateHookName(event);
|
|
2332
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
2333
|
+
return;
|
|
2334
|
+
}
|
|
2335
|
+
const eventHandlers = this._hooks.get(event);
|
|
2336
|
+
if (eventHandlers) {
|
|
2337
|
+
eventHandlers.push(handler);
|
|
2338
|
+
} else {
|
|
2339
|
+
this._hooks.set(event, [handler]);
|
|
2340
|
+
}
|
|
1874
2341
|
}
|
|
1875
|
-
|
|
1876
|
-
|
|
2342
|
+
/**
|
|
2343
|
+
* Adds a handler function for a specific event that runs before all other handlers
|
|
2344
|
+
* @param {HookEntry} hookEntry
|
|
2345
|
+
* @returns {void}
|
|
2346
|
+
*/
|
|
2347
|
+
onHookEntry(hookEntry) {
|
|
2348
|
+
this.onHook(hookEntry.event, hookEntry.handler);
|
|
2349
|
+
}
|
|
2350
|
+
/**
|
|
2351
|
+
* Alias for onHook. This is provided for compatibility with other libraries that use the `addHook` method.
|
|
2352
|
+
* @param {string} event
|
|
2353
|
+
* @param {Hook} handler - this can be async or sync
|
|
2354
|
+
* @returns {void}
|
|
2355
|
+
*/
|
|
2356
|
+
addHook(event, handler) {
|
|
2357
|
+
this.onHook(event, handler);
|
|
2358
|
+
}
|
|
2359
|
+
/**
|
|
2360
|
+
* Adds a handler function for a specific event
|
|
2361
|
+
* @param {Array<HookEntry>} hooks
|
|
2362
|
+
* @returns {void}
|
|
2363
|
+
*/
|
|
2364
|
+
onHooks(hooks) {
|
|
2365
|
+
for (const hook of hooks) {
|
|
2366
|
+
this.onHook(hook.event, hook.handler);
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
/**
|
|
2370
|
+
* Adds a handler function for a specific event that runs before all other handlers
|
|
2371
|
+
* @param {string} event
|
|
2372
|
+
* @param {Hook} handler - this can be async or sync
|
|
2373
|
+
* @returns {void}
|
|
2374
|
+
*/
|
|
2375
|
+
prependHook(event, handler) {
|
|
2376
|
+
this.validateHookName(event);
|
|
2377
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
2378
|
+
return;
|
|
2379
|
+
}
|
|
2380
|
+
const eventHandlers = this._hooks.get(event);
|
|
2381
|
+
if (eventHandlers) {
|
|
2382
|
+
eventHandlers.unshift(handler);
|
|
2383
|
+
} else {
|
|
2384
|
+
this._hooks.set(event, [handler]);
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
/**
|
|
2388
|
+
* Adds a handler that only executes once for a specific event before all other handlers
|
|
2389
|
+
* @param event
|
|
2390
|
+
* @param handler
|
|
2391
|
+
*/
|
|
2392
|
+
prependOnceHook(event, handler) {
|
|
2393
|
+
this.validateHookName(event);
|
|
2394
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
2395
|
+
return;
|
|
2396
|
+
}
|
|
2397
|
+
const hook = async (...arguments_) => {
|
|
2398
|
+
this.removeHook(event, hook);
|
|
2399
|
+
return handler(...arguments_);
|
|
2400
|
+
};
|
|
2401
|
+
this.prependHook(event, hook);
|
|
2402
|
+
}
|
|
2403
|
+
/**
|
|
2404
|
+
* Adds a handler that only executes once for a specific event
|
|
2405
|
+
* @param event
|
|
2406
|
+
* @param handler
|
|
2407
|
+
*/
|
|
2408
|
+
onceHook(event, handler) {
|
|
2409
|
+
this.validateHookName(event);
|
|
2410
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
2411
|
+
return;
|
|
2412
|
+
}
|
|
2413
|
+
const hook = async (...arguments_) => {
|
|
2414
|
+
this.removeHook(event, hook);
|
|
2415
|
+
return handler(...arguments_);
|
|
2416
|
+
};
|
|
2417
|
+
this.onHook(event, hook);
|
|
2418
|
+
}
|
|
2419
|
+
/**
|
|
2420
|
+
* Removes a handler function for a specific event
|
|
2421
|
+
* @param {string} event
|
|
2422
|
+
* @param {Hook} handler
|
|
2423
|
+
* @returns {void}
|
|
2424
|
+
*/
|
|
2425
|
+
removeHook(event, handler) {
|
|
2426
|
+
this.validateHookName(event);
|
|
2427
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
2428
|
+
return;
|
|
2429
|
+
}
|
|
2430
|
+
const eventHandlers = this._hooks.get(event);
|
|
2431
|
+
if (eventHandlers) {
|
|
2432
|
+
const index = eventHandlers.indexOf(handler);
|
|
2433
|
+
if (index !== -1) {
|
|
2434
|
+
eventHandlers.splice(index, 1);
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
/**
|
|
2439
|
+
* Removes all handlers for a specific event
|
|
2440
|
+
* @param {Array<HookEntry>} hooks
|
|
2441
|
+
* @returns {void}
|
|
2442
|
+
*/
|
|
2443
|
+
removeHooks(hooks) {
|
|
2444
|
+
for (const hook of hooks) {
|
|
2445
|
+
this.removeHook(hook.event, hook.handler);
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
/**
|
|
2449
|
+
* Calls all handlers for a specific event
|
|
2450
|
+
* @param {string} event
|
|
2451
|
+
* @param {T[]} arguments_
|
|
2452
|
+
* @returns {Promise<void>}
|
|
2453
|
+
*/
|
|
2454
|
+
async hook(event, ...arguments_) {
|
|
2455
|
+
this.validateHookName(event);
|
|
2456
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
2457
|
+
return;
|
|
2458
|
+
}
|
|
2459
|
+
const eventHandlers = this._hooks.get(event);
|
|
2460
|
+
if (eventHandlers) {
|
|
2461
|
+
for (const handler of eventHandlers) {
|
|
2462
|
+
try {
|
|
2463
|
+
await handler(...arguments_);
|
|
2464
|
+
} catch (error) {
|
|
2465
|
+
const message = `${event}: ${error.message}`;
|
|
2466
|
+
this.emit("error", new Error(message));
|
|
2467
|
+
if (this.logger) {
|
|
2468
|
+
this.logger.error(message);
|
|
2469
|
+
}
|
|
2470
|
+
if (this._throwOnHookError) {
|
|
2471
|
+
throw new Error(message);
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2477
|
+
/**
|
|
2478
|
+
* Prepends the word `before` to your hook. Example is event is `test`, the before hook is `before:test`.
|
|
2479
|
+
* @param {string} event - The event name
|
|
2480
|
+
* @param {T[]} arguments_ - The arguments to pass to the hook
|
|
2481
|
+
*/
|
|
2482
|
+
async beforeHook(event, ...arguments_) {
|
|
2483
|
+
await this.hook(`before:${event}`, ...arguments_);
|
|
2484
|
+
}
|
|
2485
|
+
/**
|
|
2486
|
+
* Prepends the word `after` to your hook. Example is event is `test`, the after hook is `after:test`.
|
|
2487
|
+
* @param {string} event - The event name
|
|
2488
|
+
* @param {T[]} arguments_ - The arguments to pass to the hook
|
|
2489
|
+
*/
|
|
2490
|
+
async afterHook(event, ...arguments_) {
|
|
2491
|
+
await this.hook(`after:${event}`, ...arguments_);
|
|
2492
|
+
}
|
|
2493
|
+
/**
|
|
2494
|
+
* Calls all handlers for a specific event. This is an alias for `hook` and is provided for
|
|
2495
|
+
* compatibility with other libraries that use the `callHook` method.
|
|
2496
|
+
* @param {string} event
|
|
2497
|
+
* @param {T[]} arguments_
|
|
2498
|
+
* @returns {Promise<void>}
|
|
2499
|
+
*/
|
|
2500
|
+
async callHook(event, ...arguments_) {
|
|
2501
|
+
await this.hook(event, ...arguments_);
|
|
2502
|
+
}
|
|
2503
|
+
/**
|
|
2504
|
+
* Gets all hooks for a specific event
|
|
2505
|
+
* @param {string} event
|
|
2506
|
+
* @returns {Hook[]}
|
|
2507
|
+
*/
|
|
2508
|
+
getHooks(event) {
|
|
2509
|
+
this.validateHookName(event);
|
|
2510
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
2511
|
+
return void 0;
|
|
2512
|
+
}
|
|
2513
|
+
return this._hooks.get(event);
|
|
1877
2514
|
}
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
2515
|
+
/**
|
|
2516
|
+
* Removes all hooks
|
|
2517
|
+
* @returns {void}
|
|
2518
|
+
*/
|
|
2519
|
+
clearHooks() {
|
|
2520
|
+
this._hooks.clear();
|
|
2521
|
+
}
|
|
2522
|
+
};
|
|
2523
|
+
|
|
2524
|
+
// node_modules/hashery/dist/node/index.js
|
|
2525
|
+
var CRC = class {
|
|
2526
|
+
get name() {
|
|
2527
|
+
return "crc32";
|
|
2528
|
+
}
|
|
2529
|
+
toHashSync(data) {
|
|
2530
|
+
let bytes;
|
|
2531
|
+
if (data instanceof Uint8Array) {
|
|
2532
|
+
bytes = data;
|
|
2533
|
+
} else if (data instanceof ArrayBuffer) {
|
|
2534
|
+
bytes = new Uint8Array(data);
|
|
2535
|
+
} else if (data instanceof DataView) {
|
|
2536
|
+
bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
2537
|
+
} else {
|
|
2538
|
+
const view = data;
|
|
2539
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
2540
|
+
}
|
|
2541
|
+
const CRC32_POLYNOMIAL = 3988292384;
|
|
2542
|
+
let crc = 4294967295;
|
|
2543
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2544
|
+
crc = crc ^ bytes[i];
|
|
2545
|
+
for (let j = 0; j < 8; j++) {
|
|
2546
|
+
crc = crc >>> 1 ^ CRC32_POLYNOMIAL & -(crc & 1);
|
|
2547
|
+
}
|
|
2548
|
+
}
|
|
2549
|
+
crc = (crc ^ 4294967295) >>> 0;
|
|
2550
|
+
const hashHex = crc.toString(16).padStart(8, "0");
|
|
2551
|
+
return hashHex;
|
|
2552
|
+
}
|
|
2553
|
+
async toHash(data) {
|
|
2554
|
+
return this.toHashSync(data);
|
|
2555
|
+
}
|
|
2556
|
+
};
|
|
2557
|
+
var WebCrypto = class {
|
|
2558
|
+
_algorithm = "SHA-256";
|
|
2559
|
+
constructor(options) {
|
|
2560
|
+
if (options?.algorithm) {
|
|
2561
|
+
this._algorithm = options?.algorithm;
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
get name() {
|
|
2565
|
+
return this._algorithm;
|
|
2566
|
+
}
|
|
2567
|
+
async toHash(data) {
|
|
2568
|
+
const hashBuffer = await crypto.subtle.digest(this._algorithm, data);
|
|
2569
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
2570
|
+
const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
2571
|
+
return hashHex;
|
|
2572
|
+
}
|
|
2573
|
+
};
|
|
2574
|
+
var DJB2 = class {
|
|
2575
|
+
/**
|
|
2576
|
+
* The name identifier for this hash provider.
|
|
2577
|
+
*/
|
|
2578
|
+
get name() {
|
|
2579
|
+
return "djb2";
|
|
2580
|
+
}
|
|
2581
|
+
/**
|
|
2582
|
+
* Computes the DJB2 hash of the provided data synchronously.
|
|
2583
|
+
*
|
|
2584
|
+
* @param data - The data to hash (Uint8Array, ArrayBuffer, or DataView)
|
|
2585
|
+
* @returns An 8-character lowercase hexadecimal string
|
|
2586
|
+
*
|
|
2587
|
+
* @example
|
|
2588
|
+
* ```typescript
|
|
2589
|
+
* const djb2 = new DJB2();
|
|
2590
|
+
* const data = new TextEncoder().encode('hello');
|
|
2591
|
+
* const hash = djb2.toHashSync(data);
|
|
2592
|
+
* console.log(hash); // "7c9df5ea"
|
|
2593
|
+
* ```
|
|
2594
|
+
*/
|
|
2595
|
+
toHashSync(data) {
|
|
2596
|
+
let bytes;
|
|
2597
|
+
if (data instanceof Uint8Array) {
|
|
2598
|
+
bytes = data;
|
|
2599
|
+
} else if (data instanceof ArrayBuffer) {
|
|
2600
|
+
bytes = new Uint8Array(data);
|
|
2601
|
+
} else if (data instanceof DataView) {
|
|
2602
|
+
bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
2603
|
+
} else {
|
|
2604
|
+
const view = data;
|
|
2605
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
2606
|
+
}
|
|
2607
|
+
let hash2 = 5381;
|
|
2608
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2609
|
+
hash2 = (hash2 << 5) + hash2 + bytes[i];
|
|
2610
|
+
hash2 = hash2 >>> 0;
|
|
2611
|
+
}
|
|
2612
|
+
const hashHex = hash2.toString(16).padStart(8, "0");
|
|
2613
|
+
return hashHex;
|
|
2614
|
+
}
|
|
2615
|
+
/**
|
|
2616
|
+
* Computes the DJB2 hash of the provided data.
|
|
2617
|
+
*
|
|
2618
|
+
* @param data - The data to hash (Uint8Array, ArrayBuffer, or DataView)
|
|
2619
|
+
* @returns A Promise resolving to an 8-character lowercase hexadecimal string
|
|
2620
|
+
*
|
|
2621
|
+
* @example
|
|
2622
|
+
* ```typescript
|
|
2623
|
+
* const djb2 = new DJB2();
|
|
2624
|
+
* const data = new TextEncoder().encode('hello');
|
|
2625
|
+
* const hash = await djb2.toHash(data);
|
|
2626
|
+
* console.log(hash); // "7c9df5ea"
|
|
2627
|
+
* ```
|
|
2628
|
+
*/
|
|
2629
|
+
async toHash(data) {
|
|
2630
|
+
return this.toHashSync(data);
|
|
2631
|
+
}
|
|
2632
|
+
};
|
|
2633
|
+
var FNV1 = class {
|
|
2634
|
+
/**
|
|
2635
|
+
* The name identifier for this hash provider.
|
|
2636
|
+
*/
|
|
2637
|
+
get name() {
|
|
2638
|
+
return "fnv1";
|
|
2639
|
+
}
|
|
2640
|
+
/**
|
|
2641
|
+
* Computes the FNV-1 hash of the provided data synchronously.
|
|
2642
|
+
*
|
|
2643
|
+
* @param data - The data to hash (Uint8Array, ArrayBuffer, or DataView)
|
|
2644
|
+
* @returns An 8-character lowercase hexadecimal string
|
|
2645
|
+
*/
|
|
2646
|
+
toHashSync(data) {
|
|
2647
|
+
let bytes;
|
|
2648
|
+
if (data instanceof Uint8Array) {
|
|
2649
|
+
bytes = data;
|
|
2650
|
+
} else if (data instanceof ArrayBuffer) {
|
|
2651
|
+
bytes = new Uint8Array(data);
|
|
2652
|
+
} else if (data instanceof DataView) {
|
|
2653
|
+
bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
2654
|
+
} else {
|
|
2655
|
+
const view = data;
|
|
2656
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
2657
|
+
}
|
|
2658
|
+
const FNV_OFFSET_BASIS = 2166136261;
|
|
2659
|
+
const FNV_PRIME = 16777619;
|
|
2660
|
+
let hash2 = FNV_OFFSET_BASIS;
|
|
2661
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2662
|
+
hash2 = hash2 * FNV_PRIME;
|
|
2663
|
+
hash2 = hash2 ^ bytes[i];
|
|
2664
|
+
hash2 = hash2 >>> 0;
|
|
2665
|
+
}
|
|
2666
|
+
const hashHex = hash2.toString(16).padStart(8, "0");
|
|
2667
|
+
return hashHex;
|
|
2668
|
+
}
|
|
2669
|
+
/**
|
|
2670
|
+
* Computes the FNV-1 hash of the provided data.
|
|
2671
|
+
*
|
|
2672
|
+
* @param data - The data to hash (Uint8Array, ArrayBuffer, or DataView)
|
|
2673
|
+
* @returns A Promise resolving to an 8-character lowercase hexadecimal string
|
|
2674
|
+
*/
|
|
2675
|
+
async toHash(data) {
|
|
2676
|
+
return this.toHashSync(data);
|
|
2677
|
+
}
|
|
2678
|
+
};
|
|
2679
|
+
var Murmer = class {
|
|
2680
|
+
_seed;
|
|
2681
|
+
/**
|
|
2682
|
+
* Creates a new Murmer instance.
|
|
2683
|
+
*
|
|
2684
|
+
* @param seed - Optional seed value for the hash (default: 0)
|
|
2685
|
+
*/
|
|
2686
|
+
constructor(seed = 0) {
|
|
2687
|
+
this._seed = seed >>> 0;
|
|
2688
|
+
}
|
|
2689
|
+
/**
|
|
2690
|
+
* The name identifier for this hash provider.
|
|
2691
|
+
*/
|
|
2692
|
+
get name() {
|
|
2693
|
+
return "murmer";
|
|
2694
|
+
}
|
|
2695
|
+
/**
|
|
2696
|
+
* Gets the current seed value used for hashing.
|
|
2697
|
+
*/
|
|
2698
|
+
get seed() {
|
|
2699
|
+
return this._seed;
|
|
2700
|
+
}
|
|
2701
|
+
/**
|
|
2702
|
+
* Computes the Murmer 32-bit hash of the provided data synchronously.
|
|
2703
|
+
*
|
|
2704
|
+
* @param data - The data to hash (Uint8Array, ArrayBuffer, or DataView)
|
|
2705
|
+
* @returns An 8-character lowercase hexadecimal string
|
|
2706
|
+
*
|
|
2707
|
+
* @example
|
|
2708
|
+
* ```typescript
|
|
2709
|
+
* const murmer = new Murmer();
|
|
2710
|
+
* const data = new TextEncoder().encode('hello');
|
|
2711
|
+
* const hash = murmer.toHashSync(data);
|
|
2712
|
+
* console.log(hash); // "248bfa47"
|
|
2713
|
+
* ```
|
|
2714
|
+
*/
|
|
2715
|
+
toHashSync(data) {
|
|
2716
|
+
let bytes;
|
|
2717
|
+
if (data instanceof Uint8Array) {
|
|
2718
|
+
bytes = data;
|
|
2719
|
+
} else if (data instanceof ArrayBuffer) {
|
|
2720
|
+
bytes = new Uint8Array(data);
|
|
2721
|
+
} else if (data instanceof DataView) {
|
|
2722
|
+
bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
2723
|
+
} else {
|
|
2724
|
+
const view = data;
|
|
2725
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
2726
|
+
}
|
|
2727
|
+
const c1 = 3432918353;
|
|
2728
|
+
const c2 = 461845907;
|
|
2729
|
+
const length = bytes.length;
|
|
2730
|
+
const nblocks = Math.floor(length / 4);
|
|
2731
|
+
let h1 = this._seed;
|
|
2732
|
+
for (let i = 0; i < nblocks; i++) {
|
|
2733
|
+
const index = i * 4;
|
|
2734
|
+
let k12 = bytes[index] & 255 | (bytes[index + 1] & 255) << 8 | (bytes[index + 2] & 255) << 16 | (bytes[index + 3] & 255) << 24;
|
|
2735
|
+
k12 = this._imul(k12, c1);
|
|
2736
|
+
k12 = this._rotl32(k12, 15);
|
|
2737
|
+
k12 = this._imul(k12, c2);
|
|
2738
|
+
h1 ^= k12;
|
|
2739
|
+
h1 = this._rotl32(h1, 13);
|
|
2740
|
+
h1 = this._imul(h1, 5) + 3864292196;
|
|
2741
|
+
}
|
|
2742
|
+
const tail = nblocks * 4;
|
|
2743
|
+
let k1 = 0;
|
|
2744
|
+
switch (length & 3) {
|
|
2745
|
+
case 3:
|
|
2746
|
+
k1 ^= (bytes[tail + 2] & 255) << 16;
|
|
2747
|
+
// fallthrough
|
|
2748
|
+
case 2:
|
|
2749
|
+
k1 ^= (bytes[tail + 1] & 255) << 8;
|
|
2750
|
+
// fallthrough
|
|
2751
|
+
case 1:
|
|
2752
|
+
k1 ^= bytes[tail] & 255;
|
|
2753
|
+
k1 = this._imul(k1, c1);
|
|
2754
|
+
k1 = this._rotl32(k1, 15);
|
|
2755
|
+
k1 = this._imul(k1, c2);
|
|
2756
|
+
h1 ^= k1;
|
|
2757
|
+
}
|
|
2758
|
+
h1 ^= length;
|
|
2759
|
+
h1 ^= h1 >>> 16;
|
|
2760
|
+
h1 = this._imul(h1, 2246822507);
|
|
2761
|
+
h1 ^= h1 >>> 13;
|
|
2762
|
+
h1 = this._imul(h1, 3266489909);
|
|
2763
|
+
h1 ^= h1 >>> 16;
|
|
2764
|
+
h1 = h1 >>> 0;
|
|
2765
|
+
const hashHex = h1.toString(16).padStart(8, "0");
|
|
2766
|
+
return hashHex;
|
|
2767
|
+
}
|
|
2768
|
+
/**
|
|
2769
|
+
* Computes the Murmer 32-bit hash of the provided data.
|
|
2770
|
+
*
|
|
2771
|
+
* @param data - The data to hash (Uint8Array, ArrayBuffer, or DataView)
|
|
2772
|
+
* @returns A Promise resolving to an 8-character lowercase hexadecimal string
|
|
2773
|
+
*
|
|
2774
|
+
* @example
|
|
2775
|
+
* ```typescript
|
|
2776
|
+
* const murmer = new Murmer();
|
|
2777
|
+
* const data = new TextEncoder().encode('hello');
|
|
2778
|
+
* const hash = await murmer.toHash(data);
|
|
2779
|
+
* console.log(hash); // "248bfa47"
|
|
2780
|
+
* ```
|
|
2781
|
+
*/
|
|
2782
|
+
async toHash(data) {
|
|
2783
|
+
return this.toHashSync(data);
|
|
2784
|
+
}
|
|
2785
|
+
/**
|
|
2786
|
+
* 32-bit integer multiplication with proper overflow handling.
|
|
2787
|
+
* @private
|
|
2788
|
+
*/
|
|
2789
|
+
_imul(a, b) {
|
|
2790
|
+
if (Math.imul) {
|
|
2791
|
+
return Math.imul(a, b);
|
|
2792
|
+
}
|
|
2793
|
+
const ah = a >>> 16 & 65535;
|
|
2794
|
+
const al = a & 65535;
|
|
2795
|
+
const bh = b >>> 16 & 65535;
|
|
2796
|
+
const bl = b & 65535;
|
|
2797
|
+
return al * bl + (ah * bl + al * bh << 16 >>> 0) | 0;
|
|
2798
|
+
}
|
|
2799
|
+
/**
|
|
2800
|
+
* Left rotate a 32-bit integer.
|
|
2801
|
+
* @private
|
|
2802
|
+
*/
|
|
2803
|
+
_rotl32(x, r) {
|
|
2804
|
+
return x << r | x >>> 32 - r;
|
|
2805
|
+
}
|
|
2806
|
+
};
|
|
2807
|
+
var HashProviders = class {
|
|
2808
|
+
_providers = /* @__PURE__ */ new Map();
|
|
2809
|
+
_getFuzzy = true;
|
|
2810
|
+
/**
|
|
2811
|
+
* Creates a new HashProviders instance.
|
|
2812
|
+
* @param options - Optional configuration including initial providers to load
|
|
2813
|
+
* @example
|
|
2814
|
+
* ```ts
|
|
2815
|
+
* const providers = new HashProviders({
|
|
2816
|
+
* providers: [{ name: 'custom', toHash: async (data) => '...' }]
|
|
2817
|
+
* });
|
|
2818
|
+
* ```
|
|
2819
|
+
*/
|
|
2820
|
+
constructor(options) {
|
|
2821
|
+
if (options?.providers) {
|
|
2822
|
+
this.loadProviders(options?.providers);
|
|
2823
|
+
}
|
|
2824
|
+
if (options?.getFuzzy !== void 0) {
|
|
2825
|
+
this._getFuzzy = Boolean(options?.getFuzzy);
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
/**
|
|
2829
|
+
* Loads multiple hash providers at once.
|
|
2830
|
+
* Each provider is added to the internal map using its name as the key.
|
|
2831
|
+
* @param providers - Array of HashProvider objects to load
|
|
2832
|
+
* @example
|
|
2833
|
+
* ```ts
|
|
2834
|
+
* const providers = new HashProviders();
|
|
2835
|
+
* providers.loadProviders([
|
|
2836
|
+
* { name: 'md5', toHash: async (data) => '...' },
|
|
2837
|
+
* { name: 'sha1', toHash: async (data) => '...' }
|
|
2838
|
+
* ]);
|
|
2839
|
+
* ```
|
|
2840
|
+
*/
|
|
2841
|
+
loadProviders(providers) {
|
|
2842
|
+
for (const provider of providers) {
|
|
2843
|
+
this._providers.set(provider.name, provider);
|
|
2844
|
+
}
|
|
2845
|
+
}
|
|
2846
|
+
/**
|
|
2847
|
+
* Gets the internal Map of all registered hash providers.
|
|
2848
|
+
* @returns Map of provider names to HashProvider objects
|
|
2849
|
+
*/
|
|
2850
|
+
get providers() {
|
|
2851
|
+
return this._providers;
|
|
1881
2852
|
}
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
2853
|
+
/**
|
|
2854
|
+
* Sets the internal Map of hash providers, replacing all existing providers.
|
|
2855
|
+
* @param providers - Map of provider names to HashProvider objects
|
|
2856
|
+
*/
|
|
2857
|
+
set providers(providers) {
|
|
2858
|
+
this._providers = providers;
|
|
1886
2859
|
}
|
|
1887
|
-
|
|
1888
|
-
|
|
2860
|
+
/**
|
|
2861
|
+
* Gets an array of all provider names.
|
|
2862
|
+
* @returns Array of provider names
|
|
2863
|
+
* @example
|
|
2864
|
+
* ```ts
|
|
2865
|
+
* const providers = new HashProviders();
|
|
2866
|
+
* providers.add({ name: 'sha256', toHash: async (data) => '...' });
|
|
2867
|
+
* providers.add({ name: 'md5', toHash: async (data) => '...' });
|
|
2868
|
+
* console.log(providers.names); // ['sha256', 'md5']
|
|
2869
|
+
* ```
|
|
2870
|
+
*/
|
|
2871
|
+
get names() {
|
|
2872
|
+
return Array.from(this._providers.keys());
|
|
1889
2873
|
}
|
|
1890
|
-
|
|
1891
|
-
|
|
2874
|
+
/**
|
|
2875
|
+
* Gets a hash provider by name with optional fuzzy matching.
|
|
2876
|
+
*
|
|
2877
|
+
* Fuzzy matching (enabled by default) attempts to find providers by:
|
|
2878
|
+
* 1. Exact match (after trimming whitespace)
|
|
2879
|
+
* 2. Case-insensitive match (lowercase)
|
|
2880
|
+
* 3. Dash-removed match (e.g., "SHA-256" matches "sha256")
|
|
2881
|
+
*
|
|
2882
|
+
* @param name - The name of the provider to retrieve
|
|
2883
|
+
* @param options - Optional configuration for the get operation
|
|
2884
|
+
* @param options.fuzzy - Enable/disable fuzzy matching (overrides constructor setting)
|
|
2885
|
+
* @returns The HashProvider if found, undefined otherwise
|
|
2886
|
+
* @example
|
|
2887
|
+
* ```ts
|
|
2888
|
+
* const providers = new HashProviders();
|
|
2889
|
+
* providers.add({ name: 'sha256', toHash: async (data) => '...' });
|
|
2890
|
+
*
|
|
2891
|
+
* // Exact match
|
|
2892
|
+
* const provider = providers.get('sha256');
|
|
2893
|
+
*
|
|
2894
|
+
* // Fuzzy match (case-insensitive)
|
|
2895
|
+
* const provider2 = providers.get('SHA256');
|
|
2896
|
+
*
|
|
2897
|
+
* // Fuzzy match (with dash)
|
|
2898
|
+
* const provider3 = providers.get('SHA-256');
|
|
2899
|
+
*
|
|
2900
|
+
* // Disable fuzzy matching
|
|
2901
|
+
* const provider4 = providers.get('SHA256', { fuzzy: false }); // returns undefined
|
|
2902
|
+
* ```
|
|
2903
|
+
*/
|
|
2904
|
+
get(name, options) {
|
|
2905
|
+
const getFuzzy = options?.fuzzy ?? this._getFuzzy;
|
|
2906
|
+
name = name.trim();
|
|
2907
|
+
let result = this._providers.get(name);
|
|
2908
|
+
if (result === void 0 && getFuzzy === true) {
|
|
2909
|
+
name = name.toLowerCase();
|
|
2910
|
+
result = this._providers.get(name);
|
|
2911
|
+
}
|
|
2912
|
+
if (result === void 0 && getFuzzy === true) {
|
|
2913
|
+
name = method_replace_all_default(
|
|
2914
|
+
/* OPTIONAL_OBJECT: false */
|
|
2915
|
+
0,
|
|
2916
|
+
name,
|
|
2917
|
+
"-",
|
|
2918
|
+
""
|
|
2919
|
+
);
|
|
2920
|
+
result = this._providers.get(name);
|
|
2921
|
+
}
|
|
2922
|
+
return result;
|
|
1892
2923
|
}
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
2924
|
+
/**
|
|
2925
|
+
* Adds a single hash provider to the collection.
|
|
2926
|
+
* If a provider with the same name already exists, it will be replaced.
|
|
2927
|
+
* @param provider - The HashProvider object to add
|
|
2928
|
+
* @example
|
|
2929
|
+
* ```ts
|
|
2930
|
+
* const providers = new HashProviders();
|
|
2931
|
+
* providers.add({
|
|
2932
|
+
* name: 'custom-hash',
|
|
2933
|
+
* toHash: async (data) => {
|
|
2934
|
+
* // Custom hashing logic
|
|
2935
|
+
* return 'hash-result';
|
|
2936
|
+
* }
|
|
2937
|
+
* });
|
|
2938
|
+
* ```
|
|
2939
|
+
*/
|
|
2940
|
+
add(provider) {
|
|
2941
|
+
this._providers.set(provider.name, provider);
|
|
1896
2942
|
}
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
2943
|
+
/**
|
|
2944
|
+
* Removes a hash provider from the collection by name.
|
|
2945
|
+
* @param name - The name of the provider to remove
|
|
2946
|
+
* @returns true if the provider was found and removed, false otherwise
|
|
2947
|
+
* @example
|
|
2948
|
+
* ```ts
|
|
2949
|
+
* const providers = new HashProviders();
|
|
2950
|
+
* providers.add({ name: 'custom', toHash: async (data) => '...' });
|
|
2951
|
+
* const removed = providers.remove('custom'); // returns true
|
|
2952
|
+
* const removed2 = providers.remove('nonexistent'); // returns false
|
|
2953
|
+
* ```
|
|
2954
|
+
*/
|
|
2955
|
+
remove(name) {
|
|
2956
|
+
return this._providers.delete(name);
|
|
1901
2957
|
}
|
|
1902
2958
|
};
|
|
1903
|
-
var
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
2959
|
+
var Hashery = class extends Hookified {
|
|
2960
|
+
_parse = JSON.parse;
|
|
2961
|
+
_stringify = JSON.stringify;
|
|
2962
|
+
_providers = new HashProviders();
|
|
2963
|
+
_defaultAlgorithm = "SHA-256";
|
|
2964
|
+
_defaultAlgorithmSync = "djb2";
|
|
2965
|
+
constructor(options) {
|
|
2966
|
+
super(options);
|
|
2967
|
+
if (options?.parse) {
|
|
2968
|
+
this._parse = options.parse;
|
|
2969
|
+
}
|
|
2970
|
+
if (options?.stringify) {
|
|
2971
|
+
this._stringify = options.stringify;
|
|
2972
|
+
}
|
|
2973
|
+
if (options?.defaultAlgorithm) {
|
|
2974
|
+
this._defaultAlgorithm = options.defaultAlgorithm;
|
|
2975
|
+
}
|
|
2976
|
+
if (options?.defaultAlgorithmSync) {
|
|
2977
|
+
this._defaultAlgorithmSync = options.defaultAlgorithmSync;
|
|
2978
|
+
}
|
|
2979
|
+
this.loadProviders(options?.providers, {
|
|
2980
|
+
includeBase: options?.includeBase ?? true
|
|
2981
|
+
});
|
|
1908
2982
|
}
|
|
1909
|
-
|
|
1910
|
-
|
|
2983
|
+
/**
|
|
2984
|
+
* Gets the parse function used to deserialize stored values.
|
|
2985
|
+
* @returns The current parse function (defaults to JSON.parse)
|
|
2986
|
+
*/
|
|
2987
|
+
get parse() {
|
|
2988
|
+
return this._parse;
|
|
1911
2989
|
}
|
|
1912
|
-
|
|
1913
|
-
|
|
2990
|
+
/**
|
|
2991
|
+
* Sets the parse function used to deserialize stored values.
|
|
2992
|
+
* @param value - The parse function to use for deserialization
|
|
2993
|
+
*/
|
|
2994
|
+
set parse(value) {
|
|
2995
|
+
this._parse = value;
|
|
1914
2996
|
}
|
|
1915
|
-
|
|
1916
|
-
|
|
2997
|
+
/**
|
|
2998
|
+
* Gets the stringify function used to serialize values for storage.
|
|
2999
|
+
* @returns The current stringify function (defaults to JSON.stringify)
|
|
3000
|
+
*/
|
|
3001
|
+
get stringify() {
|
|
3002
|
+
return this._stringify;
|
|
1917
3003
|
}
|
|
1918
|
-
|
|
1919
|
-
|
|
3004
|
+
/**
|
|
3005
|
+
* Sets the stringify function used to serialize values for storage.
|
|
3006
|
+
* @param value - The stringify function to use for serialization
|
|
3007
|
+
*/
|
|
3008
|
+
set stringify(value) {
|
|
3009
|
+
this._stringify = value;
|
|
3010
|
+
}
|
|
3011
|
+
/**
|
|
3012
|
+
* Gets the HashProviders instance used to manage hash providers.
|
|
3013
|
+
* @returns The current HashProviders instance
|
|
3014
|
+
*/
|
|
3015
|
+
get providers() {
|
|
3016
|
+
return this._providers;
|
|
1920
3017
|
}
|
|
1921
|
-
|
|
1922
|
-
|
|
3018
|
+
/**
|
|
3019
|
+
* Sets the HashProviders instance used to manage hash providers.
|
|
3020
|
+
* @param value - The HashProviders instance to use
|
|
3021
|
+
*/
|
|
3022
|
+
set providers(value) {
|
|
3023
|
+
this._providers = value;
|
|
1923
3024
|
}
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
3025
|
+
/**
|
|
3026
|
+
* Gets the names of all registered hash algorithm providers.
|
|
3027
|
+
* @returns An array of provider names (e.g., ['SHA-256', 'SHA-384', 'SHA-512'])
|
|
3028
|
+
*/
|
|
3029
|
+
get names() {
|
|
3030
|
+
return this._providers.names;
|
|
1927
3031
|
}
|
|
1928
|
-
|
|
1929
|
-
|
|
3032
|
+
/**
|
|
3033
|
+
* Gets the default hash algorithm used when none is specified.
|
|
3034
|
+
* @returns The current default algorithm (defaults to 'SHA-256')
|
|
3035
|
+
*/
|
|
3036
|
+
get defaultAlgorithm() {
|
|
3037
|
+
return this._defaultAlgorithm;
|
|
1930
3038
|
}
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
3039
|
+
/**
|
|
3040
|
+
* Sets the default hash algorithm to use when none is specified.
|
|
3041
|
+
* @param value - The default algorithm to use (e.g., 'SHA-256', 'SHA-512', 'djb2')
|
|
3042
|
+
* @example
|
|
3043
|
+
* ```ts
|
|
3044
|
+
* const hashery = new Hashery();
|
|
3045
|
+
* hashery.defaultAlgorithm = 'SHA-512';
|
|
3046
|
+
*
|
|
3047
|
+
* // Now toHash will use SHA-512 by default
|
|
3048
|
+
* const hash = await hashery.toHash({ data: 'example' });
|
|
3049
|
+
* ```
|
|
3050
|
+
*/
|
|
3051
|
+
set defaultAlgorithm(value) {
|
|
3052
|
+
this._defaultAlgorithm = value;
|
|
1934
3053
|
}
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
3054
|
+
/**
|
|
3055
|
+
* Gets the default synchronous hash algorithm used when none is specified.
|
|
3056
|
+
* @returns The current default synchronous algorithm (defaults to 'djb2')
|
|
3057
|
+
*/
|
|
3058
|
+
get defaultAlgorithmSync() {
|
|
3059
|
+
return this._defaultAlgorithmSync;
|
|
1938
3060
|
}
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
3061
|
+
/**
|
|
3062
|
+
* Sets the default synchronous hash algorithm to use when none is specified.
|
|
3063
|
+
* @param value - The default synchronous algorithm to use (e.g., 'djb2', 'fnv1', 'murmer', 'crc32')
|
|
3064
|
+
* @example
|
|
3065
|
+
* ```ts
|
|
3066
|
+
* const hashery = new Hashery();
|
|
3067
|
+
* hashery.defaultAlgorithmSync = 'fnv1';
|
|
3068
|
+
*
|
|
3069
|
+
* // Now synchronous operations will use fnv1 by default
|
|
3070
|
+
* ```
|
|
3071
|
+
*/
|
|
3072
|
+
set defaultAlgorithmSync(value) {
|
|
3073
|
+
this._defaultAlgorithmSync = value;
|
|
1942
3074
|
}
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
3075
|
+
/**
|
|
3076
|
+
* Generates a cryptographic hash of the provided data using the Web Crypto API.
|
|
3077
|
+
* The data is first stringified using the configured stringify function, then hashed.
|
|
3078
|
+
*
|
|
3079
|
+
* @param data - The data to hash (will be stringified before hashing)
|
|
3080
|
+
* @param options - Optional configuration object
|
|
3081
|
+
* @param options.algorithm - The hash algorithm to use (defaults to 'SHA-256')
|
|
3082
|
+
* @param options.maxLength - Optional maximum length for the hash output
|
|
3083
|
+
* @returns A Promise that resolves to the hexadecimal string representation of the hash
|
|
3084
|
+
*
|
|
3085
|
+
* @example
|
|
3086
|
+
* ```ts
|
|
3087
|
+
* const hashery = new Hashery();
|
|
3088
|
+
* const hash = await hashery.toHash({ name: 'John', age: 30 });
|
|
3089
|
+
* console.log(hash); // "a1b2c3d4..."
|
|
3090
|
+
*
|
|
3091
|
+
* // Using a different algorithm
|
|
3092
|
+
* const hash512 = await hashery.toHash({ name: 'John' }, { algorithm: 'SHA-512' });
|
|
3093
|
+
* ```
|
|
3094
|
+
*/
|
|
3095
|
+
async toHash(data, options) {
|
|
3096
|
+
const context = {
|
|
3097
|
+
data,
|
|
3098
|
+
algorithm: options?.algorithm ?? this._defaultAlgorithm,
|
|
3099
|
+
maxLength: options?.maxLength
|
|
3100
|
+
};
|
|
3101
|
+
await this.beforeHook("toHash", context);
|
|
3102
|
+
const stringified = this._stringify(context.data);
|
|
3103
|
+
const encoder = new TextEncoder();
|
|
3104
|
+
const dataBuffer = encoder.encode(stringified);
|
|
3105
|
+
let provider = this._providers.get(context.algorithm);
|
|
3106
|
+
if (!provider) {
|
|
3107
|
+
provider = new WebCrypto({
|
|
3108
|
+
algorithm: this._defaultAlgorithm
|
|
3109
|
+
});
|
|
3110
|
+
}
|
|
3111
|
+
let hash2 = await provider.toHash(dataBuffer);
|
|
3112
|
+
if (options?.maxLength && hash2.length > options?.maxLength) {
|
|
3113
|
+
hash2 = hash2.substring(0, options.maxLength);
|
|
1948
3114
|
}
|
|
3115
|
+
const result = {
|
|
3116
|
+
hash: hash2,
|
|
3117
|
+
data: context.data,
|
|
3118
|
+
algorithm: context.algorithm
|
|
3119
|
+
};
|
|
3120
|
+
await this.afterHook("toHash", result);
|
|
3121
|
+
return result.hash;
|
|
1949
3122
|
}
|
|
1950
|
-
|
|
1951
|
-
|
|
3123
|
+
/**
|
|
3124
|
+
* Generates a deterministic number within a specified range based on the hash of the provided data.
|
|
3125
|
+
* This method uses the toHash function to create a consistent hash, then maps it to a number
|
|
3126
|
+
* between min and max (inclusive).
|
|
3127
|
+
*
|
|
3128
|
+
* @param data - The data to hash (will be stringified before hashing)
|
|
3129
|
+
* @param options - Configuration options (optional, defaults to min: 0, max: 100)
|
|
3130
|
+
* @param options.min - The minimum value of the range (inclusive, defaults to 0)
|
|
3131
|
+
* @param options.max - The maximum value of the range (inclusive, defaults to 100)
|
|
3132
|
+
* @param options.algorithm - The hash algorithm to use (defaults to 'SHA-256')
|
|
3133
|
+
* @param options.hashLength - Number of characters from hash to use for conversion (defaults to 16)
|
|
3134
|
+
* @returns A Promise that resolves to a number between min and max (inclusive)
|
|
3135
|
+
*
|
|
3136
|
+
* @example
|
|
3137
|
+
* ```ts
|
|
3138
|
+
* const hashery = new Hashery();
|
|
3139
|
+
* const num = await hashery.toNumber({ user: 'john' }); // Uses default min: 0, max: 100
|
|
3140
|
+
* console.log(num); // Always returns the same number for the same input, e.g., 42
|
|
3141
|
+
*
|
|
3142
|
+
* // Using custom range
|
|
3143
|
+
* const num2 = await hashery.toNumber({ user: 'john' }, { min: 1, max: 100 });
|
|
3144
|
+
*
|
|
3145
|
+
* // Using a different algorithm
|
|
3146
|
+
* const num512 = await hashery.toNumber({ user: 'john' }, { min: 0, max: 255, algorithm: 'SHA-512' });
|
|
3147
|
+
* ```
|
|
3148
|
+
*/
|
|
3149
|
+
async toNumber(data, options = {}) {
|
|
3150
|
+
const {
|
|
3151
|
+
min = 0,
|
|
3152
|
+
max = 100,
|
|
3153
|
+
algorithm = this._defaultAlgorithm,
|
|
3154
|
+
hashLength = 16
|
|
3155
|
+
} = options;
|
|
3156
|
+
if (min > max) {
|
|
3157
|
+
throw new Error("min cannot be greater than max");
|
|
3158
|
+
}
|
|
3159
|
+
const hash2 = await this.toHash(data, {
|
|
3160
|
+
algorithm,
|
|
3161
|
+
maxLength: hashLength
|
|
3162
|
+
});
|
|
3163
|
+
const hashNumber = Number.parseInt(hash2, 16);
|
|
3164
|
+
const range = max - min + 1;
|
|
3165
|
+
const mapped = min + hashNumber % range;
|
|
3166
|
+
return mapped;
|
|
1952
3167
|
}
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
3168
|
+
/**
|
|
3169
|
+
* Generates a hash of the provided data synchronously using a non-cryptographic hash algorithm.
|
|
3170
|
+
* The data is first stringified using the configured stringify function, then hashed.
|
|
3171
|
+
*
|
|
3172
|
+
* Note: This method only works with synchronous hash providers (djb2, fnv1, murmer, crc32).
|
|
3173
|
+
* WebCrypto algorithms (SHA-256, SHA-384, SHA-512) are not supported and will throw an error.
|
|
3174
|
+
*
|
|
3175
|
+
* @param data - The data to hash (will be stringified before hashing)
|
|
3176
|
+
* @param options - Optional configuration object
|
|
3177
|
+
* @param options.algorithm - The hash algorithm to use (defaults to 'djb2')
|
|
3178
|
+
* @param options.maxLength - Optional maximum length for the hash output
|
|
3179
|
+
* @returns The hexadecimal string representation of the hash
|
|
3180
|
+
*
|
|
3181
|
+
* @throws {Error} If the specified algorithm does not support synchronous hashing
|
|
3182
|
+
*
|
|
3183
|
+
* @example
|
|
3184
|
+
* ```ts
|
|
3185
|
+
* const hashery = new Hashery();
|
|
3186
|
+
* const hash = hashery.toHashSync({ name: 'John', age: 30 });
|
|
3187
|
+
* console.log(hash); // "7c9df5ea..." (djb2 hash)
|
|
3188
|
+
*
|
|
3189
|
+
* // Using a different algorithm
|
|
3190
|
+
* const hashFnv1 = hashery.toHashSync({ name: 'John' }, { algorithm: 'fnv1' });
|
|
3191
|
+
* ```
|
|
3192
|
+
*/
|
|
3193
|
+
toHashSync(data, options) {
|
|
3194
|
+
const context = {
|
|
3195
|
+
data,
|
|
3196
|
+
algorithm: options?.algorithm ?? this._defaultAlgorithmSync,
|
|
3197
|
+
maxLength: options?.maxLength
|
|
3198
|
+
};
|
|
3199
|
+
this.beforeHook("toHashSync", context);
|
|
3200
|
+
const algorithm = context.algorithm;
|
|
3201
|
+
const stringified = this._stringify(context.data);
|
|
3202
|
+
const encoder = new TextEncoder();
|
|
3203
|
+
const dataBuffer = encoder.encode(stringified);
|
|
3204
|
+
const provider = this._providers.get(algorithm);
|
|
3205
|
+
if (!provider) {
|
|
3206
|
+
throw new Error(`Hash provider '${algorithm}' not found`);
|
|
3207
|
+
}
|
|
3208
|
+
if (!provider.toHashSync) {
|
|
3209
|
+
throw new Error(`Hash provider '${algorithm}' does not support synchronous hashing. Use toHash() instead or choose a different algorithm (djb2, fnv1, murmer, crc32).`);
|
|
3210
|
+
}
|
|
3211
|
+
let hash2 = provider.toHashSync(dataBuffer);
|
|
3212
|
+
if (options?.maxLength && hash2.length > options?.maxLength) {
|
|
3213
|
+
hash2 = hash2.substring(0, options.maxLength);
|
|
1960
3214
|
}
|
|
3215
|
+
const result = {
|
|
3216
|
+
hash: hash2,
|
|
3217
|
+
data: context.data,
|
|
3218
|
+
algorithm: context.algorithm
|
|
3219
|
+
};
|
|
3220
|
+
this.afterHook("toHashSync", result);
|
|
3221
|
+
return result.hash;
|
|
1961
3222
|
}
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
3223
|
+
/**
|
|
3224
|
+
* Generates a deterministic number within a specified range based on the hash of the provided data synchronously.
|
|
3225
|
+
* This method uses the toHashSync function to create a consistent hash, then maps it to a number
|
|
3226
|
+
* between min and max (inclusive).
|
|
3227
|
+
*
|
|
3228
|
+
* Note: This method only works with synchronous hash providers (djb2, fnv1, murmer, crc32).
|
|
3229
|
+
*
|
|
3230
|
+
* @param data - The data to hash (will be stringified before hashing)
|
|
3231
|
+
* @param options - Configuration options (optional, defaults to min: 0, max: 100)
|
|
3232
|
+
* @param options.min - The minimum value of the range (inclusive, defaults to 0)
|
|
3233
|
+
* @param options.max - The maximum value of the range (inclusive, defaults to 100)
|
|
3234
|
+
* @param options.algorithm - The hash algorithm to use (defaults to 'djb2')
|
|
3235
|
+
* @param options.hashLength - Number of characters from hash to use for conversion (defaults to 16)
|
|
3236
|
+
* @returns A number between min and max (inclusive)
|
|
3237
|
+
*
|
|
3238
|
+
* @throws {Error} If the specified algorithm does not support synchronous hashing
|
|
3239
|
+
* @throws {Error} If min is greater than max
|
|
3240
|
+
*
|
|
3241
|
+
* @example
|
|
3242
|
+
* ```ts
|
|
3243
|
+
* const hashery = new Hashery();
|
|
3244
|
+
* const num = hashery.toNumberSync({ user: 'john' }); // Uses default min: 0, max: 100
|
|
3245
|
+
* console.log(num); // Always returns the same number for the same input, e.g., 42
|
|
3246
|
+
*
|
|
3247
|
+
* // Using custom range
|
|
3248
|
+
* const num2 = hashery.toNumberSync({ user: 'john' }, { min: 1, max: 100 });
|
|
3249
|
+
*
|
|
3250
|
+
* // Using a different algorithm
|
|
3251
|
+
* const numFnv1 = hashery.toNumberSync({ user: 'john' }, { min: 0, max: 255, algorithm: 'fnv1' });
|
|
3252
|
+
* ```
|
|
3253
|
+
*/
|
|
3254
|
+
toNumberSync(data, options = {}) {
|
|
3255
|
+
const {
|
|
3256
|
+
min = 0,
|
|
3257
|
+
max = 100,
|
|
3258
|
+
algorithm = this._defaultAlgorithmSync,
|
|
3259
|
+
hashLength = 16
|
|
3260
|
+
} = options;
|
|
3261
|
+
if (min > max) {
|
|
3262
|
+
throw new Error("min cannot be greater than max");
|
|
3263
|
+
}
|
|
3264
|
+
const hash2 = this.toHashSync(data, {
|
|
3265
|
+
algorithm,
|
|
3266
|
+
maxLength: hashLength
|
|
3267
|
+
});
|
|
3268
|
+
const hashNumber = Number.parseInt(hash2, 16);
|
|
3269
|
+
const range = max - min + 1;
|
|
3270
|
+
const mapped = min + hashNumber % range;
|
|
3271
|
+
return mapped;
|
|
3272
|
+
}
|
|
3273
|
+
loadProviders(providers, options = {
|
|
3274
|
+
includeBase: true
|
|
3275
|
+
}) {
|
|
3276
|
+
if (providers) {
|
|
3277
|
+
for (const provider of providers) {
|
|
3278
|
+
this._providers.add(provider);
|
|
3279
|
+
}
|
|
3280
|
+
}
|
|
3281
|
+
if (options.includeBase) {
|
|
3282
|
+
this.providers.add(new WebCrypto({
|
|
3283
|
+
algorithm: "SHA-256"
|
|
3284
|
+
}));
|
|
3285
|
+
this.providers.add(new WebCrypto({
|
|
3286
|
+
algorithm: "SHA-384"
|
|
3287
|
+
}));
|
|
3288
|
+
this.providers.add(new WebCrypto({
|
|
3289
|
+
algorithm: "SHA-512"
|
|
3290
|
+
}));
|
|
3291
|
+
this.providers.add(new CRC());
|
|
3292
|
+
this.providers.add(new DJB2());
|
|
3293
|
+
this.providers.add(new FNV1());
|
|
3294
|
+
this.providers.add(new Murmer());
|
|
3295
|
+
}
|
|
1967
3296
|
}
|
|
1968
3297
|
};
|
|
1969
3298
|
|
|
1970
|
-
// node_modules
|
|
1971
|
-
import * as crypto from "crypto";
|
|
1972
|
-
var structuredClone = globalThis.structuredClone ?? ((value) => JSON.parse(JSON.stringify(value)));
|
|
3299
|
+
// node_modules/@cacheable/utils/dist/index.js
|
|
1973
3300
|
var shorthandToMilliseconds = (shorthand) => {
|
|
1974
3301
|
let milliseconds;
|
|
1975
3302
|
if (shorthand === void 0) {
|
|
@@ -1977,7 +3304,10 @@ var shorthandToMilliseconds = (shorthand) => {
|
|
|
1977
3304
|
}
|
|
1978
3305
|
if (typeof shorthand === "number") {
|
|
1979
3306
|
milliseconds = shorthand;
|
|
1980
|
-
} else
|
|
3307
|
+
} else {
|
|
3308
|
+
if (typeof shorthand !== "string") {
|
|
3309
|
+
return void 0;
|
|
3310
|
+
}
|
|
1981
3311
|
shorthand = shorthand.trim();
|
|
1982
3312
|
if (Number.isNaN(Number(shorthand))) {
|
|
1983
3313
|
const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand);
|
|
@@ -2014,7 +3344,7 @@ var shorthandToMilliseconds = (shorthand) => {
|
|
|
2014
3344
|
milliseconds = numericValue * 1e3 * 60 * 60 * 24;
|
|
2015
3345
|
break;
|
|
2016
3346
|
}
|
|
2017
|
-
/*
|
|
3347
|
+
/* v8 ignore next -- @preserve */
|
|
2018
3348
|
default: {
|
|
2019
3349
|
milliseconds = Number(shorthand);
|
|
2020
3350
|
}
|
|
@@ -2022,52 +3352,72 @@ var shorthandToMilliseconds = (shorthand) => {
|
|
|
2022
3352
|
} else {
|
|
2023
3353
|
milliseconds = Number(shorthand);
|
|
2024
3354
|
}
|
|
2025
|
-
} else {
|
|
2026
|
-
throw new TypeError("Time must be a string or a number.");
|
|
2027
3355
|
}
|
|
2028
3356
|
return milliseconds;
|
|
2029
3357
|
};
|
|
2030
3358
|
var shorthandToTime = (shorthand, fromDate) => {
|
|
2031
|
-
fromDate
|
|
3359
|
+
fromDate ?? (fromDate = /* @__PURE__ */ new Date());
|
|
2032
3360
|
const milliseconds = shorthandToMilliseconds(shorthand);
|
|
2033
3361
|
if (milliseconds === void 0) {
|
|
2034
3362
|
return fromDate.getTime();
|
|
2035
3363
|
}
|
|
2036
3364
|
return fromDate.getTime() + milliseconds;
|
|
2037
3365
|
};
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
const
|
|
2053
|
-
|
|
2054
|
-
const
|
|
2055
|
-
const
|
|
2056
|
-
|
|
2057
|
-
return min + hashNumber % range;
|
|
3366
|
+
var HashAlgorithm = /* @__PURE__ */ ((HashAlgorithm2) => {
|
|
3367
|
+
HashAlgorithm2["SHA256"] = "SHA-256";
|
|
3368
|
+
HashAlgorithm2["SHA384"] = "SHA-384";
|
|
3369
|
+
HashAlgorithm2["SHA512"] = "SHA-512";
|
|
3370
|
+
HashAlgorithm2["DJB2"] = "djb2";
|
|
3371
|
+
HashAlgorithm2["FNV1"] = "fnv1";
|
|
3372
|
+
HashAlgorithm2["MURMER"] = "murmer";
|
|
3373
|
+
HashAlgorithm2["CRC32"] = "crc32";
|
|
3374
|
+
return HashAlgorithm2;
|
|
3375
|
+
})(HashAlgorithm || {});
|
|
3376
|
+
function hashSync(object2, options = {
|
|
3377
|
+
algorithm: "djb2",
|
|
3378
|
+
serialize: JSON.stringify
|
|
3379
|
+
}) {
|
|
3380
|
+
const algorithm = options?.algorithm ?? "djb2";
|
|
3381
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
3382
|
+
const objectString = serialize(object2);
|
|
3383
|
+
const hashery = new Hashery();
|
|
3384
|
+
return hashery.toHashSync(objectString, { algorithm });
|
|
2058
3385
|
}
|
|
2059
|
-
function
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
3386
|
+
function hashToNumberSync(object2, options = {
|
|
3387
|
+
min: 0,
|
|
3388
|
+
max: 10,
|
|
3389
|
+
algorithm: "djb2",
|
|
3390
|
+
serialize: JSON.stringify
|
|
3391
|
+
}) {
|
|
3392
|
+
const min = options?.min ?? 0;
|
|
3393
|
+
const max = options?.max ?? 10;
|
|
3394
|
+
const algorithm = options?.algorithm ?? "djb2";
|
|
3395
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
3396
|
+
const hashLength = options?.hashLength ?? 16;
|
|
3397
|
+
if (min >= max) {
|
|
3398
|
+
throw new Error(
|
|
3399
|
+
`Invalid range: min (${min}) must be less than max (${max})`
|
|
3400
|
+
);
|
|
2063
3401
|
}
|
|
2064
|
-
const
|
|
2065
|
-
|
|
3402
|
+
const objectString = serialize(object2);
|
|
3403
|
+
const hashery = new Hashery();
|
|
3404
|
+
return hashery.toNumberSync(objectString, {
|
|
3405
|
+
algorithm,
|
|
3406
|
+
min,
|
|
3407
|
+
max,
|
|
3408
|
+
hashLength
|
|
3409
|
+
});
|
|
2066
3410
|
}
|
|
2067
3411
|
function wrapSync(function_, options) {
|
|
2068
|
-
const { ttl, keyPrefix, cache } = options;
|
|
2069
|
-
return
|
|
2070
|
-
|
|
3412
|
+
const { ttl, keyPrefix, cache, serialize } = options;
|
|
3413
|
+
return (...arguments_) => {
|
|
3414
|
+
let cacheKey = createWrapKey(function_, arguments_, {
|
|
3415
|
+
keyPrefix,
|
|
3416
|
+
serialize
|
|
3417
|
+
});
|
|
3418
|
+
if (options.createKey) {
|
|
3419
|
+
cacheKey = options.createKey(function_, arguments_, options);
|
|
3420
|
+
}
|
|
2071
3421
|
let value = cache.get(cacheKey);
|
|
2072
3422
|
if (value === void 0) {
|
|
2073
3423
|
try {
|
|
@@ -2083,14 +3433,17 @@ function wrapSync(function_, options) {
|
|
|
2083
3433
|
return value;
|
|
2084
3434
|
};
|
|
2085
3435
|
}
|
|
2086
|
-
function createWrapKey(function_, arguments_,
|
|
3436
|
+
function createWrapKey(function_, arguments_, options) {
|
|
3437
|
+
const { keyPrefix, serialize } = options || {};
|
|
2087
3438
|
if (!keyPrefix) {
|
|
2088
|
-
return `${function_.name}::${
|
|
3439
|
+
return `${function_.name}::${hashSync(arguments_, { serialize })}`;
|
|
2089
3440
|
}
|
|
2090
|
-
return `${keyPrefix}::${function_.name}::${
|
|
3441
|
+
return `${keyPrefix}::${function_.name}::${hashSync(arguments_, { serialize })}`;
|
|
2091
3442
|
}
|
|
3443
|
+
|
|
3444
|
+
// node_modules/@cacheable/memory/dist/index.js
|
|
3445
|
+
var structuredClone = globalThis.structuredClone ?? ((value) => JSON.parse(JSON.stringify(value)));
|
|
2092
3446
|
var ListNode = class {
|
|
2093
|
-
// eslint-disable-next-line @typescript-eslint/parameter-properties
|
|
2094
3447
|
value;
|
|
2095
3448
|
prev = void 0;
|
|
2096
3449
|
next = void 0;
|
|
@@ -2135,7 +3488,7 @@ var DoublyLinkedList = class {
|
|
|
2135
3488
|
this.head.prev = node;
|
|
2136
3489
|
}
|
|
2137
3490
|
this.head = node;
|
|
2138
|
-
this.tail
|
|
3491
|
+
this.tail ?? (this.tail = node);
|
|
2139
3492
|
}
|
|
2140
3493
|
// Get the oldest node (tail)
|
|
2141
3494
|
getOldest() {
|
|
@@ -2162,12 +3515,15 @@ var DoublyLinkedList = class {
|
|
|
2162
3515
|
};
|
|
2163
3516
|
var defaultStoreHashSize = 16;
|
|
2164
3517
|
var maximumMapSize = 16777216;
|
|
2165
|
-
var CacheableMemory = class extends
|
|
3518
|
+
var CacheableMemory = class extends Hookified {
|
|
2166
3519
|
_lru = new DoublyLinkedList();
|
|
2167
3520
|
_storeHashSize = defaultStoreHashSize;
|
|
2168
|
-
_storeHashAlgorithm =
|
|
3521
|
+
_storeHashAlgorithm = HashAlgorithm.DJB2;
|
|
2169
3522
|
// Default is djb2Hash
|
|
2170
|
-
_store = Array.from(
|
|
3523
|
+
_store = Array.from(
|
|
3524
|
+
{ length: this._storeHashSize },
|
|
3525
|
+
() => /* @__PURE__ */ new Map()
|
|
3526
|
+
);
|
|
2171
3527
|
_ttl;
|
|
2172
3528
|
// Turned off by default
|
|
2173
3529
|
_useClone = true;
|
|
@@ -2184,29 +3540,37 @@ var CacheableMemory = class extends l {
|
|
|
2184
3540
|
*/
|
|
2185
3541
|
constructor(options) {
|
|
2186
3542
|
super();
|
|
2187
|
-
if (options
|
|
3543
|
+
if (options?.ttl) {
|
|
2188
3544
|
this.setTtl(options.ttl);
|
|
2189
3545
|
}
|
|
2190
|
-
if (
|
|
3546
|
+
if (options?.useClone !== void 0) {
|
|
2191
3547
|
this._useClone = options.useClone;
|
|
2192
3548
|
}
|
|
2193
|
-
if (
|
|
3549
|
+
if (options?.storeHashSize && options.storeHashSize > 0) {
|
|
2194
3550
|
this._storeHashSize = options.storeHashSize;
|
|
2195
3551
|
}
|
|
2196
|
-
if (options
|
|
3552
|
+
if (options?.lruSize) {
|
|
2197
3553
|
if (options.lruSize > maximumMapSize) {
|
|
2198
|
-
this.emit(
|
|
3554
|
+
this.emit(
|
|
3555
|
+
"error",
|
|
3556
|
+
new Error(
|
|
3557
|
+
`LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
|
|
3558
|
+
)
|
|
3559
|
+
);
|
|
2199
3560
|
} else {
|
|
2200
3561
|
this._lruSize = options.lruSize;
|
|
2201
3562
|
}
|
|
2202
3563
|
}
|
|
2203
|
-
if (options
|
|
3564
|
+
if (options?.checkInterval) {
|
|
2204
3565
|
this._checkInterval = options.checkInterval;
|
|
2205
3566
|
}
|
|
2206
|
-
if (options
|
|
3567
|
+
if (options?.storeHashAlgorithm) {
|
|
2207
3568
|
this._storeHashAlgorithm = options.storeHashAlgorithm;
|
|
2208
3569
|
}
|
|
2209
|
-
this._store = Array.from(
|
|
3570
|
+
this._store = Array.from(
|
|
3571
|
+
{ length: this._storeHashSize },
|
|
3572
|
+
() => /* @__PURE__ */ new Map()
|
|
3573
|
+
);
|
|
2210
3574
|
this.startIntervalCheck();
|
|
2211
3575
|
}
|
|
2212
3576
|
/**
|
|
@@ -2250,7 +3614,12 @@ var CacheableMemory = class extends l {
|
|
|
2250
3614
|
*/
|
|
2251
3615
|
set lruSize(value) {
|
|
2252
3616
|
if (value > maximumMapSize) {
|
|
2253
|
-
this.emit(
|
|
3617
|
+
this.emit(
|
|
3618
|
+
"error",
|
|
3619
|
+
new Error(
|
|
3620
|
+
`LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
|
|
3621
|
+
)
|
|
3622
|
+
);
|
|
2254
3623
|
return;
|
|
2255
3624
|
}
|
|
2256
3625
|
this._lruSize = value;
|
|
@@ -2301,18 +3670,21 @@ var CacheableMemory = class extends l {
|
|
|
2301
3670
|
return;
|
|
2302
3671
|
}
|
|
2303
3672
|
this._storeHashSize = value;
|
|
2304
|
-
this._store = Array.from(
|
|
3673
|
+
this._store = Array.from(
|
|
3674
|
+
{ length: this._storeHashSize },
|
|
3675
|
+
() => /* @__PURE__ */ new Map()
|
|
3676
|
+
);
|
|
2305
3677
|
}
|
|
2306
3678
|
/**
|
|
2307
3679
|
* Gets the store hash algorithm
|
|
2308
|
-
* @returns {
|
|
3680
|
+
* @returns {HashAlgorithm | StoreHashAlgorithmFunction} - The store hash algorithm
|
|
2309
3681
|
*/
|
|
2310
3682
|
get storeHashAlgorithm() {
|
|
2311
3683
|
return this._storeHashAlgorithm;
|
|
2312
3684
|
}
|
|
2313
3685
|
/**
|
|
2314
3686
|
* Sets the store hash algorithm. This will recreate the store and all data will be cleared
|
|
2315
|
-
* @param {
|
|
3687
|
+
* @param {HashAlgorithm | HashAlgorithmFunction} value - The store hash algorithm
|
|
2316
3688
|
*/
|
|
2317
3689
|
set storeHashAlgorithm(value) {
|
|
2318
3690
|
this._storeHashAlgorithm = value;
|
|
@@ -2322,7 +3694,7 @@ var CacheableMemory = class extends l {
|
|
|
2322
3694
|
* @returns {IterableIterator<string>} - The keys
|
|
2323
3695
|
*/
|
|
2324
3696
|
get keys() {
|
|
2325
|
-
const keys2 =
|
|
3697
|
+
const keys2 = [];
|
|
2326
3698
|
for (const store of this._store) {
|
|
2327
3699
|
for (const key of store.keys()) {
|
|
2328
3700
|
const item = store.get(key);
|
|
@@ -2340,7 +3712,7 @@ var CacheableMemory = class extends l {
|
|
|
2340
3712
|
* @returns {IterableIterator<CacheableStoreItem>} - The items
|
|
2341
3713
|
*/
|
|
2342
3714
|
get items() {
|
|
2343
|
-
const items =
|
|
3715
|
+
const items = [];
|
|
2344
3716
|
for (const store of this._store) {
|
|
2345
3717
|
for (const item of store.values()) {
|
|
2346
3718
|
if (this.hasExpired(item)) {
|
|
@@ -2386,7 +3758,7 @@ var CacheableMemory = class extends l {
|
|
|
2386
3758
|
* @returns {T[]} - The values of the keys
|
|
2387
3759
|
*/
|
|
2388
3760
|
getMany(keys2) {
|
|
2389
|
-
const result =
|
|
3761
|
+
const result = [];
|
|
2390
3762
|
for (const key of keys2) {
|
|
2391
3763
|
result.push(this.get(key));
|
|
2392
3764
|
}
|
|
@@ -2416,7 +3788,7 @@ var CacheableMemory = class extends l {
|
|
|
2416
3788
|
* @returns {CacheableStoreItem[]} - The raw values of the keys
|
|
2417
3789
|
*/
|
|
2418
3790
|
getManyRaw(keys2) {
|
|
2419
|
-
const result =
|
|
3791
|
+
const result = [];
|
|
2420
3792
|
for (const key of keys2) {
|
|
2421
3793
|
result.push(this.getRaw(key));
|
|
2422
3794
|
}
|
|
@@ -2467,10 +3839,7 @@ var CacheableMemory = class extends l {
|
|
|
2467
3839
|
}
|
|
2468
3840
|
}
|
|
2469
3841
|
const item = { key, value, expires };
|
|
2470
|
-
store.set(
|
|
2471
|
-
key,
|
|
2472
|
-
item
|
|
2473
|
-
);
|
|
3842
|
+
store.set(key, item);
|
|
2474
3843
|
}
|
|
2475
3844
|
/**
|
|
2476
3845
|
* Sets the values of the keys
|
|
@@ -2497,7 +3866,7 @@ var CacheableMemory = class extends l {
|
|
|
2497
3866
|
* @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
|
|
2498
3867
|
*/
|
|
2499
3868
|
hasMany(keys2) {
|
|
2500
|
-
const result =
|
|
3869
|
+
const result = [];
|
|
2501
3870
|
for (const key of keys2) {
|
|
2502
3871
|
const item = this.get(key);
|
|
2503
3872
|
result.push(Boolean(item));
|
|
@@ -2523,7 +3892,7 @@ var CacheableMemory = class extends l {
|
|
|
2523
3892
|
* @returns {T[]} - The values of the keys
|
|
2524
3893
|
*/
|
|
2525
3894
|
takeMany(keys2) {
|
|
2526
|
-
const result =
|
|
3895
|
+
const result = [];
|
|
2527
3896
|
for (const key of keys2) {
|
|
2528
3897
|
result.push(this.take(key));
|
|
2529
3898
|
}
|
|
@@ -2553,7 +3922,10 @@ var CacheableMemory = class extends l {
|
|
|
2553
3922
|
* @returns {void}
|
|
2554
3923
|
*/
|
|
2555
3924
|
clear() {
|
|
2556
|
-
this._store = Array.from(
|
|
3925
|
+
this._store = Array.from(
|
|
3926
|
+
{ length: this._storeHashSize },
|
|
3927
|
+
() => /* @__PURE__ */ new Map()
|
|
3928
|
+
);
|
|
2557
3929
|
this._lru = new DoublyLinkedList();
|
|
2558
3930
|
}
|
|
2559
3931
|
/**
|
|
@@ -2577,19 +3949,23 @@ var CacheableMemory = class extends l {
|
|
|
2577
3949
|
if (this._store.length === 1) {
|
|
2578
3950
|
return 0;
|
|
2579
3951
|
}
|
|
2580
|
-
if (this._storeHashAlgorithm === "djb2Hash") {
|
|
2581
|
-
return djb2Hash(key, 0, this._storeHashSize);
|
|
2582
|
-
}
|
|
2583
3952
|
if (typeof this._storeHashAlgorithm === "function") {
|
|
2584
3953
|
return this._storeHashAlgorithm(key, this._storeHashSize);
|
|
2585
3954
|
}
|
|
2586
|
-
|
|
3955
|
+
const storeHashSize = this._storeHashSize - 1;
|
|
3956
|
+
const hash2 = hashToNumberSync(key, {
|
|
3957
|
+
min: 0,
|
|
3958
|
+
max: storeHashSize,
|
|
3959
|
+
algorithm: this._storeHashAlgorithm
|
|
3960
|
+
});
|
|
3961
|
+
return hash2;
|
|
2587
3962
|
}
|
|
2588
3963
|
/**
|
|
2589
3964
|
* Clone the value. This is for internal use
|
|
2590
3965
|
* @param {any} value - The value to clone
|
|
2591
3966
|
* @returns {any} - The cloned value
|
|
2592
3967
|
*/
|
|
3968
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
2593
3969
|
clone(value) {
|
|
2594
3970
|
if (this.isPrimitive(value)) {
|
|
2595
3971
|
return value;
|
|
@@ -2675,14 +4051,17 @@ var CacheableMemory = class extends l {
|
|
|
2675
4051
|
* @param {Object} [options] - The options to wrap
|
|
2676
4052
|
* @returns {Function} - The wrapped function
|
|
2677
4053
|
*/
|
|
4054
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
2678
4055
|
wrap(function_, options) {
|
|
2679
4056
|
const wrapOptions = {
|
|
2680
|
-
ttl:
|
|
2681
|
-
keyPrefix: options
|
|
4057
|
+
ttl: options?.ttl ?? this._ttl,
|
|
4058
|
+
keyPrefix: options?.keyPrefix,
|
|
4059
|
+
createKey: options?.createKey,
|
|
2682
4060
|
cache: this
|
|
2683
4061
|
};
|
|
2684
4062
|
return wrapSync(function_, wrapOptions);
|
|
2685
4063
|
}
|
|
4064
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
2686
4065
|
isPrimitive(value) {
|
|
2687
4066
|
const result = false;
|
|
2688
4067
|
if (value === null || value === void 0) {
|
|
@@ -2783,7 +4162,7 @@ var stringify2 = (value, replacer, space) => {
|
|
|
2783
4162
|
};
|
|
2784
4163
|
|
|
2785
4164
|
// node_modules/file-entry-cache/node_modules/flat-cache/dist/index.js
|
|
2786
|
-
var FlatCache = class extends
|
|
4165
|
+
var FlatCache = class extends Hookified {
|
|
2787
4166
|
_cache = new CacheableMemory();
|
|
2788
4167
|
_cacheDir = ".cache";
|
|
2789
4168
|
_cacheId = "cache1";
|
|
@@ -2802,20 +4181,20 @@ var FlatCache = class extends l {
|
|
|
2802
4181
|
checkInterval: options.expirationInterval
|
|
2803
4182
|
});
|
|
2804
4183
|
}
|
|
2805
|
-
if (options
|
|
4184
|
+
if (options?.cacheDir) {
|
|
2806
4185
|
this._cacheDir = options.cacheDir;
|
|
2807
4186
|
}
|
|
2808
|
-
if (options
|
|
4187
|
+
if (options?.cacheId) {
|
|
2809
4188
|
this._cacheId = options.cacheId;
|
|
2810
4189
|
}
|
|
2811
|
-
if (options
|
|
4190
|
+
if (options?.persistInterval) {
|
|
2812
4191
|
this._persistInterval = options.persistInterval;
|
|
2813
4192
|
this.startAutoPersist();
|
|
2814
4193
|
}
|
|
2815
|
-
if (options
|
|
4194
|
+
if (options?.deserialize) {
|
|
2816
4195
|
this._parse = options.deserialize;
|
|
2817
4196
|
}
|
|
2818
|
-
if (options
|
|
4197
|
+
if (options?.serialize) {
|
|
2819
4198
|
this._stringify = options.serialize;
|
|
2820
4199
|
}
|
|
2821
4200
|
}
|
|
@@ -2901,7 +4280,9 @@ var FlatCache = class extends l {
|
|
|
2901
4280
|
*/
|
|
2902
4281
|
load(cacheId, cacheDir) {
|
|
2903
4282
|
try {
|
|
2904
|
-
const filePath = path10.resolve(
|
|
4283
|
+
const filePath = path10.resolve(
|
|
4284
|
+
`${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}`
|
|
4285
|
+
);
|
|
2905
4286
|
this.loadFile(filePath);
|
|
2906
4287
|
this.emit(
|
|
2907
4288
|
"load"
|
|
@@ -2920,12 +4301,73 @@ var FlatCache = class extends l {
|
|
|
2920
4301
|
if (fs5.existsSync(pathToFile)) {
|
|
2921
4302
|
const data = fs5.readFileSync(pathToFile, "utf8");
|
|
2922
4303
|
const items = this._parse(data);
|
|
2923
|
-
|
|
2924
|
-
|
|
4304
|
+
if (Array.isArray(items)) {
|
|
4305
|
+
for (const item of items) {
|
|
4306
|
+
if (item && typeof item === "object" && "key" in item) {
|
|
4307
|
+
if (item.expires) {
|
|
4308
|
+
this._cache.set(item.key, item.value, { expire: item.expires });
|
|
4309
|
+
} else if (item.timestamp) {
|
|
4310
|
+
this._cache.set(item.key, item.value, { expire: item.timestamp });
|
|
4311
|
+
} else {
|
|
4312
|
+
this._cache.set(item.key, item.value);
|
|
4313
|
+
}
|
|
4314
|
+
}
|
|
4315
|
+
}
|
|
4316
|
+
} else {
|
|
4317
|
+
for (const key of Object.keys(items)) {
|
|
4318
|
+
const item = items[key];
|
|
4319
|
+
if (item && typeof item === "object" && "key" in item) {
|
|
4320
|
+
this._cache.set(item.key, item.value, {
|
|
4321
|
+
expire: item.expires
|
|
4322
|
+
});
|
|
4323
|
+
} else {
|
|
4324
|
+
if (item && typeof item === "object" && item.timestamp) {
|
|
4325
|
+
this._cache.set(key, item, { expire: item.timestamp });
|
|
4326
|
+
} else {
|
|
4327
|
+
this._cache.set(key, item);
|
|
4328
|
+
}
|
|
4329
|
+
}
|
|
4330
|
+
}
|
|
2925
4331
|
}
|
|
2926
4332
|
this._changesSinceLastSave = true;
|
|
2927
4333
|
}
|
|
2928
4334
|
}
|
|
4335
|
+
loadFileStream(pathToFile, onProgress, onEnd, onError) {
|
|
4336
|
+
if (fs5.existsSync(pathToFile)) {
|
|
4337
|
+
const stats = fs5.statSync(pathToFile);
|
|
4338
|
+
const total = stats.size;
|
|
4339
|
+
let loaded = 0;
|
|
4340
|
+
let streamData = "";
|
|
4341
|
+
const readStream = fs5.createReadStream(pathToFile, { encoding: "utf8" });
|
|
4342
|
+
readStream.on("data", (chunk) => {
|
|
4343
|
+
loaded += chunk.length;
|
|
4344
|
+
streamData += chunk;
|
|
4345
|
+
onProgress(loaded, total);
|
|
4346
|
+
});
|
|
4347
|
+
readStream.on("end", () => {
|
|
4348
|
+
const items = this._parse(streamData);
|
|
4349
|
+
for (const key of Object.keys(items)) {
|
|
4350
|
+
this._cache.set(items[key].key, items[key].value, {
|
|
4351
|
+
expire: items[key].expires
|
|
4352
|
+
});
|
|
4353
|
+
}
|
|
4354
|
+
this._changesSinceLastSave = true;
|
|
4355
|
+
onEnd();
|
|
4356
|
+
});
|
|
4357
|
+
readStream.on("error", (error) => {
|
|
4358
|
+
this.emit("error", error);
|
|
4359
|
+
if (onError) {
|
|
4360
|
+
onError(error);
|
|
4361
|
+
}
|
|
4362
|
+
});
|
|
4363
|
+
} else {
|
|
4364
|
+
const error = new Error(`Cache file ${pathToFile} does not exist`);
|
|
4365
|
+
this.emit("error", error);
|
|
4366
|
+
if (onError) {
|
|
4367
|
+
onError(error);
|
|
4368
|
+
}
|
|
4369
|
+
}
|
|
4370
|
+
}
|
|
2929
4371
|
/**
|
|
2930
4372
|
* Returns the entire persisted object
|
|
2931
4373
|
* @method all
|
|
@@ -2940,10 +4382,11 @@ var FlatCache = class extends l {
|
|
|
2940
4382
|
return result;
|
|
2941
4383
|
}
|
|
2942
4384
|
/**
|
|
2943
|
-
* Returns an array with all the items in the cache { key, value,
|
|
4385
|
+
* Returns an array with all the items in the cache { key, value, expires }
|
|
2944
4386
|
* @method items
|
|
2945
4387
|
* @returns {Array}
|
|
2946
4388
|
*/
|
|
4389
|
+
// biome-ignore lint/suspicious/noExplicitAny: cache items can store any value
|
|
2947
4390
|
get items() {
|
|
2948
4391
|
return [...this._cache.items];
|
|
2949
4392
|
}
|
|
@@ -2977,6 +4420,7 @@ var FlatCache = class extends l {
|
|
|
2977
4420
|
* @param key {string} the key to set
|
|
2978
4421
|
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
|
2979
4422
|
*/
|
|
4423
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
2980
4424
|
setKey(key, value, ttl) {
|
|
2981
4425
|
this.set(key, value, ttl);
|
|
2982
4426
|
}
|
|
@@ -2987,6 +4431,7 @@ var FlatCache = class extends l {
|
|
|
2987
4431
|
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
|
2988
4432
|
* @param [ttl] {number} the time to live in milliseconds
|
|
2989
4433
|
*/
|
|
4434
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
2990
4435
|
set(key, value, ttl) {
|
|
2991
4436
|
this._cache.set(key, value, ttl);
|
|
2992
4437
|
this._changesSinceLastSave = true;
|
|
@@ -3010,11 +4455,11 @@ var FlatCache = class extends l {
|
|
|
3010
4455
|
this.emit("delete", key);
|
|
3011
4456
|
}
|
|
3012
4457
|
/**
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
4458
|
+
* (Legacy) Return the value of the provided key. This method will be deprecated in the future
|
|
4459
|
+
* @method getKey<T>
|
|
4460
|
+
* @param key {String} the name of the key to retrieve
|
|
4461
|
+
* @returns {*} at T the value from the key
|
|
4462
|
+
*/
|
|
3018
4463
|
getKey(key) {
|
|
3019
4464
|
return this.get(key);
|
|
3020
4465
|
}
|
|
@@ -3142,25 +4587,24 @@ function createFromFile(filePath, options) {
|
|
|
3142
4587
|
}
|
|
3143
4588
|
|
|
3144
4589
|
// node_modules/file-entry-cache/dist/index.js
|
|
3145
|
-
function createFromFile2(filePath,
|
|
4590
|
+
function createFromFile2(filePath, options) {
|
|
3146
4591
|
const fname = path11.basename(filePath);
|
|
3147
4592
|
const directory = path11.dirname(filePath);
|
|
3148
|
-
return create(fname, directory,
|
|
4593
|
+
return create(fname, directory, options);
|
|
3149
4594
|
}
|
|
3150
|
-
function create(cacheId, cacheDirectory,
|
|
3151
|
-
const
|
|
3152
|
-
|
|
3153
|
-
useCheckSum,
|
|
4595
|
+
function create(cacheId, cacheDirectory, options) {
|
|
4596
|
+
const opts = {
|
|
4597
|
+
...options,
|
|
3154
4598
|
cache: {
|
|
3155
4599
|
cacheId,
|
|
3156
4600
|
cacheDir: cacheDirectory
|
|
3157
4601
|
}
|
|
3158
4602
|
};
|
|
3159
|
-
const fileEntryCache = new FileEntryCache(
|
|
4603
|
+
const fileEntryCache = new FileEntryCache(opts);
|
|
3160
4604
|
if (cacheDirectory) {
|
|
3161
4605
|
const cachePath = `${cacheDirectory}/${cacheId}`;
|
|
3162
4606
|
if (fs6.existsSync(cachePath)) {
|
|
3163
|
-
fileEntryCache.cache = createFromFile(cachePath,
|
|
4607
|
+
fileEntryCache.cache = createFromFile(cachePath, opts.cache);
|
|
3164
4608
|
}
|
|
3165
4609
|
}
|
|
3166
4610
|
return fileEntryCache;
|
|
@@ -3172,28 +4616,40 @@ var FileEntryDefault = class {
|
|
|
3172
4616
|
var FileEntryCache = class {
|
|
3173
4617
|
_cache = new FlatCache({ useClone: false });
|
|
3174
4618
|
_useCheckSum = false;
|
|
3175
|
-
_useModifiedTime = true;
|
|
3176
|
-
_currentWorkingDirectory;
|
|
3177
4619
|
_hashAlgorithm = "md5";
|
|
4620
|
+
_cwd = process.cwd();
|
|
4621
|
+
_restrictAccessToCwd = false;
|
|
4622
|
+
_logger;
|
|
4623
|
+
_useAbsolutePathAsKey = false;
|
|
4624
|
+
_useModifiedTime = true;
|
|
3178
4625
|
/**
|
|
3179
4626
|
* Create a new FileEntryCache instance
|
|
3180
|
-
* @param options - The options for the FileEntryCache
|
|
4627
|
+
* @param options - The options for the FileEntryCache (all properties are optional with defaults)
|
|
3181
4628
|
*/
|
|
3182
4629
|
constructor(options) {
|
|
3183
|
-
if (options
|
|
4630
|
+
if (options?.cache) {
|
|
3184
4631
|
this._cache = new FlatCache(options.cache);
|
|
3185
4632
|
}
|
|
3186
|
-
if (options
|
|
4633
|
+
if (options?.useCheckSum) {
|
|
4634
|
+
this._useCheckSum = options.useCheckSum;
|
|
4635
|
+
}
|
|
4636
|
+
if (options?.hashAlgorithm) {
|
|
4637
|
+
this._hashAlgorithm = options.hashAlgorithm;
|
|
4638
|
+
}
|
|
4639
|
+
if (options?.cwd) {
|
|
4640
|
+
this._cwd = options.cwd;
|
|
4641
|
+
}
|
|
4642
|
+
if (options?.useModifiedTime !== void 0) {
|
|
3187
4643
|
this._useModifiedTime = options.useModifiedTime;
|
|
3188
4644
|
}
|
|
3189
|
-
if (options
|
|
3190
|
-
this.
|
|
4645
|
+
if (options?.restrictAccessToCwd !== void 0) {
|
|
4646
|
+
this._restrictAccessToCwd = options.restrictAccessToCwd;
|
|
3191
4647
|
}
|
|
3192
|
-
if (options
|
|
3193
|
-
this.
|
|
4648
|
+
if (options?.useAbsolutePathAsKey !== void 0) {
|
|
4649
|
+
this._useAbsolutePathAsKey = options.useAbsolutePathAsKey;
|
|
3194
4650
|
}
|
|
3195
|
-
if (options
|
|
3196
|
-
this.
|
|
4651
|
+
if (options?.logger) {
|
|
4652
|
+
this._logger = options.logger;
|
|
3197
4653
|
}
|
|
3198
4654
|
}
|
|
3199
4655
|
/**
|
|
@@ -3210,9 +4666,23 @@ var FileEntryCache = class {
|
|
|
3210
4666
|
set cache(cache) {
|
|
3211
4667
|
this._cache = cache;
|
|
3212
4668
|
}
|
|
4669
|
+
/**
|
|
4670
|
+
* Get the logger
|
|
4671
|
+
* @returns {ILogger | undefined} The logger instance
|
|
4672
|
+
*/
|
|
4673
|
+
get logger() {
|
|
4674
|
+
return this._logger;
|
|
4675
|
+
}
|
|
4676
|
+
/**
|
|
4677
|
+
* Set the logger
|
|
4678
|
+
* @param {ILogger | undefined} logger - The logger to set
|
|
4679
|
+
*/
|
|
4680
|
+
set logger(logger) {
|
|
4681
|
+
this._logger = logger;
|
|
4682
|
+
}
|
|
3213
4683
|
/**
|
|
3214
4684
|
* Use the hash to check if the file has changed
|
|
3215
|
-
* @returns {boolean} if the hash is used to check if the file has changed
|
|
4685
|
+
* @returns {boolean} if the hash is used to check if the file has changed (default: false)
|
|
3216
4686
|
*/
|
|
3217
4687
|
get useCheckSum() {
|
|
3218
4688
|
return this._useCheckSum;
|
|
@@ -3225,46 +4695,74 @@ var FileEntryCache = class {
|
|
|
3225
4695
|
this._useCheckSum = value;
|
|
3226
4696
|
}
|
|
3227
4697
|
/**
|
|
3228
|
-
*
|
|
3229
|
-
* @returns {
|
|
4698
|
+
* Get the hash algorithm
|
|
4699
|
+
* @returns {string} The hash algorithm (default: 'md5')
|
|
4700
|
+
*/
|
|
4701
|
+
get hashAlgorithm() {
|
|
4702
|
+
return this._hashAlgorithm;
|
|
4703
|
+
}
|
|
4704
|
+
/**
|
|
4705
|
+
* Set the hash algorithm
|
|
4706
|
+
* @param {string} value - The value to set
|
|
4707
|
+
*/
|
|
4708
|
+
set hashAlgorithm(value) {
|
|
4709
|
+
this._hashAlgorithm = value;
|
|
4710
|
+
}
|
|
4711
|
+
/**
|
|
4712
|
+
* Get the current working directory
|
|
4713
|
+
* @returns {string} The current working directory (default: process.cwd())
|
|
4714
|
+
*/
|
|
4715
|
+
get cwd() {
|
|
4716
|
+
return this._cwd;
|
|
4717
|
+
}
|
|
4718
|
+
/**
|
|
4719
|
+
* Set the current working directory
|
|
4720
|
+
* @param {string} value - The value to set
|
|
4721
|
+
*/
|
|
4722
|
+
set cwd(value) {
|
|
4723
|
+
this._cwd = value;
|
|
4724
|
+
}
|
|
4725
|
+
/**
|
|
4726
|
+
* Get whether to use modified time for change detection
|
|
4727
|
+
* @returns {boolean} Whether modified time (mtime) is used for change detection (default: true)
|
|
3230
4728
|
*/
|
|
3231
4729
|
get useModifiedTime() {
|
|
3232
4730
|
return this._useModifiedTime;
|
|
3233
4731
|
}
|
|
3234
4732
|
/**
|
|
3235
|
-
* Set
|
|
4733
|
+
* Set whether to use modified time for change detection
|
|
3236
4734
|
* @param {boolean} value - The value to set
|
|
3237
4735
|
*/
|
|
3238
4736
|
set useModifiedTime(value) {
|
|
3239
4737
|
this._useModifiedTime = value;
|
|
3240
4738
|
}
|
|
3241
4739
|
/**
|
|
3242
|
-
* Get
|
|
3243
|
-
* @returns {
|
|
4740
|
+
* Get whether to restrict paths to cwd boundaries
|
|
4741
|
+
* @returns {boolean} Whether strict path checking is enabled (default: true)
|
|
3244
4742
|
*/
|
|
3245
|
-
get
|
|
3246
|
-
return this.
|
|
4743
|
+
get restrictAccessToCwd() {
|
|
4744
|
+
return this._restrictAccessToCwd;
|
|
3247
4745
|
}
|
|
3248
4746
|
/**
|
|
3249
|
-
* Set
|
|
3250
|
-
* @param {
|
|
4747
|
+
* Set whether to restrict paths to cwd boundaries
|
|
4748
|
+
* @param {boolean} value - The value to set
|
|
3251
4749
|
*/
|
|
3252
|
-
set
|
|
3253
|
-
this.
|
|
4750
|
+
set restrictAccessToCwd(value) {
|
|
4751
|
+
this._restrictAccessToCwd = value;
|
|
3254
4752
|
}
|
|
3255
4753
|
/**
|
|
3256
|
-
* Get
|
|
3257
|
-
* @returns {
|
|
4754
|
+
* Get whether to use absolute path as cache key
|
|
4755
|
+
* @returns {boolean} Whether cache keys use absolute paths (default: false)
|
|
3258
4756
|
*/
|
|
3259
|
-
get
|
|
3260
|
-
return this.
|
|
4757
|
+
get useAbsolutePathAsKey() {
|
|
4758
|
+
return this._useAbsolutePathAsKey;
|
|
3261
4759
|
}
|
|
3262
4760
|
/**
|
|
3263
|
-
* Set
|
|
3264
|
-
* @param {
|
|
4761
|
+
* Set whether to use absolute path as cache key
|
|
4762
|
+
* @param {boolean} value - The value to set
|
|
3265
4763
|
*/
|
|
3266
|
-
set
|
|
3267
|
-
this.
|
|
4764
|
+
set useAbsolutePathAsKey(value) {
|
|
4765
|
+
this._useAbsolutePathAsKey = value;
|
|
3268
4766
|
}
|
|
3269
4767
|
/**
|
|
3270
4768
|
* Given a buffer, calculate md5 hash of its content.
|
|
@@ -3272,7 +4770,6 @@ var FileEntryCache = class {
|
|
|
3272
4770
|
* @param {Buffer} buffer buffer to calculate hash on
|
|
3273
4771
|
* @return {String} content hash digest
|
|
3274
4772
|
*/
|
|
3275
|
-
// eslint-disable-next-line @typescript-eslint/no-restricted-types
|
|
3276
4773
|
getHash(buffer) {
|
|
3277
4774
|
return crypto2.createHash(this._hashAlgorithm).update(buffer).digest("hex");
|
|
3278
4775
|
}
|
|
@@ -3282,17 +4779,10 @@ var FileEntryCache = class {
|
|
|
3282
4779
|
* @param {String} filePath
|
|
3283
4780
|
* @return {String}
|
|
3284
4781
|
*/
|
|
3285
|
-
createFileKey(filePath
|
|
4782
|
+
createFileKey(filePath) {
|
|
3286
4783
|
let result = filePath;
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
const splitPath = filePath.split(currentWorkingDirectory).pop();
|
|
3290
|
-
if (splitPath) {
|
|
3291
|
-
result = splitPath;
|
|
3292
|
-
if (result.startsWith("/")) {
|
|
3293
|
-
result = result.slice(1);
|
|
3294
|
-
}
|
|
3295
|
-
}
|
|
4784
|
+
if (this._useAbsolutePathAsKey && this.isRelativePath(filePath)) {
|
|
4785
|
+
result = this.getAbsolutePathWithCwd(filePath, this._cwd);
|
|
3296
4786
|
}
|
|
3297
4787
|
return result;
|
|
3298
4788
|
}
|
|
@@ -3306,17 +4796,17 @@ var FileEntryCache = class {
|
|
|
3306
4796
|
return !path11.isAbsolute(filePath);
|
|
3307
4797
|
}
|
|
3308
4798
|
/**
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
4799
|
+
* Delete the cache file from the disk
|
|
4800
|
+
* @method deleteCacheFile
|
|
4801
|
+
* @return {boolean} true if the file was deleted, false otherwise
|
|
4802
|
+
*/
|
|
3313
4803
|
deleteCacheFile() {
|
|
3314
4804
|
return this._cache.removeCacheFile();
|
|
3315
4805
|
}
|
|
3316
4806
|
/**
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
4807
|
+
* Remove the cache from the file and clear the memory cache
|
|
4808
|
+
* @method destroy
|
|
4809
|
+
*/
|
|
3320
4810
|
destroy() {
|
|
3321
4811
|
this._cache.destroy();
|
|
3322
4812
|
}
|
|
@@ -3325,12 +4815,8 @@ var FileEntryCache = class {
|
|
|
3325
4815
|
* @method removeEntry
|
|
3326
4816
|
* @param filePath - The file path to remove from the cache
|
|
3327
4817
|
*/
|
|
3328
|
-
removeEntry(filePath
|
|
3329
|
-
|
|
3330
|
-
filePath = this.getAbsolutePath(filePath, { currentWorkingDirectory: options == null ? void 0 : options.currentWorkingDirectory });
|
|
3331
|
-
this._cache.removeKey(this.createFileKey(filePath));
|
|
3332
|
-
}
|
|
3333
|
-
const key = this.createFileKey(filePath, { currentWorkingDirectory: options == null ? void 0 : options.currentWorkingDirectory });
|
|
4818
|
+
removeEntry(filePath) {
|
|
4819
|
+
const key = this.createFileKey(filePath);
|
|
3334
4820
|
this._cache.removeKey(key);
|
|
3335
4821
|
}
|
|
3336
4822
|
/**
|
|
@@ -3368,34 +4854,54 @@ var FileEntryCache = class {
|
|
|
3368
4854
|
* @param options - The options for getting the file descriptor
|
|
3369
4855
|
* @returns The file descriptor
|
|
3370
4856
|
*/
|
|
3371
|
-
// eslint-disable-next-line complexity
|
|
3372
4857
|
getFileDescriptor(filePath, options) {
|
|
3373
|
-
|
|
4858
|
+
this._logger?.debug({ filePath, options }, "Getting file descriptor");
|
|
3374
4859
|
let fstat;
|
|
3375
4860
|
const result = {
|
|
3376
4861
|
key: this.createFileKey(filePath),
|
|
3377
4862
|
changed: false,
|
|
3378
4863
|
meta: {}
|
|
3379
4864
|
};
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
4865
|
+
this._logger?.trace({ key: result.key }, "Created file key");
|
|
4866
|
+
const metaCache = this._cache.getKey(result.key);
|
|
4867
|
+
if (metaCache) {
|
|
4868
|
+
this._logger?.trace({ metaCache }, "Found cached meta");
|
|
4869
|
+
} else {
|
|
4870
|
+
this._logger?.trace("No cached meta found");
|
|
4871
|
+
}
|
|
4872
|
+
result.meta = metaCache ? { ...metaCache } : {};
|
|
4873
|
+
const absolutePath = this.getAbsolutePath(filePath);
|
|
4874
|
+
this._logger?.trace({ absolutePath }, "Resolved absolute path");
|
|
4875
|
+
const useCheckSumValue = options?.useCheckSum ?? this._useCheckSum;
|
|
4876
|
+
this._logger?.debug(
|
|
4877
|
+
{ useCheckSum: useCheckSumValue },
|
|
4878
|
+
"Using checksum setting"
|
|
4879
|
+
);
|
|
4880
|
+
const useModifiedTimeValue = options?.useModifiedTime ?? this.useModifiedTime;
|
|
4881
|
+
this._logger?.debug(
|
|
4882
|
+
{ useModifiedTime: useModifiedTimeValue },
|
|
4883
|
+
"Using modified time (mtime) setting"
|
|
4884
|
+
);
|
|
3384
4885
|
try {
|
|
3385
|
-
fstat = fs6.statSync(
|
|
3386
|
-
result.meta =
|
|
3387
|
-
size: fstat.size
|
|
3388
|
-
};
|
|
4886
|
+
fstat = fs6.statSync(absolutePath);
|
|
4887
|
+
result.meta.size = fstat.size;
|
|
3389
4888
|
result.meta.mtime = fstat.mtime.getTime();
|
|
4889
|
+
this._logger?.trace(
|
|
4890
|
+
{ size: result.meta.size, mtime: result.meta.mtime },
|
|
4891
|
+
"Read file stats"
|
|
4892
|
+
);
|
|
3390
4893
|
if (useCheckSumValue) {
|
|
3391
|
-
const buffer = fs6.readFileSync(
|
|
4894
|
+
const buffer = fs6.readFileSync(absolutePath);
|
|
3392
4895
|
result.meta.hash = this.getHash(buffer);
|
|
4896
|
+
this._logger?.trace({ hash: result.meta.hash }, "Calculated file hash");
|
|
3393
4897
|
}
|
|
3394
4898
|
} catch (error) {
|
|
4899
|
+
this._logger?.error({ filePath, error }, "Error reading file");
|
|
3395
4900
|
this.removeEntry(filePath);
|
|
3396
4901
|
let notFound = false;
|
|
3397
4902
|
if (error.message.includes("ENOENT")) {
|
|
3398
4903
|
notFound = true;
|
|
4904
|
+
this._logger?.debug({ filePath }, "File not found");
|
|
3399
4905
|
}
|
|
3400
4906
|
return {
|
|
3401
4907
|
key: result.key,
|
|
@@ -3404,25 +4910,39 @@ var FileEntryCache = class {
|
|
|
3404
4910
|
meta: {}
|
|
3405
4911
|
};
|
|
3406
4912
|
}
|
|
3407
|
-
const metaCache = this._cache.getKey(result.key);
|
|
3408
4913
|
if (!metaCache) {
|
|
3409
4914
|
result.changed = true;
|
|
3410
4915
|
this._cache.setKey(result.key, result.meta);
|
|
4916
|
+
this._logger?.debug({ filePath }, "File not in cache, marked as changed");
|
|
3411
4917
|
return result;
|
|
3412
4918
|
}
|
|
3413
|
-
if (result.meta
|
|
3414
|
-
result.meta.data = metaCache.data;
|
|
3415
|
-
}
|
|
3416
|
-
if (useModifiedTimeValue && (metaCache == null ? void 0 : metaCache.mtime) !== ((_a = result.meta) == null ? void 0 : _a.mtime)) {
|
|
4919
|
+
if (useModifiedTimeValue && metaCache?.mtime !== result.meta?.mtime) {
|
|
3417
4920
|
result.changed = true;
|
|
4921
|
+
this._logger?.debug(
|
|
4922
|
+
{ filePath, oldMtime: metaCache.mtime, newMtime: result.meta.mtime },
|
|
4923
|
+
"File changed: mtime differs"
|
|
4924
|
+
);
|
|
3418
4925
|
}
|
|
3419
|
-
if (
|
|
4926
|
+
if (metaCache?.size !== result.meta?.size) {
|
|
3420
4927
|
result.changed = true;
|
|
4928
|
+
this._logger?.debug(
|
|
4929
|
+
{ filePath, oldSize: metaCache.size, newSize: result.meta.size },
|
|
4930
|
+
"File changed: size differs"
|
|
4931
|
+
);
|
|
3421
4932
|
}
|
|
3422
|
-
if (useCheckSumValue &&
|
|
4933
|
+
if (useCheckSumValue && metaCache?.hash !== result.meta?.hash) {
|
|
3423
4934
|
result.changed = true;
|
|
4935
|
+
this._logger?.debug(
|
|
4936
|
+
{ filePath, oldHash: metaCache.hash, newHash: result.meta.hash },
|
|
4937
|
+
"File changed: hash differs"
|
|
4938
|
+
);
|
|
3424
4939
|
}
|
|
3425
4940
|
this._cache.setKey(result.key, result.meta);
|
|
4941
|
+
if (result.changed) {
|
|
4942
|
+
this._logger?.info({ filePath }, "File has changed");
|
|
4943
|
+
} else {
|
|
4944
|
+
this._logger?.debug({ filePath }, "File unchanged");
|
|
4945
|
+
}
|
|
3426
4946
|
return result;
|
|
3427
4947
|
}
|
|
3428
4948
|
/**
|
|
@@ -3432,7 +4952,7 @@ var FileEntryCache = class {
|
|
|
3432
4952
|
* @returns The file descriptors
|
|
3433
4953
|
*/
|
|
3434
4954
|
normalizeEntries(files) {
|
|
3435
|
-
const result =
|
|
4955
|
+
const result = [];
|
|
3436
4956
|
if (files) {
|
|
3437
4957
|
for (const file of files) {
|
|
3438
4958
|
const fileDescriptor = this.getFileDescriptor(file);
|
|
@@ -3480,7 +5000,7 @@ var FileEntryCache = class {
|
|
|
3480
5000
|
* @returns {string[]} The updated files
|
|
3481
5001
|
*/
|
|
3482
5002
|
getUpdatedFiles(files) {
|
|
3483
|
-
const result =
|
|
5003
|
+
const result = [];
|
|
3484
5004
|
const fileDescriptors = this.normalizeEntries(files);
|
|
3485
5005
|
for (const fileDescriptor of fileDescriptors) {
|
|
3486
5006
|
if (fileDescriptor.changed) {
|
|
@@ -3490,17 +5010,16 @@ var FileEntryCache = class {
|
|
|
3490
5010
|
return result;
|
|
3491
5011
|
}
|
|
3492
5012
|
/**
|
|
3493
|
-
* Get the
|
|
5013
|
+
* Get the file descriptors by path prefix
|
|
3494
5014
|
* @method getFileDescriptorsByPath
|
|
3495
|
-
* @param filePath - the
|
|
3496
|
-
* @returns {FileDescriptor[]} The
|
|
5015
|
+
* @param filePath - the path prefix to match
|
|
5016
|
+
* @returns {FileDescriptor[]} The file descriptors
|
|
3497
5017
|
*/
|
|
3498
5018
|
getFileDescriptorsByPath(filePath) {
|
|
3499
|
-
const result =
|
|
5019
|
+
const result = [];
|
|
3500
5020
|
const keys2 = this._cache.keys();
|
|
3501
5021
|
for (const key of keys2) {
|
|
3502
|
-
|
|
3503
|
-
if (absolutePath.startsWith(filePath)) {
|
|
5022
|
+
if (key.startsWith(filePath)) {
|
|
3504
5023
|
const fileDescriptor = this.getFileDescriptor(key);
|
|
3505
5024
|
result.push(fileDescriptor);
|
|
3506
5025
|
}
|
|
@@ -3509,25 +5028,64 @@ var FileEntryCache = class {
|
|
|
3509
5028
|
}
|
|
3510
5029
|
/**
|
|
3511
5030
|
* Get the Absolute Path. If it is already absolute it will return the path as is.
|
|
5031
|
+
* When restrictAccessToCwd is enabled, ensures the resolved path stays within cwd boundaries.
|
|
3512
5032
|
* @method getAbsolutePath
|
|
3513
5033
|
* @param filePath - The file path to get the absolute path for
|
|
3514
|
-
* @param options - The options for getting the absolute path. The current working directory is used if not provided.
|
|
3515
5034
|
* @returns {string}
|
|
5035
|
+
* @throws {Error} When restrictAccessToCwd is true and path would resolve outside cwd
|
|
5036
|
+
*/
|
|
5037
|
+
getAbsolutePath(filePath) {
|
|
5038
|
+
if (this.isRelativePath(filePath)) {
|
|
5039
|
+
const sanitizedPath = filePath.replace(/\0/g, "");
|
|
5040
|
+
const resolved = path11.resolve(this._cwd, sanitizedPath);
|
|
5041
|
+
if (this._restrictAccessToCwd) {
|
|
5042
|
+
const normalizedResolved = path11.normalize(resolved);
|
|
5043
|
+
const normalizedCwd = path11.normalize(this._cwd);
|
|
5044
|
+
const isWithinCwd = normalizedResolved === normalizedCwd || normalizedResolved.startsWith(normalizedCwd + path11.sep);
|
|
5045
|
+
if (!isWithinCwd) {
|
|
5046
|
+
throw new Error(
|
|
5047
|
+
`Path traversal attempt blocked: "${filePath}" resolves outside of working directory "${this._cwd}"`
|
|
5048
|
+
);
|
|
5049
|
+
}
|
|
5050
|
+
}
|
|
5051
|
+
return resolved;
|
|
5052
|
+
}
|
|
5053
|
+
return filePath;
|
|
5054
|
+
}
|
|
5055
|
+
/**
|
|
5056
|
+
* Get the Absolute Path with a custom working directory. If it is already absolute it will return the path as is.
|
|
5057
|
+
* When restrictAccessToCwd is enabled, ensures the resolved path stays within the provided cwd boundaries.
|
|
5058
|
+
* @method getAbsolutePathWithCwd
|
|
5059
|
+
* @param filePath - The file path to get the absolute path for
|
|
5060
|
+
* @param cwd - The custom working directory to resolve relative paths from
|
|
5061
|
+
* @returns {string}
|
|
5062
|
+
* @throws {Error} When restrictAccessToCwd is true and path would resolve outside the provided cwd
|
|
3516
5063
|
*/
|
|
3517
|
-
|
|
5064
|
+
getAbsolutePathWithCwd(filePath, cwd3) {
|
|
3518
5065
|
if (this.isRelativePath(filePath)) {
|
|
3519
|
-
const
|
|
3520
|
-
|
|
5066
|
+
const sanitizedPath = filePath.replace(/\0/g, "");
|
|
5067
|
+
const resolved = path11.resolve(cwd3, sanitizedPath);
|
|
5068
|
+
if (this._restrictAccessToCwd) {
|
|
5069
|
+
const normalizedResolved = path11.normalize(resolved);
|
|
5070
|
+
const normalizedCwd = path11.normalize(cwd3);
|
|
5071
|
+
const isWithinCwd = normalizedResolved === normalizedCwd || normalizedResolved.startsWith(normalizedCwd + path11.sep);
|
|
5072
|
+
if (!isWithinCwd) {
|
|
5073
|
+
throw new Error(
|
|
5074
|
+
`Path traversal attempt blocked: "${filePath}" resolves outside of working directory "${cwd3}"`
|
|
5075
|
+
);
|
|
5076
|
+
}
|
|
5077
|
+
}
|
|
5078
|
+
return resolved;
|
|
3521
5079
|
}
|
|
3522
5080
|
return filePath;
|
|
3523
5081
|
}
|
|
3524
5082
|
/**
|
|
3525
|
-
* Rename
|
|
3526
|
-
* @method
|
|
3527
|
-
* @param oldPath - The old path to rename
|
|
3528
|
-
* @param newPath - The new path to rename to
|
|
5083
|
+
* Rename cache keys that start with a given path prefix.
|
|
5084
|
+
* @method renameCacheKeys
|
|
5085
|
+
* @param oldPath - The old path prefix to rename
|
|
5086
|
+
* @param newPath - The new path prefix to rename to
|
|
3529
5087
|
*/
|
|
3530
|
-
|
|
5088
|
+
renameCacheKeys(oldPath, newPath) {
|
|
3531
5089
|
const keys2 = this._cache.keys();
|
|
3532
5090
|
for (const key of keys2) {
|
|
3533
5091
|
if (key.startsWith(oldPath)) {
|
|
@@ -3557,46 +5115,46 @@ function getHashOfOptions(options) {
|
|
|
3557
5115
|
function getMetadataFromFileDescriptor(fileDescriptor) {
|
|
3558
5116
|
return fileDescriptor.meta;
|
|
3559
5117
|
}
|
|
3560
|
-
var _useChecksum, _fileEntryCache, _FormatResultsCache_instances, getFileDescriptor_fn;
|
|
3561
5118
|
var FormatResultsCache = class {
|
|
5119
|
+
#fileEntryCache;
|
|
3562
5120
|
/**
|
|
3563
5121
|
* @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/.prettier-cache`)
|
|
3564
5122
|
* @param {string} cacheStrategy
|
|
3565
5123
|
*/
|
|
3566
5124
|
constructor(cacheFileLocation, cacheStrategy) {
|
|
3567
|
-
__privateAdd(this, _FormatResultsCache_instances);
|
|
3568
|
-
__privateAdd(this, _useChecksum);
|
|
3569
|
-
__privateAdd(this, _fileEntryCache);
|
|
3570
5125
|
const useChecksum = cacheStrategy === "content";
|
|
5126
|
+
const fileEntryCacheOptions = {
|
|
5127
|
+
useChecksum,
|
|
5128
|
+
useModifiedTime: !useChecksum,
|
|
5129
|
+
restrictAccessToCwd: false
|
|
5130
|
+
};
|
|
3571
5131
|
try {
|
|
3572
|
-
|
|
5132
|
+
this.#fileEntryCache = FileEntryDefault.createFromFile(
|
|
3573
5133
|
/* filePath */
|
|
3574
5134
|
cacheFileLocation,
|
|
3575
|
-
|
|
3576
|
-
)
|
|
5135
|
+
fileEntryCacheOptions
|
|
5136
|
+
);
|
|
3577
5137
|
} catch {
|
|
3578
5138
|
if (fs7.existsSync(cacheFileLocation)) {
|
|
3579
5139
|
fs7.unlinkSync(cacheFileLocation);
|
|
3580
|
-
|
|
5140
|
+
this.#fileEntryCache = FileEntryDefault.createFromFile(
|
|
3581
5141
|
/* filePath */
|
|
3582
5142
|
cacheFileLocation,
|
|
3583
|
-
|
|
3584
|
-
)
|
|
5143
|
+
fileEntryCacheOptions
|
|
5144
|
+
);
|
|
3585
5145
|
}
|
|
3586
5146
|
}
|
|
3587
|
-
__privateSet(this, _useChecksum, useChecksum);
|
|
3588
5147
|
}
|
|
3589
5148
|
/**
|
|
3590
5149
|
* @param {string} filePath
|
|
3591
5150
|
* @param {any} options
|
|
3592
5151
|
*/
|
|
3593
5152
|
existsAvailableFormatResultsCache(filePath, options) {
|
|
3594
|
-
|
|
3595
|
-
const fileDescriptor = __privateMethod(this, _FormatResultsCache_instances, getFileDescriptor_fn).call(this, filePath);
|
|
5153
|
+
const fileDescriptor = this.#getFileDescriptor(filePath);
|
|
3596
5154
|
if (fileDescriptor.notFound || fileDescriptor.changed) {
|
|
3597
5155
|
return false;
|
|
3598
5156
|
}
|
|
3599
|
-
const hashOfOptions =
|
|
5157
|
+
const hashOfOptions = getMetadataFromFileDescriptor(fileDescriptor).data?.hashOfOptions;
|
|
3600
5158
|
return hashOfOptions && hashOfOptions === getHashOfOptions(options);
|
|
3601
5159
|
}
|
|
3602
5160
|
/**
|
|
@@ -3604,7 +5162,7 @@ var FormatResultsCache = class {
|
|
|
3604
5162
|
* @param {any} options
|
|
3605
5163
|
*/
|
|
3606
5164
|
setFormatResultsCache(filePath, options) {
|
|
3607
|
-
const fileDescriptor =
|
|
5165
|
+
const fileDescriptor = this.#getFileDescriptor(filePath);
|
|
3608
5166
|
if (!fileDescriptor.notFound) {
|
|
3609
5167
|
const meta = getMetadataFromFileDescriptor(fileDescriptor);
|
|
3610
5168
|
meta.data = { ...meta.data, hashOfOptions: getHashOfOptions(options) };
|
|
@@ -3614,19 +5172,14 @@ var FormatResultsCache = class {
|
|
|
3614
5172
|
* @param {string} filePath
|
|
3615
5173
|
*/
|
|
3616
5174
|
removeFormatResultsCache(filePath) {
|
|
3617
|
-
|
|
5175
|
+
this.#fileEntryCache.removeEntry(filePath);
|
|
3618
5176
|
}
|
|
3619
5177
|
reconcile() {
|
|
3620
|
-
|
|
5178
|
+
this.#fileEntryCache.reconcile();
|
|
5179
|
+
}
|
|
5180
|
+
#getFileDescriptor(filePath) {
|
|
5181
|
+
return this.#fileEntryCache.getFileDescriptor(filePath);
|
|
3621
5182
|
}
|
|
3622
|
-
};
|
|
3623
|
-
_useChecksum = new WeakMap();
|
|
3624
|
-
_fileEntryCache = new WeakMap();
|
|
3625
|
-
_FormatResultsCache_instances = new WeakSet();
|
|
3626
|
-
getFileDescriptor_fn = function(filePath) {
|
|
3627
|
-
return __privateGet(this, _fileEntryCache).getFileDescriptor(filePath, {
|
|
3628
|
-
useModifiedTime: !__privateGet(this, _useChecksum)
|
|
3629
|
-
});
|
|
3630
5183
|
};
|
|
3631
5184
|
var format_results_cache_default = FormatResultsCache;
|
|
3632
5185
|
|
|
@@ -3642,10 +5195,9 @@ import readline from "readline";
|
|
|
3642
5195
|
// node_modules/ansi-regex/index.js
|
|
3643
5196
|
function ansiRegex({ onlyFirst = false } = {}) {
|
|
3644
5197
|
const ST = "(?:\\u0007|\\u001B\\u005C|\\u009C)";
|
|
3645
|
-
const
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
].join("|");
|
|
5198
|
+
const osc = `(?:\\u001B\\][\\s\\S]*?${ST})`;
|
|
5199
|
+
const csi = "[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]";
|
|
5200
|
+
const pattern = `${osc}|${csi}`;
|
|
3649
5201
|
return new RegExp(pattern, onlyFirst ? void 0 : "g");
|
|
3650
5202
|
}
|
|
3651
5203
|
|
|
@@ -3837,23 +5389,23 @@ function wcwidth(ucs, opts) {
|
|
|
3837
5389
|
}
|
|
3838
5390
|
function wcswidth(str, opts) {
|
|
3839
5391
|
let h;
|
|
3840
|
-
let
|
|
5392
|
+
let l;
|
|
3841
5393
|
let s = 0;
|
|
3842
|
-
let
|
|
5394
|
+
let n;
|
|
3843
5395
|
if (typeof str !== "string") return wcwidth(str, opts);
|
|
3844
5396
|
for (let i = 0; i < str.length; i++) {
|
|
3845
5397
|
h = str.charCodeAt(i);
|
|
3846
5398
|
if (h >= 55296 && h <= 56319) {
|
|
3847
|
-
|
|
3848
|
-
if (
|
|
3849
|
-
h = (h - 55296) * 1024 + (
|
|
5399
|
+
l = str.charCodeAt(++i);
|
|
5400
|
+
if (l >= 56320 && l <= 57343) {
|
|
5401
|
+
h = (h - 55296) * 1024 + (l - 56320) + 65536;
|
|
3850
5402
|
} else {
|
|
3851
5403
|
i--;
|
|
3852
5404
|
}
|
|
3853
5405
|
}
|
|
3854
|
-
|
|
3855
|
-
if (
|
|
3856
|
-
s +=
|
|
5406
|
+
n = wcwidth(h, opts);
|
|
5407
|
+
if (n < 0) return -1;
|
|
5408
|
+
s += n;
|
|
3857
5409
|
}
|
|
3858
5410
|
return s;
|
|
3859
5411
|
}
|
|
@@ -3982,7 +5534,7 @@ function applyConfigPrecedence(context, options) {
|
|
|
3982
5534
|
}
|
|
3983
5535
|
async function getOptionsForFile(context, filepath) {
|
|
3984
5536
|
const options = await getOptionsOrDie(context, filepath);
|
|
3985
|
-
const hasPlugins = options
|
|
5537
|
+
const hasPlugins = options?.plugins;
|
|
3986
5538
|
if (hasPlugins) {
|
|
3987
5539
|
await context.pushContextPlugins(options.plugins);
|
|
3988
5540
|
}
|
|
@@ -4016,7 +5568,7 @@ function handleError(context, filename, error, printedFilename, ignoreUnknown) {
|
|
|
4016
5568
|
ignoreUnknown || (ignoreUnknown = context.argv.ignoreUnknown);
|
|
4017
5569
|
const errorIsUndefinedParseError = error instanceof errors.UndefinedParserError;
|
|
4018
5570
|
if (errorIsUndefinedParseError && ignoreUnknown) {
|
|
4019
|
-
printedFilename
|
|
5571
|
+
printedFilename?.clear();
|
|
4020
5572
|
return true;
|
|
4021
5573
|
}
|
|
4022
5574
|
if (printedFilename) {
|
|
@@ -4027,8 +5579,8 @@ function handleError(context, filename, error, printedFilename, ignoreUnknown) {
|
|
|
4027
5579
|
process.exitCode = 2;
|
|
4028
5580
|
return;
|
|
4029
5581
|
}
|
|
4030
|
-
const isParseError = Boolean(error
|
|
4031
|
-
const isValidationError = /^Invalid \S+ value\./u.test(error
|
|
5582
|
+
const isParseError = Boolean(error?.loc);
|
|
5583
|
+
const isValidationError = /^Invalid \S+ value\./u.test(error?.message);
|
|
4032
5584
|
if (isParseError) {
|
|
4033
5585
|
context.logger.error(`${filename}: ${String(error)}`);
|
|
4034
5586
|
} else if (isValidationError || error instanceof errors.ConfigError) {
|
|
@@ -4110,7 +5662,7 @@ async function format3(context, input, opt) {
|
|
|
4110
5662
|
return { formatted: pp, filepath: opt.filepath || "(stdin)\n" };
|
|
4111
5663
|
}
|
|
4112
5664
|
const { performanceTestFlag } = context;
|
|
4113
|
-
if (performanceTestFlag
|
|
5665
|
+
if (performanceTestFlag?.debugBenchmark) {
|
|
4114
5666
|
let Bench;
|
|
4115
5667
|
try {
|
|
4116
5668
|
({ Bench } = await import("tinybench"));
|
|
@@ -4130,7 +5682,7 @@ async function format3(context, input, opt) {
|
|
|
4130
5682
|
context.logger.debug(
|
|
4131
5683
|
"'--debug-benchmark' measurements for formatWithCursor: " + JSON.stringify(result, void 0, 2)
|
|
4132
5684
|
);
|
|
4133
|
-
} else if (performanceTestFlag
|
|
5685
|
+
} else if (performanceTestFlag?.debugRepeat) {
|
|
4134
5686
|
const repeat = performanceTestFlag.debugRepeat;
|
|
4135
5687
|
context.logger.debug(
|
|
4136
5688
|
`'${performanceTestFlag.name}' found, running formatWithCursor ${repeat} times.`
|
|
@@ -4261,12 +5813,12 @@ ${error2.message}`
|
|
|
4261
5813
|
continue;
|
|
4262
5814
|
}
|
|
4263
5815
|
if (isFileIgnored) {
|
|
4264
|
-
printedFilename
|
|
5816
|
+
printedFilename?.clear();
|
|
4265
5817
|
writeOutput(context, { formatted: input }, options);
|
|
4266
5818
|
continue;
|
|
4267
5819
|
}
|
|
4268
5820
|
const start = mockable_default.getTimestamp();
|
|
4269
|
-
const isCacheExists = formatResultsCache
|
|
5821
|
+
const isCacheExists = formatResultsCache?.existsAvailableFormatResultsCache(
|
|
4270
5822
|
filename,
|
|
4271
5823
|
options
|
|
4272
5824
|
);
|
|
@@ -4294,7 +5846,7 @@ ${error2.message}`
|
|
|
4294
5846
|
}
|
|
4295
5847
|
const isDifferent = output !== input;
|
|
4296
5848
|
let shouldSetCache = !isDifferent;
|
|
4297
|
-
printedFilename
|
|
5849
|
+
printedFilename?.clear();
|
|
4298
5850
|
if (performanceTestFlag) {
|
|
4299
5851
|
context.logger.log(
|
|
4300
5852
|
`'${performanceTestFlag.name}' option found, skipped print code or write files.`
|
|
@@ -4335,9 +5887,9 @@ ${error2.message}`
|
|
|
4335
5887
|
writeOutput(context, result, options);
|
|
4336
5888
|
}
|
|
4337
5889
|
if (shouldSetCache) {
|
|
4338
|
-
formatResultsCache
|
|
5890
|
+
formatResultsCache?.setFormatResultsCache(filename, options);
|
|
4339
5891
|
} else {
|
|
4340
|
-
formatResultsCache
|
|
5892
|
+
formatResultsCache?.removeFormatResultsCache(filename);
|
|
4341
5893
|
}
|
|
4342
5894
|
if (isDifferent) {
|
|
4343
5895
|
if (context.argv.check) {
|
|
@@ -4348,7 +5900,7 @@ ${error2.message}`
|
|
|
4348
5900
|
numberOfUnformattedFilesFound += 1;
|
|
4349
5901
|
}
|
|
4350
5902
|
}
|
|
4351
|
-
formatResultsCache
|
|
5903
|
+
formatResultsCache?.reconcile();
|
|
4352
5904
|
if (context.argv.check) {
|
|
4353
5905
|
if (numberOfFilesWithError > 0) {
|
|
4354
5906
|
const files = numberOfFilesWithError === 1 ? "the above file" : `${numberOfFilesWithError} files`;
|
|
@@ -4370,11 +5922,16 @@ ${error2.message}`
|
|
|
4370
5922
|
}
|
|
4371
5923
|
|
|
4372
5924
|
// src/cli/logger.js
|
|
4373
|
-
var {
|
|
5925
|
+
var {
|
|
5926
|
+
argv,
|
|
5927
|
+
env: env2
|
|
5928
|
+
} = process;
|
|
4374
5929
|
var isStderrColorSupported = !(Boolean(env2.NO_COLOR) || argv.includes("--no-color")) && (Boolean(env2.FORCE_COLOR) || argv.includes("--color") || process.platform === "win32" || process.stderr.isTTY && env2.TERM !== "dumb" || Boolean(env2.CI));
|
|
4375
5930
|
var picocolorsStderr = picocolors.createColors(isStderrColorSupported);
|
|
4376
|
-
var emptyLogResult = {
|
|
4377
|
-
|
|
5931
|
+
var emptyLogResult = {
|
|
5932
|
+
clear() {
|
|
5933
|
+
}
|
|
5934
|
+
};
|
|
4378
5935
|
function createLogger(logLevel = "log") {
|
|
4379
5936
|
return {
|
|
4380
5937
|
logLevel,
|
|
@@ -4396,9 +5953,9 @@ function createLogger(logLevel = "log") {
|
|
|
4396
5953
|
clearable: false,
|
|
4397
5954
|
...options
|
|
4398
5955
|
};
|
|
4399
|
-
message =
|
|
4400
|
-
/*
|
|
4401
|
-
|
|
5956
|
+
message = method_replace_all_default(
|
|
5957
|
+
/* OPTIONAL_OBJECT: false */
|
|
5958
|
+
0,
|
|
4402
5959
|
message,
|
|
4403
5960
|
/^/gmu,
|
|
4404
5961
|
prefix
|
|
@@ -4469,9 +6026,9 @@ var OPTION_USAGE_THRESHOLD = 25;
|
|
|
4469
6026
|
var CHOICE_USAGE_MARGIN = 3;
|
|
4470
6027
|
var CHOICE_USAGE_INDENTATION = 2;
|
|
4471
6028
|
function indent(str, spaces) {
|
|
4472
|
-
return
|
|
4473
|
-
/*
|
|
4474
|
-
|
|
6029
|
+
return method_replace_all_default(
|
|
6030
|
+
/* OPTIONAL_OBJECT: false */
|
|
6031
|
+
0,
|
|
4475
6032
|
str,
|
|
4476
6033
|
/^/gmu,
|
|
4477
6034
|
" ".repeat(spaces)
|
|
@@ -4481,17 +6038,14 @@ function createDefaultValueDisplay(value) {
|
|
|
4481
6038
|
return Array.isArray(value) ? `[${value.map(createDefaultValueDisplay).join(", ")}]` : value;
|
|
4482
6039
|
}
|
|
4483
6040
|
function getOptionDefaultValue(context, optionName) {
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
)
|
|
4488
|
-
if ((option == null ? void 0 : option.default) !== void 0) {
|
|
6041
|
+
const option = context.detailedOptions.find(({
|
|
6042
|
+
name
|
|
6043
|
+
}) => name === optionName);
|
|
6044
|
+
if (option?.default !== void 0) {
|
|
4489
6045
|
return option.default;
|
|
4490
6046
|
}
|
|
4491
6047
|
const optionCamelName = camelCase(optionName);
|
|
4492
|
-
return formatOptionsHiddenDefaults[optionCamelName] ?? ((
|
|
4493
|
-
(option2) => !option2.deprecated && option2.name === optionCamelName
|
|
4494
|
-
)) == null ? void 0 : _a.default);
|
|
6048
|
+
return formatOptionsHiddenDefaults[optionCamelName] ?? context.supportOptions.find((option2) => !option2.deprecated && option2.name === optionCamelName)?.default;
|
|
4495
6049
|
}
|
|
4496
6050
|
function createOptionUsageHeader(option) {
|
|
4497
6051
|
const name = `--${option.name}`;
|
|
@@ -4502,9 +6056,9 @@ function createOptionUsageHeader(option) {
|
|
|
4502
6056
|
function createOptionUsageRow(header, content, threshold) {
|
|
4503
6057
|
const separator = header.length >= threshold ? `
|
|
4504
6058
|
${" ".repeat(threshold)}` : " ".repeat(threshold - header.length);
|
|
4505
|
-
const description =
|
|
4506
|
-
/*
|
|
4507
|
-
|
|
6059
|
+
const description = method_replace_all_default(
|
|
6060
|
+
/* OPTIONAL_OBJECT: false */
|
|
6061
|
+
0,
|
|
4508
6062
|
content,
|
|
4509
6063
|
"\n",
|
|
4510
6064
|
`
|
|
@@ -4525,39 +6079,25 @@ function createOptionUsageType(option) {
|
|
|
4525
6079
|
function createChoiceUsages(choices, margin, indentation) {
|
|
4526
6080
|
const activeChoices = choices.filter((choice) => !choice.deprecated);
|
|
4527
6081
|
const threshold = Math.max(0, ...activeChoices.map((choice) => choice.value.length)) + margin;
|
|
4528
|
-
return activeChoices.map(
|
|
4529
|
-
(choice) => indent(
|
|
4530
|
-
createOptionUsageRow(choice.value, choice.description, threshold),
|
|
4531
|
-
indentation
|
|
4532
|
-
)
|
|
4533
|
-
);
|
|
6082
|
+
return activeChoices.map((choice) => indent(createOptionUsageRow(choice.value, choice.description, threshold), indentation));
|
|
4534
6083
|
}
|
|
4535
6084
|
function createOptionUsage(context, option, threshold) {
|
|
4536
6085
|
const header = createOptionUsageHeader(option);
|
|
4537
6086
|
const optionDefaultValue = getOptionDefaultValue(context, option.name);
|
|
4538
|
-
return createOptionUsageRow(
|
|
4539
|
-
|
|
4540
|
-
`${option.description}${optionDefaultValue === void 0 ? "" : `
|
|
4541
|
-
Defaults to ${createDefaultValueDisplay(optionDefaultValue)}.`}`,
|
|
4542
|
-
threshold
|
|
4543
|
-
);
|
|
6087
|
+
return createOptionUsageRow(header, `${option.description}${optionDefaultValue === void 0 ? "" : `
|
|
6088
|
+
Defaults to ${createDefaultValueDisplay(optionDefaultValue)}.`}`, threshold);
|
|
4544
6089
|
}
|
|
4545
6090
|
function getOptionsWithOpposites(options) {
|
|
4546
|
-
const optionsWithOpposites = options.map((option) => [
|
|
4547
|
-
option
|
|
4548
|
-
option.
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
description: option.oppositeDescription
|
|
4553
|
-
} : null
|
|
4554
|
-
]);
|
|
6091
|
+
const optionsWithOpposites = options.map((option) => [option.description ? option : null, option.oppositeDescription ? {
|
|
6092
|
+
...option,
|
|
6093
|
+
name: `no-${option.name}`,
|
|
6094
|
+
type: "boolean",
|
|
6095
|
+
description: option.oppositeDescription
|
|
6096
|
+
} : null]);
|
|
4555
6097
|
return optionsWithOpposites.flat().filter(Boolean);
|
|
4556
6098
|
}
|
|
4557
6099
|
function createUsage(context) {
|
|
4558
|
-
const sortedOptions = context.detailedOptions.sort(
|
|
4559
|
-
(optionA, optionB) => optionA.name.localeCompare(optionB.name)
|
|
4560
|
-
);
|
|
6100
|
+
const sortedOptions = context.detailedOptions.sort((optionA, optionB) => optionA.name.localeCompare(optionB.name));
|
|
4561
6101
|
const options = getOptionsWithOpposites(sortedOptions).filter(
|
|
4562
6102
|
// remove unnecessary option (e.g. `semi`, `color`, etc.), which is only used for --help <flag>
|
|
4563
6103
|
(option) => !(option.type === "boolean" && option.oppositeDescription && !option.name.startsWith("no-"))
|
|
@@ -4565,18 +6105,10 @@ function createUsage(context) {
|
|
|
4565
6105
|
const groupedOptions = groupBy(options, (option) => option.category);
|
|
4566
6106
|
const firstCategories = categoryOrder.slice(0, -1);
|
|
4567
6107
|
const lastCategories = categoryOrder.slice(-1);
|
|
4568
|
-
const restCategories = Object.keys(groupedOptions).filter(
|
|
4569
|
-
|
|
4570
|
-
);
|
|
4571
|
-
const allCategories = [
|
|
4572
|
-
...firstCategories,
|
|
4573
|
-
...restCategories,
|
|
4574
|
-
...lastCategories
|
|
4575
|
-
];
|
|
6108
|
+
const restCategories = Object.keys(groupedOptions).filter((category) => !categoryOrder.includes(category));
|
|
6109
|
+
const allCategories = [...firstCategories, ...restCategories, ...lastCategories];
|
|
4576
6110
|
const optionsUsage = allCategories.map((category) => {
|
|
4577
|
-
const categoryOptions = groupedOptions[category].map(
|
|
4578
|
-
(option) => createOptionUsage(context, option, OPTION_USAGE_THRESHOLD)
|
|
4579
|
-
).join("\n");
|
|
6111
|
+
const categoryOptions = groupedOptions[category].map((option) => createOptionUsage(context, option, OPTION_USAGE_THRESHOLD)).join("\n");
|
|
4580
6112
|
return `${category} options:
|
|
4581
6113
|
|
|
4582
6114
|
${indent(categoryOptions, 2)}`;
|
|
@@ -4587,19 +6119,13 @@ function createPluginDefaults(pluginDefaults) {
|
|
|
4587
6119
|
if (!pluginDefaults || Object.keys(pluginDefaults).length === 0) {
|
|
4588
6120
|
return "";
|
|
4589
6121
|
}
|
|
4590
|
-
const defaults = Object.entries(pluginDefaults).sort(
|
|
4591
|
-
([pluginNameA], [pluginNameB]) => pluginNameA.localeCompare(pluginNameB)
|
|
4592
|
-
).map(
|
|
4593
|
-
([plugin, value]) => `* ${plugin}: ${createDefaultValueDisplay(value)}`
|
|
4594
|
-
).join("\n");
|
|
6122
|
+
const defaults = Object.entries(pluginDefaults).sort(([pluginNameA], [pluginNameB]) => pluginNameA.localeCompare(pluginNameB)).map(([plugin, value]) => `* ${plugin}: ${createDefaultValueDisplay(value)}`).join("\n");
|
|
4595
6123
|
return `
|
|
4596
6124
|
Plugin defaults:
|
|
4597
6125
|
${defaults}`;
|
|
4598
6126
|
}
|
|
4599
6127
|
function createDetailedUsage(context, flag) {
|
|
4600
|
-
const option = getOptionsWithOpposites(context.detailedOptions).find(
|
|
4601
|
-
(option2) => option2.name === flag || option2.alias === flag
|
|
4602
|
-
);
|
|
6128
|
+
const option = getOptionsWithOpposites(context.detailedOptions).find((option2) => option2.name === flag || option2.alias === flag);
|
|
4603
6129
|
const header = createOptionUsageHeader(option);
|
|
4604
6130
|
const description = `
|
|
4605
6131
|
|
|
@@ -4608,11 +6134,7 @@ ${indent(option.description, 2)}`;
|
|
|
4608
6134
|
|
|
4609
6135
|
Valid options:
|
|
4610
6136
|
|
|
4611
|
-
${createChoiceUsages(
|
|
4612
|
-
option.choices,
|
|
4613
|
-
CHOICE_USAGE_MARGIN,
|
|
4614
|
-
CHOICE_USAGE_INDENTATION
|
|
4615
|
-
).join("\n")}`;
|
|
6137
|
+
${createChoiceUsages(option.choices, CHOICE_USAGE_MARGIN, CHOICE_USAGE_INDENTATION).join("\n")}`;
|
|
4616
6138
|
const optionDefaultValue = getOptionDefaultValue(context, option.name);
|
|
4617
6139
|
const defaults = optionDefaultValue !== void 0 ? `
|
|
4618
6140
|
|