Skip to content

Commit

Permalink
chore: GitHub actions warning cleanup (#819)
Browse files Browse the repository at this point in the history
* invalid JSDoc tag name "jest-environment"

* idl jsdoc warnings

* Missing JSDoc @param "s" description

* leb128 cleanup and strict arraybuffer type  enforcement

* npm audit

* changelog

fixes #811
  • Loading branch information
krpeacock committed Jan 11, 2024
1 parent e46045b commit eb3fa49
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li>chore: cleans up github actions linting warnings</li>
<li>feat: replaces `secp256k1` npm package with `@noble/curves`</li>
<li>feat: enhances `.from` methods on public key classes to support unknown types, including PublicKey instances, ArrayBuffer-like objects, DER encoded public keys, and hex strings. Also introduces a new `bufFromBufLike` util</li>
<li>feat: introduces partial identities from public keys for authentication flows</li>
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/candid/src/idl.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
/*
* @jest-environment node
*/
import * as IDL from './idl';
Expand Down
12 changes: 6 additions & 6 deletions packages/candid/src/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1531,9 +1531,9 @@ export class ServiceClass extends ConstructType<PrincipalId> {
}

/**
*
* @param x
* @returns {string}
* Takes an unknown value and returns a string representation of it.
* @param x - unknown value
* @returns {string} string representation of the value
*/
function toReadableString(x: unknown): string {
const str = JSON.stringify(x, (_key, value) =>
Expand All @@ -1547,9 +1547,9 @@ function toReadableString(x: unknown): string {

/**
* Encode a array of values
* @param argTypes
* @param args
* @returns {Buffer} serialised value
* @param argTypes - array of Types
* @param args - array of values
* @returns {ArrayBuffer} serialised value
*/
export function encode(argTypes: Array<Type<any>>, args: any[]): ArrayBuffer {
if (args.length < argTypes.length) {
Expand Down
33 changes: 31 additions & 2 deletions packages/candid/src/utils/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ export class PipeArrayBuffer {
* @param length an optional amount of bytes to use for the length.
*/
constructor(buffer?: ArrayBuffer, length = buffer?.byteLength || 0) {
this._buffer = buffer || new ArrayBuffer(0);
this._buffer = bufFromBufLike(buffer || new ArrayBuffer(0));
this._view = new Uint8Array(this._buffer, 0, length);
}

get buffer(): ArrayBuffer {
// Return a copy of the buffer.
return this._view.slice();
return bufFromBufLike(this._view.slice());
}

get byteLength(): number {
Expand Down Expand Up @@ -121,3 +121,32 @@ export class PipeArrayBuffer {
this._view = v;
}
}

/**
* Returns a true ArrayBuffer from a Uint8Array, as Uint8Array.buffer is unsafe.
* @param {Uint8Array} arr Uint8Array to convert
* @returns ArrayBuffer
*/
export function uint8ToBuf(arr: Uint8Array): ArrayBuffer {
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength).buffer;
}

/**
* Returns a true ArrayBuffer from an ArrayBufferLike object.
* @param bufLike a buffer-like object
* @returns ArrayBuffer
*/
export function bufFromBufLike(
bufLike: ArrayBuffer | Uint8Array | DataView | ArrayBufferView | ArrayBufferLike,
): ArrayBuffer {
if (bufLike instanceof Uint8Array) {
return uint8ToBuf(bufLike);
}
if (bufLike instanceof ArrayBuffer) {
return bufLike;
}
if ('buffer' in bufLike) {
return bufLike.buffer;
}
return new Uint8Array(bufLike);
}
3 changes: 2 additions & 1 deletion packages/candid/src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Hashes a string to a number. Algorithm can be found here:
* https://caml.inria.fr/pub/papers/garrigue-polymorphic_variants-ml98.pdf
* @param s
* @param s - string to hash
* @returns number representing hashed string
*/
function idlHash(s: string): number {
const utf8encoder = new TextEncoder();
Expand Down
7 changes: 4 additions & 3 deletions packages/candid/src/utils/leb128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function safeRead(pipe: Pipe, num: number): ArrayBuffer {
}

/**
* @param pipe
* @param pipe - PipeArrayBuffer simulating buffer-pipe api
*/
export function safeReadUint8(pipe: Pipe): number {
const byte = pipe.readUint8();
Expand Down Expand Up @@ -169,8 +169,9 @@ export function writeUIntLE(value: bigint | number, byteLength: number): ArrayBu

/**
*
* @param value
* @param byteLength
* @param value - bigint or number
* @param byteLength - number
* @returns ArrayBuffer
*/
export function writeIntLE(value: bigint | number, byteLength: number): ArrayBuffer {
value = BigInt(value);
Expand Down

0 comments on commit eb3fa49

Please sign in to comment.