Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(javascript): Added serialization and serialization for Type Meta Layer #1825

Merged
merged 35 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d9ee8cd
Added wrapper on typemeta
Forchapeatl Sep 2, 2024
b0d73b9
Add files via upload
Forchapeatl Sep 2, 2024
03f6227
Update object.ts
Forchapeatl Sep 2, 2024
f639b3c
using BinaryBuilderReader
Forchapeatl Sep 7, 2024
5d30608
Removed MetaString and Using TypeMeta
Forchapeatl Sep 7, 2024
b88b616
Update builder.ts
Forchapeatl Sep 7, 2024
3a4e9f4
reoved TypeMeta
Forchapeatl Sep 9, 2024
a7d7bf1
added ownName to binary.reader
Forchapeatl Sep 9, 2024
b81255a
moved from TypeMetaWrapper to TypeMetaBuilder
Forchapeatl Sep 9, 2024
d259a2d
Added TypeMeta
Forchapeatl Sep 9, 2024
2780033
Added TypeMeta to constructor
Forchapeatl Sep 11, 2024
81b274f
removed TypeMeta from constructor
Forchapeatl Sep 12, 2024
b67f509
FIxed syntax error
Forchapeatl Sep 14, 2024
753869e
fixed lint errors
Forchapeatl Sep 24, 2024
ef92c34
Fixed Meta Layer Buffer
Forchapeatl Sep 24, 2024
615a473
fixed byte length over flow
Forchapeatl Sep 24, 2024
42845e6
removed BinaryReader Builder dependency
Forchapeatl Sep 24, 2024
1434328
fixed lint errors
Forchapeatl Sep 24, 2024
418f4a5
Fixed Meta Layer Buffer
Forchapeatl Sep 24, 2024
7b666c2
fixed lint errors
Forchapeatl Sep 24, 2024
4a3b3f5
fixed lint errors
Forchapeatl Sep 24, 2024
aedf2aa
fixed lint errors
Forchapeatl Sep 24, 2024
f3b87da
Merge branch 'main' into Forchapeatl-TypeMeta
Forchapeatl Sep 24, 2024
9c48706
switched to camel casing
Forchapeatl Sep 26, 2024
b9c3cc9
switched to camel casing
Forchapeatl Sep 26, 2024
636fb05
switched to camel casing
Forchapeatl Sep 26, 2024
5418532
Added Compatible mode
Forchapeatl Sep 27, 2024
7cd91cd
Added mode to fury config
Forchapeatl Sep 27, 2024
2bb55ad
exported Mode
Forchapeatl Sep 27, 2024
1555617
used Mode from type.ts
Forchapeatl Sep 27, 2024
f107fc1
SchemaConsistent for default mode
Forchapeatl Sep 27, 2024
37e88d8
Reading TypeMeta only in Compatible mode
Forchapeatl Sep 28, 2024
91fc901
Imported Fury Mode
Forchapeatl Sep 28, 2024
1941170
fixed lint errors
Forchapeatl Sep 28, 2024
83bb434
Merge branch 'main' into Forchapeatl-TypeMeta
theweipeng Sep 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions javascript/packages/fury/lib/fury.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
import ClassResolver from "./classResolver";
import { BinaryWriter } from "./writer";
import { BinaryReader } from "./reader";
import { BinaryReaderBuilder } from "./gen/builder";
import { ReferenceResolver } from "./referenceResolver";
import { ConfigFlags, Serializer, Config, Language, MAGIC_NUMBER } from "./type";
import { OwnershipError } from "./error";
import { InputType, ResultType, TypeDescription } from "./description";
import { generateSerializer, AnySerializer } from "./gen";
import { TypeMeta } from './meta/TypeMeta';


export default class {
binaryReader: BinaryReader;
binaryWriter: BinaryWriter;
classResolver = new ClassResolver();
referenceResolver: ReferenceResolver;
anySerializer: AnySerializer;
typeMeta : TypeMeta;

constructor(public config: Config = {
refTracking: false,
Expand All @@ -44,6 +48,8 @@ export default class {
this.referenceResolver = new ReferenceResolver(this.binaryReader);
this.classResolver.init(this);
this.anySerializer = new AnySerializer(this);
this.typeMeta = new TypeMeta(BigInt(0),[]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_bytes in TypeMeta is a static method, if you want to call it, you should replace by this.typeMeta = TypeMeta


}

registerSerializer<T extends TypeDescription>(description: T) {
Expand Down Expand Up @@ -131,3 +137,11 @@ export default class {
return this.serializeInternal(data, serializer).dumpAndOwn();
}
}

class TypeMetaBuilder{
constructor(private fury: string){
}
from_bytes(reader: string){
return `${this.fury}.typeMeta.from_bytes(${reader})`;
}
}
16 changes: 15 additions & 1 deletion javascript/packages/fury/lib/gen/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ import { Scope } from "./scope";
import { getMeta } from "../meta";
import { TypeDescription } from "../description";
import Fury from "../fury";
import { TypeMeta } from '../meta/TypeMeta';
import { BinaryReader } from "../reader";

class BinaryReaderBuilder {
class TypeMetaBuilder{
constructor(private fury: string){

}
from_bytes(reader: string){
return `${this.fury}.typeMeta.from_bytes(${reader})`;
}
}

export class BinaryReaderBuilder {
constructor(private holder: string) {

}
Expand Down Expand Up @@ -309,6 +320,7 @@ class ClassResolverBuilder {
export class CodecBuilder {
reader: BinaryReaderBuilder;
writer: BinaryWriterBuilder;
typeMeta: TypeMetaBuilder; // Use the TypeMetaWrapper
referenceResolver: ReferenceResolverBuilder;
classResolver: ClassResolverBuilder;

Expand All @@ -321,6 +333,8 @@ export class CodecBuilder {
this.writer = new BinaryWriterBuilder(bw);
this.classResolver = new ClassResolverBuilder(cr);
this.referenceResolver = new ReferenceResolverBuilder(rr);
this.typeMeta = new TypeMetaBuilder("fury"); // Initialize the TypeMetaWrapper

}

furyName() {
Expand Down
13 changes: 11 additions & 2 deletions javascript/packages/fury/lib/gen/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { CodegenRegistry } from "./router";
import { BaseSerializerGenerator, RefState } from "./serializer";
import SerializerResolver from "../classResolver";
import { MetaString } from "../meta/MetaString";
import { FieldInfo, TypeMeta } from '../meta/TypeMeta';


// Ensure MetaString methods are correctly implemented
const computeMetaInformation = (description: any) => {
Expand Down Expand Up @@ -81,10 +83,17 @@ class ObjectSerializerGenerator extends BaseSerializerGenerator {
const options = this.description.options;
const expectHash = computeStructHash(this.description);
const metaInformation = Buffer.from(computeMetaInformation(this.description));
const fields = Object.entries(options.props).map(([key, value]) => {
return new FieldInfo(key, value.type);
});
const binary = TypeMeta.from_fields(256, fields).to_bytes();
const binaryLength = binary.length; // Capture the length of the binary data

// Encode binary data as a base64 string
const binaryBase64 = Buffer.from(binary).toString("base64");
return `
${this.builder.writer.int32(expectHash)};
${this.builder.writer.buffer(`Buffer.from("${metaInformation.toString("base64")}", "base64")`)};
${this.builder.writer.buffer(binaryBase64)};
theweipeng marked this conversation as resolved.
Show resolved Hide resolved
${Object.entries(options.props).sort().map(([key, inner]) => {
const InnerGeneratorClass = CodegenRegistry.get(inner.type);
if (!InnerGeneratorClass) {
Expand Down Expand Up @@ -112,7 +121,7 @@ class ObjectSerializerGenerator extends BaseSerializerGenerator {
}).join(",\n")}
};
${this.maybeReference(result, refState)}
${this.builder.reader.buffer(encodedMetaInformation.byteLength)}
${this.builder.typeMeta.from_bytes(this.builder.reader.ownName())}
${Object.entries(options.props).sort().map(([key, inner]) => {
const InnerGeneratorClass = CodegenRegistry.get(inner.type);
if (!InnerGeneratorClass) {
Expand Down
167 changes: 167 additions & 0 deletions javascript/packages/fury/lib/meta/TypeMeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { BinaryWriter } from "../writer";
import { BinaryReader } from "../reader";
import { ObjectTypeDescription, Type } from "../description";
import { MetaString } from "./MetaString";
import { BinaryReaderBuilder } from "../gen/builder";


enum Encoding {
Utf8,
AllToLowerSpecial,
LowerUpperDigitSpecial,
}

export class FieldInfo {
constructor(private field_name: string, private field_id: number) {
}

static u8_to_encoding(value: number) {
switch (value) {
case 0x00:
return Encoding.Utf8;
case 0x01:
return Encoding.AllToLowerSpecial;
case 0x02:
return Encoding.LowerUpperDigitSpecial;
}
}

static uint8ArrayToBinary(array: Uint8Array): string {
return Array.from(array)
.map(byte => byte.toString(2).padStart(8, "0"))
.join("");
}

static from_bytes(reader: BinaryReaderBuilder) {
const header = parseInt((reader.uint8()));
const encoding = this.u8_to_encoding((header & 0b11000) >> 3);
let size = (header & 0b11100000) >> 5;
size = (size === 0b111) ? parseInt(reader.varInt32()) + 7 : size;
const type_id = parseInt(reader.int16());
const base64String = reader.buffer(size);

// Decode the base64 string
const binaryString = atob(base64String);

// Convert the binary string to an array of numbers
const numberArray = Array.from(binaryString, (char) => char.charCodeAt(0));

// Create a new Uint8Array from the number array
const uint8Array = new Uint8Array(numberArray);

const field_name = MetaString.decode(uint8Array);
return new FieldInfo(field_name, type_id);
}

to_bytes() {
const writer = new BinaryWriter({});
const meta_string = MetaString.encode(this.field_name);
let header = 1 << 2;
const encoded = FieldInfo.uint8ArrayToBinary(meta_string);
const size = encoded.length;
header |= MetaString.inferBitsPerChar(this.field_name) << 3;
const big_size = size >= 7;
if (big_size) {
header |= 0b11100000;
writer.uint8(header);
writer.varInt32(size - 7);
} else {
header |= size << 5;
writer.uint8(header);
}
writer.int16(this.field_id);
writer.buffer(meta_string);
return writer.dump();
}
}

// Using classes to emulate struct methods in Rust
class TypeMetaLayer {
constructor(private type_id: number, private field_info: FieldInfo[]) {
}

get_type_id() {
return this.type_id;
}

get_field_info() {
return this.field_info;
}

to_bytes() {
const writer = new BinaryWriter({});
writer.varInt32(this.field_info.length);
writer.varInt32(this.type_id);
for (const field of this.field_info) {
writer.buffer(field.to_bytes());
}
return writer.dump();
}

static from_bytes(reader: BinaryReaderBuilder) {
const field_num = BigInt(reader.varInt32());
const type_id = parseInt(reader.varInt32());
const field_info = [];
for (let i = 0; i < field_num; i++) {
field_info.push(FieldInfo.from_bytes(reader));
}
return new TypeMetaLayer(type_id, field_info);
}
}

export class TypeMeta {
constructor(private hash: bigint, private layers: TypeMetaLayer[]) {
}

get_field_info() {
theweipeng marked this conversation as resolved.
Show resolved Hide resolved
return this.layers[0].get_field_info();
}

get_type_id() {
return this.layers[0].get_type_id();
}

static from_fields(type_id: number, field_info: FieldInfo[]) {
return new TypeMeta(BigInt(0), [new TypeMetaLayer(type_id, field_info)]);
}

static from_bytes(reader: BinaryReaderBuilder) {
let header = parseInt(reader.uint64());
const hash = !Number.isNaN(header) ? BigInt(header) >> BigInt(8) : BigInt(0);
//const hash = BigInt(header) >> BigInt(8);
const layer_count = !Number.isNaN(header) ? BigInt(header) & BigInt(0b1111): BigInt(0);
const layers = [];
for (let i = 0; i < layer_count; i++) {
layers.push(TypeMetaLayer.from_bytes(reader));
}
return new TypeMeta(hash, layers);
}

to_bytes() {
const writer = new BinaryWriter({});
writer.uint64(BigInt((this.hash << BigInt(8)) | BigInt((this.layers.length & 0b1111))));
for (const layer of this.layers) {
writer.buffer(layer.to_bytes());
}
return writer.dump();
}
}
Loading