NewEngine creates a new WASM policy engine with the given options
(opts ...engine.Option)
| 52 | |
| 53 | // NewEngine creates a new WASM policy engine with the given options |
| 54 | func NewEngine(opts ...engine.Option) *Engine { |
| 55 | options := engine.ApplyOptions(opts...) |
| 56 | |
| 57 | // Extract WASM-specific options with defaults |
| 58 | executionTimeout := options.ExecutionTimeout |
| 59 | if executionTimeout == 0 { |
| 60 | executionTimeout = 5 * time.Second |
| 61 | } |
| 62 | |
| 63 | logger := options.Logger |
| 64 | if logger == nil { |
| 65 | noopLogger := zerolog.Nop() |
| 66 | logger = &noopLogger |
| 67 | } |
| 68 | |
| 69 | // Set WASM plugin log level exactly once across all engine instances. |
| 70 | // extism.SetLogLevel modifies global state and is not safe for concurrent calls. |
| 71 | setLogLevelOnce.Do(func() { |
| 72 | switch { |
| 73 | case logger.GetLevel() <= zerolog.TraceLevel: |
| 74 | extism.SetLogLevel(extism.LogLevelTrace) |
| 75 | case logger.GetLevel() <= zerolog.DebugLevel: |
| 76 | extism.SetLogLevel(extism.LogLevelDebug) |
| 77 | case logger.GetLevel() <= zerolog.InfoLevel: |
| 78 | extism.SetLogLevel(extism.LogLevelInfo) |
| 79 | case logger.GetLevel() <= zerolog.WarnLevel: |
| 80 | extism.SetLogLevel(extism.LogLevelWarn) |
| 81 | default: |
| 82 | extism.SetLogLevel(extism.LogLevelError) |
| 83 | } |
| 84 | }) |
| 85 | |
| 86 | return &Engine{ |
| 87 | executionTimeout: executionTimeout, |
| 88 | logger: logger, |
| 89 | CommonEngineOptions: options.CommonEngineOptions, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Verify executes a WASM policy against the provided input |
| 94 | func (e *Engine) Verify(ctx context.Context, policy *engine.Policy, input []byte, args map[string]any) (*engine.EvaluationResult, error) { |