@shiqianjiang/ceru-plugin-core 0.2.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/assets/node-globals.js +8 -4
- package/assets/sandbox.js +22 -22
- package/dist/electron.js +101 -29
- package/dist/guest-store.d.ts +63 -0
- package/dist/guest-store.js +402 -0
- package/dist/index.js +137 -26
- package/dist/network.js +46 -8
- package/dist/node-worker.js +92 -17
- package/dist/node.d.ts +13 -1
- package/dist/node.js +66 -13
- package/dist/surface-document.d.ts +13 -0
- package/dist/surface-document.js +45 -0
- package/dist/surface.d.ts +18 -0
- package/dist/surface.js +70 -0
- package/package.json +74 -59
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Artifact } from '@shiqianjiang/ceru-plugin-issuer';
|
|
2
|
+
import type { JsonObject } from '@shiqianjiang/ceru-plugin-sdk';
|
|
3
|
+
/** A self-contained document for a sandbox="allow-scripts" opaque-origin iframe. */
|
|
4
|
+
export declare function createWebSurfaceDocument(artifact: Artifact, surfaceId: string, sessionId: string): Promise<{
|
|
5
|
+
title: string;
|
|
6
|
+
presentation: {
|
|
7
|
+
kind: "drawer";
|
|
8
|
+
placement: "left" | "right" | "top" | "bottom";
|
|
9
|
+
size: number;
|
|
10
|
+
};
|
|
11
|
+
html: string;
|
|
12
|
+
init: JsonObject;
|
|
13
|
+
}>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
let assets;
|
|
3
|
+
/** A self-contained document for a sandbox="allow-scripts" opaque-origin iframe. */
|
|
4
|
+
export async function createWebSurfaceDocument(artifact, surfaceId, sessionId) {
|
|
5
|
+
const surface = artifact.header.manifest.modules.surfaces?.find((item) => item.id === surfaceId);
|
|
6
|
+
if (surface?.kind !== 'web')
|
|
7
|
+
throw new Error('Unknown web Surface: ' + surfaceId);
|
|
8
|
+
const code = artifact.modules[surface.entry];
|
|
9
|
+
if (!code)
|
|
10
|
+
throw new Error('Missing Surface entry: ' + surface.entry);
|
|
11
|
+
assets ??= Promise.all([
|
|
12
|
+
readFile(new URL('../assets/sandbox.js', import.meta.url), 'utf8'),
|
|
13
|
+
readFile(new URL('../assets/catalog.json', import.meta.url), 'utf8'),
|
|
14
|
+
]);
|
|
15
|
+
const [runtime, catalog] = await assets;
|
|
16
|
+
const script = (source) => '<script src="data:text/javascript;base64,' +
|
|
17
|
+
Buffer.from(source).toString('base64') +
|
|
18
|
+
'"></script>';
|
|
19
|
+
return {
|
|
20
|
+
title: surface.title ??
|
|
21
|
+
artifact.header.manifest.contributes?.commands?.find((item) => item.view === surfaceId)
|
|
22
|
+
?.title ??
|
|
23
|
+
artifact.header.manifest.name,
|
|
24
|
+
presentation: {
|
|
25
|
+
kind: 'drawer',
|
|
26
|
+
placement: 'right',
|
|
27
|
+
size: 480,
|
|
28
|
+
...surface.presentation,
|
|
29
|
+
},
|
|
30
|
+
html: '<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">' +
|
|
31
|
+
"<meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'none'; script-src data:; style-src 'unsafe-inline'; img-src data: blob:; font-src data:; connect-src 'none'; object-src 'none'; frame-src 'none'; form-action 'none'; base-uri 'none'\">" +
|
|
32
|
+
'<div id="plugin-root"></div>' +
|
|
33
|
+
script(runtime) +
|
|
34
|
+
script('globalThis.__ceruStart(' + code + ');'),
|
|
35
|
+
init: {
|
|
36
|
+
generation: sessionId,
|
|
37
|
+
mode: 'production',
|
|
38
|
+
kind: 'web',
|
|
39
|
+
manifest: { ...artifact.header.manifest, config: {} },
|
|
40
|
+
resources: artifact.resources,
|
|
41
|
+
catalog: JSON.parse(catalog),
|
|
42
|
+
mount: { kind: 'page' },
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { JsonValue, PluginManifest, SurfaceDeclaration } from '@shiqianjiang/ceru-plugin-sdk';
|
|
2
|
+
/** Shared by preview and production hosts; no platform or login-specific behavior. */
|
|
3
|
+
export declare class SurfaceSession {
|
|
4
|
+
readonly surface: SurfaceDeclaration;
|
|
5
|
+
private readonly dispatch;
|
|
6
|
+
private closed;
|
|
7
|
+
private opened;
|
|
8
|
+
private opening?;
|
|
9
|
+
private closing?;
|
|
10
|
+
private pending;
|
|
11
|
+
private actions;
|
|
12
|
+
constructor(surface: SurfaceDeclaration, manifest: PluginManifest, dispatch: (action: string, input: JsonValue, signal?: AbortSignal) => Promise<unknown>);
|
|
13
|
+
/** Call when the view is mounted, once even if the transport repeats its ready message. */
|
|
14
|
+
open(): Promise<unknown>;
|
|
15
|
+
invoke(action: string, input?: JsonValue): Promise<unknown>;
|
|
16
|
+
/** Invalidate first, cancel in-flight calls, then let the plugin stop its background work. */
|
|
17
|
+
close(): Promise<void>;
|
|
18
|
+
}
|
package/dist/surface.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { assertNativeView } from '@shiqianjiang/ceru-plugin-sdk';
|
|
2
|
+
/** Shared by preview and production hosts; no platform or login-specific behavior. */
|
|
3
|
+
export class SurfaceSession {
|
|
4
|
+
surface;
|
|
5
|
+
dispatch;
|
|
6
|
+
closed = false;
|
|
7
|
+
opened = false;
|
|
8
|
+
opening;
|
|
9
|
+
closing;
|
|
10
|
+
pending = new Set();
|
|
11
|
+
actions;
|
|
12
|
+
constructor(surface, manifest, dispatch) {
|
|
13
|
+
this.surface = surface;
|
|
14
|
+
this.dispatch = dispatch;
|
|
15
|
+
this.actions = new Set(manifest.contributes?.commands?.map((command) => command.action));
|
|
16
|
+
if (surface.kind === 'native' && !this.actions.has(surface.entry))
|
|
17
|
+
throw new Error('Missing native render action: ' + surface.entry);
|
|
18
|
+
for (const action of Object.values(surface.lifecycle ?? {}))
|
|
19
|
+
if (!this.actions.has(action))
|
|
20
|
+
throw new Error('Unknown surface lifecycle action: ' + action);
|
|
21
|
+
}
|
|
22
|
+
/** Call when the view is mounted, once even if the transport repeats its ready message. */
|
|
23
|
+
open() {
|
|
24
|
+
if (this.closed)
|
|
25
|
+
return Promise.reject(new Error('Surface is closed'));
|
|
26
|
+
if (!this.opened) {
|
|
27
|
+
this.opened = true;
|
|
28
|
+
this.opening = this.surface.lifecycle?.openAction
|
|
29
|
+
? this.invoke(this.surface.lifecycle.openAction, {})
|
|
30
|
+
: Promise.resolve();
|
|
31
|
+
}
|
|
32
|
+
return this.opening;
|
|
33
|
+
}
|
|
34
|
+
async invoke(action, input = {}) {
|
|
35
|
+
if (this.closed)
|
|
36
|
+
throw new Error('Surface is closed');
|
|
37
|
+
if (!this.actions.has(action))
|
|
38
|
+
throw new Error('Undeclared surface action: ' + action);
|
|
39
|
+
if (this.pending.size >= 32 || JSON.stringify(input).length > 128 * 1024)
|
|
40
|
+
throw new Error('Surface request exceeds limit');
|
|
41
|
+
const controller = new AbortController();
|
|
42
|
+
this.pending.add(controller);
|
|
43
|
+
try {
|
|
44
|
+
const result = await this.dispatch(action, input, controller.signal);
|
|
45
|
+
if (this.closed)
|
|
46
|
+
throw new Error('Surface is closed');
|
|
47
|
+
if (this.surface.kind === 'native' && action === this.surface.entry)
|
|
48
|
+
assertNativeView(result, this.actions);
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
this.pending.delete(controller);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Invalidate first, cancel in-flight calls, then let the plugin stop its background work. */
|
|
56
|
+
close() {
|
|
57
|
+
if (this.closing)
|
|
58
|
+
return this.closing;
|
|
59
|
+
this.closed = true;
|
|
60
|
+
for (const controller of this.pending)
|
|
61
|
+
controller.abort();
|
|
62
|
+
this.pending.clear();
|
|
63
|
+
const action = this.surface.lifecycle?.closeAction;
|
|
64
|
+
this.closing = Promise.resolve().then(async () => {
|
|
65
|
+
if (action)
|
|
66
|
+
await this.dispatch(action, {});
|
|
67
|
+
});
|
|
68
|
+
return this.closing;
|
|
69
|
+
}
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -1,63 +1,78 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@shiqianjiang/ceru-plugin-core",
|
|
3
|
-
"version": "0.2
|
|
4
|
-
"description": "Ceru Music v2 plugin Core runtime and Host adapter boundary",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./dist/index.js",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
1
|
+
{
|
|
2
|
+
"name": "@shiqianjiang/ceru-plugin-core",
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "Ceru Music v2 plugin Core runtime and Host adapter boundary",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
"./sockets": {
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
9
|
+
"./sockets": {
|
|
10
|
+
"types": "./dist/socket-broker.d.ts",
|
|
11
|
+
"default": "./dist/socket-broker.js"
|
|
12
|
+
},
|
|
13
|
+
"./node": {
|
|
14
|
+
"types": "./dist/node.d.ts",
|
|
15
|
+
"default": "./dist/node.js"
|
|
16
|
+
},
|
|
17
|
+
"./network": {
|
|
18
|
+
"types": "./dist/network.d.ts",
|
|
19
|
+
"default": "./dist/network.js"
|
|
20
|
+
},
|
|
21
|
+
"./electron": {
|
|
22
|
+
"types": "./dist/electron.d.ts",
|
|
23
|
+
"import": "./dist/electron.js",
|
|
24
|
+
"default": "./dist/electron.js"
|
|
25
|
+
},
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./guests": {
|
|
32
|
+
"types": "./dist/guest-store.d.ts",
|
|
33
|
+
"default": "./dist/guest-store.js"
|
|
34
|
+
},
|
|
35
|
+
"./surface": {
|
|
36
|
+
"types": "./dist/surface.d.ts",
|
|
37
|
+
"default": "./dist/surface.js"
|
|
38
|
+
},
|
|
39
|
+
"./surface-document": {
|
|
40
|
+
"types": "./dist/surface-document.d.ts",
|
|
41
|
+
"default": "./dist/surface-document.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"assets",
|
|
47
|
+
"README.md",
|
|
48
|
+
"LICENSE",
|
|
49
|
+
"NOTICE"
|
|
50
|
+
],
|
|
51
|
+
"license": "MIT AND AGPL-3.0-only",
|
|
52
|
+
"author": "澜音团队",
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=22.12.0"
|
|
55
|
+
},
|
|
41
56
|
"dependencies": {
|
|
42
57
|
"socket.io-client": "^4.8.3",
|
|
43
58
|
"ws": "^8.21.3",
|
|
44
|
-
"axios": "^1.13.2",
|
|
45
|
-
"@shiqianjiang/ceru-plugin-issuer": "0.2
|
|
46
|
-
"@shiqianjiang/ceru-plugin-sdk": "0.2
|
|
47
|
-
},
|
|
48
|
-
"publishConfig": {
|
|
49
|
-
"access": "public",
|
|
50
|
-
"registry": "https://registry.npmjs.org/"
|
|
51
|
-
},
|
|
52
|
-
"repository": {
|
|
53
|
-
"type": "git",
|
|
54
|
-
"url": "git+https://github.com/CeruMusic/CeruMusic-Plugin-Cli.git",
|
|
55
|
-
"directory": "packages/core"
|
|
56
|
-
},
|
|
57
|
-
"keywords": [
|
|
58
|
-
"ceru-music",
|
|
59
|
-
"plugin",
|
|
60
|
-
"core",
|
|
61
|
-
"electron"
|
|
62
|
-
]
|
|
63
|
-
}
|
|
59
|
+
"axios": "^1.13.2",
|
|
60
|
+
"@shiqianjiang/ceru-plugin-issuer": "0.3.2",
|
|
61
|
+
"@shiqianjiang/ceru-plugin-sdk": "0.3.2"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public",
|
|
65
|
+
"registry": "https://registry.npmjs.org/"
|
|
66
|
+
},
|
|
67
|
+
"repository": {
|
|
68
|
+
"type": "git",
|
|
69
|
+
"url": "git+https://github.com/CeruMusic/CeruMusic-Plugin-Cli.git",
|
|
70
|
+
"directory": "packages/core"
|
|
71
|
+
},
|
|
72
|
+
"keywords": [
|
|
73
|
+
"ceru-music",
|
|
74
|
+
"plugin",
|
|
75
|
+
"core",
|
|
76
|
+
"electron"
|
|
77
|
+
]
|
|
78
|
+
}
|