@poe-platform/safe-bash 0.1.710 → 0.1.712

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.
Files changed (60) hide show
  1. package/README.md +21 -1
  2. package/dist/safe-bash/commands/fmt/index.d.ts +2 -0
  3. package/dist/safe-bash/commands/fmt/index.d.ts.map +1 -0
  4. package/dist/safe-bash/commands/fmt/index.js +2 -0
  5. package/dist/safe-bash/commands/fmt/index.js.map +1 -0
  6. package/dist/safe-bash/commands/fmt.d.ts +1 -2
  7. package/dist/safe-bash/commands/fmt.d.ts.map +1 -1
  8. package/dist/safe-bash/commands/fmt.js +1 -838
  9. package/dist/safe-bash/commands/fmt.js.map +1 -1
  10. package/dist/safe-bash/commands/time-env/printenv.d.ts +1 -1
  11. package/dist/safe-bash/commands/time-env/printenv.d.ts.map +1 -1
  12. package/dist/safe-bash/commands/time-env/sleep.d.ts +1 -1
  13. package/dist/safe-bash/commands/time-env/sleep.d.ts.map +1 -1
  14. package/dist/safe-bash/core.browser.js +155 -2099
  15. package/dist/safe-bash/core.browser.js.map +3 -3
  16. package/dist/safe-bash/playwright/private-target-transport.d.ts.map +1 -1
  17. package/dist/safe-bash/playwright/private-target-transport.js +18 -2
  18. package/dist/safe-bash/playwright/private-target-transport.js.map +1 -1
  19. package/dist/safe-bash/playwright/profile.d.ts +3 -1
  20. package/dist/safe-bash/playwright/profile.d.ts.map +1 -1
  21. package/dist/safe-bash/playwright/profile.js +3 -3
  22. package/dist/safe-bash/playwright/profile.js.map +1 -1
  23. package/dist/safe-bash-command-exiftool/index.js +1401 -6
  24. package/dist/safe-bash-command-exiftool/index.js.map +7 -0
  25. package/dist/safe-bash-command-fmt/arguments.d.ts +8 -0
  26. package/dist/safe-bash-command-fmt/command.d.ts +20 -0
  27. package/dist/safe-bash-command-fmt/contracts.d.ts +42 -0
  28. package/dist/safe-bash-command-fmt/engine.d.ts +20 -0
  29. package/dist/safe-bash-command-fmt/index.d.ts +5 -0
  30. package/dist/safe-bash-command-fmt/index.js +2419 -0
  31. package/dist/safe-bash-command-fmt/index.js.map +7 -0
  32. package/dist/safe-bash-command-fmt/sdk.d.ts +15 -0
  33. package/dist/safe-bash-command-wkhtmltopdf/index.js +1458 -7
  34. package/dist/safe-bash-command-wkhtmltopdf/index.js.map +7 -0
  35. package/dist/safe-bash-contracts/index.d.ts +8 -0
  36. package/dist/safe-bash-contracts/index.js +8 -0
  37. package/dist/safe-playwright-cloudflare/browser-run-code.js +15 -1
  38. package/package.json +6 -2
  39. package/dist/safe-bash-command-exiftool/argfiles.js +0 -163
  40. package/dist/safe-bash-command-exiftool/arguments.js +0 -128
  41. package/dist/safe-bash-command-exiftool/command.js +0 -324
  42. package/dist/safe-bash-command-exiftool/csv.js +0 -78
  43. package/dist/safe-bash-command-exiftool/paths.js +0 -18
  44. package/dist/safe-bash-command-exiftool/png.js +0 -308
  45. package/dist/safe-bash-command-exiftool/publication.js +0 -80
  46. package/dist/safe-bash-command-exiftool/registry.js +0 -14
  47. package/dist/safe-bash-command-exiftool/resources.js +0 -29
  48. package/dist/safe-bash-command-exiftool/scalar.js +0 -103
  49. package/dist/safe-bash-command-exiftool/sdk.js +0 -113
  50. package/dist/safe-bash-command-exiftool/shifts.js +0 -29
  51. package/dist/safe-bash-command-wkhtmltopdf/command.js +0 -373
  52. package/dist/safe-bash-command-wkhtmltopdf/engine.js +0 -99
  53. package/dist/safe-bash-command-wkhtmltopdf/errors.js +0 -11
  54. package/dist/safe-bash-command-wkhtmltopdf/limits.js +0 -69
  55. package/dist/safe-bash-command-wkhtmltopdf/outcome.js +0 -38
  56. package/dist/safe-bash-command-wkhtmltopdf/parser.js +0 -146
  57. package/dist/safe-bash-command-wkhtmltopdf/resources.js +0 -284
  58. package/dist/safe-bash-command-wkhtmltopdf/settings.js +0 -35
  59. package/dist/safe-bash-command-wkhtmltopdf/switches.js +0 -127
  60. package/dist/safe-bash-command-wkhtmltopdf/values.js +0 -66
@@ -1,839 +1,2 @@
1
- import { FsError, getCommandArguments, isFsError, writeBytes } from "../contracts/index.js";
2
- import { assertCommandRequirements } from "../contracts/command-requirements.js";
3
- import { yieldTurn } from "../contracts/yield.js";
4
- import { PublicDiagnostic } from "../diagnostics.js";
5
- import { ByteInputBudget } from "./bytes/input-budget.js";
6
- import { bufferLimit, diagnostic, output, pathOf } from "./internal.js";
7
- import { inputRequirements } from "./portable-requirements.js";
8
- class OptionDiagnostic extends Error {
9
- bytes;
10
- constructor(bytes) {
11
- super("invalid fmt option");
12
- this.bytes = bytes;
13
- }
14
- }
15
- function optionError(message) {
16
- throw new OptionDiagnostic(Uint8Array.from(`fmt: ${message}\nTry 'fmt --help' for more information.\n`, unit => unit.charCodeAt(0)));
17
- }
18
- function widthValue(text, maximum, unicode) {
19
- let offset = 0;
20
- while (text.charCodeAt(offset) === 32 || text.charCodeAt(offset) >= 9 && text.charCodeAt(offset) <= 13)
21
- offset++;
22
- if (text[offset] === "+")
23
- offset++;
24
- const start = offset;
25
- let number = 0;
26
- while (text.charCodeAt(offset) >= 48 && text.charCodeAt(offset) <= 57) {
27
- number = Math.min(1073741824, number * 10 + text.charCodeAt(offset++) - 48);
28
- }
29
- if (offset === start || offset !== text.length || number > maximum) {
30
- const range = offset === text.length && offset !== start && number > maximum;
31
- throw new PublicDiagnostic(`invalid width: ${quote(Uint8Array.from(text, unit => unit.charCodeAt(0)), false, unicode)}${range ? number > 1073741823 ? ": Value too large for defined data type" : ": Numerical result out of range" : ""}`);
32
- }
33
- return number;
34
- }
35
- function parse(context) {
36
- const settings = { width: 75, goal: 70, crown: false, tagged: false, split: false, uniform: false, prefix: new Uint8Array(), leading: 0, fullPrefix: 0, files: [] };
37
- let width;
38
- let goal;
39
- let stopped = false;
40
- const long = { "crown-margin": "c", prefix: "p", "split-only": "s", "tagged-paragraph": "t", "uniform-spacing": "u", width: "w", goal: "g", help: "help", version: "version" };
41
- let size = 0;
42
- for (const argument of context.args) {
43
- size += argument.length;
44
- if (size > 65536 || context.args.length > 4096)
45
- throw new PublicDiagnostic("argument limit exceeded");
46
- }
47
- const argumentsWithBytes = getCommandArguments(context);
48
- size = 0;
49
- const args = context.args.map((_argument, position) => {
50
- const bytes = argumentsWithBytes.bytes(position);
51
- size += bytes.length;
52
- if (size > 65536)
53
- throw new PublicDiagnostic("argument limit exceeded");
54
- return Array.from(bytes, byte => String.fromCharCode(byte)).join("");
55
- });
56
- let index = 0;
57
- const first = args[0];
58
- if (first?.startsWith("-") && first.charCodeAt(1) >= 48 && first.charCodeAt(1) <= 57) {
59
- width = first.slice(1);
60
- index++;
61
- }
62
- for (; index < args.length; index++) {
63
- const argument = args[index];
64
- if (stopped || !argument.startsWith("-") || argument === "-") {
65
- settings.files.push({ name: context.args[index], bytes: argumentsWithBytes.bytes(index) });
66
- if (context.env.POSIXLY_CORRECT !== undefined)
67
- stopped = true;
68
- continue;
69
- }
70
- if (argument === "--") {
71
- stopped = true;
72
- continue;
73
- }
74
- const expanded = [];
75
- if (argument.startsWith("--")) {
76
- const equals = argument.indexOf("=");
77
- const name = argument.slice(2, equals < 0 ? undefined : equals);
78
- const matches = Object.keys(long).filter(key => key.startsWith(name));
79
- const selected = Object.hasOwn(long, name) ? name : matches.length === 1 ? matches[0] : undefined;
80
- if (!selected) {
81
- if (matches.length > 1)
82
- optionError(`option '${argument}' is ambiguous; possibilities: ${matches.map(key => `'--${key}'`).join(" ")}`);
83
- optionError(`unrecognized option '${argument}'`);
84
- }
85
- const key = long[selected];
86
- let value;
87
- let position = index;
88
- if ("pwg".includes(key)) {
89
- value = equals < 0 ? args[++index] : argument.slice(equals + 1);
90
- position = index;
91
- if (value === undefined)
92
- optionError(`option '--${selected}' requires an argument`);
93
- }
94
- else if (equals >= 0)
95
- optionError(`option '--${selected}' doesn't allow an argument`);
96
- expanded.push({ key, ...(value === undefined ? {} : { value }), position, valueOffset: equals < 0 ? 0 : equals + 1 });
97
- }
98
- else {
99
- for (let offset = 1; offset < argument.length; offset++) {
100
- const key = argument[offset];
101
- if (key >= "0" && key <= "9")
102
- optionError(`invalid option -- ${key}; -WIDTH is recognized only when it is the first\noption; use -w N instead`);
103
- if (!"cstuwpg".includes(key))
104
- optionError(`invalid option -- '${key}'`);
105
- let value;
106
- let valueOffset = 0;
107
- if ("pwg".includes(key)) {
108
- if (offset + 1 < argument.length)
109
- valueOffset = offset + 1;
110
- value = argument.slice(offset + 1) || args[++index];
111
- if (value === undefined)
112
- optionError(`option requires an argument -- '${key}'`);
113
- offset = argument.length;
114
- }
115
- expanded.push({ key, ...(value === undefined ? {} : { value }), position: index, valueOffset });
116
- }
117
- }
118
- for (const option of expanded) {
119
- switch (option.key) {
120
- case "c":
121
- settings.crown = true;
122
- break;
123
- case "t":
124
- settings.tagged = true;
125
- break;
126
- case "s":
127
- settings.split = true;
128
- break;
129
- case "u":
130
- settings.uniform = true;
131
- break;
132
- case "w":
133
- width = option.value;
134
- break;
135
- case "g":
136
- goal = option.value;
137
- break;
138
- case "help":
139
- case "version":
140
- settings.information = option.key;
141
- return settings;
142
- case "p": {
143
- const bytes = argumentsWithBytes.bytes(option.position).subarray(option.valueOffset);
144
- let start = 0;
145
- let end = bytes.indexOf(0);
146
- if (end < 0)
147
- end = bytes.length;
148
- while (start < end && bytes[start] === 32)
149
- start++;
150
- settings.leading = start;
151
- settings.fullPrefix = end - start;
152
- while (end > start && bytes[end - 1] === 32)
153
- end--;
154
- settings.prefix = new Uint8Array(bytes.subarray(start, end));
155
- break;
156
- }
157
- }
158
- }
159
- }
160
- const unicode = unicodeLocale(context);
161
- if (width !== undefined)
162
- settings.width = widthValue(width, 2500, unicode);
163
- if (goal !== undefined) {
164
- settings.goal = widthValue(goal, settings.width, unicode);
165
- if (width === undefined)
166
- settings.width = settings.goal + 10;
167
- }
168
- else
169
- settings.goal = Math.trunc(settings.width * 187 / 200);
170
- if (!settings.files.length)
171
- settings.files.push({ name: "-", bytes: Uint8Array.of(45) });
172
- return settings;
173
- }
174
- class WorkBudget {
175
- operations = 0;
176
- output = 0;
177
- *step() {
178
- if (++this.operations > 128 * 1024 * 1024)
179
- throw new PublicDiagnostic("work limit exceeded");
180
- if (this.operations % 4096 === 0)
181
- yield undefined;
182
- }
183
- }
184
- class Formatter {
185
- settings;
186
- budget;
187
- chunk = new Uint8Array();
188
- offset = 0;
189
- eof = false;
190
- pending = [];
191
- text = new Uint8Array(5000);
192
- used = 0;
193
- words = [];
194
- costs = [];
195
- breaks = [];
196
- lengths = [];
197
- column = 0;
198
- nextPrefix = 0;
199
- prefixIndent = 0;
200
- firstIndent = 0;
201
- otherIndent = 0;
202
- lastLength = 0;
203
- outColumn = 0;
204
- tabs = false;
205
- constructor(settings, budget) {
206
- this.settings = settings;
207
- this.budget = budget;
208
- }
209
- *read() {
210
- yield* this.budget.step();
211
- while (this.offset === this.chunk.length) {
212
- if (this.eof)
213
- return -1;
214
- const incoming = yield "input";
215
- if (incoming === null) {
216
- this.eof = true;
217
- return -1;
218
- }
219
- this.chunk = incoming;
220
- this.offset = 0;
221
- }
222
- return this.chunk[this.offset++];
223
- }
224
- *emit(byte) {
225
- if (++this.budget.output > bufferLimit)
226
- throw new PublicDiagnostic("output limit exceeded");
227
- this.pending.push(byte);
228
- if (this.pending.length === 16384) {
229
- yield Uint8Array.from(this.pending);
230
- this.pending = [];
231
- }
232
- }
233
- *spaces(count) {
234
- const target = this.outColumn + count;
235
- const tabEnd = Math.trunc(target / 8) * 8;
236
- if (this.tabs && this.outColumn + 1 < tabEnd) {
237
- while (this.outColumn < tabEnd) {
238
- yield* this.emit(9);
239
- this.outColumn = (Math.trunc(this.outColumn / 8) + 1) * 8;
240
- }
241
- }
242
- while (this.outColumn < target) {
243
- yield* this.emit(32);
244
- this.outColumn++;
245
- }
246
- }
247
- *whitespace(byte) {
248
- while (byte === 32 || byte === 9) {
249
- if (byte === 32)
250
- this.column++;
251
- else {
252
- this.tabs = true;
253
- this.column = (Math.trunc(this.column / 8) + 1) * 8;
254
- }
255
- byte = yield* this.read();
256
- }
257
- return byte;
258
- }
259
- *lineStart() {
260
- this.column = 0;
261
- let byte = yield* this.whitespace(yield* this.read());
262
- const { prefix, leading } = this.settings;
263
- this.nextPrefix = prefix.length ? this.column : Math.min(leading, this.column);
264
- if (prefix.length) {
265
- for (const expected of prefix) {
266
- if (byte !== expected)
267
- return byte;
268
- this.column++;
269
- byte = yield* this.read();
270
- }
271
- byte = yield* this.whitespace(byte);
272
- }
273
- return byte;
274
- }
275
- compatible(byte) {
276
- return byte !== -1 && byte !== 10 && this.nextPrefix === this.prefixIndent && this.column >= this.nextPrefix + this.settings.fullPrefix;
277
- }
278
- secondary(compatible) {
279
- if (this.settings.split)
280
- this.otherIndent = this.firstIndent;
281
- else if (this.settings.crown)
282
- this.otherIndent = compatible ? this.column : this.firstIndent;
283
- else if (this.settings.tagged) {
284
- if (compatible && this.column !== this.firstIndent)
285
- this.otherIndent = this.column;
286
- else if (this.otherIndent === this.firstIndent)
287
- this.otherIndent = this.firstIndent === 0 ? 3 : 0;
288
- }
289
- else
290
- this.otherIndent = this.firstIndent;
291
- }
292
- *optimize() {
293
- const count = this.words.length;
294
- this.costs[count] = 0;
295
- for (let start = count - 1; start >= 0; start--) {
296
- const word = this.words[start];
297
- let penalty = 4900;
298
- const previous = this.words[start - 1];
299
- if (previous?.period)
300
- penalty += previous.final ? -2500 : 360000;
301
- else if (previous?.punctuation)
302
- penalty -= 1600;
303
- else if (previous && this.words[start - 2]?.final)
304
- penalty += Math.trunc(40000 / (previous.length + 2));
305
- if (word.opening)
306
- penalty -= 1600;
307
- else if (word.final)
308
- penalty += Math.trunc(22500 / (word.length + 2));
309
- let length = (start === 0 ? this.firstIndent : this.otherIndent) + word.length;
310
- let best = Infinity;
311
- for (let end = start + 1;; end++) {
312
- yield* this.budget.step();
313
- let cost = this.costs[end];
314
- if (end !== count) {
315
- cost += 100 * (this.settings.goal - length) ** 2;
316
- if (this.breaks[end] !== count)
317
- cost += 50 * (length - this.lengths[end]) ** 2;
318
- }
319
- if (start === 0 && this.lastLength > 0)
320
- cost += 50 * (length - this.lastLength) ** 2;
321
- if (cost < best) {
322
- best = cost;
323
- this.breaks[start] = end;
324
- this.lengths[start] = length;
325
- }
326
- if (end === count)
327
- break;
328
- length += this.words[end - 1].space + this.words[end].length;
329
- if (length >= this.settings.width)
330
- break;
331
- }
332
- this.costs[start] = best + penalty;
333
- }
334
- }
335
- *render(finish) {
336
- for (let start = 0; start < finish; start = this.breaks[start]) {
337
- this.outColumn = 0;
338
- yield* this.spaces(this.prefixIndent);
339
- for (const byte of this.settings.prefix)
340
- yield* this.emit(byte);
341
- this.outColumn += this.settings.prefix.length;
342
- yield* this.spaces((start === 0 ? this.firstIndent : this.otherIndent) - this.outColumn);
343
- const end = this.breaks[start];
344
- for (let index = start; index < end; index++) {
345
- const word = this.words[index];
346
- for (let position = word.start; position < word.start + word.length; position++)
347
- yield* this.emit(this.text[position]);
348
- this.outColumn += word.length;
349
- if (index + 1 !== end)
350
- yield* this.spaces(word.space);
351
- }
352
- this.lastLength = this.outColumn;
353
- yield* this.emit(10);
354
- }
355
- }
356
- *makeRoom(current) {
357
- this.secondary(true);
358
- if (!this.words.length) {
359
- for (let position = 0; position < this.used; position++)
360
- yield* this.emit(this.text[position]);
361
- this.used = 0;
362
- current.start = 0;
363
- return;
364
- }
365
- yield* this.optimize();
366
- let cut = this.words.length;
367
- let score = Infinity;
368
- for (let line = this.breaks[0]; line !== this.words.length; line = this.breaks[line]) {
369
- const candidate = this.costs[line] - this.costs[this.breaks[line]];
370
- if (candidate < score) {
371
- cut = line;
372
- score = candidate;
373
- }
374
- score += 9;
375
- }
376
- yield* this.render(cut);
377
- const offset = cut === this.words.length ? current.start : this.words[cut].start;
378
- this.text.copyWithin(0, offset, this.used);
379
- this.used -= offset;
380
- this.words = this.words.slice(cut);
381
- for (const word of this.words)
382
- word.start -= offset;
383
- current.start -= offset;
384
- }
385
- *readLine(byte) {
386
- do {
387
- const word = { start: this.used, length: 0, space: 0, opening: false, period: false, punctuation: false, final: false };
388
- do {
389
- if (this.used === this.text.length)
390
- yield* this.makeRoom(word);
391
- this.text[this.used++] = byte;
392
- byte = yield* this.read();
393
- } while (byte !== -1 && byte !== 32 && !(byte >= 9 && byte <= 13));
394
- word.length = this.used - word.start;
395
- this.column += word.length;
396
- const first = this.text[word.start];
397
- const last = this.text[this.used - 1];
398
- word.opening = first === 0 || "(['`\"".includes(String.fromCharCode(first));
399
- word.punctuation = last >= 33 && last <= 47 || last >= 58 && last <= 64 || last >= 91 && last <= 96 || last >= 123 && last <= 126;
400
- let terminal = this.used - 1;
401
- while (terminal > word.start && (this.text[terminal] === 0 || ")]'\"".includes(String.fromCharCode(this.text[terminal]))))
402
- terminal--;
403
- word.period = this.text[terminal] === 0 || ".?!".includes(String.fromCharCode(this.text[terminal]));
404
- const before = this.column;
405
- byte = yield* this.whitespace(byte);
406
- word.space = this.column - before;
407
- word.final = byte === -1 || word.period && (byte === 10 || word.space > 1);
408
- if (byte === 10 || byte === -1 || this.settings.uniform)
409
- word.space = word.final ? 2 : 1;
410
- if (this.words.length === 998)
411
- yield* this.makeRoom(word);
412
- this.words.push(word);
413
- } while (byte !== -1 && byte !== 10);
414
- return yield* this.lineStart();
415
- }
416
- *run() {
417
- let byte = yield* this.lineStart();
418
- while (true) {
419
- if (byte === 10 || byte === -1 || this.nextPrefix < this.settings.leading || this.column < this.nextPrefix + this.settings.fullPrefix) {
420
- this.outColumn = 0;
421
- if (this.column > this.nextPrefix || byte !== 10 && byte !== -1) {
422
- yield* this.spaces(this.nextPrefix);
423
- for (const prefixByte of this.settings.prefix) {
424
- if (this.outColumn === this.column)
425
- break;
426
- yield* this.emit(prefixByte);
427
- this.outColumn++;
428
- }
429
- if (byte !== -1 && byte !== 10)
430
- yield* this.spaces(this.column - this.outColumn);
431
- if (byte === -1 && this.column >= this.nextPrefix + this.settings.prefix.length)
432
- yield* this.emit(10);
433
- }
434
- while (byte !== -1 && byte !== 10) {
435
- yield* this.emit(byte);
436
- byte = yield* this.read();
437
- }
438
- if (byte === -1)
439
- break;
440
- yield* this.emit(10);
441
- byte = yield* this.lineStart();
442
- continue;
443
- }
444
- this.lastLength = 0;
445
- this.prefixIndent = this.nextPrefix;
446
- this.firstIndent = this.column;
447
- this.words = [];
448
- this.used = 0;
449
- byte = yield* this.readLine(byte);
450
- this.secondary(this.compatible(byte));
451
- if (!this.settings.split) {
452
- const acceptSecond = this.compatible(byte) && (this.settings.crown || this.settings.tagged ? this.settings.crown || this.column !== this.firstIndent : this.column === this.otherIndent);
453
- if (acceptSecond) {
454
- do {
455
- byte = yield* this.readLine(byte);
456
- } while (this.compatible(byte) && this.column === this.otherIndent);
457
- }
458
- }
459
- const last = this.words[this.words.length - 1];
460
- last.period = true;
461
- last.final = true;
462
- yield* this.optimize();
463
- yield* this.render(this.words.length);
464
- }
465
- if (this.pending.length) {
466
- yield Uint8Array.from(this.pending);
467
- this.pending = [];
468
- }
469
- }
470
- }
471
- class InputScope {
472
- context;
473
- budget;
474
- iterator;
475
- reader;
476
- finished = false;
477
- retirement;
478
- closing;
479
- acquisition;
480
- constructor(context, budget) {
481
- this.context = context;
482
- this.budget = budget;
483
- }
484
- async open(name, bytes) {
485
- const { context } = this;
486
- context.signal.throwIfAborted();
487
- let file;
488
- if (name !== "-") {
489
- const encoded = new TextEncoder().encode(name);
490
- if (encoded.length !== bytes.length || encoded.some((byte, index) => byte !== bytes[index]))
491
- throw new FsError("ENOENT", { path: name });
492
- const path = pathOf(context, name);
493
- const capabilities = await context.fs.capabilitiesFor?.(path, { signal: context.signal }) ?? context.fs.capabilities;
494
- context.signal.throwIfAborted();
495
- file = { path, capabilities };
496
- }
497
- this.acquisition = Promise.resolve().then(() => {
498
- context.signal.throwIfAborted();
499
- let source = context.stdin;
500
- if (file) {
501
- const { path, capabilities } = file;
502
- assertCommandRequirements(context, inputRequirements, ["file"], capabilities);
503
- if (context.fs.readStream && capabilities.streamingRead !== false)
504
- source = context.fs.readStream(path, { signal: context.signal, chunkSize: 65536 });
505
- else {
506
- if (capabilities.read === false)
507
- throw new FsError("ENOTSUP", { path, syscall: "readFile" });
508
- source = { async *[Symbol.asyncIterator]() { yield await context.fs.readFile(path, { signal: context.signal, maxBytes: bufferLimit }); } };
509
- }
510
- }
511
- context.signal.throwIfAborted();
512
- this.iterator = source[Symbol.asyncIterator]();
513
- this.reader = this.budget.read({ [Symbol.asyncIterator]: () => ({
514
- next: async () => {
515
- const item = await this.iterator.next();
516
- if (item.done)
517
- this.finished = true;
518
- return item;
519
- },
520
- return: async () => { await this.retire(); return { done: true, value: undefined }; },
521
- }) }, context.signal)[Symbol.asyncIterator]();
522
- });
523
- return this.acquisition;
524
- }
525
- async next() {
526
- const item = await this.reader.next();
527
- return item.done ? null : new Uint8Array(item.value);
528
- }
529
- retire() {
530
- this.retirement ??= Promise.resolve().then(async () => { if (!this.finished)
531
- await this.iterator?.return?.(); });
532
- return this.retirement;
533
- }
534
- close() {
535
- this.closing ??= (async () => {
536
- await this.acquisition?.catch(() => undefined);
537
- const results = await Promise.allSettled([this.retire(), this.reader?.return?.()]);
538
- for (const result of results)
539
- if (result.status === "rejected")
540
- throw result.reason;
541
- })();
542
- return this.closing;
543
- }
544
- }
545
- const glibc231PrintableRanges = [
546
- 0x20, 0x7e, 0xa0, 0x377, 0x37a, 0x37f, 0x384, 0x38a, 0x38c, 0x38c, 0x38e, 0x3a1, 0x3a3, 0x52f, 0x531, 0x556,
547
- 0x559, 0x55f, 0x561, 0x587, 0x589, 0x58a, 0x58d, 0x58f, 0x591, 0x5c7, 0x5d0, 0x5ea, 0x5f0, 0x5f4, 0x600, 0x61c,
548
- 0x61e, 0x70d, 0x70f, 0x74a, 0x74d, 0x7b1, 0x7c0, 0x7fa, 0x800, 0x82d, 0x830, 0x83e, 0x840, 0x85b, 0x85e, 0x85e,
549
- 0x8a0, 0x8b4, 0x8b6, 0x8bd, 0x8d4, 0x983, 0x985, 0x98c, 0x98f, 0x990, 0x993, 0x9a8, 0x9aa, 0x9b0, 0x9b2, 0x9b2,
550
- 0x9b6, 0x9b9, 0x9bc, 0x9c4, 0x9c7, 0x9c8, 0x9cb, 0x9ce, 0x9d7, 0x9d7, 0x9dc, 0x9dd, 0x9df, 0x9e3, 0x9e6, 0x9fb,
551
- 0xa01, 0xa03, 0xa05, 0xa0a, 0xa0f, 0xa10, 0xa13, 0xa28, 0xa2a, 0xa30, 0xa32, 0xa33, 0xa35, 0xa36, 0xa38, 0xa39,
552
- 0xa3c, 0xa3c, 0xa3e, 0xa42, 0xa47, 0xa48, 0xa4b, 0xa4d, 0xa51, 0xa51, 0xa59, 0xa5c, 0xa5e, 0xa5e, 0xa66, 0xa75,
553
- 0xa81, 0xa83, 0xa85, 0xa8d, 0xa8f, 0xa91, 0xa93, 0xaa8, 0xaaa, 0xab0, 0xab2, 0xab3, 0xab5, 0xab9, 0xabc, 0xac5,
554
- 0xac7, 0xac9, 0xacb, 0xacd, 0xad0, 0xad0, 0xae0, 0xae3, 0xae6, 0xaf1, 0xaf9, 0xaf9, 0xb01, 0xb03, 0xb05, 0xb0c,
555
- 0xb0f, 0xb10, 0xb13, 0xb28, 0xb2a, 0xb30, 0xb32, 0xb33, 0xb35, 0xb39, 0xb3c, 0xb44, 0xb47, 0xb48, 0xb4b, 0xb4d,
556
- 0xb56, 0xb57, 0xb5c, 0xb5d, 0xb5f, 0xb63, 0xb66, 0xb77, 0xb82, 0xb83, 0xb85, 0xb8a, 0xb8e, 0xb90, 0xb92, 0xb95,
557
- 0xb99, 0xb9a, 0xb9c, 0xb9c, 0xb9e, 0xb9f, 0xba3, 0xba4, 0xba8, 0xbaa, 0xbae, 0xbb9, 0xbbe, 0xbc2, 0xbc6, 0xbc8,
558
- 0xbca, 0xbcd, 0xbd0, 0xbd0, 0xbd7, 0xbd7, 0xbe6, 0xbfa, 0xc00, 0xc03, 0xc05, 0xc0c, 0xc0e, 0xc10, 0xc12, 0xc28,
559
- 0xc2a, 0xc39, 0xc3d, 0xc44, 0xc46, 0xc48, 0xc4a, 0xc4d, 0xc55, 0xc56, 0xc58, 0xc5a, 0xc60, 0xc63, 0xc66, 0xc6f,
560
- 0xc78, 0xc83, 0xc85, 0xc8c, 0xc8e, 0xc90, 0xc92, 0xca8, 0xcaa, 0xcb3, 0xcb5, 0xcb9, 0xcbc, 0xcc4, 0xcc6, 0xcc8,
561
- 0xcca, 0xccd, 0xcd5, 0xcd6, 0xcde, 0xcde, 0xce0, 0xce3, 0xce6, 0xcef, 0xcf1, 0xcf2, 0xd01, 0xd03, 0xd05, 0xd0c,
562
- 0xd0e, 0xd10, 0xd12, 0xd3a, 0xd3d, 0xd44, 0xd46, 0xd48, 0xd4a, 0xd4f, 0xd54, 0xd63, 0xd66, 0xd7f, 0xd82, 0xd83,
563
- 0xd85, 0xd96, 0xd9a, 0xdb1, 0xdb3, 0xdbb, 0xdbd, 0xdbd, 0xdc0, 0xdc6, 0xdca, 0xdca, 0xdcf, 0xdd4, 0xdd6, 0xdd6,
564
- 0xdd8, 0xddf, 0xde6, 0xdef, 0xdf2, 0xdf4, 0xe01, 0xe3a, 0xe3f, 0xe5b, 0xe81, 0xe82, 0xe84, 0xe84, 0xe87, 0xe88,
565
- 0xe8a, 0xe8a, 0xe8d, 0xe8d, 0xe94, 0xe97, 0xe99, 0xe9f, 0xea1, 0xea3, 0xea5, 0xea5, 0xea7, 0xea7, 0xeaa, 0xeab,
566
- 0xead, 0xeb9, 0xebb, 0xebd, 0xec0, 0xec4, 0xec6, 0xec6, 0xec8, 0xecd, 0xed0, 0xed9, 0xedc, 0xedf, 0xf00, 0xf47,
567
- 0xf49, 0xf6c, 0xf71, 0xf97, 0xf99, 0xfbc, 0xfbe, 0xfcc, 0xfce, 0xfda, 0x1000, 0x10c5, 0x10c7, 0x10c7, 0x10cd, 0x10cd,
568
- 0x10d0, 0x1248, 0x124a, 0x124d, 0x1250, 0x1256, 0x1258, 0x1258, 0x125a, 0x125d, 0x1260, 0x1288, 0x128a, 0x128d, 0x1290, 0x12b0,
569
- 0x12b2, 0x12b5, 0x12b8, 0x12be, 0x12c0, 0x12c0, 0x12c2, 0x12c5, 0x12c8, 0x12d6, 0x12d8, 0x1310, 0x1312, 0x1315, 0x1318, 0x135a,
570
- 0x135d, 0x137c, 0x1380, 0x1399, 0x13a0, 0x13f5, 0x13f8, 0x13fd, 0x1400, 0x169c, 0x16a0, 0x16f8, 0x1700, 0x170c, 0x170e, 0x1714,
571
- 0x1720, 0x1736, 0x1740, 0x1753, 0x1760, 0x176c, 0x176e, 0x1770, 0x1772, 0x1773, 0x1780, 0x17dd, 0x17e0, 0x17e9, 0x17f0, 0x17f9,
572
- 0x1800, 0x180e, 0x1810, 0x1819, 0x1820, 0x1877, 0x1880, 0x18aa, 0x18b0, 0x18f5, 0x1900, 0x191e, 0x1920, 0x192b, 0x1930, 0x193b,
573
- 0x1940, 0x1940, 0x1944, 0x196d, 0x1970, 0x1974, 0x1980, 0x19ab, 0x19b0, 0x19c9, 0x19d0, 0x19da, 0x19de, 0x1a1b, 0x1a1e, 0x1a5e,
574
- 0x1a60, 0x1a7c, 0x1a7f, 0x1a89, 0x1a90, 0x1a99, 0x1aa0, 0x1aad, 0x1ab0, 0x1abe, 0x1b00, 0x1b4b, 0x1b50, 0x1b7c, 0x1b80, 0x1bf3,
575
- 0x1bfc, 0x1c37, 0x1c3b, 0x1c49, 0x1c4d, 0x1c88, 0x1cc0, 0x1cc7, 0x1cd0, 0x1cf6, 0x1cf8, 0x1cf9, 0x1d00, 0x1df5, 0x1dfb, 0x1f15,
576
- 0x1f18, 0x1f1d, 0x1f20, 0x1f45, 0x1f48, 0x1f4d, 0x1f50, 0x1f57, 0x1f59, 0x1f59, 0x1f5b, 0x1f5b, 0x1f5d, 0x1f5d, 0x1f5f, 0x1f7d,
577
- 0x1f80, 0x1fb4, 0x1fb6, 0x1fc4, 0x1fc6, 0x1fd3, 0x1fd6, 0x1fdb, 0x1fdd, 0x1fef, 0x1ff2, 0x1ff4, 0x1ff6, 0x1ffe, 0x2000, 0x2027,
578
- 0x202a, 0x2064, 0x2066, 0x2071, 0x2074, 0x208e, 0x2090, 0x209c, 0x20a0, 0x20be, 0x20d0, 0x20f0, 0x2100, 0x218b, 0x2190, 0x23fe,
579
- 0x2400, 0x2426, 0x2440, 0x244a, 0x2460, 0x2b73, 0x2b76, 0x2b95, 0x2b98, 0x2bb9, 0x2bbd, 0x2bc8, 0x2bca, 0x2bd1, 0x2bec, 0x2bef,
580
- 0x2c00, 0x2c2e, 0x2c30, 0x2c5e, 0x2c60, 0x2cf3, 0x2cf9, 0x2d25, 0x2d27, 0x2d27, 0x2d2d, 0x2d2d, 0x2d30, 0x2d67, 0x2d6f, 0x2d70,
581
- 0x2d7f, 0x2d96, 0x2da0, 0x2da6, 0x2da8, 0x2dae, 0x2db0, 0x2db6, 0x2db8, 0x2dbe, 0x2dc0, 0x2dc6, 0x2dc8, 0x2dce, 0x2dd0, 0x2dd6,
582
- 0x2dd8, 0x2dde, 0x2de0, 0x2e44, 0x2e80, 0x2e99, 0x2e9b, 0x2ef3, 0x2f00, 0x2fd5, 0x2ff0, 0x2ffb, 0x3000, 0x303f, 0x3041, 0x3096,
583
- 0x3099, 0x30ff, 0x3105, 0x312d, 0x3131, 0x318e, 0x3190, 0x31ba, 0x31c0, 0x31e3, 0x31f0, 0x321e, 0x3220, 0x32fe, 0x3300, 0x4db5,
584
- 0x4dc0, 0x9fd5, 0xa000, 0xa48c, 0xa490, 0xa4c6, 0xa4d0, 0xa62b, 0xa640, 0xa6f7, 0xa700, 0xa7ae, 0xa7b0, 0xa7b7, 0xa7f7, 0xa82b,
585
- 0xa830, 0xa839, 0xa840, 0xa877, 0xa880, 0xa8c5, 0xa8ce, 0xa8d9, 0xa8e0, 0xa8fd, 0xa900, 0xa953, 0xa95f, 0xa97c, 0xa980, 0xa9cd,
586
- 0xa9cf, 0xa9d9, 0xa9de, 0xa9fe, 0xaa00, 0xaa36, 0xaa40, 0xaa4d, 0xaa50, 0xaa59, 0xaa5c, 0xaac2, 0xaadb, 0xaaf6, 0xab01, 0xab06,
587
- 0xab09, 0xab0e, 0xab11, 0xab16, 0xab20, 0xab26, 0xab28, 0xab2e, 0xab30, 0xab65, 0xab70, 0xabed, 0xabf0, 0xabf9, 0xac00, 0xd7a3,
588
- 0xd7b0, 0xd7c6, 0xd7cb, 0xd7fb, 0xe000, 0xfa6d, 0xfa70, 0xfad9, 0xfb00, 0xfb06, 0xfb13, 0xfb17, 0xfb1d, 0xfb36, 0xfb38, 0xfb3c,
589
- 0xfb3e, 0xfb3e, 0xfb40, 0xfb41, 0xfb43, 0xfb44, 0xfb46, 0xfbc1, 0xfbd3, 0xfd3f, 0xfd50, 0xfd8f, 0xfd92, 0xfdc7, 0xfdf0, 0xfdfd,
590
- 0xfe00, 0xfe19, 0xfe20, 0xfe52, 0xfe54, 0xfe66, 0xfe68, 0xfe6b, 0xfe70, 0xfe74, 0xfe76, 0xfefc, 0xfeff, 0xfeff, 0xff01, 0xffbe,
591
- 0xffc2, 0xffc7, 0xffca, 0xffcf, 0xffd2, 0xffd7, 0xffda, 0xffdc, 0xffe0, 0xffe6, 0xffe8, 0xffee, 0xfff9, 0xfffd, 0x10000, 0x1000b,
592
- 0x1000d, 0x10026, 0x10028, 0x1003a, 0x1003c, 0x1003d, 0x1003f, 0x1004d, 0x10050, 0x1005d, 0x10080, 0x100fa, 0x10100, 0x10102, 0x10107, 0x10133,
593
- 0x10137, 0x1018e, 0x10190, 0x1019b, 0x101a0, 0x101a0, 0x101d0, 0x101fd, 0x10280, 0x1029c, 0x102a0, 0x102d0, 0x102e0, 0x102fb, 0x10300, 0x10323,
594
- 0x10330, 0x1034a, 0x10350, 0x1037a, 0x10380, 0x1039d, 0x1039f, 0x103c3, 0x103c8, 0x103d5, 0x10400, 0x1049d, 0x104a0, 0x104a9, 0x104b0, 0x104d3,
595
- 0x104d8, 0x104fb, 0x10500, 0x10527, 0x10530, 0x10563, 0x1056f, 0x1056f, 0x10600, 0x10736, 0x10740, 0x10755, 0x10760, 0x10767, 0x10800, 0x10805,
596
- 0x10808, 0x10808, 0x1080a, 0x10835, 0x10837, 0x10838, 0x1083c, 0x1083c, 0x1083f, 0x10855, 0x10857, 0x1089e, 0x108a7, 0x108af, 0x108e0, 0x108f2,
597
- 0x108f4, 0x108f5, 0x108fb, 0x1091b, 0x1091f, 0x10939, 0x1093f, 0x1093f, 0x10980, 0x109b7, 0x109bc, 0x109cf, 0x109d2, 0x10a03, 0x10a05, 0x10a06,
598
- 0x10a0c, 0x10a13, 0x10a15, 0x10a17, 0x10a19, 0x10a33, 0x10a38, 0x10a3a, 0x10a3f, 0x10a47, 0x10a50, 0x10a58, 0x10a60, 0x10a9f, 0x10ac0, 0x10ae6,
599
- 0x10aeb, 0x10af6, 0x10b00, 0x10b35, 0x10b39, 0x10b55, 0x10b58, 0x10b72, 0x10b78, 0x10b91, 0x10b99, 0x10b9c, 0x10ba9, 0x10baf, 0x10c00, 0x10c48,
600
- 0x10c80, 0x10cb2, 0x10cc0, 0x10cf2, 0x10cfa, 0x10cff, 0x10e60, 0x10e7e, 0x11000, 0x1104d, 0x11052, 0x1106f, 0x1107f, 0x110c1, 0x110d0, 0x110e8,
601
- 0x110f0, 0x110f9, 0x11100, 0x11134, 0x11136, 0x11143, 0x11150, 0x11176, 0x11180, 0x111cd, 0x111d0, 0x111df, 0x111e1, 0x111f4, 0x11200, 0x11211,
602
- 0x11213, 0x1123e, 0x11280, 0x11286, 0x11288, 0x11288, 0x1128a, 0x1128d, 0x1128f, 0x1129d, 0x1129f, 0x112a9, 0x112b0, 0x112ea, 0x112f0, 0x112f9,
603
- 0x11300, 0x11303, 0x11305, 0x1130c, 0x1130f, 0x11310, 0x11313, 0x11328, 0x1132a, 0x11330, 0x11332, 0x11333, 0x11335, 0x11339, 0x1133c, 0x11344,
604
- 0x11347, 0x11348, 0x1134b, 0x1134d, 0x11350, 0x11350, 0x11357, 0x11357, 0x1135d, 0x11363, 0x11366, 0x1136c, 0x11370, 0x11374, 0x11400, 0x11459,
605
- 0x1145b, 0x1145b, 0x1145d, 0x1145d, 0x11480, 0x114c7, 0x114d0, 0x114d9, 0x11580, 0x115b5, 0x115b8, 0x115dd, 0x11600, 0x11644, 0x11650, 0x11659,
606
- 0x11660, 0x1166c, 0x11680, 0x116b7, 0x116c0, 0x116c9, 0x11700, 0x11719, 0x1171d, 0x1172b, 0x11730, 0x1173f, 0x118a0, 0x118f2, 0x118ff, 0x118ff,
607
- 0x11ac0, 0x11af8, 0x11c00, 0x11c08, 0x11c0a, 0x11c36, 0x11c38, 0x11c45, 0x11c50, 0x11c6c, 0x11c70, 0x11c8f, 0x11c92, 0x11ca7, 0x11ca9, 0x11cb6,
608
- 0x12000, 0x12399, 0x12400, 0x1246e, 0x12470, 0x12474, 0x12480, 0x12543, 0x13000, 0x1342e, 0x14400, 0x14646, 0x16800, 0x16a38, 0x16a40, 0x16a5e,
609
- 0x16a60, 0x16a69, 0x16a6e, 0x16a6f, 0x16ad0, 0x16aed, 0x16af0, 0x16af5, 0x16b00, 0x16b45, 0x16b50, 0x16b59, 0x16b5b, 0x16b61, 0x16b63, 0x16b77,
610
- 0x16b7d, 0x16b8f, 0x16f00, 0x16f44, 0x16f50, 0x16f7e, 0x16f8f, 0x16f9f, 0x16fe0, 0x16fe0, 0x17000, 0x187ec, 0x18800, 0x18af2, 0x1b000, 0x1b001,
611
- 0x1bc00, 0x1bc6a, 0x1bc70, 0x1bc7c, 0x1bc80, 0x1bc88, 0x1bc90, 0x1bc99, 0x1bc9c, 0x1bca3, 0x1d000, 0x1d0f5, 0x1d100, 0x1d126, 0x1d129, 0x1d1e8,
612
- 0x1d200, 0x1d245, 0x1d300, 0x1d356, 0x1d360, 0x1d371, 0x1d400, 0x1d454, 0x1d456, 0x1d49c, 0x1d49e, 0x1d49f, 0x1d4a2, 0x1d4a2, 0x1d4a5, 0x1d4a6,
613
- 0x1d4a9, 0x1d4ac, 0x1d4ae, 0x1d4b9, 0x1d4bb, 0x1d4bb, 0x1d4bd, 0x1d4c3, 0x1d4c5, 0x1d505, 0x1d507, 0x1d50a, 0x1d50d, 0x1d514, 0x1d516, 0x1d51c,
614
- 0x1d51e, 0x1d539, 0x1d53b, 0x1d53e, 0x1d540, 0x1d544, 0x1d546, 0x1d546, 0x1d54a, 0x1d550, 0x1d552, 0x1d6a5, 0x1d6a8, 0x1d7cb, 0x1d7ce, 0x1da8b,
615
- 0x1da9b, 0x1da9f, 0x1daa1, 0x1daaf, 0x1e000, 0x1e006, 0x1e008, 0x1e018, 0x1e01b, 0x1e021, 0x1e023, 0x1e024, 0x1e026, 0x1e02a, 0x1e800, 0x1e8c4,
616
- 0x1e8c7, 0x1e8d6, 0x1e900, 0x1e94a, 0x1e950, 0x1e959, 0x1e95e, 0x1e95f, 0x1ee00, 0x1ee03, 0x1ee05, 0x1ee1f, 0x1ee21, 0x1ee22, 0x1ee24, 0x1ee24,
617
- 0x1ee27, 0x1ee27, 0x1ee29, 0x1ee32, 0x1ee34, 0x1ee37, 0x1ee39, 0x1ee39, 0x1ee3b, 0x1ee3b, 0x1ee42, 0x1ee42, 0x1ee47, 0x1ee47, 0x1ee49, 0x1ee49,
618
- 0x1ee4b, 0x1ee4b, 0x1ee4d, 0x1ee4f, 0x1ee51, 0x1ee52, 0x1ee54, 0x1ee54, 0x1ee57, 0x1ee57, 0x1ee59, 0x1ee59, 0x1ee5b, 0x1ee5b, 0x1ee5d, 0x1ee5d,
619
- 0x1ee5f, 0x1ee5f, 0x1ee61, 0x1ee62, 0x1ee64, 0x1ee64, 0x1ee67, 0x1ee6a, 0x1ee6c, 0x1ee72, 0x1ee74, 0x1ee77, 0x1ee79, 0x1ee7c, 0x1ee7e, 0x1ee7e,
620
- 0x1ee80, 0x1ee89, 0x1ee8b, 0x1ee9b, 0x1eea1, 0x1eea3, 0x1eea5, 0x1eea9, 0x1eeab, 0x1eebb, 0x1eef0, 0x1eef1, 0x1f000, 0x1f02b, 0x1f030, 0x1f093,
621
- 0x1f0a0, 0x1f0ae, 0x1f0b1, 0x1f0bf, 0x1f0c1, 0x1f0cf, 0x1f0d1, 0x1f0f5, 0x1f100, 0x1f10c, 0x1f110, 0x1f12e, 0x1f130, 0x1f16b, 0x1f170, 0x1f1ac,
622
- 0x1f1e6, 0x1f202, 0x1f210, 0x1f23b, 0x1f240, 0x1f248, 0x1f250, 0x1f251, 0x1f300, 0x1f6d2, 0x1f6e0, 0x1f6ec, 0x1f6f0, 0x1f6f6, 0x1f700, 0x1f773,
623
- 0x1f780, 0x1f7d4, 0x1f800, 0x1f80b, 0x1f810, 0x1f847, 0x1f850, 0x1f859, 0x1f860, 0x1f887, 0x1f890, 0x1f8ad, 0x1f910, 0x1f91e, 0x1f920, 0x1f927,
624
- 0x1f930, 0x1f930, 0x1f933, 0x1f93e, 0x1f940, 0x1f94b, 0x1f950, 0x1f95e, 0x1f980, 0x1f991, 0x1f9c0, 0x1f9c0, 0x20000, 0x2a6d6, 0x2a700, 0x2b734,
625
- 0x2b740, 0x2b81d, 0x2b820, 0x2cea1, 0x2f800, 0x2fa1d, 0xe0001, 0xe0001, 0xe0020, 0xe007f, 0xe0100, 0xe01ef, 0xf0000, 0xffffd, 0x100000, 0x10fffd,
626
- ];
627
- function unicodeLocale(context) {
628
- const locale = (context.env.LC_ALL || context.env.LC_CTYPE || context.env.LANG || "C").toLowerCase();
629
- return locale.endsWith("utf-8") || locale.endsWith("utf8");
630
- }
631
- function quote(bytes, shell, unicode = false) {
632
- const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
633
- const units = [];
634
- for (let offset = 0; offset < bytes.length;) {
635
- const first = bytes[offset];
636
- let length = 1;
637
- let text = String.fromCharCode(first);
638
- let valid = first < 128;
639
- if (unicode && first >= 0xc2 && first <= 0xf4) {
640
- const size = first < 0xe0 ? 2 : first < 0xf0 ? 3 : 4;
641
- try {
642
- text = decoder.decode(bytes.subarray(offset, offset + size));
643
- length = size;
644
- valid = true;
645
- }
646
- catch {
647
- valid = false;
648
- }
649
- }
650
- units.push({ text, bytes: bytes.subarray(offset, offset + length), valid });
651
- offset += length;
652
- }
653
- const escapes = { "\u0007": "\\a", "\b": "\\b", "\t": "\\t", "\n": "\\n", "\v": "\\v", "\f": "\\f", "\r": "\\r" };
654
- const printable = (unit) => {
655
- if (!unit.valid)
656
- return false;
657
- const point = unit.text.codePointAt(0);
658
- if (point < 128)
659
- return point >= 32 && point < 127;
660
- if (!unicode)
661
- return false;
662
- let low = 0;
663
- let high = glibc231PrintableRanges.length / 2;
664
- while (low < high) {
665
- const middle = Math.trunc((low + high) / 2);
666
- if (point < glibc231PrintableRanges[middle * 2])
667
- high = middle;
668
- else if (point > glibc231PrintableRanges[middle * 2 + 1])
669
- low = middle + 1;
670
- else
671
- return true;
672
- }
673
- return false;
674
- };
675
- if (shell && bytes.includes(39) && units.every((unit, index) => printable(unit) && !'!"$&()*;<=>?[^`|\\{}'.includes(unit.text) && (index === 0 || !"#~".includes(unit.text))))
676
- return `"${units.map(unit => unit.text).join("")}"`;
677
- const left = !shell && unicode ? "‘" : "'";
678
- const right = !shell && unicode ? "’" : "'";
679
- let result = left;
680
- let escaping = shell && bytes.includes(39) && units.length > 0 && !printable(units[units.length - 1]);
681
- for (const unit of units) {
682
- if (shell && unit.text === "'") {
683
- result += "'\\''";
684
- escaping = false;
685
- }
686
- else if (!printable(unit)) {
687
- if (shell && !escaping)
688
- result += "'$'";
689
- escaping = true;
690
- result += escapes[unit.text] ?? Array.from(unit.bytes, byte => `\\${byte.toString(8).padStart(3, "0")}`).join("");
691
- }
692
- else {
693
- if (shell && escaping)
694
- result += "''";
695
- escaping = false;
696
- if (!shell && (unit.text === right || unit.text === "\\"))
697
- result += "\\";
698
- result += unit.text;
699
- }
700
- }
701
- return result + right;
702
- }
703
- function fileError(error, name, bytes, opening, context) {
704
- if (!(error instanceof FsError))
705
- return error;
706
- const messages = { ENOENT: "No such file or directory", EACCES: "Permission denied", EISDIR: "Is a directory", ENOTDIR: "Not a directory", ELOOP: "Too many levels of symbolic links", EIO: "Input/output error", EBADF: "Bad file descriptor" };
707
- const message = messages[error.code];
708
- if (!message)
709
- return error;
710
- return new PublicDiagnostic(`${opening ? `cannot open ${quote(bytes, true, unicodeLocale(context))} for reading` : name === "-" ? "standard input" : name}: ${message}`);
711
- }
712
- export function fmtCommand() {
713
- return { name: "fmt", filesystemRequirements: inputRequirements, async execute(context) {
714
- context.signal.throwIfAborted();
715
- const controller = new AbortController();
716
- const local = { ...context, signal: AbortSignal.any([context.signal, controller.signal]) };
717
- let current;
718
- let closing;
719
- const close = () => {
720
- if (!closing) {
721
- controller.abort(new FsError("EPIPE", { message: "fmt input closed" }));
722
- closing = current?.close() ?? Promise.resolve();
723
- }
724
- return closing;
725
- };
726
- context.registerCleanup?.(close);
727
- let failed = false;
728
- const run = async () => {
729
- try {
730
- const settings = parse(context);
731
- if (settings.information) {
732
- await output(context, settings.information === "version" ? "fmt (virtual-bash)\n" : "Usage: fmt [-WIDTH] [OPTION]... [FILE]...\nReformat paragraphs; omitted FILE or '-' reads standard input.\n -c, --crown-margin preserve indentation of first two lines\n -t, --tagged-paragraph use distinct first and following margins\n -p, --prefix=STRING format only lines with this prefix\n -s, --split-only split lines without joining\n -u, --uniform-spacing one space between words, two after sentences\n -w, --width=WIDTH maximum width (default 75)\n -g, --goal=WIDTH preferred width (default 93% of width)\n --help display this help\n --version display virtual command identity\n");
733
- return { exitCode: 0 };
734
- }
735
- const budget = new ByteInputBudget(bufferLimit);
736
- const work = new WorkBudget();
737
- let stdinDone = false;
738
- let emptyChunks = 0;
739
- let chunks = 0;
740
- let exitCode = 0;
741
- for (const { name, bytes: nameBytes } of settings.files) {
742
- local.signal.throwIfAborted();
743
- if (name === "-" && stdinDone)
744
- continue;
745
- current = new InputScope(local, budget);
746
- try {
747
- await current.open(name, nameBytes);
748
- }
749
- catch (error) {
750
- local.signal.throwIfAborted();
751
- await diagnostic(context, fileError(error, name, nameBytes, true, context));
752
- await current.close();
753
- exitCode = 1;
754
- continue;
755
- }
756
- const machine = new Formatter(settings, work).run();
757
- let step = machine.next();
758
- let readFailed = false;
759
- let readError;
760
- let received = false;
761
- while (!step.done) {
762
- local.signal.throwIfAborted();
763
- if (step.value === "input") {
764
- let bytes = null;
765
- if (!readFailed) {
766
- try {
767
- bytes = await current.next();
768
- if (bytes?.length)
769
- received = true;
770
- }
771
- catch (error) {
772
- local.signal.throwIfAborted();
773
- if (isFsError(error, "EFBIG"))
774
- throw new PublicDiagnostic("byte command input limit exceeded");
775
- readFailed = true;
776
- readError = !received && name !== "-" && error instanceof FsError && ["ENOENT", "EACCES", "ENOTDIR", "ELOOP"].includes(error.code) ? fileError(error, name, nameBytes, true, context) : error;
777
- }
778
- }
779
- if (bytes?.length === 0 && ++emptyChunks > 4096)
780
- throw new PublicDiagnostic("empty input chunk limit exceeded");
781
- if (++chunks % 64 === 0)
782
- await yieldTurn(local.signal);
783
- step = machine.next(bytes);
784
- }
785
- else {
786
- if (step.value)
787
- await output(context, step.value);
788
- else
789
- await yieldTurn(local.signal);
790
- step = machine.next();
791
- }
792
- }
793
- try {
794
- await current.close();
795
- }
796
- catch (error) {
797
- if (!readFailed)
798
- throw error;
799
- }
800
- current = undefined;
801
- if (name === "-")
802
- stdinDone = true;
803
- if (readFailed && !isFsError(readError, "EISDIR") && !isFsError(readError, "EIO") && !isFsError(readError, "EBADF")) {
804
- await diagnostic(context, fileError(readError, name, nameBytes, false, context));
805
- exitCode = 1;
806
- }
807
- }
808
- return { exitCode };
809
- }
810
- catch (error) {
811
- failed = true;
812
- context.signal.throwIfAborted();
813
- if (error instanceof OptionDiagnostic)
814
- await writeBytes(context.stderr, error.bytes, context.signal);
815
- else
816
- await diagnostic(context, error);
817
- return { exitCode: 1 };
818
- }
819
- };
820
- let outcome;
821
- try {
822
- outcome = { result: await run() };
823
- }
824
- catch (error) {
825
- outcome = { error };
826
- }
827
- try {
828
- await close();
829
- }
830
- catch (error) {
831
- if (!failed)
832
- throw error;
833
- }
834
- if ("error" in outcome)
835
- throw outcome.error;
836
- return outcome.result;
837
- } };
838
- }
1
+ export { fmtCommand } from "../../safe-bash-command-fmt/index.js";
839
2
  //# sourceMappingURL=fmt.js.map