From 81f017a9a82470463fa28154a89368b58c76533f Mon Sep 17 00:00:00 2001 From: Lukas Steuer <13337295+xxxLukskyxxx@users.noreply.github.com> Date: Tue, 27 Jul 2021 11:11:06 +0200 Subject: [PATCH 1/2] Update Pull request #698 | Types definitions Types definitions for generateInternalStream and StreamHelper linked to issue #697 and pull request #698 --- index.d.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/index.d.ts b/index.d.ts index d3531b51..1caf24b2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -146,6 +146,46 @@ declare namespace JSZip { } } +declare namespace StreamHelper { + type DataEventCallback = (dataChunk: any, metadata: any) => any + type EndEventCallback = () => any + type ErrorEventCallback = (error: Error) => any + + interface StreamHelper { + /** + * Register a listener on an event + * + * @param event The name of the event. Only 3 events are supported : data, end and error + * @param callback The function called when the event occurs + * @return The current StreamHelper object, for chaining + */ + on(event: 'data' | 'end' | 'error', callback: DataEventCallback | EndEventCallback | ErrorEventCallback); + + /** + * Read the whole stream and call a callback with the complete content + * + * @param updateCallback The function called every time the stream updates + * @param metadata Metadata contains for example currentFile and percent + * @return A Promise of the full content + */ + accumulate(updateCallback?): Promise; + + /** + * Resume the stream if the stream is paused. Once resumed, the stream starts sending data events again + * + * @return The current StreamHelper object, for chaining + */ + resume(): StreamHelper; + + /** + * Pause the stream if the stream is running. Once paused, the stream stops sending data events + * + * @return The current StreamHelper object, for chaining + */ + pause(): StreamHelper; + } +} + interface JSZip { files: {[key: string]: JSZip.JSZipObject}; @@ -233,6 +273,14 @@ interface JSZip { */ generateNodeStream(options?: JSZip.JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: OnUpdateCallback): NodeJS.ReadableStream; + /** + * Generates the complete zip file with the internal stream implementation + * + * @param options Optional options for the generator + * @return a StreamHelper + */ + generateInternalStream(options?: JSZip.JSZipGeneratorOptions): StreamHelper.StreamHelper + /** * Deserialize zip file asynchronously * From 581fdedaadaedf7099b492320774ec8e054eb436 Mon Sep 17 00:00:00 2001 From: Lukas Steuer <13337295+xxxLukskyxxx@users.noreply.github.com> Date: Tue, 27 Jul 2021 11:18:58 +0200 Subject: [PATCH 2/2] Update generate_internal_stream.md Example code did not work --- documentation/api_jszip/generate_internal_stream.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/documentation/api_jszip/generate_internal_stream.md b/documentation/api_jszip/generate_internal_stream.md index 78fde7ad..86b89857 100644 --- a/documentation/api_jszip/generate_internal_stream.md +++ b/documentation/api_jszip/generate_internal_stream.md @@ -26,8 +26,7 @@ zip.generateInternalStream({type:"blob"}).accumulate(function callback(err, cont // handle error } // see FileSaver.js - saveAs(content, "hello.zip"); -}, function updateCallback(metadata) { - // print progression with metadata.percent and metadata.currentFile +}.then(function (data){ + saveAs(data, "hello.zip"); }); ```