44 lines
850 B
JavaScript
44 lines
850 B
JavaScript
/**
|
|
* {}
|
|
* @author yutent<yutent.io@gmail.com>
|
|
* @date 2023/10/27 12:14:05
|
|
*/
|
|
|
|
import { EventEmitter } from 'node:events'
|
|
import { querystring } from './helper.js'
|
|
|
|
export class UrlencodedParser 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) {
|
|
let data = this.#buf.toString()
|
|
let fields = querystring(data)
|
|
|
|
this.#buf = null
|
|
|
|
this.emit('field', fields)
|
|
this.emit('end')
|
|
this.#buf = null
|
|
} else {
|
|
this.emit(
|
|
'error',
|
|
new Error(
|
|
`The uploaded data is incomplete. Expected ${
|
|
this.#byteLen
|
|
}, Received ${this.#buf.length} .`
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|