@rstest/core 0.7.1 → 0.7.3
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/0~122.js +16 -6
- package/dist/0~151.js +485 -307
- package/dist/0~173.js +27 -27
- package/dist/0~403.js +7 -7
- package/dist/0~583.js +2 -2
- package/dist/0~588.js +6 -6
- package/dist/0~62.js +1 -1
- package/dist/0~634.js +20 -18
- package/dist/0~809.js +1 -1
- package/dist/0~835.js +4 -4
- package/dist/0~919.js +65 -33
- package/dist/0~923.js +4 -4
- package/dist/{155.js → 131.js} +62 -144
- package/dist/404.js +210 -0
- package/dist/{362.js → 554.js} +134 -111
- package/dist/672.js +91 -0
- package/dist/734.js +1 -1
- package/dist/946.js +47 -31
- package/dist/index.d.ts +163 -22
- package/dist/index.js +1 -1
- package/dist/mockRuntimeCode.js +1 -0
- package/dist/worker.d.ts +86 -30
- package/dist/worker.js +1 -1
- package/package.json +8 -7
- package/dist/770.js +0 -167
- /package/dist/{0~454.js → 0~907.js} +0 -0
- /package/dist/{362.js.LICENSE.txt → 554.js.LICENSE.txt} +0 -0
package/dist/worker.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { config } from 'chai';
|
|
2
2
|
import type { RsbuildConfig } from '@rsbuild/core';
|
|
3
|
+
import type { Writable } from 'node:stream';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
|
@@ -431,16 +432,7 @@ declare type CoverageThreshold = {
|
|
|
431
432
|
lines?: number;
|
|
432
433
|
};
|
|
433
434
|
|
|
434
|
-
declare type CoverageThresholds = CoverageThreshold | (CoverageThreshold &
|
|
435
|
-
/** check thresholds for matched files */
|
|
436
|
-
[glob: string]: CoverageThreshold & {
|
|
437
|
-
/**
|
|
438
|
-
* check thresholds per file
|
|
439
|
-
* @default false
|
|
440
|
-
*/
|
|
441
|
-
perFile?: boolean;
|
|
442
|
-
};
|
|
443
|
-
});
|
|
435
|
+
declare type CoverageThresholds = CoverageThreshold | (CoverageThreshold & ThresholdGlobRecord);
|
|
444
436
|
|
|
445
437
|
declare interface CustomMatcher {
|
|
446
438
|
/**
|
|
@@ -521,6 +513,11 @@ declare type DefaultReporterOptions = {
|
|
|
521
513
|
* @default true
|
|
522
514
|
*/
|
|
523
515
|
summary?: boolean;
|
|
516
|
+
/**
|
|
517
|
+
* logger which write messages to
|
|
518
|
+
* @default process.stdout/process.stderr
|
|
519
|
+
*/
|
|
520
|
+
logger?: Options['logger'];
|
|
524
521
|
};
|
|
525
522
|
|
|
526
523
|
declare type DescribeAPI = DescribeFn & {
|
|
@@ -704,6 +701,8 @@ declare type FormattedError = {
|
|
|
704
701
|
name?: string;
|
|
705
702
|
stack?: string;
|
|
706
703
|
diff?: string;
|
|
704
|
+
expected?: string;
|
|
705
|
+
actual?: string;
|
|
707
706
|
};
|
|
708
707
|
|
|
709
708
|
declare interface Formatter {
|
|
@@ -1214,7 +1213,12 @@ declare interface LinkMapper {
|
|
|
1214
1213
|
assetPath(node: Node_2, name: string): string;
|
|
1215
1214
|
}
|
|
1216
1215
|
|
|
1217
|
-
declare
|
|
1216
|
+
declare type Location_2 = {
|
|
1217
|
+
line: number;
|
|
1218
|
+
column: number;
|
|
1219
|
+
};
|
|
1220
|
+
|
|
1221
|
+
declare interface Location_3 {
|
|
1218
1222
|
line: number;
|
|
1219
1223
|
column: number;
|
|
1220
1224
|
}
|
|
@@ -1580,9 +1584,7 @@ declare interface Node_2 {
|
|
|
1580
1584
|
visit(visitor: Visitor, state: any): void;
|
|
1581
1585
|
}
|
|
1582
1586
|
|
|
1583
|
-
declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'exclude'>> & {
|
|
1584
|
-
[key in OptionalKeys]?: RstestConfig[key];
|
|
1585
|
-
} & {
|
|
1587
|
+
declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'exclude'>> & Partial<Pick<RstestConfig, OptionalKeys>> & {
|
|
1586
1588
|
pool: RstestPoolOptions;
|
|
1587
1589
|
coverage: NormalizedCoverageOptions;
|
|
1588
1590
|
setupFiles: string[];
|
|
@@ -1608,9 +1610,7 @@ declare type NormalizedFixtures = Record<string, NormalizedFixture>;
|
|
|
1608
1610
|
|
|
1609
1611
|
declare type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>) => ReturnType<T>;
|
|
1610
1612
|
|
|
1611
|
-
declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles'>> & {
|
|
1612
|
-
[key in OptionalKeys]?: NormalizedConfig[key];
|
|
1613
|
-
} & {
|
|
1613
|
+
declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles'>> & Pick<NormalizedConfig, OptionalKeys> & {
|
|
1614
1614
|
setupFiles: string[];
|
|
1615
1615
|
};
|
|
1616
1616
|
|
|
@@ -1633,6 +1633,16 @@ declare type OnTestFinishedHandler = (params: {
|
|
|
1633
1633
|
|
|
1634
1634
|
declare type OptionalKeys = 'testNamePattern' | 'plugins' | 'source' | 'resolve' | 'output' | 'performance' | 'tools' | 'dev' | 'onConsoleLog' | 'chaiConfig' | 'resolveSnapshotPath';
|
|
1635
1635
|
|
|
1636
|
+
declare interface Options {
|
|
1637
|
+
logger: {
|
|
1638
|
+
outputStream: Writable;
|
|
1639
|
+
errorStream: Writable;
|
|
1640
|
+
getColumns: () => number;
|
|
1641
|
+
};
|
|
1642
|
+
interval?: number;
|
|
1643
|
+
getWindow: () => string[];
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1636
1646
|
declare type OptionsReceived = PrettyFormatOptions;
|
|
1637
1647
|
|
|
1638
1648
|
declare interface ParsedStack {
|
|
@@ -1709,8 +1719,8 @@ declare type PromisifyAssertion<T> = Promisify<Assertion<T>>;
|
|
|
1709
1719
|
declare type PromisifyAssertion_2<T> = Promisify_2<Assertion_2<T>>;
|
|
1710
1720
|
|
|
1711
1721
|
declare interface Range_2 {
|
|
1712
|
-
start:
|
|
1713
|
-
end:
|
|
1722
|
+
start: Location_3;
|
|
1723
|
+
end: Location_3;
|
|
1714
1724
|
}
|
|
1715
1725
|
|
|
1716
1726
|
declare interface RawMatcherFn<
|
|
@@ -1733,10 +1743,24 @@ declare interface Reporter {
|
|
|
1733
1743
|
* Called before test file run.
|
|
1734
1744
|
*/
|
|
1735
1745
|
onTestFileStart?: (test: TestFileInfo) => void;
|
|
1746
|
+
/**
|
|
1747
|
+
* Called after tests in file collected.
|
|
1748
|
+
*/
|
|
1749
|
+
onTestFileReady?: (test: TestFileInfo) => void;
|
|
1736
1750
|
/**
|
|
1737
1751
|
* Called when the test file has finished running.
|
|
1738
1752
|
*/
|
|
1739
1753
|
onTestFileResult?: (test: TestFileResult) => void;
|
|
1754
|
+
/**
|
|
1755
|
+
* Called before running the test suite.
|
|
1756
|
+
*/
|
|
1757
|
+
onTestSuiteStart?: (test: TestSuiteInfo) => void;
|
|
1758
|
+
/**
|
|
1759
|
+
* Called when the suite has finished running or was just skipped.
|
|
1760
|
+
*
|
|
1761
|
+
* `result.errors` contains only suite hooks errors
|
|
1762
|
+
*/
|
|
1763
|
+
onTestSuiteResult?: (result: TestResult) => void;
|
|
1740
1764
|
/**
|
|
1741
1765
|
* Called when the test has finished running or was just skipped.
|
|
1742
1766
|
*/
|
|
@@ -1994,6 +2018,10 @@ declare interface RstestConfig {
|
|
|
1994
2018
|
* chai configuration options
|
|
1995
2019
|
*/
|
|
1996
2020
|
chaiConfig?: ChaiConfig;
|
|
2021
|
+
/**
|
|
2022
|
+
* Include `location` property in `TestInfo` received by reporters
|
|
2023
|
+
*/
|
|
2024
|
+
includeTaskLocation?: boolean;
|
|
1997
2025
|
plugins?: RsbuildConfig['plugins'];
|
|
1998
2026
|
source?: Pick<NonNullable<RsbuildConfig['source']>, 'define' | 'tsconfigPath' | 'decorators' | 'include' | 'exclude'>;
|
|
1999
2027
|
performance?: Pick<NonNullable<RsbuildConfig['performance']>, 'bundleAnalyze'>;
|
|
@@ -2080,15 +2108,18 @@ declare type RunningModules = Map<string, {
|
|
|
2080
2108
|
results: TestResult[];
|
|
2081
2109
|
}>;
|
|
2082
2110
|
|
|
2083
|
-
declare type RuntimeConfig = Pick<RstestContext['normalizedConfig'], 'testTimeout' | 'testNamePattern' | 'globals' | 'passWithNoTests' | 'retry' | 'clearMocks' | 'resetMocks' | 'restoreMocks' | 'unstubEnvs' | 'unstubGlobals' | 'maxConcurrency' | 'printConsoleTrace' | 'disableConsoleIntercept' | 'testEnvironment' | 'isolate' | 'hookTimeout' | 'coverage' | 'snapshotFormat' | 'env' | 'logHeapUsage' | 'bail' | 'chaiConfig'>;
|
|
2111
|
+
declare type RuntimeConfig = Pick<RstestContext['normalizedConfig'], 'testTimeout' | 'testNamePattern' | 'globals' | 'passWithNoTests' | 'retry' | 'clearMocks' | 'resetMocks' | 'restoreMocks' | 'unstubEnvs' | 'unstubGlobals' | 'maxConcurrency' | 'printConsoleTrace' | 'disableConsoleIntercept' | 'testEnvironment' | 'isolate' | 'hookTimeout' | 'coverage' | 'snapshotFormat' | 'env' | 'logHeapUsage' | 'bail' | 'chaiConfig' | 'includeTaskLocation'>;
|
|
2084
2112
|
|
|
2085
2113
|
/** Runtime to Server */
|
|
2086
2114
|
declare type RuntimeRPC = {
|
|
2087
2115
|
onTestFileStart: (test: TestFileInfo) => Promise<void>;
|
|
2116
|
+
onTestFileReady: (test: TestFileInfo) => Promise<void>;
|
|
2088
2117
|
getAssetsByEntry: () => Promise<{
|
|
2089
2118
|
assetFiles: Record<string, string>;
|
|
2090
2119
|
sourceMaps: Record<string, string>;
|
|
2091
2120
|
}>;
|
|
2121
|
+
onTestSuiteStart: (test: TestSuiteInfo) => Promise<void>;
|
|
2122
|
+
onTestSuiteResult: (result: TestResult) => Promise<void>;
|
|
2092
2123
|
onTestCaseStart: (test: TestCaseInfo) => Promise<void>;
|
|
2093
2124
|
onTestCaseResult: (result: TestResult) => Promise<void>;
|
|
2094
2125
|
getCountOfFailedTests: () => Promise<number>;
|
|
@@ -2287,7 +2318,7 @@ declare class StatusRenderer {
|
|
|
2287
2318
|
private renderer;
|
|
2288
2319
|
private startTime;
|
|
2289
2320
|
private testState;
|
|
2290
|
-
constructor(rootPath: string, state: RstestTestState);
|
|
2321
|
+
constructor(rootPath: string, state: RstestTestState, logger?: Options['logger']);
|
|
2291
2322
|
getContent(): string[];
|
|
2292
2323
|
onTestFileStart(): void;
|
|
2293
2324
|
onTestCaseResult(): void;
|
|
@@ -2372,11 +2403,14 @@ declare type TestCase = TestCaseInfo & {
|
|
|
2372
2403
|
only?: boolean;
|
|
2373
2404
|
onFinished: OnTestFinishedHandler[];
|
|
2374
2405
|
onFailed: OnTestFailedHandler[];
|
|
2375
|
-
type: 'case';
|
|
2376
2406
|
/**
|
|
2377
2407
|
* Store promises (from async expects) to wait for them before finishing the test
|
|
2378
2408
|
*/
|
|
2379
2409
|
promises?: Promise<any>[];
|
|
2410
|
+
/**
|
|
2411
|
+
* Store stack trace error created when test is registered, used for trace original position
|
|
2412
|
+
*/
|
|
2413
|
+
stackTraceError: Error;
|
|
2380
2414
|
/**
|
|
2381
2415
|
* Result of the task. if `expect.soft()` failed multiple times or `retry` was triggered.
|
|
2382
2416
|
*/
|
|
@@ -2391,6 +2425,9 @@ declare type TestCaseInfo = {
|
|
|
2391
2425
|
parentNames?: string[];
|
|
2392
2426
|
project: string;
|
|
2393
2427
|
startTime?: number;
|
|
2428
|
+
/** Only included when `includeTaskLocation` config is enabled */
|
|
2429
|
+
location?: Location_2;
|
|
2430
|
+
type: 'case';
|
|
2394
2431
|
};
|
|
2395
2432
|
|
|
2396
2433
|
declare type TestContext = {
|
|
@@ -2413,6 +2450,7 @@ declare interface TesterContext {
|
|
|
2413
2450
|
|
|
2414
2451
|
declare type TestFileInfo = {
|
|
2415
2452
|
testPath: TestPath;
|
|
2453
|
+
tests: TestInfo[];
|
|
2416
2454
|
};
|
|
2417
2455
|
|
|
2418
2456
|
declare type TestFileResult = TestResult & {
|
|
@@ -2425,6 +2463,10 @@ declare type TestFn<ExtraContext = object> = (description: string, fn?: TestCall
|
|
|
2425
2463
|
|
|
2426
2464
|
declare type TestForFn<ExtraContext = object> = <T>(cases: readonly T[]) => (description: string, fn?: (param: T, context: TestContext & ExtraContext) => MaybePromise<void>, timeout?: number) => void;
|
|
2427
2465
|
|
|
2466
|
+
declare type TestInfo = TestCaseInfo | (TestSuiteInfo & {
|
|
2467
|
+
tests: TestInfo[];
|
|
2468
|
+
});
|
|
2469
|
+
|
|
2428
2470
|
/** The test file original path */
|
|
2429
2471
|
declare type TestPath = string;
|
|
2430
2472
|
|
|
@@ -2461,26 +2503,31 @@ declare class TestStateManager {
|
|
|
2461
2503
|
reset(): void;
|
|
2462
2504
|
}
|
|
2463
2505
|
|
|
2464
|
-
declare type TestSuite = {
|
|
2465
|
-
testId: string;
|
|
2466
|
-
name: string;
|
|
2467
|
-
parentNames?: string[];
|
|
2506
|
+
declare type TestSuite = TestSuiteInfo & {
|
|
2468
2507
|
runMode: TestRunMode;
|
|
2469
2508
|
each?: boolean;
|
|
2470
2509
|
inTestEach?: boolean;
|
|
2471
2510
|
concurrent?: boolean;
|
|
2472
2511
|
sequential?: boolean;
|
|
2473
|
-
testPath: TestPath;
|
|
2474
|
-
project: string;
|
|
2475
2512
|
/** nested cases and suite could in a suite */
|
|
2476
|
-
tests:
|
|
2477
|
-
type: 'suite';
|
|
2513
|
+
tests: Test_2[];
|
|
2478
2514
|
afterAllListeners?: AfterAllListener[];
|
|
2479
2515
|
beforeAllListeners?: BeforeAllListener[];
|
|
2480
2516
|
afterEachListeners?: AfterEachListener[];
|
|
2481
2517
|
beforeEachListeners?: BeforeEachListener[];
|
|
2482
2518
|
};
|
|
2483
2519
|
|
|
2520
|
+
declare type TestSuiteInfo = {
|
|
2521
|
+
testId: string;
|
|
2522
|
+
name: string;
|
|
2523
|
+
parentNames?: string[];
|
|
2524
|
+
testPath: TestPath;
|
|
2525
|
+
project: string;
|
|
2526
|
+
type: 'suite';
|
|
2527
|
+
/** Only included when `includeTaskLocation` config is enabled */
|
|
2528
|
+
location?: Location_2;
|
|
2529
|
+
};
|
|
2530
|
+
|
|
2484
2531
|
declare type TextLcovOptions = ProjectOptions;
|
|
2485
2532
|
|
|
2486
2533
|
declare interface TextOptions extends FileOptions {
|
|
@@ -2491,6 +2538,15 @@ declare interface TextOptions extends FileOptions {
|
|
|
2491
2538
|
|
|
2492
2539
|
declare type TextSummaryOptions = FileOptions;
|
|
2493
2540
|
|
|
2541
|
+
/** check thresholds for matched files */
|
|
2542
|
+
declare type ThresholdGlobRecord = Record<string, CoverageThreshold & {
|
|
2543
|
+
/**
|
|
2544
|
+
* check thresholds per file
|
|
2545
|
+
* @default false
|
|
2546
|
+
*/
|
|
2547
|
+
perFile?: boolean;
|
|
2548
|
+
}>;
|
|
2549
|
+
|
|
2494
2550
|
declare interface Totals {
|
|
2495
2551
|
total: number;
|
|
2496
2552
|
covered: number;
|
package/dist/worker.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rstest/core",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "The Rsbuild-based test tool.",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/web-infra-dev/rstest/issues"
|
|
@@ -53,14 +53,14 @@
|
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"chai": "^5.3.3",
|
|
55
55
|
"pathe": "^2.0.3",
|
|
56
|
-
"birpc": "2.
|
|
56
|
+
"birpc": "2.9.0",
|
|
57
57
|
"@vitest/expect": "^3.2.4",
|
|
58
58
|
"@vitest/snapshot": "^3.2.4",
|
|
59
59
|
"@babel/code-frame": "^7.27.1",
|
|
60
60
|
"@jridgewell/trace-mapping": "0.3.31",
|
|
61
61
|
"@microsoft/api-extractor": "^7.53.3",
|
|
62
|
-
"@rslib/core": "0.
|
|
63
|
-
"@sinonjs/fake-timers": "^
|
|
62
|
+
"@rslib/core": "0.18.3",
|
|
63
|
+
"@sinonjs/fake-timers": "^15.0.0",
|
|
64
64
|
"@types/babel__code-frame": "^7.0.6",
|
|
65
65
|
"@types/istanbul-reports": "^3.0.4",
|
|
66
66
|
"@types/istanbul-lib-coverage": "^2.0.6",
|
|
@@ -70,12 +70,13 @@
|
|
|
70
70
|
"@types/picomatch": "^4.0.2",
|
|
71
71
|
"cac": "^6.7.14",
|
|
72
72
|
"chokidar": "^4.0.3",
|
|
73
|
-
"happy-dom": "^20.0.
|
|
73
|
+
"happy-dom": "^20.0.11",
|
|
74
74
|
"jest-diff": "^30.2.0",
|
|
75
75
|
"jsdom": "^26.1.0",
|
|
76
76
|
"webpack-license-plugin": "^4.5.1",
|
|
77
77
|
"picocolors": "^1.1.1",
|
|
78
|
-
"
|
|
78
|
+
"pretty-format": "^30.2.0",
|
|
79
|
+
"rslog": "^1.3.2",
|
|
79
80
|
"source-map-support": "^0.5.21",
|
|
80
81
|
"std-env": "^3.10.0",
|
|
81
82
|
"stacktrace-parser": "0.1.11",
|
|
@@ -107,7 +108,7 @@
|
|
|
107
108
|
"scripts": {
|
|
108
109
|
"build": "rslib build && npx prettier ./LICENSE.md --write",
|
|
109
110
|
"typecheck": "tsc --noEmit",
|
|
110
|
-
"dev": "rslib build --watch",
|
|
111
|
+
"dev": "cross-env SOURCEMAP=true rslib build --watch",
|
|
111
112
|
"test": "npx rstest --globals"
|
|
112
113
|
}
|
|
113
114
|
}
|
package/dist/770.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import 'module';
|
|
2
|
-
/*#__PURE__*/ import.meta.url;
|
|
3
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
|
-
import node_v8 from "node:v8";
|
|
5
|
-
const TYPE_REQUEST = "q";
|
|
6
|
-
const TYPE_RESPONSE = "s";
|
|
7
|
-
const DEFAULT_TIMEOUT = 6e4;
|
|
8
|
-
function defaultSerialize(i) {
|
|
9
|
-
return i;
|
|
10
|
-
}
|
|
11
|
-
const defaultDeserialize = defaultSerialize;
|
|
12
|
-
const { clearTimeout: dist_clearTimeout, setTimeout: dist_setTimeout } = globalThis;
|
|
13
|
-
const random = Math.random.bind(Math);
|
|
14
|
-
function createBirpc(functions, options) {
|
|
15
|
-
const { post, on, off = ()=>{}, eventNames = [], serialize = defaultSerialize, deserialize = defaultDeserialize, resolver, bind = "rpc", timeout = DEFAULT_TIMEOUT } = options;
|
|
16
|
-
const rpcPromiseMap = /* @__PURE__ */ new Map();
|
|
17
|
-
let _promise;
|
|
18
|
-
let closed = false;
|
|
19
|
-
const rpc = new Proxy({}, {
|
|
20
|
-
get (_, method) {
|
|
21
|
-
if ("$functions" === method) return functions;
|
|
22
|
-
if ("$close" === method) return close;
|
|
23
|
-
if ("$rejectPendingCalls" === method) return rejectPendingCalls;
|
|
24
|
-
if ("$closed" === method) return closed;
|
|
25
|
-
if ("then" === method && !eventNames.includes("then") && !("then" in functions)) return;
|
|
26
|
-
const sendEvent = (...args)=>{
|
|
27
|
-
post(serialize({
|
|
28
|
-
m: method,
|
|
29
|
-
a: args,
|
|
30
|
-
t: TYPE_REQUEST
|
|
31
|
-
}));
|
|
32
|
-
};
|
|
33
|
-
if (eventNames.includes(method)) {
|
|
34
|
-
sendEvent.asEvent = sendEvent;
|
|
35
|
-
return sendEvent;
|
|
36
|
-
}
|
|
37
|
-
const sendCall = async (...args)=>{
|
|
38
|
-
if (closed) throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
|
|
39
|
-
if (_promise) try {
|
|
40
|
-
await _promise;
|
|
41
|
-
} finally{
|
|
42
|
-
_promise = void 0;
|
|
43
|
-
}
|
|
44
|
-
return new Promise((resolve, reject)=>{
|
|
45
|
-
const id = nanoid();
|
|
46
|
-
let timeoutId;
|
|
47
|
-
if (timeout >= 0) {
|
|
48
|
-
timeoutId = dist_setTimeout(()=>{
|
|
49
|
-
try {
|
|
50
|
-
const handleResult = options.onTimeoutError?.(method, args);
|
|
51
|
-
if (true !== handleResult) throw new Error(`[birpc] timeout on calling "${method}"`);
|
|
52
|
-
} catch (e) {
|
|
53
|
-
reject(e);
|
|
54
|
-
}
|
|
55
|
-
rpcPromiseMap.delete(id);
|
|
56
|
-
}, timeout);
|
|
57
|
-
if ("object" == typeof timeoutId) timeoutId = timeoutId.unref?.();
|
|
58
|
-
}
|
|
59
|
-
rpcPromiseMap.set(id, {
|
|
60
|
-
resolve,
|
|
61
|
-
reject,
|
|
62
|
-
timeoutId,
|
|
63
|
-
method
|
|
64
|
-
});
|
|
65
|
-
post(serialize({
|
|
66
|
-
m: method,
|
|
67
|
-
a: args,
|
|
68
|
-
i: id,
|
|
69
|
-
t: "q"
|
|
70
|
-
}));
|
|
71
|
-
});
|
|
72
|
-
};
|
|
73
|
-
sendCall.asEvent = sendEvent;
|
|
74
|
-
return sendCall;
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
function close(customError) {
|
|
78
|
-
closed = true;
|
|
79
|
-
rpcPromiseMap.forEach(({ reject, method })=>{
|
|
80
|
-
const error = new Error(`[birpc] rpc is closed, cannot call "${method}"`);
|
|
81
|
-
if (customError) {
|
|
82
|
-
customError.cause ??= error;
|
|
83
|
-
return reject(customError);
|
|
84
|
-
}
|
|
85
|
-
reject(error);
|
|
86
|
-
});
|
|
87
|
-
rpcPromiseMap.clear();
|
|
88
|
-
off(onMessage);
|
|
89
|
-
}
|
|
90
|
-
function rejectPendingCalls(handler) {
|
|
91
|
-
const entries = Array.from(rpcPromiseMap.values());
|
|
92
|
-
const handlerResults = entries.map(({ method, reject })=>{
|
|
93
|
-
if (!handler) return reject(new Error(`[birpc]: rejected pending call "${method}".`));
|
|
94
|
-
return handler({
|
|
95
|
-
method,
|
|
96
|
-
reject
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
rpcPromiseMap.clear();
|
|
100
|
-
return handlerResults;
|
|
101
|
-
}
|
|
102
|
-
async function onMessage(data, ...extra) {
|
|
103
|
-
let msg;
|
|
104
|
-
try {
|
|
105
|
-
msg = deserialize(data);
|
|
106
|
-
} catch (e) {
|
|
107
|
-
if (options.onGeneralError?.(e) !== true) throw e;
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
if (msg.t === TYPE_REQUEST) {
|
|
111
|
-
const { m: method, a: args } = msg;
|
|
112
|
-
let result, error;
|
|
113
|
-
const fn = await (resolver ? resolver(method, functions[method]) : functions[method]);
|
|
114
|
-
if (fn) try {
|
|
115
|
-
result = await fn.apply("rpc" === bind ? rpc : functions, args);
|
|
116
|
-
} catch (e) {
|
|
117
|
-
error = e;
|
|
118
|
-
}
|
|
119
|
-
else error = new Error(`[birpc] function "${method}" not found`);
|
|
120
|
-
if (msg.i) {
|
|
121
|
-
if (error && options.onError) options.onError(error, method, args);
|
|
122
|
-
if (error && options.onFunctionError) {
|
|
123
|
-
if (true === options.onFunctionError(error, method, args)) return;
|
|
124
|
-
}
|
|
125
|
-
if (!error) try {
|
|
126
|
-
post(serialize({
|
|
127
|
-
t: TYPE_RESPONSE,
|
|
128
|
-
i: msg.i,
|
|
129
|
-
r: result
|
|
130
|
-
}), ...extra);
|
|
131
|
-
return;
|
|
132
|
-
} catch (e) {
|
|
133
|
-
error = e;
|
|
134
|
-
if (options.onGeneralError?.(e, method, args) !== true) throw e;
|
|
135
|
-
}
|
|
136
|
-
try {
|
|
137
|
-
post(serialize({
|
|
138
|
-
t: TYPE_RESPONSE,
|
|
139
|
-
i: msg.i,
|
|
140
|
-
e: error
|
|
141
|
-
}), ...extra);
|
|
142
|
-
} catch (e) {
|
|
143
|
-
if (options.onGeneralError?.(e, method, args) !== true) throw e;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
} else {
|
|
147
|
-
const { i: ack, r: result, e: error } = msg;
|
|
148
|
-
const promise = rpcPromiseMap.get(ack);
|
|
149
|
-
if (promise) {
|
|
150
|
-
dist_clearTimeout(promise.timeoutId);
|
|
151
|
-
if (error) promise.reject(error);
|
|
152
|
-
else promise.resolve(result);
|
|
153
|
-
}
|
|
154
|
-
rpcPromiseMap.delete(ack);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
_promise = on(onMessage);
|
|
158
|
-
return rpc;
|
|
159
|
-
}
|
|
160
|
-
const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
161
|
-
function nanoid(size = 21) {
|
|
162
|
-
let id = "";
|
|
163
|
-
let i = size;
|
|
164
|
-
while(i--)id += urlAlphabet[64 * random() | 0];
|
|
165
|
-
return id;
|
|
166
|
-
}
|
|
167
|
-
export { createBirpc, fileURLToPath, node_v8, pathToFileURL };
|
|
File without changes
|
|
File without changes
|