Skip to content

Commit

Permalink
Format & lint
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlovdog committed Nov 28, 2024
1 parent 3624e7d commit ebcdfb2
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 208 deletions.
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"path": "./packages/webhook/_cjs/index.js",
"limit": "250 kB"
}
]
]
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "biomejs.biome"
"editor.formatOnSave": true,
"editor.defaultFormatter": "biomejs.biome"
}
83 changes: 43 additions & 40 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": [
"node_modules",
"**/node_modules",
"cache",
"coverage",
"tsconfig.json",
"tsconfig.*.json",
"_cjs",
"_esm",
"_types",
"bun.lockb"
]
},
"formatter": {
"enabled": true,
"indentStyle": "tab"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "double"
}
}
}
"$schema": "https://biomejs.dev/schemas/1.0.0/schema.json",
"files": {
"ignore": [
"node_modules",
"**/node_modules",
"cache",
"coverage",
"tsconfig.json",
"tsconfig.*.json",
"_cjs",
"_esm",
"_types"
]
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "warn"
},
"style": {
"noUnusedTemplateLiteral": "warn"
}
}
},
"formatter": {
"enabled": true,
"formatWithErrors": true,
"lineWidth": 80,
"indentWidth": 4,
"indentStyle": "space"
},
"javascript": {
"formatter": {
"semicolons": "asNeeded",
"trailingCommas": "none"
}
}
}
19 changes: 11 additions & 8 deletions examples/webhook/api/approve.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { VercelRequest, VercelResponse } from '@vercel/node'
import { pimlicoWebhookVerifier } from '@pimlico/webhook'
import { pimlicoWebhookVerifier } from "@pimlico/webhook"
import type { VercelRequest, VercelResponse } from "@vercel/node"

const apiKey = process.env.PIMLICO_API_KEY as string;
const apiKey = process.env.PIMLICO_API_KEY as string

const verifyWebhook = pimlicoWebhookVerifier(apiKey);
const verifyWebhook = pimlicoWebhookVerifier(apiKey)

export default async function handler(req: VercelRequest, res: VercelResponse) {
const body = await verifyWebhook(req.headers as Record<string, string>, Buffer.from(JSON.stringify(req.body)));
const body = await verifyWebhook(
req.headers as Record<string, string>,
Buffer.from(JSON.stringify(req.body))
)

return res.status(200).json({
sponsor: true
})
return res.status(200).json({
sponsor: true
})
}
4 changes: 2 additions & 2 deletions examples/webhook/api/invalid-response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { VercelRequest, VercelResponse } from '@vercel/node'
import type { VercelRequest, VercelResponse } from "@vercel/node"

export default function handler(req: VercelRequest, res: VercelResponse) {
return res.status(200).send('Invalid response test')
return res.status(200).send("Invalid response test")
}
8 changes: 4 additions & 4 deletions examples/webhook/api/non-200.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { VercelRequest, VercelResponse } from '@vercel/node'
import type { VercelRequest, VercelResponse } from "@vercel/node"

export default function handler(req: VercelRequest, res: VercelResponse) {
return res.status(403).json({
sponsor: true,
})
return res.status(403).json({
sponsor: true
})
}
8 changes: 4 additions & 4 deletions examples/webhook/api/reject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { VercelRequest, VercelResponse } from '@vercel/node'
import type { VercelRequest, VercelResponse } from "@vercel/node"

export default function handler(req: VercelRequest, res: VercelResponse) {
return res.status(200).json({
sponsor: false,
})
return res.status(200).json({
sponsor: false
})
}
12 changes: 6 additions & 6 deletions examples/webhook/api/too-long.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { VercelRequest, VercelResponse } from '@vercel/node'
import type { VercelRequest, VercelResponse } from "@vercel/node"

