Skip to content

Instantly share code, notes, and snippets.

View ralphtheninja's full-sized avatar
🏠
Working from home

ralphtheninja

🏠
Working from home
View GitHub Profile
@ralphtheninja
ralphtheninja / read-stream.js
Created December 29, 2012 02:46
refactoring idea of lib/read-stream.js
/* Copyright (c) 2012 Rod Vagg <@rvagg> */
var Stream = require('stream').Stream
, BufferStream = require('bufferstream')
, toEncoding = require('./util').toEncoding
, toSlice = require('./util').toSlice
, extend = require('./util').extend
@ralphtheninja
ralphtheninja / inherit-test.js
Last active December 10, 2015 12:08
testing different ways of inheriting in node
function Base () {}
Base.prototype = {
foo: function () {
console.log('.. in Base.foo()')
}
, dude: function () {
console.log('.. in Base.dude()')
}
@ralphtheninja
ralphtheninja / object-literal-refactor.js
Created January 3, 2013 00:28
Showing difference in scope and clarity between object literal assignment to prototype object rather than direct assignment of methods to method properties.
// Compare the following. In this snippet it's unclear in what context these methods are defined
, pipe: function (dest) {
if (typeof dest.add == 'function' && this._options.type == 'fstream') {
this._dataEvent = 'entry'
this.on('entry', function (data) {
var entry = new BufferStream()
entry.path = data.key.toString()
entry.type = 'File'
entry.props = {
@ralphtheninja
ralphtheninja / indent.js
Created January 25, 2013 01:54
Indentation problems with object literals and functions and js3-mode
// js3 mode with '(js3-lazy-commas t) and '(js3-curly-indent-offset 2) in .emacs
// produces this type of object literals, which is what I want
var obj = {
name: 'magnus'
, number: 314
}
// Howver, this fucks up indentation in functions. Here I want TWO spaces.
function foo() {
var someVar
var npm = require('npm')
, fs = require('fs')
, rimraf = require('rimraf')
, _ = require('underscore')
, skipVersions = [ '0.2.1' ]
, moduleBase = __dirname + '/node_modules/'
, levelupBase = moduleBase + 'levelup'
if (fs.existsSync(levelupBase)) {
rimraf.sync(levelupBase)
/*jshint indent:4*/
var count = 0;
while (false)
count++;
/*jshint forin:false*/
var key;
for (key in obj) {}
/*jshint newcap:false */
function foo(){}
var obj = new foo();
var bigBlob = Array.apply(null, Array(1024 * 100)).map(function () { return 'aaaaaaaaaa' }).join('')
@ralphtheninja
ralphtheninja / copy.js
Created March 3, 2013 21:58
node v0.9.11 stream problems. My video file is 350Mb large. Mplayer starts up but stops after a while (like 10 seconds). It seems that child.stdin is not emitting 'ondrain', but I'm not 100% sure. If I copy the data with createReadStream/createWriteStream it works. Works fine on v0.8.19
#!/usr/bin/env node
// This works just fine.
var fs = require('fs')
, from = fs.createReadStream('./video')
, to = fs.createWriteStream('./video-copy')
from.pipe(to)