@shiqianjiang/ceru-plugin-issuer 0.2.1 → 0.2.3

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 CHANGED
@@ -21,4 +21,6 @@ console.log(readArtifact(bytes).signatureStatus)
21
21
 
22
22
  0.2.0 静态读取 `exports.manifest` / `exports.package`,并从 `exports.activate`、`exports.surfaces`、`exports.modules` 提取运行入口。旧 `CeruPlugin.define()` v2 容器仍可验证,但会返回迁移提示;v1 注释头只用于识别迁移来源,不能绕过 v2 Manifest 和权限模型直接运行。
23
23
 
24
+ `exports.manifest.config` 是同样可静态读取的 JSON 兼容对象。`createTemplate()` 默认从配置字段和类型生成 `personalization.config` 策略;`PreparedIssuer` 只能覆盖该策略允许的配置。`resolveArtifactConfig()` 递归合并清单默认值与签名交付值,业务代码与后端无需改写 bundle。
25
+
24
26
  [完整协议与命令](https://github.com/CeruMusic/CeruMusic-Plugin-Cli#readme)
package/dist/index.d.ts CHANGED
@@ -76,7 +76,17 @@ export declare function generateSigningKeys(): {
76
76
  };
77
77
  export declare function encodeHeader(header: ArtifactHeader): Buffer;
78
78
  export declare function encodeArtifact(header: ArtifactHeader, body: string): Buffer;
79
+ /** Resolves signed delivery configuration over the defaults embedded at build time. */
80
+ export declare function resolveArtifactConfig(header: ArtifactHeader): JsonObject;
81
+ /** Returns customer-facing metadata without changing plugin identity or permissions. */
82
+ export declare function resolveArtifactDisplay(header: ArtifactHeader): {
83
+ name: string;
84
+ description?: string;
85
+ author?: string;
86
+ };
79
87
  export declare const DEFAULT_PERSONALIZATION_SCHEMA: JsonObject;
88
+ /** Creates the default delivery policy from a plugin's build-time config shape. */
89
+ export declare function createPersonalizationSchema(config?: JsonObject): JsonObject;
80
90
  export declare function readArtifact(input: Uint8Array | string, options?: ValidationOptions): Artifact;
81
91
  export declare function signArtifact(input: Uint8Array, privateKey: string): Buffer;
82
92
  export declare function createTemplate(input: Uint8Array, options: {
package/dist/index.js CHANGED
@@ -128,6 +128,8 @@ export const MANIFEST_SCHEMA = object({
128
128
  author: string,
129
129
  publisher: string,
130
130
  license: string,
131
+ homepage: string,
132
+ config: { type: 'object', additionalProperties: true },
131
133
  engines: object({
132
134
  hostApi: string,
133
135
  logicRuntime: { const: 'ceru-js@1' },
@@ -257,6 +259,7 @@ export const MANIFEST_SCHEMA = object({
257
259
  id,
258
260
  name: string,
259
261
  protocols: strings,
262
+ qualities: strings,
260
263
  connectionMode: { enum: ['none', 'single', 'multiple'] },
261
264
  icon: {
262
265
  oneOf: [
@@ -420,10 +423,12 @@ function checkProof(proof, data) {
420
423
  }
421
424
  export function encodeHeader(header) {
422
425
  const { manifest, ...metadata } = header;
426
+ const literal = (value) => JSON.stringify(value, null, 2).replace(/^(\s*)"([A-Za-z_$][A-Za-z0-9_$]*)":/gm, '$1$2:');
423
427
  const head = Buffer.from('exports.manifest = ' +
424
- JSON.stringify(manifest, null, 2) +
425
- ';\nexports.package = ' +
426
- JSON.stringify(metadata, null, 2) +
428
+ literal(manifest) +
429
+ ';\n' +
430
+ 'exports.package = ' +
431
+ literal(metadata) +
427
432
  ';\n');
428
433
  ensure(head.length <= LIMITS.header, 'Artifact metadata is too large');
429
434
  return head;
@@ -433,6 +438,49 @@ export function encodeArtifact(header, body) {
433
438
  ensure(result.length <= LIMITS.file, 'Artifact exceeds size limit');
434
439
  return result;
435
440
  }
441
+ function mergeConfig(base, override) {
442
+ const result = JSON.parse(canonical(base));
443
+ for (const [key, value] of Object.entries(override)) {
444
+ const current = result[key];
445
+ result[key] =
446
+ plain(current) && plain(value)
447
+ ? mergeConfig(current, value)
448
+ : JSON.parse(canonical(value));
449
+ }
450
+ return result;
451
+ }
452
+ /** Resolves signed delivery configuration over the defaults embedded at build time. */
453
+ export function resolveArtifactConfig(header) {
454
+ const personalization = header.delivery?.payload.personalization;
455
+ const personalizationObject = personalization && typeof personalization === 'object' && !Array.isArray(personalization)
456
+ ? personalization
457
+ : undefined;
458
+ const config = personalizationObject?.['config'];
459
+ const override = plain(config)
460
+ ? config
461
+ : Object.create(null);
462
+ return mergeConfig(header.manifest.config ?? Object.create(null), override);
463
+ }
464
+ /** Returns customer-facing metadata without changing plugin identity or permissions. */
465
+ export function resolveArtifactDisplay(header) {
466
+ const personalization = header.delivery?.payload.personalization;
467
+ const display = plain(personalization) && plain(personalization.display)
468
+ ? personalization.display
469
+ : Object.create(null);
470
+ return {
471
+ name: typeof display.name === 'string' ? display.name : header.manifest.name,
472
+ ...(typeof display.description === 'string'
473
+ ? { description: display.description }
474
+ : header.manifest.description
475
+ ? { description: header.manifest.description }
476
+ : {}),
477
+ ...(typeof display.author === 'string'
478
+ ? { author: display.author }
479
+ : header.manifest.author
480
+ ? { author: header.manifest.author }
481
+ : {}),
482
+ };
483
+ }
436
484
  function checkPolicy(schema, depth = 0) {
437
485
  ensure(depth <= 12, 'Personalization schema is too deep');
438
486
  exactKeys(schema, [
@@ -471,6 +519,7 @@ export const DEFAULT_PERSONALIZATION_SCHEMA = {
471
519
  properties: {
472
520
  name: { type: 'string', minLength: 1, maxLength: 120 },
473
521
  description: { type: 'string', maxLength: 1024 },
522
+ author: { type: 'string', maxLength: 120 },
474
523
  },
475
524
  additionalProperties: false,
476
525
  },
@@ -488,6 +537,33 @@ export const DEFAULT_PERSONALIZATION_SCHEMA = {
488
537
  },
489
538
  additionalProperties: false,
490
539
  };
540
+ function configValueSchema(value, depth = 0) {
541
+ ensure(depth <= 12, 'Plugin config is too deeply nested for personalization');
542
+ if (value === null)
543
+ return { type: 'null' };
544
+ if (typeof value === 'string')
545
+ return { type: 'string', maxLength: 8192 };
546
+ if (typeof value === 'boolean')
547
+ return { type: 'boolean' };
548
+ if (typeof value === 'number')
549
+ return { type: Number.isInteger(value) ? 'integer' : 'number' };
550
+ if (Array.isArray(value))
551
+ return {
552
+ type: 'array',
553
+ items: configValueSchema(value[0] ?? '', depth + 1),
554
+ maxItems: 256,
555
+ };
556
+ const properties = Object.create(null);
557
+ for (const [key, child] of Object.entries(value))
558
+ properties[key] = configValueSchema(child, depth + 1);
559
+ return { type: 'object', properties, additionalProperties: false };
560
+ }
561
+ /** Creates the default delivery policy from a plugin's build-time config shape. */
562
+ export function createPersonalizationSchema(config = {}) {
563
+ const schema = JSON.parse(canonical(DEFAULT_PERSONALIZATION_SCHEMA));
564
+ schema.properties.config = configValueSchema(config);
565
+ return schema;
566
+ }
491
567
  function policyValidator(schema) {
492
568
  checkPolicy(schema);
493
569
  ensure(schema.type === 'object', 'Personalization root must be an object');
@@ -535,6 +611,35 @@ function inspectBody(body, manifest) {
535
611
  let registry;
536
612
  const preamble = [];
537
613
  const functions = new Map();
614
+ const bindings = new Map();
615
+ const selfContained = new Set();
616
+ const staticOrReference = (node, depth = 0) => {
617
+ ensure(depth <= 64, 'Static export nesting exceeds 64');
618
+ if (node.type === 'Identifier' || node.type === 'Literal')
619
+ return;
620
+ if (node.type === 'UnaryExpression' &&
621
+ node.operator === '-' &&
622
+ node.argument.type === 'Literal')
623
+ return;
624
+ if (node.type === 'ArrayExpression') {
625
+ for (const item of node.elements) {
626
+ ensure(item, 'Array holes are forbidden');
627
+ staticOrReference(item, depth + 1);
628
+ }
629
+ return;
630
+ }
631
+ for (const [, child] of astObject(node))
632
+ staticOrReference(child, depth + 1);
633
+ };
634
+ const resolveBinding = (node) => {
635
+ const seen = new Set();
636
+ while (node?.type === 'Identifier' && bindings.has(node.name)) {
637
+ ensure(!seen.has(node.name), 'Circular top-level binding: ' + node.name);
638
+ seen.add(node.name);
639
+ node = bindings.get(node.name);
640
+ }
641
+ return node;
642
+ };
538
643
  let sourceFormat = 'exports-v2';
539
644
  if (tree.body.length === 1 &&
540
645
  tree.body[0].type === 'ExpressionStatement' &&
@@ -571,9 +676,15 @@ function inspectBody(body, manifest) {
571
676
  const isStatic = init && ['Literal', 'ObjectExpression', 'ArrayExpression'].includes(init.type);
572
677
  ensure(isFunction || isRequire || isStatic, 'Top-level variables must be static data, functions, or require() imports');
573
678
  if (isStatic)
574
- astJson(init);
575
- if (declaration.id.type === 'Identifier' && isFunction)
576
- functions.set(declaration.id.name, init);
679
+ staticOrReference(init);
680
+ if (declaration.id.type === 'Identifier') {
681
+ bindings.set(declaration.id.name, init);
682
+ if (isFunction) {
683
+ functions.set(declaration.id.name, init);
684
+ if (body.slice(declaration.id.end, init.start).includes('@ceru-self-contained'))
685
+ selfContained.add(declaration.id.name);
686
+ }
687
+ }
577
688
  }
578
689
  preamble.push(body.slice(statement.start, statement.end));
579
690
  continue;
@@ -601,21 +712,24 @@ function inspectBody(body, manifest) {
601
712
  value: node,
602
713
  });
603
714
  if (exported.has('modules'))
604
- for (const [key, node] of astObject(exported.get('modules')))
715
+ for (const [key, node] of astObject(resolveBinding(exported.get('modules'))))
605
716
  addModule(key, node);
606
717
  if (exported.has('activate')) {
607
718
  ensure(manifest.modules.logic, 'activate requires modules.logic');
608
719
  addModule(manifest.modules.logic.entry, exported.get('activate'));
609
720
  }
610
721
  if (exported.has('surfaces'))
611
- for (const [id, node] of astObject(exported.get('surfaces'))) {
722
+ for (const [id, node] of astObject(resolveBinding(exported.get('surfaces')))) {
612
723
  const surface = manifest.modules.surfaces?.find((item) => item.id === id && item.kind === 'web');
613
724
  ensure(surface, 'Unknown visible surface: ' + id);
614
725
  addModule(surface.entry, node);
615
726
  }
616
727
  registry = new Map([
617
728
  ['modules', { type: 'ObjectExpression', properties }],
618
- ['resources', exported.get('resources') ?? { type: 'ObjectExpression', properties: [] }],
729
+ [
730
+ 'resources',
731
+ resolveBinding(exported.get('resources')) ?? { type: 'ObjectExpression', properties: [] },
732
+ ],
619
733
  ]);
620
734
  }
621
735
  const modules = Object.create(null);
@@ -627,7 +741,8 @@ function inspectBody(body, manifest) {
627
741
  !node.generator &&
628
742
  node.params.length <= 1 &&
629
743
  (!node.params.length || node.params[0].type === 'Identifier'), 'Module must be a function with one context parameter');
630
- modules[key] = preamble.length
744
+ const generatedEntry = exportedNode.type === 'Identifier' && selfContained.has(exportedNode.name);
745
+ modules[key] = preamble.length && !generatedEntry
631
746
  ? 'async function(ctx) {\n' +
632
747
  preamble.join('\n') +
633
748
  '\nreturn (' +
@@ -835,7 +950,7 @@ export function createTemplate(input, options) {
835
950
  const artifact = readArtifact(input);
836
951
  ensure(!artifact.header.template, 'Input must be a normal plugin artifact');
837
952
  const key = createPrivateKey(options.privateKey);
838
- const policy = options.personalizationSchema ?? DEFAULT_PERSONALIZATION_SCHEMA;
953
+ const policy = options.personalizationSchema ?? createPersonalizationSchema(artifact.header.manifest.config);
839
954
  policyValidator(policy);
840
955
  ensure(options.issuerPublicKeys.length > 0 && options.issuerPublicKeys.length <= 16, 'Provide 1-16 issuer public keys');
841
956
  const header = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shiqianjiang/ceru-plugin-issuer",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Static verification, Ed25519 signing and cached personalized issuance of Ceru single-JS plugins",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -23,7 +23,7 @@
23
23
  "node": ">=22.12.0"
24
24
  },
25
25
  "dependencies": {
26
- "@shiqianjiang/ceru-plugin-sdk": "0.2.1",
26
+ "@shiqianjiang/ceru-plugin-sdk": "0.2.3",
27
27
  "acorn": "^8.18.0",
28
28
  "ajv": "^8.20.0"
29
29
  },