@gemina/sdk 0.17.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -12525,9 +12525,10 @@ var GeminaTimeoutError = class extends GeminaError {
12525
12525
  }
12526
12526
  };
12527
12527
  var GeminaWebhookVerificationError = class extends GeminaError {
12528
- constructor(reason, message) {
12528
+ constructor(reason, message, cause) {
12529
12529
  super(message);
12530
12530
  this.reason = reason;
12531
+ if (cause !== void 0) this.cause = cause;
12531
12532
  Object.setPrototypeOf(this, new.target.prototype);
12532
12533
  }
12533
12534
  };
@@ -12938,18 +12939,21 @@ function isExtractionCompleted(event) {
12938
12939
  function isWebhookTest(event) {
12939
12940
  return event.type === "webhook.test";
12940
12941
  }
12941
- var REQUIRED = {
12942
- "extraction.completed": { correlationId: "string", status: "string", resultIncluded: "boolean", resultPath: "string" },
12943
- "webhook.test": { message: "string" }
12942
+ var KNOWN = {
12943
+ "extraction.completed": {
12944
+ required: { correlationId: "string", status: "string", resultIncluded: "boolean", resultPath: "string" },
12945
+ read: ExtractionCompletedEventDataFromJSON
12946
+ },
12947
+ "webhook.test": { required: { message: "string" }, read: WebhookTestEventDataFromJSON }
12944
12948
  };
12945
- var fail = (reason, message) => new GeminaWebhookVerificationError(reason, message);
12949
+ var fail = (reason, message, cause) => new GeminaWebhookVerificationError(reason, message, cause);
12946
12950
  function isObject(value) {
12947
12951
  return value !== null && typeof value === "object" && !Array.isArray(value);
12948
12952
  }
12949
12953
  var asciiLower = (text) => text.replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32));
12950
12954
  function isComplete(type, data) {
12951
12955
  if (!isObject(data)) return false;
12952
- if (Object.entries(REQUIRED[type]).some(([field, kind]) => typeof data[field] !== kind)) return false;
12956
+ if (Object.entries(KNOWN[type].required).some(([field, kind]) => typeof data[field] !== kind)) return false;
12953
12957
  if (type !== "extraction.completed") return true;
12954
12958
  const result = data.result ?? null;
12955
12959
  const error = data.error ?? null;
@@ -12966,8 +12970,8 @@ function header(headers, name) {
12966
12970
  };
12967
12971
  const fromEntries = (entries) => {
12968
12972
  for (const [key, value] of entries) {
12969
- if (asciiLower(String(key)) !== name || value === void 0 || value === null) continue;
12970
- for (const item of Array.isArray(value) ? value : [value]) add(String(item));
12973
+ if (typeof key !== "string" || asciiLower(key) !== name) continue;
12974
+ for (const item of Array.isArray(value) ? value : [value]) if (typeof item === "string") add(item);
12971
12975
  }
12972
12976
  };
12973
12977
  if (headers === null || headers === void 0) {
@@ -12975,7 +12979,7 @@ function header(headers, name) {
12975
12979
  fromEntries(headers.entries());
12976
12980
  } else if (typeof headers.get === "function") {
12977
12981
  const value = headers.get(name);
12978
- if (value != null) add(String(value));
12982
+ if (typeof value === "string") add(value);
12979
12983
  } else {
12980
12984
  fromEntries(Object.entries(headers));
12981
12985
  }
@@ -12996,13 +13000,24 @@ function constantTimeEqual(a, b) {
12996
13000
  for (let i = 0; i < a.length; i++) difference |= a[i] ^ b[i];
12997
13001
  return difference === 0;
12998
13002
  }
13003
+ function isRawBuffer(value) {
13004
+ const tag = Object.prototype.toString.call(value);
13005
+ return tag === "[object ArrayBuffer]" || tag === "[object SharedArrayBuffer]";
13006
+ }
12999
13007
  function copyBody(body) {
13000
13008
  if (typeof body === "string") return new TextEncoder().encode(body);
13001
13009
  if (body == null) return new Uint8Array(0);
13002
- const source = ArrayBuffer.isView(body) ? new Uint8Array(body.buffer, body.byteOffset, body.byteLength) : new Uint8Array(body);
13003
- const copy = new Uint8Array(source.length);
13004
- copy.set(source);
13005
- return copy;
13010
+ if (!isRawBuffer(body) && !ArrayBuffer.isView(body)) {
13011
+ throw fail("invalid_body", "The webhook body must be the raw request bytes or text, not a parsed value.");
13012
+ }
13013
+ try {
13014
+ const source = ArrayBuffer.isView(body) ? new Uint8Array(body.buffer, body.byteOffset, body.byteLength) : new Uint8Array(body);
13015
+ const copy = new Uint8Array(source.length);
13016
+ copy.set(source);
13017
+ return copy;
13018
+ } catch (error) {
13019
+ throw fail("invalid_body", "The webhook body cannot be read: its buffer was detached.", error);
13020
+ }
13006
13021
  }
13007
13022
  async function subtleCrypto() {
13008
13023
  const global = globalThis.crypto;
@@ -13015,35 +13030,7 @@ async function subtleCrypto() {
13015
13030
  );
13016
13031
  return module2.webcrypto.subtle;
13017
13032
  }
13018
- async function unwrapWebhook(body, headers, secret, options = {}) {
13019
- let rawBody;
13020
- try {
13021
- rawBody = copyBody(body);
13022
- } catch (error) {
13023
- if (!(error instanceof TypeError)) throw error;
13024
- throw fail("invalid_body", "The webhook body cannot be read: its buffer was detached.");
13025
- }
13026
- options = options ?? {};
13027
- const id = header(headers, "webhook-id");
13028
- const timestampText = header(headers, "webhook-timestamp");
13029
- const signatureHeader = header(headers, "webhook-signature");
13030
- if (!TIMESTAMP.test(timestampText)) {
13031
- throw fail("invalid_timestamp", "The webhook-timestamp header is not a whole number of seconds.");
13032
- }
13033
- const tolerance = options.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS;
13034
- const clock = (options.now ?? (() => Date.now() / 1e3))();
13035
- if (!Number.isFinite(clock) || !Number.isFinite(tolerance) || tolerance < 0) {
13036
- throw fail(
13037
- "timestamp_out_of_tolerance",
13038
- "The clock or the tolerance is not a finite number, so the timestamp cannot be checked."
13039
- );
13040
- }
13041
- if (Math.abs(Math.floor(clock) - Number(timestampText)) > tolerance) {
13042
- throw fail(
13043
- "timestamp_out_of_tolerance",
13044
- `The webhook timestamp is more than ${tolerance} seconds from the current time.`
13045
- );
13046
- }
13033
+ async function verifySignature(rawBody, id, timestampText, signatureHeader, secret) {
13047
13034
  const key = typeof secret === "string" ? decodeBase64(secret.startsWith(SECRET_PREFIX) ? secret.slice(SECRET_PREFIX.length) : secret) : void 0;
13048
13035
  if (key === void 0 || key.length === 0) {
13049
13036
  throw fail("invalid_secret", "The signing secret is not valid: expected whsec_ followed by base64.");
@@ -13053,7 +13040,6 @@ async function unwrapWebhook(body, headers, secret, options = {}) {
13053
13040
  signed.set(prefix, 0);
13054
13041
  signed.set(rawBody, prefix.length);
13055
13042
  const subtle = await subtleCrypto();
13056
- const verifiedBody = signed.subarray(prefix.length);
13057
13043
  const cryptoKey = await subtle.importKey("raw", key, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
13058
13044
  const expected = new Uint8Array(await subtle.sign("HMAC", cryptoKey, signed));
13059
13045
  let matched = false;
@@ -13064,34 +13050,64 @@ async function unwrapWebhook(body, headers, secret, options = {}) {
13064
13050
  if (candidate !== void 0 && constantTimeEqual(candidate, expected)) matched = true;
13065
13051
  }
13066
13052
  if (!matched) throw fail("no_matching_signature", "No signature in the webhook-signature header matches.");
13053
+ return signed.subarray(prefix.length);
13054
+ }
13055
+ function parseBody(verifiedBody) {
13067
13056
  if (verifiedBody.length >= 3 && verifiedBody[0] === 239 && verifiedBody[1] === 187 && verifiedBody[2] === 191) {
13068
13057
  throw fail("invalid_body", "The webhook body starts with a byte order mark.");
13069
13058
  }
13070
13059
  let parsed;
13071
13060
  try {
13072
13061
  parsed = JSON.parse(new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(verifiedBody));
13073
- } catch {
13074
- throw fail("invalid_body", "The webhook body is not valid JSON.");
13062
+ } catch (error) {
13063
+ throw fail("invalid_body", "The webhook body is not valid JSON.", error);
13075
13064
  }
13076
13065
  if (!isObject(parsed)) throw fail("invalid_body", "The webhook body is not a JSON object.");
13066
+ return parsed;
13067
+ }
13068
+ function typeData(type, data) {
13069
+ if (type === null || !Object.prototype.hasOwnProperty.call(KNOWN, type)) return data;
13070
+ if (!isComplete(type, data)) {
13071
+ throw fail("invalid_body", `The data of this ${type} event is missing a required field.`);
13072
+ }
13073
+ try {
13074
+ return KNOWN[type].read(data);
13075
+ } catch (error) {
13076
+ throw fail("invalid_body", `The data of this ${type} event could not be read.`, error);
13077
+ }
13078
+ }
13079
+ async function unwrapWebhook(body, headers, secret, options = {}) {
13080
+ const rawBody = copyBody(body);
13081
+ options = options ?? {};
13082
+ const id = header(headers, "webhook-id");
13083
+ const timestampText = header(headers, "webhook-timestamp");
13084
+ const signatureHeader = header(headers, "webhook-signature");
13085
+ if (!TIMESTAMP.test(timestampText)) {
13086
+ throw fail("invalid_timestamp", "The webhook-timestamp header is not a whole number of seconds.");
13087
+ }
13088
+ const tolerance = options.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS;
13089
+ const clock = (options.now ?? (() => Date.now() / 1e3))();
13090
+ if (!Number.isFinite(clock) || !Number.isFinite(tolerance) || tolerance < 0) {
13091
+ throw fail(
13092
+ "timestamp_out_of_tolerance",
13093
+ "The clock or the tolerance is not a finite number, so the timestamp cannot be checked."
13094
+ );
13095
+ }
13096
+ if (Math.abs(Math.floor(clock) - Number(timestampText)) > tolerance) {
13097
+ throw fail(
13098
+ "timestamp_out_of_tolerance",
13099
+ `The webhook timestamp is more than ${tolerance} seconds from the current time.`
13100
+ );
13101
+ }
13102
+ const verifiedBody = await verifySignature(rawBody, id, timestampText, signatureHeader, secret);
13103
+ const parsed = parseBody(verifiedBody);
13077
13104
  const type = typeof parsed.type === "string" ? parsed.type : null;
13078
13105
  const timestamp = typeof parsed.timestamp === "string" ? parsed.timestamp : null;
13079
- let data = parsed.data;
13080
- if (type !== null && Object.prototype.hasOwnProperty.call(REQUIRED, type)) {
13081
- if (!isComplete(type, data)) {
13082
- throw fail("invalid_body", `The data of this ${type} event is missing a required field.`);
13083
- }
13084
- try {
13085
- data = type === "extraction.completed" ? ExtractionCompletedEventDataFromJSON(data) : WebhookTestEventDataFromJSON(data);
13086
- } catch {
13087
- throw fail("invalid_body", `The data of this ${type} event could not be read.`);
13088
- }
13089
- }
13090
- return { id, type, timestamp, data, raw: parsed };
13106
+ return { id, type, timestamp, data: typeData(type, parsed.data ?? null), raw: parsed };
13091
13107
  }
13092
13108
 
13093
13109
  // src/version.ts
13094
- var VERSION = "0.17.0";
13110
+ var VERSION = "0.17.1";
13095
13111
  // Annotate the CommonJS export names for ESM import in node:
13096
13112
  0 && (module.exports = {
13097
13113
  AddExtractionsInDTOFromJSON,