url.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "experimental"
  7. };
  8. const { Cu, components } = require("chrome");
  9. const { defer } = require("../core/promise");
  10. const { merge } = require("../util/object");
  11. const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {});
  12. /**
  13. * Open a channel synchronously for the URI given, with an optional charset, and
  14. * returns a resolved promise if succeed; rejected promise otherwise.
  15. */
  16. function readSync(uri, charset) {
  17. let { promise, resolve, reject } = defer();
  18. try {
  19. resolve(readURISync(uri, charset));
  20. }
  21. catch (e) {
  22. reject("Failed to read: '" + uri + "' (Error Code: " + e.result + ")");
  23. }
  24. return promise;
  25. }
  26. /**
  27. * Open a channel synchronously for the URI given, with an optional charset, and
  28. * returns a promise.
  29. */
  30. function readAsync(uri, charset) {
  31. let channel = NetUtil.newChannel(uri, charset, null);
  32. let { promise, resolve, reject } = defer();
  33. try {
  34. NetUtil.asyncFetch(channel, function (stream, result) {
  35. if (components.isSuccessCode(result)) {
  36. let count = stream.available();
  37. let data = NetUtil.readInputStreamToString(stream, count, { charset : charset });
  38. resolve(data);
  39. } else {
  40. reject("Failed to read: '" + uri + "' (Error Code: " + result + ")");
  41. }
  42. });
  43. }
  44. catch (e) {
  45. reject("Failed to read: '" + uri + "' (Error: " + e.message + ")");
  46. }
  47. return promise;
  48. }
  49. /**
  50. * Reads a URI and returns a promise. If the `sync` option is set to `true`, the
  51. * promise will be resolved synchronously.
  52. *
  53. * @param uri {string} The URI to read
  54. * @param [options] {object} This parameter can have any or all of the following
  55. * fields: `sync`, `charset`. By default the `charset` is set to 'UTF-8'.
  56. *
  57. * @returns {promise} The promise that will be resolved with the content of the
  58. * URL given.
  59. *
  60. * @example
  61. * let promise = readURI('resource://gre/modules/NetUtil.jsm', {
  62. * sync: true,
  63. * charset: 'US-ASCII'
  64. });
  65. */
  66. function readURI(uri, options) {
  67. options = merge({
  68. charset: "UTF-8",
  69. sync: false
  70. }, options);
  71. return options.sync
  72. ? readSync(uri, options.charset)
  73. : readAsync(uri, options.charset);
  74. }
  75. exports.readURI = readURI;
  76. /**
  77. * Reads a URI synchronously.
  78. * This function is intentionally undocumented to favorites the `readURI` usage.
  79. *
  80. * @param uri {string} The URI to read
  81. * @param [charset] {string} The character set to use when read the content of
  82. * the `uri` given. By default is set to 'UTF-8'.
  83. *
  84. * @returns {string} The content of the URI given.
  85. *
  86. * @example
  87. * let data = readURISync('resource://gre/modules/NetUtil.jsm');
  88. */
  89. function readURISync(uri, charset) {
  90. charset = typeof charset === "string" ? charset : "UTF-8";
  91. let channel = NetUtil.newChannel(uri, charset, null);
  92. let stream = channel.open();
  93. let count = stream.available();
  94. let data = NetUtil.readInputStreamToString(stream, count, { charset : charset });
  95. stream.close();
  96. return data;
  97. }
  98. exports.readURISync = readURISync;