Skip to content

Commit

Permalink
move onWriteEntry to where it can do some good
Browse files Browse the repository at this point in the history
Fix: #271
  • Loading branch information
isaacs committed Jul 26, 2024
1 parent 68a685b commit bf13718
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export class Pack
noMtime: this.noMtime,
mtime: this.mtime,
prefix: this.prefix,
onWriteEntry: this.onWriteEntry,
}
}

Expand All @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion src/write-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class WriteEntry
type?: EntryTypeName | 'Unsupported'
linkpath?: string
stat?: Stats
/* c8 ignore start */
onWriteEntry?: (entry: WriteEntry) => any

#hadError: boolean = false

Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
28 changes: 24 additions & 4 deletions test/create.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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',
)
})
22 changes: 20 additions & 2 deletions test/write-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bf13718

Please sign in to comment.