UNPKG

ram64

Version:

Multi-threaded 64bit memory cache database inspired by redis-like features

57 lines (56 loc) 2.09 kB
"use strict"; exports.processRequest = processRequest; exports.REQUESTS_PENDING = void 0; var _hash = require("../util/hash"); var _rand = require("../util/rand"); const REQUESTS_PENDING = new Map(); exports.REQUESTS_PENDING = REQUESTS_PENDING; async function processRequest(instance, req, transferList) { if (!('commandIndex' in req)) { return; // ignore, not for us } function handler() { let { workerOrPort: workerOrPort1 , key: key1 , resumeKey , keys , commandIndex , args } = req; if (!workerOrPort1 && !key1 && !resumeKey) { if (keys) { return Promise.all(keys.map((key)=>processRequest(instance, { key, commandIndex, args }))); } else { return Promise.all(instance.workerPorts.map((workerOrPort)=>processRequest(instance, { workerOrPort, commandIndex, args }))); } } if (!workerOrPort1 && key1 !== undefined) { workerOrPort1 = instance.getPortFromHash((0, _hash).getHash(key1)); } else if (!workerOrPort1 && resumeKey !== undefined) { workerOrPort1 = instance.workerPorts[Number(resumeKey.split(':')[0])]; } if (!workerOrPort1) throw new Error(`Port not found from '${key1 || resumeKey}'`); const requestId = (0, _rand).randomString(); const reqPromise = new Promise((resolve, reject)=>{ REQUESTS_PENDING.set(requestId, { requestId, resolve, reject }); }); const res = { ram64: true, key: key1, commandIndex, requestId, args }; workerOrPort1.postMessage(res, transferList); return reqPromise; } // connect cannot be queued return !req.commandIndex ? handler() : instance.limit(handler); } //# sourceMappingURL=process-request.js.map