-
-
Notifications
You must be signed in to change notification settings - Fork 10
Connect likeの実装 #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Connect likeの実装 #65
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4902598
feat(connect): add junction
azu 6d33c42
feat(connect): implement junction
azu e2fc2a0
fix(connect): use let insteadof var
azu c3a3d84
refactor(connect): textというtypeに限定しないように
azu 783fd57
test(connect): add junction test
azu cbfc8d7
fix(connect): correct errorHandler
azu 9e0acaf
chore(connect): use response object instead of string.
azu f33eb8f
fix(connect): エラーハンドリングの呼び出し順にについてを修正
azu 1352536
feat(connect): add junction example
azu 6463df7
feat(connect): 実装してみようを追加
azu 69d334e
fix(connect): ミドルウェア -> middleware
azu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
"use strict"; | ||
export default function () { | ||
return function errorHandling(err, req, res, next) { | ||
console.error(err.stack); | ||
res.status(500).send(err.message); | ||
res.writeHead(404); | ||
res.write(err.message); | ||
res.end(); | ||
next(); | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use strict"; | ||
import Junction from "./junction"; | ||
import assert from "assert"; | ||
let junction = new Junction(); | ||
junction.use(function toUpperCase(res, next) { | ||
res.value = res.value.toUpperCase(); | ||
next(); | ||
}); | ||
junction.use(function exclamationMark(res, next) { | ||
res.value = res.value + "!"; | ||
next(); | ||
}); | ||
junction.use(function errorHandling(error, res, next) { | ||
console.error(error.stack); | ||
next(); | ||
}); | ||
|
||
let text = "hello world"; | ||
junction.process(text, function (error, result) { | ||
if (error) { | ||
console.error(error); | ||
} | ||
let value = result.value; | ||
assert.equal(value, "HELLO WORLD!"); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"use strict"; | ||
function isErrorHandingMiddleware(middleware) { | ||
// middleware(error, text, next) | ||
let arity = middleware.length; | ||
return arity === 3; | ||
} | ||
function applyMiddleware(error, response, middleware, next) { | ||
let errorOnMiddleware = null; | ||
try { | ||
if (error && isErrorHandingMiddleware(middleware)) { | ||
middleware(error, response, next); | ||
} else { | ||
middleware(response, next); | ||
} | ||
return; | ||
} catch (error) { | ||
errorOnMiddleware = error; | ||
} | ||
// skip the middleware or Error on the middleware | ||
next(errorOnMiddleware, response); | ||
} | ||
|
||
export default class Junction { | ||
constructor() { | ||
this.stack = []; | ||
} | ||
|
||
use(middleware) { | ||
this.stack.push(middleware); | ||
} | ||
|
||
process(initialValue, callback) { | ||
let response = {value: initialValue}; | ||
let next = (error) => { | ||
let middleware = this.stack.shift(); | ||
if (!middleware) { | ||
return callback(error, response); | ||
} | ||
applyMiddleware(error, response, middleware, next); | ||
}; | ||
next(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// LICENSE : MIT | ||
"use strict"; | ||
import assert from "power-assert"; | ||
import Junction from "../../src/connect/junction"; | ||
describe("junction", function () { | ||
context("when register middlewares", function () { | ||
it("should connect middleware, the order is register", function (done) { | ||
let junction = new Junction(); | ||
junction.use(function errorHandling(error, text, next) { | ||
next(error); | ||
}); | ||
junction.use(function toUpper(res, next) { | ||
res.value = res.value.toLocaleUpperCase(); | ||
next(); | ||
}); | ||
junction.use(function addDesu(res, next) { | ||
res.value += " suffix"; | ||
next(); | ||
}); | ||
junction.process("text", (error, result) => { | ||
if (error) { | ||
return done(error); | ||
} | ||
assert.equal(result.value, "TEXT suffix"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
context("when occur error in middleware", function () { | ||
it("should call errorHandling middleware", function (done) { | ||
let junction = new Junction(); | ||
junction.use(function toUpper(res) { | ||
throw new Error("error on " + res); | ||
}); | ||
junction.use(function errorHandling(error, res, next) { | ||
assert(error instanceof Error); | ||
assert.equal(res.value, "text"); | ||
next(); | ||
}); | ||
junction.process("text", (error, res) => { | ||
if (error) { | ||
return done(error); | ||
} | ||
assert.equal(res.value, "text"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ミドルウェア…