Skip to content

Commit

Permalink
feat: add types (#41)
Browse files Browse the repository at this point in the history
* feat: add types

This module exports a `.path` function so add types to the module
so we can use it without needing to add `@ts-ignore`s everywhere.

* chore: add typecheck step to ci
  • Loading branch information
achingbrain authored Jul 30, 2021
1 parent 1d78c3e commit 2074599
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ jobs:
with:
node-version: 14.x
- run: npm install
- uses: gozala/[email protected]
- run: npm run build --if-present
- run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ logs
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
dist

# while testing npm5
package-lock.json
Expand Down
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@
"postinstall": "node src/post-install.js",
"restore-bin": "git reset -- bin/ipfs && git checkout -- bin/ipfs",
"test": "tape test/*.js | tap-spec",
"lint": "standard"
"lint": "tsc --noEmit && standard",
"prepublishOnly": "tsc"
},
"pre-commit": "restore-bin",
"bin": {
"ipfs": "bin/ipfs"
},
"files": [
"bin",
"dist",
"src",
"test",
"LICENSE",
"package.json",
"README.md",
"tsconfig.json"
],
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/npm-go-ipfs.git"
Expand All @@ -27,14 +38,20 @@
"url": "https://github.com/ipfs/npm-go-ipfs/issues"
},
"homepage": "https://github.com/ipfs/npm-go-ipfs",
"types": "./dist/src/types.d.ts",
"devDependencies": {
"@types/got": "^9.6.12",
"@types/gunzip-maybe": "^1.4.0",
"@types/tar-fs": "^2.0.1",
"@types/unzip-stream": "^0.3.1",
"execa": "^4.0.1",
"fs-extra": "^9.0.0",
"pre-commit": "^1.2.2",
"standard": "^13.1.0",
"tap-spec": "^5.0.0",
"tape": "^4.13.2",
"tape-promise": "^4.0.0"
"tape-promise": "^4.0.0",
"typescript": "^4.3.5"
},
"dependencies": {
"cachedir": "^2.3.0",
Expand Down
63 changes: 59 additions & 4 deletions src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,34 @@
go-ipfs architecture: the architecture of the hardware this program is run from
go-ipfs install path: './go-ipfs'
*/
// @ts-ignore no types
const goenv = require('go-platform')
const gunzip = require('gunzip-maybe')
const got = require('got')
const got = require('got').default
const path = require('path')
const tarFS = require('tar-fs')
const unzip = require('unzip-stream')
const pkgConf = require('pkg-conf')
// @ts-ignore no types
const cachedir = require('cachedir')
const pkg = require('../package.json')
const fs = require('fs')
const hasha = require('hasha')
const cproc = require('child_process')
const isWin = process.platform === 'win32'

// avoid expensive fetch if file is already in cache
/**
* avoid expensive fetch if file is already in cache
* @param {string} url
*/
async function cachingFetchAndVerify (url) {
const cacheDir = process.env.NPM_GO_IPFS_CACHE || cachedir('npm-go-ipfs')
const filename = url.split('/').pop()

if (!filename) {
throw new Error('Invalid URL')
}

const cachedFilePath = path.join(cacheDir, filename)
const cachedHashPath = `${cachedFilePath}.sha512`

Expand Down Expand Up @@ -69,6 +79,11 @@ async function cachingFetchAndVerify (url) {
return fs.createReadStream(cachedFilePath)
}

/**
* @param {string} url
* @param {string} installPath
* @param {import('stream').Readable} stream
*/
function unpack (url, installPath, stream) {
return new Promise((resolve, reject) => {
if (url.endsWith('.zip')) {
Expand All @@ -91,6 +106,12 @@ function unpack (url, installPath, stream) {
})
}

/**
* @param {string} [version]
* @param {string} [platform]
* @param {string} [arch]
* @param {string} [installPath]
*/
function cleanArguments (version, platform, arch, installPath) {
const conf = pkgConf.sync('go-ipfs', {
cwd: process.env.INIT_CWD || process.cwd(),
Expand All @@ -109,6 +130,10 @@ function cleanArguments (version, platform, arch, installPath) {
}
}

/**
* @param {string} version
* @param {string} distUrl
*/
async function ensureVersion (version, distUrl) {
console.info(`${distUrl}/go-ipfs/versions`)
const versions = (await got(`${distUrl}/go-ipfs/versions`).text()).trim().split('\n')
Expand All @@ -118,6 +143,12 @@ async function ensureVersion (version, distUrl) {
}
}

/**
* @param {string} version
* @param {string} platform
* @param {string} arch
* @param {string} distUrl
*/
async function getDownloadURL (version, platform, arch, distUrl) {
await ensureVersion(version, distUrl)

Expand All @@ -135,6 +166,14 @@ async function getDownloadURL (version, platform, arch, distUrl) {
return `${distUrl}/go-ipfs/${version}${link}`
}

/**
* @param {object} options
* @param {string} options.version
* @param {string} options.platform
* @param {string} options.arch
* @param {string} options.installPath
* @param {string} options.distUrl
*/
async function download ({ version, platform, arch, installPath, distUrl }) {
const url = await getDownloadURL(version, platform, arch, distUrl)
const data = await cachingFetchAndVerify(url)
Expand All @@ -145,6 +184,11 @@ async function download ({ version, platform, arch, installPath, distUrl }) {
return path.join(installPath, 'go-ipfs', `ipfs${platform === 'windows' ? '.exe' : ''}`)
}

/**
* @param {object} options
* @param {string} options.depBin
* @param {string} options.version
*/
async function link ({ depBin, version }) {
let localBin = path.resolve(path.join(__dirname, '..', 'bin', 'ipfs'))

Expand Down Expand Up @@ -179,6 +223,11 @@ async function link ({ depBin, version }) {

var outstr = result.stdout.toString()
var m = /ipfs version ([^\n]+)\n/.exec(outstr)

if (!m) {
throw new Error('Could not determine IPFS version')
}

var actualVersion = `v${m[1]}`

if (actualVersion !== version) {
Expand All @@ -188,8 +237,14 @@ async function link ({ depBin, version }) {
return localBin
}

module.exports = async (...args) => {
args = cleanArguments(...args)
/**
* @param {string} [version]
* @param {string} [platform]
* @param {string} [arch]
* @param {string} [installPath]
*/
module.exports = async (version, platform, arch, installPath) => {
const args = cleanArguments(version, platform, arch, installPath)

return link({
...args,
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ module.exports.path = function () {
}
}

throw new Error('go-ipfs binary not found, it may not be installed or an error may have occured during installation')
throw new Error('go-ipfs binary not found, it may not be installed or an error may have occurred during installation')
}
2 changes: 1 addition & 1 deletion test/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('Ensure go-ipfs defined in package.json is fetched on dependency install',
const exampleProjectRoot = path.join(__dirname, 'fixtures', 'example-project')

// from `example-project`, install the module
const res = execa.sync('npm', ['install'], {
execa.sync('npm', ['install'], {
cwd: exampleProjectRoot
})

Expand Down
44 changes: 44 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"compilerOptions": {
"strict": true,
// project options
"allowJs": true,
"checkJs": true,
"target": "ES2019",
"lib": [
"ES2019",
"ES2020.Promise",
"ES2020.String",
"ES2020.BigInt",
"DOM",
"DOM.Iterable"
],
"noEmitOnError": true,
"incremental": true,
"composite": true,
"isolatedModules": true,
"removeComments": false,
// module resolution
"esModuleInterop": true,
"moduleResolution": "node",
// linter checks
"noImplicitReturns": false,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
// advanced
"importsNotUsedAsValues": "error",
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"stripInternal": true,
"resolveJsonModule": true,
"outDir": "dist"
},
"include": [
"src",
"package.json"
],
"exclude": [
"node_modules"
]
}

0 comments on commit 2074599

Please sign in to comment.