addon-sdk/source/lib/sdk/net/url.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/net/url.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "experimental"
    1.12 +};
    1.13 +
    1.14 +const { Cu, components } = require("chrome");
    1.15 +const { defer } = require("../core/promise");
    1.16 +const { merge } = require("../util/object");
    1.17 +
    1.18 +const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {});
    1.19 +
    1.20 +/**
    1.21 + * Reads a URI and returns a promise.
    1.22 + *
    1.23 + * @param uri {string} The URI to read
    1.24 + * @param [options] {object} This parameter can have any or all of the following
    1.25 + * fields: `charset`. By default the `charset` is set to 'UTF-8'.
    1.26 + *
    1.27 + * @returns {promise}  The promise that will be resolved with the content of the
    1.28 + *          URL given.
    1.29 + *
    1.30 + * @example
    1.31 + *  let promise = readURI('resource://gre/modules/NetUtil.jsm', {
    1.32 + *    charset: 'US-ASCII'
    1.33 + *  });
    1.34 + */
    1.35 +function readURI(uri, options) {
    1.36 +  options = options || {};
    1.37 +  let charset = options.charset || 'UTF-8';
    1.38 +
    1.39 +  let channel = NetUtil.newChannel(uri, charset, null);
    1.40 +
    1.41 +  let { promise, resolve, reject } = defer();
    1.42 +
    1.43 +  try {
    1.44 +    NetUtil.asyncFetch(channel, function (stream, result) {
    1.45 +      if (components.isSuccessCode(result)) {
    1.46 +        let count = stream.available();
    1.47 +        let data = NetUtil.readInputStreamToString(stream, count, { charset : charset });
    1.48 +
    1.49 +        resolve(data);
    1.50 +      } else {
    1.51 +        reject("Failed to read: '" + uri + "' (Error Code: " + result + ")");
    1.52 +      }
    1.53 +    });
    1.54 +  }
    1.55 +  catch (e) {
    1.56 +    reject("Failed to read: '" + uri + "' (Error: " + e.message + ")");
    1.57 +  }
    1.58 +
    1.59 +  return promise;
    1.60 +}
    1.61 +
    1.62 +exports.readURI = readURI;
    1.63 +
    1.64 +/**
    1.65 + * Reads a URI synchronously.
    1.66 + * This function is intentionally undocumented to favorites the `readURI` usage.
    1.67 + *
    1.68 + * @param uri {string} The URI to read
    1.69 + * @param [charset] {string} The character set to use when read the content of
    1.70 + *        the `uri` given.  By default is set to 'UTF-8'.
    1.71 + *
    1.72 + * @returns {string} The content of the URI given.
    1.73 + *
    1.74 + * @example
    1.75 + *  let data = readURISync('resource://gre/modules/NetUtil.jsm');
    1.76 + */
    1.77 +function readURISync(uri, charset) {
    1.78 +  charset = typeof charset === "string" ? charset : "UTF-8";
    1.79 +
    1.80 +  let channel = NetUtil.newChannel(uri, charset, null);
    1.81 +  let stream = channel.open();
    1.82 +
    1.83 +  let count = stream.available();
    1.84 +  let data = NetUtil.readInputStreamToString(stream, count, { charset : charset });
    1.85 +
    1.86 +  stream.close();
    1.87 +
    1.88 +  return data;
    1.89 +}
    1.90 +
    1.91 +exports.readURISync = readURISync;

mercurial