Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile
#!/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) {

Instructions

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
import {ReadableStream} from 'node:stream/web';
/**
* @param iterable an iterable (asynchronous or synchronous)
*
* @see https://streams.spec.whatwg.org/#example-rs-pull
*/
function iterableToReadableStream(iterable) {
return new ReadableStream({
async start() {
// This code has moved here: https://2ality.com/2022/10/javascript-decorators.html#example-friend-visibility
// 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,

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.

//========== .split() with lookbehind ==========
// Downside: Safari doesn’t support lookbehind
import * as assert from 'node:assert/strict';
const RE_SPLIT_AFTER_EOL = /(?<=\r?\n)/;
function splitLinesWithEols1(str) {
return str.split(RE_SPLIT_AFTER_EOL);
}