@momsfriendlydevco/cowboy 1.4.1 → 1.5.0

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.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/lib/cowboy.js +49 -21
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -10,6 +10,7 @@ Features:
10
10
  * Built in middleware + request validation via [Joi](https://joi.dev)
11
11
  * Built-in debug support for testkits + Wrangler
12
12
  * Built-in JSON / Multipart (or FormData) / Plain text decoding and population of `req.body`
13
+ * Scheduled tasks can return promises and they are automatically awaited (no need to do `ctx.waitUntil()`)
13
14
 
14
15
 
15
16
  Examples
package/lib/cowboy.js CHANGED
@@ -148,10 +148,24 @@ export class Cowboy {
148
148
  if (!this.schedule.handler) return res.status(404).send('No scheduler installed');
149
149
 
150
150
  try {
151
- let result = await this.schedule.handler.call(this, {}, env, {});
152
- res.send(result);
151
+ let result = await this.schedule.handler.call(
152
+ this,
153
+ { // Faked Cloudflare `controller` context
154
+ cron: 'FAKE',
155
+ type: 'scheduled',
156
+ scheduledTime: (new Date()).toISOString(),
157
+ },
158
+ env,
159
+ { // Faked Cloudflare `ctx` context - we provide a fake waitUntil here
160
+ waitUntil() {
161
+ throw new Error('ctx.waitUntil() functionality is provided natively by Cowboy.schedule(cb:Function) - just return a promise instead of using it');
162
+ },
163
+ },
164
+ );
165
+ debug('Got scheduler response', result);
166
+ return res.send(result);
153
167
  } catch (e) {
154
- res.status(400).send(`Scheduler threw error: ${e.toString()}`);
168
+ return res.status(400).send(`Scheduler threw error: ${e.toString()}`);
155
169
  }
156
170
  },
157
171
  ]
@@ -208,23 +222,6 @@ export class Cowboy {
208
222
  }
209
223
 
210
224
 
211
- /**
212
- * Set up Cloudflare response to "scheduled" call
213
- * This is really just a map to the last handler we installed to .schedule(cb) - for now
214
- *
215
- * @param {CloudflareEvent} event The Cloudflare event context passed
216
- * @param {Object} env Environment variables
217
- * @param {CloudflareContext} ctx The Cloudflare context to respond to
218
- *
219
- * @returns {Cowboy} This chainable Cowboy router instance
220
- */
221
- scheduled(event, env, ctx) {
222
- if (!this.schedule.handler) throw new Error('Attemped to access Cowboy.scheduled without first calling .schedule() to set something up!');
223
- this.schedule.handler.call(this, event, env, ctx);
224
- return this;
225
- }
226
-
227
-
228
225
  /**
229
226
  * Call a router function as if it were invoked directly
230
227
  * This function exists as an easier way to remap body contents without
@@ -336,12 +333,43 @@ export class Cowboy {
336
333
  * @returns {Cowboy} This chainable Cowboy router instance
337
334
  */
338
335
  schedule(handler) {
339
- console.info('Installed schedule event handler. Access via http://localhost:8787/__scheduled');
336
+ debug('Installed schedule event handler');
340
337
  this.schedule.handler = handler;
341
338
  return this;
342
339
  }
343
340
 
344
341
 
342
+ /**
343
+ * Set up Cloudflare response to "scheduled" call
344
+ * This is really just a map to the last handler we installed to .schedule(cb) - for now
345
+ *
346
+ * @param {CloudflareEvent} event The Cloudflare event context passed
347
+ * @param {Object} env Environment variables
348
+ * @param {CloudflareContext} ctx The Cloudflare context to respond to
349
+ *
350
+ * @returns {Cowboy} This chainable Cowboy router instance
351
+ */
352
+ scheduled(event, env, ctx) {
353
+ if (!this.schedule.handler) throw new Error('Attemped to access Cowboy.scheduled without first calling .schedule() to set something up!');
354
+
355
+ // Wrap all scheduler calls in ctx.waitUntil() so promises are always waited on
356
+ ctx.waitUntil(
357
+ this.schedule.handler.call(
358
+ this,
359
+ event,
360
+ env,
361
+ {
362
+ waitUntil() {
363
+ throw new Error('ctx.waitUntil() functionality is provided natively by Cowboy.schedule(cb:Function) - just return a promise instead of using it');
364
+ },
365
+ },
366
+ )
367
+ );
368
+
369
+ return this;
370
+ }
371
+
372
+
345
373
  /**
346
374
  * Generial Init() sequence
347
375
  * This will be run automatically on setup or the first fetch()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momsfriendlydevco/cowboy",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
5
5
  "scripts": {
6
6
  "lint": "eslint"