Add comprehensive installation and setup documentation

- Add GETTING_STARTED.md with quick start guide and development modes
- Add INSTALL.sh automated installation script
- Add INSTALLATION_CHECKLIST.md, INSTALLATION_SUCCESS.md, and INSTALLATION_SUMMARY.md
- Add QUICK_REFERENCE.md for common commands
- Add SETUP_GUIDE.md with detailed setup instructions
- Update README.md with improved project overview
- Add did-wallet app dependencies and node_modules
This commit is contained in:
Dorian
2026-01-27 17:18:21 +00:00
parent a81f655133
commit 0d073fa89e
22658 changed files with 4494151 additions and 6 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+46
View File
@@ -0,0 +1,46 @@
import type { Writer, Reader } from './index.js';
export declare enum CODEC_TYPES {
VARINT = 0,
BIT64 = 1,
LENGTH_DELIMITED = 2,
START_GROUP = 3,
END_GROUP = 4,
BIT32 = 5
}
export interface EncodeOptions {
lengthDelimited?: boolean;
writeDefaults?: boolean;
}
export interface EncodeFunction<T> {
(value: Partial<T>, writer: Writer, opts?: EncodeOptions): void;
}
type CollectionTypes = any[] | Map<any, any>;
type PrimitiveTypes = boolean | number | string | bigint | Uint8Array;
type CollectionLimits<T> = {
[K in keyof T]: T[K] extends CollectionTypes ? number : T[K] extends PrimitiveTypes ? never : Limits<T[K]>;
};
type ArrayElementLimits<T> = {
[K in keyof T as `${string & K}$`]: T[K] extends Array<infer ElementType> ? (ElementType extends PrimitiveTypes ? never : Limits<ElementType>) : (T[K] extends PrimitiveTypes ? never : Limits<T[K]>);
};
type MapValueLimits<T> = {
[K in keyof T as `${string & K}$value`]: T[K] extends Map<any, infer MapValueType> ? (MapValueType extends PrimitiveTypes ? never : Limits<MapValueType>) : (T[K] extends PrimitiveTypes ? never : Limits<T[K]>);
};
type Limits<T> = Partial<CollectionLimits<T> & ArrayElementLimits<T> & MapValueLimits<T>>;
export interface DecodeOptions<T> {
/**
* Runtime-specified limits for lengths of repeated/map fields
*/
limits?: Limits<T>;
}
export interface DecodeFunction<T> {
(reader: Reader, length?: number, opts?: DecodeOptions<T>): T;
}
export interface Codec<T> {
name: string;
type: CODEC_TYPES;
encode: EncodeFunction<T>;
decode: DecodeFunction<T>;
}
export declare function createCodec<T>(name: string, type: CODEC_TYPES, encode: EncodeFunction<T>, decode: DecodeFunction<T>): Codec<T>;
export {};
//# sourceMappingURL=codec.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAGhD,oBAAY,WAAW;IACrB,MAAM,IAAI;IACV,KAAK,IAAA;IACL,gBAAgB,IAAA;IAChB,WAAW,IAAA;IACX,SAAS,IAAA;IACT,KAAK,IAAA;CACN;AAED,MAAM,WAAW,aAAa;IAC5B,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAED,MAAM,WAAW,cAAc,CAAC,CAAC;IAC/B,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,IAAI,CAAA;CAChE;AAGD,KAAK,eAAe,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;AAG5C,KAAK,cAAc,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,CAAA;AAGrE,KAAK,gBAAgB,CAAE,CAAC,IAAI;KACzB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,eAAe,GAAG,MAAM,GACnD,CAAC,CAAC,CAAC,CAAC,SAAS,cAAc,GAAG,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACrD,CAAA;AAGD,KAAK,kBAAkB,CAAE,CAAC,IAAI;KAC3B,CAAC,IAAI,MAAM,CAAC,IAAI,GAAG,MAAM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CAAC,CAAC,CAAA;IACzB,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,CAAA;CAC1B;AAED,wBAAgB,WAAW,CAAE,CAAC,EAAG,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAOhI"}
+19
View File
@@ -0,0 +1,19 @@
// https://developers.google.com/protocol-buffers/docs/encoding#structure
export var CODEC_TYPES;
(function (CODEC_TYPES) {
CODEC_TYPES[CODEC_TYPES["VARINT"] = 0] = "VARINT";
CODEC_TYPES[CODEC_TYPES["BIT64"] = 1] = "BIT64";
CODEC_TYPES[CODEC_TYPES["LENGTH_DELIMITED"] = 2] = "LENGTH_DELIMITED";
CODEC_TYPES[CODEC_TYPES["START_GROUP"] = 3] = "START_GROUP";
CODEC_TYPES[CODEC_TYPES["END_GROUP"] = 4] = "END_GROUP";
CODEC_TYPES[CODEC_TYPES["BIT32"] = 5] = "BIT32";
})(CODEC_TYPES || (CODEC_TYPES = {}));
export function createCodec(name, type, encode, decode) {
return {
name,
type,
encode,
decode
};
}
//# sourceMappingURL=codec.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAEA,yEAAyE;AACzE,MAAM,CAAN,IAAY,WAOX;AAPD,WAAY,WAAW;IACrB,iDAAU,CAAA;IACV,+CAAK,CAAA;IACL,qEAAgB,CAAA;IAChB,2DAAW,CAAA;IACX,uDAAS,CAAA;IACT,+CAAK,CAAA;AACP,CAAC,EAPW,WAAW,KAAX,WAAW,QAOtB;AA0DD,MAAM,UAAU,WAAW,CAAM,IAAY,EAAE,IAAiB,EAAE,MAAyB,EAAE,MAAyB;IACpH,OAAO;QACL,IAAI;QACJ,IAAI;QACJ,MAAM;QACN,MAAM;KACP,CAAA;AACH,CAAC"}
+3
View File
@@ -0,0 +1,3 @@
import type { Codec } from '../codec.js';
export declare function enumeration<T>(v: any): Codec<T>;
//# sourceMappingURL=enum.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"enum.d.ts","sourceRoot":"","sources":["../../../src/codecs/enum.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAkC,KAAK,EAAE,MAAM,aAAa,CAAA;AAExE,wBAAgB,WAAW,CAAE,CAAC,EAAG,CAAC,EAAE,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAyBjD"}
+22
View File
@@ -0,0 +1,22 @@
import { createCodec, CODEC_TYPES } from '../codec.js';
export function enumeration(v) {
function findValue(val) {
// Use the reverse mapping to look up the enum key for the stored value
// https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
if (v[val.toString()] == null) {
throw new Error('Invalid enum value');
}
return v[val];
}
const encode = function enumEncode(val, writer) {
const enumValue = findValue(val);
writer.int32(enumValue);
};
const decode = function enumDecode(reader) {
const val = reader.int32();
return findValue(val);
};
// @ts-expect-error yeah yeah
return createCodec('enum', CODEC_TYPES.VARINT, encode, decode);
}
//# sourceMappingURL=enum.js.map
@@ -0,0 +1 @@
{"version":3,"file":"enum.js","sourceRoot":"","sources":["../../../src/codecs/enum.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAGtD,MAAM,UAAU,WAAW,CAAM,CAAM;IACrC,SAAS,SAAS,CAAE,GAAoB;QACtC,uEAAuE;QACvE,2EAA2E;QAC3E,IAAI,CAAC,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;YAC9B,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;QACvC,CAAC;QAED,OAAO,CAAC,CAAC,GAAG,CAAC,CAAA;IACf,CAAC;IAED,MAAM,MAAM,GAAoC,SAAS,UAAU,CAAE,GAAG,EAAE,MAAM;QAC9E,MAAM,SAAS,GAAG,SAAS,CAAC,GAAG,CAAC,CAAA;QAEhC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IACzB,CAAC,CAAA;IAED,MAAM,MAAM,GAAoC,SAAS,UAAU,CAAE,MAAM;QACzE,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,EAAE,CAAA;QAE1B,OAAO,SAAS,CAAC,GAAG,CAAC,CAAA;IACvB,CAAC,CAAA;IAED,6BAA6B;IAC7B,OAAO,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AAChE,CAAC"}
@@ -0,0 +1,6 @@
import type { EncodeFunction, DecodeFunction, Codec } from '../codec.js';
export interface Factory<A, T> {
new (obj: A): T;
}
export declare function message<T>(encode: EncodeFunction<T>, decode: DecodeFunction<T>): Codec<T>;
//# sourceMappingURL=message.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"message.d.ts","sourceRoot":"","sources":["../../../src/codecs/message.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAExE,MAAM,WAAW,OAAO,CAAC,CAAC,EAAE,CAAC;IAC3B,KAAK,GAAG,EAAE,CAAC,GAAG,CAAC,CAAA;CAChB;AAED,wBAAgB,OAAO,CAAE,CAAC,EAAG,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAE3F"}
@@ -0,0 +1,5 @@
import { createCodec, CODEC_TYPES } from '../codec.js';
export function message(encode, decode) {
return createCodec('message', CODEC_TYPES.LENGTH_DELIMITED, encode, decode);
}
//# sourceMappingURL=message.js.map
@@ -0,0 +1 @@
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../../src/codecs/message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAOtD,MAAM,UAAU,OAAO,CAAM,MAAyB,EAAE,MAAyB;IAC/E,OAAO,WAAW,CAAC,SAAS,EAAE,WAAW,CAAC,gBAAgB,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AAC7E,CAAC"}
+4
View File
@@ -0,0 +1,4 @@
import type { Codec, DecodeOptions } from './codec.js';
import type { Uint8ArrayList } from 'uint8arraylist';
export declare function decodeMessage<T>(buf: Uint8Array | Uint8ArrayList, codec: Pick<Codec<T>, 'decode'>, opts?: DecodeOptions<T>): T;
//# sourceMappingURL=decode.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,wBAAgB,aAAa,CAAE,CAAC,EAAG,GAAG,EAAE,UAAU,GAAG,cAAc,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,EAAE,IAAI,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAIhI"}
+6
View File
@@ -0,0 +1,6 @@
import { createReader } from './utils/reader.js';
export function decodeMessage(buf, codec, opts) {
const reader = createReader(buf);
return codec.decode(reader, undefined, opts);
}
//# sourceMappingURL=decode.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAIhD,MAAM,UAAU,aAAa,CAAM,GAAgC,EAAE,KAA+B,EAAE,IAAuB;IAC3H,MAAM,MAAM,GAAG,YAAY,CAAC,GAAG,CAAC,CAAA;IAEhC,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,CAAA;AAC9C,CAAC"}
+3
View File
@@ -0,0 +1,3 @@
import type { Codec } from './codec.js';
export declare function encodeMessage<T>(message: Partial<T>, codec: Pick<Codec<T>, 'encode'>): Uint8Array;
//# sourceMappingURL=encode.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../src/encode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAEvC,wBAAgB,aAAa,CAAE,CAAC,EAAG,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,UAAU,CAQnG"}
+9
View File
@@ -0,0 +1,9 @@
import { createWriter } from './utils/writer.js';
export function encodeMessage(message, codec) {
const w = createWriter();
codec.encode(message, w, {
lengthDelimited: false
});
return w.finish();
}
//# sourceMappingURL=encode.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"encode.js","sourceRoot":"","sources":["../../src/encode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAGhD,MAAM,UAAU,aAAa,CAAM,OAAmB,EAAE,KAA+B;IACrF,MAAM,CAAC,GAAG,YAAY,EAAE,CAAA;IAExB,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE;QACvB,eAAe,EAAE,KAAK;KACvB,CAAC,CAAA;IAEF,OAAO,CAAC,CAAC,MAAM,EAAE,CAAA;AACnB,CAAC"}
+323
View File
@@ -0,0 +1,323 @@
/**
* @packageDocumentation
*
* This module contains serialization/deserialization code used when encoding/decoding protobufs.
*
* It should be declared as a dependency of your project:
*
* ```console
* npm i protons-runtime
* ```
*/
import type { Codec } from './codec.js';
export interface FieldDef {
name: string;
codec: Codec<any>;
optional?: true;
repeats?: true;
packed?: true;
}
export { decodeMessage } from './decode.js';
export { encodeMessage } from './encode.js';
export { enumeration } from './codecs/enum.js';
export { message } from './codecs/message.js';
export { createReader as reader } from './utils/reader.js';
export { createWriter as writer } from './utils/writer.js';
export type { Codec, EncodeOptions, DecodeOptions } from './codec.js';
export interface Writer {
/**
* Current length
*/
len: number;
/**
* Writes an unsigned 32 bit value as a varint
*/
uint32(value: number): this;
/**
* Writes a signed 32 bit value as a varint`
*/
int32(value: number): this;
/**
* Writes a 32 bit value as a varint, zig-zag encoded
*/
sint32(value: number): this;
/**
* Writes an unsigned 64 bit value as a varint
*/
uint64(value: bigint): this;
/**
* Writes an unsigned 64 bit value as a varint
*/
uint64Number(value: number): this;
/**
* Writes an unsigned 64 bit value as a varint
*/
uint64String(value: string): this;
/**
* Writes a signed 64 bit value as a varint
*/
int64(value: bigint): this;
/**
* Writes a signed 64 bit value as a varint
*/
int64Number(value: number): this;
/**
* Writes a signed 64 bit value as a varint
*/
int64String(value: string): this;
/**
* Writes a signed 64 bit value as a varint, zig-zag encoded
*/
sint64(value: bigint): this;
/**
* Writes a signed 64 bit value as a varint, zig-zag encoded
*/
sint64Number(value: number): this;
/**
* Writes a signed 64 bit value as a varint, zig-zag encoded
*/
sint64String(value: string): this;
/**
* Writes a boolish value as a varint
*/
bool(value: boolean): this;
/**
* Writes an unsigned 32 bit value as fixed 32 bits
*/
fixed32(value: number): this;
/**
* Writes a signed 32 bit value as fixed 32 bits
*/
sfixed32(value: number): this;
/**
* Writes an unsigned 64 bit value as fixed 64 bits
*/
fixed64(value: bigint): this;
/**
* Writes an unsigned 64 bit value as fixed 64 bits
*/
fixed64Number(value: number): this;
/**
* Writes an unsigned 64 bit value as fixed 64 bits
*/
fixed64String(value: string): this;
/**
* Writes a signed 64 bit value as fixed 64 bits
*/
sfixed64(value: bigint): this;
/**
* Writes a signed 64 bit value as fixed 64 bits
*/
sfixed64Number(value: number): this;
/**
* Writes a signed 64 bit value as fixed 64 bits
*/
sfixed64String(value: string): this;
/**
* Writes a float (32 bit)
*/
float(value: number): this;
/**
* Writes a double (64 bit float)
*/
double(value: number): this;
/**
* Writes a sequence of bytes
*/
bytes(value: Uint8Array): this;
/**
* Writes a string
*/
string(value: string): this;
/**
* Forks this writer's state by pushing it to a stack.
* Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
*/
fork(): this;
/**
* Resets this instance to the last state.
*/
reset(): this;
/**
* Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
*/
ldelim(): this;
/**
* Finishes the write operation
*/
finish(): Uint8Array;
}
export interface Reader {
/**
* Read buffer
*/
buf: Uint8Array;
/**
* Read buffer position
*/
pos: number;
/**
* Read buffer length
*/
len: number;
/**
* Reads a varint as an unsigned 32 bit value
*/
uint32(): number;
/**
* Reads a varint as a signed 32 bit value
*/
int32(): number;
/**
* Reads a zig-zag encoded varint as a signed 32 bit value
*/
sint32(): number;
/**
* Reads a varint as a boolean
*/
bool(): boolean;
/**
* Reads fixed 32 bits as an unsigned 32 bit integer
*/
fixed32(): number;
/**
* Reads fixed 32 bits as a signed 32 bit integer
*/
sfixed32(): number;
/**
* Reads a float (32 bit) as a number
*/
float(): number;
/**
* Reads a double (64 bit float) as a number
*/
double(): number;
/**
* Reads a sequence of bytes preceded by its length as a varint
*/
bytes(): Uint8Array;
/**
* Reads a string preceded by its byte length as a varint
*/
string(): string;
/**
* Skips the specified number of bytes if specified, otherwise skips a varints`
*/
skip(length?: number): void;
/**
* Skips the next element of the specified wire type
*/
skipType(wireType: number): void;
/**
* Reads a varint as a signed 64 bit value
*/
int64(): bigint;
/**
* Reads a varint as a signed 64 bit value
*/
int64Number(): number;
/**
* Reads a varint as a signed 64 bit value
*/
int64String(): string;
/**
* Reads a varint as an unsigned 64 bit value
*/
uint64(): bigint;
/**
* Reads a varint as an unsigned 64 bit value
*/
uint64Number(): number;
/**
* Reads a varint as an unsigned 64 bit value
*/
uint64String(): string;
/**
* Reads a zig-zag encoded varint as a signed 64 bit value
*/
sint64(): bigint;
/**
* Reads a zig-zag encoded varint as a signed 64 bit value
*/
sint64Number(): number;
/**
* Reads a zig-zag encoded varint as a signed 64 bit value
*/
sint64String(): string;
/**
* Reads fixed 64 bits
*/
fixed64(): bigint;
/**
* Reads fixed 64 bits
*/
fixed64Number(): number;
/**
* Reads fixed 64 bits
*/
fixed64String(): string;
/**
* Reads zig-zag encoded fixed 64 bits
*/
sfixed64(): bigint;
/**
* Reads zig-zag encoded fixed 64 bits
*/
sfixed64Number(): number;
/**
* Reads zig-zag encoded fixed 64 bits
*/
sfixed64String(): string;
}
/**
* This will be removed in a future release
*
* @deprecated
*/
export declare class CodeError extends Error {
code: string;
constructor(message: string, code: string);
}
/**
* Thrown when a repeated field has too many elements
*/
export declare class MaxLengthError extends Error {
/**
* This will be removed in a future release
*
* @deprecated use the `.name` property instead
*/
code: string;
name: string;
}
/**
* Thrown when a map has too many elements
*/
export declare class MaxSizeError extends Error {
/**
* This will be removed in a future release
*
* @deprecated use the `.name` property instead
*/
code: string;
name: string;
}
export declare class ParseError extends Error {
/**
* This will be removed in a future release
*
* @deprecated use the `.name` property instead
*/
code: string;
name: string;
}
export declare class NoMessagesFoundError extends Error {
/**
* This will be removed in a future release
*
* @deprecated use the `.name` property instead
*/
code: string;
name: string;
}
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAEvC,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;IACjB,QAAQ,CAAC,EAAE,IAAI,CAAA;IACf,OAAO,CAAC,EAAE,IAAI,CAAA;IACd,MAAM,CAAC,EAAE,IAAI,CAAA;CACd;AAED,OAAO,EACL,aAAa,EACd,MAAM,aAAa,CAAA;AAEpB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,IAAI,EAAE,MAAM,CAAA;gBAEN,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAK3C;AAED;;GAEG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC;;;;OAIG;IACI,IAAI,SAAmB;IACvB,IAAI,SAAmB;CAC/B;AAED;;GAEG;AACH,qBAAa,YAAa,SAAQ,KAAK;IACrC;;;;OAIG;IACI,IAAI,SAAiB;IACrB,IAAI,SAAiB;CAC7B;AAED,qBAAa,UAAW,SAAQ,KAAK;IACnC;;;;OAIG;IACI,IAAI,SAAoB;IACxB,IAAI,SAAe;CAC3B;AAED,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C;;;;OAIG;IACI,IAAI,SAA0B;IAC9B,IAAI,SAAyB;CACrC"}
+72
View File
@@ -0,0 +1,72 @@
/**
* @packageDocumentation
*
* This module contains serialization/deserialization code used when encoding/decoding protobufs.
*
* It should be declared as a dependency of your project:
*
* ```console
* npm i protons-runtime
* ```
*/
export { decodeMessage } from './decode.js';
export { encodeMessage } from './encode.js';
export { enumeration } from './codecs/enum.js';
export { message } from './codecs/message.js';
export { createReader as reader } from './utils/reader.js';
export { createWriter as writer } from './utils/writer.js';
/**
* This will be removed in a future release
*
* @deprecated
*/
export class CodeError extends Error {
code;
constructor(message, code) {
super(message);
this.code = code;
}
}
/**
* Thrown when a repeated field has too many elements
*/
export class MaxLengthError extends Error {
/**
* This will be removed in a future release
*
* @deprecated use the `.name` property instead
*/
code = 'ERR_MAX_LENGTH';
name = 'MaxLengthError';
}
/**
* Thrown when a map has too many elements
*/
export class MaxSizeError extends Error {
/**
* This will be removed in a future release
*
* @deprecated use the `.name` property instead
*/
code = 'ERR_MAX_SIZE';
name = 'MaxSizeError';
}
export class ParseError extends Error {
/**
* This will be removed in a future release
*
* @deprecated use the `.name` property instead
*/
code = 'ERR_PARSE_ERROR';
name = 'ParseError';
}
export class NoMessagesFoundError extends Error {
/**
* This will be removed in a future release
*
* @deprecated use the `.name` property instead
*/
code = 'ERR_NO_MESSAGES_FOUND';
name = 'NoMessagesFoundError';
}
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAYH,OAAO,EACL,aAAa,EACd,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,aAAa,EACd,MAAM,aAAa,CAAA;AAEpB,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,YAAY,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,EAAE,YAAY,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAoT1D;;;;GAIG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IAC3B,IAAI,CAAQ;IAEnB,YAAa,OAAe,EAAE,IAAY;QACxC,KAAK,CAAC,OAAO,CAAC,CAAA;QAEd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,KAAK;IACvC;;;;OAIG;IACI,IAAI,GAAG,gBAAgB,CAAA;IACvB,IAAI,GAAG,gBAAgB,CAAA;CAC/B;AAED;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC;;;;OAIG;IACI,IAAI,GAAG,cAAc,CAAA;IACrB,IAAI,GAAG,cAAc,CAAA;CAC7B;AAED,MAAM,OAAO,UAAW,SAAQ,KAAK;IACnC;;;;OAIG;IACI,IAAI,GAAG,iBAAiB,CAAA;IACxB,IAAI,GAAG,YAAY,CAAA;CAC3B;AAED,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC7C;;;;OAIG;IACI,IAAI,GAAG,uBAAuB,CAAA;IAC9B,IAAI,GAAG,sBAAsB,CAAA;CACrC"}
+33
View File
@@ -0,0 +1,33 @@
/**
* Writes a 32 bit float to a buffer using little endian byte order
*/
export declare function writeFloatLE(val: number, buf: Uint8Array, pos: number): void;
/**
* Writes a 32 bit float to a buffer using big endian byte order
*/
export declare function writeFloatBE(val: number, buf: Uint8Array, pos: number): void;
/**
* Reads a 32 bit float from a buffer using little endian byte order
*/
export declare function readFloatLE(buf: Uint8Array, pos: number): number;
/**
* Reads a 32 bit float from a buffer using big endian byte order
*/
export declare function readFloatBE(buf: Uint8Array, pos: number): number;
/**
* Writes a 64 bit double to a buffer using little endian byte order
*/
export declare function writeDoubleLE(val: number, buf: Uint8Array, pos: number): void;
/**
* Writes a 64 bit double to a buffer using big endian byte order
*/
export declare function writeDoubleBE(val: number, buf: Uint8Array, pos: number): void;
/**
* Reads a 64 bit double from a buffer using little endian byte order
*/
export declare function readDoubleLE(buf: Uint8Array, pos: number): number;
/**
* Reads a 64 bit double from a buffer using big endian byte order
*/
export declare function readDoubleBE(buf: Uint8Array, pos: number): number;
//# sourceMappingURL=float.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"float.d.ts","sourceRoot":"","sources":["../../../src/utils/float.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,wBAAgB,YAAY,CAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAM7E;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAM7E;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAMjE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAMjE;AAKD;;GAEG;AACH,wBAAgB,aAAa,CAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAU9E;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAU9E;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAUlE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAUlE"}
+101
View File
@@ -0,0 +1,101 @@
const f32 = new Float32Array([-0]);
const f8b = new Uint8Array(f32.buffer);
/**
* Writes a 32 bit float to a buffer using little endian byte order
*/
export function writeFloatLE(val, buf, pos) {
f32[0] = val;
buf[pos] = f8b[0];
buf[pos + 1] = f8b[1];
buf[pos + 2] = f8b[2];
buf[pos + 3] = f8b[3];
}
/**
* Writes a 32 bit float to a buffer using big endian byte order
*/
export function writeFloatBE(val, buf, pos) {
f32[0] = val;
buf[pos] = f8b[3];
buf[pos + 1] = f8b[2];
buf[pos + 2] = f8b[1];
buf[pos + 3] = f8b[0];
}
/**
* Reads a 32 bit float from a buffer using little endian byte order
*/
export function readFloatLE(buf, pos) {
f8b[0] = buf[pos];
f8b[1] = buf[pos + 1];
f8b[2] = buf[pos + 2];
f8b[3] = buf[pos + 3];
return f32[0];
}
/**
* Reads a 32 bit float from a buffer using big endian byte order
*/
export function readFloatBE(buf, pos) {
f8b[3] = buf[pos];
f8b[2] = buf[pos + 1];
f8b[1] = buf[pos + 2];
f8b[0] = buf[pos + 3];
return f32[0];
}
const f64 = new Float64Array([-0]);
const d8b = new Uint8Array(f64.buffer);
/**
* Writes a 64 bit double to a buffer using little endian byte order
*/
export function writeDoubleLE(val, buf, pos) {
f64[0] = val;
buf[pos] = d8b[0];
buf[pos + 1] = d8b[1];
buf[pos + 2] = d8b[2];
buf[pos + 3] = d8b[3];
buf[pos + 4] = d8b[4];
buf[pos + 5] = d8b[5];
buf[pos + 6] = d8b[6];
buf[pos + 7] = d8b[7];
}
/**
* Writes a 64 bit double to a buffer using big endian byte order
*/
export function writeDoubleBE(val, buf, pos) {
f64[0] = val;
buf[pos] = d8b[7];
buf[pos + 1] = d8b[6];
buf[pos + 2] = d8b[5];
buf[pos + 3] = d8b[4];
buf[pos + 4] = d8b[3];
buf[pos + 5] = d8b[2];
buf[pos + 6] = d8b[1];
buf[pos + 7] = d8b[0];
}
/**
* Reads a 64 bit double from a buffer using little endian byte order
*/
export function readDoubleLE(buf, pos) {
d8b[0] = buf[pos];
d8b[1] = buf[pos + 1];
d8b[2] = buf[pos + 2];
d8b[3] = buf[pos + 3];
d8b[4] = buf[pos + 4];
d8b[5] = buf[pos + 5];
d8b[6] = buf[pos + 6];
d8b[7] = buf[pos + 7];
return f64[0];
}
/**
* Reads a 64 bit double from a buffer using big endian byte order
*/
export function readDoubleBE(buf, pos) {
d8b[7] = buf[pos];
d8b[6] = buf[pos + 1];
d8b[5] = buf[pos + 2];
d8b[4] = buf[pos + 3];
d8b[3] = buf[pos + 4];
d8b[2] = buf[pos + 5];
d8b[1] = buf[pos + 6];
d8b[0] = buf[pos + 7];
return f64[0];
}
//# sourceMappingURL=float.js.map
@@ -0,0 +1 @@
{"version":3,"file":"float.js","sourceRoot":"","sources":["../../../src/utils/float.ts"],"names":[],"mappings":"AAAA,MAAM,GAAG,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAClC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AAEtC;;GAEG;AACH,MAAM,UAAU,YAAY,CAAE,GAAW,EAAE,GAAe,EAAE,GAAW;IACrE,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAA;IACZ,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;IACjB,GAAG,CAAC,G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GAAG,CAAC,CAAC,CAAA;IACrB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrB,OAAO,GAAG,CAAC,CAAC,CAAC,CAAA;AACf,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAE,GAAe,EAAE,GAAW;IACxD,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAA;IACjB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrB,OAAO,GAAG,CAAC,CAAC,CAAC,CAAA;AACf,CAAC"}
@@ -0,0 +1,54 @@
/**
* Constructs new long bits.
*
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
* @memberof util
* @function Object() { [native code] }
* @param {number} lo - Low 32 bits, unsigned
* @param {number} hi - High 32 bits, unsigned
*/
export declare class LongBits {
lo: number;
hi: number;
constructor(lo: number, hi: number);
/**
* Converts this long bits to a possibly unsafe JavaScript number
*/
toNumber(unsigned?: boolean): number;
/**
* Converts this long bits to a bigint
*/
toBigInt(unsigned?: boolean): bigint;
/**
* Converts this long bits to a string
*/
toString(unsigned?: boolean): string;
/**
* Zig-zag encodes this long bits
*/
zzEncode(): this;
/**
* Zig-zag decodes this long bits
*/
zzDecode(): this;
/**
* Calculates the length of this longbits when encoded as a varint.
*/
length(): number;
/**
* Constructs new long bits from the specified number
*/
static fromBigInt(value: bigint): LongBits;
/**
* Constructs new long bits from the specified number
*/
static fromNumber(value: number): LongBits;
/**
* Constructs new long bits from a number, long or string
*/
static from(value: bigint | number | string | {
low: number;
high: number;
}): LongBits;
}
//# sourceMappingURL=longbits.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"longbits.d.ts","sourceRoot":"","sources":["../../../src/utils/longbits.ts"],"names":[],"mappings":"AAIA;;;;;;;;GAQG;AACH,qBAAa,QAAQ;IACZ,EAAE,EAAE,MAAM,CAAA;IACV,EAAE,EAAE,MAAM,CAAA;gBAEJ,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAenC;;OAEG;IACH,QAAQ,CAAE,QAAQ,GAAE,OAAe,GAAG,MAAM;IAY5C;;OAEG;IACH,QAAQ,CAAE,QAAQ,GAAE,OAAe,GAAG,MAAM;IAiB5C;;OAEG;IACH,QAAQ,CAAE,QAAQ,GAAE,OAAe,GAAG,MAAM;IAI5C;;OAEG;IACH,QAAQ,IAAK,IAAI;IAOjB;;OAEG;IACH,QAAQ,IAAK,IAAI;IAOjB;;OAEG;IACH,MAAM,IAAK,MAAM;IAejB;;OAEG;IACH,MAAM,CAAC,UAAU,CAAE,KAAK,EAAE,MAAM,GAAG,QAAQ;IA+B3C;;OAEG;IACH,MAAM,CAAC,UAAU,CAAE,KAAK,EAAE,MAAM,GAAG,QAAQ;IAiB3C;;OAEG;IACH,MAAM,CAAC,IAAI,CAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,QAAQ;CAYxF"}
+174
View File
@@ -0,0 +1,174 @@
// the largest BigInt we can safely downcast to a Number
const MAX_SAFE_NUMBER_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
const MIN_SAFE_NUMBER_INTEGER = BigInt(Number.MIN_SAFE_INTEGER);
/**
* Constructs new long bits.
*
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
* @memberof util
* @function Object() { [native code] }
* @param {number} lo - Low 32 bits, unsigned
* @param {number} hi - High 32 bits, unsigned
*/
export class LongBits {
lo;
hi;
constructor(lo, hi) {
// note that the casts below are theoretically unnecessary as of today, but older statically
// generated converter code might still call the ctor with signed 32bits. kept for compat.
/**
* Low bits
*/
this.lo = lo | 0;
/**
* High bits
*/
this.hi = hi | 0;
}
/**
* Converts this long bits to a possibly unsafe JavaScript number
*/
toNumber(unsigned = false) {
if (!unsigned && (this.hi >>> 31) > 0) {
const lo = ~this.lo + 1 >>> 0;
let hi = ~this.hi >>> 0;
if (lo === 0) {
hi = hi + 1 >>> 0;
}
return -(lo + hi * 4294967296);
}
return this.lo + this.hi * 4294967296;
}
/**
* Converts this long bits to a bigint
*/
toBigInt(unsigned = false) {
if (unsigned) {
return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n);
}
if ((this.hi >>> 31) !== 0) {
const lo = ~this.lo + 1 >>> 0;
let hi = ~this.hi >>> 0;
if (lo === 0) {
hi = hi + 1 >>> 0;
}
return -(BigInt(lo) + (BigInt(hi) << 32n));
}
return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n);
}
/**
* Converts this long bits to a string
*/
toString(unsigned = false) {
return this.toBigInt(unsigned).toString();
}
/**
* Zig-zag encodes this long bits
*/
zzEncode() {
const mask = this.hi >> 31;
this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
this.lo = (this.lo << 1 ^ mask) >>> 0;
return this;
}
/**
* Zig-zag decodes this long bits
*/
zzDecode() {
const mask = -(this.lo & 1);
this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
this.hi = (this.hi >>> 1 ^ mask) >>> 0;
return this;
}
/**
* Calculates the length of this longbits when encoded as a varint.
*/
length() {
const part0 = this.lo;
const part1 = (this.lo >>> 28 | this.hi << 4) >>> 0;
const part2 = this.hi >>> 24;
return part2 === 0
? part1 === 0
? part0 < 16384
? part0 < 128 ? 1 : 2
: part0 < 2097152 ? 3 : 4
: part1 < 16384
? part1 < 128 ? 5 : 6
: part1 < 2097152 ? 7 : 8
: part2 < 128 ? 9 : 10;
}
/**
* Constructs new long bits from the specified number
*/
static fromBigInt(value) {
if (value === 0n) {
return zero;
}
if (value < MAX_SAFE_NUMBER_INTEGER && value > MIN_SAFE_NUMBER_INTEGER) {
return this.fromNumber(Number(value));
}
const negative = value < 0n;
if (negative) {
value = -value;
}
let hi = value >> 32n;
let lo = value - (hi << 32n);
if (negative) {
hi = ~hi | 0n;
lo = ~lo | 0n;
if (++lo > TWO_32) {
lo = 0n;
if (++hi > TWO_32) {
hi = 0n;
}
}
}
return new LongBits(Number(lo), Number(hi));
}
/**
* Constructs new long bits from the specified number
*/
static fromNumber(value) {
if (value === 0) {
return zero;
}
const sign = value < 0;
if (sign) {
value = -value;
}
let lo = value >>> 0;
let hi = (value - lo) / 4294967296 >>> 0;
if (sign) {
hi = ~hi >>> 0;
lo = ~lo >>> 0;
if (++lo > 4294967295) {
lo = 0;
if (++hi > 4294967295) {
hi = 0;
}
}
}
return new LongBits(lo, hi);
}
/**
* Constructs new long bits from a number, long or string
*/
static from(value) {
if (typeof value === 'number') {
return LongBits.fromNumber(value);
}
if (typeof value === 'bigint') {
return LongBits.fromBigInt(value);
}
if (typeof value === 'string') {
return LongBits.fromBigInt(BigInt(value));
}
return value.low != null || value.high != null ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
}
}
const zero = new LongBits(0, 0);
zero.toBigInt = function () { return 0n; };
zero.zzEncode = zero.zzDecode = function () { return this; };
zero.length = function () { return 1; };
const TWO_32 = 4294967296n;
//# sourceMappingURL=longbits.js.map
File diff suppressed because one or more lines are too long
+5
View File
@@ -0,0 +1,5 @@
/**
* A general purpose buffer pool
*/
export default function pool(size?: number): (size: number) => Uint8Array;
//# sourceMappingURL=pool.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../../../src/utils/pool.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,IAAI,CAAE,IAAI,CAAC,EAAE,MAAM,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,UAAU,CAwBzE"}
+26
View File
@@ -0,0 +1,26 @@
import { allocUnsafe } from 'uint8arrays/alloc';
/**
* A general purpose buffer pool
*/
export default function pool(size) {
const SIZE = size ?? 8192;
const MAX = SIZE >>> 1;
let slab;
let offset = SIZE;
return function poolAlloc(size) {
if (size < 1 || size > MAX) {
return allocUnsafe(size);
}
if (offset + size > SIZE) {
slab = allocUnsafe(SIZE);
offset = 0;
}
const buf = slab.subarray(offset, offset += size);
if ((offset & 7) !== 0) {
// align to 32 bit
offset = (offset | 7) + 1;
}
return buf;
};
}
//# sourceMappingURL=pool.js.map
@@ -0,0 +1 @@
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../../../src/utils/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAE/C;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,IAAI,CAAE,IAAa;IACzC,MAAM,IAAI,GAAG,IAAI,IAAI,IAAI,CAAA;IACzB,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,CAAA;IACtB,IAAI,IAAgB,CAAA;IACpB,IAAI,MAAM,GAAG,IAAI,CAAA;IACjB,OAAO,SAAS,SAAS,CAAE,IAAY;QACrC,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,GAAG,EAAE,CAAC;YAC3B,OAAO,WAAW,CAAC,IAAI,CAAC,CAAA;QAC1B,CAAC;QAED,IAAI,MAAM,GAAG,IAAI,GAAG,IAAI,EAAE,CAAC;YACzB,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,CAAA;YACxB,MAAM,GAAG,CAAC,CAAA;QACZ,CAAC;QAED,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,IAAI,IAAI,CAAC,CAAA;QAEjD,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,kBAAkB;YAClB,MAAM,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;QAC3B,CAAC;QAED,OAAO,GAAG,CAAA;IACZ,CAAC,CAAA;AACH,CAAC"}
+129
View File
@@ -0,0 +1,129 @@
import type { Reader } from '../index.js';
import type { Uint8ArrayList } from 'uint8arraylist';
/**
* Constructs a new reader instance using the specified buffer.
*/
export declare class Uint8ArrayReader implements Reader {
buf: Uint8Array;
pos: number;
len: number;
_slice: (begin?: number, end?: number) => Uint8Array<ArrayBufferLike>;
constructor(buffer: Uint8Array);
/**
* Reads a varint as an unsigned 32 bit value
*/
uint32(): number;
/**
* Reads a varint as a signed 32 bit value
*/
int32(): number;
/**
* Reads a zig-zag encoded varint as a signed 32 bit value
*/
sint32(): number;
/**
* Reads a varint as a boolean
*/
bool(): boolean;
/**
* Reads fixed 32 bits as an unsigned 32 bit integer
*/
fixed32(): number;
/**
* Reads fixed 32 bits as a signed 32 bit integer
*/
sfixed32(): number;
/**
* Reads a float (32 bit) as a number
*/
float(): number;
/**
* Reads a double (64 bit float) as a number
*/
double(): number;
/**
* Reads a sequence of bytes preceded by its length as a varint
*/
bytes(): Uint8Array;
/**
* Reads a string preceded by its byte length as a varint
*/
string(): string;
/**
* Skips the specified number of bytes if specified, otherwise skips a varint
*/
skip(length?: number): this;
/**
* Skips the next element of the specified wire type
*/
skipType(wireType: number): this;
private readLongVarint;
private readFixed64;
/**
* Reads a varint as a signed 64 bit value
*/
int64(): bigint;
/**
* Reads a varint as a signed 64 bit value returned as a possibly unsafe
* JavaScript number
*/
int64Number(): number;
/**
* Reads a varint as a signed 64 bit value returned as a string
*/
int64String(): string;
/**
* Reads a varint as an unsigned 64 bit value
*/
uint64(): bigint;
/**
* Reads a varint as an unsigned 64 bit value returned as a possibly unsafe
* JavaScript number
*/
uint64Number(): number;
/**
* Reads a varint as an unsigned 64 bit value returned as a string
*/
uint64String(): string;
/**
* Reads a zig-zag encoded varint as a signed 64 bit value
*/
sint64(): bigint;
/**
* Reads a zig-zag encoded varint as a signed 64 bit value returned as a
* possibly unsafe JavaScript number
*/
sint64Number(): number;
/**
* Reads a zig-zag encoded varint as a signed 64 bit value returned as a
* string
*/
sint64String(): string;
/**
* Reads fixed 64 bits
*/
fixed64(): bigint;
/**
* Reads fixed 64 bits returned as a possibly unsafe JavaScript number
*/
fixed64Number(): number;
/**
* Reads fixed 64 bits returned as a string
*/
fixed64String(): string;
/**
* Reads zig-zag encoded fixed 64 bits
*/
sfixed64(): bigint;
/**
* Reads zig-zag encoded fixed 64 bits returned as a possibly unsafe
* JavaScript number
*/
sfixed64Number(): number;
/**
* Reads zig-zag encoded fixed 64 bits returned as a string
*/
sfixed64String(): string;
}
export declare function createReader(buf: Uint8Array | Uint8ArrayList): Reader;
//# sourceMappingURL=reader.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"reader.d.ts","sourceRoot":"","sources":["../../../src/utils/reader.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAcpD;;GAEG;AACH,qBAAa,gBAAiB,YAAW,MAAM;IACtC,GAAG,EAAE,UAAU,CAAA;IACf,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IAEX,MAAM,gEAAgC;gBAEhC,MAAM,EAAE,UAAU;IAiB/B;;OAEG;IACH,MAAM,IAAK,MAAM;IAiBjB;;OAEG;IACH,KAAK,IAAK,MAAM;IAIhB;;OAEG;IACH,MAAM,IAAK,MAAM;IAKjB;;OAEG;IACH,IAAI,IAAK,OAAO;IAIhB;;OAEG;IACH,OAAO,IAAK,MAAM;IAQlB;;OAEG;IACH,QAAQ,IAAK,MAAM;IAUnB;;OAEG;IACH,KAAK,IAAK,MAAM;IAUhB;;OAEG;IACH,MAAM,IAAK,MAAM;IASjB;;OAEG;IACH,KAAK,IAAK,UAAU;IAiBpB;;OAEG;IACH,MAAM,IAAK,MAAM;IAKjB;;OAEG;IACH,IAAI,CAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAgB5B;;OAEG;IACH,QAAQ,CAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IA2BjC,OAAO,CAAC,cAAc;IAgDtB,OAAO,CAAC,WAAW;IAWnB;;OAEG;IACH,KAAK,IAAK,MAAM;IAIhB;;;OAGG;IACH,WAAW,IAAK,MAAM;IAItB;;OAEG;IACH,WAAW,IAAK,MAAM;IAItB;;OAEG;IACH,MAAM,IAAK,MAAM;IAIjB;;;OAGG;IACH,YAAY,IAAK,MAAM;IAMvB;;OAEG;IACH,YAAY,IAAK,MAAM;IAIvB;;OAEG;IACH,MAAM,IAAK,MAAM;IAIjB;;;OAGG;IACH,YAAY,IAAK,MAAM;IAIvB;;;OAGG;IACH,YAAY,IAAK,MAAM;IAIvB;;OAEG;IACH,OAAO,IAAK,MAAM;IAIlB;;OAEG;IACH,aAAa,IAAK,MAAM;IAIxB;;OAEG;IACH,aAAa,IAAK,MAAM;IAIxB;;OAEG;IACH,QAAQ,IAAK,MAAM;IAInB;;;OAGG;IACH,cAAc,IAAK,MAAM;IAIzB;;OAEG;IACH,cAAc,IAAK,MAAM;CAG1B;AAED,wBAAgB,YAAY,CAAE,GAAG,EAAE,UAAU,GAAG,cAAc,GAAG,MAAM,CAEtE"}
+370
View File
@@ -0,0 +1,370 @@
import { decodeUint8Array, encodingLength } from 'uint8-varint';
import { readFloatLE, readDoubleLE } from './float.js';
import { LongBits } from './longbits.js';
import * as utf8 from './utf8.js';
/* istanbul ignore next */
function indexOutOfRange(reader, writeLength) {
return RangeError(`index out of range: ${reader.pos} + ${writeLength ?? 1} > ${reader.len}`);
}
function readFixed32End(buf, end) {
return (buf[end - 4] |
buf[end - 3] << 8 |
buf[end - 2] << 16 |
buf[end - 1] << 24) >>> 0;
}
/**
* Constructs a new reader instance using the specified buffer.
*/
export class Uint8ArrayReader {
buf;
pos;
len;
_slice = Uint8Array.prototype.subarray;
constructor(buffer) {
/**
* Read buffer
*/
this.buf = buffer;
/**
* Read buffer position
*/
this.pos = 0;
/**
* Read buffer length
*/
this.len = buffer.length;
}
/**
* Reads a varint as an unsigned 32 bit value
*/
uint32() {
let value = 4294967295;
value = (this.buf[this.pos] & 127) >>> 0;
if (this.buf[this.pos++] < 128) {
return value;
}
value = (value | (this.buf[this.pos] & 127) << 7) >>> 0;
if (this.buf[this.pos++] < 128) {
return value;
}
value = (value | (this.buf[this.pos] & 127) << 14) >>> 0;
if (this.buf[this.pos++] < 128) {
return value;
}
value = (value | (this.buf[this.pos] & 127) << 21) >>> 0;
if (this.buf[this.pos++] < 128) {
return value;
}
value = (value | (this.buf[this.pos] & 15) << 28) >>> 0;
if (this.buf[this.pos++] < 128) {
return value;
}
if ((this.pos += 5) > this.len) {
this.pos = this.len;
throw indexOutOfRange(this, 10);
}
return value;
}
/**
* Reads a varint as a signed 32 bit value
*/
int32() {
return this.uint32() | 0;
}
/**
* Reads a zig-zag encoded varint as a signed 32 bit value
*/
sint32() {
const value = this.uint32();
return value >>> 1 ^ -(value & 1) | 0;
}
/**
* Reads a varint as a boolean
*/
bool() {
return this.uint32() !== 0;
}
/**
* Reads fixed 32 bits as an unsigned 32 bit integer
*/
fixed32() {
if (this.pos + 4 > this.len) {
throw indexOutOfRange(this, 4);
}
const res = readFixed32End(this.buf, this.pos += 4);
return res;
}
/**
* Reads fixed 32 bits as a signed 32 bit integer
*/
sfixed32() {
if (this.pos + 4 > this.len) {
throw indexOutOfRange(this, 4);
}
const res = readFixed32End(this.buf, this.pos += 4) | 0;
return res;
}
/**
* Reads a float (32 bit) as a number
*/
float() {
if (this.pos + 4 > this.len) {
throw indexOutOfRange(this, 4);
}
const value = readFloatLE(this.buf, this.pos);
this.pos += 4;
return value;
}
/**
* Reads a double (64 bit float) as a number
*/
double() {
/* istanbul ignore if */
if (this.pos + 8 > this.len) {
throw indexOutOfRange(this, 4);
}
const value = readDoubleLE(this.buf, this.pos);
this.pos += 8;
return value;
}
/**
* Reads a sequence of bytes preceded by its length as a varint
*/
bytes() {
const length = this.uint32();
const start = this.pos;
const end = this.pos + length;
/* istanbul ignore if */
if (end > this.len) {
throw indexOutOfRange(this, length);
}
this.pos += length;
return start === end // fix for IE 10/Win8 and others' subarray returning array of size 1
? new Uint8Array(0)
: this.buf.subarray(start, end);
}
/**
* Reads a string preceded by its byte length as a varint
*/
string() {
const bytes = this.bytes();
return utf8.read(bytes, 0, bytes.length);
}
/**
* Skips the specified number of bytes if specified, otherwise skips a varint
*/
skip(length) {
if (typeof length === 'number') {
/* istanbul ignore if */
if (this.pos + length > this.len) {
throw indexOutOfRange(this, length);
}
this.pos += length;
}
else {
do {
/* istanbul ignore if */
if (this.pos >= this.len) {
throw indexOutOfRange(this);
}
} while ((this.buf[this.pos++] & 128) !== 0);
}
return this;
}
/**
* Skips the next element of the specified wire type
*/
skipType(wireType) {
switch (wireType) {
case 0:
this.skip();
break;
case 1:
this.skip(8);
break;
case 2:
this.skip(this.uint32());
break;
case 3:
while ((wireType = this.uint32() & 7) !== 4) {
this.skipType(wireType);
}
break;
case 5:
this.skip(4);
break;
/* istanbul ignore next */
default:
throw Error(`invalid wire type ${wireType} at offset ${this.pos}`);
}
return this;
}
readLongVarint() {
// tends to deopt with local vars for octet etc.
const bits = new LongBits(0, 0);
let i = 0;
if (this.len - this.pos > 4) { // fast route (lo)
for (; i < 4; ++i) {
// 1st..4th
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
if (this.buf[this.pos++] < 128) {
return bits;
}
}
// 5th
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
if (this.buf[this.pos++] < 128) {
return bits;
}
i = 0;
}
else {
for (; i < 3; ++i) {
/* istanbul ignore if */
if (this.pos >= this.len) {
throw indexOutOfRange(this);
}
// 1st..3th
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
if (this.buf[this.pos++] < 128) {
return bits;
}
}
// 4th
bits.lo = (bits.lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0;
return bits;
}
if (this.len - this.pos > 4) { // fast route (hi)
for (; i < 5; ++i) {
// 6th..10th
bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
if (this.buf[this.pos++] < 128) {
return bits;
}
}
}
else {
for (; i < 5; ++i) {
if (this.pos >= this.len) {
throw indexOutOfRange(this);
}
// 6th..10th
bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
if (this.buf[this.pos++] < 128) {
return bits;
}
}
}
throw Error('invalid varint encoding');
}
readFixed64() {
if (this.pos + 8 > this.len) {
throw indexOutOfRange(this, 8);
}
const lo = readFixed32End(this.buf, this.pos += 4);
const hi = readFixed32End(this.buf, this.pos += 4);
return new LongBits(lo, hi);
}
/**
* Reads a varint as a signed 64 bit value
*/
int64() {
return this.readLongVarint().toBigInt();
}
/**
* Reads a varint as a signed 64 bit value returned as a possibly unsafe
* JavaScript number
*/
int64Number() {
return this.readLongVarint().toNumber();
}
/**
* Reads a varint as a signed 64 bit value returned as a string
*/
int64String() {
return this.readLongVarint().toString();
}
/**
* Reads a varint as an unsigned 64 bit value
*/
uint64() {
return this.readLongVarint().toBigInt(true);
}
/**
* Reads a varint as an unsigned 64 bit value returned as a possibly unsafe
* JavaScript number
*/
uint64Number() {
const value = decodeUint8Array(this.buf, this.pos);
this.pos += encodingLength(value);
return value;
}
/**
* Reads a varint as an unsigned 64 bit value returned as a string
*/
uint64String() {
return this.readLongVarint().toString(true);
}
/**
* Reads a zig-zag encoded varint as a signed 64 bit value
*/
sint64() {
return this.readLongVarint().zzDecode().toBigInt();
}
/**
* Reads a zig-zag encoded varint as a signed 64 bit value returned as a
* possibly unsafe JavaScript number
*/
sint64Number() {
return this.readLongVarint().zzDecode().toNumber();
}
/**
* Reads a zig-zag encoded varint as a signed 64 bit value returned as a
* string
*/
sint64String() {
return this.readLongVarint().zzDecode().toString();
}
/**
* Reads fixed 64 bits
*/
fixed64() {
return this.readFixed64().toBigInt();
}
/**
* Reads fixed 64 bits returned as a possibly unsafe JavaScript number
*/
fixed64Number() {
return this.readFixed64().toNumber();
}
/**
* Reads fixed 64 bits returned as a string
*/
fixed64String() {
return this.readFixed64().toString();
}
/**
* Reads zig-zag encoded fixed 64 bits
*/
sfixed64() {
return this.readFixed64().toBigInt();
}
/**
* Reads zig-zag encoded fixed 64 bits returned as a possibly unsafe
* JavaScript number
*/
sfixed64Number() {
return this.readFixed64().toNumber();
}
/**
* Reads zig-zag encoded fixed 64 bits returned as a string
*/
sfixed64String() {
return this.readFixed64().toString();
}
}
export function createReader(buf) {
return new Uint8ArrayReader(buf instanceof Uint8Array ? buf : buf.subarray());
}
//# sourceMappingURL=reader.js.map
File diff suppressed because one or more lines are too long
+13
View File
@@ -0,0 +1,13 @@
/**
* Calculates the UTF8 byte length of a string
*/
export declare function length(string: string): number;
/**
* Reads UTF8 bytes as a string
*/
export declare function read(buffer: Uint8Array, start: number, end: number): string;
/**
* Writes a string as UTF8 bytes
*/
export declare function write(string: string, buffer: Uint8Array, offset: number): number;
//# sourceMappingURL=utf8.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"utf8.d.ts","sourceRoot":"","sources":["../../../src/utils/utf8.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,MAAM,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAmB9C;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAE,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CA0C5E;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CA4BjF"}
+98
View File
@@ -0,0 +1,98 @@
/**
* Calculates the UTF8 byte length of a string
*/
export function length(string) {
let len = 0;
let c = 0;
for (let i = 0; i < string.length; ++i) {
c = string.charCodeAt(i);
if (c < 128) {
len += 1;
}
else if (c < 2048) {
len += 2;
}
else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
++i;
len += 4;
}
else {
len += 3;
}
}
return len;
}
/**
* Reads UTF8 bytes as a string
*/
export function read(buffer, start, end) {
const len = end - start;
if (len < 1) {
return '';
}
let parts;
const chunk = [];
let i = 0; // char offset
let t; // temporary
while (start < end) {
t = buffer[start++];
if (t < 128) {
chunk[i++] = t;
}
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) - 0x10000;
chunk[i++] = 0xD800 + (t >> 10);
chunk[i++] = 0xDC00 + (t & 1023);
}
else {
chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
}
if (i > 8191) {
(parts ?? (parts = [])).push(String.fromCharCode.apply(String, chunk));
i = 0;
}
}
if (parts != null) {
if (i > 0) {
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
}
return parts.join('');
}
return String.fromCharCode.apply(String, chunk.slice(0, i));
}
/**
* Writes a string as UTF8 bytes
*/
export function write(string, buffer, offset) {
const start = offset;
let c1; // character 1
let c2; // character 2
for (let i = 0; i < string.length; ++i) {
c1 = string.charCodeAt(i);
if (c1 < 128) {
buffer[offset++] = c1;
}
else if (c1 < 2048) {
buffer[offset++] = c1 >> 6 | 192;
buffer[offset++] = c1 & 63 | 128;
}
else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
++i;
buffer[offset++] = c1 >> 18 | 240;
buffer[offset++] = c1 >> 12 & 63 | 128;
buffer[offset++] = c1 >> 6 & 63 | 128;
buffer[offset++] = c1 & 63 | 128;
}
else {
buffer[offset++] = c1 >> 12 | 224;
buffer[offset++] = c1 >> 6 & 63 | 128;
buffer[offset++] = c1 & 63 | 128;
}
}
return offset - start;
}
//# sourceMappingURL=utf8.js.map
@@ -0,0 +1 @@
{"version":3,"file":"utf8.js","sourceRoot":"","sources":["../../../src/utils/utf8.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,MAAM,CAAE,MAAc;IACpC,IAAI,GAAG,GAAG,CAAC,CAAA;IACX,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC;QACvC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QAExB,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;YACZ,GAAG,IAAI,CAAC,CAAA;QACV,CAAC;aAAM,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC;YACpB,GAAG,IAAI,CAAC,CAAA;QACV,CAAC;aAAM,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,MAAM,EAAE,CAAC;YACrF,EAAE,CAAC,CAAA;YACH,GAAG,IAAI,CAAC,CAAA;QACV,CAAC;aAAM,CAAC;YACN,GAAG,IAAI,CA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,EAAE,GAAG,GAAG,CAAA;YACjC,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAA;YACrC,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;QAClC,CAAC;IACH,CAAC;IAED,OAAO,MAAM,GAAG,KAAK,CAAA;AACvB,CAAC"}
@@ -0,0 +1,6 @@
import type { Writer } from '../index.js';
/**
* Creates a new writer
*/
export declare function createWriter(): Writer;
//# sourceMappingURL=writer.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../../../src/utils/writer.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAgfzC;;GAEG;AACH,wBAAgB,YAAY,IAAK,MAAM,CAEtC"}
+436
View File
@@ -0,0 +1,436 @@
import { encodeUint8Array, encodingLength } from 'uint8-varint';
import { allocUnsafe } from 'uint8arrays/alloc';
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
import { writeFloatLE, writeDoubleLE } from './float.js';
import { LongBits } from './longbits.js';
import pool from './pool.js';
import * as utf8 from './utf8.js';
/**
* Constructs a new writer operation instance.
*
* @classdesc Scheduled writer operation
*/
class Op {
/**
* Function to call
*/
fn;
/**
* Value byte length
*/
len;
/**
* Next operation
*/
next;
/**
* Value to write
*/
val;
constructor(fn, len, val) {
this.fn = fn;
this.len = len;
this.next = undefined;
this.val = val; // type varies
}
}
/* istanbul ignore next */
function noop() { }
/**
* Constructs a new writer state instance
*/
class State {
/**
* Current head
*/
head;
/**
* Current tail
*/
tail;
/**
* Current buffer length
*/
len;
/**
* Next state
*/
next;
constructor(writer) {
this.head = writer.head;
this.tail = writer.tail;
this.len = writer.len;
this.next = writer.states;
}
}
const bufferPool = pool();
/**
* Allocates a buffer of the specified size
*/
function alloc(size) {
if (globalThis.Buffer != null) {
return allocUnsafe(size);
}
return bufferPool(size);
}
/**
* When a value is written, the writer calculates its byte length and puts it into a linked
* list of operations to perform when finish() is called. This both allows us to allocate
* buffers of the exact required size and reduces the amount of work we have to do compared
* to first calculating over objects and then encoding over objects. In our case, the encoding
* part is just a linked list walk calling operations with already prepared values.
*/
class Uint8ArrayWriter {
/**
* Current length
*/
len;
/**
* Operations head
*/
head;
/**
* Operations tail
*/
tail;
/**
* Linked forked states
*/
states;
constructor() {
this.len = 0;
this.head = new Op(noop, 0, 0);
this.tail = this.head;
this.states = null;
}
/**
* Pushes a new operation to the queue
*/
_push(fn, len, val) {
this.tail = this.tail.next = new Op(fn, len, val);
this.len += len;
return this;
}
/**
* Writes an unsigned 32 bit value as a varint
*/
uint32(value) {
// here, the call to this.push has been inlined and a varint specific Op subclass is used.
// uint32 is by far the most frequently used operation and benefits significantly from this.
this.len += (this.tail = this.tail.next = new VarintOp((value = value >>> 0) <
128
? 1
: value < 16384
? 2
: value < 2097152
? 3
: value < 268435456
? 4
: 5, value)).len;
return this;
}
/**
* Writes a signed 32 bit value as a varint`
*/
int32(value) {
return value < 0
? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
: this.uint32(value);
}
/**
* Writes a 32 bit value as a varint, zig-zag encoded
*/
sint32(value) {
return this.uint32((value << 1 ^ value >> 31) >>> 0);
}
/**
* Writes an unsigned 64 bit value as a varint
*/
uint64(value) {
const bits = LongBits.fromBigInt(value);
return this._push(writeVarint64, bits.length(), bits);
}
/**
* Writes an unsigned 64 bit value as a varint
*/
uint64Number(value) {
return this._push(encodeUint8Array, encodingLength(value), value);
}
/**
* Writes an unsigned 64 bit value as a varint
*/
uint64String(value) {
return this.uint64(BigInt(value));
}
/**
* Writes a signed 64 bit value as a varint
*/
int64(value) {
return this.uint64(value);
}
/**
* Writes a signed 64 bit value as a varint
*/
int64Number(value) {
return this.uint64Number(value);
}
/**
* Writes a signed 64 bit value as a varint
*/
int64String(value) {
return this.uint64String(value);
}
/**
* Writes a signed 64 bit value as a varint, zig-zag encoded
*/
sint64(value) {
const bits = LongBits.fromBigInt(value).zzEncode();
return this._push(writeVarint64, bits.length(), bits);
}
/**
* Writes a signed 64 bit value as a varint, zig-zag encoded
*/
sint64Number(value) {
const bits = LongBits.fromNumber(value).zzEncode();
return this._push(writeVarint64, bits.length(), bits);
}
/**
* Writes a signed 64 bit value as a varint, zig-zag encoded
*/
sint64String(value) {
return this.sint64(BigInt(value));
}
/**
* Writes a boolish value as a varint
*/
bool(value) {
return this._push(writeByte, 1, value ? 1 : 0);
}
/**
* Writes an unsigned 32 bit value as fixed 32 bits
*/
fixed32(value) {
return this._push(writeFixed32, 4, value >>> 0);
}
/**
* Writes a signed 32 bit value as fixed 32 bits
*/
sfixed32(value) {
return this.fixed32(value);
}
/**
* Writes an unsigned 64 bit value as fixed 64 bits
*/
fixed64(value) {
const bits = LongBits.fromBigInt(value);
return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
}
/**
* Writes an unsigned 64 bit value as fixed 64 bits
*/
fixed64Number(value) {
const bits = LongBits.fromNumber(value);
return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
}
/**
* Writes an unsigned 64 bit value as fixed 64 bits
*/
fixed64String(value) {
return this.fixed64(BigInt(value));
}
/**
* Writes a signed 64 bit value as fixed 64 bits
*/
sfixed64(value) {
return this.fixed64(value);
}
/**
* Writes a signed 64 bit value as fixed 64 bits
*/
sfixed64Number(value) {
return this.fixed64Number(value);
}
/**
* Writes a signed 64 bit value as fixed 64 bits
*/
sfixed64String(value) {
return this.fixed64String(value);
}
/**
* Writes a float (32 bit)
*/
float(value) {
return this._push(writeFloatLE, 4, value);
}
/**
* Writes a double (64 bit float).
*
* @function
* @param {number} value - Value to write
* @returns {Writer} `this`
*/
double(value) {
return this._push(writeDoubleLE, 8, value);
}
/**
* Writes a sequence of bytes
*/
bytes(value) {
const len = value.length >>> 0;
if (len === 0) {
return this._push(writeByte, 1, 0);
}
return this.uint32(len)._push(writeBytes, len, value);
}
/**
* Writes a string
*/
string(value) {
const len = utf8.length(value);
return len !== 0
? this.uint32(len)._push(utf8.write, len, value)
: this._push(writeByte, 1, 0);
}
/**
* Forks this writer's state by pushing it to a stack.
* Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
*/
fork() {
this.states = new State(this);
this.head = this.tail = new Op(noop, 0, 0);
this.len = 0;
return this;
}
/**
* Resets this instance to the last state
*/
reset() {
if (this.states != null) {
this.head = this.states.head;
this.tail = this.states.tail;
this.len = this.states.len;
this.states = this.states.next;
}
else {
this.head = this.tail = new Op(noop, 0, 0);
this.len = 0;
}
return this;
}
/**
* Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
*/
ldelim() {
const head = this.head;
const tail = this.tail;
const len = this.len;
this.reset().uint32(len);
if (len !== 0) {
this.tail.next = head.next; // skip noop
this.tail = tail;
this.len += len;
}
return this;
}
/**
* Finishes the write operation
*/
finish() {
let head = this.head.next; // skip noop
const buf = alloc(this.len);
let pos = 0;
while (head != null) {
head.fn(head.val, buf, pos);
pos += head.len;
head = head.next;
}
// this.head = this.tail = null;
return buf;
}
}
function writeByte(val, buf, pos) {
buf[pos] = val & 255;
}
function writeVarint32(val, buf, pos) {
while (val > 127) {
buf[pos++] = val & 127 | 128;
val >>>= 7;
}
buf[pos] = val;
}
/**
* Constructs a new varint writer operation instance.
*
* @classdesc Scheduled varint writer operation
*/
class VarintOp extends Op {
next;
constructor(len, val) {
super(writeVarint32, len, val);
this.next = undefined;
}
}
function writeVarint64(val, buf, pos) {
while (val.hi !== 0) {
buf[pos++] = val.lo & 127 | 128;
val.lo = (val.lo >>> 7 | val.hi << 25) >>> 0;
val.hi >>>= 7;
}
while (val.lo > 127) {
buf[pos++] = val.lo & 127 | 128;
val.lo = val.lo >>> 7;
}
buf[pos++] = val.lo;
}
function writeFixed32(val, buf, pos) {
buf[pos] = val & 255;
buf[pos + 1] = val >>> 8 & 255;
buf[pos + 2] = val >>> 16 & 255;
buf[pos + 3] = val >>> 24;
}
function writeBytes(val, buf, pos) {
buf.set(val, pos);
}
if (globalThis.Buffer != null) {
Uint8ArrayWriter.prototype.bytes = function (value) {
const len = value.length >>> 0;
this.uint32(len);
if (len > 0) {
this._push(writeBytesBuffer, len, value);
}
return this;
};
Uint8ArrayWriter.prototype.string = function (value) {
const len = globalThis.Buffer.byteLength(value);
this.uint32(len);
if (len > 0) {
this._push(writeStringBuffer, len, value);
}
return this;
};
}
function writeBytesBuffer(val, buf, pos) {
buf.set(val, pos); // faster than copy (requires node >= 4 where Buffers extend Uint8Array and set is properly inherited)
// also works for plain array values
}
function writeStringBuffer(val, buf, pos) {
if (val.length < 40) {
// plain js is faster for short strings (probably due to redundant assertions)
utf8.write(val, buf, pos);
// @ts-expect-error buf isn't a Uint8Array?
}
else if (buf.utf8Write != null) {
// @ts-expect-error buf isn't a Uint8Array?
buf.utf8Write(val, pos);
}
else {
buf.set(uint8ArrayFromString(val), pos);
}
}
/**
* Creates a new writer
*/
export function createWriter() {
return new Uint8ArrayWriter();
}
//# sourceMappingURL=writer.js.map
File diff suppressed because one or more lines are too long
+27
View File
@@ -0,0 +1,27 @@
{
"CodeError": "https://ipfs.github.io/protons/classes/protons-runtime.CodeError.html",
".:CodeError": "https://ipfs.github.io/protons/classes/protons-runtime.CodeError.html",
"MaxLengthError": "https://ipfs.github.io/protons/classes/protons-runtime.MaxLengthError.html",
".:MaxLengthError": "https://ipfs.github.io/protons/classes/protons-runtime.MaxLengthError.html",
"MaxSizeError": "https://ipfs.github.io/protons/classes/protons-runtime.MaxSizeError.html",
".:MaxSizeError": "https://ipfs.github.io/protons/classes/protons-runtime.MaxSizeError.html",
"NoMessagesFoundError": "https://ipfs.github.io/protons/classes/protons-runtime.NoMessagesFoundError.html",
".:NoMessagesFoundError": "https://ipfs.github.io/protons/classes/protons-runtime.NoMessagesFoundError.html",
"ParseError": "https://ipfs.github.io/protons/classes/protons-runtime.ParseError.html",
".:ParseError": "https://ipfs.github.io/protons/classes/protons-runtime.ParseError.html",
"Codec": "https://ipfs.github.io/protons/interfaces/protons-runtime.Codec.html",
"DecodeOptions": "https://ipfs.github.io/protons/interfaces/protons-runtime.DecodeOptions.html",
"EncodeOptions": "https://ipfs.github.io/protons/interfaces/protons-runtime.EncodeOptions.html",
"FieldDef": "https://ipfs.github.io/protons/interfaces/protons-runtime.FieldDef.html",
".:FieldDef": "https://ipfs.github.io/protons/interfaces/protons-runtime.FieldDef.html",
"Reader": "https://ipfs.github.io/protons/interfaces/protons-runtime.Reader.html",
".:Reader": "https://ipfs.github.io/protons/interfaces/protons-runtime.Reader.html",
"Writer": "https://ipfs.github.io/protons/interfaces/protons-runtime.Writer.html",
".:Writer": "https://ipfs.github.io/protons/interfaces/protons-runtime.Writer.html",
"decodeMessage": "https://ipfs.github.io/protons/functions/protons-runtime.decodeMessage.html",
"encodeMessage": "https://ipfs.github.io/protons/functions/protons-runtime.encodeMessage.html",
"enumeration": "https://ipfs.github.io/protons/functions/protons-runtime.enumeration.html",
"message": "https://ipfs.github.io/protons/functions/protons-runtime.message.html",
"reader": "https://ipfs.github.io/protons/functions/protons-runtime.reader.html",
"writer": "https://ipfs.github.io/protons/functions/protons-runtime.writer.html"
}