../big-file-download
├── package.json
├── src
│ └── server.js
└── static
├── index.html
├── not-found.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createTimer = () => { | |
let paused = true | |
let counter = 0 | |
let interval | |
return { | |
play() { | |
paused = false | |
interval = setInterval(() => counter += 1, 1000) | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |