Skip to content

Commit

Permalink
KTOR-6158 JS. Remove redundant require calls (#4422)
Browse files Browse the repository at this point in the history
* KTOR-6158 JS. Remove redundant `require` calls

* KTOR-6158 JS. Remove redundant `readBodyNode`

* KTOR-6158 JS. Remove invalid import

* KTOR-6158 WasmJS. Remove redundant `require` calls

* KTOR-6158 WasmJS. Fix `Object.assign` declaration

* KTOR-6158 Update yarn.lock file

---------

Co-authored-by: Osip Fatkullin <osip.fatkullin@jetbrains.com>
  • Loading branch information
turansky and osipxd authored Oct 29, 2024
1 parent 8c271cb commit e5e8ae7
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 180 deletions.
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ thymeleaf = "3.1.2.RELEASE"
javax-servlet = "4.0.1"
jakarta-servlet = "5.0.0"

node-fetch = "2.6.7"
abort-controller = "3.0.0"
ws = "8.5.0"
xmlutil = "0.90.2"
yamlkt = "0.13.0"
Expand Down
19 changes: 0 additions & 19 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,6 @@
resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==

abort-controller@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==
dependencies:
event-target-shim "^5.0.0"

accepts@~1.3.4:
version "1.3.8"
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e"
Expand Down Expand Up @@ -959,11 +952,6 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

event-target-shim@^5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789"
integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==

eventemitter3@^4.0.0:
version "4.0.7"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
Expand Down Expand Up @@ -1669,13 +1657,6 @@ netmask@^2.0.2:
resolved "https://registry.yarnpkg.com/netmask/-/netmask-2.0.2.tgz#8b01a07644065d536383835823bc52004ebac5e7"
integrity sha512-dBpDMdxv9Irdq66304OLfEmQ9tbNRFnFTuZiLo+bD+r332bBmMJ8GBLXklIXXgxd3+v9+KUnZaUR5PJMa75Gsg==

node-fetch@2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

node-fetch@^2.6.12:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
Expand Down
4 changes: 0 additions & 4 deletions ktor-client/ktor-client-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ kotlin.sourceSets {

jsMain {
dependencies {
api(npm("node-fetch", libs.versions.node.fetch.get()))
api(npm("abort-controller", libs.versions.abort.controller.get()))
api(npm("ws", libs.versions.ws.get()))
}
}

wasmJsMain {
dependencies {
api(npm("node-fetch", libs.versions.node.fetch.get()))
api(npm("abort-controller", libs.versions.abort.controller.get()))
api(npm("ws", libs.versions.ws.get()))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package io.ktor.client.engine.js.compatibility

import io.ktor.client.engine.js.*
import io.ktor.client.engine.js.browser.*
import io.ktor.client.engine.js.node.*
import io.ktor.client.fetch.*
import io.ktor.util.*
import io.ktor.utils.io.*
Expand All @@ -22,7 +21,7 @@ internal suspend fun commonFetch(
PlatformUtils.IS_BROWSER -> fetch(input, init)
else -> {
val options = js("Object").assign(js("Object").create(null), init, config.nodeOptions)
jsRequireNodeFetch()(input, options)
fetch(input, options)
}
}

Expand All @@ -37,25 +36,10 @@ internal suspend fun commonFetch(
}

internal fun AbortController(): AbortController {
return when {
PlatformUtils.IS_BROWSER -> js("new AbortController()")
else -> {
@Suppress("UNUSED_VARIABLE")
val controller = js("eval('require')('abort-controller')")
js("new controller()")
}
}
return js("new AbortController()")
}

internal fun CoroutineScope.readBody(
response: org.w3c.fetch.Response
): ByteReadChannel = when {
PlatformUtils.IS_NODE -> readBodyNode(response)
else -> readBodyBrowser(response)
}

private fun jsRequireNodeFetch(): dynamic = try {
js("eval('require')('node-fetch')")
} catch (cause: dynamic) {
throw Error("Error loading module 'node-fetch': $cause")
}
): ByteReadChannel =
readBodyBrowser(response)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package io.ktor.client.engine.js.compatibility

import io.ktor.client.engine.js.*
import io.ktor.client.engine.js.browser.*
import io.ktor.client.engine.js.node.*
import io.ktor.client.fetch.*
import io.ktor.client.utils.*
import io.ktor.util.*
Expand All @@ -22,8 +21,13 @@ internal suspend fun commonFetch(
val promise: Promise<org.w3c.fetch.Response> = when {
PlatformUtils.IS_BROWSER -> fetch(input, init)
else -> {
val nodeFetch = makeRequire<JsAny>("node-fetch")
makeJsCall<Promise<org.w3c.fetch.Response>>(nodeFetch, input.toJsString(), init, config.nodeOptions)
val options = makeJsCall<RequestInit>(
jsObjectAssign(),
makeJsObject<RequestInit>(),
init,
config.nodeOptions,
)
fetch(input, options)
}
}

Expand All @@ -43,16 +47,11 @@ private fun abortControllerCtorBrowser(): AbortController =
js("AbortController")

internal fun AbortController(): AbortController {
val ctor = when {
PlatformUtils.IS_BROWSER -> abortControllerCtorBrowser()
else -> makeRequire("abort-controller")
}
val ctor = abortControllerCtorBrowser()
return makeJsNew(ctor)
}

internal fun CoroutineScope.readBody(
response: org.w3c.fetch.Response
): ByteReadChannel = when {
PlatformUtils.IS_NODE -> readBodyNode(response)
else -> readBodyBrowser(response)
}
): ByteReadChannel =
readBodyBrowser(response)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import org.khronos.webgl.*

internal fun <T : JsAny> makeJsObject(): T = js("{ return {}; }")

internal fun jsObjectAssign(): JsAny =
js("Object.assign")

@Suppress("UNUSED_PARAMETER")
internal fun <T : JsAny> makeJsNew(ctor: JsAny): T = js("new ctor()")

Expand Down

0 comments on commit e5e8ae7

Please sign in to comment.