{"id":40481914,"url":"https://github.com/e280/comrade","last_synced_at":"2026-01-20T18:33:23.603Z","repository":{"id":286590679,"uuid":"961713324","full_name":"e280/comrade","owner":"e280","description":"🤖 web-workers of the world unite!","archived":false,"fork":false,"pushed_at":"2025-10-10T07:16:25.000Z","size":631,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-11T20:56:53.125Z","etag":null,"topics":[],"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/e280.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null}},"created_at":"2025-04-07T03:39:59.000Z","updated_at":"2025-10-10T07:16:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"735019b4-5577-476a-ba64-7b772a41ebf2","html_url":"https://github.com/e280/comrade","commit_stats":null,"previous_names":["e280/comrade"],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/e280/comrade","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e280%2Fcomrade","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e280%2Fcomrade/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e280%2Fcomrade/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e280%2Fcomrade/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/e280","download_url":"https://codeload.github.com/e280/comrade/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e280%2Fcomrade/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28609120,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T16:10:39.856Z","status":"ssl_error","status_checked_at":"2026-01-20T16:10:39.493Z","response_time":117,"last_error":"SSL_read: 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":[],"created_at":"2026-01-20T18:33:22.854Z","updated_at":"2026-01-20T18:33:23.591Z","avatar_url":"https://github.com/e280.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cdiv align=\"center\"\u003e\u003cimg alt=\"\" width=\"512\" src=\"./assets/comrade.avif\"/\u003e\u003c/div\u003e\n\n\u003cbr/\u003e\n\n# 🤖 Comrade\n- comrade aims to be the best web worker library for typescript\n- bidirectional by default — you can call worker functions, and they can call you\n- clusters can magically schedule async calls across web workers\n- seamless browser and node compatibility\n- async rpc powered by [renraku](https://github.com/e280/renraku)\n- a project for https://e280.org/\n\n\u003cbr/\u003e\n\n## Web-workers of the world unite!\n\n### Install comrade\n```sh\nnpm i @e280/comrade\n```\n\n### Make your schematic type\nyour `Schematic` tells comrade about your functions\n```ts\n// schematic.ts\n\nimport {AsSchematic} from \"@e280/comrade\"\n\nexport type MySchematic = AsSchematic\u003c{\n\n  // functions on the worker. main thread can call these.\n  work: {\n    add(a: number, b: number): Promise\u003cnumber\u003e\n    sub(a: number, b: number): Promise\u003cnumber\u003e\n  }\n\n  // functions on main thread. workers can call these.\n  host: {\n    mul(a: number, b: number): Promise\u003cnumber\u003e\n    div(a: number, b: number): Promise\u003cnumber\u003e\n  }\n}\u003e\n```\n\n\u003e 💁 ***note*** — *arbitrary nesting is fine, actually*  \n\u003e ```ts\n\u003e export type MySchematic = AsSchematic\u003c{\n\u003e   work: {\n\u003e     add(a: number, b: number): Promise\u003cnumber\u003e\n\u003e     nesty: {\n\u003e       is: {\n\u003e         besty(a: number, b: number): Promise\u003cnumber\u003e\n\u003e       }\n\u003e     }\n\u003e   }\n\u003e }\u003e\n\u003e ```\n\u003e ```ts\n\u003e await work.add(2, 3) // 5\n\u003e await work.nesty.is.besty(2, 3) // 5\n\u003e ```\n\n### Make your worker\n```ts\n// worker.ts\n\nimport {Comrade} from \"@e280/comrade\"\nimport {MySchematic} from \"./schematic.js\"\n\nawait Comrade.worker\u003cMySchematic\u003e(shell =\u003e ({\n  async add(a, b) {\n    return a + b\n  },\n  async sub(a, b) {\n    return a - b\n  },\n}))\n```\n\n\u003e 💁 ***terminology***  \n\u003e - the `shell` gives you access to the other side's functionality\n\u003e   ```ts\n\u003e   async add(a, b) {\n\u003e \n\u003e     // calling the host (from the worker)\n\u003e     await shell.host.mul(2, 3)\n\u003e \n\u003e     return a + b\n\u003e   },\n\u003e   ```\n\u003e - the `shell.transfer` lets you mark transferables for your returns (for zero-copy transfers)\n\u003e   ```ts\n\u003e   async getNiceBytes(a, b) {\n\u003e     const bytes = new Uint8Array([0xB0, 0x0B, 0x13, 0x5])\n\u003e \n\u003e     shell.transfer = [bytes]\n\u003e \n\u003e     return bytes\n\u003e   },\n\u003e   ```\n\n\u003e 😱 ***bundler warning***  \n\u003e you're probably going to have to bundle your worker module, especially since for some reason the spec/browser people never finished importmap support in workers, so a bundler is required to resolve dependencies in workers 🤷\n\n### Do the work\nso, now you have a choice — you can either spin up a single worker, or you can spin up a cluster of workers.\n- **spin up a single worker thread**\n  ```ts\n  // thread.ts\n\n  import {Comrade} from \"@e280/comrade\"\n  import {MySchematic} from \"./schematic.js\"\n\n  const thread = await Comrade.thread\u003cMySchematic\u003e({\n\n    // relative url to your worker module\n    workerUrl: new URL(\"./worker.js\", import.meta.url),\n\n    // functions on the main thread, workers can call these\n    setupHost: shell =\u003e ({\n      async mul(a: number, b: number) {\n        return a * b\n      },\n      async div(a: number, b: number) {\n        return a / b\n      },\n    }),\n  })\n\n  // calling worker functions\n  await thread.work.add(2, 3) // 5\n  await thread.work.sub(3, 2) // 1\n\n  // terminate the workers when you're all done\n  thread.terminate()\n  ```\n- **spin up a cluster of workers**\n  ```ts\n  // cluster.ts\n\n  import {Comrade} from \"@e280/comrade\"\n  import {MySchematic} from \"./schematic.js\"\n\n  const cluster = await Comrade.cluster\u003cMySchematic\u003e({\n\n    // relative url to your worker module\n    workerUrl: new URL(\"./worker.js\", import.meta.url),\n\n    // functions on the main thread, workers can call these\n    setupHost: shell =\u003e ({\n      async mul(a: number, b: number) {\n        return a * b\n      },\n      async div(a: number, b: number) {\n        return a / b\n      },\n    }),\n  })\n\n  // calling a worker functions\n  await cluster.work.add(2, 3) // 5\n  await cluster.work.sub(3, 2) // 1\n\n  // terminate the workers when you're all done\n  cluster.terminate()\n  ```\n  - each call is a queued task, and tasks are round-robin distributed across the worker pool\n  - your work must be *stateless* — when you call a work function, you don't know which worker will respond\n  - the number of workers in the pool will be your hardware concurrency minus one (eg, on an eight-core cpu, we expect 7 workers in the pool)\n\n\u003cbr/\u003e\n\n## Now let's get more organized\nthe helpers `host` and `work` help you export functions from separate files.\n\n```ts\n// work.ts\nexport const setupWork = Comrade.work\u003cMySchematic\u003e(shell =\u003e {\n  async add(a, b) {\n    return a + b\n  },\n  async sub(a, b) {\n    return a - b\n  },\n})\n```\n\n```ts\n// host.ts\nexport const setupHost = Comrade.host\u003cMySchematic\u003e(shell =\u003e {\n  async mul(a: number, b: number) {\n    return a * b\n  },\n  async div(a: number, b: number) {\n    return a / b\n  },\n})\n```\n\nuse these in your workers, threads, or clusters\n```ts\nawait Comrade.worker\u003cMySchematic\u003e(setupWork)\n```\n```ts\nconst thread = await Comrade.thread\u003cMySchematic\u003e({workerUrl, setupHost})\n```\n```ts\nconst cluster = await Comrade.cluster\u003cMySchematic\u003e({workerUrl, setupHost})\n```\n\n### Mocks — fake it 'till you make it\nfor testing purposes, you can skip the whole worker/thread/cluster situation and create a mock setup like this\n```ts\n// mocks.ts\nimport {setupWork} from \"./work.js\"\nimport {setupHost} from \"./host.js\"\n\nexport const {work, host} = Comrade.mocks\u003cMySchematic\u003e({setupWork, setupHost})\n\nawait work.add(2, 3) // 5\nawait host.mul(2, 3) // 6\n```\n\n### Logging\nby default, comrade uses an `ErrorTap` which logs errors to the console.\n\nif you want more verbose noisy logging (logging every request):\n- pass a logger tap to `Comrade.thread`\n    ```ts\n    import {LoggerTap} from \"@e280/comrade\"\n\n    const thread = await Comrade.thread\u003cMySchematic\u003e({\n      workerUrl,\n      setupHost,\n      tap: new LoggerTap(), // 👈 passing in a logger tap\n    })\n    ```\n- pass a logger tap to `Comrade.cluster`\n    ```ts\n    const cluster = await Comrade.cluster\u003cMySchematic\u003e({\n      workerUrl,\n      setupHost,\n      tap: new LoggerTap(), // 👈 passing in a logger tap\n    })\n    ```\n- pass a logger tap to `Comrade.mocks`\n    ```ts\n    const {host, work} = await Comrade.mocks\u003cMySchematic\u003e({\n      setupHost,\n      setupWork,\n      tap: new LoggerTap(), // 👈 passing in a logger tap\n    })\n    ```\n\nif you want silence (not even errors), provide a dud tap:\n```ts\nimport {DudTap} from \"@e280/comrade\"\n\nconst thread = await Comrade.thread\u003cMySchematic\u003e({\n  workerUrl,\n  setupHost,\n  tap: new DudTap(), // 👈 dud tap does nothing, total silence\n})\n```\n\n\u003cbr/\u003e\n\n## Tune the calls\nthis advancedness is brought to you by [renraku](https://github.com/e280/renraku)\n\n### Transferables aren't copied\nyou can provide an array of transferables on any api call\n\n```ts\nimport {tune} from \"@e280/comrade\"\n\n// some example data\nconst buffer = new Uint8Array([0xDE, 0xAD, 0xBE, 0xEF]).buffer\n\n  //                      🤫                😲\n  //                      👇                👇\nawait cluster.work.hello[tune]({transfer: [buffer]})({\n  lol: \"whatever\",\n  buffer, // \u003c-- this gets transfered speedy-fastly, not copied (we like this)\n})\n```\n\nthat's good for outgoing requests, but now you also need to set transferables for your responses, which is done like this\n\n```ts\nawait Comrade.worker\u003cMySchematic\u003e(shell =\u003e ({\n  async coolAction() {\n    const buffer = new Uint8Array([0xDE, 0xAD, 0xBE, 0xEF]).buffer\n\n    // set transferables for this response\n    shell.transfer = [buffer] // \u003c-- will be transferred, not copied\n\n    return {hello: \"world\", buffer}\n  },\n}))\n```\n\n### Notifications get no response\nyou can also make a call a *notification*, which means no response will be sent back (just shouting into the void)\n\n```ts\nimport {tune} from \"@e280/comrade\"\n\n  //                               🫢\n  //                               👇\nawait cluster.work.goodbye[tune]({notify: true})({\n  lol: \"whatever\",\n})\n```\n\n\u003cbr/\u003e\n\n## 💖 Made with open source love\nbuild with us at https://e280.org/ but only if you're cool\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fe280%2Fcomrade","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fe280%2Fcomrade","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fe280%2Fcomrade/lists"}