Skip to content

Instantly share code, notes, and snippets.

@josephrocca
josephrocca / BigMap.js
Last active May 19, 2024 21:35
BigMap - wrapper to get past the ~16 million key limit on JavaScript Maps
// only covers a small subset of the Map api!
// haven't debugged yet!
class BigMap {
constructor(iterable) {
if(iterable) throw new Error("haven't implemented construction with iterable yet");
this._maps = [new Map()];
this._perMapSizeLimit = 14000000;
this.size = 0;
}
# on local machine
git clone https://github.com/comfyanonymous/ComfyUI
cd ComfyUI/custom_nodes
git clone https://github.com/ltdrdata/ComfyUI-Manager.git
cd ../
# start docker
docker run --name comfyui -p 8188:8188 -it --rm --gpus all -v $PWD:/workspace pytorch/pytorch:2.2.1-cuda12.1-cudnn8-devel bash
apt-get update
apt-get install wget git libgl1 libglib2.0-0 -y
@josephrocca
josephrocca / generate.js
Last active November 5, 2023 16:06
Generate map tiles from a large image with nodejs
// Given a large input image, this script generates map tiles of 256*256px
// at power-of-2 zoom levels. It's not super efficient or anything, just a
// quickly hacked together script.
//Use the tiles in leaflet.js like this:
/*
<div id='map' style='height:100%;'></div>
<script>
var map = L.map('map', {
attributionControl: false
@josephrocca
josephrocca / e5-large-v2.js
Last active October 25, 2023 01:29
e5-large-v2 text embedding model in JavaScript using Transformers.js
// See the comments at the end for a model that does much better than e5-large-v2 while being a third of the size.
let { pipeline } = await import('https://cdn.jsdelivr.net/npm/@xenova/transformers@2.7.0');
let extractor = await pipeline('feature-extraction', 'Xenova/e5-large-v2');
// Note: If you're just comparing "passages" with one another, then just prepend "passage: " to all texts. Only use "query: " if the text is a short "search query" like in the above example.
let passage1 = await extractor(`passage: The Shawshank Redemption is a true masterpiece of cinema.`, { pooling: 'mean', normalize: true });
let passage2 = await extractor(`passage: The film should not be exposed to sunlight when removing it from the wrapper. Otherwise your movie will come out bad.`, { pooling: 'mean', normalize: true });
let query = await extractor(`query: movie review`, { pooling: 'mean', normalize: true });
@josephrocca
josephrocca / removeExifFromJpeg.js
Created August 18, 2023 11:50
Remove EXIF data from JPEG using JavaScript - simple and extremely fast
// Code mostly written by GPT-4. Works well for me, but not extensively tested (e.g. on old or weird jpeg files)
function removeExifFromJpeg(arrayBuffer) {
let view = new DataView(arrayBuffer);
// Check if it's a JPEG file
if (view.getUint16(0, false) !== 0xFFD8) {
throw new Error("Not a valid JPEG");
}
let position = 2; // Start after the SOI marker
// This is a version of this file: https://w3c.github.io/webcodecs/samples/capture-to-file/webm-writer2.js
// With these fixed applied: https://github.com/w3c/webcodecs/issues/332#issuecomment-1077442192
/**
* A tool for presenting an ArrayBuffer as a stream for writing some simple data
* types.
*
* By Nicholas Sherlock, with updates from jimbankoski
*
* - make it work off frames with timestamps from webcodecs
@josephrocca
josephrocca / background.js
Last active July 27, 2023 04:37
Chrome extension to force-enable CORS based on request's *source* url (i.e. the url of the browser tab) rather than the target url
// Notes: we need the `sourceTabUrl &&` before the URL check because chromebooks make weird requests that don't come from "real" tabs.
let accessHeaders = new Map();
let tabIdToUrlMap = new Map();
let requestListener = function (details) {
const accessControlRequestHeader = details.requestHeaders.find(elem => elem.name.toLowerCase() === "access-control-request-headers");
if(accessControlRequestHeader) {
accessHeaders.set(details.requestId, accessControlRequestHeader.value);
}
@josephrocca
josephrocca / escapeUnicode.js
Created June 18, 2020 11:18
Replace all Unicode characters with escape codes (JavaScript function)
// This function matches all non-ASCII characters after splitting the string in a "unicode-safe" way (using `[...str]`).
// It then splits each unicode character up into its code-points, and gets the escape code for each, and then joins all
// all the ASCII characters and Unicode escapes into one string.
function escapeUnicode(str) {
return [...str].map(c => /^[\x00-\x7F]$/.test(c) ? c : c.split("").map(a => "\\u" + a.charCodeAt().toString(16).padStart(4, "0")).join("")).join("");
}
// Based on discussion in this thread: https://gist.github.com/mathiasbynens/1243213
@josephrocca
josephrocca / resize-and-center-crop.js
Last active April 17, 2023 16:10
Center crop and bicubic/bilinear/lanczos resize image in JavaScript (using wasm-vips)
// Put this in your HTML to load the `Vips` global: <script src="https://cdn.jsdelivr.net/npm/wasm-vips@0.0.2/lib/vips.js"></script>
const vips = await Vips();
async function resizeAndCenterCrop(blob, resizeType="cubic", size=224) {
// resize types available: cubic, linear, lanczos2, lanczos3, nearest, mitchell
let im1 = vips.Image.newFromBuffer(await blob.arrayBuffer());
// Resize so smallest side is `size` px:
const scale = 224 / Math.min(im1.height, im1.width);
@josephrocca
josephrocca / reddit-comment-stream.js
Last active March 26, 2023 15:47
Reddit Comment Stream (Node.js)
module.exports = function() {
let request = require("request-promise");
let stopFlag;
async function start() {
if(!this.oncomment) {
console.error("You must attach an oncomment handler (onerror handler is optional).");
return;
}