clock.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var clock_exports = {};
  30. __export(clock_exports, {
  31. Clock: () => Clock
  32. });
  33. module.exports = __toCommonJS(clock_exports);
  34. var rawClockSource = __toESM(require("../generated/clockSource"));
  35. class Clock {
  36. constructor(browserContext) {
  37. this._initScripts = [];
  38. this._browserContext = browserContext;
  39. }
  40. async uninstall(progress) {
  41. await progress.race(this._browserContext.removeInitScripts(this._initScripts));
  42. this._initScripts = [];
  43. }
  44. async fastForward(progress, ticks) {
  45. await this._installIfNeeded(progress);
  46. const ticksMillis = parseTicks(ticks);
  47. this._initScripts.push(await this._browserContext.addInitScript(progress, `globalThis.__pwClock.controller.log('fastForward', ${Date.now()}, ${ticksMillis})`));
  48. await progress.race(this._evaluateInFrames(`globalThis.__pwClock.controller.fastForward(${ticksMillis})`));
  49. }
  50. async install(progress, time) {
  51. await this._installIfNeeded(progress);
  52. const timeMillis = time !== void 0 ? parseTime(time) : Date.now();
  53. this._initScripts.push(await this._browserContext.addInitScript(progress, `globalThis.__pwClock.controller.log('install', ${Date.now()}, ${timeMillis})`));
  54. await progress.race(this._evaluateInFrames(`globalThis.__pwClock.controller.install(${timeMillis})`));
  55. }
  56. async pauseAt(progress, ticks) {
  57. await this._installIfNeeded(progress);
  58. const timeMillis = parseTime(ticks);
  59. this._initScripts.push(await this._browserContext.addInitScript(progress, `globalThis.__pwClock.controller.log('pauseAt', ${Date.now()}, ${timeMillis})`));
  60. await progress.race(this._evaluateInFrames(`globalThis.__pwClock.controller.pauseAt(${timeMillis})`));
  61. }
  62. resumeNoReply() {
  63. if (!this._initScripts.length)
  64. return;
  65. const doResume = async () => {
  66. this._initScripts.push(await this._browserContext.addInitScript(void 0, `globalThis.__pwClock.controller.log('resume', ${Date.now()})`));
  67. await this._evaluateInFrames(`globalThis.__pwClock.controller.resume()`);
  68. };
  69. doResume().catch(() => {
  70. });
  71. }
  72. async resume(progress) {
  73. await this._installIfNeeded(progress);
  74. this._initScripts.push(await this._browserContext.addInitScript(progress, `globalThis.__pwClock.controller.log('resume', ${Date.now()})`));
  75. await progress.race(this._evaluateInFrames(`globalThis.__pwClock.controller.resume()`));
  76. }
  77. async setFixedTime(progress, time) {
  78. await this._installIfNeeded(progress);
  79. const timeMillis = parseTime(time);
  80. this._initScripts.push(await this._browserContext.addInitScript(progress, `globalThis.__pwClock.controller.log('setFixedTime', ${Date.now()}, ${timeMillis})`));
  81. await progress.race(this._evaluateInFrames(`globalThis.__pwClock.controller.setFixedTime(${timeMillis})`));
  82. }
  83. async setSystemTime(progress, time) {
  84. await this._installIfNeeded(progress);
  85. const timeMillis = parseTime(time);
  86. this._initScripts.push(await this._browserContext.addInitScript(progress, `globalThis.__pwClock.controller.log('setSystemTime', ${Date.now()}, ${timeMillis})`));
  87. await progress.race(this._evaluateInFrames(`globalThis.__pwClock.controller.setSystemTime(${timeMillis})`));
  88. }
  89. async runFor(progress, ticks) {
  90. await this._installIfNeeded(progress);
  91. const ticksMillis = parseTicks(ticks);
  92. this._initScripts.push(await this._browserContext.addInitScript(progress, `globalThis.__pwClock.controller.log('runFor', ${Date.now()}, ${ticksMillis})`));
  93. await progress.race(this._evaluateInFrames(`globalThis.__pwClock.controller.runFor(${ticksMillis})`));
  94. }
  95. async _installIfNeeded(progress) {
  96. if (this._initScripts.length)
  97. return;
  98. const script = `(() => {
  99. const module = {};
  100. ${rawClockSource.source}
  101. if (!globalThis.__pwClock)
  102. globalThis.__pwClock = (module.exports.inject())(globalThis);
  103. })();`;
  104. const initScript = await this._browserContext.addInitScript(progress, script);
  105. await progress.race(this._evaluateInFrames(script));
  106. this._initScripts.push(initScript);
  107. }
  108. async _evaluateInFrames(script) {
  109. await this._browserContext.safeNonStallingEvaluateInAllFrames(script, "main", { throwOnJSErrors: true });
  110. }
  111. }
  112. function parseTicks(value) {
  113. if (typeof value === "number")
  114. return value;
  115. if (!value)
  116. return 0;
  117. const str = value;
  118. const strings = str.split(":");
  119. const l = strings.length;
  120. let i = l;
  121. let ms = 0;
  122. let parsed;
  123. if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
  124. throw new Error(
  125. `Clock only understands numbers, 'mm:ss' and 'hh:mm:ss'`
  126. );
  127. }
  128. while (i--) {
  129. parsed = parseInt(strings[i], 10);
  130. if (parsed >= 60)
  131. throw new Error(`Invalid time ${str}`);
  132. ms += parsed * Math.pow(60, l - i - 1);
  133. }
  134. return ms * 1e3;
  135. }
  136. function parseTime(epoch) {
  137. if (!epoch)
  138. return 0;
  139. if (typeof epoch === "number")
  140. return epoch;
  141. const parsed = new Date(epoch);
  142. if (!isFinite(parsed.getTime()))
  143. throw new Error(`Invalid date: ${epoch}`);
  144. return parsed.getTime();
  145. }
  146. // Annotate the CommonJS export names for ESM import in node:
  147. 0 && (module.exports = {
  148. Clock
  149. });