0.5.236 • Published 10 months ago

@opensea/vessel v0.5.236

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

OpenSea Vessel 🚢

A promise based wrapper for the postMessage API for communications between a parent app and an iframed child app.

OpenSea Vessel can be used to send messages between a parent app and an iframed app, and await responses to messages. Example: Confirmation of receipt, result of RPC call.

Installation

You guessed it!

pnpm install @opensea/vessel

Getting Started

Import the Vessel class

import { Vessel } from "@opensea/vessel"

Vessel class contains logic for sending and receiving messages via the postMessage. It can be used in both the parent app and child app.

Parent app

The parent app mounts the iframe that loads the child app. The parent app must pass a reference to the child iframe element into the Vessel class constructor. It is used to access the postMessage method of the iframe window.

// NOTE: Replace with reference to your iframe
const iframe = document.createElement("iframe")
const vessel = new Vessel({ iframe })

Child app

The child app is mounted in an iframe and has access to the postMessage API of its parent app's window by default. It does not require any extra params to be initialized.

const vessel = new Vessel()

Vessel Constructor

Vessel instances can be constructed with an options object. It includes the following properties:

PropertyDescriptionDefault
iframeThe iframe element to communicate with. If not provided the constructed instance will be a child vessel.undefined (instance is child vessel)
applicationThe application name to use for vessel messages. Must be shared between parent and child."opensea-vessel"
defaultTimeoutDefault number of milliseconds to wait for a message response before throwing a TimeoutError5_000 (5s)
handshakeTimeoutNumber of milliseconds to wait for a handshake response before throwing a HandshakeTimeoutError10_000 (10s)
targetOriginThe target origin for the iframe or parent window to connect to. Directly used as postMessage targetOrigin."*" (Any origin)
debugWhether to log debug messages to the console.false

Sending messages

Sending messages from parent and child via a Vessel class instance is the same.

// Examples
const response = await vessel.send("Any payload!")
const objectPayload = await vessel.send({
  foo: "bar",
  baz: { odee: ["nested", "is", "fine"] },
})
const customTimeout = await vessel.send(
  "If no response in 3 seconds this promise will reject.",
  { timeout: 3_000 },
)
const noTimeout = await vessel.send("This promise might never resolve.", {
  timeout: undefined,
})

The send method takes any JSON serializable payload as first param and an options object as the second. The options object includes the following properties:

PropertyDescriptionDefault
timeoutNumber of milliseconds to wait for a response before throwing a TimeoutError5_000 (5s)

Handling incoming messages

The Vessel class can be used to add message listeners and exposes a convenient reply function for listeners to use. Message listener must return a boolean to indicate whether they handled the message or not.

type VesselMessageListener = (
  message: VesselMessage,
  reply: (response: unknown, options: ReplyOptions) => void,
) => boolean

// Example listener
vessel.addMessageListener((message, reply) => {
  if (message.shouldBeHandledHere) {
    const result = handleThisMessage(message)
    reply(result)
    return true
  } else {
    return false
  }
})

Replying with errors

Sometimes the response to a message is an error and it is preferrable for the send method to throw an error for the sender to handle. Replying to a message with the options error boolean set to true will cause sender to reject the send promise with the payload as an error when teh response is received.

interface ReplyOptions {
  error?: boolean
}

// Example reply error
vessel.addMessageListener((message, reply) => {
  if (message.shouldBeHandledHere) {
    try {
      const result = handleThisMessage(message)
      reply(result)
    } catch (error) {
      reply(
        { reason: "Handling failed", stepsToFix: [1, 2, 3] },
        { error: true },
      )
    }
    return true
  } else {
    return false
  }
})
0.5.235

10 months ago

0.5.234

10 months ago

0.5.236

10 months ago

0.5.233

10 months ago

0.5.232

10 months ago

0.5.226

10 months ago

0.5.231

10 months ago

0.5.230

10 months ago

0.5.228

10 months ago

0.5.227

10 months ago

0.5.229

10 months ago

0.5.213

10 months ago

0.5.212

10 months ago

0.5.215

10 months ago

0.5.214

10 months ago

0.5.211

10 months ago

0.5.210

10 months ago

0.5.209

10 months ago

0.5.206

10 months ago

0.5.205

10 months ago

0.5.208

10 months ago

0.5.207

10 months ago

0.5.224

10 months ago

0.5.223

10 months ago

0.5.225

10 months ago

0.5.220

10 months ago

0.5.222

10 months ago

0.5.221

10 months ago

0.5.217

10 months ago

0.5.216

10 months ago

0.5.219

10 months ago

0.5.218

10 months ago

0.5.202

10 months ago

0.5.201

10 months ago

0.5.204

10 months ago

0.5.203

10 months ago

0.5.200

10 months ago

0.5.194

10 months ago

0.5.193

10 months ago

0.5.196

10 months ago

0.5.195

10 months ago

0.5.190

10 months ago

0.5.192

10 months ago

0.5.191

10 months ago

0.5.198

10 months ago

0.5.197

10 months ago

0.5.199

10 months ago

0.5.183

10 months ago

0.5.185

10 months ago

0.5.184

10 months ago

0.5.187

10 months ago

0.5.186

10 months ago

0.5.189

10 months ago

0.5.188

10 months ago

0.5.172

10 months ago

0.5.171

10 months ago

0.5.174

10 months ago

0.5.173

10 months ago

0.5.179

10 months ago

0.5.176

10 months ago

0.5.175

10 months ago

0.5.178

10 months ago

0.5.177

10 months ago

0.5.182

10 months ago

0.5.181

10 months ago

0.5.180

10 months ago

0.5.170

10 months ago

0.5.169

10 months ago

0.5.168

10 months ago

0.5.165

10 months ago

0.5.164

10 months ago

