This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git


The following commit(s) were added to refs/heads/main by this push:
     new 9fde3975 feat(javaScript): Implement magic number feature (#1550)
9fde3975 is described below

commit 9fde3975d8075e94602b6430a204e8f128048e56
Author: weipeng <[email protected]>
AuthorDate: Sat Apr 20 01:42:39 2024 +0800

    feat(javaScript): Implement magic number feature (#1550)
    
    ## What does this PR do?
    1. Add magic number described in protocol
    
    ## Does this PR introduce any user-facing change?
    The magic number introduces a binary protocol compatibility change. But
    Xlang protocol has not been released yet.
    
    - [ ] Does this PR introduce any public API change?
    - [x] Does this PR introduce any binary protocol compatibility change?
---
 javascript/packages/fury/lib/fury.ts |  6 +++++-
 javascript/packages/fury/lib/type.ts |  2 ++
 javascript/test/fury.test.ts         | 13 +++++++++----
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/javascript/packages/fury/lib/fury.ts 
b/javascript/packages/fury/lib/fury.ts
index 2889ecc3..a7055aaf 100644
--- a/javascript/packages/fury/lib/fury.ts
+++ b/javascript/packages/fury/lib/fury.ts
@@ -21,7 +21,7 @@ import ClassResolver from "./classResolver";
 import { BinaryWriter } from "./writer";
 import { BinaryReader } from "./reader";
 import { ReferenceResolver } from "./referenceResolver";
-import { ConfigFlags, Serializer, Config, Language } from "./type";
+import { ConfigFlags, Serializer, Config, Language, MAGIC_NUMBER } from 
"./type";
 import { OwnershipError } from "./error";
 import { InputType, ResultType, TypeDescription } from "./description";
 import { generateSerializer, AnySerializer } from "./gen";
@@ -66,6 +66,9 @@ export default class {
     this.referenceResolver.reset();
     this.classResolver.reset();
     this.binaryReader.reset(bytes);
+    if (this.binaryReader.int16() !== MAGIC_NUMBER) {
+      throw new Error("the fury xlang serialization must start with magic 
number 0x%x. Please check whether the serialization is based on the xlang 
protocol and the data didn't corrupt");
+    }
     const bitmap = this.binaryReader.uint8();
     if ((bitmap & ConfigFlags.isNullFlag) === ConfigFlags.isNullFlag) {
       return null;
@@ -106,6 +109,7 @@ export default class {
     }
     bitmap |= ConfigFlags.isLittleEndianFlag;
     bitmap |= ConfigFlags.isCrossLanguageFlag;
+    this.binaryWriter.int16(MAGIC_NUMBER);
     this.binaryWriter.uint8(bitmap);
     this.binaryWriter.uint8(Language.XLANG);
     const cursor = this.binaryWriter.getCursor();
diff --git a/javascript/packages/fury/lib/type.ts 
b/javascript/packages/fury/lib/type.ts
index 9d044de7..d7746d55 100644
--- a/javascript/packages/fury/lib/type.ts
+++ b/javascript/packages/fury/lib/type.ts
@@ -116,3 +116,5 @@ export enum Language {
   CPP = 3,
   GO = 4,
 }
+
+export const MAGIC_NUMBER = 0x62D4;
diff --git a/javascript/test/fury.test.ts b/javascript/test/fury.test.ts
index cb4a265c..125e4388 100644
--- a/javascript/test/fury.test.ts
+++ b/javascript/test/fury.test.ts
@@ -20,17 +20,22 @@
 import Fury, { TypeDescription, Type } from '../packages/fury/index';
 import { describe, expect, test } from '@jest/globals';
 import { fromUint8Array } from '../packages/fury/lib/platformBuffer';
+import { MAGIC_NUMBER } from '../packages/fury/lib/type';
+
+const hight = MAGIC_NUMBER >> 8;
+const low = MAGIC_NUMBER & 0xff;
 
 describe('fury', () => {
     test('should deserialize null work', () => {
         const fury = new Fury();
-        expect(fury.deserialize(new Uint8Array([1]))).toBe(null)
+
+        expect(fury.deserialize(new Uint8Array([low, hight, 1]))).toBe(null)
     });
 
     test('should deserialize big endian work', () => {
         const fury = new Fury();
         try {
-            fury.deserialize(new Uint8Array([0]))
+            fury.deserialize(new Uint8Array([low, hight, 0]))
             throw new Error('unreachable code')
         } catch (error) {
             expect(error.message).toBe('big endian is not supported now');
@@ -40,7 +45,7 @@ describe('fury', () => {
     test('should deserialize xlang disable work', () => {
         const fury = new Fury();
         try {
-            fury.deserialize(new Uint8Array([2]))
+            fury.deserialize(new Uint8Array([low, hight, 2]))
             throw new Error('unreachable code')
         } catch (error) {
             expect(error.message).toBe('support crosslanguage mode only');
@@ -50,7 +55,7 @@ describe('fury', () => {
     test('should deserialize xlang disable work', () => {
         const fury = new Fury();
         try {
-            fury.deserialize(new Uint8Array([14]))
+            fury.deserialize(new Uint8Array([low, hight, 14]))
             throw new Error('unreachable code')
         } catch (error) {
             expect(error.message).toBe('outofband mode is not supported now');


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to