Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: global.Set is not a constructor #1

Open
chauhanvats3 opened this issue Oct 2, 2023 · 0 comments
Open

TypeError: global.Set is not a constructor #1

chauhanvats3 opened this issue Oct 2, 2023 · 0 comments

Comments

@chauhanvats3
Copy link

While trying to implement a simple PGP encryption solution using your PGP library in lib folder , i run into this error :

TypeError: global.Set is not a constructor [at Object.<anonymous> (/SuiteScripts/Third Party PGP Solution/lib/openpgp.js:31665:39)]

I had been trying to use OpenPGP.js for a long time when I stumbled upon your solution. In my expedition, I always ran into the issue of not converting OpenPGP to AMD fully because of so many dependencies on Node or Browser. Your solution seems to be working because you also included the TextEncoder that I was not able to implement. But now I have ran into this issue.

Would you please help me out?

P.S. this is my Map/Reduce file :

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(["N/file", "N/log", "./lib/openpgp.js"], function (file, log, openpgp) {
  /* ================== ENCRYPT FILE ======================== */

  async function encryptPGP(params) {
    const publicKeyArmored = params.publicKeyArmored;
    const inputFileContents = params.inputFileContents;
    const isText = params.isText;

    const encryptMessage = isText
      ? openpgp.message.fromText(inputFileContents)
      : openpgp.message.fromBinary(inputFileContents);

    const publicKeys = (await openpgp.key.readArmored(publicKeyArmored)).keys;

    const encryptionResult = await openpgp.encrypt({
      message: encryptMessage,
      publicKeys: publicKeys,
    });

    const encrypted = encryptionResult.data;

    return encrypted;
  }

  /* ======================== MAIN FUNCTION ======================= */
  async function mainAsyncExecution(processingObj) {
    log.debug("mainAsyncExecution - start -", processingObj);

    const inputFile = file.load({
      id: processingObj.inputFileId,
    }); //10 units

    const publicKeyFile = file.load({
      id: processingObj.publicKeyFileId,
    }); //10 units

    await Promise.all([inputFile, publicKeyFile]); //wait for the two inputFileContents and cryptoType loads to complete

    const inputFileContents = inputFile.isText
      ? inputFile.getContents()
      : Base64Binary.decode(inputFile.getContents());

    const publicKeyFileContents = publicKeyFile.isText
      ? publicKeyFile.getContents()
      : Base64Binary.decode(publicKeyFile.getContents());

    log.debug("inputFileContents", inputFileContents);
    log.debug("publicKeyFileContents", publicKeyFileContents);

    const outputFolderId = processingObj.outputFolderId;

    let newFileId;

    let encryptedContents = await encryptPGP({
      publicKeyArmored: publicKeyFileContents,
      inputFileContents: inputFileContents,
      isText: inputFile.isText,
    });

    const newFile = file.create({
      name: inputFile.name + ".pgp",
      fileType: file.Type.PLAINTEXT,
      contents: encryptedContents,
      folder: outputFolderId,
    });

    newFileId = newFile.save();

    log.debug("mainAsyncExecution", "new encrypted fileId:" + newFileId);

    return newFileId;
  }

  function getInputData() {
    return { myKey: "1" };
  }

  function map(mapContext) {
    const key = JSON.parse(mapContext.key);
    const value = JSON.parse(mapContext.value);
    mapContext.write({ key: key, value: value });
  }

  function reduce(reduceContext) {
    const executionOptions = {
      inputFileId: "856",
      outputFolderId: "44",
      publicKeyFileId: "808",
    };

    mainAsyncExecution(executionOptions);
  }

  return { getInputData: getInputData, map: map, reduce: reduce };
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant