Skip to content

Commit 580bf98

Browse files
loren-m-crawfordMarius Kleidl
andauthored
Add metadataForPartialUploads option to set metadata for partial uploads only (#703)
* Add partialMetadata to options. * Add api docs for partialMetadata. * Rename to `metadataForPartialUploads` * Simplify implementation and tests * Update documentation --------- Co-authored-by: Marius Kleidl <[email protected]>
1 parent 1cef03e commit 580bf98

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

docs/api.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ metadata: {
166166
}
167167
```
168168

169+
#### metadataForPartialUploads
170+
171+
_Default value:_ `{}`
172+
173+
An object with string values used as additional meta data for partial uploads. When parallel uploads are enabled via `parallelUploads`, tus-js-client creates multiple partial uploads. The values from `metadata` are not passed to these partial uploads but only passed to the final upload, which is the concatentation of the partial uploads. In contrast, the values from `metadataForPartialUploads` are only passed to the partial uploads and not the final upload. This option has no effect if parallel uploads are not enabled. Can be used to associate partial uploads to a user, for example:
174+
175+
```js
176+
metadataForPartialUploads: {
177+
userId: "1234567"
178+
}
179+
```
180+
169181
#### uploadUrl
170182

171183
_Default value:_ `null`
@@ -234,7 +246,7 @@ X-Request-ID: fe51f777-f23e-4ed9-97d7-2785cc69f961
234246

235247
_Default value:_ `1`
236248

237-
A number indicating how many parts should be uploaded in parallel. If this number is not `1`, the input file will be split into multiple parts, where each part is uploaded individually in parallel. The value of `parallelUploads` determines the number of parts. Using `parallelUploadBoundaries` the size of each part can be changed. After all parts have been uploaded, the [`concatenation` extension](https://tus.io/protocols/resumable-upload.html#concatenation) will be used to concatenate all the parts together on the server-side, so the tus server must support this extension. This option should not be used if the input file is a streaming resource.
249+
A number indicating how many parts should be uploaded in parallel. If this number is not `1`, the input file will be split into multiple parts, where each part is uploaded individually in parallel. The value of `parallelUploads` determines the number of parts. Using `parallelUploadBoundaries` the size of each part can be changed. After all parts have been uploaded, the [`concatenation` extension](https://tus.io/protocols/resumable-upload.html#concatenation) will be used to concatenate all the parts together on the server-side, so the tus server must support this extension. This option should not be used if the input file is a streaming resource. By default, the values from `metadata` are not passed to the partial uploads and only used for the final upload where the parts are concatenated together again. The `metadataForPartialUploads` option can be used to set meta data specifically for partial uploads.
238250

239251
The idea behind this option is that you can use multiple HTTP requests in parallel to better utilize the full capacity of the network connection to the tus server. If you want to use it, please evaluate it under real world situations to see if it actually improves your upload performance. In common browser session, we were not able to find a performance improve for the average user.
240252

lib/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ interface UploadOptions {
2626

2727
uploadUrl?: string | null
2828
metadata?: { [key: string]: string }
29+
metadataForPartialUploads?: { [key: string]: string }
2930
fingerprint?: (file: File, options: UploadOptions) => Promise<string>
3031
uploadSize?: number | null
3132

lib/index.test-d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const upload = new tus.Upload(file, {
1919
metadata: {
2020
filename: 'foo.txt',
2121
},
22+
metadataForPartialUploads: {
23+
userId: 'foo123bar',
24+
},
2225
onProgress: (bytesSent: number, bytesTotal: number) => {
2326
const percentage = ((bytesSent / bytesTotal) * 100).toFixed(2)
2427
console.log(bytesSent, bytesTotal, `${percentage}%`)

lib/upload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const defaultOptions = {
1212

1313
uploadUrl: null,
1414
metadata: {},
15+
metadataForPartialUploads: {},
1516
fingerprint: null,
1617
uploadSize: null,
1718

@@ -331,7 +332,7 @@ class BaseUpload {
331332
parallelUploads: 1,
332333
// Reset this option as we are not doing a parallel upload.
333334
parallelUploadBoundaries: null,
334-
metadata: {},
335+
metadata: this.options.metadataForPartialUploads,
335336
// Add the header to indicate the this is a partial upload.
336337
headers: {
337338
...this.options.headers,

test/spec/test-parallel-uploads.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ describe('tus', () => {
7676
metadata: {
7777
foo: 'hello',
7878
},
79+
metadataForPartialUploads: {
80+
test: 'world',
81+
},
7982
onProgress() {},
8083
onSuccess: waitableFunction(),
8184
fingerprint: () => Promise.resolve('fingerprinted'),
@@ -92,7 +95,7 @@ describe('tus', () => {
9295
expect(req.requestHeaders['Tus-Resumable']).toBe('1.0.0')
9396
expect(req.requestHeaders['Upload-Length']).toBe('5')
9497
expect(req.requestHeaders['Upload-Concat']).toBe('partial')
95-
expect(req.requestHeaders['Upload-Metadata']).toBeUndefined()
98+
expect(req.requestHeaders['Upload-Metadata']).toBe('test d29ybGQ=') // world
9699

97100
req.respondWith({
98101
status: 201,
@@ -108,7 +111,7 @@ describe('tus', () => {
108111
expect(req.requestHeaders['Tus-Resumable']).toBe('1.0.0')
109112
expect(req.requestHeaders['Upload-Length']).toBe('6')
110113
expect(req.requestHeaders['Upload-Concat']).toBe('partial')
111-
expect(req.requestHeaders['Upload-Metadata']).toBeUndefined()
114+
expect(req.requestHeaders['Upload-Metadata']).toBe('test d29ybGQ=') // world
112115

113116
req.respondWith({
114117
status: 201,
@@ -188,7 +191,7 @@ describe('tus', () => {
188191
expect(req.requestHeaders['Upload-Concat']).toBe(
189192
'final;https://tus.io/uploads/upload1 https://tus.io/uploads/upload2',
190193
)
191-
expect(req.requestHeaders['Upload-Metadata']).toBe('foo aGVsbG8=')
194+
expect(req.requestHeaders['Upload-Metadata']).toBe('foo aGVsbG8=') // hello
192195

193196
req.respondWith({
194197
status: 201,

0 commit comments

Comments
 (0)