@poe-platform/safe-bash 0.1.756 → 0.1.757

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 (113) hide show
  1. package/dist/image-ast/index.browser.js +4 -4
  2. package/dist/image-ast/index.js +4 -4
  3. package/dist/safe-bash/chunks/{chunk-LESPJ3OG.js → chunk-WNWIBBJZ.js} +15 -2
  4. package/dist/safe-bash/command-chunks/{chunk-S4J6MSLY.js → chunk-444OTXM7.js} +3 -3
  5. package/dist/safe-bash/command-chunks/{chunk-6AIOGLZO.js → chunk-CE6Z2ULX.js} +2 -2
  6. package/dist/safe-bash/command-chunks/{chunk-5VSGEE6J.js → chunk-F4IQCXZG.js} +5 -5
  7. package/dist/safe-bash/command-chunks/{chunk-F5MORDSU.js → chunk-FDNCPZQS.js} +32 -1
  8. package/dist/safe-bash/command-chunks/{chunk-SG3ZDZCX.js → chunk-FLMLM7Y4.js} +2 -2
  9. package/dist/safe-bash/command-chunks/{chunk-CT3FB722.js → chunk-J7UMKBRK.js} +1 -1
  10. package/dist/safe-bash/command-chunks/{chunk-QQOAK7JQ.js → chunk-KKJCKPTA.js} +1 -1
  11. package/dist/safe-bash/command-chunks/{chunk-7KS6PX4U.js → chunk-KSIGF4AG.js} +3 -3
  12. package/dist/safe-bash/command-chunks/{chunk-4HQ7WYSI.js → chunk-MED2BOOW.js} +30 -5
  13. package/dist/safe-bash/command-chunks/{chunk-JRNLDPLI.js → chunk-OZSWW23M.js} +1 -1
  14. package/dist/safe-bash/command-chunks/{chunk-DKQ5REXF.js → chunk-QDPRSPHQ.js} +1 -1
  15. package/dist/safe-bash/command-chunks/{chunk-WOPU2VWF.js → chunk-RID3B5RE.js} +1 -1
  16. package/dist/safe-bash/command-chunks/{chunk-DVZCHNB5.js → chunk-S4MLIYSN.js} +7 -7
  17. package/dist/safe-bash/command-chunks/{chunk-REYTQLLB.js → chunk-TT2DMDBY.js} +1 -1
  18. package/dist/safe-bash/command-chunks/{chunk-UGX6MXDD.js → chunk-UIIT4ULR.js} +3 -3
  19. package/dist/safe-bash/command-chunks/{chunk-A6NFLWZI.js → chunk-WKPOGTZY.js} +22 -6
  20. package/dist/safe-bash/command-chunks/{chunk-3DOHQQJR.js → chunk-Y64PA6IA.js} +8 -8
  21. package/dist/safe-bash/command-chunks/{chunk-6Z7OS3TZ.js → chunk-ZP5KDHB4.js} +7 -7
  22. package/dist/safe-bash/commands/pandoc/index.d.ts +1 -1
  23. package/dist/safe-bash/commands/pandoc/index.js +5 -3
  24. package/dist/safe-bash/commands/python/index.browser.js +1 -1
  25. package/dist/safe-bash/commands/python/index.js +6 -0
  26. package/dist/safe-bash/commands/python/jspi.js +11 -2
  27. package/dist/safe-bash/core.browser.js +6 -4
  28. package/dist/safe-bash-command-csvkit/index.js +8 -8
  29. package/dist/safe-bash-command-soffice/index.js +16 -16
  30. package/dist/safe-bash-command-ssconvert/core.js +8 -8
  31. package/dist/safe-bash-command-ssconvert/formats/csv.js +9 -9
  32. package/dist/safe-bash-command-ssconvert/formats/ods.js +8 -8
  33. package/dist/safe-bash-command-ssconvert/formats/spreadsheetml.js +1 -1
  34. package/dist/safe-bash-command-ssconvert/formats/xls.js +9 -8
  35. package/dist/safe-bash-command-ssconvert/formats/xlsx.js +8 -8
  36. package/dist/safe-bash-command-ssconvert/index.js +107 -27
  37. package/dist/safe-bash-contracts/filesystem-descriptor.js +1 -1
  38. package/dist/safe-bash-contracts/filesystem-output.js +1 -1
  39. package/dist/safe-bash-mcp/index.js +4 -4
  40. package/dist/spreadsheet-engine/formulas/ast.d.ts +13 -0
  41. package/dist/spreadsheet-engine/formulas/conventions.js +1 -1
  42. package/dist/spreadsheet-engine/formulas/dependencies.js +2 -2
  43. package/dist/spreadsheet-engine/formulas/evaluator.js +2 -2
  44. package/dist/spreadsheet-engine/formulas/named-expressions.d.ts +2 -2
  45. package/dist/spreadsheet-engine/formulas/named-expressions.js +31 -5
  46. package/dist/spreadsheet-engine/formulas/parser.js +34 -0
  47. package/dist/spreadsheet-engine/formulas/rewriting.js +19 -1
  48. package/dist/spreadsheet-engine/formulas/serialization.js +6 -0
  49. package/dist/spreadsheet-format-xls/biff-write-formulas.js +2 -0
  50. package/dist/xlsx-ast/formula-semantics.js +2 -0
  51. package/package.json +2 -2
  52. package/dist/safe-bash/chunks/browser-ECQX6GMK.js +0 -6766
  53. package/dist/safe-bash/chunks/bz2-4OP3OWW3.js +0 -8
  54. package/dist/safe-bash/chunks/chunk-22XPAAOI.js +0 -173
  55. package/dist/safe-bash/chunks/chunk-44WUNUPV.js +0 -649
  56. package/dist/safe-bash/chunks/chunk-4E3SGG37.js +0 -876
  57. package/dist/safe-bash/chunks/chunk-4QTSSQKP.js +0 -898
  58. package/dist/safe-bash/chunks/chunk-5UVGPVXP.js +0 -410
  59. package/dist/safe-bash/chunks/chunk-64OE6Q6X.js +0 -3487
  60. package/dist/safe-bash/chunks/chunk-6IIFUPN2.js +0 -1526
  61. package/dist/safe-bash/chunks/chunk-BENDLTQD.js +0 -7065
  62. package/dist/safe-bash/chunks/chunk-BMO5LWEX.js +0 -534
  63. package/dist/safe-bash/chunks/chunk-BPMCPTNP.js +0 -1920
  64. package/dist/safe-bash/chunks/chunk-BRJTAWP5.js +0 -2955
  65. package/dist/safe-bash/chunks/chunk-C3GEIN7Q.js +0 -20054
  66. package/dist/safe-bash/chunks/chunk-CBVANNKK.js +0 -174814
  67. package/dist/safe-bash/chunks/chunk-CEWCRI6H.js +0 -2813
  68. package/dist/safe-bash/chunks/chunk-CGL53DQQ.js +0 -37
  69. package/dist/safe-bash/chunks/chunk-CTYJJD4O.js +0 -869
  70. package/dist/safe-bash/chunks/chunk-CWSS4MQA.js +0 -7
  71. package/dist/safe-bash/chunks/chunk-DLIGMTCP.js +0 -3266
  72. package/dist/safe-bash/chunks/chunk-EMAJ7WUW.js +0 -1031
  73. package/dist/safe-bash/chunks/chunk-FDKIZ2IJ.js +0 -1950
  74. package/dist/safe-bash/chunks/chunk-FZWJ2A7V.js +0 -3093
  75. package/dist/safe-bash/chunks/chunk-G2VCIF2J.js +0 -22
  76. package/dist/safe-bash/chunks/chunk-GVKBUQDX.js +0 -2649
  77. package/dist/safe-bash/chunks/chunk-IO2OGW3E.js +0 -28051
  78. package/dist/safe-bash/chunks/chunk-ISCZG7WA.js +0 -2648
  79. package/dist/safe-bash/chunks/chunk-JCN3FXLP.js +0 -38983
  80. package/dist/safe-bash/chunks/chunk-KJHDD4A2.js +0 -759
  81. package/dist/safe-bash/chunks/chunk-LB6HVRXS.js +0 -857
  82. package/dist/safe-bash/chunks/chunk-LLWAJOZH.js +0 -15
  83. package/dist/safe-bash/chunks/chunk-LMUKYZIJ.js +0 -708
  84. package/dist/safe-bash/chunks/chunk-M5SCNKIK.js +0 -2012
  85. package/dist/safe-bash/chunks/chunk-MJPFHANT.js +0 -3350
  86. package/dist/safe-bash/chunks/chunk-MZQW2DOL.js +0 -82
  87. package/dist/safe-bash/chunks/chunk-NB2V7EOY.js +0 -1348
  88. package/dist/safe-bash/chunks/chunk-NJVGOIYN.js +0 -17
  89. package/dist/safe-bash/chunks/chunk-OLNZHDIQ.js +0 -5141
  90. package/dist/safe-bash/chunks/chunk-OSUXMC43.js +0 -2317
  91. package/dist/safe-bash/chunks/chunk-OUUVROTM.js +0 -38
  92. package/dist/safe-bash/chunks/chunk-PA6GYKBQ.js +0 -151
  93. package/dist/safe-bash/chunks/chunk-PEFHZ226.js +0 -6957
  94. package/dist/safe-bash/chunks/chunk-QBB5P5KW.js +0 -4839
  95. package/dist/safe-bash/chunks/chunk-RSAWYAEH.js +0 -973
  96. package/dist/safe-bash/chunks/chunk-UWLYRWT4.js +0 -739
  97. package/dist/safe-bash/chunks/chunk-V463NBQH.js +0 -13
  98. package/dist/safe-bash/chunks/chunk-WPUALYUS.js +0 -843
  99. package/dist/safe-bash/chunks/chunk-XHKFSRGU.js +0 -5747
  100. package/dist/safe-bash/chunks/chunk-XHRCLZ3N.js +0 -13391
  101. package/dist/safe-bash/chunks/chunk-XPF5XHLY.js +0 -15
  102. package/dist/safe-bash/chunks/chunk-ZBFOWARG.js +0 -305
  103. package/dist/safe-bash/chunks/chunk-ZG7GWQ5P.js +0 -1859
  104. package/dist/safe-bash/chunks/model-store-EIY3ZQLZ.js +0 -10
  105. package/dist/safe-bash/chunks/pdf-renderer-CMF3CSVU.js +0 -22
  106. package/dist/safe-bash/chunks/pptx-ZL6L7KP2-O425BFIU.js +0 -30658
  107. package/dist/safe-bash/chunks/src-HHEPGR7U.js +0 -18969
  108. package/dist/safe-bash/chunks/src-MXIKQCIC.js +0 -187
  109. package/dist/safe-bash/chunks/usage-6TGVA2OM.js +0 -35
  110. package/dist/safe-bash/chunks/xz-EGQGQUEI.js +0 -8
  111. package/dist/safe-bash/chunks/xz-K2E2WLM6.js +0 -9
  112. package/dist/safe-bash/chunks/zstd-6JBLP5EF.js +0 -8
  113. package/dist/safe-bash/git_rust-C3QYOAZL.wasm +0 -0
