pendra 0.15.1 → 0.16.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/dist/index.mjs CHANGED
@@ -13,21 +13,23 @@ var APIConnectionError = class extends APIError {
13
13
  }
14
14
  };
15
15
  var APIStatusError = class extends APIError {
16
- constructor(message, status) {
16
+ constructor(message, status, options = {}) {
17
17
  super(message, status);
18
18
  this.name = "APIStatusError";
19
19
  this.status = status;
20
+ this.body = options.body;
21
+ this.detail = options.detail;
20
22
  }
21
23
  };
22
24
  var AuthenticationError = class extends APIStatusError {
23
- constructor(message = "Invalid API key \u2014 check your PENDRA_API_KEY") {
24
- super(message, 401);
25
+ constructor(message = "Invalid API key \u2014 check your PENDRA_API_KEY", options = {}) {
26
+ super(message, 401, options);
25
27
  this.name = "AuthenticationError";
26
28
  }
27
29
  };
28
30
  var RateLimitError = class extends APIStatusError {
29
- constructor(message = "Rate limit exceeded") {
30
- super(message, 429);
31
+ constructor(message = "Rate limit exceeded", options = {}) {
32
+ super(message, 429, options);
31
33
  this.name = "RateLimitError";
32
34
  }
33
35
  };
@@ -58,6 +60,80 @@ var APIStreamError = class extends APIError {
58
60
  this.workerName = options.workerName;
59
61
  }
60
62
  };
63
+ var LOC_SOURCES = /* @__PURE__ */ new Set(["body", "query", "path", "header", "cookie"]);
64
+ function isRecord(value) {
65
+ return typeof value === "object" && value !== null && !Array.isArray(value);
66
+ }
67
+ function nonEmptyString(value) {
68
+ return typeof value === "string" && value ? value : void 0;
69
+ }
70
+ function compactJson(value) {
71
+ try {
72
+ return JSON.stringify(value) ?? String(value);
73
+ } catch {
74
+ return String(value);
75
+ }
76
+ }
77
+ function messageFromItem(item) {
78
+ if (isRecord(item)) {
79
+ const msg = nonEmptyString(item.msg) ?? nonEmptyString(item.message);
80
+ if (msg) {
81
+ let field = typeof item.field === "string" ? item.field : void 0;
82
+ if (field === void 0) {
83
+ const loc = Array.isArray(item.loc) ? [...item.loc] : [];
84
+ if (loc.length && LOC_SOURCES.has(String(loc[0]))) loc.shift();
85
+ field = loc.map(String).join(".");
86
+ }
87
+ return field ? `${field}: ${msg}` : msg;
88
+ }
89
+ }
90
+ return messageFromDetail(item);
91
+ }
92
+ function messageFromDetail(detail) {
93
+ if (detail === void 0 || detail === null) return void 0;
94
+ if (typeof detail === "string") return detail || void 0;
95
+ if (Array.isArray(detail)) {
96
+ const parts = detail.map(messageFromItem).filter((p) => !!p);
97
+ return parts.length ? parts.join("; ") : void 0;
98
+ }
99
+ if (isRecord(detail)) {
100
+ const err = detail.error;
101
+ const nested = isRecord(err) ? nonEmptyString(err.message) : void 0;
102
+ const msg = nested ?? nonEmptyString(detail.message) ?? nonEmptyString(detail.msg);
103
+ if (msg) return msg;
104
+ return Object.keys(detail).length ? compactJson(detail) : void 0;
105
+ }
106
+ return compactJson(detail);
107
+ }
108
+ async function readErrorBody(response) {
109
+ let raw;
110
+ try {
111
+ raw = await response.text();
112
+ } catch {
113
+ return void 0;
114
+ }
115
+ if (!raw) return void 0;
116
+ try {
117
+ return JSON.parse(raw);
118
+ } catch {
119
+ return raw;
120
+ }
121
+ }
122
+ function statusErrorFromBody(status, body, fallback) {
123
+ const detail = isRecord(body) ? body.detail : void 0;
124
+ const err = isRecord(body) ? body.error : void 0;
125
+ let message = (isRecord(err) ? nonEmptyString(err.message) : void 0) ?? messageFromDetail(detail) ?? (isRecord(body) ? nonEmptyString(body.message) : void 0);
126
+ if (message === void 0) {
127
+ if (typeof body === "string") message = body || fallback;
128
+ else if (Array.isArray(body) ? body.length : isRecord(body) && Object.keys(body).length) {
129
+ message = compactJson(body);
130
+ } else message = fallback;
131
+ }
132
+ const options = { body, detail };
133
+ if (status === 401) return new AuthenticationError(message, options);
134
+ if (status === 429) return new RateLimitError(message, options);
135
+ return new APIStatusError(message, status, options);
136
+ }
61
137
 
62
138
  // src/_privateInference.ts
63
139
  import { chacha20poly1305 as chacha20poly13052 } from "@noble/ciphers/chacha.js";
@@ -574,21 +650,6 @@ function asBlob(file) {
574
650
  }
575
651
  return { blob: new Blob([content]), filename };
576
652
  }
577
- async function extractErrorDetail(response) {
578
- let raw = "";
579
- try {
580
- raw = await response.text();
581
- } catch {
582
- return response.statusText;
583
- }
584
- if (!raw) return response.statusText;
585
- try {
586
- const body = JSON.parse(raw);
587
- return body.detail ?? body.error?.message ?? raw;
588
- } catch {
589
- return raw;
590
- }
591
- }
592
653
  var Transcriptions = class {
593
654
  constructor(client) {
594
655
  this.client = client;
@@ -646,14 +707,8 @@ var Transcriptions = class {
646
707
  }
647
708
  clearTimeout(timeoutId);
648
709
  if (response.status >= 400) {
649
- const detail = await extractErrorDetail(response);
650
- if (response.status === 401) {
651
- throw new AuthenticationError(detail);
652
- }
653
- if (response.status === 429) {
654
- throw new RateLimitError(detail);
655
- }
656
- throw new APIStatusError(detail, response.status);
710
+ const body = await readErrorBody(response);
711
+ throw statusErrorFromBody(response.status, body, response.statusText);
657
712
  }
658
713
  const format = params.response_format;
659
714
  if (format === "text" || format === "srt" || format === "vtt") {
@@ -1285,7 +1340,7 @@ var Rerank = class {
1285
1340
  };
1286
1341
 
1287
1342
  // src/version.ts
1288
- var VERSION = "0.15.1";
1343
+ var VERSION = "0.16.0";
1289
1344
  var USER_AGENT = `pendra-node/${VERSION}`;
1290
1345
 
1291
1346
  // src/_privateInferenceEngine.ts
@@ -1295,6 +1350,25 @@ var PrivateInferenceError = class extends Error {
1295
1350
  this.name = "PrivateInferenceError";
1296
1351
  }
1297
1352
  };
1353
+ var FINGERPRINT_RE = /^[0-9a-f]{64}$/;
1354
+ function parsePinnedFingerprints(pin) {
1355
+ if (typeof pin !== "string") {
1356
+ throw new PrivateInferenceError(`pinnedFingerprint must be a string, got ${typeof pin}.`);
1357
+ }
1358
+ const fps = pin.split(",").map((f) => f.trim().toLowerCase()).filter((f) => f.length > 0);
1359
+ if (fps.length === 0) {
1360
+ throw new PrivateInferenceError(
1361
+ "pinnedFingerprint was set but contains no fingerprint. Pass the 64-character fingerprint shown in the console, or leave it unset to use automatic trust."
1362
+ );
1363
+ }
1364
+ const bad = fps.filter((f) => !FINGERPRINT_RE.test(f)).sort();
1365
+ if (bad.length > 0) {
1366
+ throw new PrivateInferenceError(
1367
+ `pinnedFingerprint "${bad[0]}" is not a valid fingerprint: expected 64 hexadecimal characters, as shown in the console.`
1368
+ );
1369
+ }
1370
+ return new Set(fps);
1371
+ }
1298
1372
  function defaultTofuPath() {
1299
1373
  const env = typeof process !== "undefined" ? process.env : void 0;
1300
1374
  if (env?.PENDRA_TOFU_PATH) return env.PENDRA_TOFU_PATH;
@@ -1313,8 +1387,8 @@ var PrivateInferenceEngine = class {
1313
1387
  this.orgPubs = null;
1314
1388
  this.baseUrl = opts.baseUrl.replace(/\/$/, "");
1315
1389
  this.apiKey = opts.apiKey;
1316
- const fps = (opts.pinnedFingerprint || "").split(",").map((f) => f.trim().toLowerCase()).filter((f) => f.length > 0);
1317
- this.pinnedFingerprints = fps.length > 0 ? new Set(fps) : null;
1390
+ const pin = opts.pinnedFingerprint;
1391
+ this.pinnedFingerprints = pin === null || pin === void 0 ? null : parsePinnedFingerprints(pin);
1318
1392
  this.affinitySalt = opts.affinitySalt ?? null;
1319
1393
  this.tofuPath = opts.tofuPath || defaultTofuPath();
1320
1394
  }
@@ -1510,12 +1584,19 @@ var Pendra = class {
1510
1584
  }
1511
1585
  let pinnedFingerprint;
1512
1586
  if (typeof piOpt === "object" && piOpt !== null) {
1587
+ for (const key of Object.keys(piOpt)) {
1588
+ if (key !== "pinnedFingerprint") {
1589
+ throw new TypeError(
1590
+ `Unknown privateInference option "${key}". Accepted options: pinnedFingerprint.`
1591
+ );
1592
+ }
1593
+ }
1513
1594
  this.privateInferenceEnabled = true;
1514
1595
  pinnedFingerprint = piOpt.pinnedFingerprint;
1515
1596
  } else {
1516
1597
  this.privateInferenceEnabled = Boolean(piOpt);
1517
1598
  }
1518
- pinnedFingerprint = pinnedFingerprint ?? env?.PENDRA_PINNED_ORG_FINGERPRINT ?? void 0;
1599
+ pinnedFingerprint = pinnedFingerprint ?? (env?.PENDRA_PINNED_ORG_FINGERPRINT || void 0);
1519
1600
  if (this.privateInferenceEnabled) {
1520
1601
  const affinitySalt = env?.PENDRA_AFFINITY_SALT ? new TextEncoder().encode(env.PENDRA_AFFINITY_SALT) : null;
1521
1602
  this._piEngine = new PrivateInferenceEngine({
@@ -1583,19 +1664,7 @@ var Pendra = class {
1583
1664
  );
1584
1665
  }
1585
1666
  if (!response.ok) {
1586
- let message;
1587
- try {
1588
- const body = await response.json();
1589
- if (typeof body.detail === "string") message = body.detail;
1590
- else if (typeof body.error?.message === "string") message = body.error.message;
1591
- else if (body.detail != null) message = JSON.stringify(body.detail);
1592
- else message = response.statusText;
1593
- } catch {
1594
- message = response.statusText;
1595
- }
1596
- if (response.status === 401) throw new AuthenticationError(message);
1597
- if (response.status === 429) throw new RateLimitError(message);
1598
- throw new APIStatusError(message, response.status);
1667
+ throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText);
1599
1668
  }
1600
1669
  const json = await response.json();
1601
1670
  await engine.resolveOrgKey(json.keys ?? []);
@@ -1633,19 +1702,7 @@ var Pendra = class {
1633
1702
  clearTimeout(timeoutId);
1634
1703
  if (!response.ok) {
1635
1704
  this.invalidateKeyCacheIfRekeyed(response);
1636
- let message;
1637
- try {
1638
- const body = await response.json();
1639
- if (typeof body.detail === "string") message = body.detail;
1640
- else if (typeof body.error?.message === "string") message = body.error.message;
1641
- else if (body.detail != null) message = JSON.stringify(body.detail);
1642
- else message = response.statusText;
1643
- } catch {
1644
- message = response.statusText;
1645
- }
1646
- if (response.status === 401) throw new AuthenticationError(message);
1647
- if (response.status === 429) throw new RateLimitError(message);
1648
- throw new APIStatusError(message, response.status);
1705
+ throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText);
1649
1706
  }
1650
1707
  return { response, controller };
1651
1708
  }