crypto module not available

I wish to use cloud code to connect to azure cosmos db. however, the way cosmos authenciate users requires use of the hmacsha256 encryption.

in the cloud code, when I put
const crypto = require("crypto");

it says crypto module is missing

You would have to use Cloud Code C# for that. This way HMACSHA256 will be available via system libraries. You may also import any external nuget packages such as: https://www.nuget.org/packages/Microsoft.Azure.Cosmos to fulfill your usecase.

1 Like

Hi Wuzytech!

While you could achieve this with CloudCode C#, you could use the Deployment package with "bundling".

The details are here: https://docs.unity.com/cloud-code/en/manual/scripts/how-to-guides/write-scripts/deployment-window#JS_bundles

But in a nutshell, if you have an additional script you want to bundle, you do something like this:

const lib = require("./lib"); //replace by crypto here which should be installed locally
module.exports = async ({ params, context, logger }) => {
    return {
        "value": lib.helloWorld
    };
};
module.exports.bundling = true;
1 Like

Thanks MariusUrbelis and GabKBelmonte. I can use custom library now using the ccm.

Regarding the bundling. Is it used for cloud code script/module to call methods in another cloud code script/module?
I have created a cloud code module named "CloudCodeModules", and created a test cloud code script (using dashboard). but it says "CompilationError: Cannot find module './CloudCodeModules'"
In the test cloud code script, I tried to import the CloudCodeModule by adding
const ccm = require("./CloudCodeModules");
and
module.exports.bundling = true;

Is my understanding wrong?

Hi @wuzytech ,

I was on PTO, but now that I've returned I can look at this, I'm sorry for the delay.
For

Bundling currently only works from the editor with the cloud code package, the specific link here (it seems to have moved since I last sent it :(), and the deployment package.

The idea of bundles is to allow you to have libraries you embed into your cloud-code script before it gets deployed.
They get merged together, a very common use-case in JavaScript and web dev.

CloudCode Modules, on the other hand, are C# libraries you can build and call, the same way as CloudCode Scripts.
Calling each other is a different topic.

If I understand your problem correctly, you wanted to use a crypto library

const crypto = require("crypto");

This cannot be achieved from the dashboard, but can be achieved via the editor with the above two packages:

const lib = require("./crypto"); //replace by crypto here which should exist locally
module.exports = async ({ params, context, logger }) => {
    return {
        "value": lib.helloWorld
    };
};
module.exports.bundling = true; //this instructs the editor to merge the two files before deployment

Specifically, this line

module.exports.bundling = true; //this instructs the editor to merge the two files before deployment

This functionality will be brought to the UGS CLI, but not to the dashboard.

I will attach a GIF here from a previous post that will hopefully provide more clarity:

8941677--1226634--rw-2.gif