@neetru/sdk 3.8.1 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +4 -1
- package/dist/ai.cjs +1 -1
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +1 -1
- package/dist/auth.cjs +844 -803
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +841 -800
- package/dist/catalog.cjs +1 -1
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +1 -1
- package/dist/checkout.cjs +62 -49
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +62 -49
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db.cjs +200 -187
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +193 -180
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/index.cjs +880 -839
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +847 -806
- package/dist/mobile.cjs +328 -276
- package/dist/mobile.d.cts +12 -2
- package/dist/mobile.d.ts +12 -2
- package/dist/mobile.mjs +328 -276
- package/dist/mocks.cjs +5 -5
- package/dist/mocks.d.cts +2 -2
- package/dist/mocks.d.ts +2 -2
- package/dist/mocks.mjs +4 -4
- package/dist/notifications.cjs +1 -1
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +1 -1
- package/dist/react.cjs +119 -119
- package/dist/react.d.cts +8 -2
- package/dist/react.d.ts +8 -2
- package/dist/react.mjs +119 -119
- package/dist/support.cjs +1 -1
- package/dist/support.d.cts +1 -1
- package/dist/support.d.ts +1 -1
- package/dist/support.mjs +1 -1
- package/dist/telemetry.cjs +62 -49
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +62 -49
- package/dist/{types-yv3EDKg2.d.cts → types-Bl_4HROd.d.cts} +62 -3
- package/dist/{types-BYPk_3HR.d.ts → types-Bqnsqvow.d.ts} +62 -3
- package/dist/usage.cjs +1 -1
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +1 -1
- package/dist/webhooks.cjs +1 -1
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +1 -1
- package/package.json +9 -1
package/dist/db.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { openDB } from 'idb';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var Ze = Object.defineProperty;
|
|
4
4
|
var S = (r, e) => () => (r && (e = r(r = 0)), e);
|
|
5
5
|
var me = (r, e) => {
|
|
6
6
|
for (var t in e)
|
|
7
|
-
|
|
7
|
+
Ze(r, t, { get: e[t], enumerable: true });
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
// src/errors.ts
|
|
@@ -36,9 +36,9 @@ var b, x = S(() => {
|
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
// src/db-errors.ts
|
|
39
|
-
var
|
|
39
|
+
var et, f, I = S(() => {
|
|
40
40
|
x();
|
|
41
|
-
|
|
41
|
+
et = /* @__PURE__ */ new Set([
|
|
42
42
|
"db_unavailable",
|
|
43
43
|
"db_conflict",
|
|
44
44
|
"db_timeout"
|
|
@@ -56,13 +56,13 @@ var Ze, f, I = S(() => {
|
|
|
56
56
|
*/
|
|
57
57
|
dbId;
|
|
58
58
|
constructor(e, t, n) {
|
|
59
|
-
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable =
|
|
59
|
+
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable = et.has(e), this.dbId = n, Object.setPrototypeOf(this, r.prototype);
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
62
|
});
|
|
63
63
|
|
|
64
64
|
// src/idempotency.ts
|
|
65
|
-
function
|
|
65
|
+
function Re() {
|
|
66
66
|
let r = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
67
67
|
if (r && typeof r.randomUUID == "function")
|
|
68
68
|
return r.randomUUID();
|
|
@@ -71,15 +71,15 @@ function Te() {
|
|
|
71
71
|
"crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
|
|
72
72
|
);
|
|
73
73
|
}
|
|
74
|
-
var
|
|
74
|
+
var De = S(() => {
|
|
75
75
|
x();
|
|
76
76
|
});
|
|
77
77
|
|
|
78
78
|
// src/correlation.ts
|
|
79
|
-
function
|
|
80
|
-
return typeof r == "string" && r.length > 0 && r.length <= 128 &&
|
|
79
|
+
function Ce(r) {
|
|
80
|
+
return typeof r == "string" && r.length > 0 && r.length <= 128 && Tt.test(r);
|
|
81
81
|
}
|
|
82
|
-
function
|
|
82
|
+
function Rt() {
|
|
83
83
|
let r = globalThis.crypto;
|
|
84
84
|
try {
|
|
85
85
|
if (r && typeof r.randomUUID == "function") return r.randomUUID();
|
|
@@ -97,24 +97,24 @@ function St() {
|
|
|
97
97
|
let n = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join("");
|
|
98
98
|
return `${n.slice(0, 8)}-${n.slice(8, 12)}-${n.slice(12, 16)}-${n.slice(16, 20)}-${n.slice(20)}`;
|
|
99
99
|
}
|
|
100
|
-
function
|
|
100
|
+
function Ie(r, e) {
|
|
101
101
|
let t, n = {};
|
|
102
102
|
for (let [i, s] of Object.entries(r)) {
|
|
103
|
-
if (i.toLowerCase() ===
|
|
104
|
-
|
|
103
|
+
if (i.toLowerCase() === ke) {
|
|
104
|
+
Ce(s) && (t = s);
|
|
105
105
|
continue;
|
|
106
106
|
}
|
|
107
107
|
n[i] = s;
|
|
108
108
|
}
|
|
109
|
-
let o = t ?? (
|
|
110
|
-
return n[
|
|
109
|
+
let o = t ?? (Ce(e) ? e : void 0) ?? Rt();
|
|
110
|
+
return n[ke] = o, { headers: n, correlationId: o };
|
|
111
111
|
}
|
|
112
|
-
var
|
|
113
|
-
|
|
112
|
+
var ke, Tt, Pe = S(() => {
|
|
113
|
+
ke = "x-correlation-id", Tt = /^[A-Za-z0-9._:\-]+$/;
|
|
114
114
|
});
|
|
115
115
|
|
|
116
116
|
// src/server-only-key.ts
|
|
117
|
-
function
|
|
117
|
+
function Ee() {
|
|
118
118
|
try {
|
|
119
119
|
let r = globalThis.navigator;
|
|
120
120
|
if (r && r.product === "ReactNative") return "react-native";
|
|
@@ -127,20 +127,20 @@ function Pe() {
|
|
|
127
127
|
}
|
|
128
128
|
return null;
|
|
129
129
|
}
|
|
130
|
-
var
|
|
130
|
+
var Le = S(() => {
|
|
131
131
|
});
|
|
132
132
|
|
|
133
133
|
// src/http.ts
|
|
134
134
|
var U = {};
|
|
135
135
|
me(U, {
|
|
136
|
-
checkCoreVersionCompat: () =>
|
|
137
|
-
httpRequest: () =>
|
|
138
|
-
statusToCode: () =>
|
|
136
|
+
checkCoreVersionCompat: () => Fe,
|
|
137
|
+
httpRequest: () => At,
|
|
138
|
+
statusToCode: () => Ue
|
|
139
139
|
});
|
|
140
140
|
function se(r) {
|
|
141
141
|
return r.split("+")[0] ?? r;
|
|
142
142
|
}
|
|
143
|
-
function
|
|
143
|
+
function kt(r, e) {
|
|
144
144
|
let t = se(r).split(".").map(Number), n = se(e).split(".").map(Number), o = Math.max(t.length, n.length);
|
|
145
145
|
for (let i = 0; i < o; i++) {
|
|
146
146
|
let s = t[i] ?? 0, a = n[i] ?? 0;
|
|
@@ -148,62 +148,62 @@ function Rt(r, e) {
|
|
|
148
148
|
}
|
|
149
149
|
return 0;
|
|
150
150
|
}
|
|
151
|
-
function
|
|
151
|
+
function qe() {
|
|
152
152
|
try {
|
|
153
|
-
return "3.
|
|
153
|
+
return "3.9.0";
|
|
154
154
|
} catch {
|
|
155
155
|
return;
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
|
-
function
|
|
159
|
-
let r =
|
|
158
|
+
function Ct() {
|
|
159
|
+
let r = qe();
|
|
160
160
|
return r ? parseInt(r.split(".")[0] ?? "3", 10) : 3;
|
|
161
161
|
}
|
|
162
|
-
function
|
|
162
|
+
function Fe(r) {
|
|
163
163
|
if (!r) return;
|
|
164
|
-
let e =
|
|
164
|
+
let e = Ct(), t = Dt[e];
|
|
165
165
|
if (!t) return;
|
|
166
166
|
let n = se(r);
|
|
167
|
-
if (
|
|
167
|
+
if (kt(n, t) < 0) {
|
|
168
168
|
let o = `${e}:${n}`;
|
|
169
|
-
|
|
169
|
+
Me.has(o) || (Me.add(o), console.warn(
|
|
170
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.`
|
|
171
171
|
));
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
|
-
function
|
|
174
|
+
function It() {
|
|
175
175
|
let r = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
176
|
-
return
|
|
176
|
+
return Ee() ? "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
177
|
}
|
|
178
|
-
function
|
|
178
|
+
function Oe(r) {
|
|
179
179
|
let e = 200 * Math.pow(4, r), t = e * 0.2 * (Math.random() * 2 - 1);
|
|
180
180
|
return Math.max(50, Math.round(e + t));
|
|
181
181
|
}
|
|
182
|
-
function
|
|
182
|
+
function Ot(r) {
|
|
183
183
|
if (!r) return null;
|
|
184
184
|
let e = Number(r);
|
|
185
|
-
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), xe);
|
|
186
186
|
let t = Date.parse(r);
|
|
187
187
|
if (Number.isFinite(t)) {
|
|
188
188
|
let n = t - Date.now();
|
|
189
|
-
if (n > 0) return Math.min(n,
|
|
189
|
+
if (n > 0) return Math.min(n, xe);
|
|
190
190
|
}
|
|
191
191
|
return null;
|
|
192
192
|
}
|
|
193
|
-
function
|
|
193
|
+
function Ae(r) {
|
|
194
194
|
return new Promise((e) => setTimeout(e, r));
|
|
195
195
|
}
|
|
196
|
-
function
|
|
196
|
+
function Ue(r) {
|
|
197
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";
|
|
198
198
|
}
|
|
199
|
-
function
|
|
199
|
+
function xt(r, e, t) {
|
|
200
200
|
let n = r.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, i = new URL(`${n}${o}`);
|
|
201
201
|
if (t)
|
|
202
202
|
for (let [s, a] of Object.entries(t))
|
|
203
203
|
a !== void 0 && i.searchParams.set(s, String(a));
|
|
204
204
|
return i.toString();
|
|
205
205
|
}
|
|
206
|
-
async function
|
|
206
|
+
async function Ne(r) {
|
|
207
207
|
let e = await r.text();
|
|
208
208
|
if (e)
|
|
209
209
|
try {
|
|
@@ -212,18 +212,18 @@ async function Ae(r) {
|
|
|
212
212
|
return;
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
|
-
async function
|
|
216
|
-
let t = e.method ?? "GET", n =
|
|
215
|
+
async function At(r, e) {
|
|
216
|
+
let t = e.method ?? "GET", n = xt(r.baseUrl, e.path, e.query), o = Lt.has(t) ? Pt : Et, i = e.retries ?? o, { headers: s, correlationId: a } = Ie(
|
|
217
217
|
{ accept: "application/json", ...e.headers },
|
|
218
218
|
e.correlationId
|
|
219
|
-
), c =
|
|
219
|
+
), c = qe();
|
|
220
220
|
if (c && (s["x-neetru-sdk-version"] = c), e.requireAuth) {
|
|
221
221
|
if (!r.apiKey)
|
|
222
|
-
throw new b("missing_api_key",
|
|
222
|
+
throw new b("missing_api_key", It());
|
|
223
223
|
s.authorization = `Bearer ${r.apiKey}`;
|
|
224
224
|
}
|
|
225
225
|
if (e.idempotencyKey) {
|
|
226
|
-
let p = typeof e.idempotencyKey == "string" ? e.idempotencyKey :
|
|
226
|
+
let p = typeof e.idempotencyKey == "string" ? e.idempotencyKey : Re();
|
|
227
227
|
s["idempotency-key"] = p;
|
|
228
228
|
}
|
|
229
229
|
let l = e.body !== void 0 && t !== "GET" && t !== "DELETE" ? JSON.stringify(e.body) : void 0;
|
|
@@ -249,9 +249,9 @@ async function Ot(r, e) {
|
|
|
249
249
|
} catch (m) {
|
|
250
250
|
let O = m instanceof DOMException && m.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${m instanceof Error ? m.message : "fetch failed"}`;
|
|
251
251
|
if (d = new b("network_error", O, void 0, void 0, void 0, a), p < i) {
|
|
252
|
-
let he =
|
|
252
|
+
let he = Oe(p);
|
|
253
253
|
if (u == null || Date.now() + he < u) {
|
|
254
|
-
await
|
|
254
|
+
await Ae(he);
|
|
255
255
|
continue;
|
|
256
256
|
}
|
|
257
257
|
}
|
|
@@ -260,9 +260,9 @@ async function Ot(r, e) {
|
|
|
260
260
|
let B = y.headers.get("x-request-id") ?? y.headers.get("x-correlation-id") ?? void 0;
|
|
261
261
|
if (y.ok) {
|
|
262
262
|
let m = y.headers.get("x-neetru-version");
|
|
263
|
-
return m &&
|
|
263
|
+
return m && Fe(m), await Ne(y);
|
|
264
264
|
}
|
|
265
|
-
let g = await
|
|
265
|
+
let g = await Ne(y), v = Ue(y.status), w = `HTTP ${y.status}`, C;
|
|
266
266
|
if (g && typeof g == "object") {
|
|
267
267
|
let m = g.error;
|
|
268
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);
|
|
@@ -272,10 +272,10 @@ async function Ot(r, e) {
|
|
|
272
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}.`);
|
|
273
273
|
}
|
|
274
274
|
let fe = new b(v, w, y.status, B, C, a);
|
|
275
|
-
if (d = fe,
|
|
276
|
-
let O =
|
|
275
|
+
if (d = fe, Mt.has(v) && p < i) {
|
|
276
|
+
let O = Ot(y.headers.get("retry-after")) ?? Oe(p);
|
|
277
277
|
if (u == null || Date.now() + O < u) {
|
|
278
|
-
await
|
|
278
|
+
await Ae(O);
|
|
279
279
|
continue;
|
|
280
280
|
}
|
|
281
281
|
}
|
|
@@ -283,30 +283,30 @@ async function Ot(r, e) {
|
|
|
283
283
|
}
|
|
284
284
|
throw d ?? new b("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, a);
|
|
285
285
|
}
|
|
286
|
-
var
|
|
286
|
+
var Dt, Me, Pt, Et, Lt, Mt, xe, W = S(() => {
|
|
287
287
|
x();
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
288
|
+
De();
|
|
289
|
+
Pe();
|
|
290
|
+
Le();
|
|
291
|
+
Dt = {
|
|
292
292
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
293
293
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
294
294
|
3: "2026.05.1"
|
|
295
295
|
};
|
|
296
|
-
|
|
297
|
-
|
|
296
|
+
Me = /* @__PURE__ */ new Set();
|
|
297
|
+
Pt = 2, Et = 0, Lt = /* @__PURE__ */ new Set(["GET", "HEAD"]), Mt = /* @__PURE__ */ new Set([
|
|
298
298
|
"rate_limited",
|
|
299
299
|
"server_error",
|
|
300
300
|
"network_error"
|
|
301
301
|
]);
|
|
302
|
-
|
|
302
|
+
xe = 6e4;
|
|
303
303
|
});
|
|
304
304
|
|
|
305
305
|
// src/db/sql/resilient-pool.ts
|
|
306
306
|
function J(r) {
|
|
307
307
|
return typeof r == "object" && r !== null && r[ae] === true;
|
|
308
308
|
}
|
|
309
|
-
function
|
|
309
|
+
function Nt(r) {
|
|
310
310
|
let e = r && typeof r == "object" && "code" in r ? r.code : void 0;
|
|
311
311
|
if (e === "ECONNREFUSED" || e === "ETIMEDOUT" || e === "ENOTFOUND" || e === "EHOSTUNREACH" || e === "EAI_AGAIN")
|
|
312
312
|
return true;
|
|
@@ -318,15 +318,15 @@ function xt(r) {
|
|
|
318
318
|
let n = (r instanceof Error ? r.message : typeof r == "string" ? r : "").toLowerCase();
|
|
319
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"));
|
|
320
320
|
}
|
|
321
|
-
function
|
|
321
|
+
function We(r) {
|
|
322
322
|
let e = r && typeof r == "object" && "code" in r ? r.code : void 0;
|
|
323
|
-
if (typeof e == "string" &&
|
|
323
|
+
if (typeof e == "string" && qt.has(e.toUpperCase()))
|
|
324
324
|
return true;
|
|
325
325
|
let n = (r instanceof Error ? r.message : typeof r == "string" ? r : "").toLowerCase();
|
|
326
326
|
return n.length === 0 ? false : n.includes("password authentication failed") || n.includes("sasl authentication failed");
|
|
327
327
|
}
|
|
328
|
-
function
|
|
329
|
-
return
|
|
328
|
+
function Ft(r) {
|
|
329
|
+
return Nt(r) || We(r);
|
|
330
330
|
}
|
|
331
331
|
function ce(r) {
|
|
332
332
|
let e = { current: r }, t = null, n = {
|
|
@@ -356,7 +356,7 @@ function ce(r) {
|
|
|
356
356
|
}
|
|
357
357
|
return await o(d, e.current, p);
|
|
358
358
|
}
|
|
359
|
-
})(), s = i("query",
|
|
359
|
+
})(), s = i("query", Ft), a = i("connect", We), c = Object.create(Object.getPrototypeOf(r));
|
|
360
360
|
return new Proxy(c, {
|
|
361
361
|
get(d, u) {
|
|
362
362
|
if (u === ae) return true;
|
|
@@ -375,16 +375,16 @@ function ce(r) {
|
|
|
375
375
|
}
|
|
376
376
|
});
|
|
377
377
|
}
|
|
378
|
-
var ae,
|
|
378
|
+
var ae, qt, le = S(() => {
|
|
379
379
|
ae = /* @__PURE__ */ Symbol.for("neetru.sdk.resilientPool");
|
|
380
|
-
|
|
380
|
+
qt = /* @__PURE__ */ new Set(["28P01", "28000"]);
|
|
381
381
|
});
|
|
382
382
|
|
|
383
383
|
// src/db/sql/lease.ts
|
|
384
|
-
function
|
|
384
|
+
function Wt(r, e) {
|
|
385
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;
|
|
386
386
|
}
|
|
387
|
-
function
|
|
387
|
+
function $t(r) {
|
|
388
388
|
if (r instanceof f) return r;
|
|
389
389
|
let e = r instanceof Error ? r.message : String(r), t = r instanceof Error ? r.code : void 0;
|
|
390
390
|
return typeof t == "string" && (t === "ECONNREFUSED" || t === "ENOTFOUND" || t === "ETIMEDOUT") ? new f(
|
|
@@ -392,10 +392,10 @@ function Ut(r) {
|
|
|
392
392
|
`Pool connection failed (${t}): ${e}`
|
|
393
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}`);
|
|
394
394
|
}
|
|
395
|
-
var
|
|
395
|
+
var Ut, M, $, $e = S(() => {
|
|
396
396
|
I();
|
|
397
397
|
le();
|
|
398
|
-
|
|
398
|
+
Ut = 0.8, M = 3e4, $ = class {
|
|
399
399
|
/**
|
|
400
400
|
* Lease atual. `null` no modo LAZY antes da 1ª query (o 1º lease é adiado —
|
|
401
401
|
* ver `_ensureInitialLease`). Após a 1ª aquisição, sempre não-nulo.
|
|
@@ -435,7 +435,7 @@ var qt, M, $, We = S(() => {
|
|
|
435
435
|
isolationLevel: t.isolationLevel
|
|
436
436
|
}) : await this._orm.transaction(e);
|
|
437
437
|
} catch (n) {
|
|
438
|
-
throw
|
|
438
|
+
throw $t(n);
|
|
439
439
|
}
|
|
440
440
|
}
|
|
441
441
|
async close() {
|
|
@@ -458,7 +458,7 @@ var qt, M, $, We = S(() => {
|
|
|
458
458
|
if (!Number.isFinite(e)) return M;
|
|
459
459
|
let t = this._deps.now(), n = e - t;
|
|
460
460
|
if (n <= 0) return M;
|
|
461
|
-
let o = n * (1 -
|
|
461
|
+
let o = n * (1 - Ut);
|
|
462
462
|
return Math.max(M, Math.round(o));
|
|
463
463
|
}
|
|
464
464
|
_scheduleRenewal() {
|
|
@@ -597,7 +597,7 @@ var qt, M, $, We = S(() => {
|
|
|
597
597
|
* Exposto como método protegido para testes.
|
|
598
598
|
*/
|
|
599
599
|
async _swapPool(e) {
|
|
600
|
-
if (!
|
|
600
|
+
if (!Wt(this._lease, e)) {
|
|
601
601
|
this._lease = e;
|
|
602
602
|
return;
|
|
603
603
|
}
|
|
@@ -624,29 +624,29 @@ var qt, M, $, We = S(() => {
|
|
|
624
624
|
});
|
|
625
625
|
|
|
626
626
|
// src/db/sql/sql-client.ts
|
|
627
|
-
var
|
|
628
|
-
me(
|
|
629
|
-
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () =>
|
|
630
|
-
DEFAULT_POOL_IDLE_MS: () =>
|
|
631
|
-
DEFAULT_POOL_MAX: () =>
|
|
632
|
-
LEASE_ENDPOINT: () =>
|
|
633
|
-
LEASE_RENEW_ENDPOINT: () =>
|
|
634
|
-
createDrizzleHandle: () =>
|
|
627
|
+
var Ge = {};
|
|
628
|
+
me(Ge, {
|
|
629
|
+
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () => Ke,
|
|
630
|
+
DEFAULT_POOL_IDLE_MS: () => ze,
|
|
631
|
+
DEFAULT_POOL_MAX: () => Qe,
|
|
632
|
+
LEASE_ENDPOINT: () => Be,
|
|
633
|
+
LEASE_RENEW_ENDPOINT: () => Ve,
|
|
634
|
+
createDrizzleHandle: () => Qt,
|
|
635
635
|
createHttpLeaseFetcher: () => je,
|
|
636
|
-
createPgPool: () =>
|
|
637
|
-
createSqlClientFromConfig: () =>
|
|
638
|
-
createSqlClientWithDeps: () =>
|
|
636
|
+
createPgPool: () => jt,
|
|
637
|
+
createSqlClientFromConfig: () => Kt,
|
|
638
|
+
createSqlClientWithDeps: () => He,
|
|
639
639
|
resolvePoolTuning: () => ue
|
|
640
640
|
});
|
|
641
|
-
function
|
|
641
|
+
function Bt(r) {
|
|
642
642
|
return r === "workspace" ? "staging" : r === "prod" ? "production" : "dev-local";
|
|
643
643
|
}
|
|
644
644
|
function je(r) {
|
|
645
645
|
return async (e) => {
|
|
646
|
-
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 ? Ve : Be, i;
|
|
647
647
|
n ? i = { leaseId: e.leaseId } : (i = {
|
|
648
648
|
productId: r.productId ?? "",
|
|
649
|
-
environment:
|
|
649
|
+
environment: Bt(r.env ?? "prod")
|
|
650
650
|
}, e?.database !== void 0 && (i.database = e.database));
|
|
651
651
|
let s;
|
|
652
652
|
try {
|
|
@@ -672,10 +672,10 @@ function je(r) {
|
|
|
672
672
|
`Falha ao ${c} lease SQL: ${l}`
|
|
673
673
|
);
|
|
674
674
|
}
|
|
675
|
-
return
|
|
675
|
+
return Vt(s);
|
|
676
676
|
};
|
|
677
677
|
}
|
|
678
|
-
function
|
|
678
|
+
function Vt(r) {
|
|
679
679
|
if (!r || typeof r != "object")
|
|
680
680
|
throw new f("db_unavailable", "Resposta de lease inv\xE1lida (n\xE3o \xE9 objeto).");
|
|
681
681
|
let t = r.lease;
|
|
@@ -717,12 +717,12 @@ function ue() {
|
|
|
717
717
|
return {
|
|
718
718
|
// Teto 100: bem abaixo do `max_client_conn = 500` do PgBouncer, com folga
|
|
719
719
|
// para múltiplas instâncias do app dividirem o mesmo front-door.
|
|
720
|
-
max: de("NEETRU_DB_POOL_MAX",
|
|
720
|
+
max: de("NEETRU_DB_POOL_MAX", Qe, 1, 100),
|
|
721
721
|
// Teto 15min = janela do cert; piso 1s.
|
|
722
|
-
idleTimeoutMillis: de("NEETRU_DB_POOL_IDLE_MS",
|
|
722
|
+
idleTimeoutMillis: de("NEETRU_DB_POOL_IDLE_MS", ze, 1e3, 9e5),
|
|
723
723
|
connectionTimeoutMillis: de(
|
|
724
724
|
"NEETRU_DB_POOL_CONNECT_TIMEOUT_MS",
|
|
725
|
-
|
|
725
|
+
Ke,
|
|
726
726
|
1e3,
|
|
727
727
|
12e4
|
|
728
728
|
),
|
|
@@ -730,7 +730,7 @@ function ue() {
|
|
|
730
730
|
keepAliveInitialDelayMillis: 1e4
|
|
731
731
|
};
|
|
732
732
|
}
|
|
733
|
-
async function
|
|
733
|
+
async function jt(r) {
|
|
734
734
|
let { Pool: e } = await import(
|
|
735
735
|
/* webpackIgnore: true */
|
|
736
736
|
'pg'
|
|
@@ -750,14 +750,14 @@ async function Bt(r) {
|
|
|
750
750
|
...ue()
|
|
751
751
|
});
|
|
752
752
|
}
|
|
753
|
-
async function
|
|
753
|
+
async function Qt(r, e) {
|
|
754
754
|
let { drizzle: t } = await import(
|
|
755
755
|
/* webpackIgnore: true */
|
|
756
756
|
'drizzle-orm/node-postgres'
|
|
757
757
|
);
|
|
758
758
|
return t(r, { schema: e });
|
|
759
759
|
}
|
|
760
|
-
function
|
|
760
|
+
function zt(r, e) {
|
|
761
761
|
let t = null, n = null, o = () => t ? Promise.resolve(t) : (n || (n = e().then((s) => (t = s, n = null, s)).catch((s) => {
|
|
762
762
|
throw n = null, s;
|
|
763
763
|
})), n), i = {
|
|
@@ -773,10 +773,10 @@ function Vt(r, e) {
|
|
|
773
773
|
};
|
|
774
774
|
return r && Object.setPrototypeOf(i, r), i;
|
|
775
775
|
}
|
|
776
|
-
async function
|
|
776
|
+
async function He(r, e) {
|
|
777
777
|
let t = e?.database !== void 0 ? { ...r, database: e.database } : r;
|
|
778
778
|
if (t.lazy) {
|
|
779
|
-
let c = null, l =
|
|
779
|
+
let c = null, l = zt(t.poolPrototype ?? null, () => {
|
|
780
780
|
if (!c)
|
|
781
781
|
throw new f("db_unavailable", "SQL client n\xE3o inicializado (manager ausente).");
|
|
782
782
|
return c._ensureInitialLease();
|
|
@@ -817,7 +817,7 @@ async function Ke(r, e) {
|
|
|
817
817
|
let a = new $(n, i, s, t);
|
|
818
818
|
return i.__neetruSetReLease(() => a._reactiveReLease()), a;
|
|
819
819
|
}
|
|
820
|
-
async function
|
|
820
|
+
async function Kt(r, e, t) {
|
|
821
821
|
let n = je(r), o, i;
|
|
822
822
|
try {
|
|
823
823
|
o = await import(
|
|
@@ -834,7 +834,7 @@ async function Qt(r, e, t) {
|
|
|
834
834
|
);
|
|
835
835
|
}
|
|
836
836
|
let s = o.Pool ?? o.default?.Pool;
|
|
837
|
-
return
|
|
837
|
+
return He(
|
|
838
838
|
{
|
|
839
839
|
fetchLease: n,
|
|
840
840
|
createPool: (l) => {
|
|
@@ -869,24 +869,37 @@ async function Qt(r, e, t) {
|
|
|
869
869
|
t
|
|
870
870
|
);
|
|
871
871
|
}
|
|
872
|
-
var
|
|
872
|
+
var Be, Ve, Qe, ze, Ke, Xe = S(() => {
|
|
873
873
|
I();
|
|
874
874
|
x();
|
|
875
|
-
|
|
875
|
+
$e();
|
|
876
876
|
le();
|
|
877
|
-
|
|
878
|
-
|
|
877
|
+
Be = "/api/sdk/v1/db/lease", Ve = "/api/sdk/v1/db/lease/renew";
|
|
878
|
+
Qe = 10, ze = 6e4, Ke = 1e4;
|
|
879
879
|
});
|
|
880
880
|
|
|
881
881
|
// src/db/client-db.ts
|
|
882
882
|
I();
|
|
883
883
|
|
|
884
|
-
// src/
|
|
884
|
+
// src/runtime-env.ts
|
|
885
|
+
function tt() {
|
|
886
|
+
try {
|
|
887
|
+
if (typeof process < "u" && process.env)
|
|
888
|
+
return process.env.NODE_ENV === "production";
|
|
889
|
+
} catch {
|
|
890
|
+
}
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
885
893
|
function _e(r) {
|
|
894
|
+
return r === "dev" && !tt();
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// src/db/offline/query-engine.ts
|
|
898
|
+
function ye(r) {
|
|
886
899
|
return r == null ? 0 : typeof r == "number" ? 1 : typeof r == "string" ? 2 : typeof r == "boolean" ? 3 : 4;
|
|
887
900
|
}
|
|
888
901
|
function A(r, e) {
|
|
889
|
-
let t =
|
|
902
|
+
let t = ye(r), n = ye(e);
|
|
890
903
|
if (t !== n) return t - n;
|
|
891
904
|
if (r == null) return 0;
|
|
892
905
|
if (typeof r == "number" && typeof e == "number") return r - e;
|
|
@@ -904,7 +917,7 @@ function Z(r, e) {
|
|
|
904
917
|
}
|
|
905
918
|
return n;
|
|
906
919
|
}
|
|
907
|
-
function
|
|
920
|
+
function nt(r, e) {
|
|
908
921
|
let t = Z(r, e.field);
|
|
909
922
|
if (t === void 0) return false;
|
|
910
923
|
let { op: n, value: o } = e;
|
|
@@ -935,10 +948,10 @@ function et(r, e) {
|
|
|
935
948
|
);
|
|
936
949
|
}
|
|
937
950
|
}
|
|
938
|
-
function
|
|
939
|
-
return e.every((t) =>
|
|
951
|
+
function rt(r, e) {
|
|
952
|
+
return e.every((t) => nt(r, t));
|
|
940
953
|
}
|
|
941
|
-
function
|
|
954
|
+
function ot(r) {
|
|
942
955
|
return (e, t) => {
|
|
943
956
|
if (r) {
|
|
944
957
|
let n = Z(e.data, r.field), o = Z(t.data, r.field), i = A(n, o);
|
|
@@ -948,7 +961,7 @@ function nt(r) {
|
|
|
948
961
|
return e.id < t.id ? -1 : e.id > t.id ? 1 : 0;
|
|
949
962
|
};
|
|
950
963
|
}
|
|
951
|
-
function
|
|
964
|
+
function it(r, e) {
|
|
952
965
|
let t = r.findIndex((n) => n.id === e.docId);
|
|
953
966
|
return t === -1 ? r : e.type === "startAfter" ? r.slice(t + 1) : r.slice(0, t);
|
|
954
967
|
}
|
|
@@ -966,9 +979,9 @@ var P = class r {
|
|
|
966
979
|
evaluate(e, t) {
|
|
967
980
|
let n = e.filter((d) => !d.meta.deleted), o = t.where ?? [];
|
|
968
981
|
o.length > 0 && (n = n.filter(
|
|
969
|
-
(d) =>
|
|
982
|
+
(d) => rt(d.data, o)
|
|
970
983
|
));
|
|
971
|
-
let i =
|
|
984
|
+
let i = ot(t.orderBy), s = [...n].sort(i), a = t.cursor ? it(s, t.cursor) : s, c = Math.min(t.limit ?? 20, 500);
|
|
972
985
|
return {
|
|
973
986
|
docs: a.slice(0, c).map((d) => ({ id: d.id, data: d.data })),
|
|
974
987
|
// `incomplete` é sempre true aqui — o QueryEngine não sabe se o cache
|
|
@@ -988,7 +1001,7 @@ var P = class r {
|
|
|
988
1001
|
};
|
|
989
1002
|
|
|
990
1003
|
// src/db/offline/local-store.ts
|
|
991
|
-
var
|
|
1004
|
+
var at = 1, T = "documents", R = "mutations", N = "query_cache", V = "sync_meta", E = "conflict_log", j = class {
|
|
992
1005
|
db = null;
|
|
993
1006
|
dbName;
|
|
994
1007
|
constructor(e) {
|
|
@@ -1000,7 +1013,7 @@ var it = 1, T = "documents", R = "mutations", N = "query_cache", j = "sync_meta"
|
|
|
1000
1013
|
* Idempotente — chamadas subsequentes são no-ops se o banco já está aberto.
|
|
1001
1014
|
*/
|
|
1002
1015
|
async open() {
|
|
1003
|
-
this.db === null && (this.db = await openDB(this.dbName,
|
|
1016
|
+
this.db === null && (this.db = await openDB(this.dbName, at, {
|
|
1004
1017
|
upgrade(e) {
|
|
1005
1018
|
if (!e.objectStoreNames.contains(T)) {
|
|
1006
1019
|
let t = e.createObjectStore(T, {
|
|
@@ -1014,7 +1027,7 @@ var it = 1, T = "documents", R = "mutations", N = "query_cache", j = "sync_meta"
|
|
|
1014
1027
|
});
|
|
1015
1028
|
t.createIndex("by_seq", "seq"), t.createIndex("by_status", "status"), t.createIndex("by_doc", ["collection", "docId"]), t.createIndex("by_batch", "batchId");
|
|
1016
1029
|
}
|
|
1017
|
-
if (e.objectStoreNames.contains(N) || e.createObjectStore(N, { keyPath: "queryHash" }), e.objectStoreNames.contains(
|
|
1030
|
+
if (e.objectStoreNames.contains(N) || e.createObjectStore(N, { keyPath: "queryHash" }), e.objectStoreNames.contains(V) || e.createObjectStore(V, { keyPath: "key" }), !e.objectStoreNames.contains(E)) {
|
|
1018
1031
|
let t = e.createObjectStore(E, {
|
|
1019
1032
|
keyPath: "id",
|
|
1020
1033
|
autoIncrement: true
|
|
@@ -1101,14 +1114,14 @@ var it = 1, T = "documents", R = "mutations", N = "query_cache", j = "sync_meta"
|
|
|
1101
1114
|
* 'lastFullResyncAt', 'leaderTabId'.
|
|
1102
1115
|
*/
|
|
1103
1116
|
async getMeta(e) {
|
|
1104
|
-
let n = await this.assertOpen().get(
|
|
1117
|
+
let n = await this.assertOpen().get(V, e);
|
|
1105
1118
|
return n ? n.value : null;
|
|
1106
1119
|
}
|
|
1107
1120
|
/**
|
|
1108
1121
|
* Armazena ou atualiza um valor na store `sync_meta`.
|
|
1109
1122
|
*/
|
|
1110
1123
|
async setMeta(e, t) {
|
|
1111
|
-
await this.assertOpen().put(
|
|
1124
|
+
await this.assertOpen().put(V, { key: e, value: t });
|
|
1112
1125
|
}
|
|
1113
1126
|
// ─── Conflict log ───────────────────────────────────────────────────────────
|
|
1114
1127
|
/**
|
|
@@ -1549,10 +1562,10 @@ var z = class {
|
|
|
1549
1562
|
|
|
1550
1563
|
// src/db/field-value.ts
|
|
1551
1564
|
var te = "__neetru__";
|
|
1552
|
-
function
|
|
1565
|
+
function ct() {
|
|
1553
1566
|
return { __neetru__: "serverTimestamp" };
|
|
1554
1567
|
}
|
|
1555
|
-
function
|
|
1568
|
+
function lt(r) {
|
|
1556
1569
|
if (typeof r != "number" || !Number.isFinite(r))
|
|
1557
1570
|
throw new TypeError(
|
|
1558
1571
|
`increment(n): n deve ser um n\xFAmero finito (recebido: ${String(r)}).`
|
|
@@ -1562,19 +1575,19 @@ function at(r) {
|
|
|
1562
1575
|
function D(r) {
|
|
1563
1576
|
return typeof r == "object" && r !== null && te in r && typeof r[te] == "string";
|
|
1564
1577
|
}
|
|
1565
|
-
function
|
|
1578
|
+
function ge(r) {
|
|
1566
1579
|
return D(r) && r.__neetru__ === "serverTimestamp";
|
|
1567
1580
|
}
|
|
1568
|
-
function
|
|
1581
|
+
function be(r) {
|
|
1569
1582
|
return D(r) && r.__neetru__ === "increment" && typeof r.operand == "number" && Number.isFinite(r.operand);
|
|
1570
1583
|
}
|
|
1571
|
-
function
|
|
1572
|
-
return
|
|
1584
|
+
function dt(r, e) {
|
|
1585
|
+
return ge(r) ? Date.now() : be(r) ? (typeof e == "number" && Number.isFinite(e) ? e : 0) + r.operand : r;
|
|
1573
1586
|
}
|
|
1574
1587
|
function k(r, e) {
|
|
1575
1588
|
let t = false, n = {};
|
|
1576
1589
|
for (let [o, i] of Object.entries(r))
|
|
1577
|
-
D(i) ? (t = true, n[o] =
|
|
1590
|
+
D(i) ? (t = true, n[o] = dt(i, e?.[o])) : n[o] = i;
|
|
1578
1591
|
return t ? n : r;
|
|
1579
1592
|
}
|
|
1580
1593
|
function ne(r) {
|
|
@@ -1584,11 +1597,11 @@ function ne(r) {
|
|
|
1584
1597
|
}
|
|
1585
1598
|
|
|
1586
1599
|
// src/db/offline/sync-engine.ts
|
|
1587
|
-
var
|
|
1588
|
-
function
|
|
1600
|
+
var ut = 8, pt = 1e3, ft = 300 * 1e3, re = "truncatedCollections";
|
|
1601
|
+
function ht(r) {
|
|
1589
1602
|
return r !== null && ne(r);
|
|
1590
1603
|
}
|
|
1591
|
-
function
|
|
1604
|
+
function mt(r) {
|
|
1592
1605
|
if (r === null) return null;
|
|
1593
1606
|
if (!ne(r)) return r;
|
|
1594
1607
|
let e = {};
|
|
@@ -1790,13 +1803,13 @@ var K = class {
|
|
|
1790
1803
|
}), s.push({ type: "removed", collection: e, doc: { id: t, data: a.data } }));
|
|
1791
1804
|
return;
|
|
1792
1805
|
}
|
|
1793
|
-
let l =
|
|
1806
|
+
let l = mt(i.payload) ?? a?.data ?? {}, d = {
|
|
1794
1807
|
collection: e,
|
|
1795
1808
|
id: t,
|
|
1796
1809
|
data: i.op === "update" ? { ...a?.data ?? {}, ...l } : (
|
|
1797
1810
|
// add/set: se o payload tinha sentinels, o cache otimista (existing.data)
|
|
1798
1811
|
// é a fonte de verdade até o pull; senão usa o payload literal.
|
|
1799
|
-
|
|
1812
|
+
ht(i.payload) ? { ...a?.data ?? {}, ...l } : l
|
|
1800
1813
|
),
|
|
1801
1814
|
meta: {
|
|
1802
1815
|
serverVersion: n,
|
|
@@ -1949,7 +1962,7 @@ var K = class {
|
|
|
1949
1962
|
* pending do doc no cache + registra `lastError`/dispara `onError`.
|
|
1950
1963
|
*/
|
|
1951
1964
|
async _retryOrDeadLetter(e, t, n) {
|
|
1952
|
-
if (e.attempts + 1 <
|
|
1965
|
+
if (e.attempts + 1 < ut) {
|
|
1953
1966
|
await this._queue.markRetry(e.mutationId, t);
|
|
1954
1967
|
return;
|
|
1955
1968
|
}
|
|
@@ -1990,7 +2003,7 @@ var K = class {
|
|
|
1990
2003
|
}
|
|
1991
2004
|
/** Backoff exponencial keyed em `attempts`, com teto `BACKOFF_CAP_MS`. */
|
|
1992
2005
|
_backoffDelayMs(e) {
|
|
1993
|
-
return Math.min(
|
|
2006
|
+
return Math.min(pt * 2 ** (e - 1), ft);
|
|
1994
2007
|
}
|
|
1995
2008
|
// ─── FASE 2 — pull (reconciliar o cache) ─────────────────────────────────────
|
|
1996
2009
|
/**
|
|
@@ -2242,7 +2255,7 @@ var K = class {
|
|
|
2242
2255
|
};
|
|
2243
2256
|
|
|
2244
2257
|
// src/db/offline/conflict-resolver.ts
|
|
2245
|
-
function
|
|
2258
|
+
function ve(r, e) {
|
|
2246
2259
|
return e === null ? false : r === null ? true : e > r;
|
|
2247
2260
|
}
|
|
2248
2261
|
var H = class r {
|
|
@@ -2279,7 +2292,7 @@ var H = class r {
|
|
|
2279
2292
|
resolvedData: t.data,
|
|
2280
2293
|
conflictRecord: null
|
|
2281
2294
|
};
|
|
2282
|
-
if (!
|
|
2295
|
+
if (!ve(
|
|
2283
2296
|
e.meta.updatedAtServer,
|
|
2284
2297
|
t.meta.updatedAtServer
|
|
2285
2298
|
))
|
|
@@ -2326,7 +2339,7 @@ var H = class r {
|
|
|
2326
2339
|
let i = t.data;
|
|
2327
2340
|
if (e === null)
|
|
2328
2341
|
return { outcome: "no_conflict", resolvedData: { ...i, ...n.payload ?? {} }, conflictRecord: null };
|
|
2329
|
-
if (!
|
|
2342
|
+
if (!ve(
|
|
2330
2343
|
e.meta.updatedAtServer,
|
|
2331
2344
|
t.meta.updatedAtServer
|
|
2332
2345
|
))
|
|
@@ -2404,7 +2417,7 @@ var H = class r {
|
|
|
2404
2417
|
};
|
|
2405
2418
|
|
|
2406
2419
|
// src/db/offline/change-bus.ts
|
|
2407
|
-
function
|
|
2420
|
+
function _t(r) {
|
|
2408
2421
|
return `${r.collection}::${r.doc.id}`;
|
|
2409
2422
|
}
|
|
2410
2423
|
var G = class {
|
|
@@ -2452,7 +2465,7 @@ var G = class {
|
|
|
2452
2465
|
if (e.length === 0) return;
|
|
2453
2466
|
let t = /* @__PURE__ */ new Map();
|
|
2454
2467
|
for (let i of e)
|
|
2455
|
-
t.set(
|
|
2468
|
+
t.set(_t(i), i);
|
|
2456
2469
|
let n = Array.from(t.values()), o = /* @__PURE__ */ new Map();
|
|
2457
2470
|
for (let i of n)
|
|
2458
2471
|
o.has(i.collection) || o.set(i.collection, []), o.get(i.collection).push(i);
|
|
@@ -2771,15 +2784,15 @@ var q = class {
|
|
|
2771
2784
|
|
|
2772
2785
|
// src/db/collection-ref.ts
|
|
2773
2786
|
I();
|
|
2774
|
-
function
|
|
2787
|
+
function yt() {
|
|
2775
2788
|
return typeof indexedDB > "u" || typeof window > "u";
|
|
2776
2789
|
}
|
|
2777
|
-
var
|
|
2778
|
-
function
|
|
2779
|
-
if (!
|
|
2790
|
+
var we = /^[a-z0-9][a-z0-9_-]{0,62}$/;
|
|
2791
|
+
function gt(r) {
|
|
2792
|
+
if (!we.test(r))
|
|
2780
2793
|
throw new f(
|
|
2781
2794
|
"db_invalid_query",
|
|
2782
|
-
`Nome de cole\xE7\xE3o inv\xE1lido: "${r}". Deve seguir o padr\xE3o ${
|
|
2795
|
+
`Nome de cole\xE7\xE3o inv\xE1lido: "${r}". Deve seguir o padr\xE3o ${we}.`
|
|
2783
2796
|
);
|
|
2784
2797
|
}
|
|
2785
2798
|
function L(r, e = "id") {
|
|
@@ -2789,7 +2802,7 @@ function L(r, e = "id") {
|
|
|
2789
2802
|
`${e} \xE9 obrigat\xF3rio e deve ser uma string n\xE3o-vazia.`
|
|
2790
2803
|
);
|
|
2791
2804
|
}
|
|
2792
|
-
function
|
|
2805
|
+
function bt(r) {
|
|
2793
2806
|
let e = {};
|
|
2794
2807
|
if (r?.where && (e.where = r.where.map((t) => ({
|
|
2795
2808
|
field: t.field,
|
|
@@ -2806,7 +2819,7 @@ function yt(r) {
|
|
|
2806
2819
|
}
|
|
2807
2820
|
return e;
|
|
2808
2821
|
}
|
|
2809
|
-
function
|
|
2822
|
+
function vt(r, e) {
|
|
2810
2823
|
if (r.length < e) return null;
|
|
2811
2824
|
let t = r[r.length - 1];
|
|
2812
2825
|
return t ? btoa(JSON.stringify({ type: "startAfter", docId: t.id })) : null;
|
|
@@ -2853,7 +2866,7 @@ var oe = class {
|
|
|
2853
2866
|
}
|
|
2854
2867
|
/** Constrói um DbListResult para a coleção com query opcional. */
|
|
2855
2868
|
async _buildListResult(e) {
|
|
2856
|
-
let t =
|
|
2869
|
+
let t = bt(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 = vt(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;
|
|
2857
2870
|
return {
|
|
2858
2871
|
docs: o.docs.map((h) => ({ id: h.id, data: h.data })),
|
|
2859
2872
|
nextCursor: c,
|
|
@@ -3196,7 +3209,7 @@ var oe = class {
|
|
|
3196
3209
|
this._store = e.store, this._queue = e.queue, this._bus = e.bus, this._engine = e.engine, this._connectivity = e.connectivity;
|
|
3197
3210
|
}
|
|
3198
3211
|
collection(e) {
|
|
3199
|
-
return
|
|
3212
|
+
return gt(e), new oe(
|
|
3200
3213
|
e,
|
|
3201
3214
|
this._store,
|
|
3202
3215
|
this._queue,
|
|
@@ -3225,8 +3238,8 @@ var oe = class {
|
|
|
3225
3238
|
return this._engine.onError(e);
|
|
3226
3239
|
}
|
|
3227
3240
|
};
|
|
3228
|
-
async function
|
|
3229
|
-
let e =
|
|
3241
|
+
async function Se(r) {
|
|
3242
|
+
let e = yt() ? new Q() : new j(r.dbName);
|
|
3230
3243
|
await e.open();
|
|
3231
3244
|
let t = new z(e), n = new H(), o = new G(), i = new X({
|
|
3232
3245
|
// Se startOnline=false, injeta um navigator falso
|
|
@@ -3261,9 +3274,9 @@ async function we(r) {
|
|
|
3261
3274
|
}
|
|
3262
3275
|
|
|
3263
3276
|
// src/db/realtime/realtime-client.ts
|
|
3264
|
-
var
|
|
3265
|
-
function
|
|
3266
|
-
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++
|
|
3277
|
+
var wt = 0;
|
|
3278
|
+
function Te() {
|
|
3279
|
+
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++wt}-${Math.random().toString(36).slice(2)}`;
|
|
3267
3280
|
}
|
|
3268
3281
|
var F = class {
|
|
3269
3282
|
// ── configuração ──────────────────────────────────────────────────────────
|
|
@@ -3303,7 +3316,7 @@ var F = class {
|
|
|
3303
3316
|
_pendingFrames = [];
|
|
3304
3317
|
// ── construtor ────────────────────────────────────────────────────────────
|
|
3305
3318
|
constructor(e) {
|
|
3306
|
-
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ??
|
|
3319
|
+
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ?? St, 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();
|
|
3307
3320
|
}
|
|
3308
3321
|
// ── API pública ───────────────────────────────────────────────────────────
|
|
3309
3322
|
/**
|
|
@@ -3317,8 +3330,8 @@ var F = class {
|
|
|
3317
3330
|
*/
|
|
3318
3331
|
subscribe(e, t, n) {
|
|
3319
3332
|
if (this._closed)
|
|
3320
|
-
return
|
|
3321
|
-
let o =
|
|
3333
|
+
return Te();
|
|
3334
|
+
let o = Te();
|
|
3322
3335
|
this._subscriptions.set(o, {
|
|
3323
3336
|
subscriptionId: o,
|
|
3324
3337
|
collection: e,
|
|
@@ -3582,7 +3595,7 @@ var F = class {
|
|
|
3582
3595
|
_detachHandlers(e) {
|
|
3583
3596
|
e.onopen = null, e.onmessage = null, e.onclose = null, e.onerror = null;
|
|
3584
3597
|
}
|
|
3585
|
-
},
|
|
3598
|
+
}, St = (r) => {
|
|
3586
3599
|
if (typeof WebSocket > "u")
|
|
3587
3600
|
throw new Error(
|
|
3588
3601
|
"[NeetruRealtimeClient] WebSocket n\xE3o dispon\xEDvel neste ambiente. Injete uma implementa\xE7\xE3o via `webSocketFactory` nas op\xE7\xF5es."
|
|
@@ -3592,11 +3605,11 @@ var F = class {
|
|
|
3592
3605
|
|
|
3593
3606
|
// src/db/client-db.ts
|
|
3594
3607
|
I();
|
|
3595
|
-
var
|
|
3596
|
-
function
|
|
3597
|
-
return e._transport ? e._transport : r === "firestore" && e.firestoreTransport ? e.firestoreTransport : r === "nosql-vm" && e.realtimeGatewayUrl ?
|
|
3608
|
+
var Ht = (r) => `/api/sdk/v1/datastore/${encodeURIComponent(r)}`, Y = (r, e) => `/api/sdk/v1/datastore/${encodeURIComponent(r)}/${encodeURIComponent(e)}`, Je = 200;
|
|
3609
|
+
function Gt(r, e, t) {
|
|
3610
|
+
return e._transport ? e._transport : r === "firestore" && e.firestoreTransport ? e.firestoreTransport : r === "nosql-vm" && e.realtimeGatewayUrl ? Xt(e.realtimeGatewayUrl, t, e) : Ye(t);
|
|
3598
3611
|
}
|
|
3599
|
-
function
|
|
3612
|
+
function Ye(r) {
|
|
3600
3613
|
return {
|
|
3601
3614
|
async pushMutations(e) {
|
|
3602
3615
|
if (!r)
|
|
@@ -3674,7 +3687,7 @@ function Je(r) {
|
|
|
3674
3687
|
try {
|
|
3675
3688
|
l = await t(r, {
|
|
3676
3689
|
method: "GET",
|
|
3677
|
-
path:
|
|
3690
|
+
path: Ht(c),
|
|
3678
3691
|
requireAuth: !0,
|
|
3679
3692
|
retries: 2,
|
|
3680
3693
|
headers: o,
|
|
@@ -3682,7 +3695,7 @@ function Je(r) {
|
|
|
3682
3695
|
// TRUNCADAS — reportadas em `truncatedCollections` p/ o SyncEngine
|
|
3683
3696
|
// (a) NÃO rodar deleção-por-ausência e (b) sinalizar
|
|
3684
3697
|
// `DbListResult.truncated`. Paginação por cursor é tarefa Core (gated).
|
|
3685
|
-
query: { limit:
|
|
3698
|
+
query: { limit: Je }
|
|
3686
3699
|
});
|
|
3687
3700
|
} catch (p) {
|
|
3688
3701
|
let h = p instanceof Error ? p.message : String(p);
|
|
@@ -3692,7 +3705,7 @@ function Je(r) {
|
|
|
3692
3705
|
);
|
|
3693
3706
|
}
|
|
3694
3707
|
let d = l, u = Array.isArray(d?.items) ? d.items : [];
|
|
3695
|
-
u.length >=
|
|
3708
|
+
u.length >= Je && a.push(c);
|
|
3696
3709
|
for (let p of u) {
|
|
3697
3710
|
if (!p || typeof p != "object") continue;
|
|
3698
3711
|
let h = p, _ = typeof h.id == "string" ? h.id : null;
|
|
@@ -3721,10 +3734,10 @@ function Je(r) {
|
|
|
3721
3734
|
}
|
|
3722
3735
|
};
|
|
3723
3736
|
}
|
|
3724
|
-
function
|
|
3725
|
-
let n =
|
|
3737
|
+
function Xt(r, e, t) {
|
|
3738
|
+
let n = Ye(e), o = new F({
|
|
3726
3739
|
gatewayUrl: r,
|
|
3727
|
-
ticketProvider:
|
|
3740
|
+
ticketProvider: Jt(e, t),
|
|
3728
3741
|
dbId: t.dbId,
|
|
3729
3742
|
webSocketFactory: t._wsFactory
|
|
3730
3743
|
});
|
|
@@ -3760,7 +3773,7 @@ function Ht(r, e, t) {
|
|
|
3760
3773
|
}
|
|
3761
3774
|
};
|
|
3762
3775
|
}
|
|
3763
|
-
function
|
|
3776
|
+
function Jt(r, e) {
|
|
3764
3777
|
return async () => {
|
|
3765
3778
|
let { httpRequest: t } = await Promise.resolve().then(() => (W(), U));
|
|
3766
3779
|
return await t(r, {
|
|
@@ -3775,27 +3788,27 @@ function Gt(r, e) {
|
|
|
3775
3788
|
});
|
|
3776
3789
|
};
|
|
3777
3790
|
}
|
|
3778
|
-
function
|
|
3791
|
+
function Zn(r, e, t, n) {
|
|
3779
3792
|
return new F({ gatewayUrl: r, ticketProvider: e, dbId: t, webSocketFactory: n });
|
|
3780
3793
|
}
|
|
3781
3794
|
var pe = false;
|
|
3782
|
-
function
|
|
3795
|
+
function er() {
|
|
3783
3796
|
pe = false;
|
|
3784
3797
|
}
|
|
3785
|
-
function
|
|
3798
|
+
function Yt(r, e) {
|
|
3786
3799
|
pe || e.dbId && r.env !== "prod" && (typeof window > "u" || typeof console > "u" || typeof console.warn != "function" || (pe = true, console.warn(
|
|
3787
3800
|
'[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.'
|
|
3788
3801
|
)));
|
|
3789
3802
|
}
|
|
3790
|
-
function
|
|
3791
|
-
|
|
3803
|
+
function tr(r, e = {}) {
|
|
3804
|
+
Yt(r, e);
|
|
3792
3805
|
let t;
|
|
3793
3806
|
e.transport !== void 0 ? t = e.transport : e.engine !== void 0 ? (console.warn(
|
|
3794
3807
|
'[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).'
|
|
3795
3808
|
), t = e.engine) : t = "rest";
|
|
3796
|
-
let n =
|
|
3809
|
+
let n = Gt(t, e, r), o = e.dbId ?? "default", i = e.dbName ?? `neetru-db__${r.productId ?? "sdk"}__${o}__${r.env}`, s = null, a = null;
|
|
3797
3810
|
function c() {
|
|
3798
|
-
return s || (s =
|
|
3811
|
+
return s || (s = Se({
|
|
3799
3812
|
dbName: i,
|
|
3800
3813
|
transport: n,
|
|
3801
3814
|
singleTab: e.singleTab ?? r.env !== "prod"
|
|
@@ -3803,15 +3816,15 @@ function Jn(r, e = {}) {
|
|
|
3803
3816
|
}
|
|
3804
3817
|
return {
|
|
3805
3818
|
collection(l) {
|
|
3806
|
-
return a ? a.collection(l) :
|
|
3819
|
+
return a ? a.collection(l) : Zt(l, c);
|
|
3807
3820
|
},
|
|
3808
3821
|
async sql(l, d) {
|
|
3809
|
-
if (r.env
|
|
3822
|
+
if (_e(r.env))
|
|
3810
3823
|
throw new f(
|
|
3811
3824
|
"db_unavailable",
|
|
3812
3825
|
"[SDK] db.sql() n\xE3o dispon\xEDvel em NEETRU_ENV=dev. Use `neetru dev` para subir o container Postgres local."
|
|
3813
3826
|
);
|
|
3814
|
-
return (await Promise.resolve().then(() => (
|
|
3827
|
+
return (await Promise.resolve().then(() => (Xe(), Ge))).createSqlClientFromConfig(r, l, d);
|
|
3815
3828
|
},
|
|
3816
3829
|
get syncState() {
|
|
3817
3830
|
return a?.syncState ?? {
|
|
@@ -3850,7 +3863,7 @@ function Jn(r, e = {}) {
|
|
|
3850
3863
|
}
|
|
3851
3864
|
};
|
|
3852
3865
|
}
|
|
3853
|
-
function
|
|
3866
|
+
function Zt(r, e) {
|
|
3854
3867
|
async function t() {
|
|
3855
3868
|
return (await e()).collection(r);
|
|
3856
3869
|
}
|
|
@@ -3928,4 +3941,4 @@ function Jt(r, e) {
|
|
|
3928
3941
|
};
|
|
3929
3942
|
}
|
|
3930
3943
|
|
|
3931
|
-
export { te as NEETRU_SENTINEL_KEY, f as NeetruDbError,
|
|
3944
|
+
export { te as NEETRU_SENTINEL_KEY, f as NeetruDbError, er as __resetDbIdWarningForTests, tr as createNeetruDb, Ye as createRestSyncTransport, Zn as getWebSocketRealtimeClient, lt as increment, D as isFieldSentinel, be as isIncrementSentinel, ge as isServerTimestampSentinel, ct as serverTimestamp };
|