Skip to content

Commit

Permalink
[JavaScript] Add lint tools (#1232)
Browse files Browse the repository at this point in the history
* [JavaScript] Add lint tools

* [JavaScript] Lint files

* [CI] Add CI format check

* [CI] Remove redundant ci checks
  • Loading branch information
bytemain authored Dec 20, 2023
1 parent 080e351 commit 8e1dc5c
Show file tree
Hide file tree
Showing 25 changed files with 1,342 additions and 1,366 deletions.
2 changes: 1 addition & 1 deletion ci/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 16 additions & 5 deletions javascript/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -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"]
};
ignorePatterns: ["test/*", "dist/*", "*.js", "murmurHash3.ts", "packages/**/dist/*", "packages/**/build/*"],
};
3 changes: 2 additions & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"packages/fury"
],
"devDependencies": {
"prettier": "^3.1.0"
"@stylistic/eslint-plugin": "^1.5.1",
"eslint": "^8.55.0"
}
}
77 changes: 38 additions & 39 deletions javascript/packages/fury/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends TypeDescription>(description: T) {
if (
description.type !== InternalSerializerType.FURY_TYPE_TAG ||
!Cast<ObjectTypeDescription>(description)?.options.tag
) {
throw new Error("root type should be object");
}
const serializer = genSerializer(this.fury, description);
return {
serializer,
serialize: (data: ToRecordType<T>) => {
return this.fury.serialize(data, serializer);
},
deserialize: (bytes: Uint8Array) => {
return this.fury.deserialize(bytes, serializer) as ToRecordType<T>;
},
};
registerSerializer<T extends TypeDescription>(description: T) {
if (
description.type !== InternalSerializerType.FURY_TYPE_TAG
|| !Cast<ObjectTypeDescription>(description)?.options.tag
) {
throw new Error("root type should be object");
}
const serializer = genSerializer(this.fury, description);
return {
serializer,
serialize: (data: ToRecordType<T>) => {
return this.fury.serialize(data, serializer);
},
deserialize: (bytes: Uint8Array) => {
return this.fury.deserialize(bytes, serializer) as ToRecordType<T>;
},
};
}

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);
}
}
200 changes: 100 additions & 100 deletions javascript/packages/fury/lib/classResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
}
}
}
}
Loading

0 comments on commit 8e1dc5c

Please sign in to comment.