niimbot-canvas-sdk 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var be=Object.defineProperty;var Ae=(e,n,t)=>n in e?be(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t;var g=(e,n,t)=>(Ae(e,typeof n!="symbol"?n+"":n,t),t),X=(e,n,t)=>{if(!n.has(e))throw TypeError("Cannot "+t)};var h=(e,n,t)=>(X(e,n,"read from private field"),t?t.call(e):n.get(e)),A=(e,n,t)=>{if(n.has(e))throw TypeError("Cannot add the same private member more than once");n instanceof WeakSet?n.add(e):n.set(e,t)},_=(e,n,t,s)=>(X(e,n,"write to private field"),s?s.call(e,t):n.set(e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var Ee=class extends Error{constructor(n,t){super(t);g(this,"code");this.name="PenpalError",this.code=n}},P=Ee,we=e=>({name:e.name,message:e.message,stack:e.stack,penpalCode:e instanceof P?e.code:void 0}),Ie=({name:e,message:n,stack:t,penpalCode:s})=>{const r=s?new P(s,n):new Error(n);return r.name=e,r.stack=t,r},Me=Symbol("Reply"),G,re,Pe=(re=class{constructor(e,n){g(this,"value");g(this,"transferables");A(this,G,Me);this.value=e,this.transferables=n==null?void 0:n.transferables}},G=new WeakMap,re),Ce=Pe,M="penpal",W=e=>typeof e=="object"&&e!==null,ce=e=>typeof e=="function",_e=e=>W(e)&&e.namespace===M,z=e=>e.type==="SYN",Z=e=>e.type==="ACK1",B=e=>e.type==="ACK2",de=e=>e.type==="CALL",he=e=>e.type==="REPLY",Se=e=>e.type==="DESTROY",ue=(e,n=[])=>{const t=[];for(const s of Object.keys(e)){const r=e[s];ce(r)?t.push([...n,s]):W(r)&&t.push(...ue(r,[...n,s]))}return t},Oe=(e,n)=>{const t=e.reduce((s,r)=>W(s)?s[r]:void 0,n);return ce(t)?t:void 0},N=e=>e.join("."),ee=(e,n,t)=>({namespace:M,channel:e,type:"REPLY",callId:n,isError:!0,...t instanceof Error?{value:we(t),isSerializedErrorInstance:!0}:{value:t}}),Re=(e,n,t,s)=>{let r=!1;const p=async u=>{if(r||!de(u))return;s==null||s(`Received ${N(u.methodPath)}() call`,u);const{methodPath:l,args:c,id:a}=u;let i,o;try{const f=Oe(l,n);if(!f)throw new P("METHOD_NOT_FOUND",`Method \`${N(l)}\` is not found.`);let v=await f(...c);v instanceof Ce&&(o=v.transferables,v=await v.value),i={namespace:M,channel:t,type:"REPLY",callId:a,value:v}}catch(f){i=ee(t,a,f)}if(!r)try{s==null||s(`Sending ${N(l)}() reply`,i),e.sendMessage(i,o)}catch(f){throw f.name==="DataCloneError"&&(i=ee(t,a,f),s==null||s(`Sending ${N(l)}() reply`,i),e.sendMessage(i)),f}};return e.addMessageHandler(p),()=>{r=!0,e.removeMessageHandler(p)}},Ne=Re,ie,le=((ie=crypto.randomUUID)==null?void 0:ie.bind(crypto))??(()=>new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")),Te=Symbol("CallOptions"),J,ae,De=(ae=class{constructor(e){g(this,"transferables");g(this,"timeout");A(this,J,Te);this.transferables=e==null?void 0:e.transferables,this.timeout=e==null?void 0:e.timeout}},J=new WeakMap,ae),ke=De,Le=new Set(["apply","call","bind"]),fe=(e,n,t=[])=>new Proxy(t.length?()=>{}:Object.create(null),{get(s,r){if(r!=="then")return t.length&&Le.has(r)?Reflect.get(s,r):fe(e,n,[...t,r])},apply(s,r,p){return e(t,p)}}),te=e=>new P("CONNECTION_DESTROYED",`Method call ${N(e)}() failed due to destroyed connection`),xe=(e,n,t)=>{let s=!1;const r=new Map,p=c=>{if(!he(c))return;const{callId:a,value:i,isError:o,isSerializedErrorInstance:f}=c,v=r.get(a);v&&(r.delete(a),t==null||t(`Received ${N(v.methodPath)}() call`,c),o?v.reject(f?Ie(i):i):v.resolve(i))};return e.addMessageHandler(p),{remoteProxy:fe((c,a)=>{if(s)throw te(c);const i=le(),o=a[a.length-1],f=o instanceof ke,{timeout:v,transferables:b}=f?o:{},E=f?a.slice(0,-1):a;return new Promise((d,w)=>{const C=v!==void 0?window.setTimeout(()=>{r.delete(i),w(new P("METHOD_CALL_TIMEOUT",`Method call ${N(c)}() timed out after ${v}ms`))},v):void 0;r.set(i,{methodPath:c,resolve:d,reject:w,timeoutId:C});try{const y={namespace:M,channel:n,type:"CALL",id:i,methodPath:c,args:E};t==null||t(`Sending ${N(c)}() call`,y),e.sendMessage(y,b)}catch(y){w(new P("TRANSMISSION_FAILED",y.message))}})},t),destroy:()=>{s=!0,e.removeMessageHandler(p);for(const{methodPath:c,reject:a,timeoutId:i}of r.values())clearTimeout(i),a(te(c));r.clear()}}},He=xe,Ue=()=>{let e,n;return{promise:new Promise((s,r)=>{e=s,n=r}),resolve:e,reject:n}},$e=Ue,je=class extends Error{constructor(e){super(`You've hit a bug in Penpal. Please file an issue with the following information: ${e}`)}},Y=je,Q="deprecated-penpal",qe=e=>W(e)&&"penpal"in e,Fe=e=>e.split("."),ne=e=>e.join("."),ve=e=>new Y(`Unexpected message to translate: ${JSON.stringify(e)}`),Ve=e=>{if(e.penpal==="syn")return{namespace:M,channel:void 0,type:"SYN",participantId:Q};if(e.penpal==="ack")return{namespace:M,channel:void 0,type:"ACK2"};if(e.penpal==="call")return{namespace:M,channel:void 0,type:"CALL",id:e.id,methodPath:Fe(e.methodName),args:e.args};if(e.penpal==="reply")return e.resolution==="fulfilled"?{namespace:M,channel:void 0,type:"REPLY",callId:e.id,value:e.returnValue}:{namespace:M,channel:void 0,type:"REPLY",callId:e.id,isError:!0,...e.returnValueIsError?{value:e.returnValue,isSerializedErrorInstance:!0}:{value:e.returnValue}};throw ve(e)},ze=e=>{if(Z(e))return{penpal:"synAck",methodNames:e.methodPaths.map(ne)};if(de(e))return{penpal:"call",id:e.id,methodName:ne(e.methodPath),args:e.args};if(he(e))return e.isError?{penpal:"reply",id:e.callId,resolution:"rejected",...e.isSerializedErrorInstance?{returnValue:e.value,returnValueIsError:!0}:{returnValue:e.value}}:{penpal:"reply",id:e.callId,resolution:"fulfilled",returnValue:e.value};throw ve(e)},Ye=({messenger:e,methods:n,timeout:t,channel:s,log:r})=>{const p=le();let u;const l=[];let c=!1;const a=ue(n),{promise:i,resolve:o,reject:f}=$e(),v=t!==void 0?setTimeout(()=>{f(new P("CONNECTION_TIMEOUT",`Connection timed out after ${t}ms`))},t):void 0,b=()=>{for(const m of l)m()},E=()=>{if(c)return;l.push(Ne(e,n,s,r));const{remoteProxy:m,destroy:T}=He(e,s,r);l.push(T),clearTimeout(v),c=!0,o({remoteProxy:m,destroy:b})},d=()=>{const m={namespace:M,type:"SYN",channel:s,participantId:p};r==null||r("Sending handshake SYN",m);try{e.sendMessage(m)}catch(T){f(new P("TRANSMISSION_FAILED",T.message))}},w=m=>{if(r==null||r("Received handshake SYN",m),m.participantId===u&&u!==Q||(u=m.participantId,d(),!(p>u||u===Q)))return;const V={namespace:M,channel:s,type:"ACK1",methodPaths:a};r==null||r("Sending handshake ACK1",V);try{e.sendMessage(V)}catch(me){f(new P("TRANSMISSION_FAILED",me.message));return}},C=m=>{r==null||r("Received handshake ACK1",m);const T={namespace:M,channel:s,type:"ACK2"};r==null||r("Sending handshake ACK2",T);try{e.sendMessage(T)}catch(V){f(new P("TRANSMISSION_FAILED",V.message));return}E()},y=m=>{r==null||r("Received handshake ACK2",m),E()},F=m=>{z(m)&&w(m),Z(m)&&C(m),B(m)&&y(m)};return e.addMessageHandler(F),l.push(()=>e.removeMessageHandler(F)),d(),i},Ke=Ye,We=e=>{let n=!1,t;return(...s)=>(n||(n=!0,t=e(...s)),t)},Be=We,se=new WeakSet,Qe=({messenger:e,methods:n={},timeout:t,channel:s,log:r})=>{if(!e)throw new P("INVALID_ARGUMENT","messenger must be defined");if(se.has(e))throw new P("INVALID_ARGUMENT","A messenger can only be used for a single connection");se.add(e);const p=[e.destroy],u=Be(a=>{if(a){const i={namespace:M,channel:s,type:"DESTROY"};try{e.sendMessage(i)}catch{}}for(const i of p)i();r==null||r("Connection destroyed")}),l=a=>_e(a)&&a.channel===s;return{promise:(async()=>{try{e.initialize({log:r,validateReceivedMessage:l}),e.addMessageHandler(o=>{Se(o)&&u(!1)});const{remoteProxy:a,destroy:i}=await Ke({messenger:e,methods:n,timeout:t,channel:s,log:r});return p.push(i),a}catch(a){throw u(!0),a}})(),destroy:()=>{u(!0)}}},Ge=Qe,O,D,x,H,k,R,I,L,K,U,j,q,$,oe,Je=(oe=class{constructor({remoteWindow:e,allowedOrigins:n}){A(this,O,void 0);A(this,D,void 0);A(this,x,void 0);A(this,H,void 0);A(this,k,void 0);A(this,R,new Set);A(this,I,void 0);A(this,L,!1);g(this,"initialize",({log:e,validateReceivedMessage:n})=>{_(this,x,e),_(this,H,n),window.addEventListener("message",h(this,q))});g(this,"sendMessage",(e,n)=>{if(z(e)){const t=h(this,U).call(this,e);h(this,O).postMessage(e,{targetOrigin:t,transfer:n});return}if(Z(e)||h(this,L)){const t=h(this,L)?ze(e):e,s=h(this,U).call(this,e);h(this,O).postMessage(t,{targetOrigin:s,transfer:n});return}if(B(e)){const{port1:t,port2:s}=new MessageChannel;_(this,I,t),t.addEventListener("message",h(this,$)),t.start();const r=[s,...n||[]],p=h(this,U).call(this,e);h(this,O).postMessage(e,{targetOrigin:p,transfer:r});return}if(h(this,I)){h(this,I).postMessage(e,{transfer:n});return}throw new Y("Port is undefined")});g(this,"addMessageHandler",e=>{h(this,R).add(e)});g(this,"removeMessageHandler",e=>{h(this,R).delete(e)});g(this,"destroy",()=>{window.removeEventListener("message",h(this,q)),h(this,j).call(this),h(this,R).clear()});A(this,K,e=>h(this,D).some(n=>n instanceof RegExp?n.test(e):n===e||n==="*"));A(this,U,e=>{if(z(e))return"*";if(!h(this,k))throw new Y("Concrete remote origin not set");return h(this,k)==="null"&&h(this,D).includes("*")?"*":h(this,k)});A(this,j,()=>{var e,n;(e=h(this,I))==null||e.removeEventListener("message",h(this,$)),(n=h(this,I))==null||n.close(),_(this,I,void 0)});A(this,q,({source:e,origin:n,ports:t,data:s})=>{var r,p,u;if(e===h(this,O)&&(qe(s)&&((r=h(this,x))==null||r.call(this,"Please upgrade the child window to the latest version of Penpal."),_(this,L,!0),s=Ve(s)),!!((p=h(this,H))!=null&&p.call(this,s)))){if(!h(this,K).call(this,n)){(u=h(this,x))==null||u.call(this,`Received a message from origin \`${n}\` which did not match allowed origins \`[${h(this,D).join(", ")}]\``);return}if(z(s)&&(h(this,j).call(this),_(this,k,n)),B(s)&&!h(this,L)){if(_(this,I,t[0]),!h(this,I))throw new Y("No port received on ACK2");h(this,I).addEventListener("message",h(this,$)),h(this,I).start()}for(const l of h(this,R))l(s)}});A(this,$,({data:e})=>{var n;if((n=h(this,H))!=null&&n.call(this,e))for(const t of h(this,R))t(e)});if(!e)throw new P("INVALID_ARGUMENT","remoteWindow must be defined");_(this,O,e),_(this,D,n!=null&&n.length?n:[window.origin])}},O=new WeakMap,D=new WeakMap,x=new WeakMap,H=new WeakMap,k=new WeakMap,R=new WeakMap,I=new WeakMap,L=new WeakMap,K=new WeakMap,U=new WeakMap,j=new WeakMap,q=new WeakMap,$=new WeakMap,oe),Ze=Je,Xe=e=>(...n)=>{console.log(`✍️ %c${e}%c`,"font-weight: bold;","",...n)},et=Xe;function tt(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var pe={exports:{}};(function(e){var n=Object.prototype.hasOwnProperty,t="~";function s(){}Object.create&&(s.prototype=Object.create(null),new s().__proto__||(t=!1));function r(c,a,i){this.fn=c,this.context=a,this.once=i||!1}function p(c,a,i,o,f){if(typeof i!="function")throw new TypeError("The listener must be a function");var v=new r(i,o||c,f),b=t?t+a:a;return c._events[b]?c._events[b].fn?c._events[b]=[c._events[b],v]:c._events[b].push(v):(c._events[b]=v,c._eventsCount++),c}function u(c,a){--c._eventsCount===0?c._events=new s:delete c._events[a]}function l(){this._events=new s,this._eventsCount=0}l.prototype.eventNames=function(){var a=[],i,o;if(this._eventsCount===0)return a;for(o in i=this._events)n.call(i,o)&&a.push(t?o.slice(1):o);return Object.getOwnPropertySymbols?a.concat(Object.getOwnPropertySymbols(i)):a},l.prototype.listeners=function(a){var i=t?t+a:a,o=this._events[i];if(!o)return[];if(o.fn)return[o.fn];for(var f=0,v=o.length,b=new Array(v);f<v;f++)b[f]=o[f].fn;return b},l.prototype.listenerCount=function(a){var i=t?t+a:a,o=this._events[i];return o?o.fn?1:o.length:0},l.prototype.emit=function(a,i,o,f,v,b){var E=t?t+a:a;if(!this._events[E])return!1;var d=this._events[E],w=arguments.length,C,y;if(d.fn){switch(d.once&&this.removeListener(a,d.fn,void 0,!0),w){case 1:return d.fn.call(d.context),!0;case 2:return d.fn.call(d.context,i),!0;case 3:return d.fn.call(d.context,i,o),!0;case 4:return d.fn.call(d.context,i,o,f),!0;case 5:return d.fn.call(d.context,i,o,f,v),!0;case 6:return d.fn.call(d.context,i,o,f,v,b),!0}for(y=1,C=new Array(w-1);y<w;y++)C[y-1]=arguments[y];d.fn.apply(d.context,C)}else{var F=d.length,m;for(y=0;y<F;y++)switch(d[y].once&&this.removeListener(a,d[y].fn,void 0,!0),w){case 1:d[y].fn.call(d[y].context);break;case 2:d[y].fn.call(d[y].context,i);break;case 3:d[y].fn.call(d[y].context,i,o);break;case 4:d[y].fn.call(d[y].context,i,o,f);break;default:if(!C)for(m=1,C=new Array(w-1);m<w;m++)C[m-1]=arguments[m];d[y].fn.apply(d[y].context,C)}}return!0},l.prototype.on=function(a,i,o){return p(this,a,i,o,!1)},l.prototype.once=function(a,i,o){return p(this,a,i,o,!0)},l.prototype.removeListener=function(a,i,o,f){var v=t?t+a:a;if(!this._events[v])return this;if(!i)return u(this,v),this;var b=this._events[v];if(b.fn)b.fn===i&&(!f||b.once)&&(!o||b.context===o)&&u(this,v);else{for(var E=0,d=[],w=b.length;E<w;E++)(b[E].fn!==i||f&&!b[E].once||o&&b[E].context!==o)&&d.push(b[E]);d.length?this._events[v]=d.length===1?d[0]:d:u(this,v)}return this},l.prototype.removeAllListeners=function(a){var i;return a?(i=t?t+a:a,this._events[i]&&u(this,i)):(this._events=new s,this._eventsCount=0),this},l.prototype.off=l.prototype.removeListener,l.prototype.addListener=l.prototype.on,l.prefixed=t,l.EventEmitter=l,e.exports=l})(pe);var nt=pe.exports;const st=tt(nt);class ge extends st{on(n,t,s){return super.on(n,t,s)}once(n,t,s){return super.once(n,t,s)}off(n,t,s){return super.off(n,t,s)}emit(n,...t){return super.emit(n,...t)}}class rt{constructor(){g(this,"queue",[]);g(this,"maxSize",100)}enqueue(n,t=3){this.queue.length>=this.maxSize&&this.queue.shift();const s={...n,retryCount:0,maxRetries:t};return this.queue.push(s),n.messageId}dequeue(){return this.queue.shift()}hasMessages(){return this.queue.length>0}getMessageById(n){return this.queue.find(t=>t.messageId===n)}removeMessageById(n){const t=this.queue.findIndex(s=>s.messageId===n);t!==-1&&this.queue.splice(t,1)}clear(){this.queue=[]}}class it{constructor(n,t=!1){this.context=n,this.debug=t}formatMessage(n,...t){return[`[${new Date().toISOString()}] [${this.context}] [${n}]`,...t]}info(...n){console.info(...this.formatMessage("INFO",...n))}debug(...n){this.debug&&console.debug(...this.formatMessage("DEBUG",...n))}warn(...n){console.warn(...this.formatMessage("WARN",...n))}error(...n){console.error(...this.formatMessage("ERROR",...n))}}let at=e=>crypto.getRandomValues(new Uint8Array(e)),ot=(e,n,t)=>{let s=(2<<Math.log2(e.length-1))-1,r=-~(1.6*s*n/e.length);return(p=n)=>{let u="";for(;;){let l=t(r),c=r|0;for(;c--;)if(u+=e[l[c]&s]||"",u.length>=p)return u}}},ct=(e,n=21)=>ot(e,n|0,at);const dt="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",ht=ct(dt,12);class ut extends ge{constructor(t,s={}){super();g(this,"connection",null);g(this,"iframe");g(this,"options");g(this,"messageQueue");g(this,"logger");g(this,"isConnected",!1);g(this,"reconnectAttempts",0);g(this,"maxReconnectAttempts",3);this.iframe=t,this.options={timeout:3e4,debug:!1,retryCount:3,retryDelay:1e3,childOrigin:"*",...s},this.messageQueue=new rt,this.logger=new it("PenpalBridge",this.options.debug)}async connect(t={}){try{this.logger.info("正在建立连接...");const s=new Ze({remoteWindow:this.iframe,allowedOrigins:["http://127.0.0.1:3000"]});this.connection=Ge({messenger:s,methods:{add(r,p){return r+p},emitEvent:(r,p)=>{this.emit(r,p)},...t},timeout:this.options.timeout,log:et("parent")}),await this.connection.promise,this.isConnected=!0,this.reconnectAttempts=0,await this.processQueuedMessages(),this.logger.info("连接建立成功"),this.emit("connected")}catch(s){throw this.logger.error("连接失败:",s),await this.handleReconnect(),s}}async send(t,s){var p;const r={action:t,params:s,timestamp:Date.now(),messageId:ht()};if(!this.isConnected)throw this.messageQueue.enqueue(r),new Error("Connection not ready, message queued");try{if(this.logger.debug("发送消息:",r),!((p=this.connection)!=null&&p.child))throw new Error("连接未就绪");const u=await this.connection.child.handleMessage(r);return this.logger.debug("收到响应:",u),this.validateResponse(u)}catch(u){throw this.logger.error("发送消息失败:",u),this.messageQueue.enqueue(r),u}}async processQueuedMessages(){for(;this.messageQueue.hasMessages();){const t=this.messageQueue.dequeue();if(t)try{await this.send(t.action,t.params)}catch(s){this.logger.warn("队列消息发送失败:",s),this.messageQueue.enqueue(t)}}}async handleReconnect(){if(this.reconnectAttempts>=this.maxReconnectAttempts){this.logger.error("重连次数达到上限"),this.emit("connection-lost");return}this.reconnectAttempts++,this.logger.info(`尝试重连 (${this.reconnectAttempts}/${this.maxReconnectAttempts})`),await new Promise(t=>setTimeout(t,this.options.retryDelay*this.reconnectAttempts));try{await this.connect()}catch{await this.handleReconnect()}}validateResponse(t){if(!t||t.success===void 0)throw new Error("Invalid response format");if(!t.success){const s=t.error||{code:"UNKNOWN",message:"Unknown error"};throw new Error(`${s.code}: ${s.message}`)}return t.data}disconnect(){this.connection&&(this.connection.destroy(),this.isConnected=!1,this.emit("disconnected"),this.logger.info("连接已断开"))}getConnectionStatus(){return this.isConnected}}class lt{constructor(n){this.bridge=n}async initialize(n){return this.bridge.send("canvas.initialize",n)}async setData(n){return this.bridge.send("canvas.setData",n)}async getData(){return this.bridge.send("canvas.getData")}async clear(){return this.bridge.send("canvas.clear")}async undo(){return this.bridge.send("canvas.undo")}async redo(){return this.bridge.send("canvas.redo")}async export(n){return this.bridge.send("canvas.export",{format:n})}async save(){return this.bridge.send("canvas.save")}async invoke(n,...t){return this.bridge.send("canvas.invoke",{method:n,args:t})}async setZoom(n){return this.bridge.send("canvas.setZoom",{zoom:n})}async getSnapshot(){return this.bridge.send("canvas.getSnapshot")}}class ft{constructor(n){this.bridge=n}async setAbilities(n){return this.bridge.send("abilities.set",n)}async getAbilities(){return this.bridge.send("abilities.get")}async enableAbility(n){return this.bridge.send("abilities.enable",{name:n})}async disableAbility(n){return this.bridge.send("abilities.disable",{name:n})}}class vt{constructor(n){this.bridge=n}async setTheme(n){return this.bridge.send("theme.set",{theme:n})}async getCurrentTheme(){return this.bridge.send("theme.get")}async updateToken(n){return this.bridge.send("theme.updateToken",n)}}class pt{constructor(n){this.bridge=n}async setLocale(n,t){return this.bridge.send("i18n.setLocale",{locale:n,messages:t})}async getCurrentLocale(){return this.bridge.send("i18n.getLocale")}async updateMessages(n){return this.bridge.send("i18n.updateMessages",n)}}class gt{constructor(n){this.bridge=n}async validateToken(){return this.bridge.send("auth.validateToken")}}const S={READY:"ready",CHANGE:"change",SAVE:"save",ERROR:"error",SELECTION_CHANGED:"selection-changed",CONNECTED:"connected",DISCONNECTED:"disconnected",LOADED:"loaded",BEFORE_UNLOAD:"before-unload",OBJECT_ADDED:"object-added",OBJECT_MODIFIED:"object-modified",OBJECT_REMOVED:"object-removed",MOUSE_DOWN:"mouse-down",MOUSE_UP:"mouse-up",MOUSE_MOVE:"mouse-move",KEY_DOWN:"key-down",KEY_UP:"key-up"};class ye extends ge{constructor(t,s){super();g(this,"bridge");g(this,"canvasAPI");g(this,"abilityAPI");g(this,"themeAPI");g(this,"i18nAPI");g(this,"authAPI");g(this,"config");g(this,"iframe");g(this,"isInitialized",!1);this.validateConfig(s),this.config=s,this.iframe=t,this.bridge=new ut(t,{debug:s.debug||!1,timeout:3e4,childOrigin:this.extractOrigin(t.src)}),this.canvasAPI=new lt(this.bridge),this.abilityAPI=new ft(this.bridge),this.themeAPI=new vt(this.bridge),this.i18nAPI=new pt(this.bridge),this.authAPI=new gt(this.bridge),this.setupEventHandlers()}async init(){if(!this.isInitialized)try{await this.bridge.connect({getConfig:()=>this.config,log:(t,s)=>{console.log(`[Canvas ${t.toUpperCase()}]`,s)}}),await this.canvasAPI.initialize(this.config),this.config.abilities&&await this.abilityAPI.setAbilities(this.config.abilities),this.config.theme&&await this.themeAPI.setTheme(this.config.theme),this.config.locale&&await this.i18nAPI.setLocale(this.config.locale,this.config.messages),this.isInitialized=!0,this.emit(S.READY)}catch(t){throw this.emit(S.ERROR,t),t}}get canvas(){return{setData:t=>this.canvasAPI.setData(t),getData:()=>this.canvasAPI.getData(),clear:()=>this.canvasAPI.clear(),undo:()=>this.canvasAPI.undo(),redo:()=>this.canvasAPI.redo(),export:(t="png")=>this.canvasAPI.export(t),save:()=>this.canvasAPI.save(),setZoom:t=>this.canvasAPI.setZoom(t),getSnapshot:()=>this.canvasAPI.getSnapshot(),invoke:(t,...s)=>this.canvasAPI.invoke(t,...s)}}get abilities(){return{set:t=>this.abilityAPI.setAbilities(t),get:()=>this.abilityAPI.getAbilities(),enable:t=>this.abilityAPI.enableAbility(t),disable:t=>this.abilityAPI.disableAbility(t),updateConfig:(t,s)=>this.abilityAPI.updateAbilityConfig(t,s)}}get theme(){return{set:t=>this.themeAPI.setTheme(t),get:()=>this.themeAPI.getCurrentTheme(),updateToken:t=>this.themeAPI.updateToken(t),reset:()=>this.themeAPI.resetTheme()}}get i18n(){return{setLocale:(t,s)=>this.i18nAPI.setLocale(t,s),getLocale:()=>this.i18nAPI.getCurrentLocale(),updateMessages:t=>this.i18nAPI.updateMessages(t),getMessages:()=>this.i18nAPI.getMessages(),getTranslation:t=>this.i18nAPI.getTranslation(t)}}get auth(){return{validateToken:()=>this.authAPI.validateToken(),refreshToken:t=>this.authAPI.refreshToken(t),getTokenInfo:()=>this.authAPI.getTokenInfo()}}get utils(){return{getConnectionStatus:()=>this.bridge.getConnectionStatus(),disconnect:()=>this.bridge.disconnect(),reconnect:async()=>{this.bridge.disconnect(),this.isInitialized=!1,await this.init()},isInitialized:()=>this.isInitialized}}setupEventHandlers(){this.bridge.on("canvas-change",t=>{this.emit(S.CHANGE,t)}),this.bridge.on("canvas-save",t=>{this.emit(S.SAVE,t)}),this.bridge.on("canvas-error",t=>{this.emit(S.ERROR,t)}),this.bridge.on("canvas-selection-changed",t=>{this.emit(S.SELECTION_CHANGED,t)}),this.bridge.on("connected",()=>{console.log("链接建立成功"),this.emit(S.CONNECTED)}),this.bridge.on("disconnected",()=>{console.log("链接断开"),this.emit(S.DISCONNECTED)})}validateConfig(t){if(!t.token)throw new Error("Token is required");if(t.abilities){const s=["text","image","barcode","multiLayout","shape","qrCode"];Object.keys(t.abilities).forEach(r=>{s.includes(r)||console.warn(`Unknown ability: ${r}`)})}}extractOrigin(t){try{return new URL(t).origin}catch{return"*"}}}function yt(e,n){return new ye(e,n)}exports.CANVAS_EVENTS=S;exports.NiimbotCanvasSDK=ye;exports.createCanvas=yt;
|
|
@@ -1,36 +1,683 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
var me = Object.defineProperty;
|
|
2
|
+
var be = (e, n, t) => n in e ? me(e, n, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[n] = t;
|
|
3
|
+
var g = (e, n, t) => (be(e, typeof n != "symbol" ? n + "" : n, t), t), X = (e, n, t) => {
|
|
4
|
+
if (!n.has(e))
|
|
5
|
+
throw TypeError("Cannot " + t);
|
|
6
|
+
};
|
|
7
|
+
var h = (e, n, t) => (X(e, n, "read from private field"), t ? t.call(e) : n.get(e)), A = (e, n, t) => {
|
|
8
|
+
if (n.has(e))
|
|
9
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
10
|
+
n instanceof WeakSet ? n.add(e) : n.set(e, t);
|
|
11
|
+
}, _ = (e, n, t, s) => (X(e, n, "write to private field"), s ? s.call(e, t) : n.set(e, t), t);
|
|
12
|
+
var Ae = class extends Error {
|
|
13
|
+
constructor(n, t) {
|
|
14
|
+
super(t);
|
|
15
|
+
g(this, "code");
|
|
16
|
+
this.name = "PenpalError", this.code = n;
|
|
10
17
|
}
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
}, P = Ae, Ee = (e) => ({
|
|
19
|
+
name: e.name,
|
|
20
|
+
message: e.message,
|
|
21
|
+
stack: e.stack,
|
|
22
|
+
penpalCode: e instanceof P ? e.code : void 0
|
|
23
|
+
}), we = ({
|
|
24
|
+
name: e,
|
|
25
|
+
message: n,
|
|
26
|
+
stack: t,
|
|
27
|
+
penpalCode: s
|
|
28
|
+
}) => {
|
|
29
|
+
const r = s ? new P(s, n) : new Error(n);
|
|
30
|
+
return r.name = e, r.stack = t, r;
|
|
31
|
+
}, Ie = Symbol("Reply"), G, re, Me = (re = class {
|
|
32
|
+
constructor(e, n) {
|
|
33
|
+
g(this, "value");
|
|
34
|
+
g(this, "transferables");
|
|
35
|
+
// Allows TypeScript to distinguish between an actual instance of this
|
|
36
|
+
// class versus an object that looks structurally similar.
|
|
37
|
+
// eslint-disable-next-line no-unused-private-class-members
|
|
38
|
+
A(this, G, Ie);
|
|
39
|
+
this.value = e, this.transferables = n == null ? void 0 : n.transferables;
|
|
13
40
|
}
|
|
14
|
-
|
|
15
|
-
|
|
41
|
+
}, G = new WeakMap(), re), Pe = Me, M = "penpal", W = (e) => typeof e == "object" && e !== null, ce = (e) => typeof e == "function", Ce = (e) => W(e) && e.namespace === M, V = (e) => e.type === "SYN", Z = (e) => e.type === "ACK1", B = (e) => e.type === "ACK2", de = (e) => e.type === "CALL", he = (e) => e.type === "REPLY", _e = (e) => e.type === "DESTROY", ue = (e, n = []) => {
|
|
42
|
+
const t = [];
|
|
43
|
+
for (const s of Object.keys(e)) {
|
|
44
|
+
const r = e[s];
|
|
45
|
+
ce(r) ? t.push([...n, s]) : W(r) && t.push(
|
|
46
|
+
...ue(r, [...n, s])
|
|
47
|
+
);
|
|
16
48
|
}
|
|
17
|
-
|
|
18
|
-
|
|
49
|
+
return t;
|
|
50
|
+
}, Oe = (e, n) => {
|
|
51
|
+
const t = e.reduce(
|
|
52
|
+
(s, r) => W(s) ? s[r] : void 0,
|
|
53
|
+
n
|
|
54
|
+
);
|
|
55
|
+
return ce(t) ? t : void 0;
|
|
56
|
+
}, T = (e) => e.join("."), ee = (e, n, t) => ({
|
|
57
|
+
namespace: M,
|
|
58
|
+
channel: e,
|
|
59
|
+
type: "REPLY",
|
|
60
|
+
callId: n,
|
|
61
|
+
isError: !0,
|
|
62
|
+
...t instanceof Error ? { value: Ee(t), isSerializedErrorInstance: !0 } : { value: t }
|
|
63
|
+
}), Se = (e, n, t, s) => {
|
|
64
|
+
let r = !1;
|
|
65
|
+
const v = async (u) => {
|
|
66
|
+
if (r || !de(u))
|
|
67
|
+
return;
|
|
68
|
+
s == null || s(`Received ${T(u.methodPath)}() call`, u);
|
|
69
|
+
const { methodPath: l, args: c, id: a } = u;
|
|
70
|
+
let i, o;
|
|
71
|
+
try {
|
|
72
|
+
const f = Oe(l, n);
|
|
73
|
+
if (!f)
|
|
74
|
+
throw new P(
|
|
75
|
+
"METHOD_NOT_FOUND",
|
|
76
|
+
`Method \`${T(l)}\` is not found.`
|
|
77
|
+
);
|
|
78
|
+
let p = await f(...c);
|
|
79
|
+
p instanceof Pe && (o = p.transferables, p = await p.value), i = {
|
|
80
|
+
namespace: M,
|
|
81
|
+
channel: t,
|
|
82
|
+
type: "REPLY",
|
|
83
|
+
callId: a,
|
|
84
|
+
value: p
|
|
85
|
+
};
|
|
86
|
+
} catch (f) {
|
|
87
|
+
i = ee(t, a, f);
|
|
88
|
+
}
|
|
89
|
+
if (!r)
|
|
90
|
+
try {
|
|
91
|
+
s == null || s(`Sending ${T(l)}() reply`, i), e.sendMessage(i, o);
|
|
92
|
+
} catch (f) {
|
|
93
|
+
throw f.name === "DataCloneError" && (i = ee(t, a, f), s == null || s(`Sending ${T(l)}() reply`, i), e.sendMessage(i)), f;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return e.addMessageHandler(v), () => {
|
|
97
|
+
r = !0, e.removeMessageHandler(v);
|
|
98
|
+
};
|
|
99
|
+
}, Re = Se, ie, le = ((ie = crypto.randomUUID) == null ? void 0 : ie.bind(crypto)) ?? (() => new Array(4).fill(0).map(
|
|
100
|
+
() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)
|
|
101
|
+
).join("-")), Te = Symbol("CallOptions"), J, ae, De = (ae = class {
|
|
102
|
+
constructor(e) {
|
|
103
|
+
g(this, "transferables");
|
|
104
|
+
g(this, "timeout");
|
|
105
|
+
// Allows TypeScript to distinguish between an actual instance of this
|
|
106
|
+
// class versus an object that looks structurally similar.
|
|
107
|
+
// eslint-disable-next-line no-unused-private-class-members
|
|
108
|
+
A(this, J, Te);
|
|
109
|
+
this.transferables = e == null ? void 0 : e.transferables, this.timeout = e == null ? void 0 : e.timeout;
|
|
110
|
+
}
|
|
111
|
+
}, J = new WeakMap(), ae), Ne = De, ke = /* @__PURE__ */ new Set(["apply", "call", "bind"]), fe = (e, n, t = []) => new Proxy(
|
|
112
|
+
t.length ? () => {
|
|
113
|
+
} : /* @__PURE__ */ Object.create(null),
|
|
114
|
+
{
|
|
115
|
+
get(s, r) {
|
|
116
|
+
if (r !== "then")
|
|
117
|
+
return t.length && ke.has(r) ? Reflect.get(s, r) : fe(e, n, [...t, r]);
|
|
118
|
+
},
|
|
119
|
+
apply(s, r, v) {
|
|
120
|
+
return e(t, v);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
), te = (e) => new P(
|
|
124
|
+
"CONNECTION_DESTROYED",
|
|
125
|
+
`Method call ${T(
|
|
126
|
+
e
|
|
127
|
+
)}() failed due to destroyed connection`
|
|
128
|
+
), Le = (e, n, t) => {
|
|
129
|
+
let s = !1;
|
|
130
|
+
const r = /* @__PURE__ */ new Map(), v = (c) => {
|
|
131
|
+
if (!he(c))
|
|
132
|
+
return;
|
|
133
|
+
const { callId: a, value: i, isError: o, isSerializedErrorInstance: f } = c, p = r.get(a);
|
|
134
|
+
p && (r.delete(a), t == null || t(
|
|
135
|
+
`Received ${T(p.methodPath)}() call`,
|
|
136
|
+
c
|
|
137
|
+
), o ? p.reject(
|
|
138
|
+
f ? we(i) : i
|
|
139
|
+
) : p.resolve(i));
|
|
140
|
+
};
|
|
141
|
+
return e.addMessageHandler(v), {
|
|
142
|
+
remoteProxy: fe((c, a) => {
|
|
143
|
+
if (s)
|
|
144
|
+
throw te(c);
|
|
145
|
+
const i = le(), o = a[a.length - 1], f = o instanceof Ne, { timeout: p, transferables: b } = f ? o : {}, E = f ? a.slice(0, -1) : a;
|
|
146
|
+
return new Promise((d, w) => {
|
|
147
|
+
const C = p !== void 0 ? window.setTimeout(() => {
|
|
148
|
+
r.delete(i), w(
|
|
149
|
+
new P(
|
|
150
|
+
"METHOD_CALL_TIMEOUT",
|
|
151
|
+
`Method call ${T(
|
|
152
|
+
c
|
|
153
|
+
)}() timed out after ${p}ms`
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
}, p) : void 0;
|
|
157
|
+
r.set(i, { methodPath: c, resolve: d, reject: w, timeoutId: C });
|
|
158
|
+
try {
|
|
159
|
+
const y = {
|
|
160
|
+
namespace: M,
|
|
161
|
+
channel: n,
|
|
162
|
+
type: "CALL",
|
|
163
|
+
id: i,
|
|
164
|
+
methodPath: c,
|
|
165
|
+
args: E
|
|
166
|
+
};
|
|
167
|
+
t == null || t(`Sending ${T(c)}() call`, y), e.sendMessage(y, b);
|
|
168
|
+
} catch (y) {
|
|
169
|
+
w(
|
|
170
|
+
new P("TRANSMISSION_FAILED", y.message)
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}, t),
|
|
175
|
+
destroy: () => {
|
|
176
|
+
s = !0, e.removeMessageHandler(v);
|
|
177
|
+
for (const { methodPath: c, reject: a, timeoutId: i } of r.values())
|
|
178
|
+
clearTimeout(i), a(te(c));
|
|
179
|
+
r.clear();
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}, xe = Le, He = () => {
|
|
183
|
+
let e, n;
|
|
184
|
+
return {
|
|
185
|
+
promise: new Promise((s, r) => {
|
|
186
|
+
e = s, n = r;
|
|
187
|
+
}),
|
|
188
|
+
resolve: e,
|
|
189
|
+
reject: n
|
|
190
|
+
};
|
|
191
|
+
}, Ue = He, $e = class extends Error {
|
|
192
|
+
constructor(e) {
|
|
193
|
+
super(
|
|
194
|
+
`You've hit a bug in Penpal. Please file an issue with the following information: ${e}`
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}, Y = $e, Q = "deprecated-penpal", je = (e) => W(e) && "penpal" in e, qe = (e) => e.split("."), ne = (e) => e.join("."), pe = (e) => new Y(
|
|
198
|
+
`Unexpected message to translate: ${JSON.stringify(e)}`
|
|
199
|
+
), Fe = (e) => {
|
|
200
|
+
if (e.penpal === "syn")
|
|
201
|
+
return {
|
|
202
|
+
namespace: M,
|
|
203
|
+
channel: void 0,
|
|
204
|
+
type: "SYN",
|
|
205
|
+
participantId: Q
|
|
206
|
+
};
|
|
207
|
+
if (e.penpal === "ack")
|
|
208
|
+
return {
|
|
209
|
+
namespace: M,
|
|
210
|
+
channel: void 0,
|
|
211
|
+
type: "ACK2"
|
|
212
|
+
};
|
|
213
|
+
if (e.penpal === "call")
|
|
214
|
+
return {
|
|
215
|
+
namespace: M,
|
|
216
|
+
channel: void 0,
|
|
217
|
+
type: "CALL",
|
|
218
|
+
// Actually converting the ID to a string would break communication.
|
|
219
|
+
id: e.id,
|
|
220
|
+
methodPath: qe(e.methodName),
|
|
221
|
+
args: e.args
|
|
222
|
+
};
|
|
223
|
+
if (e.penpal === "reply")
|
|
224
|
+
return e.resolution === "fulfilled" ? {
|
|
225
|
+
namespace: M,
|
|
226
|
+
channel: void 0,
|
|
227
|
+
type: "REPLY",
|
|
228
|
+
// Actually converting the ID to a string would break communication.
|
|
229
|
+
callId: e.id,
|
|
230
|
+
value: e.returnValue
|
|
231
|
+
} : {
|
|
232
|
+
namespace: M,
|
|
233
|
+
channel: void 0,
|
|
234
|
+
type: "REPLY",
|
|
235
|
+
// Actually converting the ID to a string would break communication.
|
|
236
|
+
callId: e.id,
|
|
237
|
+
isError: !0,
|
|
238
|
+
...e.returnValueIsError ? {
|
|
239
|
+
value: e.returnValue,
|
|
240
|
+
isSerializedErrorInstance: !0
|
|
241
|
+
} : {
|
|
242
|
+
value: e.returnValue
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
throw pe(e);
|
|
246
|
+
}, ze = (e) => {
|
|
247
|
+
if (Z(e))
|
|
248
|
+
return {
|
|
249
|
+
penpal: "synAck",
|
|
250
|
+
methodNames: e.methodPaths.map(ne)
|
|
251
|
+
};
|
|
252
|
+
if (de(e))
|
|
253
|
+
return {
|
|
254
|
+
penpal: "call",
|
|
255
|
+
// Actually converting the ID to a number would break communication.
|
|
256
|
+
id: e.id,
|
|
257
|
+
methodName: ne(e.methodPath),
|
|
258
|
+
args: e.args
|
|
259
|
+
};
|
|
260
|
+
if (he(e))
|
|
261
|
+
return e.isError ? {
|
|
262
|
+
penpal: "reply",
|
|
263
|
+
// Actually converting the ID to a number would break communication.
|
|
264
|
+
id: e.callId,
|
|
265
|
+
resolution: "rejected",
|
|
266
|
+
...e.isSerializedErrorInstance ? {
|
|
267
|
+
returnValue: e.value,
|
|
268
|
+
returnValueIsError: !0
|
|
269
|
+
} : { returnValue: e.value }
|
|
270
|
+
} : {
|
|
271
|
+
penpal: "reply",
|
|
272
|
+
// Actually converting the ID to a number would break communication.
|
|
273
|
+
id: e.callId,
|
|
274
|
+
resolution: "fulfilled",
|
|
275
|
+
returnValue: e.value
|
|
276
|
+
};
|
|
277
|
+
throw pe(e);
|
|
278
|
+
}, Ve = ({
|
|
279
|
+
messenger: e,
|
|
280
|
+
methods: n,
|
|
281
|
+
timeout: t,
|
|
282
|
+
channel: s,
|
|
283
|
+
log: r
|
|
284
|
+
}) => {
|
|
285
|
+
const v = le();
|
|
286
|
+
let u;
|
|
287
|
+
const l = [];
|
|
288
|
+
let c = !1;
|
|
289
|
+
const a = ue(n), { promise: i, resolve: o, reject: f } = Ue(), p = t !== void 0 ? setTimeout(() => {
|
|
290
|
+
f(
|
|
291
|
+
new P(
|
|
292
|
+
"CONNECTION_TIMEOUT",
|
|
293
|
+
`Connection timed out after ${t}ms`
|
|
294
|
+
)
|
|
295
|
+
);
|
|
296
|
+
}, t) : void 0, b = () => {
|
|
297
|
+
for (const m of l)
|
|
298
|
+
m();
|
|
299
|
+
}, E = () => {
|
|
300
|
+
if (c)
|
|
301
|
+
return;
|
|
302
|
+
l.push(Re(e, n, s, r));
|
|
303
|
+
const { remoteProxy: m, destroy: D } = xe(e, s, r);
|
|
304
|
+
l.push(D), clearTimeout(p), c = !0, o({
|
|
305
|
+
remoteProxy: m,
|
|
306
|
+
destroy: b
|
|
307
|
+
});
|
|
308
|
+
}, d = () => {
|
|
309
|
+
const m = {
|
|
310
|
+
namespace: M,
|
|
311
|
+
type: "SYN",
|
|
312
|
+
channel: s,
|
|
313
|
+
participantId: v
|
|
314
|
+
};
|
|
315
|
+
r == null || r("Sending handshake SYN", m);
|
|
316
|
+
try {
|
|
317
|
+
e.sendMessage(m);
|
|
318
|
+
} catch (D) {
|
|
319
|
+
f(new P("TRANSMISSION_FAILED", D.message));
|
|
320
|
+
}
|
|
321
|
+
}, w = (m) => {
|
|
322
|
+
if (r == null || r("Received handshake SYN", m), m.participantId === u && // TODO: Used for backward-compatibility. Remove in next major version.
|
|
323
|
+
u !== Q || (u = m.participantId, d(), !(v > u || // TODO: Used for backward-compatibility. Remove in next major version.
|
|
324
|
+
u === Q)))
|
|
325
|
+
return;
|
|
326
|
+
const z = {
|
|
327
|
+
namespace: M,
|
|
328
|
+
channel: s,
|
|
329
|
+
type: "ACK1",
|
|
330
|
+
methodPaths: a
|
|
331
|
+
};
|
|
332
|
+
r == null || r("Sending handshake ACK1", z);
|
|
333
|
+
try {
|
|
334
|
+
e.sendMessage(z);
|
|
335
|
+
} catch (ye) {
|
|
336
|
+
f(new P("TRANSMISSION_FAILED", ye.message));
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
}, C = (m) => {
|
|
340
|
+
r == null || r("Received handshake ACK1", m);
|
|
341
|
+
const D = {
|
|
342
|
+
namespace: M,
|
|
343
|
+
channel: s,
|
|
344
|
+
type: "ACK2"
|
|
345
|
+
};
|
|
346
|
+
r == null || r("Sending handshake ACK2", D);
|
|
347
|
+
try {
|
|
348
|
+
e.sendMessage(D);
|
|
349
|
+
} catch (z) {
|
|
350
|
+
f(new P("TRANSMISSION_FAILED", z.message));
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
E();
|
|
354
|
+
}, y = (m) => {
|
|
355
|
+
r == null || r("Received handshake ACK2", m), E();
|
|
356
|
+
}, F = (m) => {
|
|
357
|
+
V(m) && w(m), Z(m) && C(m), B(m) && y(m);
|
|
358
|
+
};
|
|
359
|
+
return e.addMessageHandler(F), l.push(() => e.removeMessageHandler(F)), d(), i;
|
|
360
|
+
}, Ye = Ve, Ke = (e) => {
|
|
361
|
+
let n = !1, t;
|
|
362
|
+
return (...s) => (n || (n = !0, t = e(...s)), t);
|
|
363
|
+
}, We = Ke, se = /* @__PURE__ */ new WeakSet(), Be = ({
|
|
364
|
+
messenger: e,
|
|
365
|
+
methods: n = {},
|
|
366
|
+
timeout: t,
|
|
367
|
+
channel: s,
|
|
368
|
+
log: r
|
|
369
|
+
}) => {
|
|
370
|
+
if (!e)
|
|
371
|
+
throw new P("INVALID_ARGUMENT", "messenger must be defined");
|
|
372
|
+
if (se.has(e))
|
|
373
|
+
throw new P(
|
|
374
|
+
"INVALID_ARGUMENT",
|
|
375
|
+
"A messenger can only be used for a single connection"
|
|
376
|
+
);
|
|
377
|
+
se.add(e);
|
|
378
|
+
const v = [e.destroy], u = We((a) => {
|
|
379
|
+
if (a) {
|
|
380
|
+
const i = {
|
|
381
|
+
namespace: M,
|
|
382
|
+
channel: s,
|
|
383
|
+
type: "DESTROY"
|
|
384
|
+
};
|
|
385
|
+
try {
|
|
386
|
+
e.sendMessage(i);
|
|
387
|
+
} catch {
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
for (const i of v)
|
|
391
|
+
i();
|
|
392
|
+
r == null || r("Connection destroyed");
|
|
393
|
+
}), l = (a) => Ce(a) && a.channel === s;
|
|
394
|
+
return {
|
|
395
|
+
promise: (async () => {
|
|
396
|
+
try {
|
|
397
|
+
e.initialize({ log: r, validateReceivedMessage: l }), e.addMessageHandler((o) => {
|
|
398
|
+
_e(o) && u(!1);
|
|
399
|
+
});
|
|
400
|
+
const { remoteProxy: a, destroy: i } = await Ye({
|
|
401
|
+
messenger: e,
|
|
402
|
+
methods: n,
|
|
403
|
+
timeout: t,
|
|
404
|
+
channel: s,
|
|
405
|
+
log: r
|
|
406
|
+
});
|
|
407
|
+
return v.push(i), a;
|
|
408
|
+
} catch (a) {
|
|
409
|
+
throw u(!0), a;
|
|
410
|
+
}
|
|
411
|
+
})(),
|
|
412
|
+
// Why we don't reject the connection promise when consumer calls destroy():
|
|
413
|
+
// https://github.com/Aaronius/penpal/issues/51
|
|
414
|
+
destroy: () => {
|
|
415
|
+
u(!0);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
}, Qe = Be, S, N, x, H, k, R, I, L, K, U, j, q, $, oe, Ge = (oe = class {
|
|
419
|
+
constructor({ remoteWindow: e, allowedOrigins: n }) {
|
|
420
|
+
A(this, S, void 0);
|
|
421
|
+
A(this, N, void 0);
|
|
422
|
+
A(this, x, void 0);
|
|
423
|
+
A(this, H, void 0);
|
|
424
|
+
A(this, k, void 0);
|
|
425
|
+
A(this, R, /* @__PURE__ */ new Set());
|
|
426
|
+
A(this, I, void 0);
|
|
427
|
+
// TODO: Used for backward-compatibility. Remove in next major version.
|
|
428
|
+
A(this, L, !1);
|
|
429
|
+
g(this, "initialize", ({
|
|
430
|
+
log: e,
|
|
431
|
+
validateReceivedMessage: n
|
|
432
|
+
}) => {
|
|
433
|
+
_(this, x, e), _(this, H, n), window.addEventListener("message", h(this, q));
|
|
434
|
+
});
|
|
435
|
+
g(this, "sendMessage", (e, n) => {
|
|
436
|
+
if (V(e)) {
|
|
437
|
+
const t = h(this, U).call(this, e);
|
|
438
|
+
h(this, S).postMessage(e, {
|
|
439
|
+
targetOrigin: t,
|
|
440
|
+
transfer: n
|
|
441
|
+
});
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (Z(e) || // If the child is using a previous version of Penpal, we need to
|
|
445
|
+
// downgrade the message and send it through the window rather than
|
|
446
|
+
// the port because older versions of Penpal don't use MessagePorts.
|
|
447
|
+
h(this, L)) {
|
|
448
|
+
const t = h(this, L) ? ze(e) : e, s = h(this, U).call(this, e);
|
|
449
|
+
h(this, S).postMessage(t, {
|
|
450
|
+
targetOrigin: s,
|
|
451
|
+
transfer: n
|
|
452
|
+
});
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
if (B(e)) {
|
|
456
|
+
const { port1: t, port2: s } = new MessageChannel();
|
|
457
|
+
_(this, I, t), t.addEventListener("message", h(this, $)), t.start();
|
|
458
|
+
const r = [s, ...n || []], v = h(this, U).call(this, e);
|
|
459
|
+
h(this, S).postMessage(e, {
|
|
460
|
+
targetOrigin: v,
|
|
461
|
+
transfer: r
|
|
462
|
+
});
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (h(this, I)) {
|
|
466
|
+
h(this, I).postMessage(e, {
|
|
467
|
+
transfer: n
|
|
468
|
+
});
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
throw new Y("Port is undefined");
|
|
472
|
+
});
|
|
473
|
+
g(this, "addMessageHandler", (e) => {
|
|
474
|
+
h(this, R).add(e);
|
|
475
|
+
});
|
|
476
|
+
g(this, "removeMessageHandler", (e) => {
|
|
477
|
+
h(this, R).delete(e);
|
|
478
|
+
});
|
|
479
|
+
g(this, "destroy", () => {
|
|
480
|
+
window.removeEventListener("message", h(this, q)), h(this, j).call(this), h(this, R).clear();
|
|
481
|
+
});
|
|
482
|
+
A(this, K, (e) => h(this, N).some(
|
|
483
|
+
(n) => n instanceof RegExp ? n.test(e) : n === e || n === "*"
|
|
484
|
+
));
|
|
485
|
+
A(this, U, (e) => {
|
|
486
|
+
if (V(e))
|
|
487
|
+
return "*";
|
|
488
|
+
if (!h(this, k))
|
|
489
|
+
throw new Y("Concrete remote origin not set");
|
|
490
|
+
return h(this, k) === "null" && h(this, N).includes("*") ? "*" : h(this, k);
|
|
491
|
+
});
|
|
492
|
+
A(this, j, () => {
|
|
493
|
+
var e, n;
|
|
494
|
+
(e = h(this, I)) == null || e.removeEventListener("message", h(this, $)), (n = h(this, I)) == null || n.close(), _(this, I, void 0);
|
|
495
|
+
});
|
|
496
|
+
A(this, q, ({
|
|
497
|
+
source: e,
|
|
498
|
+
origin: n,
|
|
499
|
+
ports: t,
|
|
500
|
+
data: s
|
|
501
|
+
}) => {
|
|
502
|
+
var r, v, u;
|
|
503
|
+
if (e === h(this, S) && (je(s) && ((r = h(this, x)) == null || r.call(
|
|
504
|
+
this,
|
|
505
|
+
"Please upgrade the child window to the latest version of Penpal."
|
|
506
|
+
), _(this, L, !0), s = Fe(s)), !!((v = h(this, H)) != null && v.call(this, s)))) {
|
|
507
|
+
if (!h(this, K).call(this, n)) {
|
|
508
|
+
(u = h(this, x)) == null || u.call(
|
|
509
|
+
this,
|
|
510
|
+
`Received a message from origin \`${n}\` which did not match allowed origins \`[${h(this, N).join(", ")}]\``
|
|
511
|
+
);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (V(s) && (h(this, j).call(this), _(this, k, n)), B(s) && // Previous versions of Penpal don't use MessagePorts and do all
|
|
515
|
+
// communication through the window.
|
|
516
|
+
!h(this, L)) {
|
|
517
|
+
if (_(this, I, t[0]), !h(this, I))
|
|
518
|
+
throw new Y("No port received on ACK2");
|
|
519
|
+
h(this, I).addEventListener("message", h(this, $)), h(this, I).start();
|
|
520
|
+
}
|
|
521
|
+
for (const l of h(this, R))
|
|
522
|
+
l(s);
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
A(this, $, ({ data: e }) => {
|
|
526
|
+
var n;
|
|
527
|
+
if ((n = h(this, H)) != null && n.call(this, e))
|
|
528
|
+
for (const t of h(this, R))
|
|
529
|
+
t(e);
|
|
530
|
+
});
|
|
531
|
+
if (!e)
|
|
532
|
+
throw new P("INVALID_ARGUMENT", "remoteWindow must be defined");
|
|
533
|
+
_(this, S, e), _(this, N, n != null && n.length ? n : [window.origin]);
|
|
534
|
+
}
|
|
535
|
+
}, S = new WeakMap(), N = new WeakMap(), x = new WeakMap(), H = new WeakMap(), k = new WeakMap(), R = new WeakMap(), I = new WeakMap(), L = new WeakMap(), K = new WeakMap(), U = new WeakMap(), j = new WeakMap(), q = new WeakMap(), $ = new WeakMap(), oe), Je = Ge, Ze = (e) => (...n) => {
|
|
536
|
+
console.log(`✍️ %c${e}%c`, "font-weight: bold;", "", ...n);
|
|
537
|
+
}, Xe = Ze;
|
|
538
|
+
function et(e) {
|
|
539
|
+
return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e;
|
|
540
|
+
}
|
|
541
|
+
var ve = { exports: {} };
|
|
542
|
+
(function(e) {
|
|
543
|
+
var n = Object.prototype.hasOwnProperty, t = "~";
|
|
544
|
+
function s() {
|
|
545
|
+
}
|
|
546
|
+
Object.create && (s.prototype = /* @__PURE__ */ Object.create(null), new s().__proto__ || (t = !1));
|
|
547
|
+
function r(c, a, i) {
|
|
548
|
+
this.fn = c, this.context = a, this.once = i || !1;
|
|
549
|
+
}
|
|
550
|
+
function v(c, a, i, o, f) {
|
|
551
|
+
if (typeof i != "function")
|
|
552
|
+
throw new TypeError("The listener must be a function");
|
|
553
|
+
var p = new r(i, o || c, f), b = t ? t + a : a;
|
|
554
|
+
return c._events[b] ? c._events[b].fn ? c._events[b] = [c._events[b], p] : c._events[b].push(p) : (c._events[b] = p, c._eventsCount++), c;
|
|
555
|
+
}
|
|
556
|
+
function u(c, a) {
|
|
557
|
+
--c._eventsCount === 0 ? c._events = new s() : delete c._events[a];
|
|
558
|
+
}
|
|
559
|
+
function l() {
|
|
560
|
+
this._events = new s(), this._eventsCount = 0;
|
|
561
|
+
}
|
|
562
|
+
l.prototype.eventNames = function() {
|
|
563
|
+
var a = [], i, o;
|
|
564
|
+
if (this._eventsCount === 0)
|
|
565
|
+
return a;
|
|
566
|
+
for (o in i = this._events)
|
|
567
|
+
n.call(i, o) && a.push(t ? o.slice(1) : o);
|
|
568
|
+
return Object.getOwnPropertySymbols ? a.concat(Object.getOwnPropertySymbols(i)) : a;
|
|
569
|
+
}, l.prototype.listeners = function(a) {
|
|
570
|
+
var i = t ? t + a : a, o = this._events[i];
|
|
571
|
+
if (!o)
|
|
572
|
+
return [];
|
|
573
|
+
if (o.fn)
|
|
574
|
+
return [o.fn];
|
|
575
|
+
for (var f = 0, p = o.length, b = new Array(p); f < p; f++)
|
|
576
|
+
b[f] = o[f].fn;
|
|
577
|
+
return b;
|
|
578
|
+
}, l.prototype.listenerCount = function(a) {
|
|
579
|
+
var i = t ? t + a : a, o = this._events[i];
|
|
580
|
+
return o ? o.fn ? 1 : o.length : 0;
|
|
581
|
+
}, l.prototype.emit = function(a, i, o, f, p, b) {
|
|
582
|
+
var E = t ? t + a : a;
|
|
583
|
+
if (!this._events[E])
|
|
584
|
+
return !1;
|
|
585
|
+
var d = this._events[E], w = arguments.length, C, y;
|
|
586
|
+
if (d.fn) {
|
|
587
|
+
switch (d.once && this.removeListener(a, d.fn, void 0, !0), w) {
|
|
588
|
+
case 1:
|
|
589
|
+
return d.fn.call(d.context), !0;
|
|
590
|
+
case 2:
|
|
591
|
+
return d.fn.call(d.context, i), !0;
|
|
592
|
+
case 3:
|
|
593
|
+
return d.fn.call(d.context, i, o), !0;
|
|
594
|
+
case 4:
|
|
595
|
+
return d.fn.call(d.context, i, o, f), !0;
|
|
596
|
+
case 5:
|
|
597
|
+
return d.fn.call(d.context, i, o, f, p), !0;
|
|
598
|
+
case 6:
|
|
599
|
+
return d.fn.call(d.context, i, o, f, p, b), !0;
|
|
600
|
+
}
|
|
601
|
+
for (y = 1, C = new Array(w - 1); y < w; y++)
|
|
602
|
+
C[y - 1] = arguments[y];
|
|
603
|
+
d.fn.apply(d.context, C);
|
|
604
|
+
} else {
|
|
605
|
+
var F = d.length, m;
|
|
606
|
+
for (y = 0; y < F; y++)
|
|
607
|
+
switch (d[y].once && this.removeListener(a, d[y].fn, void 0, !0), w) {
|
|
608
|
+
case 1:
|
|
609
|
+
d[y].fn.call(d[y].context);
|
|
610
|
+
break;
|
|
611
|
+
case 2:
|
|
612
|
+
d[y].fn.call(d[y].context, i);
|
|
613
|
+
break;
|
|
614
|
+
case 3:
|
|
615
|
+
d[y].fn.call(d[y].context, i, o);
|
|
616
|
+
break;
|
|
617
|
+
case 4:
|
|
618
|
+
d[y].fn.call(d[y].context, i, o, f);
|
|
619
|
+
break;
|
|
620
|
+
default:
|
|
621
|
+
if (!C)
|
|
622
|
+
for (m = 1, C = new Array(w - 1); m < w; m++)
|
|
623
|
+
C[m - 1] = arguments[m];
|
|
624
|
+
d[y].fn.apply(d[y].context, C);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return !0;
|
|
628
|
+
}, l.prototype.on = function(a, i, o) {
|
|
629
|
+
return v(this, a, i, o, !1);
|
|
630
|
+
}, l.prototype.once = function(a, i, o) {
|
|
631
|
+
return v(this, a, i, o, !0);
|
|
632
|
+
}, l.prototype.removeListener = function(a, i, o, f) {
|
|
633
|
+
var p = t ? t + a : a;
|
|
634
|
+
if (!this._events[p])
|
|
635
|
+
return this;
|
|
636
|
+
if (!i)
|
|
637
|
+
return u(this, p), this;
|
|
638
|
+
var b = this._events[p];
|
|
639
|
+
if (b.fn)
|
|
640
|
+
b.fn === i && (!f || b.once) && (!o || b.context === o) && u(this, p);
|
|
641
|
+
else {
|
|
642
|
+
for (var E = 0, d = [], w = b.length; E < w; E++)
|
|
643
|
+
(b[E].fn !== i || f && !b[E].once || o && b[E].context !== o) && d.push(b[E]);
|
|
644
|
+
d.length ? this._events[p] = d.length === 1 ? d[0] : d : u(this, p);
|
|
645
|
+
}
|
|
646
|
+
return this;
|
|
647
|
+
}, l.prototype.removeAllListeners = function(a) {
|
|
648
|
+
var i;
|
|
649
|
+
return a ? (i = t ? t + a : a, this._events[i] && u(this, i)) : (this._events = new s(), this._eventsCount = 0), this;
|
|
650
|
+
}, l.prototype.off = l.prototype.removeListener, l.prototype.addListener = l.prototype.on, l.prefixed = t, l.EventEmitter = l, e.exports = l;
|
|
651
|
+
})(ve);
|
|
652
|
+
var tt = ve.exports;
|
|
653
|
+
const nt = /* @__PURE__ */ et(tt);
|
|
654
|
+
class ge extends nt {
|
|
655
|
+
on(n, t, s) {
|
|
656
|
+
return super.on(n, t, s);
|
|
657
|
+
}
|
|
658
|
+
once(n, t, s) {
|
|
659
|
+
return super.once(n, t, s);
|
|
660
|
+
}
|
|
661
|
+
off(n, t, s) {
|
|
662
|
+
return super.off(n, t, s);
|
|
663
|
+
}
|
|
664
|
+
emit(n, ...t) {
|
|
665
|
+
return super.emit(n, ...t);
|
|
19
666
|
}
|
|
20
667
|
}
|
|
21
|
-
class
|
|
668
|
+
class st {
|
|
22
669
|
constructor() {
|
|
23
|
-
|
|
24
|
-
|
|
670
|
+
g(this, "queue", []);
|
|
671
|
+
g(this, "maxSize", 100);
|
|
25
672
|
}
|
|
26
|
-
enqueue(
|
|
673
|
+
enqueue(n, t = 3) {
|
|
27
674
|
this.queue.length >= this.maxSize && this.queue.shift();
|
|
28
675
|
const s = {
|
|
29
|
-
...
|
|
676
|
+
...n,
|
|
30
677
|
retryCount: 0,
|
|
31
|
-
maxRetries:
|
|
678
|
+
maxRetries: t
|
|
32
679
|
};
|
|
33
|
-
return this.queue.push(s),
|
|
680
|
+
return this.queue.push(s), n.messageId;
|
|
34
681
|
}
|
|
35
682
|
dequeue() {
|
|
36
683
|
return this.queue.shift();
|
|
@@ -38,87 +685,99 @@ class I {
|
|
|
38
685
|
hasMessages() {
|
|
39
686
|
return this.queue.length > 0;
|
|
40
687
|
}
|
|
41
|
-
getMessageById(
|
|
42
|
-
return this.queue.find((
|
|
688
|
+
getMessageById(n) {
|
|
689
|
+
return this.queue.find((t) => t.messageId === n);
|
|
43
690
|
}
|
|
44
|
-
removeMessageById(
|
|
45
|
-
const
|
|
46
|
-
|
|
691
|
+
removeMessageById(n) {
|
|
692
|
+
const t = this.queue.findIndex((s) => s.messageId === n);
|
|
693
|
+
t !== -1 && this.queue.splice(t, 1);
|
|
47
694
|
}
|
|
48
695
|
clear() {
|
|
49
696
|
this.queue = [];
|
|
50
697
|
}
|
|
51
698
|
}
|
|
52
|
-
class
|
|
53
|
-
constructor(
|
|
54
|
-
this.context =
|
|
699
|
+
class rt {
|
|
700
|
+
constructor(n, t = !1) {
|
|
701
|
+
this.context = n, this.debug = t;
|
|
55
702
|
}
|
|
56
|
-
formatMessage(
|
|
57
|
-
return [`[${(/* @__PURE__ */ new Date()).toISOString()}] [${this.context}] [${
|
|
703
|
+
formatMessage(n, ...t) {
|
|
704
|
+
return [`[${(/* @__PURE__ */ new Date()).toISOString()}] [${this.context}] [${n}]`, ...t];
|
|
58
705
|
}
|
|
59
|
-
info(...
|
|
60
|
-
console.info(...this.formatMessage("INFO", ...
|
|
706
|
+
info(...n) {
|
|
707
|
+
console.info(...this.formatMessage("INFO", ...n));
|
|
61
708
|
}
|
|
62
|
-
debug(...
|
|
63
|
-
this.debug && console.debug(...this.formatMessage("DEBUG", ...
|
|
709
|
+
debug(...n) {
|
|
710
|
+
this.debug && console.debug(...this.formatMessage("DEBUG", ...n));
|
|
64
711
|
}
|
|
65
|
-
warn(...
|
|
66
|
-
console.warn(...this.formatMessage("WARN", ...
|
|
712
|
+
warn(...n) {
|
|
713
|
+
console.warn(...this.formatMessage("WARN", ...n));
|
|
67
714
|
}
|
|
68
|
-
error(...
|
|
69
|
-
console.error(...this.formatMessage("ERROR", ...
|
|
715
|
+
error(...n) {
|
|
716
|
+
console.error(...this.formatMessage("ERROR", ...n));
|
|
70
717
|
}
|
|
71
718
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
719
|
+
let it = (e) => crypto.getRandomValues(new Uint8Array(e)), at = (e, n, t) => {
|
|
720
|
+
let s = (2 << Math.log2(e.length - 1)) - 1, r = -~(1.6 * s * n / e.length);
|
|
721
|
+
return (v = n) => {
|
|
722
|
+
let u = "";
|
|
723
|
+
for (; ; ) {
|
|
724
|
+
let l = t(r), c = r | 0;
|
|
725
|
+
for (; c--; )
|
|
726
|
+
if (u += e[l[c] & s] || "", u.length >= v)
|
|
727
|
+
return u;
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
}, ot = (e, n = 21) => at(e, n | 0, it);
|
|
731
|
+
const ct = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", dt = ot(ct, 12);
|
|
732
|
+
class ht extends ge {
|
|
733
|
+
constructor(t, s = {}) {
|
|
75
734
|
super();
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.iframe =
|
|
735
|
+
g(this, "connection", null);
|
|
736
|
+
g(this, "iframe");
|
|
737
|
+
g(this, "options");
|
|
738
|
+
g(this, "messageQueue");
|
|
739
|
+
g(this, "logger");
|
|
740
|
+
g(this, "isConnected", !1);
|
|
741
|
+
g(this, "reconnectAttempts", 0);
|
|
742
|
+
g(this, "maxReconnectAttempts", 3);
|
|
743
|
+
this.iframe = t, this.options = {
|
|
85
744
|
timeout: 3e4,
|
|
86
745
|
debug: !1,
|
|
87
746
|
retryCount: 3,
|
|
88
747
|
retryDelay: 1e3,
|
|
89
748
|
childOrigin: "*",
|
|
90
749
|
...s
|
|
91
|
-
}, this.messageQueue = new
|
|
750
|
+
}, this.messageQueue = new st(), this.logger = new rt("PenpalBridge", this.options.debug);
|
|
92
751
|
}
|
|
93
752
|
/**
|
|
94
753
|
* 建立连接
|
|
95
754
|
*/
|
|
96
|
-
async connect(
|
|
755
|
+
async connect(t = {}) {
|
|
97
756
|
try {
|
|
98
757
|
this.logger.info("正在建立连接...");
|
|
99
|
-
const s = new
|
|
758
|
+
const s = new Je({
|
|
100
759
|
remoteWindow: this.iframe,
|
|
101
760
|
// Defaults to the current origin.
|
|
102
761
|
allowedOrigins: ["http://127.0.0.1:3000"]
|
|
103
762
|
// Alternatively,
|
|
104
763
|
// allowedOrigins: [new Url(iframe.src).origin]
|
|
105
764
|
});
|
|
106
|
-
this.connection =
|
|
765
|
+
this.connection = Qe({
|
|
107
766
|
messenger: s,
|
|
108
767
|
// Methods the parent window is exposing to the iframe window.
|
|
109
768
|
methods: {
|
|
110
|
-
add(
|
|
111
|
-
return
|
|
769
|
+
add(r, v) {
|
|
770
|
+
return r + v;
|
|
112
771
|
},
|
|
113
|
-
emitEvent: (
|
|
114
|
-
this.emit(
|
|
772
|
+
emitEvent: (r, v) => {
|
|
773
|
+
this.emit(r, v);
|
|
115
774
|
},
|
|
116
775
|
// 接收来自画板的请求
|
|
117
|
-
...
|
|
776
|
+
...t
|
|
118
777
|
},
|
|
119
778
|
timeout: this.options.timeout,
|
|
120
779
|
// childOrigin: this.options.childOrigin,
|
|
121
|
-
log:
|
|
780
|
+
log: Xe("parent")
|
|
122
781
|
}), await this.connection.promise, this.isConnected = !0, this.reconnectAttempts = 0, await this.processQueuedMessages(), this.logger.info("连接建立成功"), this.emit("connected");
|
|
123
782
|
} catch (s) {
|
|
124
783
|
throw this.logger.error("连接失败:", s), await this.handleReconnect(), s;
|
|
@@ -127,23 +786,23 @@ class v extends h {
|
|
|
127
786
|
/**
|
|
128
787
|
* 发送消息到画板
|
|
129
788
|
*/
|
|
130
|
-
async send(
|
|
131
|
-
var
|
|
132
|
-
const
|
|
133
|
-
action:
|
|
789
|
+
async send(t, s) {
|
|
790
|
+
var v;
|
|
791
|
+
const r = {
|
|
792
|
+
action: t,
|
|
134
793
|
params: s,
|
|
135
794
|
timestamp: Date.now(),
|
|
136
|
-
messageId:
|
|
795
|
+
messageId: dt()
|
|
137
796
|
};
|
|
138
797
|
if (!this.isConnected)
|
|
139
|
-
throw this.messageQueue.enqueue(
|
|
798
|
+
throw this.messageQueue.enqueue(r), new Error("Connection not ready, message queued");
|
|
140
799
|
try {
|
|
141
|
-
if (this.logger.debug("发送消息:",
|
|
800
|
+
if (this.logger.debug("发送消息:", r), !((v = this.connection) != null && v.child))
|
|
142
801
|
throw new Error("连接未就绪");
|
|
143
|
-
const
|
|
144
|
-
return this.logger.debug("收到响应:",
|
|
145
|
-
} catch (
|
|
146
|
-
throw this.logger.error("发送消息失败:",
|
|
802
|
+
const u = await this.connection.child.handleMessage(r);
|
|
803
|
+
return this.logger.debug("收到响应:", u), this.validateResponse(u);
|
|
804
|
+
} catch (u) {
|
|
805
|
+
throw this.logger.error("发送消息失败:", u), this.messageQueue.enqueue(r), u;
|
|
147
806
|
}
|
|
148
807
|
}
|
|
149
808
|
/**
|
|
@@ -151,12 +810,12 @@ class v extends h {
|
|
|
151
810
|
*/
|
|
152
811
|
async processQueuedMessages() {
|
|
153
812
|
for (; this.messageQueue.hasMessages(); ) {
|
|
154
|
-
const
|
|
155
|
-
if (
|
|
813
|
+
const t = this.messageQueue.dequeue();
|
|
814
|
+
if (t)
|
|
156
815
|
try {
|
|
157
|
-
await this.send(
|
|
816
|
+
await this.send(t.action, t.params);
|
|
158
817
|
} catch (s) {
|
|
159
|
-
this.logger.warn("队列消息发送失败:", s), this.messageQueue.enqueue(
|
|
818
|
+
this.logger.warn("队列消息发送失败:", s), this.messageQueue.enqueue(t);
|
|
160
819
|
}
|
|
161
820
|
}
|
|
162
821
|
}
|
|
@@ -169,7 +828,7 @@ class v extends h {
|
|
|
169
828
|
return;
|
|
170
829
|
}
|
|
171
830
|
this.reconnectAttempts++, this.logger.info(`尝试重连 (${this.reconnectAttempts}/${this.maxReconnectAttempts})`), await new Promise(
|
|
172
|
-
(
|
|
831
|
+
(t) => setTimeout(t, this.options.retryDelay * this.reconnectAttempts)
|
|
173
832
|
);
|
|
174
833
|
try {
|
|
175
834
|
await this.connect();
|
|
@@ -180,14 +839,14 @@ class v extends h {
|
|
|
180
839
|
/**
|
|
181
840
|
* 验证响应格式
|
|
182
841
|
*/
|
|
183
|
-
validateResponse(
|
|
184
|
-
if (!
|
|
842
|
+
validateResponse(t) {
|
|
843
|
+
if (!t || t.success === void 0)
|
|
185
844
|
throw new Error("Invalid response format");
|
|
186
|
-
if (!
|
|
187
|
-
const s =
|
|
845
|
+
if (!t.success) {
|
|
846
|
+
const s = t.error || { code: "UNKNOWN", message: "Unknown error" };
|
|
188
847
|
throw new Error(`${s.code}: ${s.message}`);
|
|
189
848
|
}
|
|
190
|
-
return
|
|
849
|
+
return t.data;
|
|
191
850
|
}
|
|
192
851
|
/**
|
|
193
852
|
* 断开连接
|
|
@@ -202,15 +861,15 @@ class v extends h {
|
|
|
202
861
|
return this.isConnected;
|
|
203
862
|
}
|
|
204
863
|
}
|
|
205
|
-
class
|
|
206
|
-
constructor(
|
|
207
|
-
this.bridge =
|
|
864
|
+
class ut {
|
|
865
|
+
constructor(n) {
|
|
866
|
+
this.bridge = n;
|
|
208
867
|
}
|
|
209
|
-
async initialize(
|
|
210
|
-
return this.bridge.send("canvas.initialize",
|
|
868
|
+
async initialize(n) {
|
|
869
|
+
return this.bridge.send("canvas.initialize", n);
|
|
211
870
|
}
|
|
212
|
-
async setData(
|
|
213
|
-
return this.bridge.send("canvas.setData",
|
|
871
|
+
async setData(n) {
|
|
872
|
+
return this.bridge.send("canvas.setData", n);
|
|
214
873
|
}
|
|
215
874
|
async getData() {
|
|
216
875
|
return this.bridge.send("canvas.getData");
|
|
@@ -224,76 +883,76 @@ class E {
|
|
|
224
883
|
async redo() {
|
|
225
884
|
return this.bridge.send("canvas.redo");
|
|
226
885
|
}
|
|
227
|
-
async export(
|
|
228
|
-
return this.bridge.send("canvas.export", { format:
|
|
886
|
+
async export(n) {
|
|
887
|
+
return this.bridge.send("canvas.export", { format: n });
|
|
229
888
|
}
|
|
230
889
|
async save() {
|
|
231
890
|
return this.bridge.send("canvas.save");
|
|
232
891
|
}
|
|
233
|
-
async invoke(
|
|
234
|
-
return this.bridge.send("canvas.invoke", { method:
|
|
892
|
+
async invoke(n, ...t) {
|
|
893
|
+
return this.bridge.send("canvas.invoke", { method: n, args: t });
|
|
235
894
|
}
|
|
236
|
-
async setZoom(
|
|
237
|
-
return this.bridge.send("canvas.setZoom", { zoom:
|
|
895
|
+
async setZoom(n) {
|
|
896
|
+
return this.bridge.send("canvas.setZoom", { zoom: n });
|
|
238
897
|
}
|
|
239
898
|
async getSnapshot() {
|
|
240
899
|
return this.bridge.send("canvas.getSnapshot");
|
|
241
900
|
}
|
|
242
901
|
}
|
|
243
|
-
class
|
|
244
|
-
constructor(
|
|
245
|
-
this.bridge =
|
|
902
|
+
class lt {
|
|
903
|
+
constructor(n) {
|
|
904
|
+
this.bridge = n;
|
|
246
905
|
}
|
|
247
|
-
async setAbilities(
|
|
248
|
-
return this.bridge.send("abilities.set",
|
|
906
|
+
async setAbilities(n) {
|
|
907
|
+
return this.bridge.send("abilities.set", n);
|
|
249
908
|
}
|
|
250
909
|
async getAbilities() {
|
|
251
910
|
return this.bridge.send("abilities.get");
|
|
252
911
|
}
|
|
253
|
-
async enableAbility(
|
|
254
|
-
return this.bridge.send("abilities.enable", { name:
|
|
912
|
+
async enableAbility(n) {
|
|
913
|
+
return this.bridge.send("abilities.enable", { name: n });
|
|
255
914
|
}
|
|
256
|
-
async disableAbility(
|
|
257
|
-
return this.bridge.send("abilities.disable", { name:
|
|
915
|
+
async disableAbility(n) {
|
|
916
|
+
return this.bridge.send("abilities.disable", { name: n });
|
|
258
917
|
}
|
|
259
918
|
}
|
|
260
|
-
class
|
|
261
|
-
constructor(
|
|
262
|
-
this.bridge =
|
|
919
|
+
class ft {
|
|
920
|
+
constructor(n) {
|
|
921
|
+
this.bridge = n;
|
|
263
922
|
}
|
|
264
|
-
async setTheme(
|
|
265
|
-
return this.bridge.send("theme.set", { theme:
|
|
923
|
+
async setTheme(n) {
|
|
924
|
+
return this.bridge.send("theme.set", { theme: n });
|
|
266
925
|
}
|
|
267
926
|
async getCurrentTheme() {
|
|
268
927
|
return this.bridge.send("theme.get");
|
|
269
928
|
}
|
|
270
|
-
async updateToken(
|
|
271
|
-
return this.bridge.send("theme.updateToken",
|
|
929
|
+
async updateToken(n) {
|
|
930
|
+
return this.bridge.send("theme.updateToken", n);
|
|
272
931
|
}
|
|
273
932
|
}
|
|
274
|
-
class
|
|
275
|
-
constructor(
|
|
276
|
-
this.bridge =
|
|
933
|
+
class pt {
|
|
934
|
+
constructor(n) {
|
|
935
|
+
this.bridge = n;
|
|
277
936
|
}
|
|
278
|
-
async setLocale(
|
|
279
|
-
return this.bridge.send("i18n.setLocale", { locale:
|
|
937
|
+
async setLocale(n, t) {
|
|
938
|
+
return this.bridge.send("i18n.setLocale", { locale: n, messages: t });
|
|
280
939
|
}
|
|
281
940
|
async getCurrentLocale() {
|
|
282
941
|
return this.bridge.send("i18n.getLocale");
|
|
283
942
|
}
|
|
284
|
-
async updateMessages(
|
|
285
|
-
return this.bridge.send("i18n.updateMessages",
|
|
943
|
+
async updateMessages(n) {
|
|
944
|
+
return this.bridge.send("i18n.updateMessages", n);
|
|
286
945
|
}
|
|
287
946
|
}
|
|
288
|
-
class
|
|
289
|
-
constructor(
|
|
290
|
-
this.bridge =
|
|
947
|
+
class vt {
|
|
948
|
+
constructor(n) {
|
|
949
|
+
this.bridge = n;
|
|
291
950
|
}
|
|
292
951
|
async validateToken() {
|
|
293
952
|
return this.bridge.send("auth.validateToken");
|
|
294
953
|
}
|
|
295
954
|
}
|
|
296
|
-
const
|
|
955
|
+
const O = {
|
|
297
956
|
READY: "ready",
|
|
298
957
|
CHANGE: "change",
|
|
299
958
|
SAVE: "save",
|
|
@@ -315,23 +974,23 @@ const r = {
|
|
|
315
974
|
KEY_DOWN: "key-down",
|
|
316
975
|
KEY_UP: "key-up"
|
|
317
976
|
};
|
|
318
|
-
class
|
|
319
|
-
constructor(
|
|
977
|
+
class gt extends ge {
|
|
978
|
+
constructor(t, s) {
|
|
320
979
|
super();
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
this.validateConfig(s), this.config = s, this.iframe =
|
|
980
|
+
g(this, "bridge");
|
|
981
|
+
g(this, "canvasAPI");
|
|
982
|
+
g(this, "abilityAPI");
|
|
983
|
+
g(this, "themeAPI");
|
|
984
|
+
g(this, "i18nAPI");
|
|
985
|
+
g(this, "authAPI");
|
|
986
|
+
g(this, "config");
|
|
987
|
+
g(this, "iframe");
|
|
988
|
+
g(this, "isInitialized", !1);
|
|
989
|
+
this.validateConfig(s), this.config = s, this.iframe = t, this.bridge = new ht(t, {
|
|
331
990
|
debug: s.debug || !1,
|
|
332
991
|
timeout: 3e4,
|
|
333
|
-
childOrigin: this.extractOrigin(
|
|
334
|
-
}), this.canvasAPI = new
|
|
992
|
+
childOrigin: this.extractOrigin(t.src)
|
|
993
|
+
}), this.canvasAPI = new ut(this.bridge), this.abilityAPI = new lt(this.bridge), this.themeAPI = new ft(this.bridge), this.i18nAPI = new pt(this.bridge), this.authAPI = new vt(this.bridge), this.setupEventHandlers();
|
|
335
994
|
}
|
|
336
995
|
/**
|
|
337
996
|
* 初始化SDK
|
|
@@ -341,63 +1000,63 @@ class O extends h {
|
|
|
341
1000
|
try {
|
|
342
1001
|
await this.bridge.connect({
|
|
343
1002
|
getConfig: () => this.config,
|
|
344
|
-
log: (
|
|
345
|
-
console.log(`[Canvas ${
|
|
1003
|
+
log: (t, s) => {
|
|
1004
|
+
console.log(`[Canvas ${t.toUpperCase()}]`, s);
|
|
346
1005
|
}
|
|
347
|
-
}), await this.canvasAPI.initialize(this.config), this.config.abilities && await this.abilityAPI.setAbilities(this.config.abilities), this.config.theme && await this.themeAPI.setTheme(this.config.theme), this.config.locale && await this.i18nAPI.setLocale(this.config.locale, this.config.messages), this.isInitialized = !0, this.emit(
|
|
348
|
-
} catch (
|
|
349
|
-
throw this.emit(
|
|
1006
|
+
}), await this.canvasAPI.initialize(this.config), this.config.abilities && await this.abilityAPI.setAbilities(this.config.abilities), this.config.theme && await this.themeAPI.setTheme(this.config.theme), this.config.locale && await this.i18nAPI.setLocale(this.config.locale, this.config.messages), this.isInitialized = !0, this.emit(O.READY);
|
|
1007
|
+
} catch (t) {
|
|
1008
|
+
throw this.emit(O.ERROR, t), t;
|
|
350
1009
|
}
|
|
351
1010
|
}
|
|
352
1011
|
// Canvas 操作方法
|
|
353
1012
|
get canvas() {
|
|
354
1013
|
return {
|
|
355
|
-
setData: (
|
|
1014
|
+
setData: (t) => this.canvasAPI.setData(t),
|
|
356
1015
|
getData: () => this.canvasAPI.getData(),
|
|
357
1016
|
clear: () => this.canvasAPI.clear(),
|
|
358
1017
|
undo: () => this.canvasAPI.undo(),
|
|
359
1018
|
redo: () => this.canvasAPI.redo(),
|
|
360
|
-
export: (
|
|
1019
|
+
export: (t = "png") => this.canvasAPI.export(t),
|
|
361
1020
|
save: () => this.canvasAPI.save(),
|
|
362
|
-
setZoom: (
|
|
1021
|
+
setZoom: (t) => this.canvasAPI.setZoom(t),
|
|
363
1022
|
getSnapshot: () => this.canvasAPI.getSnapshot(),
|
|
364
|
-
invoke: (
|
|
1023
|
+
invoke: (t, ...s) => this.canvasAPI.invoke(t, ...s)
|
|
365
1024
|
};
|
|
366
1025
|
}
|
|
367
1026
|
// 能力管理
|
|
368
1027
|
get abilities() {
|
|
369
1028
|
return {
|
|
370
|
-
set: (
|
|
1029
|
+
set: (t) => this.abilityAPI.setAbilities(t),
|
|
371
1030
|
get: () => this.abilityAPI.getAbilities(),
|
|
372
|
-
enable: (
|
|
373
|
-
disable: (
|
|
374
|
-
updateConfig: (
|
|
1031
|
+
enable: (t) => this.abilityAPI.enableAbility(t),
|
|
1032
|
+
disable: (t) => this.abilityAPI.disableAbility(t),
|
|
1033
|
+
updateConfig: (t, s) => this.abilityAPI.updateAbilityConfig(t, s)
|
|
375
1034
|
};
|
|
376
1035
|
}
|
|
377
1036
|
// 主题管理
|
|
378
1037
|
get theme() {
|
|
379
1038
|
return {
|
|
380
|
-
set: (
|
|
1039
|
+
set: (t) => this.themeAPI.setTheme(t),
|
|
381
1040
|
get: () => this.themeAPI.getCurrentTheme(),
|
|
382
|
-
updateToken: (
|
|
1041
|
+
updateToken: (t) => this.themeAPI.updateToken(t),
|
|
383
1042
|
reset: () => this.themeAPI.resetTheme()
|
|
384
1043
|
};
|
|
385
1044
|
}
|
|
386
1045
|
// 国际化管理
|
|
387
1046
|
get i18n() {
|
|
388
1047
|
return {
|
|
389
|
-
setLocale: (
|
|
1048
|
+
setLocale: (t, s) => this.i18nAPI.setLocale(t, s),
|
|
390
1049
|
getLocale: () => this.i18nAPI.getCurrentLocale(),
|
|
391
|
-
updateMessages: (
|
|
1050
|
+
updateMessages: (t) => this.i18nAPI.updateMessages(t),
|
|
392
1051
|
getMessages: () => this.i18nAPI.getMessages(),
|
|
393
|
-
getTranslation: (
|
|
1052
|
+
getTranslation: (t) => this.i18nAPI.getTranslation(t)
|
|
394
1053
|
};
|
|
395
1054
|
}
|
|
396
1055
|
// 鉴权管理
|
|
397
1056
|
get auth() {
|
|
398
1057
|
return {
|
|
399
1058
|
validateToken: () => this.authAPI.validateToken(),
|
|
400
|
-
refreshToken: (
|
|
1059
|
+
refreshToken: (t) => this.authAPI.refreshToken(t),
|
|
401
1060
|
getTokenInfo: () => this.authAPI.getTokenInfo()
|
|
402
1061
|
};
|
|
403
1062
|
}
|
|
@@ -413,43 +1072,43 @@ class O extends h {
|
|
|
413
1072
|
};
|
|
414
1073
|
}
|
|
415
1074
|
setupEventHandlers() {
|
|
416
|
-
this.bridge.on("canvas-change", (
|
|
417
|
-
this.emit(
|
|
418
|
-
}), this.bridge.on("canvas-save", (
|
|
419
|
-
this.emit(
|
|
420
|
-
}), this.bridge.on("canvas-error", (
|
|
421
|
-
this.emit(
|
|
422
|
-
}), this.bridge.on("canvas-selection-changed", (
|
|
423
|
-
this.emit(
|
|
1075
|
+
this.bridge.on("canvas-change", (t) => {
|
|
1076
|
+
this.emit(O.CHANGE, t);
|
|
1077
|
+
}), this.bridge.on("canvas-save", (t) => {
|
|
1078
|
+
this.emit(O.SAVE, t);
|
|
1079
|
+
}), this.bridge.on("canvas-error", (t) => {
|
|
1080
|
+
this.emit(O.ERROR, t);
|
|
1081
|
+
}), this.bridge.on("canvas-selection-changed", (t) => {
|
|
1082
|
+
this.emit(O.SELECTION_CHANGED, t);
|
|
424
1083
|
}), this.bridge.on("connected", () => {
|
|
425
|
-
console.log("链接建立成功"), this.emit(
|
|
1084
|
+
console.log("链接建立成功"), this.emit(O.CONNECTED);
|
|
426
1085
|
}), this.bridge.on("disconnected", () => {
|
|
427
|
-
console.log("链接断开"), this.emit(
|
|
1086
|
+
console.log("链接断开"), this.emit(O.DISCONNECTED);
|
|
428
1087
|
});
|
|
429
1088
|
}
|
|
430
|
-
validateConfig(
|
|
431
|
-
if (!
|
|
1089
|
+
validateConfig(t) {
|
|
1090
|
+
if (!t.token)
|
|
432
1091
|
throw new Error("Token is required");
|
|
433
|
-
if (
|
|
1092
|
+
if (t.abilities) {
|
|
434
1093
|
const s = ["text", "image", "barcode", "multiLayout", "shape", "qrCode"];
|
|
435
|
-
Object.keys(
|
|
436
|
-
s.includes(
|
|
1094
|
+
Object.keys(t.abilities).forEach((r) => {
|
|
1095
|
+
s.includes(r) || console.warn(`Unknown ability: ${r}`);
|
|
437
1096
|
});
|
|
438
1097
|
}
|
|
439
1098
|
}
|
|
440
|
-
extractOrigin(
|
|
1099
|
+
extractOrigin(t) {
|
|
441
1100
|
try {
|
|
442
|
-
return new URL(
|
|
1101
|
+
return new URL(t).origin;
|
|
443
1102
|
} catch {
|
|
444
1103
|
return "*";
|
|
445
1104
|
}
|
|
446
1105
|
}
|
|
447
1106
|
}
|
|
448
|
-
function
|
|
449
|
-
return new
|
|
1107
|
+
function mt(e, n) {
|
|
1108
|
+
return new gt(e, n);
|
|
450
1109
|
}
|
|
451
1110
|
export {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
1111
|
+
O as CANVAS_EVENTS,
|
|
1112
|
+
gt as NiimbotCanvasSDK,
|
|
1113
|
+
mt as createCanvas
|
|
455
1114
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "niimbot-canvas-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Niimbot Canvas iframe communication SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/niimbot-canvas-sdk.umd.js",
|
|
@@ -37,15 +37,20 @@
|
|
|
37
37
|
"penpal": "^7.0.4"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
+
"@babel/core": "^7.28.5",
|
|
41
|
+
"@babel/preset-env": "^7.28.5",
|
|
40
42
|
"@types/node": "^20.0.0",
|
|
41
43
|
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
42
44
|
"@typescript-eslint/parser": "^6.0.0",
|
|
43
45
|
"@vitejs/plugin-vue": "^4.0.0",
|
|
44
46
|
"@vue/test-utils": "^2.0.0",
|
|
47
|
+
"babel-loader": "^10.0.0",
|
|
48
|
+
"babel-plugin-transform-runtime": "^6.23.0",
|
|
45
49
|
"eslint": "^8.45.0",
|
|
46
50
|
"eslint-plugin-vue": "^9.0.0",
|
|
47
51
|
"typescript": "^5.0.0",
|
|
48
52
|
"vite": "^4.0.0",
|
|
53
|
+
"vite-plugin-babel": "^1.3.2",
|
|
49
54
|
"vitest": "^0.34.0",
|
|
50
55
|
"vue": "^3.3.0",
|
|
51
56
|
"vue-tsc": "^1.8.0"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("penpal"),require("eventemitter3"),require("nanoid")):typeof define=="function"&&define.amd?define(["exports","penpal","eventemitter3","nanoid"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n.NiimbotCanvasSDK={},n.Penpal,n.EventEmitter3,n.nanoid))})(this,function(n,a,h,m){"use strict";var D=Object.defineProperty;var T=(n,a,h)=>a in n?D(n,a,{enumerable:!0,configurable:!0,writable:!0,value:h}):n[a]=h;var i=(n,a,h)=>(T(n,typeof a!="symbol"?a+"":a,h),h);class g extends h{on(t,e,s){return super.on(t,e,s)}once(t,e,s){return super.once(t,e,s)}off(t,e,s){return super.off(t,e,s)}emit(t,...e){return super.emit(t,...e)}}class b{constructor(){i(this,"queue",[]);i(this,"maxSize",100)}enqueue(t,e=3){this.queue.length>=this.maxSize&&this.queue.shift();const s={...t,retryCount:0,maxRetries:e};return this.queue.push(s),t.messageId}dequeue(){return this.queue.shift()}hasMessages(){return this.queue.length>0}getMessageById(t){return this.queue.find(e=>e.messageId===t)}removeMessageById(t){const e=this.queue.findIndex(s=>s.messageId===t);e!==-1&&this.queue.splice(e,1)}clear(){this.queue=[]}}class A{constructor(t,e=!1){this.context=t,this.debug=e}formatMessage(t,...e){return[`[${new Date().toISOString()}] [${this.context}] [${t}]`,...e]}info(...t){console.info(...this.formatMessage("INFO",...t))}debug(...t){this.debug&&console.debug(...this.formatMessage("DEBUG",...t))}warn(...t){console.warn(...this.formatMessage("WARN",...t))}error(...t){console.error(...this.formatMessage("ERROR",...t))}}const f="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",I=m.customAlphabet(f,12);class v extends g{constructor(e,s={}){super();i(this,"connection",null);i(this,"iframe");i(this,"options");i(this,"messageQueue");i(this,"logger");i(this,"isConnected",!1);i(this,"reconnectAttempts",0);i(this,"maxReconnectAttempts",3);this.iframe=e,this.options={timeout:3e4,debug:!1,retryCount:3,retryDelay:1e3,childOrigin:"*",...s},this.messageQueue=new b,this.logger=new A("PenpalBridge",this.options.debug)}async connect(e={}){try{this.logger.info("正在建立连接...");const s=new a.WindowMessenger({remoteWindow:this.iframe,allowedOrigins:["http://127.0.0.1:3000"]});this.connection=a.connect({messenger:s,methods:{add(o,d){return o+d},emitEvent:(o,d)=>{this.emit(o,d)},...e},timeout:this.options.timeout,log:a.debug("parent")}),await this.connection.promise,this.isConnected=!0,this.reconnectAttempts=0,await this.processQueuedMessages(),this.logger.info("连接建立成功"),this.emit("connected")}catch(s){throw this.logger.error("连接失败:",s),await this.handleReconnect(),s}}async send(e,s){var d;const o={action:e,params:s,timestamp:Date.now(),messageId:I()};if(!this.isConnected)throw this.messageQueue.enqueue(o),new Error("Connection not ready, message queued");try{if(this.logger.debug("发送消息:",o),!((d=this.connection)!=null&&d.child))throw new Error("连接未就绪");const u=await this.connection.child.handleMessage(o);return this.logger.debug("收到响应:",u),this.validateResponse(u)}catch(u){throw this.logger.error("发送消息失败:",u),this.messageQueue.enqueue(o),u}}async processQueuedMessages(){for(;this.messageQueue.hasMessages();){const e=this.messageQueue.dequeue();if(e)try{await this.send(e.action,e.params)}catch(s){this.logger.warn("队列消息发送失败:",s),this.messageQueue.enqueue(e)}}}async handleReconnect(){if(this.reconnectAttempts>=this.maxReconnectAttempts){this.logger.error("重连次数达到上限"),this.emit("connection-lost");return}this.reconnectAttempts++,this.logger.info(`尝试重连 (${this.reconnectAttempts}/${this.maxReconnectAttempts})`),await new Promise(e=>setTimeout(e,this.options.retryDelay*this.reconnectAttempts));try{await this.connect()}catch{await this.handleReconnect()}}validateResponse(e){if(!e||e.success===void 0)throw new Error("Invalid response format");if(!e.success){const s=e.error||{code:"UNKNOWN",message:"Unknown error"};throw new Error(`${s.code}: ${s.message}`)}return e.data}disconnect(){this.connection&&(this.connection.destroy(),this.isConnected=!1,this.emit("disconnected"),this.logger.info("连接已断开"))}getConnectionStatus(){return this.isConnected}}class p{constructor(t){this.bridge=t}async initialize(t){return this.bridge.send("canvas.initialize",t)}async setData(t){return this.bridge.send("canvas.setData",t)}async getData(){return this.bridge.send("canvas.getData")}async clear(){return this.bridge.send("canvas.clear")}async undo(){return this.bridge.send("canvas.undo")}async redo(){return this.bridge.send("canvas.redo")}async export(t){return this.bridge.send("canvas.export",{format:t})}async save(){return this.bridge.send("canvas.save")}async invoke(t,...e){return this.bridge.send("canvas.invoke",{method:t,args:e})}async setZoom(t){return this.bridge.send("canvas.setZoom",{zoom:t})}async getSnapshot(){return this.bridge.send("canvas.getSnapshot")}}class y{constructor(t){this.bridge=t}async setAbilities(t){return this.bridge.send("abilities.set",t)}async getAbilities(){return this.bridge.send("abilities.get")}async enableAbility(t){return this.bridge.send("abilities.enable",{name:t})}async disableAbility(t){return this.bridge.send("abilities.disable",{name:t})}}class E{constructor(t){this.bridge=t}async setTheme(t){return this.bridge.send("theme.set",{theme:t})}async getCurrentTheme(){return this.bridge.send("theme.get")}async updateToken(t){return this.bridge.send("theme.updateToken",t)}}class w{constructor(t){this.bridge=t}async setLocale(t,e){return this.bridge.send("i18n.setLocale",{locale:t,messages:e})}async getCurrentLocale(){return this.bridge.send("i18n.getLocale")}async updateMessages(t){return this.bridge.send("i18n.updateMessages",t)}}class P{constructor(t){this.bridge=t}async validateToken(){return this.bridge.send("auth.validateToken")}}const c={READY:"ready",CHANGE:"change",SAVE:"save",ERROR:"error",SELECTION_CHANGED:"selection-changed",CONNECTED:"connected",DISCONNECTED:"disconnected",LOADED:"loaded",BEFORE_UNLOAD:"before-unload",OBJECT_ADDED:"object-added",OBJECT_MODIFIED:"object-modified",OBJECT_REMOVED:"object-removed",MOUSE_DOWN:"mouse-down",MOUSE_UP:"mouse-up",MOUSE_MOVE:"mouse-move",KEY_DOWN:"key-down",KEY_UP:"key-up"};class l extends g{constructor(e,s){super();i(this,"bridge");i(this,"canvasAPI");i(this,"abilityAPI");i(this,"themeAPI");i(this,"i18nAPI");i(this,"authAPI");i(this,"config");i(this,"iframe");i(this,"isInitialized",!1);this.validateConfig(s),this.config=s,this.iframe=e,this.bridge=new v(e,{debug:s.debug||!1,timeout:3e4,childOrigin:this.extractOrigin(e.src)}),this.canvasAPI=new p(this.bridge),this.abilityAPI=new y(this.bridge),this.themeAPI=new E(this.bridge),this.i18nAPI=new w(this.bridge),this.authAPI=new P(this.bridge),this.setupEventHandlers()}async init(){if(!this.isInitialized)try{await this.bridge.connect({getConfig:()=>this.config,log:(e,s)=>{console.log(`[Canvas ${e.toUpperCase()}]`,s)}}),await this.canvasAPI.initialize(this.config),this.config.abilities&&await this.abilityAPI.setAbilities(this.config.abilities),this.config.theme&&await this.themeAPI.setTheme(this.config.theme),this.config.locale&&await this.i18nAPI.setLocale(this.config.locale,this.config.messages),this.isInitialized=!0,this.emit(c.READY)}catch(e){throw this.emit(c.ERROR,e),e}}get canvas(){return{setData:e=>this.canvasAPI.setData(e),getData:()=>this.canvasAPI.getData(),clear:()=>this.canvasAPI.clear(),undo:()=>this.canvasAPI.undo(),redo:()=>this.canvasAPI.redo(),export:(e="png")=>this.canvasAPI.export(e),save:()=>this.canvasAPI.save(),setZoom:e=>this.canvasAPI.setZoom(e),getSnapshot:()=>this.canvasAPI.getSnapshot(),invoke:(e,...s)=>this.canvasAPI.invoke(e,...s)}}get abilities(){return{set:e=>this.abilityAPI.setAbilities(e),get:()=>this.abilityAPI.getAbilities(),enable:e=>this.abilityAPI.enableAbility(e),disable:e=>this.abilityAPI.disableAbility(e),updateConfig:(e,s)=>this.abilityAPI.updateAbilityConfig(e,s)}}get theme(){return{set:e=>this.themeAPI.setTheme(e),get:()=>this.themeAPI.getCurrentTheme(),updateToken:e=>this.themeAPI.updateToken(e),reset:()=>this.themeAPI.resetTheme()}}get i18n(){return{setLocale:(e,s)=>this.i18nAPI.setLocale(e,s),getLocale:()=>this.i18nAPI.getCurrentLocale(),updateMessages:e=>this.i18nAPI.updateMessages(e),getMessages:()=>this.i18nAPI.getMessages(),getTranslation:e=>this.i18nAPI.getTranslation(e)}}get auth(){return{validateToken:()=>this.authAPI.validateToken(),refreshToken:e=>this.authAPI.refreshToken(e),getTokenInfo:()=>this.authAPI.getTokenInfo()}}get utils(){return{getConnectionStatus:()=>this.bridge.getConnectionStatus(),disconnect:()=>this.bridge.disconnect(),reconnect:async()=>{this.bridge.disconnect(),this.isInitialized=!1,await this.init()},isInitialized:()=>this.isInitialized}}setupEventHandlers(){this.bridge.on("canvas-change",e=>{this.emit(c.CHANGE,e)}),this.bridge.on("canvas-save",e=>{this.emit(c.SAVE,e)}),this.bridge.on("canvas-error",e=>{this.emit(c.ERROR,e)}),this.bridge.on("canvas-selection-changed",e=>{this.emit(c.SELECTION_CHANGED,e)}),this.bridge.on("connected",()=>{console.log("链接建立成功"),this.emit(c.CONNECTED)}),this.bridge.on("disconnected",()=>{console.log("链接断开"),this.emit(c.DISCONNECTED)})}validateConfig(e){if(!e.token)throw new Error("Token is required");if(e.abilities){const s=["text","image","barcode","multiLayout","shape","qrCode"];Object.keys(e.abilities).forEach(o=>{s.includes(o)||console.warn(`Unknown ability: ${o}`)})}}extractOrigin(e){try{return new URL(e).origin}catch{return"*"}}}function C(r,t){return new l(r,t)}n.CANVAS_EVENTS=c,n.NiimbotCanvasSDK=l,n.createCanvas=C,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
|