Skip to content

Instantly share code, notes, and snippets.

@jkrems
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkrems/9691864 to your computer and use it in GitHub Desktop.
Save jkrems/9691864 to your computer and use it in GitHub Desktop.
Spidernode demo
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