resources.js
4.75 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// History of resources loaded by window.
'use strict';
var _createClass = require('babel-runtime/helpers/create-class')['default'];
var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];
var _get = require('babel-runtime/helpers/get')['default'];
var _inherits = require('babel-runtime/helpers/inherits')['default'];
var _slicedToArray = require('babel-runtime/helpers/sliced-to-array')['default'];
var _getIterator = require('babel-runtime/core-js/get-iterator')['default'];
var Fetch = require('./fetch');
// Each resource is associated with request, and on completion response or error.
var Resource = (function () {
function Resource(_ref) {
var request = _ref.request;
_classCallCheck(this, Resource);
this.request = request;
this.error = null;
this.response = null;
}
// Each window has a resources object that provides the means for retrieving
// resources and a list of all retrieved resources.
//
// The object is an array, and its elements are the resources.
// The URL of this resource
_createClass(Resource, [{
key: 'dump',
// Dump the resource to output stream/stdout
value: function dump() {
var output = arguments.length <= 0 || arguments[0] === undefined ? process.stdout : arguments[0];
var request = this.request;
var response = this.response;
var error = this.error;
// Write summary request/response header
if (response) {
var elapsed = response.time - request.time;
output.write(request.method + ' ' + this.url + ' - ' + response.status + ' ' + response.statusText + ' - ' + elapsed + 'ms\n');
} else output.write(request.method + ' ' + this.url + '\n');
// If response, write out response headers and sample of document entity
// If error, write out the error message
// Otherwise, indicate this is a pending request
if (response) {
if (response._redirectCount) output.write(' Followed ' + response._redirectCount + ' redirects\n');
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = _getIterator(response.headers), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _step$value = _slicedToArray(_step.value, 2);
var _name = _step$value[0];
var value = _step$value[1];
output.write(' ' + _name + ': ' + value + '\n');
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
output.write('\n');
var sample = response.body.slice(0, 250).toString('utf8').split('\n').map(function (line) {
return ' ' + line;
}).join('\n');
output.write(sample);
} else if (error) output.write(' Error: ' + error.message + '\n');else output.write(' Pending since ' + new Date(request.time) + '\n');
// Keep them separated
output.write('\n\n');
}
}, {
key: 'url',
get: function get() {
return this.response && this.response.url || this.request.url;
}
}]);
return Resource;
})();
var Resources = (function (_Array) {
_inherits(Resources, _Array);
function Resources(window) {
_classCallCheck(this, Resources);
_get(Object.getPrototypeOf(Resources.prototype), 'constructor', this).call(this);
this._browser = window.browser;
}
_createClass(Resources, [{
key: '_fetch',
value: function _fetch(input, init) {
var pipeline = this._browser.pipeline;
var request = new Fetch.Request(input, init);
var resource = new Resource({ request: request });
this.push(resource);
return pipeline._fetch(request).then(function (response) {
resource.response = response;
return response;
})['catch'](function (error) {
resource.error = error;
resource.response = Fetch.Response.error();
throw error;
});
}
// Human readable resource listing.
//
// output - Write to this stream (optional)
}, {
key: 'dump',
value: function dump() {
var output = arguments.length <= 0 || arguments[0] === undefined ? process.stdout : arguments[0];
if (this.length === 0) output.write('No resources\n');else this.forEach(function (resource) {
return resource.dump(output);
});
}
}]);
return Resources;
})(Array);
module.exports = Resources;
//# sourceMappingURL=resources.js.map