• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

me-viper / OpaDotNet / 13670981829

05 Mar 2025 07:47AM UTC coverage: 82.062% (-0.8%) from 82.832%
13670981829

push

github

me-viper
chore: Freeze public API

2057 of 2664 branches covered (77.21%)

Branch coverage included in aggregate %.

4000 of 4717 relevant lines covered (84.8%)

8028.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.12
/src/Wasm/src/WasmPolicyEngineOptions.cs
1
using System.Text.Encodings.Web;
2

3
using OpaDotNet.Wasm.Builtins;
4
using OpaDotNet.Wasm.Rego;
5

6
namespace OpaDotNet.Wasm;
7

8
/// <summary>
9
/// Contains members that affect OPA policy engine configuration.
10
/// </summary>
11
[PublicAPI]
12
public class WasmPolicyEngineOptions
13
{
14
    private Func<IOpaImportsAbi> _makeBuiltins;
15

16
    private readonly ImportsCache _importsCache = new();
1,762✔
17

18
    /// <summary>
19
    /// Default engine options.
20
    /// </summary>
21
    public static WasmPolicyEngineOptions Default { get; } = new();
94✔
22

23
    /// <summary>
24
    /// Creates default engine options.
25
    /// </summary>
26
    /// <param name="options">JSON serialization options.</param>
27
    /// <returns>Engine options.</returns>
28
    public static WasmPolicyEngineOptions DefaultWithJsonOptions(Action<JsonSerializerOptions> options)
29
    {
30
        ArgumentNullException.ThrowIfNull(options);
22✔
31
        var result = new WasmPolicyEngineOptions();
22✔
32
        options(result.SerializationOptions);
22✔
33
        return result;
22✔
34
    }
35

36
    private readonly JsonSerializerOptions _jsonSerializationOptions = new()
1,762✔
37
    {
1,762✔
38
        Converters = { RegoSetJsonConverterFactory.Instance },
1,762✔
39
        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
1,762✔
40
    };
1,762✔
41

42
    /// <summary>
43
    /// Creates new <see cref="WasmPolicyEngineOptions"/> instance.
44
    /// </summary>
45
    public WasmPolicyEngineOptions()
1,762✔
46
    {
47
        _makeBuiltins = DefaultBuiltins;
1,762✔
48
    }
1,762✔
49

50
    /// <summary>
51
    /// Minimal number of 64k pages available for WASM engine.
52
    /// </summary>
53
    public long MinMemoryPages { get; init; } = 2;
3,971✔
54

55
    /// <summary>
56
    /// Maximum number of 64k pages available for WASM engine.
57
    /// </summary>
58
    public long? MaxMemoryPages { get; init; }
2,212✔
59

60
    /// <summary>
61
    /// Max ABI versions to use.
62
    /// Can be useful for cases when you want evaluator to use lower ABI version than policy supports.
63
    /// </summary>
64
    public Version? MaxAbiVersion { get; init; }
2,243✔
65

66
    /// <summary>
67
    /// Directory used to keep unpacked policies. If <c>null</c> policies will be kept in memory.
68
    /// </summary>
69
    /// <remarks>
70
    /// Directory must exist and requires write permissions.
71
    /// </remarks>
72
    public string? CachePath { get; init; }
2,039✔
73

74
    /// <summary>
75
    /// If <c>true</c> errors in built-in functions will be threaded as exceptions that halt policy evaluation.
76
    /// </summary>
77
    public bool StrictBuiltinErrors { get; init; }
2,554✔
78

79
    /// <summary>
80
    /// OPA bundle signature validation options.
81
    /// </summary>
82
    public SignatureValidationOptions SignatureValidation { get; init; } = new();
5,347✔
83

84
    /// <summary>
85
    /// JSON serialization options.
86
    /// </summary>
87
    /// <exception cref="ArgumentNullException">Value is <c>null</c>.</exception>
88
    public JsonSerializerOptions SerializationOptions
89
    {
90
        get => _jsonSerializationOptions;
51,557✔
91
        init
92
        {
93
            if (value == null)
1,662!
94
                throw new ArgumentNullException(nameof(value));
×
95

96
            _jsonSerializationOptions = new(value)
1,662✔
97
            {
1,662✔
98
                Converters = { RegoSetJsonConverterFactory.Instance },
1,662✔
99
            };
1,662✔
100
        }
1,662✔
101
    }
102

103
    private IOpaImportsAbi DefaultBuiltins()
104
    {
105
        var bo = new WasmBuiltinsOptions();
333✔
106
        return new CompositeImportsHandler(bo.DefaultBuiltins, bo.CustomBuiltins.AsReadOnly(), _importsCache);
333✔
107
    }
108

109
    internal IOpaImportsAbi Builtins() => _makeBuiltins();
2,208✔
110

111
    /// <summary>
112
    /// Configure OPA built-ins.
113
    /// </summary>
114
    public void ConfigureBuiltins(Action<WasmBuiltinsOptions> configure)
115
    {
116
        ArgumentNullException.ThrowIfNull(configure);
1,651✔
117

118
        _makeBuiltins = () =>
1,651✔
119
        {
1,651✔
120
            var bo = new WasmBuiltinsOptions();
1,875✔
121
            configure(bo);
1,875✔
122
            return new CompositeImportsHandler(bo.DefaultBuiltins, bo.CustomBuiltins.AsReadOnly(), _importsCache);
1,875✔
123
        };
1,651✔
124
    }
1,651✔
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc