Last active
August 29, 2015 13:57
-
-
Save jkrems/9691864 to your computer and use it in GitHub Desktop.
Spidernode demo
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
Stream.pipe = async function(source, target) { | |
let chunk; | |
// source.read() will throw if there's an error | |
// reading data | |
while (chunk = source.read()) { | |
target.write(await chunk); | |
} | |
// .end will throw when there were errors | |
return target.end(); | |
}; | |
Stream.concat = async function(stream) { | |
let chunks = [], chunk; | |
while (chunk = source.read()) { | |
chunks.push(await chunk); | |
} | |
return Buffer.concat(chunks); | |
}; | |
FS.readStream('foo.txt').pipe(process.stdout); | |
const server = Http.createServer(async function(req) { | |
let body = Stream.concat(req); | |
return { | |
headers: req.headers, | |
body: body // Promise<Buffer> | |
status: Http.OK // 200 | |
}; | |
}); | |
server.on('request', function(req) { | |
// can instrument things, can mutate request | |
// may choose to delay handling the request | |
}); | |
server.on('response', function(res, req) { | |
// can instrument things, can mutate response | |
// may choose to delay sending the response | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment