22 * Module dependencies.
33 */
44
5+ const fs = require ( 'fs' )
6+ const util = require ( 'util' )
57const debug = require ( 'debug' ) ( 'koa-send' )
68const resolvePath = require ( 'resolve-path' )
79const createError = require ( 'http-errors' )
810const assert = require ( 'assert' )
9- const fs = require ( 'mz/fs' )
11+
12+ const stat = util . promisify ( fs . stat )
13+ const access = util . promisify ( fs . access )
14+
15+ async function exists ( path ) {
16+ try {
17+ await access ( path )
18+ return true
19+ } catch ( e ) {
20+ return false
21+ }
22+ }
1023
1124const {
1225 normalize,
@@ -72,12 +85,12 @@ async function send (ctx, path, opts = {}) {
7285
7386 let encodingExt = ''
7487 // serve brotli file when possible otherwise gzipped file when possible
75- if ( ctx . acceptsEncodings ( 'br' , 'identity' ) === 'br' && brotli && ( await fs . exists ( path + '.br' ) ) ) {
88+ if ( ctx . acceptsEncodings ( 'br' , 'identity' ) === 'br' && brotli && ( await exists ( path + '.br' ) ) ) {
7689 path = path + '.br'
7790 ctx . set ( 'Content-Encoding' , 'br' )
7891 ctx . res . removeHeader ( 'Content-Length' )
7992 encodingExt = '.br'
80- } else if ( ctx . acceptsEncodings ( 'gzip' , 'identity' ) === 'gzip' && gzip && ( await fs . exists ( path + '.gz' ) ) ) {
93+ } else if ( ctx . acceptsEncodings ( 'gzip' , 'identity' ) === 'gzip' && gzip && ( await exists ( path + '.gz' ) ) ) {
8194 path = path + '.gz'
8295 ctx . set ( 'Content-Encoding' , 'gzip' )
8396 ctx . res . removeHeader ( 'Content-Length' )
@@ -92,7 +105,7 @@ async function send (ctx, path, opts = {}) {
92105 throw new TypeError ( 'option extensions must be array of strings or false' )
93106 }
94107 if ( ! / ^ \. / . exec ( ext ) ) ext = `.${ ext } `
95- if ( await fs . exists ( `${ path } ${ ext } ` ) ) {
108+ if ( await exists ( `${ path } ${ ext } ` ) ) {
96109 path = `${ path } ${ ext } `
97110 break
98111 }
@@ -102,15 +115,15 @@ async function send (ctx, path, opts = {}) {
102115 // stat
103116 let stats
104117 try {
105- stats = await fs . stat ( path )
118+ stats = await stat ( path )
106119
107120 // Format the path to serve static file servers
108121 // and not require a trailing slash for directories,
109122 // so that you can do both `/directory` and `/directory/`
110123 if ( stats . isDirectory ( ) ) {
111124 if ( format && index ) {
112125 path += `/${ index } `
113- stats = await fs . stat ( path )
126+ stats = await stat ( path )
114127 } else {
115128 return
116129 }
0 commit comments