From 12444d7fa181663c428abd6df35cbdf8702ac565 Mon Sep 17 00:00:00 2001 From: Anthony Gabriele Date: Wed, 5 Feb 2025 13:55:33 -0500 Subject: [PATCH 1/2] esm support - first attempt --- package.json | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index cf2f0923..123f7f04 100644 --- a/package.json +++ b/package.json @@ -15,19 +15,22 @@ "publishConfig": { "access": "public" }, + "exports": { + "./*": "./src/*.ts" + }, "files": [ - "cosmos/", - "cosmos_proto/", - "cosmwasm/", - "gogoproto/", - "google/", - "ibc/", - "tendermint/", - "/binary.*", - "/helpers.*", - "/utf8.*", - "/varint.*", - "/index.*", + "src/cosmos/", + "src/cosmos_proto/", + "src/cosmwasm/", + "src/gogoproto/", + "src/google/", + "src/ibc/", + "src/tendermint/", + "/src/binary.*", + "/src/helpers.*", + "/src/utf8.*", + "/src/varint.*", + "/src/index.*", "*.md", "!wasmd-*/**/*.md", "!cosmos-sdk-*/**/*.md" From 45ce484528196a261eb23ca3c1216b5c25de4a42 Mon Sep 17 00:00:00 2001 From: Anthony Gabriele Date: Wed, 5 Feb 2025 16:28:13 -0500 Subject: [PATCH 2/2] use stricter types so that installing from github works --- src/binary.ts | 16 ++++++++-------- src/utf8.ts | 12 ++++++------ src/varint.ts | 22 +++++++++++----------- tsconfig.json | 1 + 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/binary.ts b/src/binary.ts index b5b298da..7ff4c3c9 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -41,18 +41,18 @@ import { utf8Length, utf8Read, utf8Write } from "./utf8"; import { + int64FromString, + int64Length, int64ToString, readInt32, readUInt32, uInt64ToString, varint32read, varint64read, + writeByte, + writeFixed32, writeVarint32, writeVarint64, - int64FromString, - int64Length, - writeFixed32, - writeByte, zzDecode, zzEncode, } from "./varint"; @@ -126,7 +126,7 @@ export class BinaryReader implements IBinaryReader { } else { do { if (this.pos >= this.len) throw indexOutOfRange(this); - } while (this.buf[this.pos++] & 128); + } while (this.buf[this.pos++]! & 128); } return this; } @@ -196,7 +196,7 @@ export class BinaryReader implements IBinaryReader { sint64(): bigint { let [lo, hi] = varint64read.bind(this)(); // zig zag - [lo, hi] = zzDecode(lo, hi); + [lo, hi] = zzDecode(lo, hi) as [number, number]; return BigInt(int64ToString(lo, hi)); } @@ -419,7 +419,7 @@ export class BinaryWriter implements IBinaryWriter { sint64(value: string | number | bigint): BinaryWriter { let { lo, hi } = int64FromString(value.toString()); // zig zag - [lo, hi] = zzEncode(lo, hi); + [lo, hi] = zzEncode(lo, hi) as [number, number]; return this._push(writeVarint64, int64Length(lo, hi), { lo, hi }); } @@ -466,7 +466,7 @@ function writeBytes(val: Uint8Array | number[], buf: Uint8Array | number[], pos: if (typeof Uint8Array !== "undefined") { (buf as Uint8Array).set(val, pos); } else { - for (let i = 0; i < val.length; ++i) buf[pos + i] = val[i]; + for (let i = 0; i < val.length; ++i) buf[pos + i] = val[i]!; } } diff --git a/src/utf8.ts b/src/utf8.ts index 943b147f..89913fff 100644 --- a/src/utf8.ts +++ b/src/utf8.ts @@ -69,19 +69,19 @@ export function utf8Read(buffer: ArrayLike, start: number, end: number) i = 0, // char offset t; // temporary while (start < end) { - t = buffer[start++]; + t = buffer[start++] as number; if (t < 128) chunk[i++] = t; - else if (t > 191 && t < 224) chunk[i++] = ((t & 31) << 6) | (buffer[start++] & 63); + else if (t > 191 && t < 224) chunk[i++] = ((t & 31) << 6) | (buffer[start++]! & 63); else if (t > 239 && t < 365) { t = (((t & 7) << 18) | - ((buffer[start++] & 63) << 12) | - ((buffer[start++] & 63) << 6) | - (buffer[start++] & 63)) - + ((buffer[start++]! & 63) << 12) | + ((buffer[start++]! & 63) << 6) | + (buffer[start++]! & 63)) - 0x10000; chunk[i++] = 0xd800 + (t >> 10); chunk[i++] = 0xdc00 + (t & 1023); - } else chunk[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63); + } else chunk[i++] = ((t & 15) << 12) | ((buffer[start++]! & 63) << 6) | (buffer[start++]! & 63); if (i > 8191) { (parts || (parts = [])).push(String.fromCharCode(...chunk)); i = 0; diff --git a/src/varint.ts b/src/varint.ts index 52c119c8..9b1ab517 100644 --- a/src/varint.ts +++ b/src/varint.ts @@ -56,7 +56,7 @@ export function varint64read(this: ReaderLike): [number, number] { let highBits = 0; for (let shift = 0; shift < 28; shift += 7) { - let b = this.buf[this.pos++]; + let b = this.buf[this.pos++]!; lowBits |= (b & 0x7f) << shift; if ((b & 0x80) == 0) { this.assertBounds(); @@ -64,7 +64,7 @@ export function varint64read(this: ReaderLike): [number, number] { } } - let middleByte = this.buf[this.pos++]; + let middleByte = this.buf[this.pos++]!; // last four bits of the first 32 bit number lowBits |= (middleByte & 0x0f) << 28; @@ -78,7 +78,7 @@ export function varint64read(this: ReaderLike): [number, number] { } for (let shift = 3; shift <= 31; shift += 7) { - let b = this.buf[this.pos++]; + let b = this.buf[this.pos++]!; highBits |= (b & 0x7f) << shift; if ((b & 0x80) == 0) { this.assertBounds(); @@ -314,28 +314,28 @@ export function varint32write(value: number, bytes: number[]): void { * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L220 */ export function varint32read(this: ReaderLike): number { - let b = this.buf[this.pos++]; + let b = this.buf[this.pos++]!; let result = b & 0x7f; if ((b & 0x80) == 0) { this.assertBounds(); return result; } - b = this.buf[this.pos++]; + b = this.buf[this.pos++]!; result |= (b & 0x7f) << 7; if ((b & 0x80) == 0) { this.assertBounds(); return result; } - b = this.buf[this.pos++]; + b = this.buf[this.pos++]!; result |= (b & 0x7f) << 14; if ((b & 0x80) == 0) { this.assertBounds(); return result; } - b = this.buf[this.pos++]; + b = this.buf[this.pos++]!; result |= (b & 0x7f) << 21; if ((b & 0x80) == 0) { this.assertBounds(); @@ -343,10 +343,10 @@ export function varint32read(this: ReaderLike): number { } // Extract only last 4 bits - b = this.buf[this.pos++]; + b = this.buf[this.pos++]!; result |= (b & 0x0f) << 28; - for (let readBytes = 5; (b & 0x80) !== 0 && readBytes < 10; readBytes++) b = this.buf[this.pos++]; + for (let readBytes = 5; (b & 0x80) !== 0 && readBytes < 10; readBytes++) b = this.buf[this.pos++]!; if ((b & 0x80) != 0) throw new Error("invalid varint"); @@ -387,14 +387,14 @@ export function zzDecode(lo: number, hi: number) { * unsigned int32 without moving pos. */ export function readUInt32(buf: Uint8Array, pos: number) { - return (buf[pos] | (buf[pos + 1] << 8) | (buf[pos + 2] << 16)) + buf[pos + 3] * 0x1000000; + return (buf[pos]! | (buf[pos + 1]! << 8) | (buf[pos + 2]! << 16)) + buf[pos + 3]! * 0x1000000; } /** * signed int32 without moving pos. */ export function readInt32(buf: Uint8Array, pos: number) { - return (buf[pos] | (buf[pos + 1] << 8) | (buf[pos + 2] << 16)) + (buf[pos + 3] << 24); + return (buf[pos]! | (buf[pos + 1]! << 8) | (buf[pos + 2]! << 16)) + (buf[pos + 3]! << 24); } /** diff --git a/tsconfig.json b/tsconfig.json index 8769788b..70046dfb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,7 @@ "noImplicitReturns": true, "noUnusedLocals": false, "noUnusedParameters": false, + "noUncheckedIndexedAccess": true, "outDir": "build", "pretty": true, // We want comments in the .d.ts files. TS does not allow us to remove comments