@vitest/browser 4.0.14 → 4.0.16

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/dist/index.js CHANGED
@@ -18,7 +18,7 @@ import { PNG } from 'pngjs';
18
18
  import pm from 'pixelmatch';
19
19
  import { WebSocketServer } from 'ws';
20
20
 
21
- var version = "4.0.14";
21
+ var version = "4.0.16";
22
22
 
23
23
  const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
24
24
  function normalizeWindowsPath(input = "") {
@@ -1376,8 +1376,18 @@ body {
1376
1376
  });
1377
1377
  }
1378
1378
  };
1379
- return { optimizeDeps: rolldownVersion ? { rollupOptions: { plugins: [rolldownPlugin] } } : { esbuildOptions: { plugins: [esbuildPlugin] } } };
1379
+ return { optimizeDeps: rolldownVersion ? { rolldownOptions: { plugins: [rolldownPlugin] } } : { esbuildOptions: { plugins: [esbuildPlugin] } } };
1380
1380
  }
1381
+ },
1382
+ {
1383
+ name: "vitest:browser:__vitest_browser_import_meta_env_init__",
1384
+ transform: { handler(code) {
1385
+ // this transform runs after `vitest:meta-env-replacer` so that
1386
+ // `import.meta.env` will be handled by Vite import analysis to match behavior.
1387
+ if (code.includes("__vitest_browser_import_meta_env_init__")) {
1388
+ return code.replace("__vitest_browser_import_meta_env_init__", "import.meta.env");
1389
+ }
1390
+ } }
1381
1391
  }
1382
1392
  ];
1383
1393
  };
@@ -2680,223 +2690,209 @@ class ParentBrowserProject {
2680
2690
  }
2681
2691
  }
2682
2692
 
2693
+ //#region src/messages.ts
2683
2694
  const TYPE_REQUEST = "q";
2684
2695
  const TYPE_RESPONSE = "s";
