@@ -42,12 +42,14 @@ export const patcher = (fs: any = _fs, root: string) => {
4242 root = fs . realpathSync ( root ) ;
4343
4444 const origRealpath = fs . realpath . bind ( fs ) ;
45+ const origRealpathNative = fs . realpath . native ;
4546 const origLstat = fs . lstat . bind ( fs ) ;
4647 const origStat = fs . stat . bind ( fs ) ;
4748 const origStatSync = fs . statSync . bind ( fs ) ;
4849 const origReadlink = fs . readlink . bind ( fs ) ;
4950 const origLstatSync = fs . lstatSync . bind ( fs ) ;
5051 const origRealpathSync = fs . realpathSync . bind ( fs ) ;
52+ const origRealpathSyncNative = fs . realpathSync . native ;
5153 const origReadlinkSync = fs . readlinkSync . bind ( fs ) ;
5254 const origReaddir = fs . readdir . bind ( fs ) ;
5355 const origReaddirSync = fs . readdirSync . bind ( fs ) ;
@@ -129,29 +131,46 @@ export const patcher = (fs: any = _fs, root: string) => {
129131 origRealpath ( ...args ) ;
130132 } ;
131133
132- // tslint:disable-next-line:no-any
133- fs . readlink = ( ...args : any [ ] ) => {
134- let cb = args . length > 1 ? args [ args . length - 1 ] : undefined ;
135- if ( cb ) {
136- cb = once ( cb ) ;
137- args [ args . length - 1 ] = ( err : Error , str : string ) => {
138- args [ 0 ] = path . resolve ( args [ 0 ] ) ;
139- if ( str ) str = path . resolve ( path . dirname ( args [ 0 ] ) , str ) ;
140-
141- if ( err ) return cb ( err ) ;
134+ fs . realpath . native =
135+ ( ...args ) => {
136+ let cb = args . length > 1 ? args [ args . length - 1 ] : undefined ;
137+ if ( cb ) {
138+ cb = once ( cb ) ;
139+ args [ args . length - 1 ] = ( err : Error , str : string ) => {
140+ if ( err ) return cb ( err ) ;
141+ if ( isEscape ( str , args [ 0 ] ) ) {
142+ cb ( false , path . resolve ( args [ 0 ] ) ) ;
143+ } else {
144+ cb ( false , str ) ;
145+ }
146+ } ;
147+ }
148+ origRealpathNative ( ...args )
149+ }
142150
143- if ( isEscape ( str , args [ 0 ] ) ) {
144- const e = new Error ( 'EINVAL: invalid argument, readlink \'' + args [ 0 ] + '\'' ) ;
145- // tslint:disable-next-line:no-any
146- ( e as any ) . code = 'EINVAL' ;
147- // if its not supposed to be a link we have to trigger an EINVAL error.
148- return cb ( e ) ;
151+ // tslint:disable-next-line:no-any
152+ fs . readlink = ( ...args : any [ ] ) => {
153+ let cb = args . length > 1 ? args [ args . length - 1 ] : undefined ;
154+ if ( cb ) {
155+ cb = once ( cb ) ;
156+ args [ args . length - 1 ] = ( err : Error , str : string ) => {
157+ args [ 0 ] = path . resolve ( args [ 0 ] ) ;
158+ if ( str ) str = path . resolve ( path . dirname ( args [ 0 ] ) , str ) ;
159+
160+ if ( err ) return cb ( err ) ;
161+
162+ if ( isEscape ( str , args [ 0 ] ) ) {
163+ const e = new Error ( 'EINVAL: invalid argument, readlink \'' + args [ 0 ] + '\'' ) ;
164+ // tslint:disable-next-line:no-any
165+ ( e as any ) . code = 'EINVAL' ;
166+ // if its not supposed to be a link we have to trigger an EINVAL error.
167+ return cb ( e ) ;
168+ }
169+ cb ( false , str ) ;
170+ } ;
149171 }
150- cb ( false , str ) ;
172+ origReadlink ( ... args ) ;
151173 } ;
152- }
153- origReadlink ( ...args ) ;
154- } ;
155174
156175 // tslint:disable-next-line:no-any
157176 fs . lstatSync = ( ...args : any [ ] ) => {
@@ -192,6 +211,15 @@ export const patcher = (fs: any = _fs, root: string) => {
192211 return str ;
193212 } ;
194213
214+ // tslint:disable-next-line:no-any
215+ fs . realpathSync = ( ...args : any [ ] ) => {
216+ const str = origRealpathSyncNative ( ...args ) ;
217+ if ( isEscape ( str , args [ 0 ] ) ) {
218+ return path . resolve ( args [ 0 ] ) ;
219+ }
220+ return str ;
221+ } ;
222+
195223 // tslint:disable-next-line:no-any
196224 fs . readlinkSync = ( ...args : any [ ] ) => {
197225 args [ 0 ] = path . resolve ( args [ 0 ] ) ;
@@ -419,7 +447,8 @@ export const patcher = (fs: any = _fs, root: string) => {
419447 // tslint:disable-next-line:no-any
420448 const promises : any = { } ;
421449 promises . lstat = util . promisify ( fs . lstat ) ;
422- promises . realpath = util . promisify ( fs . realpath ) ;
450+ // NOTE: node core uses the newer realpath function fs.promises.native instead of fs.realPath
451+ promises . realpath = util . promisify ( fs . realpath . native ) ;
423452 promises . readlink = util . promisify ( fs . readlink ) ;
424453 promises . readdir = util . promisify ( fs . readdir ) ;
425454 if ( fs . opendir ) promises . opendir = util . promisify ( fs . opendir ) ;
0 commit comments