From 0c995a9c4a8f3bc4c9e77207d70f92b4944fd744 Mon Sep 17 00:00:00 2001 From: Volodymyr Agafonkin Date: Thu, 22 Sep 2022 22:40:03 +0300 Subject: [PATCH] Improve integrity generation for releases (#8459) --- RELEASE.md | 3 +-- build/integrity.js | 44 +++++++++++++++++++++++++------------------- package.json | 6 +++--- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 7dce09cc6..30cb40b9b 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -15,9 +15,8 @@ - [ ] Make a new branch for the update - [ ] Write a blog post about the new release and put it in `/docs/_posts` - [ ] If necessary to preserve previous version's docs, rename `docs/reference.html` to `docs/reference-X.Y.Z.html` and add it to the list in `docs/reference-versions.html` -- [ ] Run `npm run docs` to generate the new `docs/reference.html` +- [ ] Run `npm run docs` to generate the new `docs/reference.html` and update integrity hashes in `docs/_config.yml` - [ ] Add the version number to the first paragraph in `docs/reference.html` -- [ ] Run `npm run integrity` and make sure `docs/_config.yml` is updated with new hashes - [ ] Update link to latest release in `docs/download.md` - [ ] Update the announcement section in `docs/index.html` - [ ] Commit all the changes and submit a PR for someone to review diff --git a/build/integrity.js b/build/integrity.js index 974976cf4..201610aa9 100755 --- a/build/integrity.js +++ b/build/integrity.js @@ -1,28 +1,34 @@ // This script calculates the integrity hashes of the files in dist/ , and // **overwrites** the values in the documentation. -var ssri = require('ssri'); -var fs = require('fs'); -var version = require('../package.json').version; +const ssri = require('ssri'); +const fs = require('fs'); +const https = require('https'); +const version = require('../package.json').version; -const integritySrc = ssri.fromData(fs.readFileSync('dist/leaflet-src.js')); -const integrityUglified = ssri.fromData(fs.readFileSync('dist/leaflet.js')); -const integrityCss = ssri.fromData(fs.readFileSync('dist/leaflet.css')); +const getIntegrity = async (path) => new Promise((resolve) => { + https.get(`https://unpkg.com/leaflet@${version}/dist/${path}`, (res) => { + ssri.fromStream(res, {algorithms: ['sha256']}).then(integrity => resolve(integrity.toString())); + }); +}); +(async () => { + const integrityUglified = await getIntegrity('leaflet.js'); + const integritySrc = await getIntegrity('leaflet-src.js'); + const integrityCss = await getIntegrity('leaflet.css'); -console.log('Integrity hashes for ', version, ':'); -console.log('dist/leaflet-src.js: ', integritySrc.toString()); -console.log('dist/leaflet.js: ', integrityUglified.toString()); -console.log('dist/leaflet.css: ', integrityCss.toString()); + console.log(`Integrity hashes for ${version}:`); + console.log(`dist/leaflet.js: ${integrityUglified}`); + console.log(`dist/leaflet-src.js: ${integritySrc}`); + console.log(`dist/leaflet.css: ${integrityCss}`); -var docConfig = fs.readFileSync('docs/_config.yml').toString(); + let docConfig = fs.readFileSync('docs/_config.yml', 'utf8'); -docConfig = docConfig - .replace(/latest_leaflet_version:.*/, 'latest_leaflet_version: ' + version) - .replace(/integrity_hash_source:.*/, 'integrity_hash_source: "' + integritySrc.toString() + '"') - .replace(/integrity_hash_uglified:.*/, 'integrity_hash_uglified: "' + integrityUglified.toString() + '"') - .replace(/integrity_hash_css:.*/, 'integrity_hash_css: "' + integrityCss.toString() + '"'); + docConfig = docConfig + .replace(/latest_leaflet_version:.*/, 'latest_leaflet_version: ' + version) + .replace(/integrity_hash_source:.*/, 'integrity_hash_source: "' + integritySrc + '"') + .replace(/integrity_hash_uglified:.*/, 'integrity_hash_uglified: "' + integrityUglified + '"') + .replace(/integrity_hash_css:.*/, 'integrity_hash_css: "' + integrityCss + '"'); -// console.log('New jekyll docs config: \n', docConfig); - -fs.writeFileSync('docs/_config.yml', docConfig); + fs.writeFileSync('docs/_config.yml', docConfig); +})(); diff --git a/package.json b/package.json index 469fbaac7..778a071d5 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "CHANGELOG.md" ], "scripts": { - "docs": "node ./build/docs.js", + "docs": "node ./build/docs.js && node ./build/integrity.js", "test": "karma start ./spec/karma.conf.js", "build": "npm run rollup && npm run uglify", "lint": "eslint .", @@ -50,7 +50,6 @@ "rollup": "rollup -c build/rollup-config.js", "watch": "rollup -w -c build/rollup-config.js", "uglify": "uglifyjs dist/leaflet-src.js -c -m -o dist/leaflet.js --source-map filename=dist/leaflet.js.map --source-map content=dist/leaflet-src.js.map --source-map url=leaflet.js.map --comments", - "integrity": "NODE_ENV=release npm run build && node ./build/integrity.js", "bundlemon": "bundlemon --subProject js --defaultCompression none && bundlemon --subProject js-gzip --defaultCompression gzip", "serve": "cd docs && bundle exec jekyll serve", "prepare": "husky install" @@ -65,7 +64,8 @@ "docs/examples/map-panes/eu-countries.js", "docs/examples/extending/extending-2-layers.md", "docs/_posts/2012*", - "docs/_site" + "docs/_site", + "build/integrity.js" ], "root": true, "globals": {