Skip to content

Commit

Permalink
some cleanup/formatting around onWriteEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 22, 2024
1 parent cc7ce8e commit e434c7b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ export class Pack
filter: Exclude<TarOptions['filter'], undefined>
jobs: number;

[WRITEENTRYCLASS]: typeof WriteEntry | typeof WriteEntrySync;
onWriteEntry?: (entry: WriteEntry) => void
[WRITEENTRYCLASS]: typeof WriteEntry | typeof WriteEntrySync
onWriteEntry?: (entry: WriteEntry) => void;
[QUEUE]: Yallist<PackJob>;
[JOBS]: number = 0;
[PROCESSING]: boolean = false;
Expand Down Expand Up @@ -387,9 +387,13 @@ export class Pack
[ENTRY](job: PackJob) {
this[JOBS] += 1
try {
const e = new this[WRITEENTRYCLASS](job.path, this[ENTRYOPT](job))
const e = new this[WRITEENTRYCLASS](
job.path,
this[ENTRYOPT](job),
)
this.onWriteEntry?.(e)
return e.on('end', () => this[JOBDONE](job))
return e
.on('end', () => this[JOBDONE](job))
.on('error', er => this.emit('error', er))
} catch (er) {
this.emit('error', er)
Expand Down
18 changes: 12 additions & 6 deletions test/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ t.test('basic', t => {
t.test('file maxReadSize=' + maxReadSize, t => {
t.test('sync', t => {
const actual: string[] = []
const onReadEntry = (entry: ReadEntry) => actual.push(entry.path)
const onReadEntry = (entry: ReadEntry) =>
actual.push(entry.path)
list({
file: file,
sync: true,
Expand All @@ -46,7 +47,8 @@ t.test('basic', t => {

t.test('async promise', async t => {
const actual: string[] = []
const onReadEntry = (entry: ReadEntry) => actual.push(entry.path)
const onReadEntry = (entry: ReadEntry) =>
actual.push(entry.path)
return await list({
file,
onReadEntry,
Expand All @@ -56,7 +58,8 @@ t.test('basic', t => {

t.test('async cb', t => {
const actual: string[] = []
const onReadEntry = (entry: ReadEntry) => actual.push(entry.path)
const onReadEntry = (entry: ReadEntry) =>
actual.push(entry.path)
list(
{
file: file,
Expand All @@ -79,15 +82,17 @@ t.test('basic', t => {
t.test('stream', t => {
t.test('sync', t => {
const actual: string[] = []
const onReadEntry = (entry: ReadEntry) => actual.push(entry.path)
const onReadEntry = (entry: ReadEntry) =>
actual.push(entry.path)
const l = list({ sync: true, onReadEntry })
l.end(fs.readFileSync(file))
return check(actual, t)
})

t.test('async', t => {
const actual: string[] = []
const onReadEntry = (entry: ReadEntry) => actual.push(entry.path)
const onReadEntry = (entry: ReadEntry) =>
actual.push(entry.path)
const l = list()
l.on('entry', onReadEntry)
l.on('end', _ => check(actual, t).then(_ => t.end()))
Expand Down Expand Up @@ -130,7 +135,8 @@ t.test('basic', t => {
t.test('no filter function, stream', t => {
const check = () => t.same(actual, expect)
const actual: string[] = []
const onReadEntry = (entry: ReadEntry) => actual.push(entry.path)
const onReadEntry = (entry: ReadEntry) =>
actual.push(entry.path)
fs.createReadStream(file).pipe(
list(fileList)
.on('entry', onReadEntry)
Expand Down
5 changes: 4 additions & 1 deletion test/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ 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.strictSame(
seen.map(e => e.path),
['one-byte.txt'],
)
t.end()
})
})
Expand Down

0 comments on commit e434c7b

Please sign in to comment.