Skip to content

Commit ac6807e

Browse files
committed
For Issue #44, introduce a @typedef for UnarchiverOptions
1 parent df5c5cf commit ac6807e

File tree

6 files changed

+73
-46
lines changed

6 files changed

+73
-46
lines changed

archive/decompress-internal.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import { findMimeType } from '../file/sniffer.js';
1717
* @property {Uint8Array} fileData
1818
*/
1919

20+
/**
21+
* @typedef DecompressorOptions
22+
* @property {string} pathToBitJS The path to the bitjs folder.
23+
* @property {boolean=} debug Set to true for verbose decompressor logging.
24+
*/
25+
2026
/**
2127
* The UnarchiveEvent types.
2228
*/
@@ -188,18 +194,16 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
188194
* to the decompress implementation.
189195
* @param {Function(string, MessagePort):Promise<*>} connectPortFn A function that takes a path
190196
* to a JS decompression implementation (unzip.js) and connects it to a MessagePort.
191-
* @param {Object|string} options An optional object of options, or a string representing where
192-
* the BitJS files are located. The string version of this argument is deprecated.
193-
* Available options:
194-
* 'pathToBitJS': A string indicating where the BitJS files are located.
195-
* 'debug': A boolean where true indicates that the archivers should log debug output.
197+
* @param {DecompressorOptions|string} options An optional object of options, or a string
198+
* representing where the BitJS files are located. The string version of this argument is
199+
* deprecated.
196200
*/
197201
constructor(arrayBuffer, connectPortFn, options = {}) {
198202
super();
199203

200204
if (typeof options === 'string') {
201205
console.warn(`Deprecated: Don't send a raw string to Unarchiver()`);
202-
console.warn(` send {'pathToBitJS':'${options}'} instead`);
206+
console.warn(` send DecompressorOptions instead.`);
203207
options = { 'pathToBitJS': options };
204208
}
205209

archive/decompress.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export {
3838
* @property {Uint8Array} fileData
3939
*/
4040

41+
/**
42+
* @typedef {import('./decompress-internal.js').DecompressorOptions} DecompressorOptions
43+
*/
44+
4145
/**
4246
* Creates a WebWorker with the given decompressor implementation (i.e. unzip.js)
4347
* and transfers a MessagePort for communication. Returns a Promise to the Worker.
@@ -78,9 +82,10 @@ export class Untarrer extends UntarrerInternal {
7882
* in the ArrayBuffer.
7983
* @param {ArrayBuffer} ab The ArrayBuffer to unarchive. Note that this ArrayBuffer
8084
* must not be referenced after calling this method, as the ArrayBuffer may be
81-
* tranferred to a different JS context once start() is called.
82-
* @param {Object|string} options An optional object of options, or a string
83-
* representing where the path to the unarchiver script files.
85+
* transferred to a different JS context once start() is called.
86+
* @param {DecompressorOptions|string} options An optional object of options, or a
87+
* string representing where the path to the unarchiver script files. The latter
88+
* is now deprecated (use DecompressorOptions).
8489
* @returns {Unarchiver}
8590
*/
8691
export function getUnarchiver(ab, options = {}) {

types/archive/decompress-internal.d.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* Factory method that creates an unarchiver based on the byte signature found
33
* in the arrayBuffer.
44
* @param {ArrayBuffer} ab
5-
* @param {Function(string):Worker} createWorkerFn A function that creates a Worker from a script file.
5+
* @param {Function(string):Promise<*>} connectPortFn A function that connects the impl port.
66
* @param {Object|string} options An optional object of options, or a string representing where
77
* the path to the unarchiver script files.
88
* @returns {Unarchiver}
99
*/
10-
export function getUnarchiverInternal(ab: ArrayBuffer, createWorkerFn: any, options?: any | string): Unarchiver;
10+
export function getUnarchiverInternal(ab: ArrayBuffer, connectPortFn: any, options?: any | string): Unarchiver;
1111
export namespace UnarchiveEventType {
1212
const START: string;
1313
const APPEND: string;
@@ -118,27 +118,39 @@ export class Unarchiver extends EventTarget {
118118
/**
119119
* @param {ArrayBuffer} arrayBuffer The Array Buffer. Note that this ArrayBuffer must not be
120120
* referenced once it is sent to the Unarchiver, since it is marked as Transferable and sent
121-
* to the Worker.
122-
* @param {Function(string):Worker} createWorkerFn A function that creates a Worker from a script file.
123-
* @param {Object|string} options An optional object of options, or a string representing where
124-
* the BitJS files are located. The string version of this argument is deprecated.
125-
* Available options:
126-
* 'pathToBitJS': A string indicating where the BitJS files are located.
127-
* 'debug': A boolean where true indicates that the archivers should log debug output.
121+
* to the decompress implementation.
122+
* @param {Function(string, MessagePort):Promise<*>} connectPortFn A function that takes a path
123+
* to a JS decompression implementation (unzip.js) and connects it to a MessagePort.
124+
* @param {UnarchiverOptions|string} options An optional object of options, or a string
125+
* representing where the BitJS files are located. The string version of this argument is
126+
* deprecated.
128127
*/
129-
constructor(arrayBuffer: ArrayBuffer, createWorkerFn: any, options?: any | string);
128+
constructor(arrayBuffer: ArrayBuffer, connectPortFn: any, options?: UnarchiverOptions | string);
129+
/**
130+
* A handle to the decompressor implementation context.
131+
* @type {Worker|*}
132+
* @private
133+
*/
134+
private implRef_;
135+
/**
136+
* The client-side port that sends messages to, and receives messages from the
137+
* decompressor implementation.
138+
* @type {MessagePort}
139+
* @private
140+
*/
141+
private port_;
130142
/**
131143
* The ArrayBuffer object.
132144
* @type {ArrayBuffer}
133145
* @protected
134146
*/
135147
protected ab: ArrayBuffer;
136148
/**
137-
* A factory method that creates a Worker that does the unarchive work.
138-
* @type {Function(string): Worker}
149+
* A factory method that connects a port to the decompress implementation.
150+
* @type {Function(MessagePort): Promise<*>}
139151
* @private
140152
*/
141-
private createWorkerFn_;
153+
private connectPortFn_;
142154
/**
143155
* The path to the BitJS files.
144156
* @type {string}
@@ -150,12 +162,6 @@ export class Unarchiver extends EventTarget {
150162
* @type {boolean}
151163
*/
152164
debugMode_: boolean;
153-
/**
154-
* Private web worker initialized during start().
155-
* @private
156-
* @type {Worker}
157-
*/
158-
private worker_;
159165
/**
160166
* This method must be overridden by the subclass to return the script filename.
161167
* @returns {string} The MIME type of the archive.
@@ -169,7 +175,7 @@ export class Unarchiver extends EventTarget {
169175
*/
170176
protected getScriptFileName(): string;
171177
/**
172-
* Create an UnarchiveEvent out of the object sent back from the Worker.
178+
* Create an UnarchiveEvent out of the object sent back from the implementation.
173179
* @param {Object} obj
174180
* @returns {UnarchiveEvent}
175181
* @private
@@ -181,37 +187,47 @@ export class Unarchiver extends EventTarget {
181187
* @param {Object} obj
182188
* @private
183189
*/
184-
private handleWorkerEvent_;
190+
private handlePortEvent_;
185191
/**
186-
* Starts the unarchive in a separate Web Worker thread and returns immediately.
192+
* Starts the unarchive by connecting the ports and sending the first ArrayBuffer.
187193
*/
188194
start(): void;
189195
/**
190-
* Adds more bytes to the unarchiver's Worker thread.
196+
* Adds more bytes to the unarchiver.
191197
* @param {ArrayBuffer} ab The ArrayBuffer with more bytes in it. If opt_transferable is
192198
* set to true, this ArrayBuffer must not be referenced after calling update(), since it
193-
* is marked as Transferable and sent to the Worker.
199+
* is marked as Transferable and sent to the implementation.
194200
* @param {boolean=} opt_transferable Optional boolean whether to mark this ArrayBuffer
195201
* as a Tranferable object, which means it can no longer be referenced outside of
196-
* the Worker thread.
202+
* the implementation context.
197203
*/
198204
update(ab: ArrayBuffer, opt_transferable?: boolean | undefined): void;
199205
/**
200-
* Terminates the Web Worker for this Unarchiver and returns immediately.
206+
* Closes the port to the decompressor implementation and terminates it.
201207
*/
202208
stop(): void;
203209
}
204210
export class UnzipperInternal extends Unarchiver {
205-
constructor(arrayBuffer: any, createWorkerFn: any, options: any);
211+
constructor(arrayBuffer: any, connectPortFn: any, options: any);
206212
}
207213
export class UnrarrerInternal extends Unarchiver {
208-
constructor(arrayBuffer: any, createWorkerFn: any, options: any);
214+
constructor(arrayBuffer: any, connectPortFn: any, options: any);
209215
}
210216
export class UntarrerInternal extends Unarchiver {
211-
constructor(arrayBuffer: any, createWorkerFn: any, options: any);
217+
constructor(arrayBuffer: any, connectPortFn: any, options: any);
212218
}
213219
export type UnarchivedFile = {
214220
filename: string;
215221
fileData: Uint8Array;
216222
};
223+
export type UnarchiverOptions = {
224+
/**
225+
* The path to the bitjs folder.
226+
*/
227+
pathToBitJS: string;
228+
/**
229+
* Set to true for verbose unarchiver logging.
230+
*/
231+
debug?: boolean | undefined;
232+
};
217233
//# sourceMappingURL=decompress-internal.d.ts.map

types/archive/decompress-internal.d.ts.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.

types/archive/decompress.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/**
22
* Factory method that creates an unarchiver based on the byte signature found
3-
* in the arrayBuffer.
3+
* in the ArrayBuffer.
44
* @param {ArrayBuffer} ab The ArrayBuffer to unarchive. Note that this ArrayBuffer
5-
* must not be referenced after calling this method, as the ArrayBuffer is marked
6-
* as Transferable and sent to a Worker thread once start() is called.
7-
* @param {Object|string} options An optional object of options, or a string
8-
* representing where the path to the unarchiver script files.
5+
* must not be referenced after calling this method, as the ArrayBuffer may be
6+
* transferred to a different JS context once start() is called.
7+
* @param {UnarchiverOptions|string} options An optional object of options, or a
8+
* string representing where the path to the unarchiver script files. The latter
9+
* is now deprecated (use UnarchiverOptions).
910
* @returns {Unarchiver}
1011
*/
11-
export function getUnarchiver(ab: ArrayBuffer, options?: any | string): Unarchiver;
12+
export function getUnarchiver(ab: ArrayBuffer, options?: UnarchiverOptions | string): Unarchiver;
1213
export class Unzipper extends UnzipperInternal {
1314
constructor(ab: any, options: any);
1415
}
@@ -22,6 +23,7 @@ export type UnarchivedFile = {
2223
filename: string;
2324
fileData: Uint8Array;
2425
};
26+
export type UnarchiverOptions = import('./decompress-internal.js').UnarchiverOptions;
2527
import { Unarchiver } from "./decompress-internal.js";
2628
import { UnarchiveAppendEvent } from "./decompress-internal.js";
2729
import { UnarchiveErrorEvent } from "./decompress-internal.js";

types/archive/decompress.d.ts.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)