modern-tar 0.5.5 → 0.6.0

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/dist/fs/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createTarPacker, createTarUnpacker, normalizeBody, transformHeader } from "../unpacker-BKKRRs7i.js";
1
+ import { createTarPacker, createTarUnpacker, normalizeBody, transformHeader } from "../unpacker-CrWmqt8E.js";
2
2
  import * as fs from "node:fs/promises";
3
3
  import { cpus } from "node:os";
4
4
  import * as path from "node:path";
@@ -130,6 +130,7 @@ async function normalizeBody(body) {
130
130
  if (body instanceof Blob) return new Uint8Array(await body.arrayBuffer());
131
131
  throw new TypeError("Unsupported content type for entry body.");
132
132
  }
133
+ const isBodyless = (header) => header.type === "directory" || header.type === "symlink" || header.type === "link";
133
134
 
134
135
  //#endregion
135
136
  //#region src/tar/checksum.ts
@@ -685,4 +686,4 @@ function isZeroBlock(block) {
685
686
  }
686
687
 
687
688
  //#endregion
688
- export { createTarPacker, createTarUnpacker, normalizeBody, streamToBuffer, transformHeader };
689
+ export { createTarPacker, createTarUnpacker, isBodyless, normalizeBody, streamToBuffer, transformHeader };
@@ -92,10 +92,13 @@ interface ParsedTarEntry {
92
92
  }
93
93
  /**
94
94
  * Represents an extracted entry with fully buffered content.
95
+ *
96
+ * For bodyless entries (directories, symlinks, hardlinks), `data` will be `undefined`.
97
+ * For files (including empty files), `data` will be a `Uint8Array`.
95
98
  */
96
99
  interface ParsedTarEntryWithData {
97
100
  header: TarHeader;
98
- data: Uint8Array;
101
+ data?: Uint8Array;
99
102
  }
100
103
  //#endregion
101
104
  //#region src/web/helpers.d.ts
@@ -153,11 +156,12 @@ declare function packTar(entries: TarEntry[]): Promise<Uint8Array>;
153
156
  *
154
157
  * const entries = await unpackTar(tarBuffer);
155
158
  * for (const entry of entries) {
156
- * console.log(`File: ${entry.header.name}, Size: ${entry.data.length} bytes`);
157
- *
158
- * if (entry.header.type === 'file') {
159
+ * if (entry.data) {
160
+ * console.log(`File: ${entry.header.name}, Size: ${entry.data.length} bytes`);
159
161
  * const content = new TextDecoder().decode(entry.data);
160
162
  * console.log(`Content: ${content}`);
163
+ * } else {
164
+ * console.log(`${entry.header.type}: ${entry.header.name}`);
161
165
  * }
162
166
  * }
163
167
  * ```
@@ -173,7 +177,9 @@ declare function packTar(entries: TarEntry[]): Promise<Uint8Array>;
173
177
  *
174
178
  * // Process filtered files
175
179
  * for (const file of entries) {
176
- * console.log(new TextDecoder().decode(file.data));
180
+ * if (file.data) {
181
+ * console.log(new TextDecoder().decode(file.data));
182
+ * }
177
183
  * }
178
184
  * ```
179
185
  */
package/dist/web/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createTarPacker as createTarPacker$1, createTarUnpacker, normalizeBody, streamToBuffer, transformHeader } from "../unpacker-BKKRRs7i.js";
1
+ import { createTarPacker as createTarPacker$1, createTarUnpacker, isBodyless, normalizeBody, streamToBuffer, transformHeader } from "../unpacker-CrWmqt8E.js";
2
2
 
3
3
  //#region src/web/compression.ts
4
4
  function createGzipEncoder() {
@@ -20,17 +20,17 @@ function createTarPacker() {
20
20
  } }),
21
21
  controller: {
22
22
  add(header) {
23
- const isBodyless = header.type === "directory" || header.type === "symlink" || header.type === "link";
23
+ const bodyless = isBodyless(header);
24
24
  const h = { ...header };
25
- if (isBodyless) h.size = 0;
25
+ if (bodyless) h.size = 0;
26
26
  packer.add(h);
27
- if (isBodyless) packer.endEntry();
27
+ if (bodyless) packer.endEntry();
28
28
  return new WritableStream({
29
29
  write(chunk) {
30
30
  packer.write(chunk);
31
31
  },
32
32
  close() {
33
- if (!isBodyless) packer.endEntry();
33
+ if (!bodyless) packer.endEntry();
34
34
  },
35
35
  abort(reason) {
36
36
  streamController.error(reason);
@@ -118,9 +118,11 @@ async function packTar(entries) {
118
118
  else if (body instanceof Blob) await body.stream().pipeTo(entryStream);
119
119
  else try {
120
120
  const chunk = await normalizeBody(body);
121
- const writer = entryStream.getWriter();
122
- await writer.write(chunk);
123
- await writer.close();
121
+ if (chunk.length > 0) {
122
+ const writer = entryStream.getWriter();
123
+ await writer.write(chunk);
124
+ await writer.close();
125
+ } else await entryStream.close();
124
126
  } catch {
125
127
  throw new TypeError(`Unsupported content type for entry "${entry.header.name}".`);
126
128
  }
@@ -154,7 +156,13 @@ async function unpackTar(archive, options = {}) {
154
156
  await entry.body.cancel();
155
157
  continue;
156
158
  }
157
- results.push({
159
+ if (isBodyless(processedHeader)) {
160
+ await entry.body.cancel();
161
+ results.push({
162
+ header: processedHeader,
163
+ data: void 0
164
+ });
165
+ } else results.push({
158
166
  header: processedHeader,
159
167
  data: await streamToBuffer(entry.body)
160
168
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modern-tar",
3
- "version": "0.5.5",
3
+ "version": "0.6.0",
4
4
  "description": "Zero dependency streaming tar parser and writer for JavaScript.",
5
5
  "author": "Ayuhito <hello@ayuhito.com>",
6
6
  "license": "MIT",