@lark-base-open/base-database-toolchain 1.2.0 → 1.3.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/CHANGELOG.md +27 -0
- package/README.md +7 -1
- package/bin/base-database-toolchain.mjs +13 -1
- package/dist/app-builder/core.d.mts +4 -0
- package/dist/app-builder/core.mjs +736 -110
- package/dist/app-builder/package.json +1 -1
- package/dist/data/config-schema.json +95 -0
- package/dist/dependency-policy.d.ts +13 -0
- package/dist/dependency-policy.js +187 -0
- package/dist/dev-worker.mjs +27 -21
- package/dist/function-description.d.ts +12 -0
- package/dist/function-description.js +38 -0
- package/dist/function-params.d.ts +50 -0
- package/dist/function-params.js +157 -0
- package/dist/function-schema.d.ts +35 -0
- package/dist/function-schema.js +490 -0
- package/dist/go-adapter.d.mts +1 -1
- package/dist/go-adapter.mjs +299 -225
- package/dist/mutation-schema.d.ts +151 -0
- package/dist/mutation-schema.js +158 -0
- package/dist/mutation.d.ts +12 -0
- package/dist/mutation.js +145 -0
- package/dist/npm-policy.d.ts +16 -0
- package/dist/npm-policy.js +193 -0
- package/dist/page-link-guard.js +3 -2
- package/dist/project-contract.d.ts +2 -0
- package/dist/project-contract.js +38 -0
- package/dist/project-layout.d.ts +15 -0
- package/dist/project-layout.js +89 -0
- package/dist/source-archive-inputs.d.ts +13 -0
- package/dist/source-archive-inputs.js +105 -0
- package/npm-shrinkwrap.json +13 -3
- package/package.json +15 -2
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
export declare const mutationSchema: {
|
|
2
|
+
readonly type: 'object';
|
|
3
|
+
readonly properties: {
|
|
4
|
+
readonly spec: {
|
|
5
|
+
readonly type: 'object';
|
|
6
|
+
readonly properties: {
|
|
7
|
+
readonly table: {
|
|
8
|
+
readonly type: 'object';
|
|
9
|
+
readonly properties: {
|
|
10
|
+
readonly table_id: {
|
|
11
|
+
readonly type: 'string';
|
|
12
|
+
readonly pattern: '^[A-Za-z][A-Za-z0-9_]{0,63}$';
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
readonly required: readonly ['table_id'];
|
|
16
|
+
readonly additionalProperties: false;
|
|
17
|
+
};
|
|
18
|
+
readonly operation: {
|
|
19
|
+
readonly enum: readonly ['create', 'update', 'delete', 'append_attachments', 'remove_attachments'];
|
|
20
|
+
};
|
|
21
|
+
readonly params: {
|
|
22
|
+
readonly type: 'array';
|
|
23
|
+
readonly items: {
|
|
24
|
+
readonly $ref: '#/$defs/parameter';
|
|
25
|
+
};
|
|
26
|
+
readonly maxItems: 256;
|
|
27
|
+
};
|
|
28
|
+
readonly precondition: {
|
|
29
|
+
readonly type: 'object';
|
|
30
|
+
readonly properties: {
|
|
31
|
+
readonly conjunction: {
|
|
32
|
+
readonly const: 'and';
|
|
33
|
+
};
|
|
34
|
+
readonly conditions: {
|
|
35
|
+
readonly type: 'array';
|
|
36
|
+
readonly minItems: 1;
|
|
37
|
+
readonly maxItems: 1;
|
|
38
|
+
readonly items: {
|
|
39
|
+
readonly type: 'object';
|
|
40
|
+
readonly properties: {
|
|
41
|
+
readonly field_id: {
|
|
42
|
+
readonly const: 'rec_id';
|
|
43
|
+
};
|
|
44
|
+
readonly operator: {
|
|
45
|
+
readonly const: 'is';
|
|
46
|
+
};
|
|
47
|
+
readonly param: {
|
|
48
|
+
readonly type: 'integer';
|
|
49
|
+
readonly minimum: 1;
|
|
50
|
+
readonly maximum: 256;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
readonly required: readonly ['field_id', 'operator', 'param'];
|
|
54
|
+
readonly additionalProperties: false;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
readonly required: readonly ['conjunction', 'conditions'];
|
|
59
|
+
readonly additionalProperties: false;
|
|
60
|
+
};
|
|
61
|
+
readonly set: {
|
|
62
|
+
readonly type: 'object';
|
|
63
|
+
readonly maxProperties: 200;
|
|
64
|
+
readonly propertyNames: {
|
|
65
|
+
readonly pattern: '^[A-Za-z][A-Za-z0-9_]{0,63}$';
|
|
66
|
+
};
|
|
67
|
+
readonly additionalProperties: {
|
|
68
|
+
readonly $ref: '#/$defs/source';
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
readonly returning: {
|
|
72
|
+
readonly type: 'array';
|
|
73
|
+
readonly items: {
|
|
74
|
+
readonly type: 'string';
|
|
75
|
+
};
|
|
76
|
+
readonly uniqueItems: true;
|
|
77
|
+
readonly maxItems: 201;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
readonly required: readonly ['table', 'operation'];
|
|
81
|
+
readonly additionalProperties: false;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
readonly required: readonly ['spec'];
|
|
85
|
+
readonly additionalProperties: false;
|
|
86
|
+
readonly $defs: {
|
|
87
|
+
readonly parameter: {
|
|
88
|
+
readonly type: 'object';
|
|
89
|
+
readonly properties: {
|
|
90
|
+
readonly name: {
|
|
91
|
+
readonly type: 'string';
|
|
92
|
+
};
|
|
93
|
+
readonly enum: {
|
|
94
|
+
readonly type: 'array';
|
|
95
|
+
};
|
|
96
|
+
readonly kind: {
|
|
97
|
+
readonly enum: readonly ['TEXT', 'INT', 'DOUBLE', 'BOOL', 'TIMESTAMP', 'DURATION', 'USER_ID', 'GROUP_ID', 'RECORD_ID', 'ATTACHMENT'];
|
|
98
|
+
};
|
|
99
|
+
readonly description: {
|
|
100
|
+
readonly type: 'string';
|
|
101
|
+
};
|
|
102
|
+
readonly required: {
|
|
103
|
+
readonly type: 'boolean';
|
|
104
|
+
};
|
|
105
|
+
readonly list: {
|
|
106
|
+
readonly type: 'boolean';
|
|
107
|
+
};
|
|
108
|
+
readonly max_length: {
|
|
109
|
+
readonly type: 'integer';
|
|
110
|
+
readonly minimum: 1;
|
|
111
|
+
readonly maximum: 100000;
|
|
112
|
+
};
|
|
113
|
+
readonly max_items: {
|
|
114
|
+
readonly type: 'integer';
|
|
115
|
+
readonly minimum: 1;
|
|
116
|
+
readonly maximum: 200;
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
readonly required: readonly ['kind'];
|
|
120
|
+
readonly additionalProperties: false;
|
|
121
|
+
};
|
|
122
|
+
readonly source: {
|
|
123
|
+
readonly type: 'object';
|
|
124
|
+
readonly properties: {
|
|
125
|
+
readonly const: {
|
|
126
|
+
readonly not: {
|
|
127
|
+
readonly type: 'null';
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
readonly param: {
|
|
131
|
+
readonly type: 'integer';
|
|
132
|
+
readonly minimum: 1;
|
|
133
|
+
readonly maximum: 256;
|
|
134
|
+
};
|
|
135
|
+
readonly current_user: {
|
|
136
|
+
readonly const: true;
|
|
137
|
+
};
|
|
138
|
+
readonly now: {
|
|
139
|
+
readonly const: true;
|
|
140
|
+
};
|
|
141
|
+
readonly clear: {
|
|
142
|
+
readonly const: true;
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
readonly required: readonly [];
|
|
146
|
+
readonly additionalProperties: false;
|
|
147
|
+
readonly minProperties: 1;
|
|
148
|
+
readonly maxProperties: 1;
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
export const mutationSchema = {
|
|
2
|
+
type: 'object',
|
|
3
|
+
properties: {
|
|
4
|
+
spec: {
|
|
5
|
+
type: 'object',
|
|
6
|
+
properties: {
|
|
7
|
+
table: {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
table_id: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
pattern: '^[A-Za-z][A-Za-z0-9_]{0,63}$',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
required: ['table_id'],
|
|
16
|
+
additionalProperties: false,
|
|
17
|
+
},
|
|
18
|
+
operation: {
|
|
19
|
+
enum: [
|
|
20
|
+
'create',
|
|
21
|
+
'update',
|
|
22
|
+
'delete',
|
|
23
|
+
'append_attachments',
|
|
24
|
+
'remove_attachments',
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
params: {
|
|
28
|
+
type: 'array',
|
|
29
|
+
items: {
|
|
30
|
+
$ref: '#/$defs/parameter',
|
|
31
|
+
},
|
|
32
|
+
maxItems: 256,
|
|
33
|
+
},
|
|
34
|
+
precondition: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
properties: {
|
|
37
|
+
conjunction: { const: 'and' },
|
|
38
|
+
conditions: {
|
|
39
|
+
type: 'array',
|
|
40
|
+
minItems: 1,
|
|
41
|
+
maxItems: 1,
|
|
42
|
+
items: {
|
|
43
|
+
type: 'object',
|
|
44
|
+
properties: {
|
|
45
|
+
field_id: { const: 'rec_id' },
|
|
46
|
+
operator: { const: 'is' },
|
|
47
|
+
param: { type: 'integer', minimum: 1, maximum: 256 },
|
|
48
|
+
},
|
|
49
|
+
required: ['field_id', 'operator', 'param'],
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
required: ['conjunction', 'conditions'],
|
|
55
|
+
additionalProperties: false,
|
|
56
|
+
},
|
|
57
|
+
set: {
|
|
58
|
+
type: 'object',
|
|
59
|
+
maxProperties: 200,
|
|
60
|
+
propertyNames: {
|
|
61
|
+
pattern: '^[A-Za-z][A-Za-z0-9_]{0,63}$',
|
|
62
|
+
},
|
|
63
|
+
additionalProperties: {
|
|
64
|
+
$ref: '#/$defs/source',
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
returning: {
|
|
68
|
+
type: 'array',
|
|
69
|
+
items: {
|
|
70
|
+
type: 'string',
|
|
71
|
+
},
|
|
72
|
+
uniqueItems: true,
|
|
73
|
+
maxItems: 201,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
required: ['table', 'operation'],
|
|
77
|
+
additionalProperties: false,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
required: ['spec'],
|
|
81
|
+
additionalProperties: false,
|
|
82
|
+
$defs: {
|
|
83
|
+
parameter: {
|
|
84
|
+
type: 'object',
|
|
85
|
+
properties: {
|
|
86
|
+
name: {
|
|
87
|
+
type: 'string',
|
|
88
|
+
},
|
|
89
|
+
enum: {
|
|
90
|
+
type: 'array',
|
|
91
|
+
},
|
|
92
|
+
kind: {
|
|
93
|
+
enum: [
|
|
94
|
+
'TEXT',
|
|
95
|
+
'INT',
|
|
96
|
+
'DOUBLE',
|
|
97
|
+
'BOOL',
|
|
98
|
+
'TIMESTAMP',
|
|
99
|
+
'DURATION',
|
|
100
|
+
'USER_ID',
|
|
101
|
+
'GROUP_ID',
|
|
102
|
+
'RECORD_ID',
|
|
103
|
+
'ATTACHMENT',
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
description: {
|
|
107
|
+
type: 'string',
|
|
108
|
+
},
|
|
109
|
+
required: {
|
|
110
|
+
type: 'boolean',
|
|
111
|
+
},
|
|
112
|
+
list: {
|
|
113
|
+
type: 'boolean',
|
|
114
|
+
},
|
|
115
|
+
max_length: {
|
|
116
|
+
type: 'integer',
|
|
117
|
+
minimum: 1,
|
|
118
|
+
maximum: 100000,
|
|
119
|
+
},
|
|
120
|
+
max_items: {
|
|
121
|
+
type: 'integer',
|
|
122
|
+
minimum: 1,
|
|
123
|
+
maximum: 200,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
required: ['kind'],
|
|
127
|
+
additionalProperties: false,
|
|
128
|
+
},
|
|
129
|
+
source: {
|
|
130
|
+
type: 'object',
|
|
131
|
+
properties: {
|
|
132
|
+
const: {
|
|
133
|
+
not: {
|
|
134
|
+
type: 'null',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
param: {
|
|
138
|
+
type: 'integer',
|
|
139
|
+
minimum: 1,
|
|
140
|
+
maximum: 256,
|
|
141
|
+
},
|
|
142
|
+
current_user: {
|
|
143
|
+
const: true,
|
|
144
|
+
},
|
|
145
|
+
now: {
|
|
146
|
+
const: true,
|
|
147
|
+
},
|
|
148
|
+
clear: {
|
|
149
|
+
const: true,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
required: [],
|
|
153
|
+
additionalProperties: false,
|
|
154
|
+
minProperties: 1,
|
|
155
|
+
maxProperties: 1,
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function readMutationJSON(path: string): unknown;
|
|
2
|
+
export declare function validateMutationDefinition(definition: unknown, { requireParameterContract, path, }?: {
|
|
3
|
+
requireParameterContract?: boolean;
|
|
4
|
+
path?: string;
|
|
5
|
+
}): void;
|
|
6
|
+
export declare function readMutationMetaItem(path: string, workspace: string, enforceDescription?: boolean): {
|
|
7
|
+
functionId: string;
|
|
8
|
+
functionType: 5;
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
sourceCode: string;
|
|
12
|
+
};
|
package/dist/mutation.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, join } from 'node:path';
|
|
3
|
+
import { Ajv } from 'ajv';
|
|
4
|
+
import { parseTree } from 'jsonc-parser';
|
|
5
|
+
import { validateFunctionDescription } from './function-description.js';
|
|
6
|
+
import { AGENT_TOOL_DESIGN_REFERENCE, checkDeclaredParameters, mutationEnumType, } from './function-params.js';
|
|
7
|
+
import { mutationSchema } from './mutation-schema.js';
|
|
8
|
+
const validate = new Ajv({ allErrors: true, strict: false }).compile(mutationSchema);
|
|
9
|
+
const maxMutationSourceBytes = 2 * 1024 * 1024;
|
|
10
|
+
const maxMutationJSONDepth = 16;
|
|
11
|
+
// 发布声明必须严格解析,避免 CLI 与服务端对重复键取值不同。
|
|
12
|
+
function parseMutationJSON(raw, path) {
|
|
13
|
+
if (Buffer.byteLength(raw) > maxMutationSourceBytes)
|
|
14
|
+
throw new Error(`Mutation JSON exceeds byte budget: ${path}`);
|
|
15
|
+
const errors = [];
|
|
16
|
+
const tree = parseTree(raw, errors, {
|
|
17
|
+
disallowComments: true,
|
|
18
|
+
allowTrailingComma: false,
|
|
19
|
+
});
|
|
20
|
+
if (!tree || errors.length)
|
|
21
|
+
throw new Error(`Invalid Mutation JSON: ${path}`);
|
|
22
|
+
const visit = (node, depth) => {
|
|
23
|
+
if (depth > maxMutationJSONDepth)
|
|
24
|
+
throw new Error(`Mutation JSON exceeds nesting budget: ${path}`);
|
|
25
|
+
if (node.type === 'object') {
|
|
26
|
+
const properties = node.children ?? [];
|
|
27
|
+
const keys = properties.map((property) => property.children?.[0]?.value);
|
|
28
|
+
if (new Set(keys).size !== keys.length)
|
|
29
|
+
throw new Error(`Duplicate Mutation JSON key: ${path}`);
|
|
30
|
+
for (const property of properties)
|
|
31
|
+
visit(property.children[1], depth + 1);
|
|
32
|
+
}
|
|
33
|
+
else if (node.type === 'array') {
|
|
34
|
+
for (const item of node.children ?? [])
|
|
35
|
+
visit(item, depth + 1);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
visit(tree, 0);
|
|
39
|
+
return JSON.parse(raw);
|
|
40
|
+
}
|
|
41
|
+
export function readMutationJSON(path) {
|
|
42
|
+
return parseMutationJSON(readFileSync(path, 'utf8'), path);
|
|
43
|
+
}
|
|
44
|
+
export function validateMutationDefinition(definition, { requireParameterContract = true, path = 'spec.json', } = {}) {
|
|
45
|
+
if (!validate(definition))
|
|
46
|
+
throw new Error(`Invalid Mutation declaration: ${JSON.stringify(validate.errors)}`);
|
|
47
|
+
const s = definition.spec;
|
|
48
|
+
if (requireParameterContract) {
|
|
49
|
+
// name/description/enum: the service renders the named Agent schema from these.
|
|
50
|
+
const { problems } = checkDeclaredParameters(s.params ?? [], {
|
|
51
|
+
label: 'spec.params',
|
|
52
|
+
pointer: '/params',
|
|
53
|
+
file: path,
|
|
54
|
+
enumType: mutationEnumType,
|
|
55
|
+
});
|
|
56
|
+
if (problems.length)
|
|
57
|
+
throw new Error(`Invalid Mutation parameters: ${path}: ${problems.join('; ')}. See ${AGENT_TOOL_DESIGN_REFERENCE}.`);
|
|
58
|
+
}
|
|
59
|
+
const attachmentOperation = s.operation === 'append_attachments' ||
|
|
60
|
+
s.operation === 'remove_attachments';
|
|
61
|
+
const params = s.params ?? [];
|
|
62
|
+
const parameter = (index) => {
|
|
63
|
+
const p = params[index - 1];
|
|
64
|
+
if (!p)
|
|
65
|
+
throw new Error(`Mutation references unknown parameter ${index}`);
|
|
66
|
+
return p;
|
|
67
|
+
};
|
|
68
|
+
for (const p of params) {
|
|
69
|
+
if (p.max_length !== undefined && p.kind !== 'TEXT')
|
|
70
|
+
throw new Error('Mutation max_length requires TEXT');
|
|
71
|
+
if (p.max_items !== undefined && !p.list)
|
|
72
|
+
throw new Error('Mutation max_items requires list');
|
|
73
|
+
}
|
|
74
|
+
if (s.operation === 'create') {
|
|
75
|
+
if (s.precondition || !Object.keys(s.set ?? {}).length)
|
|
76
|
+
throw new Error('Mutation create requires set and forbids precondition');
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const selection = s.precondition;
|
|
80
|
+
if (!selection)
|
|
81
|
+
throw new Error('Mutation modification requires precondition');
|
|
82
|
+
const p = parameter(selection.conditions[0].param);
|
|
83
|
+
if (p.kind !== 'RECORD_ID' || p.required === false || p.list)
|
|
84
|
+
throw new Error('Mutation precondition must reference a required scalar RECORD_ID parameter');
|
|
85
|
+
if ((s.operation === 'update' || attachmentOperation) &&
|
|
86
|
+
!Object.keys(s.set ?? {}).length)
|
|
87
|
+
throw new Error('Mutation write requires set');
|
|
88
|
+
if (s.operation === 'delete' && Object.keys(s.set ?? {}).length)
|
|
89
|
+
throw new Error('Mutation delete forbids set');
|
|
90
|
+
}
|
|
91
|
+
for (const [id, source] of Object.entries(s.set ?? {})) {
|
|
92
|
+
if (id === 'rec_id')
|
|
93
|
+
throw new Error('Mutation cannot write rec_id');
|
|
94
|
+
if (source.param)
|
|
95
|
+
parameter(source.param);
|
|
96
|
+
if (attachmentOperation) {
|
|
97
|
+
if (source.param) {
|
|
98
|
+
if (parameter(source.param).kind !== 'ATTACHMENT')
|
|
99
|
+
throw new Error('Mutation attachment operation requires ATTACHMENT parameters');
|
|
100
|
+
}
|
|
101
|
+
else if (!Object.hasOwn(source, 'const')) {
|
|
102
|
+
throw new Error('Mutation attachment operation requires param or const');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (source.clear && s.operation !== 'update')
|
|
106
|
+
throw new Error('Mutation clear is only valid for update');
|
|
107
|
+
}
|
|
108
|
+
for (const id of s.returning ?? [])
|
|
109
|
+
if (id !== 'rec_id' && !Object.hasOwn(s.set ?? {}, id))
|
|
110
|
+
throw new Error('Mutation returning may only include rec_id and set fields');
|
|
111
|
+
}
|
|
112
|
+
export function readMutationMetaItem(path, workspace, enforceDescription = true) {
|
|
113
|
+
const meta = readMutationJSON(path);
|
|
114
|
+
if (!meta ||
|
|
115
|
+
typeof meta !== 'object' ||
|
|
116
|
+
Array.isArray(meta) ||
|
|
117
|
+
Object.keys(meta).some((key) => !['name', 'description'].includes(key)))
|
|
118
|
+
throw new Error(`Invalid Mutation meta: ${path}`);
|
|
119
|
+
const functionId = basename(dirname(path));
|
|
120
|
+
if (!/^[A-Za-z0-9_-]{1,64}$/.test(functionId))
|
|
121
|
+
throw new Error(`Invalid Function ID: ${functionId}`);
|
|
122
|
+
if (typeof meta.name !== 'string' || !meta.name || [...meta.name].length > 64)
|
|
123
|
+
throw new Error('Mutation name must contain 1 to 64 characters');
|
|
124
|
+
if (enforceDescription)
|
|
125
|
+
validateFunctionDescription(meta.description, meta.name, path);
|
|
126
|
+
const specPath = join(dirname(path), 'spec.json');
|
|
127
|
+
const sourceCode = `{"spec":${readFileSync(specPath, 'utf8')}}`;
|
|
128
|
+
const definition = parseMutationJSON(sourceCode, specPath);
|
|
129
|
+
validateMutationDefinition(definition, {
|
|
130
|
+
requireParameterContract: enforceDescription,
|
|
131
|
+
path: specPath,
|
|
132
|
+
});
|
|
133
|
+
const { spec } = definition;
|
|
134
|
+
const catalog = JSON.parse(readFileSync(join(workspace, 'catalog.json'), 'utf8'));
|
|
135
|
+
const tables = catalog.tables?.filter((table) => table.id === spec.table.table_id) ?? [];
|
|
136
|
+
if (tables.length !== 1)
|
|
137
|
+
throw new Error('Mutation table must match exactly one Catalog table');
|
|
138
|
+
return {
|
|
139
|
+
functionId,
|
|
140
|
+
functionType: 5,
|
|
141
|
+
name: meta.name,
|
|
142
|
+
description: typeof meta.description === 'string' ? meta.description : '',
|
|
143
|
+
sourceCode,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare function lockRepairHint(directory: string, npmVersion: string): string;
|
|
2
|
+
export interface LockRegistryGroup {
|
|
3
|
+
domain: string;
|
|
4
|
+
count: number;
|
|
5
|
+
packages: string[];
|
|
6
|
+
}
|
|
7
|
+
export declare class LockRegistryError extends Error {
|
|
8
|
+
readonly file: string;
|
|
9
|
+
readonly groups: LockRegistryGroup[];
|
|
10
|
+
readonly code = "DEPENDENCY_LOCK_REGISTRY_NOT_ALLOWED";
|
|
11
|
+
readonly violationCount: number;
|
|
12
|
+
constructor(file: string, groups: LockRegistryGroup[], allowedHosts: string[]);
|
|
13
|
+
}
|
|
14
|
+
export declare function validateLockRegistries(lock: Record<string, unknown>, file: string, allowedHosts: string[]): void;
|
|
15
|
+
/** Fast local validation before any remote calls or node_modules changes. */
|
|
16
|
+
export declare function validateDependencyLock(project: string, directory: string, npmVersion: string): void;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
function record(value) {
|
|
4
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
export function lockRepairHint(directory, npmVersion) {
|
|
7
|
+
return `In ${directory}, review package.json, then run: npx --yes npm@${npmVersion} install --package-lock-only --ignore-scripts --no-audit --no-fund. Review the lock diff and retry the command once. Keep the original lock; do not delete it or bypass installation.`;
|
|
8
|
+
}
|
|
9
|
+
export class LockRegistryError extends Error {
|
|
10
|
+
file;
|
|
11
|
+
groups;
|
|
12
|
+
code = 'DEPENDENCY_LOCK_REGISTRY_NOT_ALLOWED';
|
|
13
|
+
violationCount;
|
|
14
|
+
constructor(file, groups, allowedHosts) {
|
|
15
|
+
const count = groups.reduce((total, group) => total + group.count, 0);
|
|
16
|
+
super([
|
|
17
|
+
`DEPENDENCY_LOCK_REGISTRY_NOT_ALLOWED: ${file} has ${count} dependency sources outside the cloud allowlist.`,
|
|
18
|
+
...groups.map((group) => `- ${group.domain}: ${group.count}; sample packages (up to 5): ${group.packages.join(', ')}`),
|
|
19
|
+
`Allowed registry hosts: ${allowedHosts.join(', ')}.`,
|
|
20
|
+
'For public npm packages, change the resolved host to a registry in the allowed list; keep the path, version and integrity. Do not delete the lockfile. For other sources, ask the administrator of the target environment to allow the source before retrying.',
|
|
21
|
+
].join('\n'));
|
|
22
|
+
this.file = file;
|
|
23
|
+
this.groups = groups;
|
|
24
|
+
this.name = 'LockRegistryError';
|
|
25
|
+
this.violationCount = count;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function legacyLockEntries(dependencies) {
|
|
29
|
+
const result = [];
|
|
30
|
+
const pending = [{ parent: '', dependencies }];
|
|
31
|
+
while (pending.length) {
|
|
32
|
+
const current = pending.pop();
|
|
33
|
+
if (!record(current.dependencies))
|
|
34
|
+
continue;
|
|
35
|
+
for (const [name, entry] of Object.entries(current.dependencies)) {
|
|
36
|
+
const path = current.parent
|
|
37
|
+
? `${current.parent}/node_modules/${name}`
|
|
38
|
+
: `node_modules/${name}`;
|
|
39
|
+
result.push([path, entry]);
|
|
40
|
+
if (record(entry))
|
|
41
|
+
pending.push({ parent: path, dependencies: entry.dependencies });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
function lockRegistryEntries(lock) {
|
|
47
|
+
if (record(lock.packages))
|
|
48
|
+
return Object.entries(lock.packages);
|
|
49
|
+
return legacyLockEntries(lock.dependencies);
|
|
50
|
+
}
|
|
51
|
+
function registryViolationDomain(resolved, allowedHosts) {
|
|
52
|
+
if (!/^[a-z][a-z0-9+.-]*:/i.test(resolved) &&
|
|
53
|
+
!resolved.startsWith('/') &&
|
|
54
|
+
!/^[^@]+@[^:]+:/.test(resolved))
|
|
55
|
+
return undefined;
|
|
56
|
+
const scp = /^[^@]+@([^:]+):/.exec(resolved);
|
|
57
|
+
if (scp?.[1])
|
|
58
|
+
return scp[1];
|
|
59
|
+
try {
|
|
60
|
+
const url = new URL(resolved);
|
|
61
|
+
const authority = /^https?:\/\/([^/?#]*)/i.exec(resolved)?.[1];
|
|
62
|
+
const allowed = (url.protocol === 'https:' || url.protocol === 'http:') &&
|
|
63
|
+
authority !== undefined &&
|
|
64
|
+
!authority.includes('@') &&
|
|
65
|
+
!authority.replace(/^\[[^\]]*\]/, '').includes(':') &&
|
|
66
|
+
!url.port &&
|
|
67
|
+
!url.username &&
|
|
68
|
+
!url.password &&
|
|
69
|
+
allowedHosts.includes(url.hostname);
|
|
70
|
+
return allowed ? undefined : url.hostname || url.protocol;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return 'invalid-url';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function lockRegistryPackageName(path, entry) {
|
|
77
|
+
return typeof entry.name === 'string'
|
|
78
|
+
? entry.name
|
|
79
|
+
: path.split('node_modules/').at(-1) || path || '(root)';
|
|
80
|
+
}
|
|
81
|
+
export function validateLockRegistries(lock, file, allowedHosts) {
|
|
82
|
+
const groups = new Map();
|
|
83
|
+
for (const [path, entry] of lockRegistryEntries(lock)) {
|
|
84
|
+
if (!record(entry) || entry.link === true)
|
|
85
|
+
continue;
|
|
86
|
+
if (typeof entry.resolved !== 'string')
|
|
87
|
+
continue;
|
|
88
|
+
const domain = registryViolationDomain(entry.resolved, allowedHosts);
|
|
89
|
+
if (domain === undefined)
|
|
90
|
+
continue;
|
|
91
|
+
const group = groups.get(domain) ?? {
|
|
92
|
+
count: 0,
|
|
93
|
+
packages: new Set(),
|
|
94
|
+
};
|
|
95
|
+
group.count++;
|
|
96
|
+
group.packages.add(lockRegistryPackageName(path, entry));
|
|
97
|
+
groups.set(domain, group);
|
|
98
|
+
}
|
|
99
|
+
if (groups.size)
|
|
100
|
+
throw new LockRegistryError(file, [...groups]
|
|
101
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
102
|
+
.map(([domain, group]) => ({
|
|
103
|
+
domain,
|
|
104
|
+
count: group.count,
|
|
105
|
+
packages: [...group.packages].sort().slice(0, 5),
|
|
106
|
+
})), allowedHosts);
|
|
107
|
+
}
|
|
108
|
+
/** Fast local validation before any remote calls or node_modules changes. */
|
|
109
|
+
export function validateDependencyLock(project, directory, npmVersion) {
|
|
110
|
+
const packagePath = join(project, 'package.json');
|
|
111
|
+
const lockName = existsSync(join(project, 'npm-shrinkwrap.json'))
|
|
112
|
+
? 'npm-shrinkwrap.json'
|
|
113
|
+
: 'package-lock.json';
|
|
114
|
+
const fail = (message) => {
|
|
115
|
+
throw new Error(`Dependency lock validation failed for ${directory}: ${message}\n${lockRepairHint(directory, npmVersion)}`);
|
|
116
|
+
};
|
|
117
|
+
let manifest;
|
|
118
|
+
let lock;
|
|
119
|
+
try {
|
|
120
|
+
manifest = JSON.parse(readFileSync(packagePath, 'utf8'));
|
|
121
|
+
lock = JSON.parse(readFileSync(join(project, lockName), 'utf8'));
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
fail(`Cannot read package.json or ${lockName}: ${String(error)}`);
|
|
125
|
+
}
|
|
126
|
+
if (!record(manifest) || !record(lock))
|
|
127
|
+
return fail('package.json and lock must be JSON objects.');
|
|
128
|
+
if (!Number.isSafeInteger(lock.lockfileVersion) ||
|
|
129
|
+
Number(lock.lockfileVersion) < 1) {
|
|
130
|
+
fail(`${lockName} has no supported lockfileVersion.`);
|
|
131
|
+
}
|
|
132
|
+
// v1 has no root manifest snapshot; npm ci performs the full graph validation.
|
|
133
|
+
if (lock.lockfileVersion === 1)
|
|
134
|
+
return;
|
|
135
|
+
const packages = lock.packages;
|
|
136
|
+
if (!record(packages) || !record(packages['']))
|
|
137
|
+
return fail(`${lockName} is missing its root package snapshot.`);
|
|
138
|
+
const root = packages[''];
|
|
139
|
+
if (Array.isArray(manifest.workspaces)) {
|
|
140
|
+
if (JSON.stringify(root.workspaces) !== JSON.stringify(manifest.workspaces))
|
|
141
|
+
fail('Workspace list differs from lock.');
|
|
142
|
+
for (const member of manifest.workspaces) {
|
|
143
|
+
if (typeof member !== 'string' || !['page', 'server'].includes(member))
|
|
144
|
+
fail('Unsupported workspace');
|
|
145
|
+
const pkg = JSON.parse(readFileSync(join(project, member, 'package.json'), 'utf8'));
|
|
146
|
+
const snapshot = packages[member];
|
|
147
|
+
if (!record(snapshot))
|
|
148
|
+
return fail(`Missing workspace snapshot ${member}`);
|
|
149
|
+
for (const field of [
|
|
150
|
+
'dependencies',
|
|
151
|
+
'devDependencies',
|
|
152
|
+
'optionalDependencies',
|
|
153
|
+
'peerDependencies',
|
|
154
|
+
]) {
|
|
155
|
+
const expected = pkg[field] ?? {}, actual = snapshot[field] ?? {};
|
|
156
|
+
if (!record(actual) || !record(expected))
|
|
157
|
+
return fail(`Invalid workspace ${field}`);
|
|
158
|
+
for (const name of new Set([
|
|
159
|
+
...Object.keys(expected),
|
|
160
|
+
...Object.keys(actual),
|
|
161
|
+
]))
|
|
162
|
+
if (expected[name] !== actual[name])
|
|
163
|
+
fail(`Workspace ${member} ${field}.${name} differs from lock`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const field of [
|
|
168
|
+
'dependencies',
|
|
169
|
+
'devDependencies',
|
|
170
|
+
'optionalDependencies',
|
|
171
|
+
'peerDependencies',
|
|
172
|
+
]) {
|
|
173
|
+
const expected = manifest[field] ?? {};
|
|
174
|
+
const actual = root[field] ?? {};
|
|
175
|
+
if (!record(expected) || !record(actual))
|
|
176
|
+
return fail(`Invalid ${field} in package.json or ${lockName}.`);
|
|
177
|
+
for (const name of new Set([
|
|
178
|
+
...Object.keys(expected),
|
|
179
|
+
...Object.keys(actual),
|
|
180
|
+
])) {
|
|
181
|
+
// npm omits required entries overridden by optionalDependencies from
|
|
182
|
+
// the root lock snapshot. Check their effective spec in the optional group.
|
|
183
|
+
if (field === 'dependencies' &&
|
|
184
|
+
record(manifest.optionalDependencies) &&
|
|
185
|
+
Object.hasOwn(manifest.optionalDependencies, name)) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (expected[name] !== actual[name]) {
|
|
189
|
+
fail(`package.json ${field}.${name} does not match ${lockName}; synchronize the declared dependency and lock.`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|