@@ -1,1526 +0,0 @@
1
- import {
2
- init_platform
3
- } from "./chunk-ZG7GWQ5P.js";
4
-
5
- // packages/safe-bash/src/commands/line-endings/index.ts
6
- init_platform();
7
-
8
- // packages/safe-bash-command-dos2unix/src/index.ts
9
- init_platform();
10
- import { builtInDirectContextExecutors, FsError as FsError2, outputFailure } from "../../safe-bash-contracts/index.js";
11
- import { createOutputOperation } from "../../safe-bash-contracts/output.js";
12
-
13
- // packages/safe-bash-line-ending-engine/src/internal.ts
14
- init_platform();
15
- import { getCommandArguments } from "../../safe-bash-contracts/index.js";
16
- import { shellValueByteLength, shellValueBytes } from "../../safe-bash-contracts/value.js";
17
- import { hasYieldCheckpoint, monotonicNow, runYieldCheckpoint, yieldTurn } from "../../safe-bash-contracts/yield.js";
18
- import { PublicDiagnostic } from "../../safe-bash-contracts/diagnostics.js";
19
- function settings(options) {
20
- const limits = {
21
- maxArguments: Infinity,
22
- maxArgumentBytes: Infinity,
23
- maxInputBytes: Infinity,
24
- maxOutputBytes: Infinity,
25
- maxBufferedBytes: Infinity,
26
- maxDiagnosticBytes: Infinity,
27
- maxFiles: Infinity,
28
- maxWork: Infinity,
29
- maxEmptyChunks: Infinity,
30
- maxPathBytes: Infinity,
31
- maxDepth: Infinity,
32
- maxTempAttempts: Infinity,
33
- chunkSize: 16384,
34
- ...options.limits
35
- };
36
- for (const [name, value] of Object.entries(options.limits ?? {})) if (value !== Infinity && (!Number.isSafeInteger(value) || value < 1)) throw new RangeError(`Invalid line-ending limit: ${name}`);
37
- return Object.freeze(limits);
38
- }
39
- var LineEndingError = class extends PublicDiagnostic {
40
- constructor(message, status = 1) {
41
- super(message);
42
- this.status = status;
43
- }
44
- status;
45
- };
46
- var Budget = class {
47
- constructor(context, limits, signal, caller, admission) {
48
- this.context = context;
49
- this.limits = limits;
50
- this.signal = signal;
51
- this.caller = caller;
52
- this.admission = admission;
53
- this.signalAborted = signal.aborted;
54
- this.pollSignal = Object.prototype.hasOwnProperty.call(signal, "aborted");
55
- if (!this.signalAborted && !this.pollSignal) {
56
- signal.addEventListener("abort", () => {
57
- this.signalAborted = true;
58
- }, { once: true });
59
- }
60
- }
61
- context;
62
- limits;
63
- signal;
64
- caller;
65
- admission;
66
- work = 0;
67
- quantum = 0;
68
- checkpointCount = 0;
69
- lastYield = monotonicNow();
70
- memory = 0;
71
- input = 0;
72
- output = 0;
73
- diagnostics = 0;
74
- files = 0;
75
- signalAborted;
76
- pollSignal;
77
- assertOpen() {
78
- if (this.pollSignal ? this.signal.aborted : this.signalAborted) this.signal.throwIfAborted();
79
- if (this.admission.closed) throw new LineEndingError("command is closed");
80
- }
81
- check(value, maximum, label) {
82
- if (value > maximum || value < 0 || (value | 0) !== value && !Number.isSafeInteger(value)) throw new LineEndingError(`${label} limit exceeded`);
83
- }
84
- retain(amount) {
85
- this.check(this.memory + amount, this.limits.maxBufferedBytes, "buffered bytes");
86
- this.memory += amount;
87
- }
88
- incoming(amount) {
89
- this.check(this.input + amount, this.limits.maxInputBytes, "input bytes");
90
- this.input += amount;
91
- }
92
- emitted(amount, diagnostic = false) {
93
- if (diagnostic) {
94
- this.check(this.diagnostics + amount, this.limits.maxDiagnosticBytes, "diagnostic bytes");
95
- this.diagnostics += amount;
96
- } else {
97
- this.check(this.output + amount, this.limits.maxOutputBytes, "output bytes");
98
- this.output += amount;
99
- }
100
- }
101
- file() {
102
- this.check(++this.files, this.limits.maxFiles, "file count");
103
- }
104
- step(amount = 1) {
105
- this.assertOpen();
106
- this.check(this.work + amount, this.limits.maxWork, "work");
107
- this.work += amount;
108
- this.quantum += amount;
109
- if (this.quantum < 1024) return;
110
- this.quantum = 0;
111
- if (++this.checkpointCount > 1 && (this.checkpointCount & 255) !== 0 && !hasYieldCheckpoint(this.caller) && monotonicNow() - this.lastYield < 25) {
112
- runYieldCheckpoint(this.caller);
113
- this.assertOpen();
114
- return;
115
- }
116
- return yieldTurn(this.caller).then(() => {
117
- this.lastYield = monotonicNow();
118
- this.assertOpen();
119
- });
120
- }
121
- async arguments() {
122
- const args = this.context.args;
123
- this.assertOpen();
124
- const count = args.length;
125
- this.assertOpen();
126
- this.check(count, this.limits.maxArguments, "argument count");
127
- const argumentValues = this.context.argumentValues;
128
- this.assertOpen();
129
- const carrier = argumentValues === void 0 ? void 0 : getCommandArguments({ args, argumentValues });
130
- const snapshot = [];
131
- this.retain(count * 64);
132
- let total = 0;
133
- for (let index = 0; index < count; index++) {
134
- const argument = args[index];
135
- this.assertOpen();
136
- if (typeof argument !== "string") throw new LineEndingError("arguments must be strings");
137
- this.check(argument.length, this.limits.maxPathBytes, "argument bytes");
138
- total += argument.length;
139
- this.check(total, this.limits.maxArgumentBytes, "argument bytes");
140
- this.retain(argument.length * 12);
141
- snapshot.push(argument);
142
- }
143
- for (const argument of snapshot) {
144
- for (let index = 0; index < argument.length; index++) {
145
- {
146
- const s = this.step();
147
- if (s) await s;
148
- }
149
- const unit = argument.charCodeAt(index);
150
- if (unit >= 55296 && unit <= 56319) {
151
- const next = argument.charCodeAt(++index);
152
- if (!(next >= 56320 && next <= 57343)) throw new LineEndingError("arguments must be valid UTF-8 paths");
153
- } else if (unit >= 56320 && unit <= 57343) throw new LineEndingError("arguments must be valid UTF-8 paths");
154
- }
155
- }
156
- const values = (carrier ?? getCommandArguments({ args: snapshot })).values;
157
- total = 0;
158
- for (const value of values) {
159
- const length = shellValueByteLength(value);
160
- this.check(length, this.limits.maxPathBytes, "argument bytes");
161
- total += length;
162
- this.check(total, this.limits.maxArgumentBytes, "argument bytes");
163
- }
164
- const result = [];
165
- for (const value of values) {
166
- {
167
- const s = this.step(shellValueByteLength(value));
168
- if (s) await s;
169
- }
170
- let text;
171
- try {
172
- text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(shellValueBytes(value));
173
- } catch {
174
- throw new LineEndingError("arguments must be valid UTF-8 paths");
175
- }
176
- if (text.includes("\0")) throw new LineEndingError("NUL is not supported in paths");
177
- result.push(text);
178
- }
179
- return result;
180
- }
181
- };
182
- function sameIdentity(first, second) {
183
- return (typeof first.identityScope === "object" && first.identityScope !== null || typeof first.identityScope === "symbol") && first.identityScope === second.identityScope && Number.isSafeInteger(first.dev) && Number.isSafeInteger(first.ino) && first.dev >= 0 && first.ino >= 0 && first.dev === second.dev && first.ino === second.ino;
184
- }
185
- function missing(error) {
186
- return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
187
- }
188
-
189
- // packages/safe-bash-line-ending-engine/src/io.ts
190
- init_platform();
191
- var typedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
192
- var viewBuffer = Object.getOwnPropertyDescriptor(typedArrayPrototype, "buffer").get;
193
- var viewOffset = Object.getOwnPropertyDescriptor(typedArrayPrototype, "byteOffset").get;
194
- var viewLength = Object.getOwnPropertyDescriptor(typedArrayPrototype, "byteLength").get;
195
- var Lifecycle = class {
196
- constructor(budget, output, captured, caller, admission) {
197
- this.budget = budget;
198
- this.captured = captured;
199
- this.caller = caller;
200
- this.admission = admission;
201
- output.registerCleanup(() => this.close());
202
- }
203
- budget;
204
- captured;
205
- caller;
206
- admission;
207
- pending = /* @__PURE__ */ new Set();
208
- cleanups = [];
209
- closing;
210
- diagnosticDestination;
211
- diagnosticCancellation;
212
- assertOpen() {
213
- this.budget.assertOpen();
214
- if (this.closing) throw new LineEndingError("command is closed");
215
- }
216
- assertDiagnosticOpen() {
217
- this.caller.throwIfAborted();
218
- const consumer = this.diagnosticDestination?.consumer;
219
- if (consumer?.aborted) {
220
- this.diagnosticCancellation = { reason: consumer.reason };
221
- consumer.throwIfAborted();
222
- }
223
- }
224
- cleanup(action) {
225
- this.assertOpen();
226
- this.cleanups.push(action);
227
- }
228
- async operation(action) {
229
- this.assertOpen();
230
- const pending = Promise.resolve().then(async () => {
231
- {
232
- const s = this.budget.step();
233
- if (s) await s;
234
- }
235
- this.assertOpen();
236
- return action();
237
- });
238
- this.pending.add(pending);
239
- try {
240
- const value = await pending;
241
- this.assertOpen();
242
- return value;
243
- } finally {
244
- this.pending.delete(pending);
245
- }
246
- }
247
- close() {
248
- return this.closing ??= Promise.resolve().then(async () => {
249
- while (this.pending.size) await Promise.allSettled([...this.pending]);
250
- const failures = [];
251
- for (const close of this.cleanups) {
252
- try {
253
- await close();
254
- } catch (error) {
255
- failures.push(error);
256
- }
257
- }
258
- if (failures.length === 1) throw failures[0];
259
- if (failures.length > 1) throw new AggregateError(failures, "line-ending cleanup failed");
260
- });
261
- }
262
- async stdout(bytes) {
263
- await this.operation(async () => {
264
- const write = this.captured.write;
265
- this.assertOpen();
266
- await Reflect.apply(write, this.captured, [bytes]);
267
- });
268
- }
269
- async diagnostic(text) {
270
- this.assertDiagnosticOpen();
271
- if (this.closing || this.admission.closed) throw new LineEndingError("command is closed");
272
- this.budget.check(text.length * 3, this.budget.limits.maxDiagnosticBytes, "diagnostic buffer bytes");
273
- const bytes = new TextEncoder().encode(text);
274
- this.budget.emitted(bytes.length, true);
275
- const pending = Promise.resolve().then(async () => {
276
- this.assertDiagnosticOpen();
277
- if (!this.diagnosticDestination) {
278
- const sink = this.budget.context.stderr;
279
- this.assertDiagnosticOpen();
280
- const capability = sink.ownedOutput;
281
- this.assertDiagnosticOpen();
282
- const consumer = capability?.consumerClosed;
283
- this.diagnosticDestination = { destination: capability ?? sink, consumer };
284
- }
285
- this.assertDiagnosticOpen();
286
- const destination = this.diagnosticDestination.destination;
287
- const write = destination.write;
288
- this.assertDiagnosticOpen();
289
- await Reflect.apply(write, destination, [bytes]);
290
- });
291
- this.pending.add(pending);
292
- try {
293
- await pending;
294
- this.assertDiagnosticOpen();
295
- } finally {
296
- this.pending.delete(pending);
297
- }
298
- }
299
- };
300
- var Reader = class {
301
- constructor(life) {
302
- this.life = life;
303
- life.cleanup(() => this.close());
304
- }
305
- life;
306
- iterator;
307
- chunk = new Uint8Array();
308
- offset = 0;
309
- ended = false;
310
- empties = 0;
311
- snapshotBytes = 0;
312
- closing;
313
- close() {
314
- return this.closing ??= (async () => {
315
- try {
316
- if (!this.ended && this.iterator) {
317
- const iterator = this.iterator;
318
- const close = iterator.return;
319
- if (close) await Reflect.apply(close, iterator, []);
320
- }
321
- } finally {
322
- this.iterator = void 0;
323
- this.life.budget.retain(-this.chunk.length - this.snapshotBytes);
324
- this.chunk = new Uint8Array();
325
- this.snapshotBytes = 0;
326
- }
327
- })();
328
- }
329
- async open(path, stat) {
330
- const { context, signal, limits } = this.life.budget;
331
- await this.life.operation(async () => {
332
- let source;
333
- let acquisitionAborted = false;
334
- if (path === void 0) source = context.stdin;
335
- else {
336
- const fs = context.fs;
337
- const capabilitiesFor = fs.capabilitiesFor;
338
- this.life.assertOpen();
339
- const selected = capabilitiesFor ? await Reflect.apply(capabilitiesFor, fs, [path, { signal }]) : void 0;
340
- this.life.assertOpen();
341
- const capabilities = selected ?? fs.capabilities;
342
- this.life.assertOpen();
343
- const readStream = fs.readStream;
344
- this.life.assertOpen();
345
- const streaming = capabilities.streamingRead !== false;
346
- this.life.assertOpen();
347
- if (readStream && streaming) {
348
- source = Reflect.apply(readStream, fs, [path, { signal, chunkSize: limits.chunkSize === Infinity ? 16384 : limits.chunkSize }]);
349
- acquisitionAborted = signal.aborted || this.life.admission.closed;
350
- } else {
351
- const maximum = Math.min(limits.maxInputBytes, Math.floor(limits.maxBufferedBytes / 3));
352
- const size = stat.size;
353
- this.life.assertOpen();
354
- this.life.budget.check(size, maximum, "buffered input bytes");
355
- this.life.budget.retain(Number.isFinite(maximum) ? maximum : size);
356
- this.snapshotBytes = Number.isFinite(maximum) ? maximum : size;
357
- const readFile = fs.readFile;
358
- this.life.assertOpen();
359
- const bytes = await Reflect.apply(readFile, fs, [path, { signal, ...Number.isFinite(maximum) ? { maxBytes: maximum } : {} }]);
360
- this.life.assertOpen();
361
- if (!(bytes instanceof Uint8Array)) throw new TypeError("line-ending input requires bytes");
362
- this.life.budget.check(viewLength.call(bytes), maximum, "buffered input bytes");
363
- this.life.budget.retain(viewLength.call(bytes) - this.snapshotBytes);
364
- this.snapshotBytes = viewLength.call(bytes);
365
- source = { [Symbol.asyncIterator]: (async function* () {
366
- try {
367
- yield bytes;
368
- } finally {
369
- this.life.budget.retain(-this.snapshotBytes);
370
- this.snapshotBytes = 0;
371
- }
372
- }).bind(this) };
373
- }
374
- }
375
- if (!acquisitionAborted) this.life.assertOpen();
376
- const factory = source[Symbol.asyncIterator];
377
- if (!acquisitionAborted) this.life.assertOpen();
378
- this.iterator = Reflect.apply(factory, source, []);
379
- });
380
- }
381
- get() {
382
- const step = this.life.budget.step();
383
- if (!step && this.offset < this.chunk.length) {
384
- this.life.assertOpen();
385
- return this.chunk[this.offset++];
386
- }
387
- return this.getSlow(step);
388
- }
389
- beforeRead;
390
- async getSlow(step) {
391
- if (step) await step;
392
- this.life.assertOpen();
393
- while (this.offset === this.chunk.length) {
394
- if (this.ended) return -1;
395
- await this.beforeRead?.();
396
- const next = await this.life.operation(() => {
397
- const iterator = this.iterator;
398
- const advance = iterator.next;
399
- this.life.assertOpen();
400
- return Reflect.apply(advance, iterator, []);
401
- });
402
- this.life.budget.retain(-this.chunk.length);
403
- this.chunk = new Uint8Array();
404
- this.offset = 0;
405
- const done = next.done;
406
- this.life.assertOpen();
407
- if (done) {
408
- this.ended = true;
409
- this.iterator = void 0;
410
- return -1;
411
- }
412
- const value = next.value;
413
- this.life.assertOpen();
414
- if (!(value instanceof Uint8Array)) throw new TypeError("line-ending input requires bytes");
415
- const buffer = viewBuffer.call(value);
416
- const offset = viewOffset.call(value);
417
- const length = viewLength.call(value);
418
- this.life.budget.incoming(length);
419
- this.life.budget.retain(length);
420
- try {
421
- if (viewOffset.call(value) !== offset || viewLength.call(value) < length) throw new TypeError("line-ending input extent changed during admission");
422
- this.chunk = new Uint8Array(new Uint8Array(buffer, offset, length));
423
- } catch (error) {
424
- this.life.budget.retain(-length);
425
- throw error;
426
- }
427
- if (!this.chunk.length) this.life.budget.check(++this.empties, this.life.budget.limits.maxEmptyChunks, "empty input chunks");
428
- }
429
- return this.chunk[this.offset++];
430
- }
431
- };
432
- var Writer = class {
433
- constructor(life, publish) {
434
- this.life = life;
435
- this.publish = publish;
436
- const { chunkSize, maxBufferedBytes } = life.budget.limits;
437
- const size = Math.min(chunkSize === Infinity ? 16384 : chunkSize, maxBufferedBytes);
438
- life.budget.retain(size);
439
- this.buffer = new Uint8Array(size);
440
- life.cleanup(async () => {
441
- this.release();
442
- });
443
- }
444
- life;
445
- publish;
446
- buffer;
447
- used = 0;
448
- release() {
449
- this.life.budget.retain(-this.buffer.length);
450
- this.buffer = new Uint8Array();
451
- this.used = 0;
452
- }
453
- put(byte) {
454
- const step = this.life.budget.step();
455
- if (step) return step.then(() => this.putAfterStep(byte));
456
- return this.putAfterStep(byte);
457
- }
458
- putAfterStep(byte) {
459
- this.life.assertOpen();
460
- this.life.budget.emitted(1);
461
- this.buffer[this.used++] = byte;
462
- if (this.used === this.buffer.length) return this.flush();
463
- }
464
- async flush() {
465
- this.life.assertOpen();
466
- if (this.used) {
467
- await this.publish(this.buffer.slice(0, this.used));
468
- this.used = 0;
469
- }
470
- }
471
- };
472
-
473
- // packages/safe-bash-line-ending-engine/src/stage.ts
474
- init_platform();
475
- import { FsError } from "../../safe-bash-contracts/index.js";
476
- import { writeFileOutput } from "../../safe-bash-contracts/filesystem-output-budget.js";
477
- import { dirname, resolvePath, retainFileSystemCleanup } from "@poe-platform/safe-fs/core";
478
- var Files = class {
479
- constructor(life) {
480
- this.life = life;
481
- }
482
- life;
483
- async call(select, args) {
484
- return await this.life.operation(async () => {
485
- const fs = this.life.budget.context.fs;
486
- this.life.assertOpen();
487
- const method = select(fs);
488
- this.life.assertOpen();
489
- return await Reflect.apply(method, fs, args);
490
- });
491
- }
492
- async stat(path) {
493
- try {
494
- return await this.call((fs) => fs.lstat, [path, { signal: this.life.budget.signal }]);
495
- } catch (error) {
496
- this.life.assertOpen();
497
- if (missing(error)) return void 0;
498
- throw error;
499
- }
500
- }
501
- path(name) {
502
- const path = resolvePath(this.life.budget.context.cwd, name);
503
- this.life.budget.check(path.length * 3, this.life.budget.limits.maxPathBytes, "path bytes");
504
- this.life.budget.check(path.split("/").length - 1, this.life.budget.limits.maxDepth, "path depth");
505
- return path;
506
- }
507
- async parents(path) {
508
- const parents = [];
509
- let current = "/";
510
- for (const component of ["", ...dirname(path).split("/").filter(Boolean)]) {
511
- if (component) current = resolvePath(current, component);
512
- const stat = await this.stat(current);
513
- if (!stat) throw new FsError("ENOENT", { path: current });
514
- if (stat.type !== "directory" || !sameIdentity(stat, stat)) throw new LineEndingError("output requires stable non-symlink directory identities");
515
- parents.push({ path: current, stat });
516
- }
517
- return parents;
518
- }
519
- };
520
- var Stage = class {
521
- constructor(files, path, expected) {
522
- this.files = files;
523
- this.path = path;
524
- this.expected = expected;
525
- const { life } = files;
526
- life.assertOpen();
527
- this.cleanup = retainFileSystemCleanup(life.budget.context.fs, async (view) => {
528
- if (!this.created || !this.identity) return;
529
- try {
530
- for (const parent of this.parents) {
531
- const stat2 = await view.lstat(parent.path);
532
- if (stat2.type !== "directory" || !sameIdentity(stat2, parent.stat)) return;
533
- }
534
- const stat = await view.lstat(this.temporary);
535
- if (stat.type === "file" && sameIdentity(stat, this.identity)) await view.rm(this.temporary);
536
- } catch (error) {
537
- if (!missing(error)) throw error;
538
- }
539
- }, { maxOperations: Math.min(4096, life.budget.limits.maxDepth + 4) });
540
- life.cleanup(this.cleanup);
541
- }
542
- files;
543
- path;
544
- expected;
545
- temporary = "";
546
- identity;
547
- created = false;
548
- parents = [];
549
- cleanup;
550
- async open() {
551
- const { life } = this.files;
552
- const { signal, limits } = life.budget;
553
- const capabilities = await life.operation(async () => {
554
- const fs = life.budget.context.fs;
555
- const query = fs.capabilitiesFor;
556
- life.assertOpen();
557
- const result = query ? await Reflect.apply(query, fs, [this.path, { signal, create: true }]) : fs.capabilities;
558
- life.assertOpen();
559
- return result;
560
- });
561
- if (capabilities.atomicRename !== true || capabilities.exclusiveCreate !== true || capabilities.permissions !== true || capabilities.append === false || capabilities.remove === false) {
562
- throw new LineEndingError("file conversion requires atomic rename, exclusive create, permissions, append and cleanup capabilities");
563
- }
564
- if (this.expected === void 0 && capabilities.atomicRenameNoReplace !== true) {
565
- throw new LineEndingError("new destination requires atomic no-replace rename capability");
566
- }
567
- this.parents = await this.files.parents(this.path);
568
- for (let attempt = 0; attempt < limits.maxTempAttempts; attempt++) {
569
- this.temporary = resolvePath(dirname(this.path), `.line-ending-${attempt + 1}`);
570
- this.files.path(this.temporary);
571
- if (this.temporary === this.path || await this.files.stat(this.temporary)) continue;
572
- try {
573
- await this.files.call((fs) => fs.writeFile, [this.temporary, new Uint8Array(), { signal, flag: "wx", mode: 384 }]);
574
- } catch (error) {
575
- life.assertOpen();
576
- if (typeof error === "object" && error !== null && "code" in error && error.code === "EEXIST") continue;
577
- throw error;
578
- }
579
- this.created = true;
580
- this.identity = await this.files.stat(this.temporary);
581
- if (!this.identity || this.identity.type !== "file" || !sameIdentity(this.identity, this.identity)) throw new LineEndingError("temporary file identity unavailable; unowned path is not removed");
582
- return;
583
- }
584
- throw new LineEndingError("temporary file attempts limit exceeded");
585
- }
586
- async checked() {
587
- for (const parent of this.parents) {
588
- const stat2 = await this.files.stat(parent.path);
589
- if (!stat2 || stat2.type !== "directory" || !sameIdentity(stat2, parent.stat)) throw new LineEndingError("output parent changed before mutation");
590
- }
591
- const stat = await this.files.stat(this.temporary);
592
- if (!this.identity || !stat || stat.type !== "file" || !sameIdentity(stat, this.identity)) throw new LineEndingError("temporary file changed before mutation");
593
- }
594
- async append(bytes) {
595
- await this.checked();
596
- const { context, signal } = this.files.life.budget;
597
- await this.files.life.operation(() => writeFileOutput(
598
- { signal, ...context.registerCleanup ? { registerCleanup: context.registerCleanup } : {} },
599
- bytes,
600
- async (chunk) => {
601
- await this.files.call((fs) => fs.appendFile, [this.temporary, chunk, { signal }]);
602
- }
603
- ));
604
- }
605
- async publish(input, options) {
606
- const { signal } = this.files.life.budget;
607
- await this.checked();
608
- if (!options.newFile && !options.allowChown && (input.uid !== void 0 && input.uid !== this.identity.uid || input.gid !== void 0 && input.gid !== this.identity.gid)) {
609
- throw new LineEndingError("filesystem cannot preserve input user/group ownership");
610
- }
611
- await this.files.call((fs) => fs.chmod, [this.temporary, input.mode & 511 & (options.newFile ? ~18 : 511), { signal }]);
612
- if (options.keepDate) {
613
- await this.checked();
614
- await this.files.call((fs) => fs.utimes, [this.temporary, Math.floor(input.atimeMs / 1e3) * 1e3, Math.floor(input.mtimeMs / 1e3) * 1e3, { signal }]);
615
- }
616
- await this.checked();
617
- const current = await this.files.stat(this.path);
618
- if (current ? !this.expected || current.type !== "file" || !sameIdentity(current, this.expected) : this.expected !== void 0) throw new LineEndingError("destination changed before publication");
619
- await this.files.call((fs) => fs.rename, [this.temporary, this.path, { signal, noReplace: this.expected === void 0 }]);
620
- this.created = false;
621
- }
622
- };
623
-
624
- // packages/safe-bash-line-ending-engine/src/convert.ts
625
- init_platform();
626
-
627
- // packages/safe-bash-line-ending-engine/src/encoding.ts
628
- init_platform();
629
- var encodingLabels = { bytes: "no_bom", le: "UTF-16LE", be: "UTF-16BE", utf8: "UTF-8", gb: "GB18030" };
630
- async function readEncoding(reader) {
631
- const prefix = [];
632
- let bom = "bytes", error = false;
633
- const first = await reader.get();
634
- if (first !== -1) {
635
- if (first !== 255 && first !== 254 && first !== 239 && first !== 132) prefix.push(first);
636
- else {
637
- const second = await reader.get();
638
- if (second === -1) error = true;
639
- else if (first === 255 && second === 254) bom = "le";
640
- else if (first === 254 && second === 255) bom = "be";
641
- else {
642
- const third = await reader.get();
643
- if (third === -1) error = true;
644
- else if (first === 239 && second === 187 && third === 191) bom = "utf8";
645
- else {
646
- prefix.push(first, second, third);
647
- if (first === 132 && second === 49 && third === 149) {
648
- const fourth = await reader.get();
649
- if (fourth === -1) error = true;
650
- else if (fourth === 51) {
651
- bom = "gb";
652
- prefix.length = 0;
653
- } else prefix.push(fourth);
654
- }
655
- }
656
- }
657
- }
658
- }
659
- let offset = 0;
660
- return { bom, error, byte: () => offset < prefix.length ? prefix[offset++] : reader.get() };
661
- }
662
-
663
- // packages/safe-bash-line-ending-engine/src/convert.ts
664
- async function convert(direction, reader, writer, life, options, name, stdio) {
665
- let high = 1;
666
- if (stdio) reader.beforeRead = () => writer.flush();
667
- const diagnostic = async (message) => {
668
- if (stdio) await writer.flush();
669
- await life.diagnostic(`${direction}: ${message}
670
- `);
671
- };
672
- const input = await readEncoding(reader);
673
- if (input.error) {
674
- if (!options.quiet) await diagnostic("can not read from input file: Success");
675
- return { kind: "bom-error", encoding: "" };
676
- }
677
- const bom = input.bom === "bytes" ? options.assume : input.bom;
678
- if (options.verbose) {
679
- if (options.assume !== "bytes") await diagnostic(`Assuming UTF-16${options.assume === "le" ? "LE" : "BE"} encoding.`);
680
- if (input.bom !== "bytes") await diagnostic(`Input file ${name} has ${encodingLabels[input.bom]} BOM.`);
681
- }
682
- const wide = bom === "le" || bom === "be";
683
- const encoding = wide ? bom === "le" ? "UTF-16LE" : "UTF-16BE" : "";
684
- const env = life.budget.context.env;
685
- const locale = env.LC_ALL || env.LC_CTYPE || env.LANG || "C";
686
- const utf8 = locale === "C.UTF-8" || locale === "C.utf8";
687
- if (wide && !options.keepUtf16 && !utf8 && locale !== "C" && locale !== "POSIX") throw new LineEndingError("UTF-16 conversion supports only C/POSIX and C.UTF-8 locales");
688
- if (options.addBom || options.keepBom && bom !== "bytes") {
689
- const bytes = options.keepUtf16 && wide ? bom === "le" ? [255, 254] : [254, 255] : bom === "gb" ? [132, 49, 149, 51] : [239, 187, 191];
690
- for (const byte2 of bytes) await writer.put(byte2);
691
- if (options.verbose) await diagnostic(`Writing ${options.keepUtf16 && wide ? encodingLabels[bom] : bom === "gb" ? "GB18030" : "UTF-8"} BOM.`);
692
- }
693
- const byte = input.byte;
694
- let pushed = -1;
695
- const unit = () => {
696
- if (pushed !== -1) {
697
- const value = pushed;
698
- pushed = -1;
699
- return value;
700
- }
701
- const lead = byte();
702
- if (typeof lead === "number") {
703
- if (lead === -1 || !wide) return lead;
704
- const trail = byte();
705
- if (typeof trail === "number") return trail === -1 ? -1 : bom === "le" ? lead + trail * 256 : lead * 256 + trail;
706
- return trail.then((t) => t === -1 ? -1 : bom === "le" ? lead + t * 256 : lead * 256 + t);
707
- }
708
- return lead.then(async (l) => {
709
- if (l === -1 || !wide) return l;
710
- const trail = await byte();
711
- return trail === -1 ? -1 : bom === "le" ? l + trail * 256 : l * 256 + trail;
712
- });
713
- };
714
- let line = 1;
715
- const result = { kind: "ok" };
716
- const put = (value) => {
717
- if (!wide) {
718
- const p = writer.put(options.sevenBit && bom === "bytes" && value >= 128 ? 32 : value);
719
- return p ? p.then(() => true) : true;
720
- }
721
- return putWide(value);
722
- };
723
- const putWide = async (value) => {
724
- if (options.keepUtf16) {
725
- await writer.put(bom === "le" ? value & 255 : value >> 8);
726
- await writer.put(bom === "le" ? value >> 8 : value & 255);
727
- return true;
728
- }
729
- if (high >= 55296 && high < 56320 && (value < 56320 || value >= 57344)) {
730
- await diagnostic("error: Invalid surrogate pair. Missing low surrogate.");
731
- result.kind = "unicode";
732
- return false;
733
- }
734
- if (value >= 55296 && value < 56320) {
735
- high = value;
736
- return true;
737
- }
738
- if (value >= 56320 && value < 57344) {
739
- if (!(high >= 55296 && high < 56320)) {
740
- await diagnostic("error: Invalid surrogate pair. Missing high surrogate.");
741
- result.kind = "unicode";
742
- return false;
743
- }
744
- value = 65536 + (high & 1023) * 1024 + (value & 1023);
745
- high = 1;
746
- }
747
- if (!utf8 && value > 127) {
748
- if (!options.quiet) await diagnostic("Invalid or incomplete multibyte or wide character");
749
- result.kind = "unicode";
750
- return false;
751
- }
752
- if (value < 128) await writer.put(value);
753
- else if (value < 2048) {
754
- await writer.put(192 | value >> 6);
755
- await writer.put(128 | value & 63);
756
- } else if (value < 65536) {
757
- await writer.put(224 | value >> 12);
758
- await writer.put(128 | value >> 6 & 63);
759
- await writer.put(128 | value & 63);
760
- } else {
761
- await writer.put(240 | value >> 18);
762
- await writer.put(128 | value >> 12 & 63);
763
- await writer.put(128 | value >> 6 & 63);
764
- await writer.put(128 | value & 63);
765
- }
766
- return true;
767
- };
768
- let previous = 0;
769
- let last = -1, converted = 0;
770
- for (; ; ) {
771
- const u = unit();
772
- const current = typeof u === "number" ? u : await u;
773
- if (current === -1) break;
774
- last = current;
775
- if (!options.force && current < 32 && current !== 9 && current !== 10 && current !== 12 && current !== 13) {
776
- result.kind = "binary";
777
- if (!options.quiet) await diagnostic(`Binary symbol 0x${current.toString(16).toUpperCase().padStart(wide ? 4 : 2, "0")} found at line ${line}`);
778
- break;
779
- }
780
- if (direction === "dos2unix") {
781
- if (current === 13) {
782
- const u2 = unit();
783
- pushed = typeof u2 === "number" ? u2 : await u2;
784
- if (pushed !== 10) {
785
- const ok = put(13);
786
- if (!(typeof ok === "boolean" ? ok : await ok)) break;
787
- } else {
788
- converted++;
789
- last = 10;
790
- if (options.newline) {
791
- const ok = put(10);
792
- if (!(typeof ok === "boolean" ? ok : await ok)) break;
793
- }
794
- }
795
- } else {
796
- const ok = put(current);
797
- if (!(typeof ok === "boolean" ? ok : await ok)) break;
798
- if (current === 10) line++;
799
- }
800
- } else {
801
- if (current === 10 && previous !== 13) {
802
- converted++;
803
- const ok2 = put(13);
804
- if (!(typeof ok2 === "boolean" ? ok2 : await ok2)) break;
805
- }
806
- const ok = put(current);
807
- if (!(typeof ok === "boolean" ? ok : await ok)) break;
808
- if (current === 10) line++;
809
- if (options.newline && current === 10) {
810
- const ok1 = put(13);
811
- if (!(typeof ok1 === "boolean" ? ok1 : await ok1)) break;
812
- const ok2 = put(10);
813
- if (!(typeof ok2 === "boolean" ? ok2 : await ok2)) break;
814
- }
815
- previous = current;
816
- }
817
- }
818
- if (result.kind === "ok" && high >= 55296 && high < 56320) {
819
- await diagnostic("error: Invalid surrogate pair. Missing low surrogate.");
820
- result.kind = "unicode";
821
- }
822
- if (result.kind === "ok" && options.addEol && last !== -1 && last !== 10) {
823
- if (options.verbose) await diagnostic("Added line break to last line.");
824
- if (direction === "unix2dos") await put(13);
825
- await put(10);
826
- }
827
- if (result.kind === "ok" && options.verbose) await diagnostic(`Converted ${converted} out of ${line - 1} line breaks.`);
828
- if (stdio || result.kind === "ok") await writer.flush();
829
- if (!options.quiet) {
830
- if (result.kind === "binary") await diagnostic(`Skipping binary file ${name}`);
831
- if (result.kind === "unicode") await diagnostic(`Skipping UTF-16 file ${name}, an UTF-16 conversion error occurred on line ${line}.`);
832
- }
833
- return { kind: result.kind, encoding };
834
- }
835
-
836
- // packages/safe-bash-line-ending-engine/src/info.ts
837
- init_platform();
838
- var FileInformation = class {
839
- constructor(life) {
840
- this.life = life;
841
- }
842
- life;
843
- flags = /* @__PURE__ */ new Set();
844
- headerDone = false;
845
- configure(value) {
846
- for (const flag of value) {
847
- if (!"0dumbtechp".includes(flag)) throw new LineEndingError(`wrong flag '${flag}' for option -i or --info`);
848
- this.flags.add(flag);
849
- }
850
- if (![...value].some((flag) => "dumbtec".includes(flag))) for (const flag of "dumbt") this.flags.add(flag);
851
- }
852
- async print(direction, reader, options, filename) {
853
- if (options.verbose && options.assume !== "bytes") await this.life.diagnostic(`${direction}: Assuming UTF-16${options.assume === "le" ? "LE" : "BE"} encoding.
854
- `);
855
- const input = await readEncoding(reader);
856
- if (input.error) {
857
- if (!options.quiet) await this.life.diagnostic(`${direction}: can not read from input file: Success
858
- `);
859
- return;
860
- }
861
- const bom = encodingLabels[input.bom];
862
- const encoding = input.bom === "bytes" ? options.assume : input.bom === "le" || input.bom === "be" ? input.bom : "bytes";
863
- const byte = input.byte;
864
- let dos = 0, unix = 0, mac = 0, previous = -1, last = "noeol", binary = false;
865
- for (; ; ) {
866
- const b1 = byte();
867
- let current = typeof b1 === "number" ? b1 : await b1;
868
- if (current === -1) break;
869
- if (encoding !== "bytes") {
870
- const b2 = byte();
871
- const trail = typeof b2 === "number" ? b2 : await b2;
872
- if (trail === -1) break;
873
- current = encoding === "le" ? current + trail * 256 : current * 256 + trail;
874
- }
875
- if (current < 32 && ![9, 10, 12, 13].includes(current)) binary = true;
876
- if (current === 13) {
877
- mac++;
878
- last = "mac";
879
- } else if (current === 10 && previous === 13) {
880
- dos++;
881
- mac--;
882
- last = "dos";
883
- } else if (current === 10) {
884
- unix++;
885
- last = "unix";
886
- } else last = "noeol";
887
- previous = current;
888
- }
889
- const flags = this.flags;
890
- if (flags.has("c") && (binary && !options.force || (direction === "dos2unix" ? dos : unix) === 0 && (!options.addEol || last === (direction === "dos2unix" ? "unix" : "dos")))) return;
891
- const eol = flags.has("e") || options.addEol && !flags.has("c");
892
- const fields = ["d", "u", "m", "b", "t", "e"].filter((flag) => flag === "e" ? eol : flags.has(flag));
893
- const separator = fields.length ? " " : "";
894
- const terminator = flags.has("0") ? "\0" : "\n";
895
- if (flags.has("p")) filename = filename.slice(Math.max(filename.lastIndexOf("/"), filename.lastIndexOf("\\")) + 1);
896
- let text = "";
897
- if (flags.has("h") && !this.headerDone) {
898
- const labels = { d: " DOS", u: " UNIX", m: " MAC", b: " BOM ", t: " TXTBIN", e: " LASTLN" };
899
- text += fields.map((field) => labels[field]).join("") + (filename ? `${separator}FILE` : "") + terminator;
900
- this.headerDone = true;
901
- }
902
- const values = {
903
- d: ` ${String(dos).padStart(6)}`,
904
- u: ` ${String(unix).padStart(6)}`,
905
- m: ` ${String(mac).padStart(6)}`,
906
- b: ` ${bom.padEnd(8)}`,
907
- t: binary ? " binary" : " text ",
908
- e: ` ${last.padEnd(5)} `
909
- };
910
- text += fields.map((field) => values[field]).join("") + (filename ? separator + filename : "") + terminator;
911
- const budget = this.life.budget;
912
- budget.check(text.length * 3, budget.limits.maxBufferedBytes, "information buffer bytes");
913
- const bytes = new TextEncoder().encode(text);
914
- budget.emitted(bytes.length);
915
- await this.life.stdout(bytes);
916
- }
917
- };
918
-
919
- // packages/safe-bash-command-dos2unix/src/index.ts
920
- function definition(direction, options) {
921
- const limits = settings(options);
922
- const command = { name: direction, description: "Convert DOS and Unix line endings in bounded VFS files or streams", async execute(context) {
923
- let life;
924
- const caller = context.signal;
925
- caller.throwIfAborted();
926
- const stdout = context.stdout;
927
- caller.throwIfAborted();
928
- const capability = stdout.ownedOutput;
929
- caller.throwIfAborted();
930
- const consumer = capability?.consumerClosed;
931
- caller.throwIfAborted();
932
- consumer?.throwIfAborted();
933
- const failureHandler = stdout[outputFailure];
934
- caller.throwIfAborted();
935
- consumer?.throwIfAborted();
936
- const registerCleanup = context.registerCleanup;
937
- caller.throwIfAborted();
938
- consumer?.throwIfAborted();
939
- const admission = { closed: false };
940
- const destination = capability ?? stdout;
941
- const write = async (bytes) => {
942
- life.assertOpen();
943
- const method = destination.write;
944
- life.assertOpen();
945
- await Reflect.apply(method, destination, [bytes]);
946
- };
947
- const captured = {
948
- write,
949
- ...capability ? { ownedOutput: { consumerClosed: consumer, write } } : {},
950
- ...failureHandler ? { [outputFailure]: failureHandler } : {}
951
- };
952
- const output = createOutputOperation({ signal: caller, ...registerCleanup ? { registerCleanup(cleanup) {
953
- caller.throwIfAborted();
954
- Reflect.apply(registerCleanup, context, [() => {
955
- admission.closed = true;
956
- return cleanup();
957
- }]);
958
- } } : {} }, captured);
959
- const budget = new Budget(context, limits, output.signal, caller, admission);
960
- let failure;
961
- let primary;
962
- let status = 0;
963
- try {
964
- life = new Lifecycle(budget, output, captured, caller, admission);
965
- const active = life;
966
- const files = new Files(active);
967
- const args = await budget.arguments();
968
- const flags = { keepBom: direction === "unix2dos", addBom: false, keepUtf16: false, assume: "bytes", force: false, quiet: false, newline: false, sevenBit: false, keepDate: false, newFile: false, addEol: false, verbose: false, allowChown: false };
969
- let toStdout = false;
970
- const information = new FileInformation(active);
971
- const diagnostic = async (text) => {
972
- await active.diagnostic(`${direction}: ${text}
973
- `);
974
- };
975
- const converted = async (result, name, destination2) => {
976
- if (flags.quiet) return;
977
- const locale = context.env.LC_ALL || context.env.LC_CTYPE || context.env.LANG || "C";
978
- const outFormat = flags.keepUtf16 ? result.encoding : locale === "C" || locale === "POSIX" ? "ANSI_X3.4-1968" : "UTF-8";
979
- const encoding = result.encoding ? `${result.encoding} ` : "";
980
- const target = result.encoding ? `${outFormat} ` : "";
981
- await diagnostic(`converting ${encoding}file ${name} to ${flags.newFile ? `${target}file ${destination2} in ` : target}${direction === "dos2unix" ? "Unix" : "DOS"} format...`);
982
- };
983
- const file = async (name, destination2) => {
984
- budget.file();
985
- const inputPath = files.path(name);
986
- const outputPath = files.path(destination2);
987
- const expected = await files.stat(outputPath);
988
- const readOnly = !flags.newFile && (toStdout || information.flags.size > 0);
989
- if (!readOnly && expected?.type === "symlink") {
990
- if (!flags.quiet) await diagnostic(flags.newFile ? `Skipping ${name}, output file ${destination2} is a symbolic link.` : `Skipping symbolic link ${name}.`);
991
- return;
992
- }
993
- let input = await files.stat(inputPath);
994
- if (!input || input.type !== "file" && input.type !== "symlink") {
995
- if (!flags.quiet) {
996
- if (!input) {
997
- status = 2;
998
- await diagnostic(`${name}: No such file or directory`);
999
- }
1000
- await diagnostic(`Skipping ${name}, not a regular file.`);
1001
- }
1002
- return;
1003
- }
1004
- let path = inputPath;
1005
- if (input.type === "symlink") {
1006
- path = await files.call((fs) => fs.realpath, [path, { signal: budget.signal }]);
1007
- path = files.path(path);
1008
- input = await files.stat(path);
1009
- if (!input || input.type !== "file") {
1010
- if (!flags.quiet) await diagnostic(`Skipping symbolic link ${name}, target is not a regular file.`);
1011
- return;
1012
- }
1013
- }
1014
- if (readOnly) {
1015
- const reader2 = new Reader(active);
1016
- try {
1017
- await reader2.open(path, input);
1018
- if (information.flags.size) await information.print(direction, reader2, flags, name);
1019
- else {
1020
- const writer2 = new Writer(active, (bytes) => active.stdout(bytes));
1021
- try {
1022
- const result2 = await convert(direction, reader2, writer2, active, flags, name, true);
1023
- if (result2.kind === "unicode") status ||= 1;
1024
- if (result2.kind === "ok") await converted(result2, name, destination2);
1025
- } finally {
1026
- writer2.release();
1027
- }
1028
- }
1029
- } finally {
1030
- await reader2.close();
1031
- }
1032
- return;
1033
- }
1034
- if (!sameIdentity(input, input) || expected && (expected.type !== "file" || !sameIdentity(expected, expected))) throw new LineEndingError("file conversion requires stable regular-file identities");
1035
- const reader = new Reader(active);
1036
- const stage = new Stage(files, outputPath, expected);
1037
- const writer = new Writer(active, (bytes) => stage.append(bytes));
1038
- let result;
1039
- let failed;
1040
- try {
1041
- await reader.open(path, input);
1042
- let opened = false;
1043
- try {
1044
- await stage.open();
1045
- opened = true;
1046
- } catch (error) {
1047
- if (error instanceof FsError2 && error.code === "ENOENT") {
1048
- if (!flags.quiet) {
1049
- status = 2;
1050
- await diagnostic("Failed to open temporary output file: No such file or directory");
1051
- await diagnostic(`problems converting file ${name}${flags.newFile ? ` to file ${destination2}` : ""}`);
1052
- }
1053
- } else throw error;
1054
- }
1055
- if (opened && await files.stat(outputPath) !== void 0 && expected === void 0) throw new LineEndingError("destination appeared during acquisition");
1056
- if (opened) result = await convert(direction, reader, writer, active, flags, name, false);
1057
- await reader.close();
1058
- if (result?.kind === "ok") {
1059
- await stage.publish(input, flags);
1060
- await converted(result, name, destination2);
1061
- } else if (result?.kind === "unicode") status ||= 1;
1062
- else if (result?.kind === "bom-error" && !flags.quiet) await diagnostic(`problems converting file ${name}${flags.newFile ? ` to file ${destination2}` : ""}`);
1063
- } catch (error) {
1064
- failed = { reason: error };
1065
- }
1066
- try {
1067
- await reader.close();
1068
- await stage.cleanup();
1069
- } catch (error) {
1070
- throw failed ? new AggregateError([failed.reason, error], "line-ending conversion and cleanup failed") : error;
1071
- } finally {
1072
- writer.release();
1073
- }
1074
- if (failed) throw failed.reason;
1075
- };
1076
- const stdin = async () => {
1077
- const reader = new Reader(active);
1078
- const writer = new Writer(active, (bytes) => active.stdout(bytes));
1079
- await reader.open();
1080
- if (information.flags.size) await information.print(direction, reader, flags, "");
1081
- else {
1082
- const result = await convert(direction, reader, writer, active, flags, "stdin", true);
1083
- if (result.kind === "unicode" || result.kind === "bom-error" || result.kind === "binary" && !flags.quiet) status ||= 1;
1084
- }
1085
- };
1086
- let paired = true, processOptions = true, sawFile = false, stop = false;
1087
- let index = 0;
1088
- for (; index < args.length && !stop; index++) {
1089
- const argument = args[index];
1090
- if (processOptions && argument.startsWith("-")) {
1091
- switch (argument) {
1092
- case "-":
1093
- sawFile = true;
1094
- await stdin();
1095
- break;
1096
- case "--":
1097
- processOptions = false;
1098
- break;
1099
- case "-b":
1100
- case "--keep-bom":
1101
- flags.keepBom = true;
1102
- break;
1103
- case "-r":
1104
- case "--remove-bom":
1105
- flags.keepBom = false;
1106
- flags.addBom = false;
1107
- break;
1108
- case "-m":
1109
- case "--add-bom":
1110
- flags.addBom = true;
1111
- break;
1112
- case "-f":
1113
- case "--force":
1114
- flags.force = true;
1115
- break;
1116
- case "-s":
1117
- case "--safe":
1118
- flags.force = false;
1119
- break;
1120
- case "-q":
1121
- case "--quiet":
1122
- flags.quiet = true;
1123
- flags.verbose = false;
1124
- break;
1125
- case "-v":
1126
- case "--verbose":
1127
- flags.verbose = true;
1128
- flags.quiet = false;
1129
- break;
1130
- case "-e":
1131
- case "--add-eol":
1132
- flags.addEol = true;
1133
- break;
1134
- case "--no-add-eol":
1135
- flags.addEol = false;
1136
- break;
1137
- case "--allow-chown":
1138
- flags.allowChown = true;
1139
- break;
1140
- case "--no-allow-chown":
1141
- flags.allowChown = false;
1142
- break;
1143
- case "-i":
1144
- case "--info":
1145
- information.configure("");
1146
- break;
1147
- case "-l":
1148
- case "--newline":
1149
- flags.newline = true;
1150
- break;
1151
- case "-k":
1152
- case "--keepdate":
1153
- flags.keepDate = true;
1154
- break;
1155
- case "-u":
1156
- case "--keep-utf16":
1157
- flags.keepUtf16 = true;
1158
- break;
1159
- case "-ul":
1160
- case "--assume-utf16le":
1161
- flags.assume = "le";
1162
- break;
1163
- case "-ub":
1164
- case "--assume-utf16be":
1165
- flags.assume = "be";
1166
- break;
1167
- case "-7":
1168
- flags.sevenBit = true;
1169
- flags.assume = "bytes";
1170
- break;
1171
- case "-ascii":
1172
- flags.assume = "bytes";
1173
- flags.sevenBit = false;
1174
- flags.keepUtf16 = false;
1175
- break;
1176
- case "-S":
1177
- case "--skip-symlink":
1178
- break;
1179
- case "-o":
1180
- case "--oldfile":
1181
- case "-n":
1182
- case "--newfile":
1183
- case "-O":
1184
- case "--to-stdout":
1185
- if (!paired) {
1186
- await diagnostic(`target of file ${args[index - 1]} not specified in new-file mode`);
1187
- status = 1;
1188
- sawFile = true;
1189
- stop = true;
1190
- }
1191
- flags.newFile = argument === "-n" || argument === "--newfile";
1192
- if (argument !== "-O" && argument !== "--to-stdout") information.flags.clear();
1193
- if (!flags.newFile) toStdout = argument === "-O" || argument === "--to-stdout";
1194
- break;
1195
- case "-c":
1196
- case "--convmode": {
1197
- const mode = args[++index];
1198
- if (mode === void 0) {
1199
- await diagnostic(`option '${argument}' requires an argument`);
1200
- status = 1;
1201
- sawFile = true;
1202
- stop = true;
1203
- } else if (mode.toLowerCase() === "ascii") {
1204
- flags.sevenBit = false;
1205
- flags.assume = "bytes";
1206
- flags.keepUtf16 = false;
1207
- } else if (mode.toLowerCase() === "7bit") {
1208
- flags.sevenBit = true;
1209
- flags.assume = "bytes";
1210
- } else {
1211
- await diagnostic(`invalid ${mode} conversion mode specified`);
1212
- status = 1;
1213
- sawFile = true;
1214
- stop = true;
1215
- }
1216
- break;
1217
- }
1218
- case "-h":
1219
- case "--help": {
1220
- const bytes = new TextEncoder().encode(`Usage: ${direction} [options] [file ...] [-n infile outfile ...]
1221
- Virtual byte/UTF-16 profile:
1222
- -o -n -O -i[FLAGS] -e -v -b -r -m -f -s -q -k -l -u -ul -ub -7 -ascii --
1223
- --no-add-eol --allow-chown --no-allow-chown
1224
- `);
1225
- budget.emitted(bytes.length);
1226
- await active.stdout(bytes);
1227
- stop = true;
1228
- sawFile = true;
1229
- break;
1230
- }
1231
- default:
1232
- if (argument.startsWith("--info=")) information.configure(argument.slice(7));
1233
- else if (argument.startsWith("-i")) information.configure(argument.slice(2));
1234
- else throw new LineEndingError(`unsupported option in virtual profile: ${argument}`);
1235
- }
1236
- } else {
1237
- sawFile = true;
1238
- if (!flags.newFile) await file(argument, argument);
1239
- else if (paired) paired = false;
1240
- else {
1241
- await file(args[index - 1], argument);
1242
- paired = true;
1243
- }
1244
- }
1245
- }
1246
- if (!sawFile) await stdin();
1247
- if (!paired) {
1248
- await diagnostic(`target of file ${args[Math.min(index - 1, args.length - 1)]} not specified in new-file mode`);
1249
- status = 1;
1250
- }
1251
- } catch (error) {
1252
- primary = { reason: error };
1253
- if (error instanceof LineEndingError && life && !output.signal.aborted) {
1254
- status = error.status;
1255
- try {
1256
- await life.diagnostic(`${direction}: ${error.message}
1257
- `);
1258
- } catch (reporting) {
1259
- failure = { reason: new AggregateError([error, reporting], "line-ending failure reporting failed") };
1260
- }
1261
- } else failure = { reason: error };
1262
- }
1263
- try {
1264
- await output.close();
1265
- } catch (cleanup) {
1266
- const previous = failure ?? primary;
1267
- failure = { reason: previous ? new AggregateError([previous.reason, cleanup], "line-ending execution and cleanup failed") : cleanup };
1268
- }
1269
- caller.throwIfAborted();
1270
- output.signal.throwIfAborted();
1271
- if (life?.diagnosticCancellation) throw life.diagnosticCancellation.reason;
1272
- if (failure) throw failure.reason;
1273
- return { exitCode: status };
1274
- } };
1275
- if (options.limits === void 0) builtInDirectContextExecutors.add(command.execute);
1276
- return command;
1277
- }
1278
- function createDos2unixCommand(options = {}) {
1279
- return definition("dos2unix", options);
1280
- }
1281
- function createUnix2dosCommand(options = {}) {
1282
- return definition("unix2dos", options);
1283
- }
1284
- function createDos2unixCommands(options = {}) {
1285
- return [createDos2unixCommand(options), createUnix2dosCommand(options)];
1286
- }
1287
- function dos2unixCommands(options = {}) {
1288
- const commands = createDos2unixCommands(options);
1289
- return { name: "line-ending-commands", setup(host) {
1290
- if (!options.replace) {
1291
- for (const command of commands) {
1292
- if (host.commands.has(command.name)) throw new Error(`Command already registered: ${command.name}`);
1293
- }
1294
- }
1295
- for (const command of commands) host.commands.register(command, { replace: options.replace ?? false });
1296
- } };
1297
- }
1298
-
1299
- // packages/safe-bash/src/commands/line-endings/index.ts
1300
- function evalSyncLineEndings(cmdName, inBytes, opArgs, readFileSync, writeFileSync) {
1301
- let force = false;
1302
- let quiet = false;
1303
- let toStdout = false;
1304
- let newFileMode = false;
1305
- let addEol = false;
1306
- let newline = false;
1307
- let convMode = "ascii";
1308
- let bomMode = "keep";
1309
- let endOfOptions = false;
1310
- const files = [];
1311
- for (let i = 0; i < opArgs.length; i++) {
1312
- const a = opArgs[i];
1313
- if (!endOfOptions && a === "--") {
1314
- endOfOptions = true;
1315
- continue;
1316
- }
1317
- if (!endOfOptions && a.startsWith("--") && a.length > 2) {
1318
- if (a === "--quiet") {
1319
- quiet = true;
1320
- continue;
1321
- }
1322
- if (a === "--force") {
1323
- force = true;
1324
- continue;
1325
- }
1326
- if (a === "--to-stdout") {
1327
- toStdout = true;
1328
- continue;
1329
- }
1330
- if (a === "--newfile") {
1331
- newFileMode = true;
1332
- continue;
1333
- }
1334
- if (a === "--add-eol") {
1335
- addEol = true;
1336
- continue;
1337
- }
1338
- if (a === "--no-add-eol") {
1339
- addEol = false;
1340
- continue;
1341
- }
1342
- if (a === "--newline") {
1343
- newline = true;
1344
- continue;
1345
- }
1346
- if (a === "--no-newline") {
1347
- newline = false;
1348
- continue;
1349
- }
1350
- if (a === "--remove-bom") {
1351
- bomMode = "remove";
1352
- continue;
1353
- }
1354
- if (a === "--add-bom") {
1355
- bomMode = "add";
1356
- continue;
1357
- }
1358
- if (a === "--keep-bom") {
1359
- bomMode = "keep";
1360
- continue;
1361
- }
1362
- if (a === "--convmode" || a.startsWith("--convmode=")) {
1363
- const v = a.startsWith("--convmode=") ? a.slice(11) : opArgs[++i];
1364
- if (v === "mac") {
1365
- convMode = "mac";
1366
- continue;
1367
- }
1368
- if (v === "ascii") {
1369
- convMode = "ascii";
1370
- continue;
1371
- }
1372
- return void 0;
1373
- }
1374
- return void 0;
1375
- }
1376
- if (!endOfOptions && a.startsWith("-") && a.length > 1) {
1377
- for (let j = 1; j < a.length; j++) {
1378
- const ch = a[j];
1379
- if (ch === "q") quiet = true;
1380
- else if (ch === "f") force = true;
1381
- else if (ch === "O") toStdout = true;
1382
- else if (ch === "n") newFileMode = true;
1383
- else if (ch === "e") addEol = true;
1384
- else if (ch === "E") addEol = false;
1385
- else if (ch === "l") newline = true;
1386
- else if (ch === "N") newline = false;
1387
- else if (ch === "r") bomMode = "remove";
1388
- else if (ch === "m") bomMode = "add";
1389
- else if (ch === "b") bomMode = "keep";
1390
- else if (ch === "c") {
1391
- const v = j + 1 < a.length ? a.slice(j + 1) : opArgs[++i];
1392
- if (v === "mac") convMode = "mac";
1393
- else if (v === "ascii") convMode = "ascii";
1394
- else return void 0;
1395
- break;
1396
- } else return void 0;
1397
- }
1398
- continue;
1399
- }
1400
- files.push(a);
1401
- }
1402
- if (files.length > 0 && !toStdout) {
1403
- if (!quiet || !readFileSync || !writeFileSync) return void 0;
1404
- if (newFileMode && files.length % 2 !== 0) return void 0;
1405
- const pairs = [];
1406
- if (newFileMode) {
1407
- for (let k = 0; k < files.length; k += 2) pairs.push([files[k], files[k + 1]]);
1408
- } else {
1409
- for (const f of files) pairs.push([f, f]);
1410
- }
1411
- for (const [inPath, outPath] of pairs) {
1412
- if (inPath === "-" || outPath === "-") return void 0;
1413
- const b = readFileSync(inPath);
1414
- if (!b || b.byteLength > 16384) return void 0;
1415
- const subArgs = [
1416
- force ? "-fO" : "-O",
1417
- ...addEol ? ["-e"] : [],
1418
- ...newline ? ["-l"] : [],
1419
- ...convMode === "mac" ? ["-c", "mac"] : [],
1420
- ...bomMode === "remove" ? ["-r"] : bomMode === "add" ? ["-m"] : []
1421
- ];
1422
- const converted = evalSyncLineEndings(cmdName, b, subArgs);
1423
- if (!converted || !writeFileSync(outPath, converted)) return void 0;
1424
- }
1425
- return new Uint8Array(0);
1426
- }
1427
- const chunks = [];
1428
- if (files.length === 0) {
1429
- if (!inBytes || inBytes.byteLength > 16384) return void 0;
1430
- chunks.push(inBytes);
1431
- } else {
1432
- if (!readFileSync) return void 0;
1433
- for (const f of files) {
1434
- const b = readFileSync(f);
1435
- if (!b || b.byteLength > 16384) return void 0;
1436
- chunks.push(b);
1437
- }
1438
- }
1439
- const out = [];
1440
- for (const src of chunks) {
1441
- let startIdx = 0;
1442
- let hasUtf8Bom = false;
1443
- if (src.byteLength >= 2) {
1444
- const b0 = src[0], b1 = src[1];
1445
- if (b0 === 255 && b1 === 254 || b0 === 254 && b1 === 255 || b0 === 132 && b1 === 49) {
1446
- return void 0;
1447
- }
1448
- if (src.byteLength >= 3 && b0 === 239 && b1 === 187 && src[2] === 191) {
1449
- hasUtf8Bom = true;
1450
- startIdx = 3;
1451
- }
1452
- }
1453
- if (!force) {
1454
- for (let i = startIdx; i < src.byteLength; i++) {
1455
- const c = src[i];
1456
- if (c < 32 && c !== 9 && c !== 10 && c !== 12 && c !== 13) return void 0;
1457
- }
1458
- }
1459
- if (bomMode === "add" || bomMode === "keep" && hasUtf8Bom) {
1460
- out.push(239, 187, 191);
1461
- }
1462
- let last = -1;
1463
- if (cmdName === "dos2unix") {
1464
- for (let i = startIdx; i < src.byteLength; i++) {
1465
- const c = src[i];
1466
- if (convMode === "mac") {
1467
- if (c === 13 && (i + 1 >= src.byteLength || src[i + 1] !== 10)) {
1468
- out.push(10);
1469
- if (newline) out.push(10);
1470
- last = 10;
1471
- } else {
1472
- out.push(c);
1473
- last = c;
1474
- }
1475
- } else if (c === 13 && i + 1 < src.byteLength && src[i + 1] === 10) {
1476
- out.push(10);
1477
- if (newline) out.push(10);
1478
- last = 10;
1479
- i++;
1480
- } else {
1481
- out.push(c);
1482
- last = c;
1483
- }
1484
- }
1485
- if (addEol && last !== -1 && last !== 10) out.push(10);
1486
- } else {
1487
- let prev = 0;
1488
- for (let i = startIdx; i < src.byteLength; i++) {
1489
- const c = src[i];
1490
- if (convMode === "mac") {
1491
- if (c === 10 && prev !== 13) {
1492
- out.push(13);
1493
- if (newline) out.push(13);
1494
- last = 13;
1495
- } else {
1496
- out.push(c);
1497
- last = c;
1498
- }
1499
- } else if (c === 10 && prev !== 13) {
1500
- out.push(13, 10);
1501
- if (newline) out.push(13, 10);
1502
- last = 10;
1503
- } else {
1504
- out.push(c);
1505
- if (newline && c === 10) out.push(13, 10);
1506
- last = c;
1507
- }
1508
- prev = c;
1509
- }
1510
- if (addEol && last !== -1 && last !== (convMode === "mac" ? 13 : 10)) {
1511
- if (convMode === "mac") out.push(13);
1512
- else out.push(13, 10);
1513
- }
1514
- }
1515
- }
1516
- return new Uint8Array(out);
1517
- }
1518
-
1519
- export {
1520
- createDos2unixCommand,
1521
- createUnix2dosCommand,
1522
- createDos2unixCommands,
1523
- dos2unixCommands,
1524
- evalSyncLineEndings
1525
- };
1526
- //# sourceMappingURL=chunk-6IIFUPN2.js.map