export default function handler(req: VercelRequest, res: VercelResponse) {
setTimeout(() => {
return res.status(200).json({
sponsor: true
});
}, 10_000);
setTimeout(() => {
return res.status(200).json({
sponsor: true
})
}, 10_000)
}
34 changes: 17 additions & 17 deletions examples/webhook/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "@pimlico/example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"private": true,
"dependencies": {
"@pimlico/webhook": "workspace:*",
"@vercel/node": "^3.1.7",
"vercel": "^34.2.7"
}
}
"name": "@pimlico/example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"private": true,
"dependencies": {
"@pimlico/webhook": "workspace:*",
"@vercel/node": "^3.1.7",
"vercel": "^34.2.7"
}
}
46 changes: 23 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"description": "",
"main": "index.js",
"scripts": {
"lint": "biome check --error-on-warnings .",
"format": "biome format --write .",
"webhook": "pnpm --filter @pimlico/webhook",
"build": "pnpm webhook build",
"clean": "rimraf ./packages/*/node_modules ./packages/*/_cjs ./packages/*/_esm ./packages/*/_types ./packages/*/dist ./packages/*/node_modules ./node_modules"
},
"private": true,
"keywords": [],
"author": "Pimlico",
"license": "MIT",
"engines": {
"node": ">=20.0.0"
},
"type": "module",
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@types/node": "^20.11.18",
"rimraf": "^6.0.1",
"tsc-alias": "^1.8.10"
}
"description": "",
"main": "index.js",
"scripts": {
"lint": "biome check --fix --error-on-warnings .",
"format": "biome format --write .",
"webhook": "pnpm --filter @pimlico/webhook",
"build": "pnpm webhook build",
"clean": "rimraf ./packages/*/node_modules ./packages/*/_cjs ./packages/*/_esm ./packages/*/_types ./packages/*/dist ./packages/*/node_modules ./node_modules"
},
"private": true,
"keywords": [],
"author": "Pimlico",
"license": "MIT",
"engines": {
"node": ">=20.0.0"
},
"type": "module",
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@types/node": "^20.11.18",
"rimraf": "^6.0.1",
"tsc-alias": "^1.8.10"
}
}
62 changes: 31 additions & 31 deletions packages/webhook/package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
"name": "@pimlico/webhook",
"version": "0.0.1",
"author": "Pimlico",
"main": "./_cjs/index.js",
"module": "./_esm/index.js",
"types": "./_types/index.d.ts",
"typings": "./_types/index.d.ts",
"type": "module",
"sideEffects": false,
"description": "A utility library for working with Pimlico webhooks.",
"license": "MIT",
"exports": {
".": {
"types": "./_types/index.d.ts",
"import": "./_esm/index.js",
"default": "./_cjs/index.js"
}
},
"scripts": {
"build": "pnpm build:cjs && pnpm build:esm",
"build:cjs": "tsc --project ./../../tsconfig/tsconfig.webhook.cjs.json && tsc-alias -p ./../../tsconfig/tsconfig.webhook.cjs.json && printf '{\"type\":\"commonjs\"}' > ./_cjs/package.json",
"build:esm": "tsc --project ./../../tsconfig/tsconfig.webhook.esm.json && tsc-alias -p ./../../tsconfig/tsconfig.webhook.esm.json && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./_esm/package.json"
},
"devDependencies": {
"typescript": "^5.7.2",
"viem": "^2.21.52"
},
"peerDependencies": {
"viem": "^2.21.52"
}
}
"name": "@pimlico/webhook",
"version": "0.0.1",
"author": "Pimlico",
"main": "./_cjs/index.js",
"module": "./_esm/index.js",
"types": "./_types/index.d.ts",
"typings": "./_types/index.d.ts",
"type": "module",
"sideEffects": false,
"description": "A utility library for working with Pimlico webhooks.",
"license": "MIT",
"exports": {
".": {
"types": "./_types/index.d.ts",
"import": "./_esm/index.js",
"default": "./_cjs/index.js"
}
},
"scripts": {
"build": "pnpm build:cjs && pnpm build:esm",
"build:cjs": "tsc --project ./../../tsconfig/tsconfig.webhook.cjs.json && tsc-alias -p ./../../tsconfig/tsconfig.webhook.cjs.json && printf '{\"type\":\"commonjs\"}' > ./_cjs/package.json",
"build:esm": "tsc --project ./../../tsconfig/tsconfig.webhook.esm.json && tsc-alias -p ./../../tsconfig/tsconfig.webhook.esm.json && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./_esm/package.json"
},
"devDependencies": {
"typescript": "^5.7.2",
"viem": "^2.21.52"
},
"peerDependencies": {
"viem": "^2.21.52"
}
}
57 changes: 31 additions & 26 deletions packages/webhook/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
import * as crypto from "crypto";
import type { PimlicoSponsorshipPolicyWebhookBody } from "./types";
import { keyFetcher } from "./key-fetcher";
import * as crypto from "node:crypto"
import { keyFetcher } from "./key-fetcher"
import type { PimlicoSponsorshipPolicyWebhookBody } from "./types"

export const pimlicoWebhookVerifier = (apiKey: string) => async (headers: Record<string, string>, body: Buffer) => {
const fetchKey = keyFetcher(apiKey);
export const pimlicoWebhookVerifier =
(apiKey: string) =>
async (headers: Record<string, string>, body: Buffer) => {
const fetchKey = keyFetcher(apiKey)

if (!Buffer.isBuffer(body)) {
throw new Error("expected body to be a Buffer");
}
if (!Buffer.isBuffer(body)) {
throw new Error("expected body to be a Buffer")
}

if (process.env.UNSAFE_SKIP_WEBHOOK_VERIFY != null) {
return JSON.parse((body).toString()) as PimlicoSponsorshipPolicyWebhookBody;
}
if (process.env.UNSAFE_SKIP_WEBHOOK_VERIFY != null) {
return JSON.parse(
body.toString()
) as PimlicoSponsorshipPolicyWebhookBody
}

if (!body || body.length == 0) {
throw new Error("invalid webhook: empty payload");
}
if (!body || body.length === 0) {
throw new Error("invalid webhook: empty payload")
}

const key = await fetchKey();
const key = await fetchKey()

const signature = Buffer.from(headers["pimlico-signature"] ?? "", "base64");
const signature = Buffer.from(
headers["pimlico-signature"] ?? "",
"base64"
)

const message = Buffer.concat(
[
body
],
);
const message = Buffer.concat([body])

if (!crypto.verify("sha256", message, key, signature)) {
throw new Error("invalid webhook: signature validation failed");
}
if (!crypto.verify("sha256", message, key, signature)) {
throw new Error("invalid webhook: signature validation failed")
}

return JSON.parse(body.toString()) as PimlicoSponsorshipPolicyWebhookBody;
};
return JSON.parse(
body.toString()
) as PimlicoSponsorshipPolicyWebhookBody
}
Loading

0 comments on commit ebcdfb2

Please sign in to comment.