mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-17 10:55:48 +00:00
Refactored STArray Class (#85)
Refactored STArray and pushed end-byte computations into the STArray and STObject classes.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as assert from "assert";
|
||||
import { Field, FieldInstance } from "../enums";
|
||||
import { FieldInstance } from "../enums";
|
||||
|
||||
/**
|
||||
* Bytes list is a collection of buffer objects
|
||||
@@ -133,11 +133,6 @@ class BinarySerializer {
|
||||
this.writeLengthEncoded(associatedValue);
|
||||
} else {
|
||||
associatedValue.toBytesSink(this.sink);
|
||||
if (field.type.name === "STObject") {
|
||||
this.sink.put(Field["ObjectEndMarker"].header);
|
||||
} else if (field.type.name === "STArray") {
|
||||
this.sink.put(Field["ArrayEndMarker"].header);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -171,7 +171,7 @@ class Path extends SerializedTypeClass {
|
||||
*/
|
||||
toJSON() {
|
||||
const json: Array<HopObject> = [];
|
||||
const pathParser = new BinaryParser(this.bytes.toString("hex"));
|
||||
const pathParser = new BinaryParser(this.toString());
|
||||
|
||||
while (!pathParser.end()) {
|
||||
json.push(Hop.fromParser(pathParser).toJSON());
|
||||
@@ -236,7 +236,7 @@ class PathSet extends SerializedTypeClass {
|
||||
*/
|
||||
toJSON(): Array<Array<HopObject>> {
|
||||
const json: Array<Array<HopObject>> = [];
|
||||
const pathParser = new BinaryParser(this.bytes.toString("hex"));
|
||||
const pathParser = new BinaryParser(this.toString());
|
||||
|
||||
while (!pathParser.end()) {
|
||||
json.push(Path.fromParser(pathParser).toJSON());
|
||||
|
||||
@@ -1,37 +1,83 @@
|
||||
import { makeClass } from "../utils/make-class";
|
||||
import { ensureArrayLikeIs, SerializedType } from "./serialized-type";
|
||||
import { SerializedTypeClass } from "./serialized-type";
|
||||
import { STObject } from "./st-object";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
|
||||
const STArray = makeClass(
|
||||
{
|
||||
mixins: SerializedType,
|
||||
inherits: Array,
|
||||
statics: {
|
||||
fromParser(parser) {
|
||||
const array = new STArray();
|
||||
while (!parser.end()) {
|
||||
const field = parser.readField();
|
||||
if (field.name === "ArrayEndMarker") {
|
||||
break;
|
||||
}
|
||||
const outer = new STObject();
|
||||
outer[field.name] = parser.readFieldValue(field);
|
||||
array.push(outer);
|
||||
}
|
||||
return array;
|
||||
},
|
||||
from(value) {
|
||||
return ensureArrayLikeIs(STArray, value).withChildren(STObject);
|
||||
},
|
||||
},
|
||||
toJSON() {
|
||||
return this.map((v) => v.toJSON());
|
||||
},
|
||||
toBytesSink(sink) {
|
||||
this.forEach((so) => so.toBytesSink(sink));
|
||||
},
|
||||
},
|
||||
undefined
|
||||
);
|
||||
const ARRAY_END_MARKER = 0xf1;
|
||||
const OBJECT_END_MARKER = 0xe1;
|
||||
|
||||
/**
|
||||
* Class for serializing and deserializing Arrays of Objects
|
||||
*/
|
||||
class STArray extends SerializedTypeClass {
|
||||
/**
|
||||
* Construct an STArray from a BinaryParser
|
||||
*
|
||||
* @param parser BinaryParser to parse an STArray from
|
||||
* @returns An STArray Object
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): STArray {
|
||||
const bytes: Array<Buffer> = [];
|
||||
|
||||
while (!parser.end()) {
|
||||
const field = parser.readField();
|
||||
if (field.name === "ArrayEndMarker") {
|
||||
break;
|
||||
}
|
||||
|
||||
bytes.push(
|
||||
field.header,
|
||||
parser.readFieldValue(field).toBytes(),
|
||||
Buffer.from([OBJECT_END_MARKER])
|
||||
);
|
||||
}
|
||||
|
||||
bytes.push(Buffer.from([ARRAY_END_MARKER]));
|
||||
return new STArray(Buffer.concat(bytes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an STArray from an Array of JSON Objects
|
||||
*
|
||||
* @param value STArray or Array of Objects to parse into an STArray
|
||||
* @returns An STArray object
|
||||
*/
|
||||
static from(value: STArray | Array<object>): STArray {
|
||||
if (value instanceof STArray) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const bytes: Array<Buffer> = [];
|
||||
value.forEach((obj) => {
|
||||
bytes.push(STObject.from(obj).toBytes());
|
||||
});
|
||||
|
||||
bytes.push(Buffer.from([ARRAY_END_MARKER]));
|
||||
return new STArray(Buffer.concat(bytes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the JSON representation of this.bytes
|
||||
*
|
||||
* @returns An Array of JSON objects
|
||||
*/
|
||||
toJSON(): Array<object> {
|
||||
const result: Array<object> = [];
|
||||
|
||||
const arrayParser = new BinaryParser(this.toString());
|
||||
|
||||
while (!arrayParser.end()) {
|
||||
const field = arrayParser.readField();
|
||||
if (field.name === "ArrayEndMarker") {
|
||||
break;
|
||||
}
|
||||
|
||||
const outer = {};
|
||||
outer[field.name] = STObject.fromParser(arrayParser).toJSON();
|
||||
result.push(outer);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export { STArray };
|
||||
|
||||
@@ -67,6 +67,9 @@ const STObject = makeClass(
|
||||
return;
|
||||
}
|
||||
serializer.writeFieldAndValue(field, value);
|
||||
if (field.type.name === "STObject") {
|
||||
serializer.put(Buffer.from([0xe1]));
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user