Skip to content

Commit bf13718

Browse files
committed
move onWriteEntry to where it can do some good
Fix: #271
1 parent 68a685b commit bf13718

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

src/pack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ export class Pack
402402
noMtime: this.noMtime,
403403
mtime: this.mtime,
404404
prefix: this.prefix,
405+
onWriteEntry: this.onWriteEntry,
405406
}
406407
}
407408

@@ -412,7 +413,6 @@ export class Pack
412413
job.path,
413414
this[ENTRYOPT](job),
414415
)
415-
this.onWriteEntry?.(e)
416416
return e
417417
.on('end', () => this[JOBDONE](job))
418418
.on('error', er => this.emit('error', er))

src/write-entry.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class WriteEntry
8888
type?: EntryTypeName | 'Unsupported'
8989
linkpath?: string
9090
stat?: Stats
91-
/* c8 ignore start */
91+
onWriteEntry?: (entry: WriteEntry) => any
9292

9393
#hadError: boolean = false
9494

@@ -109,6 +109,7 @@ export class WriteEntry
109109
this.mtime = opt.mtime
110110
this.prefix =
111111
opt.prefix ? normalizeWindowsPath(opt.prefix) : undefined
112+
this.onWriteEntry = opt.onWriteEntry
112113

113114
if (typeof opt.onwarn === 'function') {
114115
this.on('warn', opt.onwarn)
@@ -222,6 +223,7 @@ export class WriteEntry
222223
this.noMtime = true
223224
}
224225

226+
this.onWriteEntry?.(this)
225227
this.header = new Header({
226228
path: this[PREFIX](this.path),
227229
// only apply the prefix to hard links.
@@ -618,6 +620,7 @@ export class WriteEntryTar
618620
ctime?: Date
619621
linkpath?: string
620622
size: number
623+
onWriteEntry?: (entry: WriteEntry) => any
621624

622625
warn(code: string, message: string | Error, data: WarnData = {}) {
623626
return warnMethod(this, code, message, data)
@@ -634,6 +637,7 @@ export class WriteEntryTar
634637
this.strict = !!opt.strict
635638
this.noPax = !!opt.noPax
636639
this.noMtime = !!opt.noMtime
640+
this.onWriteEntry = opt.onWriteEntry
637641

638642
this.readEntry = readEntry
639643
const { type } = readEntry
@@ -684,6 +688,7 @@ export class WriteEntryTar
684688
this.remain = readEntry.size
685689
this.blockRemain = readEntry.startBlockSize
686690

691+
this.onWriteEntry?.(this as unknown as WriteEntry)
687692
this.header = new Header({
688693
path: this[PREFIX](this.path),
689694
linkpath:

test/create.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import t, { Test } from 'tap'
2-
import { c, list, Pack, PackSync } from '../dist/esm/index.js'
31
import fs from 'fs'
2+
import { mkdirp } from 'mkdirp'
43
import path from 'path'
54
import { rimraf } from 'rimraf'
6-
import { mkdirp } from 'mkdirp'
5+
import t, { Test } from 'tap'
6+
import { c, list, Pack, PackSync } from '../dist/esm/index.js'
7+
import { spawn } from 'child_process'
78
//@ts-ignore
89
import mutateFS from 'mutate-fs'
9-
import { spawn } from 'child_process'
1010
import { fileURLToPath } from 'url'
1111

1212
const isWindows = process.platform === 'win32'
@@ -288,3 +288,23 @@ t.test('must specify some files', t => {
288288
t.throws(() => c({}), 'no paths specified to add to archive')
289289
t.end()
290290
})
291+
292+
t.test('transform a filename', async t => {
293+
const cwd = t.testdir({
294+
'README.md': 'hello, world',
295+
})
296+
const data = await c(
297+
{
298+
cwd,
299+
onWriteEntry: entry => {
300+
entry.path = 'bloorg.md'
301+
},
302+
sync: true,
303+
},
304+
['README.md'],
305+
).concat()
306+
t.equal(
307+
data.subarray(0, 'bloorg.md'.length).toString(),
308+
'bloorg.md',
309+
)
310+
})

test/write-entry.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,23 @@ t.test('100 byte filename', t => {
5959
const runTest = t => {
6060
const f =
6161
'100-byte-filename-cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'
62+
let entryInOWE = undefined
6263
const ws = new WriteEntry(f, {
6364
cwd: files,
6465
linkCache: linkCache,
6566
statCache: statCache,
67+
onWriteEntry: self => {
68+
entryInOWE = self
69+
t.equal(self.path, f)
70+
t.equal(self.header, undefined)
71+
},
6672
})
6773

6874
let out = []
6975
ws.on('data', c => out.push(c))
7076
ws.on('end', _ => {
7177
out = Buffer.concat(out)
78+
t.equal(entryInOWE, ws)
7279
t.match(ws, {
7380
header: {
7481
cksumValid: true,
@@ -1103,11 +1110,22 @@ t.test('write entry from read entry', t => {
11031110
'',
11041111
])
11051112
const fileEntry = new ReadEntry(new Header(data))
1106-
const wetFile = new WriteEntryTar(fileEntry, { portable: true })
1113+
let entryInOWE = undefined
1114+
const wetFile = new WriteEntryTar(fileEntry, {
1115+
portable: true,
1116+
onWriteEntry: self => {
1117+
entryInOWE = self
1118+
t.equal(self.path, '$')
1119+
t.equal(self.header, undefined)
1120+
},
1121+
})
11071122
const out = []
11081123
let wetFileEnded = false
11091124
wetFile.on('data', c => out.push(c))
1110-
wetFile.on('end', _ => (wetFileEnded = true))
1125+
wetFile.on('end', () => {
1126+
wetFileEnded = true
1127+
t.equal(entryInOWE, wetFile)
1128+
})
11111129
fileEntry.end()
11121130
t.equal(wetFileEnded, true)
11131131
const result = Buffer.concat(out)

0 commit comments

Comments
 (0)