Skip to content

Commit e3b5997

Browse files
committed
feat: wire onDownloadProgress, add download progress tracking, bump 2.0.3
- Wire onDownloadProgress config option through convert() -> task.downloadTo() - Add onProgress parameter to FilesAPI.downloadTo() with Content-Length support - Add onProgress parameter to Task.downloadTo() - Fix TypeScript type annotation for nodeStream (NodeJS.ReadableStream) - Update devDependencies to latest in-range versions - Add tests for download progress callback wiring
1 parent f0eceb8 commit e3b5997

17 files changed

Lines changed: 624 additions & 1285 deletions

dist/index.cjs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ var FilesAPI = class {
528528
/**
529529
* Download file to path
530530
*/
531-
async downloadTo(fileId, outputPath) {
531+
async downloadTo(fileId, outputPath, onProgress) {
532532
validateFileId(fileId);
533533
const response = await this.http.get(`/files/${encodeURIComponent(fileId)}`, {
534534
raw: true
@@ -553,7 +553,12 @@ var FilesAPI = class {
553553
if (!response.body) {
554554
throw new ValidationError("No response body");
555555
}
556-
const nodeStream = stream.Readable.fromWeb(response.body);
556+
const contentLength = response.headers.get("content-length");
557+
const total = contentLength ? parseInt(contentLength, 10) : void 0;
558+
let nodeStream = stream.Readable.fromWeb(response.body);
559+
if (onProgress) {
560+
nodeStream = trackStreamProgress(nodeStream, onProgress, total);
561+
}
557562
const writeStream = fs__namespace.createWriteStream(filename);
558563
await new Promise((resolve, reject) => {
559564
nodeStream.pipe(writeStream);
@@ -810,14 +815,14 @@ var Task = class {
810815
/**
811816
* Download result file to path
812817
*/
813-
async downloadTo(outputPath) {
818+
async downloadTo(outputPath, onProgress) {
814819
if (!this._fileId) {
815820
throw new ConversionError(
816821
"No result file available. Task may not be complete.",
817822
this.id
818823
);
819824
}
820-
return this.filesAPI.downloadTo(this._fileId, outputPath);
825+
return this.filesAPI.downloadTo(this._fileId, outputPath, onProgress);
821826
}
822827
/**
823828
* Update task state from API response
@@ -941,7 +946,7 @@ var ConversionToolsClient = class {
941946
}
942947
}
943948
});
944-
const outputPath = await task.downloadTo(output);
949+
const outputPath = await task.downloadTo(output, this.config.onDownloadProgress);
945950
return outputPath;
946951
}
947952
/**

dist/index.cjs.map

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/index.d.cts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ declare class FilesAPI {
700700
/**
701701
* Download file to path
702702
*/
703-
downloadTo(fileId: string, outputPath?: string): Promise<string>;
703+
downloadTo(fileId: string, outputPath?: string, onProgress?: (progress: ProgressEvent) => void): Promise<string>;
704704
}
705705

706706
/**
@@ -839,7 +839,7 @@ declare class Task {
839839
/**
840840
* Download result file to path
841841
*/
842-
downloadTo(outputPath?: string): Promise<string>;
842+
downloadTo(outputPath?: string, onProgress?: (progress: ProgressEvent) => void): Promise<string>;
843843
/**
844844
* Update task state from API response
845845
*/

dist/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ declare class FilesAPI {
700700
/**
701701
* Download file to path
702702
*/
703-
downloadTo(fileId: string, outputPath?: string): Promise<string>;
703+
downloadTo(fileId: string, outputPath?: string, onProgress?: (progress: ProgressEvent) => void): Promise<string>;
704704
}
705705

706706
/**
@@ -839,7 +839,7 @@ declare class Task {
839839
/**
840840
* Download result file to path
841841
*/
842-
downloadTo(outputPath?: string): Promise<string>;
842+
downloadTo(outputPath?: string, onProgress?: (progress: ProgressEvent) => void): Promise<string>;
843843
/**
844844
* Update task state from API response
845845
*/

dist/index.js

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

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/legacy.cjs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ var FilesAPI = class {
528528
/**
529529
* Download file to path
530530
*/
531-
async downloadTo(fileId, outputPath) {
531+
async downloadTo(fileId, outputPath, onProgress) {
532532
validateFileId(fileId);
533533
const response = await this.http.get(`/files/${encodeURIComponent(fileId)}`, {
534534
raw: true
@@ -553,7 +553,12 @@ var FilesAPI = class {
553553
if (!response.body) {
554554
throw new ValidationError("No response body");
555555
}
556-
const nodeStream = stream.Readable.fromWeb(response.body);
556+
const contentLength = response.headers.get("content-length");
557+
const total = contentLength ? parseInt(contentLength, 10) : void 0;
558+
let nodeStream = stream.Readable.fromWeb(response.body);
559+
if (onProgress) {
560+
nodeStream = trackStreamProgress(nodeStream, onProgress, total);
561+
}
557562
const writeStream = fs__namespace.createWriteStream(filename);
558563
await new Promise((resolve, reject) => {
559564
nodeStream.pipe(writeStream);
@@ -810,14 +815,14 @@ var Task = class {
810815
/**
811816
* Download result file to path
812817
*/
813-
async downloadTo(outputPath) {
818+
async downloadTo(outputPath, onProgress) {
814819
if (!this._fileId) {
815820
throw new ConversionError(
816821
"No result file available. Task may not be complete.",
817822
this.id
818823
);
819824
}
820-
return this.filesAPI.downloadTo(this._fileId, outputPath);
825+
return this.filesAPI.downloadTo(this._fileId, outputPath, onProgress);
821826
}
822827
/**
823828
* Update task state from API response
@@ -941,7 +946,7 @@ var ConversionToolsClient = class {
941946
}
942947
}
943948
});
944-
const outputPath = await task.downloadTo(output);
949+
const outputPath = await task.downloadTo(output, this.config.onDownloadProgress);
945950
return outputPath;
946951
}
947952
/**

dist/legacy.cjs.map

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/legacy.js

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy.js.map

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

0 commit comments

Comments
 (0)