Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
fetch
提交
b9178556
F
fetch
项目概览
github
/
fetch
12 个月 前同步成功
通知
30
Star
25765
Fork
3216
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
fetch
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b9178556
编写于
1月 15, 2015
作者:
N
Nikhil Marathe
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of github.com:nikhilm/fetch
上级
7d6cac27
a1f7d49d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
112 addition
and
20 deletion
+112
-20
README.md
README.md
+1
-1
fetch.js
fetch.js
+17
-3
test/server.js
test/server.js
+16
-0
test/test.js
test/test.js
+78
-16
未找到文件。
README.md
浏览文件 @
b9178556
...
...
@@ -156,4 +156,4 @@ header hack.
![
Chrome
](
https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png
)
| !
[
Firefox
](
https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png
)
| !
[
IE
](
https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png
)
| !
[
Opera
](
https://raw.github.com/alrra/browser-logos/master/opera/opera_48x48.png
)
| !
[
Safari
](
https://raw.github.com/alrra/browser-logos/master/safari/safari_48x48.png
)
--- | --- | --- | --- | --- |
Latest ✔ | Latest ✔ |
10
+ ✔ | Latest ✔ | 6.1+ ✔ |
Latest ✔ | Latest ✔ |
9
+ ✔ | Latest ✔ | 6.1+ ✔ |
fetch.js
浏览文件 @
b9178556
...
...
@@ -187,6 +187,19 @@
return
new
Promise
(
function
(
resolve
,
reject
)
{
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:/m
.
test
(
xhr
.
getAllResponseHeaders
()))
{
return
xhr
.
getResponseHeader
(
'
X-Request-URL
'
)
}
return
;
}
xhr
.
onload
=
function
()
{
var
status
=
(
xhr
.
status
===
1223
)
?
204
:
xhr
.
status
if
(
status
<
100
||
status
>
599
)
{
...
...
@@ -197,9 +210,10 @@
status
:
status
,
statusText
:
xhr
.
statusText
,
headers
:
headers
(
xhr
),
url
:
xhr
.
responseURL
||
xhr
.
getResponseHeader
(
'
X-Request-URL
'
)
url
:
responseURL
(
)
}
resolve
(
new
Response
(
blobSupport
?
xhr
.
response
:
xhr
.
responseText
,
options
))
var
body
=
'
response
'
in
xhr
?
xhr
.
response
:
xhr
.
responseText
;
resolve
(
new
Response
(
body
,
options
))
}
xhr
.
onerror
=
function
()
{
...
...
@@ -207,7 +221,7 @@
}
xhr
.
open
(
self
.
method
,
self
.
url
)
if
(
blobSupport
)
{
if
(
'
responseType
'
in
xhr
&&
blobSupport
)
{
xhr
.
responseType
=
'
blob
'
}
...
...
test/server.js
浏览文件 @
b9178556
...
...
@@ -28,6 +28,22 @@ var routes = {
});
res
.
end
(
'
hi
'
);
},
'
/hello/utf8
'
:
function
(
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain; charset=utf-8
'
});
// "hello"
var
buf
=
new
Buffer
([
104
,
101
,
108
,
108
,
111
]);
res
.
end
(
buf
);
},
'
/hello/utf16le
'
:
function
(
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain; charset=utf-16le
'
});
// "hello"
var
buf
=
new
Buffer
([
104
,
0
,
101
,
0
,
108
,
0
,
108
,
0
,
111
,
0
]);
res
.
end
(
buf
);
},
'
/binary
'
:
function
(
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
application/octet-stream
'
});
var
buf
=
new
Buffer
(
256
);
...
...
test/test.js
浏览文件 @
b9178556
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
)
})
}
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
)
})
}
test
(
'
resolves promise on 500 error
'
,
function
()
{
return
fetch
(
'
/boom
'
).
then
(
function
(
response
)
{
assert
.
equal
(
response
.
status
,
500
)
...
...
@@ -44,19 +71,6 @@ suite('Request', function() {
// https://fetch.spec.whatwg.org/#response-class
suite
(
'
Response
'
,
function
()
{
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
)
})
}
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
suite
(
'
BodyInit extract
'
,
function
()
{
;(
Response
.
prototype
.
blob
?
suite
:
suite
.
skip
)(
'
type Blob
'
,
function
()
{
...
...
@@ -134,10 +148,34 @@ suite('Body mixin', function() {
})
})
test
(
'
arrayBuffer handles utf-8 data
'
,
function
()
{
return
fetch
(
'
/hello/utf8
'
).
then
(
function
(
response
)
{
return
response
.
arrayBuffer
()
}).
then
(
function
(
buf
)
{
assert
(
buf
instanceof
ArrayBuffer
,
'
buf is an ArrayBuffer instance
'
)
assert
.
equal
(
buf
.
byteLength
,
5
,
'
buf.byteLength is correct
'
)
var
octets
=
Array
.
prototype
.
slice
.
call
(
new
Uint8Array
(
buf
))
assert
.
deepEqual
(
octets
,
[
104
,
101
,
108
,
108
,
111
])
})
})
test
(
'
arrayBuffer handles utf-16le data
'
,
function
()
{
return
fetch
(
'
/hello/utf16le
'
).
then
(
function
(
response
)
{
return
response
.
arrayBuffer
()
}).
then
(
function
(
buf
)
{
assert
(
buf
instanceof
ArrayBuffer
,
'
buf is an ArrayBuffer instance
'
)
assert
.
equal
(
buf
.
byteLength
,
10
,
'
buf.byteLength is correct
'
)
var
octets
=
Array
.
prototype
.
slice
.
call
(
new
Uint8Array
(
buf
))
assert
.
deepEqual
(
octets
,
[
104
,
0
,
101
,
0
,
108
,
0
,
108
,
0
,
111
,
0
])
})
})
test
(
'
rejects arrayBuffer promise after body is consumed
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
assert
(
response
.
arrayBuffer
,
'
Body does not implement arrayBuffer
'
)
assert
.
equal
(
response
.
bodyUsed
,
false
)
response
.
blob
()
assert
.
equal
(
response
.
bodyUsed
,
true
)
return
response
.
arrayBuffer
()
}).
catch
(
function
(
error
)
{
assert
(
error
instanceof
TypeError
,
'
Promise rejected after body consumed
'
)
...
...
@@ -164,10 +202,30 @@ suite('Body mixin', function() {
})
})
test
(
'
blob handles utf-8 data
'
,
function
()
{
return
fetch
(
'
/hello/utf8
'
).
then
(
function
(
response
)
{
return
response
.
blob
()
}).
then
(
readBlobAsBytes
).
then
(
function
(
octets
)
{
assert
.
equal
(
octets
.
length
,
5
,
'
blob.size is correct
'
)
assert
.
deepEqual
(
octets
,
[
104
,
101
,
108
,
108
,
111
])
})
})
test
(
'
blob handles utf-16le data
'
,
function
()
{
return
fetch
(
'
/hello/utf16le
'
).
then
(
function
(
response
)
{
return
response
.
blob
()
}).
then
(
readBlobAsBytes
).
then
(
function
(
octets
)
{
assert
.
equal
(
octets
.
length
,
10
,
'
blob.size is correct
'
)
assert
.
deepEqual
(
octets
,
[
104
,
0
,
101
,
0
,
108
,
0
,
108
,
0
,
111
,
0
])
})
})
test
(
'
rejects blob promise after body is consumed
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
assert
(
response
.
blob
,
'
Body does not implement blob
'
)
response
.
blob
()
assert
.
equal
(
response
.
bodyUsed
,
false
)
response
.
text
()
assert
.
equal
(
response
.
bodyUsed
,
true
)
return
response
.
blob
()
}).
catch
(
function
(
error
)
{
assert
(
error
instanceof
TypeError
,
'
Promise rejected after body consumed
'
)
...
...
@@ -220,7 +278,9 @@ suite('Body mixin', function() {
test
(
'
rejects json promise after body is consumed
'
,
function
()
{
return
fetch
(
'
/json
'
).
then
(
function
(
response
)
{
assert
(
response
.
json
,
'
Body does not implement json
'
)
response
.
json
()
assert
.
equal
(
response
.
bodyUsed
,
false
)
response
.
text
()
assert
.
equal
(
response
.
bodyUsed
,
true
)
return
response
.
json
()
}).
catch
(
function
(
error
)
{
assert
(
error
instanceof
TypeError
,
'
Promise rejected after body consumed
'
)
...
...
@@ -258,7 +318,9 @@ suite('Body mixin', function() {
test
(
'
rejects text promise after body is consumed
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
assert
(
response
.
text
,
'
Body does not implement text
'
)
response
.
text
()
assert
.
equal
(
response
.
bodyUsed
,
false
)
response
.
json
()
assert
.
equal
(
response
.
bodyUsed
,
true
)
return
response
.
text
()
}).
catch
(
function
(
error
)
{
assert
(
error
instanceof
TypeError
,
'
Promise rejected after body consumed
'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录