From f9b2f816cc5804f57ca2389fc341cc5f9d1a58bb Mon Sep 17 00:00:00 2001
From: RahulGautamSingh <rahultesnik@gmail.com>
Date: Fri, 19 Aug 2022 19:28:11 +0530
Subject: [PATCH] feat: replace `fast-safe-stringify` with
 `safe-stable-stringify` (#17251)

---
 lib/util/clone.spec.ts | 20 ++++++++++++++++++++
 lib/util/clone.ts      |  8 ++++++--
 package.json           |  2 +-
 yarn.lock              | 10 +++++-----
 4 files changed, 32 insertions(+), 8 deletions(-)
 create mode 100644 lib/util/clone.spec.ts

diff --git a/lib/util/clone.spec.ts b/lib/util/clone.spec.ts
new file mode 100644
index 0000000000..f0ba197d2d
--- /dev/null
+++ b/lib/util/clone.spec.ts
@@ -0,0 +1,20 @@
+import { clone } from './clone';
+
+describe('util/clone', () => {
+  const obj: any = {
+    name: 'object',
+    type: 'object',
+    isObject: true,
+  };
+
+  it('maintains same order', () => {
+    const res = clone(obj);
+    expect(Object.keys(res)).toEqual(Object.keys(obj));
+  });
+
+  it('assigns "[Circular]" to circular references', () => {
+    obj.circular = obj;
+    const res = clone(obj);
+    expect(res.circular).toBe('[Circular]');
+  });
+});
diff --git a/lib/util/clone.ts b/lib/util/clone.ts
index cb0b5ba7be..13605780ef 100644
--- a/lib/util/clone.ts
+++ b/lib/util/clone.ts
@@ -1,5 +1,9 @@
-import safeStringify from 'fast-safe-stringify';
+import { configure } from 'safe-stable-stringify';
+
+const stringify = configure({
+  deterministic: false,
+});
 
 export function clone<T>(input: T | null = null): T {
-  return JSON.parse(safeStringify(input));
+  return JSON.parse(stringify(input));
 }
diff --git a/package.json b/package.json
index 205edfcda7..9a646b4cbe 100644
--- a/package.json
+++ b/package.json
@@ -169,7 +169,6 @@
     "emojibase": "6.1.0",
     "emojibase-regex": "6.0.1",
     "extract-zip": "2.0.1",
-    "fast-safe-stringify": "2.1.1",
     "find-packages": "9.0.9",
     "find-up": "5.0.0",
     "fs-extra": "10.1.0",
@@ -204,6 +203,7 @@
     "redis": "4.2.0",
     "remark": "13.0.0",
     "remark-github": "10.1.0",
+    "safe-stable-stringify": "2.3.1",
     "semver": "7.3.7",
     "semver-stable": "3.0.0",
     "semver-utils": "1.1.4",
diff --git a/yarn.lock b/yarn.lock
index 9114e83fb4..9a00670e89 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4706,11 +4706,6 @@ fast-levenshtein@^2.0.6:
   resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
   integrity sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==
 
-fast-safe-stringify@2.1.1:
-  version "2.1.1"
-  resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz#c406a83b6e70d9e35ce3b30a81141df30aeba884"
-  integrity sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==
-
 fast-xml-parser@3.19.0:
   version "3.19.0"
   resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-3.19.0.tgz#cb637ec3f3999f51406dd8ff0e6fc4d83e520d01"
@@ -8461,6 +8456,11 @@ safe-json-stringify@~1:
   resolved "https://registry.yarnpkg.com/safe-json-stringify/-/safe-json-stringify-1.2.0.tgz#356e44bc98f1f93ce45df14bcd7c01cda86e0afd"
   integrity sha512-gH8eh2nZudPQO6TytOvbxnuhYBOvDBBLW52tz5q6X58lJcd/tkmqFR+5Z9adS8aJtURSXWThWy/xJtJwixErvg==
 
+safe-stable-stringify@2.3.1:
+  version "2.3.1"
+  resolved "https://registry.yarnpkg.com/safe-stable-stringify/-/safe-stable-stringify-2.3.1.tgz#ab67cbe1fe7d40603ca641c5e765cb942d04fc73"
+  integrity sha512-kYBSfT+troD9cDA85VDnHZ1rpHC50O0g1e6WlGHVCz/g+JS+9WKLj+XwFYyR8UbrZN8ll9HUpDAAddY58MGisg==
+
 "safer-buffer@>= 2.1.2 < 3.0.0", safer-buffer@^2.0.2, safer-buffer@^2.1.0:
   version "2.1.2"
   resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
-- 
GitLab