mcbe-ipc 2.0.0 → 3.0.0
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.
- package/README.md +1 -71
- package/dist/ipc.d.ts +85 -36
- package/dist/ipc.js +775 -328
- package/dist/ipc.min.js +1 -1
- package/package.json +9 -1
package/dist/ipc.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{world,system,ScriptEventSource}from"@minecraft/server";var IPC,SERDE,CRYPTO,NET;(e=>{class _{*MAYBE_ENCRYPT(e){return!1!==this._enc?yield*CRYPTO.encrypt(JSON.stringify(e),this._enc):e}*MAYBE_DECRYPT(e){return!1!==this._enc?JSON.parse(yield*CRYPTO.decrypt(e[1],this._enc)):e[1]}get from(){return this._from}get to(){return this._to}constructor(e,t,n){this._from=e,this._to=t,this._enc=n,this._terminators=new Array}terminate(e=!0){var t=this;t._terminators.forEach(e=>e()),t._terminators.length=0,e&&system.runJob(NET.emit("ipc",t._to+":terminate",[t._from]))}send(t,...n){let i=this;system.runJob(function*(){var e=yield*i.MAYBE_ENCRYPT(n);yield*NET.emit("ipc",i._to+`:${t}:send`,[i._from,e])}())}invoke(i,...t){let r=this;return system.runJob(function*(){var e=yield*r.MAYBE_ENCRYPT(t);yield*NET.emit("ipc",r._to+`:${i}:invoke`,[r._from,e])}()),new Promise(t=>{let n=NET.listen("ipc",r._from+`:${i}:handle`,function*(e){e[0]===r._to&&(e=yield*r.MAYBE_DECRYPT(e),t(e),n())})})}on(e,t){let n=this;e=NET.listen("ipc",n._from+`:${e}:send`,function*(e){e[0]===n._to&&(e=yield*n.MAYBE_DECRYPT(e),t(...e))});return n._terminators.push(e),e}once(e,t){let n=this,i=NET.listen("ipc",n._from+`:${e}:send`,function*(e){e[0]===n._to&&(e=yield*n.MAYBE_DECRYPT(e),t(...e),i())});return n._terminators.push(i),i}handle(t,n){let i=this;var e=NET.listen("ipc",i._from+`:${t}:invoke`,function*(e){e[0]===i._to&&(e=yield*i.MAYBE_DECRYPT(e),e=n(...e),e=yield*i.MAYBE_ENCRYPT([e]),yield*NET.emit("ipc",i._to+`:${t}:handle`,[i._from,e]))});return i._terminators.push(e),e}}e.Connection=_,e.ConnectionManager=class{*MAYBE_ENCRYPT(e,t){return!1!==t?yield*CRYPTO.encrypt(JSON.stringify(e),t):e}*MAYBE_DECRYPT(e,t){return!1!==t?JSON.parse(yield*CRYPTO.decrypt(e[1],t)):e[1]}get id(){return this._id}constructor(e,t=!1){let i=this;this._id=e,this._enc_map=new Map,this._con_map=new Map,this._enc_force=t,NET.listen("ipc",this._id+":handshake:SYN",function*(e){var t=CRYPTO.make_secret(e[4]),n=yield*CRYPTO.make_public(t,e[4],e[3]),t=!(1!==e[1]&&!i._enc_force)&&(yield*CRYPTO.make_shared(t,e[2],e[3]));i._enc_map.set(e[0],t),yield*NET.emit("ipc",e[0]+":handshake:ACK",[i._id,i._enc_force?1:0,n])}),NET.listen("ipc",this._id+":terminate",function*(e){i._enc_map.delete(e[0])})}connect(l,d=!1,c=20){let a=this;return new Promise((r,o)=>{var e=this._con_map.get(l);if(void 0!==e)e.terminate(!1),r(e);else{let t=CRYPTO.make_secret(),n=d?1:0;function s(){i(),system.clearRun(e)}system.runJob(function*(){var e=yield*CRYPTO.make_public(t);yield*NET.emit("ipc",l+":handshake:SYN",[a._id,n,e,CRYPTO.PRIME,CRYPTO.MOD])}());let e=system.runTimeout(()=>{o(),s()},c),i=NET.listen("ipc",this._id+":handshake:ACK",function*(e){e[0]===l&&(e=!(1!==e[1]&&!d)&&(yield*CRYPTO.make_shared(t,e[2])),e=new _(a._id,l,e),a._con_map.set(l,e),r(e),s())})}})}send(n,...i){let r=this;system.runJob(function*(){for(var[e,t]of r._enc_map){t=yield*r.MAYBE_ENCRYPT(i,t);yield*NET.emit("ipc",e+`:${n}:send`,[r._id,t])}}())}invoke(o,...t){let s=this;var e=[];for(let[i,r]of s._enc_map)system.runJob(function*(){var e=yield*s.MAYBE_ENCRYPT(t,r);yield*NET.emit("ipc",i+`:${o}:invoke`,[s._id,e])}()),e.push(new Promise(t=>{let n=NET.listen("ipc",s._id+`:${o}:handle`,function*(e){e[0]===i&&(e=yield*s.MAYBE_DECRYPT(e,r),t(e),n())})}));return e}on(e,n){let i=this;return NET.listen("ipc",i._id+`:${e}:send`,function*(e){var t=i._enc_map.get(e[0]);void 0!==t&&(e=yield*i.MAYBE_DECRYPT(e,t),n(...e))})}once(e,n){let i=this,r=NET.listen("ipc",i._id+`:${e}:send`,function*(e){var t=i._enc_map.get(e[0]);void 0!==t&&(e=yield*i.MAYBE_DECRYPT(e,t),n(...e),r())});return r}handle(i,r){let o=this;return NET.listen("ipc",o._id+`:${i}:invoke`,function*(e){var t,n=o._enc_map.get(e[0]);void 0!==n&&(t=yield*o.MAYBE_DECRYPT(e,n),t=r(...t),t=yield*o.MAYBE_ENCRYPT([t],n),yield*NET.emit("ipc",e[0]+`:${i}:handle`,[o._id,t]))})}},e.send=function(e,...t){system.runJob(NET.emit("ipc",e+":send",t))},e.invoke=function(e,...t){return system.runJob(NET.emit("ipc",e+":invoke",t)),new Promise(t=>{let n=NET.listen("ipc",e+":handle",function*(e){t(e[0]),n()})})},e.on=function(e,t){return NET.listen("ipc",e+":send",function*(e){t(...e)})},e.once=function(e,t){let n=NET.listen("ipc",e+":send",function*(e){t(...e),n()});return n},e.handle=function(t,n){return NET.listen("ipc",t+":invoke",function*(e){e=n(...e);yield*NET.emit("ipc",t+":handle",[e])})}})(IPC=IPC||{});export default IPC;(e=>{let o=[48,49,50,51,52,53,54,55,56,57],s=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,42,43,44,47,58,59,60,61,62,63,64,91,92,93,94,96,123,124,125,126,127],r=/\?[0-9a-zA-Z.\-]{2}|[^?]+/g,l=/^\?[0-9a-zA-Z.\-]{2}$/,d="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";e.encode=function*(t){let n="";for(let e=0;e<t.length;e++){var i=t.charAt(e),r=i.charCodeAt(0);0===e&&o.includes(r)||s.includes(r)?n+="?"+(yield*function*(t){let n="";for(let e=t.charCodeAt(0);0<e;e=Math.floor(e/64))n=d[e%64]+n,yield;return n}(i)).padStart(2,"0"):n+=i,yield}return n},e.decode=function*(e){let t="";var n=e.match(r)??[];for(let e=0;e<n.length;e++){var i=n[e];i.startsWith("?")&&l.test(i)?t+=yield*function*(t){let n=0;for(let e=0;e<t.length;e++)n+=64**(t.length-1-e)*d.indexOf(t[e]),yield;return String.fromCharCode(n)}(i.slice(1)):t+=i,yield}return t}})(SERDE=SERDE||{}),(i=>{i.PRIME=19893121,i.MOD=341;let r=e=>e.toString(16).toUpperCase();function*o(e,t,n){let i=1,r=e%n;for(let e=t;0<e;e=Math.floor(e/2))e%2==1&&(i=i*r%n),r=r*r%n,yield;return i}i.make_secret=function(e=i.MOD){return Math.floor(Math.random()*(e-1))+1},i.make_public=function*(e,t=i.MOD,n=i.PRIME){return r(yield*o(t,e,n))},i.make_shared=function*(e,t,n=i.PRIME){return r(yield*o(parseInt(t,16),e,n))},i.encrypt=function*(t,n){let i="";for(let e=0;e<t.length;e++)i+=String.fromCharCode(t.charCodeAt(e)^n.charCodeAt(e%n.length)),yield;return i},i.decrypt=function*(t,n){let i="";for(let e=0;e<t.length;e++)i+=String.fromCharCode(t.charCodeAt(e)^n.charCodeAt(e%n.length)),yield;return i}})(CRYPTO=CRYPTO||{}),(e=>{let o=new Map;system.afterEvents.scriptEventReceive.subscribe(r=>{system.runJob(function*(){var e=r.id.split(":"),t=yield*SERDE.decode(e[0]),n=o.get(t);if(r.sourceType===ScriptEventSource.Server&&n){var i=E.fromString(yield*SERDE.decode(e[1]));for(let e=0;e<n.length;e++)yield*n[e](i,r.message)}}())});let E;var t;(t=E=E||{}).toString=function(e){return JSON.stringify(e)},t.fromString=function(e){return JSON.parse(e)},e.emit=function*(e,t,n){var i,r=((255&(i=4294967296*Math.random()>>>0)).toString(16).padStart(2,"0")+(i>>8&255).toString(16).padStart(2,"0")+(i>>16&255).toString(16).padStart(2,"0")+(i>>24&255).toString(16).padStart(2,"0")).toUpperCase();let o=yield*SERDE.encode(e);function*s(e,t){e=yield*SERDE.encode(E.toString(e)),world.getDimension("overworld").runCommand(`scriptevent ${o}:${e} `+t)}var l=yield*SERDE.encode(JSON.stringify(n));let d=0,c="",a=0;for(let e=0;e<l.length;e++){var _=l[e],u=_.charCodeAt(0),u=u<=127?1:u<=2047?2:u<=65535?3:4;a+u<2048?(c+=_,a+=u):(yield*s([t,r,d],c),d++,c=_,a=u),yield}yield*s(0===d?[t,r]:[t,r,d,1],c)},e.listen=function(e,n,s){let l=new Map;{var i=e,r=function*([e,i,t,r],o){if(e===n)if(void 0===t)yield*s(JSON.parse(yield*SERDE.decode(o)));else{let n=l.get(i);if(n||(n={size:-1,data_strs:[],data_size:0},l.set(i,n)),1===r&&(n.size=t+1),n.data_strs[t]=o,n.data_size+=t+1,-1!==n.size&&n.data_size===n.size*(n.size+1)/2){let t="";for(let e=0;e<n.data_strs.length;e++)t+=n.data_strs[e],yield;yield*s(JSON.parse(yield*SERDE.decode(t))),l.delete(i)}}};let t=o.get(i);return t||(t=new Array,o.set(i,t)),t.push(r),()=>{var e=t.indexOf(r);-1!==e&&t.splice(e,1),0===t.length&&o.delete(i)}}}})(NET=NET||{});export{NET};
|
|
1
|
+
import{ScriptEventSource,system,world}from"@minecraft/server";var SERDE,CRYPTO,NET,IPC;(e=>{class s{get _end(){return this._length+this._offset}constructor(e=256){this._buffer=new Uint8Array(e),this._data_view=new DataView(this._buffer.buffer),this._length=0,this._offset=0}write(...e){this._ensure_capacity(this._end+e.length),this._buffer.set(e,this._end),this._length+=e.length}read(e=1){var t;return 0<this._length?(e=e>this._length?this._length:e,t=this._buffer.subarray(this._offset,this._offset+e),this._length-=e,this._offset+=e,Array.from(t)):[]}write_uint8(e){this._ensure_capacity(this._end+1),this._data_view.setUint8(this._end,e),this._length+=1}read_uint8(){var e;if(1<=this._length)return e=this._data_view.getUint8(this._offset),--this._length,this._offset+=1,e}write_uint16(e){this._ensure_capacity(this._end+2),this._data_view.setUint16(this._end,e),this._length+=2}read_uint16(){var e;if(2<=this._length)return e=this._data_view.getUint16(this._offset),this._length-=2,this._offset+=2,e}write_uint32(e){this._ensure_capacity(this._end+4),this._data_view.setUint32(this._end,e),this._length+=4}read_uint32(){var e;if(4<=this._length)return e=this._data_view.getUint32(this._offset),this._length-=4,this._offset+=4,e}write_int8(e){this._ensure_capacity(this._end+1),this._data_view.setInt8(this._end,e),this._length+=1}read_int8(){var e;if(1<=this._length)return e=this._data_view.getInt8(this._offset),--this._length,this._offset+=1,e}write_int16(e){this._ensure_capacity(this._end+2),this._data_view.setInt16(this._end,e),this._length+=2}read_int16(){var e;if(2<=this._length)return e=this._data_view.getInt16(this._offset),this._length-=2,this._offset+=2,e}write_int32(e){this._ensure_capacity(this._end+4),this._data_view.setInt32(this._end,e),this._length+=4}read_int32(){var e;if(4<=this._length)return e=this._data_view.getInt32(this._offset),this._length-=4,this._offset+=4,e}write_f32(e){this._ensure_capacity(this._end+4),this._data_view.setFloat32(this._end,e),this._length+=4}read_f32(){var e;if(4<=this._length)return e=this._data_view.getFloat32(this._offset),this._length-=4,this._offset+=4,e}write_f64(e){this._ensure_capacity(this._end+8),this._data_view.setFloat64(this._end,e),this._length+=8}read_f64(){var e;if(8<=this._length)return e=this._data_view.getFloat64(this._offset),this._length-=8,this._offset+=8,e}_ensure_capacity(e){e>this._buffer.length&&((e=new Uint8Array(2*e)).set(this._buffer.subarray(this._offset,this._end),0),this._buffer=e,this._offset=0,this._data_view=new DataView(this._buffer.buffer))}static from_uint8array(e){var t=new s;return t._buffer=e,t._length=e.length,t._offset=0,t._data_view=new DataView(e.buffer),t}to_uint8array(){return this._buffer.subarray(this._offset,this._end)}}e.ByteArray=s,e.serialize=function*(e,t=1/0){var i=e.to_uint8array(),r=[];let n="",a=0;for(let e=0;e<i.length;e++){var s=i[e]|i[++e]<<8,o=s<=127?1:s<=2047?2:s<=65535?3:4;a+(255<s?o:3)>t&&(r.push(n),n="",a=0),255<s?(n+=String.fromCharCode(s),a+=o):(n+="?"+s.toString(16).padStart(2,"0"),a+=3),yield}return r.push(n),r},e.deserialize=function*(t){var i=[];for(let e=0;e<t.length;e++){var r=t[e];for(let e=0;e<r.length;e++){var n,a=r.charCodeAt(e);a==="?".charCodeAt(0)&&e+2<r.length?(n=r.slice(e+1,e+3),n=parseInt(n,16),i.push(255&n),i.push(n>>8),e+=2):(i.push(255&a),i.push(a>>8)),yield}yield}return s.from_uint8array(new Uint8Array(i))}})(SERDE=SERDE||{}),(r=>{r.PRIME=19893121,r.MOD=341;let n=e=>e.toString(16).toUpperCase();function*a(e,t,i){let r=1,n=e%i;for(let e=t;0<e;e=Math.floor(e/2))e%2==1&&(r=r*n%i),n=n*n%i,yield;return r}r.make_secret=function(e=r.MOD){return Math.floor(Math.random()*(e-1))+1},r.make_public=function*(e,t=r.MOD,i=r.PRIME){return n(yield*a(t,e,i))},r.make_shared=function*(e,t,i=r.PRIME){return n(yield*a(parseInt(t,16),e,i))},r.encrypt=function*(t,i){var r=new Uint8Array(t.length);for(let e=0;e<t.length;e++)r[e]=t[e]^i.charCodeAt(e%i.length),yield;return r},r.decrypt=function*(t,i){var r=new Uint8Array(t.length);for(let e=0;e<t.length;e++)r[e]=t[e]^i.charCodeAt(e%i.length),yield;return r}})(CRYPTO=CRYPTO||{});class Proto{static Object(r){return{*serialize(e,t){for(var i in r)yield*r[i].serialize(e[i],t)},*deserialize(e){var t,i={};for(t in r)i[t]=yield*r[t].deserialize(e);return i}}}static Array(n){return{*serialize(e,t){yield*Proto.VarInt.serialize(e.length,t);for(var i of e)yield*n.serialize(i,t)},*deserialize(t){var i=[],r=yield*Proto.VarInt.deserialize(t);for(let e=0;e<r;e++)i[e]=yield*n.deserialize(t);return i}}}static Tuple(...r){return{*serialize(t,i){for(let e=0;e<r.length;e++)yield*r[e].serialize(t[e],i)},*deserialize(t){var i=[];for(let e=0;e<r.length;e++)i[e]=yield*r[e].deserialize(t);return i}}}static Optional(i){return{*serialize(e,t){yield*Proto.Boolean.serialize(void 0!==e,t),void 0!==e&&(yield*i.serialize(e,t))},*deserialize(e){if(yield*Proto.Boolean.deserialize(e))return yield*i.deserialize(e)}}}static Map(s,o){return{*serialize(e,t){yield*Proto.VarInt.serialize(e.size,t);for(var[i,r]of e.entries())yield*s.serialize(i,t),yield*o.serialize(r,t)},*deserialize(t){var i=yield*Proto.VarInt.deserialize(t),r=new Map;for(let e=0;e<i;e++){var n=yield*s.deserialize(t),a=yield*o.deserialize(t);r.set(n,a)}return r}}}static Set(a){return{*serialize(e,t){yield*Proto.VarInt.serialize(e.size,t);for(var[i,r]of e.entries())yield*a.serialize(r,t)},*deserialize(t){var i=yield*Proto.VarInt.deserialize(t),r=new Set;for(let e=0;e<i;e++){var n=yield*a.deserialize(t);r.add(n)}return r}}}}Proto.Int8={*serialize(e,t){t.write_int8(e)},*deserialize(e){return e.read_int8()}},Proto.Int16={*serialize(e,t){t.write_int16(e)},*deserialize(e){return e.read_int16()}},Proto.Int32={*serialize(e,t){t.write_int32(e)},*deserialize(e){return e.read_int32()}},Proto.UInt8={*serialize(e,t){t.write_uint8(e)},*deserialize(e){return e.read_uint8()}},Proto.UInt16={*serialize(e,t){t.write_uint16(e)},*deserialize(e){return e.read_uint16()}},Proto.UInt32={*serialize(e,t){t.write_uint32(e)},*deserialize(e){return e.read_uint32()}},Proto.Float32={*serialize(e,t){t.write_f32(e)},*deserialize(e){return e.read_f32()}},Proto.Float64={*serialize(e,t){t.write_f64(e)},*deserialize(e){return e.read_f64()}},Proto.VarInt={*serialize(e,t){for(;128<=e;)t.write(127&e|128),e>>=7,yield;t.write(e)},*deserialize(e){let t=0,i=0;for(var r;r=e.read()[0],t|=(127&r)<<i,i+=7,yield,128&r;);return t}},Proto.String={*serialize(t,i){yield*Proto.VarInt.serialize(t.length,i);for(let e=0;e<t.length;e++){var r=t.charCodeAt(e);yield*Proto.VarInt.serialize(r,i)}},*deserialize(t){var i=yield*Proto.VarInt.deserialize(t);let r="";for(let e=0;e<i;e++){var n=yield*Proto.VarInt.deserialize(t);r+=String.fromCharCode(n)}return r}},Proto.Boolean={*serialize(e,t){t.write(e?1:0)},*deserialize(e){return 1===e.read()[0]}},Proto.UInt8Array={*serialize(e,t){yield*Proto.VarInt.serialize(e.length,t);for(var i of e)t.write_uint8(i),yield},*deserialize(t){var i=yield*Proto.VarInt.deserialize(t),r=new Uint8Array(i);for(let e=0;e<i;e++)r[e]=t.read_uint8();return r}},Proto.Date={*serialize(e,t){yield*Proto.VarInt.serialize(e.getTime(),t)},*deserialize(e){return new Date(yield*Proto.VarInt.deserialize(e))}},(e=>{var h=SERDE.ByteArray;let y=Proto.Object({guid:Proto.String,index:Proto.VarInt,final:Proto.Boolean}),o=new Map;system.afterEvents.scriptEventReceive.subscribe(n=>{system.runJob(function*(){var[e,t]=n.id.split(":"),e=yield*SERDE.deserialize([e]),e=yield*Proto.String.deserialize(e),i=o.get(e);if(n.sourceType===ScriptEventSource.Server&&i){var e=yield*SERDE.deserialize([t]),r=yield*y.deserialize(e);for(let e=0;e<i.length;e++)yield*i[e](r,n.message)}}())}),e.emit=function*(e,t,i){var r=((255&(n=4294967296*Math.random()>>>0)).toString(16).padStart(2,"0")+(n>>8&255).toString(16).padStart(2,"0")+(n>>16&255).toString(16).padStart(2,"0")+(n>>24&255).toString(16).padStart(2,"0")).toUpperCase(),n=new h;yield*Proto.String.serialize(e,n);let[a]=yield*SERDE.serialize(n);var e=new h,s=(yield*t.serialize(i,e),yield*SERDE.serialize(e,2048));for(let e=0;e<s.length;e++){var o=s[e],l=(d=_=l=void 0,{guid:r,index:e,final:e===s.length-1}),_=o,d=new SERDE.ByteArray,[l]=(yield*y.serialize(l,d),yield*SERDE.serialize(d));world.getDimension("overworld").runCommand(`scriptevent ${a}:${l} `+_)}},e.listen=function(e,r,n){let a=new Map;{var i=e,s=function*(e,t){let i=a.get(e.guid);i||(i={size:-1,serialized_packets:[],data_size:0},a.set(e.guid,i)),e.final&&(i.size=e.index+1),i.serialized_packets[e.index]=t,i.data_size+=e.index+1,-1!==i.size&&i.data_size===i.size*(i.size+1)/2&&(t=yield*SERDE.deserialize(i.serialized_packets),t=yield*r.deserialize(t),yield*n(t),a.delete(e.guid))};let t=o.get(i);return t||(t=new Array,o.set(i,t)),t.push(s),()=>{var e=t.indexOf(s);-1!==e&&t.splice(e,1),0===t.length&&o.delete(i)}}}})(NET=NET||{}),(e=>{let _=Proto.Object({from:Proto.String,bytes:Proto.UInt8Array}),d=Proto.Object({from:Proto.String,encryption_enabled:Proto.Boolean,encryption_public_key:Proto.String,encryption_prime:Proto.VarInt,encryption_modulus:Proto.VarInt}),h=Proto.Object({from:Proto.String,encryption_enabled:Proto.Boolean,encryption_public_key:Proto.String});class y{*MAYBE_ENCRYPT(e){return!1!==this._enc?yield*CRYPTO.encrypt(e,this._enc):e}*MAYBE_DECRYPT(e){return!1!==this._enc?yield*CRYPTO.decrypt(e,this._enc):e}get from(){return this._from}get to(){return this._to}constructor(e,t,i){this._from=e,this._to=t,this._enc=i,this._terminators=new Array}terminate(e=!0){this._terminators.forEach(e=>e()),this._terminators.length=0,e&&system.runJob(NET.emit(`ipc:${this._to}:terminate`,Proto.String,this._from))}send(t,i,r){let n=this;system.runJob(function*(){var e=new SERDE.ByteArray,e=(yield*i.serialize(r,e),yield*n.MAYBE_ENCRYPT(e.to_uint8array()));yield*NET.emit(`ipc:${n._to}:${t}:send`,_,{from:n._from,bytes:e})}())}invoke(r,t,i,n){let a=this;return system.runJob(function*(){var e=new SERDE.ByteArray,e=(yield*t.serialize(i,e),yield*a.MAYBE_ENCRYPT(e.to_uint8array()));yield*NET.emit(`ipc:${a._to}:${r}:invoke`,_,{from:a._from,bytes:e})}()),new Promise(t=>{let i=NET.listen(`ipc:${a._from}:${r}:handle`,_,function*(e){e.from===a._to&&(e=yield*a.MAYBE_DECRYPT(e.bytes),e=SERDE.ByteArray.from_uint8array(e),e=yield*n.deserialize(e),t(e),i())})})}on(e,t,i){let r=this;e=NET.listen(`ipc:${r._from}:${e}:send`,_,function*(e){e.from===r._to&&(e=yield*r.MAYBE_DECRYPT(e.bytes),e=SERDE.ByteArray.from_uint8array(e),e=yield*t.deserialize(e),i(e))});return r._terminators.push(e),e}once(e,t,i){let r=this,n=NET.listen(`ipc:${r._from}:${e}:send`,_,function*(e){e.from===r._to&&(e=yield*r.MAYBE_DECRYPT(e.bytes),e=SERDE.ByteArray.from_uint8array(e),e=yield*t.deserialize(e),i(e),n())});return r._terminators.push(n),n}handle(i,r,n,a){let s=this;var e=NET.listen(`ipc:${s._from}:${i}:invoke`,_,function*(e){var t;e.from===s._to&&(e=yield*s.MAYBE_DECRYPT(e.bytes),e=SERDE.ByteArray.from_uint8array(e),e=yield*r.deserialize(e),e=a(e),t=new SERDE.ByteArray,yield*n.serialize(e,t),e=yield*s.MAYBE_ENCRYPT(t.to_uint8array()),yield*NET.emit(`ipc:${s._to}:${i}:handle`,_,{from:s._from,bytes:e}))});return s._terminators.push(e),e}}e.Connection=y,e.ConnectionManager=class{*MAYBE_ENCRYPT(e,t){return!1!==t?yield*CRYPTO.encrypt(e,t):e}*MAYBE_DECRYPT(e,t){return!1!==t?yield*CRYPTO.decrypt(e,t):e}get id(){return this._id}constructor(e,t=!1){let r=this;this._id=e,this._enc_map=new Map,this._con_map=new Map,this._enc_force=t,NET.listen(`ipc:${this._id}:handshake:synchronize`,d,function*(e){var t=CRYPTO.make_secret(e.encryption_modulus),i=yield*CRYPTO.make_public(t,e.encryption_modulus,e.encryption_prime),t=!(!e.encryption_enabled&&!r._enc_force)&&(yield*CRYPTO.make_shared(t,e.encryption_public_key,e.encryption_prime));r._enc_map.set(e.from,t),yield*NET.emit(`ipc:${e.from}:handshake:acknowledge`,h,{from:r._id,encryption_public_key:i,encryption_enabled:r._enc_force})}),NET.listen(`ipc:${this._id}:terminate`,Proto.String,function*(e){r._enc_map.delete(e)})}connect(s,o=!1,l=20){let _=this;return new Promise((r,n)=>{var e=this._con_map.get(s);if(void 0!==e)e.terminate(!1),r(e);else{let t=CRYPTO.make_secret();function a(){i(),system.clearRun(e)}system.runJob(function*(){var e=yield*CRYPTO.make_public(t);yield*NET.emit(`ipc:${s}:handshake:synchronize`,d,{from:_._id,encryption_enabled:o,encryption_public_key:e,encryption_prime:CRYPTO.PRIME,encryption_modulus:CRYPTO.MOD})}());let e=system.runTimeout(()=>{n(),a()},l),i=NET.listen(`ipc:${this._id}:handshake:acknowledge`,h,function*(e){e.from===s&&(e=!(!e.encryption_enabled&&!o)&&(yield*CRYPTO.make_shared(t,e.encryption_public_key)),e=new y(_._id,s,e),_._con_map.set(s,e),r(e),a())})}})}send(r,n,a){let s=this;system.runJob(function*(){for(var[e,t]of s._enc_map){var i=new SERDE.ByteArray,i=(yield*n.serialize(a,i),yield*s.MAYBE_ENCRYPT(i.to_uint8array(),t));yield*NET.emit(`ipc:${e}:${r}:send`,_,{from:s._id,bytes:i})}}())}invoke(a,t,i,s){let o=this;var e=[];for(let[r,n]of o._enc_map)system.runJob(function*(){var e=new SERDE.ByteArray,e=(yield*t.serialize(i,e),yield*o.MAYBE_ENCRYPT(e.to_uint8array(),n));yield*NET.emit(`ipc:${r}:${a}:invoke`,_,{from:o._id,bytes:e})}()),e.push(new Promise(t=>{let i=NET.listen(`ipc:${o._id}:${a}:handle`,_,function*(e){e.from===r&&(e=yield*o.MAYBE_DECRYPT(e.bytes,n),e=SERDE.ByteArray.from_uint8array(e),e=yield*s.deserialize(e),t(e),i())})}));return e}on(e,i,r){let n=this;return NET.listen(`ipc:${n._id}:${e}:send`,_,function*(e){var t=n._enc_map.get(e.from);void 0!==t&&(e=yield*n.MAYBE_DECRYPT(e.bytes,t),t=SERDE.ByteArray.from_uint8array(e),e=yield*i.deserialize(t),r(e))})}once(e,i,r){let n=this,a=NET.listen(`ipc:${n._id}:${e}:send`,_,function*(e){var t=n._enc_map.get(e.from);void 0!==t&&(e=yield*n.MAYBE_DECRYPT(e.bytes,t),t=SERDE.ByteArray.from_uint8array(e),e=yield*i.deserialize(t),r(e),a())});return a}handle(n,a,s,o){let l=this;return NET.listen(`ipc:${l._id}:${n}:invoke`,_,function*(e){var t,i,r=l._enc_map.get(e.from);void 0!==r&&(i=yield*l.MAYBE_DECRYPT(e.bytes,r),i=SERDE.ByteArray.from_uint8array(i),i=yield*a.deserialize(i),i=o(i),t=new SERDE.ByteArray,yield*s.serialize(i,t),i=yield*l.MAYBE_ENCRYPT(t.to_uint8array(),r),yield*NET.emit(`ipc:${e.from}:${n}:handle`,_,{from:l._id,bytes:i}))})}},e.send=function(e,t,i){system.runJob(NET.emit(`ipc:${e}:send`,t,i))},e.invoke=function(e,t,i,r){return system.runJob(NET.emit(`ipc:${e}:invoke`,t,i)),new Promise(t=>{let i=NET.listen(`ipc:${e}:handle`,r,function*(e){t(e),i()})})},e.on=function(e,t,i){return NET.listen(`ipc:${e}:send`,t,function*(e){i(e)})},e.once=function(e,t,i){let r=NET.listen(`ipc:${e}:send`,t,function*(e){i(e),r()});return r},e.handle=function(t,e,i,r){return NET.listen(`ipc:${t}:invoke`,e,function*(e){e=r(e);yield*NET.emit(`ipc:${t}:handle`,i,e)})}})(IPC=IPC||{});export default IPC;export{SERDE,Proto,NET,IPC};
|
package/package.json
CHANGED
|
@@ -3,7 +3,15 @@
|
|
|
3
3
|
"author": "OmniacDev",
|
|
4
4
|
"description": "IPC system for MCBE Script API projects",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "
|
|
6
|
+
"version": "3.0.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/OmniacDev/mcbe-ipc.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/OmniacDev/mcbe-ipc/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/OmniacDev/mcbe-ipc",
|
|
7
15
|
"type": "module",
|
|
8
16
|
"main": "dist/ipc.js",
|
|
9
17
|
"types": "dist/ipc.d.ts",
|