Skip to content

Instantly share code, notes, and snippets.

View bodokaiser's full-sized avatar
😀
Happy to be here!

Bodo Kaiser bodokaiser

😀
Happy to be here!
View GitHub Profile
@bodokaiser
bodokaiser / stream.js
Last active December 15, 2015 14:58
stream.Readable returns data on first read call even it first should buffer
var util = require('util');
var http = require('http');
var stream = require('stream');
var crypto = require('crypto');
var server = http.createServer();
server.on('request', function(request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.end('Hello World\n');
@bodokaiser
bodokaiser / unshift_to_source_stream.js
Last active December 15, 2015 15:18
Gist which shows "unexpected" behavior that when I unshift data back on the source I get no call of "_read".
var stream = require('stream');
var rOne = new stream.Readable();
var rTwo = new stream.Readable();
rTwo.isBody = false;
rTwo.source = rOne;
rOne._read = function() {};
@bodokaiser
bodokaiser / basic_websocket_server.js
Created April 1, 2013 14:01
This is a really basic implementation of the WebSocket protocol (RFC 6455) which only does a basic handshake and then logs incoming text frames. Frames which use additional length bytes will cause the server echo shit.
var util = require('util');
var http = require('http');
var crypto = require('crypto');
var server = http.createServer();
server.on('request', function(request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.end('Hello World');
});
@bodokaiser
bodokaiser / cache_stream.js
Last active December 15, 2015 17:59
This stream example will read incoming bytes as two-byte package. So it should cache buffers until they have a length of two and it should recall itself to handle buffers which hold multiple packages. The first target of caching bytes until there are two works great but the second target where it should reread cached buffers which contain more t…
var util = require('util');
var stream = require('stream');
var parser = new stream.Readable();
var source = new stream.Readable();
parser.cache = [];
parser.source = source;
parser._read = function() {
@bodokaiser
bodokaiser / wsserver_api.js
Last active December 15, 2015 19:39
Gist to try out different API designs for node-websockets WebSocketServer.
// for small text messages
wsserver.on('message', function(message, wssocket) {
wsserver.broadcast(message);
});
// for large, fragmented messages
wsserver.on('stream', function(request, response) {
response.select([2, 4, 5]);
request.pipe(response);
});
@bodokaiser
bodokaiser / parser.cc
Last active December 16, 2015 04:38
Nodejs C++ Addon which takes buffer and does bit operations.
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
#include <iostream>
using namespace v8;
Handle<Value> CalcHeadSize(const Arguments &args) {
HandleScope scope;
@bodokaiser
bodokaiser / node_buffer_to_v8.cc
Created April 13, 2013 16:41
Gist which should export a node Buffer instance to v8 environment.
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
using namespace v8;
Handle<Value> CreateBuffer(const Arguments &args) {
HandleScope scope;
char chunk[4];
#include "parser.h"
Handle<Value> CalcHeadSize(const Arguments &args) {
HandleScope scope;
Local<Object> object = args[0]->ToObject();
if (node::Buffer::HasInstance(object)) {
if (node::Buffer::Length(object) < 2) {
ThrowException(Exception::TypeError(
#include <v8.h>
#include <node.h>
inline Local<Value> ThrowTypeError(const char* message) {
HandleScope scope;
ThrowException(Exception::TypeError(String::New(message)));
return scope.Close(Undefined());
};
@bodokaiser
bodokaiser / node_buffer_free.cc
Last active December 16, 2015 05:19
Example which shows C allocated memory we set on a buffer and we know how it is getting freed.
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
using namespace v8;
typedef unsigned char byte;
Handle<Value> ReturnBufferWithCAllocatedMemory(const Arguments &args) {
HandleScope scope;