{"id":18825008,"url":"https://github.com/lulusir/worker-webserver","last_synced_at":"2026-02-27T06:05:05.055Z","repository":{"id":180365088,"uuid":"642273880","full_name":"lulusir/worker-webserver","owner":"lulusir","description":"Worker-webserver is a lightweight NPM package that allows you to implement a web server in your service worker using the fetch event. It allows you to intercept incoming requests, match them with a user-defined router, and execute a series of middleware functions to handle them.","archived":false,"fork":false,"pushed_at":"2023-09-05T15:58:17.000Z","size":111,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-19T09:44:48.009Z","etag":null,"topics":["service-worker","web-server"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lulusir.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-18T07:47:26.000Z","updated_at":"2024-10-05T19:10:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"a3773b00-a9ae-44b5-b619-8ef6b512b75c","html_url":"https://github.com/lulusir/worker-webserver","commit_stats":null,"previous_names":["lulusir/worker-webserver"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/lulusir/worker-webserver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fworker-webserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fworker-webserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fworker-webserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fworker-webserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lulusir","download_url":"https://codeload.github.com/lulusir/worker-webserver/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fworker-webserver/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29887018,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T05:38:26.446Z","status":"ssl_error","status_checked_at":"2026-02-27T05:38:25.235Z","response_time":57,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["service-worker","web-server"],"created_at":"2024-11-08T00:58:09.810Z","updated_at":"2026-02-27T06:05:05.021Z","avatar_url":"https://github.com/lulusir.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[中文文档](https://github.com/lulusir/worker-webserver/blob/main/README.zh.md)\n\n# worker-webserver\n\nWorker-webserver is a lightweight NPM package that allows you to implement a web server in your service worker using the fetch event. It allows you to intercept incoming requests, match them with a user-defined router, and execute a series of middleware functions to handle them.\n\n\n## Installation \nYou can install worker-webserver via NPM using the following command:\n```bash\nnpm install worker-webserver --save\n```\n\n## Usage\nExport sw.js using the CLI command and place it in your static resource directory. If you are using Vite or Umi, it corresponds to the public folder\n\n```bash\nnpx worker-webserver --out public\n```\nFirst, import the necessary functions and interfaces:\n```typescript\nimport { App, Route } from 'worker-webserver'\n```\n\nThen, create an array of custom routes to match incoming requests:\n\n```typescript\nconst customRoutes: Route[] = [\n  {\n    path: \"/users/:id\",\n    method: \"POST\",\n    handler: async (ctx) =\u003e {\n      ctx.res.body = JSON.stringify({\n        test: \"test\",\n        ...ctx.params,\n      });\n      ctx.res.headers.set(\"content-type\", \"application/json\");\n    },\n  },\n];\n\n```\n\nYou can define your own middleware functions to handle incoming requests and responses. For example:\n```typescript\nconst app = new App();\n\napp.addRoutes(customRoutes);\n\n\napp.use(async (ctx, next) =\u003e {\n  await next();\n\n  // unified code to 200\n  if (ctx.res.body) {\n    const contentType = ctx.res.headers.get(\"content-type\");\n    if (contentType === \"application/json\") {\n      try {\n        let body = JSON.parse(ctx.res.body);\n        if (body.code) {\n          body.code = 200;\n        }\n        ctx.res.body = JSON.stringify(body);\n      } catch {}\n    }\n  }\n});\n\n```\n\nFinally, start the worker-webserver by calling the start() function.\n```typescript\napp.start();\n```\n\nYou can also stop the worker-webserver by calling the stop() function.\n```typescript\napp.stop();\n```\n\nThat's it! You can now intercept incoming requests, match them with a user-defined router, and execute middleware functions to handle them.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flulusir%2Fworker-webserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flulusir%2Fworker-webserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flulusir%2Fworker-webserver/lists"}