@suveren/gateway 0.9.0 → 0.10.0
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/README.md +33 -1
- package/bin/suveren-gateway.js +203 -5
- package/content/integrations/erp.json +184 -0
- package/content/integrations/index.json +2 -1
- package/dist/control-plane/index.mjs +653 -179
- package/dist/mcp-server/http.mjs +466 -259
- package/dist/ui/assets/{index-CIiOCgjJ.js → index-B1qNl7zo.js} +1 -1
- package/dist/ui/index.html +1 -1
- package/lib/as-pairing.mjs +77 -0
- package/lib/config.mjs +48 -0
- package/node_modules/undici/LICENSE +21 -0
- package/node_modules/undici/README.md +741 -0
- package/node_modules/undici/docs/docs/api/Agent.md +84 -0
- package/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
- package/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
- package/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
- package/node_modules/undici/docs/docs/api/Client.md +288 -0
- package/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
- package/node_modules/undici/docs/docs/api/Connector.md +115 -0
- package/node_modules/undici/docs/docs/api/ContentType.md +57 -0
- package/node_modules/undici/docs/docs/api/Cookies.md +128 -0
- package/node_modules/undici/docs/docs/api/Debug.md +62 -0
- package/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +317 -0
- package/node_modules/undici/docs/docs/api/Dispatcher.md +1393 -0
- package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
- package/node_modules/undici/docs/docs/api/Errors.md +49 -0
- package/node_modules/undici/docs/docs/api/EventSource.md +45 -0
- package/node_modules/undici/docs/docs/api/Fetch.md +60 -0
- package/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
- package/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
- package/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
- package/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
- package/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
- package/node_modules/undici/docs/docs/api/MockClient.md +81 -0
- package/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
- package/node_modules/undici/docs/docs/api/MockPool.md +555 -0
- package/node_modules/undici/docs/docs/api/Pool.md +84 -0
- package/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
- package/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
- package/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
- package/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
- package/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
- package/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
- package/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
- package/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +275 -0
- package/node_modules/undici/docs/docs/api/Util.md +25 -0
- package/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
- package/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
- package/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
- package/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
- package/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
- package/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
- package/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
- package/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
- package/node_modules/undici/index-fetch.js +65 -0
- package/node_modules/undici/index.d.ts +3 -0
- package/node_modules/undici/index.js +234 -0
- package/node_modules/undici/lib/api/abort-signal.js +59 -0
- package/node_modules/undici/lib/api/api-connect.js +110 -0
- package/node_modules/undici/lib/api/api-pipeline.js +252 -0
- package/node_modules/undici/lib/api/api-request.js +214 -0
- package/node_modules/undici/lib/api/api-stream.js +209 -0
- package/node_modules/undici/lib/api/api-upgrade.js +111 -0
- package/node_modules/undici/lib/api/index.js +7 -0
- package/node_modules/undici/lib/api/readable.js +580 -0
- package/node_modules/undici/lib/cache/memory-cache-store.js +279 -0
- package/node_modules/undici/lib/cache/sqlite-cache-store.js +467 -0
- package/node_modules/undici/lib/core/connect.js +153 -0
- package/node_modules/undici/lib/core/constants.js +143 -0
- package/node_modules/undici/lib/core/diagnostics.js +227 -0
- package/node_modules/undici/lib/core/errors.js +477 -0
- package/node_modules/undici/lib/core/request.js +485 -0
- package/node_modules/undici/lib/core/socks5-client.js +422 -0
- package/node_modules/undici/lib/core/socks5-utils.js +212 -0
- package/node_modules/undici/lib/core/symbols.js +75 -0
- package/node_modules/undici/lib/core/tree.js +160 -0
- package/node_modules/undici/lib/core/util.js +992 -0
- package/node_modules/undici/lib/dispatcher/agent.js +158 -0
- package/node_modules/undici/lib/dispatcher/balanced-pool.js +221 -0
- package/node_modules/undici/lib/dispatcher/client-h1.js +1746 -0
- package/node_modules/undici/lib/dispatcher/client-h2.js +1060 -0
- package/node_modules/undici/lib/dispatcher/client.js +668 -0
- package/node_modules/undici/lib/dispatcher/dispatcher-base.js +184 -0
- package/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
- package/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
- package/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
- package/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
- package/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
- package/node_modules/undici/lib/dispatcher/pool.js +118 -0
- package/node_modules/undici/lib/dispatcher/proxy-agent.js +319 -0
- package/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
- package/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
- package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +260 -0
- package/node_modules/undici/lib/encoding/index.js +33 -0
- package/node_modules/undici/lib/global.js +59 -0
- package/node_modules/undici/lib/handler/cache-handler.js +776 -0
- package/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
- package/node_modules/undici/lib/handler/decorator-handler.js +67 -0
- package/node_modules/undici/lib/handler/deduplication-handler.js +460 -0
- package/node_modules/undici/lib/handler/redirect-handler.js +238 -0
- package/node_modules/undici/lib/handler/retry-handler.js +504 -0
- package/node_modules/undici/lib/handler/unwrap-handler.js +106 -0
- package/node_modules/undici/lib/handler/wrap-handler.js +105 -0
- package/node_modules/undici/lib/interceptor/cache.js +621 -0
- package/node_modules/undici/lib/interceptor/decompress.js +551 -0
- package/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
- package/node_modules/undici/lib/interceptor/dns.js +571 -0
- package/node_modules/undici/lib/interceptor/dump.js +99 -0
- package/node_modules/undici/lib/interceptor/redirect.js +21 -0
- package/node_modules/undici/lib/interceptor/response-error.js +95 -0
- package/node_modules/undici/lib/interceptor/retry.js +19 -0
- package/node_modules/undici/lib/llhttp/.gitkeep +0 -0
- package/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
- package/node_modules/undici/lib/llhttp/constants.js +531 -0
- package/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
- package/node_modules/undici/lib/llhttp/utils.js +12 -0
- package/node_modules/undici/lib/mock/mock-agent.js +232 -0
- package/node_modules/undici/lib/mock/mock-call-history.js +248 -0
- package/node_modules/undici/lib/mock/mock-client.js +68 -0
- package/node_modules/undici/lib/mock/mock-errors.js +29 -0
- package/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
- package/node_modules/undici/lib/mock/mock-pool.js +68 -0
- package/node_modules/undici/lib/mock/mock-symbols.js +32 -0
- package/node_modules/undici/lib/mock/mock-utils.js +486 -0
- package/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
- package/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
- package/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
- package/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
- package/node_modules/undici/lib/util/cache.js +676 -0
- package/node_modules/undici/lib/util/date.js +670 -0
- package/node_modules/undici/lib/util/promise.js +28 -0
- package/node_modules/undici/lib/util/runtime-features.js +124 -0
- package/node_modules/undici/lib/util/stats.js +32 -0
- package/node_modules/undici/lib/util/timers.js +425 -0
- package/node_modules/undici/lib/web/cache/cache.js +864 -0
- package/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
- package/node_modules/undici/lib/web/cache/util.js +45 -0
- package/node_modules/undici/lib/web/cookies/constants.js +12 -0
- package/node_modules/undici/lib/web/cookies/index.js +199 -0
- package/node_modules/undici/lib/web/cookies/parse.js +314 -0
- package/node_modules/undici/lib/web/cookies/util.js +352 -0
- package/node_modules/undici/lib/web/eventsource/eventsource-stream.js +494 -0
- package/node_modules/undici/lib/web/eventsource/eventsource.js +501 -0
- package/node_modules/undici/lib/web/eventsource/util.js +29 -0
- package/node_modules/undici/lib/web/fetch/LICENSE +21 -0
- package/node_modules/undici/lib/web/fetch/body.js +509 -0
- package/node_modules/undici/lib/web/fetch/constants.js +131 -0
- package/node_modules/undici/lib/web/fetch/data-url.js +596 -0
- package/node_modules/undici/lib/web/fetch/formdata-parser.js +586 -0
- package/node_modules/undici/lib/web/fetch/formdata.js +259 -0
- package/node_modules/undici/lib/web/fetch/global.js +40 -0
- package/node_modules/undici/lib/web/fetch/headers.js +719 -0
- package/node_modules/undici/lib/web/fetch/index.js +2413 -0
- package/node_modules/undici/lib/web/fetch/request.js +1115 -0
- package/node_modules/undici/lib/web/fetch/response.js +641 -0
- package/node_modules/undici/lib/web/fetch/util.js +1522 -0
- package/node_modules/undici/lib/web/infra/index.js +229 -0
- package/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
- package/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
- package/node_modules/undici/lib/web/webidl/index.js +1006 -0
- package/node_modules/undici/lib/web/websocket/connection.js +339 -0
- package/node_modules/undici/lib/web/websocket/constants.js +126 -0
- package/node_modules/undici/lib/web/websocket/events.js +331 -0
- package/node_modules/undici/lib/web/websocket/frame.js +133 -0
- package/node_modules/undici/lib/web/websocket/permessage-deflate.js +105 -0
- package/node_modules/undici/lib/web/websocket/receiver.js +507 -0
- package/node_modules/undici/lib/web/websocket/sender.js +109 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +496 -0
- package/node_modules/undici/lib/web/websocket/util.js +347 -0
- package/node_modules/undici/lib/web/websocket/websocket.js +758 -0
- package/node_modules/undici/package.json +152 -0
- package/node_modules/undici/scripts/strip-comments.js +10 -0
- package/node_modules/undici/types/README.md +6 -0
- package/node_modules/undici/types/agent.d.ts +32 -0
- package/node_modules/undici/types/api.d.ts +43 -0
- package/node_modules/undici/types/balanced-pool.d.ts +30 -0
- package/node_modules/undici/types/cache-interceptor.d.ts +179 -0
- package/node_modules/undici/types/cache.d.ts +36 -0
- package/node_modules/undici/types/client-stats.d.ts +15 -0
- package/node_modules/undici/types/client.d.ts +139 -0
- package/node_modules/undici/types/connector.d.ts +36 -0
- package/node_modules/undici/types/content-type.d.ts +21 -0
- package/node_modules/undici/types/cookies.d.ts +30 -0
- package/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
- package/node_modules/undici/types/dispatcher.d.ts +275 -0
- package/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
- package/node_modules/undici/types/errors.d.ts +177 -0
- package/node_modules/undici/types/eventsource.d.ts +66 -0
- package/node_modules/undici/types/fetch.d.ts +231 -0
- package/node_modules/undici/types/formdata.d.ts +114 -0
- package/node_modules/undici/types/global-dispatcher.d.ts +9 -0
- package/node_modules/undici/types/global-origin.d.ts +7 -0
- package/node_modules/undici/types/h2c-client.d.ts +73 -0
- package/node_modules/undici/types/handlers.d.ts +14 -0
- package/node_modules/undici/types/header.d.ts +160 -0
- package/node_modules/undici/types/index.d.ts +91 -0
- package/node_modules/undici/types/interceptors.d.ts +82 -0
- package/node_modules/undici/types/mock-agent.d.ts +68 -0
- package/node_modules/undici/types/mock-call-history.d.ts +111 -0
- package/node_modules/undici/types/mock-client.d.ts +27 -0
- package/node_modules/undici/types/mock-errors.d.ts +12 -0
- package/node_modules/undici/types/mock-interceptor.d.ts +94 -0
- package/node_modules/undici/types/mock-pool.d.ts +27 -0
- package/node_modules/undici/types/patch.d.ts +29 -0
- package/node_modules/undici/types/pool-stats.d.ts +19 -0
- package/node_modules/undici/types/pool.d.ts +41 -0
- package/node_modules/undici/types/proxy-agent.d.ts +29 -0
- package/node_modules/undici/types/readable.d.ts +68 -0
- package/node_modules/undici/types/retry-agent.d.ts +8 -0
- package/node_modules/undici/types/retry-handler.d.ts +125 -0
- package/node_modules/undici/types/round-robin-pool.d.ts +41 -0
- package/node_modules/undici/types/snapshot-agent.d.ts +109 -0
- package/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
- package/node_modules/undici/types/util.d.ts +18 -0
- package/node_modules/undici/types/utility.d.ts +7 -0
- package/node_modules/undici/types/webidl.d.ts +347 -0
- package/node_modules/undici/types/websocket.d.ts +188 -0
- package/package.json +4 -2
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const util = require('../core/util')
|
|
4
|
+
const { kBodyUsed } = require('../core/symbols')
|
|
5
|
+
const assert = require('node:assert')
|
|
6
|
+
const { InvalidArgumentError } = require('../core/errors')
|
|
7
|
+
const EE = require('node:events')
|
|
8
|
+
|
|
9
|
+
const redirectableStatusCodes = [300, 301, 302, 303, 307, 308]
|
|
10
|
+
|
|
11
|
+
const kBody = Symbol('body')
|
|
12
|
+
|
|
13
|
+
const noop = () => {}
|
|
14
|
+
|
|
15
|
+
class BodyAsyncIterable {
|
|
16
|
+
constructor (body) {
|
|
17
|
+
this[kBody] = body
|
|
18
|
+
this[kBodyUsed] = false
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async * [Symbol.asyncIterator] () {
|
|
22
|
+
assert(!this[kBodyUsed], 'disturbed')
|
|
23
|
+
this[kBodyUsed] = true
|
|
24
|
+
yield * this[kBody]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
class RedirectHandler {
|
|
29
|
+
static buildDispatch (dispatcher, maxRedirections) {
|
|
30
|
+
if (maxRedirections != null && (!Number.isInteger(maxRedirections) || maxRedirections < 0)) {
|
|
31
|
+
throw new InvalidArgumentError('maxRedirections must be a positive number')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const dispatch = dispatcher.dispatch.bind(dispatcher)
|
|
35
|
+
return (opts, originalHandler) => dispatch(opts, new RedirectHandler(dispatch, maxRedirections, opts, originalHandler))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
constructor (dispatch, maxRedirections, opts, handler) {
|
|
39
|
+
if (maxRedirections != null && (!Number.isInteger(maxRedirections) || maxRedirections < 0)) {
|
|
40
|
+
throw new InvalidArgumentError('maxRedirections must be a positive number')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.dispatch = dispatch
|
|
44
|
+
this.location = null
|
|
45
|
+
const { maxRedirections: _, ...cleanOpts } = opts
|
|
46
|
+
this.opts = cleanOpts // opts must be a copy, exclude maxRedirections
|
|
47
|
+
this.maxRedirections = maxRedirections
|
|
48
|
+
this.handler = handler
|
|
49
|
+
this.history = []
|
|
50
|
+
|
|
51
|
+
if (util.isStream(this.opts.body)) {
|
|
52
|
+
// TODO (fix): Provide some way for the user to cache the file to e.g. /tmp
|
|
53
|
+
// so that it can be dispatched again?
|
|
54
|
+
// TODO (fix): Do we need 100-expect support to provide a way to do this properly?
|
|
55
|
+
if (util.bodyLength(this.opts.body) === 0) {
|
|
56
|
+
this.opts.body
|
|
57
|
+
.on('data', function () {
|
|
58
|
+
assert(false)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (typeof this.opts.body.readableDidRead !== 'boolean') {
|
|
63
|
+
this.opts.body[kBodyUsed] = false
|
|
64
|
+
EE.prototype.on.call(this.opts.body, 'data', function () {
|
|
65
|
+
this[kBodyUsed] = true
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
} else if (this.opts.body && typeof this.opts.body.pipeTo === 'function') {
|
|
69
|
+
// TODO (fix): We can't access ReadableStream internal state
|
|
70
|
+
// to determine whether or not it has been disturbed. This is just
|
|
71
|
+
// a workaround.
|
|
72
|
+
this.opts.body = new BodyAsyncIterable(this.opts.body)
|
|
73
|
+
} else if (
|
|
74
|
+
this.opts.body &&
|
|
75
|
+
typeof this.opts.body !== 'string' &&
|
|
76
|
+
!ArrayBuffer.isView(this.opts.body) &&
|
|
77
|
+
util.isIterable(this.opts.body) &&
|
|
78
|
+
!util.isFormDataLike(this.opts.body)
|
|
79
|
+
) {
|
|
80
|
+
// TODO: Should we allow re-using iterable if !this.opts.idempotent
|
|
81
|
+
// or through some other flag?
|
|
82
|
+
this.opts.body = new BodyAsyncIterable(this.opts.body)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
onRequestStart (controller, context) {
|
|
87
|
+
this.handler.onRequestStart?.(controller, { ...context, history: this.history })
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
onRequestUpgrade (controller, statusCode, headers, socket) {
|
|
91
|
+
this.handler.onRequestUpgrade?.(controller, statusCode, headers, socket)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
onResponseStart (controller, statusCode, headers, statusMessage) {
|
|
95
|
+
if (this.opts.throwOnMaxRedirect && this.history.length >= this.maxRedirections) {
|
|
96
|
+
throw new Error('max redirects')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// https://tools.ietf.org/html/rfc7231#section-6.4.2
|
|
100
|
+
// https://fetch.spec.whatwg.org/#http-redirect-fetch
|
|
101
|
+
// In case of HTTP 301 or 302 with POST, change the method to GET
|
|
102
|
+
if ((statusCode === 301 || statusCode === 302) && this.opts.method === 'POST') {
|
|
103
|
+
this.opts.method = 'GET'
|
|
104
|
+
if (util.isStream(this.opts.body)) {
|
|
105
|
+
util.destroy(this.opts.body.on('error', noop))
|
|
106
|
+
}
|
|
107
|
+
this.opts.body = null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// https://tools.ietf.org/html/rfc7231#section-6.4.4
|
|
111
|
+
// In case of HTTP 303, always replace method to be either HEAD or GET
|
|
112
|
+
if (statusCode === 303 && this.opts.method !== 'HEAD') {
|
|
113
|
+
this.opts.method = 'GET'
|
|
114
|
+
if (util.isStream(this.opts.body)) {
|
|
115
|
+
util.destroy(this.opts.body.on('error', noop))
|
|
116
|
+
}
|
|
117
|
+
this.opts.body = null
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
this.location = this.history.length >= this.maxRedirections || util.isDisturbed(this.opts.body) || redirectableStatusCodes.indexOf(statusCode) === -1
|
|
121
|
+
? null
|
|
122
|
+
: headers.location
|
|
123
|
+
|
|
124
|
+
if (this.opts.origin) {
|
|
125
|
+
this.history.push(new URL(this.opts.path, this.opts.origin))
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (!this.location) {
|
|
129
|
+
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const { origin, pathname, search } = util.parseURL(new URL(this.location, this.opts.origin && new URL(this.opts.path, this.opts.origin)))
|
|
134
|
+
const path = search ? `${pathname}${search}` : pathname
|
|
135
|
+
|
|
136
|
+
// Check for redirect loops by seeing if we've already visited this URL in our history
|
|
137
|
+
// This catches the case where Client/Pool try to handle cross-origin redirects but fail
|
|
138
|
+
// and keep redirecting to the same URL in an infinite loop
|
|
139
|
+
const redirectUrlString = `${origin}${path}`
|
|
140
|
+
for (const historyUrl of this.history) {
|
|
141
|
+
if (historyUrl.toString() === redirectUrlString) {
|
|
142
|
+
throw new InvalidArgumentError(`Redirect loop detected. Cannot redirect to ${origin}. This typically happens when using a Client or Pool with cross-origin redirects. Use an Agent for cross-origin redirects.`)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Remove headers referring to the original URL.
|
|
147
|
+
// By default it is Host only, unless it's a 303 (see below), which removes also all Content-* headers.
|
|
148
|
+
// https://tools.ietf.org/html/rfc7231#section-6.4
|
|
149
|
+
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin)
|
|
150
|
+
this.opts.path = path
|
|
151
|
+
this.opts.origin = origin
|
|
152
|
+
this.opts.query = null
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
onResponseData (controller, chunk) {
|
|
156
|
+
if (this.location) {
|
|
157
|
+
/*
|
|
158
|
+
https://tools.ietf.org/html/rfc7231#section-6.4
|
|
159
|
+
|
|
160
|
+
TLDR: undici always ignores 3xx response bodies.
|
|
161
|
+
|
|
162
|
+
Redirection is used to serve the requested resource from another URL, so it assumes that
|
|
163
|
+
no body is generated (and thus can be ignored). Even though generating a body is not prohibited.
|
|
164
|
+
|
|
165
|
+
For status 301, 302, 303, 307 and 308 (the latter from RFC 7238), the specs mention that the body usually
|
|
166
|
+
(which means it's optional and not mandated) contain just an hyperlink to the value of
|
|
167
|
+
the Location response header, so the body can be ignored safely.
|
|
168
|
+
|
|
169
|
+
For status 300, which is "Multiple Choices", the spec mentions both generating a Location
|
|
170
|
+
response header AND a response body with the other possible location to follow.
|
|
171
|
+
Since the spec explicitly chooses not to specify a format for such body and leave it to
|
|
172
|
+
servers and browsers implementors, we ignore the body as there is no specified way to eventually parse it.
|
|
173
|
+
*/
|
|
174
|
+
} else {
|
|
175
|
+
this.handler.onResponseData?.(controller, chunk)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
onResponseEnd (controller, trailers) {
|
|
180
|
+
if (this.location) {
|
|
181
|
+
/*
|
|
182
|
+
https://tools.ietf.org/html/rfc7231#section-6.4
|
|
183
|
+
|
|
184
|
+
TLDR: undici always ignores 3xx response trailers as they are not expected in case of redirections
|
|
185
|
+
and neither are useful if present.
|
|
186
|
+
|
|
187
|
+
See comment on onData method above for more detailed information.
|
|
188
|
+
*/
|
|
189
|
+
this.dispatch(this.opts, this)
|
|
190
|
+
} else {
|
|
191
|
+
this.handler.onResponseEnd(controller, trailers)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
onResponseError (controller, error) {
|
|
196
|
+
this.handler.onResponseError?.(controller, error)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// https://tools.ietf.org/html/rfc7231#section-6.4.4
|
|
201
|
+
function shouldRemoveHeader (header, removeContent, unknownOrigin) {
|
|
202
|
+
if (header.length === 4) {
|
|
203
|
+
return util.headerNameToString(header) === 'host'
|
|
204
|
+
}
|
|
205
|
+
if (removeContent && util.headerNameToString(header).startsWith('content-')) {
|
|
206
|
+
return true
|
|
207
|
+
}
|
|
208
|
+
if (unknownOrigin && (header.length === 13 || header.length === 6 || header.length === 19)) {
|
|
209
|
+
const name = util.headerNameToString(header)
|
|
210
|
+
return name === 'authorization' || name === 'cookie' || name === 'proxy-authorization'
|
|
211
|
+
}
|
|
212
|
+
return false
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// https://tools.ietf.org/html/rfc7231#section-6.4
|
|
216
|
+
function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
|
|
217
|
+
const ret = []
|
|
218
|
+
if (Array.isArray(headers)) {
|
|
219
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
220
|
+
if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin)) {
|
|
221
|
+
ret.push(headers[i], headers[i + 1])
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
} else if (headers && typeof headers === 'object') {
|
|
225
|
+
const entries = util.hasSafeIterator(headers) ? headers : Object.entries(headers)
|
|
226
|
+
|
|
227
|
+
for (const [key, value] of entries) {
|
|
228
|
+
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
|
|
229
|
+
ret.push(key, value)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
} else {
|
|
233
|
+
assert(headers == null, 'headers must be an object or an array')
|
|
234
|
+
}
|
|
235
|
+
return ret
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
module.exports = RedirectHandler
|