request/lib/file.js

62 lines
946 B
JavaScript

import { WriteStream } from 'node:fs'
import { EventEmitter } from 'node:events'
export default class File extends EventEmitter {
#stream = null
size = 0
path = null
name = null
type = null
lastModifiedDate = null
constructor(props = {}){
super()
for (var key in props) {
this[key] = props[key]
}
}
open() {
this.#stream = new WriteStream(this.path)
}
toJSON() {
return {
size: this.size,
path: this.path,
name: this.name,
type: this.type,
mtime: this.lastModifiedDate,
length: this.length,
filename: this.filename,
mime: this.mime
}
}
write(buffer, cb) {
this.#stream.write(buffer, _ =>{
this.lastModifiedDate = new Date()
this.size += buffer.length
this.emit('progress', this.size)
cb()
})
}
end(cb) {
this.#stream.end(() => {
this.emit('end')
cb()
})
}
}
对Http的request进一步封装, 提供常用的API
JavaScript 100%