@poe-platform/safe-js 0.1.183 → 0.1.185

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.
@@ -27,7 +27,7 @@ import {
27
27
  validateMigrationSemantics,
28
28
  validateSnapshotData,
29
29
  validateSnapshotMigration
30
- } from "./chunk-7XAXYVOY.js";
30
+ } from "./chunk-ZOPU6AKV.js";
31
31
 
32
32
  // packages/safe-js/src/migrate.ts
33
33
  import { createHash } from "node:crypto";
@@ -8249,4 +8249,4 @@ export {
8249
8249
  parseMcpConfig,
8250
8250
  makeMcpModule
8251
8251
  };
8252
- //# sourceMappingURL=chunk-VZNLY6BP.js.map
8252
+ //# sourceMappingURL=chunk-BWM5IA32.js.map
@@ -10832,6 +10832,8 @@ function callReplaceLikeMethod(value, methodName, args, budget, callClosure, use
10832
10832
  );
10833
10833
  }
10834
10834
  async function replaceRegex(value, regex, replacement, budget, callClosure, context) {
10835
+ if (["exec", "flags", ...Object.keys(regexFlagProperties)].some((key) => getSandboxPropertyDescriptor(regex, key, budget) !== void 0))
10836
+ return replaceObservableRegex(value, regex, replacement, budget, callClosure, context);
10835
10837
  if (regex.flags.includes("g")) regex.lastIndex = 0;
10836
10838
  const cursor = regex.lastIndex;
10837
10839
  let result = "";
@@ -10843,7 +10845,7 @@ async function replaceRegex(value, regex, replacement, budget, callClosure, cont
10843
10845
  let copiedThrough = 0;
10844
10846
  for (const match of matches) {
10845
10847
  result += value.slice(copiedThrough, match.index);
10846
- result += typeof replacement === "string" ? expandReplacement(replacement, match.text, match.captures, value, match.index) : await sandboxString(
10848
+ result += typeof replacement === "string" ? await expandReplacement(replacement, match.text, match.captures, value, match.index, budget, context) : await sandboxString(
10847
10849
  await callClosure(replacement, [match.text, ...match.captures, match.index, value]),
10848
10850
  budget,
10849
10851
  context
@@ -10856,45 +10858,154 @@ async function replaceRegex(value, regex, replacement, budget, callClosure, cont
10856
10858
  budget.setRetainedValues(retained, void 0);
10857
10859
  }
10858
10860
  }
10859
- function expandReplacement(replacement, match, captures, input, matchIndex) {
10861
+ function readReplacementProperty(target, key, budget, context) {
10862
+ if (context?.getProperty !== void 0) return context.getProperty(target, key);
10863
+ const descriptor = getSandboxPropertyDescriptor(target, key, budget);
10864
+ if (descriptor !== void 0) return readPropertyDescriptor(descriptor, target, context);
10865
+ return isSandboxRegex(target) ? getRegexMember(target, key, budget, context) : void 0;
10866
+ }
10867
+ async function replaceObservableRegex(value, regex, replacement, budget, callClosure, context) {
10868
+ const matches = [];
10869
+ let current;
10870
+ let field;
10871
+ let groups;
10872
+ let matched;
10873
+ let captures = [];
10860
10874
  let result = "";
10861
- for (let index = 0; index < replacement.length; index += 1) {
10862
- const character = replacement.charAt(index);
10863
- if (character !== "$") {
10864
- result += character;
10865
- continue;
10875
+ let flags;
10876
+ const release = retainValues(budget, () => [value, regex, replacement, matches, current, field, groups, matched, captures, result, flags]);
10877
+ try {
10878
+ field = await readReplacementProperty(regex, "flags", budget, context);
10879
+ flags = await sandboxString(field, budget, context);
10880
+ field = void 0;
10881
+ const global = flags.includes("g");
10882
+ const fullUnicode = flags.includes("u") || flags.includes("v");
10883
+ flags = void 0;
10884
+ if (global) regex.lastIndex = 0;
10885
+ while (true) {
10886
+ budget.visitNode();
10887
+ current = await regexExec(regex, value, budget, context);
10888
+ if (current === null) break;
10889
+ budget.allocateArrayLength(matches.length + 1);
10890
+ matches.push(current);
10891
+ if (!global) break;
10892
+ field = await readReplacementProperty(current, "0", budget, context);
10893
+ matched = await sandboxString(field, budget, context);
10894
+ field = void 0;
10895
+ if (matched.length === 0) {
10896
+ field = regex.lastIndex;
10897
+ const index = normalizeLastIndex(await sandboxNumber(field, budget, context));
10898
+ const point = fullUnicode ? value.codePointAt(index) : void 0;
10899
+ regex.lastIndex = index + (point !== void 0 && point > 65535 ? 2 : 1);
10900
+ field = void 0;
10901
+ }
10902
+ matched = void 0;
10903
+ current = void 0;
10866
10904
  }
10867
- const token = replacement.charAt(index + 1);
10868
- if (token === "$") {
10869
- result += "$";
10870
- } else if (token === "&") {
10871
- result += match;
10872
- } else if (token === "`") {
10873
- result += input.slice(0, matchIndex);
10874
- } else if (token === "'") {
10875
- result += input.slice(matchIndex + match.length);
10876
- } else if (token >= "0" && token <= "9") {
10877
- let captureIndex = Number(token);
10878
- const nextDigit = replacement.charAt(index + 2);
10879
- if (nextDigit >= "0" && nextDigit <= "9") {
10880
- const twoDigitIndex = Number(token + nextDigit);
10881
- if (twoDigitIndex > 0 && twoDigitIndex <= captures.length) {
10882
- captureIndex = twoDigitIndex;
10883
- index += 1;
10884
- }
10905
+ let copiedThrough = 0;
10906
+ for (current of matches) {
10907
+ field = await readReplacementProperty(current, "length", budget, context);
10908
+ const count = Math.max(normalizeLastIndex(await sandboxNumber(field, budget, context)) - 1, 0);
10909
+ field = await readReplacementProperty(current, "0", budget, context);
10910
+ matched = await sandboxString(field, budget, context);
10911
+ field = await readReplacementProperty(current, "index", budget, context);
10912
+ const number = await sandboxNumber(field, budget, context);
10913
+ const position = Math.min(Math.max(Number.isNaN(number) ? 0 : Math.trunc(number), 0), value.length);
10914
+ field = void 0;
10915
+ captures = [];
10916
+ budget.allocateArrayLength(count);
10917
+ for (let index = 1; index <= count; index++) {
10918
+ budget.visitNode();
10919
+ field = await readReplacementProperty(current, String(index), budget, context);
10920
+ captures.push(field === void 0 ? void 0 : await sandboxString(field, budget, context));
10921
+ field = void 0;
10922
+ }
10923
+ groups = await readReplacementProperty(current, "groups", budget, context);
10924
+ let text;
10925
+ if (typeof replacement === "string") {
10926
+ if (groups === null) throw new TypeError("Replacement groups cannot be null.");
10927
+ if (groups !== void 0 && typeof groups !== "object") {
10928
+ groups = createSandboxBox(groups);
10929
+ budget.chargeDataUsage(measureSandboxData([groups]));
10930
+ }
10931
+ text = await expandReplacement(replacement, matched, captures, value, position, budget, context, groups);
10932
+ } else {
10933
+ const args = [matched, ...captures, position, value];
10934
+ if (groups !== void 0) args.push(groups);
10935
+ budget.allocateArrayLength(args.length);
10936
+ field = await callClosure(replacement, args);
10937
+ text = await sandboxString(field, budget, context);
10938
+ field = void 0;
10939
+ }
10940
+ if (position >= copiedThrough) {
10941
+ result = budget.allocateString(result + value.slice(copiedThrough, position) + text);
10942
+ copiedThrough = position + matched.length;
10885
10943
  }
10886
- if (captureIndex === 0 || captureIndex > captures.length) {
10944
+ matched = void 0;
10945
+ captures = [];
10946
+ groups = void 0;
10947
+ }
10948
+ return budget.allocateString(result + value.slice(copiedThrough));
10949
+ } finally {
10950
+ release();
10951
+ }
10952
+ }
10953
+ async function expandReplacement(replacement, match, captures, input, matchIndex, budget, context, groups) {
10954
+ let result = "";
10955
+ const release = retainValues(budget, () => [replacement, match, captures, input, groups, result]);
10956
+ try {
10957
+ for (let index = 0; index < replacement.length; index += 1) {
10958
+ budget.visitNode();
10959
+ budget.allocateString(result);
10960
+ const character = replacement.charAt(index);
10961
+ if (character !== "$") {
10962
+ result += character;
10963
+ continue;
10964
+ }
10965
+ const token = replacement.charAt(index + 1);
10966
+ if (token === "$") {
10967
+ result += "$";
10968
+ } else if (token === "&") {
10969
+ result += match;
10970
+ } else if (token === "`") {
10971
+ result += input.slice(0, matchIndex);
10972
+ } else if (token === "'") {
10973
+ result += input.slice(matchIndex + match.length);
10974
+ } else if (token === "<" && groups !== void 0) {
10975
+ const end = replacement.indexOf(">", index + 2);
10976
+ if (end === -1) {
10977
+ result += "$";
10978
+ continue;
10979
+ }
10980
+ const capture = await readReplacementProperty(groups, replacement.slice(index + 2, end), budget, context);
10981
+ if (capture !== void 0) result += await sandboxString(capture, budget, context);
10982
+ index = end;
10983
+ continue;
10984
+ } else if (token >= "0" && token <= "9") {
10985
+ let captureIndex = Number(token);
10986
+ const nextDigit = replacement.charAt(index + 2);
10987
+ if (nextDigit >= "0" && nextDigit <= "9") {
10988
+ const twoDigitIndex = Number(token + nextDigit);
10989
+ if (twoDigitIndex > 0 && twoDigitIndex <= captures.length) {
10990
+ captureIndex = twoDigitIndex;
10991
+ index += 1;
10992
+ }
10993
+ }
10994
+ if (captureIndex === 0 || captureIndex > captures.length) {
10995
+ result += "$";
10996
+ continue;
10997
+ }
10998
+ result += captures[captureIndex - 1] ?? "";
10999
+ } else {
10887
11000
  result += "$";
10888
11001
  continue;
10889
11002
  }
10890
- result += captures[captureIndex - 1] ?? "";
10891
- } else {
10892
- result += "$";
10893
- continue;
11003
+ index += 1;
10894
11004
  }
10895
- index += 1;
11005
+ return budget.allocateString(result);
11006
+ } finally {
11007
+ release();
10896
11008
  }
10897
- return result;
10898
11009
  }
10899
11010
  async function replaceWithClosure(value, searchValue, replacer, replaceAll, budget, callClosure, context) {
10900
11011
  const offsets = findReplacementOffsets(value, searchValue, replaceAll);
@@ -34234,16 +34345,38 @@ function createRegexGlobals(options) {
34234
34345
  const operation = options.budget.acquireCompileOwner(false, selected);
34235
34346
  const compilation = context?.compilation?.owner === operation.owner ? context.compilation : new CompileScope(operation.owner);
34236
34347
  let source = "";
34348
+ let retainedSource;
34349
+ let retainedFlags;
34237
34350
  const retained = {};
34238
- options.budget.setRetainedValues(retained, () => [...args, source]);
34351
+ options.budget.setRetainedValues(retained, () => [...args, source, retainedSource, retainedFlags]);
34239
34352
  try {
34240
34353
  const pattern = args[0];
34241
34354
  const flags = args[1];
34242
- if (!construct && isSandboxRegex(pattern) && flags === void 0) return pattern;
34243
- const sourceValue = isSandboxRegex(pattern) ? pattern.source : pattern;
34244
- const flagsValue = isSandboxRegex(pattern) && flags === void 0 ? pattern.flags : flags;
34355
+ const read = (key) => {
34356
+ const descriptor = getSandboxPropertyDescriptor(pattern, key, options.budget);
34357
+ if (key === "constructor" && descriptor === void 0 && isSandboxRegex(pattern)) return constructor;
34358
+ if (context?.getProperty !== void 0) return context.getProperty(pattern, key);
34359
+ return descriptor === void 0 ? void 0 : readPropertyDescriptor(descriptor, pattern, context);
34360
+ };
34361
+ let regexLike = false;
34362
+ if (pattern !== null && typeof pattern === "object") {
34363
+ const match = await read(Symbol.match);
34364
+ regexLike = match === void 0 ? isSandboxRegex(pattern) : Boolean(match);
34365
+ }
34366
+ if (!construct && regexLike && flags === void 0 && await read("constructor") === constructor) return pattern;
34367
+ let sourceValue = pattern;
34368
+ let flagsValue = flags;
34369
+ if (isSandboxRegex(pattern)) {
34370
+ sourceValue = pattern.source;
34371
+ if (flags === void 0) flagsValue = pattern.flags;
34372
+ } else if (regexLike) {
34373
+ sourceValue = retainedSource = await read("source");
34374
+ if (flags === void 0) flagsValue = retainedFlags = await read("flags");
34375
+ }
34245
34376
  source = sourceValue === void 0 ? "" : await sandboxString(sourceValue, options.budget, context);
34377
+ retainedSource = void 0;
34246
34378
  const flagText = flagsValue === void 0 ? "" : await sandboxString(flagsValue, options.budget, context);
34379
+ retainedFlags = void 0;
34247
34380
  const regex = createSandboxRegex(
34248
34381
  source,
34249
34382
  flagText,
@@ -34260,9 +34393,8 @@ function createRegexGlobals(options) {
34260
34393
  operation.release();
34261
34394
  }
34262
34395
  };
34263
- return {
34264
- RegExp: createSandboxClosure({ sandbox: true, name: "RegExp", call: invoke(false), construct: invoke(true) })
34265
- };
34396
+ const constructor = createSandboxClosure({ sandbox: true, name: "RegExp", call: invoke(false), construct: invoke(true) });
34397
+ return { RegExp: constructor };
34266
34398
  }
34267
34399
 
34268
34400
  // packages/safe-js/src/interp/globals/misc.ts
@@ -36575,4 +36707,4 @@ export {
36575
36707
  FileSnapshotBackend,
36576
36708
  run
36577
36709
  };
36578
- //# sourceMappingURL=chunk-7XAXYVOY.js.map
36710
+ //# sourceMappingURL=chunk-ZOPU6AKV.js.map