@things-factory/figure-service 10.1.288 → 10.1.290

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 (31) hide show
  1. package/dist-server/routers/figure-capability-module-router.d.ts +11 -0
  2. package/dist-server/routers/figure-capability-module-router.js +44 -0
  3. package/dist-server/routes.js +3 -0
  4. package/dist-server/service/capability-module/capability-module-entity.d.ts +52 -0
  5. package/dist-server/service/capability-module/capability-module-entity.js +173 -0
  6. package/dist-server/service/capability-module/capability-module-resolver.d.ts +24 -0
  7. package/dist-server/service/capability-module/capability-module-resolver.js +195 -0
  8. package/dist-server/service/capability-module/capability-module-store.d.ts +109 -0
  9. package/dist-server/service/capability-module/capability-module-store.js +325 -0
  10. package/dist-server/service/capability-module/capability-release-check.d.ts +8 -0
  11. package/dist-server/service/capability-module/capability-release-check.js +57 -0
  12. package/dist-server/service/figure/figure-format.d.ts +9 -1
  13. package/dist-server/service/figure/figure-format.js +4 -2
  14. package/dist-server/service/figure/figure-mcp-agent.d.ts +6 -1
  15. package/dist-server/service/figure/figure-mcp-agent.js +2 -1
  16. package/dist-server/service/figure/figure-mutation.js +6 -1
  17. package/dist-server/service/figure/figure-propose-steps.d.ts +12 -2
  18. package/dist-server/service/figure/figure-propose-steps.js +46 -3
  19. package/dist-server/service/figure/figure-query.js +6 -3
  20. package/dist-server/service/figure/figure-tools.js +12 -2
  21. package/dist-server/service/index.d.ts +8 -2
  22. package/dist-server/service/index.js +15 -3
  23. package/dist-server/service/install-key/figure-install-key.d.ts +5 -2
  24. package/dist-server/service/install-key/figure-install-key.js +22 -6
  25. package/package.json +11 -8
  26. package/translations/en.json +18 -1
  27. package/translations/es.json +18 -1
  28. package/translations/ja.json +18 -1
  29. package/translations/ko.json +18 -1
  30. package/translations/ms.json +18 -1
  31. package/translations/zh.json +18 -1
@@ -0,0 +1,11 @@
1
+ import type { ThumbnailFiles } from '../service/figure/figure-thumbnail-store.js';
2
+ /**
3
+ * A capability module's file, by its hash (ADR-0172 decision 6): what the modeller imports, on its own origin. Served
4
+ * only to a domain that uses the module's provider, behind the figure query permission. The content is its hash, so it
5
+ * never changes: kept by the browser as long as it keeps anything (privately: it is served behind a login).
6
+ */
7
+ export declare const figureCapabilityModuleRouter: any;
8
+ /** The file of a module the domain uses. Exported for tests, which give their own `files`. */
9
+ export declare function sendCapabilityModule(context: any, file: string, domain: {
10
+ id: string;
11
+ }, files?: ThumbnailFiles): Promise<void>;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.figureCapabilityModuleRouter = void 0;
4
+ exports.sendCapabilityModule = sendCapabilityModule;
5
+ const tslib_1 = require("tslib");
6
+ const koa_router_1 = tslib_1.__importDefault(require("koa-router"));
7
+ const auth_base_1 = require("@things-factory/auth-base");
8
+ const shell_1 = require("@things-factory/shell");
9
+ const capability_module_store_js_1 = require("../service/capability-module/capability-module-store.js");
10
+ const capability_module_store_js_2 = require("../service/capability-module/capability-module-store.js");
11
+ /**
12
+ * A capability module's file, by its hash (ADR-0172 decision 6): what the modeller imports, on its own origin. Served
13
+ * only to a domain that uses the module's provider, behind the figure query permission. The content is its hash, so it
14
+ * never changes: kept by the browser as long as it keeps anything (privately: it is served behind a login).
15
+ */
16
+ exports.figureCapabilityModuleRouter = new koa_router_1.default();
17
+ exports.figureCapabilityModuleRouter.get('/figure-capability-modules/:file', async (context, next) => {
18
+ const { domain, user, unsafeIP, prohibitedPrivileges } = context.state;
19
+ const allowed = await (0, auth_base_1.checkPermission)({ category: 'figure', privilege: 'query', owner: true }, user, domain, unsafeIP, prohibitedPrivileges);
20
+ if (!allowed) {
21
+ context.status = 403;
22
+ return;
23
+ }
24
+ await sendCapabilityModule(context, context.params.file, domain);
25
+ });
26
+ /** The file of a module the domain uses. Exported for tests, which give their own `files`. */
27
+ async function sendCapabilityModule(context, file, domain, files) {
28
+ const sha256 = /^([0-9a-f]{64})\.mjs$/.exec(file)?.[1];
29
+ const bytes = sha256 ? await (0, capability_module_store_js_1.chosenModuleBytes)({ getRepository: shell_1.getRepository }, domain, sha256, files) : undefined;
30
+ if (!bytes) {
31
+ context.status = 404;
32
+ return;
33
+ }
34
+ context.type = capability_module_store_js_2.MODULE_MIMETYPE;
35
+ context.set('Cache-Control', 'private, max-age=31536000, immutable');
36
+ context.set('ETag', `"${sha256}"`);
37
+ context.status = 200;
38
+ if (context.fresh) {
39
+ context.status = 304;
40
+ return;
41
+ }
42
+ context.body = bytes;
43
+ }
44
+ //# sourceMappingURL=figure-capability-module-router.js.map
@@ -5,6 +5,7 @@ const shell_1 = require("@things-factory/shell");
5
5
  const figure_thumbnail_router_js_1 = require("./routers/figure-thumbnail-router.js");
