-
Notifications
You must be signed in to change notification settings - Fork 38
/
httpFileStore.ts
197 lines (178 loc) · 6.51 KB
/
httpFileStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { HookCancel } from 'hookpoint';
import { fileProvider, httpClientProvider, javascriptProvider, log, userInteractionProvider } from '../io';
import * as models from '../models';
import { userSessionStore as sessionStore } from '../store';
import * as utils from '../utils';
import { getEnvironmentConfig } from './getEnvironmentConfig';
import { HttpFile } from './httpFile';
import { parseHttpFile } from './parser';
import { pluginStore } from './pluginStore';
interface HttpFileStoreEntry {
version: number;
cacheKey: string;
httpFile?: models.HttpFile;
promise?: Promise<models.HttpFile>;
}
export class HttpFileStore implements models.HttpFileStore {
private readonly storeCache: Array<HttpFileStoreEntry> = [];
constructor(private readonly plugins: Record<string, models.ConfigureHooks> = {}) {}
private getFromStore(fileName: models.PathLike, version: number) {
const cacheKey = fileProvider.toString(fileName);
let httpFileStoreEntry = this.storeCache.find(obj => obj.cacheKey === cacheKey);
if (!httpFileStoreEntry) {
httpFileStoreEntry = {
cacheKey,
version,
};
this.storeCache.push(httpFileStoreEntry);
}
return httpFileStoreEntry;
}
get(fileName: models.PathLike): models.HttpFile | undefined {
const cacheKey = fileProvider.toString(fileName);
return this.storeCache.find(obj => obj.cacheKey === cacheKey)?.httpFile;
}
getAll(): Array<models.HttpFile> {
const result: Array<models.HttpFile> = [];
for (const store of this.storeCache) {
if (store.httpFile) {
result.push(store.httpFile);
}
}
return result;
}
getOrCreate(
fileName: models.PathLike,
getText: () => Promise<string>,
version: number,
options: models.HttpFileStoreOptions
): Promise<models.HttpFile> {
const httpFileStoreEntry: HttpFileStoreEntry = this.getFromStore(fileName, version);
if (version > httpFileStoreEntry.version || !httpFileStoreEntry.httpFile) {
if (httpFileStoreEntry.promise && version <= httpFileStoreEntry.version) {
return httpFileStoreEntry.promise;
}
httpFileStoreEntry.promise = getText()
.then(text => this.parse(fileName, text, options))
.then(httpFile => {
delete httpFileStoreEntry.promise;
if (httpFileStoreEntry.httpFile) {
for (const httpRegion of httpFile.httpRegions) {
const prevHttpRegion = httpFileStoreEntry.httpFile.httpRegions.find(
obj => obj.symbol.source === httpRegion.symbol.source
);
if (prevHttpRegion) {
Object.assign(httpRegion.variablesPerEnv, prevHttpRegion.variablesPerEnv);
}
}
}
httpFileStoreEntry.version = version;
httpFileStoreEntry.httpFile = httpFile;
return httpFile;
})
.catch(err => {
delete httpFileStoreEntry.promise;
if (httpFileStoreEntry.httpFile) {
this.remove(httpFileStoreEntry.httpFile.fileName);
}
throw err;
});
return httpFileStoreEntry.promise;
}
return httpFileStoreEntry.promise || Promise.resolve(httpFileStoreEntry.httpFile);
}
async parse(fileName: models.PathLike, text: string, options: models.HttpFileStoreOptions): Promise<models.HttpFile> {
const httpFile = await this.initHttpFile(fileName, options);
return await parseHttpFile(httpFile, text, this);
}
remove(fileName: models.PathLike): boolean {
const cacheKey = fileProvider.toString(fileName);
const index = this.storeCache.findIndex(obj => obj.cacheKey === cacheKey);
if (index >= 0) {
this.storeCache.splice(index, 1);
return true;
}
return false;
}
rename(oldFileName: models.PathLike, newFileName: models.PathLike): void {
const oldCacheKey = fileProvider.toString(oldFileName);
const httpFileStoreEntry = this.storeCache.find(obj => obj.cacheKey === oldCacheKey);
if (httpFileStoreEntry) {
httpFileStoreEntry.cacheKey = fileProvider.toString(newFileName);
if (httpFileStoreEntry.httpFile) {
httpFileStoreEntry.httpFile.fileName = newFileName;
}
}
}
public clear(): void {
this.storeCache.length = 0;
}
public async initHttpFile(fileName: models.PathLike, options: models.HttpFileStoreOptions): Promise<models.HttpFile> {
const absoluteFileName = (await utils.toAbsoluteFilename(fileName, options.workingDir)) || fileName;
const rootDir =
(await utils.findRootDirOfFile(absoluteFileName, options.workingDir, ...utils.defaultConfigFiles)) ||
(await utils.findRootDirOfFile(
absoluteFileName,
options.workingDir,
'package.json',
options.config?.envDirName || 'env'
)) ||
options.workingDir;
const httpFile = new HttpFile(absoluteFileName, rootDir);
options.config = await getEnvironmentConfig(options.config, httpFile);
const hooks: Record<string, models.ConfigureHooks> = { ...pluginStore, ...this.plugins };
if (rootDir) {
Object.assign(hooks, await utils.getPlugins(rootDir));
if (options.config?.configureHooks) {
hooks['.httpyac.js'] = options.config.configureHooks;
}
}
const envPluginLocation = process.env.HTTPYAC_PLUGIN;
if (envPluginLocation) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const envHook = require(envPluginLocation);
if (envHook.configureHooks) {
hooks.HTTPYAC_PLUGIN = envHook.configureHooks;
}
} catch (err) {
log.warn('Global Hook Plugin not loaded', err);
}
}
await this.configureHooks(httpFile, options, hooks);
return httpFile;
}
private async configureHooks(
httpFile: models.HttpFile,
options: models.HttpFileStoreOptions,
hooks: Record<string, models.ConfigureHooks>
) {
if (options.config) {
const api: models.HttpyacHooksApi = {
version: '1.0.0',
rootDir: httpFile.rootDir,
config: options.config,
httpFile,
hooks: httpFile.hooks,
log,
fileProvider,
sessionStore,
userInteractionProvider,
httpClientProvider,
javascriptProvider,
utils,
getHookCancel: () => HookCancel,
};
for (const [plugin, hook] of Object.entries(hooks)) {
try {
const result = hook(api);
if (utils.isPromise(result)) {
await result;
}
} catch (err) {
log.error(`error in ${plugin}`, err);
}
}
}
}
}