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.10 to 0.0.11

104

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 i = (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 o = (s, e, t) => (I(s, typeof e != "symbol" ? e + "" : e, t), t);
const d = /* @__PURE__ */ new Map();

@@ -56,8 +56,8 @@ function g(s, e, t) {

constructor(e, t) {
i(this, "type");
i(this, "id");
i(this, "from");
i(this, "operation");
i(this, "payload");
i(this, "metadata");
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) ?? 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;

@@ -87,3 +87,3 @@ }

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

@@ -95,7 +95,7 @@ return A(s.data, t ?? v);

constructor(e) {
i(this, "log");
i(this, "parent", window.parent);
i(this, "subscriptions");
i(this, "closed", !1);
i(this, "parentOrigin");
o(this, "log");
o(this, "parent", window.parent);
o(this, "subscriptions");
o(this, "closed", !1);
o(this, "parentOrigin");
this.parentOrigin = e.parentOrigin, this.subscriptions = {

@@ -109,7 +109,7 @@ [r.Message]: [],

if (e === r.Close) {
this.close(), this.subscriptions[e].forEach((o) => o(t));
this.close(), this.subscriptions[e].forEach((i) => i(t));
return;
}
this.subscriptions[e].forEach((o) => {
o(t, (a, l) => {
this.subscriptions[e].forEach((i) => {
i(t, (a, l) => {
n(

@@ -127,3 +127,3 @@ new h(r.MessageReply, {

try {
const t = m(e, this.parentOrigin);
const t = O(e, this.parentOrigin);
switch (t.type) {

@@ -162,3 +162,3 @@ case r.Handshake:

this.log.info(`Resolving action ${e.operation}`);
const { resolve: o, reject: a } = n;
const { resolve: i, reject: a } = n;
if (f(e.operation), (t = e.payload) != null && t.error) {

@@ -168,3 +168,3 @@ a(e.payload.error);

}
o(e.payload);
i(e.payload);
}

@@ -191,3 +191,3 @@ } catch (n) {

async send(e, t, n = 5e3) {
return new Promise((o, a) => {
return new Promise((i, a) => {
const l = new h(r.Message, {

@@ -197,3 +197,3 @@ payload: e,

});
g(l.operation, { resolve: o, reject: a }, n);
g(l.operation, { resolve: i, reject: a }, n);
try {

@@ -224,4 +224,4 @@ this.sendMessage(l);

once(e, t) {
const n = this.on(e, (...o) => {
n(), t(...o);
const n = this.on(e, (...i) => {
n(), t(...i);
});

@@ -240,13 +240,13 @@ }

constructor(e) {
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");
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 = $(e.url), this.connected = !1, this.handshakeOperation = null, this.handshakeInterval = null, this.subscriptions = {

@@ -283,7 +283,7 @@ [r.Message]: [],

if (e === r.Close) {
this.close(), this.subscriptions[e].forEach((o) => o(t));
this.close(), this.subscriptions[e].forEach((i) => i(t));
return;
}
this.subscriptions[e].forEach((o) => {
o(t, (a, l) => {
this.subscriptions[e].forEach((i) => {
i(t, (a, l) => {
n(

@@ -308,5 +308,5 @@ new h(r.MessageReply, {

this.log.info(`Resolving action ${this.handshakeActionResolverId}`);
const { resolve: n, reject: o } = t;
const { resolve: n, reject: i } = t;
if (f(this.handshakeActionResolverId), (a = e.payload) != null && a.error) {
o(e.payload.error);
i(e.payload.error);
return;

@@ -318,3 +318,3 @@ }

try {
const t = m(e, this.childOrigin);
const t = O(e, this.childOrigin);
switch (t.type) {

@@ -347,3 +347,3 @@ case r.HandshakeReply:

this.log.info(`Resolving action ${e.operation}`);
const { resolve: o, reject: a } = n;
const { resolve: i, reject: a } = n;
if (f(e.operation), (t = e.payload) != null && t.error) {

@@ -353,3 +353,3 @@ a(e.payload.error);

}
o(e.payload);
i(e.payload);
}

@@ -376,3 +376,3 @@ } catch (n) {

async send(e, t, n = 5e3) {
return new Promise((o, a) => {
return new Promise((i, a) => {
const l = new h(r.Message, {

@@ -382,3 +382,3 @@ payload: e,

});
g(l.operation, { resolve: o, reject: a }, n);
g(l.operation, { resolve: i, reject: a }, n);
try {

@@ -405,3 +405,3 @@ this.sendMessage(l);

return new Promise((e, t) => {
this.log.info(`Connecting to child iframe ${this.childUrl}`), this.frame.addEventListener("load", this.startHandshake.bind(this)), this.frame.src = this.childUrl, g(this.handshakeActionResolverId, { resolve: e, reject: t });
this.log.info(`Connecting to child iframe ${this.childUrl}`), this.frame.addEventListener("load", this.startHandshake.bind(this)), g(this.handshakeActionResolverId, { resolve: e, reject: t });
});

@@ -415,4 +415,4 @@ }

once(e, t) {
const n = this.on(e, (...o) => {
n(), t(...o);
const n = this.on(e, (...i) => {
n(), t(...i);
});

@@ -424,4 +424,3 @@ }

close() {
var e;
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, (e = this.frame.parentElement) == null || e.removeChild(this.frame);
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;
}

@@ -437,8 +436,7 @@ }

}
i(M, "ParentFrame", k), i(M, "ChildFrame", y);
o(M, "ParentFrame", k), o(M, "ChildFrame", y);
export {
y as ChildFrame,
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 r=(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){r(this,"type");r(this,"id");r(this,"from");r(this,"operation");r(this,"payload");r(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){r(this,"log");r(this,"parent",window.parent);r(this,"subscriptions");r(this,"closed",!1);r(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(o=>o(t));return}this.subscriptions[e].forEach(o=>{o(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:o,reject:h}=n;if(u(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}o(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((o,h)=>{const p=new c(i.Message,{payload:e,metadata:t});d(p.operation,{resolve:o,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,(...o)=>{n(),t(...o)})}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){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");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(o=>o(t));return}this.subscriptions[e].forEach(o=>{o(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:o}=t;if(u(this.handshakeActionResolverId),(h=e.payload)!=null&&h.error){o(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:o,reject:h}=n;if(u(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}o(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((o,h)=>{const p=new c(i.Message,{payload:e,metadata:t});d(p.operation,{resolve:o,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)=>{this.log.info(`Connecting to child iframe ${this.childUrl}`),this.frame.addEventListener("load",this.startHandshake.bind(this)),this.frame.src=this.childUrl,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,(...o)=>{n(),t(...o)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){var e;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,(e=this.frame.parentElement)==null||e.removeChild(this.frame)}}class b{static createParentFrame(e){return new M(e)}static createChildFrame(e){return new k(e)}}r(b,"ParentFrame",M),r(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(){return new Promise((e,t)=>{this.log.info(`Connecting to child iframe ${this.childUrl}`),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.ParentFrame=M,l.Vessel=b,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
import { ChildFrame } from "./child";
import { ActionSubscriptionFn, ParentFrame } from "./parent";
import { MessageType, MessagePayloadType } from "./utils/messaging";
import type { MessageType, Message, MessagePayloadType, MessageMetadataType } from "./utils/messaging";
export declare class Vessel {

@@ -11,2 +11,2 @@ static ParentFrame: typeof ParentFrame;

export { ChildFrame, ParentFrame, MessageType };
export type { ActionSubscriptionFn, MessagePayloadType };
export type { ActionSubscriptionFn, Message, MessagePayloadType, MessageMetadataType, };
{
"name": "@opensea/vessel",
"version": "0.0.10",
"version": "0.0.11",
"description": "🚢 Vessel: a promise-based postMessage library that sails your data smoothly across the Opensea.",

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

import { ChildFrame } from "./child"
import { ActionSubscriptionFn, ParentFrame } from "./parent"
import { MessageType, MessagePayloadType } from "./utils/messaging"
import type {
MessageType,
Message,
MessagePayloadType,
MessageMetadataType,
} from "./utils/messaging"

@@ -21,2 +26,7 @@ export class Vessel {

export type { ActionSubscriptionFn, MessagePayloadType }
export type {
ActionSubscriptionFn,
Message,
MessagePayloadType,
MessageMetadataType,
}

@@ -287,3 +287,2 @@ import {

this.frame.addEventListener("load", this.startHandshake.bind(this))
this.frame.src = this.childUrl

@@ -326,5 +325,3 @@ addActionResolver(this.handshakeActionResolverId, { resolve, reject })

this.connected = false
this.frame.parentElement?.removeChild(this.frame)
}
}
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