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 open -
method
: default'GET'
- string of http method to open -
responseType
: default'text'
- string of response type -
headers
: default{}
- object to set request header names to values -
body
: defaultnull
- object to send as request -
json
: object to be stringified as json request body -
beforeOpen
: function(xhr) => {}
called before open -
beforeSend
: function(xhr) => {}
called before send
response
(res
) is an object with:
-
headers
: object from received response header names to values -
body
: object from response body -
statusCode
: number from response status code -
statusMessage
: string from response status -
url
: string to remote location to open -
method
: string of http method to open -
xhr
: 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.