js-bao 0.5.2 → 0.6.1
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.
- package/README.md +4 -0
- package/dist/browser.cjs +4069 -350
- package/dist/browser.d.cts +2269 -50
- package/dist/browser.d.ts +2269 -50
- package/dist/browser.js +4070 -351
- package/dist/client.cjs +343 -2
- package/dist/client.d.cts +739 -12
- package/dist/client.d.ts +739 -12
- package/dist/client.js +340 -1
- package/dist/cloudflare-do.cjs +847 -6
- package/dist/cloudflare-do.d.cts +1085 -13
- package/dist/cloudflare-do.d.ts +1085 -13
- package/dist/cloudflare-do.js +845 -5
- package/dist/cloudflare.cjs +359 -10
- package/dist/cloudflare.d.cts +751 -13
- package/dist/cloudflare.d.ts +751 -13
- package/dist/cloudflare.js +356 -9
- package/dist/codegen-v2.cjs +319 -3
- package/dist/codegen.cjs +1 -1
- package/dist/index.cjs +4325 -501
- package/dist/index.d.cts +2253 -18
- package/dist/index.d.ts +2253 -18
- package/dist/index.js +4238 -470
- package/dist/node.cjs +4167 -359
- package/dist/node.d.cts +2280 -58
- package/dist/node.d.ts +2280 -58
- package/dist/node.js +4117 -361
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -112,8 +112,315 @@ var init_uniqueIndex = __esm({
|
|
|
112
112
|
}
|
|
113
113
|
});
|
|
114
114
|
|
|
115
|
-
// src/
|
|
115
|
+
// src/utils/largeDocuments.ts
|
|
116
116
|
import * as Y5 from "yjs";
|
|
117
|
+
var init_largeDocuments = __esm({
|
|
118
|
+
"src/utils/largeDocuments.ts"() {
|
|
119
|
+
"use strict";
|
|
120
|
+
init_yjsValues();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// src/models/OrmContext.ts
|
|
125
|
+
var OrmContext, defaultOrmContext, liveOrmContexts;
|
|
126
|
+
var init_OrmContext = __esm({
|
|
127
|
+
"src/models/OrmContext.ts"() {
|
|
128
|
+
"use strict";
|
|
129
|
+
OrmContext = class _OrmContext {
|
|
130
|
+
/** Declared before the field that reads it: static initialization is ordered. */
|
|
131
|
+
static instanceCounter = 0;
|
|
132
|
+
/**
|
|
133
|
+
* Which instance this is, for diagnostics. A process holding several clients
|
|
134
|
+
* needs a way to say WHICH one a warning is about, so every context takes the
|
|
135
|
+
* next number as it is created and keeps it for its lifetime.
|
|
136
|
+
*/
|
|
137
|
+
instanceId = ++_OrmContext.instanceCounter;
|
|
138
|
+
/** The engine this instance's models read and write through. */
|
|
139
|
+
dbEngine = null;
|
|
140
|
+
connectedDocuments = /* @__PURE__ */ new Map();
|
|
141
|
+
/** `docId_modelName` → the Y.Map that document holds for that model. */
|
|
142
|
+
documentYMaps = /* @__PURE__ */ new Map();
|
|
143
|
+
/** `subscribe()` callbacks, by model name. */
|
|
144
|
+
listenersMap = /* @__PURE__ */ new Map();
|
|
145
|
+
// Default-document resolution
|
|
146
|
+
modelNameToDefaultDocId = /* @__PURE__ */ new Map();
|
|
147
|
+
globalDefaultDocId = void 0;
|
|
148
|
+
defaultDocChangedListeners = /* @__PURE__ */ new Set();
|
|
149
|
+
modelDocMappingChangedListeners = /* @__PURE__ */ new Set();
|
|
150
|
+
/** Format-2 ("large document", #2816) record stores by document id. */
|
|
151
|
+
format2Documents = /* @__PURE__ */ new Map();
|
|
152
|
+
/** Format-2 epoch moves in flight (#2816). */
|
|
153
|
+
format2Transitions = /* @__PURE__ */ new Map();
|
|
154
|
+
/**
|
|
155
|
+
* Nested-Y.Map stringset fields this context has already observed. Per
|
|
156
|
+
* context, not global: a Y.Doc reconnected to a fresh instance has to be
|
|
157
|
+
* observed again by that instance, and a global guard would silently
|
|
158
|
+
* suppress it.
|
|
159
|
+
*/
|
|
160
|
+
observedStringSetMaps = /* @__PURE__ */ new WeakSet();
|
|
161
|
+
/**
|
|
162
|
+
* The registry this instance's schema lookups use. `null` on the standing
|
|
163
|
+
* default context, where callers fall back to the global singleton.
|
|
164
|
+
*/
|
|
165
|
+
modelRegistry = null;
|
|
166
|
+
/** Model classes routed to this context. */
|
|
167
|
+
boundModels = /* @__PURE__ */ new Set();
|
|
168
|
+
/** True once {@link destroy} has run; bound classes keep pointing here. */
|
|
169
|
+
destroyed = false;
|
|
170
|
+
/** Yjs `unobserve` thunks by owning document id. */
|
|
171
|
+
observerDisposers = /* @__PURE__ */ new Map();
|
|
172
|
+
/** Teardown the owner (the document manager) registers for `destroy()`. */
|
|
173
|
+
teardownHooks = /* @__PURE__ */ new Set();
|
|
174
|
+
static NO_DOCUMENT_KEY = "__no_document__";
|
|
175
|
+
/**
|
|
176
|
+
* Record how to remove an observer this context installed. Every `observe()`
|
|
177
|
+
* in the ORM pairs with one of these: without it a torn-down instance's
|
|
178
|
+
* callbacks keep firing on a Y.Doc that outlives it.
|
|
179
|
+
*/
|
|
180
|
+
registerObserverDisposer(docId, dispose) {
|
|
181
|
+
const key = docId ?? _OrmContext.NO_DOCUMENT_KEY;
|
|
182
|
+
const list = this.observerDisposers.get(key);
|
|
183
|
+
if (list) list.push(dispose);
|
|
184
|
+
else this.observerDisposers.set(key, [dispose]);
|
|
185
|
+
}
|
|
186
|
+
/** Remove every observer this context installed for `docId`. */
|
|
187
|
+
disposeObserversForDocument(docId) {
|
|
188
|
+
const list = this.observerDisposers.get(docId);
|
|
189
|
+
if (!list) return;
|
|
190
|
+
this.observerDisposers.delete(docId);
|
|
191
|
+
for (const dispose of list) {
|
|
192
|
+
try {
|
|
193
|
+
dispose();
|
|
194
|
+
} catch {
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
disposeAllObservers() {
|
|
199
|
+
const buckets = Array.from(this.observerDisposers.values());
|
|
200
|
+
this.observerDisposers.clear();
|
|
201
|
+
for (const list of buckets) {
|
|
202
|
+
for (const dispose of list) {
|
|
203
|
+
try {
|
|
204
|
+
dispose();
|
|
205
|
+
} catch {
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** Run `hook` as part of `destroy()` (the format-2 binding registers here). */
|
|
211
|
+
onDestroy(hook) {
|
|
212
|
+
this.teardownHooks.add(hook);
|
|
213
|
+
}
|
|
214
|
+
/** The record store for `docId`, or `null` when the document is format 1. */
|
|
215
|
+
getFormat2Store(docId) {
|
|
216
|
+
if (!docId) return null;
|
|
217
|
+
return this.format2Documents.get(docId) ?? null;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* The epoch move a write to `docId` has to wait out (#2816, behavior 10), or
|
|
221
|
+
* `null` when none is in flight.
|
|
222
|
+
*/
|
|
223
|
+
format2WritesHeld(docId) {
|
|
224
|
+
if (!docId) return null;
|
|
225
|
+
return this.format2Transitions.get(docId) ?? null;
|
|
226
|
+
}
|
|
227
|
+
/** Run an epoch move with `docId`'s local writes held back (#2816). */
|
|
228
|
+
async holdFormat2Writes(docId, run) {
|
|
229
|
+
const inFlight = this.format2Transitions.get(docId);
|
|
230
|
+
if (inFlight) await inFlight.catch(() => {
|
|
231
|
+
});
|
|
232
|
+
let release;
|
|
233
|
+
const held = new Promise((resolve) => {
|
|
234
|
+
release = resolve;
|
|
235
|
+
});
|
|
236
|
+
this.format2Transitions.set(docId, held);
|
|
237
|
+
try {
|
|
238
|
+
return await run();
|
|
239
|
+
} finally {
|
|
240
|
+
if (this.format2Transitions.get(docId) === held) {
|
|
241
|
+
this.format2Transitions.delete(docId);
|
|
242
|
+
}
|
|
243
|
+
release();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/** Notify `subscribe()` callbacks registered on this context for `modelName`. */
|
|
247
|
+
notifyListeners(modelName, log, onError) {
|
|
248
|
+
const listeners = this.listenersMap.get(modelName);
|
|
249
|
+
if (!listeners) return;
|
|
250
|
+
log?.(`Notifying ${listeners.size} listeners for ${modelName}`);
|
|
251
|
+
listeners.forEach((callback) => {
|
|
252
|
+
try {
|
|
253
|
+
callback();
|
|
254
|
+
} catch (e) {
|
|
255
|
+
onError?.(e);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
// ---- Default-document resolution (the `BaseModel` statics delegate here) ----
|
|
260
|
+
setModelDefaultDocumentId(modelName, docId) {
|
|
261
|
+
const previous = this.modelNameToDefaultDocId.get(modelName);
|
|
262
|
+
this.modelNameToDefaultDocId.set(modelName, docId);
|
|
263
|
+
this.announceModelDocMapping(modelName, previous, docId);
|
|
264
|
+
}
|
|
265
|
+
removeModelDefaultDocumentId(modelName) {
|
|
266
|
+
const previous = this.modelNameToDefaultDocId.get(modelName);
|
|
267
|
+
this.modelNameToDefaultDocId.delete(modelName);
|
|
268
|
+
this.announceModelDocMapping(modelName, previous, void 0);
|
|
269
|
+
}
|
|
270
|
+
clearModelDefaultDocumentIds() {
|
|
271
|
+
const entries = Array.from(this.modelNameToDefaultDocId.entries());
|
|
272
|
+
this.modelNameToDefaultDocId.clear();
|
|
273
|
+
for (const [modelName, previous] of entries) {
|
|
274
|
+
this.announceModelDocMapping(modelName, previous, void 0);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
setGlobalDefaultDocumentId(docId) {
|
|
278
|
+
const previous = this.globalDefaultDocId;
|
|
279
|
+
this.globalDefaultDocId = docId;
|
|
280
|
+
this.announceDefaultDoc(previous, docId);
|
|
281
|
+
}
|
|
282
|
+
clearGlobalDefaultDocumentId() {
|
|
283
|
+
const previous = this.globalDefaultDocId;
|
|
284
|
+
this.globalDefaultDocId = void 0;
|
|
285
|
+
this.announceDefaultDoc(previous, void 0);
|
|
286
|
+
}
|
|
287
|
+
getModelDefaultDocumentMapping() {
|
|
288
|
+
return Object.fromEntries(this.modelNameToDefaultDocId.entries());
|
|
289
|
+
}
|
|
290
|
+
getDocumentIdForModel(modelName) {
|
|
291
|
+
return this.modelNameToDefaultDocId.get(modelName);
|
|
292
|
+
}
|
|
293
|
+
getGlobalDefaultDocumentId() {
|
|
294
|
+
return this.globalDefaultDocId;
|
|
295
|
+
}
|
|
296
|
+
onDefaultDocChanged(listener) {
|
|
297
|
+
this.defaultDocChangedListeners.add(listener);
|
|
298
|
+
return () => this.defaultDocChangedListeners.delete(listener);
|
|
299
|
+
}
|
|
300
|
+
onModelDocMappingChanged(listener) {
|
|
301
|
+
this.modelDocMappingChangedListeners.add(listener);
|
|
302
|
+
return () => this.modelDocMappingChangedListeners.delete(listener);
|
|
303
|
+
}
|
|
304
|
+
/** Drop the model→document defaults that point at a disconnected document. */
|
|
305
|
+
clearMappingsForDocId(docId) {
|
|
306
|
+
const toRemove = [];
|
|
307
|
+
for (const [modelName, mappedDocId] of this.modelNameToDefaultDocId) {
|
|
308
|
+
if (mappedDocId === docId) toRemove.push(modelName);
|
|
309
|
+
}
|
|
310
|
+
for (const modelName of toRemove) {
|
|
311
|
+
this.removeModelDefaultDocumentId(modelName);
|
|
312
|
+
}
|
|
313
|
+
if (this.globalDefaultDocId === docId) {
|
|
314
|
+
this.clearGlobalDefaultDocumentId();
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
announceDefaultDoc(previous, current) {
|
|
318
|
+
for (const listener of this.defaultDocChangedListeners) {
|
|
319
|
+
try {
|
|
320
|
+
listener({ previous, current });
|
|
321
|
+
} catch {
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
announceModelDocMapping(modelName, previous, current) {
|
|
326
|
+
for (const listener of this.modelDocMappingChangedListeners) {
|
|
327
|
+
try {
|
|
328
|
+
listener({ modelName, previous, current });
|
|
329
|
+
} catch {
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Tear this instance down: observers off, engine destroyed, state cleared.
|
|
335
|
+
*
|
|
336
|
+
* Deliberately does NOT unbind its model classes. They keep resolving this
|
|
337
|
+
* context, whose engine is now null, so an operation through them throws the
|
|
338
|
+
* ordinary not-initialized error instead of silently landing on whatever
|
|
339
|
+
* other instance happens to be live (#3039, behavior 7). Only a later
|
|
340
|
+
* `initJsBao` may rebind a class whose context is destroyed.
|
|
341
|
+
*/
|
|
342
|
+
async destroy() {
|
|
343
|
+
if (this.destroyed) return;
|
|
344
|
+
this.destroyed = true;
|
|
345
|
+
this.disposeAllObservers();
|
|
346
|
+
const hooks = Array.from(this.teardownHooks);
|
|
347
|
+
this.teardownHooks.clear();
|
|
348
|
+
for (const hook of hooks) {
|
|
349
|
+
try {
|
|
350
|
+
await hook();
|
|
351
|
+
} catch {
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const engine = this.dbEngine;
|
|
355
|
+
this.dbEngine = null;
|
|
356
|
+
if (engine) {
|
|
357
|
+
try {
|
|
358
|
+
await engine.destroy();
|
|
359
|
+
} catch {
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
this.connectedDocuments.clear();
|
|
363
|
+
this.documentYMaps.clear();
|
|
364
|
+
this.listenersMap.clear();
|
|
365
|
+
this.modelNameToDefaultDocId.clear();
|
|
366
|
+
this.globalDefaultDocId = void 0;
|
|
367
|
+
this.defaultDocChangedListeners.clear();
|
|
368
|
+
this.modelDocMappingChangedListeners.clear();
|
|
369
|
+
this.format2Documents.clear();
|
|
370
|
+
this.format2Transitions.clear();
|
|
371
|
+
this.observedStringSetMaps = /* @__PURE__ */ new WeakSet();
|
|
372
|
+
liveOrmContexts.delete(this);
|
|
373
|
+
if (defaultOrmContext === this) {
|
|
374
|
+
defaultOrmContext = new _OrmContext();
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
defaultOrmContext = new OrmContext();
|
|
379
|
+
liveOrmContexts = /* @__PURE__ */ new Set();
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
// src/models/format2QueryProjection.ts
|
|
384
|
+
var FORMAT2_QUERY_PROJECTION_DDL;
|
|
385
|
+
var init_format2QueryProjection = __esm({
|
|
386
|
+
"src/models/format2QueryProjection.ts"() {
|
|
387
|
+
"use strict";
|
|
388
|
+
FORMAT2_QUERY_PROJECTION_DDL = `
|
|
389
|
+
CREATE TABLE IF NOT EXISTS _query_projection (
|
|
390
|
+
doc_id TEXT NOT NULL,
|
|
391
|
+
model TEXT NOT NULL,
|
|
392
|
+
PRIMARY KEY (doc_id, model)
|
|
393
|
+
)`.trim();
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
// src/models/format2Observer.ts
|
|
398
|
+
import * as Y6 from "yjs";
|
|
399
|
+
var init_format2Observer = __esm({
|
|
400
|
+
"src/models/format2Observer.ts"() {
|
|
401
|
+
"use strict";
|
|
402
|
+
init_largeDocuments();
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
// src/models/format2ClientWrite.ts
|
|
407
|
+
var init_format2ClientWrite = __esm({
|
|
408
|
+
"src/models/format2ClientWrite.ts"() {
|
|
409
|
+
"use strict";
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// src/models/format2OfflineWindow.ts
|
|
414
|
+
var DAY_MS;
|
|
415
|
+
var init_format2OfflineWindow = __esm({
|
|
416
|
+
"src/models/format2OfflineWindow.ts"() {
|
|
417
|
+
"use strict";
|
|
418
|
+
DAY_MS = 24 * 60 * 60 * 1e3;
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
// src/models/BaseModel.ts
|
|
423
|
+
import * as Y7 from "yjs";
|
|
117
424
|
import { ulid as ulid2 } from "ulid";
|
|
118
425
|
function generateULID() {
|
|
119
426
|
return ulid2();
|
|
@@ -131,6 +438,12 @@ var init_BaseModel = __esm({
|
|
|
131
438
|
init_writeLifecycle();
|
|
132
439
|
init_yjsValues();
|
|
133
440
|
init_uniqueIndex();
|
|
441
|
+
init_largeDocuments();
|
|
442
|
+
init_OrmContext();
|
|
443
|
+
init_format2QueryProjection();
|
|
444
|
+
init_format2Observer();
|
|
445
|
+
init_format2ClientWrite();
|
|
446
|
+
init_format2OfflineWindow();
|
|
134
447
|
Logger = class {
|
|
135
448
|
static _logLevel = 1 /* ERROR */;
|
|
136
449
|
static _logCallback = null;
|
|
@@ -238,6 +551,32 @@ var DatabaseEngine = class {
|
|
|
238
551
|
getTableName(_modelName) {
|
|
239
552
|
throw new Error("Method not implemented.");
|
|
240
553
|
}
|
|
554
|
+
/**
|
|
555
|
+
* A SYNCHRONOUS SQL executor over this engine's database, used by the
|
|
556
|
+
* large-document (format 2) record store. That store commits a merged
|
|
557
|
+
* record row and its pending-operation entry as one transaction — the
|
|
558
|
+
* barrier a `save()` resolves behind — which the async `query()` API cannot
|
|
559
|
+
* express. Engines whose underlying SQLite is synchronous (better-sqlite3,
|
|
560
|
+
* SQL.js) provide it; anything else leaves it undefined and cannot host a
|
|
561
|
+
* large document.
|
|
562
|
+
*/
|
|
563
|
+
createFormat2Sql;
|
|
564
|
+
/**
|
|
565
|
+
* Whether what {@link createFormat2Sql} writes survives this process.
|
|
566
|
+
*
|
|
567
|
+
* On a large document the merged record view IS the document: the Y.Doc
|
|
568
|
+
* holds one epoch's overlay, and everything else — including a record a
|
|
569
|
+
* `save()` has already resolved, and the pending write that still owes it to
|
|
570
|
+
* the server — lives only in this store. An engine whose database is in
|
|
571
|
+
* memory therefore cannot host one: a reload does not cost a cache, it costs
|
|
572
|
+
* the document and the writes nobody has acknowledged yet.
|
|
573
|
+
*
|
|
574
|
+
* The platform is asked separately about its storage quota; this answers the
|
|
575
|
+
* half only the engine knows. An engine that does not implement it is taken
|
|
576
|
+
* at the platform's word, which is what keeps hosts with storage of their
|
|
577
|
+
* own — a worker handed a file store, a test — working unchanged.
|
|
578
|
+
*/
|
|
579
|
+
format2StorageIsDurable;
|
|
241
580
|
// Transaction support
|
|
242
581
|
async withTransaction(_callback) {
|
|
243
582
|
throw new Error("Method not implemented.");
|