| 1 |
// Copyright 2015 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.net.FetchXmlHttp'); |
| 16 |
goog.provide('goog.net.FetchXmlHttpFactory'); |
| 17 |
|
| 18 |
goog.require('goog.asserts'); |
| 19 |
goog.require('goog.events.EventTarget'); |
| 20 |
goog.require('goog.functions'); |
| 21 |
goog.require('goog.log'); |
| 22 |
goog.require('goog.net.XhrLike'); |
| 23 |
goog.require('goog.net.XmlHttpFactory'); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Factory for creating Xhr objects that uses the native fetch() method. |
| 29 |
* https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API |
| 30 |
* Note that this factory is intended for use in Service Worker only. |
| 31 |
* @param {!WorkerGlobalScope} worker The Service Worker global scope. |
| 32 |
* @extends {goog.net.XmlHttpFactory} |
| 33 |
* @struct |
| 34 |
* @constructor |
| 35 |
*/ |
| 36 |
goog.net.FetchXmlHttpFactory = function(worker) { |
| 37 |
goog.net.FetchXmlHttpFactory.base(this, 'constructor'); |
| 38 |
|
| 39 |
/** @private @final {!WorkerGlobalScope} */ |
| 40 |
this.worker_ = worker; |
| 41 |
|
| 42 |
/** @private {!RequestCredentials|undefined} */ |
| 43 |
this.credentialsMode_ = undefined; |
| 44 |
|
| 45 |
/** @private {!RequestCache|undefined} */ |
| 46 |
this.cacheMode_ = undefined; |
| 47 |
}; |
| 48 |
goog.inherits(goog.net.FetchXmlHttpFactory, goog.net.XmlHttpFactory); |
| 49 |
|
| 50 |
|
| 51 |
/** @override */ |
| 52 |
goog.net.FetchXmlHttpFactory.prototype.createInstance = function() { |
| 53 |
var instance = new goog.net.FetchXmlHttp(this.worker_); |
| 54 |
if (this.credentialsMode_) { |
| 55 |
instance.setCredentialsMode(this.credentialsMode_); |
| 56 |
} |
| 57 |
if (this.cacheMode_) { |
| 58 |
instance.setCacheMode(this.cacheMode_); |
| 59 |
} |
| 60 |
return instance; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
/** @override */ |
| 65 |
goog.net.FetchXmlHttpFactory.prototype.internalGetOptions = |
| 66 |
goog.functions.constant({}); |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* @param {!RequestCredentials} credentialsMode The credentials mode of the |
| 71 |
* Service Worker fetch. |
| 72 |
*/ |
| 73 |
goog.net.FetchXmlHttpFactory.prototype.setCredentialsMode = function( |
| 74 |
credentialsMode) { |
| 75 |
this.credentialsMode_ = credentialsMode; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* @param {!RequestCache} cacheMode The cache mode of the Service Worker fetch. |
| 81 |
*/ |
| 82 |
goog.net.FetchXmlHttpFactory.prototype.setCacheMode = function(cacheMode) { |
| 83 |
this.cacheMode_ = cacheMode; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* FetchXmlHttp object constructor. |
| 90 |
* @param {!WorkerGlobalScope} worker |
| 91 |
* @extends {goog.events.EventTarget} |
| 92 |
* @implements {goog.net.XhrLike} |
| 93 |
* @constructor |
| 94 |
* @struct |
| 95 |
*/ |
| 96 |
goog.net.FetchXmlHttp = function(worker) { |
| 97 |
goog.net.FetchXmlHttp.base(this, 'constructor'); |
| 98 |
|
| 99 |
/** @private @final {!WorkerGlobalScope} */ |
| 100 |
this.worker_ = worker; |
| 101 |
|
| 102 |
/** @private {RequestCredentials|undefined} */ |
| 103 |
this.credentialsMode_ = undefined; |
| 104 |
|
| 105 |
/** @private {RequestCache|undefined} */ |
| 106 |
this.cacheMode_ = undefined; |
| 107 |
|
| 108 |
/** |
| 109 |
* Request state. |
| 110 |
* @type {goog.net.FetchXmlHttp.RequestState} |
| 111 |
*/ |
| 112 |
this.readyState = goog.net.FetchXmlHttp.RequestState.UNSENT; |
| 113 |
|
| 114 |
/** |
| 115 |
* HTTP status. |
| 116 |
* @type {number} |
| 117 |
*/ |
| 118 |
this.status = 0; |
| 119 |
|
| 120 |
/** |
| 121 |
* HTTP status string. |
| 122 |
* @type {string} |
| 123 |
*/ |
| 124 |
this.statusText = ''; |
| 125 |
|
| 126 |
/** |
| 127 |
* Content of the response. |
| 128 |
* @type {string} |
| 129 |
*/ |
| 130 |
this.responseText = ''; |
| 131 |
|
| 132 |
/** |
| 133 |
* Document response entity body. |
| 134 |
* NOTE: This is always null and not supported by this class. |
| 135 |
* @final {null} |
| 136 |
*/ |
| 137 |
this.responseXML = null; |
| 138 |
|
| 139 |
/** |
| 140 |
* Method to call when the state changes. |
| 141 |
* @type {?function()} |
| 142 |
*/ |
| 143 |
this.onreadystatechange = null; |
| 144 |
|
| 145 |
/** @private {!Headers} */ |
| 146 |
this.requestHeaders_ = new Headers(); |
| 147 |
|
| 148 |
/** @private {?Headers} */ |
| 149 |
this.responseHeaders_ = null; |
| 150 |
|
| 151 |
/** |
| 152 |
* Request method (GET or POST). |
| 153 |
* @private {string} |
| 154 |
*/ |
| 155 |
this.method_ = 'GET'; |
| 156 |
|
| 157 |
/** |
| 158 |
* Request URL. |
| 159 |
* @private {string} |
| 160 |
*/ |
| 161 |
this.url_ = ''; |
| 162 |
|
| 163 |
/** |
| 164 |
* Whether the request is in progress. |
| 165 |
* @private {boolean} |
| 166 |
*/ |
| 167 |
this.inProgress_ = false; |
| 168 |
|
| 169 |
/** @private @final {?goog.log.Logger} */ |
| 170 |
this.logger_ = goog.log.getLogger('goog.net.FetchXmlHttp'); |
| 171 |
|
| 172 |
}; |
| 173 |
goog.inherits(goog.net.FetchXmlHttp, goog.events.EventTarget); |
| 174 |
|
| 175 |
|
| 176 |
/** |
| 177 |
* State of the requests. |
| 178 |
* @enum {number} |
| 179 |
*/ |
| 180 |
goog.net.FetchXmlHttp.RequestState = { |
| 181 |
UNSENT: 0, |
| 182 |
OPENED: 1, |
| 183 |
HEADER_RECEIVED: 2, |
| 184 |
LOADING: 3, |
| 185 |
DONE: 4 |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
/** @override */ |
| 190 |
goog.net.FetchXmlHttp.prototype.open = function(method, url, opt_async) { |
| 191 |
goog.asserts.assert(!!opt_async, 'Only async requests are supported.'); |
| 192 |
if (this.readyState != goog.net.FetchXmlHttp.RequestState.UNSENT) { |
| 193 |
this.abort(); |
| 194 |
throw new Error('Error reopening a connection'); |
| 195 |
} |
| 196 |
|
| 197 |
this.method_ = method; |
| 198 |
this.url_ = url; |
| 199 |
|
| 200 |
this.readyState = goog.net.FetchXmlHttp.RequestState.OPENED; |
| 201 |
this.dispatchCallback_(); |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
/** @override */ |
| 206 |
goog.net.FetchXmlHttp.prototype.send = function(opt_data) { |
| 207 |
if (this.readyState != goog.net.FetchXmlHttp.RequestState.OPENED) { |
| 208 |
this.abort(); |
| 209 |
throw new Error('need to call open() first. '); |
| 210 |
} |
| 211 |
|
| 212 |
this.inProgress_ = true; |
| 213 |
var requestInit = { |
| 214 |
headers: this.requestHeaders_, |
| 215 |
method: this.method_, |
| 216 |
credentials: this.credentialsMode_, |
| 217 |
cache: this.cacheMode_ |
| 218 |
}; |
| 219 |
if (opt_data) { |
| 220 |
requestInit['body'] = opt_data; |
| 221 |
} |
| 222 |
this.worker_ |
| 223 |
.fetch(new Request(this.url_, /** @type {!RequestInit} */ (requestInit))) |
| 224 |
.then( |
| 225 |
this.handleResponse_.bind(this), this.handleSendFailure_.bind(this)); |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
/** @override */ |
| 230 |
goog.net.FetchXmlHttp.prototype.abort = function() { |
| 231 |
this.responseText = ''; |
| 232 |
this.requestHeaders_ = new Headers(); |
| 233 |
this.status = 0; |
| 234 |
if (((this.readyState >= goog.net.FetchXmlHttp.RequestState.OPENED) && |
| 235 |
this.inProgress_) && |
| 236 |
(this.readyState != goog.net.FetchXmlHttp.RequestState.DONE)) { |
| 237 |
this.readyState = goog.net.FetchXmlHttp.RequestState.DONE; |
| 238 |
this.inProgress_ = false; |
| 239 |
this.dispatchCallback_(); |
| 240 |
} |
| 241 |
|
| 242 |
this.readyState = goog.net.FetchXmlHttp.RequestState.UNSENT; |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* Handles the fetch response. |
| 248 |
* @param {!Response} response |
| 249 |
* @private |
| 250 |
*/ |
| 251 |
goog.net.FetchXmlHttp.prototype.handleResponse_ = function(response) { |
| 252 |
if (!this.inProgress_) { |
| 253 |
// The request was aborted, ignore. |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
if (!this.responseHeaders_) { |
| 258 |
this.responseHeaders_ = response.headers; |
| 259 |
this.readyState = goog.net.FetchXmlHttp.RequestState.HEADER_RECEIVED; |
| 260 |
this.dispatchCallback_(); |
| 261 |
} |
| 262 |
// A callback may abort the request. |
| 263 |
if (!this.inProgress_) { |
| 264 |
// The request was aborted, ignore. |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
this.readyState = goog.net.FetchXmlHttp.RequestState.LOADING; |
| 269 |
this.dispatchCallback_(); |
| 270 |
// A callback may abort the request. |
| 271 |
if (!this.inProgress_) { |
| 272 |
// The request was aborted, ignore. |
| 273 |
return; |
| 274 |
} |
| 275 |
response.text().then( |
| 276 |
this.handleResponseText_.bind(this, response), |
| 277 |
this.handleSendFailure_.bind(this)); |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
/** |
| 282 |
* Handles the response text. |
| 283 |
* @param {!Response} response |
| 284 |
* @param {string} responseText |
| 285 |
* @private |
| 286 |
*/ |
| 287 |
goog.net.FetchXmlHttp.prototype.handleResponseText_ = function( |
| 288 |
response, responseText) { |
| 289 |
if (!this.inProgress_) { |
| 290 |
// The request was aborted, ignore. |
| 291 |
return; |
| 292 |
} |
| 293 |
this.status = response.status; |
| 294 |
this.statusText = response.statusText; |
| 295 |
this.responseText = responseText; |
| 296 |
this.readyState = goog.net.FetchXmlHttp.RequestState.DONE; |
| 297 |
this.dispatchCallback_(); |
| 298 |
}; |
| 299 |
|
| 300 |
|
| 301 |
/** |
| 302 |
* Handles the send failure. |
| 303 |
* @param {*} error |
| 304 |
* @private |
| 305 |
*/ |
| 306 |
goog.net.FetchXmlHttp.prototype.handleSendFailure_ = function(error) { |
| 307 |
var e = error instanceof Error ? error : Error(error); |
| 308 |
goog.log.warning(this.logger_, 'Failed to fetch url ' + this.url_, e); |
| 309 |
if (!this.inProgress_) { |
| 310 |
// The request was aborted, ignore. |
| 311 |
return; |
| 312 |
} |
| 313 |
this.readyState = goog.net.FetchXmlHttp.RequestState.DONE; |
| 314 |
this.dispatchCallback_(); |
| 315 |
}; |
| 316 |
|
| 317 |
|
| 318 |
/** @override */ |
| 319 |
goog.net.FetchXmlHttp.prototype.setRequestHeader = function(header, value) { |
| 320 |
this.requestHeaders_.append(header, value); |
| 321 |
}; |
| 322 |
|
| 323 |
|
| 324 |
/** @override */ |
| 325 |
goog.net.FetchXmlHttp.prototype.getResponseHeader = function(header) { |
| 326 |
return this.responseHeaders_.get(header.toLowerCase()) || ''; |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
/** @override */ |
| 331 |
goog.net.FetchXmlHttp.prototype.getAllResponseHeaders = function() { |
| 332 |
var lines = []; |
| 333 |
var iter = this.responseHeaders_.entries(); |
| 334 |
var entry = iter.next(); |
| 335 |
while (!entry.done) { |
| 336 |
var pair = entry.value; |
| 337 |
lines.push(pair[0] + ': ' + pair[1]); |
| 338 |
entry = iter.next(); |
| 339 |
} |
| 340 |
return lines.join('\r\n'); |
| 341 |
}; |
| 342 |
|
| 343 |
|
| 344 |
/** |
| 345 |
* @param {!RequestCredentials} credentialsMode The credentials mode of the |
| 346 |
* Service Worker fetch. |
| 347 |
*/ |
| 348 |
goog.net.FetchXmlHttp.prototype.setCredentialsMode = function(credentialsMode) { |
| 349 |
this.credentialsMode_ = credentialsMode; |
| 350 |
}; |
| 351 |
|
| 352 |
|
| 353 |
/** |
| 354 |
* @param {!RequestCache} cacheMode The cache mode of the Service Worker fetch. |
| 355 |
*/ |
| 356 |
goog.net.FetchXmlHttp.prototype.setCacheMode = function(cacheMode) { |
| 357 |
this.cacheMode_ = cacheMode; |
| 358 |
}; |
| 359 |
|
| 360 |
|
| 361 |
/** |
| 362 |
* Dispatches the callback, if the callback attribute is defined. |
| 363 |
* @private |
| 364 |
*/ |
| 365 |
goog.net.FetchXmlHttp.prototype.dispatchCallback_ = function() { |
| 366 |
if (this.onreadystatechange) { |
| 367 |
this.onreadystatechange.call(this); |
| 368 |
} |
| 369 |
}; |