@dereekb/nestjs 13.10.9 → 13.11.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/index.esm.js CHANGED
@@ -1,15 +1,17 @@
1
1
  import { assetLoaderFromGetFn, fetchAssetLoader, delegatedAssetLoader, AssetLoader } from '@dereekb/rxjs';
2
2
  import { readFile } from 'fs/promises';
3
- import { resolve } from 'node:path';
3
+ import { resolve, dirname } from 'node:path';
4
4
  import { createParamDecorator, Logger, BadRequestException, InternalServerErrorException, Injectable, RequestMethod, Module, Inject } from '@nestjs/common';
5
5
  import rawbody from 'raw-body';
6
6
  import { parse } from 'querystring';
7
7
  import bodyParser from 'body-parser';
8
- import { asArray, mergeArrays, isHex, getValueFromGetter, PDF_ENCRYPT_MARKER } from '@dereekb/util';
8
+ import { asArray, mergeArrays, memoizeAsyncKeyedValueCache, memoizeAsyncValueCache, isHex, getValueFromGetter, PDF_ENCRYPT_MARKER } from '@dereekb/util';
9
9
  import { ConfigService, ConfigModule } from '@nestjs/config';
10
+ import { readFile as readFile$1, mkdirSync, writeFile, chmod } from 'node:fs';
11
+ import { rm } from 'node:fs/promises';
10
12
  import { createDecipheriv, randomBytes, createCipheriv, createHash } from 'node:crypto';
11
13
 
12
- function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
14
+ function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
13
15
  try {
14
16
  var info = gen[key](arg);
15
17
  var value = info.value;
@@ -23,22 +25,22 @@ function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
23
25
  Promise.resolve(value).then(_next, _throw);
24
26
  }
25
27
  }
26
- function _async_to_generator$1(fn) {
28
+ function _async_to_generator$2(fn) {
27
29
  return function() {
28
30
  var self = this, args = arguments;
29
31
  return new Promise(function(resolve, reject) {
30
32
  var gen = fn.apply(self, args);
31
33
  function _next(value) {
32
- asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
34
+ asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "next", value);
33
35
  }
34
36
  function _throw(err) {
35
- asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
37
+ asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "throw", err);
36
38
  }
37
39
  _next(undefined);
38
40
  });
39
41
  };
40
42
  }
