From 9d24b62051f140e5050010655f1a5ee832b0cd23 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 13 Sep 2024 10:42:38 +0100 Subject: [PATCH] chore: fix linting --- README.md | 8 +++++--- package.json | 4 ++++ src/bases/base.ts | 5 +++-- src/bases/base256emoji.ts | 17 ++++++++++++++--- src/block.ts | 1 + test/test-cid.spec.ts | 25 ++++++++++++++----------- test/test-link.spec.ts | 15 ++++++++------- test/test-multibase.spec.ts | 4 ++-- 8 files changed, 51 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 260df917..8265a253 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# multiformats + [![multiformats.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://multiformats.io) [![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multiformats.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multiformats) [![CI](https://img.shields.io/github/actions/workflow/status/multiformats/js-multiformats/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/multiformats/js-multiformats/actions/workflows/js-test-and-release.yml?query=branch%3Amaster) @@ -212,7 +214,7 @@ $ npm i multiformats ## Browser ` @@ -226,8 +228,8 @@ Loading this module through a script tag will make it's exports available as `Mu Licensed under either of -- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / ) -- MIT ([LICENSE-MIT](LICENSE-MIT) / ) +- Apache 2.0, ([LICENSE-APACHE](https://github.com/multiformats/js-multiformats/LICENSE-APACHE) / ) +- MIT ([LICENSE-MIT](https://github.com/multiformats/js-multiformats/LICENSE-MIT) / ) # Contribution diff --git a/package.json b/package.json index 9c00f25b..6967e2e0 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,10 @@ "bugs": { "url": "https://github.com/multiformats/js-multiformats/issues" }, + "publishConfig": { + "access": "public", + "provenance": true + }, "keywords": [ "ipfs", "ipld", diff --git a/src/bases/base.ts b/src/bases/base.ts index 03be1c84..9b5c160c 100644 --- a/src/bases/base.ts +++ b/src/bases/base.ts @@ -44,11 +44,12 @@ class Decoder implements MultibaseDe constructor (name: Base, prefix: Prefix, baseDecode: DecodeFn) { this.name = name this.prefix = prefix + const prefixCodePoint = prefix.codePointAt(0) /* c8 ignore next 3 */ - if (prefix.codePointAt(0) === undefined) { + if (prefixCodePoint === undefined) { throw new Error('Invalid prefix character') } - this.prefixCodePoint = prefix.codePointAt(0) as number + this.prefixCodePoint = prefixCodePoint this.baseDecode = baseDecode } diff --git a/src/bases/base256emoji.ts b/src/bases/base256emoji.ts index d17a44c2..9950cb9d 100644 --- a/src/bases/base256emoji.ts +++ b/src/bases/base256emoji.ts @@ -2,7 +2,14 @@ import { from } from './base.js' const alphabet = Array.from('๐Ÿš€๐Ÿชโ˜„๐Ÿ›ฐ๐ŸŒŒ๐ŸŒ‘๐ŸŒ’๐ŸŒ“๐ŸŒ”๐ŸŒ•๐ŸŒ–๐ŸŒ—๐ŸŒ˜๐ŸŒ๐ŸŒ๐ŸŒŽ๐Ÿ‰โ˜€๐Ÿ’ป๐Ÿ–ฅ๐Ÿ’พ๐Ÿ’ฟ๐Ÿ˜‚โค๐Ÿ˜๐Ÿคฃ๐Ÿ˜Š๐Ÿ™๐Ÿ’•๐Ÿ˜ญ๐Ÿ˜˜๐Ÿ‘๐Ÿ˜…๐Ÿ‘๐Ÿ˜๐Ÿ”ฅ๐Ÿฅฐ๐Ÿ’”๐Ÿ’–๐Ÿ’™๐Ÿ˜ข๐Ÿค”๐Ÿ˜†๐Ÿ™„๐Ÿ’ช๐Ÿ˜‰โ˜บ๐Ÿ‘Œ๐Ÿค—๐Ÿ’œ๐Ÿ˜”๐Ÿ˜Ž๐Ÿ˜‡๐ŸŒน๐Ÿคฆ๐ŸŽ‰๐Ÿ’žโœŒโœจ๐Ÿคท๐Ÿ˜ฑ๐Ÿ˜Œ๐ŸŒธ๐Ÿ™Œ๐Ÿ˜‹๐Ÿ’—๐Ÿ’š๐Ÿ˜๐Ÿ’›๐Ÿ™‚๐Ÿ’“๐Ÿคฉ๐Ÿ˜„๐Ÿ˜€๐Ÿ–ค๐Ÿ˜ƒ๐Ÿ’ฏ๐Ÿ™ˆ๐Ÿ‘‡๐ŸŽถ๐Ÿ˜’๐Ÿคญโฃ๐Ÿ˜œ๐Ÿ’‹๐Ÿ‘€๐Ÿ˜ช๐Ÿ˜‘๐Ÿ’ฅ๐Ÿ™‹๐Ÿ˜ž๐Ÿ˜ฉ๐Ÿ˜ก๐Ÿคช๐Ÿ‘Š๐Ÿฅณ๐Ÿ˜ฅ๐Ÿคค๐Ÿ‘‰๐Ÿ’ƒ๐Ÿ˜ณโœ‹๐Ÿ˜š๐Ÿ˜๐Ÿ˜ด๐ŸŒŸ๐Ÿ˜ฌ๐Ÿ™ƒ๐Ÿ€๐ŸŒท๐Ÿ˜ป๐Ÿ˜“โญโœ…๐Ÿฅบ๐ŸŒˆ๐Ÿ˜ˆ๐Ÿค˜๐Ÿ’ฆโœ”๐Ÿ˜ฃ๐Ÿƒ๐Ÿ’โ˜น๐ŸŽŠ๐Ÿ’˜๐Ÿ˜ โ˜๐Ÿ˜•๐ŸŒบ๐ŸŽ‚๐ŸŒป๐Ÿ˜๐Ÿ–•๐Ÿ’๐Ÿ™Š๐Ÿ˜น๐Ÿ—ฃ๐Ÿ’ซ๐Ÿ’€๐Ÿ‘‘๐ŸŽต๐Ÿคž๐Ÿ˜›๐Ÿ”ด๐Ÿ˜ค๐ŸŒผ๐Ÿ˜ซโšฝ๐Ÿค™โ˜•๐Ÿ†๐Ÿคซ๐Ÿ‘ˆ๐Ÿ˜ฎ๐Ÿ™†๐Ÿป๐Ÿƒ๐Ÿถ๐Ÿ’๐Ÿ˜ฒ๐ŸŒฟ๐Ÿงก๐ŸŽโšก๐ŸŒž๐ŸŽˆโŒโœŠ๐Ÿ‘‹๐Ÿ˜ฐ๐Ÿคจ๐Ÿ˜ถ๐Ÿค๐Ÿšถ๐Ÿ’ฐ๐Ÿ“๐Ÿ’ข๐ŸคŸ๐Ÿ™๐Ÿšจ๐Ÿ’จ๐Ÿคฌโœˆ๐ŸŽ€๐Ÿบ๐Ÿค“๐Ÿ˜™๐Ÿ’Ÿ๐ŸŒฑ๐Ÿ˜–๐Ÿ‘ถ๐Ÿฅดโ–ถโžกโ“๐Ÿ’Ž๐Ÿ’ธโฌ‡๐Ÿ˜จ๐ŸŒš๐Ÿฆ‹๐Ÿ˜ท๐Ÿ•บโš ๐Ÿ™…๐Ÿ˜Ÿ๐Ÿ˜ต๐Ÿ‘Ž๐Ÿคฒ๐Ÿค ๐Ÿคง๐Ÿ“Œ๐Ÿ”ต๐Ÿ’…๐Ÿง๐Ÿพ๐Ÿ’๐Ÿ˜—๐Ÿค‘๐ŸŒŠ๐Ÿคฏ๐Ÿทโ˜Ž๐Ÿ’ง๐Ÿ˜ฏ๐Ÿ’†๐Ÿ‘†๐ŸŽค๐Ÿ™‡๐Ÿ‘โ„๐ŸŒด๐Ÿ’ฃ๐Ÿธ๐Ÿ’Œ๐Ÿ“๐Ÿฅ€๐Ÿคข๐Ÿ‘…๐Ÿ’ก๐Ÿ’ฉ๐Ÿ‘๐Ÿ“ธ๐Ÿ‘ป๐Ÿค๐Ÿคฎ๐ŸŽผ๐Ÿฅต๐Ÿšฉ๐ŸŽ๐ŸŠ๐Ÿ‘ผ๐Ÿ’๐Ÿ“ฃ๐Ÿฅ‚') const alphabetBytesToChars: string[] = (alphabet.reduce((p, c, i) => { p[i] = c; return p }, ([]))) -const alphabetCharsToBytes: number[] = (alphabet.reduce((p, c, i) => { p[c.codePointAt(0) as number] = i; return p }, ([]))) +const alphabetCharsToBytes: number[] = (alphabet.reduce((p, c, i) => { + const codePoint = c.codePointAt(0) + if (codePoint == null) { + throw new Error(`Invalid character: ${c}`) + } + p[codePoint] = i + return p +}, ([]))) function encode (data: Uint8Array): string { return data.reduce((p, c) => { @@ -14,8 +21,12 @@ function encode (data: Uint8Array): string { function decode (str: string): Uint8Array { const byts = [] for (const char of str) { - const byt = alphabetCharsToBytes[char.codePointAt(0) as number] - if (byt === undefined) { + const codePoint = char.codePointAt(0) + if (codePoint == null) { + throw new Error(`Invalid character: ${char}`) + } + const byt = alphabetCharsToBytes[codePoint] + if (byt == null) { throw new Error(`Non-base256emoji character: ${char}`) } byts.push(byt) diff --git a/src/block.ts b/src/block.ts index ae59ff13..c04c2555 100644 --- a/src/block.ts +++ b/src/block.ts @@ -195,6 +195,7 @@ type CreateUnsafeInput ({ bytes, cid, value: maybeValue, codec }: CreateUnsafeInput): API.BlockView { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const value = maybeValue !== undefined ? maybeValue : (codec?.decode(bytes)) diff --git a/test/test-cid.spec.ts b/test/test-cid.spec.ts index d3f4886a..85f35677 100644 --- a/test/test-cid.spec.ts +++ b/test/test-cid.spec.ts @@ -1,4 +1,5 @@ /* globals describe, it */ +/* eslint-disable @typescript-eslint/strict-boolean-expressions */ import { assert } from 'aegir/chai' import OLDCID from 'cids' @@ -105,8 +106,8 @@ describe('CID', () => { it('should construct from an old CID', () => { const cidStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' const oldCid = CID.parse(cidStr) - const newCid = (CID.asCID(oldCid) as CID) - assert.deepStrictEqual(newCid.toString(), cidStr) + const newCid = CID.asCID(oldCid) + assert.deepStrictEqual(newCid?.toString(), cidStr) }) it('inspect bytes', () => { @@ -226,8 +227,8 @@ describe('CID', () => { const cidStr = 'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u' const oldCid = CID.parse(cidStr) - const newCid = (CID.asCID(oldCid) as CID) - assert.deepStrictEqual(newCid.toString(), cidStr) + const newCid = CID.asCID(oldCid) + assert.deepStrictEqual(newCid?.toString(), cidStr) }) it('.link() should return this CID', () => { @@ -516,7 +517,7 @@ describe('CID', () => { // @ts-expect-error - no such method assert.strictEqual(typeof incompatibleCID.toV0, 'undefined') - const cid1 = (CID.asCID(incompatibleCID) as CID) + const cid1 = CID.asCID(incompatibleCID) assert.ok(cid1 instanceof CID) assert.strictEqual(cid1.code, code) assert.strictEqual(cid1.version, version) @@ -538,7 +539,7 @@ describe('CID', () => { assert.strictEqual(cid3, cid4) const cid5 = ( - CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes))) as CID + CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes))) ) assert.ok(cid5 instanceof CID) assert.strictEqual(cid5.version, 1) @@ -557,7 +558,9 @@ describe('CID', () => { assert.deepStrictEqual({ ...x, ...empty }, { ...y, ...empty }) } - const equalDigest = (x: MultihashDigest, y: MultihashDigest): void => { + const equalDigest = (x?: MultihashDigest, y?: MultihashDigest): void => { + assert.ok(x) + assert.ok(y) assert.deepStrictEqual(x.digest, y.digest) assert.deepStrictEqual(x.code, y.code) assert.deepStrictEqual(x.digest, y.digest) @@ -662,12 +665,12 @@ describe('CID', () => { it('new CID from old CID', async () => { const hash = await sha256.digest(textEncoder.encode('abc')) const cid = ( - CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes))) as CID + CID.asCID(new OLDCID(1, 'raw', Uint8Array.from(hash.bytes))) ) - assert.deepStrictEqual(cid.version, 1) + assert.deepStrictEqual(cid?.version, 1) - equalDigest(cid.multihash, hash) - assert.deepStrictEqual(cid.code, 85) + equalDigest(cid?.multihash, hash) + assert.deepStrictEqual(cid?.code, 85) }) it('util.inspect', async () => { diff --git a/test/test-link.spec.ts b/test/test-link.spec.ts index 94fd4d2f..c70429f0 100644 --- a/test/test-link.spec.ts +++ b/test/test-link.spec.ts @@ -1,4 +1,5 @@ /* globals describe, it */ +/* eslint-disable @typescript-eslint/strict-boolean-expressions */ import { assert } from 'aegir/chai' import { sha256 } from '../src/hashes/sha2.js' @@ -25,10 +26,10 @@ describe('Link', () => { it('create v1', async () => { const hash = await sha256.digest(utf8.encode('abc')) const link = Link.create(0x71, hash) - const code = link.code as 0x71 + const code = link.code assert.deepStrictEqual(code, 0x71) - const version = link.version as 1 + const version = link.version assert.deepEqual(version, 1) const multihash = link.multihash @@ -39,10 +40,10 @@ describe('Link', () => { const hash = await sha256.digest(utf8.encode('abc')) const link = Link.createLegacy(hash) - const code = link.code as 0x70 + const code = link.code assert.deepStrictEqual(code, 0x70) - const version = link.version as 0 + const version = link.version assert.deepEqual(version, 0) const multihash = link.multihash @@ -72,7 +73,7 @@ describe('Link', () => { const t1 = link assert.ok(t1) - // ensurate that you can't cast incorrectly + // ensure that you can't cast incorrectly const t2 = // @ts-expect-error - version is 1 not 0 (link as Link.Link) @@ -124,10 +125,10 @@ describe('decode', () => { const link = Link.decode(bytes) - const code = link.code as 0x71 + const code = link.code assert.deepStrictEqual(code, 0x71) - const version = link.version as 1 + const version = link.version assert.deepEqual(version, 1) const multihash = link.multihash diff --git a/test/test-multibase.spec.ts b/test/test-multibase.spec.ts index 3063816f..93b446b5 100644 --- a/test/test-multibase.spec.ts +++ b/test/test-multibase.spec.ts @@ -159,12 +159,12 @@ describe('multibase', () => { }) it('infers prefix and name corretly', () => { - const name = base32.name as 'base32' + const name = base32.name // @ts-expect-error - TS catches mismatch const name2: 'base16' = base32.name - const prefix = base32.prefix as 'b' + const prefix = base32.prefix assert.equal(prefix, 'b') assert.equal(name, 'base32') assert.equal(name2, name)