@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.mjs
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { openDB } from 'idb';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
3
|
+
var Ye = Object.defineProperty;
|
|
4
|
+
var S = (r, e) => () => (r && (e = r(r = 0)), e);
|
|
5
|
+
var me = (r, e) => {
|
|
6
6
|
for (var t in e)
|
|
7
|
-
|
|
7
|
+
Ye(r, t, { get: e[t], enumerable: true });
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
// src/errors.ts
|
|
11
|
-
var
|
|
12
|
-
|
|
11
|
+
var b, x = S(() => {
|
|
12
|
+
b = class r extends Error {
|
|
13
13
|
code;
|
|
14
14
|
status;
|
|
15
15
|
requestId;
|
|
@@ -22,20 +22,27 @@ var g, x = T(() => {
|
|
|
22
22
|
* para um `NeetruDbErrorCode` mais específico (recuperável vs não).
|
|
23
23
|
*/
|
|
24
24
|
reason;
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
/**
|
|
26
|
+
* 3.8.0: `x-correlation-id` da requisição que falhou. O SDK gera um UUID por
|
|
27
|
+
* chamada (ou usa o que o chamador passou) e manda no header; o Core grava o
|
|
28
|
+
* mesmo id nos logs/auditoria — cite-o ao abrir um ticket de suporte.
|
|
29
|
+
* `undefined` em erros que acontecem antes de qualquer requisição.
|
|
30
|
+
*/
|
|
31
|
+
correlationId;
|
|
32
|
+
constructor(e, t, n, o, i, s) {
|
|
33
|
+
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);
|
|
27
34
|
}
|
|
28
35
|
};
|
|
29
36
|
});
|
|
30
37
|
|
|
31
38
|
// src/db-errors.ts
|
|
32
|
-
var
|
|
39
|
+
var Ze, f, I = S(() => {
|
|
33
40
|
x();
|
|
34
|
-
|
|
41
|
+
Ze = /* @__PURE__ */ new Set([
|
|
35
42
|
"db_unavailable",
|
|
36
43
|
"db_conflict",
|
|
37
44
|
"db_timeout"
|
|
38
|
-
]),
|
|
45
|
+
]), f = class r extends b {
|
|
39
46
|
/** Código de erro fechado — específico de DB. */
|
|
40
47
|
code;
|
|
41
48
|
/**
|
|
@@ -49,36 +56,91 @@ var Qe, p, k = T(() => {
|
|
|
49
56
|
*/
|
|
50
57
|
dbId;
|
|
51
58
|
constructor(e, t, n) {
|
|
52
|
-
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable =
|
|
59
|
+
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable = Ze.has(e), this.dbId = n, Object.setPrototypeOf(this, r.prototype);
|
|
53
60
|
}
|
|
54
61
|
};
|
|
55
62
|
});
|
|
56
63
|
|
|
57
64
|
// src/idempotency.ts
|
|
58
|
-
function
|
|
65
|
+
function Te() {
|
|
59
66
|
let r = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
60
67
|
if (r && typeof r.randomUUID == "function")
|
|
61
68
|
return r.randomUUID();
|
|
62
|
-
throw new
|
|
69
|
+
throw new b(
|
|
63
70
|
"runtime_unsupported",
|
|
64
71
|
"crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
|
|
65
72
|
);
|
|
66
73
|
}
|
|
67
|
-
var
|
|
74
|
+
var Re = S(() => {
|
|
68
75
|
x();
|
|
69
76
|
});
|
|
70
77
|
|
|
78
|
+
// src/correlation.ts
|
|
79
|
+
function ke(r) {
|
|
80
|
+
return typeof r == "string" && r.length > 0 && r.length <= 128 && wt.test(r);
|
|
81
|
+
}
|
|
82
|
+
function St() {
|
|
83
|
+
let r = globalThis.crypto;
|
|
84
|
+
try {
|
|
85
|
+
if (r && typeof r.randomUUID == "function") return r.randomUUID();
|
|
86
|
+
} catch {
|
|
87
|
+
}
|
|
88
|
+
let e = new Uint8Array(16), t = false;
|
|
89
|
+
try {
|
|
90
|
+
r && typeof r.getRandomValues == "function" && (r.getRandomValues(e), t = !0);
|
|
91
|
+
} catch {
|
|
92
|
+
t = false;
|
|
93
|
+
}
|
|
94
|
+
if (!t)
|
|
95
|
+
for (let o = 0; o < e.length; o++) e[o] = Math.floor(Math.random() * 256);
|
|
96
|
+
e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128;
|
|
97
|
+
let n = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join("");
|
|
98
|
+
return `${n.slice(0, 8)}-${n.slice(8, 12)}-${n.slice(12, 16)}-${n.slice(16, 20)}-${n.slice(20)}`;
|
|
99
|
+
}
|
|
100
|
+
function Ce(r, e) {
|
|
101
|
+
let t, n = {};
|
|
102
|
+
for (let [i, s] of Object.entries(r)) {
|
|
103
|
+
if (i.toLowerCase() === De) {
|
|
104
|
+
ke(s) && (t = s);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
n[i] = s;
|
|
108
|
+
}
|
|
109
|
+
let o = t ?? (ke(e) ? e : void 0) ?? St();
|
|
110
|
+
return n[De] = o, { headers: n, correlationId: o };
|
|
111
|
+
}
|
|
112
|
+
var De, wt, Ie = S(() => {
|
|
113
|
+
De = "x-correlation-id", wt = /^[A-Za-z0-9._:\-]+$/;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// src/server-only-key.ts
|
|
117
|
+
function Pe() {
|
|
118
|
+
try {
|
|
119
|
+
let r = globalThis.navigator;
|
|
120
|
+
if (r && r.product === "ReactNative") return "react-native";
|
|
121
|
+
} catch {
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
let r = globalThis;
|
|
125
|
+
if (typeof r.window < "u" && typeof r.document < "u") return "browser";
|
|
126
|
+
} catch {
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
var Ee = S(() => {
|
|
131
|
+
});
|
|
132
|
+
|
|
71
133
|
// src/http.ts
|
|
72
134
|
var U = {};
|
|
73
|
-
|
|
74
|
-
checkCoreVersionCompat: () =>
|
|
75
|
-
httpRequest: () =>
|
|
76
|
-
statusToCode: () =>
|
|
135
|
+
me(U, {
|
|
136
|
+
checkCoreVersionCompat: () => qe,
|
|
137
|
+
httpRequest: () => Ot,
|
|
138
|
+
statusToCode: () => Fe
|
|
77
139
|
});
|
|
78
140
|
function se(r) {
|
|
79
141
|
return r.split("+")[0] ?? r;
|
|
80
142
|
}
|
|
81
|
-
function
|
|
143
|
+
function Rt(r, e) {
|
|
82
144
|
let t = se(r).split(".").map(Number), n = se(e).split(".").map(Number), o = Math.max(t.length, n.length);
|
|
83
145
|
for (let i = 0; i < o; i++) {
|
|
84
146
|
let s = t[i] ?? 0, a = n[i] ?? 0;
|
|
@@ -86,58 +148,62 @@ function ht(r, e) {
|
|
|
86
148
|
}
|
|
87
149
|
return 0;
|
|
88
150
|
}
|
|
89
|
-
function
|
|
151
|
+
function Ne() {
|
|
90
152
|
try {
|
|
91
|
-
return "3.
|
|
153
|
+
return "3.8.1";
|
|
92
154
|
} catch {
|
|
93
155
|
return;
|
|
94
156
|
}
|
|
95
157
|
}
|
|
96
|
-
function
|
|
97
|
-
let r =
|
|
158
|
+
function Dt() {
|
|
159
|
+
let r = Ne();
|
|
98
160
|
return r ? parseInt(r.split(".")[0] ?? "3", 10) : 3;
|
|
99
161
|
}
|
|
100
|
-
function
|
|
162
|
+
function qe(r) {
|
|
101
163
|
if (!r) return;
|
|
102
|
-
let e =
|
|
164
|
+
let e = Dt(), t = Tt[e];
|
|
103
165
|
if (!t) return;
|
|
104
166
|
let n = se(r);
|
|
105
|
-
if (
|
|
167
|
+
if (Rt(n, t) < 0) {
|
|
106
168
|
let o = `${e}:${n}`;
|
|
107
|
-
|
|
169
|
+
Le.has(o) || (Le.add(o), console.warn(
|
|
108
170
|
`[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.`
|
|
109
171
|
));
|
|
110
172
|
}
|
|
111
173
|
}
|
|
112
|
-
function
|
|
174
|
+
function kt() {
|
|
175
|
+
let r = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
176
|
+
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;
|
|
177
|
+
}
|
|
178
|
+
function Me(r) {
|
|
113
179
|
let e = 200 * Math.pow(4, r), t = e * 0.2 * (Math.random() * 2 - 1);
|
|
114
180
|
return Math.max(50, Math.round(e + t));
|
|
115
181
|
}
|
|
116
|
-
function
|
|
182
|
+
function Lt(r) {
|
|
117
183
|
if (!r) return null;
|
|
118
184
|
let e = Number(r);
|
|
119
|
-
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3),
|
|
185
|
+
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), Oe);
|
|
120
186
|
let t = Date.parse(r);
|
|
121
187
|
if (Number.isFinite(t)) {
|
|
122
188
|
let n = t - Date.now();
|
|
123
|
-
if (n > 0) return Math.min(n,
|
|
189
|
+
if (n > 0) return Math.min(n, Oe);
|
|
124
190
|
}
|
|
125
191
|
return null;
|
|
126
192
|
}
|
|
127
|
-
function
|
|
193
|
+
function xe(r) {
|
|
128
194
|
return new Promise((e) => setTimeout(e, r));
|
|
129
195
|
}
|
|
130
|
-
function
|
|
196
|
+
function Fe(r) {
|
|
131
197
|
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";
|
|
132
198
|
}
|
|
133
|
-
function
|
|
199
|
+
function Mt(r, e, t) {
|
|
134
200
|
let n = r.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, i = new URL(`${n}${o}`);
|
|
135
201
|
if (t)
|
|
136
202
|
for (let [s, a] of Object.entries(t))
|
|
137
203
|
a !== void 0 && i.searchParams.set(s, String(a));
|
|
138
204
|
return i.toString();
|
|
139
205
|
}
|
|
140
|
-
async function
|
|
206
|
+
async function Ae(r) {
|
|
141
207
|
let e = await r.text();
|
|
142
208
|
if (e)
|
|
143
209
|
try {
|
|
@@ -146,95 +212,101 @@ async function Pe(r) {
|
|
|
146
212
|
return;
|
|
147
213
|
}
|
|
148
214
|
}
|
|
149
|
-
async function
|
|
150
|
-
let t = e.method ?? "GET", n =
|
|
151
|
-
accept: "application/json",
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (
|
|
215
|
+
async function Ot(r, e) {
|
|
216
|
+
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(
|
|
217
|
+
{ accept: "application/json", ...e.headers },
|
|
218
|
+
e.correlationId
|
|
219
|
+
), c = Ne();
|
|
220
|
+
if (c && (s["x-neetru-sdk-version"] = c), e.requireAuth) {
|
|
155
221
|
if (!r.apiKey)
|
|
156
|
-
throw new
|
|
157
|
-
"missing_api_key",
|
|
158
|
-
"This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
159
|
-
);
|
|
222
|
+
throw new b("missing_api_key", kt());
|
|
160
223
|
s.authorization = `Bearer ${r.apiKey}`;
|
|
161
224
|
}
|
|
162
225
|
if (e.idempotencyKey) {
|
|
163
|
-
let
|
|
164
|
-
s["idempotency-key"] =
|
|
165
|
-
}
|
|
166
|
-
let
|
|
167
|
-
|
|
168
|
-
let
|
|
169
|
-
for (let
|
|
170
|
-
if (
|
|
171
|
-
throw
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
226
|
+
let p = typeof e.idempotencyKey == "string" ? e.idempotencyKey : Te();
|
|
227
|
+
s["idempotency-key"] = p;
|
|
228
|
+
}
|
|
229
|
+
let l = e.body !== void 0 && t !== "GET" && t !== "DELETE" ? JSON.stringify(e.body) : void 0;
|
|
230
|
+
l !== void 0 && (s["content-type"] = "application/json");
|
|
231
|
+
let d = null, u = e.deadlineMs != null ? Date.now() + e.deadlineMs : null;
|
|
232
|
+
for (let p = 0; p <= i; p++) {
|
|
233
|
+
if (u != null && Date.now() >= u)
|
|
234
|
+
throw d ?? new b(
|
|
235
|
+
"network_error",
|
|
236
|
+
"Deadline excedido antes de completar a request",
|
|
237
|
+
void 0,
|
|
238
|
+
void 0,
|
|
239
|
+
void 0,
|
|
240
|
+
a
|
|
241
|
+
);
|
|
242
|
+
let h = { method: t, headers: s };
|
|
243
|
+
l !== void 0 && (h.body = l);
|
|
244
|
+
let _ = u != null ? Math.max(1, Math.min(3e4, u - Date.now())) : 3e4;
|
|
245
|
+
h.signal = AbortSignal.timeout(_);
|
|
246
|
+
let y;
|
|
177
247
|
try {
|
|
178
|
-
|
|
248
|
+
y = await r.fetch(n, h);
|
|
179
249
|
} catch (m) {
|
|
180
250
|
let O = m instanceof DOMException && m.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${m instanceof Error ? m.message : "fetch failed"}`;
|
|
181
|
-
if (
|
|
182
|
-
let
|
|
183
|
-
if (
|
|
184
|
-
await
|
|
251
|
+
if (d = new b("network_error", O, void 0, void 0, void 0, a), p < i) {
|
|
252
|
+
let he = Me(p);
|
|
253
|
+
if (u == null || Date.now() + he < u) {
|
|
254
|
+
await xe(he);
|
|
185
255
|
continue;
|
|
186
256
|
}
|
|
187
257
|
}
|
|
188
|
-
throw
|
|
258
|
+
throw d;
|
|
189
259
|
}
|
|
190
|
-
let
|
|
191
|
-
if (
|
|
192
|
-
let m =
|
|
193
|
-
return m &&
|
|
260
|
+
let B = y.headers.get("x-request-id") ?? y.headers.get("x-correlation-id") ?? void 0;
|
|
261
|
+
if (y.ok) {
|
|
262
|
+
let m = y.headers.get("x-neetru-version");
|
|
263
|
+
return m && qe(m), await Ae(y);
|
|
194
264
|
}
|
|
195
|
-
let
|
|
196
|
-
if (
|
|
197
|
-
let m =
|
|
198
|
-
typeof m == "string" ?
|
|
265
|
+
let g = await Ae(y), v = Fe(y.status), w = `HTTP ${y.status}`, C;
|
|
266
|
+
if (g && typeof g == "object") {
|
|
267
|
+
let m = g.error;
|
|
268
|
+
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);
|
|
199
269
|
}
|
|
200
|
-
if (
|
|
201
|
-
let m =
|
|
202
|
-
|
|
270
|
+
if (y.status === 410) {
|
|
271
|
+
let m = y.headers.get("x-neetru-replacement");
|
|
272
|
+
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}.`);
|
|
203
273
|
}
|
|
204
|
-
let
|
|
205
|
-
if (
|
|
206
|
-
let O =
|
|
207
|
-
if (
|
|
208
|
-
await
|
|
274
|
+
let fe = new b(v, w, y.status, B, C, a);
|
|
275
|
+
if (d = fe, Et.has(v) && p < i) {
|
|
276
|
+
let O = Lt(y.headers.get("retry-after")) ?? Me(p);
|
|
277
|
+
if (u == null || Date.now() + O < u) {
|
|
278
|
+
await xe(O);
|
|
209
279
|
continue;
|
|
210
280
|
}
|
|
211
281
|
}
|
|
212
|
-
throw
|
|
282
|
+
throw fe;
|
|
213
283
|
}
|
|
214
|
-
throw
|
|
284
|
+
throw d ?? new b("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, a);
|
|
215
285
|
}
|
|
216
|
-
var
|
|
286
|
+
var Tt, Le, Ct, It, Pt, Et, Oe, W = S(() => {
|
|
217
287
|
x();
|
|
218
|
-
|
|
219
|
-
|
|
288
|
+
Re();
|
|
289
|
+
Ie();
|
|
290
|
+
Ee();
|
|
291
|
+
Tt = {
|
|
220
292
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
221
293
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
222
294
|
3: "2026.05.1"
|
|
223
295
|
};
|
|
224
|
-
|
|
225
|
-
|
|
296
|
+
Le = /* @__PURE__ */ new Set();
|
|
297
|
+
Ct = 2, It = 0, Pt = /* @__PURE__ */ new Set(["GET", "HEAD"]), Et = /* @__PURE__ */ new Set([
|
|
226
298
|
"rate_limited",
|
|
227
299
|
"server_error",
|
|
228
300
|
"network_error"
|
|
229
301
|
]);
|
|
230
|
-
|
|
302
|
+
Oe = 6e4;
|
|
231
303
|
});
|
|
232
304
|
|
|
233
305
|
// src/db/sql/resilient-pool.ts
|
|
234
306
|
function J(r) {
|
|
235
307
|
return typeof r == "object" && r !== null && r[ae] === true;
|
|
236
308
|
}
|
|
237
|
-
function
|
|
309
|
+
function xt(r) {
|
|
238
310
|
let e = r && typeof r == "object" && "code" in r ? r.code : void 0;
|
|
239
311
|
if (e === "ECONNREFUSED" || e === "ETIMEDOUT" || e === "ENOTFOUND" || e === "EHOSTUNREACH" || e === "EAI_AGAIN")
|
|
240
312
|
return true;
|
|
@@ -246,67 +318,84 @@ function Tt(r) {
|
|
|
246
318
|
let n = (r instanceof Error ? r.message : typeof r == "string" ? r : "").toLowerCase();
|
|
247
319
|
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"));
|
|
248
320
|
}
|
|
321
|
+
function Ue(r) {
|
|
322
|
+
let e = r && typeof r == "object" && "code" in r ? r.code : void 0;
|
|
323
|
+
if (typeof e == "string" && At.has(e.toUpperCase()))
|
|
324
|
+
return true;
|
|
325
|
+
let n = (r instanceof Error ? r.message : typeof r == "string" ? r : "").toLowerCase();
|
|
326
|
+
return n.length === 0 ? false : n.includes("password authentication failed") || n.includes("sasl authentication failed");
|
|
327
|
+
}
|
|
328
|
+
function Nt(r) {
|
|
329
|
+
return xt(r) || Ue(r);
|
|
330
|
+
}
|
|
249
331
|
function ce(r) {
|
|
250
332
|
let e = { current: r }, t = null, n = {
|
|
251
|
-
__neetruSwapInner(
|
|
252
|
-
let
|
|
253
|
-
return e.current =
|
|
333
|
+
__neetruSwapInner(d) {
|
|
334
|
+
let u = e.current;
|
|
335
|
+
return e.current = d, u;
|
|
254
336
|
},
|
|
255
337
|
__neetruGetInner() {
|
|
256
338
|
return e.current;
|
|
257
339
|
},
|
|
258
|
-
__neetruSetReLease(
|
|
259
|
-
t =
|
|
340
|
+
__neetruSetReLease(d) {
|
|
341
|
+
t = d;
|
|
260
342
|
}
|
|
261
|
-
}, o = (
|
|
343
|
+
}, o = (d, u, p) => u[d](
|
|
344
|
+
...p
|
|
345
|
+
), i = (d, u) => (...p) => p.length > 0 && typeof p[p.length - 1] == "function" ? o(d, e.current, p) : (async () => {
|
|
262
346
|
try {
|
|
263
|
-
return await o(e.current,
|
|
264
|
-
} catch (
|
|
265
|
-
let
|
|
266
|
-
if (
|
|
267
|
-
throw
|
|
347
|
+
return await o(d, e.current, p);
|
|
348
|
+
} catch (h) {
|
|
349
|
+
let _ = t;
|
|
350
|
+
if (_ === null || !u(h))
|
|
351
|
+
throw h;
|
|
268
352
|
try {
|
|
269
|
-
await
|
|
353
|
+
await _();
|
|
270
354
|
} catch {
|
|
271
|
-
throw
|
|
355
|
+
throw h;
|
|
272
356
|
}
|
|
273
|
-
return await o(e.current,
|
|
357
|
+
return await o(d, e.current, p);
|
|
274
358
|
}
|
|
275
|
-
})(), s = Object.create(Object.getPrototypeOf(r));
|
|
276
|
-
return new Proxy(
|
|
277
|
-
get(
|
|
278
|
-
if (
|
|
279
|
-
if (
|
|
280
|
-
if (
|
|
281
|
-
if (
|
|
282
|
-
if (
|
|
283
|
-
if (
|
|
359
|
+
})(), s = i("query", Nt), a = i("connect", Ue), c = Object.create(Object.getPrototypeOf(r));
|
|
360
|
+
return new Proxy(c, {
|
|
361
|
+
get(d, u) {
|
|
362
|
+
if (u === ae) return true;
|
|
363
|
+
if (u === "__neetruSwapInner") return n.__neetruSwapInner;
|
|
364
|
+
if (u === "__neetruGetInner") return n.__neetruGetInner;
|
|
365
|
+
if (u === "__neetruSetReLease") return n.__neetruSetReLease;
|
|
366
|
+
if (u === "query") return s;
|
|
367
|
+
if (u === "connect") {
|
|
368
|
+
let _ = e.current;
|
|
369
|
+
return typeof _.connect == "function" ? a : _.connect;
|
|
370
|
+
}
|
|
371
|
+
if (u === "constructor")
|
|
284
372
|
return e.current.constructor;
|
|
285
|
-
let
|
|
286
|
-
return typeof
|
|
373
|
+
let p = e.current, h = Reflect.get(p, u, p);
|
|
374
|
+
return typeof h == "function" ? h.bind(p) : h;
|
|
287
375
|
}
|
|
288
376
|
});
|
|
289
377
|
}
|
|
290
|
-
var ae, le =
|
|
378
|
+
var ae, At, le = S(() => {
|
|
291
379
|
ae = /* @__PURE__ */ Symbol.for("neetru.sdk.resilientPool");
|
|
380
|
+
At = /* @__PURE__ */ new Set(["28P01", "28000"]);
|
|
292
381
|
});
|
|
293
382
|
|
|
294
383
|
// src/db/sql/lease.ts
|
|
295
|
-
function
|
|
384
|
+
function Ft(r, e) {
|
|
296
385
|
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;
|
|
297
386
|
}
|
|
298
|
-
function
|
|
299
|
-
if (r instanceof
|
|
387
|
+
function Ut(r) {
|
|
388
|
+
if (r instanceof f) return r;
|
|
300
389
|
let e = r instanceof Error ? r.message : String(r), t = r instanceof Error ? r.code : void 0;
|
|
301
|
-
return typeof t == "string" && (t === "ECONNREFUSED" || t === "ENOTFOUND" || t === "ETIMEDOUT") ? new
|
|
390
|
+
return typeof t == "string" && (t === "ECONNREFUSED" || t === "ENOTFOUND" || t === "ETIMEDOUT") ? new f(
|
|
302
391
|
"db_unavailable",
|
|
303
392
|
`Pool connection failed (${t}): ${e}`
|
|
304
|
-
) : e.includes("statement timeout") || e.includes("lock timeout") ? new
|
|
393
|
+
) : e.includes("statement timeout") || e.includes("lock timeout") ? new f("db_timeout", `Query timeout: ${e}`) : e.includes("40001") || e.includes("40P01") || e.includes("deadlock") ? new f("db_conflict", `Transaction conflict: ${e}`) : e.includes("42501") || e.toLowerCase().includes("permission denied") ? new f("db_permission_denied", `Permission denied: ${e}`) : new f("db_unavailable", `Database error: ${e}`);
|
|
305
394
|
}
|
|
306
|
-
var
|
|
307
|
-
|
|
395
|
+
var qt, M, $, We = S(() => {
|
|
396
|
+
I();
|
|
308
397
|
le();
|
|
309
|
-
|
|
398
|
+
qt = 0.8, M = 3e4, $ = class {
|
|
310
399
|
/**
|
|
311
400
|
* Lease atual. `null` no modo LAZY antes da 1ª query (o 1º lease é adiado —
|
|
312
401
|
* ver `_ensureInitialLease`). Após a 1ª aquisição, sempre não-nulo.
|
|
@@ -346,7 +435,7 @@ var Rt, E, $, Me = T(() => {
|
|
|
346
435
|
isolationLevel: t.isolationLevel
|
|
347
436
|
}) : await this._orm.transaction(e);
|
|
348
437
|
} catch (n) {
|
|
349
|
-
throw
|
|
438
|
+
throw Ut(n);
|
|
350
439
|
}
|
|
351
440
|
}
|
|
352
441
|
async close() {
|
|
@@ -364,13 +453,13 @@ var Rt, E, $, Me = T(() => {
|
|
|
364
453
|
* usa `MIN_RENEWAL_DELAY_MS` como fallback seguro.
|
|
365
454
|
*/
|
|
366
455
|
_calcRenewalDelayMs() {
|
|
367
|
-
if (!this._lease) return
|
|
456
|
+
if (!this._lease) return M;
|
|
368
457
|
let e = Date.parse(this._lease.expiresAt);
|
|
369
|
-
if (!Number.isFinite(e)) return
|
|
458
|
+
if (!Number.isFinite(e)) return M;
|
|
370
459
|
let t = this._deps.now(), n = e - t;
|
|
371
|
-
if (n <= 0) return
|
|
372
|
-
let o = n * (1 -
|
|
373
|
-
return Math.max(
|
|
460
|
+
if (n <= 0) return M;
|
|
461
|
+
let o = n * (1 - qt);
|
|
462
|
+
return Math.max(M, Math.round(o));
|
|
374
463
|
}
|
|
375
464
|
_scheduleRenewal() {
|
|
376
465
|
if (this._closed) return;
|
|
@@ -397,8 +486,8 @@ var Rt, E, $, Me = T(() => {
|
|
|
397
486
|
} catch (t) {
|
|
398
487
|
this._renewalTimer = setTimeout(() => {
|
|
399
488
|
this._doRenew();
|
|
400
|
-
},
|
|
401
|
-
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${
|
|
489
|
+
}, M), console.warn(
|
|
490
|
+
`[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):`,
|
|
402
491
|
t instanceof Error ? t.message : t
|
|
403
492
|
);
|
|
404
493
|
return;
|
|
@@ -410,10 +499,13 @@ var Rt, E, $, Me = T(() => {
|
|
|
410
499
|
/**
|
|
411
500
|
* Recuperação REATIVA: chamado pelo wrapper resiliente (`resilient-pool.ts`)
|
|
412
501
|
* quando uma query plana falha num erro de ESTABELECIMENTO de conexão (cert
|
|
413
|
-
* TLS
|
|
414
|
-
* renovação que não disparou)
|
|
415
|
-
* do
|
|
416
|
-
*
|
|
502
|
+
* TLS de cliente mTLS do lease expirado no meio de uma request, ou timer de
|
|
503
|
+
* renovação que não disparou) ou de AUTENTICAÇÃO (senha rotacionada por
|
|
504
|
+
* `rotate-credentials` antes do próximo renew — SQLSTATE 28P01/28000, ou o
|
|
505
|
+
* 08P01+SASL do PgBouncer; também no `connect()` de uma transação). Emite um
|
|
506
|
+
* lease NOVO e faz o swap do pool interno do wrapper com a credencial e o
|
|
507
|
+
* certificado frescos (o pool velho é drenado com `end()`); o wrapper então
|
|
508
|
+
* re-tenta a operação 1x no pool novo.
|
|
417
509
|
*
|
|
418
510
|
* Vai DIRETO ao lease novo (sem `leaseId`) — a conexão falhou porque o
|
|
419
511
|
* certificado do lease atual está ruim, então `renew` (que depende do lease
|
|
@@ -429,7 +521,7 @@ var Rt, E, $, Me = T(() => {
|
|
|
429
521
|
*/
|
|
430
522
|
async _reactiveReLease() {
|
|
431
523
|
if (this._closed)
|
|
432
|
-
throw new
|
|
524
|
+
throw new f(
|
|
433
525
|
"db_unavailable",
|
|
434
526
|
"SqlLeaseManager foi fechado \u2014 re-lease reativo abortado."
|
|
435
527
|
);
|
|
@@ -466,7 +558,7 @@ var Rt, E, $, Me = T(() => {
|
|
|
466
558
|
*/
|
|
467
559
|
async _ensureInitialLease() {
|
|
468
560
|
if (this._closed)
|
|
469
|
-
throw new
|
|
561
|
+
throw new f(
|
|
470
562
|
"db_unavailable",
|
|
471
563
|
"SqlLeaseManager foi fechado \u2014 aquisi\xE7\xE3o de lease inicial abortada."
|
|
472
564
|
);
|
|
@@ -478,16 +570,16 @@ var Rt, E, $, Me = T(() => {
|
|
|
478
570
|
try {
|
|
479
571
|
t = this._deps.database !== void 0 ? await this._deps.fetchLease({ database: this._deps.database }) : await this._deps.fetchLease();
|
|
480
572
|
} catch (o) {
|
|
481
|
-
if (o instanceof
|
|
573
|
+
if (o instanceof f) throw o;
|
|
482
574
|
let i = o instanceof Error ? o.message : String(o);
|
|
483
|
-
throw new
|
|
575
|
+
throw new f("db_unavailable", `Falha ao obter lease inicial: ${i}`);
|
|
484
576
|
}
|
|
485
577
|
let n;
|
|
486
578
|
try {
|
|
487
579
|
n = this._deps.createPool(t);
|
|
488
580
|
} catch (o) {
|
|
489
581
|
let i = o instanceof Error ? o.message : String(o);
|
|
490
|
-
throw new
|
|
582
|
+
throw new f("db_unavailable", `Falha ao abrir pool: ${i}`);
|
|
491
583
|
}
|
|
492
584
|
return J(this._pool) ? this._pool.__neetruSwapInner(n).end().catch(() => {
|
|
493
585
|
}) : this._pool = n, this._lease = t, this._scheduleRenewal(), n;
|
|
@@ -505,7 +597,7 @@ var Rt, E, $, Me = T(() => {
|
|
|
505
597
|
* Exposto como método protegido para testes.
|
|
506
598
|
*/
|
|
507
599
|
async _swapPool(e) {
|
|
508
|
-
if (!
|
|
600
|
+
if (!Ft(this._lease, e)) {
|
|
509
601
|
this._lease = e;
|
|
510
602
|
return;
|
|
511
603
|
}
|
|
@@ -523,7 +615,7 @@ var Rt, E, $, Me = T(() => {
|
|
|
523
615
|
// ─── Helpers ───────────────────────────────────────────────────────────────
|
|
524
616
|
_assertOpen() {
|
|
525
617
|
if (this._closed)
|
|
526
|
-
throw new
|
|
618
|
+
throw new f(
|
|
527
619
|
"db_unavailable",
|
|
528
620
|
"SqlLeaseManager foi fechado \u2014 chame close() apenas no shutdown."
|
|
529
621
|
);
|
|
@@ -532,29 +624,29 @@ var Rt, E, $, Me = T(() => {
|
|
|
532
624
|
});
|
|
533
625
|
|
|
534
626
|
// src/db/sql/sql-client.ts
|
|
535
|
-
var
|
|
536
|
-
|
|
537
|
-
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () =>
|
|
538
|
-
DEFAULT_POOL_IDLE_MS: () =>
|
|
539
|
-
DEFAULT_POOL_MAX: () =>
|
|
540
|
-
LEASE_ENDPOINT: () =>
|
|
541
|
-
LEASE_RENEW_ENDPOINT: () =>
|
|
542
|
-
createDrizzleHandle: () =>
|
|
543
|
-
createHttpLeaseFetcher: () =>
|
|
544
|
-
createPgPool: () =>
|
|
545
|
-
createSqlClientFromConfig: () =>
|
|
546
|
-
createSqlClientWithDeps: () =>
|
|
627
|
+
var He = {};
|
|
628
|
+
me(He, {
|
|
629
|
+
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () => ze,
|
|
630
|
+
DEFAULT_POOL_IDLE_MS: () => Qe,
|
|
631
|
+
DEFAULT_POOL_MAX: () => Ve,
|
|
632
|
+
LEASE_ENDPOINT: () => $e,
|
|
633
|
+
LEASE_RENEW_ENDPOINT: () => Be,
|
|
634
|
+
createDrizzleHandle: () => jt,
|
|
635
|
+
createHttpLeaseFetcher: () => je,
|
|
636
|
+
createPgPool: () => Bt,
|
|
637
|
+
createSqlClientFromConfig: () => Qt,
|
|
638
|
+
createSqlClientWithDeps: () => Ke,
|
|
547
639
|
resolvePoolTuning: () => ue
|
|
548
640
|
});
|
|
549
|
-
function
|
|
641
|
+
function Wt(r) {
|
|
550
642
|
return r === "workspace" ? "staging" : r === "prod" ? "production" : "dev-local";
|
|
551
643
|
}
|
|
552
|
-
function
|
|
644
|
+
function je(r) {
|
|
553
645
|
return async (e) => {
|
|
554
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (W(), U)), n = !!e?.leaseId, o = n ?
|
|
646
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (W(), U)), n = !!e?.leaseId, o = n ? Be : $e, i;
|
|
555
647
|
n ? i = { leaseId: e.leaseId } : (i = {
|
|
556
648
|
productId: r.productId ?? "",
|
|
557
|
-
environment:
|
|
649
|
+
environment: Wt(r.env ?? "prod")
|
|
558
650
|
}, e?.database !== void 0 && (i.database = e.database));
|
|
559
651
|
let s;
|
|
560
652
|
try {
|
|
@@ -567,35 +659,35 @@ function Ae(r) {
|
|
|
567
659
|
retries: 2
|
|
568
660
|
});
|
|
569
661
|
} catch (a) {
|
|
570
|
-
let c = n ? "renovar" : "obter", l = a instanceof Error ? a.message : String(a), d = a instanceof
|
|
662
|
+
let c = n ? "renovar" : "obter", l = a instanceof Error ? a.message : String(a), d = a instanceof b ? a.reason : void 0;
|
|
571
663
|
if (d === "suspended" || d === "archived" || d === "revoked") {
|
|
572
664
|
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";
|
|
573
|
-
throw new
|
|
665
|
+
throw new f(
|
|
574
666
|
"db_suspended",
|
|
575
667
|
`Falha ao ${c} lease SQL: ${u} (reason=${d}).`
|
|
576
668
|
);
|
|
577
669
|
}
|
|
578
|
-
throw new
|
|
670
|
+
throw new f(
|
|
579
671
|
"db_unavailable",
|
|
580
672
|
`Falha ao ${c} lease SQL: ${l}`
|
|
581
673
|
);
|
|
582
674
|
}
|
|
583
|
-
return
|
|
675
|
+
return $t(s);
|
|
584
676
|
};
|
|
585
677
|
}
|
|
586
|
-
function
|
|
678
|
+
function $t(r) {
|
|
587
679
|
if (!r || typeof r != "object")
|
|
588
|
-
throw new
|
|
680
|
+
throw new f("db_unavailable", "Resposta de lease inv\xE1lida (n\xE3o \xE9 objeto).");
|
|
589
681
|
let t = r.lease;
|
|
590
682
|
if (!t || typeof t != "object")
|
|
591
|
-
throw new
|
|
683
|
+
throw new f(
|
|
592
684
|
"db_unavailable",
|
|
593
685
|
'Resposta de lease inv\xE1lida: campo "lease" ausente ou n\xE3o \xE9 objeto. O Core retorna { kind, lease: { leaseId, host, ... } }.'
|
|
594
686
|
);
|
|
595
687
|
let n = t;
|
|
596
688
|
function o(i, s) {
|
|
597
689
|
if (typeof n[i] !== s)
|
|
598
|
-
throw new
|
|
690
|
+
throw new f(
|
|
599
691
|
"db_unavailable",
|
|
600
692
|
`Campo obrigat\xF3rio "${i}" ausente ou tipo inv\xE1lido na resposta de lease.`
|
|
601
693
|
);
|
|
@@ -625,12 +717,12 @@ function ue() {
|
|
|
625
717
|
return {
|
|
626
718
|
// Teto 100: bem abaixo do `max_client_conn = 500` do PgBouncer, com folga
|
|
627
719
|
// para múltiplas instâncias do app dividirem o mesmo front-door.
|
|
628
|
-
max: de("NEETRU_DB_POOL_MAX",
|
|
720
|
+
max: de("NEETRU_DB_POOL_MAX", Ve, 1, 100),
|
|
629
721
|
// Teto 15min = janela do cert; piso 1s.
|
|
630
|
-
idleTimeoutMillis: de("NEETRU_DB_POOL_IDLE_MS",
|
|
722
|
+
idleTimeoutMillis: de("NEETRU_DB_POOL_IDLE_MS", Qe, 1e3, 9e5),
|
|
631
723
|
connectionTimeoutMillis: de(
|
|
632
724
|
"NEETRU_DB_POOL_CONNECT_TIMEOUT_MS",
|
|
633
|
-
|
|
725
|
+
ze,
|
|
634
726
|
1e3,
|
|
635
727
|
12e4
|
|
636
728
|
),
|
|
@@ -638,7 +730,7 @@ function ue() {
|
|
|
638
730
|
keepAliveInitialDelayMillis: 1e4
|
|
639
731
|
};
|
|
640
732
|
}
|
|
641
|
-
async function
|
|
733
|
+
async function Bt(r) {
|
|
642
734
|
let { Pool: e } = await import(
|
|
643
735
|
/* webpackIgnore: true */
|
|
644
736
|
'pg'
|
|
@@ -658,14 +750,14 @@ async function It(r) {
|
|
|
658
750
|
...ue()
|
|
659
751
|
});
|
|
660
752
|
}
|
|
661
|
-
async function
|
|
753
|
+
async function jt(r, e) {
|
|
662
754
|
let { drizzle: t } = await import(
|
|
663
755
|
/* webpackIgnore: true */
|
|
664
756
|
'drizzle-orm/node-postgres'
|
|
665
757
|
);
|
|
666
758
|
return t(r, { schema: e });
|
|
667
759
|
}
|
|
668
|
-
function
|
|
760
|
+
function Vt(r, e) {
|
|
669
761
|
let t = null, n = null, o = () => t ? Promise.resolve(t) : (n || (n = e().then((s) => (t = s, n = null, s)).catch((s) => {
|
|
670
762
|
throw n = null, s;
|
|
671
763
|
})), n), i = {
|
|
@@ -681,37 +773,37 @@ function Lt(r, e) {
|
|
|
681
773
|
};
|
|
682
774
|
return r && Object.setPrototypeOf(i, r), i;
|
|
683
775
|
}
|
|
684
|
-
async function
|
|
776
|
+
async function Ke(r, e) {
|
|
685
777
|
let t = e?.database !== void 0 ? { ...r, database: e.database } : r;
|
|
686
778
|
if (t.lazy) {
|
|
687
|
-
let c = null, l =
|
|
779
|
+
let c = null, l = Vt(t.poolPrototype ?? null, () => {
|
|
688
780
|
if (!c)
|
|
689
|
-
throw new
|
|
781
|
+
throw new f("db_unavailable", "SQL client n\xE3o inicializado (manager ausente).");
|
|
690
782
|
return c._ensureInitialLease();
|
|
691
783
|
}), d = ce(l), u;
|
|
692
784
|
try {
|
|
693
785
|
u = t.createDrizzle(d, t.schema);
|
|
694
|
-
} catch (
|
|
695
|
-
let
|
|
696
|
-
throw new
|
|
786
|
+
} catch (h) {
|
|
787
|
+
let _ = h instanceof Error ? h.message : String(h);
|
|
788
|
+
throw new f("db_unavailable", `Falha ao criar handle Drizzle: ${_}`);
|
|
697
789
|
}
|
|
698
|
-
let
|
|
699
|
-
return c =
|
|
790
|
+
let p = new $(null, d, u, t);
|
|
791
|
+
return c = p, d.__neetruSetReLease(() => p._reactiveReLease()), p;
|
|
700
792
|
}
|
|
701
793
|
let n;
|
|
702
794
|
try {
|
|
703
795
|
e?.database !== void 0 ? n = await t.fetchLease({ database: e.database }) : n = await t.fetchLease();
|
|
704
796
|
} catch (c) {
|
|
705
|
-
if (c instanceof
|
|
797
|
+
if (c instanceof f) throw c;
|
|
706
798
|
let l = c instanceof Error ? c.message : String(c);
|
|
707
|
-
throw new
|
|
799
|
+
throw new f("db_unavailable", `Falha ao obter lease inicial: ${l}`);
|
|
708
800
|
}
|
|
709
801
|
let o;
|
|
710
802
|
try {
|
|
711
803
|
o = t.createPool(n);
|
|
712
804
|
} catch (c) {
|
|
713
805
|
let l = c instanceof Error ? c.message : String(c);
|
|
714
|
-
throw new
|
|
806
|
+
throw new f("db_unavailable", `Falha ao abrir pool: ${l}`);
|
|
715
807
|
}
|
|
716
808
|
let i = ce(o), s;
|
|
717
809
|
try {
|
|
@@ -720,13 +812,13 @@ async function Ue(r, e) {
|
|
|
720
812
|
await o.end().catch(() => {
|
|
721
813
|
});
|
|
722
814
|
let l = c instanceof Error ? c.message : String(c);
|
|
723
|
-
throw new
|
|
815
|
+
throw new f("db_unavailable", `Falha ao criar handle Drizzle: ${l}`);
|
|
724
816
|
}
|
|
725
817
|
let a = new $(n, i, s, t);
|
|
726
818
|
return i.__neetruSetReLease(() => a._reactiveReLease()), a;
|
|
727
819
|
}
|
|
728
|
-
async function
|
|
729
|
-
let n =
|
|
820
|
+
async function Qt(r, e, t) {
|
|
821
|
+
let n = je(r), o, i;
|
|
730
822
|
try {
|
|
731
823
|
o = await import(
|
|
732
824
|
/* webpackIgnore: true */
|
|
@@ -736,13 +828,13 @@ async function Mt(r, e, t) {
|
|
|
736
828
|
'drizzle-orm/node-postgres'
|
|
737
829
|
);
|
|
738
830
|
} catch (l) {
|
|
739
|
-
throw new
|
|
831
|
+
throw new f(
|
|
740
832
|
"db_unavailable",
|
|
741
833
|
`[client.db.sql] requer pg + drizzle-orm como peerDependencies do servidor. Instale: npm install pg drizzle-orm. (${l.message})`
|
|
742
834
|
);
|
|
743
835
|
}
|
|
744
836
|
let s = o.Pool ?? o.default?.Pool;
|
|
745
|
-
return
|
|
837
|
+
return Ke(
|
|
746
838
|
{
|
|
747
839
|
fetchLease: n,
|
|
748
840
|
createPool: (l) => {
|
|
@@ -777,24 +869,24 @@ async function Mt(r, e, t) {
|
|
|
777
869
|
t
|
|
778
870
|
);
|
|
779
871
|
}
|
|
780
|
-
var
|
|
781
|
-
|
|
872
|
+
var $e, Be, Ve, Qe, ze, Ge = S(() => {
|
|
873
|
+
I();
|
|
782
874
|
x();
|
|
783
|
-
|
|
875
|
+
We();
|
|
784
876
|
le();
|
|
785
|
-
|
|
786
|
-
|
|
877
|
+
$e = "/api/sdk/v1/db/lease", Be = "/api/sdk/v1/db/lease/renew";
|
|
878
|
+
Ve = 10, Qe = 6e4, ze = 1e4;
|
|
787
879
|
});
|
|
788
880
|
|
|
789
881
|
// src/db/client-db.ts
|
|
790
|
-
|
|
882
|
+
I();
|
|
791
883
|
|
|
792
884
|
// src/db/offline/query-engine.ts
|
|
793
|
-
function
|
|
885
|
+
function _e(r) {
|
|
794
886
|
return r == null ? 0 : typeof r == "number" ? 1 : typeof r == "string" ? 2 : typeof r == "boolean" ? 3 : 4;
|
|
795
887
|
}
|
|
796
888
|
function A(r, e) {
|
|
797
|
-
let t =
|
|
889
|
+
let t = _e(r), n = _e(e);
|
|
798
890
|
if (t !== n) return t - n;
|
|
799
891
|
if (r == null) return 0;
|
|
800
892
|
if (typeof r == "number" && typeof e == "number") return r - e;
|
|
@@ -812,7 +904,7 @@ function Z(r, e) {
|
|
|
812
904
|
}
|
|
813
905
|
return n;
|
|
814
906
|
}
|
|
815
|
-
function
|
|
907
|
+
function et(r, e) {
|
|
816
908
|
let t = Z(r, e.field);
|
|
817
909
|
if (t === void 0) return false;
|
|
818
910
|
let { op: n, value: o } = e;
|
|
@@ -843,10 +935,10 @@ function ze(r, e) {
|
|
|
843
935
|
);
|
|
844
936
|
}
|
|
845
937
|
}
|
|
846
|
-
function
|
|
847
|
-
return e.every((t) =>
|
|
938
|
+
function tt(r, e) {
|
|
939
|
+
return e.every((t) => et(r, t));
|
|
848
940
|
}
|
|
849
|
-
function
|
|
941
|
+
function nt(r) {
|
|
850
942
|
return (e, t) => {
|
|
851
943
|
if (r) {
|
|
852
944
|
let n = Z(e.data, r.field), o = Z(t.data, r.field), i = A(n, o);
|
|
@@ -856,11 +948,11 @@ function He(r) {
|
|
|
856
948
|
return e.id < t.id ? -1 : e.id > t.id ? 1 : 0;
|
|
857
949
|
};
|
|
858
950
|
}
|
|
859
|
-
function
|
|
951
|
+
function rt(r, e) {
|
|
860
952
|
let t = r.findIndex((n) => n.id === e.docId);
|
|
861
953
|
return t === -1 ? r : e.type === "startAfter" ? r.slice(t + 1) : r.slice(0, t);
|
|
862
954
|
}
|
|
863
|
-
var
|
|
955
|
+
var P = class r {
|
|
864
956
|
/**
|
|
865
957
|
* Avalia um `QueryDescriptor` contra um array de `OfflineDoc`.
|
|
866
958
|
*
|
|
@@ -874,9 +966,9 @@ var C = class r {
|
|
|
874
966
|
evaluate(e, t) {
|
|
875
967
|
let n = e.filter((d) => !d.meta.deleted), o = t.where ?? [];
|
|
876
968
|
o.length > 0 && (n = n.filter(
|
|
877
|
-
(d) =>
|
|
969
|
+
(d) => tt(d.data, o)
|
|
878
970
|
));
|
|
879
|
-
let i =
|
|
971
|
+
let i = nt(t.orderBy), s = [...n].sort(i), a = t.cursor ? rt(s, t.cursor) : s, c = Math.min(t.limit ?? 20, 500);
|
|
880
972
|
return {
|
|
881
973
|
docs: a.slice(0, c).map((d) => ({ id: d.id, data: d.data })),
|
|
882
974
|
// `incomplete` é sempre true aqui — o QueryEngine não sabe se o cache
|
|
@@ -896,7 +988,7 @@ var C = class r {
|
|
|
896
988
|
};
|
|
897
989
|
|
|
898
990
|
// src/db/offline/local-store.ts
|
|
899
|
-
var
|
|
991
|
+
var it = 1, T = "documents", R = "mutations", N = "query_cache", j = "sync_meta", E = "conflict_log", V = class {
|
|
900
992
|
db = null;
|
|
901
993
|
dbName;
|
|
902
994
|
constructor(e) {
|
|
@@ -908,22 +1000,22 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
908
1000
|
* Idempotente — chamadas subsequentes são no-ops se o banco já está aberto.
|
|
909
1001
|
*/
|
|
910
1002
|
async open() {
|
|
911
|
-
this.db === null && (this.db = await openDB(this.dbName,
|
|
1003
|
+
this.db === null && (this.db = await openDB(this.dbName, it, {
|
|
912
1004
|
upgrade(e) {
|
|
913
|
-
if (!e.objectStoreNames.contains(
|
|
914
|
-
let t = e.createObjectStore(
|
|
1005
|
+
if (!e.objectStoreNames.contains(T)) {
|
|
1006
|
+
let t = e.createObjectStore(T, {
|
|
915
1007
|
keyPath: ["collection", "id"]
|
|
916
1008
|
});
|
|
917
1009
|
t.createIndex("by_collection", "collection"), t.createIndex("by_collection_state", ["collection", "meta.state"]), t.createIndex("by_updatedAtServer", "meta.updatedAtServer");
|
|
918
1010
|
}
|
|
919
|
-
if (!e.objectStoreNames.contains(
|
|
920
|
-
let t = e.createObjectStore(
|
|
1011
|
+
if (!e.objectStoreNames.contains(R)) {
|
|
1012
|
+
let t = e.createObjectStore(R, {
|
|
921
1013
|
keyPath: "mutationId"
|
|
922
1014
|
});
|
|
923
1015
|
t.createIndex("by_seq", "seq"), t.createIndex("by_status", "status"), t.createIndex("by_doc", ["collection", "docId"]), t.createIndex("by_batch", "batchId");
|
|
924
1016
|
}
|
|
925
|
-
if (e.objectStoreNames.contains(N) || e.createObjectStore(N, { keyPath: "queryHash" }), e.objectStoreNames.contains(
|
|
926
|
-
let t = e.createObjectStore(
|
|
1017
|
+
if (e.objectStoreNames.contains(N) || e.createObjectStore(N, { keyPath: "queryHash" }), e.objectStoreNames.contains(j) || e.createObjectStore(j, { keyPath: "key" }), !e.objectStoreNames.contains(E)) {
|
|
1018
|
+
let t = e.createObjectStore(E, {
|
|
927
1019
|
keyPath: "id",
|
|
928
1020
|
autoIncrement: true
|
|
929
1021
|
});
|
|
@@ -965,14 +1057,14 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
965
1057
|
* Retorna tombstones (deleted: true) — o chamador decide se deve mostrá-los.
|
|
966
1058
|
*/
|
|
967
1059
|
async getDoc(e, t) {
|
|
968
|
-
return await this.assertOpen().get(
|
|
1060
|
+
return await this.assertOpen().get(T, [e, t]) ?? null;
|
|
969
1061
|
}
|
|
970
1062
|
/**
|
|
971
1063
|
* Insere ou atualiza um documento na store `documents`.
|
|
972
1064
|
* O documento é identificado pelo keyPath composto `[collection, id]`.
|
|
973
1065
|
*/
|
|
974
1066
|
async putDoc(e) {
|
|
975
|
-
await this.assertOpen().put(
|
|
1067
|
+
await this.assertOpen().put(T, e);
|
|
976
1068
|
}
|
|
977
1069
|
/**
|
|
978
1070
|
* Marca um documento como tombstone (`deleted: true`).
|
|
@@ -980,8 +1072,8 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
980
1072
|
* No-op se o documento não existir.
|
|
981
1073
|
*/
|
|
982
1074
|
async deleteDoc(e, t) {
|
|
983
|
-
let n = this.assertOpen(), o = await n.get(
|
|
984
|
-
o && await n.put(
|
|
1075
|
+
let n = this.assertOpen(), o = await n.get(T, [e, t]);
|
|
1076
|
+
o && await n.put(T, {
|
|
985
1077
|
...o,
|
|
986
1078
|
meta: {
|
|
987
1079
|
...o.meta,
|
|
@@ -997,8 +1089,8 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
997
1089
|
* e delega a filtragem/ordenação/paginação ao QueryEngine (que opera em memória).
|
|
998
1090
|
*/
|
|
999
1091
|
async listDocs(e, t) {
|
|
1000
|
-
let o = await this.assertOpen().getAllFromIndex(
|
|
1001
|
-
return
|
|
1092
|
+
let o = await this.assertOpen().getAllFromIndex(T, "by_collection", e);
|
|
1093
|
+
return P.evaluate(o, t);
|
|
1002
1094
|
}
|
|
1003
1095
|
// ─── Sync meta (key-value) ──────────────────────────────────────────────────
|
|
1004
1096
|
/**
|
|
@@ -1009,14 +1101,14 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1009
1101
|
* 'lastFullResyncAt', 'leaderTabId'.
|
|
1010
1102
|
*/
|
|
1011
1103
|
async getMeta(e) {
|
|
1012
|
-
let n = await this.assertOpen().get(
|
|
1104
|
+
let n = await this.assertOpen().get(j, e);
|
|
1013
1105
|
return n ? n.value : null;
|
|
1014
1106
|
}
|
|
1015
1107
|
/**
|
|
1016
1108
|
* Armazena ou atualiza um valor na store `sync_meta`.
|
|
1017
1109
|
*/
|
|
1018
1110
|
async setMeta(e, t) {
|
|
1019
|
-
await this.assertOpen().put(
|
|
1111
|
+
await this.assertOpen().put(j, { key: e, value: t });
|
|
1020
1112
|
}
|
|
1021
1113
|
// ─── Conflict log ───────────────────────────────────────────────────────────
|
|
1022
1114
|
/**
|
|
@@ -1024,7 +1116,7 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1024
1116
|
* O `id` é autoIncrement — não deve ser fornecido pelo chamador.
|
|
1025
1117
|
*/
|
|
1026
1118
|
async appendConflict(e) {
|
|
1027
|
-
let n = await this.assertOpen().add(
|
|
1119
|
+
let n = await this.assertOpen().add(E, e);
|
|
1028
1120
|
return { ...e, id: n };
|
|
1029
1121
|
}
|
|
1030
1122
|
/**
|
|
@@ -1032,15 +1124,15 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1032
1124
|
* Filtra por `delivered` se a opção for fornecida.
|
|
1033
1125
|
*/
|
|
1034
1126
|
async listConflicts(e) {
|
|
1035
|
-
let n = await this.assertOpen().getAll(
|
|
1127
|
+
let n = await this.assertOpen().getAll(E);
|
|
1036
1128
|
return e?.delivered !== void 0 ? n.filter((o) => o.delivered === e.delivered) : n;
|
|
1037
1129
|
}
|
|
1038
1130
|
/**
|
|
1039
1131
|
* Marca um registro do `conflict_log` como entregue ao produto.
|
|
1040
1132
|
*/
|
|
1041
1133
|
async markConflictDelivered(e) {
|
|
1042
|
-
let t = this.assertOpen(), n = await t.get(
|
|
1043
|
-
n && await t.put(
|
|
1134
|
+
let t = this.assertOpen(), n = await t.get(E, e);
|
|
1135
|
+
n && await t.put(E, { ...n, delivered: true });
|
|
1044
1136
|
}
|
|
1045
1137
|
// ─── Mutations (acessores usados pelo MutationQueue) ────────────────────────
|
|
1046
1138
|
/**
|
|
@@ -1048,13 +1140,13 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1048
1140
|
* Key: `mutationId`.
|
|
1049
1141
|
*/
|
|
1050
1142
|
async putMutation(e) {
|
|
1051
|
-
await this.assertOpen().put(
|
|
1143
|
+
await this.assertOpen().put(R, e);
|
|
1052
1144
|
}
|
|
1053
1145
|
/**
|
|
1054
1146
|
* Retorna uma mutação pelo `mutationId`, ou `null` se não existir.
|
|
1055
1147
|
*/
|
|
1056
1148
|
async getMutation(e) {
|
|
1057
|
-
return await this.assertOpen().get(
|
|
1149
|
+
return await this.assertOpen().get(R, e) ?? null;
|
|
1058
1150
|
}
|
|
1059
1151
|
/**
|
|
1060
1152
|
* Lista mutações com filtros opcionais.
|
|
@@ -1062,14 +1154,14 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1062
1154
|
*/
|
|
1063
1155
|
async listMutations(e) {
|
|
1064
1156
|
let t = this.assertOpen(), n;
|
|
1065
|
-
return e?.status !== void 0 ? n = await t.getAllFromIndex(
|
|
1157
|
+
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);
|
|
1066
1158
|
}
|
|
1067
1159
|
/**
|
|
1068
1160
|
* Remove uma mutação pelo `mutationId`.
|
|
1069
1161
|
* No-op se não existir.
|
|
1070
1162
|
*/
|
|
1071
1163
|
async deleteMutation(e) {
|
|
1072
|
-
await this.assertOpen().delete(
|
|
1164
|
+
await this.assertOpen().delete(R, e);
|
|
1073
1165
|
}
|
|
1074
1166
|
// ─── Collection discovery ────────────────────────────────────────────────────
|
|
1075
1167
|
/**
|
|
@@ -1082,7 +1174,7 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1082
1174
|
* coletar os valores únicos de forma eficiente (sem carregar os docs completos).
|
|
1083
1175
|
*/
|
|
1084
1176
|
async listCollections() {
|
|
1085
|
-
let t = await this.assertOpen().getAllFromIndex(
|
|
1177
|
+
let t = await this.assertOpen().getAllFromIndex(T, "by_collection"), n = /* @__PURE__ */ new Set();
|
|
1086
1178
|
for (let o of t)
|
|
1087
1179
|
n.add(o.collection);
|
|
1088
1180
|
return Array.from(n);
|
|
@@ -1109,7 +1201,7 @@ var Ye = 1, w = "documents", S = "mutations", N = "query_cache", B = "sync_meta"
|
|
|
1109
1201
|
};
|
|
1110
1202
|
|
|
1111
1203
|
// src/db/offline/memory-store.ts
|
|
1112
|
-
var
|
|
1204
|
+
var Q = class {
|
|
1113
1205
|
// store `documents` — key: `${collection}\0${id}`
|
|
1114
1206
|
_docs = /* @__PURE__ */ new Map();
|
|
1115
1207
|
// store `mutations` — key: mutationId
|
|
@@ -1157,7 +1249,7 @@ var V = class {
|
|
|
1157
1249
|
let n = Array.from(this._docs.values()).filter(
|
|
1158
1250
|
(o) => o.collection === e
|
|
1159
1251
|
);
|
|
1160
|
-
return
|
|
1252
|
+
return P.evaluate(n, t);
|
|
1161
1253
|
}
|
|
1162
1254
|
// ─── Sync meta ──────────────────────────────────────────────────────────────
|
|
1163
1255
|
async getMeta(e) {
|
|
@@ -1220,7 +1312,7 @@ function ee() {
|
|
|
1220
1312
|
return (r === "x" ? e : e & 3 | 8).toString(16);
|
|
1221
1313
|
});
|
|
1222
1314
|
}
|
|
1223
|
-
var
|
|
1315
|
+
var z = class {
|
|
1224
1316
|
store;
|
|
1225
1317
|
/**
|
|
1226
1318
|
* Ponteiro de sequência local.
|
|
@@ -1318,11 +1410,11 @@ var Q = class {
|
|
|
1318
1410
|
async enqueue(e) {
|
|
1319
1411
|
let { collection: t, op: n, payload: o, baseVersion: i, batchId: s, upsert: a } = e, c = e.docId ?? (n === "add" ? ee() : "");
|
|
1320
1412
|
await this.syncSeq();
|
|
1321
|
-
let d = (await this.store.listMutations({ collection: t, docId: c })).filter((
|
|
1413
|
+
let d = (await this.store.listMutations({ collection: t, docId: c })).filter((h) => h.status === "queued").sort((h, _) => _.seq - h.seq)[0];
|
|
1322
1414
|
if (d !== void 0) {
|
|
1323
|
-
let
|
|
1324
|
-
if (
|
|
1325
|
-
if ("removed" in
|
|
1415
|
+
let h = this.tryCoalesce(d, n, o, s, a);
|
|
1416
|
+
if (h !== null) {
|
|
1417
|
+
if ("removed" in h)
|
|
1326
1418
|
return await this.store.deleteMutation(d.mutationId), {
|
|
1327
1419
|
seq: d.seq,
|
|
1328
1420
|
mutationId: ee(),
|
|
@@ -1337,11 +1429,11 @@ var Q = class {
|
|
|
1337
1429
|
status: "queued",
|
|
1338
1430
|
batchId: s
|
|
1339
1431
|
};
|
|
1340
|
-
let { coalesced:
|
|
1341
|
-
return await this.store.putMutation(
|
|
1432
|
+
let { coalesced: _ } = h;
|
|
1433
|
+
return await this.store.putMutation(_), _;
|
|
1342
1434
|
}
|
|
1343
1435
|
}
|
|
1344
|
-
let
|
|
1436
|
+
let p = {
|
|
1345
1437
|
seq: this.nextSeq(),
|
|
1346
1438
|
mutationId: ee(),
|
|
1347
1439
|
collection: t,
|
|
@@ -1357,7 +1449,7 @@ var Q = class {
|
|
|
1357
1449
|
// bug_0273e4dd: só persiste o flag quando verdadeiro (mantém o doc limpo).
|
|
1358
1450
|
...a ? { upsert: true } : {}
|
|
1359
1451
|
};
|
|
1360
|
-
return await this.store.putMutation(
|
|
1452
|
+
return await this.store.putMutation(p), p;
|
|
1361
1453
|
}
|
|
1362
1454
|
// ─── Leitura da fila ─────────────────────────────────────────────────────────
|
|
1363
1455
|
/**
|
|
@@ -1457,54 +1549,54 @@ var Q = class {
|
|
|
1457
1549
|
|
|
1458
1550
|
// src/db/field-value.ts
|
|
1459
1551
|
var te = "__neetru__";
|
|
1460
|
-
function
|
|
1552
|
+
function st() {
|
|
1461
1553
|
return { __neetru__: "serverTimestamp" };
|
|
1462
1554
|
}
|
|
1463
|
-
function
|
|
1555
|
+
function at(r) {
|
|
1464
1556
|
if (typeof r != "number" || !Number.isFinite(r))
|
|
1465
1557
|
throw new TypeError(
|
|
1466
1558
|
`increment(n): n deve ser um n\xFAmero finito (recebido: ${String(r)}).`
|
|
1467
1559
|
);
|
|
1468
1560
|
return { __neetru__: "increment", operand: r };
|
|
1469
1561
|
}
|
|
1470
|
-
function
|
|
1562
|
+
function D(r) {
|
|
1471
1563
|
return typeof r == "object" && r !== null && te in r && typeof r[te] == "string";
|
|
1472
1564
|
}
|
|
1473
|
-
function _e(r) {
|
|
1474
|
-
return R(r) && r.__neetru__ === "serverTimestamp";
|
|
1475
|
-
}
|
|
1476
1565
|
function ye(r) {
|
|
1477
|
-
return
|
|
1566
|
+
return D(r) && r.__neetru__ === "serverTimestamp";
|
|
1478
1567
|
}
|
|
1479
|
-
function
|
|
1480
|
-
return
|
|
1568
|
+
function ge(r) {
|
|
1569
|
+
return D(r) && r.__neetru__ === "increment" && typeof r.operand == "number" && Number.isFinite(r.operand);
|
|
1570
|
+
}
|
|
1571
|
+
function ct(r, e) {
|
|
1572
|
+
return ye(r) ? Date.now() : ge(r) ? (typeof e == "number" && Number.isFinite(e) ? e : 0) + r.operand : r;
|
|
1481
1573
|
}
|
|
1482
|
-
function
|
|
1574
|
+
function k(r, e) {
|
|
1483
1575
|
let t = false, n = {};
|
|
1484
1576
|
for (let [o, i] of Object.entries(r))
|
|
1485
|
-
|
|
1577
|
+
D(i) ? (t = true, n[o] = ct(i, e?.[o])) : n[o] = i;
|
|
1486
1578
|
return t ? n : r;
|
|
1487
1579
|
}
|
|
1488
1580
|
function ne(r) {
|
|
1489
1581
|
for (let e of Object.values(r))
|
|
1490
|
-
if (
|
|
1582
|
+
if (D(e)) return true;
|
|
1491
1583
|
return false;
|
|
1492
1584
|
}
|
|
1493
1585
|
|
|
1494
1586
|
// src/db/offline/sync-engine.ts
|
|
1495
|
-
var
|
|
1496
|
-
function
|
|
1587
|
+
var lt = 8, dt = 1e3, ut = 300 * 1e3, re = "truncatedCollections";
|
|
1588
|
+
function pt(r) {
|
|
1497
1589
|
return r !== null && ne(r);
|
|
1498
1590
|
}
|
|
1499
|
-
function
|
|
1591
|
+
function ft(r) {
|
|
1500
1592
|
if (r === null) return null;
|
|
1501
1593
|
if (!ne(r)) return r;
|
|
1502
1594
|
let e = {};
|
|
1503
1595
|
for (let [t, n] of Object.entries(r))
|
|
1504
|
-
|
|
1596
|
+
D(n) || (e[t] = n);
|
|
1505
1597
|
return e;
|
|
1506
1598
|
}
|
|
1507
|
-
var
|
|
1599
|
+
var K = class {
|
|
1508
1600
|
_store;
|
|
1509
1601
|
_queue;
|
|
1510
1602
|
_resolver;
|
|
@@ -1698,13 +1790,13 @@ var z = class {
|
|
|
1698
1790
|
}), s.push({ type: "removed", collection: e, doc: { id: t, data: a.data } }));
|
|
1699
1791
|
return;
|
|
1700
1792
|
}
|
|
1701
|
-
let l =
|
|
1793
|
+
let l = ft(i.payload) ?? a?.data ?? {}, d = {
|
|
1702
1794
|
collection: e,
|
|
1703
1795
|
id: t,
|
|
1704
1796
|
data: i.op === "update" ? { ...a?.data ?? {}, ...l } : (
|
|
1705
1797
|
// add/set: se o payload tinha sentinels, o cache otimista (existing.data)
|
|
1706
1798
|
// é a fonte de verdade até o pull; senão usa o payload literal.
|
|
1707
|
-
|
|
1799
|
+
pt(i.payload) ? { ...a?.data ?? {}, ...l } : l
|
|
1708
1800
|
),
|
|
1709
1801
|
meta: {
|
|
1710
1802
|
serverVersion: n,
|
|
@@ -1857,7 +1949,7 @@ var z = class {
|
|
|
1857
1949
|
* pending do doc no cache + registra `lastError`/dispara `onError`.
|
|
1858
1950
|
*/
|
|
1859
1951
|
async _retryOrDeadLetter(e, t, n) {
|
|
1860
|
-
if (e.attempts + 1 <
|
|
1952
|
+
if (e.attempts + 1 < lt) {
|
|
1861
1953
|
await this._queue.markRetry(e.mutationId, t);
|
|
1862
1954
|
return;
|
|
1863
1955
|
}
|
|
@@ -1898,7 +1990,7 @@ var z = class {
|
|
|
1898
1990
|
}
|
|
1899
1991
|
/** Backoff exponencial keyed em `attempts`, com teto `BACKOFF_CAP_MS`. */
|
|
1900
1992
|
_backoffDelayMs(e) {
|
|
1901
|
-
return Math.min(
|
|
1993
|
+
return Math.min(dt * 2 ** (e - 1), ut);
|
|
1902
1994
|
}
|
|
1903
1995
|
// ─── FASE 2 — pull (reconciliar o cache) ─────────────────────────────────────
|
|
1904
1996
|
/**
|
|
@@ -2150,10 +2242,10 @@ var z = class {
|
|
|
2150
2242
|
};
|
|
2151
2243
|
|
|
2152
2244
|
// src/db/offline/conflict-resolver.ts
|
|
2153
|
-
function
|
|
2245
|
+
function be(r, e) {
|
|
2154
2246
|
return e === null ? false : r === null ? true : e > r;
|
|
2155
2247
|
}
|
|
2156
|
-
var
|
|
2248
|
+
var H = class r {
|
|
2157
2249
|
/**
|
|
2158
2250
|
* Resolve um conflito com base na operação da mutação (auto-dispatch).
|
|
2159
2251
|
*
|
|
@@ -2187,7 +2279,7 @@ var K = class r {
|
|
|
2187
2279
|
resolvedData: t.data,
|
|
2188
2280
|
conflictRecord: null
|
|
2189
2281
|
};
|
|
2190
|
-
if (!
|
|
2282
|
+
if (!be(
|
|
2191
2283
|
e.meta.updatedAtServer,
|
|
2192
2284
|
t.meta.updatedAtServer
|
|
2193
2285
|
))
|
|
@@ -2234,7 +2326,7 @@ var K = class r {
|
|
|
2234
2326
|
let i = t.data;
|
|
2235
2327
|
if (e === null)
|
|
2236
2328
|
return { outcome: "no_conflict", resolvedData: { ...i, ...n.payload ?? {} }, conflictRecord: null };
|
|
2237
|
-
if (!
|
|
2329
|
+
if (!be(
|
|
2238
2330
|
e.meta.updatedAtServer,
|
|
2239
2331
|
t.meta.updatedAtServer
|
|
2240
2332
|
))
|
|
@@ -2312,10 +2404,10 @@ var K = class r {
|
|
|
2312
2404
|
};
|
|
2313
2405
|
|
|
2314
2406
|
// src/db/offline/change-bus.ts
|
|
2315
|
-
function
|
|
2407
|
+
function ht(r) {
|
|
2316
2408
|
return `${r.collection}::${r.doc.id}`;
|
|
2317
2409
|
}
|
|
2318
|
-
var
|
|
2410
|
+
var G = class {
|
|
2319
2411
|
/** Listeners filtrados por coleção. */
|
|
2320
2412
|
_collectionListeners = /* @__PURE__ */ new Map();
|
|
2321
2413
|
/** Listeners globais (todas as coleções). */
|
|
@@ -2360,7 +2452,7 @@ var H = class {
|
|
|
2360
2452
|
if (e.length === 0) return;
|
|
2361
2453
|
let t = /* @__PURE__ */ new Map();
|
|
2362
2454
|
for (let i of e)
|
|
2363
|
-
t.set(
|
|
2455
|
+
t.set(ht(i), i);
|
|
2364
2456
|
let n = Array.from(t.values()), o = /* @__PURE__ */ new Map();
|
|
2365
2457
|
for (let i of n)
|
|
2366
2458
|
o.has(i.collection) || o.set(i.collection, []), o.get(i.collection).push(i);
|
|
@@ -2399,7 +2491,7 @@ var H = class {
|
|
|
2399
2491
|
};
|
|
2400
2492
|
|
|
2401
2493
|
// src/db/offline/connectivity-monitor.ts
|
|
2402
|
-
var
|
|
2494
|
+
var X = class {
|
|
2403
2495
|
_state;
|
|
2404
2496
|
_listeners = /* @__PURE__ */ new Set();
|
|
2405
2497
|
_nav;
|
|
@@ -2678,26 +2770,26 @@ var q = class {
|
|
|
2678
2770
|
};
|
|
2679
2771
|
|
|
2680
2772
|
// src/db/collection-ref.ts
|
|
2681
|
-
|
|
2682
|
-
function
|
|
2773
|
+
I();
|
|
2774
|
+
function mt() {
|
|
2683
2775
|
return typeof indexedDB > "u" || typeof window > "u";
|
|
2684
2776
|
}
|
|
2685
|
-
var
|
|
2686
|
-
function
|
|
2687
|
-
if (!
|
|
2688
|
-
throw new
|
|
2777
|
+
var ve = /^[a-z0-9][a-z0-9_-]{0,62}$/;
|
|
2778
|
+
function _t(r) {
|
|
2779
|
+
if (!ve.test(r))
|
|
2780
|
+
throw new f(
|
|
2689
2781
|
"db_invalid_query",
|
|
2690
|
-
`Nome de cole\xE7\xE3o inv\xE1lido: "${r}". Deve seguir o padr\xE3o ${
|
|
2782
|
+
`Nome de cole\xE7\xE3o inv\xE1lido: "${r}". Deve seguir o padr\xE3o ${ve}.`
|
|
2691
2783
|
);
|
|
2692
2784
|
}
|
|
2693
|
-
function
|
|
2785
|
+
function L(r, e = "id") {
|
|
2694
2786
|
if (!r || typeof r != "string" || r.trim() === "")
|
|
2695
|
-
throw new
|
|
2787
|
+
throw new f(
|
|
2696
2788
|
"db_invalid_query",
|
|
2697
2789
|
`${e} \xE9 obrigat\xF3rio e deve ser uma string n\xE3o-vazia.`
|
|
2698
2790
|
);
|
|
2699
2791
|
}
|
|
2700
|
-
function
|
|
2792
|
+
function yt(r) {
|
|
2701
2793
|
let e = {};
|
|
2702
2794
|
if (r?.where && (e.where = r.where.map((t) => ({
|
|
2703
2795
|
field: t.field,
|
|
@@ -2714,7 +2806,7 @@ function lt(r) {
|
|
|
2714
2806
|
}
|
|
2715
2807
|
return e;
|
|
2716
2808
|
}
|
|
2717
|
-
function
|
|
2809
|
+
function gt(r, e) {
|
|
2718
2810
|
if (r.length < e) return null;
|
|
2719
2811
|
let t = r[r.length - 1];
|
|
2720
2812
|
return t ? btoa(JSON.stringify({ type: "startAfter", docId: t.id })) : null;
|
|
@@ -2761,11 +2853,11 @@ var oe = class {
|
|
|
2761
2853
|
}
|
|
2762
2854
|
/** Constrói um DbListResult para a coleção com query opcional. */
|
|
2763
2855
|
async _buildListResult(e) {
|
|
2764
|
-
let t =
|
|
2856
|
+
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;
|
|
2765
2857
|
return {
|
|
2766
|
-
docs: o.docs.map((
|
|
2858
|
+
docs: o.docs.map((h) => ({ id: h.id, data: h.data })),
|
|
2767
2859
|
nextCursor: c,
|
|
2768
|
-
fromCache:
|
|
2860
|
+
fromCache: p,
|
|
2769
2861
|
stale: u,
|
|
2770
2862
|
hasPendingWrites: i,
|
|
2771
2863
|
changes: [],
|
|
@@ -2774,13 +2866,13 @@ var oe = class {
|
|
|
2774
2866
|
}
|
|
2775
2867
|
// ── CRUD ────────────────────────────────────────────────────────────────────
|
|
2776
2868
|
async get(e) {
|
|
2777
|
-
return
|
|
2869
|
+
return L(e), this._buildGetResult(e);
|
|
2778
2870
|
}
|
|
2779
2871
|
async list(e) {
|
|
2780
2872
|
return this._buildListResult(e);
|
|
2781
2873
|
}
|
|
2782
2874
|
async add(e) {
|
|
2783
|
-
let t = e, n =
|
|
2875
|
+
let t = e, n = k(t, void 0), o = await this._queue.enqueue({
|
|
2784
2876
|
collection: this._collection,
|
|
2785
2877
|
op: "add",
|
|
2786
2878
|
payload: t,
|
|
@@ -2807,8 +2899,8 @@ var oe = class {
|
|
|
2807
2899
|
}]), { ok: true, id: i };
|
|
2808
2900
|
}
|
|
2809
2901
|
async set(e, t) {
|
|
2810
|
-
|
|
2811
|
-
let n = await this._store.getDoc(this._collection, e), o = t, i =
|
|
2902
|
+
L(e);
|
|
2903
|
+
let n = await this._store.getDoc(this._collection, e), o = t, i = k(o, void 0), s = await this._queue.enqueue({
|
|
2812
2904
|
collection: this._collection,
|
|
2813
2905
|
docId: e,
|
|
2814
2906
|
op: "set",
|
|
@@ -2836,8 +2928,8 @@ var oe = class {
|
|
|
2836
2928
|
}]), { ok: true };
|
|
2837
2929
|
}
|
|
2838
2930
|
async update(e, t, n) {
|
|
2839
|
-
|
|
2840
|
-
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a =
|
|
2931
|
+
L(e);
|
|
2932
|
+
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({
|
|
2841
2933
|
collection: this._collection,
|
|
2842
2934
|
docId: e,
|
|
2843
2935
|
op: "update",
|
|
@@ -2870,7 +2962,7 @@ var oe = class {
|
|
|
2870
2962
|
}]), { ok: true };
|
|
2871
2963
|
}
|
|
2872
2964
|
async remove(e) {
|
|
2873
|
-
|
|
2965
|
+
L(e);
|
|
2874
2966
|
let t = await this._store.getDoc(this._collection, e);
|
|
2875
2967
|
return !t || t.meta.deleted, await this._queue.enqueue({
|
|
2876
2968
|
collection: this._collection,
|
|
@@ -2890,7 +2982,7 @@ var oe = class {
|
|
|
2890
2982
|
for (let o of e) {
|
|
2891
2983
|
let i = this._collection;
|
|
2892
2984
|
if (o.kind === "add") {
|
|
2893
|
-
let s = o.data, a =
|
|
2985
|
+
let s = o.data, a = k(s, void 0), c = await this._queue.enqueue({
|
|
2894
2986
|
collection: i,
|
|
2895
2987
|
op: "add",
|
|
2896
2988
|
payload: s,
|
|
@@ -2912,7 +3004,7 @@ var oe = class {
|
|
|
2912
3004
|
}
|
|
2913
3005
|
}), n.push({ type: "added", collection: i, doc: { id: l, data: a } });
|
|
2914
3006
|
} else if (o.kind === "set") {
|
|
2915
|
-
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), l =
|
|
3007
|
+
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), l = k(a, void 0), d = await this._queue.enqueue({
|
|
2916
3008
|
collection: i,
|
|
2917
3009
|
docId: s,
|
|
2918
3010
|
op: "set",
|
|
@@ -2939,7 +3031,7 @@ var oe = class {
|
|
|
2939
3031
|
doc: { id: s, data: l }
|
|
2940
3032
|
});
|
|
2941
3033
|
} else if (o.kind === "update") {
|
|
2942
|
-
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), l = c?.meta.deleted ? void 0 : c?.data, d =
|
|
3034
|
+
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({
|
|
2943
3035
|
collection: i,
|
|
2944
3036
|
docId: s,
|
|
2945
3037
|
op: "update",
|
|
@@ -2960,7 +3052,7 @@ var oe = class {
|
|
|
2960
3052
|
state: "pending",
|
|
2961
3053
|
pendingMutationIds: [
|
|
2962
3054
|
...c?.meta.pendingMutationIds ?? [],
|
|
2963
|
-
|
|
3055
|
+
p.mutationId
|
|
2964
3056
|
],
|
|
2965
3057
|
deleted: false,
|
|
2966
3058
|
ownerId: c?.meta.ownerId ?? null
|
|
@@ -2982,7 +3074,7 @@ var oe = class {
|
|
|
2982
3074
|
}
|
|
2983
3075
|
// ── Realtime ─────────────────────────────────────────────────────────────────
|
|
2984
3076
|
onDoc(e, t) {
|
|
2985
|
-
return
|
|
3077
|
+
return L(e), this._buildGetResult(e).then((n) => {
|
|
2986
3078
|
t(n ? n.docs[0]?.data ?? null : null);
|
|
2987
3079
|
}).catch(() => t(null)), this._bus.subscribe(this._collection, (n) => {
|
|
2988
3080
|
let o = n.find((i) => i.doc.id === e);
|
|
@@ -3041,7 +3133,7 @@ var oe = class {
|
|
|
3041
3133
|
};
|
|
3042
3134
|
}
|
|
3043
3135
|
doc(e) {
|
|
3044
|
-
|
|
3136
|
+
L(e);
|
|
3045
3137
|
let t = this;
|
|
3046
3138
|
return {
|
|
3047
3139
|
get id() {
|
|
@@ -3104,7 +3196,7 @@ var oe = class {
|
|
|
3104
3196
|
this._store = e.store, this._queue = e.queue, this._bus = e.bus, this._engine = e.engine, this._connectivity = e.connectivity;
|
|
3105
3197
|
}
|
|
3106
3198
|
collection(e) {
|
|
3107
|
-
return
|
|
3199
|
+
return _t(e), new oe(
|
|
3108
3200
|
e,
|
|
3109
3201
|
this._store,
|
|
3110
3202
|
this._queue,
|
|
@@ -3133,10 +3225,10 @@ var oe = class {
|
|
|
3133
3225
|
return this._engine.onError(e);
|
|
3134
3226
|
}
|
|
3135
3227
|
};
|
|
3136
|
-
async function
|
|
3137
|
-
let e =
|
|
3228
|
+
async function we(r) {
|
|
3229
|
+
let e = mt() ? new Q() : new V(r.dbName);
|
|
3138
3230
|
await e.open();
|
|
3139
|
-
let t = new
|
|
3231
|
+
let t = new z(e), n = new H(), o = new G(), i = new X({
|
|
3140
3232
|
// Se startOnline=false, injeta um navigator falso
|
|
3141
3233
|
navigator: r.startOnline === false ? { onLine: false } : typeof navigator < "u" ? navigator : void 0
|
|
3142
3234
|
}), s = r.singleTab ? (
|
|
@@ -3149,7 +3241,7 @@ async function ve(r) {
|
|
|
3149
3241
|
) : new q({
|
|
3150
3242
|
lockName: `neetru-offline:${r.dbName}`,
|
|
3151
3243
|
channelName: `neetru-offline:${r.dbName}`
|
|
3152
|
-
}), a = new
|
|
3244
|
+
}), a = new K({
|
|
3153
3245
|
store: e,
|
|
3154
3246
|
queue: t,
|
|
3155
3247
|
resolver: n,
|
|
@@ -3169,9 +3261,9 @@ async function ve(r) {
|
|
|
3169
3261
|
}
|
|
3170
3262
|
|
|
3171
3263
|
// src/db/realtime/realtime-client.ts
|
|
3172
|
-
var
|
|
3173
|
-
function
|
|
3174
|
-
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++
|
|
3264
|
+
var bt = 0;
|
|
3265
|
+
function Se() {
|
|
3266
|
+
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++bt}-${Math.random().toString(36).slice(2)}`;
|
|
3175
3267
|
}
|
|
3176
3268
|
var F = class {
|
|
3177
3269
|
// ── configuração ──────────────────────────────────────────────────────────
|
|
@@ -3211,7 +3303,7 @@ var F = class {
|
|
|
3211
3303
|
_pendingFrames = [];
|
|
3212
3304
|
// ── construtor ────────────────────────────────────────────────────────────
|
|
3213
3305
|
constructor(e) {
|
|
3214
|
-
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ??
|
|
3306
|
+
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();
|
|
3215
3307
|
}
|
|
3216
3308
|
// ── API pública ───────────────────────────────────────────────────────────
|
|
3217
3309
|
/**
|
|
@@ -3225,8 +3317,8 @@ var F = class {
|
|
|
3225
3317
|
*/
|
|
3226
3318
|
subscribe(e, t, n) {
|
|
3227
3319
|
if (this._closed)
|
|
3228
|
-
return
|
|
3229
|
-
let o =
|
|
3320
|
+
return Se();
|
|
3321
|
+
let o = Se();
|
|
3230
3322
|
this._subscriptions.set(o, {
|
|
3231
3323
|
subscriptionId: o,
|
|
3232
3324
|
collection: e,
|
|
@@ -3490,7 +3582,7 @@ var F = class {
|
|
|
3490
3582
|
_detachHandlers(e) {
|
|
3491
3583
|
e.onopen = null, e.onmessage = null, e.onclose = null, e.onerror = null;
|
|
3492
3584
|
}
|
|
3493
|
-
},
|
|
3585
|
+
}, vt = (r) => {
|
|
3494
3586
|
if (typeof WebSocket > "u")
|
|
3495
3587
|
throw new Error(
|
|
3496
3588
|
"[NeetruRealtimeClient] WebSocket n\xE3o dispon\xEDvel neste ambiente. Injete uma implementa\xE7\xE3o via `webSocketFactory` nas op\xE7\xF5es."
|
|
@@ -3499,16 +3591,16 @@ var F = class {
|
|
|
3499
3591
|
};
|
|
3500
3592
|
|
|
3501
3593
|
// src/db/client-db.ts
|
|
3502
|
-
|
|
3503
|
-
var
|
|
3504
|
-
function
|
|
3505
|
-
return e._transport ? e._transport : r === "firestore" && e.firestoreTransport ? e.firestoreTransport : r === "nosql-vm" && e.realtimeGatewayUrl ?
|
|
3594
|
+
I();
|
|
3595
|
+
var zt = (r) => `/api/sdk/v1/datastore/${encodeURIComponent(r)}`, Y = (r, e) => `/api/sdk/v1/datastore/${encodeURIComponent(r)}/${encodeURIComponent(e)}`, Xe = 200;
|
|
3596
|
+
function Kt(r, e, t) {
|
|
3597
|
+
return e._transport ? e._transport : r === "firestore" && e.firestoreTransport ? e.firestoreTransport : r === "nosql-vm" && e.realtimeGatewayUrl ? Ht(e.realtimeGatewayUrl, t, e) : Je(t);
|
|
3506
3598
|
}
|
|
3507
|
-
function
|
|
3599
|
+
function Je(r) {
|
|
3508
3600
|
return {
|
|
3509
3601
|
async pushMutations(e) {
|
|
3510
3602
|
if (!r)
|
|
3511
|
-
throw new
|
|
3603
|
+
throw new f(
|
|
3512
3604
|
"db_unavailable",
|
|
3513
3605
|
"[RestSyncTransport] config n\xE3o dispon\xEDvel \u2014 n\xE3o \xE9 poss\xEDvel enviar ao Core. Inicialize o transporte via createNeetruDb."
|
|
3514
3606
|
);
|
|
@@ -3554,18 +3646,18 @@ function je(r) {
|
|
|
3554
3646
|
} catch (a) {
|
|
3555
3647
|
let c = a instanceof Error ? a.message : String(a), l = a?.code, d = a?.status;
|
|
3556
3648
|
if (l === "forbidden" || l === "unauthorized" || l === "not_found" || l === "conflict" || l === "validation_failed" || typeof d == "number" && d >= 400 && d < 500 && d !== 408 && d !== 429) {
|
|
3557
|
-
let
|
|
3649
|
+
let p = l === "forbidden" || l === "unauthorized" ? "rejected_permission" : "rejected_validation";
|
|
3558
3650
|
n.push({
|
|
3559
3651
|
mutationId: s.mutationId,
|
|
3560
3652
|
outcome: "rejected",
|
|
3561
|
-
reason:
|
|
3653
|
+
reason: p,
|
|
3562
3654
|
serverVersion: null,
|
|
3563
3655
|
serverData: null
|
|
3564
3656
|
});
|
|
3565
3657
|
continue;
|
|
3566
3658
|
}
|
|
3567
3659
|
if (n.length === 0)
|
|
3568
|
-
throw new
|
|
3660
|
+
throw new f("db_unavailable", `pushMutations falhou: ${c}`);
|
|
3569
3661
|
break;
|
|
3570
3662
|
}
|
|
3571
3663
|
return { results: n };
|
|
@@ -3582,7 +3674,7 @@ function je(r) {
|
|
|
3582
3674
|
try {
|
|
3583
3675
|
l = await t(r, {
|
|
3584
3676
|
method: "GET",
|
|
3585
|
-
path:
|
|
3677
|
+
path: zt(c),
|
|
3586
3678
|
requireAuth: !0,
|
|
3587
3679
|
retries: 2,
|
|
3588
3680
|
headers: o,
|
|
@@ -3590,37 +3682,37 @@ function je(r) {
|
|
|
3590
3682
|
// TRUNCADAS — reportadas em `truncatedCollections` p/ o SyncEngine
|
|
3591
3683
|
// (a) NÃO rodar deleção-por-ausência e (b) sinalizar
|
|
3592
3684
|
// `DbListResult.truncated`. Paginação por cursor é tarefa Core (gated).
|
|
3593
|
-
query: { limit:
|
|
3685
|
+
query: { limit: Xe }
|
|
3594
3686
|
});
|
|
3595
|
-
} catch (
|
|
3596
|
-
let
|
|
3597
|
-
throw new
|
|
3687
|
+
} catch (p) {
|
|
3688
|
+
let h = p instanceof Error ? p.message : String(p);
|
|
3689
|
+
throw new f(
|
|
3598
3690
|
"db_unavailable",
|
|
3599
|
-
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${c}": ${
|
|
3691
|
+
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${c}": ${h}`
|
|
3600
3692
|
);
|
|
3601
3693
|
}
|
|
3602
3694
|
let d = l, u = Array.isArray(d?.items) ? d.items : [];
|
|
3603
|
-
u.length >=
|
|
3604
|
-
for (let
|
|
3605
|
-
if (!
|
|
3606
|
-
let
|
|
3607
|
-
if (!
|
|
3608
|
-
let { id:
|
|
3609
|
-
if (typeof
|
|
3610
|
-
|
|
3611
|
-
else if (
|
|
3612
|
-
|
|
3613
|
-
else if (typeof
|
|
3614
|
-
let
|
|
3615
|
-
Number.isNaN(
|
|
3695
|
+
u.length >= Xe && a.push(c);
|
|
3696
|
+
for (let p of u) {
|
|
3697
|
+
if (!p || typeof p != "object") continue;
|
|
3698
|
+
let h = p, _ = typeof h.id == "string" ? h.id : null;
|
|
3699
|
+
if (!_) continue;
|
|
3700
|
+
let { id: y, ...B } = h, g = i, v = B._updatedAt;
|
|
3701
|
+
if (typeof v == "number")
|
|
3702
|
+
g = v;
|
|
3703
|
+
else if (v !== null && typeof v == "object" && typeof v._seconds == "number")
|
|
3704
|
+
g = v._seconds * 1e3;
|
|
3705
|
+
else if (typeof v == "string") {
|
|
3706
|
+
let C = Date.parse(v);
|
|
3707
|
+
Number.isNaN(C) || (g = C);
|
|
3616
3708
|
}
|
|
3617
|
-
let
|
|
3709
|
+
let w = `rest_${_}_${g}`;
|
|
3618
3710
|
s.push({
|
|
3619
3711
|
collection: c,
|
|
3620
|
-
id:
|
|
3621
|
-
data:
|
|
3622
|
-
serverVersion:
|
|
3623
|
-
serverTimestamp:
|
|
3712
|
+
id: _,
|
|
3713
|
+
data: B,
|
|
3714
|
+
serverVersion: w,
|
|
3715
|
+
serverTimestamp: g,
|
|
3624
3716
|
deleted: false
|
|
3625
3717
|
});
|
|
3626
3718
|
}
|
|
@@ -3629,10 +3721,10 @@ function je(r) {
|
|
|
3629
3721
|
}
|
|
3630
3722
|
};
|
|
3631
3723
|
}
|
|
3632
|
-
function
|
|
3633
|
-
let n =
|
|
3724
|
+
function Ht(r, e, t) {
|
|
3725
|
+
let n = Je(e), o = new F({
|
|
3634
3726
|
gatewayUrl: r,
|
|
3635
|
-
ticketProvider:
|
|
3727
|
+
ticketProvider: Gt(e, t),
|
|
3636
3728
|
dbId: t.dbId,
|
|
3637
3729
|
webSocketFactory: t._wsFactory
|
|
3638
3730
|
});
|
|
@@ -3668,7 +3760,7 @@ function At(r, e, t) {
|
|
|
3668
3760
|
}
|
|
3669
3761
|
};
|
|
3670
3762
|
}
|
|
3671
|
-
function
|
|
3763
|
+
function Gt(r, e) {
|
|
3672
3764
|
return async () => {
|
|
3673
3765
|
let { httpRequest: t } = await Promise.resolve().then(() => (W(), U));
|
|
3674
3766
|
return await t(r, {
|
|
@@ -3683,27 +3775,27 @@ function Nt(r, e) {
|
|
|
3683
3775
|
});
|
|
3684
3776
|
};
|
|
3685
3777
|
}
|
|
3686
|
-
function
|
|
3778
|
+
function Gn(r, e, t, n) {
|
|
3687
3779
|
return new F({ gatewayUrl: r, ticketProvider: e, dbId: t, webSocketFactory: n });
|
|
3688
3780
|
}
|
|
3689
3781
|
var pe = false;
|
|
3690
|
-
function
|
|
3782
|
+
function Xn() {
|
|
3691
3783
|
pe = false;
|
|
3692
3784
|
}
|
|
3693
|
-
function
|
|
3785
|
+
function Xt(r, e) {
|
|
3694
3786
|
pe || e.dbId && r.env !== "prod" && (typeof window > "u" || typeof console > "u" || typeof console.warn != "function" || (pe = true, console.warn(
|
|
3695
3787
|
'[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.'
|
|
3696
3788
|
)));
|
|
3697
3789
|
}
|
|
3698
|
-
function
|
|
3699
|
-
|
|
3790
|
+
function Jn(r, e = {}) {
|
|
3791
|
+
Xt(r, e);
|
|
3700
3792
|
let t;
|
|
3701
3793
|
e.transport !== void 0 ? t = e.transport : e.engine !== void 0 ? (console.warn(
|
|
3702
3794
|
'[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).'
|
|
3703
3795
|
), t = e.engine) : t = "rest";
|
|
3704
|
-
let n =
|
|
3796
|
+
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;
|
|
3705
3797
|
function c() {
|
|
3706
|
-
return s || (s =
|
|
3798
|
+
return s || (s = we({
|
|
3707
3799
|
dbName: i,
|
|
3708
3800
|
transport: n,
|
|
3709
3801
|
singleTab: e.singleTab ?? r.env !== "prod"
|
|
@@ -3711,15 +3803,15 @@ function xn(r, e = {}) {
|
|
|
3711
3803
|
}
|
|
3712
3804
|
return {
|
|
3713
3805
|
collection(l) {
|
|
3714
|
-
return a ? a.collection(l) :
|
|
3806
|
+
return a ? a.collection(l) : Jt(l, c);
|
|
3715
3807
|
},
|
|
3716
3808
|
async sql(l, d) {
|
|
3717
3809
|
if (r.env === "dev")
|
|
3718
|
-
throw new
|
|
3810
|
+
throw new f(
|
|
3719
3811
|
"db_unavailable",
|
|
3720
3812
|
"[SDK] db.sql() n\xE3o dispon\xEDvel em NEETRU_ENV=dev. Use `neetru dev` para subir o container Postgres local."
|
|
3721
3813
|
);
|
|
3722
|
-
return (await Promise.resolve().then(() => (
|
|
3814
|
+
return (await Promise.resolve().then(() => (Ge(), He))).createSqlClientFromConfig(r, l, d);
|
|
3723
3815
|
},
|
|
3724
3816
|
get syncState() {
|
|
3725
3817
|
return a?.syncState ?? {
|
|
@@ -3732,8 +3824,8 @@ function xn(r, e = {}) {
|
|
|
3732
3824
|
onSyncStateChanged(l) {
|
|
3733
3825
|
if (a) return a.onSyncStateChanged(l);
|
|
3734
3826
|
let d = null, u = false;
|
|
3735
|
-
return c().then((
|
|
3736
|
-
u || (d =
|
|
3827
|
+
return c().then((p) => {
|
|
3828
|
+
u || (d = p.onSyncStateChanged(l));
|
|
3737
3829
|
}), () => {
|
|
3738
3830
|
u = true, d?.();
|
|
3739
3831
|
};
|
|
@@ -3750,15 +3842,15 @@ function xn(r, e = {}) {
|
|
|
3750
3842
|
onError(l) {
|
|
3751
3843
|
if (a) return a.onError(l);
|
|
3752
3844
|
let d = null, u = false;
|
|
3753
|
-
return c().then((
|
|
3754
|
-
u || (d =
|
|
3845
|
+
return c().then((p) => {
|
|
3846
|
+
u || (d = p.onError(l));
|
|
3755
3847
|
}), () => {
|
|
3756
3848
|
u = true, d?.();
|
|
3757
3849
|
};
|
|
3758
3850
|
}
|
|
3759
3851
|
};
|
|
3760
3852
|
}
|
|
3761
|
-
function
|
|
3853
|
+
function Jt(r, e) {
|
|
3762
3854
|
async function t() {
|
|
3763
3855
|
return (await e()).collection(r);
|
|
3764
3856
|
}
|
|
@@ -3836,4 +3928,4 @@ function Ft(r, e) {
|
|
|
3836
3928
|
};
|
|
3837
3929
|
}
|
|
3838
3930
|
|
|
3839
|
-
export { te as NEETRU_SENTINEL_KEY,
|
|
3931
|
+
export { te as NEETRU_SENTINEL_KEY, f as NeetruDbError, Xn as __resetDbIdWarningForTests, Jn as createNeetruDb, Je as createRestSyncTransport, Gn as getWebSocketRealtimeClient, at as increment, D as isFieldSentinel, ge as isIncrementSentinel, ye as isServerTimestampSentinel, st as serverTimestamp };
|