From 2381025306d6cf0b48e49fe0be8212e709a68257 Mon Sep 17 00:00:00 2001
From: yisding <yi.s.ding@gmail.com>
Date: Wed, 3 Jan 2024 10:26:19 -0800
Subject: [PATCH] move clone outside of toJSON

I like structuredClone although we have an issue with AWS only
supporting Node 16
---
 packages/core/src/Node.ts                      | 12 +++++-------
 packages/core/src/storage/vectorStore/utils.ts |  3 ++-
 2 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts
index 2e275805e..f967a188f 100644
--- a/packages/core/src/Node.ts
+++ b/packages/core/src/Node.ts
@@ -1,4 +1,4 @@
-import { createHash } from "node:crypto";
+import CryptoJS from "crypto-js";
 import path from "path";
 import { v4 as uuidv4 } from "uuid";
 
@@ -141,13 +141,11 @@ export abstract class BaseNode<T extends Metadata = Metadata> {
   }
 
   /**
-   * Creates a deep-clone of the node as JSON
+   * Used with built in JSON.stringify
    * @returns
    */
   toJSON(): Record<string, any> {
-    const json: Record<string, any> = structuredClone(this);
-    json.type = this.getType();
-    return json;
+    return { ...this, type: this.getType() };
   }
 }
 
@@ -179,13 +177,13 @@ export class TextNode<T extends Metadata = Metadata> extends BaseNode<T> {
    * @returns
    */
   generateHash() {
-    const hashFunction = createHash("sha256");
+    const hashFunction = CryptoJS.algo.SHA256.create();
     hashFunction.update(`type=${this.getType()}`);
     hashFunction.update(
       `startCharIdx=${this.startCharIdx} endCharIdx=${this.endCharIdx}`,
     );
     hashFunction.update(this.getContent(MetadataMode.ALL));
-    return hashFunction.digest("base64");
+    return hashFunction.finalize().toString(CryptoJS.enc.Base64);
   }
 
   getType(): ObjectType {
diff --git a/packages/core/src/storage/vectorStore/utils.ts b/packages/core/src/storage/vectorStore/utils.ts
index 1c69ae57e..cf9e5728e 100644
--- a/packages/core/src/storage/vectorStore/utils.ts
+++ b/packages/core/src/storage/vectorStore/utils.ts
@@ -1,3 +1,4 @@
+import _ from "lodash";
 import { BaseNode, jsonToNode, Metadata, ObjectType } from "../../Node";
 
 const DEFAULT_TEXT_KEY = "text";
@@ -16,7 +17,7 @@ export function nodeToMetadata(
   textField: string = DEFAULT_TEXT_KEY,
   flatMetadata: boolean = false,
 ): Metadata {
-  const { metadata, embedding, ...rest } = node.toJSON();
+  const { metadata, embedding, ...rest } = _.cloneDeep(node.toJSON());
 
   if (flatMetadata) {
     validateIsFlat(metadata);
-- 
GitLab