Repository: onflow/fcl-js
Pull request #1042 (2022-02-25 23:27:16)

[transport-http] Use node require to consume node std libraries
[transport-http] Use node require to consume node std libraries

chasefleming: @JeffreyDoyle Does this error at all if you try to run it as an esmodule in the browser?

Patch:
diff --git a/packages/transport-http/CHANGELOG.md b/packages/transport-http/CHANGELOG.md
index 13aa2799..c33d456f 100644
--- a/packages/transport-http/CHANGELOG.md
+++ b/packages/transport-http/CHANGELOG.md
@@ -1,3 +1,7 @@
+### 0.0.7 -- 2022-02-25
+
+- 2022-02-25 -- Use node require to consume node standard libraries
+
 ### 0.0.6 -- 2022-02-04
 
 - 2022-02-04 -- Remove `0x` prefix from public keys in get account response.
diff --git a/packages/transport-http/package-lock.json b/packages/transport-http/package-lock.json
index 33f4b6d6..0d2742dc 100644
--- a/packages/transport-http/package-lock.json
+++ b/packages/transport-http/package-lock.json
@@ -3547,8 +3547,7 @@
                 "esprima": "^4.0.1",
                 "estraverse": "^5.2.0",
                 "esutils": "^2.0.2",
-                "optionator": "^0.8.1",
-                "source-map": "~0.6.1"
+                "optionator": "^0.8.1"
             },
             "bin": {
                 "escodegen": "bin/escodegen.js",
@@ -4581,7 +4580,6 @@
                 "@types/node": "*",
                 "anymatch": "^3.0.3",
                 "fb-watchman": "^2.0.0",
-                "fsevents": "^2.3.2",
                 "graceful-fs": "^4.2.4",
                 "jest-regex-util": "^27.4.0",
                 "jest-serializer": "^27.4.0",
@@ -5070,9 +5068,6 @@
             "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
             "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
             "dev": true,
-            "dependencies": {
-                "graceful-fs": "^4.1.6"
-            },
             "optionalDependencies": {
                 "graceful-fs": "^4.1.6"
             }
@@ -6662,9 +6657,6 @@
             "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.67.0.tgz",
             "integrity": "sha512-W83AaERwvDiHwHEF/dfAfS3z1Be5wf7n+pO3ZAO5IQadCT2lBTr7WQ2MwZZe+nodbD+n3HtC4OCOAdsOPPcKZQ==",
             "dev": true,
-            "dependencies": {
-                "fsevents": "~2.3.2"
-            },
             "bin": {
                 "rollup": "dist/bin/rollup"
             },
diff --git a/packages/transport-http/package.json b/packages/transport-http/package.json
index 643fb620..72d32342 100644
--- a/packages/transport-http/package.json
+++ b/packages/transport-http/package.json
@@ -38,6 +38,7 @@
     "dependencies": {
         "@onflow/util-address": "^0.0.0",
         "@onflow/util-invariant": "^0.0.0",
-        "@onflow/util-template": "0.0.1"
+        "@onflow/util-template": "0.0.1",
+        "@onflow/util-node-http-modules": "0.0.1"
     }
 }
diff --git a/packages/transport-http/src/http-request.js b/packages/transport-http/src/http-request.js
index d29740b6..55b485d3 100644
--- a/packages/transport-http/src/http-request.js
+++ b/packages/transport-http/src/http-request.js
@@ -1,4 +1,5 @@
 import {invariant} from "@onflow/util-invariant"
