@surph_ai/cli 0.0.32 → 0.0.34
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/board/deploy.js +232 -0
- package/board/server.js +179 -0
- package/cli.js +28 -0
- package/package.json +1 -1
- package/scaffold/board/README.md +23 -6
- package/scaffold/board/board.json +0 -1
- package/scaffold/board/package.json +3 -5
package/board/deploy.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Deploy a board to Surph (`npm run deploy` → `surph-board-deploy`), run from
|
|
3
|
+
// the board's folder. Built up step by step; so far:
|
|
4
|
+
//
|
|
5
|
+
// 1. Zip the board's site: every file in the folder except the project
|
|
6
|
+
// files (package.json, package-lock.json, README.md, board.json,
|
|
7
|
+
// node_modules), dot-files (.env, .git, .DS_Store) and the zip itself.
|
|
8
|
+
// Written to site.zip in the board's folder.
|
|
9
|
+
// 2. Ask Surph for an upload address for it: boards/<slug>/site/site.zip on
|
|
10
|
+
// S3, the slug from board.json.
|
|
11
|
+
// 3. Upload site.zip there, then delete the local site.zip (it's kept when
|
|
12
|
+
// the upload fails, to look at).
|
|
13
|
+
// 4. Unzip it on S3 into boards/<slug>/site/ (the board's pages and assets).
|
|
14
|
+
// 5. Upload board.json to boards/<slug>/board.json — last, so the live
|
|
15
|
+
// board's definition only changes once its site is up.
|
|
16
|
+
//
|
|
17
|
+
// Node's built-ins only (zlib for compression, fetch for the upload; Node 18+).
|
|
18
|
+
|
|
19
|
+
const fs = require('fs');
|
|
20
|
+
const path = require('path');
|
|
21
|
+
const zlib = require('zlib');
|
|
22
|
+
|
|
23
|
+
const ROOT = process.cwd();
|
|
24
|
+
const ZIP_NAME = 'site.zip';
|
|
25
|
+
const RPC_URL = 'https://rpc.surph.ai';
|
|
26
|
+
// Unzips an uploaded archive in place on S3 (the `sync` action).
|
|
27
|
+
const SYNC_URL = 'https://e3lunom2hka3u6iiotrlzrcez40uwdnk.lambda-url.us-east-1.on.aws/';
|
|
28
|
+
const SLUG_RE = /^[a-z0-9][a-z0-9-]*$/;
|
|
29
|
+
|
|
30
|
+
// Left out at the board's top level: the project's own files.
|
|
31
|
+
const SKIP_AT_ROOT = new Set(['package.json', 'package-lock.json', 'README.md', 'board.json', ZIP_NAME]);
|
|
32
|
+
|
|
33
|
+
// The files to deploy, as paths relative to the board's folder ('/'-separated).
|
|
34
|
+
const siteFiles = (dir = ROOT, rel = '') => {
|
|
35
|
+
const out = [];
|
|
36
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
37
|
+
const name = entry.name;
|
|
38
|
+
if (name.startsWith('.') || name === 'node_modules') continue;
|
|
39
|
+
if (!rel && SKIP_AT_ROOT.has(name)) continue;
|
|
40
|
+
const relPath = rel ? `${rel}/${name}` : name;
|
|
41
|
+
const full = path.join(dir, name);
|
|
42
|
+
if (entry.isDirectory()) out.push(...siteFiles(full, relPath));
|
|
43
|
+
else if (entry.isFile()) out.push(relPath);
|
|
44
|
+
}
|
|
45
|
+
return out.sort();
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// CRC-32 (the zip format's checksum).
|
|
49
|
+
const CRC_TABLE = (() => {
|
|
50
|
+
const table = new Uint32Array(256);
|
|
51
|
+
for (let n = 0; n < 256; n++) {
|
|
52
|
+
let c = n;
|
|
53
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
54
|
+
table[n] = c >>> 0;
|
|
55
|
+
}
|
|
56
|
+
return table;
|
|
57
|
+
})();
|
|
58
|
+
const crc32 = (buf) => {
|
|
59
|
+
let c = 0xffffffff;
|
|
60
|
+
for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
|
|
61
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// A file's time in the zip format's (MS-DOS) date and time fields.
|
|
65
|
+
const dosTime = (date) => ({
|
|
66
|
+
time: (date.getHours() << 11) | (date.getMinutes() << 5) | Math.floor(date.getSeconds() / 2),
|
|
67
|
+
date: ((date.getFullYear() - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate(),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// A zip archive of the given files (deflated, or stored when that's smaller).
|
|
71
|
+
const zipFiles = (files) => {
|
|
72
|
+
const parts = [];
|
|
73
|
+
const central = [];
|
|
74
|
+
let offset = 0;
|
|
75
|
+
for (const rel of files) {
|
|
76
|
+
const full = path.join(ROOT, ...rel.split('/'));
|
|
77
|
+
const data = fs.readFileSync(full);
|
|
78
|
+
const deflated = zlib.deflateRawSync(data, { level: 9 });
|
|
79
|
+
const stored = deflated.length >= data.length;
|
|
80
|
+
const body = stored ? data : deflated;
|
|
81
|
+
const method = stored ? 0 : 8;
|
|
82
|
+
const crc = crc32(data);
|
|
83
|
+
const { time, date } = dosTime(fs.statSync(full).mtime);
|
|
84
|
+
const name = Buffer.from(rel, 'utf8');
|
|
85
|
+
|
|
86
|
+
const local = Buffer.alloc(30);
|
|
87
|
+
local.writeUInt32LE(0x04034b50, 0);
|
|
88
|
+
local.writeUInt16LE(20, 4); // version needed
|
|
89
|
+
local.writeUInt16LE(0x0800, 6); // UTF-8 names
|
|
90
|
+
local.writeUInt16LE(method, 8);
|
|
91
|
+
local.writeUInt16LE(time, 10);
|
|
92
|
+
local.writeUInt16LE(date, 12);
|
|
93
|
+
local.writeUInt32LE(crc, 14);
|
|
94
|
+
local.writeUInt32LE(body.length, 18);
|
|
95
|
+
local.writeUInt32LE(data.length, 22);
|
|
96
|
+
local.writeUInt16LE(name.length, 26);
|
|
97
|
+
local.writeUInt16LE(0, 28);
|
|
98
|
+
parts.push(local, name, body);
|
|
99
|
+
|
|
100
|
+
const entry = Buffer.alloc(46);
|
|
101
|
+
entry.writeUInt32LE(0x02014b50, 0);
|
|
102
|
+
entry.writeUInt16LE(20, 4); // version made by
|
|
103
|
+
entry.writeUInt16LE(20, 6); // version needed
|
|
104
|
+
entry.writeUInt16LE(0x0800, 8);
|
|
105
|
+
entry.writeUInt16LE(method, 10);
|
|
106
|
+
entry.writeUInt16LE(time, 12);
|
|
107
|
+
entry.writeUInt16LE(date, 14);
|
|
108
|
+
entry.writeUInt32LE(crc, 16);
|
|
109
|
+
entry.writeUInt32LE(body.length, 20);
|
|
110
|
+
entry.writeUInt32LE(data.length, 24);
|
|
111
|
+
entry.writeUInt16LE(name.length, 28);
|
|
112
|
+
entry.writeUInt32LE(offset, 42);
|
|
113
|
+
central.push(entry, name);
|
|
114
|
+
|
|
115
|
+
offset += local.length + name.length + body.length;
|
|
116
|
+
}
|
|
117
|
+
const centralBuf = Buffer.concat(central);
|
|
118
|
+
const end = Buffer.alloc(22);
|
|
119
|
+
end.writeUInt32LE(0x06054b50, 0);
|
|
120
|
+
end.writeUInt16LE(files.length, 8);
|
|
121
|
+
end.writeUInt16LE(files.length, 10);
|
|
122
|
+
end.writeUInt32LE(centralBuf.length, 12);
|
|
123
|
+
end.writeUInt32LE(offset, 16);
|
|
124
|
+
return Buffer.concat([...parts, centralBuf, end]);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const fail = (message) => {
|
|
128
|
+
console.error(message);
|
|
129
|
+
process.exit(1);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// The board's slug, from its board.json.
|
|
133
|
+
const boardSlug = () => {
|
|
134
|
+
let board;
|
|
135
|
+
try {
|
|
136
|
+
board = JSON.parse(fs.readFileSync(path.join(ROOT, 'board.json'), 'utf8'));
|
|
137
|
+
} catch (error) {
|
|
138
|
+
fail(`board.json isn't valid JSON: ${error.message}`);
|
|
139
|
+
}
|
|
140
|
+
const slug = typeof board?.slug === 'string' ? board.slug.trim() : '';
|
|
141
|
+
if (!SLUG_RE.test(slug)) fail('board.json needs a "slug": lowercase letters, digits and dashes (e.g. "my-board").');
|
|
142
|
+
return slug;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// A presigned S3 address to PUT a file to, under boards/<slug>/ (good for an hour).
|
|
146
|
+
const uploadUrlFor = async (slug, filename, filetype) => {
|
|
147
|
+
const res = await fetch(`${RPC_URL}/s3/upload_url`, {
|
|
148
|
+
method: 'POST',
|
|
149
|
+
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'x-surph-client': 'surph-sdk' },
|
|
150
|
+
body: JSON.stringify({
|
|
151
|
+
folder: slug,
|
|
152
|
+
filename,
|
|
153
|
+
filetype,
|
|
154
|
+
bucket: 'surph',
|
|
155
|
+
toStatic: 'false',
|
|
156
|
+
collection: 'boards',
|
|
157
|
+
}),
|
|
158
|
+
});
|
|
159
|
+
const json = await res.json().catch(() => null);
|
|
160
|
+
const uploadUrl = json?.response?.uploadUrl;
|
|
161
|
+
if (!res.ok || !uploadUrl) fail(`Couldn't get an upload address for ${filename}: ${json?.response?.error || `HTTP ${res.status}`}`);
|
|
162
|
+
return uploadUrl;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// PUT a file to its presigned address. S3 answers 200; anything else is an
|
|
166
|
+
// XML error, whose code and message say why.
|
|
167
|
+
const upload = async (uploadUrl, body, filetype, filename) => {
|
|
168
|
+
const res = await fetch(uploadUrl, {
|
|
169
|
+
method: 'PUT',
|
|
170
|
+
headers: { 'Content-Type': filetype },
|
|
171
|
+
body,
|
|
172
|
+
});
|
|
173
|
+
if (!res.ok) {
|
|
174
|
+
const text = await res.text().catch(() => '');
|
|
175
|
+
const code = (text.match(/<Code>([^<]+)<\/Code>/) || [])[1] || `HTTP ${res.status}`;
|
|
176
|
+
const message = (text.match(/<Message>([^<]+)<\/Message>/) || [])[1] || '';
|
|
177
|
+
fail(`Upload of ${filename} failed: ${code}${message ? ` — ${message}` : ''}`);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// Unzip boards/<slug>/site/site.zip on S3 into boards/<slug>/site/.
|
|
182
|
+
const unzipOnS3 = async (slug) => {
|
|
183
|
+
const res = await fetch(SYNC_URL, {
|
|
184
|
+
method: 'POST',
|
|
185
|
+
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'Turbo-Client': 'portal' },
|
|
186
|
+
body: JSON.stringify({
|
|
187
|
+
action: 'sync',
|
|
188
|
+
args: { project: slug, file: ZIP_NAME, bucket: 'surph', folder: 'site' },
|
|
189
|
+
}),
|
|
190
|
+
});
|
|
191
|
+
const text = await res.text().catch(() => '');
|
|
192
|
+
let json = null;
|
|
193
|
+
try { json = JSON.parse(text); } catch { /* not JSON */ }
|
|
194
|
+
const error = json?.error || json?.response?.error || json?.data?.error;
|
|
195
|
+
if (!res.ok || error) {
|
|
196
|
+
fail(`Unzipping ${ZIP_NAME} on S3 failed: ${typeof error === 'string' ? error : (error?.message || text.slice(0, 300) || `HTTP ${res.status}`)}`);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const main = async () => {
|
|
201
|
+
if (!fs.existsSync(path.join(ROOT, 'board.json'))) {
|
|
202
|
+
fail(`No board.json in ${ROOT}. Run this from the board's folder.`);
|
|
203
|
+
}
|
|
204
|
+
const slug = boardSlug();
|
|
205
|
+
const files = siteFiles();
|
|
206
|
+
if (!files.length) fail('Nothing to deploy: the board has no site files.');
|
|
207
|
+
|
|
208
|
+
const zip = zipFiles(files);
|
|
209
|
+
fs.writeFileSync(path.join(ROOT, ZIP_NAME), zip);
|
|
210
|
+
console.log(`Zipped ${files.length} file${files.length === 1 ? '' : 's'} into ${ZIP_NAME} (${(zip.length / 1024).toFixed(1)} KB):`);
|
|
211
|
+
for (const file of files) console.log(` ${file}`);
|
|
212
|
+
|
|
213
|
+
console.log(`\nDeploying the board "${slug}"…`);
|
|
214
|
+
const zipUrl = await uploadUrlFor(slug, `site/${ZIP_NAME}`, 'application/zip');
|
|
215
|
+
await upload(zipUrl, zip, 'application/zip', ZIP_NAME);
|
|
216
|
+
console.log(`Uploaded: ${zipUrl.split('?')[0]}`);
|
|
217
|
+
fs.rmSync(path.join(ROOT, ZIP_NAME), { force: true });
|
|
218
|
+
|
|
219
|
+
await unzipOnS3(slug);
|
|
220
|
+
console.log(`Unzipped into: https://surph.s3.us-east-1.amazonaws.com/boards/${slug}/site/`);
|
|
221
|
+
|
|
222
|
+
const boardUrl = await uploadUrlFor(slug, 'board.json', 'application/json');
|
|
223
|
+
await upload(boardUrl, fs.readFileSync(path.join(ROOT, 'board.json')), 'application/json', 'board.json');
|
|
224
|
+
console.log(`Uploaded: ${boardUrl.split('?')[0]}`);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const run = () => main().catch((error) => fail(`Deploy failed: ${error.message}`));
|
|
228
|
+
|
|
229
|
+
// `surph board-deploy` calls run(); running this file directly runs it too.
|
|
230
|
+
if (require.main === module) run();
|
|
231
|
+
|
|
232
|
+
module.exports = { run };
|
package/board/server.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The board's local dev server (`npm run dev` → `surph-board-dev`): serves
|
|
3
|
+
// the board folder it's run from (the current directory) at
|
|
4
|
+
// http://localhost:4180 for Surph's dev mode (Settings → Developer → Run
|
|
5
|
+
// Surph as a local board). Node's built-ins only.
|
|
6
|
+
//
|
|
7
|
+
// GET /<file> This folder's files, never cached, with CORS so
|
|
8
|
+
// Surph (another address) can read board.json.
|
|
9
|
+
// GET /__surph/assets The image files in assets/ (the logo picker).
|
|
10
|
+
// PUT /board.json Surph's Local board panel saving the board. Only
|
|
11
|
+
// from Surph's own addresses, only this one file,
|
|
12
|
+
// only a JSON object with a config.pages list.
|
|
13
|
+
//
|
|
14
|
+
// It listens on this machine only (127.0.0.1 and ::1), never the network.
|
|
15
|
+
|
|
16
|
+
const http = require('http');
|
|
17
|
+
const fs = require('fs');
|
|
18
|
+
const path = require('path');
|
|
19
|
+
|
|
20
|
+
const PORT = Number(process.env.PORT) || 4180;
|
|
21
|
+
// The board's folder: where `npm run dev` runs it, not this package's folder
|
|
22
|
+
// (it's installed under node_modules/@surph_ai/sdk).
|
|
23
|
+
const ROOT = process.cwd();
|
|
24
|
+
const MAX_BOARD_BYTES = 256 * 1024;
|
|
25
|
+
|
|
26
|
+
// The addresses whose pages may save board.json.
|
|
27
|
+
const SAVE_ORIGINS = [
|
|
28
|
+
/^https:\/\/([a-z0-9-]+\.)?surph\.ai$/,
|
|
29
|
+
/^http:\/\/localhost:\d+$/,
|
|
30
|
+
/^http:\/\/127\.0\.0\.1:\d+$/,
|
|
31
|
+
];
|
|
32
|
+
const canSave = (origin) => typeof origin === 'string' && SAVE_ORIGINS.some((re) => re.test(origin));
|
|
33
|
+
|
|
34
|
+
const TYPES = {
|
|
35
|
+
'.html': 'text/html; charset=utf-8',
|
|
36
|
+
'.css': 'text/css; charset=utf-8',
|
|
37
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
38
|
+
'.json': 'application/json; charset=utf-8',
|
|
39
|
+
'.svg': 'image/svg+xml',
|
|
40
|
+
'.png': 'image/png',
|
|
41
|
+
'.jpg': 'image/jpeg',
|
|
42
|
+
'.jpeg': 'image/jpeg',
|
|
43
|
+
'.gif': 'image/gif',
|
|
44
|
+
'.webp': 'image/webp',
|
|
45
|
+
'.ico': 'image/x-icon',
|
|
46
|
+
'.txt': 'text/plain; charset=utf-8',
|
|
47
|
+
'.md': 'text/markdown; charset=utf-8',
|
|
48
|
+
};
|
|
49
|
+
const IMAGE = /\.(png|jpe?g|gif|webp|svg)$/i;
|
|
50
|
+
|
|
51
|
+
const send = (res, status, body, headers = {}) => {
|
|
52
|
+
res.writeHead(status, { 'Cache-Control': 'no-store', ...headers });
|
|
53
|
+
res.end(body);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const cors = (req) => {
|
|
57
|
+
const origin = req.headers.origin;
|
|
58
|
+
const headers = { 'Access-Control-Allow-Origin': '*' };
|
|
59
|
+
// A save is only allowed from Surph; the browser asks first (preflight).
|
|
60
|
+
// Chrome asks before a public site (surph.ai) reaches a local address.
|
|
61
|
+
if (req.headers['access-control-request-private-network'] === 'true') {
|
|
62
|
+
headers['Access-Control-Allow-Private-Network'] = 'true';
|
|
63
|
+
}
|
|
64
|
+
if (canSave(origin)) {
|
|
65
|
+
headers['Access-Control-Allow-Origin'] = origin;
|
|
66
|
+
headers['Access-Control-Allow-Methods'] = 'GET, PUT, OPTIONS';
|
|
67
|
+
headers['Access-Control-Allow-Headers'] = 'Content-Type';
|
|
68
|
+
headers.Vary = 'Origin';
|
|
69
|
+
}
|
|
70
|
+
return headers;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// A path inside this folder, or null (no way out of it, no dot-files).
|
|
74
|
+
const inside = (urlPath) => {
|
|
75
|
+
let rel;
|
|
76
|
+
try { rel = decodeURIComponent(urlPath.split('?')[0]); } catch { return null; }
|
|
77
|
+
if (rel.endsWith('/')) rel += 'index.html';
|
|
78
|
+
const full = path.normalize(path.join(ROOT, rel));
|
|
79
|
+
if (full !== ROOT && !full.startsWith(ROOT + path.sep)) return null;
|
|
80
|
+
if (path.relative(ROOT, full).split(path.sep).some((part) => part.startsWith('.') || part === 'node_modules')) return null;
|
|
81
|
+
return full;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const readBody = (req) => new Promise((resolve, reject) => {
|
|
85
|
+
let size = 0;
|
|
86
|
+
const chunks = [];
|
|
87
|
+
req.on('data', (chunk) => {
|
|
88
|
+
size += chunk.length;
|
|
89
|
+
if (size > MAX_BOARD_BYTES) {
|
|
90
|
+
reject(new Error('board.json is too large'));
|
|
91
|
+
req.destroy();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
chunks.push(chunk);
|
|
95
|
+
});
|
|
96
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
97
|
+
req.on('error', reject);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const saveBoard = async (req, res, headers) => {
|
|
101
|
+
if (!canSave(req.headers.origin)) {
|
|
102
|
+
send(res, 403, JSON.stringify({ error: 'board.json can only be saved from Surph.' }), { ...headers, 'Content-Type': 'application/json' });
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
let board;
|
|
106
|
+
try {
|
|
107
|
+
board = JSON.parse(await readBody(req));
|
|
108
|
+
} catch (error) {
|
|
109
|
+
send(res, 400, JSON.stringify({ error: error.message || 'Not JSON.' }), { ...headers, 'Content-Type': 'application/json' });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (!board || typeof board !== 'object' || Array.isArray(board) || !Array.isArray(board.config && board.config.pages)) {
|
|
113
|
+
send(res, 400, JSON.stringify({ error: 'board.json must be an object with a config.pages list.' }), { ...headers, 'Content-Type': 'application/json' });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
// Written via a temporary file, so a failed write never leaves half a file.
|
|
117
|
+
const file = path.join(ROOT, 'board.json');
|
|
118
|
+
const tmp = `${file}.saving`;
|
|
119
|
+
fs.writeFileSync(tmp, `${JSON.stringify(board, null, 2)}\n`);
|
|
120
|
+
fs.renameSync(tmp, file);
|
|
121
|
+
console.log(`Saved board.json (${new Date().toLocaleTimeString()})`);
|
|
122
|
+
send(res, 200, JSON.stringify({ ok: true }), { ...headers, 'Content-Type': 'application/json' });
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const handle = async (req, res) => {
|
|
126
|
+
const headers = cors(req);
|
|
127
|
+
const urlPath = req.url || '/';
|
|
128
|
+
try {
|
|
129
|
+
if (req.method === 'OPTIONS') {
|
|
130
|
+
send(res, 204, '', headers);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (req.method === 'PUT' && urlPath.split('?')[0] === '/board.json') {
|
|
134
|
+
await saveBoard(req, res, headers);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
138
|
+
send(res, 405, 'Method not allowed', headers);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (urlPath.split('?')[0] === '/__surph/assets') {
|
|
142
|
+
const dir = path.join(ROOT, 'assets');
|
|
143
|
+
const files = fs.existsSync(dir) ? fs.readdirSync(dir).filter((name) => IMAGE.test(name)).sort() : [];
|
|
144
|
+
send(res, 200, JSON.stringify({ images: files }), { ...headers, 'Content-Type': 'application/json' });
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const full = inside(urlPath);
|
|
148
|
+
if (!full || !fs.existsSync(full) || !fs.statSync(full).isFile()) {
|
|
149
|
+
send(res, 404, 'Not found', headers);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const type = TYPES[path.extname(full).toLowerCase()] || 'application/octet-stream';
|
|
153
|
+
send(res, 200, req.method === 'HEAD' ? '' : fs.readFileSync(full), { ...headers, 'Content-Type': type });
|
|
154
|
+
} catch (error) {
|
|
155
|
+
send(res, 500, String(error && error.message || error), headers);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// Start serving the current folder. localhost may be reached over IPv4 or
|
|
160
|
+
// IPv6: listen on both loopbacks.
|
|
161
|
+
const start = () => {
|
|
162
|
+
http.createServer(handle)
|
|
163
|
+
.on('error', (error) => {
|
|
164
|
+
console.error(error.code === 'EADDRINUSE'
|
|
165
|
+
? `Port ${PORT} is already in use: another board server may be running. Stop it, or set PORT to use another port.`
|
|
166
|
+
: `Board dev server failed: ${error.message}`);
|
|
167
|
+
process.exit(1);
|
|
168
|
+
})
|
|
169
|
+
.listen(PORT, '127.0.0.1', () => {
|
|
170
|
+
console.log(`Board dev server: http://localhost:${PORT} (serving ${ROOT})`);
|
|
171
|
+
console.log('In Surph: Settings → Developer → Run Surph as a local board.');
|
|
172
|
+
});
|
|
173
|
+
http.createServer(handle).listen(PORT, '::1').on('error', () => { /* no IPv6 loopback */ });
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// `surph board-server` calls start(); running this file directly starts it too.
|
|
177
|
+
if (require.main === module) start();
|
|
178
|
+
|
|
179
|
+
module.exports = { start };
|
package/cli.js
CHANGED
|
@@ -20,6 +20,8 @@ const AdmZip = require('adm-zip')
|
|
|
20
20
|
// const auth = require('./auth')
|
|
21
21
|
const deploy = require('./deploy')
|
|
22
22
|
const scaffold = require('./scaffold')
|
|
23
|
+
const boardDeploy = require('./board/deploy')
|
|
24
|
+
const boardServer = require('./board/server')
|
|
23
25
|
const { ensureChat, chatNodePath } = require('./toolserver/ensure-chat')
|
|
24
26
|
const utils = require('./utils')
|
|
25
27
|
const { openBrowser, openBrowserWhenReady } = require('./utils/browser')
|
|
@@ -297,6 +299,32 @@ program.command('board <name>')
|
|
|
297
299
|
.description('create new Surph board')
|
|
298
300
|
.action(name => scaffold(name, 'board'))
|
|
299
301
|
|
|
302
|
+
program.command('board-deploy')
|
|
303
|
+
.description('Deploy board to Surph')
|
|
304
|
+
.action(async (cmd, options) => {
|
|
305
|
+
try {
|
|
306
|
+
// From the board's folder: zip its site, upload and unzip it, then
|
|
307
|
+
// upload board.json (board/deploy.js).
|
|
308
|
+
await boardDeploy.run()
|
|
309
|
+
} catch (error) {
|
|
310
|
+
utils.printError(error)
|
|
311
|
+
program.error(`Unexpected error: ${error}`, ERRORS.UNSPECIFIED)
|
|
312
|
+
}
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
program.command('board-server')
|
|
316
|
+
.description('local board dev server')
|
|
317
|
+
.action(async (cmd, options) => {
|
|
318
|
+
try {
|
|
319
|
+
// Serves the board's folder (the current directory) at
|
|
320
|
+
// http://localhost:4180 for Surph's dev mode (board/server.js).
|
|
321
|
+
boardServer.start()
|
|
322
|
+
} catch (error) {
|
|
323
|
+
utils.printError(error)
|
|
324
|
+
program.error(`Unexpected error: ${error}`, ERRORS.UNSPECIFIED)
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
|
|
300
328
|
program.command('manifest')
|
|
301
329
|
.description('update manifest for Surph tool')
|
|
302
330
|
.action(async (cmd, options) => {
|
package/package.json
CHANGED
package/scaffold/board/README.md
CHANGED
|
@@ -22,7 +22,7 @@ assets/
|
|
|
22
22
|
css/site.css Styles shared by every page in the board
|
|
23
23
|
js/ Scripts shared by the board's pages (optional)
|
|
24
24
|
board.json The board itself: name, contexts, instructions, and its pages (see "Registering pages")
|
|
25
|
-
package.json `npm run dev` serves this folder
|
|
25
|
+
package.json `npm run dev` serves this folder locally; `npm run deploy` publishes the board (see "Deploying")
|
|
26
26
|
README.md This file
|
|
27
27
|
```
|
|
28
28
|
|
|
@@ -46,7 +46,7 @@ Every page links it in `<head>`, before its own `<style>`:
|
|
|
46
46
|
|
|
47
47
|
Surph only shows pages that are registered with the board. The board itself is described by **`board.json`** in this folder, shaped like the board's record in Surph; its pages are `config.pages`, one entry per page. Keep it in step with the pages, because it's what Surph reads:
|
|
48
48
|
- **While you develop**, Surph's dev mode runs as the board this file describes, read from your local server (see [Developing locally](#developing-locally)). No live board is needed.
|
|
49
|
-
- **When the board goes live**,
|
|
49
|
+
- **When the board goes live**, `npm run deploy` uploads this same file, and the live board is built from it (see [Deploying](#deploying)). There's no separate record to set up.
|
|
50
50
|
|
|
51
51
|
```json
|
|
52
52
|
{
|
|
@@ -56,7 +56,6 @@ Surph only shows pages that are registered with the board. The board itself is d
|
|
|
56
56
|
"logo": "logo.png",
|
|
57
57
|
"contexts": ["<context slug>"],
|
|
58
58
|
"defaultContext": "<context slug>",
|
|
59
|
-
"waves": [],
|
|
60
59
|
"tools": [],
|
|
61
60
|
"config": {
|
|
62
61
|
"instructions": "How the board's chat should behave: its tone, what it helps with.",
|
|
@@ -75,7 +74,7 @@ Surph only shows pages that are registered with the board. The board itself is d
|
|
|
75
74
|
| `logo` | An image file in `assets/` (just its name, e.g. `logo.png`), shown in Surph's header. |
|
|
76
75
|
| `contexts` | Slugs of the Surph contexts the board draws on. Their items are what `board.get` and the chat's lookups return. They're live Surph contexts; find a context's slug in its address (`/c/<slug>`). |
|
|
77
76
|
| `defaultContext` | The context the chat starts with; else the first in `contexts`. |
|
|
78
|
-
| `
|
|
77
|
+
| `tools` | Slugs of Surph tools the board's chat can use. Optional. |
|
|
79
78
|
| `config.instructions` | Instructions from the board's owner for the board's chat: tone, behavior, what it helps with. On a board they take the place of each context's own instructions. |
|
|
80
79
|
| `config.pages` | The board's pages, one registry entry each (below). |
|
|
81
80
|
|
|
@@ -401,14 +400,31 @@ A field marked "omitted" is missing from the object, not `null`. Every URL is `h
|
|
|
401
400
|
|
|
402
401
|
## Developing locally
|
|
403
402
|
|
|
404
|
-
1.
|
|
403
|
+
1. Run `npm run dev` (nothing to install). It starts the board dev server from the Surph CLI (`npx @surph_ai/cli board-server`), which serves this folder at `http://localhost:4180` with caching off, lets Surph read it from another address, and lets Surph's Local board panel save `board.json`. It only listens on your own machine.
|
|
405
404
|
2. Keep it running while you work; the terminal shows a line each time Surph saves `board.json`.
|
|
406
405
|
3. Open `http://localhost:4180/` to check a page loads without console errors. It shows its empty layout there (`hosted` is false); real data only appears inside the Surph canvas.
|
|
407
406
|
4. **Run Surph as your board with dev mode.** Keep `npm run dev` running. In Surph, open **Settings → Developer**, turn on **Run Surph as a local board**, and keep the address `http://localhost:4180`. Surph reloads as the board `board.json` describes: its name and logo in the header, its contexts and instructions in the chat, and its pages in the canvas (it opens on your start page, the Pages menu lists your pages, and the chat's model sees their `shows` lines). The status under the address says what loaded, or why it couldn't read `board.json`; until it loads, Surph runs as usual. A **DEV** label in the canvas header shows your board is loaded. While it's on, anything the chat saves goes to your own space in Surph, not to a board's shared space.
|
|
408
|
-
5. **Configure the board from Surph.** Click the **DEV** label to open the **Local board** panel: set the board's name, slug, description and logo (an image in `assets/`), pick its contexts (yours, or public ones by search) and which one the chat starts with, pick its tools, and write its instructions. The panel also lists the pages and flags a missing `shows` line or an operation the host doesn't answer. **Save and reload** writes `board.json` through the dev server and reloads Surph as the updated board. Pages and any other fields
|
|
407
|
+
5. **Configure the board from Surph.** Click the **DEV** label to open the **Local board** panel: set the board's name, slug, description and logo (an image in `assets/`), pick its contexts (yours, or public ones by search) and which one the chat starts with, pick its tools, and write its instructions. The panel also lists the pages and flags a missing `shows` line or an operation the host doesn't answer. **Save and reload** writes `board.json` through the dev server and reloads Surph as the updated board. Pages and any other fields are kept as they are in the file; edit pages in `board.json` itself.
|
|
409
408
|
6. Dev mode affects only your browser. Turn it off in the same place to get Surph's own pages back.
|
|
410
409
|
7. In Surph, keep your browser's DevTools open with **Network → Disable cache** checked, or the canvas may keep showing an older copy of a page after you change it. After editing `board.json` by hand, reload Surph so it reads the file again.
|
|
411
410
|
|
|
411
|
+
## Deploying
|
|
412
|
+
|
|
413
|
+
`npm run deploy` (`npx @surph_ai/cli board-deploy`, from the Surph CLI) publishes the board to Surph. Run it from the board's folder. It needs Node 18 or later and nothing else installed. In order, it:
|
|
414
|
+
|
|
415
|
+
1. **Reads the board's `slug`** from `board.json` (lowercase letters, digits and dashes). The slug names the board's place in Surph; without a valid one it stops before doing anything.
|
|
416
|
+
2. **Zips the site** into `site.zip`: every file in the folder except `package.json`, `package-lock.json`, `README.md`, `board.json`, `node_modules`, dot-files (`.env`, `.git`, `.DS_Store`) and `site.zip` itself. It lists the files it zipped.
|
|
417
|
+
3. **Uploads `site.zip`** to the board's `site/` folder in Surph's storage, then deletes the local `site.zip`. If the upload fails, the zip is kept to look at.
|
|
418
|
+
4. **Unzips it there**, so the pages and `assets/` sit in the board's `site/` folder, where each page's `file` is found.
|
|
419
|
+
5. **Uploads `board.json`**, last, so the live board's definition only changes once its pages are up. If an earlier step fails, `board.json` isn't uploaded and the live board keeps its previous version.
|
|
420
|
+
|
|
421
|
+
Each step prints what it did; a failure prints why and stops. After a deploy, Surph picks up the new `board.json` within about a minute.
|
|
422
|
+
|
|
423
|
+
**For coding agents:**
|
|
424
|
+
- **Deploying publishes the board to everyone who uses it.** Don't run `npm run deploy` unless the person asks you to in this conversation. Check the board with dev mode first (see [Developing locally](#developing-locally)).
|
|
425
|
+
- **Everything in the folder outside the list above is uploaded.** Keep drafts, exports, archives and secrets out of the board's folder, or in dot-files, which are never deployed.
|
|
426
|
+
- **A deploy replaces the live `board.json` with the local one.** Make sure `slug` is the board you mean to update: a deploy under another board's slug overwrites that board.
|
|
427
|
+
|
|
412
428
|
## Checklist before handing off a change
|
|
413
429
|
|
|
414
430
|
- [ ] Every `host.rpc(…)` call uses an operation from [Operations the host answers](#operations-the-host-answers) and reads only the fields listed for it and for [Item](#item).
|
|
@@ -420,3 +436,4 @@ A field marked "omitted" is missing from the object, not `null`. Every URL is `h
|
|
|
420
436
|
- [ ] `board.json`'s `config.pages` lists every page: `name`, `file` (or `url`), `shows` written as facts, `ops` matching the file's `rpc` calls, and one `start` page.
|
|
421
437
|
- [ ] Opened at `http://localhost:4180/`, every page loads without console errors.
|
|
422
438
|
- [ ] Checked in Surph with dev mode on: the canvas opens on the start page and each page paints with real data.
|
|
439
|
+
- [ ] Not deployed unless the person asked for it; if deployed, `npm run deploy` finished without errors and its file list holds only the board's site.
|
|
@@ -3,13 +3,11 @@
|
|
|
3
3
|
"version": "1.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"dev": "
|
|
6
|
+
"dev": "npx @surph_ai/cli board-server",
|
|
7
|
+
"deploy": "npx @surph_ai/cli board-deploy"
|
|
7
8
|
},
|
|
8
9
|
"keywords": [],
|
|
9
10
|
"author": "",
|
|
10
11
|
"license": "ISC",
|
|
11
|
-
"type": "commonjs"
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@surph_ai/sdk": "latest"
|
|
14
|
-
}
|
|
12
|
+
"type": "commonjs"
|
|
15
13
|
}
|