0.5.167

10 months ago

0.5.166

10 months ago

0.5.158

10 months ago

0.5.157

10 months ago

0.5.159

10 months ago

0.5.161

10 months ago

0.5.160

10 months ago

0.5.163

10 months ago

0.5.162

10 months ago

0.5.147

10 months ago

0.5.146

10 months ago

0.5.149

10 months ago

0.5.148

10 months ago

0.5.143

10 months ago

0.5.142

10 months ago

0.5.145

10 months ago

0.5.144

10 months ago

0.5.150

10 months ago

0.5.152

10 months ago

0.5.151

10 months ago

0.5.154

10 months ago

0.5.153

10 months ago

0.5.156

10 months ago

0.5.155

10 months ago

0.5.136

10 months ago

0.5.135

10 months ago

0.5.138

10 months ago

0.5.137

10 months ago

0.5.134

10 months ago

0.5.141

10 months ago

0.5.140

10 months ago

0.5.139

10 months ago

0.5.133

10 months ago

0.5.98

11 months ago

0.5.99

11 months ago

0.5.96

11 months ago

0.5.97

11 months ago

0.5.94

11 months ago

0.5.95

11 months ago

0.5.92

11 months ago

0.5.93

11 months ago

0.5.90

11 months ago

0.5.91

11 months ago

0.5.87

11 months ago

0.5.88

11 months ago

0.5.85

11 months ago

0.5.86

11 months ago

0.5.83

11 months ago

0.5.84

11 months ago

0.5.81

11 months ago

0.5.82

11 months ago

0.5.103

11 months ago

0.5.102

11 months ago

0.5.105

11 months ago

0.5.104

11 months ago

0.5.101

11 months ago

0.5.89

11 months ago

0.5.100

11 months ago

0.5.80

11 months ago

0.5.76

11 months ago

0.5.77

11 months ago

0.5.74

11 months ago

0.5.75

11 months ago

0.5.72

11 months ago

0.5.73

11 months ago

0.5.71

11 months ago

0.5.78

11 months ago

0.5.79

11 months ago

0.5.130

10 months ago

0.5.132

10 months ago

0.5.131

10 months ago

0.5.129

11 months ago

0.5.128

11 months ago

0.5.114

11 months ago

0.5.113

11 months ago

0.5.116

11 months ago

0.5.115

11 months ago

0.5.110

11 months ago

0.5.112

11 months ago

0.5.111

11 months ago

0.5.107

11 months ago

0.5.106

11 months ago

0.5.109

11 months ago

0.5.108

11 months ago

0.5.125

11 months ago

0.5.124

11 months ago

0.5.127

11 months ago

0.5.126

11 months ago

0.5.121

11 months ago

0.5.120

11 months ago

0.5.123

11 months ago

0.5.122

11 months ago

0.5.118

11 months ago

0.5.117

11 months ago

0.5.119

11 months ago

0.5.70

11 months ago

0.5.65

11 months ago

0.5.66

11 months ago

0.5.63

11 months ago

0.5.64

11 months ago

0.5.61

11 months ago

0.5.62

11 months ago

0.5.60

11 months ago

0.5.69

11 months ago

0.5.67

11 months ago

0.5.68

11 months ago

0.5.54

11 months ago

0.5.55

11 months ago

0.5.52

11 months ago

0.5.53

11 months ago

0.5.50

11 months ago

0.5.51

11 months ago

0.5.58

11 months ago

0.5.59

11 months ago

0.5.56

11 months ago

0.5.57

11 months ago

0.5.43

11 months ago

0.5.44

11 months ago

0.5.41

11 months ago

0.5.42

11 months ago

0.5.49

11 months ago

0.5.47

11 months ago

0.5.48

11 months ago

0.5.45

11 months ago

0.5.46

11 months ago

0.5.40

11 months ago

0.5.38

11 months ago

0.5.39

11 months ago

0.5.36

11 months ago

0.5.37

11 months ago

0.5.35

11 months ago

0.5.34

11 months ago

0.5.32

11 months ago

0.5.33

11 months ago

0.5.31

12 months ago

0.5.30

12 months ago

0.5.29

12 months ago

0.5.27

12 months ago

0.5.28

12 months ago

0.5.25

12 months ago

0.5.26

12 months ago

0.5.24

12 months ago

0.5.23

12 months ago

0.5.22

12 months ago

0.5.21

12 months ago

0.5.18

12 months ago

0.5.19

12 months ago

0.5.17

12 months ago

0.5.20

12 months ago

0.5.16

12 months ago

0.5.14

12 months ago

0.5.15

12 months ago

0.5.12

12 months ago

0.5.13

12 months ago

0.5.10

12 months ago

0.5.11

12 months ago

0.5.9

12 months ago

0.4.86

1 year ago

0.4.87

1 year ago

0.4.84

1 year ago

0.4.85

1 year ago

0.4.82

1 year ago

0.4.83

1 year ago

0.4.80

1 year ago

0.4.81

1 year ago

0.4.88

1 year ago

0.4.89

1 year ago

0.4.102

1 year ago

0.4.101

1 year ago

0.4.100

1 year ago

0.4.105

1 year ago

0.4.104

1 year ago

0.4.103

1 year ago

0.4.79

1 year ago

0.5.4

12 months ago

0.5.3

12 months ago

0.5.6

12 months ago

0.5.5

12 months ago

0.5.0

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.5.8

12 months ago

0.5.7

12 months ago

0.4.97

1 year ago

0.4.98

1 year ago

0.4.95

1 year ago

0.4.96

1 year ago

0.4.93

1 year ago

0.4.94

1 year ago

0.4.91

1 year ago

0.4.92

1 year ago

0.4.99

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.4.90

1 year ago

0.2.2

1 year ago

0.1.0

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago