Skip to content

Commit 3478424

Browse files
committed
Refactored
1 parent f0e6550 commit 3478424

File tree

7 files changed

+85
-76
lines changed

7 files changed

+85
-76
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
## 1.0.0
44

55
- Replaced Jest with vitest
6-
- Updated script injector to support `connected` and `disconnected` events
6+
- Updated script injector to support `connected` and `disconnected` events in scripts
7+
- Updated script injector scripts to use `setup` and `teardown` lifecycle
78
- Updated Node.js constraint to 18.17
89
- Updated dependencies
910
- Added `proxyWebSocket` to exported utilities

docs/script-injector.md

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ interface Script {
7171
| KrasAnswer
7272
| Promise<KrasAnswer>
7373
| undefined;
74+
setup?(ctx: ScriptContextData): void;
75+
teardown?(ctx: ScriptContextData): void;
7476
connected?(ctx: ScriptContextData, e: KrasWebSocketEvent): void;
7577
disconnected?(ctx: ScriptContextData, e: KrasWebSocketEvent): void;
7678
}
@@ -207,3 +209,5 @@ export interface KrasAnswer {
207209
```
208210

209211
This allows also specifying `connected` and `disconnected` functions to handle WebSocket connections.
212+
213+
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.

src/server/helpers/build-options.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { basename } from 'path';
22
import { KrasInjectorOption } from '../types';
33

44
export interface FileInfo {
5-
file: string;
5+
path: string;
66
active: boolean;
77
error?: string;
88
}
@@ -12,7 +12,7 @@ export interface DescribeEntry<T> {
1212
}
1313

1414
function getFile(fileInfo: FileInfo) {
15-
const fileName = fileInfo.file;
15+
const fileName = fileInfo.path;
1616
return {
1717
id: Buffer.from(fileName).toString('base64'),
1818
name: fileName,
@@ -23,7 +23,7 @@ function getFile(fileInfo: FileInfo) {
2323
}
2424

2525
function getEntry<T extends FileInfo>(fileInfos: Array<T>, desc: DescribeEntry<T>) {
26-
const fileName = fileInfos[0].file;
26+
const fileName = fileInfos[0].path;
2727
return {
2828
id: Buffer.from(fileName).toString('base64'),
2929
name: fileName,

src/server/helpers/io.ts

+6-28
Original file line numberDiff line numberDiff line change
@@ -112,44 +112,22 @@ function installWatcher(
112112
function watchSingle(
113113
directory: string,
114114
extensions: Array<string>,
115-
callback: (type: string, file: string, position: number) => void,
115+
callback: (type: string, file: string) => void,
116116
watched: Array<string>,
117117
): SingleWatcher {
118-
const getPosition = (fn: string) => {
119-
const idx = watched.indexOf(fn);
120-
121-
if (idx === -1) {
122-
let i = 0;
123-
124-
while (i < watched.length) {
125-
const w = watched[i];
126-
127-
if (w.localeCompare(fn) > 0) {
128-
break;
129-
}
130-
131-
i++;
132-
}
133-
134-
watched.splice(i, 0, fn);
135-
return i;
136-
}
137-
138-
return idx;
139-
};
140118
const updateFile = (file: string) => {
141119
const fn = resolve(directory, file);
142-
callback('update', fn, getPosition(fn));
120+
callback('update', fn);
143121
};
144122
const deleteFile = (file: string) => {
145123
const fn = resolve(directory, file);
146124
const idx = watched.indexOf(fn);
147125
idx !== -1 && watched.splice(idx, 1);
148-
callback('delete', fn, -1);
126+
callback('delete', fn);
149127
};
150128
const loadFile = (file: string) => {
151129
const fn = resolve(directory, file);
152-
callback('create', fn, getPosition(fn));
130+
callback('create', fn);
153131
};
154132
const w = installWatcher(directory, extensions, loadFile, updateFile, deleteFile);
155133
return {
@@ -162,7 +140,7 @@ function watchSingle(
162140
const fn = resolve(directory, dir, file);
163141
const idx = watched.indexOf(fn);
164142
idx !== -1 && watched.splice(idx, 1);
165-
callback('delete', fn, -1);
143+
callback('delete', fn);
166144
}
167145
}
168146

@@ -174,7 +152,7 @@ function watchSingle(
174152
export function watch(
175153
directory: string | Array<string>,
176154
extensions: Array<string>,
177-
callback: (type: string, file: string, position: number) => void,
155+
callback: (type: string, file: string) => void,
178156
watched: Array<string> = [],
179157
): Watcher {
180158
if (Array.isArray(directory)) {

src/server/injectors/har-injector.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ interface HttpArchive {
7171
}
7272

7373
interface HarFileEntry {
74-
file: string;
74+
path: string;
7575
active: boolean;
7676
request: {
7777
method: string;
@@ -125,11 +125,11 @@ export default class HarInjector implements KrasInjector {
125125
address: config.map[target] as string,
126126
}));
127127

128-
this.watcher = watch(directory, ['.har'], (ev, fileName, position) => {
128+
this.watcher = watch(directory, ['.har'], (ev, fileName) => {
129129
switch (ev) {
130130
case 'create':
131131
case 'update':
132-
return this.load(fileName, position);
132+
return this.load(fileName);
133133
case 'delete':
134134
return this.unload(fileName);
135135
}
@@ -153,7 +153,7 @@ export default class HarInjector implements KrasInjector {
153153
this.config.delay = options.delay;
154154

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

158158
if (entries) {
159159
for (let i = 0; i < entries.length; i++) {
@@ -183,21 +183,21 @@ export default class HarInjector implements KrasInjector {
183183
}
184184

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

188188
if (index !== -1) {
189189
this.files.splice(index, 1);
190190
}
191191
}
192192

193-
private load(fileName: string, position: number) {
193+
private load(fileName: string) {
194194
const content = asJson(fileName, undefined);
195195
const entries = findEntries(content);
196196
const files = entries.map((entry) => this.transformEntry(fileName, entry));
197197
this.unload(fileName);
198198

199199
if (files.length > 0) {
200-
this.files.splice(position, 0, files);
200+
this.files.push(files);
201201
}
202202
}
203203

@@ -211,7 +211,7 @@ export default class HarInjector implements KrasInjector {
211211
return undefined;
212212
}
213213

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

230230
return {
231-
file,
231+
path,
232232
active: true,
233233
time: entry.time,
234234
request,
@@ -244,7 +244,7 @@ export default class HarInjector implements KrasInjector {
244244
let i = 0;
245245

246246
for (const files of this.files) {
247-
for (const { file, active, time, request, response } of files) {
247+
for (const { path, active, time, request, response } of files) {
248248
if (active) {
249249
const name = this.name;
250250

@@ -253,7 +253,7 @@ export default class HarInjector implements KrasInjector {
253253
fromHar(request.url, response, {
254254
name,
255255
file: {
256-
name: file,
256+
name: path,
257257
entry: i,
258258
},
259259
}),

src/server/injectors/json-injector.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function find(response: KrasAnswer | Array<KrasAnswer>, randomize: boolean) {
3232
}
3333

3434
interface JsonFileItem {
35-
file: string;
35+
path: string;
3636
active: boolean;
3737
request: KrasRequest;
3838
response: KrasAnswer | Array<KrasAnswer>;
@@ -68,11 +68,11 @@ export default class JsonInjector implements KrasInjector {
6868
const directory = options.directory || config.sources || config.directory;
6969
this.config = options;
7070

71-
this.watcher = watch(directory, ['.json'], (ev, fileName, position) => {
71+
this.watcher = watch(directory, ['.json'], (ev, fileName) => {
7272
switch (ev) {
7373
case 'create':
7474
case 'update':
75-
return this.load(fileName, position);
75+
return this.load(fileName);
7676
case 'delete':
7777
return this.unload(fileName);
7878
}
@@ -96,7 +96,7 @@ export default class JsonInjector implements KrasInjector {
9696
this.config.randomize = options.randomize;
9797

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

101101
if (entries) {
102102
for (let i = 0; i < entries.length; i++) {
@@ -126,14 +126,14 @@ export default class JsonInjector implements KrasInjector {
126126
}
127127

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

131131
if (index !== -1) {
132132
this.files.splice(index, 1);
133133
}
134134
}
135135

136-
private load(fileName: string, position: number) {
136+
private load(fileName: string) {
137137
const content = asJson(fileName, []);
138138
const items = Array.isArray(content) ? content : [content];
139139

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

155155
if (items.length > 0) {
156-
this.files.splice(position, 0, items);
156+
this.files.push(items);
157157
}
158158
}
159159

@@ -198,7 +198,7 @@ export default class JsonInjector implements KrasInjector {
198198
let i = 0;
199199

200200
for (const files of this.files) {
201-
for (const { file, active, request, response } of files) {
201+
for (const { path, active, request, response } of files) {
202202
if (active) {
203203
if (compareRequests(request, req)) {
204204
const rand = this.config.randomize;
@@ -209,7 +209,7 @@ export default class JsonInjector implements KrasInjector {
209209
return fromJson(request.url, res.status.code, res.status.text, res.headers, content, {
210210
name,
211211
file: {
212-
name: file,
212+
name: path,
213213
entry: i,
214214
},
215215
});

0 commit comments

Comments
 (0)