Skip to content

Commit cf6182c

Browse files
authored
feat(pack): allow overriding more metadata on tar sources (#78)
* feat(pack): allow overriding more metadata on tar sources * test(pack): support windows cases
1 parent aed639e commit cf6182c

File tree

4 files changed

+277
-39
lines changed

4 files changed

+277
-39
lines changed

REFERENCE.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -236,43 +236,49 @@ interface PackOptionsFS {
236236
map?: (header: TarHeader) => TarHeader;
237237
}
238238

239+
// Base interface for all source types
240+
interface BaseSource {
241+
/** Destination path for the entry inside the tar archive */
242+
target: string;
243+
/** Optional modification time. Overrides filesystem values or defaults to current time. */
244+
mtime?: Date;
245+
/** Optional user ID. Overrides filesystem values or defaults to 0. */
246+
uid?: number;
247+
/** Optional group ID. Overrides filesystem values or defaults to 0. */
248+
gid?: number;
249+
/** Optional user name. */
250+
uname?: string;
251+
/** Optional group name. */
252+
gname?: string;
253+
/** Optional Unix file permissions for the entry (e.g., 0o644, 0o755). */
254+
mode?: number;
255+
}
256+
239257
// Source types for packTar function
240-
interface FileSource {
258+
interface FileSource extends BaseSource {
241259
type: "file";
242260
/** Path to the source file on the local filesystem */
243261
source: string;
244-
/** Destination path inside the tar archive */
245-
target: string;
246262
}
247263

248-
interface DirectorySource {
264+
interface DirectorySource extends BaseSource {
249265
type: "directory";
250266
/** Path to the source directory on the local filesystem */
251267
source: string;
252-
/** Destination path inside the tar archive */
253-
target: string;
254268
}
255269

256-
interface ContentSource {
270+
interface ContentSource extends BaseSource {
257271
type: "content";
258272
/** Raw content to add. Supports string, Uint8Array, ArrayBuffer, ReadableStream, Blob, or null. */
259273
content: TarEntryData;
260-
/** Destination path inside the tar archive */
261-
target: string;
262-
/** Optional Unix file permissions (e.g., 0o644, 0o755) */
263-
mode?: number;
264274
}
265275

266-
interface StreamSource {
276+
interface StreamSource extends BaseSource {
267277
type: "stream";
268278
/** A Node.js Readable stream or Web ReadableStream. */
269279
content: Readable | ReadableStream;
270-
/** Destination path inside the tar archive */
271-
target: string;
272280
/** The total size of the stream's content in bytes. This is required for streams to prevent hanging. */
273281
size: number;
274-
/** Optional Unix file permissions (e.g., 0o644, 0o755) */
275-
mode?: number;
276282
}
277283

278284
type TarSource = FileSource | DirectorySource | ContentSource | StreamSource;

src/fs/pack.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ export function packTar(
248248
isDirectory: () => false,
249249
isSymbolicLink: () => false,
250250
mode: job.mode ?? 0o644,
251-
mtime: new Date(),
252-
uid: process.getuid?.() ?? 0,
253-
gid: process.getgid?.() ?? 0,
251+
mtime: job.mtime ?? new Date(),
252+
uid: job.uid ?? 0,
253+
gid: job.gid ?? 0,
254254
} as Stats;
255255

256256
if (filter && !filter(target, stat)) return;
@@ -263,6 +263,8 @@ export function packTar(
263263
mtime: stat.mtime,
264264
uid: stat.uid,
265265
gid: stat.gid,
266+
uname: job.uname,
267+
gname: job.gname,
266268
};
267269

268270
if (map) header = map(header);
@@ -297,10 +299,12 @@ export function packTar(
297299
let header: TarHeader = {
298300
name: target,
299301
size: 0,
300-
mode: Number(stat.mode),
301-
mtime: stat.mtime,
302-
uid: Number(stat.uid),
303-
gid: Number(stat.gid),
302+
mode: job.mode ?? Number(stat.mode),
303+
mtime: job.mtime ?? stat.mtime,
304+
uid: job.uid ?? Number(stat.uid),
305+
gid: job.gid ?? Number(stat.gid),
306+
uname: job.uname,
307+
gname: job.gname,
304308
type: "file", // Default type
305309
};
306310

src/fs/types.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,46 +50,52 @@ export interface UnpackOptionsFS extends UnpackOptions {
5050
concurrency?: number;
5151
}
5252

53+
/** Base interface containing common metadata properties for all source types. */
54+
export interface BaseSource {
55+
/** Destination path for the entry inside the tar archive. */
56+
target: string;
57+
/** Optional modification time. Overrides filesystem values or defaults to current time. */
58+
mtime?: Date;
59+
/** Optional user ID. Overrides filesystem values or defaults to 0. */
60+
uid?: number;
61+
/** Optional group ID. Overrides filesystem values or defaults to 0. */
62+
gid?: number;
63+
/** Optional user name. */
64+
uname?: string;
65+
/** Optional group name. */
66+
gname?: string;
67+
/** Optional Unix file permissions for the entry (e.g., 0o644, 0o755). */
68+
mode?: number;
69+
}
70+
5371
/** Describes a file on the local filesystem to be added to the archive. */
54-
export interface FileSource {
72+
export interface FileSource extends BaseSource {
5573
type: "file";
5674
/** Path to the source file on the local filesystem. */
5775
source: string;
58-
/** Destination path for the file inside the tar archive. */
59-
target: string;
6076
}
6177

6278
/** Describes a directory on the local filesystem to be added to the archive. */
63-
export interface DirectorySource {
79+
export interface DirectorySource extends BaseSource {
6480
type: "directory";
6581
/** Path to the source directory on the local filesystem. */
6682
source: string;
67-
/** Destination path for the directory inside the tar archive. */
68-
target: string;
6983
}
7084

7185
/** Describes raw, buffered content to be added to the archive. */
72-
export interface ContentSource {
86+
export interface ContentSource extends BaseSource {
7387
type: "content";
7488
/** Raw content to add. Supports string, Uint8Array, ArrayBuffer, Blob, or null. */
7589
content: TarEntryData;
76-
/** Destination path for the content inside the tar archive. */
77-
target: string;
78-
/** Optional Unix file permissions for the entry (e.g., 0o644). */
79-
mode?: number;
8090
}
8191

8292
/** Describes a stream of content to be added to the archive. */
83-
export interface StreamSource {
93+
export interface StreamSource extends BaseSource {
8494
type: "stream";
8595
/** A Readable or ReadableStream. */
8696
content: Readable | ReadableStream;
87-
/** Destination path for the content inside the tar archive. */
88-
target: string;
8997
/** The total size of the stream's content in bytes. This is required for streams. */
9098
size: number;
91-
/** Optional Unix file permissions for the entry (e.g., 0o644). */
92-
mode?: number;
9399
}
94100

95101
/** A union of all possible source types for creating a tar archive. */

0 commit comments

Comments
 (0)