An in-memory mock server compatible with Cloudinary SDK methods for uploading and retrieving files.
NOTE: No transformations are performed; the raw file is always returned.
From the cli:
npx @ceteio/cloudinary-memory-server
or, in JS:
yarn add @ceteio/cloudinary-memory-server
const cloudinaryMemServer = require("@ceteio/cloudinary-memory-server");
cloudinaryMemServer();
const cloudinary = require("cloudinary");
cloudinary.config({
cloud_name: "na",
api_key: "na",
api_secret: "na",
upload_prefix: "https://localhost:9443"
});
(NOTE: See SSL certificates below for important information to avoid SSL errors)
Method | Path |
---|---|
GET | /:cloudname/image/upload/:public_id |
POST | /:api_version/:cloudname/image/upload |
DELETE | /:api_version/:cloudname/resources/image/upload |
The cloudinary
node module (and possibly other languages also) require the
upload_prefix
to be a secure URL (ie; https
). By default,
cloudinary-memory-server
will generate an SSL certificate to enable the secure
URL.
However, this certificate will be regenerated each time
cloudinary-memory-server
is run, requiring manual approval in your browswer
before images will load.
To skip the approval step, it's possible to create and install a permanent
trusted certificate which can then be passed into cloudinary-memory-server
.
The recommended tool is mkcert
:
- Install
mkcert
- Setup the RootCA:
mkcert -install
- Create the certificates:
cd <your-project-dir> mkcert -key-file localhost-key.pem -cert-file localhost.pem localhost 127.0.0.1 ::1
- Add .pem files to .gitignore:
echo '*.pem' >> .gitignore
- Pass the certificate files to
cloudinary-memory-server
:or, in JavaScript:SSL_KEY_FILE=localhost-key.pem SSL_CERT_FILE=localhost.pem npx @ceteio/cloudinary-memory-server
const cloudinaryMemServer = require("@ceteio/cloudinary-memory-server"); cloudinaryMemServer({ sslKeyFile: "./localhost-key.pem", sslCertFile: "./localhost.pem" });
When using the node cloudinary
client, it is important to tell node that the
SSL certificate used by cloudinary-memory-server
can be trusted. How you do
that depends on how you're setting up your SSL certificates.
Node does not use the system root store, so it won't accept mkcert certificates
automatically. Instead, you will have to set the
NODE_EXTRA_CA_CERTS
environment variable.
Given your node script is setup like so:
// index.js
const cloudinary = require("cloudinary");
cloudinary.config({
cloud_name: "na",
api_key: "na",
api_secret: "na",
upload_prefix: "https://localhost:9443"
});
Run it with the environment variable set:
NODE_EXTRA_CA_CERTS="$(mkcert -CAROOT)/rootCA.pem" node index.js
Set the NODE_TLS_REJECT_UNAUTHORIZED
env var, which tells node to trust all
SSL certificates. This will trust the automatically generated certificate, but
will also trust any https connection even if it would normally throw an error.
This can pose a risk of Man In The Middle (MITM) attacks, and should be
considered insecure.
Given your node script is setup like so:
// index.js
const cloudinary = require("cloudinary");
cloudinary.config({
cloud_name: "na",
api_key: "na",
api_secret: "na",
upload_prefix: "https://localhost:9443"
});
Run it insecurely with the environment variable set:
NODE_TLS_REJECT_UNAUTHORIZED=0 node index.js
- @romajs for
cloudinary-mock
:cloudinary-memory-server
wouldn't be possible without it!