Skip to content

Commit

Permalink
Upgrade glob to v10 (#406)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gurgunday authored Feb 1, 2024
1 parent bcf5664 commit 9e56e07
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
20 changes: 8 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ const { PassThrough } = require('node:stream')
const path = require('node:path')
const { fileURLToPath } = require('node:url')
const { statSync } = require('node:fs')
const { promisify } = require('node:util')
const glob = require('glob')
const globPromise = promisify(glob)
const { glob } = require('glob')
const fp = require('fastify-plugin')
const send = require('@fastify/send')
const encodingNegotiator = require('@fastify/accept-negotiator')
const contentDisposition = require('content-disposition')

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

const winSeparatorRegex = new RegExp(`\\${path.win32.sep}`, 'gu')
const backslashRegex = /\\/gu
const startForwardSlashRegex = /^\//u
const endForwardSlashRegex = /\/$/u
const doubleForwardSlashRegex = /\/\//gu
const asteriskRegex = /\*/gu
Expand Down Expand Up @@ -129,19 +124,20 @@ async function fastifyStatic (fastify, opts) {
})
}
} else {
const globPattern = '**/**'
const indexes = opts.index === undefined ? ['index.html'] : [].concat(opts.index)
const indexDirs = new Map()
const routes = new Set()
const globPattern = '**/**'

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

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

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

routes.add(route)

setUpHeadAndGet(routeOpts, route, '/' + file, rootPath)
setUpHeadAndGet(routeOpts, route, `/${file}`, rootPath)

const key = path.posix.basename(route)
if (indexes.includes(key) && !indexDirs.has(key)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@fastify/send": "^2.0.0",
"content-disposition": "^0.5.3",
"fastify-plugin": "^4.0.0",
"glob": "^8.0.1",
"glob": "^10.3.4",
"p-limit": "^3.1.0"
},
"devDependencies": {
Expand Down

0 comments on commit 9e56e07

Please sign in to comment.