@shiqianjiang/ceru-plugin-cli 0.1.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.
Files changed (314) hide show
  1. package/ASSET_LICENSE +238 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +1 -0
  4. package/README.md +29 -0
  5. package/assets/catalog.json +1 -0
  6. package/assets/electron-app/main.cjs +1 -0
  7. package/assets/electron-app/package.json +8 -0
  8. package/assets/electron.cjs +42 -0
  9. package/assets/index.html +93 -0
  10. package/assets/originals/account.svg +1 -0
  11. package/assets/originals/add.svg +1 -0
  12. package/assets/originals/app.logo.svg +10 -0
  13. package/assets/originals/chart.svg +1 -0
  14. package/assets/originals/close.svg +1 -0
  15. package/assets/originals/cloud.svg +1 -0
  16. package/assets/originals/download.svg +1 -0
  17. package/assets/originals/heart.svg +1 -0
  18. package/assets/originals/import.svg +1 -0
  19. package/assets/originals/info.svg +1 -0
  20. package/assets/originals/library.svg +1 -0
  21. package/assets/originals/music-note.svg +1 -0
  22. package/assets/originals/pause.svg +1 -0
  23. package/assets/originals/placeholder.avatar.webp +0 -0
  24. package/assets/originals/placeholder.cover.png +0 -0
  25. package/assets/originals/platform.all.svg +1 -0
  26. package/assets/originals/platform.kg.svg +1 -0
  27. package/assets/originals/platform.kw.svg +1 -0
  28. package/assets/originals/platform.local.svg +1 -0
  29. package/assets/originals/platform.mg.svg +1 -0
  30. package/assets/originals/platform.tx.svg +1 -0
  31. package/assets/originals/platform.wy.svg +1 -0
  32. package/assets/originals/play.svg +1 -0
  33. package/assets/originals/refresh.svg +1 -0
  34. package/assets/originals/search.svg +1 -0
  35. package/assets/originals/server.svg +1 -0
  36. package/assets/originals/settings.svg +1 -0
  37. package/assets/originals/share.svg +1 -0
  38. package/assets/playground.js +446 -0
  39. package/assets/sandbox.js +36 -0
  40. package/assets/shared-react.js +77 -0
  41. package/assets/shared-vue.js +44 -0
  42. package/assets/style.css +264 -0
  43. package/assets/vendor-licenses/lodash-es.txt +47 -0
  44. package/assets/vendor-licenses/react-dom.txt +21 -0
  45. package/assets/vendor-licenses/react.txt +21 -0
  46. package/assets/vendor-licenses/scheduler.txt +21 -0
  47. package/assets/vendor-licenses/vue.txt +21 -0
  48. package/assets/vendor-licenses/vue_reactivity.txt +21 -0
  49. package/assets/vendor-licenses/vue_runtime-core.txt +21 -0
  50. package/assets/vendor-licenses/vue_runtime-dom.txt +21 -0
  51. package/assets/vendor-licenses/vue_shared.txt +21 -0
  52. package/dist/bin.d.ts +2 -0
  53. package/dist/bin.js +6 -0
  54. package/dist/dev.d.ts +7 -0
  55. package/dist/dev.js +507 -0
  56. package/dist/frameworks.d.ts +9 -0
  57. package/dist/frameworks.js +310 -0
  58. package/dist/index.d.ts +2 -0
  59. package/dist/index.js +206 -0
  60. package/dist/project.d.ts +34 -0
  61. package/dist/project.js +342 -0
  62. package/package.json +67 -0
  63. package/schemas/config.schema.json +610 -0
  64. package/templates/connected-library/js/.prettierrc.json +5 -0
  65. package/templates/connected-library/js/.vscode/ceru.code-snippets +27 -0
  66. package/templates/connected-library/js/.vscode/extensions.json +3 -0
  67. package/templates/connected-library/js/.vscode/launch.json +30 -0
  68. package/templates/connected-library/js/.vscode/settings.json +20 -0
  69. package/templates/connected-library/js/.vscode/tasks.json +36 -0
  70. package/templates/connected-library/js/README.md +38 -0
  71. package/templates/connected-library/js/_gitignore +6 -0
  72. package/templates/connected-library/js/ceru.plugin.json +76 -0
  73. package/templates/connected-library/js/package.json +21 -0
  74. package/templates/connected-library/js/src/index.js +38 -0
  75. package/templates/connected-library/js/tsconfig.json +16 -0
  76. package/templates/connected-library/js/ui/settings.json +24 -0
  77. package/templates/connected-library/ts/.prettierrc.json +5 -0
  78. package/templates/connected-library/ts/.vscode/ceru.code-snippets +27 -0
  79. package/templates/connected-library/ts/.vscode/extensions.json +3 -0
  80. package/templates/connected-library/ts/.vscode/launch.json +30 -0
  81. package/templates/connected-library/ts/.vscode/settings.json +20 -0
  82. package/templates/connected-library/ts/.vscode/tasks.json +36 -0
  83. package/templates/connected-library/ts/README.md +38 -0
  84. package/templates/connected-library/ts/_gitignore +6 -0
  85. package/templates/connected-library/ts/ceru.plugin.json +76 -0
  86. package/templates/connected-library/ts/package.json +21 -0
  87. package/templates/connected-library/ts/src/index.ts +38 -0
  88. package/templates/connected-library/ts/tsconfig.json +16 -0
  89. package/templates/connected-library/ts/ui/settings.json +24 -0
  90. package/templates/guest-adapter/js/.prettierrc.json +5 -0
  91. package/templates/guest-adapter/js/.vscode/ceru.code-snippets +27 -0
  92. package/templates/guest-adapter/js/.vscode/extensions.json +3 -0
  93. package/templates/guest-adapter/js/.vscode/launch.json +30 -0
  94. package/templates/guest-adapter/js/.vscode/settings.json +20 -0
  95. package/templates/guest-adapter/js/.vscode/tasks.json +36 -0
  96. package/templates/guest-adapter/js/README.md +38 -0
  97. package/templates/guest-adapter/js/_gitignore +6 -0
  98. package/templates/guest-adapter/js/ceru.plugin.json +95 -0
  99. package/templates/guest-adapter/js/package.json +21 -0
  100. package/templates/guest-adapter/js/src/guest.js +6 -0
  101. package/templates/guest-adapter/js/src/index.js +8 -0
  102. package/templates/guest-adapter/js/tsconfig.json +16 -0
  103. package/templates/guest-adapter/js/ui/settings.json +14 -0
  104. package/templates/guest-adapter/ts/.prettierrc.json +5 -0
  105. package/templates/guest-adapter/ts/.vscode/ceru.code-snippets +27 -0
  106. package/templates/guest-adapter/ts/.vscode/extensions.json +3 -0
  107. package/templates/guest-adapter/ts/.vscode/launch.json +30 -0
  108. package/templates/guest-adapter/ts/.vscode/settings.json +20 -0
  109. package/templates/guest-adapter/ts/.vscode/tasks.json +36 -0
  110. package/templates/guest-adapter/ts/README.md +38 -0
  111. package/templates/guest-adapter/ts/_gitignore +6 -0
  112. package/templates/guest-adapter/ts/ceru.plugin.json +95 -0
  113. package/templates/guest-adapter/ts/package.json +21 -0
  114. package/templates/guest-adapter/ts/src/guest.ts +6 -0
  115. package/templates/guest-adapter/ts/src/index.ts +8 -0
  116. package/templates/guest-adapter/ts/tsconfig.json +16 -0
  117. package/templates/guest-adapter/ts/ui/settings.json +14 -0
  118. package/templates/importer/js/.prettierrc.json +5 -0
  119. package/templates/importer/js/.vscode/ceru.code-snippets +27 -0
  120. package/templates/importer/js/.vscode/extensions.json +3 -0
  121. package/templates/importer/js/.vscode/launch.json +30 -0
  122. package/templates/importer/js/.vscode/settings.json +20 -0
  123. package/templates/importer/js/.vscode/tasks.json +36 -0
  124. package/templates/importer/js/README.md +38 -0
  125. package/templates/importer/js/_gitignore +6 -0
  126. package/templates/importer/js/ceru.plugin.json +63 -0
  127. package/templates/importer/js/package.json +21 -0
  128. package/templates/importer/js/src/index.js +37 -0
  129. package/templates/importer/js/tsconfig.json +16 -0
  130. package/templates/importer/js/ui/settings.json +24 -0
  131. package/templates/importer/ts/.prettierrc.json +5 -0
  132. package/templates/importer/ts/.vscode/ceru.code-snippets +27 -0
  133. package/templates/importer/ts/.vscode/extensions.json +3 -0
  134. package/templates/importer/ts/.vscode/launch.json +30 -0
  135. package/templates/importer/ts/.vscode/settings.json +20 -0
  136. package/templates/importer/ts/.vscode/tasks.json +36 -0
  137. package/templates/importer/ts/README.md +38 -0
  138. package/templates/importer/ts/_gitignore +6 -0
  139. package/templates/importer/ts/ceru.plugin.json +63 -0
  140. package/templates/importer/ts/package.json +21 -0
  141. package/templates/importer/ts/src/index.ts +37 -0
  142. package/templates/importer/ts/tsconfig.json +16 -0
  143. package/templates/importer/ts/ui/settings.json +24 -0
  144. package/templates/react/js/.prettierrc.json +5 -0
  145. package/templates/react/js/.vscode/ceru.code-snippets +27 -0
  146. package/templates/react/js/.vscode/extensions.json +3 -0
  147. package/templates/react/js/.vscode/launch.json +30 -0
  148. package/templates/react/js/.vscode/settings.json +20 -0
  149. package/templates/react/js/.vscode/tasks.json +36 -0
  150. package/templates/react/js/README.md +36 -0
  151. package/templates/react/js/_gitignore +6 -0
  152. package/templates/react/js/ceru.plugin.json +60 -0
  153. package/templates/react/js/package.json +25 -0
  154. package/templates/react/js/src/App.jsx +22 -0
  155. package/templates/react/js/src/index.js +7 -0
  156. package/templates/react/js/src/view.jsx +8 -0
  157. package/templates/react/js/tsconfig.json +16 -0
  158. package/templates/react/ts/.prettierrc.json +5 -0
  159. package/templates/react/ts/.vscode/ceru.code-snippets +27 -0
  160. package/templates/react/ts/.vscode/extensions.json +3 -0
  161. package/templates/react/ts/.vscode/launch.json +30 -0
  162. package/templates/react/ts/.vscode/settings.json +20 -0
  163. package/templates/react/ts/.vscode/tasks.json +36 -0
  164. package/templates/react/ts/README.md +36 -0
  165. package/templates/react/ts/_gitignore +6 -0
  166. package/templates/react/ts/ceru.plugin.json +60 -0
  167. package/templates/react/ts/package.json +25 -0
  168. package/templates/react/ts/src/App.tsx +23 -0
  169. package/templates/react/ts/src/index.ts +7 -0
  170. package/templates/react/ts/src/view.tsx +8 -0
  171. package/templates/react/ts/tsconfig.json +16 -0
  172. package/templates/snapshot.json +257 -0
  173. package/templates/source/js/.prettierrc.json +5 -0
  174. package/templates/source/js/.vscode/ceru.code-snippets +27 -0
  175. package/templates/source/js/.vscode/extensions.json +3 -0
  176. package/templates/source/js/.vscode/launch.json +30 -0
  177. package/templates/source/js/.vscode/settings.json +20 -0
  178. package/templates/source/js/.vscode/tasks.json +36 -0
  179. package/templates/source/js/README.md +38 -0
  180. package/templates/source/js/_gitignore +6 -0
  181. package/templates/source/js/ceru.plugin.json +51 -0
  182. package/templates/source/js/package.json +21 -0
  183. package/templates/source/js/src/index.js +50 -0
  184. package/templates/source/js/tsconfig.json +16 -0
  185. package/templates/source/ts/.prettierrc.json +5 -0
  186. package/templates/source/ts/.vscode/ceru.code-snippets +27 -0
  187. package/templates/source/ts/.vscode/extensions.json +3 -0
  188. package/templates/source/ts/.vscode/launch.json +30 -0
  189. package/templates/source/ts/.vscode/settings.json +20 -0
  190. package/templates/source/ts/.vscode/tasks.json +36 -0
  191. package/templates/source/ts/README.md +38 -0
  192. package/templates/source/ts/_gitignore +6 -0
  193. package/templates/source/ts/ceru.plugin.json +51 -0
  194. package/templates/source/ts/package.json +21 -0
  195. package/templates/source/ts/src/index.ts +50 -0
  196. package/templates/source/ts/tsconfig.json +16 -0
  197. package/templates/vue/js/.prettierrc.json +5 -0
  198. package/templates/vue/js/.vscode/ceru.code-snippets +27 -0
  199. package/templates/vue/js/.vscode/extensions.json +3 -0
  200. package/templates/vue/js/.vscode/launch.json +30 -0
  201. package/templates/vue/js/.vscode/settings.json +20 -0
  202. package/templates/vue/js/.vscode/tasks.json +36 -0
  203. package/templates/vue/js/README.md +36 -0
  204. package/templates/vue/js/_gitignore +6 -0
  205. package/templates/vue/js/ceru.plugin.json +59 -0
  206. package/templates/vue/js/package.json +22 -0
  207. package/templates/vue/js/src/App.vue +31 -0
  208. package/templates/vue/js/src/env.d.ts +5 -0
  209. package/templates/vue/js/src/index.js +7 -0
  210. package/templates/vue/js/src/view.js +8 -0
  211. package/templates/vue/js/tsconfig.json +17 -0
  212. package/templates/vue/ts/.prettierrc.json +5 -0
  213. package/templates/vue/ts/.vscode/ceru.code-snippets +27 -0
  214. package/templates/vue/ts/.vscode/extensions.json +3 -0
  215. package/templates/vue/ts/.vscode/launch.json +30 -0
  216. package/templates/vue/ts/.vscode/settings.json +20 -0
  217. package/templates/vue/ts/.vscode/tasks.json +36 -0
  218. package/templates/vue/ts/README.md +36 -0
  219. package/templates/vue/ts/_gitignore +6 -0
  220. package/templates/vue/ts/ceru.plugin.json +59 -0
  221. package/templates/vue/ts/package.json +22 -0
  222. package/templates/vue/ts/src/App.vue +32 -0
  223. package/templates/vue/ts/src/env.d.ts +5 -0
  224. package/templates/vue/ts/src/index.ts +7 -0
  225. package/templates/vue/ts/src/view.ts +8 -0
  226. package/templates/vue/ts/tsconfig.json +17 -0
  227. package/templates/vue-tsx/js/.prettierrc.json +5 -0
  228. package/templates/vue-tsx/js/.vscode/ceru.code-snippets +27 -0
  229. package/templates/vue-tsx/js/.vscode/extensions.json +3 -0
  230. package/templates/vue-tsx/js/.vscode/launch.json +30 -0
  231. package/templates/vue-tsx/js/.vscode/settings.json +20 -0
  232. package/templates/vue-tsx/js/.vscode/tasks.json +36 -0
  233. package/templates/vue-tsx/js/README.md +36 -0
  234. package/templates/vue-tsx/js/_gitignore +6 -0
  235. package/templates/vue-tsx/js/ceru.plugin.json +59 -0
  236. package/templates/vue-tsx/js/package.json +22 -0
  237. package/templates/vue-tsx/js/src/App.jsx +14 -0
  238. package/templates/vue-tsx/js/src/env.d.ts +5 -0
  239. package/templates/vue-tsx/js/src/index.js +7 -0
  240. package/templates/vue-tsx/js/src/view.js +8 -0
  241. package/templates/vue-tsx/js/tsconfig.json +17 -0
  242. package/templates/vue-tsx/ts/.prettierrc.json +5 -0
  243. package/templates/vue-tsx/ts/.vscode/ceru.code-snippets +27 -0
  244. package/templates/vue-tsx/ts/.vscode/extensions.json +3 -0
  245. package/templates/vue-tsx/ts/.vscode/launch.json +30 -0
  246. package/templates/vue-tsx/ts/.vscode/settings.json +20 -0
  247. package/templates/vue-tsx/ts/.vscode/tasks.json +36 -0
  248. package/templates/vue-tsx/ts/README.md +36 -0
  249. package/templates/vue-tsx/ts/_gitignore +6 -0
  250. package/templates/vue-tsx/ts/ceru.plugin.json +59 -0
  251. package/templates/vue-tsx/ts/package.json +22 -0
  252. package/templates/vue-tsx/ts/src/App.tsx +15 -0
  253. package/templates/vue-tsx/ts/src/env.d.ts +5 -0
  254. package/templates/vue-tsx/ts/src/index.ts +7 -0
  255. package/templates/vue-tsx/ts/src/view.ts +8 -0
  256. package/templates/vue-tsx/ts/tsconfig.json +17 -0
  257. package/templates/web-dist/js/.prettierrc.json +5 -0
  258. package/templates/web-dist/js/.vscode/ceru.code-snippets +27 -0
  259. package/templates/web-dist/js/.vscode/extensions.json +3 -0
  260. package/templates/web-dist/js/.vscode/launch.json +30 -0
  261. package/templates/web-dist/js/.vscode/settings.json +20 -0
  262. package/templates/web-dist/js/.vscode/tasks.json +36 -0
  263. package/templates/web-dist/js/README.md +38 -0
  264. package/templates/web-dist/js/_gitignore +6 -0
  265. package/templates/web-dist/js/ceru.plugin.json +57 -0
  266. package/templates/web-dist/js/package.json +21 -0
  267. package/templates/web-dist/js/src/index.js +7 -0
  268. package/templates/web-dist/js/tsconfig.json +16 -0
  269. package/templates/web-dist/js/web-dist/index.html +14 -0
  270. package/templates/web-dist/js/web-dist/main.js +4 -0
  271. package/templates/web-dist/js/web-dist/sample.png +0 -0
  272. package/templates/web-dist/js/web-dist/style.css +11 -0
  273. package/templates/web-dist/ts/.prettierrc.json +5 -0
  274. package/templates/web-dist/ts/.vscode/ceru.code-snippets +27 -0
  275. package/templates/web-dist/ts/.vscode/extensions.json +3 -0
  276. package/templates/web-dist/ts/.vscode/launch.json +30 -0
  277. package/templates/web-dist/ts/.vscode/settings.json +20 -0
  278. package/templates/web-dist/ts/.vscode/tasks.json +36 -0
  279. package/templates/web-dist/ts/README.md +38 -0
  280. package/templates/web-dist/ts/_gitignore +6 -0
  281. package/templates/web-dist/ts/ceru.plugin.json +57 -0
  282. package/templates/web-dist/ts/package.json +21 -0
  283. package/templates/web-dist/ts/src/index.ts +7 -0
  284. package/templates/web-dist/ts/tsconfig.json +16 -0
  285. package/templates/web-dist/ts/web-dist/index.html +14 -0
  286. package/templates/web-dist/ts/web-dist/main.js +4 -0
  287. package/templates/web-dist/ts/web-dist/sample.png +0 -0
  288. package/templates/web-dist/ts/web-dist/style.css +11 -0
  289. package/templates/web-surface/js/.prettierrc.json +5 -0
  290. package/templates/web-surface/js/.vscode/ceru.code-snippets +27 -0
  291. package/templates/web-surface/js/.vscode/extensions.json +3 -0
  292. package/templates/web-surface/js/.vscode/launch.json +30 -0
  293. package/templates/web-surface/js/.vscode/settings.json +20 -0
  294. package/templates/web-surface/js/.vscode/tasks.json +36 -0
  295. package/templates/web-surface/js/README.md +36 -0
  296. package/templates/web-surface/js/_gitignore +6 -0
  297. package/templates/web-surface/js/ceru.plugin.json +55 -0
  298. package/templates/web-surface/js/package.json +21 -0
  299. package/templates/web-surface/js/src/index.js +7 -0
  300. package/templates/web-surface/js/src/view.js +8 -0
  301. package/templates/web-surface/js/tsconfig.json +16 -0
  302. package/templates/web-surface/ts/.prettierrc.json +5 -0
  303. package/templates/web-surface/ts/.vscode/ceru.code-snippets +27 -0
  304. package/templates/web-surface/ts/.vscode/extensions.json +3 -0
  305. package/templates/web-surface/ts/.vscode/launch.json +30 -0
  306. package/templates/web-surface/ts/.vscode/settings.json +20 -0
  307. package/templates/web-surface/ts/.vscode/tasks.json +36 -0
  308. package/templates/web-surface/ts/README.md +36 -0
  309. package/templates/web-surface/ts/_gitignore +6 -0
  310. package/templates/web-surface/ts/ceru.plugin.json +55 -0
  311. package/templates/web-surface/ts/package.json +21 -0
  312. package/templates/web-surface/ts/src/index.ts +7 -0
  313. package/templates/web-surface/ts/src/view.ts +8 -0
  314. package/templates/web-surface/ts/tsconfig.json +16 -0
