reflectdb 0.1.0
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/LICENSE +21 -0
- package/README.md +1260 -0
- package/dist/cjs/client/index.cjs +1252 -0
- package/dist/cjs/client/index.d.cts +629 -0
- package/dist/cjs/client/storage/indexeddb.cjs +253 -0
- package/dist/cjs/client/storage/indexeddb.d.cts +85 -0
- package/dist/cjs/core/index.cjs +202 -0
- package/dist/cjs/core/index.d.cts +473 -0
- package/dist/cjs/react/index.cjs +1512 -0
- package/dist/cjs/react/index.d.cts +748 -0
- package/dist/cjs/server/drizzle.cjs +413 -0
- package/dist/cjs/server/drizzle.d.cts +233 -0
- package/dist/cjs/server/index.cjs +4486 -0
- package/dist/cjs/server/index.d.cts +1988 -0
- package/dist/cjs/svelte/index.cjs +1414 -0
- package/dist/cjs/svelte/index.d.cts +645 -0
- package/dist/cjs/transport/bun-ws.cjs +244 -0
- package/dist/cjs/transport/bun-ws.d.cts +215 -0
- package/dist/cjs/transport/polling.cjs +285 -0
- package/dist/cjs/transport/polling.d.cts +210 -0
- package/dist/cjs/transport/sse.cjs +312 -0
- package/dist/cjs/transport/sse.d.cts +205 -0
- package/dist/cjs/transport/ws.cjs +330 -0
- package/dist/cjs/transport/ws.d.cts +236 -0
- package/dist/cjs/vanilla/index.cjs +1430 -0
- package/dist/cjs/vanilla/index.d.cts +657 -0
- package/dist/client/index.d.ts +629 -0
- package/dist/client/index.js +106 -0
- package/dist/client/storage/indexeddb.d.ts +85 -0
- package/dist/client/storage/indexeddb.js +213 -0
- package/dist/core/index.d.ts +473 -0
- package/dist/core/index.js +56 -0
- package/dist/react/index.d.ts +748 -0
- package/dist/react/index.js +366 -0
- package/dist/server/drizzle.d.ts +233 -0
- package/dist/server/drizzle.js +9 -0
- package/dist/server/index.d.ts +1988 -0
- package/dist/server/index.js +3959 -0
- package/dist/shared/esm-3tkwvysa.js +54 -0
- package/dist/shared/esm-b7xs9cde.js +4 -0
- package/dist/shared/esm-rw7jjtrv.js +58 -0
- package/dist/shared/esm-wkwx6bd9.js +25 -0
- package/dist/shared/esm-ytrd3hbq.js +1007 -0
- package/dist/shared/esm-z1xse19c.js +369 -0
- package/dist/svelte/index.d.ts +645 -0
- package/dist/svelte/index.js +262 -0
- package/dist/transport/bun-ws.d.ts +215 -0
- package/dist/transport/bun-ws.js +140 -0
- package/dist/transport/polling.d.ts +210 -0
- package/dist/transport/polling.js +181 -0
- package/dist/transport/sse.d.ts +205 -0
- package/dist/transport/sse.js +208 -0
- package/dist/transport/ws.d.ts +236 -0
- package/dist/transport/ws.js +226 -0
- package/dist/vanilla/index.d.ts +657 -0
- package/dist/vanilla/index.js +278 -0
- package/package.json +253 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/core/types.ts
|
|
2
|
+
class TransportSendError extends Error {
|
|
3
|
+
clientId;
|
|
4
|
+
cause;
|
|
5
|
+
constructor(clientId, message, cause) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.clientId = clientId;
|
|
8
|
+
this.cause = cause;
|
|
9
|
+
this.name = "TransportSendError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
var PROTOCOL_VERSION = 1;
|
|
13
|
+
var SUPPORTED_PROTOCOL_VERSIONS = [1];
|
|
14
|
+
var MAX_CLOCK_DRIFT_MS = 5 * 60 * 1000;
|
|
15
|
+
var MAX_BATCH_SIZE = 100;
|
|
16
|
+
var TOMBSTONE_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;
|
|
17
|
+
var SERVER_TOMBSTONE_RETENTION_MS = 90 * 24 * 60 * 60 * 1000;
|
|
18
|
+
|
|
19
|
+
class MutationError extends Error {
|
|
20
|
+
reason;
|
|
21
|
+
constructor(reason, message) {
|
|
22
|
+
super(message ?? reason);
|
|
23
|
+
this.reason = reason;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
var VALID_ERROR_REASONS = new Set([
|
|
27
|
+
"outside_shape",
|
|
28
|
+
"readonly_query",
|
|
29
|
+
"rate_limited",
|
|
30
|
+
"buffer_full",
|
|
31
|
+
"server_conflict",
|
|
32
|
+
"custom_conflict",
|
|
33
|
+
"merge_stale",
|
|
34
|
+
"clock_drift",
|
|
35
|
+
"replay",
|
|
36
|
+
"batch_too_large",
|
|
37
|
+
"schema_outdated",
|
|
38
|
+
"auth_revoked",
|
|
39
|
+
"compacted",
|
|
40
|
+
"mutation_rejected",
|
|
41
|
+
"server_error",
|
|
42
|
+
"unknown_query"
|
|
43
|
+
]);
|
|
44
|
+
function isErrorReason(value) {
|
|
45
|
+
return typeof value === "string" && VALID_ERROR_REASONS.has(value);
|
|
46
|
+
}
|
|
47
|
+
function reasonFromError(err) {
|
|
48
|
+
if (err instanceof MutationError && isErrorReason(err.reason)) {
|
|
49
|
+
return err.reason;
|
|
50
|
+
}
|
|
51
|
+
return "server_error";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { TransportSendError, PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS, MAX_CLOCK_DRIFT_MS, MAX_BATCH_SIZE, TOMBSTONE_RETENTION_MS, SERVER_TOMBSTONE_RETENTION_MS, MutationError, isErrorReason, reasonFromError };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MAX_CLOCK_DRIFT_MS
|
|
3
|
+
} from "./esm-3tkwvysa.js";
|
|
4
|
+
|
|
5
|
+
// src/core/hlc.ts
|
|
6
|
+
var MS_PAD = 19;
|
|
7
|
+
var COUNTER_PAD = 4;
|
|
8
|
+
var COUNTER_MAX = 9999;
|
|
9
|
+
function createHlc(nodeId) {
|
|
10
|
+
return { ms: 0, counter: 0, nodeId };
|
|
11
|
+
}
|
|
12
|
+
function normalize(ms, counter, nodeId) {
|
|
13
|
+
if (counter > COUNTER_MAX) {
|
|
14
|
+
return { ms: ms + 1, counter: 0, nodeId };
|
|
15
|
+
}
|
|
16
|
+
return { ms, counter, nodeId };
|
|
17
|
+
}
|
|
18
|
+
function sendHlc(local) {
|
|
19
|
+
const wall = Date.now();
|
|
20
|
+
const ms = Math.max(wall, local.ms);
|
|
21
|
+
const counter = ms === local.ms ? local.counter + 1 : 0;
|
|
22
|
+
return normalize(ms, counter, local.nodeId);
|
|
23
|
+
}
|
|
24
|
+
function receiveHlc(local, remote) {
|
|
25
|
+
const wall = Date.now();
|
|
26
|
+
const clampedRemoteMs = Math.min(remote.ms, wall + MAX_CLOCK_DRIFT_MS);
|
|
27
|
+
const ms = Math.max(wall, local.ms, clampedRemoteMs);
|
|
28
|
+
const counter = ms === local.ms && ms === clampedRemoteMs ? Math.max(local.counter, remote.counter) + 1 : ms === local.ms ? local.counter + 1 : ms === clampedRemoteMs ? remote.counter + 1 : 0;
|
|
29
|
+
return normalize(ms, counter, local.nodeId);
|
|
30
|
+
}
|
|
31
|
+
function packHlc(hlc) {
|
|
32
|
+
const ms = String(hlc.ms).padStart(MS_PAD, "0");
|
|
33
|
+
const counter = String(hlc.counter).padStart(COUNTER_PAD, "0");
|
|
34
|
+
return `${ms}.${counter}.${hlc.nodeId}`;
|
|
35
|
+
}
|
|
36
|
+
function unpackHlc(packed) {
|
|
37
|
+
const firstDot = packed.indexOf(".");
|
|
38
|
+
const secondDot = firstDot < 0 ? -1 : packed.indexOf(".", firstDot + 1);
|
|
39
|
+
if (firstDot < 0 || secondDot < 0) {
|
|
40
|
+
throw new Error(`Malformed HLC: ${packed}`);
|
|
41
|
+
}
|
|
42
|
+
const ms = Number(packed.slice(0, firstDot));
|
|
43
|
+
const counter = Number(packed.slice(firstDot + 1, secondDot));
|
|
44
|
+
const nodeId = packed.slice(secondDot + 1);
|
|
45
|
+
if (!Number.isFinite(ms) || !Number.isFinite(counter) || nodeId.length === 0) {
|
|
46
|
+
throw new Error(`Malformed HLC: ${packed}`);
|
|
47
|
+
}
|
|
48
|
+
return { ms, counter, nodeId };
|
|
49
|
+
}
|
|
50
|
+
function compareHlc(a, b) {
|
|
51
|
+
if (a < b)
|
|
52
|
+
return -1;
|
|
53
|
+
if (a > b)
|
|
54
|
+
return 1;
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { createHlc, sendHlc, receiveHlc, packHlc, unpackHlc, compareHlc };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MAX_BATCH_SIZE,
|
|
3
|
+
MAX_CLOCK_DRIFT_MS,
|
|
4
|
+
MutationError,
|
|
5
|
+
PROTOCOL_VERSION,
|
|
6
|
+
SERVER_TOMBSTONE_RETENTION_MS,
|
|
7
|
+
SUPPORTED_PROTOCOL_VERSIONS,
|
|
8
|
+
TOMBSTONE_RETENTION_MS,
|
|
9
|
+
TransportSendError,
|
|
10
|
+
isErrorReason,
|
|
11
|
+
reasonFromError
|
|
12
|
+
} from "./esm-3tkwvysa.js";
|
|
13
|
+
import"./esm-b7xs9cde.js";
|
|
14
|
+
export {
|
|
15
|
+
reasonFromError,
|
|
16
|
+
isErrorReason,
|
|
17
|
+
TransportSendError,
|
|
18
|
+
TOMBSTONE_RETENTION_MS,
|
|
19
|
+
SUPPORTED_PROTOCOL_VERSIONS,
|
|
20
|
+
SERVER_TOMBSTONE_RETENTION_MS,
|
|
21
|
+
PROTOCOL_VERSION,
|
|
22
|
+
MutationError,
|
|
23
|
+
MAX_CLOCK_DRIFT_MS,
|
|
24
|
+
MAX_BATCH_SIZE
|
|
25
|
+
};
|