1
1
"use strict" ;
2
+ var __createBinding = ( this && this . __createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
3
+ if ( k2 === undefined ) k2 = k ;
4
+ Object . defineProperty ( o , k2 , { enumerable : true , get : function ( ) { return m [ k ] ; } } ) ;
5
+ } ) : ( function ( o , m , k , k2 ) {
6
+ if ( k2 === undefined ) k2 = k ;
7
+ o [ k2 ] = m [ k ] ;
8
+ } ) ) ;
9
+ var __setModuleDefault = ( this && this . __setModuleDefault ) || ( Object . create ? ( function ( o , v ) {
10
+ Object . defineProperty ( o , "default" , { enumerable : true , value : v } ) ;
11
+ } ) : function ( o , v ) {
12
+ o [ "default" ] = v ;
13
+ } ) ;
14
+ var __importStar = ( this && this . __importStar ) || function ( mod ) {
15
+ if ( mod && mod . __esModule ) return mod ;
16
+ var result = { } ;
17
+ if ( mod != null ) for ( var k in mod ) if ( k !== "default" && Object . prototype . hasOwnProperty . call ( mod , k ) ) __createBinding ( result , mod , k ) ;
18
+ __setModuleDefault ( result , mod ) ;
19
+ return result ;
20
+ } ;
2
21
var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
3
22
return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
4
23
} ;
5
24
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6
25
exports . AttachmentProcessor = exports . MAESTER_OBJECT_ID_ENDPOINT = exports . DEFAULT_STORAGE_TYPE = exports . STORAGE_TYPE_PARAMETER = void 0 ;
7
26
/* eslint-disable class-methods-use-this */
8
- const axios_1 = __importDefault ( require ( "axios" ) ) ;
27
+ const axios_1 = __importStar ( require ( "axios" ) ) ;
9
28
const url_1 = require ( "url" ) ;
10
29
const dist_1 = require ( "@elastic.io/maester-client/dist" ) ;
30
+ const form_data_1 = __importDefault ( require ( "form-data" ) ) ;
31
+ const logger_1 = require ( "../logger/logger" ) ;
32
+ const logger = ( 0 , logger_1 . getLogger ) ( ) ;
11
33
exports . STORAGE_TYPE_PARAMETER = 'storage_type' ;
12
34
exports . DEFAULT_STORAGE_TYPE = 'steward' ;
13
35
exports . MAESTER_OBJECT_ID_ENDPOINT = '/objects/' ;
@@ -16,7 +38,7 @@ const maesterCreds = { jwtSecret: ELASTICIO_OBJECT_STORAGE_TOKEN, uri: ELASTICIO
16
38
const REQUEST_TIMEOUT = process . env . REQUEST_TIMEOUT ? parseInt ( process . env . REQUEST_TIMEOUT , 10 ) : 10000 ; // 10s
17
39
const REQUEST_MAX_RETRY = process . env . REQUEST_MAX_RETRY ? parseInt ( process . env . REQUEST_MAX_RETRY , 10 ) : 7 ; // 10s
18
40
const REQUEST_RETRY_DELAY = process . env . REQUEST_RETRY_DELAY ? parseInt ( process . env . REQUEST_RETRY_DELAY , 10 ) : 7000 ; // 7s
19
- const REQUEST_MAX_BODY_LENGTH = process . env . REQUEST_MAX_BODY_LENGTH ? parseInt ( process . env . REQUEST_MAX_BODY_LENGTH , 10 ) : 104857600 ; // 100MB
41
+ const axiosCriticalErrors = [ ] ; // errors that couldn't be retried
20
42
class AttachmentProcessor {
21
43
async getAttachment ( url , responseType ) {
22
44
const storageType = AttachmentProcessor . getStorageTypeByUrl ( url ) ;
@@ -34,24 +56,9 @@ class AttachmentProcessor {
34
56
default : throw new Error ( `Storage type "${ storageType } " is not supported` ) ;
35
57
}
36
58
}
37
- async uploadAttachment ( body , contentType ) {
38
- const ax = axios_1 . default . create ( ) ;
39
- AttachmentProcessor . addRetryCountInterceptorToAxios ( ax ) ;
40
- const url = `${ ELASTICIO_OBJECT_STORAGE_URI } ${ exports . MAESTER_OBJECT_ID_ENDPOINT } ` ;
41
- const axConfig = {
42
- url,
43
- data : body ,
44
- method : 'post' ,
45
- headers : {
46
- Authorization : `Bearer ${ ELASTICIO_OBJECT_STORAGE_TOKEN } ` ,
47
- 'Content-Type' : contentType ,
48
- } ,
49
- timeout : REQUEST_TIMEOUT ,
50
- retry : REQUEST_MAX_RETRY ,
51
- delay : REQUEST_RETRY_DELAY ,
52
- maxBodyLength : REQUEST_MAX_BODY_LENGTH ,
53
- } ;
54
- return ax ( axConfig ) ;
59
+ async uploadAttachment ( body ) {
60
+ logger . debug ( 'Start uploading attachment' ) ;
61
+ return axiosUploadAttachment ( body ) ;
55
62
}
56
63
static async getStewardAttachment ( axConfig ) {
57
64
const ax = axios_1 . default . create ( ) ;
@@ -94,3 +101,40 @@ class AttachmentProcessor {
94
101
}
95
102
}
96
103
exports . AttachmentProcessor = AttachmentProcessor ;
104
+ // uploads attachment to "Maester" and applies request-retry logic
105
+ const axiosUploadAttachment = async ( body , currentRetryCount = 0 ) => {
106
+ var _a ;
107
+ const data = new form_data_1 . default ( ) ;
108
+ data . append ( 'file' , body ) ;
109
+ const config = {
110
+ method : 'post' ,
111
+ url : `${ ELASTICIO_OBJECT_STORAGE_URI } ${ exports . MAESTER_OBJECT_ID_ENDPOINT } ` ,
112
+ headers : {
113
+ Authorization : `Bearer ${ ELASTICIO_OBJECT_STORAGE_TOKEN } ` ,
114
+ ...data . getHeaders ( )
115
+ } ,
116
+ maxRedirects : 0 ,
117
+ data
118
+ } ;
119
+ try {
120
+ const resp = await ( 0 , axios_1 . default ) ( config ) ;
121
+ return resp ;
122
+ }
123
+ catch ( error ) {
124
+ logger . error ( `Error occurred: ${ ( ( _a = error . response ) === null || _a === void 0 ? void 0 : _a . data ) || error . message } ` ) ;
125
+ if ( error instanceof axios_1 . AxiosError ) {
126
+ const errorCouldNotBeRetried = axiosCriticalErrors . includes ( error . code ) ;
127
+ if ( errorCouldNotBeRetried )
128
+ throw error ;
129
+ }
130
+ if ( currentRetryCount + 1 <= REQUEST_MAX_RETRY ) {
131
+ logger . debug ( `Start retrying #${ currentRetryCount + 1 } ` ) ;
132
+ await sleep ( REQUEST_RETRY_DELAY ) ;
133
+ return axiosUploadAttachment ( body , currentRetryCount + 1 ) ;
134
+ }
135
+ throw error ;
136
+ }
137
+ } ;
138
+ const sleep = async ( ms ) => new Promise ( ( resolve ) => {
139
+ setTimeout ( resolve , ms ) ;
140
+ } ) ;
0 commit comments