cloudflare / workerd

The JavaScript / Wasm runtime that powers Cloudflare Workers
https://blog.cloudflare.com/workerd-open-source-workers-runtime/
Apache License 2.0
6.31k stars 311 forks source link

🚀 Feature Request: support FinalizationRegistry or other Weak references #2906

Open Dexus opened 1 month ago

Dexus commented 1 month ago

Describe the solution

I am currently blocked because there are not many ways to fix my problem, I need FinalizationRegistry

  Uncaught ReferenceError: FinalizationRegistry is not defined
    at null.<anonymous>
  (file:///xxx/xxx/xxx/worker_api/node_modules/capnp-es/dist/index.mjs:991:32)
   [code: 10021]

ref: https://github.com/pi0/capnp-es - https://github.com/pi0/capnp-es/blob/main/src/rpc/conn.ts#L63

together with a current little patch:

diff --git a/node_modules/capnp-es/dist/shared/capnp-es.CM7jzxJY.d.mts b/node_modules/capnp-es/dist/shared/capnp-es.CM7jzxJY.d.mts
index f34cdca..0f60513 100644
--- a/node_modules/capnp-es/dist/shared/capnp-es.CM7jzxJY.d.mts
+++ b/node_modules/capnp-es/dist/shared/capnp-es.CM7jzxJY.d.mts
@@ -108,9 +108,9 @@ declare class List<T> extends Pointer implements Array<T> {
     toSpliced(start: number, deleteCount: number, ...items: T[]): T[];
     fill(value: T, start?: number, end?: number): this;
     copyWithin(target: number, start: number, end?: number): this;
-    keys(): IterableIterator<number>;
-    values(): IterableIterator<T>;
-    entries(): IterableIterator<[number, T]>;
+    keys(): ArrayIterator<number>;
+    values(): ArrayIterator<T>;
+    entries(): ArrayIterator<[number, T]>;
     flat<A, D extends number = 1>(this: A, depth?: D): FlatArray<A, D>[];
     with(index: number, value: T): T[];
     includes(_searchElement: T, _fromIndex?: number): boolean;
@@ -169,7 +169,7 @@ declare class List<T> extends Pointer implements Array<T> {
         [Symbol.iterator]?: boolean | undefined;
         readonly [Symbol.unscopables]?: boolean | undefined;
     };
-    [Symbol.iterator](): IterableIterator<T>;
+    [Symbol.iterator](): ArrayIterator<T>;
     toJSON(): unknown;
     toString(): string;
     toLocaleString(_locales?: unknown, _options?: unknown): string;

You using it self here: https://github.com/cloudflare/workers-sdk/blob/2e649686c259c639701a62e754c53448cb694dfc/packages/miniflare/src/plugins/core/proxy/client.ts#L141

and a related bug was reported here: https://github.com/cloudflare/workers-sdk/issues/2258

jasnell commented 1 month ago

It is unlikely that we will ever actually support FinalizationRegistry. I'm sure @kentonv can explain the reasoning best but the tl;dr is that we want to avoid making garbage collection timing observable. We don't currently support WeakRef but I've been thinking more and more than we probably could do so safely.