Comparing version 0.1.2 to 0.2.0
@@ -8,2 +8,3 @@ "use strict"; | ||
var _serialize = _interopRequireDefault(require("./serialize.js")); | ||
var _deserialize = _interopRequireDefault(require("./deserialize.js")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -14,3 +15,3 @@ function call(it, method, params, { | ||
if (!params) params = []; | ||
let [params_serialized, params_functions] = (0, _serialize.default)(params, "microlink.call:"); | ||
let [params_serialized, params_functions] = (0, _serialize.default)(params); | ||
if (debug_level >= 2) { | ||
@@ -75,3 +76,7 @@ console.log("[microlink.call] serialized to ", [params_serialized, params_functions]); | ||
params_functions = null; | ||
resolve(data.result); | ||
const result = (0, _deserialize.default)(it, data.result); | ||
if (debug_level >= 2) { | ||
console.log("[microlink.call] deserialized", data.result, "to", result); | ||
} | ||
resolve(result); | ||
} | ||
@@ -78,0 +83,0 @@ }; |
@@ -9,2 +9,3 @@ "use strict"; | ||
var _batchcall = _interopRequireDefault(require("./batchcall.js")); | ||
var _enums = require("./enums.js"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -36,4 +37,4 @@ // const cache = {}; | ||
return obj; | ||
} else if (typeof inpt === "string" && inpt.startsWith("microlink.call:")) { | ||
const method = inpt.replace(/^microlink.call:/, ""); | ||
} else if (typeof inpt === "string" && inpt.startsWith(_enums.DEFAULT_FUNCTION_PREFIX)) { | ||
const method = inpt.replace(_enums.DEFAULT_FUNCTION_PREFIX, ""); | ||
const runInBatch = (0, _batch.default)(params => { | ||
@@ -48,5 +49,6 @@ return (0, _batchcall.default)(it, params, { | ||
return function () { | ||
const params = Array.from(arguments); | ||
return runInBatch({ | ||
method, | ||
params: Array.from(arguments) | ||
params | ||
}); | ||
@@ -63,2 +65,16 @@ }; | ||
// }; | ||
} else if (typeof inpt === "string" && inpt.startsWith(_enums.DEFAULT_PROMISE_PREFIX)) { | ||
const method = inpt.replace(_enums.DEFAULT_PROMISE_PREFIX, ""); | ||
const runInBatch = (0, _batch.default)(params => { | ||
return (0, _batchcall.default)(it, params, { | ||
debug_level | ||
}); | ||
}, { | ||
size: batch_size, | ||
wait: batch_wait | ||
}); | ||
return runInBatch({ | ||
method, | ||
params: [] | ||
}); | ||
} else { | ||
@@ -65,0 +81,0 @@ return inpt; |
@@ -8,2 +8,5 @@ "use strict"; | ||
var _deserialize = _interopRequireDefault(require("./deserialize.js")); | ||
var _serialize = _interopRequireDefault(require("./serialize.js")); | ||
var _enums = require("./enums.js"); | ||
var _errors = require("./errors.js"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -14,2 +17,3 @@ function expose(obj, options) { | ||
const debug_level = options && options.debug_level; | ||
const all_funcs = {}; | ||
const onmessage = async evt => { | ||
@@ -20,4 +24,31 @@ let { | ||
if (debug_level >= 2) console.log("[microlink.expose] received message data", data); | ||
if (Array.isArray(data) && data.length >= 1 && data[0].jsonrpc === "2.0" && data[0].method) { | ||
if (debug_level >= 2) console.log("[microlink.call] top thread received batch request"); | ||
if (!all_funcs) throw new Error("[microlink.call] no callable functions"); | ||
const results = await Promise.all(data.map(async req => { | ||
if (typeof all_funcs[req.method] !== "function") { | ||
return (0, _errors.MethodNotFound)({ | ||
id: req.id, | ||
method: req.method | ||
}); | ||
} | ||
try { | ||
const result = await all_funcs[req.method](...req.params); | ||
return { | ||
jsonrpc: "2.0", | ||
result, | ||
id: req.id | ||
}; | ||
} catch (error) { | ||
return (0, _errors.InternalError)({ | ||
id, | ||
error | ||
}); | ||
} | ||
})); | ||
if (debug_level >= 2) console.log("[microlink.call] exposed thread posting results to main thread:", results); | ||
return postMessage(results); | ||
} | ||
if (typeof data !== "object") return; | ||
if (!data.jsonrpc === "2.0") return; | ||
if (data.jsonrpc !== "2.0") return; | ||
if (!data.method) return; | ||
@@ -39,10 +70,6 @@ const { | ||
if (debug_level >= 2) console.error("[microlink.expose] method not found: " + method); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
error: { | ||
code: -32601, | ||
message: "Method not found" | ||
}, | ||
id | ||
}); | ||
return postMessage((0, _errors.MethodNotFound)({ | ||
id, | ||
method | ||
})); | ||
} | ||
@@ -56,6 +83,15 @@ try { | ||
const result = await obj[method](...deserialized_params); | ||
if (debug_level >= 2) console.log("[microlink.expose] posting result for " + method + ": " + JSON.stringify(result)); | ||
const [serialized_result, funcs] = (0, _serialize.default)(result, { | ||
function_prefix: _enums.DEFAULT_FUNCTION_PREFIX, | ||
promise_prefix: _enums.DEFAULT_PROMISE_PREFIX | ||
}); | ||
if (debug_level >= 2) console.log("[microlink.expose]", method, "result", result, "serialized to", [serialized_result, funcs]); | ||
Object.assign(all_funcs, funcs); | ||
// encode result in case it returns a function or promise | ||
if (debug_level >= 2) console.log("[microlink.expose] posting serialized result for " + method + ": " + JSON.stringify(serialized_result)); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
result, | ||
result: serialized_result, | ||
id | ||
@@ -65,10 +101,6 @@ }); | ||
console.error("[microlink.expose] error:", error); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
error: { | ||
code: -32603, | ||
message: "Internal error" | ||
}, | ||
return postMessage((0, _errors.InternalError)({ | ||
error, | ||
id | ||
}); | ||
})); | ||
} | ||
@@ -75,0 +107,0 @@ }; |
@@ -7,5 +7,6 @@ "use strict"; | ||
exports.default = serialize; | ||
var _enums = require("./enums.js"); | ||
/** | ||
* @name serialize | ||
* @description convert functions at any level of nesting to strings | ||
* @description convert functions and promises at any level of nesting to strings | ||
* @param {any} it | ||
@@ -15,13 +16,23 @@ * @param {String} prefix - add to beginning of function ids | ||
*/ | ||
function serialize(things, prefix = "func:") { | ||
function serialize(things, { | ||
function_prefix = _enums.DEFAULT_FUNCTION_PREFIX, | ||
promise_prefix = _enums.DEFAULT_PROMISE_PREFIX | ||
} = {}, generate_id) { | ||
const funcs = {}; | ||
const proms = {}; | ||
if (!generate_id) generate_id = () => Math.random(); | ||
function stringify(it) { | ||
if (Array.isArray(it)) { | ||
return it.map(i => stringify(i)); | ||
} else if (typeof it === "function") { | ||
const fid = generate_id(it); | ||
funcs[fid] = it; | ||
return function_prefix + fid; | ||
} else if (typeof it === "object" && typeof it.then === "function") { | ||
const pid = generate_id(it); | ||
proms[pid] = it; | ||
funcs[pid] = () => it; // create function that returns the promise | ||
return promise_prefix + pid; | ||
} else if (typeof it === "object") { | ||
return Object.fromEntries(Object.entries(it).map(([k, v]) => [k, stringify(v)])); | ||
} else if (typeof it === "function") { | ||
const fid = Math.random(); | ||
funcs[fid] = it; | ||
return prefix + fid; | ||
} else { | ||
@@ -32,3 +43,3 @@ return it; | ||
things = stringify(things); | ||
return [things, funcs]; | ||
return [things, funcs, proms]; | ||
} |
@@ -131,3 +131,3 @@ (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
} | ||
},{"./serialize.js":7}],3:[function(require,module,exports){ | ||
},{"./serialize.js":9}],3:[function(require,module,exports){ | ||
"use strict"; | ||
@@ -140,2 +140,3 @@ | ||
var _serialize = _interopRequireDefault(require("./serialize.js")); | ||
var _deserialize = _interopRequireDefault(require("./deserialize.js")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -146,3 +147,3 @@ function call(it, method, params, { | ||
if (!params) params = []; | ||
let [params_serialized, params_functions] = (0, _serialize.default)(params, "microlink.call:"); | ||
let [params_serialized, params_functions] = (0, _serialize.default)(params); | ||
if (debug_level >= 2) { | ||
@@ -207,3 +208,7 @@ console.log("[microlink.call] serialized to ", [params_serialized, params_functions]); | ||
params_functions = null; | ||
resolve(data.result); | ||
const result = (0, _deserialize.default)(it, data.result); | ||
if (debug_level >= 2) { | ||
console.log("[microlink.call] deserialized", data.result, "to", result); | ||
} | ||
resolve(result); | ||
} | ||
@@ -222,3 +227,3 @@ }; | ||
} | ||
},{"./serialize.js":7}],4:[function(require,module,exports){ | ||
},{"./deserialize.js":4,"./serialize.js":9}],4:[function(require,module,exports){ | ||
"use strict"; | ||
@@ -232,2 +237,3 @@ | ||
var _batchcall = _interopRequireDefault(require("./batchcall.js")); | ||
var _enums = require("./enums.js"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -259,4 +265,4 @@ // const cache = {}; | ||
return obj; | ||
} else if (typeof inpt === "string" && inpt.startsWith("microlink.call:")) { | ||
const method = inpt.replace(/^microlink.call:/, ""); | ||
} else if (typeof inpt === "string" && inpt.startsWith(_enums.DEFAULT_FUNCTION_PREFIX)) { | ||
const method = inpt.replace(_enums.DEFAULT_FUNCTION_PREFIX, ""); | ||
const runInBatch = (0, _batch.default)(params => { | ||
@@ -271,5 +277,6 @@ return (0, _batchcall.default)(it, params, { | ||
return function () { | ||
const params = Array.from(arguments); | ||
return runInBatch({ | ||
method, | ||
params: Array.from(arguments) | ||
params | ||
}); | ||
@@ -286,2 +293,16 @@ }; | ||
// }; | ||
} else if (typeof inpt === "string" && inpt.startsWith(_enums.DEFAULT_PROMISE_PREFIX)) { | ||
const method = inpt.replace(_enums.DEFAULT_PROMISE_PREFIX, ""); | ||
const runInBatch = (0, _batch.default)(params => { | ||
return (0, _batchcall.default)(it, params, { | ||
debug_level | ||
}); | ||
}, { | ||
size: batch_size, | ||
wait: batch_wait | ||
}); | ||
return runInBatch({ | ||
method, | ||
params: [] | ||
}); | ||
} else { | ||
@@ -291,3 +312,3 @@ return inpt; | ||
} | ||
},{"./batch.js":1,"./batchcall.js":2}],5:[function(require,module,exports){ | ||
},{"./batch.js":1,"./batchcall.js":2,"./enums.js":5}],5:[function(require,module,exports){ | ||
"use strict"; | ||
@@ -298,4 +319,56 @@ | ||
}); | ||
exports.DEFAULT_PROMISE_PREFIX = exports.DEFAULT_FUNCTION_PREFIX = void 0; | ||
const DEFAULT_FUNCTION_PREFIX = exports.DEFAULT_FUNCTION_PREFIX = "microlink.function:"; | ||
const DEFAULT_PROMISE_PREFIX = exports.DEFAULT_PROMISE_PREFIX = "microlink.promise:"; | ||
},{}],6:[function(require,module,exports){ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.InternalError = InternalError; | ||
exports.MethodNotFound = MethodNotFound; | ||
function InternalError({ | ||
error, | ||
id | ||
}) { | ||
return { | ||
jsonrpc: "2.0", | ||
error: { | ||
code: -32603, | ||
message: "Internal error", | ||
data: { | ||
error: error.message | ||
} | ||
}, | ||
id | ||
}; | ||
} | ||
function MethodNotFound({ | ||
id, | ||
method | ||
}) { | ||
return { | ||
jsonrpc: "2.0", | ||
error: { | ||
code: -32601, | ||
message: "Method not found", | ||
data: { | ||
method | ||
} | ||
}, | ||
id | ||
}; | ||
} | ||
},{}],7:[function(require,module,exports){ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = expose; | ||
var _deserialize = _interopRequireDefault(require("./deserialize.js")); | ||
var _serialize = _interopRequireDefault(require("./serialize.js")); | ||
var _enums = require("./enums.js"); | ||
var _errors = require("./errors.js"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -306,2 +379,3 @@ function expose(obj, options) { | ||
const debug_level = options && options.debug_level; | ||
const all_funcs = {}; | ||
const onmessage = async evt => { | ||
@@ -312,4 +386,31 @@ let { | ||
if (debug_level >= 2) console.log("[microlink.expose] received message data", data); | ||
if (Array.isArray(data) && data.length >= 1 && data[0].jsonrpc === "2.0" && data[0].method) { | ||
if (debug_level >= 2) console.log("[microlink.call] top thread received batch request"); | ||
if (!all_funcs) throw new Error("[microlink.call] no callable functions"); | ||
const results = await Promise.all(data.map(async req => { | ||
if (typeof all_funcs[req.method] !== "function") { | ||
return (0, _errors.MethodNotFound)({ | ||
id: req.id, | ||
method: req.method | ||
}); | ||
} | ||
try { | ||
const result = await all_funcs[req.method](...req.params); | ||
return { | ||
jsonrpc: "2.0", | ||
result, | ||
id: req.id | ||
}; | ||
} catch (error) { | ||
return (0, _errors.InternalError)({ | ||
id, | ||
error | ||
}); | ||
} | ||
})); | ||
if (debug_level >= 2) console.log("[microlink.call] exposed thread posting results to main thread:", results); | ||
return postMessage(results); | ||
} | ||
if (typeof data !== "object") return; | ||
if (!data.jsonrpc === "2.0") return; | ||
if (data.jsonrpc !== "2.0") return; | ||
if (!data.method) return; | ||
@@ -331,10 +432,6 @@ const { | ||
if (debug_level >= 2) console.error("[microlink.expose] method not found: " + method); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
error: { | ||
code: -32601, | ||
message: "Method not found" | ||
}, | ||
id | ||
}); | ||
return postMessage((0, _errors.MethodNotFound)({ | ||
id, | ||
method | ||
})); | ||
} | ||
@@ -348,6 +445,15 @@ try { | ||
const result = await obj[method](...deserialized_params); | ||
if (debug_level >= 2) console.log("[microlink.expose] posting result for " + method + ": " + JSON.stringify(result)); | ||
const [serialized_result, funcs] = (0, _serialize.default)(result, { | ||
function_prefix: _enums.DEFAULT_FUNCTION_PREFIX, | ||
promise_prefix: _enums.DEFAULT_PROMISE_PREFIX | ||
}); | ||
if (debug_level >= 2) console.log("[microlink.expose]", method, "result", result, "serialized to", [serialized_result, funcs]); | ||
Object.assign(all_funcs, funcs); | ||
// encode result in case it returns a function or promise | ||
if (debug_level >= 2) console.log("[microlink.expose] posting serialized result for " + method + ": " + JSON.stringify(serialized_result)); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
result, | ||
result: serialized_result, | ||
id | ||
@@ -357,10 +463,6 @@ }); | ||
console.error("[microlink.expose] error:", error); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
error: { | ||
code: -32603, | ||
message: "Internal error" | ||
}, | ||
return postMessage((0, _errors.InternalError)({ | ||
error, | ||
id | ||
}); | ||
})); | ||
} | ||
@@ -374,3 +476,3 @@ }; | ||
} | ||
},{"./deserialize.js":4}],6:[function(require,module,exports){ | ||
},{"./deserialize.js":4,"./enums.js":5,"./errors.js":6,"./serialize.js":9}],8:[function(require,module,exports){ | ||
"use strict"; | ||
@@ -407,3 +509,3 @@ | ||
} | ||
},{"./expose.js":5,"./wrap.js":8}],7:[function(require,module,exports){ | ||
},{"./expose.js":7,"./wrap.js":10}],9:[function(require,module,exports){ | ||
"use strict"; | ||
@@ -415,5 +517,6 @@ | ||
exports.default = serialize; | ||
var _enums = require("./enums.js"); | ||
/** | ||
* @name serialize | ||
* @description convert functions at any level of nesting to strings | ||
* @description convert functions and promises at any level of nesting to strings | ||
* @param {any} it | ||
@@ -423,13 +526,23 @@ * @param {String} prefix - add to beginning of function ids | ||
*/ | ||
function serialize(things, prefix = "func:") { | ||
function serialize(things, { | ||
function_prefix = _enums.DEFAULT_FUNCTION_PREFIX, | ||
promise_prefix = _enums.DEFAULT_PROMISE_PREFIX | ||
} = {}, generate_id) { | ||
const funcs = {}; | ||
const proms = {}; | ||
if (!generate_id) generate_id = () => Math.random(); | ||
function stringify(it) { | ||
if (Array.isArray(it)) { | ||
return it.map(i => stringify(i)); | ||
} else if (typeof it === "function") { | ||
const fid = generate_id(it); | ||
funcs[fid] = it; | ||
return function_prefix + fid; | ||
} else if (typeof it === "object" && typeof it.then === "function") { | ||
const pid = generate_id(it); | ||
proms[pid] = it; | ||
funcs[pid] = () => it; // create function that returns the promise | ||
return promise_prefix + pid; | ||
} else if (typeof it === "object") { | ||
return Object.fromEntries(Object.entries(it).map(([k, v]) => [k, stringify(v)])); | ||
} else if (typeof it === "function") { | ||
const fid = Math.random(); | ||
funcs[fid] = it; | ||
return prefix + fid; | ||
} else { | ||
@@ -440,5 +553,5 @@ return it; | ||
things = stringify(things); | ||
return [things, funcs]; | ||
return [things, funcs, proms]; | ||
} | ||
},{}],8:[function(require,module,exports){ | ||
},{"./enums.js":5}],10:[function(require,module,exports){ | ||
"use strict"; | ||
@@ -478,2 +591,2 @@ | ||
} | ||
},{"./call.js":3}]},{},[6]); | ||
},{"./call.js":3}]},{},[8]); |
import serialize from "./serialize.js"; | ||
import deserialize from "./deserialize.js"; | ||
@@ -6,3 +7,3 @@ export default function call(it, method, params, { debug_level = 0 } = {}) { | ||
let [params_serialized, params_functions] = serialize(params, "microlink.call:"); | ||
let [params_serialized, params_functions] = serialize(params); | ||
if (debug_level >= 2) { | ||
@@ -65,3 +66,8 @@ console.log("[microlink.call] serialized to ", [params_serialized, params_functions]); | ||
resolve(data.result); | ||
const result = deserialize(it, data.result); | ||
if (debug_level >= 2) { | ||
console.log("[microlink.call] deserialized", data.result, "to", result); | ||
} | ||
resolve(result); | ||
} | ||
@@ -68,0 +74,0 @@ }; |
import batch from "./batch.js"; | ||
import batchcall from "./batchcall.js"; | ||
import { DEFAULT_FUNCTION_PREFIX, DEFAULT_PROMISE_PREFIX } from "./enums.js"; | ||
@@ -19,4 +20,4 @@ // const cache = {}; | ||
return obj; | ||
} else if (typeof inpt === "string" && inpt.startsWith("microlink.call:")) { | ||
const method = inpt.replace(/^microlink.call:/, ""); | ||
} else if (typeof inpt === "string" && inpt.startsWith(DEFAULT_FUNCTION_PREFIX)) { | ||
const method = inpt.replace(DEFAULT_FUNCTION_PREFIX, ""); | ||
const runInBatch = batch( | ||
@@ -29,3 +30,4 @@ params => { | ||
return function () { | ||
return runInBatch({ method, params: Array.from(arguments) }); | ||
const params = Array.from(arguments); | ||
return runInBatch({ method, params }); | ||
}; | ||
@@ -41,2 +43,11 @@ // return async function () { | ||
// }; | ||
} else if (typeof inpt === "string" && inpt.startsWith(DEFAULT_PROMISE_PREFIX)) { | ||
const method = inpt.replace(DEFAULT_PROMISE_PREFIX, ""); | ||
const runInBatch = batch( | ||
params => { | ||
return batchcall(it, params, { debug_level }); | ||
}, | ||
{ size: batch_size, wait: batch_wait } | ||
); | ||
return runInBatch({ method, params: [] }); | ||
} else { | ||
@@ -43,0 +54,0 @@ return inpt; |
import deserialize from "./deserialize.js"; | ||
import serialize from "./serialize.js"; | ||
import { DEFAULT_FUNCTION_PREFIX, DEFAULT_PROMISE_PREFIX } from "./enums.js"; | ||
import { InternalError, MethodNotFound } from "./errors.js"; | ||
@@ -8,2 +11,4 @@ export default function expose(obj, options) { | ||
const all_funcs = {}; | ||
const onmessage = async evt => { | ||
@@ -14,5 +19,25 @@ let { data } = evt; | ||
if (Array.isArray(data) && data.length >= 1 && data[0].jsonrpc === "2.0" && data[0].method) { | ||
if (debug_level >= 2) console.log("[microlink.call] top thread received batch request"); | ||
if (!all_funcs) throw new Error("[microlink.call] no callable functions"); | ||
const results = await Promise.all( | ||
data.map(async req => { | ||
if (typeof all_funcs[req.method] !== "function") { | ||
return MethodNotFound({ id: req.id, method: req.method }); | ||
} | ||
try { | ||
const result = await all_funcs[req.method](...req.params); | ||
return { jsonrpc: "2.0", result, id: req.id }; | ||
} catch (error) { | ||
return InternalError({ id, error }); | ||
} | ||
}) | ||
); | ||
if (debug_level >= 2) console.log("[microlink.call] exposed thread posting results to main thread:", results); | ||
return postMessage(results); | ||
} | ||
if (typeof data !== "object") return; | ||
if (!data.jsonrpc === "2.0") return; | ||
if (data.jsonrpc !== "2.0") return; | ||
@@ -34,10 +59,3 @@ if (!data.method) return; | ||
if (debug_level >= 2) console.error("[microlink.expose] method not found: " + method); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
error: { | ||
code: -32601, | ||
message: "Method not found" | ||
}, | ||
id | ||
}); | ||
return postMessage(MethodNotFound({ id, method })); | ||
} | ||
@@ -49,6 +67,13 @@ | ||
const result = await obj[method](...deserialized_params); | ||
if (debug_level >= 2) console.log("[microlink.expose] posting result for " + method + ": " + JSON.stringify(result)); | ||
const [serialized_result, funcs] = serialize(result, { function_prefix: DEFAULT_FUNCTION_PREFIX, promise_prefix: DEFAULT_PROMISE_PREFIX }); | ||
if (debug_level >= 2) console.log("[microlink.expose]", method, "result", result, "serialized to", [serialized_result, funcs]); | ||
Object.assign(all_funcs, funcs); | ||
// encode result in case it returns a function or promise | ||
if (debug_level >= 2) console.log("[microlink.expose] posting serialized result for " + method + ": " + JSON.stringify(serialized_result)); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
result, | ||
result: serialized_result, | ||
id | ||
@@ -58,10 +83,3 @@ }); | ||
console.error("[microlink.expose] error:", error); | ||
return postMessage({ | ||
jsonrpc: "2.0", | ||
error: { | ||
code: -32603, | ||
message: "Internal error" | ||
}, | ||
id | ||
}); | ||
return postMessage(InternalError({ error, id })); | ||
} | ||
@@ -68,0 +86,0 @@ }; |
@@ -0,4 +1,6 @@ | ||
import { DEFAULT_FUNCTION_PREFIX, DEFAULT_PROMISE_PREFIX } from "./enums.js"; | ||
/** | ||
* @name serialize | ||
* @description convert functions at any level of nesting to strings | ||
* @description convert functions and promises at any level of nesting to strings | ||
* @param {any} it | ||
@@ -8,14 +10,22 @@ * @param {String} prefix - add to beginning of function ids | ||
*/ | ||
export default function serialize(things, prefix = "func:") { | ||
export default function serialize(things, { function_prefix = DEFAULT_FUNCTION_PREFIX, promise_prefix = DEFAULT_PROMISE_PREFIX } = {}, generate_id) { | ||
const funcs = {}; | ||
const proms = {}; | ||
if (!generate_id) generate_id = () => Math.random(); | ||
function stringify(it) { | ||
if (Array.isArray(it)) { | ||
return it.map(i => stringify(i)); | ||
} else if (typeof it === "function") { | ||
const fid = generate_id(it); | ||
funcs[fid] = it; | ||
return function_prefix + fid; | ||
} else if (typeof it === "object" && typeof it.then === "function") { | ||
const pid = generate_id(it); | ||
proms[pid] = it; | ||
funcs[pid] = () => it; // create function that returns the promise | ||
return promise_prefix + pid; | ||
} else if (typeof it === "object") { | ||
return Object.fromEntries(Object.entries(it).map(([k, v]) => [k, stringify(v)])); | ||
} else if (typeof it === "function") { | ||
const fid = Math.random(); | ||
funcs[fid] = it; | ||
return prefix + fid; | ||
} else { | ||
@@ -28,3 +38,3 @@ return it; | ||
return [things, funcs]; | ||
return [things, funcs, proms]; | ||
} |
{ | ||
"name": "microlink", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Comlink Alternative. Built with JSON-RPC.", | ||
@@ -5,0 +5,0 @@ "unpkg": "./dist/microlink.bundle.js", |
@@ -9,2 +9,3 @@ # microlink | ||
- batching | ||
- await promise in another thread | ||
@@ -57,1 +58,2 @@ ## usage | ||
``` | ||
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
60595
24
1461
57