diff options
author | Minteck <contact@minteck.org> | 2022-08-21 17:31:56 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-08-21 17:31:56 +0200 |
commit | a2df9a69dcc14cb70118cda2ded499055e7ee358 (patch) | |
tree | 6dd283e4e9452d38bce81ddaaae49b5335755842 /together/node_modules/formidable/src/parsers | |
parent | 84dd0735820b16b60f600284d35183d76547a71f (diff) | |
download | pluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.tar.gz pluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.tar.bz2 pluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.zip |
m. update
Diffstat (limited to 'together/node_modules/formidable/src/parsers')
7 files changed, 591 insertions, 0 deletions
diff --git a/together/node_modules/formidable/src/parsers/Dummy.js b/together/node_modules/formidable/src/parsers/Dummy.js new file mode 100644 index 0000000..6340959 --- /dev/null +++ b/together/node_modules/formidable/src/parsers/Dummy.js @@ -0,0 +1,21 @@ +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +const { Transform } = require('stream'); + +class DummyParser extends Transform { + constructor(incomingForm, options = {}) { + super(); + this.globalOptions = { ...options }; + this.incomingForm = incomingForm; + } + + _flush(callback) { + this.incomingForm.ended = true; + this.incomingForm._maybeEnd(); + callback(); + } +} + +module.exports = DummyParser; diff --git a/together/node_modules/formidable/src/parsers/JSON.js b/together/node_modules/formidable/src/parsers/JSON.js new file mode 100644 index 0000000..9a096c2 --- /dev/null +++ b/together/node_modules/formidable/src/parsers/JSON.js @@ -0,0 +1,35 @@ +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +const { Transform } = require('stream'); + +class JSONParser extends Transform { + constructor(options = {}) { + super({ readableObjectMode: true }); + this.chunks = []; + this.globalOptions = { ...options }; + } + + _transform(chunk, encoding, callback) { + this.chunks.push(String(chunk)); // todo consider using a string decoder + callback(); + } + + _flush(callback) { + try { + const fields = JSON.parse(this.chunks.join('')); + Object.keys(fields).forEach((key) => { + const value = fields[key]; + this.push({ key, value }); + }); + } catch (e) { + callback(e); + return; + } + this.chunks = null; + callback(); + } +} + +module.exports = JSONParser; diff --git a/together/node_modules/formidable/src/parsers/Multipart.js b/together/node_modules/formidable/src/parsers/Multipart.js new file mode 100644 index 0000000..23a298a --- /dev/null +++ b/together/node_modules/formidable/src/parsers/Multipart.js @@ -0,0 +1,347 @@ +/* eslint-disable no-fallthrough */ +/* eslint-disable no-bitwise */ +/* eslint-disable no-plusplus */ +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +const { Transform } = require('stream'); +const errors = require('../FormidableError.js'); + +const { FormidableError } = errors; + +let s = 0; +const STATE = { + PARSER_UNINITIALIZED: s++, + START: s++, + START_BOUNDARY: s++, + HEADER_FIELD_START: s++, + HEADER_FIELD: s++, + HEADER_VALUE_START: s++, + HEADER_VALUE: s++, + HEADER_VALUE_ALMOST_DONE: s++, + HEADERS_ALMOST_DONE: s++, + PART_DATA_START: s++, + PART_DATA: s++, + PART_END: s++, + END: s++, +}; + +let f = 1; +const FBOUNDARY = { PART_BOUNDARY: f, LAST_BOUNDARY: (f *= 2) }; + +const LF = 10; +const CR = 13; +const SPACE = 32; +const HYPHEN = 45; +const COLON = 58; +const A = 97; +const Z = 122; + +function lower(c) { + return c | 0x20; +} + +exports.STATES = {}; + +Object.keys(STATE).forEach((stateName) => { + exports.STATES[stateName] = STATE[stateName]; +}); + +class MultipartParser extends Transform { + constructor(options = {}) { + super({ readableObjectMode: true }); + this.boundary = null; + this.boundaryChars = null; + this.lookbehind = null; + this.bufferLength = 0; + this.state = STATE.PARSER_UNINITIALIZED; + + this.globalOptions = { ...options }; + this.index = null; + this.flags = 0; + } + + _flush(done) { + if ( + (this.state === STATE.HEADER_FIELD_START && this.index === 0) || + (this.state === STATE.PART_DATA && this.index === this.boundary.length) + ) { + this._handleCallback('partEnd'); + this._handleCallback('end'); + done(); + } else if (this.state !== STATE.END) { + done( + new FormidableError( + `MultipartParser.end(): stream ended unexpectedly: ${this.explain()}`, + errors.malformedMultipart, + 400, + ), + ); + } + } + + initWithBoundary(str) { + this.boundary = Buffer.from(`\r\n--${str}`); + this.lookbehind = Buffer.alloc(this.boundary.length + 8); + this.state = STATE.START; + this.boundaryChars = {}; + + for (let i = 0; i < this.boundary.length; i++) { + this.boundaryChars[this.boundary[i]] = true; + } + } + + // eslint-disable-next-line max-params + _handleCallback(name, buf, start, end) { + if (start !== undefined && start === end) { + return; + } + this.push({ name, buffer: buf, start, end }); + } + + // eslint-disable-next-line max-statements + _transform(buffer, _, done) { + let i = 0; + let prevIndex = this.index; + let { index, state, flags } = this; + const { lookbehind, boundary, boundaryChars } = this; + const boundaryLength = boundary.length; + const boundaryEnd = boundaryLength - 1; + this.bufferLength = buffer.length; + let c = null; + let cl = null; + + const setMark = (name, idx) => { + this[`${name}Mark`] = typeof idx === 'number' ? idx : i; + }; + + const clearMarkSymbol = (name) => { + delete this[`${name}Mark`]; + }; + + const dataCallback = (name, shouldClear) => { + const markSymbol = `${name}Mark`; + if (!(markSymbol in this)) { + return; + } + + if (!shouldClear) { + this._handleCallback(name, buffer, this[markSymbol], buffer.length); + setMark(name, 0); + } else { + this._handleCallback(name, buffer, this[markSymbol], i); + clearMarkSymbol(name); + } + }; + + for (i = 0; i < this.bufferLength; i++) { + c = buffer[i]; + switch (state) { + case STATE.PARSER_UNINITIALIZED: + return i; + case STATE.START: + index = 0; + state = STATE.START_BOUNDARY; + case STATE.START_BOUNDARY: + if (index === boundary.length - 2) { + if (c === HYPHEN) { + flags |= FBOUNDARY.LAST_BOUNDARY; + } else if (c !== CR) { + return i; + } + index++; + break; + } else if (index - 1 === boundary.length - 2) { + if (flags & FBOUNDARY.LAST_BOUNDARY && c === HYPHEN) { + this._handleCallback('end'); + state = STATE.END; + flags = 0; + } else if (!(flags & FBOUNDARY.LAST_BOUNDARY) && c === LF) { + index = 0; + this._handleCallback('partBegin'); + state = STATE.HEADER_FIELD_START; + } else { + return i; + } + break; + } + + if (c !== boundary[index + 2]) { + index = -2; + } + if (c === boundary[index + 2]) { + index++; + } + break; + case STATE.HEADER_FIELD_START: + state = STATE.HEADER_FIELD; + setMark('headerField'); + index = 0; + case STATE.HEADER_FIELD: + if (c === CR) { + clearMarkSymbol('headerField'); + state = STATE.HEADERS_ALMOST_DONE; + break; + } + + index++; + if (c === HYPHEN) { + break; + } + + if (c === COLON) { + if (index === 1) { + // empty header field + return i; + } + dataCallback('headerField', true); + state = STATE.HEADER_VALUE_START; + break; + } + + cl = lower(c); + if (cl < A || cl > Z) { + return i; + } + break; + case STATE.HEADER_VALUE_START: + if (c === SPACE) { + break; + } + + setMark('headerValue'); + state = STATE.HEADER_VALUE; + case STATE.HEADER_VALUE: + if (c === CR) { + dataCallback('headerValue', true); + this._handleCallback('headerEnd'); + state = STATE.HEADER_VALUE_ALMOST_DONE; + } + break; + case STATE.HEADER_VALUE_ALMOST_DONE: + if (c !== LF) { + return i; + } + state = STATE.HEADER_FIELD_START; + break; + case STATE.HEADERS_ALMOST_DONE: + if (c !== LF) { + return i; + } + + this._handleCallback('headersEnd'); + state = STATE.PART_DATA_START; + break; + case STATE.PART_DATA_START: + state = STATE.PART_DATA; + setMark('partData'); + case STATE.PART_DATA: + prevIndex = index; + + if (index === 0) { + // boyer-moore derrived algorithm to safely skip non-boundary data + i += boundaryEnd; + while (i < this.bufferLength && !(buffer[i] in boundaryChars)) { + i += boundaryLength; + } + i -= boundaryEnd; + c = buffer[i]; + } + + if (index < boundary.length) { + if (boundary[index] === c) { + if (index === 0) { + dataCallback('partData', true); + } + index++; + } else { + index = 0; + } + } else if (index === boundary.length) { + index++; + if (c === CR) { + // CR = part boundary + flags |= FBOUNDARY.PART_BOUNDARY; + } else if (c === HYPHEN) { + // HYPHEN = end boundary + flags |= FBOUNDARY.LAST_BOUNDARY; + } else { + index = 0; + } + } else if (index - 1 === boundary.length) { + if (flags & FBOUNDARY.PART_BOUNDARY) { + index = 0; + if (c === LF) { + // unset the PART_BOUNDARY flag + flags &= ~FBOUNDARY.PART_BOUNDARY; + this._handleCallback('partEnd'); + this._handleCallback('partBegin'); + state = STATE.HEADER_FIELD_START; + break; + } + } else if (flags & FBOUNDARY.LAST_BOUNDARY) { + if (c === HYPHEN) { + this._handleCallback('partEnd'); + this._handleCallback('end'); + state = STATE.END; + flags = 0; + } else { + index = 0; + } + } else { + index = 0; + } + } + + if (index > 0) { + // when matching a possible boundary, keep a lookbehind reference + // in case it turns out to be a false lead + lookbehind[index - 1] = c; + } else if (prevIndex > 0) { + // if our boundary turned out to be rubbish, the captured lookbehind + // belongs to partData + this._handleCallback('partData', lookbehind, 0, prevIndex); + prevIndex = 0; + setMark('partData'); + + // reconsider the current character even so it interrupted the sequence + // it could be the beginning of a new sequence + i--; + } + + break; + case STATE.END: + break; + default: + return i; + } + } + + dataCallback('headerField'); + dataCallback('headerValue'); + dataCallback('partData'); + + this.index = index; + this.state = state; + this.flags = flags; + + done(); + return this.bufferLength; + } + + explain() { + return `state = ${MultipartParser.stateToString(this.state)}`; + } +} + +// eslint-disable-next-line consistent-return +MultipartParser.stateToString = (stateNumber) => { + // eslint-disable-next-line no-restricted-syntax, guard-for-in + for (const stateName in STATE) { + const number = STATE[stateName]; + if (number === stateNumber) return stateName; + } +}; + +module.exports = Object.assign(MultipartParser, { STATES: exports.STATES }); diff --git a/together/node_modules/formidable/src/parsers/OctetStream.js b/together/node_modules/formidable/src/parsers/OctetStream.js new file mode 100644 index 0000000..cdf55f2 --- /dev/null +++ b/together/node_modules/formidable/src/parsers/OctetStream.js @@ -0,0 +1,12 @@ +'use strict'; + +const { PassThrough } = require('stream'); + +class OctetStreamParser extends PassThrough { + constructor(options = {}) { + super(); + this.globalOptions = { ...options }; + } +} + +module.exports = OctetStreamParser; diff --git a/together/node_modules/formidable/src/parsers/Querystring.js b/together/node_modules/formidable/src/parsers/Querystring.js new file mode 100644 index 0000000..a0d4243 --- /dev/null +++ b/together/node_modules/formidable/src/parsers/Querystring.js @@ -0,0 +1,38 @@ +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +const { Transform } = require('stream'); +const querystring = require('querystring'); + +// This is a buffering parser, not quite as nice as the multipart one. +// If I find time I'll rewrite this to be fully streaming as well +class QuerystringParser extends Transform { + constructor(options = {}) { + super({ readableObjectMode: true }); + this.globalOptions = { ...options }; + this.buffer = ''; + this.bufferLength = 0; + } + + _transform(buffer, encoding, callback) { + this.buffer += buffer.toString('ascii'); + this.bufferLength = this.buffer.length; + callback(); + } + + _flush(callback) { + const fields = querystring.parse(this.buffer, '&', '='); + // eslint-disable-next-line no-restricted-syntax, guard-for-in + for (const key in fields) { + this.push({ + key, + value: fields[key], + }); + } + this.buffer = ''; + callback(); + } +} + +module.exports = QuerystringParser; diff --git a/together/node_modules/formidable/src/parsers/StreamingQuerystring.js b/together/node_modules/formidable/src/parsers/StreamingQuerystring.js new file mode 100644 index 0000000..06d7577 --- /dev/null +++ b/together/node_modules/formidable/src/parsers/StreamingQuerystring.js @@ -0,0 +1,121 @@ +// not used +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +const { Transform } = require('stream'); +const errors = require('../FormidableError.js'); + +const { FormidableError } = errors; + +const AMPERSAND = 38; +const EQUALS = 61; + +class QuerystringParser extends Transform { + constructor(options = {}) { + super({ readableObjectMode: true }); + + const { maxFieldSize } = options; + this.maxFieldLength = maxFieldSize; + this.buffer = Buffer.from(''); + this.fieldCount = 0; + this.sectionStart = 0; + this.key = ''; + this.readingKey = true; + } + + _transform(buffer, encoding, callback) { + let len = buffer.length; + if (this.buffer && this.buffer.length) { + // we have some data left over from the last write which we are in the middle of processing + len += this.buffer.length; + buffer = Buffer.concat([this.buffer, buffer], len); + } + + for (let i = this.buffer.length || 0; i < len; i += 1) { + const c = buffer[i]; + if (this.readingKey) { + // KEY, check for = + if (c === EQUALS) { + this.key = this.getSection(buffer, i); + this.readingKey = false; + this.sectionStart = i + 1; + } else if (c === AMPERSAND) { + // just key, no value. Prepare to read another key + this.emitField(this.getSection(buffer, i)); + this.sectionStart = i + 1; + } + // VALUE, check for & + } else if (c === AMPERSAND) { + this.emitField(this.key, this.getSection(buffer, i)); + this.sectionStart = i + 1; + } + + if ( + this.maxFieldLength && + i - this.sectionStart === this.maxFieldLength + ) { + callback( + new FormidableError( + `${ + this.readingKey ? 'Key' : `Value for ${this.key}` + } longer than maxFieldLength`, + ), + errors.maxFieldsSizeExceeded, + 413, + ); + } + } + + // Prepare the remaining key or value (from sectionStart to the end) for the next write() or for end() + len -= this.sectionStart; + if (len) { + // i.e. Unless the last character was a & or = + this.buffer = Buffer.from(this.buffer, 0, this.sectionStart); + } else this.buffer = null; + + this.sectionStart = 0; + callback(); + } + + _flush(callback) { + // Emit the last field + if (this.readingKey) { + // we only have a key if there's something in the buffer. We definitely have no value + if (this.buffer && this.buffer.length){ + this.emitField(this.buffer.toString('ascii')); + } + } else { + // We have a key, we may or may not have a value + this.emitField( + this.key, + this.buffer && this.buffer.length && this.buffer.toString('ascii'), + ); + } + this.buffer = ''; + callback(); + } + + getSection(buffer, i) { + if (i === this.sectionStart) return ''; + + return buffer.toString('ascii', this.sectionStart, i); + } + + emitField(key, val) { + this.key = ''; + this.readingKey = true; + this.push({ key, value: val || '' }); + } +} + +module.exports = QuerystringParser; + +// const q = new QuerystringParser({maxFieldSize: 100}); +// (async function() { +// for await (const chunk of q) { +// console.log(chunk); +// } +// })(); +// q.write("a=b&c=d") +// q.end() diff --git a/together/node_modules/formidable/src/parsers/index.js b/together/node_modules/formidable/src/parsers/index.js new file mode 100644 index 0000000..bbf9ef6 --- /dev/null +++ b/together/node_modules/formidable/src/parsers/index.js @@ -0,0 +1,17 @@ +'use strict'; + +const JSONParser = require('./JSON'); +const DummyParser = require('./Dummy'); +const MultipartParser = require('./Multipart'); +const OctetStreamParser = require('./OctetStream'); +const QueryStringParser = require('./Querystring'); + +Object.assign(exports, { + JSONParser, + DummyParser, + MultipartParser, + OctetStreamParser, + OctetstreamParser: OctetStreamParser, + QueryStringParser, + QuerystringParser: QueryStringParser, +}); |