regula-wasi 3.2.4 → 3.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -89,6 +89,16 @@ if (result.summary.rule_results.FAIL > 0) {
89
89
  console.error('Security violations found!');
90
90
  process.exit(1);
91
91
  }
92
+
93
+ // Error handling with detailed error information
94
+ try {
95
+ const result = await runRegula('./main.tf');
96
+ } catch (error) {
97
+ // RegulaError includes stdout, stderr, exitCode, and command
98
+ console.error('Error:', error.message);
99
+ console.error('Stderr:', error.stderr);
100
+ console.error('Exit code:', error.exitCode);
101
+ }
92
102
  ```
93
103
 
94
104
  #### API Options
@@ -103,6 +113,16 @@ if (result.summary.rule_results.FAIL > 0) {
103
113
  | `noIgnore` | boolean | Disable .gitignore filtering |
104
114
  | `varFiles` | string[] | Terraform variable files (.tfvars) to use |
105
115
 
116
+ #### Error Handling
117
+
118
+ When an error occurs, a `RegulaError` is thrown with the following properties:
119
+
120
+ - `message` - Error description
121
+ - `stdout` - Standard output from regula command
122
+ - `stderr` - Standard error output (includes OPA errors and warnings)
123
+ - `exitCode` - Exit code from regula process
124
+ - `command` - The command that was executed
125
+
106
126
  ### Prebuilt Binary
107
127
 
108
128
  Download from [Releases](https://github.com/nonfx/regula/releases) for your platform.
package/index.d.ts CHANGED
@@ -1,3 +1,27 @@
1
+ /**
2
+ * Custom error class for Regula execution errors
3
+ */
4
+ export class RegulaError extends Error {
5
+ /** Standard output from regula command */
6
+ stdout: string;
7
+ /** Standard error output from regula command */
8
+ stderr: string;
9
+ /** Exit code from regula process */
10
+ exitCode: number;
11
+ /** The command that was executed */
12
+ command: string;
13
+
14
+ constructor(
15
+ message: string,
16
+ details: {
17
+ stdout: string;
18
+ stderr: string;
19
+ exitCode: number;
20
+ command: string;
21
+ }
22
+ );
23
+ }
24
+
1
25
  export interface RegulaOptions {
2
26
  /** Input type: auto, tf, tf-plan, cfn, k8s, arm */
3
27
  inputType?: "auto" | "tf" | "tf-plan" | "cfn" | "k8s" | "arm";
@@ -89,6 +113,7 @@ export function validate(
89
113
  declare const _default: {
90
114
  runRegula: typeof runRegula;
91
115
  validate: typeof validate;
116
+ RegulaError: typeof RegulaError;
92
117
  };
93
118
 
94
119
  export default _default;
package/index.js CHANGED
@@ -5,6 +5,20 @@ import { dirname, join } from "path";
5
5
  const __filename = fileURLToPath(import.meta.url);
6
6
  const __dirname = dirname(__filename);
7
7
 
8
+ /**
9
+ * Custom error class for Regula execution errors
10
+ */
11
+ export class RegulaError extends Error {
12
+ constructor(message, { stdout, stderr, exitCode, command }) {
13
+ super(message);
14
+ this.name = "RegulaError";
15
+ this.stdout = stdout;
16
+ this.stderr = stderr;
17
+ this.exitCode = exitCode;
18
+ this.command = command;
19
+ }
20
+ }
21
+
8
22
  /**
9
23
  * Run regula on the specified path(s) and return parsed JSON results.
10
24
  * @param {string|string[]} paths - Path(s) to IaC files or directories
@@ -17,6 +31,7 @@ const __dirname = dirname(__filename);
17
31
  * @param {boolean} options.noIgnore - Disable .gitignore filtering
18
32
  * @param {string[]} options.varFiles - Terraform variable files to use
19
33
  * @returns {Promise<Object>} - Parsed regula output with rule_results and summary
34
+ * @throws {RegulaError} - Throws RegulaError with stdout, stderr, exitCode, and command properties
20
35
  */
21
36
  export async function runRegula(paths, options = {}) {
22
37
  const pathArray = Array.isArray(paths) ? paths : [paths];
@@ -68,21 +83,38 @@ export async function runRegula(paths, options = {}) {
68
83
 
69
84
  return new Promise((resolve, reject) => {
70
85
  const cliPath = join(__dirname, "cli.js");
86
+ const command = `node ${cliPath} ${args.join(" ")}`;
71
87
 
72
88
  execFile("node", [cliPath, ...args], { maxBuffer: 50 * 1024 * 1024 }, (error, stdout, stderr) => {
73
- if (stderr) {
74
- console.error(stderr);
89
+ const exitCode = error?.code || 0;
90
+
91
+ // If there was an execution error (not just a non-zero exit), reject with details
92
+ if (error && error.code !== 1) {
93
+ // Exit code 1 is expected for security violations, so we don't treat it as an error
94
+ reject(
95
+ new RegulaError(`Regula execution failed: ${error.message}`, {
96
+ stdout,
97
+ stderr,
98
+ exitCode,
99
+ command,
100
+ })
101
+ );
102
+ return;
75
103
  }
76
104
 
105
+ // Try to parse the output
77
106
  try {
78
107
  const result = JSON.parse(stdout);
79
108
  resolve(result);
80
109
  } catch (parseError) {
81
- if (error) {
82
- reject(new Error(`Regula failed: ${error.message}\nOutput: ${stdout}`));
83
- } else {
84
- reject(new Error(`Failed to parse regula output: ${stdout}`));
85
- }
110
+ reject(
111
+ new RegulaError(`Failed to parse regula output: ${parseError.message}`, {
112
+ stdout,
113
+ stderr,
114
+ exitCode,
115
+ command,
116
+ })
117
+ );
86
118
  }
87
119
  });
88
120
  });
@@ -94,9 +126,10 @@ export async function runRegula(paths, options = {}) {
94
126
  * @param {string|string[]} paths - Path(s) to IaC files or directories
95
127
  * @param {Object} options - Optional configuration
96
128
  * @returns {Promise<Object>} - Object with rule_results and summary
129
+ * @throws {RegulaError} - Throws RegulaError with stdout, stderr, exitCode, and command properties
97
130
  */
98
131
  export async function validate(paths, options = {}) {
99
132
  return runRegula(paths, options);
100
133
  }
101
134
 
102
- export default { runRegula, validate };
135
+ export default { runRegula, validate, RegulaError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "regula-wasi",
3
- "version": "3.2.4",
3
+ "version": "3.2.5",
4
4
  "description": "Infrastructure as Code security and compliance evaluation tool (WASI build). Fork of fugue/regula with security patches.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/regula.wasm CHANGED
Binary file