Hello,
I would like to encrypt data from a Netlify Function before saving it.
When I import openpgpjs, Netlify throws an error about a missing “stream” module. I thought stream was included by default in Node and accessible from a Function.
The error appearing in a browser when GETting the page is as follow :
{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'stream'","trace":["Runtime.ImportModuleError: Error: Cannot find module 'stream'"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:45:30)"," at Module._compile (internal/modules/cjs/loader.js:778:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)"," at Module.load (internal/modules/cjs/loader.js:653:32)"," at tryModuleLoad (internal/modules/cjs/loader.js:593:12)"," at Function.Module._load (internal/modules/cjs/loader.js:585:3)"," at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)"," at startup (internal/bootstrap/node.js:283:19)"]}
Here is the full code for a function encrypting and then decrypting data for test.
const openpgp = require('openpgp');
exports.handler = async function() {
const { privateKeyArmored, publicKeyArmored } = await openpgp.generateKey({
userIds: [{ fileName: "" }],
passphrase: '',
numBits: 2048,
curve: 'p256'
})
const message = "Lorem ipsum."
const ciphertext = await openpgp.encrypt({
message: await openpgp.message.fromText(message),
publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys
}).catch(e => console.error(e.message)).then(m => m && m.data)
if (!ciphertext)
throw new Error("Internal error.")
const privateKeyObj = (await openpgp.key.readArmored(privateKeyArmored)).keys[0]
if (!privateKeyObj.isDecrypted())
await privateKeyObj.decrypt("")
const cleartext = await openpgp.decrypt({
message: await openpgp.message.readArmored(ciphertext),
privateKeys: [privateKeyObj],
}).catch(e => console.error(e.message)).then(m => m.data)
if (!cleartext)
throw new Error("Internal error.")
return {
statusCode: 200,
body: cleartext
}
}
Am I missing something ?
Thank you for your help.