Skip to content

Commit

Permalink
merge from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
undoZen committed May 11, 2015
2 parents a45f039 + 09c316d commit 5867d3e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 89 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fetch-polyfill",
"version": "0.7.5",
"version": "0.8.1",
"main": "fetch.js",
"devDependencies": {
"es6-promise": "2.1.0"
Expand All @@ -11,6 +11,7 @@
"examples/",
"Makefile",
"package.json",
"script/",
"test/"
]
}
117 changes: 53 additions & 64 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,23 @@
function Body() {
this.bodyUsed = false

if (support.blob) {
this._initBody = function(body) {
this._bodyInit = body
if (typeof body === 'string') {
this._bodyText = body
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
this._bodyBlob = body
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
this._bodyFormData = body
} else if (!body) {
this._bodyText = ''
} else {
throw new Error('unsupported BodyInit type')
}

this._initBody = function(body) {
this._bodyInit = body
if (typeof body === 'string') {
this._bodyText = body
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
this._bodyBlob = body
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
this._bodyFormData = body
} else if (!body) {
this._bodyText = ''
} else {
throw new Error('unsupported BodyInit type')
}
}

if (support.blob) {
this.blob = function() {
var rejected = consumed(this)
if (rejected) {
Expand Down Expand Up @@ -175,19 +176,6 @@
}
}
} else {
this._initBody = function(body) {
this._bodyInit = body
if (typeof body === 'string') {
this._bodyText = body
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
this._bodyFormData = body
} else if (!body) {
this._bodyText = ''
} else {
throw new Error('unsupported BodyInit type')
}
}

this.text = function() {
var rejected = consumed(this)
return rejected ? rejected : fetch.Promise.resolve(this._bodyText)
Expand Down Expand Up @@ -262,7 +250,7 @@
typeof window !== 'undefined' && !!window.ActiveXObject &&
!(window.XMLHttpRequest && (new XMLHttpRequest).dispatchEvent);

Request.prototype.getXhr = function () {
function getXhr() {
// from backbone.js 1.1.2
// https://github.com/jashkenas/backbone/blob/1.1.2/backbone.js#L1181
if (noXhrPatch && !(/^(get|post|head|put|delete|options)$/i.test(this.method))) {
Expand All @@ -272,12 +260,41 @@
return new XMLHttpRequest();
}

Request.prototype.fetch = function() {
var self = this
Body.call(Request.prototype)

function Response(bodyInit, options) {
if (!options) {
options = {}
}

this._initBody(bodyInit)
this.type = 'default'
this.url = null
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.url = options.url || ''
}

Body.call(Response.prototype)

self.Headers = Headers;
self.Request = Request;
self.Response = Response;

self.fetch = function(input, init) {
// TODO: Request constructor should accept input, init
var request
if (Request.prototype.isPrototypeOf(input) && !init) {
request = input
} else {
request = new Request(input, init)
}

return new fetch.Promise(function(resolve, reject) {
var xhr = self.getXhr();
if (self.credentials === 'cors') {
var xhr = getXhr();
if (request.credentials === 'cors') {
xhr.withCredentials = true;
}

Expand Down Expand Up @@ -320,49 +337,21 @@
}
}

xhr.open(self.method, self.url, true)
xhr.open(request.method, request.url, true)

if ('responseType' in xhr && support.blob) {
xhr.responseType = 'blob'
}

self.headers.forEach(function(name, values) {
request.headers.forEach(function(name, values) {
values.forEach(function(value) {
xhr.setRequestHeader(name, value)
})
})

xhr.send(typeof self._bodyInit === 'undefined' ? null : self._bodyInit)
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
})
}

Body.call(Request.prototype)

function Response(bodyInit, options) {
if (!options) {
options = {}
}

this._initBody(bodyInit)
this.type = 'default'
this.url = null
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.url = options.url || ''
}

Body.call(Response.prototype)

self.Headers = Headers;
self.Request = Request;
self.Response = Response;

self.fetch = fetch;
function fetch(url, options) {
return new Request(url, options).fetch()
}
fetch.Promise = self.Promise; // you could change it to your favorite alternative
fetch.polyfill = true;
self.fetch.polyfill = true
})();
2 changes: 1 addition & 1 deletion script/phantomjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ port=3900
while lsof -i :$((++port)) >/dev/null; do true; done

# Spin a test server in the background
node ./test/server.js $port &>/dev/null &
node ./script/server $port &>/dev/null &
server_pid=$!
trap "kill $server_pid" INT EXIT

Expand Down
2 changes: 1 addition & 1 deletion script/saucelabs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
port=8080

# Spin a test server in the background
node ./test/server.js $port &>/dev/null &
node ./script/server $port &>/dev/null &
server_pid=$!
trap "kill $server_pid" INT EXIT

Expand Down
2 changes: 1 addition & 1 deletion test/server.js → script/server
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var routes = {
res.destroy();
},
'/form': function(res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.writeHead(200, {'Content-Type': 'application/x-www-form-urlencoded'});
res.end('number=1&space=one+two&empty=&encoded=a%2Bb&');
},
'/json': function(res) {
Expand Down
70 changes: 49 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
function readBlobAsText(blob) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
resolve(reader.result)
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsText(blob)
})
if ('FileReader' in self) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
resolve(reader.result)
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsText(blob)
})
} else if ('FileReaderSync' in self) {
return new FileReaderSync().readAsText(blob)
} else {
throw new ReferenceError('FileReader is not defined')
}
}

function readBlobAsBytes(blob) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
var view = new Uint8Array(reader.result)
resolve(Array.prototype.slice.call(view))
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsArrayBuffer(blob)
})
if ('FileReader' in self) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
var view = new Uint8Array(reader.result)
resolve(Array.prototype.slice.call(view))
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsArrayBuffer(blob)
})
} else if ('FileReaderSync' in self) {
return new FileReaderSync().readAsArrayBuffer(blob)
} else {
throw new ReferenceError('FileReader is not defined')
}
}

test('resolves promise on 500 error', function() {
Expand Down Expand Up @@ -134,6 +146,22 @@ suite('Request', function() {
})
})

test('fetch request', function() {
var request = new Request('/request', {
headers: {
'Accept': 'application/json',
'X-Test': '42'
}
})

return fetch(request).then(function(response) {
return response.json()
}).then(function(json) {
assert.equal(json.headers['accept'], 'application/json')
assert.equal(json.headers['x-test'], '42')
})
})

test('construct with url', function() {
var request = new Request('https://fetch.spec.whatwg.org/')
assert.equal(request.url, 'https://fetch.spec.whatwg.org/')
Expand Down

0 comments on commit 5867d3e

Please sign in to comment.