@poe-platform/safe-bash 0.1.58 → 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;
@@ -10197,6 +10186,13 @@ function activeIO(io) {
10197
10186
  ...error?.closed && error.output === io.stderr ? { stderr: closedSink } : {}
10198
10187
  };
10199
10188
  }
10189
+ function appendOutputBytes(current, chunk) {
10190
+ const length = current.length + chunk.length;
10191
+ const bytes2 = current.buffer.byteLength - current.byteOffset >= length ? new Uint8Array(current.buffer, current.byteOffset, length) : new Uint8Array(Math.max(length, current.length * 2, 64));
10192
+ if (bytes2.buffer !== current.buffer) bytes2.set(current);
10193
+ bytes2.set(chunk, current.length);
10194
+ return bytes2.subarray(0, length);
10195
+ }
10200
10196
  var Flow = class extends Error {
10201
10197
  constructor(kind, status, levels = 1) {
10202
10198
  super(kind);
@@ -12167,22 +12163,27 @@ var Runtime = class _Runtime {
12167
12163
  return this.fileOperation(path, async () => {
12168
12164
  if (closed) throw new Error("Output descriptor is closed");
12169
12165
  const current = file.data;
12170
- if (append) {
12171
- await this.fs.appendFile(path, copy2, options2);
12172
- if (current) {
12173
- const bytes2 = new Uint8Array(current.length + copy2.length);
12174
- bytes2.set(current);
12175
- bytes2.set(copy2, current.length);
12176
- file.data = bytes2;
12166
+ let atEOF = false;
12167
+ if (!append && current && offset === current.length && capabilities.append === true && capabilities.stat !== false) {
12168
+ try {
12169
+ atEOF = (await interruptible(this.fs.stat(path, options2), this.signal)).size === offset;
12170
+ } catch {
12171
+ this.signal.throwIfAborted();
12177
12172
  }
12173
+ this.signal.throwIfAborted();
12174
+ }
12175
+ if (append || atEOF) {
12176
+ const bytes2 = current ? appendOutputBytes(current, copy2) : void 0;
12177
+ await this.fs.appendFile(path, copy2, options2);
12178
+ file.data = bytes2;
12178
12179
  } else {
12179
12180
  const bytes2 = new Uint8Array(Math.max(current?.length ?? 0, offset + copy2.length));
12180
12181
  if (current) bytes2.set(current);
12181
12182
  bytes2.set(copy2, offset);
12182
12183
  await this.fs.writeFile(path, bytes2, options2);
12183
12184
  file.data = bytes2;
12184
- offset += copy2.length;
12185
12185
  }
12186
+ if (!append) offset += copy2.length;
12186
12187
  });
12187
12188
  } };
12188
12189
  };
@@ -17370,6 +17371,7 @@ function sedCommand(options2 = {}) {
17370
17371
 
17371
17372
  // packages/safe-bash/src/commands/search/grep.ts
17372
17373
  init_platform();
17374
+ var maxPatternCount = 1024;
17373
17375
  function createGrepCommands(executor) {
17374
17376
  return [{ name: "grep", filesystemRequirements: grepRequirements, execute: (context) => withRegexSession(context, executor, async (session) => {
17375
17377
  try {
@@ -17382,18 +17384,44 @@ function createGrepCommands(executor) {
17382
17384
  const names = parsed.operands.length ? parsed.operands : ["-"];
17383
17385
  const patternFiles = parsed.values.get("f") ?? [];
17384
17386
  const patterns2 = [];
17385
- const addPatterns = (text, file) => {
17386
- if (file && text === "") return;
17387
- const parts = text.split("\n");
17388
- if (parts.length > 1 && parts.at(-1) === "") parts.pop();
17389
- 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
+ };
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
+ }
17390
17411
  };
17391
- for (const pattern of parsed.values.get("e") ?? []) addPatterns(import_buffer.Buffer.from(pattern).toString("latin1"), false);
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);
17392
17420
  for (const name2 of patternFiles) {
17393
- const source = name2 === "-" ? input(context) : requiredFileInput(context, grepRequirements, "pattern-file", name2, bufferLimit);
17394
- 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"));
17395
17423
  }
17396
- if (positionalPattern !== void 0) addPatterns(import_buffer.Buffer.from(positionalPattern).toString("latin1"), false);
17424
+ if (positionalPattern !== void 0) await addArgument(positionalPattern);
17397
17425
  if (parsed.flags.has("E") && parsed.flags.has("F")) throw new UsageError("conflicting matchers specified");
17398
17426
  const descriptor = {
17399
17427
  kind: "grep",