Skip to content

Commit c3218a9

Browse files
authored
Replace mz with custom promisified wrappers (#140)
util.promisify is supported since node v8 https://nodejs.org/api/util.html#util_util_promisify_original fs.access is supported since 0.11 and recommended for checking file existence. https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback
1 parent 2430e38 commit c3218a9

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

index.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
* Module dependencies.
33
*/
44

5+
const fs = require('fs')
6+
const util = require('util')
57
const debug = require('debug')('koa-send')
68
const resolvePath = require('resolve-path')
79
const createError = require('http-errors')
810
const 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

1124
const {
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
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"dependencies": {
4242
"debug": "^4.1.1",
4343
"http-errors": "^1.7.3",
44-
"mz": "^2.7.0",
4544
"resolve-path": "^1.4.0"
4645
},
4746
"scripts": {

0 commit comments

Comments
 (0)