Skip to content

Commit 1c4e7f6

Browse files
0xGeorgiir0qs
authored andcommitted
migrate from CommonJS to ESM and update ipfs-unixfs-importer
1 parent f02ca19 commit 1c4e7f6

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
22
"private": true,
33
"name": "solc-bin",
4+
"type": "module",
45
"version": "0.0.0",
56
"description": "Current and historical (emscripten) binaries for Solidity",
67
"dependencies": {
78
"semver": "^7.3.5"
89
},
910
"devDependencies": {
1011
"ethereumjs-util": "^7.1.3",
11-
"ipfs-unixfs-importer": "^9.0.6",
12-
"ipld": "^0.30.2",
13-
"ipld-in-memory": "^8.0.0",
12+
"blockstore-core": "^2.0.2",
13+
"ipfs-unixfs-importer": "^11.0.1",
1414
"standard": "^16.0.4",
1515
"swarmhash": "^0.1.1"
1616
},
1717
"scripts": {
18-
"lint": "standard update",
19-
"update": "./update --max-files-per-batch 1",
18+
"lint": "standard update.mjs",
19+
"update": "node update.mjs --max-files-per-batch 1",
2020
"test": "git checkout -f && npm run update && git diff --exit-code"
2121
},
2222
"repository": {

update renamed to update.mjs

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
#!/usr/bin/env node
2-
3-
'use strict'
4-
5-
const fs = require('fs')
6-
const util = require('util')
7-
const path = require('path')
8-
const semver = require('semver')
9-
const ethUtil = require('ethereumjs-util')
10-
const ipfsImporter = require('ipfs-unixfs-importer')
11-
const IPLD = require('ipld')
12-
const inMemory = require('ipld-in-memory')
13-
const swarmhash = require('swarmhash')
14-
151
// This script updates the index files list.js and list.txt in the directories containing binaries,
162
// as well as the 'latest' and 'nightly' symlinks/files.
173

4+
import { fileURLToPath } from 'url'
5+
import { dirname, join } from 'path'
6+
7+
import {
8+
readlinkSync,
9+
unlinkSync,
10+
symlinkSync,
11+
readFileSync,
12+
writeFile,
13+
readdir,
14+
stat,
15+
lstat
16+
} from 'fs'
17+
18+
import semver from 'semver'
19+
import swarmhash from 'swarmhash'
20+
import { readFile as readFileAsync } from 'node:fs/promises'
21+
import { keccak, sha256 } from 'ethereumjs-util'
22+
import { importer } from 'ipfs-unixfs-importer'
23+
import { MemoryBlockstore } from 'blockstore-core/memory'
24+
25+
const __filename = fileURLToPath(import.meta.url)
26+
const __dirname = dirname(__filename)
27+
1828
const ipfsHash = async (content) => {
19-
const iterator = ipfsImporter.importer([{ content }], await inMemory(IPLD), { onlyHash: true })
29+
const iterator = importer([{ content }], new MemoryBlockstore(), { onlyHash: true })
2030
const { value, done } = await iterator.next()
2131
if (done) {
2232
throw new Error('Failed to calculate an IPFS hash.')
2333
}
24-
2534
await iterator.return()
2635
return value.cid.toString()
2736
}
@@ -40,14 +49,14 @@ if (typeof(module) !== 'undefined')
4049
}
4150

