flow-typed
Version:
A repository of high quality flow type definitions
52 lines (39 loc) • 1.47 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSignedCodeVersion = getSignedCodeVersion;
exports.signCode = signCode;
exports.signCodeStream = signCodeStream;
exports.verifySignedCode = verifySignedCode;
var _md = _interopRequireDefault(require("md5"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const VERSION_COMMENT_RE = /\/\/ flow-typed version: (.*)$/;
function getSignedCodeVersion(signedCode) {
const [_, versionComment] = signedCode.split('\n');
const versionMatches = versionComment.trim().match(VERSION_COMMENT_RE);
if (versionMatches == null) {
return null;
}
return versionMatches[1];
}
function signCode(code, version) {
const versionedCode = `// flow-typed version: ${version}\n\n${code}`;
const hash = (0, _md.default)(versionedCode);
return `// flow-typed signature: ${hash}\n${versionedCode}`;
}
function signCodeStream(version) {
return code => signCode(code, version);
}
const HASH_COMMENT_RE = /\/\/ flow-typed signature: (.*)$/;
function verifySignedCode(signedCode) {
const signedCodeLines = signedCode.split('\n');
const [hashComment] = signedCodeLines;
const hashMatches = hashComment.trim().match(HASH_COMMENT_RE);
if (hashMatches == null) {
return false;
}
const [_, hash] = hashMatches;
const versionedCode = signedCodeLines.slice(1).join('\n');
return (0, _md.default)(versionedCode) === hash;
}
;