Skip to content

Commit

Permalink
release(borshest@1): pass TS strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 9, 2024
1 parent d1ea1bb commit a803b6d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
8 changes: 4 additions & 4 deletions borshest/borsh-base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type AnyField = {
encode (buffer: Writer, value: unknown)
encode (buffer: Writer, value: unknown): void
decode (buffer: Reader): unknown
}

export type Field<T> = {
encode (buffer: Writer, value: T)
encode (buffer: Writer, value: T): void
decode (buffer: Reader): T
}

Expand Down Expand Up @@ -46,7 +46,7 @@ export class Writer {
if (native) {
const [toCall, size] = native
this.grow(size);
this.view[toCall](this.offset, value, true);
;(this.view[toCall as keyof DataView] as Function)(this.offset, value, true);
this.offset += size;
} else {
throw new Error(`writeNumber got invalid type hint: ${type}`)
Expand Down Expand Up @@ -95,7 +95,7 @@ export class Reader {
if (native) {
const [toCall, size] = native
this.assertEnough(size)
const ret = this.view[toCall](this.offset, true);
const ret = (this.view[toCall as keyof DataView] as Function)(this.offset, true);
this.offset += size
return ret
} else {
Expand Down
4 changes: 2 additions & 2 deletions borshest/borsh-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const vec = <T>(element: Field<T>): Field<T[]> => ({
})

export const zVec = <T>(element: Field<T>) => ({
encode (buffer, value) {
encode (buffer: never, value: T[]) {
throw new Error('encode zVec: not implemented')
},
decode (buffer: Reader): T[] {
Expand Down Expand Up @@ -76,7 +76,7 @@ export const map = <K extends string|number|symbol, V>(k: Field<K>, v: Field<V>)
buffer.writeNumber(keys.length, 'u32') // 4 bytes for length
for (const key of keys) { // store key/values
k.encode(buffer, key as K)
v.encode(buffer, isMap ? value.get(key as K) : value[key as K])
v.encode(buffer, (isMap ? value.get(key as K) : value[key as K]) as V)
}
},
decode (buffer: Reader): Map<K, V> {
Expand Down
2 changes: 1 addition & 1 deletion borshest/borsh-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const unsigned = (bytes: number): Field<bigint> => {
encode (buffer: Writer, value: bigint) {
const chunk = new Uint8Array(bytes);
for (let i = 0; i < bytes; i++) {
buffer[i] = Number(value & 0xFFn);
;(buffer as unknown as Array<number>)[i] = Number(value & 0xFFn);
value = value >> 8n;
}
buffer.write(chunk)
Expand Down
6 changes: 4 additions & 2 deletions borshest/borsh-struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ export const struct = <T>(...fields: [string, AnyField][]): Field<T> => {
return {

encode (buffer: Writer, value: T) {
for (const [key, element] of fields) element.encode(buffer, value[key])
for (const [key, element] of fields) {
element.encode(buffer, value[key as keyof T])
}
},

decode (buffer: Reader): T {
const result: Partial<T> = {};
for (const [key, element] of fields) {
try {
result[key] = element.decode(buffer)
result[key as keyof typeof result] = element.decode(buffer) as typeof result[keyof T]
} catch (e) {
;(e as any).structPath ??= []
;(e as any).structPath.unshift(key)
Expand Down
12 changes: 7 additions & 5 deletions borshest/borshest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ import { struct } from './borsh-struct'
export type Fields = [string, AnyField][]

export function encode <T> (schema: Field<T>, decoded: T): Uint8Array {
return schema.encode(new Writer(), decoded)
const writer = new Writer()
schema.encode(writer, decoded)
return new Uint8Array(writer.buffer)
}

export function decode <T> (schema: Field<T>, encoded: Uint8Array|Array<number>): T {
if (!(encoded instanceof Uint8Array)) encoded = new Uint8Array(encoded)
return schema.decode(new Reader(encoded))
}

export function Struct (...fields) {
export function Struct (...fields: [string, AnyField][]) {
const schema = struct(...fields)
return class Struct {
static decode (encoded: Uint8Array) {
return new this(decode(schema, encoded))
return new this(decode(schema, encoded) as Record<string, unknown>)
}
constructor (data) {
constructor (data: Record<string, unknown>) {
for (const [key, _] of fields) {
this[key] = data[key]
Object.assign(this, { [key]: data[key] })
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion borshest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"bugs": "https://github.com/hackbg/toolbox/issues",
"files": [ "README.md", "*.ts" ],
"version": "1.0.0-rc.1",
"version": "1.0.0",
"license": "MIT",
"keywords": [ "borsh", "zcash", "encoding", "decoding", "binary", "format", "fadroma", "namada" ],
"homepage": "https://github.com/hackbg/toolbox",
Expand Down
8 changes: 7 additions & 1 deletion borshest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
{ "extends": "../tsconfig.json", "include": [ "*.ts" ] }
{
"extends": "../tsconfig.json",
"include": [ "*.ts" ],
"compilerOptions": {
"strict": true
}
}

0 comments on commit a803b6d

Please sign in to comment.