Skip to content

Instantly share code, notes, and snippets.

View bennycode's full-sized avatar
🏡
Working from home

Benny Neugebauer bennycode

🏡
Working from home
View GitHub Profile
@bennycode
bennycode / libsodium.js
Last active February 15, 2018 14:22
Working with libsodium.js v0.7.3
const _sodium = require('libsodium-wrappers-sumo');
(async () => {
await _sodium.ready;
const sodium = _sodium;
console.log(sodium.from_base64('SGVsbG8=', sodium.base64_variants.ORIGINAL)); // Uint8Array(5) [72, 101, 108, 108, 111]
console.log(sodium.from_hex('48656c6c6f')); // Uint8Array(5) [72, 101, 108, 108, 111]
console.log(sodium.from_string('Hello')); // Uint8Array(5) [72, 101, 108, 108, 111]
console.log(sodium.to_base64(new Uint8Array([72, 101, 108, 108, 111]), sodium.base64_variants.ORIGINAL)); // "SGVsbG8="
async function getAllDexieDatabaseNames(dexieRootName = '__dbnames') {
return new Promise((resolve, reject) => {
const DBOpenRequest = window.indexedDB.open(dexieRootName);
DBOpenRequest.onerror = () => reject(DBOpenRequest.error);
DBOpenRequest.onsuccess = () => {
const db = DBOpenRequest.result;
const dbNameStore = Array.from(event.target.result.objectStoreNames);
if (dbNameStore.length > 0) {
const transaction = db.transaction('dbnames', 'readonly');
transaction.onerror = () => reject(transaction.error);
@bennycode
bennycode / image-to-base64.html
Created March 14, 2018 17:07
Image file to Base64 data URI
<html>
<head></head>
<body>
<input id="file-upload" multiple="multiple" type="file"/>
<img id="file-placeholder"/>
<script>
function blobToDataURI(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
@bennycode
bennycode / init_objection.ts
Created April 15, 2018 14:30
Initialize Objection.js with Knex configuration file
const config = require(`${process.cwd()}/knexfile`)[String(process.env.NODE_ENV)];
const knex = require('knex')(config);
import {Model} from 'objection';
module.exports = async () => {
await knex.migrate.latest();
Model.knex(knex);
};
@bennycode
bennycode / ObjectionSpec.js
Created April 15, 2018 15:03
Testing Objection.js Model with Knex & Jasmine
const config = require(`${process.cwd()}/knexfile`)[process.env.NODE_ENV];
const knex = require('knex')(config);
const {Model} = require('objection');
beforeAll(async done => {
await knex.migrate.latest();
await knex(Candlestick.tableName).truncate();
Model.knex(knex);
done();
@bennycode
bennycode / isFirefoxInPrivateMode.js
Last active April 26, 2018 13:43
Check if Firefox is in private mode
var isInPrivateMode = () => {
if (!window.indexedDB) {
return Promise.resolve(false);
} else {
return new Promise((resolve, reject) => {
const name = 'test';
const DBOpenRequest = window.indexedDB.open(name);
DBOpenRequest.onerror = () => resolve(true);
DBOpenRequest.onsuccess = () => {
const db = DBOpenRequest.result;
@bennycode
bennycode / append-to-file.js
Created May 2, 2018 15:52
Node.js: Append text to file
const os = require('os');
const fs = require('fs-extra');
const file = 'logfile.txt';
const options = {flag: 'a'};
async function writeToFile(text) {
await fs.outputFile(file, `${text}${os.EOL}`, options);
}
@bennycode
bennycode / gource.sh
Created May 12, 2018 09:35
Gource settings
gource -1280x720 -o - --seconds-per-day 0.1 --hide progress,filenames | ffmpeg -y -r 60 -f image2pipe -vcodec ppm -i - -vcodec libx264 -preset ultrafast -pix_fmt yuv420p -crf 1 -threads 0 -bf 0 gource.mp4
@bennycode
bennycode / index.html
Created June 23, 2018 18:02
HTML5 Skeleton
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>HTML5</title>
</head>
<body>
<p>Hello</p>
</body>
</html>
@bennycode
bennycode / NetworkError.ts
Created June 27, 2018 12:55
Custom Error
class NetworkError extends Error {
constructor(public message: string) {
super(message);
Object.setPrototypeOf(this, NetworkError.prototype);
this.name = 'NetworkError'; // Don't use "constructor.name" here because it won't work as expected when minifying the code ;)
this.stack = new Error().stack;
}
}
export {NetworkError};