41
- function _ts_generator$1(thisArg, body) {
43
+ function _ts_generator$2(thisArg, body) {
42
44
  var f, y, t, _ = {
43
45
  label: 0,
44
46
  sent: function() {
@@ -157,9 +159,9 @@ function _ts_generator$1(thisArg, body) {
157
159
  */ function nodeJsLocalAssetLoader(config) {
158
160
  var basePath = config.basePath;
159
161
  var getFn = function getFn(ref) {
160
- return _async_to_generator$1(function() {
162
+ return _async_to_generator$2(function() {
161
163
  var localRef, fullPath, buffer;
162
- return _ts_generator$1(this, function(_state) {
164
+ return _ts_generator$2(this, function(_state) {
163
165
  switch(_state.label){
164
166
  case 0:
165
167
  localRef = ref;
@@ -238,7 +240,7 @@ function _ts_generator$1(thisArg, body) {
238
240
  return origin.startsWith('http://localhost') || origin.startsWith('https://localhost');
239
241
  }
240
242
 
241
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
243
+ function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
242
244
  try {
243
245
  var info = gen[key](arg);
244
246
  var value = info.value;
@@ -252,22 +254,22 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
252
254
  Promise.resolve(value).then(_next, _throw);
253
255
  }
254
256
  }
255
- function _async_to_generator(fn) {
257
+ function _async_to_generator$1(fn) {
256
258
  return function() {
257
259
  var self = this, args = arguments;
258
260
  return new Promise(function(resolve, reject) {
259
261
  var gen = fn.apply(self, args);
260
262
  function _next(value) {
261
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
263
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
262
264
  }
263
265
  function _throw(err) {
264
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
266
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
265
267
  }
266
268
  _next(undefined);
267
269
  });
268
270
  };
269
271
  }
270
- function _ts_generator(thisArg, body) {
272
+ function _ts_generator$1(thisArg, body) {
271
273
  var f, y, t, _ = {
272
274
  label: 0,
273
275
  sent: function() {
@@ -380,9 +382,9 @@ function _ts_generator(thisArg, body) {
380
382
  * ```
381
383
  */ var rawBodyLogger = new Logger('RawBody');
382
384
  var ParseRawBody = createParamDecorator(function(_, context) {
383
- return _async_to_generator(function() {
385
+ return _async_to_generator$1(function() {
384
386
  var req, body;
385
- return _ts_generator(this, function(_state) {
387
+ return _ts_generator$1(this, function(_state) {
386
388
  switch(_state.label){
387
389
  case 0:
388
390
  req = context.switchToHttp().getRequest();
@@ -417,9 +419,9 @@ var ParseRawBody = createParamDecorator(function(_, context) {
417
419
  * handleWebhook(@RawBody() body: RawBodyBuffer) { ... }
418
420
  * ```
419
421
  */ var RawBody = createParamDecorator(function(_, context) {
420
- return _async_to_generator(function() {
422
+ return _async_to_generator$1(function() {
421
423
  var req, body;
422
- return _ts_generator(this, function(_state) {
424
+ return _ts_generator$1(this, function(_state) {
423
425
  req = context.switchToHttp().getRequest();
424
426
  body = req.body;
425
427
  if (!Buffer.isBuffer(body)) {
@@ -443,9 +445,9 @@ var ParseRawBody = createParamDecorator(function(_, context) {
443
445
  * handleForm(@ParsedQueryRawBody() body: ParsedUrlQuery) { ... }
444
446
  * ```
445
447
  */ var ParsedQueryRawBody = createParamDecorator(function(_, context) {
446
- return _async_to_generator(function() {
448
+ return _async_to_generator$1(function() {
447
449
  var req;
448
- return _ts_generator(this, function(_state) {
450
+ return _ts_generator$1(this, function(_state) {
449
451
  req = context.switchToHttp().getRequest();
450
452
  req.body = RawBodyToParsedQueryString(req.body);
451
453
  return [
@@ -629,7 +631,7 @@ function _create_class$3(Constructor, protoProps, staticProps) {
629
631
  if (protoProps) _defineProperties$3(Constructor.prototype, protoProps);
630
632
  return Constructor;
631
633
  }
632
- function _define_property$3(obj, key, value) {
634
+ function _define_property$4(obj, key, value) {
633
635
  if (key in obj) {
634
636
  Object.defineProperty(obj, key, {
635
637
  value: value,
@@ -737,7 +739,7 @@ var DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO = {
737
739
  function ConfigureWebhookMiddlewareModule() {
738
740
  _class_call_check$5(this, ConfigureWebhookMiddlewareModule);
739
741
  var _this;
740
- _this = _call_super(this, ConfigureWebhookMiddlewareModule, arguments), _define_property$3(_this, "logger", new Logger('ConfigureWebhookMiddlewareModule'));
742
+ _this = _call_super(this, ConfigureWebhookMiddlewareModule, arguments), _define_property$4(_this, "logger", new Logger('ConfigureWebhookMiddlewareModule'));
741
743
  return _this;
742
744
  }
743
745
  _create_class$3(ConfigureWebhookMiddlewareModule, [
@@ -834,7 +836,7 @@ function _create_class$2(Constructor, protoProps, staticProps) {
834
836
  if (staticProps) _defineProperties$2(Constructor, staticProps);
835
837
  return Constructor;
836
838
  }
837
- function _define_property$2(obj, key, value) {
839
+ function _define_property$3(obj, key, value) {
838
840
  if (key in obj) {
839
841
  Object.defineProperty(obj, key, {
840
842
  value: value,
@@ -854,7 +856,7 @@ function _define_property$2(obj, key, value) {
854
856
  */ var ClientAppServiceConfig = /*#__PURE__*/ function() {
855
857
  function ClientAppServiceConfig() {
856
858
  _class_call_check$4(this, ClientAppServiceConfig);
857
- _define_property$2(this, "client", void 0);
859
+ _define_property$3(this, "client", void 0);
858
860
  }
859
861
  _create_class$2(ClientAppServiceConfig, null, [
860
862
  {
@@ -893,7 +895,7 @@ function _create_class$1(Constructor, protoProps, staticProps) {
893
895
  if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
894
896
  return Constructor;
895
897
  }
896
- function _define_property$1(obj, key, value) {
898
+ function _define_property$2(obj, key, value) {
897
899
  if (key in obj) {
898
900
  Object.defineProperty(obj, key, {
899
901
  value: value,
@@ -911,7 +913,7 @@ function _define_property$1(obj, key, value) {
911
913
  */ var ClientAppService = /*#__PURE__*/ function() {
912
914
  function ClientAppService(config) {
913
915
  _class_call_check$3(this, ClientAppService);
914
- _define_property$1(this, "_config", void 0);
916
+ _define_property$2(this, "_config", void 0);
915
917
  this._config = config;
916
918
  }
917
919
  _create_class$1(ClientAppService, [
@@ -1050,7 +1052,7 @@ function _create_class(Constructor, protoProps, staticProps) {
1050
1052
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
1051
1053
  return Constructor;
1052
1054
  }
1053
- function _define_property(obj, key, value) {
1055
+ function _define_property$1(obj, key, value) {
1054
1056
  if (key in obj) {
1055
1057
  Object.defineProperty(obj, key, {
1056
1058
  value: value,
@@ -1066,7 +1068,7 @@ function _define_property(obj, key, value) {
1066
1068
  var ServerEnvironmentService = /*#__PURE__*/ function() {
1067
1069
  function ServerEnvironmentService(env) {
1068
1070
  _class_call_check(this, ServerEnvironmentService);
1069
- _define_property(this, "env", void 0);
1071
+ _define_property$1(this, "env", void 0);
1070
1072
  this.env = env;
1071
1073
  }
1072
1074
  _create_class(ServerEnvironmentService, [
@@ -1114,6 +1116,639 @@ ServerEnvironmentService = __decorate([
1114
1116
  __param(0, Inject(SERVER_ENV_TOKEN))
1115
1117
  ], ServerEnvironmentService);
1116
1118
 
1119
+ /**
1120
+ * Reads JSON from disk, resolving `undefined` when the file is missing (ENOENT) or its
1121
+ * contents fail to parse as JSON. Other I/O errors (permission denied, busy handles) are
1122
+ * forwarded to the caller — silently swallowing them masks real failures.
1123
+ *
1124
+ * @param filePath - Absolute path to the JSON file to read.
1125
+ * @returns The parsed JSON cast to `T`, or `undefined` when the file does not exist or its contents are not valid JSON.
1126
+ */ function readJsonFile(filePath) {
1127
+ return new Promise(function(resolve, reject) {
1128
+ readFile$1(filePath, {
1129
+ encoding: 'utf-8'
1130
+ }, function(err, data) {
1131
+ if (err) {
1132
+ if (err.code === 'ENOENT') {
1133
+ resolve(undefined);
1134
+ } else {
1135
+ reject(err);
1136
+ }
1137
+ return;
1138
+ }
1139
+ try {
1140
+ resolve(JSON.parse(data));
1141
+ } catch (unused) {
1142
+ resolve(undefined);
1143
+ }
1144
+ });
1145
+ });
1146
+ }
1147
+ /**
1148
+ * Writes a value to disk as JSON. Creates the parent directory if it does not exist.
1149
+ *
1150
+ * The `mode` option is enforced via an explicit `chmod` after the write — `writeFile`'s `mode`
1151
+ * parameter is ignored when the file already exists, which would otherwise leave a
1152
+ * pre-existing file at its original (potentially world-readable) permissions.
1153
+ *
1154
+ * @param input - Configuration describing the destination, payload, and optional file mode.
1155
+ * @param input.filePath - Absolute path to the file to write.
1156
+ * @param input.dirPath - Absolute path to the parent directory; created (recursively) when missing.
1157
+ * @param input.data - Value to serialize as JSON (pretty-printed with two-space indentation).
1158
+ * @param input.mode - Optional numeric file mode (e.g. `0o600`) applied via `chmod` after the write.
1159
+ * @returns A promise that resolves once the JSON has been written and the optional `chmod` has completed.
1160
+ */ function writeJsonFile(input) {
1161
+ mkdirSync(input.dirPath, {
1162
+ recursive: true
1163
+ });
1164
+ return new Promise(function(resolve, reject) {
1165
+ writeFile(input.filePath, JSON.stringify(input.data, null, 2), {
1166
+ mode: input.mode
1167
+ }, function(err) {
1168
+ if (err) {
1169
+ reject(err);
1170
+ return;
1171
+ }
1172
+ if (input.mode == null) {
1173
+ resolve();
1174
+ return;
1175
+ }
1176
+ chmod(input.filePath, input.mode, function(chmodErr) {
1177
+ if (chmodErr) reject(chmodErr);
1178
+ else resolve();
1179
+ });
1180
+ });
1181
+ });
1182
+ }
1183
+ /**
1184
+ * Removes the file at the given path.
1185
+ *
1186
+ * Resolves silently when the file does not exist (`force: true`); other errors
1187
+ * (permission denied, busy file handles) are forwarded to the caller — silently
1188
+ * swallowing them masks real failures and makes operations appear to have succeeded.
1189
+ *
1190
+ * @param filePath - Absolute path to the file to remove.
1191
+ * @returns A promise that resolves once the file is removed (or was already absent).
1192
+ */ function removeFile(filePath) {
1193
+ return rm(filePath, {
1194
+ force: true
1195
+ });
1196
+ }
1197
+
1198
+ function _array_like_to_array$1(arr, len) {
1199
+ if (len == null || len > arr.length) len = arr.length;
1200
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
1201
+ return arr2;
1202
+ }
1203
+ function _array_with_holes$1(arr) {
1204
+ if (Array.isArray(arr)) return arr;
1205
+ }
1206
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
1207
+ try {
1208
+ var info = gen[key](arg);
1209
+ var value = info.value;
1210
+ } catch (error) {
1211
+ reject(error);
1212
+ return;
1213
+ }
1214
+ if (info.done) {
1215
+ resolve(value);
1216
+ } else {
1217
+ Promise.resolve(value).then(_next, _throw);
1218
+ }
1219
+ }
1220
+ function _async_to_generator(fn) {
1221
+ return function() {
1222
+ var self = this, args = arguments;
1223
+ return new Promise(function(resolve, reject) {
1224
+ var gen = fn.apply(self, args);
1225
+ function _next(value) {
1226
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
1227
+ }
1228
+ function _throw(err) {
1229
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
1230
+ }
1231
+ _next(undefined);
1232
+ });
1233
+ };
1234
+ }
1235
+ function _define_property(obj, key, value) {
1236
+ if (key in obj) {
1237
+ Object.defineProperty(obj, key, {
1238
+ value: value,
1239
+ enumerable: true,
1240
+ configurable: true,
1241
+ writable: true
1242
+ });
1243
+ } else {
1244
+ obj[key] = value;
1245
+ }
1246
+ return obj;
1247
+ }
1248
+ function _iterable_to_array_limit$1(arr, i) {
1249
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
1250
+ if (_i == null) return;
1251
+ var _arr = [];
1252
+ var _n = true;
1253
+ var _d = false;
1254
+ var _s, _e;
1255
+ try {
1256
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
1257
+ _arr.push(_s.value);
1258
+ if (i && _arr.length === i) break;
1259
+ }
1260
+ } catch (err) {
1261
+ _d = true;
1262
+ _e = err;
1263
+ } finally{
1264
+ try {
1265
+ if (!_n && _i["return"] != null) _i["return"]();
1266
+ } finally{
1267
+ if (_d) throw _e;
1268
+ }
1269
+ }
1270
+ return _arr;
1271
+ }
1272
+ function _non_iterable_rest$1() {
1273
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1274
+ }
1275
+ function _object_spread(target) {
1276
+ for(var i = 1; i < arguments.length; i++){
1277
+ var source = arguments[i] != null ? arguments[i] : {};
1278
+ var ownKeys = Object.keys(source);
1279
+ if (typeof Object.getOwnPropertySymbols === "function") {
1280
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
1281
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
1282
+ }));
1283
+ }
1284
+ ownKeys.forEach(function(key) {
1285
+ _define_property(target, key, source[key]);
1286
+ });
1287
+ }
1288
+ return target;
1289
+ }
1290
+ function ownKeys(object, enumerableOnly) {
1291
+ var keys = Object.keys(object);
1292
+ if (Object.getOwnPropertySymbols) {
1293
+ var symbols = Object.getOwnPropertySymbols(object);
1294
+ keys.push.apply(keys, symbols);
1295
+ }
1296
+ return keys;
1297
+ }
1298
+ function _object_spread_props(target, source) {
1299
+ source = source != null ? source : {};
1300
+ if (Object.getOwnPropertyDescriptors) {
1301
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
1302
+ } else {
1303
+ ownKeys(Object(source)).forEach(function(key) {
1304
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
1305
+ });
1306
+ }
1307
+ return target;
1308
+ }
1309
+ function _sliced_to_array$1(arr, i) {
1310
+ return _array_with_holes$1(arr) || _iterable_to_array_limit$1(arr, i) || _unsupported_iterable_to_array$1(arr, i) || _non_iterable_rest$1();
1311
+ }
1312
+ function _unsupported_iterable_to_array$1(o, minLen) {
1313
+ if (!o) return;
1314
+ if (typeof o === "string") return _array_like_to_array$1(o, minLen);
1315
+ var n = Object.prototype.toString.call(o).slice(8, -1);
1316
+ if (n === "Object" && o.constructor) n = o.constructor.name;
1317
+ if (n === "Map" || n === "Set") return Array.from(n);
1318
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$1(o, minLen);
1319
+ }
1320
+ function _ts_generator(thisArg, body) {
1321
+ var f, y, t, _ = {
1322
+ label: 0,
1323
+ sent: function() {
1324
+ if (t[0] & 1) throw t[1];
1325
+ return t[1];
1326
+ },
1327
+ trys: [],
1328
+ ops: []
1329
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
1330
+ return d(g, "next", {
1331
+ value: verb(0)
1332
+ }), d(g, "throw", {
1333
+ value: verb(1)
1334
+ }), d(g, "return", {
1335
+ value: verb(2)
1336
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
1337
+ value: function() {
1338
+ return this;
1339
+ }
1340
+ }), g;
1341
+ function verb(n) {
1342
+ return function(v) {
1343
+ return step([
1344
+ n,
1345
+ v
1346
+ ]);
1347
+ };
1348
+ }
1349
+ function step(op) {
1350
+ if (f) throw new TypeError("Generator is already executing.");
1351
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
1352
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
1353
+ if (y = 0, t) op = [
1354
+ op[0] & 2,
1355
+ t.value
1356
+ ];
1357
+ switch(op[0]){
1358
+ case 0:
1359
+ case 1:
1360
+ t = op;
1361
+ break;
1362
+ case 4:
1363
+ _.label++;
1364
+ return {
1365
+ value: op[1],
1366
+ done: false
1367
+ };
1368
+ case 5:
1369
+ _.label++;
1370
+ y = op[1];
1371
+ op = [
1372
+ 0
1373
+ ];
1374
+ continue;
1375
+ case 7:
1376
+ op = _.ops.pop();
1377
+ _.trys.pop();
1378
+ continue;
1379
+ default:
1380
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
1381
+ _ = 0;
1382
+ continue;
1383
+ }
1384
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
1385
+ _.label = op[1];
1386
+ break;
1387
+ }
1388
+ if (op[0] === 6 && _.label < t[1]) {
1389
+ _.label = t[1];
1390
+ t = op;
1391
+ break;
1392
+ }
1393
+ if (t && _.label < t[2]) {
1394
+ _.label = t[2];
1395
+ _.ops.push(op);
1396
+ break;
1397
+ }
1398
+ if (t[2]) _.ops.pop();
1399
+ _.trys.pop();
1400
+ continue;
1401
+ }
1402
+ op = body.call(thisArg, _);
1403
+ } catch (e) {
1404
+ op = [
1405
+ 6,
1406
+ e
1407
+ ];
1408
+ y = 0;
1409
+ } finally{
1410
+ f = t = 0;
1411
+ }
1412
+ if (op[0] & 5) throw op[1];
1413
+ return {
1414
+ value: op[0] ? op[1] : void 0,
1415
+ done: true
1416
+ };
1417
+ }
1418
+ }
1419
+ /**
1420
+ * Default file mode used by the JSON-file caches when none is provided.
1421
+ *
1422
+ * 0o600 restricts the file to read/write for the owning user only — appropriate for
1423
+ * secrets like cached access/refresh tokens.
1424
+ */ var DEFAULT_JSON_FILE_CACHE_MODE = 384;
1425
+ /**
1426
+ * Creates an {@link AsyncValueCache} backed by a single JSON file on disk.
1427
+ *
1428
+ * Reads always touch disk; wrap with {@link memoizeAsyncValueCache} (or use
1429
+ * {@link createMemoizedJsonFileAsyncValueCache}) to add per-process memoization.
1430
+ *
1431
+ * Multi-process invalidation is not provided. Two processes writing to the same file
1432
+ * will last-writer-wins; once memoized, a long-running process will not observe writes
1433
+ * from another process.
1434
+ *
1435
+ * @param input - Configuration bag describing the backing file and optional JSON revive/replace hooks.
1436
+ * @param input.filePath - Absolute path to the JSON file that backs the cache.
1437
+ * @param input.mode - Optional file mode applied on write; defaults to {@link DEFAULT_JSON_FILE_CACHE_MODE} (0o600).
1438
+ * @param input.reviver - Optional transform applied to the raw JSON-parsed payload before it is returned from `load()` (e.g. revive `Date` fields).
1439
+ * @param input.replacer - Optional transform applied to the value before it is stringified to JSON on `update()`.
1440
+ * @returns An {@link AsyncValueCache} that persists the value to the configured JSON file.
1441
+ */ function createJsonFileAsyncValueCache(input) {
1442
+ var filePath = input.filePath, mode = input.mode, reviver = input.reviver, replacer = input.replacer;
1443
+ var fileMode = mode !== null && mode !== void 0 ? mode : DEFAULT_JSON_FILE_CACHE_MODE;
1444
+ return {
1445
+ load: function load() {
1446
+ return _async_to_generator(function() {
1447
+ var raw;
1448
+ return _ts_generator(this, function(_state) {
1449
+ switch(_state.label){
1450
+ case 0:
1451
+ return [
1452
+ 4,
1453
+ readJsonFile(filePath)
1454
+ ];
1455
+ case 1:
1456
+ raw = _state.sent();
1457
+ if (raw == null) {
1458
+ return [
1459
+ 2,
1460
+ undefined
1461
+ ];
1462
+ }
1463
+ return [
1464
+ 2,
1465
+ reviver == null ? raw : reviver(raw)
1466
+ ];
1467
+ }
1468
+ });
1469
+ })();
1470
+ },
1471
+ update: function update(value) {
1472
+ return _async_to_generator(function() {
1473
+ var data;
1474
+ return _ts_generator(this, function(_state) {
1475
+ switch(_state.label){
1476
+ case 0:
1477
+ data = replacer == null ? value : replacer(value);
1478
+ return [
1479
+ 4,
1480
+ writeJsonFile({
1481
+ filePath: filePath,
1482
+ dirPath: dirname(filePath),
1483
+ data: data,
1484
+ mode: fileMode
1485
+ })
1486
+ ];
1487
+ case 1:
1488
+ _state.sent();
1489
+ return [
1490
+ 2
1491
+ ];
1492
+ }
1493
+ });
1494
+ })();
1495
+ },
1496
+ clear: function clear() {
1497
+ return _async_to_generator(function() {
1498
+ return _ts_generator(this, function(_state) {
1499
+ switch(_state.label){
1500
+ case 0:
1501
+ return [
1502
+ 4,
1503
+ removeFile(filePath)
1504
+ ];
1505
+ case 1:
1506
+ _state.sent();
1507
+ return [
1508
+ 2
1509
+ ];
1510
+ }
1511
+ });
1512
+ })();
1513
+ }
1514
+ };
1515
+ }
1516
+ /**
1517
+ * Convenience wrapper around {@link createJsonFileAsyncValueCache} composed with
1518
+ * {@link memoizeAsyncValueCache}.
1519
+ *
1520
+ * @param input - Same configuration accepted by {@link createJsonFileAsyncValueCache}; see {@link CreateJsonFileAsyncValueCacheInput}.
1521
+ * @returns An {@link AsyncValueCache} backed by the JSON file with a per-process single-load memoization layer in front.
1522
+ */ function createMemoizedJsonFileAsyncValueCache(input) {
1523
+ return memoizeAsyncValueCache(createJsonFileAsyncValueCache(input));
1524
+ }
1525
+ /**
1526
+ * Creates an {@link AsyncKeyedValueCache} backed by a single JSON file holding a `Record<string, T>`.
1527
+ *
1528
+ * Reads/writes always touch disk; wrap with {@link memoizeAsyncKeyedValueCache} (or use
1529
+ * {@link createMemoizedJsonFileAsyncKeyedValueCache}) to add per-process memoization of the
1530
+ * entire record.
1531
+ *
1532
+ * Multi-process invalidation is not provided; see {@link createJsonFileAsyncValueCache} for caveats.
1533
+ *
1534
+ * Concurrent `set`/`remove`/`clear` calls are serialized through a per-instance promise chain so
1535
+ * the read-modify-write of the entire record file does not race within the same process.
1536
+ *
1537
+ * @param input - Configuration bag describing the backing file and optional per-entry revive/replace hooks.
1538
+ * @param input.filePath - Absolute path to the JSON file that holds the entire `Record<string, T>`.
1539
+ * @param input.mode - Optional file mode applied on write; defaults to {@link DEFAULT_JSON_FILE_CACHE_MODE} (0o600).
1540
+ * @param input.reviver - Optional transform applied to each entry after it is JSON-parsed on `load()`/`get()`. Returning null/undefined drops the entry.
1541
+ * @param input.replacer - Optional transform applied to each entry before it is stringified on `set()`.
1542
+ * @returns An {@link AsyncKeyedValueCache} that persists all entries in the configured JSON file.
1543
+ */ function createJsonFileAsyncKeyedValueCache(input) {
1544
+ var filePath = input.filePath, mode = input.mode, reviver = input.reviver, replacer = input.replacer;
1545
+ var fileMode = mode !== null && mode !== void 0 ? mode : DEFAULT_JSON_FILE_CACHE_MODE;
1546
+ function readEntries() {
1547
+ return _async_to_generator(function() {
1548
+ var raw, result, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, key, revived;
1549
+ return _ts_generator(this, function(_state) {
1550
+ switch(_state.label){
1551
+ case 0:
1552
+ return [
1553
+ 4,
1554
+ readJsonFile(filePath)
1555
+ ];
1556
+ case 1:
1557
+ raw = _state.sent();
1558
+ if (raw == null) {
1559
+ return [
1560
+ 2,
1561
+ {}
1562
+ ];
1563
+ }
1564
+ if (reviver == null) {
1565
+ return [
1566
+ 2,
1567
+ raw
1568
+ ];
1569
+ }
1570
+ result = {};
1571
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1572
+ try {
1573
+ for(_iterator = Object.keys(raw)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1574
+ key = _step.value;
1575
+ revived = reviver(raw[key]);
1576
+ if (revived != null) {
1577
+ result[key] = revived;
1578
+ }
1579
+ }
1580
+ } catch (err) {
1581
+ _didIteratorError = true;
1582
+ _iteratorError = err;
1583
+ } finally{
1584
+ try {
1585
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1586
+ _iterator.return();
1587
+ }
1588
+ } finally{
1589
+ if (_didIteratorError) {
1590
+ throw _iteratorError;
1591
+ }
1592
+ }
1593
+ }
1594
+ return [
1595
+ 2,
1596
+ result
1597
+ ];
1598
+ }
1599
+ });
1600
+ })();
1601
+ }
1602
+ function writeEntries(entries) {
1603
+ return _async_to_generator(function() {
1604
+ var data;
1605
+ return _ts_generator(this, function(_state) {
1606
+ switch(_state.label){
1607
+ case 0:
1608
+ data = replacer == null ? entries : Object.fromEntries(Object.entries(entries).map(function(param) {
1609
+ var _param = _sliced_to_array$1(param, 2), key = _param[0], value = _param[1];
1610
+ return [
1611
+ key,
1612
+ replacer(value)
1613
+ ];
1614
+ }));
1615
+ return [
1616
+ 4,
1617
+ writeJsonFile({
1618
+ filePath: filePath,
1619
+ dirPath: dirname(filePath),
1620
+ data: data,
1621
+ mode: fileMode
1622
+ })
1623
+ ];
1624
+ case 1:
1625
+ _state.sent();
1626
+ return [
1627
+ 2
1628
+ ];
1629
+ }
1630
+ });
1631
+ })();
1632
+ }
1633
+ // Serialize set/remove through a per-instance promise chain. Without this, two concurrent
1634
+ // mutations would race on the read-modify-write of the entire record file and the second
1635
+ // writer's snapshot of the entries would clobber the first.
1636
+ var mutationQueue = Promise.resolve();
1637
+ function enqueueMutation(fn) {
1638
+ var next = mutationQueue.then(fn, fn);
1639
+ mutationQueue = next.catch(function() {
1640
+ return undefined;
1641
+ });
1642
+ return next;
1643
+ }
1644
+ return {
1645
+ load: readEntries,
1646
+ get: function get(key) {
1647
+ return _async_to_generator(function() {
1648
+ return _ts_generator(this, function(_state) {
1649
+ switch(_state.label){
1650
+ case 0:
1651
+ return [
1652
+ 4,
1653
+ readEntries()
1654
+ ];
1655
+ case 1:
1656
+ return [
1657
+ 2,
1658
+ _state.sent()[key]
1659
+ ];
1660
+ }
1661
+ });
1662
+ })();
1663
+ },
1664
+ set: function set(key, value) {
1665
+ return enqueueMutation(function() {
1666
+ return _async_to_generator(function() {
1667
+ var current;
1668
+ return _ts_generator(this, function(_state) {
1669
+ switch(_state.label){
1670
+ case 0:
1671
+ return [
1672
+ 4,
1673
+ readEntries()
1674
+ ];
1675
+ case 1:
1676
+ current = _state.sent();
1677
+ return [
1678
+ 4,
1679
+ writeEntries(_object_spread_props(_object_spread({}, current), _define_property({}, key, value)))
1680
+ ];
1681
+ case 2:
1682
+ _state.sent();
1683
+ return [
1684
+ 2
1685
+ ];
1686
+ }
1687
+ });
1688
+ })();
1689
+ });
1690
+ },
1691
+ remove: function remove(key) {
1692
+ return enqueueMutation(function() {
1693
+ return _async_to_generator(function() {
1694
+ var current, next;
1695
+ return _ts_generator(this, function(_state) {
1696
+ switch(_state.label){
1697
+ case 0:
1698
+ return [
1699
+ 4,
1700
+ readEntries()
1701
+ ];
1702
+ case 1:
1703
+ current = _state.sent();
1704
+ next = _object_spread({}, current);
1705
+ delete next[key];
1706
+ return [
1707
+ 4,
1708
+ writeEntries(next)
1709
+ ];
1710
+ case 2:
1711
+ _state.sent();
1712
+ return [
1713
+ 2
1714
+ ];
1715
+ }
1716
+ });
1717
+ })();
1718
+ });
1719
+ },
1720
+ clear: function clear() {
1721
+ return enqueueMutation(function() {
1722
+ return _async_to_generator(function() {
1723
+ return _ts_generator(this, function(_state) {
1724
+ switch(_state.label){
1725
+ case 0:
1726
+ return [
1727
+ 4,
1728
+ removeFile(filePath)
1729
+ ];
1730
+ case 1:
1731
+ _state.sent();
1732
+ return [
1733
+ 2
1734
+ ];
1735
+ }
1736
+ });
1737
+ })();
1738
+ });
1739
+ }
1740
+ };
1741
+ }
1742
+ /**
1743
+ * Convenience wrapper around {@link createJsonFileAsyncKeyedValueCache} composed with
1744
+ * {@link memoizeAsyncKeyedValueCache}.
1745
+ *
1746
+ * @param input - Same configuration accepted by {@link createJsonFileAsyncKeyedValueCache}; see {@link CreateJsonFileAsyncKeyedValueCacheInput}.
1747
+ * @returns An {@link AsyncKeyedValueCache} backed by the JSON file with a per-process record-level memoization layer in front.
1748
+ */ function createMemoizedJsonFileAsyncKeyedValueCache(input) {
1749
+ return memoizeAsyncKeyedValueCache(createJsonFileAsyncKeyedValueCache(input));
1750
+ }
1751
+
1117
1752
  // MARK: Constants
1118
1753
  /**
1119
1754
  * AES-256-GCM encryption constants.
@@ -2093,4 +2728,4 @@ function constantTimeEquals(a, b) {
2093
2728
  return result;
2094
2729
  }
2095
2730
 
2096
- export { AppModuleWithWebhooksEnabled, CLIENT_WEB_APP_URL_ENV_VAR, ClientAppModule, ClientAppService, ClientAppServiceConfig, ConfigureWebhookMiddlewareModule, DEFAULT_BASE_WEBHOOK_PATH, DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO, IsRequestFromLocalHost, JsonBodyMiddleware, ParseRawBody, ParsedQueryRawBody, RawBody, RawBodyMiddleware, RawBodyToJson, RawBodyToParsedQueryString, RawBodyToString, SERVER_ENV_TOKEN, ServerEnvironmentConfig, ServerEnvironmentService, appAssetLoaderModuleMetadata, clientAppConfigFactory, consumeWebhooksWithRawBodyMiddleware, createAES256GCMEncryption, createAesStringEncryptionProvider, decryptValue, detectPdfEncryption, encryptValue, injectionTokensFromProviders, isLocalhost, isTestNodeEnv, isValidAES256GCMEncryptionSecret, mergeModuleMetadata, nodeJsLocalAssetLoader, resolveEncryptionKey, serverEnvTokenProvider };
2731
+ export { AppModuleWithWebhooksEnabled, CLIENT_WEB_APP_URL_ENV_VAR, ClientAppModule, ClientAppService, ClientAppServiceConfig, ConfigureWebhookMiddlewareModule, DEFAULT_BASE_WEBHOOK_PATH, DEFAULT_JSON_FILE_CACHE_MODE, DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO, IsRequestFromLocalHost, JsonBodyMiddleware, ParseRawBody, ParsedQueryRawBody, RawBody, RawBodyMiddleware, RawBodyToJson, RawBodyToParsedQueryString, RawBodyToString, SERVER_ENV_TOKEN, ServerEnvironmentConfig, ServerEnvironmentService, appAssetLoaderModuleMetadata, clientAppConfigFactory, consumeWebhooksWithRawBodyMiddleware, createAES256GCMEncryption, createAesStringEncryptionProvider, createJsonFileAsyncKeyedValueCache, createJsonFileAsyncValueCache, createMemoizedJsonFileAsyncKeyedValueCache, createMemoizedJsonFileAsyncValueCache, decryptValue, detectPdfEncryption, encryptValue, injectionTokensFromProviders, isLocalhost, isTestNodeEnv, isValidAES256GCMEncryptionSecret, mergeModuleMetadata, nodeJsLocalAssetLoader, readJsonFile, removeFile, resolveEncryptionKey, serverEnvTokenProvider, writeJsonFile };