Skip to content

Commit

Permalink
fixing import logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Idrinth committed May 18, 2024
1 parent 4c35e9f commit 55d4cf3
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 288 deletions.
74 changes: 37 additions & 37 deletions src/middlewares/access-token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
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 HashMap from '../helper/hashmap.js';
Expand All @@ -19,44 +22,41 @@ const get = (
return fallback;
};

class Access implements Middleware {
public prepare(request: Request,): Request {
const access = store.get('access', '',);
const refresh = store.get('refresh', '',);
if (access) {
if (typeof request.body === 'string') {
request.body = request.body.replace(
/%refresh-token-middleware%/ug,
refresh,
);
request.body = request.body.replace(
/%access-token-middleware%/ug,
access,
);
}
if (typeof request.headers === 'undefined') {
request.headers = {};
}
request.headers.authorization = `Bearer ${ access }`;
export const prepare: prepareType = (request: Request,): Request => {
const access = store.get('access', '',);
const refresh = store.get('refresh', '',);
if (access) {
if (typeof request.body === 'string') {
request.body = request.body.replace(
/%refresh-token-middleware%/ug,
refresh,
);
request.body = request.body.replace(
/%access-token-middleware%/ug,
access,
);
}
return request;
if (typeof request.headers === 'undefined') {
request.headers = {};
}
request.headers.authorization = `Bearer ${ access }`;
}
return request;
}

Check failure on line 45 in src/middlewares/access-token.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

public process(response: Result,): void {
if (typeof response.response.headers === 'undefined') {
return;
}
const contentType = response.response.headers['content-type'];
if (! jsonCheck.test(contentType,)) {
return;
}
const body = JSON.parse(response.response.body,);
let access = store.get('access', '',);
let refresh = store.get('refresh', '',);
access = get(access, body, 'access', 'access_token', 'access-token',);
refresh = get(refresh, body, 'refresh', 'refresh_token', 'refresh-token',);
store.set('access', access,);
store.set('refresh', refresh,);
export const process: processType = (response: Result,): void => {
if (typeof response.response.headers === 'undefined') {
return;
}
const contentType = response.response.headers['content-type'];
if (! jsonCheck.test(contentType,)) {
return;
}
const body = JSON.parse(response.response.body,);
let access = store.get('access', '',);
let refresh = store.get('refresh', '',);
access = get(access, body, 'access', 'access_token', 'access-token',);
refresh = get(refresh, body, 'refresh', 'refresh_token', 'refresh-token',);
store.set('access', access,);
store.set('refresh', refresh,);
}

Check failure on line 62 in src/middlewares/access-token.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
export default Access;
46 changes: 23 additions & 23 deletions src/middlewares/cookie.ts
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;
}

Check failure on line 20 in src/middlewares/cookie.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

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,),);
}

Check failure on line 33 in src/middlewares/cookie.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
export default Cookie;
40 changes: 20 additions & 20 deletions src/middlewares/csrf-header.ts
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;
}

Check failure on line 20 in src/middlewares/csrf-header.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

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'],);
}
}

Check failure on line 29 in src/middlewares/csrf-header.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
export default CsrfHeader;
30 changes: 12 additions & 18 deletions src/middlewares/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Middleware from './middleware.js';
import {
prepare as prepareType

Check warning on line 2 in src/middlewares/encoding.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
} from './middleware.js';
import Request from '../routes/request.js';
import formUrlEncoded from 'form-urlencoded';
import Result from '../messaging/result.js';

const handleForm = (request: Request,): Request => {
if (! request.headers['content-type']) {
Expand All @@ -18,22 +19,15 @@ const handleJSON = (request: Request,): Request => {
return request;
};

class Encoding implements Middleware {
public prepare(request: Request,): Request {
if (typeof request.headers === 'undefined') {
request.headers = {};
}
if (request.autohandle === 'json') {
return handleJSON(request,);
}
if (request.autohandle === 'form' && typeof request.body === 'object') {
return handleForm(request,);
}
return request;
export const prepare: prepareType = (request: Request,): Request => {
if (typeof request.headers === 'undefined') {
request.headers = {};
}

public process(response: Result,): void {
//no task here
if (request.autohandle === 'json') {
return handleJSON(request,);
}
if (request.autohandle === 'form' && typeof request.body === 'object') {
return handleForm(request,);
}
return request;
}

Check failure on line 33 in src/middlewares/encoding.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
export default Encoding;
41 changes: 18 additions & 23 deletions src/middlewares/failure-check.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import Middleware from './middleware.js';
import {
process as processType

Check warning on line 2 in src/middlewares/failure-check.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
} 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 }`,),
);
}
}

Check failure on line 26 in src/middlewares/failure-check.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
33 changes: 14 additions & 19 deletions src/middlewares/json-validator.ts
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 }`,),);
}
}

Check failure on line 22 in src/middlewares/json-validator.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
21 changes: 8 additions & 13 deletions src/middlewares/max-time.ts
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 }`,),);
}
}

Check failure on line 14 in src/middlewares/max-time.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
8 changes: 2 additions & 6 deletions src/middlewares/middleware.ts
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;
21 changes: 8 additions & 13 deletions src/middlewares/silent-server-validator.ts
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

Check warning on line 2 in src/middlewares/silent-server-validator.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
} 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',),);
}
}
Loading

0 comments on commit 55d4cf3

Please sign in to comment.