Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
fetch
提交
1b84d7b2
F
fetch
项目概览
github
/
fetch
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
1b84d7b2
编写于
1月 12, 2015
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'blob-binary-data' of
https://github.com/lrowe/fetch
into lrowe-blob-binary-data
上级
91ae44c1
74fa62be
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
110 addition
and
33 deletion
+110
-33
fetch.js
fetch.js
+59
-33
test/server.js
test/server.js
+8
-0
test/test.js
test/test.js
+43
-0
未找到文件。
fetch.js
浏览文件 @
1b84d7b2
...
...
@@ -69,56 +69,79 @@
body
.
bodyUsed
=
true
}
var
blobSupport
=
self
.
FileReader
&&
(
function
()
{
try
{
new
Blob
();
return
true
}
catch
(
e
)
{
return
false
}
})();
function
Body
()
{
this
.
_body
=
null
this
.
bodyUsed
=
false
this
.
arrayBuffer
=
function
()
{
throw
new
Error
(
'
Not implemented yet
'
)
}
if
(
blobSupport
)
{
this
.
arrayBuffer
=
function
()
{
var
rejected
=
consumed
(
this
)
if
(
rejected
)
{
return
rejected
}
var
blobSupport
=
(
function
()
{
try
{
new
Blob
();
return
true
}
catch
(
e
)
{
return
false
var
body
=
this
.
_body
return
new
Promise
(
function
(
resolve
,
reject
)
{
var
fileReader
=
new
FileReader
()
fileReader
.
readAsArrayBuffer
(
body
)
fileReader
.
onloadend
=
function
(
e
)
{
if
(
e
.
target
.
error
)
{
reject
(
e
.
target
.
error
)
}
else
{
resolve
(
e
.
target
.
result
)
}
}
})
}
})();
if
(
blobSupport
)
{
this
.
blob
=
function
()
{
var
rejected
=
consumed
(
this
)
return
rejected
?
rejected
:
Promise
.
resolve
(
new
Blob
([
this
.
_body
])
)
return
rejected
?
rejected
:
Promise
.
resolve
(
this
.
_body
)
}
}
if
(
self
.
FormData
)
{
this
.
formData
=
function
()
{
this
.
text
=
function
()
{
var
rejected
=
consumed
(
this
)
if
(
rejected
)
{
return
rejected
}
var
body
=
this
.
_body
return
new
Promise
(
function
(
resolve
,
reject
)
{
var
fileReader
=
new
FileReader
()
fileReader
.
readAsText
(
body
)
fileReader
.
onloadend
=
function
(
e
)
{
if
(
e
.
target
.
error
)
{
reject
(
e
.
target
.
error
)
}
else
{
resolve
(
e
.
target
.
result
)
}
}
})
}
}
else
{
this
.
text
=
function
()
{
var
rejected
=
consumed
(
this
)
return
rejected
?
rejected
:
Promise
.
resolve
(
decode
(
this
.
_body
)
)
return
rejected
?
rejected
:
Promise
.
resolve
(
this
.
_body
)
}
}
this
.
json
=
function
()
{
var
rejected
=
consumed
(
this
)
if
(
rejected
)
{
return
rejected
if
(
self
.
FormData
)
{
this
.
formData
=
function
()
{
return
this
.
text
().
then
(
function
(
text
)
{
return
decode
(
text
)
})
}
var
body
=
this
.
_body
return
new
Promise
(
function
(
resolve
,
reject
)
{
try
{
resolve
(
JSON
.
parse
(
body
))
}
catch
(
ex
)
{
reject
(
ex
)
}
})
}
this
.
text
=
function
()
{
var
rejected
=
consumed
(
this
)
return
rejected
?
rejected
:
Promise
.
resolve
(
this
.
_body
)
this
.
json
=
function
()
{
return
this
.
text
().
then
(
function
(
text
)
{
return
JSON
.
parse
(
text
)
})
}
return
this
...
...
@@ -186,7 +209,7 @@
headers
:
headers
(
xhr
),
url
:
xhr
.
responseURL
||
xhr
.
getResponseHeader
(
'
X-Request-URL
'
)
}
resolve
(
new
Response
(
xhr
.
responseText
,
options
))
resolve
(
new
Response
(
blobSupport
?
xhr
.
response
:
xhr
.
responseText
,
options
))
}
xhr
.
onerror
=
function
()
{
...
...
@@ -194,6 +217,9 @@
}
xhr
.
open
(
self
.
method
,
self
.
url
)
if
(
blobSupport
)
{
xhr
.
responseType
=
'
blob
'
}
self
.
headers
.
forEach
(
function
(
name
,
values
)
{
values
.
forEach
(
function
(
value
)
{
...
...
test/server.js
浏览文件 @
1b84d7b2
...
...
@@ -28,6 +28,14 @@ var routes = {
});
res
.
end
(
'
hi
'
);
},
'
/nonascii
'
:
function
(
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain
'
});
var
buf
=
new
Buffer
(
256
);
for
(
var
i
=
0
;
i
<
256
;
i
++
)
{
buf
[
i
]
=
i
;
}
res
.
end
(
buf
);
},
'
/redirect/301
'
:
function
(
res
)
{
res
.
writeHead
(
301
,
{
'
Location
'
:
'
/hello
'
});
res
.
end
();
...
...
test/test.js
浏览文件 @
1b84d7b2
...
...
@@ -63,6 +63,40 @@ suite('Response', function() {
// https://fetch.spec.whatwg.org/#body-mixin
suite
(
'
Body mixin
'
,
function
()
{
;(
Response
.
prototype
.
arrayBuffer
?
suite
:
suite
.
skip
)(
'
arrayBuffer
'
,
function
()
{
test
(
'
resolves arrayBuffer promise
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
return
response
.
arrayBuffer
()
}).
then
(
function
(
buf
)
{
assert
(
buf
instanceof
ArrayBuffer
,
'
buf is an ArrayBuffer instance
'
)
assert
.
equal
(
buf
.
byteLength
,
2
)
})
})
test
(
'
arrayBuffer handles non-ascii data
'
,
function
()
{
return
fetch
(
'
/nonascii
'
).
then
(
function
(
response
)
{
return
response
.
arrayBuffer
()
}).
then
(
function
(
buf
)
{
assert
(
buf
instanceof
ArrayBuffer
,
'
buf is an ArrayBuffer instance
'
)
assert
.
equal
(
buf
.
byteLength
,
256
,
'
buf.byteLength is correct
'
)
var
view
=
new
Uint8Array
(
buf
)
for
(
var
i
=
0
;
i
<
256
;
i
++
)
{
assert
.
equal
(
view
[
i
],
i
)
}
})
})
test
(
'
rejects arrayBuffer promise after body is consumed
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
assert
(
response
.
arrayBuffer
,
'
Body does not implement arrayBuffer
'
)
response
.
blob
()
return
response
.
arrayBuffer
()
}).
catch
(
function
(
error
)
{
assert
(
error
instanceof
TypeError
,
'
Promise rejected after body consumed
'
)
})
})
})
;(
Response
.
prototype
.
blob
?
suite
:
suite
.
skip
)(
'
blob
'
,
function
()
{
test
(
'
resolves blob promise
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
...
...
@@ -73,6 +107,15 @@ suite('Body mixin', function() {
})
})
test
(
'
blob handles non-ascii data
'
,
function
()
{
return
fetch
(
'
/nonascii
'
).
then
(
function
(
response
)
{
return
response
.
blob
()
}).
then
(
function
(
blob
)
{
assert
(
blob
instanceof
Blob
,
'
blob is a Blob instance
'
)
assert
.
equal
(
blob
.
size
,
256
,
'
blob.size is correct
'
)
})
})
test
(
'
rejects blob promise after body is consumed
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
assert
(
response
.
blob
,
'
Body does not implement blob
'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录