From 91e9e4b0682363544dc0f9a18fdf9c1afe562f1a Mon Sep 17 00:00:00 2001 From: Nathan Downs Date: Wed, 29 May 2019 13:05:24 -0700 Subject: [PATCH] Generate sha384 for SRI; update script tags --- 2/scripts/create-dist | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/2/scripts/create-dist b/2/scripts/create-dist index afe6041c..dac6eb9a 100755 --- a/2/scripts/create-dist +++ b/2/scripts/create-dist @@ -4,12 +4,12 @@ https://github.com/twitter/twemoji/blob/gh-pages/LICENSE */ -var fs = require('fs'); -var path = require('path'); -var { spawnSync } = require('child_process'); +const fs = require('fs'); +const path = require('path'); +const { spawnSync } = require('child_process'); -function file(which) { - return path.join(__dirname, '..', which); +function file(...which) { + return path.join(__dirname, '..', ...which); } fs.writeFileSync( @@ -50,3 +50,20 @@ fs.writeFileSync( fs.readFileSync(file('twemoji.tmp.js')) ); fs.unlinkSync(file('twemoji.tmp.js')); + +const algorithm = 'sha384'; +const digest = spawnSync('openssl', ['dgst', `-${algorithm}`, '-binary', file('twemoji.min.js')]); +if (digest.status || digest.signal){ + throw new Error(digest.stderr.toString('utf8')); +} +const integrity = `integrity="${algorithm}-${digest.stdout.toString('base64')}"`; + +function updateIntegrity(filename) { + fs.writeFileSync( + filename, + fs.readFileSync(filename).toString('utf8').replace(/integrity="[^ ]*"/, integrity)); +} + +updateIntegrity(file('..', 'README.md')); +updateIntegrity(file('templates', 'preview.html')); +