mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 14:49:53 +00:00
549ce23ce8
* All projects into monorepo * Update CI * More CI updates * Extracted a bunch of things into packages * Publish - insomnia-plugin-base64@1.0.1 - insomnia-plugin-default-headers@1.0.2 - insomnia-plugin-file@1.0.1 - insomnia-plugin-hash@1.0.1 - insomnia-plugin-now@1.0.1 - insomnia-plugin-request@1.0.1 - insomnia-plugin-response@1.0.1 - insomnia-plugin-uuid@1.0.1 - insomnia-cookies@0.0.2 - insomnia-importers@1.5.2 - insomnia-prettify@0.0.3 - insomnia-url@0.0.2 - insomnia-xpath@0.0.2 * A bunch of small fixes * Improved build script * Fixed * Merge dangling files * Usability refactor * Handle duplicate plugin names
96 lines
2.7 KiB
JavaScript
96 lines
2.7 KiB
JavaScript
// @flow
|
|
import * as electron from 'electron';
|
|
import mimes from 'mime-types';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import type {RequestBodyParameter} from '../models/request';
|
|
|
|
export const DEFAULT_BOUNDARY = 'X-INSOMNIA-BOUNDARY';
|
|
|
|
export async function buildMultipart (params: Array<RequestBodyParameter>) {
|
|
return new Promise(async (resolve: Function, reject: Function) => {
|
|
const filePath = path.join(electron.remote.app.getPath('temp'), Math.random() + '.body');
|
|
const writeStream = fs.createWriteStream(filePath);
|
|
const lineBreak = '\r\n';
|
|
let totalSize = 0;
|
|
|
|
function addFile (path: string): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
let size;
|
|
try {
|
|
size = fs.statSync(path).size;
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
const stream = fs.createReadStream(path);
|
|
stream.once('end', () => {
|
|
resolve();
|
|
});
|
|
stream.pipe(writeStream, {end: false});
|
|
totalSize += size;
|
|
});
|
|
}
|
|
|
|
const addString = (v: string) => {
|
|
const buffer = Buffer.from(v);
|
|
writeStream.write(buffer);
|
|
totalSize += buffer.length;
|
|
};
|
|
|
|
for (const param of params) {
|
|
const noName = !param.name;
|
|
const noValue = !(param.value || param.fileName);
|
|
|
|
if (noName && noValue) {
|
|
continue;
|
|
}
|
|
|
|
addString(`--${DEFAULT_BOUNDARY}`);
|
|
addString(lineBreak);
|
|
|
|
if (param.type === 'file' && param.fileName) {
|
|
const name = param.name || '';
|
|
const fileName = param.fileName;
|
|
const contentType = mimes.lookup(fileName) || 'application/octet-stream';
|
|
addString(
|
|
'Content-Disposition: form-data; ' +
|
|
`name="${name.replace(/"/g, '\\"')}"; ` +
|
|
`filename="${path.basename(fileName).replace(/"/g, '\\"')}"`
|
|
);
|
|
addString(lineBreak);
|
|
addString(`Content-Type: ${contentType}`);
|
|
addString(lineBreak);
|
|
addString(lineBreak);
|
|
try {
|
|
await addFile(fileName);
|
|
} catch (err) {
|
|
return reject(err);
|
|
}
|
|
} else {
|
|
const name = param.name || '';
|
|
const value = param.value || '';
|
|
addString(`Content-Disposition: form-data; name="${name}"`);
|
|
addString(lineBreak);
|
|
addString(lineBreak);
|
|
addString(value);
|
|
}
|
|
|
|
addString(lineBreak);
|
|
}
|
|
|
|
addString(`--${DEFAULT_BOUNDARY}--`);
|
|
addString(lineBreak);
|
|
|
|
writeStream.on('error', err => {
|
|
reject(err);
|
|
});
|
|
|
|
writeStream.on('close', () => {
|
|
resolve({boundary: DEFAULT_BOUNDARY, filePath, contentLength: totalSize});
|
|
});
|
|
|
|
// We're done here. End the stream and tell FS to save/close the file.
|
|
writeStream.end();
|
|
});
|
|
}
|