+import {getNodeHttpModules} from "@onflow/util-node-http-modules"
 
 class HTTPRequestError extends Error {
   constructor({transport, error, hostname, path, port, method, requestBody, responseBody, responseStatusText, reqOn}) {
@@ -45,16 +46,13 @@ export async function httpRequest({
   try {
     fetchTransport = window?.fetch || fetch
   } catch (e) {}
-  let nodeHttpsTransport
-  try {
-    nodeHttpsTransport = await import("https").catch(_ => undefined)
-  } catch (e) {}
-  let nodeHttpTransport
-  try {
-    nodeHttpTransport = await import("http").catch(_ => undefined)
-  } catch (e) {}
 
-  invariant((fetchTransport || nodeHttpsTransport), "HTTP Request error: Could not find a supported HTTP module.")
+  const {
+    nodeHttpsTransport,
+    nodeHttpTransport
+  } = await getNodeHttpModules()
+
+  invariant((fetchTransport || nodeHttpsTransport || nodeHttpTransport), "HTTP Request error: Could not find a supported HTTP module.")
 
   if (fetchTransport) {
 
diff --git a/packages/util-node-http-modules/CHANGELOG.md b/packages/util-node-http-modules/CHANGELOG.md
new file mode 100644
index 00000000..18c60750
--- /dev/null
+++ b/packages/util-node-http-modules/CHANGELOG.md
@@ -0,0 +1,3 @@
+### 0.0.1 -- 2022-03-03
+
+- 2022-03-03 -- Initial implementation
\ No newline at end of file
diff --git a/packages/util-node-http-modules/README.md b/packages/util-node-http-modules/README.md
new file mode 100644
index 00000000..24f20a9f
--- /dev/null
+++ b/packages/util-node-http-modules/README.md
@@ -0,0 +1,22 @@
+---
+title: Util Node Http Modules
+description: Environment conditional Node Http(s) module loader
+---
+
+# @onflow/util-node-http-modules
+
+Environment conditional Node Http(s) module loader
+
+## Status
+
+- **Last Updated:** March 3rd 2022
+- **Stable:** Yes
+- **Risk of Breaking Change:** High
+
+This package is working and in active development, breaking changes may happen.
+
+## Install
+
+```bash
+npm install --save @onflow/util-node-http-modules
+```
\ No newline at end of file
diff --git a/packages/util-node-http-modules/package-lock.json b/packages/util-node-http-modules/package-lock.json
new file mode 100644
index 00000000..e69de29b
diff --git a/packages/util-node-http-modules/package.json b/packages/util-node-http-modules/package.json
new file mode 100644
index 00000000..94bc9d3e
--- /dev/null
+++ b/packages/util-node-http-modules/package.json
@@ -0,0 +1,42 @@
+{
+    "name": "@onflow/util-node-http-modules",
+    "version": "0.0.1",
+    "description": "Flow SDK Util Get Node Http Modules",
+    "license": "Apache-2.0",
+    "author": "Dapper Labs <dev@dapperlabs.com>",
+    "homepage": "https://onflow.org",
+    "repository": {
+        "type": "git",
+        "url": "git+ssh://git@github.com/onflow/flow-js-sdk.git"
+    },
+    "bugs": {
+        "url": "https://github.com/onflow/flow-js-sdk/issues"
+    },
+    "jest": {
+        "transform": {
+            ".js": "jest-esm-transformer"
+        }
+    },
+    "devDependencies": {
+        "jest": "^27.0.6",
+        "jest-esm-transformer": "1.0.0",
+        "microbundle": "^0.13.3"
+    },
+    "main": "./src/util-node-http-modules.noop.js",
+    "exports": {
+        "node": {
+            "import": "./src/util-node-http-modules.module.js",
+            "require": "./src/util-node-http-modules.cjs.js"
+        },
+        "default": "./src/util-node-http-modules.noop.js"
+    },
+    "scripts": {
+        "alpha": "npm publish --tag alpha",
+        "prepublishOnly": "npm test && npm run build",
+        "test": "jest",
+        "build": "microbundle",
+        "test:watch": "jest --watch",
+        "start": "microbundle watch"
+    },
+    "dependencies": {}
+}
diff --git a/packages/util-node-http-modules/src/util-node-http-modules.cjs.js b/packages/util-node-http-modules/src/util-node-http-modules.cjs.js
new file mode 100644
index 00000000..c884b8e6
--- /dev/null
+++ b/packages/util-node-http-modules/src/util-node-http-modules.cjs.js
@@ -0,0 +1,17 @@
+module.exports = {
+  async getNodeHttpModules() {
+    let nodeHttpsTransport
+    try {
+      nodeHttpsTransport = require("https")
+    } catch (e) {}
+    let nodeHttpTransport
+    try {
+      nodeHttpTransport = require("http")
+    } catch (e) {}
+
+    return {
+      nodeHttpsTransport,
+      nodeHttpTransport
+    }
+  } 
+}
diff --git a/packages/util-node-http-modules/src/util-node-http-modules.module.js b/packages/util-node-http-modules/src/util-node-http-modules.module.js
new file mode 100644
index 00000000..6e6efd15
--- /dev/null
+++ b/packages/util-node-http-modules/src/util-node-http-modules.module.js
@@ -0,0 +1,15 @@
+export async function getNodeHttpModules() {
+  let nodeHttpsTransport
+  try {
+    nodeHttpsTransport = await import("https").catch(e => null)
+  } catch (e) {}
+  let nodeHttpTransport
+  try {
+    nodeHttpTransport = await import("http").catch(e => null)
+  } catch (e) {}
+
+  return {
+    nodeHttpsTransport,
+    nodeHttpTransport
+  }
+}
diff --git a/packages/util-node-http-modules/src/util-node-http-modules.noop.js b/packages/util-node-http-modules/src/util-node-http-modules.noop.js
new file mode 100644
index 00000000..f338092f
--- /dev/null
+++ b/packages/util-node-http-modules/src/util-node-http-modules.noop.js
@@ -0,0 +1,7 @@
+export async function getNodeHttpModules() {
+  return {
+    nodeHttpsTransport: undefined,
+    nodeHttpTransport: undefined,
+  }
+}
+  
\ No newline at end of file


Test patch:
diff --git a/packages/util-node-http-modules/src/util-node-http-modules.test.js b/packages/util-node-http-modules/src/util-node-http-modules.test.js
new file mode 100644
index 00000000..7b3b8a2f
--- /dev/null
+++ b/packages/util-node-http-modules/src/util-node-http-modules.test.js
@@ -0,0 +1,4 @@
+test("placeholder", () => {
+    expect(1).toBe(1)
+})
+  
\ No newline at end of file