Commit e6f24aead1fb2f4f11eccbdecbd008ab8fbe30af
Make bootstrap.pack deterministic
cel authored on 9/25/2017, 10:51:29 PMHenry committed on 11/30/2017, 9:46:32 PM
Parent: 53a41420bb71793b685b19ba128638d8bfb59b95
Files changed
plugins/bootstrap.js | changed |
plugins/bootstrap.js | ||
---|---|---|
@@ -1,10 +1,40 @@ | ||
1 … | +var fs = require('fs') | |
1 | 2 … | var zlib = require('zlib') |
2 | 3 … | var pull = require('pull-stream') |
3 | 4 … | var tar = require('tar-fs') |
4 | 5 … | var toPull = require('stream-to-pull-stream') |
5 | 6 … | var path = require('path') |
7 … | +var pkg = require('../package.json') | |
8 … | +var Readable = require('stream').Readable | |
6 | 9 … | |
10 … | +var normalizedPkgJsonBuf = Buffer.from(JSON.stringify({ | |
11 … | + name: pkg.name, | |
12 … | + description: pkg.description, | |
13 … | + version: pkg.version, | |
14 … | + homepage: pkg.homepage, | |
15 … | + repository: pkg.repository, | |
16 … | + dependencies: pkg.dependencies, | |
17 … | + devDependencies: pkg.devDependencies, | |
18 … | + bin: pkg.bin, | |
19 … | + scripts: pkg.scripts, | |
20 … | + author: typeof pkg.author === 'string' ? pkg.author : | |
21 … | + pkg.author.name + ' <' + pkg.author.email + '>', | |
22 … | + license: pkg.license, | |
23 … | + files: pkg.files | |
24 … | +}, 0, 2) + '\n') | |
25 … | + | |
26 … | +function streamOnce(data) { | |
27 … | + var done | |
28 … | + return new Readable({ | |
29 … | + read: function (size) { | |
30 … | + if (done) return this.push(null) | |
31 … | + done = true | |
32 … | + this.push(data) | |
33 … | + } | |
34 … | + }) | |
35 … | +} | |
36 … | + | |
7 | 37 … | exports.name = 'bootstrap' |
8 | 38 … | exports.version = '1.0.0' |
9 | 39 … | exports.manifest = { |
10 | 40 … | getPackageLock: 'sync', |
@@ -15,17 +45,32 @@ | ||
15 | 45 … | getPackageLock: function () { |
16 | 46 … | return require('../package-lock.json') |
17 | 47 … | }, |
18 | 48 … | pack: function () { |
19 | - var pkg = require('../package.json') | |
20 | 49 … | var files = pkg.files |
21 | 50 … | if (!files) throw new Error('package.json missing \'files\' property') |
22 | 51 … | var gzip = zlib.createGzip() |
23 | 52 … | tar.pack(path.join(__dirname, '../'), { |
24 | 53 … | entries: files.slice(), |
25 | 54 … | map: function (header) { |
26 | 55 … | header.name = 'package/' + header.name |
56 … | + // deterministicify | |
57 … | + header.uid = header.gid = 0 | |
58 … | + header.uname = header.gname = null | |
59 … | + header.mtime = new Date(0) | |
60 … | + if (header.name === 'package/package.json') { | |
61 … | + header.size = normalizedPkgJsonBuf.length | |
62 … | + } | |
27 | 63 … | return header |
64 … | + }, | |
65 … | + mapStream: function (stream, header) { | |
66 … | + if (header.name === 'package/package.json') { | |
67 … | + // npm changes package.json during install. | |
68 … | + // change it back, to make things more deterministic | |
69 … | + stream.resume() | |
70 … | + return streamOnce(normalizedPkgJsonBuf) | |
71 … | + } | |
72 … | + return stream | |
28 | 73 … | } |
29 | 74 … | }).pipe(gzip) |
30 | 75 … | return toPull.source(gzip) |
31 | 76 … | } |
Built with git-ssb-web