Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URL-impl.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. "use strict";
  2. const usm = require("./url-state-machine");
  3. exports.implementation = class URLImpl {
  4. constructor(constructorArgs) {
  5. const url = constructorArgs[0];
  6. const base = constructorArgs[1];
  7. let parsedBase = null;
  8. if (base !== undefined) {
  9. parsedBase = usm.basicURLParse(base);
  10. if (parsedBase === "failure") {
  11. throw new TypeError("Invalid base URL");
  12. }
  13. }
  14. const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
  15. if (parsedURL === "failure") {
  16. throw new TypeError("Invalid URL");
  17. }
  18. this._url = parsedURL;
  19. // TODO: query stuff
  20. }
  21. get href() {
  22. return usm.serializeURL(this._url);
  23. }
  24. set href(v) {
  25. const parsedURL = usm.basicURLParse(v);
  26. if (parsedURL === "failure") {
  27. throw new TypeError("Invalid URL");
  28. }
  29. this._url = parsedURL;
  30. }
  31. get origin() {
  32. return usm.serializeURLOrigin(this._url);
  33. }
  34. get protocol() {
  35. return this._url.scheme + ":";
  36. }
  37. set protocol(v) {
  38. usm.basicURLParse(v + ":", { url: this._url, stateOverride: "scheme start" });
  39. }
  40. get username() {
  41. return this._url.username;
  42. }
  43. set username(v) {
  44. if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
  45. return;
  46. }
  47. usm.setTheUsername(this._url, v);
  48. }
  49. get password() {
  50. return this._url.password;
  51. }
  52. set password(v) {
  53. if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
  54. return;
  55. }
  56. usm.setThePassword(this._url, v);
  57. }
  58. get host() {
  59. const url = this._url;
  60. if (url.host === null) {
  61. return "";
  62. }
  63. if (url.port === null) {
  64. return usm.serializeHost(url.host);
  65. }
  66. return usm.serializeHost(url.host) + ":" + usm.serializeInteger(url.port);
  67. }
  68. set host(v) {
  69. if (this._url.cannotBeABaseURL) {
  70. return;
  71. }
  72. usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
  73. }
  74. get hostname() {
  75. if (this._url.host === null) {
  76. return "";
  77. }
  78. return usm.serializeHost(this._url.host);
  79. }
  80. set hostname(v) {
  81. if (this._url.cannotBeABaseURL) {
  82. return;
  83. }
  84. usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
  85. }
  86. get port() {
  87. if (this._url.port === null) {
  88. return "";
  89. }
  90. return usm.serializeInteger(this._url.port);
  91. }
  92. set port(v) {
  93. if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
  94. return;
  95. }
  96. if (v === "") {
  97. this._url.port = null;
  98. } else {
  99. usm.basicURLParse(v, { url: this._url, stateOverride: "port" });
  100. }
  101. }
  102. get pathname() {
  103. if (this._url.cannotBeABaseURL) {
  104. return this._url.path[0];
  105. }
  106. if (this._url.path.length === 0) {
  107. return "";
  108. }
  109. return "/" + this._url.path.join("/");
  110. }
  111. set pathname(v) {
  112. if (this._url.cannotBeABaseURL) {
  113. return;
  114. }
  115. this._url.path = [];
  116. usm.basicURLParse(v, { url: this._url, stateOverride: "path start" });
  117. }
  118. get search() {
  119. if (this._url.query === null || this._url.query === "") {
  120. return "";
  121. }
  122. return "?" + this._url.query;
  123. }
  124. set search(v) {
  125. // TODO: query stuff
  126. const url = this._url;
  127. if (v === "") {
  128. url.query = null;
  129. return;
  130. }
  131. const input = v[0] === "?" ? v.substring(1) : v;
  132. url.query = "";
  133. usm.basicURLParse(input, { url, stateOverride: "query" });
  134. }
  135. get hash() {
  136. if (this._url.fragment === null || this._url.fragment === "") {
  137. return "";
  138. }
  139. return "#" + this._url.fragment;
  140. }
  141. set hash(v) {
  142. if (v === "") {
  143. this._url.fragment = null;
  144. return;
  145. }
  146. const input = v[0] === "#" ? v.substring(1) : v;
  147. this._url.fragment = "";
  148. usm.basicURLParse(input, { url: this._url, stateOverride: "fragment" });
  149. }
  150. toJSON() {
  151. return this.href;
  152. }
  153. };