2685
- const DEFAULT_TIMEOUT = 6e4;
2686
- function defaultSerialize(i) {
2687
- return i;
2688
- }
2689
- const defaultDeserialize = defaultSerialize;
2690
- const { clearTimeout, setTimeout: setTimeout$1 } = globalThis;
2691
- const random = Math.random.bind(Math);
2692
- function createBirpc($functions, options) {
2693
- const {
2694
- post,
2695
- on,
2696
- off = () => {
2697
- },
2698
- eventNames = [],
2699
- serialize = defaultSerialize,
2700
- deserialize = defaultDeserialize,
2701
- resolver,
2702
- bind = "rpc",
2703
- timeout = DEFAULT_TIMEOUT
2704
- } = options;
2705
- let $closed = false;
2706
- const _rpcPromiseMap = /* @__PURE__ */ new Map();
2707
- let _promiseInit;
2708
- async function _call(method, args, event, optional) {
2709
- if ($closed)
2710
- throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
2711
- const req = { m: method, a: args, t: TYPE_REQUEST };
2712
- if (optional)
2713
- req.o = true;
2714
- const send = async (_req) => post(serialize(_req));
2715
- if (event) {
2716
- await send(req);
2717
- return;
2718
- }
2719
- if (_promiseInit) {
2720
- try {
2721
- await _promiseInit;
2722
- } finally {
2723
- _promiseInit = void 0;
2724
- }
2725
- }
2726
- let { promise, resolve, reject } = createPromiseWithResolvers();
2727
- const id = nanoid();
2728
- req.i = id;
2729
- let timeoutId;
2730
- async function handler(newReq = req) {
2731
- if (timeout >= 0) {
2732
- timeoutId = setTimeout$1(() => {
2733
- try {
2734
- const handleResult = options.onTimeoutError?.(method, args);
2735
- if (handleResult !== true)
2736
- throw new Error(`[birpc] timeout on calling "${method}"`);
2737
- } catch (e) {
2738
- reject(e);
2739
- }
2740
- _rpcPromiseMap.delete(id);
2741
- }, timeout);
2742
- if (typeof timeoutId === "object")
2743
- timeoutId = timeoutId.unref?.();
2744
- }
2745
- _rpcPromiseMap.set(id, { resolve, reject, timeoutId, method });
2746
- await send(newReq);
2747
- return promise;
2748
- }
2749
- try {
2750
- if (options.onRequest)
2751
- await options.onRequest(req, handler, resolve);
2752
- else
2753
- await handler();
2754
- } catch (e) {
2755
- if (options.onGeneralError?.(e) !== true)
2756
- throw e;
2757
- return;
2758
- } finally {
2759
- clearTimeout(timeoutId);
2760
- _rpcPromiseMap.delete(id);
2761
- }
2762
- return promise;
2763
- }
2764
- const $call = (method, ...args) => _call(method, args, false);
2765
- const $callOptional = (method, ...args) => _call(method, args, false, true);
2766
- const $callEvent = (method, ...args) => _call(method, args, true);
2767
- const $callRaw = (options2) => _call(options2.method, options2.args, options2.event, options2.optional);
2768
- const builtinMethods = {
2769
- $call,
2770
- $callOptional,
2771
- $callEvent,
2772
- $callRaw,
2773
- $rejectPendingCalls,
2774
- get $closed() {
2775
- return $closed;
2776
- },
2777
- $close,
2778
- $functions
2779
- };
2780
- const rpc = new Proxy({}, {
2781
- get(_, method) {
2782
- if (Object.prototype.hasOwnProperty.call(builtinMethods, method))
2783
- return builtinMethods[method];
2784
- if (method === "then" && !eventNames.includes("then") && !("then" in $functions))
2785
- return void 0;
2786
- const sendEvent = (...args) => _call(method, args, true);
2787
- if (eventNames.includes(method)) {
2788
- sendEvent.asEvent = sendEvent;
2789
- return sendEvent;
2790
- }
2791
- const sendCall = (...args) => _call(method, args, false);
2792
- sendCall.asEvent = sendEvent;
2793
- return sendCall;
2794
- }
2795
- });
2796
- function $close(customError) {
2797
- $closed = true;
2798
- _rpcPromiseMap.forEach(({ reject, method }) => {
2799
- const error = new Error(`[birpc] rpc is closed, cannot call "${method}"`);
2800
- if (customError) {
2801
- customError.cause ??= error;
2802
- return reject(customError);
2803
- }
2804
- reject(error);
2805
- });
2806
- _rpcPromiseMap.clear();
2807
- off(onMessage);
2808
- }
2809
- function $rejectPendingCalls(handler) {
2810
- const entries = Array.from(_rpcPromiseMap.values());
2811
- const handlerResults = entries.map(({ method, reject }) => {
2812
- if (!handler) {
2813
- return reject(new Error(`[birpc]: rejected pending call "${method}".`));
2814
- }
2815
- return handler({ method, reject });
2816
- });
2817
- _rpcPromiseMap.clear();
2818
- return handlerResults;
2819
- }
2820
- async function onMessage(data, ...extra) {
2821
- let msg;
2822
- try {
2823
- msg = deserialize(data);
2824
- } catch (e) {
2825
- if (options.onGeneralError?.(e) !== true)
2826
- throw e;
2827
- return;
2828
- }
2829
- if (msg.t === TYPE_REQUEST) {
2830
- const { m: method, a: args, o: optional } = msg;
2831
- let result, error;
2832
- let fn = await (resolver ? resolver(method, $functions[method]) : $functions[method]);
2833
- if (optional)
2834
- fn ||= () => void 0;
2835
- if (!fn) {
2836
- error = new Error(`[birpc] function "${method}" not found`);
2837
- } else {
2838
- try {
2839
- result = await fn.apply(bind === "rpc" ? rpc : $functions, args);
2840
- } catch (e) {
2841
- error = e;
2842
- }
2843
- }
2844
- if (msg.i) {
2845
- if (error && options.onError)
2846
- options.onError(error, method, args);
2847
- if (error && options.onFunctionError) {
2848
- if (options.onFunctionError(error, method, args) === true)
2849
- return;
2850
- }
2851
- if (!error) {
2852
- try {
2853
- await post(serialize({ t: TYPE_RESPONSE, i: msg.i, r: result }), ...extra);
2854
- return;
2855
- } catch (e) {
2856
- error = e;
2857
- if (options.onGeneralError?.(e, method, args) !== true)
2858
- throw e;
2859
- }
2860
- }
2861
- try {
2862
- await post(serialize({ t: TYPE_RESPONSE, i: msg.i, e: error }), ...extra);
2863
- } catch (e) {
2864
- if (options.onGeneralError?.(e, method, args) !== true)
2865
- throw e;
2866
- }
2867
- }
2868
- } else {
2869
- const { i: ack, r: result, e: error } = msg;
2870
- const promise = _rpcPromiseMap.get(ack);
2871
- if (promise) {
2872
- clearTimeout(promise.timeoutId);
2873
- if (error)
2874
- promise.reject(error);
2875
- else
2876
- promise.resolve(result);
2877
- }
2878
- _rpcPromiseMap.delete(ack);
2879
- }
2880
- }
2881
- _promiseInit = on(onMessage);
2882
- return rpc;
2883
- }
2696
+
2697
+ //#endregion
2698
+ //#region src/utils.ts
2884
2699
  function createPromiseWithResolvers() {
2885
- let resolve;
2886
- let reject;
2887
- const promise = new Promise((res, rej) => {
2888
- resolve = res;
2889
- reject = rej;
2890
- });
2891
- return { promise, resolve, reject };
2700
+ let resolve;
2701
+ let reject;
2702
+ return {
2703
+ promise: new Promise((res, rej) => {
2704
+ resolve = res;
2705
+ reject = rej;
2706
+ }),
2707
+ resolve,
2708
+ reject
2709
+ };
2892
2710
  }
