request/lib/urlencoded_parser.js

27 lines
482 B
JavaScript
Raw Permalink Normal View History

2023-10-27 19:16:32 +08:00
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2023/10/27 12:14:05
*/
import { parse } from 'node:querystring'
import { EventEmitter } from 'node:events'
export class UrlencodedParser extends EventEmitter {
#buff = Buffer.from('')
write(buffer) {
this.#buff = Buffer.concat([this.#buff, buffer])
}
end() {
let data = this.#buff.toString()
let fields = parse(data)
2023-10-30 16:41:37 +08:00
2023-10-27 19:16:32 +08:00
this.#buff = null
2023-10-30 16:41:37 +08:00
2023-10-27 19:16:32 +08:00
this.emit('field', fields)
this.emit('end')
}
}