Skip to content

Commit f09b67d

Browse files
Merge branch 'master' into ts-routes
2 parents d37591f + 0d66dae commit f09b67d

23 files changed

+505
-445
lines changed

src/registry/domain/components-cache/components-list.js

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import async from 'async';
2+
import semver from 'semver';
3+
import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
4+
import { Cdn, ComponentsList, Config } from '../../../types';
5+
6+
export default function componentsList(conf: Config, cdn: Cdn) {
7+
const filePath = (): string =>
8+
`${conf.storage.options.componentsDir}/components.json`;
9+
10+
const componentsList = {
11+
getFromJson: (callback: Callback<any, string>) =>
12+
cdn.getJson(filePath(), true, callback),
13+
14+
getFromDirectories: (callback: Callback<ComponentsList, string>) => {
15+
const componentsInfo: Dictionary<string[]> = {};
16+
17+
const getVersionsForComponent = (
18+
componentName: string,
19+
cb: Callback<string[]>
20+
) => {
21+
cdn.listSubDirectories(
22+
`${conf.storage.options.componentsDir}/${componentName}`,
23+
(err, versions) => {
24+
if (err) {
25+
return cb(err, undefined as any);
26+
}
27+
cb(null, versions.sort(semver.compare));
28+
}
29+
);
30+
};
31+
32+
cdn.listSubDirectories(
33+
conf.storage.options.componentsDir,
34+
(err, components) => {
35+
if (err) {
36+
if (err.code === 'dir_not_found') {
37+
return callback(null, {
38+
lastEdit: getUnixUTCTimestamp(),
39+
components: [] as any
40+
});
41+
}
42+
43+
return callback(err as any, undefined as any);
44+
}
45+
46+
async.mapLimit(
47+
components,
48+
cdn.maxConcurrentRequests,
49+
getVersionsForComponent,
50+
(errors, versions) => {
51+
if (errors) {
52+
return callback(errors as any, undefined as any);
53+
}
54+
55+
components.forEach((component, i) => {
56+
componentsInfo[component] = (versions as any)[i];
57+
});
58+
59+
callback(null, {
60+
lastEdit: getUnixUTCTimestamp(),
61+
components: componentsInfo
62+
});
63+
}
64+
);
65+
}
66+
);
67+
},
68+
69+
refresh(callback: Callback<ComponentsList, string>) {
70+
componentsList.getFromDirectories((err, components) => {
71+
if (err) {
72+
return callback(err, undefined as any);
73+
}
74+
componentsList.save(components, err => {
75+
if (err) {
76+
return callback(err, undefined as any);
77+
}
78+
callback(err, components);
79+
});
80+
});
81+
},
82+
83+
save: (data: ComponentsList, callback: Callback<unknown, string>) =>
84+
cdn.putFileContent(JSON.stringify(data), filePath(), true, callback)
85+
};
86+
87+
return componentsList;
88+
}

src/registry/domain/components-cache/index.js renamed to src/registry/domain/components-cache/index.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
'use strict';
1+
import _ from 'lodash';
2+
import getComponentsList from './components-list';
3+
import * as eventsHandler from '../events-handler';
4+
import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
5+
import { Cdn, ComponentsList, Config } from '../../../types';
26

3-
const _ = require('lodash');
7+
export default function componentsCache(conf: Config, cdn: Cdn) {
8+
let cachedComponentsList: ComponentsList;
9+
let refreshLoop: NodeJS.Timeout;
410

5-
const ComponentsList = require('./components-list');
6-
const eventsHandler = require('../events-handler');
7-
const getUnixUTCTimestamp = require('oc-get-unix-utc-timestamp');
8-
9-
module.exports = (conf, cdn) => {
10-
let cachedComponentsList, refreshLoop;
11-
12-
const componentsList = ComponentsList(conf, cdn);
11+
const componentsList = getComponentsList(conf, cdn);
1312

1413
const poll = () =>
1514
setTimeout(() => {
@@ -30,19 +29,26 @@ module.exports = (conf, cdn) => {
3029
});
3130
}, conf.pollingInterval * 1000);
3231

33-
const cacheDataAndStartPolling = (data, callback) => {
32+
const cacheDataAndStartPolling = (
33+
data: ComponentsList,
34+
callback: (err: null, data: ComponentsList) => void
35+
) => {
3436
cachedComponentsList = data;
3537
refreshLoop = poll();
3638
callback(null, data);
3739
};
3840

39-
const returnError = (code, message, callback) => {
41+
const returnError = (
42+
code: string,
43+
message: string,
44+
callback: Callback<any, any>
45+
) => {
4046
eventsHandler.fire('error', { code, message });
41-
return callback(code);
47+
return callback(code, undefined as any);
4248
};
4349

4450
return {
45-
get: callback => {
51+
get(callback: Callback<ComponentsList>) {
4652
if (!cachedComponentsList) {
4753
return returnError(
4854
'components_cache_empty',
@@ -54,7 +60,7 @@ module.exports = (conf, cdn) => {
5460
callback(null, cachedComponentsList);
5561
},
5662

57-
load: callback => {
63+
load(callback: Callback<ComponentsList>) {
5864
componentsList.getFromJson((jsonErr, jsonComponents) => {
5965
componentsList.getFromDirectories((dirErr, dirComponents) => {
6066
if (dirErr) {
@@ -75,7 +81,7 @@ module.exports = (conf, cdn) => {
7581
});
7682
});
7783
},
78-
refresh: callback => {
84+
refresh(callback: Callback<ComponentsList>) {
7985
clearTimeout(refreshLoop);
8086
componentsList.refresh((err, components) => {
8187
if (err) {
@@ -86,4 +92,4 @@ module.exports = (conf, cdn) => {
8692
});
8793
}
8894
};
89-
};
95+
}

src/registry/domain/components-details.js

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)