@jsenv/core 40.3.3 → 40.5.0

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.
@@ -0,0 +1,1550 @@
1
+ import { createSupportsColor, isUnicodeSupported, emojiRegex, eastAsianWidth, clearTerminal, eraseLines } from "./jsenv_core_node_modules.js";
2
+ import { stripVTControlCharacters } from "node:util";
3
+ import { pathToFileURL } from "node:url";
4
+
5
+ const createCallbackListNotifiedOnce = () => {
6
+ let callbacks = [];
7
+ let status = "waiting";
8
+ let currentCallbackIndex = -1;
9
+
10
+ const callbackListOnce = {};
11
+
12
+ const add = (callback) => {
13
+ if (status !== "waiting") {
14
+ emitUnexpectedActionWarning({ action: "add", status });
15
+ return removeNoop;
16
+ }
17
+
18
+ if (typeof callback !== "function") {
19
+ throw new Error(`callback must be a function, got ${callback}`);
20
+ }
21
+
22
+ // don't register twice
23
+ const existingCallback = callbacks.find((callbackCandidate) => {
24
+ return callbackCandidate === callback;
25
+ });
26
+ if (existingCallback) {
27
+ emitCallbackDuplicationWarning();
28
+ return removeNoop;
29
+ }
30
+
31
+ callbacks.push(callback);
32
+ return () => {
33
+ if (status === "notified") {
34
+ // once called removing does nothing
35
+ // as the callbacks array is frozen to null
36
+ return;
37
+ }
38
+
39
+ const index = callbacks.indexOf(callback);
40
+ if (index === -1) {
41
+ return;
42
+ }
43
+
44
+ if (status === "looping") {
45
+ if (index <= currentCallbackIndex) {
46
+ // The callback was already called (or is the current callback)
47
+ // We don't want to mutate the callbacks array
48
+ // or it would alter the looping done in "call" and the next callback
49
+ // would be skipped
50
+ return;
51
+ }
52
+
53
+ // Callback is part of the next callback to call,
54
+ // we mutate the callbacks array to prevent this callback to be called
55
+ }
56
+
57
+ callbacks.splice(index, 1);
58
+ };
59
+ };
60
+
61
+ const notify = (param) => {
62
+ if (status !== "waiting") {
63
+ emitUnexpectedActionWarning({ action: "call", status });
64
+ return [];
65
+ }
66
+ status = "looping";
67
+ const values = callbacks.map((callback, index) => {
68
+ currentCallbackIndex = index;
69
+ return callback(param);
70
+ });
71
+ callbackListOnce.notified = true;
72
+ status = "notified";
73
+ // we reset callbacks to null after looping
74
+ // so that it's possible to remove during the loop
75
+ callbacks = null;
76
+ currentCallbackIndex = -1;
77
+
78
+ return values;
79
+ };
80
+
81
+ callbackListOnce.notified = false;
82
+ callbackListOnce.add = add;
83
+ callbackListOnce.notify = notify;
84
+
85
+ return callbackListOnce;
86
+ };
87
+
88
+ const emitUnexpectedActionWarning = ({ action, status }) => {
89
+ if (typeof process.emitWarning === "function") {
90
+ process.emitWarning(
91
+ `"${action}" should not happen when callback list is ${status}`,
92
+ {
93
+ CODE: "UNEXPECTED_ACTION_ON_CALLBACK_LIST",
94
+ detail: `Code is potentially executed when it should not`,
95
+ },
96
+ );
97
+ } else {
98
+ console.warn(
99
+ `"${action}" should not happen when callback list is ${status}`,
100
+ );
101
+ }
102
+ };
103
+
104
+ const emitCallbackDuplicationWarning = () => {
105
+ if (typeof process.emitWarning === "function") {
106
+ process.emitWarning(`Trying to add a callback already in the list`, {
107
+ CODE: "CALLBACK_DUPLICATION",
108
+ detail: `Code is potentially executed more than it should`,
109
+ });
110
+ } else {
111
+ console.warn(`Trying to add same callback twice`);
112
+ }
113
+ };
114
+
115
+ const removeNoop = () => {};
116
+
117
+ /*
118
+ * See callback_race.md
119
+ */
120
+
121
+ const raceCallbacks = (raceDescription, winnerCallback) => {
122
+ let cleanCallbacks = [];
123
+ let status = "racing";
124
+
125
+ const clean = () => {
126
+ cleanCallbacks.forEach((clean) => {
127
+ clean();
128
+ });
129
+ cleanCallbacks = null;
130
+ };
131
+
132
+ const cancel = () => {
133
+ if (status !== "racing") {
134
+ return;
135
+ }
136
+ status = "cancelled";
137
+ clean();
138
+ };
139
+
140
+ Object.keys(raceDescription).forEach((candidateName) => {
141
+ const register = raceDescription[candidateName];
142
+ const returnValue = register((data) => {
143
+ if (status !== "racing") {
144
+ return;
145
+ }
146
+ status = "done";
147
+ clean();
148
+ winnerCallback({
149
+ name: candidateName,
150
+ data,
151
+ });
152
+ });
153
+ if (typeof returnValue === "function") {
154
+ cleanCallbacks.push(returnValue);
155
+ }
156
+ });
157
+
158
+ return cancel;
159
+ };
160
+
161
+ /*
162
+ * https://github.com/whatwg/dom/issues/920
163
+ */
164
+
165
+
166
+ const Abort = {
167
+ isAbortError: (error) => {
168
+ return error && error.name === "AbortError";
169
+ },
170
+
171
+ startOperation: () => {
172
+ return createOperation();
173
+ },
174
+
175
+ throwIfAborted: (signal) => {
176
+ if (signal.aborted) {
177
+ const error = new Error(`The operation was aborted`);
178
+ error.name = "AbortError";
179
+ error.type = "aborted";
180
+ throw error;
181
+ }
182
+ },
183
+ };
184
+
185
+ const createOperation = () => {
186
+ const operationAbortController = new AbortController();
187
+ // const abortOperation = (value) => abortController.abort(value)
188
+ const operationSignal = operationAbortController.signal;
189
+
190
+ // abortCallbackList is used to ignore the max listeners warning from Node.js
191
+ // this warning is useful but becomes problematic when it's expect
192
+ // (a function doing 20 http call in parallel)
193
+ // To be 100% sure we don't have memory leak, only Abortable.asyncCallback
194
+ // uses abortCallbackList to know when something is aborted
195
+ const abortCallbackList = createCallbackListNotifiedOnce();
196
+ const endCallbackList = createCallbackListNotifiedOnce();
197
+
198
+ let isAbortAfterEnd = false;
199
+
200
+ operationSignal.onabort = () => {
201
+ operationSignal.onabort = null;
202
+
203
+ const allAbortCallbacksPromise = Promise.all(abortCallbackList.notify());
204
+ if (!isAbortAfterEnd) {
205
+ addEndCallback(async () => {
206
+ await allAbortCallbacksPromise;
207
+ });
208
+ }
209
+ };
210
+
211
+ const throwIfAborted = () => {
212
+ Abort.throwIfAborted(operationSignal);
213
+ };
214
+
215
+ // add a callback called on abort
216
+ // differences with signal.addEventListener('abort')
217
+ // - operation.end awaits the return value of this callback
218
+ // - It won't increase the count of listeners for "abort" that would
219
+ // trigger max listeners warning when count > 10
220
+ const addAbortCallback = (callback) => {
221
+ // It would be painful and not super redable to check if signal is aborted
222
+ // before deciding if it's an abort or end callback
223
+ // with pseudo-code below where we want to stop server either
224
+ // on abort or when ended because signal is aborted
225
+ // operation[operation.signal.aborted ? 'addAbortCallback': 'addEndCallback'](async () => {
226
+ // await server.stop()
227
+ // })
228
+ if (operationSignal.aborted) {
229
+ return addEndCallback(callback);
230
+ }
231
+ return abortCallbackList.add(callback);
232
+ };
233
+
234
+ const addEndCallback = (callback) => {
235
+ return endCallbackList.add(callback);
236
+ };
237
+
238
+ const end = async ({ abortAfterEnd = false } = {}) => {
239
+ await Promise.all(endCallbackList.notify());
240
+
241
+ // "abortAfterEnd" can be handy to ensure "abort" callbacks
242
+ // added with { once: true } are removed
243
+ // It might also help garbage collection because
244
+ // runtime implementing AbortSignal (Node.js, browsers) can consider abortSignal
245
+ // as settled and clean up things
246
+ if (abortAfterEnd) {
247
+ // because of operationSignal.onabort = null
248
+ // + abortCallbackList.clear() this won't re-call
249
+ // callbacks
250
+ if (!operationSignal.aborted) {
251
+ isAbortAfterEnd = true;
252
+ operationAbortController.abort();
253
+ }
254
+ }
255
+ };
256
+
257
+ const addAbortSignal = (
258
+ signal,
259
+ { onAbort = callbackNoop, onRemove = callbackNoop } = {},
260
+ ) => {
261
+ const applyAbortEffects = () => {
262
+ const onAbortCallback = onAbort;
263
+ onAbort = callbackNoop;
264
+ onAbortCallback();
265
+ };
266
+ const applyRemoveEffects = () => {
267
+ const onRemoveCallback = onRemove;
268
+ onRemove = callbackNoop;
269
+ onAbort = callbackNoop;
270
+ onRemoveCallback();
271
+ };
272
+
273
+ if (operationSignal.aborted) {
274
+ applyAbortEffects();
275
+ applyRemoveEffects();
276
+ return callbackNoop;
277
+ }
278
+
279
+ if (signal.aborted) {
280
+ operationAbortController.abort();
281
+ applyAbortEffects();
282
+ applyRemoveEffects();
283
+ return callbackNoop;
284
+ }
285
+
286
+ const cancelRace = raceCallbacks(
287
+ {
288
+ operation_abort: (cb) => {
289
+ return addAbortCallback(cb);
290
+ },
291
+ operation_end: (cb) => {
292
+ return addEndCallback(cb);
293
+ },
294
+ child_abort: (cb) => {
295
+ return addEventListener(signal, "abort", cb);
296
+ },
297
+ },
298
+ (winner) => {
299
+ const raceEffects = {
300
+ // Both "operation_abort" and "operation_end"
301
+ // means we don't care anymore if the child aborts.
302
+ // So we can:
303
+ // - remove "abort" event listener on child (done by raceCallback)
304
+ // - remove abort callback on operation (done by raceCallback)
305
+ // - remove end callback on operation (done by raceCallback)
306
+ // - call any custom cancel function
307
+ operation_abort: () => {
308
+ applyAbortEffects();
309
+ applyRemoveEffects();
310
+ },
311
+ operation_end: () => {
312
+ // Exists to
313
+ // - remove abort callback on operation
314
+ // - remove "abort" event listener on child
315
+ // - call any custom cancel function
316
+ applyRemoveEffects();
317
+ },
318
+ child_abort: () => {
319
+ applyAbortEffects();
320
+ operationAbortController.abort();
321
+ },
322
+ };
323
+ raceEffects[winner.name](winner.value);
324
+ },
325
+ );
326
+
327
+ return () => {
328
+ cancelRace();
329
+ applyRemoveEffects();
330
+ };
331
+ };
332
+
333
+ const addAbortSource = (abortSourceCallback) => {
334
+ const abortSource = {
335
+ cleaned: false,
336
+ signal: null,
337
+ remove: callbackNoop,
338
+ };
339
+ const abortSourceController = new AbortController();
340
+ const abortSourceSignal = abortSourceController.signal;
341
+ abortSource.signal = abortSourceSignal;
342
+ if (operationSignal.aborted) {
343
+ return abortSource;
344
+ }
345
+ const returnValue = abortSourceCallback((value) => {
346
+ abortSourceController.abort(value);
347
+ });
348
+ const removeAbortSignal = addAbortSignal(abortSourceSignal, {
349
+ onRemove: () => {
350
+ if (typeof returnValue === "function") {
351
+ returnValue();
352
+ }
353
+ abortSource.cleaned = true;
354
+ },
355
+ });
356
+ abortSource.remove = removeAbortSignal;
357
+ return abortSource;
358
+ };
359
+
360
+ const timeout = (ms) => {
361
+ return addAbortSource((abort) => {
362
+ const timeoutId = setTimeout(abort, ms);
363
+ // an abort source return value is called when:
364
+ // - operation is aborted (by an other source)
365
+ // - operation ends
366
+ return () => {
367
+ clearTimeout(timeoutId);
368
+ };
369
+ });
370
+ };
371
+
372
+ const wait = (ms) => {
373
+ return new Promise((resolve) => {
374
+ const timeoutId = setTimeout(() => {
375
+ removeAbortCallback();
376
+ resolve();
377
+ }, ms);
378
+ const removeAbortCallback = addAbortCallback(() => {
379
+ clearTimeout(timeoutId);
380
+ });
381
+ });
382
+ };
383
+
384
+ const withSignal = async (asyncCallback) => {
385
+ const abortController = new AbortController();
386
+ const signal = abortController.signal;
387
+ const removeAbortSignal = addAbortSignal(signal, {
388
+ onAbort: () => {
389
+ abortController.abort();
390
+ },
391
+ });
392
+ try {
393
+ const value = await asyncCallback(signal);
394
+ removeAbortSignal();
395
+ return value;
396
+ } catch (e) {
397
+ removeAbortSignal();
398
+ throw e;
399
+ }
400
+ };
401
+
402
+ const withSignalSync = (callback) => {
403
+ const abortController = new AbortController();
404
+ const signal = abortController.signal;
405
+ const removeAbortSignal = addAbortSignal(signal, {
406
+ onAbort: () => {
407
+ abortController.abort();
408
+ },
409
+ });
410
+ try {
411
+ const value = callback(signal);
412
+ removeAbortSignal();
413
+ return value;
414
+ } catch (e) {
415
+ removeAbortSignal();
416
+ throw e;
417
+ }
418
+ };
419
+
420
+ const fork = () => {
421
+ const forkedOperation = createOperation();
422
+ forkedOperation.addAbortSignal(operationSignal);
423
+ return forkedOperation;
424
+ };
425
+
426
+ return {
427
+ // We could almost hide the operationSignal
428
+ // But it can be handy for 2 things:
429
+ // - know if operation is aborted (operation.signal.aborted)
430
+ // - forward the operation.signal directly (not using "withSignal" or "withSignalSync")
431
+ signal: operationSignal,
432
+
433
+ throwIfAborted,
434
+ addAbortCallback,
435
+ addAbortSignal,
436
+ addAbortSource,
437
+ fork,
438
+ timeout,
439
+ wait,
440
+ withSignal,
441
+ withSignalSync,
442
+ addEndCallback,
443
+ end,
444
+ };
445
+ };
446
+
447
+ const callbackNoop = () => {};
448
+
449
+ const addEventListener = (target, eventName, cb) => {
450
+ target.addEventListener(eventName, cb);
451
+ return () => {
452
+ target.removeEventListener(eventName, cb);
453
+ };
454
+ };
455
+
456
+ const raceProcessTeardownEvents = (processTeardownEvents, callback) => {
457
+ return raceCallbacks(
458
+ {
459
+ ...(processTeardownEvents.SIGHUP ? SIGHUP_CALLBACK : {}),
460
+ ...(processTeardownEvents.SIGTERM ? SIGTERM_CALLBACK : {}),
461
+ ...(SIGINT_CALLBACK ),
462
+ ...(processTeardownEvents.beforeExit ? BEFORE_EXIT_CALLBACK : {}),
463
+ ...(processTeardownEvents.exit ? EXIT_CALLBACK : {}),
464
+ },
465
+ callback,
466
+ );
467
+ };
468
+
469
+ const SIGHUP_CALLBACK = {
470
+ SIGHUP: (cb) => {
471
+ process.on("SIGHUP", cb);
472
+ return () => {
473
+ process.removeListener("SIGHUP", cb);
474
+ };
475
+ },
476
+ };
477
+
478
+ const SIGTERM_CALLBACK = {
479
+ SIGTERM: (cb) => {
480
+ process.on("SIGTERM", cb);
481
+ return () => {
482
+ process.removeListener("SIGTERM", cb);
483
+ };
484
+ },
485
+ };
486
+
487
+ const BEFORE_EXIT_CALLBACK = {
488
+ beforeExit: (cb) => {
489
+ process.on("beforeExit", cb);
490
+ return () => {
491
+ process.removeListener("beforeExit", cb);
492
+ };
493
+ },
494
+ };
495
+
496
+ const EXIT_CALLBACK = {
497
+ exit: (cb) => {
498
+ process.on("exit", cb);
499
+ return () => {
500
+ process.removeListener("exit", cb);
501
+ };
502
+ },
503
+ };
504
+
505
+ const SIGINT_CALLBACK = {
506
+ SIGINT: (cb) => {
507
+ process.on("SIGINT", cb);
508
+ return () => {
509
+ process.removeListener("SIGINT", cb);
510
+ };
511
+ },
512
+ };
513
+
514
+ // https://github.com/Marak/colors.js/blob/master/lib/styles.js
515
+ // https://stackoverflow.com/a/75985833/2634179
516
+ const RESET = "\x1b[0m";
517
+
518
+ const RED = "red";
519
+ const GREEN = "green";
520
+ const YELLOW = "yellow";
521
+ const BLUE = "blue";
522
+ const MAGENTA = "magenta";
523
+ const CYAN = "cyan";
524
+ const GREY = "grey";
525
+ const WHITE = "white";
526
+ const BLACK = "black";
527
+
528
+ const TEXT_COLOR_ANSI_CODES = {
529
+ [RED]: "\x1b[31m",
530
+ [GREEN]: "\x1b[32m",
531
+ [YELLOW]: "\x1b[33m",
532
+ [BLUE]: "\x1b[34m",
533
+ [MAGENTA]: "\x1b[35m",
534
+ [CYAN]: "\x1b[36m",
535
+ [GREY]: "\x1b[90m",
536
+ [WHITE]: "\x1b[37m",
537
+ [BLACK]: "\x1b[30m",
538
+ };
539
+ const BACKGROUND_COLOR_ANSI_CODES = {
540
+ [RED]: "\x1b[41m",
541
+ [GREEN]: "\x1b[42m",
542
+ [YELLOW]: "\x1b[43m",
543
+ [BLUE]: "\x1b[44m",
544
+ [MAGENTA]: "\x1b[45m",
545
+ [CYAN]: "\x1b[46m",
546
+ [GREY]: "\x1b[100m",
547
+ [WHITE]: "\x1b[47m",
548
+ [BLACK]: "\x1b[40m",
549
+ };
550
+
551
+ const createAnsi = ({ supported }) => {
552
+ const ANSI = {
553
+ supported,
554
+
555
+ RED,
556
+ GREEN,
557
+ YELLOW,
558
+ BLUE,
559
+ MAGENTA,
560
+ CYAN,
561
+ GREY,
562
+ WHITE,
563
+ BLACK,
564
+ color: (text, color) => {
565
+ if (!ANSI.supported) {
566
+ return text;
567
+ }
568
+ if (!color) {
569
+ return text;
570
+ }
571
+ if (typeof text === "string" && text.trim() === "") {
572
+ // cannot set color of blank chars
573
+ return text;
574
+ }
575
+ const ansiEscapeCodeForTextColor = TEXT_COLOR_ANSI_CODES[color];
576
+ if (!ansiEscapeCodeForTextColor) {
577
+ return text;
578
+ }
579
+ return `${ansiEscapeCodeForTextColor}${text}${RESET}`;
580
+ },
581
+ backgroundColor: (text, color) => {
582
+ if (!ANSI.supported) {
583
+ return text;
584
+ }
585
+ if (!color) {
586
+ return text;
587
+ }
588
+ if (typeof text === "string" && text.trim() === "") {
589
+ // cannot set background color of blank chars
590
+ return text;
591
+ }
592
+ const ansiEscapeCodeForBackgroundColor =
593
+ BACKGROUND_COLOR_ANSI_CODES[color];
594
+ if (!ansiEscapeCodeForBackgroundColor) {
595
+ return text;
596
+ }
597
+ return `${ansiEscapeCodeForBackgroundColor}${text}${RESET}`;
598
+ },
599
+
600
+ BOLD: "\x1b[1m",
601
+ UNDERLINE: "\x1b[4m",
602
+ STRIKE: "\x1b[9m",
603
+ effect: (text, effect) => {
604
+ if (!ANSI.supported) {
605
+ return text;
606
+ }
607
+ if (!effect) {
608
+ return text;
609
+ }
610
+ // cannot add effect to empty string
611
+ if (text === "") {
612
+ return text;
613
+ }
614
+ const ansiEscapeCodeForEffect = effect;
615
+ return `${ansiEscapeCodeForEffect}${text}${RESET}`;
616
+ },
617
+ };
618
+
619
+ return ANSI;
620
+ };
621
+
622
+ const processSupportsBasicColor = createSupportsColor(process.stdout).hasBasic;
623
+
624
+ const ANSI = createAnsi({
625
+ supported:
626
+ process.env.FORCE_COLOR === "1" ||
627
+ processSupportsBasicColor ||
628
+ // GitHub workflow does support ANSI but "supports-color" returns false
629
+ // because stream.isTTY returns false, see https://github.com/actions/runner/issues/241
630
+ process.env.GITHUB_WORKFLOW,
631
+ });
632
+
633
+ // see also https://github.com/sindresorhus/figures
634
+
635
+ const createUnicode = ({ supported, ANSI }) => {
636
+ const UNICODE = {
637
+ supported,
638
+ get COMMAND_RAW() {
639
+ return UNICODE.supported ? `❯` : `>`;
640
+ },
641
+ get OK_RAW() {
642
+ return UNICODE.supported ? `✔` : `√`;
643
+ },
644
+ get FAILURE_RAW() {
645
+ return UNICODE.supported ? `✖` : `×`;
646
+ },
647
+ get DEBUG_RAW() {
648
+ return UNICODE.supported ? `◆` : `♦`;
649
+ },
650
+ get INFO_RAW() {
651
+ return UNICODE.supported ? `ℹ` : `i`;
652
+ },
653
+ get WARNING_RAW() {
654
+ return UNICODE.supported ? `⚠` : `‼`;
655
+ },
656
+ get CIRCLE_CROSS_RAW() {
657
+ return UNICODE.supported ? `ⓧ` : `(×)`;
658
+ },
659
+ get CIRCLE_DOTTED_RAW() {
660
+ return UNICODE.supported ? `◌` : `*`;
661
+ },
662
+ get COMMAND() {
663
+ return ANSI.color(UNICODE.COMMAND_RAW, ANSI.GREY); // ANSI_MAGENTA)
664
+ },
665
+ get OK() {
666
+ return ANSI.color(UNICODE.OK_RAW, ANSI.GREEN);
667
+ },
668
+ get FAILURE() {
669
+ return ANSI.color(UNICODE.FAILURE_RAW, ANSI.RED);
670
+ },
671
+ get DEBUG() {
672
+ return ANSI.color(UNICODE.DEBUG_RAW, ANSI.GREY);
673
+ },
674
+ get INFO() {
675
+ return ANSI.color(UNICODE.INFO_RAW, ANSI.BLUE);
676
+ },
677
+ get WARNING() {
678
+ return ANSI.color(UNICODE.WARNING_RAW, ANSI.YELLOW);
679
+ },
680
+ get CIRCLE_CROSS() {
681
+ return ANSI.color(UNICODE.CIRCLE_CROSS_RAW, ANSI.RED);
682
+ },
683
+ get ELLIPSIS() {
684
+ return UNICODE.supported ? `…` : `...`;
685
+ },
686
+ };
687
+ return UNICODE;
688
+ };
689
+
690
+ const UNICODE = createUnicode({
691
+ supported: process.env.FORCE_UNICODE === "1" || isUnicodeSupported(),
692
+ ANSI,
693
+ });
694
+
695
+ const setRoundedPrecision = (
696
+ number,
697
+ { decimals = 1, decimalsWhenSmall = decimals } = {},
698
+ ) => {
699
+ return setDecimalsPrecision(number, {
700
+ decimals,
701
+ decimalsWhenSmall,
702
+ transform: Math.round,
703
+ });
704
+ };
705
+
706
+ const setPrecision = (
707
+ number,
708
+ { decimals = 1, decimalsWhenSmall = decimals } = {},
709
+ ) => {
710
+ return setDecimalsPrecision(number, {
711
+ decimals,
712
+ decimalsWhenSmall,
713
+ transform: parseInt,
714
+ });
715
+ };
716
+
717
+ const setDecimalsPrecision = (
718
+ number,
719
+ {
720
+ transform,
721
+ decimals, // max decimals for number in [-Infinity, -1[]1, Infinity]
722
+ decimalsWhenSmall, // max decimals for number in [-1,1]
723
+ } = {},
724
+ ) => {
725
+ if (number === 0) {
726
+ return 0;
727
+ }
728
+ let numberCandidate = Math.abs(number);
729
+ if (numberCandidate < 1) {
730
+ const integerGoal = Math.pow(10, decimalsWhenSmall - 1);
731
+ let i = 1;
732
+ while (numberCandidate < integerGoal) {
733
+ numberCandidate *= 10;
734
+ i *= 10;
735
+ }
736
+ const asInteger = transform(numberCandidate);
737
+ const asFloat = asInteger / i;
738
+ return number < 0 ? -asFloat : asFloat;
739
+ }
740
+ const coef = Math.pow(10, decimals);
741
+ const numberMultiplied = (number + Number.EPSILON) * coef;
742
+ const asInteger = transform(numberMultiplied);
743
+ const asFloat = asInteger / coef;
744
+ return number < 0 ? -asFloat : asFloat;
745
+ };
746
+
747
+ // https://www.codingem.com/javascript-how-to-limit-decimal-places/
748
+ // export const roundNumber = (number, maxDecimals) => {
749
+ // const decimalsExp = Math.pow(10, maxDecimals)
750
+ // const numberRoundInt = Math.round(decimalsExp * (number + Number.EPSILON))
751
+ // const numberRoundFloat = numberRoundInt / decimalsExp
752
+ // return numberRoundFloat
753
+ // }
754
+
755
+ // export const setPrecision = (number, precision) => {
756
+ // if (Math.floor(number) === number) return number
757
+ // const [int, decimals] = number.toString().split(".")
758
+ // if (precision <= 0) return int
759
+ // const numberTruncated = `${int}.${decimals.slice(0, precision)}`
760
+ // return numberTruncated
761
+ // }
762
+
763
+ const unitShort = {
764
+ year: "y",
765
+ month: "m",
766
+ week: "w",
767
+ day: "d",
768
+ hour: "h",
769
+ minute: "m",
770
+ second: "s",
771
+ };
772
+
773
+ const humanizeDuration = (
774
+ ms,
775
+ { short, rounded = true, decimals } = {},
776
+ ) => {
777
+ // ignore ms below meaningfulMs so that:
778
+ // humanizeDuration(0.5) -> "0 second"
779
+ // humanizeDuration(1.1) -> "0.001 second" (and not "0.0011 second")
780
+ // This tool is meant to be read by humans and it would be barely readable to see
781
+ // "0.0001 second" (stands for 0.1 millisecond)
782
+ // yes we could return "0.1 millisecond" but we choosed consistency over precision
783
+ // so that the prefered unit is "second" (and does not become millisecond when ms is super small)
784
+ if (ms < 1) {
785
+ return short ? "0s" : "0 second";
786
+ }
787
+ const { primary, remaining } = parseMs(ms);
788
+ if (!remaining) {
789
+ return humanizeDurationUnit(primary, {
790
+ decimals:
791
+ decimals === undefined ? (primary.name === "second" ? 1 : 0) : decimals,
792
+ short,
793
+ rounded,
794
+ });
795
+ }
796
+ return `${humanizeDurationUnit(primary, {
797
+ decimals: decimals === undefined ? 0 : decimals,
798
+ short,
799
+ rounded,
800
+ })} and ${humanizeDurationUnit(remaining, {
801
+ decimals: decimals === undefined ? 0 : decimals,
802
+ short,
803
+ rounded,
804
+ })}`;
805
+ };
806
+ const humanizeDurationUnit = (unit, { decimals, short, rounded }) => {
807
+ const count = rounded
808
+ ? setRoundedPrecision(unit.count, { decimals })
809
+ : setPrecision(unit.count, { decimals });
810
+ let name = unit.name;
811
+ if (short) {
812
+ name = unitShort[name];
813
+ return `${count}${name}`;
814
+ }
815
+ if (count <= 1) {
816
+ return `${count} ${name}`;
817
+ }
818
+ return `${count} ${name}s`;
819
+ };
820
+ const MS_PER_UNITS = {
821
+ year: 31_557_600_000,
822
+ month: 2_629_000_000,
823
+ week: 604_800_000,
824
+ day: 86_400_000,
825
+ hour: 3_600_000,
826
+ minute: 60_000,
827
+ second: 1000,
828
+ };
829
+
830
+ const parseMs = (ms) => {
831
+ const unitNames = Object.keys(MS_PER_UNITS);
832
+ const smallestUnitName = unitNames[unitNames.length - 1];
833
+ let firstUnitName = smallestUnitName;
834
+ let firstUnitCount = ms / MS_PER_UNITS[smallestUnitName];
835
+ const firstUnitIndex = unitNames.findIndex((unitName) => {
836
+ if (unitName === smallestUnitName) {
837
+ return false;
838
+ }
839
+ const msPerUnit = MS_PER_UNITS[unitName];
840
+ const unitCount = Math.floor(ms / msPerUnit);
841
+ if (unitCount) {
842
+ firstUnitName = unitName;
843
+ firstUnitCount = unitCount;
844
+ return true;
845
+ }
846
+ return false;
847
+ });
848
+ if (firstUnitName === smallestUnitName) {
849
+ return {
850
+ primary: {
851
+ name: firstUnitName,
852
+ count: firstUnitCount,
853
+ },
854
+ };
855
+ }
856
+ const remainingMs = ms - firstUnitCount * MS_PER_UNITS[firstUnitName];
857
+ const remainingUnitName = unitNames[firstUnitIndex + 1];
858
+ const remainingUnitCount = remainingMs / MS_PER_UNITS[remainingUnitName];
859
+ // - 1 year and 1 second is too much information
860
+ // so we don't check the remaining units
861
+ // - 1 year and 0.0001 week is awful
862
+ // hence the if below
863
+ if (Math.round(remainingUnitCount) < 1) {
864
+ return {
865
+ primary: {
866
+ name: firstUnitName,
867
+ count: firstUnitCount,
868
+ },
869
+ };
870
+ }
871
+ // - 1 year and 1 month is great
872
+ return {
873
+ primary: {
874
+ name: firstUnitName,
875
+ count: firstUnitCount,
876
+ },
877
+ remaining: {
878
+ name: remainingUnitName,
879
+ count: remainingUnitCount,
880
+ },
881
+ };
882
+ };
883
+
884
+ const LOG_LEVEL_OFF = "off";
885
+
886
+ const LOG_LEVEL_DEBUG = "debug";
887
+
888
+ const LOG_LEVEL_INFO = "info";
889
+
890
+ const LOG_LEVEL_WARN = "warn";
891
+
892
+ const LOG_LEVEL_ERROR = "error";
893
+
894
+ const createLogger = ({ logLevel = LOG_LEVEL_INFO } = {}) => {
895
+ if (logLevel === LOG_LEVEL_DEBUG) {
896
+ return {
897
+ level: "debug",
898
+ levels: { debug: true, info: true, warn: true, error: true },
899
+ debug,
900
+ info,
901
+ warn,
902
+ error,
903
+ };
904
+ }
905
+ if (logLevel === LOG_LEVEL_INFO) {
906
+ return {
907
+ level: "info",
908
+ levels: { debug: false, info: true, warn: true, error: true },
909
+ debug: debugDisabled,
910
+ info,
911
+ warn,
912
+ error,
913
+ };
914
+ }
915
+ if (logLevel === LOG_LEVEL_WARN) {
916
+ return {
917
+ level: "warn",
918
+ levels: { debug: false, info: false, warn: true, error: true },
919
+ debug: debugDisabled,
920
+ info: infoDisabled,
921
+ warn,
922
+ error,
923
+ };
924
+ }
925
+ if (logLevel === LOG_LEVEL_ERROR) {
926
+ return {
927
+ level: "error",
928
+ levels: { debug: false, info: false, warn: false, error: true },
929
+ debug: debugDisabled,
930
+ info: infoDisabled,
931
+ warn: warnDisabled,
932
+ error,
933
+ };
934
+ }
935
+ if (logLevel === LOG_LEVEL_OFF) {
936
+ return {
937
+ level: "off",
938
+ levels: { debug: false, info: false, warn: false, error: false },
939
+ debug: debugDisabled,
940
+ info: infoDisabled,
941
+ warn: warnDisabled,
942
+ error: errorDisabled,
943
+ };
944
+ }
945
+ throw new Error(`unexpected logLevel.
946
+ --- logLevel ---
947
+ ${logLevel}
948
+ --- allowed log levels ---
949
+ ${LOG_LEVEL_OFF}
950
+ ${LOG_LEVEL_ERROR}
951
+ ${LOG_LEVEL_WARN}
952
+ ${LOG_LEVEL_INFO}
953
+ ${LOG_LEVEL_DEBUG}`);
954
+ };
955
+
956
+ const debug = (...args) => console.debug(...args);
957
+
958
+ const debugDisabled = () => {};
959
+
960
+ const info = (...args) => console.info(...args);
961
+
962
+ const infoDisabled = () => {};
963
+
964
+ const warn = (...args) => console.warn(...args);
965
+
966
+ const warnDisabled = () => {};
967
+
968
+ const error = (...args) => console.error(...args);
969
+
970
+ const errorDisabled = () => {};
971
+
972
+ const createMeasureTextWidth = ({ stripAnsi }) => {
973
+ const segmenter = new Intl.Segmenter();
974
+ const defaultIgnorableCodePointRegex = /^\p{Default_Ignorable_Code_Point}$/u;
975
+
976
+ const measureTextWidth = (
977
+ string,
978
+ {
979
+ ambiguousIsNarrow = true,
980
+ countAnsiEscapeCodes = false,
981
+ skipEmojis = false,
982
+ } = {},
983
+ ) => {
984
+ if (typeof string !== "string" || string.length === 0) {
985
+ return 0;
986
+ }
987
+
988
+ if (!countAnsiEscapeCodes) {
989
+ string = stripAnsi(string);
990
+ }
991
+
992
+ if (string.length === 0) {
993
+ return 0;
994
+ }
995
+
996
+ let width = 0;
997
+ const eastAsianWidthOptions = { ambiguousAsWide: !ambiguousIsNarrow };
998
+
999
+ for (const { segment: character } of segmenter.segment(string)) {
1000
+ const codePoint = character.codePointAt(0);
1001
+
1002
+ // Ignore control characters
1003
+ if (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f)) {
1004
+ continue;
1005
+ }
1006
+
1007
+ // Ignore zero-width characters
1008
+ if (
1009
+ (codePoint >= 0x20_0b && codePoint <= 0x20_0f) || // Zero-width space, non-joiner, joiner, left-to-right mark, right-to-left mark
1010
+ codePoint === 0xfe_ff // Zero-width no-break space
1011
+ ) {
1012
+ continue;
1013
+ }
1014
+
1015
+ // Ignore combining characters
1016
+ if (
1017
+ (codePoint >= 0x3_00 && codePoint <= 0x3_6f) || // Combining diacritical marks
1018
+ (codePoint >= 0x1a_b0 && codePoint <= 0x1a_ff) || // Combining diacritical marks extended
1019
+ (codePoint >= 0x1d_c0 && codePoint <= 0x1d_ff) || // Combining diacritical marks supplement
1020
+ (codePoint >= 0x20_d0 && codePoint <= 0x20_ff) || // Combining diacritical marks for symbols
1021
+ (codePoint >= 0xfe_20 && codePoint <= 0xfe_2f) // Combining half marks
1022
+ ) {
1023
+ continue;
1024
+ }
1025
+
1026
+ // Ignore surrogate pairs
1027
+ if (codePoint >= 0xd8_00 && codePoint <= 0xdf_ff) {
1028
+ continue;
1029
+ }
1030
+
1031
+ // Ignore variation selectors
1032
+ if (codePoint >= 0xfe_00 && codePoint <= 0xfe_0f) {
1033
+ continue;
1034
+ }
1035
+
1036
+ // This covers some of the above cases, but we still keep them for performance reasons.
1037
+ if (defaultIgnorableCodePointRegex.test(character)) {
1038
+ continue;
1039
+ }
1040
+
1041
+ if (!skipEmojis && emojiRegex().test(character)) {
1042
+ if (process.env.CAPTURING_SIDE_EFFECTS) {
1043
+ if (character === "✔️") {
1044
+ width += 2;
1045
+ continue;
1046
+ }
1047
+ }
1048
+ width += measureTextWidth(character, {
1049
+ skipEmojis: true,
1050
+ countAnsiEscapeCodes: true, // to skip call to stripAnsi
1051
+ });
1052
+ continue;
1053
+ }
1054
+
1055
+ width += eastAsianWidth(codePoint, eastAsianWidthOptions);
1056
+ }
1057
+
1058
+ return width;
1059
+ };
1060
+ return measureTextWidth;
1061
+ };
1062
+
1063
+ const measureTextWidth = createMeasureTextWidth({
1064
+ stripAnsi: stripVTControlCharacters,
1065
+ });
1066
+
1067
+ /*
1068
+ * see also https://github.com/vadimdemedes/ink
1069
+ */
1070
+
1071
+
1072
+ const createDynamicLog = ({
1073
+ stream = process.stdout,
1074
+ clearTerminalAllowed,
1075
+ onVerticalOverflow = () => {},
1076
+ onWriteFromOutside = () => {},
1077
+ } = {}) => {
1078
+ const { columns = 80, rows = 24 } = stream;
1079
+ const dynamicLog = {
1080
+ destroyed: false,
1081
+ onVerticalOverflow,
1082
+ onWriteFromOutside,
1083
+ };
1084
+
1085
+ let lastOutput = "";
1086
+ let lastOutputFromOutside = "";
1087
+ let clearAttemptResult;
1088
+ let writing = false;
1089
+
1090
+ const getErasePreviousOutput = () => {
1091
+ // nothing to clear
1092
+ if (!lastOutput) {
1093
+ return "";
1094
+ }
1095
+ if (clearAttemptResult !== undefined) {
1096
+ return "";
1097
+ }
1098
+
1099
+ const logLines = lastOutput.split(/\r\n|\r|\n/);
1100
+ let visualLineCount = 0;
1101
+ for (const logLine of logLines) {
1102
+ const width = measureTextWidth(logLine);
1103
+ if (width === 0) {
1104
+ visualLineCount++;
1105
+ } else {
1106
+ visualLineCount += Math.ceil(width / columns);
1107
+ }
1108
+ }
1109
+
1110
+ if (visualLineCount > rows) {
1111
+ if (clearTerminalAllowed) {
1112
+ clearAttemptResult = true;
1113
+ return clearTerminal;
1114
+ }
1115
+ // the whole log cannot be cleared because it's vertically to long
1116
+ // (longer than terminal height)
1117
+ // readline.moveCursor cannot move cursor higher than screen height
1118
+ // it means we would only clear the visible part of the log
1119
+ // better keep the log untouched
1120
+ clearAttemptResult = false;
1121
+ dynamicLog.onVerticalOverflow();
1122
+ return "";
1123
+ }
1124
+
1125
+ clearAttemptResult = true;
1126
+ return eraseLines(visualLineCount);
1127
+ };
1128
+
1129
+ const update = (string) => {
1130
+ if (dynamicLog.destroyed) {
1131
+ throw new Error("Cannot write log after destroy");
1132
+ }
1133
+ let stringToWrite = string;
1134
+ if (lastOutput) {
1135
+ if (lastOutputFromOutside) {
1136
+ // We don't want to clear logs written by other code,
1137
+ // it makes output unreadable and might erase precious information
1138
+ // To detect this we put a spy on the stream.
1139
+ // The spy is required only if we actually wrote something in the stream
1140
+ // something else than this code has written in the stream
1141
+ // so we just write without clearing (append instead of replacing)
1142
+ lastOutput = "";
1143
+ lastOutputFromOutside = "";
1144
+ } else {
1145
+ stringToWrite = `${getErasePreviousOutput()}${string}`;
1146
+ }
1147
+ }
1148
+ writing = true;
1149
+ stream.write(stringToWrite);
1150
+ lastOutput = string;
1151
+ writing = false;
1152
+ clearAttemptResult = undefined;
1153
+ };
1154
+
1155
+ const clearDuringFunctionCall = (
1156
+ callback,
1157
+ ouputAfterCallback = lastOutput,
1158
+ ) => {
1159
+ // 1. Erase the current log
1160
+ // 2. Call callback (expect to write something on stdout)
1161
+ // 3. Restore the current log
1162
+ // During step 2. we expect a "write from outside" so we uninstall
1163
+ // the stream spy during function call
1164
+ update("");
1165
+
1166
+ writing = true;
1167
+ callback(update);
1168
+ lastOutput = "";
1169
+ writing = false;
1170
+
1171
+ update(ouputAfterCallback);
1172
+ };
1173
+
1174
+ const writeFromOutsideEffect = (value) => {
1175
+ if (!lastOutput) {
1176
+ // we don't care if the log never wrote anything
1177
+ // or if last update() wrote an empty string
1178
+ return;
1179
+ }
1180
+ if (writing) {
1181
+ return;
1182
+ }
1183
+ lastOutputFromOutside = value;
1184
+ dynamicLog.onWriteFromOutside(value);
1185
+ };
1186
+
1187
+ let removeStreamSpy;
1188
+ if (stream === process.stdout) {
1189
+ const removeStdoutSpy = spyStreamOutput(
1190
+ process.stdout,
1191
+ writeFromOutsideEffect,
1192
+ );
1193
+ const removeStderrSpy = spyStreamOutput(
1194
+ process.stderr,
1195
+ writeFromOutsideEffect,
1196
+ );
1197
+ removeStreamSpy = () => {
1198
+ removeStdoutSpy();
1199
+ removeStderrSpy();
1200
+ };
1201
+ } else {
1202
+ removeStreamSpy = spyStreamOutput(stream, writeFromOutsideEffect);
1203
+ }
1204
+
1205
+ const destroy = () => {
1206
+ dynamicLog.destroyed = true;
1207
+ if (removeStreamSpy) {
1208
+ removeStreamSpy();
1209
+ removeStreamSpy = null;
1210
+ lastOutput = "";
1211
+ lastOutputFromOutside = "";
1212
+ }
1213
+ };
1214
+
1215
+ Object.assign(dynamicLog, {
1216
+ update,
1217
+ destroy,
1218
+ stream,
1219
+ clearDuringFunctionCall,
1220
+ });
1221
+ return dynamicLog;
1222
+ };
1223
+
1224
+ // maybe https://github.com/gajus/output-interceptor/tree/v3.0.0 ?
1225
+ // the problem with listening data on stdout
1226
+ // is that node.js will later throw error if stream gets closed
1227
+ // while something listening data on it
1228
+ const spyStreamOutput = (stream, callback) => {
1229
+ let output = "";
1230
+ let installed = true;
1231
+ const originalWrite = stream.write;
1232
+ stream.write = function (...args /* chunk, encoding, callback */) {
1233
+ output += args;
1234
+ callback(output);
1235
+ return originalWrite.call(this, ...args);
1236
+ };
1237
+
1238
+ const uninstall = () => {
1239
+ if (!installed) {
1240
+ return;
1241
+ }
1242
+ stream.write = originalWrite;
1243
+ installed = false;
1244
+ };
1245
+
1246
+ return () => {
1247
+ uninstall();
1248
+ return output;
1249
+ };
1250
+ };
1251
+
1252
+ const startSpinner = ({
1253
+ dynamicLog,
1254
+ frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
1255
+ fps = 20,
1256
+ keepProcessAlive = false,
1257
+ stopOnWriteFromOutside = true,
1258
+ stopOnVerticalOverflow = true,
1259
+ render = () => "",
1260
+ effect = () => {},
1261
+ animated = dynamicLog.stream.isTTY,
1262
+ }) => {
1263
+ let frameIndex = 0;
1264
+ let interval;
1265
+ let running = true;
1266
+
1267
+ const spinner = {
1268
+ message: undefined,
1269
+ };
1270
+
1271
+ const update = (message) => {
1272
+ spinner.message = running
1273
+ ? `${frames[frameIndex]} ${message}\n`
1274
+ : `${message}\n`;
1275
+ return spinner.message;
1276
+ };
1277
+ spinner.update = update;
1278
+
1279
+ let cleanup;
1280
+ if (animated && ANSI.supported) {
1281
+ running = true;
1282
+ cleanup = effect();
1283
+ dynamicLog.update(update(render()));
1284
+
1285
+ interval = setInterval(() => {
1286
+ frameIndex = frameIndex === frames.length - 1 ? 0 : frameIndex + 1;
1287
+ dynamicLog.update(update(render()));
1288
+ }, 1000 / fps);
1289
+ if (!keepProcessAlive) {
1290
+ interval.unref();
1291
+ }
1292
+ } else {
1293
+ dynamicLog.update(update(render()));
1294
+ }
1295
+
1296
+ const stop = (message) => {
1297
+ running = false;
1298
+ if (interval) {
1299
+ clearInterval(interval);
1300
+ interval = null;
1301
+ }
1302
+ if (cleanup) {
1303
+ cleanup();
1304
+ cleanup = null;
1305
+ }
1306
+ if (dynamicLog && message) {
1307
+ dynamicLog.update(update(message));
1308
+ dynamicLog = null;
1309
+ }
1310
+ };
1311
+ spinner.stop = stop;
1312
+
1313
+ if (stopOnVerticalOverflow) {
1314
+ dynamicLog.onVerticalOverflow = stop;
1315
+ }
1316
+ if (stopOnWriteFromOutside) {
1317
+ dynamicLog.onWriteFromOutside = stop;
1318
+ }
1319
+
1320
+ return spinner;
1321
+ };
1322
+
1323
+ const createTaskLog = (
1324
+ label,
1325
+ { disabled = false, animated = true, stopOnWriteFromOutside } = {},
1326
+ ) => {
1327
+ if (disabled) {
1328
+ return {
1329
+ setRightText: () => {},
1330
+ done: () => {},
1331
+ happen: () => {},
1332
+ fail: () => {},
1333
+ };
1334
+ }
1335
+ if (animated && process.env.CAPTURING_SIDE_EFFECTS) {
1336
+ animated = false;
1337
+ }
1338
+ const startMs = Date.now();
1339
+ const dynamicLog = createDynamicLog();
1340
+ let message = label;
1341
+ const taskSpinner = startSpinner({
1342
+ dynamicLog,
1343
+ render: () => message,
1344
+ stopOnWriteFromOutside,
1345
+ animated,
1346
+ });
1347
+ return {
1348
+ setRightText: (value) => {
1349
+ message = `${label} ${value}`;
1350
+ },
1351
+ done: () => {
1352
+ const msEllapsed = Date.now() - startMs;
1353
+ taskSpinner.stop(
1354
+ `${UNICODE.OK} ${label} (done in ${humanizeDuration(msEllapsed)})`,
1355
+ );
1356
+ },
1357
+ happen: (message) => {
1358
+ taskSpinner.stop(
1359
+ `${UNICODE.INFO} ${message} (at ${new Date().toLocaleTimeString()})`,
1360
+ );
1361
+ },
1362
+ fail: (message = `failed to ${label}`) => {
1363
+ taskSpinner.stop(`${UNICODE.FAILURE} ${message}`);
1364
+ },
1365
+ };
1366
+ };
1367
+
1368
+ const pathnameToExtension = (pathname) => {
1369
+ const slashLastIndex = pathname.lastIndexOf("/");
1370
+ const filename =
1371
+ slashLastIndex === -1 ? pathname : pathname.slice(slashLastIndex + 1);
1372
+ if (filename.match(/@([0-9])+(\.[0-9]+)?(\.[0-9]+)?$/)) {
1373
+ return "";
1374
+ }
1375
+ const dotLastIndex = filename.lastIndexOf(".");
1376
+ if (dotLastIndex === -1) {
1377
+ return "";
1378
+ }
1379
+ // if (dotLastIndex === pathname.length - 1) return ""
1380
+ const extension = filename.slice(dotLastIndex);
1381
+ return extension;
1382
+ };
1383
+
1384
+ const resourceToPathname = (resource) => {
1385
+ const searchSeparatorIndex = resource.indexOf("?");
1386
+ if (searchSeparatorIndex > -1) {
1387
+ return resource.slice(0, searchSeparatorIndex);
1388
+ }
1389
+ const hashIndex = resource.indexOf("#");
1390
+ if (hashIndex > -1) {
1391
+ return resource.slice(0, hashIndex);
1392
+ }
1393
+ return resource;
1394
+ };
1395
+
1396
+ const urlToScheme = (url) => {
1397
+ const urlString = String(url);
1398
+ const colonIndex = urlString.indexOf(":");
1399
+ if (colonIndex === -1) {
1400
+ return "";
1401
+ }
1402
+
1403
+ const scheme = urlString.slice(0, colonIndex);
1404
+ return scheme;
1405
+ };
1406
+
1407
+ const urlToResource = (url) => {
1408
+ const scheme = urlToScheme(url);
1409
+
1410
+ if (scheme === "file") {
1411
+ const urlAsStringWithoutFileProtocol = String(url).slice("file://".length);
1412
+ return urlAsStringWithoutFileProtocol;
1413
+ }
1414
+
1415
+ if (scheme === "https" || scheme === "http") {
1416
+ // remove origin
1417
+ const afterProtocol = String(url).slice(scheme.length + "://".length);
1418
+ const pathnameSlashIndex = afterProtocol.indexOf("/", "://".length);
1419
+ const urlAsStringWithoutOrigin = afterProtocol.slice(pathnameSlashIndex);
1420
+ return urlAsStringWithoutOrigin;
1421
+ }
1422
+
1423
+ const urlAsStringWithoutProtocol = String(url).slice(scheme.length + 1);
1424
+ return urlAsStringWithoutProtocol;
1425
+ };
1426
+
1427
+ const urlToPathname = (url) => {
1428
+ const resource = urlToResource(url);
1429
+ const pathname = resourceToPathname(resource);
1430
+ return pathname;
1431
+ };
1432
+
1433
+ const urlToExtension = (url) => {
1434
+ const pathname = urlToPathname(url);
1435
+ return pathnameToExtension(pathname);
1436
+ };
1437
+
1438
+ const transformUrlPathname = (url, transformer) => {
1439
+ if (typeof url === "string") {
1440
+ const urlObject = new URL(url);
1441
+ const { pathname } = urlObject;
1442
+ const pathnameTransformed = transformer(pathname);
1443
+ if (pathnameTransformed === pathname) {
1444
+ return url;
1445
+ }
1446
+ let { origin } = urlObject;
1447
+ // origin is "null" for "file://" urls with Node.js
1448
+ if (origin === "null" && urlObject.href.startsWith("file:")) {
1449
+ origin = "file://";
1450
+ }
1451
+ const { search, hash } = urlObject;
1452
+ const urlWithPathnameTransformed = `${origin}${pathnameTransformed}${search}${hash}`;
1453
+ return urlWithPathnameTransformed;
1454
+ }
1455
+ const pathnameTransformed = transformer(url.pathname);
1456
+ url.pathname = pathnameTransformed;
1457
+ return url;
1458
+ };
1459
+ const ensurePathnameTrailingSlash = (url) => {
1460
+ return transformUrlPathname(url, (pathname) => {
1461
+ return pathname.endsWith("/") ? pathname : `${pathname}/`;
1462
+ });
1463
+ };
1464
+
1465
+ const isFileSystemPath = (value) => {
1466
+ if (typeof value !== "string") {
1467
+ throw new TypeError(
1468
+ `isFileSystemPath first arg must be a string, got ${value}`,
1469
+ );
1470
+ }
1471
+ if (value[0] === "/") {
1472
+ return true;
1473
+ }
1474
+ return startsWithWindowsDriveLetter(value);
1475
+ };
1476
+
1477
+ const startsWithWindowsDriveLetter = (string) => {
1478
+ const firstChar = string[0];
1479
+ if (!/[a-zA-Z]/.test(firstChar)) return false;
1480
+
1481
+ const secondChar = string[1];
1482
+ if (secondChar !== ":") return false;
1483
+
1484
+ return true;
1485
+ };
1486
+
1487
+ const fileSystemPathToUrl = (value) => {
1488
+ if (!isFileSystemPath(value)) {
1489
+ throw new Error(`value must be a filesystem path, got ${value}`);
1490
+ }
1491
+ return String(pathToFileURL(value));
1492
+ };
1493
+
1494
+ const validateDirectoryUrl = (value) => {
1495
+ let urlString;
1496
+
1497
+ if (value instanceof URL) {
1498
+ urlString = value.href;
1499
+ } else if (typeof value === "string") {
1500
+ if (isFileSystemPath(value)) {
1501
+ urlString = fileSystemPathToUrl(value);
1502
+ } else {
1503
+ try {
1504
+ urlString = String(new URL(value));
1505
+ } catch {
1506
+ return {
1507
+ valid: false,
1508
+ value,
1509
+ message: `must be a valid url`,
1510
+ };
1511
+ }
1512
+ }
1513
+ } else if (
1514
+ value &&
1515
+ typeof value === "object" &&
1516
+ typeof value.href === "string"
1517
+ ) {
1518
+ value = value.href;
1519
+ } else {
1520
+ return {
1521
+ valid: false,
1522
+ value,
1523
+ message: `must be a string or an url`,
1524
+ };
1525
+ }
1526
+ if (!urlString.startsWith("file://")) {
1527
+ return {
1528
+ valid: false,
1529
+ value,
1530
+ message: 'must start with "file://"',
1531
+ };
1532
+ }
1533
+ return {
1534
+ valid: true,
1535
+ value: ensurePathnameTrailingSlash(urlString),
1536
+ };
1537
+ };
1538
+
1539
+ const assertAndNormalizeDirectoryUrl = (
1540
+ directoryUrl,
1541
+ name = "directoryUrl",
1542
+ ) => {
1543
+ const { valid, message, value } = validateDirectoryUrl(directoryUrl);
1544
+ if (!valid) {
1545
+ throw new TypeError(`${name} ${message}, got ${value}`);
1546
+ }
1547
+ return value;
1548
+ };
1549
+
1550
+ export { Abort, assertAndNormalizeDirectoryUrl, createLogger, createTaskLog, raceProcessTeardownEvents, urlToExtension, urlToPathname };