@rstest/core 0.7.1 → 0.7.2
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 +9 -3
- package/dist/0~151.js +303 -264
- package/dist/0~583.js +1 -1
- package/dist/0~588.js +1 -1
- package/dist/0~62.js +1 -1
- package/dist/0~634.js +3 -3
- package/dist/0~809.js +1 -1
- package/dist/0~835.js +1 -1
- package/dist/0~919.js +65 -33
- package/dist/0~923.js +1 -1
- package/dist/{155.js → 131.js} +12 -12
- package/dist/404.js +210 -0
- package/dist/{362.js → 554.js} +68 -49
- package/dist/734.js +1 -1
- package/dist/946.js +40 -30
- package/dist/index.d.ts +49 -17
- package/dist/index.js +1 -1
- package/dist/worker.d.ts +52 -23
- package/dist/worker.js +1 -1
- package/package.json +7 -6
- 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/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { config } from 'chai';
|
|
|
3
3
|
import { LoadConfigOptions } from '@rsbuild/core';
|
|
4
4
|
import type { RsbuildConfig } from '@rsbuild/core';
|
|
5
5
|
import type { RsbuildPlugin } from '@rsbuild/core';
|
|
6
|
+
import type { Writable } from 'node:stream';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
|
@@ -519,16 +520,7 @@ declare type CoverageThreshold = {
|
|
|
519
520
|
lines?: number;
|
|
520
521
|
};
|
|
521
522
|
|
|
522
|
-
declare type CoverageThresholds = CoverageThreshold | (CoverageThreshold &
|
|
523
|
-
/** check thresholds for matched files */
|
|
524
|
-
[glob: string]: CoverageThreshold & {
|
|
525
|
-
/**
|
|
526
|
-
* check thresholds per file
|
|
527
|
-
* @default false
|
|
528
|
-
*/
|
|
529
|
-
perFile?: boolean;
|
|
530
|
-
};
|
|
531
|
-
});
|
|
523
|
+
declare type CoverageThresholds = CoverageThreshold | (CoverageThreshold & ThresholdGlobRecord);
|
|
532
524
|
|
|
533
525
|
export declare function createRstest({ config, projects, configFilePath, }: {
|
|
534
526
|
config: RstestConfig;
|
|
@@ -615,6 +607,11 @@ declare type DefaultReporterOptions = {
|
|
|
615
607
|
* @default true
|
|
616
608
|
*/
|
|
617
609
|
summary?: boolean;
|
|
610
|
+
/**
|
|
611
|
+
* logger which write messages to
|
|
612
|
+
* @default process.stdout/process.stderr
|
|
613
|
+
*/
|
|
614
|
+
logger?: Options['logger'];
|
|
618
615
|
};
|
|
619
616
|
|
|
620
617
|
/**
|
|
@@ -872,6 +869,8 @@ declare type FormattedError = {
|
|
|
872
869
|
name?: string;
|
|
873
870
|
stack?: string;
|
|
874
871
|
diff?: string;
|
|
872
|
+
expected?: string;
|
|
873
|
+
actual?: string;
|
|
875
874
|
};
|
|
876
875
|
|
|
877
876
|
declare interface Formatter {
|
|
@@ -1926,9 +1925,7 @@ declare interface Node_2 {
|
|
|
1926
1925
|
visit(visitor: Visitor, state: any): void;
|
|
1927
1926
|
}
|
|
1928
1927
|
|
|
1929
|
-
declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'exclude'>> & {
|
|
1930
|
-
[key in OptionalKeys]?: RstestConfig[key];
|
|
1931
|
-
} & {
|
|
1928
|
+
declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'exclude'>> & Partial<Pick<RstestConfig, OptionalKeys>> & {
|
|
1932
1929
|
pool: RstestPoolOptions;
|
|
1933
1930
|
coverage: NormalizedCoverageOptions;
|
|
1934
1931
|
setupFiles: string[];
|
|
@@ -1947,9 +1944,7 @@ declare type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>)
|
|
|
1947
1944
|
|
|
1948
1945
|
declare type NormalizedProcedure_2<T extends Procedure_2> = (...args: Parameters<T>) => ReturnType<T>;
|
|
1949
1946
|
|
|
1950
|
-
declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles'>> & {
|
|
1951
|
-
[key in OptionalKeys]?: NormalizedConfig[key];
|
|
1952
|
-
} & {
|
|
1947
|
+
declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles'>> & Pick<NormalizedConfig, OptionalKeys> & {
|
|
1953
1948
|
setupFiles: string[];
|
|
1954
1949
|
};
|
|
1955
1950
|
|
|
@@ -1976,6 +1971,16 @@ declare type OnTestFinishedHandler = (params: {
|
|
|
1976
1971
|
|
|
1977
1972
|
declare type OptionalKeys = 'testNamePattern' | 'plugins' | 'source' | 'resolve' | 'output' | 'performance' | 'tools' | 'dev' | 'onConsoleLog' | 'chaiConfig' | 'resolveSnapshotPath';
|
|
1978
1973
|
|
|
1974
|
+
declare interface Options {
|
|
1975
|
+
logger: {
|
|
1976
|
+
outputStream: Writable;
|
|
1977
|
+
errorStream: Writable;
|
|
1978
|
+
getColumns: () => number;
|
|
1979
|
+
};
|
|
1980
|
+
interval?: number;
|
|
1981
|
+
getWindow: () => string[];
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1979
1984
|
declare type OptionsReceived = PrettyFormatOptions;
|
|
1980
1985
|
|
|
1981
1986
|
declare interface ParsedStack {
|
|
@@ -2091,6 +2096,16 @@ export declare interface Reporter {
|
|
|
2091
2096
|
* Called when the test file has finished running.
|
|
2092
2097
|
*/
|
|
2093
2098
|
onTestFileResult?: (test: TestFileResult) => void;
|
|
2099
|
+
/**
|
|
2100
|
+
* Called before running the test suite.
|
|
2101
|
+
*/
|
|
2102
|
+
onTestSuiteStart?: (test: TestSuiteInfo) => void;
|
|
2103
|
+
/**
|
|
2104
|
+
* Called when the suite has finished running or was just skipped.
|
|
2105
|
+
*
|
|
2106
|
+
* `result.errors` contains only suite hooks errors
|
|
2107
|
+
*/
|
|
2108
|
+
onTestSuiteResult?: (result: TestResult) => void;
|
|
2094
2109
|
/**
|
|
2095
2110
|
* Called when the test has finished running or was just skipped.
|
|
2096
2111
|
*/
|
|
@@ -2760,7 +2775,7 @@ declare class StatusRenderer {
|
|
|
2760
2775
|
private renderer;
|
|
2761
2776
|
private startTime;
|
|
2762
2777
|
private testState;
|
|
2763
|
-
constructor(rootPath: string, state: RstestTestState);
|
|
2778
|
+
constructor(rootPath: string, state: RstestTestState, logger?: Options['logger']);
|
|
2764
2779
|
getContent(): string[];
|
|
2765
2780
|
onTestFileStart(): void;
|
|
2766
2781
|
onTestCaseResult(): void;
|
|
@@ -2890,6 +2905,14 @@ declare class TestStateManager {
|
|
|
2890
2905
|
reset(): void;
|
|
2891
2906
|
}
|
|
2892
2907
|
|
|
2908
|
+
export declare type TestSuiteInfo = {
|
|
2909
|
+
testId: string;
|
|
2910
|
+
name: string;
|
|
2911
|
+
parentNames?: string[];
|
|
2912
|
+
testPath: TestPath;
|
|
2913
|
+
project: string;
|
|
2914
|
+
};
|
|
2915
|
+
|
|
2893
2916
|
declare type TextLcovOptions = ProjectOptions;
|
|
2894
2917
|
|
|
2895
2918
|
declare interface TextOptions extends FileOptions {
|
|
@@ -2900,6 +2923,15 @@ declare interface TextOptions extends FileOptions {
|
|
|
2900
2923
|
|
|
2901
2924
|
declare type TextSummaryOptions = FileOptions;
|
|
2902
2925
|
|
|
2926
|
+
/** check thresholds for matched files */
|
|
2927
|
+
declare type ThresholdGlobRecord = Record<string, CoverageThreshold & {
|
|
2928
|
+
/**
|
|
2929
|
+
* check thresholds per file
|
|
2930
|
+
* @default false
|
|
2931
|
+
*/
|
|
2932
|
+
perFile?: boolean;
|
|
2933
|
+
}>;
|
|
2934
|
+
|
|
2903
2935
|
declare interface Totals {
|
|
2904
2936
|
total: number;
|
|
2905
2937
|
covered: number;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import 'module';
|
|
2
2
|
/*#__PURE__*/ import.meta.url;
|
|
3
|
-
export { afterAll, afterEach, assert, beforeAll, beforeEach, createRstest, defineConfig, defineProject, describe, expect, initCli, it, loadConfig, mergeRstestConfig, onTestFailed, onTestFinished, rs, rstest, runCLI, test } from "./
|
|
3
|
+
export { afterAll, afterEach, assert, beforeAll, beforeEach, createRstest, defineConfig, defineProject, describe, expect, initCli, it, loadConfig, mergeRstestConfig, onTestFailed, onTestFinished, rs, rstest, runCLI, test } from "./131.js";
|
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 {
|
|
@@ -1580,9 +1579,7 @@ declare interface Node_2 {
|
|
|
1580
1579
|
visit(visitor: Visitor, state: any): void;
|
|
1581
1580
|
}
|
|
1582
1581
|
|
|
1583
|
-
declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'exclude'>> & {
|
|
1584
|
-
[key in OptionalKeys]?: RstestConfig[key];
|
|
1585
|
-
} & {
|
|
1582
|
+
declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'exclude'>> & Partial<Pick<RstestConfig, OptionalKeys>> & {
|
|
1586
1583
|
pool: RstestPoolOptions;
|
|
1587
1584
|
coverage: NormalizedCoverageOptions;
|
|
1588
1585
|
setupFiles: string[];
|
|
@@ -1608,9 +1605,7 @@ declare type NormalizedFixtures = Record<string, NormalizedFixture>;
|
|
|
1608
1605
|
|
|
1609
1606
|
declare type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>) => ReturnType<T>;
|
|
1610
1607
|
|
|
1611
|
-
declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles'>> & {
|
|
1612
|
-
[key in OptionalKeys]?: NormalizedConfig[key];
|
|
1613
|
-
} & {
|
|
1608
|
+
declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles'>> & Pick<NormalizedConfig, OptionalKeys> & {
|
|
1614
1609
|
setupFiles: string[];
|
|
1615
1610
|
};
|
|
1616
1611
|
|
|
@@ -1633,6 +1628,16 @@ declare type OnTestFinishedHandler = (params: {
|
|
|
1633
1628
|
|
|
1634
1629
|
declare type OptionalKeys = 'testNamePattern' | 'plugins' | 'source' | 'resolve' | 'output' | 'performance' | 'tools' | 'dev' | 'onConsoleLog' | 'chaiConfig' | 'resolveSnapshotPath';
|
|
1635
1630
|
|
|
1631
|
+
declare interface Options {
|
|
1632
|
+
logger: {
|
|
1633
|
+
outputStream: Writable;
|
|
1634
|
+
errorStream: Writable;
|
|
1635
|
+
getColumns: () => number;
|
|
1636
|
+
};
|
|
1637
|
+
interval?: number;
|
|
1638
|
+
getWindow: () => string[];
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1636
1641
|
declare type OptionsReceived = PrettyFormatOptions;
|
|
1637
1642
|
|
|
1638
1643
|
declare interface ParsedStack {
|
|
@@ -1737,6 +1742,16 @@ declare interface Reporter {
|
|
|
1737
1742
|
* Called when the test file has finished running.
|
|
1738
1743
|
*/
|
|
1739
1744
|
onTestFileResult?: (test: TestFileResult) => void;
|
|
1745
|
+
/**
|
|
1746
|
+
* Called before running the test suite.
|
|
1747
|
+
*/
|
|
1748
|
+
onTestSuiteStart?: (test: TestSuiteInfo) => void;
|
|
1749
|
+
/**
|
|
1750
|
+
* Called when the suite has finished running or was just skipped.
|
|
1751
|
+
*
|
|
1752
|
+
* `result.errors` contains only suite hooks errors
|
|
1753
|
+
*/
|
|
1754
|
+
onTestSuiteResult?: (result: TestResult) => void;
|
|
1740
1755
|
/**
|
|
1741
1756
|
* Called when the test has finished running or was just skipped.
|
|
1742
1757
|
*/
|
|
@@ -2089,6 +2104,8 @@ declare type RuntimeRPC = {
|
|
|
2089
2104
|
assetFiles: Record<string, string>;
|
|
2090
2105
|
sourceMaps: Record<string, string>;
|
|
2091
2106
|
}>;
|
|
2107
|
+
onTestSuiteStart: (test: TestSuiteInfo) => Promise<void>;
|
|
2108
|
+
onTestSuiteResult: (result: TestResult) => Promise<void>;
|
|
2092
2109
|
onTestCaseStart: (test: TestCaseInfo) => Promise<void>;
|
|
2093
2110
|
onTestCaseResult: (result: TestResult) => Promise<void>;
|
|
2094
2111
|
getCountOfFailedTests: () => Promise<number>;
|
|
@@ -2287,7 +2304,7 @@ declare class StatusRenderer {
|
|
|
2287
2304
|
private renderer;
|
|
2288
2305
|
private startTime;
|
|
2289
2306
|
private testState;
|
|
2290
|
-
constructor(rootPath: string, state: RstestTestState);
|
|
2307
|
+
constructor(rootPath: string, state: RstestTestState, logger?: Options['logger']);
|
|
2291
2308
|
getContent(): string[];
|
|
2292
2309
|
onTestFileStart(): void;
|
|
2293
2310
|
onTestCaseResult(): void;
|
|
@@ -2461,17 +2478,12 @@ declare class TestStateManager {
|
|
|
2461
2478
|
reset(): void;
|
|
2462
2479
|
}
|
|
2463
2480
|
|
|
2464
|
-
declare type TestSuite = {
|
|
2465
|
-
testId: string;
|
|
2466
|
-
name: string;
|
|
2467
|
-
parentNames?: string[];
|
|
2481
|
+
declare type TestSuite = TestSuiteInfo & {
|
|
2468
2482
|
runMode: TestRunMode;
|
|
2469
2483
|
each?: boolean;
|
|
2470
2484
|
inTestEach?: boolean;
|
|
2471
2485
|
concurrent?: boolean;
|
|
2472
2486
|
sequential?: boolean;
|
|
2473
|
-
testPath: TestPath;
|
|
2474
|
-
project: string;
|
|
2475
2487
|
/** nested cases and suite could in a suite */
|
|
2476
2488
|
tests: (TestSuite | TestCase)[];
|
|
2477
2489
|
type: 'suite';
|
|
@@ -2481,6 +2493,14 @@ declare type TestSuite = {
|
|
|
2481
2493
|
beforeEachListeners?: BeforeEachListener[];
|
|
2482
2494
|
};
|
|
2483
2495
|
|
|
2496
|
+
declare type TestSuiteInfo = {
|
|
2497
|
+
testId: string;
|
|
2498
|
+
name: string;
|
|
2499
|
+
parentNames?: string[];
|
|
2500
|
+
testPath: TestPath;
|
|
2501
|
+
project: string;
|
|
2502
|
+
};
|
|
2503
|
+
|
|
2484
2504
|
declare type TextLcovOptions = ProjectOptions;
|
|
2485
2505
|
|
|
2486
2506
|
declare interface TextOptions extends FileOptions {
|
|
@@ -2491,6 +2511,15 @@ declare interface TextOptions extends FileOptions {
|
|
|
2491
2511
|
|
|
2492
2512
|
declare type TextSummaryOptions = FileOptions;
|
|
2493
2513
|
|
|
2514
|
+
/** check thresholds for matched files */
|
|
2515
|
+
declare type ThresholdGlobRecord = Record<string, CoverageThreshold & {
|
|
2516
|
+
/**
|
|
2517
|
+
* check thresholds per file
|
|
2518
|
+
* @default false
|
|
2519
|
+
*/
|
|
2520
|
+
perFile?: boolean;
|
|
2521
|
+
}>;
|
|
2522
|
+
|
|
2494
2523
|
declare interface Totals {
|
|
2495
2524
|
total: number;
|
|
2496
2525
|
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.2",
|
|
4
4
|
"description": "The Rsbuild-based test tool.",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/web-infra-dev/rstest/issues"
|
|
@@ -53,13 +53,13 @@
|
|
|
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.
|
|
62
|
+
"@rslib/core": "0.18.3",
|
|
63
63
|
"@sinonjs/fake-timers": "^14.0.0",
|
|
64
64
|
"@types/babel__code-frame": "^7.0.6",
|
|
65
65
|
"@types/istanbul-reports": "^3.0.4",
|
|
@@ -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
|