diff --git a/javascript/benchmark/index.js b/javascript/benchmark/index.mjs similarity index 91% rename from javascript/benchmark/index.js rename to javascript/benchmark/index.mjs index 016b7710ce..5cb89fb1ef 100644 --- a/javascript/benchmark/index.js +++ b/javascript/benchmark/index.mjs @@ -17,17 +17,19 @@ * under the License. */ -const Fury = require("@furyjs/fury"); -const utils = require("../test/util"); -const hps = require('@furyjs/hps'); -const fury = new Fury.default({ hps, refTracking: false, useSliceString: true }); -const Benchmark = require("benchmark"); -const protobuf = require("protobufjs"); -const path = require('path'); -const Type = Fury.Type; -const assert = require('assert'); -const { spawn } = require("child_process"); +import utils from '../test/util.js'; +import Fury from "@furyjs/fury"; +import Hps from '@furyjs/hps'; +import Benchmark from 'benchmark'; +import protobuf from 'protobufjs'; +import path, { dirname } from 'path'; +import assert from 'assert'; +import { fileURLToPath } from 'url'; +import { spawn } from 'child_process'; +const fury = new Fury({ hps: Hps, refTracking: false, useSliceString: true }); +const currentModulePath = fileURLToPath(import.meta.url); +const currentModuleDir = dirname(currentModulePath); const sample = { id: 123456, @@ -116,7 +118,7 @@ const sampleJson = JSON.stringify(sample); function loadProto() { return new Promise((resolve) => { - protobuf.load(path.join(__dirname, 'sample.proto'), function (err, root) { + protobuf.load(path.join(currentModuleDir, 'sample.proto'), function (err, root) { if (err) throw err; const AwesomeMessage = root.lookupType("SomeMessage"); resolve({ @@ -208,7 +210,7 @@ async function start() { `python3`, ['draw.py', result.json.serialize, result.json.deserialize, result.protobuf.serialize, result.protobuf.deserialize, result.fury.serialize, result.fury.deserialize], { - cwd: __dirname, + cwd: currentModuleDir, } ) } diff --git a/javascript/package.json b/javascript/package.json index e8c6b3df74..b63f7e1e5d 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -11,9 +11,11 @@ "packages/fury" ], "devDependencies": { + "@rollup/plugin-typescript": "^11.1.6", "@stylistic/eslint-plugin": "^1.5.1", "@types/js-beautify": "^1.14.3", "eslint": "^8.55.0", - "js-beautify": "^1.14.11" + "js-beautify": "^1.14.11", + "rollup": "^4.9.5" } } diff --git a/javascript/packages/fury/lib/classResolver.ts b/javascript/packages/fury/lib/classResolver.ts index 62cc1b0bfd..4377f07632 100644 --- a/javascript/packages/fury/lib/classResolver.ts +++ b/javascript/packages/fury/lib/classResolver.ts @@ -17,16 +17,11 @@ * under the License. */ -import { InternalSerializerType, Serializer, BinaryReader, BinaryWriter as TBinaryWriter } from "./type"; -import { fromString } from "./platformBuffer"; -import { x64hash128 } from "./murmurHash3"; -import { BinaryWriter } from "./writer"; +import { InternalSerializerType, Serializer, BinaryReader, BinaryWriter as TBinaryWriter, USESTRINGID, USESTRINGVALUE } from "./type"; import { generateSerializer } from "./gen"; import { Type, TypeDescription } from "./description"; import Fury from "./fury"; - -const USESTRINGVALUE = 0; -const USESTRINGID = 1; +import { tagBuffer } from "./meta"; class LazyString { private string: string | null = null; @@ -150,27 +145,10 @@ export default class SerializerResolver { return this.customSerializer[tag]; } - static tagBuffer(tag: string) { - const tagBuffer = fromString(tag); - const bufferLen = tagBuffer.byteLength; - const writer = BinaryWriter({}); - - let tagHash = x64hash128(tagBuffer, 47).getBigUint64(0); - if (tagHash === 0n) { - tagHash = 1n; - } - - writer.uint8(USESTRINGVALUE); - writer.uint64(tagHash); - writer.int16(bufferLen); - writer.bufferWithoutMemCheck(tagBuffer, bufferLen); - return writer.dump(); - } - createTagWriter(tag: string) { this.writeStringIndex.push(-1); const idx = this.writeStringIndex.length - 1; - const fullBuffer = SerializerResolver.tagBuffer(tag); + const fullBuffer = tagBuffer(tag); return { write: (binaryWriter: TBinaryWriter) => { diff --git a/javascript/packages/fury/lib/meta.ts b/javascript/packages/fury/lib/meta.ts index d46dafb65a..fcbb12aac3 100644 --- a/javascript/packages/fury/lib/meta.ts +++ b/javascript/packages/fury/lib/meta.ts @@ -20,7 +20,10 @@ import Fury from "./fury"; import ClassResolver from "./classResolver"; import { ObjectTypeDescription, TypeDescription } from "./description"; -import { InternalSerializerType } from "./type"; +import { InternalSerializerType, USESTRINGVALUE } from "./type"; +import { fromString } from "./platformBuffer"; +import { BinaryWriter } from "./writer"; +import { x64hash128 } from "./murmurHash3"; export type Meta = { fixedSize: number @@ -28,6 +31,23 @@ export type Meta = { type: InternalSerializerType }; +export const tagBuffer = (tag: string) => { + const tagBuffer = fromString(tag); + const bufferLen = tagBuffer.byteLength; + const writer = BinaryWriter({}); + + let tagHash = x64hash128(tagBuffer, 47).getBigUint64(0); + if (tagHash === 0n) { + tagHash = 1n; + } + + writer.uint8(USESTRINGVALUE); + writer.uint64(tagHash); + writer.int16(bufferLen); + writer.bufferWithoutMemCheck(tagBuffer, bufferLen); + return writer.dump(); +}; + export const getMeta = (description: TypeDescription, fury: Fury): Meta => { const type = description.type; switch (type) { @@ -107,7 +127,7 @@ export const getMeta = (description: TypeDescription, fury: Fury): Meta => { case InternalSerializerType.FURY_TYPE_TAG: { const options = (description).options; - let fixedSize = ClassResolver.tagBuffer(options.tag).byteLength + 8; + let fixedSize = tagBuffer(options.tag).byteLength + 8; if (options.props) { Object.values(options.props).forEach(x => fixedSize += getMeta(x, fury).fixedSize); } else { diff --git a/javascript/packages/fury/lib/type.ts b/javascript/packages/fury/lib/type.ts index fe5036329b..2ecb2e9cf0 100644 --- a/javascript/packages/fury/lib/type.ts +++ b/javascript/packages/fury/lib/type.ts @@ -114,3 +114,6 @@ export enum Language { CPP = 3, GO = 4, } + +export const USESTRINGVALUE = 0; +export const USESTRINGID = 1; diff --git a/javascript/packages/fury/package.json b/javascript/packages/fury/package.json index 9875851818..091878bc8f 100644 --- a/javascript/packages/fury/package.json +++ b/javascript/packages/fury/package.json @@ -1,10 +1,16 @@ { "name": "@furyjs/fury", - "version": "0.5.6-beta", + "version": "0.5.7-beta", "description": "A blazing fast multi-language serialization framework powered by jit and zero-copy", "main": "dist/index.js", + "types": "types/index.d.ts", + "module": "dist/esm/index.mjs", + "exports": { + "import": "./dist/esm/index.mjs", + "require": "./dist/index.js" + }, "scripts": { - "build": "tsc", + "build": "rollup -c ../../rollup.config.mjs", "prepublishOnly": "npm run build" }, "files": [ diff --git a/javascript/packages/fury/tsconfig.json b/javascript/packages/fury/tsconfig.json index 176ef55b31..debfecf36a 100644 --- a/javascript/packages/fury/tsconfig.json +++ b/javascript/packages/fury/tsconfig.json @@ -14,7 +14,7 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "CommonJS", /* Specify what module code is generated. */ + "module": "ES6", /* Specify what module code is generated. */ "rootDir": "./", /* Specify the root folder within your source files. */ "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ @@ -24,7 +24,7 @@ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ /* Emit */ - "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + "declaration": false, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ // "declarationMap": true, /* Create sourcemaps for d.ts files. */ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ @@ -45,7 +45,7 @@ "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ - // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "declarationDir": "", /* Specify the output directory for generated declaration files. */ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ diff --git a/javascript/packages/hps/package.json b/javascript/packages/hps/package.json index 4febf187f9..ed65b1d589 100644 --- a/javascript/packages/hps/package.json +++ b/javascript/packages/hps/package.json @@ -1,8 +1,9 @@ { "name": "@furyjs/hps", - "version": "0.5.0.dev", + "version": "0.5.1-beta", "description": "fury nodejs high-performance suite", "main": "dist/index.js", + "types": "types/index.d.ts", "files": [ "dist", "src", @@ -10,7 +11,7 @@ ], "scripts": { "postinstall": "npx node-gyp rebuild", - "build": "npx node-gyp rebuild && tsc", + "build": "npx node-gyp rebuild && tsc -p tsconfig.json", "prepublishOnly": "npm run build" }, "license": "Apache", diff --git a/javascript/packages/hps/tsconfig.json b/javascript/packages/hps/tsconfig.json index 176ef55b31..944341c182 100644 --- a/javascript/packages/hps/tsconfig.json +++ b/javascript/packages/hps/tsconfig.json @@ -45,7 +45,7 @@ "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ - // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + "declarationDir": "./dist/types", /* Specify the output directory for generated declaration files. */ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ diff --git a/javascript/rollup.config.mjs b/javascript/rollup.config.mjs new file mode 100644 index 0000000000..e340a3ea8e --- /dev/null +++ b/javascript/rollup.config.mjs @@ -0,0 +1,38 @@ +import typescript from '@rollup/plugin-typescript'; + +export default [ + { + input: './index.ts', + output: { + preserveModules: true, + dir: './dist/', + format: 'cjs', + }, + external: [/(.)*.node$/], + plugins: [ + typescript({ + compilerOptions: { + declaration: true, + declarationDir: "./dist/types" + } + }), + ] + }, + { + input: './index.ts', + output: { + preserveModules: true, + dir: './dist/esm/', + entryFileNames: '[name].mjs', + format: 'es', + }, + external: [/(.)*.node$/], + plugins: [ + typescript({ + compilerOptions: { + outDir: "./dist/esm" + } + }), + ] + } +]; \ No newline at end of file