Skip to content

Commit e2bb163

Browse files
committed
✨ [kmp/browser,toolkit] jsProcess现在也加入了debugTags的配置支持
支持使用`:js:`前缀作为特调。
1 parent 69bca27 commit e2bb163

6 files changed

Lines changed: 611 additions & 17 deletions

File tree

next/kmp/browser/src/commonMain/kotlin/org/dweb_browser/browser/jsProcess/JsProcessNMM.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.dweb_browser.browser.jsProcess
33
import io.ktor.http.HttpStatusCode
44
import io.ktor.http.fullPath
55
import kotlinx.coroutines.async
6-
import kotlinx.serialization.encodeToString
76
import kotlinx.serialization.json.Json
87
import org.dweb_browser.browser.BrowserI18nResource
98
import org.dweb_browser.browser.jmm.JsMicroModule
@@ -25,6 +24,7 @@ import org.dweb_browser.helper.ImageResource
2524
import org.dweb_browser.helper.SafeHashMap
2625
import org.dweb_browser.helper.collectIn
2726
import org.dweb_browser.helper.encodeURI
27+
import org.dweb_browser.helper.getDebugTags
2828
import org.dweb_browser.helper.randomUUID
2929
import org.dweb_browser.helper.resolvePath
3030
import org.dweb_browser.helper.toJsonElement
@@ -167,8 +167,7 @@ class JsProcessNMM : NativeMicroModule("js.browser.dweb", "Js Process") {
167167
apis.createIpcEndpoint(processId, manifestJson, ids.first).globalId.also {
168168
debugMM("create-ipc-endpoint-success", "globalId=$it manifest=$manifestJson")
169169
}
170-
},
171-
"/create-ipc" bind PureMethod.GET by defineEmptyResponse {
170+
}, "/create-ipc" bind PureMethod.GET by defineEmptyResponse {
172171
val processToken = request.query("token")
173172
val processId = tokenPidMap[processToken] ?: throw ResponseException(
174173
code = HttpStatusCode.NotFound,
@@ -179,12 +178,9 @@ class JsProcessNMM : NativeMicroModule("js.browser.dweb", "Js Process") {
179178
val manifestJson = request.query("manifest")
180179
debugMM("/create-ipc") { "remoteGlobalId=$remoteGlobalId,manifestJson=$manifestJson" }
181180
apis.createJsIpc(
182-
processId,
183-
GlobalWebMessageEndpoint.get(remoteGlobalId).port,
184-
manifestJson
181+
processId, GlobalWebMessageEndpoint.get(remoteGlobalId).port, manifestJson
185182
) {}
186-
}
187-
)
183+
})
188184
}
189185

190186
override suspend fun _shutdown() {
@@ -238,14 +234,25 @@ class JsProcessNMM : NativeMicroModule("js.browser.dweb", "Js Process") {
238234
},
239235
)
240236
}
241-
237+
fun getJsDebugTags(): List<String> {
238+
val allTags = getDebugTags()
239+
// 如果有js特调,那么久返回特调的,否则和native共享同一套配置
240+
val jsOnly = allTags.filter { it.startsWith(":js:") }
241+
return when {
242+
jsOnly.isEmpty() -> allTags
243+
else -> jsOnly.map { tag -> tag.slice(":js:".length..<tag.length) }
244+
}
245+
}
242246
/// TODO env 允许远端传过来扩展
243247
val env = mutableMapOf<String, String>(
244248
// ...your envs
245249
// 这不是是它代码的请求路径,代码请求路径从 import.meta.url 中读取,这里是用来为开发者提供一个 baseURL 而已
246250
"host" to httpDwebServer.startResult.urlInfo.host,
247-
// native环境是否启用调试
248-
"debug" to debugJsProcess.isEnable.toString(),
251+
// 按需开启输出
252+
"debug" to when {
253+
debugJsProcess.isEnable -> Json.encodeToString(getJsDebugTags())
254+
else -> "[]"
255+
},
249256
// jmm的版本信息
250257
"jsMicroModule" to "${JsMicroModule.VERSION}.${JsMicroModule.PATCH}",
251258
// web brands

next/kmp/helper/src/commonMain/kotlin/org/dweb_browser/helper/Debugger.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ private data class DebugTags(
3535
val debugNames: Set<String>,
3636
val verboseRegexes: Set<Regex>,
3737
val verboseNames: Set<String>,
38+
val origin: Iterable<String>,
3839
) {
3940
companion object {
40-
var singleton: DebugTags = DebugTags(setOf(), setOf(), setOf(), setOf())
41+
var singleton: DebugTags = DebugTags(setOf(), setOf(), setOf(), setOf(), listOf())
4142
private set
4243

4344
private val sync = SynchronizedObject()
@@ -90,7 +91,7 @@ private data class DebugTags(
9091
}
9192

9293
}
93-
singleton = DebugTags(debugRegexes, debugNames, verboseRegexes, verboseNames)
94+
singleton = DebugTags(debugRegexes, debugNames, verboseRegexes, verboseNames, tags)
9495
}
9596

9697
fun add(name: String) = synchronized(sync) {
@@ -125,6 +126,8 @@ public fun addDebugTags(tags: Iterable<String>) {
125126
DebugTags.from(tags)
126127
}
127128

129+
public fun getDebugTags(): List<String> = DebugTags.singleton.origin.toList()
130+
128131
public class Debugger(public val scope: String) {
129132
init {
130133
println("Debugger scope: $scope")

0 commit comments

Comments
 (0)