@meterapp/car-image-sdk 1.30.0 → 1.32.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/README.md +1 -1
- package/dist/client.d.ts +23 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +51 -0
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/mcp/index.d.ts +30 -2
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +33 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/webhooks.d.ts +147 -0
- package/dist/webhooks.d.ts.map +1 -0
- package/dist/webhooks.js +91 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +1 -1
package/dist/webhooks.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account webhooks (https://carimage.dev/docs/webhooks?ref=sdk): the event types, the
|
|
3
|
+
* shapes the API answers with, and `verifyWebhook()`, which checks a
|
|
4
|
+
* delivery's `X-CarImage-Signature` before you trust its body.
|
|
5
|
+
*/
|
|
6
|
+
/** Every account webhook event type. */
|
|
7
|
+
export const WEBHOOK_EVENT_TYPES = [
|
|
8
|
+
"webhook.test",
|
|
9
|
+
"image.ready",
|
|
10
|
+
"3d_model.ready",
|
|
11
|
+
"3d_model.failed",
|
|
12
|
+
"image_upgrade.succeeded",
|
|
13
|
+
"image_upgrade.failed",
|
|
14
|
+
"image.upgraded",
|
|
15
|
+
"image.issue_reported",
|
|
16
|
+
"image.issue_resolved",
|
|
17
|
+
"image.replaced",
|
|
18
|
+
"vehicle.available",
|
|
19
|
+
];
|
|
20
|
+
export const WEBHOOK_SIGNATURE_HEADER = "X-CarImage-Signature";
|
|
21
|
+
export const WEBHOOK_EVENT_HEADER = "X-CarImage-Event";
|
|
22
|
+
export const WEBHOOK_DELIVERY_HEADER = "X-CarImage-Delivery";
|
|
23
|
+
export class WebhookVerificationError extends Error {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "WebhookVerificationError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const encoder = new TextEncoder();
|
|
30
|
+
function toHex(bytes) {
|
|
31
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
32
|
+
}
|
|
33
|
+
function constantTimeEqual(a, b) {
|
|
34
|
+
if (a.length !== b.length)
|
|
35
|
+
return false;
|
|
36
|
+
let diff = 0;
|
|
37
|
+
for (let i = 0; i < a.length; i++)
|
|
38
|
+
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
39
|
+
return diff === 0;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Verifies an account webhook delivery and returns its event. Pass the raw
|
|
43
|
+
* request body exactly as received (not re-serialized JSON), the
|
|
44
|
+
* `X-CarImage-Signature` header and the endpoint's `whsec_…` secret. Throws
|
|
45
|
+
* `WebhookVerificationError` when the header is missing or malformed, the
|
|
46
|
+
* timestamp is outside the tolerance (a replay), or no signature matches.
|
|
47
|
+
*
|
|
48
|
+
* const event = await verifyWebhook(await request.text(), request.headers.get("X-CarImage-Signature"), webhookSecret); // the endpoint's whsec_ secret
|
|
49
|
+
*
|
|
50
|
+
* Uses Web Crypto: Node 20+, Bun, Deno and edge runtimes.
|
|
51
|
+
*/
|
|
52
|
+
export async function verifyWebhook(body, signatureHeader, secret, options = {}) {
|
|
53
|
+
if (!signatureHeader)
|
|
54
|
+
throw new WebhookVerificationError(`Missing ${WEBHOOK_SIGNATURE_HEADER} header`);
|
|
55
|
+
if (!secret)
|
|
56
|
+
throw new WebhookVerificationError("Missing webhook secret");
|
|
57
|
+
let timestamp = null;
|
|
58
|
+
const signatures = [];
|
|
59
|
+
for (const part of signatureHeader.split(",")) {
|
|
60
|
+
const [name, value = ""] = part.trim().split("=", 2);
|
|
61
|
+
if (name === "t" && /^\d+$/.test(value))
|
|
62
|
+
timestamp = Number(value);
|
|
63
|
+
else if (name === "sha256" && value)
|
|
64
|
+
signatures.push(value.toLowerCase());
|
|
65
|
+
}
|
|
66
|
+
if (timestamp === null || signatures.length === 0)
|
|
67
|
+
throw new WebhookVerificationError(`Malformed ${WEBHOOK_SIGNATURE_HEADER} header`);
|
|
68
|
+
const now = options.now ?? Date.now();
|
|
69
|
+
const tolerance = options.toleranceSeconds ?? 300;
|
|
70
|
+
if (Math.abs(now / 1000 - timestamp) > tolerance)
|
|
71
|
+
throw new WebhookVerificationError("Webhook timestamp is outside the tolerance");
|
|
72
|
+
const subtle = globalThis.crypto?.subtle;
|
|
73
|
+
if (!subtle)
|
|
74
|
+
throw new TypeError("verifyWebhook needs Web Crypto (globalThis.crypto.subtle): Node 20+, Bun, Deno or an edge runtime");
|
|
75
|
+
const key = await subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
76
|
+
const expected = toHex(new Uint8Array(await subtle.sign("HMAC", key, encoder.encode(`${timestamp}.${body}`))));
|
|
77
|
+
if (!signatures.some((signature) => constantTimeEqual(signature, expected)))
|
|
78
|
+
throw new WebhookVerificationError("Webhook signature does not match");
|
|
79
|
+
let event;
|
|
80
|
+
try {
|
|
81
|
+
event = JSON.parse(body);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
throw new WebhookVerificationError("Webhook body is not JSON");
|
|
85
|
+
}
|
|
86
|
+
if (!event || typeof event !== "object" || typeof event.id !== "string" || typeof event.type !== "string") {
|
|
87
|
+
throw new WebhookVerificationError("Webhook body is not an event");
|
|
88
|
+
}
|
|
89
|
+
return event;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=webhooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../src/webhooks.ts"],"names":[],"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|
|
2
2
|
"name": "@meterapp/car-image-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.32.0",
|
|
4
4
|
"description": "TypeScript SDK for the Car Image API by Meter: studio-quality, transparent-background renders of any vehicle in the open @meterapp/vehicle-db catalog in any paint color, catalog make logos, VIN decoding, stable vehicle ids and textured 3D models. Zero runtime dependencies; works in Node 20+, edge runtimes and browsers.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Meter <support@carimage.dev> (https://meterapp.co)",
|