| OLD | NEW |
| (Empty) | |
| 1 // Copyright 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 fs = require("fs"); |
| 6 var http = require("http"); |
| 7 var https = require("https"); |
| 8 var parseURL = require("url").parse; |
| 9 var Stream = require("stream").Transform; |
| 10 |
| 11 function fetch(url) |
| 12 { |
| 13 return new Promise(fetchPromise); |
| 14 |
| 15 function fetchPromise(resolve, reject) |
| 16 { |
| 17 var request; |
| 18 var protocol = parseURL(url).protocol; |
| 19 var handleResponse = getCallback.bind(null, resolve, reject); |
| 20 if (protocol === "https:") { |
| 21 request = https.get(url, handleResponse); |
| 22 } else if (protocol === "http:") { |
| 23 request = http.get(url, handleResponse); |
| 24 } else { |
| 25 reject(new Error(`Invalid protocol for url: ${url}`)); |
| 26 return; |
| 27 } |
| 28 request.on("error", err => reject(err)); |
| 29 } |
| 30 |
| 31 function getCallback(resolve, reject, response) |
| 32 { |
| 33 if (response.statusCode !== 200) { |
| 34 reject(new Error(`Request error: + ${response.statusCode}`)); |
| 35 return; |
| 36 } |
| 37 var body = new Stream(); |
| 38 response.on("data", chunk => body.push(chunk)); |
| 39 response.on("end", () => resolve(body.read())); |
| 40 } |
| 41 } |
| 42 |
| 43 function atob(str) |
| 44 { |
| 45 return new Buffer(str, "base64").toString("binary"); |
| 46 } |
| 47 |
| 48 |
| 49 function isFile(path) |
| 50 { |
| 51 try { |
| 52 return fs.statSync(path).isFile(); |
| 53 } catch (e) { |
| 54 return false; |
| 55 } |
| 56 } |
| 57 |
| 58 module.exports = { |
| 59 fetch, |
| 60 atob, |
| 61 isFile, |
| 62 }; |
| OLD | NEW |