-
Notifications
You must be signed in to change notification settings - Fork 72
/
index.ts
67 lines (59 loc) · 1.97 KB
/
index.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
import * as crypto from 'crypto';
import * as aws from './cloud-platforms/aws';
import * as gcp from './cloud-platforms/gcp';
import * as localHandlers from './local-handlers';
import { WorkerParameters, WorkerResponse } from '../types';
import { LocalOrCloudProvider } from '../config';
const DEPLOYMENT_ID_LENGTH = 8;
export function spawn(params: WorkerParameters): Promise<WorkerResponse> {
switch (params.cloudProvider.type) {
case 'aws':
return aws.spawn(params);
case 'gcp':
return gcp.spawn(params);
case 'local':
switch (params.payload.functionName) {
case 'initializeProvider':
return localHandlers.initializeProvider(params.payload);
case 'callApi':
return localHandlers.callApi(params.payload);
case 'processTransactions':
return localHandlers.processTransactions(params.payload);
}
}
}
function cloudProviderHashElements(cloudProvider: LocalOrCloudProvider) {
const hashElements: string[] = [cloudProvider.type];
if (cloudProvider.type !== 'local') {
hashElements.push(cloudProvider.region);
}
if (cloudProvider.type === 'gcp') {
hashElements.push(cloudProvider.projectId);
}
return hashElements;
}
export function deriveDeploymentId(
cloudProvider: LocalOrCloudProvider,
airnodeAddress: string,
stage: string,
airnodeVersion: string
) {
return `${cloudProvider.type}${crypto
.createHash('sha256')
.update([...cloudProviderHashElements(cloudProvider), airnodeAddress, stage, airnodeVersion].join(''))
.digest('hex')
.substring(0, DEPLOYMENT_ID_LENGTH)}`;
}
export function deriveDeploymentVersionId(
cloudProvider: LocalOrCloudProvider,
airnodeAddress: string,
stage: string,
airnodeVersion: string,
timestamp: string
) {
return crypto
.createHash('sha256')
.update([...cloudProviderHashElements(cloudProvider), airnodeAddress, stage, airnodeVersion, timestamp].join(''))
.digest('hex')
.substring(0, DEPLOYMENT_ID_LENGTH);
}