Skip to content

Commit 2015486

Browse files
authored
feat(core): faster YAML and JSON parser (#299)
1 parent 54c203a commit 2015486

9 files changed

Lines changed: 35 additions & 41 deletions

File tree

apps/cli/src/command/convert.command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { OpenAPIConverter } from '@api7/adc-converter-openapi';
22
import * as ADCSDK from '@api7/adc-sdk';
33
import OpenAPIParser from '@readme/openapi-parser';
4+
import { dump } from 'js-yaml';
45
import { Listr } from 'listr2';
56
import { cloneDeep } from 'lodash';
67
import { existsSync, writeFileSync } from 'node:fs';
78
import { resolve } from 'node:path';
89
import { OpenAPIV3 } from 'openapi-types';
9-
import { stringify } from 'yaml';
1010

1111
import { SignaleRenderer } from '../utils/listr';
1212
import { TaskContext } from './diff.command';
@@ -103,7 +103,7 @@ const OpenAPICommand = new BaseConvertCommand('openapi')
103103
title: 'Write converted OpenAPI file',
104104
task: (ctx, task) => {
105105
ctx.local.services = ctx.buffer.flatMap((item) => item.services);
106-
const yamlStr = stringify(ctx.local, {});
106+
const yamlStr = dump(ctx.local, { noRefs: true, sortKeys: true });
107107
writeFileSync(opts.output, yamlStr);
108108
task.title = `Converted OpenAPI file to "${resolve(
109109
opts.output,

apps/cli/src/command/diff.command.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ADCSDK from '@api7/adc-sdk';
2+
import YAML from 'js-yaml';
23
import { Listr } from 'listr2';
3-
import { readFile, writeFile } from 'node:fs/promises';
4-
import YAML from 'yaml';
4+
import { writeFile } from 'node:fs/promises';
55

66
import {
77
DiffResourceTask,
@@ -82,7 +82,11 @@ export const DiffCommand = new BackendCommand<DiffOptions>(
8282
{
8383
title: 'Write detail diff result to file',
8484
task: async (ctx, task) => {
85-
await writeFile('./diff.yaml', YAML.stringify(ctx.diff), {});
85+
await writeFile(
86+
'./diff.yaml',
87+
YAML.dump(ctx.diff, { noRefs: true }),
88+
{ encoding: 'utf-8' },
89+
);
8690
task.output = 'Detail diff result has been wrote to diff.yaml';
8791
},
8892
},

apps/cli/src/command/dump.command.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { dump } from 'js-yaml';
12
import { Listr } from 'listr2';
23
import { writeFile } from 'node:fs/promises';
34
import path from 'node:path';
4-
import { Scalar, stringify } from 'yaml';
55

66
import { LoadRemoteConfigurationTask } from '../tasks';
77
import { InitializeBackendTask } from '../tasks/init_backend';
@@ -69,32 +69,9 @@ export const DumpCommand = new BackendCommand<DumpOptions>(
6969
task: async (ctx, task) => {
7070
await writeFile(
7171
opts.output,
72-
stringify(resortConfiguration(ctx.remote), {
73-
sortMapEntries: (a, b) => {
74-
const nameKey = 'name';
75-
const descKey = 'description';
76-
const labelKey = 'labels';
77-
const consumerUsernameKey = 'username';
78-
const aKey = (a.key as Scalar)?.value;
79-
const bKey = (b.key as Scalar)?.value;
80-
81-
// make sure the metadata is always at the top
82-
if (aKey && bKey) {
83-
if (aKey === nameKey || bKey === nameKey)
84-
return aKey === nameKey ? -1 : 1;
85-
if (
86-
aKey === consumerUsernameKey ||
87-
bKey === consumerUsernameKey
88-
)
89-
return aKey === consumerUsernameKey ? -1 : 1;
90-
if (aKey === descKey || bKey === descKey)
91-
return aKey === descKey ? -1 : 1;
92-
if (aKey === labelKey || bKey === labelKey)
93-
return aKey === labelKey ? -1 : 1;
94-
}
95-
96-
return a.key > b.key ? 1 : a.key < b.key ? -1 : 0;
97-
},
72+
dump(resortConfiguration(ctx.remote), {
73+
noRefs: true,
74+
sortKeys: true,
9875
}),
9976
{ encoding: 'utf8' },
10077
);

apps/cli/src/tasks/experimental.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { readFile } from 'fs/promises';
2-
import YAML from 'yaml';
2+
import YAML from 'js-yaml';
33

44
export const ExperimentalRemoteStateFileTask = (file: string) => ({
55
task: async (ctx) => {
66
const fileContent =
77
(await readFile(file, {
88
encoding: 'utf-8',
99
})) ?? '';
10-
ctx.remote = YAML.parse(fileContent);
10+
ctx.remote = YAML.load(fileContent);
1111
},
1212
});

apps/cli/src/tasks/load_local.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as ADCSDK from '@api7/adc-sdk';
22
import { existsSync } from 'fs';
33
import { readFile } from 'fs/promises';
44
import { globSync } from 'glob';
5+
import YAML from 'js-yaml';
56
import { ListrTask } from 'listr2';
67
import path from 'node:path';
7-
import YAML from 'yaml';
88

99
import {
1010
fillLabels,
@@ -51,7 +51,13 @@ export const LoadLocalConfigurationTask = (
5151
const fileContent =
5252
(await readFile(filePath, { encoding: 'utf-8' })) ?? '';
5353

54-
subCtx.configurations[filePath] = YAML.parse(fileContent) ?? {};
54+
const ext = path.extname(filePath).toLowerCase();
55+
if (ext === '.json') {
56+
subCtx.configurations[filePath] =
57+
JSON.parse(fileContent) ?? {};
58+
return;
59+
}
60+
subCtx.configurations[filePath] = YAML.load(fileContent) ?? {};
5561
},
5662
};
5763
}),

libs/converter-openapi/test/basic.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { readFileSync } from 'node:fs';
2-
import { join } from 'node:path';
3-
import { OpenAPIV3 } from 'openapi-types';
4-
import { parse } from 'yaml';
1+
import { load } from 'js-yaml';
52

63
import { OpenAPIConverter } from '../src';
74
import { loadAsset, runTask } from './utils';
85

6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
const parse = (content: string): any => load(content);
8+
99
describe('Basic', () => {
1010
it('case 1 (single path)', async () => {
1111
const oas = parse(loadAsset('basic-1.yaml'));

libs/converter-openapi/test/extension.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { parse } from 'yaml';
1+
import { load } from 'js-yaml';
22

33
import { OpenAPIConverter } from '../src';
44
import { loadAsset, runTask } from './utils';
55

6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
const parse = (content: string): any => load(content);
8+
69
describe('Extension', () => {
710
it('case 1 (override resource name)', async () => {
811
const oas = parse(loadAsset('extension-1.yaml'));

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"dotenv": "^16.4.5",
5959
"glob": "^11.0.0",
6060
"immer": "^10.1.1",
61+
"js-yaml": "^4.1.0",
6162
"json-schema": "^0.4.0",
6263
"listr2": "^8.2.1",
6364
"lodash": "^4.17.21",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)