Skip to content

Commit d6e300a

Browse files
committed
feat: esm support
1 parent 9ec9adc commit d6e300a

File tree

10 files changed

+98
-48
lines changed

10 files changed

+98
-48
lines changed

javascript/benchmark/index.js renamed to javascript/benchmark/index.mjs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717
* under the License.
1818
*/
1919

20-
const Fury = require("@furyjs/fury");
21-
const utils = require("../test/util");
22-
const hps = require('@furyjs/hps');
23-
const fury = new Fury.default({ hps, refTracking: false, useSliceString: true });
24-
const Benchmark = require("benchmark");
25-
const protobuf = require("protobufjs");
26-
const path = require('path');
27-
const Type = Fury.Type;
28-
const assert = require('assert');
29-
const { spawn } = require("child_process");
20+
import utils from '../test/util.js';
21+
import Fury from "@furyjs/fury";
22+
import Hps from '@furyjs/hps';
23+
import Benchmark from 'benchmark';
24+
import protobuf from 'protobufjs';
25+
import path, { dirname } from 'path';
26+
import assert from 'assert';
27+
import { fileURLToPath } from 'url';
28+
import { spawn } from 'child_process';
3029

30+
const fury = new Fury({ hps: Hps, refTracking: false, useSliceString: true });
31+
const currentModulePath = fileURLToPath(import.meta.url);
32+
const currentModuleDir = dirname(currentModulePath);
3133

