Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: Update to eslint-config-transloadit@v2 #291

Merged
merged 14 commits into from
Oct 29, 2021
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ demos/*/node_modules
demos/cordova/platforms
demos/cordova/plugins
demos/cordova/www/js/tus.js
demos/reactnative
lib.esm
lib.es5
dist
24 changes: 5 additions & 19 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,16 @@
"extends": [
"transloadit"
],
"plugins": [
"@babel/eslint-plugin",
"jest",
"node",
"prefer-import",
"promise",
"react"
],
"env": {
"es6": true,
"node": true,
"browser": true,
"jasmine": true
},
"parser": "@babel/eslint-parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"requireConfigFile": false,
"babelOptions": {
"plugins": [
"@babel/plugin-syntax-jsx"
]
},
"ecmaFeatures": {
"jsx": true
}
"requireConfigFile": false
},
"rules": {
// transloadit rules we are actually ok with in the tus-js-client repo
Expand Down Expand Up @@ -61,6 +44,9 @@
"prefer-rest-params": ["warn"],
"react/destructuring-assignment": ["warn"],
"react/sort-comp": ["warn"],
"vars-on-top": ["warn"]
"vars-on-top": ["warn"],

// Used so many places
"no-underscore-dangle": ["off"]
}
}
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ jobs:
uses: sergioramos/yarn-actions/install@v6
with:
frozen-lockfile: true
- name: Lint
uses: sergioramos/yarn-actions/run@v6
with:
script: lint
- name: Build
uses: sergioramos/yarn-actions/run@v6
with:
script: build
- name: Lint
uses: sergioramos/yarn-actions/run@v6
with:
script: lint
- name: Test
uses: sergioramos/yarn-actions/run@v6
with:
Expand Down
116 changes: 2 additions & 114 deletions lib/browser/fileReader.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,8 @@
import isReactNative from './isReactNative'
import uriToBlob from './uriToBlob'
import isCordova from './isCordova'
import readAsByteArray from './readAsByteArray'

class FileSource {
// Make this.size a method
constructor (file) {
this._file = file
this.size = file.size
}

slice (start, end) {
// In Apache Cordova applications, a File must be resolved using
// FileReader instances, see
// https://cordova.apache.org/docs/en/8.x/reference/cordova-plugin-file/index.html#read-a-file
if (isCordova()) {
return readAsByteArray(this._file.slice(start, end))
}

const value = this._file.slice(start, end)
return Promise.resolve({ value })
}

close () {
// Nothing to do here since we don't need to release any resources.
}
}

class StreamSource {
constructor (reader, chunkSize) {
this._chunkSize = chunkSize
this._buffer = undefined
this._bufferOffset = 0
this._reader = reader
this._done = false
}

slice (start, end) {
if (start < this._bufferOffset) {
return Promise.reject(new Error("Requested data is before the reader's current offset"))
}

return this._readUntilEnoughDataOrDone(start, end)
}

_readUntilEnoughDataOrDone (start, end) {
const hasEnoughData = end <= this._bufferOffset + len(this._buffer)
if (this._done || hasEnoughData) {
var value = this._getDataFromBuffer(start, end)
var done = value == null ? this._done : false
return Promise.resolve({ value, done })
}

return this._reader.read().then(({ value, done }) => {
if (done) {
this._done = true
} else if (this._buffer === undefined) {
this._buffer = value
} else {
this._buffer = concat(this._buffer, value)
}

return this._readUntilEnoughDataOrDone(start, end)
})
}

_getDataFromBuffer (start, end) {
// Remove data from buffer before `start`.
// Data might be reread from the buffer if an upload fails, so we can only
// safely delete data when it comes *before* what is currently being read.
if (start > this._bufferOffset) {
this._buffer = this._buffer.slice(start - this._bufferOffset)
this._bufferOffset = start
}
// If the buffer is empty after removing old data, all data has been read.
const hasAllDataBeenRead = len(this._buffer) === 0
if (this._done && hasAllDataBeenRead) {
return null
}
// We already removed data before `start`, so we just return the first
// chunk from the buffer.
return this._buffer.slice(0, end - start)
}

close () {
if (this._reader.cancel) {
this._reader.cancel()
}
}
}

function len (blobOrArray) {
if (blobOrArray === undefined) return 0
if (blobOrArray.size !== undefined) return blobOrArray.size
return blobOrArray.length
}

/*
Typed arrays and blobs don't have a concat method.
This function helps StreamSource accumulate data to reach chunkSize.
*/
function concat (a, b) {
if (a.concat) { // Is `a` an Array?
return a.concat(b)
}
if (a instanceof Blob) {
return new Blob([a, b], { type: a.type })
}
if (a.set) { // Is `a` a typed array?
var c = new a.constructor(a.length + b.length)
c.set(a)
c.set(b, a.length)
return c
}
throw new Error('Unknown data type')
}
import FileSource from './sources/FileSource'
import StreamSource from './sources/StreamSource'

