poe-code 15.0.15 → 15.0.17
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.
- package/dist/metafile.json +1 -1
- package/package.json +13 -1
- package/packages/safe-bash/dist/chunks/chunk-A4UXQKWZ.js +2609 -0
- package/packages/safe-bash/dist/chunks/chunk-A4UXQKWZ.js.map +7 -0
- package/packages/safe-bash/dist/chunks/chunk-MVVKEMEG.js +1069 -0
- package/packages/safe-bash/dist/chunks/chunk-MVVKEMEG.js.map +7 -0
- package/packages/safe-bash/dist/commands/iconv/codec.d.ts +10 -0
- package/packages/safe-bash/dist/commands/iconv/codec.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/codec.js +200 -0
- package/packages/safe-bash/dist/commands/iconv/codec.js.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/command.d.ts +4 -0
- package/packages/safe-bash/dist/commands/iconv/command.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/command.js +116 -0
- package/packages/safe-bash/dist/commands/iconv/command.js.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/index.browser.js +15 -0
- package/packages/safe-bash/dist/commands/iconv/index.browser.js.map +7 -0
- package/packages/safe-bash/dist/commands/iconv/index.d.ts +7 -0
- package/packages/safe-bash/dist/commands/iconv/index.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/index.js +11 -0
- package/packages/safe-bash/dist/commands/iconv/index.js.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/internal.d.ts +54 -0
- package/packages/safe-bash/dist/commands/iconv/internal.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/internal.js +154 -0
- package/packages/safe-bash/dist/commands/iconv/internal.js.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/lifecycle.d.ts +24 -0
- package/packages/safe-bash/dist/commands/iconv/lifecycle.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/lifecycle.js +111 -0
- package/packages/safe-bash/dist/commands/iconv/lifecycle.js.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/options.d.ts +11 -0
- package/packages/safe-bash/dist/commands/iconv/options.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/options.js +82 -0
- package/packages/safe-bash/dist/commands/iconv/options.js.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/reader.d.ts +21 -0
- package/packages/safe-bash/dist/commands/iconv/reader.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/reader.js +192 -0
- package/packages/safe-bash/dist/commands/iconv/reader.js.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/transliterations.d.ts +2 -0
- package/packages/safe-bash/dist/commands/iconv/transliterations.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/iconv/transliterations.js +1785 -0
- package/packages/safe-bash/dist/commands/iconv/transliterations.js.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/command.d.ts +5 -0
- package/packages/safe-bash/dist/commands/line-endings/command.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/command.js +320 -0
- package/packages/safe-bash/dist/commands/line-endings/command.js.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/convert.d.ts +11 -0
- package/packages/safe-bash/dist/commands/line-endings/convert.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/convert.js +196 -0
- package/packages/safe-bash/dist/commands/line-endings/convert.js.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/index.browser.js +18 -0
- package/packages/safe-bash/dist/commands/line-endings/index.browser.js.map +7 -0
- package/packages/safe-bash/dist/commands/line-endings/index.d.ts +7 -0
- package/packages/safe-bash/dist/commands/line-endings/index.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/index.js +9 -0
- package/packages/safe-bash/dist/commands/line-endings/index.js.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/internal.d.ts +67 -0
- package/packages/safe-bash/dist/commands/line-endings/internal.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/internal.js +144 -0
- package/packages/safe-bash/dist/commands/line-endings/internal.js.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/io.d.ts +53 -0
- package/packages/safe-bash/dist/commands/line-endings/io.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/io.js +267 -0
- package/packages/safe-bash/dist/commands/line-endings/io.js.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/stage.d.ts +30 -0
- package/packages/safe-bash/dist/commands/line-endings/stage.d.ts.map +1 -0
- package/packages/safe-bash/dist/commands/line-endings/stage.js +159 -0
- package/packages/safe-bash/dist/commands/line-endings/stage.js.map +1 -0
- package/packages/safe-bash/dist/core.browser.js +23 -1
- package/packages/safe-bash/dist/core.browser.js.map +2 -2
- package/packages/safe-bash/dist/core.d.ts +2 -0
- package/packages/safe-bash/dist/core.d.ts.map +1 -1
- package/packages/safe-bash/dist/core.js +2 -0
- package/packages/safe-bash/dist/core.js.map +1 -1
- package/packages/safe-bash/dist/plugins/composition.d.ts +4 -0
- package/packages/safe-bash/dist/plugins/composition.d.ts.map +1 -1
- package/packages/safe-bash/dist/plugins/composition.js +5 -1
- package/packages/safe-bash/dist/plugins/composition.js.map +1 -1
|
@@ -0,0 +1,1069 @@
|
|
|
1
|
+
import {
|
|
2
|
+
writeFileOutput
|
|
3
|
+
} from "./chunk-AZNI263O.js";
|
|
4
|
+
import {
|
|
5
|
+
dirname,
|
|
6
|
+
resolvePath
|
|
7
|
+
} from "./chunk-DPJWEGIX.js";
|
|
8
|
+
import {
|
|
9
|
+
createOutputOperation
|
|
10
|
+
} from "./chunk-PZMJ4EC3.js";
|
|
11
|
+
import {
|
|
12
|
+
FsError,
|
|
13
|
+
PublicDiagnostic,
|
|
14
|
+
getCommandArguments,
|
|
15
|
+
outputFailure,
|
|
16
|
+
shellValueByteLength,
|
|
17
|
+
shellValueBytes,
|
|
18
|
+
yieldTurn
|
|
19
|
+
} from "./chunk-5VBLSPNM.js";
|
|
20
|
+
import {
|
|
21
|
+
init_platform
|
|
22
|
+
} from "./chunk-2KL6W6NB.js";
|
|
23
|
+
|
|
24
|
+
// packages/safe-bash/src/commands/line-endings/index.ts
|
|
25
|
+
init_platform();
|
|
26
|
+
|
|
27
|
+
// packages/safe-bash/src/commands/line-endings/command.ts
|
|
28
|
+
init_platform();
|
|
29
|
+
|
|
30
|
+
// packages/safe-bash/src/commands/line-endings/internal.ts
|
|
31
|
+
init_platform();
|
|
32
|
+
function settings(options) {
|
|
33
|
+
const limits = {
|
|
34
|
+
maxArguments: 1024,
|
|
35
|
+
maxArgumentBytes: 262144,
|
|
36
|
+
maxInputBytes: 16777216,
|
|
37
|
+
maxOutputBytes: 33554432,
|
|
38
|
+
maxBufferedBytes: 2097152,
|
|
39
|
+
maxDiagnosticBytes: 65536,
|
|
40
|
+
maxFiles: 128,
|
|
41
|
+
maxWork: 134217728,
|
|
42
|
+
maxEmptyChunks: 1024,
|
|
43
|
+
maxPathBytes: 4096,
|
|
44
|
+
maxDepth: 64,
|
|
45
|
+
maxTempAttempts: 128,
|
|
46
|
+
chunkSize: 16384,
|
|
47
|
+
...options.limits
|
|
48
|
+
};
|
|
49
|
+
for (const [name, value] of Object.entries(limits)) if (!Number.isSafeInteger(value) || value < 1) throw new RangeError(`Invalid line-ending limit: ${name}`);
|
|
50
|
+
return Object.freeze(limits);
|
|
51
|
+
}
|
|
52
|
+
var LineEndingError = class extends PublicDiagnostic {
|
|
53
|
+
constructor(message, status = 1) {
|
|
54
|
+
super(message);
|
|
55
|
+
this.status = status;
|
|
56
|
+
}
|
|
57
|
+
status;
|
|
58
|
+
};
|
|
59
|
+
var Budget = class {
|
|
60
|
+
constructor(context, limits, signal, caller, admission) {
|
|
61
|
+
this.context = context;
|
|
62
|
+
this.limits = limits;
|
|
63
|
+
this.signal = signal;
|
|
64
|
+
this.caller = caller;
|
|
65
|
+
this.admission = admission;
|
|
66
|
+
}
|
|
67
|
+
context;
|
|
68
|
+
limits;
|
|
69
|
+
signal;
|
|
70
|
+
caller;
|
|
71
|
+
admission;
|
|
72
|
+
work = 0;
|
|
73
|
+
quantum = 0;
|
|
74
|
+
memory = 0;
|
|
75
|
+
input = 0;
|
|
76
|
+
output = 0;
|
|
77
|
+
diagnostics = 0;
|
|
78
|
+
files = 0;
|
|
79
|
+
assertOpen() {
|
|
80
|
+
this.signal.throwIfAborted();
|
|
81
|
+
if (this.admission.closed) throw new LineEndingError("command is closed");
|
|
82
|
+
}
|
|
83
|
+
check(value, maximum, label) {
|
|
84
|
+
if (!Number.isSafeInteger(value) || value < 0 || value > maximum) throw new LineEndingError(`${label} limit exceeded`);
|
|
85
|
+
}
|
|
86
|
+
retain(amount) {
|
|
87
|
+
this.check(this.memory + amount, this.limits.maxBufferedBytes, "buffered bytes");
|
|
88
|
+
this.memory += amount;
|
|
89
|
+
}
|
|
90
|
+
incoming(amount) {
|
|
91
|
+
this.check(this.input + amount, this.limits.maxInputBytes, "input bytes");
|
|
92
|
+
this.input += amount;
|
|
93
|
+
}
|
|
94
|
+
emitted(amount, diagnostic = false) {
|
|
95
|
+
if (diagnostic) {
|
|
96
|
+
this.check(this.diagnostics + amount, this.limits.maxDiagnosticBytes, "diagnostic bytes");
|
|
97
|
+
this.diagnostics += amount;
|
|
98
|
+
} else {
|
|
99
|
+
this.check(this.output + amount, this.limits.maxOutputBytes, "output bytes");
|
|
100
|
+
this.output += amount;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
file() {
|
|
104
|
+
this.check(++this.files, this.limits.maxFiles, "file count");
|
|
105
|
+
}
|
|
106
|
+
async step(amount = 1) {
|
|
107
|
+
this.assertOpen();
|
|
108
|
+
this.check(this.work + amount, this.limits.maxWork, "work");
|
|
109
|
+
this.work += amount;
|
|
110
|
+
this.quantum += amount;
|
|
111
|
+
if (this.quantum >= 1024) {
|
|
112
|
+
this.quantum = 0;
|
|
113
|
+
await yieldTurn(this.caller);
|
|
114
|
+
this.assertOpen();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async arguments() {
|
|
118
|
+
const args = this.context.args;
|
|
119
|
+
this.assertOpen();
|
|
120
|
+
const count = args.length;
|
|
121
|
+
this.assertOpen();
|
|
122
|
+
this.check(count, this.limits.maxArguments, "argument count");
|
|
123
|
+
const argumentValues = this.context.argumentValues;
|
|
124
|
+
this.assertOpen();
|
|
125
|
+
const carrier = argumentValues === void 0 ? void 0 : getCommandArguments({ args, argumentValues });
|
|
126
|
+
const snapshot = [];
|
|
127
|
+
this.retain(count * 64);
|
|
128
|
+
let total = 0;
|
|
129
|
+
for (let index = 0; index < count; index++) {
|
|
130
|
+
const argument = args[index];
|
|
131
|
+
this.assertOpen();
|
|
132
|
+
if (typeof argument !== "string") throw new LineEndingError("arguments must be strings");
|
|
133
|
+
this.check(argument.length, this.limits.maxPathBytes, "argument bytes");
|
|
134
|
+
total += argument.length;
|
|
135
|
+
this.check(total, this.limits.maxArgumentBytes, "argument bytes");
|
|
136
|
+
this.retain(argument.length * 12);
|
|
137
|
+
snapshot.push(argument);
|
|
138
|
+
}
|
|
139
|
+
for (const argument of snapshot) {
|
|
140
|
+
for (let index = 0; index < argument.length; index++) {
|
|
141
|
+
await this.step();
|
|
142
|
+
const unit = argument.charCodeAt(index);
|
|
143
|
+
if (unit >= 55296 && unit <= 56319) {
|
|
144
|
+
const next = argument.charCodeAt(++index);
|
|
145
|
+
if (!(next >= 56320 && next <= 57343)) throw new LineEndingError("arguments must be valid UTF-8 paths");
|
|
146
|
+
} else if (unit >= 56320 && unit <= 57343) throw new LineEndingError("arguments must be valid UTF-8 paths");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const values = (carrier ?? getCommandArguments({ args: snapshot })).values;
|
|
150
|
+
total = 0;
|
|
151
|
+
for (const value of values) {
|
|
152
|
+
const length = shellValueByteLength(value);
|
|
153
|
+
this.check(length, this.limits.maxPathBytes, "argument bytes");
|
|
154
|
+
total += length;
|
|
155
|
+
this.check(total, this.limits.maxArgumentBytes, "argument bytes");
|
|
156
|
+
}
|
|
157
|
+
const result = [];
|
|
158
|
+
for (const value of values) {
|
|
159
|
+
await this.step(shellValueByteLength(value));
|
|
160
|
+
let text;
|
|
161
|
+
try {
|
|
162
|
+
text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(shellValueBytes(value));
|
|
163
|
+
} catch {
|
|
164
|
+
throw new LineEndingError("arguments must be valid UTF-8 paths");
|
|
165
|
+
}
|
|
166
|
+
if (text.includes("\0")) throw new LineEndingError("NUL is not supported in paths");
|
|
167
|
+
result.push(text);
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function sameIdentity(first, second) {
|
|
173
|
+
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;
|
|
174
|
+
}
|
|
175
|
+
function missing(error) {
|
|
176
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// packages/safe-bash/src/commands/line-endings/io.ts
|
|
180
|
+
init_platform();
|
|
181
|
+
var typedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
|
|
182
|
+
var viewBuffer = Object.getOwnPropertyDescriptor(typedArrayPrototype, "buffer").get;
|
|
183
|
+
var viewOffset = Object.getOwnPropertyDescriptor(typedArrayPrototype, "byteOffset").get;
|
|
184
|
+
var viewLength = Object.getOwnPropertyDescriptor(typedArrayPrototype, "byteLength").get;
|
|
185
|
+
var Lifecycle = class {
|
|
186
|
+
constructor(budget, output, captured, caller, admission) {
|
|
187
|
+
this.budget = budget;
|
|
188
|
+
this.captured = captured;
|
|
189
|
+
this.caller = caller;
|
|
190
|
+
this.admission = admission;
|
|
191
|
+
output.registerCleanup(() => this.close());
|
|
192
|
+
}
|
|
193
|
+
budget;
|
|
194
|
+
captured;
|
|
195
|
+
caller;
|
|
196
|
+
admission;
|
|
197
|
+
pending = /* @__PURE__ */ new Set();
|
|
198
|
+
cleanups = [];
|
|
199
|
+
closing;
|
|
200
|
+
diagnosticDestination;
|
|
201
|
+
diagnosticCancellation;
|
|
202
|
+
assertOpen() {
|
|
203
|
+
this.budget.signal.throwIfAborted();
|
|
204
|
+
if (this.closing || this.admission.closed) throw new LineEndingError("command is closed");
|
|
205
|
+
}
|
|
206
|
+
assertDiagnosticOpen() {
|
|
207
|
+
this.caller.throwIfAborted();
|
|
208
|
+
const consumer = this.diagnosticDestination?.consumer;
|
|
209
|
+
if (consumer?.aborted) {
|
|
210
|
+
this.diagnosticCancellation = { reason: consumer.reason };
|
|
211
|
+
consumer.throwIfAborted();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
cleanup(action) {
|
|
215
|
+
this.assertOpen();
|
|
216
|
+
this.cleanups.push(action);
|
|
217
|
+
}
|
|
218
|
+
async operation(action) {
|
|
219
|
+
this.assertOpen();
|
|
220
|
+
const pending = Promise.resolve().then(async () => {
|
|
221
|
+
await this.budget.step();
|
|
222
|
+
this.assertOpen();
|
|
223
|
+
return action();
|
|
224
|
+
});
|
|
225
|
+
this.pending.add(pending);
|
|
226
|
+
try {
|
|
227
|
+
const value = await pending;
|
|
228
|
+
this.assertOpen();
|
|
229
|
+
return value;
|
|
230
|
+
} finally {
|
|
231
|
+
this.pending.delete(pending);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
close() {
|
|
235
|
+
return this.closing ??= Promise.resolve().then(async () => {
|
|
236
|
+
while (this.pending.size) await Promise.allSettled([...this.pending]);
|
|
237
|
+
const failures = [];
|
|
238
|
+
for (const close of this.cleanups) {
|
|
239
|
+
try {
|
|
240
|
+
await close();
|
|
241
|
+
} catch (error) {
|
|
242
|
+
failures.push(error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (failures.length === 1) throw failures[0];
|
|
246
|
+
if (failures.length > 1) throw new AggregateError(failures, "line-ending cleanup failed");
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
async stdout(bytes) {
|
|
250
|
+
await this.operation(async () => {
|
|
251
|
+
const write = this.captured.write;
|
|
252
|
+
this.assertOpen();
|
|
253
|
+
await Reflect.apply(write, this.captured, [bytes]);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
async diagnostic(text) {
|
|
257
|
+
this.assertDiagnosticOpen();
|
|
258
|
+
if (this.closing || this.admission.closed) throw new LineEndingError("command is closed");
|
|
259
|
+
this.budget.check(text.length * 3, this.budget.limits.maxDiagnosticBytes, "diagnostic buffer bytes");
|
|
260
|
+
const bytes = new TextEncoder().encode(text);
|
|
261
|
+
this.budget.emitted(bytes.length, true);
|
|
262
|
+
const pending = Promise.resolve().then(async () => {
|
|
263
|
+
this.assertDiagnosticOpen();
|
|
264
|
+
if (!this.diagnosticDestination) {
|
|
265
|
+
const sink = this.budget.context.stderr;
|
|
266
|
+
this.assertDiagnosticOpen();
|
|
267
|
+
const capability = sink.ownedOutput;
|
|
268
|
+
this.assertDiagnosticOpen();
|
|
269
|
+
const consumer = capability?.consumerClosed;
|
|
270
|
+
this.diagnosticDestination = { destination: capability ?? sink, consumer };
|
|
271
|
+
}
|
|
272
|
+
this.assertDiagnosticOpen();
|
|
273
|
+
const destination = this.diagnosticDestination.destination;
|
|
274
|
+
const write = destination.write;
|
|
275
|
+
this.assertDiagnosticOpen();
|
|
276
|
+
await Reflect.apply(write, destination, [bytes]);
|
|
277
|
+
});
|
|
278
|
+
this.pending.add(pending);
|
|
279
|
+
try {
|
|
280
|
+
await pending;
|
|
281
|
+
this.assertDiagnosticOpen();
|
|
282
|
+
} finally {
|
|
283
|
+
this.pending.delete(pending);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
var Reader = class {
|
|
288
|
+
constructor(life) {
|
|
289
|
+
this.life = life;
|
|
290
|
+
life.cleanup(() => this.close());
|
|
291
|
+
}
|
|
292
|
+
life;
|
|
293
|
+
iterator;
|
|
294
|
+
chunk = new Uint8Array();
|
|
295
|
+
offset = 0;
|
|
296
|
+
ended = false;
|
|
297
|
+
empties = 0;
|
|
298
|
+
snapshotBytes = 0;
|
|
299
|
+
closing;
|
|
300
|
+
close() {
|
|
301
|
+
return this.closing ??= (async () => {
|
|
302
|
+
try {
|
|
303
|
+
if (!this.ended && this.iterator) {
|
|
304
|
+
const iterator = this.iterator;
|
|
305
|
+
const close = iterator.return;
|
|
306
|
+
if (close) await Reflect.apply(close, iterator, []);
|
|
307
|
+
}
|
|
308
|
+
} finally {
|
|
309
|
+
this.iterator = void 0;
|
|
310
|
+
this.life.budget.retain(-this.chunk.length - this.snapshotBytes);
|
|
311
|
+
this.chunk = new Uint8Array();
|
|
312
|
+
this.snapshotBytes = 0;
|
|
313
|
+
}
|
|
314
|
+
})();
|
|
315
|
+
}
|
|
316
|
+
async open(path, stat) {
|
|
317
|
+
const { context, signal, limits } = this.life.budget;
|
|
318
|
+
await this.life.operation(async () => {
|
|
319
|
+
let source;
|
|
320
|
+
let acquisitionAborted = false;
|
|
321
|
+
if (path === void 0) source = context.stdin;
|
|
322
|
+
else {
|
|
323
|
+
const fs = context.fs;
|
|
324
|
+
const capabilitiesFor = fs.capabilitiesFor;
|
|
325
|
+
this.life.assertOpen();
|
|
326
|
+
const selected = capabilitiesFor ? await Reflect.apply(capabilitiesFor, fs, [path, { signal }]) : void 0;
|
|
327
|
+
this.life.assertOpen();
|
|
328
|
+
const capabilities = selected ?? fs.capabilities;
|
|
329
|
+
this.life.assertOpen();
|
|
330
|
+
const readStream = fs.readStream;
|
|
331
|
+
this.life.assertOpen();
|
|
332
|
+
const streaming = capabilities.streamingRead !== false;
|
|
333
|
+
this.life.assertOpen();
|
|
334
|
+
if (readStream && streaming) {
|
|
335
|
+
source = Reflect.apply(readStream, fs, [path, { signal, chunkSize: limits.chunkSize }]);
|
|
336
|
+
acquisitionAborted = signal.aborted || this.life.admission.closed;
|
|
337
|
+
} else {
|
|
338
|
+
const maximum = Math.min(limits.maxInputBytes, Math.floor(limits.maxBufferedBytes / 3));
|
|
339
|
+
const size = stat.size;
|
|
340
|
+
this.life.assertOpen();
|
|
341
|
+
this.life.budget.check(size, maximum, "buffered input bytes");
|
|
342
|
+
this.life.budget.retain(maximum);
|
|
343
|
+
this.snapshotBytes = maximum;
|
|
344
|
+
const readFile = fs.readFile;
|
|
345
|
+
this.life.assertOpen();
|
|
346
|
+
const bytes = await Reflect.apply(readFile, fs, [path, { signal, maxBytes: maximum }]);
|
|
347
|
+
this.life.assertOpen();
|
|
348
|
+
if (!(bytes instanceof Uint8Array)) throw new TypeError("line-ending input requires bytes");
|
|
349
|
+
this.life.budget.check(viewLength.call(bytes), maximum, "buffered input bytes");
|
|
350
|
+
source = { [Symbol.asyncIterator]: (async function* () {
|
|
351
|
+
try {
|
|
352
|
+
yield bytes;
|
|
353
|
+
} finally {
|
|
354
|
+
this.life.budget.retain(-this.snapshotBytes);
|
|
355
|
+
this.snapshotBytes = 0;
|
|
356
|
+
}
|
|
357
|
+
}).bind(this) };
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (!acquisitionAborted) this.life.assertOpen();
|
|
361
|
+
const factory = source[Symbol.asyncIterator];
|
|
362
|
+
if (!acquisitionAborted) this.life.assertOpen();
|
|
363
|
+
this.iterator = Reflect.apply(factory, source, []);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
async get() {
|
|
367
|
+
await this.life.budget.step();
|
|
368
|
+
this.life.assertOpen();
|
|
369
|
+
while (this.offset === this.chunk.length) {
|
|
370
|
+
if (this.ended) return -1;
|
|
371
|
+
const next = await this.life.operation(() => {
|
|
372
|
+
const iterator = this.iterator;
|
|
373
|
+
const advance = iterator.next;
|
|
374
|
+
this.life.assertOpen();
|
|
375
|
+
return Reflect.apply(advance, iterator, []);
|
|
376
|
+
});
|
|
377
|
+
this.life.budget.retain(-this.chunk.length);
|
|
378
|
+
this.chunk = new Uint8Array();
|
|
379
|
+
this.offset = 0;
|
|
380
|
+
const done = next.done;
|
|
381
|
+
this.life.assertOpen();
|
|
382
|
+
if (done) {
|
|
383
|
+
this.ended = true;
|
|
384
|
+
this.iterator = void 0;
|
|
385
|
+
return -1;
|
|
386
|
+
}
|
|
387
|
+
const value = next.value;
|
|
388
|
+
this.life.assertOpen();
|
|
389
|
+
if (!(value instanceof Uint8Array)) throw new TypeError("line-ending input requires bytes");
|
|
390
|
+
const buffer = viewBuffer.call(value);
|
|
391
|
+
const offset = viewOffset.call(value);
|
|
392
|
+
const length = viewLength.call(value);
|
|
393
|
+
this.life.budget.incoming(length);
|
|
394
|
+
this.life.budget.retain(length);
|
|
395
|
+
try {
|
|
396
|
+
if (viewOffset.call(value) !== offset || viewLength.call(value) < length) throw new TypeError("line-ending input extent changed during admission");
|
|
397
|
+
this.chunk = new Uint8Array(new Uint8Array(buffer, offset, length));
|
|
398
|
+
} catch (error) {
|
|
399
|
+
this.life.budget.retain(-length);
|
|
400
|
+
throw error;
|
|
401
|
+
}
|
|
402
|
+
if (!this.chunk.length) this.life.budget.check(++this.empties, this.life.budget.limits.maxEmptyChunks, "empty input chunks");
|
|
403
|
+
}
|
|
404
|
+
return this.chunk[this.offset++];
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
var Writer = class {
|
|
408
|
+
constructor(life, publish) {
|
|
409
|
+
this.life = life;
|
|
410
|
+
this.publish = publish;
|
|
411
|
+
const size = Math.min(life.budget.limits.chunkSize, life.budget.limits.maxBufferedBytes);
|
|
412
|
+
life.budget.retain(size);
|
|
413
|
+
this.buffer = new Uint8Array(size);
|
|
414
|
+
life.cleanup(async () => {
|
|
415
|
+
this.release();
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
life;
|
|
419
|
+
publish;
|
|
420
|
+
buffer;
|
|
421
|
+
used = 0;
|
|
422
|
+
release() {
|
|
423
|
+
this.life.budget.retain(-this.buffer.length);
|
|
424
|
+
this.buffer = new Uint8Array();
|
|
425
|
+
this.used = 0;
|
|
426
|
+
}
|
|
427
|
+
async put(byte) {
|
|
428
|
+
await this.life.budget.step();
|
|
429
|
+
this.life.assertOpen();
|
|
430
|
+
this.life.budget.emitted(1);
|
|
431
|
+
this.buffer[this.used++] = byte;
|
|
432
|
+
if (this.used === this.buffer.length) await this.flush();
|
|
433
|
+
}
|
|
434
|
+
async flush() {
|
|
435
|
+
this.life.assertOpen();
|
|
436
|
+
if (this.used) {
|
|
437
|
+
await this.publish(this.buffer.subarray(0, this.used));
|
|
438
|
+
this.used = 0;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
// packages/safe-bash/src/commands/line-endings/stage.ts
|
|
444
|
+
init_platform();
|
|
445
|
+
import { retainFileSystemCleanup } from "poe-code/safe-fs/core";
|
|
446
|
+
var Files = class {
|
|
447
|
+
constructor(life) {
|
|
448
|
+
this.life = life;
|
|
449
|
+
}
|
|
450
|
+
life;
|
|
451
|
+
async call(select, args) {
|
|
452
|
+
return await this.life.operation(async () => {
|
|
453
|
+
const fs = this.life.budget.context.fs;
|
|
454
|
+
this.life.assertOpen();
|
|
455
|
+
const method = select(fs);
|
|
456
|
+
this.life.assertOpen();
|
|
457
|
+
return await Reflect.apply(method, fs, args);
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
async stat(path) {
|
|
461
|
+
try {
|
|
462
|
+
return await this.call((fs) => fs.lstat, [path, { signal: this.life.budget.signal }]);
|
|
463
|
+
} catch (error) {
|
|
464
|
+
this.life.assertOpen();
|
|
465
|
+
if (missing(error)) return void 0;
|
|
466
|
+
throw error;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
path(name) {
|
|
470
|
+
const path = resolvePath(this.life.budget.context.cwd, name);
|
|
471
|
+
this.life.budget.check(path.length * 3, this.life.budget.limits.maxPathBytes, "path bytes");
|
|
472
|
+
this.life.budget.check(path.split("/").length - 1, this.life.budget.limits.maxDepth, "path depth");
|
|
473
|
+
return path;
|
|
474
|
+
}
|
|
475
|
+
async parents(path) {
|
|
476
|
+
const parents = [];
|
|
477
|
+
let current = "/";
|
|
478
|
+
for (const component of ["", ...dirname(path).split("/").filter(Boolean)]) {
|
|
479
|
+
if (component) current = resolvePath(current, component);
|
|
480
|
+
const stat = await this.stat(current);
|
|
481
|
+
if (!stat) throw new FsError("ENOENT", { path: current });
|
|
482
|
+
if (stat.type !== "directory" || !sameIdentity(stat, stat)) throw new LineEndingError("output requires stable non-symlink directory identities");
|
|
483
|
+
parents.push({ path: current, stat });
|
|
484
|
+
}
|
|
485
|
+
return parents;
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
var Stage = class {
|
|
489
|
+
constructor(files, path, expected) {
|
|
490
|
+
this.files = files;
|
|
491
|
+
this.path = path;
|
|
492
|
+
this.expected = expected;
|
|
493
|
+
const { life } = files;
|
|
494
|
+
life.assertOpen();
|
|
495
|
+
this.cleanup = retainFileSystemCleanup(life.budget.context.fs, async (view) => {
|
|
496
|
+
if (!this.created || !this.identity) return;
|
|
497
|
+
try {
|
|
498
|
+
for (const parent of this.parents) {
|
|
499
|
+
const stat2 = await view.lstat(parent.path);
|
|
500
|
+
if (stat2.type !== "directory" || !sameIdentity(stat2, parent.stat)) return;
|
|
501
|
+
}
|
|
502
|
+
const stat = await view.lstat(this.temporary);
|
|
503
|
+
if (stat.type === "file" && sameIdentity(stat, this.identity)) await view.rm(this.temporary);
|
|
504
|
+
} catch (error) {
|
|
505
|
+
if (!missing(error)) throw error;
|
|
506
|
+
}
|
|
507
|
+
}, { maxOperations: Math.min(4096, life.budget.limits.maxDepth + 4) });
|
|
508
|
+
life.cleanup(this.cleanup);
|
|
509
|
+
}
|
|
510
|
+
files;
|
|
511
|
+
path;
|
|
512
|
+
expected;
|
|
513
|
+
temporary = "";
|
|
514
|
+
identity;
|
|
515
|
+
created = false;
|
|
516
|
+
parents = [];
|
|
517
|
+
cleanup;
|
|
518
|
+
async open() {
|
|
519
|
+
const { life } = this.files;
|
|
520
|
+
const { signal, limits } = life.budget;
|
|
521
|
+
const capabilities = await life.operation(async () => {
|
|
522
|
+
const fs = life.budget.context.fs;
|
|
523
|
+
const query = fs.capabilitiesFor;
|
|
524
|
+
life.assertOpen();
|
|
525
|
+
const result = query ? await Reflect.apply(query, fs, [this.path, { signal, create: true }]) : fs.capabilities;
|
|
526
|
+
life.assertOpen();
|
|
527
|
+
return result;
|
|
528
|
+
});
|
|
529
|
+
if (capabilities.atomicRename !== true || capabilities.exclusiveCreate !== true || capabilities.permissions !== true || capabilities.append === false || capabilities.remove === false) {
|
|
530
|
+
throw new LineEndingError("file conversion requires atomic rename, exclusive create, permissions, append and cleanup capabilities");
|
|
531
|
+
}
|
|
532
|
+
this.parents = await this.files.parents(this.path);
|
|
533
|
+
for (let attempt = 0; attempt < limits.maxTempAttempts; attempt++) {
|
|
534
|
+
this.temporary = resolvePath(dirname(this.path), `.line-ending-${attempt + 1}`);
|
|
535
|
+
this.files.path(this.temporary);
|
|
536
|
+
if (this.temporary === this.path || await this.files.stat(this.temporary)) continue;
|
|
537
|
+
try {
|
|
538
|
+
await this.files.call((fs) => fs.writeFile, [this.temporary, new Uint8Array(), { signal, flag: "wx", mode: 384 }]);
|
|
539
|
+
} catch (error) {
|
|
540
|
+
life.assertOpen();
|
|
541
|
+
if (typeof error === "object" && error !== null && "code" in error && error.code === "EEXIST") continue;
|
|
542
|
+
throw error;
|
|
543
|
+
}
|
|
544
|
+
this.created = true;
|
|
545
|
+
this.identity = await this.files.stat(this.temporary);
|
|
546
|
+
if (!this.identity || this.identity.type !== "file" || !sameIdentity(this.identity, this.identity)) throw new LineEndingError("temporary file identity unavailable; unowned path is not removed");
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
throw new LineEndingError("temporary file attempts limit exceeded");
|
|
550
|
+
}
|
|
551
|
+
async checked() {
|
|
552
|
+
for (const parent of this.parents) {
|
|
553
|
+
const stat2 = await this.files.stat(parent.path);
|
|
554
|
+
if (!stat2 || stat2.type !== "directory" || !sameIdentity(stat2, parent.stat)) throw new LineEndingError("output parent changed before mutation");
|
|
555
|
+
}
|
|
556
|
+
const stat = await this.files.stat(this.temporary);
|
|
557
|
+
if (!this.identity || !stat || stat.type !== "file" || !sameIdentity(stat, this.identity)) throw new LineEndingError("temporary file changed before mutation");
|
|
558
|
+
}
|
|
559
|
+
async append(bytes) {
|
|
560
|
+
await this.checked();
|
|
561
|
+
const { context, signal } = this.files.life.budget;
|
|
562
|
+
await this.files.life.operation(() => writeFileOutput(
|
|
563
|
+
{ signal, ...context.registerCleanup ? { registerCleanup: context.registerCleanup } : {} },
|
|
564
|
+
bytes,
|
|
565
|
+
async (chunk) => {
|
|
566
|
+
await this.files.call((fs) => fs.appendFile, [this.temporary, chunk, { signal }]);
|
|
567
|
+
}
|
|
568
|
+
));
|
|
569
|
+
}
|
|
570
|
+
async publish(input, options) {
|
|
571
|
+
const { signal } = this.files.life.budget;
|
|
572
|
+
await this.checked();
|
|
573
|
+
if (!options.newFile && (input.uid !== void 0 && input.uid !== this.identity.uid || input.gid !== void 0 && input.gid !== this.identity.gid)) {
|
|
574
|
+
throw new LineEndingError("filesystem cannot preserve input user/group ownership");
|
|
575
|
+
}
|
|
576
|
+
await this.files.call((fs) => fs.chmod, [this.temporary, input.mode & 511 & (options.newFile ? ~18 : 511), { signal }]);
|
|
577
|
+
if (options.keepDate) {
|
|
578
|
+
await this.checked();
|
|
579
|
+
await this.files.call((fs) => fs.utimes, [this.temporary, Math.floor(input.atimeMs / 1e3) * 1e3, Math.floor(input.mtimeMs / 1e3) * 1e3, { signal }]);
|
|
580
|
+
}
|
|
581
|
+
await this.checked();
|
|
582
|
+
const current = await this.files.stat(this.path);
|
|
583
|
+
if (current ? !this.expected || current.type !== "file" || !sameIdentity(current, this.expected) : this.expected !== void 0) throw new LineEndingError("destination changed before publication");
|
|
584
|
+
await this.files.call((fs) => fs.rename, [this.temporary, this.path, { signal }]);
|
|
585
|
+
this.created = false;
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
// packages/safe-bash/src/commands/line-endings/convert.ts
|
|
590
|
+
init_platform();
|
|
591
|
+
async function convert(direction, reader, writer, life, options, state, name, stdio) {
|
|
592
|
+
const prefix = [];
|
|
593
|
+
let bom = "bytes";
|
|
594
|
+
const diagnostic = async (message) => {
|
|
595
|
+
await life.diagnostic(`${direction}: ${message}
|
|
596
|
+
`);
|
|
597
|
+
};
|
|
598
|
+
const first = await reader.get();
|
|
599
|
+
let bomError = false;
|
|
600
|
+
if (first !== -1) {
|
|
601
|
+
if (first !== 255 && first !== 254 && first !== 239 && first !== 132) prefix.push(first);
|
|
602
|
+
else {
|
|
603
|
+
const second = await reader.get();
|
|
604
|
+
if (second === -1) bomError = true;
|
|
605
|
+
else if (first === 255 && second === 254) bom = "le";
|
|
606
|
+
else if (first === 254 && second === 255) bom = "be";
|
|
607
|
+
else {
|
|
608
|
+
const third = await reader.get();
|
|
609
|
+
if (third === -1) bomError = true;
|
|
610
|
+
else if (first === 239 && second === 187 && third === 191) bom = "utf8";
|
|
611
|
+
else {
|
|
612
|
+
prefix.push(first, second, third);
|
|
613
|
+
if (first === 132 && second === 49 && third === 149) {
|
|
614
|
+
const fourth = await reader.get();
|
|
615
|
+
if (fourth === -1) bomError = true;
|
|
616
|
+
else if (fourth === 51) {
|
|
617
|
+
bom = "gb";
|
|
618
|
+
prefix.length = 0;
|
|
619
|
+
} else prefix.push(fourth);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (bomError) {
|
|
626
|
+
if (!options.quiet) await diagnostic("can not read from input file: Success");
|
|
627
|
+
return { kind: "bom-error", encoding: "" };
|
|
628
|
+
}
|
|
629
|
+
if (bom === "bytes") bom = options.assume;
|
|
630
|
+
const wide = bom === "le" || bom === "be";
|
|
631
|
+
const encoding = wide ? bom === "le" ? "UTF-16LE" : "UTF-16BE" : "";
|
|
632
|
+
const env = life.budget.context.env;
|
|
633
|
+
const locale = env.LC_ALL || env.LC_CTYPE || env.LANG || "C";
|
|
634
|
+
const utf8 = locale === "C.UTF-8" || locale === "C.utf8";
|
|
635
|
+
if (wide && !options.keepUtf16 && !utf8 && locale !== "C" && locale !== "POSIX") throw new LineEndingError("UTF-16 conversion supports only C/POSIX and C.UTF-8 locales");
|
|
636
|
+
if (options.addBom || options.keepBom && bom !== "bytes") {
|
|
637
|
+
const bytes = options.keepUtf16 && wide ? bom === "le" ? [255, 254] : [254, 255] : bom === "gb" ? [132, 49, 149, 51] : [239, 187, 191];
|
|
638
|
+
for (const byte2 of bytes) await writer.put(byte2);
|
|
639
|
+
}
|
|
640
|
+
let prefixOffset = 0;
|
|
641
|
+
const byte = async () => prefixOffset < prefix.length ? prefix[prefixOffset++] : await reader.get();
|
|
642
|
+
let pushed = -1;
|
|
643
|
+
const unit = async () => {
|
|
644
|
+
if (pushed !== -1) {
|
|
645
|
+
const value = pushed;
|
|
646
|
+
pushed = -1;
|
|
647
|
+
return value;
|
|
648
|
+
}
|
|
649
|
+
const lead = await byte();
|
|
650
|
+
if (lead === -1 || !wide) return lead;
|
|
651
|
+
const trail = await byte();
|
|
652
|
+
return trail === -1 ? -1 : bom === "le" ? lead + trail * 256 : lead * 256 + trail;
|
|
653
|
+
};
|
|
654
|
+
let line = 1;
|
|
655
|
+
const result = { kind: "ok" };
|
|
656
|
+
const put = async (value) => {
|
|
657
|
+
if (!wide) {
|
|
658
|
+
await writer.put(options.sevenBit && bom === "bytes" && value >= 128 ? 32 : value);
|
|
659
|
+
return true;
|
|
660
|
+
}
|
|
661
|
+
if (options.keepUtf16) {
|
|
662
|
+
await writer.put(bom === "le" ? value & 255 : value >> 8);
|
|
663
|
+
await writer.put(bom === "le" ? value >> 8 : value & 255);
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
if (state.high >= 55296 && state.high < 56320 && (value < 56320 || value >= 57344)) {
|
|
667
|
+
await diagnostic("error: Invalid surrogate pair. Missing low surrogate.");
|
|
668
|
+
result.kind = "unicode";
|
|
669
|
+
return false;
|
|
670
|
+
}
|
|
671
|
+
if (value >= 55296 && value < 56320) {
|
|
672
|
+
state.high = value;
|
|
673
|
+
return true;
|
|
674
|
+
}
|
|
675
|
+
if (value >= 56320 && value < 57344) {
|
|
676
|
+
if (!(state.high >= 55296 && state.high < 56320)) {
|
|
677
|
+
await diagnostic("error: Invalid surrogate pair. Missing high surrogate.");
|
|
678
|
+
result.kind = "unicode";
|
|
679
|
+
return false;
|
|
680
|
+
}
|
|
681
|
+
value = 65536 + (state.high & 1023) * 1024 + (value & 1023);
|
|
682
|
+
state.high = 1;
|
|
683
|
+
}
|
|
684
|
+
if (!utf8 && value > 127) {
|
|
685
|
+
if (!options.quiet) await diagnostic("Invalid or incomplete multibyte or wide character");
|
|
686
|
+
result.kind = "unicode";
|
|
687
|
+
return false;
|
|
688
|
+
}
|
|
689
|
+
if (value < 128) await writer.put(value);
|
|
690
|
+
else if (value < 2048) {
|
|
691
|
+
await writer.put(192 | value >> 6);
|
|
692
|
+
await writer.put(128 | value & 63);
|
|
693
|
+
} else if (value < 65536) {
|
|
694
|
+
await writer.put(224 | value >> 12);
|
|
695
|
+
await writer.put(128 | value >> 6 & 63);
|
|
696
|
+
await writer.put(128 | value & 63);
|
|
697
|
+
} else {
|
|
698
|
+
await writer.put(240 | value >> 18);
|
|
699
|
+
await writer.put(128 | value >> 12 & 63);
|
|
700
|
+
await writer.put(128 | value >> 6 & 63);
|
|
701
|
+
await writer.put(128 | value & 63);
|
|
702
|
+
}
|
|
703
|
+
return true;
|
|
704
|
+
};
|
|
705
|
+
let previous = 0;
|
|
706
|
+
for (; ; ) {
|
|
707
|
+
let current = await unit();
|
|
708
|
+
if (current === -1) break;
|
|
709
|
+
if (!options.force && current < 32 && current !== 9 && current !== 10 && current !== 12 && current !== 13) {
|
|
710
|
+
result.kind = "binary";
|
|
711
|
+
if (!options.quiet) await diagnostic(`Binary symbol 0x${current.toString(16).toUpperCase().padStart(wide ? 4 : 2, "0")} found at line ${line}`);
|
|
712
|
+
break;
|
|
713
|
+
}
|
|
714
|
+
if (direction === "dos2unix") {
|
|
715
|
+
if (current === 13) {
|
|
716
|
+
pushed = await unit();
|
|
717
|
+
if (pushed !== 10) {
|
|
718
|
+
if (!await put(13)) break;
|
|
719
|
+
} else if (options.newline && !await put(10)) break;
|
|
720
|
+
} else {
|
|
721
|
+
if (current === 10) line++;
|
|
722
|
+
if (!await put(current)) break;
|
|
723
|
+
}
|
|
724
|
+
} else {
|
|
725
|
+
if (current === 10) {
|
|
726
|
+
if (!await put(13)) break;
|
|
727
|
+
} else if (current === 13) {
|
|
728
|
+
current = await unit();
|
|
729
|
+
if (current === -1) current = 13;
|
|
730
|
+
else {
|
|
731
|
+
if (!await put(13)) break;
|
|
732
|
+
previous = 13;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (current === 10) line++;
|
|
736
|
+
if (!await put(current)) break;
|
|
737
|
+
if (options.newline && current === 10 && previous !== 13) {
|
|
738
|
+
if (!await put(13) || !await put(10)) break;
|
|
739
|
+
}
|
|
740
|
+
previous = current;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
if (stdio || result.kind === "ok") await writer.flush();
|
|
744
|
+
if (!options.quiet) {
|
|
745
|
+
if (result.kind === "binary") await diagnostic(`Skipping binary file ${name}`);
|
|
746
|
+
if (result.kind === "unicode") await diagnostic(`Skipping UTF-16 file ${name}, an UTF-16 conversion error occurred on line ${line}.`);
|
|
747
|
+
}
|
|
748
|
+
return { kind: result.kind, encoding };
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
// packages/safe-bash/src/commands/line-endings/command.ts
|
|
752
|
+
function definition(direction, options) {
|
|
753
|
+
const limits = settings(options);
|
|
754
|
+
return { name: direction, description: "Convert DOS and Unix line endings in bounded VFS files or streams", async execute(context) {
|
|
755
|
+
let life;
|
|
756
|
+
const caller = context.signal;
|
|
757
|
+
caller.throwIfAborted();
|
|
758
|
+
const stdout = context.stdout;
|
|
759
|
+
caller.throwIfAborted();
|
|
760
|
+
const capability = stdout.ownedOutput;
|
|
761
|
+
caller.throwIfAborted();
|
|
762
|
+
const consumer = capability?.consumerClosed;
|
|
763
|
+
caller.throwIfAborted();
|
|
764
|
+
consumer?.throwIfAborted();
|
|
765
|
+
const failureHandler = stdout[outputFailure];
|
|
766
|
+
caller.throwIfAborted();
|
|
767
|
+
consumer?.throwIfAborted();
|
|
768
|
+
const registerCleanup = context.registerCleanup;
|
|
769
|
+
caller.throwIfAborted();
|
|
770
|
+
consumer?.throwIfAborted();
|
|
771
|
+
const admission = { closed: false };
|
|
772
|
+
const destination = capability ?? stdout;
|
|
773
|
+
const write = async (bytes) => {
|
|
774
|
+
life.assertOpen();
|
|
775
|
+
const method = destination.write;
|
|
776
|
+
life.assertOpen();
|
|
777
|
+
await Reflect.apply(method, destination, [bytes]);
|
|
778
|
+
};
|
|
779
|
+
const captured = {
|
|
780
|
+
write,
|
|
781
|
+
...capability ? { ownedOutput: { consumerClosed: consumer, write } } : {},
|
|
782
|
+
...failureHandler ? { [outputFailure]: failureHandler } : {}
|
|
783
|
+
};
|
|
784
|
+
const output = createOutputOperation({ signal: caller, ...registerCleanup ? { registerCleanup(cleanup) {
|
|
785
|
+
caller.throwIfAborted();
|
|
786
|
+
Reflect.apply(registerCleanup, context, [() => {
|
|
787
|
+
admission.closed = true;
|
|
788
|
+
return cleanup();
|
|
789
|
+
}]);
|
|
790
|
+
} } : {} }, captured);
|
|
791
|
+
const budget = new Budget(context, limits, output.signal, caller, admission);
|
|
792
|
+
let failure;
|
|
793
|
+
let primary;
|
|
794
|
+
let status = 0;
|
|
795
|
+
try {
|
|
796
|
+
life = new Lifecycle(budget, output, captured, caller, admission);
|
|
797
|
+
const active = life;
|
|
798
|
+
const files = new Files(active);
|
|
799
|
+
const args = await budget.arguments();
|
|
800
|
+
const flags = { keepBom: direction === "unix2dos", addBom: false, keepUtf16: false, assume: "bytes", force: false, quiet: false, newline: false, sevenBit: false, keepDate: false, newFile: false };
|
|
801
|
+
const state = { high: 1 };
|
|
802
|
+
const diagnostic = async (text) => {
|
|
803
|
+
await active.diagnostic(`${direction}: ${text}
|
|
804
|
+
`);
|
|
805
|
+
};
|
|
806
|
+
const file = async (name, destination2) => {
|
|
807
|
+
budget.file();
|
|
808
|
+
const inputPath = files.path(name);
|
|
809
|
+
const outputPath = files.path(destination2);
|
|
810
|
+
const expected = await files.stat(outputPath);
|
|
811
|
+
if (expected?.type === "symlink") {
|
|
812
|
+
if (!flags.quiet) await diagnostic(flags.newFile ? `Skipping ${name}, output file ${destination2} is a symbolic link.` : `Skipping symbolic link ${name}.`);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
let input = await files.stat(inputPath);
|
|
816
|
+
if (!input || input.type !== "file" && input.type !== "symlink") {
|
|
817
|
+
if (!flags.quiet) {
|
|
818
|
+
if (!input) {
|
|
819
|
+
status = 2;
|
|
820
|
+
await diagnostic(`${name}: No such file or directory`);
|
|
821
|
+
}
|
|
822
|
+
await diagnostic(`Skipping ${name}, not a regular file.`);
|
|
823
|
+
}
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
let path = inputPath;
|
|
827
|
+
if (input.type === "symlink") {
|
|
828
|
+
path = await files.call((fs) => fs.realpath, [path, { signal: budget.signal }]);
|
|
829
|
+
path = files.path(path);
|
|
830
|
+
input = await files.stat(path);
|
|
831
|
+
if (!input || input.type !== "file") {
|
|
832
|
+
if (!flags.quiet) await diagnostic(`Skipping symbolic link ${name}, target is not a regular file.`);
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
if (!sameIdentity(input, input) || expected && (expected.type !== "file" || !sameIdentity(expected, expected))) throw new LineEndingError("file conversion requires stable regular-file identities");
|
|
837
|
+
const reader = new Reader(active);
|
|
838
|
+
const stage = new Stage(files, outputPath, expected);
|
|
839
|
+
const writer = new Writer(active, (bytes) => stage.append(bytes));
|
|
840
|
+
let result;
|
|
841
|
+
let failed;
|
|
842
|
+
try {
|
|
843
|
+
await reader.open(path, input);
|
|
844
|
+
let opened = false;
|
|
845
|
+
try {
|
|
846
|
+
await stage.open();
|
|
847
|
+
opened = true;
|
|
848
|
+
} catch (error) {
|
|
849
|
+
if (error instanceof FsError && error.code === "ENOENT") {
|
|
850
|
+
if (!flags.quiet) {
|
|
851
|
+
status = 2;
|
|
852
|
+
await diagnostic("Failed to open temporary output file: No such file or directory");
|
|
853
|
+
await diagnostic(`problems converting file ${name}${flags.newFile ? ` to file ${destination2}` : ""}`);
|
|
854
|
+
}
|
|
855
|
+
} else throw error;
|
|
856
|
+
}
|
|
857
|
+
if (opened && await files.stat(outputPath) !== void 0 && expected === void 0) throw new LineEndingError("destination appeared during acquisition");
|
|
858
|
+
if (opened) result = await convert(direction, reader, writer, active, flags, state, name, false);
|
|
859
|
+
await reader.close();
|
|
860
|
+
if (result?.kind === "ok") {
|
|
861
|
+
await stage.publish(input, flags);
|
|
862
|
+
if (!flags.quiet) {
|
|
863
|
+
const locale = context.env.LC_ALL || context.env.LC_CTYPE || context.env.LANG || "C";
|
|
864
|
+
const outFormat = flags.keepUtf16 ? result.encoding : locale === "C" || locale === "POSIX" ? "ANSI_X3.4-1968" : "UTF-8";
|
|
865
|
+
const encoding = result.encoding ? `${result.encoding} ` : "";
|
|
866
|
+
const target = result.encoding ? `${outFormat} ` : "";
|
|
867
|
+
await diagnostic(`converting ${encoding}file ${name} to ${flags.newFile ? `${target}file ${destination2} in ` : target}${direction === "dos2unix" ? "Unix" : "DOS"} format...`);
|
|
868
|
+
}
|
|
869
|
+
} else if (result?.kind === "unicode") status ||= 1;
|
|
870
|
+
else if (result?.kind === "bom-error" && !flags.quiet) await diagnostic(`problems converting file ${name}${flags.newFile ? ` to file ${destination2}` : ""}`);
|
|
871
|
+
} catch (error) {
|
|
872
|
+
failed = { reason: error };
|
|
873
|
+
}
|
|
874
|
+
try {
|
|
875
|
+
await reader.close();
|
|
876
|
+
await stage.cleanup();
|
|
877
|
+
} catch (error) {
|
|
878
|
+
throw failed ? new AggregateError([failed.reason, error], "line-ending conversion and cleanup failed") : error;
|
|
879
|
+
} finally {
|
|
880
|
+
writer.release();
|
|
881
|
+
}
|
|
882
|
+
if (failed) throw failed.reason;
|
|
883
|
+
};
|
|
884
|
+
let paired = true, processOptions = true, sawFile = false, stop = false;
|
|
885
|
+
let index = 0;
|
|
886
|
+
for (; index < args.length && !stop; index++) {
|
|
887
|
+
const argument = args[index];
|
|
888
|
+
if (processOptions && argument.startsWith("-")) {
|
|
889
|
+
switch (argument) {
|
|
890
|
+
case "--":
|
|
891
|
+
processOptions = false;
|
|
892
|
+
break;
|
|
893
|
+
case "-b":
|
|
894
|
+
case "--keep-bom":
|
|
895
|
+
flags.keepBom = true;
|
|
896
|
+
break;
|
|
897
|
+
case "-r":
|
|
898
|
+
case "--remove-bom":
|
|
899
|
+
flags.keepBom = false;
|
|
900
|
+
flags.addBom = false;
|
|
901
|
+
break;
|
|
902
|
+
case "-m":
|
|
903
|
+
case "--add-bom":
|
|
904
|
+
flags.addBom = true;
|
|
905
|
+
break;
|
|
906
|
+
case "-f":
|
|
907
|
+
case "--force":
|
|
908
|
+
flags.force = true;
|
|
909
|
+
break;
|
|
910
|
+
case "-s":
|
|
911
|
+
case "--safe":
|
|
912
|
+
flags.force = false;
|
|
913
|
+
break;
|
|
914
|
+
case "-q":
|
|
915
|
+
case "--quiet":
|
|
916
|
+
flags.quiet = true;
|
|
917
|
+
break;
|
|
918
|
+
case "-l":
|
|
919
|
+
case "--newline":
|
|
920
|
+
flags.newline = true;
|
|
921
|
+
break;
|
|
922
|
+
case "-k":
|
|
923
|
+
case "--keepdate":
|
|
924
|
+
flags.keepDate = true;
|
|
925
|
+
break;
|
|
926
|
+
case "-u":
|
|
927
|
+
case "--keep-utf16":
|
|
928
|
+
flags.keepUtf16 = true;
|
|
929
|
+
break;
|
|
930
|
+
case "-ul":
|
|
931
|
+
case "--assume-utf16le":
|
|
932
|
+
flags.assume = "le";
|
|
933
|
+
break;
|
|
934
|
+
case "-ub":
|
|
935
|
+
case "--assume-utf16be":
|
|
936
|
+
flags.assume = "be";
|
|
937
|
+
break;
|
|
938
|
+
case "-7":
|
|
939
|
+
flags.sevenBit = true;
|
|
940
|
+
flags.assume = "bytes";
|
|
941
|
+
break;
|
|
942
|
+
case "-ascii":
|
|
943
|
+
flags.assume = "bytes";
|
|
944
|
+
flags.sevenBit = false;
|
|
945
|
+
flags.keepUtf16 = false;
|
|
946
|
+
break;
|
|
947
|
+
case "-S":
|
|
948
|
+
case "--skip-symlink":
|
|
949
|
+
break;
|
|
950
|
+
case "-o":
|
|
951
|
+
case "--oldfile":
|
|
952
|
+
case "-n":
|
|
953
|
+
case "--newfile":
|
|
954
|
+
if (!paired) {
|
|
955
|
+
await diagnostic(`target of file ${args[index - 1]} not specified in new-file mode`);
|
|
956
|
+
status = 1;
|
|
957
|
+
sawFile = true;
|
|
958
|
+
stop = true;
|
|
959
|
+
}
|
|
960
|
+
flags.newFile = argument === "-n" || argument === "--newfile";
|
|
961
|
+
break;
|
|
962
|
+
case "-c":
|
|
963
|
+
case "--convmode": {
|
|
964
|
+
const mode = args[++index];
|
|
965
|
+
if (mode === void 0) {
|
|
966
|
+
await diagnostic(`option '${argument}' requires an argument`);
|
|
967
|
+
status = 1;
|
|
968
|
+
sawFile = true;
|
|
969
|
+
stop = true;
|
|
970
|
+
} else if (mode.toLowerCase() === "ascii") {
|
|
971
|
+
flags.sevenBit = false;
|
|
972
|
+
flags.assume = "bytes";
|
|
973
|
+
flags.keepUtf16 = false;
|
|
974
|
+
} else if (mode.toLowerCase() === "7bit") {
|
|
975
|
+
flags.sevenBit = true;
|
|
976
|
+
flags.assume = "bytes";
|
|
977
|
+
} else {
|
|
978
|
+
await diagnostic(`invalid ${mode} conversion mode specified`);
|
|
979
|
+
status = 1;
|
|
980
|
+
sawFile = true;
|
|
981
|
+
stop = true;
|
|
982
|
+
}
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
case "-h":
|
|
986
|
+
case "--help": {
|
|
987
|
+
const bytes = new TextEncoder().encode(`Usage: ${direction} [options] [file ...] [-n infile outfile ...]
|
|
988
|
+
Virtual byte/UTF-16 profile: -o -n -b -r -m -f -s -q -k -l -u -ul -ub -7 -ascii --
|
|
989
|
+
`);
|
|
990
|
+
budget.emitted(bytes.length);
|
|
991
|
+
await active.stdout(bytes);
|
|
992
|
+
stop = true;
|
|
993
|
+
sawFile = true;
|
|
994
|
+
break;
|
|
995
|
+
}
|
|
996
|
+
default:
|
|
997
|
+
throw new LineEndingError(`unsupported option in virtual profile: ${argument}`);
|
|
998
|
+
}
|
|
999
|
+
} else {
|
|
1000
|
+
sawFile = true;
|
|
1001
|
+
if (!flags.newFile) await file(argument, argument);
|
|
1002
|
+
else if (paired) paired = false;
|
|
1003
|
+
else {
|
|
1004
|
+
await file(args[index - 1], argument);
|
|
1005
|
+
paired = true;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
if (!sawFile) {
|
|
1010
|
+
const reader = new Reader(active);
|
|
1011
|
+
const writer = new Writer(active, (bytes) => active.stdout(bytes));
|
|
1012
|
+
await reader.open();
|
|
1013
|
+
const result = await convert(direction, reader, writer, active, flags, state, "stdin", true);
|
|
1014
|
+
if (result.kind === "binary" && !flags.quiet) status ||= 1;
|
|
1015
|
+
}
|
|
1016
|
+
if (!paired) {
|
|
1017
|
+
await diagnostic(`target of file ${args[Math.min(index - 1, args.length - 1)]} not specified in new-file mode`);
|
|
1018
|
+
status = 1;
|
|
1019
|
+
}
|
|
1020
|
+
} catch (error) {
|
|
1021
|
+
primary = { reason: error };
|
|
1022
|
+
if (error instanceof LineEndingError && life && !output.signal.aborted) {
|
|
1023
|
+
status = error.status;
|
|
1024
|
+
try {
|
|
1025
|
+
await life.diagnostic(`${direction}: ${error.message}
|
|
1026
|
+
`);
|
|
1027
|
+
} catch (reporting) {
|
|
1028
|
+
failure = { reason: new AggregateError([error, reporting], "line-ending failure reporting failed") };
|
|
1029
|
+
}
|
|
1030
|
+
} else failure = { reason: error };
|
|
1031
|
+
}
|
|
1032
|
+
try {
|
|
1033
|
+
await output.close();
|
|
1034
|
+
} catch (cleanup) {
|
|
1035
|
+
const previous = failure ?? primary;
|
|
1036
|
+
failure = { reason: previous ? new AggregateError([previous.reason, cleanup], "line-ending execution and cleanup failed") : cleanup };
|
|
1037
|
+
}
|
|
1038
|
+
caller.throwIfAborted();
|
|
1039
|
+
output.signal.throwIfAborted();
|
|
1040
|
+
if (life?.diagnosticCancellation) throw life.diagnosticCancellation.reason;
|
|
1041
|
+
if (failure) throw failure.reason;
|
|
1042
|
+
return { exitCode: status };
|
|
1043
|
+
} };
|
|
1044
|
+
}
|
|
1045
|
+
function createDos2unixCommand(options = {}) {
|
|
1046
|
+
return definition("dos2unix", options);
|
|
1047
|
+
}
|
|
1048
|
+
function createUnix2dosCommand(options = {}) {
|
|
1049
|
+
return definition("unix2dos", options);
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
// packages/safe-bash/src/commands/line-endings/index.ts
|
|
1053
|
+
function createLineEndingCommands(options = {}) {
|
|
1054
|
+
return [createDos2unixCommand(options), createUnix2dosCommand(options)];
|
|
1055
|
+
}
|
|
1056
|
+
function lineEndingCommands(options = {}) {
|
|
1057
|
+
const commands = createLineEndingCommands(options);
|
|
1058
|
+
return { name: "line-ending-commands", setup(host) {
|
|
1059
|
+
for (const command of commands) host.commands.register(command, { replace: options.replace ?? false });
|
|
1060
|
+
} };
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
export {
|
|
1064
|
+
createDos2unixCommand,
|
|
1065
|
+
createUnix2dosCommand,
|
|
1066
|
+
createLineEndingCommands,
|
|
1067
|
+
lineEndingCommands
|
|
1068
|
+
};
|
|
1069
|
+
//# sourceMappingURL=chunk-MVVKEMEG.js.map
|