Skip to content

Commit ff593f8

Browse files
authored
fix(webhooks): make the SDK browser-bundle-safe (namespace import of node:crypto) (#4)
webhooks.ts did a NAMED import of node:crypto (createHmac, timingSafeEqual). Because the package index re-exports verifyWebhook, any bundler building an app that imports the SDK (Vite/webpack/rollup) externalizes node:crypto to an empty module and then ERRORS on the missing named exports - so the SDK could not be bundled for the browser at all (found while building a browser demo). Switch to a namespace import (import * as nodeCrypto) and reference nodeCrypto.createHmac / nodeCrypto.timingSafeEqual. Browser bundles now build (a single benign 'externalized for browser' warning instead of a hard error); verifyWebhook stays synchronous and works unchanged in Node, where it actually runs. No API change. 17 tests still green. Bump 0.1.2 -> 0.1.3.
1 parent 5bcad60 commit ff593f8

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mik3fly-lab/rektradar-sdk",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "Official SDK for the RektRadar API - Ethereum scam & rug-pull detection (token risk scores, flags, live rug feed, webhooks).",
55
"type": "module",
66
"main": "./dist/index.js",

src/webhooks.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { createHmac, timingSafeEqual } from "node:crypto";
1+
// Namespace import (not named) so browser bundlers don't choke. verifyWebhook
2+
// is server-only - a browser never receives webhooks - but the SDK's index
3+
// re-exports it, and a *named* `node:crypto` import makes Vite/webpack/rollup
4+
// fail to build ANY app that imports the SDK (they externalize node:crypto to an
5+
// empty module, then error on the missing named exports). A namespace import
6+
// only warns; the functions stay reachable in Node where verifyWebhook runs.
7+
import * as nodeCrypto from "node:crypto";
28

39
/**
410
* Verify a RektRadar webhook signature.
@@ -21,11 +27,11 @@ export function verifyWebhook(rawBody: string, signature: string, secret: string
2127
if (!signature || !secret) {
2228
return false;
2329
}
24-
const expected = `sha256=${createHmac("sha256", secret).update(rawBody, "utf8").digest("hex")}`;
30+
const expected = `sha256=${nodeCrypto.createHmac("sha256", secret).update(rawBody, "utf8").digest("hex")}`;
2531
const provided = Buffer.from(signature);
2632
const computed = Buffer.from(expected);
2733
if (provided.length !== computed.length) {
2834
return false;
2935
}
30-
return timingSafeEqual(provided, computed);
36+
return nodeCrypto.timingSafeEqual(provided, computed);
3137
}

0 commit comments

Comments
 (0)