@shiqianjiang/ceru-plugin-cli 0.1.1 → 0.2.0
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/README.md +9 -5
- package/assets/catalog.json +1 -1
- package/assets/core-contracts.js +1 -0
- package/assets/index.html +94 -93
- package/assets/playground.js +293 -30
- package/assets/sandbox.js +50 -19
- package/assets/style.css +16 -0
- package/dist/dependencies.d.ts +6 -0
- package/dist/dependencies.js +45 -0
- package/dist/dev.d.ts +2 -0
- package/dist/dev.js +297 -144
- package/dist/frameworks.d.ts +2 -2
- package/dist/frameworks.js +11 -29
- package/dist/index.js +12 -4
- package/dist/project.d.ts +1 -0
- package/dist/project.js +48 -21
- package/dist/socket-broker.d.ts +23 -0
- package/dist/socket-broker.js +230 -0
- package/package.json +71 -67
- package/schemas/config.schema.json +349 -0
- package/templates/connected-library/js/.vscode/ceru.code-snippets +37 -9
- package/templates/connected-library/js/.vscode/launch.json +36 -46
- package/templates/connected-library/js/.vscode/tasks.json +11 -3
- package/templates/connected-library/js/README.md +7 -2
- package/templates/connected-library/js/ceru-plugin.code-workspace +8 -0
- package/templates/connected-library/js/package.json +4 -3
- package/templates/connected-library/js/src/index.js +7 -5
- package/templates/connected-library/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/connected-library/ts/.vscode/launch.json +36 -46
- package/templates/connected-library/ts/.vscode/tasks.json +11 -3
- package/templates/connected-library/ts/README.md +7 -2
- package/templates/connected-library/ts/ceru-plugin.code-workspace +8 -0
- package/templates/connected-library/ts/package.json +4 -3
- package/templates/connected-library/ts/src/index.ts +7 -5
- package/templates/guest-adapter/js/.vscode/ceru.code-snippets +37 -9
- package/templates/guest-adapter/js/.vscode/launch.json +36 -46
- package/templates/guest-adapter/js/.vscode/tasks.json +11 -3
- package/templates/guest-adapter/js/README.md +7 -2
- package/templates/guest-adapter/js/ceru-plugin.code-workspace +8 -0
- package/templates/guest-adapter/js/package.json +4 -3
- package/templates/guest-adapter/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/guest-adapter/ts/.vscode/launch.json +36 -46
- package/templates/guest-adapter/ts/.vscode/tasks.json +11 -3
- package/templates/guest-adapter/ts/README.md +7 -2
- package/templates/guest-adapter/ts/ceru-plugin.code-workspace +8 -0
- package/templates/guest-adapter/ts/package.json +4 -3
- package/templates/importer/js/.vscode/ceru.code-snippets +37 -9
- package/templates/importer/js/.vscode/launch.json +36 -46
- package/templates/importer/js/.vscode/tasks.json +11 -3
- package/templates/importer/js/README.md +8 -3
- package/templates/importer/js/ceru-plugin.code-workspace +8 -0
- package/templates/importer/js/ceru.plugin.json +8 -24
- package/templates/importer/js/package.json +4 -3
- package/templates/importer/js/src/index.js +28 -34
- package/templates/importer/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/importer/ts/.vscode/launch.json +36 -46
- package/templates/importer/ts/.vscode/tasks.json +11 -3
- package/templates/importer/ts/README.md +8 -3
- package/templates/importer/ts/ceru-plugin.code-workspace +8 -0
- package/templates/importer/ts/ceru.plugin.json +8 -24
- package/templates/importer/ts/package.json +4 -3
- package/templates/importer/ts/src/index.ts +28 -34
- package/templates/react/js/.vscode/ceru.code-snippets +37 -9
- package/templates/react/js/.vscode/launch.json +36 -46
- package/templates/react/js/.vscode/tasks.json +11 -3
- package/templates/react/js/README.md +7 -2
- package/templates/react/js/ceru-plugin.code-workspace +8 -0
- package/templates/react/js/ceru.plugin.json +1 -5
- package/templates/react/js/package.json +4 -3
- package/templates/react/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/react/ts/.vscode/launch.json +36 -46
- package/templates/react/ts/.vscode/tasks.json +11 -3
- package/templates/react/ts/README.md +7 -2
- package/templates/react/ts/ceru-plugin.code-workspace +8 -0
- package/templates/react/ts/ceru.plugin.json +1 -5
- package/templates/react/ts/package.json +4 -3
- package/templates/snapshot.json +22 -2
- package/templates/source/js/.vscode/ceru.code-snippets +37 -9
- package/templates/source/js/.vscode/launch.json +36 -46
- package/templates/source/js/.vscode/tasks.json +11 -3
- package/templates/source/js/README.md +7 -2
- package/templates/source/js/ceru-plugin.code-workspace +8 -0
- package/templates/source/js/ceru.plugin.json +9 -0
- package/templates/source/js/package.json +4 -3
- package/templates/source/js/src/index.js +29 -26
- package/templates/source/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/source/ts/.vscode/launch.json +36 -46
- package/templates/source/ts/.vscode/tasks.json +11 -3
- package/templates/source/ts/README.md +7 -2
- package/templates/source/ts/ceru-plugin.code-workspace +8 -0
- package/templates/source/ts/ceru.plugin.json +9 -0
- package/templates/source/ts/package.json +4 -3
- package/templates/source/ts/src/index.ts +29 -26
- package/templates/vue/js/.vscode/ceru.code-snippets +37 -9
- package/templates/vue/js/.vscode/launch.json +36 -46
- package/templates/vue/js/.vscode/tasks.json +11 -3
- package/templates/vue/js/README.md +7 -2
- package/templates/vue/js/ceru-plugin.code-workspace +8 -0
- package/templates/vue/js/ceru.plugin.json +1 -4
- package/templates/vue/js/package.json +4 -3
- package/templates/vue/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/vue/ts/.vscode/launch.json +36 -46
- package/templates/vue/ts/.vscode/tasks.json +11 -3
- package/templates/vue/ts/README.md +7 -2
- package/templates/vue/ts/ceru-plugin.code-workspace +8 -0
- package/templates/vue/ts/ceru.plugin.json +1 -4
- package/templates/vue/ts/package.json +4 -3
- package/templates/vue-tsx/js/.vscode/ceru.code-snippets +37 -9
- package/templates/vue-tsx/js/.vscode/launch.json +36 -46
- package/templates/vue-tsx/js/.vscode/tasks.json +11 -3
- package/templates/vue-tsx/js/README.md +7 -2
- package/templates/vue-tsx/js/ceru-plugin.code-workspace +8 -0
- package/templates/vue-tsx/js/ceru.plugin.json +1 -4
- package/templates/vue-tsx/js/package.json +4 -3
- package/templates/vue-tsx/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/vue-tsx/ts/.vscode/launch.json +36 -46
- package/templates/vue-tsx/ts/.vscode/tasks.json +11 -3
- package/templates/vue-tsx/ts/README.md +7 -2
- package/templates/vue-tsx/ts/ceru-plugin.code-workspace +8 -0
- package/templates/vue-tsx/ts/ceru.plugin.json +1 -4
- package/templates/vue-tsx/ts/package.json +4 -3
- package/templates/web-dist/js/.vscode/ceru.code-snippets +37 -9
- package/templates/web-dist/js/.vscode/launch.json +36 -46
- package/templates/web-dist/js/.vscode/tasks.json +11 -3
- package/templates/web-dist/js/README.md +7 -2
- package/templates/web-dist/js/ceru-plugin.code-workspace +8 -0
- package/templates/web-dist/js/package.json +4 -3
- package/templates/web-dist/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/web-dist/ts/.vscode/launch.json +36 -46
- package/templates/web-dist/ts/.vscode/tasks.json +11 -3
- package/templates/web-dist/ts/README.md +7 -2
- package/templates/web-dist/ts/ceru-plugin.code-workspace +8 -0
- package/templates/web-dist/ts/package.json +4 -3
- package/templates/web-surface/js/.vscode/ceru.code-snippets +37 -9
- package/templates/web-surface/js/.vscode/launch.json +36 -46
- package/templates/web-surface/js/.vscode/tasks.json +11 -3
- package/templates/web-surface/js/README.md +7 -2
- package/templates/web-surface/js/ceru-plugin.code-workspace +8 -0
- package/templates/web-surface/js/ceru.plugin.json +9 -0
- package/templates/web-surface/js/package.json +4 -3
- package/templates/web-surface/js/src/view.js +4 -1
- package/templates/web-surface/js/ui/search-note.css +3 -0
- package/templates/web-surface/ts/.vscode/ceru.code-snippets +37 -9
- package/templates/web-surface/ts/.vscode/launch.json +36 -46
- package/templates/web-surface/ts/.vscode/tasks.json +11 -3
- package/templates/web-surface/ts/README.md +7 -2
- package/templates/web-surface/ts/ceru-plugin.code-workspace +8 -0
- package/templates/web-surface/ts/ceru.plugin.json +9 -0
- package/templates/web-surface/ts/package.json +4 -3
- package/templates/web-surface/ts/src/view.ts +4 -1
- package/templates/web-surface/ts/ui/search-note.css +3 -0
- package/assets/shared-react.js +0 -77
- package/assets/shared-vue.js +0 -44
package/dist/dev.js
CHANGED
|
@@ -1,31 +1,57 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises';
|
|
1
|
+
import { readFile, realpath } from 'node:fs/promises';
|
|
2
2
|
import { watch } from 'node:fs';
|
|
3
|
-
import { createServer,
|
|
4
|
-
import {
|
|
3
|
+
import { createServer, Agent as HttpAgent } from 'node:http';
|
|
4
|
+
import { Agent as HttpsAgent } from 'node:https';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
import { SocketBroker } from './socket-broker.js';
|
|
5
7
|
import { lookup } from 'node:dns/promises';
|
|
6
8
|
import { isIP } from 'node:net';
|
|
7
|
-
import { resolve, relative } from 'node:path';
|
|
9
|
+
import { resolve, relative, dirname } from 'node:path';
|
|
8
10
|
import { fileURLToPath } from 'node:url';
|
|
9
11
|
import { createRequire } from 'node:module';
|
|
10
|
-
import { randomBytes } from 'node:crypto';
|
|
12
|
+
import { randomBytes, createHash } from 'node:crypto';
|
|
11
13
|
import { spawn } from 'node:child_process';
|
|
12
|
-
import { satisfies } from 'semver';
|
|
13
14
|
import { readArtifact, parseJsonStrict } from '@shiqianjiang/ceru-plugin-issuer';
|
|
14
15
|
import { buildProject } from './project.js';
|
|
15
16
|
const asset = (name) => fileURLToPath(new URL('../assets/' + name, import.meta.url));
|
|
17
|
+
async function readDebugTargets(port) {
|
|
18
|
+
const response = await fetch('http://127.0.0.1:' + port + '/json/list', {
|
|
19
|
+
signal: AbortSignal.timeout(750),
|
|
20
|
+
});
|
|
21
|
+
if (!response.ok)
|
|
22
|
+
throw new Error('Debugger is not ready');
|
|
23
|
+
const value = await response.json();
|
|
24
|
+
return Array.isArray(value) ? value : [];
|
|
25
|
+
}
|
|
26
|
+
async function waitForDebugger(origin, port, exited) {
|
|
27
|
+
const deadline = Date.now() + 15000;
|
|
28
|
+
while (Date.now() < deadline && !exited()) {
|
|
29
|
+
try {
|
|
30
|
+
const targets = await readDebugTargets(port);
|
|
31
|
+
if (targets.some((t) => t.type === 'page' && t.url === origin + '/'))
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
catch { }
|
|
35
|
+
await new Promise((done) => setTimeout(done, 100));
|
|
36
|
+
}
|
|
37
|
+
throw new Error('Electron debug target did not become ready at ' + origin);
|
|
38
|
+
}
|
|
16
39
|
const privateAddress = (address) => {
|
|
17
40
|
const value = address.toLowerCase();
|
|
41
|
+
if (value.startsWith('::ffff:'))
|
|
42
|
+
return privateAddress(value.slice('::ffff:'.length));
|
|
18
43
|
if (value.includes(':'))
|
|
19
44
|
return (value === '::1' ||
|
|
20
45
|
value.startsWith('fc') ||
|
|
21
46
|
value.startsWith('fd') ||
|
|
22
|
-
value.startsWith('fe80')
|
|
23
|
-
value.startsWith('::ffff:'));
|
|
47
|
+
value.startsWith('fe80'));
|
|
24
48
|
const p = value.split('.').map(Number);
|
|
25
49
|
return (p[0] === 0 ||
|
|
26
50
|
p[0] === 10 ||
|
|
27
51
|
p[0] === 127 ||
|
|
28
|
-
p[0] === 169 ||
|
|
52
|
+
(p[0] === 169 && p[1] === 254) ||
|
|
53
|
+
(p[0] === 100 && p[1] >= 64 && p[1] <= 127) ||
|
|
54
|
+
(p[0] === 198 && (p[1] === 18 || p[1] === 19)) ||
|
|
29
55
|
p[0] >= 224 ||
|
|
30
56
|
(p[0] === 172 && p[1] >= 16 && p[1] <= 31) ||
|
|
31
57
|
(p[0] === 192 && p[1] === 168));
|
|
@@ -41,22 +67,7 @@ async function bodyJson(request) {
|
|
|
41
67
|
}
|
|
42
68
|
return parseJsonStrict(Buffer.concat(chunks).toString('utf8'));
|
|
43
69
|
}
|
|
44
|
-
|
|
45
|
-
async function fetchAllowed(input, permission, privateAllowed, binary = false, reservedPorts = []) {
|
|
46
|
-
const url = new URL(input.url);
|
|
47
|
-
if (url.username || url.password || !['http:', 'https:'].includes(url.protocol))
|
|
48
|
-
throw new Error('Unsupported URL');
|
|
49
|
-
if (url.origin !== permission.origin)
|
|
50
|
-
throw new Error('Origin is not granted: ' + url.origin);
|
|
51
|
-
if (url.protocol === 'http:' && !privateAllowed)
|
|
52
|
-
throw new Error('HTTP requires a separately granted network.private permission');
|
|
53
|
-
const method = String(input.method ?? 'GET').toUpperCase();
|
|
54
|
-
if (!permission.methods.includes(method))
|
|
55
|
-
throw new Error('Method is outside the declared scope');
|
|
56
|
-
if (/%(?:2f|5c)/i.test(url.pathname))
|
|
57
|
-
throw new Error('Encoded path separator is not supported');
|
|
58
|
-
if (!permission.paths.some((path) => url.pathname === path || url.pathname.startsWith(path.endsWith('/') ? path : path + '/')))
|
|
59
|
-
throw new Error('Path is outside the declared scope');
|
|
70
|
+
async function allowedAddress(url, privateAllowed, reservedPorts) {
|
|
60
71
|
const hostname = url.hostname.replace(/^\[|\]$/g, '');
|
|
61
72
|
const addresses = isIP(hostname)
|
|
62
73
|
? [{ address: hostname, family: isIP(hostname) }]
|
|
@@ -65,15 +76,20 @@ async function fetchAllowed(input, permission, privateAllowed, binary = false, r
|
|
|
65
76
|
throw new Error('No address found');
|
|
66
77
|
for (const { address } of addresses) {
|
|
67
78
|
if ((address.startsWith('127.') || address === '::1') &&
|
|
68
|
-
reservedPorts.includes(Number(url.port) || (url.protocol === 'https:' ? 443 : 80)))
|
|
79
|
+
reservedPorts.includes(Number(url.port) || (url.protocol === 'https:' ? 443 : 80)))
|
|
69
80
|
throw new Error('Development Host and debugger endpoints are reserved');
|
|
70
|
-
}
|
|
71
|
-
if (/^(169\.254\.|fe80:|::ffff:)/i.test(address))
|
|
72
|
-
throw new Error('Sensitive/link-local address is blocked');
|
|
73
81
|
if (privateAddress(address) && !privateAllowed)
|
|
74
82
|
throw new Error('Private network access is not granted');
|
|
75
83
|
}
|
|
76
|
-
|
|
84
|
+
return addresses[0];
|
|
85
|
+
}
|
|
86
|
+
/** Development broker: capability-gated network, private-network split, pinned DNS, bounded response. */
|
|
87
|
+
async function fetchAllowed(input, privateAllowed, reservedPorts = [], signal, redirects = 0) {
|
|
88
|
+
const url = new URL(input.url);
|
|
89
|
+
if (!['http:', 'https:'].includes(url.protocol))
|
|
90
|
+
throw new Error('Unsupported URL');
|
|
91
|
+
const method = String(input.method ?? 'GET').toUpperCase();
|
|
92
|
+
const chosen = await allowedAddress(url, privateAllowed, reservedPorts);
|
|
77
93
|
const headers = { 'Accept-Encoding': 'identity' };
|
|
78
94
|
for (const [name, value] of Object.entries(input.headers ?? {})) {
|
|
79
95
|
if (/^(host|connection|content-length|transfer-encoding|accept-encoding|proxy-.*|upgrade)$/i.test(name))
|
|
@@ -85,55 +101,107 @@ async function fetchAllowed(input, permission, privateAllowed, binary = false, r
|
|
|
85
101
|
if (input.body !== undefined &&
|
|
86
102
|
(typeof input.body !== 'string' || Buffer.byteLength(input.body) > 1024 * 1024))
|
|
87
103
|
throw new Error('Invalid request body');
|
|
88
|
-
|
|
89
|
-
|
|
104
|
+
const pinLookup = ((_host, options, callback) => options?.all ? callback(null, [chosen]) : callback(null, chosen.address, chosen.family));
|
|
105
|
+
const httpAgent = new HttpAgent({ lookup: pinLookup });
|
|
106
|
+
const httpsAgent = new HttpsAgent({ lookup: pinLookup });
|
|
107
|
+
try {
|
|
108
|
+
const response = await axios.request({
|
|
109
|
+
url: url.href,
|
|
90
110
|
method,
|
|
91
111
|
headers,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
});
|
|
106
|
-
response.once('error', reject);
|
|
107
|
-
response.once('end', () => {
|
|
108
|
-
if (binary) {
|
|
109
|
-
resolveResponse({
|
|
110
|
-
status: response.statusCode,
|
|
111
|
-
headers: response.headers,
|
|
112
|
-
bytes: Buffer.concat(chunks),
|
|
113
|
-
});
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
const text = Buffer.concat(chunks).toString('utf8');
|
|
117
|
-
let body = text;
|
|
118
|
-
try {
|
|
119
|
-
body = JSON.parse(text);
|
|
120
|
-
}
|
|
121
|
-
catch { }
|
|
122
|
-
resolveResponse({ status: response.statusCode, headers: response.headers, body });
|
|
123
|
-
});
|
|
112
|
+
data: input.body,
|
|
113
|
+
adapter: 'http',
|
|
114
|
+
httpAgent,
|
|
115
|
+
httpsAgent,
|
|
116
|
+
proxy: false,
|
|
117
|
+
maxRedirects: 0,
|
|
118
|
+
timeout: Math.max(1000, Math.min(30000, Number(input.timeoutMs) || 15000)),
|
|
119
|
+
maxContentLength: 2 * 1024 * 1024,
|
|
120
|
+
maxBodyLength: 1024 * 1024,
|
|
121
|
+
responseType: 'arraybuffer',
|
|
122
|
+
validateStatus: () => true,
|
|
123
|
+
transformResponse: [(data) => data],
|
|
124
|
+
signal,
|
|
124
125
|
});
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
126
|
+
const bytes = Buffer.from(response.data);
|
|
127
|
+
const responseHeaders = Object.fromEntries(Object.entries(response.headers).filter(([, value]) => value != null && typeof value !== 'function'));
|
|
128
|
+
if (response.status >= 300 &&
|
|
129
|
+
response.status < 400 &&
|
|
130
|
+
typeof responseHeaders.location === 'string') {
|
|
131
|
+
if (redirects >= 5)
|
|
132
|
+
throw new Error('HTTP redirect limit exceeded');
|
|
133
|
+
const nextMethod = response.status === 303 ? 'GET' : method;
|
|
134
|
+
return fetchAllowed({
|
|
135
|
+
...input,
|
|
136
|
+
url: new URL(responseHeaders.location, url).href,
|
|
137
|
+
method: nextMethod,
|
|
138
|
+
...(nextMethod === 'GET' ? { body: undefined } : {}),
|
|
139
|
+
}, privateAllowed, reservedPorts, signal, redirects + 1);
|
|
140
|
+
}
|
|
141
|
+
const text = bytes.toString('utf8');
|
|
142
|
+
let body = text;
|
|
143
|
+
try {
|
|
144
|
+
body = JSON.parse(text);
|
|
145
|
+
}
|
|
146
|
+
catch { }
|
|
147
|
+
return { status: response.status, headers: responseHeaders, body };
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
// AxiosError includes config/headers: never send it through plugin logs or RPC.
|
|
151
|
+
throw new Error(axios.isAxiosError(error)
|
|
152
|
+
? 'HTTP request failed (' + (error.code || 'NETWORK_ERROR') + ')'
|
|
153
|
+
: 'HTTP request failed');
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
httpAgent.destroy();
|
|
157
|
+
httpsAgent.destroy();
|
|
158
|
+
}
|
|
129
159
|
}
|
|
130
160
|
export async function runDev(project, options = {}) {
|
|
131
|
-
const root = resolve(project);
|
|
161
|
+
const root = await realpath(resolve(project));
|
|
132
162
|
const port = options.port ?? 4179;
|
|
133
163
|
const debugPort = options.debugPort ?? 9223;
|
|
134
164
|
if (![port, debugPort].every((n) => Number.isInteger(n) && n >= 0 && n <= 65535))
|
|
135
165
|
throw new Error('Invalid port');
|
|
136
|
-
|
|
166
|
+
const projectKey = createHash('sha256')
|
|
167
|
+
.update(process.platform === 'win32' ? root.toLowerCase() : root)
|
|
168
|
+
.digest('hex');
|
|
169
|
+
if (options.ensureRunning && port > 0) {
|
|
170
|
+
let existing;
|
|
171
|
+
try {
|
|
172
|
+
const response = await fetch('http://127.0.0.1:' + port + '/__ceru_dev__/status', {
|
|
173
|
+
signal: AbortSignal.timeout(750),
|
|
174
|
+
});
|
|
175
|
+
if (response.ok)
|
|
176
|
+
existing = await response.json();
|
|
177
|
+
}
|
|
178
|
+
catch { }
|
|
179
|
+
if (existing) {
|
|
180
|
+
if (existing.projectKey !== projectKey || existing.debugPort !== debugPort)
|
|
181
|
+
throw new Error('This dev port belongs to another project or debug port. Stop it or select Attach to Ceru plugin with the correct ports.');
|
|
182
|
+
await waitForDebugger(existing.origin, debugPort, () => false);
|
|
183
|
+
console.log('Reusing existing Ceru development Host.');
|
|
184
|
+
console.log('CERU_DEBUG_READY ' + existing.origin + '/');
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const prepare = async () => {
|
|
189
|
+
if (!options.artifact)
|
|
190
|
+
return buildProject(root, { development: true });
|
|
191
|
+
const path = resolve(options.artifact);
|
|
192
|
+
const bytes = await readFile(path);
|
|
193
|
+
const parsed = readArtifact(bytes);
|
|
194
|
+
if (Object.keys(parsed.header.manifest.engines.libraries ?? {}).length)
|
|
195
|
+
throw new Error('This artifact still requires Host frameworks. Rebuild it with CLI 0.2.0 or later.');
|
|
196
|
+
// Serve the verified release functions as scripts inside the same isolated
|
|
197
|
+
// Surface. No project imports, transpiler, or framework is involved here.
|
|
198
|
+
const devModules = Object.fromEntries(Object.entries(parsed.modules).map(([id, code]) => [
|
|
199
|
+
id,
|
|
200
|
+
'globalThis.__ceruStart(' + code + ');\n',
|
|
201
|
+
]));
|
|
202
|
+
return { path, bytes: bytes.length, devModules };
|
|
203
|
+
};
|
|
204
|
+
let build = await prepare();
|
|
137
205
|
let artifact = readArtifact(await readFile(build.path));
|
|
138
206
|
let revision = 1;
|
|
139
207
|
let error = null;
|
|
@@ -143,22 +211,19 @@ export async function runDev(project, options = {}) {
|
|
|
143
211
|
const token = randomBytes(24).toString('hex');
|
|
144
212
|
const nonce = randomBytes(24).toString('base64');
|
|
145
213
|
const catalog = JSON.parse(await readFile(asset('catalog.json'), 'utf8'));
|
|
146
|
-
const checkLibraries = () => {
|
|
147
|
-
for (const [name, range] of Object.entries(artifact.header.manifest.engines.libraries ?? {})) {
|
|
148
|
-
if (!catalog.libraries[name] || !satisfies(catalog.libraries[name], range))
|
|
149
|
-
throw new Error('Plugin requires Host ' +
|
|
150
|
-
name +
|
|
151
|
-
' ' +
|
|
152
|
-
range +
|
|
153
|
-
'; dev Host provides ' +
|
|
154
|
-
catalog.libraries[name]);
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
checkLibraries();
|
|
158
214
|
let config = {};
|
|
215
|
+
try {
|
|
216
|
+
const configPath = resolve(root, '.ceru-dev/config.json');
|
|
217
|
+
config = parseJsonStrict(await readFile(configPath, 'utf8'));
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
if (!(error instanceof Error) || !('code' in error) || error.code !== 'ENOENT')
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
159
223
|
const grants = new Map();
|
|
224
|
+
const sockets = new SocketBroker();
|
|
225
|
+
const operations = new Map();
|
|
160
226
|
const storage = new Map();
|
|
161
|
-
const leases = new Map();
|
|
162
227
|
const rebuild = async () => {
|
|
163
228
|
if (running) {
|
|
164
229
|
dirty = true;
|
|
@@ -168,13 +233,15 @@ export async function runDev(project, options = {}) {
|
|
|
168
233
|
do {
|
|
169
234
|
dirty = false;
|
|
170
235
|
try {
|
|
171
|
-
build = await
|
|
236
|
+
build = await prepare();
|
|
172
237
|
artifact = readArtifact(await readFile(build.path));
|
|
173
|
-
checkLibraries();
|
|
174
238
|
revision++;
|
|
175
239
|
error = null;
|
|
240
|
+
sockets.closeAll();
|
|
241
|
+
for (const operation of operations.values())
|
|
242
|
+
operation.abort();
|
|
243
|
+
operations.clear();
|
|
176
244
|
grants.clear();
|
|
177
|
-
leases.clear();
|
|
178
245
|
console.log('Reloaded plugin revision ' + revision);
|
|
179
246
|
}
|
|
180
247
|
catch (e) {
|
|
@@ -184,10 +251,18 @@ export async function runDev(project, options = {}) {
|
|
|
184
251
|
} while (dirty);
|
|
185
252
|
running = false;
|
|
186
253
|
};
|
|
187
|
-
const
|
|
254
|
+
const watchRoot = options.artifact ? dirname(resolve(options.artifact)) : root;
|
|
255
|
+
const watcher = watch(watchRoot, { recursive: !options.artifact }, (_event, path) => {
|
|
188
256
|
if (!path)
|
|
189
257
|
return;
|
|
190
258
|
const rel = path.toString().replaceAll('\\', '/');
|
|
259
|
+
if (options.artifact) {
|
|
260
|
+
if (resolve(watchRoot, path.toString()) !== resolve(options.artifact))
|
|
261
|
+
return;
|
|
262
|
+
clearTimeout(timer);
|
|
263
|
+
timer = setTimeout(() => void rebuild(), 250);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
191
266
|
if (/^(node_modules|dist|\.git|\.keys|\.ceru-dev)(\/|$)/.test(rel) ||
|
|
192
267
|
rel === relative(root, build.path).replaceAll('\\', '/') ||
|
|
193
268
|
rel.includes('.tmp'))
|
|
@@ -213,26 +288,14 @@ export async function runDev(project, options = {}) {
|
|
|
213
288
|
return;
|
|
214
289
|
}
|
|
215
290
|
const url = new URL(request.url ?? '/', origin);
|
|
216
|
-
if (url.pathname
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
url: lease.url,
|
|
224
|
-
headers: request.headers.range ? { Range: request.headers.range } : {},
|
|
225
|
-
}, grant, privateAllowed, true, [Number(new URL(origin).port), debugPort]);
|
|
226
|
-
response.writeHead(media.status, {
|
|
227
|
-
'Content-Type': media.headers['content-type'] || 'application/octet-stream',
|
|
228
|
-
'Cache-Control': 'no-store',
|
|
229
|
-
'Content-Length': media.bytes.length,
|
|
230
|
-
...(media.headers['content-range']
|
|
231
|
-
? { 'Content-Range': media.headers['content-range'] }
|
|
232
|
-
: {}),
|
|
233
|
-
'X-Content-Type-Options': 'nosniff',
|
|
291
|
+
if (url.pathname === '/__ceru_dev__/status' && request.method === 'GET') {
|
|
292
|
+
json(200, {
|
|
293
|
+
protocol: 1,
|
|
294
|
+
projectKey,
|
|
295
|
+
origin,
|
|
296
|
+
debugPort,
|
|
297
|
+
mode: options.artifact ? 'preview' : 'dev',
|
|
234
298
|
});
|
|
235
|
-
response.end(media.bytes);
|
|
236
299
|
return;
|
|
237
300
|
}
|
|
238
301
|
if (url.pathname === '/plugin.js' && ['GET', 'HEAD'].includes(request.method ?? '')) {
|
|
@@ -256,11 +319,16 @@ export async function runDev(project, options = {}) {
|
|
|
256
319
|
json(403, { error: 'Invalid development session' });
|
|
257
320
|
return;
|
|
258
321
|
}
|
|
322
|
+
if (url.pathname === '/api/socket-events' && request.method === 'GET') {
|
|
323
|
+
json(200, { events: sockets.drain() });
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
259
326
|
if (url.pathname === '/api/state' && request.method === 'GET') {
|
|
260
327
|
json(200, {
|
|
261
328
|
revision,
|
|
262
329
|
error,
|
|
263
330
|
manifest: artifact.header.manifest,
|
|
331
|
+
migrationWarnings: artifact.migrationWarnings,
|
|
264
332
|
resources: artifact.resources,
|
|
265
333
|
catalog,
|
|
266
334
|
config,
|
|
@@ -284,25 +352,62 @@ export async function runDev(project, options = {}) {
|
|
|
284
352
|
const declaration = artifact.header.manifest.permissions?.find((p) => p.key === input.key);
|
|
285
353
|
if (!declaration)
|
|
286
354
|
throw new Error('Permission was not declared');
|
|
287
|
-
if (!input.allow)
|
|
355
|
+
if (!input.allow) {
|
|
288
356
|
grants.delete(input.key);
|
|
357
|
+
sockets.revoke(input.key);
|
|
358
|
+
}
|
|
289
359
|
else {
|
|
290
|
-
|
|
291
|
-
const declaredOrigin = scope.origin;
|
|
292
|
-
const chosen = typeof declaredOrigin === 'string' ? declaredOrigin : input.origin;
|
|
293
|
-
if (declaration.name.startsWith('network.') && !chosen)
|
|
294
|
-
throw new Error('Specify an exact origin');
|
|
295
|
-
grants.set(input.key, {
|
|
296
|
-
origin: chosen ? new URL(chosen).origin : '',
|
|
297
|
-
methods: Array.isArray(scope.methods) ? scope.methods : ['GET', 'POST'],
|
|
298
|
-
paths: Array.isArray(scope.pathPrefixes) ? scope.pathPrefixes : ['/'],
|
|
299
|
-
});
|
|
360
|
+
grants.set(input.key, { name: declaration.name });
|
|
300
361
|
}
|
|
301
362
|
json(200, { ok: true });
|
|
302
363
|
return;
|
|
303
364
|
}
|
|
304
365
|
if (url.pathname === '/api/rpc') {
|
|
305
366
|
const data = input.data ?? {};
|
|
367
|
+
if (input.method.startsWith('sockets.')) {
|
|
368
|
+
if (input.method === 'sockets.closeAll') {
|
|
369
|
+
sockets.closeAll();
|
|
370
|
+
json(200, { value: null });
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (input.method === 'sockets.connect') {
|
|
374
|
+
const declaration = artifact.header.manifest.permissions?.find((p) => p.key === data.permissionKey && p.name === 'network.socket');
|
|
375
|
+
const grant = declaration && grants.get(declaration.key);
|
|
376
|
+
if (!grant)
|
|
377
|
+
throw new Error('Socket permission is not granted');
|
|
378
|
+
const privateAllowed = artifact.header.manifest.permissions?.some((permission) => permission.name === 'network.private' && grants.has(permission.key)) ?? false;
|
|
379
|
+
const value = await sockets.connect(data, privateAllowed, [
|
|
380
|
+
Number(new URL(origin).port),
|
|
381
|
+
debugPort,
|
|
382
|
+
]);
|
|
383
|
+
// A pending handshake must not outlive a revoked grant or plugin reload.
|
|
384
|
+
if (grants.get(declaration.key) !== grant) {
|
|
385
|
+
sockets.disconnect(value.id);
|
|
386
|
+
throw new Error('Socket permission was revoked');
|
|
387
|
+
}
|
|
388
|
+
json(200, { value });
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (input.method === 'sockets.send') {
|
|
392
|
+
sockets.send(data.id, data.event, data.data);
|
|
393
|
+
json(200, { value: null });
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (input.method === 'sockets.disconnect') {
|
|
397
|
+
sockets.disconnect(data.id);
|
|
398
|
+
json(200, { value: null });
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
throw new Error('Unknown socket method');
|
|
402
|
+
}
|
|
403
|
+
if (input.method === 'operations.cancel') {
|
|
404
|
+
operations.get(String(data.id))?.abort();
|
|
405
|
+
json(200, { value: null });
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (input.method.startsWith('library.playlists.')) {
|
|
409
|
+
throw new Error('此独立开发 Host 尚未连接澜音的歌单服务。本地歌单和云歌单由澜音管理;请在正式 Host 中调用此能力。开发环境不会创建替代歌单库。');
|
|
410
|
+
}
|
|
306
411
|
if (input.method === 'config.get') {
|
|
307
412
|
json(200, { value: config });
|
|
308
413
|
return;
|
|
@@ -333,18 +438,61 @@ export async function runDev(project, options = {}) {
|
|
|
333
438
|
});
|
|
334
439
|
return;
|
|
335
440
|
}
|
|
336
|
-
if (input.method === '
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
441
|
+
if (input.method === 'permissions.getGranted') {
|
|
442
|
+
json(200, {
|
|
443
|
+
value: (artifact.header.manifest.permissions ?? [])
|
|
444
|
+
.filter((permission) => grants.has(permission.key))
|
|
445
|
+
.map((permission) => ({
|
|
446
|
+
key: permission.key,
|
|
447
|
+
name: permission.name,
|
|
448
|
+
scope: permission.scope ?? {},
|
|
449
|
+
status: 'granted',
|
|
450
|
+
})),
|
|
451
|
+
});
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (input.method === 'services.capabilities.list') {
|
|
455
|
+
json(200, {
|
|
456
|
+
value: [
|
|
457
|
+
'account',
|
|
458
|
+
'library',
|
|
459
|
+
'player',
|
|
460
|
+
'queue',
|
|
461
|
+
'favorites',
|
|
462
|
+
'history',
|
|
463
|
+
'downloads',
|
|
464
|
+
'files',
|
|
465
|
+
'clipboard',
|
|
466
|
+
'localMusic',
|
|
467
|
+
'settings',
|
|
468
|
+
'window',
|
|
469
|
+
'hotkeys',
|
|
470
|
+
'sharing',
|
|
471
|
+
'rooms',
|
|
472
|
+
'devices',
|
|
473
|
+
'ai',
|
|
474
|
+
'tasks',
|
|
475
|
+
].map((service) => ({
|
|
476
|
+
service,
|
|
477
|
+
version: '1.0.0',
|
|
478
|
+
available: false,
|
|
479
|
+
reason: 'host-not-connected',
|
|
480
|
+
permissionGroups: [],
|
|
481
|
+
})),
|
|
482
|
+
});
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (input.method === 'services.capabilities.get') {
|
|
486
|
+
const service = String(data.args?.[0] ?? '');
|
|
487
|
+
json(200, {
|
|
488
|
+
value: {
|
|
489
|
+
service,
|
|
490
|
+
version: '1.0.0',
|
|
491
|
+
available: false,
|
|
492
|
+
reason: 'host-not-connected',
|
|
493
|
+
permissionGroups: [],
|
|
494
|
+
},
|
|
346
495
|
});
|
|
347
|
-
json(200, { value: { kind: 'media', id } });
|
|
348
496
|
return;
|
|
349
497
|
}
|
|
350
498
|
if (input.method === 'http.request') {
|
|
@@ -352,13 +500,20 @@ export async function runDev(project, options = {}) {
|
|
|
352
500
|
const grant = declaration && grants.get(declaration.key);
|
|
353
501
|
if (!grant)
|
|
354
502
|
throw new Error('Network permission is not granted');
|
|
355
|
-
const hasPrivate = artifact.header.manifest.permissions?.some((
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
503
|
+
const hasPrivate = artifact.header.manifest.permissions?.some((permission) => permission.name === 'network.private' && grants.has(permission.key)) ?? false;
|
|
504
|
+
const operationId = String(data.operation?.id ?? '');
|
|
505
|
+
const controller = new AbortController();
|
|
506
|
+
if (operationId)
|
|
507
|
+
operations.set(operationId, controller);
|
|
508
|
+
try {
|
|
509
|
+
json(200, {
|
|
510
|
+
value: await fetchAllowed(data, hasPrivate, [Number(new URL(origin).port), debugPort], controller.signal),
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
finally {
|
|
514
|
+
if (operations.get(operationId) === controller)
|
|
515
|
+
operations.delete(operationId);
|
|
516
|
+
}
|
|
362
517
|
return;
|
|
363
518
|
}
|
|
364
519
|
throw new Error('Development Host does not implement ' +
|
|
@@ -381,22 +536,16 @@ export async function runDev(project, options = {}) {
|
|
|
381
536
|
type = 'text/html';
|
|
382
537
|
response.setHeader('Content-Security-Policy', "default-src 'none'; script-src 'self' 'nonce-" +
|
|
383
538
|
nonce +
|
|
384
|
-
"'; style-src 'self'; img-src 'self' data:; connect-src 'self'; frame-src 'self'; frame-ancestors 'none'; base-uri 'none'; object-src 'none'");
|
|
539
|
+
"'; style-src 'self'; img-src 'self' data:; media-src 'self' http: https: data: blob:; connect-src 'self'; frame-src 'self'; frame-ancestors 'none'; base-uri 'none'; object-src 'none'");
|
|
385
540
|
}
|
|
386
541
|
else if (url.pathname === '/sandbox.html') {
|
|
387
542
|
const moduleId = url.searchParams.get('module') ?? '';
|
|
388
543
|
if (!Object.hasOwn(build.devModules, moduleId))
|
|
389
544
|
throw new Error('Unknown module');
|
|
390
|
-
const libraries = artifact.header.manifest.engines.libraries ?? {};
|
|
391
|
-
const shared = [libraries.vue ? 'vue' : '', libraries.react ? 'react' : '']
|
|
392
|
-
.filter(Boolean)
|
|
393
|
-
.map((name) => '<script nonce="' + nonce + '" src="/shared/' + name + '.js"></script>')
|
|
394
|
-
.join('');
|
|
395
545
|
contents =
|
|
396
546
|
'<!doctype html><meta charset="utf-8"><div id="plugin-root"></div><script nonce="' +
|
|
397
547
|
nonce +
|
|
398
548
|
'" src="/sandbox.js"></script>' +
|
|
399
|
-
shared +
|
|
400
549
|
'<script nonce="' +
|
|
401
550
|
nonce +
|
|
402
551
|
'" src="/modules/' +
|
|
@@ -417,10 +566,9 @@ export async function runDev(project, options = {}) {
|
|
|
417
566
|
else {
|
|
418
567
|
const file = {
|
|
419
568
|
'/playground.js': 'playground.js',
|
|
569
|
+
'/core-contracts.js': 'core-contracts.js',
|
|
420
570
|
'/style.css': 'style.css',
|
|
421
571
|
'/sandbox.js': 'sandbox.js',
|
|
422
|
-
'/shared/vue.js': 'shared-vue.js',
|
|
423
|
-
'/shared/react.js': 'shared-react.js',
|
|
424
572
|
}[url.pathname];
|
|
425
573
|
if (!file) {
|
|
426
574
|
json(404, { error: 'Not found' });
|
|
@@ -458,6 +606,9 @@ export async function runDev(project, options = {}) {
|
|
|
458
606
|
const address = server.address();
|
|
459
607
|
origin = 'http://127.0.0.1:' + (typeof address === 'object' && address ? address.port : port);
|
|
460
608
|
const close = () => {
|
|
609
|
+
sockets.closeAll();
|
|
610
|
+
for (const operation of operations.values())
|
|
611
|
+
operation.abort();
|
|
461
612
|
clearTimeout(timer);
|
|
462
613
|
watcher.close();
|
|
463
614
|
electron?.kill();
|
|
@@ -495,7 +646,9 @@ export async function runDev(project, options = {}) {
|
|
|
495
646
|
console.log('Electron exited: ' + code);
|
|
496
647
|
close();
|
|
497
648
|
});
|
|
649
|
+
await waitForDebugger(origin, debugPort, () => electron?.exitCode !== null);
|
|
498
650
|
console.log('Electron debugger: 127.0.0.1:' + debugPort + ' (VS Code: Attach to Ceru plugin)');
|
|
651
|
+
console.log('CERU_DEBUG_READY ' + origin + '/');
|
|
499
652
|
}
|
|
500
653
|
catch (e) {
|
|
501
654
|
console.error('Electron could not start. Run npm install in the generated project, or use dev --no-open and open the playground URL.');
|
package/dist/frameworks.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Plugin } from 'esbuild';
|
|
2
|
-
|
|
3
|
-
export declare function
|
|
2
|
+
/** Side-effect stylesheet imports must survive single-file builds (React/Vue/vanilla). */
|
|
3
|
+
export declare function stylesheetPlugin(): Plugin;
|
|
4
4
|
export declare function vuePlugin(root: string, framework?: string): Plugin;
|
|
5
5
|
/** Convert a local, already-built SPA into a Surface entry. No uploaded HTML is executed by the CLI. */
|
|
6
6
|
export declare function webDistEntry(directoryInput: string): Promise<{
|