@things-factory/figure-library 10.1.293 → 10.1.297

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.
@@ -170,9 +170,9 @@ let FigureSourceSettingLet = class FigureSourceSettingLet extends localize(i18ne
170
170
  this.checking = true;
171
171
  this.checked = undefined;
172
172
  try {
173
- const response = await client.query({
174
- query: gql `
175
- query {
173
+ const response = await client.mutate({
174
+ mutation: gql `
175
+ mutation {
176
176
  checkFigureLibrarySource {
177
177
  ok
178
178
  code
@@ -180,8 +180,7 @@ let FigureSourceSettingLet = class FigureSourceSettingLet extends localize(i18ne
180
180
  total
181
181
  }
182
182
  }
183
- `,
184
- fetchPolicy: 'no-cache'
183
+ `
185
184
  });
186
185
  const answer = response.data.checkFigureLibrarySource;
187
186
  /* The check pushes the capability module too: read where it stands again. */
@@ -8,10 +8,9 @@ exports.pushCapabilityModulesOfLinkedDomains = pushCapabilityModulesOfLinkedDoma
8
8
  const crypto_1 = require("crypto");
9
9
  const fs_1 = require("fs");
10
10
  const shell_1 = require("@things-factory/shell");
11
+ const env_1 = require("@things-factory/env");
11
12
  const source_client_1 = require("./source-client");
12
13
  const source_setting_rules_1 = require("./source-setting-rules");
13
- /* Node's own require, which loads an ES module file (22.12+), past any loader in front of this file's `require`. */
14
- const nodeRequire = process.getBuiltinModule?.('node:module')?.createRequire(__filename) ?? require;
15
14
  /** The domain attributes the push keeps: the hashes it last pushed (module, choices), and why the last one failed. */
16
15
  exports.PUSHED_HASH = 'FigureLibrary::source::capability-module-hash';
17
16
  exports.PUSHED_CHOICES_HASH = 'FigureLibrary::source::capability-choices-hash';
@@ -22,12 +21,14 @@ let declared;
22
21
  * left out are read from the module itself.
23
22
  */
24
23
  function declareFigureCapabilityModule(declaration) {
25
- const { provider, version } = declaration.provider && declaration.version ? declaration : nodeRequire(declaration.file);
24
+ const { provider, version } = declaration.provider && declaration.version ? declaration : (0, env_1.requireModuleFile)(declaration.file);
26
25
  declared = { file: declaration.file, provider, version, ...(declaration.choices ? { choices: declaration.choices } : {}) };
27
26
  }
28
27
  /** The app's declared module, or none. */
29
28
  const declaredFigureCapabilityModule = () => declared;
30
29
  exports.declaredFigureCapabilityModule = declaredFigureCapabilityModule;
30
+ /** The key the link uses cannot register capabilities: a key issued before every key could (ADR-0172 decision 14). */
31
+ const pushForbidden = source => new shell_1.Refusal('figure-capability-push-forbidden', { host: new URL(source.url).host, domain: source.domain }, "The token has no right to register capabilities. Issue a new key in the board server install keys of {domain} on {host}, and enter it as the token.");
31
32
  const PUSH = `mutation ($source: String!, $choices: Object) { pushFigureCapabilityModule(source: $source, choices: $choices) { provider version sha256 } }`;
32
33
  async function attribute(manager, domain, name) {
33
34
  return manager.getRepository(shell_1.EnvVar).findOne({ where: { domain: { id: domain.id }, name } });
@@ -62,7 +63,7 @@ async function pushCapabilityModule(manager, domain, source, options = {}) {
62
63
  try {
63
64
  const bytes = (options.read ?? fs_1.readFileSync)(declared.file);
64
65
  const hash = (0, crypto_1.createHash)('sha256').update(bytes).digest('hex');
65
- /* The lists as the app holds them now; a list that cannot be read goes up as none rather than stopping the module. */
66
+ /* The lists as the app holds them now. One that cannot be read stops the push: what failed is kept for the card. */
66
67
  const choices = declared.choices ? await declared.choices(domain) : undefined;
67
68
  const choicesHash = choices ? (0, crypto_1.createHash)('sha256').update(JSON.stringify(choices)).digest('hex') : null;
68
69
  const last = (await attribute(manager, domain, exports.PUSHED_HASH))?.value;
@@ -70,7 +71,7 @@ async function pushCapabilityModule(manager, domain, source, options = {}) {
70
71
  const failed = (await attribute(manager, domain, exports.PUSH_ERROR))?.value;
71
72
  if (!options.force && last === hash && lastChoices === choicesHash && !failed)
72
73
  return capabilityPushState(manager, domain);
73
- await (0, source_client_1.sourceQuery)(source, PUSH, { source: bytes.toString('utf8'), ...(choices ? { choices } : {}) }, options.doFetch);
74
+ await (0, source_client_1.sourceQuery)(source, PUSH, { source: bytes.toString('utf8'), ...(choices ? { choices } : {}) }, options.doFetch, pushForbidden);
74
75
  await write(manager, domain, exports.PUSHED_HASH, hash);
75
76
  await write(manager, domain, exports.PUSHED_CHOICES_HASH, choicesHash);
76
77
  await write(manager, domain, exports.PUSH_ERROR, null);
@@ -227,13 +227,16 @@ let FigureSourceResolver = class FigureSourceResolver {
227
227
  const capabilityModule = await (0, capability_push_1.pushCapabilityModule)(tx, domain, (0, source_setting_rules_1.sourceOf)(attributes), { t: context.t });
228
228
  return { ...(0, source_setting_rules_1.sourceSettingState)(attributes, shell_1.secretHint), capabilityModule };
229
229
  }
230
+ /*
231
+ A mutation: besides asking, it pushes the app's capability module again when it is not the one last pushed and keeps
232
+ where that stands -- writes, so it runs in a transaction and asks the setting's mutation privilege.
233
+ */
230
234
  async checkFigureLibrarySource(context) {
231
235
  const { domain, tx } = context.state;
232
236
  try {
233
- const manager = tx ?? (0, shell_1.getDataSource)().manager;
234
- const source = await figureSourceOf(domain, manager);
237
+ const source = await figureSourceOf(domain, tx);
235
238
  const { total } = await (0, source_install_1.sourceCatalogPage)(source, { pagination: { page: 1, limit: 1 } });
236
- await (0, capability_push_1.pushCapabilityModule)(manager, domain, source, { t: context.t });
239
+ await (0, capability_push_1.pushCapabilityModule)(tx, domain, source, { t: context.t });
237
240
  return { ok: true, total };
238
241
  }
239
242
  catch (error) {
@@ -279,8 +282,9 @@ tslib_1.__decorate([
279
282
  tslib_1.__metadata("design:returntype", Promise)
280
283
  ], FigureSourceResolver.prototype, "updateFigureLibrarySource", null);
281
284
  tslib_1.__decorate([
282
- (0, type_graphql_1.Directive)('@privilege(category: "env-var", privilege: "query", domainOwnerGranted: true)'),
283
- (0, type_graphql_1.Query)(returns => FigureSourceCheck, { description: 'Asks the figure server for one catalog row with the setting as it is.' }),
285
+ (0, type_graphql_1.Directive)('@transaction'),
286
+ (0, type_graphql_1.Directive)('@privilege(category: "env-var", privilege: "mutation", domainOwnerGranted: true)'),
287
+ (0, type_graphql_1.Mutation)(returns => FigureSourceCheck, { description: "Asks the figure server for one catalog row with the setting as it is, and pushes the app's capability module when it changed." }),
284
288
  tslib_1.__param(0, (0, type_graphql_1.Ctx)()),
285
289
  tslib_1.__metadata("design:type", Function),
286
290
  tslib_1.__metadata("design:paramtypes", [Object]),
@@ -1,3 +1,4 @@
1
+ import { Refusal } from '@things-factory/shell';
1
2
  import type { FigureSource } from './source-setting-rules';
2
3
  /**
3
4
  * The calls a board server makes to the figure server it installs from (ADR-0114 decisions 22 · 25 · 26). Only an
@@ -14,8 +15,10 @@ export interface SourceFetch {
14
15
  }
15
16
  /** The domain-scoped address of a REST door on the figure server: {url}/domain/{domain}/{path}. */
16
17
  export declare const sourceRestUrl: (source: FigureSource, path: string) => string;
17
- /** One GraphQL query on the figure server, as the token's domain. */
18
- export declare function sourceQuery<T>(source: FigureSource, query: string, variables: Record<string, unknown>, doFetch?: SourceFetch): Promise<T>;
18
+ /** What a refusal of the token's rights means for a call: by default, reading the domain's released figures. */
19
+ export type SourceForbidden = (source: FigureSource) => Refusal;
20
+ /** One GraphQL query on the figure server, as the token's domain. `forbidden` says what a refusal of the token's rights means for it. */
21
+ export declare function sourceQuery<T>(source: FigureSource, query: string, variables: Record<string, unknown>, doFetch?: SourceFetch, forbidden?: SourceForbidden): Promise<T>;
19
22
  /** A package of these releases from the figure server's `/figure-package` door, as bytes. */
20
23
  export declare function sourcePackage(source: FigureSource, refs: readonly string[], max: number, doFetch?: SourceFetch): Promise<Buffer>;
21
24
  /** The picture of a figure's latest release, as the figure server sends it; null when it has none. */
@@ -22,7 +22,8 @@ function headersOf(source, extra = {}) {
22
22
  /** The domain-scoped address of a REST door on the figure server: {url}/domain/{domain}/{path}. */
23
23
  const sourceRestUrl = (source, path) => `${source.url}/domain/${encodeURIComponent(source.domain)}/${path}`;
24
24
  exports.sourceRestUrl = sourceRestUrl;
25
- async function call(source, url, init, doFetch) {
25
+ const readForbidden = source => new shell_1.Refusal('figure-source-forbidden', { host: host(source), domain: source.domain }, 'The token has no right to read released figures of {domain} on {host}.');
26
+ async function call(source, url, init, doFetch, forbidden = readForbidden) {
26
27
  let response;
27
28
  try {
28
29
  response = await doFetch(url, { ...init, redirect: 'manual', signal: AbortSignal.timeout(exports.SOURCE_TIMEOUT_MS) });
@@ -33,7 +34,7 @@ async function call(source, url, init, doFetch) {
33
34
  if (response.status === 401)
34
35
  throw new shell_1.Refusal('figure-source-unauthorized', { host: host(source) }, 'The figure server {host} did not accept the token.');
35
36
  if (response.status === 403)
36
- throw new shell_1.Refusal('figure-source-forbidden', { host: host(source), domain: source.domain }, 'The token has no right to read released figures of {domain} on {host}.');
37
+ throw forbidden(source);
37
38
  if (response.status >= 300 && response.status < 400)
38
39
  throw new shell_1.Refusal('figure-source-unreachable', { host: host(source) }, 'Cannot connect to the figure server {host}.');
39
40
  return response;
@@ -71,9 +72,9 @@ async function refusalOf(source, response) {
71
72
  catch { }
72
73
  return new shell_1.Refusal('figure-source-failed', { host: host(source), status: response.status }, 'The figure server {host} answered {status}.');
73
74
  }
74
- /** One GraphQL query on the figure server, as the token's domain. */
75
- async function sourceQuery(source, query, variables, doFetch = fetch) {
76
- const response = await call(source, `${source.url}/graphql`, { method: 'POST', headers: headersOf(source, { 'content-type': 'application/json' }), body: JSON.stringify({ query, variables }) }, doFetch);
75
+ /** One GraphQL query on the figure server, as the token's domain. `forbidden` says what a refusal of the token's rights means for it. */
76
+ async function sourceQuery(source, query, variables, doFetch = fetch, forbidden = readForbidden) {
77
+ const response = await call(source, `${source.url}/graphql`, { method: 'POST', headers: headersOf(source, { 'content-type': 'application/json' }), body: JSON.stringify({ query, variables }) }, doFetch, forbidden);
77
78
  if (!response.ok)
78
79
  throw await refusalOf(source, response);
79
80
  const body = (await response.json());
@@ -81,7 +82,7 @@ async function sourceQuery(source, query, variables, doFetch = fetch) {
81
82
  const [error] = body.errors;
82
83
  const message = error.message ?? '';
83
84
  if (/unauthori[sz]ed|permission|privilege/i.test(message) || error.extensions?.code === 'FORBIDDEN')
84
- throw new shell_1.Refusal('figure-source-forbidden', { host: host(source), domain: source.domain }, 'The token has no right to read released figures of {domain} on {host}.');
85
+ throw forbidden(source);
85
86
  throw new shell_1.Refusal('figure-source-failed-message', { host: host(source), message }, 'The figure server {host} refused: {message}');
86
87
  }
87
88
  return body.data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/figure-library",
3
- "version": "10.1.293",
3
+ "version": "10.1.297",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "dist-client/index.js",
6
6
  "files": [
@@ -55,13 +55,13 @@
55
55
  "@operato/slot-layout-editor": "^10.29.87",
56
56
  "@operato/styles": "^10.29.23",
57
57
  "@operato/utils": "^10.0.0",
58
- "@things-factory/attachment-base": "^10.1.291",
59
- "@things-factory/auth-base": "^10.1.291",
60
- "@things-factory/board-service": "^10.1.293",
61
- "@things-factory/env": "^10.1.281",
58
+ "@things-factory/attachment-base": "^10.1.297",
59
+ "@things-factory/auth-base": "^10.1.297",
60
+ "@things-factory/board-service": "^10.1.297",
61
+ "@things-factory/env": "^10.1.297",
62
62
  "@things-factory/figure-viewer": "^10.1.291",
63
- "@things-factory/setting-base": "^10.1.291",
64
- "@things-factory/shell": "^10.1.291",
63
+ "@things-factory/setting-base": "^10.1.297",
64
+ "@things-factory/shell": "^10.1.297",
65
65
  "graphql-scalars": "^1.22.4",
66
66
  "graphql-tag": "^2.12.6",
67
67
  "graphql-upload": "^15.0.2",
@@ -69,5 +69,5 @@
69
69
  "koa-router": "^7.4.0",
70
70
  "lit": "^3.1.2"
71
71
  },
72
- "gitHead": "e8837586177550338b7f0cd5071726b5109e804d"
72
+ "gitHead": "656ec78e5adf500b3b636eb9790cfe22f2a49ba9"
73
73
  }
@@ -176,6 +176,7 @@
176
176
  "error.figure-source-unreachable": "Cannot connect to the figure server {host}.",
177
177
  "error.figure-source-unauthorized": "The figure server {host} did not accept the token. Check the access token.",
178
178
  "error.figure-source-forbidden": "The token has no right to read released figures of {domain} on {host}.",
179
+ "error.figure-capability-push-forbidden": "The token has no right to register capabilities. Issue a new key in the board server install keys of {domain} on {host}, and enter it as the token.",
179
180
  "error.figure-source-failed": "The figure server {host} answered {status}.",
180
181
  "error.figure-source-failed-message": "The figure server {host} refused: {message}",
181
182
  "error.figure-source-url-invalid": "The address is a web address that starts with http:// or https://.",
@@ -176,6 +176,7 @@
176
176
  "error.figure-source-unreachable": "No se puede conectar con el servidor de figuras {host}.",
177
177
  "error.figure-source-unauthorized": "El servidor de figuras {host} no aceptó el token. Revise el token de acceso.",
178
178
  "error.figure-source-forbidden": "El token no tiene permiso para leer las figuras publicadas de {domain} en {host}.",
179
+ "error.figure-capability-push-forbidden": "El token no tiene permiso para registrar capacidades. Emita una clave nueva en las claves de instalación del servidor de tableros de {domain} en {host} e introdúzcala como token.",
179
180
  "error.figure-source-failed": "El servidor de figuras {host} respondió {status}.",
180
181
  "error.figure-source-failed-message": "El servidor de figuras {host} rechazó la solicitud: {message}",
181
182
  "error.figure-source-url-invalid": "La dirección debe empezar por http:// o https://.",
@@ -176,6 +176,7 @@
176
176
  "error.figure-source-unreachable": "Figure サーバー {host} に接続できません。",
177
177
  "error.figure-source-unauthorized": "Figure サーバー {host} でトークンを認証できませんでした。アクセストークンを確認してください。",
178
178
  "error.figure-source-forbidden": "アクセストークンに {host} の {domain} の発行済み Figure を参照する権限がありません。",
179
+ "error.figure-capability-push-forbidden": "アクセストークンに能力を登録する権限がありません。{host} の {domain} の「ボードサーバー設置キー」でキーを新しく発行し、アクセストークンとして入力してください。",
179
180
  "error.figure-source-failed": "Figure サーバー {host} でエラー({status})が発生しました。",
180
181
  "error.figure-source-failed-message": "Figure サーバー {host} が要求を拒否しました: {message}",
181
182
  "error.figure-source-url-invalid": "サーバーアドレスは http:// または https:// で始まる Web アドレスである必要があります。",
@@ -176,6 +176,7 @@
176
176
  "error.figure-source-unreachable": "피규어 서버 {host}에 연결할 수 없습니다.",
177
177
  "error.figure-source-unauthorized": "피규어 서버 {host}에서 토큰을 인증하지 못했습니다. 접속 토큰을 확인하세요.",
178
178
  "error.figure-source-forbidden": "접속 토큰에 {host}의 {domain} 사업장 발행 피규어를 조회할 권한이 없습니다.",
179
+ "error.figure-capability-push-forbidden": "접속 토큰에 능력 등록 권한이 없습니다. {host}의 {domain} 사업장 「보드 서버 설치 키」에서 키를 새로 발급해 접속 토큰으로 입력하세요.",
179
180
  "error.figure-source-failed": "피규어 서버 {host}에서 오류({status})가 발생했습니다.",
180
181
  "error.figure-source-failed-message": "피규어 서버 {host}에서 요청을 거절했습니다: {message}",
181
182
  "error.figure-source-url-invalid": "서버 주소는 http:// 또는 https://로 시작하는 웹 주소여야 합니다.",
@@ -176,6 +176,7 @@
176
176
  "error.figure-source-unreachable": "Tidak dapat menyambung ke pelayan figure {host}.",
177
177
  "error.figure-source-unauthorized": "Pelayan figure {host} tidak menerima token. Semak token akses.",
178
178
  "error.figure-source-forbidden": "Token tiada hak untuk membaca figure terbitan {domain} pada {host}.",
179
+ "error.figure-capability-push-forbidden": "Token tiada hak untuk mendaftar keupayaan. Keluarkan kunci baharu dalam kunci pemasangan pelayan papan {domain} pada {host}, dan masukkannya sebagai token.",
179
180
  "error.figure-source-failed": "Pelayan figure {host} menjawab {status}.",
180
181
  "error.figure-source-failed-message": "Pelayan figure {host} menolak: {message}",
181
182
  "error.figure-source-url-invalid": "Alamat mesti bermula dengan http:// atau https://.",
@@ -176,6 +176,7 @@
176
176
  "error.figure-source-unreachable": "无法连接到图形服务器 {host}。",
177
177
  "error.figure-source-unauthorized": "图形服务器 {host} 未能验证令牌。请检查访问令牌。",
178
178
  "error.figure-source-forbidden": "访问令牌无权读取 {host} 上 {domain} 的已发布图形。",
179
+ "error.figure-capability-push-forbidden": "访问令牌没有注册能力的权限。请在 {host} 的 {domain} 的“看板服务器安装密钥”中重新签发密钥,并将其作为访问令牌输入。",
179
180
  "error.figure-source-failed": "图形服务器 {host} 返回错误({status})。",
180
181
  "error.figure-source-failed-message": "图形服务器 {host} 拒绝了请求:{message}",
181
182
  "error.figure-source-url-invalid": "服务器地址必须是以 http:// 或 https:// 开头的网址。",