Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Dec 15, 2024
1 parent f0e6550 commit 3478424
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 76 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## 1.0.0

- Replaced Jest with vitest
- Updated script injector to support `connected` and `disconnected` events
- Updated script injector to support `connected` and `disconnected` events in scripts
- Updated script injector scripts to use `setup` and `teardown` lifecycle
- Updated Node.js constraint to 18.17
- Updated dependencies
- Added `proxyWebSocket` to exported utilities
Expand Down
4 changes: 4 additions & 0 deletions docs/script-injector.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ interface Script {
| KrasAnswer
| Promise<KrasAnswer>
| undefined;
setup?(ctx: ScriptContextData): void;
teardown?(ctx: ScriptContextData): void;
connected?(ctx: ScriptContextData, e: KrasWebSocketEvent): void;
disconnected?(ctx: ScriptContextData, e: KrasWebSocketEvent): void;
}
Expand Down Expand Up @@ -207,3 +209,5 @@ export interface KrasAnswer {
```

This allows also specifying `connected` and `disconnected` functions to handle WebSocket connections.

The `setup` and `teardown` functions are used to properly initialize or dispose relevant resources. They are called when the script is first discovered or removed / replaced, e.g., in case of a file change.
6 changes: 3 additions & 3 deletions src/server/helpers/build-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { basename } from 'path';
import { KrasInjectorOption } from '../types';

export interface FileInfo {
file: string;
path: string;
active: boolean;
error?: string;
}
Expand All @@ -12,7 +12,7 @@ export interface DescribeEntry<T> {
}

function getFile(fileInfo: FileInfo) {
const fileName = fileInfo.file;
const fileName = fileInfo.path;
return {
id: Buffer.from(fileName).toString('base64'),
name: fileName,
Expand All @@ -23,7 +23,7 @@ function getFile(fileInfo: FileInfo) {
}

function getEntry<T extends FileInfo>(fileInfos: Array<T>, desc: DescribeEntry<T>) {
const fileName = fileInfos[0].file;
const fileName = fileInfos[0].path;
return {
id: Buffer.from(fileName).toString('base64'),
name: fileName,
Expand Down
34 changes: 6 additions & 28 deletions src/server/helpers/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,44 +112,22 @@ function installWatcher(
function watchSingle(
directory: string,
extensions: Array<string>,
callback: (type: string, file: string, position: number) => void,
callback: (type: string, file: string) => void,
watched: Array<string>,
): SingleWatcher {
const getPosition = (fn: string) => {
const idx = watched.indexOf(fn);

if (idx === -1) {
let i = 0;

while (i < watched.length) {
const w = watched[i];

if (w.localeCompare(fn) > 0) {
break;
}

i++;
}

watched.splice(i, 0, fn);
return i;
}

return idx;
};
const updateFile = (file: string) => {
const fn = resolve(directory, file);
callback('update', fn, getPosition(fn));
callback('update', fn);
};
const deleteFile = (file: string) => {
const fn = resolve(directory, file);
const idx = watched.indexOf(fn);
idx !== -1 && watched.splice(idx, 1);
callback('delete', fn, -1);
callback('delete', fn);
};
const loadFile = (file: string) => {
const fn = resolve(directory, file);
callback('create', fn, getPosition(fn));
callback('create', fn);
};
const w = installWatcher(directory, extensions, loadFile, updateFile, deleteFile);
return {
Expand All @@ -162,7 +140,7 @@ function watchSingle(
const fn = resolve(directory, dir, file);
const idx = watched.indexOf(fn);
idx !== -1 && watched.splice(idx, 1);
callback('delete', fn, -1);
callback('delete', fn);
}
}

Expand All @@ -174,7 +152,7 @@ function watchSingle(
export function watch(
directory: string | Array<string>,
extensions: Array<string>,
callback: (type: string, file: string, position: number) => void,
callback: (type: string, file: string) => void,
watched: Array<string> = [],
): Watcher {
if (Array.isArray(directory)) {
Expand Down
22 changes: 11 additions & 11 deletions src/server/injectors/har-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ interface HttpArchive {
}

interface HarFileEntry {
file: string;
path: string;
active: boolean;
request: {
method: string;
Expand Down Expand Up @@ -125,11 +125,11 @@ export default class HarInjector implements KrasInjector {
address: config.map[target] as string,
}));

this.watcher = watch(directory, ['.har'], (ev, fileName, position) => {
this.watcher = watch(directory, ['.har'], (ev, fileName) => {
switch (ev) {
case 'create':
case 'update':
return this.load(fileName, position);
return this.load(fileName);
case 'delete':
return this.unload(fileName);
}
Expand All @@ -153,7 +153,7 @@ export default class HarInjector implements KrasInjector {
this.config.delay = options.delay;

for (const { name, entries } of options.files) {
const files = this.files.find((m) => m[0].file === name);
const files = this.files.find((m) => m[0].path === name);

if (entries) {
for (let i = 0; i < entries.length; i++) {
Expand Down Expand Up @@ -183,21 +183,21 @@ export default class HarInjector implements KrasInjector {
}

private unload(fileName: string) {
const index = this.files.findIndex((m) => m[0].file === fileName);
const index = this.files.findIndex((m) => m[0].path === fileName);

if (index !== -1) {
this.files.splice(index, 1);
}
}

private load(fileName: string, position: number) {
private load(fileName: string) {
const content = asJson(fileName, undefined);
const entries = findEntries(content);
const files = entries.map((entry) => this.transformEntry(fileName, entry));
this.unload(fileName);

if (files.length > 0) {
this.files.splice(position, 0, files);
this.files.push(files);
}
}

Expand All @@ -211,7 +211,7 @@ export default class HarInjector implements KrasInjector {
return undefined;
}

private transformEntry(file: string, entry: HttpArchive) {
private transformEntry(path: string, entry: HttpArchive) {
const original = entry.request;
const response = entry.response;
const content = (original.postData || {}).text || '';
Expand All @@ -228,7 +228,7 @@ export default class HarInjector implements KrasInjector {
delete request.headers._;

return {
file,
path,
active: true,
time: entry.time,
request,
Expand All @@ -244,7 +244,7 @@ export default class HarInjector implements KrasInjector {
let i = 0;

for (const files of this.files) {
for (const { file, active, time, request, response } of files) {
for (const { path, active, time, request, response } of files) {
if (active) {
const name = this.name;

Expand All @@ -253,7 +253,7 @@ export default class HarInjector implements KrasInjector {
fromHar(request.url, response, {
name,
file: {
name: file,
name: path,
entry: i,
},
}),
Expand Down
18 changes: 9 additions & 9 deletions src/server/injectors/json-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function find(response: KrasAnswer | Array<KrasAnswer>, randomize: boolean) {
}

interface JsonFileItem {
file: string;
path: string;
active: boolean;
request: KrasRequest;
response: KrasAnswer | Array<KrasAnswer>;
Expand Down Expand Up @@ -68,11 +68,11 @@ export default class JsonInjector implements KrasInjector {
const directory = options.directory || config.sources || config.directory;
this.config = options;

this.watcher = watch(directory, ['.json'], (ev, fileName, position) => {
this.watcher = watch(directory, ['.json'], (ev, fileName) => {
switch (ev) {
case 'create':
case 'update':
return this.load(fileName, position);
return this.load(fileName);
case 'delete':
return this.unload(fileName);
}
Expand All @@ -96,7 +96,7 @@ export default class JsonInjector implements KrasInjector {
this.config.randomize = options.randomize;

for (const { name, entries } of options.files) {
const files = this.files.find((m) => m[0].file === name);
const files = this.files.find((m) => m[0].path === name);

if (entries) {
for (let i = 0; i < entries.length; i++) {
Expand Down Expand Up @@ -126,14 +126,14 @@ export default class JsonInjector implements KrasInjector {
}

private unload(fileName: string) {
const index = this.files.findIndex((m) => m[0].file === fileName);
const index = this.files.findIndex((m) => m[0].path === fileName);

if (index !== -1) {
this.files.splice(index, 1);
}
}

private load(fileName: string, position: number) {
private load(fileName: string) {
const content = asJson(fileName, []);
const items = Array.isArray(content) ? content : [content];

Expand All @@ -153,7 +153,7 @@ export default class JsonInjector implements KrasInjector {
this.unload(fileName);

if (items.length > 0) {
this.files.splice(position, 0, items);
this.files.push(items);
}
}

Expand Down Expand Up @@ -198,7 +198,7 @@ export default class JsonInjector implements KrasInjector {
let i = 0;

for (const files of this.files) {
for (const { file, active, request, response } of files) {
for (const { path, active, request, response } of files) {
if (active) {
if (compareRequests(request, req)) {
const rand = this.config.randomize;
Expand All @@ -209,7 +209,7 @@ export default class JsonInjector implements KrasInjector {
return fromJson(request.url, res.status.code, res.status.text, res.headers, content, {
name,
file: {
name: file,
name: path,
entry: i,
},
});
Expand Down
Loading

0 comments on commit 3478424

Please sign in to comment.