Skip to content

Instantly share code, notes, and snippets.

View montanaflynn's full-sized avatar

Montana Flynn montanaflynn

View GitHub Profile
@montanaflynn
montanaflynn / CONCURRENCY.md
Last active April 20, 2024 17:00
Examples of sequential, concurrent and parallel requests in node.js

Concurrency in JavaScript

Javascript is a programming language with a peculiar twist. Its event driven model means that nothing blocks and everything runs concurrently. This is not to be confused with the same type of concurrency as running in parallel on multiple cores. Javascript is single threaded so each program runs on a single core yet every line of code executes without waiting for anything to return. This sounds weird but it's true. If you want to have any type of sequential ordering you can use events, callbacks, or as of late promises.

@montanaflynn
montanaflynn / aes.js
Last active April 9, 2024 15:01
Encrypt and decrypt text using AES with a shared key in nodejs
const crypto = require('crypto');
const password = 'password';
function encrypt(password, text) {
const algorithm = 'aes-256-ctr';
const key = Buffer.concat([Buffer.from(password), Buffer.alloc(32)], 32);
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(text);
@montanaflynn
montanaflynn / streamer.js
Created January 27, 2015 19:38
Streaming a response with Express.js
var express = require('express')
var app = express()
app.listen(1337)
app.all('/stream/:chunks', function (req, res, next) {
res.writeHead(200, {
'Content-Type': 'text/plain',
@montanaflynn
montanaflynn / aes.go
Last active December 23, 2023 14:39
Decrypt aes-256-ctr in Golang encrypted with node.js
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
"strings"
)
@montanaflynn
montanaflynn / concurrency.go
Last active October 26, 2023 13:35
A simple example of bounded concurrency and wait groups in Golang
package main
import (
"fmt"
"sync"
"time"
)
func main() {
@montanaflynn
montanaflynn / pget.go
Last active October 21, 2023 06:12
Bounded Parallel Get Requests in Golang
package main
import (
"fmt"
"net/http"
"sort"
"time"
)
// a struct to hold the result from each request including an index
@montanaflynn
montanaflynn / main.go
Last active October 19, 2023 23:54
Example of using Golang errgroup with context.WithTimeout()
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
@montanaflynn
montanaflynn / main.go
Last active October 19, 2023 15:27
Gin request timeout middleware and handler
package main
import (
"context"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
@montanaflynn
montanaflynn / fortune.fish
Created October 3, 2014 15:45
Fun with fish and fortunes
# place this in your fish path
# ~/.config/fish/config.fish
function fish_greeting
if not type fortune > /dev/null 2>&1
apt-get install fortune
end
fortune -a
end
@montanaflynn
montanaflynn / context.go
Last active September 13, 2023 12:51
An example of using context to cancel goroutines between server handlers
package main
import (
"context"
"fmt"
"log"
"net/http"
"sync"
"time"
)