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

esm support #94

Closed
wants to merge 2 commits into from
Closed
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
27 changes: 15 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 8 additions & 8 deletions src/binary.ts
Original file line number Diff line number Diff line change
@@ -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]!;
}
}

12 changes: 6 additions & 6 deletions src/utf8.ts
Original file line number Diff line number Diff line change
@@ -69,19 +69,19 @@ export function utf8Read(buffer: ArrayLike<number>, 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;
22 changes: 11 additions & 11 deletions src/varint.ts
Original file line number Diff line number Diff line change
@@ -56,15 +56,15 @@ 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();
return [lowBits, highBits];
}
}

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,39 +314,39 @@ 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();
return result;
}

// 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);
}

/**
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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