generated from idrinth-api-bench/project-defaults
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
237 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import Middleware from './middleware.js'; | ||
import { | ||
process as processType, | ||
prepare as prepareType, | ||
} from './middleware.js'; | ||
import Request from '../routes/request.js'; | ||
import Result from '../messaging/result.js'; | ||
import store from '../store/store.js'; | ||
|
||
class Cookie implements Middleware { | ||
public prepare(request: Request,): Request { | ||
if (typeof request.cookies === 'undefined') { | ||
request.cookies = {}; | ||
} | ||
const jar = JSON.parse(store.get('cookie', '{}',),); | ||
for (const cookie in jar) { | ||
if (typeof jar[cookie] === 'string') { | ||
request.cookies[cookie] = request.cookies[cookie] || jar[cookie]; | ||
} | ||
export const prepare: prepareType = (request: Request,): Request => { | ||
if (typeof request.cookies === 'undefined') { | ||
request.cookies = {}; | ||
} | ||
const jar = JSON.parse(store.get('cookie', '{}',),); | ||
for (const cookie in jar) { | ||
if (typeof jar[cookie] === 'string') { | ||
request.cookies[cookie] = request.cookies[cookie] || jar[cookie]; | ||
} | ||
return request; | ||
} | ||
return request; | ||
} | ||
|
||
public process(response: Result,): void { | ||
if (typeof response.response.cookies === 'undefined') { | ||
return; | ||
} | ||
const jar = JSON.parse(store.get('cookie', '{}',),); | ||
for (const cookie in response.response.cookies) { | ||
if (typeof response.response.cookies[cookie] === 'string') { | ||
jar[cookie] = response.response.cookies[cookie]; | ||
} | ||
export const process: processType = (response: Result,): void => { | ||
if (typeof response.response.cookies === 'undefined') { | ||
return; | ||
} | ||
const jar = JSON.parse(store.get('cookie', '{}',),); | ||
for (const cookie in response.response.cookies) { | ||
if (typeof response.response.cookies[cookie] === 'string') { | ||
jar[cookie] = response.response.cookies[cookie]; | ||
} | ||
store.set('cookie', JSON.stringify(jar,),); | ||
} | ||
store.set('cookie', JSON.stringify(jar,),); | ||
} | ||
export default Cookie; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
import Middleware from './middleware.js'; | ||
import { | ||
process as processType, | ||
prepare as prepareType, | ||
} from './middleware.js'; | ||
import Request from '../routes/request.js'; | ||
import Result from '../messaging/result.js'; | ||
import store from '../store/store.js'; | ||
|
||
class CsrfHeader implements Middleware { | ||
public prepare(request: Request,): Request { | ||
const csrf = store.get('csrf', '',); | ||
if (csrf) { | ||
if (typeof request.headers === 'undefined') { | ||
request.headers = {}; | ||
} | ||
if (! request.headers['x-csrf-token']) { | ||
request.headers['x-csrf-token'] = csrf; | ||
} | ||
export const prepare: prepareType = (request: Request,): Request => { | ||
const csrf = store.get('csrf', '',); | ||
if (csrf) { | ||
if (typeof request.headers === 'undefined') { | ||
request.headers = {}; | ||
} | ||
if (! request.headers['x-csrf-token']) { | ||
request.headers['x-csrf-token'] = csrf; | ||
} | ||
return request; | ||
} | ||
return request; | ||
} | ||
|
||
public process(response: Result,): void { | ||
if (typeof response.response.headers === 'undefined') { | ||
return; | ||
} | ||
if (response.response.headers['x-csrf-token']) { | ||
store.set('csrf', response.response.headers['x-csrf-token'],); | ||
} | ||
export const process: processType = (response: Result,): void => { | ||
if (typeof response.response.headers === 'undefined') { | ||
return; | ||
} | ||
if (response.response.headers['x-csrf-token']) { | ||
store.set('csrf', response.response.headers['x-csrf-token'],); | ||
} | ||
} | ||
export default CsrfHeader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,26 @@ | ||
import Middleware from './middleware.js'; | ||
import { | ||
process as processType | ||
} from './middleware.js'; | ||
import Result from '../messaging/result.js'; | ||
import language from '../helper/language.js'; | ||
import StandardResponse from '../helper/standard-response.js'; | ||
import Request from '../routes/request.js'; | ||
|
||
export default class FailureCheck implements Middleware { | ||
public prepare(request: Request,): Request { | ||
return request; | ||
export const process: processType = (result: Result,): void => { | ||
let response: StandardResponse; | ||
try { | ||
response = JSON.parse(result.response.body,); | ||
} catch (e) { | ||
throw Error(language('invalid_json_body', `${ e }`,),); | ||
} | ||
if (response.status === 'success') { | ||
throw new Error( | ||
language('response_not_failure', 'status', `${ response.status }`,), | ||
); | ||
} | ||
|
||
public process(result: Result,): void { | ||
let response: StandardResponse; | ||
try { | ||
response = JSON.parse(result.response.body,); | ||
} catch (e) { | ||
throw Error(language('invalid_json_body', `${ e }`,),); | ||
} | ||
if (response.status === 'success') { | ||
throw new Error( | ||
language('response_not_failure', 'status', `${ response.status }`,), | ||
); | ||
} | ||
|
||
if (response.success === true) { | ||
throw new Error( | ||
language('response_not_failure', 'success', `${ response.success }`,), | ||
); | ||
} | ||
if (response.success === true) { | ||
throw new Error( | ||
language('response_not_failure', 'success', `${ response.success }`,), | ||
); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,22 @@ | ||
import Middleware from './middleware.js'; | ||
import Request from '../routes/request.js'; | ||
import { | ||
process as processType, | ||
} from './middleware.js'; | ||
import Result from '../messaging/result.js'; | ||
import language from '../helper/language.js'; | ||
|
||
const jsonCheck = /^application\/json/ui; | ||
|
||
export default class JsonValidator implements Middleware { | ||
public prepare(request: Request,): Request { | ||
return request; | ||
export const process: processType = (result: Result,): void => { | ||
if (typeof result.response.headers['content-type'] === 'undefined') { | ||
throw Error(language('no_content_type',),); | ||
} | ||
|
||
public process(result: Result,): void { | ||
if (typeof result.response.headers['content-type'] === 'undefined') { | ||
throw Error(language('no_content_type',),); | ||
} | ||
const contentType = result.response.headers['content-type']; | ||
if (! jsonCheck.test(contentType,)) { | ||
throw Error(language('no_json_content_type', contentType,),); | ||
} | ||
try { | ||
JSON.parse(result.response.body,); | ||
} catch (e) { | ||
throw Error(language('invalid_json_body', `${ e }`,),); | ||
} | ||
const contentType = result.response.headers['content-type']; | ||
if (! jsonCheck.test(contentType,)) { | ||
throw Error(language('no_json_content_type', contentType,),); | ||
} | ||
try { | ||
JSON.parse(result.response.body,); | ||
} catch (e) { | ||
throw Error(language('invalid_json_body', `${ e }`,),); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import Middleware from './middleware.js'; | ||
import Request from '../routes/request.js'; | ||
import { | ||
process as processType, | ||
} from './middleware.js'; | ||
import Result from '../messaging/result.js'; | ||
import language from '../helper/language.js'; | ||
|
||
export default class MaxTime implements Middleware { | ||
public prepare(request: Request,): Request { | ||
return request; | ||
export const process: processType = (result: Result,): void => { | ||
if (typeof result.maxDuration !== 'number') { | ||
return; | ||
} | ||
|
||
public process(result: Result,): void { | ||
if (typeof result.maxDuration !== 'number') { | ||
return; | ||
} | ||
if (result.duration > result.maxDuration) { | ||
throw new Error(language('too_slow', `${ result.maxDuration }`,),); | ||
} | ||
if (result.duration > result.maxDuration) { | ||
throw new Error(language('too_slow', `${ result.maxDuration }`,),); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import Request from '../routes/request.js'; | ||
import Result from '../messaging/result.js'; | ||
|
||
export interface Middleware { | ||
process(response: Result): void; | ||
prepare(request: Request): Request; | ||
} | ||
|
||
export default Middleware; | ||
export type process = (response: Result) => void; | ||
export type prepare = (request: Request) => Request; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import Middleware from './middleware.js'; | ||
import Request from '../routes/request.js'; | ||
import { | ||
process as processType | ||
} from './middleware.js'; | ||
import Result from '../messaging/result.js'; | ||
import language from '../helper/language.js'; | ||
|
||
export default class SilentServerValidator implements Middleware { | ||
public prepare(request: Request,): Request { | ||
return request; | ||
export const process: processType = (result: Result,): void => { | ||
if (typeof result.response.headers.Server !== 'undefined') { | ||
throw Error(language('server_header_is_set',),); | ||
} | ||
|
||
public process(result: Result,): void { | ||
if (typeof result.response.headers.Server !== 'undefined') { | ||
throw Error(language('server_header_is_set',),); | ||
} | ||
if (typeof result.response.headers['X-Powered-By'] !== 'undefined') { | ||
throw Error(language('powered_by_is_set',),); | ||
} | ||
if (typeof result.response.headers['X-Powered-By'] !== 'undefined') { | ||
throw Error(language('powered_by_is_set',),); | ||
} | ||
} |
Oops, something went wrong.