From 927d5d6c42369817c1df290a4f575a3d431983b5 Mon Sep 17 00:00:00 2001 From: ayman Date: Wed, 5 Jun 2024 08:21:17 +0530 Subject: [PATCH 1/2] fix: cjs import --- dist/cjs/test/index.cjs | 24 ++++---- package.json | 122 ++++++++++++++++++++-------------------- src/test/index.ts | 36 ++++++------ tsconfig.base.json | 19 ++++--- tsconfig.cjs.json | 15 +++-- tsconfig.json | 2 +- tslint.json | 6 +- 7 files changed, 112 insertions(+), 112 deletions(-) diff --git a/dist/cjs/test/index.cjs b/dist/cjs/test/index.cjs index 9c2dcd5..087f6fc 100644 --- a/dist/cjs/test/index.cjs +++ b/dist/cjs/test/index.cjs @@ -1,19 +1,19 @@ -'use strict'; +"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // @ts-ignore const bech32Lib = require("../index.cjs"); const tape = require("tape"); -const fixtures = require('../../../src/test/fixtures'); +const fixtures = require("../../../src/test/fixtures"); function testValidFixture(f, bech32) { if (f.hex) { tape(`fromWords/toWords ${f.hex}`, (t) => { t.plan(3); - const words = bech32.toWords(Buffer.from(f.hex, 'hex')); + const words = bech32.toWords(Buffer.from(f.hex, "hex")); const bytes = Buffer.from(bech32.fromWords(f.words)); const bytes2 = Buffer.from(bech32.fromWordsUnsafe(f.words)); t.same(words, f.words); - t.same(bytes.toString('hex'), f.hex); - t.same(bytes2.toString('hex'), f.hex); + t.same(bytes.toString("hex"), f.hex); + t.same(bytes2.toString("hex"), f.hex); }); } tape(`encode ${f.prefix} ${f.hex || f.words}`, (t) => { @@ -31,13 +31,13 @@ function testValidFixture(f, bech32) { }); tape(`fails for ${f.string} with 1 bit flipped`, (t) => { t.plan(2); - const buffer = Buffer.from(f.string, 'utf8'); - buffer[f.string.lastIndexOf('1') + 1] ^= 0x1; // flip a bit, after the prefix - const str = buffer.toString('utf8'); + const buffer = Buffer.from(f.string, "utf8"); + buffer[f.string.lastIndexOf("1") + 1] ^= 0x1; // flip a bit, after the prefix + const str = buffer.toString("utf8"); t.equal(bech32.decodeUnsafe(str, f.limit), undefined); t.throws(() => { bech32.decode(str, f.limit); - }, new RegExp('Invalid checksum|Unknown character')); + }, new RegExp("Invalid checksum|Unknown character")); }); // === compare of objects compares reference in memory, so this works const wrongBech32 = bech32 === bech32Lib.bech32 ? bech32Lib.bech32m : bech32Lib.bech32; @@ -46,7 +46,7 @@ function testValidFixture(f, bech32) { t.equal(wrongBech32.decodeUnsafe(f.string, f.limit), undefined); t.throws(() => { wrongBech32.decode(f.string, f.limit); - }, new RegExp('Invalid checksum')); + }, new RegExp("Invalid checksum")); }); } function testInvalidFixture(f, bech32) { @@ -59,7 +59,7 @@ function testInvalidFixture(f, bech32) { }); } if (f.string !== undefined || f.stringHex) { - const str = f.string || Buffer.from(f.stringHex, 'hex').toString('binary'); + const str = f.string || Buffer.from(f.stringHex, "hex").toString("binary"); tape(`decode fails for ${str} (${f.exception})`, (t) => { t.plan(2); t.equal(bech32.decodeUnsafe(str), undefined); @@ -90,7 +90,7 @@ fixtures.fromWords.invalid.forEach((f) => { }, new RegExp(f.exception)); }); }); -tape('toWords/toWordsUnsafe accept bytes as ArrayLike', (t) => { +tape("toWords/toWordsUnsafe accept bytes as ArrayLike", (t) => { // Ensures that only the two operations from // interface ArrayLike { // readonly length: number; diff --git a/package.json b/package.json index 748f921..4b3e18b 100644 --- a/package.json +++ b/package.json @@ -1,64 +1,64 @@ { - "name": "bech32", - "version": "2.0.0", - "description": "Bech32 encoding / decoding", - "type": "module", - "keywords": [ - "base32", - "bech32", - "bech32m", - "bitcoin", - "crypto", - "crytography", - "decode", - "decoding", - "encode", - "encoding" - ], - "main": "dist/cjs/index.js", - "module": "dist/esm/index.js", - "types": "dist/index.d.ts", - "files": [ - "dist/cjs/index.js", - "dist/cjs/index.d.ts", - "dist/esm/index.js" - ], - "exports": { - ".": { - "require": "./dist/cjs/index.js", - "import": "./dist/esm/index.js", - "types": "./dist/cjs/index.d.ts" - } - }, - "license": "MIT", - "devDependencies": { - "@types/node": "^14.0.14", - "@types/tape": "^4.13.4", - "nyc": "^15.0.0", - "prettier": "^2.0.5", - "rimraf": "^3.0.2", - "tap-dot": "*", - "tape": "^5.3.0", - "ts-node": "^10.9.2", - "tslint": "^6.1.3", - "tsx": "^4.7.2", - "typescript": "^3.9.5" - }, - "repository": { - "url": "http://github.com/bitcoinjs/bech32", - "type": "git" - }, - "scripts": { - "build": "npm run clean && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json", - "postbuild": "find dist/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\;", - "clean": "rimraf dist", - "coverage": "nyc -x [src/test/*.ts] --check-coverage --branches 90 --functions 90 npm test", - "format": "npm run prettier -- --write", - "format:ci": "npm run prettier -- --check", - "gitdiff:ci": "npm run build && git diff --exit-code", - "lint:fix": "npm run lint -- --fix", - "lint": "tslint -p tsconfig.cjs.json -c tslint.json", - "prettier": "prettier --print-width 100 --single-quote --trailing-comma=all \"**/!(*.d).ts\"", - "test": "tape dist/cjs/test/*.cjs | tap-dot" + "name": "bech32", + "version": "2.0.0", + "description": "Bech32 encoding / decoding", + "type": "module", + "keywords": [ + "base32", + "bech32", + "bech32m", + "bitcoin", + "crypto", + "crytography", + "decode", + "decoding", + "encode", + "encoding" + ], + "main": "dist/cjs/index.cjs", + "module": "dist/esm/index.js", + "types": "dist/index.d.ts", + "files": [ + "dist/cjs/index.cjs", + "dist/cjs/index.d.ts", + "dist/esm/index.js" + ], + "exports": { + ".": { + "require": "./dist/cjs/index.cjs", + "import": "./dist/esm/index.js", + "types": "./dist/cjs/index.d.ts" } + }, + "license": "MIT", + "devDependencies": { + "@types/node": "^14.0.14", + "@types/tape": "^4.13.4", + "nyc": "^15.0.0", + "prettier": "^2.0.5", + "rimraf": "^3.0.2", + "tap-dot": "*", + "tape": "^5.3.0", + "ts-node": "^10.9.2", + "tslint": "^6.1.3", + "tsx": "^4.7.2", + "typescript": "^3.9.5" + }, + "repository": { + "url": "http://github.com/bitcoinjs/bech32", + "type": "git" + }, + "scripts": { + "build": "npm run clean && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json", + "postbuild": "find dist/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\;", + "clean": "rimraf dist", + "coverage": "nyc -x [dist/cjs/test/*.ts] --check-coverage --branches 90 --functions 90 npm test", + "format": "npm run prettier -- --write", + "format:ci": "npm run prettier -- --check", + "gitdiff:ci": "npm run build && git diff --exit-code", + "lint:fix": "npm run lint -- --fix", + "lint": "tslint -p tsconfig.cjs.json -c tslint.json", + "prettier": "prettier --print-width 100 --single-quote --trailing-comma=all \"**/!(*.d).ts\"", + "test": "tape dist/cjs/test/*.cjs | tap-dot" + } } diff --git a/src/test/index.ts b/src/test/index.ts index 4aee821..9ac2d17 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -1,9 +1,9 @@ -'use strict'; +"use strict"; // @ts-ignore -import * as bech32Lib from '../index.cjs'; -import * as tape from 'tape'; -const fixtures = require('../../../src/test/fixtures'); +import * as bech32Lib from "../index.cjs"; +import * as tape from "tape"; +const fixtures = require("../../../src/test/fixtures"); type Fixture = { string: string; @@ -28,18 +28,21 @@ function testValidFixture(f: Fixture, bech32: any): void { tape(`fromWords/toWords ${f.hex}`, (t): void => { t.plan(3); - const words = bech32.toWords(Buffer.from(f.hex, 'hex')); + const words = bech32.toWords(Buffer.from(f.hex, "hex")); const bytes = Buffer.from(bech32.fromWords(f.words)); const bytes2 = Buffer.from(bech32.fromWordsUnsafe(f.words)); t.same(words, f.words); - t.same(bytes.toString('hex'), f.hex); - t.same(bytes2.toString('hex'), f.hex); + t.same(bytes.toString("hex"), f.hex); + t.same(bytes2.toString("hex"), f.hex); }); } tape(`encode ${f.prefix} ${f.hex || f.words}`, (t): void => { t.plan(1); - t.strictEqual(bech32.encode(f.prefix, f.words, f.limit), f.string.toLowerCase()); + t.strictEqual( + bech32.encode(f.prefix, f.words, f.limit), + f.string.toLowerCase() + ); }); tape(`decode ${f.string}`, (t): void => { @@ -56,24 +59,25 @@ function testValidFixture(f: Fixture, bech32: any): void { tape(`fails for ${f.string} with 1 bit flipped`, (t): void => { t.plan(2); - const buffer = Buffer.from(f.string, 'utf8'); - buffer[f.string.lastIndexOf('1') + 1] ^= 0x1; // flip a bit, after the prefix - const str = buffer.toString('utf8'); + const buffer = Buffer.from(f.string, "utf8"); + buffer[f.string.lastIndexOf("1") + 1] ^= 0x1; // flip a bit, after the prefix + const str = buffer.toString("utf8"); t.equal(bech32.decodeUnsafe(str, f.limit), undefined); t.throws((): void => { bech32.decode(str, f.limit); - }, new RegExp('Invalid checksum|Unknown character')); + }, new RegExp("Invalid checksum|Unknown character")); }); // === compare of objects compares reference in memory, so this works - const wrongBech32 = bech32 === bech32Lib.bech32 ? bech32Lib.bech32m : bech32Lib.bech32; + const wrongBech32 = + bech32 === bech32Lib.bech32 ? bech32Lib.bech32m : bech32Lib.bech32; tape(`fails for ${f.string} with wrong encoding`, (t): void => { t.plan(2); t.equal(wrongBech32.decodeUnsafe(f.string, f.limit), undefined); t.throws((): void => { wrongBech32.decode(f.string, f.limit); - }, new RegExp('Invalid checksum')); + }, new RegExp("Invalid checksum")); }); } @@ -89,7 +93,7 @@ function testInvalidFixture(f: InvalidFixture, bech32: any): void { } if (f.string !== undefined || f.stringHex) { - const str = f.string || Buffer.from(f.stringHex, 'hex').toString('binary'); + const str = f.string || Buffer.from(f.stringHex, "hex").toString("binary"); tape(`decode fails for ${str} (${f.exception})`, (t): void => { t.plan(2); @@ -127,7 +131,7 @@ fixtures.fromWords.invalid.forEach((f: InvalidFixture): void => { }); }); -tape('toWords/toWordsUnsafe accept bytes as ArrayLike', (t): void => { +tape("toWords/toWordsUnsafe accept bytes as ArrayLike", (t): void => { // Ensures that only the two operations from // interface ArrayLike { // readonly length: number; diff --git a/tsconfig.base.json b/tsconfig.base.json index 0d939e5..045411c 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -1,11 +1,12 @@ { - "compilerOptions": { - "strict": true, - "resolveJsonModule": true, - "target": "es2015", - "moduleResolution": "node", - "noImplicitAny": true, - "preserveConstEnums": true - }, - "include": ["src"] + "compilerOptions": { + "strict": true, + "resolveJsonModule": true, + "target": "es2015", + "moduleResolution": "node", + "noImplicitAny": true, + "preserveConstEnums": true + }, + "include": ["src"], + "exclude": ["node_modules"] } diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 1f967c3..c2e3ddd 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -1,10 +1,9 @@ { - "extends": "./tsconfig.base.json", - "compilerOptions": { - "declaration": true, - "emitDeclarationOnly": false, - "outDir": "dist/cjs", - "module": "commonjs" - }, - "exclude": ["node_modules"] + "extends": "./tsconfig.base.json", + "compilerOptions": { + "declaration": true, + "emitDeclarationOnly": false, + "outDir": "dist/cjs", + "module": "commonjs" + } } diff --git a/tsconfig.json b/tsconfig.json index d697636..46edf16 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,5 +6,5 @@ "resolveJsonModule": true, "module": "ESNext" }, - "exclude": ["node_modules", "src/test/**/*"] + "exclude": ["src/test/**/*"] } diff --git a/tslint.json b/tslint.json index c5fa3c2..b542cf1 100644 --- a/tslint.json +++ b/tslint.json @@ -4,11 +4,7 @@ "rules": { "arrow-parens": [true, "ban-single-arg-parens"], "curly": false, - "indent": [ - true, - "spaces", - 2 - ], + "indent": [true, "spaces", 2], "interface-name": [false], "match-default-export-name": true, "max-classes-per-file": [false], From 3aeb9b0cdc7c75656ec7c90f07511d5958e34bee Mon Sep 17 00:00:00 2001 From: ayman Date: Wed, 5 Jun 2024 08:23:58 +0530 Subject: [PATCH 2/2] chore: format --- src/test/index.ts | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/test/index.ts b/src/test/index.ts index 9ac2d17..4aee821 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -1,9 +1,9 @@ -"use strict"; +'use strict'; // @ts-ignore -import * as bech32Lib from "../index.cjs"; -import * as tape from "tape"; -const fixtures = require("../../../src/test/fixtures"); +import * as bech32Lib from '../index.cjs'; +import * as tape from 'tape'; +const fixtures = require('../../../src/test/fixtures'); type Fixture = { string: string; @@ -28,21 +28,18 @@ function testValidFixture(f: Fixture, bech32: any): void { tape(`fromWords/toWords ${f.hex}`, (t): void => { t.plan(3); - const words = bech32.toWords(Buffer.from(f.hex, "hex")); + const words = bech32.toWords(Buffer.from(f.hex, 'hex')); const bytes = Buffer.from(bech32.fromWords(f.words)); const bytes2 = Buffer.from(bech32.fromWordsUnsafe(f.words)); t.same(words, f.words); - t.same(bytes.toString("hex"), f.hex); - t.same(bytes2.toString("hex"), f.hex); + t.same(bytes.toString('hex'), f.hex); + t.same(bytes2.toString('hex'), f.hex); }); } tape(`encode ${f.prefix} ${f.hex || f.words}`, (t): void => { t.plan(1); - t.strictEqual( - bech32.encode(f.prefix, f.words, f.limit), - f.string.toLowerCase() - ); + t.strictEqual(bech32.encode(f.prefix, f.words, f.limit), f.string.toLowerCase()); }); tape(`decode ${f.string}`, (t): void => { @@ -59,25 +56,24 @@ function testValidFixture(f: Fixture, bech32: any): void { tape(`fails for ${f.string} with 1 bit flipped`, (t): void => { t.plan(2); - const buffer = Buffer.from(f.string, "utf8"); - buffer[f.string.lastIndexOf("1") + 1] ^= 0x1; // flip a bit, after the prefix - const str = buffer.toString("utf8"); + const buffer = Buffer.from(f.string, 'utf8'); + buffer[f.string.lastIndexOf('1') + 1] ^= 0x1; // flip a bit, after the prefix + const str = buffer.toString('utf8'); t.equal(bech32.decodeUnsafe(str, f.limit), undefined); t.throws((): void => { bech32.decode(str, f.limit); - }, new RegExp("Invalid checksum|Unknown character")); + }, new RegExp('Invalid checksum|Unknown character')); }); // === compare of objects compares reference in memory, so this works - const wrongBech32 = - bech32 === bech32Lib.bech32 ? bech32Lib.bech32m : bech32Lib.bech32; + const wrongBech32 = bech32 === bech32Lib.bech32 ? bech32Lib.bech32m : bech32Lib.bech32; tape(`fails for ${f.string} with wrong encoding`, (t): void => { t.plan(2); t.equal(wrongBech32.decodeUnsafe(f.string, f.limit), undefined); t.throws((): void => { wrongBech32.decode(f.string, f.limit); - }, new RegExp("Invalid checksum")); + }, new RegExp('Invalid checksum')); }); } @@ -93,7 +89,7 @@ function testInvalidFixture(f: InvalidFixture, bech32: any): void { } if (f.string !== undefined || f.stringHex) { - const str = f.string || Buffer.from(f.stringHex, "hex").toString("binary"); + const str = f.string || Buffer.from(f.stringHex, 'hex').toString('binary'); tape(`decode fails for ${str} (${f.exception})`, (t): void => { t.plan(2); @@ -131,7 +127,7 @@ fixtures.fromWords.invalid.forEach((f: InvalidFixture): void => { }); }); -tape("toWords/toWordsUnsafe accept bytes as ArrayLike", (t): void => { +tape('toWords/toWordsUnsafe accept bytes as ArrayLike', (t): void => { // Ensures that only the two operations from // interface ArrayLike { // readonly length: number;