From f32852a59f988caa0f8ba863510dfc17ec4e4ea2 Mon Sep 17 00:00:00 2001 From: Edward Stone Date: Tue, 10 May 2022 22:31:45 -0700 Subject: [PATCH] wip: loggable event handler --- index.js | 2 ++ lib/logTransportHandler/index.test.js | 2 +- lib/loggableEventHandler/index.js | 16 ++++++++++++++++ lib/loggableEventHandler/index.test.js | 7 +++++++ package.json | 3 ++- {test => util}/worker/index.js | 0 6 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 lib/loggableEventHandler/index.js create mode 100644 lib/loggableEventHandler/index.test.js rename {test => util}/worker/index.js (100%) diff --git a/index.js b/index.js index c95e032..a175b10 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ import { createKVCredentialsVerifier } from './lib/basicAuthenticationHandler' import { createVerifyJwksHandler } from './lib/verifyJwksHandler' +import { createLoggableEventHandler } from './lib/loggableEventHandler' export { createSitesHandler, @@ -19,4 +20,5 @@ export { createCookieEncryptor, generateStateParam, createVerifyJwksHandler, + createLoggableEventHandler } diff --git a/lib/logTransportHandler/index.test.js b/lib/logTransportHandler/index.test.js index 10bef50..b9df58e 100644 --- a/lib/logTransportHandler/index.test.js +++ b/lib/logTransportHandler/index.test.js @@ -6,7 +6,7 @@ test('send log event', async t => { const transport = sinon.spy() const mf = new Miniflare({ - scriptPath: './test/worker/dist/index.js', + scriptPath: './util/worker/dist/index.js', buildCommand: 'npm run build-test', globals: { createHandlerImportName: 'createLogTransportHandler', diff --git a/lib/loggableEventHandler/index.js b/lib/loggableEventHandler/index.js new file mode 100644 index 0000000..f301f6d --- /dev/null +++ b/lib/loggableEventHandler/index.js @@ -0,0 +1,16 @@ +import { Logger } from '@autotelic/worker-logger' + +export const createLoggableEventHandler = ({ + router, + logOptions = {} +}) => { + const log = new Logger(logOptions) + + return async event => { + const request = new Request(event.request) + request.log = log + const res = await router.handle(request, event) + event.waitUntil(request.log.report()) + return res + } +} diff --git a/lib/loggableEventHandler/index.test.js b/lib/loggableEventHandler/index.test.js new file mode 100644 index 0000000..6a45455 --- /dev/null +++ b/lib/loggableEventHandler/index.test.js @@ -0,0 +1,7 @@ +import test from 'ava' +import { createLoggableEventHandler } from './index.js' + +test('createLoggableEventHandler', async t => { + const eventHandler = createLoggableEventHandler({}) + t.pass() +}) diff --git a/package.json b/package.json index 648e80b..8115a30 100644 --- a/package.json +++ b/package.json @@ -8,13 +8,14 @@ "fix": "npm run lint -- --fix", "lint": "standard", "test": "ava --verbose", - "build-test": "esbuild --bundle --sourcemap --outdir=./test/worker/dist ./test/worker/index.js", + "build-test": "esbuild --bundle --sourcemap --outdir=./util/worker/dist ./util/worker/index.js", "validate": "npm run lint && npm run test", "verify": "miniflare examples/verify-jwks/index.js" }, "author": "", "license": "MIT", "dependencies": { + "@autotelic/worker-logger": "github:autotelic/worker-logger#main", "@cloudflare/kv-asset-handler": "^0.2.0", "@worker-tools/request-cookie-store": "^0.4.2", "base64url": "^3.0.1", diff --git a/test/worker/index.js b/util/worker/index.js similarity index 100% rename from test/worker/index.js rename to util/worker/index.js