Skip to content

Commit

Permalink
make sure node_modules is not there and add another test
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Feb 2, 2024
1 parent e3f7bc3 commit e28166e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ async function generateFileHash (filePath) {
* 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 {string|string[]|undefined} ignore - An array of glob patterns to ignore.
* @param {string|string[]} [ignore] - An array of glob patterns to ignore.
* @param {boolean} [includeDotFiles] - Whether to include dot files.
* @param {boolean} [writeToFile] - Whether to write the hash map to a file (for build) or return it (for runtime).
* @param {string} [outputPath='.tmp/hashes.json'] - The output file path, if writing to a file.
* @returns {Promise<void|Map<string, string>>} - Returns nothing if writing to a file, or the hash map if not.
*/
async function generateHashes (rootPaths, ignore, includeDotFiles = false, writeToFile = false, outputPath = '.tmp/hashes.json') {
async function generateHashes (rootPaths, ignore = ['node_modules/**'], includeDotFiles = false, writeToFile = false, outputPath = '.tmp/hashes.json') {
const fileHashes = new Map()
const roots = Array.isArray(rootPaths) ? rootPaths : [rootPaths]

Expand Down
39 changes: 39 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,45 @@ t.test('register /static with hash prebuilt hashes', (t) => {
})
})

t.test('register /static with hash prebuilt hashes, two roots', (t) => {
t.plan(2)

const pluginOptions = {
root: [path.join(__dirname, '/static'), path.join(__dirname, '/static-pre-compressed')],
prefix: '/static/',
maxAge: 365 * 24 * 60 * 60 * 1000,
serveDotFiles: true,
hash: true,
immutable: true,
wildcard: false
}

generateHashes(pluginOptions.root, [], true, true).then(() => {
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)
t.teardown(fastify.close.bind(fastify))

fastify.listen({ port: 0 }, (err) => {
t.error(err)

fastify.server.unref()

t.test('/static/foo.html', (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + fastify.getHashedAsset('uncompressed.html')
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), uncompressedStatic)
genericResponseChecks(t, response)
})
})
})
})
})

t.test('register /static with hash (incorrect)', async (t) => {
const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand Down

0 comments on commit e28166e

Please sign in to comment.