move bundle signature to own file

Reviewed By: tadeuzagallo

Differential Revision: D3437491

fbshipit-source-id: c2e00932bacd799d547110c7214374c51ff0b830
This commit is contained in:
David Aurelio 2016-06-17 06:16:41 -07:00 committed by Facebook Github Bot 2
parent 49f20f4154
commit ae6afcf3f6
2 changed files with 24 additions and 14 deletions

View File

@ -9,7 +9,7 @@
'use strict';
const Promise = require('promise');
const hash = require('./hash');
const meta = require('./meta');
const writeFile = require('./writeFile');
function buildBundle(packagerClient, requestOptions) {
@ -37,15 +37,20 @@ function saveBundleAndMap(bundle, options, log) {
log('Writing bundle output to:', bundleOutput);
const code = hash.appendToString(codeWithMap.code, encoding);
const {code} = codeWithMap;
const writeBundle = writeFile(bundleOutput, code, encoding);
writeBundle.then(() => log('Done writing bundle output'));
const writeMetadata = writeFile(
bundleOutput + '.meta',
meta(code, encoding),
'binary');
Promise.all([writeBundle, writeMetadata])
.then(() => log('Done writing bundle output'));
if (sourcemapOutput) {
log('Writing sourcemap output to:', sourcemapOutput);
const writeMap = writeFile(sourcemapOutput, codeWithMap.map, null);
writeMap.then(() => log('Done writing sourcemap output'));
return Promise.all([writeBundle, writeMap]);
return Promise.all([writeBundle, writeMetadata, writeMap]);
} else {
return writeBundle;
}

View File

@ -10,14 +10,23 @@
const crypto = require('crypto');
const createHash = () => crypto.createHash('sha1');
const isUTF8 = encoding => /^utf-?8$/i.test(encoding);
exports.appendToString = (string, encoding) => {
const hash = createHash();
hash.update(string, encoding);
encoding = tryAsciiPromotion(string, encoding);
return string + formatSignature(encoding, hash);
const constantFor = encoding =>
/^ascii$/i.test(encoding) ? 1 :
isUTF8(encoding) ? 2 :
/^(?:utf-?16(?:le)?|ucs-?2)$/ ? 3 : 0;
module.exports = function(code, encoding) {
const hash = crypto.createHash('sha1');
hash.update(code, encoding);
const digest = hash.digest('binary');
const signature = Buffer(digest.length + 1);
signature.write(digest, 'binary');
signature.writeUInt8(
constantFor(tryAsciiPromotion(code, encoding)),
signature.length - 1);
return signature;
};
function tryAsciiPromotion(string, encoding) {
@ -27,7 +36,3 @@ function tryAsciiPromotion(string, encoding) {
}
return 'ascii';
}
function formatSignature(encoding, hash) {
return `/*${encoding}:${hash.digest('hex')}*/`;
}