Skip to content

Commit ba4c400

Browse files
committed
options: generate config schema in C++
1 parent 73cc947 commit ba4c400

7 files changed

Lines changed: 270 additions & 1210 deletions

File tree

lib/internal/options.js

Lines changed: 3 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
'use strict';
22

33
const {
4-
ArrayPrototypeMap,
5-
ArrayPrototypeSort,
6-
ObjectEntries,
7-
ObjectFromEntries,
8-
ObjectKeys,
9-
StringPrototypeReplace,
4+
JSONParse,
105
} = primordials;
116

127
const {
138
getCLIOptionsValues,
149
getCLIOptionsInfo,
1510
getOptionsAsFlags,
1611
getEmbedderOptions: getEmbedderOptionsFromBinding,
17-
getEnvOptionsInputType,
18-
getNamespaceOptionsInputType,
12+
getConfigJsonSchema,
1913
} = internalBinding('options');
2014

2115
let warnOnAllowUnauthorized = true;
@@ -46,101 +40,7 @@ function getEmbedderOptions() {
4640
}
4741

4842
function generateConfigJsonSchema() {
49-
const envOptionsMap = getEnvOptionsInputType();
50-
const namespaceOptionsMap = getNamespaceOptionsInputType();
51-
52-
function createPropertyForOptionDetail(detail) {
53-
const { type, description } = detail;
54-
if (type === 'array') {
55-
return {
56-
__proto__: null,
57-
oneOf: [
58-
{ __proto__: null, type: 'string' },
59-
{ __proto__: null, type: 'array', minItems: 1, items: { __proto__: null, type: 'string' } },
60-
],
61-
description,
62-
};
63-
}
64-
65-
return { __proto__: null, type, description };
66-
}
67-
68-
const schema = {
69-
__proto__: null,
70-
$schema: 'https://json-schema.org/draft/2020-12/schema',
71-
additionalProperties: false,
72-
required: [],
73-
properties: {
74-
$schema: {
75-
__proto__: null,
76-
type: 'string',
77-
},
78-
nodeOptions: {
79-
__proto__: null,
80-
additionalProperties: false,
81-
required: [],
82-
properties: { __proto__: null },
83-
type: 'object',
84-
},
85-
__proto__: null,
86-
},
87-
type: 'object',
88-
};
89-
90-
// Get the root properties object for adding namespaces
91-
const rootProperties = schema.properties;
92-
const nodeOptions = rootProperties.nodeOptions.properties;
93-
94-
// Add env options to nodeOptions (backward compatibility)
95-
for (const { 0: key, 1: type } of ObjectEntries(envOptionsMap)) {
96-
const keyWithoutPrefix = StringPrototypeReplace(key, '--', '');
97-
nodeOptions[keyWithoutPrefix] = createPropertyForOptionDetail(type);
98-
}
99-
100-
// Add namespace properties at the root level
101-
for (const { 0: namespace, 1: optionsMap } of namespaceOptionsMap) {
102-
// Create namespace object at the root level
103-
rootProperties[namespace] = {
104-
__proto__: null,
105-
type: 'object',
106-
additionalProperties: false,
107-
required: [],
108-
properties: { __proto__: null },
109-
};
110-
111-
const namespaceProperties = rootProperties[namespace].properties;
112-
113-
// Add all options for this namespace
114-
for (const { 0: optionName, 1: optionType } of ObjectEntries(optionsMap)) {
115-
const keyWithoutPrefix = StringPrototypeReplace(optionName, '--', '');
116-
namespaceProperties[keyWithoutPrefix] = createPropertyForOptionDetail(optionType);
117-
}
118-
119-
// Sort the namespace properties alphabetically
120-
const sortedNamespaceKeys = ArrayPrototypeSort(ObjectKeys(namespaceProperties));
121-
const sortedNamespaceProperties = ObjectFromEntries(
122-
ArrayPrototypeMap(sortedNamespaceKeys, (key) => [key, namespaceProperties[key]]),
123-
);
124-
rootProperties[namespace].properties = sortedNamespaceProperties;
125-
}
126-
127-
// Sort the top-level properties by key alphabetically
128-
const sortedKeys = ArrayPrototypeSort(ObjectKeys(nodeOptions));
129-
const sortedProperties = ObjectFromEntries(
130-
ArrayPrototypeMap(sortedKeys, (key) => [key, nodeOptions[key]]),
131-
);
132-
133-
schema.properties.nodeOptions.properties = sortedProperties;
134-
135-
// Also sort the root level properties
136-
const sortedRootKeys = ArrayPrototypeSort(ObjectKeys(rootProperties));
137-
const sortedRootProperties = ObjectFromEntries(
138-
ArrayPrototypeMap(sortedRootKeys, (key) => [key, rootProperties[key]]),
139-
);
140-
141-
schema.properties = sortedRootProperties;
142-
143-
return schema;
43+
return JSONParse(getConfigJsonSchema());
14444
}
14545

14646
function refreshOptions() {

src/node_config_file.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "node_config_file.h"
22
#include "ata.h"
33
#include "debug_utils-inl.h"
4-
#include "node_config_schema.h"
4+
#include "node_options.h"
55
#include "simdjson.h"
66

77
namespace node {
@@ -237,8 +237,8 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
237237
}
238238

239239
{
240-
static const ata::schema_ref compiled_schema =
241-
ata::compile(kNodeConfigSchema);
240+
static const ata::schema_ref compiled_schema = ata::compile(
241+
options_parser::GenerateConfigJsonSchema(false));
242242
CHECK(compiled_schema);
243243
auto result = ata::validate(compiled_schema, file_content);
244244
if (!result.valid) {

0 commit comments

Comments
 (0)