@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,708 +0,0 @@
1
- import {
2
- builtInDirectContextExecutors
3
- } from "./chunk-ISCZG7WA.js";
4
- import {
5
- init_platform
6
- } from "./chunk-ZG7GWQ5P.js";
7
-
8
- // packages/safe-bash/src/commands/factor/index.ts
9
- init_platform();
10
-
11
- // packages/safe-bash-command-factor/src/index.ts
12
- init_platform();
13
-
14
- // packages/safe-bash-command-factor/src/command.ts
15
- init_platform();
16
- import { createOutputOperation } from "../../safe-bash-contracts/output.js";
17
-
18
- // packages/safe-bash-command-factor/src/internal.ts
19
- init_platform();
20
- import { getCommandArguments } from "../../safe-bash-contracts/index.js";
21
- import { shellValueByteLength, shellValueBytes } from "../../safe-bash-contracts/value.js";
22
- import { yieldTurn } from "../../safe-bash-contracts/yield.js";
23
- import { PublicDiagnostic } from "../../safe-bash-contracts/diagnostics.js";
24
- function settings(options) {
25
- const limits = {
26
- maxValue: Infinity,
27
- maxArguments: Infinity,
28
- maxArgumentBytes: Infinity,
29
- maxInputBytes: Infinity,
30
- maxTokenBytes: Infinity,
31
- maxNumbers: Infinity,
32
- maxBufferedBytes: Infinity,
33
- maxOutputBytes: Infinity,
34
- maxDiagnosticBytes: Infinity,
35
- maxWork: Infinity,
36
- maxEmptyChunks: Infinity,
37
- ...options.limits
38
- };
39
- for (const [name, value] of Object.entries(limits)) {
40
- if (name === "maxValue" && typeof value === "bigint" && value > 0n) continue;
41
- if (value !== Infinity && (!Number.isSafeInteger(value) || value < 1)) throw new RangeError(`Invalid factor limit: ${name}`);
42
- }
43
- return Object.freeze(limits);
44
- }
45
- var FactorError = class extends PublicDiagnostic {
46
- constructor(message, usage = false) {
47
- super(message);
48
- this.usage = usage;
49
- }
50
- usage;
51
- };
52
- function raw(value) {
53
- let result = "";
54
- for (let offset = 0; offset < value.length; offset += 4096) result += String.fromCharCode(...value.subarray(offset, offset + 4096));
55
- return result;
56
- }
57
- function bytes(value) {
58
- const out = new Uint8Array(value.length);
59
- for (let i = 0; i < value.length; i++) out[i] = value.charCodeAt(i);
60
- return out;
61
- }
62
- var escapes = { "\x07": "\\a", "\b": "\\b", "\f": "\\f", "\n": "\\n", "\r": "\\r", " ": "\\t", "\v": "\\v", "\\": "\\\\", "'": "\\'" };
63
- function quote(value, budget, suffixLength) {
64
- let length = 2;
65
- for (const character of value) {
66
- const code = character.charCodeAt(0);
67
- length += escapes[character]?.length ?? (code >= 32 && code < 127 ? 1 : 4);
68
- }
69
- budget.diagnosticRoom(length + suffixLength);
70
- let result = "'";
71
- for (const character of value) {
72
- const code = character.charCodeAt(0);
73
- result += escapes[character] ?? (code >= 32 && code < 127 ? character : `\\${code.toString(8).padStart(3, "0")}`);
74
- }
75
- return `${result}'`;
76
- }
77
- var Budget = class {
78
- constructor(context, limits, signal) {
79
- this.context = context;
80
- this.limits = limits;
81
- this.signal = signal;
82
- this.pollSignal = Object.prototype.hasOwnProperty.call(signal, "aborted");
83
- if (signal.aborted) {
84
- this.signalAborted = true;
85
- } else if (!this.pollSignal) {
86
- signal.addEventListener("abort", () => {
87
- this.signalAborted = true;
88
- }, { once: true });
89
- }
90
- }
91
- context;
92
- limits;
93
- signal;
94
- work = 0;
95
- checkpoint = 0;
96
- retained = 0;
97
- input = 0;
98
- numbers = 0;
99
- output = 0;
100
- diagnostics = 0;
101
- signalAborted = false;
102
- pollSignal;
103
- check(value, maximum, label) {
104
- if ((value | 0) === value && value >= 0) {
105
- if (value <= maximum) return;
106
- throw new FactorError(`${label} limit exceeded`);
107
- }
108
- if (!Number.isSafeInteger(value) || value < 0 || value > maximum) throw new FactorError(`${label} limit exceeded`);
109
- }
110
- charge(amount = 1) {
111
- if (this.signalAborted || this.pollSignal && this.signal.aborted) this.signal.throwIfAborted();
112
- const next = this.work + amount;
113
- this.check(next, this.limits.maxWork, "work");
114
- this.work = next;
115
- }
116
- checkpointWork() {
117
- if (this.signalAborted || this.pollSignal && this.signal.aborted) this.signal.throwIfAborted();
118
- if (this.work - this.checkpoint < 1024) return;
119
- this.checkpoint = this.work;
120
- return yieldTurn(this.signal);
121
- }
122
- retain(amount) {
123
- this.check(this.retained + amount, this.limits.maxBufferedBytes, "buffered bytes");
124
- this.retained += amount;
125
- }
126
- number() {
127
- this.check(++this.numbers, this.limits.maxNumbers, "numbers");
128
- this.charge();
129
- }
130
- inputBytes(amount) {
131
- this.check(this.input + amount, this.limits.maxInputBytes, "input bytes");
132
- this.input += amount;
133
- }
134
- outputRoom(amount) {
135
- this.check(this.output + amount, this.limits.maxOutputBytes, "output bytes");
136
- }
137
- diagnosticRoom(amount) {
138
- this.check(this.diagnostics + amount, this.limits.maxDiagnosticBytes, "diagnostic bytes");
139
- }
140
- emitted(amount, diagnostic) {
141
- if (diagnostic) {
142
- this.diagnosticRoom(amount);
143
- this.diagnostics += amount;
144
- } else {
145
- this.outputRoom(amount);
146
- this.charge(amount);
147
- this.output += amount;
148
- }
149
- }
150
- arguments() {
151
- this.check(this.context.args.length, this.limits.maxArguments, "argument count");
152
- let total = 0;
153
- for (const argument of this.context.args) {
154
- total += argument.length;
155
- this.check(total, this.limits.maxArgumentBytes, "argument bytes");
156
- for (let offset = 0; offset < argument.length; offset++) {
157
- const unit = argument.charCodeAt(offset);
158
- if (unit >= 55296 && unit <= 56319) {
159
- const next = argument.charCodeAt(++offset);
160
- if (!(next >= 56320 && next <= 57343)) throw new FactorError("arguments must contain well-formed Unicode");
161
- } else if (unit >= 56320 && unit <= 57343) throw new FactorError("arguments must contain well-formed Unicode");
162
- }
163
- }
164
- total = 0;
165
- const values = this.context.argumentValues === void 0 ? this.context.args : getCommandArguments(this.context).values;
166
- for (const argument of values) {
167
- total += shellValueByteLength(argument);
168
- this.check(total, this.limits.maxArgumentBytes, "argument bytes");
169
- }
170
- this.charge(total);
171
- this.retain(total * 8 + this.context.args.length * 64 + 128);
172
- return getCommandArguments(this.context).values.map((argument) => {
173
- const value = shellValueBytes(argument);
174
- if (value.includes(0)) throw new FactorError("NUL is not supported in arguments");
175
- return raw(value);
176
- });
177
- }
178
- };
179
-
180
- // packages/safe-bash-command-factor/src/io.ts
181
- init_platform();
182
- import { FsError } from "../../safe-bash-contracts/index.js";
183
- var Lifecycle = class {
184
- constructor(budget, output) {
185
- this.budget = budget;
186
- output.registerCleanup(() => this.close());
187
- }
188
- budget;
189
- pending = /* @__PURE__ */ new Set();
190
- cleanups = [];
191
- closing;
192
- cleanup(action) {
193
- this.assertOpen();
194
- this.cleanups.push(action);
195
- }
196
- assertOpen() {
197
- this.budget.signal.throwIfAborted();
198
- if (this.closing) throw new FactorError("command is closed");
199
- }
200
- async operation(action) {
201
- this.assertOpen();
202
- const pending = Promise.resolve().then(() => {
203
- this.assertOpen();
204
- return action();
205
- });
206
- this.pending.add(pending);
207
- try {
208
- const value = await pending;
209
- this.assertOpen();
210
- return value;
211
- } finally {
212
- this.pending.delete(pending);
213
- }
214
- }
215
- close() {
216
- return this.closing ??= Promise.resolve().then(async () => {
217
- while (this.pending.size) await Promise.allSettled([...this.pending]);
218
- const failures = [];
219
- for (const cleanup of this.cleanups) {
220
- try {
221
- await cleanup();
222
- } catch (error) {
223
- failures.push(error);
224
- }
225
- }
226
- if (failures.length === 1) throw failures[0];
227
- if (failures.length > 1) throw new AggregateError(failures, "factor cleanup failed");
228
- });
229
- }
230
- async write(value, diagnostic = false) {
231
- this.budget.emitted(value.length, diagnostic);
232
- await this.operation(async () => {
233
- const sink = diagnostic ? this.budget.context.stderr : this.budget.context.stdout;
234
- this.assertOpen();
235
- const destination = sink.ownedOutput ?? sink;
236
- this.assertOpen();
237
- const write = destination.write;
238
- this.assertOpen();
239
- await Reflect.apply(write, destination, [value]);
240
- });
241
- }
242
- async diagnostic(value) {
243
- this.budget.diagnosticRoom(value.length);
244
- await this.write(bytes(value), true);
245
- }
246
- };
247
- var TokenReader = class {
248
- constructor(lifecycle) {
249
- this.lifecycle = lifecycle;
250
- lifecycle.cleanup(async () => {
251
- try {
252
- if (!this.ended) await this.iterator?.return?.();
253
- } finally {
254
- this.iterator = void 0;
255
- lifecycle.budget.retain(-this.chunk.length - this.token.length);
256
- this.chunk = new Uint8Array();
257
- this.token = new Uint8Array();
258
- }
259
- });
260
- }
261
- lifecycle;
262
- iterator;
263
- chunk = new Uint8Array();
264
- token = new Uint8Array();
265
- offset = 0;
266
- ended = false;
267
- empties = 0;
268
- get() {
269
- const { budget } = this.lifecycle;
270
- budget.charge();
271
- const cp = budget.checkpointWork();
272
- if (!cp && this.offset < this.chunk.length) return this.chunk[this.offset++];
273
- return this.getSlow(cp);
274
- }
275
- async getSlow(cp) {
276
- const { budget } = this.lifecycle;
277
- if (cp) await cp;
278
- while (this.offset === this.chunk.length) {
279
- if (this.ended) return -1;
280
- let next;
281
- try {
282
- if (!this.iterator) await this.lifecycle.operation(() => {
283
- const source = budget.context.stdin;
284
- this.lifecycle.assertOpen();
285
- const factory = source[Symbol.asyncIterator];
286
- this.lifecycle.assertOpen();
287
- this.iterator = Reflect.apply(factory, source, []);
288
- });
289
- budget.charge();
290
- next = await this.lifecycle.operation(() => {
291
- const iterator = this.iterator;
292
- const advance = iterator.next;
293
- this.lifecycle.assertOpen();
294
- return Reflect.apply(advance, iterator, []);
295
- });
296
- } catch (error) {
297
- if (error instanceof FsError) throw new FactorError(`standard input: ${error.code === "EIO" ? "Input/output error" : error.message}`);
298
- throw error;
299
- }
300
- budget.retain(-this.chunk.length);
301
- this.chunk = new Uint8Array();
302
- this.offset = 0;
303
- if (next.done) {
304
- this.ended = true;
305
- this.iterator = void 0;
306
- return -1;
307
- }
308
- if (!(next.value instanceof Uint8Array)) throw new TypeError("factor input requires bytes");
309
- budget.inputBytes(next.value.length);
310
- budget.charge(next.value.length);
311
- budget.retain(next.value.length);
312
- this.chunk = Uint8Array.from(next.value);
313
- if (!this.chunk.length) budget.check(++this.empties, budget.limits.maxEmptyChunks, "empty input chunks");
314
- }
315
- return this.chunk[this.offset++];
316
- }
317
- async next() {
318
- const { budget } = this.lifecycle;
319
- let length = 0, kept = 0;
320
- let nul = false;
321
- for (; ; ) {
322
- const r = this.get();
323
- const value = typeof r === "number" ? r : await r;
324
- if (value < 0 || value === 32 || value === 9 || value === 10) {
325
- if (length) {
326
- budget.charge(kept);
327
- budget.retain(kept * 4);
328
- return raw(this.token.subarray(0, kept));
329
- }
330
- if (value < 0) return void 0;
331
- } else {
332
- budget.check(++length, budget.limits.maxTokenBytes, "token bytes");
333
- if (value === 0) nul = true;
334
- if (!nul) {
335
- if (kept === this.token.length) {
336
- const capacity = Math.min(budget.limits.maxTokenBytes, Math.max(64, this.token.length * 2));
337
- budget.retain(capacity);
338
- budget.charge(this.token.length);
339
- const grown = new Uint8Array(capacity);
340
- grown.set(this.token);
341
- budget.retain(-this.token.length);
342
- this.token = grown;
343
- }
344
- this.token[kept++] = value;
345
- }
346
- }
347
- }
348
- }
349
- };
350
- var RecordWriter = class {
351
- constructor(lifecycle) {
352
- this.lifecycle = lifecycle;
353
- lifecycle.budget.retain(1024);
354
- this.retained = true;
355
- this.buffer = new Uint8Array(1024);
356
- lifecycle.cleanup(async () => {
357
- if (this.retained) {
358
- lifecycle.budget.retain(-1024);
359
- this.retained = false;
360
- }
361
- this.buffer = new Uint8Array();
362
- });
363
- }
364
- lifecycle;
365
- buffer;
366
- used = 0;
367
- retained = false;
368
- async append(record) {
369
- const { budget } = this.lifecycle;
370
- this.lifecycle.assertOpen();
371
- budget.outputRoom(this.used + record.length);
372
- budget.charge(record.length);
373
- if (record.length > 512) {
374
- await this.finish();
375
- this.used = 0;
376
- budget.retain(record.length);
377
- try {
378
- await this.lifecycle.write(bytes(record));
379
- } finally {
380
- budget.retain(-record.length);
381
- }
382
- return;
383
- }
384
- for (let offset = 0; offset < record.length; offset++) this.buffer[this.used++] = record.charCodeAt(offset);
385
- if (this.used >= 512) {
386
- let end = 512;
387
- while (this.buffer[end - 1] !== 10) end--;
388
- budget.retain(end);
389
- try {
390
- await this.lifecycle.write(this.buffer.slice(0, end));
391
- } finally {
392
- budget.retain(-end);
393
- }
394
- this.buffer.copyWithin(0, end, this.used);
395
- this.used -= end;
396
- }
397
- }
398
- async finish() {
399
- if (this.used) {
400
- const size = this.used;
401
- this.lifecycle.budget.retain(size);
402
- try {
403
- await this.lifecycle.write(this.buffer.slice(0, size));
404
- this.used = 0;
405
- } finally {
406
- this.lifecycle.budget.retain(-size);
407
- }
408
- }
409
- }
410
- };
411
-
412
- // packages/safe-bash-command-factor/src/factor.ts
413
- init_platform();
414
- async function parseNumber(input, budget) {
415
- let start = 0;
416
- while (input[start] === " ") {
417
- budget.charge();
418
- start++;
419
- {
420
- const cp = budget.checkpointWork();
421
- if (cp) await cp;
422
- }
423
- }
424
- if (input[start] === "+") start++;
425
- if (start === input.length) return "invalid";
426
- const maximum = budget.limits.maxValue === Infinity ? void 0 : BigInt(budget.limits.maxValue);
427
- let result = 0n, large = false;
428
- for (let offset = start; offset < input.length; offset++) {
429
- budget.charge();
430
- const digit = input.charCodeAt(offset) - 48;
431
- if (digit < 0 || digit > 9) return "invalid";
432
- if (!large) {
433
- result = result * 10n + BigInt(digit);
434
- if (maximum !== void 0 && result > maximum) large = true;
435
- }
436
- {
437
- const cp = budget.checkpointWork();
438
- if (cp) await cp;
439
- }
440
- }
441
- return large ? "large" : result;
442
- }
443
- async function prime64(value, budget) {
444
- if (value >= 1n << 64n || value % 2n === 0n) return false;
445
- let odd = value - 1n, powers = 0;
446
- while (odd % 2n === 0n) {
447
- odd /= 2n;
448
- powers++;
449
- budget.charge();
450
- }
451
- for (const witness of [2n, 325n, 9375n, 28178n, 450775n, 9780504n, 1795265022n]) {
452
- let base = witness % value;
453
- if (base === 0n) continue;
454
- let exponent = odd, result = 1n;
455
- while (exponent > 0n) {
456
- budget.charge();
457
- if (exponent % 2n) result = result * base % value;
458
- base = base * base % value;
459
- exponent /= 2n;
460
- {
461
- const cp = budget.checkpointWork();
462
- if (cp) await cp;
463
- }
464
- }
465
- if (result === 1n || result === value - 1n) continue;
466
- let passed = false;
467
- for (let power = 1; power < powers; power++) {
468
- budget.charge();
469
- result = result * result % value;
470
- {
471
- const cp = budget.checkpointWork();
472
- if (cp) await cp;
473
- }
474
- if (result === value - 1n) {
475
- passed = true;
476
- break;
477
- }
478
- }
479
- if (!passed) return false;
480
- }
481
- return true;
482
- }
483
- function prime32Sync(n) {
484
- if (n < 2) return false;
485
- if (n === 2 || n === 3 || n === 5 || n === 7) return true;
486
- if ((n & 1) === 0 || n % 3 === 0) return false;
487
- const bn = BigInt(n);
488
- let odd = bn - 1n;
489
- let powers = 0;
490
- while ((odd & 1n) === 0n) {
491
- odd >>= 1n;
492
- powers++;
493
- }
494
- for (const witness of [2n, 7n, 61n]) {
495
- if (bn <= witness) break;
496
- let base = witness % bn;
497
- if (base === 0n) continue;
498
- let exp = odd;
499
- let res = 1n;
500
- while (exp > 0n) {
501
- if ((exp & 1n) === 1n) res = res * base % bn;
502
- base = base * base % bn;
503
- exp >>= 1n;
504
- }
505
- if (res === 1n || res === bn - 1n) continue;
506
- let passed = false;
507
- for (let r = 1; r < powers; r++) {
508
- res = res * res % bn;
509
- if (res === bn - 1n) {
510
- passed = true;
511
- break;
512
- }
513
- }
514
- if (!passed) return false;
515
- }
516
- return true;
517
- }
518
- async function factorRecord(value, budget, exponents) {
519
- let remaining = value;
520
- const factors = [];
521
- let retained = 512, checkPrime = true;
522
- const unlimitedWork = budget.limits.maxWork === Infinity;
523
- budget.retain(retained);
524
- try {
525
- for (let divisor = 2n; divisor * divisor <= remaining; divisor = divisor === 2n ? 3n : divisor + 2n) {
526
- if (checkPrime && unlimitedWork && remaining > 256n && remaining <= 4294967295n && prime32Sync(Number(remaining))) {
527
- budget.charge(1024);
528
- {
529
- const cp = budget.checkpointWork();
530
- if (cp) await cp;
531
- }
532
- break;
533
- }
534
- if (checkPrime && remaining > 4294967295n && await prime64(remaining, budget)) break;
535
- checkPrime = false;
536
- for (; ; ) {
537
- budget.charge();
538
- {
539
- const cp = budget.checkpointWork();
540
- if (cp) await cp;
541
- }
542
- if (remaining % divisor !== 0n) break;
543
- budget.charge(2);
544
- budget.retain(16);
545
- retained += 16;
546
- factors.push(divisor);
547
- remaining /= divisor;
548
- checkPrime = true;
549
- }
550
- }
551
- if (remaining > 1n) factors.push(remaining);
552
- budget.charge(1 + factors.length * 11);
553
- let record = `${value}:`;
554
- for (let index = 0; index < factors.length; index++) {
555
- const factor = factors[index];
556
- let exponent = 1;
557
- if (exponents) {
558
- while (factors[index + 1] === factor) {
559
- exponent++;
560
- index++;
561
- }
562
- }
563
- const part = ` ${factor}${exponent > 1 ? `^${exponent}` : ""}`;
564
- budget.outputRoom(record.length + part.length + 1);
565
- budget.retain(part.length * 2);
566
- retained += part.length * 2;
567
- record += part;
568
- }
569
- return `${record}
570
- `;
571
- } finally {
572
- budget.retain(-retained);
573
- }
574
- }
575
-
576
- // packages/safe-bash-command-factor/src/command.ts
577
- function createFactorCommand(options = {}) {
578
- const limits = settings(options);
579
- return { name: "factor", description: "Print prime factors of nonnegative integers", async execute(context) {
580
- const output = createOutputOperation(context, context.stdout);
581
- const budget = new Budget(context, limits, output.signal);
582
- let lifecycle;
583
- let failure;
584
- let primary;
585
- let exitCode = 0;
586
- try {
587
- lifecycle = new Lifecycle(budget, output);
588
- const operands = [];
589
- let optionsEnded = false, debug = false, exponents = false;
590
- let information;
591
- for (const argument of budget.arguments()) {
592
- if (!optionsEnded && argument === "--") {
593
- optionsEnded = true;
594
- continue;
595
- }
596
- if (!optionsEnded && argument.startsWith("--")) {
597
- const equals = argument.indexOf("=");
598
- const option = equals < 0 ? argument.slice(2) : argument.slice(2, equals);
599
- const matches = ["-debug", "exponents", "help", "version"].filter((name) => name.startsWith(option));
600
- if (matches.length > 1) throw new FactorError(`option '${argument}' is ambiguous; possibilities: ${matches.map((name) => `'--${name}'`).join(" ")}`, true);
601
- const matched = matches[0];
602
- if (!matched) throw new FactorError(`unrecognized option '${argument}'`, true);
603
- if (equals >= 0) throw new FactorError(`option '--${matched}' doesn't allow an argument`, true);
604
- if (matched === "-debug") debug = true;
605
- else if (matched === "exponents") exponents = true;
606
- else {
607
- information = matched;
608
- break;
609
- }
610
- } else {
611
- if (!optionsEnded && argument.length > 1 && argument.startsWith("-")) {
612
- for (let index = 1; index < argument.length; index++) {
613
- if (argument[index] !== "h") throw new FactorError(`invalid option -- '${argument[index]}'`, true);
614
- exponents = true;
615
- }
616
- continue;
617
- }
618
- operands.push(argument);
619
- if (context.env.POSIXLY_CORRECT !== void 0) optionsEnded = true;
620
- }
621
- }
622
- if (information) {
623
- const message = information === "version" ? "factor (virtual-bash)\n" : `Usage: factor [NUMBER]...
624
- Print prime factors; with no numbers, read standard input.
625
- Options: --exponents (print repeated factors as powers) --help --version
626
- Maximum supported value: ${limits.maxValue}
627
- `;
628
- budget.outputRoom(message.length);
629
- await lifecycle.write(bytes(message));
630
- } else {
631
- const writer = new RecordWriter(lifecycle);
632
- const process = async (input) => {
633
- budget.number();
634
- budget.check(input.length, limits.maxTokenBytes, "token bytes");
635
- const value = await parseNumber(input, budget);
636
- if (typeof value === "string") {
637
- exitCode = 1;
638
- const suffix = value === "invalid" ? " is not a valid positive integer\n" : ` exceeds supported maximum ${limits.maxValue}
639
- `;
640
- await lifecycle.diagnostic(`factor: ${quote(input, budget, 8 + suffix.length)}${suffix}`);
641
- } else {
642
- if (debug) await lifecycle.diagnostic("[using single-precision arithmetic] ");
643
- await writer.append(await factorRecord(value, budget, exponents));
644
- }
645
- };
646
- if (operands.length) {
647
- for (const operand of operands) await process(operand);
648
- } else {
649
- const reader = new TokenReader(lifecycle);
650
- for (; ; ) {
651
- const token = await reader.next();
652
- if (token === void 0) break;
653
- try {
654
- await process(token);
655
- } finally {
656
- budget.retain(-token.length * 4);
657
- }
658
- }
659
- }
660
- await writer.finish();
661
- }
662
- } catch (error) {
663
- primary = { reason: error };
664
- if (error instanceof FactorError && !output.signal.aborted && lifecycle) {
665
- exitCode = 1;
666
- try {
667
- await lifecycle.diagnostic(`factor: ${error.message}
668
- ${error.usage ? "Try 'factor --help' for more information.\n" : ""}`);
669
- } catch (reporting) {
670
- failure = { reason: new AggregateError([error, reporting], "factor failure reporting failed") };
671
- }
672
- } else failure = { reason: error };
673
- }
674
- try {
675
- await output.close();
676
- } catch (cleanup) {
677
- const previous = failure ?? primary;
678
- failure = { reason: previous ? new AggregateError([previous.reason, cleanup], "factor execution and cleanup failed") : cleanup };
679
- }
680
- context.signal.throwIfAborted();
681
- output.signal.throwIfAborted();
682
- if (failure) throw failure.reason;
683
- return { exitCode };
684
- } };
685
- }
686
-
687
- // packages/safe-bash-command-factor/src/index.ts
688
- function createFactorCommand2(options = {}) {
689
- const def = createFactorCommand(options);
690
- builtInDirectContextExecutors.add(def.execute);
691
- return def;
692
- }
693
- function createFactorCommands(options = {}) {
694
- return [createFactorCommand2(options)];
695
- }
696
- function factorCommands(options = {}) {
697
- const commands = createFactorCommands(options);
698
- return { name: "factor-commands", setup(host) {
699
- for (const command of commands) host.commands.register(command, { replace: options.replace ?? false });
700
- } };
701
- }
702
-
703
- export {
704
- createFactorCommand2 as createFactorCommand,
705
- createFactorCommands,
706
- factorCommands
707
- };
708
- //# sourceMappingURL=chunk-LMUKYZIJ.js.map