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

Types definitions for generateInternalStream and StreamHelper #774

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions documentation/api_jszip/generate_internal_stream.md
Expand Up @@ -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");
});
```
48 changes: 48 additions & 0 deletions index.d.ts
Expand Up @@ -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<any>;

/**
* 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};

Expand Down Expand Up @@ -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
*
Expand Down