Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions lib/event-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ function EventBus ({ logger = console } = {}) {
})
}

/**
* Emit an event to this machine's in-memory EventEmitter
* @param {object} opts
* @param {string} opts.channel - Channel name
* @param {any} opts.payload
*/
EventBus.prototype.emitLocalEvent = function (opts) {
return this.events.emit(opts.channel, opts.payload)
}

/**
* Emit an event to the Redis bus, which will tell every subscriber about it
* @param {object} opts
Expand All @@ -68,7 +58,7 @@ EventBus.prototype.emitEvent = process.env.REDIS_URL
// Only emit local events if Redis isn't configured

: async function (opts) {
this.emitLocalEvent(opts)
return this.events.emit(opts.channel, opts.payload)
}

module.exports = EventBus
102 changes: 58 additions & 44 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const crypto = require('crypto')
const path = require('path')
const fs = require('fs')
const Fastify = require('fastify')
const fastifyRawBody = require('fastify-raw-body')
const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('@fastify/swagger-ui')
const fastifyCors = require('@fastify/cors')
Expand All @@ -27,6 +28,7 @@ const EventBus = require('./event-bus')
const KeepAlive = require('./keep-alive')

const isProd = process.env.NODE_ENV === 'production'
const port = parseInt(process.env.PORT, 10) || 3000

const [
faviconIco,
Expand Down Expand Up @@ -54,10 +56,42 @@ const [
Sentry.setupFastifyErrorHandler(fastify)
}

await fastify.register(fastifySwagger)
await fastify.register(fastifySwaggerUi)
await fastify.register(fastifyCors)
await fastify.register(fastifyHelmet)
await fastify.register(fastifyRawBody, {
global: false,
})

await fastify.register(fastifySwagger, {
openapi: {
openapi: '3.0.0',
info: {
title: 'smee.io',
description: 'The smee.io REST-API',
version: '0.1.0'
},
servers: [
{
url: `http://localhost:${port}`,
description: 'Development server'
},
{
url: 'https://smee.io',
description: 'Production server'
}
]
}
})
await fastify.register(fastifySwaggerUi, {
logo: false,
theme: {
title: 'smee.io API',
}
})
await fastify.register(fastifyCors, {
origin: '*'
})
await fastify.register(fastifyHelmet, {
contentSecurityPolicy: false
})

if (process.env.FORCE_HTTPS) {
await fastify.register(forceHttps)
Expand Down Expand Up @@ -253,6 +287,9 @@ const [
})

fastify.post('/:channel', {
config: {
rawBody: true
},
onRequest: channelsBlockedHandler,
schema: {
params: {
Expand All @@ -266,63 +303,40 @@ const [
}
}
},
headers: {
type: 'object',
properties: {
'x-github-event': { type: 'string' },
'x-github-delivery': { type: 'string', format: 'uuid' },
'x-hub-signature-256': {
type: 'string',
description: 'SHA256 HMAC hex digest of the body, using the webhook secret as the key, in the format sha256=...',
pattern: '^sha256=[a-f0-9]{64}$'
},
},
additionalProperties: {
type: 'string'
}
},
body: {
type: 'object'
}
}
}, async (req, reply) => {
// Emit an event to the Redis bus
await bus.emitEvent({
channel: req.params.channel,
payload: {
...req.headers,
body: req.body,
rawBody: req.rawBody.toString('utf-8'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rawBody is unmodified body, which will fit x-hub-signature-256

query: req.query,
timestamp: Date.now()
}
})

return reply.status(200).send()
return reply.status(200).send('ok\n')
})

// Resend payload via the event emitter
fastify.post('/:channel/redeliver',
{
onRequest: channelsBlockedHandler,
params: {
type: 'object',
properties: {
channel: {
type: 'string',
minLength: 1,
maxLength: 128,
pattern: '^[a-zA-Z0-9-_]+$'
}
}
},
schema: {
params: {
type: 'object',
properties: {
channel: {
type: 'string'
}
}
},
body: {
type: 'object'
}
}
}, async (req, reply) => {
// Emit an event to the Redis bus
await bus.emitEvent({
channel: req.params.channel,
payload: req.body
})
return reply.status(200)
})

const port = parseInt(process.env.PORT, 10) || 3000
fastify.listen({ port, host: '0.0.0.0' }, (err, address) => {
if (err) {
console.error(err)
Expand Down
Loading