@hot-updater/plugin-core 1.2.0-rc.4 → 1.2.0-rc.41
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/dist/coreApi.d.cts +204 -9
- package/dist/coreApi.d.mts +204 -9
- package/dist/engine/core/schema.cjs +171 -3
- package/dist/engine/core/schema.d.cts +115 -1
- package/dist/engine/core/schema.d.mts +115 -1
- package/dist/engine/core/schema.mjs +171 -3
- package/dist/engine/createEngine.cjs +1 -2
- package/dist/engine/createEngine.d.cts +1 -6
- package/dist/engine/createEngine.d.mts +1 -6
- package/dist/engine/createEngine.mjs +1 -2
- package/dist/engine/database/aggregateBatching.cjs +24 -116
- package/dist/engine/database/aggregateBatching.mjs +24 -116
- package/dist/engine/database/database.cjs +1 -4
- package/dist/engine/database/database.mjs +1 -4
- package/dist/engine/database/resolveSchema.cjs +6 -3
- package/dist/engine/database/resolveSchema.mjs +6 -3
- package/dist/index.cjs +1 -7
- package/dist/index.d.cts +5 -6
- package/dist/index.d.mts +5 -6
- package/dist/index.mjs +2 -3
- package/dist/serverPlugin/definitions.d.cts +4 -0
- package/dist/serverPlugin/definitions.d.mts +4 -0
- package/dist/serverPlugin/schema.d.cts +4 -0
- package/dist/serverPlugin/schema.d.mts +4 -0
- package/dist/types/databaseConfig.d.cts +8 -19
- package/dist/types/databaseConfig.d.mts +8 -19
- package/dist/types/databaseRows.d.cts +70 -2
- package/dist/types/databaseRows.d.mts +70 -2
- package/dist/types/index.cjs +3 -0
- package/dist/types/index.d.cts +118 -60
- package/dist/types/index.d.mts +118 -60
- package/dist/types/index.mjs +3 -1
- package/dist/types/models.d.cts +9 -1
- package/dist/types/models.d.mts +9 -1
- package/dist/types/public.d.cts +3 -3
- package/dist/types/public.d.mts +3 -3
- package/package.json +3 -9
- package/dist/remoteBundleSigning.cjs +0 -357
- package/dist/remoteBundleSigning.d.cts +0 -38
- package/dist/remoteBundleSigning.d.mts +0 -38
- package/dist/remoteBundleSigning.mjs +0 -352
|
@@ -16,7 +16,10 @@ const CHANNEL_ID = {
|
|
|
16
16
|
* every database's migrator creates missing tables and then records the
|
|
17
17
|
* version, and none of them changes an existing table, so changing one
|
|
18
18
|
* needs a migration step of its own first. The schema fence answers 503 on
|
|
19
|
-
* a database whose row differs, until it runs.
|
|
19
|
+
* a database whose row differs, until it runs. Before a version is released,
|
|
20
|
+
* its tables change in place in its own migration, and a database one of its
|
|
21
|
+
* prereleases migrated takes the same change by hand: the fence compares this
|
|
22
|
+
* row, not columns, so nothing reports it.
|
|
20
23
|
*/
|
|
21
24
|
const HOT_UPDATER_SCHEMA_VERSION = "1.2.0";
|
|
22
25
|
const bundles = defineTable({
|
|
@@ -270,7 +273,10 @@ const channels = defineTable({
|
|
|
270
273
|
/**
|
|
271
274
|
* The native builds `native-build android` and `native-build ios` track, newest first
|
|
272
275
|
* in each scope: a platform, a channel, and the fingerprint or app version
|
|
273
|
-
* the build compiled in.
|
|
276
|
+
* the build compiled in. A sandbox build's scope has no channel: its app ID,
|
|
277
|
+
* URL scheme, artifact kind and distribution in its place, so pull requests
|
|
278
|
+
* reuse it across channels, while `channel_id` still names the channel it
|
|
279
|
+
* embeds. `min_bundle_id` names the build's built-in bundle,
|
|
274
280
|
* a `bundles` row no release publishes. Its references keep no counters, so
|
|
275
281
|
* adding this table changes no existing one; a channel with builds is
|
|
276
282
|
* refused deletion through `byChannel`. `build_key` is the native build key
|
|
@@ -280,6 +286,27 @@ const channels = defineTable({
|
|
|
280
286
|
* or `versionCode`. `submitted_to`, `submitted_at_ms` and
|
|
281
287
|
* `submitted_version` record the store track a build went to, when, and
|
|
282
288
|
* the store's label of the build; they stay null for a build no store took.
|
|
289
|
+
* `is_store_build` is true on the build a person or a job says the store
|
|
290
|
+
* sells, and false on every other: deploys patch from it over the scope's
|
|
291
|
+
* newest build. The engine copies the scope key into `store_build_scope_key`
|
|
292
|
+
* for that build alone, so `byStoreBuild` holds one entry per scope at most,
|
|
293
|
+
* on every store, while any number of a scope's builds hold none.
|
|
294
|
+
* `is_sandbox` is true on a sandbox build, set from its artifact when it
|
|
295
|
+
* registers and never changed, and `sandbox_url_scheme` is the URL scheme its artifact registers,
|
|
296
|
+
* which its preview links open; null on any other build, and on a sandbox
|
|
297
|
+
* build registered before the column, which registered the derived one.
|
|
298
|
+
* `export_method` is how an `.ipa` was exported, read from its profile when
|
|
299
|
+
* it registers and never changed; null on any other artifact, on an `.ipa`
|
|
300
|
+
* whose profile couldn't be read, and on one registered before the column.
|
|
301
|
+
* `native_app_version` is the app version the artifact itself reports, its
|
|
302
|
+
* `CFBundleShortVersionString` or `versionName`, read from the artifact when
|
|
303
|
+
* it registers and never changed, whatever the update strategy; `app_version`
|
|
304
|
+
* stays the version OTAs target, null on a fingerprint build. It is null
|
|
305
|
+
* when the artifact's can't be read, and on a build registered before the
|
|
306
|
+
* column.
|
|
307
|
+
* Boolean columns follow one rule: `is_*` for a fact about the row or
|
|
308
|
+
* `should_*` for an instruction to the client, never null; a unique index over
|
|
309
|
+
* the rows where one is true keys a derived slot, not the boolean.
|
|
283
310
|
*/
|
|
284
311
|
const nativeBuilds = defineTable({
|
|
285
312
|
id: {
|
|
@@ -356,9 +383,34 @@ const nativeBuilds = defineTable({
|
|
|
356
383
|
type: "string",
|
|
357
384
|
maxLength: 255,
|
|
358
385
|
required: false
|
|
386
|
+
},
|
|
387
|
+
is_store_build: { type: "boolean" },
|
|
388
|
+
is_sandbox: { type: "boolean" },
|
|
389
|
+
sandbox_url_scheme: {
|
|
390
|
+
type: "string",
|
|
391
|
+
maxLength: 255,
|
|
392
|
+
ascii: true,
|
|
393
|
+
required: false
|
|
394
|
+
},
|
|
395
|
+
export_method: {
|
|
396
|
+
type: "string",
|
|
397
|
+
maxLength: 16,
|
|
398
|
+
required: false
|
|
399
|
+
},
|
|
400
|
+
native_app_version: {
|
|
401
|
+
type: "string",
|
|
402
|
+
required: false
|
|
359
403
|
}
|
|
360
404
|
}, {
|
|
361
405
|
key: ["id"],
|
|
406
|
+
derived: {
|
|
407
|
+
/** The scope this build is the store build of; null on every other. */
|
|
408
|
+
store_build_scope_key: {
|
|
409
|
+
type: "string",
|
|
410
|
+
maxLength: 2048,
|
|
411
|
+
ascii: true,
|
|
412
|
+
compute: (row) => row.is_store_build ? row.scope_key : null
|
|
413
|
+
} },
|
|
362
414
|
indexes: {
|
|
363
415
|
byScope: {
|
|
364
416
|
eq: ["scope_key"],
|
|
@@ -368,6 +420,11 @@ const nativeBuilds = defineTable({
|
|
|
368
420
|
eq: ["scope_key", "build_key"],
|
|
369
421
|
sort: ["id"]
|
|
370
422
|
},
|
|
423
|
+
byStoreBuild: {
|
|
424
|
+
eq: ["store_build_scope_key"],
|
|
425
|
+
sort: [],
|
|
426
|
+
unique: true
|
|
427
|
+
},
|
|
371
428
|
byChannel: {
|
|
372
429
|
eq: ["channel_id"],
|
|
373
430
|
sort: ["id"],
|
|
@@ -379,6 +436,115 @@ const nativeBuilds = defineTable({
|
|
|
379
436
|
}
|
|
380
437
|
}
|
|
381
438
|
});
|
|
439
|
+
/**
|
|
440
|
+
* The iPhones a team asks to install ad hoc builds on. A row decides what the
|
|
441
|
+
* CLI registers with Apple, not what an export includes: Xcode's automatic ad
|
|
442
|
+
* hoc profile lists every device the Apple account has enabled. `status` is
|
|
443
|
+
* `pending` until someone approves it, `approved` until a build or
|
|
444
|
+
* `native-build devices add` registers it with Apple, then `registered`;
|
|
445
|
+
* declining or removing one deletes its row. `udid` is the UDID in lower case.
|
|
446
|
+
* `apple_issuer_id` is the App Store Connect issuer whose key registered it,
|
|
447
|
+
* set once: a build signed by another account never registers these rows.
|
|
448
|
+
* `apple_error` is Apple's refusal of the device, or why it stopped counting
|
|
449
|
+
* it, cleared by the next success. `build_id` is the newest native build whose
|
|
450
|
+
* .ipa the CLI checked lists it; deleting that build never touches the row.
|
|
451
|
+
* It holds personal data: a UDID, a name, and the emails of who asked and who
|
|
452
|
+
* approved, null for the CLI.
|
|
453
|
+
*/
|
|
454
|
+
const iosDevices = defineTable({
|
|
455
|
+
id: {
|
|
456
|
+
type: "string",
|
|
457
|
+
maxLength: 36
|
|
458
|
+
},
|
|
459
|
+
udid: {
|
|
460
|
+
type: "string",
|
|
461
|
+
maxLength: 40,
|
|
462
|
+
ascii: true,
|
|
463
|
+
unique: true
|
|
464
|
+
},
|
|
465
|
+
name: {
|
|
466
|
+
type: "string",
|
|
467
|
+
maxLength: 100
|
|
468
|
+
},
|
|
469
|
+
status: {
|
|
470
|
+
type: "string",
|
|
471
|
+
maxLength: 16
|
|
472
|
+
},
|
|
473
|
+
requested_by: {
|
|
474
|
+
type: "string",
|
|
475
|
+
maxLength: 320,
|
|
476
|
+
required: false
|
|
477
|
+
},
|
|
478
|
+
created_at_ms: { type: "integer" },
|
|
479
|
+
approved_by: {
|
|
480
|
+
type: "string",
|
|
481
|
+
maxLength: 320,
|
|
482
|
+
required: false
|
|
483
|
+
},
|
|
484
|
+
approved_at_ms: {
|
|
485
|
+
type: "integer",
|
|
486
|
+
required: false
|
|
487
|
+
},
|
|
488
|
+
registered_at_ms: {
|
|
489
|
+
type: "integer",
|
|
490
|
+
required: false
|
|
491
|
+
},
|
|
492
|
+
apple_issuer_id: {
|
|
493
|
+
type: "string",
|
|
494
|
+
maxLength: 64,
|
|
495
|
+
ascii: true,
|
|
496
|
+
required: false
|
|
497
|
+
},
|
|
498
|
+
apple_error: {
|
|
499
|
+
type: "string",
|
|
500
|
+
maxLength: 512,
|
|
501
|
+
required: false
|
|
502
|
+
},
|
|
503
|
+
build_id: {
|
|
504
|
+
type: "string",
|
|
505
|
+
maxLength: 36,
|
|
506
|
+
required: false
|
|
507
|
+
}
|
|
508
|
+
}, {
|
|
509
|
+
key: ["id"],
|
|
510
|
+
indexes: {
|
|
511
|
+
byStatus: {
|
|
512
|
+
eq: ["status"],
|
|
513
|
+
sort: ["id"]
|
|
514
|
+
},
|
|
515
|
+
all: {
|
|
516
|
+
eq: [],
|
|
517
|
+
sort: ["id"]
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
/**
|
|
522
|
+
* The Apple account the iOS device registry belongs to: one row, keyed
|
|
523
|
+
* `registry`, holding the App Store Connect issuer that first registered a
|
|
524
|
+
* device. A build claims it before it sends Apple anything, and its key makes
|
|
525
|
+
* the claim atomic on every store, so two builds of two accounts can't both
|
|
526
|
+
* spend a slot; another account's build then registers nothing. Removing
|
|
527
|
+
* devices never releases it.
|
|
528
|
+
*/
|
|
529
|
+
const iosDeviceAccount = defineTable({
|
|
530
|
+
id: {
|
|
531
|
+
type: "string",
|
|
532
|
+
maxLength: 16,
|
|
533
|
+
ascii: true
|
|
534
|
+
},
|
|
535
|
+
apple_issuer_id: {
|
|
536
|
+
type: "string",
|
|
537
|
+
maxLength: 64,
|
|
538
|
+
ascii: true
|
|
539
|
+
},
|
|
540
|
+
claimed_at_ms: { type: "integer" }
|
|
541
|
+
}, {
|
|
542
|
+
key: ["id"],
|
|
543
|
+
indexes: { all: {
|
|
544
|
+
eq: [],
|
|
545
|
+
sort: ["id"]
|
|
546
|
+
} }
|
|
547
|
+
});
|
|
382
548
|
const coreSchema = {
|
|
383
549
|
bundles,
|
|
384
550
|
bundle_patches: bundlePatches,
|
|
@@ -396,7 +562,9 @@ const coreSchema = {
|
|
|
396
562
|
sort: []
|
|
397
563
|
} }
|
|
398
564
|
}),
|
|
399
|
-
native_builds: nativeBuilds
|
|
565
|
+
native_builds: nativeBuilds,
|
|
566
|
+
ios_devices: iosDevices,
|
|
567
|
+
ios_device_account: iosDeviceAccount
|
|
400
568
|
};
|
|
401
569
|
/** Core's module: always installed, never a plugin. */
|
|
402
570
|
const coreModule = {
|
|
@@ -64,8 +64,7 @@ const createEngine = (database, { plugins = [], now = Date.now } = {}) => {
|
|
|
64
64
|
if (module === void 0) throw new require_configError.HotUpdaterConfigError(`Plugin "${plugin.id}" is not one of this engine's plugins.`);
|
|
65
65
|
return engine.database(module);
|
|
66
66
|
},
|
|
67
|
-
flush: () => engine.flush()
|
|
68
|
-
dispose: () => engine.dispose()
|
|
67
|
+
flush: () => engine.flush()
|
|
69
68
|
};
|
|
70
69
|
};
|
|
71
70
|
//#endregion
|
|
@@ -23,13 +23,8 @@ interface Engine {
|
|
|
23
23
|
readonly id: string;
|
|
24
24
|
readonly schema: TSchema;
|
|
25
25
|
}): HotUpdaterDatabase<TSchema>;
|
|
26
|
-
/**
|
|
27
|
-
* Applies aggregate changes the database batches that are still pending:
|
|
28
|
-
* this process's buffer, and the log rows no compaction has merged yet.
|
|
29
|
-
*/
|
|
26
|
+
/** Applies the log rows of batched aggregates that no compaction has merged yet. */
|
|
30
27
|
flush(): Promise<void>;
|
|
31
|
-
/** Stops batching's timer after a last flush. */
|
|
32
|
-
dispose(): Promise<void>;
|
|
33
28
|
}
|
|
34
29
|
/**
|
|
35
30
|
* The storage engine over a database, as a server runs it: core's tables
|
|
@@ -23,13 +23,8 @@ interface Engine {
|
|
|
23
23
|
readonly id: string;
|
|
24
24
|
readonly schema: TSchema;
|
|
25
25
|
}): HotUpdaterDatabase<TSchema>;
|
|
26
|
-
/**
|
|
27
|
-
* Applies aggregate changes the database batches that are still pending:
|
|
28
|
-
* this process's buffer, and the log rows no compaction has merged yet.
|
|
29
|
-
*/
|
|
26
|
+
/** Applies the log rows of batched aggregates that no compaction has merged yet. */
|
|
30
27
|
flush(): Promise<void>;
|
|
31
|
-
/** Stops batching's timer after a last flush. */
|
|
32
|
-
dispose(): Promise<void>;
|
|
33
28
|
}
|
|
34
29
|
/**
|
|
35
30
|
* The storage engine over a database, as a server runs it: core's tables
|
|
@@ -64,8 +64,7 @@ const createEngine = (database, { plugins = [], now = Date.now } = {}) => {
|
|
|
64
64
|
if (module === void 0) throw new HotUpdaterConfigError(`Plugin "${plugin.id}" is not one of this engine's plugins.`);
|
|
65
65
|
return engine.database(module);
|
|
66
66
|
},
|
|
67
|
-
flush: () => engine.flush()
|
|
68
|
-
dispose: () => engine.dispose()
|
|
67
|
+
flush: () => engine.flush()
|
|
69
68
|
};
|
|
70
69
|
};
|
|
71
70
|
//#endregion
|
|
@@ -19,15 +19,8 @@ const LEASE_TABLE = "aggregate_lease";
|
|
|
19
19
|
const LEASE = { id: "compaction" };
|
|
20
20
|
/** How long a compaction holds the lease before another process may take it. */
|
|
21
21
|
const LEASE_MS = 3e4;
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
* first; memory mode flushes every 15, since its window is what a crash
|
|
25
|
-
* loses.
|
|
26
|
-
*/
|
|
27
|
-
const WINDOW_MS = {
|
|
28
|
-
log: 6e4,
|
|
29
|
-
memory: 15e3
|
|
30
|
-
};
|
|
22
|
+
/** A commit compacts once this has passed since the last compaction. */
|
|
23
|
+
const WINDOW_MS = 6e4;
|
|
31
24
|
/** Log rows a compaction reads from each log table for one group. */
|
|
32
25
|
const LOG_PAGE = 200;
|
|
33
26
|
/**
|
|
@@ -41,8 +34,6 @@ const GROUPS = {
|
|
|
41
34
|
};
|
|
42
35
|
/** Tries per write after a conflict or a transient error. */
|
|
43
36
|
const ATTEMPTS = 5;
|
|
44
|
-
/** Aggregate rows a memory buffer holds before a commit waits for a flush. */
|
|
45
|
-
const MAX_BUFFERED = 1e4;
|
|
46
37
|
const log = require_schema.defineTable({
|
|
47
38
|
id: {
|
|
48
39
|
type: "string",
|
|
@@ -92,7 +83,7 @@ const valuesOf = ({ deltas, sketches }) => ({
|
|
|
92
83
|
/**
|
|
93
84
|
* Merges `values` into the change for `key`'s identity as `tx.aggregate`
|
|
94
85
|
* does: deltas add up and sketches merge. One constant `shardBy` keys each
|
|
95
|
-
* identity to one entry;
|
|
86
|
+
* identity to one entry; `compile` writes it to shard 0.
|
|
96
87
|
*/
|
|
97
88
|
const merge = (into, model, key, values) => require_engineAggregates.recordAggregate(into, model, Object.fromEntries(shapeOf(model).key.map((field, at) => [field, key[at]])), values, "");
|
|
98
89
|
const mergeAll = (into, changes) => {
|
|
@@ -126,7 +117,7 @@ const increment = ({ model, deltas }, key) => Object.values(deltas).some((delta)
|
|
|
126
117
|
/**
|
|
127
118
|
* The guarded write that merges `change` into `current`: insert, patch, or
|
|
128
119
|
* delete at zero. Unlike a transaction's, a gauge may go below zero here: a
|
|
129
|
-
*
|
|
120
|
+
* group can hold a loss without the gain before it, and reads sum every shard.
|
|
130
121
|
*/
|
|
131
122
|
const rewrite = (change, key, current) => {
|
|
132
123
|
const { model } = change;
|
|
@@ -164,12 +155,12 @@ const rewrite = (change, key, current) => {
|
|
|
164
155
|
};
|
|
165
156
|
};
|
|
166
157
|
/**
|
|
167
|
-
* The write for each change on
|
|
168
|
-
* the row for counters, and a guarded rewrite
|
|
169
|
-
* rows are read in one batch per table.
|
|
158
|
+
* The write for each change on shard 0, since one compaction runs at a time:
|
|
159
|
+
* a blind increment that creates the row for counters, and a guarded rewrite
|
|
160
|
+
* for gauges and sketches, whose rows are read in one batch per table.
|
|
170
161
|
*/
|
|
171
|
-
const compile = async (adapter, changes
|
|
172
|
-
const keys = changes.map(({
|
|
162
|
+
const compile = async (adapter, changes) => {
|
|
163
|
+
const keys = changes.map(({ key }) => [...key.slice(0, -1), 0]);
|
|
173
164
|
const reads = /* @__PURE__ */ new Map();
|
|
174
165
|
changes.forEach((change, at) => {
|
|
175
166
|
if (rewrites(change)) reads.set(change.model, [...reads.get(change.model) ?? [], at]);
|
|
@@ -223,14 +214,13 @@ const encode = async (changes) => {
|
|
|
223
214
|
return zipped !== void 0 && zipped.length < json.length ? `z${zipped}` : json;
|
|
224
215
|
};
|
|
225
216
|
/**
|
|
226
|
-
* Batches changes to aggregates declared `batched`. A transaction
|
|
227
|
-
* them
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* applies exactly once. One lease row keeps compactions one at a time.
|
|
217
|
+
* Batches changes to aggregates declared `batched`. A transaction writes
|
|
218
|
+
* them as one log row in its own atomic write, in place of their rows; a
|
|
219
|
+
* compaction later merges log rows into the aggregates and deletes them,
|
|
220
|
+
* atomically again, so each applies exactly once. One lease row keeps
|
|
221
|
+
* compactions one at a time.
|
|
232
222
|
*/
|
|
233
|
-
const createAggregateBatches = ({ engine, adapter, schema, batching: {
|
|
223
|
+
const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs = WINDOW_MS }, now }) => {
|
|
234
224
|
if (!schema.models.has(LEASE_TABLE)) throw new require_errors.DatabaseTransactionError("An engine that batches aggregates needs aggregateBatchingModule in its schema.");
|
|
235
225
|
const holder = crypto.randomUUID().replaceAll("-", "");
|
|
236
226
|
const table = (name) => schema.models.get(name).table;
|
|
@@ -258,68 +248,6 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { mode = "l
|
|
|
258
248
|
}
|
|
259
249
|
return changes;
|
|
260
250
|
};
|
|
261
|
-
/**
|
|
262
|
-
* Writes changes in atomic chunks within `fits`, and returns the ones it
|
|
263
|
-
* could not apply. A write whose outcome is unknown is dropped, never
|
|
264
|
-
* resent, so no change applies twice.
|
|
265
|
-
*/
|
|
266
|
-
const apply = async (changes, shard) => {
|
|
267
|
-
let rest = changes;
|
|
268
|
-
let ops = [];
|
|
269
|
-
let read = true;
|
|
270
|
-
for (let failures = 0; rest.length > 0 && failures < ATTEMPTS;) try {
|
|
271
|
-
if (read) ops = await compile(adapter, rest, shard);
|
|
272
|
-
let size = rest.length;
|
|
273
|
-
const chunk = () => ops.slice(0, size).filter(isOp);
|
|
274
|
-
while (size > 1 && !adapter.fits(chunk())) size = Math.ceil(size / 2);
|
|
275
|
-
let result = { ok: true };
|
|
276
|
-
if (chunk().length > 0) result = await adapter.write(chunk()).catch((error) => {
|
|
277
|
-
warn("flush")(error);
|
|
278
|
-
return { ok: true };
|
|
279
|
-
});
|
|
280
|
-
read = !result.ok && !("retry" in result);
|
|
281
|
-
if (result.ok) {
|
|
282
|
-
rest = rest.slice(size);
|
|
283
|
-
ops = ops.slice(size);
|
|
284
|
-
failures = 0;
|
|
285
|
-
} else {
|
|
286
|
-
failures += 1;
|
|
287
|
-
if (!read) await sleep(failures);
|
|
288
|
-
}
|
|
289
|
-
} catch (error) {
|
|
290
|
-
warn("flush")(error);
|
|
291
|
-
return rest;
|
|
292
|
-
}
|
|
293
|
-
return rest;
|
|
294
|
-
};
|
|
295
|
-
let buffer = /* @__PURE__ */ new Map();
|
|
296
|
-
let timer;
|
|
297
|
-
let flushing;
|
|
298
|
-
const shards = /* @__PURE__ */ new Map();
|
|
299
|
-
/** Each process writes its own shard, so flushes of two processes rarely meet. */
|
|
300
|
-
const processShard = (model) => {
|
|
301
|
-
if (!shards.has(model)) shards.set(model, Math.floor(Math.random() * shapeOf(model).shards));
|
|
302
|
-
return shards.get(model);
|
|
303
|
-
};
|
|
304
|
-
const schedule = () => {
|
|
305
|
-
if (timer !== void 0 || buffer.size === 0) return;
|
|
306
|
-
timer = setTimeout(() => {
|
|
307
|
-
timer = void 0;
|
|
308
|
-
flushBuffer();
|
|
309
|
-
}, windowMs);
|
|
310
|
-
timer.unref?.();
|
|
311
|
-
};
|
|
312
|
-
/** Never rejects: what it cannot apply goes back into the buffer. */
|
|
313
|
-
const flushBuffer = () => flushing ??= (async () => {
|
|
314
|
-
clearTimeout(timer);
|
|
315
|
-
timer = void 0;
|
|
316
|
-
const pending = [...buffer.values()];
|
|
317
|
-
buffer = /* @__PURE__ */ new Map();
|
|
318
|
-
mergeAll(buffer, await apply(pending, processShard));
|
|
319
|
-
})().catch(warn("flush")).finally(() => {
|
|
320
|
-
flushing = void 0;
|
|
321
|
-
schedule();
|
|
322
|
-
});
|
|
323
251
|
let nextDue = 0;
|
|
324
252
|
let compacting;
|
|
325
253
|
/** Log rows this process read, by `[log table's index, id]`, for the deletes after a group. */
|
|
@@ -454,7 +382,7 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { mode = "l
|
|
|
454
382
|
more
|
|
455
383
|
};
|
|
456
384
|
};
|
|
457
|
-
const rowsFor = async (changes) => (await compile(adapter, changes
|
|
385
|
+
const rowsFor = async (changes) => (await compile(adapter, changes)).filter(isOp);
|
|
458
386
|
/**
|
|
459
387
|
* Applies the oldest pending log rows whose merged changes fit in one
|
|
460
388
|
* atomic write with the lease, which lists them as applied; the rows are
|
|
@@ -548,20 +476,6 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { mode = "l
|
|
|
548
476
|
}))).then((pages) => pages.some((page) => page.rows.length > 0)).finally(() => {
|
|
549
477
|
checking = void 0;
|
|
550
478
|
});
|
|
551
|
-
/** Takes a committed transaction's changes; never rejects, since the transaction already committed. */
|
|
552
|
-
const committed = async (changes) => {
|
|
553
|
-
if (mode === "log") {
|
|
554
|
-
if (now() >= nextDue) await compact(false, GROUPS.commit);
|
|
555
|
-
return;
|
|
556
|
-
}
|
|
557
|
-
mergeAll(buffer, changes.values());
|
|
558
|
-
schedule();
|
|
559
|
-
if (buffer.size >= MAX_BUFFERED) await flushBuffer();
|
|
560
|
-
};
|
|
561
|
-
const flush = async () => {
|
|
562
|
-
await flushBuffer();
|
|
563
|
-
if (mode === "log" && await hasPending()) await compact(true, GROUPS.flush);
|
|
564
|
-
};
|
|
565
479
|
return {
|
|
566
480
|
async transaction(fn) {
|
|
567
481
|
let changes = /* @__PURE__ */ new Map();
|
|
@@ -587,7 +501,7 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { mode = "l
|
|
|
587
501
|
aggregate
|
|
588
502
|
});
|
|
589
503
|
if (failure) throw failure.error;
|
|
590
|
-
if (
|
|
504
|
+
if (attempt.size > 0) {
|
|
591
505
|
const id = `${now().toString(36).padStart(9, "0")}${holder.slice(0, 8)}${crypto.randomUUID().slice(0, 8)}`;
|
|
592
506
|
const shard = Math.floor(Math.random() * LOG_TABLES.length);
|
|
593
507
|
const merged = mergeAll(/* @__PURE__ */ new Map(), attempt.values());
|
|
@@ -601,24 +515,18 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { mode = "l
|
|
|
601
515
|
open = false;
|
|
602
516
|
}
|
|
603
517
|
});
|
|
604
|
-
if (changes.size > 0) await
|
|
518
|
+
if (changes.size > 0 && now() >= nextDue) await compact(false, GROUPS.commit);
|
|
605
519
|
return result;
|
|
606
520
|
},
|
|
607
521
|
async beforeRead(name) {
|
|
608
522
|
if (!isBatched(schema.models.get(name))) return;
|
|
609
|
-
if (
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
return false;
|
|
614
|
-
})) await compact(true, GROUPS.read);
|
|
615
|
-
}
|
|
523
|
+
if (await hasPending().catch((error) => {
|
|
524
|
+
warn("read")(error);
|
|
525
|
+
return false;
|
|
526
|
+
})) await compact(true, GROUPS.read);
|
|
616
527
|
},
|
|
617
|
-
flush
|
|
618
|
-
|
|
619
|
-
clearTimeout(timer);
|
|
620
|
-
timer = void 0;
|
|
621
|
-
await flush();
|
|
528
|
+
async flush() {
|
|
529
|
+
if (await hasPending()) await compact(true, GROUPS.flush);
|
|
622
530
|
}
|
|
623
531
|
};
|
|
624
532
|
};
|