1

I was deploying something onto an akamai edgeworker and hence was using the node package rollup. One of the packages i have uses require('crypto') which causes issues for me as i cant seem to get the rollup to include the code. I was able to work around and get it so i dont have the import at the top but now getting TypeError: crypto.randomFillSync is not a function. just wondering how i can deploy something that requires crypto onto edgeworker. my current rollup.config file is:

banner.js

var window = {};
var TextDecoder = function() {};
var setTimeout = function(callback) { callback(); };

rollup.config.js

import * as fs from "fs";
import resolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import commonjs from "@rollup/plugin-commonjs";
import nodePolyfills from 'rollup-plugin-node-polyfills';
import { babel } from '@rollup/plugin-babel';


const BANNER = fs.readFileSync("./src/banner.js").toString();

function getPlugins(babelConfig) {
  return [
    // Convert CommonJS modules to ES6
    commonjs(),
    nodePolyfills(),
    // Resolve modules from node_modules
    resolve({
      // use the "browser" property in package.json
      browser: true,
      preferBuiltins: false
    }),
    babel(babelConfig),
    // Package json data as an ES6 module
    json()
  ];
}

export default [
  {
    input: "src/index.js",
    output: {
      banner: BANNER,
      name: "main",
      file: "dist/main.js",
      format: "esm",
      sourcemap: false
    },
    external: ['cookies', 'http-request', 'log', 'create-response'],
    plugins: getPlugins(
      {
        inputSourceMap: true,
        sourceMaps: true,
        exclude: ["node_modules/**", /\/core-js\//],

        presets: [
          [
            "@babel/preset-env",
            {
              useBuiltIns: "usage",
              corejs: 3,
              modules: false,
              targets: {
                browsers: [
                  "last 2 Chrome versions",
                  "last 2 Firefox versions",
                  "last 2 Safari versions"
                ]
              }
            }
          ]
        ],
        plugins: []
      }
    )
  }
];

1
  • You might want to update your question as to why you need to bundle crypto in the first place. crypto is a core Node.js module, it shouldn't be included in the bundle unless there is another issue not being mentioned here. Commented May 26, 2022 at 20:49

1 Answer 1

1

the crypto module relies on having either a Node.js or a browser environment, as it provides the implementation by wrapping the underlying platform APIs. Akamai EdgeWorkers does not yet provide a native crypto functionality, so you would need to use a JS library which provides a pure JS implementation.

I have had success with using crypto-es (https://www.npmjs.com/package/crypto-es) as long as you are careful to watch CPU limits.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.