Skip to content

Commit

Permalink
finish typescript/esm port
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Apr 10, 2024
1 parent 114c7ac commit 9522a69
Show file tree
Hide file tree
Showing 175 changed files with 4,341 additions and 7,377 deletions.
15 changes: 9 additions & 6 deletions map.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const { basename } = require('path')
import { basename } from 'path'

const map = test =>
test === 'index.js' || test === 'map.js' ? test
: test === 'unpack.js' ? ['lib/unpack.js', 'lib/mkdir.js']
: test === 'load-all.js' ? []
: `lib/${test}`
test === 'map.js'
? test
: test === 'unpack.js'
? ['src/unpack.ts', 'src/mkdir.ts']
: test === 'load-all.js'
? []
: `src/${test.replace(/js$/, 'ts')}`

module.exports = test => map(basename(test))
export default test => map(basename(test))
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
"chownr": "^3.0.0",
"minipass": "^5.0.0",
"minizlib": "^3.0.1",
"mkdirp": "^3.0.1"
"mkdirp": "^3.0.1",
"yallist": "^5.0.0"
},
"devDependencies": {
"chmodr": "^1.2.0",
"end-of-stream": "^1.4.3",
"events-to-array": "^2.0.3",
"mutate-fs": "^2.1.1",
"nock": "^13.2.9",
"nock": "^13.5.4",
"prettier": "^3.2.5",
"rimraf": "^3.0.2",
"tap": "^16.0.1",
"rimraf": "^5.0.5",
"tap": "^18.7.2",
"tshy": "^1.13.1",
"typedoc": "^0.25.13"
},
Expand Down
21 changes: 12 additions & 9 deletions scripts/generate-parse-fixtures.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict'
const Parse = require('../lib/parse.js')
const fs = require('fs')
const path = require('path')
const tardir = path.resolve(__dirname, '../test/fixtures/tars')
const parsedir = path.resolve(__dirname, '../test/fixtures/parse')
import { Parser } from '../dist/esm/parse.js'
import fs from 'fs'
import path, {dirname, resolve} from 'path'
import {fileURLToPath} from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