4251
function updateSymlinkSync (linkPathRelativeToRoot, targetRelativeToLink) {
43-
const absoluteLinkPath = path.join(__dirname, linkPathRelativeToRoot)
52+
const absoluteLinkPath = join(__dirname, linkPathRelativeToRoot)
4453
let linkString
4554

4655
try {
47-
linkString = fs.readlinkSync(absoluteLinkPath)
56+
linkString = readlinkSync(absoluteLinkPath)
4857

4958
if (targetRelativeToLink !== linkString) {
50-
fs.unlinkSync(absoluteLinkPath)
59+
unlinkSync(absoluteLinkPath)
5160
console.log('Removed link ' + linkPathRelativeToRoot + ' -> ' + linkString)
5261
}
5362
} catch (err) {
@@ -57,29 +66,29 @@ function updateSymlinkSync (linkPathRelativeToRoot, targetRelativeToLink) {
5766
}
5867

5968
if (targetRelativeToLink !== linkString) {
60-
fs.symlinkSync(targetRelativeToLink, absoluteLinkPath, 'file')
69+
symlinkSync(targetRelativeToLink, absoluteLinkPath, 'file')
6170
console.log('Created link ' + linkPathRelativeToRoot + ' -> ' + targetRelativeToLink)
6271
}
6372
}
6473

6574
function updateCopy (srcRelativeToRoot, destRelativeToRoot) {
66-
fs.readFile(path.join(__dirname, srcRelativeToRoot), function (err, data) {
75+
readFileSync(join(__dirname, srcRelativeToRoot), function (err, data) {
6776
if (err) {
6877
throw err
6978
}
7079

71-
const absoluteDest = path.join(__dirname, destRelativeToRoot)
72-
fs.stat(absoluteDest, function (err, stats) {
80+
const absoluteDest = join(__dirname, destRelativeToRoot)
81+
stat(absoluteDest, function (err, stats) {
7382
if (err && err.code !== 'ENOENT') {
7483
throw err
7584
}
7685

7786
// If the target is a symlink, we want to replace it with a copy rather than overwrite the file it links to
7887
if (!err && stats.isSymbolicLink()) {
79-
fs.unlinkSync(absoluteDest)
88+
unlinkSync(absoluteDest)
8089
}
8190

82-
fs.writeFile(absoluteDest, data, function (err) {
91+
writeFile(absoluteDest, data, function (err) {
8392
if (err) {
8493
throw err
8594
}
@@ -90,16 +99,16 @@ function updateCopy (srcRelativeToRoot, destRelativeToRoot) {
9099
}
91100

92101
function deleteIfExists (filePathRelativeToRoot) {
93-
const absoluteFilePath = path.join(__dirname, filePathRelativeToRoot)
102+
const absoluteFilePath = join(__dirname, filePathRelativeToRoot)
94103

95-
fs.lstat(absoluteFilePath, function (err, stats) {
104+
lstat(absoluteFilePath, function (err, stats) {
96105
if (err && err.code !== 'ENOENT') {
97106
throw err
98107
}
99108

100109
if (!err) {
101110
console.log('Deleted ' + filePathRelativeToRoot)
102-
fs.unlinkSync(absoluteFilePath)
111+
unlinkSync(absoluteFilePath)
103112
}
104113
})
105114
}
@@ -116,8 +125,8 @@ function buildVersion (build) {
116125
}
117126

118127
async function makeEntry (dir, parsedFileName, oldList) {
119-
const pathRelativeToRoot = path.join(dir, parsedFileName[0])
120-
const absolutePath = path.join(__dirname, pathRelativeToRoot)
128+
const pathRelativeToRoot = join(dir, parsedFileName[0])
129+
const absolutePath = join(__dirname, pathRelativeToRoot)
121130

122131
const build = {
123132
path: parsedFileName[0],
@@ -141,11 +150,10 @@ async function makeEntry (dir, parsedFileName, oldList) {
141150
}
142151

143152
if (!build.sha256 || !build.keccak256 || !build.urls || build.urls.length !== 2) {
144-
const readFile = util.promisify(fs.readFile)
145-
const fileContent = await readFile(absolutePath)
146-
build.keccak256 = '0x' + ethUtil.keccak(fileContent).toString('hex')
153+
const fileContent = await readFileAsync(absolutePath)
154+
build.keccak256 = '0x' + keccak(fileContent).toString('hex')
147155
console.log("Computing hashes of '" + pathRelativeToRoot + "'")
148-
build.sha256 = '0x' + ethUtil.sha256(fileContent).toString('hex')
156+
build.sha256 = '0x' + sha256(fileContent).toString('hex')
149157
build.urls = [
150158
'bzzr://' + swarmhash(fileContent).toString('hex'),
151159
'dweb:/ipfs/' + await ipfsHash(fileContent)
@@ -168,15 +176,15 @@ async function batchedAsyncMap (values, batchSize, asyncMapFunction) {
168176
}
169177

170178
function processDir (dir, options, listCallback) {
171-
fs.readdir(path.join(__dirname, dir), { withFileTypes: true }, async function (err, files) {
179+
readdir(join(__dirname, dir), { withFileTypes: true }, async function (err, files) {
172180
if (err) {
173181
throw err
174182
}
175183

176184
let oldList
177185
if (options.reuseHashes) {
178186
try {
179-
oldList = JSON.parse(fs.readFileSync(path.join(__dirname, dir, '/list.json')))
187+
oldList = JSON.parse(readFileSync(join(__dirname, dir, '/list.json')))
180188
} catch (err) {
181189
// Not being able to read the existing list is not a critical error.
182190
// We'll just recreate it from scratch.
@@ -269,7 +277,7 @@ function processDir (dir, options, listCallback) {
269277

270278
// Write list.txt
271279
// A descending list of file names.
272-
fs.writeFile(path.join(__dirname, dir, '/list.txt'), buildNames.join('\n'), function (err) {
280+
writeFile(join(__dirname, dir, '/list.txt'), buildNames.join('\n'), function (err) {
273281
if (err) {
274282
throw err
275283
}
@@ -278,7 +286,7 @@ function processDir (dir, options, listCallback) {
278286

279287
// Write bin/list.json
280288
// Ascending list of builds and descending map of releases.
281-
fs.writeFile(path.join(__dirname, dir, '/list.json'), JSON.stringify({ builds: parsedList, releases: releases, latestRelease: latestRelease }, null, 2), function (err) {
289+
writeFile(join(__dirname, dir, '/list.json'), JSON.stringify({ builds: parsedList, releases: releases, latestRelease: latestRelease }, null, 2), function (err) {
282290
if (err) {
283291
throw err
284292
}
@@ -287,7 +295,7 @@ function processDir (dir, options, listCallback) {
287295

288296
// Write bin/list.js
289297
// Descending list of build filenames and descending map of releases.
290-
fs.writeFile(path.join(__dirname, dir, '/list.js'), generateLegacyListJS(buildNames, releases), function (err) {
298+
writeFile(join(__dirname, dir, '/list.js'), generateLegacyListJS(buildNames, releases), function (err) {
291299
if (err) {
292300
throw err
293301
}
@@ -302,14 +310,14 @@ function processDir (dir, options, listCallback) {
302310

303311
binaryExtensions.forEach(function (extension) {
304312
if (extension !== releaseExtension) {
305-
deleteIfExists(path.join(dir, binaryPrefix + '-latest' + extension))
313+
deleteIfExists(join(dir, binaryPrefix + '-latest' + extension))
306314
}
307315
})
308316

309317
if (dir === '/bin') {
310-
updateCopy(path.join(dir, latestReleaseFile), path.join(dir, binaryPrefix + '-latest' + releaseExtension))
318+
updateCopy(join(dir, latestReleaseFile), join(dir, binaryPrefix + '-latest' + releaseExtension))
311319
} else {
312-
updateSymlinkSync(path.join(dir, binaryPrefix + '-latest' + releaseExtension), latestReleaseFile)
320+
updateSymlinkSync(join(dir, binaryPrefix + '-latest' + releaseExtension), latestReleaseFile)
313321
}
314322
}
315323

@@ -319,11 +327,11 @@ function processDir (dir, options, listCallback) {
319327

320328
binaryExtensions.forEach(function (extension) {
321329
if (extension !== nightlyExtension) {
322-
deleteIfExists(path.join(dir, binaryPrefix + '-latest' + extension))
330+
deleteIfExists(join(dir, binaryPrefix + '-latest' + extension))
323331
}
324332
})
325333

326-
updateSymlinkSync(path.join(dir, binaryPrefix + '-nightly' + nightlyExtension), latestBuildFile)
334+
updateSymlinkSync(join(dir, binaryPrefix + '-nightly' + nightlyExtension), latestBuildFile)
327335
}
328336
})
329337
}
@@ -387,13 +395,13 @@ DIRS.forEach(function (dir) {
387395
// Starting with 0.6.2 we no longer build asm.js releases and the new builds added to bin/ are all wasm.
388396
if (semver.gt(release.version, '0.6.1')) {
389397
updateSymlinkSync(
390-
path.join('/wasm', release.path),
391-
path.join('..', 'bin', release.path)
398+
join('/wasm', release.path),
399+
join('..', 'bin', release.path)
392400
)
393401
} else {
394402
updateSymlinkSync(
395-
path.join('/emscripten-asmjs', 'solc-emscripten-asmjs-v' + release.longVersion + '.js'),
396-
path.join('..', 'bin', release.path)
403+
join('/emscripten-asmjs', 'solc-emscripten-asmjs-v' + release.longVersion + '.js'),
404+
join('..', 'bin', release.path)
397405
)
398406
}
399407
}
@@ -405,8 +413,8 @@ DIRS.forEach(function (dir) {
405413
parsedList.forEach(function (release) {
406414
if (release.prerelease === undefined) {
407415
updateSymlinkSync(
408-
path.join('/emscripten-wasm32', 'solc-emscripten-wasm32-v' + release.longVersion + '.js'),
409-
path.join('..', 'wasm', release.path)
416+
join('/emscripten-wasm32', 'solc-emscripten-wasm32-v' + release.longVersion + '.js'),
417+
join('..', 'wasm', release.path)
410418
)
411419
}
412420
})

0 commit comments

Comments
 (0)