koa-spdy-push
Advanced tools
Comparing version 0.2.0 to 0.3.0
86
index.js
@@ -6,2 +6,3 @@ | ||
var inspect = require('util').inspect | ||
var Promise = require('bluebird') | ||
var mime = require('mime-types') | ||
@@ -20,8 +21,5 @@ var dethroy = require('dethroy') | ||
return function push(context, options, done) { | ||
done = done || noop | ||
return function push(context, options) { | ||
// koa properties | ||
var res = context.res | ||
var socket = context.socket | ||
var onerror = context.onerror | ||
@@ -40,20 +38,31 @@ | ||
var filename = options.filename | ||
var length = contentLength() | ||
var type = headers['content-type'] | ||
if (!type) { | ||
type = mime.contentType(basename(path)) | ||
if (type) headers['content-type'] = type | ||
} | ||
// check whether to compress the stream | ||
var length = contentLength() | ||
var compress = (body || filename) | ||
var compress = (body || filename) // need some sort of body | ||
// must be above the threshold, but if we don't know the length, | ||
// i.e. a stream, just compress it | ||
&& (typeof length !== 'number' || length > threshold) | ||
// can't already set a content-encoding, | ||
// even if it's `identity` | ||
&& !headers['content-encoding'] | ||
&& filter(headers['content-type']) | ||
// must be a compressible content type | ||
&& filter(type) | ||
if (compress) { | ||
headers['content-encoding'] = 'gzip' | ||
// delete the content length as it's going to change with | ||
// compression. ideally we'll update with the compressed | ||
// content-length but whatever | ||
delete headers['content-length'] | ||
} else if (typeof length === 'number') { | ||
// set the content-length if we have it | ||
headers['content-length'] = String(length) | ||
} | ||
if (!headers['content-type']) { | ||
var type = mime.contentType(basename(path)) | ||
if (type) headers['content-type'] = type | ||
} | ||
debug('pushing %s w/ \n%s', path, inspect(headers)) | ||
@@ -66,22 +75,30 @@ | ||
stream.on('close', cleanup) | ||
socket.on('close', cleanup) | ||
// handle the deferred thunk | ||
stream.on('finish', finish) | ||
stream.on('error', finish) | ||
stream.on('close', finish) | ||
socket.on('close', finish) | ||
return new Promise(function (resolve, reject) { | ||
stream.on('finish', finish) | ||
stream.on('error', finish) | ||
stream.on('close', finish) | ||
return function (fn) { | ||
done = fn | ||
} | ||
function finish(err) { | ||
if (err = filterError(err)) reject(err) | ||
else resolve(stream) | ||
stream.removeListener('finish', finish) | ||
stream.removeListener('error', finish) | ||
stream.removeListener('close', finish) | ||
} | ||
}) | ||
function acknowledge() { | ||
cleanup() | ||
// empty stream | ||
if (!body && !filename) return stream.end() | ||
// we can just use the utility method | ||
if (typeof body === 'string' || Buffer.isBuffer(body)) { | ||
if (!compress) return stream.end(body) | ||
zlib.gzip(body, function (err, body) { | ||
// doubt this would ever happen, | ||
// but be sure to destroy the stream in case of errors | ||
if (err) { | ||
@@ -116,3 +133,2 @@ onerror(err) | ||
stream.on('finish', destroy) | ||
socket.on('close', destroy) | ||
@@ -125,13 +141,10 @@ function destroy(err) { | ||
stream.removeListener('finish', destroy) | ||
socket.removeListener('close', destroy) | ||
} | ||
} | ||
// try to get the content-length of the request | ||
function contentLength() { | ||
if (filename) { | ||
// already set | ||
if (!headers['content-length']) return false | ||
return parseInt(headers['content-length'], 10) | ||
} | ||
if (headers['content-length']) return parseInt(headers['content-length'], 10) | ||
// not worth `fs.stat()`ing for this | ||
if (filename) return false | ||
if (!body) return 0 | ||
@@ -147,16 +160,8 @@ if (typeof body === 'string') return Buffer.byteLength(body) | ||
stream.removeListener('close', cleanup) | ||
socket.removeListener('close', cleanup) | ||
} | ||
function finish(err) { | ||
done(filterError(err), stream) | ||
stream.removeListener('finish', finish) | ||
stream.removeListener('error', finish) | ||
stream.removeListener('close', finish) | ||
socket.removeListener('close', finish) | ||
} | ||
} | ||
} | ||
// we don't care about these errors | ||
// and we don't want to clog up `this.onerror` | ||
function filterError(err) { | ||
@@ -169,6 +174,5 @@ if (err == null) return | ||
} | ||
// WHY AM I GETTING THESE ERRORS? | ||
if (err.message === 'Write after end!') return | ||
return err | ||
} | ||
function noop() {} |
{ | ||
"name": "koa-spdy-push", | ||
"description": "SPDY Push helper for Koa", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": { | ||
@@ -16,2 +16,3 @@ "name": "Jonathan Ong", | ||
"mime-types": "1", | ||
"bluebird": "2", | ||
"dethroy": "1", | ||
@@ -18,0 +19,0 @@ "debug": "*", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8018
144
6
+ Addedbluebird@2
+ Addedbluebird@2.11.0(transitive)