From b45313961ecd045a46bb91c326f7946e6a24a659 Mon Sep 17 00:00:00 2001 From: Nathan Holzworth Date: Thu, 15 Sep 2022 13:27:59 -0400 Subject: [PATCH 1/9] Moved WatchManager.js from WatchKeeper --- lib/WatchManager.js | 94 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 lib/WatchManager.js diff --git a/lib/WatchManager.js b/lib/WatchManager.js new file mode 100644 index 0000000..c567db5 --- /dev/null +++ b/lib/WatchManager.js @@ -0,0 +1,94 @@ +/** + * Copyright 2019 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const Watchman = require('./Watchman'); +const log = require('./bunyan-api').createLogger('WatchManager'); +const objectPath = require('object-path'); +const hash = require('object-hash'); + +var _watchObjects = {}; + +module.exports = function WatchManager() { + // private + let _saveWatch = function (wm, querySelector, startWatch = true) { + let selfLink = wm.selfLink; + _removeWatch(selfLink); + _watchObjects[selfLink] = { selfLink: wm.selfLink, watchman: wm, querySelectorHash: hash(querySelector) }; + if (startWatch) { + wm.watch(); + } + log.info(`Watch added: ${selfLink} ${JSON.stringify(querySelector)}`); + return _watchObjects[selfLink]; + }; + + let _ensureWatch = function (options, objectHandler, globalWatch = false, startWatch = true) { + let querySelector = objectPath.get(options, 'requestOptions.qs', {}); + let selfLink = options?.requestOptions?.uri; + let w = _getWatch(selfLink); + if (w && (!globalWatch || (globalWatch && w.querySelectorHash == hash(querySelector)))) { + return w; + } + var wm = new Watchman(options, objectHandler); + return _saveWatch(wm, querySelector, startWatch); + }; + + let _startWatch = function (selfLink) { + return _reWatch(selfLink); + }; + + let _removeWatch = function (selfLink) { + let w = objectPath.get(_getWatch(selfLink), 'watchman'); + if (w) { + w.end(); + delete _watchObjects[selfLink]; + log.info(`Watch removed: ${selfLink}`); + } + return _watchObjects[selfLink]; + }; + + let _getWatch = function (selfLink) { + return _watchObjects[selfLink]; + }; + + let _reWatch = function (selfLink) { + let w = objectPath.get(_getWatch(selfLink), 'watchman'); + if (w) { + w.watch(); + } + return w; + }; + + // public + return { + saveWatch: _saveWatch, + ensureWatch: _ensureWatch, + startWatch: _startWatch, + removeWatch: _removeWatch, + removeAllWatches: function () { + let watches = Object.keys(_watchObjects); + watches.forEach(w => _removeWatch(w)); + }, + getWatch: _getWatch, + getAllWatches: function () { + return _watchObjects; + }, + reWatch: _reWatch, + reWatchAll: function () { + let watches = Object.keys(_watchObjects); + watches.forEach(w => _reWatch(w)); + } + }; +}; From 036542993a1ce3bbb82fbb3f93c9b58948ec8713 Mon Sep 17 00:00:00 2001 From: Nathan Holzworth Date: Tue, 20 Sep 2022 13:24:28 -0400 Subject: [PATCH 2/9] add WatchManager to module exports --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1f5b342..a6e4eab 100644 --- a/index.js +++ b/index.js @@ -24,10 +24,13 @@ const Watchman = require('./lib/Watchman'); const EventHandler = require('./lib/EventHandler'); +const WatchManager = require('./lib/WatchManager'); + module.exports = { KubeClass, KubeApiConfig, KubeResourceMeta, Watchman, - EventHandler + EventHandler, + WatchManager }; From c445e251b7de7420668fc3c849e5d6de5337bdca Mon Sep 17 00:00:00 2001 From: Nathan Holzworth Date: Tue, 20 Sep 2022 17:02:18 -0400 Subject: [PATCH 3/9] npm audit fix --- package-lock.json | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1663372..ecee7b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3289,14 +3289,18 @@ } }, "node_modules/jose": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/jose/-/jose-2.0.5.tgz", - "integrity": "sha512-BAiDNeDKTMgk4tvD0BbxJ8xHEHBZgpeRZ1zGPPsitSyMgjoMWiLGYAE7H7NpP5h0lPppQajQs871E8NHUrzVPA==", + "version": "2.0.6", + "resolved": "https://na.artifactory.swg-devops.com/artifactory/api/npm/wcp-alchemy-containers-team-npm-virtual/jose/-/jose-2.0.6.tgz?dl=https%3A%2F%2Fregistry.npmjs.org%2Fjose%2F-%2Fjose-2.0.6.tgz", + "integrity": "sha512-FVoPY7SflDodE4lknJmbAHSUjLCzE2H1F6MS0RYKMQ8SR+lNccpMf8R4eqkNYyyUjR5qZReOzZo5C5YiHOCjjg==", + "license": "MIT", "dependencies": { "@panva/asn1.js": "^1.0.0" }, "engines": { "node": ">=10.13.0 < 13 || >=13.7.0" + }, + "funding": { + "url": "https://github.com/sponsors/panva" } }, "node_modules/js-tokens": { @@ -8794,9 +8798,9 @@ } }, "jose": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/jose/-/jose-2.0.5.tgz", - "integrity": "sha512-BAiDNeDKTMgk4tvD0BbxJ8xHEHBZgpeRZ1zGPPsitSyMgjoMWiLGYAE7H7NpP5h0lPppQajQs871E8NHUrzVPA==", + "version": "2.0.6", + "resolved": "https://na.artifactory.swg-devops.com/artifactory/api/npm/wcp-alchemy-containers-team-npm-virtual/jose/-/jose-2.0.6.tgz?dl=https%3A%2F%2Fregistry.npmjs.org%2Fjose%2F-%2Fjose-2.0.6.tgz", + "integrity": "sha512-FVoPY7SflDodE4lknJmbAHSUjLCzE2H1F6MS0RYKMQ8SR+lNccpMf8R4eqkNYyyUjR5qZReOzZo5C5YiHOCjjg==", "requires": { "@panva/asn1.js": "^1.0.0" } From 1c0874c64ccc46d6b9ad1e067fdfebbce584a428 Mon Sep 17 00:00:00 2001 From: Nathan Holzworth Date: Tue, 20 Sep 2022 17:30:40 -0400 Subject: [PATCH 4/9] Revert "npm audit fix" This reverts commit c445e251b7de7420668fc3c849e5d6de5337bdca. --- package-lock.json | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index ecee7b4..1663372 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3289,18 +3289,14 @@ } }, "node_modules/jose": { - "version": "2.0.6", - "resolved": "https://na.artifactory.swg-devops.com/artifactory/api/npm/wcp-alchemy-containers-team-npm-virtual/jose/-/jose-2.0.6.tgz?dl=https%3A%2F%2Fregistry.npmjs.org%2Fjose%2F-%2Fjose-2.0.6.tgz", - "integrity": "sha512-FVoPY7SflDodE4lknJmbAHSUjLCzE2H1F6MS0RYKMQ8SR+lNccpMf8R4eqkNYyyUjR5qZReOzZo5C5YiHOCjjg==", - "license": "MIT", + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/jose/-/jose-2.0.5.tgz", + "integrity": "sha512-BAiDNeDKTMgk4tvD0BbxJ8xHEHBZgpeRZ1zGPPsitSyMgjoMWiLGYAE7H7NpP5h0lPppQajQs871E8NHUrzVPA==", "dependencies": { "@panva/asn1.js": "^1.0.0" }, "engines": { "node": ">=10.13.0 < 13 || >=13.7.0" - }, - "funding": { - "url": "https://github.com/sponsors/panva" } }, "node_modules/js-tokens": { @@ -8798,9 +8794,9 @@ } }, "jose": { - "version": "2.0.6", - "resolved": "https://na.artifactory.swg-devops.com/artifactory/api/npm/wcp-alchemy-containers-team-npm-virtual/jose/-/jose-2.0.6.tgz?dl=https%3A%2F%2Fregistry.npmjs.org%2Fjose%2F-%2Fjose-2.0.6.tgz", - "integrity": "sha512-FVoPY7SflDodE4lknJmbAHSUjLCzE2H1F6MS0RYKMQ8SR+lNccpMf8R4eqkNYyyUjR5qZReOzZo5C5YiHOCjjg==", + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/jose/-/jose-2.0.5.tgz", + "integrity": "sha512-BAiDNeDKTMgk4tvD0BbxJ8xHEHBZgpeRZ1zGPPsitSyMgjoMWiLGYAE7H7NpP5h0lPppQajQs871E8NHUrzVPA==", "requires": { "@panva/asn1.js": "^1.0.0" } From 1177c21b4663b7440ff48588da8449cfa1c6803a Mon Sep 17 00:00:00 2001 From: Nathan Holzworth Date: Tue, 20 Sep 2022 17:31:13 -0400 Subject: [PATCH 5/9] npm audit fix take 2 --- package-lock.json | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1663372..f64614d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3289,14 +3289,17 @@ } }, "node_modules/jose": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/jose/-/jose-2.0.5.tgz", - "integrity": "sha512-BAiDNeDKTMgk4tvD0BbxJ8xHEHBZgpeRZ1zGPPsitSyMgjoMWiLGYAE7H7NpP5h0lPppQajQs871E8NHUrzVPA==", + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/jose/-/jose-2.0.6.tgz", + "integrity": "sha512-FVoPY7SflDodE4lknJmbAHSUjLCzE2H1F6MS0RYKMQ8SR+lNccpMf8R4eqkNYyyUjR5qZReOzZo5C5YiHOCjjg==", "dependencies": { "@panva/asn1.js": "^1.0.0" }, "engines": { "node": ">=10.13.0 < 13 || >=13.7.0" + }, + "funding": { + "url": "https://github.com/sponsors/panva" } }, "node_modules/js-tokens": { @@ -8794,9 +8797,9 @@ } }, "jose": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/jose/-/jose-2.0.5.tgz", - "integrity": "sha512-BAiDNeDKTMgk4tvD0BbxJ8xHEHBZgpeRZ1zGPPsitSyMgjoMWiLGYAE7H7NpP5h0lPppQajQs871E8NHUrzVPA==", + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/jose/-/jose-2.0.6.tgz", + "integrity": "sha512-FVoPY7SflDodE4lknJmbAHSUjLCzE2H1F6MS0RYKMQ8SR+lNccpMf8R4eqkNYyyUjR5qZReOzZo5C5YiHOCjjg==", "requires": { "@panva/asn1.js": "^1.0.0" } From 4bad4bfb42cfe5ee7d3766e75888eb2bb4d04364 Mon Sep 17 00:00:00 2001 From: nathanholzworth <94406077+nathanholzworth@users.noreply.github.com> Date: Wed, 28 Sep 2022 10:21:04 -0400 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Adam King --- lib/WatchManager.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/WatchManager.js b/lib/WatchManager.js index c567db5..7e14fca 100644 --- a/lib/WatchManager.js +++ b/lib/WatchManager.js @@ -1,5 +1,5 @@ /** - * Copyright 2019 IBM Corp. All Rights Reserved. + * Copyright 2019, 2022 IBM Corp. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,7 @@ const log = require('./bunyan-api').createLogger('WatchManager'); const objectPath = require('object-path'); const hash = require('object-hash'); -var _watchObjects = {}; +const _watchObjects = {}; module.exports = function WatchManager() { // private From 612c773c06e7aed6b9bf69e0b7575fab2aa44b7d Mon Sep 17 00:00:00 2001 From: Nathan Holzworth Date: Wed, 28 Sep 2022 12:27:11 -0400 Subject: [PATCH 7/9] update index.js copywrite note --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index a6e4eab..bb408f6 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ /** -* Copyright 2019 IBM Corp. All Rights Reserved. +* Copyright 2019, 2022 IBM Corp. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From be1f11ba39712036ef8cb2b9c9cf90e379e8ecf0 Mon Sep 17 00:00:00 2001 From: Nathan Holzworth Date: Wed, 12 Oct 2022 13:32:37 -0400 Subject: [PATCH 8/9] replace lets/vars with const --- lib/WatchManager.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/WatchManager.js b/lib/WatchManager.js index 7e14fca..dcf7bd8 100644 --- a/lib/WatchManager.js +++ b/lib/WatchManager.js @@ -23,8 +23,8 @@ const _watchObjects = {}; module.exports = function WatchManager() { // private - let _saveWatch = function (wm, querySelector, startWatch = true) { - let selfLink = wm.selfLink; + const _saveWatch = function (wm, querySelector, startWatch = true) { + const selfLink = wm.selfLink; _removeWatch(selfLink); _watchObjects[selfLink] = { selfLink: wm.selfLink, watchman: wm, querySelectorHash: hash(querySelector) }; if (startWatch) { @@ -35,22 +35,22 @@ module.exports = function WatchManager() { }; let _ensureWatch = function (options, objectHandler, globalWatch = false, startWatch = true) { - let querySelector = objectPath.get(options, 'requestOptions.qs', {}); - let selfLink = options?.requestOptions?.uri; - let w = _getWatch(selfLink); + const querySelector = objectPath.get(options, 'requestOptions.qs', {}); + const selfLink = options?.requestOptions?.uri; + const w = _getWatch(selfLink); if (w && (!globalWatch || (globalWatch && w.querySelectorHash == hash(querySelector)))) { return w; } - var wm = new Watchman(options, objectHandler); + const wm = new Watchman(options, objectHandler); return _saveWatch(wm, querySelector, startWatch); }; - let _startWatch = function (selfLink) { + const _startWatch = function (selfLink) { return _reWatch(selfLink); }; - let _removeWatch = function (selfLink) { - let w = objectPath.get(_getWatch(selfLink), 'watchman'); + const _removeWatch = function (selfLink) { + const w = objectPath.get(_getWatch(selfLink), 'watchman'); if (w) { w.end(); delete _watchObjects[selfLink]; @@ -59,12 +59,12 @@ module.exports = function WatchManager() { return _watchObjects[selfLink]; }; - let _getWatch = function (selfLink) { + const _getWatch = function (selfLink) { return _watchObjects[selfLink]; }; - let _reWatch = function (selfLink) { - let w = objectPath.get(_getWatch(selfLink), 'watchman'); + const _reWatch = function (selfLink) { + const w = objectPath.get(_getWatch(selfLink), 'watchman'); if (w) { w.watch(); } @@ -78,7 +78,7 @@ module.exports = function WatchManager() { startWatch: _startWatch, removeWatch: _removeWatch, removeAllWatches: function () { - let watches = Object.keys(_watchObjects); + const watches = Object.keys(_watchObjects); watches.forEach(w => _removeWatch(w)); }, getWatch: _getWatch, @@ -87,7 +87,7 @@ module.exports = function WatchManager() { }, reWatch: _reWatch, reWatchAll: function () { - let watches = Object.keys(_watchObjects); + const watches = Object.keys(_watchObjects); watches.forEach(w => _reWatch(w)); } }; From 86d2eb22c4db8c3d209e77b1441732c02cde24c4 Mon Sep 17 00:00:00 2001 From: Nathan Holzworth Date: Wed, 12 Oct 2022 13:33:43 -0400 Subject: [PATCH 9/9] changed missed let --- lib/WatchManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/WatchManager.js b/lib/WatchManager.js index dcf7bd8..5f0071e 100644 --- a/lib/WatchManager.js +++ b/lib/WatchManager.js @@ -34,7 +34,7 @@ module.exports = function WatchManager() { return _watchObjects[selfLink]; }; - let _ensureWatch = function (options, objectHandler, globalWatch = false, startWatch = true) { + const _ensureWatch = function (options, objectHandler, globalWatch = false, startWatch = true) { const querySelector = objectPath.get(options, 'requestOptions.qs', {}); const selfLink = options?.requestOptions?.uri; const w = _getWatch(selfLink);