Sha256: a270a55ce60949ad1e174d295867627d26c9e0996132acd9d0fe1ab90f7727a6

Contents?: true

Size: 1.82 KB

Versions: 26

Compression:

Stored size: 1.82 KB

Contents

import { isNodeProcess } from "is-node-process";
const SET_TIMEOUT_MAX_ALLOWED_INT = 2147483647;
const MIN_SERVER_RESPONSE_TIME = 100;
const MAX_SERVER_RESPONSE_TIME = 400;
const NODE_SERVER_RESPONSE_TIME = 5;
function getRealisticResponseTime() {
  if (isNodeProcess()) {
    return NODE_SERVER_RESPONSE_TIME;
  }
  return Math.floor(
    Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) + MIN_SERVER_RESPONSE_TIME
  );
}
async function delay(durationOrMode) {
  let delayTime;
  if (typeof durationOrMode === "string") {
    switch (durationOrMode) {
      case "infinite": {
        delayTime = SET_TIMEOUT_MAX_ALLOWED_INT;
        break;
      }
      case "real": {
        delayTime = getRealisticResponseTime();
        break;
      }
      default: {
        throw new Error(
          `Failed to delay a response: unknown delay mode "${durationOrMode}". Please make sure you provide one of the supported modes ("real", "infinite") or a number.`
        );
      }
    }
  } else if (typeof durationOrMode === "undefined") {
    delayTime = getRealisticResponseTime();
  } else {
    if (durationOrMode > SET_TIMEOUT_MAX_ALLOWED_INT) {
      throw new Error(
        `Failed to delay a response: provided delay duration (${durationOrMode}) exceeds the maximum allowed duration for "setTimeout" (${SET_TIMEOUT_MAX_ALLOWED_INT}). This will cause the response to be returned immediately. Please use a number within the allowed range to delay the response by exact duration, or consider the "infinite" delay mode to delay the response indefinitely.`
      );
    }
    delayTime = durationOrMode;
  }
  return new Promise((resolve) => setTimeout(resolve, delayTime));
}
export {
  MAX_SERVER_RESPONSE_TIME,
  MIN_SERVER_RESPONSE_TIME,
  NODE_SERVER_RESPONSE_TIME,
  SET_TIMEOUT_MAX_ALLOWED_INT,
  delay
};
//# sourceMappingURL=delay.mjs.map

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.25 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.24 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.23 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.22 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.21 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.20 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.19 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.18 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.17 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.16 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.15 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.14 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.13 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.12 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.11 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.10 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.9 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.8 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs
clapton-0.0.7 lib/clapton/javascripts/node_modules/msw/lib/core/delay.mjs