mirror of
https://github.com/0x5eal/rbxts-pako.git
synced 2025-04-22 12:53:52 +01:00
Examples: send data as form
This commit is contained in:
parent
a3b3ae3b60
commit
61f59e7909
3 changed files with 106 additions and 45 deletions
|
@ -43,7 +43,14 @@ var resultAsUint8Array = pako.deflate(data);
|
||||||
var resultAsBinString = pako.deflate(data, { to: 'string' });
|
var resultAsBinString = pako.deflate(data, { to: 'string' });
|
||||||
|
|
||||||
|
|
||||||
// Send data to server /////////////////////////////////////////////////////////
|
// Send data to server
|
||||||
|
//
|
||||||
|
// Use multipart/form-data:
|
||||||
|
//
|
||||||
|
// - small overhead
|
||||||
|
// - well known format, easy to process anywhere
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function sendModern() {
|
function sendModern() {
|
||||||
var xhr = new XMLHttpRequest;
|
var xhr = new XMLHttpRequest;
|
||||||
|
@ -52,11 +59,13 @@ function sendModern() {
|
||||||
|
|
||||||
xhr.open('POST', 'http://localhost:8000/', true);
|
xhr.open('POST', 'http://localhost:8000/', true);
|
||||||
|
|
||||||
// We could make this work everywhere, if send data as base64 string.
|
var formData = new FormData(document.forms.person);
|
||||||
// But that will add 25% of size.
|
var blob = new Blob([ resultAsUint8Array ], { type: 'application/octet-stream'});
|
||||||
xhr.send(resultAsUint8Array);
|
|
||||||
|
|
||||||
setTimeout(sendModern, 2000);
|
formData.append('binson', blob);
|
||||||
|
xhr.send(formData);
|
||||||
|
|
||||||
|
setTimeout(sendModern, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendAncient() {
|
function sendAncient() {
|
||||||
|
@ -64,10 +73,26 @@ function sendAncient() {
|
||||||
|
|
||||||
console.log('Sending data in ancient browsers...');
|
console.log('Sending data in ancient browsers...');
|
||||||
|
|
||||||
xhr.open('POST', 'http://localhost:8000/', true);
|
// Emulate form body. But since we can send intact only 7-bit
|
||||||
xhr.send(resultAsBinString);
|
// characters, wrap binary data to base64. That will add 30% of size.
|
||||||
|
var boundary = '----' + String(Math.random()).slice(2);
|
||||||
|
|
||||||
setTimeout(sendAncient, 2000);
|
var data = '';
|
||||||
|
|
||||||
|
data += '--' + boundary + '\r\n';
|
||||||
|
data += 'Content-Disposition: form-data; name="binson"; filename="blob"\r\n';
|
||||||
|
data += 'Content-Type: application/octet-stream\r\n';
|
||||||
|
data += 'Content-Transfer-Encoding: base64\r\n'
|
||||||
|
data += '\r\n';
|
||||||
|
data += btoa(resultAsBinString) + '\r\n';
|
||||||
|
data += '--' + boundary + '--\r\n';
|
||||||
|
|
||||||
|
|
||||||
|
xhr.open('POST', 'http://localhost:8000/');
|
||||||
|
xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
|
||||||
|
xhr.send(data);
|
||||||
|
|
||||||
|
setTimeout(sendAncient, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendModern();
|
sendModern();
|
||||||
|
|
|
@ -2,39 +2,76 @@
|
||||||
|
|
||||||
/*eslint-disable no-console*/
|
/*eslint-disable no-console*/
|
||||||
|
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const pako = require('../');
|
const pako = require('../');
|
||||||
|
const multiparty = require('multiparty');
|
||||||
|
const Promise = require('bluebird');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
|
||||||
|
const MULTIPART_RE = /^multipart\/form-data(?:;|$)/i;
|
||||||
|
const MAX_FIELDS_SIZE = 100 * 1024; // 100kb
|
||||||
|
const MAX_FILES_SIZE = 10 * 1024 * 1024; // 10mb
|
||||||
|
|
||||||
|
|
||||||
|
function error(msg) {
|
||||||
|
let e = new Error(msg);
|
||||||
|
e.statusCode = 400;
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const server = http.createServer((req, res) => {
|
const server = http.createServer((req, res) => {
|
||||||
var buf = [];
|
|
||||||
|
|
||||||
req.on('data', data => buf.push(data));
|
console.log('--- received request');
|
||||||
|
|
||||||
req.on('end', () => {
|
// Quick hack to bypass security restrictions when demo html is opened from
|
||||||
console.log('--- received request');
|
// file system. Don't do such things on production.
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
|
|
||||||
|
Promise.coroutine(function* () {
|
||||||
|
//
|
||||||
|
// Check request size early by header and terminate immediately for big data
|
||||||
|
//
|
||||||
|
let length = parseInt((req.headers['content-length'] || '0'), 10);
|
||||||
|
|
||||||
|
if (!length || isNaN(length)) throw error('Length required');
|
||||||
|
|
||||||
|
if (!MULTIPART_RE.test(req.headers['content-type'])) {
|
||||||
|
throw error('Expect form data');
|
||||||
|
}
|
||||||
|
|
||||||
|
let err = null;
|
||||||
|
|
||||||
|
let form = new multiparty.Form({
|
||||||
|
maxFieldsSize: MAX_FIELDS_SIZE,
|
||||||
|
maxFilesSize: MAX_FILES_SIZE
|
||||||
|
});
|
||||||
|
|
||||||
|
let files = yield new Promise(resolve => {
|
||||||
|
form.parse(req, function (e, fields, files) {
|
||||||
|
if (e) err = e;
|
||||||
|
resolve(files);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
err.statusCode = err.statusCode || 400;
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
// In ideal world, we should process data as stream to minimize memory use
|
// In ideal world, we should process data as stream to minimize memory use
|
||||||
// on big data (and use node's `zlib` inflate).
|
// on big data (and use node's `zlib` inflate).
|
||||||
//
|
//
|
||||||
// But that's just a quick sample to explain data reencoding steps from
|
// But that's just a quick sample to explain data reencoding steps from
|
||||||
// browser to server. Feel free to improve.
|
// browser to server. Feel free to improve.
|
||||||
|
let bin = yield Promise.fromCallback(cb => {
|
||||||
|
fs.readFile(files.binson[0].path, cb);
|
||||||
|
});
|
||||||
|
|
||||||
// Join all pending chunks.
|
// Kludge - here we should cleanup all files
|
||||||
let bin = Buffer.concat(buf);
|
fs.unlinkSync(files.binson[0].path);
|
||||||
|
|
||||||
// Test header to understand if data was sent as raw binary (modern browser)
|
|
||||||
// or string (utf8) format. If utf8 - reencode to binary.
|
|
||||||
//
|
|
||||||
// We could also use base64 encoding for strings on client side, but that
|
|
||||||
// needs more libraries for old browsers (for unsupported `window.btoa()`).
|
|
||||||
//
|
|
||||||
// If you don't need IE8/9 support - just drop this part.
|
|
||||||
if (/UTF-8/i.test(String(req.headers['content-type']))) {
|
|
||||||
console.log('--- data has utf-8 encoding');
|
|
||||||
|
|
||||||
bin = Buffer.from(bin.toString(), 'binary');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decompress binary content
|
// Decompress binary content
|
||||||
// Note! Can throw error on bad data
|
// Note! Can throw error on bad data
|
||||||
|
@ -48,17 +85,14 @@ const server = http.createServer((req, res) => {
|
||||||
let obj = JSON.parse(decoded);
|
let obj = JSON.parse(decoded);
|
||||||
|
|
||||||
console.log('--- received object is: ', obj);
|
console.log('--- received object is: ', obj);
|
||||||
|
|
||||||
// Quick hack to bypass security restrictions when demo html is opened from
|
|
||||||
// file system. Don't do such things on production.
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
||||||
res.end('ok');
|
res.end('ok');
|
||||||
|
})()
|
||||||
|
.catch(err => {
|
||||||
|
console.log(err);
|
||||||
|
res.statusCode = err.statusCode || 400;
|
||||||
|
res.end(err.message);
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', () => {
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
||||||
res.end('error');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(8000);
|
server.listen(8000);
|
||||||
|
|
18
package.json
18
package.json
|
@ -21,20 +21,22 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": "nodeca/pako",
|
"repository": "nodeca/pako",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "^3.2.0",
|
|
||||||
"benchmark": "*",
|
|
||||||
"ansi": "*",
|
"ansi": "*",
|
||||||
|
"async": "*",
|
||||||
|
"benchmark": "*",
|
||||||
|
"bluebird": "^3.5.0",
|
||||||
"browserify": "*",
|
"browserify": "*",
|
||||||
"eslint": "^3.12.2",
|
"eslint": "^3.12.2",
|
||||||
"eslint-plugin-nodeca": "~1.0.3",
|
"eslint-plugin-nodeca": "~1.0.3",
|
||||||
"uglify-js": "*",
|
|
||||||
"istanbul": "*",
|
|
||||||
"ndoc": "*",
|
|
||||||
"lodash": "*",
|
|
||||||
"async": "*",
|
|
||||||
"grunt": "^1.0.1",
|
"grunt": "^1.0.1",
|
||||||
"grunt-cli": "^1.2.0",
|
"grunt-cli": "^1.2.0",
|
||||||
|
"grunt-contrib-connect": "^1.0.2",
|
||||||
"grunt-saucelabs": "^9.0.0",
|
"grunt-saucelabs": "^9.0.0",
|
||||||
"grunt-contrib-connect": "^1.0.2"
|
"istanbul": "*",
|
||||||
|
"lodash": "*",
|
||||||
|
"mocha": "^3.2.0",
|
||||||
|
"multiparty": "^4.1.3",
|
||||||
|
"ndoc": "*",
|
||||||
|
"uglify-js": "*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue