diff --git a/__tests__/mainTest.js b/__tests__/mainTest.js index ec7b091..b3d1623 100644 --- a/__tests__/mainTest.js +++ b/__tests__/mainTest.js @@ -8,160 +8,160 @@ const nativeEmitter = new NativeEventEmitter(RNBackgroundDownloaderNative) let downloadTask test('download function', () => { - downloadTask = RNBackgroundDownloader.download({ - id: 'test', - url: 'test', - destination: 'test' - }) - expect(downloadTask).toBeInstanceOf(DownloadTask) - expect(RNBackgroundDownloaderNative.download).toHaveBeenCalled() + downloadTask = RNBackgroundDownloader.download({ + id: 'test', + url: 'test', + destination: 'test', + }) + expect(downloadTask).toBeInstanceOf(DownloadTask) + expect(RNBackgroundDownloaderNative.download).toHaveBeenCalled() }) test('begin event', () => { - const mockedHeaders = { Etag: '123' } - return new Promise(resolve => { - const beginDT = RNBackgroundDownloader.download({ - id: 'testBegin', - url: 'test', - destination: 'test' - }).begin(({ expectedBytes, headers }) => { - expect(expectedBytes).toBe(9001) - expect(headers).toBe(mockedHeaders) - expect(beginDT.state).toBe('DOWNLOADING') - resolve() - }) - nativeEmitter.emit('downloadBegin', { - id: 'testBegin', - expectedBytes: 9001, - headers: mockedHeaders, - }) + const mockedHeaders = { Etag: '123' } + return new Promise(resolve => { + const beginDT = RNBackgroundDownloader.download({ + id: 'testBegin', + url: 'test', + destination: 'test', + }).begin(({ expectedBytes, headers }) => { + expect(expectedBytes).toBe(9001) + expect(headers).toBe(mockedHeaders) + expect(beginDT.state).toBe('DOWNLOADING') + resolve() }) + nativeEmitter.emit('downloadBegin', { + id: 'testBegin', + expectedBytes: 9001, + headers: mockedHeaders, + }) + }) }) test('progress event', () => { - return new Promise(resolve => { - RNBackgroundDownloader.download({ - id: 'testProgress', - url: 'test', - destination: 'test' - }).progress((percent, bytesWritten, totalBytes) => { - expect(percent).toBeCloseTo(0.7) - expect(bytesWritten).toBe(100) - expect(totalBytes).toBe(200) - resolve() - }) - nativeEmitter.emit('downloadProgress', [{ - id: 'testProgress', - percent: 0.7, - written: 100, - total: 200 - }]) + return new Promise(resolve => { + RNBackgroundDownloader.download({ + id: 'testProgress', + url: 'test', + destination: 'test', + }).progress((percent, bytesWritten, totalBytes) => { + expect(percent).toBeCloseTo(0.7) + expect(bytesWritten).toBe(100) + expect(totalBytes).toBe(200) + resolve() }) + nativeEmitter.emit('downloadProgress', [{ + id: 'testProgress', + percent: 0.7, + written: 100, + total: 200, + }]) + }) }) test('done event', () => { - return new Promise(resolve => { - const doneDT = RNBackgroundDownloader.download({ - id: 'testDone', - url: 'test', - destination: 'test' - }).done(() => { - expect(doneDT.state).toBe('DONE') - resolve() - }) - nativeEmitter.emit('downloadComplete', { - id: 'testDone' - }) + return new Promise(resolve => { + const doneDT = RNBackgroundDownloader.download({ + id: 'testDone', + url: 'test', + destination: 'test', + }).done(() => { + expect(doneDT.state).toBe('DONE') + resolve() }) + nativeEmitter.emit('downloadComplete', { + id: 'testDone', + }) + }) }) test('fail event', () => { - return new Promise(resolve => { - const failDT = RNBackgroundDownloader.download({ - id: 'testFail', - url: 'test', - destination: 'test' - }).error(error => { - expect(error).toBeInstanceOf(Error) - expect(failDT.state).toBe('FAILED') - resolve() - }) - nativeEmitter.emit('downloadFailed', { - id: 'testFail', - error: new Error('test') - }) + return new Promise(resolve => { + const failDT = RNBackgroundDownloader.download({ + id: 'testFail', + url: 'test', + destination: 'test', + }).error(error => { + expect(error).toBeInstanceOf(Error) + expect(failDT.state).toBe('FAILED') + resolve() }) + nativeEmitter.emit('downloadFailed', { + id: 'testFail', + error: new Error('test'), + }) + }) }) test('pause', () => { - const pauseDT = RNBackgroundDownloader.download({ - id: 'testPause', - url: 'test', - destination: 'test' - }) + const pauseDT = RNBackgroundDownloader.download({ + id: 'testPause', + url: 'test', + destination: 'test', + }) - pauseDT.pause() - expect(pauseDT.state).toBe('PAUSED') - expect(RNBackgroundDownloaderNative.pauseTask).toHaveBeenCalled() + pauseDT.pause() + expect(pauseDT.state).toBe('PAUSED') + expect(RNBackgroundDownloaderNative.pauseTask).toHaveBeenCalled() }) test('resume', () => { - const resumeDT = RNBackgroundDownloader.download({ - id: 'testResume', - url: 'test', - destination: 'test' - }) + const resumeDT = RNBackgroundDownloader.download({ + id: 'testResume', + url: 'test', + destination: 'test', + }) - resumeDT.resume() - expect(resumeDT.state).toBe('DOWNLOADING') - expect(RNBackgroundDownloaderNative.resumeTask).toHaveBeenCalled() + resumeDT.resume() + expect(resumeDT.state).toBe('DOWNLOADING') + expect(RNBackgroundDownloaderNative.resumeTask).toHaveBeenCalled() }) test('stop', () => { - const stopDT = RNBackgroundDownloader.download({ - id: 'testStop', - url: 'test', - destination: 'test' - }) + const stopDT = RNBackgroundDownloader.download({ + id: 'testStop', + url: 'test', + destination: 'test', + }) - stopDT.stop() - expect(stopDT.state).toBe('STOPPED') - expect(RNBackgroundDownloaderNative.stopTask).toHaveBeenCalled() + stopDT.stop() + expect(stopDT.state).toBe('STOPPED') + expect(RNBackgroundDownloaderNative.stopTask).toHaveBeenCalled() }) test('checkForExistingDownloads', () => { - return RNBackgroundDownloader.checkForExistingDownloads() - .then(foundDownloads => { - expect(RNBackgroundDownloaderNative.checkForExistingDownloads).toHaveBeenCalled() - expect(foundDownloads.length).toBe(4) - foundDownloads.forEach(foundDownload => { - expect(foundDownload).toBeInstanceOf(DownloadTask) - expect(foundDownload.state).not.toBe('FAILED') - expect(foundDownload.state).not.toBe('STOPPED') - }) - }) + return RNBackgroundDownloader.checkForExistingDownloads() + .then(foundDownloads => { + expect(RNBackgroundDownloaderNative.checkForExistingDownloads).toHaveBeenCalled() + expect(foundDownloads.length).toBe(4) + foundDownloads.forEach(foundDownload => { + expect(foundDownload).toBeInstanceOf(DownloadTask) + expect(foundDownload.state).not.toBe('FAILED') + expect(foundDownload.state).not.toBe('STOPPED') + }) + }) }) test('wrong handler type', () => { - let dt = RNBackgroundDownloader.download({ - id: 'test22222', - url: 'test', - destination: 'test' - }) + const dt = RNBackgroundDownloader.download({ + id: 'test22222', + url: 'test', + destination: 'test', + }) - expect(() => { - dt.begin('not function') - }).toThrow() + expect(() => { + dt.begin('not function') + }).toThrow() - expect(() => { - dt.progress(7) - }).toThrow() + expect(() => { + dt.progress(7) + }).toThrow() - expect(() => { - dt.done({iamnota: 'function'}) - }).toThrow() + expect(() => { + dt.done({ iamnota: 'function' }) + }).toThrow() - expect(() => { - dt.error('not function') - }).toThrow() + expect(() => { + dt.error('not function') + }).toThrow() }) diff --git a/index.js b/index.js index 635a270..7fd5fb4 100644 --- a/index.js +++ b/index.js @@ -32,14 +32,14 @@ RNBackgroundDownloaderEmitter.addListener('downloadBegin', ({ id, expectedBytes, task?.onBegin({ expectedBytes, headers }) }) -export function setHeaders(h = {}) { +export function setHeaders (h = {}) { if (typeof h !== 'object') throw new Error('[RNBackgroundDownloader] headers must be an object') headers = h } -export function checkForExistingDownloads() { +export function checkForExistingDownloads () { return RNBackgroundDownloader.checkForExistingDownloads() .then(foundTasks => { return foundTasks.map(taskInfo => { @@ -64,7 +64,7 @@ export function checkForExistingDownloads() { }) } -export function ensureDownloadsAreRunning() { +export function ensureDownloadsAreRunning () { return checkForExistingDownloads() .then(tasks => { for (const task of tasks) @@ -75,11 +75,11 @@ export function ensureDownloadsAreRunning() { }) } -export function completeHandler(jobId) { +export function completeHandler (jobId) { return RNBackgroundDownloader.completeHandler(jobId) } -export function download(options) { +export function download (options) { if (!options.id || !options.url || !options.destination) throw new Error('[RNBackgroundDownloader] id, url and destination are required') diff --git a/lib/downloadTask.js b/lib/downloadTask.js index cfa2bfa..b8d8310 100644 --- a/lib/downloadTask.js +++ b/lib/downloadTask.js @@ -9,97 +9,97 @@ function validateHandler (handler) { } export default class DownloadTask { - state = 'PENDING' - percent = 0 - bytesWritten = 0 - totalBytes = 0 - metadata = {} + state = 'PENDING' + percent = 0 + bytesWritten = 0 + totalBytes = 0 + metadata = {} - constructor (taskInfo, originalTask) { - this.id = taskInfo.id - this.percent = taskInfo.percent ?? 0 - this.bytesWritten = taskInfo.bytesWritten ?? 0 - this.totalBytes = taskInfo.totalBytes ?? 0 + constructor (taskInfo, originalTask) { + this.id = taskInfo.id + this.percent = taskInfo.percent ?? 0 + this.bytesWritten = taskInfo.bytesWritten ?? 0 + this.totalBytes = taskInfo.totalBytes ?? 0 - this.metadata = this.tryParseJson(taskInfo.metadata) + this.metadata = this.tryParseJson(taskInfo.metadata) - if (originalTask) { - this.beginHandler = originalTask.beginHandler - this.progressHandler = originalTask.progressHandler - this.doneHandler = originalTask.doneHandler - this.errorHandler = originalTask.errorHandler - } + if (originalTask) { + this.beginHandler = originalTask.beginHandler + this.progressHandler = originalTask.progressHandler + this.doneHandler = originalTask.doneHandler + this.errorHandler = originalTask.errorHandler } + } - begin (handler) { - validateHandler(handler) - this.beginHandler = handler - return this - } + begin (handler) { + validateHandler(handler) + this.beginHandler = handler + return this + } - progress (handler) { - validateHandler(handler) - this.progressHandler = handler - return this - } + progress (handler) { + validateHandler(handler) + this.progressHandler = handler + return this + } - done (handler) { - validateHandler(handler) - this.doneHandler = handler - return this - } + done (handler) { + validateHandler(handler) + this.doneHandler = handler + return this + } - error (handler) { - validateHandler(handler) - this.errorHandler = handler - return this - } + error (handler) { + validateHandler(handler) + this.errorHandler = handler + return this + } - onBegin ({ expectedBytes, headers }) { - this.state = 'DOWNLOADING' - this?.beginHandler({ expectedBytes, headers }) - } + onBegin ({ expectedBytes, headers }) { + this.state = 'DOWNLOADING' + this?.beginHandler({ expectedBytes, headers }) + } - onProgress (percent, bytesWritten, totalBytes) { - this.percent = percent - this.bytesWritten = bytesWritten - this.totalBytes = totalBytes - this?.progressHandler(percent, bytesWritten, totalBytes) - } + onProgress (percent, bytesWritten, totalBytes) { + this.percent = percent + this.bytesWritten = bytesWritten + this.totalBytes = totalBytes + this?.progressHandler(percent, bytesWritten, totalBytes) + } - onDone ({ location }) { - this.state = 'DONE' - this?.doneHandler({ location }) - } + onDone ({ location }) { + this.state = 'DONE' + this?.doneHandler({ location }) + } - onError (error, errorCode) { - this.state = 'FAILED' - this?.errorHandler(error, errorCode) - } + onError (error, errorCode) { + this.state = 'FAILED' + this?.errorHandler(error, errorCode) + } - pause () { - this.state = 'PAUSED' - RNBackgroundDownloader.pauseTask(this.id) - } + pause () { + this.state = 'PAUSED' + RNBackgroundDownloader.pauseTask(this.id) + } - resume () { - this.state = 'DOWNLOADING' - RNBackgroundDownloader.resumeTask(this.id) - } + resume () { + this.state = 'DOWNLOADING' + RNBackgroundDownloader.resumeTask(this.id) + } - stop () { - this.state = 'STOPPED' - RNBackgroundDownloader.stopTask(this.id) - } + stop () { + this.state = 'STOPPED' + RNBackgroundDownloader.stopTask(this.id) + } - tryParseJson (element) { - try { - if (element) - element = JSON.parse(element) - return element - } catch (e) { - console.warn('DownloadTask tryParseJson', e) - return null - } + tryParseJson (element) { + try { + if (element) + element = JSON.parse(element) + return element + } catch (e) { + console.warn('DownloadTask tryParseJson', e) + return null } + } } diff --git a/package.json b/package.json index 3f0530b..5bc4e92 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "eslint-config-standard": "^17.0.0", "eslint-config-standard-jsx": "^11.0.0", "eslint-plugin-import": "^2.27.5", + "eslint-plugin-n": "^15.6.1", "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^6.1.1", "eslint-plugin-react": "^7.32.2", diff --git a/yarn.lock b/yarn.lock index 0fc9712..6847bf7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2874,6 +2874,13 @@ buffer@^5.5.0: base64-js "^1.3.1" ieee754 "^1.1.13" +builtins@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/builtins/-/builtins-5.0.1.tgz#87f6db9ab0458be728564fa81d876d8d74552fa9" + integrity sha512-qwVpFEHNfhYJIzNRBvd2C1kyo6jz3ZSMPyyuR47OPdiKWlbYnZNyDWuyR175qDnAJLiCo5fBBqPb3RiXgWlkOQ== + dependencies: + semver "^7.0.0" + bytes@3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048" @@ -3641,6 +3648,14 @@ eslint-plugin-es@^3.0.0: eslint-utils "^2.0.0" regexpp "^3.0.0" +eslint-plugin-es@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-es/-/eslint-plugin-es-4.1.0.tgz#f0822f0c18a535a97c3e714e89f88586a7641ec9" + integrity sha512-GILhQTnjYE2WorX5Jyi5i4dz5ALWxBIdQECVQavL6s7cI76IZTDWleTHkxz/QT3kvcs2QlGHvKLYsSlPOlPXnQ== + dependencies: + eslint-utils "^2.0.0" + regexpp "^3.0.0" + eslint-plugin-eslint-comments@^3.2.0: version "3.2.0" resolved "https://registry.yarnpkg.com/eslint-plugin-eslint-comments/-/eslint-plugin-eslint-comments-3.2.0.tgz#9e1cd7b4413526abb313933071d7aba05ca12ffa" @@ -3685,6 +3700,20 @@ eslint-plugin-jest@^26.5.3: dependencies: "@typescript-eslint/utils" "^5.10.0" +eslint-plugin-n@^15.6.1: + version "15.6.1" + resolved "https://registry.yarnpkg.com/eslint-plugin-n/-/eslint-plugin-n-15.6.1.tgz#f7e77f24abb92a550115cf11e29695da122c398c" + integrity sha512-R9xw9OtCRxxaxaszTQmQAlPgM+RdGjaL1akWuY/Fv9fRAi8Wj4CUKc6iYVG8QNRjRuo8/BqVYIpfqberJUEacA== + dependencies: + builtins "^5.0.1" + eslint-plugin-es "^4.1.0" + eslint-utils "^3.0.0" + ignore "^5.1.1" + is-core-module "^2.11.0" + minimatch "^3.1.2" + resolve "^1.22.1" + semver "^7.3.8" + eslint-plugin-node@^11.1.0: version "11.1.0" resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-11.1.0.tgz#c95544416ee4ada26740a30474eefc5402dc671d" @@ -7173,7 +7202,7 @@ semver@^6.0.0, semver@^6.1.0, semver@^6.1.1, semver@^6.1.2, semver@^6.3.0: resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw== -semver@^7.3.5, semver@^7.3.7: +semver@^7.0.0, semver@^7.3.5, semver@^7.3.7, semver@^7.3.8: version "7.3.8" resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.8.tgz#07a78feafb3f7b32347d725e33de7e2a2df67798" integrity sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==