-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KTOR-326 Allow to set followRedirect property for js client engine #3053
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,9 @@ import org.w3c.dom.* | |
import org.w3c.dom.events.* | ||
import kotlin.coroutines.* | ||
|
||
internal class JsClientEngine(override val config: HttpClientEngineConfig) : HttpClientEngineBase("ktor-js") { | ||
internal class JsClientEngine( | ||
override val config: HttpClientEngineConfig | ||
) : HttpClientEngineBase("ktor-js") { | ||
|
||
override val dispatcher = Dispatchers.Default | ||
|
||
|
@@ -31,13 +33,14 @@ internal class JsClientEngine(override val config: HttpClientEngineConfig) : Htt | |
@OptIn(InternalAPI::class) | ||
override suspend fun execute(data: HttpRequestData): HttpResponseData { | ||
val callContext = callContext() | ||
val clientConfig = data.attributes[CLIENT_CONFIG] | ||
|
||
if (data.isUpgradeRequest()) { | ||
return executeWebSocketRequest(data, callContext) | ||
} | ||
|
||
val requestTime = GMTDate() | ||
val rawRequest = data.toRaw(callContext) | ||
val rawRequest = data.toRaw(clientConfig, callContext) | ||
val rawResponse = commonFetch(data.url.toString(), rawRequest) | ||
|
||
val status = HttpStatusCode(rawResponse.status.toInt(), rawResponse.statusText) | ||
|
@@ -59,17 +62,19 @@ internal class JsClientEngine(override val config: HttpClientEngineConfig) : Htt | |
// Adding "_capturingHack" to reduce chances of JS IR backend to rename variable, | ||
// so it can be accessed inside js("") function | ||
@Suppress("UNUSED_PARAMETER", "UnsafeCastFromDynamic", "UNUSED_VARIABLE", "LocalVariableName") | ||
private fun createWebSocket(urlString_capturingHack: String, headers: Headers): WebSocket = | ||
if (PlatformUtils.IS_NODE) { | ||
val ws_capturingHack = js("eval('require')('ws')") | ||
val headers_capturingHack: dynamic = object {} | ||
headers.forEach { name, values -> | ||
headers_capturingHack[name] = values.joinToString(",") | ||
} | ||
js("new ws_capturingHack(urlString_capturingHack, { headers: headers_capturingHack })") | ||
} else { | ||
js("new WebSocket(urlString_capturingHack)") | ||
private fun createWebSocket( | ||
urlString_capturingHack: String, | ||
headers: Headers | ||
): WebSocket = if (PlatformUtils.IS_NODE) { | ||
val ws_capturingHack = js("eval('require')('ws')") | ||
val headers_capturingHack: dynamic = object {} | ||
headers.forEach { name, values -> | ||
headers_capturingHack[name] = values.joinToString(",") | ||
} | ||
js("new ws_capturingHack(urlString_capturingHack, { headers: headers_capturingHack })") | ||
} else { | ||
js("new WebSocket(urlString_capturingHack)") | ||
} | ||
|
||
private suspend fun executeWebSocketRequest( | ||
request: HttpRequestData, | ||
|
@@ -129,7 +134,7 @@ private fun Event.asString(): String = buildString { | |
append(JSON.stringify(this@asString, arrayOf("message", "target", "type", "isTrusted"))) | ||
} | ||
|
||
private fun io.ktor.client.fetch.Headers.mapToKtor(): Headers = buildHeaders { | ||
private fun org.w3c.fetch.Headers.mapToKtor(): Headers = buildHeaders { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this works on nodeJS? See also #2592 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, we replace some types, and the actual implementation stays the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests are also fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unit tests? AFAIK they does not work with nodeJS production, at least 1 year ago, which results into the same problem at runtime: #2385 so you needed to create a test project and use the artifact. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will try to set up a sample There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made a sample for node: it works |
||
this@mapToKtor.asDynamic().forEach { value: String, key: String -> | ||
append(key, value) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why pass it through data? there is a property
config
in classThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cause we need client config, not engine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Let's create a ticket to pass client config in client factory
HttpClientEngineFactory.create
method in 3.0.0 for a proper fix.