@neetru/sdk 3.7.0 → 3.8.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/CHANGELOG.md +75 -0
- package/README.md +66 -1
- package/dist/ai.cjs +221 -177
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +221 -177
- package/dist/auth.cjs +1051 -868
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +1048 -865
- package/dist/catalog.cjs +164 -97
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +164 -97
- package/dist/checkout.cjs +182 -115
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +181 -114
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db.cjs +437 -345
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +455 -363
- package/dist/entitlements.cjs +17 -10
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/entitlements.mjs +17 -10
- package/dist/errors.cjs +15 -8
- package/dist/errors.d.cts +24 -3
- package/dist/errors.d.ts +24 -3
- package/dist/errors.mjs +14 -7
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/index.cjs +1544 -1190
- package/dist/index.d.cts +177 -3
- package/dist/index.d.ts +177 -3
- package/dist/index.mjs +1676 -1329
- package/dist/mobile.cjs +400 -181
- package/dist/mobile.d.cts +59 -4
- package/dist/mobile.d.ts +59 -4
- package/dist/mobile.mjs +400 -181
- package/dist/mocks.cjs +9 -2
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.mjs +9 -2
- package/dist/notifications.cjs +197 -130
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +196 -129
- package/dist/react.cjs +303 -282
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +295 -274
- package/dist/support.cjs +299 -229
- package/dist/support.d.cts +8 -4
- package/dist/support.d.ts +8 -4
- package/dist/support.mjs +298 -228
- package/dist/telemetry.cjs +234 -167
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +234 -167
- package/dist/{types-Dx0IrOSF.d.ts → types-BYPk_3HR.d.ts} +104 -17
- package/dist/{types-Bx4kTLmk.d.cts → types-yv3EDKg2.d.cts} +104 -17
- package/dist/usage.cjs +183 -116
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +183 -116
- package/dist/webhooks.cjs +215 -148
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +212 -145
- package/package.json +2 -1
package/dist/db.cjs
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
var idb = require('idb');
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
5
|
+
var Ye = Object.defineProperty;
|
|
6
|
+
var S = (r, e) => () => (r && (e = r(r = 0)), e);
|
|
7
|
+
var me = (r, e) => {
|
|
8
8
|
for (var t in e)
|
|
9
|
-
|
|
9
|
+
Ye(r, t, { get: e[t], enumerable: true });
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// src/errors.ts
|
|
13
|
-
var
|
|
14
|
-
|
|
13
|
+
var b, x = S(() => {
|
|
14
|
+
b = class r extends Error {
|
|
15
15
|
code;
|
|
16
16
|
status;
|
|
17
17
|
requestId;
|
|
@@ -24,20 +24,27 @@ var g, x = T(() => {
|
|
|
24
24
|
* para um `NeetruDbErrorCode` mais específico (recuperável vs não).
|
|
25
25
|
*/
|
|
26
26
|
reason;
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* 3.8.0: `x-correlation-id` da requisição que falhou. O SDK gera um UUID por
|
|
29
|
+
* chamada (ou usa o que o chamador passou) e manda no header; o Core grava o
|
|
30
|
+
* mesmo id nos logs/auditoria — cite-o ao abrir um ticket de suporte.
|
|
31
|
+
* `undefined` em erros que acontecem antes de qualquer requisição.
|
|
32
|
+
*/
|
|
33
|
+
correlationId;
|
|
34
|
+
constructor(e, t, n, o, i, s) {
|
|
35
|
+
super(t), this.name = "NeetruError", this.code = e, this.status = n, this.requestId = o, this.reason = i, this.correlationId = s, Object.setPrototypeOf(this, r.prototype);
|
|
29
36
|
}
|
|
30
37
|
};
|
|
31
38
|
});
|
|
32
39
|
|
|
33
40
|
// src/db-errors.ts
|
|
34
|
-
var
|
|
41
|
+
var Ze; exports.NeetruDbError = void 0; var I = S(() => {
|
|
35
42
|
x();
|
|
36
|
-
|
|
43
|
+
Ze = /* @__PURE__ */ new Set([
|
|
37
44
|
"db_unavailable",
|
|
38
45
|
"db_conflict",
|
|
39
46
|
"db_timeout"
|
|
40
|
-
]), exports.NeetruDbError = class r extends
|
|
47
|
+
]), exports.NeetruDbError = class r extends b {
|
|
41
48
|
/** Código de erro fechado — específico de DB. */
|
|
42
49
|
code;
|
|
43
50
|
/**
|
|
@@ -51,36 +58,91 @@ var Qe; exports.NeetruDbError = void 0; var k = T(() => {
|
|
|
51
58
|
*/
|
|
52
59
|
dbId;
|
|
53
60
|
constructor(e, t, n) {
|
|
54
|
-
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable =
|
|
61
|
+
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable = Ze.has(e), this.dbId = n, Object.setPrototypeOf(this, r.prototype);
|
|
55
62
|
}
|
|
56
63
|
};
|
|
57
64
|
});
|
|
58
65
|
|
|
59
66
|
// src/idempotency.ts
|
|
60
|
-
function
|
|
67
|
+
function Te() {
|
|
61
68
|
let r = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
62
69
|
if (r && typeof r.randomUUID == "function")
|
|
63
70
|
return r.randomUUID();
|
|
64
|
-
throw new
|
|
71
|
+
throw new b(
|
|
65
72
|
"runtime_unsupported",
|
|
66
73
|
"crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
|
|
67
74
|
);
|
|
68
75
|
}
|
|
69
|
-
var
|
|
76
|
+
var Re = S(() => {
|
|
70
77
|
x();
|
|
71
78
|
});
|
|
72
79
|
|
|
80
|
+
// src/correlation.ts
|
|
81
|
+
function ke(r) {
|
|
82
|
+
return typeof r == "string" && r.length > 0 && r.length <= 128 && wt.test(r);
|
|
83
|
+
}
|
|
84
|
+
function St() {
|
|
85
|
+
let r = globalThis.crypto;
|
|
86
|
+
try {
|
|
87
|
+
if (r && typeof r.randomUUID == "function") return r.randomUUID();
|
|
88
|
+
} catch {
|
|
89
|
+
}
|
|
90
|
+
let e = new Uint8Array(16), t = false;
|
|
91
|
+
try {
|
|
92
|
+
r && typeof r.getRandomValues == "function" && (r.getRandomValues(e), t = !0);
|
|
93
|
+
} catch {
|
|
94
|
+
t = false;
|
|
95
|
+
}
|
|
96
|
+
if (!t)
|
|
97
|
+
for (let o = 0; o < e.length; o++) e[o] = Math.floor(Math.random() * 256);
|
|
98
|
+
e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128;
|
|
99
|
+
let n = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join("");
|
|
100
|
+
return `${n.slice(0, 8)}-${n.slice(8, 12)}-${n.slice(12, 16)}-${n.slice(16, 20)}-${n.slice(20)}`;
|
|
101
|
+
}
|
|
102
|
+
function Ce(r, e) {
|
|
103
|
+
let t, n = {};
|
|
104
|
+
for (let [i, s] of Object.entries(r)) {
|
|
105
|
+
if (i.toLowerCase() === De) {
|
|
106
|
+
ke(s) && (t = s);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
n[i] = s;
|
|
110
|
+
}
|
|
111
|
+
let o = t ?? (ke(e) ? e : void 0) ?? St();
|
|
112
|
+
return n[De] = o, { headers: n, correlationId: o };
|
|
113
|
+
}
|
|
114
|
+
var De, wt, Ie = S(() => {
|
|
115
|
+
De = "x-correlation-id", wt = /^[A-Za-z0-9._:\-]+$/;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// src/server-only-key.ts
|
|
119
|
+
function Pe() {
|
|
120
|
+
try {
|
|
121
|
+
let r = globalThis.navigator;
|
|
122
|
+
if (r && r.product === "ReactNative") return "react-native";
|
|
123
|
+
} catch {
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
let r = globalThis;
|
|
127
|
+
if (typeof r.window < "u" && typeof r.document < "u") return "browser";
|
|
128
|
+
} catch {
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
var Ee = S(() => {
|
|
133
|
+
});
|
|
134
|
+
|
|
73
135
|
// src/http.ts
|
|
74
136
|
var U = {};
|
|
75
|
-
|
|
76
|
-
checkCoreVersionCompat: () =>
|
|
77
|
-
httpRequest: () =>
|
|
78
|
-
statusToCode: () =>
|
|
137
|
+
me(U, {
|
|
138
|
+
checkCoreVersionCompat: () => qe,
|
|
139
|
+
httpRequest: () => Ot,
|
|
140
|
+
statusToCode: () => Fe
|
|
79
141
|
});
|
|
80
142
|
function se(r) {
|
|
81
143
|
return r.split("+")[0] ?? r;
|
|
82
144
|
}
|
|
83
|
-
function
|
|
145
|
+
function Rt(r, e) {
|
|
84
146
|
let t = se(r).split(".").map(Number), n = se(e).split(".").map(Number), o = Math.max(t.length, n.length);
|
|
85
147
|
for (let i = 0; i < o; i++) {
|
|
86
148
|
let s = t[i] ?? 0, a = n[i] ?? 0;
|
|
@@ -88,58 +150,62 @@ function ht(r, e) {
|
|
|
88
150
|
}
|
|
89
151
|
return 0;
|
|
90
152
|
}
|
|
91
|
-
function
|
|
153
|
+
function Ne() {
|
|
92
154
|
try {
|
|
93
|
-
return "3.
|
|
155
|
+
return "3.8.1";
|
|
94
156
|
} catch {
|
|
95
157
|
return;
|
|
96
158
|
}
|
|
97
159
|
}
|
|
98
|
-
function
|
|
99
|
-
let r =
|
|
160
|
+
function Dt() {
|
|
161
|
+
let r = Ne();
|
|
100
162
|
return r ? parseInt(r.split(".")[0] ?? "3", 10) : 3;
|
|
101
163
|
}
|
|
102
|
-
function
|
|
164
|
+
function qe(r) {
|
|
103
165
|
if (!r) return;
|
|
104
|
-
let e =
|
|
166
|
+
let e = Dt(), t = Tt[e];
|
|
105
167
|
if (!t) return;
|
|
106
168
|
let n = se(r);
|
|
107
|
-
if (
|
|
169
|
+
if (Rt(n, t) < 0) {
|
|
108
170
|
let o = `${e}:${n}`;
|
|
109
|
-
|
|
171
|
+
Le.has(o) || (Le.add(o), console.warn(
|
|
110
172
|
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${r}" \xE9 anterior ao m\xEDnimo recomendado "${t}" para SDK major ${e}. Alguns recursos podem n\xE3o funcionar corretamente. Atualize o Core para a vers\xE3o mais recente.`
|
|
111
173
|
));
|
|
112
174
|
}
|
|
113
175
|
}
|
|
114
|
-
function
|
|
176
|
+
function kt() {
|
|
177
|
+
let r = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
178
|
+
return Pe() ? "Esta opera\xE7\xE3o exige a credencial nrt_ do produto, que \xE9 S\xD3 DE SERVIDOR. No navegador/app, chame a API do seu produto (ex.: https://<produto>.neetru.com/api/v1) com o token do usu\xE1rio \u2014 o backend do produto faz esta chamada com o SDK e a chave. (" + r + ")" : r;
|
|
179
|
+
}
|
|
180
|
+
function Me(r) {
|
|
115
181
|
let e = 200 * Math.pow(4, r), t = e * 0.2 * (Math.random() * 2 - 1);
|
|
116
182
|
return Math.max(50, Math.round(e + t));
|
|
117
183
|
}
|
|
118
|
-
function
|
|
184
|
+
function Lt(r) {
|
|
119
185
|
if (!r) return null;
|
|
120
186
|
let e = Number(r);
|
|
121
|
-
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3),
|
|
187
|
+
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), Oe);
|
|
122
188
|
let t = Date.parse(r);
|
|
123
189
|
if (Number.isFinite(t)) {
|
|
124
190
|
let n = t - Date.now();
|
|
125
|
-
if (n > 0) return Math.min(n,
|
|
191
|
+
if (n > 0) return Math.min(n, Oe);
|
|
126
192
|
}
|
|
127
193
|
return null;
|
|
128
194
|
}
|
|
129
|
-
function
|
|
195
|
+
function xe(r) {
|
|
130
196
|
return new Promise((e) => setTimeout(e, r));
|
|
131
197
|
}
|
|
132
|
-
function
|
|
198
|
+
function Fe(r) {
|
|
133
199
|
return r === 401 ? "unauthorized" : r === 403 ? "forbidden" : r === 404 ? "not_found" : r === 409 ? "conflict" : r === 410 ? "gone" : r === 422 || r === 400 ? "validation_failed" : r === 429 ? "rate_limited" : r >= 500 ? "server_error" : "unknown";
|
|
134
200
|
}
|
|
135
|
-
function
|
|
201
|
+
function Mt(r, e, t) {
|
|
136
202
|
let n = r.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, i = new URL(`${n}${o}`);
|
|
137
203
|
if (t)
|
|
138
204
|
for (let [s, a] of Object.entries(t))
|
|
139
205
|
a !== void 0 && i.searchParams.set(s, String(a));
|
|
140
206
|
return i.toString();
|
|
141
207
|
}
|
|
142
|
-
async function
|
|
208
|
+
async function Ae(r) {
|
|
143
209
|
let e = await r.text();
|
|
144
210
|
if (e)
|
|
145
211
|
try {
|
|
@@ -148,95 +214,101 @@ async function Pe(r) {
|
|
|
148
214
|
return;
|
|
149
215
|
}
|
|
150
216
|
}
|
|
151
|
-
async function
|
|
152
|
-
let t = e.method ?? "GET", n =
|
|
153
|
-
accept: "application/json",
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (
|
|
217
|
+
async function Ot(r, e) {
|
|
218
|
+
let t = e.method ?? "GET", n = Mt(r.baseUrl, e.path, e.query), o = Pt.has(t) ? Ct : It, i = e.retries ?? o, { headers: s, correlationId: a } = Ce(
|
|
219
|
+
{ accept: "application/json", ...e.headers },
|
|
220
|
+
e.correlationId
|
|
221
|
+
), c = Ne();
|
|
222
|
+
if (c && (s["x-neetru-sdk-version"] = c), e.requireAuth) {
|
|
157
223
|
if (!r.apiKey)
|
|
158
|
-
throw new
|
|
159
|
-
"missing_api_key",
|
|
160
|
-
"This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
161
|
-
);
|
|
224
|
+
throw new b("missing_api_key", kt());
|
|
162
225
|
s.authorization = `Bearer ${r.apiKey}`;
|
|
163
226
|
}
|
|
164
227
|
if (e.idempotencyKey) {
|
|
165
|
-
let
|
|
166
|
-
s["idempotency-key"] =
|
|
167
|
-
}
|
|
168
|
-
let
|
|
169
|
-
|
|
170
|
-
let
|
|
171
|
-
for (let
|
|
172
|
-
if (
|
|
173
|
-
throw
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
228
|
+
let p = typeof e.idempotencyKey == "string" ? e.idempotencyKey : Te();
|
|
229
|
+
s["idempotency-key"] = p;
|
|
230
|
+
}
|
|
231
|
+
let l = e.body !== void 0 && t !== "GET" && t !== "DELETE" ? JSON.stringify(e.body) : void 0;
|
|
232
|
+
l !== void 0 && (s["content-type"] = "application/json");
|
|
233
|
+
let d = null, u = e.deadlineMs != null ? Date.now() + e.deadlineMs : null;
|
|
234
|
+
for (let p = 0; p <= i; p++) {
|
|
235
|
+
if (u != null && Date.now() >= u)
|
|
236
|
+
throw d ?? new b(
|
|
237
|
+
"network_error",
|
|
238
|
+
"Deadline excedido antes de completar a request",
|
|
239
|
+
void 0,
|
|
240
|
+
void 0,
|
|
241
|
+
void 0,
|
|
242
|
+
a
|
|
243
|
+
);
|
|
244
|
+
let h = { method: t, headers: s };
|
|
245
|
+
l !== void 0 && (h.body = l);
|
|
246
|
+
let _ = u != null ? Math.max(1, Math.min(3e4, u - Date.now())) : 3e4;
|
|
247
|
+
h.signal = AbortSignal.timeout(_);
|
|
248
|
+
let y;
|
|
179
249
|
try {
|
|
180
|
-
|
|
250
|
+
y = await r.fetch(n, h);
|
|
181
251
|
} catch (m) {
|
|
182
252
|
let O = m instanceof DOMException && m.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${m instanceof Error ? m.message : "fetch failed"}`;
|
|
183
|
-
if (
|
|
184
|
-
let
|
|
185
|
-
if (
|
|
186
|
-
await
|
|
253
|
+
if (d = new b("network_error", O, void 0, void 0, void 0, a), p < i) {
|
|
254
|
+
let he = Me(p);
|
|
255
|
+
if (u == null || Date.now() + he < u) {
|
|
256
|
+
await xe(he);
|
|
187
257
|
continue;
|
|
188
258
|
}
|
|
189
259
|
}
|
|
190
|
-
throw
|
|
260
|
+
throw d;
|
|
191
261
|
}
|
|
192
|
-
let
|
|
193
|
-
if (
|
|
194
|
-
let m =
|
|
195
|
-
return m &&
|
|
262
|
+
let B = y.headers.get("x-request-id") ?? y.headers.get("x-correlation-id") ?? void 0;
|
|
263
|
+
if (y.ok) {
|
|
264
|
+
let m = y.headers.get("x-neetru-version");
|
|
265
|
+
return m && qe(m), await Ae(y);
|
|
196
266
|
}
|
|
197
|
-
let
|
|
198
|
-
if (
|
|
199
|
-
let m =
|
|
200
|
-
typeof m == "string" ?
|
|
267
|
+
let g = await Ae(y), v = Fe(y.status), w = `HTTP ${y.status}`, C;
|
|
268
|
+
if (g && typeof g == "object") {
|
|
269
|
+
let m = g.error;
|
|
270
|
+
typeof m == "string" ? w = m : m && typeof m == "object" ? (typeof m.code == "string" && (v = m.code), typeof m.message == "string" && (w = m.message), typeof m.reason == "string" && (C = m.reason)) : (typeof g.code == "string" && (v = g.code), typeof g.message == "string" && (w = g.message)), typeof g.reason == "string" && (C = g.reason);
|
|
201
271
|
}
|
|
202
|
-
if (
|
|
203
|
-
let m =
|
|
204
|
-
|
|
272
|
+
if (y.status === 410) {
|
|
273
|
+
let m = y.headers.get("x-neetru-replacement");
|
|
274
|
+
w === `HTTP ${y.status}` && (w = `Endpoint ${e.path} foi removido do Core (HTTP 410 Gone) \u2014 atualize o @neetru/sdk e migre para a API substituta.`), m && !w.includes(m) && (w += ` Substituto: ${m}.`);
|
|
205
275
|
}
|
|
206
|
-
let
|
|
207
|
-
if (
|
|
208
|
-
let O =
|
|
209
|
-
if (
|
|
210
|
-
await
|
|
276
|
+
let fe = new b(v, w, y.status, B, C, a);
|
|
277
|
+
if (d = fe, Et.has(v) && p < i) {
|
|
278
|
+
let O = Lt(y.headers.get("retry-after")) ?? Me(p);
|
|
279
|
+
if (u == null || Date.now() + O < u) {
|
|
280
|
+
await xe(O);
|
|
211
281
|
continue;
|
|
212
282
|
}
|
|
213
283
|
}
|
|
214
|
-
throw
|
|
284
|
+
throw fe;
|
|
215
285
|
}
|
|
216
|
-
throw
|
|
286
|
+
throw d ?? new b("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, a);
|
|
217
287
|
}
|
|
218
|
-
var
|
|
288
|
+
var Tt, Le, Ct, It, Pt, Et, Oe, W = S(() => {
|
|
219
289
|
x();
|
|
220
|
-
|
|
221
|
-
|
|
290
|
+
Re();
|
|
291
|
+
Ie();
|
|
292
|
+
Ee();
|
|
293
|
+
Tt = {
|
|
222
294
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
223
295
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
224
296
|
3: "2026.05.1"
|
|
225
297
|
};
|
|
226
|
-
|
|
227
|
-
|
|
298
|
+
Le = /* @__PURE__ */ new Set();
|
|
299
|
+
Ct = 2, It = 0, Pt = /* @__PURE__ */ new Set(["GET", "HEAD"]), Et = /* @__PURE__ */ new Set([
|
|
228
300
|
"rate_limited",
|
|
229
301
|
"server_error",
|
|
230
302
|
"network_error"
|
|
231
303
|
]);
|
|
232
|
-
|
|
304
|
+
Oe = 6e4;
|
|
233
305
|
});
|
|
234
306
|
|
|
235
307
|
// src/db/sql/resilient-pool.ts
|
|
236
308
|
function J(r) {
|
|
237
309
|
return typeof r == "object" && r !== null && r[ae] === true;
|
|
238
310
|
}
|
|
239
|
-
function
|
|
311
|
+
function xt(r) {
|
|
240
312
|
let e = r && typeof r == "object" && "code" in r ? r.code : void 0;
|
|
241
313
|
if (e === "ECONNREFUSED" || e === "ETIMEDOUT" || e === "ENOTFOUND" || e === "EHOSTUNREACH" || e === "EAI_AGAIN")
|
|
242
314
|
return true;
|
|
@@ -248,56 +320,73 @@ function Tt(r) {
|
|
|
248
320
|
let n = (r instanceof Error ? r.message : typeof r == "string" ? r : "").toLowerCase();
|
|
249
321
|
return n.length === 0 ? false : !!(n.includes("certificate expired") || n.includes("certificate has expired") || n.includes("cert_has_expired") || n.includes("alert certificate expired") || n.includes("err_ssl") || n.includes("sslv3 alert") || n.includes("tlsv1 alert") || n.includes("econnrefused") || n.includes("etimedout") || n.includes("enotfound") || n.includes("ehostunreach") || n.includes("getaddrinfo"));
|
|
250
322
|
}
|
|
323
|
+
function Ue(r) {
|
|
324
|
+
let e = r && typeof r == "object" && "code" in r ? r.code : void 0;
|
|
325
|
+
if (typeof e == "string" && At.has(e.toUpperCase()))
|
|
326
|
+
return true;
|
|
327
|
+
let n = (r instanceof Error ? r.message : typeof r == "string" ? r : "").toLowerCase();
|
|
328
|
+
return n.length === 0 ? false : n.includes("password authentication failed") || n.includes("sasl authentication failed");
|
|
329
|
+
}
|
|
330
|
+
function Nt(r) {
|
|
331
|
+
return xt(r) || Ue(r);
|
|
332
|
+
}
|
|
251
333
|
function ce(r) {
|
|
252
334
|
let e = { current: r }, t = null, n = {
|
|
253
|
-
__neetruSwapInner(
|
|
254
|
-
let
|
|
255
|
-
return e.current =
|
|
335
|
+
__neetruSwapInner(d) {
|
|
336
|
+
let u = e.current;
|
|
337
|
+
return e.current = d, u;
|
|
256
338
|
},
|
|
257
339
|
__neetruGetInner() {
|
|
258
340
|
return e.current;
|
|
259
341
|
},
|
|
260
|
-
__neetruSetReLease(
|
|
261
|
-
t =
|
|
342
|
+
__neetruSetReLease(d) {
|
|
343
|
+
t = d;
|
|
262
344
|
}
|
|
263
|
-
}, o = (
|
|
345
|
+
}, o = (d, u, p) => u[d](
|
|
346
|
+
...p
|
|
347
|
+
), i = (d, u) => (...p) => p.length > 0 && typeof p[p.length - 1] == "function" ? o(d, e.current, p) : (async () => {
|
|
264
348
|
try {
|
|
265
|
-
return await o(e.current,
|
|
266
|
-
} catch (
|
|
267
|
-
let
|
|
268
|
-
if (
|
|
269
|
-
throw
|
|
349
|
+
return await o(d, e.current, p);
|
|
350
|
+
} catch (h) {
|
|
351
|
+
let _ = t;
|
|
352
|
+
if (_ === null || !u(h))
|
|
353
|
+
throw h;
|
|
270
354
|
try {
|
|
271
|
-
await
|
|
355
|
+
await _();
|
|
272
356
|
} catch {
|
|
273
|
-
throw
|
|
357
|
+
throw h;
|
|
274
358
|
}
|
|
275
|
-
return await o(e.current,
|
|
359
|
+
return await o(d, e.current, p);
|
|
276
360
|
}
|
|
277
|
-
})(), s = Object.create(Object.getPrototypeOf(r));
|
|
278
|
-
return new Proxy(
|
|
279
|
-
get(
|
|
280
|
-
if (
|
|
281
|
-
if (
|
|
282
|
-
if (
|
|
283
|
-
if (
|
|
284
|
-
if (
|
|
285
|
-
if (
|
|
361
|
+
})(), s = i("query", Nt), a = i("connect", Ue), c = Object.create(Object.getPrototypeOf(r));
|
|
362
|
+
return new Proxy(c, {
|
|
363
|
+
get(d, u) {
|
|
364
|
+
if (u === ae) return true;
|
|
365
|
+
if (u === "__neetruSwapInner") return n.__neetruSwapInner;
|
|
366
|
+
if (u === "__neetruGetInner") return n.__neetruGetInner;
|
|
367
|
+
if (u === "__neetruSetReLease") return n.__neetruSetReLease;
|
|
368
|
+
if (u === "query") return s;
|
|
369
|
+
if (u === "connect") {
|
|
370
|
+
let _ = e.current;
|
|
371
|
+
return typeof _.connect == "function" ? a : _.connect;
|
|
372
|
+
}
|
|
373
|
+
if (u === "constructor")
|
|
286
374
|
return e.current.constructor;
|
|
287
|
-
let
|
|
288
|
-
return typeof
|
|
375
|
+
let p = e.current, h = Reflect.get(p, u, p);
|
|
376
|
+
return typeof h == "function" ? h.bind(p) : h;
|
|
289
377
|
}
|
|
290
378
|
});
|
|
291
379
|
}
|
|
292
|
-
var ae, le =
|
|
380
|
+
var ae, At, le = S(() => {
|
|
293
381
|
ae = /* @__PURE__ */ Symbol.for("neetru.sdk.resilientPool");
|
|
382
|
+
At = /* @__PURE__ */ new Set(["28P01", "28000"]);
|
|
294
383
|
});
|
|
295
384
|
|
|
296
385
|
// src/db/sql/lease.ts
|
|
297
|
-
function
|
|
386
|
+
function Ft(r, e) {
|
|
298
387
|
return r ? r.leaseId !== e.leaseId || r.credentialVersion !== e.credentialVersion || r.host !== e.host || r.port !== e.port || r.dbName !== e.dbName || r.user !== e.user || r.password !== e.password || r.sslca !== e.sslca || r.clientCert !== e.clientCert || r.clientKey !== e.clientKey : true;
|
|
299
388
|
}
|
|
300
|
-
function
|
|
389
|
+
function Ut(r) {
|
|
301
390
|
if (r instanceof exports.NeetruDbError) return r;
|
|
302
391
|
let e = r instanceof Error ? r.message : String(r), t = r instanceof Error ? r.code : void 0;
|
|
303
392
|
return typeof t == "string" && (t === "ECONNREFUSED" || t === "ENOTFOUND" || t === "ETIMEDOUT") ? new exports.NeetruDbError(
|
|
@@ -305,10 +394,10 @@ function kt(r) {
|
|
|
305
394
|
`Pool connection failed (${t}): ${e}`
|
|
306
395
|
) : e.includes("statement timeout") || e.includes("lock timeout") ? new exports.NeetruDbError("db_timeout", `Query timeout: ${e}`) : e.includes("40001") || e.includes("40P01") || e.includes("deadlock") ? new exports.NeetruDbError("db_conflict", `Transaction conflict: ${e}`) : e.includes("42501") || e.toLowerCase().includes("permission denied") ? new exports.NeetruDbError("db_permission_denied", `Permission denied: ${e}`) : new exports.NeetruDbError("db_unavailable", `Database error: ${e}`);
|
|
307
396
|
}
|
|
308
|
-
var
|
|
309
|
-
|
|
397
|
+
var qt, M, $, We = S(() => {
|
|
398
|
+
I();
|
|
310
399
|
le();
|
|
311
|
-
|
|
400
|
+
qt = 0.8, M = 3e4, $ = class {
|
|
312
401
|
/**
|
|
313
402
|
* Lease atual. `null` no modo LAZY antes da 1ª query (o 1º lease é adiado —
|
|
314
403
|
* ver `_ensureInitialLease`). Após a 1ª aquisição, sempre não-nulo.
|
|
@@ -348,7 +437,7 @@ var Rt, E, $, Me = T(() => {
|
|
|
348
437
|
isolationLevel: t.isolationLevel
|
|
349
438
|
}) : await this._orm.transaction(e);
|
|
350
439
|
} catch (n) {
|
|
351
|
-
throw
|
|
440
|
+
throw Ut(n);
|
|
352
441
|
}
|
|
353
442
|
}
|
|
354
443
|
async close() {
|
|
@@ -366,13 +455,13 @@ var Rt, E, $, Me = T(() => {
|
|
|
366
455
|
* usa `MIN_RENEWAL_DELAY_MS` como fallback seguro.
|
|
367
456
|
*/
|
|
368
457
|
_calcRenewalDelayMs() {
|
|
369
|
-
if (!this._lease) return
|
|
458
|
+
if (!this._lease) return M;
|
|
370
459
|
let e = Date.parse(this._lease.expiresAt);
|
|
371
|
-
if (!Number.isFinite(e)) return
|
|
460
|
+
if (!Number.isFinite(e)) return M;
|
|
372
461
|
let t = this._deps.now(), n = e - t;
|
|
373
|
-
if (n <= 0) return
|
|
374
|
-
let o = n * (1 -
|
|
375
|
-
return Math.max(
|
|
462
|
+
if (n <= 0) return M;
|
|
463
|
+
let o = n * (1 - qt);
|
|
464
|
+
return Math.max(M, Math.round(o));
|
|
376
465
|
}
|
|
377
466
|
_scheduleRenewal() {
|
|
378
467
|
if (this._closed) return;
|
|
@@ -399,8 +488,8 @@ var Rt, E, $, Me = T(() => {
|
|
|
399
488
|
} catch (t) {
|
|
400
489
|
this._renewalTimer = setTimeout(() => {
|
|
401
490
|
this._doRenew();
|
|
402
|
-
},
|
|
403
|
-
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${
|
|
491
|
+
}, M), console.warn(
|
|
492
|
+
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${M}ms (o cert de cliente mTLS do lease expira ~15min ap\xF3s o \xFAltimo swap):`,
|
|
404
493
|
t instanceof Error ? t.message : t
|
|
405
494
|
);
|
|
406
495
|
return;
|
|
@@ -412,10 +501,13 @@ var Rt, E, $, Me = T(() => {
|
|
|
412
501
|
/**
|
|
413
502
|
* Recuperação REATIVA: chamado pelo wrapper resiliente (`resilient-pool.ts`)
|
|
414
503
|
* quando uma query plana falha num erro de ESTABELECIMENTO de conexão (cert
|
|
415
|
-
* TLS
|
|
416
|
-
* renovação que não disparou)
|
|
417
|
-
* do
|
|
418
|
-
*
|
|
504
|
+
* TLS de cliente mTLS do lease expirado no meio de uma request, ou timer de
|
|
505
|
+
* renovação que não disparou) ou de AUTENTICAÇÃO (senha rotacionada por
|
|
506
|
+
* `rotate-credentials` antes do próximo renew — SQLSTATE 28P01/28000, ou o
|
|
507
|
+
* 08P01+SASL do PgBouncer; também no `connect()` de uma transação). Emite um
|
|
508
|
+
* lease NOVO e faz o swap do pool interno do wrapper com a credencial e o
|
|
509
|
+
* certificado frescos (o pool velho é drenado com `end()`); o wrapper então
|
|
510
|
+
* re-tenta a operação 1x no pool novo.
|
|
419
511
|
*
|
|
420
512
|
* Vai DIRETO ao lease novo (sem `leaseId`) — a conexão falhou porque o
|
|
421
513
|
* certificado do lease atual está ruim, então `renew` (que depende do lease
|
|
@@ -507,7 +599,7 @@ var Rt, E, $, Me = T(() => {
|
|
|
507
599
|
* Exposto como método protegido para testes.
|
|
508
600
|
*/
|
|
509
601
|
async _swapPool(e) {
|
|
510
|
-
if (!
|
|
602
|
+
if (!Ft(this._lease, e)) {
|
|
511
603
|
this._lease = e;
|
|
512
604
|
return;
|
|
513
605
|
}
|
|
@@ -534,29 +626,29 @@ var Rt, E, $, Me = T(() => {
|
|
|
534
626
|
});
|
|
535
627
|
|
|
536
628
|
// src/db/sql/sql-client.ts
|
|
537
|
-
var
|
|
538
|
-
|
|
539
|
-
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () =>
|
|
540
|
-
DEFAULT_POOL_IDLE_MS: () =>
|
|
541
|
-
DEFAULT_POOL_MAX: () =>
|
|
542
|
-
LEASE_ENDPOINT: () =>
|
|
543
|
-
LEASE_RENEW_ENDPOINT: () =>
|
|
544
|
-
createDrizzleHandle: () =>
|
|
545
|
-
createHttpLeaseFetcher: () =>
|
|
546
|
-
createPgPool: () =>
|
|
547
|
-
createSqlClientFromConfig: () =>
|
|
548
|
-
createSqlClientWithDeps: () =>
|
|
629
|
+
var He = {};
|
|
630
|
+
me(He, {
|
|
631
|
+
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () => ze,
|
|
632
|
+
DEFAULT_POOL_IDLE_MS: () => Qe,
|
|
633
|
+
DEFAULT_POOL_MAX: () => Ve,
|
|
634
|
+
LEASE_ENDPOINT: () => $e,
|
|
635
|
+
LEASE_RENEW_ENDPOINT: () => Be,
|
|
636
|
+
createDrizzleHandle: () => jt,
|
|
637
|
+
createHttpLeaseFetcher: () => je,
|
|
638
|
+
createPgPool: () => Bt,
|
|
639
|
+
createSqlClientFromConfig: () => Qt,
|
|
640
|
+
createSqlClientWithDeps: () => Ke,
|
|
549
641
|
resolvePoolTuning: () => ue
|
|
550
642
|
});
|
|
551
|
-
function
|
|
643
|
+
function Wt(r) {
|
|
552
644
|
return r === "workspace" ? "staging" : r === "prod" ? "production" : "dev-local";
|
|
553
645
|
}
|
|
554
|
-
function
|
|
646
|
+
function je(r) {
|
|
555
647
|
return async (e) => {
|
|
556
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (W(), U)), n = !!e?.leaseId, o = n ?
|
|
648
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (W(), U)), n = !!e?.leaseId, o = n ? Be : $e, i;
|
|
557
649
|
n ? i = { leaseId: e.leaseId } : (i = {
|
|
558
650
|
productId: r.productId ?? "",
|
|
559
|
-
environment:
|
|
651
|
+
environment: Wt(r.env ?? "prod")
|
|
560
652
|
}, e?.database !== void 0 && (i.database = e.database));
|
|
561
653
|
let s;
|
|
562
654
|
try {
|
|
@@ -569,7 +661,7 @@ function Ae(r) {
|
|
|
569
661
|
retries: 2
|
|
570
662
|
});
|
|
571
663
|
} catch (a) {
|
|
572
|
-
let c = n ? "renovar" : "obter", l = a instanceof Error ? a.message : String(a), d = a instanceof
|
|
664
|
+
let c = n ? "renovar" : "obter", l = a instanceof Error ? a.message : String(a), d = a instanceof b ? a.reason : void 0;
|
|
573
665
|
if (d === "suspended" || d === "archived" || d === "revoked") {
|
|
574
666
|
let u = d === "suspended" ? "ambiente suspenso \u2014 a m\xE1quina que o servia parou; ser\xE1 religado quando o ambiente voltar" : d === "archived" ? "ambiente arquivado \u2014 este banco foi arquivado e n\xE3o aceita mais conex\xF5es" : "lease revogado \u2014 as credenciais deste ambiente foram revogadas pelo Core";
|
|
575
667
|
throw new exports.NeetruDbError(
|
|
@@ -582,10 +674,10 @@ function Ae(r) {
|
|
|
582
674
|
`Falha ao ${c} lease SQL: ${l}`
|
|
583
675
|
);
|
|
584
676
|
}
|
|
585
|
-
return
|
|
677
|
+
return $t(s);
|
|
586
678
|
};
|
|
587
679
|
}
|
|
588
|
-
function
|
|
680
|
+
function $t(r) {
|
|
589
681
|
if (!r || typeof r != "object")
|
|
590
682
|
throw new exports.NeetruDbError("db_unavailable", "Resposta de lease inv\xE1lida (n\xE3o \xE9 objeto).");
|
|
591
683
|
let t = r.lease;
|
|
@@ -627,12 +719,12 @@ function ue() {
|
|
|
627
719
|
return {
|
|
628
720
|
// Teto 100: bem abaixo do `max_client_conn = 500` do PgBouncer, com folga
|
|
629
721
|
// para múltiplas instâncias do app dividirem o mesmo front-door.
|
|
630
|
-
max: de("NEETRU_DB_POOL_MAX",
|
|
722
|
+
max: de("NEETRU_DB_POOL_MAX", Ve, 1, 100),
|
|
631
723
|
// Teto 15min = janela do cert; piso 1s.
|
|
632
|
-
idleTimeoutMillis: de("NEETRU_DB_POOL_IDLE_MS",
|
|
724
|
+
idleTimeoutMillis: de("NEETRU_DB_POOL_IDLE_MS", Qe, 1e3, 9e5),
|
|
633
725
|
connectionTimeoutMillis: de(
|
|
634
726
|
"NEETRU_DB_POOL_CONNECT_TIMEOUT_MS",
|
|
635
|
-
|
|
727
|
+
ze,
|
|
636
728
|
1e3,
|
|
637
729
|
12e4
|
|
638
730
|
),
|
|
@@ -640,7 +732,7 @@ function ue() {
|
|
|
640
732
|
keepAliveInitialDelayMillis: 1e4
|
|
641
733
|
};
|
|
642
734
|
}
|
|
643
|
-
async function
|
|
735
|
+
async function Bt(r) {
|
|
644
736
|
let { Pool: e } = await import(
|
|
645
737
|
/* webpackIgnore: true */
|
|
646
738
|
'pg'
|
|
@@ -660,14 +752,14 @@ async function It(r) {
|
|
|
660
752
|
...ue()
|
|
661
753
|
});
|
|
662
754
|
}
|
|
663
|
-
async function
|
|
755
|
+
async function jt(r, e) {
|
|
664
756
|
let { drizzle: t } = await import(
|
|
665
757
|
/* webpackIgnore: true */
|
|
666
758
|
'drizzle-orm/node-postgres'
|
|
667
759
|
);
|
|
668
760
|
return t(r, { schema: e });
|
|
669
761
|
}
|
|
670
|
-
function
|
|
762
|
+
function Vt(r, e) {
|
|
671
763
|
let t = null, n = null, o = () => t ? Promise.resolve(t) : (n || (n = e().then((s) => (t = s, n = null, s)).catch((s) => {
|
|
672
764
|
throw n = null, s;
|
|
673
765
|
})), n), i = {
|
|
@@ -683,22 +775,22 @@ function Lt(r, e) {
|
|
|
683
775
|
};
|
|
684
776
|
return r && Object.setPrototypeOf(i, r), i;
|
|
685
777
|
}
|
|
686
|
-
async function
|
|
778
|
+
async function Ke(r, e) {
|
|
687
779
|
let t = e?.database !== void 0 ? { ...r, database: e.database } : r;
|
|
688
780
|
if (t.lazy) {
|
|
689
|
-
let c = null, l =
|
|
781
|
+
let c = null, l = Vt(t.poolPrototype ?? null, () => {
|
|
690
782
|
if (!c)
|
|
691
783
|
throw new exports.NeetruDbError("db_unavailable", "SQL client n\xE3o inicializado (manager ausente).");
|
|
692
784
|
return c._ensureInitialLease();
|
|
693
785
|
}), d = ce(l), u;
|
|
694
786
|
try {
|
|
695
787
|
u = t.createDrizzle(d, t.schema);
|
|
696
|
-
} catch (
|
|
697
|
-
let
|
|
698
|
-
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${
|
|
788
|
+
} catch (h) {
|
|
789
|
+
let _ = h instanceof Error ? h.message : String(h);
|
|
790
|
+
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${_}`);
|
|
699
791
|
}
|
|
700
|
-
let
|
|
701
|
-
return c =
|
|
792
|
+
let p = new $(null, d, u, t);
|
|
793
|
+
return c = p, d.__neetruSetReLease(() => p._reactiveReLease()), p;
|
|
702
794
|
}
|
|
703
795
|
let n;
|
|
704
796
|
try {
|
|
@@ -727,8 +819,8 @@ async function Ue(r, e) {
|
|
|
727
819
|
let a = new $(n, i, s, t);
|
|
728
820
|
return i.__neetruSetReLease(() => a._reactiveReLease()), a;
|
|
729
821
|
}
|
|
730
|
-
async function
|
|
731
|
-
let n =
|
|
822
|
+
async function Qt(r, e, t) {
|
|
823
|
+
let n = je(r), o, i;
|
|
732
824
|
try {
|
|
733
825
|
o = await import(
|
|
734
826
|
/* webpackIgnore: true */
|
|
@@ -744,7 +836,7 @@ async function Mt(r, e, t) {
|
|
|
744
836
|
);
|
|
745
837
|
}
|
|
746
838
|
let s = o.Pool ?? o.default?.Pool;
|
|
747
|
-
return
|
|
839
|
+
return Ke(
|
|
748
840
|
{
|
|
749
841
|
fetchLease: n,
|
|
750
842
|
createPool: (l) => {
|
|
@@ -779,24 +871,24 @@ async function Mt(r, e, t) {
|
|
|
779
871
|
t
|
|
780
872
|
);
|
|
781
873
|
}
|
|
782
|
-
var
|
|
783
|
-
|
|
874
|
+
var $e, Be, Ve, Qe, ze, Ge = S(() => {
|
|
875
|
+
I();
|
|
784
876
|
x();
|
|
785
|
-
|
|
877
|
+
We();
|
|
786
878
|
le();
|
|
787
|
-
|
|
788
|
-
|
|
879
|
+
$e = "/api/sdk/v1/db/lease", Be = "/api/sdk/v1/db/lease/renew";
|
|
880
|
+
Ve = 10, Qe = 6e4, ze = 1e4;
|
|
789
881
|
});
|
|
790
882
|
|
|
791
883
|
// src/db/client-db.ts
|
|
792
|
-
|
|
884
|
+
I();
|
|
793
885
|
|
|
794
886
|
// src/db/offline/query-engine.ts
|
|
795
|
-
function
|
|
887
|
+
function _e(r) {
|
|
796
888
|
return r == null ? 0 : typeof r == "number" ? 1 : typeof r == "string" ? 2 : typeof r == "boolean" ? 3 : 4;
|
|
797
889
|
}
|
|
798
890
|
function A(r, e) {
|
|
799
|
-
let t =
|
|
891
|
+
let t = _e(r), n = _e(e);
|
|
800
892
|
if (t !== n) return t - n;
|
|
801
893
|
if (r == null) return 0;
|
|
802
894
|
if (typeof r == "number" && typeof e == "number") return r - e;
|
|
@@ -814,7 +906,7 @@ function Z(r, e) {
|
|
|
814
906
|
}
|
|
815
907
|
return n;
|
|
816
908
|
}
|
|
817
|
-
function
|
|
909
|
+
function et(r, e) {
|
|
818
910
|
let t = Z(r, e.field);
|
|
819
911
|
if (t === void 0) return false;
|
|
820
912
|
let { op: n, value: o } = e;
|
|
@@ -845,10 +937,10 @@ function ze(r, e) {
|
|
|
845
937
|
);
|
|
846
938
|
}
|
|
847
939
|
}
|
|
848
|
-
function
|
|
849
|
-
return e.every((t) =>
|
|
940
|
+
function tt(r, e) {
|
|
941
|
+
return e.every((t) => et(r, t));
|
|
850
942
|
}
|
|
851
|
-
function
|
|
943
|
+
function nt(r) {
|
|
852
944
|
return (e, t) => {
|
|
853
945
|
if (r) {
|
|
854
946
|
let n = Z(e.data, r.field), o = Z(t.data, r.field), i = A(n, o);
|
|
@@ -858,11 +950,11 @@ function He(r) {
|
|
|
858
950
|
return e.id < t.id ? -1 : e.id > t.id ? 1 : 0;
|
|
859
951
|
};
|
|
860
952
|
}
|
|
861
|
-
function
|
|
953
|
+
function rt(r, e) {
|
|
862
954
|
let t = r.findIndex((n) => n.id === e.docId);
|
|
863
955
|
return t === -1 ? r : e.type === "startAfter" ? r.slice(t + 1) : r.slice(0, t);
|
|
864
956
|
}
|
|
865
|
-
var
|
|
957
|
+
var P = class r {
|
|
866
958
|
/**
|
|
867
959
|
* Avalia um `QueryDescriptor` contra um array de `OfflineDoc`.
|
|
868
960
|
*
|
|
@@ -876,9 +968,9 @@ var C = class r {
|
|
|
876
968
|
evaluate(e, t) {
|
|
877
969
|
let n = e.filter((d) => !d.meta.deleted), o = t.where ?? [];
|
|
878
970
|
o.length > 0 && (n = n.filter(
|
|
879
|
-
(d) =>
|
|
971
|
+
(d) => tt(d.data, o)
|
|
880
972
|
));
|
|
881
|
-
let i =
|
|
973
|
+
let i = nt(t.orderBy), s = [...n].sort(i), a = t.cursor ? rt(s, t.cursor) : s, c = Math.min(t.limit ?? 20, 500);
|
|
882
974
|
return {
|
|
883
975
|
docs: a.slice(0, c).map((d) => ({ id: d.id, data: d.data })),
|
|
884
976
|
// `incomplete` é sempre true aqui — o QueryEngine não sabe se o cache
|
|
@@ -898,7 +990,7 @@ var C = class r {
|
|
|
898
990
|
};
|
|
899
991
|
|
|
900
992
|
// src/db/offline/local-store.ts
|
|
901
|
-
var
|
|
993
|
+
var it = 1, T = "documents", R = "mutations", N = "query_cache", j = "sync_meta", E = "conflict_log", V = class {
|
|
902
994
|
db = null;
|
|
903
995
|
dbName;
|
|
904
996
|
constructor(e) {
|
|
@@ -910,22 +1002,22 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
910
1002
|
* Idempotente — chamadas subsequentes são no-ops se o banco já está aberto.
|
|
911
1003
|
*/
|
|
912
1004
|
async open() {
|
|
913
|
-
this.db === null && (this.db = await idb.openDB(this.dbName,
|
|
1005
|
+
this.db === null && (this.db = await idb.openDB(this.dbName, it, {
|
|
914
1006
|
upgrade(e) {
|
|
915
|
-
if (!e.objectStoreNames.contains(
|
|
916
|
-
let t = e.createObjectStore(
|
|
1007
|
+
if (!e.objectStoreNames.contains(T)) {
|
|
1008
|
+
let t = e.createObjectStore(T, {
|
|
917
1009
|
keyPath: ["collection", "id"]
|
|
918
1010
|
});
|
|
919
1011
|
t.createIndex("by_collection", "collection"), t.createIndex("by_collection_state", ["collection", "meta.state"]), t.createIndex("by_updatedAtServer", "meta.updatedAtServer");
|
|
920
1012
|
}
|
|
921
|
-
if (!e.objectStoreNames.contains(
|
|
922
|
-
let t = e.createObjectStore(
|
|
1013
|
+
if (!e.objectStoreNames.contains(R)) {
|
|
1014
|
+
let t = e.createObjectStore(R, {
|
|
923
1015
|
keyPath: "mutationId"
|
|
924
1016
|
});
|
|
925
1017
|
t.createIndex("by_seq", "seq"), t.createIndex("by_status", "status"), t.createIndex("by_doc", ["collection", "docId"]), t.createIndex("by_batch", "batchId");
|
|
926
1018
|
}
|
|
927
|
-
if (e.objectStoreNames.contains(N) || e.createObjectStore(N, { keyPath: "queryHash" }), e.objectStoreNames.contains(
|
|
928
|
-
let t = e.createObjectStore(
|
|
1019
|
+
if (e.objectStoreNames.contains(N) || e.createObjectStore(N, { keyPath: "queryHash" }), e.objectStoreNames.contains(j) || e.createObjectStore(j, { keyPath: "key" }), !e.objectStoreNames.contains(E)) {
|
|
1020
|
+
let t = e.createObjectStore(E, {
|
|
929
1021
|
keyPath: "id",
|
|
930
1022
|
autoIncrement: true
|
|
931
1023
|
});
|
|
@@ -967,14 +1059,14 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
967
1059
|
* Retorna tombstones (deleted: true) — o chamador decide se deve mostrá-los.
|
|
968
1060
|
*/
|
|
969
1061
|
async getDoc(e, t) {
|
|
970
|
-
return await this.assertOpen().get(
|
|
1062
|
+
return await this.assertOpen().get(T, [e, t]) ?? null;
|
|
971
1063
|
}
|
|
972
1064
|
/**
|
|
973
1065
|
* Insere ou atualiza um documento na store `documents`.
|
|
974
1066
|
* O documento é identificado pelo keyPath composto `[collection, id]`.
|
|
975
1067
|
*/
|
|
976
1068
|
async putDoc(e) {
|
|
977
|
-
await this.assertOpen().put(
|
|
1069
|
+
await this.assertOpen().put(T, e);
|
|
978
1070
|
}
|
|
979
1071
|
/**
|
|
980
1072
|
* Marca um documento como tombstone (`deleted: true`).
|
|
@@ -982,8 +1074,8 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
982
1074
|
* No-op se o documento não existir.
|
|
983
1075
|
*/
|
|
984
1076
|
async deleteDoc(e, t) {
|
|
985
|
-
let n = this.assertOpen(), o = await n.get(
|
|
986
|
-
o && await n.put(
|
|
1077
|
+
let n = this.assertOpen(), o = await n.get(T, [e, t]);
|
|
1078
|
+
o && await n.put(T, {
|
|
987
1079
|
...o,
|
|
988
1080
|
meta: {
|
|
989
1081
|
...o.meta,
|
|
@@ -999,8 +1091,8 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
999
1091
|
* e delega a filtragem/ordenação/paginação ao QueryEngine (que opera em memória).
|
|
1000
1092
|
*/
|
|
1001
1093
|
async listDocs(e, t) {
|
|
1002
|
-
let o = await this.assertOpen().getAllFromIndex(
|
|
1003
|
-
return
|
|
1094
|
+
let o = await this.assertOpen().getAllFromIndex(T, "by_collection", e);
|
|
1095
|
+
return P.evaluate(o, t);
|
|
1004
1096
|
}
|
|
1005
1097
|
// ─── Sync meta (key-value) ──────────────────────────────────────────────────
|
|
1006
1098
|
/**
|
|
@@ -1011,14 +1103,14 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1011
1103
|
* 'lastFullResyncAt', 'leaderTabId'.
|
|
1012
1104
|
*/
|
|
1013
1105
|
async getMeta(e) {
|
|
1014
|
-
let n = await this.assertOpen().get(
|
|
1106
|
+
let n = await this.assertOpen().get(j, e);
|
|
1015
1107
|
return n ? n.value : null;
|
|
1016
1108
|
}
|
|
1017
1109
|
/**
|
|
1018
1110
|
* Armazena ou atualiza um valor na store `sync_meta`.
|
|
1019
1111
|
*/
|
|
1020
1112
|
async setMeta(e, t) {
|
|
1021
|
-
await this.assertOpen().put(
|
|
1113
|
+
await this.assertOpen().put(j, { key: e, value: t });
|
|
1022
1114
|
}
|
|
1023
1115
|
// ─── Conflict log ───────────────────────────────────────────────────────────
|
|
1024
1116
|
/**
|
|
@@ -1026,7 +1118,7 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1026
1118
|
* O `id` é autoIncrement — não deve ser fornecido pelo chamador.
|
|
1027
1119
|
*/
|
|
1028
1120
|
async appendConflict(e) {
|
|
1029
|
-
let n = await this.assertOpen().add(
|
|
1121
|
+
let n = await this.assertOpen().add(E, e);
|
|
1030
1122
|
return { ...e, id: n };
|
|
1031
1123
|
}
|
|
1032
1124
|
/**
|
|
@@ -1034,15 +1126,15 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1034
1126
|
* Filtra por `delivered` se a opção for fornecida.
|
|
1035
1127
|
*/
|
|
1036
1128
|
async listConflicts(e) {
|
|
1037
|
-
let n = await this.assertOpen().getAll(
|
|
1129
|
+
let n = await this.assertOpen().getAll(E);
|
|
1038
1130
|
return e?.delivered !== void 0 ? n.filter((o) => o.delivered === e.delivered) : n;
|
|
1039
1131
|
}
|
|
1040
1132
|
/**
|
|
1041
1133
|
* Marca um registro do `conflict_log` como entregue ao produto.
|
|
1042
1134
|
*/
|
|
1043
1135
|
async markConflictDelivered(e) {
|
|
1044
|
-
let t = this.assertOpen(), n = await t.get(
|
|
1045
|
-
n && await t.put(
|
|
1136
|
+
let t = this.assertOpen(), n = await t.get(E, e);
|
|
1137
|
+
n && await t.put(E, { ...n, delivered: true });
|
|
1046
1138
|
}
|
|
1047
1139
|
// ─── Mutations (acessores usados pelo MutationQueue) ────────────────────────
|
|
1048
1140
|
/**
|
|
@@ -1050,13 +1142,13 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1050
1142
|
* Key: `mutationId`.
|
|
1051
1143
|
*/
|
|
1052
1144
|
async putMutation(e) {
|
|
1053
|
-
await this.assertOpen().put(
|
|
1145
|
+
await this.assertOpen().put(R, e);
|
|
1054
1146
|
}
|
|
1055
1147
|
/**
|
|
1056
1148
|
* Retorna uma mutação pelo `mutationId`, ou `null` se não existir.
|
|
1057
1149
|
*/
|
|
1058
1150
|
async getMutation(e) {
|
|
1059
|
-
return await this.assertOpen().get(
|
|
1151
|
+
return await this.assertOpen().get(R, e) ?? null;
|
|
1060
1152
|
}
|
|
1061
1153
|
/**
|
|
1062
1154
|
* Lista mutações com filtros opcionais.
|
|
@@ -1064,14 +1156,14 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1064
1156
|
*/
|
|
1065
1157
|
async listMutations(e) {
|
|
1066
1158
|
let t = this.assertOpen(), n;
|
|
1067
|
-
return e?.status !== void 0 ? n = await t.getAllFromIndex(
|
|
1159
|
+
return e?.status !== void 0 ? n = await t.getAllFromIndex(R, "by_status", e.status) : e?.collection !== void 0 && e.docId !== void 0 ? n = await t.getAllFromIndex(R, "by_doc", [e.collection, e.docId]) : n = await t.getAllFromIndex(R, "by_seq"), n.sort((o, i) => o.seq - i.seq);
|
|
1068
1160
|
}
|
|
1069
1161
|
/**
|
|
1070
1162
|
* Remove uma mutação pelo `mutationId`.
|
|
1071
1163
|
* No-op se não existir.
|
|
1072
1164
|
*/
|
|
1073
1165
|
async deleteMutation(e) {
|
|
1074
|
-
await this.assertOpen().delete(
|
|
1166
|
+
await this.assertOpen().delete(R, e);
|
|
1075
1167
|
}
|
|
1076
1168
|
// ─── Collection discovery ────────────────────────────────────────────────────
|
|
1077
1169
|
/**
|
|
@@ -1084,7 +1176,7 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1084
1176
|
* coletar os valores únicos de forma eficiente (sem carregar os docs completos).
|
|
1085
1177
|
*/
|
|
1086
1178
|
async listCollections() {
|
|
1087
|
-
let t = await this.assertOpen().getAllFromIndex(
|
|
1179
|
+
let t = await this.assertOpen().getAllFromIndex(T, "by_collection"), n = /* @__PURE__ */ new Set();
|
|
1088
1180
|
for (let o of t)
|
|
1089
1181
|
n.add(o.collection);
|
|
1090
1182
|
return Array.from(n);
|
|
@@ -1111,7 +1203,7 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1111
1203
|
};
|
|
1112
1204
|
|
|
1113
1205
|
// src/db/offline/memory-store.ts
|
|
1114
|
-
var
|
|
1206
|
+
var Q = class {
|
|
1115
1207
|
// store `documents` — key: `${collection}\0${id}`
|
|
1116
1208
|
_docs = /* @__PURE__ */ new Map();
|
|
1117
1209
|
// store `mutations` — key: mutationId
|
|
@@ -1159,7 +1251,7 @@ var V = class {
|
|
|
1159
1251
|
let n = Array.from(this._docs.values()).filter(
|
|
1160
1252
|
(o) => o.collection === e
|
|
1161
1253
|
);
|
|
1162
|
-
return
|
|
1254
|
+
return P.evaluate(n, t);
|
|
1163
1255
|
}
|
|
1164
1256
|
// ─── Sync meta ──────────────────────────────────────────────────────────────
|
|
1165
1257
|
async getMeta(e) {
|
|
@@ -1222,7 +1314,7 @@ function ee() {
|
|
|
1222
1314
|
return (r === "x" ? e : e & 3 | 8).toString(16);
|
|
1223
1315
|
});
|
|
1224
1316
|
}
|
|
1225
|
-
var
|
|
1317
|
+
var z = class {
|
|
1226
1318
|
store;
|
|
1227
1319
|
/**
|
|
1228
1320
|
* Ponteiro de sequência local.
|
|
@@ -1320,11 +1412,11 @@ var Q = class {
|
|
|
1320
1412
|
async enqueue(e) {
|
|
1321
1413
|
let { collection: t, op: n, payload: o, baseVersion: i, batchId: s, upsert: a } = e, c = e.docId ?? (n === "add" ? ee() : "");
|
|
1322
1414
|
await this.syncSeq();
|
|
1323
|
-
let d = (await this.store.listMutations({ collection: t, docId: c })).filter((
|
|
1415
|
+
let d = (await this.store.listMutations({ collection: t, docId: c })).filter((h) => h.status === "queued").sort((h, _) => _.seq - h.seq)[0];
|
|
1324
1416
|
if (d !== void 0) {
|
|
1325
|
-
let
|
|
1326
|
-
if (
|
|
1327
|
-
if ("removed" in
|
|
1417
|
+
let h = this.tryCoalesce(d, n, o, s, a);
|
|
1418
|
+
if (h !== null) {
|
|
1419
|
+
if ("removed" in h)
|
|
1328
1420
|
return await this.store.deleteMutation(d.mutationId), {
|
|
1329
1421
|
seq: d.seq,
|
|
1330
1422
|
mutationId: ee(),
|
|
@@ -1339,11 +1431,11 @@ var Q = class {
|
|
|
1339
1431
|
status: "queued",
|
|
1340
1432
|
batchId: s
|
|
1341
1433
|
};
|
|
1342
|
-
let { coalesced:
|
|
1343
|
-
return await this.store.putMutation(
|
|
1434
|
+
let { coalesced: _ } = h;
|
|
1435
|
+
return await this.store.putMutation(_), _;
|
|
1344
1436
|
}
|
|
1345
1437
|
}
|
|
1346
|
-
let
|
|
1438
|
+
let p = {
|
|
1347
1439
|
seq: this.nextSeq(),
|
|
1348
1440
|
mutationId: ee(),
|
|
1349
1441
|
collection: t,
|
|
@@ -1359,7 +1451,7 @@ var Q = class {
|
|
|
1359
1451
|
// bug_0273e4dd: só persiste o flag quando verdadeiro (mantém o doc limpo).
|
|
1360
1452
|
...a ? { upsert: true } : {}
|
|
1361
1453
|
};
|
|
1362
|
-
return await this.store.putMutation(
|
|
1454
|
+
return await this.store.putMutation(p), p;
|
|
1363
1455
|
}
|
|
1364
1456
|
// ─── Leitura da fila ─────────────────────────────────────────────────────────
|
|
1365
1457
|
/**
|
|
@@ -1459,54 +1551,54 @@ var Q = class {
|
|
|
1459
1551
|
|
|
1460
1552
|
// src/db/field-value.ts
|
|
1461
1553
|
var te = "__neetru__";
|
|
1462
|
-
function
|
|
1554
|
+
function st() {
|
|
1463
1555
|
return { __neetru__: "serverTimestamp" };
|
|
1464
1556
|
}
|
|
1465
|
-
function
|
|
1557
|
+
function at(r) {
|
|
1466
1558
|
if (typeof r != "number" || !Number.isFinite(r))
|
|
1467
1559
|
throw new TypeError(
|
|
1468
1560
|
`increment(n): n deve ser um n\xFAmero finito (recebido: ${String(r)}).`
|
|
1469
1561
|
);
|
|
1470
1562
|
return { __neetru__: "increment", operand: r };
|
|
1471
1563
|
}
|
|
1472
|
-
function
|
|
1564
|
+
function D(r) {
|
|
1473
1565
|
return typeof r == "object" && r !== null && te in r && typeof r[te] == "string";
|
|
1474
1566
|
}
|
|
1475
|
-
function _e(r) {
|
|
1476
|
-
return R(r) && r.__neetru__ === "serverTimestamp";
|
|
1477
|
-
}
|
|
1478
1567
|
function ye(r) {
|
|
1479
|
-
return
|
|
1568
|
+
return D(r) && r.__neetru__ === "serverTimestamp";
|
|
1480
1569
|
}
|
|
1481
|
-
function
|
|
1482
|
-
return
|
|
1570
|
+
function ge(r) {
|
|
1571
|
+
return D(r) && r.__neetru__ === "increment" && typeof r.operand == "number" && Number.isFinite(r.operand);
|
|
1572
|
+
}
|
|
1573
|
+
function ct(r, e) {
|
|
1574
|
+
return ye(r) ? Date.now() : ge(r) ? (typeof e == "number" && Number.isFinite(e) ? e : 0) + r.operand : r;
|
|
1483
1575
|
}
|
|
1484
|
-
function
|
|
1576
|
+
function k(r, e) {
|
|
1485
1577
|
let t = false, n = {};
|
|
1486
1578
|
for (let [o, i] of Object.entries(r))
|
|
1487
|
-
|
|
1579
|
+
D(i) ? (t = true, n[o] = ct(i, e?.[o])) : n[o] = i;
|
|
1488
1580
|
return t ? n : r;
|
|
1489
1581
|
}
|
|
1490
1582
|
function ne(r) {
|
|
1491
1583
|
for (let e of Object.values(r))
|
|
1492
|
-
if (
|
|
1584
|
+
if (D(e)) return true;
|
|
1493
1585
|
return false;
|
|
1494
1586
|
}
|
|
1495
1587
|
|
|
1496
1588
|
// src/db/offline/sync-engine.ts
|
|
1497
|
-
var
|
|
1498
|
-
function
|
|
1589
|
+
var lt = 8, dt = 1e3, ut = 300 * 1e3, re = "truncatedCollections";
|
|
1590
|
+
function pt(r) {
|
|
1499
1591
|
return r !== null && ne(r);
|
|
1500
1592
|
}
|
|
1501
|
-
function
|
|
1593
|
+
function ft(r) {
|
|
1502
1594
|
if (r === null) return null;
|
|
1503
1595
|
if (!ne(r)) return r;
|
|
1504
1596
|
let e = {};
|
|
1505
1597
|
for (let [t, n] of Object.entries(r))
|
|
1506
|
-
|
|
1598
|
+
D(n) || (e[t] = n);
|
|
1507
1599
|
return e;
|
|
1508
1600
|
}
|
|
1509
|
-
var
|
|
1601
|
+
var K = class {
|
|
1510
1602
|
_store;
|
|
1511
1603
|
_queue;
|
|
1512
1604
|
_resolver;
|
|
@@ -1700,13 +1792,13 @@ var z = class {
|
|
|
1700
1792
|
}), s.push({ type: "removed", collection: e, doc: { id: t, data: a.data } }));
|
|
1701
1793
|
return;
|
|
1702
1794
|
}
|
|
1703
|
-
let l =
|
|
1795
|
+
let l = ft(i.payload) ?? a?.data ?? {}, d = {
|
|
1704
1796
|
collection: e,
|
|
1705
1797
|
id: t,
|
|
1706
1798
|
data: i.op === "update" ? { ...a?.data ?? {}, ...l } : (
|
|
1707
1799
|
// add/set: se o payload tinha sentinels, o cache otimista (existing.data)
|
|
1708
1800
|
// é a fonte de verdade até o pull; senão usa o payload literal.
|
|
1709
|
-
|
|
1801
|
+
pt(i.payload) ? { ...a?.data ?? {}, ...l } : l
|
|
1710
1802
|
),
|
|
1711
1803
|
meta: {
|
|
1712
1804
|
serverVersion: n,
|
|
@@ -1859,7 +1951,7 @@ var z = class {
|
|
|
1859
1951
|
* pending do doc no cache + registra `lastError`/dispara `onError`.
|
|
1860
1952
|
*/
|
|
1861
1953
|
async _retryOrDeadLetter(e, t, n) {
|
|
1862
|
-
if (e.attempts + 1 <
|
|
1954
|
+
if (e.attempts + 1 < lt) {
|
|
1863
1955
|
await this._queue.markRetry(e.mutationId, t);
|
|
1864
1956
|
return;
|
|
1865
1957
|
}
|
|
@@ -1900,7 +1992,7 @@ var z = class {
|
|
|
1900
1992
|
}
|
|
1901
1993
|
/** Backoff exponencial keyed em `attempts`, com teto `BACKOFF_CAP_MS`. */
|
|
1902
1994
|
_backoffDelayMs(e) {
|
|
1903
|
-
return Math.min(
|
|
1995
|
+
return Math.min(dt * 2 ** (e - 1), ut);
|
|
1904
1996
|
}
|
|
1905
1997
|
// ─── FASE 2 — pull (reconciliar o cache) ─────────────────────────────────────
|
|
1906
1998
|
/**
|
|
@@ -2152,10 +2244,10 @@ var z = class {
|
|
|
2152
2244
|
};
|
|
2153
2245
|
|
|
2154
2246
|
// src/db/offline/conflict-resolver.ts
|
|
2155
|
-
function
|
|
2247
|
+
function be(r, e) {
|
|
2156
2248
|
return e === null ? false : r === null ? true : e > r;
|
|
2157
2249
|
}
|
|
2158
|
-
var
|
|
2250
|
+
var H = class r {
|
|
2159
2251
|
/**
|
|
2160
2252
|
* Resolve um conflito com base na operação da mutação (auto-dispatch).
|
|
2161
2253
|
*
|
|
@@ -2189,7 +2281,7 @@ var K = class r {
|
|
|
2189
2281
|
resolvedData: t.data,
|
|
2190
2282
|
conflictRecord: null
|
|
2191
2283
|
};
|
|
2192
|
-
if (!
|
|
2284
|
+
if (!be(
|
|
2193
2285
|
e.meta.updatedAtServer,
|
|
2194
2286
|
t.meta.updatedAtServer
|
|
2195
2287
|
))
|
|
@@ -2236,7 +2328,7 @@ var K = class r {
|
|
|
2236
2328
|
let i = t.data;
|
|
2237
2329
|
if (e === null)
|
|
2238
2330
|
return { outcome: "no_conflict", resolvedData: { ...i, ...n.payload ?? {} }, conflictRecord: null };
|
|
2239
|
-
if (!
|
|
2331
|
+
if (!be(
|
|
2240
2332
|
e.meta.updatedAtServer,
|
|
2241
2333
|
t.meta.updatedAtServer
|
|
2242
2334
|
))
|
|
@@ -2314,10 +2406,10 @@ var K = class r {
|
|
|
2314
2406
|
};
|
|
2315
2407
|
|
|
2316
2408
|
// src/db/offline/change-bus.ts
|
|
2317
|
-
function
|
|
2409
|
+
function ht(r) {
|
|
2318
2410
|
return `${r.collection}::${r.doc.id}`;
|
|
2319
2411
|
}
|
|
2320
|
-
var
|
|
2412
|
+
var G = class {
|
|
2321
2413
|
/** Listeners filtrados por coleção. */
|
|
2322
2414
|
_collectionListeners = /* @__PURE__ */ new Map();
|
|
2323
2415
|
/** Listeners globais (todas as coleções). */
|
|
@@ -2362,7 +2454,7 @@ var H = class {
|
|
|
2362
2454
|
if (e.length === 0) return;
|
|
2363
2455
|
let t = /* @__PURE__ */ new Map();
|
|
2364
2456
|
for (let i of e)
|
|
2365
|
-
t.set(
|
|
2457
|
+
t.set(ht(i), i);
|
|
2366
2458
|
let n = Array.from(t.values()), o = /* @__PURE__ */ new Map();
|
|
2367
2459
|
for (let i of n)
|
|
2368
2460
|
o.has(i.collection) || o.set(i.collection, []), o.get(i.collection).push(i);
|
|
@@ -2401,7 +2493,7 @@ var H = class {
|
|
|
2401
2493
|
};
|
|
2402
2494
|
|
|
2403
2495
|
// src/db/offline/connectivity-monitor.ts
|
|
2404
|
-
var
|
|
2496
|
+
var X = class {
|
|
2405
2497
|
_state;
|
|
2406
2498
|
_listeners = /* @__PURE__ */ new Set();
|
|
2407
2499
|
_nav;
|
|
@@ -2680,26 +2772,26 @@ var q = class {
|
|
|
2680
2772
|
};
|
|
2681
2773
|
|
|
2682
2774
|
// src/db/collection-ref.ts
|
|
2683
|
-
|
|
2684
|
-
function
|
|
2775
|
+
I();
|
|
2776
|
+
function mt() {
|
|
2685
2777
|
return typeof indexedDB > "u" || typeof window > "u";
|
|
2686
2778
|
}
|
|
2687
|
-
var
|
|
2688
|
-
function
|
|
2689
|
-
if (!
|
|
2779
|
+
var ve = /^[a-z0-9][a-z0-9_-]{0,62}$/;
|
|
2780
|
+
function _t(r) {
|
|
2781
|
+
if (!ve.test(r))
|
|
2690
2782
|
throw new exports.NeetruDbError(
|
|
2691
2783
|
"db_invalid_query",
|
|
2692
|
-
`Nome de cole\xE7\xE3o inv\xE1lido: "${r}". Deve seguir o padr\xE3o ${
|
|
2784
|
+
`Nome de cole\xE7\xE3o inv\xE1lido: "${r}". Deve seguir o padr\xE3o ${ve}.`
|
|
2693
2785
|
);
|
|
2694
2786
|
}
|
|
2695
|
-
function
|
|
2787
|
+
function L(r, e = "id") {
|
|
2696
2788
|
if (!r || typeof r != "string" || r.trim() === "")
|
|
2697
2789
|
throw new exports.NeetruDbError(
|
|
2698
2790
|
"db_invalid_query",
|
|
2699
2791
|
`${e} \xE9 obrigat\xF3rio e deve ser uma string n\xE3o-vazia.`
|
|
2700
2792
|
);
|
|
2701
2793
|
}
|
|
2702
|
-
function
|
|
2794
|
+
function yt(r) {
|
|
2703
2795
|
let e = {};
|
|
2704
2796
|
if (r?.where && (e.where = r.where.map((t) => ({
|
|
2705
2797
|
field: t.field,
|
|
@@ -2716,7 +2808,7 @@ function lt(r) {
|
|
|
2716
2808
|
}
|
|
2717
2809
|
return e;
|
|
2718
2810
|
}
|
|
2719
|
-
function
|
|
2811
|
+
function gt(r, e) {
|
|
2720
2812
|
if (r.length < e) return null;
|
|
2721
2813
|
let t = r[r.length - 1];
|
|
2722
2814
|
return t ? btoa(JSON.stringify({ type: "startAfter", docId: t.id })) : null;
|
|
@@ -2763,11 +2855,11 @@ var oe = class {
|
|
|
2763
2855
|
}
|
|
2764
2856
|
/** Constrói um DbListResult para a coleção com query opcional. */
|
|
2765
2857
|
async _buildListResult(e) {
|
|
2766
|
-
let t =
|
|
2858
|
+
let t = yt(e), n = t.limit ?? 20, o = await this._store.listDocs(this._collection, t), i = await this._hasPendingWrites(), s = this._isOnline(), a = this._engine.getSyncState(), c = gt(o.docs, n), l = await this._store.getMeta(re), d = Array.isArray(l) && l.includes(this._collection), u = !s || a.status === "offline" || d, p = i || a.lastSyncedAt === null || u;
|
|
2767
2859
|
return {
|
|
2768
|
-
docs: o.docs.map((
|
|
2860
|
+
docs: o.docs.map((h) => ({ id: h.id, data: h.data })),
|
|
2769
2861
|
nextCursor: c,
|
|
2770
|
-
fromCache:
|
|
2862
|
+
fromCache: p,
|
|
2771
2863
|
stale: u,
|
|
2772
2864
|
hasPendingWrites: i,
|
|
2773
2865
|
changes: [],
|
|
@@ -2776,13 +2868,13 @@ var oe = class {
|
|
|
2776
2868
|
}
|
|
2777
2869
|
// ── CRUD ────────────────────────────────────────────────────────────────────
|
|
2778
2870
|
async get(e) {
|
|
2779
|
-
return
|
|
2871
|
+
return L(e), this._buildGetResult(e);
|
|
2780
2872
|
}
|
|
2781
2873
|
async list(e) {
|
|
2782
2874
|
return this._buildListResult(e);
|
|
2783
2875
|
}
|
|
2784
2876
|
async add(e) {
|
|
2785
|
-
let t = e, n =
|
|
2877
|
+
let t = e, n = k(t, void 0), o = await this._queue.enqueue({
|
|
2786
2878
|
collection: this._collection,
|
|
2787
2879
|
op: "add",
|
|
2788
2880
|
payload: t,
|
|
@@ -2809,8 +2901,8 @@ var oe = class {
|
|
|
2809
2901
|
}]), { ok: true, id: i };
|
|
2810
2902
|
}
|
|
2811
2903
|
async set(e, t) {
|
|
2812
|
-
|
|
2813
|
-
let n = await this._store.getDoc(this._collection, e), o = t, i =
|
|
2904
|
+
L(e);
|
|
2905
|
+
let n = await this._store.getDoc(this._collection, e), o = t, i = k(o, void 0), s = await this._queue.enqueue({
|
|
2814
2906
|
collection: this._collection,
|
|
2815
2907
|
docId: e,
|
|
2816
2908
|
op: "set",
|
|
@@ -2838,8 +2930,8 @@ var oe = class {
|
|
|
2838
2930
|
}]), { ok: true };
|
|
2839
2931
|
}
|
|
2840
2932
|
async update(e, t, n) {
|
|
2841
|
-
|
|
2842
|
-
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a =
|
|
2933
|
+
L(e);
|
|
2934
|
+
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a = k(i, s), c = o?.meta.deleted ? { ...a } : { ...o?.data ?? {}, ...a }, l = await this._queue.enqueue({
|
|
2843
2935
|
collection: this._collection,
|
|
2844
2936
|
docId: e,
|
|
2845
2937
|
op: "update",
|
|
@@ -2872,7 +2964,7 @@ var oe = class {
|
|
|
2872
2964
|
}]), { ok: true };
|
|
2873
2965
|
}
|
|
2874
2966
|
async remove(e) {
|
|
2875
|
-
|
|
2967
|
+
L(e);
|
|
2876
2968
|
let t = await this._store.getDoc(this._collection, e);
|
|
2877
2969
|
return !t || t.meta.deleted, await this._queue.enqueue({
|
|
2878
2970
|
collection: this._collection,
|
|
@@ -2892,7 +2984,7 @@ var oe = class {
|
|
|
2892
2984
|
for (let o of e) {
|
|
2893
2985
|
let i = this._collection;
|
|
2894
2986
|
if (o.kind === "add") {
|
|
2895
|
-
let s = o.data, a =
|
|
2987
|
+
let s = o.data, a = k(s, void 0), c = await this._queue.enqueue({
|
|
2896
2988
|
collection: i,
|
|
2897
2989
|
op: "add",
|
|
2898
2990
|
payload: s,
|
|
@@ -2914,7 +3006,7 @@ var oe = class {
|
|
|
2914
3006
|
}
|
|
2915
3007
|
}), n.push({ type: "added", collection: i, doc: { id: l, data: a } });
|
|
2916
3008
|
} else if (o.kind === "set") {
|
|
2917
|
-
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), l =
|
|
3009
|
+
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), l = k(a, void 0), d = await this._queue.enqueue({
|
|
2918
3010
|
collection: i,
|
|
2919
3011
|
docId: s,
|
|
2920
3012
|
op: "set",
|
|
@@ -2941,7 +3033,7 @@ var oe = class {
|
|
|
2941
3033
|
doc: { id: s, data: l }
|
|
2942
3034
|
});
|
|
2943
3035
|
} else if (o.kind === "update") {
|
|
2944
|
-
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), l = c?.meta.deleted ? void 0 : c?.data, d =
|
|
3036
|
+
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), l = c?.meta.deleted ? void 0 : c?.data, d = k(a, l), u = { ...c?.data ?? {}, ...d }, p = await this._queue.enqueue({
|
|
2945
3037
|
collection: i,
|
|
2946
3038
|
docId: s,
|
|
2947
3039
|
op: "update",
|
|
@@ -2962,7 +3054,7 @@ var oe = class {
|
|
|
2962
3054
|
state: "pending",
|
|
2963
3055
|
pendingMutationIds: [
|
|
2964
3056
|
...c?.meta.pendingMutationIds ?? [],
|
|
2965
|
-
|
|
3057
|
+
p.mutationId
|
|
2966
3058
|
],
|
|
2967
3059
|
deleted: false,
|
|
2968
3060
|
ownerId: c?.meta.ownerId ?? null
|
|
@@ -2984,7 +3076,7 @@ var oe = class {
|
|
|
2984
3076
|
}
|
|
2985
3077
|
// ── Realtime ─────────────────────────────────────────────────────────────────
|
|
2986
3078
|
onDoc(e, t) {
|
|
2987
|
-
return
|
|
3079
|
+
return L(e), this._buildGetResult(e).then((n) => {
|
|
2988
3080
|
t(n ? n.docs[0]?.data ?? null : null);
|
|
2989
3081
|
}).catch(() => t(null)), this._bus.subscribe(this._collection, (n) => {
|
|
2990
3082
|
let o = n.find((i) => i.doc.id === e);
|
|
@@ -3043,7 +3135,7 @@ var oe = class {
|
|
|
3043
3135
|
};
|
|
3044
3136
|
}
|
|
3045
3137
|
doc(e) {
|
|
3046
|
-
|
|
3138
|
+
L(e);
|
|
3047
3139
|
let t = this;
|
|
3048
3140
|
return {
|
|
3049
3141
|
get id() {
|
|
@@ -3106,7 +3198,7 @@ var oe = class {
|
|
|
3106
3198
|
this._store = e.store, this._queue = e.queue, this._bus = e.bus, this._engine = e.engine, this._connectivity = e.connectivity;
|
|
3107
3199
|
}
|
|
3108
3200
|
collection(e) {
|
|
3109
|
-
return
|
|
3201
|
+
return _t(e), new oe(
|
|
3110
3202
|
e,
|
|
3111
3203
|
this._store,
|
|
3112
3204
|
this._queue,
|
|
@@ -3135,10 +3227,10 @@ var oe = class {
|
|
|
3135
3227
|
return this._engine.onError(e);
|
|
3136
3228
|
}
|
|
3137
3229
|
};
|
|
3138
|
-
async function
|
|
3139
|
-
let e =
|
|
3230
|
+
async function we(r) {
|
|
3231
|
+
let e = mt() ? new Q() : new V(r.dbName);
|
|
3140
3232
|
await e.open();
|
|
3141
|
-
let t = new
|
|
3233
|
+
let t = new z(e), n = new H(), o = new G(), i = new X({
|
|
3142
3234
|
// Se startOnline=false, injeta um navigator falso
|
|
3143
3235
|
navigator: r.startOnline === false ? { onLine: false } : typeof navigator < "u" ? navigator : void 0
|
|
3144
3236
|
}), s = r.singleTab ? (
|
|
@@ -3151,7 +3243,7 @@ async function ve(r) {
|
|
|
3151
3243
|
) : new q({
|
|
3152
3244
|
lockName: `neetru-offline:${r.dbName}`,
|
|
3153
3245
|
channelName: `neetru-offline:${r.dbName}`
|
|
3154
|
-
}), a = new
|
|
3246
|
+
}), a = new K({
|
|
3155
3247
|
store: e,
|
|
3156
3248
|
queue: t,
|
|
3157
3249
|
resolver: n,
|
|
@@ -3171,9 +3263,9 @@ async function ve(r) {
|
|
|
3171
3263
|
}
|
|
3172
3264
|
|
|
3173
3265
|
// src/db/realtime/realtime-client.ts
|
|
3174
|
-
var
|
|
3175
|
-
function
|
|
3176
|
-
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++
|
|
3266
|
+
var bt = 0;
|
|
3267
|
+
function Se() {
|
|
3268
|
+
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++bt}-${Math.random().toString(36).slice(2)}`;
|
|
3177
3269
|
}
|
|
3178
3270
|
var F = class {
|
|
3179
3271
|
// ── configuração ──────────────────────────────────────────────────────────
|
|
@@ -3213,7 +3305,7 @@ var F = class {
|
|
|
3213
3305
|
_pendingFrames = [];
|
|
3214
3306
|
// ── construtor ────────────────────────────────────────────────────────────
|
|
3215
3307
|
constructor(e) {
|
|
3216
|
-
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ??
|
|
3308
|
+
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ?? vt, this._setTimeout = e.setTimeoutFn ?? globalThis.setTimeout.bind(globalThis), this._clearTimeout = e.clearTimeoutFn ?? globalThis.clearTimeout.bind(globalThis), this._backoffBaseMs = e.backoffBaseMs ?? 1e3, this._backoffMaxMs = e.backoffMaxMs ?? 3e4, this._heartbeatIntervalMs = e.heartbeatIntervalMs ?? 25e3, this._ticketProvider = e.ticketProvider, this._dbId = e.dbId, this._connect();
|
|
3217
3309
|
}
|
|
3218
3310
|
// ── API pública ───────────────────────────────────────────────────────────
|
|
3219
3311
|
/**
|
|
@@ -3227,8 +3319,8 @@ var F = class {
|
|
|
3227
3319
|
*/
|
|
3228
3320
|
subscribe(e, t, n) {
|
|
3229
3321
|
if (this._closed)
|
|
3230
|
-
return
|
|
3231
|
-
let o =
|
|
3322
|
+
return Se();
|
|
3323
|
+
let o = Se();
|
|
3232
3324
|
this._subscriptions.set(o, {
|
|
3233
3325
|
subscriptionId: o,
|
|
3234
3326
|
collection: e,
|
|
@@ -3492,7 +3584,7 @@ var F = class {
|
|
|
3492
3584
|
_detachHandlers(e) {
|
|
3493
3585
|
e.onopen = null, e.onmessage = null, e.onclose = null, e.onerror = null;
|
|
3494
3586
|
}
|
|
3495
|
-
},
|
|
3587
|
+
}, vt = (r) => {
|
|
3496
3588
|
if (typeof WebSocket > "u")
|
|
3497
3589
|
throw new Error(
|
|
3498
3590
|
"[NeetruRealtimeClient] WebSocket n\xE3o dispon\xEDvel neste ambiente. Injete uma implementa\xE7\xE3o via `webSocketFactory` nas op\xE7\xF5es."
|
|
@@ -3501,12 +3593,12 @@ var F = class {
|
|
|
3501
3593
|
};
|
|
3502
3594
|
|
|
3503
3595
|
// src/db/client-db.ts
|
|
3504
|
-
|
|
3505
|
-
var
|
|
3506
|
-
function
|
|
3507
|
-
return e._transport ? e._transport : r === "firestore" && e.firestoreTransport ? e.firestoreTransport : r === "nosql-vm" && e.realtimeGatewayUrl ?
|
|
3596
|
+
I();
|
|
3597
|
+
var zt = (r) => `/api/sdk/v1/datastore/${encodeURIComponent(r)}`, Y = (r, e) => `/api/sdk/v1/datastore/${encodeURIComponent(r)}/${encodeURIComponent(e)}`, Xe = 200;
|
|
3598
|
+
function Kt(r, e, t) {
|
|
3599
|
+
return e._transport ? e._transport : r === "firestore" && e.firestoreTransport ? e.firestoreTransport : r === "nosql-vm" && e.realtimeGatewayUrl ? Ht(e.realtimeGatewayUrl, t, e) : Je(t);
|
|
3508
3600
|
}
|
|
3509
|
-
function
|
|
3601
|
+
function Je(r) {
|
|
3510
3602
|
return {
|
|
3511
3603
|
async pushMutations(e) {
|
|
3512
3604
|
if (!r)
|
|
@@ -3556,11 +3648,11 @@ function je(r) {
|
|
|
3556
3648
|
} catch (a) {
|
|
3557
3649
|
let c = a instanceof Error ? a.message : String(a), l = a?.code, d = a?.status;
|
|
3558
3650
|
if (l === "forbidden" || l === "unauthorized" || l === "not_found" || l === "conflict" || l === "validation_failed" || typeof d == "number" && d >= 400 && d < 500 && d !== 408 && d !== 429) {
|
|
3559
|
-
let
|
|
3651
|
+
let p = l === "forbidden" || l === "unauthorized" ? "rejected_permission" : "rejected_validation";
|
|
3560
3652
|
n.push({
|
|
3561
3653
|
mutationId: s.mutationId,
|
|
3562
3654
|
outcome: "rejected",
|
|
3563
|
-
reason:
|
|
3655
|
+
reason: p,
|
|
3564
3656
|
serverVersion: null,
|
|
3565
3657
|
serverData: null
|
|
3566
3658
|
});
|
|
@@ -3584,7 +3676,7 @@ function je(r) {
|
|
|
3584
3676
|
try {
|
|
3585
3677
|
l = await t(r, {
|
|
3586
3678
|
method: "GET",
|
|
3587
|
-
path:
|
|
3679
|
+
path: zt(c),
|
|
3588
3680
|
requireAuth: !0,
|
|
3589
3681
|
retries: 2,
|
|
3590
3682
|
headers: o,
|
|
@@ -3592,37 +3684,37 @@ function je(r) {
|
|
|
3592
3684
|
// TRUNCADAS — reportadas em `truncatedCollections` p/ o SyncEngine
|
|
3593
3685
|
// (a) NÃO rodar deleção-por-ausência e (b) sinalizar
|
|
3594
3686
|
// `DbListResult.truncated`. Paginação por cursor é tarefa Core (gated).
|
|
3595
|
-
query: { limit:
|
|
3687
|
+
query: { limit: Xe }
|
|
3596
3688
|
});
|
|
3597
|
-
} catch (
|
|
3598
|
-
let
|
|
3689
|
+
} catch (p) {
|
|
3690
|
+
let h = p instanceof Error ? p.message : String(p);
|
|
3599
3691
|
throw new exports.NeetruDbError(
|
|
3600
3692
|
"db_unavailable",
|
|
3601
|
-
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${c}": ${
|
|
3693
|
+
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${c}": ${h}`
|
|
3602
3694
|
);
|
|
3603
3695
|
}
|
|
3604
3696
|
let d = l, u = Array.isArray(d?.items) ? d.items : [];
|
|
3605
|
-
u.length >=
|
|
3606
|
-
for (let
|
|
3607
|
-
if (!
|
|
3608
|
-
let
|
|
3609
|
-
if (!
|
|
3610
|
-
let { id:
|
|
3611
|
-
if (typeof
|
|
3612
|
-
|
|
3613
|
-
else if (
|
|
3614
|
-
|
|
3615
|
-
else if (typeof
|
|
3616
|
-
let
|
|
3617
|
-
Number.isNaN(
|
|
3697
|
+
u.length >= Xe && a.push(c);
|
|
3698
|
+
for (let p of u) {
|
|
3699
|
+
if (!p || typeof p != "object") continue;
|
|
3700
|
+
let h = p, _ = typeof h.id == "string" ? h.id : null;
|
|
3701
|
+
if (!_) continue;
|
|
3702
|
+
let { id: y, ...B } = h, g = i, v = B._updatedAt;
|
|
3703
|
+
if (typeof v == "number")
|
|
3704
|
+
g = v;
|
|
3705
|
+
else if (v !== null && typeof v == "object" && typeof v._seconds == "number")
|
|
3706
|
+
g = v._seconds * 1e3;
|
|
3707
|
+
else if (typeof v == "string") {
|
|
3708
|
+
let C = Date.parse(v);
|
|
3709
|
+
Number.isNaN(C) || (g = C);
|
|
3618
3710
|
}
|
|
3619
|
-
let
|
|
3711
|
+
let w = `rest_${_}_${g}`;
|
|
3620
3712
|
s.push({
|
|
3621
3713
|
collection: c,
|
|
3622
|
-
id:
|
|
3623
|
-
data:
|
|
3624
|
-
serverVersion:
|
|
3625
|
-
serverTimestamp:
|
|
3714
|
+
id: _,
|
|
3715
|
+
data: B,
|
|
3716
|
+
serverVersion: w,
|
|
3717
|
+
serverTimestamp: g,
|
|
3626
3718
|
deleted: false
|
|
3627
3719
|
});
|
|
3628
3720
|
}
|
|
@@ -3631,10 +3723,10 @@ function je(r) {
|
|
|
3631
3723
|
}
|
|
3632
3724
|
};
|
|
3633
3725
|
}
|
|
3634
|
-
function
|
|
3635
|
-
let n =
|
|
3726
|
+
function Ht(r, e, t) {
|
|
3727
|
+
let n = Je(e), o = new F({
|
|
3636
3728
|
gatewayUrl: r,
|
|
3637
|
-
ticketProvider:
|
|
3729
|
+
ticketProvider: Gt(e, t),
|
|
3638
3730
|
dbId: t.dbId,
|
|
3639
3731
|
webSocketFactory: t._wsFactory
|
|
3640
3732
|
});
|
|
@@ -3670,7 +3762,7 @@ function At(r, e, t) {
|
|
|
3670
3762
|
}
|
|
3671
3763
|
};
|
|
3672
3764
|
}
|
|
3673
|
-
function
|
|
3765
|
+
function Gt(r, e) {
|
|
3674
3766
|
return async () => {
|
|
3675
3767
|
let { httpRequest: t } = await Promise.resolve().then(() => (W(), U));
|
|
3676
3768
|
return await t(r, {
|
|
@@ -3685,27 +3777,27 @@ function Nt(r, e) {
|
|
|
3685
3777
|
});
|
|
3686
3778
|
};
|
|
3687
3779
|
}
|
|
3688
|
-
function
|
|
3780
|
+
function Gn(r, e, t, n) {
|
|
3689
3781
|
return new F({ gatewayUrl: r, ticketProvider: e, dbId: t, webSocketFactory: n });
|
|
3690
3782
|
}
|
|
3691
3783
|
var pe = false;
|
|
3692
|
-
function
|
|
3784
|
+
function Xn() {
|
|
3693
3785
|
pe = false;
|
|
3694
3786
|
}
|
|
3695
|
-
function
|
|
3787
|
+
function Xt(r, e) {
|
|
3696
3788
|
pe || e.dbId && r.env !== "prod" && (typeof window > "u" || typeof console > "u" || typeof console.warn != "function" || (pe = true, console.warn(
|
|
3697
3789
|
'[neetru/sdk] client.db recebeu `dbId` ("' + e.dbId + '") mas ele N\xC3O roteia nem escopa nada no servidor: as escritas do mundo Documentos (add/set/update/remove/list) SEMPRE caem no datastore gen\xE9rico do Core, escopado s\xF3 por tenant/produto (header x-neetru-tenant). `dbId` apenas nomeia o cache offline LOCAL (IndexedDB). ATEN\xC7\xC3O: usar dbIds distintos no MESMO tenant/produto N\xC3O isola os dados no servidor \u2014 eles se misturam (co-mingling). Veja o JSDoc de NeetruDbOptions.dbId.'
|
|
3698
3790
|
)));
|
|
3699
3791
|
}
|
|
3700
|
-
function
|
|
3701
|
-
|
|
3792
|
+
function Jn(r, e = {}) {
|
|
3793
|
+
Xt(r, e);
|
|
3702
3794
|
let t;
|
|
3703
3795
|
e.transport !== void 0 ? t = e.transport : e.engine !== void 0 ? (console.warn(
|
|
3704
3796
|
'[neetru/sdk] NeetruDbOptions.engine est\xE1 depreciado e ser\xE1 removido em v3.0. Use NeetruDbOptions.transport em vez de engine. Valores v\xE1lidos: "rest" | "firestore" | "nosql-vm". Nota: "engine" no SDK configura o TRANSPORTE (REST/Firestore/WebSocket), n\xE3o o storage engine do admin database (firestore-instance, cloud-sql-postgres, \u2026).'
|
|
3705
3797
|
), t = e.engine) : t = "rest";
|
|
3706
|
-
let n =
|
|
3798
|
+
let n = Kt(t, e, r), o = e.dbId ?? "default", i = e.dbName ?? `neetru-db__${r.productId ?? "sdk"}__${o}__${r.env}`, s = null, a = null;
|
|
3707
3799
|
function c() {
|
|
3708
|
-
return s || (s =
|
|
3800
|
+
return s || (s = we({
|
|
3709
3801
|
dbName: i,
|
|
3710
3802
|
transport: n,
|
|
3711
3803
|
singleTab: e.singleTab ?? r.env !== "prod"
|
|
@@ -3713,7 +3805,7 @@ function xn(r, e = {}) {
|
|
|
3713
3805
|
}
|
|
3714
3806
|
return {
|
|
3715
3807
|
collection(l) {
|
|
3716
|
-
return a ? a.collection(l) :
|
|
3808
|
+
return a ? a.collection(l) : Jt(l, c);
|
|
3717
3809
|
},
|
|
3718
3810
|
async sql(l, d) {
|
|
3719
3811
|
if (r.env === "dev")
|
|
@@ -3721,7 +3813,7 @@ function xn(r, e = {}) {
|
|
|
3721
3813
|
"db_unavailable",
|
|
3722
3814
|
"[SDK] db.sql() n\xE3o dispon\xEDvel em NEETRU_ENV=dev. Use `neetru dev` para subir o container Postgres local."
|
|
3723
3815
|
);
|
|
3724
|
-
return (await Promise.resolve().then(() => (
|
|
3816
|
+
return (await Promise.resolve().then(() => (Ge(), He))).createSqlClientFromConfig(r, l, d);
|
|
3725
3817
|
},
|
|
3726
3818
|
get syncState() {
|
|
3727
3819
|
return a?.syncState ?? {
|
|
@@ -3734,8 +3826,8 @@ function xn(r, e = {}) {
|
|
|
3734
3826
|
onSyncStateChanged(l) {
|
|
3735
3827
|
if (a) return a.onSyncStateChanged(l);
|
|
3736
3828
|
let d = null, u = false;
|
|
3737
|
-
return c().then((
|
|
3738
|
-
u || (d =
|
|
3829
|
+
return c().then((p) => {
|
|
3830
|
+
u || (d = p.onSyncStateChanged(l));
|
|
3739
3831
|
}), () => {
|
|
3740
3832
|
u = true, d?.();
|
|
3741
3833
|
};
|
|
@@ -3752,15 +3844,15 @@ function xn(r, e = {}) {
|
|
|
3752
3844
|
onError(l) {
|
|
3753
3845
|
if (a) return a.onError(l);
|
|
3754
3846
|
let d = null, u = false;
|
|
3755
|
-
return c().then((
|
|
3756
|
-
u || (d =
|
|
3847
|
+
return c().then((p) => {
|
|
3848
|
+
u || (d = p.onError(l));
|
|
3757
3849
|
}), () => {
|
|
3758
3850
|
u = true, d?.();
|
|
3759
3851
|
};
|
|
3760
3852
|
}
|
|
3761
3853
|
};
|
|
3762
3854
|
}
|
|
3763
|
-
function
|
|
3855
|
+
function Jt(r, e) {
|
|
3764
3856
|
async function t() {
|
|
3765
3857
|
return (await e()).collection(r);
|
|
3766
3858
|
}
|
|
@@ -3839,12 +3931,12 @@ function Ft(r, e) {
|
|
|
3839
3931
|
}
|
|
3840
3932
|
|
|
3841
3933
|
exports.NEETRU_SENTINEL_KEY = te;
|
|
3842
|
-
exports.__resetDbIdWarningForTests =
|
|
3843
|
-
exports.createNeetruDb =
|
|
3844
|
-
exports.createRestSyncTransport =
|
|
3845
|
-
exports.getWebSocketRealtimeClient =
|
|
3846
|
-
exports.increment =
|
|
3847
|
-
exports.isFieldSentinel =
|
|
3848
|
-
exports.isIncrementSentinel =
|
|
3849
|
-
exports.isServerTimestampSentinel =
|
|
3850
|
-
exports.serverTimestamp =
|
|
3934
|
+
exports.__resetDbIdWarningForTests = Xn;
|
|
3935
|
+
exports.createNeetruDb = Jn;
|
|
3936
|
+
exports.createRestSyncTransport = Je;
|
|
3937
|
+
exports.getWebSocketRealtimeClient = Gn;
|
|
3938
|
+
exports.increment = at;
|
|
3939
|
+
exports.isFieldSentinel = D;
|
|
3940
|
+
exports.isIncrementSentinel = ge;
|
|
3941
|
+
exports.isServerTimestampSentinel = ye;
|
|
3942
|
+
exports.serverTimestamp = st;
|