docx-plus 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,11 +21,12 @@
21
21
 
22
22
  ## What's Different from docx?
23
23
 
24
- | | docx | docx-plus |
25
- | --------------------- | ----------------------------------------- | ------------------------------------ |
26
- | ZIP handling | jszip | **fflate** (faster, smaller) |
27
- | Data conversion | Manual env detection (`Buffer.from` etc.) | **undio** (universal `toUint8Array`) |
28
- | Test environment | jsdom | happy-dom |
24
+ | | docx | docx-plus |
25
+ | ------------------- | ----------------------------------------- | ------------------------------------ |
26
+ | ZIP handling | jszip | **fflate** (faster, smaller) |
27
+ | Data conversion | Manual env detection (`Buffer.from` etc.) | **undio** (universal `toUint8Array`) |
28
+ | `Packer.toStream()` | Removed (pseudo-streaming) | **Restored** with real streaming ZIP |
29
+ | Test environment | jsdom | happy-dom |
29
30
 
30
31
  ## Installation
31
32
 
package/dist/index.cjs CHANGED
@@ -25,6 +25,7 @@ let xml_js = require("xml-js");
25
25
  let hash_js = require("hash.js");
26
26
  hash_js = __toESM(hash_js);
27
27
  let nanoid_non_secure = require("nanoid/non-secure");
28
+ let stream = require("stream");
28
29
  let fflate = require("fflate");
29
30
  let undio = require("undio");
30
31
  let xml = require("xml");
@@ -19019,6 +19020,50 @@ var Packer = class Packer {
19019
19020
  static toArrayBuffer(file, prettify, overrides = []) {
19020
19021
  return Packer.pack(file, "arraybuffer", prettify, overrides);
19021
19022
  }
19023
+ /**
19024
+ * Exports a document to a Node.js Readable stream.
19025
+ *
19026
+ * Uses fflate's streaming Zip API to emit compressed chunks incrementally,
19027
+ * avoiding buffering the entire archive in memory before the first byte
19028
+ * is available to the consumer.
19029
+ *
19030
+ * @param file - The document to export
19031
+ * @param prettify - Whether to prettify the XML output
19032
+ * @param overrides - Optional array of file overrides
19033
+ * @returns A readable stream containing the compressed .docx data
19034
+ *
19035
+ * @example
19036
+ * ```typescript
19037
+ * import { createWriteStream } from "fs";
19038
+ * Packer.toStream(doc).pipe(createWriteStream("output.docx"));
19039
+ * ```
19040
+ */
19041
+ static toStream(file, prettify, overrides = []) {
19042
+ const stream$1 = new stream.Readable({ read() {} });
19043
+ try {
19044
+ const files = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
19045
+ const zip = new fflate.Zip((err, chunk, final) => {
19046
+ if (err) {
19047
+ stream$1.destroy(err);
19048
+ return;
19049
+ }
19050
+ if (!stream$1.destroyed) stream$1.push(chunk);
19051
+ if (final) stream$1.push(null);
19052
+ });
19053
+ for (const [name, data] of Object.entries(files)) {
19054
+ var _level;
19055
+ const raw = Array.isArray(data) ? data[0] : data;
19056
+ const level = Array.isArray(data) ? (_level = data[1].level) !== null && _level !== void 0 ? _level : 6 : 6;
19057
+ const entry = level === 0 ? new fflate.ZipPassThrough(name) : new fflate.ZipDeflate(name, { level });
19058
+ zip.add(entry);
19059
+ entry.push(raw, true);
19060
+ }
19061
+ zip.end();
19062
+ } catch (err) {
19063
+ stream$1.destroy(err instanceof Error ? err : new Error(String(err)));
19064
+ }
19065
+ return stream$1;
19066
+ }
19022
19067
  };
19023
19068
  _defineProperty(Packer, "compiler", new Compiler());
19024
19069
  //#endregion
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Element } from "xml-js";
2
+ import { Readable } from "stream";
2
3
  //#region src/file/file-child.d.ts
