UNPKG

@ethersphere/bee-js

Version:
121 lines (120 loc) 4.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareRequestHeaders = exports.readFileHeaders = void 0; const cafe_utility_1 = require("cafe-utility"); const error_1 = require("./error"); const stamps_1 = require("./stamps"); const typed_bytes_1 = require("./typed-bytes"); function readFileHeaders(headers) { const name = readContentDispositionFilename(headers['content-disposition']); const tagUid = readTagUid(headers['swarm-tag-uid']); const contentType = headers['content-type'] || undefined; return { name, tagUid, contentType, }; } exports.readFileHeaders = readFileHeaders; function readContentDispositionFilename(header) { if (!header) { throw new error_1.BeeError('missing content-disposition header'); } // Regex was found here // https://stackoverflow.com/questions/23054475/javascript-regex-for-extracting-filename-from-content-disposition-header const dispositionMatch = header.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i); if (dispositionMatch && dispositionMatch.length > 0) { return dispositionMatch[1]; } throw new error_1.BeeError('invalid content-disposition header'); } function readTagUid(header) { if (!header) { return undefined; } return parseInt(header, 10); } function prepareRequestHeaders(stamp, nullableOptions) { const headers = {}; if (isEnvelopeWithBatchId(stamp)) { headers['swarm-postage-stamp'] = (0, stamps_1.convertEnvelopeToMarshaledStamp)(stamp).toHex(); } else if (stamp) { stamp = new typed_bytes_1.BatchId(stamp); headers['swarm-postage-batch-id'] = stamp.toHex(); } if (!nullableOptions) { return headers; } const options = cafe_utility_1.Types.asObject(nullableOptions); if (options.size) { headers['content-length'] = String(options.size); } if (options.contentType) { headers['content-type'] = String(options.contentType); } if (options.redundancyLevel) { headers['swarm-redundancy-level'] = String(options.redundancyLevel); } if (cafe_utility_1.Types.isBoolean(options.act)) { headers['swarm-act'] = String(options.act); } if (cafe_utility_1.Types.isBoolean(options.pin)) { headers['swarm-pin'] = String(options.pin); } if (cafe_utility_1.Types.isBoolean(options.encrypt)) { headers['swarm-encrypt'] = options.encrypt.toString(); } if (options.tag) { headers['swarm-tag'] = String(options.tag); } if (cafe_utility_1.Types.isBoolean(options.deferred)) { headers['swarm-deferred-upload'] = options.deferred.toString(); } if (options.redundancyStrategy) { headers['swarm-redundancy-strategy'] = String(options.redundancyStrategy); } if (cafe_utility_1.Types.isBoolean(options.fallback)) { headers['swarm-redundancy-fallback-mode'] = options.fallback.toString(); } if (options.timeoutMs) { headers['swarm-chunk-retrieval-timeout'] = String(options.timeoutMs); } if (options.indexDocument) { headers['swarm-index-document'] = String(options.indexDocument); } if (options.errorDocument) { headers['swarm-error-document'] = String(options.errorDocument); } if (options.actPublisher) { headers['swarm-act-publisher'] = new typed_bytes_1.PublicKey(options.actPublisher).toCompressedHex(); } if (options.actHistoryAddress) { headers['swarm-act-history-address'] = new typed_bytes_1.Reference(options.actHistoryAddress).toHex(); } if (options.actTimestamp) { headers['swarm-act-timestamp'] = String(options.actTimestamp); } if (options.actPublisher || options.actHistoryAddress || options.actTimestamp) { headers['swarm-act'] = 'true'; } if (options.gasPrice) { headers['gas-price'] = String(options.gasPrice); } if (options.gasLimit) { headers['gas-limit'] = String(options.gasLimit); } return headers; } exports.prepareRequestHeaders = prepareRequestHeaders; function isEnvelopeWithBatchId(value) { if (!cafe_utility_1.Types.isObject(value)) { return false; } const envelope = value; return (envelope.issuer !== undefined && envelope.index !== undefined && envelope.signature !== undefined && envelope.timestamp !== undefined && envelope.batchId !== undefined); }