2711
+ const random = Math.random.bind(Math);
2893
2712
  const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
2894
2713
  function nanoid(size = 21) {
2895
- let id = "";
2896
- let i = size;
2897
- while (i--)
2898
- id += urlAlphabet[random() * 64 | 0];
2899
- return id;
2714
+ let id = "";
2715
+ let i = size;
2716
+ while (i--) id += urlAlphabet[random() * 64 | 0];
2717
+ return id;
2718
+ }
2719
+
2720
+ //#endregion
2721
+ //#region src/main.ts
2722
+ const DEFAULT_TIMEOUT = 6e4;
2723
+ const defaultSerialize = (i) => i;
2724
+ const defaultDeserialize = defaultSerialize;
2725
+ const { clearTimeout, setTimeout: setTimeout$1 } = globalThis;
2726
+ function createBirpc($functions, options) {
2727
+ const { post, on, off = () => {}, eventNames = [], serialize = defaultSerialize, deserialize = defaultDeserialize, resolver, bind = "rpc", timeout = DEFAULT_TIMEOUT, proxify = true } = options;
2728
+ let $closed = false;
2729
+ const _rpcPromiseMap = /* @__PURE__ */ new Map();
2730
+ let _promiseInit;
2731
+ let rpc;
2732
+ async function _call(method, args, event, optional) {
2733
+ if ($closed) throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
2734
+ const req = {
2735
+ m: method,
2736
+ a: args,
2737
+ t: TYPE_REQUEST
2738
+ };
2739
+ if (optional) req.o = true;
2740
+ const send = async (_req) => post(serialize(_req));
2741
+ if (event) {
2742
+ await send(req);
2743
+ return;
2744
+ }
2745
+ if (_promiseInit) try {
2746
+ await _promiseInit;
2747
+ } finally {
2748
+ _promiseInit = void 0;
2749
+ }
2750
+ let { promise, resolve, reject } = createPromiseWithResolvers();
2751
+ const id = nanoid();
2752
+ req.i = id;
2753
+ let timeoutId;
2754
+ async function handler(newReq = req) {
2755
+ if (timeout >= 0) {
2756
+ timeoutId = setTimeout$1(() => {
2757
+ try {
2758
+ if (options.onTimeoutError?.call(rpc, method, args) !== true) throw new Error(`[birpc] timeout on calling "${method}"`);
2759
+ } catch (e) {
2760
+ reject(e);
2761
+ }
2762
+ _rpcPromiseMap.delete(id);
2763
+ }, timeout);
2764
+ if (typeof timeoutId === "object") timeoutId = timeoutId.unref?.();
2765
+ }
2766
+ _rpcPromiseMap.set(id, {
2767
+ resolve,
2768
+ reject,
2769
+ timeoutId,
2770
+ method
2771
+ });
2772
+ await send(newReq);
2773
+ return promise;
2774
+ }
2775
+ try {
2776
+ if (options.onRequest) await options.onRequest.call(rpc, req, handler, resolve);
2777
+ else await handler();
2778
+ } catch (e) {
2779
+ if (options.onGeneralError?.call(rpc, e) !== true) throw e;
2780
+ return;
2781
+ } finally {
2782
+ clearTimeout(timeoutId);
2783
+ _rpcPromiseMap.delete(id);
2784
+ }
2785
+ return promise;
2786
+ }
2787
+ const builtinMethods = {
2788
+ $call: (method, ...args) => _call(method, args, false),
2789
+ $callOptional: (method, ...args) => _call(method, args, false, true),
2790
+ $callEvent: (method, ...args) => _call(method, args, true),
2791
+ $callRaw: (options$1) => _call(options$1.method, options$1.args, options$1.event, options$1.optional),
2792
+ $rejectPendingCalls,
2793
+ get $closed() {
2794
+ return $closed;
2795
+ },
2796
+ get $meta() {
2797
+ return options.meta;
2798
+ },
2799
+ $close,
2800
+ $functions
2801
+ };
2802
+ if (proxify) rpc = new Proxy({}, { get(_, method) {
2803
+ if (Object.prototype.hasOwnProperty.call(builtinMethods, method)) return builtinMethods[method];
2804
+ if (method === "then" && !eventNames.includes("then") && !("then" in $functions)) return void 0;
2805
+ const sendEvent = (...args) => _call(method, args, true);
2806
+ if (eventNames.includes(method)) {
2807
+ sendEvent.asEvent = sendEvent;
2808
+ return sendEvent;
2809
+ }
2810
+ const sendCall = (...args) => _call(method, args, false);
2811
+ sendCall.asEvent = sendEvent;
2812
+ return sendCall;
2813
+ } });
2814
+ else rpc = builtinMethods;
2815
+ function $close(customError) {
2816
+ $closed = true;
2817
+ _rpcPromiseMap.forEach(({ reject, method }) => {
2818
+ const error = /* @__PURE__ */ new Error(`[birpc] rpc is closed, cannot call "${method}"`);
2819
+ if (customError) {
2820
+ customError.cause ??= error;
2821
+ return reject(customError);
2822
+ }
2823
+ reject(error);
2824
+ });
2825
+ _rpcPromiseMap.clear();
2826
+ off(onMessage);
2827
+ }
2828
+ function $rejectPendingCalls(handler) {
2829
+ const handlerResults = Array.from(_rpcPromiseMap.values()).map(({ method, reject }) => {
2830
+ if (!handler) return reject(/* @__PURE__ */ new Error(`[birpc]: rejected pending call "${method}".`));
2831
+ return handler({
2832
+ method,
2833
+ reject
2834
+ });
2835
+ });
2836
+ _rpcPromiseMap.clear();
2837
+ return handlerResults;
2838
+ }
2839
+ async function onMessage(data, ...extra) {
2840
+ let msg;
2841
+ try {
2842
+ msg = deserialize(data);
2843
+ } catch (e) {
2844
+ if (options.onGeneralError?.call(rpc, e) !== true) throw e;
2845
+ return;
2846
+ }
2847
+ if (msg.t === TYPE_REQUEST) {
2848
+ const { m: method, a: args, o: optional } = msg;
2849
+ let result, error;
2850
+ let fn = await (resolver ? resolver.call(rpc, method, $functions[method]) : $functions[method]);
2851
+ if (optional) fn ||= () => void 0;
2852
+ if (!fn) error = /* @__PURE__ */ new Error(`[birpc] function "${method}" not found`);
2853
+ else try {
2854
+ result = await fn.apply(bind === "rpc" ? rpc : $functions, args);
2855
+ } catch (e) {
2856
+ error = e;
2857
+ }
2858
+ if (msg.i) {
2859
+ if (error && options.onFunctionError) {
2860
+ if (options.onFunctionError.call(rpc, error, method, args) === true) return;
2861
+ }
2862
+ if (!error) try {
2863
+ await post(serialize({
2864
+ t: TYPE_RESPONSE,
2865
+ i: msg.i,
2866
+ r: result
2867
+ }), ...extra);
2868
+ return;
2869
+ } catch (e) {
2870
+ error = e;
2871
+ if (options.onGeneralError?.call(rpc, e, method, args) !== true) throw e;
2872
+ }
2873
+ try {
2874
+ await post(serialize({
2875
+ t: TYPE_RESPONSE,
2876
+ i: msg.i,
2877
+ e: error
2878
+ }), ...extra);
2879
+ } catch (e) {
2880
+ if (options.onGeneralError?.call(rpc, e, method, args) !== true) throw e;
2881
+ }
2882
+ }
2883
+ } else {
2884
+ const { i: ack, r: result, e: error } = msg;
2885
+ const promise = _rpcPromiseMap.get(ack);
2886
+ if (promise) {
2887
+ clearTimeout(promise.timeoutId);
2888
+ if (error) promise.reject(error);
2889
+ else promise.resolve(result);
2890
+ }
2891
+ _rpcPromiseMap.delete(ack);
2892
+ }
2893
+ }
2894
+ _promiseInit = on(onMessage);
2895
+ return rpc;
2900
2896
  }
2901
2897
 
2902
2898
  const debug = createDebugger("vitest:browser:api");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/browser",
3
3
  "type": "module",
4
- "version": "4.0.14",
4
+ "version": "4.0.16",
5
5
  "description": "Browser running for Vitest",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -51,7 +51,7 @@
51
51
  "providers"
52
52
  ],
53
53
  "peerDependencies": {
54
- "vitest": "4.0.14"
54
+ "vitest": "4.0.16"
55
55
  },
56
56
  "dependencies": {
57
57
  "magic-string": "^0.30.21",
@@ -60,20 +60,20 @@
60
60
  "sirv": "^3.0.2",
61
61
  "tinyrainbow": "^3.0.3",
62
62
  "ws": "^8.18.3",
63
- "@vitest/utils": "4.0.14",
64
- "@vitest/mocker": "4.0.14"
63
+ "@vitest/mocker": "4.0.16",
64
+ "@vitest/utils": "4.0.16"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@testing-library/user-event": "^14.6.1",
68
68
  "@types/pngjs": "^6.0.5",
69
69
  "@types/ws": "^8.18.1",
70
- "birpc": "^2.8.0",
70
+ "birpc": "^4.0.0",
71
71
  "flatted": "^3.3.3",
72
72
  "ivya": "^1.7.0",
73
73
  "mime": "^4.1.0",
74
74
  "pathe": "^2.0.3",
75
- "@vitest/runner": "4.0.14",
76
- "vitest": "4.0.14"
75
+ "@vitest/runner": "4.0.16",
76
+ "vitest": "4.0.16"
77
77
  },
78
78
  "scripts": {
79
79
  "typecheck": "tsc -p ./src/client/tsconfig.json --noEmit",