Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
This commit is contained in:
parent
93e3a48004
commit
b38dea0597
25901 changed files with 9982925 additions and 369 deletions
1344
gaia/apps/sms/lib/bridge/client.js
Executable file
1344
gaia/apps/sms/lib/bridge/client.js
Executable file
File diff suppressed because it is too large
Load diff
29
gaia/apps/sms/lib/bridge/client.min.js
vendored
Executable file
29
gaia/apps/sms/lib/bridge/client.min.js
vendored
Executable file
|
|
@ -0,0 +1,29 @@
|
|||
var $jscomp={scope:{},global:this},Symbol;$jscomp.initSymbol=function(){$jscomp.global.Symbol||(Symbol=$jscomp.Symbol);$jscomp.initSymbol=function(){}};$jscomp.symbolCounter_=0;$jscomp.Symbol=function(h){return"jscomp_symbol_"+h+$jscomp.symbolCounter_++};$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();Symbol.iterator||(Symbol.iterator=Symbol("iterator"));$jscomp.initSymbolIterator=function(){}};
|
||||
$jscomp.makeIterator=function(h){$jscomp.initSymbolIterator();if(h[Symbol.iterator])return h[Symbol.iterator]();if(!(h instanceof Array||"string"==typeof h||h instanceof String))throw new TypeError(h+" is not iterable");var d=0;return{next:function(){return d==h.length?{done:!0}:{done:!1,value:h[d++]}}}};$jscomp.arrayFromIterator=function(h){for(var d,g=[];!(d=h.next()).done;)g.push(d.value);return g};$jscomp.arrayFromIterable=function(h){return h instanceof Array?h:$jscomp.arrayFromIterator($jscomp.makeIterator(h))};
|
||||
$jscomp.arrayFromArguments=function(h){for(var d=[],g=0;g<h.length;g++)d.push(h[g]);return d};$jscomp.inherits=function(h,d){function g(){}g.prototype=d.prototype;h.prototype=new g;h.prototype.constructor=h;for(var l in d)if($jscomp.global.Object.defineProperties){var c=$jscomp.global.Object.getOwnPropertyDescriptor(d,l);void 0!==c&&$jscomp.global.Object.defineProperty(h,l,c)}else h[l]=d[l]};
|
||||
(function(h){"object"===typeof exports&&"undefined"!==typeof module?module.exports=h():"function"===typeof define&&define.amd?define([],h):("undefined"!==typeof window?window:"undefined"!==typeof global?global:"undefined"!==typeof self?self:this).bridge=h()})(function(){return function d(g,l,c){function m(f,r){if(!l[f]){if(!g[f]){var k="function"==typeof require&&require;if(!r&&k)return k(f,!0);if(n)return n(f,!0);k=Error("Cannot find module '"+f+"'");throw k.code="MODULE_NOT_FOUND",k;}k=l[f]={exports:{}};
|
||||
g[f][0].call(k.exports,function(b){var a=g[f][1][b];return m(a?a:b)},k,k.exports,d,g,l,c)}return l[f].exports}for(var n="function"==typeof require&&require,f=0;f<c.length;f++)m(c[f]);return m}({1:[function(d,g,l){var c=g.exports=self.bridge||{};c.client=d("../src/client");"u"!="undefined"[0]?(void 0)([],function(){return c}):self.bridge=c},{"../src/client":2}],2:[function(d,g,l){function c(a,t,b){if(!(this instanceof c))return new c(a,t,b);"object"==typeof a&&(t=a.endpoint,b=a.timeout,a=a.service);
|
||||
this.id=r();this.service=a;this.timeout=b;this.endpoint=t||this.endpoint;if(!this.endpoint)throw m(1);this.setPort(this.endpoint);this.pending=new Set;this.receiver=q.receiver(this.id).on("_push",this.onPush.bind(this));k("initialized",a)}function m(a,b){for(var e=[],p=1;p<arguments.length;++p)e[p-1]=arguments[p];return Error({1:"an endpoint must be defined",2:'Unable to establish a connection with "'+e[0]+'". Either the target endpoint is not alive or the Service is not `.listen()`ing.',3:'Method "'+
|
||||
e[0]+"\" didn't get a response. Either the target endpoint is not alive or the Service is not `.listen()`ing."}[a])}var n=d("./message/port-adaptors"),f=d("./emitter"),q=d("./message"),r=d("./utils").uuid;g.exports=c;var k={0:function(){},1:function(a){return performance.mark("["+self.constructor.name+"][Client] - "+a)},2:function(a,b){for(var e=[],p=1;p<arguments.length;++p)e[p-1]=arguments[p];console.log.apply(console,[].concat(["[Client]"+("["+self.constructor.name+"]["+location.pathname+"]")+
|
||||
' - "'+a+'"'],$jscomp.arrayFromIterable(e)))}}[0],b=self.constructor.name;c.prototype={connect:function(){var a=this;k("connect");if(this.connected)return this.connected;k("connecting...",this.service);var t=new MessageChannel;this.channel=t.port1;this.channel.start();var e={clientId:this.id,service:this.service,originEnv:b};return this.connected=this.message("_connect").set("transfer",[t.port2]).set("data",e).listen(t.port1).send().then(function(b){k("connected",b);b.event.target===a.channel?a.setPort(a.channel):
|
||||
(a.channel.close(),delete a.channel);a.receiver.listen(a.port)}).catch(function(b){"timeout"==(b&&b.message)&&(b=m(2,a.service),console.error(b.message));throw b;})},disconnect:function(a){var b=this;if(!this.connected)return Promise.resolve();k("disconnecting ...");a={noRespond:a&&a.noRespond,data:this.id};this.cancelPending();return this.message("_disconnect").set(a).send().then(function(){return b.onDisconnected()})},method:function(a,b){for(var e=[],c=1;c<arguments.length;++c)e[c-1]=arguments[c];
|
||||
var d=this;return this.connect().then(function(){k("method",a);return d.message("_method").set({recipient:d.service,data:{name:a,args:e}}).send()}).then(function(a){return a.value}).catch(function(b){"timeout"==(b&&b.message)&&(b=m(3,a),console.error(b.message));throw b;})},plugin:function(a){a(this,{Emitter:f,uuid:r});return this},message:function(a){var b=this;k("create message",a);var e=q(a).set("port",this.port).set("timeout",this.timeout).on("response",function(){return b.pending.delete(e)}).on("cancel",
|
||||
function(){return b.pending.delete(e)});this.pending.add(e);return e},cancelPending:function(){k("cancel pending");this.pending.forEach(function(a){a.cancel()});this.pending.clear()},pendingResponded:function(){var a=[];this.pending.forEach(function(b){return a.push(b.responded)});return Promise.all(a)},onPush:function(a){k("on push",a.data);this._emit(a.data.type,a.data.data)},onDisconnected:function(){var a=this;delete this.connected;this.pendingResponded().then(function(){k("disconnected");a.channel&&
|
||||
a.channel.close();a._emit("disconnected")})},setPort:function(a){k("set port");this.port=n(a)},destroy:function(){var a=this;return this.disconnect().then(function(){a.destroyed||(k("destroy"),a.destroyed=!0,a.receiver.destroy(),a._off(),a.port=a.endpoint=a.receiver=null)})},_on:f.prototype.on,_off:f.prototype.off,_emit:f.prototype.emit};c.prototype.on=function(a,b){var e=this;this.connect().then(function(){k("bind on",a);f.prototype.on.call(e,a,b);e.message("_on").set("noRespond",!0).set("data",
|
||||
{name:a,clientId:e.id}).send(e.port)});return this};c.prototype.off=function(a,b){var e=this;this.connect().then(function(){f.prototype.off.call(e,a,b);e.message("_off").set("noRespond",!0).set("data",{name:a,clientId:e.id}).send(e.port)});return this}},{"./emitter":3,"./message":4,"./message/port-adaptors":5,"./utils":6}],3:[function(d,g,l){function c(d){if(d)return Object.assign(d,c.prototype)}g.exports=c;c.prototype={on:function(c,d){this._callbacks||(this._callbacks={});this._callbacks[c]||(this._callbacks[c]=
|
||||
[]);this._callbacks[c].push(d);return this},off:function(c,d){if(this._callbacks)switch(arguments.length){case 0:this._callbacks={};break;case 1:delete this._callbacks[c];break;default:var f=this._callbacks[c];if(!f)return;var g=f.indexOf(d);~g&&f.splice(g,1)}return this},emit:function(c,d){if(this._callbacks)for(var f=this._callbacks[c]||[],f=f.concat(this._callbacks["*"]||[]),g=0;g<f.length;g++)f[g].call(this,d,c);return this}};d=c.prototype;d.off=d.off;d.on=d.on},{}],4:[function(d,g,l){function c(a){this.cancelled=
|
||||
!1;this.listeners=[];this.deferred=r();this.listen=this.listen.bind(this);this.onMessage=this.onMessage.bind(this);this.onTimeout=this.onTimeout.bind(this);"object"===typeof a?this.setupInbound(a):this.setupOutbound(a);b("initialized",this.type)}function m(a){this.name=a;this.ports=new Set;this.onMessage=this.onMessage.bind(this);this.listen=this.listen.bind(this);this.unlisten=this.unlisten.bind(this);b("receiver initialized",a)}function n(a,b){for(var e=1;e<arguments.length;++e);return Error({1:".send() can only be called once",
|
||||
2:"response already sent for this message",3:"a port must be defined",4:"timeout"}[a])}var f=d("./port-adaptors"),q=d("../emitter");d=d("../utils");var r=d.deferred,k=d.uuid;l=g.exports=function(a){return new c(a)};l.receiver=function(a,b){return new m(a,b)};l.Receiver=m;l.Message=c;var b={0:function(){},1:function(a){return performance.mark("["+self.constructor.name+"][Message] - "+a)},2:function(a,b){for(var e=[],c=1;c<arguments.length;++c)e[c-1]=arguments[c];console.log.apply(console,[].concat(["[Message]"+
|
||||
("["+self.constructor.name+"]["+location.pathname+"]")+' - "'+a+'"'],$jscomp.arrayFromIterable(e)))}}[0];c.prototype={setupOutbound:function(a){this.id=k();this.type=a;this.sent=!1;this.recipient="*"},setupInbound:function(a){b("inbound");this.hasResponded=!1;this.setSourcePort(a.source||a.target);this.event=a;Object.assign(this,a.data)},setSourcePort:function(a){b("set source",a.constructor.name);this.sourcePort=f(a,{ready:!0});return this},set:function(a,c){b("set",a,c);"object"==typeof a?Object.assign(this,
|
||||
a):this[a]=c;return this},serialize:function(){return{id:this.id,type:this.type,data:this.data,recipient:this.recipient,noRespond:this.noRespond}},preventDefault:function(){b("prevent default");this.defaultPrevented=!0},send:function(a){b("send",this.type);if(this.sent)throw n(1);var c=this.serialize(),e=!this.noRespond;this.port=a?f(a):this.port;if(!this.port)throw n(3);e?(this.listen(this.port),this.setResponseTimeout()):this.deferred.resolve();this.port.postMessage(c,this.getTransfer());b("sent",
|
||||
c);return this.deferred.promise},setResponseTimeout:function(){!1!==this.timeout&&(this._timer=setTimeout(this.onTimeout,this.timeout||2E3))},clearResponseTimeout:function(){clearTimeout(this._timer)},getTransfer:function(){return this.transfer||this.event&&this.event.ports},onMessage:function(a){if(a.data.response&&a.data.id===this.id&&!this.cancelled)this.onResponse(a)},onTimeout:function(){b("response timeout",this.type);this.silentTimeout||this.deferred.reject(n(4));this.teardown()},listen:function(a){b("add response listener",
|
||||
a);a=f(a);a.addListener(this.onMessage,this.listen);this.listeners.push(a);return this},unlisten:function(){var a=this;b("remove response listeners");this.listeners.forEach(function(b){return b.removeListener(a.onMessage)});this.listeners=[]},cancel:function(){this.teardown();this.cancelled=!0;this.emit("cancel")},teardown:function(){this.clearResponseTimeout();this.unlisten()},respond:function(a){function c(a){b("resolve",a);d({type:"resolve",value:a})}function e(a){a:switch(a&&a.constructor.name){case "DOMException":case "Error":a=
|
||||
{message:a.message};break a;case "DOMError":a={message:a.message,name:a.name};break a}b("reject",a);d({type:"reject",value:a})}function d(a){f.response=a;f.sourcePort.postMessage({id:f.id,response:a},f.transfer);b("responded with:",a)}b("respond",a,this.id);if(this.hasResponded)throw n(2);if(this.sourcePort&&!this.noRespond){this.hasResponded=!0;var f=this;this.error&&e(this.error);Promise.resolve(a).then(c,e).catch(e)}},forward:function(a){var c=this;b("forward");return this.set("silentTimeout",
|
||||
!0).send(a).then(function(a){return c.respond(a.value)})},onResponse:function(a){b("on response",a.data);var c=a.data.response,e="reject"==c.type?c.value:c;c.event=a;this.response=c;this.teardown();this.deferred[this.response.type](e);this.emit("response",c)}};q(c.prototype);m.prototype={listen:function(a){b("listen");a=f(a||self,{receiver:!0});if(!this.ports.has(a))return a.addListener(this.onMessage,this.listen),this.ports.add(a),this},unlisten:function(){var a=this;b("unlisten");this.ports.forEach(function(b){return b.removeListener(a.onMessage)})},
|
||||
onMessage:function(a){if(a.data.id&&a.data.type&&this.isRecipient(a.data.recipient)&&(b("receiver on message",a.data),a=new c(a),this.emit("message",a),!a.defaultPrevented))try{this.emit(a.type,a)}catch(d){throw a.error=d,a.respond(),d;}},isRecipient:function(a){return a==this.name||"*"==a||"*"==this.name},destroy:function(){this.unlisten();delete this.name;return this}};q(m.prototype)},{"../emitter":3,"../utils":6,"./port-adaptors":5}],5:[function(d,g,l){function c(b){this.target=b}function m(b,
|
||||
a,c){b.addEventListener(a,c)}function n(b,a,c){a={data:a,source:self};c&&(a.ports=c);b.dispatchEvent(new MessageEvent("message",a))}var f=d("../utils").deferred;g.exports=function(b,a){if(!b)throw Error({1:"target is undefined"}[1]);if(b&&b.addListener)return b;var d=r[b.constructor.name];return d?d(b,a):new c(b,a)};var q=c.prototype={constructor:c,addListener:function(b){this.target.addEventListener("message",b)},removeListener:function(b){this.target.removeEventListener("message",b)},postMessage:function(b,
|
||||
a){this.target.postMessage(b,a)}},r={HTMLIFrameElement:function(b){var a=k(b);return{addListener:function(a){window.addEventListener("message",a)},removeListener:function(a){window.removeEventListener("message",a)},postMessage:function(c,e){a.then(function(){return n(b.contentWindow,c,e)})}}},BroadcastChannel:function(b,a){function c(){var a=f();b.postMessage("ready?");m(b,"message",function u(c){"ready"==c.data&&(b.removeEventListener("message",u),a.resolve())});return a.promise}var e=a&&a.receiver,
|
||||
d=a&&a.ready,d=d||e?Promise.resolve():c();e&&(b.postMessage("ready"),m(b,"message",function(a){"ready?"==a.data&&b.postMessage("ready")}));return{target:b,addListener:q.addListener,removeListener:q.removeListener,postMessage:function(a,c){d.then(function(){return b.postMessage(a,c)})}}},Window:function(b,a){var c=a&&a.ready||b===parent||b===self,c=c?Promise.resolve():k(b);return{addListener:function(a){window.addEventListener("message",a)},removeListener:function(a){window.removeEventListener("message",
|
||||
a)},postMessage:function(a,d){c.then(function(){return n(b,a,d)})}}},SharedWorker:function(b){b.port.start();return new c(b.port)},SharedWorkerGlobalScope:function(){var b=[];return{postMessage:function(){},addListener:function(a,c){this.onconnect=function(a){a=a.ports[0];b.push(a);a.start();c(a)};self.addEventListener("connect",this.onconnect)},removeListener:function(a){self.removeEventListener("connect",this.onconnect);b.forEach(function(b){b.close();b.removeEventListener("message",a)})}}}},k=
|
||||
function(){if("undefined"!=typeof window){var b=window.opener||window.parent,a=new WeakSet;b!=self&&m(window,"DOMContentLoaded",function e(){window.removeEventListener("DOMContentLoaded",e);n(b,"load")});m(self,"message",function(b){return"load"==b.data&&a.add(b.source)});return function(b){var c=b.contentWindow||b;if(a.has(c)||c==window.parent)return Promise.resolve();var d=f();m(window,"message",function u(a){"load"==a.data&&a.source==c&&(window.removeEventListener("message",u),d.resolve())});return d.promise}}}()},
|
||||
{"../utils":6}],6:[function(d,g,l){l.uuid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(c){var d=16*Math.random()|0;return("x"==c?d:d&3|8).toString(16)})};l.deferred=function(){var c={};c.promise=new Promise(function(d,g){c.resolve=d;c.reject=g});return c}},{}]},{},[1])(1)});
|
||||
197
gaia/apps/sms/lib/bridge/plugins/stream/client.js
Executable file
197
gaia/apps/sms/lib/bridge/plugins/stream/client.js
Executable file
|
|
@ -0,0 +1,197 @@
|
|||
!((define)=>{define((require,exports,module)=>{
|
||||
|
||||
/**
|
||||
* Mini Logger
|
||||
*
|
||||
* @type {Function}
|
||||
*/
|
||||
|
||||
var debug = 0 ? function(arg1, ...args) {
|
||||
var type = `[${self.constructor.name}][${location.pathname}]`;
|
||||
console.log(`[ClientStream]${type} - "${arg1}"`, ...args);
|
||||
} : () => {};
|
||||
|
||||
/**
|
||||
* Exports
|
||||
*/
|
||||
|
||||
module.exports = (client, utils) => {
|
||||
client._activeStreams = [];
|
||||
client.on('streamevent', onStreamEvent);
|
||||
|
||||
client.stream = function(name) {
|
||||
debug('stream open', name);
|
||||
var args = [].slice.call(arguments, 1);
|
||||
var stream = new ClientStream(client, utils);
|
||||
var self = this;
|
||||
|
||||
this.method('_stream', name, stream.id, this.id, args)
|
||||
.then(() => {
|
||||
debug('stream connected', name);
|
||||
stream._connected.resolve();
|
||||
})
|
||||
|
||||
.catch(err => {
|
||||
debug('error', err);
|
||||
onStreamEvent({
|
||||
type: 'abort',
|
||||
id: stream.id,
|
||||
data: err
|
||||
});
|
||||
});
|
||||
|
||||
this._activeStreams[stream.id] = stream;
|
||||
stream.emitter.on('cancel', onEnd);
|
||||
stream.emitter.on('abort', onEnd);
|
||||
stream.emitter.on('close', onEnd);
|
||||
|
||||
function onEnd() {
|
||||
debug('end');
|
||||
delete self._activeStreams[stream.id];
|
||||
stream.destroy();
|
||||
}
|
||||
|
||||
return stream;
|
||||
};
|
||||
|
||||
/**
|
||||
* Called every time the service calls
|
||||
* write/abort/close on the ServiceStream
|
||||
*
|
||||
* @param {Object} broadcast
|
||||
* @param {String} broadcast.id Stream ID
|
||||
* @param {String} broadcast.type Event type ('write', 'abort' or 'close')
|
||||
* @private
|
||||
*/
|
||||
|
||||
function onStreamEvent(data) {
|
||||
debug('stream event', data.type);
|
||||
var stream = client._activeStreams[data.streamId];
|
||||
stream.emitter.emit(data.type, data.data);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Readable stream instance returned by
|
||||
* a `client.stream('methodName')` call.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @param {String} options.id Stream Id, used to match client/service streams
|
||||
* @param {Client} options.client Client instance
|
||||
*/
|
||||
|
||||
function ClientStream(client, utils) {
|
||||
this.id = utils.uuid();
|
||||
this.client = client;
|
||||
this.emitter = new utils.Emitter();
|
||||
this.emitter.on('close', this.onClose.bind(this));
|
||||
this.emitter.on('abort', this.onAbort.bind(this));
|
||||
this._connected = defer();
|
||||
this.connected = this._connected.promise;
|
||||
this._closed = defer();
|
||||
this.closed = this._closed.promise;
|
||||
debug('initialized');
|
||||
}
|
||||
|
||||
/**
|
||||
* Prototype assigned to variable
|
||||
* to improve compression.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
ClientStream.prototype = {
|
||||
|
||||
/**
|
||||
* Add a listener that will be called
|
||||
* every time the service broadcasts
|
||||
* a new chunk of data.
|
||||
*
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
listen(callback) {
|
||||
debug('listen', callback);
|
||||
this.emitter.on('write', callback);
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes 'data' listener
|
||||
*
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
unlisten(callback) {
|
||||
debug('unlisten', callback);
|
||||
this.emitter.off('write', callback);
|
||||
},
|
||||
|
||||
/**
|
||||
* Notify the service that
|
||||
* action should be canceled
|
||||
*
|
||||
* @param {*} [reason] Optional data to be sent to service.
|
||||
*/
|
||||
|
||||
cancel(reason) {
|
||||
debug('cancel', reason);
|
||||
return this.client.method('_streamcancel', this.id, reason)
|
||||
.then(result => {
|
||||
debug('cancelled', result);
|
||||
this.emitter.emit('cancel');
|
||||
return result;
|
||||
})
|
||||
|
||||
.catch(e => {
|
||||
this.emitter.emit('cancel');
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Used internally by Client when
|
||||
* it receives an 'abort' event
|
||||
* from the service.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
|
||||
onAbort(reason) {
|
||||
debug('abort', reason);
|
||||
this._closed.reject(reason);
|
||||
},
|
||||
|
||||
/**
|
||||
* Used internally by Client when
|
||||
* it receives a 'close' event
|
||||
* from the service.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
|
||||
onClose() {
|
||||
debug('close');
|
||||
this._closed.resolve();
|
||||
},
|
||||
|
||||
destroy() {
|
||||
debug('destroy');
|
||||
this.emitter.off();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Utils
|
||||
*/
|
||||
|
||||
function defer() {
|
||||
var result = {};
|
||||
result.promise = new Promise((resolve, reject) => {
|
||||
result.resolve = resolve;
|
||||
result.reject = reject;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
});})((typeof define)[0]=='f'&&define.amd?define:((n,w)=>{return(typeof
|
||||
module)[0]=='o'?c=>{c(require,exports,module);}:(c)=>{var m={exports:{}};
|
||||
c(n=>w[n],m.exports,m);w[n]=m.exports;};})('streamClient',this));
|
||||
206
gaia/apps/sms/lib/bridge/plugins/stream/service.js
Executable file
206
gaia/apps/sms/lib/bridge/plugins/stream/service.js
Executable file
|
|
@ -0,0 +1,206 @@
|
|||
!((define)=>{define((require,exports,module)=>{
|
||||
|
||||
/**
|
||||
* Mini Logger
|
||||
*
|
||||
* @type {Function}
|
||||
*/
|
||||
|
||||
var debug = 0 ? function(arg1, ...args) {
|
||||
var type = `[${self.constructor.name}][${location.pathname}]`;
|
||||
console.log(`[ServiceStream]${type} - "${arg1}"`, ...args);
|
||||
} : () => {};
|
||||
|
||||
module.exports = function(service) {
|
||||
debug('attaching plugin');
|
||||
|
||||
service
|
||||
.method('_stream', onStream)
|
||||
.method('_streamcancel', onStreamCancel);
|
||||
|
||||
service._streamHandlers = {};
|
||||
service._activeStreams = [];
|
||||
|
||||
service.stream = function(name, fn) {
|
||||
debug('register stream', name);
|
||||
this._streamHandlers[name] = fn;
|
||||
return this;
|
||||
};
|
||||
|
||||
function onStream(name, id, clientId, args) {
|
||||
debug('on stream open', name);
|
||||
var e = {
|
||||
preventDefault: () => e.defaultPrevented = true,
|
||||
arguments: arguments
|
||||
};
|
||||
|
||||
// Allow other plugins to override default behaviour
|
||||
service.emit('before-stream', e);
|
||||
if (e.defaultPrevented) return;
|
||||
|
||||
var fn = service._streamHandlers[name];
|
||||
if (!fn) throw error(1, name);
|
||||
|
||||
var stream = new ServiceStream(id, service, clientId);
|
||||
service._activeStreams[id] = stream;
|
||||
|
||||
// Always pass stream object as first
|
||||
// argument to simplify the process
|
||||
fn.apply(service, [stream].concat(args));
|
||||
}
|
||||
|
||||
function onStreamCancel(id, reason) {
|
||||
debug('stream cancel');
|
||||
var stream = service._activeStreams[id];
|
||||
delete service._activeStreams[id];
|
||||
return stream.cancel(reason);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Writable Stream instance passed to the
|
||||
* `service.stream` implementation
|
||||
*
|
||||
* @param {Object} options
|
||||
* @param {String} options.id Stream ID used to sync client and service streams
|
||||
* @param {BroadcastChannel} options.channel Channel used to postMessage
|
||||
* @param {String} options.serviceId ID of the service
|
||||
* @param {String} options.clientId ID of client that should receive message
|
||||
*/
|
||||
|
||||
function ServiceStream(streamId, service, clientId) {
|
||||
this.id = streamId;
|
||||
this.service = service;
|
||||
this.clientId = clientId;
|
||||
this.writable = true;
|
||||
debug('initialized');
|
||||
}
|
||||
|
||||
/**
|
||||
* Prototype assigned to variable
|
||||
* to improve compression.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
var ServiceStreamPrototype = ServiceStream.prototype;
|
||||
|
||||
/**
|
||||
* Services that allows clients to
|
||||
* cancel the operation before it's
|
||||
* complete should override the
|
||||
* `stream.cancel` method.
|
||||
*
|
||||
* @param {*} [reason] Data sent from client about the cancellation
|
||||
* @returns {(Promise|*)}
|
||||
*/
|
||||
|
||||
ServiceStreamPrototype.cancel = function(reason) {
|
||||
var err = new TypeError('service should implement stream.cancel()');
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
/**
|
||||
* Signal to client that action was
|
||||
* aborted during the process, this
|
||||
* should be used as a way to
|
||||
* communicate errors.
|
||||
*
|
||||
* @param {*} [data] Reason of failure
|
||||
* @returns {Promise}
|
||||
* @public
|
||||
*/
|
||||
|
||||
ServiceStreamPrototype.abort = function(data) {
|
||||
debug('abort', data);
|
||||
this._post('abort', data);
|
||||
this.writable = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends a chunk of data to the client.
|
||||
*
|
||||
* @param {*} data Chunk of data to be sent to client.
|
||||
* @returns {Promise}
|
||||
* @public
|
||||
*/
|
||||
|
||||
ServiceStreamPrototype.write = function(data) {
|
||||
debug('write', data);
|
||||
this._post('write', data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Closes the stream, signals that
|
||||
* action was completed with success.
|
||||
*
|
||||
* According to whatwg streams spec,
|
||||
* WritableStream#close() doesn't send data.
|
||||
*
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
ServiceStreamPrototype.close = function() {
|
||||
debug('close');
|
||||
this._post('close');
|
||||
this.writable = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate the current state and
|
||||
* call cancel on the target stream.
|
||||
*
|
||||
* Called by the Service when client
|
||||
* sends a 'streamcancel' message.
|
||||
*
|
||||
* @param {*} [reason] Reason for cancelation sent by the client
|
||||
* @returns {Promise}
|
||||
* @private
|
||||
*/
|
||||
|
||||
ServiceStreamPrototype._cancel = function(reason) {
|
||||
debug('cancel', reason);
|
||||
return this.cancel(reason)
|
||||
.then(result => {
|
||||
this.writable = false;
|
||||
return result;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate the current state and post message to client.
|
||||
*
|
||||
* @param {String} type 'write', 'abort' or 'close'
|
||||
* @param {String} state 'writable', 'aborted' or 'closed'
|
||||
* @param {*} [data] Data to be sent to the client
|
||||
* @returns {Promise}
|
||||
* @private
|
||||
*/
|
||||
|
||||
ServiceStreamPrototype._post = function(type, data) {
|
||||
debug('post', type, data, this.writable);
|
||||
if (!this.writable) return Promise.reject();
|
||||
this.service.broadcast('streamevent', {
|
||||
streamId: this.id,
|
||||
type: type,
|
||||
data: data
|
||||
}, [this.clientId]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create new `Error` from registery.
|
||||
*
|
||||
* @param {Number} id Error Id
|
||||
* @return {Error}
|
||||
* @private
|
||||
*/
|
||||
function error(id) {
|
||||
var args = [].slice.call(arguments, 1);
|
||||
return new Error({
|
||||
1: 'stream "' + args[0] + '" doesn\'t exist'
|
||||
}[id]);
|
||||
}
|
||||
|
||||
});})((typeof define)[0]=='f'&&define.amd?define:((n,w)=>{return(typeof
|
||||
module)[0]=='o'?c=>{c(require,exports,module);}:(c)=>{var m={exports:{}};
|
||||
c(n=>w[n],m.exports,m);w[n]=m.exports;};})('streamService',this));
|
||||
1358
gaia/apps/sms/lib/bridge/service.js
Executable file
1358
gaia/apps/sms/lib/bridge/service.js
Executable file
File diff suppressed because it is too large
Load diff
27
gaia/apps/sms/lib/bridge/service.min.js
vendored
Executable file
27
gaia/apps/sms/lib/bridge/service.min.js
vendored
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
var $jscomp={scope:{},global:this},Symbol;$jscomp.initSymbol=function(){$jscomp.global.Symbol||(Symbol=$jscomp.Symbol);$jscomp.initSymbol=function(){}};$jscomp.symbolCounter_=0;$jscomp.Symbol=function(k){return"jscomp_symbol_"+k+$jscomp.symbolCounter_++};$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();Symbol.iterator||(Symbol.iterator=Symbol("iterator"));$jscomp.initSymbolIterator=function(){}};
|
||||
$jscomp.makeIterator=function(k){$jscomp.initSymbolIterator();if(k[Symbol.iterator])return k[Symbol.iterator]();if(!(k instanceof Array||"string"==typeof k||k instanceof String))throw new TypeError(k+" is not iterable");var c=0;return{next:function(){return c==k.length?{done:!0}:{done:!1,value:k[c++]}}}};$jscomp.arrayFromIterator=function(k){for(var c,e=[];!(c=k.next()).done;)e.push(c.value);return e};$jscomp.arrayFromIterable=function(k){return k instanceof Array?k:$jscomp.arrayFromIterator($jscomp.makeIterator(k))};
|
||||
$jscomp.arrayFromArguments=function(k){for(var c=[],e=0;e<k.length;e++)c.push(k[e]);return c};$jscomp.inherits=function(k,c){function e(){}e.prototype=c.prototype;k.prototype=new e;k.prototype.constructor=k;for(var l in c)if($jscomp.global.Object.defineProperties){var a=$jscomp.global.Object.getOwnPropertyDescriptor(c,l);void 0!==a&&$jscomp.global.Object.defineProperty(k,l,a)}else k[l]=c[l]};
|
||||
(function(k){"object"===typeof exports&&"undefined"!==typeof module?module.exports=k():"function"===typeof define&&define.amd?define([],k):("undefined"!==typeof window?window:"undefined"!==typeof global?global:"undefined"!==typeof self?self:this).bridge=k()})(function(){return function c(e,l,a){function m(h,f){if(!l[h]){if(!e[h]){var g="function"==typeof require&&require;if(!f&&g)return g(h,!0);if(p)return p(h,!0);g=Error("Cannot find module '"+h+"'");throw g.code="MODULE_NOT_FOUND",g;}g=l[h]={exports:{}};
|
||||
e[h][0].call(g.exports,function(d){var b=e[h][1][d];return m(b?b:d)},g,g.exports,c,e,l,a)}return l[h].exports}for(var p="function"==typeof require&&require,n=0;n<a.length;n++)m(a[n]);return m}({1:[function(c,e,l){var a=e.exports=self.bridge||{};a.service=c("../src/service");"u"!="undefined"[0]?(void 0)([],function(){return a}):self.bridge=a},{"../src/service":5}],2:[function(c,e,l){function a(c){if(c)return Object.assign(c,a.prototype)}e.exports=a;a.prototype={on:function(a,c){this._callbacks||(this._callbacks=
|
||||
{});this._callbacks[a]||(this._callbacks[a]=[]);this._callbacks[a].push(c);return this},off:function(a,c){if(this._callbacks)switch(arguments.length){case 0:this._callbacks={};break;case 1:delete this._callbacks[a];break;default:var e=this._callbacks[a];if(!e)return;var h=e.indexOf(c);~h&&e.splice(h,1)}return this},emit:function(a,c){if(this._callbacks)for(var e=this._callbacks[a]||[],e=e.concat(this._callbacks["*"]||[]),h=0;h<e.length;h++)e[h].call(this,c,a);return this}};c=a.prototype;c.off=c.off;
|
||||
c.on=c.on},{}],3:[function(c,e,l){function a(b){this.cancelled=!1;this.listeners=[];this.deferred=f();this.listen=this.listen.bind(this);this.onMessage=this.onMessage.bind(this);this.onTimeout=this.onTimeout.bind(this);"object"===typeof b?this.setupInbound(b):this.setupOutbound(b);d("initialized",this.type)}function m(b){this.name=b;this.ports=new Set;this.onMessage=this.onMessage.bind(this);this.listen=this.listen.bind(this);this.unlisten=this.unlisten.bind(this);d("receiver initialized",b)}function p(b,
|
||||
d){for(var a=1;a<arguments.length;++a);return Error({1:".send() can only be called once",2:"response already sent for this message",3:"a port must be defined",4:"timeout"}[b])}var n=c("./port-adaptors"),h=c("../emitter");c=c("../utils");var f=c.deferred,g=c.uuid;l=e.exports=function(b){return new a(b)};l.receiver=function(b,d){return new m(b,d)};l.Receiver=m;l.Message=a;var d={0:function(){},1:function(b){return performance.mark("["+self.constructor.name+"][Message] - "+b)},2:function(b,d){for(var a=
|
||||
[],f=1;f<arguments.length;++f)a[f-1]=arguments[f];console.log.apply(console,[].concat(["[Message]"+("["+self.constructor.name+"]["+location.pathname+"]")+' - "'+b+'"'],$jscomp.arrayFromIterable(a)))}}[0];a.prototype={setupOutbound:function(b){this.id=g();this.type=b;this.sent=!1;this.recipient="*"},setupInbound:function(b){d("inbound");this.hasResponded=!1;this.setSourcePort(b.source||b.target);this.event=b;Object.assign(this,b.data)},setSourcePort:function(b){d("set source",b.constructor.name);this.sourcePort=
|
||||
n(b,{ready:!0});return this},set:function(b,a){d("set",b,a);"object"==typeof b?Object.assign(this,b):this[b]=a;return this},serialize:function(){return{id:this.id,type:this.type,data:this.data,recipient:this.recipient,noRespond:this.noRespond}},preventDefault:function(){d("prevent default");this.defaultPrevented=!0},send:function(b){d("send",this.type);if(this.sent)throw p(1);var a=this.serialize(),f=!this.noRespond;this.port=b?n(b):this.port;if(!this.port)throw p(3);f?(this.listen(this.port),this.setResponseTimeout()):
|
||||
this.deferred.resolve();this.port.postMessage(a,this.getTransfer());d("sent",a);return this.deferred.promise},setResponseTimeout:function(){!1!==this.timeout&&(this._timer=setTimeout(this.onTimeout,this.timeout||2E3))},clearResponseTimeout:function(){clearTimeout(this._timer)},getTransfer:function(){return this.transfer||this.event&&this.event.ports},onMessage:function(b){if(b.data.response&&b.data.id===this.id&&!this.cancelled)this.onResponse(b)},onTimeout:function(){d("response timeout",this.type);
|
||||
this.silentTimeout||this.deferred.reject(p(4));this.teardown()},listen:function(b){d("add response listener",b);b=n(b);b.addListener(this.onMessage,this.listen);this.listeners.push(b);return this},unlisten:function(){var b=this;d("remove response listeners");this.listeners.forEach(function(d){return d.removeListener(b.onMessage)});this.listeners=[]},cancel:function(){this.teardown();this.cancelled=!0;this.emit("cancel")},teardown:function(){this.clearResponseTimeout();this.unlisten()},respond:function(b){function a(b){d("resolve",
|
||||
b);c({type:"resolve",value:b})}function f(b){a:switch(b&&b.constructor.name){case "DOMException":case "Error":b={message:b.message};break a;case "DOMError":b={message:b.message,name:b.name};break a}d("reject",b);c({type:"reject",value:b})}function c(b){g.response=b;g.sourcePort.postMessage({id:g.id,response:b},g.transfer);d("responded with:",b)}d("respond",b,this.id);if(this.hasResponded)throw p(2);if(this.sourcePort&&!this.noRespond){this.hasResponded=!0;var g=this;this.error&&f(this.error);Promise.resolve(b).then(a,
|
||||
f).catch(f)}},forward:function(b){var a=this;d("forward");return this.set("silentTimeout",!0).send(b).then(function(b){return a.respond(b.value)})},onResponse:function(b){d("on response",b.data);var a=b.data.response,f="reject"==a.type?a.value:a;a.event=b;this.response=a;this.teardown();this.deferred[this.response.type](f);this.emit("response",a)}};h(a.prototype);m.prototype={listen:function(b){d("listen");b=n(b||self,{receiver:!0});if(!this.ports.has(b))return b.addListener(this.onMessage,this.listen),
|
||||
this.ports.add(b),this},unlisten:function(){var b=this;d("unlisten");this.ports.forEach(function(d){return d.removeListener(b.onMessage)})},onMessage:function(b){if(b.data.id&&b.data.type&&this.isRecipient(b.data.recipient)&&(d("receiver on message",b.data),b=new a(b),this.emit("message",b),!b.defaultPrevented))try{this.emit(b.type,b)}catch(f){throw b.error=f,b.respond(),f;}},isRecipient:function(b){return b==this.name||"*"==b||"*"==this.name},destroy:function(){this.unlisten();delete this.name;return this}};
|
||||
h(m.prototype)},{"../emitter":2,"../utils":6,"./port-adaptors":4}],4:[function(c,e,l){function a(d){this.target=d}function m(d,b,a){d.addEventListener(b,a)}function p(d,b,a){b={data:b,source:self};a&&(b.ports=a);d.dispatchEvent(new MessageEvent("message",b))}var n=c("../utils").deferred;e.exports=function(d,b){if(!d)throw Error({1:"target is undefined"}[1]);if(d&&d.addListener)return d;var g=f[d.constructor.name];return g?g(d,b):new a(d,b)};var h=a.prototype={constructor:a,addListener:function(d){this.target.addEventListener("message",
|
||||
d)},removeListener:function(d){this.target.removeEventListener("message",d)},postMessage:function(d,b){this.target.postMessage(d,b)}},f={HTMLIFrameElement:function(d){var b=g(d);return{addListener:function(b){window.addEventListener("message",b)},removeListener:function(b){window.removeEventListener("message",b)},postMessage:function(a,f){b.then(function(){return p(d.contentWindow,a,f)})}}},BroadcastChannel:function(a,b){function f(){var b=n();a.postMessage("ready?");m(a,"message",function q(f){"ready"==
|
||||
f.data&&(a.removeEventListener("message",q),b.resolve())});return b.promise}var g=b&&b.receiver,c=b&&b.ready,c=c||g?Promise.resolve():f();g&&(a.postMessage("ready"),m(a,"message",function(b){"ready?"==b.data&&a.postMessage("ready")}));return{target:a,addListener:h.addListener,removeListener:h.removeListener,postMessage:function(b,f){c.then(function(){return a.postMessage(b,f)})}}},Window:function(a,b){var f=b&&b.ready||a===parent||a===self,f=f?Promise.resolve():g(a);return{addListener:function(b){window.addEventListener("message",
|
||||
b)},removeListener:function(b){window.removeEventListener("message",b)},postMessage:function(b,g){f.then(function(){return p(a,b,g)})}}},SharedWorker:function(f){f.port.start();return new a(f.port)},SharedWorkerGlobalScope:function(){var a=[];return{postMessage:function(){},addListener:function(b,f){this.onconnect=function(b){b=b.ports[0];a.push(b);b.start();f(b)};self.addEventListener("connect",this.onconnect)},removeListener:function(b){self.removeEventListener("connect",this.onconnect);a.forEach(function(a){a.close();
|
||||
a.removeEventListener("message",b)})}}}},g=function(){if("undefined"!=typeof window){var a=window.opener||window.parent,b=new WeakSet;a!=self&&m(window,"DOMContentLoaded",function r(){window.removeEventListener("DOMContentLoaded",r);p(a,"load")});m(self,"message",function(a){return"load"==a.data&&b.add(a.source)});return function(a){var f=a.contentWindow||a;if(b.has(f)||f==window.parent)return Promise.resolve();var d=n();m(window,"message",function q(b){"load"==b.data&&b.source==f&&(window.removeEventListener("message",
|
||||
q),d.resolve())});return d.promise}}}()},{"../utils":6}],5:[function(c,e,l){function a(f){if(!(this instanceof a))return new a(f);n.Receiver.call(this,f);this.clients={};this.methods={};this.on("_disconnect",this.onDisconnect.bind(this)).on("_connect",this.onConnect.bind(this)).on("_method",this.onMethod.bind(this)).on("_off",this.onOff.bind(this)).on("_on",this.onOn.bind(this));this.destroy=this.destroy.bind(this);h("initialized",f)}function m(a){var g=[].slice.call(arguments,1);return Error({4:'method "'+
|
||||
g[0]+"\" doesn't exist"}[a])}var p=c("./utils").uuid,n=c("./message");c=n.Receiver;e.exports=a;var h={0:function(){},1:function(a){return performance.mark("["+self.constructor.name+"][Service] - "+a)},2:function(a,g){for(var d=[],b=1;b<arguments.length;++b)d[b-1]=arguments[b];console.log.apply(console,[].concat(["[Service]"+("["+self.constructor.name+"]["+location.pathname+"]")+' - "'+a+'"'],$jscomp.arrayFromIterable(d)))}}[0];a.prototype=Object.create(c.prototype);a.prototype.inWindow="Window"===
|
||||
constructor.name;a.prototype.method=function(a,g){this.methods[a]=g;return this};a.prototype.broadcast=function(a,g,d){var b=this;h("broadcast",a,g,d);this.eachClient(function(c){if(!d||~d.indexOf(c.id))h("broadcasting to",c.id),b.push(a,g,c.id,{noRespond:!0})});return this};a.prototype.push=function(a,g,d,b){b=b&&b.noRespond;var c=this.getClient(d);return n("_push").set({recipient:d,noRespond:b,data:{type:a,data:g}}).send(c.port)};a.prototype.eachClient=function(a){for(var c in this.clients)a(this.clients[c])};
|
||||
a.prototype.getClient=function(a){return this.clients[a]};a.prototype.onConnect=function(a){h("connection attempt",a.data,this.name);var c=a.data,d=c.clientId;d&&c.service===this.name&&!this.clients[d]&&(this.emit("before-connect",a),a.defaultPrevented||(this.upgradeChannel(a),this.addClient(d,a.sourcePort),a.respond(),this.emit("connected",d),h("connected",d)))};a.prototype.upgradeChannel=function(a){if(!this.inWindow||"Window"!==a.data.originEnv){var c=a.event.ports;if(c=c&&c[0])a.setSourcePort(c),
|
||||
this.listen(c),c.start();h("channel upgraded")}};a.prototype.onDisconnect=function(a){var c=this.clients[a.data];c&&(this.emit("before-disconnect",a),a.defaultPrevented||(this.removeClient(c.id),a.respond(),this.emit("disconnected",c.id),h("disconnected",c.id)))};a.prototype.onMethod=function(a){h("on method",a.data);this.emit("before-method",a);if(!a.defaultPrevented){var c=a.data,d=c.name,b=this.methods[d],e;if(!b)throw m(4,d);try{e=b.apply(this,c.args)}catch(l){a.error=l}a.respond(e)}};a.prototype.onOn=
|
||||
function(a){h("on on",a.data);this.emit("on",a.data)};a.prototype.onOff=function(a){h("on off");this.emit("off",a.data)};a.prototype.addClient=function(a,c){this.clients[a]={id:a,port:c}};a.prototype.removeClient=function(a){delete this.clients[a]};a.prototype.plugin=function(a){a(this,{uuid:p});return this};a.prototype.disconnect=function(a){this.removeClient(a.id);n("disconnect").set({recipient:a.id,noRespond:!0}).send(a.port)};a.prototype.destroy=function(){delete this.clients;this.unlisten();
|
||||
this.off()};e=a.prototype;e.broadcast=e.broadcast;e.destroy=e.destroy;e.method=e.method;e.plugin=e.plugin},{"./message":3,"./utils":6}],6:[function(c,e,l){l.uuid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var c=16*Math.random()|0;return("x"==a?c:c&3|8).toString(16)})};l.deferred=function(){var a={};a.promise=new Promise(function(c,e){a.resolve=c;a.reject=e});return a}},{}]},{},[1])(1)});
|
||||
Loading…
Reference in a new issue