Skip to content

Commit

Permalink
Fix: better browser runtime port transport port type (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniGuardiola committed Dec 20, 2023
1 parent c4975a2 commit 4058d02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-colts-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rpc-anywhere": patch
---

Fix: better browser runtime port transport port type.
12 changes: 6 additions & 6 deletions src/transports/browser-runtime-port.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Browser } from "browser-namespace";
import { type Browser, type Chrome } from "browser-namespace";

import {
rpcTransportMessageIn,
Expand All @@ -7,6 +7,8 @@ import {
} from "../transport-utils.js";
import { type RPCTransport } from "../types.js";

type Port = Browser.Runtime.Port | Chrome.runtime.Port;

/**
* Options for the message port transport.
*/
Expand All @@ -24,21 +26,19 @@ export type RPCBrowserRuntimePortTransportOptions = Pick<
* as arguments. For example, messages can be filtered
* based on `port.name` or `port.sender`.
*/
filter?: (message: any, port: Browser.Runtime.Port) => boolean;
filter?: (message: any, port: Port) => boolean;
};

/**
* Creates a transport from a browser runtime port. Useful for RPCs
* between content scripts and service workers in web extensions.
*/
export function createTransportFromBrowserRuntimePort(
port: Browser.Runtime.Port,
port: Port,
options: RPCBrowserRuntimePortTransportOptions = {},
): RPCTransport {
const { transportId, filter } = options;
let transportHandler:
| ((message: any, port: Browser.Runtime.Port) => void)
| undefined;
let transportHandler: ((message: any, port: Port) => void) | undefined;
return {
send(data) {
port.postMessage.bind(rpcTransportMessageOut(data, { transportId }));
Expand Down

0 comments on commit 4058d02

Please sign in to comment.