From 9e56e07a6da5e9be9b946650a6bdb6d71073226f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 1 Feb 2024 16:08:28 +0100 Subject: [PATCH] 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 32f875d6562ccdd21d3d94402e733371c755051f. * move order * remove redundant split-join * upgrade to glob v9 * Revert "upgrade to glob v9" This reverts commit aad054877fc2d45392ce0578315ee9624ade2144. * use one replace --- index.js | 20 ++++++++------------ package.json | 2 +- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 266a37d..df76735 100644 --- a/index.js +++ b/index.js @@ -4,9 +4,7 @@ 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') @@ -14,9 +12,6 @@ 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 @@ -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)) { @@ -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)) { diff --git a/package.json b/package.json index c9da255..dcbb1d8 100644 --- a/package.json +++ b/package.json @@ -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": {