@@ -1,3 +1,4 @@
|
||||
/* eslint-disable unicorn/custom-error-definition */
|
||||
export class InvalidSignatureError extends Error {
|
||||
constructor() {
|
||||
super('Invalid Signature')
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import assert from 'assert'
|
||||
import { createHmac, timingSafeEqual } from 'crypto'
|
||||
import { EventEmitter } from 'events'
|
||||
import type { IncomingMessage, ServerResponse } from 'http'
|
||||
import assert from 'node:assert'
|
||||
import { createHmac, timingSafeEqual } from 'node:crypto'
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { InvalidSignatureError } from './error'
|
||||
import type { IncomingMessage, ServerResponse } from 'node:http'
|
||||
import type { BusinessEvents } from './event.enum'
|
||||
import type { ExtendedEventEmitter, GenericEvent } from './types'
|
||||
|
||||
import { InvalidSignatureError } from './error'
|
||||
|
||||
interface CreateHandlerOptions {
|
||||
secret: string
|
||||
// events?: 'all' | BusinessEvents[]
|
||||
@@ -35,8 +34,8 @@ export const createHandler = (options: CreateHandlerOptions): Handler => {
|
||||
res.statusCode = 200
|
||||
res.setHeader('Content-Type', 'application/json')
|
||||
res.end(JSON.stringify({ ok: 1 }))
|
||||
} catch (err) {
|
||||
if (err instanceof InvalidSignatureError) {
|
||||
} catch (error) {
|
||||
if (error instanceof InvalidSignatureError) {
|
||||
handler.emitter.emit('error', new Error('invalidate signature'))
|
||||
|
||||
res.statusCode = 400
|
||||
@@ -45,7 +44,7 @@ export const createHandler = (options: CreateHandlerOptions): Handler => {
|
||||
return
|
||||
}
|
||||
res.statusCode = 500
|
||||
res.end(JSON.stringify({ ok: 0, message: err.message }))
|
||||
res.end(JSON.stringify({ ok: 0, message: error.message }))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,18 +7,17 @@ import type { PostModel } from '@core/modules/post/post.model'
|
||||
import type { NormalizedPost } from '@core/modules/post/post.type'
|
||||
import type { RecentlyModel } from '@core/modules/recently/recently.model'
|
||||
import type { SayModel } from '@core/modules/say/say.model'
|
||||
import type { EventEmitter } from 'events'
|
||||
import type { EventEmitter } from 'node:events'
|
||||
import type { BusinessEvents } from './event.enum'
|
||||
|
||||
export interface ExtendedEventEmitter extends EventEmitter {
|
||||
on<T extends BusinessEvents>(
|
||||
on: (<T extends BusinessEvents>(
|
||||
event: T,
|
||||
listener: (
|
||||
data: EventPayloadMapping[Extract<T, keyof EventPayloadMapping>],
|
||||
) => void,
|
||||
): this
|
||||
|
||||
on(event: '*', listener: (event: GenericEvent) => void): this
|
||||
) => this) &
|
||||
((event: '*', listener: (event: GenericEvent) => void) => this)
|
||||
}
|
||||
export type Id = string
|
||||
export type PayloadOnlyId = { data: Id }
|
||||
|
||||
Reference in New Issue
Block a user