@momsfriendlydevco/cowboy 1.2.0 → 1.3.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.
- package/README.md +5 -0
- package/lib/cowboy.js +1 -1
- package/lib/request.js +12 -8
- package/middleware/devOnly.js +15 -0
- package/middleware/index.js +2 -0
- package/middleware/validate.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -322,6 +322,11 @@ cors(options)
|
|
|
322
322
|
Inject simple CORS headers to allow websites to use the endpoint from the browser frontend.
|
|
323
323
|
|
|
324
324
|
|
|
325
|
+
devOnly()
|
|
326
|
+
---------
|
|
327
|
+
Allow access to the endpoint ONLY if Cloudflare is running in local development mode. Throws a 403 otherwise.
|
|
328
|
+
|
|
329
|
+
|
|
325
330
|
validate(key, validator)
|
|
326
331
|
------------------------
|
|
327
332
|
Validate the incoming `req.$KEY` object using [Joyful](https://github.com/MomsFriendlyDevCo/Joyful).
|
package/lib/cowboy.js
CHANGED
|
@@ -256,7 +256,7 @@ export class Cowboy {
|
|
|
256
256
|
: e?.code && e?.message && typeof e.code == 'string' && typeof e.message == 'string' ? `${e.code}: ${e.message}` // Supabase error objects
|
|
257
257
|
: 'An unknown error has occured';
|
|
258
258
|
|
|
259
|
-
|
|
259
|
+
console.warn('Error thrown', e);
|
|
260
260
|
debug('Extracted error text digest', {errorText});
|
|
261
261
|
|
|
262
262
|
// Form: '404: Not found'
|
package/lib/request.js
CHANGED
|
@@ -93,33 +93,37 @@ export default class CowboyRequest {
|
|
|
93
93
|
switch (type) {
|
|
94
94
|
case 'json':
|
|
95
95
|
case 'application/json':
|
|
96
|
-
|
|
97
|
-
this.body =
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
96
|
+
if (this.headers['content-length'] == 0) { // Sending JSON but its blank
|
|
97
|
+
this.body = {};
|
|
98
|
+
} else { // Try to decode JSON in a wrapper
|
|
99
|
+
try {
|
|
100
|
+
this.body = await this.body.json();
|
|
101
|
+
} catch (e) {
|
|
102
|
+
if (debug.enabled) debug('Failed to decode request body as JSON:', e.toString());
|
|
103
|
+
throw new Error('Invalid JSON body');
|
|
104
|
+
}
|
|
102
105
|
}
|
|
106
|
+
break;
|
|
103
107
|
case 'formData':
|
|
104
108
|
case 'multipart/form-data': // Decode as multi-part
|
|
105
109
|
case 'application/x-www-form-urlencoded': // Decode as multi-part
|
|
106
110
|
try {
|
|
107
111
|
let formData = await this.body.formData();
|
|
108
112
|
this.body = Object.fromEntries(formData.entries());
|
|
109
|
-
break;
|
|
110
113
|
} catch (e) {
|
|
111
114
|
if (debug.enabled) debug('Failed to decode multi-part body:', e.toString());
|
|
112
115
|
throw new Error('Invalid multi-part encoded body');
|
|
113
116
|
}
|
|
117
|
+
break;
|
|
114
118
|
case 'text':
|
|
115
119
|
case 'text/plain': // Decode as plain text
|
|
116
120
|
try {
|
|
117
121
|
this.body = await this.body.text();
|
|
118
|
-
break;
|
|
119
122
|
} catch (e) {
|
|
120
123
|
if (debug.enabled) debug('Failed to decode plain-text body:', e.toString());
|
|
121
124
|
throw new Error('Invalid text body');
|
|
122
125
|
}
|
|
126
|
+
break;
|
|
123
127
|
default:
|
|
124
128
|
debug('Empty Body Payload - assuming raw payload');
|
|
125
129
|
this.text = await this.body.text();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Only permit access if Cloudflare is running in development mode
|
|
3
|
+
* This should only permit access in local dev environments
|
|
4
|
+
*
|
|
5
|
+
* @returns {CowboyMiddleware} A CowboyMiddleware worker which will return 403 if not in development mode
|
|
6
|
+
*/
|
|
7
|
+
export default function CowboyMiddlewareDevOnly() {
|
|
8
|
+
return (req, res, env) => {
|
|
9
|
+
const isDev = self.ENVIRONMENT === 'development';
|
|
10
|
+
|
|
11
|
+
if (!isDev) return res
|
|
12
|
+
.status(403)
|
|
13
|
+
.send('Endpoint responds in development mode only');
|
|
14
|
+
};
|
|
15
|
+
}
|
package/middleware/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import cors from '#middleware/cors';
|
|
2
|
+
import devOnly from '#middleware/devOnly';
|
|
2
3
|
import parseJwt from '#middleware/parseJwt';
|
|
3
4
|
import validate from '#middleware/validate';
|
|
4
5
|
import validateBody from '#middleware/validateBody';
|
|
@@ -8,6 +9,7 @@ import validateQuery from '#middleware/validateQuery';
|
|
|
8
9
|
|
|
9
10
|
export default {
|
|
10
11
|
cors,
|
|
12
|
+
devOnly,
|
|
11
13
|
parseJwt,
|
|
12
14
|
validate,
|
|
13
15
|
validateBody,
|
package/middleware/validate.js
CHANGED
|
@@ -5,7 +5,7 @@ import joyful from '@momsfriendlydevco/joyful';
|
|
|
5
5
|
*
|
|
6
6
|
* @param {String} subkey The subkey to run against
|
|
7
7
|
* @param {Function|Object} Callback to use with Joyful to validate with
|
|
8
|
-
* @returns {Void} Either a successful middleware cycle (if
|
|
8
|
+
* @returns {Void} Either a successful middleware cycle (if validation succeeds) or a call to `res.status(400)` if failed
|
|
9
9
|
*/
|
|
10
10
|
export default function CowboyMiddlewareValidate(subkey, validator) {
|
|
11
11
|
return (req, res) => {
|