{"id":27200715,"url":"https://github.com/speedapi/driver-ts","last_synced_at":"2025-04-09T21:30:03.603Z","repository":{"id":57177376,"uuid":"444474577","full_name":"speedapi/driver-ts","owner":"speedapi","description":"API building toolkit - TypeScript support library","archived":false,"fork":false,"pushed_at":"2023-10-17T22:40:01.000Z","size":512,"stargazers_count":0,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T22:32:10.787Z","etag":null,"topics":["api","protocol","serialization","typescript"],"latest_commit_sha":null,"homepage":"https://github.com/speedapi/info","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/speedapi.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":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-04T15:43:33.000Z","updated_at":"2022-06-29T06:32:17.000Z","dependencies_parsed_at":"2024-09-23T18:15:48.090Z","dependency_job_id":null,"html_url":"https://github.com/speedapi/driver-ts","commit_stats":null,"previous_names":["amogus-api/driver-ts"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/speedapi%2Fdriver-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/speedapi%2Fdriver-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/speedapi%2Fdriver-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/speedapi%2Fdriver-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/speedapi","download_url":"https://codeload.github.com/speedapi/driver-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248114414,"owners_count":21050032,"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":["api","protocol","serialization","typescript"],"created_at":"2025-04-09T21:30:02.509Z","updated_at":"2025-04-09T21:30:03.595Z","avatar_url":"https://github.com/speedapi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg align=\"right\" width=\"128\" src=\"https://github.com/speedapi/info/blob/master/logos/logo_color.png?raw=true\"\u003e\n\n![License](https://img.shields.io/github/license/speedapi/driver-ts)\n![Version](https://img.shields.io/npm/v/@speedapi/driver)\n![Coverage](https://coveralls.io/repos/github/speedapi/driver-ts/badge.svg?branch=master)\n![Downloads](https://img.shields.io/npm/dt/@speedapi/driver)\n![Size](https://img.shields.io/bundlephobia/minzip/@speedapi/driver)\n![PRs and issues](https://img.shields.io/badge/PRs%20and%20issues-welcome-brightgreen)\n\n# SpeedAPI wire protocol implementation\nThis library provides a SpeedAPI implementation for JS and TS in all major environments (browser, node, etc.). Install it with:\n```console\nnpm i @speedapi/driver\n```\n\nAlso install a transport level library according to your environment:\n  - `npm i @speedapi/node` for Node.JS\n  - don't see an appropriate transport here? [implement your own](#implementing-a-transport-layer)\n\nYou don't need additional libraries if you just use the `Serializer` class.\n\n**If you're using a BigInt polyfill**, add this as close to entry as possible:\n```typescript\nimport * as speedapi from \"@speedapi/driver\";\n\n// if using a polyfill that provides a BigInt(string, radix) constructor\n// (e.g. 'big-integer', 'bigint-polyfill'):\nspeedapi.repr.BigInteger.polyfillMode = \"radix\";\n\n// if using a polyfill that supports BigInt(\"0x\u003cdata\u003e\"):\nspeedapi.repr.BigInteger.polyfillMode = \"0x\";\n\n// if not using a polyfill or using a polyfill that implements\n// operators like native BigInts do (haven't seen one of those\n// in the wild):\nspeedapi.repr.BigInteger.polyfillMode = \"none\";\n// OR don't do anything, this is the default value\n```\n\n# What is SpeedAPI?\nIt's a platform-agnostic API and serialization tool specifically geared towards high-throughput realtime applications. You can read more about its features [here](https://github.com/speedapi/info)\n\n# How do I use it?\nThere's a complete tutorial over [here](https://github.com/speedapi/info/tree/master/speedapi-tutorial).\n\n## Implementing a transport layer\nYou just have to write glue code between an implementation of your desired transport protocol and SpeedAPI. The requirements for the underlying protocol are as follows:\n  - for datagram/packet/frame-based protocols like UDP: reliable and unordered - packets _must not_ get lost, but their order _may_ be mixed up (note that UDP doesn't fit this description as it's unreliable)\n  - for stream-based protocols like TCP: reliable and ordered - bytes _must not_ get lost and their order _must not_ get mixed up\n\nYou will need to write 3 or 4 classes depending on the protocol nature:\n  - A `Link`, `Client` and `Server` if the protocol is two-partied in nature, meaning there are only two active devices on the bus/network (e.g. UART)\n  - A `Link`, `Client`, `Server` and `Listener` if one server can serve multiple clients (the overwhelming majority of Internet protocols)\n\nNote that for every client-to-server link there's a `Client` object on the client and a `Server` object on the server. There's only one `Listener` on the server.\n\n### `Link`\nThis class acts as the reader/writer for SpeedAPI and should extend `Duplex`. Its only job is to read and write `Uint8Array`s. This class will be instantiated by you, therefore you can make the constructor of an arbitrary signature. Here's the skeleton for such a class:\n```ts\nimport { Duplex } from \"@speedapi/driver\";\nclass MyLink extends Duplex {\n    constructor(/* pass what you need (e.g. a socket) */) { }\n    async write(data: Uint8Array): Promise\u003cvoid\u003e { }\n    async read(cnt: number): Promise\u003cUint8Array\u003e { }\n    async close(): Promise\u003cvoid\u003e { }\n}\n```\n\nAlternatively, you can extend from `BufferedLink` if the underlying protocol implementation is event-driven, meaning it provides a \"data has arrived\" callback, but not a \"read me N bytes\" function.\n\n```ts\nimport { BufferedLink } from \"@speedapi/driver/transport/universal\";\nclass MyLink extends BufferedLink {\n    constructor(/* again, pass what you need */) {\n        someProtocol.on(\"bytesArrived\", (data: Uint8Array) =\u003e {\n            // feed BufferedLink data as it arrives\n            this.dataArrived(data);\n        });\n    }\n\n    protected async dataWrite(data: Uint8Array): Promise\u003cvoid\u003e { }\n    override async close(): Promise\u003cvoid\u003e { }\n}\n```\n\n### `Server` and `Client`\nVery thin wrappers that tell SpeedAPI about your `Link` while preserving type information.\n\n**Important**: if you omit type information (e.g. if you're using plain JS to implement these two classes), your IDE will not be able to provide suggestions.\n```ts\nimport { SpecSpaceGen, Session } from \"@speedapi/driver\";\nclass MyClient\u003cGen extends SpecSpaceGen\u003e extends Session\u003cGen\u003e {\n    constructor(specSpace: Gen /* any other arguments */) {\n        super(specSpace, new MyLink(/* your arguments */), \"client\");\n    }\n}\nclass MyServer\u003cGen extends SpecSpaceGen\u003e extends Session\u003cGen\u003e {\n    constructor(specSpace: Gen /* any other arguments */) {\n        super(specSpace, new MyLink(/* your arguments */), \"server\");\n    }\n}\n```\n\n### `Listener` (optional)\nThe notice from before applies here as well.\n```ts\nclass MyListener\u003cGen extends SpecSpaceGen\u003e {\n    constructor(specSpace: Gen, /* any other arguments */, callback: (server: MyServer\u003cGen\u003e) =\u003e void /* optional too */) {\n        someProtocol.on(\"clientConnected\", (socket) =\u003e {\n            const session = new MyServer(specSpace, socket /* or any other arguments per your definition */);\n            callback(session);\n        });\n    }\n\n    async close() { }\n}\n```\n\n# Testing\n**Warning**: this repository uses a `pnpm` lock file, hence you can't substitute it for `npm` below.\n```\ngit clone https://github.com/speedapi/driver-ts\ncd driver-ts\npnpm i\npip3 install susc\npnpm test\n```\n\n# TypeScript notice\nThis project relies _heavily_ on typing. As such, the language server gets misled into thinking that an expression type is `any` even though it's not just because the type is deeply nested. If you see a type error in your IDE that you think shouldn't be there or you're missing some autocomplete entries, try restarting your IDE and/or language server.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspeedapi%2Fdriver-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspeedapi%2Fdriver-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspeedapi%2Fdriver-ts/lists"}