Skip to content

Commit 80cbb7a

Browse files
committed
chore: replace js-yaml with yaml for YAML 1.1 compliance
1 parent 8e3d654 commit 80cbb7a

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@
6262
"form-data": "^4.0.0",
6363
"hpagent": "^1.2.0",
6464
"isomorphic-ws": "^5.0.0",
65-
"js-yaml": "^4.1.0",
6665
"jsonpath-plus": "^10.3.0",
6766
"node-fetch": "^2.6.9",
6867
"openid-client": "^6.1.3",
6968
"rfc4648": "^1.3.0",
7069
"socks-proxy-agent": "^8.0.4",
7170
"stream-buffers": "^3.0.2",
7271
"tar-fs": "^3.0.9",
73-
"ws": "^8.18.2"
72+
"ws": "^8.18.2",
73+
"yaml": "^2.8.0"
7474
},
7575
"devDependencies": {
7676
"@eslint/js": "^9.18.0",

src/yaml.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import yaml from 'js-yaml';
1+
import YAML from 'yaml';
22
import { getSerializationType } from './util.js';
33
import { KubernetesObject } from './types.js';
44
import { ObjectSerializer } from './serializer.js';
@@ -9,13 +9,12 @@ import { ObjectSerializer } from './serializer.js';
99
* @param opts - Optional YAML load options.
1010
* @returns The deserialized Kubernetes object.
1111
*/
12-
export function loadYaml<T>(data: string, opts?: yaml.LoadOptions): T {
13-
const yml = yaml.load(data, opts) as any as KubernetesObject;
12+
export function loadYaml<T>(data: string): T {
13+
const yml = YAML.parse(data, { version: '1.1' }) as any as KubernetesObject;
1414
if (!yml) {
1515
throw new Error('Failed to load YAML');
1616
}
1717
const type = getSerializationType(yml.apiVersion, yml.kind);
18-
1918
return ObjectSerializer.deserialize(yml, type) as T;
2019
}
2120

@@ -25,12 +24,12 @@ export function loadYaml<T>(data: string, opts?: yaml.LoadOptions): T {
2524
* @param opts - Optional YAML load options.
2625
* @returns An array of deserialized Kubernetes objects.
2726
*/
28-
export function loadAllYaml(data: string, opts?: yaml.LoadOptions): any[] {
29-
const ymls = yaml.loadAll(data, undefined, opts);
30-
return ymls.map((yml) => {
31-
const obj = yml as KubernetesObject;
27+
export function loadAllYaml(data: string): any[] {
28+
const ymls = YAML.parseAllDocuments(data, { version: '1.1' });
29+
return ymls.map((doc) => {
30+
const obj = doc.toJS() as KubernetesObject;
3231
const type = getSerializationType(obj.apiVersion, obj.kind);
33-
return ObjectSerializer.deserialize(yml, type);
32+
return ObjectSerializer.deserialize(obj, type);
3433
});
3534
}
3635

@@ -40,9 +39,9 @@ export function loadAllYaml(data: string, opts?: yaml.LoadOptions): any[] {
4039
* @param opts - Optional YAML dump options.
4140
* @returns The YAML string representation of the serialized Kubernetes object.
4241
*/
43-
export function dumpYaml(object: any, opts?: yaml.DumpOptions): string {
42+
export function dumpYaml(object: any): string {
4443
const kubeObject = object as KubernetesObject;
4544
const type = getSerializationType(kubeObject.apiVersion, kubeObject.kind);
4645
const serialized = ObjectSerializer.serialize(kubeObject, type);
47-
return yaml.dump(serialized, opts);
46+
return YAML.stringify(serialized);
4847
}

src/yaml_test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,39 @@ spec:
154154
// not using strict equality as types are not matching
155155
deepEqual(actual, expected);
156156
});
157+
158+
it('should parse octal-like strings as numbers (YAML 1.1 style)', () => {
159+
const yaml = `
160+
defaultMode: 0644
161+
fileMode: 0755
162+
`;
163+
const result = loadYaml<{
164+
defaultMode: number;
165+
fileMode: number;
166+
}>(yaml);
167+
168+
// 0644 (octal) = 420 decimal, 0755 = 493
169+
strictEqual(result.defaultMode, 420);
170+
strictEqual(result.fileMode, 493);
171+
});
172+
173+
it('should parse boolean-like strings as booleans (YAML 1.1 style)', () => {
174+
const yaml = `
175+
enableFeature: yes
176+
debugMode: ON
177+
maintenance: no
178+
safeMode: off
179+
`;
180+
const result = loadYaml<{
181+
enableFeature: boolean;
182+
debugMode: boolean;
183+
maintenance: boolean;
184+
safeMode: boolean;
185+
}>(yaml);
186+
187+
strictEqual(result.enableFeature, true);
188+
strictEqual(result.debugMode, true);
189+
strictEqual(result.maintenance, false);
190+
strictEqual(result.safeMode, false);
191+
});
157192
});

0 commit comments

Comments
 (0)