UNPKG

app-builder-lib

Version:
153 lines 18.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildBlockMap = buildBlockMap; const crypto_1 = require("crypto"); const fs_1 = require("fs"); const promises_1 = require("fs/promises"); const zlib = require("zlib"); const blake2_js_1 = require("@noble/hashes/blake2.js"); // Rabin fingerprinting constants from github.com/aclements/go-rabin: // Poly64 = 0xbfe6b8a5bf378d83 (degree-63 irreducible polynomial over GF(2)) // Window = 64 bytes, Avg = 16 KB, Min = 8 KB, Max = 32 KB // Boundary condition: (hash & (AVG-1)) === (AVG-1), i.e. all mask bits set. // // The tables below are precomputed offline (avoiding BigInt in production): // PUSH[i] = (i * x^63 + (i * x^63 mod p)) truncated to 64 bits // POP[i] = (i * x^(63*8)) mod p // Each stored as signed Int32 hi/lo pair; hash = hi*2^32 + lo (unsigned 64-bit). // prettier-ignore const PUSH_HI = new Int32Array([0, -1075398491, 2144170315, -1070872082, 1081891379, -6626666, 1068575608, -2141744163, 1058933955, -2131184538, 1087602056, -13253331, 2137151216, -1065030059, 11478971, -1085694178, 2117867910, -1042636509, 32598221, -1105735576, 1044399029, -2119763184, 1099772670, -26506661, 1092752709, -20664864, 1055876110, -2130060117, 22957942, -1095174189, 2123578941, -1049262440, 1134463913, -59231476, 1012136674, -2085273017, 65196442, -1140561601, 2083496145, -1010231180, 2088798058, -1016709169, 55440929, -1129623932, 1023204697, -2095421956, 1127328786, -53013321, 1034064431, -2109461878, 1114628964, -41329727, 2111752220, -1036488519, 34847063, -1108016654, 45915884, -1118165431, 2104618919, -1030269182, 1119929567, -47809414, 1024308628, -2098524879, 953921527, -2026039470, 1192681148, -118462951, 2024273348, -952021663, 124421263, -1198769110, 130392884, -1205658735, 2013844095, -940676390, 1203370247, -127975006, 947161164, -2020462359, 1189590641, -117371180, 959104826, -2033418337, 110881858, -1182968601, 2035719433, -961534548, 2046409394, -971046377, 104123385, -1177390244, 965075073, -2040309724, 1179161034, -106026641, 2068128862, -995910405, 76043541, -1150358096, 993623661, -2065709368, 1156845350, -82659453, 1146824861, -71462856, 999709142, -2072977037, 69694126, -1144927733, 2078933989, -1005798592, 91831768, -1163950723, 2058636435, -984419274, 1157982187, -85729458, 986191520, -2060538363, 979841307, -2055108162, 1168785488, -95618827, 2048617256, -973220979, 97917539, -1171217722, 1907843054, -833493173, 242888357, -1315138048, 835388893, -1909605000, 1309045910, -236925901, 1319719725, -246420600, 828645990, -1904043325, 248842526, -1322012229, 1897429077, -822165264, 260785768, -1334968627, 1883649827, -811561082, 1341594715, -267279106, 809135376, -1881352779, 815090347, -1888226802, 1331182560, -255950011, 1894322328, -821057475, 254042579, -1329407626, 841602119, -1915786014, 1306830092, -234742359, 1918209652, -843893039, 228130623, -1300346982, 221763716, -1294901215, 1929030095, -853798550, 1296794295, -223528430, 847705084, -1923069095, 1276497345, -202148508, 869842058, -1942092753, 208246770, -1282461865, 1940186809, -868065764, 1930150146, -856852057, 214347849, -1289746196, 863476529, -1936645228, 1287318138, -212053281, 1231876121, -158709572, 916554066, -1991820809, 152087082, -1225387377, 1994251105, -918854716, 1987247322, -913030017, 163548561, -1235667660, 906929897, -1981276596, 1237571490, -165318905, 928049567, -2001317574, 1218287828, -142925711, 1999418284, -926282999, 149013223, -1224246718, 139388252, -1213702663, 2005111831, -932893518, 1211285359, -137099318, 939511332, -2011597183, 183663536, -1256930539, 1967065851, -891702690, 1250828675, -177694426, 893604040, -1968838547, 904689523, -1979002922, 1243678264, -171458915, 1972383040, -898198043, 173890571, -1245977426, 1959682614, -886515053, 184750973, -1260016680, 884094981, -1957396320, 1266633038, -191237653, 1271951093, -197732784, 874323902, -1946441957, 195835078, -1270182813, 1952531853, -880280280]); // prettier-ignore const PUSH_LO = new Int32Array([0, -1086878333, 2121210630, -1051158907, 1139391375, -52546036, 1032233097, -2102317814, 954530461, -2016184546, 1183572379, -105092072, 2064466194, -1002779503, 90331668, -1168779369, 1909060922, -822183751, 262598204, -1332649025, 840978101, -1927822538, 1280267699, -210184144, 1227689895, -166034908, 927077537, -2005559006, 180663336, -1242351189, 1957408558, -878959955, 1554278391, -476845452, 583761137, -1644367502, 525196408, -1602596357, 1629669246, -569029891, 1681956202, -612953879, 439322220, -1525150737, 665396965, -1734431898, 1506164195, -420368288, 762155725, -1839587506, 1392675275, -332069816, 1854155074, -776756031, 283849284, -1344487481, 361326672, -1430329901, 1810264918, -724435243, 1449186271, -380150180, 672123097, -1757919910, 109003373, -1186410514, 2014337387, -953690904, 1167522274, -90082207, 1006232292, -2066845849, 1050392816, -2119420557, 1089774582, -3985803, 2104623999, -1035628804, 52238457, -1138059782, 2008463191, -931054892, 165260369, -1225907758, 878644440, -1956085413, 1244665822, -184051107, 1330793930, -261767095, 826103500, -1911891121, 213644869, -1282638906, 1926556995, -840736576, 1524311450, -437475303, 615792284, -1685867745, 422747669, -1509616746, 1734182163, -664139632, 1648353031, -586657148, 475055105, -1553512062, 567698568, -1629361909, 1605992334, -527502835, 722653344, -1809490653, 1434307494, -364231131, 1761307439, -674437460, 378826793, -1448870486, 334899773, -1396594754, 1838756155, -760300360, 1344246194, -282584015, 779127476, -1857616073, 218006746, -1278670503, 1922146268, -844754337, 1326923605, -266292522, 829957203, -1907381808, 874151495, -1959922748, 1249207617, -180164414, 2012464584, -926660533, 161275598, -1230286003, 2100785632, -1040122781, 56126182, -1133516955, 1054788207, -2115418132, 1085395305, -7971606, 1171491709, -85719298, 1002213499, -2071257608, 104476914, -1190281871, 2018847732, -949836169, 1348133677, -278040914, 775288875, -1862109784, 330520738, -1400580831, 1843151780, -756298201, 1757288880, -678849485, 382796470, -1444507851, 727163455, -1805635652, 1429780793, -368102214, 563287575, -1633379436, 1610353937, -523534190, 1652207000, -582148069, 471185054, -1558037731, 427289738, -1505730295, 1729689484, -667977201, 1520326405, -441853306, 619793411, -1681473152, 176277175, -1246344396, 1961811377, -874950606, 1231584568, -161484613, 923231806, -2010059843, 845495338, -1923960407, 1275733804, -214062417, 1905035173, -826602970, 266575011, -1328279264, 2068329357, -998261234, 86452363, -1173314296, 950110210, -2020211327, 1187943172, -101114233, 1135397136, -56933229, 1036243478, -2097913963, 4551327, -1082982628, 2116708761, -1055005670, 1445306688, -384684861, 675985990, -1753401403, 365697743, -1426352308, 1805844937, -728462262, 1858165725, -772352418, 279855323, -1348874920, 757653586, -1843434031, 1397226324, -328173865, 669799546, -1730422279, 1501777788, -424361217, 1678110709, -617454986, 443217139, -1520600720, 520662759, -1606474908, 1634186721, -565168030, 1558254952, -472475413, 579735150, -1648786451]); // prettier-ignore const POP_HI = new Int32Array([0, 2138251555, 1090583266, 1047780289, 1038568800, 1117023299, 2095560578, 60069537, 2077137601, 79543266, 986603555, 1170030848, 1177147297, 962262658, 120139075, 2019167328, 1216056615, 923315204, 159086533, 1980258022, 1973207111, 183492964, 882653861, 1273961350, 867506150, 1288058565, 1924525316, 231132199, 240278150, 1898019749, 1330815076, 807502151, 789684970, 1348633545, 1846630408, 291666219, 318173066, 1837485737, 1408636264, 746927179, 1423915051, 732698888, 366985929, 1789715434, 1765307723, 374035560, 674795433, 1464577674, 1735012301, 404295406, 644535599, 1494873100, 1519215277, 637420430, 462264399, 1694415212, 480556300, 1675072559, 1571049454, 584543949, 558102636, 1580259663, 1615004302, 523248557, 1579369941, 558916854, 522436407, 1615896084, 1673791669, 481765782, 583332439, 1572328308, 636346132, 1520369207, 1693263350, 463340757, 403346036, 1736029015, 1493854358, 645482933, 374917362, 1764485585, 1465397776, 673911603, 733971858, 1422697649, 1790934896, 365715027, 1838567987, 317027088, 748071121, 1407551986, 1349590867, 788676208, 292677041, 1845675154, 1899161407, 239191580, 808590813, 1329675518, 1289071199, 866552700, 232083645, 1923510686, 184319486, 1972329693, 1274840860, 881829439, 924528798, 1214779837, 1981532796, 157870943, 961112600, 1178225467, 2018087162, 121287129, 78522232, 2078082651, 1169087898, 987626681, 1116205273, 1039454714, 59181627, 2096376600, 2137046457, 1285274, 1046497115, 1091790456, 60830479, 2094207532, 1117833709, 1037297870, 1044872815, 1093951308, 2135393421, 3450286, 2020518350, 119376109, 963531564, 1176334863, 1166664878, 989513101, 76095052, 2079997807, 1272692264, 883465995, 182142154, 1973970409, 1983705928, 156226155, 926681514, 1213147273, 806692073, 1332086218, 1897258507, 241631016, 233990537, 1921075370, 1290965867, 864137800, 749834725, 1405268166, 1840343815, 314722852, 290905221, 1847983526, 1347823207, 790955844, 1467943716, 671885831, 377483718, 1762447589, 1788364356, 367748967, 731430054, 1424727429, 1690967234, 465124833, 634054176, 1522124547, 1496142242, 643723393, 405646144, 1734249059, 520390147, 1618454304, 1577352417, 561470914, 585354083, 1569778240, 1675833729, 479203490, 1568896730, 586176505, 478383160, 1676717339, 1617181626, 521607833, 560251224, 1578623099, 642640923, 1497287992, 1733105401, 406730714, 464167291, 1691975768, 1521114009, 635009722, 368638973, 1787550430, 1425539359, 730538044, 673167005, 1466734526, 1763658879, 376204636, 1849057596, 289751071, 792107998, 1346747133, 1406217308, 748817791, 315741886, 1839396765, 1922225200, 232912147, 865218258, 1289818097, 1333107024, 805746803, 242574258, 1896235665, 157044465, 1982820306, 1214034963, 925865264, 884671377, 1271407282, 1975253363, 180934736, 988371223, 1167751220, 2078909429, 77234902, 118363255, 2021471572, 1175383701, 964546486, 1093125078, 1045750517, 2570548, 2136217623, 2092994230, 62107541, 1036022868, 1119049079]); // prettier-ignore const POP_LO = new Int32Array([0, -1826189856, -1720019389, 173808547, -1916624635, 518270181, 347617094, -2019640666, 461718026, -2002530326, -2097163191, 299456937, -1773673713, 90271471, 255685964, -1675888468, -2009265769, 454788215, 289906644, -2107567564, 100640914, -1764153998, -1668989231, 262386481, -1816638563, 10405501, 180542942, -1713090498, 511371928, -1923324040, -2030011173, 338096443, -1353867603, 1013650253, 909576430, -1525578482, 579813288, -1314278840, -1141528085, 684925963, -1261904729, 669929799, 766659300, -1097545980, 956988834, -1439895486, -1603212319, 861300225, 661690170, -1269425446, -1106640519, 757894297, -1431161281, 966048735, 868786300, -1595003492, 1022743856, -1345103664, -1517337741, 917098131, -1321765835, 571603413, 676192886, -1150586986, 1587232090, -843206470, -941511911, 1422337785, -749101985, 1082069439, 1243810332, -653948932, 1159626576, -700910928, -597366509, 1329751283, -925049259, 1543132085, 1369851926, -1031748106, -693750579, 1166063917, 1339859598, -587583634, 1533318600, -935192536, -1038220405, 1362660971, -853313849, 1577450279, 1415176324, -947950236, 1088542658, -741909982, -644136575, 1253952609, -237591561, 1659907607, 1756116404, -74794924, 2081686258, -281899246, -445737807, 1984436561, -363601411, 2037738525, 1932097470, -535823778, 1737572600, -189281000, -18098501, 1842174811, 2045487712, -355653760, -527352797, 1941418435, -198636699, 1729070725, 1834196262, -25882426, 1651435626, -246913654, -82543063, 1748169673, -273921681, 2089469071, 1993793324, -437234996, 33683255, -1859683625, -1686412940, 140373140, -1883023822, 484826066, 381272177, -2053157487, 428127549, -1969080099, -2130828418, 332967582, -1807346632, 123771352, 222069371, -1642459237, -1975714144, 421163840, 323381475, -2141133565, 134111141, -1797729723, -1635464730, 228738054, -1850098518, 43987274, 147008233, -1679448311, 477830575, -1889693617, -2063496212, 371656204, -1387501158, 1047192698, 876019673, -1492094407, 546228383, -1280817793, -1175167268, 718459708, -1228330096, 636462704, 800308691, -1131073485, 990612117, -1473443979, -1569645354, 827822390, 628187149, -1235751443, -1140066738, 791509934, -1464614648, 999640296, 835278667, -1561339221, 1056186887, -1378701337, -1483819964, 883440036, -1288273150, 537923298, 709629249, -1184196447, 1553648237, -809744499, -975152082, 1455870414, -782734488, 1115612808, 1210252587, -620465973, 1193248871, -734460537, -563798492, 1296274372, -891475614, 1509663874, 1403502369, -1065274687, -727202822, 1199656474, 1306351033, -553920423, 1499816703, -901517537, -1071647556, 1396275548, -819822096, 1543769104, 1448613811, -981558701, 1121984757, -775508715, -610617674, 1220295510, -203991872, 1626462496, 1789772419, -108310685, 2115368389, -315394011, -412130426, 1951002214, -397273398, 2071239466, 1898479753, -502395543, 1703983055, -155829713, -51764852, 1875684460, 2078956887, -389230409, -493827308, 1907771124, -165086126, 1695445426, 1867672081, -59447311, 1617895261, -213282115, -116029154, 1781728510, -307380648, 2123051960, 1960257563, -403593733]); // Chunker parameters const RABIN_MIN = 8192; const RABIN_AVG = 16384; const RABIN_MAX = 32768; const RABIN_WINDOW = 64; // Boundary: all mask bits (14 bits) set in lo word. RABIN_AVG is a power of 2. const RABIN_HASH_MASK = RABIN_AVG - 1; // 0x3FFF function compress(data, format) { return format === "deflate" ? zlib.deflateRawSync(data, { level: 9 }) : zlib.gzipSync(data, { level: 9 }); } /** * Build a content-defined block map for `inFile` using Rabin fingerprinting. * * Files are processed via streaming (peak memory ≈ RABIN_MAX = 32 KB per chunk, * not the full file size), making this safe for large installers. * * - If `outFile` is omitted: compressed blockmap is appended to `inFile` * (used for NSIS web installer / AppImage embed); `blockMapSize` is returned. * - If `outFile` is provided: compressed blockmap is written to that file; * `blockMapSize` is not included in the result. * * Returned `sha512` is SHA-512 of the full file as it exists after the call. */ async function buildBlockMap(inFile, compressionFormat, outFile) { const fileHash = (0, crypto_1.createHash)("sha512"); const checksums = []; const sizes = []; let totalSize = 0; // Per-chunk Rabin state. Peak memory ≈ RABIN_MAX (32 KB) — not file size. const chunkBuf = Buffer.allocUnsafe(RABIN_MAX); let chunkN = 0; // bytes accumulated for current chunk let hi = 0; let lo = 0; const win = new Uint8Array(RABIN_WINDOW); // rolling window ring buffer let wpos = 0; function emitChunk() { checksums.push(Buffer.from((0, blake2_js_1.blake2b)(chunkBuf.subarray(0, chunkN), { dkLen: 18 })).toString("base64")); sizes.push(chunkN); chunkN = 0; hi = 0; lo = 0; win.fill(0); wpos = 0; } // Called once per byte; inlines the three-phase Rabin state machine: // skip (< MIN-WINDOW bytes) → prime (next WINDOW bytes) → scan (≥ MIN bytes) function processByte(b) { chunkBuf[chunkN++] = b; if (chunkN <= RABIN_MIN - RABIN_WINDOW) { // Skip phase: not close enough to MIN to start priming yet return; } if (chunkN <= RABIN_MIN) { // Prime phase: non-windowed hash update, filling the ring buffer win[wpos] = b; wpos = (wpos + 1) % RABIN_WINDOW; const top = (hi >>> 23) & 0xff; const nhi = ((hi << 8) | (lo >>> 24)) & 0x7fffffff; const nlo = ((lo << 8) | b) & 0xffffffff; hi = (nhi ^ PUSH_HI[top]) | 0; lo = (nlo ^ PUSH_LO[top]) | 0; // At exactly MIN: check whether the primed hash is already a boundary if (chunkN === RABIN_MIN && (lo & RABIN_HASH_MASK) === RABIN_HASH_MASK) { emitChunk(); } return; } // Scan phase: sliding window — pop oldest byte, push new byte const old = win[wpos]; win[wpos] = b; wpos = (wpos + 1) % RABIN_WINDOW; hi = (hi ^ POP_HI[old]) | 0; lo = (lo ^ POP_LO[old]) | 0; const top = (hi >>> 23) & 0xff; const nhi = ((hi << 8) | (lo >>> 24)) & 0x7fffffff; const nlo = ((lo << 8) | b) & 0xffffffff; hi = (nhi ^ PUSH_HI[top]) | 0; lo = (nlo ^ PUSH_LO[top]) | 0; if ((lo & RABIN_HASH_MASK) === RABIN_HASH_MASK || chunkN >= RABIN_MAX) { emitChunk(); } } // Stream-read the file, feeding SHA-512 and the Rabin chunker simultaneously await new Promise((resolve, reject) => { const rs = (0, fs_1.createReadStream)(inFile, { highWaterMark: 256 * 1024 }); rs.on("data", (chunk) => { const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); chunk = buf; fileHash.update(chunk); totalSize += chunk.length; for (let i = 0; i < chunk.length; i++) { processByte(chunk[i]); } }); rs.on("end", () => { if (chunkN > 0) { emitChunk(); } resolve(); }); rs.on("error", reject); }); const blockMap = { version: "2", files: [{ name: "file", offset: 0, checksums, sizes }], }; const compressed = compress(Buffer.from(JSON.stringify(blockMap)), compressionFormat); if (outFile == null || outFile === "") { // Append mode: write compressed blockmap + 4-byte size in one atomic call // to avoid a partial-write window between the two separate appendFile calls. const sizeHeader = Buffer.allocUnsafe(4); sizeHeader.writeUInt32BE(compressed.length, 0); const appended = Buffer.concat([compressed, sizeHeader]); await (0, promises_1.appendFile)(inFile, appended); fileHash.update(appended); return { size: totalSize + appended.length, sha512: fileHash.digest("base64"), blockMapSize: compressed.length, }; } await (0, promises_1.writeFile)(outFile, compressed); return { size: totalSize, sha512: fileHash.digest("base64"), }; } //# sourceMappingURL=blockmap.js.map