UNPKG

1.47 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getSignedCodeVersion = getSignedCodeVersion;
7exports.signCode = signCode;
8exports.signCodeStream = signCodeStream;
9exports.verifySignedCode = verifySignedCode;
10
11var _md = _interopRequireDefault(require("md5"));
12
13function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
15const VERSION_COMMENT_RE = /\/\/ flow-typed version: (.*)$/;
16
17function getSignedCodeVersion(signedCode) {
18 const [_, versionComment] = signedCode.split('\n');
19 const versionMatches = versionComment.trim().match(VERSION_COMMENT_RE);
20
21 if (versionMatches == null) {
22 return null;
23 }
24
25 return versionMatches[1];
26}
27
28function signCode(code, version) {
29 const versionedCode = `// flow-typed version: ${version}\n\n${code}`;
30 const hash = (0, _md.default)(versionedCode);
31 return `// flow-typed signature: ${hash}\n${versionedCode}`;
32}
33
34function signCodeStream(version) {
35 return code => signCode(code, version);
36}
37
38const HASH_COMMENT_RE = /\/\/ flow-typed signature: (.*)$/;
39
40function verifySignedCode(signedCode) {
41 const signedCodeLines = signedCode.split('\n');
42 const [hashComment] = signedCodeLines;
43 const hashMatches = hashComment.trim().match(HASH_COMMENT_RE);
44
45 if (hashMatches == null) {
46 return false;
47 }
48
49 const [_, hash] = hashMatches;
50 const versionedCode = signedCodeLines.slice(1).join('\n');
51 return (0, _md.default)(versionedCode) === hash;
52}
\No newline at end of file