Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
d23f9d5d
A
axios
项目概览
xjbieke
/
axios
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
axios
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d23f9d5d
编写于
4月 14, 2016
作者:
N
Nick Uraltsev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for Stream
上级
9e7b1b59
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
72 addition
and
44 deletion
+72
-44
lib/adapters/http.js
lib/adapters/http.js
+45
-36
lib/defaults.js
lib/defaults.js
+3
-6
test/unit/adapters/http.js
test/unit/adapters/http.js
+24
-2
未找到文件。
lib/adapters/http.js
浏览文件 @
d23f9d5d
...
...
@@ -10,6 +10,13 @@ var zlib = require('zlib');
var
pkg
=
require
(
'
./../../package.json
'
);
var
Buffer
=
require
(
'
buffer
'
).
Buffer
;
// Resolve or reject the Promise based on the status
function
settle
(
resolve
,
reject
,
response
)
{
(
response
.
status
>=
200
&&
response
.
status
<
300
?
resolve
:
reject
)(
response
);
}
/*eslint consistent-return:0*/
module
.
exports
=
function
httpAdapter
(
resolve
,
reject
,
config
)
{
var
data
=
config
.
data
;
...
...
@@ -24,13 +31,13 @@ module.exports = function httpAdapter(resolve, reject, config) {
headers
[
'
User-Agent
'
]
=
'
axios/
'
+
pkg
.
version
;
}
if
(
data
)
{
if
(
data
&&
!
utils
.
isStream
(
data
)
)
{
if
(
utils
.
isArrayBuffer
(
data
))
{
data
=
new
Buffer
(
new
Uint8Array
(
data
));
}
else
if
(
utils
.
isString
(
data
))
{
data
=
new
Buffer
(
data
,
'
utf-8
'
);
}
else
{
return
reject
(
new
Error
(
'
Data after transformation must be a string
or an ArrayBuffer
'
));
return
reject
(
new
Error
(
'
Data after transformation must be a string
, an ArrayBuffer, or a Stream
'
));
}
// Add Content-Length header if data exists
...
...
@@ -93,39 +100,37 @@ module.exports = function httpAdapter(resolve, reject, config) {
break
;
}
var
responseBuffer
=
[];
stream
.
on
(
'
data
'
,
function
handleStreamData
(
chunk
)
{
responseBuffer
.
push
(
chunk
);
// make sure the content length is not over the maxContentLength if specified
if
(
config
.
maxContentLength
>
-
1
&&
Buffer
.
concat
(
responseBuffer
).
length
>
config
.
maxContentLength
)
{
reject
(
new
Error
(
'
maxContentLength size of
'
+
config
.
maxContentLength
+
'
exceeded
'
));
}
});
stream
.
on
(
'
end
'
,
function
handleStreamEnd
()
{
var
d
=
Buffer
.
concat
(
responseBuffer
);
if
(
config
.
responseType
!==
'
arraybuffer
'
)
{
d
=
d
.
toString
(
'
utf8
'
);
}
var
response
=
{
data
:
transformData
(
d
,
res
.
headers
,
config
.
transformResponse
),
status
:
res
.
statusCode
,
statusText
:
res
.
statusMessage
,
headers
:
res
.
headers
,
config
:
config
,
request
:
req
};
// Resolve or reject the Promise based on the status
(
res
.
statusCode
>=
200
&&
res
.
statusCode
<
300
?
resolve
:
reject
)(
response
);
});
var
response
=
{
status
:
res
.
statusCode
,
statusText
:
res
.
statusMessage
,
headers
:
res
.
headers
,
config
:
config
,
request
:
req
};
if
(
config
.
responseType
===
'
stream
'
)
{
response
.
data
=
stream
;
settle
(
resolve
,
reject
,
response
);
}
else
{
var
responseBuffer
=
[];
stream
.
on
(
'
data
'
,
function
handleStreamData
(
chunk
)
{
responseBuffer
.
push
(
chunk
);
// make sure the content length is not over the maxContentLength if specified
if
(
config
.
maxContentLength
>
-
1
&&
Buffer
.
concat
(
responseBuffer
).
length
>
config
.
maxContentLength
)
{
reject
(
new
Error
(
'
maxContentLength size of
'
+
config
.
maxContentLength
+
'
exceeded
'
));
}
});
stream
.
on
(
'
end
'
,
function
handleStreamEnd
()
{
var
responseData
=
Buffer
.
concat
(
responseBuffer
);
if
(
config
.
responseType
!==
'
arraybuffer
'
)
{
responseData
=
responseData
.
toString
(
'
utf8
'
);
}
response
.
data
=
transformData
(
responseData
,
res
.
headers
,
config
.
transformResponse
);
settle
(
resolve
,
reject
,
response
);
});
}
});
// Handle errors
...
...
@@ -147,5 +152,9 @@ module.exports = function httpAdapter(resolve, reject, config) {
}
// Send the request
req
.
end
(
data
);
if
(
utils
.
isStream
(
data
))
{
data
.
pipe
(
req
);
}
else
{
req
.
end
(
data
);
}
};
lib/defaults.js
浏览文件 @
d23f9d5d
...
...
@@ -8,11 +8,8 @@ var DEFAULT_CONTENT_TYPE = {
};
module
.
exports
=
{
transformRequest
:
[
function
transformRequestJSON
(
data
,
headers
)
{
if
(
utils
.
isFormData
(
data
))
{
return
data
;
}
if
(
utils
.
isArrayBuffer
(
data
))
{
transformRequest
:
[
function
transformRequest
(
data
,
headers
)
{
if
(
utils
.
isFormData
(
data
)
||
utils
.
isArrayBuffer
(
data
)
||
utils
.
isStream
(
data
))
{
return
data
;
}
if
(
utils
.
isArrayBufferView
(
data
))
{
...
...
@@ -36,7 +33,7 @@ module.exports = {
return
data
;
}],
transformResponse
:
[
function
transformResponse
JSON
(
data
)
{
transformResponse
:
[
function
transformResponse
(
data
)
{
/*eslint no-param-reassign:0*/
if
(
typeof
data
===
'
string
'
)
{
data
=
data
.
replace
(
PROTECTION_PREFIX
,
''
);
...
...
test/unit/adapters/http.js
浏览文件 @
d23f9d5d
...
...
@@ -2,6 +2,7 @@ var axios = require('../../../index');
var
http
=
require
(
'
http
'
);
var
url
=
require
(
'
url
'
);
var
zlib
=
require
(
'
zlib
'
);
var
fs
=
require
(
'
fs
'
);
var
server
;
module
.
exports
=
{
...
...
@@ -127,7 +128,7 @@ module.exports = {
});
});
},
testMaxContentLength
:
function
(
test
)
{
var
str
=
Array
(
100000
).
join
(
'
ж
'
);
...
...
@@ -145,7 +146,7 @@ module.exports = {
error
=
res
;
failure
=
true
;
});
setTimeout
(
function
()
{
test
.
equal
(
success
,
false
,
'
request should not succeed
'
);
test
.
equal
(
failure
,
true
,
'
request should fail
'
);
...
...
@@ -153,5 +154,26 @@ module.exports = {
test
.
done
();
},
100
);
});
},
testStream
:
function
(
test
)
{
server
=
http
.
createServer
(
function
(
req
,
res
)
{
req
.
pipe
(
res
);
}).
listen
(
4444
,
function
()
{
axios
.
post
(
'
http://localhost:4444/
'
,
fs
.
createReadStream
(
__filename
),
{
responseType
:
'
stream
'
}).
then
(
function
(
res
)
{
var
stream
=
res
.
data
;
var
string
=
''
;
stream
.
on
(
'
data
'
,
function
(
chunk
)
{
string
+=
chunk
.
toString
(
'
utf8
'
);
});
stream
.
on
(
'
end
'
,
function
()
{
test
.
equal
(
string
,
fs
.
readFileSync
(
__filename
,
'
utf8
'
));
test
.
done
();
});
});
});
}
};
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录