UNPKG

2.71 kBJavaScriptView Raw
1(function() {
2 var Reader, Response, ResponseReader, Writer, _,
3 __hasProp = Object.prototype.hasOwnProperty,
4 __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
5
6 Writer = require("../io/writer");
7
8 Reader = require("../io/reader");
9
10 _ = require("underscore");
11
12 ResponseReader = (function(_super) {
13
14 __extends(ResponseReader, _super);
15
16 function ResponseReader() {
17 ResponseReader.__super__.constructor.apply(this, arguments);
18 }
19
20 /*
21 */
22
23 ResponseReader.prototype._listenTo = function() {
24 return ResponseReader.__super__._listenTo.call(this).concat("headers");
25 };
26
27 /*
28 */
29
30 ResponseReader.prototype._listen = function() {
31 var _this = this;
32 ResponseReader.__super__._listen.call(this);
33 return this.on("headers", function(headers) {
34 return _this.headers = headers;
35 });
36 };
37
38 ResponseReader.prototype._dumpCached = function(pipedReader) {
39 if (this.headers) pipedReader.emit("headers", this.headers);
40 return ResponseReader.__super__._dumpCached.call(this, pipedReader);
41 };
42
43 return ResponseReader;
44
45 })(Reader);
46
47 module.exports = Response = (function(_super) {
48
49 __extends(Response, _super);
50
51 /*
52 */
53
54 function Response(messenger) {
55 this.messenger = messenger;
56 Response.__super__.constructor.call(this);
57 this._headers = {};
58 }
59
60 /*
61 */
62
63 Response.prototype.headers = function(typeOrObj, value) {
64 if (typeof typeOrObj === "object") {
65 return _.extend(this._headers, typeOrObj);
66 } else {
67 return this._headers[typeOfObj] = value;
68 }
69 };
70
71 /*
72 */
73
74 Response.prototype.write = function(chunk, encoding) {
75 if (encoding == null) encoding = "utf8";
76 this.sendHeaders();
77 return Response.__super__.write.call(this, chunk, encoding);
78 };
79
80 /*
81 */
82
83 Response.prototype.end = function(chunk, encoding) {
84 if (encoding == null) encoding = "utf8";
85 this.sendHeaders();
86 return Response.__super__.end.call(this, chunk, encoding);
87 };
88
89 /*
90 */
91
92 Response.prototype.sendHeaders = function() {
93 if (this.sentHeaders) return this;
94 this.sentHeaders = true;
95 this.emit("headers", this._headers);
96 return this;
97 };
98
99 /*
100 */
101
102 Response.prototype.reader = function() {
103 return new ResponseReader(this);
104 };
105
106 return Response;
107
108 })(Writer);
109
110 Writer.prototype.writable = true;
111
112}).call(this);