Skip to content

Instantly share code, notes, and snippets.

View thibauts's full-sized avatar

Thibaut Séguy thibauts

View GitHub Profile
@thibauts
thibauts / exemple.js
Last active August 3, 2018 11:52
express rerouting middleware
//
// middleware module
//
module.exports.reroute = function(url, func) {
return function(req, res, next) {
if(req.url.split('?', 1).pop() === url) {
req.url = func(req, res);
req.authorized = true;
}
next();
@thibauts
thibauts / app.js
Last active August 3, 2018 11:51
simple datastore
var fs = require('fs');
var localStore = require('./local-store');
var store = new localStore.Store({
path: '/var/data',
bucket: 'test'
});
var data = fs.readFileSync('/etc/passwd');
@thibauts
thibauts / extractLinks.js
Created May 9, 2014 22:01
Extract links from a webpage with concat-stream
@thibauts
thibauts / extractLinksStream.js
Last active August 3, 2018 11:50
Extract links from a webpage the streaming way
var request = require('request');
var extractLinksStream = extractStream.bind(null, /<a.*?href="(.*?)".*?>(.*?)<\/a>/gi);
request(process.argv[2]).pipe(extractLinksStream(function(found) {
var hrefs = found.map(function(item) { return item[1]; }); // Extract 1st capture
console.log(hrefs);
}));
@thibauts
thibauts / getContents.js
Created May 9, 2014 22:43
Simpler way to get the contents of a webpage
var request = require('request');
request(process.argv[2], function(err, res, body) {
console.log(body);
});
@thibauts
thibauts / Makefile
Last active December 3, 2021 03:39
Example Makefile publishing to gh-pages
# Publishes the html counterpart to index.md to the repo's gh-pages branch root
dist/%.html: %.md
pandoc -f markdown -t html $< > $@
build: dist/index.html
clean:
rm -rf dist/*
publish: build
@thibauts
thibauts / index.js
Last active August 29, 2015 14:08
Simple SSE middleware with implicit routing
var app = express();
var sse = new SSE();
app.get('/foo', function() {
sse.emit('sse', { message: 'hello, sse !' });
});
app.use('/sse', sse.forward('sse')); // The param to forward is the name of the dispatched event
@thibauts
thibauts / stream-stream.js
Last active August 3, 2018 11:44
Takes a stream of filenames on stdin and outputs the file contents on stdout each time a new filename is pushed.
/*
* Takes a stream of filenames on stdin and outputs
* the file contents on stdout each time a new filename is pushed.
*/
var _ = require('highland');
var fs = require('fs');
function foo(filename) {
return _(fs.createReadStream(filename)); // `sequence()` expects an highland stream so let's wrap
}
var express = require('express');
var subtitlesUrl = 'http://your-local-ip:8000/subtitles.vtt';
var app = express();
app.use(function(req, res, next) {
res.header('transferMode.dlna.org', 'Streaming');
res.header('contentFeatures.dlna.org', 'DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000')
res.header('CaptionInfo.sec', subtitlesUrl);
{ id: 19789,
name: 'Main Chunk',
length: 687696,
children:
[ { id: 2, name: 'Version', length: 10 },
{ id: 15677,
name: '3D Editor Chunk',
length: 679823,
children:
[ { id: 15678, name: 'Unknown', length: 10 },