node-fetch/src/utils/form-data.js

79 lines
1.6 KiB
JavaScript
Raw Normal View History

import {randomBytes} from 'crypto';
import {isBlob} from './is.js';
const carriage = '\r\n';
const dashes = '-'.repeat(2);
const carriageLength = Buffer.byteLength(carriage);
/**
* @param {string} boundary
*/
const getFooter = boundary => `${dashes}${boundary}${dashes}${carriage.repeat(2)}`;
/**
* @param {string} boundary
* @param {string} name
* @param {*} field
*
* @return {string}
*/
function getHeader(boundary, name, field) {
let header = '';
header += `${dashes}${boundary}${carriage}`;
header += `Content-Disposition: form-data; name="${name}"`;
if (isBlob(field)) {
header += `; filename="${field.name}"${carriage}`;
header += `Content-Type: ${field.type || 'application/octet-stream'}`;
}
return `${header}${carriage.repeat(2)}`;
}
/**
* @return {string}
*/
export const getBoundary = () => randomBytes(8).toString('hex');
/**
* @param {FormData} form
* @param {string} boundary
*/
export async function * formDataIterator(form, boundary) {
for (const [name, value] of form) {
yield getHeader(boundary, name, value);
if (isBlob(value)) {
yield * value.stream();
} else {
yield value;
}
yield carriage;
}
yield getFooter(boundary);
}
/**
* @param {FormData} form
* @param {string} boundary
*/
export function getFormDataLength(form, boundary) {
let length = 0;
for (const [name, value] of form) {
length += Buffer.byteLength(getHeader(boundary, name, value));
Require Node.js 12.20.0 and move to ESM (#1141) * Use ESM import in runkit example file * Update dependencies, version and transition to ESM * Use ESM imports, add ESM-related info * Remove rollup * Lint TypeScript-related files * Update dependency * Lint & update dependency * Lint * Remove commonjs tests * chore: update changelog * Remove commonjs GitHub action * Update funding.yml * Update linter rules * Lint * Fix tsd * Remove unnecessary types * Simplify * Use top-level await * Update GitHub Actions * Use Mocha with ESM * Revamp * specify what node version * update formdata-node dep * remove lint from example using top await * updated name and link to formdata-polyfill * Stop recommend form-data * filter example - it has many duplicate variables * Update type definitions to ESM * Remove unused lint rule disable comment * Remove leftover rollup and dist folder * updated depn * updated d.ts * lint * Fix breaking changes with blob v3 stream() * revert eslint comment * revert back to xo 0.39 Don't want to deal with all those new rules right now. will fix it later fixed some of them... * none TS fan trying to fix type definition * Give me a break * Test on all minimum supported Node.js versions (#1170) * Test on all minimum supported Node.js versions * Tweak Node.js workaround version range * Handle Node.js 16 aborted error message * fix node version string compare Co-authored-by: Jimmy Wärting <[email protected]> * bumped fetch-blob version * import from dom lib * rm unused comment * updated required version in docs * fixed named import * set lowest support to 12.20.0 * comment explaining both * rm log Co-authored-by: Jimmy Wärting <[email protected]> Co-authored-by: Linus Unnebäck <[email protected]>
2021-07-18 13:15:19 -07:00
length += isBlob(value) ? value.size : Buffer.byteLength(String(value));
length += carriageLength;
}
length += Buffer.byteLength(getFooter(boundary));
return length;
}