New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@elasticbottle/trpc-post-message

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@elasticbottle/trpc-post-message

tRPC adapter for post messages 📨

  • 0.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
73
decreased by-6.41%
Maintainers
1
Weekly downloads
 
Created
Source

trpc-post-message



Help this repo out, STAR it!

Post Message support for tRPC 📨

  • Easy communication between iframes.
  • Typesafe messaging between parent and child windows
  • soon to be compatible with web workers

Usage

1. Install trpc-post-message.

# npm
npm install @elasticbottle/trpc-post-message
# yarn
yarn add @elasticbottle/trpc-post-message
# pnpm
pnpm add @elasticbottle/trpc-post-message

2. Add createPostMessageHandler in your background script.

// background.ts
import { initTRPC } from "@trpc/server";
import { createPostMessageHandler } from "@elasticbottle/trpc-post-message/adapter";

const t = initTRPC.create({
  isServer: false,
  allowOutsideOfServer: true,
});

const appRouter = t.router({
  // ...procedures
});

export type AppRouter = typeof appRouter;

createPostMessageHandler({
  router: appRouter,
  postMessage: ({ message }) => window.postMessage(message, "your_targeted_url"),
  addEventListener: (listener) =>
    window.addEventListener("message", (e) => {
      if (e.origin !== 'your_whitelisted_domain') {
        return;
      }
      listener(e);
    }),
}); /* 👈 */,

3. Add a PostMessageLink to the client in your content script.

// content.ts
import { createTRPCClient } from "@trpc/client";
import { PostMessageLink } from "@elasticbottle/trpc-post-message/link";

import type { AppRouter } from "./background";

export const PostMessageClient = createTRPCClient<AppRouter>({
  links: [
    PostMessageLink({
      postMessage: ({ message }) => window.postMessage(message, "your_targeted_url"),
      addEventListener: (listener) => {
        const customerListener = (e) => {
          if (e.origin !== 'your_whitelisted_domain') {
            return;
          }
          listener(e);
        }
        window.addEventListener("message", customerListener)
        // if you don't return anything it is assumed that the default listener was used
        return customerListener;
      },
      removeEventListener: (listener) =>
        window.removeEventListener("message", listener),
    }),
  ], /* 👈 */,
});

Requirements

Peer dependencies:

  • tRPC Server v10 (@trpc/server) must be installed.
  • tRPC Client v10 (@trpc/client) must be installed.

Types

PostMessageLinkOption

Please see full typings here.

PropertyTypeDescriptionRequired
postMessageFunctionCalled to send data to the "server". You must send the message param as istrue
addEventListenerFunctionCalled to add listener to receive request from the "server".true

CreatePostMessageHandlerOptions

Please see full typings here.

PropertyTypeDescriptionRequired
routerRouterYour application tRPC router.true
postMessageFunctionCalled to send data to the "client". You must send the message param as istrue
addEventListenerFunctionCalled to add listener to receive request from the "client".true
createContextFunctionPasses contextual (ctx) data to procedure resolvers.false
onErrorFunctionCalled if error occurs inside handler.false

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Winston Yeo - Follow me on Twitter @winston_yeo 💖

Acknowledgements

Ths project would not have been possible without @jlalmes and his well-documented trpc-chrome package for which this code base was heavily built upon.

Keywords

FAQs

Package last updated on 15 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc