@neetru/sdk 3.8.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 +20 -0
- 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 +708 -688
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +705 -685
- 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 +1 -1
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +1 -1
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db.cjs +242 -222
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +263 -243
- 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 +730 -710
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +874 -854
- package/dist/mobile.d.cts +1 -1
- package/dist/mobile.d.ts +1 -1
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- 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.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- 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 +1 -1
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +1 -1
- package/dist/{types-bVztFQTI.d.ts → types-BYPk_3HR.d.ts} +56 -16
- package/dist/{types-D-9bjlM0.d.cts → types-yv3EDKg2.d.cts} +56 -16
- 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 +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
var jose = require('jose');
|
|
4
4
|
var idb = require('idb');
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var sr = Object.defineProperty;
|
|
7
7
|
var W = (n, e) => () => (n && (e = n(n = 0)), e);
|
|
8
8
|
var ft = (n, e) => {
|
|
9
9
|
for (var t in e)
|
|
10
|
-
|
|
10
|
+
sr(n, t, { get: e[t], enumerable: true });
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
// src/errors.ts
|
|
@@ -58,9 +58,9 @@ var St = W(() => {
|
|
|
58
58
|
|
|
59
59
|
// src/correlation.ts
|
|
60
60
|
function Rt(n) {
|
|
61
|
-
return typeof n == "string" && n.length > 0 && n.length <= 128 &&
|
|
61
|
+
return typeof n == "string" && n.length > 0 && n.length <= 128 && fr.test(n);
|
|
62
62
|
}
|
|
63
|
-
function
|
|
63
|
+
function mr() {
|
|
64
64
|
let n = globalThis.crypto;
|
|
65
65
|
try {
|
|
66
66
|
if (n && typeof n.randomUUID == "function") return n.randomUUID();
|
|
@@ -87,11 +87,11 @@ function _e(n, e) {
|
|
|
87
87
|
}
|
|
88
88
|
r[i] = s;
|
|
89
89
|
}
|
|
90
|
-
let o = t ?? (Rt(e) ? e : void 0) ??
|
|
90
|
+
let o = t ?? (Rt(e) ? e : void 0) ?? mr();
|
|
91
91
|
return r[Tt] = o, { headers: r, correlationId: o };
|
|
92
92
|
}
|
|
93
|
-
var Tt,
|
|
94
|
-
Tt = "x-correlation-id",
|
|
93
|
+
var Tt, fr, We = W(() => {
|
|
94
|
+
Tt = "x-correlation-id", fr = /^[A-Za-z0-9._:\-]+$/;
|
|
95
95
|
});
|
|
96
96
|
|
|
97
97
|
// src/server-only-key.ts
|
|
@@ -109,7 +109,7 @@ function Q() {
|
|
|
109
109
|
return null;
|
|
110
110
|
}
|
|
111
111
|
function It(n, e) {
|
|
112
|
-
let t =
|
|
112
|
+
let t = hr[n];
|
|
113
113
|
return (`${e ? `[Neetru SDK] Recusado: a credencial nrt_ do produto foi passada ao SDK rodando ${t}.` : `[Neetru SDK] ATEN\xC7\xC3O: a credencial nrt_ do produto est\xE1 sendo usada ${t}.`} Essa chave \xE9 S\xD3 DE SERVIDOR \u2014 quem abrir o DevTools (ou descompactar o app) copia a chave e fala com o Core como se fosse o seu produto. Como corrigir: o site/app chama a API do SEU produto (ex.: https://<produto>.neetru.com/api/v1) com o token do usu\xE1rio (Authorization: Bearer <access_token>); o backend do produto valida com verifyAccessToken() e usa o SDK com a chave (NEETRU_API_KEY, sem prefixo NEXT_PUBLIC_/EXPO_PUBLIC_). ` + (e ? "" : "Nesta vers\xE3o \xE9 s\xF3 um aviso; use strictServerOnlyKey: true para bloquear j\xE1. Na pr\xF3xima vers\xE3o MAJOR do SDK isso passa a falhar sempre.")).trim();
|
|
114
114
|
}
|
|
115
115
|
function At(n) {
|
|
@@ -127,9 +127,9 @@ function At(n) {
|
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
-
var
|
|
130
|
+
var hr, Ct, be = W(() => {
|
|
131
131
|
C();
|
|
132
|
-
|
|
132
|
+
hr = {
|
|
133
133
|
browser: "no navegador",
|
|
134
134
|
"react-native": "num app React Native"
|
|
135
135
|
};
|
|
@@ -146,7 +146,7 @@ ft(ne, {
|
|
|
146
146
|
function $e(n) {
|
|
147
147
|
return n.split("+")[0] ?? n;
|
|
148
148
|
}
|
|
149
|
-
function
|
|
149
|
+
function yr(n, e) {
|
|
150
150
|
let t = $e(n).split(".").map(Number), r = $e(e).split(".").map(Number), o = Math.max(t.length, r.length);
|
|
151
151
|
for (let i = 0; i < o; i++) {
|
|
152
152
|
let s = t[i] ?? 0, a = r[i] ?? 0;
|
|
@@ -156,28 +156,28 @@ function gr(n, e) {
|
|
|
156
156
|
}
|
|
157
157
|
function Ot() {
|
|
158
158
|
try {
|
|
159
|
-
return "3.8.
|
|
159
|
+
return "3.8.1";
|
|
160
160
|
} catch {
|
|
161
161
|
return;
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
-
function
|
|
164
|
+
function _r() {
|
|
165
165
|
let n = Ot();
|
|
166
166
|
return n ? parseInt(n.split(".")[0] ?? "3", 10) : 3;
|
|
167
167
|
}
|
|
168
168
|
function te(n) {
|
|
169
169
|
if (!n) return;
|
|
170
|
-
let e =
|
|
170
|
+
let e = _r(), t = gr[e];
|
|
171
171
|
if (!t) return;
|
|
172
172
|
let r = $e(n);
|
|
173
|
-
if (
|
|
173
|
+
if (yr(r, t) < 0) {
|
|
174
174
|
let o = `${e}:${r}`;
|
|
175
175
|
Et.has(o) || (Et.add(o), console.warn(
|
|
176
176
|
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${n}" \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.`
|
|
177
177
|
));
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
|
-
function
|
|
180
|
+
function br() {
|
|
181
181
|
let n = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
182
182
|
return Q() ? "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. (" + n + ")" : n;
|
|
183
183
|
}
|
|
@@ -185,7 +185,7 @@ function Pt(n) {
|
|
|
185
185
|
let e = 200 * Math.pow(4, n), t = e * 0.2 * (Math.random() * 2 - 1);
|
|
186
186
|
return Math.max(50, Math.round(e + t));
|
|
187
187
|
}
|
|
188
|
-
function
|
|
188
|
+
function Tr(n) {
|
|
189
189
|
if (!n) return null;
|
|
190
190
|
let e = Number(n);
|
|
191
191
|
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), Dt);
|
|
@@ -202,7 +202,7 @@ function Nt(n) {
|
|
|
202
202
|
function ve(n) {
|
|
203
203
|
return n === 401 ? "unauthorized" : n === 403 ? "forbidden" : n === 404 ? "not_found" : n === 409 ? "conflict" : n === 410 ? "gone" : n === 422 || n === 400 ? "validation_failed" : n === 429 ? "rate_limited" : n >= 500 ? "server_error" : "unknown";
|
|
204
204
|
}
|
|
205
|
-
function
|
|
205
|
+
function Rr(n, e, t) {
|
|
206
206
|
let r = n.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, i = new URL(`${r}${o}`);
|
|
207
207
|
if (t)
|
|
208
208
|
for (let [s, a] of Object.entries(t))
|
|
@@ -219,25 +219,25 @@ async function xt(n) {
|
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
async function S(n, e) {
|
|
222
|
-
let t = e.method ?? "GET", r =
|
|
222
|
+
let t = e.method ?? "GET", r = Rr(n.baseUrl, e.path, e.query), o = kr.has(t) ? vr : wr, i = e.retries ?? o, { headers: s, correlationId: a } = _e(
|
|
223
223
|
{ accept: "application/json", ...e.headers },
|
|
224
224
|
e.correlationId
|
|
225
|
-
),
|
|
226
|
-
if (
|
|
225
|
+
), d = Ot();
|
|
226
|
+
if (d && (s["x-neetru-sdk-version"] = d), e.requireAuth) {
|
|
227
227
|
if (!n.apiKey)
|
|
228
|
-
throw new exports.NeetruError("missing_api_key",
|
|
228
|
+
throw new exports.NeetruError("missing_api_key", br());
|
|
229
229
|
s.authorization = `Bearer ${n.apiKey}`;
|
|
230
230
|
}
|
|
231
231
|
if (e.idempotencyKey) {
|
|
232
232
|
let f = typeof e.idempotencyKey == "string" ? e.idempotencyKey : kt();
|
|
233
233
|
s["idempotency-key"] = f;
|
|
234
234
|
}
|
|
235
|
-
let
|
|
236
|
-
|
|
237
|
-
let
|
|
235
|
+
let p = e.body !== void 0 && t !== "GET" && t !== "DELETE" ? JSON.stringify(e.body) : void 0;
|
|
236
|
+
p !== void 0 && (s["content-type"] = "application/json");
|
|
237
|
+
let l = null, u = e.deadlineMs != null ? Date.now() + e.deadlineMs : null;
|
|
238
238
|
for (let f = 0; f <= i; f++) {
|
|
239
|
-
if (
|
|
240
|
-
throw
|
|
239
|
+
if (u != null && Date.now() >= u)
|
|
240
|
+
throw l ?? new exports.NeetruError(
|
|
241
241
|
"network_error",
|
|
242
242
|
"Deadline excedido antes de completar a request",
|
|
243
243
|
void 0,
|
|
@@ -246,22 +246,22 @@ async function S(n, e) {
|
|
|
246
246
|
a
|
|
247
247
|
);
|
|
248
248
|
let m = { method: t, headers: s };
|
|
249
|
-
|
|
250
|
-
let h =
|
|
249
|
+
p !== void 0 && (m.body = p);
|
|
250
|
+
let h = u != null ? Math.max(1, Math.min(3e4, u - Date.now())) : 3e4;
|
|
251
251
|
m.signal = AbortSignal.timeout(h);
|
|
252
252
|
let y;
|
|
253
253
|
try {
|
|
254
254
|
y = await n.fetch(r, m);
|
|
255
255
|
} catch (b) {
|
|
256
256
|
let E = b instanceof DOMException && b.name === "TimeoutError" ? "Network error: timeout after 30s" : `Network error: ${b instanceof Error ? b.message : "fetch failed"}`;
|
|
257
|
-
if (
|
|
257
|
+
if (l = new exports.NeetruError("network_error", E, void 0, void 0, void 0, a), f < i) {
|
|
258
258
|
let x = Pt(f);
|
|
259
|
-
if (
|
|
259
|
+
if (u == null || Date.now() + x < u) {
|
|
260
260
|
await Nt(x);
|
|
261
261
|
continue;
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
|
-
throw
|
|
264
|
+
throw l;
|
|
265
265
|
}
|
|
266
266
|
let I = y.headers.get("x-request-id") ?? y.headers.get("x-correlation-id") ?? void 0;
|
|
267
267
|
if (y.ok) {
|
|
@@ -278,29 +278,29 @@ async function S(n, e) {
|
|
|
278
278
|
N === `HTTP ${y.status}` && (N = `Endpoint ${e.path} foi removido do Core (HTTP 410 Gone) \u2014 atualize o @neetru/sdk e migre para a API substituta.`), b && !N.includes(b) && (N += ` Substituto: ${b}.`);
|
|
279
279
|
}
|
|
280
280
|
let _ = new exports.NeetruError(k, N, y.status, I, g, a);
|
|
281
|
-
if (
|
|
282
|
-
let E =
|
|
283
|
-
if (
|
|
281
|
+
if (l = _, Sr.has(k) && f < i) {
|
|
282
|
+
let E = Tr(y.headers.get("retry-after")) ?? Pt(f);
|
|
283
|
+
if (u == null || Date.now() + E < u) {
|
|
284
284
|
await Nt(E);
|
|
285
285
|
continue;
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
288
|
throw _;
|
|
289
289
|
}
|
|
290
|
-
throw
|
|
290
|
+
throw l ?? new exports.NeetruError("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, a);
|
|
291
291
|
}
|
|
292
|
-
var
|
|
292
|
+
var gr, Et, vr, wr, kr, Sr, Dt, q = W(() => {
|
|
293
293
|
C();
|
|
294
294
|
St();
|
|
295
295
|
We();
|
|
296
296
|
be();
|
|
297
|
-
|
|
297
|
+
gr = {
|
|
298
298
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
299
299
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
300
300
|
3: "2026.05.1"
|
|
301
301
|
};
|
|
302
302
|
Et = /* @__PURE__ */ new Set();
|
|
303
|
-
|
|
303
|
+
vr = 2, wr = 0, kr = /* @__PURE__ */ new Set(["GET", "HEAD"]), Sr = /* @__PURE__ */ new Set([
|
|
304
304
|
"rate_limited",
|
|
305
305
|
"server_error",
|
|
306
306
|
"network_error"
|
|
@@ -309,9 +309,9 @@ var hr, Et, br, vr, wr, kr, Dt, q = W(() => {
|
|
|
309
309
|
});
|
|
310
310
|
|
|
311
311
|
// src/db-errors.ts
|
|
312
|
-
var
|
|
312
|
+
var qr; exports.NeetruDbError = void 0; var z = W(() => {
|
|
313
313
|
C();
|
|
314
|
-
|
|
314
|
+
qr = /* @__PURE__ */ new Set([
|
|
315
315
|
"db_unavailable",
|
|
316
316
|
"db_conflict",
|
|
317
317
|
"db_timeout"
|
|
@@ -329,7 +329,7 @@ var Mr; exports.NeetruDbError = void 0; var z = W(() => {
|
|
|
329
329
|
*/
|
|
330
330
|
dbId;
|
|
331
331
|
constructor(e, t, r) {
|
|
332
|
-
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable =
|
|
332
|
+
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable = qr.has(e), this.dbId = r, Object.setPrototypeOf(this, n.prototype);
|
|
333
333
|
}
|
|
334
334
|
};
|
|
335
335
|
});
|
|
@@ -338,7 +338,7 @@ var Mr; exports.NeetruDbError = void 0; var z = W(() => {
|
|
|
338
338
|
function De(n) {
|
|
339
339
|
return typeof n == "object" && n !== null && n[Xe] === true;
|
|
340
340
|
}
|
|
341
|
-
function
|
|
341
|
+
function so(n) {
|
|
342
342
|
let e = n && typeof n == "object" && "code" in n ? n.code : void 0;
|
|
343
343
|
if (e === "ECONNREFUSED" || e === "ETIMEDOUT" || e === "ENOTFOUND" || e === "EHOSTUNREACH" || e === "EAI_AGAIN")
|
|
344
344
|
return true;
|
|
@@ -350,56 +350,73 @@ function io(n) {
|
|
|
350
350
|
let r = (n instanceof Error ? n.message : typeof n == "string" ? n : "").toLowerCase();
|
|
351
351
|
return r.length === 0 ? false : !!(r.includes("certificate expired") || r.includes("certificate has expired") || r.includes("cert_has_expired") || r.includes("alert certificate expired") || r.includes("err_ssl") || r.includes("sslv3 alert") || r.includes("tlsv1 alert") || r.includes("econnrefused") || r.includes("etimedout") || r.includes("enotfound") || r.includes("ehostunreach") || r.includes("getaddrinfo"));
|
|
352
352
|
}
|
|
353
|
+
function rn(n) {
|
|
354
|
+
let e = n && typeof n == "object" && "code" in n ? n.code : void 0;
|
|
355
|
+
if (typeof e == "string" && ao.has(e.toUpperCase()))
|
|
356
|
+
return true;
|
|
357
|
+
let r = (n instanceof Error ? n.message : typeof n == "string" ? n : "").toLowerCase();
|
|
358
|
+
return r.length === 0 ? false : r.includes("password authentication failed") || r.includes("sasl authentication failed");
|
|
359
|
+
}
|
|
360
|
+
function co(n) {
|
|
361
|
+
return so(n) || rn(n);
|
|
362
|
+
}
|
|
353
363
|
function Ye(n) {
|
|
354
364
|
let e = { current: n }, t = null, r = {
|
|
355
|
-
__neetruSwapInner(
|
|
365
|
+
__neetruSwapInner(l) {
|
|
356
366
|
let u = e.current;
|
|
357
|
-
return e.current =
|
|
367
|
+
return e.current = l, u;
|
|
358
368
|
},
|
|
359
369
|
__neetruGetInner() {
|
|
360
370
|
return e.current;
|
|
361
371
|
},
|
|
362
|
-
__neetruSetReLease(
|
|
363
|
-
t =
|
|
372
|
+
__neetruSetReLease(l) {
|
|
373
|
+
t = l;
|
|
364
374
|
}
|
|
365
|
-
}, o = (
|
|
375
|
+
}, o = (l, u, f) => u[l](
|
|
376
|
+
...f
|
|
377
|
+
), i = (l, u) => (...f) => f.length > 0 && typeof f[f.length - 1] == "function" ? o(l, e.current, f) : (async () => {
|
|
366
378
|
try {
|
|
367
|
-
return await o(e.current,
|
|
368
|
-
} catch (
|
|
369
|
-
let
|
|
370
|
-
if (
|
|
371
|
-
throw
|
|
379
|
+
return await o(l, e.current, f);
|
|
380
|
+
} catch (m) {
|
|
381
|
+
let h = t;
|
|
382
|
+
if (h === null || !u(m))
|
|
383
|
+
throw m;
|
|
372
384
|
try {
|
|
373
|
-
await
|
|
385
|
+
await h();
|
|
374
386
|
} catch {
|
|
375
|
-
throw
|
|
387
|
+
throw m;
|
|
376
388
|
}
|
|
377
|
-
return await o(e.current,
|
|
389
|
+
return await o(l, e.current, f);
|
|
378
390
|
}
|
|
379
|
-
})(), s = Object.create(Object.getPrototypeOf(n));
|
|
380
|
-
return new Proxy(
|
|
381
|
-
get(
|
|
391
|
+
})(), s = i("query", co), a = i("connect", rn), d = Object.create(Object.getPrototypeOf(n));
|
|
392
|
+
return new Proxy(d, {
|
|
393
|
+
get(l, u) {
|
|
382
394
|
if (u === Xe) return true;
|
|
383
395
|
if (u === "__neetruSwapInner") return r.__neetruSwapInner;
|
|
384
396
|
if (u === "__neetruGetInner") return r.__neetruGetInner;
|
|
385
397
|
if (u === "__neetruSetReLease") return r.__neetruSetReLease;
|
|
386
|
-
if (u === "query") return
|
|
398
|
+
if (u === "query") return s;
|
|
399
|
+
if (u === "connect") {
|
|
400
|
+
let h = e.current;
|
|
401
|
+
return typeof h.connect == "function" ? a : h.connect;
|
|
402
|
+
}
|
|
387
403
|
if (u === "constructor")
|
|
388
404
|
return e.current.constructor;
|
|
389
|
-
let
|
|
390
|
-
return typeof
|
|
405
|
+
let f = e.current, m = Reflect.get(f, u, f);
|
|
406
|
+
return typeof m == "function" ? m.bind(f) : m;
|
|
391
407
|
}
|
|
392
408
|
});
|
|
393
409
|
}
|
|
394
|
-
var Xe, Ze = W(() => {
|
|
410
|
+
var Xe, ao, Ze = W(() => {
|
|
395
411
|
Xe = /* @__PURE__ */ Symbol.for("neetru.sdk.resilientPool");
|
|
412
|
+
ao = /* @__PURE__ */ new Set(["28P01", "28000"]);
|
|
396
413
|
});
|
|
397
414
|
|
|
398
415
|
// src/db/sql/lease.ts
|
|
399
|
-
function
|
|
416
|
+
function uo(n, e) {
|
|
400
417
|
return n ? n.leaseId !== e.leaseId || n.credentialVersion !== e.credentialVersion || n.host !== e.host || n.port !== e.port || n.dbName !== e.dbName || n.user !== e.user || n.password !== e.password || n.sslca !== e.sslca || n.clientCert !== e.clientCert || n.clientKey !== e.clientKey : true;
|
|
401
418
|
}
|
|
402
|
-
function
|
|
419
|
+
function po(n) {
|
|
403
420
|
if (n instanceof exports.NeetruDbError) return n;
|
|
404
421
|
let e = n instanceof Error ? n.message : String(n), t = n instanceof Error ? n.code : void 0;
|
|
405
422
|
return typeof t == "string" && (t === "ECONNREFUSED" || t === "ENOTFOUND" || t === "ETIMEDOUT") ? new exports.NeetruDbError(
|
|
@@ -407,10 +424,10 @@ function co(n) {
|
|
|
407
424
|
`Pool connection failed (${t}): ${e}`
|
|
408
425
|
) : e.includes("statement timeout") || e.includes("lock timeout") ? new exports.NeetruDbError("db_timeout", `Query timeout: ${e}`) : e.includes("40001") || e.includes("40P01") || e.includes("deadlock") ? new exports.NeetruDbError("db_conflict", `Transaction conflict: ${e}`) : e.includes("42501") || e.toLowerCase().includes("permission denied") ? new exports.NeetruDbError("db_permission_denied", `Permission denied: ${e}`) : new exports.NeetruDbError("db_unavailable", `Database error: ${e}`);
|
|
409
426
|
}
|
|
410
|
-
var
|
|
427
|
+
var lo, Z, se, on = W(() => {
|
|
411
428
|
z();
|
|
412
429
|
Ze();
|
|
413
|
-
|
|
430
|
+
lo = 0.8, Z = 3e4, se = class {
|
|
414
431
|
/**
|
|
415
432
|
* Lease atual. `null` no modo LAZY antes da 1ª query (o 1º lease é adiado —
|
|
416
433
|
* ver `_ensureInitialLease`). Após a 1ª aquisição, sempre não-nulo.
|
|
@@ -450,7 +467,7 @@ var so, Z, se, rn = W(() => {
|
|
|
450
467
|
isolationLevel: t.isolationLevel
|
|
451
468
|
}) : await this._orm.transaction(e);
|
|
452
469
|
} catch (r) {
|
|
453
|
-
throw
|
|
470
|
+
throw po(r);
|
|
454
471
|
}
|
|
455
472
|
}
|
|
456
473
|
async close() {
|
|
@@ -473,7 +490,7 @@ var so, Z, se, rn = W(() => {
|
|
|
473
490
|
if (!Number.isFinite(e)) return Z;
|
|
474
491
|
let t = this._deps.now(), r = e - t;
|
|
475
492
|
if (r <= 0) return Z;
|
|
476
|
-
let o = r * (1 -
|
|
493
|
+
let o = r * (1 - lo);
|
|
477
494
|
return Math.max(Z, Math.round(o));
|
|
478
495
|
}
|
|
479
496
|
_scheduleRenewal() {
|
|
@@ -502,7 +519,7 @@ var so, Z, se, rn = W(() => {
|
|
|
502
519
|
this._renewalTimer = setTimeout(() => {
|
|
503
520
|
this._doRenew();
|
|
504
521
|
}, Z), console.warn(
|
|
505
|
-
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${Z}ms (cert
|
|
522
|
+
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${Z}ms (o cert de cliente mTLS do lease expira ~15min ap\xF3s o \xFAltimo swap):`,
|
|
506
523
|
t instanceof Error ? t.message : t
|
|
507
524
|
);
|
|
508
525
|
return;
|
|
@@ -514,10 +531,13 @@ var so, Z, se, rn = W(() => {
|
|
|
514
531
|
/**
|
|
515
532
|
* Recuperação REATIVA: chamado pelo wrapper resiliente (`resilient-pool.ts`)
|
|
516
533
|
* quando uma query plana falha num erro de ESTABELECIMENTO de conexão (cert
|
|
517
|
-
* TLS
|
|
518
|
-
* renovação que não disparou)
|
|
519
|
-
* do
|
|
520
|
-
*
|
|
534
|
+
* TLS de cliente mTLS do lease expirado no meio de uma request, ou timer de
|
|
535
|
+
* renovação que não disparou) ou de AUTENTICAÇÃO (senha rotacionada por
|
|
536
|
+
* `rotate-credentials` antes do próximo renew — SQLSTATE 28P01/28000, ou o
|
|
537
|
+
* 08P01+SASL do PgBouncer; também no `connect()` de uma transação). Emite um
|
|
538
|
+
* lease NOVO e faz o swap do pool interno do wrapper com a credencial e o
|
|
539
|
+
* certificado frescos (o pool velho é drenado com `end()`); o wrapper então
|
|
540
|
+
* re-tenta a operação 1x no pool novo.
|
|
521
541
|
*
|
|
522
542
|
* Vai DIRETO ao lease novo (sem `leaseId`) — a conexão falhou porque o
|
|
523
543
|
* certificado do lease atual está ruim, então `renew` (que depende do lease
|
|
@@ -609,7 +629,7 @@ var so, Z, se, rn = W(() => {
|
|
|
609
629
|
* Exposto como método protegido para testes.
|
|
610
630
|
*/
|
|
611
631
|
async _swapPool(e) {
|
|
612
|
-
if (!
|
|
632
|
+
if (!uo(this._lease, e)) {
|
|
613
633
|
this._lease = e;
|
|
614
634
|
return;
|
|
615
635
|
}
|
|
@@ -636,29 +656,29 @@ var so, Z, se, rn = W(() => {
|
|
|
636
656
|
});
|
|
637
657
|
|
|
638
658
|
// src/db/sql/sql-client.ts
|
|
639
|
-
var
|
|
640
|
-
ft(
|
|
641
|
-
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () =>
|
|
642
|
-
DEFAULT_POOL_IDLE_MS: () =>
|
|
643
|
-
DEFAULT_POOL_MAX: () =>
|
|
644
|
-
LEASE_ENDPOINT: () =>
|
|
645
|
-
LEASE_RENEW_ENDPOINT: () =>
|
|
646
|
-
createDrizzleHandle: () =>
|
|
647
|
-
createHttpLeaseFetcher: () =>
|
|
648
|
-
createPgPool: () =>
|
|
649
|
-
createSqlClientFromConfig: () =>
|
|
650
|
-
createSqlClientWithDeps: () =>
|
|
659
|
+
var fn = {};
|
|
660
|
+
ft(fn, {
|
|
661
|
+
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () => un,
|
|
662
|
+
DEFAULT_POOL_IDLE_MS: () => dn,
|
|
663
|
+
DEFAULT_POOL_MAX: () => ln,
|
|
664
|
+
LEASE_ENDPOINT: () => sn,
|
|
665
|
+
LEASE_RENEW_ENDPOINT: () => an,
|
|
666
|
+
createDrizzleHandle: () => go,
|
|
667
|
+
createHttpLeaseFetcher: () => cn,
|
|
668
|
+
createPgPool: () => ho,
|
|
669
|
+
createSqlClientFromConfig: () => _o,
|
|
670
|
+
createSqlClientWithDeps: () => pn,
|
|
651
671
|
resolvePoolTuning: () => tt
|
|
652
672
|
});
|
|
653
|
-
function
|
|
673
|
+
function fo(n) {
|
|
654
674
|
return n === "workspace" ? "staging" : n === "prod" ? "production" : "dev-local";
|
|
655
675
|
}
|
|
656
|
-
function
|
|
676
|
+
function cn(n) {
|
|
657
677
|
return async (e) => {
|
|
658
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (q(), ne)), r = !!e?.leaseId, o = r ?
|
|
678
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (q(), ne)), r = !!e?.leaseId, o = r ? an : sn, i;
|
|
659
679
|
r ? i = { leaseId: e.leaseId } : (i = {
|
|
660
680
|
productId: n.productId ?? "",
|
|
661
|
-
environment:
|
|
681
|
+
environment: fo(n.env ?? "prod")
|
|
662
682
|
}, e?.database !== void 0 && (i.database = e.database));
|
|
663
683
|
let s;
|
|
664
684
|
try {
|
|
@@ -671,23 +691,23 @@ function an(n) {
|
|
|
671
691
|
retries: 2
|
|
672
692
|
});
|
|
673
693
|
} catch (a) {
|
|
674
|
-
let
|
|
675
|
-
if (
|
|
676
|
-
let
|
|
694
|
+
let d = r ? "renovar" : "obter", p = a instanceof Error ? a.message : String(a), l = a instanceof exports.NeetruError ? a.reason : void 0;
|
|
695
|
+
if (l === "suspended" || l === "archived" || l === "revoked") {
|
|
696
|
+
let u = l === "suspended" ? "ambiente suspenso \u2014 a m\xE1quina que o servia parou; ser\xE1 religado quando o ambiente voltar" : l === "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";
|
|
677
697
|
throw new exports.NeetruDbError(
|
|
678
698
|
"db_suspended",
|
|
679
|
-
`Falha ao ${
|
|
699
|
+
`Falha ao ${d} lease SQL: ${u} (reason=${l}).`
|
|
680
700
|
);
|
|
681
701
|
}
|
|
682
702
|
throw new exports.NeetruDbError(
|
|
683
703
|
"db_unavailable",
|
|
684
|
-
`Falha ao ${
|
|
704
|
+
`Falha ao ${d} lease SQL: ${p}`
|
|
685
705
|
);
|
|
686
706
|
}
|
|
687
|
-
return
|
|
707
|
+
return mo(s);
|
|
688
708
|
};
|
|
689
709
|
}
|
|
690
|
-
function
|
|
710
|
+
function mo(n) {
|
|
691
711
|
if (!n || typeof n != "object")
|
|
692
712
|
throw new exports.NeetruDbError("db_unavailable", "Resposta de lease inv\xE1lida (n\xE3o \xE9 objeto).");
|
|
693
713
|
let t = n.lease;
|
|
@@ -729,12 +749,12 @@ function tt() {
|
|
|
729
749
|
return {
|
|
730
750
|
// Teto 100: bem abaixo do `max_client_conn = 500` do PgBouncer, com folga
|
|
731
751
|
// para múltiplas instâncias do app dividirem o mesmo front-door.
|
|
732
|
-
max: et("NEETRU_DB_POOL_MAX",
|
|
752
|
+
max: et("NEETRU_DB_POOL_MAX", ln, 1, 100),
|
|
733
753
|
// Teto 15min = janela do cert; piso 1s.
|
|
734
|
-
idleTimeoutMillis: et("NEETRU_DB_POOL_IDLE_MS",
|
|
754
|
+
idleTimeoutMillis: et("NEETRU_DB_POOL_IDLE_MS", dn, 1e3, 9e5),
|
|
735
755
|
connectionTimeoutMillis: et(
|
|
736
756
|
"NEETRU_DB_POOL_CONNECT_TIMEOUT_MS",
|
|
737
|
-
|
|
757
|
+
un,
|
|
738
758
|
1e3,
|
|
739
759
|
12e4
|
|
740
760
|
),
|
|
@@ -742,7 +762,7 @@ function tt() {
|
|
|
742
762
|
keepAliveInitialDelayMillis: 1e4
|
|
743
763
|
};
|
|
744
764
|
}
|
|
745
|
-
async function
|
|
765
|
+
async function ho(n) {
|
|
746
766
|
let { Pool: e } = await import(
|
|
747
767
|
/* webpackIgnore: true */
|
|
748
768
|
'pg'
|
|
@@ -762,14 +782,14 @@ async function po(n) {
|
|
|
762
782
|
...tt()
|
|
763
783
|
});
|
|
764
784
|
}
|
|
765
|
-
async function
|
|
785
|
+
async function go(n, e) {
|
|
766
786
|
let { drizzle: t } = await import(
|
|
767
787
|
/* webpackIgnore: true */
|
|
768
788
|
'drizzle-orm/node-postgres'
|
|
769
789
|
);
|
|
770
790
|
return t(n, { schema: e });
|
|
771
791
|
}
|
|
772
|
-
function
|
|
792
|
+
function yo(n, e) {
|
|
773
793
|
let t = null, r = null, o = () => t ? Promise.resolve(t) : (r || (r = e().then((s) => (t = s, r = null, s)).catch((s) => {
|
|
774
794
|
throw r = null, s;
|
|
775
795
|
})), r), i = {
|
|
@@ -785,52 +805,52 @@ function mo(n, e) {
|
|
|
785
805
|
};
|
|
786
806
|
return n && Object.setPrototypeOf(i, n), i;
|
|
787
807
|
}
|
|
788
|
-
async function
|
|
808
|
+
async function pn(n, e) {
|
|
789
809
|
let t = e?.database !== void 0 ? { ...n, database: e.database } : n;
|
|
790
810
|
if (t.lazy) {
|
|
791
|
-
let
|
|
792
|
-
if (!
|
|
811
|
+
let d = null, p = yo(t.poolPrototype ?? null, () => {
|
|
812
|
+
if (!d)
|
|
793
813
|
throw new exports.NeetruDbError("db_unavailable", "SQL client n\xE3o inicializado (manager ausente).");
|
|
794
|
-
return
|
|
795
|
-
}),
|
|
814
|
+
return d._ensureInitialLease();
|
|
815
|
+
}), l = Ye(p), u;
|
|
796
816
|
try {
|
|
797
|
-
|
|
817
|
+
u = t.createDrizzle(l, t.schema);
|
|
798
818
|
} catch (m) {
|
|
799
819
|
let h = m instanceof Error ? m.message : String(m);
|
|
800
820
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${h}`);
|
|
801
821
|
}
|
|
802
|
-
let f = new se(null,
|
|
803
|
-
return
|
|
822
|
+
let f = new se(null, l, u, t);
|
|
823
|
+
return d = f, l.__neetruSetReLease(() => f._reactiveReLease()), f;
|
|
804
824
|
}
|
|
805
825
|
let r;
|
|
806
826
|
try {
|
|
807
827
|
e?.database !== void 0 ? r = await t.fetchLease({ database: e.database }) : r = await t.fetchLease();
|
|
808
|
-
} catch (
|
|
809
|
-
if (
|
|
810
|
-
let
|
|
811
|
-
throw new exports.NeetruDbError("db_unavailable", `Falha ao obter lease inicial: ${
|
|
828
|
+
} catch (d) {
|
|
829
|
+
if (d instanceof exports.NeetruDbError) throw d;
|
|
830
|
+
let p = d instanceof Error ? d.message : String(d);
|
|
831
|
+
throw new exports.NeetruDbError("db_unavailable", `Falha ao obter lease inicial: ${p}`);
|
|
812
832
|
}
|
|
813
833
|
let o;
|
|
814
834
|
try {
|
|
815
835
|
o = t.createPool(r);
|
|
816
|
-
} catch (
|
|
817
|
-
let
|
|
818
|
-
throw new exports.NeetruDbError("db_unavailable", `Falha ao abrir pool: ${
|
|
836
|
+
} catch (d) {
|
|
837
|
+
let p = d instanceof Error ? d.message : String(d);
|
|
838
|
+
throw new exports.NeetruDbError("db_unavailable", `Falha ao abrir pool: ${p}`);
|
|
819
839
|
}
|
|
820
840
|
let i = Ye(o), s;
|
|
821
841
|
try {
|
|
822
842
|
s = t.createDrizzle(i, t.schema);
|
|
823
|
-
} catch (
|
|
843
|
+
} catch (d) {
|
|
824
844
|
await o.end().catch(() => {
|
|
825
845
|
});
|
|
826
|
-
let
|
|
827
|
-
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${
|
|
846
|
+
let p = d instanceof Error ? d.message : String(d);
|
|
847
|
+
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${p}`);
|
|
828
848
|
}
|
|
829
849
|
let a = new se(r, i, s, t);
|
|
830
850
|
return i.__neetruSetReLease(() => a._reactiveReLease()), a;
|
|
831
851
|
}
|
|
832
|
-
async function
|
|
833
|
-
let r =
|
|
852
|
+
async function _o(n, e, t) {
|
|
853
|
+
let r = cn(n), o, i;
|
|
834
854
|
try {
|
|
835
855
|
o = await import(
|
|
836
856
|
/* webpackIgnore: true */
|
|
@@ -839,36 +859,36 @@ async function ho(n, e, t) {
|
|
|
839
859
|
/* webpackIgnore: true */
|
|
840
860
|
'drizzle-orm/node-postgres'
|
|
841
861
|
);
|
|
842
|
-
} catch (
|
|
862
|
+
} catch (p) {
|
|
843
863
|
throw new exports.NeetruDbError(
|
|
844
864
|
"db_unavailable",
|
|
845
|
-
`[client.db.sql] requer pg + drizzle-orm como peerDependencies do servidor. Instale: npm install pg drizzle-orm. (${
|
|
865
|
+
`[client.db.sql] requer pg + drizzle-orm como peerDependencies do servidor. Instale: npm install pg drizzle-orm. (${p.message})`
|
|
846
866
|
);
|
|
847
867
|
}
|
|
848
868
|
let s = o.Pool ?? o.default?.Pool;
|
|
849
|
-
return
|
|
869
|
+
return pn(
|
|
850
870
|
{
|
|
851
871
|
fetchLease: r,
|
|
852
|
-
createPool: (
|
|
853
|
-
let
|
|
854
|
-
ca:
|
|
855
|
-
cert:
|
|
856
|
-
key:
|
|
872
|
+
createPool: (p) => {
|
|
873
|
+
let l = p.sslca ? {
|
|
874
|
+
ca: p.sslca,
|
|
875
|
+
cert: p.clientCert ?? void 0,
|
|
876
|
+
key: p.clientKey ?? void 0,
|
|
857
877
|
rejectUnauthorized: true
|
|
858
878
|
} : false;
|
|
859
879
|
return new s({
|
|
860
|
-
host:
|
|
861
|
-
port:
|
|
862
|
-
database:
|
|
863
|
-
user:
|
|
864
|
-
password:
|
|
865
|
-
ssl:
|
|
880
|
+
host: p.host,
|
|
881
|
+
port: p.port,
|
|
882
|
+
database: p.dbName,
|
|
883
|
+
user: p.user,
|
|
884
|
+
password: p.password,
|
|
885
|
+
ssl: l,
|
|
866
886
|
// Tuning centralizado (env-tunável) — ver resolvePoolTuning + auditoria
|
|
867
887
|
// docs/_review/AUDITORIA_DB_CONN_PERF_2026-09-06.md no Core.
|
|
868
888
|
...tt()
|
|
869
889
|
});
|
|
870
890
|
},
|
|
871
|
-
createDrizzle: (
|
|
891
|
+
createDrizzle: (p, l) => i.drizzle(p, { schema: l }),
|
|
872
892
|
schema: e,
|
|
873
893
|
// Lease inicial LAZY (motor de ambientes suspenso): `client.db.sql()` não
|
|
874
894
|
// pode explodir o `import` do módulo do produto quando o ambiente está
|
|
@@ -881,13 +901,13 @@ async function ho(n, e, t) {
|
|
|
881
901
|
t
|
|
882
902
|
);
|
|
883
903
|
}
|
|
884
|
-
var
|
|
904
|
+
var sn, an, ln, dn, un, mn = W(() => {
|
|
885
905
|
z();
|
|
886
906
|
C();
|
|
887
|
-
|
|
907
|
+
on();
|
|
888
908
|
Ze();
|
|
889
|
-
|
|
890
|
-
|
|
909
|
+
sn = "/api/sdk/v1/db/lease", an = "/api/sdk/v1/db/lease/renew";
|
|
910
|
+
ln = 10, dn = 6e4, un = 1e4;
|
|
891
911
|
});
|
|
892
912
|
var mt = /* @__PURE__ */ new Map(), ee = 3600 * 1e3;
|
|
893
913
|
function ye(n, e = ee) {
|
|
@@ -926,11 +946,11 @@ function yt(n) {
|
|
|
926
946
|
|
|
927
947
|
// src/base-url-allowlist.ts
|
|
928
948
|
C();
|
|
929
|
-
var _t = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]),
|
|
949
|
+
var _t = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]), cr = ".neetru.com", lr = "neetru.com", dr = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\.(test|localhost)$/;
|
|
930
950
|
function bt(n) {
|
|
931
|
-
return
|
|
951
|
+
return dr.test(n);
|
|
932
952
|
}
|
|
933
|
-
function
|
|
953
|
+
function ur(n) {
|
|
934
954
|
return n.toLowerCase().replace(/\.+$/, "");
|
|
935
955
|
}
|
|
936
956
|
function vt(n, e = []) {
|
|
@@ -942,8 +962,8 @@ function vt(n, e = []) {
|
|
|
942
962
|
} catch {
|
|
943
963
|
throw new exports.NeetruError("invalid_config", `baseUrl is not a valid URL: ${n}`);
|
|
944
964
|
}
|
|
945
|
-
let r = t.protocol, o =
|
|
946
|
-
if (
|
|
965
|
+
let r = t.protocol, o = ur(t.hostname);
|
|
966
|
+
if (pr())
|
|
947
967
|
return typeof console < "u" && console.warn(
|
|
948
968
|
`[neetru-sdk] NEETRU_ALLOW_INSECURE_BASEURL=1 ativo. baseUrl=${n} aceito sem valida\xE7\xE3o.`
|
|
949
969
|
), n.replace(/\/+$/, "");
|
|
@@ -960,9 +980,9 @@ function vt(n, e = []) {
|
|
|
960
980
|
);
|
|
961
981
|
return n.replace(/\/+$/, "");
|
|
962
982
|
}
|
|
963
|
-
let i = o ===
|
|
964
|
-
let
|
|
965
|
-
return o ===
|
|
983
|
+
let i = o === lr || o.endsWith(cr), s = e.some((d) => {
|
|
984
|
+
let p = d.toLowerCase();
|
|
985
|
+
return o === p || o.endsWith(`.${p}`);
|
|
966
986
|
}), a = _t.has(o) || bt(o);
|
|
967
987
|
if (!i && !s && !a)
|
|
968
988
|
throw new exports.NeetruError(
|
|
@@ -971,7 +991,7 @@ function vt(n, e = []) {
|
|
|
971
991
|
);
|
|
972
992
|
return n.replace(/\/+$/, "");
|
|
973
993
|
}
|
|
974
|
-
function
|
|
994
|
+
function pr() {
|
|
975
995
|
try {
|
|
976
996
|
let e = globalThis.process?.env?.NEETRU_ALLOW_INSECURE_BASEURL;
|
|
977
997
|
if (e === "1" || e === "true") return !0;
|
|
@@ -1055,9 +1075,9 @@ function Ut(n) {
|
|
|
1055
1075
|
|
|
1056
1076
|
// src/entitlements.ts
|
|
1057
1077
|
C();
|
|
1058
|
-
var
|
|
1078
|
+
var Ir = "client.entitlements.check()/checkDetailed() est\xE3o DEPRECADOS \u2014 apontavam para GET /api/v1/sdk/entitlements/check, um endpoint que o Core nunca escreve (sempre retornava not_subscribed). Use client.usage.check(resource, { productId, tenantId }) no lugar \u2014 mesmo resultado (allowed/reason/remaining/limit/behavior), endpoint real.";
|
|
1059
1079
|
function Mt() {
|
|
1060
|
-
throw new exports.NeetruError("validation_failed",
|
|
1080
|
+
throw new exports.NeetruError("validation_failed", Ir);
|
|
1061
1081
|
}
|
|
1062
1082
|
function qt(n) {
|
|
1063
1083
|
return {
|
|
@@ -1085,13 +1105,13 @@ function qt(n) {
|
|
|
1085
1105
|
C();
|
|
1086
1106
|
q();
|
|
1087
1107
|
var Ft = ["debug", "info", "warn", "error", "fatal"];
|
|
1088
|
-
function
|
|
1108
|
+
function Cr() {
|
|
1089
1109
|
return typeof globalThis < "u" && typeof globalThis.window < "u";
|
|
1090
1110
|
}
|
|
1091
1111
|
function jt() {
|
|
1092
|
-
return
|
|
1112
|
+
return Cr() ? { path: "/api/sdk/v1/telemetry/event/public", requireAuth: false } : { path: "/api/sdk/v1/telemetry/event", requireAuth: true };
|
|
1093
1113
|
}
|
|
1094
|
-
function
|
|
1114
|
+
function Ar(n) {
|
|
1095
1115
|
switch (n) {
|
|
1096
1116
|
case "debug":
|
|
1097
1117
|
return console.debug.bind(console);
|
|
@@ -1107,30 +1127,30 @@ function Cr(n) {
|
|
|
1107
1127
|
return console.log.bind(console);
|
|
1108
1128
|
}
|
|
1109
1129
|
}
|
|
1110
|
-
var
|
|
1130
|
+
var Er = 500, Pr = 50;
|
|
1111
1131
|
function Wt(n) {
|
|
1112
1132
|
let e = [], t = null, r = null;
|
|
1113
1133
|
async function o() {
|
|
1114
1134
|
if (e.length === 0) return;
|
|
1115
1135
|
let s = e.splice(0, e.length), a = Promise.allSettled(
|
|
1116
|
-
s.map(async (
|
|
1136
|
+
s.map(async (d) => {
|
|
1117
1137
|
try {
|
|
1118
|
-
let
|
|
1119
|
-
|
|
1120
|
-
let
|
|
1138
|
+
let p = { name: d.name }, l = d.productSlug ?? n.productId;
|
|
1139
|
+
l && (p.productSlug = l), d.properties && typeof d.properties == "object" && (p.properties = d.properties), d.timestamp && (p.timestamp = d.timestamp);
|
|
1140
|
+
let u = jt();
|
|
1121
1141
|
await S(n, {
|
|
1122
1142
|
method: "POST",
|
|
1123
|
-
path:
|
|
1124
|
-
body:
|
|
1125
|
-
requireAuth:
|
|
1143
|
+
path: u.path,
|
|
1144
|
+
body: p,
|
|
1145
|
+
requireAuth: u.requireAuth,
|
|
1126
1146
|
retries: 1,
|
|
1127
1147
|
// caça-bruxas: retry sem Idempotency-Key duplicava o evento se o
|
|
1128
1148
|
// servidor já tinha persistido antes da falha transiente. Mesmo
|
|
1129
1149
|
// padrão do usage.report (key estável por chamada, reusada no retry).
|
|
1130
1150
|
idempotencyKey: !0
|
|
1131
1151
|
});
|
|
1132
|
-
} catch (
|
|
1133
|
-
typeof console < "u" && console.warn("[neetru-sdk] telemetry.track flush failed for event",
|
|
1152
|
+
} catch (p) {
|
|
1153
|
+
typeof console < "u" && console.warn("[neetru-sdk] telemetry.track flush failed for event", d.name, p);
|
|
1134
1154
|
}
|
|
1135
1155
|
})
|
|
1136
1156
|
);
|
|
@@ -1141,7 +1161,7 @@ function Wt(n) {
|
|
|
1141
1161
|
function i() {
|
|
1142
1162
|
t || (t = setTimeout(() => {
|
|
1143
1163
|
o();
|
|
1144
|
-
},
|
|
1164
|
+
}, Er));
|
|
1145
1165
|
}
|
|
1146
1166
|
return {
|
|
1147
1167
|
/**
|
|
@@ -1163,17 +1183,17 @@ function Wt(n) {
|
|
|
1163
1183
|
throw new exports.NeetruError("validation_failed", "event.name is required");
|
|
1164
1184
|
if (s.name.length > 128)
|
|
1165
1185
|
throw new exports.NeetruError("validation_failed", "event.name max 128 chars");
|
|
1166
|
-
let a = { name: s.name },
|
|
1167
|
-
|
|
1168
|
-
let
|
|
1186
|
+
let a = { name: s.name }, d = s.productSlug ?? n.productId;
|
|
1187
|
+
d && (a.productSlug = d), s.properties && typeof s.properties == "object" && (a.properties = s.properties), s.timestamp && (a.timestamp = s.timestamp);
|
|
1188
|
+
let p = jt(), l = await S(n, {
|
|
1169
1189
|
method: "POST",
|
|
1170
|
-
path:
|
|
1190
|
+
path: p.path,
|
|
1171
1191
|
body: a,
|
|
1172
|
-
requireAuth:
|
|
1192
|
+
requireAuth: p.requireAuth
|
|
1173
1193
|
});
|
|
1174
|
-
if (!
|
|
1194
|
+
if (!l || l.ok !== true || typeof l.eventId != "string")
|
|
1175
1195
|
throw new exports.NeetruError("invalid_response", "Telemetry response missing eventId");
|
|
1176
|
-
return { ok: true, eventId:
|
|
1196
|
+
return { ok: true, eventId: l.eventId };
|
|
1177
1197
|
},
|
|
1178
1198
|
/**
|
|
1179
1199
|
* Fire-and-forget: enqueue + flush 500ms debounce. Não retorna `eventId`
|
|
@@ -1186,7 +1206,7 @@ function Wt(n) {
|
|
|
1186
1206
|
* ```
|
|
1187
1207
|
*/
|
|
1188
1208
|
track(s) {
|
|
1189
|
-
!s || typeof s != "object" || typeof s.name != "string" || s.name.length === 0 || s.name.length > 128 || (e.push(s), e.length >=
|
|
1209
|
+
!s || typeof s != "object" || typeof s.name != "string" || s.name.length === 0 || s.name.length > 128 || (e.push(s), e.length >= Pr ? (t && (clearTimeout(t), t = null), o()) : i());
|
|
1190
1210
|
},
|
|
1191
1211
|
/**
|
|
1192
1212
|
* Força flush imediato da queue de `track()`. Resolva antes de
|
|
@@ -1220,35 +1240,35 @@ function Wt(n) {
|
|
|
1220
1240
|
if (s.message.length > 4e3)
|
|
1221
1241
|
throw new exports.NeetruError("validation_failed", "message max 4000 chars");
|
|
1222
1242
|
if (n.env === "dev")
|
|
1223
|
-
return
|
|
1243
|
+
return Ar(s.level)(`[neetru-sdk] ${s.level}: ${s.message}`, s.metadata ?? {}), { ok: true, mode: "mock" };
|
|
1224
1244
|
let a = {
|
|
1225
1245
|
level: s.level,
|
|
1226
1246
|
message: s.message
|
|
1227
1247
|
};
|
|
1228
1248
|
s.metadata && (a.metadata = s.metadata);
|
|
1229
|
-
let
|
|
1230
|
-
|
|
1231
|
-
let
|
|
1232
|
-
if (!
|
|
1249
|
+
let d = s.productSlug ?? n.productId;
|
|
1250
|
+
d && (a.productSlug = d);
|
|
1251
|
+
let p = s.correlationId;
|
|
1252
|
+
if (!p)
|
|
1233
1253
|
try {
|
|
1234
1254
|
let f = globalThis.NEETRU_CORRELATION_ID;
|
|
1235
|
-
typeof f == "string" && f.length > 0 && (
|
|
1255
|
+
typeof f == "string" && f.length > 0 && (p = f);
|
|
1236
1256
|
} catch {
|
|
1237
1257
|
}
|
|
1238
|
-
let
|
|
1239
|
-
|
|
1240
|
-
let
|
|
1258
|
+
let l = {};
|
|
1259
|
+
p && (l["x-correlation-id"] = p);
|
|
1260
|
+
let u = await S(n, {
|
|
1241
1261
|
method: "POST",
|
|
1242
1262
|
path: "/api/sdk/v1/telemetry/log",
|
|
1243
1263
|
body: a,
|
|
1244
1264
|
requireAuth: true,
|
|
1245
|
-
headers:
|
|
1265
|
+
headers: l
|
|
1246
1266
|
});
|
|
1247
|
-
if (!
|
|
1267
|
+
if (!u || u.ok !== true)
|
|
1248
1268
|
throw new exports.NeetruError("invalid_response", "Telemetry log response missing ok");
|
|
1249
1269
|
return {
|
|
1250
1270
|
ok: true,
|
|
1251
|
-
logId: typeof
|
|
1271
|
+
logId: typeof u.logId == "string" ? u.logId : void 0,
|
|
1252
1272
|
mode: "http"
|
|
1253
1273
|
};
|
|
1254
1274
|
}
|
|
@@ -1361,7 +1381,7 @@ function Vt(n) {
|
|
|
1361
1381
|
// src/support.ts
|
|
1362
1382
|
C();
|
|
1363
1383
|
q();
|
|
1364
|
-
var Ve = ["low", "normal", "high", "urgent"],
|
|
1384
|
+
var Ve = ["low", "normal", "high", "urgent"], Dr = ["open", "pending", "resolved", "closed"], Bt = 50;
|
|
1365
1385
|
function Kt(n) {
|
|
1366
1386
|
if (!n || typeof n != "object")
|
|
1367
1387
|
throw new exports.NeetruError("invalid_response", "Ticket response is not an object");
|
|
@@ -1374,7 +1394,7 @@ function Kt(n) {
|
|
|
1374
1394
|
subject: typeof e.subject == "string" ? e.subject : "",
|
|
1375
1395
|
message: typeof e.message == "string" ? e.message : "",
|
|
1376
1396
|
severity: Ve.includes(e.severity) ? e.severity : "normal",
|
|
1377
|
-
status:
|
|
1397
|
+
status: Dr.includes(e.status) ? e.status : "open",
|
|
1378
1398
|
createdAt: typeof e.createdAt == "string" ? e.createdAt : (/* @__PURE__ */ new Date()).toISOString(),
|
|
1379
1399
|
updatedAt: typeof e.updatedAt == "string" ? e.updatedAt : void 0,
|
|
1380
1400
|
productSlug: typeof e.productId == "string" ? e.productId : typeof e.productSlug == "string" ? e.productSlug : void 0,
|
|
@@ -1411,12 +1431,12 @@ function Ht(n, ...e) {
|
|
|
1411
1431
|
"productId required (pass to input.productSlug or set on createNeetruClient)"
|
|
1412
1432
|
);
|
|
1413
1433
|
}
|
|
1414
|
-
var
|
|
1434
|
+
var Nr = "X-Neetru-End-User-Token", xr = "Esta opera\xE7\xE3o de suporte exige um usu\xE1rio final logado: o Core s\xF3 mostra/altera os tickets do pr\xF3prio usu\xE1rio (header X-Neetru-End-User-Token com o id_token OIDC). Fa\xE7a login com client.auth.signIn() e tente de novo.", Or = "O Core exige a identidade do usu\xE1rio final para esta opera\xE7\xE3o (403 end_user_token_required) e nenhum id_token foi aceito. Fa\xE7a login com client.auth.signIn() e tente de novo.", Lr = "O Core recusou o token do usu\xE1rio final (401 invalid_end_user_token \u2014 sess\xE3o expirada, usu\xE1rio inativo ou token que n\xE3o \xE9 id_token) e a renova\xE7\xE3o autom\xE1tica n\xE3o resolveu. Fa\xE7a login novamente com client.auth.signIn() e tente de novo.", Ur = "O id_token do usu\xE1rio final foi emitido para o client OIDC de OUTRO produto (403 end_user_token_product_mismatch). Confira se o login (client.auth / clientId OIDC) e a apiKey/productId do createNeetruClient s\xE3o do MESMO produto.";
|
|
1415
1435
|
function Gt(n) {
|
|
1416
1436
|
if (n instanceof exports.NeetruError && !(n instanceof exports.NeetruAuthRequiredError)) {
|
|
1417
1437
|
if (n.code === "end_user_token_required")
|
|
1418
1438
|
throw new exports.NeetruAuthRequiredError(
|
|
1419
|
-
|
|
1439
|
+
Or,
|
|
1420
1440
|
"end_user_token_required",
|
|
1421
1441
|
n.status,
|
|
1422
1442
|
n.requestId,
|
|
@@ -1424,7 +1444,7 @@ function Gt(n) {
|
|
|
1424
1444
|
);
|
|
1425
1445
|
if (n.code === "invalid_end_user_token")
|
|
1426
1446
|
throw new exports.NeetruAuthRequiredError(
|
|
1427
|
-
|
|
1447
|
+
Lr,
|
|
1428
1448
|
"invalid_end_user_token",
|
|
1429
1449
|
n.status,
|
|
1430
1450
|
n.requestId,
|
|
@@ -1433,7 +1453,7 @@ function Gt(n) {
|
|
|
1433
1453
|
if (n.code === "end_user_token_product_mismatch")
|
|
1434
1454
|
throw new exports.NeetruError(
|
|
1435
1455
|
"forbidden",
|
|
1436
|
-
|
|
1456
|
+
Ur,
|
|
1437
1457
|
n.status,
|
|
1438
1458
|
n.requestId,
|
|
1439
1459
|
"end_user_token_product_mismatch",
|
|
@@ -1442,7 +1462,7 @@ function Gt(n) {
|
|
|
1442
1462
|
}
|
|
1443
1463
|
throw n;
|
|
1444
1464
|
}
|
|
1445
|
-
function
|
|
1465
|
+
function Mr(n) {
|
|
1446
1466
|
return n instanceof exports.NeetruError && n.code === "invalid_end_user_token";
|
|
1447
1467
|
}
|
|
1448
1468
|
function Qt(n, e = {}) {
|
|
@@ -1463,36 +1483,36 @@ function Qt(n, e = {}) {
|
|
|
1463
1483
|
}
|
|
1464
1484
|
}
|
|
1465
1485
|
function o(a) {
|
|
1466
|
-
return a ? { [
|
|
1486
|
+
return a ? { [Nr]: a } : {};
|
|
1467
1487
|
}
|
|
1468
|
-
async function i(a,
|
|
1469
|
-
let
|
|
1470
|
-
if (!
|
|
1471
|
-
throw new exports.NeetruAuthRequiredError(
|
|
1488
|
+
async function i(a, d) {
|
|
1489
|
+
let p = await t();
|
|
1490
|
+
if (!p && a && await r() && (p = await t()), !p && a)
|
|
1491
|
+
throw new exports.NeetruAuthRequiredError(xr, "no_signed_in_user");
|
|
1472
1492
|
try {
|
|
1473
|
-
return await
|
|
1474
|
-
} catch (
|
|
1475
|
-
if (
|
|
1476
|
-
let
|
|
1477
|
-
if (
|
|
1493
|
+
return await d(o(p));
|
|
1494
|
+
} catch (l) {
|
|
1495
|
+
if (p && Mr(l) && await r()) {
|
|
1496
|
+
let u = await t();
|
|
1497
|
+
if (u)
|
|
1478
1498
|
try {
|
|
1479
|
-
return await
|
|
1499
|
+
return await d(o(u));
|
|
1480
1500
|
} catch (f) {
|
|
1481
1501
|
Gt(f);
|
|
1482
1502
|
}
|
|
1483
1503
|
}
|
|
1484
|
-
Gt(
|
|
1504
|
+
Gt(l);
|
|
1485
1505
|
}
|
|
1486
1506
|
}
|
|
1487
|
-
async function s(a,
|
|
1507
|
+
async function s(a, d = {}) {
|
|
1488
1508
|
await i(
|
|
1489
1509
|
true,
|
|
1490
|
-
(
|
|
1510
|
+
(p) => S(n, {
|
|
1491
1511
|
method: "POST",
|
|
1492
1512
|
path: a,
|
|
1493
1513
|
requireAuth: !0,
|
|
1494
|
-
headers:
|
|
1495
|
-
...
|
|
1514
|
+
headers: p,
|
|
1515
|
+
...d
|
|
1496
1516
|
})
|
|
1497
1517
|
);
|
|
1498
1518
|
}
|
|
@@ -1519,25 +1539,25 @@ function Qt(n, e = {}) {
|
|
|
1519
1539
|
"validation_failed",
|
|
1520
1540
|
`severity must be one of ${Ve.join(", ")}`
|
|
1521
1541
|
);
|
|
1522
|
-
let
|
|
1542
|
+
let p = {
|
|
1523
1543
|
productId: Ht(n, a.productSlug),
|
|
1524
1544
|
subject: a.subject,
|
|
1525
1545
|
message: a.message,
|
|
1526
1546
|
severity: a.severity ?? "normal"
|
|
1527
1547
|
};
|
|
1528
|
-
a.metadata && typeof a.metadata == "object" && (
|
|
1529
|
-
let
|
|
1548
|
+
a.metadata && typeof a.metadata == "object" && (p.metadata = a.metadata), typeof a.pageUrl == "string" && (p.pageUrl = a.pageUrl), typeof a.screenshotBase64 == "string" && (p.screenshotBase64 = a.screenshotBase64), typeof a.consoleLogs == "string" && (p.consoleLogs = a.consoleLogs);
|
|
1549
|
+
let l = await i(
|
|
1530
1550
|
false,
|
|
1531
1551
|
(f) => S(n, {
|
|
1532
1552
|
method: "POST",
|
|
1533
1553
|
path: "/api/sdk/v1/support/tickets",
|
|
1534
|
-
body:
|
|
1554
|
+
body: p,
|
|
1535
1555
|
requireAuth: !0,
|
|
1536
1556
|
idempotencyKey: !0,
|
|
1537
1557
|
headers: f
|
|
1538
1558
|
})
|
|
1539
|
-
),
|
|
1540
|
-
return Kt(
|
|
1559
|
+
), u = l && typeof l == "object" && "ticket" in l ? l.ticket : l;
|
|
1560
|
+
return Kt(u);
|
|
1541
1561
|
},
|
|
1542
1562
|
/**
|
|
1543
1563
|
* Lista tickets do customer no produto atual. `productSlug` no contexto.
|
|
@@ -1545,23 +1565,23 @@ function Qt(n, e = {}) {
|
|
|
1545
1565
|
* Endpoint canonical: `GET /api/sdk/v1/support/tickets?productId=...`.
|
|
1546
1566
|
*/
|
|
1547
1567
|
async listMyTickets(a) {
|
|
1548
|
-
let
|
|
1549
|
-
if (
|
|
1568
|
+
let d = Ht(n, a?.productSlug), p = a?.limit;
|
|
1569
|
+
if (p !== void 0 && (!Number.isInteger(p) || p < 1 || p > Bt))
|
|
1550
1570
|
throw new exports.NeetruError(
|
|
1551
1571
|
"validation_failed",
|
|
1552
1572
|
`limit must be an integer 1-${Bt} (Core default 20)`
|
|
1553
1573
|
);
|
|
1554
|
-
let
|
|
1574
|
+
let l = await i(
|
|
1555
1575
|
true,
|
|
1556
1576
|
(f) => S(n, {
|
|
1557
1577
|
method: "GET",
|
|
1558
1578
|
path: "/api/sdk/v1/support/tickets",
|
|
1559
|
-
query: { productId:
|
|
1579
|
+
query: { productId: d, limit: p },
|
|
1560
1580
|
requireAuth: !0,
|
|
1561
1581
|
headers: f
|
|
1562
1582
|
})
|
|
1563
1583
|
);
|
|
1564
|
-
return (Array.isArray(
|
|
1584
|
+
return (Array.isArray(l) ? l : l && typeof l == "object" && "tickets" in l ? l.tickets ?? [] : []).map(Kt);
|
|
1565
1585
|
},
|
|
1566
1586
|
/**
|
|
1567
1587
|
* 3.4: marca a notificação de resolução como vista (`resolvedNotifiedAt`,
|
|
@@ -1612,10 +1632,10 @@ function Qt(n, e = {}) {
|
|
|
1612
1632
|
* satisfação/notificação. Endpoint canonical:
|
|
1613
1633
|
* `POST /api/sdk/v1/support/tickets/{id}/contest`.
|
|
1614
1634
|
*/
|
|
1615
|
-
async contestTicket(a,
|
|
1616
|
-
we(a), zt(
|
|
1635
|
+
async contestTicket(a, d) {
|
|
1636
|
+
we(a), zt(d?.comment), await s(
|
|
1617
1637
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/contest`,
|
|
1618
|
-
{ body: { comment:
|
|
1638
|
+
{ body: { comment: d?.comment ?? void 0 } }
|
|
1619
1639
|
);
|
|
1620
1640
|
}
|
|
1621
1641
|
};
|
|
@@ -1647,7 +1667,7 @@ function Be(n, e) {
|
|
|
1647
1667
|
}
|
|
1648
1668
|
return r;
|
|
1649
1669
|
}
|
|
1650
|
-
function
|
|
1670
|
+
function Fr(n, e) {
|
|
1651
1671
|
let t = Be(n, e.field);
|
|
1652
1672
|
if (t === void 0) return false;
|
|
1653
1673
|
let { op: r, value: o } = e;
|
|
@@ -1678,10 +1698,10 @@ function qr(n, e) {
|
|
|
1678
1698
|
);
|
|
1679
1699
|
}
|
|
1680
1700
|
}
|
|
1681
|
-
function
|
|
1682
|
-
return e.every((t) =>
|
|
1701
|
+
function jr(n, e) {
|
|
1702
|
+
return e.every((t) => Fr(n, t));
|
|
1683
1703
|
}
|
|
1684
|
-
function
|
|
1704
|
+
function Wr(n) {
|
|
1685
1705
|
return (e, t) => {
|
|
1686
1706
|
if (n) {
|
|
1687
1707
|
let r = Be(e.data, n.field), o = Be(t.data, n.field), i = re(r, o);
|
|
@@ -1691,7 +1711,7 @@ function jr(n) {
|
|
|
1691
1711
|
return e.id < t.id ? -1 : e.id > t.id ? 1 : 0;
|
|
1692
1712
|
};
|
|
1693
1713
|
}
|
|
1694
|
-
function
|
|
1714
|
+
function $r(n, e) {
|
|
1695
1715
|
let t = n.findIndex((r) => r.id === e.docId);
|
|
1696
1716
|
return t === -1 ? n : e.type === "startAfter" ? n.slice(t + 1) : n.slice(0, t);
|
|
1697
1717
|
}
|
|
@@ -1707,13 +1727,13 @@ var J = class n {
|
|
|
1707
1727
|
* 5. Corta em `limit`.
|
|
1708
1728
|
*/
|
|
1709
1729
|
evaluate(e, t) {
|
|
1710
|
-
let r = e.filter((
|
|
1730
|
+
let r = e.filter((l) => !l.meta.deleted), o = t.where ?? [];
|
|
1711
1731
|
o.length > 0 && (r = r.filter(
|
|
1712
|
-
(
|
|
1732
|
+
(l) => jr(l.data, o)
|
|
1713
1733
|
));
|
|
1714
|
-
let i =
|
|
1734
|
+
let i = Wr(t.orderBy), s = [...r].sort(i), a = t.cursor ? $r(s, t.cursor) : s, d = Math.min(t.limit ?? 20, 500);
|
|
1715
1735
|
return {
|
|
1716
|
-
docs: a.slice(0,
|
|
1736
|
+
docs: a.slice(0, d).map((l) => ({ id: l.id, data: l.data })),
|
|
1717
1737
|
// `incomplete` é sempre true aqui — o QueryEngine não sabe se o cache
|
|
1718
1738
|
// tem todos os docs da coleção. É responsabilidade do chamador (LocalStore)
|
|
1719
1739
|
// injetar o flag de completude baseado nos metadados do query_cache.
|
|
@@ -1731,7 +1751,7 @@ var J = class n {
|
|
|
1731
1751
|
};
|
|
1732
1752
|
|
|
1733
1753
|
// src/db/offline/local-store.ts
|
|
1734
|
-
var
|
|
1754
|
+
var Br = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_meta", X = "conflict_log", Se = class {
|
|
1735
1755
|
db = null;
|
|
1736
1756
|
dbName;
|
|
1737
1757
|
constructor(e) {
|
|
@@ -1743,7 +1763,7 @@ var Vr = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_met
|
|
|
1743
1763
|
* Idempotente — chamadas subsequentes são no-ops se o banco já está aberto.
|
|
1744
1764
|
*/
|
|
1745
1765
|
async open() {
|
|
1746
|
-
this.db === null && (this.db = await idb.openDB(this.dbName,
|
|
1766
|
+
this.db === null && (this.db = await idb.openDB(this.dbName, Br, {
|
|
1747
1767
|
upgrade(e) {
|
|
1748
1768
|
if (!e.objectStoreNames.contains(V)) {
|
|
1749
1769
|
let t = e.createObjectStore(V, {
|
|
@@ -2151,18 +2171,18 @@ var Re = class {
|
|
|
2151
2171
|
* Atômico: a persistência final é uma única operação putMutation.
|
|
2152
2172
|
*/
|
|
2153
2173
|
async enqueue(e) {
|
|
2154
|
-
let { collection: t, op: r, payload: o, baseVersion: i, batchId: s, upsert: a } = e,
|
|
2174
|
+
let { collection: t, op: r, payload: o, baseVersion: i, batchId: s, upsert: a } = e, d = e.docId ?? (r === "add" ? Ke() : "");
|
|
2155
2175
|
await this.syncSeq();
|
|
2156
|
-
let
|
|
2157
|
-
if (
|
|
2158
|
-
let m = this.tryCoalesce(
|
|
2176
|
+
let l = (await this.store.listMutations({ collection: t, docId: d })).filter((m) => m.status === "queued").sort((m, h) => h.seq - m.seq)[0];
|
|
2177
|
+
if (l !== void 0) {
|
|
2178
|
+
let m = this.tryCoalesce(l, r, o, s, a);
|
|
2159
2179
|
if (m !== null) {
|
|
2160
2180
|
if ("removed" in m)
|
|
2161
|
-
return await this.store.deleteMutation(
|
|
2162
|
-
seq:
|
|
2181
|
+
return await this.store.deleteMutation(l.mutationId), {
|
|
2182
|
+
seq: l.seq,
|
|
2163
2183
|
mutationId: Ke(),
|
|
2164
2184
|
collection: t,
|
|
2165
|
-
docId:
|
|
2185
|
+
docId: d,
|
|
2166
2186
|
op: "remove",
|
|
2167
2187
|
payload: null,
|
|
2168
2188
|
baseVersion: i,
|
|
@@ -2180,7 +2200,7 @@ var Re = class {
|
|
|
2180
2200
|
seq: this.nextSeq(),
|
|
2181
2201
|
mutationId: Ke(),
|
|
2182
2202
|
collection: t,
|
|
2183
|
-
docId:
|
|
2203
|
+
docId: d,
|
|
2184
2204
|
op: r,
|
|
2185
2205
|
payload: o,
|
|
2186
2206
|
baseVersion: i,
|
|
@@ -2292,10 +2312,10 @@ var Re = class {
|
|
|
2292
2312
|
|
|
2293
2313
|
// src/db/field-value.ts
|
|
2294
2314
|
var ze = "__neetru__";
|
|
2295
|
-
function
|
|
2315
|
+
function Kr() {
|
|
2296
2316
|
return { __neetru__: "serverTimestamp" };
|
|
2297
2317
|
}
|
|
2298
|
-
function
|
|
2318
|
+
function zr(n) {
|
|
2299
2319
|
if (typeof n != "number" || !Number.isFinite(n))
|
|
2300
2320
|
throw new TypeError(
|
|
2301
2321
|
`increment(n): n deve ser um n\xFAmero finito (recebido: ${String(n)}).`
|
|
@@ -2311,13 +2331,13 @@ function Xt(n) {
|
|
|
2311
2331
|
function Yt(n) {
|
|
2312
2332
|
return H(n) && n.__neetru__ === "increment" && typeof n.operand == "number" && Number.isFinite(n.operand);
|
|
2313
2333
|
}
|
|
2314
|
-
function
|
|
2334
|
+
function Hr(n, e) {
|
|
2315
2335
|
return Xt(n) ? Date.now() : Yt(n) ? (typeof e == "number" && Number.isFinite(e) ? e : 0) + n.operand : n;
|
|
2316
2336
|
}
|
|
2317
2337
|
function G(n, e) {
|
|
2318
2338
|
let t = false, r = {};
|
|
2319
2339
|
for (let [o, i] of Object.entries(n))
|
|
2320
|
-
H(i) ? (t = true, r[o] =
|
|
2340
|
+
H(i) ? (t = true, r[o] = Hr(i, e?.[o])) : r[o] = i;
|
|
2321
2341
|
return t ? r : n;
|
|
2322
2342
|
}
|
|
2323
2343
|
function He(n) {
|
|
@@ -2327,11 +2347,11 @@ function He(n) {
|
|
|
2327
2347
|
}
|
|
2328
2348
|
|
|
2329
2349
|
// src/db/offline/sync-engine.ts
|
|
2330
|
-
var
|
|
2331
|
-
function
|
|
2350
|
+
var Gr = 8, Qr = 1e3, Jr = 300 * 1e3, Ge = "truncatedCollections";
|
|
2351
|
+
function Xr(n) {
|
|
2332
2352
|
return n !== null && He(n);
|
|
2333
2353
|
}
|
|
2334
|
-
function
|
|
2354
|
+
function Yr(n) {
|
|
2335
2355
|
if (n === null) return null;
|
|
2336
2356
|
if (!He(n)) return n;
|
|
2337
2357
|
let e = {};
|
|
@@ -2483,16 +2503,16 @@ var Ie = class {
|
|
|
2483
2503
|
try {
|
|
2484
2504
|
t = await this._transport.pushMutations(e);
|
|
2485
2505
|
} catch (s) {
|
|
2486
|
-
let a = s instanceof Error ? s.message : String(s),
|
|
2487
|
-
await this._retryOrDeadLetter(
|
|
2488
|
-
for (let
|
|
2489
|
-
await this._queue.requeueInflight(
|
|
2490
|
-
return
|
|
2506
|
+
let a = s instanceof Error ? s.message : String(s), d = [], [p, ...l] = e;
|
|
2507
|
+
await this._retryOrDeadLetter(p, a, d);
|
|
2508
|
+
for (let u of l)
|
|
2509
|
+
await this._queue.requeueInflight(u.mutationId);
|
|
2510
|
+
return d.length > 0 && this._bus.emit(d), this._refreshPendingWrites().catch(() => {
|
|
2491
2511
|
}), false;
|
|
2492
2512
|
}
|
|
2493
2513
|
let r = [];
|
|
2494
2514
|
for (let s of t.results) {
|
|
2495
|
-
let a = e.find((
|
|
2515
|
+
let a = e.find((d) => d.mutationId === s.mutationId);
|
|
2496
2516
|
a && (s.outcome === "confirmed" ? (await this._queue.markApplied(a.mutationId), await this._applySyncedDoc(
|
|
2497
2517
|
a.collection,
|
|
2498
2518
|
a.docId,
|
|
@@ -2510,8 +2530,8 @@ var Ie = class {
|
|
|
2510
2530
|
"partial_push: muta\xE7\xE3o n\xE3o processada no lote",
|
|
2511
2531
|
r
|
|
2512
2532
|
);
|
|
2513
|
-
for (let
|
|
2514
|
-
await this._queue.requeueInflight(
|
|
2533
|
+
for (let d of a)
|
|
2534
|
+
await this._queue.requeueInflight(d.mutationId);
|
|
2515
2535
|
}
|
|
2516
2536
|
return r.length > 0 && this._bus.emit(r), i.length === 0;
|
|
2517
2537
|
}
|
|
@@ -2533,13 +2553,13 @@ var Ie = class {
|
|
|
2533
2553
|
}), s.push({ type: "removed", collection: e, doc: { id: t, data: a.data } }));
|
|
2534
2554
|
return;
|
|
2535
2555
|
}
|
|
2536
|
-
let
|
|
2556
|
+
let p = Yr(i.payload) ?? a?.data ?? {}, l = {
|
|
2537
2557
|
collection: e,
|
|
2538
2558
|
id: t,
|
|
2539
|
-
data: i.op === "update" ? { ...a?.data ?? {}, ...
|
|
2559
|
+
data: i.op === "update" ? { ...a?.data ?? {}, ...p } : (
|
|
2540
2560
|
// add/set: se o payload tinha sentinels, o cache otimista (existing.data)
|
|
2541
2561
|
// é a fonte de verdade até o pull; senão usa o payload literal.
|
|
2542
|
-
|
|
2562
|
+
Xr(i.payload) ? { ...a?.data ?? {}, ...p } : p
|
|
2543
2563
|
),
|
|
2544
2564
|
meta: {
|
|
2545
2565
|
serverVersion: r,
|
|
@@ -2551,10 +2571,10 @@ var Ie = class {
|
|
|
2551
2571
|
ownerId: a?.meta.ownerId ?? null
|
|
2552
2572
|
}
|
|
2553
2573
|
};
|
|
2554
|
-
await this._store.putDoc(
|
|
2574
|
+
await this._store.putDoc(l), s.push({
|
|
2555
2575
|
type: a ? "modified" : "added",
|
|
2556
2576
|
collection: e,
|
|
2557
|
-
doc: { id: t, data:
|
|
2577
|
+
doc: { id: t, data: l.data }
|
|
2558
2578
|
});
|
|
2559
2579
|
}
|
|
2560
2580
|
/** Processa resultado 'superseded' (LWW: servidor venceu). */
|
|
@@ -2586,7 +2606,7 @@ var Ie = class {
|
|
|
2586
2606
|
deleted: false,
|
|
2587
2607
|
ownerId: null
|
|
2588
2608
|
}
|
|
2589
|
-
},
|
|
2609
|
+
}, d = this._resolver.resolve(
|
|
2590
2610
|
s,
|
|
2591
2611
|
i,
|
|
2592
2612
|
e
|
|
@@ -2600,8 +2620,8 @@ var Ie = class {
|
|
|
2600
2620
|
detectedAt: Date.now(),
|
|
2601
2621
|
delivered: false
|
|
2602
2622
|
};
|
|
2603
|
-
await this._store.appendConflict(
|
|
2604
|
-
let
|
|
2623
|
+
await this._store.appendConflict(d);
|
|
2624
|
+
let p = {
|
|
2605
2625
|
collection: e.collection,
|
|
2606
2626
|
id: e.docId,
|
|
2607
2627
|
data: t.serverData,
|
|
@@ -2615,7 +2635,7 @@ var Ie = class {
|
|
|
2615
2635
|
ownerId: o?.meta.ownerId ?? null
|
|
2616
2636
|
}
|
|
2617
2637
|
};
|
|
2618
|
-
await this._store.putDoc(
|
|
2638
|
+
await this._store.putDoc(p), r.push({
|
|
2619
2639
|
type: o ? "modified" : "added",
|
|
2620
2640
|
collection: e.collection,
|
|
2621
2641
|
doc: { id: e.docId, data: t.serverData }
|
|
@@ -2643,7 +2663,7 @@ var Ie = class {
|
|
|
2643
2663
|
...i.meta,
|
|
2644
2664
|
serverVersion: t.serverVersion ?? i.meta.serverVersion
|
|
2645
2665
|
}
|
|
2646
|
-
} : null,
|
|
2666
|
+
} : null, d = this._resolver.resolveRejected(
|
|
2647
2667
|
i,
|
|
2648
2668
|
s,
|
|
2649
2669
|
e,
|
|
@@ -2658,7 +2678,7 @@ var Ie = class {
|
|
|
2658
2678
|
detectedAt: Date.now(),
|
|
2659
2679
|
delivered: false
|
|
2660
2680
|
};
|
|
2661
|
-
await this._store.appendConflict(
|
|
2681
|
+
await this._store.appendConflict(d), t.serverData ? (await this._store.putDoc({
|
|
2662
2682
|
...i,
|
|
2663
2683
|
data: t.serverData,
|
|
2664
2684
|
meta: {
|
|
@@ -2692,7 +2712,7 @@ var Ie = class {
|
|
|
2692
2712
|
* pending do doc no cache + registra `lastError`/dispara `onError`.
|
|
2693
2713
|
*/
|
|
2694
2714
|
async _retryOrDeadLetter(e, t, r) {
|
|
2695
|
-
if (e.attempts + 1 <
|
|
2715
|
+
if (e.attempts + 1 < Gr) {
|
|
2696
2716
|
await this._queue.markRetry(e.mutationId, t);
|
|
2697
2717
|
return;
|
|
2698
2718
|
}
|
|
@@ -2733,7 +2753,7 @@ var Ie = class {
|
|
|
2733
2753
|
}
|
|
2734
2754
|
/** Backoff exponencial keyed em `attempts`, com teto `BACKOFF_CAP_MS`. */
|
|
2735
2755
|
_backoffDelayMs(e) {
|
|
2736
|
-
return Math.min(
|
|
2756
|
+
return Math.min(Qr * 2 ** (e - 1), Jr);
|
|
2737
2757
|
}
|
|
2738
2758
|
// ─── FASE 2 — pull (reconciliar o cache) ─────────────────────────────────────
|
|
2739
2759
|
/**
|
|
@@ -2772,15 +2792,15 @@ var Ie = class {
|
|
|
2772
2792
|
);
|
|
2773
2793
|
for (let a of e) {
|
|
2774
2794
|
if (i.has(a)) continue;
|
|
2775
|
-
let
|
|
2776
|
-
for (let { id:
|
|
2777
|
-
let
|
|
2778
|
-
if (!s.has(
|
|
2779
|
-
let
|
|
2780
|
-
|
|
2781
|
-
...
|
|
2795
|
+
let d = await this._store.listDocs(a, {});
|
|
2796
|
+
for (let { id: p } of d.docs) {
|
|
2797
|
+
let l = `${a}::${p}`;
|
|
2798
|
+
if (!s.has(l)) {
|
|
2799
|
+
let u = await this._store.getDoc(a, p);
|
|
2800
|
+
u && !u.meta.deleted && (await this._store.putDoc({
|
|
2801
|
+
...u,
|
|
2782
2802
|
meta: {
|
|
2783
|
-
...
|
|
2803
|
+
...u.meta,
|
|
2784
2804
|
deleted: true,
|
|
2785
2805
|
updatedAtLocal: Date.now(),
|
|
2786
2806
|
state: "synced"
|
|
@@ -2788,7 +2808,7 @@ var Ie = class {
|
|
|
2788
2808
|
}), r.push({
|
|
2789
2809
|
type: "removed",
|
|
2790
2810
|
collection: a,
|
|
2791
|
-
doc: { id:
|
|
2811
|
+
doc: { id: p, data: u.data }
|
|
2792
2812
|
}));
|
|
2793
2813
|
}
|
|
2794
2814
|
}
|
|
@@ -2823,7 +2843,7 @@ var Ie = class {
|
|
|
2823
2843
|
continue;
|
|
2824
2844
|
}
|
|
2825
2845
|
if (i && i.meta.state === "pending") {
|
|
2826
|
-
let
|
|
2846
|
+
let d = {
|
|
2827
2847
|
collection: o.collection,
|
|
2828
2848
|
id: o.id,
|
|
2829
2849
|
data: o.data,
|
|
@@ -2836,12 +2856,12 @@ var Ie = class {
|
|
|
2836
2856
|
deleted: false,
|
|
2837
2857
|
ownerId: i.meta.ownerId
|
|
2838
2858
|
}
|
|
2839
|
-
},
|
|
2840
|
-
(
|
|
2859
|
+
}, l = (await this._queue.listAll()).find(
|
|
2860
|
+
(u) => u.collection === o.collection && u.docId === o.id
|
|
2841
2861
|
);
|
|
2842
|
-
if (
|
|
2843
|
-
let
|
|
2844
|
-
|
|
2862
|
+
if (l) {
|
|
2863
|
+
let u = this._resolver.resolve(i, d, l);
|
|
2864
|
+
u.conflictRecord && await this._store.appendConflict(u.conflictRecord), await this._store.putDoc({
|
|
2845
2865
|
...i,
|
|
2846
2866
|
meta: { ...i.meta, state: "conflict" }
|
|
2847
2867
|
});
|
|
@@ -3075,9 +3095,9 @@ var Ce = class n {
|
|
|
3075
3095
|
))
|
|
3076
3096
|
return { outcome: "local_wins", resolvedData: { ...i, ...r.payload ?? {} }, conflictRecord: null };
|
|
3077
3097
|
let a = {};
|
|
3078
|
-
for (let
|
|
3079
|
-
a[
|
|
3080
|
-
let
|
|
3098
|
+
for (let p of o)
|
|
3099
|
+
a[p] = e.data[p];
|
|
3100
|
+
let d = {
|
|
3081
3101
|
collection: r.collection,
|
|
3082
3102
|
docId: r.docId,
|
|
3083
3103
|
mutationId: r.mutationId,
|
|
@@ -3090,7 +3110,7 @@ var Ce = class n {
|
|
|
3090
3110
|
return {
|
|
3091
3111
|
outcome: "server_wins",
|
|
3092
3112
|
resolvedData: i,
|
|
3093
|
-
conflictRecord:
|
|
3113
|
+
conflictRecord: d
|
|
3094
3114
|
};
|
|
3095
3115
|
}
|
|
3096
3116
|
/**
|
|
@@ -3147,7 +3167,7 @@ var Ce = class n {
|
|
|
3147
3167
|
};
|
|
3148
3168
|
|
|
3149
3169
|
// src/db/offline/change-bus.ts
|
|
3150
|
-
function
|
|
3170
|
+
function Zr(n) {
|
|
3151
3171
|
return `${n.collection}::${n.doc.id}`;
|
|
3152
3172
|
}
|
|
3153
3173
|
var Ae = class {
|
|
@@ -3195,15 +3215,15 @@ var Ae = class {
|
|
|
3195
3215
|
if (e.length === 0) return;
|
|
3196
3216
|
let t = /* @__PURE__ */ new Map();
|
|
3197
3217
|
for (let i of e)
|
|
3198
|
-
t.set(
|
|
3218
|
+
t.set(Zr(i), i);
|
|
3199
3219
|
let r = Array.from(t.values()), o = /* @__PURE__ */ new Map();
|
|
3200
3220
|
for (let i of r)
|
|
3201
3221
|
o.has(i.collection) || o.set(i.collection, []), o.get(i.collection).push(i);
|
|
3202
3222
|
for (let [i, s] of o) {
|
|
3203
3223
|
let a = this._collectionListeners.get(i);
|
|
3204
3224
|
if (a)
|
|
3205
|
-
for (let
|
|
3206
|
-
this._safeCall(
|
|
3225
|
+
for (let d of a)
|
|
3226
|
+
this._safeCall(d, s);
|
|
3207
3227
|
}
|
|
3208
3228
|
for (let i of this._globalListeners)
|
|
3209
3229
|
this._safeCall(i, r);
|
|
@@ -3514,11 +3534,11 @@ var ie = class {
|
|
|
3514
3534
|
|
|
3515
3535
|
// src/db/collection-ref.ts
|
|
3516
3536
|
z();
|
|
3517
|
-
function
|
|
3537
|
+
function eo() {
|
|
3518
3538
|
return typeof indexedDB > "u" || typeof window > "u";
|
|
3519
3539
|
}
|
|
3520
3540
|
var en = /^[a-z0-9][a-z0-9_-]{0,62}$/;
|
|
3521
|
-
function
|
|
3541
|
+
function to(n) {
|
|
3522
3542
|
if (!en.test(n))
|
|
3523
3543
|
throw new exports.NeetruDbError(
|
|
3524
3544
|
"db_invalid_query",
|
|
@@ -3532,7 +3552,7 @@ function Y(n, e = "id") {
|
|
|
3532
3552
|
`${e} \xE9 obrigat\xF3rio e deve ser uma string n\xE3o-vazia.`
|
|
3533
3553
|
);
|
|
3534
3554
|
}
|
|
3535
|
-
function
|
|
3555
|
+
function no(n) {
|
|
3536
3556
|
let e = {};
|
|
3537
3557
|
if (n?.where && (e.where = n.where.map((t) => ({
|
|
3538
3558
|
field: t.field,
|
|
@@ -3549,7 +3569,7 @@ function to(n) {
|
|
|
3549
3569
|
}
|
|
3550
3570
|
return e;
|
|
3551
3571
|
}
|
|
3552
|
-
function
|
|
3572
|
+
function ro(n, e) {
|
|
3553
3573
|
if (n.length < e) return null;
|
|
3554
3574
|
let t = n[n.length - 1];
|
|
3555
3575
|
return t ? btoa(JSON.stringify({ type: "startAfter", docId: t.id })) : null;
|
|
@@ -3596,15 +3616,15 @@ var Qe = class {
|
|
|
3596
3616
|
}
|
|
3597
3617
|
/** Constrói um DbListResult para a coleção com query opcional. */
|
|
3598
3618
|
async _buildListResult(e) {
|
|
3599
|
-
let t =
|
|
3619
|
+
let t = no(e), r = t.limit ?? 20, o = await this._store.listDocs(this._collection, t), i = await this._hasPendingWrites(), s = this._isOnline(), a = this._engine.getSyncState(), d = ro(o.docs, r), p = await this._store.getMeta(Ge), l = Array.isArray(p) && p.includes(this._collection), u = !s || a.status === "offline" || l, f = i || a.lastSyncedAt === null || u;
|
|
3600
3620
|
return {
|
|
3601
3621
|
docs: o.docs.map((m) => ({ id: m.id, data: m.data })),
|
|
3602
|
-
nextCursor:
|
|
3622
|
+
nextCursor: d,
|
|
3603
3623
|
fromCache: f,
|
|
3604
|
-
stale:
|
|
3624
|
+
stale: u,
|
|
3605
3625
|
hasPendingWrites: i,
|
|
3606
3626
|
changes: [],
|
|
3607
|
-
truncated:
|
|
3627
|
+
truncated: l
|
|
3608
3628
|
};
|
|
3609
3629
|
}
|
|
3610
3630
|
// ── CRUD ────────────────────────────────────────────────────────────────────
|
|
@@ -3672,7 +3692,7 @@ var Qe = class {
|
|
|
3672
3692
|
}
|
|
3673
3693
|
async update(e, t, r) {
|
|
3674
3694
|
Y(e);
|
|
3675
|
-
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a = G(i, s),
|
|
3695
|
+
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a = G(i, s), d = o?.meta.deleted ? { ...a } : { ...o?.data ?? {}, ...a }, p = await this._queue.enqueue({
|
|
3676
3696
|
collection: this._collection,
|
|
3677
3697
|
docId: e,
|
|
3678
3698
|
op: "update",
|
|
@@ -3685,7 +3705,7 @@ var Qe = class {
|
|
|
3685
3705
|
return await this._store.putDoc({
|
|
3686
3706
|
collection: this._collection,
|
|
3687
3707
|
id: e,
|
|
3688
|
-
data:
|
|
3708
|
+
data: d,
|
|
3689
3709
|
meta: {
|
|
3690
3710
|
serverVersion: o?.meta.serverVersion ?? null,
|
|
3691
3711
|
updatedAtServer: o?.meta.updatedAtServer ?? null,
|
|
@@ -3693,7 +3713,7 @@ var Qe = class {
|
|
|
3693
3713
|
state: "pending",
|
|
3694
3714
|
pendingMutationIds: [
|
|
3695
3715
|
...o?.meta.pendingMutationIds ?? [],
|
|
3696
|
-
|
|
3716
|
+
p.mutationId
|
|
3697
3717
|
],
|
|
3698
3718
|
deleted: false,
|
|
3699
3719
|
ownerId: o?.meta.ownerId ?? null
|
|
@@ -3701,7 +3721,7 @@ var Qe = class {
|
|
|
3701
3721
|
}), this._bus.emit([{
|
|
3702
3722
|
type: "modified",
|
|
3703
3723
|
collection: this._collection,
|
|
3704
|
-
doc: { id: e, data:
|
|
3724
|
+
doc: { id: e, data: d }
|
|
3705
3725
|
}]), { ok: true };
|
|
3706
3726
|
}
|
|
3707
3727
|
async remove(e) {
|
|
@@ -3725,61 +3745,61 @@ var Qe = class {
|
|
|
3725
3745
|
for (let o of e) {
|
|
3726
3746
|
let i = this._collection;
|
|
3727
3747
|
if (o.kind === "add") {
|
|
3728
|
-
let s = o.data, a = G(s, void 0),
|
|
3748
|
+
let s = o.data, a = G(s, void 0), d = await this._queue.enqueue({
|
|
3729
3749
|
collection: i,
|
|
3730
3750
|
op: "add",
|
|
3731
3751
|
payload: s,
|
|
3732
3752
|
baseVersion: null,
|
|
3733
3753
|
batchId: t
|
|
3734
|
-
}),
|
|
3754
|
+
}), p = d.docId;
|
|
3735
3755
|
await this._store.putDoc({
|
|
3736
3756
|
collection: i,
|
|
3737
|
-
id:
|
|
3757
|
+
id: p,
|
|
3738
3758
|
data: a,
|
|
3739
3759
|
meta: {
|
|
3740
3760
|
serverVersion: null,
|
|
3741
3761
|
updatedAtServer: null,
|
|
3742
3762
|
updatedAtLocal: Date.now(),
|
|
3743
3763
|
state: "pending",
|
|
3744
|
-
pendingMutationIds: [
|
|
3764
|
+
pendingMutationIds: [d.mutationId],
|
|
3745
3765
|
deleted: false,
|
|
3746
3766
|
ownerId: null
|
|
3747
3767
|
}
|
|
3748
|
-
}), r.push({ type: "added", collection: i, doc: { id:
|
|
3768
|
+
}), r.push({ type: "added", collection: i, doc: { id: p, data: a } });
|
|
3749
3769
|
} else if (o.kind === "set") {
|
|
3750
|
-
let s = o.id, a = o.data,
|
|
3770
|
+
let s = o.id, a = o.data, d = await this._store.getDoc(i, s), p = G(a, void 0), l = await this._queue.enqueue({
|
|
3751
3771
|
collection: i,
|
|
3752
3772
|
docId: s,
|
|
3753
3773
|
op: "set",
|
|
3754
3774
|
payload: a,
|
|
3755
|
-
baseVersion:
|
|
3775
|
+
baseVersion: d?.meta.serverVersion ?? null,
|
|
3756
3776
|
batchId: t
|
|
3757
3777
|
});
|
|
3758
3778
|
await this._store.putDoc({
|
|
3759
3779
|
collection: i,
|
|
3760
3780
|
id: s,
|
|
3761
|
-
data:
|
|
3781
|
+
data: p,
|
|
3762
3782
|
meta: {
|
|
3763
|
-
serverVersion:
|
|
3764
|
-
updatedAtServer:
|
|
3783
|
+
serverVersion: d?.meta.serverVersion ?? null,
|
|
3784
|
+
updatedAtServer: d?.meta.updatedAtServer ?? null,
|
|
3765
3785
|
updatedAtLocal: Date.now(),
|
|
3766
3786
|
state: "pending",
|
|
3767
|
-
pendingMutationIds: [
|
|
3787
|
+
pendingMutationIds: [l.mutationId],
|
|
3768
3788
|
deleted: false,
|
|
3769
3789
|
ownerId: null
|
|
3770
3790
|
}
|
|
3771
3791
|
}), r.push({
|
|
3772
|
-
type:
|
|
3792
|
+
type: d && !d.meta.deleted ? "modified" : "added",
|
|
3773
3793
|
collection: i,
|
|
3774
|
-
doc: { id: s, data:
|
|
3794
|
+
doc: { id: s, data: p }
|
|
3775
3795
|
});
|
|
3776
3796
|
} else if (o.kind === "update") {
|
|
3777
|
-
let s = o.id, a = o.data,
|
|
3797
|
+
let s = o.id, a = o.data, d = await this._store.getDoc(i, s), p = d?.meta.deleted ? void 0 : d?.data, l = G(a, p), u = { ...d?.data ?? {}, ...l }, f = await this._queue.enqueue({
|
|
3778
3798
|
collection: i,
|
|
3779
3799
|
docId: s,
|
|
3780
3800
|
op: "update",
|
|
3781
3801
|
payload: a,
|
|
3782
|
-
baseVersion:
|
|
3802
|
+
baseVersion: d?.meta.serverVersion ?? null,
|
|
3783
3803
|
batchId: t,
|
|
3784
3804
|
// bug_0273e4dd: propaga merge-upsert (writeBatch.set(merge) cria-se-ausente).
|
|
3785
3805
|
upsert: o.upsert
|
|
@@ -3787,20 +3807,20 @@ var Qe = class {
|
|
|
3787
3807
|
await this._store.putDoc({
|
|
3788
3808
|
collection: i,
|
|
3789
3809
|
id: s,
|
|
3790
|
-
data:
|
|
3810
|
+
data: u,
|
|
3791
3811
|
meta: {
|
|
3792
|
-
serverVersion:
|
|
3793
|
-
updatedAtServer:
|
|
3812
|
+
serverVersion: d?.meta.serverVersion ?? null,
|
|
3813
|
+
updatedAtServer: d?.meta.updatedAtServer ?? null,
|
|
3794
3814
|
updatedAtLocal: Date.now(),
|
|
3795
3815
|
state: "pending",
|
|
3796
3816
|
pendingMutationIds: [
|
|
3797
|
-
...
|
|
3817
|
+
...d?.meta.pendingMutationIds ?? [],
|
|
3798
3818
|
f.mutationId
|
|
3799
3819
|
],
|
|
3800
3820
|
deleted: false,
|
|
3801
|
-
ownerId:
|
|
3821
|
+
ownerId: d?.meta.ownerId ?? null
|
|
3802
3822
|
}
|
|
3803
|
-
}), r.push({ type: "modified", collection: i, doc: { id: s, data:
|
|
3823
|
+
}), r.push({ type: "modified", collection: i, doc: { id: s, data: u } });
|
|
3804
3824
|
} else if (o.kind === "remove") {
|
|
3805
3825
|
let s = o.id, a = await this._store.getDoc(i, s);
|
|
3806
3826
|
await this._queue.enqueue({
|
|
@@ -3829,11 +3849,11 @@ var Qe = class {
|
|
|
3829
3849
|
});
|
|
3830
3850
|
let r = this._bus.subscribe(this._collection, async (s) => {
|
|
3831
3851
|
try {
|
|
3832
|
-
let a = await this._buildListResult(e),
|
|
3833
|
-
type:
|
|
3834
|
-
doc: { id:
|
|
3852
|
+
let a = await this._buildListResult(e), d = s.map((p) => ({
|
|
3853
|
+
type: p.type,
|
|
3854
|
+
doc: { id: p.doc.id, data: p.doc.data }
|
|
3835
3855
|
}));
|
|
3836
|
-
t({ ...a, changes:
|
|
3856
|
+
t({ ...a, changes: d });
|
|
3837
3857
|
} catch {
|
|
3838
3858
|
}
|
|
3839
3859
|
}), o = this._engine.onSyncStateChange(async () => {
|
|
@@ -3847,15 +3867,15 @@ var Qe = class {
|
|
|
3847
3867
|
this._collection,
|
|
3848
3868
|
async (s, a) => {
|
|
3849
3869
|
if (!a)
|
|
3850
|
-
for (let
|
|
3851
|
-
|
|
3870
|
+
for (let d of s)
|
|
3871
|
+
d.type === "removed" || d.data === null ? (await this._store.deleteDoc(this._collection, d.docId), this._bus.emit([{
|
|
3852
3872
|
type: "removed",
|
|
3853
3873
|
collection: this._collection,
|
|
3854
|
-
doc: { id:
|
|
3874
|
+
doc: { id: d.docId, data: {} }
|
|
3855
3875
|
}])) : (await this._store.putDoc({
|
|
3856
3876
|
collection: this._collection,
|
|
3857
|
-
id:
|
|
3858
|
-
data:
|
|
3877
|
+
id: d.docId,
|
|
3878
|
+
data: d.data,
|
|
3859
3879
|
meta: {
|
|
3860
3880
|
serverVersion: `rt_${Date.now()}`,
|
|
3861
3881
|
updatedAtServer: Date.now(),
|
|
@@ -3866,9 +3886,9 @@ var Qe = class {
|
|
|
3866
3886
|
ownerId: null
|
|
3867
3887
|
}
|
|
3868
3888
|
}), this._bus.emit([{
|
|
3869
|
-
type:
|
|
3889
|
+
type: d.type,
|
|
3870
3890
|
collection: this._collection,
|
|
3871
|
-
doc: { id:
|
|
3891
|
+
doc: { id: d.docId, data: d.data }
|
|
3872
3892
|
}]));
|
|
3873
3893
|
}
|
|
3874
3894
|
)), () => {
|
|
@@ -3903,14 +3923,14 @@ var Qe = class {
|
|
|
3903
3923
|
onSnapshot(r) {
|
|
3904
3924
|
t._buildGetResult(e).then((s) => r(s)).catch(() => r(null));
|
|
3905
3925
|
let o = t._bus.subscribe(t._collection, async (s) => {
|
|
3906
|
-
let a = s.find((
|
|
3926
|
+
let a = s.find((d) => d.doc.id === e);
|
|
3907
3927
|
if (a)
|
|
3908
3928
|
try {
|
|
3909
3929
|
if (a.type === "removed")
|
|
3910
3930
|
r(null);
|
|
3911
3931
|
else {
|
|
3912
|
-
let
|
|
3913
|
-
r(
|
|
3932
|
+
let d = await t._buildGetResult(e);
|
|
3933
|
+
r(d);
|
|
3914
3934
|
}
|
|
3915
3935
|
} catch {
|
|
3916
3936
|
r(null);
|
|
@@ -3939,7 +3959,7 @@ var Qe = class {
|
|
|
3939
3959
|
this._store = e.store, this._queue = e.queue, this._bus = e.bus, this._engine = e.engine, this._connectivity = e.connectivity;
|
|
3940
3960
|
}
|
|
3941
3961
|
collection(e) {
|
|
3942
|
-
return
|
|
3962
|
+
return to(e), new Qe(
|
|
3943
3963
|
e,
|
|
3944
3964
|
this._store,
|
|
3945
3965
|
this._queue,
|
|
@@ -3969,7 +3989,7 @@ var Qe = class {
|
|
|
3969
3989
|
}
|
|
3970
3990
|
};
|
|
3971
3991
|
async function tn(n) {
|
|
3972
|
-
let e =
|
|
3992
|
+
let e = eo() ? new Te() : new Se(n.dbName);
|
|
3973
3993
|
await e.open();
|
|
3974
3994
|
let t = new Re(e), r = new Ce(), o = new Ae(), i = new Ee({
|
|
3975
3995
|
// Se startOnline=false, injeta um navigator falso
|
|
@@ -3978,7 +3998,7 @@ async function tn(n) {
|
|
|
3978
3998
|
// Fake tab coordinator para single-tab mode (sempre líder)
|
|
3979
3999
|
{
|
|
3980
4000
|
isLeader: () => true,
|
|
3981
|
-
onRoleChange: (
|
|
4001
|
+
onRoleChange: (d) => () => {
|
|
3982
4002
|
}
|
|
3983
4003
|
}
|
|
3984
4004
|
) : new ie({
|
|
@@ -4004,9 +4024,9 @@ async function tn(n) {
|
|
|
4004
4024
|
}
|
|
4005
4025
|
|
|
4006
4026
|
// src/db/realtime/realtime-client.ts
|
|
4007
|
-
var
|
|
4027
|
+
var oo = 0;
|
|
4008
4028
|
function nn() {
|
|
4009
|
-
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++
|
|
4029
|
+
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++oo}-${Math.random().toString(36).slice(2)}`;
|
|
4010
4030
|
}
|
|
4011
4031
|
var Pe = class {
|
|
4012
4032
|
// ── configuração ──────────────────────────────────────────────────────────
|
|
@@ -4046,7 +4066,7 @@ var Pe = class {
|
|
|
4046
4066
|
_pendingFrames = [];
|
|
4047
4067
|
// ── construtor ────────────────────────────────────────────────────────────
|
|
4048
4068
|
constructor(e) {
|
|
4049
|
-
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ??
|
|
4069
|
+
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ?? io, 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();
|
|
4050
4070
|
}
|
|
4051
4071
|
// ── API pública ───────────────────────────────────────────────────────────
|
|
4052
4072
|
/**
|
|
@@ -4325,7 +4345,7 @@ var Pe = class {
|
|
|
4325
4345
|
_detachHandlers(e) {
|
|
4326
4346
|
e.onopen = null, e.onmessage = null, e.onclose = null, e.onerror = null;
|
|
4327
4347
|
}
|
|
4328
|
-
},
|
|
4348
|
+
}, io = (n) => {
|
|
4329
4349
|
if (typeof WebSocket > "u")
|
|
4330
4350
|
throw new Error(
|
|
4331
4351
|
"[NeetruRealtimeClient] WebSocket n\xE3o dispon\xEDvel neste ambiente. Injete uma implementa\xE7\xE3o via `webSocketFactory` nas op\xE7\xF5es."
|
|
@@ -4334,11 +4354,11 @@ var Pe = class {
|
|
|
4334
4354
|
};
|
|
4335
4355
|
|
|
4336
4356
|
// src/db/client-db.ts
|
|
4337
|
-
var
|
|
4338
|
-
function
|
|
4339
|
-
return e._transport ? e._transport : n === "firestore" && e.firestoreTransport ? e.firestoreTransport : n === "nosql-vm" && e.realtimeGatewayUrl ?
|
|
4357
|
+
var bo = (n) => `/api/sdk/v1/datastore/${encodeURIComponent(n)}`, Ne = (n, e) => `/api/sdk/v1/datastore/${encodeURIComponent(n)}/${encodeURIComponent(e)}`, hn = 200;
|
|
4358
|
+
function vo(n, e, t) {
|
|
4359
|
+
return e._transport ? e._transport : n === "firestore" && e.firestoreTransport ? e.firestoreTransport : n === "nosql-vm" && e.realtimeGatewayUrl ? wo(e.realtimeGatewayUrl, t, e) : yn(t);
|
|
4340
4360
|
}
|
|
4341
|
-
function
|
|
4361
|
+
function yn(n) {
|
|
4342
4362
|
return {
|
|
4343
4363
|
async pushMutations(e) {
|
|
4344
4364
|
if (!n)
|
|
@@ -4378,17 +4398,17 @@ function gn(n) {
|
|
|
4378
4398
|
retries: 2,
|
|
4379
4399
|
headers: i
|
|
4380
4400
|
});
|
|
4381
|
-
let
|
|
4401
|
+
let d = a;
|
|
4382
4402
|
r.push({
|
|
4383
4403
|
mutationId: s.mutationId,
|
|
4384
4404
|
outcome: "confirmed",
|
|
4385
|
-
serverVersion:
|
|
4405
|
+
serverVersion: d?.serverVersion ?? d?.id ?? `rest_${Date.now()}`,
|
|
4386
4406
|
serverTimestamp: Date.now()
|
|
4387
4407
|
});
|
|
4388
4408
|
} catch (a) {
|
|
4389
|
-
let
|
|
4390
|
-
if (
|
|
4391
|
-
let f =
|
|
4409
|
+
let d = a instanceof Error ? a.message : String(a), p = a?.code, l = a?.status;
|
|
4410
|
+
if (p === "forbidden" || p === "unauthorized" || p === "not_found" || p === "conflict" || p === "validation_failed" || typeof l == "number" && l >= 400 && l < 500 && l !== 408 && l !== 429) {
|
|
4411
|
+
let f = p === "forbidden" || p === "unauthorized" ? "rejected_permission" : "rejected_validation";
|
|
4392
4412
|
r.push({
|
|
4393
4413
|
mutationId: s.mutationId,
|
|
4394
4414
|
outcome: "rejected",
|
|
@@ -4399,7 +4419,7 @@ function gn(n) {
|
|
|
4399
4419
|
continue;
|
|
4400
4420
|
}
|
|
4401
4421
|
if (r.length === 0)
|
|
4402
|
-
throw new exports.NeetruDbError("db_unavailable", `pushMutations falhou: ${
|
|
4422
|
+
throw new exports.NeetruDbError("db_unavailable", `pushMutations falhou: ${d}`);
|
|
4403
4423
|
break;
|
|
4404
4424
|
}
|
|
4405
4425
|
return { results: r };
|
|
@@ -4411,12 +4431,12 @@ function gn(n) {
|
|
|
4411
4431
|
if (!n)
|
|
4412
4432
|
return { docs: [], newWatermark: Date.now() };
|
|
4413
4433
|
let { httpRequest: t } = await Promise.resolve().then(() => (q(), ne)), r = n.tenantId ?? n.productId, o = r ? { "x-neetru-tenant": r } : void 0, i = Date.now(), s = [], a = [];
|
|
4414
|
-
for (let
|
|
4415
|
-
let
|
|
4434
|
+
for (let d of e) {
|
|
4435
|
+
let p;
|
|
4416
4436
|
try {
|
|
4417
|
-
|
|
4437
|
+
p = await t(n, {
|
|
4418
4438
|
method: "GET",
|
|
4419
|
-
path:
|
|
4439
|
+
path: bo(d),
|
|
4420
4440
|
requireAuth: !0,
|
|
4421
4441
|
retries: 2,
|
|
4422
4442
|
headers: o,
|
|
@@ -4424,18 +4444,18 @@ function gn(n) {
|
|
|
4424
4444
|
// TRUNCADAS — reportadas em `truncatedCollections` p/ o SyncEngine
|
|
4425
4445
|
// (a) NÃO rodar deleção-por-ausência e (b) sinalizar
|
|
4426
4446
|
// `DbListResult.truncated`. Paginação por cursor é tarefa Core (gated).
|
|
4427
|
-
query: { limit:
|
|
4447
|
+
query: { limit: hn }
|
|
4428
4448
|
});
|
|
4429
4449
|
} catch (f) {
|
|
4430
4450
|
let m = f instanceof Error ? f.message : String(f);
|
|
4431
4451
|
throw new exports.NeetruDbError(
|
|
4432
4452
|
"db_unavailable",
|
|
4433
|
-
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${
|
|
4453
|
+
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${d}": ${m}`
|
|
4434
4454
|
);
|
|
4435
4455
|
}
|
|
4436
|
-
let
|
|
4437
|
-
|
|
4438
|
-
for (let f of
|
|
4456
|
+
let l = p, u = Array.isArray(l?.items) ? l.items : [];
|
|
4457
|
+
u.length >= hn && a.push(d);
|
|
4458
|
+
for (let f of u) {
|
|
4439
4459
|
if (!f || typeof f != "object") continue;
|
|
4440
4460
|
let m = f, h = typeof m.id == "string" ? m.id : null;
|
|
4441
4461
|
if (!h) continue;
|
|
@@ -4450,7 +4470,7 @@ function gn(n) {
|
|
|
4450
4470
|
}
|
|
4451
4471
|
let N = `rest_${h}_${w}`;
|
|
4452
4472
|
s.push({
|
|
4453
|
-
collection:
|
|
4473
|
+
collection: d,
|
|
4454
4474
|
id: h,
|
|
4455
4475
|
data: I,
|
|
4456
4476
|
serverVersion: N,
|
|
@@ -4463,10 +4483,10 @@ function gn(n) {
|
|
|
4463
4483
|
}
|
|
4464
4484
|
};
|
|
4465
4485
|
}
|
|
4466
|
-
function
|
|
4467
|
-
let r =
|
|
4486
|
+
function wo(n, e, t) {
|
|
4487
|
+
let r = yn(e), o = new Pe({
|
|
4468
4488
|
gatewayUrl: n,
|
|
4469
|
-
ticketProvider:
|
|
4489
|
+
ticketProvider: ko(e, t),
|
|
4470
4490
|
dbId: t.dbId,
|
|
4471
4491
|
webSocketFactory: t._wsFactory
|
|
4472
4492
|
});
|
|
@@ -4478,22 +4498,22 @@ function _o(n, e, t) {
|
|
|
4478
4498
|
// quando o engine é nosql-vm. Registra a subscription no NeetruRealtimeClient
|
|
4479
4499
|
// e traduz os frames inbound para o contrato de `onChange`.
|
|
4480
4500
|
subscribeCollection(i, s) {
|
|
4481
|
-
let a = o.subscribe(i, {}, (
|
|
4482
|
-
if (
|
|
4501
|
+
let a = o.subscribe(i, {}, (d) => {
|
|
4502
|
+
if (d.op === "resync") {
|
|
4483
4503
|
s([], true);
|
|
4484
4504
|
return;
|
|
4485
4505
|
}
|
|
4486
|
-
if (
|
|
4506
|
+
if (d.op === "stale") {
|
|
4487
4507
|
s([], true);
|
|
4488
4508
|
return;
|
|
4489
4509
|
}
|
|
4490
|
-
if (
|
|
4491
|
-
let
|
|
4492
|
-
type:
|
|
4493
|
-
docId:
|
|
4494
|
-
data:
|
|
4510
|
+
if (d.op === "delta" && d.changes) {
|
|
4511
|
+
let p = d.changes.map((l) => ({
|
|
4512
|
+
type: l.type === "insert" ? "added" : l.type === "delete" ? "removed" : "modified",
|
|
4513
|
+
docId: l.documentId,
|
|
4514
|
+
data: l.data ?? null
|
|
4495
4515
|
}));
|
|
4496
|
-
|
|
4516
|
+
p.length > 0 && s(p, false);
|
|
4497
4517
|
}
|
|
4498
4518
|
});
|
|
4499
4519
|
return () => {
|
|
@@ -4502,7 +4522,7 @@ function _o(n, e, t) {
|
|
|
4502
4522
|
}
|
|
4503
4523
|
};
|
|
4504
4524
|
}
|
|
4505
|
-
function
|
|
4525
|
+
function ko(n, e) {
|
|
4506
4526
|
return async () => {
|
|
4507
4527
|
let { httpRequest: t } = await Promise.resolve().then(() => (q(), ne));
|
|
4508
4528
|
return await t(n, {
|
|
@@ -4517,37 +4537,37 @@ function bo(n, e) {
|
|
|
4517
4537
|
});
|
|
4518
4538
|
};
|
|
4519
4539
|
}
|
|
4520
|
-
var
|
|
4521
|
-
function
|
|
4522
|
-
|
|
4540
|
+
var gn = false;
|
|
4541
|
+
function So(n, e) {
|
|
4542
|
+
gn || e.dbId && n.env !== "prod" && (typeof window > "u" || typeof console > "u" || typeof console.warn != "function" || (gn = true, console.warn(
|
|
4523
4543
|
'[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.'
|
|
4524
4544
|
)));
|
|
4525
4545
|
}
|
|
4526
4546
|
function nt(n, e = {}) {
|
|
4527
|
-
|
|
4547
|
+
So(n, e);
|
|
4528
4548
|
let t;
|
|
4529
4549
|
e.transport !== void 0 ? t = e.transport : e.engine !== void 0 ? (console.warn(
|
|
4530
4550
|
'[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).'
|
|
4531
4551
|
), t = e.engine) : t = "rest";
|
|
4532
|
-
let r =
|
|
4533
|
-
function
|
|
4552
|
+
let r = vo(t, e, n), o = e.dbId ?? "default", i = e.dbName ?? `neetru-db__${n.productId ?? "sdk"}__${o}__${n.env}`, s = null, a = null;
|
|
4553
|
+
function d() {
|
|
4534
4554
|
return s || (s = tn({
|
|
4535
4555
|
dbName: i,
|
|
4536
4556
|
transport: r,
|
|
4537
4557
|
singleTab: e.singleTab ?? n.env !== "prod"
|
|
4538
|
-
}).then((
|
|
4558
|
+
}).then((p) => (a = p, p))), s;
|
|
4539
4559
|
}
|
|
4540
4560
|
return {
|
|
4541
|
-
collection(
|
|
4542
|
-
return a ? a.collection(
|
|
4561
|
+
collection(p) {
|
|
4562
|
+
return a ? a.collection(p) : To(p, d);
|
|
4543
4563
|
},
|
|
4544
|
-
async sql(
|
|
4564
|
+
async sql(p, l) {
|
|
4545
4565
|
if (n.env === "dev")
|
|
4546
4566
|
throw new exports.NeetruDbError(
|
|
4547
4567
|
"db_unavailable",
|
|
4548
4568
|
"[SDK] db.sql() n\xE3o dispon\xEDvel em NEETRU_ENV=dev. Use `neetru dev` para subir o container Postgres local."
|
|
4549
4569
|
);
|
|
4550
|
-
return (await Promise.resolve().then(() => (
|
|
4570
|
+
return (await Promise.resolve().then(() => (mn(), fn))).createSqlClientFromConfig(n, p, l);
|
|
4551
4571
|
},
|
|
4552
4572
|
get syncState() {
|
|
4553
4573
|
return a?.syncState ?? {
|
|
@@ -4557,36 +4577,36 @@ function nt(n, e = {}) {
|
|
|
4557
4577
|
isLeaderTab: false
|
|
4558
4578
|
};
|
|
4559
4579
|
},
|
|
4560
|
-
onSyncStateChanged(
|
|
4561
|
-
if (a) return a.onSyncStateChanged(
|
|
4562
|
-
let
|
|
4563
|
-
return
|
|
4564
|
-
|
|
4580
|
+
onSyncStateChanged(p) {
|
|
4581
|
+
if (a) return a.onSyncStateChanged(p);
|
|
4582
|
+
let l = null, u = false;
|
|
4583
|
+
return d().then((f) => {
|
|
4584
|
+
u || (l = f.onSyncStateChanged(p));
|
|
4565
4585
|
}), () => {
|
|
4566
|
-
|
|
4586
|
+
u = true, l?.();
|
|
4567
4587
|
};
|
|
4568
4588
|
},
|
|
4569
4589
|
async flush() {
|
|
4570
|
-
return (await
|
|
4590
|
+
return (await d()).flush();
|
|
4571
4591
|
},
|
|
4572
4592
|
async clearCache() {
|
|
4573
|
-
return (await
|
|
4593
|
+
return (await d()).clearCache();
|
|
4574
4594
|
},
|
|
4575
4595
|
async getConflicts() {
|
|
4576
|
-
return (await
|
|
4596
|
+
return (await d()).getConflicts();
|
|
4577
4597
|
},
|
|
4578
|
-
onError(
|
|
4579
|
-
if (a) return a.onError(
|
|
4580
|
-
let
|
|
4581
|
-
return
|
|
4582
|
-
|
|
4598
|
+
onError(p) {
|
|
4599
|
+
if (a) return a.onError(p);
|
|
4600
|
+
let l = null, u = false;
|
|
4601
|
+
return d().then((f) => {
|
|
4602
|
+
u || (l = f.onError(p));
|
|
4583
4603
|
}), () => {
|
|
4584
|
-
|
|
4604
|
+
u = true, l?.();
|
|
4585
4605
|
};
|
|
4586
4606
|
}
|
|
4587
4607
|
};
|
|
4588
4608
|
}
|
|
4589
|
-
function
|
|
4609
|
+
function To(n, e) {
|
|
4590
4610
|
async function t() {
|
|
4591
4611
|
return (await e()).collection(n);
|
|
4592
4612
|
}
|
|
@@ -4676,7 +4696,7 @@ function rt(n, e) {
|
|
|
4676
4696
|
);
|
|
4677
4697
|
return { requireAuth: false, headers: { authorization: `Bearer ${t}` } };
|
|
4678
4698
|
}
|
|
4679
|
-
function
|
|
4699
|
+
function Ro(n) {
|
|
4680
4700
|
if (!n || typeof n != "object")
|
|
4681
4701
|
throw new exports.NeetruError("invalid_response", "checkout.start response is not an object");
|
|
4682
4702
|
let e = n;
|
|
@@ -4692,7 +4712,7 @@ function ko(n) {
|
|
|
4692
4712
|
requiresKyc: e.requiresKyc === true
|
|
4693
4713
|
};
|
|
4694
4714
|
}
|
|
4695
|
-
function
|
|
4715
|
+
function Io(n) {
|
|
4696
4716
|
if (!n || typeof n != "object")
|
|
4697
4717
|
throw new exports.NeetruError("invalid_response", "checkout.get response is not an object");
|
|
4698
4718
|
let e = n, t = e.intent;
|
|
@@ -4714,20 +4734,20 @@ function So(n) {
|
|
|
4714
4734
|
isStale: e.isStale === true
|
|
4715
4735
|
};
|
|
4716
4736
|
}
|
|
4717
|
-
function
|
|
4737
|
+
function Co() {
|
|
4718
4738
|
try {
|
|
4719
4739
|
return typeof globalThis < "u" && typeof globalThis.window < "u" && typeof globalThis.location < "u" && typeof globalThis.location.assign == "function";
|
|
4720
4740
|
} catch {
|
|
4721
4741
|
return false;
|
|
4722
4742
|
}
|
|
4723
4743
|
}
|
|
4724
|
-
function
|
|
4744
|
+
function Ao(n) {
|
|
4725
4745
|
try {
|
|
4726
4746
|
globalThis.location.assign(n);
|
|
4727
4747
|
} catch {
|
|
4728
4748
|
}
|
|
4729
4749
|
}
|
|
4730
|
-
function
|
|
4750
|
+
function Eo(n) {
|
|
4731
4751
|
return {
|
|
4732
4752
|
async start(e) {
|
|
4733
4753
|
if (!e?.productId)
|
|
@@ -4752,8 +4772,8 @@ function Io(n) {
|
|
|
4752
4772
|
requireAuth: r,
|
|
4753
4773
|
headers: o,
|
|
4754
4774
|
idempotencyKey: true
|
|
4755
|
-
}), s =
|
|
4756
|
-
return e.autoRedirect !== false &&
|
|
4775
|
+
}), s = Ro(i);
|
|
4776
|
+
return e.autoRedirect !== false && Co() && Ao(s.redirectUrl), s;
|
|
4757
4777
|
},
|
|
4758
4778
|
async get(e, t) {
|
|
4759
4779
|
if (!e || typeof e != "string")
|
|
@@ -4767,7 +4787,7 @@ function Io(n) {
|
|
|
4767
4787
|
requireAuth: r,
|
|
4768
4788
|
headers: o
|
|
4769
4789
|
});
|
|
4770
|
-
return
|
|
4790
|
+
return Io(i);
|
|
4771
4791
|
},
|
|
4772
4792
|
async cancel(e, t) {
|
|
4773
4793
|
if (!e || typeof e != "string")
|
|
@@ -4832,13 +4852,13 @@ var xe = class {
|
|
|
4832
4852
|
}
|
|
4833
4853
|
};
|
|
4834
4854
|
function ot(n) {
|
|
4835
|
-
return n.env === "dev" ? new xe() :
|
|
4855
|
+
return n.env === "dev" ? new xe() : Eo(n);
|
|
4836
4856
|
}
|
|
4837
4857
|
|
|
4838
4858
|
// src/webhooks.ts
|
|
4839
4859
|
C();
|
|
4840
4860
|
q();
|
|
4841
|
-
async function
|
|
4861
|
+
async function bn(n, e, t, r) {
|
|
4842
4862
|
if (!e || !t) return false;
|
|
4843
4863
|
let o = e.startsWith("sha256=") ? e.slice(7) : e;
|
|
4844
4864
|
if (!/^[0-9a-f]+$/i.test(o)) return false;
|
|
@@ -4848,33 +4868,33 @@ async function _n(n, e, t, r) {
|
|
|
4848
4868
|
"runtime_unsupported",
|
|
4849
4869
|
"verifyWebhookSignature requires globalThis.crypto.subtle (Node \u226520, modern browser, or Edge runtime)."
|
|
4850
4870
|
);
|
|
4851
|
-
let s = new TextEncoder(), a = s.encode(t),
|
|
4871
|
+
let s = new TextEncoder(), a = s.encode(t), d = typeof n == "string" ? s.encode(n) : n, p = d;
|
|
4852
4872
|
if (r !== void 0) {
|
|
4853
|
-
let y = s.encode(`${r}.`), I = new Uint8Array(y.length +
|
|
4854
|
-
I.set(y, 0), I.set(
|
|
4873
|
+
let y = s.encode(`${r}.`), I = new Uint8Array(y.length + d.length);
|
|
4874
|
+
I.set(y, 0), I.set(d, y.length), p = I;
|
|
4855
4875
|
}
|
|
4856
|
-
let
|
|
4876
|
+
let l = await i.importKey(
|
|
4857
4877
|
"raw",
|
|
4858
4878
|
a,
|
|
4859
4879
|
{ name: "HMAC", hash: "SHA-256" },
|
|
4860
4880
|
false,
|
|
4861
4881
|
["sign"]
|
|
4862
|
-
),
|
|
4882
|
+
), u = await i.sign(
|
|
4863
4883
|
"HMAC",
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
), f = new Uint8Array(
|
|
4884
|
+
l,
|
|
4885
|
+
p
|
|
4886
|
+
), f = new Uint8Array(u), m = Do(o.toLowerCase());
|
|
4867
4887
|
if (!m || m.length !== f.length) return false;
|
|
4868
4888
|
let h = 0;
|
|
4869
4889
|
for (let y = 0; y < f.length; y++)
|
|
4870
4890
|
h |= f[y] ^ m[y];
|
|
4871
4891
|
return h === 0;
|
|
4872
4892
|
}
|
|
4873
|
-
async function
|
|
4874
|
-
let o = r.toleranceMs ?? 3e5, i = (
|
|
4893
|
+
async function Po(n, e, t, r = {}) {
|
|
4894
|
+
let o = r.toleranceMs ?? 3e5, i = (u) => {
|
|
4875
4895
|
if (typeof e.get == "function")
|
|
4876
|
-
return e.get(
|
|
4877
|
-
let f = e, m =
|
|
4896
|
+
return e.get(u);
|
|
4897
|
+
let f = e, m = u.toLowerCase();
|
|
4878
4898
|
for (let h of Object.keys(f))
|
|
4879
4899
|
if (h.toLowerCase() === m) return f[h] ?? null;
|
|
4880
4900
|
return null;
|
|
@@ -4886,10 +4906,10 @@ async function Co(n, e, t, r = {}) {
|
|
|
4886
4906
|
return { ok: false, reason: "timestamp_missing" };
|
|
4887
4907
|
if (Math.abs(Date.now() - a) > o)
|
|
4888
4908
|
return { ok: false, reason: "timestamp_expired" };
|
|
4889
|
-
let
|
|
4890
|
-
return await
|
|
4909
|
+
let p = i("x-neetru-signature");
|
|
4910
|
+
return await bn(n, p, t, s) ? { ok: true } : { ok: false, reason: "invalid_signature" };
|
|
4891
4911
|
}
|
|
4892
|
-
function
|
|
4912
|
+
function Do(n) {
|
|
4893
4913
|
if (n.length % 2 !== 0) return null;
|
|
4894
4914
|
let e = new Uint8Array(n.length / 2);
|
|
4895
4915
|
for (let t = 0; t < e.length; t++) {
|
|
@@ -4899,7 +4919,7 @@ function Ao(n) {
|
|
|
4899
4919
|
}
|
|
4900
4920
|
return e;
|
|
4901
4921
|
}
|
|
4902
|
-
var
|
|
4922
|
+
var vn = [
|
|
4903
4923
|
"subscription.activated",
|
|
4904
4924
|
"subscription.cancelled",
|
|
4905
4925
|
"subscription.payment_failed",
|
|
@@ -4909,7 +4929,7 @@ var bn = [
|
|
|
4909
4929
|
"account.reactivated",
|
|
4910
4930
|
"support.ticket_replied"
|
|
4911
4931
|
];
|
|
4912
|
-
function
|
|
4932
|
+
function _n(n) {
|
|
4913
4933
|
if (!n || typeof n != "object")
|
|
4914
4934
|
throw new exports.NeetruError("invalid_response", "Webhook response is not an object");
|
|
4915
4935
|
let e = n;
|
|
@@ -4919,7 +4939,7 @@ function yn(n) {
|
|
|
4919
4939
|
id: e.id,
|
|
4920
4940
|
url: typeof e.url == "string" ? e.url : "",
|
|
4921
4941
|
events: Array.isArray(e.events) ? e.events.filter(
|
|
4922
|
-
(t) =>
|
|
4942
|
+
(t) => vn.includes(t)
|
|
4923
4943
|
) : [],
|
|
4924
4944
|
hasSecret: e.hasSecret === true,
|
|
4925
4945
|
status: e.status === "active" || e.status === "degraded" || e.status === "disabled" ? e.status : "active",
|
|
@@ -4928,7 +4948,7 @@ function yn(n) {
|
|
|
4928
4948
|
createdAt: typeof e.createdAt == "string" ? e.createdAt : (/* @__PURE__ */ new Date()).toISOString()
|
|
4929
4949
|
};
|
|
4930
4950
|
}
|
|
4931
|
-
function
|
|
4951
|
+
function wn(n) {
|
|
4932
4952
|
if (!n.url || typeof n.url != "string")
|
|
4933
4953
|
throw new exports.NeetruError("validation_failed", "url \xE9 obrigat\xF3ria");
|
|
4934
4954
|
try {
|
|
@@ -4941,7 +4961,7 @@ function vn(n) {
|
|
|
4941
4961
|
if (!Array.isArray(n.events) || n.events.length === 0)
|
|
4942
4962
|
throw new exports.NeetruError("validation_failed", "events deve ter pelo menos 1 evento");
|
|
4943
4963
|
for (let e of n.events)
|
|
4944
|
-
if (!
|
|
4964
|
+
if (!vn.includes(e))
|
|
4945
4965
|
throw new exports.NeetruError("validation_failed", `evento desconhecido: ${e}`);
|
|
4946
4966
|
if (n.secret !== void 0 && n.secret.length < 16)
|
|
4947
4967
|
throw new exports.NeetruError("validation_failed", "secret deve ter \u226516 chars (recomendado 32+)");
|
|
@@ -4960,7 +4980,7 @@ function Oe(n, ...e) {
|
|
|
4960
4980
|
function it(n) {
|
|
4961
4981
|
return {
|
|
4962
4982
|
async register(e) {
|
|
4963
|
-
|
|
4983
|
+
wn(e);
|
|
4964
4984
|
let t = Oe(n, e.productId), r = await S(n, {
|
|
4965
4985
|
method: "POST",
|
|
4966
4986
|
path: "/api/sdk/v1/webhooks",
|
|
@@ -4977,7 +4997,7 @@ function it(n) {
|
|
|
4977
4997
|
// dedup TTL 24h (mesmo padrão de usage.report/support.createTicket).
|
|
4978
4998
|
idempotencyKey: true
|
|
4979
4999
|
});
|
|
4980
|
-
return
|
|
5000
|
+
return _n(r);
|
|
4981
5001
|
},
|
|
4982
5002
|
async list(e) {
|
|
4983
5003
|
let t = Oe(n, e?.productId), r = await S(n, {
|
|
@@ -4986,7 +5006,7 @@ function it(n) {
|
|
|
4986
5006
|
query: { productId: t },
|
|
4987
5007
|
requireAuth: true
|
|
4988
5008
|
});
|
|
4989
|
-
return (Array.isArray(r?.endpoints) ? r.endpoints : []).map(
|
|
5009
|
+
return (Array.isArray(r?.endpoints) ? r.endpoints : []).map(_n);
|
|
4990
5010
|
},
|
|
4991
5011
|
async unregister(e, t) {
|
|
4992
5012
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
@@ -5023,7 +5043,7 @@ var ae = class {
|
|
|
5023
5043
|
endpoints = /* @__PURE__ */ new Map();
|
|
5024
5044
|
nextId = 1;
|
|
5025
5045
|
async register(e) {
|
|
5026
|
-
|
|
5046
|
+
wn(e);
|
|
5027
5047
|
let t = `mock_wh_${this.nextId++}`, r = {
|
|
5028
5048
|
id: t,
|
|
5029
5049
|
url: e.url,
|
|
@@ -5058,7 +5078,7 @@ var st = [
|
|
|
5058
5078
|
"warning",
|
|
5059
5079
|
"error"
|
|
5060
5080
|
];
|
|
5061
|
-
function
|
|
5081
|
+
function kn(n) {
|
|
5062
5082
|
if (!n || typeof n != "object")
|
|
5063
5083
|
throw new exports.NeetruError("invalid_response", "Notification response is not an object");
|
|
5064
5084
|
let e = n;
|
|
@@ -5079,7 +5099,7 @@ function wn(n) {
|
|
|
5079
5099
|
dismissedAt: typeof e.dismissedAt == "string" ? e.dismissedAt : void 0
|
|
5080
5100
|
};
|
|
5081
5101
|
}
|
|
5082
|
-
function
|
|
5102
|
+
function Sn(n) {
|
|
5083
5103
|
if (!n.userId) throw new exports.NeetruError("validation_failed", "userId obrigat\xF3rio");
|
|
5084
5104
|
if (!n.kind) throw new exports.NeetruError("validation_failed", "kind obrigat\xF3rio");
|
|
5085
5105
|
if (!n.title) throw new exports.NeetruError("validation_failed", "title obrigat\xF3rio");
|
|
@@ -5107,7 +5127,7 @@ function Le(n, ...e) {
|
|
|
5107
5127
|
function at(n) {
|
|
5108
5128
|
return {
|
|
5109
5129
|
async send(e) {
|
|
5110
|
-
|
|
5130
|
+
Sn(e);
|
|
5111
5131
|
let r = {
|
|
5112
5132
|
productId: Le(n, e.productId),
|
|
5113
5133
|
userId: e.userId,
|
|
@@ -5123,7 +5143,7 @@ function at(n) {
|
|
|
5123
5143
|
requireAuth: true,
|
|
5124
5144
|
idempotencyKey: true
|
|
5125
5145
|
});
|
|
5126
|
-
return
|
|
5146
|
+
return kn(o);
|
|
5127
5147
|
},
|
|
5128
5148
|
async list(e, t) {
|
|
5129
5149
|
if (!e) throw new exports.NeetruError("validation_failed", "userId obrigat\xF3rio");
|
|
@@ -5137,7 +5157,7 @@ function at(n) {
|
|
|
5137
5157
|
query: o,
|
|
5138
5158
|
requireAuth: true
|
|
5139
5159
|
});
|
|
5140
|
-
return (Array.isArray(i?.notifications) ? i.notifications : []).map(
|
|
5160
|
+
return (Array.isArray(i?.notifications) ? i.notifications : []).map(kn);
|
|
5141
5161
|
},
|
|
5142
5162
|
async markRead(e, t) {
|
|
5143
5163
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
@@ -5165,7 +5185,7 @@ var ce = class {
|
|
|
5165
5185
|
notifications = /* @__PURE__ */ new Map();
|
|
5166
5186
|
nextId = 1;
|
|
5167
5187
|
async send(e) {
|
|
5168
|
-
if (
|
|
5188
|
+
if (Sn(e), e.fingerprint) {
|
|
5169
5189
|
let o = Date.now() - 864e5;
|
|
5170
5190
|
for (let i of this.notifications.values())
|
|
5171
5191
|
if ((i.metadata ?? {}).fingerprint === e.fingerprint && i.userId === e.userId && new Date(i.createdAt).getTime() > o)
|
|
@@ -5205,41 +5225,41 @@ var ce = class {
|
|
|
5205
5225
|
C();
|
|
5206
5226
|
q();
|
|
5207
5227
|
We();
|
|
5208
|
-
var
|
|
5228
|
+
var No = "/api/sdk/v1/ai/chat", Tn = 12e4, Ue = 32768, Rn = 32, In = 4194304, Cn = 2048, xo = /^data:image\/(?:png|jpe?g|gif|webp);base64,[A-Za-z0-9+/]+={0,2}$/, Oo = /^https:\/\/[^\s]+$/, Lo = /* @__PURE__ */ new Set(["low", "high", "auto"]), Uo = /* @__PURE__ */ new Set(["system", "user", "assistant"]), An = 100, En = 128, Pn = 32768, Dn = 0, Nn = 2, xn = 200, On = 20, Ln = 12 * 1024 * 1024, Un = 5e4, Mn = 16, qn = 500, Mo = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]), qo = /^[a-zA-Z0-9_-]+$/;
|
|
5209
5229
|
function R(n) {
|
|
5210
5230
|
throw new exports.NeetruError("validation_failed", n);
|
|
5211
5231
|
}
|
|
5212
|
-
function
|
|
5232
|
+
function Fo(n) {
|
|
5213
5233
|
if (n.length === 0 || n.length % 4 !== 0) return false;
|
|
5214
5234
|
let e = n.endsWith("==") ? 2 : n.endsWith("=") ? 1 : 0;
|
|
5215
5235
|
return !(e > 0 ? n.slice(0, n.length - e) : n).includes("=");
|
|
5216
5236
|
}
|
|
5217
|
-
function
|
|
5237
|
+
function jo(n) {
|
|
5218
5238
|
let e = n.indexOf(",");
|
|
5219
5239
|
if (e === -1) return 0;
|
|
5220
5240
|
let t = n.slice(e + 1), r = t.endsWith("==") ? 2 : t.endsWith("=") ? 1 : 0;
|
|
5221
5241
|
return Math.max(0, Math.floor(t.length * 3 / 4) - r);
|
|
5222
5242
|
}
|
|
5223
|
-
function
|
|
5243
|
+
function Wo(n) {
|
|
5224
5244
|
let e = 0, t = 0, r = 0;
|
|
5225
5245
|
for (let o of n) {
|
|
5226
5246
|
let i = o?.content;
|
|
5227
5247
|
if (Array.isArray(i))
|
|
5228
5248
|
for (let s of i) {
|
|
5229
|
-
if (e++, e >
|
|
5230
|
-
return `ai: total content parts exceeds ${
|
|
5249
|
+
if (e++, e > xn)
|
|
5250
|
+
return `ai: total content parts exceeds ${xn} per request`;
|
|
5231
5251
|
if (s && typeof s == "object" && s.type === "image_url") {
|
|
5232
|
-
if (t++, t >
|
|
5233
|
-
return `ai: total images exceeds ${
|
|
5252
|
+
if (t++, t > On)
|
|
5253
|
+
return `ai: total images exceeds ${On} per request`;
|
|
5234
5254
|
let a = s.image_url?.url;
|
|
5235
|
-
if (typeof a == "string" && a.startsWith("data:") && (r +=
|
|
5236
|
-
return `ai: aggregate image bytes exceeds ${
|
|
5255
|
+
if (typeof a == "string" && a.startsWith("data:") && (r += jo(a), r > Ln))
|
|
5256
|
+
return `ai: aggregate image bytes exceeds ${Ln} per request`;
|
|
5237
5257
|
}
|
|
5238
5258
|
}
|
|
5239
5259
|
}
|
|
5240
5260
|
return null;
|
|
5241
5261
|
}
|
|
5242
|
-
function
|
|
5262
|
+
function $o(n) {
|
|
5243
5263
|
if (n === void 0) return null;
|
|
5244
5264
|
if (n === null || typeof n != "object")
|
|
5245
5265
|
return "ai: responseFormat json_schema.schema must be an object";
|
|
@@ -5250,11 +5270,11 @@ function Fo(n) {
|
|
|
5250
5270
|
return "ai: responseFormat json_schema.schema is not serializable";
|
|
5251
5271
|
}
|
|
5252
5272
|
if (e === void 0) return "ai: responseFormat json_schema.schema is invalid";
|
|
5253
|
-
if (e.length >
|
|
5254
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
5273
|
+
if (e.length > Un)
|
|
5274
|
+
return `ai: responseFormat json_schema.schema exceeds ${Un} serialized chars`;
|
|
5255
5275
|
let t = 0, r = (o, i) => {
|
|
5256
|
-
if (i >
|
|
5257
|
-
return `ai: responseFormat json_schema.schema exceeds max depth ${
|
|
5276
|
+
if (i > Mn)
|
|
5277
|
+
return `ai: responseFormat json_schema.schema exceeds max depth ${Mn}`;
|
|
5258
5278
|
if (Array.isArray(o)) {
|
|
5259
5279
|
for (let s of o) {
|
|
5260
5280
|
let a = r(s, i + 1);
|
|
@@ -5264,10 +5284,10 @@ function Fo(n) {
|
|
|
5264
5284
|
}
|
|
5265
5285
|
if (o && typeof o == "object")
|
|
5266
5286
|
for (let s of Object.getOwnPropertyNames(o)) {
|
|
5267
|
-
if (
|
|
5287
|
+
if (Mo.has(s))
|
|
5268
5288
|
return `ai: responseFormat json_schema.schema has forbidden key: ${s}`;
|
|
5269
|
-
if (t++, t >
|
|
5270
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
5289
|
+
if (t++, t > qn)
|
|
5290
|
+
return `ai: responseFormat json_schema.schema exceeds ${qn} keys`;
|
|
5271
5291
|
let a = r(o[s], i + 1);
|
|
5272
5292
|
if (a) return a;
|
|
5273
5293
|
}
|
|
@@ -5275,27 +5295,27 @@ function Fo(n) {
|
|
|
5275
5295
|
};
|
|
5276
5296
|
return r(n, 0);
|
|
5277
5297
|
}
|
|
5278
|
-
function
|
|
5298
|
+
function Vo(n) {
|
|
5279
5299
|
(typeof n != "string" || n.length === 0) && R("ai: image_url.url must be a non-empty string");
|
|
5280
5300
|
let e = n;
|
|
5281
5301
|
if (e.startsWith("data:")) {
|
|
5282
|
-
e.length >
|
|
5302
|
+
e.length > In && R(`ai: image_url.url (data:) exceeds ${In} chars`), xo.test(e) || R("ai: image_url.url data: must be data:image/<png|jpeg|jpg|gif|webp>;base64,<...>");
|
|
5283
5303
|
let t = e.slice(e.indexOf(",") + 1);
|
|
5284
|
-
|
|
5304
|
+
Fo(t) || R("ai: image_url.url data: has invalid base64 (length/padding)");
|
|
5285
5305
|
return;
|
|
5286
5306
|
}
|
|
5287
5307
|
if (e.startsWith("https://")) {
|
|
5288
|
-
e.length >
|
|
5308
|
+
e.length > Cn && R(`ai: image_url.url (https:) exceeds ${Cn} chars`), Oo.test(e) || R("ai: image_url.url https: is invalid");
|
|
5289
5309
|
return;
|
|
5290
5310
|
}
|
|
5291
5311
|
R("ai: image_url.url must be a data:image/...;base64 or https:// URL (other schemes are rejected)");
|
|
5292
5312
|
}
|
|
5293
|
-
function
|
|
5313
|
+
function Bo(n, e) {
|
|
5294
5314
|
if (typeof n == "string") {
|
|
5295
5315
|
n.length === 0 && R("ai: message content string must be non-empty"), n.length > Ue && R(`ai: message content exceeds ${Ue} chars`);
|
|
5296
5316
|
return;
|
|
5297
5317
|
}
|
|
5298
|
-
(!Array.isArray(n) || n.length === 0) && R("ai: message content must be a non-empty string or array of parts"), n.length >
|
|
5318
|
+
(!Array.isArray(n) || n.length === 0) && R("ai: message content must be a non-empty string or array of parts"), n.length > Rn && R(`ai: message content has more than ${Rn} parts`);
|
|
5299
5319
|
for (let t of n) {
|
|
5300
5320
|
(!t || typeof t != "object") && R("ai: each content part must be an object");
|
|
5301
5321
|
let r = t.type;
|
|
@@ -5305,14 +5325,14 @@ function Wo(n, e) {
|
|
|
5305
5325
|
} else if (r === "image_url") {
|
|
5306
5326
|
e !== "user" && R(`ai: image_url is only allowed in role 'user' messages (got '${e}')`);
|
|
5307
5327
|
let o = t.image_url;
|
|
5308
|
-
(!o || typeof o != "object") && R("ai: image_url part must have { image_url: { url } }"),
|
|
5328
|
+
(!o || typeof o != "object") && R("ai: image_url part must have { image_url: { url } }"), Vo(o.url);
|
|
5309
5329
|
let i = o.detail;
|
|
5310
|
-
i !== void 0 && (typeof i != "string" || !
|
|
5330
|
+
i !== void 0 && (typeof i != "string" || !Lo.has(i)) && R("ai: image_url.detail must be 'low' | 'high' | 'auto'");
|
|
5311
5331
|
} else
|
|
5312
5332
|
R("ai: content part type must be 'text' or 'image_url'");
|
|
5313
5333
|
}
|
|
5314
5334
|
}
|
|
5315
|
-
function
|
|
5335
|
+
function Ko(n) {
|
|
5316
5336
|
(!n || typeof n != "object") && R("ai: responseFormat must be an object");
|
|
5317
5337
|
let e = n.type;
|
|
5318
5338
|
if (!(e === "text" || e === "json_object")) {
|
|
@@ -5320,8 +5340,8 @@ function $o(n) {
|
|
|
5320
5340
|
let t = n.json_schema;
|
|
5321
5341
|
(!t || typeof t != "object") && R("ai: responseFormat json_schema requires { json_schema: { name } }");
|
|
5322
5342
|
let r = t, o = r.name;
|
|
5323
|
-
if ((typeof o != "string" || o.length === 0) && R("ai: responseFormat json_schema.name is required"), o.length > 64 && R("ai: responseFormat json_schema.name exceeds 64 chars"),
|
|
5324
|
-
let i =
|
|
5343
|
+
if ((typeof o != "string" || o.length === 0) && R("ai: responseFormat json_schema.name is required"), o.length > 64 && R("ai: responseFormat json_schema.name exceeds 64 chars"), qo.test(o) || R("ai: responseFormat json_schema.name must match a-zA-Z0-9_-"), r.description !== void 0 && (typeof r.description != "string" && R("ai: responseFormat json_schema.description must be a string"), r.description.length > 1e3 && R("ai: responseFormat json_schema.description exceeds 1000 chars")), r.strict !== void 0 && typeof r.strict != "boolean" && R("ai: responseFormat json_schema.strict must be a boolean"), r.schema !== void 0) {
|
|
5344
|
+
let i = $o(r.schema);
|
|
5325
5345
|
i && R(i);
|
|
5326
5346
|
}
|
|
5327
5347
|
return;
|
|
@@ -5329,26 +5349,26 @@ function $o(n) {
|
|
|
5329
5349
|
R("ai: responseFormat.type must be 'text' | 'json_object' | 'json_schema'");
|
|
5330
5350
|
}
|
|
5331
5351
|
}
|
|
5332
|
-
function
|
|
5352
|
+
function Fn(n, e, t) {
|
|
5333
5353
|
if (!e || typeof e != "object")
|
|
5334
5354
|
throw new exports.NeetruError("validation_failed", "ai: options object is required");
|
|
5335
5355
|
if (typeof e.model != "string" || e.model.length === 0)
|
|
5336
5356
|
throw new exports.NeetruError("validation_failed", "ai: model is required");
|
|
5337
|
-
if (e.model.length >
|
|
5338
|
-
throw new exports.NeetruError("validation_failed", `ai: model exceeds ${
|
|
5357
|
+
if (e.model.length > En)
|
|
5358
|
+
throw new exports.NeetruError("validation_failed", `ai: model exceeds ${En} chars`);
|
|
5339
5359
|
if (!Array.isArray(e.messages) || e.messages.length === 0)
|
|
5340
5360
|
throw new exports.NeetruError("validation_failed", "ai: messages must be a non-empty array");
|
|
5341
|
-
if (e.messages.length >
|
|
5342
|
-
throw new exports.NeetruError("validation_failed", `ai: messages exceeds ${
|
|
5361
|
+
if (e.messages.length > An)
|
|
5362
|
+
throw new exports.NeetruError("validation_failed", `ai: messages exceeds ${An} per request`);
|
|
5343
5363
|
for (let a of e.messages) {
|
|
5344
|
-
if (!a || typeof a != "object" || typeof a.role != "string" || !
|
|
5364
|
+
if (!a || typeof a != "object" || typeof a.role != "string" || !Uo.has(a.role))
|
|
5345
5365
|
throw new exports.NeetruError(
|
|
5346
5366
|
"validation_failed",
|
|
5347
5367
|
"ai: each message role must be 'system' | 'user' | 'assistant'"
|
|
5348
5368
|
);
|
|
5349
|
-
|
|
5369
|
+
Bo(a.content, a.role);
|
|
5350
5370
|
}
|
|
5351
|
-
let r =
|
|
5371
|
+
let r = Wo(e.messages);
|
|
5352
5372
|
if (r) throw new exports.NeetruError("validation_failed", r);
|
|
5353
5373
|
let o = e.productId ?? n.productId, i = e.tenantId ?? n.tenantId;
|
|
5354
5374
|
if (!o)
|
|
@@ -5370,24 +5390,24 @@ function qn(n, e, t) {
|
|
|
5370
5390
|
stream: t
|
|
5371
5391
|
};
|
|
5372
5392
|
if (e.maxTokens !== void 0) {
|
|
5373
|
-
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens >
|
|
5393
|
+
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens > Pn)
|
|
5374
5394
|
throw new exports.NeetruError(
|
|
5375
5395
|
"validation_failed",
|
|
5376
|
-
`ai: maxTokens must be a positive integer <= ${
|
|
5396
|
+
`ai: maxTokens must be a positive integer <= ${Pn}`
|
|
5377
5397
|
);
|
|
5378
5398
|
s.maxTokens = e.maxTokens;
|
|
5379
5399
|
}
|
|
5380
5400
|
if (e.temperature !== void 0) {
|
|
5381
|
-
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature <
|
|
5401
|
+
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature < Dn || e.temperature > Nn)
|
|
5382
5402
|
throw new exports.NeetruError(
|
|
5383
5403
|
"validation_failed",
|
|
5384
|
-
`ai: temperature must be a finite number between ${
|
|
5404
|
+
`ai: temperature must be a finite number between ${Dn} and ${Nn}`
|
|
5385
5405
|
);
|
|
5386
5406
|
s.temperature = e.temperature;
|
|
5387
5407
|
}
|
|
5388
|
-
return e.responseFormat !== void 0 && (
|
|
5408
|
+
return e.responseFormat !== void 0 && (Ko(e.responseFormat), s.responseFormat = e.responseFormat), s;
|
|
5389
5409
|
}
|
|
5390
|
-
async function
|
|
5410
|
+
async function Vn(n) {
|
|
5391
5411
|
try {
|
|
5392
5412
|
let e = await n.text();
|
|
5393
5413
|
return e ? JSON.parse(e) : void 0;
|
|
@@ -5395,48 +5415,48 @@ async function $n(n) {
|
|
|
5395
5415
|
return;
|
|
5396
5416
|
}
|
|
5397
5417
|
}
|
|
5398
|
-
function
|
|
5418
|
+
function zo(n, e) {
|
|
5399
5419
|
let t = n.replace(/\/+$/, ""), r = e.startsWith("/") ? e : `/${e}`;
|
|
5400
5420
|
return `${t}${r}`;
|
|
5401
5421
|
}
|
|
5402
|
-
async function
|
|
5422
|
+
async function jn(n, e, t, r) {
|
|
5403
5423
|
if (!n.apiKey)
|
|
5404
5424
|
throw new exports.NeetruError(
|
|
5405
5425
|
"missing_api_key",
|
|
5406
5426
|
"ai requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
5407
5427
|
);
|
|
5408
|
-
let o =
|
|
5428
|
+
let o = zo(n.baseUrl, No), { headers: i, correlationId: s } = _e({
|
|
5409
5429
|
"content-type": "application/json",
|
|
5410
5430
|
accept: r ? "text/event-stream" : "application/json",
|
|
5411
5431
|
authorization: `Bearer ${n.apiKey}`
|
|
5412
5432
|
}), a = { method: "POST", headers: i, body: JSON.stringify(e) };
|
|
5413
|
-
r ? t && (a.signal = t) : a.signal = t ?? AbortSignal.timeout(
|
|
5414
|
-
let
|
|
5433
|
+
r ? t && (a.signal = t) : a.signal = t ?? AbortSignal.timeout(Tn);
|
|
5434
|
+
let d;
|
|
5415
5435
|
try {
|
|
5416
|
-
|
|
5417
|
-
} catch (
|
|
5418
|
-
let f =
|
|
5436
|
+
d = await n.fetch(o, a);
|
|
5437
|
+
} catch (u) {
|
|
5438
|
+
let f = u instanceof DOMException && u.name === "TimeoutError" ? `Network error: timeout after ${Tn / 1e3}s` : u instanceof DOMException && u.name === "AbortError" ? "Network error: request aborted" : `Network error: ${u instanceof Error ? u.message : "fetch failed"}`;
|
|
5419
5439
|
throw new exports.NeetruError("network_error", f, void 0, void 0, void 0, s);
|
|
5420
5440
|
}
|
|
5421
|
-
let
|
|
5422
|
-
if (!
|
|
5423
|
-
let
|
|
5424
|
-
if (
|
|
5425
|
-
let h =
|
|
5441
|
+
let p = d.headers.get("x-request-id") ?? d.headers.get("x-correlation-id") ?? void 0;
|
|
5442
|
+
if (!d.ok) {
|
|
5443
|
+
let u = await Vn(d), f = ve(d.status), m = `HTTP ${d.status}`;
|
|
5444
|
+
if (u && typeof u == "object" && "error" in u) {
|
|
5445
|
+
let h = u.error;
|
|
5426
5446
|
typeof h == "string" ? m = h : h && typeof h == "object" && (typeof h.code == "string" && (f = h.code), typeof h.message == "string" && (m = h.message));
|
|
5427
5447
|
}
|
|
5428
|
-
throw new exports.NeetruError(f, m,
|
|
5448
|
+
throw new exports.NeetruError(f, m, d.status, p, void 0, s);
|
|
5429
5449
|
}
|
|
5430
|
-
let
|
|
5431
|
-
return
|
|
5450
|
+
let l = d.headers.get("x-neetru-version");
|
|
5451
|
+
return l && te(l), d;
|
|
5432
5452
|
}
|
|
5433
|
-
function
|
|
5453
|
+
function Wn(n) {
|
|
5434
5454
|
return {
|
|
5435
5455
|
provider: n.headers.get("x-neetru-provider") ?? "",
|
|
5436
5456
|
usageWarning: n.headers.get("x-neetru-ai-usage-warning") === "1"
|
|
5437
5457
|
};
|
|
5438
5458
|
}
|
|
5439
|
-
function
|
|
5459
|
+
function $n(n) {
|
|
5440
5460
|
let e = [];
|
|
5441
5461
|
for (let r of n.split(`
|
|
5442
5462
|
`)) {
|
|
@@ -5460,7 +5480,7 @@ function Wn(n) {
|
|
|
5460
5480
|
function ct(n) {
|
|
5461
5481
|
return {
|
|
5462
5482
|
async chat(e) {
|
|
5463
|
-
let t =
|
|
5483
|
+
let t = Fn(n, e, false), r = await jn(n, t, e.signal, false), { provider: o, usageWarning: i } = Wn(r), s = await Vn(r);
|
|
5464
5484
|
if (!s || s.ok !== true || !Array.isArray(s.choices))
|
|
5465
5485
|
throw new exports.NeetruError("invalid_response", "ai.chat response missing ok/choices");
|
|
5466
5486
|
return {
|
|
@@ -5472,38 +5492,38 @@ function ct(n) {
|
|
|
5472
5492
|
};
|
|
5473
5493
|
},
|
|
5474
5494
|
stream(e) {
|
|
5475
|
-
let t =
|
|
5495
|
+
let t = Fn(n, e, true), r = jn(n, t, e.signal, true), o = r.then(Wn);
|
|
5476
5496
|
o.catch(() => {
|
|
5477
5497
|
});
|
|
5478
5498
|
async function* i() {
|
|
5479
5499
|
let a = (await r).body;
|
|
5480
5500
|
if (!a)
|
|
5481
5501
|
throw new exports.NeetruError("invalid_response", "ai.stream: response has no body");
|
|
5482
|
-
let
|
|
5502
|
+
let d = a.getReader(), p = new TextDecoder(), l = "";
|
|
5483
5503
|
try {
|
|
5484
5504
|
for (; ; ) {
|
|
5485
|
-
let { done: f, value: m } = await
|
|
5505
|
+
let { done: f, value: m } = await d.read();
|
|
5486
5506
|
if (f) break;
|
|
5487
|
-
|
|
5507
|
+
l += p.decode(m, { stream: !0 });
|
|
5488
5508
|
let h;
|
|
5489
|
-
for (; (h =
|
|
5509
|
+
for (; (h = l.indexOf(`
|
|
5490
5510
|
|
|
5491
5511
|
`)) !== -1; ) {
|
|
5492
|
-
let y =
|
|
5493
|
-
|
|
5494
|
-
let I =
|
|
5512
|
+
let y = l.slice(0, h);
|
|
5513
|
+
l = l.slice(h + 2);
|
|
5514
|
+
let I = $n(y);
|
|
5495
5515
|
if (I && "done" in I) return;
|
|
5496
5516
|
I && "content" in I && (yield I.content);
|
|
5497
5517
|
}
|
|
5498
5518
|
}
|
|
5499
|
-
let
|
|
5500
|
-
if (
|
|
5501
|
-
let f =
|
|
5519
|
+
let u = l.trim();
|
|
5520
|
+
if (u) {
|
|
5521
|
+
let f = $n(u);
|
|
5502
5522
|
f && "content" in f && (yield f.content);
|
|
5503
5523
|
}
|
|
5504
5524
|
} finally {
|
|
5505
5525
|
try {
|
|
5506
|
-
|
|
5526
|
+
d.releaseLock();
|
|
5507
5527
|
} catch {
|
|
5508
5528
|
}
|
|
5509
5529
|
try {
|
|
@@ -5695,14 +5715,14 @@ var qe = Object.freeze({
|
|
|
5695
5715
|
tenantId: r?.tenantId,
|
|
5696
5716
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
5697
5717
|
});
|
|
5698
|
-
let
|
|
5718
|
+
let d = this._quotas.get(e)?.limit ?? -1;
|
|
5699
5719
|
return {
|
|
5700
5720
|
ok: true,
|
|
5701
5721
|
counterId: `mock_${e}`,
|
|
5702
5722
|
value: s,
|
|
5703
|
-
limit:
|
|
5704
|
-
remaining:
|
|
5705
|
-
status:
|
|
5723
|
+
limit: d,
|
|
5724
|
+
remaining: d < 0 ? -1 : Math.max(0, d - s),
|
|
5725
|
+
status: d > 0 && s >= d ? "read_only_overlimit" : "ok"
|
|
5706
5726
|
};
|
|
5707
5727
|
}
|
|
5708
5728
|
async check(e, t) {
|
|
@@ -5848,40 +5868,40 @@ var qe = Object.freeze({
|
|
|
5848
5868
|
collection(e) {
|
|
5849
5869
|
let t = this._store, r = this._fixtures;
|
|
5850
5870
|
if (!t.has(e)) {
|
|
5851
|
-
let
|
|
5871
|
+
let l = r.get(e);
|
|
5852
5872
|
t.set(
|
|
5853
5873
|
e,
|
|
5854
|
-
|
|
5874
|
+
l ? new Map(Object.entries(l)) : /* @__PURE__ */ new Map()
|
|
5855
5875
|
);
|
|
5856
5876
|
}
|
|
5857
|
-
let o = t.get(e), i = (
|
|
5858
|
-
let f =
|
|
5859
|
-
switch (
|
|
5877
|
+
let o = t.get(e), i = (l, u) => {
|
|
5878
|
+
let f = l[u.field];
|
|
5879
|
+
switch (u.op) {
|
|
5860
5880
|
case "==":
|
|
5861
|
-
return f ===
|
|
5881
|
+
return f === u.value;
|
|
5862
5882
|
case "!=":
|
|
5863
|
-
return f !==
|
|
5883
|
+
return f !== u.value;
|
|
5864
5884
|
case "<":
|
|
5865
|
-
return typeof f == "number" && typeof
|
|
5885
|
+
return typeof f == "number" && typeof u.value == "number" && f < u.value;
|
|
5866
5886
|
case "<=":
|
|
5867
|
-
return typeof f == "number" && typeof
|
|
5887
|
+
return typeof f == "number" && typeof u.value == "number" && f <= u.value;
|
|
5868
5888
|
case ">":
|
|
5869
|
-
return typeof f == "number" && typeof
|
|
5889
|
+
return typeof f == "number" && typeof u.value == "number" && f > u.value;
|
|
5870
5890
|
case ">=":
|
|
5871
|
-
return typeof f == "number" && typeof
|
|
5891
|
+
return typeof f == "number" && typeof u.value == "number" && f >= u.value;
|
|
5872
5892
|
case "in":
|
|
5873
|
-
return Array.isArray(
|
|
5893
|
+
return Array.isArray(u.value) && u.value.includes(f);
|
|
5874
5894
|
case "array-contains":
|
|
5875
|
-
return Array.isArray(f) && f.includes(
|
|
5895
|
+
return Array.isArray(f) && f.includes(u.value);
|
|
5876
5896
|
case "not-in":
|
|
5877
|
-
return Array.isArray(
|
|
5897
|
+
return Array.isArray(u.value) && !u.value.includes(f);
|
|
5878
5898
|
default:
|
|
5879
5899
|
return true;
|
|
5880
5900
|
}
|
|
5881
5901
|
}, s = 0;
|
|
5882
|
-
function a(
|
|
5902
|
+
function a(l) {
|
|
5883
5903
|
return {
|
|
5884
|
-
docs:
|
|
5904
|
+
docs: l.map((u, f) => ({ id: u.id ?? String(f), data: u })),
|
|
5885
5905
|
nextCursor: null,
|
|
5886
5906
|
fromCache: false,
|
|
5887
5907
|
stale: false,
|
|
@@ -5889,111 +5909,111 @@ var qe = Object.freeze({
|
|
|
5889
5909
|
changes: []
|
|
5890
5910
|
};
|
|
5891
5911
|
}
|
|
5892
|
-
function
|
|
5912
|
+
function d(l, u) {
|
|
5893
5913
|
return {
|
|
5894
|
-
docs: [{ id:
|
|
5914
|
+
docs: [{ id: u, data: l }],
|
|
5895
5915
|
fromCache: false,
|
|
5896
5916
|
stale: false,
|
|
5897
5917
|
hasPendingWrites: false,
|
|
5898
5918
|
changes: []
|
|
5899
5919
|
};
|
|
5900
5920
|
}
|
|
5901
|
-
let
|
|
5902
|
-
async list(
|
|
5903
|
-
let
|
|
5904
|
-
if (
|
|
5905
|
-
(m) =>
|
|
5906
|
-
)),
|
|
5907
|
-
let { field: m, direction: h } =
|
|
5908
|
-
|
|
5921
|
+
let p = {
|
|
5922
|
+
async list(l) {
|
|
5923
|
+
let u = Array.from(o.values());
|
|
5924
|
+
if (l?.where && l.where.length > 0 && (u = u.filter(
|
|
5925
|
+
(m) => l.where.every((h) => i(m, h))
|
|
5926
|
+
)), l?.orderBy) {
|
|
5927
|
+
let { field: m, direction: h } = l.orderBy;
|
|
5928
|
+
u = u.slice().sort((y, I) => {
|
|
5909
5929
|
let w = y[m], k = I[m], N = w ?? "", g = k ?? "", _ = N < g ? -1 : N > g ? 1 : 0;
|
|
5910
5930
|
return h === "desc" ? -_ : _;
|
|
5911
5931
|
});
|
|
5912
5932
|
}
|
|
5913
|
-
let f =
|
|
5914
|
-
return
|
|
5933
|
+
let f = l?.limit ?? 20;
|
|
5934
|
+
return u = u.slice(0, f), a(u);
|
|
5915
5935
|
},
|
|
5916
|
-
async get(
|
|
5917
|
-
let
|
|
5918
|
-
return
|
|
5936
|
+
async get(l) {
|
|
5937
|
+
let u = o.get(l);
|
|
5938
|
+
return u ? d(u, l) : null;
|
|
5919
5939
|
},
|
|
5920
|
-
async add(
|
|
5921
|
-
let
|
|
5922
|
-
return o.set(
|
|
5940
|
+
async add(l) {
|
|
5941
|
+
let u = `mock-${++s}-${Math.random().toString(36).slice(2, 8)}`;
|
|
5942
|
+
return o.set(u, { ...l, id: u }), { ok: true, id: u };
|
|
5923
5943
|
},
|
|
5924
|
-
async set(
|
|
5925
|
-
return o.set(
|
|
5944
|
+
async set(l, u) {
|
|
5945
|
+
return o.set(l, { ...u, id: l }), { ok: true };
|
|
5926
5946
|
},
|
|
5927
|
-
async update(
|
|
5928
|
-
let f = o.get(
|
|
5929
|
-
return f ? o.set(
|
|
5947
|
+
async update(l, u) {
|
|
5948
|
+
let f = o.get(l);
|
|
5949
|
+
return f ? o.set(l, { ...f, ...u }) : o.set(l, { ...u, id: l }), { ok: true };
|
|
5930
5950
|
},
|
|
5931
|
-
async remove(
|
|
5932
|
-
return o.delete(
|
|
5951
|
+
async remove(l) {
|
|
5952
|
+
return o.delete(l), { ok: true };
|
|
5933
5953
|
},
|
|
5934
|
-
async batch(
|
|
5935
|
-
for (let
|
|
5954
|
+
async batch(l) {
|
|
5955
|
+
for (let u of l) {
|
|
5936
5956
|
let f = o;
|
|
5937
|
-
if (
|
|
5957
|
+
if (u.kind === "add") {
|
|
5938
5958
|
let m = `mock-batch-${++s}`;
|
|
5939
|
-
f.set(m, { ...
|
|
5940
|
-
} else if (
|
|
5941
|
-
f.set(
|
|
5942
|
-
else if (
|
|
5943
|
-
let m = f.get(
|
|
5944
|
-
f.set(
|
|
5945
|
-
} else
|
|
5959
|
+
f.set(m, { ...u.data, id: m });
|
|
5960
|
+
} else if (u.kind === "set")
|
|
5961
|
+
f.set(u.id, { ...u.data, id: u.id });
|
|
5962
|
+
else if (u.kind === "update") {
|
|
5963
|
+
let m = f.get(u.id);
|
|
5964
|
+
f.set(u.id, { ...m ?? {}, ...u.data, id: u.id });
|
|
5965
|
+
} else u.kind === "remove" && f.delete(u.id);
|
|
5946
5966
|
}
|
|
5947
5967
|
return { ok: true };
|
|
5948
5968
|
},
|
|
5949
|
-
onDoc(
|
|
5950
|
-
let f = o.get(
|
|
5951
|
-
return Promise.resolve().then(() =>
|
|
5969
|
+
onDoc(l, u) {
|
|
5970
|
+
let f = o.get(l);
|
|
5971
|
+
return Promise.resolve().then(() => u(f || null)), () => {
|
|
5952
5972
|
};
|
|
5953
5973
|
},
|
|
5954
|
-
onSnapshot(
|
|
5974
|
+
onSnapshot(l, u) {
|
|
5955
5975
|
return Promise.resolve().then(async () => {
|
|
5956
|
-
let f = await
|
|
5957
|
-
|
|
5976
|
+
let f = await p.list(l);
|
|
5977
|
+
u(f);
|
|
5958
5978
|
}), () => {
|
|
5959
5979
|
};
|
|
5960
5980
|
},
|
|
5961
|
-
doc(
|
|
5981
|
+
doc(l) {
|
|
5962
5982
|
return {
|
|
5963
5983
|
get id() {
|
|
5964
|
-
return
|
|
5984
|
+
return l;
|
|
5965
5985
|
},
|
|
5966
5986
|
get path() {
|
|
5967
|
-
return `${e}/${
|
|
5987
|
+
return `${e}/${l}`;
|
|
5968
5988
|
},
|
|
5969
5989
|
get collection() {
|
|
5970
5990
|
return e;
|
|
5971
5991
|
},
|
|
5972
5992
|
async get() {
|
|
5973
|
-
let
|
|
5974
|
-
return
|
|
5993
|
+
let u = o.get(l);
|
|
5994
|
+
return u ? d(u, l) : null;
|
|
5975
5995
|
},
|
|
5976
|
-
async set(
|
|
5977
|
-
return o.set(
|
|
5996
|
+
async set(u) {
|
|
5997
|
+
return o.set(l, { ...u, id: l }), { ok: true };
|
|
5978
5998
|
},
|
|
5979
|
-
async update(
|
|
5980
|
-
let f = o.get(
|
|
5981
|
-
return f ? o.set(
|
|
5999
|
+
async update(u) {
|
|
6000
|
+
let f = o.get(l);
|
|
6001
|
+
return f ? o.set(l, { ...f, ...u }) : o.set(l, { ...u, id: l }), { ok: true };
|
|
5982
6002
|
},
|
|
5983
6003
|
async remove() {
|
|
5984
|
-
return o.delete(
|
|
6004
|
+
return o.delete(l), { ok: true };
|
|
5985
6005
|
},
|
|
5986
|
-
onSnapshot(
|
|
5987
|
-
let f = o.get(
|
|
6006
|
+
onSnapshot(u) {
|
|
6007
|
+
let f = o.get(l);
|
|
5988
6008
|
return Promise.resolve().then(
|
|
5989
|
-
() =>
|
|
6009
|
+
() => u(f ? d(f, l) : null)
|
|
5990
6010
|
), () => {
|
|
5991
6011
|
};
|
|
5992
6012
|
}
|
|
5993
6013
|
};
|
|
5994
6014
|
}
|
|
5995
6015
|
};
|
|
5996
|
-
return
|
|
6016
|
+
return p;
|
|
5997
6017
|
}
|
|
5998
6018
|
/**
|
|
5999
6019
|
* Implementa SQL in-memory para testes — preferência: `@electric-sql/pglite`
|
|
@@ -6057,22 +6077,22 @@ var qe = Object.freeze({
|
|
|
6057
6077
|
if (r.length > 0)
|
|
6058
6078
|
for (let f of r)
|
|
6059
6079
|
s.public.none(f);
|
|
6060
|
-
let
|
|
6061
|
-
|
|
6080
|
+
let d = s.adapters.createPg().Pool, p = new d(), l = p.query.bind(p);
|
|
6081
|
+
p.query = (f, ...m) => {
|
|
6062
6082
|
if (f !== null && typeof f == "object") {
|
|
6063
6083
|
let y = f.types;
|
|
6064
6084
|
y !== null && typeof y == "object" && "getTypeParser" in y && delete y.getTypeParser;
|
|
6065
6085
|
}
|
|
6066
|
-
return
|
|
6086
|
+
return l(f, ...m);
|
|
6067
6087
|
};
|
|
6068
|
-
let
|
|
6088
|
+
let u = i.drizzle(p, { schema: e });
|
|
6069
6089
|
return {
|
|
6070
|
-
orm:
|
|
6090
|
+
orm: u,
|
|
6071
6091
|
async transaction(f, m) {
|
|
6072
|
-
return await
|
|
6092
|
+
return await u.transaction(f);
|
|
6073
6093
|
},
|
|
6074
6094
|
async close() {
|
|
6075
|
-
let f =
|
|
6095
|
+
let f = p;
|
|
6076
6096
|
f.end && await f.end();
|
|
6077
6097
|
}
|
|
6078
6098
|
};
|
|
@@ -6154,7 +6174,7 @@ var qe = Object.freeze({
|
|
|
6154
6174
|
|
|
6155
6175
|
// src/auth-pkce-shared.ts
|
|
6156
6176
|
C();
|
|
6157
|
-
function
|
|
6177
|
+
function Bn() {
|
|
6158
6178
|
let n = globalThis.crypto;
|
|
6159
6179
|
if (!n || typeof n.getRandomValues != "function" || !n.subtle)
|
|
6160
6180
|
throw new exports.NeetruError(
|
|
@@ -6163,23 +6183,23 @@ function Vn() {
|
|
|
6163
6183
|
);
|
|
6164
6184
|
return n;
|
|
6165
6185
|
}
|
|
6166
|
-
function
|
|
6186
|
+
function Kn(n) {
|
|
6167
6187
|
let e = "";
|
|
6168
6188
|
for (let r of n) e += String.fromCharCode(r);
|
|
6169
6189
|
return (typeof btoa == "function" ? btoa(e) : Buffer.from(e, "binary").toString("base64")).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
6170
6190
|
}
|
|
6171
6191
|
function Fe(n, e) {
|
|
6172
6192
|
let t = new Uint8Array(e);
|
|
6173
|
-
return n.getRandomValues(t),
|
|
6193
|
+
return n.getRandomValues(t), Kn(t);
|
|
6174
6194
|
}
|
|
6175
|
-
async function
|
|
6195
|
+
async function zn(n, e) {
|
|
6176
6196
|
let t = new TextEncoder().encode(e), r = await n.subtle.digest("SHA-256", t);
|
|
6177
|
-
return
|
|
6197
|
+
return Kn(new Uint8Array(r));
|
|
6178
6198
|
}
|
|
6179
6199
|
function me(n) {
|
|
6180
6200
|
return (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? n.replace(/^https?:\/\/api\./, "https://auth.");
|
|
6181
6201
|
}
|
|
6182
|
-
function
|
|
6202
|
+
function Hn(n) {
|
|
6183
6203
|
let e = n.oidcClientId;
|
|
6184
6204
|
if (!e && n.apiKey && (e = gt(n.apiKey).keyId), !e)
|
|
6185
6205
|
throw new exports.NeetruError(
|
|
@@ -6188,7 +6208,7 @@ function zn(n) {
|
|
|
6188
6208
|
);
|
|
6189
6209
|
return e;
|
|
6190
6210
|
}
|
|
6191
|
-
function
|
|
6211
|
+
function Gn(n) {
|
|
6192
6212
|
let e = new URL("/api/v1/oauth/authorize", n.idpOrigin);
|
|
6193
6213
|
return e.searchParams.set("response_type", "code"), e.searchParams.set("client_id", n.clientId), e.searchParams.set("redirect_uri", n.redirectUri), e.searchParams.set("scope", n.scope), e.searchParams.set("state", n.state), e.searchParams.set("nonce", n.nonce), e.searchParams.set("code_challenge", n.codeChallenge), e.searchParams.set("code_challenge_method", "S256"), e;
|
|
6194
6214
|
}
|
|
@@ -6216,27 +6236,27 @@ function he(n) {
|
|
|
6216
6236
|
...e
|
|
6217
6237
|
};
|
|
6218
6238
|
}
|
|
6219
|
-
var
|
|
6220
|
-
async function
|
|
6221
|
-
let o = `${e}${
|
|
6239
|
+
var Ho = "/api/v1/oauth/revoke";
|
|
6240
|
+
async function Qn(n, e, t, r) {
|
|
6241
|
+
let o = `${e}${Ho}`, i = true;
|
|
6222
6242
|
for (let s of r) {
|
|
6223
6243
|
if (!s.token) continue;
|
|
6224
6244
|
let a = new URLSearchParams({
|
|
6225
6245
|
token: s.token,
|
|
6226
6246
|
token_type_hint: s.hint,
|
|
6227
6247
|
client_id: t
|
|
6228
|
-
}).toString(),
|
|
6229
|
-
for (let
|
|
6248
|
+
}).toString(), d = false;
|
|
6249
|
+
for (let p = 0; p < 2 && !d; p++)
|
|
6230
6250
|
try {
|
|
6231
|
-
if (
|
|
6251
|
+
if (d = (await n(o, {
|
|
6232
6252
|
method: "POST",
|
|
6233
6253
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
6234
6254
|
body: a
|
|
6235
|
-
})).ok, !
|
|
6255
|
+
})).ok, !d) break;
|
|
6236
6256
|
} catch {
|
|
6237
|
-
|
|
6257
|
+
p === 0 && await new Promise((l) => setTimeout(l, 500));
|
|
6238
6258
|
}
|
|
6239
|
-
|
|
6259
|
+
d || (i = false);
|
|
6240
6260
|
}
|
|
6241
6261
|
return i;
|
|
6242
6262
|
}
|
|
@@ -6245,14 +6265,14 @@ async function Gn(n, e, t, r) {
|
|
|
6245
6265
|
be();
|
|
6246
6266
|
|
|
6247
6267
|
// src/end-user-token.ts
|
|
6248
|
-
var
|
|
6249
|
-
function
|
|
6250
|
-
|
|
6268
|
+
var Go = /* @__PURE__ */ new WeakMap();
|
|
6269
|
+
function Jn(n, e) {
|
|
6270
|
+
Go.set(n, e);
|
|
6251
6271
|
}
|
|
6252
6272
|
|
|
6253
6273
|
// src/unsupported-db.ts
|
|
6254
6274
|
C();
|
|
6255
|
-
var
|
|
6275
|
+
var Qo = "[Neetru SDK] client.db n\xE3o est\xE1 dispon\xEDvel em React Native. O app n\xE3o acessa o banco do produto diretamente: chame a API do SEU produto (ex.: https://<produto>.neetru.com/api/v1) com o access_token do usu\xE1rio (Authorization: Bearer) \u2014 o backend do produto usa client.db no servidor. (At\xE9 a 3.7.x o db ca\xEDa em mem\xF3ria em sil\xEAncio e os dados sumiam ao fechar o app.)", Jo = /* @__PURE__ */ new Set([
|
|
6256
6276
|
"then",
|
|
6257
6277
|
"toJSON",
|
|
6258
6278
|
"valueOf",
|
|
@@ -6261,13 +6281,13 @@ var zo = "[Neetru SDK] client.db n\xE3o est\xE1 dispon\xEDvel em React Native. O
|
|
|
6261
6281
|
"asymmetricMatch",
|
|
6262
6282
|
"nodeType"
|
|
6263
6283
|
]);
|
|
6264
|
-
function
|
|
6284
|
+
function Xn() {
|
|
6265
6285
|
let n = () => {
|
|
6266
|
-
throw new exports.NeetruError("runtime_unsupported",
|
|
6286
|
+
throw new exports.NeetruError("runtime_unsupported", Qo, void 0, void 0, "react-native");
|
|
6267
6287
|
}, e = {};
|
|
6268
6288
|
return new Proxy(e, {
|
|
6269
6289
|
get(t, r) {
|
|
6270
|
-
if (!(typeof r == "symbol" ||
|
|
6290
|
+
if (!(typeof r == "symbol" || Jo.has(r)))
|
|
6271
6291
|
return r === "toString" ? () => "[NeetruDb indispon\xEDvel em React Native]" : n;
|
|
6272
6292
|
},
|
|
6273
6293
|
set() {
|
|
@@ -6277,7 +6297,7 @@ function Jn() {
|
|
|
6277
6297
|
}
|
|
6278
6298
|
|
|
6279
6299
|
// src/auth.ts
|
|
6280
|
-
function
|
|
6300
|
+
function Yo() {
|
|
6281
6301
|
try {
|
|
6282
6302
|
let e = globalThis.process?.env?.NEETRU_API_KEY;
|
|
6283
6303
|
return typeof e == "string" && e.length > 0 ? e : void 0;
|
|
@@ -6285,7 +6305,7 @@ function Qo() {
|
|
|
6285
6305
|
return;
|
|
6286
6306
|
}
|
|
6287
6307
|
}
|
|
6288
|
-
function
|
|
6308
|
+
function Zo(n) {
|
|
6289
6309
|
if (n) return n;
|
|
6290
6310
|
try {
|
|
6291
6311
|
let t = globalThis.process?.env?.NEETRU_ENV;
|
|
@@ -6299,8 +6319,8 @@ function Jo(n) {
|
|
|
6299
6319
|
}
|
|
6300
6320
|
return "prod";
|
|
6301
6321
|
}
|
|
6302
|
-
var
|
|
6303
|
-
function
|
|
6322
|
+
var ei = ye;
|
|
6323
|
+
function ti(n) {
|
|
6304
6324
|
let e = n.sub ?? "", t = typeof n.email == "string" ? n.email : "";
|
|
6305
6325
|
return {
|
|
6306
6326
|
uid: e,
|
|
@@ -6327,7 +6347,7 @@ function ut(n) {
|
|
|
6327
6347
|
} catch {
|
|
6328
6348
|
}
|
|
6329
6349
|
}
|
|
6330
|
-
function
|
|
6350
|
+
function ni(n) {
|
|
6331
6351
|
let e = "neetru_id_token", t = "neetru_refresh_token", r = /* @__PURE__ */ new Set(), o = null, i = null, s = null;
|
|
6332
6352
|
function a() {
|
|
6333
6353
|
try {
|
|
@@ -6336,17 +6356,17 @@ function Zo(n) {
|
|
|
6336
6356
|
return null;
|
|
6337
6357
|
}
|
|
6338
6358
|
}
|
|
6339
|
-
function
|
|
6359
|
+
function d() {
|
|
6340
6360
|
try {
|
|
6341
6361
|
return typeof globalThis.sessionStorage < "u" ? globalThis.sessionStorage : null;
|
|
6342
6362
|
} catch {
|
|
6343
6363
|
return null;
|
|
6344
6364
|
}
|
|
6345
6365
|
}
|
|
6346
|
-
function
|
|
6366
|
+
function p() {
|
|
6347
6367
|
if (o) {
|
|
6348
6368
|
let b = o.exp;
|
|
6349
|
-
return typeof b == "number" && b * 1e3 < Date.now() ? (o = null, a()?.removeItem(e),
|
|
6369
|
+
return typeof b == "number" && b * 1e3 < Date.now() ? (o = null, a()?.removeItem(e), l(), y(), null) : o;
|
|
6350
6370
|
}
|
|
6351
6371
|
let g = a();
|
|
6352
6372
|
if (!g) return null;
|
|
@@ -6356,14 +6376,14 @@ function Zo(n) {
|
|
|
6356
6376
|
let T = he(_);
|
|
6357
6377
|
return T ? (o = T, I(T), o) : (g.removeItem(e), y(), null);
|
|
6358
6378
|
}
|
|
6359
|
-
function
|
|
6379
|
+
function l() {
|
|
6360
6380
|
for (let g of r)
|
|
6361
6381
|
try {
|
|
6362
6382
|
g(o);
|
|
6363
6383
|
} catch {
|
|
6364
6384
|
}
|
|
6365
6385
|
}
|
|
6366
|
-
function
|
|
6386
|
+
function u() {
|
|
6367
6387
|
i !== null && (clearTimeout(i), i = null);
|
|
6368
6388
|
}
|
|
6369
6389
|
function f() {
|
|
@@ -6372,7 +6392,7 @@ function Zo(n) {
|
|
|
6372
6392
|
function m() {
|
|
6373
6393
|
o = null;
|
|
6374
6394
|
let g = a();
|
|
6375
|
-
g?.removeItem(e), g?.removeItem(t),
|
|
6395
|
+
g?.removeItem(e), g?.removeItem(t), l();
|
|
6376
6396
|
}
|
|
6377
6397
|
function h() {
|
|
6378
6398
|
return s || (s = (async () => {
|
|
@@ -6412,7 +6432,7 @@ function Zo(n) {
|
|
|
6412
6432
|
let P = a();
|
|
6413
6433
|
P?.setItem(e, A);
|
|
6414
6434
|
let M = D?.refresh_token;
|
|
6415
|
-
return typeof M == "string" && M.length > 0 && P?.setItem(t, M), o = L, I(L),
|
|
6435
|
+
return typeof M == "string" && M.length > 0 && P?.setItem(t, M), o = L, I(L), l(), true;
|
|
6416
6436
|
})().finally(() => {
|
|
6417
6437
|
s = null;
|
|
6418
6438
|
}), s);
|
|
@@ -6423,7 +6443,7 @@ function Zo(n) {
|
|
|
6423
6443
|
});
|
|
6424
6444
|
}
|
|
6425
6445
|
function I(g) {
|
|
6426
|
-
|
|
6446
|
+
u();
|
|
6427
6447
|
let _ = g.exp;
|
|
6428
6448
|
if (typeof _ != "number") return;
|
|
6429
6449
|
let T = _ * 1e3 - Date.now();
|
|
@@ -6438,12 +6458,12 @@ function Zo(n) {
|
|
|
6438
6458
|
return;
|
|
6439
6459
|
}
|
|
6440
6460
|
i = setTimeout(() => {
|
|
6441
|
-
i = null,
|
|
6461
|
+
i = null, p();
|
|
6442
6462
|
}, T + 1);
|
|
6443
6463
|
}
|
|
6444
6464
|
}
|
|
6445
6465
|
function w() {
|
|
6446
|
-
return
|
|
6466
|
+
return Hn(n);
|
|
6447
6467
|
}
|
|
6448
6468
|
async function k(g) {
|
|
6449
6469
|
let _ = g ?? (typeof globalThis.location < "u" ? globalThis.location.href : void 0);
|
|
@@ -6457,11 +6477,11 @@ function Zo(n) {
|
|
|
6457
6477
|
let b = T.searchParams, E = b.get("error");
|
|
6458
6478
|
if (E) {
|
|
6459
6479
|
let M = b.get("error_description") ?? E;
|
|
6460
|
-
throw
|
|
6480
|
+
throw d()?.removeItem(ge), ut(T), new exports.NeetruError(E, `OIDC authorize falhou: ${M}`);
|
|
6461
6481
|
}
|
|
6462
6482
|
let x = b.get("code");
|
|
6463
6483
|
if (!x) return null;
|
|
6464
|
-
let D = b.get("state"), A =
|
|
6484
|
+
let D = b.get("state"), A = d(), L = A?.getItem(ge) ?? null, P = null;
|
|
6465
6485
|
if (L)
|
|
6466
6486
|
try {
|
|
6467
6487
|
P = JSON.parse(L);
|
|
@@ -6550,12 +6570,12 @@ function Zo(n) {
|
|
|
6550
6570
|
let F = a();
|
|
6551
6571
|
F?.setItem(e, L);
|
|
6552
6572
|
let j = A?.refresh_token;
|
|
6553
|
-
return typeof j == "string" && j.length > 0 && F?.setItem(t, j), o = M, I(M),
|
|
6573
|
+
return typeof j == "string" && j.length > 0 && F?.setItem(t, j), o = M, I(M), l(), { user: M, postLoginRedirect: _.postLoginRedirect };
|
|
6554
6574
|
}
|
|
6555
6575
|
return {
|
|
6556
6576
|
async signIn(g) {
|
|
6557
6577
|
if (typeof globalThis.location < "u" && typeof globalThis.location.assign == "function") {
|
|
6558
|
-
let _ =
|
|
6578
|
+
let _ = Bn(), T = g?.redirectUri ?? globalThis.location.origin, b = g?.scope ?? "openid profile email", E = w(), x = Fe(_, 16), D = Fe(_, 16), A = Fe(_, 32), L = await zn(_, A), P = d();
|
|
6559
6579
|
if (!P)
|
|
6560
6580
|
throw new exports.NeetruError(
|
|
6561
6581
|
"runtime_unsupported",
|
|
@@ -6569,7 +6589,7 @@ function Zo(n) {
|
|
|
6569
6589
|
postLoginRedirect: g?.postLoginRedirect
|
|
6570
6590
|
};
|
|
6571
6591
|
P.setItem(ge, JSON.stringify(M));
|
|
6572
|
-
let F = me(n.baseUrl), j =
|
|
6592
|
+
let F = me(n.baseUrl), j = Gn({
|
|
6573
6593
|
idpOrigin: F,
|
|
6574
6594
|
clientId: E,
|
|
6575
6595
|
redirectUri: T,
|
|
@@ -6597,28 +6617,28 @@ function Zo(n) {
|
|
|
6597
6617
|
},
|
|
6598
6618
|
async signOut() {
|
|
6599
6619
|
let g = a(), _ = g?.getItem(t) ?? null;
|
|
6600
|
-
if (g && (g.removeItem(e), g.removeItem(t)),
|
|
6620
|
+
if (g && (g.removeItem(e), g.removeItem(t)), u(), o = null, _) {
|
|
6601
6621
|
let T = null;
|
|
6602
6622
|
try {
|
|
6603
6623
|
T = w();
|
|
6604
6624
|
} catch {
|
|
6605
6625
|
T = null;
|
|
6606
6626
|
}
|
|
6607
|
-
!(T && await
|
|
6627
|
+
!(T && await Qn(n.fetch, me(n.baseUrl), T, [
|
|
6608
6628
|
{ token: _, hint: "refresh_token" }
|
|
6609
6629
|
])) && typeof console < "u" && console.warn(
|
|
6610
6630
|
"[neetru-sdk] signOut: a revoga\xE7\xE3o do refresh_token no servidor n\xE3o foi confirmada. A sess\xE3o local foi encerrada, mas o refresh_token pode seguir v\xE1lido at\xE9 expirar."
|
|
6611
6631
|
);
|
|
6612
6632
|
}
|
|
6613
|
-
|
|
6633
|
+
l();
|
|
6614
6634
|
},
|
|
6615
6635
|
getUser() {
|
|
6616
|
-
return
|
|
6636
|
+
return p();
|
|
6617
6637
|
},
|
|
6618
6638
|
onAuthStateChanged(g) {
|
|
6619
6639
|
r.add(g);
|
|
6620
6640
|
try {
|
|
6621
|
-
g(
|
|
6641
|
+
g(p());
|
|
6622
6642
|
} catch {
|
|
6623
6643
|
}
|
|
6624
6644
|
return () => {
|
|
@@ -6634,14 +6654,14 @@ function Zo(n) {
|
|
|
6634
6654
|
"invalid_config",
|
|
6635
6655
|
"verifyToken requires an audience: provide oidcClientId/apiKey on the client or pass options.audience. Calling jwtVerify without an audience allows tokens issued for other OIDC clients to pass."
|
|
6636
6656
|
);
|
|
6637
|
-
let M =
|
|
6657
|
+
let M = ei(x, D);
|
|
6638
6658
|
try {
|
|
6639
6659
|
let { payload: F } = await jose.jwtVerify(g, M, {
|
|
6640
6660
|
issuer: E,
|
|
6641
6661
|
audience: P,
|
|
6642
6662
|
algorithms: ["RS256"]
|
|
6643
6663
|
});
|
|
6644
|
-
return
|
|
6664
|
+
return ti(F);
|
|
6645
6665
|
} catch (F) {
|
|
6646
6666
|
let j = F?.code ?? F?.name ?? "", U = F?.message ?? String(F);
|
|
6647
6667
|
throw j === "ERR_JWT_EXPIRED" ? new exports.NeetruError("token_expired", `Token expired: ${U}`) : j === "ERR_JWS_SIGNATURE_VERIFICATION_FAILED" ? new exports.NeetruError("token_invalid_signature", `Token signature invalid: ${U}`) : j === "ERR_JWT_CLAIM_VALIDATION_FAILED" ? U.includes('"aud"') || U.includes("audience") ? new exports.NeetruError("token_invalid_audience", `Token audience mismatch: ${U}`) : U.includes('"iss"') || U.includes("issuer") ? new exports.NeetruError("token_invalid_issuer", `Token issuer mismatch: ${U}`) : new exports.NeetruError("token_invalid_signature", `Token claim validation failed: ${U}`) : new exports.NeetruError("token_invalid_signature", `Token verification failed: ${U}`);
|
|
@@ -6649,16 +6669,16 @@ function Zo(n) {
|
|
|
6649
6669
|
}
|
|
6650
6670
|
};
|
|
6651
6671
|
}
|
|
6652
|
-
function
|
|
6672
|
+
function Yn(n = {}) {
|
|
6653
6673
|
let e = n.fetch ?? globalThis.fetch;
|
|
6654
6674
|
if (typeof e != "function")
|
|
6655
6675
|
throw new exports.NeetruError(
|
|
6656
6676
|
"invalid_config",
|
|
6657
6677
|
"fetch is not available in this runtime. Pass `fetch` explicitly to createNeetruClient."
|
|
6658
6678
|
);
|
|
6659
|
-
let t = n.apiKey ??
|
|
6679
|
+
let t = n.apiKey ?? Yo();
|
|
6660
6680
|
At({ hasApiKey: !!t, strict: n.strictServerOnlyKey === true });
|
|
6661
|
-
let r =
|
|
6681
|
+
let r = Zo(n.env), o = n.baseUrl ?? (r === "dev-local" ? wt : je), i = vt(o, n.allowedHosts ?? []), s = Object.freeze({
|
|
6662
6682
|
apiKey: t,
|
|
6663
6683
|
oidcClientId: n.oidcClientId,
|
|
6664
6684
|
baseUrl: i,
|
|
@@ -6666,33 +6686,33 @@ function Xn(n = {}) {
|
|
|
6666
6686
|
env: r,
|
|
6667
6687
|
productId: n.productId,
|
|
6668
6688
|
tenantId: n.tenantId
|
|
6669
|
-
}), a = r === "dev" && !Me(),
|
|
6670
|
-
getEndUserToken: () =>
|
|
6689
|
+
}), a = r === "dev" && !Me(), d = n.mocks?.auth ?? (a ? new le({ ...qe }) : ni(s)), p = n.mocks?.usage ?? (a ? new de() : Vt(s)), l = n.endUserAuth ?? d, u = n.mocks?.support ?? (a ? new ue() : Qt(s, {
|
|
6690
|
+
getEndUserToken: () => l.getIdToken(),
|
|
6671
6691
|
// `refreshTokens` é opcional (MockAuth/mocks próprios omitem).
|
|
6672
6692
|
refreshEndUserToken: (() => {
|
|
6673
|
-
let k =
|
|
6674
|
-
return typeof k == "function" ? () => k.call(
|
|
6693
|
+
let k = l.refreshTokens;
|
|
6694
|
+
return typeof k == "function" ? () => k.call(l) : void 0;
|
|
6675
6695
|
})()
|
|
6676
|
-
})), f = n.mocks?.entitlements ?? (a ? new pe() : qt()), m = n.mocks?.db ?? (Q() === "react-native" ?
|
|
6696
|
+
})), f = n.mocks?.entitlements ?? (a ? new pe() : qt()), m = n.mocks?.db ?? (Q() === "react-native" ? Xn() : nt(s, n.db)), h = n.mocks?.webhooks ?? (a ? new ae() : it(s)), y = n.mocks?.notifications ?? (a ? new ce() : at(s)), I = n.mocks?.ai ?? (a ? new fe() : ct(s)), w = Object.freeze({
|
|
6677
6697
|
config: s,
|
|
6678
|
-
auth:
|
|
6698
|
+
auth: d,
|
|
6679
6699
|
catalog: Ut(s),
|
|
6680
6700
|
entitlements: f,
|
|
6681
6701
|
telemetry: Wt(s),
|
|
6682
|
-
usage:
|
|
6683
|
-
support:
|
|
6702
|
+
usage: p,
|
|
6703
|
+
support: u,
|
|
6684
6704
|
db: m,
|
|
6685
6705
|
checkout: ot(s),
|
|
6686
6706
|
webhooks: h,
|
|
6687
6707
|
notifications: y,
|
|
6688
6708
|
ai: I
|
|
6689
6709
|
});
|
|
6690
|
-
return n.endUserAuth &&
|
|
6710
|
+
return n.endUserAuth && Jn(w, n.endUserAuth), w;
|
|
6691
6711
|
}
|
|
6692
6712
|
var pt = /* @__PURE__ */ Symbol.for("@neetru/sdk:serverClient");
|
|
6693
|
-
function
|
|
6713
|
+
function ri(n = {}) {
|
|
6694
6714
|
let e = globalThis;
|
|
6695
|
-
return e[pt] || (e[pt] =
|
|
6715
|
+
return e[pt] || (e[pt] = Yn(n)), e[pt];
|
|
6696
6716
|
}
|
|
6697
6717
|
|
|
6698
6718
|
// src/index.ts
|
|
@@ -6702,8 +6722,8 @@ q();
|
|
|
6702
6722
|
|
|
6703
6723
|
// src/access-token.ts
|
|
6704
6724
|
C();
|
|
6705
|
-
var
|
|
6706
|
-
function
|
|
6725
|
+
var er = "https://auth.neetru.com", ii = "/api/v1/oauth/.well-known/jwks";
|
|
6726
|
+
function tr(n) {
|
|
6707
6727
|
let e = (Array.isArray(n) ? n : [n]).filter(
|
|
6708
6728
|
(t) => typeof t == "string" && t.trim().length > 0
|
|
6709
6729
|
);
|
|
@@ -6714,17 +6734,17 @@ function er(n) {
|
|
|
6714
6734
|
);
|
|
6715
6735
|
return e;
|
|
6716
6736
|
}
|
|
6717
|
-
var
|
|
6737
|
+
var Zn = "O token apresentado n\xE3o \xE9 um access_token (parece um id_token). Mande o access_token no Authorization.";
|
|
6718
6738
|
function K(n, e) {
|
|
6719
6739
|
return new exports.NeetruError("token_invalid", n, 401, void 0, e);
|
|
6720
6740
|
}
|
|
6721
|
-
async function
|
|
6722
|
-
let t =
|
|
6741
|
+
async function nr(n, e) {
|
|
6742
|
+
let t = tr(e?.audience);
|
|
6723
6743
|
if (typeof n != "string" || n.length === 0)
|
|
6724
6744
|
throw K("access_token ausente.", "missing_token");
|
|
6725
6745
|
if (n.split(".").length !== 3)
|
|
6726
6746
|
throw K("O token n\xE3o \xE9 um JWT (access_token OIDC esperado).", "not_a_jwt");
|
|
6727
|
-
let r = (e.issuer ??
|
|
6747
|
+
let r = (e.issuer ?? er).replace(/\/+$/, ""), o = e.jwksUrl ?? `${r}${ii}`, i = Math.min(Math.max(e.clockToleranceSec ?? 5, 0), 60), s = ye(o, e.jwksCacheTtlMs ?? ee), a;
|
|
6728
6748
|
try {
|
|
6729
6749
|
({ payload: a } = await jose.jwtVerify(n, s, {
|
|
6730
6750
|
issuer: r,
|
|
@@ -6745,7 +6765,7 @@ async function tr(n, e) {
|
|
|
6745
6765
|
401,
|
|
6746
6766
|
void 0,
|
|
6747
6767
|
"audience"
|
|
6748
|
-
) : _ === "iss" ? new exports.NeetruError("token_invalid_issuer", "access_token de outro emissor.", 401, void 0, "issuer") : _ === "jti" ? K(
|
|
6768
|
+
) : _ === "iss" ? new exports.NeetruError("token_invalid_issuer", "access_token de outro emissor.", 401, void 0, "issuer") : _ === "jti" ? K(Zn, "not_access_token") : K(`Claim obrigat\xF3rio ausente ou inv\xE1lido no access_token (${_ ?? "desconhecido"}).`, "claims");
|
|
6749
6769
|
}
|
|
6750
6770
|
throw k === "ERR_JWS_SIGNATURE_VERIFICATION_FAILED" || k === "ERR_JWKS_NO_MATCHING_KEY" || k === "ERR_JOSE_ALG_NOT_ALLOWED" || k === "ERR_JWKS_MULTIPLE_MATCHING_KEYS" ? new exports.NeetruError(
|
|
6751
6771
|
"token_invalid_signature",
|
|
@@ -6755,11 +6775,11 @@ async function tr(n, e) {
|
|
|
6755
6775
|
"signature"
|
|
6756
6776
|
) : k === "ERR_JWKS_TIMEOUT" || k === "ERR_JOSE_GENERIC" && /JSON Web Key Set/i.test(N) || k === "" && w instanceof TypeError ? new exports.NeetruError("network_error", `Falha ao obter o JWKS do Core: ${N}`, void 0, void 0, "jwks") : K(`access_token inv\xE1lido: ${N}`, "malformed");
|
|
6757
6777
|
}
|
|
6758
|
-
let
|
|
6759
|
-
if (typeof
|
|
6760
|
-
throw K(
|
|
6761
|
-
let
|
|
6762
|
-
if (typeof f != "string" || f !==
|
|
6778
|
+
let d = a.jti, p = a.scope, l = a.client_id;
|
|
6779
|
+
if (typeof d != "string" || d.length === 0 || typeof p != "string" || typeof l != "string")
|
|
6780
|
+
throw K(Zn, "not_access_token");
|
|
6781
|
+
let u = a.aud, f = typeof u == "string" ? u : Array.isArray(u) && u.length === 1 ? u[0] : void 0;
|
|
6782
|
+
if (typeof f != "string" || f !== l)
|
|
6763
6783
|
throw K("access_token inconsistente: client_id difere do aud.", "client_id_mismatch");
|
|
6764
6784
|
let m = typeof a.sub == "string" ? a.sub : "", h = m.startsWith("client:");
|
|
6765
6785
|
if (!m || h && e.allowClientCredentials !== true)
|
|
@@ -6767,7 +6787,7 @@ async function tr(n, e) {
|
|
|
6767
6787
|
"access_token de m\xE1quina (client_credentials) n\xE3o representa um usu\xE1rio final.",
|
|
6768
6788
|
"client_credentials"
|
|
6769
6789
|
);
|
|
6770
|
-
let y =
|
|
6790
|
+
let y = p.split(" ").filter(Boolean), I = (e.requiredScopes ?? []).filter((w) => !y.includes(w));
|
|
6771
6791
|
if (I.length > 0)
|
|
6772
6792
|
throw new exports.NeetruError(
|
|
6773
6793
|
"insufficient_scope",
|
|
@@ -6778,17 +6798,17 @@ async function tr(n, e) {
|
|
|
6778
6798
|
);
|
|
6779
6799
|
return {
|
|
6780
6800
|
sub: m,
|
|
6781
|
-
clientId:
|
|
6801
|
+
clientId: l,
|
|
6782
6802
|
audience: f,
|
|
6783
6803
|
scopes: y,
|
|
6784
|
-
jti:
|
|
6804
|
+
jti: d,
|
|
6785
6805
|
issuedAt: a.iat,
|
|
6786
6806
|
expiresAt: a.exp,
|
|
6787
6807
|
isClientCredentials: h,
|
|
6788
6808
|
claims: a
|
|
6789
6809
|
};
|
|
6790
6810
|
}
|
|
6791
|
-
function
|
|
6811
|
+
function rr(n, e) {
|
|
6792
6812
|
let t = e.toLowerCase(), r = n && typeof n == "object" && "headers" in n && n.headers ? n.headers : n;
|
|
6793
6813
|
if (!r || typeof r != "object") return null;
|
|
6794
6814
|
if (typeof r.get == "function")
|
|
@@ -6798,14 +6818,14 @@ function nr(n, e) {
|
|
|
6798
6818
|
return Array.isArray(i) ? typeof i[0] == "string" ? i[0] : null : typeof i == "string" ? i : null;
|
|
6799
6819
|
return null;
|
|
6800
6820
|
}
|
|
6801
|
-
function
|
|
6802
|
-
let e =
|
|
6821
|
+
function or(n) {
|
|
6822
|
+
let e = rr(n, "authorization");
|
|
6803
6823
|
if (!e) return null;
|
|
6804
6824
|
let t = /^\s*Bearer\s+(\S+)\s*$/i.exec(e);
|
|
6805
6825
|
return t ? t[1] : null;
|
|
6806
6826
|
}
|
|
6807
|
-
function
|
|
6808
|
-
let t =
|
|
6827
|
+
function ir(n, e) {
|
|
6828
|
+
let t = rr(n, "cookie");
|
|
6809
6829
|
if (!t) return null;
|
|
6810
6830
|
for (let r of t.split(";")) {
|
|
6811
6831
|
let o = r.indexOf("=");
|
|
@@ -6819,13 +6839,13 @@ function or(n, e) {
|
|
|
6819
6839
|
}
|
|
6820
6840
|
return null;
|
|
6821
6841
|
}
|
|
6822
|
-
async function
|
|
6823
|
-
|
|
6824
|
-
let t =
|
|
6842
|
+
async function si(n, e) {
|
|
6843
|
+
tr(e?.audience);
|
|
6844
|
+
let t = or(n);
|
|
6825
6845
|
if (t)
|
|
6826
|
-
return { kind: "access_token", token: await
|
|
6846
|
+
return { kind: "access_token", token: await nr(t, e) };
|
|
6827
6847
|
if (e.cookie) {
|
|
6828
|
-
let r =
|
|
6848
|
+
let r = ir(n, e.cookie.name);
|
|
6829
6849
|
if (r) {
|
|
6830
6850
|
let o = await e.cookie.verify(r);
|
|
6831
6851
|
if (o != null) return { kind: "cookie", session: o };
|
|
@@ -6833,7 +6853,7 @@ async function ri(n, e) {
|
|
|
6833
6853
|
}
|
|
6834
6854
|
return null;
|
|
6835
6855
|
}
|
|
6836
|
-
function
|
|
6856
|
+
function ai(n) {
|
|
6837
6857
|
if (n instanceof exports.NeetruError) {
|
|
6838
6858
|
if (n.code === "insufficient_scope")
|
|
6839
6859
|
return {
|
|
@@ -6866,7 +6886,7 @@ function oi(n) {
|
|
|
6866
6886
|
|
|
6867
6887
|
// src/index.ts
|
|
6868
6888
|
be();
|
|
6869
|
-
var
|
|
6889
|
+
var Da = "3.8.1";
|
|
6870
6890
|
|
|
6871
6891
|
exports.DEFAULT_BASE_URL = je;
|
|
6872
6892
|
exports.DEV_FIXTURE_USER = qe;
|
|
@@ -6879,27 +6899,27 @@ exports.MockNotifications = ce;
|
|
|
6879
6899
|
exports.MockSupport = ue;
|
|
6880
6900
|
exports.MockUsage = de;
|
|
6881
6901
|
exports.MockWebhooks = ae;
|
|
6882
|
-
exports.NEETRU_OIDC_ISSUER =
|
|
6902
|
+
exports.NEETRU_OIDC_ISSUER = er;
|
|
6883
6903
|
exports.NEETRU_SENTINEL_KEY = ze;
|
|
6884
|
-
exports.VERSION =
|
|
6885
|
-
exports.accessTokenErrorToHttp =
|
|
6886
|
-
exports.authenticateProductRequest =
|
|
6904
|
+
exports.VERSION = Da;
|
|
6905
|
+
exports.accessTokenErrorToHttp = ai;
|
|
6906
|
+
exports.authenticateProductRequest = si;
|
|
6887
6907
|
exports.checkCoreVersionCompat = te;
|
|
6888
6908
|
exports.createAiNamespace = ct;
|
|
6889
6909
|
exports.createCheckoutNamespace = ot;
|
|
6890
|
-
exports.createNeetruClient =
|
|
6910
|
+
exports.createNeetruClient = Yn;
|
|
6891
6911
|
exports.createNeetruDb = nt;
|
|
6892
6912
|
exports.createNotificationsNamespace = at;
|
|
6893
6913
|
exports.createWebhooksNamespace = it;
|
|
6894
6914
|
exports.detectNonServerRuntime = Q;
|
|
6895
|
-
exports.getServerNeetruClient =
|
|
6896
|
-
exports.increment =
|
|
6915
|
+
exports.getServerNeetruClient = ri;
|
|
6916
|
+
exports.increment = zr;
|
|
6897
6917
|
exports.isFieldSentinel = H;
|
|
6898
6918
|
exports.isIncrementSentinel = Yt;
|
|
6899
6919
|
exports.isServerTimestampSentinel = Xt;
|
|
6900
|
-
exports.readBearerToken =
|
|
6901
|
-
exports.readCookie =
|
|
6902
|
-
exports.serverTimestamp =
|
|
6903
|
-
exports.verifyAccessToken =
|
|
6904
|
-
exports.verifyWebhookRequest =
|
|
6905
|
-
exports.verifyWebhookSignature =
|
|
6920
|
+
exports.readBearerToken = or;
|
|
6921
|
+
exports.readCookie = ir;
|
|
6922
|
+
exports.serverTimestamp = Kr;
|
|
6923
|
+
exports.verifyAccessToken = nr;
|
|
6924
|
+
exports.verifyWebhookRequest = Po;
|
|
6925
|
+
exports.verifyWebhookSignature = bn;
|