From bd7114d00fca021e3d1cf94c5568b214d50e73d0 Mon Sep 17 00:00:00 2001 From: Konstantin Raev Date: Wed, 18 May 2016 08:30:51 -0700 Subject: [PATCH] Added timeouts to fs operations in packager Reviewed By: davidaurelio Differential Revision: D3316555 fbshipit-source-id: edf6e08569b6cb434219f4460367eec0827530fd --- react-packager/src/AssetServer/index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/react-packager/src/AssetServer/index.js b/react-packager/src/AssetServer/index.js index 2b25218c..1a7bb75e 100644 --- a/react-packager/src/AssetServer/index.js +++ b/react-packager/src/AssetServer/index.js @@ -16,9 +16,21 @@ const fs = require('fs'); const getAssetDataFromName = require('node-haste').getAssetDataFromName; const path = require('path'); -const stat = Promise.denodeify(fs.stat); -const readDir = Promise.denodeify(fs.readdir); -const readFile = Promise.denodeify(fs.readFile); +const createTimeoutPromise = (timeout) => new Promise((resolve, reject) => { + setTimeout(reject, timeout, 'fs operation timeout'); +}); +function timeoutableDenodeify(fsFunc, timeout) { + return function raceWrapper(...args) { + return new Promise.race([ + createTimeoutPromise(timeout), + Promise.denodeify(fsFunc).apply(this, args) + ]); + }; +} + +const stat = timeoutableDenodeify(fs.stat, 5000); +const readDir = timeoutableDenodeify(fs.readdir, 5000); +const readFile = timeoutableDenodeify(fs.readFile, 5000); const validateOpts = declareOpts({ projectRoots: {