-
Notifications
You must be signed in to change notification settings - Fork 332
feat: checksum extension #347
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
262d44f
9c0526c
eb51375
f887195
5e18e6f
a036309
05adba7
5d353ea
bfc6179
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
"corejs": false, | ||
"forceAllTransforms": true | ||
} ] | ||
] | ||
], | ||
"plugins": ["@babel/transform-runtime"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSaveMode": "modifications" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Checksum { | ||
static supportedAlogrithms = ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512']; | ||
|
||
constructor (algo = 'SHA-256') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need a default value here. If no algorithm or an unsupported one is supplied, we can just error out. |
||
this.algo = this._resolveAlgo(algo) | ||
} | ||
|
||
/** | ||
* Resolves algorithm to one of the inbuilt types | ||
* @param {String} algo contains user provided checksumAlgo option | ||
*/ | ||
_resolveAlgo = (algo) => { | ||
const resolvedAlgo = Checksum.supportedAlgorithms.find(supportedAlgo => supportedAlgo === algo.toUpperCase()) | ||
manohar27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!resolvedAlgo) { | ||
throw new Error( | ||
`tus: unsupported checksumAlgo provided. Supported values are : ${Checksum.supportedAlgorithms.join(',')}`, | ||
) | ||
} | ||
return resolvedAlgo | ||
} | ||
/** | ||
* Gets Hexadecimal digest using the algorithm set in this.algo | ||
* @param {ArrayBuffer} data contains the chunk of data to be hashed | ||
*/ | ||
|
||
getHexDigest = async (data) => { | ||
try { | ||
const hashBuffer = await crypto.subtle.digest(this.algo, data) | ||
manohar27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const hashArray = Array.from(new Uint8Array(hashBuffer)) // convert buffer to byte array | ||
const hashHex = hashArray | ||
.map((b) => b.toString(16).padStart(2, '0')) | ||
.join('') // convert bytes to hex string | ||
return hashHex | ||
} catch (err) { | ||
throw new Error('tus: could not compute checksum for integrity check') | ||
manohar27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}; | ||
} | ||
export default Checksum |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import crypto from 'crypto' | ||
|
||
class Checksum { | ||
static supportedAlgorithms = ['sha1', 'sha256', 'sha384', 'sha512', 'md5']; | ||
|
||
constructor (algo = 'sha256') { | ||
this.algo = this.resolveAlgo(algo) | ||
} | ||
|
||
/** | ||
* Resolves algorithm to one of the inbuilt types | ||
* @param {String} algo contains user provided checksumAlgo option | ||
*/ | ||
resolveAlgo = (algo) => { | ||
const resolvedAlgo = Checksum.supportedAlgorithms.find(supportedAlgo => supportedAlgo === algo.toLowerCase()) | ||
manohar27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!resolvedAlgo) throw new Error(`Checksum: unsupported checksumAlgo provided. Supported values are ${Checksum.supportedAlgorithms.join(',')}`) | ||
return resolvedAlgo | ||
} | ||
|
||
/** | ||
* Gets Hexadecimal digest using the algorithm set in this.algo | ||
* @param {ArrayBuffer} data contains the chunk of data to be hashed | ||
*/ | ||
getHexDigest = async (data) => { | ||
try { | ||
const hashHex = await crypto | ||
.createHash(this.algo) | ||
.update(data) | ||
.digest('hex') | ||
|
||
return hashHex | ||
} catch (err) { | ||
throw new Error('tus: could not compute checksum for integrity check') | ||
} | ||
}; | ||
} | ||
|
||
export default Checksum |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,9 @@ class BaseUpload { | |
// An array of upload URLs which are used for uploading the different | ||
// parts, if the parallelUploads option is used. | ||
this._parallelUploadUrls = null | ||
|
||
// A platform dependent checksum generator, this is instantiated if options.enableChecksum is provided | ||
this._checksum = null | ||
} | ||
|
||
/** | ||
|
@@ -531,6 +534,40 @@ class BaseUpload { | |
return | ||
} | ||
|
||
if (this.options.enableChecksum) { | ||
const optionsReq = this._openRequest('OPTIONS', this.options.endpoint) | ||
manohar27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this._sendRequest(optionsReq) | ||
.then((res) => { | ||
const isChecksumSupported = res | ||
.getHeader('Tus-Extension') | ||
.split(',') | ||
.includes('checksum') | ||
if (!isChecksumSupported) { | ||
log('Checksum is not supported by this tus server') | ||
return | ||
} | ||
const supportedAlgorithms = res.getHeader('Tus-Checksum-Algorithm') | ||
if (!supportedAlgorithms) { | ||
throw new Error( | ||
'tus: checksum is enabled, but Tus-Checksum-Algorithm is not defined by tus server', | ||
) | ||
} | ||
let Checksum | ||
if (window) { | ||
manohar27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// eslint-disable-next-line global-require | ||
Checksum = require('./browser/extensions/checksum').default | ||
} else { | ||
// eslint-disable-next-line global-require | ||
Checksum = require('./browser/extensions/checksum').default | ||
} | ||
this._checksum = new Checksum( | ||
Checksum.supportedAlogrithms.find((supportedAlgo) => supportedAlgorithms.includes(supportedAlgo)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this check do? The combination of find and includes looks suspicious. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to fetch the supported algorithms from the server and see if one of them matches one of the supported algorithms on the client. If we're saying client and server have agreed upon an algorithm from the get go, then we can skip this OPTIONS call and let client just configure the algorithm in options. |
||
) | ||
}) | ||
.catch((err) => { | ||
log('Failed to fetch options response', err) | ||
}) | ||
} | ||
const req = this._openRequest('POST', this.options.endpoint) | ||
|
||
if (this.options.uploadLengthDeferred) { | ||
|
@@ -749,11 +786,25 @@ class BaseUpload { | |
req.setHeader('Upload-Length', this._size) | ||
} | ||
|
||
if (value === null) { | ||
if (this.options.enableChecksum) { | ||
if (this._checksum) { | ||
value.arrayBuffer().then((chunkBuffer) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this entire if block be merged with the last else block? They seem very similar and would avoid some duplication. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how this might reduce the duplication as one is async. I've removed one outer if condition. Is that okay? |
||
this._checksum | ||
.getHexDigest(chunkBuffer) | ||
.then((hash) => { | ||
req.setHeader('Upload-Checksum', `${this._checksum.algo} ${hash}`) | ||
this._emitProgress(this._offset, this._size) | ||
return this._sendRequest(req, value) | ||
}) | ||
.catch((err) => log(err)) | ||
manohar27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
} else if (value === null) { | ||
return this._sendRequest(req) | ||
} else { | ||
this._emitProgress(this._offset, this._size) | ||
return this._sendRequest(req, value) | ||
} | ||
this._emitProgress(this._offset, this._size) | ||
return this._sendRequest(req, value) | ||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
const assertUrlStorage = require('./helpers/assertUrlStorage') | ||
const { TestHttpStack, waitableFunction, wait } = require('./helpers/utils') | ||
const tus = require('../..') | ||
const { default: Checksum } = require('../../lib.es5/browser/extensions/checksum') | ||
manohar27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
describe('tus', () => { | ||
beforeEach(() => { | ||
|
@@ -778,4 +779,24 @@ describe('tus', () => { | |
await assertUrlStorage(tus.defaultOptions.urlStorage) | ||
}) | ||
}) | ||
|
||
describe('#Checksum', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would also need a full test to ensure the checksum header is added to the actual requests. Such a test should be placed in Let me know if you need help with getting this running :) |
||
it('should generate hex digest for a given chunk of file', async () => { | ||
const checksum = new Checksum() | ||
const hexDigest = await checksum.getHexDigest( | ||
new TextEncoder().encode('hello'), | ||
) | ||
expect(hexDigest).toBe( | ||
'2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824', | ||
) | ||
}) | ||
|
||
it('should throw an error when an unsupported algo is provided', () => { | ||
try { | ||
const checksum = new Checksum('md5') | ||
} catch (err) { | ||
expect(err.message).toContain('unsupported checksumAlgo') | ||
} | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain why this plugin is necessary?