Skip to content

Commit 9e56e07

Browse files
authoredFeb 1, 2024
Upgrade glob to v10 (#406)
* update glob * use posix path * don't use replaceAll * use posix true * use posix.join * remove redundant replace * add back check * fix file paths on windows * normalize path after it's joined * remove console.log * bump * move glob pattern down * lessen split-joins * Revert "lessen split-joins" This reverts commit 32f875d. * move order * remove redundant split-join * upgrade to glob v9 * Revert "upgrade to glob v9" This reverts commit aad0548. * use one replace
1 parent bcf5664 commit 9e56e07

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed
 

‎index.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ const { PassThrough } = require('node:stream')
44
const path = require('node:path')
55
const { fileURLToPath } = require('node:url')
66
const { statSync } = require('node:fs')
7-
const { promisify } = require('node:util')
8-
const glob = require('glob')
9-
const globPromise = promisify(glob)
7+
const { glob } = require('glob')
108
const fp = require('fastify-plugin')
119
const send = require('@fastify/send')
1210
const encodingNegotiator = require('@fastify/accept-negotiator')
1311
const contentDisposition = require('content-disposition')
1412

1513
const dirList = require('./lib/dirList')
1614

17-
const winSeparatorRegex = new RegExp(`\\${path.win32.sep}`, 'gu')
18-
const backslashRegex = /\\/gu
19-
const startForwardSlashRegex = /^\//u
2015
const endForwardSlashRegex = /\/$/u
2116
const doubleForwardSlashRegex = /\/\//gu
2217
const asteriskRegex = /\*/gu
@@ -129,19 +124,20 @@ async function fastifyStatic (fastify, opts) {
129124
})
130125
}
131126
} else {
132-
const globPattern = '**/**'
127+
const indexes = opts.index === undefined ? ['index.html'] : [].concat(opts.index)
133128
const indexDirs = new Map()
134129
const routes = new Set()
130+
const globPattern = '**/**'
135131

136132
const roots = Array.isArray(sendOptions.root) ? sendOptions.root : [sendOptions.root]
137133
for (let i = 0; i < roots.length; ++i) {
138134
const rootPath = roots[i]
139-
const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot: opts.serveDotFiles })
140-
const indexes = opts.index === undefined ? ['index.html'] : [].concat(opts.index)
135+
const posixRootPath = rootPath.split(path.win32.sep).join(path.posix.sep)
136+
const files = await glob(`${posixRootPath}/${globPattern}`, { follow: true, nodir: true, dot: opts.serveDotFiles })
141137

142138
for (let i = 0; i < files.length; ++i) {
143-
const file = files[i].replace(rootPath.replace(backslashRegex, '/'), '')
144-
.replace(startForwardSlashRegex, '')
139+
const file = files[i].split(path.win32.sep).join(path.posix.sep)
140+
.replace(`${posixRootPath}/`, '')
145141
const route = (prefix + file).replace(doubleForwardSlashRegex, '/')
146142

147143
if (routes.has(route)) {
@@ -150,7 +146,7 @@ async function fastifyStatic (fastify, opts) {
150146

151147
routes.add(route)
152148

153-
setUpHeadAndGet(routeOpts, route, '/' + file, rootPath)
149+
setUpHeadAndGet(routeOpts, route, `/${file}`, rootPath)
154150

155151
const key = path.posix.basename(route)
156152
if (indexes.includes(key) && !indexDirs.has(key)) {

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@fastify/send": "^2.0.0",
3636
"content-disposition": "^0.5.3",
3737
"fastify-plugin": "^4.0.0",
38-
"glob": "^8.0.1",
38+
"glob": "^10.3.4",
3939
"p-limit": "^3.1.0"
4040
},
4141
"devDependencies": {

0 commit comments

Comments
 (0)
Please sign in to comment.