@poe-platform/safe-bash 0.1.72 → 0.1.74

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.
@@ -18713,7 +18713,7 @@ function descriptor(value2, limits) {
18713
18713
  for (const flag of flags) if (typeof value2[flag] !== "boolean") fail("protocol", `invalid ${flag} flag`);
18714
18714
  if (kind.value === "rg" && !["sensitive", "insensitive", "smart"].includes(value2.case)) fail("protocol", "invalid case flag");
18715
18715
  if (value2.word || kind.value === "grep" && value2.insensitive || kind.value === "rg" && value2.case !== "sensitive") fail("unsupported", "only case-sensitive, non-word selection is supported");
18716
- if (kind.value === "rg" && !value2.fixed) fail("unsupported", "rg regex and Unicode modes are unsupported; use fixed ASCII patterns");
18716
+ if (kind.value === "rg" && !value2.fixed) fail("unsupported", "rg regex modes are unsupported; use fixed UTF-8 patterns");
18717
18717
  array(value2.patterns, limits.maxPatterns, "pattern");
18718
18718
  let bytes2 = 0;
18719
18719
  for (let index = 0; index < value2.patterns.length; index++) {
@@ -18741,7 +18741,7 @@ function admit(input3, limits, signal) {
18741
18741
  bytes2 += length;
18742
18742
  }
18743
18743
  const ledger = new EreLedger({ maxExpansionBytes: 1048576, maxExpansionFields: 8192 }, {
18744
- patternBytes: limits.maxPatternBytes + 4,
18744
+ patternBytes: limits.maxPatternBytes + (selected.fixed ? 0 : 4),
18745
18745
  subjectBytes: limits.maxInputBytes,
18746
18746
  work: limits.maxWork,
18747
18747
  allocationUnits: limits.maxAllocationUnits,
@@ -18787,8 +18787,116 @@ async function admitBre(pattern, ledger, signal) {
18787
18787
  }
18788
18788
  }
18789
18789
  }
18790
+ async function validateUtf8(input3, ledger, signal) {
18791
+ for (let index = 0; index < input3.length; ) {
18792
+ const first = typeof input3 === "string" ? input3.charCodeAt(index) : input3[index];
18793
+ const width = first < 128 ? 1 : first >= 194 && first <= 223 ? 2 : first >= 224 && first <= 239 ? 3 : first >= 240 && first <= 244 ? 4 : 0;
18794
+ ledger.charge("work", Math.max(width, 1), signal);
18795
+ if (first === 0 || width === 0 || width > input3.length - index) fail("unsupported", "literal patterns and subjects require valid non-NUL UTF-8 bytes");
18796
+ for (let continuation = 1; continuation < width; continuation++) {
18797
+ const byte = typeof input3 === "string" ? input3.charCodeAt(index + continuation) : input3[index + continuation];
18798
+ if (byte < 128 || byte > 191 || continuation === 1 && (first === 224 && byte < 160 || first === 237 && byte > 159 || first === 240 && byte < 144 || first === 244 && byte > 143)) fail("unsupported", "literal patterns and subjects require valid non-NUL UTF-8 bytes");
18799
+ }
18800
+ index += width;
18801
+ await ledger.checkpoint(signal);
18802
+ }
18803
+ }
18804
+ async function literalBytes(pattern, selected, ledger, signal) {
18805
+ const { kind } = selected;
18806
+ let length = 0;
18807
+ if (kind === "grep") {
18808
+ await validateUtf8(pattern, ledger, signal);
18809
+ length = pattern.length;
18810
+ } else {
18811
+ for (let index = 0; index < pattern.length; ) {
18812
+ const scalar = pattern.codePointAt(index);
18813
+ const units = scalar > 65535 ? 2 : 1;
18814
+ ledger.charge("work", units, signal);
18815
+ if (scalar === 0 || scalar >= 55296 && scalar <= 57343) fail("unsupported", "rg literal patterns require non-NUL Unicode scalars for UTF-8");
18816
+ if (scalar === 10 && selected.kind === "rg" && !selected.nullData) fail("unsupported", "rg multiline matching is unsupported");
18817
+ length += scalar < 128 ? 1 : scalar < 2048 ? 2 : scalar < 65536 ? 3 : 4;
18818
+ index += units;
18819
+ await ledger.checkpoint(signal);
18820
+ }
18821
+ }
18822
+ ledger.charge("patternBytes", length, signal);
18823
+ ledger.charge("allocationUnits", length + 1, signal);
18824
+ const bytes2 = new Uint8Array(length);
18825
+ let offset = 0;
18826
+ for (let index = 0; index < pattern.length; ) {
18827
+ const scalar = kind === "grep" ? pattern.charCodeAt(index) : pattern.codePointAt(index);
18828
+ const width = kind === "grep" || scalar < 128 ? 1 : scalar < 2048 ? 2 : scalar < 65536 ? 3 : 4;
18829
+ ledger.charge("work", width, signal);
18830
+ if (width === 1) bytes2[offset++] = scalar;
18831
+ else {
18832
+ bytes2[offset++] = width === 2 ? 192 | scalar >> 6 : width === 3 ? 224 | scalar >> 12 : 240 | scalar >> 18;
18833
+ if (width === 4) bytes2[offset++] = 128 | scalar >> 12 & 63;
18834
+ if (width >= 3) bytes2[offset++] = 128 | scalar >> 6 & 63;
18835
+ bytes2[offset++] = 128 | scalar & 63;
18836
+ }
18837
+ index += kind === "rg" && scalar > 65535 ? 2 : 1;
18838
+ await ledger.checkpoint(signal);
18839
+ }
18840
+ return bytes2;
18841
+ }
18842
+ async function compileLiteral(bytes2, ledger, signal) {
18843
+ ledger.charge("states", bytes2.length, signal);
18844
+ ledger.charge("allocationUnits", bytes2.length * 4 + 3, signal);
18845
+ const fallback = new Uint32Array(bytes2.length);
18846
+ for (let index = 1, prefix2 = 0; index < bytes2.length; ) {
18847
+ ledger.charge("work", 1, signal);
18848
+ if (bytes2[index] === bytes2[prefix2]) fallback[index++] = ++prefix2;
18849
+ else if (prefix2 > 0) prefix2 = fallback[prefix2 - 1];
18850
+ else index++;
18851
+ await ledger.checkpoint(signal);
18852
+ }
18853
+ return { bytes: bytes2, fallback };
18854
+ }
18855
+ async function literalStart(program, subject, whole, ledger, signal) {
18856
+ const { bytes: bytes2, fallback } = program;
18857
+ ledger.charge("work", 1, signal);
18858
+ await ledger.checkpoint(signal);
18859
+ if (whole && bytes2.length !== subject.length || bytes2.length > subject.length) return -1;
18860
+ if (bytes2.length === 0) return 0;
18861
+ for (let index = 0, prefix2 = 0; index < subject.length; ) {
18862
+ ledger.charge("work", 1, signal);
18863
+ if (subject[index] === bytes2[prefix2]) {
18864
+ index++;
18865
+ if (++prefix2 === bytes2.length) return index - prefix2;
18866
+ } else if (prefix2 > 0) prefix2 = fallback[prefix2 - 1];
18867
+ else index++;
18868
+ await ledger.checkpoint(signal);
18869
+ }
18870
+ return -1;
18871
+ }
18872
+ async function executeLiteral(input3, signal) {
18873
+ const { descriptor: selected, rows, ledger } = input3;
18874
+ const programs2 = [];
18875
+ for (const pattern of selected.patterns) {
18876
+ programs2.push(await compileLiteral(await literalBytes(pattern, selected, ledger, signal), ledger, signal));
18877
+ }
18878
+ const results = [];
18879
+ for (const row of rows) {
18880
+ await validateUtf8(row.bytes, ledger, signal);
18881
+ let start = -1;
18882
+ let end = -1;
18883
+ for (const program of programs2) {
18884
+ const candidate = await literalStart(program, row.bytes, selected.whole, ledger, signal);
18885
+ if (candidate < 0) continue;
18886
+ if (start < 0 || candidate < start) {
18887
+ start = candidate;
18888
+ end = start + program.bytes.length;
18889
+ }
18890
+ if (selected.kind === "grep") break;
18891
+ }
18892
+ signal.throwIfAborted();
18893
+ results.push(start < 0 ? new Float64Array() : new Float64Array([start, end]));
18894
+ }
18895
+ return { id: input3.id, results };
18896
+ }
18790
18897
  async function execute2(input3, signal) {
18791
18898
  const { descriptor: selected, rows, ledger } = input3;
18899
+ if (selected.fixed) return executeLiteral(input3, signal);
18792
18900
  const programs2 = [];
18793
18901
  for (const pattern of selected.patterns) {
18794
18902
  if (selected.kind === "grep" && !selected.fixed && !selected.extended) await admitBre(pattern, ledger, signal);