@neetru/sdk 3.7.0 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/README.md +66 -1
- package/dist/ai.cjs +221 -177
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +221 -177
- package/dist/auth.cjs +1227 -1064
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +1224 -1061
- package/dist/catalog.cjs +164 -97
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +164 -97
- package/dist/checkout.cjs +182 -115
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +181 -114
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db.cjs +370 -298
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +362 -290
- package/dist/entitlements.cjs +17 -10
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/entitlements.mjs +17 -10
- package/dist/errors.cjs +15 -8
- package/dist/errors.d.cts +24 -3
- package/dist/errors.d.ts +24 -3
- package/dist/errors.mjs +14 -7
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/index.cjs +1409 -1075
- package/dist/index.d.cts +177 -3
- package/dist/index.d.ts +177 -3
- package/dist/index.mjs +1544 -1217
- package/dist/mobile.cjs +400 -181
- package/dist/mobile.d.cts +59 -4
- package/dist/mobile.d.ts +59 -4
- package/dist/mobile.mjs +400 -181
- package/dist/mocks.cjs +9 -2
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.mjs +9 -2
- package/dist/notifications.cjs +197 -130
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +196 -129
- package/dist/react.cjs +303 -282
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +295 -274
- package/dist/support.cjs +299 -229
- package/dist/support.d.cts +8 -4
- package/dist/support.d.ts +8 -4
- package/dist/support.mjs +298 -228
- package/dist/telemetry.cjs +234 -167
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +234 -167
- package/dist/{types-Bx4kTLmk.d.cts → types-D-9bjlM0.d.cts} +48 -1
- package/dist/{types-Dx0IrOSF.d.ts → types-bVztFQTI.d.ts} +48 -1
- package/dist/usage.cjs +183 -116
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +183 -116
- package/dist/webhooks.cjs +215 -148
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +212 -145
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
var jose = require('jose');
|
|
4
4
|
var idb = require('idb');
|
|
5
5
|
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
6
|
+
var ir = Object.defineProperty;
|
|
7
|
+
var W = (n, e) => () => (n && (e = n(n = 0)), e);
|
|
8
|
+
var ft = (n, e) => {
|
|
9
9
|
for (var t in e)
|
|
10
|
-
|
|
10
|
+
ir(n, t, { get: e[t], enumerable: true });
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
// src/errors.ts
|
|
14
|
-
exports.NeetruError = void 0; exports.NeetruAuthRequiredError = void 0; var
|
|
14
|
+
exports.NeetruError = void 0; exports.NeetruAuthRequiredError = void 0; var C = W(() => {
|
|
15
15
|
exports.NeetruError = class n extends Error {
|
|
16
16
|
code;
|
|
17
17
|
status;
|
|
@@ -25,18 +25,25 @@ exports.NeetruError = void 0; exports.NeetruAuthRequiredError = void 0; var P =
|
|
|
25
25
|
* para um `NeetruDbErrorCode` mais específico (recuperável vs não).
|
|
26
26
|
*/
|
|
27
27
|
reason;
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
/**
|
|
29
|
+
* 3.8.0: `x-correlation-id` da requisição que falhou. O SDK gera um UUID por
|
|
30
|
+
* chamada (ou usa o que o chamador passou) e manda no header; o Core grava o
|
|
31
|
+
* mesmo id nos logs/auditoria — cite-o ao abrir um ticket de suporte.
|
|
32
|
+
* `undefined` em erros que acontecem antes de qualquer requisição.
|
|
33
|
+
*/
|
|
34
|
+
correlationId;
|
|
35
|
+
constructor(e, t, r, o, i, s) {
|
|
36
|
+
super(t), this.name = "NeetruError", this.code = e, this.status = r, this.requestId = o, this.reason = i, this.correlationId = s, Object.setPrototypeOf(this, n.prototype);
|
|
30
37
|
}
|
|
31
38
|
}, exports.NeetruAuthRequiredError = class n extends exports.NeetruError {
|
|
32
|
-
constructor(e, t, r, o) {
|
|
33
|
-
super("auth_required", e, r, o, t), this.name = "NeetruAuthRequiredError", Object.setPrototypeOf(this, n.prototype);
|
|
39
|
+
constructor(e, t, r, o, i) {
|
|
40
|
+
super("auth_required", e, r, o, t, i), this.name = "NeetruAuthRequiredError", Object.setPrototypeOf(this, n.prototype);
|
|
34
41
|
}
|
|
35
42
|
};
|
|
36
43
|
});
|
|
37
44
|
|
|
38
45
|
// src/idempotency.ts
|
|
39
|
-
function
|
|
46
|
+
function kt() {
|
|
40
47
|
let n = typeof globalThis < "u" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
41
48
|
if (n && typeof n.randomUUID == "function")
|
|
42
49
|
return n.randomUUID();
|
|
@@ -45,80 +52,164 @@ function mt() {
|
|
|
45
52
|
"crypto.randomUUID not available in this runtime. SDK 3.0 requires Node \u226520 or modern browser/Edge."
|
|
46
53
|
);
|
|
47
54
|
}
|
|
48
|
-
var
|
|
49
|
-
|
|
55
|
+
var St = W(() => {
|
|
56
|
+
C();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// src/correlation.ts
|
|
60
|
+
function Rt(n) {
|
|
61
|
+
return typeof n == "string" && n.length > 0 && n.length <= 128 && pr.test(n);
|
|
62
|
+
}
|
|
63
|
+
function fr() {
|
|
64
|
+
let n = globalThis.crypto;
|
|
65
|
+
try {
|
|
66
|
+
if (n && typeof n.randomUUID == "function") return n.randomUUID();
|
|
67
|
+
} catch {
|
|
68
|
+
}
|
|
69
|
+
let e = new Uint8Array(16), t = false;
|
|
70
|
+
try {
|
|
71
|
+
n && typeof n.getRandomValues == "function" && (n.getRandomValues(e), t = !0);
|
|
72
|
+
} catch {
|
|
73
|
+
t = false;
|
|
74
|
+
}
|
|
75
|
+
if (!t)
|
|
76
|
+
for (let o = 0; o < e.length; o++) e[o] = Math.floor(Math.random() * 256);
|
|
77
|
+
e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128;
|
|
78
|
+
let r = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join("");
|
|
79
|
+
return `${r.slice(0, 8)}-${r.slice(8, 12)}-${r.slice(12, 16)}-${r.slice(16, 20)}-${r.slice(20)}`;
|
|
80
|
+
}
|
|
81
|
+
function _e(n, e) {
|
|
82
|
+
let t, r = {};
|
|
83
|
+
for (let [i, s] of Object.entries(n)) {
|
|
84
|
+
if (i.toLowerCase() === Tt) {
|
|
85
|
+
Rt(s) && (t = s);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
r[i] = s;
|
|
89
|
+
}
|
|
90
|
+
let o = t ?? (Rt(e) ? e : void 0) ?? fr();
|
|
91
|
+
return r[Tt] = o, { headers: r, correlationId: o };
|
|
92
|
+
}
|
|
93
|
+
var Tt, pr, We = W(() => {
|
|
94
|
+
Tt = "x-correlation-id", pr = /^[A-Za-z0-9._:\-]+$/;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// src/server-only-key.ts
|
|
98
|
+
function Q() {
|
|
99
|
+
try {
|
|
100
|
+
let n = globalThis.navigator;
|
|
101
|
+
if (n && n.product === "ReactNative") return "react-native";
|
|
102
|
+
} catch {
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
let n = globalThis;
|
|
106
|
+
if (typeof n.window < "u" && typeof n.document < "u") return "browser";
|
|
107
|
+
} catch {
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
function It(n, e) {
|
|
112
|
+
let t = mr[n];
|
|
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
|
+
}
|
|
115
|
+
function At(n) {
|
|
116
|
+
if (!n.hasApiKey) return;
|
|
117
|
+
let e = n.runtime === void 0 ? Q() : n.runtime;
|
|
118
|
+
if (e) {
|
|
119
|
+
if (n.strict)
|
|
120
|
+
throw new exports.NeetruError("server_only_key", It(e, true), void 0, void 0, e);
|
|
121
|
+
if (!Ct) {
|
|
122
|
+
Ct = true;
|
|
123
|
+
try {
|
|
124
|
+
typeof console < "u" && typeof console.warn == "function" && console.warn(It(e, !1));
|
|
125
|
+
} catch {
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
var mr, Ct, be = W(() => {
|
|
131
|
+
C();
|
|
132
|
+
mr = {
|
|
133
|
+
browser: "no navegador",
|
|
134
|
+
"react-native": "num app React Native"
|
|
135
|
+
};
|
|
136
|
+
Ct = false;
|
|
50
137
|
});
|
|
51
138
|
|
|
52
139
|
// src/http.ts
|
|
53
|
-
var
|
|
54
|
-
|
|
55
|
-
checkCoreVersionCompat: () =>
|
|
56
|
-
httpRequest: () =>
|
|
57
|
-
statusToCode: () =>
|
|
140
|
+
var ne = {};
|
|
141
|
+
ft(ne, {
|
|
142
|
+
checkCoreVersionCompat: () => te,
|
|
143
|
+
httpRequest: () => S,
|
|
144
|
+
statusToCode: () => ve
|
|
58
145
|
});
|
|
59
|
-
function
|
|
146
|
+
function $e(n) {
|
|
60
147
|
return n.split("+")[0] ?? n;
|
|
61
148
|
}
|
|
62
|
-
function
|
|
63
|
-
let t =
|
|
149
|
+
function gr(n, e) {
|
|
150
|
+
let t = $e(n).split(".").map(Number), r = $e(e).split(".").map(Number), o = Math.max(t.length, r.length);
|
|
64
151
|
for (let i = 0; i < o; i++) {
|
|
65
152
|
let s = t[i] ?? 0, a = r[i] ?? 0;
|
|
66
153
|
if (s !== a) return s - a;
|
|
67
154
|
}
|
|
68
155
|
return 0;
|
|
69
156
|
}
|
|
70
|
-
function
|
|
157
|
+
function Ot() {
|
|
71
158
|
try {
|
|
72
|
-
return "3.
|
|
159
|
+
return "3.8.0";
|
|
73
160
|
} catch {
|
|
74
161
|
return;
|
|
75
162
|
}
|
|
76
163
|
}
|
|
77
|
-
function
|
|
78
|
-
let n =
|
|
164
|
+
function yr() {
|
|
165
|
+
let n = Ot();
|
|
79
166
|
return n ? parseInt(n.split(".")[0] ?? "3", 10) : 3;
|
|
80
167
|
}
|
|
81
|
-
function
|
|
168
|
+
function te(n) {
|
|
82
169
|
if (!n) return;
|
|
83
|
-
let e =
|
|
170
|
+
let e = yr(), t = hr[e];
|
|
84
171
|
if (!t) return;
|
|
85
|
-
let r =
|
|
86
|
-
if (
|
|
172
|
+
let r = $e(n);
|
|
173
|
+
if (gr(r, t) < 0) {
|
|
87
174
|
let o = `${e}:${r}`;
|
|
88
|
-
|
|
175
|
+
Et.has(o) || (Et.add(o), console.warn(
|
|
89
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.`
|
|
90
177
|
));
|
|
91
178
|
}
|
|
92
179
|
}
|
|
93
|
-
function
|
|
180
|
+
function _r() {
|
|
181
|
+
let n = "This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var.";
|
|
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
|
+
}
|
|
184
|
+
function Pt(n) {
|
|
94
185
|
let e = 200 * Math.pow(4, n), t = e * 0.2 * (Math.random() * 2 - 1);
|
|
95
186
|
return Math.max(50, Math.round(e + t));
|
|
96
187
|
}
|
|
97
|
-
function
|
|
188
|
+
function Sr(n) {
|
|
98
189
|
if (!n) return null;
|
|
99
190
|
let e = Number(n);
|
|
100
|
-
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3),
|
|
191
|
+
if (Number.isFinite(e) && e >= 0) return Math.min(Math.round(e * 1e3), Dt);
|
|
101
192
|
let t = Date.parse(n);
|
|
102
193
|
if (Number.isFinite(t)) {
|
|
103
194
|
let r = t - Date.now();
|
|
104
|
-
if (r > 0) return Math.min(r,
|
|
195
|
+
if (r > 0) return Math.min(r, Dt);
|
|
105
196
|
}
|
|
106
197
|
return null;
|
|
107
198
|
}
|
|
108
|
-
function
|
|
199
|
+
function Nt(n) {
|
|
109
200
|
return new Promise((e) => setTimeout(e, n));
|
|
110
201
|
}
|
|
111
|
-
function
|
|
202
|
+
function ve(n) {
|
|
112
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";
|
|
113
204
|
}
|
|
114
|
-
function
|
|
205
|
+
function Tr(n, e, t) {
|
|
115
206
|
let r = n.replace(/\/+$/, ""), o = e.startsWith("/") ? e : `/${e}`, i = new URL(`${r}${o}`);
|
|
116
207
|
if (t)
|
|
117
208
|
for (let [s, a] of Object.entries(t))
|
|
118
209
|
a !== void 0 && i.searchParams.set(s, String(a));
|
|
119
210
|
return i.toString();
|
|
120
211
|
}
|
|
121
|
-
async function
|
|
212
|
+
async function xt(n) {
|
|
122
213
|
let e = await n.text();
|
|
123
214
|
if (e)
|
|
124
215
|
try {
|
|
@@ -127,94 +218,100 @@ async function vt(n) {
|
|
|
127
218
|
return;
|
|
128
219
|
}
|
|
129
220
|
}
|
|
130
|
-
async function
|
|
131
|
-
let t = e.method ?? "GET", r =
|
|
132
|
-
accept: "application/json",
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (
|
|
221
|
+
async function S(n, e) {
|
|
222
|
+
let t = e.method ?? "GET", r = Tr(n.baseUrl, e.path, e.query), o = wr.has(t) ? br : vr, i = e.retries ?? o, { headers: s, correlationId: a } = _e(
|
|
223
|
+
{ accept: "application/json", ...e.headers },
|
|
224
|
+
e.correlationId
|
|
225
|
+
), c = Ot();
|
|
226
|
+
if (c && (s["x-neetru-sdk-version"] = c), e.requireAuth) {
|
|
136
227
|
if (!n.apiKey)
|
|
137
|
-
throw new exports.NeetruError(
|
|
138
|
-
"missing_api_key",
|
|
139
|
-
"This operation requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
140
|
-
);
|
|
228
|
+
throw new exports.NeetruError("missing_api_key", _r());
|
|
141
229
|
s.authorization = `Bearer ${n.apiKey}`;
|
|
142
230
|
}
|
|
143
231
|
if (e.idempotencyKey) {
|
|
144
|
-
let
|
|
145
|
-
s["idempotency-key"] =
|
|
146
|
-
}
|
|
147
|
-
let
|
|
148
|
-
|
|
149
|
-
let
|
|
150
|
-
for (let
|
|
151
|
-
if (
|
|
152
|
-
throw
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
232
|
+
let f = typeof e.idempotencyKey == "string" ? e.idempotencyKey : kt();
|
|
233
|
+
s["idempotency-key"] = f;
|
|
234
|
+
}
|
|
235
|
+
let u = e.body !== void 0 && t !== "GET" && t !== "DELETE" ? JSON.stringify(e.body) : void 0;
|
|
236
|
+
u !== void 0 && (s["content-type"] = "application/json");
|
|
237
|
+
let d = null, p = e.deadlineMs != null ? Date.now() + e.deadlineMs : null;
|
|
238
|
+
for (let f = 0; f <= i; f++) {
|
|
239
|
+
if (p != null && Date.now() >= p)
|
|
240
|
+
throw d ?? new exports.NeetruError(
|
|
241
|
+
"network_error",
|
|
242
|
+
"Deadline excedido antes de completar a request",
|
|
243
|
+
void 0,
|
|
244
|
+
void 0,
|
|
245
|
+
void 0,
|
|
246
|
+
a
|
|
247
|
+
);
|
|
248
|
+
let m = { method: t, headers: s };
|
|
249
|
+
u !== void 0 && (m.body = u);
|
|
250
|
+
let h = p != null ? Math.max(1, Math.min(3e4, p - Date.now())) : 3e4;
|
|
251
|
+
m.signal = AbortSignal.timeout(h);
|
|
252
|
+
let y;
|
|
158
253
|
try {
|
|
159
|
-
|
|
160
|
-
} catch (
|
|
161
|
-
let
|
|
162
|
-
if (
|
|
163
|
-
let
|
|
164
|
-
if (
|
|
165
|
-
await
|
|
254
|
+
y = await n.fetch(r, m);
|
|
255
|
+
} catch (b) {
|
|
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 (d = new exports.NeetruError("network_error", E, void 0, void 0, void 0, a), f < i) {
|
|
258
|
+
let x = Pt(f);
|
|
259
|
+
if (p == null || Date.now() + x < p) {
|
|
260
|
+
await Nt(x);
|
|
166
261
|
continue;
|
|
167
262
|
}
|
|
168
263
|
}
|
|
169
|
-
throw
|
|
264
|
+
throw d;
|
|
170
265
|
}
|
|
171
|
-
let
|
|
172
|
-
if (
|
|
173
|
-
let
|
|
174
|
-
return
|
|
266
|
+
let I = y.headers.get("x-request-id") ?? y.headers.get("x-correlation-id") ?? void 0;
|
|
267
|
+
if (y.ok) {
|
|
268
|
+
let b = y.headers.get("x-neetru-version");
|
|
269
|
+
return b && te(b), await xt(y);
|
|
175
270
|
}
|
|
176
|
-
let w = await
|
|
271
|
+
let w = await xt(y), k = ve(y.status), N = `HTTP ${y.status}`, g;
|
|
177
272
|
if (w && typeof w == "object") {
|
|
178
|
-
let
|
|
179
|
-
typeof
|
|
273
|
+
let b = w.error;
|
|
274
|
+
typeof b == "string" ? N = b : b && typeof b == "object" ? (typeof b.code == "string" && (k = b.code), typeof b.message == "string" && (N = b.message), typeof b.reason == "string" && (g = b.reason)) : (typeof w.code == "string" && (k = w.code), typeof w.message == "string" && (N = w.message)), typeof w.reason == "string" && (g = w.reason);
|
|
180
275
|
}
|
|
181
|
-
if (
|
|
182
|
-
let
|
|
183
|
-
|
|
276
|
+
if (y.status === 410) {
|
|
277
|
+
let b = y.headers.get("x-neetru-replacement");
|
|
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}.`);
|
|
184
279
|
}
|
|
185
|
-
let
|
|
186
|
-
if (
|
|
187
|
-
let
|
|
188
|
-
if (
|
|
189
|
-
await
|
|
280
|
+
let _ = new exports.NeetruError(k, N, y.status, I, g, a);
|
|
281
|
+
if (d = _, kr.has(k) && f < i) {
|
|
282
|
+
let E = Sr(y.headers.get("retry-after")) ?? Pt(f);
|
|
283
|
+
if (p == null || Date.now() + E < p) {
|
|
284
|
+
await Nt(E);
|
|
190
285
|
continue;
|
|
191
286
|
}
|
|
192
287
|
}
|
|
193
|
-
throw
|
|
288
|
+
throw _;
|
|
194
289
|
}
|
|
195
|
-
throw
|
|
290
|
+
throw d ?? new exports.NeetruError("unknown", "unexpected httpRequest exit", void 0, void 0, void 0, a);
|
|
196
291
|
}
|
|
197
|
-
var
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
292
|
+
var hr, Et, br, vr, wr, kr, Dt, q = W(() => {
|
|
293
|
+
C();
|
|
294
|
+
St();
|
|
295
|
+
We();
|
|
296
|
+
be();
|
|
297
|
+
hr = {
|
|
201
298
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
202
299
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
203
300
|
3: "2026.05.1"
|
|
204
301
|
};
|
|
205
|
-
|
|
206
|
-
|
|
302
|
+
Et = /* @__PURE__ */ new Set();
|
|
303
|
+
br = 2, vr = 0, wr = /* @__PURE__ */ new Set(["GET", "HEAD"]), kr = /* @__PURE__ */ new Set([
|
|
207
304
|
"rate_limited",
|
|
208
305
|
"server_error",
|
|
209
306
|
"network_error"
|
|
210
307
|
]);
|
|
211
|
-
|
|
308
|
+
Dt = 6e4;
|
|
212
309
|
});
|
|
213
310
|
|
|
214
311
|
// src/db-errors.ts
|
|
215
|
-
var
|
|
216
|
-
|
|
217
|
-
|
|
312
|
+
var Mr; exports.NeetruDbError = void 0; var z = W(() => {
|
|
313
|
+
C();
|
|
314
|
+
Mr = /* @__PURE__ */ new Set([
|
|
218
315
|
"db_unavailable",
|
|
219
316
|
"db_conflict",
|
|
220
317
|
"db_timeout"
|
|
@@ -232,16 +329,16 @@ var dr; exports.NeetruDbError = void 0; var z = B(() => {
|
|
|
232
329
|
*/
|
|
233
330
|
dbId;
|
|
234
331
|
constructor(e, t, r) {
|
|
235
|
-
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable =
|
|
332
|
+
super(e, t), this.name = "NeetruDbError", this.code = e, this.retryable = Mr.has(e), this.dbId = r, Object.setPrototypeOf(this, n.prototype);
|
|
236
333
|
}
|
|
237
334
|
};
|
|
238
335
|
});
|
|
239
336
|
|
|
240
337
|
// src/db/sql/resilient-pool.ts
|
|
241
|
-
function
|
|
242
|
-
return typeof n == "object" && n !== null && n[
|
|
338
|
+
function De(n) {
|
|
339
|
+
return typeof n == "object" && n !== null && n[Xe] === true;
|
|
243
340
|
}
|
|
244
|
-
function
|
|
341
|
+
function io(n) {
|
|
245
342
|
let e = n && typeof n == "object" && "code" in n ? n.code : void 0;
|
|
246
343
|
if (e === "ECONNREFUSED" || e === "ETIMEDOUT" || e === "ENOTFOUND" || e === "EHOSTUNREACH" || e === "EAI_AGAIN")
|
|
247
344
|
return true;
|
|
@@ -253,56 +350,56 @@ function Nr(n) {
|
|
|
253
350
|
let r = (n instanceof Error ? n.message : typeof n == "string" ? n : "").toLowerCase();
|
|
254
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"));
|
|
255
352
|
}
|
|
256
|
-
function
|
|
353
|
+
function Ye(n) {
|
|
257
354
|
let e = { current: n }, t = null, r = {
|
|
258
|
-
__neetruSwapInner(
|
|
355
|
+
__neetruSwapInner(c) {
|
|
259
356
|
let u = e.current;
|
|
260
|
-
return e.current =
|
|
357
|
+
return e.current = c, u;
|
|
261
358
|
},
|
|
262
359
|
__neetruGetInner() {
|
|
263
360
|
return e.current;
|
|
264
361
|
},
|
|
265
|
-
__neetruSetReLease(
|
|
266
|
-
t =
|
|
362
|
+
__neetruSetReLease(c) {
|
|
363
|
+
t = c;
|
|
267
364
|
}
|
|
268
|
-
}, o = (
|
|
365
|
+
}, o = (c, u) => c.query(...u), i = (...c) => c.length > 0 && typeof c[c.length - 1] == "function" ? o(e.current, c) : (async () => {
|
|
269
366
|
try {
|
|
270
|
-
return await o(e.current,
|
|
367
|
+
return await o(e.current, c);
|
|
271
368
|
} catch (u) {
|
|
272
|
-
let
|
|
273
|
-
if (
|
|
369
|
+
let d = t;
|
|
370
|
+
if (d === null || !io(u))
|
|
274
371
|
throw u;
|
|
275
372
|
try {
|
|
276
|
-
await
|
|
373
|
+
await d();
|
|
277
374
|
} catch {
|
|
278
375
|
throw u;
|
|
279
376
|
}
|
|
280
|
-
return await o(e.current,
|
|
377
|
+
return await o(e.current, c);
|
|
281
378
|
}
|
|
282
379
|
})(), s = Object.create(Object.getPrototypeOf(n));
|
|
283
380
|
return new Proxy(s, {
|
|
284
|
-
get(
|
|
285
|
-
if (u ===
|
|
381
|
+
get(c, u) {
|
|
382
|
+
if (u === Xe) return true;
|
|
286
383
|
if (u === "__neetruSwapInner") return r.__neetruSwapInner;
|
|
287
384
|
if (u === "__neetruGetInner") return r.__neetruGetInner;
|
|
288
385
|
if (u === "__neetruSetReLease") return r.__neetruSetReLease;
|
|
289
386
|
if (u === "query") return i;
|
|
290
387
|
if (u === "constructor")
|
|
291
388
|
return e.current.constructor;
|
|
292
|
-
let
|
|
293
|
-
return typeof p == "function" ? p.bind(
|
|
389
|
+
let d = e.current, p = Reflect.get(d, u, d);
|
|
390
|
+
return typeof p == "function" ? p.bind(d) : p;
|
|
294
391
|
}
|
|
295
392
|
});
|
|
296
393
|
}
|
|
297
|
-
var
|
|
298
|
-
|
|
394
|
+
var Xe, Ze = W(() => {
|
|
395
|
+
Xe = /* @__PURE__ */ Symbol.for("neetru.sdk.resilientPool");
|
|
299
396
|
});
|
|
300
397
|
|
|
301
398
|
// src/db/sql/lease.ts
|
|
302
|
-
function
|
|
399
|
+
function ao(n, e) {
|
|
303
400
|
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;
|
|
304
401
|
}
|
|
305
|
-
function
|
|
402
|
+
function co(n) {
|
|
306
403
|
if (n instanceof exports.NeetruDbError) return n;
|
|
307
404
|
let e = n instanceof Error ? n.message : String(n), t = n instanceof Error ? n.code : void 0;
|
|
308
405
|
return typeof t == "string" && (t === "ECONNREFUSED" || t === "ENOTFOUND" || t === "ETIMEDOUT") ? new exports.NeetruDbError(
|
|
@@ -310,10 +407,10 @@ function Lr(n) {
|
|
|
310
407
|
`Pool connection failed (${t}): ${e}`
|
|
311
408
|
) : 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}`);
|
|
312
409
|
}
|
|
313
|
-
var
|
|
410
|
+
var so, Z, se, rn = W(() => {
|
|
314
411
|
z();
|
|
315
|
-
|
|
316
|
-
|
|
412
|
+
Ze();
|
|
413
|
+
so = 0.8, Z = 3e4, se = class {
|
|
317
414
|
/**
|
|
318
415
|
* Lease atual. `null` no modo LAZY antes da 1ª query (o 1º lease é adiado —
|
|
319
416
|
* ver `_ensureInitialLease`). Após a 1ª aquisição, sempre não-nulo.
|
|
@@ -353,7 +450,7 @@ var xr, X, re, Bt = B(() => {
|
|
|
353
450
|
isolationLevel: t.isolationLevel
|
|
354
451
|
}) : await this._orm.transaction(e);
|
|
355
452
|
} catch (r) {
|
|
356
|
-
throw
|
|
453
|
+
throw co(r);
|
|
357
454
|
}
|
|
358
455
|
}
|
|
359
456
|
async close() {
|
|
@@ -371,13 +468,13 @@ var xr, X, re, Bt = B(() => {
|
|
|
371
468
|
* usa `MIN_RENEWAL_DELAY_MS` como fallback seguro.
|
|
372
469
|
*/
|
|
373
470
|
_calcRenewalDelayMs() {
|
|
374
|
-
if (!this._lease) return
|
|
471
|
+
if (!this._lease) return Z;
|
|
375
472
|
let e = Date.parse(this._lease.expiresAt);
|
|
376
|
-
if (!Number.isFinite(e)) return
|
|
473
|
+
if (!Number.isFinite(e)) return Z;
|
|
377
474
|
let t = this._deps.now(), r = e - t;
|
|
378
|
-
if (r <= 0) return
|
|
379
|
-
let o = r * (1 -
|
|
380
|
-
return Math.max(
|
|
475
|
+
if (r <= 0) return Z;
|
|
476
|
+
let o = r * (1 - so);
|
|
477
|
+
return Math.max(Z, Math.round(o));
|
|
381
478
|
}
|
|
382
479
|
_scheduleRenewal() {
|
|
383
480
|
if (this._closed) return;
|
|
@@ -404,8 +501,8 @@ var xr, X, re, Bt = B(() => {
|
|
|
404
501
|
} catch (t) {
|
|
405
502
|
this._renewalTimer = setTimeout(() => {
|
|
406
503
|
this._doRenew();
|
|
407
|
-
},
|
|
408
|
-
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${
|
|
504
|
+
}, Z), console.warn(
|
|
505
|
+
`[SqlLeaseManager] renova\xE7\xE3o E emiss\xE3o de lease novo falharam; re-tentando em ${Z}ms (cert TLS ef\xEAmero do servidor expira ~15min ap\xF3s o \xFAltimo swap):`,
|
|
409
506
|
t instanceof Error ? t.message : t
|
|
410
507
|
);
|
|
411
508
|
return;
|
|
@@ -478,7 +575,7 @@ var xr, X, re, Bt = B(() => {
|
|
|
478
575
|
"SqlLeaseManager foi fechado \u2014 aquisi\xE7\xE3o de lease inicial abortada."
|
|
479
576
|
);
|
|
480
577
|
if (this._lease)
|
|
481
|
-
return
|
|
578
|
+
return De(this._pool) ? this._pool.__neetruGetInner() : this._pool;
|
|
482
579
|
if (this._initInFlight !== null) return this._initInFlight;
|
|
483
580
|
let e = (async () => {
|
|
484
581
|
let t;
|
|
@@ -496,7 +593,7 @@ var xr, X, re, Bt = B(() => {
|
|
|
496
593
|
let i = o instanceof Error ? o.message : String(o);
|
|
497
594
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao abrir pool: ${i}`);
|
|
498
595
|
}
|
|
499
|
-
return
|
|
596
|
+
return De(this._pool) ? this._pool.__neetruSwapInner(r).end().catch(() => {
|
|
500
597
|
}) : this._pool = r, this._lease = t, this._scheduleRenewal(), r;
|
|
501
598
|
})();
|
|
502
599
|
this._initInFlight = e;
|
|
@@ -512,12 +609,12 @@ var xr, X, re, Bt = B(() => {
|
|
|
512
609
|
* Exposto como método protegido para testes.
|
|
513
610
|
*/
|
|
514
611
|
async _swapPool(e) {
|
|
515
|
-
if (!
|
|
612
|
+
if (!ao(this._lease, e)) {
|
|
516
613
|
this._lease = e;
|
|
517
614
|
return;
|
|
518
615
|
}
|
|
519
616
|
let t = this._deps.createPool(e);
|
|
520
|
-
if (
|
|
617
|
+
if (De(this._pool)) {
|
|
521
618
|
let i = this._pool.__neetruSwapInner(t);
|
|
522
619
|
this._lease = e, i.end().catch(() => {
|
|
523
620
|
});
|
|
@@ -539,29 +636,29 @@ var xr, X, re, Bt = B(() => {
|
|
|
539
636
|
});
|
|
540
637
|
|
|
541
638
|
// src/db/sql/sql-client.ts
|
|
542
|
-
var
|
|
543
|
-
|
|
544
|
-
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () =>
|
|
545
|
-
DEFAULT_POOL_IDLE_MS: () =>
|
|
546
|
-
DEFAULT_POOL_MAX: () =>
|
|
547
|
-
LEASE_ENDPOINT: () =>
|
|
548
|
-
LEASE_RENEW_ENDPOINT: () =>
|
|
549
|
-
createDrizzleHandle: () =>
|
|
550
|
-
createHttpLeaseFetcher: () =>
|
|
551
|
-
createPgPool: () =>
|
|
552
|
-
createSqlClientFromConfig: () =>
|
|
553
|
-
createSqlClientWithDeps: () =>
|
|
554
|
-
resolvePoolTuning: () =>
|
|
639
|
+
var pn = {};
|
|
640
|
+
ft(pn, {
|
|
641
|
+
DEFAULT_POOL_CONNECT_TIMEOUT_MS: () => dn,
|
|
642
|
+
DEFAULT_POOL_IDLE_MS: () => ln,
|
|
643
|
+
DEFAULT_POOL_MAX: () => cn,
|
|
644
|
+
LEASE_ENDPOINT: () => on,
|
|
645
|
+
LEASE_RENEW_ENDPOINT: () => sn,
|
|
646
|
+
createDrizzleHandle: () => fo,
|
|
647
|
+
createHttpLeaseFetcher: () => an,
|
|
648
|
+
createPgPool: () => po,
|
|
649
|
+
createSqlClientFromConfig: () => ho,
|
|
650
|
+
createSqlClientWithDeps: () => un,
|
|
651
|
+
resolvePoolTuning: () => tt
|
|
555
652
|
});
|
|
556
|
-
function
|
|
653
|
+
function lo(n) {
|
|
557
654
|
return n === "workspace" ? "staging" : n === "prod" ? "production" : "dev-local";
|
|
558
655
|
}
|
|
559
|
-
function
|
|
656
|
+
function an(n) {
|
|
560
657
|
return async (e) => {
|
|
561
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
658
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (q(), ne)), r = !!e?.leaseId, o = r ? sn : on, i;
|
|
562
659
|
r ? i = { leaseId: e.leaseId } : (i = {
|
|
563
660
|
productId: n.productId ?? "",
|
|
564
|
-
environment:
|
|
661
|
+
environment: lo(n.env ?? "prod")
|
|
565
662
|
}, e?.database !== void 0 && (i.database = e.database));
|
|
566
663
|
let s;
|
|
567
664
|
try {
|
|
@@ -574,23 +671,23 @@ function Ht(n) {
|
|
|
574
671
|
retries: 2
|
|
575
672
|
});
|
|
576
673
|
} catch (a) {
|
|
577
|
-
let
|
|
578
|
-
if (
|
|
579
|
-
let p =
|
|
674
|
+
let c = r ? "renovar" : "obter", u = a instanceof Error ? a.message : String(a), d = a instanceof exports.NeetruError ? a.reason : void 0;
|
|
675
|
+
if (d === "suspended" || d === "archived" || d === "revoked") {
|
|
676
|
+
let p = d === "suspended" ? "ambiente suspenso \u2014 a m\xE1quina que o servia parou; ser\xE1 religado quando o ambiente voltar" : d === "archived" ? "ambiente arquivado \u2014 este banco foi arquivado e n\xE3o aceita mais conex\xF5es" : "lease revogado \u2014 as credenciais deste ambiente foram revogadas pelo Core";
|
|
580
677
|
throw new exports.NeetruDbError(
|
|
581
678
|
"db_suspended",
|
|
582
|
-
`Falha ao ${
|
|
679
|
+
`Falha ao ${c} lease SQL: ${p} (reason=${d}).`
|
|
583
680
|
);
|
|
584
681
|
}
|
|
585
682
|
throw new exports.NeetruDbError(
|
|
586
683
|
"db_unavailable",
|
|
587
|
-
`Falha ao ${
|
|
684
|
+
`Falha ao ${c} lease SQL: ${u}`
|
|
588
685
|
);
|
|
589
686
|
}
|
|
590
|
-
return
|
|
687
|
+
return uo(s);
|
|
591
688
|
};
|
|
592
689
|
}
|
|
593
|
-
function
|
|
690
|
+
function uo(n) {
|
|
594
691
|
if (!n || typeof n != "object")
|
|
595
692
|
throw new exports.NeetruDbError("db_unavailable", "Resposta de lease inv\xE1lida (n\xE3o \xE9 objeto).");
|
|
596
693
|
let t = n.lease;
|
|
@@ -622,22 +719,22 @@ function Mr(n) {
|
|
|
622
719
|
expiresAt: o("expiresAt", "string")
|
|
623
720
|
};
|
|
624
721
|
}
|
|
625
|
-
function
|
|
722
|
+
function et(n, e, t, r) {
|
|
626
723
|
let o = typeof process < "u" ? process.env?.[n] : void 0;
|
|
627
724
|
if (o === void 0 || o.trim() === "") return e;
|
|
628
725
|
let i = Number.parseInt(o.trim(), 10);
|
|
629
726
|
return !Number.isFinite(i) || i < t || i > r ? e : i;
|
|
630
727
|
}
|
|
631
|
-
function
|
|
728
|
+
function tt() {
|
|
632
729
|
return {
|
|
633
730
|
// Teto 100: bem abaixo do `max_client_conn = 500` do PgBouncer, com folga
|
|
634
731
|
// para múltiplas instâncias do app dividirem o mesmo front-door.
|
|
635
|
-
max:
|
|
732
|
+
max: et("NEETRU_DB_POOL_MAX", cn, 1, 100),
|
|
636
733
|
// Teto 15min = janela do cert; piso 1s.
|
|
637
|
-
idleTimeoutMillis:
|
|
638
|
-
connectionTimeoutMillis:
|
|
734
|
+
idleTimeoutMillis: et("NEETRU_DB_POOL_IDLE_MS", ln, 1e3, 9e5),
|
|
735
|
+
connectionTimeoutMillis: et(
|
|
639
736
|
"NEETRU_DB_POOL_CONNECT_TIMEOUT_MS",
|
|
640
|
-
|
|
737
|
+
dn,
|
|
641
738
|
1e3,
|
|
642
739
|
12e4
|
|
643
740
|
),
|
|
@@ -645,7 +742,7 @@ function Qe() {
|
|
|
645
742
|
keepAliveInitialDelayMillis: 1e4
|
|
646
743
|
};
|
|
647
744
|
}
|
|
648
|
-
async function
|
|
745
|
+
async function po(n) {
|
|
649
746
|
let { Pool: e } = await import(
|
|
650
747
|
/* webpackIgnore: true */
|
|
651
748
|
'pg'
|
|
@@ -662,17 +759,17 @@ async function qr(n) {
|
|
|
662
759
|
user: n.user,
|
|
663
760
|
password: n.password,
|
|
664
761
|
ssl: t,
|
|
665
|
-
...
|
|
762
|
+
...tt()
|
|
666
763
|
});
|
|
667
764
|
}
|
|
668
|
-
async function
|
|
765
|
+
async function fo(n, e) {
|
|
669
766
|
let { drizzle: t } = await import(
|
|
670
767
|
/* webpackIgnore: true */
|
|
671
768
|
'drizzle-orm/node-postgres'
|
|
672
769
|
);
|
|
673
770
|
return t(n, { schema: e });
|
|
674
771
|
}
|
|
675
|
-
function
|
|
772
|
+
function mo(n, e) {
|
|
676
773
|
let t = null, r = null, o = () => t ? Promise.resolve(t) : (r || (r = e().then((s) => (t = s, r = null, s)).catch((s) => {
|
|
677
774
|
throw r = null, s;
|
|
678
775
|
})), r), i = {
|
|
@@ -688,52 +785,52 @@ function jr(n, e) {
|
|
|
688
785
|
};
|
|
689
786
|
return n && Object.setPrototypeOf(i, n), i;
|
|
690
787
|
}
|
|
691
|
-
async function
|
|
788
|
+
async function un(n, e) {
|
|
692
789
|
let t = e?.database !== void 0 ? { ...n, database: e.database } : n;
|
|
693
790
|
if (t.lazy) {
|
|
694
|
-
let
|
|
695
|
-
if (!
|
|
791
|
+
let c = null, u = mo(t.poolPrototype ?? null, () => {
|
|
792
|
+
if (!c)
|
|
696
793
|
throw new exports.NeetruDbError("db_unavailable", "SQL client n\xE3o inicializado (manager ausente).");
|
|
697
|
-
return
|
|
698
|
-
}),
|
|
794
|
+
return c._ensureInitialLease();
|
|
795
|
+
}), d = Ye(u), p;
|
|
699
796
|
try {
|
|
700
|
-
p = t.createDrizzle(
|
|
797
|
+
p = t.createDrizzle(d, t.schema);
|
|
701
798
|
} catch (m) {
|
|
702
799
|
let h = m instanceof Error ? m.message : String(m);
|
|
703
800
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${h}`);
|
|
704
801
|
}
|
|
705
|
-
let f = new
|
|
706
|
-
return
|
|
802
|
+
let f = new se(null, d, p, t);
|
|
803
|
+
return c = f, d.__neetruSetReLease(() => f._reactiveReLease()), f;
|
|
707
804
|
}
|
|
708
805
|
let r;
|
|
709
806
|
try {
|
|
710
807
|
e?.database !== void 0 ? r = await t.fetchLease({ database: e.database }) : r = await t.fetchLease();
|
|
711
|
-
} catch (
|
|
712
|
-
if (
|
|
713
|
-
let u =
|
|
808
|
+
} catch (c) {
|
|
809
|
+
if (c instanceof exports.NeetruDbError) throw c;
|
|
810
|
+
let u = c instanceof Error ? c.message : String(c);
|
|
714
811
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao obter lease inicial: ${u}`);
|
|
715
812
|
}
|
|
716
813
|
let o;
|
|
717
814
|
try {
|
|
718
815
|
o = t.createPool(r);
|
|
719
|
-
} catch (
|
|
720
|
-
let u =
|
|
816
|
+
} catch (c) {
|
|
817
|
+
let u = c instanceof Error ? c.message : String(c);
|
|
721
818
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao abrir pool: ${u}`);
|
|
722
819
|
}
|
|
723
|
-
let i =
|
|
820
|
+
let i = Ye(o), s;
|
|
724
821
|
try {
|
|
725
822
|
s = t.createDrizzle(i, t.schema);
|
|
726
|
-
} catch (
|
|
823
|
+
} catch (c) {
|
|
727
824
|
await o.end().catch(() => {
|
|
728
825
|
});
|
|
729
|
-
let u =
|
|
826
|
+
let u = c instanceof Error ? c.message : String(c);
|
|
730
827
|
throw new exports.NeetruDbError("db_unavailable", `Falha ao criar handle Drizzle: ${u}`);
|
|
731
828
|
}
|
|
732
|
-
let a = new
|
|
829
|
+
let a = new se(r, i, s, t);
|
|
733
830
|
return i.__neetruSetReLease(() => a._reactiveReLease()), a;
|
|
734
831
|
}
|
|
735
|
-
async function
|
|
736
|
-
let r =
|
|
832
|
+
async function ho(n, e, t) {
|
|
833
|
+
let r = an(n), o, i;
|
|
737
834
|
try {
|
|
738
835
|
o = await import(
|
|
739
836
|
/* webpackIgnore: true */
|
|
@@ -749,11 +846,11 @@ async function Wr(n, e, t) {
|
|
|
749
846
|
);
|
|
750
847
|
}
|
|
751
848
|
let s = o.Pool ?? o.default?.Pool;
|
|
752
|
-
return
|
|
849
|
+
return un(
|
|
753
850
|
{
|
|
754
851
|
fetchLease: r,
|
|
755
852
|
createPool: (u) => {
|
|
756
|
-
let
|
|
853
|
+
let d = u.sslca ? {
|
|
757
854
|
ca: u.sslca,
|
|
758
855
|
cert: u.clientCert ?? void 0,
|
|
759
856
|
key: u.clientKey ?? void 0,
|
|
@@ -765,13 +862,13 @@ async function Wr(n, e, t) {
|
|
|
765
862
|
database: u.dbName,
|
|
766
863
|
user: u.user,
|
|
767
864
|
password: u.password,
|
|
768
|
-
ssl:
|
|
865
|
+
ssl: d,
|
|
769
866
|
// Tuning centralizado (env-tunável) — ver resolvePoolTuning + auditoria
|
|
770
867
|
// docs/_review/AUDITORIA_DB_CONN_PERF_2026-09-06.md no Core.
|
|
771
|
-
...
|
|
868
|
+
...tt()
|
|
772
869
|
});
|
|
773
870
|
},
|
|
774
|
-
createDrizzle: (u,
|
|
871
|
+
createDrizzle: (u, d) => i.drizzle(u, { schema: d }),
|
|
775
872
|
schema: e,
|
|
776
873
|
// Lease inicial LAZY (motor de ambientes suspenso): `client.db.sql()` não
|
|
777
874
|
// pode explodir o `import` do módulo do produto quando o ambiente está
|
|
@@ -784,28 +881,36 @@ async function Wr(n, e, t) {
|
|
|
784
881
|
t
|
|
785
882
|
);
|
|
786
883
|
}
|
|
787
|
-
var
|
|
884
|
+
var on, sn, cn, ln, dn, fn = W(() => {
|
|
788
885
|
z();
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
886
|
+
C();
|
|
887
|
+
rn();
|
|
888
|
+
Ze();
|
|
889
|
+
on = "/api/sdk/v1/db/lease", sn = "/api/sdk/v1/db/lease/renew";
|
|
890
|
+
cn = 10, ln = 6e4, dn = 1e4;
|
|
794
891
|
});
|
|
892
|
+
var mt = /* @__PURE__ */ new Map(), ee = 3600 * 1e3;
|
|
893
|
+
function ye(n, e = ee) {
|
|
894
|
+
let t = Date.now(), r = mt.get(n);
|
|
895
|
+
if (r && t - r.createdAt < e)
|
|
896
|
+
return r.jwks;
|
|
897
|
+
let o = jose.createRemoteJWKSet(new URL(n));
|
|
898
|
+
return mt.set(n, { jwks: o, createdAt: t }), o;
|
|
899
|
+
}
|
|
795
900
|
|
|
796
901
|
// src/auth.ts
|
|
797
|
-
|
|
902
|
+
C();
|
|
798
903
|
|
|
799
904
|
// src/api-key.ts
|
|
800
|
-
|
|
801
|
-
var
|
|
802
|
-
function
|
|
905
|
+
C();
|
|
906
|
+
var ht = /^nrt_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/;
|
|
907
|
+
function gt(n) {
|
|
803
908
|
if (typeof n != "string" || n.length === 0)
|
|
804
909
|
throw new exports.NeetruError(
|
|
805
910
|
"invalid_config",
|
|
806
911
|
"apiKey is required (formato nrt_<keyId>_<secret>)"
|
|
807
912
|
);
|
|
808
|
-
let e =
|
|
913
|
+
let e = ht.exec(n);
|
|
809
914
|
if (!e)
|
|
810
915
|
throw new exports.NeetruError(
|
|
811
916
|
"invalid_config",
|
|
@@ -813,22 +918,22 @@ function ct(n) {
|
|
|
813
918
|
);
|
|
814
919
|
return { keyId: e[1], secret: e[2] };
|
|
815
920
|
}
|
|
816
|
-
function
|
|
921
|
+
function yt(n) {
|
|
817
922
|
if (typeof n != "string" || n.length === 0) return null;
|
|
818
|
-
let e =
|
|
923
|
+
let e = ht.exec(n);
|
|
819
924
|
return e ? { keyId: e[1], secret: e[2] } : null;
|
|
820
925
|
}
|
|
821
926
|
|
|
822
927
|
// src/base-url-allowlist.ts
|
|
823
|
-
|
|
824
|
-
var
|
|
825
|
-
function
|
|
826
|
-
return
|
|
928
|
+
C();
|
|
929
|
+
var _t = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]), ar = ".neetru.com", cr = "neetru.com", lr = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\.(test|localhost)$/;
|
|
930
|
+
function bt(n) {
|
|
931
|
+
return lr.test(n);
|
|
827
932
|
}
|
|
828
|
-
function
|
|
933
|
+
function dr(n) {
|
|
829
934
|
return n.toLowerCase().replace(/\.+$/, "");
|
|
830
935
|
}
|
|
831
|
-
function
|
|
936
|
+
function vt(n, e = []) {
|
|
832
937
|
if (typeof n != "string" || n.length === 0)
|
|
833
938
|
throw new exports.NeetruError("invalid_config", "baseUrl is required");
|
|
834
939
|
let t;
|
|
@@ -837,8 +942,8 @@ function pt(n, e = []) {
|
|
|
837
942
|
} catch {
|
|
838
943
|
throw new exports.NeetruError("invalid_config", `baseUrl is not a valid URL: ${n}`);
|
|
839
944
|
}
|
|
840
|
-
let r = t.protocol, o =
|
|
841
|
-
if (
|
|
945
|
+
let r = t.protocol, o = dr(t.hostname);
|
|
946
|
+
if (ur())
|
|
842
947
|
return typeof console < "u" && console.warn(
|
|
843
948
|
`[neetru-sdk] NEETRU_ALLOW_INSECURE_BASEURL=1 ativo. baseUrl=${n} aceito sem valida\xE7\xE3o.`
|
|
844
949
|
), n.replace(/\/+$/, "");
|
|
@@ -848,17 +953,17 @@ function pt(n, e = []) {
|
|
|
848
953
|
`baseUrl protocol must be http: or https:, got ${r}`
|
|
849
954
|
);
|
|
850
955
|
if (r === "http:") {
|
|
851
|
-
if (!(
|
|
956
|
+
if (!(_t.has(o) || bt(o)))
|
|
852
957
|
throw new exports.NeetruError(
|
|
853
958
|
"invalid_config",
|
|
854
959
|
`baseUrl with http: only allowed for localhost/127.0.0.1/*.test/*.localhost. Got: ${o}. Use https: or set NEETRU_ALLOW_INSECURE_BASEURL=1 to bypass.`
|
|
855
960
|
);
|
|
856
961
|
return n.replace(/\/+$/, "");
|
|
857
962
|
}
|
|
858
|
-
let i = o ===
|
|
859
|
-
let u =
|
|
963
|
+
let i = o === cr || o.endsWith(ar), s = e.some((c) => {
|
|
964
|
+
let u = c.toLowerCase();
|
|
860
965
|
return o === u || o.endsWith(`.${u}`);
|
|
861
|
-
}), a =
|
|
966
|
+
}), a = _t.has(o) || bt(o);
|
|
862
967
|
if (!i && !s && !a)
|
|
863
968
|
throw new exports.NeetruError(
|
|
864
969
|
"invalid_config",
|
|
@@ -866,7 +971,7 @@ function pt(n, e = []) {
|
|
|
866
971
|
);
|
|
867
972
|
return n.replace(/\/+$/, "");
|
|
868
973
|
}
|
|
869
|
-
function
|
|
974
|
+
function ur() {
|
|
870
975
|
try {
|
|
871
976
|
let e = globalThis.process?.env?.NEETRU_ALLOW_INSECURE_BASEURL;
|
|
872
977
|
if (e === "1" || e === "true") return !0;
|
|
@@ -881,12 +986,12 @@ function $n() {
|
|
|
881
986
|
}
|
|
882
987
|
|
|
883
988
|
// src/types.ts
|
|
884
|
-
var
|
|
989
|
+
var je = "https://api.neetru.com", wt = "http://localhost:9003";
|
|
885
990
|
|
|
886
991
|
// src/catalog.ts
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
function
|
|
992
|
+
C();
|
|
993
|
+
q();
|
|
994
|
+
function Lt(n) {
|
|
890
995
|
if (!n || typeof n != "object")
|
|
891
996
|
throw new exports.NeetruError("invalid_response", "Catalog response item is not an object");
|
|
892
997
|
let e = n;
|
|
@@ -905,14 +1010,14 @@ function kt(n) {
|
|
|
905
1010
|
return typeof r.amountCents == "number" && (o.amountCents = r.amountCents), Array.isArray(r.features) && r.features.every((i) => typeof i == "string") && (o.features = r.features), o;
|
|
906
1011
|
})), t;
|
|
907
1012
|
}
|
|
908
|
-
function
|
|
1013
|
+
function Ut(n) {
|
|
909
1014
|
return {
|
|
910
1015
|
/**
|
|
911
1016
|
* Lista produtos publicados. Por default só `published=true`; staff
|
|
912
1017
|
* pode passar `includeDrafts: true` (requer Bearer com role admin/operator).
|
|
913
1018
|
*/
|
|
914
1019
|
async list(e = {}) {
|
|
915
|
-
let t = await
|
|
1020
|
+
let t = await S(n, {
|
|
916
1021
|
method: "GET",
|
|
917
1022
|
path: "/api/sdk/v1/catalog"
|
|
918
1023
|
});
|
|
@@ -922,7 +1027,7 @@ function St(n) {
|
|
|
922
1027
|
"Catalog list response missing products array"
|
|
923
1028
|
);
|
|
924
1029
|
return {
|
|
925
|
-
products: t.products.map(
|
|
1030
|
+
products: t.products.map(Lt),
|
|
926
1031
|
fetchedAt: typeof t.fetchedAt == "string" ? t.fetchedAt : (/* @__PURE__ */ new Date()).toISOString()
|
|
927
1032
|
};
|
|
928
1033
|
},
|
|
@@ -934,7 +1039,7 @@ function St(n) {
|
|
|
934
1039
|
async get(e) {
|
|
935
1040
|
if (!e || typeof e != "string")
|
|
936
1041
|
throw new exports.NeetruError("validation_failed", "slug is required");
|
|
937
|
-
let t = await
|
|
1042
|
+
let t = await S(n, {
|
|
938
1043
|
method: "GET",
|
|
939
1044
|
path: `/api/sdk/v1/catalog/${encodeURIComponent(e)}`
|
|
940
1045
|
});
|
|
@@ -943,32 +1048,32 @@ function St(n) {
|
|
|
943
1048
|
"invalid_response",
|
|
944
1049
|
"Catalog get response missing product"
|
|
945
1050
|
);
|
|
946
|
-
return
|
|
1051
|
+
return Lt(t.product);
|
|
947
1052
|
}
|
|
948
1053
|
};
|
|
949
1054
|
}
|
|
950
1055
|
|
|
951
1056
|
// src/entitlements.ts
|
|
952
|
-
|
|
953
|
-
var
|
|
954
|
-
function
|
|
955
|
-
throw new exports.NeetruError("validation_failed",
|
|
1057
|
+
C();
|
|
1058
|
+
var Rr = "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
|
+
function Mt() {
|
|
1060
|
+
throw new exports.NeetruError("validation_failed", Rr);
|
|
956
1061
|
}
|
|
957
|
-
function
|
|
1062
|
+
function qt(n) {
|
|
958
1063
|
return {
|
|
959
1064
|
/**
|
|
960
1065
|
* @deprecated Endpoint morto no Core — sempre retornava `not_subscribed`.
|
|
961
1066
|
* Lança `NeetruError`. Use `client.usage.check(resource, {productId, tenantId})`.
|
|
962
1067
|
*/
|
|
963
1068
|
async check(e, t, r) {
|
|
964
|
-
|
|
1069
|
+
Mt();
|
|
965
1070
|
},
|
|
966
1071
|
/**
|
|
967
1072
|
* @deprecated Endpoint morto no Core — sempre retornava `not_subscribed`.
|
|
968
1073
|
* Lança `NeetruError`. Use `client.usage.check(resource, {productId, tenantId})`.
|
|
969
1074
|
*/
|
|
970
1075
|
async checkDetailed(e, t, r) {
|
|
971
|
-
|
|
1076
|
+
Mt();
|
|
972
1077
|
},
|
|
973
1078
|
/** Test helper legado — no-op (não há mais cache). Mantido por compat. */
|
|
974
1079
|
__resetCache() {
|
|
@@ -977,16 +1082,16 @@ function Rt(n) {
|
|
|
977
1082
|
}
|
|
978
1083
|
|
|
979
1084
|
// src/telemetry.ts
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
var
|
|
983
|
-
function
|
|
1085
|
+
C();
|
|
1086
|
+
q();
|
|
1087
|
+
var Ft = ["debug", "info", "warn", "error", "fatal"];
|
|
1088
|
+
function Ir() {
|
|
984
1089
|
return typeof globalThis < "u" && typeof globalThis.window < "u";
|
|
985
1090
|
}
|
|
986
|
-
function
|
|
987
|
-
return
|
|
1091
|
+
function jt() {
|
|
1092
|
+
return Ir() ? { path: "/api/sdk/v1/telemetry/event/public", requireAuth: false } : { path: "/api/sdk/v1/telemetry/event", requireAuth: true };
|
|
988
1093
|
}
|
|
989
|
-
function
|
|
1094
|
+
function Cr(n) {
|
|
990
1095
|
switch (n) {
|
|
991
1096
|
case "debug":
|
|
992
1097
|
return console.debug.bind(console);
|
|
@@ -1002,18 +1107,18 @@ function er(n) {
|
|
|
1002
1107
|
return console.log.bind(console);
|
|
1003
1108
|
}
|
|
1004
1109
|
}
|
|
1005
|
-
var
|
|
1006
|
-
function
|
|
1110
|
+
var Ar = 500, Er = 50;
|
|
1111
|
+
function Wt(n) {
|
|
1007
1112
|
let e = [], t = null, r = null;
|
|
1008
1113
|
async function o() {
|
|
1009
1114
|
if (e.length === 0) return;
|
|
1010
1115
|
let s = e.splice(0, e.length), a = Promise.allSettled(
|
|
1011
|
-
s.map(async (
|
|
1116
|
+
s.map(async (c) => {
|
|
1012
1117
|
try {
|
|
1013
|
-
let u = { name:
|
|
1014
|
-
|
|
1015
|
-
let p =
|
|
1016
|
-
await
|
|
1118
|
+
let u = { name: c.name }, d = c.productSlug ?? n.productId;
|
|
1119
|
+
d && (u.productSlug = d), c.properties && typeof c.properties == "object" && (u.properties = c.properties), c.timestamp && (u.timestamp = c.timestamp);
|
|
1120
|
+
let p = jt();
|
|
1121
|
+
await S(n, {
|
|
1017
1122
|
method: "POST",
|
|
1018
1123
|
path: p.path,
|
|
1019
1124
|
body: u,
|
|
@@ -1025,7 +1130,7 @@ function At(n) {
|
|
|
1025
1130
|
idempotencyKey: !0
|
|
1026
1131
|
});
|
|
1027
1132
|
} catch (u) {
|
|
1028
|
-
typeof console < "u" && console.warn("[neetru-sdk] telemetry.track flush failed for event",
|
|
1133
|
+
typeof console < "u" && console.warn("[neetru-sdk] telemetry.track flush failed for event", c.name, u);
|
|
1029
1134
|
}
|
|
1030
1135
|
})
|
|
1031
1136
|
);
|
|
@@ -1036,7 +1141,7 @@ function At(n) {
|
|
|
1036
1141
|
function i() {
|
|
1037
1142
|
t || (t = setTimeout(() => {
|
|
1038
1143
|
o();
|
|
1039
|
-
},
|
|
1144
|
+
}, Ar));
|
|
1040
1145
|
}
|
|
1041
1146
|
return {
|
|
1042
1147
|
/**
|
|
@@ -1058,17 +1163,17 @@ function At(n) {
|
|
|
1058
1163
|
throw new exports.NeetruError("validation_failed", "event.name is required");
|
|
1059
1164
|
if (s.name.length > 128)
|
|
1060
1165
|
throw new exports.NeetruError("validation_failed", "event.name max 128 chars");
|
|
1061
|
-
let a = { name: s.name },
|
|
1062
|
-
|
|
1063
|
-
let u =
|
|
1166
|
+
let a = { name: s.name }, c = s.productSlug ?? n.productId;
|
|
1167
|
+
c && (a.productSlug = c), s.properties && typeof s.properties == "object" && (a.properties = s.properties), s.timestamp && (a.timestamp = s.timestamp);
|
|
1168
|
+
let u = jt(), d = await S(n, {
|
|
1064
1169
|
method: "POST",
|
|
1065
1170
|
path: u.path,
|
|
1066
1171
|
body: a,
|
|
1067
1172
|
requireAuth: u.requireAuth
|
|
1068
1173
|
});
|
|
1069
|
-
if (!
|
|
1174
|
+
if (!d || d.ok !== true || typeof d.eventId != "string")
|
|
1070
1175
|
throw new exports.NeetruError("invalid_response", "Telemetry response missing eventId");
|
|
1071
|
-
return { ok: true, eventId:
|
|
1176
|
+
return { ok: true, eventId: d.eventId };
|
|
1072
1177
|
},
|
|
1073
1178
|
/**
|
|
1074
1179
|
* Fire-and-forget: enqueue + flush 500ms debounce. Não retorna `eventId`
|
|
@@ -1081,7 +1186,7 @@ function At(n) {
|
|
|
1081
1186
|
* ```
|
|
1082
1187
|
*/
|
|
1083
1188
|
track(s) {
|
|
1084
|
-
!s || typeof s != "object" || typeof s.name != "string" || s.name.length === 0 || s.name.length > 128 || (e.push(s), e.length >=
|
|
1189
|
+
!s || typeof s != "object" || typeof s.name != "string" || s.name.length === 0 || s.name.length > 128 || (e.push(s), e.length >= Er ? (t && (clearTimeout(t), t = null), o()) : i());
|
|
1085
1190
|
},
|
|
1086
1191
|
/**
|
|
1087
1192
|
* Força flush imediato da queue de `track()`. Resolva antes de
|
|
@@ -1108,21 +1213,21 @@ function At(n) {
|
|
|
1108
1213
|
async log(s) {
|
|
1109
1214
|
if (!s || typeof s != "object")
|
|
1110
1215
|
throw new exports.NeetruError("validation_failed", "log input is required");
|
|
1111
|
-
if (!
|
|
1112
|
-
throw new exports.NeetruError("validation_failed", `level must be one of ${
|
|
1216
|
+
if (!Ft.includes(s.level))
|
|
1217
|
+
throw new exports.NeetruError("validation_failed", `level must be one of ${Ft.join(", ")}`);
|
|
1113
1218
|
if (typeof s.message != "string" || s.message.length === 0)
|
|
1114
1219
|
throw new exports.NeetruError("validation_failed", "message is required");
|
|
1115
1220
|
if (s.message.length > 4e3)
|
|
1116
1221
|
throw new exports.NeetruError("validation_failed", "message max 4000 chars");
|
|
1117
1222
|
if (n.env === "dev")
|
|
1118
|
-
return
|
|
1223
|
+
return Cr(s.level)(`[neetru-sdk] ${s.level}: ${s.message}`, s.metadata ?? {}), { ok: true, mode: "mock" };
|
|
1119
1224
|
let a = {
|
|
1120
1225
|
level: s.level,
|
|
1121
1226
|
message: s.message
|
|
1122
1227
|
};
|
|
1123
1228
|
s.metadata && (a.metadata = s.metadata);
|
|
1124
|
-
let
|
|
1125
|
-
|
|
1229
|
+
let c = s.productSlug ?? n.productId;
|
|
1230
|
+
c && (a.productSlug = c);
|
|
1126
1231
|
let u = s.correlationId;
|
|
1127
1232
|
if (!u)
|
|
1128
1233
|
try {
|
|
@@ -1130,14 +1235,14 @@ function At(n) {
|
|
|
1130
1235
|
typeof f == "string" && f.length > 0 && (u = f);
|
|
1131
1236
|
} catch {
|
|
1132
1237
|
}
|
|
1133
|
-
let
|
|
1134
|
-
u && (
|
|
1135
|
-
let p = await
|
|
1238
|
+
let d = {};
|
|
1239
|
+
u && (d["x-correlation-id"] = u);
|
|
1240
|
+
let p = await S(n, {
|
|
1136
1241
|
method: "POST",
|
|
1137
1242
|
path: "/api/sdk/v1/telemetry/log",
|
|
1138
1243
|
body: a,
|
|
1139
1244
|
requireAuth: true,
|
|
1140
|
-
headers:
|
|
1245
|
+
headers: d
|
|
1141
1246
|
});
|
|
1142
1247
|
if (!p || p.ok !== true)
|
|
1143
1248
|
throw new exports.NeetruError("invalid_response", "Telemetry log response missing ok");
|
|
@@ -1151,22 +1256,22 @@ function At(n) {
|
|
|
1151
1256
|
}
|
|
1152
1257
|
|
|
1153
1258
|
// src/usage.ts
|
|
1154
|
-
|
|
1155
|
-
|
|
1259
|
+
C();
|
|
1260
|
+
q();
|
|
1156
1261
|
|
|
1157
1262
|
// src/validators.ts
|
|
1158
|
-
|
|
1159
|
-
var
|
|
1263
|
+
C();
|
|
1264
|
+
var $t = /^[a-z0-9][a-z0-9_-]{0,62}$/i;
|
|
1160
1265
|
function O(n, e) {
|
|
1161
|
-
if (typeof e != "string" ||
|
|
1266
|
+
if (typeof e != "string" || !$t.test(e))
|
|
1162
1267
|
throw new exports.NeetruError(
|
|
1163
1268
|
"validation_failed",
|
|
1164
|
-
`${n} must match ${
|
|
1269
|
+
`${n} must match ${$t.source} (lowercase alphanumeric, _, -; first char must be alphanumeric; max 63 chars)`
|
|
1165
1270
|
);
|
|
1166
1271
|
}
|
|
1167
1272
|
|
|
1168
1273
|
// src/usage.ts
|
|
1169
|
-
function
|
|
1274
|
+
function Vt(n) {
|
|
1170
1275
|
return {
|
|
1171
1276
|
/**
|
|
1172
1277
|
* Reporta consumo metered de um resource. Hit no endpoint canônico
|
|
@@ -1197,7 +1302,7 @@ function Pt(n) {
|
|
|
1197
1302
|
"tenantId required (pass to options or set on createNeetruClient)"
|
|
1198
1303
|
);
|
|
1199
1304
|
O("productId", o), O("tenantId", i);
|
|
1200
|
-
let s = await
|
|
1305
|
+
let s = await S(n, {
|
|
1201
1306
|
method: "POST",
|
|
1202
1307
|
path: "/api/sdk/v1/usage/record",
|
|
1203
1308
|
body: { productId: o, tenantId: i, resource: e, qty: Math.floor(t) },
|
|
@@ -1231,7 +1336,7 @@ function Pt(n) {
|
|
|
1231
1336
|
"productId and tenantId required (pass to options or set on createNeetruClient)"
|
|
1232
1337
|
);
|
|
1233
1338
|
O("productId", r), O("tenantId", o);
|
|
1234
|
-
let i = await
|
|
1339
|
+
let i = await S(n, {
|
|
1235
1340
|
method: "GET",
|
|
1236
1341
|
path: "/api/sdk/v1/entitlements",
|
|
1237
1342
|
query: { productId: r, tenantId: o, feature: e },
|
|
@@ -1254,10 +1359,10 @@ function Pt(n) {
|
|
|
1254
1359
|
}
|
|
1255
1360
|
|
|
1256
1361
|
// src/support.ts
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
var
|
|
1260
|
-
function
|
|
1362
|
+
C();
|
|
1363
|
+
q();
|
|
1364
|
+
var Ve = ["low", "normal", "high", "urgent"], Pr = ["open", "pending", "resolved", "closed"], Bt = 50;
|
|
1365
|
+
function Kt(n) {
|
|
1261
1366
|
if (!n || typeof n != "object")
|
|
1262
1367
|
throw new exports.NeetruError("invalid_response", "Ticket response is not an object");
|
|
1263
1368
|
let e = n;
|
|
@@ -1268,8 +1373,8 @@ function Nt(n) {
|
|
|
1268
1373
|
id: e.id,
|
|
1269
1374
|
subject: typeof e.subject == "string" ? e.subject : "",
|
|
1270
1375
|
message: typeof e.message == "string" ? e.message : "",
|
|
1271
|
-
severity:
|
|
1272
|
-
status:
|
|
1376
|
+
severity: Ve.includes(e.severity) ? e.severity : "normal",
|
|
1377
|
+
status: Pr.includes(e.status) ? e.status : "open",
|
|
1273
1378
|
createdAt: typeof e.createdAt == "string" ? e.createdAt : (/* @__PURE__ */ new Date()).toISOString(),
|
|
1274
1379
|
updatedAt: typeof e.updatedAt == "string" ? e.updatedAt : void 0,
|
|
1275
1380
|
productSlug: typeof e.productId == "string" ? e.productId : typeof e.productSlug == "string" ? e.productSlug : void 0,
|
|
@@ -1287,15 +1392,15 @@ function Nt(n) {
|
|
|
1287
1392
|
satisfactionComment: typeof e.satisfactionComment == "string" ? e.satisfactionComment : null
|
|
1288
1393
|
};
|
|
1289
1394
|
}
|
|
1290
|
-
function
|
|
1395
|
+
function we(n) {
|
|
1291
1396
|
if (!n || typeof n != "string")
|
|
1292
1397
|
throw new exports.NeetruError("validation_failed", "ticketId is required");
|
|
1293
1398
|
}
|
|
1294
|
-
function
|
|
1399
|
+
function zt(n) {
|
|
1295
1400
|
if (typeof n == "string" && n.length > 2e3)
|
|
1296
1401
|
throw new exports.NeetruError("validation_failed", "comment max 2000 chars");
|
|
1297
1402
|
}
|
|
1298
|
-
function
|
|
1403
|
+
function Ht(n, ...e) {
|
|
1299
1404
|
for (let t of e)
|
|
1300
1405
|
if (typeof t == "string" && t.length > 0)
|
|
1301
1406
|
return O("productSlug", t), t;
|
|
@@ -1306,41 +1411,44 @@ function Ot(n, ...e) {
|
|
|
1306
1411
|
"productId required (pass to input.productSlug or set on createNeetruClient)"
|
|
1307
1412
|
);
|
|
1308
1413
|
}
|
|
1309
|
-
var
|
|
1310
|
-
function
|
|
1414
|
+
var Dr = "X-Neetru-End-User-Token", Nr = "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.", xr = "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.", Or = "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.", Lr = "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
|
+
function Gt(n) {
|
|
1311
1416
|
if (n instanceof exports.NeetruError && !(n instanceof exports.NeetruAuthRequiredError)) {
|
|
1312
1417
|
if (n.code === "end_user_token_required")
|
|
1313
1418
|
throw new exports.NeetruAuthRequiredError(
|
|
1314
|
-
|
|
1419
|
+
xr,
|
|
1315
1420
|
"end_user_token_required",
|
|
1316
1421
|
n.status,
|
|
1317
|
-
n.requestId
|
|
1422
|
+
n.requestId,
|
|
1423
|
+
n.correlationId
|
|
1318
1424
|
);
|
|
1319
1425
|
if (n.code === "invalid_end_user_token")
|
|
1320
1426
|
throw new exports.NeetruAuthRequiredError(
|
|
1321
|
-
|
|
1427
|
+
Or,
|
|
1322
1428
|
"invalid_end_user_token",
|
|
1323
1429
|
n.status,
|
|
1324
|
-
n.requestId
|
|
1430
|
+
n.requestId,
|
|
1431
|
+
n.correlationId
|
|
1325
1432
|
);
|
|
1326
1433
|
if (n.code === "end_user_token_product_mismatch")
|
|
1327
1434
|
throw new exports.NeetruError(
|
|
1328
1435
|
"forbidden",
|
|
1329
|
-
|
|
1436
|
+
Lr,
|
|
1330
1437
|
n.status,
|
|
1331
1438
|
n.requestId,
|
|
1332
|
-
"end_user_token_product_mismatch"
|
|
1439
|
+
"end_user_token_product_mismatch",
|
|
1440
|
+
n.correlationId
|
|
1333
1441
|
);
|
|
1334
1442
|
}
|
|
1335
1443
|
throw n;
|
|
1336
1444
|
}
|
|
1337
|
-
function
|
|
1445
|
+
function Ur(n) {
|
|
1338
1446
|
return n instanceof exports.NeetruError && n.code === "invalid_end_user_token";
|
|
1339
1447
|
}
|
|
1340
|
-
function
|
|
1341
|
-
function t() {
|
|
1448
|
+
function Qt(n, e = {}) {
|
|
1449
|
+
async function t() {
|
|
1342
1450
|
try {
|
|
1343
|
-
let a = e.getEndUserToken?.();
|
|
1451
|
+
let a = await e.getEndUserToken?.();
|
|
1344
1452
|
return typeof a == "string" && a.length > 0 ? a : null;
|
|
1345
1453
|
} catch {
|
|
1346
1454
|
return null;
|
|
@@ -1355,36 +1463,36 @@ function Ut(n, e = {}) {
|
|
|
1355
1463
|
}
|
|
1356
1464
|
}
|
|
1357
1465
|
function o(a) {
|
|
1358
|
-
return a ? { [
|
|
1466
|
+
return a ? { [Dr]: a } : {};
|
|
1359
1467
|
}
|
|
1360
|
-
async function i(a,
|
|
1361
|
-
let u = t();
|
|
1362
|
-
if (!u && a && await r() && (u = t()), !u && a)
|
|
1363
|
-
throw new exports.NeetruAuthRequiredError(
|
|
1468
|
+
async function i(a, c) {
|
|
1469
|
+
let u = await t();
|
|
1470
|
+
if (!u && a && await r() && (u = await t()), !u && a)
|
|
1471
|
+
throw new exports.NeetruAuthRequiredError(Nr, "no_signed_in_user");
|
|
1364
1472
|
try {
|
|
1365
|
-
return await
|
|
1366
|
-
} catch (
|
|
1367
|
-
if (u &&
|
|
1368
|
-
let p = t();
|
|
1473
|
+
return await c(o(u));
|
|
1474
|
+
} catch (d) {
|
|
1475
|
+
if (u && Ur(d) && await r()) {
|
|
1476
|
+
let p = await t();
|
|
1369
1477
|
if (p)
|
|
1370
1478
|
try {
|
|
1371
|
-
return await
|
|
1479
|
+
return await c(o(p));
|
|
1372
1480
|
} catch (f) {
|
|
1373
|
-
|
|
1481
|
+
Gt(f);
|
|
1374
1482
|
}
|
|
1375
1483
|
}
|
|
1376
|
-
|
|
1484
|
+
Gt(d);
|
|
1377
1485
|
}
|
|
1378
1486
|
}
|
|
1379
|
-
async function s(a,
|
|
1487
|
+
async function s(a, c = {}) {
|
|
1380
1488
|
await i(
|
|
1381
1489
|
true,
|
|
1382
|
-
(u) =>
|
|
1490
|
+
(u) => S(n, {
|
|
1383
1491
|
method: "POST",
|
|
1384
1492
|
path: a,
|
|
1385
1493
|
requireAuth: !0,
|
|
1386
1494
|
headers: u,
|
|
1387
|
-
...
|
|
1495
|
+
...c
|
|
1388
1496
|
})
|
|
1389
1497
|
);
|
|
1390
1498
|
}
|
|
@@ -1406,21 +1514,21 @@ function Ut(n, e = {}) {
|
|
|
1406
1514
|
throw new exports.NeetruError("validation_failed", "message is required");
|
|
1407
1515
|
if (a.message.length > 1e4)
|
|
1408
1516
|
throw new exports.NeetruError("validation_failed", "message max 10000 chars");
|
|
1409
|
-
if (a.severity && !
|
|
1517
|
+
if (a.severity && !Ve.includes(a.severity))
|
|
1410
1518
|
throw new exports.NeetruError(
|
|
1411
1519
|
"validation_failed",
|
|
1412
|
-
`severity must be one of ${
|
|
1520
|
+
`severity must be one of ${Ve.join(", ")}`
|
|
1413
1521
|
);
|
|
1414
1522
|
let u = {
|
|
1415
|
-
productId:
|
|
1523
|
+
productId: Ht(n, a.productSlug),
|
|
1416
1524
|
subject: a.subject,
|
|
1417
1525
|
message: a.message,
|
|
1418
1526
|
severity: a.severity ?? "normal"
|
|
1419
1527
|
};
|
|
1420
1528
|
a.metadata && typeof a.metadata == "object" && (u.metadata = a.metadata), typeof a.pageUrl == "string" && (u.pageUrl = a.pageUrl), typeof a.screenshotBase64 == "string" && (u.screenshotBase64 = a.screenshotBase64), typeof a.consoleLogs == "string" && (u.consoleLogs = a.consoleLogs);
|
|
1421
|
-
let
|
|
1529
|
+
let d = await i(
|
|
1422
1530
|
false,
|
|
1423
|
-
(f) =>
|
|
1531
|
+
(f) => S(n, {
|
|
1424
1532
|
method: "POST",
|
|
1425
1533
|
path: "/api/sdk/v1/support/tickets",
|
|
1426
1534
|
body: u,
|
|
@@ -1428,8 +1536,8 @@ function Ut(n, e = {}) {
|
|
|
1428
1536
|
idempotencyKey: !0,
|
|
1429
1537
|
headers: f
|
|
1430
1538
|
})
|
|
1431
|
-
), p =
|
|
1432
|
-
return
|
|
1539
|
+
), p = d && typeof d == "object" && "ticket" in d ? d.ticket : d;
|
|
1540
|
+
return Kt(p);
|
|
1433
1541
|
},
|
|
1434
1542
|
/**
|
|
1435
1543
|
* Lista tickets do customer no produto atual. `productSlug` no contexto.
|
|
@@ -1437,23 +1545,23 @@ function Ut(n, e = {}) {
|
|
|
1437
1545
|
* Endpoint canonical: `GET /api/sdk/v1/support/tickets?productId=...`.
|
|
1438
1546
|
*/
|
|
1439
1547
|
async listMyTickets(a) {
|
|
1440
|
-
let
|
|
1441
|
-
if (u !== void 0 && (!Number.isInteger(u) || u < 1 || u >
|
|
1548
|
+
let c = Ht(n, a?.productSlug), u = a?.limit;
|
|
1549
|
+
if (u !== void 0 && (!Number.isInteger(u) || u < 1 || u > Bt))
|
|
1442
1550
|
throw new exports.NeetruError(
|
|
1443
1551
|
"validation_failed",
|
|
1444
|
-
`limit must be an integer 1-${
|
|
1552
|
+
`limit must be an integer 1-${Bt} (Core default 20)`
|
|
1445
1553
|
);
|
|
1446
|
-
let
|
|
1554
|
+
let d = await i(
|
|
1447
1555
|
true,
|
|
1448
|
-
(f) =>
|
|
1556
|
+
(f) => S(n, {
|
|
1449
1557
|
method: "GET",
|
|
1450
1558
|
path: "/api/sdk/v1/support/tickets",
|
|
1451
|
-
query: { productId:
|
|
1559
|
+
query: { productId: c, limit: u },
|
|
1452
1560
|
requireAuth: !0,
|
|
1453
1561
|
headers: f
|
|
1454
1562
|
})
|
|
1455
1563
|
);
|
|
1456
|
-
return (Array.isArray(
|
|
1564
|
+
return (Array.isArray(d) ? d : d && typeof d == "object" && "tickets" in d ? d.tickets ?? [] : []).map(Kt);
|
|
1457
1565
|
},
|
|
1458
1566
|
/**
|
|
1459
1567
|
* 3.4: marca a notificação de resolução como vista (`resolvedNotifiedAt`,
|
|
@@ -1461,7 +1569,7 @@ function Ut(n, e = {}) {
|
|
|
1461
1569
|
* `POST /api/sdk/v1/support/tickets/{id}/resolved-seen`.
|
|
1462
1570
|
*/
|
|
1463
1571
|
async markResolvedSeen(a) {
|
|
1464
|
-
|
|
1572
|
+
we(a), await s(
|
|
1465
1573
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/resolved-seen`
|
|
1466
1574
|
);
|
|
1467
1575
|
},
|
|
@@ -1472,7 +1580,7 @@ function Ut(n, e = {}) {
|
|
|
1472
1580
|
* `POST /api/sdk/v1/support/tickets/{id}/satisfaction-attempt`.
|
|
1473
1581
|
*/
|
|
1474
1582
|
async recordSatisfactionAttempt(a) {
|
|
1475
|
-
|
|
1583
|
+
we(a), await s(
|
|
1476
1584
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/satisfaction-attempt`,
|
|
1477
1585
|
// Incrementa satisfactionAttempts += 1 no Core — sem idempotencyKey,
|
|
1478
1586
|
// um retry de rede infla o contador (mesmo padrão de createTicket acima).
|
|
@@ -1486,9 +1594,9 @@ function Ut(n, e = {}) {
|
|
|
1486
1594
|
async submitSatisfaction(a) {
|
|
1487
1595
|
if (!a || typeof a != "object")
|
|
1488
1596
|
throw new exports.NeetruError("validation_failed", "input is required");
|
|
1489
|
-
if (
|
|
1597
|
+
if (we(a.ticketId), a.rating !== null && a.rating !== void 0 && (!Number.isInteger(a.rating) || a.rating < 1 || a.rating > 5))
|
|
1490
1598
|
throw new exports.NeetruError("validation_failed", "rating must be an integer 1-5 or null");
|
|
1491
|
-
|
|
1599
|
+
zt(a.comment), await s(
|
|
1492
1600
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a.ticketId)}/satisfaction`,
|
|
1493
1601
|
{
|
|
1494
1602
|
body: {
|
|
@@ -1504,10 +1612,10 @@ function Ut(n, e = {}) {
|
|
|
1504
1612
|
* satisfação/notificação. Endpoint canonical:
|
|
1505
1613
|
* `POST /api/sdk/v1/support/tickets/{id}/contest`.
|
|
1506
1614
|
*/
|
|
1507
|
-
async contestTicket(a,
|
|
1508
|
-
|
|
1615
|
+
async contestTicket(a, c) {
|
|
1616
|
+
we(a), zt(c?.comment), await s(
|
|
1509
1617
|
`/api/sdk/v1/support/tickets/${encodeURIComponent(a)}/contest`,
|
|
1510
|
-
{ body: { comment:
|
|
1618
|
+
{ body: { comment: c?.comment ?? void 0 } }
|
|
1511
1619
|
);
|
|
1512
1620
|
}
|
|
1513
1621
|
};
|
|
@@ -1517,11 +1625,11 @@ function Ut(n, e = {}) {
|
|
|
1517
1625
|
z();
|
|
1518
1626
|
|
|
1519
1627
|
// src/db/offline/query-engine.ts
|
|
1520
|
-
function
|
|
1628
|
+
function Jt(n) {
|
|
1521
1629
|
return n == null ? 0 : typeof n == "number" ? 1 : typeof n == "string" ? 2 : typeof n == "boolean" ? 3 : 4;
|
|
1522
1630
|
}
|
|
1523
|
-
function
|
|
1524
|
-
let t =
|
|
1631
|
+
function re(n, e) {
|
|
1632
|
+
let t = Jt(n), r = Jt(e);
|
|
1525
1633
|
if (t !== r) return t - r;
|
|
1526
1634
|
if (n == null) return 0;
|
|
1527
1635
|
if (typeof n == "number" && typeof e == "number") return n - e;
|
|
@@ -1530,7 +1638,7 @@ function ee(n, e) {
|
|
|
1530
1638
|
let o = String(n), i = String(e);
|
|
1531
1639
|
return o < i ? -1 : o > i ? 1 : 0;
|
|
1532
1640
|
}
|
|
1533
|
-
function
|
|
1641
|
+
function Be(n, e) {
|
|
1534
1642
|
let t = e.split("."), r = n;
|
|
1535
1643
|
for (let o of t) {
|
|
1536
1644
|
if (r == null || typeof r != "object")
|
|
@@ -1539,8 +1647,8 @@ function Me(n, e) {
|
|
|
1539
1647
|
}
|
|
1540
1648
|
return r;
|
|
1541
1649
|
}
|
|
1542
|
-
function
|
|
1543
|
-
let t =
|
|
1650
|
+
function qr(n, e) {
|
|
1651
|
+
let t = Be(n, e.field);
|
|
1544
1652
|
if (t === void 0) return false;
|
|
1545
1653
|
let { op: r, value: o } = e;
|
|
1546
1654
|
switch (r) {
|
|
@@ -1549,13 +1657,13 @@ function ur(n, e) {
|
|
|
1549
1657
|
case "!=":
|
|
1550
1658
|
return t !== o;
|
|
1551
1659
|
case "<":
|
|
1552
|
-
return
|
|
1660
|
+
return re(t, o) < 0;
|
|
1553
1661
|
case "<=":
|
|
1554
|
-
return
|
|
1662
|
+
return re(t, o) <= 0;
|
|
1555
1663
|
case ">":
|
|
1556
|
-
return
|
|
1664
|
+
return re(t, o) > 0;
|
|
1557
1665
|
case ">=":
|
|
1558
|
-
return
|
|
1666
|
+
return re(t, o) >= 0;
|
|
1559
1667
|
case "array-contains":
|
|
1560
1668
|
return Array.isArray(t) && t.includes(o);
|
|
1561
1669
|
case "in":
|
|
@@ -1570,24 +1678,24 @@ function ur(n, e) {
|
|
|
1570
1678
|
);
|
|
1571
1679
|
}
|
|
1572
1680
|
}
|
|
1573
|
-
function
|
|
1574
|
-
return e.every((t) =>
|
|
1681
|
+
function Fr(n, e) {
|
|
1682
|
+
return e.every((t) => qr(n, t));
|
|
1575
1683
|
}
|
|
1576
|
-
function
|
|
1684
|
+
function jr(n) {
|
|
1577
1685
|
return (e, t) => {
|
|
1578
1686
|
if (n) {
|
|
1579
|
-
let r =
|
|
1687
|
+
let r = Be(e.data, n.field), o = Be(t.data, n.field), i = re(r, o);
|
|
1580
1688
|
if (i !== 0)
|
|
1581
1689
|
return n.direction === "desc" ? -i : i;
|
|
1582
1690
|
}
|
|
1583
1691
|
return e.id < t.id ? -1 : e.id > t.id ? 1 : 0;
|
|
1584
1692
|
};
|
|
1585
1693
|
}
|
|
1586
|
-
function
|
|
1694
|
+
function Wr(n, e) {
|
|
1587
1695
|
let t = n.findIndex((r) => r.id === e.docId);
|
|
1588
1696
|
return t === -1 ? n : e.type === "startAfter" ? n.slice(t + 1) : n.slice(0, t);
|
|
1589
1697
|
}
|
|
1590
|
-
var
|
|
1698
|
+
var J = class n {
|
|
1591
1699
|
/**
|
|
1592
1700
|
* Avalia um `QueryDescriptor` contra um array de `OfflineDoc`.
|
|
1593
1701
|
*
|
|
@@ -1599,13 +1707,13 @@ var Q = class n {
|
|
|
1599
1707
|
* 5. Corta em `limit`.
|
|
1600
1708
|
*/
|
|
1601
1709
|
evaluate(e, t) {
|
|
1602
|
-
let r = e.filter((
|
|
1710
|
+
let r = e.filter((d) => !d.meta.deleted), o = t.where ?? [];
|
|
1603
1711
|
o.length > 0 && (r = r.filter(
|
|
1604
|
-
(
|
|
1712
|
+
(d) => Fr(d.data, o)
|
|
1605
1713
|
));
|
|
1606
|
-
let i =
|
|
1714
|
+
let i = jr(t.orderBy), s = [...r].sort(i), a = t.cursor ? Wr(s, t.cursor) : s, c = Math.min(t.limit ?? 20, 500);
|
|
1607
1715
|
return {
|
|
1608
|
-
docs: a.slice(0,
|
|
1716
|
+
docs: a.slice(0, c).map((d) => ({ id: d.id, data: d.data })),
|
|
1609
1717
|
// `incomplete` é sempre true aqui — o QueryEngine não sabe se o cache
|
|
1610
1718
|
// tem todos os docs da coleção. É responsabilidade do chamador (LocalStore)
|
|
1611
1719
|
// injetar o flag de completude baseado nos metadados do query_cache.
|
|
@@ -1623,7 +1731,7 @@ var Q = class n {
|
|
|
1623
1731
|
};
|
|
1624
1732
|
|
|
1625
1733
|
// src/db/offline/local-store.ts
|
|
1626
|
-
var
|
|
1734
|
+
var Vr = 1, V = "documents", B = "mutations", oe = "query_cache", ke = "sync_meta", X = "conflict_log", Se = class {
|
|
1627
1735
|
db = null;
|
|
1628
1736
|
dbName;
|
|
1629
1737
|
constructor(e) {
|
|
@@ -1635,22 +1743,22 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1635
1743
|
* Idempotente — chamadas subsequentes são no-ops se o banco já está aberto.
|
|
1636
1744
|
*/
|
|
1637
1745
|
async open() {
|
|
1638
|
-
this.db === null && (this.db = await idb.openDB(this.dbName,
|
|
1746
|
+
this.db === null && (this.db = await idb.openDB(this.dbName, Vr, {
|
|
1639
1747
|
upgrade(e) {
|
|
1640
|
-
if (!e.objectStoreNames.contains(
|
|
1641
|
-
let t = e.createObjectStore(
|
|
1748
|
+
if (!e.objectStoreNames.contains(V)) {
|
|
1749
|
+
let t = e.createObjectStore(V, {
|
|
1642
1750
|
keyPath: ["collection", "id"]
|
|
1643
1751
|
});
|
|
1644
1752
|
t.createIndex("by_collection", "collection"), t.createIndex("by_collection_state", ["collection", "meta.state"]), t.createIndex("by_updatedAtServer", "meta.updatedAtServer");
|
|
1645
1753
|
}
|
|
1646
|
-
if (!e.objectStoreNames.contains(
|
|
1647
|
-
let t = e.createObjectStore(
|
|
1754
|
+
if (!e.objectStoreNames.contains(B)) {
|
|
1755
|
+
let t = e.createObjectStore(B, {
|
|
1648
1756
|
keyPath: "mutationId"
|
|
1649
1757
|
});
|
|
1650
1758
|
t.createIndex("by_seq", "seq"), t.createIndex("by_status", "status"), t.createIndex("by_doc", ["collection", "docId"]), t.createIndex("by_batch", "batchId");
|
|
1651
1759
|
}
|
|
1652
|
-
if (e.objectStoreNames.contains(
|
|
1653
|
-
let t = e.createObjectStore(
|
|
1760
|
+
if (e.objectStoreNames.contains(oe) || e.createObjectStore(oe, { keyPath: "queryHash" }), e.objectStoreNames.contains(ke) || e.createObjectStore(ke, { keyPath: "key" }), !e.objectStoreNames.contains(X)) {
|
|
1761
|
+
let t = e.createObjectStore(X, {
|
|
1654
1762
|
keyPath: "id",
|
|
1655
1763
|
autoIncrement: true
|
|
1656
1764
|
});
|
|
@@ -1692,14 +1800,14 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1692
1800
|
* Retorna tombstones (deleted: true) — o chamador decide se deve mostrá-los.
|
|
1693
1801
|
*/
|
|
1694
1802
|
async getDoc(e, t) {
|
|
1695
|
-
return await this.assertOpen().get(
|
|
1803
|
+
return await this.assertOpen().get(V, [e, t]) ?? null;
|
|
1696
1804
|
}
|
|
1697
1805
|
/**
|
|
1698
1806
|
* Insere ou atualiza um documento na store `documents`.
|
|
1699
1807
|
* O documento é identificado pelo keyPath composto `[collection, id]`.
|
|
1700
1808
|
*/
|
|
1701
1809
|
async putDoc(e) {
|
|
1702
|
-
await this.assertOpen().put(
|
|
1810
|
+
await this.assertOpen().put(V, e);
|
|
1703
1811
|
}
|
|
1704
1812
|
/**
|
|
1705
1813
|
* Marca um documento como tombstone (`deleted: true`).
|
|
@@ -1707,8 +1815,8 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1707
1815
|
* No-op se o documento não existir.
|
|
1708
1816
|
*/
|
|
1709
1817
|
async deleteDoc(e, t) {
|
|
1710
|
-
let r = this.assertOpen(), o = await r.get(
|
|
1711
|
-
o && await r.put(
|
|
1818
|
+
let r = this.assertOpen(), o = await r.get(V, [e, t]);
|
|
1819
|
+
o && await r.put(V, {
|
|
1712
1820
|
...o,
|
|
1713
1821
|
meta: {
|
|
1714
1822
|
...o.meta,
|
|
@@ -1724,8 +1832,8 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1724
1832
|
* e delega a filtragem/ordenação/paginação ao QueryEngine (que opera em memória).
|
|
1725
1833
|
*/
|
|
1726
1834
|
async listDocs(e, t) {
|
|
1727
|
-
let o = await this.assertOpen().getAllFromIndex(
|
|
1728
|
-
return
|
|
1835
|
+
let o = await this.assertOpen().getAllFromIndex(V, "by_collection", e);
|
|
1836
|
+
return J.evaluate(o, t);
|
|
1729
1837
|
}
|
|
1730
1838
|
// ─── Sync meta (key-value) ──────────────────────────────────────────────────
|
|
1731
1839
|
/**
|
|
@@ -1736,14 +1844,14 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1736
1844
|
* 'lastFullResyncAt', 'leaderTabId'.
|
|
1737
1845
|
*/
|
|
1738
1846
|
async getMeta(e) {
|
|
1739
|
-
let r = await this.assertOpen().get(
|
|
1847
|
+
let r = await this.assertOpen().get(ke, e);
|
|
1740
1848
|
return r ? r.value : null;
|
|
1741
1849
|
}
|
|
1742
1850
|
/**
|
|
1743
1851
|
* Armazena ou atualiza um valor na store `sync_meta`.
|
|
1744
1852
|
*/
|
|
1745
1853
|
async setMeta(e, t) {
|
|
1746
|
-
await this.assertOpen().put(
|
|
1854
|
+
await this.assertOpen().put(ke, { key: e, value: t });
|
|
1747
1855
|
}
|
|
1748
1856
|
// ─── Conflict log ───────────────────────────────────────────────────────────
|
|
1749
1857
|
/**
|
|
@@ -1751,7 +1859,7 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1751
1859
|
* O `id` é autoIncrement — não deve ser fornecido pelo chamador.
|
|
1752
1860
|
*/
|
|
1753
1861
|
async appendConflict(e) {
|
|
1754
|
-
let r = await this.assertOpen().add(
|
|
1862
|
+
let r = await this.assertOpen().add(X, e);
|
|
1755
1863
|
return { ...e, id: r };
|
|
1756
1864
|
}
|
|
1757
1865
|
/**
|
|
@@ -1759,15 +1867,15 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1759
1867
|
* Filtra por `delivered` se a opção for fornecida.
|
|
1760
1868
|
*/
|
|
1761
1869
|
async listConflicts(e) {
|
|
1762
|
-
let r = await this.assertOpen().getAll(
|
|
1870
|
+
let r = await this.assertOpen().getAll(X);
|
|
1763
1871
|
return e?.delivered !== void 0 ? r.filter((o) => o.delivered === e.delivered) : r;
|
|
1764
1872
|
}
|
|
1765
1873
|
/**
|
|
1766
1874
|
* Marca um registro do `conflict_log` como entregue ao produto.
|
|
1767
1875
|
*/
|
|
1768
1876
|
async markConflictDelivered(e) {
|
|
1769
|
-
let t = this.assertOpen(), r = await t.get(
|
|
1770
|
-
r && await t.put(
|
|
1877
|
+
let t = this.assertOpen(), r = await t.get(X, e);
|
|
1878
|
+
r && await t.put(X, { ...r, delivered: true });
|
|
1771
1879
|
}
|
|
1772
1880
|
// ─── Mutations (acessores usados pelo MutationQueue) ────────────────────────
|
|
1773
1881
|
/**
|
|
@@ -1775,13 +1883,13 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1775
1883
|
* Key: `mutationId`.
|
|
1776
1884
|
*/
|
|
1777
1885
|
async putMutation(e) {
|
|
1778
|
-
await this.assertOpen().put(
|
|
1886
|
+
await this.assertOpen().put(B, e);
|
|
1779
1887
|
}
|
|
1780
1888
|
/**
|
|
1781
1889
|
* Retorna uma mutação pelo `mutationId`, ou `null` se não existir.
|
|
1782
1890
|
*/
|
|
1783
1891
|
async getMutation(e) {
|
|
1784
|
-
return await this.assertOpen().get(
|
|
1892
|
+
return await this.assertOpen().get(B, e) ?? null;
|
|
1785
1893
|
}
|
|
1786
1894
|
/**
|
|
1787
1895
|
* Lista mutações com filtros opcionais.
|
|
@@ -1789,14 +1897,14 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1789
1897
|
*/
|
|
1790
1898
|
async listMutations(e) {
|
|
1791
1899
|
let t = this.assertOpen(), r;
|
|
1792
|
-
return e?.status !== void 0 ? r = await t.getAllFromIndex(
|
|
1900
|
+
return e?.status !== void 0 ? r = await t.getAllFromIndex(B, "by_status", e.status) : e?.collection !== void 0 && e.docId !== void 0 ? r = await t.getAllFromIndex(B, "by_doc", [e.collection, e.docId]) : r = await t.getAllFromIndex(B, "by_seq"), r.sort((o, i) => o.seq - i.seq);
|
|
1793
1901
|
}
|
|
1794
1902
|
/**
|
|
1795
1903
|
* Remove uma mutação pelo `mutationId`.
|
|
1796
1904
|
* No-op se não existir.
|
|
1797
1905
|
*/
|
|
1798
1906
|
async deleteMutation(e) {
|
|
1799
|
-
await this.assertOpen().delete(
|
|
1907
|
+
await this.assertOpen().delete(B, e);
|
|
1800
1908
|
}
|
|
1801
1909
|
// ─── Collection discovery ────────────────────────────────────────────────────
|
|
1802
1910
|
/**
|
|
@@ -1809,7 +1917,7 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1809
1917
|
* coletar os valores únicos de forma eficiente (sem carregar os docs completos).
|
|
1810
1918
|
*/
|
|
1811
1919
|
async listCollections() {
|
|
1812
|
-
let t = await this.assertOpen().getAllFromIndex(
|
|
1920
|
+
let t = await this.assertOpen().getAllFromIndex(V, "by_collection"), r = /* @__PURE__ */ new Set();
|
|
1813
1921
|
for (let o of t)
|
|
1814
1922
|
r.add(o.collection);
|
|
1815
1923
|
return Array.from(r);
|
|
@@ -1819,24 +1927,24 @@ var gr = 1, $ = "documents", V = "mutations", te = "query_cache", he = "sync_met
|
|
|
1819
1927
|
* Retorna a entrada de `query_cache` para um hash de query, ou `null`.
|
|
1820
1928
|
*/
|
|
1821
1929
|
async getQueryCache(e) {
|
|
1822
|
-
return await this.assertOpen().get(
|
|
1930
|
+
return await this.assertOpen().get(oe, e) ?? null;
|
|
1823
1931
|
}
|
|
1824
1932
|
/**
|
|
1825
1933
|
* Insere ou atualiza uma entrada de `query_cache`.
|
|
1826
1934
|
*/
|
|
1827
1935
|
async putQueryCache(e) {
|
|
1828
|
-
await this.assertOpen().put(
|
|
1936
|
+
await this.assertOpen().put(oe, e);
|
|
1829
1937
|
}
|
|
1830
1938
|
/**
|
|
1831
1939
|
* Remove uma entrada de `query_cache` pelo queryHash.
|
|
1832
1940
|
*/
|
|
1833
1941
|
async deleteQueryCache(e) {
|
|
1834
|
-
await this.assertOpen().delete(
|
|
1942
|
+
await this.assertOpen().delete(oe, e);
|
|
1835
1943
|
}
|
|
1836
1944
|
};
|
|
1837
1945
|
|
|
1838
1946
|
// src/db/offline/memory-store.ts
|
|
1839
|
-
var
|
|
1947
|
+
var Te = class {
|
|
1840
1948
|
// store `documents` — key: `${collection}\0${id}`
|
|
1841
1949
|
_docs = /* @__PURE__ */ new Map();
|
|
1842
1950
|
// store `mutations` — key: mutationId
|
|
@@ -1884,7 +1992,7 @@ var ye = class {
|
|
|
1884
1992
|
let r = Array.from(this._docs.values()).filter(
|
|
1885
1993
|
(o) => o.collection === e
|
|
1886
1994
|
);
|
|
1887
|
-
return
|
|
1995
|
+
return J.evaluate(r, t);
|
|
1888
1996
|
}
|
|
1889
1997
|
// ─── Sync meta ──────────────────────────────────────────────────────────────
|
|
1890
1998
|
async getMeta(e) {
|
|
@@ -1941,13 +2049,13 @@ var ye = class {
|
|
|
1941
2049
|
};
|
|
1942
2050
|
|
|
1943
2051
|
// src/db/offline/mutation-queue.ts
|
|
1944
|
-
function
|
|
2052
|
+
function Ke() {
|
|
1945
2053
|
return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (n) => {
|
|
1946
2054
|
let e = Math.random() * 16 | 0;
|
|
1947
2055
|
return (n === "x" ? e : e & 3 | 8).toString(16);
|
|
1948
2056
|
});
|
|
1949
2057
|
}
|
|
1950
|
-
var
|
|
2058
|
+
var Re = class {
|
|
1951
2059
|
store;
|
|
1952
2060
|
/**
|
|
1953
2061
|
* Ponteiro de sequência local.
|
|
@@ -2043,18 +2151,18 @@ var _e = class {
|
|
|
2043
2151
|
* Atômico: a persistência final é uma única operação putMutation.
|
|
2044
2152
|
*/
|
|
2045
2153
|
async enqueue(e) {
|
|
2046
|
-
let { collection: t, op: r, payload: o, baseVersion: i, batchId: s, upsert: a } = e,
|
|
2154
|
+
let { collection: t, op: r, payload: o, baseVersion: i, batchId: s, upsert: a } = e, c = e.docId ?? (r === "add" ? Ke() : "");
|
|
2047
2155
|
await this.syncSeq();
|
|
2048
|
-
let
|
|
2049
|
-
if (
|
|
2050
|
-
let m = this.tryCoalesce(
|
|
2156
|
+
let d = (await this.store.listMutations({ collection: t, docId: c })).filter((m) => m.status === "queued").sort((m, h) => h.seq - m.seq)[0];
|
|
2157
|
+
if (d !== void 0) {
|
|
2158
|
+
let m = this.tryCoalesce(d, r, o, s, a);
|
|
2051
2159
|
if (m !== null) {
|
|
2052
2160
|
if ("removed" in m)
|
|
2053
|
-
return await this.store.deleteMutation(
|
|
2054
|
-
seq:
|
|
2055
|
-
mutationId:
|
|
2161
|
+
return await this.store.deleteMutation(d.mutationId), {
|
|
2162
|
+
seq: d.seq,
|
|
2163
|
+
mutationId: Ke(),
|
|
2056
2164
|
collection: t,
|
|
2057
|
-
docId:
|
|
2165
|
+
docId: c,
|
|
2058
2166
|
op: "remove",
|
|
2059
2167
|
payload: null,
|
|
2060
2168
|
baseVersion: i,
|
|
@@ -2070,9 +2178,9 @@ var _e = class {
|
|
|
2070
2178
|
}
|
|
2071
2179
|
let f = {
|
|
2072
2180
|
seq: this.nextSeq(),
|
|
2073
|
-
mutationId:
|
|
2181
|
+
mutationId: Ke(),
|
|
2074
2182
|
collection: t,
|
|
2075
|
-
docId:
|
|
2183
|
+
docId: c,
|
|
2076
2184
|
op: r,
|
|
2077
2185
|
payload: o,
|
|
2078
2186
|
baseVersion: i,
|
|
@@ -2183,55 +2291,55 @@ var _e = class {
|
|
|
2183
2291
|
};
|
|
2184
2292
|
|
|
2185
2293
|
// src/db/field-value.ts
|
|
2186
|
-
var
|
|
2187
|
-
function
|
|
2294
|
+
var ze = "__neetru__";
|
|
2295
|
+
function Br() {
|
|
2188
2296
|
return { __neetru__: "serverTimestamp" };
|
|
2189
2297
|
}
|
|
2190
|
-
function
|
|
2298
|
+
function Kr(n) {
|
|
2191
2299
|
if (typeof n != "number" || !Number.isFinite(n))
|
|
2192
2300
|
throw new TypeError(
|
|
2193
2301
|
`increment(n): n deve ser um n\xFAmero finito (recebido: ${String(n)}).`
|
|
2194
2302
|
);
|
|
2195
2303
|
return { __neetru__: "increment", operand: n };
|
|
2196
2304
|
}
|
|
2197
|
-
function
|
|
2198
|
-
return typeof n == "object" && n !== null &&
|
|
2305
|
+
function H(n) {
|
|
2306
|
+
return typeof n == "object" && n !== null && ze in n && typeof n[ze] == "string";
|
|
2199
2307
|
}
|
|
2200
|
-
function
|
|
2201
|
-
return
|
|
2308
|
+
function Xt(n) {
|
|
2309
|
+
return H(n) && n.__neetru__ === "serverTimestamp";
|
|
2202
2310
|
}
|
|
2203
|
-
function
|
|
2204
|
-
return
|
|
2311
|
+
function Yt(n) {
|
|
2312
|
+
return H(n) && n.__neetru__ === "increment" && typeof n.operand == "number" && Number.isFinite(n.operand);
|
|
2205
2313
|
}
|
|
2206
|
-
function
|
|
2207
|
-
return
|
|
2314
|
+
function zr(n, e) {
|
|
2315
|
+
return Xt(n) ? Date.now() : Yt(n) ? (typeof e == "number" && Number.isFinite(e) ? e : 0) + n.operand : n;
|
|
2208
2316
|
}
|
|
2209
|
-
function
|
|
2317
|
+
function G(n, e) {
|
|
2210
2318
|
let t = false, r = {};
|
|
2211
2319
|
for (let [o, i] of Object.entries(n))
|
|
2212
|
-
|
|
2320
|
+
H(i) ? (t = true, r[o] = zr(i, e?.[o])) : r[o] = i;
|
|
2213
2321
|
return t ? r : n;
|
|
2214
2322
|
}
|
|
2215
|
-
function
|
|
2323
|
+
function He(n) {
|
|
2216
2324
|
for (let e of Object.values(n))
|
|
2217
|
-
if (
|
|
2325
|
+
if (H(e)) return true;
|
|
2218
2326
|
return false;
|
|
2219
2327
|
}
|
|
2220
2328
|
|
|
2221
2329
|
// src/db/offline/sync-engine.ts
|
|
2222
|
-
var
|
|
2223
|
-
function
|
|
2224
|
-
return n !== null &&
|
|
2330
|
+
var Hr = 8, Gr = 1e3, Qr = 300 * 1e3, Ge = "truncatedCollections";
|
|
2331
|
+
function Jr(n) {
|
|
2332
|
+
return n !== null && He(n);
|
|
2225
2333
|
}
|
|
2226
|
-
function
|
|
2334
|
+
function Xr(n) {
|
|
2227
2335
|
if (n === null) return null;
|
|
2228
|
-
if (!
|
|
2336
|
+
if (!He(n)) return n;
|
|
2229
2337
|
let e = {};
|
|
2230
2338
|
for (let [t, r] of Object.entries(n))
|
|
2231
|
-
|
|
2339
|
+
H(r) || (e[t] = r);
|
|
2232
2340
|
return e;
|
|
2233
2341
|
}
|
|
2234
|
-
var
|
|
2342
|
+
var Ie = class {
|
|
2235
2343
|
_store;
|
|
2236
2344
|
_queue;
|
|
2237
2345
|
_resolver;
|
|
@@ -2375,16 +2483,16 @@ var be = class {
|
|
|
2375
2483
|
try {
|
|
2376
2484
|
t = await this._transport.pushMutations(e);
|
|
2377
2485
|
} catch (s) {
|
|
2378
|
-
let a = s instanceof Error ? s.message : String(s),
|
|
2379
|
-
await this._retryOrDeadLetter(u, a,
|
|
2380
|
-
for (let p of
|
|
2486
|
+
let a = s instanceof Error ? s.message : String(s), c = [], [u, ...d] = e;
|
|
2487
|
+
await this._retryOrDeadLetter(u, a, c);
|
|
2488
|
+
for (let p of d)
|
|
2381
2489
|
await this._queue.requeueInflight(p.mutationId);
|
|
2382
|
-
return
|
|
2490
|
+
return c.length > 0 && this._bus.emit(c), this._refreshPendingWrites().catch(() => {
|
|
2383
2491
|
}), false;
|
|
2384
2492
|
}
|
|
2385
2493
|
let r = [];
|
|
2386
2494
|
for (let s of t.results) {
|
|
2387
|
-
let a = e.find((
|
|
2495
|
+
let a = e.find((c) => c.mutationId === s.mutationId);
|
|
2388
2496
|
a && (s.outcome === "confirmed" ? (await this._queue.markApplied(a.mutationId), await this._applySyncedDoc(
|
|
2389
2497
|
a.collection,
|
|
2390
2498
|
a.docId,
|
|
@@ -2402,8 +2510,8 @@ var be = class {
|
|
|
2402
2510
|
"partial_push: muta\xE7\xE3o n\xE3o processada no lote",
|
|
2403
2511
|
r
|
|
2404
2512
|
);
|
|
2405
|
-
for (let
|
|
2406
|
-
await this._queue.requeueInflight(
|
|
2513
|
+
for (let c of a)
|
|
2514
|
+
await this._queue.requeueInflight(c.mutationId);
|
|
2407
2515
|
}
|
|
2408
2516
|
return r.length > 0 && this._bus.emit(r), i.length === 0;
|
|
2409
2517
|
}
|
|
@@ -2425,13 +2533,13 @@ var be = class {
|
|
|
2425
2533
|
}), s.push({ type: "removed", collection: e, doc: { id: t, data: a.data } }));
|
|
2426
2534
|
return;
|
|
2427
2535
|
}
|
|
2428
|
-
let u =
|
|
2536
|
+
let u = Xr(i.payload) ?? a?.data ?? {}, d = {
|
|
2429
2537
|
collection: e,
|
|
2430
2538
|
id: t,
|
|
2431
2539
|
data: i.op === "update" ? { ...a?.data ?? {}, ...u } : (
|
|
2432
2540
|
// add/set: se o payload tinha sentinels, o cache otimista (existing.data)
|
|
2433
2541
|
// é a fonte de verdade até o pull; senão usa o payload literal.
|
|
2434
|
-
|
|
2542
|
+
Jr(i.payload) ? { ...a?.data ?? {}, ...u } : u
|
|
2435
2543
|
),
|
|
2436
2544
|
meta: {
|
|
2437
2545
|
serverVersion: r,
|
|
@@ -2443,10 +2551,10 @@ var be = class {
|
|
|
2443
2551
|
ownerId: a?.meta.ownerId ?? null
|
|
2444
2552
|
}
|
|
2445
2553
|
};
|
|
2446
|
-
await this._store.putDoc(
|
|
2554
|
+
await this._store.putDoc(d), s.push({
|
|
2447
2555
|
type: a ? "modified" : "added",
|
|
2448
2556
|
collection: e,
|
|
2449
|
-
doc: { id: t, data:
|
|
2557
|
+
doc: { id: t, data: d.data }
|
|
2450
2558
|
});
|
|
2451
2559
|
}
|
|
2452
2560
|
/** Processa resultado 'superseded' (LWW: servidor venceu). */
|
|
@@ -2478,7 +2586,7 @@ var be = class {
|
|
|
2478
2586
|
deleted: false,
|
|
2479
2587
|
ownerId: null
|
|
2480
2588
|
}
|
|
2481
|
-
},
|
|
2589
|
+
}, c = this._resolver.resolve(
|
|
2482
2590
|
s,
|
|
2483
2591
|
i,
|
|
2484
2592
|
e
|
|
@@ -2492,7 +2600,7 @@ var be = class {
|
|
|
2492
2600
|
detectedAt: Date.now(),
|
|
2493
2601
|
delivered: false
|
|
2494
2602
|
};
|
|
2495
|
-
await this._store.appendConflict(
|
|
2603
|
+
await this._store.appendConflict(c);
|
|
2496
2604
|
let u = {
|
|
2497
2605
|
collection: e.collection,
|
|
2498
2606
|
id: e.docId,
|
|
@@ -2535,7 +2643,7 @@ var be = class {
|
|
|
2535
2643
|
...i.meta,
|
|
2536
2644
|
serverVersion: t.serverVersion ?? i.meta.serverVersion
|
|
2537
2645
|
}
|
|
2538
|
-
} : null,
|
|
2646
|
+
} : null, c = this._resolver.resolveRejected(
|
|
2539
2647
|
i,
|
|
2540
2648
|
s,
|
|
2541
2649
|
e,
|
|
@@ -2550,7 +2658,7 @@ var be = class {
|
|
|
2550
2658
|
detectedAt: Date.now(),
|
|
2551
2659
|
delivered: false
|
|
2552
2660
|
};
|
|
2553
|
-
await this._store.appendConflict(
|
|
2661
|
+
await this._store.appendConflict(c), t.serverData ? (await this._store.putDoc({
|
|
2554
2662
|
...i,
|
|
2555
2663
|
data: t.serverData,
|
|
2556
2664
|
meta: {
|
|
@@ -2584,7 +2692,7 @@ var be = class {
|
|
|
2584
2692
|
* pending do doc no cache + registra `lastError`/dispara `onError`.
|
|
2585
2693
|
*/
|
|
2586
2694
|
async _retryOrDeadLetter(e, t, r) {
|
|
2587
|
-
if (e.attempts + 1 <
|
|
2695
|
+
if (e.attempts + 1 < Hr) {
|
|
2588
2696
|
await this._queue.markRetry(e.mutationId, t);
|
|
2589
2697
|
return;
|
|
2590
2698
|
}
|
|
@@ -2625,7 +2733,7 @@ var be = class {
|
|
|
2625
2733
|
}
|
|
2626
2734
|
/** Backoff exponencial keyed em `attempts`, com teto `BACKOFF_CAP_MS`. */
|
|
2627
2735
|
_backoffDelayMs(e) {
|
|
2628
|
-
return Math.min(
|
|
2736
|
+
return Math.min(Gr * 2 ** (e - 1), Qr);
|
|
2629
2737
|
}
|
|
2630
2738
|
// ─── FASE 2 — pull (reconciliar o cache) ─────────────────────────────────────
|
|
2631
2739
|
/**
|
|
@@ -2658,16 +2766,16 @@ var be = class {
|
|
|
2658
2766
|
let r = [];
|
|
2659
2767
|
await this._applyServerDocs(t.docs, r, true);
|
|
2660
2768
|
let o = t.truncatedCollections ?? [], i = new Set(o);
|
|
2661
|
-
await this._store.setMeta(
|
|
2769
|
+
await this._store.setMeta(Ge, o);
|
|
2662
2770
|
let s = new Set(
|
|
2663
2771
|
t.docs.map((a) => `${a.collection}::${a.id}`)
|
|
2664
2772
|
);
|
|
2665
2773
|
for (let a of e) {
|
|
2666
2774
|
if (i.has(a)) continue;
|
|
2667
|
-
let
|
|
2668
|
-
for (let { id: u } of
|
|
2669
|
-
let
|
|
2670
|
-
if (!s.has(
|
|
2775
|
+
let c = await this._store.listDocs(a, {});
|
|
2776
|
+
for (let { id: u } of c.docs) {
|
|
2777
|
+
let d = `${a}::${u}`;
|
|
2778
|
+
if (!s.has(d)) {
|
|
2671
2779
|
let p = await this._store.getDoc(a, u);
|
|
2672
2780
|
p && !p.meta.deleted && (await this._store.putDoc({
|
|
2673
2781
|
...p,
|
|
@@ -2715,7 +2823,7 @@ var be = class {
|
|
|
2715
2823
|
continue;
|
|
2716
2824
|
}
|
|
2717
2825
|
if (i && i.meta.state === "pending") {
|
|
2718
|
-
let
|
|
2826
|
+
let c = {
|
|
2719
2827
|
collection: o.collection,
|
|
2720
2828
|
id: o.id,
|
|
2721
2829
|
data: o.data,
|
|
@@ -2728,11 +2836,11 @@ var be = class {
|
|
|
2728
2836
|
deleted: false,
|
|
2729
2837
|
ownerId: i.meta.ownerId
|
|
2730
2838
|
}
|
|
2731
|
-
},
|
|
2839
|
+
}, d = (await this._queue.listAll()).find(
|
|
2732
2840
|
(p) => p.collection === o.collection && p.docId === o.id
|
|
2733
2841
|
);
|
|
2734
|
-
if (
|
|
2735
|
-
let p = this._resolver.resolve(i,
|
|
2842
|
+
if (d) {
|
|
2843
|
+
let p = this._resolver.resolve(i, c, d);
|
|
2736
2844
|
p.conflictRecord && await this._store.appendConflict(p.conflictRecord), await this._store.putDoc({
|
|
2737
2845
|
...i,
|
|
2738
2846
|
meta: { ...i.meta, state: "conflict" }
|
|
@@ -2877,10 +2985,10 @@ var be = class {
|
|
|
2877
2985
|
};
|
|
2878
2986
|
|
|
2879
2987
|
// src/db/offline/conflict-resolver.ts
|
|
2880
|
-
function
|
|
2988
|
+
function Zt(n, e) {
|
|
2881
2989
|
return e === null ? false : n === null ? true : e > n;
|
|
2882
2990
|
}
|
|
2883
|
-
var
|
|
2991
|
+
var Ce = class n {
|
|
2884
2992
|
/**
|
|
2885
2993
|
* Resolve um conflito com base na operação da mutação (auto-dispatch).
|
|
2886
2994
|
*
|
|
@@ -2914,7 +3022,7 @@ var ve = class n {
|
|
|
2914
3022
|
resolvedData: t.data,
|
|
2915
3023
|
conflictRecord: null
|
|
2916
3024
|
};
|
|
2917
|
-
if (!
|
|
3025
|
+
if (!Zt(
|
|
2918
3026
|
e.meta.updatedAtServer,
|
|
2919
3027
|
t.meta.updatedAtServer
|
|
2920
3028
|
))
|
|
@@ -2961,7 +3069,7 @@ var ve = class n {
|
|
|
2961
3069
|
let i = t.data;
|
|
2962
3070
|
if (e === null)
|
|
2963
3071
|
return { outcome: "no_conflict", resolvedData: { ...i, ...r.payload ?? {} }, conflictRecord: null };
|
|
2964
|
-
if (!
|
|
3072
|
+
if (!Zt(
|
|
2965
3073
|
e.meta.updatedAtServer,
|
|
2966
3074
|
t.meta.updatedAtServer
|
|
2967
3075
|
))
|
|
@@ -2969,7 +3077,7 @@ var ve = class n {
|
|
|
2969
3077
|
let a = {};
|
|
2970
3078
|
for (let u of o)
|
|
2971
3079
|
a[u] = e.data[u];
|
|
2972
|
-
let
|
|
3080
|
+
let c = {
|
|
2973
3081
|
collection: r.collection,
|
|
2974
3082
|
docId: r.docId,
|
|
2975
3083
|
mutationId: r.mutationId,
|
|
@@ -2982,7 +3090,7 @@ var ve = class n {
|
|
|
2982
3090
|
return {
|
|
2983
3091
|
outcome: "server_wins",
|
|
2984
3092
|
resolvedData: i,
|
|
2985
|
-
conflictRecord:
|
|
3093
|
+
conflictRecord: c
|
|
2986
3094
|
};
|
|
2987
3095
|
}
|
|
2988
3096
|
/**
|
|
@@ -3039,10 +3147,10 @@ var ve = class n {
|
|
|
3039
3147
|
};
|
|
3040
3148
|
|
|
3041
3149
|
// src/db/offline/change-bus.ts
|
|
3042
|
-
function
|
|
3150
|
+
function Yr(n) {
|
|
3043
3151
|
return `${n.collection}::${n.doc.id}`;
|
|
3044
3152
|
}
|
|
3045
|
-
var
|
|
3153
|
+
var Ae = class {
|
|
3046
3154
|
/** Listeners filtrados por coleção. */
|
|
3047
3155
|
_collectionListeners = /* @__PURE__ */ new Map();
|
|
3048
3156
|
/** Listeners globais (todas as coleções). */
|
|
@@ -3087,15 +3195,15 @@ var we = class {
|
|
|
3087
3195
|
if (e.length === 0) return;
|
|
3088
3196
|
let t = /* @__PURE__ */ new Map();
|
|
3089
3197
|
for (let i of e)
|
|
3090
|
-
t.set(
|
|
3198
|
+
t.set(Yr(i), i);
|
|
3091
3199
|
let r = Array.from(t.values()), o = /* @__PURE__ */ new Map();
|
|
3092
3200
|
for (let i of r)
|
|
3093
3201
|
o.has(i.collection) || o.set(i.collection, []), o.get(i.collection).push(i);
|
|
3094
3202
|
for (let [i, s] of o) {
|
|
3095
3203
|
let a = this._collectionListeners.get(i);
|
|
3096
3204
|
if (a)
|
|
3097
|
-
for (let
|
|
3098
|
-
this._safeCall(
|
|
3205
|
+
for (let c of a)
|
|
3206
|
+
this._safeCall(c, s);
|
|
3099
3207
|
}
|
|
3100
3208
|
for (let i of this._globalListeners)
|
|
3101
3209
|
this._safeCall(i, r);
|
|
@@ -3126,7 +3234,7 @@ var we = class {
|
|
|
3126
3234
|
};
|
|
3127
3235
|
|
|
3128
3236
|
// src/db/offline/connectivity-monitor.ts
|
|
3129
|
-
var
|
|
3237
|
+
var Ee = class {
|
|
3130
3238
|
_state;
|
|
3131
3239
|
_listeners = /* @__PURE__ */ new Set();
|
|
3132
3240
|
_nav;
|
|
@@ -3228,7 +3336,7 @@ var ke = class {
|
|
|
3228
3336
|
};
|
|
3229
3337
|
|
|
3230
3338
|
// src/db/offline/tab-coordinator.ts
|
|
3231
|
-
var
|
|
3339
|
+
var ie = class {
|
|
3232
3340
|
_lockName;
|
|
3233
3341
|
_channelName;
|
|
3234
3342
|
_lockManager;
|
|
@@ -3406,25 +3514,25 @@ var ne = class {
|
|
|
3406
3514
|
|
|
3407
3515
|
// src/db/collection-ref.ts
|
|
3408
3516
|
z();
|
|
3409
|
-
function
|
|
3517
|
+
function Zr() {
|
|
3410
3518
|
return typeof indexedDB > "u" || typeof window > "u";
|
|
3411
3519
|
}
|
|
3412
|
-
var
|
|
3413
|
-
function
|
|
3414
|
-
if (!
|
|
3520
|
+
var en = /^[a-z0-9][a-z0-9_-]{0,62}$/;
|
|
3521
|
+
function eo(n) {
|
|
3522
|
+
if (!en.test(n))
|
|
3415
3523
|
throw new exports.NeetruDbError(
|
|
3416
3524
|
"db_invalid_query",
|
|
3417
|
-
`Nome de cole\xE7\xE3o inv\xE1lido: "${n}". Deve seguir o padr\xE3o ${
|
|
3525
|
+
`Nome de cole\xE7\xE3o inv\xE1lido: "${n}". Deve seguir o padr\xE3o ${en}.`
|
|
3418
3526
|
);
|
|
3419
3527
|
}
|
|
3420
|
-
function
|
|
3528
|
+
function Y(n, e = "id") {
|
|
3421
3529
|
if (!n || typeof n != "string" || n.trim() === "")
|
|
3422
3530
|
throw new exports.NeetruDbError(
|
|
3423
3531
|
"db_invalid_query",
|
|
3424
3532
|
`${e} \xE9 obrigat\xF3rio e deve ser uma string n\xE3o-vazia.`
|
|
3425
3533
|
);
|
|
3426
3534
|
}
|
|
3427
|
-
function
|
|
3535
|
+
function to(n) {
|
|
3428
3536
|
let e = {};
|
|
3429
3537
|
if (n?.where && (e.where = n.where.map((t) => ({
|
|
3430
3538
|
field: t.field,
|
|
@@ -3441,12 +3549,12 @@ function Ar(n) {
|
|
|
3441
3549
|
}
|
|
3442
3550
|
return e;
|
|
3443
3551
|
}
|
|
3444
|
-
function
|
|
3552
|
+
function no(n, e) {
|
|
3445
3553
|
if (n.length < e) return null;
|
|
3446
3554
|
let t = n[n.length - 1];
|
|
3447
3555
|
return t ? btoa(JSON.stringify({ type: "startAfter", docId: t.id })) : null;
|
|
3448
3556
|
}
|
|
3449
|
-
var
|
|
3557
|
+
var Qe = class {
|
|
3450
3558
|
constructor(e, t, r, o, i, s) {
|
|
3451
3559
|
this._collection = e;
|
|
3452
3560
|
this._store = t;
|
|
@@ -3488,26 +3596,26 @@ var $e = class {
|
|
|
3488
3596
|
}
|
|
3489
3597
|
/** Constrói um DbListResult para a coleção com query opcional. */
|
|
3490
3598
|
async _buildListResult(e) {
|
|
3491
|
-
let t =
|
|
3599
|
+
let t = to(e), r = t.limit ?? 20, o = await this._store.listDocs(this._collection, t), i = await this._hasPendingWrites(), s = this._isOnline(), a = this._engine.getSyncState(), c = no(o.docs, r), u = await this._store.getMeta(Ge), d = Array.isArray(u) && u.includes(this._collection), p = !s || a.status === "offline" || d, f = i || a.lastSyncedAt === null || p;
|
|
3492
3600
|
return {
|
|
3493
3601
|
docs: o.docs.map((m) => ({ id: m.id, data: m.data })),
|
|
3494
|
-
nextCursor:
|
|
3602
|
+
nextCursor: c,
|
|
3495
3603
|
fromCache: f,
|
|
3496
3604
|
stale: p,
|
|
3497
3605
|
hasPendingWrites: i,
|
|
3498
3606
|
changes: [],
|
|
3499
|
-
truncated:
|
|
3607
|
+
truncated: d
|
|
3500
3608
|
};
|
|
3501
3609
|
}
|
|
3502
3610
|
// ── CRUD ────────────────────────────────────────────────────────────────────
|
|
3503
3611
|
async get(e) {
|
|
3504
|
-
return
|
|
3612
|
+
return Y(e), this._buildGetResult(e);
|
|
3505
3613
|
}
|
|
3506
3614
|
async list(e) {
|
|
3507
3615
|
return this._buildListResult(e);
|
|
3508
3616
|
}
|
|
3509
3617
|
async add(e) {
|
|
3510
|
-
let t = e, r =
|
|
3618
|
+
let t = e, r = G(t, void 0), o = await this._queue.enqueue({
|
|
3511
3619
|
collection: this._collection,
|
|
3512
3620
|
op: "add",
|
|
3513
3621
|
payload: t,
|
|
@@ -3534,8 +3642,8 @@ var $e = class {
|
|
|
3534
3642
|
}]), { ok: true, id: i };
|
|
3535
3643
|
}
|
|
3536
3644
|
async set(e, t) {
|
|
3537
|
-
|
|
3538
|
-
let r = await this._store.getDoc(this._collection, e), o = t, i =
|
|
3645
|
+
Y(e);
|
|
3646
|
+
let r = await this._store.getDoc(this._collection, e), o = t, i = G(o, void 0), s = await this._queue.enqueue({
|
|
3539
3647
|
collection: this._collection,
|
|
3540
3648
|
docId: e,
|
|
3541
3649
|
op: "set",
|
|
@@ -3563,8 +3671,8 @@ var $e = class {
|
|
|
3563
3671
|
}]), { ok: true };
|
|
3564
3672
|
}
|
|
3565
3673
|
async update(e, t, r) {
|
|
3566
|
-
|
|
3567
|
-
let o = await this._store.getDoc(this._collection, e), i = t, s = o?.meta.deleted ? void 0 : o?.data, a =
|
|
3674
|
+
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), c = o?.meta.deleted ? { ...a } : { ...o?.data ?? {}, ...a }, u = await this._queue.enqueue({
|
|
3568
3676
|
collection: this._collection,
|
|
3569
3677
|
docId: e,
|
|
3570
3678
|
op: "update",
|
|
@@ -3577,7 +3685,7 @@ var $e = class {
|
|
|
3577
3685
|
return await this._store.putDoc({
|
|
3578
3686
|
collection: this._collection,
|
|
3579
3687
|
id: e,
|
|
3580
|
-
data:
|
|
3688
|
+
data: c,
|
|
3581
3689
|
meta: {
|
|
3582
3690
|
serverVersion: o?.meta.serverVersion ?? null,
|
|
3583
3691
|
updatedAtServer: o?.meta.updatedAtServer ?? null,
|
|
@@ -3593,11 +3701,11 @@ var $e = class {
|
|
|
3593
3701
|
}), this._bus.emit([{
|
|
3594
3702
|
type: "modified",
|
|
3595
3703
|
collection: this._collection,
|
|
3596
|
-
doc: { id: e, data:
|
|
3704
|
+
doc: { id: e, data: c }
|
|
3597
3705
|
}]), { ok: true };
|
|
3598
3706
|
}
|
|
3599
3707
|
async remove(e) {
|
|
3600
|
-
|
|
3708
|
+
Y(e);
|
|
3601
3709
|
let t = await this._store.getDoc(this._collection, e);
|
|
3602
3710
|
return !t || t.meta.deleted, await this._queue.enqueue({
|
|
3603
3711
|
collection: this._collection,
|
|
@@ -3617,13 +3725,13 @@ var $e = class {
|
|
|
3617
3725
|
for (let o of e) {
|
|
3618
3726
|
let i = this._collection;
|
|
3619
3727
|
if (o.kind === "add") {
|
|
3620
|
-
let s = o.data, a =
|
|
3728
|
+
let s = o.data, a = G(s, void 0), c = await this._queue.enqueue({
|
|
3621
3729
|
collection: i,
|
|
3622
3730
|
op: "add",
|
|
3623
3731
|
payload: s,
|
|
3624
3732
|
baseVersion: null,
|
|
3625
3733
|
batchId: t
|
|
3626
|
-
}), u =
|
|
3734
|
+
}), u = c.docId;
|
|
3627
3735
|
await this._store.putDoc({
|
|
3628
3736
|
collection: i,
|
|
3629
3737
|
id: u,
|
|
@@ -3633,18 +3741,18 @@ var $e = class {
|
|
|
3633
3741
|
updatedAtServer: null,
|
|
3634
3742
|
updatedAtLocal: Date.now(),
|
|
3635
3743
|
state: "pending",
|
|
3636
|
-
pendingMutationIds: [
|
|
3744
|
+
pendingMutationIds: [c.mutationId],
|
|
3637
3745
|
deleted: false,
|
|
3638
3746
|
ownerId: null
|
|
3639
3747
|
}
|
|
3640
3748
|
}), r.push({ type: "added", collection: i, doc: { id: u, data: a } });
|
|
3641
3749
|
} else if (o.kind === "set") {
|
|
3642
|
-
let s = o.id, a = o.data,
|
|
3750
|
+
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), u = G(a, void 0), d = await this._queue.enqueue({
|
|
3643
3751
|
collection: i,
|
|
3644
3752
|
docId: s,
|
|
3645
3753
|
op: "set",
|
|
3646
3754
|
payload: a,
|
|
3647
|
-
baseVersion:
|
|
3755
|
+
baseVersion: c?.meta.serverVersion ?? null,
|
|
3648
3756
|
batchId: t
|
|
3649
3757
|
});
|
|
3650
3758
|
await this._store.putDoc({
|
|
@@ -3652,26 +3760,26 @@ var $e = class {
|
|
|
3652
3760
|
id: s,
|
|
3653
3761
|
data: u,
|
|
3654
3762
|
meta: {
|
|
3655
|
-
serverVersion:
|
|
3656
|
-
updatedAtServer:
|
|
3763
|
+
serverVersion: c?.meta.serverVersion ?? null,
|
|
3764
|
+
updatedAtServer: c?.meta.updatedAtServer ?? null,
|
|
3657
3765
|
updatedAtLocal: Date.now(),
|
|
3658
3766
|
state: "pending",
|
|
3659
|
-
pendingMutationIds: [
|
|
3767
|
+
pendingMutationIds: [d.mutationId],
|
|
3660
3768
|
deleted: false,
|
|
3661
3769
|
ownerId: null
|
|
3662
3770
|
}
|
|
3663
3771
|
}), r.push({
|
|
3664
|
-
type:
|
|
3772
|
+
type: c && !c.meta.deleted ? "modified" : "added",
|
|
3665
3773
|
collection: i,
|
|
3666
3774
|
doc: { id: s, data: u }
|
|
3667
3775
|
});
|
|
3668
3776
|
} else if (o.kind === "update") {
|
|
3669
|
-
let s = o.id, a = o.data,
|
|
3777
|
+
let s = o.id, a = o.data, c = await this._store.getDoc(i, s), u = c?.meta.deleted ? void 0 : c?.data, d = G(a, u), p = { ...c?.data ?? {}, ...d }, f = await this._queue.enqueue({
|
|
3670
3778
|
collection: i,
|
|
3671
3779
|
docId: s,
|
|
3672
3780
|
op: "update",
|
|
3673
3781
|
payload: a,
|
|
3674
|
-
baseVersion:
|
|
3782
|
+
baseVersion: c?.meta.serverVersion ?? null,
|
|
3675
3783
|
batchId: t,
|
|
3676
3784
|
// bug_0273e4dd: propaga merge-upsert (writeBatch.set(merge) cria-se-ausente).
|
|
3677
3785
|
upsert: o.upsert
|
|
@@ -3681,16 +3789,16 @@ var $e = class {
|
|
|
3681
3789
|
id: s,
|
|
3682
3790
|
data: p,
|
|
3683
3791
|
meta: {
|
|
3684
|
-
serverVersion:
|
|
3685
|
-
updatedAtServer:
|
|
3792
|
+
serverVersion: c?.meta.serverVersion ?? null,
|
|
3793
|
+
updatedAtServer: c?.meta.updatedAtServer ?? null,
|
|
3686
3794
|
updatedAtLocal: Date.now(),
|
|
3687
3795
|
state: "pending",
|
|
3688
3796
|
pendingMutationIds: [
|
|
3689
|
-
...
|
|
3797
|
+
...c?.meta.pendingMutationIds ?? [],
|
|
3690
3798
|
f.mutationId
|
|
3691
3799
|
],
|
|
3692
3800
|
deleted: false,
|
|
3693
|
-
ownerId:
|
|
3801
|
+
ownerId: c?.meta.ownerId ?? null
|
|
3694
3802
|
}
|
|
3695
3803
|
}), r.push({ type: "modified", collection: i, doc: { id: s, data: p } });
|
|
3696
3804
|
} else if (o.kind === "remove") {
|
|
@@ -3709,7 +3817,7 @@ var $e = class {
|
|
|
3709
3817
|
}
|
|
3710
3818
|
// ── Realtime ─────────────────────────────────────────────────────────────────
|
|
3711
3819
|
onDoc(e, t) {
|
|
3712
|
-
return
|
|
3820
|
+
return Y(e), this._buildGetResult(e).then((r) => {
|
|
3713
3821
|
t(r ? r.docs[0]?.data ?? null : null);
|
|
3714
3822
|
}).catch(() => t(null)), this._bus.subscribe(this._collection, (r) => {
|
|
3715
3823
|
let o = r.find((i) => i.doc.id === e);
|
|
@@ -3721,11 +3829,11 @@ var $e = class {
|
|
|
3721
3829
|
});
|
|
3722
3830
|
let r = this._bus.subscribe(this._collection, async (s) => {
|
|
3723
3831
|
try {
|
|
3724
|
-
let a = await this._buildListResult(e),
|
|
3832
|
+
let a = await this._buildListResult(e), c = s.map((u) => ({
|
|
3725
3833
|
type: u.type,
|
|
3726
3834
|
doc: { id: u.doc.id, data: u.doc.data }
|
|
3727
3835
|
}));
|
|
3728
|
-
t({ ...a, changes:
|
|
3836
|
+
t({ ...a, changes: c });
|
|
3729
3837
|
} catch {
|
|
3730
3838
|
}
|
|
3731
3839
|
}), o = this._engine.onSyncStateChange(async () => {
|
|
@@ -3739,15 +3847,15 @@ var $e = class {
|
|
|
3739
3847
|
this._collection,
|
|
3740
3848
|
async (s, a) => {
|
|
3741
3849
|
if (!a)
|
|
3742
|
-
for (let
|
|
3743
|
-
|
|
3850
|
+
for (let c of s)
|
|
3851
|
+
c.type === "removed" || c.data === null ? (await this._store.deleteDoc(this._collection, c.docId), this._bus.emit([{
|
|
3744
3852
|
type: "removed",
|
|
3745
3853
|
collection: this._collection,
|
|
3746
|
-
doc: { id:
|
|
3854
|
+
doc: { id: c.docId, data: {} }
|
|
3747
3855
|
}])) : (await this._store.putDoc({
|
|
3748
3856
|
collection: this._collection,
|
|
3749
|
-
id:
|
|
3750
|
-
data:
|
|
3857
|
+
id: c.docId,
|
|
3858
|
+
data: c.data,
|
|
3751
3859
|
meta: {
|
|
3752
3860
|
serverVersion: `rt_${Date.now()}`,
|
|
3753
3861
|
updatedAtServer: Date.now(),
|
|
@@ -3758,9 +3866,9 @@ var $e = class {
|
|
|
3758
3866
|
ownerId: null
|
|
3759
3867
|
}
|
|
3760
3868
|
}), this._bus.emit([{
|
|
3761
|
-
type:
|
|
3869
|
+
type: c.type,
|
|
3762
3870
|
collection: this._collection,
|
|
3763
|
-
doc: { id:
|
|
3871
|
+
doc: { id: c.docId, data: c.data }
|
|
3764
3872
|
}]));
|
|
3765
3873
|
}
|
|
3766
3874
|
)), () => {
|
|
@@ -3768,7 +3876,7 @@ var $e = class {
|
|
|
3768
3876
|
};
|
|
3769
3877
|
}
|
|
3770
3878
|
doc(e) {
|
|
3771
|
-
|
|
3879
|
+
Y(e);
|
|
3772
3880
|
let t = this;
|
|
3773
3881
|
return {
|
|
3774
3882
|
get id() {
|
|
@@ -3795,14 +3903,14 @@ var $e = class {
|
|
|
3795
3903
|
onSnapshot(r) {
|
|
3796
3904
|
t._buildGetResult(e).then((s) => r(s)).catch(() => r(null));
|
|
3797
3905
|
let o = t._bus.subscribe(t._collection, async (s) => {
|
|
3798
|
-
let a = s.find((
|
|
3906
|
+
let a = s.find((c) => c.doc.id === e);
|
|
3799
3907
|
if (a)
|
|
3800
3908
|
try {
|
|
3801
3909
|
if (a.type === "removed")
|
|
3802
3910
|
r(null);
|
|
3803
3911
|
else {
|
|
3804
|
-
let
|
|
3805
|
-
r(
|
|
3912
|
+
let c = await t._buildGetResult(e);
|
|
3913
|
+
r(c);
|
|
3806
3914
|
}
|
|
3807
3915
|
} catch {
|
|
3808
3916
|
r(null);
|
|
@@ -3821,7 +3929,7 @@ var $e = class {
|
|
|
3821
3929
|
}
|
|
3822
3930
|
};
|
|
3823
3931
|
}
|
|
3824
|
-
},
|
|
3932
|
+
}, Je = class {
|
|
3825
3933
|
_store;
|
|
3826
3934
|
_queue;
|
|
3827
3935
|
_bus;
|
|
@@ -3831,7 +3939,7 @@ var $e = class {
|
|
|
3831
3939
|
this._store = e.store, this._queue = e.queue, this._bus = e.bus, this._engine = e.engine, this._connectivity = e.connectivity;
|
|
3832
3940
|
}
|
|
3833
3941
|
collection(e) {
|
|
3834
|
-
return
|
|
3942
|
+
return eo(e), new Qe(
|
|
3835
3943
|
e,
|
|
3836
3944
|
this._store,
|
|
3837
3945
|
this._queue,
|
|
@@ -3860,23 +3968,23 @@ var $e = class {
|
|
|
3860
3968
|
return this._engine.onError(e);
|
|
3861
3969
|
}
|
|
3862
3970
|
};
|
|
3863
|
-
async function
|
|
3864
|
-
let e =
|
|
3971
|
+
async function tn(n) {
|
|
3972
|
+
let e = Zr() ? new Te() : new Se(n.dbName);
|
|
3865
3973
|
await e.open();
|
|
3866
|
-
let t = new
|
|
3974
|
+
let t = new Re(e), r = new Ce(), o = new Ae(), i = new Ee({
|
|
3867
3975
|
// Se startOnline=false, injeta um navigator falso
|
|
3868
3976
|
navigator: n.startOnline === false ? { onLine: false } : typeof navigator < "u" ? navigator : void 0
|
|
3869
3977
|
}), s = n.singleTab ? (
|
|
3870
3978
|
// Fake tab coordinator para single-tab mode (sempre líder)
|
|
3871
3979
|
{
|
|
3872
3980
|
isLeader: () => true,
|
|
3873
|
-
onRoleChange: (
|
|
3981
|
+
onRoleChange: (c) => () => {
|
|
3874
3982
|
}
|
|
3875
3983
|
}
|
|
3876
|
-
) : new
|
|
3984
|
+
) : new ie({
|
|
3877
3985
|
lockName: `neetru-offline:${n.dbName}`,
|
|
3878
3986
|
channelName: `neetru-offline:${n.dbName}`
|
|
3879
|
-
}), a = new
|
|
3987
|
+
}), a = new Ie({
|
|
3880
3988
|
store: e,
|
|
3881
3989
|
queue: t,
|
|
3882
3990
|
resolver: r,
|
|
@@ -3886,7 +3994,7 @@ async function $t(n) {
|
|
|
3886
3994
|
connectivity: i,
|
|
3887
3995
|
periodicSyncIntervalMs: n.periodicSyncIntervalMs ?? 300 * 1e3
|
|
3888
3996
|
});
|
|
3889
|
-
return i.start(), !n.singleTab && s instanceof
|
|
3997
|
+
return i.start(), !n.singleTab && s instanceof ie && s.start(), new Je({
|
|
3890
3998
|
store: e,
|
|
3891
3999
|
queue: t,
|
|
3892
4000
|
bus: o,
|
|
@@ -3896,11 +4004,11 @@ async function $t(n) {
|
|
|
3896
4004
|
}
|
|
3897
4005
|
|
|
3898
4006
|
// src/db/realtime/realtime-client.ts
|
|
3899
|
-
var
|
|
3900
|
-
function
|
|
3901
|
-
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++
|
|
4007
|
+
var ro = 0;
|
|
4008
|
+
function nn() {
|
|
4009
|
+
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `nrt-sub-${Date.now()}-${++ro}-${Math.random().toString(36).slice(2)}`;
|
|
3902
4010
|
}
|
|
3903
|
-
var
|
|
4011
|
+
var Pe = class {
|
|
3904
4012
|
// ── configuração ──────────────────────────────────────────────────────────
|
|
3905
4013
|
_gatewayUrl;
|
|
3906
4014
|
_wsFactory;
|
|
@@ -3938,7 +4046,7 @@ var Se = class {
|
|
|
3938
4046
|
_pendingFrames = [];
|
|
3939
4047
|
// ── construtor ────────────────────────────────────────────────────────────
|
|
3940
4048
|
constructor(e) {
|
|
3941
|
-
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ??
|
|
4049
|
+
this._gatewayUrl = e.gatewayUrl, this._wsFactory = e.webSocketFactory ?? oo, 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();
|
|
3942
4050
|
}
|
|
3943
4051
|
// ── API pública ───────────────────────────────────────────────────────────
|
|
3944
4052
|
/**
|
|
@@ -3952,8 +4060,8 @@ var Se = class {
|
|
|
3952
4060
|
*/
|
|
3953
4061
|
subscribe(e, t, r) {
|
|
3954
4062
|
if (this._closed)
|
|
3955
|
-
return
|
|
3956
|
-
let o =
|
|
4063
|
+
return nn();
|
|
4064
|
+
let o = nn();
|
|
3957
4065
|
this._subscriptions.set(o, {
|
|
3958
4066
|
subscriptionId: o,
|
|
3959
4067
|
collection: e,
|
|
@@ -4217,7 +4325,7 @@ var Se = class {
|
|
|
4217
4325
|
_detachHandlers(e) {
|
|
4218
4326
|
e.onopen = null, e.onmessage = null, e.onclose = null, e.onerror = null;
|
|
4219
4327
|
}
|
|
4220
|
-
},
|
|
4328
|
+
}, oo = (n) => {
|
|
4221
4329
|
if (typeof WebSocket > "u")
|
|
4222
4330
|
throw new Error(
|
|
4223
4331
|
"[NeetruRealtimeClient] WebSocket n\xE3o dispon\xEDvel neste ambiente. Injete uma implementa\xE7\xE3o via `webSocketFactory` nas op\xE7\xF5es."
|
|
@@ -4226,11 +4334,11 @@ var Se = class {
|
|
|
4226
4334
|
};
|
|
4227
4335
|
|
|
4228
4336
|
// src/db/client-db.ts
|
|
4229
|
-
var
|
|
4230
|
-
function
|
|
4231
|
-
return e._transport ? e._transport : n === "firestore" && e.firestoreTransport ? e.firestoreTransport : n === "nosql-vm" && e.realtimeGatewayUrl ?
|
|
4337
|
+
var go = (n) => `/api/sdk/v1/datastore/${encodeURIComponent(n)}`, Ne = (n, e) => `/api/sdk/v1/datastore/${encodeURIComponent(n)}/${encodeURIComponent(e)}`, mn = 200;
|
|
4338
|
+
function yo(n, e, t) {
|
|
4339
|
+
return e._transport ? e._transport : n === "firestore" && e.firestoreTransport ? e.firestoreTransport : n === "nosql-vm" && e.realtimeGatewayUrl ? _o(e.realtimeGatewayUrl, t, e) : gn(t);
|
|
4232
4340
|
}
|
|
4233
|
-
function
|
|
4341
|
+
function gn(n) {
|
|
4234
4342
|
return {
|
|
4235
4343
|
async pushMutations(e) {
|
|
4236
4344
|
if (!n)
|
|
@@ -4238,48 +4346,48 @@ function nn(n) {
|
|
|
4238
4346
|
"db_unavailable",
|
|
4239
4347
|
"[RestSyncTransport] config n\xE3o dispon\xEDvel \u2014 n\xE3o \xE9 poss\xEDvel enviar ao Core. Inicialize o transporte via createNeetruDb."
|
|
4240
4348
|
);
|
|
4241
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4349
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (q(), ne)), r = [], o = n.tenantId ?? n.productId, i = o ? { "x-neetru-tenant": o } : void 0;
|
|
4242
4350
|
for (let s of e)
|
|
4243
4351
|
try {
|
|
4244
4352
|
let a;
|
|
4245
4353
|
s.op === "add" ? a = await t(n, {
|
|
4246
4354
|
method: "PUT",
|
|
4247
|
-
path:
|
|
4355
|
+
path: Ne(s.collection, s.docId),
|
|
4248
4356
|
body: { data: s.payload ?? {} },
|
|
4249
4357
|
requireAuth: !0,
|
|
4250
4358
|
retries: 2,
|
|
4251
4359
|
headers: i
|
|
4252
4360
|
}) : s.op === "set" ? a = await t(n, {
|
|
4253
4361
|
method: "PUT",
|
|
4254
|
-
path:
|
|
4362
|
+
path: Ne(s.collection, s.docId),
|
|
4255
4363
|
body: { data: s.payload ?? {} },
|
|
4256
4364
|
requireAuth: !0,
|
|
4257
4365
|
retries: 2,
|
|
4258
4366
|
headers: i
|
|
4259
4367
|
}) : s.op === "update" ? a = await t(n, {
|
|
4260
4368
|
method: "PATCH",
|
|
4261
|
-
path:
|
|
4369
|
+
path: Ne(s.collection, s.docId),
|
|
4262
4370
|
body: { data: s.payload ?? {}, ...s.upsert ? { upsert: !0 } : {} },
|
|
4263
4371
|
requireAuth: !0,
|
|
4264
4372
|
retries: 2,
|
|
4265
4373
|
headers: i
|
|
4266
4374
|
}) : a = await t(n, {
|
|
4267
4375
|
method: "DELETE",
|
|
4268
|
-
path:
|
|
4376
|
+
path: Ne(s.collection, s.docId),
|
|
4269
4377
|
requireAuth: !0,
|
|
4270
4378
|
retries: 2,
|
|
4271
4379
|
headers: i
|
|
4272
4380
|
});
|
|
4273
|
-
let
|
|
4381
|
+
let c = a;
|
|
4274
4382
|
r.push({
|
|
4275
4383
|
mutationId: s.mutationId,
|
|
4276
4384
|
outcome: "confirmed",
|
|
4277
|
-
serverVersion:
|
|
4385
|
+
serverVersion: c?.serverVersion ?? c?.id ?? `rest_${Date.now()}`,
|
|
4278
4386
|
serverTimestamp: Date.now()
|
|
4279
4387
|
});
|
|
4280
4388
|
} catch (a) {
|
|
4281
|
-
let
|
|
4282
|
-
if (u === "forbidden" || u === "unauthorized" || u === "not_found" || u === "conflict" || u === "validation_failed" || typeof
|
|
4389
|
+
let c = a instanceof Error ? a.message : String(a), u = a?.code, d = a?.status;
|
|
4390
|
+
if (u === "forbidden" || u === "unauthorized" || u === "not_found" || u === "conflict" || u === "validation_failed" || typeof d == "number" && d >= 400 && d < 500 && d !== 408 && d !== 429) {
|
|
4283
4391
|
let f = u === "forbidden" || u === "unauthorized" ? "rejected_permission" : "rejected_validation";
|
|
4284
4392
|
r.push({
|
|
4285
4393
|
mutationId: s.mutationId,
|
|
@@ -4291,7 +4399,7 @@ function nn(n) {
|
|
|
4291
4399
|
continue;
|
|
4292
4400
|
}
|
|
4293
4401
|
if (r.length === 0)
|
|
4294
|
-
throw new exports.NeetruDbError("db_unavailable", `pushMutations falhou: ${
|
|
4402
|
+
throw new exports.NeetruDbError("db_unavailable", `pushMutations falhou: ${c}`);
|
|
4295
4403
|
break;
|
|
4296
4404
|
}
|
|
4297
4405
|
return { results: r };
|
|
@@ -4302,13 +4410,13 @@ function nn(n) {
|
|
|
4302
4410
|
async fullResync(e) {
|
|
4303
4411
|
if (!n)
|
|
4304
4412
|
return { docs: [], newWatermark: Date.now() };
|
|
4305
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4306
|
-
for (let
|
|
4413
|
+
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 c of e) {
|
|
4307
4415
|
let u;
|
|
4308
4416
|
try {
|
|
4309
4417
|
u = await t(n, {
|
|
4310
4418
|
method: "GET",
|
|
4311
|
-
path:
|
|
4419
|
+
path: go(c),
|
|
4312
4420
|
requireAuth: !0,
|
|
4313
4421
|
retries: 2,
|
|
4314
4422
|
headers: o,
|
|
@@ -4316,37 +4424,37 @@ function nn(n) {
|
|
|
4316
4424
|
// TRUNCADAS — reportadas em `truncatedCollections` p/ o SyncEngine
|
|
4317
4425
|
// (a) NÃO rodar deleção-por-ausência e (b) sinalizar
|
|
4318
4426
|
// `DbListResult.truncated`. Paginação por cursor é tarefa Core (gated).
|
|
4319
|
-
query: { limit:
|
|
4427
|
+
query: { limit: mn }
|
|
4320
4428
|
});
|
|
4321
4429
|
} catch (f) {
|
|
4322
4430
|
let m = f instanceof Error ? f.message : String(f);
|
|
4323
4431
|
throw new exports.NeetruDbError(
|
|
4324
4432
|
"db_unavailable",
|
|
4325
|
-
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${
|
|
4433
|
+
`[RestSyncTransport] fullResync falhou na cole\xE7\xE3o "${c}": ${m}`
|
|
4326
4434
|
);
|
|
4327
4435
|
}
|
|
4328
|
-
let
|
|
4329
|
-
p.length >=
|
|
4436
|
+
let d = u, p = Array.isArray(d?.items) ? d.items : [];
|
|
4437
|
+
p.length >= mn && a.push(c);
|
|
4330
4438
|
for (let f of p) {
|
|
4331
4439
|
if (!f || typeof f != "object") continue;
|
|
4332
4440
|
let m = f, h = typeof m.id == "string" ? m.id : null;
|
|
4333
4441
|
if (!h) continue;
|
|
4334
|
-
let { id:
|
|
4335
|
-
if (typeof
|
|
4336
|
-
|
|
4337
|
-
else if (
|
|
4338
|
-
|
|
4339
|
-
else if (typeof
|
|
4340
|
-
let
|
|
4341
|
-
Number.isNaN(
|
|
4442
|
+
let { id: y, ...I } = m, w = i, k = I._updatedAt;
|
|
4443
|
+
if (typeof k == "number")
|
|
4444
|
+
w = k;
|
|
4445
|
+
else if (k !== null && typeof k == "object" && typeof k._seconds == "number")
|
|
4446
|
+
w = k._seconds * 1e3;
|
|
4447
|
+
else if (typeof k == "string") {
|
|
4448
|
+
let g = Date.parse(k);
|
|
4449
|
+
Number.isNaN(g) || (w = g);
|
|
4342
4450
|
}
|
|
4343
|
-
let
|
|
4451
|
+
let N = `rest_${h}_${w}`;
|
|
4344
4452
|
s.push({
|
|
4345
|
-
collection:
|
|
4453
|
+
collection: c,
|
|
4346
4454
|
id: h,
|
|
4347
|
-
data:
|
|
4348
|
-
serverVersion:
|
|
4349
|
-
serverTimestamp:
|
|
4455
|
+
data: I,
|
|
4456
|
+
serverVersion: N,
|
|
4457
|
+
serverTimestamp: w,
|
|
4350
4458
|
deleted: false
|
|
4351
4459
|
});
|
|
4352
4460
|
}
|
|
@@ -4355,10 +4463,10 @@ function nn(n) {
|
|
|
4355
4463
|
}
|
|
4356
4464
|
};
|
|
4357
4465
|
}
|
|
4358
|
-
function
|
|
4359
|
-
let r =
|
|
4466
|
+
function _o(n, e, t) {
|
|
4467
|
+
let r = gn(e), o = new Pe({
|
|
4360
4468
|
gatewayUrl: n,
|
|
4361
|
-
ticketProvider:
|
|
4469
|
+
ticketProvider: bo(e, t),
|
|
4362
4470
|
dbId: t.dbId,
|
|
4363
4471
|
webSocketFactory: t._wsFactory
|
|
4364
4472
|
});
|
|
@@ -4370,20 +4478,20 @@ function Br(n, e, t) {
|
|
|
4370
4478
|
// quando o engine é nosql-vm. Registra a subscription no NeetruRealtimeClient
|
|
4371
4479
|
// e traduz os frames inbound para o contrato de `onChange`.
|
|
4372
4480
|
subscribeCollection(i, s) {
|
|
4373
|
-
let a = o.subscribe(i, {}, (
|
|
4374
|
-
if (
|
|
4481
|
+
let a = o.subscribe(i, {}, (c) => {
|
|
4482
|
+
if (c.op === "resync") {
|
|
4375
4483
|
s([], true);
|
|
4376
4484
|
return;
|
|
4377
4485
|
}
|
|
4378
|
-
if (
|
|
4486
|
+
if (c.op === "stale") {
|
|
4379
4487
|
s([], true);
|
|
4380
4488
|
return;
|
|
4381
4489
|
}
|
|
4382
|
-
if (
|
|
4383
|
-
let u =
|
|
4384
|
-
type:
|
|
4385
|
-
docId:
|
|
4386
|
-
data:
|
|
4490
|
+
if (c.op === "delta" && c.changes) {
|
|
4491
|
+
let u = c.changes.map((d) => ({
|
|
4492
|
+
type: d.type === "insert" ? "added" : d.type === "delete" ? "removed" : "modified",
|
|
4493
|
+
docId: d.documentId,
|
|
4494
|
+
data: d.data ?? null
|
|
4387
4495
|
}));
|
|
4388
4496
|
u.length > 0 && s(u, false);
|
|
4389
4497
|
}
|
|
@@ -4394,9 +4502,9 @@ function Br(n, e, t) {
|
|
|
4394
4502
|
}
|
|
4395
4503
|
};
|
|
4396
4504
|
}
|
|
4397
|
-
function
|
|
4505
|
+
function bo(n, e) {
|
|
4398
4506
|
return async () => {
|
|
4399
|
-
let { httpRequest: t } = await Promise.resolve().then(() => (
|
|
4507
|
+
let { httpRequest: t } = await Promise.resolve().then(() => (q(), ne));
|
|
4400
4508
|
return await t(n, {
|
|
4401
4509
|
method: "POST",
|
|
4402
4510
|
path: "/api/sdk/v1/db/realtime/ticket",
|
|
@@ -4409,21 +4517,21 @@ function zr(n, e) {
|
|
|
4409
4517
|
});
|
|
4410
4518
|
};
|
|
4411
4519
|
}
|
|
4412
|
-
var
|
|
4413
|
-
function
|
|
4414
|
-
|
|
4520
|
+
var hn = false;
|
|
4521
|
+
function vo(n, e) {
|
|
4522
|
+
hn || e.dbId && n.env !== "prod" && (typeof window > "u" || typeof console > "u" || typeof console.warn != "function" || (hn = true, console.warn(
|
|
4415
4523
|
'[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.'
|
|
4416
4524
|
)));
|
|
4417
4525
|
}
|
|
4418
|
-
function
|
|
4419
|
-
|
|
4526
|
+
function nt(n, e = {}) {
|
|
4527
|
+
vo(n, e);
|
|
4420
4528
|
let t;
|
|
4421
4529
|
e.transport !== void 0 ? t = e.transport : e.engine !== void 0 ? (console.warn(
|
|
4422
4530
|
'[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).'
|
|
4423
4531
|
), t = e.engine) : t = "rest";
|
|
4424
|
-
let r =
|
|
4425
|
-
function
|
|
4426
|
-
return s || (s =
|
|
4532
|
+
let r = yo(t, e, n), o = e.dbId ?? "default", i = e.dbName ?? `neetru-db__${n.productId ?? "sdk"}__${o}__${n.env}`, s = null, a = null;
|
|
4533
|
+
function c() {
|
|
4534
|
+
return s || (s = tn({
|
|
4427
4535
|
dbName: i,
|
|
4428
4536
|
transport: r,
|
|
4429
4537
|
singleTab: e.singleTab ?? n.env !== "prod"
|
|
@@ -4431,15 +4539,15 @@ function Ge(n, e = {}) {
|
|
|
4431
4539
|
}
|
|
4432
4540
|
return {
|
|
4433
4541
|
collection(u) {
|
|
4434
|
-
return a ? a.collection(u) :
|
|
4542
|
+
return a ? a.collection(u) : wo(u, c);
|
|
4435
4543
|
},
|
|
4436
|
-
async sql(u,
|
|
4544
|
+
async sql(u, d) {
|
|
4437
4545
|
if (n.env === "dev")
|
|
4438
4546
|
throw new exports.NeetruDbError(
|
|
4439
4547
|
"db_unavailable",
|
|
4440
4548
|
"[SDK] db.sql() n\xE3o dispon\xEDvel em NEETRU_ENV=dev. Use `neetru dev` para subir o container Postgres local."
|
|
4441
4549
|
);
|
|
4442
|
-
return (await Promise.resolve().then(() => (
|
|
4550
|
+
return (await Promise.resolve().then(() => (fn(), pn))).createSqlClientFromConfig(n, u, d);
|
|
4443
4551
|
},
|
|
4444
4552
|
get syncState() {
|
|
4445
4553
|
return a?.syncState ?? {
|
|
@@ -4451,34 +4559,34 @@ function Ge(n, e = {}) {
|
|
|
4451
4559
|
},
|
|
4452
4560
|
onSyncStateChanged(u) {
|
|
4453
4561
|
if (a) return a.onSyncStateChanged(u);
|
|
4454
|
-
let
|
|
4455
|
-
return
|
|
4456
|
-
p || (
|
|
4562
|
+
let d = null, p = false;
|
|
4563
|
+
return c().then((f) => {
|
|
4564
|
+
p || (d = f.onSyncStateChanged(u));
|
|
4457
4565
|
}), () => {
|
|
4458
|
-
p = true,
|
|
4566
|
+
p = true, d?.();
|
|
4459
4567
|
};
|
|
4460
4568
|
},
|
|
4461
4569
|
async flush() {
|
|
4462
|
-
return (await
|
|
4570
|
+
return (await c()).flush();
|
|
4463
4571
|
},
|
|
4464
4572
|
async clearCache() {
|
|
4465
|
-
return (await
|
|
4573
|
+
return (await c()).clearCache();
|
|
4466
4574
|
},
|
|
4467
4575
|
async getConflicts() {
|
|
4468
|
-
return (await
|
|
4576
|
+
return (await c()).getConflicts();
|
|
4469
4577
|
},
|
|
4470
4578
|
onError(u) {
|
|
4471
4579
|
if (a) return a.onError(u);
|
|
4472
|
-
let
|
|
4473
|
-
return
|
|
4474
|
-
p || (
|
|
4580
|
+
let d = null, p = false;
|
|
4581
|
+
return c().then((f) => {
|
|
4582
|
+
p || (d = f.onError(u));
|
|
4475
4583
|
}), () => {
|
|
4476
|
-
p = true,
|
|
4584
|
+
p = true, d?.();
|
|
4477
4585
|
};
|
|
4478
4586
|
}
|
|
4479
4587
|
};
|
|
4480
4588
|
}
|
|
4481
|
-
function
|
|
4589
|
+
function wo(n, e) {
|
|
4482
4590
|
async function t() {
|
|
4483
4591
|
return (await e()).collection(n);
|
|
4484
4592
|
}
|
|
@@ -4557,9 +4665,9 @@ function Hr(n, e) {
|
|
|
4557
4665
|
}
|
|
4558
4666
|
|
|
4559
4667
|
// src/checkout.ts
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
function
|
|
4668
|
+
C();
|
|
4669
|
+
q();
|
|
4670
|
+
function rt(n, e) {
|
|
4563
4671
|
let t = n?.accessToken || n?.idToken;
|
|
4564
4672
|
if (!t || typeof t != "string")
|
|
4565
4673
|
throw new exports.NeetruError(
|
|
@@ -4568,7 +4676,7 @@ function Je(n, e) {
|
|
|
4568
4676
|
);
|
|
4569
4677
|
return { requireAuth: false, headers: { authorization: `Bearer ${t}` } };
|
|
4570
4678
|
}
|
|
4571
|
-
function
|
|
4679
|
+
function ko(n) {
|
|
4572
4680
|
if (!n || typeof n != "object")
|
|
4573
4681
|
throw new exports.NeetruError("invalid_response", "checkout.start response is not an object");
|
|
4574
4682
|
let e = n;
|
|
@@ -4584,7 +4692,7 @@ function Qr(n) {
|
|
|
4584
4692
|
requiresKyc: e.requiresKyc === true
|
|
4585
4693
|
};
|
|
4586
4694
|
}
|
|
4587
|
-
function
|
|
4695
|
+
function So(n) {
|
|
4588
4696
|
if (!n || typeof n != "object")
|
|
4589
4697
|
throw new exports.NeetruError("invalid_response", "checkout.get response is not an object");
|
|
4590
4698
|
let e = n, t = e.intent;
|
|
@@ -4606,20 +4714,20 @@ function Gr(n) {
|
|
|
4606
4714
|
isStale: e.isStale === true
|
|
4607
4715
|
};
|
|
4608
4716
|
}
|
|
4609
|
-
function
|
|
4717
|
+
function To() {
|
|
4610
4718
|
try {
|
|
4611
4719
|
return typeof globalThis < "u" && typeof globalThis.window < "u" && typeof globalThis.location < "u" && typeof globalThis.location.assign == "function";
|
|
4612
4720
|
} catch {
|
|
4613
4721
|
return false;
|
|
4614
4722
|
}
|
|
4615
4723
|
}
|
|
4616
|
-
function
|
|
4724
|
+
function Ro(n) {
|
|
4617
4725
|
try {
|
|
4618
4726
|
globalThis.location.assign(n);
|
|
4619
4727
|
} catch {
|
|
4620
4728
|
}
|
|
4621
4729
|
}
|
|
4622
|
-
function
|
|
4730
|
+
function Io(n) {
|
|
4623
4731
|
return {
|
|
4624
4732
|
async start(e) {
|
|
4625
4733
|
if (!e?.productId)
|
|
@@ -4634,43 +4742,43 @@ function Yr(n) {
|
|
|
4634
4742
|
callbackUrl: e.callbackUrl
|
|
4635
4743
|
};
|
|
4636
4744
|
e.tenantType && (t.targetTenantType = e.tenantType), e.tenantId && (t.targetTenantId = e.tenantId);
|
|
4637
|
-
let { requireAuth: r, headers: o } =
|
|
4745
|
+
let { requireAuth: r, headers: o } = rt(
|
|
4638
4746
|
{ accessToken: e.accessToken, idToken: e.idToken },
|
|
4639
4747
|
"start"
|
|
4640
|
-
), i = await
|
|
4748
|
+
), i = await S(n, {
|
|
4641
4749
|
method: "POST",
|
|
4642
4750
|
path: "/api/v1/checkout/intents",
|
|
4643
4751
|
body: t,
|
|
4644
4752
|
requireAuth: r,
|
|
4645
4753
|
headers: o,
|
|
4646
4754
|
idempotencyKey: true
|
|
4647
|
-
}), s =
|
|
4648
|
-
return e.autoRedirect !== false &&
|
|
4755
|
+
}), s = ko(i);
|
|
4756
|
+
return e.autoRedirect !== false && To() && Ro(s.redirectUrl), s;
|
|
4649
4757
|
},
|
|
4650
4758
|
async get(e, t) {
|
|
4651
4759
|
if (!e || typeof e != "string")
|
|
4652
4760
|
throw new exports.NeetruError("validation_failed", "checkout.get: intentId is required");
|
|
4653
|
-
let { requireAuth: r, headers: o } =
|
|
4761
|
+
let { requireAuth: r, headers: o } = rt(
|
|
4654
4762
|
{ accessToken: t?.accessToken, idToken: t?.idToken },
|
|
4655
4763
|
"get"
|
|
4656
|
-
), i = await
|
|
4764
|
+
), i = await S(n, {
|
|
4657
4765
|
method: "GET",
|
|
4658
4766
|
path: `/api/v1/checkout/intents/${encodeURIComponent(e)}`,
|
|
4659
4767
|
requireAuth: r,
|
|
4660
4768
|
headers: o
|
|
4661
4769
|
});
|
|
4662
|
-
return
|
|
4770
|
+
return So(i);
|
|
4663
4771
|
},
|
|
4664
4772
|
async cancel(e, t) {
|
|
4665
4773
|
if (!e || typeof e != "string")
|
|
4666
4774
|
throw new exports.NeetruError("validation_failed", "checkout.cancel: intentId is required");
|
|
4667
|
-
let { requireAuth: r, headers: o } =
|
|
4775
|
+
let { requireAuth: r, headers: o } = rt(
|
|
4668
4776
|
{ accessToken: t?.accessToken, idToken: t?.idToken },
|
|
4669
4777
|
"cancel"
|
|
4670
4778
|
);
|
|
4671
4779
|
return {
|
|
4672
4780
|
ok: true,
|
|
4673
|
-
alreadyCancelled: (await
|
|
4781
|
+
alreadyCancelled: (await S(n, {
|
|
4674
4782
|
method: "DELETE",
|
|
4675
4783
|
path: `/api/v1/checkout/intents/${encodeURIComponent(e)}`,
|
|
4676
4784
|
requireAuth: r,
|
|
@@ -4680,7 +4788,7 @@ function Yr(n) {
|
|
|
4680
4788
|
}
|
|
4681
4789
|
};
|
|
4682
4790
|
}
|
|
4683
|
-
var
|
|
4791
|
+
var xe = class {
|
|
4684
4792
|
intents = /* @__PURE__ */ new Map();
|
|
4685
4793
|
async start(e) {
|
|
4686
4794
|
if (!e?.productId)
|
|
@@ -4723,14 +4831,14 @@ var Ie = class {
|
|
|
4723
4831
|
return this.intents.set(e, { ...r, status: "cancelled" }), { ok: true, alreadyCancelled: o };
|
|
4724
4832
|
}
|
|
4725
4833
|
};
|
|
4726
|
-
function
|
|
4727
|
-
return n.env === "dev" ? new
|
|
4834
|
+
function ot(n) {
|
|
4835
|
+
return n.env === "dev" ? new xe() : Io(n);
|
|
4728
4836
|
}
|
|
4729
4837
|
|
|
4730
4838
|
// src/webhooks.ts
|
|
4731
|
-
|
|
4732
|
-
|
|
4733
|
-
async function
|
|
4839
|
+
C();
|
|
4840
|
+
q();
|
|
4841
|
+
async function _n(n, e, t, r) {
|
|
4734
4842
|
if (!e || !t) return false;
|
|
4735
4843
|
let o = e.startsWith("sha256=") ? e.slice(7) : e;
|
|
4736
4844
|
if (!/^[0-9a-f]+$/i.test(o)) return false;
|
|
@@ -4740,12 +4848,12 @@ async function on(n, e, t, r) {
|
|
|
4740
4848
|
"runtime_unsupported",
|
|
4741
4849
|
"verifyWebhookSignature requires globalThis.crypto.subtle (Node \u226520, modern browser, or Edge runtime)."
|
|
4742
4850
|
);
|
|
4743
|
-
let s = new TextEncoder(), a = s.encode(t),
|
|
4851
|
+
let s = new TextEncoder(), a = s.encode(t), c = typeof n == "string" ? s.encode(n) : n, u = c;
|
|
4744
4852
|
if (r !== void 0) {
|
|
4745
|
-
let
|
|
4746
|
-
|
|
4853
|
+
let y = s.encode(`${r}.`), I = new Uint8Array(y.length + c.length);
|
|
4854
|
+
I.set(y, 0), I.set(c, y.length), u = I;
|
|
4747
4855
|
}
|
|
4748
|
-
let
|
|
4856
|
+
let d = await i.importKey(
|
|
4749
4857
|
"raw",
|
|
4750
4858
|
a,
|
|
4751
4859
|
{ name: "HMAC", hash: "SHA-256" },
|
|
@@ -4753,16 +4861,16 @@ async function on(n, e, t, r) {
|
|
|
4753
4861
|
["sign"]
|
|
4754
4862
|
), p = await i.sign(
|
|
4755
4863
|
"HMAC",
|
|
4756
|
-
|
|
4864
|
+
d,
|
|
4757
4865
|
u
|
|
4758
|
-
), f = new Uint8Array(p), m =
|
|
4866
|
+
), f = new Uint8Array(p), m = Ao(o.toLowerCase());
|
|
4759
4867
|
if (!m || m.length !== f.length) return false;
|
|
4760
4868
|
let h = 0;
|
|
4761
|
-
for (let
|
|
4762
|
-
h |= f[
|
|
4869
|
+
for (let y = 0; y < f.length; y++)
|
|
4870
|
+
h |= f[y] ^ m[y];
|
|
4763
4871
|
return h === 0;
|
|
4764
4872
|
}
|
|
4765
|
-
async function
|
|
4873
|
+
async function Co(n, e, t, r = {}) {
|
|
4766
4874
|
let o = r.toleranceMs ?? 3e5, i = (p) => {
|
|
4767
4875
|
if (typeof e.get == "function")
|
|
4768
4876
|
return e.get(p);
|
|
@@ -4779,9 +4887,9 @@ async function Zr(n, e, t, r = {}) {
|
|
|
4779
4887
|
if (Math.abs(Date.now() - a) > o)
|
|
4780
4888
|
return { ok: false, reason: "timestamp_expired" };
|
|
4781
4889
|
let u = i("x-neetru-signature");
|
|
4782
|
-
return await
|
|
4890
|
+
return await _n(n, u, t, s) ? { ok: true } : { ok: false, reason: "invalid_signature" };
|
|
4783
4891
|
}
|
|
4784
|
-
function
|
|
4892
|
+
function Ao(n) {
|
|
4785
4893
|
if (n.length % 2 !== 0) return null;
|
|
4786
4894
|
let e = new Uint8Array(n.length / 2);
|
|
4787
4895
|
for (let t = 0; t < e.length; t++) {
|
|
@@ -4791,7 +4899,7 @@ function eo(n) {
|
|
|
4791
4899
|
}
|
|
4792
4900
|
return e;
|
|
4793
4901
|
}
|
|
4794
|
-
var
|
|
4902
|
+
var bn = [
|
|
4795
4903
|
"subscription.activated",
|
|
4796
4904
|
"subscription.cancelled",
|
|
4797
4905
|
"subscription.payment_failed",
|
|
@@ -4801,7 +4909,7 @@ var sn = [
|
|
|
4801
4909
|
"account.reactivated",
|
|
4802
4910
|
"support.ticket_replied"
|
|
4803
4911
|
];
|
|
4804
|
-
function
|
|
4912
|
+
function yn(n) {
|
|
4805
4913
|
if (!n || typeof n != "object")
|
|
4806
4914
|
throw new exports.NeetruError("invalid_response", "Webhook response is not an object");
|
|
4807
4915
|
let e = n;
|
|
@@ -4811,7 +4919,7 @@ function rn(n) {
|
|
|
4811
4919
|
id: e.id,
|
|
4812
4920
|
url: typeof e.url == "string" ? e.url : "",
|
|
4813
4921
|
events: Array.isArray(e.events) ? e.events.filter(
|
|
4814
|
-
(t) =>
|
|
4922
|
+
(t) => bn.includes(t)
|
|
4815
4923
|
) : [],
|
|
4816
4924
|
hasSecret: e.hasSecret === true,
|
|
4817
4925
|
status: e.status === "active" || e.status === "degraded" || e.status === "disabled" ? e.status : "active",
|
|
@@ -4820,7 +4928,7 @@ function rn(n) {
|
|
|
4820
4928
|
createdAt: typeof e.createdAt == "string" ? e.createdAt : (/* @__PURE__ */ new Date()).toISOString()
|
|
4821
4929
|
};
|
|
4822
4930
|
}
|
|
4823
|
-
function
|
|
4931
|
+
function vn(n) {
|
|
4824
4932
|
if (!n.url || typeof n.url != "string")
|
|
4825
4933
|
throw new exports.NeetruError("validation_failed", "url \xE9 obrigat\xF3ria");
|
|
4826
4934
|
try {
|
|
@@ -4833,12 +4941,12 @@ function an(n) {
|
|
|
4833
4941
|
if (!Array.isArray(n.events) || n.events.length === 0)
|
|
4834
4942
|
throw new exports.NeetruError("validation_failed", "events deve ter pelo menos 1 evento");
|
|
4835
4943
|
for (let e of n.events)
|
|
4836
|
-
if (!
|
|
4944
|
+
if (!bn.includes(e))
|
|
4837
4945
|
throw new exports.NeetruError("validation_failed", `evento desconhecido: ${e}`);
|
|
4838
4946
|
if (n.secret !== void 0 && n.secret.length < 16)
|
|
4839
4947
|
throw new exports.NeetruError("validation_failed", "secret deve ter \u226516 chars (recomendado 32+)");
|
|
4840
4948
|
}
|
|
4841
|
-
function
|
|
4949
|
+
function Oe(n, ...e) {
|
|
4842
4950
|
for (let t of e)
|
|
4843
4951
|
if (typeof t == "string" && t.length > 0)
|
|
4844
4952
|
return O("productId", t), t;
|
|
@@ -4849,11 +4957,11 @@ function Ce(n, ...e) {
|
|
|
4849
4957
|
"productId required (pass to options/input or set on createNeetruClient)"
|
|
4850
4958
|
);
|
|
4851
4959
|
}
|
|
4852
|
-
function
|
|
4960
|
+
function it(n) {
|
|
4853
4961
|
return {
|
|
4854
4962
|
async register(e) {
|
|
4855
|
-
|
|
4856
|
-
let t =
|
|
4963
|
+
vn(e);
|
|
4964
|
+
let t = Oe(n, e.productId), r = await S(n, {
|
|
4857
4965
|
method: "POST",
|
|
4858
4966
|
path: "/api/sdk/v1/webhooks",
|
|
4859
4967
|
body: {
|
|
@@ -4869,21 +4977,21 @@ function Ye(n) {
|
|
|
4869
4977
|
// dedup TTL 24h (mesmo padrão de usage.report/support.createTicket).
|
|
4870
4978
|
idempotencyKey: true
|
|
4871
4979
|
});
|
|
4872
|
-
return
|
|
4980
|
+
return yn(r);
|
|
4873
4981
|
},
|
|
4874
4982
|
async list(e) {
|
|
4875
|
-
let t =
|
|
4983
|
+
let t = Oe(n, e?.productId), r = await S(n, {
|
|
4876
4984
|
method: "GET",
|
|
4877
4985
|
path: "/api/sdk/v1/webhooks",
|
|
4878
4986
|
query: { productId: t },
|
|
4879
4987
|
requireAuth: true
|
|
4880
4988
|
});
|
|
4881
|
-
return (Array.isArray(r?.endpoints) ? r.endpoints : []).map(
|
|
4989
|
+
return (Array.isArray(r?.endpoints) ? r.endpoints : []).map(yn);
|
|
4882
4990
|
},
|
|
4883
4991
|
async unregister(e, t) {
|
|
4884
4992
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
4885
|
-
let r =
|
|
4886
|
-
return await
|
|
4993
|
+
let r = Oe(n, t?.productId);
|
|
4994
|
+
return await S(n, {
|
|
4887
4995
|
method: "DELETE",
|
|
4888
4996
|
path: `/api/sdk/v1/webhooks/${encodeURIComponent(e)}`,
|
|
4889
4997
|
query: { productId: r },
|
|
@@ -4892,7 +5000,7 @@ function Ye(n) {
|
|
|
4892
5000
|
},
|
|
4893
5001
|
async test(e, t) {
|
|
4894
5002
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
4895
|
-
let r =
|
|
5003
|
+
let r = Oe(n, t?.productId), o = await S(n, {
|
|
4896
5004
|
method: "POST",
|
|
4897
5005
|
path: `/api/sdk/v1/webhooks/${encodeURIComponent(e)}/test`,
|
|
4898
5006
|
query: { productId: r },
|
|
@@ -4911,11 +5019,11 @@ function Ye(n) {
|
|
|
4911
5019
|
}
|
|
4912
5020
|
};
|
|
4913
5021
|
}
|
|
4914
|
-
var
|
|
5022
|
+
var ae = class {
|
|
4915
5023
|
endpoints = /* @__PURE__ */ new Map();
|
|
4916
5024
|
nextId = 1;
|
|
4917
5025
|
async register(e) {
|
|
4918
|
-
|
|
5026
|
+
vn(e);
|
|
4919
5027
|
let t = `mock_wh_${this.nextId++}`, r = {
|
|
4920
5028
|
id: t,
|
|
4921
5029
|
url: e.url,
|
|
@@ -4942,21 +5050,21 @@ var oe = class {
|
|
|
4942
5050
|
};
|
|
4943
5051
|
|
|
4944
5052
|
// src/notifications.ts
|
|
4945
|
-
|
|
4946
|
-
|
|
4947
|
-
var
|
|
5053
|
+
C();
|
|
5054
|
+
q();
|
|
5055
|
+
var st = [
|
|
4948
5056
|
"info",
|
|
4949
5057
|
"success",
|
|
4950
5058
|
"warning",
|
|
4951
5059
|
"error"
|
|
4952
5060
|
];
|
|
4953
|
-
function
|
|
5061
|
+
function wn(n) {
|
|
4954
5062
|
if (!n || typeof n != "object")
|
|
4955
5063
|
throw new exports.NeetruError("invalid_response", "Notification response is not an object");
|
|
4956
5064
|
let e = n;
|
|
4957
5065
|
if (typeof e.id != "string")
|
|
4958
5066
|
throw new exports.NeetruError("invalid_response", "Notification missing id");
|
|
4959
|
-
let t =
|
|
5067
|
+
let t = st.includes(e.severity) ? e.severity : "info";
|
|
4960
5068
|
return {
|
|
4961
5069
|
id: e.id,
|
|
4962
5070
|
userId: typeof e.userId == "string" ? e.userId : "",
|
|
@@ -4971,21 +5079,21 @@ function cn(n) {
|
|
|
4971
5079
|
dismissedAt: typeof e.dismissedAt == "string" ? e.dismissedAt : void 0
|
|
4972
5080
|
};
|
|
4973
5081
|
}
|
|
4974
|
-
function
|
|
5082
|
+
function kn(n) {
|
|
4975
5083
|
if (!n.userId) throw new exports.NeetruError("validation_failed", "userId obrigat\xF3rio");
|
|
4976
5084
|
if (!n.kind) throw new exports.NeetruError("validation_failed", "kind obrigat\xF3rio");
|
|
4977
5085
|
if (!n.title) throw new exports.NeetruError("validation_failed", "title obrigat\xF3rio");
|
|
4978
|
-
if (n.severity && !
|
|
5086
|
+
if (n.severity && !st.includes(n.severity))
|
|
4979
5087
|
throw new exports.NeetruError(
|
|
4980
5088
|
"validation_failed",
|
|
4981
|
-
`severity inv\xE1lida: ${n.severity} (use ${
|
|
5089
|
+
`severity inv\xE1lida: ${n.severity} (use ${st.join("|")})`
|
|
4982
5090
|
);
|
|
4983
5091
|
if (n.title.length > 200)
|
|
4984
5092
|
throw new exports.NeetruError("validation_failed", "title m\xE1x 200 chars");
|
|
4985
5093
|
if (n.body && n.body.length > 2e3)
|
|
4986
5094
|
throw new exports.NeetruError("validation_failed", "body m\xE1x 2000 chars");
|
|
4987
5095
|
}
|
|
4988
|
-
function
|
|
5096
|
+
function Le(n, ...e) {
|
|
4989
5097
|
for (let t of e)
|
|
4990
5098
|
if (typeof t == "string" && t.length > 0)
|
|
4991
5099
|
return O("productId", t), t;
|
|
@@ -4996,45 +5104,45 @@ function Ae(n, ...e) {
|
|
|
4996
5104
|
"productId required (pass to options/input or set on createNeetruClient)"
|
|
4997
5105
|
);
|
|
4998
5106
|
}
|
|
4999
|
-
function
|
|
5107
|
+
function at(n) {
|
|
5000
5108
|
return {
|
|
5001
5109
|
async send(e) {
|
|
5002
|
-
|
|
5110
|
+
kn(e);
|
|
5003
5111
|
let r = {
|
|
5004
|
-
productId:
|
|
5112
|
+
productId: Le(n, e.productId),
|
|
5005
5113
|
userId: e.userId,
|
|
5006
5114
|
kind: e.kind,
|
|
5007
5115
|
title: e.title,
|
|
5008
5116
|
severity: e.severity ?? "info"
|
|
5009
5117
|
};
|
|
5010
5118
|
e.body !== void 0 && (r.body = e.body), e.link !== void 0 && (r.link = e.link), e.metadata !== void 0 && (r.metadata = e.metadata), e.fingerprint !== void 0 && (r.fingerprint = e.fingerprint);
|
|
5011
|
-
let o = await
|
|
5119
|
+
let o = await S(n, {
|
|
5012
5120
|
method: "POST",
|
|
5013
5121
|
path: "/api/sdk/v1/notifications",
|
|
5014
5122
|
body: r,
|
|
5015
5123
|
requireAuth: true,
|
|
5016
5124
|
idempotencyKey: true
|
|
5017
5125
|
});
|
|
5018
|
-
return
|
|
5126
|
+
return wn(o);
|
|
5019
5127
|
},
|
|
5020
5128
|
async list(e, t) {
|
|
5021
5129
|
if (!e) throw new exports.NeetruError("validation_failed", "userId obrigat\xF3rio");
|
|
5022
5130
|
let o = {
|
|
5023
|
-
productId:
|
|
5131
|
+
productId: Le(n, t?.productId)
|
|
5024
5132
|
};
|
|
5025
5133
|
t?.includeDismissed && (o.includeDismissed = true), t?.onlyUnread && (o.onlyUnread = true), t?.limit && (o.limit = Math.min(Math.max(1, t.limit), 200));
|
|
5026
|
-
let i = await
|
|
5134
|
+
let i = await S(n, {
|
|
5027
5135
|
method: "GET",
|
|
5028
5136
|
path: `/api/sdk/v1/notifications/user/${encodeURIComponent(e)}`,
|
|
5029
5137
|
query: o,
|
|
5030
5138
|
requireAuth: true
|
|
5031
5139
|
});
|
|
5032
|
-
return (Array.isArray(i?.notifications) ? i.notifications : []).map(
|
|
5140
|
+
return (Array.isArray(i?.notifications) ? i.notifications : []).map(wn);
|
|
5033
5141
|
},
|
|
5034
5142
|
async markRead(e, t) {
|
|
5035
5143
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
5036
|
-
let r =
|
|
5037
|
-
return await
|
|
5144
|
+
let r = Le(n, t?.productId);
|
|
5145
|
+
return await S(n, {
|
|
5038
5146
|
method: "POST",
|
|
5039
5147
|
path: `/api/sdk/v1/notifications/${encodeURIComponent(e)}/read`,
|
|
5040
5148
|
query: { productId: r },
|
|
@@ -5043,8 +5151,8 @@ function et(n) {
|
|
|
5043
5151
|
},
|
|
5044
5152
|
async dismiss(e, t) {
|
|
5045
5153
|
if (!e) throw new exports.NeetruError("validation_failed", "id obrigat\xF3rio");
|
|
5046
|
-
let r =
|
|
5047
|
-
return await
|
|
5154
|
+
let r = Le(n, t?.productId);
|
|
5155
|
+
return await S(n, {
|
|
5048
5156
|
method: "POST",
|
|
5049
5157
|
path: `/api/sdk/v1/notifications/${encodeURIComponent(e)}/dismiss`,
|
|
5050
5158
|
query: { productId: r },
|
|
@@ -5053,11 +5161,11 @@ function et(n) {
|
|
|
5053
5161
|
}
|
|
5054
5162
|
};
|
|
5055
5163
|
}
|
|
5056
|
-
var
|
|
5164
|
+
var ce = class {
|
|
5057
5165
|
notifications = /* @__PURE__ */ new Map();
|
|
5058
5166
|
nextId = 1;
|
|
5059
5167
|
async send(e) {
|
|
5060
|
-
if (
|
|
5168
|
+
if (kn(e), e.fingerprint) {
|
|
5061
5169
|
let o = Date.now() - 864e5;
|
|
5062
5170
|
for (let i of this.notifications.values())
|
|
5063
5171
|
if ((i.metadata ?? {}).fingerprint === e.fingerprint && i.userId === e.userId && new Date(i.createdAt).getTime() > o)
|
|
@@ -5094,43 +5202,44 @@ var ie = class {
|
|
|
5094
5202
|
};
|
|
5095
5203
|
|
|
5096
5204
|
// src/ai.ts
|
|
5097
|
-
|
|
5098
|
-
|
|
5099
|
-
|
|
5100
|
-
|
|
5205
|
+
C();
|
|
5206
|
+
q();
|
|
5207
|
+
We();
|
|
5208
|
+
var Eo = "/api/sdk/v1/ai/chat", Sn = 12e4, Ue = 32768, Tn = 32, Rn = 4194304, In = 2048, Po = /^data:image\/(?:png|jpe?g|gif|webp);base64,[A-Za-z0-9+/]+={0,2}$/, Do = /^https:\/\/[^\s]+$/, No = /* @__PURE__ */ new Set(["low", "high", "auto"]), xo = /* @__PURE__ */ new Set(["system", "user", "assistant"]), Cn = 100, An = 128, En = 32768, Pn = 0, Dn = 2, Nn = 200, xn = 20, On = 12 * 1024 * 1024, Ln = 5e4, Un = 16, Mn = 500, Oo = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]), Lo = /^[a-zA-Z0-9_-]+$/;
|
|
5209
|
+
function R(n) {
|
|
5101
5210
|
throw new exports.NeetruError("validation_failed", n);
|
|
5102
5211
|
}
|
|
5103
|
-
function
|
|
5212
|
+
function Uo(n) {
|
|
5104
5213
|
if (n.length === 0 || n.length % 4 !== 0) return false;
|
|
5105
5214
|
let e = n.endsWith("==") ? 2 : n.endsWith("=") ? 1 : 0;
|
|
5106
5215
|
return !(e > 0 ? n.slice(0, n.length - e) : n).includes("=");
|
|
5107
5216
|
}
|
|
5108
|
-
function
|
|
5217
|
+
function Mo(n) {
|
|
5109
5218
|
let e = n.indexOf(",");
|
|
5110
5219
|
if (e === -1) return 0;
|
|
5111
5220
|
let t = n.slice(e + 1), r = t.endsWith("==") ? 2 : t.endsWith("=") ? 1 : 0;
|
|
5112
5221
|
return Math.max(0, Math.floor(t.length * 3 / 4) - r);
|
|
5113
5222
|
}
|
|
5114
|
-
function
|
|
5223
|
+
function qo(n) {
|
|
5115
5224
|
let e = 0, t = 0, r = 0;
|
|
5116
5225
|
for (let o of n) {
|
|
5117
5226
|
let i = o?.content;
|
|
5118
5227
|
if (Array.isArray(i))
|
|
5119
5228
|
for (let s of i) {
|
|
5120
|
-
if (e++, e >
|
|
5121
|
-
return `ai: total content parts exceeds ${
|
|
5229
|
+
if (e++, e > Nn)
|
|
5230
|
+
return `ai: total content parts exceeds ${Nn} per request`;
|
|
5122
5231
|
if (s && typeof s == "object" && s.type === "image_url") {
|
|
5123
|
-
if (t++, t >
|
|
5124
|
-
return `ai: total images exceeds ${
|
|
5232
|
+
if (t++, t > xn)
|
|
5233
|
+
return `ai: total images exceeds ${xn} per request`;
|
|
5125
5234
|
let a = s.image_url?.url;
|
|
5126
|
-
if (typeof a == "string" && a.startsWith("data:") && (r +=
|
|
5127
|
-
return `ai: aggregate image bytes exceeds ${
|
|
5235
|
+
if (typeof a == "string" && a.startsWith("data:") && (r += Mo(a), r > On))
|
|
5236
|
+
return `ai: aggregate image bytes exceeds ${On} per request`;
|
|
5128
5237
|
}
|
|
5129
5238
|
}
|
|
5130
5239
|
}
|
|
5131
5240
|
return null;
|
|
5132
5241
|
}
|
|
5133
|
-
function
|
|
5242
|
+
function Fo(n) {
|
|
5134
5243
|
if (n === void 0) return null;
|
|
5135
5244
|
if (n === null || typeof n != "object")
|
|
5136
5245
|
return "ai: responseFormat json_schema.schema must be an object";
|
|
@@ -5141,11 +5250,11 @@ function po(n) {
|
|
|
5141
5250
|
return "ai: responseFormat json_schema.schema is not serializable";
|
|
5142
5251
|
}
|
|
5143
5252
|
if (e === void 0) return "ai: responseFormat json_schema.schema is invalid";
|
|
5144
|
-
if (e.length >
|
|
5145
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
5253
|
+
if (e.length > Ln)
|
|
5254
|
+
return `ai: responseFormat json_schema.schema exceeds ${Ln} serialized chars`;
|
|
5146
5255
|
let t = 0, r = (o, i) => {
|
|
5147
|
-
if (i >
|
|
5148
|
-
return `ai: responseFormat json_schema.schema exceeds max depth ${
|
|
5256
|
+
if (i > Un)
|
|
5257
|
+
return `ai: responseFormat json_schema.schema exceeds max depth ${Un}`;
|
|
5149
5258
|
if (Array.isArray(o)) {
|
|
5150
5259
|
for (let s of o) {
|
|
5151
5260
|
let a = r(s, i + 1);
|
|
@@ -5155,10 +5264,10 @@ function po(n) {
|
|
|
5155
5264
|
}
|
|
5156
5265
|
if (o && typeof o == "object")
|
|
5157
5266
|
for (let s of Object.getOwnPropertyNames(o)) {
|
|
5158
|
-
if (
|
|
5267
|
+
if (Oo.has(s))
|
|
5159
5268
|
return `ai: responseFormat json_schema.schema has forbidden key: ${s}`;
|
|
5160
|
-
if (t++, t >
|
|
5161
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
5269
|
+
if (t++, t > Mn)
|
|
5270
|
+
return `ai: responseFormat json_schema.schema exceeds ${Mn} keys`;
|
|
5162
5271
|
let a = r(o[s], i + 1);
|
|
5163
5272
|
if (a) return a;
|
|
5164
5273
|
}
|
|
@@ -5166,80 +5275,80 @@ function po(n) {
|
|
|
5166
5275
|
};
|
|
5167
5276
|
return r(n, 0);
|
|
5168
5277
|
}
|
|
5169
|
-
function
|
|
5170
|
-
(typeof n != "string" || n.length === 0) &&
|
|
5278
|
+
function jo(n) {
|
|
5279
|
+
(typeof n != "string" || n.length === 0) && R("ai: image_url.url must be a non-empty string");
|
|
5171
5280
|
let e = n;
|
|
5172
5281
|
if (e.startsWith("data:")) {
|
|
5173
|
-
e.length >
|
|
5282
|
+
e.length > Rn && R(`ai: image_url.url (data:) exceeds ${Rn} chars`), Po.test(e) || R("ai: image_url.url data: must be data:image/<png|jpeg|jpg|gif|webp>;base64,<...>");
|
|
5174
5283
|
let t = e.slice(e.indexOf(",") + 1);
|
|
5175
|
-
|
|
5284
|
+
Uo(t) || R("ai: image_url.url data: has invalid base64 (length/padding)");
|
|
5176
5285
|
return;
|
|
5177
5286
|
}
|
|
5178
5287
|
if (e.startsWith("https://")) {
|
|
5179
|
-
e.length >
|
|
5288
|
+
e.length > In && R(`ai: image_url.url (https:) exceeds ${In} chars`), Do.test(e) || R("ai: image_url.url https: is invalid");
|
|
5180
5289
|
return;
|
|
5181
5290
|
}
|
|
5182
|
-
|
|
5291
|
+
R("ai: image_url.url must be a data:image/...;base64 or https:// URL (other schemes are rejected)");
|
|
5183
5292
|
}
|
|
5184
|
-
function
|
|
5293
|
+
function Wo(n, e) {
|
|
5185
5294
|
if (typeof n == "string") {
|
|
5186
|
-
n.length === 0 &&
|
|
5295
|
+
n.length === 0 && R("ai: message content string must be non-empty"), n.length > Ue && R(`ai: message content exceeds ${Ue} chars`);
|
|
5187
5296
|
return;
|
|
5188
5297
|
}
|
|
5189
|
-
(!Array.isArray(n) || n.length === 0) &&
|
|
5298
|
+
(!Array.isArray(n) || n.length === 0) && R("ai: message content must be a non-empty string or array of parts"), n.length > Tn && R(`ai: message content has more than ${Tn} parts`);
|
|
5190
5299
|
for (let t of n) {
|
|
5191
|
-
(!t || typeof t != "object") &&
|
|
5300
|
+
(!t || typeof t != "object") && R("ai: each content part must be an object");
|
|
5192
5301
|
let r = t.type;
|
|
5193
5302
|
if (r === "text") {
|
|
5194
5303
|
let o = t.text;
|
|
5195
|
-
(typeof o != "string" || o.length === 0) &&
|
|
5304
|
+
(typeof o != "string" || o.length === 0) && R("ai: text part must have non-empty { text: string }"), o.length > Ue && R(`ai: text part exceeds ${Ue} chars`);
|
|
5196
5305
|
} else if (r === "image_url") {
|
|
5197
|
-
e !== "user" &&
|
|
5306
|
+
e !== "user" && R(`ai: image_url is only allowed in role 'user' messages (got '${e}')`);
|
|
5198
5307
|
let o = t.image_url;
|
|
5199
|
-
(!o || typeof o != "object") &&
|
|
5308
|
+
(!o || typeof o != "object") && R("ai: image_url part must have { image_url: { url } }"), jo(o.url);
|
|
5200
5309
|
let i = o.detail;
|
|
5201
|
-
i !== void 0 && (typeof i != "string" || !
|
|
5310
|
+
i !== void 0 && (typeof i != "string" || !No.has(i)) && R("ai: image_url.detail must be 'low' | 'high' | 'auto'");
|
|
5202
5311
|
} else
|
|
5203
|
-
|
|
5312
|
+
R("ai: content part type must be 'text' or 'image_url'");
|
|
5204
5313
|
}
|
|
5205
5314
|
}
|
|
5206
|
-
function
|
|
5207
|
-
(!n || typeof n != "object") &&
|
|
5315
|
+
function $o(n) {
|
|
5316
|
+
(!n || typeof n != "object") && R("ai: responseFormat must be an object");
|
|
5208
5317
|
let e = n.type;
|
|
5209
5318
|
if (!(e === "text" || e === "json_object")) {
|
|
5210
5319
|
if (e === "json_schema") {
|
|
5211
5320
|
let t = n.json_schema;
|
|
5212
|
-
(!t || typeof t != "object") &&
|
|
5321
|
+
(!t || typeof t != "object") && R("ai: responseFormat json_schema requires { json_schema: { name } }");
|
|
5213
5322
|
let r = t, o = r.name;
|
|
5214
|
-
if ((typeof o != "string" || o.length === 0) &&
|
|
5215
|
-
let i =
|
|
5216
|
-
i &&
|
|
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"), Lo.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) {
|
|
5324
|
+
let i = Fo(r.schema);
|
|
5325
|
+
i && R(i);
|
|
5217
5326
|
}
|
|
5218
5327
|
return;
|
|
5219
5328
|
}
|
|
5220
|
-
|
|
5329
|
+
R("ai: responseFormat.type must be 'text' | 'json_object' | 'json_schema'");
|
|
5221
5330
|
}
|
|
5222
5331
|
}
|
|
5223
|
-
function
|
|
5332
|
+
function qn(n, e, t) {
|
|
5224
5333
|
if (!e || typeof e != "object")
|
|
5225
5334
|
throw new exports.NeetruError("validation_failed", "ai: options object is required");
|
|
5226
5335
|
if (typeof e.model != "string" || e.model.length === 0)
|
|
5227
5336
|
throw new exports.NeetruError("validation_failed", "ai: model is required");
|
|
5228
|
-
if (e.model.length >
|
|
5229
|
-
throw new exports.NeetruError("validation_failed", `ai: model exceeds ${
|
|
5337
|
+
if (e.model.length > An)
|
|
5338
|
+
throw new exports.NeetruError("validation_failed", `ai: model exceeds ${An} chars`);
|
|
5230
5339
|
if (!Array.isArray(e.messages) || e.messages.length === 0)
|
|
5231
5340
|
throw new exports.NeetruError("validation_failed", "ai: messages must be a non-empty array");
|
|
5232
|
-
if (e.messages.length >
|
|
5233
|
-
throw new exports.NeetruError("validation_failed", `ai: messages exceeds ${
|
|
5341
|
+
if (e.messages.length > Cn)
|
|
5342
|
+
throw new exports.NeetruError("validation_failed", `ai: messages exceeds ${Cn} per request`);
|
|
5234
5343
|
for (let a of e.messages) {
|
|
5235
|
-
if (!a || typeof a != "object" || typeof a.role != "string" || !
|
|
5344
|
+
if (!a || typeof a != "object" || typeof a.role != "string" || !xo.has(a.role))
|
|
5236
5345
|
throw new exports.NeetruError(
|
|
5237
5346
|
"validation_failed",
|
|
5238
5347
|
"ai: each message role must be 'system' | 'user' | 'assistant'"
|
|
5239
5348
|
);
|
|
5240
|
-
|
|
5349
|
+
Wo(a.content, a.role);
|
|
5241
5350
|
}
|
|
5242
|
-
let r =
|
|
5351
|
+
let r = qo(e.messages);
|
|
5243
5352
|
if (r) throw new exports.NeetruError("validation_failed", r);
|
|
5244
5353
|
let o = e.productId ?? n.productId, i = e.tenantId ?? n.tenantId;
|
|
5245
5354
|
if (!o)
|
|
@@ -5261,24 +5370,24 @@ function Rn(n, e, t) {
|
|
|
5261
5370
|
stream: t
|
|
5262
5371
|
};
|
|
5263
5372
|
if (e.maxTokens !== void 0) {
|
|
5264
|
-
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens >
|
|
5373
|
+
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens > En)
|
|
5265
5374
|
throw new exports.NeetruError(
|
|
5266
5375
|
"validation_failed",
|
|
5267
|
-
`ai: maxTokens must be a positive integer <= ${
|
|
5376
|
+
`ai: maxTokens must be a positive integer <= ${En}`
|
|
5268
5377
|
);
|
|
5269
5378
|
s.maxTokens = e.maxTokens;
|
|
5270
5379
|
}
|
|
5271
5380
|
if (e.temperature !== void 0) {
|
|
5272
|
-
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature <
|
|
5381
|
+
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature < Pn || e.temperature > Dn)
|
|
5273
5382
|
throw new exports.NeetruError(
|
|
5274
5383
|
"validation_failed",
|
|
5275
|
-
`ai: temperature must be a finite number between ${
|
|
5384
|
+
`ai: temperature must be a finite number between ${Pn} and ${Dn}`
|
|
5276
5385
|
);
|
|
5277
5386
|
s.temperature = e.temperature;
|
|
5278
5387
|
}
|
|
5279
|
-
return e.responseFormat !== void 0 && (
|
|
5388
|
+
return e.responseFormat !== void 0 && ($o(e.responseFormat), s.responseFormat = e.responseFormat), s;
|
|
5280
5389
|
}
|
|
5281
|
-
async function
|
|
5390
|
+
async function $n(n) {
|
|
5282
5391
|
try {
|
|
5283
5392
|
let e = await n.text();
|
|
5284
5393
|
return e ? JSON.parse(e) : void 0;
|
|
@@ -5286,48 +5395,48 @@ async function En(n) {
|
|
|
5286
5395
|
return;
|
|
5287
5396
|
}
|
|
5288
5397
|
}
|
|
5289
|
-
function
|
|
5398
|
+
function Vo(n, e) {
|
|
5290
5399
|
let t = n.replace(/\/+$/, ""), r = e.startsWith("/") ? e : `/${e}`;
|
|
5291
5400
|
return `${t}${r}`;
|
|
5292
5401
|
}
|
|
5293
|
-
async function
|
|
5402
|
+
async function Fn(n, e, t, r) {
|
|
5294
5403
|
if (!n.apiKey)
|
|
5295
5404
|
throw new exports.NeetruError(
|
|
5296
5405
|
"missing_api_key",
|
|
5297
5406
|
"ai requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
5298
5407
|
);
|
|
5299
|
-
let o =
|
|
5408
|
+
let o = Vo(n.baseUrl, Eo), { headers: i, correlationId: s } = _e({
|
|
5300
5409
|
"content-type": "application/json",
|
|
5301
5410
|
accept: r ? "text/event-stream" : "application/json",
|
|
5302
5411
|
authorization: `Bearer ${n.apiKey}`
|
|
5303
|
-
}, body: JSON.stringify(e) };
|
|
5304
|
-
r ? t && (
|
|
5305
|
-
let
|
|
5412
|
+
}), a = { method: "POST", headers: i, body: JSON.stringify(e) };
|
|
5413
|
+
r ? t && (a.signal = t) : a.signal = t ?? AbortSignal.timeout(Sn);
|
|
5414
|
+
let c;
|
|
5306
5415
|
try {
|
|
5307
|
-
|
|
5308
|
-
} catch (
|
|
5309
|
-
let
|
|
5310
|
-
throw new exports.NeetruError("network_error",
|
|
5311
|
-
}
|
|
5312
|
-
let
|
|
5313
|
-
if (!
|
|
5314
|
-
let
|
|
5315
|
-
if (
|
|
5316
|
-
let
|
|
5317
|
-
typeof
|
|
5416
|
+
c = await n.fetch(o, a);
|
|
5417
|
+
} catch (p) {
|
|
5418
|
+
let f = p instanceof DOMException && p.name === "TimeoutError" ? `Network error: timeout after ${Sn / 1e3}s` : p instanceof DOMException && p.name === "AbortError" ? "Network error: request aborted" : `Network error: ${p instanceof Error ? p.message : "fetch failed"}`;
|
|
5419
|
+
throw new exports.NeetruError("network_error", f, void 0, void 0, void 0, s);
|
|
5420
|
+
}
|
|
5421
|
+
let u = c.headers.get("x-request-id") ?? c.headers.get("x-correlation-id") ?? void 0;
|
|
5422
|
+
if (!c.ok) {
|
|
5423
|
+
let p = await $n(c), f = ve(c.status), m = `HTTP ${c.status}`;
|
|
5424
|
+
if (p && typeof p == "object" && "error" in p) {
|
|
5425
|
+
let h = p.error;
|
|
5426
|
+
typeof h == "string" ? m = h : h && typeof h == "object" && (typeof h.code == "string" && (f = h.code), typeof h.message == "string" && (m = h.message));
|
|
5318
5427
|
}
|
|
5319
|
-
throw new exports.NeetruError(
|
|
5428
|
+
throw new exports.NeetruError(f, m, c.status, u, void 0, s);
|
|
5320
5429
|
}
|
|
5321
|
-
let
|
|
5322
|
-
return
|
|
5430
|
+
let d = c.headers.get("x-neetru-version");
|
|
5431
|
+
return d && te(d), c;
|
|
5323
5432
|
}
|
|
5324
|
-
function
|
|
5433
|
+
function jn(n) {
|
|
5325
5434
|
return {
|
|
5326
5435
|
provider: n.headers.get("x-neetru-provider") ?? "",
|
|
5327
5436
|
usageWarning: n.headers.get("x-neetru-ai-usage-warning") === "1"
|
|
5328
5437
|
};
|
|
5329
5438
|
}
|
|
5330
|
-
function
|
|
5439
|
+
function Wn(n) {
|
|
5331
5440
|
let e = [];
|
|
5332
5441
|
for (let r of n.split(`
|
|
5333
5442
|
`)) {
|
|
@@ -5348,10 +5457,10 @@ function An(n) {
|
|
|
5348
5457
|
return null;
|
|
5349
5458
|
}
|
|
5350
5459
|
}
|
|
5351
|
-
function
|
|
5460
|
+
function ct(n) {
|
|
5352
5461
|
return {
|
|
5353
5462
|
async chat(e) {
|
|
5354
|
-
let t =
|
|
5463
|
+
let t = qn(n, e, false), r = await Fn(n, t, e.signal, false), { provider: o, usageWarning: i } = jn(r), s = await $n(r);
|
|
5355
5464
|
if (!s || s.ok !== true || !Array.isArray(s.choices))
|
|
5356
5465
|
throw new exports.NeetruError("invalid_response", "ai.chat response missing ok/choices");
|
|
5357
5466
|
return {
|
|
@@ -5363,38 +5472,38 @@ function tt(n) {
|
|
|
5363
5472
|
};
|
|
5364
5473
|
},
|
|
5365
5474
|
stream(e) {
|
|
5366
|
-
let t =
|
|
5475
|
+
let t = qn(n, e, true), r = Fn(n, t, e.signal, true), o = r.then(jn);
|
|
5367
5476
|
o.catch(() => {
|
|
5368
5477
|
});
|
|
5369
5478
|
async function* i() {
|
|
5370
5479
|
let a = (await r).body;
|
|
5371
5480
|
if (!a)
|
|
5372
5481
|
throw new exports.NeetruError("invalid_response", "ai.stream: response has no body");
|
|
5373
|
-
let
|
|
5482
|
+
let c = a.getReader(), u = new TextDecoder(), d = "";
|
|
5374
5483
|
try {
|
|
5375
5484
|
for (; ; ) {
|
|
5376
|
-
let { done: f, value: m } = await
|
|
5485
|
+
let { done: f, value: m } = await c.read();
|
|
5377
5486
|
if (f) break;
|
|
5378
|
-
|
|
5487
|
+
d += u.decode(m, { stream: !0 });
|
|
5379
5488
|
let h;
|
|
5380
|
-
for (; (h =
|
|
5489
|
+
for (; (h = d.indexOf(`
|
|
5381
5490
|
|
|
5382
5491
|
`)) !== -1; ) {
|
|
5383
|
-
let
|
|
5384
|
-
|
|
5385
|
-
let
|
|
5386
|
-
if (
|
|
5387
|
-
|
|
5492
|
+
let y = d.slice(0, h);
|
|
5493
|
+
d = d.slice(h + 2);
|
|
5494
|
+
let I = Wn(y);
|
|
5495
|
+
if (I && "done" in I) return;
|
|
5496
|
+
I && "content" in I && (yield I.content);
|
|
5388
5497
|
}
|
|
5389
5498
|
}
|
|
5390
|
-
let p =
|
|
5499
|
+
let p = d.trim();
|
|
5391
5500
|
if (p) {
|
|
5392
|
-
let f =
|
|
5501
|
+
let f = Wn(p);
|
|
5393
5502
|
f && "content" in f && (yield f.content);
|
|
5394
5503
|
}
|
|
5395
5504
|
} finally {
|
|
5396
5505
|
try {
|
|
5397
|
-
|
|
5506
|
+
c.releaseLock();
|
|
5398
5507
|
} catch {
|
|
5399
5508
|
}
|
|
5400
5509
|
try {
|
|
@@ -5413,10 +5522,10 @@ function tt(n) {
|
|
|
5413
5522
|
|
|
5414
5523
|
// src/mocks.ts
|
|
5415
5524
|
z();
|
|
5416
|
-
|
|
5525
|
+
C();
|
|
5417
5526
|
|
|
5418
5527
|
// src/runtime-env.ts
|
|
5419
|
-
function
|
|
5528
|
+
function Me() {
|
|
5420
5529
|
try {
|
|
5421
5530
|
if (typeof process < "u" && process.env)
|
|
5422
5531
|
return process.env.NODE_ENV === "production";
|
|
@@ -5426,19 +5535,19 @@ function Pe() {
|
|
|
5426
5535
|
}
|
|
5427
5536
|
|
|
5428
5537
|
// src/mocks.ts
|
|
5429
|
-
var
|
|
5538
|
+
var qe = Object.freeze({
|
|
5430
5539
|
uid: "dev-fixture-uid-0001",
|
|
5431
5540
|
email: "dev@neetru.local",
|
|
5432
5541
|
emailVerified: true,
|
|
5433
5542
|
displayName: "Dev Fixture",
|
|
5434
5543
|
isCustomer: true,
|
|
5435
5544
|
isStaff: false
|
|
5436
|
-
}),
|
|
5545
|
+
}), le = class {
|
|
5437
5546
|
_user;
|
|
5438
5547
|
_listeners;
|
|
5439
5548
|
_allowUnsignedDecode;
|
|
5440
5549
|
constructor(e = null, t) {
|
|
5441
|
-
if (this._user = e, this._listeners = /* @__PURE__ */ new Set(), this._allowUnsignedDecode = t?.allowUnsignedDecode ?? false, this._allowUnsignedDecode &&
|
|
5550
|
+
if (this._user = e, this._listeners = /* @__PURE__ */ new Set(), this._allowUnsignedDecode = t?.allowUnsignedDecode ?? false, this._allowUnsignedDecode && Me())
|
|
5442
5551
|
throw new exports.NeetruError(
|
|
5443
5552
|
"invalid_config",
|
|
5444
5553
|
"MockAuth com allowUnsignedDecode=true \xE9 PROIBIDO em produ\xE7\xE3o (NODE_ENV=production): decodificar JWT sem verificar assinatura \xE9 bypass de autentica\xE7\xE3o. Este modo existe exclusivamente para testes."
|
|
@@ -5448,7 +5557,7 @@ var De = Object.freeze({
|
|
|
5448
5557
|
);
|
|
5449
5558
|
}
|
|
5450
5559
|
async signIn(e) {
|
|
5451
|
-
return this._user || (this._user = { ...
|
|
5560
|
+
return this._user || (this._user = { ...qe }), this._notify(), this._user;
|
|
5452
5561
|
}
|
|
5453
5562
|
async signOut() {
|
|
5454
5563
|
this._user = null, this._notify();
|
|
@@ -5569,7 +5678,7 @@ var De = Object.freeze({
|
|
|
5569
5678
|
} catch {
|
|
5570
5679
|
}
|
|
5571
5680
|
}
|
|
5572
|
-
},
|
|
5681
|
+
}, de = class {
|
|
5573
5682
|
_reports = [];
|
|
5574
5683
|
_quotas;
|
|
5575
5684
|
_counters = /* @__PURE__ */ new Map();
|
|
@@ -5586,14 +5695,14 @@ var De = Object.freeze({
|
|
|
5586
5695
|
tenantId: r?.tenantId,
|
|
5587
5696
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
5588
5697
|
});
|
|
5589
|
-
let
|
|
5698
|
+
let c = this._quotas.get(e)?.limit ?? -1;
|
|
5590
5699
|
return {
|
|
5591
5700
|
ok: true,
|
|
5592
5701
|
counterId: `mock_${e}`,
|
|
5593
5702
|
value: s,
|
|
5594
|
-
limit:
|
|
5595
|
-
remaining:
|
|
5596
|
-
status:
|
|
5703
|
+
limit: c,
|
|
5704
|
+
remaining: c < 0 ? -1 : Math.max(0, c - s),
|
|
5705
|
+
status: c > 0 && s >= c ? "read_only_overlimit" : "ok"
|
|
5597
5706
|
};
|
|
5598
5707
|
}
|
|
5599
5708
|
async check(e, t) {
|
|
@@ -5631,7 +5740,7 @@ var De = Object.freeze({
|
|
|
5631
5740
|
__reset() {
|
|
5632
5741
|
this._reports = [], this._quotas.clear(), this._counters.clear();
|
|
5633
5742
|
}
|
|
5634
|
-
},
|
|
5743
|
+
}, ue = class {
|
|
5635
5744
|
_tickets = [];
|
|
5636
5745
|
_ticketSeq = 0;
|
|
5637
5746
|
async createTicket(e) {
|
|
@@ -5708,7 +5817,7 @@ var De = Object.freeze({
|
|
|
5708
5817
|
__reset() {
|
|
5709
5818
|
this._tickets = [], this._ticketSeq = 0;
|
|
5710
5819
|
}
|
|
5711
|
-
},
|
|
5820
|
+
}, pe = class {
|
|
5712
5821
|
_denied = /* @__PURE__ */ new Set();
|
|
5713
5822
|
async check(e, t) {
|
|
5714
5823
|
return !this._denied.has(`${e}:${t}`);
|
|
@@ -5730,7 +5839,7 @@ var De = Object.freeze({
|
|
|
5730
5839
|
__reset() {
|
|
5731
5840
|
this._denied.clear();
|
|
5732
5841
|
}
|
|
5733
|
-
},
|
|
5842
|
+
}, lt = class {
|
|
5734
5843
|
_store = /* @__PURE__ */ new Map();
|
|
5735
5844
|
_fixtures;
|
|
5736
5845
|
constructor(e = {}) {
|
|
@@ -5739,14 +5848,14 @@ var De = Object.freeze({
|
|
|
5739
5848
|
collection(e) {
|
|
5740
5849
|
let t = this._store, r = this._fixtures;
|
|
5741
5850
|
if (!t.has(e)) {
|
|
5742
|
-
let
|
|
5851
|
+
let d = r.get(e);
|
|
5743
5852
|
t.set(
|
|
5744
5853
|
e,
|
|
5745
|
-
|
|
5854
|
+
d ? new Map(Object.entries(d)) : /* @__PURE__ */ new Map()
|
|
5746
5855
|
);
|
|
5747
5856
|
}
|
|
5748
|
-
let o = t.get(e), i = (
|
|
5749
|
-
let f =
|
|
5857
|
+
let o = t.get(e), i = (d, p) => {
|
|
5858
|
+
let f = d[p.field];
|
|
5750
5859
|
switch (p.op) {
|
|
5751
5860
|
case "==":
|
|
5752
5861
|
return f === p.value;
|
|
@@ -5770,9 +5879,9 @@ var De = Object.freeze({
|
|
|
5770
5879
|
return true;
|
|
5771
5880
|
}
|
|
5772
5881
|
}, s = 0;
|
|
5773
|
-
function a(
|
|
5882
|
+
function a(d) {
|
|
5774
5883
|
return {
|
|
5775
|
-
docs:
|
|
5884
|
+
docs: d.map((p, f) => ({ id: p.id ?? String(f), data: p })),
|
|
5776
5885
|
nextCursor: null,
|
|
5777
5886
|
fromCache: false,
|
|
5778
5887
|
stale: false,
|
|
@@ -5780,9 +5889,9 @@ var De = Object.freeze({
|
|
|
5780
5889
|
changes: []
|
|
5781
5890
|
};
|
|
5782
5891
|
}
|
|
5783
|
-
function
|
|
5892
|
+
function c(d, p) {
|
|
5784
5893
|
return {
|
|
5785
|
-
docs: [{ id: p, data:
|
|
5894
|
+
docs: [{ id: p, data: d }],
|
|
5786
5895
|
fromCache: false,
|
|
5787
5896
|
stale: false,
|
|
5788
5897
|
hasPendingWrites: false,
|
|
@@ -5790,40 +5899,40 @@ var De = Object.freeze({
|
|
|
5790
5899
|
};
|
|
5791
5900
|
}
|
|
5792
5901
|
let u = {
|
|
5793
|
-
async list(
|
|
5902
|
+
async list(d) {
|
|
5794
5903
|
let p = Array.from(o.values());
|
|
5795
|
-
if (
|
|
5796
|
-
(m) =>
|
|
5797
|
-
)),
|
|
5798
|
-
let { field: m, direction: h } =
|
|
5799
|
-
p = p.slice().sort((
|
|
5800
|
-
let
|
|
5801
|
-
return h === "desc" ? -
|
|
5904
|
+
if (d?.where && d.where.length > 0 && (p = p.filter(
|
|
5905
|
+
(m) => d.where.every((h) => i(m, h))
|
|
5906
|
+
)), d?.orderBy) {
|
|
5907
|
+
let { field: m, direction: h } = d.orderBy;
|
|
5908
|
+
p = p.slice().sort((y, I) => {
|
|
5909
|
+
let w = y[m], k = I[m], N = w ?? "", g = k ?? "", _ = N < g ? -1 : N > g ? 1 : 0;
|
|
5910
|
+
return h === "desc" ? -_ : _;
|
|
5802
5911
|
});
|
|
5803
5912
|
}
|
|
5804
|
-
let f =
|
|
5913
|
+
let f = d?.limit ?? 20;
|
|
5805
5914
|
return p = p.slice(0, f), a(p);
|
|
5806
5915
|
},
|
|
5807
|
-
async get(
|
|
5808
|
-
let p = o.get(
|
|
5809
|
-
return p ?
|
|
5916
|
+
async get(d) {
|
|
5917
|
+
let p = o.get(d);
|
|
5918
|
+
return p ? c(p, d) : null;
|
|
5810
5919
|
},
|
|
5811
|
-
async add(
|
|
5920
|
+
async add(d) {
|
|
5812
5921
|
let p = `mock-${++s}-${Math.random().toString(36).slice(2, 8)}`;
|
|
5813
|
-
return o.set(p, { ...
|
|
5922
|
+
return o.set(p, { ...d, id: p }), { ok: true, id: p };
|
|
5814
5923
|
},
|
|
5815
|
-
async set(
|
|
5816
|
-
return o.set(
|
|
5924
|
+
async set(d, p) {
|
|
5925
|
+
return o.set(d, { ...p, id: d }), { ok: true };
|
|
5817
5926
|
},
|
|
5818
|
-
async update(
|
|
5819
|
-
let f = o.get(
|
|
5820
|
-
return f ? o.set(
|
|
5927
|
+
async update(d, p) {
|
|
5928
|
+
let f = o.get(d);
|
|
5929
|
+
return f ? o.set(d, { ...f, ...p }) : o.set(d, { ...p, id: d }), { ok: true };
|
|
5821
5930
|
},
|
|
5822
|
-
async remove(
|
|
5823
|
-
return o.delete(
|
|
5931
|
+
async remove(d) {
|
|
5932
|
+
return o.delete(d), { ok: true };
|
|
5824
5933
|
},
|
|
5825
|
-
async batch(
|
|
5826
|
-
for (let p of
|
|
5934
|
+
async batch(d) {
|
|
5935
|
+
for (let p of d) {
|
|
5827
5936
|
let f = o;
|
|
5828
5937
|
if (p.kind === "add") {
|
|
5829
5938
|
let m = `mock-batch-${++s}`;
|
|
@@ -5837,47 +5946,47 @@ var De = Object.freeze({
|
|
|
5837
5946
|
}
|
|
5838
5947
|
return { ok: true };
|
|
5839
5948
|
},
|
|
5840
|
-
onDoc(
|
|
5841
|
-
let f = o.get(
|
|
5949
|
+
onDoc(d, p) {
|
|
5950
|
+
let f = o.get(d);
|
|
5842
5951
|
return Promise.resolve().then(() => p(f || null)), () => {
|
|
5843
5952
|
};
|
|
5844
5953
|
},
|
|
5845
|
-
onSnapshot(
|
|
5954
|
+
onSnapshot(d, p) {
|
|
5846
5955
|
return Promise.resolve().then(async () => {
|
|
5847
|
-
let f = await u.list(
|
|
5956
|
+
let f = await u.list(d);
|
|
5848
5957
|
p(f);
|
|
5849
5958
|
}), () => {
|
|
5850
5959
|
};
|
|
5851
5960
|
},
|
|
5852
|
-
doc(
|
|
5961
|
+
doc(d) {
|
|
5853
5962
|
return {
|
|
5854
5963
|
get id() {
|
|
5855
|
-
return
|
|
5964
|
+
return d;
|
|
5856
5965
|
},
|
|
5857
5966
|
get path() {
|
|
5858
|
-
return `${e}/${
|
|
5967
|
+
return `${e}/${d}`;
|
|
5859
5968
|
},
|
|
5860
5969
|
get collection() {
|
|
5861
5970
|
return e;
|
|
5862
5971
|
},
|
|
5863
5972
|
async get() {
|
|
5864
|
-
let p = o.get(
|
|
5865
|
-
return p ?
|
|
5973
|
+
let p = o.get(d);
|
|
5974
|
+
return p ? c(p, d) : null;
|
|
5866
5975
|
},
|
|
5867
5976
|
async set(p) {
|
|
5868
|
-
return o.set(
|
|
5977
|
+
return o.set(d, { ...p, id: d }), { ok: true };
|
|
5869
5978
|
},
|
|
5870
5979
|
async update(p) {
|
|
5871
|
-
let f = o.get(
|
|
5872
|
-
return f ? o.set(
|
|
5980
|
+
let f = o.get(d);
|
|
5981
|
+
return f ? o.set(d, { ...f, ...p }) : o.set(d, { ...p, id: d }), { ok: true };
|
|
5873
5982
|
},
|
|
5874
5983
|
async remove() {
|
|
5875
|
-
return o.delete(
|
|
5984
|
+
return o.delete(d), { ok: true };
|
|
5876
5985
|
},
|
|
5877
5986
|
onSnapshot(p) {
|
|
5878
|
-
let f = o.get(
|
|
5987
|
+
let f = o.get(d);
|
|
5879
5988
|
return Promise.resolve().then(
|
|
5880
|
-
() => p(f ?
|
|
5989
|
+
() => p(f ? c(f, d) : null)
|
|
5881
5990
|
), () => {
|
|
5882
5991
|
};
|
|
5883
5992
|
}
|
|
@@ -5915,13 +6024,13 @@ var De = Object.freeze({
|
|
|
5915
6024
|
/* webpackIgnore: true */
|
|
5916
6025
|
'drizzle-orm/pglite'
|
|
5917
6026
|
), h = await f.PGlite.create();
|
|
5918
|
-
for (let
|
|
5919
|
-
await h.query(
|
|
5920
|
-
let
|
|
6027
|
+
for (let w of r)
|
|
6028
|
+
await h.query(w);
|
|
6029
|
+
let y = m.drizzle(h, { schema: e });
|
|
5921
6030
|
return {
|
|
5922
|
-
orm:
|
|
5923
|
-
async transaction(
|
|
5924
|
-
return await
|
|
6031
|
+
orm: y,
|
|
6032
|
+
async transaction(w, k) {
|
|
6033
|
+
return await y.transaction(w);
|
|
5925
6034
|
},
|
|
5926
6035
|
async close() {
|
|
5927
6036
|
h.closed || await h.close();
|
|
@@ -5948,13 +6057,13 @@ var De = Object.freeze({
|
|
|
5948
6057
|
if (r.length > 0)
|
|
5949
6058
|
for (let f of r)
|
|
5950
6059
|
s.public.none(f);
|
|
5951
|
-
let
|
|
6060
|
+
let c = s.adapters.createPg().Pool, u = new c(), d = u.query.bind(u);
|
|
5952
6061
|
u.query = (f, ...m) => {
|
|
5953
6062
|
if (f !== null && typeof f == "object") {
|
|
5954
|
-
let
|
|
5955
|
-
|
|
6063
|
+
let y = f.types;
|
|
6064
|
+
y !== null && typeof y == "object" && "getTypeParser" in y && delete y.getTypeParser;
|
|
5956
6065
|
}
|
|
5957
|
-
return
|
|
6066
|
+
return d(f, ...m);
|
|
5958
6067
|
};
|
|
5959
6068
|
let p = i.drizzle(u, { schema: e });
|
|
5960
6069
|
return {
|
|
@@ -5996,7 +6105,7 @@ var De = Object.freeze({
|
|
|
5996
6105
|
__reset() {
|
|
5997
6106
|
this._store.clear();
|
|
5998
6107
|
}
|
|
5999
|
-
},
|
|
6108
|
+
}, fe = class {
|
|
6000
6109
|
_calls = [];
|
|
6001
6110
|
/** Extrai texto de um content string OU array multimodal (imagens ignoradas). */
|
|
6002
6111
|
_contentToText(e) {
|
|
@@ -6044,8 +6153,8 @@ var De = Object.freeze({
|
|
|
6044
6153
|
};
|
|
6045
6154
|
|
|
6046
6155
|
// src/auth-pkce-shared.ts
|
|
6047
|
-
|
|
6048
|
-
function
|
|
6156
|
+
C();
|
|
6157
|
+
function Vn() {
|
|
6049
6158
|
let n = globalThis.crypto;
|
|
6050
6159
|
if (!n || typeof n.getRandomValues != "function" || !n.subtle)
|
|
6051
6160
|
throw new exports.NeetruError(
|
|
@@ -6054,36 +6163,36 @@ function Pn() {
|
|
|
6054
6163
|
);
|
|
6055
6164
|
return n;
|
|
6056
6165
|
}
|
|
6057
|
-
function
|
|
6166
|
+
function Bn(n) {
|
|
6058
6167
|
let e = "";
|
|
6059
6168
|
for (let r of n) e += String.fromCharCode(r);
|
|
6060
6169
|
return (typeof btoa == "function" ? btoa(e) : Buffer.from(e, "binary").toString("base64")).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
6061
6170
|
}
|
|
6062
|
-
function
|
|
6171
|
+
function Fe(n, e) {
|
|
6063
6172
|
let t = new Uint8Array(e);
|
|
6064
|
-
return n.getRandomValues(t),
|
|
6173
|
+
return n.getRandomValues(t), Bn(t);
|
|
6065
6174
|
}
|
|
6066
|
-
async function
|
|
6175
|
+
async function Kn(n, e) {
|
|
6067
6176
|
let t = new TextEncoder().encode(e), r = await n.subtle.digest("SHA-256", t);
|
|
6068
|
-
return
|
|
6177
|
+
return Bn(new Uint8Array(r));
|
|
6069
6178
|
}
|
|
6070
|
-
function
|
|
6179
|
+
function me(n) {
|
|
6071
6180
|
return (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? n.replace(/^https?:\/\/api\./, "https://auth.");
|
|
6072
6181
|
}
|
|
6073
|
-
function
|
|
6182
|
+
function zn(n) {
|
|
6074
6183
|
let e = n.oidcClientId;
|
|
6075
|
-
if (!e && n.apiKey && (e =
|
|
6184
|
+
if (!e && n.apiKey && (e = gt(n.apiKey).keyId), !e)
|
|
6076
6185
|
throw new exports.NeetruError(
|
|
6077
6186
|
"invalid_config",
|
|
6078
6187
|
"auth OIDC requer `oidcClientId` (ou `apiKey`) configurado para montar o client_id."
|
|
6079
6188
|
);
|
|
6080
6189
|
return e;
|
|
6081
6190
|
}
|
|
6082
|
-
function
|
|
6191
|
+
function Hn(n) {
|
|
6083
6192
|
let e = new URL("/api/v1/oauth/authorize", n.idpOrigin);
|
|
6084
6193
|
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;
|
|
6085
6194
|
}
|
|
6086
|
-
function
|
|
6195
|
+
function dt(n) {
|
|
6087
6196
|
let e = n.split(".");
|
|
6088
6197
|
if (e.length !== 3) return null;
|
|
6089
6198
|
try {
|
|
@@ -6093,8 +6202,8 @@ function rt(n) {
|
|
|
6093
6202
|
return null;
|
|
6094
6203
|
}
|
|
6095
6204
|
}
|
|
6096
|
-
function
|
|
6097
|
-
let e =
|
|
6205
|
+
function he(n) {
|
|
6206
|
+
let e = dt(n);
|
|
6098
6207
|
return !e || typeof e.sub != "string" || typeof e.exp == "number" && e.exp * 1e3 < Date.now() ? null : {
|
|
6099
6208
|
uid: e.sub,
|
|
6100
6209
|
email: typeof e.email == "string" ? e.email : "",
|
|
@@ -6107,9 +6216,68 @@ function ue(n) {
|
|
|
6107
6216
|
...e
|
|
6108
6217
|
};
|
|
6109
6218
|
}
|
|
6219
|
+
var Bo = "/api/v1/oauth/revoke";
|
|
6220
|
+
async function Gn(n, e, t, r) {
|
|
6221
|
+
let o = `${e}${Bo}`, i = true;
|
|
6222
|
+
for (let s of r) {
|
|
6223
|
+
if (!s.token) continue;
|
|
6224
|
+
let a = new URLSearchParams({
|
|
6225
|
+
token: s.token,
|
|
6226
|
+
token_type_hint: s.hint,
|
|
6227
|
+
client_id: t
|
|
6228
|
+
}).toString(), c = false;
|
|
6229
|
+
for (let u = 0; u < 2 && !c; u++)
|
|
6230
|
+
try {
|
|
6231
|
+
if (c = (await n(o, {
|
|
6232
|
+
method: "POST",
|
|
6233
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
6234
|
+
body: a
|
|
6235
|
+
})).ok, !c) break;
|
|
6236
|
+
} catch {
|
|
6237
|
+
u === 0 && await new Promise((d) => setTimeout(d, 500));
|
|
6238
|
+
}
|
|
6239
|
+
c || (i = false);
|
|
6240
|
+
}
|
|
6241
|
+
return i;
|
|
6242
|
+
}
|
|
6110
6243
|
|
|
6111
6244
|
// src/auth.ts
|
|
6112
|
-
|
|
6245
|
+
be();
|
|
6246
|
+
|
|
6247
|
+
// src/end-user-token.ts
|
|
6248
|
+
var Ko = /* @__PURE__ */ new WeakMap();
|
|
6249
|
+
function Qn(n, e) {
|
|
6250
|
+
Ko.set(n, e);
|
|
6251
|
+
}
|
|
6252
|
+
|
|
6253
|
+
// src/unsupported-db.ts
|
|
6254
|
+
C();
|
|
6255
|
+
var zo = "[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.)", Ho = /* @__PURE__ */ new Set([
|
|
6256
|
+
"then",
|
|
6257
|
+
"toJSON",
|
|
6258
|
+
"valueOf",
|
|
6259
|
+
"constructor",
|
|
6260
|
+
"$$typeof",
|
|
6261
|
+
"asymmetricMatch",
|
|
6262
|
+
"nodeType"
|
|
6263
|
+
]);
|
|
6264
|
+
function Jn() {
|
|
6265
|
+
let n = () => {
|
|
6266
|
+
throw new exports.NeetruError("runtime_unsupported", zo, void 0, void 0, "react-native");
|
|
6267
|
+
}, e = {};
|
|
6268
|
+
return new Proxy(e, {
|
|
6269
|
+
get(t, r) {
|
|
6270
|
+
if (!(typeof r == "symbol" || Ho.has(r)))
|
|
6271
|
+
return r === "toString" ? () => "[NeetruDb indispon\xEDvel em React Native]" : n;
|
|
6272
|
+
},
|
|
6273
|
+
set() {
|
|
6274
|
+
return n(), false;
|
|
6275
|
+
}
|
|
6276
|
+
});
|
|
6277
|
+
}
|
|
6278
|
+
|
|
6279
|
+
// src/auth.ts
|
|
6280
|
+
function Qo() {
|
|
6113
6281
|
try {
|
|
6114
6282
|
let e = globalThis.process?.env?.NEETRU_API_KEY;
|
|
6115
6283
|
return typeof e == "string" && e.length > 0 ? e : void 0;
|
|
@@ -6117,7 +6285,7 @@ function bo() {
|
|
|
6117
6285
|
return;
|
|
6118
6286
|
}
|
|
6119
6287
|
}
|
|
6120
|
-
function
|
|
6288
|
+
function Jo(n) {
|
|
6121
6289
|
if (n) return n;
|
|
6122
6290
|
try {
|
|
6123
6291
|
let t = globalThis.process?.env?.NEETRU_ENV;
|
|
@@ -6131,15 +6299,8 @@ function vo(n) {
|
|
|
6131
6299
|
}
|
|
6132
6300
|
return "prod";
|
|
6133
6301
|
}
|
|
6134
|
-
var
|
|
6135
|
-
function
|
|
6136
|
-
let t = Date.now(), r = Ln.get(n);
|
|
6137
|
-
if (r && t - r.createdAt < e)
|
|
6138
|
-
return r.jwks;
|
|
6139
|
-
let o = jose.createRemoteJWKSet(new URL(n));
|
|
6140
|
-
return Ln.set(n, { jwks: o, createdAt: t }), o;
|
|
6141
|
-
}
|
|
6142
|
-
function So(n) {
|
|
6302
|
+
var Xo = ye;
|
|
6303
|
+
function Yo(n) {
|
|
6143
6304
|
let e = n.sub ?? "", t = typeof n.email == "string" ? n.email : "";
|
|
6144
6305
|
return {
|
|
6145
6306
|
uid: e,
|
|
@@ -6154,8 +6315,8 @@ function So(n) {
|
|
|
6154
6315
|
...n
|
|
6155
6316
|
};
|
|
6156
6317
|
}
|
|
6157
|
-
var
|
|
6158
|
-
function
|
|
6318
|
+
var ge = "neetru_oauth_tx";
|
|
6319
|
+
function ut(n) {
|
|
6159
6320
|
try {
|
|
6160
6321
|
let e = globalThis.history;
|
|
6161
6322
|
if (!e || typeof e.replaceState != "function") return;
|
|
@@ -6166,7 +6327,7 @@ function ot(n) {
|
|
|
6166
6327
|
} catch {
|
|
6167
6328
|
}
|
|
6168
6329
|
}
|
|
6169
|
-
function
|
|
6330
|
+
function Zo(n) {
|
|
6170
6331
|
let e = "neetru_id_token", t = "neetru_refresh_token", r = /* @__PURE__ */ new Set(), o = null, i = null, s = null;
|
|
6171
6332
|
function a() {
|
|
6172
6333
|
try {
|
|
@@ -6175,7 +6336,7 @@ function To(n) {
|
|
|
6175
6336
|
return null;
|
|
6176
6337
|
}
|
|
6177
6338
|
}
|
|
6178
|
-
function
|
|
6339
|
+
function c() {
|
|
6179
6340
|
try {
|
|
6180
6341
|
return typeof globalThis.sessionStorage < "u" ? globalThis.sessionStorage : null;
|
|
6181
6342
|
} catch {
|
|
@@ -6184,21 +6345,21 @@ function To(n) {
|
|
|
6184
6345
|
}
|
|
6185
6346
|
function u() {
|
|
6186
6347
|
if (o) {
|
|
6187
|
-
let
|
|
6188
|
-
return typeof
|
|
6348
|
+
let b = o.exp;
|
|
6349
|
+
return typeof b == "number" && b * 1e3 < Date.now() ? (o = null, a()?.removeItem(e), d(), y(), null) : o;
|
|
6189
6350
|
}
|
|
6190
|
-
let
|
|
6191
|
-
if (!
|
|
6192
|
-
let
|
|
6193
|
-
if (!
|
|
6194
|
-
return
|
|
6195
|
-
let
|
|
6196
|
-
return
|
|
6197
|
-
}
|
|
6198
|
-
function
|
|
6199
|
-
for (let
|
|
6351
|
+
let g = a();
|
|
6352
|
+
if (!g) return null;
|
|
6353
|
+
let _ = g.getItem(e);
|
|
6354
|
+
if (!_)
|
|
6355
|
+
return y(), null;
|
|
6356
|
+
let T = he(_);
|
|
6357
|
+
return T ? (o = T, I(T), o) : (g.removeItem(e), y(), null);
|
|
6358
|
+
}
|
|
6359
|
+
function d() {
|
|
6360
|
+
for (let g of r)
|
|
6200
6361
|
try {
|
|
6201
|
-
|
|
6362
|
+
g(o);
|
|
6202
6363
|
} catch {
|
|
6203
6364
|
}
|
|
6204
6365
|
}
|
|
@@ -6210,212 +6371,212 @@ function To(n) {
|
|
|
6210
6371
|
}
|
|
6211
6372
|
function m() {
|
|
6212
6373
|
o = null;
|
|
6213
|
-
let
|
|
6214
|
-
|
|
6374
|
+
let g = a();
|
|
6375
|
+
g?.removeItem(e), g?.removeItem(t), d();
|
|
6215
6376
|
}
|
|
6216
6377
|
function h() {
|
|
6217
6378
|
return s || (s = (async () => {
|
|
6218
|
-
let
|
|
6219
|
-
if (!
|
|
6220
|
-
let
|
|
6379
|
+
let g = f();
|
|
6380
|
+
if (!g) return false;
|
|
6381
|
+
let _;
|
|
6221
6382
|
try {
|
|
6222
|
-
|
|
6383
|
+
_ = w();
|
|
6223
6384
|
} catch {
|
|
6224
6385
|
return false;
|
|
6225
6386
|
}
|
|
6226
|
-
let
|
|
6387
|
+
let b = `${me(n.baseUrl)}/api/v1/oauth/token`, E = new URLSearchParams({
|
|
6227
6388
|
grant_type: "refresh_token",
|
|
6228
|
-
refresh_token:
|
|
6229
|
-
client_id:
|
|
6230
|
-
}),
|
|
6389
|
+
refresh_token: g,
|
|
6390
|
+
client_id: _
|
|
6391
|
+
}), x;
|
|
6231
6392
|
try {
|
|
6232
|
-
|
|
6393
|
+
x = await n.fetch(b, {
|
|
6233
6394
|
method: "POST",
|
|
6234
6395
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
6235
|
-
body:
|
|
6396
|
+
body: E.toString()
|
|
6236
6397
|
});
|
|
6237
6398
|
} catch {
|
|
6238
6399
|
return false;
|
|
6239
6400
|
}
|
|
6240
|
-
let
|
|
6401
|
+
let D = null;
|
|
6241
6402
|
try {
|
|
6242
|
-
|
|
6403
|
+
D = await x.json();
|
|
6243
6404
|
} catch {
|
|
6244
|
-
|
|
6405
|
+
D = null;
|
|
6245
6406
|
}
|
|
6246
|
-
if (!
|
|
6247
|
-
let
|
|
6248
|
-
if (typeof
|
|
6249
|
-
let
|
|
6250
|
-
if (!
|
|
6251
|
-
let
|
|
6252
|
-
|
|
6253
|
-
let
|
|
6254
|
-
return typeof
|
|
6407
|
+
if (!x.ok || typeof D?.error == "string") return false;
|
|
6408
|
+
let A = D?.id_token;
|
|
6409
|
+
if (typeof A != "string" || A.length === 0) return false;
|
|
6410
|
+
let L = he(A);
|
|
6411
|
+
if (!L) return false;
|
|
6412
|
+
let P = a();
|
|
6413
|
+
P?.setItem(e, A);
|
|
6414
|
+
let M = D?.refresh_token;
|
|
6415
|
+
return typeof M == "string" && M.length > 0 && P?.setItem(t, M), o = L, I(L), d(), true;
|
|
6255
6416
|
})().finally(() => {
|
|
6256
6417
|
s = null;
|
|
6257
6418
|
}), s);
|
|
6258
6419
|
}
|
|
6259
|
-
function
|
|
6260
|
-
f() && h().then((
|
|
6261
|
-
|
|
6420
|
+
function y() {
|
|
6421
|
+
f() && h().then((g) => {
|
|
6422
|
+
g || m();
|
|
6262
6423
|
});
|
|
6263
6424
|
}
|
|
6264
|
-
function
|
|
6425
|
+
function I(g) {
|
|
6265
6426
|
p();
|
|
6266
|
-
let
|
|
6267
|
-
if (typeof
|
|
6268
|
-
let
|
|
6269
|
-
if (!(
|
|
6427
|
+
let _ = g.exp;
|
|
6428
|
+
if (typeof _ != "number") return;
|
|
6429
|
+
let T = _ * 1e3 - Date.now();
|
|
6430
|
+
if (!(T <= 0)) {
|
|
6270
6431
|
if (f()) {
|
|
6271
|
-
let
|
|
6432
|
+
let b = Math.min(T, Math.max(3e4, T * 0.2)), E = Math.max(1, T - b);
|
|
6272
6433
|
i = setTimeout(() => {
|
|
6273
|
-
i = null, h().then((
|
|
6274
|
-
|
|
6434
|
+
i = null, h().then((x) => {
|
|
6435
|
+
x || m();
|
|
6275
6436
|
});
|
|
6276
|
-
},
|
|
6437
|
+
}, E);
|
|
6277
6438
|
return;
|
|
6278
6439
|
}
|
|
6279
6440
|
i = setTimeout(() => {
|
|
6280
6441
|
i = null, u();
|
|
6281
|
-
},
|
|
6442
|
+
}, T + 1);
|
|
6282
6443
|
}
|
|
6283
6444
|
}
|
|
6284
|
-
function
|
|
6285
|
-
return
|
|
6445
|
+
function w() {
|
|
6446
|
+
return zn(n);
|
|
6286
6447
|
}
|
|
6287
|
-
async function
|
|
6288
|
-
let
|
|
6289
|
-
if (!
|
|
6290
|
-
let
|
|
6448
|
+
async function k(g) {
|
|
6449
|
+
let _ = g ?? (typeof globalThis.location < "u" ? globalThis.location.href : void 0);
|
|
6450
|
+
if (!_) return null;
|
|
6451
|
+
let T;
|
|
6291
6452
|
try {
|
|
6292
|
-
|
|
6453
|
+
T = new URL(_);
|
|
6293
6454
|
} catch {
|
|
6294
6455
|
return null;
|
|
6295
6456
|
}
|
|
6296
|
-
let
|
|
6297
|
-
if (
|
|
6298
|
-
let
|
|
6299
|
-
throw
|
|
6457
|
+
let b = T.searchParams, E = b.get("error");
|
|
6458
|
+
if (E) {
|
|
6459
|
+
let M = b.get("error_description") ?? E;
|
|
6460
|
+
throw c()?.removeItem(ge), ut(T), new exports.NeetruError(E, `OIDC authorize falhou: ${M}`);
|
|
6300
6461
|
}
|
|
6301
|
-
let
|
|
6302
|
-
if (!
|
|
6303
|
-
let
|
|
6304
|
-
if (
|
|
6462
|
+
let x = b.get("code");
|
|
6463
|
+
if (!x) return null;
|
|
6464
|
+
let D = b.get("state"), A = c(), L = A?.getItem(ge) ?? null, P = null;
|
|
6465
|
+
if (L)
|
|
6305
6466
|
try {
|
|
6306
|
-
|
|
6467
|
+
P = JSON.parse(L);
|
|
6307
6468
|
} catch {
|
|
6308
|
-
|
|
6469
|
+
P = null;
|
|
6309
6470
|
}
|
|
6310
|
-
if (!
|
|
6471
|
+
if (!P)
|
|
6311
6472
|
throw new exports.NeetruError(
|
|
6312
6473
|
"oidc_state_mismatch",
|
|
6313
6474
|
"Callback OIDC sem transa\xE7\xE3o pendente \u2014 `signIn` n\xE3o foi iniciado nesta sess\xE3o (ou j\xE1 consumido)."
|
|
6314
6475
|
);
|
|
6315
|
-
if (!
|
|
6316
|
-
throw
|
|
6476
|
+
if (!D || D !== P.state)
|
|
6477
|
+
throw A?.removeItem(ge), ut(T), new exports.NeetruError(
|
|
6317
6478
|
"oidc_state_mismatch",
|
|
6318
6479
|
"state do callback OIDC n\xE3o confere com o da transa\xE7\xE3o (poss\xEDvel CSRF)."
|
|
6319
6480
|
);
|
|
6320
|
-
return
|
|
6321
|
-
code:
|
|
6322
|
-
codeVerifier:
|
|
6323
|
-
redirectUri:
|
|
6324
|
-
nonce:
|
|
6325
|
-
state:
|
|
6326
|
-
postLoginRedirect:
|
|
6481
|
+
return A?.removeItem(ge), ut(T), {
|
|
6482
|
+
code: x,
|
|
6483
|
+
codeVerifier: P.codeVerifier,
|
|
6484
|
+
redirectUri: P.redirectUri,
|
|
6485
|
+
nonce: P.nonce,
|
|
6486
|
+
state: P.state,
|
|
6487
|
+
postLoginRedirect: P.postLoginRedirect
|
|
6327
6488
|
};
|
|
6328
6489
|
}
|
|
6329
|
-
async function
|
|
6330
|
-
let
|
|
6331
|
-
if (!
|
|
6332
|
-
let
|
|
6490
|
+
async function N(g) {
|
|
6491
|
+
let _ = await k(g);
|
|
6492
|
+
if (!_) return null;
|
|
6493
|
+
let T = w(), E = `${me(n.baseUrl)}/api/v1/oauth/token`, x = new URLSearchParams({
|
|
6333
6494
|
grant_type: "authorization_code",
|
|
6334
|
-
code:
|
|
6335
|
-
redirect_uri:
|
|
6336
|
-
client_id:
|
|
6337
|
-
code_verifier:
|
|
6338
|
-
}),
|
|
6495
|
+
code: _.code,
|
|
6496
|
+
redirect_uri: _.redirectUri,
|
|
6497
|
+
client_id: T,
|
|
6498
|
+
code_verifier: _.codeVerifier
|
|
6499
|
+
}), D;
|
|
6339
6500
|
try {
|
|
6340
|
-
|
|
6501
|
+
D = await n.fetch(E, {
|
|
6341
6502
|
method: "POST",
|
|
6342
6503
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
6343
|
-
body:
|
|
6504
|
+
body: x.toString()
|
|
6344
6505
|
});
|
|
6345
|
-
} catch (
|
|
6506
|
+
} catch (U) {
|
|
6346
6507
|
throw new exports.NeetruError(
|
|
6347
6508
|
"network_error",
|
|
6348
|
-
`POST ${
|
|
6509
|
+
`POST ${E} falhou (network): ${U?.message ?? String(U)}`
|
|
6349
6510
|
);
|
|
6350
6511
|
}
|
|
6351
|
-
let
|
|
6512
|
+
let A = null;
|
|
6352
6513
|
try {
|
|
6353
|
-
|
|
6514
|
+
A = await D.json();
|
|
6354
6515
|
} catch {
|
|
6355
|
-
|
|
6516
|
+
A = null;
|
|
6356
6517
|
}
|
|
6357
|
-
if (!
|
|
6358
|
-
let
|
|
6518
|
+
if (!D.ok || typeof A?.error == "string") {
|
|
6519
|
+
let U = typeof A?.error_description == "string" && A.error_description || typeof A?.error == "string" && A.error || `HTTP ${D.status}`;
|
|
6359
6520
|
throw new exports.NeetruError(
|
|
6360
6521
|
"token_exchange_failed",
|
|
6361
|
-
`Troca do authorization_code por tokens falhou no /token: ${
|
|
6362
|
-
|
|
6522
|
+
`Troca do authorization_code por tokens falhou no /token: ${U}`,
|
|
6523
|
+
D.status
|
|
6363
6524
|
);
|
|
6364
6525
|
}
|
|
6365
|
-
let
|
|
6366
|
-
if (typeof
|
|
6526
|
+
let L = A?.id_token;
|
|
6527
|
+
if (typeof L != "string" || L.length === 0)
|
|
6367
6528
|
throw new exports.NeetruError(
|
|
6368
6529
|
"token_exchange_failed",
|
|
6369
6530
|
"Resposta do /token sem `id_token` \u2014 o login n\xE3o pode ser completado.",
|
|
6370
|
-
|
|
6531
|
+
D.status
|
|
6371
6532
|
);
|
|
6372
|
-
let
|
|
6373
|
-
if (!
|
|
6533
|
+
let P = dt(L);
|
|
6534
|
+
if (!P)
|
|
6374
6535
|
throw new exports.NeetruError(
|
|
6375
6536
|
"token_exchange_failed",
|
|
6376
6537
|
"id_token retornado pelo /token \xE9 malformado (payload indecodific\xE1vel)."
|
|
6377
6538
|
);
|
|
6378
|
-
if (
|
|
6539
|
+
if (P.nonce !== _.nonce)
|
|
6379
6540
|
throw new exports.NeetruError(
|
|
6380
6541
|
"oidc_nonce_mismatch",
|
|
6381
6542
|
"nonce do id_token n\xE3o confere com o da transa\xE7\xE3o OIDC (poss\xEDvel replay). Token descartado."
|
|
6382
6543
|
);
|
|
6383
|
-
let
|
|
6384
|
-
if (!
|
|
6544
|
+
let M = he(L);
|
|
6545
|
+
if (!M)
|
|
6385
6546
|
throw new exports.NeetruError(
|
|
6386
6547
|
"token_exchange_failed",
|
|
6387
6548
|
"id_token retornado pelo /token n\xE3o tem `sub` ou j\xE1 est\xE1 expirado."
|
|
6388
6549
|
);
|
|
6389
|
-
let
|
|
6390
|
-
|
|
6391
|
-
let j =
|
|
6392
|
-
return typeof j == "string" && j.length > 0 &&
|
|
6550
|
+
let F = a();
|
|
6551
|
+
F?.setItem(e, L);
|
|
6552
|
+
let j = A?.refresh_token;
|
|
6553
|
+
return typeof j == "string" && j.length > 0 && F?.setItem(t, j), o = M, I(M), d(), { user: M, postLoginRedirect: _.postLoginRedirect };
|
|
6393
6554
|
}
|
|
6394
6555
|
return {
|
|
6395
|
-
async signIn(
|
|
6556
|
+
async signIn(g) {
|
|
6396
6557
|
if (typeof globalThis.location < "u" && typeof globalThis.location.assign == "function") {
|
|
6397
|
-
let
|
|
6398
|
-
if (!
|
|
6558
|
+
let _ = Vn(), 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 Kn(_, A), P = c();
|
|
6559
|
+
if (!P)
|
|
6399
6560
|
throw new exports.NeetruError(
|
|
6400
6561
|
"runtime_unsupported",
|
|
6401
6562
|
"auth.signIn requer sessionStorage para guardar a transa\xE7\xE3o PKCE entre o redirect e o callback."
|
|
6402
6563
|
);
|
|
6403
|
-
let
|
|
6404
|
-
state:
|
|
6405
|
-
nonce:
|
|
6406
|
-
codeVerifier:
|
|
6407
|
-
redirectUri:
|
|
6408
|
-
postLoginRedirect:
|
|
6564
|
+
let M = {
|
|
6565
|
+
state: x,
|
|
6566
|
+
nonce: D,
|
|
6567
|
+
codeVerifier: A,
|
|
6568
|
+
redirectUri: T,
|
|
6569
|
+
postLoginRedirect: g?.postLoginRedirect
|
|
6409
6570
|
};
|
|
6410
|
-
|
|
6411
|
-
let
|
|
6412
|
-
idpOrigin:
|
|
6413
|
-
clientId:
|
|
6414
|
-
redirectUri:
|
|
6415
|
-
scope:
|
|
6416
|
-
state:
|
|
6417
|
-
nonce:
|
|
6418
|
-
codeChallenge:
|
|
6571
|
+
P.setItem(ge, JSON.stringify(M));
|
|
6572
|
+
let F = me(n.baseUrl), j = Hn({
|
|
6573
|
+
idpOrigin: F,
|
|
6574
|
+
clientId: E,
|
|
6575
|
+
redirectUri: T,
|
|
6576
|
+
scope: b,
|
|
6577
|
+
state: x,
|
|
6578
|
+
nonce: D,
|
|
6579
|
+
codeChallenge: L
|
|
6419
6580
|
});
|
|
6420
6581
|
globalThis.location.assign(j.toString());
|
|
6421
6582
|
return;
|
|
@@ -6425,80 +6586,79 @@ function To(n) {
|
|
|
6425
6586
|
"auth.signIn requires a browser context or mocks. Use NEETRU_ENV=dev or pass mocks.auth."
|
|
6426
6587
|
);
|
|
6427
6588
|
},
|
|
6428
|
-
handleRedirectCallback:
|
|
6429
|
-
completeSignIn:
|
|
6589
|
+
handleRedirectCallback: k,
|
|
6590
|
+
completeSignIn: N,
|
|
6430
6591
|
// 3.7.0: exposto pro client.support renovar a sessão 1x num 401
|
|
6431
6592
|
// invalid_end_user_token (mesma função do refresh proativo, com dedup).
|
|
6432
6593
|
refreshTokens: h,
|
|
6433
6594
|
getIdToken() {
|
|
6434
|
-
let
|
|
6435
|
-
return
|
|
6595
|
+
let _ = a()?.getItem(e) ?? null;
|
|
6596
|
+
return _ && he(_) ? _ : null;
|
|
6436
6597
|
},
|
|
6437
6598
|
async signOut() {
|
|
6438
|
-
let
|
|
6439
|
-
|
|
6440
|
-
|
|
6441
|
-
for (let T = 0; T < 2; T++)
|
|
6599
|
+
let g = a(), _ = g?.getItem(t) ?? null;
|
|
6600
|
+
if (g && (g.removeItem(e), g.removeItem(t)), p(), o = null, _) {
|
|
6601
|
+
let T = null;
|
|
6442
6602
|
try {
|
|
6443
|
-
|
|
6444
|
-
|
|
6445
|
-
|
|
6446
|
-
body: N
|
|
6447
|
-
}), R = null;
|
|
6448
|
-
break;
|
|
6449
|
-
} catch (x) {
|
|
6450
|
-
R = x, T === 0 && await new Promise((E) => setTimeout(E, 500));
|
|
6603
|
+
T = w();
|
|
6604
|
+
} catch {
|
|
6605
|
+
T = null;
|
|
6451
6606
|
}
|
|
6452
|
-
|
|
6453
|
-
|
|
6454
|
-
|
|
6455
|
-
|
|
6607
|
+
!(T && await Gn(n.fetch, me(n.baseUrl), T, [
|
|
6608
|
+
{ token: _, hint: "refresh_token" }
|
|
6609
|
+
])) && typeof console < "u" && console.warn(
|
|
6610
|
+
"[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
|
+
);
|
|
6612
|
+
}
|
|
6613
|
+
d();
|
|
6456
6614
|
},
|
|
6457
6615
|
getUser() {
|
|
6458
6616
|
return u();
|
|
6459
6617
|
},
|
|
6460
|
-
onAuthStateChanged(
|
|
6461
|
-
r.add(
|
|
6618
|
+
onAuthStateChanged(g) {
|
|
6619
|
+
r.add(g);
|
|
6462
6620
|
try {
|
|
6463
|
-
|
|
6621
|
+
g(u());
|
|
6464
6622
|
} catch {
|
|
6465
6623
|
}
|
|
6466
6624
|
return () => {
|
|
6467
|
-
r.delete(
|
|
6625
|
+
r.delete(g);
|
|
6468
6626
|
};
|
|
6469
6627
|
},
|
|
6470
|
-
async verifyToken(
|
|
6471
|
-
if (!
|
|
6628
|
+
async verifyToken(g, _) {
|
|
6629
|
+
if (!g || typeof g != "string")
|
|
6472
6630
|
throw new exports.NeetruError("token_invalid_signature", "token must be a non-empty string");
|
|
6473
|
-
let
|
|
6474
|
-
if (!
|
|
6631
|
+
let b = (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? n.baseUrl.replace(/^https?:\/\/api\./, "https://auth."), E = b, x = `${b}/api/v1/oauth/.well-known/jwks`, D = _?.jwksCacheTtlMs ?? ee, A = _?.audience, L = yt(n.apiKey), P = A ?? n.oidcClientId ?? L?.keyId;
|
|
6632
|
+
if (!P)
|
|
6475
6633
|
throw new exports.NeetruError(
|
|
6476
6634
|
"invalid_config",
|
|
6477
6635
|
"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."
|
|
6478
6636
|
);
|
|
6479
|
-
let
|
|
6637
|
+
let M = Xo(x, D);
|
|
6480
6638
|
try {
|
|
6481
|
-
let { payload:
|
|
6482
|
-
issuer:
|
|
6483
|
-
audience:
|
|
6639
|
+
let { payload: F } = await jose.jwtVerify(g, M, {
|
|
6640
|
+
issuer: E,
|
|
6641
|
+
audience: P,
|
|
6484
6642
|
algorithms: ["RS256"]
|
|
6485
6643
|
});
|
|
6486
|
-
return
|
|
6487
|
-
} catch (
|
|
6488
|
-
let j =
|
|
6489
|
-
throw j === "ERR_JWT_EXPIRED" ? new exports.NeetruError("token_expired", `Token expired: ${
|
|
6644
|
+
return Yo(F);
|
|
6645
|
+
} catch (F) {
|
|
6646
|
+
let j = F?.code ?? F?.name ?? "", U = F?.message ?? String(F);
|
|
6647
|
+
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}`);
|
|
6490
6648
|
}
|
|
6491
6649
|
}
|
|
6492
6650
|
};
|
|
6493
6651
|
}
|
|
6494
|
-
function
|
|
6652
|
+
function Xn(n = {}) {
|
|
6495
6653
|
let e = n.fetch ?? globalThis.fetch;
|
|
6496
6654
|
if (typeof e != "function")
|
|
6497
6655
|
throw new exports.NeetruError(
|
|
6498
6656
|
"invalid_config",
|
|
6499
6657
|
"fetch is not available in this runtime. Pass `fetch` explicitly to createNeetruClient."
|
|
6500
6658
|
);
|
|
6501
|
-
let t = n.apiKey ??
|
|
6659
|
+
let t = n.apiKey ?? Qo();
|
|
6660
|
+
At({ hasApiKey: !!t, strict: n.strictServerOnlyKey === true });
|
|
6661
|
+
let r = Jo(n.env), o = n.baseUrl ?? (r === "dev-local" ? wt : je), i = vt(o, n.allowedHosts ?? []), s = Object.freeze({
|
|
6502
6662
|
apiKey: t,
|
|
6503
6663
|
oidcClientId: n.oidcClientId,
|
|
6504
6664
|
baseUrl: i,
|
|
@@ -6506,66 +6666,240 @@ function Un(n = {}) {
|
|
|
6506
6666
|
env: r,
|
|
6507
6667
|
productId: n.productId,
|
|
6508
6668
|
tenantId: n.tenantId
|
|
6509
|
-
}), a = r === "dev" && !
|
|
6510
|
-
getEndUserToken: () =>
|
|
6511
|
-
// `refreshTokens` é opcional
|
|
6669
|
+
}), a = r === "dev" && !Me(), c = n.mocks?.auth ?? (a ? new le({ ...qe }) : Zo(s)), u = n.mocks?.usage ?? (a ? new de() : Vt(s)), d = n.endUserAuth ?? c, p = n.mocks?.support ?? (a ? new ue() : Qt(s, {
|
|
6670
|
+
getEndUserToken: () => d.getIdToken(),
|
|
6671
|
+
// `refreshTokens` é opcional (MockAuth/mocks próprios omitem).
|
|
6512
6672
|
refreshEndUserToken: (() => {
|
|
6513
|
-
let
|
|
6514
|
-
return typeof
|
|
6673
|
+
let k = d.refreshTokens;
|
|
6674
|
+
return typeof k == "function" ? () => k.call(d) : void 0;
|
|
6515
6675
|
})()
|
|
6516
|
-
})),
|
|
6517
|
-
return Object.freeze({
|
|
6676
|
+
})), f = n.mocks?.entitlements ?? (a ? new pe() : qt()), m = n.mocks?.db ?? (Q() === "react-native" ? Jn() : 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({
|
|
6518
6677
|
config: s,
|
|
6519
|
-
auth:
|
|
6520
|
-
catalog:
|
|
6521
|
-
entitlements:
|
|
6522
|
-
telemetry:
|
|
6678
|
+
auth: c,
|
|
6679
|
+
catalog: Ut(s),
|
|
6680
|
+
entitlements: f,
|
|
6681
|
+
telemetry: Wt(s),
|
|
6523
6682
|
usage: u,
|
|
6524
|
-
support:
|
|
6525
|
-
db:
|
|
6526
|
-
checkout:
|
|
6527
|
-
webhooks:
|
|
6528
|
-
notifications:
|
|
6529
|
-
ai:
|
|
6683
|
+
support: p,
|
|
6684
|
+
db: m,
|
|
6685
|
+
checkout: ot(s),
|
|
6686
|
+
webhooks: h,
|
|
6687
|
+
notifications: y,
|
|
6688
|
+
ai: I
|
|
6530
6689
|
});
|
|
6690
|
+
return n.endUserAuth && Qn(w, n.endUserAuth), w;
|
|
6531
6691
|
}
|
|
6532
|
-
var
|
|
6533
|
-
function
|
|
6692
|
+
var pt = /* @__PURE__ */ Symbol.for("@neetru/sdk:serverClient");
|
|
6693
|
+
function ei(n = {}) {
|
|
6534
6694
|
let e = globalThis;
|
|
6535
|
-
return e[
|
|
6695
|
+
return e[pt] || (e[pt] = Xn(n)), e[pt];
|
|
6536
6696
|
}
|
|
6537
6697
|
|
|
6538
6698
|
// src/index.ts
|
|
6539
|
-
|
|
6699
|
+
C();
|
|
6540
6700
|
z();
|
|
6541
|
-
|
|
6542
|
-
|
|
6701
|
+
q();
|
|
6702
|
+
|
|
6703
|
+
// src/access-token.ts
|
|
6704
|
+
C();
|
|
6705
|
+
var Zn = "https://auth.neetru.com", ni = "/api/v1/oauth/.well-known/jwks";
|
|
6706
|
+
function er(n) {
|
|
6707
|
+
let e = (Array.isArray(n) ? n : [n]).filter(
|
|
6708
|
+
(t) => typeof t == "string" && t.trim().length > 0
|
|
6709
|
+
);
|
|
6710
|
+
if (e.length === 0)
|
|
6711
|
+
throw new exports.NeetruError(
|
|
6712
|
+
"invalid_config",
|
|
6713
|
+
"verifyAccessToken exige `audience` com ao menos um client_id OIDC do produto (web/iOS/Android). Sem audience, tokens emitidos para OUTROS produtos seriam aceitos."
|
|
6714
|
+
);
|
|
6715
|
+
return e;
|
|
6716
|
+
}
|
|
6717
|
+
var Yn = "O token apresentado n\xE3o \xE9 um access_token (parece um id_token). Mande o access_token no Authorization.";
|
|
6718
|
+
function K(n, e) {
|
|
6719
|
+
return new exports.NeetruError("token_invalid", n, 401, void 0, e);
|
|
6720
|
+
}
|
|
6721
|
+
async function tr(n, e) {
|
|
6722
|
+
let t = er(e?.audience);
|
|
6723
|
+
if (typeof n != "string" || n.length === 0)
|
|
6724
|
+
throw K("access_token ausente.", "missing_token");
|
|
6725
|
+
if (n.split(".").length !== 3)
|
|
6726
|
+
throw K("O token n\xE3o \xE9 um JWT (access_token OIDC esperado).", "not_a_jwt");
|
|
6727
|
+
let r = (e.issuer ?? Zn).replace(/\/+$/, ""), o = e.jwksUrl ?? `${r}${ni}`, i = Math.min(Math.max(e.clockToleranceSec ?? 5, 0), 60), s = ye(o, e.jwksCacheTtlMs ?? ee), a;
|
|
6728
|
+
try {
|
|
6729
|
+
({ payload: a } = await jose.jwtVerify(n, s, {
|
|
6730
|
+
issuer: r,
|
|
6731
|
+
audience: t,
|
|
6732
|
+
algorithms: ["RS256"],
|
|
6733
|
+
clockTolerance: i,
|
|
6734
|
+
requiredClaims: ["sub", "exp", "iat", "jti"]
|
|
6735
|
+
}));
|
|
6736
|
+
} catch (w) {
|
|
6737
|
+
let k = w?.code ?? "", N = w?.message ?? String(w);
|
|
6738
|
+
if (k === "ERR_JWT_EXPIRED")
|
|
6739
|
+
throw new exports.NeetruError("token_expired", "access_token expirado.", 401, void 0, "expired");
|
|
6740
|
+
if (k === "ERR_JWT_CLAIM_VALIDATION_FAILED") {
|
|
6741
|
+
let _ = w?.claim;
|
|
6742
|
+
throw _ === "aud" ? new exports.NeetruError(
|
|
6743
|
+
"token_invalid_audience",
|
|
6744
|
+
"access_token emitido para um client OIDC que n\xE3o est\xE1 na lista desta API.",
|
|
6745
|
+
401,
|
|
6746
|
+
void 0,
|
|
6747
|
+
"audience"
|
|
6748
|
+
) : _ === "iss" ? new exports.NeetruError("token_invalid_issuer", "access_token de outro emissor.", 401, void 0, "issuer") : _ === "jti" ? K(Yn, "not_access_token") : K(`Claim obrigat\xF3rio ausente ou inv\xE1lido no access_token (${_ ?? "desconhecido"}).`, "claims");
|
|
6749
|
+
}
|
|
6750
|
+
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
|
+
"token_invalid_signature",
|
|
6752
|
+
"Assinatura do access_token inv\xE1lida.",
|
|
6753
|
+
401,
|
|
6754
|
+
void 0,
|
|
6755
|
+
"signature"
|
|
6756
|
+
) : 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
|
+
}
|
|
6758
|
+
let c = a.jti, u = a.scope, d = a.client_id;
|
|
6759
|
+
if (typeof c != "string" || c.length === 0 || typeof u != "string" || typeof d != "string")
|
|
6760
|
+
throw K(Yn, "not_access_token");
|
|
6761
|
+
let p = a.aud, f = typeof p == "string" ? p : Array.isArray(p) && p.length === 1 ? p[0] : void 0;
|
|
6762
|
+
if (typeof f != "string" || f !== d)
|
|
6763
|
+
throw K("access_token inconsistente: client_id difere do aud.", "client_id_mismatch");
|
|
6764
|
+
let m = typeof a.sub == "string" ? a.sub : "", h = m.startsWith("client:");
|
|
6765
|
+
if (!m || h && e.allowClientCredentials !== true)
|
|
6766
|
+
throw K(
|
|
6767
|
+
"access_token de m\xE1quina (client_credentials) n\xE3o representa um usu\xE1rio final.",
|
|
6768
|
+
"client_credentials"
|
|
6769
|
+
);
|
|
6770
|
+
let y = u.split(" ").filter(Boolean), I = (e.requiredScopes ?? []).filter((w) => !y.includes(w));
|
|
6771
|
+
if (I.length > 0)
|
|
6772
|
+
throw new exports.NeetruError(
|
|
6773
|
+
"insufficient_scope",
|
|
6774
|
+
`access_token sem os escopos exigidos: ${I.join(", ")}.`,
|
|
6775
|
+
403,
|
|
6776
|
+
void 0,
|
|
6777
|
+
I.join(" ")
|
|
6778
|
+
);
|
|
6779
|
+
return {
|
|
6780
|
+
sub: m,
|
|
6781
|
+
clientId: d,
|
|
6782
|
+
audience: f,
|
|
6783
|
+
scopes: y,
|
|
6784
|
+
jti: c,
|
|
6785
|
+
issuedAt: a.iat,
|
|
6786
|
+
expiresAt: a.exp,
|
|
6787
|
+
isClientCredentials: h,
|
|
6788
|
+
claims: a
|
|
6789
|
+
};
|
|
6790
|
+
}
|
|
6791
|
+
function nr(n, e) {
|
|
6792
|
+
let t = e.toLowerCase(), r = n && typeof n == "object" && "headers" in n && n.headers ? n.headers : n;
|
|
6793
|
+
if (!r || typeof r != "object") return null;
|
|
6794
|
+
if (typeof r.get == "function")
|
|
6795
|
+
return r.get(t);
|
|
6796
|
+
for (let [o, i] of Object.entries(r))
|
|
6797
|
+
if (o.toLowerCase() === t)
|
|
6798
|
+
return Array.isArray(i) ? typeof i[0] == "string" ? i[0] : null : typeof i == "string" ? i : null;
|
|
6799
|
+
return null;
|
|
6800
|
+
}
|
|
6801
|
+
function rr(n) {
|
|
6802
|
+
let e = nr(n, "authorization");
|
|
6803
|
+
if (!e) return null;
|
|
6804
|
+
let t = /^\s*Bearer\s+(\S+)\s*$/i.exec(e);
|
|
6805
|
+
return t ? t[1] : null;
|
|
6806
|
+
}
|
|
6807
|
+
function or(n, e) {
|
|
6808
|
+
let t = nr(n, "cookie");
|
|
6809
|
+
if (!t) return null;
|
|
6810
|
+
for (let r of t.split(";")) {
|
|
6811
|
+
let o = r.indexOf("=");
|
|
6812
|
+
if (o < 0 || r.slice(0, o).trim() !== e) continue;
|
|
6813
|
+
let i = r.slice(o + 1).trim();
|
|
6814
|
+
try {
|
|
6815
|
+
return decodeURIComponent(i);
|
|
6816
|
+
} catch {
|
|
6817
|
+
return i;
|
|
6818
|
+
}
|
|
6819
|
+
}
|
|
6820
|
+
return null;
|
|
6821
|
+
}
|
|
6822
|
+
async function ri(n, e) {
|
|
6823
|
+
er(e?.audience);
|
|
6824
|
+
let t = rr(n);
|
|
6825
|
+
if (t)
|
|
6826
|
+
return { kind: "access_token", token: await tr(t, e) };
|
|
6827
|
+
if (e.cookie) {
|
|
6828
|
+
let r = or(n, e.cookie.name);
|
|
6829
|
+
if (r) {
|
|
6830
|
+
let o = await e.cookie.verify(r);
|
|
6831
|
+
if (o != null) return { kind: "cookie", session: o };
|
|
6832
|
+
}
|
|
6833
|
+
}
|
|
6834
|
+
return null;
|
|
6835
|
+
}
|
|
6836
|
+
function oi(n) {
|
|
6837
|
+
if (n instanceof exports.NeetruError) {
|
|
6838
|
+
if (n.code === "insufficient_scope")
|
|
6839
|
+
return {
|
|
6840
|
+
status: 403,
|
|
6841
|
+
headers: {
|
|
6842
|
+
"www-authenticate": `Bearer error="insufficient_scope", scope="${n.reason ?? ""}"`,
|
|
6843
|
+
"cache-control": "no-store"
|
|
6844
|
+
},
|
|
6845
|
+
body: { error: "insufficient_scope", error_description: n.message }
|
|
6846
|
+
};
|
|
6847
|
+
if (n.code === "token_invalid" || n.code === "token_expired" || n.code === "token_invalid_signature" || n.code === "token_invalid_audience" || n.code === "token_invalid_issuer")
|
|
6848
|
+
return {
|
|
6849
|
+
status: 401,
|
|
6850
|
+
headers: { "www-authenticate": 'Bearer error="invalid_token"', "cache-control": "no-store" },
|
|
6851
|
+
body: { error: "invalid_token", error_description: n.message }
|
|
6852
|
+
};
|
|
6853
|
+
if (n.code === "network_error")
|
|
6854
|
+
return {
|
|
6855
|
+
status: 503,
|
|
6856
|
+
headers: { "cache-control": "no-store", "retry-after": "5" },
|
|
6857
|
+
body: { error: "temporarily_unavailable", error_description: "Valida\xE7\xE3o de token indispon\xEDvel." }
|
|
6858
|
+
};
|
|
6859
|
+
}
|
|
6860
|
+
return {
|
|
6861
|
+
status: 500,
|
|
6862
|
+
headers: { "cache-control": "no-store" },
|
|
6863
|
+
body: { error: "server_error", error_description: "Erro interno ao validar o token." }
|
|
6864
|
+
};
|
|
6865
|
+
}
|
|
6866
|
+
|
|
6867
|
+
// src/index.ts
|
|
6868
|
+
be();
|
|
6869
|
+
var Aa = "3.8.0";
|
|
6543
6870
|
|
|
6544
|
-
exports.DEFAULT_BASE_URL =
|
|
6545
|
-
exports.DEV_FIXTURE_USER =
|
|
6546
|
-
exports.MockAi =
|
|
6547
|
-
exports.MockAuth =
|
|
6548
|
-
exports.MockCheckout =
|
|
6549
|
-
exports.MockDb =
|
|
6550
|
-
exports.MockEntitlements =
|
|
6551
|
-
exports.MockNotifications =
|
|
6552
|
-
exports.MockSupport =
|
|
6553
|
-
exports.MockUsage =
|
|
6554
|
-
exports.MockWebhooks =
|
|
6555
|
-
exports.
|
|
6556
|
-
exports.
|
|
6557
|
-
exports.
|
|
6558
|
-
exports.
|
|
6559
|
-
exports.
|
|
6560
|
-
exports.
|
|
6561
|
-
exports.
|
|
6562
|
-
exports.
|
|
6563
|
-
exports.
|
|
6564
|
-
exports.
|
|
6565
|
-
exports.
|
|
6566
|
-
exports.
|
|
6567
|
-
exports.
|
|
6568
|
-
exports.
|
|
6569
|
-
exports.
|
|
6570
|
-
exports.
|
|
6571
|
-
exports.
|
|
6871
|
+
exports.DEFAULT_BASE_URL = je;
|
|
6872
|
+
exports.DEV_FIXTURE_USER = qe;
|
|
6873
|
+
exports.MockAi = fe;
|
|
6874
|
+
exports.MockAuth = le;
|
|
6875
|
+
exports.MockCheckout = xe;
|
|
6876
|
+
exports.MockDb = lt;
|
|
6877
|
+
exports.MockEntitlements = pe;
|
|
6878
|
+
exports.MockNotifications = ce;
|
|
6879
|
+
exports.MockSupport = ue;
|
|
6880
|
+
exports.MockUsage = de;
|
|
6881
|
+
exports.MockWebhooks = ae;
|
|
6882
|
+
exports.NEETRU_OIDC_ISSUER = Zn;
|
|
6883
|
+
exports.NEETRU_SENTINEL_KEY = ze;
|
|
6884
|
+
exports.VERSION = Aa;
|
|
6885
|
+
exports.accessTokenErrorToHttp = oi;
|
|
6886
|
+
exports.authenticateProductRequest = ri;
|
|
6887
|
+
exports.checkCoreVersionCompat = te;
|
|
6888
|
+
exports.createAiNamespace = ct;
|
|
6889
|
+
exports.createCheckoutNamespace = ot;
|
|
6890
|
+
exports.createNeetruClient = Xn;
|
|
6891
|
+
exports.createNeetruDb = nt;
|
|
6892
|
+
exports.createNotificationsNamespace = at;
|
|
6893
|
+
exports.createWebhooksNamespace = it;
|
|
6894
|
+
exports.detectNonServerRuntime = Q;
|
|
6895
|
+
exports.getServerNeetruClient = ei;
|
|
6896
|
+
exports.increment = Kr;
|
|
6897
|
+
exports.isFieldSentinel = H;
|
|
6898
|
+
exports.isIncrementSentinel = Yt;
|
|
6899
|
+
exports.isServerTimestampSentinel = Xt;
|
|
6900
|
+
exports.readBearerToken = rr;
|
|
6901
|
+
exports.readCookie = or;
|
|
6902
|
+
exports.serverTimestamp = Br;
|
|
6903
|
+
exports.verifyAccessToken = tr;
|
|
6904
|
+
exports.verifyWebhookRequest = Co;
|
|
6905
|
+
exports.verifyWebhookSignature = _n;
|