Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 var http = require("http"); | |
| 6 var https = require("https"); | |
| 7 var parseUrl = require("url").parse; | |
| 8 | |
| 9 function request(url) | |
|
lushnikov
2016/07/22 21:06:04
can we rename this into fetch? (to avoid conflicti
chenwilliam
2016/07/22 23:20:31
Done.
| |
| 10 { | |
| 11 return new Promise((resolve, reject) => { | |
| 12 var request; | |
| 13 var protocol = parseUrl(url).protocol; | |
| 14 | |
| 15 if (protocol === "https:") | |
| 16 request = https.get(url, handleResponse); | |
| 17 else if (protocol === "http:") | |
|
lushnikov
2016/07/22 21:06:05
You never have any HTTP urls - let's remove this
chenwilliam
2016/07/22 23:20:31
Can't remove it because we make a request to http:
| |
| 18 request = http.get(url, handleResponse); | |
| 19 else | |
| 20 reject(new Error(`Invalid protocol for url: ${url}`)) | |
|
lushnikov
2016/07/22 21:06:04
you want to return here as well, (otherwise, the r
chenwilliam
2016/07/22 23:20:32
Done.
| |
| 21 | |
| 22 function handleResponse(response) | |
| 23 { | |
| 24 if (response.statusCode !== 200) { | |
| 25 return reject(new Error(`Request error: + ${response.statusCode} `)); | |
|
lushnikov
2016/07/22 21:06:04
unfortunately, our style guide does not allow this
chenwilliam
2016/07/22 23:20:32
Done.
| |
| 26 } | |
| 27 var body = ""; | |
| 28 response.on("data", chunk => body += chunk); | |
| 29 response.on("end", () => resolve(body)); | |
| 30 } | |
| 31 | |
| 32 request.on("error", (err) => reject(err)); | |
| 33 }); | |
| 34 } | |
| 35 | |
| 36 module.exports = request; | |
| OLD | NEW |