lib/producer.jsView |
---|
23 | 23 … | return str.replace(key, vars[key]) |
24 | 24 … | }, template) |
25 | 25 … | } |
26 | 26 … | |
| 27 … | +function getJson(file, cb) { |
| 28 … | + fs.readFile(file, 'utf8', function (err, data) { |
| 29 … | + if (err) return cb(err) |
| 30 … | + var obj |
| 31 … | + try { obj = JSON.parse(data) } |
| 32 … | + catch(e) { return cb(e) } |
| 33 … | + cb(null, obj) |
| 34 … | + }) |
| 35 … | +} |
| 36 … | + |
27 | 37 … | function addGzBlobs(ssb, file, cb) { |
28 | | - var idFile = file + '.gzids' |
29 | | - fs.readFile(idFile, 'utf8', function (err, contents) { |
30 | | - if (!err) return cb(null, contents.trim().split('\n')) |
31 | | - if (err.code !== 'ENOENT') return cb(err) |
| 38 … | + var linksFile = file + '.gzlinks' |
| 39 … | + getJson(linksFile, function (err, links) { |
| 40 … | + if (!err) return cb(null, links) |
| 41 … | + if (err.code !== 'ENOENT' && err.message !== 'SyntaxError') return cb(err) |
32 | 42 … | fs.createReadStream(file) |
33 | 43 … | .pipe(zlib.createGzip()) |
34 | | - .pipe(ssb.addBlobs(function (err, ids) { |
| 44 … | + .pipe(ssb.addBlobs(function (err, links) { |
35 | 45 … | if (err) return cb(err) |
36 | | - fs.writeFile(idFile, ids.join('\n')+'\n', function (err) { |
| 46 … | + fs.writeFile(linksFile, JSON.stringify(links, null, 2), function (err) { |
37 | 47 … | if (err) console.trace(err) |
38 | | - cb(null, ids) |
| 48 … | + cb(null, links) |
39 | 49 … | }) |
40 | 50 … | })) |
41 | 51 … | }) |
42 | 52 … | } |
43 | 53 … | |
44 | | -function caseNodeBlobs(ids, pattern) { |
45 | | - return ' ' + pattern + ') set -- \\\n ' + ids.map(function (id) { |
46 | | - return "'" + id + "'" |
| 54 … | +function caseNodeBlobs(links, pattern) { |
| 55 … | + return ' ' + pattern + ') set -- \\\n ' + links.map(function (link) { |
| 56 … | + return "'" + link.link + "'" |
47 | 57 … | }).join(' \\\n ') + ';;\n' |
48 | 58 … | } |
49 | 59 … | |
50 | | -function switchNodeBlobs(nodeBlobIds, targets) { |
| 60 … | +function switchNodeBlobs(nodeBlobs, targets) { |
51 | 61 … | return Object.keys(targets).map(function (t) { |
52 | 62 … | var target = targets[t] |
53 | | - var ids = nodeBlobIds[target.platform + '-' + target.arch] |
54 | | - if (!ids) throw new Error('target without node blob ids: ' + t) |
| 63 … | + var links = nodeBlobs[target.platform + '-' + target.arch] |
| 64 … | + if (!links) throw new Error('target without node blobs: ' + t) |
55 | 65 … | var system = |
56 | 66 … | target.platform === 'linux' ? 'Linux' : |
57 | 67 … | target.platform === 'macos' ? 'Darwin*' : |
58 | 68 … | null |
64 | 74 … | null |
65 | 75 … | if (!system) throw new Error('Unknown system: ' + target.platform) |
66 | 76 … | if (!machine) throw new Error('Unknown machine: ' + target.arch) |
67 | 77 … | var pattern = system + '\\ ' + machine |
68 | | - return caseNodeBlobs(ids, pattern) |
| 78 … | + return caseNodeBlobs(links, pattern) |
69 | 79 … | }).filter(Boolean).join('\n') |
70 | 80 … | } |
71 | 81 … | |
72 | 82 … | export default function ({ backpack, bakes, slash, targets, fabricator, output, binName, ssb }) { |
74 | 84 … | if (!Buffer.alloc) { |
75 | 85 … | throw wasReported('Your node.js does not have Buffer.alloc. Please upgrade!'); |
76 | 86 … | } |
77 | 87 … | |
78 | | - var nodeBlobIds = {} |
79 | | - var preludeBlobIds, payloadBlobIds, vfsBlobIds, installJsBlobId |
| 88 … | + var nodeBlobs = {} |
| 89 … | + var preludeBlobs, payloadBlobs, vfsBlobs, installJsBlobId |
80 | 90 … | var waiting = 3 |
81 | 91 … | |
82 | 92 … | let { prelude, entrypoint, otherEntrypoints, stripes } = backpack; |
83 | 93 … | entrypoint = snapshotify(entrypoint, slash); |
102 | 112 … | } |
103 | 113 … | |
104 | 114 … | for (const target of targets) { |
105 | 115 … | waiting++ |
106 | | - addGzBlobs(ssb, target.binaryPath, function (err, ids) { |
| 116 … | + addGzBlobs(ssb, target.binaryPath, function (err, links) { |
107 | 117 … | if (err) return reject(err) |
108 | | - nodeBlobIds[target.platform + '-' + target.arch] = ids |
| 118 … | + nodeBlobs[target.platform + '-' + target.arch] = links |
109 | 119 … | if (!--waiting) next() |
110 | 120 … | }) |
111 | 121 … | } |
112 | 122 … | |
165 | 175 … | |
166 | 176 … | var blobGroup = stripe.blobGroup |
167 | 177 … | if (blobGroup) { |
168 | 178 … | var readFile = fs.createReadStream(stripe.file) |
169 | | - return readFile.pipe(ssb.addBlobs(function (err, ids) { |
| 179 … | + return readFile.pipe(ssb.addBlobs(function (err, links) { |
170 | 180 … | if (err) return cb(err) |
171 | 181 … | var snap = snapshotify(stripe.snap, slash); |
172 | 182 … | var blobs = fileBlobs[blobGroup] || (fileBlobs[blobGroup] = {}) |
173 | | - blobs[snap] = ids |
| 183 … | + blobs[snap] = links |
174 | 184 … | |
175 | 185 … | stripe.skip = true |
176 | 186 … | cb(null, intoStream(Buffer.alloc(0))) |
177 | 187 … | })) |
191 | 201 … | ).on('error', (error) => { |
192 | 202 … | reject(error); |
193 | 203 … | }); |
194 | 204 … | |
195 | | - function onAddInstallJsBlobs(err, ids) { |
| 205 … | + function onAddInstallJsBlobs(err, links) { |
196 | 206 … | if (err) return reject(err) |
197 | | - if (ids.length > 1) { |
| 207 … | + if (links.length > 1) { |
198 | 208 … | return reject(new Error('install.js must fit in one blob')) |
199 | 209 … | } |
200 | | - installJsBlobId = ids |
| 210 … | + installJsBlobId = links[0].link |
201 | 211 … | if (!--waiting) next() |
202 | 212 … | } |
203 | 213 … | |
204 | | - var writable = ssb.addBlobs(function (err, ids) { |
| 214 … | + var writable = ssb.addBlobs(function (err, links) { |
205 | 215 … | if (err) return reject(err) |
206 | | - preludeBlobIds = ids |
| 216 … | + preludeBlobs = links |
207 | 217 … | if (!--waiting) next() |
208 | 218 … | }) |
209 | 219 … | writable.write(prelude) |
210 | 220 … | writable.end() |
211 | 221 … | |
212 | 222 … | |
213 | | - function onAddPayloadBlobs(err, ids) { |
| 223 … | + function onAddPayloadBlobs(err, links) { |
214 | 224 … | if (err) return reject(err) |
215 | | - payloadBlobIds = ids |
216 | | - var writable = ssb.addBlobs(function (err, ids) { |
217 | | - vfsBlobIds = ids |
| 225 … | + payloadBlobs = links |
| 226 … | + var writable = ssb.addBlobs(function (err, links) { |
| 227 … | + vfsBlobs = links |
218 | 228 … | if (err) return reject(err) |
219 | 229 … | if (!--waiting) next() |
220 | 230 … | }) |
221 | 231 … | writable.write(JSON.stringify(vfs)) |
226 | 236 … | var installSh = path.join(__dirname, '../prelude/install.sh') |
227 | 237 … | var installScriptTemplate = fs.readFileSync(installSh, 'utf8') |
228 | 238 … | var settings = { |
229 | 239 … | '%INSTALL_JS_BLOB%': installJsBlobId, |
230 | | - '%SWITCH_NODE_BLOBS%': switchNodeBlobs(nodeBlobIds, targets), |
| 240 … | + '%SWITCH_NODE_BLOBS%': switchNodeBlobs(nodeBlobs, targets), |
231 | 241 … | '%SETTINGS%': JSON.stringify({ |
232 | | - preludeBlobs: preludeBlobIds, |
233 | | - payloadBlobs: payloadBlobIds, |
234 | | - vfsBlobs: vfsBlobIds, |
| 242 … | + preludeBlobs: preludeBlobs, |
| 243 … | + payloadBlobs: payloadBlobs, |
| 244 … | + vfsBlobs: vfsBlobs, |
235 | 245 … | binName: binName, |
236 | 246 … | entrypoint: entrypoint, |
237 | 247 … | otherEntrypoints: otherEntrypoints, |
238 | 248 … | bakes: bakes, |