Skip to content

Commit a2c3ae0

Browse files
Merge pull request #2 from apivideo/add-attributes-to-progress-events
Add attributes to progress event
2 parents 140194a + cb807b4 commit a2c3ae0

File tree

7 files changed

+77
-20
lines changed

7 files changed

+77
-20
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
![](https://github.com/apivideo/API_OAS_file/blob/master/apivideo_banner.png)
22

33
# api.video video uploader
4+
![npm](https://img.shields.io/npm/v/@api.video/video-uploader) ![ts](https://badgen.net/badge/-/TypeScript/blue?icon=typescript&label)
45

56
Typescript library to upload videos to api.video using delegated upload token (or usual access token) from the front-end.
67

@@ -145,14 +146,25 @@ On fail, the promise embeds the status code & error message returned by the API.
145146
## `onProgress()`
146147

147148
The onProgress() method let you defined an upload progress listener. It takes a callback function with one parameter: the onProgress events.
148-
An onProgress event contains 2 attributes:
149-
- loaded: the number of uploaded bytes
150-
- total: the total number of bytes
149+
An onProgress event contains the following attributes:
150+
- uploadedBytes (number): total number of bytes uploaded for this upload
151+
- totalBytes (number): total size of the file
152+
- chunksCount (number): number of upload chunks
153+
- chunksBytes (number): size of a chunk
154+
- currentChunk (number): index of the chunk being uploaded
155+
- currentChunkUploadedBytes (number): number of bytes uploaded for the current chunk
151156

152157
### Example
153158

154159
```javascript
155160
// ... uploader instanciation
156161

157-
uploader.onProgress((event) => console.log(event.loaded, event.total));
162+
uploader.onProgress((event) => {
163+
console.log(`total number of bytes uploaded for this upload: ${event.uploadedBytes}.`);
164+
console.log(`total size of the file: ${event.totalBytes}.`);
165+
console.log(`number of upload chunks: ${event.chunksCount} .`);
166+
console.log(`size of a chunk: ${event.chunksBytes}.`);
167+
console.log(`index of the chunk being uploaded: ${event.currentChunk}.`);
168+
console.log(`number of bytes uploaded for the current chunk: ${event.currentChunkUploadedBytes}.`);
169+
});
158170
```

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/index.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ interface Options {
1111
apiHost?: string;
1212
retries?: number;
1313
}
14-
interface UploadProgressEvent {
15-
loaded: number;
16-
total: number;
14+
export interface UploadProgressEvent {
15+
uploadedBytes: number;
16+
totalBytes: number;
17+
chunksCount: number;
18+
chunksBytes: number;
19+
currentChunk: number;
20+
currentChunkUploadedBytes: number;
1721
}
1822
export declare class VideoUploader {
1923
private file;
@@ -28,7 +32,7 @@ export declare class VideoUploader {
2832
private onProgressCallbacks;
2933
private headers;
3034
constructor(options: OptionsWithAccessToken | OptionsWithUploadToken);
31-
onProgress(cb: () => UploadProgressEvent): void;
35+
onProgress(cb: (e: UploadProgressEvent) => void): void;
3236
upload(): Promise<any>;
3337
private sleep;
3438
private createFormData;

dist/test/index.test.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/video-uploader",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "api.video video uploader",
55
"repository": {
66
"type": "git",

src/index.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ interface Options {
1212
retries?: number;
1313
}
1414

15-
interface UploadProgressEvent {
16-
loaded: number;
17-
total: number;
15+
export interface UploadProgressEvent {
16+
uploadedBytes: number;
17+
totalBytes: number;
18+
chunksCount: number;
19+
chunksBytes: number;
20+
currentChunk: number;
21+
currentChunkUploadedBytes: number;
1822
}
1923

2024
const DEFAULT_CHUNK_SIZE = 1024 * 1024; // 1mb
@@ -65,7 +69,7 @@ export class VideoUploader {
6569
this.chunksCount = Math.ceil(this.fileSize / this.chunkSize);
6670
}
6771

68-
public onProgress(cb: () => UploadProgressEvent) {
72+
public onProgress(cb: (e: UploadProgressEvent) => void) {
6973
this.onProgressCallbacks.push(cb);
7074
}
7175

@@ -80,7 +84,7 @@ export class VideoUploader {
8084
this.currentChunk++;
8185

8286
} catch (e) {
83-
if(retriesCount >= this.retries) {
87+
if (retriesCount >= this.retries) {
8488
reject(e);
8589
break;
8690
}
@@ -122,7 +126,7 @@ export class VideoUploader {
122126
for (const headerName of Object.keys(this.headers)) {
123127
xhr.setRequestHeader(headerName, this.headers[headerName]);
124128
}
125-
xhr.onreadystatechange = (e) => {
129+
xhr.onreadystatechange = (_) => {
126130
if (xhr.readyState === 4) { // DONE
127131
if (xhr.status >= 400) {
128132
reject({
@@ -132,10 +136,14 @@ export class VideoUploader {
132136
}
133137
}
134138
};
135-
xhr.onload = (e) => resolve(JSON.parse(xhr.response));
139+
xhr.onload = (_) => resolve(JSON.parse(xhr.response));
136140
xhr.onprogress = (e) => this.onProgressCallbacks.forEach(cb => cb({
137-
loaded: e.loaded + firstByte,
138-
total: this.fileSize
141+
uploadedBytes: e.loaded + firstByte,
142+
totalBytes: this.fileSize,
143+
chunksCount: this.chunksCount,
144+
chunksBytes: this.chunkSize,
145+
currentChunk: this.currentChunk + 1,
146+
currentChunkUploadedBytes: e.loaded,
139147
}));
140148
xhr.send(this.createFormData(firstByte, lastByte));
141149
});

test/index.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai';
2-
import { VideoUploader } from '../src/index';
2+
import { UploadProgressEvent, VideoUploader } from '../src/index';
33
import mock from 'xhr-mock';
44

55

@@ -78,6 +78,38 @@ describe('Access token auth', () => {
7878
});
7979
});
8080

81+
describe('Progress listener', () => {
82+
beforeEach(() => mock.setup());
83+
afterEach(() => mock.teardown());
84+
85+
it('upload retries', (done) => {
86+
const videoId = "9876";
87+
let lastUploadProgressEvent: UploadProgressEvent;
88+
89+
const uploader = new VideoUploader({
90+
file: new File([new ArrayBuffer(2000)], "filename"),
91+
accessToken: "1234",
92+
videoId,
93+
chunkSize: 500
94+
});
95+
96+
mock.post(`https://ws.api.video/videos/${videoId}/source`, (req, res) => res.status(201).body("{}"));
97+
98+
uploader.onProgress((e: UploadProgressEvent) => lastUploadProgressEvent = e);
99+
100+
uploader.upload().then(() => {
101+
expect(lastUploadProgressEvent).to.deep.equal({
102+
...lastUploadProgressEvent,
103+
totalBytes: 2000,
104+
chunksCount: 4,
105+
chunksBytes: 500,
106+
currentChunk: 4,
107+
});
108+
done();
109+
});
110+
});
111+
});
112+
81113
describe('Errors & retries', () => {
82114
beforeEach(() => mock.setup());
83115
afterEach(() => mock.teardown());

0 commit comments

Comments
 (0)