export default class FileReader {
openFile (input, chunkSize) {
Expand Down
3 changes: 1 addition & 2 deletions lib/browser/httpStack.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global window */

/* eslint-disable max-classes-per-file */
export default class XHRHttpStack {
createRequest (method, url) {
return new Request(method, url)
Expand Down
3 changes: 1 addition & 2 deletions lib/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global window */
import BaseUpload from '../upload'
import NoopUrlStorage from '../noopUrlStorage'
import { enableDebugLog } from '../logger'
Expand Down Expand Up @@ -42,5 +41,5 @@ export {
defaultOptions,
isSupported,
enableDebugLog,
HttpStack
HttpStack,
}
26 changes: 26 additions & 0 deletions lib/browser/sources/FileSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import isCordova from './isCordova'
import readAsByteArray from './readAsByteArray'

export default class FileSource {
// Make this.size a method
constructor (file) {
this._file = file
this.size = file.size
}

slice (start, end) {
// In Apache Cordova applications, a File must be resolved using
// FileReader instances, see
// https://cordova.apache.org/docs/en/8.x/reference/cordova-plugin-file/index.html#read-a-file
if (isCordova()) {
return readAsByteArray(this._file.slice(start, end))
}

const value = this._file.slice(start, end)
return Promise.resolve({ value })
}

close () {
// Nothing to do here since we don't need to release any resources.
}
}
88 changes: 88 additions & 0 deletions lib/browser/sources/StreamSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
function len (blobOrArray) {
if (blobOrArray === undefined) return 0
if (blobOrArray.size !== undefined) return blobOrArray.size
return blobOrArray.length
}

/*
Typed arrays and blobs don't have a concat method.
This function helps StreamSource accumulate data to reach chunkSize.
*/
function concat (a, b) {
if (a.concat) { // Is `a` an Array?
return a.concat(b)
}
if (a instanceof Blob) {
return new Blob([a, b], { type: a.type })
}
if (a.set) { // Is `a` a typed array?
var c = new a.constructor(a.length + b.length)
c.set(a)
c.set(b, a.length)
return c
}
throw new Error('Unknown data type')
}

export default class StreamSource {
constructor (reader, chunkSize) {
this._chunkSize = chunkSize
this._buffer = undefined
this._bufferOffset = 0
this._reader = reader
this._done = false
}

slice (start, end) {
if (start < this._bufferOffset) {
return Promise.reject(new Error("Requested data is before the reader's current offset"))
}

return this._readUntilEnoughDataOrDone(start, end)
}

_readUntilEnoughDataOrDone (start, end) {
const hasEnoughData = end <= this._bufferOffset + len(this._buffer)
if (this._done || hasEnoughData) {
var value = this._getDataFromBuffer(start, end)
var done = value == null ? this._done : false
return Promise.resolve({ value, done })
}

return this._reader.read().then(({ value, done }) => {
if (done) {
this._done = true
} else if (this._buffer === undefined) {
this._buffer = value
} else {
this._buffer = concat(this._buffer, value)
}

return this._readUntilEnoughDataOrDone(start, end)
})
}

_getDataFromBuffer (start, end) {
// Remove data from buffer before `start`.
// Data might be reread from the buffer if an upload fails, so we can only
// safely delete data when it comes *before* what is currently being read.
if (start > this._bufferOffset) {
this._buffer = this._buffer.slice(start - this._bufferOffset)
this._bufferOffset = start
}
// If the buffer is empty after removing old data, all data has been read.
const hasAllDataBeenRead = len(this._buffer) === 0
if (this._done && hasAllDataBeenRead) {
return null
}
// We already removed data before `start`, so we just return the first
// chunk from the buffer.
return this._buffer.slice(0, end - start)
}

close () {
if (this._reader.cancel) {
this._reader.cancel()
}
}
}
File renamed without changes.
File renamed without changes.