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

@opensea/vessel

Package Overview
Dependencies
Maintainers
10
Versions
623
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opensea/vessel - npm Package Compare versions

Comparing version 0.0.13 to 0.0.14

145

dist/@opensea/vessel.es.js

@@ -1,4 +0,4 @@

var O = Object.defineProperty;
var I = (s, e, t) => e in s ? O(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var r = (s, e, t) => (I(s, typeof e != "symbol" ? e + "" : e, t), t);
var m = Object.defineProperty;
var I = (s, e, t) => e in s ? m(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var i = (s, e, t) => (I(s, typeof e != "symbol" ? e + "" : e, t), t);
const d = /* @__PURE__ */ new Map();

@@ -10,6 +10,6 @@ function g(s, e, t) {

), d.delete(s)), d.set(s, e), typeof t == "number" && t > 0 && (e.timeout = setTimeout(() => {
u(s), e.reject(new Error("[Vessel] Action timed out."));
f(s), e.reject(new Error("[Vessel] Action timed out."));
}, t));
}
function u(s) {
function f(s) {
const e = d.get(s);

@@ -24,3 +24,3 @@ e && (e.timeout && clearTimeout(e.timeout), d.delete(s));

}
function f(s = "log", e) {
function u(s = "log", e) {
const t = b(e == null ? void 0 : e.scope);

@@ -33,5 +33,5 @@ return (...n) => {

return {
info: f("info", s),
warn: f("warn", s),
error: f("error", s)
info: u("info", s),
warn: u("warn", s),
error: u("error", s)
};

@@ -56,11 +56,11 @@ }

}
var i = /* @__PURE__ */ ((s) => (s.Handshake = "handshake", s.HandshakeReply = "handshake-reply", s.Message = "message", s.MessageReply = "message-reply", s.Close = "close", s))(i || {});
var r = /* @__PURE__ */ ((s) => (s.Handshake = "handshake", s.HandshakeReply = "handshake-reply", s.Message = "message", s.MessageReply = "message-reply", s.Close = "close", s))(r || {});
class h {
constructor(e, t) {
r(this, "type");
r(this, "id");
r(this, "from");
r(this, "operation");
r(this, "payload");
r(this, "metadata");
i(this, "type");
i(this, "id");
i(this, "from");
i(this, "operation");
i(this, "payload");
i(this, "metadata");
this.type = e, this.id = (t == null ? void 0 : t.id) ?? H(), this.from = (t == null ? void 0 : t.from) ?? v, this.operation = (t == null ? void 0 : t.operation) ?? C(), this.payload = t == null ? void 0 : t.payload, this.metadata = t == null ? void 0 : t.metadata;

@@ -80,3 +80,3 @@ }

function A(s, e = v) {
if (s && typeof s == "object" && "from" in s && s.from === e && "type" in s && Object.values(i).includes(s.type) && "id" in s && "operation" in s)
if (s && typeof s == "object" && "from" in s && s.from === e && "type" in s && Object.values(r).includes(s.type) && "id" in s && "operation" in s)
return new h(s.type, {

@@ -91,3 +91,3 @@ id: s.id,

}
function m(s, e, t) {
function O(s, e, t) {
if (e === "*" || s.origin === e)

@@ -99,10 +99,10 @@ return A(s.data, t ?? v);

constructor(e) {
r(this, "log");
r(this, "parent", window.parent);
r(this, "subscriptions");
r(this, "closed", !1);
r(this, "parentOrigin");
i(this, "log");
i(this, "parent", window.parent);
i(this, "subscriptions");
i(this, "closed", !1);
i(this, "parentOrigin");
this.parentOrigin = e.parentOrigin, this.subscriptions = {
[i.Message]: [],
[i.Close]: []
[r.Message]: [],
[r.Close]: []
}, this.log = R({ debug: e.debug, scope: "CHILD" }), this.onMessage = this.onMessage.bind(this), window.addEventListener("message", this.onMessage, !1);

@@ -112,3 +112,3 @@ }

const n = this.sendMessage.bind(this);
if (e === i.Close) {
if (e === r.Close) {
this.close(), this.subscriptions[e].forEach((o) => o(t));

@@ -120,3 +120,3 @@ return;

n(
new h(i.MessageReply, {
new h(r.MessageReply, {
operation: t.operation,

@@ -132,11 +132,11 @@ payload: a,

try {
const t = m(e, this.parentOrigin);
const t = O(e, this.parentOrigin);
switch (t.type) {
case i.Handshake:
case r.Handshake:
this.sendHandshakeReply(t);
return;
case i.Message:
this.callSubscriptions(i.Message, t);
case r.Message:
this.callSubscriptions(r.Message, t);
return;
case i.MessageReply:
case r.MessageReply:
this.resolveReply(t);

@@ -151,3 +151,3 @@ return;

this.log.info(`Received handshake from ${this.parentOrigin}`);
const t = new h(i.HandshakeReply, {
const t = new h(r.HandshakeReply, {
operation: e.operation

@@ -169,3 +169,3 @@ });

const { resolve: o, reject: a } = n;
if (u(e.operation), (t = e.payload) != null && t.error) {
if (f(e.operation), (t = e.payload) != null && t.error) {
a(e.payload.error);

@@ -197,3 +197,3 @@ return;

return new Promise((o, a) => {
const l = new h(i.Message, {
const l = new h(r.Message, {
payload: e,

@@ -216,3 +216,3 @@ metadata: t

emit(e, t) {
const n = new h(i.Message, {
const n = new h(r.Message, {
payload: e,

@@ -238,3 +238,3 @@ metadata: t

this.closed = !0, window.removeEventListener("message", this.onMessage);
const e = new h(i.Close);
const e = new h(r.Close);
this.parent.postMessage(e.toObject(), this.parentOrigin);

@@ -245,16 +245,16 @@ }

constructor(e) {
r(this, "parentFrame");
r(this, "child");
r(this, "frame");
r(this, "handshakeOperation");
r(this, "handshakeInterval");
r(this, "handshakeActionResolverId", "HANDSHAKE");
r(this, "subscriptions");
r(this, "log");
r(this, "childOrigin");
r(this, "childUrl");
r(this, "connected");
i(this, "parentFrame");
i(this, "child");
i(this, "frame");
i(this, "handshakeOperation");
i(this, "handshakeInterval");
i(this, "handshakeActionResolverId", "HANDSHAKE");
i(this, "subscriptions");
i(this, "log");
i(this, "childOrigin");
i(this, "childUrl");
i(this, "connected");
this.frame = e.frame, this.parentFrame = window, this.child = null, this.childUrl = e.url, this.childOrigin = $(e.url), this.connected = !1, this.handshakeOperation = null, this.handshakeInterval = null, this.subscriptions = {
[i.Message]: [],
[i.Close]: []
[r.Message]: [],
[r.Close]: []
}, this.log = R({ debug: e.debug, scope: "PARENT" }), this.parentFrame.addEventListener(

@@ -273,3 +273,3 @@ "message",

e += 1;
const n = new h(i.Handshake);
const n = new h(r.Handshake);
this.handshakeOperation = n.operation, this.sendMessage(n);

@@ -288,3 +288,3 @@ }

const n = this.sendMessage.bind(this);
if (e === i.Close) {
if (e === r.Close) {
this.close(), this.subscriptions[e].forEach((o) => o(t));

@@ -296,3 +296,3 @@ return;

n(
new h(i.MessageReply, {
new h(r.MessageReply, {
operation: t.operation,

@@ -316,3 +316,3 @@ payload: a,

const { resolve: n, reject: o } = t;
if (u(this.handshakeActionResolverId), (a = e.payload) != null && a.error) {
if (f(this.handshakeActionResolverId), (a = e.payload) != null && a.error) {
o(e.payload.error);

@@ -325,15 +325,15 @@ return;

try {
const t = m(e, this.childOrigin);
const t = O(e, this.childOrigin);
switch (t.type) {
case i.HandshakeReply:
case r.HandshakeReply:
this.handleHandshakeReply(t);
break;
case i.Message:
this.callSubscriptions(i.Message, t);
case r.Message:
this.callSubscriptions(r.Message, t);
break;
case i.MessageReply:
case r.MessageReply:
this.resolveReply(t);
break;
case i.Close:
this.callSubscriptions(i.Close, t);
case r.Close:
this.callSubscriptions(r.Close, t);
break;

@@ -355,3 +355,3 @@ default:

const { resolve: o, reject: a } = n;
if (u(e.operation), (t = e.payload) != null && t.error) {
if (f(e.operation), (t = e.payload) != null && t.error) {
a(e.payload.error);

@@ -369,3 +369,3 @@ return;

throw new c("Child frame not found.");
if (e.type !== i.Handshake && !this.connected)
if (e.type !== r.Handshake && !this.connected)
throw new c("Not connected to child frame.");

@@ -384,3 +384,3 @@ this.child.postMessage(e.toObject(), this.childOrigin);

return new Promise((o, a) => {
const l = new h(i.Message, {
const l = new h(r.Message, {
payload: e,

@@ -403,3 +403,3 @@ metadata: t

emit(e, t) {
const n = new h(i.Message, {
const n = new h(r.Message, {
payload: e,

@@ -410,8 +410,5 @@ metadata: t

}
connect() {
return new Promise((e, t) => {
var o;
this.log.info(`Connecting to child iframe ${this.childUrl}`), this.child && ["loaded", "interactive", "complete"].includes(
(o = this.child) == null ? void 0 : o.document.readyState
) ? this.startHandshake() : this.frame.addEventListener("load", this.startHandshake.bind(this)), g(this.handshakeActionResolverId, { resolve: e, reject: t });
connect({ isFrameLoaded: e = !1 }) {
return new Promise((t, n) => {
this.log.info(`Connecting to child iframe ${this.childUrl}`), e ? this.startHandshake() : this.frame.addEventListener("load", this.startHandshake.bind(this)), g(this.handshakeActionResolverId, { resolve: t, reject: n });
});

@@ -433,3 +430,3 @@ }

close() {
this.log.info("Closing frames"), this.subscriptions[i.Message] = [], this.subscriptions[i.Close] = [], this.handshakeOperation = null, this.handshakeInterval && window.clearInterval(this.handshakeInterval), u(this.handshakeActionResolverId), this.connected = !1;
this.log.info("Closing frames"), this.subscriptions[r.Message] = [], this.subscriptions[r.Close] = [], this.handshakeOperation = null, this.handshakeInterval && window.clearInterval(this.handshakeInterval), f(this.handshakeActionResolverId), this.connected = !1;
}

@@ -445,8 +442,8 @@ }

}
r(M, "ParentFrame", k), r(M, "ChildFrame", y);
i(M, "ParentFrame", k), i(M, "ChildFrame", y);
export {
y as ChildFrame,
i as MessageType,
r as MessageType,
k as ParentFrame,
M as Vessel
};

@@ -1,1 +0,1 @@

(function(l,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(l=typeof globalThis<"u"?globalThis:l||self,a(l.Vessel={}))})(this,function(l){"use strict";var $=Object.defineProperty;var A=(l,a,d)=>a in l?$(l,a,{enumerable:!0,configurable:!0,writable:!0,value:d}):l[a]=d;var o=(l,a,d)=>(A(l,typeof a!="symbol"?a+"":a,d),d);const a=new Map;function d(s,e,t){const n=a.get(s);n&&(n.timeout&&clearTimeout(n.timeout),n.reject(new Error("[Vessel] Same action fired again. Keeping only latest.")),a.delete(s)),a.set(s,e),typeof t=="number"&&t>0&&(e.timeout=setTimeout(()=>{u(s),e.reject(new Error("[Vessel] Action timed out."))},t))}function u(s){const e=a.get(s);e&&(e.timeout&&clearTimeout(e.timeout),a.delete(s))}function w(s){return a.get(s)}function R(s){return`${s?`[VESSEL-${s.toUpperCase()}]:`:"[VESSEL]:"}`.trimStart()}function v(s="log",e){const t=R(e==null?void 0:e.scope);return(...n)=>{e!=null&&e.debug&&console[s](t,...n)}}function m(s){return{info:v("info",s),warn:v("warn",s),error:v("error",s)}}class f extends Error{constructor(e,t){super(`${R(t)} ${e}`),this.name="VesselError"}}let E=(s=21)=>crypto.getRandomValues(new Uint8Array(s)).reduce((e,t)=>(t&=63,t<36?e+=t.toString(36):t<62?e+=(t-26).toString(36).toUpperCase():t>62?e+="-":e+="_",e),"");const y="application/x-opensea-vessel-v1+json";function I(){return`op-${E()}`}function C(){return`msg-${E()}`}function H(s){const e=new URL(s);return`${e.protocol}//${e.host}`}var i=(s=>(s.Handshake="handshake",s.HandshakeReply="handshake-reply",s.Message="message",s.MessageReply="message-reply",s.Close="close",s))(i||{});class c{constructor(e,t){o(this,"type");o(this,"id");o(this,"from");o(this,"operation");o(this,"payload");o(this,"metadata");this.type=e,this.id=(t==null?void 0:t.id)??C(),this.from=(t==null?void 0:t.from)??y,this.operation=(t==null?void 0:t.operation)??I(),this.payload=t==null?void 0:t.payload,this.metadata=t==null?void 0:t.metadata}toObject(){return{type:this.type,id:this.id,from:this.from,operation:this.operation,payload:this.payload,metadata:this.metadata}}}function S(s,e=y){if(s&&typeof s=="object"&&"from"in s&&s.from===e&&"type"in s&&Object.values(i).includes(s.type)&&"id"in s&&"operation"in s)return new c(s.type,{id:s.id,from:s.from,operation:s.operation,payload:s.payload,metadata:s.metadata});throw new f("Invalid message.")}function O(s,e,t){if(e==="*"||s.origin===e)return S(s.data,t??y);throw new f("Invalid message event origin.")}class k{constructor(e){o(this,"log");o(this,"parent",window.parent);o(this,"subscriptions");o(this,"closed",!1);o(this,"parentOrigin");this.parentOrigin=e.parentOrigin,this.subscriptions={[i.Message]:[],[i.Close]:[]},this.log=m({debug:e.debug,scope:"CHILD"}),this.onMessage=this.onMessage.bind(this),window.addEventListener("message",this.onMessage,!1)}callSubscriptions(e,t){const n=this.sendMessage.bind(this);if(e===i.Close){this.close(),this.subscriptions[e].forEach(r=>r(t));return}this.subscriptions[e].forEach(r=>{r(t,(h,p)=>{n(new c(i.MessageReply,{operation:t.operation,payload:h,metadata:p}))})})}onMessage(e){try{const t=O(e,this.parentOrigin);switch(t.type){case i.Handshake:this.sendHandshakeReply(t);return;case i.Message:this.callSubscriptions(i.Message,t);return;case i.MessageReply:this.resolveReply(t);return}}catch(t){this.log.error("Error parsing message from parent",t)}}sendHandshakeReply(e){this.log.info(`Received handshake from ${this.parentOrigin}`);const t=new c(i.HandshakeReply,{operation:e.operation});try{this.parent.postMessage(t.toObject(),this.parentOrigin)}catch(n){this.log.error("Error sending handshake reply",n)}}resolveReply(e){var t;this.log.info(`Received reply for action ${e.operation}`);try{const n=w(e.operation);if(n){this.log.info(`Resolving action ${e.operation}`);const{resolve:r,reject:h}=n;if(u(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}r(e.payload)}}catch(n){this.log.error("Error resolving reply",n)}}sendMessage(e){if(!this.parent)throw new f("Parent frame not found.");if(this.closed)throw new f("Cannot send message, frame is closed.");this.parent.postMessage(e.toObject(),this.parentOrigin)}async send(e,t,n=5e3){return new Promise((r,h)=>{const p=new c(i.Message,{payload:e,metadata:t});d(p.operation,{resolve:r,reject:h},n);try{this.sendMessage(p)}catch(g){this.log.error("Error sending message to parent",g),h(g)}})}emit(e,t){const n=new c(i.Message,{payload:e,metadata:t});this.sendMessage(n)}on(e,t){return this.subscriptions[e].push(t),()=>{this.off.call(this,e,t)}}once(e,t){const n=this.on(e,(...r)=>{n(),t(...r)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){this.closed=!0,window.removeEventListener("message",this.onMessage);const e=new c(i.Close);this.parent.postMessage(e.toObject(),this.parentOrigin)}}class M{constructor(e){o(this,"parentFrame");o(this,"child");o(this,"frame");o(this,"handshakeOperation");o(this,"handshakeInterval");o(this,"handshakeActionResolverId","HANDSHAKE");o(this,"subscriptions");o(this,"log");o(this,"childOrigin");o(this,"childUrl");o(this,"connected");this.frame=e.frame,this.parentFrame=window,this.child=null,this.childUrl=e.url,this.childOrigin=H(e.url),this.connected=!1,this.handshakeOperation=null,this.handshakeInterval=null,this.subscriptions={[i.Message]:[],[i.Close]:[]},this.log=m({debug:e.debug,scope:"PARENT"}),this.parentFrame.addEventListener("message",this.onMessage.bind(this),!1)}startHandshake(){if(this.log.info(`Sending handshake to ${this.childUrl}`),this.child=this.frame.contentWindow,!this.child)throw new Error("Child frame not found");let e=0;const t=()=>{if(this.log.info("Attempting handshake..."),this.child){e+=1;const n=new c(i.Handshake);this.handshakeOperation=n.operation,this.sendMessage(n)}};t(),this.handshakeInterval=window.setInterval(()=>{if(e>=5){this.log.warn("Handshake failed after 5 attempts. Giving up."),this.handshakeInterval&&window.clearInterval(this.handshakeInterval);return}t()},500)}callSubscriptions(e,t){const n=this.sendMessage.bind(this);if(e===i.Close){this.close(),this.subscriptions[e].forEach(r=>r(t));return}this.subscriptions[e].forEach(r=>{r(t,(h,p)=>{n(new c(i.MessageReply,{operation:t.operation,payload:h,metadata:p}))})})}handleHandshakeReply(e){var h;if(this.log.info(`Received handshake reply from ${this.childUrl}`),e.operation!==this.handshakeOperation)throw new Error("Handshake reply operation does not match");this.handshakeInterval&&window.clearInterval(this.handshakeInterval),this.connected=!0;const t=w(this.handshakeActionResolverId);if(!t)throw new f("Handshake resolver not found");this.log.info(`Resolving action ${this.handshakeActionResolverId}`);const{resolve:n,reject:r}=t;if(u(this.handshakeActionResolverId),(h=e.payload)!=null&&h.error){r(e.payload.error);return}n(e.payload)}onMessage(e){try{const t=O(e,this.childOrigin);switch(t.type){case i.HandshakeReply:this.handleHandshakeReply(t);break;case i.Message:this.callSubscriptions(i.Message,t);break;case i.MessageReply:this.resolveReply(t);break;case i.Close:this.callSubscriptions(i.Close,t);break;default:this.log.warn(`Unknown message type: ${t.type}`)}}catch(t){this.log.error("Error handling message",t)}}resolveReply(e){var t;this.log.info(`Received reply for action ${e.operation}`);try{const n=w(e.operation);if(n){this.log.info(`Resolving action ${e.operation}`);const{resolve:r,reject:h}=n;if(u(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}r(e.payload)}}catch(n){this.log.error("Error resolving reply",n)}}sendMessage(e){if(!this.child)throw new f("Child frame not found.");if(e.type!==i.Handshake&&!this.connected)throw new f("Not connected to child frame.");this.child.postMessage(e.toObject(),this.childOrigin)}async send(e,t,n=5e3){return new Promise((r,h)=>{const p=new c(i.Message,{payload:e,metadata:t});d(p.operation,{resolve:r,reject:h},n);try{this.sendMessage(p)}catch(g){this.log.error("Error sending message to child",g),h(g)}})}emit(e,t){const n=new c(i.Message,{payload:e,metadata:t});this.sendMessage(n)}connect(){return new Promise((e,t)=>{var r;this.log.info(`Connecting to child iframe ${this.childUrl}`),this.child&&["loaded","interactive","complete"].includes((r=this.child)==null?void 0:r.document.readyState)?this.startHandshake():this.frame.addEventListener("load",this.startHandshake.bind(this)),d(this.handshakeActionResolverId,{resolve:e,reject:t})})}on(e,t){return this.subscriptions[e].push(t),()=>{this.off.call(this,e,t)}}once(e,t){const n=this.on(e,(...r)=>{n(),t(...r)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){this.log.info("Closing frames"),this.subscriptions[i.Message]=[],this.subscriptions[i.Close]=[],this.handshakeOperation=null,this.handshakeInterval&&window.clearInterval(this.handshakeInterval),u(this.handshakeActionResolverId),this.connected=!1}}class b{static createParentFrame(e){return new M(e)}static createChildFrame(e){return new k(e)}}o(b,"ParentFrame",M),o(b,"ChildFrame",k),l.ChildFrame=k,l.MessageType=i,l.ParentFrame=M,l.Vessel=b,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
(function(l,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(l=typeof globalThis<"u"?globalThis:l||self,a(l.Vessel={}))})(this,function(l){"use strict";var $=Object.defineProperty;var A=(l,a,d)=>a in l?$(l,a,{enumerable:!0,configurable:!0,writable:!0,value:d}):l[a]=d;var o=(l,a,d)=>(A(l,typeof a!="symbol"?a+"":a,d),d);const a=new Map;function d(s,e,t){const n=a.get(s);n&&(n.timeout&&clearTimeout(n.timeout),n.reject(new Error("[Vessel] Same action fired again. Keeping only latest.")),a.delete(s)),a.set(s,e),typeof t=="number"&&t>0&&(e.timeout=setTimeout(()=>{u(s),e.reject(new Error("[Vessel] Action timed out."))},t))}function u(s){const e=a.get(s);e&&(e.timeout&&clearTimeout(e.timeout),a.delete(s))}function w(s){return a.get(s)}function R(s){return`${s?`[VESSEL-${s.toUpperCase()}]:`:"[VESSEL]:"}`.trimStart()}function v(s="log",e){const t=R(e==null?void 0:e.scope);return(...n)=>{e!=null&&e.debug&&console[s](t,...n)}}function m(s){return{info:v("info",s),warn:v("warn",s),error:v("error",s)}}class f extends Error{constructor(e,t){super(`${R(t)} ${e}`),this.name="VesselError"}}let E=(s=21)=>crypto.getRandomValues(new Uint8Array(s)).reduce((e,t)=>(t&=63,t<36?e+=t.toString(36):t<62?e+=(t-26).toString(36).toUpperCase():t>62?e+="-":e+="_",e),"");const y="application/x-opensea-vessel-v1+json";function I(){return`op-${E()}`}function C(){return`msg-${E()}`}function H(s){const e=new URL(s);return`${e.protocol}//${e.host}`}var i=(s=>(s.Handshake="handshake",s.HandshakeReply="handshake-reply",s.Message="message",s.MessageReply="message-reply",s.Close="close",s))(i||{});class c{constructor(e,t){o(this,"type");o(this,"id");o(this,"from");o(this,"operation");o(this,"payload");o(this,"metadata");this.type=e,this.id=(t==null?void 0:t.id)??C(),this.from=(t==null?void 0:t.from)??y,this.operation=(t==null?void 0:t.operation)??I(),this.payload=t==null?void 0:t.payload,this.metadata=t==null?void 0:t.metadata}toObject(){return{type:this.type,id:this.id,from:this.from,operation:this.operation,payload:this.payload,metadata:this.metadata}}}function S(s,e=y){if(s&&typeof s=="object"&&"from"in s&&s.from===e&&"type"in s&&Object.values(i).includes(s.type)&&"id"in s&&"operation"in s)return new c(s.type,{id:s.id,from:s.from,operation:s.operation,payload:s.payload,metadata:s.metadata});throw new f("Invalid message.")}function O(s,e,t){if(e==="*"||s.origin===e)return S(s.data,t??y);throw new f("Invalid message event origin.")}class k{constructor(e){o(this,"log");o(this,"parent",window.parent);o(this,"subscriptions");o(this,"closed",!1);o(this,"parentOrigin");this.parentOrigin=e.parentOrigin,this.subscriptions={[i.Message]:[],[i.Close]:[]},this.log=m({debug:e.debug,scope:"CHILD"}),this.onMessage=this.onMessage.bind(this),window.addEventListener("message",this.onMessage,!1)}callSubscriptions(e,t){const n=this.sendMessage.bind(this);if(e===i.Close){this.close(),this.subscriptions[e].forEach(r=>r(t));return}this.subscriptions[e].forEach(r=>{r(t,(h,p)=>{n(new c(i.MessageReply,{operation:t.operation,payload:h,metadata:p}))})})}onMessage(e){try{const t=O(e,this.parentOrigin);switch(t.type){case i.Handshake:this.sendHandshakeReply(t);return;case i.Message:this.callSubscriptions(i.Message,t);return;case i.MessageReply:this.resolveReply(t);return}}catch(t){this.log.error("Error parsing message from parent",t)}}sendHandshakeReply(e){this.log.info(`Received handshake from ${this.parentOrigin}`);const t=new c(i.HandshakeReply,{operation:e.operation});try{this.parent.postMessage(t.toObject(),this.parentOrigin)}catch(n){this.log.error("Error sending handshake reply",n)}}resolveReply(e){var t;this.log.info(`Received reply for action ${e.operation}`);try{const n=w(e.operation);if(n){this.log.info(`Resolving action ${e.operation}`);const{resolve:r,reject:h}=n;if(u(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}r(e.payload)}}catch(n){this.log.error("Error resolving reply",n)}}sendMessage(e){if(!this.parent)throw new f("Parent frame not found.");if(this.closed)throw new f("Cannot send message, frame is closed.");this.parent.postMessage(e.toObject(),this.parentOrigin)}async send(e,t,n=5e3){return new Promise((r,h)=>{const p=new c(i.Message,{payload:e,metadata:t});d(p.operation,{resolve:r,reject:h},n);try{this.sendMessage(p)}catch(g){this.log.error("Error sending message to parent",g),h(g)}})}emit(e,t){const n=new c(i.Message,{payload:e,metadata:t});this.sendMessage(n)}on(e,t){return this.subscriptions[e].push(t),()=>{this.off.call(this,e,t)}}once(e,t){const n=this.on(e,(...r)=>{n(),t(...r)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){this.closed=!0,window.removeEventListener("message",this.onMessage);const e=new c(i.Close);this.parent.postMessage(e.toObject(),this.parentOrigin)}}class M{constructor(e){o(this,"parentFrame");o(this,"child");o(this,"frame");o(this,"handshakeOperation");o(this,"handshakeInterval");o(this,"handshakeActionResolverId","HANDSHAKE");o(this,"subscriptions");o(this,"log");o(this,"childOrigin");o(this,"childUrl");o(this,"connected");this.frame=e.frame,this.parentFrame=window,this.child=null,this.childUrl=e.url,this.childOrigin=H(e.url),this.connected=!1,this.handshakeOperation=null,this.handshakeInterval=null,this.subscriptions={[i.Message]:[],[i.Close]:[]},this.log=m({debug:e.debug,scope:"PARENT"}),this.parentFrame.addEventListener("message",this.onMessage.bind(this),!1)}startHandshake(){if(this.log.info(`Sending handshake to ${this.childUrl}`),this.child=this.frame.contentWindow,!this.child)throw new Error("Child frame not found");let e=0;const t=()=>{if(this.log.info("Attempting handshake..."),this.child){e+=1;const n=new c(i.Handshake);this.handshakeOperation=n.operation,this.sendMessage(n)}};t(),this.handshakeInterval=window.setInterval(()=>{if(e>=5){this.log.warn("Handshake failed after 5 attempts. Giving up."),this.handshakeInterval&&window.clearInterval(this.handshakeInterval);return}t()},500)}callSubscriptions(e,t){const n=this.sendMessage.bind(this);if(e===i.Close){this.close(),this.subscriptions[e].forEach(r=>r(t));return}this.subscriptions[e].forEach(r=>{r(t,(h,p)=>{n(new c(i.MessageReply,{operation:t.operation,payload:h,metadata:p}))})})}handleHandshakeReply(e){var h;if(this.log.info(`Received handshake reply from ${this.childUrl}`),e.operation!==this.handshakeOperation)throw new Error("Handshake reply operation does not match");this.handshakeInterval&&window.clearInterval(this.handshakeInterval),this.connected=!0;const t=w(this.handshakeActionResolverId);if(!t)throw new f("Handshake resolver not found");this.log.info(`Resolving action ${this.handshakeActionResolverId}`);const{resolve:n,reject:r}=t;if(u(this.handshakeActionResolverId),(h=e.payload)!=null&&h.error){r(e.payload.error);return}n(e.payload)}onMessage(e){try{const t=O(e,this.childOrigin);switch(t.type){case i.HandshakeReply:this.handleHandshakeReply(t);break;case i.Message:this.callSubscriptions(i.Message,t);break;case i.MessageReply:this.resolveReply(t);break;case i.Close:this.callSubscriptions(i.Close,t);break;default:this.log.warn(`Unknown message type: ${t.type}`)}}catch(t){this.log.error("Error handling message",t)}}resolveReply(e){var t;this.log.info(`Received reply for action ${e.operation}`);try{const n=w(e.operation);if(n){this.log.info(`Resolving action ${e.operation}`);const{resolve:r,reject:h}=n;if(u(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}r(e.payload)}}catch(n){this.log.error("Error resolving reply",n)}}sendMessage(e){if(!this.child)throw new f("Child frame not found.");if(e.type!==i.Handshake&&!this.connected)throw new f("Not connected to child frame.");this.child.postMessage(e.toObject(),this.childOrigin)}async send(e,t,n=5e3){return new Promise((r,h)=>{const p=new c(i.Message,{payload:e,metadata:t});d(p.operation,{resolve:r,reject:h},n);try{this.sendMessage(p)}catch(g){this.log.error("Error sending message to child",g),h(g)}})}emit(e,t){const n=new c(i.Message,{payload:e,metadata:t});this.sendMessage(n)}connect({isFrameLoaded:e=!1}){return new Promise((t,n)=>{this.log.info(`Connecting to child iframe ${this.childUrl}`),e?this.startHandshake():this.frame.addEventListener("load",this.startHandshake.bind(this)),d(this.handshakeActionResolverId,{resolve:t,reject:n})})}on(e,t){return this.subscriptions[e].push(t),()=>{this.off.call(this,e,t)}}once(e,t){const n=this.on(e,(...r)=>{n(),t(...r)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){this.log.info("Closing frames"),this.subscriptions[i.Message]=[],this.subscriptions[i.Close]=[],this.handshakeOperation=null,this.handshakeInterval&&window.clearInterval(this.handshakeInterval),u(this.handshakeActionResolverId),this.connected=!1}}class b{static createParentFrame(e){return new M(e)}static createChildFrame(e){return new k(e)}}o(b,"ParentFrame",M),o(b,"ChildFrame",k),l.ChildFrame=k,l.MessageType=i,l.ParentFrame=M,l.Vessel=b,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});

@@ -9,2 +9,5 @@ import { Message, MessageMetadataType, MessagePayloadType, MessageType } from "./utils/messaging";

export type CloseSubscriptionFn = (message: Message<any, any>) => void;
type ConnectOptions = {
isFrameLoaded?: boolean;
};
export declare class ParentFrame {

@@ -44,3 +47,3 @@ private parentFrame;

emit<P extends MessagePayloadType, M extends MessageMetadataType>(payload: P, metadata?: M): void;
connect(): Promise<void>;
connect({ isFrameLoaded }: ConnectOptions): Promise<void>;
on(type: MessageType.Message, fn: ActionSubscriptionFn): () => void;

@@ -47,0 +50,0 @@ once(type: MessageType.Message, fn: ActionSubscriptionFn): void;

{
"name": "@opensea/vessel",
"version": "0.0.13",
"version": "0.0.14",
"description": "🚢 Vessel: a promise-based postMessage library that sails your data smoothly across the Opensea.",

@@ -5,0 +5,0 @@ "files": [

@@ -29,2 +29,6 @@ import {

type ConnectOptions = {
isFrameLoaded?: boolean
}
export class ParentFrame {

@@ -283,13 +287,7 @@ private parentFrame: Window

connect() {
connect({ isFrameLoaded = false }: ConnectOptions) {
return new Promise<void>((resolve, reject) => {
this.log.info(`Connecting to child iframe ${this.childUrl}`)
const isChildFrameLoaded =
this.child &&
["loaded", "interactive", "complete"].includes(
this.child?.document.readyState,
)
if (isChildFrameLoaded) {
if (isFrameLoaded) {
this.startHandshake()

@@ -296,0 +294,0 @@ } else {

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