Skip to content

Instantly share code, notes, and snippets.

@aron

aron/opa.js.diff Secret

Created December 19, 2021 15:15
Show Gist options
  • Save aron/41eac08e8f1469a1c8139766524d3bad to your computer and use it in GitHub Desktop.
Save aron/41eac08e8f1469a1c8139766524d3bad to your computer and use it in GitHub Desktop.
diff --git a/src/opa.js b/src/opa.js
index 0991b74..6df85b9 100644
--- a/src/opa.js
+++ b/src/opa.js
@@ -2,7 +2,16 @@
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
const builtIns = require("./builtins/index");
-const util = require("util");
+
+let { TextEncoder, TextDecoder } = global;
+if (typeof TextEncoder === "undefined") {
+ try {
+ TextEncoder = require("util").TextEncoder;
+ TextDecoder = require("util").TextDecoder;
+ } catch (err) {
+ throw new Error("util module missing");
+ }
+}
/**
* @param {WebAssembly.Memory} mem
@@ -35,7 +44,7 @@ function _loadJSON(wasmInstance, memory, value) {
valueBuf = new Uint8Array(value);
} else {
const valueAsText = JSON.stringify(value);
- valueBuf = new util.TextEncoder().encode(valueAsText);
+ valueBuf = new TextEncoder().encode(valueAsText);
}
const valueBufLen = valueBuf.byteLength;
@@ -79,7 +88,7 @@ function _dumpJSONRaw(memory, addr) {
}
const utf8View = new Uint8Array(memory.buffer, addr, idx - addr);
- const jsonAsText = new util.TextDecoder().decode(utf8View);
+ const jsonAsText = new TextDecoder().decode(utf8View);
return JSON.parse(jsonAsText);
}
@@ -301,7 +310,7 @@ class LoadedPolicy {
inputBuf = new Uint8Array(input);
} else {
const inputAsText = JSON.stringify(input);
- inputBuf = new util.TextEncoder().encode(inputAsText);
+ inputBuf = new TextEncoder().encode(inputAsText);
}
inputAddr = this.dataHeapPtr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment