diff --git a/bin/fastify-static-prehash.js b/bin/fastify-static-prehash.js index f57470f..39dad01 100755 --- a/bin/fastify-static-prehash.js +++ b/bin/fastify-static-prehash.js @@ -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) diff --git a/index.js b/index.js index 6aa78b8..00b3a24 100644 --- a/index.js +++ b/index.js @@ -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 + } + )) } } diff --git a/lib/hash.js b/lib/hash.js index e814700..c8ef4e9 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -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>} - 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] @@ -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) { diff --git a/test/static.test.js b/test/static.test.js index 3f69f15..f0826c0 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -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(() => { @@ -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(() => { @@ -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(() => { @@ -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(() => { @@ -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) }