Skip to content

Instantly share code, notes, and snippets.

View WORMSS's full-sized avatar

WORMSS WORMSS

View GitHub Profile
@WORMSS
WORMSS / ws.js
Last active June 26, 2023 08:59 — forked from tabjy/ws.js
WebSocket from scratch. In Node.js
const http = require('http')
const crypto = require('crypto')
const server = http.createServer((req, res) => {
console.log('got request', req.url)
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('okay')
})
server.on('upgrade', function (req, socket) {
@WORMSS
WORMSS / ApiBuilder.ts
Last active March 15, 2022 12:50
Mini Api Builder
type URLSearchParamsInit = ConstructorParameters<typeof URLSearchParams>[0];
type ApiFormatMap = {
arrayBuffer: ArrayBuffer;
text: string;
json: object;
formData: FormData;
blob: Blob;
stream: ReadableStream<Uint8Array>;
response: Response;
};
@WORMSS
WORMSS / gitcommands.md
Last active October 20, 2021 10:18
Git Commands I usually forget but need maybe once every 2 years

Git Get Remote Sha:

$ git fetch origin <sha>

Git Octopus merge staging:

Make sure the staging area is how you want the commit to be

$ git write-tree
@WORMSS
WORMSS / types.d.ts
Last active December 1, 2020 13:26
Typescript: My advanced types I don't want to lose
type Optional<T, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
type AllOrNothing<T> = T | Partial<Record<keyof T, never>>;
type OnlyOne<T> = {
[K in keyof T]: (
Required<Pick<T, K>> & Partial<Record<Exclude<keyof T, K>, never>>
)
}[keyof T];
type Test = {
@WORMSS
WORMSS / i18n.js
Last active December 31, 2019 15:55
Localisation of html pages for Chrome Extensions.
/**
* @author Colin Richardson
*
* @description this script in combination with a chrome extension enviroment adds i18n capabilities to html pages without jQuery.
* html elements will need a data-i18n tag with the message name and will be placed within the elements innerHTML attribute.
*
* This can be expanded/modified with the use of data-i18n-attr and place the name of the attribute you wish to assign the message to.
* EG: title, value, etc etc
* EG: data-i18n-attr="title", data-i18n-attr="value", etc etc
*
@WORMSS
WORMSS / octopus_merge.sh
Last active April 27, 2018 10:09
Octopus Merge
# Make octopus from current commit/index
commit_id=$(git commit-tree $(git write-tree) -p commit1 -p commit2 -p commit3 -m "Octopus Message")
git branch -f "tmp/octopus" "$commit_id"
@WORMSS
WORMSS / express-upload-multer-example
Created July 7, 2017 12:32
Example code for uploading a file with express and multer
const express = require("express");
const multer = require("multer");
const upload = multer({"dest": "./mult"});
const app = express();
app.get("/", indexHandler);
app.post("/save", upload.single("fileField"), saveHandler);
@WORMSS
WORMSS / MongoUtils.js
Last active July 5, 2017 13:50
Mongo Connection Sharing
var _db;
module.exports = {
connect: function ({host = "localhost", port = 27017, dbname = "exampleDb"} = {}) {
return require("mongodb").MongoClient.connect(`mongodb://${host}:${port}/${dbname}`)
.then(db => _db = db);
},
collection: function (col_name) {
if ( !_db ) {
return null;
@WORMSS
WORMSS / index.js
Last active April 28, 2017 08:20
Express MethodOverride
const Express = require("express");
const methodOverride = require("method-override");
const app = new Express();
const router = new Express.Router();
//app.use(methodOverride("_method", {"methods": ["POST", "GET"]})); // Works too.
router.use(methodOverride("_method", {"methods": ["POST", "GET"]})); // on route so only the following use the method override.
router.get("/:id", all("get"));
@WORMSS
WORMSS / micro-rest-api-server.js
Last active April 18, 2017 13:27
micro-rest-api-server
// imports
const Express = require("express");
const bodyParser = require("body-parser");
// Some datastore to play with.
const bookDb = initDb();
// Set up express app.
const app = new Express();