Skip to content

Instantly share code, notes, and snippets.

View ortense's full-sized avatar
💀
Coding...

Marcus Ortense ortense

💀
Coding...
View GitHub Profile
@ortense
ortense / node-stream-v3.js
Created May 3, 2019 00:49
Example of node stream API V3
import { promisify } from 'util'
import { pipeline } from 'stream'
import { createGzip } from 'zlib'
import { createReadStream, createWriteStream } from 'fs'
console.time('processing time')
promisify(pipeline)(
createReadStream('./big-file.iso'),
createGzip(),
@ortense
ortense / index.ts
Created February 4, 2019 14:20 — forked from andregardi/index.ts
import "reflect-metadata";
import { createConnection } from "typeorm";
import * as express from "express";
import * as bodyParser from "body-parser";
import * as helmet from "helmet";
import * as cors from "cors";
//Connects to the Database -> then starts the express
createConnection()
.then(async connection => {
const createTimer = () => {
let paused = true
let counter = 0
let interval
return {
play() {
paused = false
interval = setInterval(() => counter += 1, 1000)
},
@ortense
ortense / prop.js
Last active March 31, 2018 14:28
A simple way to retrieve a property value from an object if it exist.
const get = (obj, prop, ...props) => {
const val = obj[prop]
if (!props.length || !val) return val
return get(val, ...props)
}
const propertyPathToArray = (path = '') =>
path.replace(/\[/g, '.').replace(/\]/g, '').split('.')
const prop = (path, obj) =>

Node stream big file

../big-file-download
├── package.json
├── src
│   └── server.js
└── static
    ├── index.html
 ├── not-found.html
const Joi = require('joi')
// @see https://github.com/hapijs/joi/blob/v13.0.2/API.md#extendextension
const customJoi = Joi.extend((joi) => ({
base: joi.number(),
name: 'number',
language: { stringify: 'parse number to string' },
pre(value, state, options) {
if (this._flags.stringify) return value.toString()
return value
const { MongoClient } = require('mongodb')
const Bluebird = require('bluebird')
const DBURL = 'mongodb://localhost/demo'
const COLNAME = 'demo'
MongoClient.connect(DBURL, { promiseLibrary: Bluebird })
.tap(() => console.log('Connected!'))
.then(async db => {
const result = await db.collection(COLNAME).insert({ value: Math.random() })
@ortense
ortense / custom-errors.js
Last active September 23, 2017 13:32
A simple way to create elegant custom errors with ES6 classes.
'use strict'
export class CustomError extends Error {
constructor(message) {
super(message)
this.type = this.name = this.constructor.name
Object.defineProperties(this, {
type: { enumerable: true, writable: false },
@ortense
ortense / express-joi-request-validation-middleware.js
Last active September 23, 2017 13:38
A simple way to validate request schema in express with Joi
const Joi = require('joi')
class ValidationError extends Error {
constructor(joiError) {
super('Request validation error')
this.details = joiError.details
this.type = this.name = this.constructor.name
Object.defineProperties(this, {
type: { enumerable: true, writable: false },
name: { enumerable: false, writable: false },
@ortense
ortense / stack.js
Created August 7, 2017 02:43
Simple factory function of immutable stack
const stack = (...values) =>
Object.create({
get: index => values[index],
add: value => stack(...[value].concat(values)),
remove: () => stack(...values.slice(1)),
toString: () => `stack [${values.join(' ')}]`,
toArray: () => values.slice(0),
get length() { return values.length },
constructor: stack
})