diff --git a/.eslintrc b/.eslintrc index 15b8728b..a0ad0f94 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,9 +4,11 @@ ], "env": { "es6": true, - "node": true, - "browser": true, - "jasmine": true + "node": false, + "jest": false, + "browser": false, + "shared-node-browser": true, + "jasmine": false }, "parserOptions": { "ecmaVersion": 2020, @@ -21,6 +23,60 @@ "no-param-reassign": ["off"], "import/no-extraneous-dependencies": ["error", { "devDependencies": ["test/**/*.js"] - }] - } + }], + "import/extensions": ["error", "ignorePackages"] + }, + "overrides": [ + { + "files": ["lib/browser/**/*.js", "demos/**/*.js"], + "env": { + "browser": true + } + }, + { + "files": ["lib/node/**/*.js"], + "env": { + "node": true + }, + "rules": { + "no-restricted-globals": [ + "error", + { + "name": "__filename", + "message": "Use import.meta.url instead" + }, + { + "name": "__dirname", + "message": "Not available in ESM" + }, + { + "name": "exports", + "message": "Not available in ESM" + }, + { + "name": "module", + "message": "Not available in ESM" + }, + { + "name": "require", + "message": "Use import instead" + } + ] + } + }, + { + "files": ["test/**/*.js", "demos/**/*.js"], + "env": { + "browser": true, + "node": true, + "jasmine": true + }, + "parserOptions": { + "sourceType": "script" + }, + "rules": { + "import/extensions": "off" + } + } + ] } diff --git a/.gitignore b/.gitignore index b83bec94..5c9147fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.eslintcache +.vscode/settings.json node_modules demos/reactnative/.expo lib.es5 diff --git a/demos/browser/demo.js b/demos/browser/demo.js index 38f71164..97d22cb7 100644 --- a/demos/browser/demo.js +++ b/demos/browser/demo.js @@ -1,6 +1,8 @@ /* global tus */ /* eslint-disable no-console, no-alert */ +'use strict' + let upload = null let uploadIsRunning = false const toggleBtn = document.querySelector('#toggle-btn') diff --git a/demos/browser/video.js b/demos/browser/video.js index 7ab95049..a3215ae6 100644 --- a/demos/browser/video.js +++ b/demos/browser/video.js @@ -1,6 +1,8 @@ /* global tus */ /* eslint-disable no-console, no-alert */ +'use strict' + let stopRecording = null let upload = null const recordBtn = document.querySelector('#record-btn') diff --git a/demos/cordova/hooks/before_prepare/copy_tus_files.js b/demos/cordova/hooks/before_prepare/copy_tus_files.js index 2954f745..fc686395 100755 --- a/demos/cordova/hooks/before_prepare/copy_tus_files.js +++ b/demos/cordova/hooks/before_prepare/copy_tus_files.js @@ -1,5 +1,7 @@ #!/usr/bin/env node +'use strict' + const fs = require('fs') const path = require('path') diff --git a/demos/cordova/www/js/index.js b/demos/cordova/www/js/index.js index aa45456d..6e84a9a0 100644 --- a/demos/cordova/www/js/index.js +++ b/demos/cordova/www/js/index.js @@ -1,6 +1,8 @@ /* global tus Camera */ /* eslint-disable no-alert */ +'use strict' + let upload = null let uploadIsRunning = false let file = null diff --git a/demos/nodejs/index.js b/demos/nodejs/index.js index dd338217..2631b451 100644 --- a/demos/nodejs/index.js +++ b/demos/nodejs/index.js @@ -1,5 +1,7 @@ /* eslint-disable no-console */ +'use strict' + const fs = require('fs') const tus = require('../..') diff --git a/lib/browser/fileReader.js b/lib/browser/fileReader.js index 91c14c44..cc10a927 100644 --- a/lib/browser/fileReader.js +++ b/lib/browser/fileReader.js @@ -1,8 +1,8 @@ -import isReactNative from './isReactNative' -import uriToBlob from './uriToBlob' +import isReactNative from './isReactNative.js' +import uriToBlob from './uriToBlob.js' -import FileSource from './sources/FileSource' -import StreamSource from './sources/StreamSource' +import FileSource from './sources/FileSource.js' +import StreamSource from './sources/StreamSource.js' export default class FileReader { openFile (input, chunkSize) { diff --git a/lib/browser/fileSignature.js b/lib/browser/fileSignature.js index 6e88ce05..bd94085a 100644 --- a/lib/browser/fileSignature.js +++ b/lib/browser/fileSignature.js @@ -1,4 +1,4 @@ -import isReactNative from './isReactNative' +import isReactNative from './isReactNative.js' // TODO: Differenciate between input types diff --git a/lib/browser/index.js b/lib/browser/index.js index 9022748e..49666b0b 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -1,12 +1,12 @@ -import BaseUpload from '../upload' -import NoopUrlStorage from '../noopUrlStorage' -import { enableDebugLog } from '../logger' -import DetailedError from '../error' +import BaseUpload from '../upload.js' +import NoopUrlStorage from '../noopUrlStorage.js' +import { enableDebugLog } from '../logger.js' +import DetailedError from '../error.js' -import { canStoreURLs, WebStorageUrlStorage } from './urlStorage' -import DefaultHttpStack from './httpStack' -import FileReader from './fileReader' -import fingerprint from './fileSignature' +import { canStoreURLs, WebStorageUrlStorage } from './urlStorage.js' +import DefaultHttpStack from './httpStack.js' +import FileReader from './fileReader.js' +import fingerprint from './fileSignature.js' const defaultOptions = { ...BaseUpload.defaultOptions, diff --git a/lib/browser/sources/FileSource.js b/lib/browser/sources/FileSource.js index 50a8d352..eb3b0888 100644 --- a/lib/browser/sources/FileSource.js +++ b/lib/browser/sources/FileSource.js @@ -1,5 +1,5 @@ -import isCordova from './isCordova' -import readAsByteArray from './readAsByteArray' +import isCordova from './isCordova.js' +import readAsByteArray from './readAsByteArray.js' export default class FileSource { // Make this.size a method diff --git a/lib/node/fileReader.js b/lib/node/fileReader.js index 0bf2888a..a0757d8c 100644 --- a/lib/node/fileReader.js +++ b/lib/node/fileReader.js @@ -1,9 +1,9 @@ import { ReadStream } from 'fs' import isStream from 'is-stream' -import BufferSource from './sources/BufferSource' -import getFileSource from './sources/FileSource' -import StreamSource from './sources/StreamSource' +import BufferSource from './sources/BufferSource.js' +import getFileSource from './sources/FileSource.js' +import StreamSource from './sources/StreamSource.js' export default class FileReader { openFile (input, chunkSize) { diff --git a/lib/node/index.js b/lib/node/index.js index c90ea32f..4667461a 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -1,13 +1,13 @@ -import BaseUpload from '../upload' -import NoopUrlStorage from '../noopUrlStorage' -import { enableDebugLog } from '../logger' -import DetailedError from '../error' +import BaseUpload from '../upload.js' +import NoopUrlStorage from '../noopUrlStorage.js' +import { enableDebugLog } from '../logger.js' +import DetailedError from '../error.js' -import { FileUrlStorage, canStoreURLs } from './urlStorage' -import DefaultHttpStack from './httpStack' -import FileReader from './fileReader' -import fingerprint from './fileSignature' -import StreamSource from './sources/StreamSource' +import { FileUrlStorage, canStoreURLs } from './urlStorage.js' +import DefaultHttpStack from './httpStack.js' +import FileReader from './fileReader.js' +import fingerprint from './fileSignature.js' +import StreamSource from './sources/StreamSource.js' const defaultOptions = { ...BaseUpload.defaultOptions, diff --git a/lib/upload.js b/lib/upload.js index 8943b4ae..25ff92f0 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -1,8 +1,8 @@ import { Base64 } from 'js-base64' import URL from 'url-parse' -import DetailedError from './error' -import { log } from './logger' -import uuid from './uuid' +import DetailedError from './error.js' +import { log } from './logger.js' +import uuid from './uuid.js' const defaultOptions = { endpoint: null, @@ -924,8 +924,8 @@ function sendRequest (req, body, options) { function isOnline () { let online = true if (typeof window !== 'undefined' - && 'navigator' in window - && window.navigator.onLine === false) { + && 'navigator' in window // eslint-disable-line no-undef + && window.navigator.onLine === false) { // eslint-disable-line no-undef online = false } diff --git a/package.json b/package.json index 3e5d31f0..b0c24234 100644 --- a/package.json +++ b/package.json @@ -99,6 +99,6 @@ "test-browserstack": "karma start test/karma/browserstack.conf.js", "test-node": "jasmine test/spec/node-index.js", "test-types": "tsd", - "lint": "eslint ." + "lint": "eslint . --cache" } } diff --git a/test/karma/base.conf.js b/test/karma/base.conf.js index 34ff939d..ebfba7f5 100644 --- a/test/karma/base.conf.js +++ b/test/karma/base.conf.js @@ -1,5 +1,7 @@ // Karma base configuration +'use strict' + module.exports = (config) => { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) diff --git a/test/karma/browserstack.conf.js b/test/karma/browserstack.conf.js index 29fb1efd..ded7d98d 100644 --- a/test/karma/browserstack.conf.js +++ b/test/karma/browserstack.conf.js @@ -1,3 +1,5 @@ +'use strict' + // Karma configuration for testing using BrowserStack const baseConfig = require('./base.conf') diff --git a/test/karma/puppeteer.conf.js b/test/karma/puppeteer.conf.js index dfffdc5c..b908ef6f 100644 --- a/test/karma/puppeteer.conf.js +++ b/test/karma/puppeteer.conf.js @@ -1,3 +1,5 @@ +'use strict' + // Karma configuration for testing using Puppeteer const baseConfig = require('./base.conf') diff --git a/test/spec/browser-index.js b/test/spec/browser-index.js index c9705dad..856d61bf 100644 --- a/test/spec/browser-index.js +++ b/test/spec/browser-index.js @@ -1,7 +1,9 @@ +'use strict' + // The regenerator runtime is needed since the test use functions // with the async/await keywords. See // https://babeljs.io/docs/en/babel-plugin-transform-regenerator -import 'regenerator-runtime/runtime' +require('regenerator-runtime/runtime') beforeEach(() => { // Clear localStorage before every test to prevent stored URLs to diff --git a/test/spec/helpers/assertUrlStorage.js b/test/spec/helpers/assertUrlStorage.js index b4c248da..9bd39388 100644 --- a/test/spec/helpers/assertUrlStorage.js +++ b/test/spec/helpers/assertUrlStorage.js @@ -1,3 +1,5 @@ +'use strict' + module.exports = async function assertUrlStorage (urlStorage) { // In the beginning of the test, the storage should be empty. let result = await urlStorage.findAllUploads() diff --git a/test/spec/helpers/utils.js b/test/spec/helpers/utils.js index d5a8d8ed..5b07a0d0 100644 --- a/test/spec/helpers/utils.js +++ b/test/spec/helpers/utils.js @@ -1,4 +1,7 @@ /* eslint-disable max-classes-per-file */ + +'use strict' + const isBrowser = typeof window !== 'undefined' const isNode = !isBrowser diff --git a/test/spec/node-index.js b/test/spec/node-index.js index fef8fc39..9db81a70 100644 --- a/test/spec/node-index.js +++ b/test/spec/node-index.js @@ -1,3 +1,5 @@ +'use strict' + require('./test-common') require('./test-node-specific') require('./test-parallel-uploads') diff --git a/test/spec/test-browser-specific.js b/test/spec/test-browser-specific.js index 95840935..0c34ea56 100644 --- a/test/spec/test-browser-specific.js +++ b/test/spec/test-browser-specific.js @@ -1,3 +1,5 @@ +'use strict' + const assertUrlStorage = require('./helpers/assertUrlStorage') const { TestHttpStack, waitableFunction, wait } = require('./helpers/utils') const tus = require('../..') diff --git a/test/spec/test-common.js b/test/spec/test-common.js index 1801b59f..1de4305f 100644 --- a/test/spec/test-common.js +++ b/test/spec/test-common.js @@ -1,3 +1,5 @@ +'use strict' + const { TestHttpStack, waitableFunction, wait, getBlob } = require('./helpers/utils') const tus = require('../..') diff --git a/test/spec/test-end-to-end.js b/test/spec/test-end-to-end.js index 79fa5aa8..e5500089 100644 --- a/test/spec/test-end-to-end.js +++ b/test/spec/test-end-to-end.js @@ -1,3 +1,5 @@ +'use strict' + const axios = require('axios') const { getBlob } = require('./helpers/utils') const tus = require('../..') diff --git a/test/spec/test-node-specific.js b/test/spec/test-node-specific.js index c68a0915..9644bb52 100644 --- a/test/spec/test-node-specific.js +++ b/test/spec/test-node-specific.js @@ -1,3 +1,5 @@ +'use strict' + const stream = require('stream') const temp = require('temp') const fs = require('fs') diff --git a/test/spec/test-parallel-uploads.js b/test/spec/test-parallel-uploads.js index c8a5ffee..1002693f 100644 --- a/test/spec/test-parallel-uploads.js +++ b/test/spec/test-parallel-uploads.js @@ -1,3 +1,5 @@ +'use strict' + const { TestHttpStack, waitableFunction, wait, getBlob } = require('./helpers/utils') const tus = require('../..') diff --git a/test/spec/test-terminate.js b/test/spec/test-terminate.js index dddacc34..f257ae08 100644 --- a/test/spec/test-terminate.js +++ b/test/spec/test-terminate.js @@ -1,3 +1,5 @@ +'use strict' + const { TestHttpStack, getBlob } = require('./helpers/utils') const tus = require('../..')