| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const fs = require('node:fs'); |
| 6 | const path = require('node:path'); |
| 7 | const crypto = require('node:crypto'); |
| 8 | const { spawnSync } = require('node:child_process'); |
| 9 | |
| 10 | const root = path.join(__dirname, '..'); |
| 11 | const htmlPath = path.join(root, 'build/index.html'); |
| 12 | const htaccessPath = path.join(root, 'server/.htaccess'); |
| 13 | |
| 14 | function sha12(file) |
| 15 | { |
| 16 | const buf = fs.readFileSync(path.join(root, file)); |
| 17 | return crypto.createHash('sha256').update(buf).digest('hex').slice(0, 12); |
| 18 | } |
| 19 | |
| 20 | function attr(html, attr, file) |
| 21 | { |
| 22 | const re = new RegExp(attr + '="' + file.replace('.', '\\.') + '\\?v=([0-9a-f]{12})"'); |
| 23 | const m = html.match(re); |
| 24 | assert.ok(m, attr + ' ' + file + ' is missing a 12-char hash query in index.html'); |
| 25 | return m[1]; |
| 26 | } |
| 27 | |
| 28 | describe('asset cache busting (t1256)', () => |
| 29 | { |
| 30 | it('stamps content hashes on JS and CSS in index.html', () => |
| 31 | { |
| 32 | const html = fs.readFileSync(htmlPath, 'utf8'); |
| 33 | assert.equal(attr(html, 'href', 'style.css'), sha12('build/style.css')); |
| 34 | assert.equal(attr(html, 'src', 'framework.js'), sha12('build/framework.js')); |
| 35 | assert.equal(attr(html, 'src', 'game.js'), sha12('build/game.js')); |
| 36 | assert.doesNotMatch(html, /(?:href|src)="(?:style\.css|framework\.js|game\.js)"/); |
| 37 | }); |
| 38 | |
| 39 | it('is idempotent and follows a catalog or JS change', () => |
| 40 | { |
| 41 | const script = fs.readFileSync(path.join(root, 'src/build.sh'), 'utf8'); |
| 42 | assert.match(script, /asset_hash/); |
| 43 | assert.match(script, /game\.js\?v=/); |
| 44 | assert.match(script, /i18n\.json/); |
| 45 | // Restamp in a temp copy so we do not race other tests touching build/. |
| 46 | const tmp = fs.mkdtempSync(path.join(require('node:os').tmpdir(), 'meteors-stamp-')); |
| 47 | const index = path.join(tmp, 'index.html'); |
| 48 | fs.writeFileSync(index, '<link rel="stylesheet" href="style.css">\n' |
| 49 | + '<script src="framework.js"></script>\n' |
| 50 | + '<script src="game.js"></script>\n'); |
| 51 | const envScript = `sha256sum() { sha256sum "$@"; } |
| 52 | asset_hash() { sha256sum "$1" | cut -c1-12; } |
| 53 | INDEX="${index}" |
| 54 | GAME_HASH="aaaaaaaaaaaa" |
| 55 | CSS_HASH="bbbbbbbbbbbb" |
| 56 | FW_HASH="cccccccccccc" |
| 57 | sed -i -E "s/href=\\"style\\.css(\\?v=[^\\"]*)?\\"/href=\\"style.css?v=\${CSS_HASH}\\"/" "$INDEX" |
| 58 | sed -i -E "s/src=\\"framework\\.js(\\?v=[^\\"]*)?\\"/src=\\"framework.js?v=\${FW_HASH}\\"/" "$INDEX" |
| 59 | sed -i -E "s/src=\\"game\\.js(\\?v=[^\\"]*)?\\"/src=\\"game.js?v=\${GAME_HASH}\\"/" "$INDEX" |
| 60 | `; |
| 61 | // Drive the same sed the build script uses, twice. |
| 62 | const run = (game, css, fw) => |
| 63 | { |
| 64 | const r = spawnSync('bash', ['-lc', ` |
| 65 | INDEX=${JSON.stringify(index)} |
| 66 | sed -i -E "s/href=\\"style\\.css(\\?v=[^\\"]*)?\\"/href=\\"style.css?v=${css}\\"/" "$INDEX" |
| 67 | sed -i -E "s/src=\\"framework\\.js(\\?v=[^\\"]*)?\\"/src=\\"framework.js?v=${fw}\\"/" "$INDEX" |
| 68 | sed -i -E "s/src=\\"game\\.js(\\?v=[^\\"]*)?\\"/src=\\"game.js?v=${game}\\"/" "$INDEX" |
| 69 | `], { encoding: 'utf8' }); |
| 70 | assert.equal(r.status, 0, r.stderr); |
| 71 | return fs.readFileSync(index, 'utf8'); |
| 72 | }; |
| 73 | const first = run('aaaaaaaaaaaa', 'bbbbbbbbbbbb', 'cccccccccccc'); |
| 74 | assert.match(first, /style\.css\?v=bbbbbbbbbbbb/); |
| 75 | assert.match(first, /framework\.js\?v=cccccccccccc/); |
| 76 | assert.match(first, /game\.js\?v=aaaaaaaaaaaa/); |
| 77 | const second = run('dddddddddddd', 'bbbbbbbbbbbb', 'cccccccccccc'); |
| 78 | assert.match(second, /game\.js\?v=dddddddddddd/); |
| 79 | assert.doesNotMatch(second, /game\.js\?v=aaaaaaaaaaaa/); |
| 80 | fs.rmSync(tmp, { recursive: true, force: true }); |
| 81 | }); |
| 82 | |
| 83 | it('sets Cache-Control on HTML (no-cache) and versioned static assets', () => |
| 84 | { |
| 85 | const conf = fs.readFileSync(htaccessPath, 'utf8'); |
| 86 | assert.match(conf, /FilesMatch "\\?\.\(html\)\$"/); |
| 87 | assert.match(conf, /Cache-Control "no-cache"/); |
| 88 | assert.match(conf, /FilesMatch "\\?\.\(js\|css\|json\)\$"/); |
| 89 | assert.match(conf, /Cache-Control "public, max-age=31536000, immutable"/); |
| 90 | const router = fs.readFileSync(path.join(root, 'server/router.php'), 'utf8'); |
| 91 | assert.match(router, /Cache-Control: no-cache/); |
| 92 | }); |
| 93 | }); |
projects-timfalken-com meteors