diff --git a/index.js b/index.js index f4f16e8..76ea30c 100644 --- a/index.js +++ b/index.js @@ -197,6 +197,9 @@ export default class Request { } } }) + .on('buffer', buf => { + this.#body = buf + }) .on('error', out.reject) .on('end', _ => { if (contentType.includes('urlencoded')) { diff --git a/lib/index.js b/lib/index.js index 5647007..1deb997 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,7 +6,7 @@ import { EventEmitter } from 'node:events' import File from './file.js' import { MultipartParser } from './multipart_parser.js' import { UrlencodedParser } from './urlencoded_parser.js' -import { OctetParser, EmptyParser } from './octet_parser.js' +import { OctetParser, BufferParser, EmptyParser } from './octet_parser.js' import { JSONParser } from './json_parser.js' function randomPath(uploadDir) { @@ -98,8 +98,8 @@ export default class IncomingForm extends EventEmitter { let value = Buffer.from('') part - .on('data', buff => { - value = Buffer.concat([value, buff]) + .on('data', buf => { + value = Buffer.concat([value, buf]) }) .on('end', () => { this.emit('field', part.name, value.toString(this.encoding)) @@ -167,7 +167,7 @@ export default class IncomingForm extends EventEmitter { return this.#createJsonParser() } - this.#handleError(new TypeError('unknown content-type: ' + contentType)) + this.#createBufferParser() } #parseContentLength() { @@ -316,6 +316,21 @@ export default class IncomingForm extends EventEmitter { .on('error', err => this.#handleError(err)) } + #createBufferParser() { + this.#parser = new BufferParser() + + if (this.bytesExpected) { + this.#parser.initLength(this.bytesExpected) + } + + this.#parser + .on('buffer', buf => { + this.emit('buffer', buf) + }) + .on('end', () => this.#handleEnd()) + .on('error', err => this.#handleError(err)) + } + #clearUploads() { while (this.#openedFiles.length) { let file = this.#openedFiles.pop() diff --git a/lib/json_parser.js b/lib/json_parser.js index 779a2ad..9fb5247 100644 --- a/lib/json_parser.js +++ b/lib/json_parser.js @@ -1,7 +1,7 @@ import { EventEmitter } from 'node:events' export class JSONParser extends EventEmitter { - #buff = Buffer.from('') + #buf = Buffer.from('') #byteLen = 0 initLength(length) { @@ -9,12 +9,12 @@ export class JSONParser extends EventEmitter { } write(buffer) { - this.#buff = Buffer.concat([this.#buff, buffer]) + this.#buf = Buffer.concat([this.#buf, buffer]) } end() { - if (this.#buff.length === this.#byteLen) { - let data = this.#buff.toString() + if (this.#buf.length === this.#byteLen) { + let data = this.#buf.toString() let fields = data try { fields = JSON.parse(data) @@ -28,14 +28,14 @@ export class JSONParser extends EventEmitter { this.emit('field', false, fields) this.emit('end') - this.#buff = null + this.#buf = null } else { this.emit( 'error', new Error( `The uploaded data is incomplete. Expected ${ this.#byteLen - }, Received ${this.#buff.length} .` + }, Received ${this.#buf.length} .` ) ) } diff --git a/lib/multipart_parser.js b/lib/multipart_parser.js index 8c04033..75a1e83 100644 --- a/lib/multipart_parser.js +++ b/lib/multipart_parser.js @@ -73,11 +73,11 @@ export class MultipartParser { this[k + 'Mark'] = v } - #emit(name, buff, idx, cleanup) { + #emit(name, buf, idx, cleanup) { let mark = name + 'Mark' if (this[mark] !== void 0) { let start = this[mark] - let end = buff.length + let end = buf.length if (cleanup) { end = idx @@ -89,7 +89,7 @@ export class MultipartParser { if (start === end) { return } - this['$' + name](buff.slice(start, end)) + this['$' + name](buf.slice(start, end)) } } diff --git a/lib/octet_parser.js b/lib/octet_parser.js index fc0e4b4..cff9ca8 100644 --- a/lib/octet_parser.js +++ b/lib/octet_parser.js @@ -47,6 +47,36 @@ export class OctetParser extends EventEmitter { } } +export class BufferParser extends EventEmitter { + #buf = Buffer.from('') + #byteLen = 0 + + initLength(length) { + this.#byteLen = length + } + + write(buffer) { + this.#buf = Buffer.concat([this.#buf, buffer]) + } + + end() { + if (this.#buf.length === this.#byteLen) { + this.emit('buffer', this.#buf) + this.emit('end') + this.#buf = null + } else { + this.emit( + 'error', + new Error( + `The uploaded data is incomplete. Expected ${ + this.#byteLen + }, Received ${this.#buf.length} .` + ) + ) + } + } +} + export class EmptyParser extends EventEmitter { write() {} diff --git a/lib/urlencoded_parser.js b/lib/urlencoded_parser.js index 78c61d0..55020be 100644 --- a/lib/urlencoded_parser.js +++ b/lib/urlencoded_parser.js @@ -8,17 +8,17 @@ import { parse } from 'node:querystring' import { EventEmitter } from 'node:events' export class UrlencodedParser extends EventEmitter { - #buff = Buffer.from('') + #buf = Buffer.from('') write(buffer) { - this.#buff = Buffer.concat([this.#buff, buffer]) + this.#buf = Buffer.concat([this.#buf, buffer]) } end() { - let data = this.#buff.toString() + let data = this.#buf.toString() let fields = parse(data) - this.#buff = null + this.#buf = null this.emit('field', fields) this.emit('end') diff --git a/package.json b/package.json index c1151b1..324f8f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gm5/request", - "version": "2.0.4", + "version": "2.0.5", "description": "对Http的Request进一步封装, 提供常用的API", "main": "index.js", "author": "yutent",