3
4
  declare class FileChild extends XmlComponent {
4
5
  readonly fileChild: symbol;
@@ -3166,6 +3167,7 @@ declare class Packer {
3166
3167
  static toBase64String(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType], overrides?: readonly IXmlifyedFile[]): Promise<string>;
3167
3168
  static toBlob(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType], overrides?: readonly IXmlifyedFile[]): Promise<Blob>;
3168
3169
  static toArrayBuffer(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType], overrides?: readonly IXmlifyedFile[]): Promise<ArrayBuffer>;
3170
+ static toStream(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType], overrides?: readonly IXmlifyedFile[]): Readable;
3169
3171
  private static readonly compiler;
3170
3172
  }
3171
3173
  //#endregion
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Element } from "xml-js";
2
+ import { Readable } from "stream";
2
3
  //#region src/file/file-child.d.ts
3
4
  declare class FileChild extends XmlComponent {
4
5
  readonly fileChild: symbol;
@@ -3166,6 +3167,7 @@ declare class Packer {
3166
3167
  static toBase64String(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType], overrides?: readonly IXmlifyedFile[]): Promise<string>;
3167
3168
  static toBlob(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType], overrides?: readonly IXmlifyedFile[]): Promise<Blob>;
3168
3169
  static toArrayBuffer(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType], overrides?: readonly IXmlifyedFile[]): Promise<ArrayBuffer>;
3170
+ static toStream(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType], overrides?: readonly IXmlifyedFile[]): Readable;
3169
3171
  private static readonly compiler;
3170
3172
  }
3171
3173
  //#endregion