3234
const sample = {
3335
id: 123456,
@@ -116,7 +118,7 @@ const sampleJson = JSON.stringify(sample);
116118

117119
function loadProto() {
118120
return new Promise((resolve) => {
119-
protobuf.load(path.join(__dirname, 'sample.proto'), function (err, root) {
121+
protobuf.load(path.join(currentModuleDir, 'sample.proto'), function (err, root) {
120122
if (err) throw err;
121123
const AwesomeMessage = root.lookupType("SomeMessage");
122124
resolve({
@@ -208,7 +210,7 @@ async function start() {
208210
`python3`,
209211
['draw.py', result.json.serialize, result.json.deserialize, result.protobuf.serialize, result.protobuf.deserialize, result.fury.serialize, result.fury.deserialize],
210212
{
211-
cwd: __dirname,
213+
cwd: currentModuleDir,
212214
}
213215
)
214216
}

javascript/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
"packages/fury"
1212
],
1313
"devDependencies": {
14+
"@rollup/plugin-typescript": "^11.1.6",
1415
"@stylistic/eslint-plugin": "^1.5.1",
1516
"@types/js-beautify": "^1.14.3",
1617
"eslint": "^8.55.0",
17-
"js-beautify": "^1.14.11"
18+
"js-beautify": "^1.14.11",
19+
"rollup": "^4.9.5"
1820
}
1921
}

javascript/packages/fury/lib/classResolver.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@
1717
* under the License.
1818
*/
1919

20-
import { InternalSerializerType, Serializer, BinaryReader, BinaryWriter as TBinaryWriter } from "./type";
21-
import { fromString } from "./platformBuffer";
22-
import { x64hash128 } from "./murmurHash3";
23-
import { BinaryWriter } from "./writer";
20+
import { InternalSerializerType, Serializer, BinaryReader, BinaryWriter as TBinaryWriter, USESTRINGID, USESTRINGVALUE } from "./type";
2421
import { generateSerializer } from "./gen";
2522
import { Type, TypeDescription } from "./description";
2623
import Fury from "./fury";
27-
28-
const USESTRINGVALUE = 0;
29-
const USESTRINGID = 1;
24+
import { tagBuffer } from "./meta";
3025

3126
class LazyString {
3227
private string: string | null = null;
@@ -150,27 +145,10 @@ export default class SerializerResolver {
150145
return this.customSerializer[tag];
151146
}
152147

153-
static tagBuffer(tag: string) {
154-
const tagBuffer = fromString(tag);
155-
const bufferLen = tagBuffer.byteLength;
156-
const writer = BinaryWriter({});
157-
158-
let tagHash = x64hash128(tagBuffer, 47).getBigUint64(0);
159-
if (tagHash === 0n) {
160-
tagHash = 1n;
161-
}
162-
163-
writer.uint8(USESTRINGVALUE);
164-
writer.uint64(tagHash);
165-
writer.int16(bufferLen);
166-
writer.bufferWithoutMemCheck(tagBuffer, bufferLen);
167-
return writer.dump();
168-
}
169-
170148
createTagWriter(tag: string) {
171149
this.writeStringIndex.push(-1);
172150
const idx = this.writeStringIndex.length - 1;
173-
const fullBuffer = SerializerResolver.tagBuffer(tag);
151+
const fullBuffer = tagBuffer(tag);
174152

175153
return {
176154
write: (binaryWriter: TBinaryWriter) => {

javascript/packages/fury/lib/meta.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,34 @@
2020
import Fury from "./fury";
2121
import ClassResolver from "./classResolver";
2222
import { ObjectTypeDescription, TypeDescription } from "./description";
23-
import { InternalSerializerType } from "./type";
23+
import { InternalSerializerType, USESTRINGVALUE } from "./type";
24+
import { fromString } from "./platformBuffer";
25+
import { BinaryWriter } from "./writer";
26+
import { x64hash128 } from "./murmurHash3";
2427

2528
export type Meta = {
2629
fixedSize: number
2730
needToWriteRef: boolean
2831
type: InternalSerializerType
2932
};
3033

34+
export const tagBuffer = (tag: string) => {
35+
const tagBuffer = fromString(tag);
36+
const bufferLen = tagBuffer.byteLength;
37+
const writer = BinaryWriter({});
38+
39+
let tagHash = x64hash128(tagBuffer, 47).getBigUint64(0);
40+
if (tagHash === 0n) {
41+
tagHash = 1n;
42+
}
43+
44+
writer.uint8(USESTRINGVALUE);
45+
writer.uint64(tagHash);
46+
writer.int16(bufferLen);
47+
writer.bufferWithoutMemCheck(tagBuffer, bufferLen);
48+
return writer.dump();
49+
};
50+
3151
export const getMeta = (description: TypeDescription, fury: Fury): Meta => {
3252
const type = description.type;
3353
switch (type) {
@@ -107,7 +127,7 @@ export const getMeta = (description: TypeDescription, fury: Fury): Meta => {
107127
case InternalSerializerType.FURY_TYPE_TAG:
108128
{
109129
const options = (<ObjectTypeDescription>description).options;
110-
let fixedSize = ClassResolver.tagBuffer(options.tag).byteLength + 8;
130+
let fixedSize = tagBuffer(options.tag).byteLength + 8;
111131
if (options.props) {
112132
Object.values(options.props).forEach(x => fixedSize += getMeta(x, fury).fixedSize);
113133
} else {

javascript/packages/fury/lib/type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,6 @@ export enum Language {
114114
CPP = 3,
115115
GO = 4,
116116
}
117+
118+
export const USESTRINGVALUE = 0;
119+
export const USESTRINGID = 1;

javascript/packages/fury/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
{
22
"name": "@furyjs/fury",
3-
"version": "0.5.6-beta",
3+
"version": "0.5.7-beta",
44
"description": "A blazing fast multi-language serialization framework powered by jit and zero-copy",
55
"main": "dist/index.js",
6+
"types": "types/index.d.ts",
7+
"module": "dist/esm/index.mjs",
8+
"exports": {
9+
"import": "./dist/esm/index.mjs",
10+
"require": "./dist/index.js"
11+
},
612
"scripts": {
7-
"build": "tsc",
13+
"build": "rollup -c ../../rollup.config.mjs",
814
"prepublishOnly": "npm run build"
915
},
1016
"files": [

javascript/packages/fury/tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
1515

1616
/* Modules */
17-
"module": "CommonJS", /* Specify what module code is generated. */
17+
"module": "ES6", /* Specify what module code is generated. */
1818
"rootDir": "./", /* Specify the root folder within your source files. */
1919
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
2020
"baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
@@ -24,7 +24,7 @@
2424
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
2525

2626
/* Emit */
27-
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
27+
"declaration": false, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
2828
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
2929
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
3030
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
@@ -45,7 +45,7 @@
4545
"noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
4646
"noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
4747
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
48-
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
48+
// "declarationDir": "", /* Specify the output directory for generated declaration files. */
4949
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
5050

5151
/* Interop Constraints */

javascript/packages/hps/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"name": "@furyjs/hps",
3-
"version": "0.5.0.dev",
3+
"version": "0.5.1-beta",
44
"description": "fury nodejs high-performance suite",
55
"main": "dist/index.js",
6+
"types": "types/index.d.ts",
67
"files": [
78
"dist",
89
"src",
910
"binding.gyp"
1011
],
1112
"scripts": {
1213
"postinstall": "npx node-gyp rebuild",
13-
"build": "npx node-gyp rebuild && tsc",
14+
"build": "npx node-gyp rebuild && tsc -p tsconfig.json",
1415
"prepublishOnly": "npm run build"
1516
},
1617
"license": "Apache",

javascript/packages/hps/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
4646
"noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
4747
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
48-
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
48+
"declarationDir": "./dist/types", /* Specify the output directory for generated declaration files. */
4949
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
5050

5151
/* Interop Constraints */

javascript/rollup.config.mjs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import typescript from '@rollup/plugin-typescript';
2+
3+
export default [
4+
{
5+
input: './index.ts',
6+
output: {
7+
preserveModules: true,
8+
dir: './dist/',
9+
format: 'cjs',
10+
},
11+
external: [/(.)*.node$/],
12+
plugins: [
13+
typescript({
14+
compilerOptions: {
15+
declaration: true,
16+
declarationDir: "./dist/types"
17+
}
18+
}),
19+
]
20+
},
21+
{
22+
input: './index.ts',
23+
output: {
24+
preserveModules: true,
25+
dir: './dist/esm/',
26+
entryFileNames: '[name].mjs',
27+
format: 'es',
28+
},
29+
external: [/(.)*.node$/],
30+
plugins: [
31+
typescript({
32+
compilerOptions: {
33+
outDir: "./dist/esm"
34+
}
35+
}),
36+
]
37+
}
38+
];

0 commit comments

Comments
 (0)