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

"crypto" is undefined for extensions #3338

Closed
josias-r opened this issue Dec 7, 2020 · 7 comments
Closed

"crypto" is undefined for extensions #3338

josias-r opened this issue Dec 7, 2020 · 7 comments

Comments

@josias-r
Copy link

josias-r commented Dec 7, 2020

I've got a hook that makes use of the npm package "uuid". This package under the hood makes use of the crypto API (supported by modern browsers).

This crypto API is not available inside the directus hooks. Here's a simple example that will break directus:

var registerHook = function () {
  return {
    "server.start": function () {
      console.log("crypto", crypto);
    },
  };
};

module.exports = registerHook;

Seeing the crypto API not being available, I wonder what else might not work inside the hooks.

@rijkvanzanten
Copy link
Member

Did you require crypto from node at the top of the file?

const crypto = require('crypto');

@josias-r
Copy link
Author

josias-r commented Dec 7, 2020

@rijkvanzanten You are right, with my example the problem is fixed. I put some more effort into debugging my scenario.

I figured out that I have a problem with rollup and my bundled hook. This works:

const uuidv4 = require("uuid").v4;

var registerHook = function () {
  return {
    "server.start": function () {
      console.log("uuid:", uuidv4());
    },
  };
};

module.exports = registerHook;

But the same example (using "import" & typescript) does not work anymore after I but my hooks through rollup bundler.

This is my rollup config:

import typescript from "@rollup/plugin-typescript";
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import multiInput from "rollup-plugin-multi-input";
import json from "@rollup/plugin-json";

export default {
  input: ["src/extensions/*/*/index.ts"],
  output: {
    dir: "dist",
    format: "cjs",
    chunkFileNames: "chunks/[name]-[hash].js",
  },
  plugins: [
    multiInput({ relative: "src/" }),
    commonjs(),
    nodeResolve(),
    json(),
    typescript(),
  ],
};

Do I need to reconfigure my rollup set-up?

@rijkvanzanten
Copy link
Member

There's no need to bundle your hooks if it's running under Node

@josias-r
Copy link
Author

josias-r commented Dec 7, 2020

That's a bummer. I was setting something up so I could bundle extensions externally.
This way I could have a repo for the extensions + a repo for direcus. This means, I would be able to configure extensions on a per-project basis (with an extra build step) without having to create a custom repo for each project (means DigitalOcean i.e. would automatically deploy all projects)

@rijkvanzanten
Copy link
Member

I mean, you could. I'm assuming that you're rollup config is bundling things even though it shouldn't, or your TypeScript example exports it wrong. Keep in mind that TS

export default _____

is generated to

module.exports = {
  default: ____
}

which is not the correct format

@josias-r
Copy link
Author

josias-r commented Dec 7, 2020

@rijkvanzanten For me, the export default works fine (maybe this depends on the tsconfig?).

I think my problem actually has it's origin on how the package "uuid" is built, because my config works fine when I use import for crypto(or other node modules) myself.

For example import crypto from "crypto" will compile to const crypto = require("crypto") as it should be, while the compiled code of "uuid" does not try to import/require it and just checks whether crypto is already defined or not.

I was able to find a workaround for my specific problem by adding import "crypto"; to the top of my hook 😄 Never mind, doesn't work either, I'm still searching one

@josias-r
Copy link
Author

josias-r commented Dec 8, 2020

Here we go: uuidjs/uuid#544

Turns out this barely documented node exportOption is what I needed inside the rollup config: nodeResolve({exportConditions: ["node"]})
Works like a charm now 🥳

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants