diff --git a/src/pack.ts b/src/pack.ts index 04132292..cb721476 100644 --- a/src/pack.ts +++ b/src/pack.ts @@ -402,6 +402,7 @@ export class Pack noMtime: this.noMtime, mtime: this.mtime, prefix: this.prefix, + onWriteEntry: this.onWriteEntry, } } @@ -412,7 +413,6 @@ export class Pack job.path, this[ENTRYOPT](job), ) - this.onWriteEntry?.(e) return e .on('end', () => this[JOBDONE](job)) .on('error', er => this.emit('error', er)) diff --git a/src/write-entry.ts b/src/write-entry.ts index 4a7b2c65..fabead4d 100644 --- a/src/write-entry.ts +++ b/src/write-entry.ts @@ -88,7 +88,7 @@ export class WriteEntry type?: EntryTypeName | 'Unsupported' linkpath?: string stat?: Stats - /* c8 ignore start */ + onWriteEntry?: (entry: WriteEntry) => any #hadError: boolean = false @@ -109,6 +109,7 @@ export class WriteEntry this.mtime = opt.mtime this.prefix = opt.prefix ? normalizeWindowsPath(opt.prefix) : undefined + this.onWriteEntry = opt.onWriteEntry if (typeof opt.onwarn === 'function') { this.on('warn', opt.onwarn) @@ -222,6 +223,7 @@ export class WriteEntry this.noMtime = true } + this.onWriteEntry?.(this) this.header = new Header({ path: this[PREFIX](this.path), // only apply the prefix to hard links. @@ -618,6 +620,7 @@ export class WriteEntryTar ctime?: Date linkpath?: string size: number + onWriteEntry?: (entry: WriteEntry) => any warn(code: string, message: string | Error, data: WarnData = {}) { return warnMethod(this, code, message, data) @@ -634,6 +637,7 @@ export class WriteEntryTar this.strict = !!opt.strict this.noPax = !!opt.noPax this.noMtime = !!opt.noMtime + this.onWriteEntry = opt.onWriteEntry this.readEntry = readEntry const { type } = readEntry @@ -684,6 +688,7 @@ export class WriteEntryTar this.remain = readEntry.size this.blockRemain = readEntry.startBlockSize + this.onWriteEntry?.(this as unknown as WriteEntry) this.header = new Header({ path: this[PREFIX](this.path), linkpath: diff --git a/test/create.ts b/test/create.ts index 0ff86486..e0c4a0e8 100644 --- a/test/create.ts +++ b/test/create.ts @@ -1,12 +1,12 @@ -import t, { Test } from 'tap' -import { c, list, Pack, PackSync } from '../dist/esm/index.js' import fs from 'fs' +import { mkdirp } from 'mkdirp' import path from 'path' import { rimraf } from 'rimraf' -import { mkdirp } from 'mkdirp' +import t, { Test } from 'tap' +import { c, list, Pack, PackSync } from '../dist/esm/index.js' +import { spawn } from 'child_process' //@ts-ignore import mutateFS from 'mutate-fs' -import { spawn } from 'child_process' import { fileURLToPath } from 'url' const isWindows = process.platform === 'win32' @@ -288,3 +288,23 @@ t.test('must specify some files', t => { t.throws(() => c({}), 'no paths specified to add to archive') t.end() }) + +t.test('transform a filename', async t => { + const cwd = t.testdir({ + 'README.md': 'hello, world', + }) + const data = await c( + { + cwd, + onWriteEntry: entry => { + entry.path = 'bloorg.md' + }, + sync: true, + }, + ['README.md'], + ).concat() + t.equal( + data.subarray(0, 'bloorg.md'.length).toString(), + 'bloorg.md', + ) +}) diff --git a/test/write-entry.js b/test/write-entry.js index 3cc6e7fa..5e23b629 100644 --- a/test/write-entry.js +++ b/test/write-entry.js @@ -59,16 +59,23 @@ t.test('100 byte filename', t => { const runTest = t => { const f = '100-byte-filename-cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc' + let entryInOWE = undefined const ws = new WriteEntry(f, { cwd: files, linkCache: linkCache, statCache: statCache, + onWriteEntry: self => { + entryInOWE = self + t.equal(self.path, f) + t.equal(self.header, undefined) + }, }) let out = [] ws.on('data', c => out.push(c)) ws.on('end', _ => { out = Buffer.concat(out) + t.equal(entryInOWE, ws) t.match(ws, { header: { cksumValid: true, @@ -1103,11 +1110,22 @@ t.test('write entry from read entry', t => { '', ]) const fileEntry = new ReadEntry(new Header(data)) - const wetFile = new WriteEntryTar(fileEntry, { portable: true }) + let entryInOWE = undefined + const wetFile = new WriteEntryTar(fileEntry, { + portable: true, + onWriteEntry: self => { + entryInOWE = self + t.equal(self.path, '$') + t.equal(self.header, undefined) + }, + }) const out = [] let wetFileEnded = false wetFile.on('data', c => out.push(c)) - wetFile.on('end', _ => (wetFileEnded = true)) + wetFile.on('end', () => { + wetFileEnded = true + t.equal(entryInOWE, wetFile) + }) fileEntry.end() t.equal(wetFileEnded, true) const result = Buffer.concat(out)