Skip to content

Instantly share code, notes, and snippets.

// Option 1
async function streamToString(stream: NodeJS.ReadableStream): Promise<string> {
const chunks: Array<any> = [];
for await (let chunk of stream) {
chunks.push(chunk)
}
const buffer = Buffer.concat(chunks);
return buffer.toString("utf-8")
}
@IcarusFW
IcarusFW / .gulpfile
Last active August 3, 2022 12:52
Nunjucks - JSON into Template Macro
var manageEnvironment = function(environment) {
environment.addFilter("json", function(value) {
return JSON.parse(value); // convert the complete string imported by Nunjucks into JSON and return
});
};
gulp.task("nunjucks", function() {
// .njk files in pages
return (
gulp
@Curtis017
Curtis017 / index.ts
Created March 8, 2019 03:29
Simple middleware implementation using typescript and ES6
import pipeline, { Middleware } from './pipeline';
const step1: Middleware<any, any> = (req, res, next) => {
if (req.body) {
console.log(`STEP 1: \n req: ${JSON.stringify(req)}\n res: ${JSON.stringify(res)}`);
next();
}
}
const step2: Middleware<any, any> = async (req, res, next) => {
@evanderkoogh
evanderkoogh / gist:f9012bc0163b6d9f2bde49046d8cf720
Created April 23, 2017 15:18
Example Edge@Lambda in AWS Cloudfront. Change URI based on host header
'use strict';
/*
* HTTP headers are case-insensitive. So 'Host' is a valid header, but so
* is 'host' or 'hOst'. To make sure we don't miss a header we first lowerCase
* all of them and then check for the lowercase version of the header
*/
const normaliseHeaders = (headers) => {
const normalisedHeaders = {};
const fields = Object.keys(headers);
@natemartinsf
natemartinsf / LinkButton.js
Created November 27, 2016 05:48
Wrapped @blueprintjs Button to work with React-Router
import React, {Component} from 'react';
import { Button } from '@blueprintjs/core';
import withRouter from 'react-router/lib/withRouter';
let typeOf = (o) => toString.call(o).slice(8, -1).toLowerCase();
function createLocationDescriptor({to, query, hash, state}) {
if (typeOf(to) === 'string') {
return {pathname: to, query, hash, state};
}
@zmmbreeze
zmmbreeze / resolvePath.js
Created December 2, 2013 11:03
`path.resolve` for browser
var resolvePath = function () {
function resolve(pathA, pathB) {
// 先做split,得到的结果如下几种
// ‘a’ => ['a']
// 'a/b' => ['a', 'b']
// '/a/b' => ['', 'a', 'b']
// '/a/b/' => ['', 'a', 'b', '']
pathB = pathB.split('/');
if (pathB[0] === '') {
// 如果pathB是想对于根目录
@billie66
billie66 / nginx.conf
Created September 11, 2012 06:49
Deploy Rails on Ubuntu server 12.04 with Nginx, Unicorn, Mongodb, Mongoid
# This file should be placed on the directory of ~/blog/config
upstream unicorn {
server unix:/tmp/unicorn.todo.socket fail_timeout=0;
}
server {
listen 80 default;
#server_name example.com;
root /home/username/blog/public;
@killercup
killercup / README.md
Created March 16, 2012 11:03
Unicorn, RVM Wrapper, Init.d

You have your Rails Apps with specific Gemsets in RVM.

The following commands creates a wrapped unicorn_rails bin. Be sure to replace the variables and that you have unicorn in your bundle.

rvmsudo rvm wrapper [RUBY VERSION]@[GEMSET] [GEMSET] unicorn_rails

Now you have a /usr/local/rvm/bin/[GEMSET]_unicorn_rails I will refer to [GEMSET]_unicorn_rails as [WRAPPED_NAME]

Proposal for Improving Mass Assignment

For a while, I have felt that the following is the correct way to improve the mass assignment problem without increasing the burden on new users. Now that the problem with the Rails default has been brought up again, it's a good time to revisit it.

Sign Allowed Fields

When creating a form with form_for, include a signed token including all of the fields that were created at form creation time. Only these fields are allowed.

To allow new known fields to be added via JS, we could add:

@potatosalad
potatosalad / project-web-reload.conf
Created February 11, 2012 03:32
Upstart + Bluepill + Unicorn with hot restart
# /etc/init/project-web-reload.conf
pre-start script
initctl restart project-web
sleep 15
end script
exec /usr/local/rvm/bin/default_bluepill restart