vitest 4.0.7 → 4.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +2 -2
- package/dist/chunks/{base.D3GxgUMI.js → base.CiIV2DDC.js} +71 -36
- package/dist/chunks/{benchmark.DHKMYAts.js → benchmark.B3N2zMcH.js} +9 -4
- package/dist/chunks/{browser.d.-LKfRopd.d.ts → browser.d.DnU_kh8a.d.ts} +1 -1
- package/dist/chunks/{cac.G9DAn-c7.js → cac.B_NTJoIH.js} +115 -42
- package/dist/chunks/{cli-api.Csks4as1.js → cli-api.D48wY175.js} +1863 -845
- package/dist/chunks/console.Cf-YriPC.js +146 -0
- package/dist/chunks/{coverage.C2LA1DSL.js → coverage.BUlIqJrL.js} +284 -114
- package/dist/chunks/{creator.cqqifzG7.js → creator.BzqvXeRE.js} +75 -31
- package/dist/chunks/{date.-jtEtIeV.js → date.Bq6ZW5rf.js} +17 -6
- package/dist/chunks/{git.BFNcloKD.js → git.Bm2pzPAa.js} +3 -3
- package/dist/chunks/{global.d.DxtanrNO.d.ts → global.d.BQDgW9Pr.d.ts} +1 -1
- package/dist/chunks/{globals.BGT_RUsD.js → globals.DBrtKPdh.js} +5 -5
- package/dist/chunks/index.0kCJoeWi.js +220 -0
- package/dist/chunks/{index.CWIFvlX5.js → index.BfmpdV5p.js} +165 -54
- package/dist/chunks/{index.RwjEGCQ0.js → index.CGezRSGU.js} +2 -2
- package/dist/chunks/{index.DEPqWSIZ.js → index.CPA8jGhR.js} +33 -16
- package/dist/chunks/{index.CVpyv-Zg.js → index.kotH7DY7.js} +832 -373
- package/dist/chunks/{index.jMQYiEWE.js → index.op2Re5rn.js} +22 -12
- package/dist/chunks/{index.Dc3xnDvT.js → index.z7NPOg2E.js} +4 -4
- package/dist/chunks/{init-forks.IU-xQ2_X.js → init-forks.aqTzCSR2.js} +14 -4
- package/dist/chunks/{init-threads.C_NWvZkU.js → init-threads.C7T0-YMD.js} +1 -1
- package/dist/chunks/{init.fmH9J833.js → init.BQhNfT0h.js} +53 -30
- package/dist/chunks/{inspector.DLZxSeU3.js → inspector.CvyFGlXm.js} +25 -10
- package/dist/chunks/{moduleRunner.d.DEkTotCv.d.ts → moduleRunner.d.BxT-OjLR.d.ts} +1 -1
- package/dist/chunks/{node.BwAWWjHZ.js → node.Ce0vMQM7.js} +1 -1
- package/dist/chunks/{plugin.d.Cpes8Bt6.d.ts → plugin.d.DevON6kQ.d.ts} +1 -1
- package/dist/chunks/{reporters.d.CSNcMDxF.d.ts → reporters.d.BQ0wpUaj.d.ts} +6 -5
- package/dist/chunks/{rpc.D38ahn14.js → rpc.BytlcPfC.js} +20 -7
- package/dist/chunks/{setup-common.DR1sucx6.js → setup-common.Dw1XgX0v.js} +20 -8
- package/dist/chunks/{startModuleRunner.Cn7hCL7D.js → startModuleRunner.DLjmA_wU.js} +209 -86
- package/dist/chunks/{test.B6aJd6T3.js → test.w5HLbjmU.js} +48 -22
- package/dist/chunks/{utils.CG9h5ccR.js → utils.DvEY5TfP.js} +14 -5
- package/dist/chunks/{vi.BZvkKVkM.js → vi.CyIUVSoU.js} +267 -117
- package/dist/chunks/{vm.BL7_zzOr.js → vm.DXN8eCh2.js} +181 -75
- package/dist/chunks/{worker.d.D25zYZ7N.d.ts → worker.d.ZGohxCEd.d.ts} +74 -7
- package/dist/cli.js +2 -2
- package/dist/config.d.ts +5 -5
- package/dist/coverage.d.ts +3 -3
- package/dist/coverage.js +1 -1
- package/dist/environments.js +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -5
- package/dist/module-evaluator.d.ts +2 -2
- package/dist/module-evaluator.js +88 -38
- package/dist/module-runner.js +2 -2
- package/dist/node.d.ts +7 -7
- package/dist/node.js +17 -13
- package/dist/reporters.d.ts +3 -3
- package/dist/reporters.js +2 -2
- package/dist/runners.js +7 -7
- package/dist/snapshot.js +2 -2
- package/dist/suite.js +2 -2
- package/dist/worker.d.ts +1 -1
- package/dist/worker.js +15 -15
- package/dist/workers/forks.js +16 -16
- package/dist/workers/runVmTests.js +41 -22
- package/dist/workers/threads.js +16 -16
- package/dist/workers/vmForks.js +11 -11
- package/dist/workers/vmThreads.js +11 -11
- package/package.json +20 -20
- package/dist/chunks/console.CTJL2nuH.js +0 -115
- package/dist/chunks/index.Bgo3tNWt.js +0 -176
|
@@ -2,7 +2,7 @@ import { existsSync, writeFileSync, readFileSync } from 'node:fs';
|
|
|
2
2
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { resolve, dirname, relative } from 'node:path';
|
|
4
4
|
import { detectPackageManager, installPackage } from './index.D3XRDfWc.js';
|
|
5
|
-
import { p as prompt, a as any } from './index.
|
|
5
|
+
import { p as prompt, a as any } from './index.z7NPOg2E.js';
|
|
6
6
|
import { x } from 'tinyexec';
|
|
7
7
|
import c from 'tinyrainbow';
|
|
8
8
|
import { c as configFiles } from './constants.D_Q9UYh-.js';
|
|
@@ -43,7 +43,8 @@ test('renders name', async () => {
|
|
|
43
43
|
await expect.element(getByText('Hello Vitest!')).toBeInTheDocument()
|
|
44
44
|
})
|
|
45
45
|
`
|
|
46
|
-
}
|
|
46
|
+
};
|
|
47
|
+
const vueExample = {
|
|
47
48
|
name: "HelloWorld.vue",
|
|
48
49
|
js: `
|
|
49
50
|
<script setup>
|
|
@@ -83,7 +84,8 @@ test('renders name', async () => {
|
|
|
83
84
|
await expect.element(getByText('Hello Vitest!')).toBeInTheDocument()
|
|
84
85
|
})
|
|
85
86
|
`
|
|
86
|
-
}
|
|
87
|
+
};
|
|
88
|
+
const svelteExample = {
|
|
87
89
|
name: "HelloWorld.svelte",
|
|
88
90
|
js: `
|
|
89
91
|
<script>
|
|
@@ -109,7 +111,8 @@ test('renders name', async () => {
|
|
|
109
111
|
await expect.element(getByText('Hello Vitest!')).toBeInTheDocument()
|
|
110
112
|
})
|
|
111
113
|
`
|
|
112
|
-
}
|
|
114
|
+
};
|
|
115
|
+
const markoExample = {
|
|
113
116
|
name: "HelloWorld.marko",
|
|
114
117
|
js: `
|
|
115
118
|
class {
|
|
@@ -138,7 +141,8 @@ test('renders name', async () => {
|
|
|
138
141
|
expect(element).toBeInTheDocument()
|
|
139
142
|
})
|
|
140
143
|
`
|
|
141
|
-
}
|
|
144
|
+
};
|
|
145
|
+
const litExample = {
|
|
142
146
|
name: "HelloWorld.js",
|
|
143
147
|
js: `
|
|
144
148
|
import { html, LitElement } from 'lit'
|
|
@@ -192,7 +196,8 @@ test('renders name', async () => {
|
|
|
192
196
|
await expect.element(element).toBeInTheDocument()
|
|
193
197
|
})
|
|
194
198
|
`
|
|
195
|
-
}
|
|
199
|
+
};
|
|
200
|
+
const qwikExample = {
|
|
196
201
|
name: "HelloWorld.jsx",
|
|
197
202
|
js: `
|
|
198
203
|
import { component$ } from '@builder.io/qwik'
|
|
@@ -226,7 +231,8 @@ test('renders name', async () => {
|
|
|
226
231
|
await expect.element(getByText('Hello Vitest!')).toBeInTheDocument()
|
|
227
232
|
})
|
|
228
233
|
`
|
|
229
|
-
}
|
|
234
|
+
};
|
|
235
|
+
const vanillaExample = {
|
|
230
236
|
name: "HelloWorld.js",
|
|
231
237
|
js: `
|
|
232
238
|
export default function HelloWorld({ name }) {
|
|
@@ -286,14 +292,18 @@ function getExampleTest(framework) {
|
|
|
286
292
|
async function generateExampleFiles(framework, lang) {
|
|
287
293
|
const example = getExampleTest(framework);
|
|
288
294
|
let fileName = example.name;
|
|
289
|
-
const folder = resolve(process.cwd(), "vitest-example")
|
|
295
|
+
const folder = resolve(process.cwd(), "vitest-example");
|
|
296
|
+
const fileContent = example[lang];
|
|
290
297
|
if (!existsSync(folder)) await mkdir(folder, { recursive: true });
|
|
291
298
|
const isJSX = fileName.endsWith(".jsx");
|
|
292
299
|
if (isJSX && lang === "ts") fileName = fileName.replace(".jsx", ".tsx");
|
|
293
300
|
else if (fileName.endsWith(".js") && lang === "ts") fileName = fileName.replace(".js", ".ts");
|
|
294
301
|
example.test = example.test.replace("<EXT>", lang);
|
|
295
|
-
const filePath = resolve(folder, fileName)
|
|
296
|
-
|
|
302
|
+
const filePath = resolve(folder, fileName);
|
|
303
|
+
const testPath = resolve(folder, `HelloWorld.test.${isJSX ? `${lang}x` : lang}`);
|
|
304
|
+
writeFileSync(filePath, fileContent.trimStart(), "utf-8");
|
|
305
|
+
writeFileSync(testPath, example.test.trimStart(), "utf-8");
|
|
306
|
+
return testPath;
|
|
297
307
|
}
|
|
298
308
|
|
|
299
309
|
// eslint-disable-next-line no-console
|
|
@@ -422,7 +432,10 @@ async function installPackages(pkgManager, packages) {
|
|
|
422
432
|
log(c.green("✔"), c.bold("All packages are already installed."));
|
|
423
433
|
return;
|
|
424
434
|
}
|
|
425
|
-
log(c.cyan("◼"), c.bold("Installing packages..."))
|
|
435
|
+
log(c.cyan("◼"), c.bold("Installing packages..."));
|
|
436
|
+
log(c.cyan("◼"), packages.join(", "));
|
|
437
|
+
log();
|
|
438
|
+
await installPackage(packages, {
|
|
426
439
|
dev: true,
|
|
427
440
|
packageManager: pkgManager ?? void 0
|
|
428
441
|
});
|
|
@@ -433,19 +446,27 @@ function readPkgJson(path) {
|
|
|
433
446
|
return JSON.parse(content);
|
|
434
447
|
}
|
|
435
448
|
function getPossibleDefaults(dependencies) {
|
|
436
|
-
const provider = getPossibleProvider(dependencies), framework = getPossibleFramework(dependencies);
|
|
437
449
|
return {
|
|
438
450
|
lang: "ts",
|
|
439
|
-
provider,
|
|
440
|
-
framework
|
|
451
|
+
provider: getPossibleProvider(dependencies),
|
|
452
|
+
framework: getPossibleFramework(dependencies)
|
|
441
453
|
};
|
|
442
454
|
}
|
|
443
455
|
function getPossibleFramework(dependencies) {
|
|
444
|
-
|
|
456
|
+
if (dependencies.vue || dependencies["vue-tsc"] || dependencies["@vue/reactivity"]) return "vue";
|
|
457
|
+
if (dependencies.react || dependencies["react-dom"]) return "react";
|
|
458
|
+
if (dependencies.svelte || dependencies["@sveltejs/kit"]) return "svelte";
|
|
459
|
+
if (dependencies.lit || dependencies["lit-html"]) return "lit";
|
|
460
|
+
if (dependencies.preact) return "preact";
|
|
461
|
+
if (dependencies["solid-js"] || dependencies["@solidjs/start"]) return "solid";
|
|
462
|
+
if (dependencies.marko) return "marko";
|
|
463
|
+
if (dependencies["@builder.io/qwik"] || dependencies["@qwik.dev/core"]) return "qwik";
|
|
464
|
+
return "vanilla";
|
|
445
465
|
}
|
|
446
466
|
function getPossibleProvider(dependencies) {
|
|
467
|
+
if (dependencies.webdriverio || dependencies["@wdio/cli"] || dependencies["@wdio/config"]) return "webdriverio";
|
|
447
468
|
// playwright is the default recommendation
|
|
448
|
-
return
|
|
469
|
+
return "playwright";
|
|
449
470
|
}
|
|
450
471
|
function getProviderDocsLink(provider) {
|
|
451
472
|
switch (provider) {
|
|
@@ -455,7 +476,8 @@ function getProviderDocsLink(provider) {
|
|
|
455
476
|
}
|
|
456
477
|
function sort(choices, value) {
|
|
457
478
|
const index = choices.findIndex((i) => i.value === value);
|
|
458
|
-
|
|
479
|
+
if (index === -1) return choices;
|
|
480
|
+
return [choices.splice(index, 1)[0], ...choices];
|
|
459
481
|
}
|
|
460
482
|
function fail() {
|
|
461
483
|
process.exitCode = 1;
|
|
@@ -477,7 +499,9 @@ function getFrameworkImportInfo(framework) {
|
|
|
477
499
|
}
|
|
478
500
|
}
|
|
479
501
|
async function generateFrameworkConfigFile(options) {
|
|
480
|
-
const { importName, isNamedExport } = getFrameworkImportInfo(options.framework)
|
|
502
|
+
const { importName, isNamedExport } = getFrameworkImportInfo(options.framework);
|
|
503
|
+
const frameworkImport = isNamedExport ? `import { ${importName} } from '${options.frameworkPlugin}'` : `import ${importName} from '${options.frameworkPlugin}'`;
|
|
504
|
+
const configContent = [
|
|
481
505
|
`import { defineConfig } from 'vitest/config'`,
|
|
482
506
|
`import { ${options.provider} } from '@vitest/browser-${options.provider}'`,
|
|
483
507
|
options.frameworkPlugin ? frameworkImport : null,
|
|
@@ -505,7 +529,9 @@ async function updatePkgJsonScripts(pkgJsonPath, vitestScript) {
|
|
|
505
529
|
await writeFile(pkgJsonPath, `${JSON.stringify(pkg, null, 2)}\n`, "utf-8");
|
|
506
530
|
} else {
|
|
507
531
|
const pkg = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));
|
|
508
|
-
pkg.scripts = pkg.scripts || {}
|
|
532
|
+
pkg.scripts = pkg.scripts || {};
|
|
533
|
+
pkg.scripts["test:browser"] = vitestScript;
|
|
534
|
+
await writeFile(pkgJsonPath, `${JSON.stringify(pkg, null, 2)}\n`, "utf-8");
|
|
509
535
|
}
|
|
510
536
|
log(c.green("✔"), "Added \"test:browser\" script to your package.json.");
|
|
511
537
|
}
|
|
@@ -531,10 +557,14 @@ function getPlaywrightRunArgs(pkgManager) {
|
|
|
531
557
|
}
|
|
532
558
|
async function create() {
|
|
533
559
|
log(c.cyan("◼"), "This utility will help you set up a browser testing environment.\n");
|
|
534
|
-
const pkgJsonPath = resolve(process.cwd(), "package.json")
|
|
560
|
+
const pkgJsonPath = resolve(process.cwd(), "package.json");
|
|
561
|
+
const pkg = readPkgJson(pkgJsonPath) || {};
|
|
562
|
+
const dependencies = {
|
|
535
563
|
...pkg.dependencies,
|
|
536
564
|
...pkg.devDependencies
|
|
537
|
-
}
|
|
565
|
+
};
|
|
566
|
+
const defaults = getPossibleDefaults(dependencies);
|
|
567
|
+
const { lang } = await prompt({
|
|
538
568
|
type: "select",
|
|
539
569
|
name: "lang",
|
|
540
570
|
message: "Choose a language for your tests",
|
|
@@ -572,23 +602,28 @@ async function create() {
|
|
|
572
602
|
message: `Install Playwright browsers (can be done manually via 'pnpm exec playwright install')?`
|
|
573
603
|
}));
|
|
574
604
|
if (installPlaywright == null) return fail();
|
|
575
|
-
const dependenciesToInstall = [`@vitest/browser-${provider}`]
|
|
605
|
+
const dependenciesToInstall = [`@vitest/browser-${provider}`];
|
|
606
|
+
const frameworkPackage = getFrameworkTestPackage(framework);
|
|
576
607
|
if (frameworkPackage) dependenciesToInstall.push(frameworkPackage);
|
|
577
608
|
const frameworkPlugin = getFrameworkPluginPackage(framework);
|
|
578
609
|
if (frameworkPlugin) dependenciesToInstall.push(frameworkPlugin);
|
|
579
610
|
const pkgManager = await detectPackageManager();
|
|
580
|
-
log()
|
|
611
|
+
log();
|
|
612
|
+
await installPackages(pkgManager, dependenciesToInstall.filter((pkg) => !dependencies[pkg]));
|
|
581
613
|
const rootConfig = any(configFiles, { cwd: process.cwd() });
|
|
582
614
|
let scriptCommand = "vitest";
|
|
583
|
-
|
|
615
|
+
log();
|
|
616
|
+
if (rootConfig) {
|
|
584
617
|
const configPath = resolve(dirname(rootConfig), `vitest.browser.config.${lang}`);
|
|
585
|
-
scriptCommand = `vitest --config=${relative(process.cwd(), configPath)}
|
|
618
|
+
scriptCommand = `vitest --config=${relative(process.cwd(), configPath)}`;
|
|
619
|
+
await generateFrameworkConfigFile({
|
|
586
620
|
configPath,
|
|
587
621
|
framework,
|
|
588
622
|
frameworkPlugin,
|
|
589
623
|
provider,
|
|
590
624
|
browsers
|
|
591
|
-
})
|
|
625
|
+
});
|
|
626
|
+
log(
|
|
592
627
|
c.green("✔"),
|
|
593
628
|
"Created a new config file for browser tests:",
|
|
594
629
|
c.bold(relative(process.cwd(), configPath)),
|
|
@@ -605,17 +640,23 @@ async function create() {
|
|
|
605
640
|
frameworkPlugin,
|
|
606
641
|
provider,
|
|
607
642
|
browsers
|
|
608
|
-
})
|
|
643
|
+
});
|
|
644
|
+
log(c.green("✔"), "Created a config file for browser tests:", c.bold(relative(process.cwd(), configPath)));
|
|
609
645
|
}
|
|
610
|
-
|
|
646
|
+
log();
|
|
647
|
+
await updatePkgJsonScripts(pkgJsonPath, scriptCommand);
|
|
648
|
+
if (installPlaywright) {
|
|
611
649
|
log();
|
|
612
|
-
const [command, ...args] = getPlaywrightRunArgs(pkgManager)
|
|
650
|
+
const [command, ...args] = getPlaywrightRunArgs(pkgManager);
|
|
651
|
+
const allArgs = [
|
|
613
652
|
...args,
|
|
614
653
|
"playwright",
|
|
615
654
|
"install",
|
|
616
655
|
"--with-deps"
|
|
617
656
|
];
|
|
618
|
-
log(c.cyan("◼"), `Installing Playwright dependencies with \`${c.bold(command)} ${c.bold(allArgs.join(" "))}\`...`)
|
|
657
|
+
log(c.cyan("◼"), `Installing Playwright dependencies with \`${c.bold(command)} ${c.bold(allArgs.join(" "))}\`...`);
|
|
658
|
+
log();
|
|
659
|
+
await x(command, allArgs, { nodeOptions: { stdio: [
|
|
619
660
|
"pipe",
|
|
620
661
|
"inherit",
|
|
621
662
|
"inherit"
|
|
@@ -623,7 +664,10 @@ async function create() {
|
|
|
623
664
|
}
|
|
624
665
|
log();
|
|
625
666
|
const exampleTestFile = await generateExampleFiles(framework, lang);
|
|
626
|
-
log(c.green("✔"), "Created example test file in", c.bold(relative(process.cwd(), exampleTestFile)))
|
|
667
|
+
log(c.green("✔"), "Created example test file in", c.bold(relative(process.cwd(), exampleTestFile)));
|
|
668
|
+
log(c.dim(" You can safely delete this file once you have written your own tests."));
|
|
669
|
+
log();
|
|
670
|
+
log(c.cyan("◼"), "All done! Run your tests with", c.bold(getRunScript(pkgManager)));
|
|
627
671
|
}
|
|
628
672
|
|
|
629
673
|
export { create };
|
|
@@ -37,23 +37,34 @@ class MockDate extends RealDate {
|
|
|
37
37
|
date = new RealDate(y);
|
|
38
38
|
break;
|
|
39
39
|
default:
|
|
40
|
-
d = typeof d === "undefined" ? 1 : d
|
|
40
|
+
d = typeof d === "undefined" ? 1 : d;
|
|
41
|
+
h = h || 0;
|
|
42
|
+
M = M || 0;
|
|
43
|
+
s = s || 0;
|
|
44
|
+
ms = ms || 0;
|
|
45
|
+
date = new RealDate(y, m, d, h, M, s, ms);
|
|
41
46
|
break;
|
|
42
47
|
}
|
|
43
|
-
|
|
48
|
+
Object.setPrototypeOf(date, MockDate.prototype);
|
|
49
|
+
return date;
|
|
44
50
|
}
|
|
45
51
|
}
|
|
46
|
-
MockDate.UTC = RealDate.UTC
|
|
52
|
+
MockDate.UTC = RealDate.UTC;
|
|
53
|
+
MockDate.now = function() {
|
|
47
54
|
return new MockDate().valueOf();
|
|
48
|
-
}
|
|
55
|
+
};
|
|
56
|
+
MockDate.parse = function(dateString) {
|
|
49
57
|
return RealDate.parse(dateString);
|
|
50
|
-
}
|
|
58
|
+
};
|
|
59
|
+
MockDate.toString = function() {
|
|
51
60
|
return RealDate.toString();
|
|
52
61
|
};
|
|
53
62
|
function mockDate(date) {
|
|
54
63
|
const dateObj = new RealDate(date.valueOf());
|
|
55
64
|
if (Number.isNaN(dateObj.getTime())) throw new TypeError(`mockdate: The time set is an invalid date: ${date}`);
|
|
56
|
-
|
|
65
|
+
// @ts-expect-error global
|
|
66
|
+
globalThis.Date = MockDate;
|
|
67
|
+
now = dateObj.valueOf();
|
|
57
68
|
}
|
|
58
69
|
function resetDate() {
|
|
59
70
|
globalThis.Date = RealDate;
|
|
@@ -11,7 +11,8 @@ class VitestGit {
|
|
|
11
11
|
try {
|
|
12
12
|
result = await x("git", args, { nodeOptions: { cwd: this.root } });
|
|
13
13
|
} catch (e) {
|
|
14
|
-
|
|
14
|
+
e.message = e.stderr;
|
|
15
|
+
throw e;
|
|
15
16
|
}
|
|
16
17
|
return result.stdout.split("\n").filter((s) => s !== "").map((changedPath) => resolve(this.root, changedPath));
|
|
17
18
|
}
|
|
@@ -60,8 +61,7 @@ class VitestGit {
|
|
|
60
61
|
async getRoot(cwd) {
|
|
61
62
|
const args = ["rev-parse", "--show-cdup"];
|
|
62
63
|
try {
|
|
63
|
-
|
|
64
|
-
return resolve(cwd, result.stdout.trim());
|
|
64
|
+
return resolve(cwd, (await x("git", args, { nodeOptions: { cwd } })).stdout.trim());
|
|
65
65
|
} catch {
|
|
66
66
|
return null;
|
|
67
67
|
}
|
|
@@ -2,7 +2,7 @@ import { PromisifyAssertion, Tester, ExpectStatic } from '@vitest/expect';
|
|
|
2
2
|
import { Plugin } from '@vitest/pretty-format';
|
|
3
3
|
import { SnapshotState } from '@vitest/snapshot';
|
|
4
4
|
import { B as BenchmarkResult } from './benchmark.d.DAaHLpsq.js';
|
|
5
|
-
import { U as UserConsoleLog } from './worker.d.
|
|
5
|
+
import { U as UserConsoleLog } from './worker.d.ZGohxCEd.js';
|
|
6
6
|
|
|
7
7
|
interface SnapshotMatcher<T> {
|
|
8
8
|
<U extends { [P in keyof T] : any }>(snapshot: Partial<U>, hint?: string): void;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { g as globalApis } from './constants.D_Q9UYh-.js';
|
|
2
|
-
import { i as index } from './index.
|
|
3
|
-
import './vi.
|
|
2
|
+
import { i as index } from './index.CGezRSGU.js';
|
|
3
|
+
import './vi.CyIUVSoU.js';
|
|
4
4
|
import '@vitest/expect';
|
|
5
5
|
import '@vitest/runner';
|
|
6
6
|
import '@vitest/runner/utils';
|
|
7
|
-
import './utils.
|
|
7
|
+
import './utils.DvEY5TfP.js';
|
|
8
8
|
import '@vitest/utils/timers';
|
|
9
9
|
import '@vitest/snapshot';
|
|
10
10
|
import '@vitest/utils/error';
|
|
@@ -13,8 +13,8 @@ import '@vitest/spy';
|
|
|
13
13
|
import '@vitest/utils/offset';
|
|
14
14
|
import '@vitest/utils/source-map';
|
|
15
15
|
import './_commonjsHelpers.BFTU3MAI.js';
|
|
16
|
-
import './date
|
|
17
|
-
import './benchmark.
|
|
16
|
+
import './date.Bq6ZW5rf.js';
|
|
17
|
+
import './benchmark.B3N2zMcH.js';
|
|
18
18
|
import './evaluatedModules.Dg1zASAC.js';
|
|
19
19
|
import 'pathe';
|
|
20
20
|
import 'vite/module-runner';
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
const TYPE_REQUEST = "q";
|
|
2
|
+
const TYPE_RESPONSE = "s";
|
|
3
|
+
const DEFAULT_TIMEOUT = 6e4;
|
|
4
|
+
function defaultSerialize(i) {
|
|
5
|
+
return i;
|
|
6
|
+
}
|
|
7
|
+
const defaultDeserialize = defaultSerialize;
|
|
8
|
+
const { clearTimeout, setTimeout } = globalThis;
|
|
9
|
+
const random = Math.random.bind(Math);
|
|
10
|
+
function createBirpc($functions, options) {
|
|
11
|
+
const {
|
|
12
|
+
post,
|
|
13
|
+
on,
|
|
14
|
+
off = () => {
|
|
15
|
+
},
|
|
16
|
+
eventNames = [],
|
|
17
|
+
serialize = defaultSerialize,
|
|
18
|
+
deserialize = defaultDeserialize,
|
|
19
|
+
resolver,
|
|
20
|
+
bind = "rpc",
|
|
21
|
+
timeout = DEFAULT_TIMEOUT
|
|
22
|
+
} = options;
|
|
23
|
+
let $closed = false;
|
|
24
|
+
const _rpcPromiseMap = /* @__PURE__ */ new Map();
|
|
25
|
+
let _promiseInit;
|
|
26
|
+
async function _call(method, args, event, optional) {
|
|
27
|
+
if ($closed)
|
|
28
|
+
throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
|
|
29
|
+
const req = { m: method, a: args, t: TYPE_REQUEST };
|
|
30
|
+
if (optional)
|
|
31
|
+
req.o = true;
|
|
32
|
+
const send = async (_req) => post(serialize(_req));
|
|
33
|
+
if (event) {
|
|
34
|
+
await send(req);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (_promiseInit) {
|
|
38
|
+
try {
|
|
39
|
+
await _promiseInit;
|
|
40
|
+
} finally {
|
|
41
|
+
_promiseInit = void 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
let { promise, resolve, reject } = createPromiseWithResolvers();
|
|
45
|
+
const id = nanoid();
|
|
46
|
+
req.i = id;
|
|
47
|
+
let timeoutId;
|
|
48
|
+
async function handler(newReq = req) {
|
|
49
|
+
if (timeout >= 0) {
|
|
50
|
+
timeoutId = setTimeout(() => {
|
|
51
|
+
try {
|
|
52
|
+
const handleResult = options.onTimeoutError?.(method, args);
|
|
53
|
+
if (handleResult !== true)
|
|
54
|
+
throw new Error(`[birpc] timeout on calling "${method}"`);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
reject(e);
|
|
57
|
+
}
|
|
58
|
+
_rpcPromiseMap.delete(id);
|
|
59
|
+
}, timeout);
|
|
60
|
+
if (typeof timeoutId === "object")
|
|
61
|
+
timeoutId = timeoutId.unref?.();
|
|
62
|
+
}
|
|
63
|
+
_rpcPromiseMap.set(id, { resolve, reject, timeoutId, method });
|
|
64
|
+
await send(newReq);
|
|
65
|
+
return promise;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
if (options.onRequest)
|
|
69
|
+
await options.onRequest(req, handler, resolve);
|
|
70
|
+
else
|
|
71
|
+
await handler();
|
|
72
|
+
} catch (e) {
|
|
73
|
+
if (options.onGeneralError?.(e) !== true)
|
|
74
|
+
throw e;
|
|
75
|
+
return;
|
|
76
|
+
} finally {
|
|
77
|
+
clearTimeout(timeoutId);
|
|
78
|
+
_rpcPromiseMap.delete(id);
|
|
79
|
+
}
|
|
80
|
+
return promise;
|
|
81
|
+
}
|
|
82
|
+
const $call = (method, ...args) => _call(method, args, false);
|
|
83
|
+
const $callOptional = (method, ...args) => _call(method, args, false, true);
|
|
84
|
+
const $callEvent = (method, ...args) => _call(method, args, true);
|
|
85
|
+
const $callRaw = (options2) => _call(options2.method, options2.args, options2.event, options2.optional);
|
|
86
|
+
const builtinMethods = {
|
|
87
|
+
$call,
|
|
88
|
+
$callOptional,
|
|
89
|
+
$callEvent,
|
|
90
|
+
$callRaw,
|
|
91
|
+
$rejectPendingCalls,
|
|
92
|
+
get $closed() {
|
|
93
|
+
return $closed;
|
|
94
|
+
},
|
|
95
|
+
$close,
|
|
96
|
+
$functions
|
|
97
|
+
};
|
|
98
|
+
const rpc = new Proxy({}, {
|
|
99
|
+
get(_, method) {
|
|
100
|
+
if (Object.prototype.hasOwnProperty.call(builtinMethods, method))
|
|
101
|
+
return builtinMethods[method];
|
|
102
|
+
if (method === "then" && !eventNames.includes("then") && !("then" in $functions))
|
|
103
|
+
return void 0;
|
|
104
|
+
const sendEvent = (...args) => _call(method, args, true);
|
|
105
|
+
if (eventNames.includes(method)) {
|
|
106
|
+
sendEvent.asEvent = sendEvent;
|
|
107
|
+
return sendEvent;
|
|
108
|
+
}
|
|
109
|
+
const sendCall = (...args) => _call(method, args, false);
|
|
110
|
+
sendCall.asEvent = sendEvent;
|
|
111
|
+
return sendCall;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
function $close(customError) {
|
|
115
|
+
$closed = true;
|
|
116
|
+
_rpcPromiseMap.forEach(({ reject, method }) => {
|
|
117
|
+
const error = new Error(`[birpc] rpc is closed, cannot call "${method}"`);
|
|
118
|
+
if (customError) {
|
|
119
|
+
customError.cause ??= error;
|
|
120
|
+
return reject(customError);
|
|
121
|
+
}
|
|
122
|
+
reject(error);
|
|
123
|
+
});
|
|
124
|
+
_rpcPromiseMap.clear();
|
|
125
|
+
off(onMessage);
|
|
126
|
+
}
|
|
127
|
+
function $rejectPendingCalls(handler) {
|
|
128
|
+
const entries = Array.from(_rpcPromiseMap.values());
|
|
129
|
+
const handlerResults = entries.map(({ method, reject }) => {
|
|
130
|
+
if (!handler) {
|
|
131
|
+
return reject(new Error(`[birpc]: rejected pending call "${method}".`));
|
|
132
|
+
}
|
|
133
|
+
return handler({ method, reject });
|
|
134
|
+
});
|
|
135
|
+
_rpcPromiseMap.clear();
|
|
136
|
+
return handlerResults;
|
|
137
|
+
}
|
|
138
|
+
async function onMessage(data, ...extra) {
|
|
139
|
+
let msg;
|
|
140
|
+
try {
|
|
141
|
+
msg = deserialize(data);
|
|
142
|
+
} catch (e) {
|
|
143
|
+
if (options.onGeneralError?.(e) !== true)
|
|
144
|
+
throw e;
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (msg.t === TYPE_REQUEST) {
|
|
148
|
+
const { m: method, a: args, o: optional } = msg;
|
|
149
|
+
let result, error;
|
|
150
|
+
let fn = await (resolver ? resolver(method, $functions[method]) : $functions[method]);
|
|
151
|
+
if (optional)
|
|
152
|
+
fn ||= () => void 0;
|
|
153
|
+
if (!fn) {
|
|
154
|
+
error = new Error(`[birpc] function "${method}" not found`);
|
|
155
|
+
} else {
|
|
156
|
+
try {
|
|
157
|
+
result = await fn.apply(bind === "rpc" ? rpc : $functions, args);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
error = e;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (msg.i) {
|
|
163
|
+
if (error && options.onError)
|
|
164
|
+
options.onError(error, method, args);
|
|
165
|
+
if (error && options.onFunctionError) {
|
|
166
|
+
if (options.onFunctionError(error, method, args) === true)
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (!error) {
|
|
170
|
+
try {
|
|
171
|
+
await post(serialize({ t: TYPE_RESPONSE, i: msg.i, r: result }), ...extra);
|
|
172
|
+
return;
|
|
173
|
+
} catch (e) {
|
|
174
|
+
error = e;
|
|
175
|
+
if (options.onGeneralError?.(e, method, args) !== true)
|
|
176
|
+
throw e;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
await post(serialize({ t: TYPE_RESPONSE, i: msg.i, e: error }), ...extra);
|
|
181
|
+
} catch (e) {
|
|
182
|
+
if (options.onGeneralError?.(e, method, args) !== true)
|
|
183
|
+
throw e;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
const { i: ack, r: result, e: error } = msg;
|
|
188
|
+
const promise = _rpcPromiseMap.get(ack);
|
|
189
|
+
if (promise) {
|
|
190
|
+
clearTimeout(promise.timeoutId);
|
|
191
|
+
if (error)
|
|
192
|
+
promise.reject(error);
|
|
193
|
+
else
|
|
194
|
+
promise.resolve(result);
|
|
195
|
+
}
|
|
196
|
+
_rpcPromiseMap.delete(ack);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
_promiseInit = on(onMessage);
|
|
200
|
+
return rpc;
|
|
201
|
+
}
|
|
202
|
+
function createPromiseWithResolvers() {
|
|
203
|
+
let resolve;
|
|
204
|
+
let reject;
|
|
205
|
+
const promise = new Promise((res, rej) => {
|
|
206
|
+
resolve = res;
|
|
207
|
+
reject = rej;
|
|
208
|
+
});
|
|
209
|
+
return { promise, resolve, reject };
|
|
210
|
+
}
|
|
211
|
+
const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
212
|
+
function nanoid(size = 21) {
|
|
213
|
+
let id = "";
|
|
214
|
+
let i = size;
|
|
215
|
+
while (i--)
|
|
216
|
+
id += urlAlphabet[random() * 64 | 0];
|
|
217
|
+
return id;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export { createBirpc as c };
|