@dwmt/comlink
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -70,2 +70,10 @@ import Cookies from 'js-cookie'; | ||
function generateUUID() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = Math.random() * 16 | 0; | ||
var v = c === 'x' ? r : r & 0x3 | 0x8; | ||
return v.toString(16); | ||
}); | ||
} | ||
function _await(value, then, direct) { | ||
@@ -106,2 +114,18 @@ if (direct) { | ||
var retry = function retry(fn) { | ||
var ms = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000; | ||
var maxRetries = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; | ||
return new Promise(function (resolve, reject) { | ||
fn().then(resolve)["catch"](function () { | ||
setTimeout(function () { | ||
if (maxRetries === 0) { | ||
return reject(new Error('maximum retries exceeded')); | ||
} | ||
retry(fn, ms, maxRetries - 1).then(resolve); | ||
}, ms); | ||
}); | ||
}); | ||
}; | ||
function _finallyRethrows(body, finalizer) { | ||
@@ -139,26 +163,2 @@ try { | ||
function generateUUID() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = Math.random() * 16 | 0; | ||
var v = c === 'x' ? r : r & 0x3 | 0x8; | ||
return v.toString(16); | ||
}); | ||
} | ||
var retry = function retry(fn) { | ||
var ms = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000; | ||
var maxRetries = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; | ||
return new Promise(function (resolve, reject) { | ||
fn().then(resolve)["catch"](function () { | ||
setTimeout(function () { | ||
if (maxRetries === 0) { | ||
return reject(new Error('maximum retries exceeded')); | ||
} | ||
retry(fn, ms, maxRetries - 1).then(resolve); | ||
}, ms); | ||
}); | ||
}); | ||
}; | ||
function httpStrategy(options) { | ||
@@ -174,2 +174,5 @@ return { | ||
}, | ||
headerHandler: options.headerHandler || function (headers) { | ||
return _await(true); | ||
}, | ||
loader: options.loader || new Loader(), | ||
@@ -234,2 +237,6 @@ logger: options.logger, | ||
if (options.rpc && options.rpc.headerHandler) { | ||
options.rpc.headerHandler(tr.headers || {}); | ||
} | ||
if (tr.id) { | ||
@@ -414,6 +421,12 @@ self._channels[options.name].answers[tr.id] = tr.result || { | ||
return _await(axios.get(url, options.axios || {})); | ||
return _await(axios.get(url, options.axios || {}), function (resp) { | ||
return _await(channel.headerHandler(resp.headers), function () { | ||
return resp; | ||
}); | ||
}); | ||
}, function (err) { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
return _await(channel.headerHandler(err.response.headers), function () { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
}); | ||
}); | ||
@@ -451,6 +464,12 @@ }); | ||
return _await(axios.post(url, data, options.axios || {})); | ||
return _await(axios.post(url, data, options.axios || {}), function (resp) { | ||
return _await(channel.headerHandler(resp.headers), function () { | ||
return resp; | ||
}); | ||
}); | ||
}, function (err) { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
return _await(channel.headerHandler(err.response.headers), function () { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
}); | ||
}); | ||
@@ -488,6 +507,12 @@ }); | ||
return _await(axios.put(url, data, options.axios || {})); | ||
return _await(axios.put(url, data, options.axios || {}), function (resp) { | ||
return _await(channel.headerHandler(resp.headers), function () { | ||
return resp; | ||
}); | ||
}); | ||
}, function (err) { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
return _await(channel.headerHandler(err.response.headers), function () { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
}); | ||
}); | ||
@@ -525,6 +550,12 @@ }); | ||
return _await(axios["delete"](url, options.axios || {})); | ||
return _await(axios["delete"](url, options.axios || {}), function (resp) { | ||
return _await(channel.headerHandler(resp.headers), function () { | ||
return resp; | ||
}); | ||
}); | ||
}, function (err) { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
return _await(channel.headerHandler(err.response.headers), function () { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
}); | ||
}); | ||
@@ -704,2 +735,3 @@ }); | ||
this._clients = {}; | ||
this.session = {}; | ||
} | ||
@@ -737,2 +769,5 @@ | ||
wss.on('connection', _async(function (ws, req) { | ||
var clientID = generateUUID(); | ||
_this.session[clientID] = {}; | ||
if (channel.auth) { | ||
@@ -750,5 +785,10 @@ console.log('Authenticating ws connection'); | ||
} | ||
_this.session[clientID].token = req.headers['sec-websocket-protocol']; | ||
ws.id = clientID; | ||
} | ||
ws.on('message', _async(function (message) { | ||
var _exit = false; | ||
console.log('Incoming message from client: ' + ws.id); | ||
var parsed = JSON.parse(message); | ||
@@ -772,15 +812,41 @@ var dialect = parsed._dialect; | ||
return _continueIgnored(_catch$1(function () { | ||
return _await$1(_this._dialects[dialect].onRequest(parsed), function (returnValue) { | ||
var headers = {}; | ||
return _invoke$1(function () { | ||
if (channel.headerInjector) { | ||
return _catch$1(function () { | ||
return _await$1(channel.headerInjector(_this.session[ws.id]), function (h) { | ||
headers = Object.assign({}, h); | ||
}); | ||
}, function (err) { | ||
_exit = true; | ||
return ws.send(JSON.stringify({ | ||
id: id, | ||
error: err.message, | ||
headers: Object.assign({}, headers, { | ||
serverTime: Date.now() | ||
}) | ||
})); | ||
}); | ||
} | ||
}, function (_result2) { | ||
return _exit ? _result2 : _continueIgnored(_catch$1(function () { | ||
return _await$1(_this._dialects[dialect].onRequest(parsed), function (returnValue) { | ||
ws.send(JSON.stringify({ | ||
id: id, | ||
result: returnValue, | ||
headers: Object.assign({}, headers, { | ||
serverTime: Date.now() | ||
}) | ||
})); | ||
}); | ||
}, function (err) { | ||
ws.send(JSON.stringify({ | ||
id: id, | ||
result: returnValue | ||
error: err.message, | ||
headers: Object.assign({}, headers, { | ||
serverTime: Date.now() | ||
}) | ||
})); | ||
}); | ||
}, function (err) { | ||
ws.send(JSON.stringify({ | ||
id: id, | ||
error: err.message | ||
})); | ||
})); | ||
}); | ||
})); | ||
@@ -831,2 +897,12 @@ return _await$1(); | ||
function _invoke$1(body, then) { | ||
var result = body(); | ||
if (result && result.then) { | ||
return result.then(then); | ||
} | ||
return then(result); | ||
} | ||
var Comlink = { | ||
@@ -833,0 +909,0 @@ Client: Client, |
@@ -74,2 +74,10 @@ 'use strict'; | ||
function generateUUID() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = Math.random() * 16 | 0; | ||
var v = c === 'x' ? r : r & 0x3 | 0x8; | ||
return v.toString(16); | ||
}); | ||
} | ||
function _await(value, then, direct) { | ||
@@ -110,2 +118,18 @@ if (direct) { | ||
var retry = function retry(fn) { | ||
var ms = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000; | ||
var maxRetries = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; | ||
return new Promise(function (resolve, reject) { | ||
fn().then(resolve)["catch"](function () { | ||
setTimeout(function () { | ||
if (maxRetries === 0) { | ||
return reject(new Error('maximum retries exceeded')); | ||
} | ||
retry(fn, ms, maxRetries - 1).then(resolve); | ||
}, ms); | ||
}); | ||
}); | ||
}; | ||
function _finallyRethrows(body, finalizer) { | ||
@@ -143,26 +167,2 @@ try { | ||
function generateUUID() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = Math.random() * 16 | 0; | ||
var v = c === 'x' ? r : r & 0x3 | 0x8; | ||
return v.toString(16); | ||
}); | ||
} | ||
var retry = function retry(fn) { | ||
var ms = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000; | ||
var maxRetries = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; | ||
return new Promise(function (resolve, reject) { | ||
fn().then(resolve)["catch"](function () { | ||
setTimeout(function () { | ||
if (maxRetries === 0) { | ||
return reject(new Error('maximum retries exceeded')); | ||
} | ||
retry(fn, ms, maxRetries - 1).then(resolve); | ||
}, ms); | ||
}); | ||
}); | ||
}; | ||
function httpStrategy(options) { | ||
@@ -178,2 +178,5 @@ return { | ||
}, | ||
headerHandler: options.headerHandler || function (headers) { | ||
return _await(true); | ||
}, | ||
loader: options.loader || new Loader(), | ||
@@ -238,2 +241,6 @@ logger: options.logger, | ||
if (options.rpc && options.rpc.headerHandler) { | ||
options.rpc.headerHandler(tr.headers || {}); | ||
} | ||
if (tr.id) { | ||
@@ -418,6 +425,12 @@ self._channels[options.name].answers[tr.id] = tr.result || { | ||
return _await(axios.get(url, options.axios || {})); | ||
return _await(axios.get(url, options.axios || {}), function (resp) { | ||
return _await(channel.headerHandler(resp.headers), function () { | ||
return resp; | ||
}); | ||
}); | ||
}, function (err) { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
return _await(channel.headerHandler(err.response.headers), function () { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
}); | ||
}); | ||
@@ -455,6 +468,12 @@ }); | ||
return _await(axios.post(url, data, options.axios || {})); | ||
return _await(axios.post(url, data, options.axios || {}), function (resp) { | ||
return _await(channel.headerHandler(resp.headers), function () { | ||
return resp; | ||
}); | ||
}); | ||
}, function (err) { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
return _await(channel.headerHandler(err.response.headers), function () { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
}); | ||
}); | ||
@@ -492,6 +511,12 @@ }); | ||
return _await(axios.put(url, data, options.axios || {})); | ||
return _await(axios.put(url, data, options.axios || {}), function (resp) { | ||
return _await(channel.headerHandler(resp.headers), function () { | ||
return resp; | ||
}); | ||
}); | ||
}, function (err) { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
return _await(channel.headerHandler(err.response.headers), function () { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
}); | ||
}); | ||
@@ -529,6 +554,12 @@ }); | ||
return _await(axios["delete"](url, options.axios || {})); | ||
return _await(axios["delete"](url, options.axios || {}), function (resp) { | ||
return _await(channel.headerHandler(resp.headers), function () { | ||
return resp; | ||
}); | ||
}); | ||
}, function (err) { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
return _await(channel.headerHandler(err.response.headers), function () { | ||
return _await(errorHandler(err), function () { | ||
throw err; | ||
}); | ||
}); | ||
@@ -708,2 +739,3 @@ }); | ||
this._clients = {}; | ||
this.session = {}; | ||
} | ||
@@ -741,2 +773,5 @@ | ||
wss.on('connection', _async(function (ws, req) { | ||
var clientID = generateUUID(); | ||
_this.session[clientID] = {}; | ||
if (channel.auth) { | ||
@@ -754,5 +789,10 @@ console.log('Authenticating ws connection'); | ||
} | ||
_this.session[clientID].token = req.headers['sec-websocket-protocol']; | ||
ws.id = clientID; | ||
} | ||
ws.on('message', _async(function (message) { | ||
var _exit = false; | ||
console.log('Incoming message from client: ' + ws.id); | ||
var parsed = JSON.parse(message); | ||
@@ -776,15 +816,41 @@ var dialect = parsed._dialect; | ||
return _continueIgnored(_catch$1(function () { | ||
return _await$1(_this._dialects[dialect].onRequest(parsed), function (returnValue) { | ||
var headers = {}; | ||
return _invoke$1(function () { | ||
if (channel.headerInjector) { | ||
return _catch$1(function () { | ||
return _await$1(channel.headerInjector(_this.session[ws.id]), function (h) { | ||
headers = Object.assign({}, h); | ||
}); | ||
}, function (err) { | ||
_exit = true; | ||
return ws.send(JSON.stringify({ | ||
id: id, | ||
error: err.message, | ||
headers: Object.assign({}, headers, { | ||
serverTime: Date.now() | ||
}) | ||
})); | ||
}); | ||
} | ||
}, function (_result2) { | ||
return _exit ? _result2 : _continueIgnored(_catch$1(function () { | ||
return _await$1(_this._dialects[dialect].onRequest(parsed), function (returnValue) { | ||
ws.send(JSON.stringify({ | ||
id: id, | ||
result: returnValue, | ||
headers: Object.assign({}, headers, { | ||
serverTime: Date.now() | ||
}) | ||
})); | ||
}); | ||
}, function (err) { | ||
ws.send(JSON.stringify({ | ||
id: id, | ||
result: returnValue | ||
error: err.message, | ||
headers: Object.assign({}, headers, { | ||
serverTime: Date.now() | ||
}) | ||
})); | ||
}); | ||
}, function (err) { | ||
ws.send(JSON.stringify({ | ||
id: id, | ||
error: err.message | ||
})); | ||
})); | ||
}); | ||
})); | ||
@@ -835,2 +901,12 @@ return _await$1(); | ||
function _invoke$1(body, then) { | ||
var result = body(); | ||
if (result && result.then) { | ||
return result.then(then); | ||
} | ||
return then(result); | ||
} | ||
var Comlink = { | ||
@@ -837,0 +913,0 @@ Client: Client, |
{ | ||
"name": "@dwmt/comlink", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Communication library for lazy enthusiasts", | ||
@@ -5,0 +5,0 @@ "main": "dist/Comlink.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58119
1708