Skip to content

Instantly share code, notes, and snippets.

@jonshipman
jonshipman / map-usa.js
Created December 14, 2022 15:38
USA Contiguous GeoJSON Formatted features.
/**
* Simple map of the USA.
* Merged from johan/world.geo.json countries\USA folder.
*
* @see https://github.com/johan/world.geo.json
*/
const MapPolygon = {
type: 'FeatureCollection',
features: [
@jonshipman
jonshipman / sudo.ps1
Created December 9, 2022 19:43
sudo implementation for powershell
# Place this function in you $Env:Profile
# Has worked with most of what I've thrown at it.
# E.g. `sudo cmd.exe /C mklink /J C:\Test C:\Windows`
function Sudo {
[string] $Location = (Get-Location)
$NewArgs = @()
For ( $i = 0; $i -Lt $args.Length; $i++) {
$NewItem = "$($args[$i])".Replace(" ","`` ").Replace("'","``'").Replace('"','``"')
@jonshipman
jonshipman / Mongoose Async Virtuals.md
Created October 31, 2022 20:33
A no-plugins approach to implementing virtuals that use a promise.

Mongoose Async Virtuals

Demo of Async Virtuals in Monoose 6.2.7. Using no plugins, it's three steps.

  1. Declare an instance method (it's loadChildren in the example). The instance method should store the results into a this._variable (variable name is not relevant, just be sure not to use one that's already in use).

  2. In schema.post('find'), run an Promise that fires your instance method in a loop using await Promise.all().

NodeJS static class override example

Mongoose model replacment for .watch()

Just a gist from a larger project. Worker.runonce is a function that will ask the primary process for permission to proceed. The primary process will track what activites are run where (through the unique key) and pass down the approved pid. If the worker's pid doesn't match what prime messages out, it will not run the callback.

The purpose of overriding watch is that having so many watch actions on the prime process was causing OOM Errors in Heroku. Model.watch().on() can be called as normal.

@jonshipman
jonshipman / Worker.js
Created October 25, 2022 13:28
NodeJS Worker Forking - Allows workers to execute a command once regardless of process. Primary process keeps track of open "slots" and broadcasts an open PID when getting execution request (through runonce). If the PID matches that process, the callback executes.
import EventEmitter from 'node:events';
import cluster from 'cluster';
import { v4 as uuidv4 } from 'uuid';
import CONFIG, { isDevelopment } from '../configuration.js';
class Worker extends EventEmitter {
variables = {};
workers = {};
@jonshipman
jonshipman / search-keywords.js
Created August 17, 2022 21:54
Example of MongoDB Change Stream updating the same document.
/**
* I wanted to create an example of a change stream that updates itself.
* I couldn't find any examples online that weren't a simple "console.log" in the change stream.
* The secret sauce is in the $update.$match.
* We are only watching for changes that do not update the searchKeywords (in this example).
*
* In execution this collection has a text index on "searchKeywords" and createEdgeNGrams is a standard algorithm for splitting up words.
* If you ran LeadModel.updateOne({_id: anyId}, {name:'Hello World'}), the on('change') will be triggered and update the searchKeywords.
* Since this followup update changes searchKeywords, the filter will prevent an infinite loop.
*/
@jonshipman
jonshipman / Hooks.js
Created May 27, 2022 22:02
Gets all the styling hooks in use on SLDS
export default class Hooks {
constructor() {
console.log(this.styleHooks);
}
get stylesheets() {
return [...document.styleSheets, ...document.adoptedStyleSheets];
}
get rules() {
@jonshipman
jonshipman / desc.log
Created March 18, 2022 00:44
Error building on Linux ARM64 Yarn 3.2.0
➤ YN0000: ┌ Resolution step
➤ YN0032: │ fsevents@npm:2.3.2: Implicit dependencies on node-gyp are discouraged
➤ YN0061: │ rollup-plugin-inject@npm:3.0.2 is deprecated: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.
➤ YN0013: │ vscode-textmate@https://github.com/octref/vscode-textmate.git#commit=e65aabe2227febd
➤ YN0032: │ fsevents@npm:2.1.3: Implicit dependencies on node-gyp are discouraged
➤ YN0061: │ fsevents@npm:2.1.3 is deprecated: "Please update to latest v2.3 or v2.2"
➤ YN0013: │ vscode-textmate@https://github.com/octref/vscode-textmate.git#commit=e65aabe2227febd
➤ YN0058: │ vscode-textmate@https://github.com/octref/vscode-textmate.git#commit=e65aabe2227febda7beaad31dd0fca1228c5ddf3: Packing the package failed (exit code 1, logs can be found here: /tmp/xfs-bda6b316/pack.log)
➤ YN0000: └ Completed in 2m 7s
➤ YN0000: Failed with errors in 2m 7s
@jonshipman
jonshipman / Set-ScreenResolution.ps1
Created February 7, 2022 13:59
Change Screen resolution with powershell.exe (not pwsh.exe core!)
# REF: https://kindleit.blogspot.com/2012/09/changing-screen-resolution-with.html
# Execute via ``powershell.exe -File .\path-to-script.ps1 1920 1080``
param (
[Parameter(Mandatory=$true, Position = 0)]
[int]
$Width,
[Parameter(Mandatory=$true, Position = 1)]
[int]
$Height