mirror of
https://github.com/plausible/analytics.git
synced 2024-12-23 17:44:43 +03:00
37116a2b12
* move util function to util module * move playwright config file to tracker dir root * update Playwright + add gitignores * Playwright: enable reuseExistingServer (non-CI env) * store tracker src copies to avoid re-compilation in dev env * test pageleave on simple navigation * fix test util fn * rename local_test npm script * make test util able to expect multiple requests * test pageleaves in SPAs * test pageleave with manual URL * test pageleave not sent in manual when pageview not triggered * extend util fn to refute event requests * test pageleaves not sent in excluded hash pages * store hashes instead of file copies to detect /tracker/src changes * drop async * speed up test suite
52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const crypto = require('crypto');
|
|
|
|
const LAST_HASH_FILEPATH = path.join(__dirname, './last-hash.txt')
|
|
|
|
// Re-compilation is only required if any of these files have been changed.
|
|
const COMPILE_DEPENDENCIES = [
|
|
path.join(__dirname, '../compile.js'),
|
|
path.join(__dirname, '../src/plausible.js'),
|
|
path.join(__dirname, '../src/customEvents.js')
|
|
]
|
|
|
|
function currentHash() {
|
|
const combinedHash = crypto.createHash('sha256');
|
|
|
|
for (const filePath of COMPILE_DEPENDENCIES) {
|
|
try {
|
|
const fileContent = fs.readFileSync(filePath);
|
|
const fileHash = crypto.createHash('sha256').update(fileContent).digest();
|
|
combinedHash.update(fileHash);
|
|
} catch (error) {
|
|
throw new Error(`Failed to read or hash ${filePath}: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
return combinedHash.digest('hex');
|
|
}
|
|
|
|
function lastHash() {
|
|
if (fs.existsSync(LAST_HASH_FILEPATH)) {
|
|
return fs.readFileSync(LAST_HASH_FILEPATH).toString()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns a boolean indicating whether the tracker compilation can be skipped.
|
|
* Every time this function gets executed, the hash of the tracker dependencies
|
|
* will be updated. Compilation can be skipped if the hash hasn't changed since
|
|
* the last execution.
|
|
*/
|
|
exports.canSkipCompile = function() {
|
|
const current = currentHash()
|
|
const last = lastHash()
|
|
|
|
if (current === last) {
|
|
return true
|
|
} else {
|
|
fs.writeFileSync(LAST_HASH_FILEPATH, current)
|
|
return false
|
|
}
|
|
} |