-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
309 lines (272 loc) · 8.71 KB
/
index.ts
File metadata and controls
309 lines (272 loc) · 8.71 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* This worker gets source map from the Registry and puts it to Mongo
* to provide access for it for JS Worker
*/
import { RawSourceMap } from 'source-map';
import { Readable } from 'stream';
import { DatabaseController } from '../../../lib/db/controller';
import { Worker } from '../../../lib/worker';
import { DatabaseReadWriteError, NonCriticalError } from '../../../lib/workerErrors';
import * as pkg from '../package.json';
import { ReleaseWorkerTask, ReleaseWorkerAddReleasePayload, CommitDataUnparsed } from '../types';
import { Collection, MongoClient } from 'mongodb';
import { SourceMapDataExtended, SourceMapFileChunk, CommitData, SourcemapCollectedData, ReleaseDBScheme } from '@hawk.so/types';
/**
* Worker to save releases
*/
export default class ReleaseWorker extends Worker {
/**
* Worker type (will pull tasks from Registry queue with the same name)
*/
public readonly type: string = pkg.workerType;
/**
* Database Controller
*/
private db: DatabaseController = new DatabaseController(process.env.MONGO_EVENTS_DATABASE_URI);
/**
* Source maps will stored in this collection
* One for all projects
*/
private readonly dbCollectionName: string = 'releases';
/**
* Collection to save releases
*/
private releasesCollection: Collection<ReleaseDBScheme>;
/**
* Mongo client for events database, used for transactions
*/
private client: MongoClient = new MongoClient(process.env.MONGO_EVENTS_DATABASE_URI);
/**
* Start consuming messages
*/
public async start(): Promise<void> {
await this.client.connect();
await this.db.connect();
this.db.createGridFsBucket(this.dbCollectionName);
this.releasesCollection = this.db.getConnection().collection(this.dbCollectionName);
await super.start();
}
/**
* Finish everything
*/
public async finish(): Promise<void> {
await super.finish();
await this.db.close();
await this.client.close();
}
/**
* Message handle function
*
* @param task - Message object from consume method
*/
public async handle(task: ReleaseWorkerTask): Promise<void> {
switch (task.type) {
case 'add-release': await this.saveRelease(task.projectId, task.payload as ReleaseWorkerAddReleasePayload); break;
}
}
/**
* Save user's release
*
* @param projectId - project id to bind the corresponding release.
* @param payload - release payload
*/
private async saveRelease(projectId: string, payload: ReleaseWorkerAddReleasePayload): Promise<void> {
this.logger.info(`saveRelease: save release for project: ${projectId}, release: ${payload.release}`);
try {
const commits = payload.commits;
/**
* Save commits
*/
if (commits && this.areCommitsValid(commits)) {
const commitsWithParsedDate: CommitData[] = commits.map(commit => ({
...commit,
date: new Date(commit.date),
}));
await this.releasesCollection.updateOne({
projectId: projectId,
release: payload.release,
}, {
$set: {
commits: commitsWithParsedDate,
},
}, {
upsert: true,
});
}
// save source maps
if (payload.files) {
await this.saveSourceMap(projectId, payload);
}
} catch (err) {
this.logger.error(`Couldn't save the release due to: ${err}`);
throw new DatabaseReadWriteError(err);
}
}
/**
* Check commtis for the content of all required data
*
* @param commits - stringified commits
*/
private areCommitsValid(commits: CommitDataUnparsed[]): boolean {
try {
const commitValidation = (commit: CommitDataUnparsed): boolean => {
const date = Date.parse(commit.date);
return 'hash' in commit && 'author' in commit && !isNaN(date) && 'title' in commit;
};
return commits.every(commitValidation);
} catch (err) {
throw new Error(`Commtis are not valid: ${err}`);
}
}
/**
* Extract original file name from source-map's "file" property
* and extend data-to-save with it
*
* @param projectId - project id in hawk
* @param payload - source map data
*/
private async saveSourceMap(projectId: string, payload: ReleaseWorkerAddReleasePayload): Promise<void> {
/**
* Start transaction to avoid race condition
*/
const session = await this.client.startSession();
try {
const files: SourceMapDataExtended[] = this.extendReleaseInfo(payload.files);
/**
* Use same transaction for read and related write operations
*/
await session.withTransaction(async () => {
const existedRelease = await this.releasesCollection.findOne({
projectId: projectId,
release: payload.release,
}, { session });
/**
* Iterate all maps of the new release and save only new
*/
let savedFiles = await Promise.all(files.map(async (map: SourceMapDataExtended) => {
/**
* Skip already saved maps
*/
const alreadySaved = existedRelease && existedRelease.files && existedRelease.files.find((savedFile) => {
return savedFile.mapFileName === map.mapFileName;
});
if (alreadySaved) {
return;
}
try {
const fileInfo = await this.saveFile(map);
/**
* Save id of saved file instead
*/
map._id = fileInfo._id;
return map;
} catch (error) {
this.logger.error(`Map ${map.mapFileName} was not saved: ${error}`);
}
}));
/**
* Delete file content after it is saved to the GridFS
*/
savedFiles.forEach(file => {
delete file.content;
})
/**
* Filter unsaved maps
*/
savedFiles = savedFiles.filter((file) => file !== undefined);
/**
* Nothing to save: maps was previously saved
*/
if (savedFiles.length === 0) {
return;
}
/**
* - insert new record with saved maps
* or
* - update previous record with adding new saved maps
*/
if (!existedRelease) {
this.logger.info('inserted new release');
await this.releasesCollection.insertOne({
projectId: projectId,
release: payload.release,
files: savedFiles as SourceMapDataExtended[],
} as ReleaseDBScheme, { session });
}
await this.releasesCollection.findOneAndUpdate({
projectId: projectId,
release: payload.release,
}, {
$push: {
files: {
$each: savedFiles as SourceMapDataExtended[],
},
},
}, { session });
});
} catch (error) {
this.logger.error('Can\'t extract release info:\n', {
error,
});
throw new NonCriticalError('Can\'t parse source-map file');
} finally {
/**
* End transaction
*/
await session.endSession();
}
}
/**
* Extract original file name from source-map's "file" property
* and extend data-to-save with it
*
* @param {SourcemapCollectedData[]} sourceMaps — source maps passed from user after bundle
*/
private extendReleaseInfo(sourceMaps: SourcemapCollectedData[]): SourceMapDataExtended[] {
return sourceMaps.flatMap((file: SourcemapCollectedData) => {
/**
* Decode base64 source map content
*/
const buffer = Buffer.from(file.payload, 'base64');
const mapBodyString = buffer.toString();
/**
* If content is empty, return nothing for this file
*/
if (!mapBodyString || mapBodyString.trim().length === 0) {
return [];
}
/**
* @todo use more efficient method to extract "file" from big JSON
*/
const mapContent = JSON.parse(mapBodyString) as RawSourceMap;
return [ {
mapFileName: file.name,
originFileName: mapContent.file,
content: mapBodyString,
} ];
});
}
/**
* Saves source map file to the GridFS
*
* @param file - source map file extended
*/
private saveFile(file: SourceMapDataExtended): Promise<SourceMapFileChunk> {
return new Promise((resolve, reject) => {
if (!file.content) {
return reject(new Error('Source map content is empty'));
}
const readable = Readable.from([ file.content ]);
const writeStream = this.db.getBucket().openUploadStream(file.mapFileName);
readable
.pipe(writeStream)
.on('error', (error) => {
reject(error);
})
.on('finish', (info: SourceMapFileChunk) => {
readable.destroy();
resolve(info);
});
});
}
}