Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile

I find it easiest to make sense of this as follows:

  • It’s an implicit parameter of functions (other than arrow functions).
  • It’s always provided when we invoke such functions – how depends on how we invoke them.

Demo:

import assert from 'node:assert/strict';

//========== this is an implicit parameter of getThis() ==========

#!/usr/bin/env node
// Use curl to download a URL to a file whose name is URL-decoded
// Related: https://github.com/curl/curl/issues/2573
import {execSync} from 'node:child_process';
// Documentation for node:child_process – https://exploringjs.com/nodejs-shell-scripting/ch_nodejs-child-process.html
export const WEB_PATH_SEP = '/';
export function getWebBasename(webPath) {
@rauschma
rauschma / README.md
Last active November 19, 2024 16:33
Bookmarklet for Mastodon: Transport a profile or post to your server

Bookmarklet for Mastodon: Transport a profile or post to your server

This bookmarklet shows a profile or a post on another Mastodon server in your server’s web app.

Installation

  • Change the value of HOST to the domain of your Mastodon server.
  • Create a bookmark and paste the lines in show-in-mastodon-web-app.js into its address (browsers are OK with pasting multiple lines).

Usage

import assert from 'node:assert/strict';
// Extended RegExp mode (flag /x) [1] via a template tag
//
// Quote: “While the x-mode flag can be used in a RegularExpressionLiteral,
// it does not permit the use of LineTerminator in RegularExpressonLiteral.
// For multi-line regular expressions you would need to use the RegExp
// constructor.”
//
// The plan is to include this functionality in re-template-tag [2]. Then
// The actual API is documented at the end of this file
/**
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
*/
const ansiConstants = {
//----- Attributes -----
Normal: 0,
// This code has moved here: https://2ality.com/2022/10/javascript-decorators.html#example-friend-visibility

My writing process

Stages of my writing process

  • Collecting material: For a number of topics that I may write about in the future, I have text files where I collect information as I come across it during coding, on the web, on Twitter, etc.

  • Outline: The collected material is my starting point. I rearrange it into an outline which I refine until I’m happy with it. Refining includes adding/removing/rearranging items and doing more research when I notice gaps in my knowledge.

  • Skeletal draft: I add bullet points and code examples until almost all of the content exists at least in skeletal form. This process often uncovers knowledge gaps and flaws in the structure of the content which I then can fix.

These are a few thoughts on how Node.js apps can make data portable between platforms. The focus in this case is on paths. Handling line terminators is covered elsewhere.

Feedback welcome!

Using the same paths on different platforms

Sometimes we’d like to use the same paths on different platforms. Then there are two issues that we are facing:

  • The path separator may be different.
  • The file structure may be different: home directories and directories for temporary files may be in different locations, etc.

Creating standalone ESM-based Node.js scripts on Unix and Windows

Approach:

  1. The file starts with shell code.
  2. That code uses Node.js to execute the file in ESM mode, after it removes the initial non-JavaScript lines.
    • Node.js does not currently have CLI flags for achieving what we do in this step. Therefore, we have to pipe to the node executable.

When editing this file, we want to use the JavaScript mode of our IDE or editor. Therefore, we try to “hide” the shell code from JavaScript as much as possible.