Skip to content

Commit

Permalink
refactor: align async nextHop with sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Apr 18, 2023
1 parent e061f8b commit 368c35f
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 184 deletions.
88 changes: 45 additions & 43 deletions js/private/node-patches/fs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 58 additions & 47 deletions js/private/node-patches/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,55 +523,39 @@ export const patcher = (fs: any = _fs, roots: string[]) => {

function nextHop(loc: string, cb: (next: string | false) => void): void {
let nested: string[] = []
let maybe = loc
let escapedHop: string | false = false
const oneHop = (maybe, cb: (next: string | false) => void) => {
origReadlink(maybe, (err: Error, str: string) => {
if (err) {
if ((err as any).code === 'ENOENT') {
// file does not exist
return cb(undefined)
}
nested.push(path.basename(maybe))
const dirname = path.dirname(maybe)
if (
!dirname ||
dirname == maybe ||
dirname == '.' ||
dirname == '/'
) {
// not a link
return cb(escapedHop)
}
maybe = dirname
return oneHop(maybe, cb)
}
if (!path.isAbsolute(str)) {
str = path.resolve(path.dirname(maybe), str)

readHopLink(maybe, function readNextHop(link) {
if (link === HOP_NOT_FOUND) {
return cb(undefined)
}

if (link !== HOP_NON_LINK) {
link = path.join(link, ...nested.reverse())

if (!isEscape(loc, link)) {
return cb(link)
}
str = path.join(str, ...nested.reverse())
if (isEscape(loc, str)) {
if (!escapedHop) {
escapedHop = str
}
nested.push(path.basename(maybe))
const dirname = path.dirname(maybe)
if (
!dirname ||
dirname == maybe ||
dirname == '.' ||
dirname == '/'
) {
// not a link
return cb(escapedHop)
}
maybe = dirname
return oneHop(maybe, cb)
} else {
return cb(str)
if (!escapedHop) {
escapedHop = link
}
})
}
oneHop(loc, cb)
}

const dirname = path.dirname(maybe)
if (
!dirname ||
dirname == maybe ||
dirname == '.' ||
dirname == '/'
) {
// not a link
return cb(escapedHop)
}
nested.push(path.basename(maybe))
maybe = dirname
readHopLink(maybe, readNextHop)
})
}

const hopLinkCache = Object.create(null) as { [f: string]: HopResults }
Expand Down Expand Up @@ -604,6 +588,29 @@ export const patcher = (fs: any = _fs, roots: string[]) => {
return link
}

function readHopLink(p: string, cb: (l: HopResults) => void) {
origReadlink(p, (err: Error, link: string) => {
if (err) {
if ((err as any).code === 'ENOENT') {
// file does not exist
return cb(HOP_NOT_FOUND)
}

return cb(HOP_NON_LINK)
}

if (link === undefined) {
return cb(HOP_NON_LINK)
}

if (!path.isAbsolute(link)) {
link = path.resolve(path.dirname(p), link)
}

cb(link)
})
}

function nextHopSync(loc: string): string | false {
let nested: string[] = []
let maybe = loc
Expand All @@ -612,8 +619,12 @@ export const patcher = (fs: any = _fs, roots: string[]) => {
for (;;) {
let link = readHopLinkSync(maybe)

if (link === HOP_NOT_FOUND) {
return undefined
}

if (link !== HOP_NON_LINK) {
link = path.join(link as string, ...nested.reverse())
link = path.join(link, ...nested.reverse())

if (!isEscape(loc, link)) {
return link
Expand Down
88 changes: 45 additions & 43 deletions js/private/node-patches_legacy/fs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 368c35f

Please sign in to comment.