import { URI } from 'vscode-uri' ; import { Connection , WorkspaceFolder } from 'vscode-languageserver' ; import { xhr , XHRResponse , getErrorStatusDescription } from 'request-light' ; import * as URL from 'url' ; import { CustomSchemaContentRequest , VSCodeContentRequest } from '../../requestTypes' ; import { isRelativePath , relativeToAbsolutePath } from '../utils/paths' ; import { WorkspaceContextService } from '../yamlLanguageService' ; export interface FileSystem { readFile ( fsPath , encoding ? ) : Promise < string > ; } param uri export const schemaRequestHandler = ( connection , uri , workspaceFolders , workspaceRoot , useVSCodeContentRequest , fs ) : Promise < string > => { if ( ! uri ) { return Promise . reject ( 'No schema specified' ) ; } if ( isRelativePath ( uri ) ) { uri = relativeToAbsolutePath ( workspaceFolders , workspaceRoot , uri ) ; } let scheme = URI . parse ( uri ) . scheme . toLowerCase ( ) ; if ( /^[a-z]:[\\/]/i . test ( uri ) ) { const winUri = URI . file ( uri ) ; scheme = winUri . scheme . toLowerCase ( ) ; uri = winUri . toString ( ) ; } if ( scheme === 'file' ) { const fsPath = URI . parse ( uri ) . fsPath ; return fs . readFile ( fsPath , 'UTF-8' ) . catch ( ( ) => { return '' ; } ) ; } if ( scheme === 'http' || scheme === 'https' ) { if ( useVSCodeContentRequest ) { return connection . sendRequest ( VSCodeContentRequest . type , uri ) . then ( ( responseText ) => { return responseText ; } , ( error ) => { return Promise . reject ( error . message ) ; } ) as Promise < string > ; } const headers = { 'Accept-Encoding' : 'gzip, deflate' } ; return xhr ( { url : uri , followRedirects : 5 , headers } ) . then ( ( response ) => { return response . responseText ; } , ( error ) => { return Promise . reject ( error . responseText || getErrorStatusDescription ( error . status ) || error . toString ( ) ) ; } ) ; } return connection . sendRequest ( CustomSchemaContentRequest . type , uri ) as Promise < string > ; } ; export const workspaceContext = { resolveRelativePath : ( relativePath , resource ) => { return URL . resolve ( resource , relativePath ) ; } , } ;