feat: use cloud runtime package (#9)

* feat: Include @noodl/cloud-runtime in the dist folder

* feat: Use the builtin cloud runtime

Unless "NOODL_CLOUD_RUNTIMES_LOCATION" is provided.

* chore: bump version
This commit is contained in:
Eric Tuvesson
2024-01-26 10:38:48 +01:00
committed by GitHub
parent c89f7ef796
commit 413e7b3fcd
7 changed files with 72 additions and 27 deletions

View File

@@ -58,3 +58,16 @@ This package contains the docker image of the Noodl Self Hosted Cloud Service.
**Description**: The **`MAX_UPLOAD_SIZE`** variable specifies the maximum allowed size for file uploads in the application.
**Default Value: `20mb`**
### `NOODL_CLOUD_RUNTIMES_LOCATION`
**Description**: The **`NOODL_CLOUD_RUNTIMES_LOCATION`** variable specifies the URL location where to find the runtime to use.
To use the Noodl hosted cloud runtimes, use this URL:
`https://runtimes.noodl.cloud/{runtime}.js`
`{runtime}` will be replaced with the desired runtime by the Cloud Functions.
If this variable is not provided then the built in cloud runtime will be used.
**Default Value: `undefined`**

View File

@@ -1,6 +1,6 @@
{
"name": "@noodl/cloudservice-docker",
"version": "0.2.5",
"version": "0.3.0",
"description": "Low-code for when experience matter",
"author": "Noodl <info@noodl.net>",
"homepage": "https://noodl.net",

View File

@@ -1,14 +1,15 @@
{
"name": "@noodl/cloudservice",
"version": "0.2.4",
"version": "0.2.5",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@noodl/cloudservice",
"version": "0.2.4",
"version": "0.2.5",
"license": "MIT",
"dependencies": {
"@noodl/cloud-runtime": "^0.6.3",
"isolated-vm": "^4.4.2",
"node-fetch": "2.6.7",
"parse-server": "^4.10.4",
@@ -656,6 +657,11 @@
"tslib": "^2.0.1"
}
},
"node_modules/@noodl/cloud-runtime": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/@noodl/cloud-runtime/-/cloud-runtime-0.6.3.tgz",
"integrity": "sha512-RZtzTSi4I5UVBEMVAhmA1OX7Gqo5mcVkHWWm51oCNk/7benNa/D+PEUmGl5VMicVrJUG+/Ca6KI+9xq0D+kr1A=="
},
"node_modules/@parse/fs-files-adapter": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@parse/fs-files-adapter/-/fs-files-adapter-1.2.0.tgz",
@@ -7405,6 +7411,11 @@
"tslib": "^2.0.1"
}
},
"@noodl/cloud-runtime": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/@noodl/cloud-runtime/-/cloud-runtime-0.6.3.tgz",
"integrity": "sha512-RZtzTSi4I5UVBEMVAhmA1OX7Gqo5mcVkHWWm51oCNk/7benNa/D+PEUmGl5VMicVrJUG+/Ca6KI+9xq0D+kr1A=="
},
"@parse/fs-files-adapter": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@parse/fs-files-adapter/-/fs-files-adapter-1.2.0.tgz",

View File

@@ -1,6 +1,6 @@
{
"name": "@noodl/cloudservice",
"version": "0.2.5",
"version": "0.3.0",
"description": "Low-code for when experience matter",
"author": "Noodl <info@noodl.net>",
"homepage": "https://noodl.net",
@@ -18,6 +18,7 @@
"build": "npm run build:clean && npm run build:ts && npm run build:static"
},
"dependencies": {
"@noodl/cloud-runtime": "^0.6.3",
"isolated-vm": "^4.4.2",
"node-fetch": "2.6.7",
"parse-server": "^4.10.4",

View File

@@ -9,3 +9,12 @@ fs.cpSync(
dist,
{ recursive: true }
);
// Copy the noodl cloud runtime to the dist folder
fs.copyFile(
path.join(__dirname, "../node_modules/@noodl/cloud-runtime/dist/main.js"),
path.join(__dirname, "../dist/static/cloud-runtime.js"),
(err) => {
if (err) throw err;
}
);

View File

@@ -219,17 +219,7 @@ export async function createContext(global: ContextGlobalState, env: CreateConte
);
}
const _defaultRuntime = process.env.NOODL_DEFAULT_CLOUD_RUNTIME;
let runtime = cloudRuntime || _defaultRuntime;
if (!runtime.endsWith(".js")) runtime = runtime + ".js";
console.log("- Using runtime: " + runtime);
const snapshot = await getRuntimeSnapshot(
(process.env.NOODL_CLOUD_RUNTIMES_LOCATION ||
"https://runtimes.noodl.cloud") +
"/" +
runtime
);
const snapshot = await getRuntimeSnapshot(cloudRuntime);
console.log("- Starting up isolate");
const isolate = new ivm.Isolate({ memoryLimit: env.memoryLimit, snapshot });

View File

@@ -1,5 +1,7 @@
import fetch from "node-fetch";
import ivm from "isolated-vm";
import path from "path";
import fs from "fs";
// Create a snapshot of a given runtime if needed
// of serve from the cache
@@ -21,20 +23,39 @@ async function fetchRuntime(url: string) {
return createSnapshot(script)
}
export async function getRuntimeSnapshot(url: string) {
if (snapshots[url]) {
try {
await snapshots[url];
} catch (e) {
console.log(`Disposing runtime snapshot due to error in create: `, e);
delete snapshots[url];
export async function getRuntimeSnapshot(functionRuntimeVersion: string) {
const cloudRuntimeUrl = process.env.NOODL_CLOUD_RUNTIMES_LOCATION;
if (cloudRuntimeUrl) {
const url = cloudRuntimeUrl.replace("{runtime}", functionRuntimeVersion);
if (snapshots[url]) {
try {
await snapshots[url];
} catch (e) {
console.log(`Disposing runtime snapshot due to error in create: `, e);
delete snapshots[url];
}
}
if (!snapshots[url]) {
snapshots[url] = fetchRuntime(url);
}
}
if (snapshots[url]) {
console.log("- Using runtime: " + url);
return snapshots[url];
} else {
return snapshots[url] = fetchRuntime(url);
}
}
// Create a snapshot with the builtin cloud runtime
if (!snapshots['__builtin']) {
const filePath = path.join(__dirname, '../static/cloud-runtime.js');
if (!fs.existsSync(filePath)) {
throw new Error("Failed to find builtin cloud runtime: " + filePath);
}
const fileContent = fs.readFileSync(filePath, 'utf-8');
snapshots['__builtin'] = Promise.resolve(createSnapshot(fileContent));
console.log("- Using runtime: builtin");
}
return snapshots['__builtin'];
}