@poe-platform/safe-bash 0.1.59 → 0.1.60

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.
@@ -2796,17 +2796,6 @@ function concatenate(chunks, size = chunks.reduce((sum, chunk) => sum + chunk.le
2796
2796
  }
2797
2797
  return result;
2798
2798
  }
2799
- async function collect(source, signal, limit = bufferLimit) {
2800
- const chunks = [];
2801
- let size = 0;
2802
- for await (const chunk of source) {
2803
- signal.throwIfAborted();
2804
- size += chunk.length;
2805
- if (size > limit) throw new FsError("EFBIG", { message: `buffer limit exceeded (${limit} bytes)` });
2806
- chunks.push(new Uint8Array(chunk));
2807
- }
2808
- return concatenate(chunks, size);
2809
- }
2810
2799
  async function* lines(source, separator = 10) {
2811
2800
  let pending = [];
2812
2801
  let size = 0;
@@ -17382,6 +17371,7 @@ function sedCommand(options2 = {}) {
17382
17371
 
17383
17372
  // packages/safe-bash/src/commands/search/grep.ts
17384
17373
  init_platform();
17374
+ var maxPatternCount = 1024;
17385
17375
  function createGrepCommands(executor) {
17386
17376
  return [{ name: "grep", filesystemRequirements: grepRequirements, execute: (context) => withRegexSession(context, executor, async (session) => {
17387
17377
  try {
@@ -17394,18 +17384,44 @@ function createGrepCommands(executor) {
17394
17384
  const names = parsed.operands.length ? parsed.operands : ["-"];
17395
17385
  const patternFiles = parsed.values.get("f") ?? [];
17396
17386
  const patterns2 = [];
17397
- const addPatterns = (text, file) => {
17398
- if (file && text === "") return;
17399
- const parts = text.split("\n");
17400
- if (parts.length > 1 && parts.at(-1) === "") parts.pop();
17401
- patterns2.push(...parts);
17387
+ let patternCount = 0;
17388
+ let patternBytes = 0;
17389
+ const admit = (chunk, atStart) => {
17390
+ context.signal.throwIfAborted();
17391
+ const size = typeof chunk === "string" ? import_buffer.Buffer.byteLength(chunk) : chunk.length;
17392
+ if (size > bufferLimit - patternBytes) throw new UsageError(`pattern byte limit exceeded (${bufferLimit} bytes)`);
17393
+ patternBytes += size;
17394
+ for (let offset = 0; offset < chunk.length; ) {
17395
+ if (atStart && ++patternCount > maxPatternCount) throw new UsageError(`pattern count limit exceeded (${maxPatternCount})`);
17396
+ const newline = typeof chunk === "string" ? chunk.indexOf("\n", offset) : chunk.indexOf(10, offset);
17397
+ if (newline < 0) return false;
17398
+ offset = newline + 1;
17399
+ atStart = true;
17400
+ }
17401
+ return atStart;
17402
17402
  };
17403
- for (const pattern of parsed.values.get("e") ?? []) addPatterns(import_buffer.Buffer.from(pattern).toString("latin1"), false);
17403
+ const addArgument = async (pattern) => {
17404
+ admit(pattern, true);
17405
+ if (pattern === "") {
17406
+ if (++patternCount > maxPatternCount) throw new UsageError(`pattern count limit exceeded (${maxPatternCount})`);
17407
+ patterns2.push("");
17408
+ } else {
17409
+ for await (const line of lines(toByteSource(pattern))) patterns2.push(import_buffer.Buffer.from(line.bytes).toString("latin1"));
17410
+ }
17411
+ };
17412
+ async function* admitted(source) {
17413
+ let atStart = true;
17414
+ for await (const chunk of source) {
17415
+ atStart = admit(chunk, atStart);
17416
+ yield chunk;
17417
+ }
17418
+ }
17419
+ for (const pattern of parsed.values.get("e") ?? []) await addArgument(pattern);
17404
17420
  for (const name2 of patternFiles) {
17405
- const source = name2 === "-" ? input(context) : requiredFileInput(context, grepRequirements, "pattern-file", name2, bufferLimit);
17406
- addPatterns(import_buffer.Buffer.from(await collect(source, context.signal)).toString("latin1"), true);
17421
+ const source = name2 === "-" ? input(context) : requiredFileInput(context, grepRequirements, "pattern-file", name2, bufferLimit - patternBytes);
17422
+ for await (const line of lines(admitted(source))) patterns2.push(import_buffer.Buffer.from(line.bytes).toString("latin1"));
17407
17423
  }
17408
- if (positionalPattern !== void 0) addPatterns(import_buffer.Buffer.from(positionalPattern).toString("latin1"), false);
17424
+ if (positionalPattern !== void 0) await addArgument(positionalPattern);
17409
17425
  if (parsed.flags.has("E") && parsed.flags.has("F")) throw new UsageError("conflicting matchers specified");
17410
17426
  const descriptor = {
17411
17427
  kind: "grep",