package/dist/dev.js ADDED
@@ -0,0 +1,507 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { watch } from 'node:fs';
3
+ import { createServer, request as httpRequest } from 'node:http';
4
+ import { request as httpsRequest } from 'node:https';
5
+ import { lookup } from 'node:dns/promises';
6
+ import { isIP } from 'node:net';
7
+ import { resolve, relative } from 'node:path';
8
+ import { fileURLToPath } from 'node:url';
9
+ import { createRequire } from 'node:module';
10
+ import { randomBytes } from 'node:crypto';
11
+ import { spawn } from 'node:child_process';
12
+ import { satisfies } from 'semver';
13
+ import { readArtifact, parseJsonStrict } from '@shiqianjiang/ceru-plugin-issuer';
14
+ import { buildProject } from './project.js';
15
+ const asset = (name) => fileURLToPath(new URL('../assets/' + name, import.meta.url));
16
+ const privateAddress = (address) => {
17
+ const value = address.toLowerCase();
18
+ if (value.includes(':'))
19
+ return (value === '::1' ||
20
+ value.startsWith('fc') ||
21
+ value.startsWith('fd') ||
22
+ value.startsWith('fe80') ||
23
+ value.startsWith('::ffff:'));
24
+ const p = value.split('.').map(Number);
25
+ return (p[0] === 0 ||
26
+ p[0] === 10 ||
27
+ p[0] === 127 ||
28
+ p[0] === 169 ||
29
+ p[0] >= 224 ||
30
+ (p[0] === 172 && p[1] >= 16 && p[1] <= 31) ||
31
+ (p[0] === 192 && p[1] === 168));
32
+ };
33
+ async function bodyJson(request) {
34
+ const chunks = [];
35
+ let size = 0;
36
+ for await (const chunk of request) {
37
+ size += chunk.length;
38
+ if (size > 256 * 1024)
39
+ throw new Error('Request too large');
40
+ chunks.push(chunk);
41
+ }
42
+ return parseJsonStrict(Buffer.concat(chunks).toString('utf8'));
43
+ }
44
+ /** Narrow development broker: exact granted origin, no redirects, pinned DNS, bounded response. */
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');
60
+ const hostname = url.hostname.replace(/^\[|\]$/g, '');
61
+ const addresses = isIP(hostname)
62
+ ? [{ address: hostname, family: isIP(hostname) }]
63
+ : await lookup(hostname, { all: true });
64
+ if (!addresses.length)
65
+ throw new Error('No address found');
66
+ for (const { address } of addresses) {
67
+ if ((address.startsWith('127.') || address === '::1') &&
68
+ reservedPorts.includes(Number(url.port) || (url.protocol === 'https:' ? 443 : 80))) {
69
+ 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
+ if (privateAddress(address) && !privateAllowed)
74
+ throw new Error('Private network access is not granted');
75
+ }
76
+ const chosen = addresses[0];
77
+ const headers = { 'Accept-Encoding': 'identity' };
78
+ for (const [name, value] of Object.entries(input.headers ?? {})) {
79
+ if (/^(host|connection|content-length|transfer-encoding|accept-encoding|proxy-.*|upgrade)$/i.test(name))
80
+ throw new Error('Reserved header: ' + name);
81
+ if (typeof value !== 'string' || /[\r\n]/.test(value))
82
+ throw new Error('Invalid header');
83
+ headers[name] = value;
84
+ }
85
+ if (input.body !== undefined &&
86
+ (typeof input.body !== 'string' || Buffer.byteLength(input.body) > 1024 * 1024))
87
+ throw new Error('Invalid request body');
88
+ return new Promise((resolveResponse, reject) => {
89
+ const request = (url.protocol === 'https:' ? httpsRequest : httpRequest)(url, {
90
+ method,
91
+ headers,
92
+ lookup: ((_host, options, callback) => options?.all
93
+ ? callback(null, [chosen])
94
+ : callback(null, chosen.address, chosen.family)),
95
+ }, (response) => {
96
+ const chunks = [];
97
+ let total = 0;
98
+ response.on('data', (chunk) => {
99
+ total += chunk.length;
100
+ if (total > (binary ? 32 : 2) * 1024 * 1024) {
101
+ request.destroy(new Error('Response exceeds development limit'));
102
+ return;
103
+ }
104
+ chunks.push(chunk);
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
+ });
124
+ });
125
+ request.setTimeout(15000, () => request.destroy(new Error('HTTP timeout')));
126
+ request.once('error', reject);
127
+ request.end(input.body);
128
+ });
129
+ }
130
+ export async function runDev(project, options = {}) {
131
+ const root = resolve(project);
132
+ const port = options.port ?? 4179;
133
+ const debugPort = options.debugPort ?? 9223;
134
+ if (![port, debugPort].every((n) => Number.isInteger(n) && n >= 0 && n <= 65535))
135
+ throw new Error('Invalid port');
136
+ let build = await buildProject(root, { development: true });
137
+ let artifact = readArtifact(await readFile(build.path));
138
+ let revision = 1;
139
+ let error = null;
140
+ let running = false;
141
+ let dirty = false;
142
+ let timer;
143
+ const token = randomBytes(24).toString('hex');
144
+ const nonce = randomBytes(24).toString('base64');
145
+ 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
+ let config = {};
159
+ const grants = new Map();
160
+ const storage = new Map();
161
+ const leases = new Map();
162
+ const rebuild = async () => {
163
+ if (running) {
164
+ dirty = true;
165
+ return;
166
+ }
167
+ running = true;
168
+ do {
169
+ dirty = false;
170
+ try {
171
+ build = await buildProject(root, { development: true });
172
+ artifact = readArtifact(await readFile(build.path));
173
+ checkLibraries();
174
+ revision++;
175
+ error = null;
176
+ grants.clear();
177
+ leases.clear();
178
+ console.log('Reloaded plugin revision ' + revision);
179
+ }
180
+ catch (e) {
181
+ error = e instanceof Error ? e.message : String(e);
182
+ console.error(error);
183
+ }
184
+ } while (dirty);
185
+ running = false;
186
+ };
187
+ const watcher = watch(root, { recursive: true }, (_event, path) => {
188
+ if (!path)
189
+ return;
190
+ const rel = path.toString().replaceAll('\\', '/');
191
+ if (/^(node_modules|dist|\.git|\.keys|\.ceru-dev)(\/|$)/.test(rel) ||
192
+ rel === relative(root, build.path).replaceAll('\\', '/') ||
193
+ rel.includes('.tmp'))
194
+ return;
195
+ if (!/\.(?:vue|html|tsx?|jsx?|json|css|svg|png|jpe?g|webp|woff2)$/.test(rel))
196
+ return;
197
+ clearTimeout(timer);
198
+ timer = setTimeout(() => void rebuild(), 250);
199
+ });
200
+ let origin = '';
201
+ const server = createServer(async (request, response) => {
202
+ const json = (status, value) => {
203
+ response.writeHead(status, {
204
+ 'Content-Type': 'application/json',
205
+ 'Cache-Control': 'no-store',
206
+ 'X-Content-Type-Options': 'nosniff',
207
+ });
208
+ response.end(JSON.stringify(value));
209
+ };
210
+ try {
211
+ if (request.headers.host !== new URL(origin).host) {
212
+ json(403, { error: 'Invalid Host' });
213
+ return;
214
+ }
215
+ const url = new URL(request.url ?? '/', origin);
216
+ if (url.pathname.startsWith('/media/') && request.method === 'GET') {
217
+ const lease = leases.get(url.pathname.slice('/media/'.length));
218
+ const grant = lease && grants.get(lease.permissionKey);
219
+ if (!lease || !grant || lease.expiresAt < Date.now())
220
+ throw new Error('Media lease expired or revoked');
221
+ const privateAllowed = artifact.header.manifest.permissions?.some((p) => p.name === 'network.private' && grants.get(p.key)?.origin === grant.origin) ?? false;
222
+ const media = await fetchAllowed({
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',
234
+ });
235
+ response.end(media.bytes);
236
+ return;
237
+ }
238
+ if (url.pathname === '/plugin.js' && ['GET', 'HEAD'].includes(request.method ?? '')) {
239
+ if (error) {
240
+ json(503, { error });
241
+ return;
242
+ }
243
+ const bytes = await readFile(build.path);
244
+ response.writeHead(200, {
245
+ 'Content-Type': 'application/javascript; charset=utf-8',
246
+ 'Cache-Control': 'no-store',
247
+ 'X-Content-Type-Options': 'nosniff',
248
+ 'Cross-Origin-Resource-Policy': 'same-origin',
249
+ });
250
+ response.end(request.method === 'HEAD' ? undefined : bytes);
251
+ return;
252
+ }
253
+ if (url.pathname.startsWith('/api/')) {
254
+ if (request.headers['x-ceru-dev-token'] !== token ||
255
+ (request.headers.origin && request.headers.origin !== origin)) {
256
+ json(403, { error: 'Invalid development session' });
257
+ return;
258
+ }
259
+ if (url.pathname === '/api/state' && request.method === 'GET') {
260
+ json(200, {
261
+ revision,
262
+ error,
263
+ manifest: artifact.header.manifest,
264
+ resources: artifact.resources,
265
+ catalog,
266
+ config,
267
+ grants: Object.fromEntries(grants),
268
+ });
269
+ return;
270
+ }
271
+ if (request.method !== 'POST') {
272
+ json(405, { error: 'Method not allowed' });
273
+ return;
274
+ }
275
+ const input = await bodyJson(request);
276
+ if (url.pathname === '/api/config') {
277
+ if (!input || typeof input !== 'object' || Array.isArray(input))
278
+ throw new Error('Config must be an object');
279
+ config = input;
280
+ json(200, { ok: true });
281
+ return;
282
+ }
283
+ if (url.pathname === '/api/grant') {
284
+ const declaration = artifact.header.manifest.permissions?.find((p) => p.key === input.key);
285
+ if (!declaration)
286
+ throw new Error('Permission was not declared');
287
+ if (!input.allow)
288
+ grants.delete(input.key);
289
+ else {
290
+ const scope = declaration.scope ?? {};
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
+ });
300
+ }
301
+ json(200, { ok: true });
302
+ return;
303
+ }
304
+ if (url.pathname === '/api/rpc') {
305
+ const data = input.data ?? {};
306
+ if (input.method === 'config.get') {
307
+ json(200, { value: config });
308
+ return;
309
+ }
310
+ if (input.method === 'storage.get') {
311
+ json(200, { value: storage.get(data.key) ?? null });
312
+ return;
313
+ }
314
+ if (input.method === 'storage.set') {
315
+ storage.set(String(data.key), data.value);
316
+ json(200, { value: null });
317
+ return;
318
+ }
319
+ if (input.method === 'storage.delete') {
320
+ storage.delete(String(data.key));
321
+ json(200, { value: null });
322
+ return;
323
+ }
324
+ if (input.method === 'permissions.query') {
325
+ json(200, {
326
+ value: {
327
+ status: !artifact.header.manifest.permissions?.some((p) => p.key === data.key)
328
+ ? 'undeclared'
329
+ : grants.has(data.key)
330
+ ? 'granted'
331
+ : 'prompt',
332
+ },
333
+ });
334
+ return;
335
+ }
336
+ if (input.method === 'media.createLease') {
337
+ const declaration = artifact.header.manifest.permissions?.find((p) => p.key === data.permissionKey && p.name === 'network.request');
338
+ const grant = declaration && grants.get(declaration.key);
339
+ if (!grant || new URL(data.url).origin !== grant.origin)
340
+ throw new Error('Media origin is not granted');
341
+ const id = randomBytes(18).toString('hex');
342
+ leases.set(id, {
343
+ url: data.url,
344
+ permissionKey: data.permissionKey,
345
+ expiresAt: Math.min(Number(data.expiresAt) || Date.now() + 60000, Date.now() + 60000),
346
+ });
347
+ json(200, { value: { kind: 'media', id } });
348
+ return;
349
+ }
350
+ if (input.method === 'http.request') {
351
+ const declaration = artifact.header.manifest.permissions?.find((p) => p.key === data.permissionKey && p.name === 'network.request');
352
+ const grant = declaration && grants.get(declaration.key);
353
+ if (!grant)
354
+ throw new Error('Network permission is not granted');
355
+ const hasPrivate = artifact.header.manifest.permissions?.some((p) => p.name === 'network.private' && grants.get(p.key)?.origin === grant.origin) ?? false;
356
+ json(200, {
357
+ value: await fetchAllowed(data, grant, hasPrivate, false, [
358
+ Number(new URL(origin).port),
359
+ debugPort,
360
+ ]),
361
+ });
362
+ return;
363
+ }
364
+ throw new Error('Development Host does not implement ' +
365
+ input.method +
366
+ '. Use a mock or implement the production Host capability.');
367
+ }
368
+ json(404, { error: 'Unknown endpoint' });
369
+ return;
370
+ }
371
+ if (request.method !== 'GET') {
372
+ json(405, { error: 'Method not allowed' });
373
+ return;
374
+ }
375
+ let contents;
376
+ let type;
377
+ if (url.pathname === '/') {
378
+ contents = (await readFile(asset('index.html'), 'utf8'))
379
+ .replaceAll('{{NONCE}}', nonce)
380
+ .replace('{{TOKEN}}', JSON.stringify(token));
381
+ type = 'text/html';
382
+ response.setHeader('Content-Security-Policy', "default-src 'none'; script-src 'self' 'nonce-" +
383
+ nonce +
384
+ "'; style-src 'self'; img-src 'self' data:; connect-src 'self'; frame-src 'self'; frame-ancestors 'none'; base-uri 'none'; object-src 'none'");
385
+ }
386
+ else if (url.pathname === '/sandbox.html') {
387
+ const moduleId = url.searchParams.get('module') ?? '';
388
+ if (!Object.hasOwn(build.devModules, moduleId))
389
+ 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
+ contents =
396
+ '<!doctype html><meta charset="utf-8"><div id="plugin-root"></div><script nonce="' +
397
+ nonce +
398
+ '" src="/sandbox.js"></script>' +
399
+ shared +
400
+ '<script nonce="' +
401
+ nonce +
402
+ '" src="/modules/' +
403
+ encodeURIComponent(moduleId) +
404
+ '.js"></script>';
405
+ type = 'text/html';
406
+ response.setHeader('Content-Security-Policy', "default-src 'none'; script-src 'nonce-" +
407
+ nonce +
408
+ "'; style-src 'unsafe-inline' data:; img-src data:; connect-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'");
409
+ }
410
+ else if (url.pathname.startsWith('/modules/')) {
411
+ const moduleId = decodeURIComponent(url.pathname.slice('/modules/'.length, -3));
412
+ if (!Object.hasOwn(build.devModules, moduleId))
413
+ throw new Error('Unknown module');
414
+ contents = build.devModules[moduleId];
415
+ type = 'application/javascript';
416
+ }
417
+ else {
418
+ const file = {
419
+ '/playground.js': 'playground.js',
420
+ '/style.css': 'style.css',
421
+ '/sandbox.js': 'sandbox.js',
422
+ '/shared/vue.js': 'shared-vue.js',
423
+ '/shared/react.js': 'shared-react.js',
424
+ }[url.pathname];
425
+ if (!file) {
426
+ json(404, { error: 'Not found' });
427
+ return;
428
+ }
429
+ contents = await readFile(asset(file));
430
+ type = file.endsWith('.css') ? 'text/css' : 'application/javascript';
431
+ }
432
+ response.writeHead(200, {
433
+ 'Content-Type': type + '; charset=utf-8',
434
+ 'Cache-Control': 'no-store',
435
+ 'X-Content-Type-Options': 'nosniff',
436
+ });
437
+ response.end(contents);
438
+ }
439
+ catch (e) {
440
+ if (response.headersSent) {
441
+ response.destroy();
442
+ return;
443
+ }
444
+ json(400, { error: e instanceof Error ? e.message : String(e) });
445
+ }
446
+ });
447
+ let electron;
448
+ try {
449
+ await new Promise((res, rej) => {
450
+ server.once('error', rej);
451
+ server.listen(port, '127.0.0.1', res);
452
+ });
453
+ }
454
+ catch (e) {
455
+ watcher.close();
456
+ throw e;
457
+ }
458
+ const address = server.address();
459
+ origin = 'http://127.0.0.1:' + (typeof address === 'object' && address ? address.port : port);
460
+ const close = () => {
461
+ clearTimeout(timer);
462
+ watcher.close();
463
+ electron?.kill();
464
+ server.close();
465
+ process.exitCode = 0;
466
+ };
467
+ console.log('Dev playground: ' + origin + '/');
468
+ console.log('Development artifact: ' + origin + '/plugin.js');
469
+ if (!options.noOpen) {
470
+ try {
471
+ const executable = options.electron ?? createRequire(resolve(root, 'package.json'))('electron');
472
+ const env = {
473
+ ...process.env,
474
+ ELECTRON_ENABLE_LOGGING: '1',
475
+ CERU_DEV_ORIGIN: origin,
476
+ CERU_DEV_DEBUG_PORT: String(debugPort),
477
+ CERU_DEV_HIDDEN: options.hidden ? '1' : '0',
478
+ };
479
+ delete env.ELECTRON_RUN_AS_NODE;
480
+ delete env.NODE_OPTIONS;
481
+ delete env.NODE_PATH;
482
+ // Keep URLs out of positional arguments: Windows Electron rejects URL + trailing args.
483
+ electron = spawn(executable, [asset('electron-app')], {
484
+ env,
485
+ stdio: ['ignore', 'pipe', 'pipe'],
486
+ windowsHide: false,
487
+ });
488
+ electron.stdout?.pipe(process.stdout, { end: false });
489
+ electron.stderr?.pipe(process.stderr, { end: false });
490
+ electron.once('error', (e) => {
491
+ console.error(e.message);
492
+ close();
493
+ });
494
+ electron.once('exit', (code) => {
495
+ console.log('Electron exited: ' + code);
496
+ close();
497
+ });
498
+ console.log('Electron debugger: 127.0.0.1:' + debugPort + ' (VS Code: Attach to Ceru plugin)');
499
+ }
500
+ catch (e) {
501
+ console.error('Electron could not start. Run npm install in the generated project, or use dev --no-open and open the playground URL.');
502
+ console.error(e instanceof Error ? e.message : String(e));
503
+ }
504
+ }
505
+ process.once('SIGINT', close);
506
+ process.once('SIGTERM', close);
507
+ }
@@ -0,0 +1,9 @@
1
+ import { type Plugin } from 'esbuild';
2
+ export declare const SHARED_IMPORTS: Record<string, string>;
3
+ export declare function sharedPlugin(libraries?: Record<string, string>): Plugin;
4
+ export declare function vuePlugin(root: string, framework?: string): Plugin;
5
+ /** Convert a local, already-built SPA into a Surface entry. No uploaded HTML is executed by the CLI. */
6
+ export declare function webDistEntry(directoryInput: string): Promise<{
7
+ plugin: Plugin;
8
+ entry: string;
9
+ }>;