Socket
Socket
Sign inDemoInstall

buffered-xhr-stream

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

buffered-xhr-stream - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

31

index.js

@@ -14,3 +14,2 @@ var stream = require('stream')

this.offset = 0
this.paused = false
this.chunkSize = options.chunkSize || 65536

@@ -26,6 +25,9 @@ this.readable = true

this.xhr = new XMLHttpRequest
this.xhr.open('GET', options.url, true)
this.xhr.open(options.method || 'GET', options.url, true)
}
if (options.contentType) {
this.xhr.setRequestHeader('Content-type', options.contentType)
}
this.xhr.onreadystatechange = this.handle.bind(this)
this.xhr.send(null)
this.xhr.send(options.data)
}

@@ -36,3 +38,4 @@

Stream.prototype.handle = function () {
if (this.capable && this.xhr.readyState === 3) {
this.emit('readystatechange', this.xhr.readyState)
if (this.capable && this.xhr.readyState === XMLHttpRequest.LOADING) {
try {

@@ -43,5 +46,7 @@ this.write()

}
} else if (this.xhr.readyState === 4) {
if (this.xhr.error) {
this.emit('error')
} else if (this.xhr.readyState === XMLHttpRequest.DONE) {
if (this.xhr.status >= 400) {
var e = new Error(this.xhr.statusText || this.xhr.status)
e.xhr = this.xhr
this.emit('error', e)
} else {

@@ -64,3 +69,6 @@ this.downloaded = true

function flush (stream) {
if (!stream.xhr.responseText) {
// In IE9, responseText will be "unknown" (not undefined) until the response is done, and
// any attempt to access it throws this error:
// SCRIPT10: The data necessary to complete this operation is not yet available
if (typeof stream.xhr.responseText === 'unknown' || !stream.xhr.responseText) {
return

@@ -71,7 +79,10 @@ }

var chunk = stream.xhr.responseText.substr(stream.offset, stream.chunkSize)
stream.offset += chunk.length
// emit chunk LAST in case triggering the data event leads to even more
// data being flushed
stream.emit('data', chunk)
stream.offset += chunk.length
}
if (stream.offset === stream.xhr.responseText.length) {
if (stream.xhr.readyState === XMLHttpRequest.DONE &&
stream.offset === stream.xhr.responseText.length) {
stream.emit('end')

@@ -78,0 +89,0 @@ }

{
"name": "buffered-xhr-stream",
"version": "0.1.4",
"version": "0.1.5",
"description": "A pausable/resumable xhr stream",

@@ -5,0 +5,0 @@ "main": "index.js",

var http = require('http')
var server = http.createServer(function (req, res) {
if (req.url == '/404') {
res.statusCode = 404
return res.end()
}
res.end('foos and bars')
}).listen(parseInt(process.env.PORT))

@@ -43,1 +43,21 @@ var test = require('tape')

})
test('emits error on HTTP error', function (t) {
t.plan(4)
var url = resolve(location.href, '/404')
, s = new Stream({url: url})
s.on('error', function (err) {
t.ok(err)
t.ok(err.xhr)
t.equal(err.xhr.status, 404)
t.equal(err.message, 'Not Found')
t.end()
})
setTimeout(function () {
t.end()
}, 500)
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc