From 4d8b70c80e66a5f2562c073d95080fab7e739ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Thu, 23 Dec 2021 23:08:06 +0000 Subject: [PATCH] fix: return null instead of throw error on unsupported version --- src/__test__/index.test.js | 16 ++++++++++++++++ src/index.js | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/__test__/index.test.js diff --git a/src/__test__/index.test.js b/src/__test__/index.test.js new file mode 100644 index 0000000..4884266 --- /dev/null +++ b/src/__test__/index.test.js @@ -0,0 +1,16 @@ +const DecryptorV4 = require("../crypto/DecryptorV4"); +const jooxFactory = require("../index"); + +describe("package entry", () => { + it("should return null on unsupported file", () => { + const input = Buffer.from("E!99........", "utf-8"); + const decryptor = jooxFactory(input, "".padStart(32, "0")); + expect(decryptor).toBeNull(); + }); + + it("should return an encryptor on v4 magic file", () => { + const input = Buffer.from("E!04........", "utf-8"); + const decryptor = jooxFactory(input, "".padStart(32, "0")); + expect(decryptor).toBeInstanceOf(DecryptorV4); + }); +}); diff --git a/src/index.js b/src/index.js index 33eeb8c..dabb0a1 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,7 @@ function jooxFactory(fileBody, seed) { return new DecryptorV4(seed); } - throw new Error("input file not supported or invalid"); + return null; } module.exports = jooxFactory;