MCPcopy
hub / github.com/nock/nock / record

Function record

lib/recorder.js:177–314  ·  view source on GitHub ↗
(recOptions)

Source from the content-addressed store, hash-verified

175}
176
177function record(recOptions) {
178 // Trying to start recording with recording already in progress implies an error
179 // in the recording configuration (double recording makes no sense and used to lead
180 // to duplicates in output)
181 if (recordingInProgress) {
182 throw new Error('Nock recording already in progress')
183 }
184
185 recordingInProgress = true
186
187 // Set the new current recording ID and capture its value in this instance of record().
188 currentRecordingId = currentRecordingId + 1
189 const thisRecordingId = currentRecordingId
190
191 // Originally the parameter was a dont_print boolean flag.
192 // To keep the existing code compatible we take that case into account.
193 if (typeof recOptions === 'boolean') {
194 recOptions = { dont_print: recOptions }
195 }
196
197 recOptions = { ...defaultRecordOptions, ...recOptions }
198
199 debug('start recording', thisRecordingId, recOptions)
200
201 const {
202 dont_print: dontPrint,
203 enable_reqheaders_recording: enableReqHeadersRecording,
204 logging,
205 output_objects: outputObjects,
206 use_separator: useSeparator,
207 } = recOptions
208
209 debug(thisRecordingId, 'restoring overridden requests before new overrides')
210 // To preserve backward compatibility (starting recording wasn't throwing if nock was already active)
211 // we restore any requests that may have been overridden by other parts of nock (e.g. intercept)
212 // NOTE: This is hacky as hell but it keeps the backward compatibility *and* allows correct
213 // behavior in the face of other modules also overriding ClientRequest.
214 // common.restoreOverriddenRequests()
215 // We restore ClientRequest as it messes with recording of modules that also override ClientRequest (e.g. xhr2)
216 restoreOverriddenClientRequest()
217
218 // We override the requests so that we can save information on them before executing.
219 clientRequestInterceptor.apply()
220 fetchRequestInterceptor.apply()
221 clientRequestInterceptor.on(
222 'response',
223 async function ({ request, response }) {
224 await recordResponse(request, response)
225 },
226 )
227 fetchRequestInterceptor.on(
228 'response',
229 async function ({ request, response }) {
230 // fetch decompresses the body automatically, so we need to recompress it
231 const codings =
232 response.headers
233 .get('content-encoding')
234 ?.toLowerCase()

Callers

nothing calls this directly

Calls 3

recordResponseFunction · 0.85
onMethod · 0.80
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…