This commit is contained in:
lyswhut
2023-01-06 14:39:44 +08:00
parent 1db54195f5
commit 27c1bf8eb6
30 changed files with 196 additions and 85 deletions
+1 -3
View File
@@ -65,9 +65,7 @@ declare namespace LX {
interface HotKeyConfig {
enable: boolean
keys: {
[key: string]: HotKey
}
keys: Record<string, HotKey>
}
interface HotKeyConfigAll {
local: HotKeyConfig
+28 -9
View File
@@ -135,12 +135,13 @@ class Task extends EventEmitter {
try {
this.__initDownload(response)
} catch (error: any) {
return this.__handleError(error)
this.__handleError(error)
return
}
this.status = STATUS.running
response
.on('data', this.__handleWriteData.bind(this))
.on('error', err => this.__handleError(err))
.on('error', err => { this.__handleError(err) })
.on('end', () => {
if (response.complete) {
this.__handleComplete()
@@ -150,7 +151,7 @@ class Task extends EventEmitter {
}
})
})
.on('error', err => this.__handleError(err))
.on('error', err => { this.__handleError(err) })
.on('close', () => {
void this.__closeWriteStream()
})
@@ -159,7 +160,10 @@ class Task extends EventEmitter {
__initDownload(response: http.IncomingMessage) {
this.progress.total = response.headers['content-length'] ? parseInt(response.headers['content-length']) : 0
if (!this.progress.total) return this.__handleError(new Error('Content length is 0'))
if (!this.progress.total) {
this.__handleError(new Error('Content length is 0'))
return
}
let options: any = {}
let isResumable = this.options.forceResume ||
response.headers['accept-ranges'] !== 'none' ||
@@ -170,11 +174,17 @@ class Task extends EventEmitter {
options.flags = 'a'
if (this.progress.downloaded) this.progress.total -= 10
} else {
if (this.chunkInfo.startByte != '0') return this.__handleError(new Error('The resource cannot be resumed download.'))
if (this.chunkInfo.startByte != '0') {
this.__handleError(new Error('The resource cannot be resumed download.'))
return
}
}
this.progress.total += this.progress.downloaded
this.statsEstimate.prevBytes = this.progress.downloaded
if (!this.chunkInfo.path) return this.__handleError(new Error('Chunk save Path is not set.'))
if (!this.chunkInfo.path) {
this.__handleError(new Error('Chunk save Path is not set.'))
return
}
this.ws = fs.createWriteStream(this.chunkInfo.path, options)
this.ws.on('finish', () => {
@@ -216,7 +226,10 @@ class Task extends EventEmitter {
async __closeWriteStream() {
return new Promise<void>((resolve, reject) => {
if (!this.ws) return resolve()
if (!this.ws) {
resolve()
return
}
// console.log('close write stream')
this.ws.close(err => {
if (err) {
@@ -251,7 +264,10 @@ class Task extends EventEmitter {
// this.__handleError(err)
this.chunkInfo.startByte = '0'
this.resumeLastChunk = null
if (unlinkErr && unlinkErr.code !== 'ENOENT') return this.__handleError(unlinkErr)
if (unlinkErr && unlinkErr.code !== 'ENOENT') {
this.__handleError(unlinkErr)
return
}
void this.start()
})
})
@@ -259,7 +275,10 @@ class Task extends EventEmitter {
}
}
// console.log('data', chunk)
if (this.status == STATUS.stopped || this.ws == null) return console.log('cancel write')
if (this.status == STATUS.stopped || this.ws == null) {
console.log('cancel write')
return
}
this.__calculateProgress(chunk.length)
this.ws.write(chunk, err => {
if (!err) return
+49 -23
View File
@@ -16,9 +16,15 @@ export const dirname = (p: string): string => path.dirname(p)
*/
export const checkPath = async(path: string): Promise<boolean> => {
return await new Promise(resolve => {
if (!path) return resolve(false)
if (!path) {
resolve(false)
return
}
fs.access(path, fs.constants.F_OK, err => {
if (err) return resolve(false)
if (err) {
resolve(false)
return
}
resolve(true)
})
})
@@ -26,9 +32,15 @@ export const checkPath = async(path: string): Promise<boolean> => {
export const getFileStats = async(path: string): Promise<fs.Stats | null> => {
return await new Promise(resolve => {
if (!path) return resolve(null)
if (!path) {
resolve(null)
return
}
fs.stat(path, (err, stats) => {
if (err) return resolve(null)
if (err) {
resolve(null)
return
}
resolve(stats)
})
})
@@ -39,29 +51,37 @@ export const getFileStats = async(path: string): Promise<fs.Stats | null> => {
* @param path
* @returns
*/
export const createDir = async(path: string): Promise<void> => {
return await new Promise((resolve, reject) => {
fs.access(path, fs.constants.F_OK | fs.constants.W_OK, err => {
if (err) {
if (err.code === 'ENOENT') {
fs.mkdir(path, { recursive: true }, err => {
if (err) return reject(err)
resolve()
})
return
}
return reject(err)
export const createDir = async(path: string) => new Promise<void>((resolve, reject) => {
fs.access(path, fs.constants.F_OK | fs.constants.W_OK, err => {
if (err) {
if (err.code === 'ENOENT') {
fs.mkdir(path, { recursive: true }, err => {
if (err) {
reject(err)
return
}
resolve()
})
return
}
resolve()
})
reject(err)
return
}
resolve()
})
}
})
export const removeFile = async(path: string) => new Promise<void>((resolve, reject) => {
fs.access(path, fs.constants.F_OK, err => {
if (err) return err.code == 'ENOENT' ? resolve() : reject(err)
if (err) {
err.code == 'ENOENT' ? resolve() : reject(err)
return
}
fs.unlink(path, err => {
if (err) return reject(err)
if (err) {
reject(err)
return
}
resolve()
})
})
@@ -79,7 +99,10 @@ export const toMD5 = (str: string) => crypto.createHash('md5').update(str).diges
export const gzipData = async(str: string): Promise<Buffer> => {
return await new Promise((resolve, reject) => {
gzip(str, (err, result) => {
if (err) return reject(err)
if (err) {
reject(err)
return
}
resolve(result)
})
})
@@ -88,7 +111,10 @@ export const gzipData = async(str: string): Promise<Buffer> => {
export const gunzipData = async(buf: Buffer): Promise<string> => {
return await new Promise((resolve, reject) => {
gunzip(buf, (err, result) => {
if (err) return reject(err)
if (err) {
reject(err)
return
}
resolve(result.toString())
})
})
+12 -3
View File
@@ -45,7 +45,10 @@ const handleScrollY = (element: HTMLElement, to: number, duration = 300, fn = no
element.scrollTop = val
}
if (currentTime < duration) {
if (cancel) return fn()
if (cancel) {
fn()
return
}
window.setTimeout(animateScroll, increment)
} else {
fn()
@@ -117,7 +120,10 @@ const handleScrollX = (element: HTMLElement, to: number, duration = 300, fn = ()
element.scrollLeft = val
}
if (currentTime < duration) {
if (cancel) return fn()
if (cancel) {
fn()
return
}
window.setTimeout(animateScroll, increment)
} else {
fn()
@@ -192,7 +198,10 @@ const handleScrollXR = (element: HTMLElement, to: number, duration = 300, fn = (
element.scrollLeft = val
}
if (currentTime < duration) {
if (cancel) return fn()
if (cancel) {
fn()
return
}
window.setTimeout(animateScroll, increment)
} else {
fn()