@kapeta/local-cluster-service 0.16.3 → 0.16.4

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.16.4](https://github.com/kapetacom/local-cluster-service/compare/v0.16.3...v0.16.4) (2023-08-14)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Avoid multiple layers of cache ([#61](https://github.com/kapetacom/local-cluster-service/issues/61)) ([e8efcd2](https://github.com/kapetacom/local-cluster-service/commit/e8efcd26184c792a8798781eb6bd9952ccfa0fec))
7
+
1
8
  ## [0.16.3](https://github.com/kapetacom/local-cluster-service/compare/v0.16.2...v0.16.3) (2023-08-14)
2
9
 
3
10
 
@@ -1,9 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  declare class ProviderManager {
3
- private _webAssetCache;
4
- constructor();
5
3
  getWebProviders(): import("@kapeta/local-cluster-config").DefinitionInfo[];
6
- getAsset(handle: string, name: string, version: string, sourceMap?: boolean): Promise<string | Buffer | null>;
4
+ getProviderWebJS(handle: string, name: string, version: string, sourceMap?: boolean): Promise<string | Buffer | null>;
7
5
  }
8
6
  export declare const providerManager: ProviderManager;
9
7
  export {};
@@ -8,19 +8,18 @@ const path_1 = __importDefault(require("path"));
8
8
  const fs_extra_1 = __importDefault(require("fs-extra"));
9
9
  const repositoryManager_1 = require("./repositoryManager");
10
10
  const definitionsManager_1 = require("./definitionsManager");
11
+ const cacheManager_1 = require("./cacheManager");
11
12
  class ProviderManager {
12
- _webAssetCache;
13
- constructor() {
14
- this._webAssetCache = {};
15
- }
16
13
  getWebProviders() {
17
14
  return definitionsManager_1.definitionsManager.getProviderDefinitions().filter((providerDefinition) => providerDefinition.hasWeb);
18
15
  }
19
- async getAsset(handle, name, version, sourceMap = false) {
16
+ async getProviderWebJS(handle, name, version, sourceMap = false) {
20
17
  const fullName = `${handle}/${name}`;
21
18
  const id = `${handle}/${name}/${version}/web.js${sourceMap ? '.map' : ''}`;
22
- if (this._webAssetCache[id] && (await fs_extra_1.default.pathExists(this._webAssetCache[id]))) {
23
- return fs_extra_1.default.readFile(this._webAssetCache[id], 'utf8');
19
+ const cacheKey = `provider:web:${id}`;
20
+ const file = cacheManager_1.cacheManager.get(cacheKey);
21
+ if (file && await fs_extra_1.default.pathExists(file)) {
22
+ return fs_extra_1.default.readFile(file, 'utf8');
24
23
  }
25
24
  await repositoryManager_1.repositoryManager.ensureAsset(handle, name, version, true);
26
25
  const installedProvider = this.getWebProviders().find((providerDefinition) => {
@@ -30,7 +29,7 @@ class ProviderManager {
30
29
  //Check locally installed providers
31
30
  const path = path_1.default.join(installedProvider.path, 'web', handle, `${name}.js${sourceMap ? '.map' : ''}`);
32
31
  if (await fs_extra_1.default.pathExists(path)) {
33
- this._webAssetCache[id] = path;
32
+ cacheManager_1.cacheManager.set(cacheKey, path, 24 * 60 * 60 * 1000);
34
33
  return fs_extra_1.default.readFile(path);
35
34
  }
36
35
  }
@@ -14,7 +14,7 @@ router.get('/', async (req, res) => {
14
14
  });
15
15
  router.get('/asset/:handle/:name/:version/web.js', async (req, res) => {
16
16
  const { handle, name, version } = req.params;
17
- let result = await providerManager_1.providerManager.getAsset(handle, name, version);
17
+ let result = await providerManager_1.providerManager.getProviderWebJS(handle, name, version);
18
18
  if (!result) {
19
19
  res.status(404).send('');
20
20
  }
@@ -27,7 +27,7 @@ router.get('/asset/:handle/:name/:version/web.js', async (req, res) => {
27
27
  });
28
28
  router.get('/asset/:handle/:name/:version/web.js.map', async (req, res) => {
29
29
  const { handle, name, version } = req.params;
30
- const result = await providerManager_1.providerManager.getAsset(handle, name, version, true);
30
+ const result = await providerManager_1.providerManager.getProviderWebJS(handle, name, version, true);
31
31
  if (!result) {
32
32
  res.status(404).send('');
33
33
  }
@@ -2,7 +2,6 @@ import { Task } from './taskManager';
2
2
  import { SourceOfChange } from './types';
3
3
  declare class RepositoryManager {
4
4
  private _registryService;
5
- private _cache;
6
5
  private watcher;
7
6
  constructor();
8
7
  listenForChanges(): void;
@@ -29,14 +29,11 @@ const DEFAULT_PROVIDERS = [
29
29
  'kapeta/language-target-nodejs',
30
30
  'kapeta/language-target-java-spring-boot',
31
31
  ];
32
- const INSTALL_ATTEMPTED = {};
33
32
  class RepositoryManager {
34
33
  _registryService;
35
- _cache;
36
34
  watcher;
37
35
  constructor() {
38
36
  this._registryService = new nodejs_registry_utils_1.RegistryService(nodejs_registry_utils_1.Config.data.registry.url);
39
- this._cache = {};
40
37
  this.watcher = new RepositoryWatcher_1.RepositoryWatcher();
41
38
  this.listenForChanges();
42
39
  }
@@ -67,14 +64,10 @@ class RepositoryManager {
67
64
  //We make sure to only install one asset at a time - otherwise unexpected things might happen
68
65
  const createInstaller = (ref) => {
69
66
  return async () => {
70
- if (INSTALL_ATTEMPTED[ref]) {
71
- return;
72
- }
73
67
  if (definitionsManager_1.definitionsManager.exists(ref)) {
74
68
  return;
75
69
  }
76
70
  //console.log(`Installing asset: ${ref}`);
77
- INSTALL_ATTEMPTED[ref] = true;
78
71
  //Auto-install missing asset
79
72
  try {
80
73
  //We change to a temp dir to avoid issues with the current working directory
@@ -96,9 +89,6 @@ class RepositoryManager {
96
89
  continue;
97
90
  }
98
91
  ref = (0, utils_1.normalizeKapetaUri)(ref);
99
- if (INSTALL_ATTEMPTED[ref]) {
100
- continue;
101
- }
102
92
  if (definitionsManager_1.definitionsManager.exists(ref)) {
103
93
  continue;
104
94
  }
@@ -119,17 +109,10 @@ class RepositoryManager {
119
109
  }
120
110
  const definitions = definitionsManager_1.definitionsManager.getDefinitions();
121
111
  const installedAsset = definitions.find((d) => d.definition.metadata.name === fullName && d.version === version);
122
- if (installedAsset && this._cache[ref] === true) {
123
- return;
124
- }
125
- if (!installedAsset && this._cache[ref] === false) {
126
- return;
127
- }
128
112
  let assetVersion;
129
113
  try {
130
114
  assetVersion = await this._registryService.getVersion(fullName, version);
131
115
  if (!assetVersion) {
132
- this._cache[ref] = false;
133
116
  return;
134
117
  }
135
118
  }
@@ -140,7 +123,6 @@ class RepositoryManager {
140
123
  }
141
124
  throw e;
142
125
  }
143
- this._cache[ref] = true;
144
126
  let tasks = undefined;
145
127
  if (!installedAsset) {
146
128
  tasks = this._install([ref]);
@@ -1,9 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  declare class ProviderManager {
3
- private _webAssetCache;
4
- constructor();
5
3
  getWebProviders(): import("@kapeta/local-cluster-config").DefinitionInfo[];
6
- getAsset(handle: string, name: string, version: string, sourceMap?: boolean): Promise<string | Buffer | null>;
4
+ getProviderWebJS(handle: string, name: string, version: string, sourceMap?: boolean): Promise<string | Buffer | null>;
7
5
  }
8
6
  export declare const providerManager: ProviderManager;
9
7
  export {};
@@ -2,19 +2,18 @@ import Path from 'path';
2
2
  import FSExtra from 'fs-extra';
3
3
  import { repositoryManager } from './repositoryManager';
4
4
  import { definitionsManager } from './definitionsManager';
5
+ import { cacheManager } from "./cacheManager";
5
6
  class ProviderManager {
6
- _webAssetCache;
7
- constructor() {
8
- this._webAssetCache = {};
9
- }
10
7
  getWebProviders() {
11
8
  return definitionsManager.getProviderDefinitions().filter((providerDefinition) => providerDefinition.hasWeb);
12
9
  }
13
- async getAsset(handle, name, version, sourceMap = false) {
10
+ async getProviderWebJS(handle, name, version, sourceMap = false) {
14
11
  const fullName = `${handle}/${name}`;
15
12
  const id = `${handle}/${name}/${version}/web.js${sourceMap ? '.map' : ''}`;
16
- if (this._webAssetCache[id] && (await FSExtra.pathExists(this._webAssetCache[id]))) {
17
- return FSExtra.readFile(this._webAssetCache[id], 'utf8');
13
+ const cacheKey = `provider:web:${id}`;
14
+ const file = cacheManager.get(cacheKey);
15
+ if (file && await FSExtra.pathExists(file)) {
16
+ return FSExtra.readFile(file, 'utf8');
18
17
  }
19
18
  await repositoryManager.ensureAsset(handle, name, version, true);
20
19
  const installedProvider = this.getWebProviders().find((providerDefinition) => {
@@ -24,7 +23,7 @@ class ProviderManager {
24
23
  //Check locally installed providers
25
24
  const path = Path.join(installedProvider.path, 'web', handle, `${name}.js${sourceMap ? '.map' : ''}`);
26
25
  if (await FSExtra.pathExists(path)) {
27
- this._webAssetCache[id] = path;
26
+ cacheManager.set(cacheKey, path, 24 * 60 * 60 * 1000);
28
27
  return FSExtra.readFile(path);
29
28
  }
30
29
  }
@@ -9,7 +9,7 @@ router.get('/', async (req, res) => {
9
9
  });
10
10
  router.get('/asset/:handle/:name/:version/web.js', async (req, res) => {
11
11
  const { handle, name, version } = req.params;
12
- let result = await providerManager.getAsset(handle, name, version);
12
+ let result = await providerManager.getProviderWebJS(handle, name, version);
13
13
  if (!result) {
14
14
  res.status(404).send('');
15
15
  }
@@ -22,7 +22,7 @@ router.get('/asset/:handle/:name/:version/web.js', async (req, res) => {
22
22
  });
23
23
  router.get('/asset/:handle/:name/:version/web.js.map', async (req, res) => {
24
24
  const { handle, name, version } = req.params;
25
- const result = await providerManager.getAsset(handle, name, version, true);
25
+ const result = await providerManager.getProviderWebJS(handle, name, version, true);
26
26
  if (!result) {
27
27
  res.status(404).send('');
28
28
  }
@@ -2,7 +2,6 @@ import { Task } from './taskManager';
2
2
  import { SourceOfChange } from './types';
3
3
  declare class RepositoryManager {
4
4
  private _registryService;
5
- private _cache;
6
5
  private watcher;
7
6
  constructor();
8
7
  listenForChanges(): void;
@@ -23,14 +23,11 @@ const DEFAULT_PROVIDERS = [
23
23
  'kapeta/language-target-nodejs',
24
24
  'kapeta/language-target-java-spring-boot',
25
25
  ];
26
- const INSTALL_ATTEMPTED = {};
27
26
  class RepositoryManager {
28
27
  _registryService;
29
- _cache;
30
28
  watcher;
31
29
  constructor() {
32
30
  this._registryService = new RegistryService(Config.data.registry.url);
33
- this._cache = {};
34
31
  this.watcher = new RepositoryWatcher();
35
32
  this.listenForChanges();
36
33
  }
@@ -61,14 +58,10 @@ class RepositoryManager {
61
58
  //We make sure to only install one asset at a time - otherwise unexpected things might happen
62
59
  const createInstaller = (ref) => {
63
60
  return async () => {
64
- if (INSTALL_ATTEMPTED[ref]) {
65
- return;
66
- }
67
61
  if (definitionsManager.exists(ref)) {
68
62
  return;
69
63
  }
70
64
  //console.log(`Installing asset: ${ref}`);
71
- INSTALL_ATTEMPTED[ref] = true;
72
65
  //Auto-install missing asset
73
66
  try {
74
67
  //We change to a temp dir to avoid issues with the current working directory
@@ -90,9 +83,6 @@ class RepositoryManager {
90
83
  continue;
91
84
  }
92
85
  ref = normalizeKapetaUri(ref);
93
- if (INSTALL_ATTEMPTED[ref]) {
94
- continue;
95
- }
96
86
  if (definitionsManager.exists(ref)) {
97
87
  continue;
98
88
  }
@@ -113,17 +103,10 @@ class RepositoryManager {
113
103
  }
114
104
  const definitions = definitionsManager.getDefinitions();
115
105
  const installedAsset = definitions.find((d) => d.definition.metadata.name === fullName && d.version === version);
116
- if (installedAsset && this._cache[ref] === true) {
117
- return;
118
- }
119
- if (!installedAsset && this._cache[ref] === false) {
120
- return;
121
- }
122
106
  let assetVersion;
123
107
  try {
124
108
  assetVersion = await this._registryService.getVersion(fullName, version);
125
109
  if (!assetVersion) {
126
- this._cache[ref] = false;
127
110
  return;
128
111
  }
129
112
  }
@@ -134,7 +117,6 @@ class RepositoryManager {
134
117
  }
135
118
  throw e;
136
119
  }
137
- this._cache[ref] = true;
138
120
  let tasks = undefined;
139
121
  if (!installedAsset) {
140
122
  tasks = this._install([ref]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kapeta/local-cluster-service",
3
- "version": "0.16.3",
3
+ "version": "0.16.4",
4
4
  "description": "Manages configuration, ports and service discovery for locally running Kapeta systems",
5
5
  "type": "commonjs",
6
6
  "exports": {
@@ -4,23 +4,24 @@ import { repositoryManager } from './repositoryManager';
4
4
  import ClusterConfiguration from '@kapeta/local-cluster-config';
5
5
  import { StringMap } from './types';
6
6
  import { definitionsManager } from './definitionsManager';
7
+ import {cacheManager} from "./cacheManager";
7
8
 
8
9
  class ProviderManager {
9
- private _webAssetCache: StringMap;
10
- constructor() {
11
- this._webAssetCache = {};
12
- }
10
+
13
11
 
14
12
  getWebProviders() {
15
13
  return definitionsManager.getProviderDefinitions().filter((providerDefinition) => providerDefinition.hasWeb);
16
14
  }
17
15
 
18
- async getAsset(handle: string, name: string, version: string, sourceMap: boolean = false) {
16
+ async getProviderWebJS(handle: string, name: string, version: string, sourceMap: boolean = false) {
19
17
  const fullName = `${handle}/${name}`;
20
18
  const id = `${handle}/${name}/${version}/web.js${sourceMap ? '.map' : ''}`;
21
19
 
22
- if (this._webAssetCache[id] && (await FSExtra.pathExists(this._webAssetCache[id]))) {
23
- return FSExtra.readFile(this._webAssetCache[id], 'utf8');
20
+ const cacheKey = `provider:web:${id}`;
21
+
22
+ const file = cacheManager.get<string>(cacheKey);
23
+ if (file && await FSExtra.pathExists(file)) {
24
+ return FSExtra.readFile(file, 'utf8');
24
25
  }
25
26
 
26
27
  await repositoryManager.ensureAsset(handle, name, version, true);
@@ -33,8 +34,7 @@ class ProviderManager {
33
34
  //Check locally installed providers
34
35
  const path = Path.join(installedProvider.path, 'web', handle, `${name}.js${sourceMap ? '.map' : ''}`);
35
36
  if (await FSExtra.pathExists(path)) {
36
- this._webAssetCache[id] = path;
37
-
37
+ cacheManager.set(cacheKey, path, 24* 60 * 60 * 1000);
38
38
  return FSExtra.readFile(path);
39
39
  }
40
40
  }
@@ -16,7 +16,7 @@ router.get('/', async (req: Request, res: Response) => {
16
16
 
17
17
  router.get('/asset/:handle/:name/:version/web.js', async (req: Request, res: Response) => {
18
18
  const { handle, name, version } = req.params;
19
- let result = await providerManager.getAsset(handle, name, version);
19
+ let result = await providerManager.getProviderWebJS(handle, name, version);
20
20
 
21
21
  if (!result) {
22
22
  res.status(404).send('');
@@ -30,7 +30,7 @@ router.get('/asset/:handle/:name/:version/web.js', async (req: Request, res: Res
30
30
 
31
31
  router.get('/asset/:handle/:name/:version/web.js.map', async (req: Request, res: Response) => {
32
32
  const { handle, name, version } = req.params;
33
- const result = await providerManager.getAsset(handle, name, version, true);
33
+ const result = await providerManager.getProviderWebJS(handle, name, version, true);
34
34
 
35
35
  if (!result) {
36
36
  res.status(404).send('');
@@ -28,16 +28,12 @@ const DEFAULT_PROVIDERS = [
28
28
  'kapeta/language-target-java-spring-boot',
29
29
  ];
30
30
 
31
- const INSTALL_ATTEMPTED: { [p: string]: boolean } = {};
32
-
33
31
  class RepositoryManager {
34
32
  private _registryService: RegistryService;
35
- private _cache: { [key: string]: boolean };
36
33
  private watcher: RepositoryWatcher;
37
34
 
38
35
  constructor() {
39
36
  this._registryService = new RegistryService(Config.data.registry.url);
40
- this._cache = {};
41
37
  this.watcher = new RepositoryWatcher();
42
38
  this.listenForChanges();
43
39
  }
@@ -74,15 +70,10 @@ class RepositoryManager {
74
70
  //We make sure to only install one asset at a time - otherwise unexpected things might happen
75
71
  const createInstaller = (ref: string) => {
76
72
  return async () => {
77
- if (INSTALL_ATTEMPTED[ref]) {
78
- return;
79
- }
80
-
81
73
  if (definitionsManager.exists(ref)) {
82
74
  return;
83
75
  }
84
76
  //console.log(`Installing asset: ${ref}`);
85
- INSTALL_ATTEMPTED[ref] = true;
86
77
  //Auto-install missing asset
87
78
  try {
88
79
  //We change to a temp dir to avoid issues with the current working directory
@@ -106,10 +97,6 @@ class RepositoryManager {
106
97
  }
107
98
  ref = normalizeKapetaUri(ref);
108
99
 
109
- if (INSTALL_ATTEMPTED[ref]) {
110
- continue;
111
- }
112
-
113
100
  if (definitionsManager.exists(ref)) {
114
101
  continue;
115
102
  }
@@ -144,19 +131,10 @@ class RepositoryManager {
144
131
  (d) => d.definition.metadata.name === fullName && d.version === version
145
132
  );
146
133
 
147
- if (installedAsset && this._cache[ref] === true) {
148
- return;
149
- }
150
-
151
- if (!installedAsset && this._cache[ref] === false) {
152
- return;
153
- }
154
-
155
134
  let assetVersion;
156
135
  try {
157
136
  assetVersion = await this._registryService.getVersion(fullName, version);
158
137
  if (!assetVersion) {
159
- this._cache[ref] = false;
160
138
  return;
161
139
  }
162
140
  } catch (e) {
@@ -167,7 +145,6 @@ class RepositoryManager {
167
145
  throw e;
168
146
  }
169
147
 
170
- this._cache[ref] = true;
171
148
  let tasks: Task[] | undefined = undefined;
172
149
  if (!installedAsset) {
173
150
  tasks = this._install([ref]);