Skip to content

Commit 0648586

Browse files
committed
save
1 parent 891dc25 commit 0648586

File tree

12 files changed

+116
-179
lines changed

12 files changed

+116
-179
lines changed

.coveralls.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
service_name: travis-pro
2+
repo_token: QiuhMbOuEHYcMAJxzVIvCVjfwuI7dWGPc

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.nyc_support
22
coverage
3+
backup
34
packages/**/node_modules

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: node_js
2+
node_js:
3+
- 8
4+
- 10
5+
- 12
6+
os:
7+
- linux
8+
9+
script:
10+
- "npm run lint"
11+
- "npm run cover"
12+
13+
after_success:
14+
- if [[ $TRAVIS_NODE_VERSION == "12" ]]; then cat ./coverage/lcov.info | coveralls; fi

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
1-
# tiny.js
1+
# tiny.io
2+
[![Build Status](https://travis-ci.org/TinyIO/tiny.js.svg?branch=dev)](https://travis-ci.org/TinyIO/tiny.js)
3+
[![codebeat badge](https://codebeat.co/badges/79ae878a-275f-4b68-86f1-5b3c749a58b0)](https://codebeat.co/projects/github-com-tinyio-tiny-js-dev)
4+
[![Coverage Status](https://coveralls.io/repos/github/TinyIO/tiny.js/badge.svg?branch=dev)](https://coveralls.io/github/TinyIO/tiny.js?branch=dev)
5+
[![install size](https://packagephobia.now.sh/badge?p=tiny.io)](https://packagephobia.now.sh/result?p=tiny.io)
6+
[![Dependency Status](https://david-dm.org/TinyIO/tiny.js/status.svg)](https://david-dm.org/TinyIO/tiny.js)
7+
8+
tiny.io 是一个精简的web开发框架,以兼容express的思路进行编写,可以直接使用大部分express的middleware
9+
10+
和express [![install size](https://packagephobia.now.sh/badge?p=express)](https://packagephobia.now.sh/result?p=express)
11+
比它真的[![install size](https://packagephobia.now.sh/badge?p=tiny.io)](https://packagephobia.now.sh/result?p=tiny.io)很小,核心功能是路由和middleware结构, 将非核心功能从项目中剥离, 来保持一个简单、清晰、高效的内核.
12+
13+
| | Requests/s | Latency | Throughput/Mb |
14+
|------------|:----------:|--------:|-----:|
15+
| NativeHttp | 46524 | 10.53 | 4.58 |
16+
| Tiny | 41200.81 | 11.82 | 4.05 |
17+
| Express | 15666 | 29.96 | 1.55 |
18+
19+
性能测试来看效率还不错(当然这并不意味你的应用真的能有这么大幅度的性能提升, 通常CURD才是性能杀手)
20+
21+
# TODO
22+
23+
- [ ] 说明文档补全
24+
- [ ] API列表
25+
- [ ] 和express的差异和注意事项
26+
- [ ] 测试用例补全
27+
- [ ] 覆盖率达到100%
28+
- [ ] 编写若干和express常用middleware结合的例子

packages/tiny/static.js renamed to backup/static/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = function(dir, opts = {}) {
3434
dir = resolve(dir || '.');
3535
const FILES = {};
3636
const find = (uri, extns) => {
37-
if (!!~uri.lastIndexOf('.')) return FILES[uri];
37+
if (~uri.lastIndexOf('.')) return FILES[uri];
3838
let i = 0;
3939
let data;
4040
const arr = toAssume(uri, extns);
@@ -76,8 +76,8 @@ module.exports = function(dir, opts = {}) {
7676
'last-modified': stats.mtime.toUTCString()
7777
};
7878
cc && (headers['cache-control'] = cc);
79-
opts.etag && (headers['etag'] = toEtag(stats));
80-
FILES['/' + str.replace(/\\+/g, '/')] = { abs, stats, headers };
79+
opts.etag && (headers.etag = toEtag(stats));
80+
FILES[`/${str.replace(/\\+/g, '/')}`] = { abs, stats, headers };
8181
});
8282

8383
return (req, res, next) => {

package.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,32 @@
1515
"scripts": {
1616
"test": "node test/index.js",
1717
"tape": "tape tests/*.js | tap-spec",
18-
"cover": "istanbul cover test/index.js",
19-
"eslint": "eslint --quiet --fix .",
18+
"lint": "eslint --quiet --fix .",
19+
"cover": "nyc --reporter=lcovonly --reporter=text --reporter=text-summary --exclude=tests tape tests/*.js | tap-spec",
2020
"bench": "node test/benchmarks"
2121
},
2222
"license": "MIT",
2323
"devDependencies": {
2424
"autocannon": "^2.4.1",
2525
"axios": "^0.18.0",
2626
"cli-table": "^0.3.1",
27-
"eslint": "^5.12.0",
27+
"coveralls": "^3.0.3",
28+
"eslint": "^5.16.0",
2829
"eslint-config-airbnb-base": "^13.1.0",
29-
"eslint-config-prettier": "^3.4.0",
30-
"eslint-plugin-import": "^2.14.0",
30+
"eslint-config-prettier": "^3.6.0",
31+
"eslint-plugin-import": "^2.17.2",
3132
"eslint-plugin-prettier": "^2.7.0",
32-
"express": "^4.16.4",
33+
"express": "^4.17.0",
3334
"kleur": "^2.0.2",
3435
"minimist": "^1.2.0",
35-
"ora": "^3.0.0",
36+
"nyc": "^14.1.1",
37+
"ora": "^3.4.0",
3638
"pancho": "^1.0.2",
37-
"polka": "^0.5.1",
38-
"prettier": "^1.15.3",
39-
"rayo": "^1.2.7",
39+
"polka": "^0.5.2",
40+
"prettier": "^1.17.1",
41+
"rayo": "^1.3.2",
4042
"tap-spec": "^5.0.0",
41-
"tape": "^4.9.2"
43+
"tape": "^4.10.1"
4244
},
4345
"dependencies": {}
4446
}

packages/tiny/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ class Tiny extends Router {
3333
}
3434

3535
mount(base, router) {
36-
if (base instanceof Router) {
37-
router = base;
38-
}
39-
if (router.basePath != null) {
40-
throw new Error('');
41-
}
42-
router.basePath = base;
36+
router.setBasePath(base);
4337
this.subRoutes.push(router);
4438
return this;
4539
}
@@ -55,6 +49,7 @@ class Tiny extends Router {
5549
}
5650

5751
handler(req, res) {
52+
req.originalUrl = req.originalUrl || req.url;
5853
const parsedUrl = parseurl(req);
5954
const params = {};
6055
const route = this.match(req.method, parsedUrl.pathname, params);

packages/tiny/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
2-
"name": "tiny",
3-
"version": "0.0.1",
4-
"description": "tiny, an micro-framework for Nodejs.",
2+
"name": "tiny.io",
3+
"version": "0.0.2",
4+
"description": "tiny.io, a tiny-web-framework for Nodejs.",
55
"main": "index.js",
6+
"files": [
7+
"*.js"
8+
],
69
"engines": {
710
"node": ">=8.11.4"
811
},

packages/tiny/router.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,38 @@ const NAME = Symbol('name');
1515
const TYPE = Symbol('type');
1616
const HANDLER = Symbol('handler');
1717

18+
const lead = (x) => (x.charCodeAt(0) === 47 ? x : `/${x}`);
19+
1820
const strip = (str) => {
1921
if (str === SEP) return str;
22+
// eslint-disable-next-line no-unused-expressions
2023
str.charCodeAt(0) === SLASH && (str = str.substr(1));
2124
const len = str.length - 1;
2225
return str.charCodeAt(len) === SLASH ? str.substr(0, len) : str;
2326
};
2427

28+
// eslint-disable-next-line no-cond-assign
2529
const split = (str) => ((str = strip(str)) === SEP ? EMPTY_ARRAY : str.split(SEP));
2630

2731
module.exports = class Router {
2832
constructor(path = null) {
29-
this.basePath = path;
33+
this.basePath = path && lead(path);
34+
const baseLen = path && this.basePath.length;
3035
this[WAY] = null;
3136
this.routes = {
3237
// FILTER(*) for through filter (aka use) handler
3338
[FILTER]: {
3439
[TYPE]: STYPE,
3540
[NAME]: SEP,
36-
[HANDLER]: [],
41+
[HANDLER]: path
42+
? [
43+
(req, res, next) => {
44+
req.url = req.url.substring(baseLen) || '/';
45+
req.path = req.path.substring(baseLen) || '/';
46+
next();
47+
}
48+
]
49+
: [],
3750
[PARAM]: null
3851
}
3952
};
@@ -53,12 +66,29 @@ module.exports = class Router {
5366
this.all = this.add.bind(this, ...bind);
5467
}
5568

69+
setBasePath(path) {
70+
if (this.basePath) {
71+
throw new Error('router may mounted?');
72+
}
73+
this.basePath = lead(path);
74+
const baseLen = this.basePath.length;
75+
76+
const routes = this.routes[FILTER];
77+
routes[HANDLER] = [
78+
(req, res, next) => {
79+
req.url = req.url.substring(baseLen) || '/';
80+
next();
81+
},
82+
...routes[HANDLER]
83+
];
84+
}
85+
5686
add(verb, path, ...handlers) {
5787
if (Array.isArray(path)) {
5888
path.forEach((url) => {
5989
this.add(verb, url, ...handlers);
6090
});
61-
return;
91+
return null;
6292
}
6393

6494
handlers.forEach((handle) => {

0 commit comments

Comments
 (0)