Skip to content

require('fs').stat mangling argument #8361

Closed
@cbaron

Description

@cbaron

Version: v6.1.0, v6.5.0
Platform: Darwin ${mymacbook}.local 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64

Subsystem: fs

The repo is here : https://github.com/cbaron/Enterprise

It is a web application. The static file server I wrote is having an issue with the latest node. The app.js, router.js, and lib/MyObject.js files are of concern. Sometimes, everything works fine, other times, it seems that the closure in my code is not being respected as I will try to illustrate below. I would be happy to work through this issue if you do not have enough information.

    static( request, response, path ) {
            var fileName = path.pop()
                filePath = `${__dirname}/${path.join('/')}/${fileName}`,
                ext = this.Path.extname( filePath )

            return this.P( this.FS.stat, [ filePath ] )
            .then( ( [ stat ] ) => new Promise( ( resolve, reject ) => {

                var stream = this.FS.createReadStream( filePath )

                response.on( 'error', e => { stream.end(); reject(e) } )
                stream.on( 'error', reject )
                stream.on( 'end', () => {
                    console.log( fileName, filePath, stat.size, stream.bytesRead )
                    response.end();
                    resolve()
                } )

                response.writeHead(
                    200,
                    {
                        'Connection': 'keep-alive',
                        'Content-Encoding': ext === ".gz" ? 'gzip' : 'identity',
                        'Content-Length': stat.size,
                        'Content-Type':
                            /\.css/.test(fileName)
                                ? 'text/css'
                                : ext === '.svg'
                                    ? 'image/svg+xml'
                                    : 'text/plain'
                    }
                )
                stream.pipe( response, { end: false } )
            } ) )
        }

this.P refers to the function ->

( fun, args=[ ], thisArg=undefined ) =>
        new Promise( ( resolve, reject ) => Reflect.apply( fun, thisArg, args.concat( ( e, ...callback ) => e ? reject(e) : resolve(callback) ) ) )

this.FS refers to require('fs')

The request argument is an http request, the response is the response object, and path is information that pertains to a file on the local system. You can simply pointfilePath point to a local file on your system.

The issue is occurring when my web page requests 3 static files at the same time.

When it works correctly, I get the following STDOUT

main.css.gz /Users/cbaron/freelancR/Enterprise/static/css/main.css.gz 1514 1514
debug.js.gz /Users/cbaron/freelancR/Enterprise/static/js/debug.js.gz 53243 53243
vendor.js.gz /Users/cbaron/freelancR/Enterprise/static/js/debug.js.gz 140517 140517

When it doesn't =>

main.css.gz /Users/cbaron/freelancR/Enterprise/static/js/debug.js.gz 1514 53243
vendor.js.gz /Users/cbaron/freelancR/Enterprise/static/js/debug.js.gz 140517 53243
debug.js.gz /Users/cbaron/freelancR/Enterprise/static/js/debug.js.gz 53243 53243

Note that the filePath variable in the static function is not being closed properly. It seems something is happening to that literal in require('fs').stat.

Let me know how I can help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions