Skip to content

Commit

Permalink
Fix exports, streaming unzip
Browse files Browse the repository at this point in the history
  • Loading branch information
101arrowz committed Feb 13, 2021
1 parent 4ed4e68 commit 5916b65
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 0.7.0
## 0.6.3
- Patch exports of async functions
- Fix streaming unzip
## 0.6.2
- Replace Adler-32 implementation (used in Zlib compression) with one more optimized for V8
- Advice from @SheetJSDev
- Add support for extra fields, file comments in ZIP files
Expand Down
4 changes: 2 additions & 2 deletions docs/classes/asyncunzipinflate.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Asynchronous streaming DEFLATE decompression for ZIP archives

### constructor

\+ **new AsyncUnzipInflate**(`_`: string, `sz`: number): [AsyncUnzipInflate](asyncunzipinflate.md)
\+ **new AsyncUnzipInflate**(`_`: string, `sz?`: number): [AsyncUnzipInflate](asyncunzipinflate.md)

Creates a DEFLATE decompression that can be used in ZIP archives

Expand All @@ -39,7 +39,7 @@ Creates a DEFLATE decompression that can be used in ZIP archives
Name | Type |
------ | ------ |
`_` | string |
`sz` | number |
`sz?` | number |

**Returns:** [AsyncUnzipInflate](asyncunzipinflate.md)

Expand Down
4 changes: 2 additions & 2 deletions docs/classes/unzip.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The handler to call whenever a file is discovered

### push

**push**(`chunk`: Uint8Array, `final`: boolean): any
**push**(`chunk`: Uint8Array, `final?`: boolean): any

Pushes a chunk to be unzipped

Expand All @@ -58,7 +58,7 @@ Pushes a chunk to be unzipped
Name | Type | Description |
------ | ------ | ------ |
`chunk` | Uint8Array | The chunk to push |
`final` | boolean | Whether this is the last chunk |
`final?` | boolean | Whether this is the last chunk |

**Returns:** any

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fflate",
"version": "0.6.2",
"version": "0.6.3",
"description": "High performance (de)compression in an 8kB package",
"main": "./lib/index.cjs",
"module": "./esm/browser.js",
Expand Down
27 changes: 19 additions & 8 deletions scripts/rewriteBuilds.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import { readFileSync, writeFileSync, unlinkSync, renameSync } from 'fs';
import { join } from 'path';
const atClass = /\/\*\* \@class \*\//g, pure = '/*#__PURE__*/';
const extraneousExports = /exports\.(.*) = void 0;\n/;
const esModule = /exports.__esModule = true;\n/;
const libDir = join(__dirname, '..', 'lib');
const libIndex = join(libDir, 'index.js');
const lib = readFileSync(libIndex, 'utf-8').replace(atClass, pure).replace(extraneousExports, '');
const lib = readFileSync(libIndex, 'utf-8')
.replace(atClass, pure)
.replace(esModule, '')
.replace(/exports\.(.*) = void 0;\n/, '');

writeFileSync(libIndex, lib);
const esmDir = join(__dirname, '..', 'esm');
const esmIndex = join(esmDir, 'index.js'),
esmWK = join(esmDir, 'worker.js'),
esmNWK = join(esmDir, 'node-worker.js');
esmNWK = join(esmDir, 'node-worker.js'),
libWK = join(libDir, 'worker.js'),
libNWK = join(libDir, 'node-worker.js');
const esm = readFileSync(esmIndex, 'utf-8').replace(atClass, pure);
const wk = readFileSync(esmWK, 'utf-8'),
nwk = readFileSync(esmNWK, 'utf-8');
unlinkSync(esmIndex), unlinkSync(esmWK), unlinkSync(esmNWK), unlinkSync(libIndex);
renameSync(join(libDir, 'worker.js'), join(libDir, 'worker.cjs'));
renameSync(join(libDir, 'node-worker.js'), join(libDir, 'node-worker.cjs'));
writeFileSync(join(libDir, 'worker.cjs'), readFileSync(join(libDir, 'worker.js'), 'utf-8').replace(esModule, ''));
writeFileSync(join(libDir, 'node-worker.cjs'), readFileSync(join(libDir, 'node-worker.js'), 'utf-8').replace(esModule, ''));
unlinkSync(esmIndex), unlinkSync(esmWK), unlinkSync(esmNWK), unlinkSync(libIndex), unlinkSync(libWK), unlinkSync(libNWK);
unlinkSync(join(libDir, 'worker.d.ts')), unlinkSync(join(libDir, 'node-worker.d.ts'));
const workerImport = /import (.*) from '\.\/node-worker';/;
const workerRequire = /var (.*) = require\("\.\/node-worker"\);/;
const defaultExport = /export default/;
writeFileSync(join(esmDir, 'index.mjs'), esm.replace(workerImport, name => nwk.replace(defaultExport, `var ${name.slice(7, name.indexOf(' ', 8))} =`)));
writeFileSync(join(esmDir, 'browser.js'), esm.replace(workerImport, name => wk.replace(defaultExport, `var ${name.slice(7, name.indexOf(' ', 8))} =`)));
writeFileSync(join(libDir, 'node.cjs'), lib.replace(workerRequire, name => nwk.replace(defaultExport, `var ${name.slice(4, name.indexOf(' ', 5))} =`)));
writeFileSync(join(libDir, 'browser.cjs'), lib.replace(workerRequire, name => wk.replace(defaultExport, `var ${name.slice(4, name.indexOf(' ', 5))} =`)));
writeFileSync(join(libDir, 'node.cjs'), lib.replace(workerRequire, name => {
name = name.slice(4, name.indexOf(' ', 5));
return nwk.replace(defaultExport, `var ${name} = {};\n${name}["default"] =`)
}));
writeFileSync(join(libDir, 'browser.cjs'), lib.replace(workerRequire, name => {
name = name.slice(4, name.indexOf(' ', 5));
return wk.replace(defaultExport, `var ${name} = {};\n${name}["default"] =`)
}));
writeFileSync(join(libDir, 'index.cjs'), lib.replace(workerRequire, name => `var ${name.slice(4, name.indexOf(' ', 5))} = require("./node-worker.cjs");`));
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2151,11 +2151,11 @@ export function strToU8(str: string, latin1?: boolean): Uint8Array {
}
let c = str.charCodeAt(i);
if (c < 128 || latin1) w(c);
else if (c < 2048) w(192 | (c >>> 6)), w(128 | (c & 63));
else if (c < 2048) w(192 | (c >> 6)), w(128 | (c & 63));
else if (c > 55295 && c < 57344)
c = 65536 + (c & 1023 << 10) | (str.charCodeAt(++i) & 1023),
w(240 | (c >>> 18)), w(128 | ((c >>> 12) & 63)), w(128 | ((c >>> 6) & 63)), w(128 | (c & 63));
else w(224 | (c >>> 12)), w(128 | ((c >>> 6) & 63)), w(128 | (c & 63));
w(240 | (c >> 18)), w(128 | ((c >> 12) & 63)), w(128 | ((c >> 6) & 63)), w(128 | (c & 63));
else w(224 | (c >> 12)), w(128 | ((c >> 6) & 63)), w(128 | (c & 63));
}
return slc(ar, 0, ai);
}
Expand Down Expand Up @@ -2936,7 +2936,7 @@ export class AsyncUnzipInflate implements UnzipDecoder {
/**
* Creates a DEFLATE decompression that can be used in ZIP archives
*/
constructor(_: string, sz: number) {
constructor(_: string, sz?: number) {
if (sz < 320000) {
this.i = new Inflate((dat, final) => {
this.ondata(null, dat, final);
Expand Down Expand Up @@ -2983,7 +2983,7 @@ export class Unzip {
* @param chunk The chunk to push
* @param final Whether this is the last chunk
*/
push(chunk: Uint8Array, final: boolean) {
push(chunk: Uint8Array, final?: boolean) {
if (!this.onfile) throw 'no callback';
if (this.c > 0) {
const len = Math.min(this.c, chunk.length);
Expand Down Expand Up @@ -3045,10 +3045,10 @@ export class Unzip {
break;
} else if (oc) {
if (sig == 0x8074B50) {
is = i += 12 + (oc == -2 && 8), f = 2, this.c = 0;
is = i += 12 + (oc == -2 && 8), f = 3, this.c = 0;
break;
} else if (sig == 0x2014B50) {
is = i -= 4, f = 2, this.c = 0;
is = i -= 4, f = 3, this.c = 0;
break;
}
}
Expand Down

0 comments on commit 5916b65

Please sign in to comment.