formdata-node
Advanced tools
Comparing version 1.8.1 to 2.0.0
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _toStringTag = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/symbol/to-string-tag")); | ||
var _initializerDefineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/initializerDefineProperty")); | ||
var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/entries")); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/keys")); | ||
var _applyDecoratedDescriptor2 = _interopRequireDefault(require("@babel/runtime/helpers/applyDecoratedDescriptor")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator")); | ||
var _initializerWarningHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/initializerWarningHelper")); | ||
var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/map")); | ||
var _stream = require("stream"); | ||
var _asyncIterator2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/symbol/async-iterator")); | ||
var _util = require("util"); | ||
var _iterator = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/symbol/iterator")); | ||
var _path = require("path"); | ||
var _from = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/from")); | ||
var _values = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/values")); | ||
var _repeat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/repeat")); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty")); | ||
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); | ||
var _wrapAsyncGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/wrapAsyncGenerator")); | ||
var _awaitAsyncGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/awaitAsyncGenerator")); | ||
var _asyncIterator3 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncIterator")); | ||
var _asyncGeneratorDelegate2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncGeneratorDelegate")); | ||
var _stream = _interopRequireDefault(require("stream")); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _mimeTypes = _interopRequireDefault(require("mime-types")); | ||
var _invariant = _interopRequireDefault(require("@octetstream/invariant")); | ||
var _getStreamIterator = _interopRequireDefault(require("./util/getStreamIterator")); | ||
var _StreamIterator = _interopRequireDefault(require("./util/StreamIterator")); | ||
var _isFunction = _interopRequireDefault(require("./util/isFunction")); | ||
var _getLength = _interopRequireDefault(require("./util/getLength")); | ||
var _isString = _interopRequireDefault(require("./util/isString")); | ||
var _isObject = _interopRequireDefault(require("./util/isObject")); | ||
@@ -71,3 +38,3 @@ | ||
var _getType = _interopRequireDefault(require("./util/getType")); | ||
var _readOnly = _interopRequireDefault(require("./util/readOnly")); | ||
@@ -78,5 +45,14 @@ var _isBlob = _interopRequireDefault(require("./util/isBlob")); | ||
var _bind = _interopRequireDefault(require("./util/bind")); | ||
var _toFile = _interopRequireDefault(require("./util/toFile")); | ||
const isArray = _isArray.default; | ||
let _Symbol$toStringTag, _Symbol$iterator, _Symbol$asyncIterator, _inspect$custom; | ||
var _class, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _temp; | ||
const { | ||
isArray | ||
} = Array; | ||
const { | ||
freeze | ||
} = Object; | ||
/** | ||
@@ -90,35 +66,79 @@ * FormData implementation for Node.js environments. | ||
var _Symbol$toStringTag = _toStringTag.default; | ||
var _Symbol$iterator = _iterator.default; | ||
var _Symbol$asyncIterator = _asyncIterator2.default; | ||
let FormData = (_class = (_temp = (_Symbol$toStringTag = Symbol.toStringTag, _Symbol$iterator = Symbol.iterator, _Symbol$asyncIterator = Symbol.asyncIterator, _inspect$custom = _util.inspect.custom, class FormData { | ||
/** | ||
* Generates a new boundary string once FormData instance constructed | ||
* | ||
* @type string | ||
* | ||
* @public | ||
*/ | ||
class FormData { | ||
/** | ||
* Check if given value is instance of FormData | ||
* Note: This method is not a part of client-side FormData interface. | ||
* Returns headers for multipart/form-data | ||
* | ||
* @param {any} value | ||
* @type object | ||
* | ||
* @return {boolean} | ||
* @public | ||
*/ | ||
/** | ||
* Refers to the internal Readable stream | ||
* | ||
* @type stream.Readable | ||
* | ||
* @public | ||
*/ | ||
static isFormData(value) { | ||
return value instanceof FormData; | ||
} | ||
/** | ||
* @param {array} fields – an optional FormData initial fields. | ||
* Each initial field should be passed as a collection of the objects | ||
* with "name", "value" and "filename" props. | ||
* See the FormData#append for more info about the available format. | ||
* @type string | ||
* | ||
* @private | ||
*/ | ||
/** | ||
* @type string | ||
* | ||
* @private | ||
*/ | ||
constructor(entries = null) { | ||
(0, _defineProperty2.default)(this, "__getMime", filename => _mimeTypes.default.lookup(filename) || this.__defaultContentType); | ||
(0, _defineProperty2.default)(this, "__getFooter", () => { | ||
var _context; | ||
/** | ||
* @type string | ||
* | ||
* @private | ||
*/ | ||
return (0, _concat.default)([this.__dashes, this.__boundary, this.__dashes, (0, _repeat.default)(_context = this.__carriage).call(_context, 2)]); | ||
}); | ||
/** | ||
* @type string | ||
* | ||
* @private | ||
*/ | ||
/** | ||
* @type Map | ||
* | ||
* @private | ||
*/ | ||
/** | ||
* @type IterableIterator<Promise<Buffer>> | ||
* | ||
* @private | ||
*/ | ||
/** | ||
* @param {array} fields – an optional FormData initial fields. | ||
* Each field must be passed as a collection of the objects | ||
* with "name", "value" and "filename" props. | ||
* See the FormData#append() method for more information. | ||
*/ | ||
constructor(fields = null) { | ||
(0, _initializerDefineProperty2.default)(this, "boundary", _descriptor, this); | ||
(0, _initializerDefineProperty2.default)(this, "headers", _descriptor2, this); | ||
(0, _initializerDefineProperty2.default)(this, "stream", _descriptor3, this); | ||
(0, _initializerDefineProperty2.default)(this, "__carriage", _descriptor4, this); | ||
(0, _initializerDefineProperty2.default)(this, "__dashes", _descriptor5, this); | ||
(0, _initializerDefineProperty2.default)(this, "__footer", _descriptor6, this); | ||
(0, _initializerDefineProperty2.default)(this, "__defaultContentType", _descriptor7, this); | ||
(0, _initializerDefineProperty2.default)(this, "__content", _descriptor8, this); | ||
(0, _initializerDefineProperty2.default)(this, "__curr", _descriptor9, this); | ||
(0, _defineProperty2.default)(this, "__read", () => { | ||
@@ -130,55 +150,16 @@ const onFulfilled = ({ | ||
if (done) { | ||
return this.__stream.push(null); | ||
return this.stream.push(null); | ||
} | ||
this.__stream.push((0, _isBuffer.default)(value) ? value : Buffer.from(String(value))); | ||
this.stream.push((0, _isBuffer.default)(value) ? value : Buffer.from(String(value))); | ||
}; | ||
const onRejected = err => this.__stream.emit("error", err); | ||
const onRejected = err => this.stream.emit("error", err); | ||
this.__curr.next().then(onFulfilled).catch(onRejected); | ||
}); | ||
(0, _defineProperty2.default)(this, "__appendFromInitialFields", fields => { | ||
for (const field of fields) { | ||
if ((0, _isObject.default)(field) && !isArray(field)) { | ||
this.append(field.name, field.value, field.filename); | ||
} | ||
} | ||
}); | ||
(0, _defineProperty2.default)(this, "append", (name, value, filename) => this.__setField(name, value, filename, 1)); | ||
(0, _defineProperty2.default)(this, "set", (name, value, filename) => this.__setField(name, value, filename)); | ||
(0, _defineProperty2.default)(this, "has", name => this.__content.has(name)); | ||
(0, _defineProperty2.default)(this, "get", name => { | ||
const field = this.__content.get(name); | ||
(0, _defineProperty2.default)(this, "inspect", this[_util.inspect.custom]); | ||
if (!field) { | ||
return undefined; | ||
} | ||
return (0, _values.default)(field)[0]; | ||
}); | ||
(0, _defineProperty2.default)(this, "getAll", name => { | ||
const field = this.__content.get(name); | ||
return field ? (0, _from.default)((0, _values.default)(field)) : []; | ||
}); | ||
(0, _defineProperty2.default)(this, "delete", name => void this.__content.delete(name)); | ||
(0, _defineProperty2.default)(this, "forEach", (fn, ctx = null) => { | ||
for (const [name, value] of this) { | ||
fn.call(ctx, value, name, this); | ||
} | ||
}); | ||
(0, _bind.default)([_iterator.default, _asyncIterator2.default, "toString", "inspect", "keys", "values", "entries"], this); | ||
this.__carriage = "\r\n"; | ||
this.__defaultContentType = "application/octet-stream"; | ||
this.__dashes = "--"; | ||
this.__boundary = (0, _concat.default)(["NodeJSFormDataStream", (0, _boundary.default)()]); | ||
this.__content = new _map.default(); | ||
this.__curr = this.__getField(); | ||
this.__stream = new _stream.default.Readable({ | ||
read: this.__read | ||
}); | ||
if (isArray(entries)) { | ||
this.__appendFromInitialFields(entries); | ||
if (isArray(fields)) { | ||
this.__appendFromInitialFields(fields); | ||
} | ||
@@ -191,24 +172,22 @@ } | ||
__getMime(filename) { | ||
return _mimeTypes.default.lookup(filename) || this.__defaultContentType; | ||
} | ||
/** | ||
* @private | ||
*/ | ||
__getHeader(name, filename) { | ||
var _context2; | ||
const head = [this.__dashes, this.boundary, this.__carriage, "Content-Disposition: form-data; ", `name="${name}"`]; | ||
const head = [this.__dashes, this.__boundary, this.__carriage, "Content-Disposition: form-data; ", `name="${name}"`]; | ||
if (filename) { | ||
head.push(`; filename="${filename}"${this.__carriage}`); | ||
head.push(`Content-Type: "${this.__getMime(filename)}"`); | ||
head.push("Content-Type: ", this.__getMime(filename)); | ||
} | ||
head.push((0, _repeat.default)(_context2 = this.__carriage).call(_context2, 2)); | ||
head.push(this.__carriage.repeat(2)); | ||
return (0, _concat.default)(head); | ||
} | ||
/** | ||
* @private | ||
*/ | ||
/** | ||
* Get each field from internal Map | ||
@@ -218,31 +197,29 @@ * | ||
*/ | ||
__getField() { | ||
var _this = this; | ||
return (0, _wrapAsyncGenerator2.default)(function* () { | ||
for (const [name, { | ||
values, | ||
filename | ||
}] of _this.__content) { | ||
// Set field header | ||
yield _this.__getHeader(name, filename); | ||
for (const value of values) { | ||
if ((0, _isBlob.default)(value)) { | ||
yield* (0, _asyncGeneratorDelegate2.default)((0, _asyncIterator3.default)((0, _getStreamIterator.default)(value.stream())), _awaitAsyncGenerator2.default); | ||
} else if ((0, _isStream.default)(value)) { | ||
// Read the stream content | ||
yield* (0, _asyncGeneratorDelegate2.default)((0, _asyncIterator3.default)((0, _getStreamIterator.default)(value)), _awaitAsyncGenerator2.default); | ||
} else { | ||
yield value; | ||
} | ||
} // Add trailing carriage | ||
async *__getField() { | ||
for (const [name, { | ||
values, | ||
filename | ||
}] of this.__content) { | ||
// Set field header | ||
yield this.__getHeader(name, filename); | ||
for (const value of values) { | ||
if ((0, _isBlob.default)(value)) { | ||
yield* (0, _getStreamIterator.default)(value.stream()); | ||
} else if ((0, _isStream.default)(value)) { | ||
// Read the stream content | ||
yield* (0, _getStreamIterator.default)(value); | ||
} else { | ||
yield value; | ||
} | ||
} // Add trailing carriage | ||
yield _this.__carriage; | ||
} // Add a footer when all fields ended | ||
yield this.__carriage; | ||
} // Add a footer when all fields ended | ||
yield _this.__getFooter(); | ||
})(); | ||
yield this.__footer; | ||
} | ||
@@ -259,2 +236,22 @@ /** | ||
/** | ||
* Read values from internal storage and push it to the internal strea | ||
this.__curr.next().then(onFulfilled).catch(onRejected) | ||
} | ||
/** | ||
* Append initial fields | ||
* | ||
* @param {array} fields | ||
* | ||
* @return {void} | ||
* | ||
* @private | ||
*/ | ||
__appendFromInitialFields(fields) { | ||
for (const field of fields) { | ||
if ((0, _isObject.default)(field) && !isArray(field)) { | ||
this.append(field.name, field.value, field.filename); | ||
} | ||
} | ||
} | ||
/** | ||
* Appends a new value onto an existing key inside a FormData object, | ||
@@ -274,2 +271,4 @@ * or adds the key if it does not already exist. | ||
* | ||
* @param {object} [options = {}] An object with additional paramerets. | ||
* | ||
* @return {void} | ||
@@ -279,10 +278,27 @@ * | ||
*/ | ||
__setField(name, value, filename, append = false) { | ||
(0, _invariant.default)(!(0, _isString.default)(name), TypeError, "Field name should be a string. Received %s", (0, _getType.default)(name)); | ||
(0, _invariant.default)(filename && !(0, _isString.default)(filename), TypeError, "Filename should be a string (if passed). Received %s", (0, _getType.default)(filename)); // Getting a filename for Buffer and Readable values | ||
__setField(name, value, filename, options, append, argsLength) { | ||
const methodName = append ? "append" : "set"; | ||
if ((0, _isObject.default)(filename)) { | ||
[options, filename] = [filename, undefined]; | ||
} // FormData required at least 2 arguments to be set. | ||
if (argsLength < 2) { | ||
throw new TypeError(`Failed to execute '${methodName}' on 'FormData': ` + `2 arguments required, but only ${argsLength} present.`); | ||
} // FormData requires the second argument to be some kind of binary data | ||
// when a filename has been set. | ||
if (filename && !((0, _isBlob.default)(value) || (0, _isStream.default)(value) || (0, _isBuffer.default)(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"); | ||
} // Get a filename for Buffer, Blob, File, ReadableStream and Readable values | ||
if ((0, _isBuffer.default)(value) && filename) { | ||
filename = _path.default.basename(filename); | ||
filename = (0, _path.basename)(filename); | ||
} else if ((0, _isBlob.default)(value)) { | ||
filename = _path.default.basename(value.name || filename); | ||
filename = (0, _path.basename)(value.name || filename); | ||
} else if ((0, _isStream.default)(value) && (value.path || filename)) { | ||
@@ -292,12 +308,13 @@ // Readable stream which created from fs.createReadStream | ||
// from the stream itself. | ||
filename = _path.default.basename(value.path || filename); | ||
} else { | ||
// TODO: Add an error here if the filename prop is set to fit | ||
// browser implementations behaviour | ||
filename = undefined; | ||
} | ||
filename = (0, _path.basename)(value.path || filename); | ||
} // Normalize field content | ||
append = Boolean(append); | ||
if (!((0, _isStream.default)(value) || (0, _isBuffer.default)(value) || (0, _isBlob.default)(value))) { | ||
if ((0, _isStream.default)(value)) { | ||
if (options.size != null) { | ||
value = (0, _toFile.default)(value, filename || name || options.filename, options); | ||
} | ||
} else if ((0, _isBlob.default)(value) || (0, _isBuffer.default)(value)) { | ||
value = (0, _toFile.default)(value, filename || name || options.filename, options); | ||
} else { | ||
value = String(value); | ||
@@ -332,3 +349,3 @@ } | ||
(0, _values.default)(field).push(value); | ||
field.values.push(value); | ||
@@ -338,36 +355,2 @@ this.__content.set(name, field); | ||
/** | ||
* Returns boundary string | ||
* | ||
* @return {string} | ||
* | ||
* @public | ||
*/ | ||
get boundary() { | ||
return this.__boundary; | ||
} | ||
/** | ||
* Returns headers for multipart/form-data | ||
*/ | ||
get headers() { | ||
return { | ||
"Content-Type": (0, _concat.default)(["multipart/form-data; ", "boundary=", this.boundary]) | ||
}; | ||
} | ||
/** | ||
* Returns the internal stream | ||
* | ||
* @return {stream.Readable} | ||
* | ||
* @public | ||
*/ | ||
get stream() { | ||
return this.__stream; | ||
} | ||
/** | ||
* Returns computed length of the FormData content. | ||
@@ -381,35 +364,31 @@ * If data contains stream.Readable field(s), | ||
getComputedLength() { | ||
var _this2 = this; | ||
async getComputedLength() { | ||
if (this.__content.size === 0) { | ||
return 0; | ||
} | ||
return (0, _asyncToGenerator2.default)(function* () { | ||
if (_this2.__content.size === 0) { | ||
return 0; | ||
} | ||
let length = 0; | ||
const carriageLength = Buffer.from(this.__carriage).length; | ||
let length = 0; | ||
const carriageLength = Buffer.from(_this2.__carriage).length; | ||
for (const [name, { | ||
filename, | ||
values | ||
}] of this.__content) { | ||
length += Buffer.from(this.__getHeader(name, filename)).length; | ||
for (const [name, { | ||
filename, | ||
values | ||
}] of _this2.__content) { | ||
length += Buffer.from(_this2.__getHeader(name, filename)).length; | ||
for (const value of values) { | ||
const valueLength = await (0, _getLength.default)(value); | ||
for (const value of values) { | ||
const valueLength = yield (0, _getLength.default)(value); | ||
if (valueLength == null) { | ||
return undefined; | ||
} | ||
length += Number(valueLength); | ||
if (valueLength == null) { | ||
return undefined; | ||
} | ||
length += carriageLength; | ||
length += Number(valueLength); | ||
} | ||
length += Buffer.from(_this2.__getFooter()).length; | ||
return length; | ||
})(); | ||
length += carriageLength; | ||
} | ||
length += Buffer.from(this.__footer).length; | ||
return length; | ||
} | ||
@@ -437,21 +416,90 @@ /** | ||
append(name, value, filename = undefined, options = {}) { | ||
return this.__setField(name, value, filename, options, true, arguments.length); | ||
} | ||
/** | ||
* Returns a string representation of the FormData | ||
* Set a new value for an existing key inside FormData, | ||
* or add the new field if it does not already exist. | ||
* | ||
* @return {string} | ||
* @param {string} name – The name of the field whose data | ||
* is contained in value | ||
* | ||
* @param {any} value – The field value. You can pass any primitive type | ||
* (including null and undefined), Buffer or Readable stream. | ||
* Note that Arrays and Object will be converted to string | ||
* by using String function. | ||
* | ||
* @param {string} [filename = undefined] A filename of given field. | ||
* Can be added only for Buffer and Readable | ||
* | ||
* @return {void} | ||
* | ||
* @public | ||
*/ | ||
toString() { | ||
return "[object FormData]"; | ||
set(name, value, filename = undefined, options = {}) { | ||
return this.__setField(name, value, filename, options, false, arguments.length); | ||
} | ||
/** | ||
* Returns a string representation of the FormData | ||
* Check if a field with the given name exists inside FormData. | ||
* | ||
* @return {string} | ||
* @param {string} name – A name of the field you want to test for. | ||
* | ||
* @return {boolean} | ||
* | ||
* @public | ||
*/ | ||
inspect() { | ||
return "FormData"; | ||
has(name) { | ||
return this.__content.has(name); | ||
} | ||
/** | ||
* Returns the first value associated with the given name. | ||
* Buffer and Readable values will be returned as-is. | ||
* | ||
* @param {string} name – A name of the value you want to retrieve. | ||
* | ||
* @public | ||
*/ | ||
get(name) { | ||
const field = this.__content.get(name); | ||
if (!field) { | ||
return undefined; | ||
} | ||
return field.values[0]; | ||
} | ||
/** | ||
* Returns all the values associated with | ||
* a given key from within a FormData object. | ||
* | ||
* @param {string} name – A name of the value you want to retrieve. | ||
* | ||
* @public | ||
*/ | ||
getAll(name) { | ||
const field = this.__content.get(name); | ||
return field ? Array.from(field.values) : []; | ||
} | ||
/** | ||
* Deletes a key and its value(s) from a FormData object. | ||
* | ||
* @param {string} name – The name of the key you want to delete. | ||
* | ||
* @public | ||
*/ | ||
delete(name) { | ||
this.__content.delete(name); | ||
} | ||
get [_Symbol$toStringTag]() { | ||
@@ -466,5 +514,3 @@ return "FormData"; | ||
*keys() { | ||
for (const key of (0, _keys.default)(_context3 = this.__content).call(_context3)) { | ||
var _context3; | ||
for (const key of this.__content.keys()) { | ||
yield key; | ||
@@ -479,5 +525,3 @@ } | ||
*entries() { | ||
for (const name of (0, _keys.default)(_context4 = this).call(_context4)) { | ||
var _context4; | ||
for (const name of this.keys()) { | ||
const values = this.getAll(name); // Yield each value of a field, like browser-side FormData does. | ||
@@ -501,2 +545,10 @@ | ||
/** | ||
* @return {IterableIterator<[string, any]>} | ||
*/ | ||
[_Symbol$iterator]() { | ||
return this.entries(); | ||
} | ||
/** | ||
* Executes a given callback for each field of the FormData instance | ||
@@ -517,9 +569,6 @@ * | ||
/** | ||
* @return {IterableIterator<[string, any]>} | ||
*/ | ||
[_Symbol$iterator]() { | ||
var _context5; | ||
return (0, _entries.default)(_context5 = this).call(_context5); | ||
forEach(fn, ctx = null) { | ||
for (const [name, value] of this) { | ||
fn.call(ctx, value, name, this); | ||
} | ||
} | ||
@@ -537,11 +586,92 @@ /** | ||
[_Symbol$asyncIterator]() { | ||
if ((0, _isFunction.default)(this.stream[_asyncIterator2.default])) { | ||
return this.stream[_asyncIterator2.default](); | ||
} | ||
return this.stream[Symbol.asyncIterator](); | ||
} | ||
/** | ||
* Alias of the FormData#[util.inspect.custom]() | ||
* | ||
* @return {string} | ||
*/ | ||
return new _StreamIterator.default(this.stream); | ||
/** | ||
* Returns a string representation of the FormData | ||
* | ||
* @return {string} | ||
*/ | ||
toString() { | ||
return "[object FormData]"; | ||
} | ||
} | ||
[_inspect$custom]() { | ||
return "FormData"; | ||
} | ||
}), _temp), (_descriptor = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "boundary", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return (0, _concat.default)(["NodeJSFormDataStreamBoundary", (0, _boundary.default)()]); | ||
} | ||
}), _descriptor2 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "headers", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return freeze({ | ||
"Content-Type": (0, _concat.default)(["multipart/form-data; ", "boundary=", this.boundary]) | ||
}); | ||
} | ||
}), _descriptor3 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "stream", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return new _stream.Readable({ | ||
read: () => this.__read() | ||
}); | ||
} | ||
}), _descriptor4 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "__carriage", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return "\r\n"; | ||
} | ||
}), _descriptor5 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "__dashes", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return "-".repeat(2); | ||
} | ||
}), _descriptor6 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "__footer", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return (0, _concat.default)([this.__dashes, this.boundary, this.__dashes, this.__carriage.repeat(2)]); | ||
} | ||
}), _descriptor7 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "__defaultContentType", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return "application/octet-stream"; | ||
} | ||
}), _descriptor8 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "__content", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return new Map(); | ||
} | ||
}), _descriptor9 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "__curr", [_readOnly.default], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: function () { | ||
return this.__getField(); | ||
} | ||
})), _class); | ||
var _default = FormData; | ||
@@ -548,0 +678,0 @@ exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -12,0 +9,0 @@ |
"use strict"; | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -10,0 +7,0 @@ |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator")); | ||
var _fs = require("fs"); | ||
var _promiseFs = _interopRequireDefault(require("promise-fs")); | ||
var _isReadStream = _interopRequireDefault(require("./isReadStream")); | ||
@@ -25,2 +20,5 @@ | ||
const { | ||
stat | ||
} = _fs.promises; | ||
/** | ||
@@ -35,29 +33,23 @@ * Get lenght of given value (in bytes) | ||
*/ | ||
function getLength(_x) { | ||
return _getLength.apply(this, arguments); | ||
} | ||
function _getLength() { | ||
_getLength = (0, _asyncToGenerator2.default)(function* (value) { | ||
if ((0, _isStream.default)(value)) { | ||
if (!(0, _isReadStream.default)(value)) { | ||
return undefined; | ||
} | ||
return _promiseFs.default.stat(value.path).then(({ | ||
size | ||
}) => size); | ||
async function getLength(value) { | ||
if ((0, _isStream.default)(value)) { | ||
if (!(0, _isReadStream.default)(value)) { | ||
return undefined; | ||
} | ||
if ((0, _isBuffer.default)(value)) { | ||
return value.length; | ||
} | ||
return stat(value.path).then(({ | ||
size | ||
}) => size); | ||
} | ||
if ((0, _isBlob.default)(value)) { | ||
return value.size; | ||
} | ||
if ((0, _isBuffer.default)(value)) { | ||
return value.length; | ||
} | ||
return Buffer.from(String(value)).length; | ||
}); | ||
return _getLength.apply(this, arguments); | ||
if ((0, _isBlob.default)(value)) { | ||
return value.size; | ||
} | ||
return Buffer.from(String(value)).length; | ||
} | ||
@@ -64,0 +56,0 @@ |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _asyncIterator = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/symbol/async-iterator")); | ||
var _readableStreamIterator = _interopRequireDefault(require("./readableStreamIterator")); | ||
@@ -19,6 +14,2 @@ | ||
var _StreamIterator = _interopRequireDefault(require("./StreamIterator")); | ||
var _isFunction = _interopRequireDefault(require("./isFunction")); | ||
/** | ||
@@ -38,6 +29,2 @@ * Returns stream iterator for given stream-like object | ||
if (!(0, _isFunction.default)(value[_asyncIterator.default])) { | ||
return new _StreamIterator.default(value); | ||
} | ||
return value; | ||
@@ -44,0 +31,0 @@ } |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes")); | ||
var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice")); | ||
const basicTypes = ["null", "number", "object", "array", "string", "function", "undefined", "boolean"]; | ||
@@ -29,7 +19,5 @@ /** | ||
function getType(val) { | ||
var _context; | ||
const type = Object.prototype.toString.call(val).slice(8, -1); | ||
const type = (0, _slice.default)(_context = Object.prototype.toString.call(val)).call(_context, 8, -1); | ||
if ((0, _includes.default)(basicTypes).call(basicTypes, type.toLowerCase())) { | ||
if (basicTypes.includes(type.toLowerCase())) { | ||
return type.toLowerCase(); | ||
@@ -36,0 +24,0 @@ } |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes")); | ||
var _isFunction = _interopRequireDefault(require("./isFunction")); | ||
@@ -32,3 +27,3 @@ | ||
const isBlob = value => (0, _isObject.default)(value) && (0, _isString.default)(value.type) && (0, _isFunction.default)(value.arrayBuffer) && (0, _isFunction.default)(value.stream) && (0, _isFunction.default)(value.constructor) && (0, _includes.default)(names).call(names, value.constructor.name) && "size" in value; | ||
const isBlob = value => (0, _isObject.default)(value) && (0, _isString.default)(value.type) && (0, _isFunction.default)(value.arrayBuffer) && (0, _isFunction.default)(value.stream) && (0, _isFunction.default)(value.constructor) && names.includes(value.constructor.name) && "size" in value; | ||
@@ -35,0 +30,0 @@ var _default = isBlob; |
"use strict"; | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -10,0 +7,0 @@ |
"use strict"; | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -10,0 +7,0 @@ |
"use strict"; | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -10,0 +7,0 @@ |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _stream = _interopRequireDefault(require("stream")); | ||
var _stream = require("stream"); | ||
/** | ||
* @api private | ||
*/ | ||
const isReadable = val => val instanceof _stream.default.Readable; | ||
const isReadable = val => val instanceof _stream.Readable; | ||
@@ -20,0 +12,0 @@ var _default = isReadable; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _fs = require("fs"); | ||
/** | ||
* @api private | ||
*/ | ||
const isReadStream = value => value instanceof _fs.default.ReadStream; | ||
const isReadStream = value => value instanceof _fs.ReadStream; | ||
@@ -20,0 +12,0 @@ var _default = isReadStream; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -12,0 +9,0 @@ |
"use strict"; | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -10,0 +7,0 @@ |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -12,0 +9,0 @@ |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _awaitAsyncGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/awaitAsyncGenerator")); | ||
var _wrapAsyncGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/wrapAsyncGenerator")); | ||
/** | ||
@@ -24,22 +15,15 @@ * Reads a content from given ReadableStream | ||
*/ | ||
function readableStreamIterator(_x) { | ||
return _readableStreamIterator.apply(this, arguments); | ||
} | ||
async function* readableStreamIterator(reader) { | ||
while (true) { | ||
const { | ||
done, | ||
value | ||
} = await reader.read(); | ||
function _readableStreamIterator() { | ||
_readableStreamIterator = (0, _wrapAsyncGenerator2.default)(function* (reader) { | ||
while (true) { | ||
const { | ||
done, | ||
value | ||
} = yield (0, _awaitAsyncGenerator2.default)(reader.read()); | ||
if (done) { | ||
return value; | ||
} | ||
if (done) { | ||
return value; | ||
} | ||
yield value; | ||
} | ||
}); | ||
return _readableStreamIterator.apply(this, arguments); | ||
yield value; | ||
} | ||
} | ||
@@ -46,0 +30,0 @@ |
{ | ||
"name": "formdata-node", | ||
"version": "1.8.1", | ||
"version": "2.0.0", | ||
"description": "FormData implementation for Node.js. Built over Readable stream and async generators. Can be used to communicate between servers with multipart/form-data format.", | ||
@@ -15,5 +15,5 @@ "repository": "octet-stream/form-data", | ||
"main": "lib/FormData", | ||
"types": "type/FormDataNode.d.ts", | ||
"types": "type/FormData.d.ts", | ||
"engines": { | ||
"node": ">= 8" | ||
"node": ">= 10.2.x" | ||
}, | ||
@@ -55,37 +55,34 @@ "scripts": { | ||
"devDependencies": { | ||
"@babel/cli": "7.4.4", | ||
"@babel/core": "7.4.5", | ||
"@babel/plugin-proposal-async-generator-functions": "7.2.0", | ||
"@babel/plugin-proposal-class-properties": "7.4.4", | ||
"@babel/plugin-proposal-decorators": "7.4.4", | ||
"@babel/plugin-transform-async-to-generator": "7.4.4", | ||
"@babel/plugin-transform-modules-commonjs": "7.4.4", | ||
"@babel/plugin-transform-runtime": "7.4.4", | ||
"@octetstream/eslint-config": "3.1.0", | ||
"@types/node": "12.0.10", | ||
"ava": "2.1.0", | ||
"@babel/cli": "7.7.4", | ||
"@babel/core": "7.7.4", | ||
"@babel/plugin-proposal-class-properties": "7.7.4", | ||
"@babel/plugin-proposal-decorators": "7.7.4", | ||
"@babel/plugin-transform-modules-commonjs": "7.7.4", | ||
"@babel/plugin-transform-runtime": "7.7.4", | ||
"@octetstream/eslint-config": "4.0.0", | ||
"@types/node": "12.12.14", | ||
"ava": "2.4.0", | ||
"babel-plugin-add-module-exports": "1.0.2", | ||
"codecov": "3.5.0", | ||
"eslint": "6.0.1", | ||
"eslint-plugin-ava": "7.1.0", | ||
"fetch-blob": "1.0.3", | ||
"husky": "3.0.0", | ||
"lint-staged": "9.0.2", | ||
"codecov": "3.6.1", | ||
"eslint": "6.7.2", | ||
"eslint-plugin-ava": "9.0.0", | ||
"fetch-blob": "1.0.5", | ||
"husky": "3.1.0", | ||
"lint-staged": "9.5.0", | ||
"lodash.isplainobject": "4.0.6", | ||
"nyc": "14.1.1", | ||
"proxyquire": "2.1.0", | ||
"rimraf": "2.6.3", | ||
"sinon": "7.3.2", | ||
"proxyquire": "2.1.3", | ||
"rimraf": "3.0.0", | ||
"sinon": "7.5.0", | ||
"supertest": "4.0.2", | ||
"then-busboy": "4.3.1", | ||
"tsd": "0.7.3", | ||
"web-streams-polyfill": "2.0.3" | ||
"then-busboy": "4.4.0", | ||
"tsd": "0.11.0", | ||
"web-streams-polyfill": "2.0.6" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime-corejs3": "7.4.5", | ||
"@babel/runtime": "7.7.4", | ||
"@octetstream/invariant": "1.2.0", | ||
"mime-types": "2.1.24", | ||
"nanoid": "2.0.3", | ||
"promise-fs": "2.1.1" | ||
"mime-types": "2.1.25", | ||
"nanoid": "2.1.7" | ||
} | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); | ||
_Object$defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -12,0 +9,0 @@ |
# FormData | ||
FormData implementation for Node.js. Built over Readable stream and async generators. | ||
Can be used to communicate between servers with multipart/form-data format. | ||
@@ -65,7 +64,7 @@ [![Build Status](https://travis-ci.org/octet-stream/form-data.svg?branch=master)](https://travis-ci.org/octet-stream/form-data) | ||
##### `get boundary() -> {string}` | ||
##### `boundary -> {string}` | ||
Returns a boundary string of the current `FormData` instance. | ||
##### `get stream() -> {stream.Readable}` | ||
##### `stream -> {stream.Readable}` | ||
@@ -75,3 +74,3 @@ Returns an internal Readable stream. Use it to send queries, but don't push | ||
##### `get headers() -> {object}` | ||
##### `headers -> {object}` | ||
@@ -82,3 +81,3 @@ Returns object with `content-type` header | ||
##### `set(name, value[, filename]) -> {void}` | ||
##### `set(name, value[, filename, options]) -> {void}` | ||
@@ -96,4 +95,7 @@ Set a new value for an existing key inside **FormData**, | ||
- **{string}** [filename = undefined] – A filename of given field. Can be added only for `Buffer` and `Readable` . | ||
- **{object}** [object = {}] - Additional field options | ||
- **{number}** [object.size = undefined] – A size of field's content. If it set on a stream, then given stream will be treated as File-like object. | ||
Can be omited for `Blob`, `File` and `Buffer` values or if you don't know the **actual** length of the stream. | ||
##### `append(name, value[, filename]) -> {void}` | ||
##### `append(name, value[, filename, options]) -> {void}` | ||
@@ -111,13 +113,16 @@ Appends a new value onto an existing key inside a FormData object, | ||
- **{string}** [filename = undefined] – A filename of given field. Can be added only for `Buffer` and `Readable` . | ||
- **{number}** [object.size = undefined] – A size of field's content. If it set on a stream, then given stream will be treated as File-like object. | ||
Can be omited for `Blob`, `File` and `Buffer` values or if you don't know the **actual** length of the stream. | ||
##### `get(name) -> {string | Buffer | Readable | ReadStream | ReadableStream | Blob | file}` | ||
##### `get(name) -> {string | Readable | ReadStream | ReadableStream | File}` | ||
Returns the first value associated with the given name. | ||
**Buffer** and **Readable** values will be returned as-is. | ||
If the field has `Blob`, `Buffer` or any `Readable` and `ReadableStream` (and when options.size is set for this stream) value, the File-like object will be returned. | ||
- **{string}** name – A name of the value you want to retrieve. | ||
##### `getAll(name) -> {Array<string | Buffer | Readable | ReadStream | ReadableStream | Blob | file>}` | ||
##### `getAll(name) -> {Array<string | Readable | ReadStream | ReadableStream | File>}` | ||
Returns all the values associated with a given key from within a **FormData** object. | ||
If the field has `Blob`, `Buffer` or any `Readable` and `ReadableStream` (and when options.size is set for this stream) value, the File-like object will be returned. | ||
@@ -124,0 +129,0 @@ - **{string}** name – A name of the value you want to retrieve. |
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
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
4
25
1839
183
69045
5
+ Added@babel/runtime@7.7.4
+ Added@babel/runtime@7.7.4(transitive)
+ Addedmime-db@1.42.0(transitive)
+ Addedmime-types@2.1.25(transitive)
+ Addednanoid@2.1.7(transitive)
- Removed@babel/runtime-corejs3@7.4.5
- Removedpromise-fs@2.1.1
- Removed@babel/runtime-corejs3@7.4.5(transitive)
- Removed@octetstream/promisify@2.0.2(transitive)
- Removedcore-js-pure@3.39.0(transitive)
- Removedmime-db@1.40.0(transitive)
- Removedmime-types@2.1.24(transitive)
- Removednanoid@2.0.3(transitive)
- Removedpromise-fs@2.1.1(transitive)
Updatedmime-types@2.1.25
Updatednanoid@2.1.7