@cloudoperators/juno-k8s-client 1.1.3 → 1.1.6

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("https");function I(t){return typeof t=="string"||t instanceof String}function $(t){return typeof t=="number"&&t.constructor===Number}function B(t){return t!==null&&typeof t=="object"&&t.constructor===Object}const H=t=>{if(!t)return;const n=[];for(const o in t)if(Object.keys(t).includes(o)){const h=t[o];n.push(encodeURIComponent(o)+"="+encodeURIComponent(h))}return n.join("&")};function p(...t){if(t.length<2)throw new Error("Bad parameters: buildUrl needs at least two non-empty parameters.");const n=B(t[t.length-1])?t.pop():null;t.forEach(r=>{if(!r||!(I(r)||$(r)))throw new Error("Bad parameters: buildUrl accepts only strings and numbers, with a hash map as the last parameter");if(I(r)&&r.indexOf("?")>=0)throw new Error('Bad parameters: buildUrl does not accept strings with "?" character. Please use a hashmap as the last parameter instead!')});let o=t.join("/");const[h,e]=o.split("://");if(!h||!e)throw new Error(`Bad URL: ${o}`);return o=`${h}://${e.replace(/\/\/+/g,"/")}`,n&&(o+=`?${H(n)}`),o}const k="[k8sClient]:",C=t=>function(...n){try{const o=[`${k}`,...n];console[t](...o)}catch(o){console.error(k,"Logging error:",o)}},P=C("error"),l=C("debug"),U=t=>{if(t.status>=200&&t.status<300)return t;{const n=new Error(t.statusText||`${t.status}`);throw n.response=t,n}};function X(t,n){if(!(!t||!n.startsWith("https:")||typeof window<"u"))try{return j?.Agent?new j.Agent({rejectUnauthorized:!1}):void 0}catch{return}}function E(t,n,o={}){o.params&&(n=p(n,o.params));const{ignoreSsl:h,debug:e,...r}=o,s=X(h||!1,n);s&&e===!0&&process.env.NODE_ENV!=="test"&&l(`⚠️ SSL verification disabled for request to: ${n}`);const a=["signal","headers","body","mode","cache","credentials"].reduce((m,f)=>r[f]?{...m,[f]:r[f]}:m,{credentials:"same-origin",method:t});return s&&(a.agent=s),a.body&&typeof a.body!="string"&&(a.body=JSON.stringify(a.body)),e===!0&&l("fetch >",n,{...a,agent:s?"HTTPS Agent (SSL ignored)":a.agent}),fetch(n,a).then(U)}const w="ADDED",R="MODIFIED",A="DELETED",b="ERROR",W="BOOKMARK";class S{constructor(n,o={},h){this.listeners={[w]:[],[R]:[],[A]:[],[b]:[]},this.controller=new AbortController,this.signal=this.controller.signal,this.resourceVersion=null,this.PREFIX="watch >",this.decoder=new TextDecoder,this.refreshToken=e=>{this.options.headers&&(this.options.headers.Authorization=`Bearer ${e}`)},this.checkType=e=>{if(![w,R,A,b].includes(e))throw new Error(`Bad type: ${e}`)},this.addListener=(e,r)=>{this.checkType(e),this.listeners[e].push(r)},this.informListeners=(e,r)=>{const s=this.listeners[e];s&&s.forEach(c=>{try{c(r)}catch(a){P(a)}})},this.handleEvents=e=>{Array.isArray(e)||(e=[e]);const r=this;setTimeout(()=>{const s={};e.forEach(c=>{c.type===W?r.resourceVersion=c?.object?.metadata?.resourceVersion??null:(s[c.type]||(s[c.type]=[]),s[c.type].push(c.object))}),Object.keys(s).forEach(c=>r.informListeners(c,s[c]))})},this.getResourceVersion=async()=>{if(this.resourceVersion)return this.resourceVersion;l(this.PREFIX,"get resource version from API");const{metadata:e,items:r}=await E("GET",this.url,this.options).then(s=>s.json());return this.resourceVersion=e.resourceVersion,r&&this.informListeners(w,r),this.resourceVersion},this.start=()=>{const e=this;return e.getCurrentToken&&e.refreshToken(e.getCurrentToken()),e.getResourceVersion().then(r=>(l(e.PREFIX,"current resource version",r),E("GET",e.url,{...e.options,params:{...e.options.params,watch:1,allowWatchBookmarks:!0,resourceVersion:r},signal:e.signal}))).then(async r=>{const s=r.body?.getReader();let c=!1,a="";for(;!c;){l(e.PREFIX,"read stream chunk");const m=await s?.read();c=m?.done??!0,a+=e.decoder.decode(m?.value||new Uint8Array);const f=a.split(/\n|\r|\r\n/);a=f.pop()??"";const D=[];f.forEach(O=>{const T=JSON.parse(O);if(T.type===b&&T?.object?.code===410){e.cancel(),setTimeout(()=>{l(e.PREFIX,"resource is gone 410","recreate watch request!"),e.start()});return}D.push(T)}),l(e.PREFIX,"inform listeners",f),e.handleEvents(D)}}).catch(r=>{if(r.name==="AbortError")return;const s=r.code??r?.response?.status;if(s===410){l(e.PREFIX,"resource is gone 410"),e.resourceVersion=null,setTimeout(()=>e.start(),0);return}if(s===404){e.handleEvents({type:b,object:r}),e.cancel();return}P("WATCH ERROR",r,r.code,s,r.response),console.dir(r),setTimeout(()=>{l(e.PREFIX,"recover connection","restart!"),e.start()},3e4)}),e.cancel},this.on=(e,r)=>(this.addListener(e,r),this),this.cancel=()=>{l(this.PREFIX,"cancel connection"),this.controller.abort()},this.url=n,this.options=o,this.getCurrentToken=h,o?.params?.resourceVersion&&(this.resourceVersion=o.params.resourceVersion,delete o.params.resourceVersion)}}const y=async t=>{const n=t.response?.data?new Error(t.response.data.message??t.message):t;return Promise.reject(n)};function v(t){const{apiEndpoint:n,ignoreSsl:o=!1,debug:h=!1}=t;let e=t.token;if(!n||!e)throw new Error(`Bad options: ${JSON.stringify(t,null,4)}. Please provide apiEndpoint and token`);o&&h===!0&&(console.warn(`⚠️ K8s Client: SSL certificate verification disabled for ${n}`),console.warn("🔒 This should only be used in development or secure internal networks"));const r=()=>({Authorization:`Bearer ${e}`,"Content-Type":"application/json"});function s(i,u={}){const d={...r(),...u.headers,...i.headers};return{debug:h,ignoreSsl:o,...u,...i,headers:d}}function c(i,u={}){return E("HEAD",p(n,i),s(u)).catch(y)}function a(i,u={}){return E("GET",p(n,i),s(u)).then(d=>d.json()).catch(y)}function m(i,u,d={}){return E("POST",p(n,i),s(d,{body:u})).then(V=>V.json()).catch(y)}function f(i,u,d={}){return E("PUT",p(n,i),s(d,{body:u})).then(g=>g.json()).catch(y)}function D(i,u,d={}){return E("PATCH",p(n,i),s(d,{headers:{"Content-Type":"application/merge-patch+json"},body:u})).then(g=>g.json()).catch(y)}function O(i,u,d={}){return E("DELETE",p(n,i),s(d,{body:u})).then(g=>g.json()).catch(y)}function T(i,u={}){return new S(p(n,i),s(u),()=>e)}function F(i){e=i}function L(){return e}return{WATCH_ERROR:b,WATCH_ADDED:w,WATCH_DELETED:A,WATCH_MODIFIED:R,head:c,get:a,post:m,put:f,patch:D,delete:O,watch:T,refreshToken:F,currentToken:L}}exports.ADDED=w;exports.DELETED=A;exports.ERROR=b;exports.MODIFIED=R;exports.Watch=S;exports.createClient=v;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,s)=>(s=n==null?{}:e(i(n)),o(r||!n||!n.__esModule||!a.call(n,`default`)?t(s,`default`,{value:n,enumerable:!0}):s,n));let c=require("https");c=s(c);function l(e){return typeof e==`string`||e instanceof String}function u(e){return typeof e==`number`&&e.constructor===Number}function d(e){return typeof e==`object`&&!!e&&e.constructor===Object}var f=e=>{if(!e)return;let t=[];for(let n in e)if(Object.keys(e).includes(n)){let r=e[n];t.push(encodeURIComponent(n)+`=`+encodeURIComponent(r))}return t.join(`&`)};function p(...e){if(e.length<2)throw Error(`Bad parameters: buildUrl needs at least two non-empty parameters.`);let t=d(e[e.length-1])?e.pop():null;e.forEach(e=>{if(!e||!(l(e)||u(e)))throw Error(`Bad parameters: buildUrl accepts only strings and numbers, with a hash map as the last parameter`);if(l(e)&&e.indexOf(`?`)>=0)throw Error(`Bad parameters: buildUrl does not accept strings with "?" character. Please use a hashmap as the last parameter instead!`)});let n=e.join(`/`),[r,i]=n.split(`://`);if(!r||!i)throw Error(`Bad URL: ${n}`);return n=`${r}://${i.replace(/\/\/+/g,`/`)}`,t&&(n+=`?${f(t)}`),n}var m=`[k8sClient]:`,h=e=>function(...t){try{let n=[`${m}`,...t];console[e](...n)}catch(e){console.error(m,`Logging error:`,e)}},g=h(`error`),_=h(`debug`),v=e=>{if(e.status>=200&&e.status<300)return e;{let t=Error(e.statusText||`${e.status}`);throw t.response=e,t}};function y(e,t){if(e&&t.startsWith(`https:`)&&typeof window>`u`)try{return c.default?.Agent?new c.default.Agent({rejectUnauthorized:!1}):void 0}catch{return}}function b(e,t,n={}){n.params&&(t=p(t,n.params));let{ignoreSsl:r,debug:i,...a}=n,o=y(r||!1,t);o&&i===!0&&process.env.NODE_ENV!==`test`&&_(`⚠️ SSL verification disabled for request to: ${t}`);let s=[`signal`,`headers`,`body`,`mode`,`cache`,`credentials`].reduce((e,t)=>a[t]?{...e,[t]:a[t]}:e,{credentials:`same-origin`,method:e});return o&&(s.agent=o),s.body&&typeof s.body!=`string`&&(s.body=JSON.stringify(s.body)),i===!0&&_(`fetch >`,t,{...s,agent:o?`HTTPS Agent (SSL ignored)`:s.agent}),fetch(t,s).then(v)}var x=`ADDED`,S=`MODIFIED`,C=`DELETED`,w=`ERROR`,T=`BOOKMARK`,E=class{constructor(e,t={},n){this.listeners={[x]:[],[S]:[],[C]:[],[w]:[]},this.controller=new AbortController,this.signal=this.controller.signal,this.resourceVersion=null,this.PREFIX=`watch >`,this.decoder=new TextDecoder,this.refreshToken=e=>{this.options.headers&&(this.options.headers.Authorization=`Bearer ${e}`)},this.checkType=e=>{if(![`ADDED`,`MODIFIED`,`DELETED`,`ERROR`].includes(e))throw Error(`Bad type: ${e}`)},this.addListener=(e,t)=>{this.checkType(e),this.listeners[e].push(t)},this.informListeners=(e,t)=>{let n=this.listeners[e];n&&n.forEach(e=>{try{e(t)}catch(e){g(e)}})},this.handleEvents=e=>{Array.isArray(e)||(e=[e]);let t=this;setTimeout(()=>{let n={};e.forEach(e=>{e.type===T?t.resourceVersion=e?.object?.metadata?.resourceVersion??null:(n[e.type]||(n[e.type]=[]),n[e.type].push(e.object))}),Object.keys(n).forEach(e=>t.informListeners(e,n[e]))})},this.getResourceVersion=async()=>{if(this.resourceVersion)return this.resourceVersion;_(this.PREFIX,`get resource version from API`);let{metadata:e,items:t}=await b(`GET`,this.url,this.options).then(e=>e.json());return this.resourceVersion=e.resourceVersion,t&&this.informListeners(x,t),this.resourceVersion},this.start=()=>{let e=this;return e.getCurrentToken&&e.refreshToken(e.getCurrentToken()),e.getResourceVersion().then(t=>(_(e.PREFIX,`current resource version`,t),b(`GET`,e.url,{...e.options,params:{...e.options.params,watch:1,allowWatchBookmarks:!0,resourceVersion:t},signal:e.signal}))).then(async t=>{let n=t.body?.getReader(),r=!1,i=``;for(;!r;){_(e.PREFIX,`read stream chunk`);let t=await n?.read();r=t?.done??!0,i+=e.decoder.decode(t?.value||new Uint8Array);let a=i.split(/\n|\r|\r\n/);i=a.pop()??``;let o=[];a.forEach(t=>{let n=JSON.parse(t);if(n.type===`ERROR`&&n?.object?.code===410){e.cancel(),setTimeout(()=>{_(e.PREFIX,`resource is gone 410`,`recreate watch request!`),e.start()});return}o.push(n)}),_(e.PREFIX,`inform listeners`,a),e.handleEvents(o)}}).catch(t=>{if(t.name===`AbortError`)return;let n=t.code??t?.response?.status;if(n===410){_(e.PREFIX,`resource is gone 410`),e.resourceVersion=null,setTimeout(()=>e.start(),0);return}if(n===404){e.handleEvents({type:w,object:t}),e.cancel();return}g(`WATCH ERROR`,t,t.code,n,t.response),console.dir(t),setTimeout(()=>{_(e.PREFIX,`recover connection`,`restart!`),e.start()},3e4)}),e.cancel},this.on=(e,t)=>(this.addListener(e,t),this),this.cancel=()=>{_(this.PREFIX,`cancel connection`),this.controller.abort()},this.url=e,this.options=t,this.getCurrentToken=n,t?.params?.resourceVersion&&(this.resourceVersion=t.params.resourceVersion,delete t.params.resourceVersion)}},D=async e=>{let t=e.response?.data?Error(e.response.data.message??e.message):e;return Promise.reject(t)};function O(e){let{apiEndpoint:t,ignoreSsl:n=!1,debug:r=!1}=e,i=e.token;if(!t||!i)throw Error(`Bad options: ${JSON.stringify(e,null,4)}. Please provide apiEndpoint and token`);n&&r===!0&&(console.warn(`⚠️ K8s Client: SSL certificate verification disabled for ${t}`),console.warn(`🔒 This should only be used in development or secure internal networks`));let a=()=>({Authorization:`Bearer ${i}`,"Content-Type":`application/json`});function o(e,t={}){let i={...a(),...t.headers,...e.headers};return{debug:r,ignoreSsl:n,...t,...e,headers:i}}function s(e,n={}){return b(`HEAD`,p(t,e),o(n)).catch(D)}function c(e,n={}){return b(`GET`,p(t,e),o(n)).then(e=>e.json()).catch(D)}function l(e,n,r={}){return b(`POST`,p(t,e),o(r,{body:n})).then(e=>e.json()).catch(D)}function u(e,n,r={}){return b(`PUT`,p(t,e),o(r,{body:n})).then(e=>e.json()).catch(D)}function d(e,n,r={}){return b(`PATCH`,p(t,e),o(r,{headers:{"Content-Type":`application/merge-patch+json`},body:n})).then(e=>e.json()).catch(D)}function f(e,n,r={}){return b(`DELETE`,p(t,e),o(r,{body:n})).then(e=>e.json()).catch(D)}function m(e,n={}){return new E(p(t,e),o(n),()=>i)}function h(e){i=e}function g(){return i}return{WATCH_ERROR:w,WATCH_ADDED:x,WATCH_DELETED:C,WATCH_MODIFIED:S,head:s,get:c,post:l,put:u,patch:d,delete:f,watch:m,refreshToken:h,currentToken:g}}exports.ADDED=x,exports.DELETED=C,exports.ERROR=w,exports.MODIFIED=S,exports.Watch=E,exports.createClient=O;
package/build/index.es.js CHANGED
@@ -1,259 +1,251 @@
1
- import j from "https";
2
- function k(t) {
3
- return typeof t == "string" || t instanceof String;
1
+ import e from "https";
2
+ //#region src/utils.ts
3
+ function t(e) {
4
+ return typeof e == "string" || e instanceof String;
4
5
  }
5
- function V(t) {
6
- return typeof t == "number" && t.constructor === Number;
6
+ function n(e) {
7
+ return typeof e == "number" && e.constructor === Number;
7
8
  }
8
- function $(t) {
9
- return t !== null && typeof t == "object" && t.constructor === Object;
9
+ function r(e) {
10
+ return typeof e == "object" && !!e && e.constructor === Object;
10
11
  }
11
- const B = (t) => {
12
- if (!t) return;
13
- const n = [];
14
- for (const o in t)
15
- if (Object.keys(t).includes(o)) {
16
- const h = t[o];
17
- n.push(encodeURIComponent(o) + "=" + encodeURIComponent(h));
18
- }
19
- return n.join("&");
12
+ //#endregion
13
+ //#region src/urlHelpers.ts
14
+ var i = (e) => {
15
+ if (!e) return;
16
+ let t = [];
17
+ for (let n in e) if (Object.keys(e).includes(n)) {
18
+ let r = e[n];
19
+ t.push(encodeURIComponent(n) + "=" + encodeURIComponent(r));
20
+ }
21
+ return t.join("&");
20
22
  };
21
- function p(...t) {
22
- if (t.length < 2)
23
- throw new Error("Bad parameters: buildUrl needs at least two non-empty parameters.");
24
- const n = $(t[t.length - 1]) ? t.pop() : null;
25
- t.forEach((r) => {
26
- if (!r || !(k(r) || V(r)))
27
- throw new Error(
28
- "Bad parameters: buildUrl accepts only strings and numbers, with a hash map as the last parameter"
29
- );
30
- if (k(r) && r.indexOf("?") >= 0)
31
- throw new Error(
32
- 'Bad parameters: buildUrl does not accept strings with "?" character. Please use a hashmap as the last parameter instead!'
33
- );
34
- });
35
- let o = t.join("/");
36
- const [h, e] = o.split("://");
37
- if (!h || !e)
38
- throw new Error(`Bad URL: ${o}`);
39
- return o = `${h}://${e.replace(/\/\/+/g, "/")}`, n && (o += `?${B(n)}`), o;
23
+ function a(...e) {
24
+ if (e.length < 2) throw Error("Bad parameters: buildUrl needs at least two non-empty parameters.");
25
+ let a = r(e[e.length - 1]) ? e.pop() : null;
26
+ e.forEach((e) => {
27
+ if (!e || !(t(e) || n(e))) throw Error("Bad parameters: buildUrl accepts only strings and numbers, with a hash map as the last parameter");
28
+ if (t(e) && e.indexOf("?") >= 0) throw Error("Bad parameters: buildUrl does not accept strings with \"?\" character. Please use a hashmap as the last parameter instead!");
29
+ });
30
+ let o = e.join("/"), [s, c] = o.split("://");
31
+ if (!s || !c) throw Error(`Bad URL: ${o}`);
32
+ return o = `${s}://${c.replace(/\/\/+/g, "/")}`, a && (o += `?${i(a)}`), o;
40
33
  }
41
- const I = "[k8sClient]:", C = (t) => function(...n) {
42
- try {
43
- const o = [`${I}`, ...n];
44
- console[t](...o);
45
- } catch (o) {
46
- console.error(I, "Logging error:", o);
47
- }
48
- }, P = C("error"), l = C("debug"), H = (t) => {
49
- if (t.status >= 200 && t.status < 300)
50
- return t;
51
- {
52
- const n = new Error(t.statusText || `${t.status}`);
53
- throw n.response = t, n;
54
- }
34
+ //#endregion
35
+ //#region src/logger.ts
36
+ var o = "[k8sClient]:", s = (e) => function(...t) {
37
+ try {
38
+ let n = [`${o}`, ...t];
39
+ console[e](...n);
40
+ } catch (e) {
41
+ console.error(o, "Logging error:", e);
42
+ }
43
+ }, c = s("error"), l = s("debug"), u = (e) => {
44
+ if (e.status >= 200 && e.status < 300) return e;
45
+ {
46
+ let t = Error(e.statusText || `${e.status}`);
47
+ throw t.response = e, t;
48
+ }
55
49
  };
56
- function U(t, n) {
57
- if (!(!t || !n.startsWith("https:") || typeof window < "u"))
58
- try {
59
- return j?.Agent ? new j.Agent({
60
- rejectUnauthorized: !1
61
- }) : void 0;
62
- } catch {
63
- return;
64
- }
50
+ function d(t, n) {
51
+ if (t && n.startsWith("https:") && typeof window > "u") try {
52
+ return e?.Agent ? new e.Agent({ rejectUnauthorized: !1 }) : void 0;
53
+ } catch {
54
+ return;
55
+ }
65
56
  }
66
- function E(t, n, o = {}) {
67
- o.params && (n = p(n, o.params));
68
- const { ignoreSsl: h, debug: e, ...r } = o, s = U(h || !1, n);
69
- s && e === !0 && process.env.NODE_ENV !== "test" && l(`⚠️ SSL verification disabled for request to: ${n}`);
70
- const a = ["signal", "headers", "body", "mode", "cache", "credentials"].reduce(
71
- (m, f) => r[f] ? { ...m, [f]: r[f] } : m,
72
- { credentials: "same-origin", method: t }
73
- );
74
- return s && (a.agent = s), a.body && typeof a.body != "string" && (a.body = JSON.stringify(a.body)), e === !0 && l("fetch >", n, {
75
- ...a,
76
- agent: s ? "HTTPS Agent (SSL ignored)" : a.agent
77
- }), fetch(n, a).then(H);
57
+ function f(e, t, n = {}) {
58
+ n.params && (t = a(t, n.params));
59
+ let { ignoreSsl: r, debug: i, ...o } = n, s = d(r || !1, t);
60
+ s && i === !0 && process.env.NODE_ENV !== "test" && l(`⚠️ SSL verification disabled for request to: ${t}`);
61
+ let c = [
62
+ "signal",
63
+ "headers",
64
+ "body",
65
+ "mode",
66
+ "cache",
67
+ "credentials"
68
+ ].reduce((e, t) => o[t] ? {
69
+ ...e,
70
+ [t]: o[t]
71
+ } : e, {
72
+ credentials: "same-origin",
73
+ method: e
74
+ });
75
+ return s && (c.agent = s), c.body && typeof c.body != "string" && (c.body = JSON.stringify(c.body)), i === !0 && l("fetch >", t, {
76
+ ...c,
77
+ agent: s ? "HTTPS Agent (SSL ignored)" : c.agent
78
+ }), fetch(t, c).then(u);
78
79
  }
79
- const R = "ADDED", A = "MODIFIED", O = "DELETED", w = "ERROR", X = "BOOKMARK";
80
- class W {
81
- constructor(n, o = {}, h) {
82
- this.listeners = {
83
- [R]: [],
84
- [A]: [],
85
- [O]: [],
86
- [w]: []
87
- }, this.controller = new AbortController(), this.signal = this.controller.signal, this.resourceVersion = null, this.PREFIX = "watch >", this.decoder = new TextDecoder(), this.refreshToken = (e) => {
88
- this.options.headers && (this.options.headers.Authorization = `Bearer ${e}`);
89
- }, this.checkType = (e) => {
90
- if (![R, A, O, w].includes(e))
91
- throw new Error(`Bad type: ${e}`);
92
- }, this.addListener = (e, r) => {
93
- this.checkType(e), this.listeners[e].push(r);
94
- }, this.informListeners = (e, r) => {
95
- const s = this.listeners[e];
96
- s && s.forEach((c) => {
97
- try {
98
- c(r);
99
- } catch (a) {
100
- P(a);
101
- }
102
- });
103
- }, this.handleEvents = (e) => {
104
- Array.isArray(e) || (e = [e]);
105
- const r = this;
106
- setTimeout(() => {
107
- const s = {};
108
- e.forEach((c) => {
109
- c.type === X ? r.resourceVersion = c?.object?.metadata?.resourceVersion ?? null : (s[c.type] || (s[c.type] = []), s[c.type].push(c.object));
110
- }), Object.keys(s).forEach((c) => r.informListeners(c, s[c]));
111
- });
112
- }, this.getResourceVersion = async () => {
113
- if (this.resourceVersion) return this.resourceVersion;
114
- l(this.PREFIX, "get resource version from API");
115
- const { metadata: e, items: r } = await E("GET", this.url, this.options).then(
116
- (s) => (
117
- // 'Body.json()' type definition expects 'any' as a return value
118
- s.json()
119
- )
120
- );
121
- return this.resourceVersion = e.resourceVersion, r && this.informListeners(R, r), this.resourceVersion;
122
- }, this.start = () => {
123
- const e = this;
124
- return e.getCurrentToken && e.refreshToken(e.getCurrentToken()), e.getResourceVersion().then((r) => (l(e.PREFIX, "current resource version", r), E("GET", e.url, {
125
- ...e.options,
126
- params: {
127
- ...e.options.params,
128
- watch: 1,
129
- allowWatchBookmarks: !0,
130
- resourceVersion: r
131
- },
132
- signal: e.signal
133
- }))).then(async (r) => {
134
- const s = r.body?.getReader();
135
- let c = !1, a = "";
136
- for (; !c; ) {
137
- l(e.PREFIX, "read stream chunk");
138
- const m = await s?.read();
139
- c = m?.done ?? !0, a += e.decoder.decode(m?.value || new Uint8Array());
140
- const f = a.split(/\n|\r|\r\n/);
141
- a = f.pop() ?? "";
142
- const T = [];
143
- f.forEach((D) => {
144
- const b = JSON.parse(D);
145
- if (b.type === w && b?.object?.code === 410) {
146
- e.cancel(), setTimeout(() => {
147
- l(e.PREFIX, "resource is gone 410", "recreate watch request!"), e.start();
148
- });
149
- return;
150
- }
151
- T.push(b);
152
- }), l(e.PREFIX, "inform listeners", f), e.handleEvents(T);
153
- }
154
- }).catch((r) => {
155
- if (r.name === "AbortError") return;
156
- const s = r.code ?? r?.response?.status;
157
- if (s === 410) {
158
- l(e.PREFIX, "resource is gone 410"), e.resourceVersion = null, setTimeout(() => e.start(), 0);
159
- return;
160
- }
161
- if (s === 404) {
162
- e.handleEvents({ type: w, object: r }), e.cancel();
163
- return;
164
- }
165
- P("WATCH ERROR", r, r.code, s, r.response), console.dir(r), setTimeout(() => {
166
- l(e.PREFIX, "recover connection", "restart!"), e.start();
167
- }, 3e4);
168
- }), e.cancel;
169
- }, this.on = (e, r) => (this.addListener(e, r), this), this.cancel = () => {
170
- l(this.PREFIX, "cancel connection"), this.controller.abort();
171
- }, this.url = n, this.options = o, this.getCurrentToken = h, o?.params?.resourceVersion && (this.resourceVersion = o.params.resourceVersion, delete o.params.resourceVersion);
172
- }
173
- }
174
- const y = async (t) => {
175
- const n = t.response?.data ? new Error(t.response.data.message ?? t.message) : t;
176
- return Promise.reject(n);
80
+ //#endregion
81
+ //#region src/watch.ts
82
+ var p = "ADDED", m = "MODIFIED", h = "DELETED", g = "ERROR", _ = "BOOKMARK", v = class {
83
+ constructor(e, t = {}, n) {
84
+ this.listeners = {
85
+ [p]: [],
86
+ [m]: [],
87
+ [h]: [],
88
+ [g]: []
89
+ }, this.controller = new AbortController(), this.signal = this.controller.signal, this.resourceVersion = null, this.PREFIX = "watch >", this.decoder = new TextDecoder(), this.refreshToken = (e) => {
90
+ this.options.headers && (this.options.headers.Authorization = `Bearer ${e}`);
91
+ }, this.checkType = (e) => {
92
+ if (![
93
+ "ADDED",
94
+ "MODIFIED",
95
+ "DELETED",
96
+ "ERROR"
97
+ ].includes(e)) throw Error(`Bad type: ${e}`);
98
+ }, this.addListener = (e, t) => {
99
+ this.checkType(e), this.listeners[e].push(t);
100
+ }, this.informListeners = (e, t) => {
101
+ let n = this.listeners[e];
102
+ n && n.forEach((e) => {
103
+ try {
104
+ e(t);
105
+ } catch (e) {
106
+ c(e);
107
+ }
108
+ });
109
+ }, this.handleEvents = (e) => {
110
+ Array.isArray(e) || (e = [e]);
111
+ let t = this;
112
+ setTimeout(() => {
113
+ let n = {};
114
+ e.forEach((e) => {
115
+ e.type === _ ? t.resourceVersion = e?.object?.metadata?.resourceVersion ?? null : (n[e.type] || (n[e.type] = []), n[e.type].push(e.object));
116
+ }), Object.keys(n).forEach((e) => t.informListeners(e, n[e]));
117
+ });
118
+ }, this.getResourceVersion = async () => {
119
+ if (this.resourceVersion) return this.resourceVersion;
120
+ l(this.PREFIX, "get resource version from API");
121
+ let { metadata: e, items: t } = await f("GET", this.url, this.options).then((e) => e.json());
122
+ return this.resourceVersion = e.resourceVersion, t && this.informListeners(p, t), this.resourceVersion;
123
+ }, this.start = () => {
124
+ let e = this;
125
+ return e.getCurrentToken && e.refreshToken(e.getCurrentToken()), e.getResourceVersion().then((t) => (l(e.PREFIX, "current resource version", t), f("GET", e.url, {
126
+ ...e.options,
127
+ params: {
128
+ ...e.options.params,
129
+ watch: 1,
130
+ allowWatchBookmarks: !0,
131
+ resourceVersion: t
132
+ },
133
+ signal: e.signal
134
+ }))).then(async (t) => {
135
+ let n = t.body?.getReader(), r = !1, i = "";
136
+ for (; !r;) {
137
+ l(e.PREFIX, "read stream chunk");
138
+ let t = await n?.read();
139
+ r = t?.done ?? !0, i += e.decoder.decode(t?.value || /* @__PURE__ */ new Uint8Array());
140
+ let a = i.split(/\n|\r|\r\n/);
141
+ i = a.pop() ?? "";
142
+ let o = [];
143
+ a.forEach((t) => {
144
+ let n = JSON.parse(t);
145
+ if (n.type === "ERROR" && n?.object?.code === 410) {
146
+ e.cancel(), setTimeout(() => {
147
+ l(e.PREFIX, "resource is gone 410", "recreate watch request!"), e.start();
148
+ });
149
+ return;
150
+ }
151
+ o.push(n);
152
+ }), l(e.PREFIX, "inform listeners", a), e.handleEvents(o);
153
+ }
154
+ }).catch((t) => {
155
+ if (t.name === "AbortError") return;
156
+ let n = t.code ?? t?.response?.status;
157
+ if (n === 410) {
158
+ l(e.PREFIX, "resource is gone 410"), e.resourceVersion = null, setTimeout(() => e.start(), 0);
159
+ return;
160
+ }
161
+ if (n === 404) {
162
+ e.handleEvents({
163
+ type: g,
164
+ object: t
165
+ }), e.cancel();
166
+ return;
167
+ }
168
+ c("WATCH ERROR", t, t.code, n, t.response), console.dir(t), setTimeout(() => {
169
+ l(e.PREFIX, "recover connection", "restart!"), e.start();
170
+ }, 3e4);
171
+ }), e.cancel;
172
+ }, this.on = (e, t) => (this.addListener(e, t), this), this.cancel = () => {
173
+ l(this.PREFIX, "cancel connection"), this.controller.abort();
174
+ }, this.url = e, this.options = t, this.getCurrentToken = n, t?.params?.resourceVersion && (this.resourceVersion = t.params.resourceVersion, delete t.params.resourceVersion);
175
+ }
176
+ }, y = async (e) => {
177
+ let t = e.response?.data ? Error(e.response.data.message ?? e.message) : e;
178
+ return Promise.reject(t);
177
179
  };
178
- function x(t) {
179
- const { apiEndpoint: n, ignoreSsl: o = !1, debug: h = !1 } = t;
180
- let e = t.token;
181
- if (!n || !e)
182
- throw new Error(`Bad options: ${JSON.stringify(t, null, 4)}. Please provide apiEndpoint and token`);
183
- o && h === !0 && (console.warn(`⚠️ K8s Client: SSL certificate verification disabled for ${n}`), console.warn("🔒 This should only be used in development or secure internal networks"));
184
- const r = () => ({
185
- Authorization: `Bearer ${e}`,
186
- "Content-Type": "application/json"
187
- });
188
- function s(i, u = {}) {
189
- const d = {
190
- ...r(),
191
- ...u.headers,
192
- ...i.headers
193
- };
194
- return {
195
- debug: h,
196
- ignoreSsl: o,
197
- ...u,
198
- ...i,
199
- headers: d
200
- };
201
- }
202
- function c(i, u = {}) {
203
- return E("HEAD", p(n, i), s(u)).catch(y);
204
- }
205
- function a(i, u = {}) {
206
- return E("GET", p(n, i), s(u)).then((d) => d.json()).catch(y);
207
- }
208
- function m(i, u, d = {}) {
209
- return E("POST", p(n, i), s(d, { body: u })).then((L) => L.json()).catch(y);
210
- }
211
- function f(i, u, d = {}) {
212
- return E("PUT", p(n, i), s(d, { body: u })).then((g) => g.json()).catch(y);
213
- }
214
- function T(i, u, d = {}) {
215
- return E(
216
- "PATCH",
217
- p(n, i),
218
- s(d, {
219
- headers: { "Content-Type": "application/merge-patch+json" },
220
- body: u
221
- })
222
- ).then((g) => g.json()).catch(y);
223
- }
224
- function D(i, u, d = {}) {
225
- return E("DELETE", p(n, i), s(d, { body: u })).then((g) => g.json()).catch(y);
226
- }
227
- function b(i, u = {}) {
228
- return new W(p(n, i), s(u), () => e);
229
- }
230
- function S(i) {
231
- e = i;
232
- }
233
- function F() {
234
- return e;
235
- }
236
- return {
237
- WATCH_ERROR: w,
238
- WATCH_ADDED: R,
239
- WATCH_DELETED: O,
240
- WATCH_MODIFIED: A,
241
- head: c,
242
- get: a,
243
- post: m,
244
- put: f,
245
- patch: T,
246
- delete: D,
247
- watch: b,
248
- refreshToken: S,
249
- currentToken: F
250
- };
180
+ //#endregion
181
+ //#region src/client.ts
182
+ function b(e) {
183
+ let { apiEndpoint: t, ignoreSsl: n = !1, debug: r = !1 } = e, i = e.token;
184
+ if (!t || !i) throw Error(`Bad options: ${JSON.stringify(e, null, 4)}. Please provide apiEndpoint and token`);
185
+ n && r === !0 && (console.warn(`⚠️ K8s Client: SSL certificate verification disabled for ${t}`), console.warn("🔒 This should only be used in development or secure internal networks"));
186
+ let o = () => ({
187
+ Authorization: `Bearer ${i}`,
188
+ "Content-Type": "application/json"
189
+ });
190
+ function s(e, t = {}) {
191
+ let i = {
192
+ ...o(),
193
+ ...t.headers,
194
+ ...e.headers
195
+ };
196
+ return {
197
+ debug: r,
198
+ ignoreSsl: n,
199
+ ...t,
200
+ ...e,
201
+ headers: i
202
+ };
203
+ }
204
+ function c(e, n = {}) {
205
+ return f("HEAD", a(t, e), s(n)).catch(y);
206
+ }
207
+ function l(e, n = {}) {
208
+ return f("GET", a(t, e), s(n)).then((e) => e.json()).catch(y);
209
+ }
210
+ function u(e, n, r = {}) {
211
+ return f("POST", a(t, e), s(r, { body: n })).then((e) => e.json()).catch(y);
212
+ }
213
+ function d(e, n, r = {}) {
214
+ return f("PUT", a(t, e), s(r, { body: n })).then((e) => e.json()).catch(y);
215
+ }
216
+ function _(e, n, r = {}) {
217
+ return f("PATCH", a(t, e), s(r, {
218
+ headers: { "Content-Type": "application/merge-patch+json" },
219
+ body: n
220
+ })).then((e) => e.json()).catch(y);
221
+ }
222
+ function b(e, n, r = {}) {
223
+ return f("DELETE", a(t, e), s(r, { body: n })).then((e) => e.json()).catch(y);
224
+ }
225
+ function x(e, n = {}) {
226
+ return new v(a(t, e), s(n), () => i);
227
+ }
228
+ function S(e) {
229
+ i = e;
230
+ }
231
+ function C() {
232
+ return i;
233
+ }
234
+ return {
235
+ WATCH_ERROR: g,
236
+ WATCH_ADDED: p,
237
+ WATCH_DELETED: h,
238
+ WATCH_MODIFIED: m,
239
+ head: c,
240
+ get: l,
241
+ post: u,
242
+ put: d,
243
+ patch: _,
244
+ delete: b,
245
+ watch: x,
246
+ refreshToken: S,
247
+ currentToken: C
248
+ };
251
249
  }
252
- export {
253
- R as ADDED,
254
- O as DELETED,
255
- w as ERROR,
256
- A as MODIFIED,
257
- W as Watch,
258
- x as createClient
259
- };
250
+ //#endregion
251
+ export { p as ADDED, h as DELETED, g as ERROR, m as MODIFIED, v as Watch, b as createClient };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudoperators/juno-k8s-client",
3
- "version": "1.1.3",
3
+ "version": "1.1.6",
4
4
  "author": "UI-Team",
5
5
  "description": "JavaScript client for Kubernetes API",
6
6
  "main": "build/index.cjs.js",
@@ -17,6 +17,11 @@
17
17
  "build/**",
18
18
  "LICENCE"
19
19
  ],
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/cloudoperators/juno/tree/main/packages/k8s-client"
23
+ },
24
+ "license": "Apache-2.0",
20
25
  "keywords": [
21
26
  "kubernetes",
22
27
  "js",
@@ -24,10 +29,10 @@
24
29
  "client"
25
30
  ],
26
31
  "devDependencies": {
27
- "vite": "7.3.2",
28
- "vitest": "3.2.4",
29
- "vite-plugin-dts": "4.5.4",
30
- "@cloudoperators/juno-config": "0.0.6"
32
+ "@cloudoperators/juno-config": "0.1.1",
33
+ "vite": "8.3.0",
34
+ "vite-plugin-dts": "5.1.1",
35
+ "vitest": "5.0.0"
31
36
  },
32
37
  "scripts": {
33
38
  "test": "vitest run",