/
nativeVerifier.ts
191 lines (170 loc) · 5.71 KB
/
nativeVerifier.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
import { VerifierOptions } from './types';
import logger, { setLogLevel } from '../logger';
import { getFfiLib } from '../ffi';
import { VERIFY_PROVIDER_RESPONSE } from '../ffi/types';
import { URL } from 'url';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../../package.json');
import fs = require('fs');
const objArrayToStringArray = (obj: unknown[]) => {
return obj.map((o) => JSON.stringify(o));
};
export const verify = (opts: VerifierOptions): Promise<string> => {
const ffi = getFfiLib(opts.logLevel);
if (opts.logLevel) {
setLogLevel(opts.logLevel);
}
const handle = ffi.pactffiVerifierNewForApplication(
pkg.name.split('/')[1],
pkg.version
);
const uri = new URL(opts.providerBaseUrl);
ffi.pactffiVerifierSetProviderInfo(
handle,
opts.provider || '',
uri.protocol.split(':')[0],
uri.hostname,
parseInt(uri.port, 10),
uri.pathname
);
if (opts.providerStatesSetupUrl) {
ffi.pactffiVerifierSetProviderState(
handle,
opts.providerStatesSetupUrl,
opts.providerStatesSetupTeardown || true,
opts.providerStatesSetupBody || true // dumb, this means they are always set!
);
}
if (opts.customProviderHeaders) {
Object.entries(opts.customProviderHeaders).forEach(([key, value]) => {
ffi.pactffiVerifierAddCustomHeader(handle, key, value);
});
}
const filterDescription = process.env.PACT_DESCRIPTION || '';
const filterState = process.env.PACT_PROVIDER_STATE || '';
const filterNoState = process.env.PACT_PROVIDER_NO_STATE ? true : false;
ffi.pactffiVerifierSetFilterInfo(
handle,
filterDescription,
filterState,
filterNoState
);
if (opts.pactUrls) {
opts.pactUrls.forEach((file) => {
logger.debug(`checking source type of given pactUrl: ${file}`);
try {
const u = new URL(file);
if (u.hostname) {
logger.debug(`adding ${file} as a Url source`);
ffi.pactffiVerifierUrlSource(
handle,
file,
opts.pactBrokerUsername || '',
opts.pactBrokerPassword || '',
opts.pactBrokerToken || ''
);
}
} catch {
logger.debug(`${file} is not a URI`);
}
try {
const f = fs.lstatSync(file);
if (f.isDirectory()) {
logger.debug(`adding ${file} as Directory source`);
ffi.pactffiVerifierAddDirectorySource(handle, file);
} else if (f.isFile() || f.isSymbolicLink()) {
logger.debug(`adding ${file} as File source`);
ffi.pactffiVerifierAddFileSource(handle, file);
}
} catch {
logger.debug(`${file} is not a file`);
}
});
}
// TODO: extract these options into its own subtype, and check keyof
if (opts.disableSslVerification || opts.timeout) {
ffi.pactffiVerifierSetVerificationOptions(
handle,
opts.disableSslVerification || false,
opts.timeout || 30000
);
}
// TODO: extract these options into its own subtype, and check keyof
if (
opts.publishVerificationResult ||
opts.providerVersion ||
opts.buildUrl ||
opts.disableSslVerification ||
opts.timeout ||
opts.providerVersionTags
) {
ffi.pactffiVerifierSetPublishOptions(
handle,
opts.providerVersion || '',
opts.buildUrl || '',
opts.providerVersionTags || [],
opts.providerBranch || ''
);
}
const brokerUrl = opts.pactBrokerUrl || process.env.PACT_BROKER_BASE_URL;
if (brokerUrl && opts.provider) {
ffi.pactffiVerifierBrokerSourceWithSelectors(
handle,
brokerUrl,
opts.pactBrokerUsername || process.env.PACT_BROKER_USERNAME || '',
opts.pactBrokerPassword || process.env.PACT_BROKER_PASSWORD || '',
opts.pactBrokerToken || process.env.PACT_BROKER_TOKEN || '',
opts.enablePending || false,
opts.includeWipPactsSince || '',
opts.providerVersionTags || [],
opts.providerBranch || '',
opts.consumerVersionSelectors
? objArrayToStringArray(opts.consumerVersionSelectors)
: [],
opts.consumerVersionTags || []
);
}
// Todo: probably separate out the sections of this logic into separate promises
return new Promise<string>((resolve, reject) => {
ffi.pactffiVerifierExecute(handle, (err: Error, res: number) => {
logger.debug(`shutting down verifier with handle ${handle}`);
ffi.pactffiVerifierShutdown(handle);
logger.debug(`response from verifier: ${err}, ${res}`);
if (err) {
if (typeof err === 'string') {
// It might not really be an `Error`, because it comes from native code.
logger.error(err);
} else if (err.message) {
logger.error(err.message);
}
logger.pactCrash(
'The underlying pact core returned an error through the ffi interface'
);
reject(err);
} else {
switch (res) {
case VERIFY_PROVIDER_RESPONSE.VERIFICATION_SUCCESSFUL:
logger.info('Verification successful');
resolve(`finished: ${res}`);
break;
case VERIFY_PROVIDER_RESPONSE.VERIFICATION_FAILED:
logger.error('Verification unsuccessful');
reject(new Error('Verfication failed'));
break;
case VERIFY_PROVIDER_RESPONSE.INVALID_ARGUMENTS:
logger.pactCrash(
'The underlying pact core was invoked incorrectly.'
);
reject(new Error('Verification was unable to run'));
break;
default:
logger.pactCrash(
'The underlying pact core crashed in an unexpected way.'
);
reject(new Error('Pact core crashed'));
break;
}
}
});
});
};