formdata-node
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -32,4 +32,2 @@ "use strict"; | ||
var _isBuffer = _interopRequireDefault(require("./util/isBuffer")); | ||
var _isStream = _interopRequireDefault(require("./util/isStream")); | ||
@@ -43,4 +41,2 @@ | ||
var _concat = _interopRequireDefault(require("./util/concat")); | ||
var _toFile = _interopRequireDefault(require("./util/toFile")); | ||
@@ -53,2 +49,5 @@ | ||
const { | ||
isBuffer | ||
} = Buffer; | ||
const { | ||
isArray | ||
@@ -60,5 +59,3 @@ } = Array; | ||
/** | ||
* FormData implementation for Node.js environments. | ||
* Bult over Readable stream and async generators. | ||
* Can be used to communicate between servers with multipart/form-data format. | ||
* FormData implementation for Node.js. | ||
* | ||
@@ -124,3 +121,3 @@ * @api public | ||
/** | ||
* @type IterableIterator<Promise<Buffer>> | ||
* @type AsyncIterableIterator<Buffer> | ||
* | ||
@@ -131,2 +128,10 @@ * @private | ||
/** | ||
* Returns a string representation of the FormData | ||
* | ||
* @return {string} | ||
*/ | ||
get [_Symbol$toStringTag]() { | ||
return "FormData"; | ||
} | ||
/** | ||
* @param {array} fields – an optional FormData initial fields. | ||
@@ -137,2 +142,4 @@ * Each field must be passed as a collection of the objects | ||
*/ | ||
constructor(fields = null) { | ||
@@ -157,3 +164,3 @@ (0, _initializerDefineProperty2.default)(this, "boundary", _descriptor, this); | ||
this.stream.push((0, _isBuffer.default)(value) ? value : Buffer.from(String(value))); | ||
this.stream.push(isBuffer(value) ? value : Buffer.from(String(value))); | ||
}; | ||
@@ -172,2 +179,4 @@ | ||
/** | ||
* Returns a mime type by field's filename | ||
* | ||
* @private | ||
@@ -181,2 +190,4 @@ */ | ||
/** | ||
* Returns a headers for given field's data | ||
* | ||
* @private | ||
@@ -187,11 +198,12 @@ */ | ||
__getHeader(name, filename) { | ||
const head = [this.__dashes, this.boundary, this.__carriage, "Content-Disposition: form-data; ", `name="${name}"`]; | ||
let header = ""; | ||
header += `${this.__dashes}${this.boundary}${this.__carriage}`; | ||
header += `Content-Disposition: form-data; name="${name}"`; | ||
if (filename) { | ||
head.push(`; filename="${filename}"${this.__carriage}`); | ||
head.push("Content-Type: ", this.__getMime(filename)); | ||
header += `; filename="${filename}"${this.__carriage}`; | ||
header += `Content-Type: ${this.__getMime(filename)}`; | ||
} | ||
head.push(this.__carriage.repeat(2)); | ||
return (0, _concat.default)(head); | ||
return `${header}${this.__carriage.repeat(2)}`; | ||
} | ||
@@ -207,9 +219,11 @@ /** | ||
for (const [name, { | ||
values, | ||
filename | ||
values | ||
}] of this.__content) { | ||
// Set field header | ||
yield this.__getHeader(name, filename); | ||
for (const { | ||
value, | ||
filename | ||
} of values) { | ||
// Set field's header | ||
yield this.__getHeader(name, filename); | ||
for (const value of values) { | ||
if ((0, _isBlob.default)(value)) { | ||
@@ -222,7 +236,7 @@ yield* (0, _getStreamIterator.default)(value.stream()); | ||
yield value; | ||
} | ||
} // Add trailing carriage | ||
} // Add trailing carriage | ||
yield this.__carriage; | ||
yield this.__carriage; | ||
} | ||
} // Add a footer when all fields ended | ||
@@ -295,3 +309,3 @@ | ||
if (filename && !((0, _isBlob.default)(value) || (0, _isStream.default)(value) || (0, _isBuffer.default)(value))) { | ||
if (filename && !((0, _isBlob.default)(value) || (0, _isStream.default)(value) || isBuffer(value))) { | ||
throw new TypeError(`Failed to execute '${methodName}' on 'FormData': ` + "parameter 2 is not one of the following types: ", "ReadableStream | ReadStream | Readable | Buffer | File | Blob"); | ||
@@ -301,3 +315,3 @@ } // Get a filename for Buffer, Blob, File, ReadableStream and Readable values | ||
if ((0, _isBuffer.default)(value) && filename) { | ||
if (isBuffer(value) && filename) { | ||
filename = (0, _path.basename)(filename); | ||
@@ -318,3 +332,3 @@ } else if ((0, _isBlob.default)(value)) { | ||
} | ||
} else if ((0, _isBlob.default)(value) || (0, _isBuffer.default)(value)) { | ||
} else if ((0, _isBlob.default)(value) || isBuffer(value)) { | ||
value = (0, _toFile.default)(value, filename || name || options.filename, options); | ||
@@ -331,4 +345,6 @@ } else { | ||
append, | ||
filename, | ||
values: [value] | ||
values: [{ | ||
value, | ||
filename | ||
}] | ||
}); | ||
@@ -341,4 +357,6 @@ } // Replace a value of the existing field if "set" called | ||
append, | ||
filename, | ||
values: [value] | ||
values: [{ | ||
value, | ||
filename | ||
}] | ||
}); | ||
@@ -353,3 +371,6 @@ } // Do nothing if the field has been created from .set() | ||
field.values.push(value); | ||
field.values.push({ | ||
value, | ||
filename | ||
}); | ||
@@ -363,3 +384,5 @@ this.__content.set(name, field); | ||
* | ||
* @return {number} | ||
* @return {Promise<number | undefined>} | ||
* | ||
* @public | ||
*/ | ||
@@ -369,18 +392,21 @@ | ||
async getComputedLength() { | ||
let length = 0; | ||
if (this.__content.size === 0) { | ||
return 0; | ||
return length; | ||
} | ||
let length = 0; | ||
const carriageLength = Buffer.from(this.__carriage).length; | ||
for (const [name, { | ||
filename, | ||
values | ||
}] of this.__content) { | ||
length += Buffer.from(this.__getHeader(name, filename)).length; | ||
for (const { | ||
value, | ||
filename | ||
} of values) { | ||
length += Buffer.from(this.__getHeader(name, filename)).length; | ||
const valueLength = await (0, _getLength.default)(value); // Return `undefined` if can't tell field's length | ||
// (it's probably a stream with unknown length) | ||
for (const value of values) { | ||
const valueLength = await (0, _getLength.default)(value); | ||
if (valueLength == null) { | ||
@@ -390,10 +416,7 @@ return undefined; | ||
length += Number(valueLength); | ||
length += Number(valueLength) + carriageLength; | ||
} | ||
length += carriageLength; | ||
} | ||
length += Buffer.from(this.__footer).length; | ||
return length; | ||
return length + Buffer.from(this.__footer).length; | ||
} | ||
@@ -479,3 +502,3 @@ /** | ||
return field.values[0]; | ||
return field.values[0].value; | ||
} | ||
@@ -495,3 +518,5 @@ /** | ||
return field ? Array.from(field.values) : []; | ||
return field ? Array.from(field.values, ({ | ||
value | ||
}) => value) : []; | ||
} | ||
@@ -510,6 +535,2 @@ /** | ||
} | ||
get [_Symbol$toStringTag]() { | ||
return "FormData"; | ||
} | ||
/** | ||
@@ -580,5 +601,6 @@ * @return {IterableIterator<string>} | ||
* This method allows to read a content from internal stream | ||
* using async generators and for-await-of APIs | ||
* using async generators and for-await-of APIs. | ||
* An alias of FormData#stream[Symbol.asyncIterator]() | ||
* | ||
* @return {IterableIterator<Promise<Buffer>>} | ||
* @return {AsyncIterableIterator<Buffer>} | ||
* | ||
@@ -617,3 +639,3 @@ * @public | ||
initializer: function () { | ||
return (0, _concat.default)(["NodeJSFormDataStreamBoundary", (0, _boundary.default)()]); | ||
return `NodeJSFormDataStreamBoundary${(0, _boundary.default)()}`; | ||
} | ||
@@ -626,3 +648,3 @@ }), _descriptor2 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "headers", [_readOnly.default], { | ||
return freeze({ | ||
"Content-Type": (0, _concat.default)(["multipart/form-data; ", "boundary=", this.boundary]) | ||
"Content-Type": `multipart/form-data; boundary=${this.boundary}` | ||
}); | ||
@@ -658,3 +680,3 @@ } | ||
initializer: function () { | ||
return (0, _concat.default)([this.__dashes, this.boundary, this.__dashes, this.__carriage.repeat(2)]); | ||
return `${this.__dashes}${this.boundary}${this.__dashes}` + `${this.__carriage.repeat(2)}`; | ||
} | ||
@@ -661,0 +683,0 @@ }), _descriptor7 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "__defaultContentType", [_readOnly.default], { |
@@ -16,9 +16,11 @@ "use strict"; | ||
var _isBuffer = _interopRequireDefault(require("./isBuffer")); | ||
var _isBlob = _interopRequireDefault(require("./isBlob")); | ||
const { | ||
isBuffer | ||
} = Buffer; | ||
/** | ||
* @api private | ||
*/ | ||
class File { | ||
@@ -40,3 +42,3 @@ constructor(content, name, options = {}) { | ||
if ((0, _isBuffer.default)(content)) { | ||
if (isBuffer(content)) { | ||
const readable = new _stream.Readable({ | ||
@@ -43,0 +45,0 @@ read() {} |
@@ -16,7 +16,8 @@ "use strict"; | ||
var _isBuffer = _interopRequireDefault(require("./isBuffer")); | ||
var _isBlob = _interopRequireDefault(require("./isBlob")); | ||
const { | ||
isBuffer | ||
} = Buffer; | ||
const { | ||
stat | ||
@@ -45,3 +46,3 @@ } = _fs.promises; | ||
if ((0, _isBuffer.default)(value)) { | ||
if (isBuffer(value)) { | ||
return value.length; | ||
@@ -48,0 +49,0 @@ } |
@@ -10,4 +10,2 @@ "use strict"; | ||
var _isBuffer = _interopRequireDefault(require("./isBuffer")); | ||
var _isBlob = _interopRequireDefault(require("./isBlob")); | ||
@@ -17,5 +15,9 @@ | ||
const { | ||
isBuffer | ||
} = Buffer; | ||
/** | ||
* @api private | ||
*/ | ||
function toFile(value, name, options = {}) { | ||
@@ -26,3 +28,3 @@ if (value.constructor.name === "File") { | ||
if ((0, _isBuffer.default)(value)) { | ||
if (isBuffer(value)) { | ||
options.size = value.length; | ||
@@ -29,0 +31,0 @@ } else if ((0, _isBlob.default)(value)) { |
{ | ||
"name": "formdata-node", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "FormData implementation for Node.js. Built over Readable stream and async generators.", | ||
@@ -10,3 +10,6 @@ "repository": "octet-stream/form-data", | ||
"async-iterator", | ||
"stream" | ||
"stream", | ||
"form", | ||
"upload", | ||
"files-upload" | ||
], | ||
@@ -62,3 +65,3 @@ "author": "Nick K. <nick.kruchinin@gmail.com>", | ||
"@octetstream/eslint-config": "4.0.0", | ||
"@types/node": "13.1.1", | ||
"@types/node": "13.1.4", | ||
"ava": "2.4.0", | ||
@@ -76,3 +79,3 @@ "babel-plugin-add-module-exports": "1.0.2", | ||
"rimraf": "3.0.0", | ||
"sinon": "8.0.1", | ||
"sinon": "8.0.2", | ||
"supertest": "4.0.2", | ||
@@ -79,0 +82,0 @@ "then-busboy": "4.4.0", |
@@ -138,6 +138,6 @@ # FormData | ||
##### `getComputedLength() -> {Promise<number>}` | ||
##### `getComputedLength() -> {Promise<number | undefined>}` | ||
Returns computed length of the FormData content. If FormData instance contains | ||
a field with stream.Readable value, this method will always return `undefined`. | ||
a stream value with unknown length, the method will always return `undefined`. | ||
@@ -170,3 +170,3 @@ ##### `forEach(callback[, ctx]) -> {void}` | ||
##### `[Symbol.asyncIterator]() -> {IterableIterator<Promise<Buffer>>}` | ||
##### `[Symbol.asyncIterator]() -> {AsyncIterableIterator<Buffer>}` | ||
@@ -173,0 +173,0 @@ Returns an async iterator allowing to read a data from internal Readable stream using **for-await** syntax. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1847
68054
40