@shiqianjiang/ceru-plugin-sdk 0.1.0 → 0.2.1
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/LICENSE +21 -21
- package/README.md +5 -3
- package/dist/catalog.d.ts +4 -1
- package/dist/catalog.js +30 -0
- package/dist/compat/crypto.d.ts +29 -0
- package/dist/compat/crypto.js +105 -0
- package/dist/compat/encoding.d.ts +4 -0
- package/dist/compat/encoding.js +1 -0
- package/dist/compat/format.d.ts +8 -0
- package/dist/compat/format.js +27 -0
- package/dist/compat/zlib.d.ts +8 -0
- package/dist/compat/zlib.js +12 -0
- package/dist/host-library.d.ts +20 -0
- package/dist/host-library.js +46 -0
- package/dist/host-modules.d.ts +47 -0
- package/dist/http.d.ts +46 -0
- package/dist/http.js +103 -0
- package/dist/index.d.ts +86 -12
- package/dist/index.js +7 -0
- package/dist/legacy-http.d.ts +25 -0
- package/dist/legacy-http.js +118 -0
- package/dist/library.d.ts +59 -0
- package/dist/library.js +1 -0
- package/dist/manifest.d.ts +46 -0
- package/dist/modules.d.ts +19 -0
- package/dist/modules.js +1 -0
- package/dist/music.d.ts +65 -0
- package/dist/music.js +109 -0
- package/dist/permissions.d.ts +149 -0
- package/dist/permissions.js +45 -0
- package/dist/services.d.ts +344 -0
- package/dist/services.js +22 -0
- package/dist/sockets.d.ts +25 -0
- package/dist/sockets.js +1 -0
- package/package.json +126 -57
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Ceru Music contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ceru Music contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
面向 Ceru Music v2 的 TypeScript 类型与小型作者辅助函数。
|
|
4
4
|
|
|
5
|
-
包含 PluginContext
|
|
5
|
+
包含 PluginContext、分组 Provider、歌曲/歌词/歌单标准数据、宿主服务、权限组、Surface、资源引用、结构化错误、UI Slot、图标名和类型化 Lodash。运行能力由 Host 提供,SDK 本身不授予权限。
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```ts
|
|
8
8
|
import { definePlugin, hostIcon } from '@shiqianjiang/ceru-plugin-sdk'
|
|
9
9
|
|
|
10
10
|
export default definePlugin((ctx) => {
|
|
@@ -15,8 +15,10 @@ export default definePlugin((ctx) => {
|
|
|
15
15
|
})
|
|
16
16
|
|
|
17
17
|
const icon = hostIcon('platform.tx')
|
|
18
|
-
|
|
18
|
+
```
|
|
19
19
|
|
|
20
20
|
动作需要在 Manifest 中声明。平台图标复用 Host 资源,不内嵌图片。
|
|
21
21
|
|
|
22
|
+
CLI 并非必需。插件也可以直接写成 `exports.manifest`、`exports.activate`、`exports.surfaces` 的单个 JavaScript 文件,通过 `require('@ceru/http')` 等内置模块使用相同能力。第三方 npm 包必须在发行前打入文件。
|
|
23
|
+
|
|
22
24
|
[完整指南与示例](https://github.com/CeruMusic/CeruMusic-Plugin-Cli#readme)
|
package/dist/catalog.d.ts
CHANGED
|
@@ -3,8 +3,11 @@ export declare const HOST_ICON_NAMES: readonly ["platform.tx", "platform.kg", "p
|
|
|
3
3
|
export type HostIconName = (typeof HOST_ICON_NAMES)[number];
|
|
4
4
|
export declare const HOST_ASSET_NAMES: readonly ["app.logo", "placeholder.cover", "placeholder.avatar"];
|
|
5
5
|
export type HostAssetName = (typeof HOST_ASSET_NAMES)[number];
|
|
6
|
-
export declare const PERMISSION_NAMES: readonly ["network.request", "network.private", "network.discovery", "credentials.use", "credentials.read", "library.read", "library.write", "library.delete", "player.read", "player.control", "playback.fallback.hold", "files.read", "files.write", "downloads.create", "clipboard.read", "clipboard.write", "notifications.system", "external.open", "background.run", "services.consume", "sharing.publish", "sharing.revoke", "guests.manage", "guests.run"];
|
|
6
|
+
export declare const PERMISSION_NAMES: readonly ["account.profile", "downloads.manage", "localMusic.read", "localMusic.write", "settings.read", "settings.write", "window.control", "hotkeys.register", "rooms.read", "rooms.control", "ai.use", "devices.control", "ui.styles.global", "network.request", "network.socket", "network.private", "network.discovery", "credentials.use", "credentials.read", "library.read", "library.write", "library.delete", "player.read", "player.control", "playback.fallback.hold", "files.read", "files.write", "downloads.create", "clipboard.read", "clipboard.write", "notifications.system", "external.open", "background.run", "services.consume", "sharing.publish", "sharing.revoke", "guests.manage", "guests.run"];
|
|
7
7
|
export type PermissionName = (typeof PERMISSION_NAMES)[number];
|
|
8
|
+
/** Runtime modules available to hand-written plugins without bundling. */
|
|
9
|
+
export declare const HOST_MODULE_NAMES: readonly ["ceru", "@ceru/http", "@ceru/ui", "@ceru/socket", "@ceru/library", "@ceru/account", "@ceru/player", "@ceru/tools", "@ceru/crypto", "@ceru/compression", "@ceru/encoding", "@ceru/legacy-http", "lodash"];
|
|
10
|
+
export type HostModuleName = (typeof HOST_MODULE_NAMES)[number];
|
|
8
11
|
export declare const LODASH_METHODS: readonly ["chunk", "compact", "concat", "difference", "drop", "dropRight", "flatten", "flattenDeep", "head", "last", "intersection", "uniq", "uniqBy", "union", "zip", "take", "takeRight", "groupBy", "keyBy", "countBy", "orderBy", "sortBy", "partition", "shuffle", "sample", "map", "filter", "find", "some", "every", "reduce", "includes", "get", "has", "pick", "omit", "mapKeys", "mapValues", "cloneDeep", "isEqual", "isEmpty", "isNil", "isString", "isNumber", "isArray", "isPlainObject", "camelCase", "kebabCase", "snakeCase", "startCase", "capitalize", "escape", "unescape", "trim", "truncate", "debounce", "throttle", "once", "memoize", "clamp", "range", "sum", "sumBy", "round"];
|
|
9
12
|
/** 引用宿主图标;例如 hostIcon('platform.tx'),不内嵌图片。 */
|
|
10
13
|
export declare function hostIcon(name: HostIconName): {
|
package/dist/catalog.js
CHANGED
|
@@ -28,7 +28,21 @@ export const HOST_ICON_NAMES = [
|
|
|
28
28
|
];
|
|
29
29
|
export const HOST_ASSET_NAMES = ['app.logo', 'placeholder.cover', 'placeholder.avatar'];
|
|
30
30
|
export const PERMISSION_NAMES = [
|
|
31
|
+
'account.profile',
|
|
32
|
+
'downloads.manage',
|
|
33
|
+
'localMusic.read',
|
|
34
|
+
'localMusic.write',
|
|
35
|
+
'settings.read',
|
|
36
|
+
'settings.write',
|
|
37
|
+
'window.control',
|
|
38
|
+
'hotkeys.register',
|
|
39
|
+
'rooms.read',
|
|
40
|
+
'rooms.control',
|
|
41
|
+
'ai.use',
|
|
42
|
+
'devices.control',
|
|
43
|
+
'ui.styles.global',
|
|
31
44
|
'network.request',
|
|
45
|
+
'network.socket',
|
|
32
46
|
'network.private',
|
|
33
47
|
'network.discovery',
|
|
34
48
|
'credentials.use',
|
|
@@ -53,6 +67,22 @@ export const PERMISSION_NAMES = [
|
|
|
53
67
|
'guests.manage',
|
|
54
68
|
'guests.run',
|
|
55
69
|
];
|
|
70
|
+
/** Runtime modules available to hand-written plugins without bundling. */
|
|
71
|
+
export const HOST_MODULE_NAMES = [
|
|
72
|
+
'ceru',
|
|
73
|
+
'@ceru/http',
|
|
74
|
+
'@ceru/ui',
|
|
75
|
+
'@ceru/socket',
|
|
76
|
+
'@ceru/library',
|
|
77
|
+
'@ceru/account',
|
|
78
|
+
'@ceru/player',
|
|
79
|
+
'@ceru/tools',
|
|
80
|
+
'@ceru/crypto',
|
|
81
|
+
'@ceru/compression',
|
|
82
|
+
'@ceru/encoding',
|
|
83
|
+
'@ceru/legacy-http',
|
|
84
|
+
'lodash',
|
|
85
|
+
];
|
|
56
86
|
export const LODASH_METHODS = [
|
|
57
87
|
'chunk',
|
|
58
88
|
'compact',
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function createHash(name: any): {
|
|
2
|
+
update(value: any): /*elided*/ any;
|
|
3
|
+
digest(encoding: any): string | Buffer<ArrayBuffer>;
|
|
4
|
+
};
|
|
5
|
+
export function publicEncrypt(options: any, value: any): Buffer<ArrayBuffer>;
|
|
6
|
+
export function createCipheriv(mode: any, key: any, iv: any): {
|
|
7
|
+
update(value: any): Buffer<ArrayBuffer>;
|
|
8
|
+
final(): Buffer<ArrayBuffer>;
|
|
9
|
+
};
|
|
10
|
+
export function createDecipheriv(mode: any, key: any, iv: any): {
|
|
11
|
+
update(value: any): Buffer<ArrayBuffer>;
|
|
12
|
+
final(): Buffer<ArrayBuffer>;
|
|
13
|
+
};
|
|
14
|
+
export function randomBytes(size: any): Buffer<ArrayBuffer>;
|
|
15
|
+
export function randomUUID(): `${string}-${string}-${string}-${string}-${string}`;
|
|
16
|
+
export namespace constants {
|
|
17
|
+
let RSA_NO_PADDING: number;
|
|
18
|
+
}
|
|
19
|
+
declare namespace _default {
|
|
20
|
+
export { createHash };
|
|
21
|
+
export { createCipheriv };
|
|
22
|
+
export { createDecipheriv };
|
|
23
|
+
export { publicEncrypt };
|
|
24
|
+
export { randomBytes };
|
|
25
|
+
export { randomUUID };
|
|
26
|
+
export { constants };
|
|
27
|
+
}
|
|
28
|
+
export default _default;
|
|
29
|
+
import { Buffer } from 'buffer';
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import CryptoJS from 'crypto-js';
|
|
2
|
+
import { Buffer } from 'buffer';
|
|
3
|
+
const words = (value) => CryptoJS.enc.Hex.parse(Buffer.from(value).toString('hex'));
|
|
4
|
+
const bytes = (value) => Buffer.from(value.toString(CryptoJS.enc.Hex), 'hex');
|
|
5
|
+
export function createHash(name) {
|
|
6
|
+
const factory = {
|
|
7
|
+
md5: CryptoJS.algo.MD5,
|
|
8
|
+
sha1: CryptoJS.algo.SHA1,
|
|
9
|
+
sha256: CryptoJS.algo.SHA256,
|
|
10
|
+
}[name.toLowerCase()];
|
|
11
|
+
if (!factory)
|
|
12
|
+
throw new Error('Unsupported digest: ' + name);
|
|
13
|
+
const hash = factory.create();
|
|
14
|
+
return {
|
|
15
|
+
update(value) {
|
|
16
|
+
hash.update(words(value));
|
|
17
|
+
return this;
|
|
18
|
+
},
|
|
19
|
+
digest(encoding) {
|
|
20
|
+
const result = bytes(hash.finalize());
|
|
21
|
+
return encoding ? result.toString(encoding) : result;
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function cipher(mode, key, iv, decrypt) {
|
|
26
|
+
if (!/^aes-(128|192|256)-(cbc|ecb)$/i.test(mode))
|
|
27
|
+
throw new Error('Unsupported cipher: ' + mode);
|
|
28
|
+
const chunks = [];
|
|
29
|
+
return {
|
|
30
|
+
update(value) {
|
|
31
|
+
chunks.push(Buffer.from(value));
|
|
32
|
+
return Buffer.alloc(0);
|
|
33
|
+
},
|
|
34
|
+
final() {
|
|
35
|
+
const data = words(Buffer.concat(chunks));
|
|
36
|
+
const options = {
|
|
37
|
+
mode: mode.endsWith('ecb') ? CryptoJS.mode.ECB : CryptoJS.mode.CBC,
|
|
38
|
+
iv: words(iv || ''),
|
|
39
|
+
padding: CryptoJS.pad.Pkcs7,
|
|
40
|
+
};
|
|
41
|
+
return bytes(decrypt
|
|
42
|
+
? CryptoJS.AES.decrypt({ ciphertext: data }, words(key), options)
|
|
43
|
+
: CryptoJS.AES.encrypt(data, words(key), options).ciphertext);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export const createCipheriv = (mode, key, iv) => cipher(mode, key, iv, false);
|
|
48
|
+
export const createDecipheriv = (mode, key, iv) => cipher(mode, key, iv, true);
|
|
49
|
+
export const randomBytes = (size) => Buffer.from(globalThis.crypto.getRandomValues(new Uint8Array(size)));
|
|
50
|
+
export const randomUUID = () => globalThis.crypto.randomUUID();
|
|
51
|
+
export const constants = { RSA_NO_PADDING: 3 };
|
|
52
|
+
// SPKI public-key parsing for the existing NetEase no-padding RSA request format.
|
|
53
|
+
export function publicEncrypt(options, value) {
|
|
54
|
+
if (options.padding !== constants.RSA_NO_PADDING)
|
|
55
|
+
throw new Error('Unsupported RSA padding');
|
|
56
|
+
const der = Buffer.from(options.key.replace(/-----[^-]+-----|\s/g, ''), 'base64');
|
|
57
|
+
let offset = 0;
|
|
58
|
+
function tlv() {
|
|
59
|
+
const tag = der[offset++];
|
|
60
|
+
let length = der[offset++];
|
|
61
|
+
if (length & 128) {
|
|
62
|
+
let count = length & 127;
|
|
63
|
+
length = 0;
|
|
64
|
+
while (count--)
|
|
65
|
+
length = length * 256 + der[offset++];
|
|
66
|
+
}
|
|
67
|
+
const start = offset;
|
|
68
|
+
offset += length;
|
|
69
|
+
if (offset > der.length)
|
|
70
|
+
throw new Error('Invalid RSA key');
|
|
71
|
+
return { tag, start, end: offset };
|
|
72
|
+
}
|
|
73
|
+
const outer = tlv();
|
|
74
|
+
offset = outer.start;
|
|
75
|
+
tlv();
|
|
76
|
+
const bits = tlv();
|
|
77
|
+
offset = bits.start + 1;
|
|
78
|
+
const sequence = tlv();
|
|
79
|
+
offset = sequence.start;
|
|
80
|
+
const modulus = tlv();
|
|
81
|
+
const exponent = tlv();
|
|
82
|
+
const n = BigInt('0x' + der.subarray(modulus.start, modulus.end).toString('hex'));
|
|
83
|
+
let e = BigInt('0x' + der.subarray(exponent.start, exponent.end).toString('hex'));
|
|
84
|
+
let base = BigInt('0x' + Buffer.from(value).toString('hex'));
|
|
85
|
+
let result = 1n;
|
|
86
|
+
if (base >= n)
|
|
87
|
+
throw new Error('RSA input exceeds modulus');
|
|
88
|
+
while (e) {
|
|
89
|
+
if (e & 1n)
|
|
90
|
+
result = (result * base) % n;
|
|
91
|
+
base = (base * base) % n;
|
|
92
|
+
e >>= 1n;
|
|
93
|
+
}
|
|
94
|
+
const size = Math.ceil(n.toString(16).length / 2);
|
|
95
|
+
return Buffer.from(result.toString(16).padStart(size * 2, '0'), 'hex');
|
|
96
|
+
}
|
|
97
|
+
export default {
|
|
98
|
+
createHash,
|
|
99
|
+
createCipheriv,
|
|
100
|
+
createDecipheriv,
|
|
101
|
+
publicEncrypt,
|
|
102
|
+
randomBytes,
|
|
103
|
+
randomUUID,
|
|
104
|
+
constants,
|
|
105
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default { decode: (bytes, encoding) => new TextDecoder(encoding).decode(bytes) };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function decodeName(value: any): string;
|
|
2
|
+
export function formatPlayTime(seconds: any): string;
|
|
3
|
+
export function sizeFormate(size: any): string;
|
|
4
|
+
export function formatPlayCount(value: any): string;
|
|
5
|
+
export function dateFormat(value: any): string;
|
|
6
|
+
export function dateFormat2(value: any): string;
|
|
7
|
+
export function formatNumberToChineseSimple(value: any): string;
|
|
8
|
+
export function formatMinutesFlexible(seconds: any): string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const decodeName = (value) => String(value ?? '').replace(/&(?:amp|lt|gt|quot|apos|nbsp);|&#(?:39|039);/g, (x) => ({
|
|
2
|
+
'&': '&',
|
|
3
|
+
'<': '<',
|
|
4
|
+
'>': '>',
|
|
5
|
+
'"': '"',
|
|
6
|
+
''': "'",
|
|
7
|
+
''': "'",
|
|
8
|
+
''': "'",
|
|
9
|
+
' ': ' ',
|
|
10
|
+
})[x]);
|
|
11
|
+
export const formatPlayTime = (seconds) => {
|
|
12
|
+
seconds = Math.max(0, Math.floor(Number(seconds) || 0));
|
|
13
|
+
return `${String(Math.floor(seconds / 60)).padStart(2, '0')}:${String(seconds % 60).padStart(2, '0')}`;
|
|
14
|
+
};
|
|
15
|
+
export const sizeFormate = (size) => {
|
|
16
|
+
const value = Number(size) || 0;
|
|
17
|
+
return value >= 1048576 ? (value / 1048576).toFixed(2) + ' MB' : (value / 1024).toFixed(2) + ' KB';
|
|
18
|
+
};
|
|
19
|
+
export const formatPlayCount = (value) => value >= 1e8
|
|
20
|
+
? (value / 1e8).toFixed(1) + '亿'
|
|
21
|
+
: value >= 1e4
|
|
22
|
+
? (value / 1e4).toFixed(1) + '万'
|
|
23
|
+
: String(value ?? 0);
|
|
24
|
+
export const dateFormat = (value) => new Date(value).toLocaleDateString('zh-CN');
|
|
25
|
+
export const dateFormat2 = dateFormat;
|
|
26
|
+
export const formatNumberToChineseSimple = (value) => formatPlayCount(value);
|
|
27
|
+
export const formatMinutesFlexible = (seconds) => formatPlayTime(seconds);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { unzlibSync, decompressSync } from 'fflate';
|
|
2
|
+
import { Buffer } from 'buffer';
|
|
3
|
+
export const unzipSync = (input) => Buffer.from(decompressSync(input));
|
|
4
|
+
export function inflate(input, callback) {
|
|
5
|
+
try {
|
|
6
|
+
callback(null, Buffer.from(unzlibSync(input)));
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
callback(error);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export default { unzipSync, inflate };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { LibraryAPI, LibraryPlaylist, PlaylistImportRequest, PlaylistLocation, PlaylistReference } from './library.js';
|
|
2
|
+
import type { ContentEntity, OperationContext, Page } from './index.js';
|
|
3
|
+
/** Bind these functions to existing app services. No database, account or UI is created here. */
|
|
4
|
+
export interface HostLibraryServices {
|
|
5
|
+
authorize(permissionKey: string, capability: 'library.read' | 'library.write', operation: OperationContext, target?: PlaylistReference): Promise<void>;
|
|
6
|
+
chooseTarget(suggestedName: string | undefined, operation: OperationContext): Promise<PlaylistReference | null>;
|
|
7
|
+
list(location: PlaylistLocation | undefined, cursor: string | undefined, operation: OperationContext): Promise<Page<LibraryPlaylist>>;
|
|
8
|
+
getTracks(target: PlaylistReference, cursor: string | undefined, operation: OperationContext): Promise<Page<ContentEntity>>;
|
|
9
|
+
/** Existing local/cloud service must deduplicate requestId per plugin/user/target. */
|
|
10
|
+
append(input: PlaylistImportRequest & {
|
|
11
|
+
target: PlaylistReference;
|
|
12
|
+
}): Promise<{
|
|
13
|
+
added: number;
|
|
14
|
+
skipped: number;
|
|
15
|
+
}>;
|
|
16
|
+
/** Notify the existing store/event bus only after persistence succeeds. */
|
|
17
|
+
changed(target: PlaylistReference): void;
|
|
18
|
+
}
|
|
19
|
+
/** Core-side validation/delegation. Pass only a Host-authenticated OperationContext. */
|
|
20
|
+
export declare function createHostLibraryBridge(services: HostLibraryServices): LibraryAPI;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { assertContentPage } from './music.js';
|
|
2
|
+
function checkTarget(target) {
|
|
3
|
+
if (!target ||
|
|
4
|
+
typeof target.id !== 'string' ||
|
|
5
|
+
!target.id ||
|
|
6
|
+
!['local', 'cloud'].includes(target.location))
|
|
7
|
+
throw new Error('Invalid target playlist');
|
|
8
|
+
}
|
|
9
|
+
/** Core-side validation/delegation. Pass only a Host-authenticated OperationContext. */
|
|
10
|
+
export function createHostLibraryBridge(services) {
|
|
11
|
+
return {
|
|
12
|
+
playlists: {
|
|
13
|
+
async list(input) {
|
|
14
|
+
input.operation.signal.throwIfAborted();
|
|
15
|
+
await services.authorize(input.permissionKey, 'library.read', input.operation);
|
|
16
|
+
return services.list(input.location, input.cursor, input.operation);
|
|
17
|
+
},
|
|
18
|
+
async getTracks(input) {
|
|
19
|
+
checkTarget(input.target);
|
|
20
|
+
await services.authorize(input.permissionKey, 'library.read', input.operation, input.target);
|
|
21
|
+
const result = await services.getTracks(input.target, input.cursor, input.operation);
|
|
22
|
+
assertContentPage(result);
|
|
23
|
+
return result;
|
|
24
|
+
},
|
|
25
|
+
async import(input) {
|
|
26
|
+
input.operation.signal.throwIfAborted();
|
|
27
|
+
assertContentPage({ items: input.items });
|
|
28
|
+
if (input.items.some((item) => item.ref.kind !== 'track'))
|
|
29
|
+
throw new Error('Only tracks can be imported');
|
|
30
|
+
if (typeof input.requestId !== 'string' || !input.requestId || input.requestId.length > 256)
|
|
31
|
+
throw new Error('Invalid import request ID');
|
|
32
|
+
await services.authorize(input.permissionKey, 'library.write', input.operation, input.target);
|
|
33
|
+
const target = input.target ?? (await services.chooseTarget(input.suggestedName, input.operation));
|
|
34
|
+
if (!target)
|
|
35
|
+
return { cancelled: true, added: 0, skipped: 0 };
|
|
36
|
+
checkTarget(target);
|
|
37
|
+
// The final user-selected destination may have a different grant/account scope.
|
|
38
|
+
await services.authorize(input.permissionKey, 'library.write', input.operation, target);
|
|
39
|
+
input.operation.signal.throwIfAborted();
|
|
40
|
+
const result = await services.append({ ...input, target });
|
|
41
|
+
services.changed(target);
|
|
42
|
+
return { cancelled: false, target, added: result.added, skipped: result.skipped };
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
declare module 'ceru' {
|
|
2
|
+
const core: import('./index.js').PluginContext
|
|
3
|
+
export = core
|
|
4
|
+
}
|
|
5
|
+
declare module '@ceru/http' {
|
|
6
|
+
const http: import('./index.js').PluginContext['http']
|
|
7
|
+
export = http
|
|
8
|
+
}
|
|
9
|
+
declare module '@ceru/ui' {
|
|
10
|
+
const ui: import('./index.js').PluginContext['ui']
|
|
11
|
+
export = ui
|
|
12
|
+
}
|
|
13
|
+
declare module '@ceru/socket' {
|
|
14
|
+
const sockets: import('./index.js').PluginContext['sockets']
|
|
15
|
+
export = sockets
|
|
16
|
+
}
|
|
17
|
+
declare module '@ceru/library' {
|
|
18
|
+
const library: import('./index.js').PluginContext['library']
|
|
19
|
+
export = library
|
|
20
|
+
}
|
|
21
|
+
declare module '@ceru/account' {
|
|
22
|
+
const account: import('./index.js').PluginContext['account']
|
|
23
|
+
export = account
|
|
24
|
+
}
|
|
25
|
+
declare module '@ceru/player' {
|
|
26
|
+
const player: import('./index.js').PluginContext['player']
|
|
27
|
+
export = player
|
|
28
|
+
}
|
|
29
|
+
declare module '@ceru/tools' {
|
|
30
|
+
const tools: import('./index.js').PluginContext['utils']
|
|
31
|
+
export = tools
|
|
32
|
+
}
|
|
33
|
+
declare module '@ceru/crypto' {
|
|
34
|
+
const crypto: typeof import('./compat/crypto.js')
|
|
35
|
+
export = crypto
|
|
36
|
+
}
|
|
37
|
+
declare module '@ceru/compression' {
|
|
38
|
+
const compression: typeof import('./compat/zlib.js')
|
|
39
|
+
export = compression
|
|
40
|
+
}
|
|
41
|
+
declare module '@ceru/encoding' {
|
|
42
|
+
const encoding: typeof import('./compat/encoding.js')
|
|
43
|
+
export = encoding
|
|
44
|
+
}
|
|
45
|
+
declare module '@ceru/legacy-http' {
|
|
46
|
+
export const createLegacyHttpBridge: typeof import('./legacy-http.js').createLegacyHttpBridge
|
|
47
|
+
}
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { CredentialRef, OperationContext, PluginContext } from './index.js';
|
|
2
|
+
export type QueryValue = string | number | boolean | null | undefined;
|
|
3
|
+
export interface HttpResponse<T> {
|
|
4
|
+
status: number;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
data: T;
|
|
7
|
+
}
|
|
8
|
+
export interface HttpRequestOptions {
|
|
9
|
+
operation: OperationContext;
|
|
10
|
+
permissionKey?: string;
|
|
11
|
+
query?: Record<string, QueryValue | QueryValue[]>;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
method?: string;
|
|
14
|
+
json?: unknown;
|
|
15
|
+
form?: Record<string, QueryValue>;
|
|
16
|
+
body?: string;
|
|
17
|
+
credential?: CredentialRef;
|
|
18
|
+
timeoutMs?: number;
|
|
19
|
+
/** Default: true. Set false to inspect non-2xx platform responses yourself. */
|
|
20
|
+
throwHttpErrors?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface HttpClientOptions {
|
|
23
|
+
baseURL?: string;
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
permissionKey?: string | ((url: URL) => string);
|
|
26
|
+
/** Prompt only when a permission is in the prompt state. Never bypass a denial. */
|
|
27
|
+
requestPermission?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare class HttpError<T = unknown> extends Error {
|
|
30
|
+
readonly response: HttpResponse<T>;
|
|
31
|
+
readonly origin: string;
|
|
32
|
+
readonly name = "HttpError";
|
|
33
|
+
constructor(response: HttpResponse<T>, origin: string);
|
|
34
|
+
get status(): number;
|
|
35
|
+
}
|
|
36
|
+
export declare function networkPermissionKey(origin: string): string;
|
|
37
|
+
/** Typed convenience layer over the Host broker. Does not use fetch or grant permissions. */
|
|
38
|
+
export declare function createHttpClient(ctx: {
|
|
39
|
+
http: Pick<PluginContext['http'], 'request'>;
|
|
40
|
+
permissions: Pick<PluginContext['permissions'], 'query' | 'request'>;
|
|
41
|
+
}, options: HttpClientOptions): {
|
|
42
|
+
request: <T = unknown>(address: string, input: HttpRequestOptions) => Promise<HttpResponse<T>>;
|
|
43
|
+
authorize: (key: string, _origin: string, operation: OperationContext) => Promise<void>;
|
|
44
|
+
get<T = unknown>(address: string, input: Omit<HttpRequestOptions, "method">): Promise<T>;
|
|
45
|
+
post<T = unknown>(address: string, input: Omit<HttpRequestOptions, "method">): Promise<T>;
|
|
46
|
+
};
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export class HttpError extends Error {
|
|
2
|
+
response;
|
|
3
|
+
origin;
|
|
4
|
+
name = 'HttpError';
|
|
5
|
+
constructor(response, origin) {
|
|
6
|
+
// URLs may contain credentials. Do not include query strings or headers in errors.
|
|
7
|
+
super(`${origin} returned HTTP ${response.status}`);
|
|
8
|
+
this.response = response;
|
|
9
|
+
this.origin = origin;
|
|
10
|
+
}
|
|
11
|
+
get status() {
|
|
12
|
+
return this.response.status;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function networkPermissionKey(origin) {
|
|
16
|
+
new URL(origin);
|
|
17
|
+
return 'network';
|
|
18
|
+
}
|
|
19
|
+
/** Typed convenience layer over the Host broker. Does not use fetch or grant permissions. */
|
|
20
|
+
export function createHttpClient(ctx, options) {
|
|
21
|
+
const prompts = new Map();
|
|
22
|
+
async function authorize(key, _origin, operation) {
|
|
23
|
+
operation.signal.throwIfAborted();
|
|
24
|
+
const state = await ctx.permissions.query({ key });
|
|
25
|
+
if (state.status === 'granted')
|
|
26
|
+
return;
|
|
27
|
+
if (state.status !== 'prompt' || !options.requestPermission)
|
|
28
|
+
throw new Error(`Network permission ${key}: ${state.status}`);
|
|
29
|
+
const promptKey = key;
|
|
30
|
+
let pending = prompts.get(promptKey);
|
|
31
|
+
if (!pending) {
|
|
32
|
+
pending = (async () => {
|
|
33
|
+
const result = await ctx.permissions.request({
|
|
34
|
+
key,
|
|
35
|
+
intent: operation.userIntent,
|
|
36
|
+
});
|
|
37
|
+
if (result.status !== 'granted')
|
|
38
|
+
throw new Error(`Network permission ${key}: ${result.status}`);
|
|
39
|
+
})().finally(() => prompts.delete(promptKey));
|
|
40
|
+
prompts.set(promptKey, pending);
|
|
41
|
+
}
|
|
42
|
+
await pending;
|
|
43
|
+
operation.signal.throwIfAborted();
|
|
44
|
+
}
|
|
45
|
+
async function request(address, input) {
|
|
46
|
+
const url = new URL(address, options.baseURL);
|
|
47
|
+
if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password)
|
|
48
|
+
throw new Error('Invalid HTTP URL');
|
|
49
|
+
for (const [name, value] of Object.entries(input.query ?? {})) {
|
|
50
|
+
url.searchParams.delete(name);
|
|
51
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
52
|
+
if (item != null)
|
|
53
|
+
url.searchParams.append(name, String(item));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if ([input.json, input.form, input.body].filter((value) => value !== undefined).length > 1)
|
|
57
|
+
throw new Error('Choose one of json, form or body');
|
|
58
|
+
const key = input.permissionKey ??
|
|
59
|
+
(typeof options.permissionKey === 'function'
|
|
60
|
+
? options.permissionKey(url)
|
|
61
|
+
: (options.permissionKey ?? 'network'));
|
|
62
|
+
await authorize(key, url.origin, input.operation);
|
|
63
|
+
const headers = { ...options.headers, ...input.headers };
|
|
64
|
+
let body = input.body;
|
|
65
|
+
if (input.json !== undefined) {
|
|
66
|
+
body = JSON.stringify(input.json);
|
|
67
|
+
if (!Object.keys(headers).some((name) => name.toLowerCase() === 'content-type'))
|
|
68
|
+
headers['Content-Type'] = 'application/json';
|
|
69
|
+
}
|
|
70
|
+
if (input.form) {
|
|
71
|
+
body = new URLSearchParams(Object.entries(input.form)
|
|
72
|
+
.filter(([, value]) => value != null)
|
|
73
|
+
.map(([name, value]) => [name, String(value)])).toString();
|
|
74
|
+
if (!Object.keys(headers).some((name) => name.toLowerCase() === 'content-type'))
|
|
75
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
76
|
+
}
|
|
77
|
+
const result = await ctx.http.request({
|
|
78
|
+
permissionKey: key,
|
|
79
|
+
url: url.href,
|
|
80
|
+
method: (input.method ?? 'GET').toUpperCase(),
|
|
81
|
+
headers,
|
|
82
|
+
body,
|
|
83
|
+
credential: input.credential,
|
|
84
|
+
timeoutMs: input.timeoutMs,
|
|
85
|
+
operation: input.operation,
|
|
86
|
+
});
|
|
87
|
+
input.operation.signal.throwIfAborted();
|
|
88
|
+
const response = { status: result.status, headers: result.headers, data: result.body };
|
|
89
|
+
if (input.throwHttpErrors !== false && (result.status < 200 || result.status >= 300))
|
|
90
|
+
throw new HttpError(response, url.origin);
|
|
91
|
+
return response;
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
request,
|
|
95
|
+
authorize,
|
|
96
|
+
async get(address, input) {
|
|
97
|
+
return (await request(address, { ...input, method: 'GET' })).data;
|
|
98
|
+
},
|
|
99
|
+
async post(address, input) {
|
|
100
|
+
return (await request(address, { ...input, method: 'POST' })).data;
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|