biketrack-app/node_modules/@ethersphere/bee-js/dist/cjs/utils/headers.js

60 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-07-11 10:27:11 +02:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractUploadHeaders = exports.readFileHeaders = void 0;
const error_1 = require("./error");
/**
* Read the filename from the content-disposition header
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
*
* @param header the content-disposition header value
*
* @returns the filename
*/
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 readFileHeaders(headers) {
const name = readContentDispositionFilename(headers.get('content-disposition'));
const tagUid = readTagUid(headers.get('swarm-tag-uid'));
const contentType = headers.get('content-type') || undefined;
return {
name,
tagUid,
contentType,
};
}
exports.readFileHeaders = readFileHeaders;
function extractUploadHeaders(postageBatchId, options) {
if (!postageBatchId) {
throw new error_1.BeeError('Postage BatchID has to be specified!');
}
const headers = {
'swarm-postage-batch-id': postageBatchId,
};
if (options === null || options === void 0 ? void 0 : options.pin)
headers['swarm-pin'] = String(options.pin);
if (options === null || options === void 0 ? void 0 : options.encrypt)
headers['swarm-encrypt'] = String(options.encrypt);
if (options === null || options === void 0 ? void 0 : options.tag)
headers['swarm-tag'] = String(options.tag);
if (typeof (options === null || options === void 0 ? void 0 : options.deferred) === 'boolean')
headers['swarm-deferred-upload'] = options.deferred.toString();
return headers;
}
exports.extractUploadHeaders = extractUploadHeaders;