pull-xhr v1.0.6
pull-xhr
window.XMLHttpRequest
as a (pseudo) pull-stream
npm install --save pull-xhr
only supports modern browsers and IE 10+
api
Xhr = require('pull-xhr')
request
(req
) is an object with:
url
: default''
- string to remote location to openmethod
: default'GET'
- string of http method to openresponseType
: default'text'
- string of response typeheaders
: default{}
- object to set request header names to valuesbody
: defaultnull
- object to send as requestjson
: object to be stringified as json request bodybeforeOpen
: function(xhr) => {}
called before openbeforeSend
: function(xhr) => {}
called before send
response
(res
) is an object with:
headers
: object from received response header names to valuesbody
: object from response bodystatusCode
: number from response status codestatusMessage
: string from response statusurl
: string to remote location to openmethod
: string of http method to openxhr
: raw XMLHttpRequest instance
Xhr.async(req, cb(err, res.body, res))
don't use streams at all. just ask a question and get an answer.
Xhr.source(req, cb (err, res)) => source
use for downloads. the source is the res
if req.responseType === 'json'
the source will be parsed from double newline delimited json.
Xhr.sink(req, cb(err, res.body, res)) => sink
use for uploads. the sink is the req.
if the first chunk in the source to the sink is not a Buffer, the source will be stringified to double newline delimited json.
then everything is concat and sent as an ArrayBuffer.
license
The Apache License
Copyright © 2016 Michael Williams
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.