Skip to content

Commit ed3e5fb

Browse files
upgrade storage adapters and parse legacy ones
1 parent 608c188 commit ed3e5fb

12 files changed

+266
-105
lines changed

package-lock.json

Lines changed: 23 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@
115115
"oc-client-browser": "1.5.4",
116116
"oc-empty-response-handler": "1.0.2",
117117
"oc-get-unix-utc-timestamp": "1.0.5",
118-
"oc-s3-storage-adapter": "1.1.6",
119-
"oc-storage-adapters-utils": "1.0.4",
118+
"oc-s3-storage-adapter": "1.2.0",
119+
"oc-storage-adapters-utils": "1.1.0",
120120
"oc-template-es6": "1.0.6",
121121
"oc-template-es6-compiler": "1.1.13",
122122
"oc-template-handlebars": "6.0.24",

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
import { promisify } from 'util';
21
import semver from 'semver';
32
import pLimit from 'p-limit';
43
import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
5-
import { Cdn, ComponentsList, Config } from '../../../types';
4+
import { ComponentsList, Config } from '../../../types';
5+
import { StorageAdapter } from 'oc-storage-adapters-utils';
66

7-
export default function componentsList(conf: Config, cdn: Cdn) {
7+
export default function componentsList(conf: Config, cdn: StorageAdapter) {
88
const filePath = (): string =>
99
`${conf.storage.options.componentsDir}/components.json`;
1010

1111
const componentsList = {
12-
getFromJson: (): Promise<ComponentsList> =>
13-
promisify(cdn.getJson)(filePath(), true),
12+
getFromJson: (): Promise<ComponentsList> => cdn.getJson(filePath(), true),
1413

1514
getFromDirectories: async (): Promise<ComponentsList> => {
1615
const componentsInfo: Dictionary<string[]> = {};
1716

1817
const getVersionsForComponent = async (
1918
componentName: string
2019
): Promise<string[]> => {
21-
const versions = await promisify(cdn.listSubDirectories)(
20+
const versions = await cdn.listSubDirectories(
2221
`${conf.storage.options.componentsDir}/${componentName}`
2322
);
2423

2524
return versions.sort(semver.compare);
2625
};
2726

2827
try {
29-
const components = await promisify(cdn.listSubDirectories)(
28+
const components = await cdn.listSubDirectories(
3029
conf.storage.options.componentsDir
3130
);
3231
const limit = pLimit(cdn.maxConcurrentRequests);
@@ -64,7 +63,7 @@ export default function componentsList(conf: Config, cdn: Cdn) {
6463
},
6564

6665
save: (data: ComponentsList): Promise<unknown> =>
67-
promisify(cdn.putFileContent)(JSON.stringify(data), filePath(), true)
66+
cdn.putFileContent(JSON.stringify(data), filePath(), true)
6867
};
6968

7069
return componentsList;

src/registry/domain/components-cache/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import _ from 'lodash';
22
import getComponentsList from './components-list';
33
import * as eventsHandler from '../events-handler';
44
import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
5-
import { Cdn, ComponentsList, Config } from '../../../types';
5+
import { ComponentsList, Config } from '../../../types';
6+
import { StorageAdapter } from 'oc-storage-adapters-utils';
67

7-
export default function componentsCache(conf: Config, cdn: Cdn) {
8+
export default function componentsCache(conf: Config, cdn: StorageAdapter) {
89
let cachedComponentsList: ComponentsList;
910
let refreshLoop: NodeJS.Timeout;
1011

@@ -36,8 +37,8 @@ export default function componentsCache(conf: Config, cdn: Cdn) {
3637
return data;
3738
};
3839

39-
const throwError = (code: string, message: unknown) => {
40-
eventsHandler.fire('error', { code, message });
40+
const throwError = (code: string, message: any) => {
41+
eventsHandler.fire('error', { code, message: message?.message ?? message });
4142
throw code;
4243
};
4344

src/registry/domain/components-details.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import _ from 'lodash';
33
import * as eventsHandler from './events-handler';
44
import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
55
import {
6-
Cdn,
76
Component,
87
ComponentsDetails,
98
ComponentsList,
109
Config
1110
} from '../../types';
11+
import { StorageAdapter } from 'oc-storage-adapters-utils';
1212

13-
export default function componentsDetails(conf: Config, cdn: Cdn) {
13+
export default function componentsDetails(conf: Config, cdn: StorageAdapter) {
1414
const returnError = (
1515
code: string,
1616
message: string | Error,

src/registry/domain/repository.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from 'fs-extra';
2-
import { promisify } from 'util';
32
import getUnixUtcTimestamp from 'oc-get-unix-utc-timestamp';
43
import path from 'path';
54
import _ from 'lodash';
@@ -10,24 +9,27 @@ import registerTemplates from './register-templates';
109
import settings from '../../resources/settings';
1110
import strings from '../../resources';
1211
import * as validator from './validators';
12+
import parseAdapter from './storage-adapter';
1313
import * as versionHandler from './version-handler';
1414
import errorToString from '../../utils/error-to-string';
1515
import {
16-
Cdn,
1716
Component,
1817
ComponentsDetails,
1918
ComponentsList,
2019
Config,
2120
Repository
2221
} from '../../types';
2322
import { fromCallback, fromPromise } from 'universalify';
23+
import { StorageAdapter } from 'oc-storage-adapters-utils';
2424

2525
const packageInfo = fs.readJsonSync(
2626
path.join(__dirname, '..', '..', '..', 'package.json')
2727
);
2828

2929
export default function repository(conf: Config): Repository {
30-
const cdn: Cdn = !conf.local && conf.storage.adapter(conf.storage.options);
30+
const cdn: StorageAdapter =
31+
!conf.local &&
32+
(parseAdapter(conf.storage.adapter(conf.storage.options)) as any);
3133
const options = !conf.local ? conf.storage.options : null;
3234
const repositorySource = conf.local
3335
? 'local repository'
@@ -120,7 +122,7 @@ export default function repository(conf: Config): Repository {
120122
return Promise.resolve(local.getCompiledView(componentName));
121123
}
122124

123-
return promisify(cdn.getFile)(
125+
return cdn.getFile(
124126
getFilePath(componentName, componentVersion, 'template.js')
125127
);
126128
},
@@ -212,7 +214,7 @@ export default function repository(conf: Config): Repository {
212214
}
213215
}
214216

215-
return promisify(cdn.getJson)<Component>(
217+
return cdn.getJson<Component>(
216218
getFilePath(componentName, componentVersion, 'package.json'),
217219
false
218220
);
@@ -260,7 +262,7 @@ export default function repository(conf: Config): Repository {
260262
'server.js'
261263
);
262264

263-
const content = await promisify(cdn.getFile)(filePath);
265+
const content = await cdn.getFile(filePath);
264266

265267
return { content, filePath };
266268
},
@@ -391,7 +393,7 @@ export default function repository(conf: Config): Repository {
391393
if (err) {
392394
return callback(err as any, undefined as any);
393395
}
394-
cdn.putDir(
396+
fromPromise(cdn.putDir)(
395397
pkgDetails.outputFolder,
396398
`${
397399
options!.componentsDir
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { fromCallback } from 'universalify';
2+
import { StorageAdapter } from 'oc-storage-adapters-utils';
3+
4+
type RemovePromiseOverload<T> = T extends {
5+
(...args: infer B): void;
6+
(...args: any[]): Promise<any>;
7+
}
8+
? (...args: B) => void
9+
: T;
10+
11+
type LegacyStorageAdapter = {
12+
[P in keyof StorageAdapter]: RemovePromiseOverload<StorageAdapter[P]>;
13+
};
14+
15+
const officialAdapters = {
16+
s3: { name: 'oc-s3-storage-adapter', version: '1.2.0' },
17+
gs: { name: 'oc-gs-storage-adapter', version: '1.1.0' },
18+
'azure-blob-storage': { name: 'oc-azure-storage-adapter', version: '0.1.0' }
19+
};
20+
type OfficialAdapter = keyof typeof officialAdapters;
21+
22+
function isOfficialAdapter(
23+
adapter: LegacyStorageAdapter
24+
): adapter is LegacyStorageAdapter & { adapterType: OfficialAdapter } {
25+
return Object.keys(officialAdapters).includes(
26+
adapter.adapterType as OfficialAdapter
27+
);
28+
}
29+
30+
function isPromiseBased(tryFunction: () => unknown) {
31+
try {
32+
(tryFunction as () => Promise<unknown>)().catch(() => {
33+
// To not throw unhandled promise exceptions
34+
});
35+
return true;
36+
} catch (err) {
37+
return false;
38+
}
39+
}
40+
41+
function isLegacyAdapter(
42+
adapter: StorageAdapter | LegacyStorageAdapter
43+
): adapter is LegacyStorageAdapter {
44+
return !isPromiseBased(() => (adapter as StorageAdapter).getFile(''));
45+
}
46+
47+
function convertLegacyAdapter(adapter: LegacyStorageAdapter): StorageAdapter {
48+
return {
49+
getFile: fromCallback(adapter.getFile),
50+
getJson: fromCallback(adapter.getJson),
51+
listSubDirectories: fromCallback(adapter.listSubDirectories),
52+
putDir: fromCallback(adapter.putDir),
53+
putFile: fromCallback(adapter.putFile),
54+
putFileContent: fromCallback(adapter.putFileContent),
55+
getUrl: adapter.getUrl,
56+
maxConcurrentRequests: adapter.maxConcurrentRequests,
57+
adapterType: adapter.adapterType
58+
};
59+
}
60+
61+
export default function getAdapter(
62+
adapter: StorageAdapter | LegacyStorageAdapter
63+
): StorageAdapter {
64+
if (isLegacyAdapter(adapter)) {
65+
if (isOfficialAdapter(adapter)) {
66+
const pkg = officialAdapters[adapter.adapterType];
67+
process.emitWarning(
68+
`Adapters now should work with promises. Consider upgrading your package ${pkg.name} to at least version ${pkg.version}`,
69+
'DeprecationWarning'
70+
);
71+
} else {
72+
process.emitWarning(
73+
'Your adapter is using the old interface of working with callbacks. Consider upgrading it to work with promises, as the previous one will be deprecated.',
74+
'DeprecationWarning'
75+
);
76+
}
77+
78+
return convertLegacyAdapter(adapter);
79+
}
80+
81+
return adapter;
82+
}

src/registry/routes/static-redirector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs-extra';
22
import path from 'path';
33

4-
import { getFileInfo } from 'oc-storage-adapters-utils';
4+
import adaptersUtils from 'oc-storage-adapters-utils';
55
import { Request, Response } from 'express';
66
import { Repository } from '../../types';
77

@@ -57,7 +57,7 @@ export default function staticRedirector(repository: Repository) {
5757
}
5858

5959
const fileStream = fs.createReadStream(filePath);
60-
const fileInfo = getFileInfo(filePath);
60+
const fileInfo = adaptersUtils.getFileInfo(filePath);
6161

6262
if (fileInfo.mimeType) {
6363
res.set('Content-Type', fileInfo.mimeType);

src/types.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,6 @@ export interface Config {
168168
verbosity: number;
169169
}
170170

171-
export interface Cdn {
172-
adapterType: string;
173-
getFile: (filePath: string, cb: Callback<string>) => void;
174-
getJson<T>(filePath: string, force: boolean, cb: Callback<T, string>): void;
175-
listSubDirectories: (
176-
dir: string,
177-
cb: Callback<string[], Error & { code?: string }>
178-
) => void;
179-
maxConcurrentRequests: number;
180-
putDir: (folderPath: string, filePath: string, cb: Callback) => void;
181-
putFileContent: (
182-
data: unknown,
183-
path: string,
184-
isPrivate: boolean,
185-
callback: Callback<unknown, string>
186-
) => void;
187-
}
188-
189171
type CompiledTemplate = (model: unknown) => string;
190172

191173
interface CompilerOptions {

0 commit comments

Comments
 (0)