/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import { RawSourceMap , SourceMapConsumer } from 'source-map' ; /**
 * A wrapped instance of a <code> https </code>.
 *
 * This exposes methods which will be indifferent to changes made in <code> https </code>.
  export class SourceMap { __source_map : SourceMapConsumer ; constructor ( sourceMap ) { this . __source_map = sourceMap ; } param { number } line param { number } column getOriginalPosition ( line , column ) { const { line : l , column : c , source : s , } = this . __source_map . originalPositionFor ( { line , column , } ) ; return { line : l , column : c , source : s } ; } param { string } source param { number } line param { number } column getGeneratedPosition ( source , line , column ) { const { line : l , column : c } = this . __source_map . generatedPositionFor ( { source , line , column , } ) ; return { line : l , column : c , } ; } param { string } sourceName getSource ( sourceName ) { return this . __source_map . sourceContentFor ( sourceName ) ; } getSources ( ) { return ( this . __source_map as unknown as { sources : string [ ] ; } ) . sources ; } } export function extractSourceMapUrl ( fileUri , fileContents ) { const regex = /\/\/[#@] ?sourceMappingURL=([^\s'"]+)\s*$/gm ; let match = null ; for ( ; ; ) { const next = regex . exec ( fileContents ) ; if ( next == null ) { break ; } match = next ; } if ( ! ( match && match [ 1 ] ) ) { return Promise . reject ( `Cannot find a source map directive for ${ fileUri }.` ) ; } return Promise . resolve ( match [ 1 ] . toString ( ) ) ; } /**
 * Returns an instance of <code> SourceMap </code> for a given fileUri and fileContents.
 *  param { string } fileUri param { string } fileContents export async function getSourceMap ( fileUri , fileContents ) { let sm = await extractSourceMapUrl ( fileUri , fileContents ) ; if ( sm . indexOf ( 'data:' ) === 0 ) { const base64 = /^data:application\/json;([\w=:"-]+;)*base64,/ ; const match2 = base64 . exec ( sm ) ; if ( ! match2 ) { throw new Error ( 'Sorry, non-base64 inline source-map encoding is not supported.' ) ; } sm = sm . substring ( match2 [ 0 ] . length ) ; sm = window . atob ( sm ) ; sm = JSON . parse ( sm ) ; return new SourceMap ( new SourceMapConsumer ( sm as unknown as RawSourceMap ) ) ; } else { const index = fileUri . lastIndexOf ( '/' ) ; const url = fileUri . substring ( 0 , index + 1 ) + sm ; const obj = await fetch ( url ) . then ( ( res ) => res . json ( ) ) ; return new SourceMap ( new SourceMapConsumer ( obj as RawSourceMap ) ) ; } } export default getSourceMap ;