{"id":51395738,"url":"https://github.com/ntoskrnl7/typed-message-transport","last_synced_at":"2026-07-04T02:06:54.610Z","repository":{"id":258930527,"uuid":"875549407","full_name":"ntoskrnl7/typed-message-transport","owner":"ntoskrnl7","description":"typed-message-transport is a TypeScript-based library designed for handling strongly-typed message exchanges between different environments, such as browser workers or Node.js processes. This library facilitates sending and receiving messages with a clear structure and type safety, using message channels. ex) WebSocket, RTCDataChannel, MessagePort","archived":false,"fork":false,"pushed_at":"2025-12-18T07:43:26.000Z","size":343,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-12-21T15:17:46.274Z","etag":null,"topics":["data-transfer","message","message-broker","message-bus","message-passing-interface","transport","typecript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/ntoskrnl7.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-10-20T09:31:12.000Z","updated_at":"2025-12-18T07:43:29.000Z","dependencies_parsed_at":"2024-12-12T02:32:34.216Z","dependency_job_id":"9d16fa2a-b4d4-447b-823f-2b7fccb45729","html_url":"https://github.com/ntoskrnl7/typed-message-transport","commit_stats":null,"previous_names":["ntoskrnl7/typed-message-transport"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ntoskrnl7/typed-message-transport","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ntoskrnl7%2Ftyped-message-transport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ntoskrnl7%2Ftyped-message-transport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ntoskrnl7%2Ftyped-message-transport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ntoskrnl7%2Ftyped-message-transport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ntoskrnl7","download_url":"https://codeload.github.com/ntoskrnl7/typed-message-transport/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ntoskrnl7%2Ftyped-message-transport/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35107508,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-04T02:00:05.987Z","response_time":113,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["data-transfer","message","message-broker","message-bus","message-passing-interface","transport","typecript"],"created_at":"2026-07-04T02:06:53.610Z","updated_at":"2026-07-04T02:06:54.605Z","avatar_url":"https://github.com/ntoskrnl7.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `typed-message-transport` - Message Transport API\n\n`typed-message-transport` is a TypeScript-based library designed for handling strongly-typed message exchanges between different environments, such as browser workers or Node.js processes. This library facilitates sending and receiving messages with a clear structure and type safety, using message channels (e.g., `MessageChannel` or `MessagePort`).\n\n## Table of Contents\n\n- [`typed-message-transport` - Message Transport API](#typed-message-transport---message-transport-api)\n  - [Table of Contents](#table-of-contents)\n  - [Overview](#overview)\n    - [Key Features](#key-features)\n  - [Usage](#usage)\n    - [Installation](#installation)\n    - [Basic Usage](#basic-usage)\n    - [Message Types](#message-types)\n    - [Sending Messages](#sending-messages)\n    - [Receiving Messages](#receiving-messages)\n      - [Setting up a Specific Handler](#setting-up-a-specific-handler)\n      - [Setting up a General Handler](#setting-up-a-general-handler)\n      - [Listening for Messages with `on`](#listening-for-messages-with-on)\n\n## Overview\n\n`typed-message-transport` provides a simple and type-safe way to send messages between different parts of your application. This can be used in various contexts, such as inter-process communication (IPC) or cross-environment communication between browsers, workers, or Node.js processes. It simplifies the task of dealing with `MessagePort` or `MessageChannel` while keeping your code clean and type-safe.\n\nThe library utilizes TypeScript's type system to ensure that the messages sent between different endpoints match expected types, reducing runtime errors.\n\n### Key Features\n\n- Type-safe message exchange using TypeScript's type system.\n- Flexible integration with `MessagePort`, `MessageChannel`, or custom transport mechanisms.\n- Supports sending requests and receiving responses with built-in promise handling.\n\n## Usage\n\nTo use `typed-message-transport`, you need to import `MessageTransport` and instantiate it with a message map that defines the message types.\n\n### Installation\n\nYou can install the package via npm or yarn:\n\n```bash\nnpm install typed-message-transport\n# or\nyarn add typed-message-transport\n```\n\n### Basic Usage\n\nFirst, import MessageTransport and define your message types in a MessageMap. The MessageMap allows you to define the structure of each message type, including both requests and responses.\n\n```typescript\nimport { MessageTransport } from 'typed-message-transport';\n\ntype MessageMap = {\n  'message-type': {\n    request: [message: string, value: number];\n    response: { replyMessage: string };\n  };\n};\n\nconst transport = new MessageTransport\u003cMessageMap\u003e({\n  send(data: ArrayBuffer) {\n    // send the data over a MessagePort or another transport mechanism\n  },\n  onMessage(onMessage) {\n    // receive the message and invoke the provided callback\n  },\n});\n```\n\n### Message Types\n\nIn the MessageMap, you define each message type by specifying:\n\n- A request structure, which defines the data sent when the message is initiated.\n- A response structure, which defines the data returned in response to the message.\n\nFor example:\n\n- The message type 'message-type' has a request with a tuple containing a string and a number.\n- The response for 'message-type' will be an object with a success boolean.\n\n### Sending Messages\n\nTo send a message, use the `send` method, passing the appropriate message type and payload.\n\n```typescript\nawait transport.send('message-type', 'Hello', 42);\nconst result = await transport.sendAndWait('message-type', 'Hello', 42);\n// result.replyMessage\n```\n\n- `send`: Sends a message without expecting a reply.\n- `sendAndWait`: Sends a message and waits for a response, returning the result of the handler's reply.\n\nThe message will be sent over the transport mechanism, and the payload will be type-checked based on the `MessageMap`.\n\n### Receiving Messages\n\n#### Setting up a Specific Handler\n\nTo receive and handle messages, use the `setHandler` method to register a specific handler for a message type.\n\n```typescript\ntransport.setHandler('message-type', async (message, value) =\u003e {\n    message // 'Hello'\n    value // 42\n    return { replyMessage: message + value };\n});\n```\n\nIn this example, the handler processes incoming messages of type `'message-type'` with parameters message and value. The handler can return a response, which will be sent back if `sendAndWait` was used.\n\n#### Setting up a General Handler\n\nYou can also use a general handler to handle multiple message types, enabling more flexible message handling based on `type`.\n\n```typescript\ntransport.setHandler((type, args, done) =\u003e {\n    switch (type) {\n        case 'message-type':\n            return done({ replyMessage: args[0] + args[1] });\n        default:\n            throw new Error('not supported');\n            break;\n    }\n});\n```\n\nHere, setHandler processes different message types by evaluating type and handling arguments dynamically, supporting a more flexible approach for multiple message types.\n\n#### Listening for Messages with `on`\n\nFor non-async handling or simpler message processing, use the `on` method. This method listens for the specified message type and triggers the callback whenever a message of that type is received.\n\n```typescript\ntransport.on('message-type', (message, value) =\u003e {\n    message // 'Hello'\n    value // 42\n});\n```\n\nThe `on` method allows for direct handling of the `message-type` messages with immediate access to message and value parameters, ideal for non-blocking message handling.\n\nFor more detailed examples and test cases, refer to the message.transport.test.ts file in the typed-message-transport repository.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fntoskrnl7%2Ftyped-message-transport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fntoskrnl7%2Ftyped-message-transport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fntoskrnl7%2Ftyped-message-transport/lists"}