|
| 1 | +import { DurableObject } from 'cloudflare:workers'; |
| 2 | +import { Env } from '../../worker-configuration'; |
| 3 | +import { AppConfig } from '../config'; |
| 4 | +import { handleRequest } from '../utils/request-handler-util'; |
| 5 | +import { ApiError } from '../utils/api-error-util'; |
| 6 | +import { ErrorCode } from '../utils/error-catalog-util'; |
| 7 | +import { Logger } from '../utils/logger-util'; |
| 8 | + |
| 9 | +export class ChainhooksDO extends DurableObject<Env> { |
| 10 | + // Configuration constants |
| 11 | + private readonly BASE_PATH: string = '/chainhooks'; |
| 12 | + private readonly CACHE_PREFIX: string = this.BASE_PATH.replaceAll('/', ''); |
| 13 | + private readonly SUPPORTED_ENDPOINTS: string[] = ['/post_event']; |
| 14 | + |
| 15 | + constructor(ctx: DurableObjectState, env: Env) { |
| 16 | + super(ctx, env); |
| 17 | + this.ctx = ctx; |
| 18 | + this.env = env; |
| 19 | + |
| 20 | + // Initialize AppConfig with environment |
| 21 | + const config = AppConfig.getInstance(env).getConfig(); |
| 22 | + } |
| 23 | + |
| 24 | + async fetch(request: Request): Promise<Response> { |
| 25 | + const url = new URL(request.url); |
| 26 | + const path = url.pathname; |
| 27 | + const method = request.method; |
| 28 | + |
| 29 | + return handleRequest( |
| 30 | + async () => { |
| 31 | + if (!path.startsWith(this.BASE_PATH)) { |
| 32 | + throw new ApiError(ErrorCode.NOT_FOUND, { resource: path }); |
| 33 | + } |
| 34 | + |
| 35 | + // Remove base path to get the endpoint |
| 36 | + const endpoint = path.replace(this.BASE_PATH, ''); |
| 37 | + |
| 38 | + // Handle root path |
| 39 | + if (endpoint === '' || endpoint === '/') { |
| 40 | + return { |
| 41 | + message: `Supported endpoints: ${this.SUPPORTED_ENDPOINTS.join(', ')}`, |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + // Handle post_event endpoint |
| 46 | + if (endpoint === '/post_event') { |
| 47 | + if (method !== 'POST') { |
| 48 | + throw new ApiError(ErrorCode.INVALID_REQUEST, { |
| 49 | + reason: `Method ${method} not allowed for this endpoint. Use POST.`, |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + return await this.handlePostEvent(request); |
| 54 | + } |
| 55 | + |
| 56 | + // If we get here, the endpoint is not supported |
| 57 | + throw new ApiError(ErrorCode.NOT_FOUND, { |
| 58 | + resource: endpoint, |
| 59 | + supportedEndpoints: this.SUPPORTED_ENDPOINTS, |
| 60 | + }); |
| 61 | + }, |
| 62 | + this.env, |
| 63 | + { |
| 64 | + path, |
| 65 | + method, |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + private async handlePostEvent(request: Request): Promise<any> { |
| 71 | + const logger = Logger.getInstance(this.env); |
| 72 | + |
| 73 | + try { |
| 74 | + // Clone the request to read the body |
| 75 | + const clonedRequest = request.clone(); |
| 76 | + |
| 77 | + // Try to parse as JSON first |
| 78 | + let body; |
| 79 | + try { |
| 80 | + body = await clonedRequest.json(); |
| 81 | + } catch (e) { |
| 82 | + // If JSON parsing fails, get the body as text |
| 83 | + body = await request.text(); |
| 84 | + } |
| 85 | + |
| 86 | + // Log the received event |
| 87 | + logger.info('Received chainhook event', { |
| 88 | + body, |
| 89 | + headers: Object.fromEntries(request.headers.entries()), |
| 90 | + }); |
| 91 | + |
| 92 | + // Store the event in Durable Object storage for later analysis |
| 93 | + const eventId = crypto.randomUUID(); |
| 94 | + await this.ctx.storage.put(`event_${eventId}`, { |
| 95 | + timestamp: new Date().toISOString(), |
| 96 | + body, |
| 97 | + headers: Object.fromEntries(request.headers.entries()), |
| 98 | + }); |
| 99 | + |
| 100 | + return { |
| 101 | + message: 'Event received and logged successfully', |
| 102 | + eventId, |
| 103 | + }; |
| 104 | + } catch (error) { |
| 105 | + logger.error('Error processing chainhook event', error instanceof Error ? error : new Error(String(error))); |
| 106 | + throw new ApiError(ErrorCode.INTERNAL_ERROR, { |
| 107 | + reason: 'Failed to process chainhook event', |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments