UNPKG

@ipfn/cell-codecs

Version:
30 lines (29 loc) 995 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const multicodec = require("multicodec"); const util_1 = require("multicodec/src/util"); const codecs_1 = require("./codecs"); function prepend(body, name) { const codec = codecs_1.codecByName(name); if (!codec.hex) { return body; } return multicodec.addPrefix(name, body); } exports.prepend = prepend; function splitPrefix(body) { if (isJSONObject(body)) { const codec = codecs_1.codecByName('cell-json-v1'); return [codec, body]; } const buffer = body instanceof Buffer ? body : Buffer.from(body); const prefix = util_1.varintBufferDecode(buffer).toString('hex'); const codec = codecs_1.codecByHex(prefix); return [codec, multicodec.rmPrefix(buffer)]; } exports.splitPrefix = splitPrefix; function isJSONObject(body) { body = body.toString(); const length = body.length; return length >= 2 && body[0] === '{' && body[length - 1] === '}'; }