Skip to content

Commit

Permalink
Merge pull request #428 from mislav/spec-compat
Browse files Browse the repository at this point in the history
Spec compatibility for Request/Response constructors and cloning
  • Loading branch information
mislav authored Nov 11, 2016
2 parents 67627a4 + 9edd55d commit 87fd1cd
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 163 deletions.
57 changes: 20 additions & 37 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@
function Request(input, options) {
options = options || {}
var body = options.body
if (Request.prototype.isPrototypeOf(input)) {

if (typeof input === 'string') {
this.url = input
} else {
if (input.bodyUsed) {
throw new TypeError('Already read')
}
Expand All @@ -271,12 +274,10 @@
}
this.method = input.method
this.mode = input.mode
if (!body) {
if (!body && input._bodyInit != null) {
body = input._bodyInit
input.bodyUsed = true
}
} else {
this.url = input
}

this.credentials = options.credentials || this.credentials || 'omit'
Expand All @@ -294,7 +295,7 @@
}

Request.prototype.clone = function() {
return new Request(this)
return new Request(this, { body: this._bodyInit })
}

function decode(body) {
Expand All @@ -310,16 +311,17 @@
return form
}

function headers(xhr) {
var head = new Headers()
var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n')
pairs.forEach(function(header) {
var split = header.trim().split(':')
var key = split.shift().trim()
var value = split.join(':').trim()
head.append(key, value)
function parseHeaders(rawHeaders) {
var headers = new Headers()
rawHeaders.split('\r\n').forEach(function(line) {
var parts = line.split(':')
var key = parts.shift().trim()
if (key) {
var value = parts.join(':').trim()
headers.append(key, value)
}
})
return head
return headers
}

Body.call(Request.prototype)
Expand All @@ -333,7 +335,7 @@
this.status = options.status
this.ok = this.status >= 200 && this.status < 300
this.statusText = options.statusText
this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
this.headers = new Headers(options.headers)
this.url = options.url || ''
this._initBody(bodyInit)
}
Expand Down Expand Up @@ -371,35 +373,16 @@

self.fetch = function(input, init) {
return new Promise(function(resolve, reject) {
var request
if (Request.prototype.isPrototypeOf(input) && !init) {
request = input
} else {
request = new Request(input, init)
}

var request = new Request(input, init)
var xhr = new XMLHttpRequest()

function responseURL() {
if ('responseURL' in xhr) {
return xhr.responseURL
}

// Avoid security warnings on getResponseHeader when not allowed by CORS
if (/^X-Request-URL:/mi.test(xhr.getAllResponseHeaders())) {
return xhr.getResponseHeader('X-Request-URL')
}

return
}

xhr.onload = function() {
var options = {
status: xhr.status,
statusText: xhr.statusText,
headers: headers(xhr),
url: responseURL()
headers: parseHeaders(xhr.getAllResponseHeaders() || '')
}
options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL')
var body = 'response' in xhr ? xhr.response : xhr.responseText
resolve(new Response(body, options))
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"jshint": "2.8.0",
"mocha": "2.1.0",
"mocha-phantomjs-core": "2.0.1",
"url-search-params": "0.5.0"
"url-search-params": "0.6.1"
},
"files": [
"LICENSE",
Expand Down
Loading

0 comments on commit 87fd1cd

Please sign in to comment.