refactor: encapsulate internal context keys and add Get (by AI)
This commit is contained in:
parent
147579b0d1
commit
a81fb32f78
8
CHANGELOG.md
Normal file
8
CHANGELOG.md
Normal file
@ -0,0 +1,8 @@
|
||||
# Changelog: @go/jsmod
|
||||
|
||||
## v1.5.1 (2026-06-08)
|
||||
- **重构**: 完全隐藏内部 Context 键值(采用 `__GoJSContext__`),并废弃暴露的 `SafeModeKey`。
|
||||
- **新增**: 新增 `Get(ctx, key)` 辅助方法,统一承接来自 `go/js` 注入的 `map[string]any` 运行时配置。
|
||||
|
||||
## [1.5.0] - 2026-05-10
|
||||
- 初始化模块。
|
||||
16
jsmod.go
16
jsmod.go
@ -7,12 +7,22 @@ import (
|
||||
|
||||
type contextKey string
|
||||
|
||||
const SafeModeKey contextKey = "SafeMode"
|
||||
const internalContextKey contextKey = "__GoJSContext__"
|
||||
|
||||
// Get retrieves an injected value from the context using the unified context map.
|
||||
func Get(ctx context.Context, key string) any {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
if m, ok := ctx.Value(internalContextKey).(map[string]any); ok {
|
||||
return m[key]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsSafeMode checks if the provided context indicates that the execution is in safe mode.
|
||||
func IsSafeMode(ctx context.Context) bool {
|
||||
v := ctx.Value(SafeModeKey)
|
||||
if sm, ok := v.(bool); ok {
|
||||
if sm, ok := Get(ctx, "SafeMode").(bool); ok {
|
||||
return sm
|
||||
}
|
||||
return false // Default to false if not specified (internal trusted caller)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user