From c5835c9106596b58a9b1769803ba08abc8c0c062 Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Thu, 27 Feb 2020 17:11:00 +0100 Subject: [PATCH] Support read_file only on non-browser environments (#496) * add missing fs import * read_file doesn't work under browser * different appraoch towards read_file * remove dangling line --- packages/autobahn/lib/util.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/autobahn/lib/util.js b/packages/autobahn/lib/util.js index 00c60024..45508680 100644 --- a/packages/autobahn/lib/util.js +++ b/packages/autobahn/lib/util.js @@ -417,7 +417,8 @@ let sleep = async function sleep (ms) { return new Promise(resolve => setTimeout(resolve, ms)); }; -let read_file = async function read_file (path) { +let _read_file = async function read_file (path) { + let fs = require("fs"); return new Promise((resolve, reject) => { fs.readFile(path, function (err, data) { if (err) { @@ -429,6 +430,12 @@ let read_file = async function read_file (path) { }); }; +if ('fs' in global) { + exports.read_file = _read_file; +} else { + exports.read_file = null; +} + exports.handle_error = handle_error; exports.rand_normal = rand_normal; @@ -441,4 +448,3 @@ exports.new_global_id = new_global_id; exports.deferred_factory = deferred_factory; exports.promise = promise; exports.sleep = sleep; -exports.read_file = read_file;