Skip to content

Commit

Permalink
use named param
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Feb 5, 2024
1 parent fd94f70 commit 14da551
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
9 changes: 8 additions & 1 deletion bin/fastify-static-prehash.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ async function run () {
ignorePatterns = ignorePatterns ? ignorePatterns.split(',') : []

try {
await generateHashes(rootPaths, includeDotFiles, ignorePatterns, true, writeLocation)
await generateHashes({
rootPaths,
includeDotFiles,
skip: ignorePatterns,
writeToFile: true,
outputPath: writeLocation
}
)
console.log('Hashes generated successfully.')
} catch (error) {
console.error('Error generating hashes:', error)
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ async function fastifyStatic (fastify, opts) {
const hashesContent = await readFile(opts.hash.path, 'utf8')
fastify.decorate(kFileHashes, new Map(Object.entries(JSON.parse(hashesContent))))
} else {
fastify.decorate(kFileHashes, await generateHashes(opts.root, opts.serveDotFiles, opts.hash.skip))
fastify.decorate(kFileHashes, await generateHashes({
rootPaths: opts.root,
includeDotFiles: opts.serveDotFiles,
skip: opts.hash.skip
}
))
}
}

Expand Down
17 changes: 9 additions & 8 deletions lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ async function generateFileHash (filePath) {
/**
* Generates hashes for files matching the glob pattern within specified directories.
* Can be used both at build time and runtime.
*
* @param {string|string[]} rootPaths - A single root directory or an array of root directories to process.
* @param {boolean} [includeDotFiles] - Whether to include dot files.
* @param {string|string[]} [ignore] - An array of glob patterns to ignore.
* @param {boolean} [writeToFile] - Whether to write the hash map to a file (for build) or return it (for runtime).
* @param {string} [outputPath] - The output file path, if writing to a file.
* @param {{ rootPaths: string|string[], includeDotFiles?: boolean, skip?: string|string[], writeToFile?: boolean, outputPath?: string }} options - The options object.
* @returns {Promise<void|Map<string, string>>} - Returns nothing if writing to a file, or the hash map if not.
*/
async function generateHashes (rootPaths, includeDotFiles = false, ignore = ['node_modules/**'], writeToFile = false, outputPath) {
async function generateHashes ({
rootPaths,
includeDotFiles = false,
skip = ['node_modules/**'],
writeToFile = false,
outputPath
}) {
const fileHashes = new Map()
const roots = Array.isArray(rootPaths) ? rootPaths : [rootPaths]

Expand All @@ -42,7 +43,7 @@ async function generateHashes (rootPaths, includeDotFiles = false, ignore = ['no
const files = []

const filesIterable = new Glob('**/**', {
cwd: rootPath, absolute: true, follow: true, nodir: true, dot: includeDotFiles, ignore
cwd: rootPath, absolute: true, follow: true, nodir: true, dot: includeDotFiles, ignore: skip
})

for await (let file of filesIterable) {
Expand Down
39 changes: 34 additions & 5 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,14 @@ t.test('register /static with hash pregenerated hashes', (t) => {
hash: { path: hashPath }
}

generateHashes(pluginOptions.root, true, ['foo.html'], true, hashPath).then(() => {
generateHashes({
rootPaths: pluginOptions.root,
includeDotFiles: true,
skip: ['foo.html'],
writeToFile: true,
outputPath: hashPath
}
).then(() => {
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)
t.teardown(() => {
Expand Down Expand Up @@ -496,7 +503,13 @@ t.test('register /static with hash pregenerated hashes', (t) => {
wildcard: false
}

generateHashes(pluginOptions.root, true, undefined, true, hashPath).then(() => {
generateHashes({
rootPaths: pluginOptions.root,
includeDotFiles: true,
writeToFile: true,
outputPath: hashPath
}
).then(() => {
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)
t.teardown(() => {
Expand Down Expand Up @@ -538,7 +551,13 @@ t.test('register /static with hash prebuilt hashes, two roots', (t) => {
wildcard: false
}

generateHashes(pluginOptions.root, true, [], true, hashPath).then(() => {
generateHashes({
rootPaths: pluginOptions.root,
includeDotFiles: true,
writeToFile: true,
outputPath: hashPath
}
).then(() => {
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)
t.teardown(() => {
Expand Down Expand Up @@ -581,7 +600,13 @@ t.test('register /static with hash prebuilt hashes with custom location', (t) =>
wildcard: false
}

generateHashes(pluginOptions.root, true, [], true, hashPath).then(() => {
generateHashes({
rootPaths: pluginOptions.root,
includeDotFiles: true,
writeToFile: true,
outputPath: hashPath
}
).then(() => {
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)
t.teardown(() => {
Expand Down Expand Up @@ -625,7 +650,11 @@ t.test('register /static with hash (incorrect)', async (t) => {
}

try {
await generateHashes(pluginOptions.root, true, [], true)
await generateHashes({
rootPaths: pluginOptions.root,
includeDotFiles: true,
writeToFile: true
})
} catch (error) {
t.ok(error instanceof Error)
}
Expand Down

0 comments on commit 14da551

Please sign in to comment.