@typicalday/firegraph 0.11.1 → 0.11.2

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.
@@ -1,5 +1,6 @@
1
1
  import { W as WritableRecord, U as UpdatePayload, S as StorageBackend, T as TransactionBackend, B as BatchBackend } from '../backend-np4gEVhB.cjs';
2
2
  import { c as GraphRegistry, I as IndexSpec, i as QueryFilter, z as QueryOptions, C as CascadeResult, F as FindEdgesParams, m as BulkOptions, o as BulkResult, a as GraphClient, D as DynamicGraphClient, S as StoredGraphRecord, d as GraphReader, G as GraphClientOptions, e as DynamicRegistryConfig } from '../types-BGWxcpI_.cjs';
3
+ import { DurableObject } from 'cloudflare:workers';
3
4
  import '@google-cloud/firestore';
4
5
 
5
6
  /**
@@ -79,14 +80,26 @@ interface DORecordWire {
79
80
  * }
80
81
  * ```
81
82
  *
82
- * ## Why a plain class (not `extends DurableObject`)?
83
+ * ## Why `extends DurableObject`?
83
84
  *
84
- * Cloudflare accepts any class with the `(state, env)` constructor shape as
85
- * a DO class. Extending `DurableObject` from `cloudflare:workers` would pull
86
- * a runtime import into this module and prevent it from loading in Node
87
- * tests. The plain-class form keeps this file runtime-neutral the only
88
- * Cloudflare thing we touch is `ctx.storage.sql`, typed via local minimal
89
- * interfaces.
85
+ * Cloudflare's modern Durable Objects RPC dispatcher only accepts arbitrary
86
+ * method invocations on stubs whose backing class extends the special
87
+ * `DurableObject` base from `cloudflare:workers`. Plain classes with the
88
+ * `(state, env)` constructor shape still load and serve `fetch()`, but a
89
+ * stub method call (`stub._fgGetDoc(...)`) on a plain-class DO throws:
90
+ *
91
+ * The receiving Durable Object does not support RPC, because its class
92
+ * was not declared with `extends DurableObject`.
93
+ *
94
+ * `DORPCBackend` calls every operation as a stub method (see
95
+ * `src/cloudflare/backend.ts`), so extending `DurableObject` is mandatory
96
+ * for this library's design to work on production Workers.
97
+ *
98
+ * The `cloudflare:workers` import is virtual — only the workerd runtime
99
+ * resolves it. For Node tests we route the import through a vitest alias
100
+ * to a tiny stub class (`tests/__shims__/cloudflare-workers.ts`) that just
101
+ * captures `ctx`/`env`. Tests instantiating `FiregraphDO` directly still
102
+ * work; they just go through the stub instead of the real base class.
90
103
  */
91
104
 
92
105
  interface DOSqlCursor<T> {
@@ -146,11 +159,23 @@ interface FiregraphDOOptions {
146
159
  */
147
160
  coreIndexes?: IndexSpec[];
148
161
  }
149
- declare class FiregraphDO {
150
- /** @internal — exposed for subclass access, not part of the public RPC. */
151
- protected readonly ctx: DurableObjectStateLike;
152
- /** @internal exposed for subclass access; opaque to this class. */
153
- protected readonly env: unknown;
162
+ declare class FiregraphDO extends DurableObject<unknown> {
163
+ /**
164
+ * @internal — locally-narrowed alias for `this.ctx`, used only by
165
+ * FiregraphDO's own SQL helpers. Same runtime object as the inherited
166
+ * `this.ctx`, but typed as `DurableObjectStateLike` (just `storage.sql`
167
+ * / `transactionSync` / `blockConcurrencyWhile`) so internal calls
168
+ * don't trip over workers-types' stricter
169
+ * `SqlStorage.exec<T extends Record<string, SqlStorageValue>>`
170
+ * constraint vs the `Record<string, unknown>` rows firegraph passes.
171
+ *
172
+ * **Subclasses should use `this.ctx`, not `this.state`.** `this.state`
173
+ * deliberately exposes only the slice FiregraphDO needs internally;
174
+ * subclasses that want `id`, `acceptWebSocket`, `setAlarm`, `getAlarm`,
175
+ * `waitUntil`, `props`, etc. must reach for the inherited `this.ctx`
176
+ * (the full workers-types `DurableObjectState`).
177
+ */
178
+ protected readonly state: DurableObjectStateLike;
154
179
  /** @internal — table name used by every compiled statement. */
155
180
  protected readonly table: string;
156
181
  /** @internal — registry consulted by `runSchema` for per-entry indexes. */
@@ -1,5 +1,6 @@
1
1
  import { W as WritableRecord, U as UpdatePayload, S as StorageBackend, T as TransactionBackend, B as BatchBackend } from '../backend-U-MLShlg.js';
2
2
  import { c as GraphRegistry, I as IndexSpec, i as QueryFilter, z as QueryOptions, C as CascadeResult, F as FindEdgesParams, m as BulkOptions, o as BulkResult, a as GraphClient, D as DynamicGraphClient, S as StoredGraphRecord, d as GraphReader, G as GraphClientOptions, e as DynamicRegistryConfig } from '../types-BGWxcpI_.js';
3
+ import { DurableObject } from 'cloudflare:workers';
3
4
  import '@google-cloud/firestore';
4
5
 
5
6
  /**
@@ -79,14 +80,26 @@ interface DORecordWire {
79
80
  * }
80
81
  * ```
81
82
  *
82
- * ## Why a plain class (not `extends DurableObject`)?
83
+ * ## Why `extends DurableObject`?
83
84
  *
84
- * Cloudflare accepts any class with the `(state, env)` constructor shape as
85
- * a DO class. Extending `DurableObject` from `cloudflare:workers` would pull
86
- * a runtime import into this module and prevent it from loading in Node
87
- * tests. The plain-class form keeps this file runtime-neutral the only
88
- * Cloudflare thing we touch is `ctx.storage.sql`, typed via local minimal
89
- * interfaces.
85
+ * Cloudflare's modern Durable Objects RPC dispatcher only accepts arbitrary
86
+ * method invocations on stubs whose backing class extends the special
87
+ * `DurableObject` base from `cloudflare:workers`. Plain classes with the
88
+ * `(state, env)` constructor shape still load and serve `fetch()`, but a
89
+ * stub method call (`stub._fgGetDoc(...)`) on a plain-class DO throws:
90
+ *
91
+ * The receiving Durable Object does not support RPC, because its class
92
+ * was not declared with `extends DurableObject`.
93
+ *
94
+ * `DORPCBackend` calls every operation as a stub method (see
95
+ * `src/cloudflare/backend.ts`), so extending `DurableObject` is mandatory
96
+ * for this library's design to work on production Workers.
97
+ *
98
+ * The `cloudflare:workers` import is virtual — only the workerd runtime
99
+ * resolves it. For Node tests we route the import through a vitest alias
100
+ * to a tiny stub class (`tests/__shims__/cloudflare-workers.ts`) that just
101
+ * captures `ctx`/`env`. Tests instantiating `FiregraphDO` directly still
102
+ * work; they just go through the stub instead of the real base class.
90
103
  */
91
104
 
92
105
  interface DOSqlCursor<T> {
@@ -146,11 +159,23 @@ interface FiregraphDOOptions {
146
159
  */
147
160
  coreIndexes?: IndexSpec[];
148
161
  }
149
- declare class FiregraphDO {
150
- /** @internal — exposed for subclass access, not part of the public RPC. */
151
- protected readonly ctx: DurableObjectStateLike;
152
- /** @internal exposed for subclass access; opaque to this class. */
153
- protected readonly env: unknown;
162
+ declare class FiregraphDO extends DurableObject<unknown> {
163
+ /**
164
+ * @internal — locally-narrowed alias for `this.ctx`, used only by
165
+ * FiregraphDO's own SQL helpers. Same runtime object as the inherited
166
+ * `this.ctx`, but typed as `DurableObjectStateLike` (just `storage.sql`
167
+ * / `transactionSync` / `blockConcurrencyWhile`) so internal calls
168
+ * don't trip over workers-types' stricter
169
+ * `SqlStorage.exec<T extends Record<string, SqlStorageValue>>`
170
+ * constraint vs the `Record<string, unknown>` rows firegraph passes.
171
+ *
172
+ * **Subclasses should use `this.ctx`, not `this.state`.** `this.state`
173
+ * deliberately exposes only the slice FiregraphDO needs internally;
174
+ * subclasses that want `id`, `acceptWebSocket`, `setAlarm`, `getAlarm`,
175
+ * `waitUntil`, `props`, etc. must reach for the inherited `this.ctx`
176
+ * (the full workers-types `DurableObjectState`).
177
+ */
178
+ protected readonly state: DurableObjectStateLike;
154
179
  /** @internal — table name used by every compiled statement. */
155
180
  protected readonly table: string;
156
181
  /** @internal — registry consulted by `runSchema` for per-entry indexes. */
@@ -5,7 +5,7 @@ import {
5
5
  computeEdgeDocId,
6
6
  computeNodeDocId,
7
7
  createGraphClientFromBackend
8
- } from "../chunk-6SB34IPQ.js";
8
+ } from "../chunk-NJSOD64C.js";
9
9
  import {
10
10
  FiregraphError
11
11
  } from "../chunk-R7CRGYY4.js";
@@ -697,15 +697,28 @@ function createSiblingClient(client, siblingRootKey) {
697
697
  }
698
698
 
699
699
  // src/cloudflare/do.ts
700
+ import { DurableObject } from "cloudflare:workers";
700
701
  var DEFAULT_OPTIONS = {
701
702
  table: "firegraph",
702
703
  autoMigrate: true
703
704
  };
704
- var FiregraphDO = class {
705
- /** @internal — exposed for subclass access, not part of the public RPC. */
706
- ctx;
707
- /** @internal exposed for subclass access; opaque to this class. */
708
- env;
705
+ var FiregraphDO = class extends DurableObject {
706
+ /**
707
+ * @internal — locally-narrowed alias for `this.ctx`, used only by
708
+ * FiregraphDO's own SQL helpers. Same runtime object as the inherited
709
+ * `this.ctx`, but typed as `DurableObjectStateLike` (just `storage.sql`
710
+ * / `transactionSync` / `blockConcurrencyWhile`) so internal calls
711
+ * don't trip over workers-types' stricter
712
+ * `SqlStorage.exec<T extends Record<string, SqlStorageValue>>`
713
+ * constraint vs the `Record<string, unknown>` rows firegraph passes.
714
+ *
715
+ * **Subclasses should use `this.ctx`, not `this.state`.** `this.state`
716
+ * deliberately exposes only the slice FiregraphDO needs internally;
717
+ * subclasses that want `id`, `acceptWebSocket`, `setAlarm`, `getAlarm`,
718
+ * `waitUntil`, `props`, etc. must reach for the inherited `this.ctx`
719
+ * (the full workers-types `DurableObjectState`).
720
+ */
721
+ state;
709
722
  /** @internal — table name used by every compiled statement. */
710
723
  table;
711
724
  /** @internal — registry consulted by `runSchema` for per-entry indexes. */
@@ -713,8 +726,8 @@ var FiregraphDO = class {
713
726
  /** @internal — overrides `DEFAULT_CORE_INDEXES` when set. */
714
727
  coreIndexes;
715
728
  constructor(ctx, env, options = {}) {
716
- this.ctx = ctx;
717
- this.env = env;
729
+ super(ctx, env);
730
+ this.state = ctx;
718
731
  const table = options.table ?? DEFAULT_OPTIONS.table;
719
732
  validateDOTableName(table);
720
733
  this.table = table;
@@ -722,7 +735,7 @@ var FiregraphDO = class {
722
735
  this.coreIndexes = options.coreIndexes;
723
736
  const autoMigrate = options.autoMigrate ?? DEFAULT_OPTIONS.autoMigrate;
724
737
  if (autoMigrate) {
725
- void this.ctx.blockConcurrencyWhile(async () => {
738
+ void this.state.blockConcurrencyWhile(async () => {
726
739
  this.runSchema();
727
740
  });
728
741
  }
@@ -754,7 +767,7 @@ var FiregraphDO = class {
754
767
  async _fgUpdateDoc(docId, update) {
755
768
  const stmt = compileDOUpdate(this.table, docId, update, Date.now());
756
769
  const sqlWithReturning = `${stmt.sql} RETURNING "doc_id"`;
757
- const rows = this.ctx.storage.sql.exec(sqlWithReturning, ...stmt.params).toArray();
770
+ const rows = this.state.storage.sql.exec(sqlWithReturning, ...stmt.params).toArray();
758
771
  if (rows.length === 0) {
759
772
  throw new FiregraphError(`updateDoc: no document found for doc_id=${docId}`, "NOT_FOUND");
760
773
  }
@@ -785,9 +798,9 @@ var FiregraphDO = class {
785
798
  return compileDODelete(this.table, op.docId);
786
799
  }
787
800
  });
788
- this.ctx.storage.transactionSync(() => {
801
+ this.state.storage.transactionSync(() => {
789
802
  for (const stmt of statements) {
790
- this.ctx.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
803
+ this.state.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
791
804
  }
792
805
  });
793
806
  }
@@ -836,9 +849,9 @@ var FiregraphDO = class {
836
849
  };
837
850
  }
838
851
  try {
839
- this.ctx.storage.transactionSync(() => {
852
+ this.state.storage.transactionSync(() => {
840
853
  for (const stmt of statements) {
841
- this.ctx.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
854
+ this.state.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
842
855
  }
843
856
  });
844
857
  return {
@@ -878,9 +891,9 @@ var FiregraphDO = class {
878
891
  }
879
892
  const deleteStmts = docIds.map((id) => compileDODelete(this.table, id));
880
893
  try {
881
- this.ctx.storage.transactionSync(() => {
894
+ this.state.storage.transactionSync(() => {
882
895
  for (const stmt of deleteStmts) {
883
- this.ctx.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
896
+ this.state.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
884
897
  }
885
898
  });
886
899
  return { deleted: deleteStmts.length, batches: 1, errors: [] };
@@ -914,14 +927,14 @@ var FiregraphDO = class {
914
927
  registry: this.registry
915
928
  });
916
929
  for (const sql of statements) {
917
- this.ctx.storage.sql.exec(sql).toArray();
930
+ this.state.storage.sql.exec(sql).toArray();
918
931
  }
919
932
  }
920
933
  execAll(stmt) {
921
- return this.ctx.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
934
+ return this.state.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
922
935
  }
923
936
  execRun(stmt) {
924
- this.ctx.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
937
+ this.state.storage.sql.exec(stmt.sql, ...stmt.params).toArray();
925
938
  }
926
939
  };
927
940
  export {