diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e41e45f --- /dev/null +++ b/CHANGELOG.md @@ -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 +- 初始化模块。 diff --git a/jsmod.go b/jsmod.go index 2176442..7110f98 100644 --- a/jsmod.go +++ b/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)