Skip to content

Commit fcf128a

Browse files
committed
save
1 parent 5f832ac commit fcf128a

File tree

2 files changed

+34
-40
lines changed

2 files changed

+34
-40
lines changed

packages/tiny/router.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const strip = (str) => {
3131
return str.charCodeAt(len) === SLASH ? str.substr(0, len) : str;
3232
};
3333

34-
const split = (str) => ((str = strip(str)) === SEP ? [SEP] : str.split(SEP));
34+
const split = (str) => ((str = strip(str)) === SEP ? EMPTY_ARRAY : str.split(SEP));
3535

3636
module.exports = class Router {
3737
constructor(path = null) {
@@ -262,39 +262,33 @@ module.exports = class Router {
262262
// 然后applay自身所有Filter
263263

264264
const perpand = (route, handler) => {
265-
Object.keys(route).forEach((key) => {
266-
const target = route[key][HANDLER];
267-
if (target.length > 0) {
268-
route[key][HANDLER] = handler.concat(target);
269-
}
270-
});
271-
272-
if (route[PARAM]) {
273-
const target = route[PARAM][HANDLER];
274-
if (target.length > 0) {
275-
route[PARAM][HANDLER] = handler.concat(target);
276-
}
277-
}
278-
279265
{
280266
const target = route[HANDLER];
281267
if (target.length > 0) {
282268
route[HANDLER] = handler.concat(target);
283269
}
284270
}
271+
272+
Object.keys(route).forEach((key) => {
273+
perpand(route[key], handler);
274+
});
275+
276+
if (route[PARAM]) {
277+
perpand(route[PARAM], handler);
278+
}
285279
};
286280

287281
const walk = (way, filter) => {
288-
const handler = filter[HANDLER];
289-
if (way && handler.length > 0) {
290-
perpand(way, handler);
291-
}
292282
Object.keys(filter).forEach((key) => {
293283
walk(way[key], filter[key]);
294284
});
295285
if (filter[PARAM]) {
296286
walk(way[PARAM], filter[PARAM]);
297287
}
288+
const handler = filter[HANDLER];
289+
if (way && handler.length > 0) {
290+
perpand(way, handler);
291+
}
298292
};
299293

300294
const filter = ways[FILTER];

tests/tiny.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ tape('tiny::internals', (t) => {
5858
});
5959

6060
tape('tiny::usage::basic', (t) => {
61-
t.plan(1);
61+
t.plan(3);
6262

6363
const app = tiny();
6464
const arr = [['GET', '/'], ['POST', '/users'], ['PUT', '/users/:id']];
@@ -161,7 +161,7 @@ tape('tiny::usage::variadic', async (t) => {
161161
});
162162

163163
tape('tiny::usage::middleware', async (t) => {
164-
t.plan(21);
164+
t.plan(20);
165165

166166
const app = tiny()
167167
.filter((req, res, next) => {
@@ -171,16 +171,27 @@ tape('tiny::usage::middleware', async (t) => {
171171
(req.two = 'world') && next();
172172
})
173173
.filter('/about', (req, res, next) => {
174-
t.is(req.one, 'hello', '~> sub-mware runs after first global middleware');
175-
t.is(req.two, 'world', '~> sub-mware runs after second global middleware');
176-
res.end('About');
174+
t.is(req.one, 'hello', '~> sub-mware [/about] runs after first global middleware');
175+
t.is(req.two, 'world', '~> sub-mware [/about] runs after second global middleware');
176+
next();
177177
})
178178
.filter('/subgroup', (req, res, next) => {
179179
req.subgroup = true;
180-
t.is(req.one, 'hello', '~> sub-mware runs after first global middleware');
181-
t.is(req.two, 'world', '~> sub-mware runs after second global middleware');
180+
t.is(
181+
req.one,
182+
'hello',
183+
'~> sub-mware [/subgroup] runs after first global middleware'
184+
);
185+
t.is(
186+
req.two,
187+
'world',
188+
'~> sub-mware [/subgroup] runs after second global middleware'
189+
);
182190
next();
183191
})
192+
.get('/about', (req, res) => {
193+
res.end('About');
194+
})
184195
.post('/subgroup', (req, res) => {
185196
t.is(req.subgroup, true, '~~> POST /subgroup ran after its shared middleware');
186197
res.end('POST /subgroup');
@@ -222,7 +233,7 @@ tape('tiny::usage::middleware', async (t) => {
222233
});
223234

224235
tape('tiny::usage::middleware (async)', async (t) => {
225-
t.plan(7);
236+
t.plan(6);
226237

227238
const app = tiny()
228239
.filter((req, res, next) => {
@@ -247,8 +258,6 @@ tape('tiny::usage::middleware (async)', async (t) => {
247258
res.end('Hello');
248259
});
249260

250-
t.is(app.wares.length, 2, 'added 2 middleware functions');
251-
252261
app.build();
253262
app.listen(8080, 'localhost');
254263
const uri = 'http://localhost:8080';
@@ -262,7 +271,7 @@ tape('tiny::usage::middleware (async)', async (t) => {
262271
});
263272

264273
tape('tiny::usage::middleware (basenames)', async (t) => {
265-
t.plan(40);
274+
t.plan(37);
266275

267276
let chk = false;
268277
const aaa = (req, res, next) => ((req.aaa = 'aaa'), next());
@@ -317,15 +326,6 @@ tape('tiny::usage::middleware (basenames)', async (t) => {
317326
res.end('hello from main');
318327
});
319328

320-
t.is(app.wares.length, 3, 'added 3 global middleware functions');
321-
const keys = Object.keys(app.bwares);
322-
t.is(keys.length, 2, 'added 2 basename middleware groups');
323-
t.deepEqual(
324-
keys,
325-
['/foo', '/bar'],
326-
'~> has middleware groups for `/foo` & `/bar` path matches'
327-
);
328-
329329
app.build();
330330
app.listen(8080, 'localhost');
331331
const uri = 'http://localhost:8080';
@@ -381,7 +381,7 @@ tape('tiny::usage::middleware (wildcard)', async (t) => {
381381
);
382382
res.end('hello from bar');
383383
})
384-
.get('*', (req, res) => {
384+
.get('*all', (req, res) => {
385385
// runs 3x
386386
t.pass('runs the MAIN app handler for GET /*');
387387
t.is(req.foo, 'foo', '~> runs after `foo` global middleware');

0 commit comments

Comments
 (0)