UNPKG

memcached-node

Version:
92 lines (91 loc) 2.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var RETURN = "\r\n"; var ResponseCode; (function (ResponseCode) { ResponseCode["END"] = "END"; ResponseCode["STORED"] = "STORED"; ResponseCode["NOT_STORED"] = "NOT_STORED"; ResponseCode["NOT_FOUND"] = "NOT_FOUND"; ResponseCode["EXISTS"] = "EXISTS"; ResponseCode["DELETED"] = "DELETED"; ResponseCode["TOUCHED"] = "TOUCHED"; ResponseCode["ERROR"] = "ERROR"; ResponseCode["CLIENT_ERROR"] = "CLIENT_ERROR"; ResponseCode["SERVER_ERROR"] = "SERVER_ERROR"; })(ResponseCode = exports.ResponseCode || (exports.ResponseCode = {})); var codes = [ ResponseCode.END, ResponseCode.STORED, ResponseCode.NOT_STORED, ResponseCode.NOT_FOUND, ResponseCode.EXISTS, ResponseCode.DELETED, ResponseCode.TOUCHED, ResponseCode.ERROR, ResponseCode.CLIENT_ERROR, ResponseCode.SERVER_ERROR, ]; function parseResponseCode(chunk) { var str = chunk.toString(); var match = str.match(/^[A-Z_]+\r\n$/); if (match) { return match[0].replace(/\r\n$/, ""); } var tail = str.match(/\r\n[A-Z_]+\r\n$/); if (tail) { return tail[0].replace(/\r\n/g, ""); } throw new Error("no matching error"); } exports.parseResponseCode = parseResponseCode; function parseResponse(chunk) { var base = 0; var metadata = {}; do { var start = chunk.indexOf(RETURN, base); var meta = chunk.slice(base, start).toString("utf8").split(" "); start += RETURN.length; if (codes.includes(meta[0])) { break; } var value = chunk.slice(start, start + parseInt(meta[3], 10)); metadata[meta[1]] = { key: meta[1], flags: parseInt(meta[2], 10), bytes: parseInt(meta[3], 10), value: value.toString("utf8"), }; if (meta.length > 3 && meta[4]) { metadata[meta[1]].casId = parseInt(meta[4], 10); } base = start + parseInt(meta[3]) + RETURN.length; } while (true); return metadata; } exports.parseResponse = parseResponse; function parseStatResponse(chunk) { var base = 0; var stats = {}; do { var start = chunk.indexOf(RETURN, base); var meta = chunk.slice(base, start).toString("utf8").split(" "); start += RETURN.length; if (meta[0] === ResponseCode.END) { break; } var key = meta[1]; var value = Number(meta[2]); if (value === NaN) { value = meta[2]; } stats[key] = value; base = start; } while (true); return stats; } exports.parseStatResponse = parseStatResponse; exports.default = { parseResponseCode: parseResponseCode, parseStatResponse: parseStatResponse, };