1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975/**
* This file is loaded in every test file in the repository.
*
* Avoid adding external dependencies here so that we can still run some tests
* without always needing to run `bun install` in development.
*/
import { gc as bunGC, sleepSync, spawnSync, unsafe, which, write } from "bun";
import { heapStats } from "bun:jsc";
import { beforeAll, describe, expect } from "bun:test";
import { ChildProcess, execSync, fork } from "child_process";
import { readdir, readFile, readlink, rm, writeFile } from "fs/promises";
import fs, { closeSync, openSync, rmSync } from "node:fs";
import os from "node:os";
import { dirname, isAbsolute, join } from "path";
import * as numeric from "_util/numeric.ts";
export const BREAKING_CHANGES_BUN_1_2 = false;
export const isMacOS = process.platform === "darwin";
export const isLinux = process.platform === "linux";
export const isPosix = isMacOS || isLinux;
export const isWindows = process.platform === "win32";
export const isIntelMacOS = isMacOS && process.arch === "x64";
export const isArm64 = process.arch === "arm64";
export const isDebug = Bun.version.includes("debug");
export const isCI = process.env.CI !== undefined;
export const libcFamily: "glibc" | "musl" =
process.platform !== "linux"
? "glibc"
: // process.report.getReport() has incorrect type definitions.
(process.report.getReport() as { header: { glibcVersionRuntime: boolean } }).header.glibcVersionRuntime
? "glibc"
: "musl";
export const isMusl = isLinux && libcFamily === "musl";
export const isGlibc = isLinux && libcFamily === "glibc";
export const isBuildKite = process.env.BUILDKITE === "true";
export const isVerbose = process.env.DEBUG === "1";
// Use these to mark a test as flaky or broken.
// This will help us keep track of these tests.
//
// test.todoIf(isFlaky && isMacOS)("this test is flaky");
export const isFlaky = isCI;
export const isBroken = isCI;
export const isASAN = basename(process.execPath).includes("bun-asan");
export const bunEnv: NodeJS.Dict<string> = {
...process.env,
GITHUB_ACTIONS: "false",
BUN_DEBUG_QUIET_LOGS: "1",
NO_COLOR: "1",
FORCE_COLOR: undefined,
TZ: "Etc/UTC",
CI: "1",
BUN_RUNTIME_TRANSPILER_CACHE_PATH: "0",
BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING: "1",
BUN_GARBAGE_COLLECTOR_LEVEL: process.env.BUN_GARBAGE_COLLECTOR_LEVEL || "0",
BUN_FEATURE_FLAG_EXPERIMENTAL_BAKE: "1",
BUN_DEBUG_linkerctx: "0",
WANTS_LOUD: "0",
AGENT: "false",
};
const ciEnv = { ...bunEnv };
if (isASAN) {
bunEnv.ASAN_OPTIONS ??= "allow_user_segv_handler=1:disable_coredump=0";
}
if (isWindows) {
bunEnv.SHELLOPTS = "igncr"; // Ignore carriage return
}
for (let key in bunEnv) {
if (bunEnv[key] === undefined) {
delete ciEnv[key];
delete bunEnv[key];
}
if (key.startsWith("BUN_DEBUG_") && key !== "BUN_DEBUG_QUIET_LOGS") {
delete ciEnv[key];
delete bunEnv[key];
}
if (key.startsWith("BUILDKITE")) {
delete bunEnv[key];
delete process.env[key];
}
}
delete bunEnv.BUN_INSPECT_CONNECT_TO;
delete bunEnv.NODE_ENV;
if (isDebug) {
// This makes debug build memory leak tests more reliable.
// The code for dumping out the debug build transpiled source code has leaks.
bunEnv.BUN_DEBUG_NO_DUMP = "1";
}
export function bunExe() {
if (isWindows) return process.execPath.replaceAll("\\", "/");
return process.execPath;
}
export function nodeExe(): string | null {
return which("node") || null;
}
export function shellExe(): string {
return isWindows ? "pwsh" : "bash";
}
export function gc(force = true) {
bunGC(force);
}
/**
* The garbage collector is not 100% deterministic
*
* We want to assert that SOME of the objects are collected
* But we cannot reliably assert that ALL of them are collected
*
* Therefore, we check that the count is less than or equal to the expected count
*
* @param type
* @param count
* @param maxWait
* @returns
*/
export async function expectMaxObjectTypeCount(
expect: typeof import("bun:test").expect,
type: string,
count: number,
maxWait = 1000,
) {
var { heapStats } = require("bun:jsc");
gc();
if (heapStats().objectTypeCounts[type] <= count) return;
gc(true);
for (const wait = 20; maxWait > 0; maxWait -= wait) {
if (heapStats().objectTypeCounts[type] <= count) break;
await Bun.sleep(wait);
gc();
}
expect(heapStats().objectTypeCounts[type] || 0).toBeLessThanOrEqual(count);
}
// we must ensure that finalizers are run
// so that the reference-counting logic is exercised
export function gcTick(trace = false) {
trace && console.trace("");
// console.trace("hello");
gc();
return Bun.sleep(0);
}
export function withoutAggressiveGC(block: () => unknown) {
if (!unsafe.gcAggressionLevel) return block();
const origGC = unsafe.gcAggressionLevel();
unsafe.gcAggressionLevel(0);
try {
return block();
} finally {
unsafe.gcAggressionLevel(origGC);
}
}
export function hideFromStackTrace(block: CallableFunction) {
Object.defineProperty(block, "name", {
value: "::bunternal::",
configurable: true,
enumerable: true,
writable: true,
});
}
export type DirectoryTree = {
[name: string]:
| string
| Buffer
| DirectoryTree
| ((opts: { root: string }) => Bun.MaybePromise<string | Buffer | DirectoryTree>);
};
export async function makeTree(base: string, tree: DirectoryTree) {
const isDirectoryTree = (value: string | DirectoryTree | Buffer): value is DirectoryTree =>
typeof value === "object" && value && typeof value?.byteLength === "undefined";
for (const [name, raw_contents] of Object.entries(tree)) {
const contents = typeof raw_contents === "function" ? await raw_contents({ root: base }) : raw_contents;
const joined = join(base, name);
if (name.includes("/")) {
const dir = dirname(name);
if (dir !== name && dir !== ".") {
fs.mkdirSync(join(base, dir), { recursive: true });
}
}
if (isDirectoryTree(contents)) {
fs.mkdirSync(joined);
makeTree(joined, contents);
continue;
}
fs.writeFileSync(joined, contents);
}
}
export function makeTreeSyncFromDirectoryTree(base: string, tree: DirectoryTree) {
const isDirectoryTree = (value: string | DirectoryTree | Buffer): value is DirectoryTree =>
typeof value === "object" && value && typeof value?.byteLength === "undefined";
for (const [name, raw_contents] of Object.entries(tree)) {
const contents = (typeof raw_contents === "function" ? raw_contents({ root: base }) : raw_contents) as string;
const joined = join(base, name);
if (name.includes("/")) {
const dir = dirname(name);
if (dir !== name && dir !== ".") {
fs.mkdirSync(join(base, dir), { recursive: true });
}
}
if (isDirectoryTree(contents)) {
fs.mkdirSync(joined);
makeTreeSync(joined, contents);
continue;
}
fs.writeFileSync(joined, contents);
}
}
export function makeTreeSync(base: string, filesOrAbsolutePathToCopyFolderFrom: DirectoryTree | string) {
if (typeof filesOrAbsolutePathToCopyFolderFrom === "string") {
fs.cpSync(filesOrAbsolutePathToCopyFolderFrom, base, { recursive: true });
return;
}
return makeTreeSyncFromDirectoryTree(base, filesOrAbsolutePathToCopyFolderFrom);
}
/**
* Recursively create files within a new temporary directory.
*
* @param basename prefix of the new temporary directory
* @param filesOrAbsolutePathToCopyFolderFrom Directory tree or absolute path to a folder to copy. If passing an object each key is a folder or file, and each value is the contents of the file. Use objects for directories.
* @returns an absolute path to the new temporary directory
*
* @example
* ```ts
* const dir = tempDirWithFiles("my-test", {
* "index.js": `import foo from "./src/foo";`,
* "src": {
* "foo.js": `export default "foo";`,
* },
* });
* ```
*/
export function tempDirWithFiles(
basename: string,
filesOrAbsolutePathToCopyFolderFrom: DirectoryTree | string,
): string {
const base = fs.mkdtempSync(join(fs.realpathSync.native(os.tmpdir()), basename + "_"));
makeTreeSync(base, filesOrAbsolutePathToCopyFolderFrom);
return base;
}
class DisposableString extends String {
[Symbol.dispose]() {
fs.rmSync(this + "", { recursive: true, force: true });
}
[Symbol.asyncDispose]() {
return fs.promises.rm(this + "", { recursive: true, force: true });
}
}
export function tempDir(
basename: string,
filesOrAbsolutePathToCopyFolderFrom: DirectoryTree | string,
): string & DisposableString & AsyncDisposable {
const base = tempDirWithFiles(basename, filesOrAbsolutePathToCopyFolderFrom);
return new DisposableString(base) as string & DisposableString & AsyncDisposable;
}
export function tempDirWithFilesAnon(filesOrAbsolutePathToCopyFolderFrom: DirectoryTree | string): string {
const base = tmpdirSync();
makeTreeSync(base, filesOrAbsolutePathToCopyFolderFrom);
return base;
}
export function bunRun(file: string, env?: Record<string, string> | NodeJS.ProcessEnv, dump = false) {
var path = require("path");
const result = Bun.spawnSync([bunExe(), file], {
cwd: path.dirname(file),
env: {
...bunEnv,
NODE_ENV: undefined,
...env,
},
stdin: "ignore",
stdout: !dump ? "pipe" : "inherit",
stderr: !dump ? "pipe" : "inherit",
});
if (!result.success) {
if (dump) {
throw new Error(
"exited with code " + result.exitCode + (result.signalCode ? `signal: ${result.signalCode}` : ""),
);
}
throw new Error(String(result.stderr) + "\n" + String(result.stdout));
}
return {
stdout: String(result.stdout ?? "").trim(),
stderr: String(result.stderr ?? "").trim(),
};
}
export function bunTest(file: string, env?: Record<string, string>) {
var path = require("path");
const result = Bun.spawnSync([bunExe(), "test", path.basename(file)], {
cwd: path.dirname(file),
env: {
...bunEnv,
NODE_ENV: undefined,
...env,
},
});
if (!result.success) throw new Error(result.stderr.toString("utf8"));
return {
stdout: result.stdout.toString("utf8").trim(),
stderr: result.stderr.toString("utf8").trim(),
};
}
export function bunRunAsScript(
dir: string,
script: string,
env?: Record<string, string | undefined>,
execArgv?: string[],
) {
const result = Bun.spawnSync([bunExe(), ...(execArgv ?? []), `run`, `${script}`], {
cwd: dir,
env: {
...bunEnv,
NODE_ENV: undefined,
...env,
},
});
if (!result.success) throw new Error(result.stderr.toString("utf8"));
return {
stdout: result.stdout.toString("utf8").trim(),
stderr: result.stderr.toString("utf8").trim(),
};
}
export function randomLoneSurrogate() {
const n = numeric.random.between(0, 2, { domain: "integral" });
if (n === 0) return randomLoneHighSurrogate();
return randomLoneLowSurrogate();
}
export function randomInvalidSurrogatePair() {
const low = randomLoneLowSurrogate();
const high = randomLoneHighSurrogate();
return `${low}${high}`;
}
// Generates a random lone high surrogate (from the range D800-DBFF)
export function randomLoneHighSurrogate() {
return String.fromCharCode(numeric.random.between(0xd800, 0xdbff, { domain: "integral" }));
}
// Generates a random lone high surrogate (from the range DC00-DFFF)
export function randomLoneLowSurrogate() {
return String.fromCharCode(numeric.random.between(0xdc00, 0xdfff, { domain: "integral" }));
}
export function runWithError(cb: () => unknown): Error | undefined {
try {
cb();
} catch (e) {
return e as Error;
}
return undefined;
}
export async function runWithErrorPromise(cb: () => unknown): Promise<Error | undefined> {
try {
await cb();
} catch (e) {
return e as Error;
}
return undefined;
}
export function fakeNodeRun(dir: string, file: string | string[], env?: Record<string, string>) {
var path = require("path");
const result = Bun.spawnSync([bunExe(), "--bun", "node", ...(Array.isArray(file) ? file : [file])], {
cwd: dir ?? path.dirname(file),
env: {
...bunEnv,
NODE_ENV: undefined,
...env,
},
});
if (!result.success) throw new Error(result.stderr.toString("utf8"));
return {
stdout: result.stdout.toString("utf8").trim(),
stderr: result.stderr.toString("utf8").trim(),
};
}
export function randomPort(): number {
return 1024 + Math.floor(Math.random() * (65535 - 1024));
}
const binaryTypes = {
"buffer": Buffer,
"arraybuffer": ArrayBuffer,
"uint8array": Uint8Array,
"uint16array": Uint16Array,
"uint32array": Uint32Array,
"int8array": Int8Array,
"int16array": Int16Array,
"int32array": Int32Array,
"float16array": globalThis.Float16Array,
"float32array": Float32Array,
"float64array": Float64Array,
} as const;
if (expect.extend)
expect.extend({
toHaveTestTimedOutAfter(actual: any, expected: number) {
if (typeof actual !== "string") {
return {
pass: false,
message: () => `Expected ${actual} to be a string`,
};
}
const preStartI = actual.indexOf("timed out after ");
if (preStartI === -1) {
return {
pass: false,
message: () => `Expected ${actual} to contain "timed out after "`,
};
}
const startI = preStartI + "timed out after ".length;
const endI = actual.indexOf("ms", startI);
if (endI === -1) {
return {
pass: false,
message: () => `Expected ${actual} to contain "ms" after "timed out after "`,
};
}
const int = parseInt(actual.slice(startI, endI));
if (!Number.isSafeInteger(int)) {
return {
pass: false,
message: () => `Expected ${int} to be a safe integer`,
};
}
return {
pass: int >= expected,
message: () => `Expected ${int} to be >= ${expected}`,
};
},
toBeBinaryType(actual: any, expected: keyof typeof binaryTypes) {
switch (expected) {
case "buffer":
return {
pass: Buffer.isBuffer(actual),
message: () => `Expected ${actual} to be buffer`,
};
case "arraybuffer":
return {
pass: actual instanceof ArrayBuffer,
message: () => `Expected ${actual} to be ArrayBuffer`,
};
default: {
const ctor = binaryTypes[expected];
if (!ctor) {
return {
pass: false,
message: () => `Expected ${expected} to be a binary type`,
};
}
return {
pass: actual instanceof ctor,
message: () => `Expected ${actual} to be ${expected}`,
};
}
}
},
toRun(cmds: string[], optionalStdout?: string, expectedCode: number = 0) {
const result = Bun.spawnSync({
cmd: [bunExe(), ...cmds],
env: bunEnv,
stdio: ["inherit", "pipe", "inherit"],
});
if (result.exitCode !== expectedCode) {
return {
pass: false,
message: () => `Command ${cmds.join(" ")} failed:` + "\n" + result.stdout.toString("utf-8"),
};
}
if (optionalStdout != null) {
return {
pass: result.stdout.toString("utf-8") === optionalStdout,
message: () =>
`Expected ${cmds.join(" ")} to output ${optionalStdout} but got ${result.stdout.toString("utf-8")}`,
};
}
return {
pass: true,
message: () => `Expected ${cmds.join(" ")} to fail`,
};
},
toThrowWithCode(fn: CallableFunction, cls: CallableFunction, code: string) {
try {
fn();
return {
pass: false,
message: () => `Received function did not throw`,
};
} catch (e) {
// expect(e).toBeInstanceOf(cls);
if (!(e instanceof cls)) {
return {
pass: false,
message: () => `Expected error to be instanceof ${cls.name}; got ${e.__proto__.constructor.name}`,
};
}
// expect(e).toHaveProperty("code");
if (!("code" in e)) {
return {
pass: false,
message: () => `Expected error to have property 'code'; got ${e}`,
};
}
// expect(e.code).toEqual(code);
if (e.code !== code) {
return {
pass: false,
message: () => `Expected error to have code '${code}'; got ${e.code}`,
};
}
return {
pass: true,
};
}
},
async toThrowWithCodeAsync(fn: CallableFunction, cls: CallableFunction, code: string) {
try {
await fn();
return {
pass: false,
message: () => `Received function did not throw`,
};
} catch (e) {
// expect(e).toBeInstanceOf(cls);
if (!(e instanceof cls)) {
return {
pass: false,
message: () => `Expected error to be instanceof ${cls.name}; got ${e.__proto__.constructor.name}`,
};
}
// expect(e).toHaveProperty("code");
if (!("code" in e)) {
return {
pass: false,
message: () => `Expected error to have property 'code'; got ${e}`,
};
}
// expect(e.code).toEqual(code);
if (e.code !== code) {
return {
pass: false,
message: () => `Expected error to have code '${code}'; got ${e.code}`,
};
}
return {
pass: true,
};
}
},
toBeLatin1String(actual: unknown) {
if ((actual as string).isLatin1()) {
return {
pass: true,
message: () => `Expected ${actual} to be a Latin1 string`,
};
}
return {
pass: false,
message: () => `Expected ${actual} to be a Latin1 string`,
};
},
toBeUTF16String(actual: unknown) {
if ((actual as string).isUTF16()) {
return {
pass: true,
message: () => `Expected ${actual} to be a UTF16 string`,
};
}
return {
pass: false,
message: () => `Expected ${actual} to be a UTF16 string`,
};
},
});
export function ospath(path: string) {
if (isWindows) {
return path.replace(/\//g, "\\");
}
return path;
}
/**
* Iterates through each tree in the lockfile, checking for each package
* on disk. Also requires each package dependency. Not tested well for
* non-npm packages (links, folders, git dependencies, etc.)
*/
export async function toMatchNodeModulesAt(lockfile: any, root: string) {
function shouldSkip(pkg: any, dep: any): boolean {
// Band-aid as toMatchNodeModulesAt will sometimes ask this function
// if a package depends on itself
if (pkg?.name === dep?.name) return true;
return (
!pkg ||
!pkg.resolution ||
dep.behavior.optional ||
(dep.behavior.dev && pkg.id !== 0) ||
(pkg.arch && pkg.arch !== process.arch)
);
}
for (const { path, dependencies } of lockfile.trees) {
for (const { package_id, id } of Object.values(dependencies) as any[]) {
const treeDep = lockfile.dependencies[id];
const treePkg = lockfile.packages[package_id];
if (shouldSkip(treePkg, treeDep)) continue;
const treeDepPath = join(root, path, treeDep.name);
switch (treePkg.resolution.tag) {
case "npm":
const onDisk = await Bun.file(join(treeDepPath, "package.json")).json();
if (!Bun.deepMatch({ name: treePkg.name, version: treePkg.resolution.value }, onDisk)) {
return {
pass: false,
message: () => `
Expected at ${join(path, treeDep.name)}: ${JSON.stringify({ name: treePkg.name, version: treePkg.resolution.value })}
Received ${JSON.stringify({ name: onDisk.name, version: onDisk.version })}`,
};
}
// Ok, we've confirmed the package exists and has the correct version. Now go through
// each of its transitive dependencies and confirm the same.
for (const depId of treePkg.dependencies) {
const dep = lockfile.dependencies[depId];
const pkg = lockfile.packages[dep.package_id];
if (shouldSkip(pkg, dep)) continue;
try {
const resolved = await Bun.file(Bun.resolveSync(join(dep.name, "package.json"), treeDepPath)).json();
switch (pkg.resolution.tag) {
case "npm":
const name = dep.is_alias ? dep.npm.name : dep.name;
if (!Bun.deepMatch({ name, version: pkg.resolution.value }, resolved)) {
if (dep.literal === "*") {
// allow any version, just needs to be resolvable
continue;
}
if (dep.behavior.peer && dep.npm) {
// allow peer dependencies to not match exactly, but still satisfy
if (Bun.semver.satisfies(pkg.resolution.value, dep.npm.version)) continue;
}
return {
pass: false,
message: () =>
`Expected ${dep.name} to have version ${pkg.resolution.value} in ${treeDepPath}, but got ${resolved.version}`,
};
}
break;
}
} catch (e) {
return {
pass: false,
message: () => `Expected ${dep.name} to be resolvable in ${treeDepPath}`,
};
}
}
break;
default:
if (!fs.existsSync(treeDepPath)) {
return {
pass: false,
message: () => `Expected ${treePkg.resolution.tag} "${treeDepPath}" to exist`,
};
}
for (const depId of treePkg.dependencies) {
const dep = lockfile.dependencies[depId];
const pkg = lockfile.packages[dep.package_id];
if (shouldSkip(pkg, dep)) continue;
try {
const resolved = await Bun.file(Bun.resolveSync(join(dep.name, "package.json"), treeDepPath)).json();
switch (pkg.resolution.tag) {
case "npm":
const name = dep.is_alias ? dep.npm.name : dep.name;
if (!Bun.deepMatch({ name, version: pkg.resolution.value }, resolved)) {
if (dep.literal === "*") {
// allow any version, just needs to be resolvable
continue;
}
// workspaces don't need a version
if (treePkg.resolution.tag === "workspace" && !resolved.version) continue;
if (dep.behavior.peer && dep.npm) {
// allow peer dependencies to not match exactly, but still satisfy
if (Bun.semver.satisfies(pkg.resolution.value, dep.npm.version)) continue;
}
return {
pass: false,
message: () =>
`Expected ${dep.name} to have version ${pkg.resolution.value} in ${treeDepPath}, but got ${resolved.version}`,
};
}
break;
}
} catch (e) {
return {
pass: false,
message: () => `Expected ${dep.name} to be resolvable in ${treeDepPath}`,
};
}
}
break;
}
}
}
return {
pass: true,
};
}
export async function toHaveBins(actual: string[], expectedBins: string[]) {
const message = () => `Expected ${actual} to be package bins ${expectedBins}`;
if (isWindows) {
for (var i = 0; i < actual.length; i += 2) {
if (!actual[i].includes(expectedBins[i / 2]) || !actual[i + 1].includes(expectedBins[i / 2])) {
return { pass: false, message };
}
}
return { pass: true, message };
}
return { pass: actual.every((bin, i) => bin === expectedBins[i]), message };
}
export async function toBeValidBin(actual: string, expectedLinkPath: string) {
const message = () => `Expected ${actual} to be a link to ${expectedLinkPath}`;
if (isWindows) {
const contents = await readFile(actual + ".bunx", "utf16le");
const expected = expectedLinkPath.slice(3);
return { pass: contents.includes(expected), message };
}
return { pass: (await readlink(actual)) === expectedLinkPath, message };
}
export async function toBeWorkspaceLink(actual: string, expectedLinkPath: string) {
const message = () => `Expected ${actual} to be a link to ${expectedLinkPath}`;
if (isWindows) {
if (isAbsolute(actual)) {
// junctions on windows will have an absolute path
return {
pass: actual.includes(expectedLinkPath.split("..").at(-1)!),
message,
};
}
return {
pass: actual === expectedLinkPath,
message,
};
}
const pass = actual === expectedLinkPath;
return { pass, message };
}
export function getFDCount(): number {
if (isMacOS || isLinux) {
return fs.readdirSync(isMacOS ? "/dev/fd" : "/proc/self/fd").length;
}
const maxFD = openSync("/dev/null", "r");
closeSync(maxFD);
return maxFD;
}
export function getMaxFD(): number {
if (isMacOS || isLinux) {
let max = -1;
// https://github.com/python/cpython/commit/e21a7a976a7e3368dc1eba0895e15c47cb06c810
for (let entry of fs.readdirSync(isMacOS ? "/dev/fd" : "/proc/self/fd")) {
const fd = parseInt(entry.trim(), 10);
if (Number.isSafeInteger(fd) && fd >= 0) {
max = Math.max(max, fd);
}
}
if (max >= 0) {
return max;
}
}
const maxFD = openSync("/dev/null", "r");
closeSync(maxFD);
return maxFD;
}
// This is extremely frowned upon but I think it's easier to deal with than
// remembering to do this manually everywhere
declare global {
interface Buffer {
/**
* **INTERNAL USE ONLY, NOT An API IN BUN**
*/
toUnixString(): string;
}
interface String {
/**
* **INTERNAL USE ONLY, NOT An API IN BUN**
*/
isLatin1(): boolean;
/**
* **INTERNAL USE ONLY, NOT An API IN BUN**
*/
isUTF16(): boolean;
}
}
Buffer.prototype.toUnixString = function () {
return this.toString("utf-8").replaceAll("\r\n", "\n");
};
export function dockerExe(): string | null {
return which("docker") || which("podman") || null;
}
export function isDockerEnabled(): boolean {
const dockerCLI = dockerExe();
if (!dockerCLI) {
if (isCI && isLinux) {
throw new Error("A functional `docker` is required in CI for some tests.");
}
return false;
}
// TODO: investigate why Docker tests are not working on Linux arm64
if (isLinux && process.arch === "arm64") {
return false;
}
try {
const info = execSync(`"${dockerCLI}" info`, { stdio: ["ignore", "pipe", "inherit"] });
return info.toString().indexOf("Server Version:") !== -1;
} catch {
if (isCI && isLinux) {
throw new Error("A functional `docker` is required in CI for some tests.");
}
return false;
}
}
export async function waitForPort(port: number, timeout: number = 60_000): Promise<void> {
let deadline = Date.now() + Math.max(1, timeout);
let error: unknown;
while (Date.now() < deadline) {
error = await new Promise(resolve => {
Bun.connect({
hostname: "localhost",
port,
socket: {
data: socket => {
resolve(undefined);
socket.end();
},
end: () => resolve(new Error("Socket closed")),
error: (_, cause) => resolve(new Error("Socket error", { cause })),
connectError: (_, cause) => resolve(new Error("Socket connect error", { cause })),
},
});
});
if (error) {
await Bun.sleep(1000);
} else {
return;
}
}
throw error;
}
export async function describeWithContainer(
label: string,
{
image,
env = {},
args = [],
archs,
concurrent = false,
}: {
image: string;
env?: Record<string, string>;
args?: string[];
archs?: NodeJS.Architecture[];
concurrent?: boolean;
},
fn: (container: { port: number; host: string; ready: Promise<void> }) => void,
) {
// Skip if Docker is not available
if (!isDockerEnabled()) {
describe.todo(label);
return;
}
(concurrent && Bun.version !== "1.2.22" ? describe.concurrent : describe)(label, () => {
// Check if this is one of our docker-compose services
const services: Record<string, number> = {
"postgres_plain": 5432,
"postgres_tls": 5432,
"postgres_auth": 5432,
"mysql_plain": 3306,
"mysql_native_password": 3306,
"mysql_tls": 3306,
"mysql:8": 3306, // Map mysql:8 to mysql_plain
"mysql:9": 3306, // Map mysql:9 to mysql_native_password
"redis_plain": 6379,
"redis_unified": 6379,
"minio": 9000,
"autobahn": 9002,
};
const servicePort = services[image];
if (servicePort) {
// Map mysql:8 and mysql:9 based on environment variables
let actualService = image;
if (image === "mysql:8" || image === "mysql:9") {
if (env.MYSQL_ROOT_PASSWORD === "bun") {
actualService = "mysql_native_password"; // Has password "bun"
} else if (env.MYSQL_ALLOW_EMPTY_PASSWORD === "yes") {
actualService = "mysql_plain"; // No password
} else {
actualService = "mysql_plain"; // Default to no password
}
}
// Create a container descriptor with stable references and a ready promise
let readyResolver: () => void;
let readyRejecter: (error: any) => void;
const readyPromise = new Promise<void>((resolve, reject) => {
readyResolver = resolve;
readyRejecter = reject;
});
// Internal state that will be updated when container is ready
let _host = "127.0.0.1";
let _port = 0;
// Container descriptor with live getters and ready promise
const containerDescriptor = {
get host() {
return _host;
},
get port() {
return _port;
},
ready: readyPromise,
};
// Start the service before any tests
beforeAll(async () => {
try {
const dockerHelper = await import("./docker/index.ts");
const info = await dockerHelper.ensure(actualService as any);
_host = info.host;
_port = info.ports[servicePort];
console.log(`Container ready via docker-compose: ${image} at ${_host}:${_port}`);
readyResolver!();
} catch (error) {
readyRejecter!(error);
throw error;
}
});
fn(containerDescriptor);
return;
}
// No fallback - if the image isn't in docker-compose, it should fail
throw new Error(
`Image "${image}" is not configured in docker-compose.yml. All test containers must use docker-compose.`,
);
});
}
export function osSlashes(path: string) {
return isWindows ? path.replace(/\//g, "\\") : path;
}
import * as child_process from "node:child_process";
import { basename } from "node:path";
class WriteBlockedError extends Error {
constructor(time) {
super("Write blocked for " + (time | 0) + "ms");
this.name = "WriteBlockedError";
}
}
function failTestsOnBlockingWriteCall() {
const prop = Object.getOwnPropertyDescriptor(child_process.ChildProcess.prototype, "stdin");
const didAttachSymbol = Symbol("kDidAttach");
if (prop) {
Object.defineProperty(child_process.ChildProcess.prototype, "stdin", {
...prop,
get() {
const actual = prop.get.call(this);
if (actual?.write && !actual.__proto__[didAttachSymbol]) {
actual.__proto__[didAttachSymbol] = true;
attachWriteMeasurement(actual);
}
return actual;
},
});
}
function attachWriteMeasurement(stream) {
const prop = Object.getOwnPropertyDescriptor(stream.__proto__, "write");
if (prop) {
Object.defineProperty(stream.__proto__, "write", {
...prop,
value(chunk, encoding, cb) {
const start = performance.now();
const rc = prop.value.apply(this, arguments);
const end = performance.now();
if (end - start > 8) {
const err = new WriteBlockedError(end - start);
throw err;
}
return rc;
},
});
}
}
}
failTestsOnBlockingWriteCall();
export function dumpStats() {
const stats = heapStats();
const { objectTypeCounts, protectedObjectTypeCounts } = stats;
console.log({
objects: Object.fromEntries(Object.entries(objectTypeCounts).sort()),
protected: Object.fromEntries(Object.entries(protectedObjectTypeCounts).sort()),
});
}
export function fillRepeating(dstBuffer: NodeJS.TypedArray, start: number, end: number) {
let len = dstBuffer.length, // important: use indices length, not byte-length
sLen = end - start,
p = sLen; // set initial position = source sequence length
// step 2: copy existing data doubling segment length per iteration
while (p < len) {
if (p + sLen > len) sLen = len - p; // if not power of 2, truncate last segment
dstBuffer.copyWithin(p, start, sLen); // internal copy
p += sLen; // add current length to offset
sLen <<= 1; // double length for next segment
}
}
function makeFlatPropertyMap(opts: object) {
// return all properties of opts as paths for nested objects with dot notation
// like { a: { b: 1 } } => { "a.b": 1 }
// combining names of nested objects with dot notation
// infinitely deep
const ret: any = {};
function recurse(obj: object, path = "") {
for (const [key, value] of Object.entries(obj)) {
if (value === undefined) continue;
if (value && typeof value === "object") {
recurse(value, path ? `${path}.${key}` : key);
} else {
ret[path ? `${path}.${key}` : key] = value;
}
}
}
recurse(opts);
return ret;
}
export function toTOMLString(opts: object) {
// return a TOML string of the given options
const props = makeFlatPropertyMap(opts);
let ret = "";
for (const [key, value] of Object.entries(props)) {
if (value === undefined) continue;
ret += `${key} = ${JSON.stringify(value)}` + "\n";
}
return ret;
}
const shebang_posix = (program: string) => `#!/usr/bin/env ${program}
`;
const shebang_windows = (program: string) => `0</* :{
@echo off
${program} %~f0 %*
exit /b %errorlevel%
:} */0;
`;
export function writeShebangScript(path: string, program: string, data: string) {
if (!isWindows) {
return writeFile(path, shebang_posix(program) + "\n" + data, { mode: 0o777 });
} else {
return writeFile(path + ".cmd", shebang_windows(program) + "\n" + data);
}
}
export async function* forEachLine(iter: AsyncIterable<NodeJS.TypedArray | ArrayBufferLike>) {
var decoder = new (require("string_decoder").StringDecoder)("utf8");
var str = "";
for await (const chunk of iter) {
str += decoder.write(chunk);
let i = str.indexOf("\n");
while (i >= 0) {
yield str.slice(0, i);
str = str.slice(i + 1);
i = str.indexOf("\n");
}
}
str += decoder.end();
{
let i = str.indexOf("\n");
while (i >= 0) {
yield str.slice(0, i);
str = str.slice(i + 1);
i = str.indexOf("\n");
}
}
if (str.length > 0) {
yield str;
}
}
export function joinP(...paths: string[]) {
return join(...paths).replaceAll("\\", "/");
}
/**
* TODO: see if this is the default behavior of node child_process APIs if so,
* we need to do case-insensitive stuff within our Bun.spawn implementation
*
* Windows has case-insensitive environment variables, so sometimes an
* object like { Path: "...", PATH: "..." } will be passed. Bun lets
* the first one win, but we really want the LAST one to win.
*
* This is mostly needed if you want to override env vars, such like:
* env: {
* ...bunEnv,
* PATH: "my path override here",
* }
* becomes
* env: mergeWindowEnvs([
* bunEnv,
* {
* PATH: "my path override here",
* },
* ])
*/
export function mergeWindowEnvs(envs: Record<string, string | undefined>[]) {
const keys: Record<string, string | undefined> = {};
const flat: Record<string, string | undefined> = {};
for (const env of envs) {
for (const key in env) {
if (!env[key]) continue;
const normalized = (keys[key.toUpperCase()] ??= key);
flat[normalized] = env[key];
}
}
return flat;
}
export function tmpdirSync(pattern: string = "bun.test."): string {
return fs.mkdtempSync(join(fs.realpathSync.native(os.tmpdir()), pattern));
}
export async function runBunInstall(
env: NodeJS.Dict<string>,
cwd: string,
options: {
allowWarnings?: boolean;
allowErrors?: boolean;
expectedExitCode?: number | null;
savesLockfile?: boolean;
production?: boolean;
frozenLockfile?: boolean;
saveTextLockfile?: boolean;
packages?: string[];
verbose?: boolean;
} = {},
) {
const production = options?.production ?? false;
const args = [bunExe(), "install"];
if (options?.packages) {
args.push(...options.packages);
}
if (production) {
args.push("--production");
}
if (options?.frozenLockfile) {
args.push("--frozen-lockfile");
}
if (options?.saveTextLockfile) {
args.push("--save-text-lockfile");
}
if (options?.verbose) {
args.push("--verbose");
}
const { stdout, stderr, exited } = Bun.spawn({
cmd: args,
cwd,
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
env,
});
expect(stdout).toBeDefined();
expect(stderr).toBeDefined();
let err: string = stderrForInstall(await stderr.text());
expect(err).not.toContain("panic:");
if (!options?.allowErrors) {
expect(err).not.toContain("error:");
}
if (!options?.allowWarnings) {
expect(err).not.toContain("warn:");
}
if ((options?.savesLockfile ?? true) && !production && !options?.frozenLockfile) {
expect(err).toContain("Saved lockfile");
}
let out: string = await stdout.text();
expect(await exited).toBe(options?.expectedExitCode ?? 0);
return { out, err, exited };
}
// stderr with `slow filesystem` warning removed
export function stderrForInstall(err: string) {
return err.replace(/warn: Slow filesystem.*/g, "");
}
export async function runBunUpdate(
env: NodeJS.ProcessEnv,
cwd: string,
args?: string[],
): Promise<{ out: string[]; err: string; exitCode: number }> {
const { stdout, stderr, exited } = Bun.spawn({
cmd: [bunExe(), "update", ...(args ?? [])],
cwd,
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
env,
});
let err = await stderr.text();
let out = await stdout.text();
let exitCode = await exited;
if (exitCode !== 0) {
console.log("stdout:", out);
console.log("stderr:", err);
expect().fail("bun update failed");
}
return { out: out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/), err, exitCode };
}
export async function pack(cwd: string, env: NodeJS.Dict<string>, ...args: string[]) {
const { stdout, stderr, exited } = Bun.spawn({
cmd: [bunExe(), "pm", "pack", ...args],
cwd,
stdout: "pipe",
stderr: "pipe",
stdin: "ignore",
env,
});
const err = await stderr.text();
expect(err).not.toContain("error:");
expect(err).not.toContain("warning:");
expect(err).not.toContain("failed");
expect(err).not.toContain("panic:");
const out = await stdout.text();
const exitCode = await exited;
expect(exitCode).toBe(0);
return { out, err };
}
// If you need to modify, clone it
export const expiredTls = Object.freeze({
cert: "-----BEGIN CERTIFICATE-----\nMIIDXTCCAkWgAwIBAgIJAKLdQVPy90jjMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\naWRnaXRzIFB0eSBMdGQwHhcNMTkwMjAzMTQ0OTM1WhcNMjAwMjAzMTQ0OTM1WjBF\nMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\nZXJuZXQgV2lkZ2l0cyBQdHk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uYirUZSgjRwknmrbUi9QckRbxwOSqVeMOwOWLm1GmYaXRf39u2CTI5\nV9gTMHJ5jnKd4gYDnaA99eiOcBhgS+9PbgKSAyuUlWwR2ciL/4uDzaVeDQKBgDym\nvRSeTRn99bSQMMZuuD5N6wkD/RxeCbEnpKrw2aZVN63eGCtkj0v9LCu4gptjseOu\n7+a4Qplqw3B/SXN5/otqPbEOKv8Shl/PT6RBv06PiFKZClkEU2T3iH27sws2EGru\nw3C3GaiVMxcVewdg1YOvh5vH8ZVlxApxIzuFlDvnAoGAN5w+gukxd5QnP/7hcLDZ\nF+vesAykJX71AuqFXB4Wh/qFY92CSm7ImexWA/L9z461+NKeJwb64Nc53z59oA10\n/3o2OcIe44kddZXQVP6KTZBd7ySVhbtOiK3/pCy+BQRsrC7d71W914DxNWadwZ+a\njtwwKjDzmPwdIXDSQarCx0U=\n-----END PRIVATE KEY-----",
passphrase: "1234",
});
// openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \
// -keyout localhost.key \
// -out localhost.crt \
// -subj "/C=US/ST=CA/L=San Francisco/O=Oven/OU=Team Bun/CN=server-bun" \
// -addext "subjectAltName = DNS:localhost,IP:127.0.0.1,IP:::1"
// notAfter=Sep 4 03:00:49 2035 GMT
export const tls = Object.freeze({
cert: "-----BEGIN CERTIFICATE-----\nMIID4jCCAsqgAwIBAgIUcaRq6J/YF++Bo01Zc+HeQvCbnWMwDQYJKoZIhvcNAQEL\nBQAwaTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRYwFAYDVQQHDA1TYW4gRnJh\nbmNpc2NvMQ0wCwYDVQQKDARPdmVuMREwDwYDVQQLDAhUZWFtIEJ1bjETMBEGA1UE\nAwwKc2VydmVyLWJ1bjAeFw0yNTA5MDYwMzAwNDlaFw0zNTA5MDQwMzAwNDlaMGkx\nCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNj\nbzENMAsGA1UECgwET3ZlbjER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NP0TNxY9Uc9/xThN0r/pLDhKhh\neOCUJKIxbwIgilnjUb5U1uYaG7sTzHoY0Wvd94YWTPaFBhk/sn/mbJhRAoGAA+/E\nWZsmKdEfS2dFj0ytcS75hDSOy7fQWkGPmphvS127Pbh0v+eXr/q6+yX1NFcRBtmC\nKzs133YXsiG5Sl439Fg6oCmcPHZgxgN26cjctmtESrNcZXFrpV7XAqQ0f0+Ex/w4\nD81Cghz8JNPJyRG+plHFKXIHY6BBYMDuCMhNPpMCgYEA1BVG5scNtmBE3SaRns2G\npKgWiwmzPDTqwf3R0rgkHQroZUIz616jLgKXIVMBPaq/771uq+hzJZro9sNcNL8p\n9PkLRr4V4KtUSqkjvitU68vMM1qxtO9NVwCI5u3wicVC5mMqcH8FN+sO/5/jIPBl\nO/qEOVDlCuYtURcnh/Oz1cE=\n-----END PRIVATE KEY-----\n",
});
export function disableAggressiveGCScope() {
const gc = Bun.unsafe.gcAggressionLevel(0);
return {
[Symbol.dispose]() {
Bun.unsafe.gcAggressionLevel(gc);
},
};
}
String.prototype.isLatin1 = function () {
return require("bun:internal-for-testing").jscInternals.isLatin1String(this);
};
String.prototype.isUTF16 = function () {
return require("bun:internal-for-testing").jscInternals.isUTF16String(this);
};
interface BunHarnessTestMatchers {
toBeLatin1String(): void;
toBeUTF16String(): void;
toHaveTestTimedOutAfter(expected: number): void;
toBeBinaryType(expected: keyof typeof binaryTypes): void;
toRun(optionalStdout?: string, expectedCode?: number): void;
toThrowWithCode(cls: CallableFunction, code: string): void;
toThrowWithCodeAsync(cls: CallableFunction, code: string): Promise<void>;
}
declare module "bun:test" {
interface Matchers<T> extends BunHarnessTestMatchers {}
interface AsymmetricMatchers extends BunHarnessTestMatchers {}
}
/**
* Set `NODE_TLS_REJECT_UNAUTHORIZED` for a scope.
*/
export function rejectUnauthorizedScope(value: boolean) {
const original_rejectUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = value ? "1" : "0";
return {
[Symbol.dispose]() {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = original_rejectUnauthorized;
},
};
}
let networkInterfaces: any;
function isIP(type: "IPv4" | "IPv6") {
if (!networkInterfaces) {
networkInterfaces = os.networkInterfaces();
}
for (const networkInterface of Object.values(networkInterfaces)) {
for (const { family } of networkInterface as any[]) {
if (family === type) {
return true;
}
}
}
return false;
}
export function isIPv6() {
// FIXME: AWS instances on Linux for Buildkite are not setup with IPv6
if (isBuildKite && isLinux) {
return false;
}
return isIP("IPv6");
}
export function isIPv4() {
return isIP("IPv4");
}
let glibcVersion: string | undefined;
export function getGlibcVersion() {
if (glibcVersion || !isLinux) {
return glibcVersion;
}
try {
const { header } = process.report!.getReport() as any;
const { glibcVersionRuntime: version } = header;
if (typeof version === "string") {
return (glibcVersion = version);
}
} catch (error) {
console.warn("Failed to detect glibc version", error);
}
}
export function isGlibcVersionAtLeast(version: string): boolean {
const glibcVersion = getGlibcVersion();
if (!glibcVersion) {
return false;
}
return Bun.semver.satisfies(glibcVersion, `>=${version}`);
}
let macOSVersion: string | undefined;
export function getMacOSVersion(): string | undefined {
if (macOSVersion || !isMacOS) {
return macOSVersion;
}
try {
const { stdout } = Bun.spawnSync({
cmd: ["sw_vers", "-productVersion"],
});
return (macOSVersion = stdout.toString().trim());
} catch (error) {
console.warn("Failed to detect macOS version:", error);
}
}
export function isMacOSVersionAtLeast(minVersion: number): boolean {
const macOSVersion = getMacOSVersion();
if (!macOSVersion) {
return false;
}
return parseFloat(macOSVersion) >= minVersion;
}
export function readableStreamFromArray(array) {
return new ReadableStream({
pull(controller) {
for (let entry of array) {
controller.enqueue(entry);
}
controller.close();
},
});
}
let hasGuardMalloc = -1;
export function forceGuardMalloc(env) {
if (process.platform !== "darwin") {
return;
}
if (hasGuardMalloc === -1) {
hasGuardMalloc = Number(fs.existsSync("/usr/lib/libgmalloc.dylib"));
}
if (hasGuardMalloc === 1) {
env.DYLD_INSERT_LIBRARIES = "/usr/lib/libgmalloc.dylib";
env.MALLOC_PROTECT_BEFORE = "1";
env.MallocScribble = "1";
env.MallocGuardEdges = "1";
env.MALLOC_FILL_SPACE = "1";
env.MALLOC_STRICT_SIZE = "1";
} else {
console.warn("Guard malloc is not available on this platform for some reason.");
}
}
export function fileDescriptorLeakChecker() {
const initial = getMaxFD();
return {
[Symbol.dispose]() {
const current = getMaxFD();
if (current > initial) {
throw new Error(`File descriptor leak detected: ${current} (current) > ${initial} (initial)`);
}
},
};
}
/**
* Gets a secret from the environment.
*
* In Buildkite, secrets must be retrieved using the `buildkite-agent secret get` command
* and are not available as an environment variable.
*/
export function getSecret(name: string): string | undefined {
let value = process.env[name]?.trim();
// When not running in CI, allow the secret to be missing.
if (!isCI) {
return value;
}
// In Buildkite, secrets must be retrieved using the `buildkite-agent secret get` command
if (!value && isBuildKite) {
const { exitCode, stdout } = spawnSync({
cmd: ["buildkite-agent", "secret", "get", name],
stdout: "pipe",
env: ciEnv,
stderr: "inherit",
});
if (exitCode === 0) {
value = stdout.toString().trim();
}
}
// Throw an error if the secret is not found, so the test fails in CI.
if (!value) {
let hint;
if (isBuildKite) {
hint = `Create a secret with the name "${name}" in the Buildkite UI.
https://buildkite.com/docs/pipelines/security/secrets/buildkite-secrets`;
} else {
hint = `Define an environment variable with the name "${name}".`;
}
throw new Error(`Secret not found: ${name}\n${hint}`);
}
// Set the secret in the environment so that it can be used in tests.
process.env[name] = value;
return value;
}
export function assertManifestsPopulated(absCachePath: string, registryUrl: string) {
const { npm_manifest_test_helpers } = require("bun:internal-for-testing");
const { parseManifest } = npm_manifest_test_helpers;
for (const file of fs.readdirSync(absCachePath)) {
if (!file.endsWith(".npm")) continue;
const manifest = parseManifest(join(absCachePath, file), registryUrl);
expect(manifest.versions.length).toBeGreaterThan(0);
}
}
// Make it easier to run some node tests.
Object.defineProperty(globalThis, "gc", {
value: Bun.gc,
writable: true,
enumerable: false,
configurable: true,
});
export function waitForFileToExist(path: string, interval_ms: number) {
while (!fs.existsSync(path)) {
sleepSync(interval_ms);
}
}
export function libcPathForDlopen() {
switch (process.platform) {
case "linux":
switch (libcFamily) {
case "glibc":
return "libc.so.6";
case "musl":
return "/usr/lib/libc.so";
}
case "darwin":
return "libc.dylib";
default:
throw new Error("TODO");
}
}
export function cwdScope(cwd: string) {
const original = process.cwd();
process.chdir(cwd);
return {
[Symbol.dispose]() {
process.chdir(original);
},
};
}
export function rmScope(path: string) {
return {
[Symbol.dispose]() {
fs.rmSync(path, { recursive: true, force: true });
},
};
}
export function textLockfile(version: number, pkgs: any): string {
return JSON.stringify({
lockfileVersion: version,
configVersion: 1,
...pkgs,
});
}
export class VerdaccioRegistry {
port: number;
process: ChildProcess | undefined;
configPath: string;
packagesPath: string;
users: Record<string, string> = {};
constructor(opts?: { configPath?: string; packagesPath?: string; verbose?: boolean }) {
this.port = randomPort();
this.configPath = opts?.configPath ?? join(import.meta.dir, "cli", "install", "registry", "verdaccio.yaml");
this.packagesPath = opts?.packagesPath ?? join(import.meta.dir, "cli", "install", "registry", "packages");
}
async start(silent: boolean = true) {
await rm(join(dirname(this.configPath), "htpasswd"), { force: true });
this.process = fork(require.resolve("verdaccio/bin/verdaccio"), ["-c", this.configPath, "-l", `${this.port}`], {
silent,
// Prefer using a release build of Bun since it's faster
execPath: isCI ? bunExe() : Bun.which("bun") || bunExe(),
env: {
...(bunEnv as any),
NODE_NO_WARNINGS: "1",
},
});
this.process.stderr?.on("data", data => {
console.error(`[verdaccio] stderr: ${data}`);
});
const started = Promise.withResolvers();
this.process.on("error", error => {
console.error(`Failed to start verdaccio: ${error}`);
started.reject(error);
});
this.process.on("exit", (code, signal) => {
if (code !== 0) {
console.error(`Verdaccio exited with code ${code} and signal ${signal}`);
} else {
console.log("Verdaccio exited successfully");
}
});
this.process.on("message", (message: { verdaccio_started: boolean }) => {
if (message.verdaccio_started) {
started.resolve();
}
});
await started.promise;
}
registryUrl() {
return `http://localhost:${this.port}/`;
}
stop() {
rmSync(join(dirname(this.configPath), "htpasswd"), { force: true });
this.process?.kill(0);
}
/**
* returns auth token
*/
async generateUser(username: string, password: string): Promise<string> {
if (this.users[username]) {
throw new Error(`User ${username} already exists`);
} else this.users[username] = password;
const url = `http://localhost:${this.port}/-/user/org.couchdb.user:${username}`;
const user = {
name: username,
password: password,
email: `${username}@example.com`,
};
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(user),
});
if (response.ok) {
const data = await response.json();
return data.token;
}
throw new Error("Failed to create user:", response.statusText);
}
async authBunfig(user: string) {
const authToken = await this.generateUser(user, user);
return `
[install]
cache = false
registry = { url = "http://localhost:${this.port}/", token = "${authToken}" }
`;
}
async createTestDir(
opts: { bunfigOpts?: BunfigOpts; files?: DirectoryTree | string } = {
bunfigOpts: { linker: "hoisted" },
files: {},
},
) {
await rm(join(dirname(this.configPath), "htpasswd"), { force: true });
await rm(join(this.packagesPath, "private-pkg-dont-touch"), { force: true });
const packageDir = tempDir("verdaccio-test-", opts.files ?? {});
const packageJson = join(packageDir, "package.json");
await this.writeBunfig(packageDir, opts.bunfigOpts);
this.users = {};
return { packageDir: String(packageDir), packageJson };
}
async writeBunfig(dir: string, opts: BunfigOpts = {}) {
let bunfig = `
[install]
cache = "${join(dir, ".bun-cache").replaceAll("\\", "\\\\")}"
`;
if ("saveTextLockfile" in opts) {
bunfig += `saveTextLockfile = ${opts.saveTextLockfile}
`;
}
if (!opts.npm) {
bunfig += `registry = "${this.registryUrl()}"\n`;
}
if (opts.linker) {
bunfig += `linker = "${opts.linker}"\n`;
}
if (opts.publicHoistPattern) {
if (typeof opts.publicHoistPattern === "string") {
bunfig += `publicHoistPattern = "${opts.publicHoistPattern}"`;
} else {
bunfig += `publicHoistPattern = [${opts.publicHoistPattern.map(p => `"${p}"`).join(", ")}]`;
}
}
await write(join(dir, "bunfig.toml"), bunfig);
}
}
type BunfigOpts = {
saveTextLockfile?: boolean;
npm?: boolean;
linker?: "isolated" | "hoisted";
publicHoistPattern?: string | string[];
};
export async function readdirSorted(path: string): Promise<string[]> {
const results = await readdir(path);
results.sort();
return results;
}
/**
* Helper function for making automatically lazily-executed promises.
*
* The difference is that the promise has not already started to be evaluated when it is created,
* only when you await it does it execute the function.
*
* @example
* ```ts
* function createMyFixture() {
* return {
* start: lazyPromiseLike(() => fetch("https://example.com")),
* stop: lazyPromiseLike(() => fetch("https://example.com")),
* }
* }
*
* const { start, stop } = createMyFixture();
*
* await start; // Calls only the start function
* ```
*
* @param fn A function to make lazily evaluated.
* @returns A promise-like object that will evaluate the function when `then` is called.
*/
export function lazyPromiseLike<T>(fn: () => Promise<T>): PromiseLike<T> {
let p: Promise<T>;
return {
then(onfulfilled, onrejected) {
if (!p) {
p = fn();
}
return p.then(onfulfilled, onrejected);
},
};
}
export async function gunzipJsonRequest(req: Request) {
const buf = await req.arrayBuffer();
const inflated = Bun.gunzipSync(buf);
const body = JSON.parse(Buffer.from(inflated).toString("utf-8"));
return body;
}
/**
* HTML content from example.com, used as a test fixture to replace
* external calls to example.com with local server responses.
*/
export const exampleHtml = Buffer.from(
"PCFkb2N0eXBlIGh0bWw+CjxodG1sPgogIDxoZWFkPgogICAgPHRpdGxlPkV4YW1wbGUgRG9tYWluPC90aXRsZT4KCiAgICA8bWV0YSBjaGFyc2V0PSJ1dGYtOCIgLz4KICAgIDxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PXV0Zi04IiAvPgogICAgPG1ldGEgbmFtZT0idmlld3BvcnQiIGNvbnRlbnQ9IndpZHRoPWRldmljZS13aWR0aCwgaW5pdGlhbC1zY2FsZT0xIiAvPgogICAgPHN0eWxlIHR5cGU9InRleHQvY3NzIj4KICAgICAgYm9keSB7CiAgICAgICAgYmFja2dyb3VuZC1jb2xvcjogI2YwZjBmMjsKICAgICAgICBtYXJnaW46IDA7CiAgICAgICAgcGFkZGluZzogMDsKICAgICAgICBmb250LWZhbWlseToKICAgICAgICAgIC1hcHBsZS1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3d3LmlhbmEub3JnL2RvbWFpbnMvZXhhbXBsZSI+TW9yZSBpbmZvcm1hdGlvbi4uLjwvYT48L3A+CiAgICA8L2Rpdj4KICA8L2JvZHk+CjwvaHRtbD4K",
"base64",
).toString();
/**
* Creates a local HTTP or HTTPS server serving example.com HTML content.
* Useful for testing without external network calls.
*
* @param protocol - Protocol to use: "https" (default) or "http"
* @returns Object with server URL, optional CA cert, server instance, and cleanup methods
*
* @example
* ```ts
* const site = exampleSite("http");
* const response = await fetch(site.url);
* site.stop();
* ```
*
* @example Using disposal pattern
* ```ts
* using site = exampleSite();
* await fetch(site.url, { tls: { ca: site.ca } });
* // server automatically stopped when scope exits
* ```
*/
export function exampleSite(protocol: "https" | "http" = "https") {
const server = Bun.serve({
port: 0,
tls: protocol === "https" ? tls : undefined,
hostname: "127.0.0.1",
fetch(req) {
return new Response(exampleHtml, {
headers: {
"Content-Type": "text/html",
},
});
},
});
return {
url: server.url,
ca: protocol === "https" ? tls.cert : undefined,
server,
stop() {
return server.stop();
},
async [Symbol.asyncDispose]() {
await server.stop();
},
};
}
export function normalizeBunSnapshot(snapshot: string, optionalDir?: string) {
if (optionalDir) {
snapshot = snapshot
.replaceAll(fs.realpathSync.native(optionalDir).replaceAll("\\", "/"), "<dir>")
.replaceAll(optionalDir, "<dir>");
}
// Remove timestamps from test result lines that start with (pass), (fail), (skip), or (todo)
snapshot = snapshot.replace(/^((?:pass|fail|skip|todo)\) .+) \[[\d.]+\s?m?s\]$/gm, "$1");
return (
snapshot
.replaceAll("\r\n", "\n")
.replaceAll("\\", "/")
.replaceAll(fs.realpathSync.native(process.cwd()).replaceAll("\\", "/"), "<cwd>")
.replaceAll(fs.realpathSync.native(os.tmpdir()).replaceAll("\\", "/"), "<tmp>")
.replaceAll(fs.realpathSync.native(os.homedir()).replaceAll("\\", "/"), "<home>")
// look for [\d\d ms] or [\d\d s] with optional periods
.replace(/\s\[[\d.]+\s?m?s\]/gm, "")
.replace(/^\[[\d.]+\s?m?s\]/gm, "")
// line numbers in stack traces like at FunctionName (NN:NN)
// it must specifically look at the stacktrace format
.replace(/^\s+at (.*?)\(.*?:\d+(?::\d+)?\)/gm, " at $1(file:NN:NN)")
// Handle version strings in error messages like "Bun v1.2.21+revision (platform arch)"
// This needs to come before the other version replacements
.replace(/Bun v[\d.]+(?:-[\w.]+)?(?:\+[\w]+)?(?:\s+\([^)]+\))?/g, "Bun v<bun-version>")
.replaceAll(Bun.version_with_sha, "<version> (<revision>)")
.replaceAll(Bun.version, "<bun-version>")
.replaceAll(Bun.revision, "<revision>")
.trim()
);
}
export function nodeModulesPackages(nodeModulesPath: string): string {
const packages: string[] = [];
function scanDirectory(dir: string, relativePath: string = "") {
try {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name === ".bun-cache") {
// Skip .bun-cache directories
continue;
}
const newRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
// Check if this directory contains a package.json
const packageJsonPath = join(fullPath, "package.json");
if (fs.existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
const name = packageJson.name || "unknown";
const version = packageJson.version || "unknown";
packages.push(`${newRelativePath}/${name}@${version}`);
} catch {
// If package.json is invalid, still include the path
packages.push(`${newRelativePath}/[invalid-package.json]`);
}
}
// Recursively scan subdirectories (including nested node_modules)
scanDirectory(fullPath, newRelativePath);
}
}
} catch (err) {
// Ignore errors for directories we can't read
}
}
scanDirectory(nodeModulesPath);
// Sort the packages alphabetically
packages.sort();
return packages.join("\n");
}