市场夺宝奇兵
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
848 B

  1. import {setTimeout} from 'node:timers/promises';
  2. import {DiscardedError} from '../return/final-error.js';
  3. // Validate `timeout` option
  4. export const validateTimeout = ({timeout}) => {
  5. if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {
  6. throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);
  7. }
  8. };
  9. // Fails when the `timeout` option is exceeded
  10. export const throwOnTimeout = (subprocess, timeout, context, controller) => timeout === 0 || timeout === undefined
  11. ? []
  12. : [killAfterTimeout(subprocess, timeout, context, controller)];
  13. const killAfterTimeout = async (subprocess, timeout, context, {signal}) => {
  14. await setTimeout(timeout, undefined, {signal});
  15. context.terminationReason ??= 'timeout';
  16. subprocess.kill();
  17. throw new DiscardedError();
  18. };