@@ -1,4 +1,4 @@
1
- var docxPlus = (function(exports, xml_js, hash_js, nanoid_non_secure, fflate, undio, xml) {
1
+ var docxPlus = (function(exports, xml_js, hash_js, nanoid_non_secure, stream, fflate, undio, xml) {
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  //#region \0rolldown/runtime.js
4
4
  var __create = Object.create;
@@ -19014,6 +19014,50 @@ var docxPlus = (function(exports, xml_js, hash_js, nanoid_non_secure, fflate, un
19014
19014
  static toArrayBuffer(file, prettify, overrides = []) {
19015
19015
  return Packer.pack(file, "arraybuffer", prettify, overrides);
19016
19016
  }
19017
+ /**
19018
+ * Exports a document to a Node.js Readable stream.
19019
+ *
19020
+ * Uses fflate's streaming Zip API to emit compressed chunks incrementally,
19021
+ * avoiding buffering the entire archive in memory before the first byte
19022
+ * is available to the consumer.
19023
+ *
19024
+ * @param file - The document to export
19025
+ * @param prettify - Whether to prettify the XML output
19026
+ * @param overrides - Optional array of file overrides
19027
+ * @returns A readable stream containing the compressed .docx data
19028
+ *
19029
+ * @example
19030
+ * ```typescript
19031
+ * import { createWriteStream } from "fs";
19032
+ * Packer.toStream(doc).pipe(createWriteStream("output.docx"));
19033
+ * ```
19034
+ */
19035
+ static toStream(file, prettify, overrides = []) {
19036
+ const stream$1 = new stream.Readable({ read() {} });
19037
+ try {
19038
+ const files = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
19039
+ const zip = new fflate.Zip((err, chunk, final) => {
19040
+ if (err) {
19041
+ stream$1.destroy(err);
19042
+ return;
19043
+ }
19044
+ if (!stream$1.destroyed) stream$1.push(chunk);
19045
+ if (final) stream$1.push(null);
19046
+ });
19047
+ for (const [name, data] of Object.entries(files)) {
19048
+ var _level;
19049
+ const raw = Array.isArray(data) ? data[0] : data;
19050
+ const level = Array.isArray(data) ? (_level = data[1].level) !== null && _level !== void 0 ? _level : 6 : 6;
19051
+ const entry = level === 0 ? new fflate.ZipPassThrough(name) : new fflate.ZipDeflate(name, { level });
19052
+ zip.add(entry);
19053
+ entry.push(raw, true);
19054
+ }
19055
+ zip.end();
19056
+ } catch (err) {
19057
+ stream$1.destroy(err instanceof Error ? err : new Error(String(err)));
19058
+ }
19059
+ return stream$1;
19060
+ }
19017
19061
  };
19018
19062
  _defineProperty(Packer, "compiler", new Compiler());
19019
19063
  //#endregion
@@ -20138,4 +20182,4 @@ var docxPlus = (function(exports, xml_js, hash_js, nanoid_non_secure, fflate, un
20138
20182
  exports.universalMeasureValue = universalMeasureValue;
20139
20183
  exports.unsignedDecimalNumber = unsignedDecimalNumber;
20140
20184
  return exports;
20141
- })({}, xml_js, hash_js, nanoid_non_secure, fflate, undio, xml);
20185
+ })({}, xml_js, hash_js, nanoid_non_secure, stream, fflate, undio, xml);
package/dist/index.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import { js2xml, xml2js } from "xml-js";
2
2
  import hash from "hash.js";
3
3
  import { customAlphabet, nanoid } from "nanoid/non-secure";
4
- import { strFromU8, unzipSync, zipSync } from "fflate";
4
+ import { Readable } from "stream";
5
+ import { Zip, ZipDeflate, ZipPassThrough, strFromU8, unzipSync, zipSync } from "fflate";
5
6
  import { textToUint8Array, toUint8Array } from "undio";
6
7
  import xml from "xml";
7
8
  //#region \0@oxc-project+runtime@0.123.0/helpers/typeof.js
@@ -18994,6 +18995,50 @@ var Packer = class Packer {
18994
18995
  static toArrayBuffer(file, prettify, overrides = []) {
18995
18996
  return Packer.pack(file, "arraybuffer", prettify, overrides);
18996
18997
  }
18998
+ /**
18999
+ * Exports a document to a Node.js Readable stream.
19000
+ *
19001
+ * Uses fflate's streaming Zip API to emit compressed chunks incrementally,
19002
+ * avoiding buffering the entire archive in memory before the first byte
19003
+ * is available to the consumer.
19004
+ *
19005
+ * @param file - The document to export
19006
+ * @param prettify - Whether to prettify the XML output
19007
+ * @param overrides - Optional array of file overrides
19008
+ * @returns A readable stream containing the compressed .docx data
19009
+ *
19010
+ * @example
19011
+ * ```typescript
19012
+ * import { createWriteStream } from "fs";
19013
+ * Packer.toStream(doc).pipe(createWriteStream("output.docx"));
19014
+ * ```
19015
+ */
19016
+ static toStream(file, prettify, overrides = []) {
19017
+ const stream = new Readable({ read() {} });
19018
+ try {
19019
+ const files = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
19020
+ const zip = new Zip((err, chunk, final) => {
19021
+ if (err) {
19022
+ stream.destroy(err);
19023
+ return;
19024
+ }
19025
+ if (!stream.destroyed) stream.push(chunk);
19026
+ if (final) stream.push(null);
19027
+ });
19028
+ for (const [name, data] of Object.entries(files)) {
19029
+ var _level;
19030
+ const raw = Array.isArray(data) ? data[0] : data;
19031
+ const level = Array.isArray(data) ? (_level = data[1].level) !== null && _level !== void 0 ? _level : 6 : 6;
19032
+ const entry = level === 0 ? new ZipPassThrough(name) : new ZipDeflate(name, { level });
19033
+ zip.add(entry);
19034
+ entry.push(raw, true);
19035
+ }
19036
+ zip.end();
19037
+ } catch (err) {
19038
+ stream.destroy(err instanceof Error ? err : new Error(String(err)));
19039
+ }
19040
+ return stream;
19041
+ }
18997
19042
  };
18998
19043
  _defineProperty(Packer, "compiler", new Compiler());
18999
19044
  //#endregion
package/dist/index.umd.js CHANGED
@@ -1,14 +1,15 @@
1
1
  (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("xml-js"), require("hash.js"), require("nanoid/non-secure"), require("fflate"), require("undio"), require("xml")) : typeof define === "function" && define.amd ? define([
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("xml-js"), require("hash.js"), require("nanoid/non-secure"), require("stream"), require("fflate"), require("undio"), require("xml")) : typeof define === "function" && define.amd ? define([
3
3
  "exports",
4
4
  "xml-js",
5
5
  "hash.js",
6
6
  "nanoid/non-secure",
7
+ "stream",
7
8
  "fflate",
8
9
  "undio",
9
10
  "xml"
10
- ], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.docxPlus = {}, global.xml_js, global.hash_js, global.nanoid_non_secure, global.fflate, global.undio, global.xml));
11
- })(this, function(exports, xml_js, hash_js, nanoid_non_secure, fflate, undio, xml) {
11
+ ], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.docxPlus = {}, global.xml_js, global.hash_js, global.nanoid_non_secure, global.stream, global.fflate, global.undio, global.xml));
12
+ })(this, function(exports, xml_js, hash_js, nanoid_non_secure, stream, fflate, undio, xml) {
12
13
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
13
14
  //#region \0rolldown/runtime.js
14
15
  var __create = Object.create;
@@ -19024,6 +19025,50 @@
19024
19025
  static toArrayBuffer(file, prettify, overrides = []) {
19025
19026
  return Packer.pack(file, "arraybuffer", prettify, overrides);
19026
19027
  }
19028
+ /**
19029
+ * Exports a document to a Node.js Readable stream.
19030
+ *
19031
+ * Uses fflate's streaming Zip API to emit compressed chunks incrementally,
19032
+ * avoiding buffering the entire archive in memory before the first byte
19033
+ * is available to the consumer.
19034
+ *
19035
+ * @param file - The document to export
19036
+ * @param prettify - Whether to prettify the XML output
19037
+ * @param overrides - Optional array of file overrides
19038
+ * @returns A readable stream containing the compressed .docx data
19039
+ *
19040
+ * @example
19041
+ * ```typescript
19042
+ * import { createWriteStream } from "fs";
19043
+ * Packer.toStream(doc).pipe(createWriteStream("output.docx"));
19044
+ * ```
19045
+ */
19046
+ static toStream(file, prettify, overrides = []) {
19047
+ const stream$1 = new stream.Readable({ read() {} });
19048
+ try {
19049
+ const files = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
19050
+ const zip = new fflate.Zip((err, chunk, final) => {
19051
+ if (err) {
19052
+ stream$1.destroy(err);
19053
+ return;
19054
+ }
19055
+ if (!stream$1.destroyed) stream$1.push(chunk);
19056
+ if (final) stream$1.push(null);
19057
+ });
19058
+ for (const [name, data] of Object.entries(files)) {
19059
+ var _level;
19060
+ const raw = Array.isArray(data) ? data[0] : data;
19061
+ const level = Array.isArray(data) ? (_level = data[1].level) !== null && _level !== void 0 ? _level : 6 : 6;
19062
+ const entry = level === 0 ? new fflate.ZipPassThrough(name) : new fflate.ZipDeflate(name, { level });
19063
+ zip.add(entry);
19064
+ entry.push(raw, true);
19065
+ }
19066
+ zip.end();
19067
+ } catch (err) {
19068
+ stream$1.destroy(err instanceof Error ? err : new Error(String(err)));
19069
+ }
19070
+ return stream$1;
19071
+ }
19027
19072
  };
19028
19073
  _defineProperty(Packer, "compiler", new Compiler());
19029
19074
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "docx-plus",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
5
5
  "keywords": [
6
6
  "clippy",