Innovenergy_trunk/frontend/node_modules/testcafe-hammerhead/lib/upload/form-data-entry.js

93 lines
3.7 KiB
JavaScript
Raw Normal View History

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bufferUtils = __importStar(require("../utils/buffer"));
const builtin_header_names_1 = __importDefault(require("../request-pipeline/builtin-header-names"));
const INPUT_NAME_RE = /;\s*name="([^"]*)"/i;
const FILE_NAME_RE = /;\s*filename="([^"]*)"/i;
const HEADER_RE = /^(.+?):\s*(.*)$/;
class FormDataEntry {
constructor() {
this._headers = new Map();
this.body = [];
this.name = '';
this.fileName = '';
}
_parseContentDisposition(contentDisposition) {
const inputNameMatch = contentDisposition.match(INPUT_NAME_RE);
const fileNameMatch = contentDisposition.match(FILE_NAME_RE);
this.name = inputNameMatch && inputNameMatch[1] || '';
this.fileName = fileNameMatch && fileNameMatch[1] || '';
}
_setHeader(name, value, rawHeader) {
if (!this._headers.has(name))
this._headers.set(name, { rawName: typeof rawHeader === 'string' ? rawHeader : name, value });
else {
const header = this._headers.get(name);
if (header)
header.value = value;
}
}
_setContentDisposition(name, fileName) {
this.name = name;
this.fileName = fileName;
this._setHeader(builtin_header_names_1.default.contentDisposition, `form-data; name="${name}"; filename="${fileName}"`);
}
// API
addFileInfo(fileInfo, idx) {
const file = fileInfo.files[idx];
this._setContentDisposition(fileInfo.name, file.name);
this.body = [Buffer.from(file.data, 'base64')];
this._setHeader(builtin_header_names_1.default.contentType, file.type);
}
setRawHeader(rawHeader) {
const [, rawName = '', value = ''] = rawHeader.match(HEADER_RE) || [];
const name = rawName.toLowerCase();
this._headers.set(name, { rawName, value });
if (name === builtin_header_names_1.default.contentDisposition)
this._parseContentDisposition(value);
}
toBuffer() {
const chunks = [];
for (const { rawName, value } of this._headers.values()) {
chunks.push(Buffer.from(`${rawName}: ${value}`));
chunks.push(bufferUtils.CRLF);
}
chunks.push(bufferUtils.CRLF);
return Buffer.concat(chunks.concat(this.body));
}
cloneWithRawHeaders() {
const entry = new FormDataEntry();
for (const [name, { rawName }] of this._headers)
entry._setHeader(name, '', rawName);
return entry;
}
}
exports.default = FormDataEntry;module.exports = exports.default;