Skip to content

Commit

Permalink
add onWriteEntry method
Browse files Browse the repository at this point in the history
fix: #316
  • Loading branch information
isaacs committed Jun 19, 2024
1 parent ee87da2 commit 61b9789
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type GzipOptions, type ZlibOptions } from 'minizlib'
import { type Stats } from 'node:fs'
import { type ReadEntry } from './read-entry.js'
import { type WarnData } from './warn-method.js'
import { WriteEntry } from './write-entry.js'

const argmap = new Map<keyof TarOptionsWithAliases, keyof TarOptions>(
[
Expand Down Expand Up @@ -275,6 +276,16 @@ export interface TarOptions {
*/
noResume?: boolean

/**
* When creating, updating, or replacing within archives, this method will
* be called with each WriteEntry that is created.
*
* It's not called 'onentry' because that's already taken for the ReadEntry
* when reading archives, and it's just easier to only have one type of
* options object that this whole library can pass around without issue.
*/
onWriteEntry?: (entry: WriteEntry) => any

/**
* When extracting or listing archives, this method will be called with
* each entry that is not excluded by a `filter`.
Expand Down
9 changes: 6 additions & 3 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class Pack
jobs: number;

[WRITEENTRYCLASS]: typeof WriteEntry | typeof WriteEntrySync;
onWriteEntry?: (entry: WriteEntry) => void
[QUEUE]: Yallist<PackJob>;
[JOBS]: number = 0;
[PROCESSING]: boolean = false;
Expand All @@ -110,6 +111,7 @@ export class Pack
this.linkCache = opt.linkCache || new Map()
this.statCache = opt.statCache || new Map()
this.readdirCache = opt.readdirCache || new Map()
this.onWriteEntry = opt.onWriteEntry

this[WRITEENTRYCLASS] = WriteEntry
if (typeof opt.onwarn === 'function') {
Expand Down Expand Up @@ -154,7 +156,7 @@ export class Pack
if (opt.mtime) this.mtime = opt.mtime

this.filter =
typeof opt.filter === 'function' ? opt.filter : _ => true
typeof opt.filter === 'function' ? opt.filter : () => true

this[QUEUE] = new Yallist<PackJob>()
this[JOBS] = 0
Expand Down Expand Up @@ -385,8 +387,9 @@ export class Pack
[ENTRY](job: PackJob) {
this[JOBS] += 1
try {
return new this[WRITEENTRYCLASS](job.path, this[ENTRYOPT](job))
.on('end', () => this[JOBDONE](job))
const e = new this[WRITEENTRYCLASS](job.path, this[ENTRYOPT](job))
this.onWriteEntry?.(e)
return e.on('end', () => this[JOBDONE](job))
.on('error', er => this.emit('error', er))
} catch (er) {
this.emit('error', er)
Expand Down
4 changes: 3 additions & 1 deletion test/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ t.test('set up', t => {

t.test('pack a file', t => {
const out = []
new Pack({ cwd: files })
const seen = []
new Pack({ cwd: files, onWriteEntry: e => seen.push(e) })
.end('one-byte.txt')
.on('data', c => out.push(c))
.on('end', _ => {
Expand Down Expand Up @@ -98,6 +99,7 @@ t.test('pack a file', t => {
)
const hs = new Header(sync)
t.match(hs, expect)
t.strictSame(seen.map(e => e.path), ['one-byte.txt'])
t.end()
})
})
Expand Down

0 comments on commit 61b9789

Please sign in to comment.