From 5a6d3f99e2a3c5e17e867f22e631bac6f2da2c11 Mon Sep 17 00:00:00 2001 From: Ovidiu Viorel Iepure Date: Tue, 8 Nov 2016 12:19:36 -0800 Subject: [PATCH] Use native Promises Summary: Use native Promises in React Native Packager. Remove all the non-standard `Promise.done()` calls throughout the codebase & replace `Promise.denodeify` with the stand-alone `denodeify` module. Reviewed By: davidaurelio Differential Revision: D4146965 fbshipit-source-id: 1730531c914863ac3c52626801d9f91c28eed717 --- .../src/AssetServer/__tests__/AssetServer-test.js | 2 -- react-packager/src/AssetServer/index.js | 5 ++--- .../src/Bundler/__tests__/Bundle-test.js | 1 - react-packager/src/Bundler/index.js | 5 ++--- react-packager/src/JSTransformer/index.js | 6 +++--- react-packager/src/Server/index.js | 15 +++++++-------- 6 files changed, 14 insertions(+), 20 deletions(-) diff --git a/react-packager/src/AssetServer/__tests__/AssetServer-test.js b/react-packager/src/AssetServer/__tests__/AssetServer-test.js index 4bbbdcec..7e70d6bc 100644 --- a/react-packager/src/AssetServer/__tests__/AssetServer-test.js +++ b/react-packager/src/AssetServer/__tests__/AssetServer-test.js @@ -13,8 +13,6 @@ jest.disableAutomock(); jest.mock('fs'); -const Promise = require('promise'); - const AssetServer = require('../'); const crypto = require('crypto'); const {EventEmitter} = require('events'); diff --git a/react-packager/src/AssetServer/index.js b/react-packager/src/AssetServer/index.js index 14ea7f28..e1a02f65 100644 --- a/react-packager/src/AssetServer/index.js +++ b/react-packager/src/AssetServer/index.js @@ -8,10 +8,9 @@ */ 'use strict'; -const Promise = require('promise'); - const crypto = require('crypto'); const declareOpts = require('../lib/declareOpts'); +const denodeify = require('denodeify'); const fs = require('fs'); const getAssetDataFromName = require('../node-haste').getAssetDataFromName; const path = require('path'); @@ -23,7 +22,7 @@ function timeoutableDenodeify(fsFunc, timeout) { return function raceWrapper(...args) { return Promise.race([ createTimeoutPromise(timeout), - Promise.denodeify(fsFunc).apply(this, args) + denodeify(fsFunc).apply(this, args) ]); }; } diff --git a/react-packager/src/Bundler/__tests__/Bundle-test.js b/react-packager/src/Bundler/__tests__/Bundle-test.js index 9aef2b35..815837b3 100644 --- a/react-packager/src/Bundler/__tests__/Bundle-test.js +++ b/react-packager/src/Bundler/__tests__/Bundle-test.js @@ -12,7 +12,6 @@ jest.disableAutomock(); const Bundle = require('../Bundle'); const ModuleTransport = require('../../lib/ModuleTransport'); -const Promise = require('Promise'); const SourceMapGenerator = require('source-map').SourceMapGenerator; const crypto = require('crypto'); diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index e6a1dd3a..4aaecc51 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -11,8 +11,6 @@ 'use strict'; -const Promise = require('promise'); - const assert = require('assert'); const fs = require('fs'); const path = require('path'); @@ -25,12 +23,13 @@ const ModuleTransport = require('../lib/ModuleTransport'); const declareOpts = require('../lib/declareOpts'); const imageSize = require('image-size'); const version = require('../../../../package.json').version; +const denodeify = require('denodeify'); import AssetServer from '../AssetServer'; import Module from '../node-haste/Module'; import ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; -const sizeOf = Promise.denodeify(imageSize); +const sizeOf = denodeify(imageSize); const noop = () => {}; diff --git a/react-packager/src/JSTransformer/index.js b/react-packager/src/JSTransformer/index.js index f301de12..712489db 100644 --- a/react-packager/src/JSTransformer/index.js +++ b/react-packager/src/JSTransformer/index.js @@ -9,9 +9,9 @@ 'use strict'; const Logger = require('../Logger'); -const Promise = require('promise'); const declareOpts = require('../lib/declareOpts'); +const denodeify = require('denodeify'); const os = require('os'); const util = require('util'); const workerFarm = require('worker-farm'); @@ -99,8 +99,8 @@ class Transformer { ['minify', 'transformAndExtractDependencies'], opts.transformTimeoutInterval, ); - this._transform = Promise.denodeify(this._workers.transformAndExtractDependencies); - this.minify = Promise.denodeify(this._workers.minify); + this._transform = denodeify(this._workers.transformAndExtractDependencies); + this.minify = denodeify(this._workers.minify); } } diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index 178f7ea2..f57a17e8 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -14,7 +14,6 @@ const getPlatformExtension = require('../node-haste').getPlatformExtension; const Bundler = require('../Bundler'); const MultipartResponse = require('./MultipartResponse'); const ProgressBar = require('progress'); -const Promise = require('promise'); const SourceMapConsumer = require('source-map').SourceMapConsumer; const declareOpts = require('../lib/declareOpts'); @@ -505,15 +504,14 @@ class Server { // This is safe as the asset url contains a hash of the asset. res.setHeader('Cache-Control', 'max-age=31536000'); res.end(this._rangeRequestMiddleware(req, res, data, assetPath)); + print(log(createActionEndEntry(processingAssetRequestLogEntry)), ['asset']); }, error => { console.error(error.stack); res.writeHead('404'); res.end('Asset not found'); } - ).done(() => { - print(log(createActionEndEntry(processingAssetRequestLogEntry)), ['asset']); - }); + ); } optionsHash(options) { @@ -776,15 +774,16 @@ class Server { }); }); }).then( - stack => res.end(JSON.stringify({stack: stack})), + stack => { + res.end(JSON.stringify({stack: stack})); + print(log(createActionEndEntry(symbolicatingLogEntry))); + }, error => { console.error(error.stack || error); res.statusCode = 500; res.end(JSON.stringify({error: error.message})); } - ).done(() => { - print(log(createActionEndEntry(symbolicatingLogEntry))); - }); + ); } _sourceMapForURL(reqUrl) {