From 8e1dc5cd8c34b364a0051872e9e28c12c1ab8d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=A3=B0?= Date: Wed, 20 Dec 2023 15:44:53 +0800 Subject: [PATCH] [JavaScript] Add lint tools (#1232) * [JavaScript] Add lint tools * [JavaScript] Lint files * [CI] Add CI format check * [CI] Remove redundant ci checks --- ci/format.sh | 2 +- javascript/.eslintrc.cjs | 21 +- javascript/package.json | 3 +- javascript/packages/fury/index.ts | 77 ++- javascript/packages/fury/lib/classResolver.ts | 200 ++++---- javascript/packages/fury/lib/codeGen.ts | 270 +++++----- javascript/packages/fury/lib/description.ts | 461 +++++++++--------- javascript/packages/fury/lib/fury.ts | 143 +++--- .../fury/lib/internalSerializer/any.ts | 251 +++++----- .../fury/lib/internalSerializer/array.ts | 79 ++- .../fury/lib/internalSerializer/binary.ts | 49 +- .../fury/lib/internalSerializer/bool.ts | 43 +- .../fury/lib/internalSerializer/datetime.ts | 78 +-- .../fury/lib/internalSerializer/map.ts | 68 ++- .../fury/lib/internalSerializer/number.ts | 376 +++++++------- .../fury/lib/internalSerializer/set.ts | 61 ++- .../fury/lib/internalSerializer/string.ts | 52 +- .../fury/lib/internalSerializer/tuple.ts | 63 ++- .../packages/fury/lib/platformBuffer.ts | 195 ++++---- javascript/packages/fury/lib/reader.ts | 2 +- .../packages/fury/lib/referenceResolver.ts | 7 +- javascript/packages/fury/lib/type.ts | 137 +++--- javascript/packages/fury/lib/util.ts | 42 +- javascript/packages/fury/lib/writer.ts | 16 +- javascript/packages/hps/index.ts | 12 +- 25 files changed, 1342 insertions(+), 1366 deletions(-) diff --git a/ci/format.sh b/ci/format.sh index 4c86fe3f55..4b1c334a92 100755 --- a/ci/format.sh +++ b/ci/format.sh @@ -89,7 +89,7 @@ fi if [ ! -f "$ROOT/javascript/node_modules/.bin/eslint" ]; then echo "eslint is not installed, start to install it." pushd "$ROOT/javascript" - npm install --registry=https://registry.npmmirror.com + npm install --registry=https://registry.npmmirror.com popd fi diff --git a/javascript/.eslintrc.cjs b/javascript/.eslintrc.cjs index a6cf2223d4..05a26fa12c 100644 --- a/javascript/.eslintrc.cjs +++ b/javascript/.eslintrc.cjs @@ -1,12 +1,23 @@ +const stylistic = require("@stylistic/eslint-plugin"); + +const customized = stylistic.configs.customize({ + indent: 2, + quotes: "double", + semi: true, + jsx: true, +}); + module.exports = { - extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'], - parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint'], + extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + parser: "@typescript-eslint/parser", + plugins: ["@typescript-eslint", "@stylistic"], root: true, rules: { + ...customized.rules, + "@stylistic/brace-style": ["error", "1tbs"], "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-var-requires": "off", }, - ignorePatterns: ["test/*", "dist/*", "*.js", "murmurHash3.ts"] -}; \ No newline at end of file + ignorePatterns: ["test/*", "dist/*", "*.js", "murmurHash3.ts", "packages/**/dist/*", "packages/**/build/*"], +}; diff --git a/javascript/package.json b/javascript/package.json index 3afe903167..750047ebeb 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -11,6 +11,7 @@ "packages/fury" ], "devDependencies": { - "prettier": "^3.1.0" + "@stylistic/eslint-plugin": "^1.5.1", + "eslint": "^8.55.0" } } diff --git a/javascript/packages/fury/index.ts b/javascript/packages/fury/index.ts index 9716931d2f..d798a4aab8 100644 --- a/javascript/packages/fury/index.ts +++ b/javascript/packages/fury/index.ts @@ -15,57 +15,56 @@ */ import { - genSerializer, + genSerializer, } from "./lib/codeGen"; import { - Cast, - ObjectTypeDescription, - TypeDescription, - ArrayTypeDescription, - Type, - ToRecordType, + Cast, + ObjectTypeDescription, + TypeDescription, + ArrayTypeDescription, + Type, + ToRecordType, } from "./lib/description"; import { Serializer, Fury, InternalSerializerType, Config } from "./lib/type"; import FuryInternal from "./lib/fury"; export { - Serializer, - InternalSerializerType, - TypeDescription, - ArrayTypeDescription, - ObjectTypeDescription, - Type, + Serializer, + InternalSerializerType, + TypeDescription, + ArrayTypeDescription, + ObjectTypeDescription, + Type, }; - export default class { - constructor(private config?: Config) { } - private fury: Fury = FuryInternal(this.config || {}); + constructor(private config?: Config) { } + private fury: Fury = FuryInternal(this.config || {}); - registerSerializer(description: T) { - if ( - description.type !== InternalSerializerType.FURY_TYPE_TAG || - !Cast(description)?.options.tag - ) { - throw new Error("root type should be object"); - } - const serializer = genSerializer(this.fury, description); - return { - serializer, - serialize: (data: ToRecordType) => { - return this.fury.serialize(data, serializer); - }, - deserialize: (bytes: Uint8Array) => { - return this.fury.deserialize(bytes, serializer) as ToRecordType; - }, - }; + registerSerializer(description: T) { + if ( + description.type !== InternalSerializerType.FURY_TYPE_TAG + || !Cast(description)?.options.tag + ) { + throw new Error("root type should be object"); } + const serializer = genSerializer(this.fury, description); + return { + serializer, + serialize: (data: ToRecordType) => { + return this.fury.serialize(data, serializer); + }, + deserialize: (bytes: Uint8Array) => { + return this.fury.deserialize(bytes, serializer) as ToRecordType; + }, + }; + } - serialize(v: any, serialize?: Serializer) { - return this.fury.serialize(v, serialize); - } + serialize(v: any, serialize?: Serializer) { + return this.fury.serialize(v, serialize); + } - deserialize(bytes: Uint8Array) { - return this.fury.deserialize(bytes); - } + deserialize(bytes: Uint8Array) { + return this.fury.deserialize(bytes); + } } diff --git a/javascript/packages/fury/lib/classResolver.ts b/javascript/packages/fury/lib/classResolver.ts index acf2074eb3..0376c2ff66 100644 --- a/javascript/packages/fury/lib/classResolver.ts +++ b/javascript/packages/fury/lib/classResolver.ts @@ -23,123 +23,123 @@ import setSerializer from "./internalSerializer/set"; import boolSerializer from "./internalSerializer/bool"; import { uInt16Serializer, int16Serializer, int32Serializer, uInt32Serializer, uInt64Serializer, floatSerializer, doubleSerializer, uInt8Serializer, int64Serializer, int8Serializer } from "./internalSerializer/number"; import { InternalSerializerType, Serializer, Fury, BinaryReader, BinaryWriter } from "./type"; -import anySerializer from './internalSerializer/any'; +import anySerializer from "./internalSerializer/any"; import { PlatformBuffer, fromUint8Array } from "./platformBuffer"; import { x64hash128 } from "./murmurHash3"; -import { BinaryWriter as BufferWriter } from './writer' +import { BinaryWriter as BufferWriter } from "./writer"; const USESTRINGVALUE = 0; -const USESTRINGID = 1 +const USESTRINGID = 1; export default class SerializerResolver { - private internalSerializer: Serializer[] = new Array(300); - private customSerializer: { [key: string]: Serializer } = { - }; - private readStringPool: string[] = []; - private writeStringIndex: string[] = []; + private internalSerializer: Serializer[] = new Array(300); + private customSerializer: { [key: string]: Serializer } = { + }; - private initInternalSerializer(fury: Fury) { - const _anySerializer = anySerializer(fury); - this.internalSerializer[InternalSerializerType.ANY] = _anySerializer; - this.internalSerializer[InternalSerializerType.STRING] = stringSerializer(fury); - this.internalSerializer[InternalSerializerType.ARRAY] = arraySerializer(fury, _anySerializer); - this.internalSerializer[InternalSerializerType.MAP] = mapSerializer(fury, _anySerializer, _anySerializer); - this.internalSerializer[InternalSerializerType.BOOL] = boolSerializer(fury); - this.internalSerializer[InternalSerializerType.UINT8] = uInt8Serializer(fury); - this.internalSerializer[InternalSerializerType.INT8] = int8Serializer(fury); - this.internalSerializer[InternalSerializerType.UINT16] = uInt16Serializer(fury); - this.internalSerializer[InternalSerializerType.INT16] = int16Serializer(fury); - this.internalSerializer[InternalSerializerType.UINT32] = uInt32Serializer(fury); - this.internalSerializer[InternalSerializerType.INT32] = int32Serializer(fury); - this.internalSerializer[InternalSerializerType.UINT64] = uInt64Serializer(fury); - this.internalSerializer[InternalSerializerType.INT64] = int64Serializer(fury); - this.internalSerializer[InternalSerializerType.FLOAT] = floatSerializer(fury); - this.internalSerializer[InternalSerializerType.DOUBLE] = doubleSerializer(fury); - this.internalSerializer[InternalSerializerType.TIMESTAMP] = timestampSerializer(fury); - this.internalSerializer[InternalSerializerType.DATE] = dateSerializer(fury); - this.internalSerializer[InternalSerializerType.FURY_SET] = setSerializer(fury, anySerializer(fury)); - this.internalSerializer[InternalSerializerType.FURY_STRING_ARRAY] = stringArraySerializer(fury); - this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_BOOL_ARRAY] = boolArraySerializer(fury); - this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_SHORT_ARRAY] = shortArraySerializer(fury); - this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_INT_ARRAY] = intArraySerializer(fury); - this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_LONG_ARRAY] = longArraySerializer(fury); - this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_FLOAT_ARRAY] = floatArraySerializer(fury); - this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_DOUBLE_ARRAY] = doubleArraySerializer(fury); - this.internalSerializer[InternalSerializerType.BINARY] = binarySerializer(fury); - } + private readStringPool: string[] = []; + private writeStringIndex: string[] = []; - init(fury: Fury) { - this.initInternalSerializer(fury); - } + private initInternalSerializer(fury: Fury) { + const _anySerializer = anySerializer(fury); + this.internalSerializer[InternalSerializerType.ANY] = _anySerializer; + this.internalSerializer[InternalSerializerType.STRING] = stringSerializer(fury); + this.internalSerializer[InternalSerializerType.ARRAY] = arraySerializer(fury, _anySerializer); + this.internalSerializer[InternalSerializerType.MAP] = mapSerializer(fury, _anySerializer, _anySerializer); + this.internalSerializer[InternalSerializerType.BOOL] = boolSerializer(fury); + this.internalSerializer[InternalSerializerType.UINT8] = uInt8Serializer(fury); + this.internalSerializer[InternalSerializerType.INT8] = int8Serializer(fury); + this.internalSerializer[InternalSerializerType.UINT16] = uInt16Serializer(fury); + this.internalSerializer[InternalSerializerType.INT16] = int16Serializer(fury); + this.internalSerializer[InternalSerializerType.UINT32] = uInt32Serializer(fury); + this.internalSerializer[InternalSerializerType.INT32] = int32Serializer(fury); + this.internalSerializer[InternalSerializerType.UINT64] = uInt64Serializer(fury); + this.internalSerializer[InternalSerializerType.INT64] = int64Serializer(fury); + this.internalSerializer[InternalSerializerType.FLOAT] = floatSerializer(fury); + this.internalSerializer[InternalSerializerType.DOUBLE] = doubleSerializer(fury); + this.internalSerializer[InternalSerializerType.TIMESTAMP] = timestampSerializer(fury); + this.internalSerializer[InternalSerializerType.DATE] = dateSerializer(fury); + this.internalSerializer[InternalSerializerType.FURY_SET] = setSerializer(fury, anySerializer(fury)); + this.internalSerializer[InternalSerializerType.FURY_STRING_ARRAY] = stringArraySerializer(fury); + this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_BOOL_ARRAY] = boolArraySerializer(fury); + this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_SHORT_ARRAY] = shortArraySerializer(fury); + this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_INT_ARRAY] = intArraySerializer(fury); + this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_LONG_ARRAY] = longArraySerializer(fury); + this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_FLOAT_ARRAY] = floatArraySerializer(fury); + this.internalSerializer[InternalSerializerType.FURY_PRIMITIVE_DOUBLE_ARRAY] = doubleArraySerializer(fury); + this.internalSerializer[InternalSerializerType.BINARY] = binarySerializer(fury); + } - reset() { - this.readStringPool = []; - this.writeStringIndex = []; - } + init(fury: Fury) { + this.initInternalSerializer(fury); + } - getSerializerById(id: InternalSerializerType) { - return this.internalSerializer[id]; - } + reset() { + this.readStringPool = []; + this.writeStringIndex = []; + } - registerSerializerByTag(tag: string, serializer: Serializer) { - if (this.customSerializer[tag]) { - Object.assign(this.customSerializer[tag], serializer); - } else { - this.customSerializer[tag] = {...serializer}; - } - return this.customSerializer[tag] - } + getSerializerById(id: InternalSerializerType) { + return this.internalSerializer[id]; + } - getSerializerByTag(tag: string) { - return this.customSerializer[tag]; + registerSerializerByTag(tag: string, serializer: Serializer) { + if (this.customSerializer[tag]) { + Object.assign(this.customSerializer[tag], serializer); + } else { + this.customSerializer[tag] = { ...serializer }; } + return this.customSerializer[tag]; + } - tagToBuffer(tag: string) { - const tagBuffer = fromUint8Array(new TextEncoder().encode(tag)); - let tagHash = x64hash128(tagBuffer, 47).getBigUint64(0); - if (tagHash === BigInt(0)) { - tagHash = BigInt(1); - } - const bufferLen =tagBuffer.byteLength; - const writer = BufferWriter({}); - writer.uint8(USESTRINGVALUE); - writer.uint64(tagHash); - writer.int16(bufferLen); - writer.bufferWithoutMemCheck(tagBuffer, bufferLen); - return writer.dump(); - } + getSerializerByTag(tag: string) { + return this.customSerializer[tag]; + } - writeTag(binaryWriter: BinaryWriter, tag: string, bf: PlatformBuffer, byteLength: number) { - const index = this.writeStringIndex.indexOf(tag); - if (index > -1) { - binaryWriter.uint8(USESTRINGID) - binaryWriter.int16(index); - return; - } - this.writeStringIndex.push(tag); - binaryWriter.bufferWithoutMemCheck(bf, byteLength); + tagToBuffer(tag: string) { + const tagBuffer = fromUint8Array(new TextEncoder().encode(tag)); + let tagHash = x64hash128(tagBuffer, 47).getBigUint64(0); + if (tagHash === BigInt(0)) { + tagHash = BigInt(1); } + const bufferLen = tagBuffer.byteLength; + const writer = BufferWriter({}); + writer.uint8(USESTRINGVALUE); + writer.uint64(tagHash); + writer.int16(bufferLen); + writer.bufferWithoutMemCheck(tagBuffer, bufferLen); + return writer.dump(); + } + writeTag(binaryWriter: BinaryWriter, tag: string, bf: PlatformBuffer, byteLength: number) { + const index = this.writeStringIndex.indexOf(tag); + if (index > -1) { + binaryWriter.uint8(USESTRINGID); + binaryWriter.int16(index); + return; + } + this.writeStringIndex.push(tag); + binaryWriter.bufferWithoutMemCheck(bf, byteLength); + } - detectTag(binaryReader: BinaryReader) { - const flag = binaryReader.uint8(); - if (flag === USESTRINGVALUE) { - binaryReader.skip(8); // The tag hash is not needed at the moment. - const str = binaryReader.stringUtf8(binaryReader.int16()); - return str - } else { - return this.readStringPool[binaryReader.int16()] - } + detectTag(binaryReader: BinaryReader) { + const flag = binaryReader.uint8(); + if (flag === USESTRINGVALUE) { + binaryReader.skip(8); // The tag hash is not needed at the moment. + const str = binaryReader.stringUtf8(binaryReader.int16()); + return str; + } else { + return this.readStringPool[binaryReader.int16()]; } + } - readTag(binaryReader: BinaryReader) { - const flag = binaryReader.uint8(); - if (flag === USESTRINGVALUE) { - binaryReader.skip(8); // The tag hash is not needed at the moment. - const str = binaryReader.stringUtf8(binaryReader.int16()); - this.readStringPool.push(str); - return str - } else { - return this.readStringPool[binaryReader.int16()] - } + readTag(binaryReader: BinaryReader) { + const flag = binaryReader.uint8(); + if (flag === USESTRINGVALUE) { + binaryReader.skip(8); // The tag hash is not needed at the moment. + const str = binaryReader.stringUtf8(binaryReader.int16()); + this.readStringPool.push(str); + return str; + } else { + return this.readStringPool[binaryReader.int16()]; } -} \ No newline at end of file + } +} diff --git a/javascript/packages/fury/lib/codeGen.ts b/javascript/packages/fury/lib/codeGen.ts index 09e5dcec34..4c205c70a9 100644 --- a/javascript/packages/fury/lib/codeGen.ts +++ b/javascript/packages/fury/lib/codeGen.ts @@ -14,166 +14,166 @@ * limitations under the License. */ -import { InternalSerializerType, MaxInt32, RefFlags, Fury } from './type'; -import { replaceBackslashAndQuote, safePropAccessor, safePropName } from './util'; -import mapSerializer from './internalSerializer/map'; -import setSerializer from './internalSerializer/set'; -import { arraySerializer } from './internalSerializer/array'; -import { tupleSerializer } from './internalSerializer/tuple'; -import { ArrayTypeDescription, Cast, MapTypeDescription, ObjectTypeDescription, SetTypeDescription, TupleTypeDescription, TypeDescription } from './description'; +import { InternalSerializerType, MaxInt32, RefFlags, Fury } from "./type"; +import { replaceBackslashAndQuote, safePropAccessor, safePropName } from "./util"; +import mapSerializer from "./internalSerializer/map"; +import setSerializer from "./internalSerializer/set"; +import { arraySerializer } from "./internalSerializer/array"; +import { tupleSerializer } from "./internalSerializer/tuple"; +import { ArrayTypeDescription, Cast, MapTypeDescription, ObjectTypeDescription, SetTypeDescription, TupleTypeDescription, TypeDescription } from "./description"; function computeFieldHash(hash: number, id: number): number { - let newHash = (hash) * 31 + (id); - while (newHash >= MaxInt32) { - newHash = Math.floor(newHash / 7); - } - return newHash + let newHash = (hash) * 31 + (id); + while (newHash >= MaxInt32) { + newHash = Math.floor(newHash / 7); + } + return newHash; } const computeStringHash = (str: string) => { - const bytes = new TextEncoder().encode(str); - let hash = 17 - bytes.forEach(b => { - hash = hash * 31 + b - while (hash >= MaxInt32) { - hash = Math.floor(hash / 7) - } - }); - return hash; -} + const bytes = new TextEncoder().encode(str); + let hash = 17; + bytes.forEach((b) => { + hash = hash * 31 + b; + while (hash >= MaxInt32) { + hash = Math.floor(hash / 7); + } + }); + return hash; +}; const computeStructHash = (description: TypeDescription) => { - let hash = 17; - for (const [, value] of Object.entries(Cast(description).options.props).sort()) { - let id = value.type; - if (value.type === InternalSerializerType.ARRAY || value.type === InternalSerializerType.MAP) { - id = value.type; // TODO add map key&value type into schema hash - } else if (value.type === InternalSerializerType.FURY_TYPE_TAG) { - id = computeStringHash(Cast(value).options.tag); - } - hash = computeFieldHash(hash, id); + let hash = 17; + for (const [, value] of Object.entries(Cast(description).options.props).sort()) { + let id = value.type; + if (value.type === InternalSerializerType.ARRAY || value.type === InternalSerializerType.MAP) { + id = value.type; // TODO add map key&value type into schema hash + } else if (value.type === InternalSerializerType.FURY_TYPE_TAG) { + id = computeStringHash(Cast(value).options.tag); } - return hash; -} + hash = computeFieldHash(hash, id); + } + return hash; +}; function typeHandlerDeclaration(fury: Fury) { - let declarations: string[] = []; - let count = 0; - const exists = new Map(); - function addDeclar(name: string, declar: string, uniqueKey?: string) { - const unique = uniqueKey || name; - if (exists.has(unique)) { - return exists.get(unique)!; - } - declarations.push(declar); - exists.set(unique, name); - return name; + let declarations: string[] = []; + let count = 0; + const exists = new Map(); + function addDeclar(name: string, declar: string, uniqueKey?: string) { + const unique = uniqueKey || name; + if (exists.has(unique)) { + return exists.get(unique)!; } + declarations.push(declar); + exists.set(unique, name); + return name; + } - const genBuiltinDeclaration = (type: number) => { - const name = `type_${type}`.replace('-', '_'); - return addDeclar(name, ` + const genBuiltinDeclaration = (type: number) => { + const name = `type_${type}`.replace("-", "_"); + return addDeclar(name, ` const ${name} = classResolver.getSerializerById(${type})`); - } + }; - const genTagDeclaration = (tag: string) => { - const name = `tag_${count++}`; - return addDeclar(name, ` + const genTagDeclaration = (tag: string) => { + const name = `tag_${count++}`; + return addDeclar(name, ` const ${name} = classResolver.getSerializerByTag("${replaceBackslashAndQuote(tag)}")`, tag); + }; + + const genDeclaration = (description: TypeDescription): string => { + if (description.type === InternalSerializerType.FURY_TYPE_TAG) { + genSerializer(fury, description); + return genTagDeclaration(Cast(description).options.tag); } + if (description.type === InternalSerializerType.ARRAY) { + const tupleOptions = Cast(description).options; + if (tupleOptions && tupleOptions.isTuple) { + const names = [] as string[]; + Cast(description).options.inner.forEach((v) => { + names.push(genDeclaration(v)); + }); - const genDeclaration = (description: TypeDescription): string => { - if (description.type === InternalSerializerType.FURY_TYPE_TAG) { - genSerializer(fury, description); - return genTagDeclaration(Cast(description).options.tag); - } - if (description.type === InternalSerializerType.ARRAY) { - const tupleOptions = Cast(description).options; - if (tupleOptions && tupleOptions.isTuple) { - const names = [] as string[]; - Cast(description).options.inner.forEach(v => { - names.push(genDeclaration(v)); - }) - - const name = `tuple_${names.join('_')}`; - return addDeclar(name, ` - const ${name} = tupleSerializer(fury, [${names.join(', ')}])` - ) - } + const name = `tuple_${names.join("_")}`; + return addDeclar(name, ` + const ${name} = tupleSerializer(fury, [${names.join(", ")}])` + ); + } - const inner = genDeclaration(Cast(description).options.inner); - const name = `array_${inner}`; - return addDeclar(name, ` + const inner = genDeclaration(Cast(description).options.inner); + const name = `array_${inner}`; + return addDeclar(name, ` const ${name} = arraySerializer(fury, ${inner})` - ) - } - if (description.type === InternalSerializerType.FURY_SET) { - const inner = genDeclaration(Cast(description).options.key); - const name = `set_${inner}`; - return addDeclar(name, ` + ); + } + if (description.type === InternalSerializerType.FURY_SET) { + const inner = genDeclaration(Cast(description).options.key); + const name = `set_${inner}`; + return addDeclar(name, ` const ${name} = setSerializer(fury, ${inner})` - ) - } - if (description.type === InternalSerializerType.MAP) { - const key = genDeclaration(Cast(description).options.key); - const value = genDeclaration(Cast(description).options.value); + ); + } + if (description.type === InternalSerializerType.MAP) { + const key = genDeclaration(Cast(description).options.key); + const value = genDeclaration(Cast(description).options.value); - const name = `map_${key}_${value}`; - return addDeclar(name, ` + const name = `map_${key}_${value}`; + return addDeclar(name, ` const ${name} = mapSerializer(fury, ${key}, ${value})` - ) - } - return genBuiltinDeclaration(description.type); - } - return { - genDeclaration, - finish() { - const result = { - declarations, - names: [...exists.values()] - }; - declarations = []; - exists.clear(); - return result; - } + ); } + return genBuiltinDeclaration(description.type); + }; + return { + genDeclaration, + finish() { + const result = { + declarations, + names: [...exists.values()], + }; + declarations = []; + exists.clear(); + return result; + }, + }; } export const generateInlineCode = (fury: Fury, description: TypeDescription) => { - const options = Cast(description).options; - const tag = options?.tag; - const { genDeclaration, finish } = typeHandlerDeclaration(fury); - const expectHash = computeStructHash(description); - const read = ` + const options = Cast(description).options; + const tag = options?.tag; + const { genDeclaration, finish } = typeHandlerDeclaration(fury); + const expectHash = computeStructHash(description); + const read = ` // relation tag: ${tag} const result = { ${Object.entries(options.props).sort().map(([key]) => { - return `${safePropName(key)}: null` - }).join(',\n')} + return `${safePropName(key)}: null`; + }).join(",\n")} }; pushReadObject(result); ${Object.entries(options.props).sort().map(([key, value]) => { return `result${safePropAccessor(key)} = ${genDeclaration(value)}.read()`; - }).join(';\n') + }).join(";\n") } return result; `; - const write = Object.entries(options.props).sort().map(([key, value]) => { - return `${genDeclaration(value)}.write(v${safePropAccessor(key)})`; - }).join(';\n'); - const { names, declarations} = finish(); - const validTag = replaceBackslashAndQuote(tag); - return new Function( + const write = Object.entries(options.props).sort().map(([key, value]) => { + return `${genDeclaration(value)}.write(v${safePropAccessor(key)})`; + }).join(";\n"); + const { names, declarations } = finish(); + const validTag = replaceBackslashAndQuote(tag); + return new Function( ` return function (fury, scope) { const { referenceResolver, binaryWriter, classResolver, binaryReader } = fury; const { writeNullOrRef, pushReadObject } = referenceResolver; const { RefFlags, InternalSerializerType, arraySerializer, tupleSerializer, mapSerializer, setSerializer } = scope; - ${declarations.join('')} + ${declarations.join("")} const tagBuffer = classResolver.tagToBuffer("${validTag}"); const bufferLen = tagBuffer.byteLength; - const reserves = ${names.map(x => `${x}.config().reserve`).join(' + ')}; + const reserves = ${names.map(x => `${x}.config().reserve`).join(" + ")}; return { ...referenceResolver.deref(() => { const hash = binaryReader.int32(); @@ -198,24 +198,24 @@ return function (fury, scope) { } } ` - ) -} + ); +}; export const genSerializer = (fury: Fury, description: TypeDescription) => { - const tag = Cast(description).options?.tag; - if (fury.classResolver.getSerializerByTag(tag)) { - return fury.classResolver.getSerializerByTag(tag); - } - - fury.classResolver.registerSerializerByTag(tag, fury.classResolver.getSerializerById(InternalSerializerType.ANY)); - - const func = generateInlineCode(fury, description); - return fury.classResolver.registerSerializerByTag(tag, func()(fury, { - InternalSerializerType, - RefFlags, - arraySerializer, - tupleSerializer, - mapSerializer, - setSerializer, - })); -} + const tag = Cast(description).options?.tag; + if (fury.classResolver.getSerializerByTag(tag)) { + return fury.classResolver.getSerializerByTag(tag); + } + + fury.classResolver.registerSerializerByTag(tag, fury.classResolver.getSerializerById(InternalSerializerType.ANY)); + + const func = generateInlineCode(fury, description); + return fury.classResolver.registerSerializerByTag(tag, func()(fury, { + InternalSerializerType, + RefFlags, + arraySerializer, + tupleSerializer, + mapSerializer, + setSerializer, + })); +}; diff --git a/javascript/packages/fury/lib/description.ts b/javascript/packages/fury/lib/description.ts index a11efb3e81..cff97f2f98 100644 --- a/javascript/packages/fury/lib/description.ts +++ b/javascript/packages/fury/lib/description.ts @@ -16,280 +16,277 @@ import { InternalSerializerType } from "./type"; - export interface TypeDescription { - type: InternalSerializerType, - label?: string, + type: InternalSerializerType + label?: string } export interface ObjectTypeDescription extends TypeDescription { - options: { - props: { [key: string]: TypeDescription }, - tag: string, - } + options: { + props: { [key: string]: TypeDescription } + tag: string + } } export interface ArrayTypeDescription extends TypeDescription { - options: { - inner: TypeDescription; - } + options: { + inner: TypeDescription + } } export interface TupleTypeDescription extends TypeDescription { - options: { - isTuple: true, - inner: TypeDescription[]; - } + options: { + isTuple: true + inner: TypeDescription[] + } } - export interface SetTypeDescription extends TypeDescription { - options: { - key: TypeDescription; - } + options: { + key: TypeDescription + } } export interface MapTypeDescription extends TypeDescription { - options: { - key: TypeDescription; - value: TypeDescription - } + options: { + key: TypeDescription + value: TypeDescription + } } export function Cast(p: TypeDescription) { - return p as unknown as T1; + return p as unknown as T1; } type Props = T extends { - options: { - props?: infer T2 extends { [key: string]: any }; - tag: string; - }; + options: { + props?: infer T2 extends { [key: string]: any } + tag: string + } } - ? { - [P in keyof T2]?: (ToRecordType | null); + ? { + [P in keyof T2]?: (ToRecordType | null); } - : unknown; + : unknown; type InnerProps = T extends { - options: { - inner: infer T2 extends TypeDescription; - }; + options: { + inner: infer T2 extends TypeDescription + } } - ? (ToRecordType | null)[] - : unknown; + ? (ToRecordType | null)[] + : unknown; type MapProps = T extends { - options: { - key: infer T2 extends TypeDescription; - value: infer T3 extends TypeDescription; - }; + options: { + key: infer T2 extends TypeDescription + value: infer T3 extends TypeDescription + } } - ? Map, ToRecordType | null> - : unknown; + ? Map, ToRecordType | null> + : unknown; type TupleProps = T extends { - options: { - inner: infer T2 extends readonly[...TypeDescription[]]; - }; - } - ? { [K in keyof T2]: ToRecordType } - : unknown; + options: { + inner: infer T2 extends readonly[...TypeDescription[]] + } +} + ? { [K in keyof T2]: ToRecordType } + : unknown; type SetProps = T extends { - options: { - key: infer T2 extends TypeDescription; - }; + options: { + key: infer T2 extends TypeDescription + } } - ? Set<(ToRecordType | null)> - : unknown; + ? Set<(ToRecordType | null)> + : unknown; export type ToRecordType = T extends { - type: InternalSerializerType.FURY_TYPE_TAG; + type: InternalSerializerType.FURY_TYPE_TAG } - ? Props - : T extends { - type: InternalSerializerType.STRING; - } + ? Props + : T extends { + type: InternalSerializerType.STRING + } ? string : T extends { - type: InternalSerializerType.TUPLE; + type: InternalSerializerType.TUPLE } - ? TupleProps - : T extends { + ? TupleProps + : T extends { type: - | InternalSerializerType.UINT8 - | InternalSerializerType.UINT16 - | InternalSerializerType.UINT32 - | InternalSerializerType.UINT64 - | InternalSerializerType.INT8 - | InternalSerializerType.INT16 - | InternalSerializerType.INT32 - | InternalSerializerType.INT64 - | InternalSerializerType.FLOAT - | InternalSerializerType.DOUBLE; - } - ? number - : T extends { - type: InternalSerializerType.MAP; - } - ? MapProps - : T extends { - type: InternalSerializerType.FURY_SET; - } - ? SetProps - : T extends { - type: InternalSerializerType.ARRAY; - } - ? InnerProps - : T extends { - type: InternalSerializerType.BOOL; - } - ? boolean - : T extends { - type: InternalSerializerType.DATE; - } - ? Date - : T extends { - type: InternalSerializerType.TIMESTAMP; - } - ? number - : T extends { - type: InternalSerializerType.BINARY; - } - ? Uint8Array - : T extends { - type: InternalSerializerType.ANY; - } - ? any - : unknown; - + | InternalSerializerType.UINT8 + | InternalSerializerType.UINT16 + | InternalSerializerType.UINT32 + | InternalSerializerType.UINT64 + | InternalSerializerType.INT8 + | InternalSerializerType.INT16 + | InternalSerializerType.INT32 + | InternalSerializerType.INT64 + | InternalSerializerType.FLOAT + | InternalSerializerType.DOUBLE + } + ? number + : T extends { + type: InternalSerializerType.MAP + } + ? MapProps + : T extends { + type: InternalSerializerType.FURY_SET + } + ? SetProps + : T extends { + type: InternalSerializerType.ARRAY + } + ? InnerProps + : T extends { + type: InternalSerializerType.BOOL + } + ? boolean + : T extends { + type: InternalSerializerType.DATE + } + ? Date + : T extends { + type: InternalSerializerType.TIMESTAMP + } + ? number + : T extends { + type: InternalSerializerType.BINARY + } + ? Uint8Array + : T extends { + type: InternalSerializerType.ANY + } + ? any + : unknown; export const Type = { - any() { - return { - type: InternalSerializerType.ANY as const, - }; - }, - string() { - return { - type: InternalSerializerType.STRING as const, - }; - }, - array(def: T) { - return { - type: InternalSerializerType.ARRAY as const, - options: { - inner: def, - }, - }; - }, - tuple(t1: T1) { - return { - type: InternalSerializerType.TUPLE as const, - options: { - isTuple: true, - inner: t1, - }, - }; - }, - map( - key: T1, - value: T2 - ) { - return { - type: InternalSerializerType.MAP as const, - options: { - key, - value, - }, - }; - }, - set(key: T) { - return { - type: InternalSerializerType.FURY_SET as const, - options: { - key, - }, - }; - }, - bool() { - return { - type: InternalSerializerType.BOOL as const, - }; - }, - object(tag: string, props?: T) { - return { - type: InternalSerializerType.FURY_TYPE_TAG as const, - options: { - tag, - props, - }, - }; - }, - uint8() { - return { - type: InternalSerializerType.UINT8 as const, - }; - }, - uint16() { - return { - type: InternalSerializerType.UINT16 as const, - }; - }, - uint32() { - return { - type: InternalSerializerType.UINT32 as const, - }; - }, - uint64() { - return { - type: InternalSerializerType.UINT64 as const, - }; - }, - int8() { - return { - type: InternalSerializerType.INT8 as const, - }; - }, - int16() { - return { - type: InternalSerializerType.INT16 as const, - }; - }, - int32() { - return { - type: InternalSerializerType.INT32 as const, - }; - }, - int64() { - return { - type: InternalSerializerType.INT64 as const, - }; - }, - float() { - return { - type: InternalSerializerType.FLOAT as const, - }; - }, - double() { - return { - type: InternalSerializerType.DOUBLE as const, - }; - }, - binary() { - return { - type: InternalSerializerType.BINARY as const, - }; - }, - date() { - return { - type: InternalSerializerType.DATE as const, - }; - }, - timestamp() { - return { - type: InternalSerializerType.TIMESTAMP as const, - }; - }, + any() { + return { + type: InternalSerializerType.ANY as const, + }; + }, + string() { + return { + type: InternalSerializerType.STRING as const, + }; + }, + array(def: T) { + return { + type: InternalSerializerType.ARRAY as const, + options: { + inner: def, + }, + }; + }, + tuple(t1: T1) { + return { + type: InternalSerializerType.TUPLE as const, + options: { + isTuple: true, + inner: t1, + }, + }; + }, + map( + key: T1, + value: T2 + ) { + return { + type: InternalSerializerType.MAP as const, + options: { + key, + value, + }, + }; + }, + set(key: T) { + return { + type: InternalSerializerType.FURY_SET as const, + options: { + key, + }, + }; + }, + bool() { + return { + type: InternalSerializerType.BOOL as const, + }; + }, + object(tag: string, props?: T) { + return { + type: InternalSerializerType.FURY_TYPE_TAG as const, + options: { + tag, + props, + }, + }; + }, + uint8() { + return { + type: InternalSerializerType.UINT8 as const, + }; + }, + uint16() { + return { + type: InternalSerializerType.UINT16 as const, + }; + }, + uint32() { + return { + type: InternalSerializerType.UINT32 as const, + }; + }, + uint64() { + return { + type: InternalSerializerType.UINT64 as const, + }; + }, + int8() { + return { + type: InternalSerializerType.INT8 as const, + }; + }, + int16() { + return { + type: InternalSerializerType.INT16 as const, + }; + }, + int32() { + return { + type: InternalSerializerType.INT32 as const, + }; + }, + int64() { + return { + type: InternalSerializerType.INT64 as const, + }; + }, + float() { + return { + type: InternalSerializerType.FLOAT as const, + }; + }, + double() { + return { + type: InternalSerializerType.DOUBLE as const, + }; + }, + binary() { + return { + type: InternalSerializerType.BINARY as const, + }; + }, + date() { + return { + type: InternalSerializerType.DATE as const, + }; + }, + timestamp() { + return { + type: InternalSerializerType.TIMESTAMP as const, + }; + }, }; diff --git a/javascript/packages/fury/lib/fury.ts b/javascript/packages/fury/lib/fury.ts index 0d67ae175d..0a38031be7 100644 --- a/javascript/packages/fury/lib/fury.ts +++ b/javascript/packages/fury/lib/fury.ts @@ -14,83 +14,82 @@ * limitations under the License. */ -import ClassResolver from './classResolver'; -import { BinaryWriter } from './writer'; -import { BinaryReader } from './reader'; -import { ReferenceResolver } from './referenceResolver'; -import { ConfigFlags, Serializer, Config, InternalSerializerType, Language } from './type'; +import ClassResolver from "./classResolver"; +import { BinaryWriter } from "./writer"; +import { BinaryReader } from "./reader"; +import { ReferenceResolver } from "./referenceResolver"; +import { ConfigFlags, Serializer, Config, InternalSerializerType, Language } from "./type"; export default (config: Config) => { - const binaryReader = BinaryReader(config); - const binaryWriter = BinaryWriter(config); + const binaryReader = BinaryReader(config); + const binaryWriter = BinaryWriter(config); - const classResolver = new ClassResolver(); - const referenceResolver = ReferenceResolver(config, binaryWriter, binaryReader, classResolver); - - const fury = { - config, - deserialize, - serialize, - referenceResolver, - classResolver, - binaryReader, - binaryWriter, - } - classResolver.init(fury); + const classResolver = new ClassResolver(); + const referenceResolver = ReferenceResolver(config, binaryWriter, binaryReader, classResolver); + const fury = { + config, + deserialize, + serialize, + referenceResolver, + classResolver, + binaryReader, + binaryWriter, + }; + classResolver.init(fury); - function deserialize(bytes: Uint8Array, serializer?: Serializer): T | null { - referenceResolver.reset(); - classResolver.reset(); - binaryReader.reset(bytes); - const bitmap = binaryReader.uint8(); - if ((bitmap & ConfigFlags.isNullFlag) === ConfigFlags.isNullFlag) { - return null; - } - const isLittleEndian = (bitmap & ConfigFlags.isLittleEndianFlag) === ConfigFlags.isLittleEndianFlag; - if (!isLittleEndian) { - throw new Error('big endian is not supported now') - } - const isCrossLanguage = (bitmap & ConfigFlags.isCrossLanguageFlag) == ConfigFlags.isCrossLanguageFlag - if (!isCrossLanguage) { - throw new Error('support crosslanguage mode only') - } - binaryReader.uint8(); // skip language - const isOutOfBandEnabled = (bitmap & ConfigFlags.isOutOfBandFlag) === ConfigFlags.isOutOfBandFlag; - if (isOutOfBandEnabled) { - throw new Error('outofband mode is not supported now') - } - binaryReader.int32(); // native object offset. should skip. javascript support cross mode only - binaryReader.int32(); // native object size. should skip. - if (serializer) { - return serializer.read(); - } else { - return classResolver.getSerializerById(InternalSerializerType.ANY).read(); - } + function deserialize(bytes: Uint8Array, serializer?: Serializer): T | null { + referenceResolver.reset(); + classResolver.reset(); + binaryReader.reset(bytes); + const bitmap = binaryReader.uint8(); + if ((bitmap & ConfigFlags.isNullFlag) === ConfigFlags.isNullFlag) { + return null; + } + const isLittleEndian = (bitmap & ConfigFlags.isLittleEndianFlag) === ConfigFlags.isLittleEndianFlag; + if (!isLittleEndian) { + throw new Error("big endian is not supported now"); + } + const isCrossLanguage = (bitmap & ConfigFlags.isCrossLanguageFlag) == ConfigFlags.isCrossLanguageFlag; + if (!isCrossLanguage) { + throw new Error("support crosslanguage mode only"); } + binaryReader.uint8(); // skip language + const isOutOfBandEnabled = (bitmap & ConfigFlags.isOutOfBandFlag) === ConfigFlags.isOutOfBandFlag; + if (isOutOfBandEnabled) { + throw new Error("outofband mode is not supported now"); + } + binaryReader.int32(); // native object offset. should skip. javascript support cross mode only + binaryReader.int32(); // native object size. should skip. + if (serializer) { + return serializer.read(); + } else { + return classResolver.getSerializerById(InternalSerializerType.ANY).read(); + } + } - function serialize(data: T, serializer?: Serializer) { - referenceResolver.reset(); - classResolver.reset(); - binaryWriter.reset(); - let bitmap = 0; - if (data === null) { - bitmap |= ConfigFlags.isNullFlag; - } - bitmap |= ConfigFlags.isLittleEndianFlag; - bitmap |= ConfigFlags.isCrossLanguageFlag - binaryWriter.uint8(bitmap); - binaryWriter.uint8(Language.XLANG); - const cursor = binaryWriter.getCursor(); - binaryWriter.skip(4); // preserve 4-byte for nativeObjects start offsets. - binaryWriter.uint32(0); // nativeObjects length. - if (serializer) { - serializer.write(data); - } else { - classResolver.getSerializerById(InternalSerializerType.ANY).write(data); - } - binaryWriter.setUint32Position(cursor, binaryWriter.getCursor()); // nativeObjects start offsets; - return binaryWriter.dump(); + function serialize(data: T, serializer?: Serializer) { + referenceResolver.reset(); + classResolver.reset(); + binaryWriter.reset(); + let bitmap = 0; + if (data === null) { + bitmap |= ConfigFlags.isNullFlag; + } + bitmap |= ConfigFlags.isLittleEndianFlag; + bitmap |= ConfigFlags.isCrossLanguageFlag; + binaryWriter.uint8(bitmap); + binaryWriter.uint8(Language.XLANG); + const cursor = binaryWriter.getCursor(); + binaryWriter.skip(4); // preserve 4-byte for nativeObjects start offsets. + binaryWriter.uint32(0); // nativeObjects length. + if (serializer) { + serializer.write(data); + } else { + classResolver.getSerializerById(InternalSerializerType.ANY).write(data); } - return fury; -} + binaryWriter.setUint32Position(cursor, binaryWriter.getCursor()); // nativeObjects start offsets; + return binaryWriter.dump(); + } + return fury; +}; diff --git a/javascript/packages/fury/lib/internalSerializer/any.ts b/javascript/packages/fury/lib/internalSerializer/any.ts index e7ba089b2f..6c1b432921 100644 --- a/javascript/packages/fury/lib/internalSerializer/any.ts +++ b/javascript/packages/fury/lib/internalSerializer/any.ts @@ -14,137 +14,132 @@ * limitations under the License. */ - import { Fury, MaxInt32, MinInt32, Serializer } from "../type"; import { InternalSerializerType, RefFlags } from "../type"; - - - export default (fury: Fury) => { - const { binaryReader, binaryWriter, referenceResolver, classResolver } = fury; - - - function detectSerializer(cursor: number) { - const typeId = binaryReader.int16(); - let serializer: Serializer; - if (typeId === InternalSerializerType.FURY_TYPE_TAG) { - serializer = classResolver.getSerializerByTag(classResolver.detectTag(binaryReader)); - } else { - serializer = classResolver.getSerializerById(typeId); - } - if (!serializer) { - throw new Error(`cant find implements of typeId: ${typeId}`); - } - binaryReader.setCursor(cursor); - - return serializer; + const { binaryReader, binaryWriter, referenceResolver, classResolver } = fury; + + function detectSerializer(cursor: number) { + const typeId = binaryReader.int16(); + let serializer: Serializer; + if (typeId === InternalSerializerType.FURY_TYPE_TAG) { + serializer = classResolver.getSerializerByTag(classResolver.detectTag(binaryReader)); + } else { + serializer = classResolver.getSerializerById(typeId); } - - return { - read: () => { - const cursor = binaryReader.getCursor(); - const flag = referenceResolver.readRefFlag(); - switch (flag) { - case RefFlags.RefValueFlag: - return detectSerializer(cursor).read(); - case RefFlags.RefFlag: - return referenceResolver.getReadObjectByRefId(binaryReader.varInt32()); - case RefFlags.NullFlag: - return null; - case RefFlags.NotNullValueFlag: - return detectSerializer(cursor).read(); - } - }, - write: (v: any) => { - const { write: writeInt64, config: int64Config } = classResolver.getSerializerById(InternalSerializerType.INT64); - const { write: writeDouble, config: doubleConfig } = classResolver.getSerializerById(InternalSerializerType.DOUBLE); - const { write: writeInt32, config: int32Config } = classResolver.getSerializerById(InternalSerializerType.INT32); - const { write: writeBool, config: boolConfig } = classResolver.getSerializerById(InternalSerializerType.BOOL); - const { write: stringWrite, config: stringConfig } = classResolver.getSerializerById(InternalSerializerType.STRING); - const { write: arrayWrite, config: arrayConfig } = classResolver.getSerializerById(InternalSerializerType.ARRAY); - const { write: mapWrite, config: mapConfig } = classResolver.getSerializerById(InternalSerializerType.MAP); - const { write: setWrite, config: setConfig } = classResolver.getSerializerById(InternalSerializerType.FURY_SET); - const { write: timestampWrite, config: timestampConfig } = classResolver.getSerializerById(InternalSerializerType.TIMESTAMP); - - // NullFlag - if (v === null || v === undefined) { - binaryWriter.reserve(1); - binaryWriter.int8(RefFlags.NullFlag); // null - return; - } - - // NotNullValueFlag - if (typeof v === "number") { - if (Number.isNaN(v) || !Number.isFinite(v)) { - binaryWriter.reserve(1); - binaryWriter.int8(RefFlags.NullFlag); // null - return; - } - if (Number.isInteger(v)) { - if (v > MaxInt32 || v < MinInt32) { - binaryWriter.reserve(int64Config().reserve); - writeInt64(BigInt(v)); - return; - } else { - binaryWriter.reserve(int32Config().reserve); - writeInt32(v); - return; - } - } else { - binaryWriter.reserve(doubleConfig().reserve); - writeDouble(v); - return; - } - } - - if (typeof v === "bigint") { - binaryWriter.reserve(int64Config().reserve); - writeInt64(v) - return; - } - - if (typeof v === "boolean") { - binaryWriter.reserve(boolConfig().reserve); - writeBool(v) - return; - } - - if (v instanceof Date) { - binaryWriter.reserve(timestampConfig().reserve); - timestampWrite(v); - return; - } - - if (typeof v === "string") { - binaryWriter.reserve(stringConfig().reserve); - stringWrite(v); - return; - } - - if (v instanceof Map) { - binaryWriter.reserve(5); - binaryWriter.reserve(mapConfig().reserve); - mapWrite(v); - return; - } - if (v instanceof Set) { - binaryWriter.reserve(setConfig().reserve); - setWrite(v); - return; - } - if (Array.isArray(v)) { - binaryWriter.reserve(arrayConfig().reserve); - arrayWrite(v); - return; - } - - throw new Error(`serializer not support ${typeof v} yet`); - }, - config: () => { - return { - reserve: 11, - } - } + if (!serializer) { + throw new Error(`cant find implements of typeId: ${typeId}`); } -} + binaryReader.setCursor(cursor); + + return serializer; + } + + return { + read: () => { + const cursor = binaryReader.getCursor(); + const flag = referenceResolver.readRefFlag(); + switch (flag) { + case RefFlags.RefValueFlag: + return detectSerializer(cursor).read(); + case RefFlags.RefFlag: + return referenceResolver.getReadObjectByRefId(binaryReader.varInt32()); + case RefFlags.NullFlag: + return null; + case RefFlags.NotNullValueFlag: + return detectSerializer(cursor).read(); + } + }, + write: (v: any) => { + const { write: writeInt64, config: int64Config } = classResolver.getSerializerById(InternalSerializerType.INT64); + const { write: writeDouble, config: doubleConfig } = classResolver.getSerializerById(InternalSerializerType.DOUBLE); + const { write: writeInt32, config: int32Config } = classResolver.getSerializerById(InternalSerializerType.INT32); + const { write: writeBool, config: boolConfig } = classResolver.getSerializerById(InternalSerializerType.BOOL); + const { write: stringWrite, config: stringConfig } = classResolver.getSerializerById(InternalSerializerType.STRING); + const { write: arrayWrite, config: arrayConfig } = classResolver.getSerializerById(InternalSerializerType.ARRAY); + const { write: mapWrite, config: mapConfig } = classResolver.getSerializerById(InternalSerializerType.MAP); + const { write: setWrite, config: setConfig } = classResolver.getSerializerById(InternalSerializerType.FURY_SET); + const { write: timestampWrite, config: timestampConfig } = classResolver.getSerializerById(InternalSerializerType.TIMESTAMP); + + // NullFlag + if (v === null || v === undefined) { + binaryWriter.reserve(1); + binaryWriter.int8(RefFlags.NullFlag); // null + return; + } + + // NotNullValueFlag + if (typeof v === "number") { + if (Number.isNaN(v) || !Number.isFinite(v)) { + binaryWriter.reserve(1); + binaryWriter.int8(RefFlags.NullFlag); // null + return; + } + if (Number.isInteger(v)) { + if (v > MaxInt32 || v < MinInt32) { + binaryWriter.reserve(int64Config().reserve); + writeInt64(BigInt(v)); + return; + } else { + binaryWriter.reserve(int32Config().reserve); + writeInt32(v); + return; + } + } else { + binaryWriter.reserve(doubleConfig().reserve); + writeDouble(v); + return; + } + } + + if (typeof v === "bigint") { + binaryWriter.reserve(int64Config().reserve); + writeInt64(v); + return; + } + + if (typeof v === "boolean") { + binaryWriter.reserve(boolConfig().reserve); + writeBool(v); + return; + } + + if (v instanceof Date) { + binaryWriter.reserve(timestampConfig().reserve); + timestampWrite(v); + return; + } + + if (typeof v === "string") { + binaryWriter.reserve(stringConfig().reserve); + stringWrite(v); + return; + } + + if (v instanceof Map) { + binaryWriter.reserve(5); + binaryWriter.reserve(mapConfig().reserve); + mapWrite(v); + return; + } + if (v instanceof Set) { + binaryWriter.reserve(setConfig().reserve); + setWrite(v); + return; + } + if (Array.isArray(v)) { + binaryWriter.reserve(arrayConfig().reserve); + arrayWrite(v); + return; + } + + throw new Error(`serializer not support ${typeof v} yet`); + }, + config: () => { + return { + reserve: 11, + }; + }, + }; +}; diff --git a/javascript/packages/fury/lib/internalSerializer/array.ts b/javascript/packages/fury/lib/internalSerializer/array.ts index 30d2e6be25..0a15872049 100644 --- a/javascript/packages/fury/lib/internalSerializer/array.ts +++ b/javascript/packages/fury/lib/internalSerializer/array.ts @@ -17,50 +17,49 @@ import { InternalSerializerType, Serializer } from "../type"; import { Fury } from "../type"; - export const buildArray = (fury: Fury, item: Serializer, type: InternalSerializerType) => { - const { binaryReader, binaryWriter, referenceResolver } = fury; + const { binaryReader, binaryWriter, referenceResolver } = fury; + + const { pushReadObject } = referenceResolver; + const { varInt32: writeVarInt32, reserve: reserves } = binaryWriter; + const { varInt32: readVarInt32 } = binaryReader; + const { write, read } = item; + const innerHeadSize = (item.config().reserve); + return { + ...referenceResolver.deref(() => { + const len = readVarInt32(); + const result = new Array(len); + pushReadObject(result); + for (let i = 0; i < result.length; i++) { + result[i] = read(); + } + return result; + }), + write: referenceResolver.withNullableOrRefWriter(type, (v: any[]) => { + writeVarInt32(v.length); - const { pushReadObject } = referenceResolver; - const { varInt32: writeVarInt32, reserve: reserves } = binaryWriter; - const { varInt32: readVarInt32 } = binaryReader; - const { write, read } = item; - const innerHeadSize = (item.config().reserve); - return { - ...referenceResolver.deref(() => { - const len = readVarInt32(); - const result = new Array(len); - pushReadObject(result); - for (let i = 0; i < result.length; i++) { - result[i] = read(); - } - return result; - }), - write: referenceResolver.withNullableOrRefWriter(type, (v: any[]) => { - writeVarInt32(v.length); + reserves(innerHeadSize * v.length); - reserves(innerHeadSize * v.length); - - for (const x of v) { - write(x); - } - }), - config: () => { - return { - reserve: 7, - } - } - } -} + for (const x of v) { + write(x); + } + }), + config: () => { + return { + reserve: 7, + }; + }, + }; +}; const buildTypedArray = (fury: Fury, serializeType: InternalSerializerType, typeArrayType: InternalSerializerType) => { - const serializer = fury.classResolver.getSerializerById(serializeType); - return buildArray(fury, { - read: serializer.readWithoutType!, - write: serializer.writeWithoutType!, - config: serializer.config, - }, typeArrayType) -} + const serializer = fury.classResolver.getSerializerById(serializeType); + return buildArray(fury, { + read: serializer.readWithoutType!, + write: serializer.writeWithoutType!, + config: serializer.config, + }, typeArrayType); +}; export const stringArraySerializer = (fury: Fury) => buildTypedArray(fury, InternalSerializerType.STRING, InternalSerializerType.FURY_STRING_ARRAY); export const boolArraySerializer = (fury: Fury) => buildTypedArray(fury, InternalSerializerType.BOOL, InternalSerializerType.FURY_PRIMITIVE_BOOL_ARRAY); @@ -70,6 +69,4 @@ export const floatArraySerializer = (fury: Fury) => buildTypedArray(fury, Intern export const doubleArraySerializer = (fury: Fury) => buildTypedArray(fury, InternalSerializerType.DOUBLE, InternalSerializerType.FURY_PRIMITIVE_DOUBLE_ARRAY); export const shortArraySerializer = (fury: Fury) => buildTypedArray(fury, InternalSerializerType.INT16, InternalSerializerType.FURY_PRIMITIVE_SHORT_ARRAY); - - export const arraySerializer = (fury: Fury, item: Serializer) => buildArray(fury, item, InternalSerializerType.ARRAY); diff --git a/javascript/packages/fury/lib/internalSerializer/binary.ts b/javascript/packages/fury/lib/internalSerializer/binary.ts index fdfb05fca1..03f1e8dbe6 100644 --- a/javascript/packages/fury/lib/internalSerializer/binary.ts +++ b/javascript/packages/fury/lib/internalSerializer/binary.ts @@ -17,31 +17,30 @@ import { Fury } from "../type"; import { InternalSerializerType } from "../type"; - export default (fury: Fury) => { - const { binaryReader, binaryWriter, referenceResolver } = fury; + const { binaryReader, binaryWriter, referenceResolver } = fury; - const { uint8: writeUInt8, int32: writeInt32, buffer: writeBuffer } = binaryWriter; - const { uint8: readUInt8, int32: readInt32, buffer: readBuffer } = binaryReader; - const { pushReadObject } = referenceResolver; + const { uint8: writeUInt8, int32: writeInt32, buffer: writeBuffer } = binaryWriter; + const { uint8: readUInt8, int32: readInt32, buffer: readBuffer } = binaryReader; + const { pushReadObject } = referenceResolver; - return { - ...referenceResolver.deref(() => { - readUInt8(); // isInBand - const len = readInt32(); - const result = readBuffer(len); - pushReadObject(result); - return result - }), - write: referenceResolver.withNullableOrRefWriter(InternalSerializerType.BINARY, (v: Uint8Array) => { - writeUInt8(1); // is inBand - writeInt32(v.byteLength); - writeBuffer(v); - }), - config: () => { - return { - reserve: 8, - } - } - } -} \ No newline at end of file + return { + ...referenceResolver.deref(() => { + readUInt8(); // isInBand + const len = readInt32(); + const result = readBuffer(len); + pushReadObject(result); + return result; + }), + write: referenceResolver.withNullableOrRefWriter(InternalSerializerType.BINARY, (v: Uint8Array) => { + writeUInt8(1); // is inBand + writeInt32(v.byteLength); + writeBuffer(v); + }), + config: () => { + return { + reserve: 8, + }; + }, + }; +}; diff --git a/javascript/packages/fury/lib/internalSerializer/bool.ts b/javascript/packages/fury/lib/internalSerializer/bool.ts index c966a4a340..00f0793058 100644 --- a/javascript/packages/fury/lib/internalSerializer/bool.ts +++ b/javascript/packages/fury/lib/internalSerializer/bool.ts @@ -17,26 +17,25 @@ import { Fury } from "../type"; import { InternalSerializerType, RefFlags } from "../type"; - export default (fury: Fury) => { - const { binaryReader, binaryWriter, referenceResolver } = fury; - const { uint8: readUInt8 } = binaryReader; - const { int8: writeInt8, uint8: writeUInt8 } = binaryWriter; - return { - ...referenceResolver.deref(() => { - return readUInt8() === 0 ? false : true - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.BOOL, false, (v: boolean) => { - writeUInt8(v ? 1 : 0) - }), - writeWithoutType: (v: boolean) => { - writeInt8(RefFlags.NotNullValueFlag); - writeUInt8(v ? 1 : 0) - }, - config: () => { - return { - reserve: 4, - } - } - } -} \ No newline at end of file + const { binaryReader, binaryWriter, referenceResolver } = fury; + const { uint8: readUInt8 } = binaryReader; + const { int8: writeInt8, uint8: writeUInt8 } = binaryWriter; + return { + ...referenceResolver.deref(() => { + return readUInt8() === 0 ? false : true; + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.BOOL, false, (v: boolean) => { + writeUInt8(v ? 1 : 0); + }), + writeWithoutType: (v: boolean) => { + writeInt8(RefFlags.NotNullValueFlag); + writeUInt8(v ? 1 : 0); + }, + config: () => { + return { + reserve: 4, + }; + }, + }; +}; diff --git a/javascript/packages/fury/lib/internalSerializer/datetime.ts b/javascript/packages/fury/lib/internalSerializer/datetime.ts index e43aaa4a28..f463b468b3 100644 --- a/javascript/packages/fury/lib/internalSerializer/datetime.ts +++ b/javascript/packages/fury/lib/internalSerializer/datetime.ts @@ -14,50 +14,50 @@ * limitations under the License. */ -import {Fury} from "../type"; +import { Fury } from "../type"; import { InternalSerializerType } from "../type"; -const epochDate = new Date('1970/01/01 00:00'); +const epochDate = new Date("1970/01/01 00:00"); const epoch = epochDate.getTime(); export const timestampSerializer = (fury: Fury) => { - const { binaryReader, binaryWriter, referenceResolver} = fury; - const { int64: writeInt64 } = binaryWriter; - const { int64: readInt64} = binaryReader; + const { binaryReader, binaryWriter, referenceResolver } = fury; + const { int64: writeInt64 } = binaryWriter; + const { int64: readInt64 } = binaryReader; - return { - ...referenceResolver.deref(() => { - return new Date(Number(readInt64())); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.TIMESTAMP, epochDate, (v: Date) => { - writeInt64(BigInt(v.getTime())); - }), - config: () => { - return { - reserve: 11 - } - } - } -} + return { + ...referenceResolver.deref(() => { + return new Date(Number(readInt64())); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.TIMESTAMP, epochDate, (v: Date) => { + writeInt64(BigInt(v.getTime())); + }), + config: () => { + return { + reserve: 11, + }; + }, + }; +}; export const dateSerializer = (fury: Fury) => { - const { binaryReader, binaryWriter, referenceResolver} = fury; - const { int32: writeInt32 } = binaryWriter; - const { int32: readInt32} = binaryReader; - return { - ...referenceResolver.deref(() => { - const day = readInt32(); - return new Date(epoch + (day * (24*60*60) * 1000)); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.DATE, epochDate, (v: Date) => { - const diff = v.getTime() - epoch; - const day = Math.floor(diff / 1000 / (24*60*60)) - writeInt32(day); - }), - config: () => { - return { - reserve: 7, - } - } - } -} + const { binaryReader, binaryWriter, referenceResolver } = fury; + const { int32: writeInt32 } = binaryWriter; + const { int32: readInt32 } = binaryReader; + return { + ...referenceResolver.deref(() => { + const day = readInt32(); + return new Date(epoch + (day * (24 * 60 * 60) * 1000)); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.DATE, epochDate, (v: Date) => { + const diff = v.getTime() - epoch; + const day = Math.floor(diff / 1000 / (24 * 60 * 60)); + writeInt32(day); + }), + config: () => { + return { + reserve: 7, + }; + }, + }; +}; diff --git a/javascript/packages/fury/lib/internalSerializer/map.ts b/javascript/packages/fury/lib/internalSerializer/map.ts index d59327508d..f6349f67f9 100644 --- a/javascript/packages/fury/lib/internalSerializer/map.ts +++ b/javascript/packages/fury/lib/internalSerializer/map.ts @@ -16,40 +16,38 @@ import { InternalSerializerType, Fury, Serializer } from "../type"; - - export default (fury: Fury, keySerializer: Serializer, valueSerializer: Serializer) => { - const { binaryReader, binaryWriter, referenceResolver } = fury; - const { varInt32: readVarInt32 } = binaryReader; + const { binaryReader, binaryWriter, referenceResolver } = fury; + const { varInt32: readVarInt32 } = binaryReader; - const { varInt32: writeVarInt32, reserve: reserves } = binaryWriter; - const { pushReadObject } = referenceResolver; - const innerHeadSize = keySerializer.config().reserve + valueSerializer.config().reserve; - return { - ...referenceResolver.deref(() => { - const len = readVarInt32(); - const result = new Map(); - pushReadObject(result); - for (let index = 0; index < len; index++) { - const key = keySerializer.read(); - const value = valueSerializer.read(); - result.set(key, value); - } - return result; - }), - write: referenceResolver.withNullableOrRefWriter(InternalSerializerType.MAP, (v: Map) => { - const len = v.size; - writeVarInt32(len); - reserves(innerHeadSize * v.size); - for (const [key, value] of v.entries()) { - keySerializer.write(key); - valueSerializer.write(value); - } - }), - config: () => { - return { - reserve: 7, - } - } - } -} + const { varInt32: writeVarInt32, reserve: reserves } = binaryWriter; + const { pushReadObject } = referenceResolver; + const innerHeadSize = keySerializer.config().reserve + valueSerializer.config().reserve; + return { + ...referenceResolver.deref(() => { + const len = readVarInt32(); + const result = new Map(); + pushReadObject(result); + for (let index = 0; index < len; index++) { + const key = keySerializer.read(); + const value = valueSerializer.read(); + result.set(key, value); + } + return result; + }), + write: referenceResolver.withNullableOrRefWriter(InternalSerializerType.MAP, (v: Map) => { + const len = v.size; + writeVarInt32(len); + reserves(innerHeadSize * v.size); + for (const [key, value] of v.entries()) { + keySerializer.write(key); + valueSerializer.write(value); + } + }), + config: () => { + return { + reserve: 7, + }; + }, + }; +}; diff --git a/javascript/packages/fury/lib/internalSerializer/number.ts b/javascript/packages/fury/lib/internalSerializer/number.ts index 9751d8efaf..df80cca4c4 100644 --- a/javascript/packages/fury/lib/internalSerializer/number.ts +++ b/javascript/packages/fury/lib/internalSerializer/number.ts @@ -17,219 +17,219 @@ import { InternalSerializerType, RefFlags, Fury } from "../type"; export const uInt8Serializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8, uint8: writeUInt8 } = binaryWriter; - const { uint8: readUInt8 } = binaryReader; - return { - ...referenceResolver.deref(() => { - return readUInt8(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.UINT8, 0, (v: number) => { - writeUInt8(v); - }), - config: () => { - return { - reserve: 4, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8, uint8: writeUInt8 } = binaryWriter; + const { uint8: readUInt8 } = binaryReader; + return { + ...referenceResolver.deref(() => { + return readUInt8(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.UINT8, 0, (v: number) => { + writeUInt8(v); + }), + config: () => { + return { + reserve: 4, + }; + }, + }; }; export const floatSerializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8, float: writeFloat } = binaryWriter; - const { float: readFloat } = binaryReader; - - return { - ...referenceResolver.deref(() => { - return readFloat(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.FLOAT, 0, (v: number) => { - writeFloat(v); - }), - writeWithoutType: (v: number) => { - writeInt8(RefFlags.NotNullValueFlag); - writeFloat(v); - }, - config: () => { - return { - reserve: 7, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8, float: writeFloat } = binaryWriter; + const { float: readFloat } = binaryReader; + + return { + ...referenceResolver.deref(() => { + return readFloat(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.FLOAT, 0, (v: number) => { + writeFloat(v); + }), + writeWithoutType: (v: number) => { + writeInt8(RefFlags.NotNullValueFlag); + writeFloat(v); + }, + config: () => { + return { + reserve: 7, + }; + }, + }; }; export const doubleSerializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8, double: writeDouble } = binaryWriter; - const { double: readDouble } = binaryReader; - - return { - ...referenceResolver.deref(() => { - return readDouble(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.DOUBLE, 0, (v: number) => { - writeDouble(v); - }), - writeWithoutType: (v: number) => { - writeInt8(RefFlags.NotNullValueFlag); - writeDouble(v); - }, - config: () => { - return { - reserve: 11, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8, double: writeDouble } = binaryWriter; + const { double: readDouble } = binaryReader; + + return { + ...referenceResolver.deref(() => { + return readDouble(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.DOUBLE, 0, (v: number) => { + writeDouble(v); + }), + writeWithoutType: (v: number) => { + writeInt8(RefFlags.NotNullValueFlag); + writeDouble(v); + }, + config: () => { + return { + reserve: 11, + }; + }, + }; }; export const int8Serializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8 } = binaryWriter; - const { int8: readInt8 } = binaryReader; - return { - ...referenceResolver.deref(() => { - return readInt8(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.INT8, 0, (v: number) => { - writeInt8(v); - }), - config: () => { - return { - reserve: 4, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8 } = binaryWriter; + const { int8: readInt8 } = binaryReader; + return { + ...referenceResolver.deref(() => { + return readInt8(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.INT8, 0, (v: number) => { + writeInt8(v); + }), + config: () => { + return { + reserve: 4, + }; + }, + }; }; export const uInt16Serializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8, uint16: writeUInt16 } = binaryWriter; - const { uint16: readUInt16 } = binaryReader; - - return { - ...referenceResolver.deref(() => { - return readUInt16(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.UINT16, 0, (v: number) => { - writeUInt16(v); - }), - config: () => { - return { - reserve: 5, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8, uint16: writeUInt16 } = binaryWriter; + const { uint16: readUInt16 } = binaryReader; + + return { + ...referenceResolver.deref(() => { + return readUInt16(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.UINT16, 0, (v: number) => { + writeUInt16(v); + }), + config: () => { + return { + reserve: 5, + }; + }, + }; }; export const int16Serializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int16: writeInt16, int8: writeInt8 } = binaryWriter; - const { int16: readInt16 } = binaryReader; - - return { - ...referenceResolver.deref(() => { - return readInt16(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.INT16, 0, (v: number) => { - writeInt16(v); - }), - writeWithoutType: (v: number) => { - writeInt8(RefFlags.NotNullValueFlag); - writeInt16(v); - }, - config: () => { - return { - reserve: 5, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int16: writeInt16, int8: writeInt8 } = binaryWriter; + const { int16: readInt16 } = binaryReader; + + return { + ...referenceResolver.deref(() => { + return readInt16(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.INT16, 0, (v: number) => { + writeInt16(v); + }), + writeWithoutType: (v: number) => { + writeInt8(RefFlags.NotNullValueFlag); + writeInt16(v); + }, + config: () => { + return { + reserve: 5, + }; + }, + }; }; export const uInt32Serializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8, uint32: writeUInt32 } = binaryWriter; - const { uint32: readUInt32 } = binaryReader; - - return { - ...referenceResolver.deref(() => { - return readUInt32(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.UINT32, 0, (v: number) => { - writeUInt32(v); - }), - config: () => { - return { - reserve: 7, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8, uint32: writeUInt32 } = binaryWriter; + const { uint32: readUInt32 } = binaryReader; + + return { + ...referenceResolver.deref(() => { + return readUInt32(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.UINT32, 0, (v: number) => { + writeUInt32(v); + }), + config: () => { + return { + reserve: 7, + }; + }, + }; }; export const int32Serializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8, int32: writeInt32 } = binaryWriter; - const { int32: readInt32 } = binaryReader; - - return { - ...referenceResolver.deref(() => { - return readInt32(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.INT32, 0, (v: number) => { - writeInt32(v); - }), - writeWithoutType: (v: number) => { - writeInt8(RefFlags.NotNullValueFlag); - writeInt32(v); - }, - config: () => { - return { - reserve: 7, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8, int32: writeInt32 } = binaryWriter; + const { int32: readInt32 } = binaryReader; + + return { + ...referenceResolver.deref(() => { + return readInt32(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.INT32, 0, (v: number) => { + writeInt32(v); + }), + writeWithoutType: (v: number) => { + writeInt8(RefFlags.NotNullValueFlag); + writeInt32(v); + }, + config: () => { + return { + reserve: 7, + }; + }, + }; }; export const uInt64Serializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8, uint64: writeUInt64 } = binaryWriter; - const { uint64: readUInt64 } = binaryReader; - - return { - ...referenceResolver.deref(() => { - return readUInt64(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.UINT64, BigInt(0), (v: bigint) => { - writeUInt64(v); - }), - config: () => { - return { - reserve: 11, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8, uint64: writeUInt64 } = binaryWriter; + const { uint64: readUInt64 } = binaryReader; + + return { + ...referenceResolver.deref(() => { + return readUInt64(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.UINT64, BigInt(0), (v: bigint) => { + writeUInt64(v); + }), + config: () => { + return { + reserve: 11, + }; + }, + }; }; export const int64Serializer = (fury: Fury) => { - const { binaryWriter, binaryReader, referenceResolver } = fury; - const { int8: writeInt8, int64: writeInt64 } = binaryWriter; - const { int64: readInt64 } = binaryReader; - - return { - ...referenceResolver.deref(() => { - return readInt64(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.INT64, BigInt(0), (v: bigint) => { - writeInt64(v); - }), - writeWithoutType: (v: bigint) => { - writeInt8(RefFlags.NotNullValueFlag); - writeInt64(v); - }, - config: () => { - return { - reserve: 11, - } - } - }; + const { binaryWriter, binaryReader, referenceResolver } = fury; + const { int8: writeInt8, int64: writeInt64 } = binaryWriter; + const { int64: readInt64 } = binaryReader; + + return { + ...referenceResolver.deref(() => { + return readInt64(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.INT64, BigInt(0), (v: bigint) => { + writeInt64(v); + }), + writeWithoutType: (v: bigint) => { + writeInt8(RefFlags.NotNullValueFlag); + writeInt64(v); + }, + config: () => { + return { + reserve: 11, + }; + }, + }; }; diff --git a/javascript/packages/fury/lib/internalSerializer/set.ts b/javascript/packages/fury/lib/internalSerializer/set.ts index 2327a23ff3..6ed41b6984 100644 --- a/javascript/packages/fury/lib/internalSerializer/set.ts +++ b/javascript/packages/fury/lib/internalSerializer/set.ts @@ -17,35 +17,34 @@ import { Fury, Serializer } from "../type"; import { InternalSerializerType } from "../type"; - export default (fury: Fury, nestedSerializer: Serializer) => { - const { binaryReader, binaryWriter, referenceResolver } = fury; - const { varInt32: writeVarInt32, reserve: reserves } = binaryWriter; - const { varInt32: readVarInt32 } = binaryReader; - const { pushReadObject } = referenceResolver; - const innerHeadSize = nestedSerializer.config().reserve; - return { - ...referenceResolver.deref(() => { - const len = readVarInt32(); - const result = new Set(); - pushReadObject(result); - for (let index = 0; index < len; index++) { - result.add(nestedSerializer.read()); - } - return result; - }), - write: referenceResolver.withNullableOrRefWriter(InternalSerializerType.FURY_SET, (v: Set) => { - const len = v.size; - writeVarInt32(len); - reserves(innerHeadSize * v.size); - for (const value of v.values()) { - nestedSerializer.write(value); - } - }), - config: () => { - return { - reserve: 7, - } - } - } -} + const { binaryReader, binaryWriter, referenceResolver } = fury; + const { varInt32: writeVarInt32, reserve: reserves } = binaryWriter; + const { varInt32: readVarInt32 } = binaryReader; + const { pushReadObject } = referenceResolver; + const innerHeadSize = nestedSerializer.config().reserve; + return { + ...referenceResolver.deref(() => { + const len = readVarInt32(); + const result = new Set(); + pushReadObject(result); + for (let index = 0; index < len; index++) { + result.add(nestedSerializer.read()); + } + return result; + }), + write: referenceResolver.withNullableOrRefWriter(InternalSerializerType.FURY_SET, (v: Set) => { + const len = v.size; + writeVarInt32(len); + reserves(innerHeadSize * v.size); + for (const value of v.values()) { + nestedSerializer.write(value); + } + }), + config: () => { + return { + reserve: 7, + }; + }, + }; +}; diff --git a/javascript/packages/fury/lib/internalSerializer/string.ts b/javascript/packages/fury/lib/internalSerializer/string.ts index 2eea88b1e9..cda78cee92 100644 --- a/javascript/packages/fury/lib/internalSerializer/string.ts +++ b/javascript/packages/fury/lib/internalSerializer/string.ts @@ -17,32 +17,30 @@ import { Fury } from "../type"; import { InternalSerializerType, RefFlags } from "../type"; - - export default (fury: Fury) => { - const { binaryReader, binaryWriter, referenceResolver } = fury; - const { stringOfVarInt32: writeStringOfVarInt32, int8 } = binaryWriter - const { stringOfVarInt32: readStringOfVarInt32 } = binaryReader; + const { binaryReader, binaryWriter, referenceResolver } = fury; + const { stringOfVarInt32: writeStringOfVarInt32, int8 } = binaryWriter; + const { stringOfVarInt32: readStringOfVarInt32 } = binaryReader; - return { - ...referenceResolver.deref(() => { - return readStringOfVarInt32(); - }), - write: referenceResolver.withNotNullableWriter(InternalSerializerType.STRING, "", (v: string) => { - writeStringOfVarInt32(v); - }), - writeWithoutType: (v: string) => { - if (v === null) { - binaryWriter.int8(RefFlags.NullFlag); - return; - } - int8(RefFlags.NotNullValueFlag); - writeStringOfVarInt32(v); - }, - config: () => { - return { - reserve: 8, - } - } - } -} + return { + ...referenceResolver.deref(() => { + return readStringOfVarInt32(); + }), + write: referenceResolver.withNotNullableWriter(InternalSerializerType.STRING, "", (v: string) => { + writeStringOfVarInt32(v); + }), + writeWithoutType: (v: string) => { + if (v === null) { + binaryWriter.int8(RefFlags.NullFlag); + return; + } + int8(RefFlags.NotNullValueFlag); + writeStringOfVarInt32(v); + }, + config: () => { + return { + reserve: 8, + }; + }, + }; +}; diff --git a/javascript/packages/fury/lib/internalSerializer/tuple.ts b/javascript/packages/fury/lib/internalSerializer/tuple.ts index 285a9aae0a..56b9e8e2ae 100644 --- a/javascript/packages/fury/lib/internalSerializer/tuple.ts +++ b/javascript/packages/fury/lib/internalSerializer/tuple.ts @@ -17,38 +17,37 @@ import { InternalSerializerType, Serializer } from "../type"; import { Fury } from "../type"; - export const tupleSerializer = (fury: Fury, serializers: Serializer[]) => { - const { binaryReader, binaryWriter, referenceResolver } = fury; + const { binaryReader, binaryWriter, referenceResolver } = fury; + + const { pushReadObject } = referenceResolver; + const { varInt32: writeVarInt32, reserve: reserves } = binaryWriter; + const { varInt32: readVarInt32 } = binaryReader; - const { pushReadObject } = referenceResolver; - const { varInt32: writeVarInt32, reserve: reserves } = binaryWriter; - const { varInt32: readVarInt32 } = binaryReader; + return { + ...referenceResolver.deref(() => { + const len = readVarInt32(); + const result = new Array(len); + pushReadObject(result); + for (let i = 0; i < len; i++) { + const item = serializers[i]; + result[i] = item.read(); + } + return result; + }), + write: referenceResolver.withNullableOrRefWriter(InternalSerializerType.TUPLE, (v: any[]) => { + writeVarInt32(serializers.length); - return { - ...referenceResolver.deref(() => { - const len = readVarInt32(); - const result = new Array(len); - pushReadObject(result); - for (let i = 0; i < len; i++) { - const item = serializers[i]; - result[i] = item.read(); - } - return result; - }), - write: referenceResolver.withNullableOrRefWriter(InternalSerializerType.TUPLE, (v: any[]) => { - writeVarInt32(serializers.length); - - for (let i = 0; i < serializers.length; i++) { - const item = serializers[i]; - reserves(item.config().reserve); - item.write(v[i]); - } - }), - config: () => { - return { - reserve: 7, - } - } - } -} + for (let i = 0; i < serializers.length; i++) { + const item = serializers[i]; + reserves(item.config().reserve); + item.write(v[i]); + } + }), + config: () => { + return { + reserve: 7, + }; + }, + }; +}; diff --git a/javascript/packages/fury/lib/platformBuffer.ts b/javascript/packages/fury/lib/platformBuffer.ts index 85959f2601..6c09410c4f 100644 --- a/javascript/packages/fury/lib/platformBuffer.ts +++ b/javascript/packages/fury/lib/platformBuffer.ts @@ -17,119 +17,118 @@ import { isNodeEnv } from "./util"; export interface PlatformBuffer extends Uint8Array { - latin1Slice(start: number, end: number): string, - utf8Slice(start: number, end: number): string, - latin1Write(v: string, offset: number): void, - utf8Write(v: string, offset: number): void, - copy(target: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): void + latin1Slice(start: number, end: number): string + utf8Slice(start: number, end: number): string + latin1Write(v: string, offset: number): void + utf8Write(v: string, offset: number): void + copy(target: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): void } - export class BrowserBuffer extends Uint8Array implements PlatformBuffer { - static alloc(size: number) { - return new BrowserBuffer(new Uint8Array(size)); - } + static alloc(size: number) { + return new BrowserBuffer(new Uint8Array(size)); + } - latin1Write(string: string, offset: number) { - for (let index = 0; index < string.length; index++) { - this[offset++] = string.charCodeAt(index); - } + latin1Write(string: string, offset: number) { + for (let index = 0; index < string.length; index++) { + this[offset++] = string.charCodeAt(index); } + } - utf8Write(string: string, offset: number) { - let c1: number; - let c2: number; - for (let i = 0; i < string.length; ++i) { - c1 = string.charCodeAt(i); - if (c1 < 128) { - this[offset++] = c1; - } else if (c1 < 2048) { - this[offset++] = (c1 >> 6) | 192; - this[offset++] = (c1 & 63) | 128; - } else if ( - (c1 & 0xfc00) === 0xd800 && - ((c2 = string.charCodeAt(i + 1)) & 0xfc00) === 0xdc00 - ) { - c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff); - ++i; - this[offset++] = (c1 >> 18) | 240; - this[offset++] = ((c1 >> 12) & 63) | 128; - this[offset++] = ((c1 >> 6) & 63) | 128; - this[offset++] = (c1 & 63) | 128; - } else { - this[offset++] = (c1 >> 12) | 224; - this[offset++] = ((c1 >> 6) & 63) | 128; - this[offset++] = (c1 & 63) | 128; - } - } + utf8Write(string: string, offset: number) { + let c1: number; + let c2: number; + for (let i = 0; i < string.length; ++i) { + c1 = string.charCodeAt(i); + if (c1 < 128) { + this[offset++] = c1; + } else if (c1 < 2048) { + this[offset++] = (c1 >> 6) | 192; + this[offset++] = (c1 & 63) | 128; + } else if ( + (c1 & 0xfc00) === 0xd800 + && ((c2 = string.charCodeAt(i + 1)) & 0xfc00) === 0xdc00 + ) { + c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff); + ++i; + this[offset++] = (c1 >> 18) | 240; + this[offset++] = ((c1 >> 12) & 63) | 128; + this[offset++] = ((c1 >> 6) & 63) | 128; + this[offset++] = (c1 & 63) | 128; + } else { + this[offset++] = (c1 >> 12) | 224; + this[offset++] = ((c1 >> 6) & 63) | 128; + this[offset++] = (c1 & 63) | 128; + } } + } - latin1Slice(start: number, end: number) { - if (end - start < 1) { - return ""; - } - let str = ""; - for (let i = start; i < end;) { - str += String.fromCharCode(this[i++]); - } - return str; + latin1Slice(start: number, end: number) { + if (end - start < 1) { + return ""; } - - utf8Slice(start: number, end: number) { - if (end - start < 1) { - return ""; - } - let str = ""; - for (let i = start; i < end;) { - const t = this[i++]; - if (t <= 0x7F) { - str += String.fromCharCode(t); - } else if (t >= 0xC0 && t < 0xE0) { - str += String.fromCharCode((t & 0x1F) << 6 | this[i++] & 0x3F); - } else if (t >= 0xE0 && t < 0xF0) { - str += String.fromCharCode((t & 0xF) << 12 | (this[i++] & 0x3F) << 6 | this[i++] & 0x3F); - } else if (t >= 0xF0) { - const t2 = ((t & 7) << 18 | (this[i++] & 0x3F) << 12 | (this[i++] & 0x3F) << 6 | this[i++] & 0x3F) - 0x10000; - str += String.fromCharCode(0xD800 + (t2 >> 10)); - str += String.fromCharCode(0xDC00 + (t2 & 0x3FF)); - } - } - return str; + let str = ""; + for (let i = start; i < end;) { + str += String.fromCharCode(this[i++]); } + return str; + } - copy(target: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number) { - target.set(this.subarray(sourceStart, sourceEnd), targetStart); + utf8Slice(start: number, end: number) { + if (end - start < 1) { + return ""; } + let str = ""; + for (let i = start; i < end;) { + const t = this[i++]; + if (t <= 0x7F) { + str += String.fromCharCode(t); + } else if (t >= 0xC0 && t < 0xE0) { + str += String.fromCharCode((t & 0x1F) << 6 | this[i++] & 0x3F); + } else if (t >= 0xE0 && t < 0xF0) { + str += String.fromCharCode((t & 0xF) << 12 | (this[i++] & 0x3F) << 6 | this[i++] & 0x3F); + } else if (t >= 0xF0) { + const t2 = ((t & 7) << 18 | (this[i++] & 0x3F) << 12 | (this[i++] & 0x3F) << 6 | this[i++] & 0x3F) - 0x10000; + str += String.fromCharCode(0xD800 + (t2 >> 10)); + str += String.fromCharCode(0xDC00 + (t2 & 0x3FF)); + } + } + return str; + } + + copy(target: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number) { + target.set(this.subarray(sourceStart, sourceEnd), targetStart); + } - static byteLength(str: string) { - let len = 0; - let c = 0; - for (let i = 0; i < str.length; ++i) { - c = str.charCodeAt(i); - if (c < 128) - len += 1; - else if (c < 2048) - len += 2; - else if ((c & 0xFC00) === 0xD800 && (str.charCodeAt(i + 1) & 0xFC00) === 0xDC00) { - ++i; - len += 4; - } else - len += 3; - } - return len; + static byteLength(str: string) { + let len = 0; + let c = 0; + for (let i = 0; i < str.length; ++i) { + c = str.charCodeAt(i); + if (c < 128) + len += 1; + else if (c < 2048) + len += 2; + else if ((c & 0xFC00) === 0xD800 && (str.charCodeAt(i + 1) & 0xFC00) === 0xDC00) { + ++i; + len += 4; + } else + len += 3; } + return len; + } } -export const fromUint8Array = isNodeEnv ? - (ab: Buffer | Uint8Array) => { - if (!Buffer.isBuffer(ab)) { - return (Buffer.from(ab) as unknown as PlatformBuffer) - } else { - return ab as unknown as PlatformBuffer; - } - } : - (ab: Buffer | Uint8Array) => new BrowserBuffer(ab) +export const fromUint8Array = isNodeEnv + ? (ab: Buffer | Uint8Array) => { + if (!Buffer.isBuffer(ab)) { + return (Buffer.from(ab) as unknown as PlatformBuffer); + } else { + return ab as unknown as PlatformBuffer; + } + } + : (ab: Buffer | Uint8Array) => new BrowserBuffer(ab); -export const alloc = (isNodeEnv ? Buffer.allocUnsafe : BrowserBuffer.alloc) as unknown as (size: number) => PlatformBuffer +export const alloc = (isNodeEnv ? Buffer.allocUnsafe : BrowserBuffer.alloc) as unknown as (size: number) => PlatformBuffer; -export const strByteLength = isNodeEnv ? Buffer.byteLength : BrowserBuffer.byteLength +export const strByteLength = isNodeEnv ? Buffer.byteLength : BrowserBuffer.byteLength; diff --git a/javascript/packages/fury/lib/reader.ts b/javascript/packages/fury/lib/reader.ts index 0f3b141675..1483f1b8db 100644 --- a/javascript/packages/fury/lib/reader.ts +++ b/javascript/packages/fury/lib/reader.ts @@ -16,7 +16,7 @@ import { Config, LATIN1 } from "./type"; import { isNodeEnv } from "./util"; -import { PlatformBuffer, alloc, fromUint8Array } from './platformBuffer'; +import { PlatformBuffer, alloc, fromUint8Array } from "./platformBuffer"; export const BinaryReader = (config: Config) => { const sliceStringEnable = isNodeEnv && config.useSliceString; diff --git a/javascript/packages/fury/lib/referenceResolver.ts b/javascript/packages/fury/lib/referenceResolver.ts index b0fb92e62b..0be9a467f7 100644 --- a/javascript/packages/fury/lib/referenceResolver.ts +++ b/javascript/packages/fury/lib/referenceResolver.ts @@ -25,12 +25,12 @@ import { import type ClassResolver from "./classResolver"; export const makeHead = (flag: RefFlags, type: InternalSerializerType) => { - return (((type << 16) >>> 16) << 8) | ((flag << 24) >>> 24) -} + return (((type << 16) >>> 16) << 8) | ((flag << 24) >>> 24); +}; export const ReferenceResolver = ( config: { - refTracking?: boolean; + refTracking?: boolean }, binaryWriter: BinaryWriter, binaryReader: BinaryReader, @@ -68,7 +68,6 @@ export const ReferenceResolver = ( } } - function skipType() { const typeId = binaryReader.int16(); if (typeId === InternalSerializerType.FURY_TYPE_TAG) { diff --git a/javascript/packages/fury/lib/type.ts b/javascript/packages/fury/lib/type.ts index 2d23f82a93..7ba7911f3c 100644 --- a/javascript/packages/fury/lib/type.ts +++ b/javascript/packages/fury/lib/type.ts @@ -19,108 +19,101 @@ import type { BinaryReader } from "./reader"; import type FuryFunc from "./fury"; export type Fury = ReturnType; -export type BinaryWriter = ReturnType -export type BinaryReader = ReturnType - +export type BinaryWriter = ReturnType; +export type BinaryReader = ReturnType; export enum InternalSerializerType { - STRING = 13, - ARRAY = 25, - TUPLE = 25, - MAP = 30, - BOOL = 1, - UINT8 = 2, - INT8 = 3, - UINT16 = 4, - INT16 = 5, - UINT32 = 6, - INT32 = 7, - UINT64 = 8, - INT64 = 9, - FLOAT = 11, - DOUBLE = 12, - BINARY = 14, - DATE = 16, - TIMESTAMP = 18, - FURY_TYPE_TAG = 256, - FURY_SET = 257, - FURY_PRIMITIVE_BOOL_ARRAY = 258, - FURY_PRIMITIVE_SHORT_ARRAY = 259, - FURY_PRIMITIVE_INT_ARRAY = 260, - FURY_PRIMITIVE_LONG_ARRAY = 261, - FURY_PRIMITIVE_FLOAT_ARRAY = 262, - FURY_PRIMITIVE_DOUBLE_ARRAY = 263, - FURY_STRING_ARRAY = 264, - ANY = -1, + STRING = 13, + ARRAY = 25, + TUPLE = 25, + MAP = 30, + BOOL = 1, + UINT8 = 2, + INT8 = 3, + UINT16 = 4, + INT16 = 5, + UINT32 = 6, + INT32 = 7, + UINT64 = 8, + INT64 = 9, + FLOAT = 11, + DOUBLE = 12, + BINARY = 14, + DATE = 16, + TIMESTAMP = 18, + FURY_TYPE_TAG = 256, + FURY_SET = 257, + FURY_PRIMITIVE_BOOL_ARRAY = 258, + FURY_PRIMITIVE_SHORT_ARRAY = 259, + FURY_PRIMITIVE_INT_ARRAY = 260, + FURY_PRIMITIVE_LONG_ARRAY = 261, + FURY_PRIMITIVE_FLOAT_ARRAY = 262, + FURY_PRIMITIVE_DOUBLE_ARRAY = 263, + FURY_STRING_ARRAY = 264, + ANY = -1, } - export enum ConfigFlags { - isNullFlag = 1 << 0, - isLittleEndianFlag = 2, - isCrossLanguageFlag = 4, - isOutOfBandFlag = 8, + isNullFlag = 1 << 0, + isLittleEndianFlag = 2, + isCrossLanguageFlag = 4, + isOutOfBandFlag = 8, } export type SerializerRead = ( -) => T +) => T; export type SerializerWrite = ( - v: T, -) => void + v: T, +) => void; export type SerializerConfig = ( ) => { - reserve: number, -} - + reserve: number +}; // read, write export type Serializer = { - read: SerializerRead, - write: SerializerWrite, - readWithoutType?: SerializerRead, - writeWithoutType?: SerializerWrite, - config: SerializerConfig, + read: SerializerRead + write: SerializerWrite + readWithoutType?: SerializerRead + writeWithoutType?: SerializerWrite + config: SerializerConfig }; - - export enum RefFlags { - NullFlag = -3, - // RefFlag indicates that object is a not-null value. - // We don't use another byte to indicate REF, so that we can save one byte. - RefFlag = -2, - // NotNullValueFlag indicates that the object is a non-null value. - NotNullValueFlag = -1, - // RefValueFlag indicates that the object is a referencable and first read. - RefValueFlag = 0, + NullFlag = -3, + // RefFlag indicates that object is a not-null value. + // We don't use another byte to indicate REF, so that we can save one byte. + RefFlag = -2, + // NotNullValueFlag indicates that the object is a non-null value. + NotNullValueFlag = -1, + // RefValueFlag indicates that the object is a referencable and first read. + RefValueFlag = 0, } -export const MaxInt32 = 2147483647; -export const MinInt32 = -2147483648; - +export const MaxInt32 = 2147483647; +export const MinInt32 = -2147483648; export const LATIN1 = 0; export const UTF8 = 1; export interface Hps { - isLatin1: (str: string) => boolean - stringCopy: (str: string, dist: Uint8Array, offset: number) => void + isLatin1: (str: string) => boolean + stringCopy: (str: string, dist: Uint8Array, offset: number) => void } export interface Config { - hps?: Hps, - refTracking?: boolean, - useLatin1?: boolean, - useSliceString?: boolean, + hps?: Hps + refTracking?: boolean + useLatin1?: boolean + useSliceString?: boolean } - export enum Language { - XLANG = 0, - JAVA = 1, - PYTHON = 2, - CPP = 3, - GO = 4, + XLANG = 0, + JAVA = 1, + PYTHON = 2, + CPP = 3, + GO = 4, } diff --git a/javascript/packages/fury/lib/util.ts b/javascript/packages/fury/lib/util.ts index fd9e2d5eea..b355979593 100644 --- a/javascript/packages/fury/lib/util.ts +++ b/javascript/packages/fury/lib/util.ts @@ -16,36 +16,34 @@ export const utf8Encoder = new TextEncoder(); - const isReserved = (key: string) => { - return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(key); + return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(key); }; const isDotPropAccessor = (prop: string) => { - return /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(prop); -} - + return /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(prop); +}; export const replaceBackslashAndQuote = (v: string) => { - return v.replace(/\\/g, '\\\\').replace(/"/g, '\\"') -} + return v.replace(/\\/g, "\\\\").replace(/"/g, "\\\""); +}; export const safePropAccessor = (prop: string) => { - if (!isDotPropAccessor(prop) || isReserved(prop)) { - return `["${replaceBackslashAndQuote(prop)}"]` - } - return `.${prop}`; -} + if (!isDotPropAccessor(prop) || isReserved(prop)) { + return `["${replaceBackslashAndQuote(prop)}"]`; + } + return `.${prop}`; +}; export const safePropName = (prop: string) => { - if (!isDotPropAccessor(prop) || isReserved(prop)) { - return `["${replaceBackslashAndQuote(prop)}"]` - } - return prop; -} + if (!isDotPropAccessor(prop) || isReserved(prop)) { + return `["${replaceBackslashAndQuote(prop)}"]`; + } + return prop; +}; -export const isNodeEnv: boolean = - typeof process !== "undefined" && - process.versions != null && - process.env.ECMA_ONLY !== 'true' && - process.versions.node != null; +export const isNodeEnv: boolean + = typeof process !== "undefined" + && process.versions != null + && process.env.ECMA_ONLY !== "true" + && process.versions.node != null; diff --git a/javascript/packages/fury/lib/writer.ts b/javascript/packages/fury/lib/writer.ts index b4b85bd42e..940254abec 100644 --- a/javascript/packages/fury/lib/writer.ts +++ b/javascript/packages/fury/lib/writer.ts @@ -15,13 +15,10 @@ */ import { Config, LATIN1, UTF8 } from "./type"; -import { PlatformBuffer, alloc, strByteLength } from './platformBuffer'; +import { PlatformBuffer, alloc, strByteLength } from "./platformBuffer"; const MAX_POOL_SIZE = 1024 * 1024 * 3; // 3MB - - - export const BinaryWriter = (config: Config) => { let cursor = 0; let byteLength: number; @@ -40,7 +37,7 @@ export const BinaryWriter = (config: Config) => { function reserve(len: number) { reserved += len; if (byteLength - cursor <= reserved) { - const newAb = alloc(byteLength*2 + len); + const newAb = alloc(byteLength * 2 + len); arrayBuffer.copy(newAb, 0); arrayBuffer = newAb; byteLength = arrayBuffer.byteLength; @@ -104,7 +101,6 @@ export const BinaryWriter = (config: Config) => { function float(v: number) { dataView.setFloat32(cursor, v, true); cursor += 4; - } function double(v: number) { @@ -145,8 +141,8 @@ export const BinaryWriter = (config: Config) => { dataView.setUint16(offset, (u1 << 8) | u2); offset += 2; } else if ( - (c1 & 0xfc00) === 0xd800 && - ((c2 = string.charCodeAt(i + 1)) & 0xfc00) === 0xdc00 + (c1 & 0xfc00) === 0xd800 + && ((c2 = string.charCodeAt(i + 1)) & 0xfc00) === 0xdc00 ) { c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff); ++i; @@ -218,8 +214,8 @@ export const BinaryWriter = (config: Config) => { function varInt32(val: number) { val = val >>> 0; while (val > 127) { - arrayBuffer[cursor++] = val & 127 | 128; - val >>>= 7; + arrayBuffer[cursor++] = val & 127 | 128; + val >>>= 7; } arrayBuffer[cursor++] = val; } diff --git a/javascript/packages/hps/index.ts b/javascript/packages/hps/index.ts index 40982a8881..9c5997a8cc 100644 --- a/javascript/packages/hps/index.ts +++ b/javascript/packages/hps/index.ts @@ -14,16 +14,16 @@ * limitations under the License. */ -const hps: Hps = require('bindings')('hps.node'); +const hps: Hps = require("bindings")("hps.node"); interface Hps { - isLatin1: (str: string) => boolean - stringCopy: (str: string, dist: Uint8Array, offset: number) => void + isLatin1: (str: string) => boolean + stringCopy: (str: string, dist: Uint8Array, offset: number) => void } const { isLatin1, stringCopy } = hps; export { - isLatin1, - stringCopy -} \ No newline at end of file + isLatin1, + stringCopy, +};