@@ -10,7 +10,7 @@ import { promisify } from "util"
1010
1111import type { IGatsbyPage } from "../../internal"
1212import type { ISSRData } from "./entry"
13- import { link } from "linkfs"
13+ import { link , rewritableMethods as linkRewritableMethods } from "linkfs"
1414
1515const cdnDatastore = `%CDN_DATASTORE_PATH%`
1616const PATH_PREFIX = `%PATH_PREFIX%`
@@ -30,7 +30,7 @@ function setupFsWrapper(): string {
3030 global . __GATSBY . root = TEMP_DIR
3131
3232 // TODO: don't hardcode this
33- const cacheDir = `/var/task /.cache`
33+ const cacheDir = `${ process . cwd ( ) } /.cache`
3434
3535 // we need to rewrite fs
3636 const rewrites = [
@@ -47,25 +47,132 @@ function setupFsWrapper(): string {
4747 to : TEMP_CACHE_DIR ,
4848 rewrites,
4949 } )
50+
51+ // copied from https://github.com/streamich/linkfs/blob/master/src/index.ts#L126-L142
52+ function mapPathUsingRewrites ( fsPath : fs . PathLike ) : string {
53+ let filename = path . resolve ( String ( fsPath ) )
54+ for ( const [ from , to ] of rewrites ) {
55+ if ( filename . indexOf ( from ) === 0 ) {
56+ const rootRegex = / (?: ^ [ a - z A - Z ] : \\ $ ) | (?: ^ \/ $ ) / // C:\ vs /
57+ const isRoot = from . match ( rootRegex )
58+ const baseRegex = `^(` + from . replace ( / \\ / g, `\\\\` ) + `)`
59+
60+ if ( isRoot ) {
61+ const regex = new RegExp ( baseRegex )
62+ filename = filename . replace ( regex , ( ) => to + path . sep )
63+ } else {
64+ const regex = new RegExp ( baseRegex + `(\\\\|/|$)` )
65+ filename = filename . replace ( regex , ( _match , _p1 , p2 ) => to + p2 )
66+ }
67+ }
68+ }
69+ return filename
70+ }
71+
72+ function applyRename <
73+ T = typeof import ( "fs" ) | typeof import ( "fs" ) . promises
74+ > ( fsToRewrite : T , lfs : T , method : "rename" | "renameSync" ) : void {
75+ const original = fsToRewrite [ method ]
76+ if ( original ) {
77+ // @ts -ignore - complains about __promisify__ which doesn't actually exist in runtime
78+ lfs [ method ] = (
79+ ...args : Parameters < typeof import ( "fs" ) [ "rename" | "renameSync" ] >
80+ ) : ReturnType < typeof import ( "fs" ) [ "rename" | "renameSync" ] > => {
81+ args [ 0 ] = mapPathUsingRewrites ( args [ 0 ] )
82+ args [ 1 ] = mapPathUsingRewrites ( args [ 1 ] )
83+ // @ts -ignore - can't decide which signature this is, but we just preserve the original
84+ // signature here and mostly care about first 2 arguments being PathLike
85+ return original . apply ( fsToRewrite , args )
86+ }
87+ }
88+ }
89+
90+ // linkfs doesn't handle following methods, so we need to add them manually
91+ linkRewritableMethods . push ( `createWriteStream` , `createReadStream` , `rm` )
92+
93+ function createLinkedFS <
94+ T = typeof import ( "fs" ) | typeof import ( "fs" ) . promises
95+ > ( fsToRewrite : T ) : T {
96+ const linkedFS = link ( fsToRewrite , rewrites ) as T
97+
98+ // linkfs doesn't handle `to` argument in `rename` and `renameSync` methods
99+ applyRename ( fsToRewrite , linkedFS , `rename` )
100+ applyRename ( fsToRewrite , linkedFS , `renameSync` )
101+
102+ return linkedFS
103+ }
104+
50105 // Alias the cache dir paths to the temp dir
51- const lfs = link ( fs , rewrites ) as typeof import ( "fs" )
106+ const lfs = createLinkedFS ( fs )
52107
53108 // linkfs doesn't pass across the `native` prop, which graceful-fs needs
54109 for ( const key in lfs ) {
55110 if ( Object . hasOwnProperty . call ( fs [ key ] , `native` ) ) {
56111 lfs [ key ] . native = fs [ key ] . native
57112 }
58113 }
59-
60- const dbPath = path . join ( TEMP_CACHE_DIR , `data` , `datastore` )
61-
62114 // 'promises' is not initially linked within the 'linkfs'
63115 // package, and is needed by underlying Gatsby code (the
64116 // @graphql -tools/code-file-loader)
65- lfs . promises = link ( fs . promises , rewrites )
117+ lfs . promises = createLinkedFS ( fs . promises )
118+
119+ const originalWritesStream = fs . WriteStream
120+ function LinkedWriteStream (
121+ this : fs . WriteStream ,
122+ ...args : Parameters < ( typeof fs ) [ "createWriteStream" ] >
123+ ) : fs . WriteStream {
124+ args [ 0 ] = mapPathUsingRewrites ( args [ 0 ] )
125+ args [ 1 ] =
126+ typeof args [ 1 ] === `string`
127+ ? {
128+ flags : args [ 1 ] ,
129+ // @ts -ignore there is `fs` property in options doh (https://nodejs.org/api/fs.html#fscreatewritestreampath-options)
130+ fs : lfs ,
131+ }
132+ : {
133+ ...( args [ 1 ] || { } ) ,
134+ // @ts -ignore there is `fs` property in options doh (https://nodejs.org/api/fs.html#fscreatewritestreampath-options)
135+ fs : lfs ,
136+ }
137+
138+ // @ts -ignore TS doesn't like extending prototype "classes"
139+ return originalWritesStream . apply ( this , args )
140+ }
141+ LinkedWriteStream . prototype = Object . create ( originalWritesStream . prototype )
142+ // @ts -ignore TS doesn't like extending prototype "classes"
143+ lfs . WriteStream = LinkedWriteStream
144+
145+ const originalReadStream = fs . ReadStream
146+ function LinkedReadStream (
147+ this : fs . ReadStream ,
148+ ...args : Parameters < ( typeof fs ) [ "createReadStream" ] >
149+ ) : fs . ReadStream {
150+ args [ 0 ] = mapPathUsingRewrites ( args [ 0 ] )
151+ args [ 1 ] =
152+ typeof args [ 1 ] === `string`
153+ ? {
154+ flags : args [ 1 ] ,
155+ // @ts -ignore there is `fs` property in options doh (https://nodejs.org/api/fs.html#fscreatewritestreampath-options)
156+ fs : lfs ,
157+ }
158+ : {
159+ ...( args [ 1 ] || { } ) ,
160+ // @ts -ignore there is `fs` property in options doh (https://nodejs.org/api/fs.html#fscreatewritestreampath-options)
161+ fs : lfs ,
162+ }
163+
164+ // @ts -ignore TS doesn't like extending prototype "classes"
165+ return originalReadStream . apply ( this , args )
166+ }
167+ LinkedReadStream . prototype = Object . create ( originalReadStream . prototype )
168+ // @ts -ignore TS doesn't like extending prototype "classes"
169+ lfs . ReadStream = LinkedReadStream
170+
171+ const dbPath = path . join ( TEMP_CACHE_DIR , `data` , `datastore` )
66172
67173 // Gatsby uses this instead of fs if present
68174 // eslint-disable-next-line no-underscore-dangle
175+ // @ts -ignore __promisify__ stuff
69176 global . _fsWrapper = lfs
70177
71178 if ( ! cdnDatastore ) {
0 commit comments