Skip to content

Commit

Permalink
Prepare for next release
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Mar 7, 2023
1 parent 736ca19 commit 528af17
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.15.3

- Added possibility to specify multiple headers or query parameters
- Updated dependencies

## 0.15.2
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"tslint": "^5.20.1",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.3.0",
"typescript": "^3.9.10",
"typescript": "^4.9.5",
"uuid": "^8.3.2",
"ws": "^7.5.9"
}
Expand Down
22 changes: 2 additions & 20 deletions src/server/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as chalk from 'chalk';
import { resolve, dirname } from 'path';
import { existsSync, readFileSync } from 'fs';
import { name, version, currentDir } from '../info';
import { deepMerge } from '../helpers';
import { Dict, KrasConfiguration, LogLevel } from '../types';
import * as chalk from 'chalk';

export interface ConfigurationOptions {
name?: string;
Expand Down Expand Up @@ -90,25 +91,6 @@ export function readConfiguration(path: string): ConfigurationFile {
return {};
}

function deepMerge(obj: any, value: any) {
Object.keys(value).forEach((key) => {
const oldItem = obj[key];
const newItem = value[key];

if (newItem === undefined) {
delete obj[key];
} else if (Array.isArray(oldItem) && Array.isArray(newItem)) {
obj[key] = [...oldItem, ...newItem] as any;
} else if (typeof oldItem === 'object') {
obj[key] = deepMerge({ ...oldItem }, newItem);
} else {
obj[key] = newItem;
}
});

return obj;
}

function mergeObjects<T>(
sources: Array<Partial<KrasConfiguration>>,
select: (config: Partial<KrasConfiguration>) => Dict<T>,
Expand Down
35 changes: 35 additions & 0 deletions src/server/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,38 @@ export function filterReverse<T>(

return dest;
}

export function deepMerge(obj: any, value: any) {
Object.keys(value).forEach((key) => {
const oldItem = obj[key];
const newItem = value[key];

if (newItem === undefined) {
delete obj[key];
} else if (Array.isArray(oldItem) && Array.isArray(newItem)) {
obj[key] = [...oldItem, ...newItem] as any;
} else if (typeof oldItem === 'object') {
obj[key] = deepMerge({ ...oldItem }, newItem);
} else {
obj[key] = newItem;
}
});

return obj;
}

export function getLast(value: string | Array<string>) {
if (Array.isArray(value)) {
return value[value.length - 1];
}

return value;
}

export function getFirst(value: string | Array<string>) {
if (Array.isArray(value)) {
return value[0];
}

return value;
}
34 changes: 15 additions & 19 deletions src/server/injectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { resolve, basename } from 'path';
import { EventEmitter } from 'events';
import { Request, Response } from 'express';
import { parse } from 'url';
import { fromMissing, isEncrypted, getPort } from '../helpers';
import { fromMissing, isEncrypted, getPort, deepMerge, getLast } from '../helpers';
import { injectorDebug, injectorConfig, injectorMain } from '../info';
import { KrasConfiguration, KrasServer, KrasAnswer, KrasInjector, KrasInjectorConfig, KrasRequest } from '../types';

Expand All @@ -15,6 +15,7 @@ import ScriptInjector from './script-injector';
import StoreInjector from './store-injector';

const specialHeaders = ['origin', 'content-type'];
const multipleHeaders = ['set-cookie'];

const coreInjectors: Record<string, any> = {
har: HarInjector,
Expand All @@ -27,11 +28,19 @@ const coreInjectors: Record<string, any> = {
function sendResponse(req: KrasRequest, ans: KrasAnswer, res: Response) {
if (!ans.redirectUrl) {
const origin = req.headers.origin;
const type = ans.headers['content-type'];
const type = getLast(ans.headers['content-type']);

for (const headerName of Object.keys(ans.headers)) {
if (specialHeaders.indexOf(headerName) === -1) {
res.setHeader(headerName, ans.headers[headerName]);
const value = ans.headers[headerName];

if (Array.isArray(value) && multipleHeaders.includes(headerName)) {
for (const item of value) {
res.setHeader(headerName, item);
}
} else {
res.setHeader(headerName, value);
}
}
}

Expand Down Expand Up @@ -70,20 +79,9 @@ function getTarget(targets: Array<string>, url: string) {
function normalizeRequest(targets: Array<string>, req: Request): KrasRequest {
const target = getTarget(targets, req.originalUrl) || '';
const url = req.originalUrl.substring(target.length);

const query = Object.assign(
{
...req.addedQuery,
},
req.query,
) as Record<string, string>;

const headers = Object.assign(
{
...req.addedHeaders,
},
req.headers,
) as Record<string, string>;
const headers: Record<string, string | Array<string>> = deepMerge({ ...req.headers }, req.addedHeaders);
const query: Record<string, string | Array<string>> = deepMerge({ ...req.query }, req.addedQuery);
const method = typeof req.method === 'string' ? req.method : 'GET';

let content: any;

Expand All @@ -106,8 +104,6 @@ function normalizeRequest(targets: Array<string>, req: Request): KrasRequest {
content = typeof req.body === 'string' ? req.body : '';
}

const method = typeof req.method === 'string' ? req.method : 'GET';

for (const name of req.removedHeaders) {
delete headers[name];
}
Expand Down
22 changes: 19 additions & 3 deletions src/server/injectors/json-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import * as cookie from 'cookie';
import * as parser from 'accept-language-parser';
import fakerLocale from '../helpers/faker-locale';
import { generateFromTemplate } from '../helpers/generate-from-template';
import { asJson, watch, Watcher, editDirectoryOption, editEntryOption, fromJson, compareRequests } from '../helpers';
import {
asJson,
watch,
Watcher,
editDirectoryOption,
editEntryOption,
fromJson,
compareRequests,
getFirst,
} from '../helpers';
import {
KrasInjectorConfig,
KrasAnswer,
Expand Down Expand Up @@ -158,20 +167,27 @@ export default class JsonInjector implements KrasInjector {
const cookies = cookie.parse(req.headers.cookie || '');
const acceptLanguage = parser.parse(req.headers['accept-language']);
let locale = 'en';

if (req.query[localeName]) {
locale = req.query[localeName];
locale = getFirst(req.query[localeName]);
} else if (cookies[localeName]) {
locale = cookies[localeName];
} else if (acceptLanguage.length && acceptLanguage[0].code) {
locale = acceptLanguage[0].code;
}

// Convert like: en, en-US to en_US
faker.setLocale(fakerLocale(locale) || locale);

// Ignore Buffer content
if (Buffer.isBuffer(content)) return content;
if (Buffer.isBuffer(content)) {
return content;
}

if (typeof content === 'string') {
content = JSON.parse(content);
}

const templateJson = generateFromTemplate(content);
return JSON.stringify(templateJson);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/types/kras-basics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ export interface LogEntry {
data: any;
}

export type Headers = Dict<string>;
export type Headers = Dict<string | Array<string>>;
4 changes: 2 additions & 2 deletions src/server/types/kras-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { EventEmitter } from 'events';
declare global {
namespace Express {
interface Request {
addedHeaders: Record<string, string>;
addedHeaders: Record<string, string | Array<string>>;
removedHeaders: Array<string>;
addedQuery: Record<string, string>;
addedQuery: Record<string, string | Array<string>>;
removedQuery: Array<string>;
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/types/kras-request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IncomingHttpHeaders } from 'http';

export interface KrasRequestQuery {
[key: string]: string;
[key: string]: string | Array<string>;
}

export interface BasicKrasRequest {
Expand Down

0 comments on commit 528af17

Please sign in to comment.