diff --git a/lib/decodeOpaqueId.js b/lib/decodeOpaqueId.js index abf465a..bbe0a71 100644 --- a/lib/decodeOpaqueId.js +++ b/lib/decodeOpaqueId.js @@ -1,16 +1,23 @@ +import config from "./config.js"; + /** * @name decodeOpaqueId * @method * @memberof GraphQL/Transforms - * @summary Transforms an opaque ID to an internal ID + * @summary Transforms an opaque ID to an internal ID. Returns the `id` + * unchanged and the `namespace` as null if the `REACTION_SHOULD_ENCODE_IDS` + * environment variable is `false` * @param {String} opaqueId The ID to transform * @returns {String} An internal ID */ export default function decodeOpaqueId(opaqueId) { if (opaqueId === undefined || opaqueId === null) return null; - const [namespace, id] = Buffer - .from(opaqueId, "base64") + if (config.REACTION_SHOULD_ENCODE_IDS === false) { + return { namespace: null, id: opaqueId }; + } + + const [namespace, id] = Buffer.from(opaqueId, "base64") .toString("utf8") .split(":", 2); diff --git a/lib/decodeOpaqueId.test.js b/lib/decodeOpaqueId.test.js index 6112ca5..563dc13 100644 --- a/lib/decodeOpaqueId.test.js +++ b/lib/decodeOpaqueId.test.js @@ -1,5 +1,14 @@ +import { jest } from "@jest/globals"; +import config from "./config.js"; import decodeOpaqueId from "./decodeOpaqueId.js"; +jest.mock("./config.js", () => ({ + __esModule: true, // this property makes it work + default: { + REACTION_SHOULD_ENCODE_IDS: true + } +})); + test("decodes base64", () => { const encodedId = "cmVhY3Rpb24vc2hvcDpieTV3cGRnM25NcThnWDU0Yw=="; expect(decodeOpaqueId(encodedId)).toEqual({ @@ -15,3 +24,13 @@ test("passes through non-base64", () => { namespace: null }); }); + +test("skips decoding if REACTION_SHOULD_ENCODE_IDS env is false", async () => { + const id = "by5wpdg3nMq8gX54c"; + config.REACTION_SHOULD_ENCODE_IDS = false; + expect(decodeOpaqueId(id)).toEqual({ + id, + namespace: null + }); + config.REACTION_SHOULD_ENCODE_IDS = true; +});