@nodegx/backend 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nodegx-online.js +1 -0
- package/dist/cloud-bundle.cjs +38 -2
- package/dist/cloudflare-deploy.cjs +139 -100
- package/package.json +1 -1
package/bin/nodegx-online.js
CHANGED
|
@@ -77,6 +77,7 @@ function wrangler(args) {
|
|
|
77
77
|
const { wranglerCommand } = require(need(path.join(DIST, 'cloudflare-deploy.cjs'), 'The deploy (dist/cloudflare-deploy.cjs)'));
|
|
78
78
|
const [cmd, ...pre] = wranglerCommand();
|
|
79
79
|
const run = spawnSync(cmd, [...pre, ...args], { stdio: 'inherit', env: { ...process.env, WRANGLER_SEND_METRICS: 'false' } });
|
|
80
|
+
if (run.error) process.stderr.write(`@nodegx/backend: wrangler could not be started (${[cmd, ...pre].join(' ')}: ${run.error.code || run.error.message}).\n`);
|
|
80
81
|
return run.status ?? 1;
|
|
81
82
|
}
|
|
82
83
|
|
package/dist/cloud-bundle.cjs
CHANGED
|
@@ -28418,6 +28418,20 @@ var init_node_catalog = __esm({
|
|
|
28418
28418
|
valueType: "signal",
|
|
28419
28419
|
example: "changed-Sa1"
|
|
28420
28420
|
},
|
|
28421
|
+
{
|
|
28422
|
+
pattern: "changed-createdAt",
|
|
28423
|
+
plug: "output",
|
|
28424
|
+
group: "Events",
|
|
28425
|
+
valueType: "signal",
|
|
28426
|
+
example: "changed-createdAt"
|
|
28427
|
+
},
|
|
28428
|
+
{
|
|
28429
|
+
pattern: "changed-updatedAt",
|
|
28430
|
+
plug: "output",
|
|
28431
|
+
group: "Events",
|
|
28432
|
+
valueType: "signal",
|
|
28433
|
+
example: "changed-updatedAt"
|
|
28434
|
+
},
|
|
28421
28435
|
{
|
|
28422
28436
|
pattern: "collectionName",
|
|
28423
28437
|
plug: "input",
|
|
@@ -28434,6 +28448,20 @@ var init_node_catalog = __esm({
|
|
|
28434
28448
|
group: "Properties",
|
|
28435
28449
|
valueType: "string",
|
|
28436
28450
|
example: "prop-Sa1"
|
|
28451
|
+
},
|
|
28452
|
+
{
|
|
28453
|
+
pattern: "prop-createdAt",
|
|
28454
|
+
plug: "output",
|
|
28455
|
+
group: "Properties",
|
|
28456
|
+
valueType: "date",
|
|
28457
|
+
example: "prop-createdAt"
|
|
28458
|
+
},
|
|
28459
|
+
{
|
|
28460
|
+
pattern: "prop-updatedAt",
|
|
28461
|
+
plug: "output",
|
|
28462
|
+
group: "Properties",
|
|
28463
|
+
valueType: "date",
|
|
28464
|
+
example: "prop-updatedAt"
|
|
28437
28465
|
}
|
|
28438
28466
|
]
|
|
28439
28467
|
},
|
|
@@ -144602,7 +144630,7 @@ var init_cloud_node_library = __esm({
|
|
|
144602
144630
|
plug: "output",
|
|
144603
144631
|
group: "Events",
|
|
144604
144632
|
displayName: "Fetched",
|
|
144605
|
-
description: "Fires when the Id binds a record
|
|
144633
|
+
description: "Fires when the Id binds a record already in memory, when a record the Id names is first read from the backend, and when a Fetch finishes reading it; use Done to act on the answer to a Fetch"
|
|
144606
144634
|
},
|
|
144607
144635
|
{
|
|
144608
144636
|
name: "changed",
|
|
@@ -144778,6 +144806,14 @@ var init_cloud_node_library = __esm({
|
|
|
144778
144806
|
displayName: "Only If Unchanged",
|
|
144779
144807
|
description: "Property names, comma-separated (e.g. version). The update is applied only if these still hold the values the record had when it was read; otherwise Failure fires and nothing is written. Fetch the record again before retrying. Needs a NodeGX backend. Leave empty to always write."
|
|
144780
144808
|
},
|
|
144809
|
+
{
|
|
144810
|
+
name: "addTo",
|
|
144811
|
+
type: "string",
|
|
144812
|
+
plug: "input",
|
|
144813
|
+
group: "General",
|
|
144814
|
+
displayName: "Add To",
|
|
144815
|
+
description: "Property names, comma-separated (e.g. points). For these, the value wired in is an amount added to what the backend holds, in one step on the backend, instead of replacing it: two devices adding at once both count. Wire the amount (1, -1, 5). The record then shows the backend's total. Leave empty to write every value as given."
|
|
144816
|
+
},
|
|
144781
144817
|
{
|
|
144782
144818
|
name: "idSource",
|
|
144783
144819
|
type: {
|
|
@@ -151647,7 +151683,7 @@ var init_cloud_node_library = __esm({
|
|
|
151647
151683
|
},
|
|
151648
151684
|
{
|
|
151649
151685
|
name: "userId",
|
|
151650
|
-
type: "
|
|
151686
|
+
type: "string",
|
|
151651
151687
|
plug: "output",
|
|
151652
151688
|
group: "Request",
|
|
151653
151689
|
displayName: "User Id",
|
|
@@ -251,14 +251,14 @@ var require_model = __commonJS({
|
|
|
251
251
|
};
|
|
252
252
|
Model2.prototype.set = function(name2, value2, args) {
|
|
253
253
|
if (args && args.resolve && name2.indexOf(".") !== -1) {
|
|
254
|
-
var
|
|
254
|
+
var path5 = name2.split(".");
|
|
255
255
|
var model = this;
|
|
256
|
-
for (var i = 0; i <
|
|
257
|
-
var v = model.get(
|
|
256
|
+
for (var i = 0; i < path5.length - 1; i++) {
|
|
257
|
+
var v = model.get(path5[i]);
|
|
258
258
|
if (Model2.instanceOf(v)) model = v;
|
|
259
259
|
else return;
|
|
260
260
|
}
|
|
261
|
-
model.set(
|
|
261
|
+
model.set(path5[path5.length - 1], value2);
|
|
262
262
|
return;
|
|
263
263
|
}
|
|
264
264
|
const forceChange = args && args.forceChange;
|
|
@@ -271,14 +271,14 @@ var require_model = __commonJS({
|
|
|
271
271
|
};
|
|
272
272
|
Model2.prototype.get = function(name2, args) {
|
|
273
273
|
if (args && args.resolve && name2.indexOf(".") !== -1) {
|
|
274
|
-
var
|
|
274
|
+
var path5 = name2.split(".");
|
|
275
275
|
var model = this;
|
|
276
|
-
for (var i = 0; i <
|
|
277
|
-
var v = model.get(
|
|
276
|
+
for (var i = 0; i < path5.length - 1; i++) {
|
|
277
|
+
var v = model.get(path5[i]);
|
|
278
278
|
if (Model2.instanceOf(v)) model = v;
|
|
279
279
|
else return;
|
|
280
280
|
}
|
|
281
|
-
return model.get(
|
|
281
|
+
return model.get(path5[path5.length - 1]);
|
|
282
282
|
}
|
|
283
283
|
return this.data[name2];
|
|
284
284
|
};
|
|
@@ -3742,9 +3742,9 @@ __export(deployCloudflare_exports, {
|
|
|
3742
3742
|
module.exports = __toCommonJS(deployCloudflare_exports);
|
|
3743
3743
|
var import_node_child_process = require("node:child_process");
|
|
3744
3744
|
var crypto4 = __toESM(require("node:crypto"));
|
|
3745
|
-
var
|
|
3745
|
+
var fs5 = __toESM(require("node:fs"));
|
|
3746
3746
|
var os2 = __toESM(require("node:os"));
|
|
3747
|
-
var
|
|
3747
|
+
var path4 = __toESM(require("node:path"));
|
|
3748
3748
|
|
|
3749
3749
|
// ../noodl-runtime/src/nodes/std-library/expression-ports.ts
|
|
3750
3750
|
var EXPRESSION_MATH_ALIASES = [
|
|
@@ -4314,11 +4314,11 @@ function namedRoles(config) {
|
|
|
4314
4314
|
// ../nodegx-backend/cloudflare/applyProject.ts
|
|
4315
4315
|
var ApplyError = class extends Error {
|
|
4316
4316
|
};
|
|
4317
|
-
async function must(call, method,
|
|
4318
|
-
const answer = await call(method,
|
|
4317
|
+
async function must(call, method, path5, body) {
|
|
4318
|
+
const answer = await call(method, path5, body);
|
|
4319
4319
|
if (answer.status >= 300) {
|
|
4320
4320
|
const said = answer.json?.error ?? answer.text.slice(0, 600);
|
|
4321
|
-
throw new ApplyError(`${method} /${
|
|
4321
|
+
throw new ApplyError(`${method} /${path5.replace(/^\//, "")} answered ${answer.status}: ${said}`);
|
|
4322
4322
|
}
|
|
4323
4323
|
return answer;
|
|
4324
4324
|
}
|
|
@@ -4419,11 +4419,11 @@ async function applyProject(call, project, options) {
|
|
|
4419
4419
|
function automationsSha256(text) {
|
|
4420
4420
|
return crypto.createHash("sha256").update(text).digest("hex");
|
|
4421
4421
|
}
|
|
4422
|
-
async function must2(call, method,
|
|
4423
|
-
const answer = await call(method,
|
|
4422
|
+
async function must2(call, method, path5, body) {
|
|
4423
|
+
const answer = await call(method, path5, body);
|
|
4424
4424
|
if (answer.status >= 300) {
|
|
4425
4425
|
const said = answer.json?.error ?? answer.text.slice(0, 600);
|
|
4426
|
-
throw new ApplyError(`${method} /${
|
|
4426
|
+
throw new ApplyError(`${method} /${path5.replace(/^\//, "")} answered ${answer.status}: ${said}`);
|
|
4427
4427
|
}
|
|
4428
4428
|
return answer;
|
|
4429
4429
|
}
|
|
@@ -5088,9 +5088,50 @@ function wranglerConfig(input2) {
|
|
|
5088
5088
|
return lines.join("\n");
|
|
5089
5089
|
}
|
|
5090
5090
|
|
|
5091
|
+
// ../nodegx-backend/cloudflare/wranglerSpawn.ts
|
|
5092
|
+
var fs4 = __toESM(require("node:fs"));
|
|
5093
|
+
var path3 = __toESM(require("node:path"));
|
|
5094
|
+
var NPX_WRANGLER = ["--yes", "wrangler@4"];
|
|
5095
|
+
function windowsNpxCli(host) {
|
|
5096
|
+
const p = path3.win32;
|
|
5097
|
+
const candidates = [p.join(p.dirname(host.execPath), "node_modules", "npm", "bin", "npx-cli.js")];
|
|
5098
|
+
const npm = host.env.npm_execpath;
|
|
5099
|
+
if (npm && /npm-cli\.js$/i.test(npm)) candidates.push(p.join(p.dirname(npm), "npx-cli.js"));
|
|
5100
|
+
return candidates.find((f) => host.exists(f)) ?? null;
|
|
5101
|
+
}
|
|
5102
|
+
function wranglerCommandFor(host) {
|
|
5103
|
+
if (host.env.NODEGX_WRANGLER) return [host.execPath, host.env.NODEGX_WRANGLER];
|
|
5104
|
+
for (const from of host.from) {
|
|
5105
|
+
try {
|
|
5106
|
+
return [host.execPath, host.resolve("wrangler/bin/wrangler.js", from)];
|
|
5107
|
+
} catch {
|
|
5108
|
+
}
|
|
5109
|
+
}
|
|
5110
|
+
if (host.platform === "win32") {
|
|
5111
|
+
const cli = windowsNpxCli(host);
|
|
5112
|
+
if (cli) return [host.execPath, cli, ...NPX_WRANGLER];
|
|
5113
|
+
}
|
|
5114
|
+
return ["npx", ...NPX_WRANGLER];
|
|
5115
|
+
}
|
|
5116
|
+
function realHost(from) {
|
|
5117
|
+
return {
|
|
5118
|
+
env: process.env,
|
|
5119
|
+
platform: process.platform,
|
|
5120
|
+
execPath: process.execPath,
|
|
5121
|
+
from,
|
|
5122
|
+
resolve: (request, dir) => require.resolve(request, { paths: [dir] }),
|
|
5123
|
+
exists: (f) => fs4.existsSync(f)
|
|
5124
|
+
};
|
|
5125
|
+
}
|
|
5126
|
+
function spawnFailure(command, error) {
|
|
5127
|
+
if (!error) return null;
|
|
5128
|
+
const code = error.code;
|
|
5129
|
+
return `wrangler could not be started (${command.join(" ")}: ${code || error.message}). Install it with \`npm install -g wrangler@4\` and point the deploy at it with NODEGX_WRANGLER set to its bin/wrangler.js.`;
|
|
5130
|
+
}
|
|
5131
|
+
|
|
5091
5132
|
// ../nodegx-backend/cloudflare/deployCloudflare.ts
|
|
5092
5133
|
var HERE = process.env.NODEGX_CLOUDFLARE_SRC || __dirname;
|
|
5093
|
-
var PACKAGES =
|
|
5134
|
+
var PACKAGES = path4.resolve(HERE, "..", "..");
|
|
5094
5135
|
var DeployError = class extends Error {
|
|
5095
5136
|
};
|
|
5096
5137
|
var say = (line = "") => process.stdout.write(line + "\n");
|
|
@@ -5113,9 +5154,9 @@ ${USAGE}`);
|
|
|
5113
5154
|
if (a === "--name") o.name = next();
|
|
5114
5155
|
else if (a === "--bucket") o.bucket = next();
|
|
5115
5156
|
else if (a === "--no-bucket") o.bucket = null;
|
|
5116
|
-
else if (a === "--viewer") o.viewer =
|
|
5157
|
+
else if (a === "--viewer") o.viewer = path4.resolve(next());
|
|
5117
5158
|
else if (a === "--allow-development-engine") o.allowDevelopmentEngine = true;
|
|
5118
|
-
else if (a === "--out") o.out =
|
|
5159
|
+
else if (a === "--out") o.out = path4.resolve(next());
|
|
5119
5160
|
else if (a === "--local") o.local = true;
|
|
5120
5161
|
else if (a === "--port") o.port = Number(next());
|
|
5121
5162
|
else if (a === "--make-roles") o.makeRoles = true;
|
|
@@ -5130,7 +5171,7 @@ ${USAGE}`);
|
|
|
5130
5171
|
else if (a === "--account") o.account = next().trim();
|
|
5131
5172
|
else if (a.startsWith("--")) throw new DeployError(`${a} is not an option.
|
|
5132
5173
|
${USAGE}`);
|
|
5133
|
-
else if (!o.project) o.project =
|
|
5174
|
+
else if (!o.project) o.project = path4.resolve(a);
|
|
5134
5175
|
else throw new DeployError(`One project at a time.
|
|
5135
5176
|
${USAGE}`);
|
|
5136
5177
|
}
|
|
@@ -5138,20 +5179,20 @@ ${USAGE}`);
|
|
|
5138
5179
|
return o;
|
|
5139
5180
|
}
|
|
5140
5181
|
function readProject(dir) {
|
|
5141
|
-
const file =
|
|
5142
|
-
if (!
|
|
5143
|
-
const json = JSON.parse(
|
|
5144
|
-
const policyFile =
|
|
5145
|
-
const policyText =
|
|
5182
|
+
const file = path4.join(dir, "nodegx.project.json");
|
|
5183
|
+
if (!fs5.existsSync(file)) throw new DeployError(`${dir} is not a NodeGX project: it has no nodegx.project.json.`);
|
|
5184
|
+
const json = JSON.parse(fs5.readFileSync(file, "utf8"));
|
|
5185
|
+
const policyFile = path4.join(dir, "nodegx.security.json");
|
|
5186
|
+
const policyText = fs5.existsSync(policyFile) ? fs5.readFileSync(policyFile, "utf8") : null;
|
|
5146
5187
|
if (policyText) JSON.parse(policyText);
|
|
5147
|
-
const automationsFile =
|
|
5148
|
-
const automationsText =
|
|
5188
|
+
const automationsFile = path4.join(dir, "nodegx.automations.json");
|
|
5189
|
+
const automationsText = fs5.existsSync(automationsFile) ? fs5.readFileSync(automationsFile, "utf8") : null;
|
|
5149
5190
|
if (automationsText) JSON.parse(automationsText);
|
|
5150
|
-
const envFile =
|
|
5151
|
-
const envValues =
|
|
5191
|
+
const envFile = path4.join(dir, ".env");
|
|
5192
|
+
const envValues = fs5.existsSync(envFile) ? fs5.readFileSync(envFile, "utf8").split("\n").map((l) => l.replace(/^\s*(export\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=\s*/, "").replace(/^['"]|['"]\s*$/g, "").trim()).filter((v) => v.length >= 8) : [];
|
|
5152
5193
|
return {
|
|
5153
5194
|
dir,
|
|
5154
|
-
name: json.name ||
|
|
5195
|
+
name: json.name || path4.basename(dir),
|
|
5155
5196
|
file: json,
|
|
5156
5197
|
policyText,
|
|
5157
5198
|
automationsText,
|
|
@@ -5160,20 +5201,20 @@ function readProject(dir) {
|
|
|
5160
5201
|
};
|
|
5161
5202
|
}
|
|
5162
5203
|
function deploymentsRoot() {
|
|
5163
|
-
return process.env.NODEGX_CLOUDFLARE_HOME ||
|
|
5204
|
+
return process.env.NODEGX_CLOUDFLARE_HOME || path4.join(os2.homedir(), ".nodegx", "cloudflare");
|
|
5164
5205
|
}
|
|
5165
5206
|
function readRecord(dir) {
|
|
5166
|
-
const file =
|
|
5167
|
-
return
|
|
5207
|
+
const file = path4.join(dir, "deployment.json");
|
|
5208
|
+
return fs5.existsSync(file) ? JSON.parse(fs5.readFileSync(file, "utf8")) : null;
|
|
5168
5209
|
}
|
|
5169
5210
|
function writeRecord(dir, record) {
|
|
5170
|
-
const file =
|
|
5171
|
-
|
|
5172
|
-
|
|
5211
|
+
const file = path4.join(dir, "deployment.json");
|
|
5212
|
+
fs5.writeFileSync(file, JSON.stringify(record, null, 2) + "\n", { mode: 384 });
|
|
5213
|
+
fs5.chmodSync(file, 384);
|
|
5173
5214
|
}
|
|
5174
5215
|
function cloudFunctions(projectDir) {
|
|
5175
|
-
const entry = process.env.NODEGX_CLOUD_BUNDLE ||
|
|
5176
|
-
if (!
|
|
5216
|
+
const entry = process.env.NODEGX_CLOUD_BUNDLE || path4.join(PACKAGES, "noodl-mcp", "dist", "cloud-bundle.cjs");
|
|
5217
|
+
if (!fs5.existsSync(entry)) {
|
|
5177
5218
|
throw new DeployError(`The cloud-function bundler is not here (${entry}): run \`npm run build\` in packages/noodl-mcp.`);
|
|
5178
5219
|
}
|
|
5179
5220
|
const run = (0, import_node_child_process.spawnSync)(process.execPath, [entry, projectDir], { encoding: "utf8", maxBuffer: 64 * 1024 * 1024, timeout: 12e4 });
|
|
@@ -5184,8 +5225,8 @@ ${(run.stderr || "").trim() || "no output"}`);
|
|
|
5184
5225
|
var NOT_FOR_THE_HOST = ["DEPLOY.md", "serve.js", "package.json", ".nodegx-deploy.json", "nodegx-config.json"];
|
|
5185
5226
|
var VIEWER_HINT = "\nName a production viewer with --viewer <folder holding noodl.deploy.js>.";
|
|
5186
5227
|
function siteThroughCli(copy, out, allowDev) {
|
|
5187
|
-
const cli = process.env.NODEGX_CLI ||
|
|
5188
|
-
if (!process.env.NODEGX_CLI && process.env.NODEGX_DEPLOY_CLI && !
|
|
5228
|
+
const cli = process.env.NODEGX_CLI || path4.join(PACKAGES, "nodegx-export", "dist", "cli.mjs");
|
|
5229
|
+
if (!process.env.NODEGX_CLI && process.env.NODEGX_DEPLOY_CLI && !fs5.existsSync(cli)) return null;
|
|
5189
5230
|
const args = [cli, "deploy", copy, out];
|
|
5190
5231
|
if (allowDev) args.push("--allow-development-engine");
|
|
5191
5232
|
const run = (0, import_node_child_process.spawnSync)(process.execPath, args, { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
|
@@ -5197,7 +5238,7 @@ ${(run.stdout + run.stderr).trim()}` + (run.status === 11 ? VIEWER_HINT : ""));
|
|
|
5197
5238
|
}
|
|
5198
5239
|
function siteThroughEngine(copy, out, allowDev) {
|
|
5199
5240
|
const engine = process.env.NODEGX_DEPLOY_CLI;
|
|
5200
|
-
if (!
|
|
5241
|
+
if (!fs5.existsSync(engine)) throw new DeployError(`NODEGX_DEPLOY_CLI is set to ${engine}, and there is nothing there.`);
|
|
5201
5242
|
const args = [engine, copy, out];
|
|
5202
5243
|
if (allowDev) args.push("--allow-development-engine");
|
|
5203
5244
|
const run = (0, import_node_child_process.spawnSync)(process.execPath, args, { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
|
@@ -5221,45 +5262,45 @@ ${report.blank}`);
|
|
|
5221
5262
|
return notes;
|
|
5222
5263
|
}
|
|
5223
5264
|
function buildSite(project, name2, siteDir, o) {
|
|
5224
|
-
const work =
|
|
5265
|
+
const work = fs5.mkdtempSync(path4.join(os2.tmpdir(), "nodegx-cf-site-"));
|
|
5225
5266
|
try {
|
|
5226
|
-
const copy =
|
|
5227
|
-
|
|
5228
|
-
const json = JSON.parse(
|
|
5267
|
+
const copy = path4.join(work, "project");
|
|
5268
|
+
fs5.cpSync(project.dir, copy, { recursive: true, filter: (src) => !/[/\\](\.git|node_modules|\.previews)$/.test(src) });
|
|
5269
|
+
const json = JSON.parse(fs5.readFileSync(path4.join(copy, "nodegx.project.json"), "utf8"));
|
|
5229
5270
|
json.metadata = { ...json.metadata || {}, cloudservices: { endpoint: "", appId: name2, instanceId: name2, type: "nodegx" } };
|
|
5230
|
-
|
|
5231
|
-
const out =
|
|
5271
|
+
fs5.writeFileSync(path4.join(copy, "nodegx.project.json"), JSON.stringify(json, null, 2));
|
|
5272
|
+
const out = path4.join(work, "site");
|
|
5232
5273
|
const allowDev = !!(o.viewer || o.allowDevelopmentEngine);
|
|
5233
5274
|
const notes = siteThroughCli(copy, out, allowDev) ?? siteThroughEngine(copy, out, allowDev);
|
|
5234
5275
|
if (o.viewer) {
|
|
5235
5276
|
for (const f of ["noodl.deploy.js", "noodl.deploy.js.LICENSE.txt"]) {
|
|
5236
|
-
const from =
|
|
5237
|
-
if (
|
|
5277
|
+
const from = path4.join(o.viewer, f);
|
|
5278
|
+
if (fs5.existsSync(from)) fs5.copyFileSync(from, path4.join(out, f));
|
|
5238
5279
|
else if (f === "noodl.deploy.js") throw new DeployError(`--viewer ${o.viewer} holds no noodl.deploy.js.`);
|
|
5239
5280
|
}
|
|
5240
|
-
const engine = readViewerBuild(
|
|
5281
|
+
const engine = readViewerBuild(path4.join(out, "noodl.deploy.js"));
|
|
5241
5282
|
if (engine.kind !== "production") {
|
|
5242
5283
|
throw new DeployError(`--viewer ${o.viewer} is a DEVELOPMENT build of the viewer: ${engine.reasons.join("; ")}.`);
|
|
5243
5284
|
}
|
|
5244
5285
|
notes.push(`viewer: production, from ${o.viewer} (${(engine.bytes / 1e6).toFixed(2)} MB)`);
|
|
5245
5286
|
}
|
|
5246
|
-
for (const f of NOT_FOR_THE_HOST)
|
|
5247
|
-
const hidden = filesUnder2(out).filter((f) =>
|
|
5248
|
-
for (const f of hidden)
|
|
5249
|
-
if (hidden.length) notes.push(`left out (hidden files are never published): ${hidden.map((f) =>
|
|
5250
|
-
|
|
5251
|
-
|
|
5287
|
+
for (const f of NOT_FOR_THE_HOST) fs5.rmSync(path4.join(out, f), { force: true });
|
|
5288
|
+
const hidden = filesUnder2(out).filter((f) => path4.relative(out, f).split(path4.sep).some((part) => part.startsWith(".")));
|
|
5289
|
+
for (const f of hidden) fs5.rmSync(f, { force: true });
|
|
5290
|
+
if (hidden.length) notes.push(`left out (hidden files are never published): ${hidden.map((f) => path4.relative(out, f)).join(", ")}`);
|
|
5291
|
+
fs5.rmSync(siteDir, { recursive: true, force: true });
|
|
5292
|
+
fs5.cpSync(out, siteDir, { recursive: true });
|
|
5252
5293
|
return notes;
|
|
5253
5294
|
} finally {
|
|
5254
|
-
|
|
5295
|
+
fs5.rmSync(work, { recursive: true, force: true });
|
|
5255
5296
|
}
|
|
5256
5297
|
}
|
|
5257
5298
|
async function bundleWorker(outfile, plugins = [], esbuild = require("esbuild")) {
|
|
5258
|
-
const { shared } = require(
|
|
5259
|
-
const nodeSqlite =
|
|
5299
|
+
const { shared } = require(path4.join(HERE, "..", "scripts", "build.js"));
|
|
5300
|
+
const nodeSqlite = path4.join(HERE, "nodeSqlite.ts");
|
|
5260
5301
|
await esbuild.build({
|
|
5261
5302
|
...shared,
|
|
5262
|
-
entryPoints: [
|
|
5303
|
+
entryPoints: [path4.join(HERE, "worker.ts")],
|
|
5263
5304
|
outfile,
|
|
5264
5305
|
format: "esm",
|
|
5265
5306
|
target: "es2022",
|
|
@@ -5287,55 +5328,49 @@ async function bundleWorker(outfile, plugins = [], esbuild = require("esbuild"))
|
|
|
5287
5328
|
}
|
|
5288
5329
|
var WORKER_OWN_MODULES = ["./printed-code.mjs", "./app.mjs"];
|
|
5289
5330
|
async function buildWorker(distDir, app, printed) {
|
|
5290
|
-
|
|
5291
|
-
|
|
5292
|
-
|
|
5331
|
+
fs5.mkdirSync(distDir, { recursive: true });
|
|
5332
|
+
fs5.writeFileSync(path4.join(distDir, "printed-code.mjs"), printed);
|
|
5333
|
+
fs5.writeFileSync(path4.join(distDir, "app.mjs"), `export default ${JSON.stringify(app)};
|
|
5293
5334
|
`);
|
|
5294
5335
|
const prebuilt = process.env.NODEGX_CLOUDFLARE_WORKER;
|
|
5295
5336
|
if (prebuilt) {
|
|
5296
|
-
if (!
|
|
5297
|
-
|
|
5337
|
+
if (!fs5.existsSync(prebuilt)) throw new DeployError(`The prebuilt Worker is not here (${prebuilt}): this copy of the deploy is incomplete.`);
|
|
5338
|
+
fs5.copyFileSync(prebuilt, path4.join(distDir, "worker.mjs"));
|
|
5298
5339
|
return "prebuilt";
|
|
5299
5340
|
}
|
|
5300
|
-
await bundleWorker(
|
|
5341
|
+
await bundleWorker(path4.join(distDir, "worker.mjs"));
|
|
5301
5342
|
return "bundled";
|
|
5302
5343
|
}
|
|
5303
5344
|
function filesUnder2(dir) {
|
|
5304
5345
|
const out = [];
|
|
5305
5346
|
const walk = (d) => {
|
|
5306
|
-
for (const e of
|
|
5307
|
-
const full =
|
|
5347
|
+
for (const e of fs5.readdirSync(d, { withFileTypes: true })) {
|
|
5348
|
+
const full = path4.join(d, e.name);
|
|
5308
5349
|
if (e.isDirectory()) walk(full);
|
|
5309
5350
|
else out.push(full);
|
|
5310
5351
|
}
|
|
5311
5352
|
};
|
|
5312
|
-
if (
|
|
5353
|
+
if (fs5.existsSync(dir)) walk(dir);
|
|
5313
5354
|
return out;
|
|
5314
5355
|
}
|
|
5315
5356
|
function leaks(files, secrets) {
|
|
5316
5357
|
const found = [];
|
|
5317
5358
|
for (const f of files) {
|
|
5318
|
-
const text =
|
|
5359
|
+
const text = fs5.readFileSync(f, "latin1");
|
|
5319
5360
|
for (const s of secrets) if (s.value && text.includes(s.value)) found.push(`${s.what} is in ${f}`);
|
|
5320
5361
|
}
|
|
5321
5362
|
return found;
|
|
5322
5363
|
}
|
|
5323
5364
|
function wranglerCommand() {
|
|
5324
|
-
|
|
5325
|
-
for (const from of [process.cwd(), HERE]) {
|
|
5326
|
-
try {
|
|
5327
|
-
return [process.execPath, require.resolve("wrangler/bin/wrangler.js", { paths: [from] })];
|
|
5328
|
-
} catch {
|
|
5329
|
-
}
|
|
5330
|
-
}
|
|
5331
|
-
return ["npx", "--yes", "wrangler@4"];
|
|
5365
|
+
return wranglerCommandFor(realHost([process.cwd(), HERE]));
|
|
5332
5366
|
}
|
|
5333
5367
|
function wrangler(args, cwd, opts = {}) {
|
|
5334
5368
|
const [cmd, ...pre] = wranglerCommand();
|
|
5335
5369
|
const run = (0, import_node_child_process.spawnSync)(cmd, [...pre, ...args], { cwd, encoding: "utf8", maxBuffer: 64 * 1024 * 1024, env: { ...process.env, WRANGLER_SEND_METRICS: "false" } });
|
|
5336
|
-
const
|
|
5370
|
+
const unstarted = spawnFailure([cmd, ...pre], run.error);
|
|
5371
|
+
const out = unstarted ?? `${run.stdout || ""}${run.stderr || ""}`;
|
|
5337
5372
|
if (!opts.quiet) process.stdout.write(out.split("\n").map((l) => l ? ` \u2502 ${l}` : l).join("\n") + "\n");
|
|
5338
|
-
return { status: run.status ?? 1, out };
|
|
5373
|
+
return { status: run.status ?? 1, out, started: !unstarted };
|
|
5339
5374
|
}
|
|
5340
5375
|
async function waitFor(url, seconds) {
|
|
5341
5376
|
const until = Date.now() + seconds * 1e3;
|
|
@@ -5396,8 +5431,8 @@ async function applyEverything(base, token, project, functions, makeRoles, where
|
|
|
5396
5431
|
const target = where.local ? "local" : "live";
|
|
5397
5432
|
const automations = await applyAutomations(call, project.automationsText, { installedSha256: where.record.automations?.[target] ?? null });
|
|
5398
5433
|
if (automations.before) {
|
|
5399
|
-
const kept =
|
|
5400
|
-
|
|
5434
|
+
const kept = path4.join(where.dir, `automations.before-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-")}.json`);
|
|
5435
|
+
fs5.writeFileSync(kept, JSON.stringify(automations.before, null, 2) + "\n");
|
|
5401
5436
|
automations.lines.push(`The backend's previous ones are kept in ${kept}.`);
|
|
5402
5437
|
}
|
|
5403
5438
|
for (const l of automations.lines) say(` ${l}`);
|
|
@@ -5450,10 +5485,10 @@ async function deploy(o) {
|
|
|
5450
5485
|
const name2 = o.name || (o.out ? readRecord(o.out)?.name : void 0) || workerNameFrom(project.name);
|
|
5451
5486
|
const problem = workerNameProblem(name2);
|
|
5452
5487
|
if (problem) throw new DeployError(problem);
|
|
5453
|
-
const dir = o.out ||
|
|
5454
|
-
|
|
5488
|
+
const dir = o.out || path4.join(root, name2);
|
|
5489
|
+
fs5.mkdirSync(dir, { recursive: true });
|
|
5455
5490
|
const before = readRecord(dir);
|
|
5456
|
-
if (before &&
|
|
5491
|
+
if (before && path4.resolve(before.project) !== path4.resolve(project.dir)) {
|
|
5457
5492
|
throw new DeployError(`${name2} was deployed from ${before.project}, not ${project.dir}. Choose another --name.`);
|
|
5458
5493
|
}
|
|
5459
5494
|
const firstDeploy = !before || !before.deploys.some((d) => d.local === !!o.local);
|
|
@@ -5488,16 +5523,16 @@ ${functions.parts.failures.map((f) => ` ${f.name}: ${f.reason}`).join("\n")}`);
|
|
|
5488
5523
|
if (refusals.length) throw new DeployError(refusals.map(refusalSentence).join("\n"));
|
|
5489
5524
|
say(` ${functions.parts.shipped.length} function(s), ${print.length} code text(s) printed.`);
|
|
5490
5525
|
say("2. The site");
|
|
5491
|
-
const siteDir =
|
|
5526
|
+
const siteDir = path4.join(dir, "site");
|
|
5492
5527
|
for (const n of buildSite(project, name2, siteDir, o)) say(` ${n}`);
|
|
5493
5528
|
say("3. The Worker");
|
|
5494
|
-
const distDir =
|
|
5495
|
-
|
|
5529
|
+
const distDir = path4.join(dir, "dist");
|
|
5530
|
+
fs5.rmSync(distDir, { recursive: true, force: true });
|
|
5496
5531
|
const how = await buildWorker(distDir, { appId: name2, appName: project.name, policy: project.policyText, automations: project.automationsText }, printCodeModule(print));
|
|
5497
5532
|
if (how === "prebuilt") say(" the backend: the prebuilt Worker (nothing bundled on this computer); the app's code beside it");
|
|
5498
|
-
|
|
5533
|
+
fs5.writeFileSync(path4.join(dir, "wrangler.toml"), wranglerConfig({ name: name2, bucket: record.bucket, domain: o.local ? null : record.domain, alert: record.alert }));
|
|
5499
5534
|
const uploaded = [...filesUnder2(siteDir), ...filesUnder2(distDir)];
|
|
5500
|
-
const mb = uploaded.reduce((n, f) => n +
|
|
5535
|
+
const mb = uploaded.reduce((n, f) => n + fs5.statSync(f).size, 0) / 1e6;
|
|
5501
5536
|
say(` ${uploaded.length} files to upload, ${mb.toFixed(2)} MB; files in ${record.bucket ? `the bucket ${record.bucket}` : "NO bucket (uploads are lost on a restart)"}.`);
|
|
5502
5537
|
say("4. Nothing private in what is uploaded");
|
|
5503
5538
|
const found = leaks(uploaded, [{ what: "the admin credential", value: record.adminToken }, ...project.envValues.map((v, i) => ({ what: `.env value ${i + 1}`, value: v }))]);
|
|
@@ -5511,26 +5546,28 @@ ${functions.parts.failures.map((f) => ` ${f.name}: ${f.reason}`).join("\n")}`);
|
|
|
5511
5546
|
if (o.local) {
|
|
5512
5547
|
say("5. wrangler dev");
|
|
5513
5548
|
const port = o.port || 8787;
|
|
5514
|
-
|
|
5549
|
+
fs5.writeFileSync(path4.join(dir, ".dev.vars"), `NODEGX_ADMIN_TOKEN=${record.adminToken}
|
|
5515
5550
|
`, { mode: 384 });
|
|
5516
5551
|
const [cmd, ...pre] = wranglerCommand();
|
|
5517
|
-
const child = (0, import_node_child_process.spawn)(cmd, [...pre, "dev", "--config", "wrangler.toml", "--port", String(port), "--ip", "127.0.0.1", "--persist-to",
|
|
5552
|
+
const child = (0, import_node_child_process.spawn)(cmd, [...pre, "dev", "--config", "wrangler.toml", "--port", String(port), "--ip", "127.0.0.1", "--persist-to", path4.join(dir, ".state")], {
|
|
5518
5553
|
cwd: dir,
|
|
5519
5554
|
stdio: ["ignore", "pipe", "pipe"],
|
|
5520
5555
|
env: { ...process.env, WRANGLER_SEND_METRICS: "false" }
|
|
5521
5556
|
});
|
|
5522
|
-
const log =
|
|
5557
|
+
const log = fs5.createWriteStream(path4.join(dir, "wrangler-dev.log"));
|
|
5523
5558
|
child.stdout.pipe(log);
|
|
5524
5559
|
child.stderr.pipe(log);
|
|
5560
|
+
const unstarted = new Promise((_, reject) => child.on("error", (e) => reject(new DeployError(spawnFailure([cmd, ...pre], e) ?? String(e)))));
|
|
5561
|
+
unstarted.catch(() => void 0);
|
|
5525
5562
|
const base = `http://127.0.0.1:${port}`;
|
|
5526
|
-
await waitFor(`${base}/health`, 90);
|
|
5563
|
+
await Promise.race([waitFor(`${base}/health`, 90), unstarted]);
|
|
5527
5564
|
say(`6. The project onto the backend at ${base}`);
|
|
5528
5565
|
await applyEverything(base, record.adminToken, project, functions, firstDeploy || !!o.makeRoles, { dir, record, local: true });
|
|
5529
5566
|
record.deploys.push({ at: (/* @__PURE__ */ new Date()).toISOString(), url: base, local: true });
|
|
5530
5567
|
writeRecord(dir, record);
|
|
5531
5568
|
await testAlert(base, record, dir, true);
|
|
5532
5569
|
say(`
|
|
5533
|
-
Running on workerd at ${base} (log: ${
|
|
5570
|
+
Running on workerd at ${base} (log: ${path4.join(dir, "wrangler-dev.log")}). Ctrl-C stops it; its data stays in ${path4.join(dir, ".state")}.`);
|
|
5534
5571
|
await new Promise((resolve2) => child.on("exit", () => resolve2()));
|
|
5535
5572
|
return 0;
|
|
5536
5573
|
}
|
|
@@ -5585,15 +5622,16 @@ Online: ${url}`);
|
|
|
5585
5622
|
);
|
|
5586
5623
|
}
|
|
5587
5624
|
await testAlert(url, record, dir);
|
|
5588
|
-
say(`The backend's dashboard: ${url}/_admin \u2014 its admin credential is in ${
|
|
5625
|
+
say(`The backend's dashboard: ${url}/_admin \u2014 its admin credential is in ${path4.join(dir, "deployment.json")} (never uploaded).`);
|
|
5589
5626
|
return 0;
|
|
5590
5627
|
}
|
|
5591
5628
|
function wranglerDeploy(dir, record, firstDeploy, mint) {
|
|
5592
5629
|
if (record.bucket && firstDeploy) {
|
|
5593
5630
|
const made = wrangler(["r2", "bucket", "create", record.bucket], dir, { quiet: true });
|
|
5594
5631
|
if (made.status !== 0 && !/already exists|already own/i.test(made.out)) {
|
|
5632
|
+
if (!made.started) throw new DeployError(made.out);
|
|
5595
5633
|
throw new DeployError(`The bucket ${record.bucket} could not be made:
|
|
5596
|
-
${made.out.trim()}
|
|
5634
|
+
${made.out.trim() || `(wrangler exited ${made.status} and said nothing)`}
|
|
5597
5635
|
(R2 must be enabled on the account; --no-bucket deploys without one.)`);
|
|
5598
5636
|
}
|
|
5599
5637
|
say(` bucket ${record.bucket}: ${made.status === 0 ? "made" : "already there"}`);
|
|
@@ -5601,16 +5639,17 @@ ${made.out.trim()}
|
|
|
5601
5639
|
const deployArgs = ["deploy", "--config", "wrangler.toml"];
|
|
5602
5640
|
let secretsFile = null;
|
|
5603
5641
|
if (mint) {
|
|
5604
|
-
secretsFile =
|
|
5605
|
-
|
|
5642
|
+
secretsFile = path4.join(fs5.mkdtempSync(path4.join(os2.tmpdir(), "nodegx-cf-")), "secrets.json");
|
|
5643
|
+
fs5.writeFileSync(secretsFile, JSON.stringify({ NODEGX_ADMIN_TOKEN: record.adminToken }), { mode: 384 });
|
|
5606
5644
|
deployArgs.push("--secrets-file", secretsFile);
|
|
5607
5645
|
}
|
|
5608
5646
|
let done;
|
|
5609
5647
|
try {
|
|
5610
5648
|
done = wrangler(deployArgs, dir);
|
|
5611
5649
|
} finally {
|
|
5612
|
-
if (secretsFile)
|
|
5650
|
+
if (secretsFile) fs5.rmSync(path4.dirname(secretsFile), { recursive: true, force: true });
|
|
5613
5651
|
}
|
|
5652
|
+
if (!done.started) throw new DeployError(`${done.out} Nothing on the backend was changed.`);
|
|
5614
5653
|
if (done.status !== 0) throw new DeployError("wrangler deploy failed (above). Nothing on the backend was changed.");
|
|
5615
5654
|
const url = /https:\/\/[a-z0-9.-]+\.workers\.dev/.exec(done.out)?.[0] ?? record.url;
|
|
5616
5655
|
if (!url) throw new DeployError("wrangler deployed, but did not say the address: deploy again to finish.");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nodegx/backend",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Put a NodeGX app online on your own Cloudflare account: its pages, its backend, its rules and its functions, on one Worker, the same address every time.",
|
|
5
5
|
"license": "GPL-3.0-only",
|
|
6
6
|
"homepage": "https://nodegx.io",
|