microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/request.ts
29lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
3fb37ad5unknown10 years ago | 4 | import http = require("http"); |
f338085detatanova4 years ago | 5 | import https = require("https"); |
3fb37ad5unknown10 years ago | 6 | |
| 7 | export class Request { | |
f338085detatanova4 years ago | 8 | public static request(url: string, expectStatusOK = false, isHttps = false): Promise<string> { |
ce5e88eeYuri Skorokhodov5 years ago | 9 | return new Promise((resolve, reject) => { |
d8f741bfEzio Li1 years ago | 10 | // CodeQL [SM04580] extension send all requests to 'localhost:port' which are built by local application |
09f6024fHeniker4 years ago | 11 | const req = (isHttps ? https : http).get(url, res => { |
ce5e88eeYuri Skorokhodov5 years ago | 12 | let responseString = ""; |
| 13 | res.on("data", (data: Buffer) => { | |
| 14 | responseString += data.toString(); | |
| 15 | }); | |
| 16 | res.on("end", () => { | |
| 17 | if (expectStatusOK && res.statusCode !== 200) { | |
| 18 | reject(new Error(responseString)); | |
| 19 | } else { | |
| 20 | resolve(responseString); | |
| 21 | } | |
| 22 | }); | |
2f10b3adunknown10 years ago | 23 | }); |
ce5e88eeYuri Skorokhodov5 years ago | 24 | req.on("error", (err: Error) => { |
| 25 | reject(err); | |
2f10b3adunknown10 years ago | 26 | }); |
3fb37ad5unknown10 years ago | 27 | }); |
| 28 | } | |
| 29 | } |