Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: updates all deps and adds types #11

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"start": "node src/cli/bin.js",
"test": "aegir test -t node",
"build": "aegir build",
"lint": "aegir lint",
"lint": "aegir lint && aegir ts --check",
"release": "aegir release -t node",
"release-minor": "aegir release -t node --type minor",
"release-major": "aegir release -t node --type major",
Expand All @@ -30,15 +30,16 @@
"dependencies": {
"cids": "^1.0.0",
"explain-error": "^1.0.4",
"multibase": "^3.0.0",
"multihashes": "^3.0.1",
"multibase": "^4.0.2",
"multihashes": "^4.0.2",
"split2": "^3.1.1",
"uint8arrays": "^1.1.0",
"yargs": "^15.0.2"
"uint8arrays": "^2.1.3",
"yargs": "^16.2.0"
},
"devDependencies": {
"aegir": "^25.1.0",
"execa": "^4.0.0"
"@types/split2": "^2.1.6",
"aegir": "^33.0.0",
"execa": "^5.0.0"
},
"contributors": [
"Alan Shaw <[email protected]>",
Expand Down
4 changes: 4 additions & 0 deletions src/cli/commands/base32.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module.exports = {

describe: 'Convert CIDs to base 32 CID version 1.',

/**
* @param {object} argv
* @param {string[]} [argv.cids]
*/
handler (argv) {
if (argv.cids && argv.cids.length) {
return argv.cids.forEach(cid => console.log(CIDTool.base32(cid))) // eslint-disable-line no-console
Expand Down
5 changes: 5 additions & 0 deletions src/cli/commands/bases.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module.exports = {
}
},

/**
* @param {object} argv
* @param {boolean} [argv.prefix]
* @param {boolean} [argv.numeric]
*/
handler (argv) {
CIDTool.bases().forEach(({ name, code }) => {
if (argv.prefix && argv.numeric) {
Expand Down
4 changes: 4 additions & 0 deletions src/cli/commands/codecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module.exports = {
}
},

/**
* @param {object} argv
* @param {boolean} [argv.numeric]
*/
handler (argv) {
CIDTool.codecs().forEach(({ name, code }) => {
if (argv.numeric) {
Expand Down
7 changes: 7 additions & 0 deletions src/cli/commands/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ module.exports = {
}
},

/**
* @param {object} argv
* @param {string[]} [argv.cids]
* @param {string} [argv.format]
* @param {import('cids').CIDVersion} [argv.cidVersion]
* @param {import('multibase').BaseNameOrCode} [argv.base]
*/
handler (argv) {
const options = {
format: argv.format,
Expand Down
4 changes: 4 additions & 0 deletions src/cli/commands/hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module.exports = {
}
},

/**
* @param {object} argv
* @param {boolean} [argv.numeric]
*/
handler (argv) {
CIDTool.hashes().forEach(({ name, code }) => {
if (argv.numeric) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/base32.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
'use strict'

const CID = require('cids')
// @ts-ignore no types
const explain = require('explain-error')

/**
* @param {CID | string | Uint8Array} cid
*/
module.exports = function base32 (cid) {
try {
cid = new CID(cid)
Expand Down
10 changes: 6 additions & 4 deletions src/core/bases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const multibase = require('multibase')

module.exports = function bases () {
return Object.keys(multibase.names).map(name => {
const base = multibase.names[name]
const output = []

return { name: base.name, code: base.code }
})
for (const base of Object.values(multibase.names)) {
output.push({ name: base.name, code: base.code })
}

return output
}
10 changes: 7 additions & 3 deletions src/core/codecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
const CID = require('cids')

module.exports = function codecs () {
return Object.keys(CID.codecs).map(name => {
return { name, code: CID.codecs[name] }
})
const output = []

for (const [key, value] of Object.entries(CID.codecs)) {
output.push({ name: key, code: value })
}

return output
}
90 changes: 70 additions & 20 deletions src/core/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
const CID = require('cids')
const bases = require('./bases')
const codecs = require('./codecs')
// @ts-ignore no types
const explain = require('explain-error')
const multibase = require('multibase')
const multihash = require('multihashes')
const uint8ArrayToString = require('uint8arrays/to-string')

/**
* @typedef {import('multibase').BaseName} BaseName
* @typedef {import('multibase').BaseNameOrCode} BaseNameOrCode
*/

/**
* @param {CID | string | Uint8Array} cid
* @param {import('./types').FormatOptions} options
*/
module.exports = function format (cid, options) {
options = options || {}

Expand Down Expand Up @@ -39,54 +49,62 @@ module.exports = function format (cid, options) {
}
}

let base
/**
* @type {BaseName}
*/
let base = 'base58btc'

if (options.base) {
base = options.base
// Validate passed base name/code
base = findBase(options.base).name
} else if (isString(originalCid)) {
// Use base of input CID if string
base = multibase.isEncoded(originalCid)
}

base = base || 'base58btc'

// Using multibase code instead of name
if (base.length === 1) {
const baseNameCode = bases().find(b => b.code === base)
if (!baseNameCode) throw new Error(`invalid multibase: ${base}`)
base = baseNameCode.name
base = multibase.isEncoded(originalCid) || base
}

return formatStr.replace(/%([a-zA-Z%])/g, replacer(cid, base, options))
return formatStr.replace(/%([a-zA-Z%])/g, replacer(cid, base))
}

/**
* @param {*} obj
* @returns {obj is String}
*/
function isString (obj) {
return Object.prototype.toString.call(obj) === '[object String]'
}

function replacer (cid, base, options) {
return (match, specifier) => {
/**
* @param {CID} cid
* @param {BaseName} base
* @returns {(match: any, specifier: string) => string}
*/
function replacer (cid, base) {
/**
* @param {*} match
* @param {string} specifier
*/
const replace = (match, specifier) => {
switch (specifier) {
case '%':
return '%'
case 'b': // base name
return base
case 'B': // base code
return bases().find(b => b.name === base).code
return findBase(base).code
case 'v': // version string
return `cidv${cid.version}`
case 'V': // version num
return cid.version
return cid.version.toString()
case 'c': // codec name
return cid.codec
case 'C': // codec code
return codecs().find(c => c.name === cid.codec).code
return findCodec(cid).toString()
case 'h': // hash fun name
return multihash.decode(cid.multihash).name
case 'H': // hash fun code
return multihash.decode(cid.multihash).code
return multihash.decode(cid.multihash).code.toString()
case 'L': // hash length
return multihash.decode(cid.multihash).length
return multihash.decode(cid.multihash).length.toString()
case 'm': // multihash encoded in base %b
return uint8ArrayToString(multibase.encode(base, cid.multihash))
case 'M': // multihash encoded in base %b without base prefix
Expand All @@ -103,12 +121,44 @@ function replacer (cid, base, options) {
: uint8ArrayToString(cid.bytes, base)
case 'P': // prefix
return prefix(cid)

default:
throw new Error(`unrecognized specifier in format string: ${specifier}`)
}
}

return replace
}

/**
* @param {BaseNameOrCode} nameOrCode
*/
function findBase (nameOrCode) {
const baseNameCode = bases().find(b => (b.code === nameOrCode) || b.name === nameOrCode)

if (!baseNameCode) {
throw new Error(`invalid multibase: ${nameOrCode}`)
}

return baseNameCode
}

/**
* @param {CID} cid
*/
function findCodec (cid) {
const codec = codecs().find(c => c.name === cid.codec)

if (!codec) {
throw new Error(`invalid codec: ${cid.codec}`)
}

return codec.code
}

/**
* @param {CID} cid
*/
function prefix (cid) {
const { name, length } = multihash.decode(cid.multihash)
return `cidv${cid.version}-${cid.codec}-${name}-${length}`
Expand Down
10 changes: 7 additions & 3 deletions src/core/hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const multihash = require('multihashes')

// TODO: list only safe hashes https://github.com/ipfs/go-verifcid
module.exports = function hashes () {
return Object.keys(multihash.names).map(name => {
return { name, code: multihash.names[name] }
})
const output = []

for (const [name, code] of Object.entries(multihash.names)) {
output.push({ name, code })
}

return output
}
8 changes: 8 additions & 0 deletions src/core/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { CIDVersion } from 'cids'
import type { BaseNameOrCode } from 'multibase'

export interface FormatOptions {
format?: string
cidVersion?: CIDVersion
base?: BaseNameOrCode
}
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
'use strict'

/**
* @typedef {import('./core/types').FormatOptions} FormatOptions
*/

module.exports = require('./core')
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src"
]
}