Repository: iopipe/iopipe-js-core (js)

Issue:
Payload field plugins[].enabled should always be boolean
If a plugin passes through the string value of a `IOPIPE_*_ENABLED` environment variable, the agent should properly cast any string/numeric value to a boolean.

Pull request description:
fix(strToBool): Cast plugins[].enabled as boolean
Closes #296

Patch:
diff --git a/src/report.js b/src/report.js
index 464fc0e..ad1bec8 100644
--- a/src/report.js
+++ b/src/report.js
@@ -1,5 +1,7 @@
 import os from 'os';
+
 import { sendReport } from './sendReport';
+import strToBool from './util/strToBool';
 
 import {
   COLDSTART,
@@ -70,6 +72,13 @@ class Report {
 
         if (meta) {
           meta.uploads = meta.uploads || [];
+          if (meta.enabled && typeof meta.enabled === 'number') {
+            meta.enabled = Boolean(meta.enabled);
+          }
+
+          if (meta.enabled && typeof meta.enabled === 'string') {
+            meta.enabled = strToBool(meta.enabled);
+          }
         }
 
         return meta;
diff --git a/src/util/strToBool.js b/src/util/strToBool.js
new file mode 100644
index 0000000..7913a0b
--- /dev/null
+++ b/src/util/strToBool.js
@@ -0,0 +1,15 @@
+export default function(string) {
+  switch (string.toLowerCase().trim()) {
+    case 'true':
+    case 't':
+    case '1':
+      return true;
+    case 'false':
+    case 'f':
+    case '0':
+    case null:
+      return false;
+    default:
+      return Boolean(string);
+  }
+}


Test patch:
diff --git a/src/util/strToBool.test.js b/src/util/strToBool.test.js
new file mode 100644
index 0000000..8274d47
--- /dev/null
+++ b/src/util/strToBool.test.js
@@ -0,0 +1,13 @@
+import strToBool from './strToBool';
+
+test('Strings are cast as bools', () => {
+  expect(strToBool('true')).toBe(true);
+  expect(strToBool('True')).toBe(true);
+  expect(strToBool('t')).toBe(true);
+  expect(strToBool('1')).toBe(true);
+
+  expect(strToBool('false')).toBe(false);
+  expect(strToBool('False')).toBe(false);
+  expect(strToBool('f')).toBe(false);
+  expect(strToBool('0')).toBe(false);
+});