react-native-macos/Libraries/JavaScriptAppEngine/Initialization/SourceMapsUtils.js

89 строки
2.5 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule SourceMapsUtils
* @flow
*/
'use strict';
const Promise = require('Promise');
const NativeModules = require('NativeModules');
const SourceMapConsumer = require('SourceMap').SourceMapConsumer;
const SourceMapURL = require('./source-map-url');
const RCTSourceCode = NativeModules.SourceCode;
const RCTNetworking = NativeModules.Networking;
const SourceMapsUtils = {
fetchMainSourceMap(): Promise {
return SourceMapsUtils._getMainSourceMapURL().then(url =>
SourceMapsUtils.fetchSourceMap(url)
);
},
fetchSourceMap(sourceMappingURL: string): Promise {
return fetch(sourceMappingURL)
.then(response => response.text())
.then(map => new SourceMapConsumer(map));
},
extractSourceMapURL(data: ({url?:string, text?:string, fullSourceMappingURL?:string})): ?string {
const url = data.url;
const text = data.text;
const fullSourceMappingURL = data.fullSourceMappingURL;
if (fullSourceMappingURL) {
return fullSourceMappingURL;
}
const mapURL = SourceMapURL.getFrom(text);
if (!mapURL) {
return null;
}
if (!url) {
return null;
}
const baseURLs = url.match(/(.+:\/\/.*?)\//);
if (!baseURLs || baseURLs.length < 2) {
return null;
}
return baseURLs[1] + mapURL;
},
_getMainSourceMapURL(): Promise {
if (global.RAW_SOURCE_MAP) {
return Promise.resolve(global.RAW_SOURCE_MAP);
}
if (!RCTSourceCode) {
return Promise.reject(new Error('RCTSourceCode module is not available'));
}
if (!RCTNetworking) {
// Used internally by fetch
return Promise.reject(new Error('RCTNetworking module is not available'));
}
const scriptText = RCTSourceCode.getScriptText();
if (scriptText) {
return scriptText
.then(SourceMapsUtils.extractSourceMapURL)
.then((url) => {
if (url === null) {
return Promise.reject(new Error('No source map URL found. May be running from bundled file.'));
}
return Promise.resolve(url);
});
} else {
// Running in mock-config mode
return Promise.reject(new Error('Couldn\'t fetch script text'));
}
},
};
module.exports = SourceMapsUtils;