@fastify/static
Advanced tools
Comparing version 7.0.0 to 7.0.1
28
index.js
@@ -16,3 +16,2 @@ 'use strict' | ||
const endForwardSlashRegex = /\/$/u | ||
const doubleForwardSlashRegex = /\/\//gu | ||
const asteriskRegex = /\*/gu | ||
@@ -116,10 +115,7 @@ | ||
if (opts.wildcard === undefined || opts.wildcard === true) { | ||
fastify.head(prefix + '*', routeOpts, function (req, reply) { | ||
fastify.get(prefix + '*', { ...routeOpts, exposeHeadRoute: true }, (req, reply) => { | ||
pumpSendToReply(req, reply, '/' + req.params['*'], sendOptions.root) | ||
}) | ||
fastify.get(prefix + '*', routeOpts, function (req, reply) { | ||
pumpSendToReply(req, reply, '/' + req.params['*'], sendOptions.root) | ||
}) | ||
if (opts.redirect === true && prefix !== opts.prefix) { | ||
fastify.get(opts.prefix, routeOpts, function (req, reply) { | ||
fastify.get(opts.prefix, routeOpts, (req, reply) => { | ||
reply.redirect(301, getRedirectUrl(req.raw.url)) | ||
@@ -132,14 +128,14 @@ }) | ||
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 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 rootPath of roots) { | ||
rootPath = rootPath.split(path.win32.sep).join(path.posix.sep) | ||
!rootPath.endsWith('/') && (rootPath += '/') | ||
const files = await glob('**/**', { | ||
cwd: rootPath, absolute: false, follow: true, nodir: true, dot: opts.serveDotFiles | ||
}) | ||
for (let i = 0; i < files.length; ++i) { | ||
const file = files[i].split(path.win32.sep).join(path.posix.sep) | ||
.replace(`${posixRootPath}/`, '') | ||
const route = (prefix + file).replace(doubleForwardSlashRegex, '/') | ||
for (let file of files) { | ||
file = file.split(path.win32.sep).join(path.posix.sep) | ||
const route = prefix + file | ||
@@ -181,3 +177,3 @@ if (routes.has(route)) { | ||
rootPathOffset = 0, | ||
pumpOptions = {}, | ||
pumpOptions, | ||
checkedEncodings | ||
@@ -184,0 +180,0 @@ ) { |
'use strict' | ||
const os = require('node:os') | ||
const path = require('node:path') | ||
const fs = require('node:fs/promises') | ||
const pLimit = require('p-limit') | ||
const fastq = require('fastq') | ||
const fastqConcurrency = Math.max(1, os.cpus().length - 1) | ||
const dirList = { | ||
_getExtendedInfo: async function (dir, info) { | ||
const depth = dir.split(path.sep).length | ||
const files = await fs.readdir(dir) | ||
const worker = async (filename) => { | ||
const filePath = path.join(dir, filename) | ||
let stats | ||
try { | ||
stats = await fs.stat(filePath) | ||
} catch { | ||
return | ||
} | ||
if (stats.isDirectory()) { | ||
info.totalFolderCount++ | ||
filePath.split(path.sep).length === depth + 1 && info.folderCount++ | ||
await dirList._getExtendedInfo(filePath, info) | ||
} else { | ||
info.totalSize += stats.size | ||
info.totalFileCount++ | ||
filePath.split(path.sep).length === depth + 1 && info.fileCount++ | ||
info.lastModified = Math.max(info.lastModified, stats.mtimeMs) | ||
} | ||
} | ||
const queue = fastq.promise(worker, fastqConcurrency) | ||
await Promise.all(files.map(filename => queue.push(filename))) | ||
}, | ||
/** | ||
* get extended info about a folder | ||
* @param {string} folderPath full path fs dir | ||
* @return {Promise<ExtendedInfo>} | ||
*/ | ||
getExtendedInfo: async function (folderPath) { | ||
const info = { | ||
totalSize: 0, | ||
fileCount: 0, | ||
totalFileCount: 0, | ||
folderCount: 0, | ||
totalFolderCount: 0, | ||
lastModified: 0 | ||
} | ||
await dirList._getExtendedInfo(folderPath, info) | ||
return info | ||
}, | ||
/** | ||
* get files and dirs from dir, or error | ||
@@ -25,4 +75,3 @@ * @param {string} dir full path fs dir | ||
const limit = pLimit(4) | ||
await Promise.all(files.map(filename => limit(async () => { | ||
const worker = async (filename) => { | ||
let stats | ||
@@ -37,3 +86,3 @@ try { | ||
if (options.extendedFolderInfo) { | ||
entry.extendedInfo = await getExtendedInfo(path.join(dir, filename)) | ||
entry.extendedInfo = await dirList.getExtendedInfo(path.join(dir, filename)) | ||
} | ||
@@ -44,52 +93,5 @@ entries.dirs.push(entry) | ||
} | ||
}))) | ||
async function getExtendedInfo (folderPath) { | ||
const depth = folderPath.split(path.sep).length | ||
let totalSize = 0 | ||
let fileCount = 0 | ||
let totalFileCount = 0 | ||
let folderCount = 0 | ||
let totalFolderCount = 0 | ||
let lastModified = 0 | ||
async function walk (dir) { | ||
const files = await fs.readdir(dir) | ||
const limit = pLimit(4) | ||
await Promise.all(files.map(filename => limit(async () => { | ||
const filePath = path.join(dir, filename) | ||
let stats | ||
try { | ||
stats = await fs.stat(filePath) | ||
} catch { | ||
return | ||
} | ||
if (stats.isDirectory()) { | ||
totalFolderCount++ | ||
if (filePath.split(path.sep).length === depth + 1) { | ||
folderCount++ | ||
} | ||
await walk(filePath) | ||
} else { | ||
totalSize += stats.size | ||
totalFileCount++ | ||
if (filePath.split(path.sep).length === depth + 1) { | ||
fileCount++ | ||
} | ||
lastModified = Math.max(lastModified, stats.mtimeMs) | ||
} | ||
}))) | ||
} | ||
await walk(folderPath) | ||
return { | ||
totalSize, | ||
fileCount, | ||
totalFileCount, | ||
folderCount, | ||
totalFolderCount, | ||
lastModified | ||
} | ||
} | ||
const queue = fastq.promise(worker, fastqConcurrency) | ||
await Promise.all(files.map(filename => queue.push(filename))) | ||
@@ -121,2 +123,3 @@ entries.dirs.sort((a, b) => a.name.localeCompare(b.name)) | ||
} | ||
const format = reply.request.query.format || options.format | ||
@@ -210,5 +213,4 @@ if (format !== 'html') { | ||
} | ||
} | ||
module.exports = dirList |
{ | ||
"name": "@fastify/static", | ||
"version": "7.0.0", | ||
"version": "7.0.1", | ||
"description": "Plugin for serving static files as fast as possible.", | ||
@@ -38,7 +38,7 @@ "main": "index.js", | ||
"fastify-plugin": "^4.0.0", | ||
"glob": "^10.3.4", | ||
"p-limit": "^3.1.0" | ||
"fastq": "^1.17.0", | ||
"glob": "^10.3.4" | ||
}, | ||
"devDependencies": { | ||
"@fastify/compress": "^6.0.0", | ||
"@fastify/compress": "^7.0.0", | ||
"@fastify/pre-commit": "^2.0.2", | ||
@@ -45,0 +45,0 @@ "@types/node": "^20.1.0", |
Sorry, the diff of this file is too big to display
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
413255
5379
1
+ Addedfastq@^1.17.0
+ Addedfastq@1.17.1(transitive)
+ Addedreusify@1.0.4(transitive)
- Removedp-limit@^3.1.0
- Removedp-limit@3.1.0(transitive)
- Removedyocto-queue@0.1.0(transitive)