@momsfriendlydevco/cowboy 1.4.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cowboy.js +43 -6
- package/lib/testkit.js +5 -0
- package/package.json +1 -1
package/lib/cowboy.js
CHANGED
|
@@ -25,10 +25,11 @@ export class Cowboy {
|
|
|
25
25
|
* General settings for this Cowboy instance
|
|
26
26
|
*
|
|
27
27
|
* @type {Object}
|
|
28
|
+
* @property {Boolean|'VAR'} scheduler Add a generic `/__scheduled` endpoint to execute any queued scheduler. If value is `'VAR'` this will only apply if the env `COWBOY_SCHEDULER` is set and truthy
|
|
28
29
|
* @property {Function} pathTidy Additional tidyup for server request paths, useful if the API does not live at the server root. Defaults to removing a "/api/:worker/" prefix
|
|
29
30
|
*/
|
|
30
31
|
settings = {
|
|
31
|
-
|
|
32
|
+
scheduler: 'VAR',
|
|
32
33
|
pathTidy(path) {
|
|
33
34
|
return path
|
|
34
35
|
.replace(/^\/api\/\w+/, '/')
|
|
@@ -62,7 +63,7 @@ export class Cowboy {
|
|
|
62
63
|
* Queue up a middleware path
|
|
63
64
|
* All given middleware is called in sequence, if middleware
|
|
64
65
|
*
|
|
65
|
-
* @param {String|Array<String>} methods A method matcher or array of available methods
|
|
66
|
+
* @param {String|Array<String>} methods A method matcher or array of available methods in upper-case
|
|
66
67
|
* @param {String|RegExp|Array<String|RegExp>} paths A prefix path to match
|
|
67
68
|
* @param {CowboyMiddleware...} middleware Middleware to call in sequence
|
|
68
69
|
*
|
|
@@ -118,6 +119,46 @@ export class Cowboy {
|
|
|
118
119
|
if (env.COWBOY_DEBUG)
|
|
119
120
|
debug.enabled = true;
|
|
120
121
|
|
|
122
|
+
if ( // Setup scheduler endpoint if this is the first fetch() hit were we can access the `env` state
|
|
123
|
+
!this._schedulerSetup
|
|
124
|
+
&& (
|
|
125
|
+
this.settings.scheduler === true
|
|
126
|
+
|| (
|
|
127
|
+
this.settings.scheduler == 'VAR'
|
|
128
|
+
&& env.COWBOY_SCHEDULER
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
) {
|
|
132
|
+
console.log('Scheduler setup against /__scheduled');
|
|
133
|
+
this._schedulerSetup = true;
|
|
134
|
+
|
|
135
|
+
// Find first GET operation
|
|
136
|
+
let routeIndex = this.routes.findIndex(r => r.methods.some(m => m == 'GET'));
|
|
137
|
+
if (routeIndex < 0) routeIndex = this.routes.length; // If no GETS, assume end position
|
|
138
|
+
|
|
139
|
+
// Splice into position before first GET
|
|
140
|
+
let matcher = compileRoutePaths('/__scheduled');
|
|
141
|
+
this.routes.splice(routeIndex, 0, {
|
|
142
|
+
methods: ['GET'],
|
|
143
|
+
paths: matcher.paths,
|
|
144
|
+
matcher,
|
|
145
|
+
middleware: [
|
|
146
|
+
async (req, res, env) => {
|
|
147
|
+
debug('Executing schedule handler');
|
|
148
|
+
if (!this.schedule.handler) return res.status(404).send('No scheduler installed');
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
let result = await this.schedule.handler.call(this, {}, env, {});
|
|
152
|
+
res.send(result);
|
|
153
|
+
} catch (e) {
|
|
154
|
+
res.status(400).send(`Scheduler threw error: ${e.toString()}`);
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
]
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
|
|
121
162
|
// Create basic [req]uest / [res]ponse objects
|
|
122
163
|
let req = new CowboyRequest(cfReq, {
|
|
123
164
|
router: this,
|
|
@@ -310,10 +351,6 @@ export class Cowboy {
|
|
|
310
351
|
if (this.doneInit) return this; // Already completed init
|
|
311
352
|
debug('INIT!');
|
|
312
353
|
|
|
313
|
-
if (this.settings.patchAxios) {
|
|
314
|
-
// TODO: Patch Axios somehow
|
|
315
|
-
// axios.defaults.adapter = axiosFetchAdapter;
|
|
316
|
-
}
|
|
317
354
|
return this;
|
|
318
355
|
}
|
|
319
356
|
}
|
package/lib/testkit.js
CHANGED
|
@@ -19,6 +19,7 @@ export let worker;
|
|
|
19
19
|
* @param {Axios} [options.axios] Axios instance to mutate with the base URL, if specified
|
|
20
20
|
* @param {Boolean} [options.server=true] Initialize a local server - disable this if you're running your own
|
|
21
21
|
* @param {Boolean} [options.debug=false] Force debug as if `DEBUG=cowboy` was set
|
|
22
|
+
* @param {Boolean} [options.scheduler=false] Bind any scedhuler callback to `/__scheduled`
|
|
22
23
|
* @param {Function} [options.logOutput] Function to wrap STDOUT output. Called as `(line:String)`
|
|
23
24
|
* @param {Function} [options.logOutputErr] Function to wrap STDERR output. Called as `(line:String)`
|
|
24
25
|
* @param {String} [options.host='127.0.0.1'] Host to run Wrangler on
|
|
@@ -32,6 +33,7 @@ export function start(options) {
|
|
|
32
33
|
axios: null,
|
|
33
34
|
server: true,
|
|
34
35
|
debug: false,
|
|
36
|
+
scheduler: false,
|
|
35
37
|
logOutput: output => console.log('WRANGLER>', output),
|
|
36
38
|
logOutputErr: output => console.log('WRANGLER!', output),
|
|
37
39
|
host: '127.0.0.1',
|
|
@@ -70,6 +72,9 @@ export function start(options) {
|
|
|
70
72
|
...(debug.enabled ? [
|
|
71
73
|
'--var=COWBOY_DEBUG:1'
|
|
72
74
|
]: []),
|
|
75
|
+
...(settings.scheduler ? [
|
|
76
|
+
'--var=COWBOY_SCHEDULER:1'
|
|
77
|
+
]: []),
|
|
73
78
|
]);
|
|
74
79
|
|
|
75
80
|
worker.stdout.on('data', data => {
|