Pārlūkot izejas kodu

Patch imports on Node.js

Arjun Barrett 4 gadi atpakaļ
vecāks
revīzija
8cd81460b6

+ 3 - 3
README.md

@@ -61,7 +61,7 @@ with only the features you need. This build is about 27kB, or 9kB gzipped.
 
 You may also want to specify the version, e.g. with [email protected]
 -->
-<script src="https://unpkg.com/fflate/umd/index.js"></script>
+<script src="https://unpkg.com/fflate"></script>
 <script src="https://cdn.jsdelivr.net/npm/fflate/umd/index.js"></script>
 <!-- Now, the global variable fflate contains the library -->
 
@@ -77,8 +77,8 @@ If your environment doesn't support bundling:
 
 // For the browser:
 import * as fflate from 'fflate/esm/browser.js';
-// If for some reason the standard ESM import fails on Node:
-import * as fflate from 'fflate/esm/index.mjs';
+// If the standard ESM import fails on Node (i.e. older version):
+import * as fflate from 'fflate/esm';
 ```
 
 And use:

+ 9 - 0
docs/classes/asynczipdeflate.md

@@ -23,6 +23,7 @@ Asynchronous streaming DEFLATE compression for ZIP archives
 * [crc](asynczipdeflate.md#crc)
 * [filename](asynczipdeflate.md#filename)
 * [flag](asynczipdeflate.md#flag)
+* [mtime](asynczipdeflate.md#mtime)
 * [ondata](asynczipdeflate.md#ondata)
 * [os](asynczipdeflate.md#os)
 * [size](asynczipdeflate.md#size)
@@ -92,6 +93,14 @@ ___
 
 ___
 
+### mtime
+
+• `Optional` **mtime**: GzipOptions[\"mtime\"]
+
+*Implementation of [ZipInputFile](../interfaces/zipinputfile.md).[mtime](../interfaces/zipinputfile.md#mtime)*
+
+___
+
 ### ondata
 
 •  **ondata**: [AsyncFlateStreamHandler](../README.md#asyncflatestreamhandler)

+ 1 - 1
docs/classes/zip.md

@@ -81,6 +81,6 @@ ___
 ▸ **terminate**(): void
 
 A method to terminate any internal workers used by the stream. Subsequent
-calls to add() will silently fail.
+calls to add() will fail.
 
 **Returns:** void

+ 9 - 0
docs/classes/zipdeflate.md

@@ -24,6 +24,7 @@ for better performance
 * [crc](zipdeflate.md#crc)
 * [filename](zipdeflate.md#filename)
 * [flag](zipdeflate.md#flag)
+* [mtime](zipdeflate.md#mtime)
 * [ondata](zipdeflate.md#ondata)
 * [os](zipdeflate.md#os)
 * [size](zipdeflate.md#size)
@@ -92,6 +93,14 @@ ___
 
 ___
 
+### mtime
+
+• `Optional` **mtime**: GzipOptions[\"mtime\"]
+
+*Implementation of [ZipInputFile](../interfaces/zipinputfile.md).[mtime](../interfaces/zipinputfile.md#mtime)*
+
+___
+
 ### ondata
 
 •  **ondata**: [AsyncFlateStreamHandler](../README.md#asyncflatestreamhandler)

+ 9 - 0
docs/classes/zippassthrough.md

@@ -22,6 +22,7 @@ A pass-through stream to keep data uncompressed in a ZIP archive.
 * [compression](zippassthrough.md#compression)
 * [crc](zippassthrough.md#crc)
 * [filename](zippassthrough.md#filename)
+* [mtime](zippassthrough.md#mtime)
 * [ondata](zippassthrough.md#ondata)
 * [os](zippassthrough.md#os)
 * [size](zippassthrough.md#size)
@@ -80,6 +81,14 @@ ___
 
 ___
 
+### mtime
+
+• `Optional` **mtime**: GzipOptions[\"mtime\"]
+
+*Implementation of [ZipInputFile](../interfaces/zipinputfile.md).[mtime](../interfaces/zipinputfile.md#mtime)*
+
+___
+
 ### ondata
 
 •  **ondata**: [AsyncFlateStreamHandler](../README.md#asyncflatestreamhandler)

+ 11 - 9
package.json

@@ -1,29 +1,31 @@
 {
   "name": "fflate",
-  "version": "0.6.0",
+  "version": "0.6.1",
   "description": "High performance (de)compression in an 8kB package",
-  "main": "./lib/index.js",
+  "main": "./lib/index.cjs",
   "module": "./esm/browser.js",
   "types": "./lib/index.d.ts",
   "unpkg": "./umd/index.js",
   "jsdelivr": "./umd/index.js",
   "browser": {
-    "./lib/node-worker.js": "./lib/worker.js",
-    "./esm/index.mjs": "./esm/browser.js"
+    "./lib/node-worker.cjs": "./lib/worker.cjs"
   },
   "exports": {
     ".": {
-      "node": "./esm/index.mjs",
-      "require": "./lib/index.js",
-      "default": "./esm/browser.js"
+      "node": {
+        "import": "./esm/index.mjs",
+        "require": "./lib/node.cjs"
+      },
+      "import": "./esm/browser.js",
+      "require": "./lib/index.cjs"
     },
     "./node": {
       "import": "./esm/index.mjs",
-      "require": "./lib/node.js"
+      "require": "./lib/node.cjs"
     },
     "./browser": {
       "import": "./esm/browser.js",
-      "require": "./lib/browser.js"
+      "require": "./lib/browser.cjs"
     }
   },
   "targets": {

+ 4 - 4
scripts/buildUMD.ts

@@ -4,9 +4,9 @@ import { join } from 'path';
 
 const p = (...fns: string[]) => join(__dirname, '..', ...fns);
 
-const src = readFileSync(p('lib', 'index.js'), 'utf8');
-const worker = readFileSync(p('lib', 'worker.js'), 'utf8');
-const nodeWorker = readFileSync(p('lib', 'node-worker.js'), 'utf8');
+const src = readFileSync(p('lib', 'index.cjs'), 'utf8');
+const worker = readFileSync(p('lib', 'worker.cjs'), 'utf8');
+const nodeWorker = readFileSync(p('lib', 'node-worker.cjs'), 'utf8');
 
 const opts: MinifyOptions = {
   mangle: {
@@ -30,7 +30,7 @@ minify(src, opts).then(async out => {
     ''
   );
   const res = "!function(f){typeof module!='undefined'&&typeof exports=='object'?module.exports=f():typeof define!='undefined'&&define.amd?define(['fflate',f]):(typeof self!='undefined'?self:this).fflate=f()}(function(){var _e={};" +
-    out.code!.replace(/exports\.(.*) = void 0;\n/, '').replace(/exports\./g, '_e.').replace(/require\("\.\/node-worker"\)/,
+    out.code!.replace(/exports\.(.*) = void 0;\n/, '').replace(/exports\./g, '_e.').replace(/require\("\.\/node-worker\.cjs"\)/,
     "(typeof module!='undefined'&&typeof exports=='object'?function(_f){" + nodeWkrOut + 'return _f}:function(_f){' + wkrOut + 'return _f})({})'
   ) + 'return _e})';
   if (!existsSync(p('umd'))) mkdirSync(p('umd'));

+ 7 - 4
scripts/rewriteBuilds.ts

@@ -1,4 +1,4 @@
-import { readFileSync, writeFileSync, unlinkSync } from 'fs';
+import { readFileSync, writeFileSync, unlinkSync, renameSync } from 'fs';
 import { join } from 'path';
 const atClass = /\/\*\* \@class \*\//g, pure = '/*#__PURE__*/';
 const extraneousExports = /exports\.(.*) = void 0;\n/;
@@ -14,12 +14,15 @@ const esmIndex = join(esmDir, 'index.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(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'));
 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.js'), lib.replace(workerRequire, name => nwk.replace(defaultExport, `var ${name.slice(4, name.indexOf(' ', 5))} =`)));
-writeFileSync(join(libDir, 'browser.js'), lib.replace(workerRequire, name => wk.replace(defaultExport, `var ${name.slice(4, name.indexOf(' ', 5))} =`)));
+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, 'index.cjs'), lib.replace(workerRequire, name => `var ${name.slice(4, name.indexOf(' ', 5))} = require("./node-worker.cjs");`));

+ 5 - 2
src/index.ts

@@ -2305,9 +2305,10 @@ export class ZipPassThrough implements ZipInputFile {
   filename: string;
   crc: number;
   size: number;
+  compression: number;
   os?: number;
   attrs?: number;
-  compression: number;
+  mtime?: GzipOptions['mtime'];
   ondata: AsyncFlateStreamHandler;
   private c: CRCV;
 
@@ -2364,6 +2365,7 @@ export class ZipDeflate implements ZipInputFile {
   flag: 0 | 1 | 2 | 3;
   os?: number;
   attrs?: number;
+  mtime?: GzipOptions['mtime'];
   ondata: AsyncFlateStreamHandler;
   private d: Deflate;
 
@@ -2410,6 +2412,7 @@ export class AsyncZipDeflate implements ZipInputFile {
   flag: 0 | 1 | 2 | 3;
   os?: number;
   attrs?: number;
+  mtime?: GzipOptions['mtime'];
   ondata: AsyncFlateStreamHandler;
   private d: AsyncDeflate;
   terminate: AsyncTerminable;
@@ -2576,7 +2579,7 @@ export class Zip {
 
   /**
    * A method to terminate any internal workers used by the stream. Subsequent
-   * calls to add() will silently fail.
+   * calls to add() will fail.
    */
   terminate() {
     for (const f of this.u) f.t();