90 lines
No EOL
2.7 KiB
TypeScript
90 lines
No EOL
2.7 KiB
TypeScript
import Buffer from 'buffer';
|
|
|
|
/**
|
|
* Polyfill for Buffer in browser environment
|
|
*
|
|
* Many Node.js libraries like isomorphic-git and bitcoinjs-lib expect Buffer to be globally available.
|
|
* This polyfill makes the buffer package's Buffer available globally.
|
|
*/
|
|
if (!globalThis.Buffer) {
|
|
globalThis.Buffer = Buffer.Buffer;
|
|
}
|
|
|
|
/**
|
|
* Polyfill for AbortSignal.any()
|
|
*
|
|
* AbortSignal.any() creates an AbortSignal that will be aborted when any of the
|
|
* provided signals are aborted. This is useful for combining multiple abort signals.
|
|
*
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static
|
|
*/
|
|
|
|
// Check if AbortSignal.any is already available
|
|
if (!AbortSignal.any) {
|
|
AbortSignal.any = function(signals: AbortSignal[]): AbortSignal {
|
|
// If no signals provided, return a signal that never aborts
|
|
if (signals.length === 0) {
|
|
return new AbortController().signal;
|
|
}
|
|
|
|
// If only one signal, return it directly for efficiency
|
|
if (signals.length === 1) {
|
|
return signals[0];
|
|
}
|
|
|
|
// Check if any signal is already aborted
|
|
for (const signal of signals) {
|
|
if (signal.aborted) {
|
|
// Create an already-aborted signal with the same reason
|
|
const controller = new AbortController();
|
|
controller.abort(signal.reason);
|
|
return controller.signal;
|
|
}
|
|
}
|
|
|
|
// Create a new controller for the combined signal
|
|
const controller = new AbortController();
|
|
|
|
// Function to abort the combined signal
|
|
const onAbort = (event: Event) => {
|
|
const target = event.target as AbortSignal;
|
|
controller.abort(target.reason);
|
|
};
|
|
|
|
// Listen for abort events on all input signals
|
|
for (const signal of signals) {
|
|
signal.addEventListener('abort', onAbort, { once: true });
|
|
}
|
|
|
|
// Clean up listeners when the combined signal is aborted
|
|
controller.signal.addEventListener('abort', () => {
|
|
for (const signal of signals) {
|
|
signal.removeEventListener('abort', onAbort);
|
|
}
|
|
}, { once: true });
|
|
|
|
return controller.signal;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Polyfill for AbortSignal.timeout()
|
|
*
|
|
* AbortSignal.timeout() creates an AbortSignal that will be aborted after a
|
|
* specified number of milliseconds.
|
|
*
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static
|
|
*/
|
|
|
|
// Check if AbortSignal.timeout is already available
|
|
if (!AbortSignal.timeout) {
|
|
AbortSignal.timeout = function(milliseconds: number): AbortSignal {
|
|
const controller = new AbortController();
|
|
|
|
setTimeout(() => {
|
|
controller.abort(new DOMException('The operation was aborted due to timeout', 'TimeoutError'));
|
|
}, milliseconds);
|
|
|
|
return controller.signal;
|
|
};
|
|
} |