@@ -2,10 +2,12 @@ import { asyncRetry, linearDelay } from '../../../util/async_retry'
22import { isRetryableError } from '../../network/is_retryable_error'
33import fetch from 'cross-fetch'
44import os from 'os'
5- import { agent } from '@packages/network'
5+ import { strictAgent } from '@packages/network'
66import { PUBLIC_KEY_VERSION } from '../../constants'
77import { createWriteStream } from 'fs'
88import { verifySignatureFromFile } from '../../encryption'
9+ import { HttpError } from '../../network/http_error'
10+ import { SystemError } from '../../network/system_error'
911
1012const pkg = require ( '@packages/root' )
1113const _delay = linearDelay ( 500 )
@@ -15,39 +17,56 @@ export const getCyPromptBundle = async ({ cyPromptUrl, projectId, bundlePath }:
1517 let responseManifestSignature : string | null = null
1618
1719 await ( asyncRetry ( async ( ) => {
18- const response = await fetch ( cyPromptUrl , {
19- // @ts -expect-error - this is supported
20- agent,
21- method : 'GET' ,
22- headers : {
23- 'x-route-version' : '1' ,
24- 'x-cypress-signature' : PUBLIC_KEY_VERSION ,
25- ...( projectId ? { 'x-cypress-project-slug' : projectId } : { } ) ,
26- 'x-cypress-cy-prompt-mount-version' : '1' ,
27- 'x-os-name' : os . platform ( ) ,
28- 'x-cypress-version' : pkg . version ,
29- } ,
30- encrypt : 'signed' ,
31- } )
32-
33- if ( ! response . ok ) {
34- throw new Error ( `Failed to download cy-prompt bundle: ${ response . statusText } ` )
35- }
20+ try {
21+ const response = await fetch ( cyPromptUrl , {
22+ // @ts -expect-error - this is supported
23+ agent : strictAgent ,
24+ method : 'GET' ,
25+ headers : {
26+ 'x-route-version' : '1' ,
27+ 'x-cypress-signature' : PUBLIC_KEY_VERSION ,
28+ ...( projectId ? { 'x-cypress-project-slug' : projectId } : { } ) ,
29+ 'x-cypress-cy-prompt-mount-version' : '1' ,
30+ 'x-os-name' : os . platform ( ) ,
31+ 'x-cypress-version' : pkg . version ,
32+ } ,
33+ encrypt : 'signed' ,
34+ } )
35+
36+ if ( ! response . ok ) {
37+ const err = await HttpError . fromResponse ( response )
38+
39+ throw err
40+ }
3641
37- responseSignature = response . headers . get ( 'x-cypress-signature' )
38- responseManifestSignature = response . headers . get ( 'x-cypress-manifest-signature' )
42+ responseSignature = response . headers . get ( 'x-cypress-signature' )
43+ responseManifestSignature = response . headers . get ( 'x-cypress-manifest-signature' )
3944
40- await new Promise < void > ( ( resolve , reject ) => {
41- const writeStream = createWriteStream ( bundlePath )
45+ await new Promise < void > ( ( resolve , reject ) => {
46+ const writeStream = createWriteStream ( bundlePath )
4247
43- writeStream . on ( 'error' , reject )
44- writeStream . on ( 'finish' , ( ) => {
45- resolve ( )
48+ writeStream . on ( 'error' , reject )
49+ writeStream . on ( 'finish' , ( ) => {
50+ resolve ( )
51+ } )
52+
53+ // @ts -expect-error - this is supported
54+ response . body ?. pipe ( writeStream )
4655 } )
56+ } catch ( error ) {
57+ if ( HttpError . isHttpError ( error ) ) {
58+ throw error
59+ }
60+
61+ if ( error . errno || error . code ) {
62+ const sysError = new SystemError ( error , cyPromptUrl , error . code , error . errno )
4763
48- // @ts -expect-error - this is supported
49- response . body ?. pipe ( writeStream )
50- } )
64+ sysError . stack = error . stack
65+ throw sysError
66+ }
67+
68+ throw error
69+ }
5170 } , {
5271 maxAttempts : 3 ,
5372 retryDelay : _delay ,
0 commit comments