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 {
|
2024-07-10 16:44:17 +08:00
|
|
|
#buf = Buffer.from('')
|
2023-10-27 19:16:32 +08:00
|
|
|
|
|
|
|
write(buffer) {
|
2024-07-10 16:44:17 +08:00
|
|
|
this.#buf = Buffer.concat([this.#buf, buffer])
|
2023-10-27 19:16:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
2024-07-10 16:44:17 +08:00
|
|
|
let data = this.#buf.toString()
|
2023-10-27 19:16:32 +08:00
|
|
|
let fields = parse(data)
|
2023-10-30 16:41:37 +08:00
|
|
|
|
2024-07-10 16:44:17 +08:00
|
|
|
this.#buf = null
|
2023-10-30 16:41:37 +08:00
|
|
|
|
2023-10-27 19:16:32 +08:00
|
|
|
this.emit('field', fields)
|
|
|
|
this.emit('end')
|
|
|
|
}
|
|
|
|
}
|