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

feat: checksum extension #347

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

manohar27
Copy link

Adds Checksum Extension support.

This is work in progress.
Uses SubtleCrypto on browser and crypto library on nodejs

@manohar27 manohar27 marked this pull request as draft May 30, 2022 05:34
@Acconut
Copy link
Member

Acconut commented Jul 25, 2022

Thank you very much for this PR! It looks interesting. Let me know once it is ready for a review!

@manohar27 manohar27 marked this pull request as ready for review October 26, 2022 07:17
@manohar27
Copy link
Author

Thank you very much for this PR! It looks interesting. Let me know once it is ready for a review!

Sorry this took so long.

Need some help with code reviews and testing this

@Acconut
Copy link
Member

Acconut commented Oct 29, 2022

Thank you! I will try to review this in the next two weeks!

1token added a commit to cargomail-org/cargomail-tus-js-client that referenced this pull request Jan 8, 2023
Copy link
Member

@Acconut Acconut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you again for this incredible work. I left some comments to your code :)

lib/browser/extensions/checksum.js Outdated Show resolved Hide resolved
lib/browser/extensions/checksum.js Show resolved Hide resolved
lib/browser/extensions/checksum.js Outdated Show resolved Hide resolved
lib/node/extensions/checksum.js Outdated Show resolved Hide resolved
lib/upload.js Outdated Show resolved Hide resolved
lib/upload.js Outdated Show resolved Hide resolved
lib/upload.js Outdated
Checksum = require('./browser/extensions/checksum').default
}
this._checksum = new Checksum(
Checksum.supportedAlogrithms.find((supportedAlgo) => supportedAlgorithms.includes(supportedAlgo)),
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

lib/upload.js Outdated Show resolved Hide resolved
lib/upload.js Outdated
if (value === null) {
if (this.options.enableChecksum) {
if (this._checksum) {
value.arrayBuffer().then((chunkBuffer) => {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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?

@manohar27 manohar27 requested a review from Acconut April 12, 2023 09:03
@manohar27
Copy link
Author

Checksum header

Checksum header being added in the demo browser page

@manohar27
Copy link
Author

Thank you for the review. Please have a look @Acconut, I see there are conflicts. I'll try to address them, might need help

@manohar27 manohar27 changed the title Work In Progress - feat: checksum extension feat: checksum extension Apr 26, 2023
@manohar27
Copy link
Author

@Acconut please have another look at this

@mahyarmirrashed
Copy link

mahyarmirrashed commented Dec 19, 2023

Can I work on this? I may have a need to have this working in the future.
@Acconut

@Acconut
Copy link
Member

Acconut commented Dec 19, 2023

Apologies, I wasn't aware that @manohar27 performed changes after my last review!

@mahyarmirrashed Feel free to continue, but did you have anything specific in mind that needs to be changed here?

@mahyarmirrashed
Copy link

@Acconut I have not taken a close look at the PR. Just wondering if there is a way that I can contribute/continue if needed. I might have a need for the checksum extension in about a month.

Copy link
Member

@Acconut Acconut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, thanks very much for the work and apologies for losing track of this! I left a few comments if you or anybody else is interested in continuing this. It's on a very good track and we just need a few adjustments to get this ready.

"forceAllTransforms": true
} ]
],
"plugins": ["@babel/transform-runtime"]
Copy link
Member

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?

@@ -66,6 +66,7 @@ function startUpload() {
endpoint,
chunkSize,
retryDelays: [0, 1000, 3000, 5000],
checksumAlgo: "SHA-256",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tus protocol itself uses a naming conventional like sha1, so I would prefer if we could also use sha256 here.

class Checksum {
static supportedAlgorithms = ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'];

constructor (algo = 'SHA-256') {
Copy link
Member

Choose a reason for hiding this comment

The 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.

@@ -129,3 +131,7 @@ export class DetailedError extends Error {
originalResponse: HttpResponse
causingError: Error
}

export interface Checksum {
getHexDigest(): string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Upload-Checksum encodes the checksum in base64, instead of hexadecimal. So we need to adjust the interface itself as well as the implementations. I am also wondering if the checksum provider should just return a byte array and let the main tus-js-client logic take care of encoding it into the right format.

@@ -129,3 +131,7 @@ export class DetailedError extends Error {
originalResponse: HttpResponse
causingError: Error
}

export interface Checksum {
getHexDigest(): string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inferface definition is also missing the data argument.


urlStorage?: UrlStorage
fileReader?: FileReader
httpStack?: HttpStack
checksum?: Checksum;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a name that aligns with fileReader for the checksum as well. Maybe we can call this option checksumProvider and the classes also ChecksumGenerator.

"Digest" would also likely be a better fit than checksum, although the protocol specification does not use that term, unfortunately.

})
.catch((err) => {
log(err)
throw err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For error handling, please use _emitError.

@@ -787,4 +787,24 @@ describe('tus', () => {
await assertUrlStorage(tus.defaultOptions.urlStorage)
})
})

describe('#Checksum', () => {
Copy link
Member

Choose a reason for hiding this comment

The 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 test-common, which includes tests shared between browsers and Node.js.

Let me know if you need help with getting this running :)

@@ -129,3 +131,7 @@ export class DetailedError extends Error {
originalResponse: HttpResponse
causingError: Error
}

export interface Checksum {
Copy link
Member

Choose a reason for hiding this comment

The 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.

@mahyarmirrashed
Copy link

I'll try to do some work on this in the new year. By the way @Acconut , should there be any documentation changes with this? Something to let people know how to use this feature?

@Acconut
Copy link
Member

Acconut commented Jan 8, 2024

should there be any documentation changes with this? Something to let people know how to use this feature?

Yes, once we have settled on how the feature's API should work, we can also address the documentation.

I'll try to do some work on this in the new year.

Great! I am looking forward to your contributions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants