Skip to content

Commit

Permalink
Update typedjson with annotations, fix bytes functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmyshchyshyn committed Nov 12, 2024
1 parent 16c486c commit bf02217
Show file tree
Hide file tree
Showing 26 changed files with 390 additions and 276 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-casper-client-sdk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set release tag
run: |
if [ ${{ github.event.release.target_commitish }} == 'feat-5.0.0' ]; then
echo "release_tag=5.0.0-rc1" >> $GITHUB_ENV
echo "release_tag=condor" >> $GITHUB_ENV
else
echo "release_tag=latest" >> $GITHUB_ENV
fi
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './types';
export * from './rpc';
export * from './sse';
export * from './types';
30 changes: 15 additions & 15 deletions src/rpc/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class RpcRequest {
@jsonMember({ constructor: String })
version: string;

@jsonMember({ isRequired: false, constructor: IDValue })
@jsonMember(() => ({ constructor: IDValue, isRequired: false }))
id?: IDValue;

@jsonMember({ constructor: String })
Expand Down Expand Up @@ -137,7 +137,7 @@ export class ParamQueryGlobalStateID {

@jsonObject
export class ParamTransactionHash {
@jsonMember({ isRequired: false, constructor: TransactionHash })
@jsonMember(() => ({ constructor: TransactionHash, isRequired: false }))
transactionHash?: TransactionHash;

@jsonMember({ isRequired: false, constructor: Boolean })
Expand Down Expand Up @@ -199,10 +199,10 @@ export class ParamGetStateEntity {

@jsonObject
export class AccountIdentifier {
@jsonMember({ isRequired: false, constructor: AccountHash })
@jsonMember(() => ({ constructor: AccountHash, isRequired: false }))
accountHash?: AccountHash;

@jsonMember({ isRequired: false, constructor: PublicKey })
@jsonMember(() => ({ constructor: PublicKey, isRequired: false }))
publicKey?: PublicKey;

constructor(accountHash?: AccountHash, publicKey?: PublicKey) {
Expand All @@ -213,13 +213,13 @@ export class AccountIdentifier {

@jsonObject
export class EntityIdentifier {
@jsonMember({ isRequired: false, constructor: AccountHash })
@jsonMember(() => ({ constructor: AccountHash, isRequired: false }))
accountHash?: AccountHash;

@jsonMember({ isRequired: false, constructor: PublicKey })
@jsonMember(() => ({ constructor: PublicKey, isRequired: false }))
publicKey?: PublicKey;

@jsonMember({ isRequired: false, constructor: EntityAddr })
@jsonMember(() => ({ constructor: EntityAddr, isRequired: false }))
entityAddr?: EntityAddr;

constructor(
Expand Down Expand Up @@ -247,7 +247,7 @@ export class EntityIdentifier {

@jsonObject
export class PutDeployRequest {
@jsonMember({ constructor: Deploy })
@jsonMember(() => ({ constructor: Deploy }))
deploy: Deploy;

constructor(deploy: Deploy) {
Expand All @@ -257,7 +257,7 @@ export class PutDeployRequest {

@jsonObject
export class PutTransactionRequest {
@jsonMember({ constructor: TransactionWrapper })
@jsonMember(() => ({ constructor: TransactionWrapper }))
transaction: TransactionWrapper;

constructor(transaction: TransactionWrapper) {
Expand Down Expand Up @@ -414,7 +414,7 @@ export class ParamDictionaryIdentifierURef {

@jsonObject
export class SpeculativeExecParams {
@jsonMember({ constructor: Deploy })
@jsonMember(() => ({ constructor: Deploy }))
deploy: Deploy;

@jsonMember({ isRequired: false, constructor: BlockIdentifier })
Expand All @@ -428,16 +428,16 @@ export class SpeculativeExecParams {

@jsonObject
export class PurseIdentifier {
@jsonMember({ isRequired: false, constructor: PublicKey })
@jsonMember(() => ({ constructor: PublicKey, isRequired: false }))
mainPurseUnderPublicKey?: PublicKey;

@jsonMember({ isRequired: false, constructor: AccountHash })
@jsonMember(() => ({ constructor: AccountHash, isRequired: false }))
mainPurseUnderAccountHash?: AccountHash;

@jsonMember({ isRequired: false, constructor: EntityAddr })
@jsonMember(() => ({ constructor: EntityAddr, isRequired: false }))
mainPurseUnderEntityAddr?: EntityAddr;

@jsonMember({ isRequired: false, constructor: URef })
@jsonMember(() => ({ constructor: URef, isRequired: false }))
purseUref?: URef;

constructor(
Expand Down Expand Up @@ -505,7 +505,7 @@ export class QueryBalanceDetailsRequest {

@jsonObject
export class InfoGetRewardRequest {
@jsonMember({ constructor: PublicKey })
@jsonMember(() => ({ constructor: PublicKey, isRequired: false }))
validator: PublicKey;

@jsonMember({ isRequired: false, constructor: PublicKey })
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export class RpcResponse {
@jsonMember({ name: 'jsonrpc', constructor: String })
version: string;

@jsonMember({ name: 'id', constructor: IDValue })
@jsonMember(() => ({ constructor: IDValue, name: 'id' }))
id?: IDValue;

@jsonMember({ name: 'result', constructor: String })
result: string;

@jsonMember({ name: 'error', constructor: RpcError })
@jsonMember(() => ({ name: 'error', constructor: RpcError }))
error?: RpcError;
}

Expand All @@ -56,7 +56,7 @@ export class StateGetAuctionInfoResult {
@jsonMember({ name: 'api_version', constructor: String })
version: string;

@jsonMember({ name: 'auction_state', constructor: AuctionState })
@jsonMember(() => ({ name: 'auction_state', constructor: AuctionState }))
auctionState: AuctionState;

rawJSON?: string;
Expand All @@ -67,7 +67,7 @@ export class StateGetBalanceResult {
@jsonMember({ name: 'api_version', constructor: String })
apiVersion: string;

@jsonMember({ name: 'balance_value', constructor: CLValueUInt512 })
@jsonMember(() => ({ name: 'balance_value', constructor: CLValueUInt512 }))
balanceValue: CLValueUInt512;

public rawJSON: string;
Expand Down
55 changes: 38 additions & 17 deletions src/sse/event_parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AllEventsNames, EventType, RawEvent } from "./event";
import { ErrUnknownEventType } from "./errors";
import { concat } from '@ethersproject/bytes';

const headerID = "id:";
const headerData = "data:";
import { AllEventsNames, EventType, RawEvent } from './event';
import { ErrUnknownEventType } from './errors';

const headerID = 'id:';
const headerData = 'data:';

export class EventParser {
private eventsToParse: Map<number, string> = new Map();
Expand All @@ -29,25 +31,38 @@ export class EventParser {
if (this.hasPrefix(line, headerID)) {
eventID = this.trimPrefix(headerID.length, line);
} else if (this.hasPrefix(line, headerData)) {
const lineData = this.trimPrefix(headerData.length, line);
if (eventData) {
eventData = new Uint8Array([...eventData, ...this.trimPrefix(headerData.length, line), '\n'.charCodeAt(0)]);
eventData = concat([
eventData,
lineData,
Uint8Array.of('\n'.charCodeAt(0))
]);
} else {
eventData = this.trimPrefix(headerData.length, line);
eventData = lineData;
}
}
}

return this.parseEventType(eventID, eventData);
}

parseEventType(eventID: Uint8Array | undefined, eventData: Uint8Array | undefined): RawEvent | Error {
parseEventType(
eventID: Uint8Array | undefined,
eventData: Uint8Array | undefined
): RawEvent | Error {
if (!eventID || !eventData) {
return new Error("Missing eventID or eventData");
return new Error('Missing eventID or eventData');
}

let eventType: EventType | undefined;
const trimmedData = this.trimPrefix("{\"".length, eventData);
const trimmedData = this.trimPrefix('{"'.length, eventData);

for (const [eType, typeName] of this.eventsToParse.entries()) {
// Convert entries to an array to allow iteration in ES5
const eventEntries = Array.from(this.eventsToParse.entries());

for (let i = 0; i < eventEntries.length; i++) {
const [eType, typeName] = eventEntries[i];
if (this.hasPrefix(trimmedData, typeName)) {
eventType = eType;
break;
Expand All @@ -59,24 +74,31 @@ export class EventParser {
}

if (eventType === EventType.APIVersionEventType) {
return new RawEvent(eventType, eventData.toString(), Number(eventID.toString()));
const idString = Buffer.from(eventID).toString();
return new RawEvent(
eventType,
Buffer.from(eventData).toString(),
Number(idString)
);
}

const parsedID = parseInt(new TextDecoder().decode(eventID), 10);
if (isNaN(parsedID)) {
return new Error("Error parsing event id");
return new Error('Error parsing event id');
}

return new RawEvent(eventType, eventData.toString(), parsedID);
return new RawEvent(eventType, Buffer.from(eventData).toString(), parsedID);
}

private hasPrefix(line: Uint8Array, prefix: string): boolean {
return String.fromCharCode(...line.slice(0, prefix.length)) === prefix;
const lineString = Buffer.from(line.slice(0, prefix.length)).toString();
return lineString === prefix;
}

private splitData(data: Uint8Array | string): Uint8Array[] {
const strData = typeof data === 'string' ? data : new TextDecoder().decode(data);
return strData.split(/\r?\n/).map((line) => new TextEncoder().encode(line));
const strData =
typeof data === 'string' ? data : new TextDecoder().decode(data);
return strData.split(/\r?\n/).map(line => new TextEncoder().encode(line));
}

trimPrefix(size: number, data: Uint8Array): Uint8Array {
Expand All @@ -97,4 +119,3 @@ export class EventParser {
return data;
}
}

10 changes: 9 additions & 1 deletion src/sse/stream_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ export class EventStreamReader {
const { value, done } = await reader.next();
if (done) break;

buffer = new Uint8Array([...buffer, ...value]);
// Concatenate new data to buffer using typed array allocation
const tempBuffer = new Uint8Array(buffer.length + value.length);
tempBuffer.set(buffer, 0);
tempBuffer.set(value, buffer.length);
buffer = tempBuffer;

// Enforce max buffer size limit
if (buffer.length > this.maxBufferSize) {
buffer = buffer.slice(-this.maxBufferSize);
}

let splitIndex: number;
let delimiterLength: number;

// Process each complete section separated by double newlines
while (
(([splitIndex, delimiterLength] = containsDoubleNewline(buffer)),
splitIndex >= 0)
Expand All @@ -41,6 +47,8 @@ export class EventStreamReader {
buffer = buffer.slice(splitIndex + delimiterLength);
}
}

// Yield remaining buffer if any data remains
if (buffer.length > 0) yield buffer;
}

Expand Down
Loading

0 comments on commit bf02217

Please sign in to comment.