1
- import { StepCheckCaptures , StepCheckJSONPath , StepCheckMatcher , StepCheckPerformance } from ".." ;
2
- import { CapturesStorage } from "./../utils/runner" ;
3
- import { TLSCertificate , getTLSCertificate } from "./../utils/auth" ;
1
+ import {
2
+ StepCheckCaptures ,
3
+ StepCheckJSONPath ,
4
+ StepCheckMatcher ,
5
+ StepCheckPerformance ,
6
+ } from '..'
7
+ import { CapturesStorage } from './../utils/runner'
8
+ import { TLSCertificate , getTLSCertificate } from './../utils/auth'
4
9
import { Credential } from './../utils/auth'
5
10
6
- import path from " node:path" ;
7
- import { JSONPath } from " jsonpath-plus" ;
8
- import Ajv from " ajv" ;
9
- import { StepRunResult , WorkflowConfig , WorkflowOptions } from ".." ;
10
- import { Matcher , checkResult } from " ../matcher" ;
11
- import { gRPCRequestMetadata , makeRequest } from " cool-grpc" ;
12
- const { co2 } = require ( " @tgwf/co2" ) ;
11
+ import path from ' node:path'
12
+ import { JSONPath } from ' jsonpath-plus'
13
+ import Ajv from ' ajv'
14
+ import { StepRunResult , WorkflowConfig , WorkflowOptions } from '..'
15
+ import { Matcher , checkResult } from ' ../matcher'
16
+ import { gRPCRequestMetadata , makeRequest } from ' cool-grpc'
17
+ const { co2 } = require ( ' @tgwf/co2' )
13
18
14
19
export type gRPCStep = {
15
20
proto : string | string [ ]
@@ -74,33 +79,33 @@ export default async function gRPCStep(
74
79
config ?: WorkflowConfig
75
80
) {
76
81
const stepResult : StepRunResult = {
77
- type : " grpc" ,
78
- } ;
82
+ type : ' grpc' ,
83
+ }
79
84
80
- const ssw = new co2 ( ) ;
85
+ const ssw = new co2 ( )
81
86
82
87
// Load TLS configuration from file or string
83
- let tlsConfig : TLSCertificate | undefined ;
88
+ let tlsConfig : TLSCertificate | undefined
84
89
if ( params . auth ) {
85
90
tlsConfig = await getTLSCertificate ( params . auth . tls , {
86
91
workflowPath : options ?. path ,
87
- } ) ;
92
+ } )
88
93
}
89
94
90
- const protos : string [ ] = [ ] ;
95
+ const protos : string [ ] = [ ]
91
96
if ( config ?. grpc ?. proto ) {
92
- protos . push ( ...config . grpc . proto ) ;
97
+ protos . push ( ...config . grpc . proto )
93
98
}
94
99
95
100
if ( params . proto ) {
96
101
protos . push (
97
102
...( Array . isArray ( params . proto ) ? params . proto : [ params . proto ] )
98
- ) ;
103
+ )
99
104
}
100
105
101
106
const proto = protos . map ( ( p ) =>
102
107
path . join ( path . dirname ( options ?. path || __dirname ) , p )
103
- ) ;
108
+ )
104
109
105
110
const request : gRPCStepRequest = {
106
111
proto,
@@ -109,110 +114,110 @@ export default async function gRPCStep(
109
114
service : params . service ,
110
115
method : params . method ,
111
116
data : params . data ,
112
- } ;
117
+ }
113
118
114
119
const { metadata, statusCode, statusMessage, data, size } = await makeRequest (
115
120
proto ,
116
121
{
117
122
...request ,
118
123
tls : tlsConfig ,
119
124
beforeRequest : ( req ) => {
120
- options ?. ee ?. emit ( " step:grpc_request" , request ) ;
125
+ options ?. ee ?. emit ( ' step:grpc_request' , request )
121
126
} ,
122
127
afterResponse : ( res ) => {
123
- options ?. ee ?. emit ( " step:grpc_response" , res ) ;
128
+ options ?. ee ?. emit ( ' step:grpc_response' , res )
124
129
} ,
125
130
}
126
- ) ;
131
+ )
127
132
128
- stepResult . request = request ;
133
+ stepResult . request = request
129
134
stepResult . response = {
130
135
body : data ,
131
136
co2 : ssw . perByte ( size ) ,
132
137
size : size ,
133
138
status : statusCode ,
134
139
statusText : statusMessage ,
135
140
metadata,
136
- } ;
141
+ }
137
142
138
143
// Captures
139
144
if ( params . captures ) {
140
145
for ( const name in params . captures ) {
141
- const capture = params . captures [ name ] ;
146
+ const capture = params . captures [ name ]
142
147
if ( capture . jsonpath ) {
143
- captures [ name ] = JSONPath ( { path : capture . jsonpath , json : data } ) [ 0 ] ;
148
+ captures [ name ] = JSONPath ( { path : capture . jsonpath , json : data } ) [ 0 ]
144
149
}
145
150
}
146
151
}
147
152
148
153
if ( params . check ) {
149
- stepResult . checks = { } ;
154
+ stepResult . checks = { }
150
155
151
156
// Check JSON
152
157
if ( params . check . json ) {
153
- stepResult . checks . json = checkResult ( data , params . check . json ) ;
158
+ stepResult . checks . json = checkResult ( data , params . check . json )
154
159
}
155
160
156
161
// Check Schema
157
162
if ( params . check . schema ) {
158
- const validate = schemaValidator . compile ( params . check . schema ) ;
163
+ const validate = schemaValidator . compile ( params . check . schema )
159
164
stepResult . checks . schema = {
160
165
expected : params . check . schema ,
161
166
given : data ,
162
167
passed : validate ( data ) ,
163
- } ;
168
+ }
164
169
}
165
170
166
171
// Check JSONPath
167
172
if ( params . check . jsonpath ) {
168
- stepResult . checks . jsonpath = { } ;
173
+ stepResult . checks . jsonpath = { }
169
174
170
175
for ( const path in params . check . jsonpath ) {
171
- const result = JSONPath ( { path, json : data } ) ;
176
+ const result = JSONPath ( { path, json : data } )
172
177
stepResult . checks . jsonpath [ path ] = checkResult (
173
178
result [ 0 ] ,
174
179
params . check . jsonpath [ path ]
175
- ) ;
180
+ )
176
181
}
177
182
}
178
183
179
184
// Check captures
180
185
if ( params . check . captures ) {
181
- stepResult . checks . captures = { } ;
186
+ stepResult . checks . captures = { }
182
187
183
188
for ( const capture in params . check . captures ) {
184
189
stepResult . checks . captures [ capture ] = checkResult (
185
190
captures [ capture ] ,
186
191
params . check . captures [ capture ]
187
- ) ;
192
+ )
188
193
}
189
194
}
190
195
191
196
// Check performance
192
197
if ( params . check . performance ) {
193
- stepResult . checks . performance = { } ;
198
+ stepResult . checks . performance = { }
194
199
195
200
if ( params . check . performance . total ) {
196
201
stepResult . checks . performance . total = checkResult (
197
202
stepResult . response ?. duration ,
198
203
params . check . performance . total
199
- ) ;
204
+ )
200
205
}
201
206
}
202
207
203
208
// Check byte size
204
209
if ( params . check . size ) {
205
- stepResult . checks . size = checkResult ( size , params . check . size ) ;
210
+ stepResult . checks . size = checkResult ( size , params . check . size )
206
211
}
207
212
208
213
// Check co2 emissions
209
214
if ( params . check . co2 ) {
210
215
stepResult . checks . co2 = checkResult (
211
216
stepResult . response ?. co2 ,
212
217
params . check . co2
213
- ) ;
218
+ )
214
219
}
215
220
}
216
221
217
- return stepResult ;
222
+ return stepResult
218
223
}
0 commit comments