@@ -11,13 +11,34 @@ import Page from '../Page';
11
11
// All pages with custom page key for reference and file name
12
12
const pages = Object . freeze ( {
13
13
passwordReset : new Page ( { id : 'passwordReset' , defaultFile : 'password_reset.html' } ) ,
14
- passwordResetSuccess : new Page ( { id : 'passwordResetSuccess' , defaultFile : 'password_reset_success.html' } ) ,
15
- passwordResetLinkInvalid : new Page ( { id : 'passwordResetLinkInvalid' , defaultFile : 'password_reset_link_invalid.html' } ) ,
16
- emailVerificationSuccess : new Page ( { id : 'emailVerificationSuccess' , defaultFile : 'email_verification_success.html' } ) ,
17
- emailVerificationSendFail : new Page ( { id : 'emailVerificationSendFail' , defaultFile : 'email_verification_send_fail.html' } ) ,
18
- emailVerificationSendSuccess : new Page ( { id : 'emailVerificationSendSuccess' , defaultFile : 'email_verification_send_success.html' } ) ,
19
- emailVerificationLinkInvalid : new Page ( { id : 'emailVerificationLinkInvalid' , defaultFile : 'email_verification_link_invalid.html' } ) ,
20
- emailVerificationLinkExpired : new Page ( { id : 'emailVerificationLinkExpired' , defaultFile : 'email_verification_link_expired.html' } ) ,
14
+ passwordResetSuccess : new Page ( {
15
+ id : 'passwordResetSuccess' ,
16
+ defaultFile : 'password_reset_success.html' ,
17
+ } ) ,
18
+ passwordResetLinkInvalid : new Page ( {
19
+ id : 'passwordResetLinkInvalid' ,
20
+ defaultFile : 'password_reset_link_invalid.html' ,
21
+ } ) ,
22
+ emailVerificationSuccess : new Page ( {
23
+ id : 'emailVerificationSuccess' ,
24
+ defaultFile : 'email_verification_success.html' ,
25
+ } ) ,
26
+ emailVerificationSendFail : new Page ( {
27
+ id : 'emailVerificationSendFail' ,
28
+ defaultFile : 'email_verification_send_fail.html' ,
29
+ } ) ,
30
+ emailVerificationSendSuccess : new Page ( {
31
+ id : 'emailVerificationSendSuccess' ,
32
+ defaultFile : 'email_verification_send_success.html' ,
33
+ } ) ,
34
+ emailVerificationLinkInvalid : new Page ( {
35
+ id : 'emailVerificationLinkInvalid' ,
36
+ defaultFile : 'email_verification_link_invalid.html' ,
37
+ } ) ,
38
+ emailVerificationLinkExpired : new Page ( {
39
+ id : 'emailVerificationLinkExpired' ,
40
+ defaultFile : 'email_verification_link_expired.html' ,
41
+ } ) ,
21
42
} ) ;
22
43
23
44
// All page parameters for reference to be used as template placeholders or query params
@@ -36,8 +57,8 @@ const pageParamHeaderPrefix = 'x-parse-page-param-';
36
57
37
58
// The errors being thrown
38
59
const errors = Object . freeze ( {
39
- jsonFailedFileLoading : " failed to load JSON file" ,
40
- fileOutsideAllowedScope : " not allowed to read file outside of pages directory"
60
+ jsonFailedFileLoading : ' failed to load JSON file' ,
61
+ fileOutsideAllowedScope : ' not allowed to read file outside of pages directory' ,
41
62
} ) ;
42
63
43
64
export class PagesRouter extends PromiseRouter {
@@ -50,9 +71,7 @@ export class PagesRouter extends PromiseRouter {
50
71
51
72
// Set instance properties
52
73
this . pagesConfig = pages ;
53
- this . pagesEndpoint = pages . pagesEndpoint
54
- ? pages . pagesEndpoint
55
- : 'apps' ;
74
+ this . pagesEndpoint = pages . pagesEndpoint ? pages . pagesEndpoint : 'apps' ;
56
75
this . pagesPath = pages . pagesPath
57
76
? path . resolve ( './' , pages . pagesPath )
58
77
: path . resolve ( __dirname , '../../public' ) ;
@@ -121,7 +140,7 @@ export class PagesRouter extends PromiseRouter {
121
140
[ pageParams . appName ] : config . appName ,
122
141
[ pageParams . token ] : req . query . token ,
123
142
[ pageParams . username ] : req . query . username ,
124
- [ pageParams . publicServerUrl ] : config . publicServerURL
143
+ [ pageParams . publicServerUrl ] : config . publicServerURL ,
125
144
} ;
126
145
return this . goToPage ( req , pages . passwordReset , params ) ;
127
146
}
@@ -289,7 +308,10 @@ export class PagesRouter extends PromiseRouter {
289
308
if ( config . pages . enableLocalization && locale ) {
290
309
return Utils . getLocalizedPath ( defaultPath , locale ) . then ( ( { path, subdir } ) =>
291
310
redirect
292
- ? this . redirectResponse ( this . composePageUrl ( defaultFile , config . publicServerURL , subdir ) , params )
311
+ ? this . redirectResponse (
312
+ this . composePageUrl ( defaultFile , config . publicServerURL , subdir ) ,
313
+ params
314
+ )
293
315
: this . pageResponse ( path , params , placeholders )
294
316
) ;
295
317
} else {
@@ -356,7 +378,6 @@ export class PagesRouter extends PromiseRouter {
356
378
* translation was found.
357
379
*/
358
380
getJsonTranslation ( locale ) {
359
-
360
381
// If there is no JSON resource
361
382
if ( this . jsonParameters === undefined ) {
362
383
return { } ;
@@ -366,11 +387,12 @@ export class PagesRouter extends PromiseRouter {
366
387
locale = locale || this . pagesConfig . localizationFallbackLocale ;
367
388
368
389
// Get matching translation by locale, language or fallback locale
369
- const language = locale . split ( "-" ) [ 0 ] ;
370
- const resource = this . jsonParameters [ locale ]
371
- || this . jsonParameters [ language ]
372
- || this . jsonParameters [ this . pagesConfig . localizationFallbackLocale ]
373
- || { } ;
390
+ const language = locale . split ( '-' ) [ 0 ] ;
391
+ const resource =
392
+ this . jsonParameters [ locale ] ||
393
+ this . jsonParameters [ language ] ||
394
+ this . jsonParameters [ this . pagesConfig . localizationFallbackLocale ] ||
395
+ { } ;
374
396
const translation = resource . translation || { } ;
375
397
return translation ;
376
398
}
@@ -385,7 +407,6 @@ export class PagesRouter extends PromiseRouter {
385
407
* translation was found.
386
408
*/
387
409
getJsonPlaceholders ( locale , params = { } ) {
388
-
389
410
// If localization is disabled or there is no JSON resource
390
411
if ( ! this . pagesConfig . enableLocalization || ! this . pagesConfig . localizationJsonPath ) {
391
412
return { } ;
@@ -422,11 +443,12 @@ export class PagesRouter extends PromiseRouter {
422
443
}
423
444
424
445
// Get config placeholders; can be an object, a function or an async function
425
- let configPlaceholders = typeof this . pagesConfig . placeholders === 'function'
426
- ? this . pagesConfig . placeholders ( )
427
- : Object . prototype . toString . call ( this . pagesConfig . placeholders ) === '[object Object]'
428
- ? this . pagesConfig . placeholders
429
- : { } ;
446
+ let configPlaceholders =
447
+ typeof this . pagesConfig . placeholders === 'function'
448
+ ? this . pagesConfig . placeholders ( )
449
+ : Object . prototype . toString . call ( this . pagesConfig . placeholders ) === '[object Object]'
450
+ ? this . pagesConfig . placeholders
451
+ : { } ;
430
452
if ( configPlaceholders instanceof Promise ) {
431
453
configPlaceholders = await configPlaceholders ;
432
454
}
@@ -477,7 +499,6 @@ export class PagesRouter extends PromiseRouter {
477
499
* @returns {Promise<String> } The file content.
478
500
*/
479
501
async readFile ( filePath ) {
480
-
481
502
// Normalize path to prevent it from containing any directory changing
482
503
// UNIX patterns which could expose the whole file system, e.g.
483
504
// `http://example.com/parse/apps/../file.txt` requests a file outside
@@ -486,7 +507,7 @@ export class PagesRouter extends PromiseRouter {
486
507
487
508
// Abort if the path is outside of the path directory scope
488
509
if ( ! normalizedPath . startsWith ( this . pagesPath ) ) {
489
- throw ( errors . fileOutsideAllowedScope ) ;
510
+ throw errors . fileOutsideAllowedScope ;
490
511
}
491
512
492
513
return await fs . readFile ( normalizedPath , 'utf-8' ) ;
@@ -503,7 +524,7 @@ export class PagesRouter extends PromiseRouter {
503
524
const json = require ( path . resolve ( './' , this . pagesConfig . localizationJsonPath ) ) ;
504
525
this . jsonParameters = json ;
505
526
} catch ( e ) {
506
- throw ( errors . jsonFailedFileLoading ) ;
527
+ throw errors . jsonFailedFileLoading ;
507
528
}
508
529
}
509
530
@@ -531,10 +552,10 @@ export class PagesRouter extends PromiseRouter {
531
552
*/
532
553
getLocale ( req ) {
533
554
const locale =
534
- ( req . query || { } ) [ pageParams . locale ]
535
- || ( req . body || { } ) [ pageParams . locale ]
536
- || ( req . params || { } ) [ pageParams . locale ]
537
- || ( req . headers || { } ) [ pageParamHeaderPrefix + pageParams . locale ] ;
555
+ ( req . query || { } ) [ pageParams . locale ] ||
556
+ ( req . body || { } ) [ pageParams . locale ] ||
557
+ ( req . params || { } ) [ pageParams . locale ] ||
558
+ ( req . headers || { } ) [ pageParamHeaderPrefix + pageParams . locale ] ;
538
559
return locale ;
539
560
}
540
561
0 commit comments