6
6
  const figure_package_router_js_1 = require("./routers/figure-package-router.js");
7
7
  const figure_mesh_router_js_1 = require("./routers/figure-mesh-router.js");
8
+ const figure_capability_module_router_js_1 = require("./routers/figure-capability-module-router.js");
8
9
  /**
9
10
  * 도메인 경로가 주소에 들어가는 배치인가.
10
11
  *
@@ -17,11 +18,13 @@ process.on('bootstrap-module-domain-private-route', (app, domainPrivateRouter) =
17
18
  domainPrivateRouter.use(`/${(0, shell_1.getRoutePrefixForDomainType)()}/:domain`, figure_thumbnail_router_js_1.figureThumbnailRouter.routes(), figure_thumbnail_router_js_1.figureThumbnailRouter.allowedMethods());
18
19
  domainPrivateRouter.use(`/${(0, shell_1.getRoutePrefixForDomainType)()}/:domain`, figure_package_router_js_1.figurePackageRouter.routes(), figure_package_router_js_1.figurePackageRouter.allowedMethods());
19
20
  domainPrivateRouter.use(`/${(0, shell_1.getRoutePrefixForDomainType)()}/:domain`, figure_mesh_router_js_1.figureMeshRouter.routes(), figure_mesh_router_js_1.figureMeshRouter.allowedMethods());
21
+ domainPrivateRouter.use(`/${(0, shell_1.getRoutePrefixForDomainType)()}/:domain`, figure_capability_module_router_js_1.figureCapabilityModuleRouter.routes(), figure_capability_module_router_js_1.figureCapabilityModuleRouter.allowedMethods());
20
22
  }
21
23
  else {
22
24
  domainPrivateRouter.use('', figure_thumbnail_router_js_1.figureThumbnailRouter.routes(), figure_thumbnail_router_js_1.figureThumbnailRouter.allowedMethods());
23
25
  domainPrivateRouter.use('', figure_package_router_js_1.figurePackageRouter.routes(), figure_package_router_js_1.figurePackageRouter.allowedMethods());
24
26
  domainPrivateRouter.use('', figure_mesh_router_js_1.figureMeshRouter.routes(), figure_mesh_router_js_1.figureMeshRouter.allowedMethods());
27
+ domainPrivateRouter.use('', figure_capability_module_router_js_1.figureCapabilityModuleRouter.routes(), figure_capability_module_router_js_1.figureCapabilityModuleRouter.allowedMethods());
25
28
  }
26
29
  });
27
30
  //# sourceMappingURL=routes.js.map
@@ -0,0 +1,52 @@
1
+ import { User } from '@things-factory/auth-base';
2
+ import { Domain } from '@things-factory/shell';
3
+ /**
4
+ * One version of a capability module (ADR-0172 decisions 3, 4 and 15): one provider's ESM file, kept in attachment-base
5
+ * by its SHA-256. A module with no domain is the system's, registered by a superuser; one with a domain was pushed by
6
+ * that domain's consuming app over its figure server link and is that domain's alone. A provider has one current
7
+ * version per owner.
8
+ */
9
+ export declare class FigureCapabilityModule {
10
+ readonly id?: string;
11
+ domain?: Domain | null;
12
+ domainId?: string | null;
13
+ provider?: string;
14
+ version?: string;
15
+ sha256?: string;
16
+ capabilities?: string[];
17
+ current?: boolean;
18
+ createdAt?: Date;
19
+ creator?: User;
20
+ creatorId?: string;
21
+ }
22
+ /** A provider a domain chose (ADR-0172 decision 5): its modeller loads it and its releases are checked by it. */
23
+ export declare class FigureCapabilityUse {
24
+ readonly id?: string;
25
+ domain?: Domain;
26
+ domainId?: string;
27
+ provider?: string;
28
+ createdAt?: Date;
29
+ creator?: User;
30
+ }
31
+ /**
32
+ * The choice lists a domain's own provider pushed with its module (ADR-0172 amendment 2, decisions 19 and 20): the
33
+ * last ones received, by `<capability>.<setting>`. They change apart from the module's version.
34
+ */
35
+ export declare class FigureCapabilityChoiceLists {
36
+ readonly id?: string;
37
+ domain?: Domain;
38
+ provider?: string;
39
+ choices?: Record<string, unknown[]>;
40
+ updatedAt?: Date;
41
+ }
42
+ /**
43
+ * What an install key may push besides reading released figures (ADR-0172 decision 14): the capability module of one
44
+ * provider. A key without this row pushes nothing; the domain owner chooses it when issuing the key.
45
+ */
46
+ export declare class FigureInstallKeyCapability {
47
+ readonly id?: string;
48
+ domain?: Domain;
49
+ /** The install key (an appliance) */
50
+ applianceId?: string;
51
+ provider?: string;
52
+ }
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FigureInstallKeyCapability = exports.FigureCapabilityChoiceLists = exports.FigureCapabilityUse = exports.FigureCapabilityModule = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const typeorm_1 = require("typeorm");
6
+ const type_graphql_1 = require("type-graphql");
7
+ const auth_base_1 = require("@things-factory/auth-base");
8
+ const shell_1 = require("@things-factory/shell");
9
+ /**
10
+ * One version of a capability module (ADR-0172 decisions 3, 4 and 15): one provider's ESM file, kept in attachment-base
11
+ * by its SHA-256. A module with no domain is the system's, registered by a superuser; one with a domain was pushed by
12
+ * that domain's consuming app over its figure server link and is that domain's alone. A provider has one current
13
+ * version per owner.
14
+ */
15
+ let FigureCapabilityModule = class FigureCapabilityModule {
16
+ };
17
+ exports.FigureCapabilityModule = FigureCapabilityModule;
18
+ tslib_1.__decorate([
19
+ (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
20
+ (0, type_graphql_1.Field)(type => type_graphql_1.ID, { description: 'Unique identifier.' }),
21
+ tslib_1.__metadata("design:type", String)
22
+ ], FigureCapabilityModule.prototype, "id", void 0);
23
+ tslib_1.__decorate([
24
+ (0, typeorm_1.ManyToOne)(type => shell_1.Domain, { nullable: true }),
25
+ (0, type_graphql_1.Field)(type => shell_1.Domain, { nullable: true, description: "The domain whose app pushed it over its link; none for the system's." }),
26
+ tslib_1.__metadata("design:type", shell_1.Domain)
27
+ ], FigureCapabilityModule.prototype, "domain", void 0);
28
+ tslib_1.__decorate([
29
+ (0, typeorm_1.RelationId)((m) => m.domain),
30
+ tslib_1.__metadata("design:type", String)
31
+ ], FigureCapabilityModule.prototype, "domainId", void 0);
32
+ tslib_1.__decorate([
33
+ (0, typeorm_1.Column)(),
34
+ (0, type_graphql_1.Field)({ description: 'The capability provider: the part of its capability names before the dot.' }),
35
+ tslib_1.__metadata("design:type", String)
36
+ ], FigureCapabilityModule.prototype, "provider", void 0);
37
+ tslib_1.__decorate([
38
+ (0, typeorm_1.Column)(),
39
+ (0, type_graphql_1.Field)({ description: "The module's version (semver)." }),
40
+ tslib_1.__metadata("design:type", String)
41
+ ], FigureCapabilityModule.prototype, "version", void 0);
42
+ tslib_1.__decorate([
43
+ (0, typeorm_1.Column)(),
44
+ (0, type_graphql_1.Field)({ description: "The SHA-256 of the file: the file's address." }),
45
+ tslib_1.__metadata("design:type", String)
46
+ ], FigureCapabilityModule.prototype, "sha256", void 0);
47
+ tslib_1.__decorate([
48
+ (0, typeorm_1.Column)('simple-json'),
49
+ (0, type_graphql_1.Field)(type => [String], { description: 'The capability names the module gives.' }),
50
+ tslib_1.__metadata("design:type", Array)
51
+ ], FigureCapabilityModule.prototype, "capabilities", void 0);
52
+ tslib_1.__decorate([
53
+ (0, typeorm_1.Column)({ default: false }),
54
+ (0, type_graphql_1.Field)({ description: 'Whether this is the version of the provider in use.' }),
55
+ tslib_1.__metadata("design:type", Boolean)
56
+ ], FigureCapabilityModule.prototype, "current", void 0);
57
+ tslib_1.__decorate([
58
+ (0, typeorm_1.CreateDateColumn)(),
59
+ (0, type_graphql_1.Field)({ nullable: true, description: 'When it was registered.' }),
60
+ tslib_1.__metadata("design:type", Date)
61
+ ], FigureCapabilityModule.prototype, "createdAt", void 0);
62
+ tslib_1.__decorate([
63
+ (0, typeorm_1.ManyToOne)(type => auth_base_1.User, { nullable: true }),
64
+ (0, type_graphql_1.Field)(type => auth_base_1.User, { nullable: true, description: 'Who registered it.' }),
65
+ tslib_1.__metadata("design:type", auth_base_1.User)
66
+ ], FigureCapabilityModule.prototype, "creator", void 0);
67
+ tslib_1.__decorate([
68
+ (0, typeorm_1.RelationId)((m) => m.creator),
69
+ tslib_1.__metadata("design:type", String)
70
+ ], FigureCapabilityModule.prototype, "creatorId", void 0);
71
+ exports.FigureCapabilityModule = FigureCapabilityModule = tslib_1.__decorate([
72
+ (0, typeorm_1.Entity)(),
73
+ (0, typeorm_1.Index)('ix_figure_capability_module_0', (m) => [m.provider, m.version, m.domain]),
74
+ (0, typeorm_1.Index)('ix_figure_capability_module_1', (m) => [m.sha256]),
75
+ (0, type_graphql_1.ObjectType)({ description: "One version of a capability module: a provider's descriptors in one ESM file." })
76
+ ], FigureCapabilityModule);
77
+ /** A provider a domain chose (ADR-0172 decision 5): its modeller loads it and its releases are checked by it. */
78
+ let FigureCapabilityUse = class FigureCapabilityUse {
79
+ };
80
+ exports.FigureCapabilityUse = FigureCapabilityUse;
81
+ tslib_1.__decorate([
82
+ (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
83
+ (0, type_graphql_1.Field)(type => type_graphql_1.ID, { description: 'Unique identifier.' }),
84
+ tslib_1.__metadata("design:type", String)
85
+ ], FigureCapabilityUse.prototype, "id", void 0);
86
+ tslib_1.__decorate([
87
+ (0, typeorm_1.ManyToOne)(type => shell_1.Domain),
88
+ tslib_1.__metadata("design:type", shell_1.Domain)
89
+ ], FigureCapabilityUse.prototype, "domain", void 0);
90
+ tslib_1.__decorate([
91
+ (0, typeorm_1.RelationId)((u) => u.domain),
92
+ tslib_1.__metadata("design:type", String)
93
+ ], FigureCapabilityUse.prototype, "domainId", void 0);
94
+ tslib_1.__decorate([
95
+ (0, typeorm_1.Column)(),
96
+ (0, type_graphql_1.Field)({ description: 'The provider the domain uses.' }),
97
+ tslib_1.__metadata("design:type", String)
98
+ ], FigureCapabilityUse.prototype, "provider", void 0);
99
+ tslib_1.__decorate([
100
+ (0, typeorm_1.CreateDateColumn)(),
101
+ (0, type_graphql_1.Field)({ nullable: true, description: 'When it was registered.' }),
102
+ tslib_1.__metadata("design:type", Date)
103
+ ], FigureCapabilityUse.prototype, "createdAt", void 0);
104
+ tslib_1.__decorate([
105
+ (0, typeorm_1.ManyToOne)(type => auth_base_1.User, { nullable: true }),
106
+ tslib_1.__metadata("design:type", auth_base_1.User)
107
+ ], FigureCapabilityUse.prototype, "creator", void 0);
108
+ exports.FigureCapabilityUse = FigureCapabilityUse = tslib_1.__decorate([
109
+ (0, typeorm_1.Entity)(),
110
+ (0, typeorm_1.Index)('ix_figure_capability_use_0', (u) => [u.domain, u.provider], { unique: true }),
111
+ (0, type_graphql_1.ObjectType)({ description: 'A capability provider a domain uses.' })
112
+ ], FigureCapabilityUse);
113
+ /**
114
+ * The choice lists a domain's own provider pushed with its module (ADR-0172 amendment 2, decisions 19 and 20): the
115
+ * last ones received, by `<capability>.<setting>`. They change apart from the module's version.
116
+ */
117
+ let FigureCapabilityChoiceLists = class FigureCapabilityChoiceLists {
118
+ };
119
+ exports.FigureCapabilityChoiceLists = FigureCapabilityChoiceLists;
120
+ tslib_1.__decorate([
121
+ (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
122
+ tslib_1.__metadata("design:type", String)
123
+ ], FigureCapabilityChoiceLists.prototype, "id", void 0);
124
+ tslib_1.__decorate([
125
+ (0, typeorm_1.ManyToOne)(type => shell_1.Domain),
126
+ tslib_1.__metadata("design:type", shell_1.Domain)
127
+ ], FigureCapabilityChoiceLists.prototype, "domain", void 0);
128
+ tslib_1.__decorate([
129
+ (0, typeorm_1.Column)(),
130
+ tslib_1.__metadata("design:type", String)
131
+ ], FigureCapabilityChoiceLists.prototype, "provider", void 0);
132
+ tslib_1.__decorate([
133
+ (0, typeorm_1.Column)('simple-json'),
134
+ tslib_1.__metadata("design:type", Object)
135
+ ], FigureCapabilityChoiceLists.prototype, "choices", void 0);
136
+ tslib_1.__decorate([
137
+ (0, typeorm_1.UpdateDateColumn)(),
138
+ tslib_1.__metadata("design:type", Date)
139
+ ], FigureCapabilityChoiceLists.prototype, "updatedAt", void 0);
140
+ exports.FigureCapabilityChoiceLists = FigureCapabilityChoiceLists = tslib_1.__decorate([
141
+ (0, typeorm_1.Entity)(),
142
+ (0, typeorm_1.Index)('ix_figure_capability_choices_0', (c) => [c.domain, c.provider], { unique: true })
143
+ ], FigureCapabilityChoiceLists);
144
+ /**
145
+ * What an install key may push besides reading released figures (ADR-0172 decision 14): the capability module of one
146
+ * provider. A key without this row pushes nothing; the domain owner chooses it when issuing the key.
147
+ */
148
+ let FigureInstallKeyCapability = class FigureInstallKeyCapability {
149
+ };
150
+ exports.FigureInstallKeyCapability = FigureInstallKeyCapability;
151
+ tslib_1.__decorate([
152
+ (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
153
+ tslib_1.__metadata("design:type", String)
154
+ ], FigureInstallKeyCapability.prototype, "id", void 0);
155
+ tslib_1.__decorate([
156
+ (0, typeorm_1.ManyToOne)(type => shell_1.Domain),
157
+ tslib_1.__metadata("design:type", shell_1.Domain
158
+ /** The install key (an appliance) */
159
+ )
160
+ ], FigureInstallKeyCapability.prototype, "domain", void 0);
161
+ tslib_1.__decorate([
162
+ (0, typeorm_1.Column)(),
163
+ tslib_1.__metadata("design:type", String)
164
+ ], FigureInstallKeyCapability.prototype, "applianceId", void 0);
165
+ tslib_1.__decorate([
166
+ (0, typeorm_1.Column)(),
167
+ tslib_1.__metadata("design:type", String)
168
+ ], FigureInstallKeyCapability.prototype, "provider", void 0);
169
+ exports.FigureInstallKeyCapability = FigureInstallKeyCapability = tslib_1.__decorate([
170
+ (0, typeorm_1.Entity)(),
171
+ (0, typeorm_1.Index)('ix_figure_install_key_capability_0', (g) => [g.applianceId], { unique: true })
172
+ ], FigureInstallKeyCapability);
173
+ //# sourceMappingURL=capability-module-entity.js.map
@@ -0,0 +1,24 @@
1
+ import type { FileUpload } from 'graphql-upload/GraphQLUpload.js';
2
+ import { FigureCapabilityModule } from './capability-module-entity.js';
3
+ export declare class FigureCapabilityModuleRef {
4
+ provider: string;
5
+ version: string;
6
+ sha256: string;
7
+ capabilities: string[];
8
+ }
9
+ export declare class FigureCapabilityProvider {
10
+ provider: string;
11
+ current: FigureCapabilityModule;
12
+ own: boolean;
13
+ chosen: boolean;
14
+ versions: FigureCapabilityModule[];
15
+ }
16
+ export declare class FigureCapabilityModuleResolver {
17
+ figureCapabilityModules(context: ResolverContext): Promise<FigureCapabilityModuleRef[]>;
18
+ figureCapabilityChoices(context: ResolverContext): Promise<Record<string, unknown>>;
19
+ figureCapabilityProviders(context: ResolverContext): Promise<FigureCapabilityProvider[]>;
20
+ useFigureCapabilityProvider(provider: string, on: boolean, context: ResolverContext): Promise<boolean>;
21
+ registerFigureCapabilityModule(file: FileUpload, context: ResolverContext): Promise<FigureCapabilityModule>;
22
+ setCurrentFigureCapabilityModule(provider: string, version: string, context: ResolverContext): Promise<FigureCapabilityModule>;
23
+ pushFigureCapabilityModule(source: string, context: ResolverContext, choices?: Record<string, unknown>): Promise<FigureCapabilityModuleRef>;
24
+ }
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FigureCapabilityModuleResolver = exports.FigureCapabilityProvider = exports.FigureCapabilityModuleRef = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const type_graphql_1 = require("type-graphql");
6
+ const GraphQLUpload_js_1 = tslib_1.__importDefault(require("graphql-upload/GraphQLUpload.js"));
7
+ const shell_1 = require("@things-factory/shell");
8
+ const capability_module_entity_js_1 = require("./capability-module-entity.js");
9
+ const capability_module_store_js_1 = require("./capability-module-store.js");
10
+ let FigureCapabilityModuleRef = class FigureCapabilityModuleRef {
11
+ };
12
+ exports.FigureCapabilityModuleRef = FigureCapabilityModuleRef;
13
+ tslib_1.__decorate([
14
+ (0, type_graphql_1.Field)({ description: 'The capability provider.' }),
15
+ tslib_1.__metadata("design:type", String)
16
+ ], FigureCapabilityModuleRef.prototype, "provider", void 0);
17
+ tslib_1.__decorate([
18
+ (0, type_graphql_1.Field)({ description: "The module's version." }),
19
+ tslib_1.__metadata("design:type", String)
20
+ ], FigureCapabilityModuleRef.prototype, "version", void 0);
21
+ tslib_1.__decorate([
22
+ (0, type_graphql_1.Field)({ description: "The SHA-256 of the file: it is served at /figure-capability-modules/<sha256>.mjs." }),
23
+ tslib_1.__metadata("design:type", String)
24
+ ], FigureCapabilityModuleRef.prototype, "sha256", void 0);
25
+ tslib_1.__decorate([
26
+ (0, type_graphql_1.Field)(type => [String], { description: 'The capability names it gives.' }),
27
+ tslib_1.__metadata("design:type", Array)
28
+ ], FigureCapabilityModuleRef.prototype, "capabilities", void 0);
29
+ exports.FigureCapabilityModuleRef = FigureCapabilityModuleRef = tslib_1.__decorate([
30
+ (0, type_graphql_1.ObjectType)({ description: 'A capability module a domain uses, as the modeller loads it.' })
31
+ ], FigureCapabilityModuleRef);
32
+ let FigureCapabilityProvider = class FigureCapabilityProvider {
33
+ };
34
+ exports.FigureCapabilityProvider = FigureCapabilityProvider;
35
+ tslib_1.__decorate([
36
+ (0, type_graphql_1.Field)({ description: 'The capability provider.' }),
37
+ tslib_1.__metadata("design:type", String)
38
+ ], FigureCapabilityProvider.prototype, "provider", void 0);
39
+ tslib_1.__decorate([
40
+ (0, type_graphql_1.Field)(type => capability_module_entity_js_1.FigureCapabilityModule, { description: 'Its current version.' }),
41
+ tslib_1.__metadata("design:type", capability_module_entity_js_1.FigureCapabilityModule)
42
+ ], FigureCapabilityProvider.prototype, "current", void 0);
43
+ tslib_1.__decorate([
44
+ (0, type_graphql_1.Field)({ description: "Whether this domain's app pushed it over its figure server link; else it is the system's." }),
45
+ tslib_1.__metadata("design:type", Boolean)
46
+ ], FigureCapabilityProvider.prototype, "own", void 0);
47
+ tslib_1.__decorate([
48
+ (0, type_graphql_1.Field)({ description: 'Whether this domain uses it.' }),
49
+ tslib_1.__metadata("design:type", Boolean)
50
+ ], FigureCapabilityProvider.prototype, "chosen", void 0);
51
+ tslib_1.__decorate([
52
+ (0, type_graphql_1.Field)(type => [capability_module_entity_js_1.FigureCapabilityModule], { description: 'Every version kept, latest first.' }),
53
+ tslib_1.__metadata("design:type", Array)
54
+ ], FigureCapabilityProvider.prototype, "versions", void 0);
55
+ exports.FigureCapabilityProvider = FigureCapabilityProvider = tslib_1.__decorate([
56
+ (0, type_graphql_1.ObjectType)({ description: 'A capability provider this domain may use, as the Settings card lists it.' })
57
+ ], FigureCapabilityProvider);
58
+ /** Reads a whole upload, refused past the size a module may have. */
59
+ async function bytesOf(file) {
60
+ const { createReadStream } = await file;
61
+ const chunks = [];
62
+ let size = 0;
63
+ for await (const chunk of createReadStream()) {
64
+ size += chunk.length;
65
+ if (size > capability_module_store_js_1.MODULE_MAX_BYTES)
66
+ throw new shell_1.Refusal('figure-capability-module-too-large', { max: capability_module_store_js_1.MODULE_MAX_BYTES }, 'a capability module is at most {max} bytes.');
67
+ chunks.push(Buffer.from(chunk));
68
+ }
69
+ return Buffer.concat(chunks);
70
+ }
71
+ /*
72
+ * Capability modules (ADR-0172): the modeller reads the modules its domain uses; the domain owner chooses providers
73
+ * (decision 5); a superuser registers a module for the whole server and steps a provider back (decision 4); a consuming
74
+ * app pushes its own module with an install key that may (amendment 1, decisions 13-15).
75
+ */
76
+ let FigureCapabilityModuleResolver = class FigureCapabilityModuleResolver {
77
+ async figureCapabilityModules(context) {
78
+ const { domain, tx } = context.state;
79
+ return (0, capability_module_store_js_1.chosenModules)(tx ?? { getRepository: shell_1.getRepository }, domain);
80
+ }
81
+ async figureCapabilityChoices(context) {
82
+ const { domain, tx } = context.state;
83
+ return { ...(await (0, capability_module_store_js_1.chosenCapabilityChoices)(tx ?? { getRepository: shell_1.getRepository }, domain)) };
84
+ }
85
+ async figureCapabilityProviders(context) {
86
+ const { domain, tx } = context.state;
87
+ return (0, capability_module_store_js_1.providersFor)(tx ?? { getRepository: shell_1.getRepository }, domain);
88
+ }
89
+ async useFigureCapabilityProvider(provider, on, context) {
90
+ const { domain, user, tx } = context.state;
91
+ return (0, capability_module_store_js_1.chooseProvider)(tx, domain, provider, on, user);
92
+ }
93
+ async registerFigureCapabilityModule(file, context) {
94
+ const { domain, user, tx } = context.state;
95
+ return (0, capability_module_store_js_1.registerCapabilityModule)(tx, { bytes: await bytesOf(file), owner: null, asker: domain, user });
96
+ }
97
+ async setCurrentFigureCapabilityModule(provider, version, context) {
98
+ const { tx } = context.state;
99
+ return (0, capability_module_store_js_1.setCurrentCapabilityModule)(tx, provider, version);
100
+ }
101
+ /*
102
+ * A consuming app's push over its figure server link (ADR-0172 amendment 1, decisions 13-15): the caller is an install
103
+ * key's user, whose key the domain owner issued with the capability provider it may push. The module becomes this
104
+ * domain's, chosen here at once.
105
+ */
106
+ async pushFigureCapabilityModule(source, context, choices) {
107
+ const { domain, user, tx } = context.state;
108
+ const applianceId = user?.userType === 'appliance' ? user.reference : undefined;
109
+ const grant = applianceId ? await tx.getRepository(capability_module_entity_js_1.FigureInstallKeyCapability).findOne({ where: { applianceId, domain: { id: domain.id } } }) : null;
110
+ if (!grant)
111
+ throw new shell_1.Refusal('figure-capability-push-not-allowed', {}, "this key may not push a capability module. issue a key that may, in this domain's Settings.");
112
+ const bytes = Buffer.from(String(source ?? ''), 'utf8');
113
+ if (bytes.length > capability_module_store_js_1.MODULE_MAX_BYTES)
114
+ throw new shell_1.Refusal('figure-capability-module-too-large', { max: capability_module_store_js_1.MODULE_MAX_BYTES }, 'a capability module is at most {max} bytes.');
115
+ const m = await (0, capability_module_store_js_1.registerCapabilityModule)(tx, { bytes, owner: domain, asker: domain, user, provider: grant.provider });
116
+ if (choices)
117
+ await (0, capability_module_store_js_1.keepCapabilityChoices)(tx, domain, m.provider, choices);
118
+ return { provider: m.provider, version: m.version, sha256: m.sha256, capabilities: m.capabilities ?? [] };
119
+ }
120
+ };
121
+ exports.FigureCapabilityModuleResolver = FigureCapabilityModuleResolver;
122
+ tslib_1.__decorate([
123
+ (0, type_graphql_1.Directive)('@privilege(category: "figure", privilege: "query", domainOwnerGranted: true)'),
124
+ (0, type_graphql_1.Query)(returns => [FigureCapabilityModuleRef], { description: 'The capability modules this domain uses: the current version of each provider it chose.' }),
125
+ tslib_1.__param(0, (0, type_graphql_1.Ctx)()),
126
+ tslib_1.__metadata("design:type", Function),
127
+ tslib_1.__metadata("design:paramtypes", [Object]),
128
+ tslib_1.__metadata("design:returntype", Promise)
129
+ ], FigureCapabilityModuleResolver.prototype, "figureCapabilityModules", null);
130
+ tslib_1.__decorate([
131
+ (0, type_graphql_1.Directive)('@privilege(category: "figure", privilege: "query", domainOwnerGranted: true)'),
132
+ (0, type_graphql_1.Query)(returns => shell_1.ScalarObject, {
133
+ description: 'The choice lists of the providers this domain uses, by <capability>.<setting>: what their apps pushed last (ADR-0172 amendment 2).'
134
+ }),
135
+ tslib_1.__param(0, (0, type_graphql_1.Ctx)()),
136
+ tslib_1.__metadata("design:type", Function),
137
+ tslib_1.__metadata("design:paramtypes", [Object]),
138
+ tslib_1.__metadata("design:returntype", Promise)
139
+ ], FigureCapabilityModuleResolver.prototype, "figureCapabilityChoices", null);
140
+ tslib_1.__decorate([
141
+ (0, type_graphql_1.Directive)('@privilege(category: "figure-capability", privilege: "query", domainOwnerGranted: true, superUserGranted: true)'),
142
+ (0, type_graphql_1.Query)(returns => [FigureCapabilityProvider], { description: "The capability providers this domain may use: the system's and those its own app pushed." }),
143
+ tslib_1.__param(0, (0, type_graphql_1.Ctx)()),
144
+ tslib_1.__metadata("design:type", Function),
145
+ tslib_1.__metadata("design:paramtypes", [Object]),
146
+ tslib_1.__metadata("design:returntype", Promise)
147
+ ], FigureCapabilityModuleResolver.prototype, "figureCapabilityProviders", null);
148
+ tslib_1.__decorate([
149
+ (0, type_graphql_1.Directive)('@transaction'),
150
+ (0, type_graphql_1.Directive)('@privilege(category: "figure-capability", privilege: "mutation", domainOwnerGranted: true, superUserGranted: true)'),
151
+ (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'Uses a capability provider in this domain, or stops using it. Answers whether it is used now.' }),
152
+ tslib_1.__param(0, (0, type_graphql_1.Arg)('provider')),
153
+ tslib_1.__param(1, (0, type_graphql_1.Arg)('on')),
154
+ tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
155
+ tslib_1.__metadata("design:type", Function),
156
+ tslib_1.__metadata("design:paramtypes", [String, Boolean, Object]),
157
+ tslib_1.__metadata("design:returntype", Promise)
158
+ ], FigureCapabilityModuleResolver.prototype, "useFigureCapabilityProvider", null);
159
+ tslib_1.__decorate([
160
+ (0, type_graphql_1.Directive)('@transaction'),
161
+ (0, type_graphql_1.Directive)('@privilege(superUserGranted: true)'),
162
+ (0, type_graphql_1.Mutation)(returns => capability_module_entity_js_1.FigureCapabilityModule, { description: "Registers a capability module for the whole server and makes it its provider's current version." }),
163
+ tslib_1.__param(0, (0, type_graphql_1.Arg)('file', type => GraphQLUpload_js_1.default)),
164
+ tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
165
+ tslib_1.__metadata("design:type", Function),
166
+ tslib_1.__metadata("design:paramtypes", [Object, Object]),
167
+ tslib_1.__metadata("design:returntype", Promise)
168
+ ], FigureCapabilityModuleResolver.prototype, "registerFigureCapabilityModule", null);
169
+ tslib_1.__decorate([
170
+ (0, type_graphql_1.Directive)('@transaction'),
171
+ (0, type_graphql_1.Directive)('@privilege(superUserGranted: true)'),
172
+ (0, type_graphql_1.Mutation)(returns => capability_module_entity_js_1.FigureCapabilityModule, { description: "Makes a kept version of a system provider its current one again." }),
173
+ tslib_1.__param(0, (0, type_graphql_1.Arg)('provider')),
174
+ tslib_1.__param(1, (0, type_graphql_1.Arg)('version')),
175
+ tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
176
+ tslib_1.__metadata("design:type", Function),
177
+ tslib_1.__metadata("design:paramtypes", [String, String, Object]),
178
+ tslib_1.__metadata("design:returntype", Promise)
179
+ ], FigureCapabilityModuleResolver.prototype, "setCurrentFigureCapabilityModule", null);
180
+ tslib_1.__decorate([
181
+ (0, type_graphql_1.Directive)('@transaction'),
182
+ (0, type_graphql_1.Directive)('@privilege(category: "figure-published", privilege: "query")'),
183
+ (0, type_graphql_1.Mutation)(returns => FigureCapabilityModuleRef, { description: "Pushes the caller app's capability module to this domain, with an install key that may push it." }),
184
+ tslib_1.__param(0, (0, type_graphql_1.Arg)('source', { description: 'The module file, as text.' })),
185
+ tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
186
+ tslib_1.__param(2, (0, type_graphql_1.Arg)('choices', type => shell_1.ScalarObject, { nullable: true, description: 'The choice lists of its settings, by <capability>.<setting> (ADR-0172 amendment 2).' })),
187
+ tslib_1.__metadata("design:type", Function),
188
+ tslib_1.__metadata("design:paramtypes", [String, Object, Object]),
189
+ tslib_1.__metadata("design:returntype", Promise)
190
+ ], FigureCapabilityModuleResolver.prototype, "pushFigureCapabilityModule", null);
191
+ exports.FigureCapabilityModuleResolver = FigureCapabilityModuleResolver = tslib_1.__decorate([
192
+ (0, type_graphql_1.Resolver)(),
193
+ (0, type_graphql_1.UseMiddleware)(shell_1.SpeakRefusals)
194
+ ], FigureCapabilityModuleResolver);
195
+ //# sourceMappingURL=capability-module-resolver.js.map
@@ -0,0 +1,109 @@
1
+ import { type EntityManager } from 'typeorm';
2
+ import { type FigureCapabilityChoices, type FigureCapabilityDescriptor, type FigureCapabilityModule as LoadedModule } from '@hatiolab/figure-model';
3
+ import { Attachment } from '@things-factory/attachment-base';
4
+ import { Domain } from '@things-factory/shell';
5
+ import { type ThumbnailFiles } from '../figure/figure-thumbnail-store.js';
6
+ import { FigureCapabilityModule } from './capability-module-entity.js';
7
+ export declare const MODULE_REF_TYPE = "figure-capability-module";
8
+ export declare const MODULE_MIMETYPE = "text/javascript";
9
+ /** A module is a few descriptors: a file larger than this is not one. */
10
+ export declare const MODULE_MAX_BYTES: number;
11
+ type Tx = Pick<EntityManager, 'getRepository'>;
12
+ type Actor = {
13
+ id?: string;
14
+ } | undefined;
15
+ /** A module the modeller and the release check load: the current version of a provider a domain uses. */
16
+ export interface ChosenModule {
17
+ provider: string;
18
+ version: string;
19
+ sha256: string;
20
+ capabilities: string[];
21
+ }
22
+ /**
23
+ * The folder this server loads modules from: the app's `cache` (a copy of what attachment-base keeps, written again when
24
+ * missing), unless a test gives another.
25
+ */
26
+ export declare const moduleFolder: () => string;
27
+ /** For tests: load modules from this folder until set back; returns the one it replaced. */
28
+ export declare function useModuleFolder(folder: string | undefined): string | undefined;
29
+ /**
30
+ * Loads the module of these bytes: written to the data folder as `<sha256>.mjs` (when not there yet) and required. The
31
+ * bytes are checked against the hash: a file that is not its address is not loaded.
32
+ */
33
+ export declare function loadModuleBytes(sha256: string, bytes?: Buffer, folder?: string): LoadedModule;
34
+ /**
35
+ * Why these bytes are not a capability module (decision 3): too large, not text, a run-time import, a file that does not
36
+ * load, or exports that break the contract. Empty when it is one; the module, loaded, comes with it.
37
+ */
38
+ export declare function moduleContractOf(bytes: Buffer, folder?: string): {
39
+ issues: string[];
40
+ module?: LoadedModule;
41
+ sha256: string;
42
+ };
43
+ /** The attachment holding a module's file, wherever it is kept. */
44
+ export declare function moduleAttachmentOf(tx: Tx, sha256: string): Promise<Attachment | undefined>;
45
+ /**
46
+ * Registers a module (decisions 4 and 15): checked by the contract, kept by its hash, and made its provider's current
47
+ * version. `owner` is the domain whose app pushed it over its link, or none for the system's. Refused: a module that
48
+ * breaks the contract, and a version not later than every version the same owner has of the provider. The same file
49
+ * again is the version already kept. A pushed provider is chosen in its domain at once (decision 15).
50
+ */
51
+ export declare function registerCapabilityModule(tx: Tx, input: {
52
+ bytes: Buffer;
53
+ owner: Domain | null;
54
+ asker: Domain;
55
+ user: Actor;
56
+ provider?: string;
57
+ }, files?: ThumbnailFiles, folder?: string): Promise<FigureCapabilityModule>;
58
+ /**
59
+ * Keeps the choice lists a domain's provider pushed (ADR-0172 amendment 2, decisions 19 and 20): the last ones replace
60
+ * the ones before, and the same ones are not written again. They are compared apart from the module, which keeps its
61
+ * version when only the lists changed. Refused when they break the shape or name another provider's capability.
62
+ * Answers whether they were written.
63
+ */
64
+ export declare function keepCapabilityChoices(tx: Tx, domain: Domain, provider: string, choices: unknown): Promise<boolean>;
65
+ /**
66
+ * The choice lists of the providers a domain uses, by `<capability>.<setting>` (decision 21): those its own providers
67
+ * pushed. A system provider has none.
68
+ */
69
+ export declare function chosenCapabilityChoices(tx: Tx, domain: {
70
+ id: string;
71
+ }): Promise<FigureCapabilityChoices>;
72
+ /** Makes one kept version of a system provider its current one again (a step back). */
73
+ export declare function setCurrentCapabilityModule(tx: Tx, provider: string, version: string): Promise<FigureCapabilityModule>;
74
+ /** Uses a provider in a domain, or stops using it (decision 5). Refused for a provider nobody registered for it. */
75
+ export declare function chooseProvider(tx: Tx, domain: Domain, provider: string, on: boolean, user: Actor): Promise<boolean>;
76
+ /** The modules a domain uses: the current version of every provider it chose. A parent's choice is not inherited. */
77
+ export declare function chosenModules(tx: Tx, domain: {
78
+ id: string;
79
+ }): Promise<ChosenModule[]>;
80
+ /** A provider as the Settings card lists it: its current version, whose it is, whether the domain uses it, its versions. */
81
+ export interface ProviderListing {
82
+ provider: string;
83
+ current: FigureCapabilityModule;
84
+ /** Pushed by this domain's app over its link; else the system's */
85
+ own: boolean;
86
+ chosen: boolean;
87
+ versions: FigureCapabilityModule[];
88
+ }
89
+ /** Every provider this domain may choose: the system's, and its own pushed ones in their place. */
90
+ export declare function providersFor(tx: Tx, domain: {
91
+ id: string;
92
+ }): Promise<ProviderListing[]>;
93
+ /** The bytes of a module this domain uses, or undefined: the file door serves only those (decision 6). */
94
+ export declare function chosenModuleBytes(tx: Tx, domain: {
95
+ id: string;
96
+ }, sha256: string, files?: ThumbnailFiles): Promise<Buffer | undefined>;
97
+ /**
98
+ * Loads the modules a domain uses, for the release check to read (decision 9): each file from the data folder, or from
99
+ * attachment-base when the folder does not have it yet. A module that does not load is left out, and the figure's
100
+ * capabilities of it are then not verified -- the check blocks them.
101
+ */
102
+ export declare function prepareCapabilityModules(tx: Tx, domain: {
103
+ id: string;
104
+ }, files?: ThumbnailFiles, folder?: string): Promise<Map<string, FigureCapabilityDescriptor>>;
105
+ /** The descriptors last prepared for a domain; none when it was never prepared. */
106
+ export declare const preparedDescriptors: (domainId?: string) => ReadonlyMap<string, FigureCapabilityDescriptor>;
107
+ /** For tests: forget loaded and prepared modules. */
108
+ export declare function forgetCapabilityModules(): void;
109
+ export {};