Last active
November 15, 2016 03:38
-
-
Save wilsoncook/5cfd535b377d7ed4d252643ef8902c1d to your computer and use it in GitHub Desktop.
模拟HTTP响应中,动态生成的内容流,然后经过gzip+chunked返回(测试学习用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var zlib = require('zlib'); | |
var Readable = require('stream').Readable; | |
var Transform = require('stream').Transform; | |
var count = 0, data = ['t007', '是', '个', 'idiot!']; | |
function MyReader (opts) { | |
Readable.call(this, opts); | |
}; | |
MyReader.prototype = Object.create(Readable.prototype); | |
MyReader.prototype._read = function (size) { | |
console.log('---_read', count); | |
if (count >= data.length) { | |
this.push(null); | |
} else { | |
/* | |
var curData = data[count++], | |
str = curData.length + '\r\n' + curData + '\r\n', | |
buf = new Buffer(str, 'ascii'); | |
this.push(buf); | |
*/ | |
//this.push(new Buffer(data[count++], 'utf-8')); | |
this.push(data[count++]); | |
} | |
}; | |
function MyChunker (opts) { | |
Transform.call(this, opts); | |
} | |
MyChunker.prototype = Object.create(Transform.prototype); | |
MyChunker.prototype._transform = function (chunk, encoding, callback) { | |
console.log('----_transform', chunk, encoding); | |
//this.push(new Buffer(chunk.length.toString(16) + '\r\n', 'ascii')); | |
this.push(chunk.length.toString(16) + '\r\n'); //注意是16进制 | |
this.push(chunk); | |
//this.push(new Buffer('\r\n', 'ascii')); | |
this.push('\r\n'); | |
callback(); | |
}; | |
MyChunker.prototype._flush = function (callback) { | |
console.log('----_flush'); | |
//this.push(new Buffer('0\r\n\r\n', 'ascii')); | |
this.push('0\r\n\r\n'); | |
callback(); | |
}; | |
require('net').createServer(function (socket) { | |
socket.on('data', function (data) { | |
console.log('-----IN COME DATA: ', data.toString()); | |
socket.write('HTTP/1.1 200 OK\r\n'); | |
socket.write('Content-Type: text/plain; charset=utf-8\r\n'); | |
socket.write('Transfer-Encoding: chunked\r\n'); | |
socket.write('Content-Encoding: gzip\r\n'); | |
socket.write('\r\n'); | |
var r = new MyReader(); | |
var chunker = new MyChunker(); | |
r.pipe(zlib.createGzip()).pipe(chunker).pipe(socket); | |
//r.pipe(zlib.createGzip()).pipe(socket); | |
/* | |
setTimeout(function () { | |
console.log('----传输1分块'); | |
socket.write('5\r\n'); | |
socket.write('t007 \r\n'); | |
setTimeout(function () { | |
console.log('----传输1分块'); | |
socket.write('3\r\n'); | |
socket.write('is \r\n'); | |
setTimeout(function () { | |
console.log('----传输1分块'); | |
socket.write('8\r\n'); | |
socket.write(' a idiot\r\n'); | |
setTimeout(function () { | |
console.log('----最后一块'); | |
socket.write('0\r\n'); | |
socket.write('\r\n'); | |
}, 3000); | |
}, 1000); | |
}, 1500); | |
}, 1000); | |
*/ | |
}); | |
}).listen(9090); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[注意]chunked分块的大小是用16进制表示的