Added object compression example, closes #85

This commit is contained in:
Vitaly Puzrin 2016-08-24 23:41:08 +03:00
parent 653c0b00d8
commit 728979dd0f
4 changed files with 118 additions and 0 deletions

View file

@ -13,6 +13,7 @@
"**/.*",
"node_modules",
"bower_components",
"examples",
"test",
"Makefile",
"index*",

6
examples/README.md Normal file
View file

@ -0,0 +1,6 @@
Demo of sending compressed objects from browser to server.
Run `node server.js` and open `browser.html`.
__Warning!__ This is the sample only, to explain data reencoding steps. It does
not have error checks and so on. Don't copy-paste to production code "as is".

66
examples/browser.html Normal file
View file

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/pako/1.0.3/pako.min.js"></script>
<script>
'use strict';
var pako = window.pako;
// In browser
// Initial object //////////////////////////////////////////////////////////////
var obj = new Array(10);
for (var i = 0; i < obj.length; i++) {
obj[i] = { foo: 'bar', baz: 'БАТ' }
}
// Convert /////////////////////////////////////////////////////////////////////
var str = JSON.stringify(obj);
//
// Now `str` is standard utf16 (16-bit) string. But we need 8-bit data
//
// Let's reencode to utf8
// This method is dirty, but simple and ok for example.
var data = unescape(encodeURIComponent(str));
// Compress ////////////////////////////////////////////////////////////////////
//
// After compression we have binary data (typed array here).
//
// `XMLHttpRequest.send` accepts typed arrays in modern browsers.
// For ancient ones - more work needed.
//
var result = pako.deflate(data);
// Send data to server /////////////////////////////////////////////////////////
function send() {
var xhr = new XMLHttpRequest;
console.log('Sending data...');
xhr.open('POST', 'http://localhost:8000/', true);
// Modern browsers support sending typed array directly
xhr.send(result);
setTimeout(send, 2000);
}
send();
</script>
</html>
<body>
Sending objects to server, run server code to see result.
</body>

45
examples/server.js Normal file
View file

@ -0,0 +1,45 @@
'use strict';
/*eslint-disable no-console*/
const http = require('http');
const pako = require('../');
////////////////////////////////////////////////////////////////////////////////
// This is the main part of example
function processData(bin) {
// Decompress binary content or POST request
let uncompressed = pako.inflate(new Uint8Array(bin), { to: 'string' });
// Convert utf8 -> utf16
let decoded = decodeURIComponent(escape(uncompressed));
console.log(decoded);
}
////////////////////////////////////////////////////////////////////////////////
const server = http.createServer((req, res) => {
var buf = [];
req.on('data', data => buf.push(data));
req.on('end', () => {
let bin = Buffer.concat(buf);
processData(bin);
res.setHeader('Access-Control-Allow-Origin', '*');
res.end('ok');
});
req.on('error', () => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.end('error');
});
});
server.listen(8000);
console.log('Listening browser requests. Open `browser.html` to see');