@opra/core 0.19.0 → 0.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/adapter/http/http-message.host.js +251 -0
- package/cjs/adapter/http/http-request-message.js +152 -0
- package/cjs/adapter/http/http-response-message.js +238 -0
- package/cjs/index.js +2 -0
- package/esm/adapter/http/http-message.host.js +246 -0
- package/esm/adapter/http/http-request-message.js +148 -0
- package/esm/adapter/http/http-response-message.js +233 -0
- package/esm/index.js +2 -0
- package/package.json +15 -3
- package/types/adapter/http/http-adapter.d.ts +2 -1
- package/types/adapter/http/http-message.host.d.ts +122 -0
- package/types/adapter/http/http-request-message.d.ts +213 -0
- package/types/adapter/http/http-request.host.d.ts +1 -1
- package/types/adapter/http/http-response-message.d.ts +321 -0
- package/types/adapter/http/http-response.host.d.ts +1 -1
- package/types/adapter/interfaces/request-context.interface.d.ts +3 -1
- package/types/adapter/interfaces/request.interface.d.ts +2 -1
- package/types/adapter/interfaces/response.interface.d.ts +1 -1
- package/types/adapter/request.host.d.ts +2 -1
- package/types/adapter/response.host.d.ts +1 -1
- package/types/index.d.ts +2 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpMessageHost = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const stream = tslib_1.__importStar(require("stream"));
|
|
6
|
+
const http_parser_1 = require("@browsery/http-parser");
|
|
7
|
+
const common_1 = require("@opra/common");
|
|
8
|
+
const kHeaders = Symbol('kHeaders');
|
|
9
|
+
const kHeadersProxy = Symbol('kHeadersProxy');
|
|
10
|
+
const kTrailers = Symbol('kTrailers');
|
|
11
|
+
const kTrailersProxy = Symbol('kTrailersProxy');
|
|
12
|
+
const kOnHeaderReceived = Symbol('kOnHeaderReceived');
|
|
13
|
+
const kOnTrailersReceived = Symbol('kOnTrailersReceived');
|
|
14
|
+
const kOnBodyChunk = Symbol('kOnBodyChunk');
|
|
15
|
+
const kOnReadComplete = Symbol('kOnReadComplete');
|
|
16
|
+
const crlfBuffer = Buffer.from('\r\n');
|
|
17
|
+
const HTTP_VERSION_PATTERN = /^(\d)\.(\d)$/;
|
|
18
|
+
class HttpMessageHost {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.complete = false;
|
|
21
|
+
stream.Duplex.apply(this);
|
|
22
|
+
this[kHeaders] = new common_1.HttpHeaders(undefined, {
|
|
23
|
+
onChange: () => this._headersChanged = true
|
|
24
|
+
});
|
|
25
|
+
this[kTrailers] = new common_1.HttpHeaders(undefined, {
|
|
26
|
+
onChange: () => this._trailersChanged = true
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
get httpVersion() {
|
|
30
|
+
return this.httpVersionMajor
|
|
31
|
+
? this.httpVersionMajor + '.' + (this.httpVersionMinor || 0)
|
|
32
|
+
: undefined;
|
|
33
|
+
}
|
|
34
|
+
set httpVersion(value) {
|
|
35
|
+
if (value) {
|
|
36
|
+
const m = HTTP_VERSION_PATTERN.exec(value);
|
|
37
|
+
if (!m)
|
|
38
|
+
throw new TypeError(`Invalid http version string (${value})`);
|
|
39
|
+
this.httpVersionMajor = parseInt(m[1], 10);
|
|
40
|
+
this.httpVersionMinor = parseInt(m[2], 10);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this.httpVersionMajor = undefined;
|
|
44
|
+
this.httpVersionMinor = undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
get headers() {
|
|
48
|
+
this._initHeaders();
|
|
49
|
+
return this[kHeadersProxy];
|
|
50
|
+
}
|
|
51
|
+
set headers(headers) {
|
|
52
|
+
this[kHeaders].clear();
|
|
53
|
+
this[kHeaders].set(headers);
|
|
54
|
+
}
|
|
55
|
+
get trailers() {
|
|
56
|
+
this._initTrailers();
|
|
57
|
+
return this[kTrailersProxy];
|
|
58
|
+
}
|
|
59
|
+
set trailers(trailers) {
|
|
60
|
+
this[kTrailers].clear();
|
|
61
|
+
this[kTrailers].set(trailers);
|
|
62
|
+
}
|
|
63
|
+
get rawHeaders() {
|
|
64
|
+
this._buildRawHeaders();
|
|
65
|
+
return this._rawHeaders;
|
|
66
|
+
}
|
|
67
|
+
set rawHeaders(headers) {
|
|
68
|
+
this[kHeadersProxy] = undefined;
|
|
69
|
+
this._headersChanged = false;
|
|
70
|
+
this._rawHeaders = headers;
|
|
71
|
+
}
|
|
72
|
+
get rawTrailers() {
|
|
73
|
+
this._buildRawTrailers();
|
|
74
|
+
return this._rawTrailers;
|
|
75
|
+
}
|
|
76
|
+
set rawTrailers(trailers) {
|
|
77
|
+
this[kTrailersProxy] = undefined;
|
|
78
|
+
this._trailersChanged = false;
|
|
79
|
+
this._rawTrailers = trailers;
|
|
80
|
+
}
|
|
81
|
+
getHeader(name) {
|
|
82
|
+
if (!name)
|
|
83
|
+
return;
|
|
84
|
+
this._initHeaders();
|
|
85
|
+
switch (name.toLowerCase()) {
|
|
86
|
+
case 'referer':
|
|
87
|
+
case 'referrer':
|
|
88
|
+
return this[kHeaders].get(name) ||
|
|
89
|
+
this[kHeaders].get('referrer') ||
|
|
90
|
+
this[kHeaders].get('referer');
|
|
91
|
+
default:
|
|
92
|
+
return this[kHeaders].get(name);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
get(name) {
|
|
96
|
+
this._initHeaders();
|
|
97
|
+
return this[kHeaders].get(name);
|
|
98
|
+
}
|
|
99
|
+
setHeader(arg0, arg1) {
|
|
100
|
+
this._initHeaders();
|
|
101
|
+
if (typeof arg0 === 'object')
|
|
102
|
+
this[kHeaders].set(arg0);
|
|
103
|
+
else
|
|
104
|
+
this[kHeaders].set(arg0, arg1);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
set(arg0, arg1) {
|
|
108
|
+
return this.setHeader(arg0, arg1);
|
|
109
|
+
}
|
|
110
|
+
getHeaders() {
|
|
111
|
+
this._initHeaders();
|
|
112
|
+
return this[kHeaders].toObject();
|
|
113
|
+
}
|
|
114
|
+
getHeaderNames() {
|
|
115
|
+
this._initHeaders();
|
|
116
|
+
return Array.from(this[kHeaders].keys());
|
|
117
|
+
}
|
|
118
|
+
hasHeader(name) {
|
|
119
|
+
this._initHeaders();
|
|
120
|
+
return this[kHeaders].has(name);
|
|
121
|
+
}
|
|
122
|
+
removeHeader(name) {
|
|
123
|
+
this._initHeaders();
|
|
124
|
+
this[kHeaders].delete(name);
|
|
125
|
+
}
|
|
126
|
+
send(body) {
|
|
127
|
+
this.body = body;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
end(body) {
|
|
131
|
+
if (body)
|
|
132
|
+
this.body = body;
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
setTimeout() {
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
_init(args) {
|
|
139
|
+
this.complete = true;
|
|
140
|
+
this.httpVersionMajor = args?.httpVersionMajor;
|
|
141
|
+
this.httpVersionMinor = args?.httpVersionMinor;
|
|
142
|
+
this._rawHeaders = args.rawHeaders;
|
|
143
|
+
this._rawTrailers = args.rawTrailers;
|
|
144
|
+
if (args.headers)
|
|
145
|
+
this[kHeaders].set(args.headers);
|
|
146
|
+
if (args.trailers)
|
|
147
|
+
this[kTrailers].set(args.trailers);
|
|
148
|
+
this.body = args.body;
|
|
149
|
+
}
|
|
150
|
+
_parseBuffer(buf, parserType) {
|
|
151
|
+
const parser = new http_parser_1.HTTPParser(parserType);
|
|
152
|
+
parser[http_parser_1.HTTPParser.kOnHeadersComplete] = this[kOnHeaderReceived].bind(this);
|
|
153
|
+
parser[http_parser_1.HTTPParser.kOnBody] = this[kOnBodyChunk].bind(this);
|
|
154
|
+
parser[http_parser_1.HTTPParser.kOnHeaders] = this[kOnTrailersReceived].bind(this);
|
|
155
|
+
parser[http_parser_1.HTTPParser.kOnMessageComplete] = this[kOnReadComplete].bind(this);
|
|
156
|
+
const buffer = Buffer.from(buf);
|
|
157
|
+
let x = parser.execute(buffer);
|
|
158
|
+
if (typeof x === 'object')
|
|
159
|
+
throw x;
|
|
160
|
+
if (!this.complete) {
|
|
161
|
+
x = parser.execute(crlfBuffer);
|
|
162
|
+
if (typeof x === 'object')
|
|
163
|
+
throw x;
|
|
164
|
+
}
|
|
165
|
+
parser.finish();
|
|
166
|
+
}
|
|
167
|
+
_initHeaders() {
|
|
168
|
+
if (!this[kHeadersProxy]) {
|
|
169
|
+
this[kHeadersProxy] = this[kHeaders].getProxy();
|
|
170
|
+
if (this._rawHeaders) {
|
|
171
|
+
const src = this._rawHeaders;
|
|
172
|
+
const l = Math.floor(src.length / 2);
|
|
173
|
+
for (let n = 0; n <= l; n += 2) {
|
|
174
|
+
this[kHeaders].append(src[n], src[n + 1]);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
_initTrailers() {
|
|
180
|
+
if (!this[kTrailersProxy]) {
|
|
181
|
+
this[kTrailersProxy] = this[kTrailers].getProxy();
|
|
182
|
+
if (this._rawTrailers) {
|
|
183
|
+
const src = this._rawTrailers;
|
|
184
|
+
const l = Math.floor(src.length / 2);
|
|
185
|
+
for (let n = 0; n <= l; n += 2) {
|
|
186
|
+
this[kTrailers].append(src[n], src[n + 1]);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
_buildRawHeaders() {
|
|
192
|
+
// Rebuild rawHeaders if headers object changed
|
|
193
|
+
if (this._headersChanged || !this._rawHeaders) {
|
|
194
|
+
this._headersChanged = false;
|
|
195
|
+
this._rawHeaders = Object.entries(this.headers)
|
|
196
|
+
.reduce((a, [k, v]) => {
|
|
197
|
+
if (Array.isArray(v))
|
|
198
|
+
v.forEach(x => a.push(k, String(x)));
|
|
199
|
+
else
|
|
200
|
+
a.push(k, String(v));
|
|
201
|
+
return a;
|
|
202
|
+
}, []);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
_buildRawTrailers() {
|
|
206
|
+
// Rebuild rawHeaders if headers object changed
|
|
207
|
+
if (this._trailersChanged || !this._rawTrailers) {
|
|
208
|
+
this._trailersChanged = false;
|
|
209
|
+
this._rawTrailers = Object.entries(this.trailers)
|
|
210
|
+
.reduce((a, [k, v]) => {
|
|
211
|
+
if (Array.isArray(v))
|
|
212
|
+
v.forEach(x => a.push(k, String(x)));
|
|
213
|
+
else
|
|
214
|
+
a.push(k, String(v));
|
|
215
|
+
return a;
|
|
216
|
+
}, []);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
[kOnHeaderReceived](info) {
|
|
220
|
+
this.httpVersionMajor = info.versionMajor;
|
|
221
|
+
this.httpVersionMinor = info.versionMinor;
|
|
222
|
+
this._rawHeaders = info.headers;
|
|
223
|
+
this.shouldKeepAlive = info.shouldKeepAlive;
|
|
224
|
+
this.upgrade = info.upgrade;
|
|
225
|
+
}
|
|
226
|
+
[kOnTrailersReceived](trailers) {
|
|
227
|
+
this._rawTrailers = trailers;
|
|
228
|
+
}
|
|
229
|
+
[kOnBodyChunk](chunk, offset, length) {
|
|
230
|
+
this._bodyChunks = this._bodyChunks || [];
|
|
231
|
+
this._bodyChunks.push(chunk.subarray(offset, offset + length));
|
|
232
|
+
}
|
|
233
|
+
[kOnReadComplete]() {
|
|
234
|
+
this.complete = true;
|
|
235
|
+
if (this._bodyChunks) {
|
|
236
|
+
this.body = Buffer.concat(this._bodyChunks);
|
|
237
|
+
this._bodyChunks = undefined;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.HttpMessageHost = HttpMessageHost;
|
|
242
|
+
HttpMessageHost.kHeaders = kHeaders;
|
|
243
|
+
HttpMessageHost.kHeadersProxy = kHeadersProxy;
|
|
244
|
+
HttpMessageHost.kTrailers = kTrailers;
|
|
245
|
+
HttpMessageHost.kTrailersProxy = kTrailersProxy;
|
|
246
|
+
HttpMessageHost.kOnHeaderReceived = kOnHeaderReceived;
|
|
247
|
+
HttpMessageHost.kOnTrailersReceived = kOnTrailersReceived;
|
|
248
|
+
HttpMessageHost.kOnBodyChunk = kOnBodyChunk;
|
|
249
|
+
HttpMessageHost.kOnReadComplete = kOnReadComplete;
|
|
250
|
+
// Mixin with Duplex
|
|
251
|
+
Object.assign(HttpMessageHost.prototype, stream.Duplex.prototype);
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpRequestMessage = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const accepts_1 = tslib_1.__importDefault(require("accepts"));
|
|
6
|
+
const type_is_1 = tslib_1.__importDefault(require("type-is"));
|
|
7
|
+
const http_parser_1 = require("@browsery/http-parser");
|
|
8
|
+
const common_1 = require("@opra/common");
|
|
9
|
+
const http_message_host_js_1 = require("./http-message.host.js");
|
|
10
|
+
const HTTP_VERSION_PATTERN = /^(\d)\.(\d)$/;
|
|
11
|
+
var HttpRequestMessage;
|
|
12
|
+
(function (HttpRequestMessage) {
|
|
13
|
+
function create(init) {
|
|
14
|
+
return HttpRequestMessageHost.create(init);
|
|
15
|
+
}
|
|
16
|
+
HttpRequestMessage.create = create;
|
|
17
|
+
function fromBuffer(buffer) {
|
|
18
|
+
return HttpRequestMessageHost.fromBuffer(buffer);
|
|
19
|
+
}
|
|
20
|
+
HttpRequestMessage.fromBuffer = fromBuffer;
|
|
21
|
+
async function fromStream(readable) {
|
|
22
|
+
return HttpRequestMessageHost.fromStream(readable);
|
|
23
|
+
}
|
|
24
|
+
HttpRequestMessage.fromStream = fromStream;
|
|
25
|
+
})(HttpRequestMessage || (exports.HttpRequestMessage = HttpRequestMessage = {}));
|
|
26
|
+
const kQuery = Symbol('kQuery');
|
|
27
|
+
const kQueryProxy = Symbol('kQueryProxy');
|
|
28
|
+
const kProtocol = Symbol('kProtocol');
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
class HttpRequestMessageHost extends http_message_host_js_1.HttpMessageHost {
|
|
33
|
+
constructor() {
|
|
34
|
+
super();
|
|
35
|
+
this.httpVersionMajor = 1;
|
|
36
|
+
this.httpVersionMinor = 0;
|
|
37
|
+
this[kQuery] = new common_1.HttpParams();
|
|
38
|
+
this[kQueryProxy] = this[kQuery].getProxy();
|
|
39
|
+
}
|
|
40
|
+
get httpVersion() {
|
|
41
|
+
return this.httpVersionMajor + '.' + this.httpVersionMinor;
|
|
42
|
+
}
|
|
43
|
+
set httpVersion(value) {
|
|
44
|
+
const m = HTTP_VERSION_PATTERN.exec(value);
|
|
45
|
+
if (!m)
|
|
46
|
+
throw new TypeError(`Invalid http version string (${value})`);
|
|
47
|
+
this.httpVersionMajor = parseInt(m[1], 10);
|
|
48
|
+
this.httpVersionMinor = parseInt(m[2], 10);
|
|
49
|
+
}
|
|
50
|
+
get hostname() {
|
|
51
|
+
let host = this.get('X-Forwarded-Host') || this.get('Host');
|
|
52
|
+
if (host && host.indexOf(',') !== -1) {
|
|
53
|
+
// Note: X-Forwarded-Host is normally only ever a single value, but this is to be safe.
|
|
54
|
+
host = host.substring(0, host.indexOf(',')).trimEnd();
|
|
55
|
+
}
|
|
56
|
+
if (!host)
|
|
57
|
+
return '';
|
|
58
|
+
// IPv6 literal support
|
|
59
|
+
const offset = host[0] === '['
|
|
60
|
+
? host.indexOf(']') + 1
|
|
61
|
+
: 0;
|
|
62
|
+
const index = host.indexOf(':', offset);
|
|
63
|
+
return index !== -1
|
|
64
|
+
? host.substring(0, index)
|
|
65
|
+
: host;
|
|
66
|
+
}
|
|
67
|
+
get protocol() {
|
|
68
|
+
if (this[kProtocol])
|
|
69
|
+
return this[kProtocol];
|
|
70
|
+
// Note: X-Forwarded-Proto is normally only ever a
|
|
71
|
+
// single value, but this is to be safe.
|
|
72
|
+
const header = this.get('X-Forwarded-Proto');
|
|
73
|
+
if (header) {
|
|
74
|
+
const index = header.indexOf(',');
|
|
75
|
+
return index !== -1
|
|
76
|
+
? header.substring(0, index).trim()
|
|
77
|
+
: header.trim();
|
|
78
|
+
}
|
|
79
|
+
return 'http';
|
|
80
|
+
}
|
|
81
|
+
set protocol(v) {
|
|
82
|
+
this[kProtocol] = v;
|
|
83
|
+
}
|
|
84
|
+
get secure() {
|
|
85
|
+
return this.protocol === 'https';
|
|
86
|
+
}
|
|
87
|
+
get query() {
|
|
88
|
+
return this[kQueryProxy];
|
|
89
|
+
}
|
|
90
|
+
set query(value) {
|
|
91
|
+
this[kQuery].clear();
|
|
92
|
+
this[kQuery].appendAll(value);
|
|
93
|
+
}
|
|
94
|
+
header(name) {
|
|
95
|
+
return this.getHeader(name);
|
|
96
|
+
}
|
|
97
|
+
accepts(...type) {
|
|
98
|
+
const accept = (0, accepts_1.default)(this);
|
|
99
|
+
// eslint-disable-next-line prefer-spread
|
|
100
|
+
return accept.types.apply(accept, type);
|
|
101
|
+
}
|
|
102
|
+
acceptsCharsets(...charset) {
|
|
103
|
+
const accept = (0, accepts_1.default)(this);
|
|
104
|
+
// eslint-disable-next-line prefer-spread
|
|
105
|
+
return accept.charsets.apply(accept, charset);
|
|
106
|
+
}
|
|
107
|
+
acceptsEncodings(...encoding) {
|
|
108
|
+
const accept = (0, accepts_1.default)(this);
|
|
109
|
+
// eslint-disable-next-line prefer-spread
|
|
110
|
+
return accept.encodings.apply(accept, encoding);
|
|
111
|
+
}
|
|
112
|
+
acceptsLanguages(...lang) {
|
|
113
|
+
const accept = (0, accepts_1.default)(this);
|
|
114
|
+
// eslint-disable-next-line prefer-spread
|
|
115
|
+
return accept.languages.apply(accept, lang);
|
|
116
|
+
}
|
|
117
|
+
is(type, ...otherTypes) {
|
|
118
|
+
const types = Array.isArray(type) ? type : [type];
|
|
119
|
+
if (otherTypes.length)
|
|
120
|
+
types.push(...otherTypes);
|
|
121
|
+
const contentType = this.getHeader('content-type');
|
|
122
|
+
return contentType ? type_is_1.default.is(contentType, types) : null;
|
|
123
|
+
}
|
|
124
|
+
_init(init) {
|
|
125
|
+
super._init(init);
|
|
126
|
+
this.method = init.method.toUpperCase();
|
|
127
|
+
this.url = init.url;
|
|
128
|
+
this.baseUrl = init.baseUrl || this.url;
|
|
129
|
+
this.protocol = init.protocol;
|
|
130
|
+
}
|
|
131
|
+
[http_message_host_js_1.HttpMessageHost.kOnHeaderReceived](info) {
|
|
132
|
+
super[http_message_host_js_1.HttpMessageHost.kOnHeaderReceived](info);
|
|
133
|
+
this.method = http_parser_1.HTTPParser.methods[info.method];
|
|
134
|
+
this.url = info.url;
|
|
135
|
+
this.baseUrl = info.url;
|
|
136
|
+
}
|
|
137
|
+
static create(init) {
|
|
138
|
+
const msg = new HttpRequestMessageHost();
|
|
139
|
+
msg._init(init);
|
|
140
|
+
return msg;
|
|
141
|
+
}
|
|
142
|
+
static fromBuffer(buffer) {
|
|
143
|
+
const msg = new HttpRequestMessageHost();
|
|
144
|
+
msg._parseBuffer(buffer, http_parser_1.HTTPParser.REQUEST);
|
|
145
|
+
return msg;
|
|
146
|
+
}
|
|
147
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
148
|
+
static async fromStream(readable) {
|
|
149
|
+
throw new Error('fromStream is not implemented yet');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
HttpRequestMessageHost.kQuery = kQuery;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpResponseMessageHost = exports.HttpResponseMessage = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const content_disposition_1 = tslib_1.__importDefault(require("content-disposition"));
|
|
6
|
+
const cookie_1 = tslib_1.__importDefault(require("cookie"));
|
|
7
|
+
const cookie_signature_1 = tslib_1.__importDefault(require("cookie-signature"));
|
|
8
|
+
const encodeurl_1 = tslib_1.__importDefault(require("encodeurl"));
|
|
9
|
+
const mime_types_1 = tslib_1.__importDefault(require("mime-types"));
|
|
10
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
11
|
+
const http_parser_1 = require("@browsery/http-parser");
|
|
12
|
+
const common_1 = require("@opra/common");
|
|
13
|
+
const http_message_host_js_1 = require("./http-message.host.js");
|
|
14
|
+
/**
|
|
15
|
+
* @namespace HttpResponseMessage
|
|
16
|
+
*/
|
|
17
|
+
var HttpResponseMessage;
|
|
18
|
+
(function (HttpResponseMessage) {
|
|
19
|
+
function create(init) {
|
|
20
|
+
return HttpResponseMessageHost.create(init);
|
|
21
|
+
}
|
|
22
|
+
HttpResponseMessage.create = create;
|
|
23
|
+
function fromBuffer(buffer) {
|
|
24
|
+
return HttpResponseMessageHost.fromBuffer(buffer);
|
|
25
|
+
}
|
|
26
|
+
HttpResponseMessage.fromBuffer = fromBuffer;
|
|
27
|
+
async function fromStream(readable) {
|
|
28
|
+
return HttpResponseMessageHost.fromStream(readable);
|
|
29
|
+
}
|
|
30
|
+
HttpResponseMessage.fromStream = fromStream;
|
|
31
|
+
})(HttpResponseMessage || (exports.HttpResponseMessage = HttpResponseMessage = {}));
|
|
32
|
+
/**
|
|
33
|
+
* @class HttpResponseMessageHost
|
|
34
|
+
*/
|
|
35
|
+
class HttpResponseMessageHost extends http_message_host_js_1.HttpMessageHost {
|
|
36
|
+
constructor() {
|
|
37
|
+
super();
|
|
38
|
+
}
|
|
39
|
+
header(arg0, arg1) {
|
|
40
|
+
return this.set(arg0, arg1);
|
|
41
|
+
}
|
|
42
|
+
append(name, value) {
|
|
43
|
+
this[http_message_host_js_1.HttpMessageHost.kHeaders].append(name, value);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Set "Content-Disposition" header to "attachment" with optional `filename`.
|
|
48
|
+
*/
|
|
49
|
+
attachment(filename) {
|
|
50
|
+
if (filename)
|
|
51
|
+
this.type(path_1.default.extname(filename));
|
|
52
|
+
this.set('Content-Disposition', (0, content_disposition_1.default)(filename));
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Alias for msg.type()
|
|
57
|
+
*/
|
|
58
|
+
contentType(type) {
|
|
59
|
+
return this.type(type);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Set _Content-Type_ response header with `type` through `mime.lookup()`
|
|
63
|
+
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
64
|
+
*
|
|
65
|
+
* Examples:
|
|
66
|
+
*
|
|
67
|
+
* res.type('.html');
|
|
68
|
+
* res.type('html');
|
|
69
|
+
* res.type('json');
|
|
70
|
+
* res.type('application/json');
|
|
71
|
+
* res.type('png');
|
|
72
|
+
*/
|
|
73
|
+
type(type) {
|
|
74
|
+
const ct = type.indexOf('/') === -1
|
|
75
|
+
? mime_types_1.default.lookup(type)
|
|
76
|
+
: type;
|
|
77
|
+
if (ct)
|
|
78
|
+
this.set('Content-Type', ct);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Set cookie `name` to `value`, with the given `options`.
|
|
83
|
+
*
|
|
84
|
+
* Options:
|
|
85
|
+
*
|
|
86
|
+
* - `maxAge` max-age in milliseconds, converted to `expires`
|
|
87
|
+
* - `signed` sign the cookie
|
|
88
|
+
* - `path` defaults to "/"
|
|
89
|
+
*
|
|
90
|
+
* Examples:
|
|
91
|
+
*
|
|
92
|
+
* // "Remember Me" for 15 minutes
|
|
93
|
+
* res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
|
|
94
|
+
*
|
|
95
|
+
* // same as above
|
|
96
|
+
* res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
|
|
97
|
+
*
|
|
98
|
+
*/
|
|
99
|
+
cookie(name, value, options) {
|
|
100
|
+
const opts = { ...options };
|
|
101
|
+
const secret = this.req?.secret;
|
|
102
|
+
const signed = opts.signed;
|
|
103
|
+
if (signed && !secret) {
|
|
104
|
+
throw new Error('cookieParser("secret") required for signed cookies');
|
|
105
|
+
}
|
|
106
|
+
let val = typeof value === 'object'
|
|
107
|
+
? 'j:' + JSON.stringify(value)
|
|
108
|
+
: String(value);
|
|
109
|
+
if (signed)
|
|
110
|
+
val = 's:' + cookie_signature_1.default.sign(val, secret);
|
|
111
|
+
if (opts.maxAge != null) {
|
|
112
|
+
const maxAge = opts.maxAge - 0;
|
|
113
|
+
if (!isNaN(maxAge)) {
|
|
114
|
+
opts.expires = new Date(Date.now() + maxAge);
|
|
115
|
+
opts.maxAge = Math.floor(maxAge / 1000);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (opts.path == null)
|
|
119
|
+
opts.path = '/';
|
|
120
|
+
// Remove old cookie
|
|
121
|
+
let a = this.get('Set-Cookie');
|
|
122
|
+
if (a && Array.isArray(a)) {
|
|
123
|
+
a = a.filter(x => !x.startsWith(name + '='));
|
|
124
|
+
this.set('Set-Cookie', a);
|
|
125
|
+
}
|
|
126
|
+
this.append('Set-Cookie', cookie_1.default.serialize(name, String(val), opts));
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Clear cookie `name`.
|
|
131
|
+
*/
|
|
132
|
+
clearCookie(name, options) {
|
|
133
|
+
return this.cookie(name, '', { expires: new Date(1), path: '/', ...options });
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Set Link header field with the given `links`.
|
|
137
|
+
*
|
|
138
|
+
* Examples:
|
|
139
|
+
*
|
|
140
|
+
* res.links({
|
|
141
|
+
* next: 'http://api.example.com/users?page=2',
|
|
142
|
+
* last: 'http://api.example.com/users?page=5'
|
|
143
|
+
* });
|
|
144
|
+
*
|
|
145
|
+
*/
|
|
146
|
+
links(links) {
|
|
147
|
+
let link = this.get('Link') || '';
|
|
148
|
+
if (link)
|
|
149
|
+
link += ', ';
|
|
150
|
+
return this.set('Link', link + Object.keys(links).map(rel => '<' + links[rel] + '>; rel="' + rel + '"').join(', '));
|
|
151
|
+
}
|
|
152
|
+
redirect(arg0, url) {
|
|
153
|
+
let status = 302;
|
|
154
|
+
// allow status / url
|
|
155
|
+
if (typeof arg0 === 'number') {
|
|
156
|
+
status = arg0;
|
|
157
|
+
}
|
|
158
|
+
else
|
|
159
|
+
url = arg0 || '';
|
|
160
|
+
// Set location header
|
|
161
|
+
this.location(url || '/');
|
|
162
|
+
// Respond
|
|
163
|
+
this.statusCode = status;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Send JSON response.
|
|
167
|
+
*
|
|
168
|
+
* Examples:
|
|
169
|
+
*
|
|
170
|
+
* res.json(null);
|
|
171
|
+
* res.json({ user: 'tj' });
|
|
172
|
+
*/
|
|
173
|
+
json(obj) {
|
|
174
|
+
if (!this.get('Content-Type'))
|
|
175
|
+
this.set('Content-Type', 'application/json');
|
|
176
|
+
const body = JSON.stringify(obj);
|
|
177
|
+
return this.send(body);
|
|
178
|
+
}
|
|
179
|
+
location(url) {
|
|
180
|
+
let loc = url;
|
|
181
|
+
// "back" is an alias for the referrer
|
|
182
|
+
if (url === 'back')
|
|
183
|
+
loc = this.req?.get('Referrer') || '/';
|
|
184
|
+
// set location
|
|
185
|
+
return this.set('Location', (0, encodeurl_1.default)(loc));
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Set status `code`.
|
|
189
|
+
*/
|
|
190
|
+
status(code) {
|
|
191
|
+
this.statusCode = code;
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Set the response HTTP status code to `statusCode` and send its string representation as the response body.
|
|
196
|
+
* @link http://expressjs.com/4x/api.html#res.sendStatus
|
|
197
|
+
*
|
|
198
|
+
* Examples:
|
|
199
|
+
*
|
|
200
|
+
* res.sendStatus(200); // equivalent to res.status(200).send('OK')
|
|
201
|
+
* res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')
|
|
202
|
+
* res.sendStatus(404); // equivalent to res.status(404).send('Not Found')
|
|
203
|
+
* res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')
|
|
204
|
+
*/
|
|
205
|
+
sendStatus(statusCode) {
|
|
206
|
+
const body = common_1.HttpStatusMessages[statusCode] || String(statusCode);
|
|
207
|
+
this.statusCode = statusCode;
|
|
208
|
+
this.type('txt');
|
|
209
|
+
return this.send(body);
|
|
210
|
+
}
|
|
211
|
+
_init(init) {
|
|
212
|
+
super._init(init);
|
|
213
|
+
this.statusCode = init?.statusCode;
|
|
214
|
+
this.statusMessage = init?.statusMessage;
|
|
215
|
+
this.req = init?.req;
|
|
216
|
+
this.chunkedEncoding = init?.chunkedEncoding;
|
|
217
|
+
}
|
|
218
|
+
[http_message_host_js_1.HttpMessageHost.kOnHeaderReceived](info) {
|
|
219
|
+
super[http_message_host_js_1.HttpMessageHost.kOnHeaderReceived](info);
|
|
220
|
+
this.statusCode = info.statusCode;
|
|
221
|
+
this.statusMessage = info.statusMessage;
|
|
222
|
+
}
|
|
223
|
+
static create(init) {
|
|
224
|
+
const msg = new HttpResponseMessageHost();
|
|
225
|
+
msg._init(init);
|
|
226
|
+
return msg;
|
|
227
|
+
}
|
|
228
|
+
static fromBuffer(buffer) {
|
|
229
|
+
const msg = new HttpResponseMessageHost();
|
|
230
|
+
msg._parseBuffer(buffer, http_parser_1.HTTPParser.RESPONSE);
|
|
231
|
+
return msg;
|
|
232
|
+
}
|
|
233
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
234
|
+
static async fromStream(readable) {
|
|
235
|
+
throw new Error('fromStream is not implemented yet');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
exports.HttpResponseMessageHost = HttpResponseMessageHost;
|
package/cjs/index.js
CHANGED
|
@@ -7,6 +7,8 @@ tslib_1.__exportStar(require("./types.js"), exports);
|
|
|
7
7
|
tslib_1.__exportStar(require("./adapter/adapter.js"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./adapter/http/express-adapter.js"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./adapter/http/http-adapter.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./adapter/http/http-request-message.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./adapter/http/http-response-message.js"), exports);
|
|
10
12
|
tslib_1.__exportStar(require("./adapter/interfaces/request-context.interface.js"), exports);
|
|
11
13
|
tslib_1.__exportStar(require("./adapter/interfaces/logger.interface.js"), exports);
|
|
12
14
|
tslib_1.__exportStar(require("./adapter/interfaces/request.interface.js"), exports);
|