{"id":13779694,"url":"https://github.com/koajs/middleware-hook","last_synced_at":"2025-10-19T13:31:38.370Z","repository":{"id":66114044,"uuid":"44295559","full_name":"koajs/middleware-hook","owner":"koajs","description":"low-level hooks for your middleware","archived":true,"fork":false,"pushed_at":"2016-01-01T07:07:41.000Z","size":5,"stargazers_count":6,"open_issues_count":1,"forks_count":3,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-29T15:32:22.721Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koajs.png","metadata":{"files":{"readme":"Readme.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-15T05:23:49.000Z","updated_at":"2024-08-31T21:09:07.000Z","dependencies_parsed_at":"2023-02-20T00:30:56.512Z","dependency_job_id":null,"html_url":"https://github.com/koajs/middleware-hook","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fmiddleware-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fmiddleware-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fmiddleware-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fmiddleware-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/middleware-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237133608,"owners_count":19260509,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:01:08.153Z","updated_at":"2025-10-19T13:31:33.094Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","readme":"\n# koa-middleware-hook\n\nlow-level hooks for your middleware. this is mostly an experiment,\nbut it lets you hook into the following spots:\n\n  1. Downstream: Beginning of the middleware\n  2. Downstream: Before yielding to the next middleware in the stack\n  3. Upstream: Beginning on the way back up the stack\n  4. Upstream: When the middleware finishs\n\nuseful for profiling your middleware, logging your middleware, timing your middleware, etc.\n\nprobably not suitable for production, though I haven't profiled it or anything.\n\n## Install\n\n```\nnpm install koa-middleware-hook\n```\n\n## Usage\n\n##### `hook = Hook(function: marker, function: reduce)`\n\n`marker` is a function called during each point in time where you'd return what you want to track. Here's an example:\n\n```js\nfunction marker () {\n  return new Date()\n}\n```\n\n`reduce` takes all 4 hooks and allows you to merge them together in some meaningful way. Something to keep in mind is that the innermost middleware will only execute 2 hooks:\n\n```js\nfunction reduce (middleware_name, downstream_start, downstream_end | upstream_end, upstream_start | null, upstream_end | null) {\n  if (arguments.length === 3) {\n    debug('%s: %sms', middleware_name, downstream_end - downstream_start)\n  } else {\n    debug('%s: %sms', middleware_name, (downstream_end - downstream_start) + (upstream_end - upstream_start))\n  }\n}\n```\n\nHere's how you use hook:\n\n```\napp.use(hook(bodyparser()))\n```\n\n## TODO\n\n- solicit ideas for a better API\n- some more usage examples\n\n## License\n\nMIT\n","funding_links":[],"categories":["Middleware"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fmiddleware-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Fmiddleware-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fmiddleware-hook/lists"}