-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (40 loc) · 932 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const messagesByCode = require('http').STATUS_CODES;
const pull = require('pull-stream');
const toPullSink = require('stream-to-pull-stream').sink;
const stringify = require('pull-json-stringify')();
module.exports = send;
function send( response, {
// headers
code = 200,
message = messagesByCode[code],
headers,
// body
json,
raw,
}, cb){
if (json !== undefined) {
response.setHeader('Content-Type', 'application/x-ndjson; charset=utf-8');
response.writeHead(code, message, headers);
if (typeof json === 'function')
return pull(
json,
stringify,
toPullSink(response, cb)
);
response.end(JSON.stringify(json)+'\n');
return cb();
}
response.writeHead(code, message, headers);
if (raw !== undefined) {
if (typeof raw === 'function')
return pull(
raw,
toPullSink(response, cb)
);
response.end(raw);
return cb();
}
response.end();
return cb();
}