Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(JavaScript): Support ESM #1347

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions javascript/benchmark/index.js → javascript/benchmark/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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,
}
)
}
Expand Down
4 changes: 3 additions & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
28 changes: 3 additions & 25 deletions javascript/packages/fury/lib/classResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down
24 changes: 22 additions & 2 deletions javascript/packages/fury/lib/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,34 @@
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
needToWriteRef: boolean
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) {
Expand Down Expand Up @@ -107,7 +127,7 @@ export const getMeta = (description: TypeDescription, fury: Fury): Meta => {
case InternalSerializerType.FURY_TYPE_TAG:
{
const options = (<ObjectTypeDescription>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 {
Expand Down
3 changes: 3 additions & 0 deletions javascript/packages/fury/lib/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@ export enum Language {
CPP = 3,
GO = 4,
}

export const USESTRINGVALUE = 0;
export const USESTRINGID = 1;
10 changes: 8 additions & 2 deletions javascript/packages/fury/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
6 changes: 3 additions & 3 deletions javascript/packages/fury/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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. */
Expand All @@ -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 */
Expand Down
5 changes: 3 additions & 2 deletions javascript/packages/hps/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"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",
"binding.gyp"
],
"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",
Expand Down
2 changes: 1 addition & 1 deletion javascript/packages/hps/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
38 changes: 38 additions & 0 deletions javascript/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -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"
}
}),
]
}
];
Loading