{"id":28425235,"url":"https://github.com/metamask/post-message-stream","last_synced_at":"2025-06-26T01:31:38.389Z","repository":{"id":37183100,"uuid":"65500270","full_name":"MetaMask/post-message-stream","owner":"MetaMask","description":"Sets up a duplex object stream over window.postMessage","archived":false,"fork":false,"pushed_at":"2025-04-15T14:47:38.000Z","size":716,"stargazers_count":59,"open_issues_count":3,"forks_count":28,"subscribers_count":63,"default_branch":"main","last_synced_at":"2025-06-05T10:50:28.032Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MetaMask.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":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":null,"patreon":null,"open_collective":"metamask","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-08-11T20:42:41.000Z","updated_at":"2025-04-15T14:38:40.000Z","dependencies_parsed_at":"2023-02-08T18:46:03.491Z","dependency_job_id":"85a2e49e-9fad-40d3-8fd5-75a223a4caaf","html_url":"https://github.com/MetaMask/post-message-stream","commit_stats":{"total_commits":64,"total_committers":13,"mean_commits":4.923076923076923,"dds":0.640625,"last_synced_commit":"b26220d45c3d451c3672d9ecfb8c07b4fd418fba"},"previous_names":["kumavis/post-message-stream"],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/MetaMask/post-message-stream","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fpost-message-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fpost-message-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fpost-message-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fpost-message-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MetaMask","download_url":"https://codeload.github.com/MetaMask/post-message-stream/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fpost-message-stream/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261982521,"owners_count":23240080,"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":"2025-06-05T10:36:39.505Z","updated_at":"2025-06-26T01:31:38.381Z","avatar_url":"https://github.com/MetaMask.png","language":"TypeScript","funding_links":["https://opencollective.com/metamask"],"categories":[],"sub_categories":[],"readme":"# @metamask/post-message-stream\n\nA Node.js duplex stream interface over various kinds of JavaScript inter-\"process\" communication channels, for Node.js and the Web.\nOriginally the only communication channel used was `window.postMessage()`, but the package has since expanded in scope.\n\n## Usage (Node.js)\n\n### `ProcessParentMessageStream` and `ProcessMessageStream`\n\nNode.js [`child_process.fork()`](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options) streams.\nThe parent process creates a child process with a dedicated IPC channel using `child_process.fork()`.\n\nIn the parent process:\n\n```javascript\nimport { fork } from 'child_process';\nimport { ProcessParentMessageStream } from '@metamask/post-message-stream';\n\n// `modulePath` is the path to the JavaScript module that will instantiate the\n// child stream.\nconst process = fork(modulePath);\n\nconst parentStream = new ProcessParentMessageStream({ process });\nparentStream.write('hello');\n```\n\nIn the child process:\n\n```javascript\nimport { ProcessMessageStream } from '@metamask/post-message-stream';\n\n// The child stream automatically \"connects\" to the dedicated IPC channel via\n// properties on `globalThis.process`.\nconst childStream = new ProcessMessageStream();\nchildStream.on('data', (data) =\u003e console.log(data + ', world'));\n// \u003e 'hello, world'\n```\n\n### `ThreadParentMessageStream` and `ThreadMessageStream`\n\nNode.js [`worker_threads`](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options) streams.\nThe parent process creates a worker thread using `new worker_threads.Worker()`.\n\nIn the parent environment:\n\n```javascript\nimport { Worker } from 'worker_threads';\nimport { ThreadParentMessageStream } from '@metamask/post-message-stream';\n\n// `modulePath` is the path to the JavaScript module that will instantiate the\n// child stream.\nconst thread = new Worker(modulePath);\n\nconst parentStream = new ThreadParentMessageStream({ thread });\nparentStream.write('hello');\n```\n\nIn the child thread:\n\n```javascript\nimport { ThreadMessageStream } from '@metamask/post-message-stream';\n\n// The child stream automatically \"connects\" to the parent via\n// `worker_threads.parentPort`.\nconst childStream = new ThreadMessageStream();\nchildStream.on('data', (data) =\u003e console.log(data + ', world'));\n// \u003e 'hello, world'\n```\n\n## Usage (Web)\n\n### `WebWorkerParentPostMessageStream` and `WebWorkerPostMessageStream`\n\nThese streams are intended for **dedicated** [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) only.\nThey might sort-of work with shared workers, but attempt that at your own risk.\n\nIn the parent window:\n\n```javascript\nimport { WebWorkerParentPostMessageStream } from '@metamask/post-message-stream';\n\nconst worker = new Worker(url);\n\nconst parentStream = new WebWorkerParentPostMessageStream({ worker });\nparentStream.write('hello');\n```\n\nIn the child `WebWorker`:\n\n```javascript\nimport { WebWorkerPostMessageStream } from '@metamask/post-message-stream';\n\nconst workerStream = new WebWorkerPostMessageStream();\nworkerStream.on('data', (data) =\u003e console.log(data + ', world'));\n// \u003e 'hello, world'\n```\n\n### `WindowPostMessageStream`\n\nIf you have two windows, A and B, that can communicate over `postMessage`, set up a stream in each.\nBe sure to make use of the `targetOrigin` and `targetWindow` parameters to ensure that you are communicating with your intended subject.\n\nIn window A, with URL `https://foo.com`, trying to communicate with an iframe, `iframeB`:\n\n```javascript\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\n\nconst streamA = new WindowPostMessageStream({\n  name: 'streamA', // We give this stream a name that the other side can target.\n\n  target: 'streamB', // This must match the `name` of the other side.\n\n  // Adding `targetWindow` below already ensures that we will only _send_\n  // messages to `iframeB`, but we need to specify its origin as well to ensure\n  // that we only _receive_ messages from `iframeB`.\n  targetOrigin: new URL(iframeB.src).origin,\n\n  // We have to specify the content window of `iframeB` as the target, or it\n  // won't receive our messages.\n  targetWindow: iframeB.contentWindow,\n});\n\nstreamA.write('hello');\n```\n\nIn window B, running in an iframe accessible in window A:\n\n```javascript\nconst streamB = new WindowPostMessageStream({\n  // Notice that these values are reversed relative to window A.\n  name: 'streamB',\n  target: 'streamA',\n\n  // The origin of window A. If we don't specify this, it would default to\n  // `location.origin`, which won't work if the local origin is different. We\n  // could pass `*`, but that's potentially unsafe.\n  targetOrigin: 'https://foo.com',\n\n  // We omit `targetWindow` here because it defaults to `window`.\n});\n\nstreamB.on('data', (data) =\u003e console.log(data + ', world'));\n// \u003e 'hello, world'\n```\n\n#### Gotchas\n\nUnder the hood, `WindowPostMessageStream` uses `window.addEventListener('message', (event) =\u003e ...)`.\nIf `event.source` is not referentially equal to the stream's `targetWindow`, all messages will be ignored.\nThis can happen in environments where `window` objects are proxied, such as Electron.\n\n## Contributing\n\n### Setup\n\n- Install [Node.js](https://nodejs.org) version 12\n  - If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.\n- Install [Yarn v1](https://yarnpkg.com/en/docs/install)\n- Run `yarn setup` to install dependencies and run any requried post-install scripts\n  - **Warning:** Do not use the `yarn` / `yarn install` command directly. Use `yarn setup` instead. The normal install command will skip required post-install scripts, leaving your development environment in an invalid state.\n\n### Testing and Linting\n\nRun `yarn test` to run the tests once. To run tests on file changes, run `yarn test:watch`.\n\nRun `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and fix any automatically fixable issues.\n\n### Release \u0026 Publishing\n\nThe project follows the same release process as the other libraries in the MetaMask organization. The GitHub Actions [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) are used to automate the release process; see those repositories for more information about how they work.\n\n1. Choose a release version.\n\n   - The release version should be chosen according to SemVer. Analyze the changes to see whether they include any breaking changes, new features, or deprecations, then choose the appropriate SemVer version. See [the SemVer specification](https://semver.org/) for more information.\n\n2. If this release is backporting changes onto a previous release, then ensure there is a major version branch for that version (e.g. `1.x` for a `v1` backport release).\n\n   - The major version branch should be set to the most recent release with that major version. For example, when backporting a `v1.0.2` release, you'd want to ensure there was a `1.x` branch that was set to the `v1.0.1` tag.\n\n3. Trigger the [`workflow_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch) event [manually](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow) for the `Create Release Pull Request` action to create the release PR.\n\n   - For a backport release, the base branch should be the major version branch that you ensured existed in step 2. For a normal release, the base branch should be the main branch for that repository (which should be the default value).\n   - This should trigger the [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) workflow to create the release PR.\n\n4. Update the changelog to move each change entry into the appropriate change category ([See here](https://keepachangelog.com/en/1.0.0/#types) for the full list of change categories, and the correct ordering), and edit them to be more easily understood by users of the package.\n\n   - Generally any changes that don't affect consumers of the package (e.g. lockfile changes or development environment changes) are omitted. Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).\n   - Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).\n   - Consolidate related changes into one change entry if it makes it easier to explain.\n   - Run `yarn auto-changelog validate --rc` to check that the changelog is correctly formatted.\n\n5. Review and QA the release.\n\n   - If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.\n\n6. Squash \u0026 Merge the release.\n\n   - This should trigger the [`action-publish-release`](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.\n\n7. Publish the release on npm.\n\n   - Be very careful to use a clean local environment to publish the release, and follow exactly the same steps used during CI.\n   - Use `npm publish --dry-run` to examine the release contents to ensure the correct files are included. Compare to previous releases if necessary (e.g. using `https://unpkg.com/browse/[package name]@[package version]/`).\n   - Once you are confident the release contents are correct, publish the release using `npm publish`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetamask%2Fpost-message-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetamask%2Fpost-message-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetamask%2Fpost-message-stream/lists"}