michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "experimental" michael@0: }; michael@0: michael@0: const { Cu, components } = require("chrome"); michael@0: const { defer } = require("../core/promise"); michael@0: const { merge } = require("../util/object"); michael@0: michael@0: const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {}); michael@0: michael@0: /** michael@0: * Reads a URI and returns a promise. michael@0: * michael@0: * @param uri {string} The URI to read michael@0: * @param [options] {object} This parameter can have any or all of the following michael@0: * fields: `charset`. By default the `charset` is set to 'UTF-8'. michael@0: * michael@0: * @returns {promise} The promise that will be resolved with the content of the michael@0: * URL given. michael@0: * michael@0: * @example michael@0: * let promise = readURI('resource://gre/modules/NetUtil.jsm', { michael@0: * charset: 'US-ASCII' michael@0: * }); michael@0: */ michael@0: function readURI(uri, options) { michael@0: options = options || {}; michael@0: let charset = options.charset || 'UTF-8'; michael@0: michael@0: let channel = NetUtil.newChannel(uri, charset, null); michael@0: michael@0: let { promise, resolve, reject } = defer(); michael@0: michael@0: try { michael@0: NetUtil.asyncFetch(channel, function (stream, result) { michael@0: if (components.isSuccessCode(result)) { michael@0: let count = stream.available(); michael@0: let data = NetUtil.readInputStreamToString(stream, count, { charset : charset }); michael@0: michael@0: resolve(data); michael@0: } else { michael@0: reject("Failed to read: '" + uri + "' (Error Code: " + result + ")"); michael@0: } michael@0: }); michael@0: } michael@0: catch (e) { michael@0: reject("Failed to read: '" + uri + "' (Error: " + e.message + ")"); michael@0: } michael@0: michael@0: return promise; michael@0: } michael@0: michael@0: exports.readURI = readURI; michael@0: michael@0: /** michael@0: * Reads a URI synchronously. michael@0: * This function is intentionally undocumented to favorites the `readURI` usage. michael@0: * michael@0: * @param uri {string} The URI to read michael@0: * @param [charset] {string} The character set to use when read the content of michael@0: * the `uri` given. By default is set to 'UTF-8'. michael@0: * michael@0: * @returns {string} The content of the URI given. michael@0: * michael@0: * @example michael@0: * let data = readURISync('resource://gre/modules/NetUtil.jsm'); michael@0: */ michael@0: function readURISync(uri, charset) { michael@0: charset = typeof charset === "string" ? charset : "UTF-8"; michael@0: michael@0: let channel = NetUtil.newChannel(uri, charset, null); michael@0: let stream = channel.open(); michael@0: michael@0: let count = stream.available(); michael@0: let data = NetUtil.readInputStreamToString(stream, count, { charset : charset }); michael@0: michael@0: stream.close(); michael@0: michael@0: return data; michael@0: } michael@0: michael@0: exports.readURISync = readURISync;