-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
63 lines (57 loc) · 1.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import Server from './src/server';
import Mockingjay from './src/mockingjay';
import Rehasher from './src/rehasher';
import DefaultOptions from './src/default_options';
let Mockingjays = function () {};
/**
* Start creates a Mockingjays server and starts proxying
* or mocking responses based on previous training.
*
* @param options - An {Object} with options
* :cacheDir - Path to cache storage.
* :port - Port that Mockingjays should bind to.
* :serverBaseUrl - Base URL for the source server.
* @param onReady - A {Function} to be called when the proxy is ready. [Optional]
*/
Mockingjays.prototype.start = function (options, onReady) {
let defaultOptions = new DefaultOptions();
let finalOptions = defaultOptions.merge(options);
let mockingjay = new Mockingjay(finalOptions);
let requestHandler = function (req, res) {
mockingjay.onRequest(req, res);
};
this.server = Server.listen(finalOptions, requestHandler, onReady);
return this;
};
/**
* Stop a an Mockingjays server.
*
* @param done - A {Function} to be called when the server has closed. [Optional]
*/
Mockingjays.prototype.stop = function (done) {
this.server.forceShutdown(done || function () {});
};
/**
* @deprecated in favor of .stop
*
* Stop a an Mockingjays server.
*
* @param done - A {Function} to be called when the server has closed. [Optional]
*/
Mockingjays.prototype.close = function (done) {
this.server.forceShutdown(done || function () {});
};
/**
* Processes the existing cache with a new set of options.
*
* @param options - An {Object} with options
* :cacheDir - Path to cache storage.
* :port - Port that Mockingjays should bind to.
* :serverBaseUrl - Base URL for the source server.
*/
Mockingjays.prototype.rehash = function (options) {
let defaultOptions = new DefaultOptions();
let finalOptions = defaultOptions.merge(options, { attemptToCreateCacheDir: false });
new Rehasher(finalOptions).process();
};
export default Mockingjays;