@slipher/generic-adapter
Advanced tools
| import type { Logger, UsingClient } from 'seyfert'; | ||
| import type { HttpServerAdapter } from 'seyfert/lib/client/types'; | ||
| import { type APIInteraction } from 'seyfert/lib/types'; | ||
| export declare class GenericAdapter implements HttpServerAdapter { | ||
| client: UsingClient; | ||
| publicKeyHex: Buffer; | ||
| applicationId: string; | ||
| debugger?: Logger; | ||
| logger: Logger; | ||
| constructor(client: UsingClient); | ||
| start(): Promise<void>; | ||
| protected verifySignature(req: Request): Promise<APIInteraction | undefined>; | ||
| fetch(req: Request): Promise<any>; | ||
| } |
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.GenericAdapter = void 0; | ||
| const common_1 = require("seyfert/lib/common"); | ||
| const types_1 = require("seyfert/lib/types"); | ||
| const tweetnacl_1 = __importDefault(require("tweetnacl")); | ||
| class GenericAdapter { | ||
| client; | ||
| publicKeyHex; | ||
| applicationId; | ||
| debugger; | ||
| logger; | ||
| constructor(client) { | ||
| this.client = client; | ||
| this.logger = client.logger; | ||
| } | ||
| async start() { | ||
| if (this.client.debugger) | ||
| this.debugger = this.client.debugger; | ||
| const { publicKey, applicationId } = await this.client.getRC(); | ||
| if (!publicKey) { | ||
| throw new Error('Expected a publicKey, check your config file'); | ||
| } | ||
| if (applicationId) { | ||
| this.applicationId = applicationId; | ||
| } | ||
| this.publicKeyHex = Buffer.from(publicKey, 'hex'); | ||
| this.logger.info('Running on <url>'); | ||
| } | ||
| async verifySignature(req) { | ||
| const timestamp = req.headers.get('x-signature-timestamp'); | ||
| const ed25519 = req.headers.get('x-signature-ed25519') ?? ''; | ||
| const body = (await req.json()); | ||
| if (tweetnacl_1.default.sign.detached.verify(Buffer.from(timestamp + JSON.stringify(body)), Buffer.from(ed25519, 'hex'), this.publicKeyHex)) { | ||
| return body; | ||
| } | ||
| return; | ||
| } | ||
| async fetch(req) { | ||
| const rawBody = await this.verifySignature(req); | ||
| if (!rawBody) { | ||
| this.debugger?.debug('Invalid request/No info, returning 418 status.'); | ||
| // I'm a teapot | ||
| return new Response('', { status: 418 }); | ||
| } | ||
| switch (rawBody.type) { | ||
| case types_1.InteractionType.Ping: { | ||
| this.debugger?.debug('Ping interaction received, responding.'); | ||
| return Response.json({ type: types_1.InteractionResponseType.Pong }, { | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| } | ||
| default: { | ||
| if ((0, common_1.isCloudfareWorker)()) { | ||
| // you can not do more net requests after responding. | ||
| // so we use discord api instead | ||
| return this.client.handleCommand | ||
| .interaction(rawBody, -1) | ||
| .then(() => new Response()) | ||
| .catch(() => new Response()); | ||
| } | ||
| return new Promise(async (r) => { | ||
| const { headers, response } = await this.client.onInteractionRequest(rawBody); | ||
| r(response instanceof FormData | ||
| ? new Response(response, { headers }) | ||
| : Response.json(response, { | ||
| headers, | ||
| })); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| exports.GenericAdapter = GenericAdapter; |
| export * from './adapter'; |
| "use strict"; | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| var desc = Object.getOwnPropertyDescriptor(m, k); | ||
| if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
| desc = { enumerable: true, get: function() { return m[k]; } }; | ||
| } | ||
| Object.defineProperty(o, k2, desc); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| __exportStar(require("./adapter"), exports); |
+4
-4
| { | ||
| "name": "@slipher/generic-adapter", | ||
| "version": "0.0.7", | ||
| "version": "0.0.8", | ||
| "private": false, | ||
@@ -16,8 +16,8 @@ "license": "MIT", | ||
| "dependencies": { | ||
| "seyfert": "4.1.0", | ||
| "seyfert": "4.4.0", | ||
| "tweetnacl": "^1.0.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^25.0.10", | ||
| "typescript": "^5.9.3" | ||
| "@types/node": "^26.0.1", | ||
| "typescript": "^6.0.3" | ||
| }, | ||
@@ -24,0 +24,0 @@ "scripts": { |
9989
80.99%10
66.67%222
100%4
100%+ Added
- Removed
Updated