@andrian.yablonskyy/thub-common 1.0.24 → 1.0.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrian.yablonskyy/thub-common",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "description": "Shared job-spec schema, state enums and a thin API client used by the Agent and Client",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @file packages/shared/src/client-config.js
3
+ * @description Schema and validation for the part of a Client's config that can be edited from the dashboard
4
+ * (its capabilities: the `hw` or `sw` section) — used by the Coordinator on Save and by the Client
5
+ * before applying it
6
+ *
7
+ * @author Andrian Yablonskyy
8
+ * @copyright Copyright (c) 2026 Andrian Yablonskyy. All rights reserved.
9
+ *
10
+ * This file is part of TestHub and is proprietary and confidential.
11
+ * Unauthorized copying, modification, distribution, or use of this file,
12
+ * via any medium, is strictly prohibited without prior written permission
13
+ * from AdSystem.PRO.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const Ajv = require('ajv'),
19
+ addFormats = require('ajv-formats'),
20
+ { DOCKER_IMAGE_PATTERN } = require('./job-spec.schema');
21
+
22
+ const MAX_DEVICES = 8,
23
+ device = (extra = {}) => ({
24
+ type: 'object',
25
+ additionalProperties: false,
26
+ properties: {
27
+ // udev index N -> /dev/thub/dut<N>-<kind>, or an explicit path.
28
+ index: { type: 'integer', minimum: 1, maximum: MAX_DEVICES },
29
+ path: { type: 'string', pattern: '^/dev/[A-Za-z0-9._/+-]+$', maxLength: 128 },
30
+ // udev rule for the symlink (README §8.2): the USB port path, plus
31
+ // optional id/subsystem overrides.
32
+ devpath: { type: 'string', pattern: '^[A-Za-z0-9.:+/-]+$', maxLength: 64 },
33
+ vendorId: { type: 'string', pattern: '^[0-9a-fA-F]{4}$' },
34
+ productId: { type: 'string', pattern: '^[0-9a-fA-F]{4}$' },
35
+ subsystem: { enum: ['usb', 'tty'] },
36
+ ...extra
37
+ },
38
+ anyOf: [{ required: ['index'] }, { required: ['path'] }, ...(extra.serial ? [{ required: ['serial'] }] : [])]
39
+ }),
40
+ list = (item) => ({ type: 'array', maxItems: MAX_DEVICES, items: item }),
41
+ hwSchema = {
42
+ type: 'object',
43
+ additionalProperties: false,
44
+ properties: {
45
+ stlinks: list(device({ serial: { type: 'string', pattern: '^[A-Za-z0-9]{1,64}$' } })),
46
+ uarts: list(device({ baudRate: { type: 'integer', minimum: 50, maximum: 4000000 } })),
47
+ usbs: list(device()),
48
+ relays: list({
49
+ type: 'object',
50
+ additionalProperties: false,
51
+ required: ['channel'],
52
+ properties: {
53
+ channel: { type: 'integer', minimum: 0, maximum: 7 },
54
+ baseUrl: { type: 'string', format: 'uri', pattern: '^https?://' }
55
+ }
56
+ }),
57
+ power: {
58
+ type: ['object', 'null'],
59
+ additionalProperties: false,
60
+ properties: {
61
+ method: { enum: ['uhubctl', 'relay'] },
62
+ hub: { type: 'string', pattern: '^[A-Za-z0-9.:-]{1,32}$' },
63
+ port: { type: 'integer', minimum: 1, maximum: 64 },
64
+ baseUrl: { type: 'string', format: 'uri', pattern: '^https?://' }
65
+ }
66
+ }
67
+ }
68
+ },
69
+ swSchema = {
70
+ type: 'object',
71
+ additionalProperties: false,
72
+ properties: {
73
+ image: { type: 'string', maxLength: 255, pattern: DOCKER_IMAGE_PATTERN },
74
+ registry: { type: 'string', pattern: '^(https?://)?[A-Za-z0-9.-]+(:[0-9]+)?/?$', maxLength: 255 },
75
+ allowDockerHub: { type: 'boolean' },
76
+ allowJobImages: { type: 'boolean' },
77
+ cpus: { type: 'number', exclusiveMinimum: 0, maximum: 256 },
78
+ memory: { type: 'string', pattern: '^[0-9]+[kmgKMG]?$' },
79
+ cmd: { type: 'array', maxItems: 32, items: { type: 'string', maxLength: 1024 } }
80
+ }
81
+ },
82
+ ajv = new Ajv({ allErrors: true, strict: false }),
83
+ validators = { hw: null, sw: null };
84
+ addFormats(ajv);
85
+ validators.hw = ajv.compile(hwSchema);
86
+ validators.sw = ajv.compile(swSchema);
87
+
88
+ // Fields of a section that never leave the Client (secrets) — kept on the
89
+ // Client when a dashboard edit is applied, never reported or accepted.
90
+ const PRIVATE_FIELDS = { hw: [], sw: ['registryAuth'] };
91
+
92
+ // { valid, errors } for a Client's `type` ('hw' | 'sw') config section.
93
+ function validateClientConfig(type, section){
94
+ const validate = validators[type];
95
+ if (!validate){
96
+ return { valid: false, errors: [`unknown Client type "${type}"`] };
97
+ }
98
+ if (!section || typeof section !== 'object' || Array.isArray(section)){
99
+ return { valid: false, errors: [`the ${type} section must be an object`] };
100
+ }
101
+ const valid = validate(section);
102
+ return {
103
+ valid,
104
+ errors: valid ? [] : validate.errors.map((e) => `${type}${e.instancePath.replace(/\//g, '.')} ${e.message}`)
105
+ };
106
+ }
107
+
108
+ // The editable view of a section: without its private fields.
109
+ function publicClientConfig(type, section){
110
+ const out = { ...(section || {}) };
111
+ for (const key of PRIVATE_FIELDS[type] || []){
112
+ delete out[key];
113
+ }
114
+ return out;
115
+ }
116
+
117
+ module.exports = { validateClientConfig, publicClientConfig, CLIENT_CONFIG_PRIVATE_FIELDS: PRIVATE_FIELDS };
package/src/index.js CHANGED
@@ -19,5 +19,6 @@ module.exports = {
19
19
  ...require('./updates'),
20
20
  ...require('./datetime'),
21
21
  ...require('./cron'),
22
+ ...require('./client-config'),
22
23
  ApiClient: require('./api-client').ApiClient
23
24
  };