Skip to content

Commit 231519d

Browse files
authored
PR #23 (GetRayo/test/integration)
Test/integrations, updated and version patch.
2 parents cce11b5 + c5a1180 commit 231519d

File tree

15 files changed

+294
-83
lines changed

15 files changed

+294
-83
lines changed

.eslintrc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
"error",
1414
{
1515
"semi": true,
16-
"printWidth": 100,
16+
"printWidth": 80,
1717
"singleQuote": true,
1818
"arrowParens": "always"
1919
}
2020
],
2121
"func-names": ["error", "always"],
2222
"comma-dangle": ["error", "never"],
2323
"no-param-reassign": ["error", { "props": false }],
24-
"max-len": ["error", 100, { "ignoreRegExpLiterals": true }]
24+
"max-len": ["error", 80, { "ignoreRegExpLiterals": true }]
2525
}
2626
}

.prettierrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"semi": true,
3-
"printWidth": 100,
3+
"printWidth": 80,
44
"singleQuote": true,
55
"arrowParens": "always"
66
}

README.md

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111

1212
```
1313
Everything in the -modern- web is arguable,
14-
however we are convinced that Rayo is the fastest framework.
15-
In the world.
14+
however we believe that Rayo is the fastest framework for Nodejs.
1615
Period.
1716
```
1817

1918
## In a nutshell
2019

21-
- Really fast (yeah, like really fast. Up to 50% faster than Express)
22-
- API similiar to Express.
23-
- Compatible with existing Express middlware.
24-
- Extensible & plugable.
25-
- Less than 150 lines of code, with routing and all.
20+
- Really fast (yeah, like really fast. See [how it compares](#how-does-it-compare)),
21+
- similar API to express¹,
22+
- compatible with express middleware,
23+
- extensible & plugable,
24+
- less than 150 lines of code, with routing and all.
25+
26+
> ¹ `Rayo` is not intended to be an express replacement, thus the API is similar, inspired-by, and not identical.
2627
2728

2829
## Install
@@ -75,7 +76,7 @@ rayo({ port: 5050 })
7576

7677
`handler` functions accept an [IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage) (a.k.a `req`), a [ServerResponse](https://nodejs.org/dist/latest-v9.x/docs/api/http.html#http_class_http_serverresponse) (a.k.a `res`) and a `step through` (a.k.a `step`) function. `step()` is optional and it may be used to move the program's execution logic to the next handler in the stack.
7778

78-
`step()` may also be used to invoke an error at any time. See [error handling](#error-handling).
79+
`step()` may also be used to return an error at any time. See [error handling](#error-handling).
7980

8081
> **Note:** An error will be thrown if `step()` is called on an empty stack.
8182
@@ -114,15 +115,35 @@ const fn = (req, res, step) => {
114115

115116
#### Error handling
116117

117-
## API
118-
119118
```
120119
Please keep in mind that:
121120
"Your code, your errors."¹
122121
- It's your responsibility to deal with them accordingly.
123122
```
124123

125-
> ¹ `Rayo` is WIP, so you may encounter actual errors that need to be dealt with. If so, please point them out to us via a `pull request`. 👍
124+
> ² `Rayo` is WIP, so you may encounter actual errors that need to be dealt with. If so, please point them out to us via a `pull request`. 👍
125+
126+
If you have implemented a custom error function (see `onError` under [options](#rayooptions--)) you may invoke it at any time by calling the `step()` function with an argument.
127+
```js
128+
const rayo = require('rayo');
129+
130+
const options = {
131+
port: 5050,
132+
onError: (error, req, res) => {
133+
res.end(`Here's your error: ${error}`);
134+
}
135+
};
136+
137+
rayo(options)
138+
.get('/', (req, res, step) => step('Thunderstruck!'))
139+
.start();
140+
```
141+
In the above example, the error will be returned on the `/` path, since `step()` is being called with an argument. Run the example, open your browser and go to [http://localhost:5050](http://localhost:5050) and you will see "Here's your error: Thunderstruck!".
142+
143+
If you don't have a custom error function, you may still call `step()` (with an argument), in which case the error will be returned using Rayo's standard function.
144+
145+
146+
## API
126147

127148
#### rayo(options = {})
128149
```
@@ -155,26 +176,7 @@ Please keep in mind that:
155176
// Your custom logic.
156177
}
157178
```
158-
159-
`Default:` A "Page not found." message with a `404` status code.<br />
160-
161-
```js
162-
const rayo = require('rayo');
163-
164-
const options = {
165-
port: 5050,
166-
notFound: (req, res) => {
167-
res.end('The requested page is magically gone.');
168-
}
169-
};
170-
171-
/**
172-
* Visit `/hello` to trigger the `notFound` method.
173-
*/
174-
rayo(options)
175-
.get('/', (req, res) => res.end('Thunderstruck, GET'))
176-
.start();
177-
```
179+
`Default:` Rayo will send a "Page not found." message with a `404` status code.
178180

179181
* `options.onError` _{function}_
180182

@@ -192,26 +194,6 @@ Please keep in mind that:
192194
}
193195
```
194196

195-
`Default:` The error message (the argument) with a `400` status code.<br />
196-
197-
```js
198-
const rayo = require('rayo');
199-
200-
const options = {
201-
port: 5050,
202-
onError: (error, req, res) => {
203-
res.end(`Here's your error: ${error}`);
204-
}
205-
};
206-
207-
/**
208-
* Visit `/` to trigger the `onError` method.
209-
*/
210-
rayo(options)
211-
.get('/', (req, res, step) => step('Thunderstruck, error'))
212-
.start();
213-
```
214-
215197

216198
#### .verb(path, ...handlers)
217199
```

bin/bridge.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ module.exports = class Bridge {
4848
this.t = this.h['*'] && this.h['*']['*'] ? this.h['*']['*'] : [];
4949
bridgeThrough(this);
5050
} else {
51-
const [verb, path] = this.bridgedPath ? ['all', this.bridgedPath] : ['*', '*'];
51+
const [verb, path] = this.bridgedPath
52+
? ['all', this.bridgedPath]
53+
: ['*', '*'];
5254
this.route(verb, path, ...handlers);
5355
}
5456

@@ -61,7 +63,9 @@ module.exports = class Bridge {
6163
this.h[m] = this.h[m] || {};
6264
this.routes[m].push(parse(path));
6365
this.h[m][path] = this.h[m][path] || [];
64-
this.h[m][path] = this.h[m][path].concat(handlers).map((fn) => (...args) => fn(...args));
66+
this.h[m][path] = this.h[m][path]
67+
.concat(handlers)
68+
.map((fn) => (...args) => fn(...args));
6569
};
6670

6771
if (verb === 'all') {
@@ -75,7 +79,8 @@ module.exports = class Bridge {
7579

7680
fetch(verb, path) {
7781
this.cache.urls[verb] = this.cache.urls[verb] || {};
78-
const url = this.cache.urls[verb][path] || match(path, this.routes[verb] || []);
82+
const url =
83+
this.cache.urls[verb][path] || match(path, this.routes[verb] || []);
7984
this.cache.urls[verb][path] = url;
8085
return !url.length
8186
? null

bin/rayo.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class Index extends Bridge {
3333
const parsedUrl = parseurl(req);
3434
const route = this.fetch(req.method, parsedUrl.pathname);
3535
if (!route) {
36-
return this.notFound ? this.notFound.call(null, req, res) : res.send('Page not found.', 404);
36+
return this.notFound
37+
? this.notFound(req, res)
38+
: res.send('Page not found.', 404);
3739
}
3840

3941
req.params = route.params;
@@ -47,15 +49,15 @@ class Index extends Bridge {
4749
const fn = middleware.shift();
4850
if (error) {
4951
return this.onError
50-
? this.onError.call(null, error, req, res, fn)
52+
? this.onError(error, req, res, fn)
5153
: res.send(error, statusCode || 400);
5254
}
5355

5456
if (fn) {
5557
return fn(req, res, this.step.bind(this, req, res, middleware));
5658
}
5759

58-
throw new Error('No middleware to move to, there is nothing left in the stack.');
60+
throw new Error('No handler to move to, the stack is empty.');
5961
}
6062
}
6163

bin/response.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ const isJSON = (payload) => {
1717
module.exports = {
1818
send(payload, statusCode) {
1919
const { valid, json } = isJSON(payload);
20+
const response = valid ? json : payload;
2021
this.writeHead(statusCode || 200, {
21-
'Content-Type': valid ? 'application/json' : 'text/plain'
22+
'Content-Type': valid ? 'application/json' : 'text/plain',
23+
'Content-Length': (response || '').length
2224
});
23-
this.write(valid ? json : payload);
25+
this.write(response);
2426
this.end();
2527
}
2628
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rayo",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "⚡️⚡️ Rayo, an extremely fast micro-framework for Nodejs.",
55
"main": "bin/rayo.js",
66
"engines": {
@@ -40,7 +40,7 @@
4040
"hapi": "17.x",
4141
"minimist": "1.x",
4242
"mocha": "5.x",
43-
"nyc": "11.x",
43+
"nyc": "12.x",
4444
"ora": "2.x",
4545
"polka": "0.x",
4646
"prettier": "1.x",

test/benchmarks/compare/fastify.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ const schema = {
1515
}
1616
};
1717

18-
fastify.get('/', schema, (req, reply) => reply.send('Thunderstruck...')).listen(5050);
18+
fastify
19+
.get('/', schema, (req, reply) => reply.send('Thunderstruck...'))
20+
.listen(5050);

test/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const loadUnit = (unit) => require.call(null, `./${unit}`);
22

33
describe('Unit tests', () => {
4-
describe('rayo.js', loadUnit('units/rayo'));
5-
describe('response.js', loadUnit('units/response'));
6-
describe('bridge.js', loadUnit('units/bridge'));
4+
describe('Rayo', loadUnit('units/rayo'));
5+
describe('Bridge', loadUnit('units/bridge'));
6+
describe('Response', loadUnit('units/response'));
7+
8+
describe('Integration tests', loadUnit('units/integration'));
79
});

test/units/bridge.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
const Bridge = require('../../bin/bridge');
21
const should = require('should');
32
const sinon = require('sinon');
43
const { METHODS } = require('http');
4+
const Bridge = require('../../bin/bridge');
55

66
const test = (bridge, path = null) => {
77
should(bridge).be.an.Object();
8-
should(bridge).have.properties('id', 'bridgedPath', 'routes', 'through', 'route', 'fetch');
8+
should(bridge).have.properties(
9+
'id',
10+
'bridgedPath',
11+
'routes',
12+
'through',
13+
'route',
14+
'fetch'
15+
);
916
should(bridge.id).be.a.String();
1017
should(bridge.bridgedPath).be.equal(path);
1118
should(bridge.routes).be.an.Object();

0 commit comments

Comments
 (0)