Skip to content

Instantly share code, notes, and snippets.

@wearhere
wearhere / xss_in_electron.html
Created January 11, 2016 09:10
What could happen if you load remote web content in Electron without toggling `nodeIntegration` off. See https://mixmax.com/blog/turnkey-electron-apps-with-meteor#safe-native-bridge for more information.
<script>
require('child_process').exec('rm -rf /*'); // Worst XSS attack ever.
</script>
@wearhere
wearhere / commands.sh
Created October 22, 2015 00:06
A pretty good-quality way to create a GIF from a 10-second 720p screencast (recorded using Quicktime Player).
# Run these commands in the Terminal. Prerequisite: you have Homebrew (http://brew.sh/) installed.
#
# Thanks to http://superuser.com/a/556031 for this technique.
# 1. Install ffmeg. Only need to do this once.
brew install ffmpeg
# 2. Generate a color palette from the video to avoid dithering (http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html).
# Default options:
# - fps=20 (Speeds up the GIF a little bit)
@wearhere
wearhere / node_memory_harness.js
Last active August 29, 2015 14:26
Simple harness to exercise a memory leak in Node. Copied from https://github.com/alexeypetrushin/synchronize/pull/32, credit @d3m3vilurr.
var immediately = function(cb) {
sync.fiber(function() {
setImmediate(sync.defer());
sync.await();
}, cb);
};
function test(m, cb) {
if (m-- === 0) return cb();
immediately(function(err) {
@wearhere
wearhere / syncing-an-async-function.js
Created July 22, 2015 19:44
Making an asynchronous method fiber-aware so that it can be called without a callback. More info at https://www.mixmax.com/blog/node-fibers-using-synchronize-js.
sync(fs, 'readFile');
sync.fiber(function() {
var data = fs.readFile('a.txt');
});
@wearhere
wearhere / calling-done-automatically.js
Created July 22, 2015 19:43
What a fiber-using function looks like if you let the fiber call `done` for you. Compare https://gist.github.com/wearhere/825a51a6a16a4337733f. More info at https://www.mixmax.com/blog/node-fibers-using-synchronize-js.
function processFile(fileName, done) {
sync.fiber(function() {
// No need for try-catches even, assuming that you're ok with `done` handling errors!
var data = /* first `sync` call */;
return /* second `sync` call */;
}, done);
}
@wearhere
wearhere / calling-done-manually.js
Last active August 29, 2015 14:25
What a fiber-using function would look like if you called `done` rather than letting the fiber do it for you. Compare https://gist.github.com/wearhere/4f07d298d077da9dcb2d. More info at https://www.mixmax.com/blog/node-fibers-using-synchronize-js.
function processFile(fileName, done) {
sync.fiber(function() {
var data;
try {
data = /* first `sync` call */;
} catch(e) {
done(e);
return;
}
function processFile(fileName, done) {
sync.fiber(function() {
var data = sync.await(fs.readFile(fileName, sync.defer()));
return /* do something with the data */;
}, done);
}
@wearhere
wearhere / fiber-functions-require-a-fiber.js
Created July 22, 2015 19:39
Fiber functions can't be used outside of a fiber. More info at https://www.mixmax.com/blog/node-fibers-using-synchronize-js.
processFile('a.txt');
// Console:
Error: no current Fiber, defer can't be used without Fiber!
sync.fiber(function() {
var processedData = processFile('a.txt');
});
function processFile(filename) {
var data = sync.await(fs.readFile(fileName, sync.defer()));
return /* do something with the data */;
}
sync.fiber(function() {
var data = sync.await(fs.readFile('a.txt', sync.defer()));
var processedData = /* do something with the data */;
});