const tardir = resolve(__dirname, '../test/fixtures/tars')
const parsedir = resolve(__dirname, '../test/fixtures/parse')
const maxMetaOpt = [250, null]
const filterOpt = [true, false]
const strictOpt = [true, false]
Expand All @@ -16,9 +19,9 @@ const makeTest = (tarfile, tardata, maxMeta, filter, strict) => {
const tail = (o ? '-' + o : '') + '.json'
const eventsfile = parsedir + '/' + path.basename(tarfile, '.tar') + tail

const p = new Parse({
const p = new Parser({
maxMetaEntrySize: maxMeta,
filter: filter ? (path, entry) => entry.size % 2 !== 0 : null,
filter: filter ? (_path, entry) => entry.size % 2 !== 0 : null,
strict: strict,
})
const events = []
Expand Down Expand Up @@ -70,7 +73,7 @@ const makeTest = (tarfile, tardata, maxMeta, filter, strict) => {

p.on('entry', pushEntry('entry'))
p.on('ignoredEntry', pushEntry('ignoredEntry'))
p.on('warn', (code, message, data) => events.push(['warn', code, message]))
p.on('warn', (code, message, _data) => events.push(['warn', code, message]))
p.on('error', er => events.push(['error', {
message: er.message,
code: er.code,
Expand Down
11 changes: 6 additions & 5 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,17 @@ const addFilesSync = (p: PackSync, files: string[]) => {
const addFilesAsync = async (
p: Pack,
files: string[],
i = 0,
): Promise<void> => {
for (; i < files.length; i++) {
for (let i = 0; i < files.length; i++) {
const file = String(files[i])
if (file.charAt(0) === '@') {
return list({
await list({
file: path.resolve(String(p.cwd), file.slice(1)),
noResume: true,
onentry: entry => p.add(entry),
}).then(_ => addFilesAsync(p, files))
onentry: entry => {
p.add(entry)
},
})
} else {
p.add(file)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cwd-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class CwdError extends Error {
syscall: 'chdir' = 'chdir'

constructor(path: string, code: string) {
super(code + ": Cannot cd into '" + path + "'")
super(`${code}: Cannot cd into '${path}'`)
this.path = path
this.code = code
}
Expand Down
2 changes: 1 addition & 1 deletion src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const extractFileSync = (opt: TarOptionsSyncFile) => {

const extractFile = (
opt: TarOptionsFile,
cb: () => void = () => {},
cb?: () => void,
) => {
const u = new Unpack(opt)
const readSize = opt.maxReadSize || 16 * 1024 * 1024
Expand Down
22 changes: 10 additions & 12 deletions src/get-write-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
// library is used for is extracting tarballs of many
// relatively small files in npm packages and the like,
// it can be a big boost on Windows platforms.
// Only supported in Node v12.9.0 and above.

import fs from 'fs'

const platform = process.env.__FAKE_PLATFORM__ || process.platform
const isWindows = platform === 'win32'
const g = globalThis as typeof globalThis & {
__FAKE_TESTING_FS__: typeof import('fs')
}
const fs = g.__FAKE_TESTING_FS__ || require('fs')

/* istanbul ignore next */
const {
O_CREAT,
O_TRUNC,
O_WRONLY,
UV_FS_O_FILEMAP = 0,
} = fs.constants
/* c8 ignore start */
const { O_CREAT, O_TRUNC, O_WRONLY } = fs.constants
const UV_FS_O_FILEMAP =
Number(process.env.__FAKE_FS_O_FILENAME__) ||
fs.constants.UV_FS_O_FILEMAP ||
0
/* c8 ignore stop */

const fMapEnabled = isWindows && !!UV_FS_O_FILEMAP
const fMapLimit = 512 * 1024
Expand Down
30 changes: 19 additions & 11 deletions src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type HeaderData = {
gid?: number
size?: number
cksum?: number
type?: EntryTypeCode | EntryTypeName
type?: EntryTypeName | 'Unsupported'
linkpath?: string
uname?: string
gname?: string
Expand Down Expand Up @@ -46,7 +46,7 @@ export class Header implements HeaderData {
gid?: number
size?: number
cksum?: number
#type: EntryTypeCode = '0'
#type: EntryTypeCode | 'Unsupported' = 'Unsupported'
linkpath?: string
uname?: string
gname?: string
Expand Down Expand Up @@ -101,10 +101,8 @@ export class Header implements HeaderData {

// old tar versions marked dirs as a file with a trailing /
const t = decString(buf, off + 156, 1)
if (types.isCode(t)) this.#type = t
else this.#type = '0'
if (this.#type === '') {
this.#type = '0'
if (types.isCode(t)) {
this.#type = t || '0'
}
if (this.#type === '0' && this.path.slice(-1) === '/') {
this.#type = '5'
Expand All @@ -126,8 +124,10 @@ export class Header implements HeaderData {
) {
this.uname = decString(buf, off + 265, 32)
this.gname = decString(buf, off + 297, 32)
/* c8 ignore start */
this.devmaj = decNumber(buf, off + 329, 8) ?? 0
this.devmin = decNumber(buf, off + 337, 8) ?? 0
/* c8 ignore stop */
if (buf[off + 475] !== 0) {
// definitely a prefix, definitely >130 chars.
const prefix = decString(buf, off + 345, 155)
Expand All @@ -152,7 +152,7 @@ export class Header implements HeaderData {
}

this.cksumValid = sum === this.cksum
if (this.cksum === null && sum === 8 * 0x20) {
if (this.cksum === undefined && sum === 8 * 0x20) {
this.nullBlock = true
}
}
Expand Down Expand Up @@ -180,6 +180,10 @@ export class Header implements HeaderData {
buf = this.block = Buffer.alloc(512)
}

if (this.#type === 'Unsupported') {
this.#type = '0'
}

if (!(buf.length >= off + 512)) {
throw new Error('need 512 bytes for header')
}
Expand Down Expand Up @@ -244,16 +248,20 @@ export class Header implements HeaderData {
}

get type(): EntryTypeName {
return types.name.get(this.#type) as EntryTypeName
return (
this.#type === 'Unsupported'
? this.#type
: types.name.get(this.#type)
) as EntryTypeName
}

get typeKey(): EntryTypeCode {
get typeKey(): EntryTypeCode | 'Unsupported' {
return this.#type
}

set type(type: EntryTypeCode | EntryTypeName) {
set type(type: EntryTypeCode | EntryTypeName | 'Unsupported') {
const c = String(types.code.get(type as EntryTypeName))
if (types.isCode(c)) {
if (types.isCode(c) || c === 'Unsupported') {
this.#type = c
} else if (types.isCode(type)) {
this.#type = type
Expand Down
6 changes: 4 additions & 2 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isSyncFile,
TarOptions,
TarOptionsFile,
TarOptionsSyncFile,
TarOptionsWithAliases,
TarOptionsWithAliasesFile,
TarOptionsWithAliasesSync,
Expand Down Expand Up @@ -126,7 +127,7 @@ const filesFilter = (opt: TarOptions, files: string[]) => {
: file => mapHas(stripTrailingSlashes(file))
}

const listFileSync = (opt: TarOptionsWithAliasesSyncFile) => {
const listFileSync = (opt: TarOptionsSyncFile) => {
const p = list_(opt)
const file = opt.file
let threw = true
Expand All @@ -152,12 +153,13 @@ const listFileSync = (opt: TarOptionsWithAliasesSyncFile) => {
if (threw && fd) {
try {
fs.closeSync(fd)
/* c8 ignore next */
} catch (er) {}
}
}
}

const listFile = (opt: TarOptionsFile, cb?: () => void) => {
const listFile = (opt: TarOptionsFile, cb?: () => void): Promise<void> => {
const parse = new Parser(opt)
const readSize = opt.maxReadSize || 16 * 1024 * 1024

Expand Down
2 changes: 2 additions & 0 deletions src/mkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const mkdir = (

// if there's any overlap between mask and mode,
// then we'll need an explicit chmod
/* c8 ignore next */
const umask = opt.umask ?? 0o22
const mode = opt.mode | 0o0700
const needChmod = (mode & umask) !== 0
Expand Down Expand Up @@ -215,6 +216,7 @@ export const mkdirSync = (dir: string, opt: MkdirOptions) => {
dir = normalizeWindowsPath(dir)
// if there's any overlap between mask and mode,
// then we'll need an explicit chmod
/* c8 ignore next */
const umask = opt.umask ?? 0o22
const mode = opt.mode | 0o700
const needChmod = (mode & umask) !== 0
Expand Down
14 changes: 7 additions & 7 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,18 @@ export interface TarOptionsWithAliases extends TarOptions {
export type TarOptionsWithAliasesSync = TarOptionsWithAliases & {
sync: true
}
export type TarOptionsWithAliasesFile = TarOptionsWithAliases & {
file: string
}
export type TarOptionsWithAliasesFile =
| (TarOptionsWithAliases & {
file: string
})
| (TarOptionsWithAliases & { f: string })
export type TarOptionsWithAliasesSyncFile =
TarOptionsWithAliasesSync & TarOptionsWithAliasesFile

export const isSyncFile = (o: TarOptions): o is TarOptionsSyncFile =>
!!o.sync && !!o.file
export const isSync = (o: TarOptions): o is TarOptionsSync =>
!!o.sync
export const isFile = (o: TarOptions): o is TarOptionsFile =>
!!o.file
export const isSync = (o: TarOptions): o is TarOptionsSync => !!o.sync
export const isFile = (o: TarOptions): o is TarOptionsFile => !!o.file

const dealiasKey = (
k: keyof TarOptionsWithAliases,
Expand Down
Loading

0 comments on commit 9522a69

Please sign in to comment.