@cloudflare/vitest-pool-workers 0.16.10 → 0.16.12

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.
@@ -320,11 +320,11 @@ function createProxyPrototypeClass(superClass, getUnknownPrototypeKey) {
320
320
  }
321
321
  /**
322
322
  * Only properties and methods declared on the prototype can be accessed over
323
- * RPC. This function gets a property from the prototype if it's defined, and
324
- * throws a helpful error message if not. Note we need to distinguish between a
325
- * property that returns `undefined` and something not being defined at all.
323
+ * RPC. This function throws a helpful error message if a property isn't
324
+ * defined. Note we need to distinguish between a property that returns
325
+ * `undefined` and something not being defined at all.
326
326
  */
327
- function getRPCProperty(ctor, instance, key) {
327
+ function assertRPCPropertyAccessible(ctor, instance, key) {
328
328
  if (!Reflect.has(ctor.prototype, key)) {
329
329
  const quotedKey = JSON.stringify(key);
330
330
  const instanceHasKey = Reflect.has(instance, key);
@@ -338,6 +338,9 @@ function getRPCProperty(ctor, instance, key) {
338
338
  else message = `The RPC receiver does not implement ${quotedKey}.`;
339
339
  throw new TypeError(message);
340
340
  }
341
+ }
342
+ function getRPCProperty(ctor, instance, key) {
343
+ assertRPCPropertyAccessible(ctor, instance, key);
341
344
  return Reflect.get(ctor.prototype, key, instance);
342
345
  }
343
346
  /**
@@ -358,7 +361,7 @@ function getRPCProperty(ctor, instance, key) {
358
361
  */
359
362
  function getRPCPropertyCallableThenable(key, property, queueOwner) {
360
363
  const fn = async function(...args) {
361
- return enqueueRPCInvocation(queueOwner, async (release) => {
364
+ return enqueueInvocation(queueOwner, async (release) => {
362
365
  try {
363
366
  const maybeFn = await property;
364
367
  if (typeof maybeFn === "function") return maybeFn(...args);
@@ -373,18 +376,19 @@ function getRPCPropertyCallableThenable(key, property, queueOwner) {
373
376
  fn.finally = (onFinally) => property.finally(onFinally);
374
377
  return fn;
375
378
  }
376
- const rpcInvocationQueues = /* @__PURE__ */ new WeakMap();
379
+ const invocationQueues = /* @__PURE__ */ new WeakMap();
377
380
  /**
378
- * Preserve the order in which dynamically-wrapped RPC methods begin executing.
381
+ * Preserve the order in which async wrapper invocations begin executing.
379
382
  *
380
- * Resolving a property like `stub.method` may need to import user modules or
381
- * instantiate wrapper objects. If several calls are fired synchronously, those
382
- * async steps can otherwise complete out of order before the actual user method
383
- * is invoked. The queue is released as soon as invocation starts, so async RPC
384
- * completions can still run concurrently.
383
+ * Resolving a property like `stub.method`, or ensuring a Durable Object handler
384
+ * instance, may need to import user modules or instantiate wrapper objects. If
385
+ * several calls are fired synchronously, those async steps can otherwise
386
+ * complete out of order before the actual user code is invoked. The queue is
387
+ * released as soon as invocation starts, so async completions can still run
388
+ * concurrently.
385
389
  */
386
- async function enqueueRPCInvocation(owner, callback) {
387
- const previous = rpcInvocationQueues.get(owner) ?? Promise.resolve();
390
+ async function enqueueInvocation(owner, callback) {
391
+ const previous = invocationQueues.get(owner) ?? Promise.resolve();
388
392
  let releaseStarted;
389
393
  const started = new Promise((resolve) => {
390
394
  releaseStarted = resolve;
@@ -394,7 +398,7 @@ async function enqueueRPCInvocation(owner, callback) {
394
398
  if (releaseStartedFn !== void 0) releaseStartedFn();
395
399
  };
396
400
  const result = previous.catch(() => {}).then(() => callback(release));
397
- rpcInvocationQueues.set(owner, started);
401
+ invocationQueues.set(owner, started);
398
402
  return result;
399
403
  }
400
404
  function getEntrypointState(instance) {
@@ -429,7 +433,7 @@ async function getWorkerEntrypointExport(env$2, entrypoint) {
429
433
  const mainModule = await importModule(mainPath);
430
434
  const entrypointValue = typeof mainModule === "object" && mainModule !== null && entrypoint in mainModule && mainModule[entrypoint];
431
435
  if (!entrypointValue) {
432
- const message = `${mainPath} does not export a ${entrypoint} entrypoint. \`@cloudflare/vitest-pool-workers\` does not support service workers or named entrypoints for \`SELF\`.\nIf you're using service workers, please migrate to the modules format: https://developers.cloudflare.com/workers/reference/migrate-to-module-workers.`;
436
+ const message = `${mainPath} does not export a ${entrypoint} entrypoint. \`@cloudflare/vitest-pool-workers\` does not support service workers or named entrypoints for \`SELF\`.\nIf you're using service workers, please migrate to the modules format: https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/`;
433
437
  throw new TypeError(message);
434
438
  }
435
439
  return {
@@ -502,7 +506,9 @@ async function getDurableObjectRPCProperty(wrapper, className, key) {
502
506
  const message = `Expected ${className} exported by ${mainPath} be a subclass of \`DurableObject\` for RPC`;
503
507
  throw new TypeError(message);
504
508
  }
505
- const value = getRPCProperty(instanceCtor, instance, key);
509
+ assertRPCPropertyAccessible(instanceCtor, instance, key);
510
+ if (Object.hasOwn(instance, key)) throw new TypeError(`The RPC receiver does not implement the method ${JSON.stringify(key)}.`);
511
+ const value = Reflect.get(instance, key, instance);
506
512
  if (typeof value === "function") return value.bind(instance);
507
513
  else return value;
508
514
  }
@@ -547,13 +553,19 @@ function createDurableObjectWrapper(className) {
547
553
  for (const key of DURABLE_OBJECT_KEYS) {
548
554
  if (key === "fetch") continue;
549
555
  Wrapper.prototype[key] = async function(...args) {
550
- const { mainPath, instance } = await this[kEnsureInstance]();
551
- const maybeFn = instance[key];
552
- if (typeof maybeFn === "function") return maybeFn.apply(instance, args);
553
- else {
554
- const message = `${className} exported by ${mainPath} does not define a \`${key}()\` method`;
555
- throw new TypeError(message);
556
- }
556
+ return enqueueInvocation(this, async (release) => {
557
+ try {
558
+ const { mainPath, instance } = await this[kEnsureInstance]();
559
+ const maybeFn = instance[key];
560
+ if (typeof maybeFn === "function") return maybeFn.apply(instance, args);
561
+ else {
562
+ const message = `${className} exported by ${mainPath} does not define a \`${key}()\` method`;
563
+ throw new TypeError(message);
564
+ }
565
+ } finally {
566
+ release();
567
+ }
568
+ });
557
569
  };
558
570
  }
559
571
  return Wrapper;