-
Notifications
You must be signed in to change notification settings - Fork 316
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
feat: checksum extension #347
base: main
Are you sure you want to change the base?
Changes from all 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 |
---|---|---|
@@ -1,12 +1,10 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"modules": false, | ||
"corejs": false, | ||
"forceAllTransforms": true | ||
} | ||
] | ||
] | ||
"presets": [ | ||
[ "@babel/preset-env", { | ||
"modules": false, | ||
"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 |
---|---|---|
|
@@ -66,6 +66,7 @@ function startUpload() { | |
endpoint, | ||
chunkSize, | ||
retryDelays: [0, 1000, 3000, 5000], | ||
checksumAlgo: "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. The tus protocol itself uses a naming conventional like |
||
parallelUploads, | ||
metadata: { | ||
filename: file.name, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Checksum { | ||
static supportedAlgorithms = ['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. |
||
// Checking support for crypto on the browser | ||
if (!crypto) { | ||
throw new Error(`tus: this browser does not support checksum`) | ||
} else if (!Checksum.supportedAlgorithms.includes(algo)) { | ||
throw new Error( | ||
`tus: unsupported checksumAlgo provided. Supported values are : ${Checksum.supportedAlgorithms.join( | ||
',', | ||
)}`, | ||
) | ||
} else { | ||
this.algo = algo | ||
} | ||
} | ||
|
||
/** | ||
* 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', err) | ||
} | ||
}; | ||
} | ||
export default Checksum |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,10 +51,12 @@ interface UploadOptions { | |
removeFingerprintOnSuccess?: boolean | ||
uploadLengthDeferred?: boolean | ||
uploadDataDuringCreation?: boolean | ||
checksumAlgo?: string | ||
|
||
urlStorage?: UrlStorage | ||
fileReader?: FileReader | ||
httpStack?: HttpStack | ||
checksum?: 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. I would prefer a name that aligns with "Digest" would also likely be a better fit than checksum, although the protocol specification does not use that term, unfortunately. |
||
} | ||
|
||
interface UrlStorage { | ||
|
@@ -129,3 +131,7 @@ export class DetailedError extends Error { | |
originalResponse: HttpResponse | ||
causingError: Error | ||
} | ||
|
||
export interface 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. Once we have finalized the interface and options, we should also add documentation. But that can wait for now. |
||
getHexDigest(): string; | ||
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. The 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. The inferface definition is also missing the |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import crypto from 'crypto' | ||
|
||
class Checksum { | ||
static supportedAlgorithms = ['sha1', 'sha256', 'sha384', 'sha512', 'md5']; | ||
|
||
constructor (algo = 'sha256') { | ||
if (!Checksum.supportedAlgorithms.includes(algo)) { | ||
throw new Error( | ||
`Checksum: unsupported checksumAlgo provided. Supported values are ${Checksum.supportedAlgorithms.join( | ||
',', | ||
)}`, | ||
) | ||
} else { | ||
this.algo = algo | ||
} | ||
} | ||
|
||
/** | ||
* 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 |
---|---|---|
|
@@ -34,9 +34,10 @@ const defaultOptions = { | |
uploadLengthDeferred: false, | ||
uploadDataDuringCreation: false, | ||
|
||
urlStorage: null, | ||
fileReader: null, | ||
httpStack: null, | ||
urlStorage : null, | ||
fileReader : null, | ||
httpStack : null, | ||
checksumAlgo: null, | ||
} | ||
|
||
class BaseUpload { | ||
|
@@ -102,6 +103,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.checksumAlgo is provided | ||
this._checksum = null | ||
} | ||
|
||
/** | ||
|
@@ -565,6 +569,9 @@ class BaseUpload { | |
return | ||
} | ||
|
||
if (this.options.checksumAlgo) { | ||
this._checksum = new this.options.Checksum(this.options.checksumAlgo) | ||
} | ||
const req = this._openRequest('POST', this.options.endpoint) | ||
|
||
if (this.options.uploadLengthDeferred) { | ||
|
@@ -793,8 +800,27 @@ class BaseUpload { | |
if (value === null) { | ||
return this._sendRequest(req) | ||
} | ||
this._emitProgress(this._offset, this._size) | ||
return this._sendRequest(req, value) | ||
|
||
if (this.options.checksumAlgo) { | ||
if (this._checksum) { | ||
value.arrayBuffer().then((chunkBuffer) => { | ||
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) | ||
throw err | ||
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. For error handling, please use _emitError. |
||
}) | ||
}) | ||
} | ||
} else { | ||
this._emitProgress(this._offset, this._size) | ||
return this._sendRequest(req, value) | ||
} | ||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -787,4 +787,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 tus.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 tus.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?