@momsfriendlydevco/cowboy 1.0.15 → 1.0.17
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 +1 -1
- package/lib/cowboy.js +10 -6
- package/lib/request.js +3 -0
- package/lib/response.js +6 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -106,7 +106,7 @@ let router = new Cowboy()
|
|
|
106
106
|
Notes:
|
|
107
107
|
* All middleware items are called in sequence - and are async waited-on)
|
|
108
108
|
* If any middleware functions fail the entire chain aborts with an error
|
|
109
|
-
* All middleware functions are called as `(CowboyRequest, CowboyResponse)`
|
|
109
|
+
* All middleware functions are called as `(CowboyRequest, CowboyResponse, Env)`
|
|
110
110
|
* If any middleware functions call `res.end()` (or any of its automatic methods like `res.send()` / `res.sendStatus()`) the chain also aborts successfully
|
|
111
111
|
* If the last middleware function returns a non response object - i.e. the function didn't call `res.send()` its assumed to be a valid output and is automatically wrapped
|
|
112
112
|
|
package/lib/cowboy.js
CHANGED
|
@@ -2,6 +2,7 @@ import debug from '#lib/debug';
|
|
|
2
2
|
import CowboyMiddleware from '#middleware';
|
|
3
3
|
import CowboyRequest from '#lib/request';
|
|
4
4
|
import CowboyResponse from '#lib/response';
|
|
5
|
+
import {compile as compileRoutePaths} from '@momsfriendlydevco/path-match';
|
|
5
6
|
|
|
6
7
|
export class Cowboy {
|
|
7
8
|
/**
|
|
@@ -68,11 +69,14 @@ export class Cowboy {
|
|
|
68
69
|
* @returns {Cowboy} This chainable Cowboy router instance
|
|
69
70
|
*/
|
|
70
71
|
route(methods, paths, ...middleware) {
|
|
72
|
+
let matcher = compileRoutePaths(paths);
|
|
71
73
|
this.routes.push({
|
|
72
74
|
methods: Array.isArray(methods) ? methods : [methods],
|
|
73
|
-
paths:
|
|
75
|
+
paths: matcher.paths,
|
|
76
|
+
matcher,
|
|
74
77
|
middleware,
|
|
75
78
|
})
|
|
79
|
+
|
|
76
80
|
return this;
|
|
77
81
|
}
|
|
78
82
|
|
|
@@ -96,11 +100,7 @@ export class Cowboy {
|
|
|
96
100
|
resolve(req) {
|
|
97
101
|
return this.routes.find(route =>
|
|
98
102
|
route.methods.includes(req.method) // Method matches
|
|
99
|
-
&& route.
|
|
100
|
-
typeof path == 'string' ? req.path == path
|
|
101
|
-
: path instanceof RegExp ? path.test(req.path)
|
|
102
|
-
: (()=> { throw new Error('Path is not a String or RegExp') })()
|
|
103
|
-
)
|
|
103
|
+
&& route.matcher.isMatch(req.path)
|
|
104
104
|
);
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -150,6 +150,10 @@ export class Cowboy {
|
|
|
150
150
|
return res.sendStatus(404).toCloudflareResponse(); // No matching route
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
// Populate params
|
|
154
|
+
let firstPathIndex = route.paths.findIndex(re => re.test(req.path));
|
|
155
|
+
req.params = route.paths[firstPathIndex].exec(req.path)?.groups;
|
|
156
|
+
|
|
153
157
|
// Exec route middleware
|
|
154
158
|
let response = await this.execMiddleware({
|
|
155
159
|
req, res, env,
|
package/lib/request.js
CHANGED
|
@@ -54,6 +54,9 @@ export default class CowboyRequest {
|
|
|
54
54
|
this.hostname = url.hostname;
|
|
55
55
|
this.query = Object.fromEntries(url.searchParams);
|
|
56
56
|
|
|
57
|
+
this.routePath = ''; // Eventually matching routePath segment
|
|
58
|
+
this.params = {}; // Set empty object for path extraction
|
|
59
|
+
|
|
57
60
|
// Slurp the headers
|
|
58
61
|
this.headers = Object.fromEntries(cfReq.headers.entries());
|
|
59
62
|
|
package/lib/response.js
CHANGED
|
@@ -99,7 +99,12 @@ export default class CowboyResponse {
|
|
|
99
99
|
status: this.code,
|
|
100
100
|
headers: this.headers,
|
|
101
101
|
};
|
|
102
|
-
console.log('
|
|
102
|
+
console.log('Response', JSON.stringify({
|
|
103
|
+
...cfOptions,
|
|
104
|
+
body:
|
|
105
|
+
typeof this.body == 'string' && this.body.length > 30 ? this.body.substr(0, 50) + '…'
|
|
106
|
+
: this.body,
|
|
107
|
+
}, null, '\t'));
|
|
103
108
|
return new this.CloudflareResponse(this.body, cfOptions);
|
|
104
109
|
}
|
|
105
110
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@momsfriendlydevco/cowboy",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "eslint ."
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@momsfriendlydevco/joyful": "^1.0.1",
|
|
39
|
+
"@momsfriendlydevco/path-match": "^1.0.0",
|
|
39
40
|
"toml": "^3.0.0"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|