Skip to content

Commit

Permalink
wip: log transport with test
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmundo committed May 9, 2022
1 parent 6b17f8b commit d3de406
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/logTransportHandler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const defaultConfig = {
transport: async(logEvent, event) => console.log(logEvent)
}

export const createLogTransportHandler = (config = defaultConfig) => {
const {
transport
} = {
...defaultConfig,
...config
}

return async (request, event) => {
const logEvent = await request.json()
event.waitUntil(transport(logEvent, event))
return new Response(null, { status: 202 })
}
}
29 changes: 29 additions & 0 deletions lib/logTransportHandler/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import test from 'ava'
import sinon from 'sinon'
import { Miniflare } from 'miniflare'

test('send log event', async t => {
const transport = sinon.spy()

const mf = new Miniflare({
scriptPath: './test/worker/dist/index.js',
buildCommand: 'npm run build-test',
globals: {
createHandlerImportName: 'createLogTransportHandler',
createHandlerArgs: [{ transport }],
},
})

const logEvent = {
level: 30,
message: 'log message'
}

const res = await mf.dispatchFetch('http://localhost:8787/', {
method: 'POST',
body: JSON.stringify(logEvent)
})
t.true(transport.calledOnceWith(logEvent))
t.is(res.status, 202)
t.is(res.body, null)
})
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"fix": "npm run lint -- --fix",
"lint": "standard",
"test": "ava --verbose lib/**/*.test.js",
"test": "ava --verbose",
"build-test": "esbuild --bundle --sourcemap --outdir=./test/worker/dist ./test/worker/index.js",
"validate": "npm run lint && npm run test",
"verify": "miniflare examples/verify-jwks/index.js"
},
Expand All @@ -19,11 +20,13 @@
"base64url": "^3.0.1",
"itty-router": "^2.4.4",
"itty-router-extras": "^0.4.2",
"oso": "^0.26.0",
"jose": "^4.8.0"
"jose": "^4.8.0",
"oso": "^0.26.0"
},
"devDependencies": {
"ava": "^4.1.0",
"esbuild": "^0.14.38",
"miniflare": "^2.4.0",
"sinon": "^13.0.2",
"standard": "^17.0.0"
}
Expand Down
21 changes: 21 additions & 0 deletions test/worker/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ThrowableRouter } from 'itty-router-extras'
// import { createLogTransportHandler } from '../../lib/logTransportHandler/index.js'

addEventListener('fetch', event => {
const { request } = event
// event.respondWith(router.handle(request, event))
event.respondWith(eventHandler(event))
})

async function eventHandler(event) {
// const handlerImportName = 'createLogTransportHandler'
const { [createHandlerImportName]: createHandler } = await import('../../lib/logTransportHandler/index.js')
const handler = createHandler(...createHandlerArgs)

const router = ThrowableRouter()
router.all('*', handler)

const { request } = event
const response = await router.handle(request, event)
return response
}

0 comments on commit d3de406

Please sign in to comment.