@axe-core/watcher 4.3.0-next.edd20fc3 → 4.3.0-next.f6c299b0

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.
@@ -101,8 +101,70 @@ export const shouldUseEnvProxy = function () {
101
101
  * @property {Object} options - The fetch options.
102
102
  * @property {number} timeout - The timeout in milliseconds.
103
103
  * @property {MessagePort} port - The message port for communication.
104
+ * @property {SharedArrayBuffer} signalBuffer - Used to wake the main thread.
105
+ * Encoding: Int32Array of length 2.
106
+ * [0] wake-reason: 0 = still waiting, 1 = postMessage delivered,
107
+ * 2 = worker died before delivering a message.
108
+ * [1] exit code when [0] === 2; otherwise 0.
104
109
  */
105
- const { url, options, timeout, port } = workerData
110
+ const SIGNAL_WAITING = 0
111
+ const SIGNAL_POSTMESSAGE = 1
112
+ const SIGNAL_DIED = 2
113
+
114
+ const { url, options, timeout, port, signalBuffer } = workerData
115
+ const signalArray = new Int32Array(signalBuffer)
116
+
117
+ /**
118
+ * Wake the main thread, which is blocked in Atomics.wait on signalArray[0].
119
+ * Call after every postMessage so the caller never spins waiting for a
120
+ * message that's already arrived. Idempotent — once signalArray[0] has been
121
+ * set, re-entry is a no-op.
122
+ */
123
+ function notifyMainThread() {
124
+ if (Atomics.load(signalArray, 0) !== SIGNAL_WAITING) {
125
+ return
126
+ }
127
+ Atomics.store(signalArray, 0, SIGNAL_POSTMESSAGE)
128
+ Atomics.notify(signalArray, 0)
129
+ }
130
+
131
+ // Worker-death watchdogs. These run inside the worker thread (unlike
132
+ // worker.on('error') / worker.on('exit') on the parent, which can't fire
133
+ // before syncFetch's synchronous throw because libuv events don't deliver
134
+ // while the main thread is in Atomics.wait or running sync code).
135
+ //
136
+ // They sit between top-level destructuring and the fetch try-block, which
137
+ // means any error originating before this point (bad workerData, broken
138
+ // imports) still falls back to the main thread's plain timeout. That trade
139
+ // is acceptable: catastrophic-startup is rare and unrecoverable.
140
+ process.on('uncaughtException', error => {
141
+ if (Atomics.load(signalArray, 0) !== SIGNAL_WAITING) {
142
+ return
143
+ }
144
+ // Forward through the normal port channel so the caller gets the full
145
+ // error chain. If postMessage itself throws (e.g. error wasn't cloneable),
146
+ // we have no remaining channel to deliver the error — force an exit so
147
+ // the death watchdog below fires synchronously and wakes the parent.
148
+ // Without this, installing an uncaughtException handler suppresses Node's
149
+ // default exit and the parent blocks for the full mainThreadTimeout.
150
+ try {
151
+ const wrapped = new FetchWorkerError({ url: sanitizeUrl(url) })
152
+ wrapped.cause = error
153
+ port.postMessage({ error: wrapped })
154
+ notifyMainThread()
155
+ } catch {
156
+ process.exit(1)
157
+ }
158
+ })
159
+
160
+ process.on('exit', code => {
161
+ if (Atomics.load(signalArray, 0) !== SIGNAL_WAITING) {
162
+ return
163
+ }
164
+ Atomics.store(signalArray, 1, code)
165
+ Atomics.store(signalArray, 0, SIGNAL_DIED)
166
+ Atomics.notify(signalArray, 0)
167
+ })
106
168
 
107
169
  try {
108
170
  let dispatcher = getGlobalDispatcher()
@@ -133,6 +195,7 @@ try {
133
195
  port.postMessage({
134
196
  data: responseData
135
197
  })
198
+ notifyMainThread()
136
199
  } catch (error) {
137
200
  const HTTP_PROXY = process.env.http_proxy || process.env.HTTP_PROXY
138
201
  const HTTPS_PROXY = process.env.https_proxy || process.env.HTTPS_PROXY
@@ -165,6 +228,7 @@ try {
165
228
  port.postMessage({
166
229
  error: err
167
230
  })
231
+ notifyMainThread()
168
232
  } finally {
169
233
  port.close()
170
234
  }
@@ -1,5 +1,13 @@
1
1
  export interface SyncFetchCustomOptions {
2
2
  timeout?: number;
3
+ /**
4
+ * @internal Test-only override for the worker script path. Allows tests to
5
+ * substitute a fixture worker that intentionally dies before notifying, so
6
+ * the in-worker death watchdogs (process.on('uncaughtException') /
7
+ * process.on('exit')) and the signalBuffer-driven timeout error can be
8
+ * exercised end-to-end.
9
+ */
10
+ _workerPath?: string;
3
11
  }
4
12
  export interface SyncFetchResponse {
5
13
  status: number;
@@ -9,3 +17,27 @@ export interface SyncFetchResponse {
9
17
  ok: boolean;
10
18
  }
11
19
  export declare function syncFetch(url: string, options?: RequestInit, customOptions?: SyncFetchCustomOptions): SyncFetchResponse;
20
+ /**
21
+ * Wake-reason values written to `signalArray[0]` by sync-fetch-worker.mjs.
22
+ *
23
+ * @internal Exported for tests.
24
+ */
25
+ export declare const SIGNAL_WAITING = 0;
26
+ /** @internal */
27
+ export declare const SIGNAL_POSTMESSAGE = 1;
28
+ /** @internal */
29
+ export declare const SIGNAL_DIED = 2;
30
+ /**
31
+ * Build the diagnostic Error thrown when the worker fails to deliver a message
32
+ * before the main-thread timeout. When the worker's death-watchdog fired
33
+ * (`wakeReason === SIGNAL_DIED`) the exit code is folded into the message so
34
+ * the failure is debuggable instead of opaque. Otherwise the wait simply
35
+ * timed out and we report the plain timeout.
36
+ *
37
+ * @internal Exported for tests.
38
+ */
39
+ export declare function buildTimeoutError(args: {
40
+ mainThreadTimeout: number;
41
+ wakeReason: number;
42
+ exitCode: number;
43
+ }): Error;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SIGNAL_DIED = exports.SIGNAL_POSTMESSAGE = exports.SIGNAL_WAITING = void 0;
3
4
  exports.syncFetch = syncFetch;
5
+ exports.buildTimeoutError = buildTimeoutError;
4
6
  const node_worker_threads_1 = require("node:worker_threads");
5
7
  const node_path_1 = require("node:path");
6
8
  function syncFetch(url, options = {}, customOptions = {
@@ -10,28 +12,34 @@ function syncFetch(url, options = {}, customOptions = {
10
12
  const processingBuffer = 3000;
11
13
  const mainThreadTimeout = timeout + processingBuffer;
12
14
  const { port1, port2 } = new node_worker_threads_1.MessageChannel();
13
- const sleepBuffer = new SharedArrayBuffer(4);
14
- const sleepArray = new Int32Array(sleepBuffer);
15
- const worker = new node_worker_threads_1.Worker((0, node_path_1.join)(__dirname, 'sync-fetch-worker.mjs'), {
15
+ const signalBuffer = new SharedArrayBuffer(8);
16
+ const signalArray = new Int32Array(signalBuffer);
17
+ const workerPath = customOptions._workerPath ?? (0, node_path_1.join)(__dirname, 'sync-fetch-worker.mjs');
18
+ const worker = new node_worker_threads_1.Worker(workerPath, {
16
19
  name: 'sync-fetch',
17
20
  eval: false,
18
21
  workerData: {
19
22
  url,
20
23
  options,
21
24
  timeout,
22
- port: port2
25
+ port: port2,
26
+ signalBuffer
23
27
  },
24
28
  transferList: [port2]
25
29
  });
26
- const { startTime } = performance.mark('axe-watcher:sync-fetch:start');
30
+ worker.on('error', () => {
31
+ });
32
+ performance.mark('axe-watcher:sync-fetch:start');
27
33
  let message;
28
34
  try {
29
- while (message === undefined) {
30
- if (performance.now() - startTime > mainThreadTimeout) {
31
- throw new Error(`Request timed out - worker thread did not respond within ${mainThreadTimeout}ms`);
32
- }
33
- Atomics.wait(sleepArray, 0, 0, 5);
34
- message = (0, node_worker_threads_1.receiveMessageOnPort)(port1);
35
+ Atomics.wait(signalArray, 0, 0, mainThreadTimeout);
36
+ message = (0, node_worker_threads_1.receiveMessageOnPort)(port1);
37
+ if (message === undefined) {
38
+ throw buildTimeoutError({
39
+ mainThreadTimeout,
40
+ wakeReason: Atomics.load(signalArray, 0),
41
+ exitCode: Atomics.load(signalArray, 1)
42
+ });
35
43
  }
36
44
  const { data, error } = message.message;
37
45
  if (error) {
@@ -63,6 +71,16 @@ function syncFetch(url, options = {}, customOptions = {
63
71
  });
64
72
  }
65
73
  }
74
+ exports.SIGNAL_WAITING = 0;
75
+ exports.SIGNAL_POSTMESSAGE = 1;
76
+ exports.SIGNAL_DIED = 2;
77
+ function buildTimeoutError(args) {
78
+ const { mainThreadTimeout, wakeReason, exitCode } = args;
79
+ const reason = wakeReason === exports.SIGNAL_DIED
80
+ ? `worker exited with code ${exitCode} before responding`
81
+ : `worker thread did not respond within ${mainThreadTimeout}ms`;
82
+ return new Error(`Request timed out - ${reason}`);
83
+ }
66
84
  function sanitizeHeaders(headers) {
67
85
  if (!headers) {
68
86
  return undefined;
@@ -1 +1 @@
1
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