1
1
const { parseString } = require ( 'xml2js' ) ;
2
- const { errors, s3middleware } = require ( 'arsenal' ) ;
2
+ const { errors, errorInstances , s3middleware } = require ( 'arsenal' ) ;
3
3
4
4
const escapeForXml = s3middleware . escapeForXml ;
5
5
const { WebsiteConfiguration } =
@@ -152,7 +152,7 @@ function _validateWebsiteConfigXml(parsingResult) {
152
152
if ( ! parsingResult . IndexDocument && ! parsingResult . RedirectAllRequestsTo ) {
153
153
errMsg = 'Value for IndexDocument Suffix must be provided if ' +
154
154
'RedirectAllRequestsTo is empty' ;
155
- return errors . InvalidArgument . customizeDescription ( errMsg ) ;
155
+ return errorInstances . InvalidArgument . customizeDescription ( errMsg ) ;
156
156
}
157
157
158
158
if ( parsingResult . RedirectAllRequestsTo ) {
@@ -162,23 +162,23 @@ function _validateWebsiteConfigXml(parsingResult) {
162
162
parsingResult . RoutingRules ) {
163
163
errMsg = 'RedirectAllRequestsTo cannot be provided in ' +
164
164
'conjunction with other Routing Rules.' ;
165
- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
165
+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
166
166
}
167
167
if ( ! xmlContainsElem ( parent , 'HostName' ) ) {
168
168
errMsg = 'RedirectAllRequestsTo not well-formed' ;
169
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
169
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
170
170
}
171
171
if ( ! _isValidString ( parent [ 0 ] . HostName [ 0 ] ) ) {
172
172
errMsg = 'Valid HostName required in RedirectAllRequestsTo' ;
173
- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
173
+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
174
174
}
175
175
redirectAllObj . hostName = parent [ 0 ] . HostName [ 0 ] ;
176
176
if ( xmlContainsElem ( parent , 'Protocol' , { validateParent : false } ) ) {
177
177
if ( parent [ 0 ] . Protocol [ 0 ] !== 'http' &&
178
178
parent [ 0 ] . Protocol [ 0 ] !== 'https' ) {
179
179
errMsg = 'Invalid protocol, protocol can be http or https. ' +
180
180
'If not defined, the protocol will be selected automatically.' ;
181
- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
181
+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
182
182
}
183
183
redirectAllObj . protocol = parent [ 0 ] . Protocol [ 0 ] ;
184
184
}
@@ -189,11 +189,11 @@ function _validateWebsiteConfigXml(parsingResult) {
189
189
const parent = parsingResult . IndexDocument ;
190
190
if ( ! xmlContainsElem ( parent , 'Suffix' ) ) {
191
191
errMsg = 'IndexDocument is not well-formed' ;
192
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
192
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
193
193
} else if ( ! _isValidString ( parent [ 0 ] . Suffix [ 0 ] )
194
194
|| parent [ 0 ] . Suffix [ 0 ] . indexOf ( '/' ) !== - 1 ) {
195
195
errMsg = 'IndexDocument Suffix is not well-formed' ;
196
- return errors . InvalidArgument . customizeDescription ( errMsg ) ;
196
+ return errorInstances . InvalidArgument . customizeDescription ( errMsg ) ;
197
197
}
198
198
websiteConfig . setIndexDocument ( parent [ 0 ] . Suffix [ 0 ] ) ;
199
199
}
@@ -202,11 +202,11 @@ function _validateWebsiteConfigXml(parsingResult) {
202
202
const parent = parsingResult . ErrorDocument ;
203
203
if ( ! xmlContainsElem ( parent , 'Key' ) ) {
204
204
errMsg = 'ErrorDocument is not well-formed' ;
205
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
205
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
206
206
}
207
207
if ( ! _isValidString ( parent [ 0 ] . Key [ 0 ] ) ) {
208
208
errMsg = 'ErrorDocument Key is not well-formed' ;
209
- return errors . InvalidArgument . customizeDescription ( errMsg ) ;
209
+ return errorInstances . InvalidArgument . customizeDescription ( errMsg ) ;
210
210
}
211
211
websiteConfig . setErrorDocument ( parent [ 0 ] . Key [ 0 ] ) ;
212
212
}
@@ -215,15 +215,15 @@ function _validateWebsiteConfigXml(parsingResult) {
215
215
const parent = parsingResult . RoutingRules ;
216
216
if ( ! xmlContainsElem ( parent , 'RoutingRule' , { isList : true } ) ) {
217
217
errMsg = 'RoutingRules is not well-formed' ;
218
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
218
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
219
219
}
220
220
for ( let i = 0 ; i < parent [ 0 ] . RoutingRule . length ; i ++ ) {
221
221
const rule = parent [ 0 ] . RoutingRule [ i ] ;
222
222
const ruleObj = { redirect : { } } ;
223
223
if ( ! _isValidElem ( rule . Redirect ) ) {
224
224
errMsg = 'RoutingRule requires Redirect, which is ' +
225
225
'missing or not well-formed' ;
226
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
226
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
227
227
}
228
228
// Looks like AWS doesn't actually make this check, but AWS
229
229
// documentation specifies at least one of the following elements
@@ -237,7 +237,7 @@ function _validateWebsiteConfigXml(parsingResult) {
237
237
errMsg = 'Redirect must contain at least one of ' +
238
238
'following: Protocol, HostName, ReplaceKeyPrefixWith, ' +
239
239
'ReplaceKeyWith, or HttpRedirectCode element' ;
240
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
240
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
241
241
}
242
242
if ( rule . Redirect [ 0 ] . Protocol ) {
243
243
if ( ! _isValidElem ( rule . Redirect [ 0 ] . Protocol ) ||
@@ -246,24 +246,24 @@ function _validateWebsiteConfigXml(parsingResult) {
246
246
errMsg = 'Invalid protocol, protocol can be http or ' +
247
247
'https. If not defined, the protocol will be selected ' +
248
248
'automatically.' ;
249
- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
249
+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
250
250
}
251
251
ruleObj . redirect . protocol = rule . Redirect [ 0 ] . Protocol [ 0 ] ;
252
252
}
253
253
if ( rule . Redirect [ 0 ] . HttpRedirectCode ) {
254
254
errMsg = 'The provided HTTP redirect code is not valid. ' +
255
255
'It should be a string containing a number.' ;
256
256
if ( ! _isValidElem ( rule . Redirect [ 0 ] . HttpRedirectCode ) ) {
257
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
257
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
258
258
}
259
259
const code = parseInt ( rule . Redirect [ 0 ] . HttpRedirectCode [ 0 ] , 10 ) ;
260
260
if ( Number . isNaN ( code ) ) {
261
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
261
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
262
262
}
263
263
if ( ! ( code > 300 && code < 400 ) ) {
264
264
errMsg = `The provided HTTP redirect code (${ code } ) is ` +
265
265
'not valid. Valid codes are 3XX except 300' ;
266
- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
266
+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
267
267
}
268
268
ruleObj . redirect . httpRedirectCode = code ;
269
269
}
@@ -273,7 +273,7 @@ function _validateWebsiteConfigXml(parsingResult) {
273
273
if ( elem ) {
274
274
if ( ! _isValidElem ( elem ) || ! _isValidString ( elem [ 0 ] ) ) {
275
275
errMsg = `Redirect ${ elem } is not well-formed` ;
276
- return errors . InvalidArgument
276
+ return errorInstances . InvalidArgument
277
277
. customizeDescription ( errMsg ) ;
278
278
}
279
279
ruleObj . redirect [ `${ elemName . charAt ( 0 ) . toLowerCase ( ) } ` +
@@ -286,7 +286,7 @@ function _validateWebsiteConfigXml(parsingResult) {
286
286
{ validateParent : false , checkForAll : true } ) ) {
287
287
errMsg = 'Redirect must not contain both ReplaceKeyWith ' +
288
288
'and ReplaceKeyPrefixWith' ;
289
- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
289
+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
290
290
}
291
291
if ( Array . isArray ( rule . Condition ) && rule . Condition . length === 1 ) {
292
292
ruleObj . condition = { } ;
@@ -295,14 +295,14 @@ function _validateWebsiteConfigXml(parsingResult) {
295
295
errMsg = 'Condition is not well-formed. ' +
296
296
'Condition should contain valid KeyPrefixEquals or ' +
297
297
'HttpErrorCodeReturnEquals element.' ;
298
- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
298
+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
299
299
}
300
300
if ( rule . Condition [ 0 ] . KeyPrefixEquals ) {
301
301
const keyPrefixEquals = rule . Condition [ 0 ] . KeyPrefixEquals ;
302
302
if ( ! _isValidElem ( keyPrefixEquals ) ||
303
303
! _isValidString ( keyPrefixEquals [ 0 ] ) ) {
304
304
errMsg = 'Condition KeyPrefixEquals is not well-formed' ;
305
- return errors . InvalidArgument
305
+ return errorInstances . InvalidArgument
306
306
. customizeDescription ( errMsg ) ;
307
307
}
308
308
ruleObj . condition . keyPrefixEquals = keyPrefixEquals [ 0 ] ;
@@ -312,17 +312,17 @@ function _validateWebsiteConfigXml(parsingResult) {
312
312
'It should be a string containing a number.' ;
313
313
if ( ! _isValidElem ( rule . Condition [ 0 ]
314
314
. HttpErrorCodeReturnedEquals ) ) {
315
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
315
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
316
316
}
317
317
const code = parseInt ( rule . Condition [ 0 ]
318
318
. HttpErrorCodeReturnedEquals [ 0 ] , 10 ) ;
319
319
if ( Number . isNaN ( code ) ) {
320
- return errors . MalformedXML . customizeDescription ( errMsg ) ;
320
+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
321
321
}
322
322
if ( ! ( code > 399 && code < 600 ) ) {
323
323
errMsg = `The provided HTTP error code (${ code } ) is ` +
324
324
'not valid. Valid codes are 4XX or 5XX.' ;
325
- return errors . InvalidRequest
325
+ return errorInstances . InvalidRequest
326
326
. customizeDescription ( errMsg ) ;
327
327
}
328
328
ruleObj . condition . httpErrorCodeReturnedEquals = code ;
@@ -347,7 +347,7 @@ function parseWebsiteConfigXml(xml, log, cb) {
347
347
348
348
if ( ! result || ! result . WebsiteConfiguration ) {
349
349
const errMsg = 'Invalid website configuration xml' ;
350
- return cb ( errors . MalformedXML . customizeDescription ( errMsg ) ) ;
350
+ return cb ( errorInstances . MalformedXML . customizeDescription ( errMsg ) ) ;
351
351
}
352
352
353
353
const validationRes =
0 commit comments