@hologit/lens-lib-k8s 0.6.0 → 0.6.2

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.
Files changed (2) hide show
  1. package/index.js +86 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,6 +1,85 @@
1
1
  const fs = require('fs');
2
2
  const yaml = require('js-yaml');
3
3
 
4
+ // Create a custom int type that handles both regular ints and octals
5
+ const CustomIntType = new yaml.Type('tag:yaml.org,2002:int', {
6
+ kind: 'scalar',
7
+ resolve: function (data) {
8
+ if (data === null || data === undefined) return false;
9
+
10
+ // Check for octal pattern first
11
+ const octalPattern = /^0[0-7]+$/;
12
+ if (octalPattern.test(data)) {
13
+ return true;
14
+ }
15
+
16
+ // Otherwise check for regular int patterns
17
+ const intPattern = /^[-+]?[0-9]+$/;
18
+ const hexPattern = /^0x[0-9a-fA-F]+$/;
19
+ return intPattern.test(data) || hexPattern.test(data);
20
+ },
21
+ construct: function (data) {
22
+ const octalPattern = /^0[0-7]+$/;
23
+
24
+ if (octalPattern.test(data)) {
25
+ // Parse as octal and return a wrapper object
26
+ const value = parseInt(data, 8);
27
+ return { __octal: true, value: value, original: data };
28
+ }
29
+
30
+ // Parse as regular int
31
+ return parseInt(data, 10);
32
+ },
33
+ predicate: function (object) {
34
+ // Explicitly reject null and undefined
35
+ if (object === null || object === undefined) {
36
+ return false;
37
+ }
38
+ // Check if this is our octal wrapper object or a regular number
39
+ if (typeof object === 'object' && object.__octal === true) {
40
+ return true;
41
+ }
42
+ return Object.prototype.toString.call(object) === '[object Number]' &&
43
+ (object % 1 === 0 && !isNaN(object));
44
+ },
45
+ represent: function (object) {
46
+ // If it's an octal wrapper, use the stored format
47
+ if (object && typeof object === 'object' && object.__octal === true) {
48
+ return object.original;
49
+ }
50
+ // Otherwise represent as decimal
51
+ return String(object);
52
+ },
53
+ defaultStyle: 'decimal'
54
+ });
55
+
56
+ // Create a custom schema with our custom int type
57
+ // We extend the CORE schema (which has null, bool, etc.) and add our custom int type
58
+ // This ensures null/bool are processed before our int type
59
+ const OCTAL_SCHEMA = yaml.DEFAULT_SCHEMA.extend({
60
+ implicit: [CustomIntType]
61
+ });
62
+
63
+ /**
64
+ * Load YAML content with OCTAL_SCHEMA by default
65
+ * @param {string} content - YAML content to parse
66
+ * @param {object} options - Additional options to pass to yaml.loadAll
67
+ * @returns {array} Array of parsed objects
68
+ */
69
+ function loadYaml(content, options = {}) {
70
+ return yaml.loadAll(content, { schema: OCTAL_SCHEMA, ...options });
71
+ }
72
+
73
+ /**
74
+ * Dump object to YAML with OCTAL_SCHEMA by default
75
+ * @param {object} object - Object to serialize
76
+ * @param {object} options - Additional options to pass to yaml.dump
77
+ * @returns {string} YAML string
78
+ */
79
+ function dumpYaml(object, options = {}) {
80
+ return yaml.dump(object, { schema: OCTAL_SCHEMA, ...options });
81
+ }
82
+
4
83
  // List of kinds that don't support namespaces
5
84
  const namespacelessKinds = [
6
85
  'apiservices',
@@ -78,7 +157,7 @@ async function patchNamespaces(yamlPath, {
78
157
  }
79
158
 
80
159
  // load objects
81
- const objects = yaml.loadAll(fs.readFileSync(yamlPath, 'utf8'));
160
+ const objects = loadYaml(fs.readFileSync(yamlPath, 'utf8'));
82
161
 
83
162
  // patch namespaces
84
163
  let patchedCount = 0;
@@ -115,7 +194,7 @@ async function patchNamespaces(yamlPath, {
115
194
  yamlPath,
116
195
  objects
117
196
  .filter(obj => obj !== null)
118
- .map(object => yaml.dump(object))
197
+ .map(object => dumpYaml(object))
119
198
  .join('\n---\n\n')
120
199
  );
121
200
  console.error(`patched ${patchedCount} namespaces in ${yamlPath}`);
@@ -127,6 +206,9 @@ module.exports = {
127
206
  generateNamespaceManifest,
128
207
  patchNamespaces,
129
208
 
130
- // Export common utilities that might be needed by lenses
131
- yaml
209
+ // Export YAML utilities
210
+ yaml,
211
+ OCTAL_SCHEMA,
212
+ loadYaml,
213
+ dumpYaml
132
214
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hologit/lens-lib-k8s",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Shared kubernetes library for hologit lenses",
5
5
  "main": "index.js",
6
6
  "scripts": {