clivly 0.24.0 → 0.25.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 +34 -7
- package/{app-id-DcQ4u1c8.cjs → app-id-C2F25WhK.cjs} +1 -1
- package/{app-id-5cB6yioz.mjs → app-id-CMMadyIC.mjs} +1 -1
- package/auth-better-auth.cjs +51 -0
- package/auth-better-auth.d.cts +51 -1
- package/auth-better-auth.d.mts +51 -1
- package/auth-better-auth.mjs +51 -1
- package/auth-clerk.cjs +53 -0
- package/auth-clerk.d.cts +12 -1
- package/auth-clerk.d.mts +12 -1
- package/auth-clerk.mjs +53 -1
- package/auth-supabase.cjs +10 -0
- package/auth-supabase.d.cts +17 -1
- package/auth-supabase.d.mts +17 -1
- package/auth-supabase.mjs +10 -1
- package/auth-webhook-3if7gY6b.mjs +162 -0
- package/auth-webhook-BihEUc9C.cjs +197 -0
- package/auth-webhook-DKdaCZrW.d.cts +30 -0
- package/auth-webhook-DKdaCZrW.d.mts +30 -0
- package/auth-workos.cjs +43 -0
- package/auth-workos.d.cts +12 -1
- package/auth-workos.d.mts +12 -1
- package/auth-workos.mjs +43 -1
- package/drizzle.cjs +269 -17
- package/drizzle.d.cts +23 -3
- package/drizzle.d.mts +23 -3
- package/drizzle.mjs +270 -19
- package/{from-sql-CFgXkCe2.cjs → from-sql-Dw2ipynp.cjs} +7 -1
- package/{from-sql-S8dSvU-e.d.cts → from-sql-MyPE5elx.d.cts} +1 -1
- package/{from-sql-CpGP7K3J.d.mts → from-sql-YW6LfdVK.d.mts} +1 -1
- package/{from-sql-BD7gR4bC.mjs → from-sql-kqs9CbQ-.mjs} +7 -1
- package/{index-CIupXz_w.d.cts → index-B4kWWHYm.d.cts} +2 -2
- package/{index-BoemTWH4.d.mts → index-CKElWhvq.d.mts} +2 -2
- package/index.cjs +3354 -2829
- package/index.d.cts +3 -3
- package/index.d.mts +3 -3
- package/index.mjs +3354 -2829
- package/{introspector-5w2x89RD.mjs → introspector-B1S7qmvy.mjs} +4 -4
- package/{introspector-DT_Ma59k.cjs → introspector-DRmEXDai.cjs} +4 -4
- package/kysely.cjs +163 -2
- package/kysely.d.cts +16 -3
- package/kysely.d.mts +16 -3
- package/kysely.mjs +163 -3
- package/{namespace-probe-CUR2zG2l.d.mts → namespace-probe-LKC4OU5X.d.mts} +54 -19
- package/package.json +1 -1
- package/{projection-guard-DOt_nbGn.mjs → projection-guard-CADdAdzg.mjs} +1 -1
- package/{projection-guard-C70l5U24.cjs → projection-guard-yhVTvx37.cjs} +1 -1
- package/sdk.cjs +1 -1
- package/sdk.d.cts +1 -1
- package/sdk.d.mts +1 -1
- package/sdk.mjs +1 -1
- package/sql.cjs +5 -5
- package/sql.d.cts +2 -2
- package/sql.d.mts +2 -2
- package/sql.mjs +5 -5
- package/{src-CZIdwwI2.cjs → src-BQasv4EL.cjs} +368 -38
- package/{src-tFhC1Ru2.mjs → src-CAn-zrEl.mjs} +368 -38
- package/{supabase-contact-BXQ0nnLq.cjs → supabase-contact-0GmBVpiQ.cjs} +24 -15
- package/{supabase-contact-3ke1KKUs.mjs → supabase-contact-D-oDC4Qk.mjs} +24 -15
- package/supabase.cjs +39 -9
- package/supabase.d.cts +3 -2
- package/supabase.d.mts +3 -2
- package/supabase.mjs +39 -9
- package/{sync-Q0O-2kFI.mjs → sync-CnWT5juF.mjs} +65 -5
- package/{sync-CdA9dgZz.cjs → sync-R0iVlooB.cjs} +94 -4
- package/track-writes-4yyZbTZX.d.cts +17 -0
- package/track-writes-BJSWg8wg.d.mts +17 -0
- package/track-writes-BnZAig2Y.mjs +77 -0
- package/track-writes-C22_e_W-.cjs +112 -0
- package/{types-CzL7lUH9.d.cts → types-BPG4l-oM.d.cts} +53 -18
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
package/sql.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as connectPostgresJs, i as SqlExecutor, n as existsFromSql, o as pgExecutor, r as fromSql, s as postgresJsExecutor } from "./from-sql-
|
|
2
|
-
import { a as supabaseContactSource, c as sqlIntrospector, i as SupabaseContactSource, l as discoverFromSql, n as SUPABASE_CONTACT_NAME_COLUMN, o as filtersAnonymous, r as SupabaseContactOptions, s as fromSqlProjection, t as ContactTableMissing, u as sqlAppIdStore } from "./index-
|
|
1
|
+
import { a as connectPostgresJs, i as SqlExecutor, n as existsFromSql, o as pgExecutor, r as fromSql, s as postgresJsExecutor } from "./from-sql-MyPE5elx.cjs";
|
|
2
|
+
import { a as supabaseContactSource, c as sqlIntrospector, i as SupabaseContactSource, l as discoverFromSql, n as SUPABASE_CONTACT_NAME_COLUMN, o as filtersAnonymous, r as SupabaseContactOptions, s as fromSqlProjection, t as ContactTableMissing, u as sqlAppIdStore } from "./index-B4kWWHYm.cjs";
|
|
3
3
|
export { ContactTableMissing, SUPABASE_CONTACT_NAME_COLUMN, type SqlExecutor, type SupabaseContactOptions, type SupabaseContactSource, connectPostgresJs, discoverFromSql, existsFromSql, filtersAnonymous, fromSql, fromSqlProjection, pgExecutor, postgresJsExecutor, sqlAppIdStore, sqlIntrospector, supabaseContactSource };
|
package/sql.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as connectPostgresJs, i as SqlExecutor, n as existsFromSql, o as pgExecutor, r as fromSql, s as postgresJsExecutor } from "./from-sql-
|
|
2
|
-
import { a as supabaseContactSource, c as sqlIntrospector, i as SupabaseContactSource, l as discoverFromSql, n as SUPABASE_CONTACT_NAME_COLUMN, o as filtersAnonymous, r as SupabaseContactOptions, s as fromSqlProjection, t as ContactTableMissing, u as sqlAppIdStore } from "./index-
|
|
1
|
+
import { a as connectPostgresJs, i as SqlExecutor, n as existsFromSql, o as pgExecutor, r as fromSql, s as postgresJsExecutor } from "./from-sql-YW6LfdVK.mjs";
|
|
2
|
+
import { a as supabaseContactSource, c as sqlIntrospector, i as SupabaseContactSource, l as discoverFromSql, n as SUPABASE_CONTACT_NAME_COLUMN, o as filtersAnonymous, r as SupabaseContactOptions, s as fromSqlProjection, t as ContactTableMissing, u as sqlAppIdStore } from "./index-CKElWhvq.mjs";
|
|
3
3
|
export { ContactTableMissing, SUPABASE_CONTACT_NAME_COLUMN, type SqlExecutor, type SupabaseContactOptions, type SupabaseContactSource, connectPostgresJs, discoverFromSql, existsFromSql, filtersAnonymous, fromSql, fromSqlProjection, pgExecutor, postgresJsExecutor, sqlAppIdStore, sqlIntrospector, supabaseContactSource };
|
package/sql.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { n as fromSql, t as existsFromSql } from "./from-sql-
|
|
2
|
-
import { r as sqlAppIdStore } from "./app-id-
|
|
3
|
-
import { a as discoverFromSql, i as fromSqlProjection, n as SUPABASE_CONTACT_NAME_COLUMN, r as supabaseContactSource, t as ContactTableMissing } from "./supabase-contact-
|
|
4
|
-
import {
|
|
1
|
+
import { n as fromSql, t as existsFromSql } from "./from-sql-kqs9CbQ-.mjs";
|
|
2
|
+
import { r as sqlAppIdStore } from "./app-id-CMMadyIC.mjs";
|
|
3
|
+
import { a as discoverFromSql, i as fromSqlProjection, n as SUPABASE_CONTACT_NAME_COLUMN, r as supabaseContactSource, t as ContactTableMissing } from "./supabase-contact-D-oDC4Qk.mjs";
|
|
4
|
+
import { E as filtersAnonymous } from "./sync-CnWT5juF.mjs";
|
|
5
5
|
import { n as pgExecutor, r as postgresJsExecutor, t as connectPostgresJs } from "./executor-CJEWurjZ.mjs";
|
|
6
|
-
import { t as sqlIntrospector } from "./introspector-
|
|
6
|
+
import { t as sqlIntrospector } from "./introspector-B1S7qmvy.mjs";
|
|
7
7
|
export { ContactTableMissing, SUPABASE_CONTACT_NAME_COLUMN, connectPostgresJs, discoverFromSql, existsFromSql, filtersAnonymous, fromSql, fromSqlProjection, pgExecutor, postgresJsExecutor, sqlAppIdStore, sqlIntrospector, supabaseContactSource };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const require_entity_config = require("./entity-config-BP8Cc_nC.cjs");
|
|
2
2
|
const require_mappings_config = require("./mappings-config-CC-vltWb.cjs");
|
|
3
|
-
const require_sync = require("./sync-
|
|
4
|
-
const require_projection_guard = require("./projection-guard-
|
|
3
|
+
const require_sync = require("./sync-R0iVlooB.cjs");
|
|
4
|
+
const require_projection_guard = require("./projection-guard-yhVTvx37.cjs");
|
|
5
5
|
const require_projection = require("./projection-w4Z6vSL2.cjs");
|
|
6
|
+
const require_track_writes = require("./track-writes-C22_e_W-.cjs");
|
|
6
7
|
// ../sdk/src/deployment-identity.ts
|
|
7
8
|
const MAX_DEPLOYMENT_ID_LENGTH = 128;
|
|
8
9
|
const PLATFORM_DEPLOYMENT_KEYS = [
|
|
@@ -643,6 +644,7 @@ function createInstantPushGate(options = {}) {
|
|
|
643
644
|
refilledAt = at;
|
|
644
645
|
};
|
|
645
646
|
return {
|
|
647
|
+
isPaused: () => now() < pausedUntil,
|
|
646
648
|
acquire() {
|
|
647
649
|
const at = now();
|
|
648
650
|
if (at < pausedUntil) return {
|
|
@@ -689,6 +691,159 @@ function createInstantPushGate(options = {}) {
|
|
|
689
691
|
function isInstantPushDisabled(env) {
|
|
690
692
|
return env.CLIVLY_INSTANT_PUSH_DISABLED === "true";
|
|
691
693
|
}
|
|
694
|
+
const IDS_PER_READ = 200;
|
|
695
|
+
const LOCAL_LIMIT_WAIT_MS = 1e3;
|
|
696
|
+
/** Reads that may fail before an id is taken to be unreadable. */
|
|
697
|
+
const MAX_FAILED_READS = 5;
|
|
698
|
+
/** Failed single reads in a row that mean the database itself is down. */
|
|
699
|
+
const MAX_FAILURES_IN_A_ROW = 2;
|
|
700
|
+
/** A run with no progress for this long was cut off with its request. */
|
|
701
|
+
const RUN_DEAD_MS = 3e4;
|
|
702
|
+
function createInstantRecovery(options) {
|
|
703
|
+
const missed = /* @__PURE__ */ new Map();
|
|
704
|
+
const overflowed = /* @__PURE__ */ new Set();
|
|
705
|
+
const running = /* @__PURE__ */ new Map();
|
|
706
|
+
const remember = (entity, ids, deleted) => {
|
|
707
|
+
const known = missed.get(entity) ?? /* @__PURE__ */ new Map();
|
|
708
|
+
missed.set(entity, known);
|
|
709
|
+
for (const id of ids) {
|
|
710
|
+
if (known.size >= 1e3 && !known.has(String(id))) {
|
|
711
|
+
overflowed.add(entity);
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
known.set(String(id), {
|
|
715
|
+
deleted,
|
|
716
|
+
failedReads: 0,
|
|
717
|
+
id
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
/** Put a batch back. Anything recorded meanwhile is newer than it. */
|
|
722
|
+
const keep = (entity, batch) => {
|
|
723
|
+
for (const entry of batch) if (!missed.get(entity)?.has(String(entry.id))) {
|
|
724
|
+
remember(entity, [entry.id], entry.deleted);
|
|
725
|
+
missed.get(entity)?.set(String(entry.id), entry);
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
const sendBatch = async (entity, target, batch) => {
|
|
729
|
+
let rows;
|
|
730
|
+
try {
|
|
731
|
+
rows = await target.read(batch.map(({ id }) => id));
|
|
732
|
+
} catch {
|
|
733
|
+
return await afterFailedRead(entity, target, batch);
|
|
734
|
+
}
|
|
735
|
+
const present = new Set(rows.map((row) => String(row[target.idField])));
|
|
736
|
+
const gone = batch.filter(({ deleted, id }) => deleted && !present.has(String(id))).map(({ id }) => id);
|
|
737
|
+
if (rows.length + gone.length === 0) return "sent";
|
|
738
|
+
let result;
|
|
739
|
+
try {
|
|
740
|
+
result = await target.send(rows, gone);
|
|
741
|
+
} catch {
|
|
742
|
+
result = "refused";
|
|
743
|
+
}
|
|
744
|
+
if (result !== "sent" && result !== "refused") keep(entity, batch);
|
|
745
|
+
return result;
|
|
746
|
+
};
|
|
747
|
+
const afterFailedRead = async (entity, target, batch) => {
|
|
748
|
+
const [only] = batch;
|
|
749
|
+
if (only && batch.length === 1) {
|
|
750
|
+
if (only.failedReads + 1 < MAX_FAILED_READS) {
|
|
751
|
+
keep(entity, [{
|
|
752
|
+
...only,
|
|
753
|
+
failedReads: only.failedReads + 1
|
|
754
|
+
}]);
|
|
755
|
+
return "paused";
|
|
756
|
+
}
|
|
757
|
+
return "refused";
|
|
758
|
+
}
|
|
759
|
+
let last = "sent";
|
|
760
|
+
let failures = 0;
|
|
761
|
+
for (const [index, one] of batch.entries()) {
|
|
762
|
+
const result = await sendBatch(entity, target, [one]);
|
|
763
|
+
if (result === "sent" || result === "refused") {
|
|
764
|
+
failures = 0;
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
last = result;
|
|
768
|
+
failures += 1;
|
|
769
|
+
if (failures >= MAX_FAILURES_IN_A_ROW) {
|
|
770
|
+
keep(entity, batch.slice(index + 1));
|
|
771
|
+
break;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return last;
|
|
775
|
+
};
|
|
776
|
+
const drain = async (entity, target, run) => {
|
|
777
|
+
for (;;) {
|
|
778
|
+
const known = missed.get(entity);
|
|
779
|
+
const batch = [...known?.values() ?? []].slice(0, IDS_PER_READ);
|
|
780
|
+
if (batch.length === 0) return "sent";
|
|
781
|
+
for (const { id } of batch) known?.delete(String(id));
|
|
782
|
+
run.batch = batch;
|
|
783
|
+
run.progressAt = Date.now();
|
|
784
|
+
const result = await sendBatch(entity, target, batch);
|
|
785
|
+
run.batch = [];
|
|
786
|
+
if (result !== "sent" && result !== "refused") return result;
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
/**
|
|
790
|
+
* Send what was missed, once. One run per entity at a time; a second caller
|
|
791
|
+
* returns at once rather than awaiting the first one's promise, which on
|
|
792
|
+
* Workers belongs to another request and may never settle. The run under
|
|
793
|
+
* way sends whatever the second caller added.
|
|
794
|
+
*/
|
|
795
|
+
const recover = async (entity, target) => {
|
|
796
|
+
const current = running.get(entity);
|
|
797
|
+
if (current) {
|
|
798
|
+
if (Date.now() - current.progressAt < RUN_DEAD_MS) return "paused";
|
|
799
|
+
keep(entity, current.batch);
|
|
800
|
+
}
|
|
801
|
+
const run = {
|
|
802
|
+
batch: [],
|
|
803
|
+
progressAt: Date.now()
|
|
804
|
+
};
|
|
805
|
+
running.set(entity, run);
|
|
806
|
+
try {
|
|
807
|
+
return await drain(entity, target, run);
|
|
808
|
+
} finally {
|
|
809
|
+
if (running.get(entity) === run) running.delete(entity);
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
/**
|
|
813
|
+
* After a push came to `result`: keep the ids if it did not arrive, and try
|
|
814
|
+
* again while trying is likely to help. A pause (Clivly is limiting or down)
|
|
815
|
+
* is not waited out here; the next push or catch-up sends them.
|
|
816
|
+
*/
|
|
817
|
+
const afterPush = async (entity, target, ids, deleted, result) => {
|
|
818
|
+
if (result === "refused") return result;
|
|
819
|
+
if (result === "sent") return (missed.get(entity)?.size ?? 0) > 0 ? await recover(entity, target) : result;
|
|
820
|
+
remember(entity, ids, deleted);
|
|
821
|
+
let last = result;
|
|
822
|
+
let attempt = 1;
|
|
823
|
+
while (last === "limited" || last === "failed") {
|
|
824
|
+
if (last === "failed") {
|
|
825
|
+
if (attempt >= options.maxAttempts) break;
|
|
826
|
+
await options.sleep(options.baseDelayMs * 2 ** (attempt - 1) + Math.random() * options.baseDelayMs);
|
|
827
|
+
attempt += 1;
|
|
828
|
+
} else await options.sleep(LOCAL_LIMIT_WAIT_MS);
|
|
829
|
+
last = await recover(entity, target);
|
|
830
|
+
}
|
|
831
|
+
return last;
|
|
832
|
+
};
|
|
833
|
+
return {
|
|
834
|
+
afterPush,
|
|
835
|
+
recover,
|
|
836
|
+
remember,
|
|
837
|
+
hasMissed: (entity) => (missed.get(entity)?.size ?? 0) > 0,
|
|
838
|
+
entities: () => [...missed.keys()],
|
|
839
|
+
/** Entities with more missed rows than are kept, cleared as it is read. */
|
|
840
|
+
takeOverflowed: () => {
|
|
841
|
+
const all = [...overflowed];
|
|
842
|
+
overflowed.clear();
|
|
843
|
+
return all;
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
}
|
|
692
847
|
// ../sdk/src/presence-machine.ts
|
|
693
848
|
const PRESENCE_BACKOFF_BASE_MS = 1e3;
|
|
694
849
|
const PRESENCE_BACKOFF_MAX_MS = 3e4;
|
|
@@ -1389,11 +1544,11 @@ function idOrderAfter(after) {
|
|
|
1389
1544
|
* profile lowers the Supabase join's `greatest`), and a row that jumps behind
|
|
1390
1545
|
* a cursor-ordered listing is left out of it and archived. An id never moves.
|
|
1391
1546
|
*/
|
|
1392
|
-
async function idOrderPage(fetchIdPage, after, limit) {
|
|
1393
|
-
const rows = await fetchIdPage({
|
|
1547
|
+
async function idOrderPage(fetchIdPage, idField, after, limit) {
|
|
1548
|
+
const rows = require_sync.withRowKeys(await fetchIdPage({
|
|
1394
1549
|
afterId: idOrderAfter(after),
|
|
1395
1550
|
limit
|
|
1396
|
-
});
|
|
1551
|
+
}), idField);
|
|
1397
1552
|
const ids = idsOf(rows);
|
|
1398
1553
|
const last = ids.at(-1);
|
|
1399
1554
|
return {
|
|
@@ -1407,10 +1562,10 @@ async function idOrderPage(fetchIdPage, after, limit) {
|
|
|
1407
1562
|
/** A page in the order a delta pull reads, for a source with no id listing. */
|
|
1408
1563
|
async function cursorOrderPage(source, after, limit) {
|
|
1409
1564
|
const since = require_sync.parseWireCursors({ after })?.after ?? null;
|
|
1410
|
-
const rows = await source.fetchPage({
|
|
1565
|
+
const rows = require_sync.withRowKeys(await source.fetchPage({
|
|
1411
1566
|
since,
|
|
1412
1567
|
limit
|
|
1413
|
-
});
|
|
1568
|
+
}), source.idField);
|
|
1414
1569
|
const cursor = require_sync.advanceCursor(source, rows, since);
|
|
1415
1570
|
const stalled = cursor !== null && since !== null && cursor.time.getTime() === since.time.getTime() && cursor.id === since.id;
|
|
1416
1571
|
return {
|
|
@@ -1441,7 +1596,7 @@ async function listSourceIds(config, request) {
|
|
|
1441
1596
|
};
|
|
1442
1597
|
const { source } = found;
|
|
1443
1598
|
const limit = readLimit(request.limit);
|
|
1444
|
-
const { ids, next } = source.fetchIdPage ? await idOrderPage(source.fetchIdPage.bind(source), request.after ?? null, limit) : await cursorOrderPage(source, request.after ?? null, limit);
|
|
1599
|
+
const { ids, next } = source.fetchIdPage ? await idOrderPage(source.fetchIdPage.bind(source), source.idField, request.after ?? null, limit) : await cursorOrderPage(source, request.after ?? null, limit);
|
|
1445
1600
|
return {
|
|
1446
1601
|
status: 200,
|
|
1447
1602
|
body: {
|
|
@@ -1845,6 +2000,14 @@ function bearerToken(req) {
|
|
|
1845
2000
|
if (!(header && BEARER_PREFIX.test(header))) return null;
|
|
1846
2001
|
return header.replace(BEARER_PREFIX, "").trim();
|
|
1847
2002
|
}
|
|
2003
|
+
const DATABASE_AWAKE_MS = 6e4;
|
|
2004
|
+
const CATCH_UP_DEAD_MS = 12e4;
|
|
2005
|
+
const CATCH_UP_INTERVAL_MS = 9e5;
|
|
2006
|
+
const CATCH_UP_MARKER = "clivly:catch-up";
|
|
2007
|
+
const bareTable = (table) => table.startsWith("public.") ? table.slice(7) : table;
|
|
2008
|
+
function hasWaitUntil(value) {
|
|
2009
|
+
return typeof value === "object" && value !== null && typeof value.waitUntil === "function";
|
|
2010
|
+
}
|
|
1848
2011
|
function isRetryableStatus(status) {
|
|
1849
2012
|
return status === RATE_LIMITED_STATUS || status >= RETRYABLE_STATUS_FLOOR;
|
|
1850
2013
|
}
|
|
@@ -2018,6 +2181,15 @@ function createClivlySDK(config) {
|
|
|
2018
2181
|
const cursorStore = config.cursorStore ?? memoryCursorStore();
|
|
2019
2182
|
const fetcher = createFetcher(config.retry);
|
|
2020
2183
|
const instantFetcher = createFetcher(config.retry, { retryRateLimited: false });
|
|
2184
|
+
const singleFetcher = createFetcher({
|
|
2185
|
+
...config.retry,
|
|
2186
|
+
maxAttempts: 1
|
|
2187
|
+
});
|
|
2188
|
+
const recovery = createInstantRecovery({
|
|
2189
|
+
baseDelayMs: config.retry?.baseDelayMs ?? DEFAULT_BASE_DELAY_MS,
|
|
2190
|
+
maxAttempts: Math.max(1, config.retry?.maxAttempts ?? DEFAULT_MAX_ATTEMPTS),
|
|
2191
|
+
sleep
|
|
2192
|
+
});
|
|
2021
2193
|
const instantGate = createInstantPushGate({ perSecond: config.sync?.instantPushesPerSecond });
|
|
2022
2194
|
let warnedUnsignedTrigger = false;
|
|
2023
2195
|
let warnedNonPostTrigger = false;
|
|
@@ -2222,10 +2394,10 @@ function createClivlySDK(config) {
|
|
|
2222
2394
|
...rest
|
|
2223
2395
|
};
|
|
2224
2396
|
};
|
|
2397
|
+
if (config.sync?.instant === false) console.warn("Clivly: sync.instant: false is ignored. Instant push is on for every app; set CLIVLY_INSTANT_PUSH_DISABLED=true to stop it in an emergency.");
|
|
2225
2398
|
const instantTarget = (entityKey) => {
|
|
2226
|
-
if (config.sync?.instant !== true) return null;
|
|
2227
2399
|
if (isInstantPushDisabled(config.env ?? readProcessEnv())) return null;
|
|
2228
|
-
const resolved = require_sync.
|
|
2400
|
+
const resolved = require_sync.resolvePushSource(config, entityKey);
|
|
2229
2401
|
if (!resolved) {
|
|
2230
2402
|
console.warn(`Clivly instant push: no source is configured for entity "${entityKey}" — this row was not pushed. It will still sync on the next delta run once \`entities\` declares it.`);
|
|
2231
2403
|
return null;
|
|
@@ -2244,13 +2416,13 @@ function createClivlySDK(config) {
|
|
|
2244
2416
|
skipWarnedAt = at;
|
|
2245
2417
|
console.warn(`Clivly instant push skipped for "${entityKey}" (${reason === "paused" ? "Clivly is rate limiting or unreachable, paused briefly" : "more than the instant push rate a second"}). Skipped rows sync on the next delta run.`);
|
|
2246
2418
|
};
|
|
2247
|
-
const sendInstant = async (entityKey, path, body) => {
|
|
2419
|
+
const sendInstant = async (entityKey, path, body, send) => {
|
|
2248
2420
|
const decision = instantGate.acquire();
|
|
2249
2421
|
if (!decision.allowed) {
|
|
2250
2422
|
warnSkipped(entityKey, decision.reason);
|
|
2251
|
-
return "
|
|
2423
|
+
return decision.reason === "paused" ? "paused" : "limited";
|
|
2252
2424
|
}
|
|
2253
|
-
const response = await
|
|
2425
|
+
const response = await send(`${apiUrl}${path}`, {
|
|
2254
2426
|
method: "POST",
|
|
2255
2427
|
headers: {
|
|
2256
2428
|
"content-type": "application/json",
|
|
@@ -2262,43 +2434,192 @@ function createClivlySDK(config) {
|
|
|
2262
2434
|
status: response?.status ?? null,
|
|
2263
2435
|
retryAfterMs: response ? retryAfterMs(response) : null
|
|
2264
2436
|
});
|
|
2265
|
-
if (
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2437
|
+
if (response?.ok) {
|
|
2438
|
+
await warnIfSilent({
|
|
2439
|
+
source: body.source,
|
|
2440
|
+
objectType: body.objectType
|
|
2441
|
+
}, response);
|
|
2442
|
+
return "sent";
|
|
2443
|
+
}
|
|
2444
|
+
if (response?.status === RATE_LIMITED_STATUS) return "paused";
|
|
2445
|
+
return response && !isRetryableStatus(response.status) ? "refused" : "failed";
|
|
2446
|
+
};
|
|
2447
|
+
const warnPushFailed = (entityKey, error) => {
|
|
2448
|
+
const why = error === void 0 ? "" : `: ${error instanceof Error ? error.message : String(error)}`;
|
|
2449
|
+
console.warn(`Clivly instant push failed for "${entityKey}"${why}. This row will still sync on the next delta run.`);
|
|
2450
|
+
};
|
|
2451
|
+
const sendChanges = (entityKey, { path, resolved }, rows, deleted, send) => {
|
|
2452
|
+
const keyed = require_sync.withRowKeys(rows, resolved.source.idField);
|
|
2453
|
+
return sendInstant(entityKey, path, {
|
|
2454
|
+
source: require_sync.pushSourceOf(resolved.source),
|
|
2455
|
+
rows: keyed,
|
|
2456
|
+
mode: "delta",
|
|
2457
|
+
...keyed.length > 0 ? { sourceFields: require_sync.sourceTimeFields(resolved.source, keyed) } : {},
|
|
2458
|
+
...deleted.length > 0 ? { deleted: deleted.map(String) } : {},
|
|
2459
|
+
...objectTypeOf(resolved)
|
|
2460
|
+
}, send);
|
|
2461
|
+
};
|
|
2462
|
+
const recoveryTargetOf = (entityKey, target) => {
|
|
2463
|
+
const read = require_sync.fetchByIdsOf(target.resolved.source);
|
|
2464
|
+
return read ? {
|
|
2465
|
+
read,
|
|
2466
|
+
idField: target.resolved.source.idField ?? "id",
|
|
2467
|
+
send: (rows, deleted) => sendChanges(entityKey, target, rows, deleted, singleFetcher)
|
|
2468
|
+
} : null;
|
|
2469
|
+
};
|
|
2470
|
+
const deliver = async (entityKey, target, rows, deleted) => {
|
|
2471
|
+
const again = recoveryTargetOf(entityKey, target);
|
|
2472
|
+
const first = await sendChanges(entityKey, target, rows, deleted, again ? singleFetcher : instantFetcher);
|
|
2473
|
+
const idField = target.resolved.source.idField ?? "id";
|
|
2474
|
+
const ids = rows.map((row) => row[idField] ?? row.id).filter((id) => typeof id === "string" || typeof id === "number");
|
|
2475
|
+
const result = again ? await recovery.afterPush(entityKey, again, deleted.length > 0 ? deleted : ids, deleted.length > 0, first) : first;
|
|
2476
|
+
if (result === "failed" || result === "refused") warnPushFailed(entityKey);
|
|
2477
|
+
return result;
|
|
2271
2478
|
};
|
|
2272
2479
|
const pushOne = async (entityKey, row) => {
|
|
2480
|
+
noteDatabaseActivity();
|
|
2273
2481
|
const target = instantTarget(entityKey);
|
|
2274
2482
|
if (!target) return;
|
|
2275
|
-
const { path, resolved } = target;
|
|
2276
2483
|
try {
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2484
|
+
await deliver(entityKey, target, [row], []);
|
|
2485
|
+
} catch (error) {
|
|
2486
|
+
warnPushFailed(entityKey, error);
|
|
2487
|
+
}
|
|
2488
|
+
};
|
|
2489
|
+
const pushById = async (entityKey, ids) => {
|
|
2490
|
+
noteDatabaseActivity();
|
|
2491
|
+
const target = instantTarget(entityKey);
|
|
2492
|
+
if (!target || ids.length === 0) return {
|
|
2493
|
+
missing: [],
|
|
2494
|
+
retry: false
|
|
2495
|
+
};
|
|
2496
|
+
const again = recoveryTargetOf(entityKey, target);
|
|
2497
|
+
if (!again) {
|
|
2498
|
+
console.warn(`Clivly instant push: the source for "${entityKey}" cannot read rows by id (it is hand-written, or its fetchPage was replaced). Give it a fetchByIds; until then these rows sync on the next delta run.`);
|
|
2499
|
+
return {
|
|
2500
|
+
missing: [],
|
|
2501
|
+
retry: false
|
|
2502
|
+
};
|
|
2503
|
+
}
|
|
2504
|
+
try {
|
|
2505
|
+
const rows = await again.read(ids);
|
|
2506
|
+
const present = new Set(rows.map((row) => String(row[again.idField])));
|
|
2507
|
+
const result = rows.length > 0 ? await deliver(entityKey, target, rows, []) : "sent";
|
|
2508
|
+
return {
|
|
2509
|
+
missing: ids.filter((id) => !present.has(String(id))),
|
|
2510
|
+
retry: result !== "sent" && result !== "refused"
|
|
2511
|
+
};
|
|
2284
2512
|
} catch (error) {
|
|
2285
|
-
|
|
2513
|
+
recovery.remember(entityKey, ids, false);
|
|
2514
|
+
warnPushFailed(entityKey, error);
|
|
2515
|
+
return {
|
|
2516
|
+
missing: [],
|
|
2517
|
+
retry: true
|
|
2518
|
+
};
|
|
2286
2519
|
}
|
|
2287
2520
|
};
|
|
2288
2521
|
const pushDelete = async (entityKey, ids) => {
|
|
2522
|
+
noteDatabaseActivity();
|
|
2289
2523
|
const target = instantTarget(entityKey);
|
|
2290
2524
|
if (!target || ids.length === 0) return;
|
|
2291
|
-
const { path, resolved } = target;
|
|
2292
2525
|
try {
|
|
2293
|
-
|
|
2294
|
-
source: require_sync.pushSourceOf(resolved.source),
|
|
2295
|
-
rows: [],
|
|
2296
|
-
mode: "delta",
|
|
2297
|
-
deleted: ids.map(String),
|
|
2298
|
-
...objectTypeOf(resolved)
|
|
2299
|
-
}) === "failed") console.warn(`Clivly instant delete failed for "${entityKey}". Clivly's daily delete check will still archive these records.`);
|
|
2526
|
+
await deliver(entityKey, target, [], ids);
|
|
2300
2527
|
} catch (error) {
|
|
2301
|
-
|
|
2528
|
+
warnPushFailed(entityKey, error);
|
|
2529
|
+
}
|
|
2530
|
+
};
|
|
2531
|
+
let databaseUsedAt = Number.NEGATIVE_INFINITY;
|
|
2532
|
+
function noteDatabaseActivity() {
|
|
2533
|
+
databaseUsedAt = Date.now();
|
|
2534
|
+
}
|
|
2535
|
+
const databaseIsAwake = () => Date.now() - databaseUsedAt <= DATABASE_AWAKE_MS;
|
|
2536
|
+
const instantStopped = () => isInstantPushDisabled(config.env ?? readProcessEnv());
|
|
2537
|
+
const deltaFromSavedPosition = async (entityKey) => {
|
|
2538
|
+
const target = instantTarget(entityKey);
|
|
2539
|
+
if (!target || target.resolved.source.mode === "full") return;
|
|
2540
|
+
const { source } = target.resolved;
|
|
2541
|
+
if (!await cursorStore.get(source.entity)) {
|
|
2542
|
+
warnNoPosition(entityKey);
|
|
2543
|
+
return;
|
|
2544
|
+
}
|
|
2545
|
+
await syncOne(source, target.path, "delta", objectTypeOf(target.resolved).objectType);
|
|
2546
|
+
};
|
|
2547
|
+
const warnedNoPosition = /* @__PURE__ */ new Set();
|
|
2548
|
+
const warnNoPosition = (entityKey) => {
|
|
2549
|
+
if (warnedNoPosition.has(entityKey)) return;
|
|
2550
|
+
warnedNoPosition.add(entityKey);
|
|
2551
|
+
console.warn(`Clivly instant push: "${entityKey}" has no saved sync position here, so writes whose ids are unknown wait for the next sync. Add .returning() to those writes, or pass a persistent cursorStore.`);
|
|
2552
|
+
};
|
|
2553
|
+
const processStartedAt = Date.now();
|
|
2554
|
+
let lastCatchUpAt = null;
|
|
2555
|
+
const catchUpIsDue = async () => {
|
|
2556
|
+
if (lastCatchUpAt === null) lastCatchUpAt = (config.cursorStore ? await cursorStore.get(CATCH_UP_MARKER) : null)?.time.getTime() ?? (config.cursorStore ? 0 : processStartedAt);
|
|
2557
|
+
return Date.now() - lastCatchUpAt >= CATCH_UP_INTERVAL_MS;
|
|
2558
|
+
};
|
|
2559
|
+
const runCatchUp = async () => {
|
|
2560
|
+
for (const entityKey of recovery.entities()) {
|
|
2561
|
+
const target = instantTarget(entityKey);
|
|
2562
|
+
const again = target && recoveryTargetOf(entityKey, target);
|
|
2563
|
+
if (again && recovery.hasMissed(entityKey)) await recovery.recover(entityKey, again);
|
|
2564
|
+
}
|
|
2565
|
+
for (const entityKey of recovery.takeOverflowed()) await deltaFromSavedPosition(entityKey);
|
|
2566
|
+
if (!await catchUpIsDue()) return;
|
|
2567
|
+
lastCatchUpAt = Date.now();
|
|
2568
|
+
if (config.cursorStore) await cursorStore.set(CATCH_UP_MARKER, {
|
|
2569
|
+
id: "",
|
|
2570
|
+
time: new Date(lastCatchUpAt)
|
|
2571
|
+
});
|
|
2572
|
+
for (const entityKey of Object.keys(config.entities?.entities ?? {})) if (require_sync.resolveEntitySource(config, entityKey)) await deltaFromSavedPosition(entityKey);
|
|
2573
|
+
};
|
|
2574
|
+
let catchingUpSince = null;
|
|
2575
|
+
const catchUp = async () => {
|
|
2576
|
+
if (!databaseIsAwake() || instantStopped() || instantGate.isPaused()) return;
|
|
2577
|
+
const startedAt = Date.now();
|
|
2578
|
+
if (catchingUpSince !== null && startedAt - catchingUpSince < CATCH_UP_DEAD_MS) return;
|
|
2579
|
+
catchingUpSince = startedAt;
|
|
2580
|
+
try {
|
|
2581
|
+
await runCatchUp();
|
|
2582
|
+
} catch (error) {
|
|
2583
|
+
console.warn(`Clivly catch-up failed: ${error instanceof Error ? error.message : String(error)}. It will be tried again.`);
|
|
2584
|
+
} finally {
|
|
2585
|
+
if (catchingUpSince === startedAt) catchingUpSince = null;
|
|
2586
|
+
}
|
|
2587
|
+
};
|
|
2588
|
+
const withCatchUp = (handler) => async (...args) => {
|
|
2589
|
+
const response = await handler(...args);
|
|
2590
|
+
const work = catchUp();
|
|
2591
|
+
const runtime = args.find(hasWaitUntil);
|
|
2592
|
+
try {
|
|
2593
|
+
runtime?.waitUntil(work);
|
|
2594
|
+
} catch {}
|
|
2595
|
+
return response;
|
|
2596
|
+
};
|
|
2597
|
+
const pushTracked = async (entityKey, write) => {
|
|
2598
|
+
const target = instantTarget(entityKey);
|
|
2599
|
+
if (!target) return;
|
|
2600
|
+
const { source } = target.resolved;
|
|
2601
|
+
const idsKnown = write.key === (source.idField ?? "id") && !config.entities?.entities?.[entityKey]?.projection;
|
|
2602
|
+
if (!(write.complete && idsKnown)) {
|
|
2603
|
+
if (!write.deleted) await deltaFromSavedPosition(entityKey);
|
|
2604
|
+
return;
|
|
2605
|
+
}
|
|
2606
|
+
const again = recoveryTargetOf(entityKey, target);
|
|
2607
|
+
if (!again) {
|
|
2608
|
+
await deltaFromSavedPosition(entityKey);
|
|
2609
|
+
return;
|
|
2610
|
+
}
|
|
2611
|
+
recovery.remember(entityKey, write.ids, write.deleted);
|
|
2612
|
+
const first = await recovery.recover(entityKey, again);
|
|
2613
|
+
await recovery.afterPush(entityKey, again, [], false, first);
|
|
2614
|
+
};
|
|
2615
|
+
const entitiesOfTable = (table) => Object.entries(config.entities?.entities ?? {}).filter(([key, entity]) => bareTable(entity.projection?.baseTable ?? entity.source) === bareTable(table) && require_sync.resolveEntitySource(config, key) !== null).map(([key]) => key);
|
|
2616
|
+
const recordWrites = async (writes) => {
|
|
2617
|
+
noteDatabaseActivity();
|
|
2618
|
+
if (instantStopped()) return;
|
|
2619
|
+
for (const write of writes) for (const entityKey of entitiesOfTable(write.table)) try {
|
|
2620
|
+
await pushTracked(entityKey, write);
|
|
2621
|
+
} catch (error) {
|
|
2622
|
+
warnPushFailed(entityKey, error);
|
|
2302
2623
|
}
|
|
2303
2624
|
};
|
|
2304
2625
|
const describeSyncTrigger = () => ({
|
|
@@ -2749,7 +3070,7 @@ function createClivlySDK(config) {
|
|
|
2749
3070
|
if (!user) return jsonResponse({ error: "unauthorized" }, 401);
|
|
2750
3071
|
return jsonResponse(user, 200);
|
|
2751
3072
|
};
|
|
2752
|
-
|
|
3073
|
+
const sdk = {
|
|
2753
3074
|
start,
|
|
2754
3075
|
stop,
|
|
2755
3076
|
heartbeat,
|
|
@@ -2765,9 +3086,18 @@ function createClivlySDK(config) {
|
|
|
2765
3086
|
sync: runSync,
|
|
2766
3087
|
createAuthVerifyHandler,
|
|
2767
3088
|
pushOne,
|
|
3089
|
+
pushById,
|
|
2768
3090
|
pushDelete,
|
|
3091
|
+
catchUp,
|
|
3092
|
+
withCatchUp,
|
|
3093
|
+
tracker: {
|
|
3094
|
+
noteDatabaseActivity,
|
|
3095
|
+
recordWrites
|
|
3096
|
+
},
|
|
2769
3097
|
recordConsent
|
|
2770
3098
|
};
|
|
3099
|
+
if (config.source) require_track_writes.registerTrackedSDK(sdk);
|
|
3100
|
+
return sdk;
|
|
2771
3101
|
}
|
|
2772
3102
|
Object.defineProperty(exports, "CURSOR_KEY_PREFIX", {
|
|
2773
3103
|
enumerable: true,
|