Fetch in nodejs

#!/usr/bin/env node --experimental-modules

import https from 'https'; // or http if you need

async function fetch(url) {
    return new Promise((resolve, reject) => {
        const request = https.get(url, {timeout: 1000}, (res) => {
            if (res.statusCode < 200 || res.statusCode > 299) {
                return reject(new Error(`HTTP status code ${res.statusCode}`));
            }

            const body = [];
            res.on('data', (chunk) => body.push(chunk));

            res.on('end', () => {
                resolve(Buffer.concat(body).toString());
            });
        });

        request.on('error', (err) => {
            reject(err);
        });

        request.on('timeout', () => {
            request.destroy();
            reject(new Error('timed out'));
        });
    });
}

const html = await fetch('https://ozana.cz');

#nodejs#Snippets#2021

File exists in nodejs

There are two functions fs.exists() and fs.existsSync(). Function fs.exists() is deprecated, but fs.existsSync() is not. Everyone should use fs.stat() or fs.access() instead, these functions are part of Callback API and can be easily transform to the promise:

#!/usr/bin/env node --experimental-modules

import {access, constants} from 'fs';
import {homedir} from 'os';

let fileExists = s => new Promise(resolve => access(s, constants.F_OK, error => resolve(!error)));
const exists = await fileExists(`${homedir()}/Downloads/something.png`);

#nodejs#Snippets#2021

How to delete all local TimeMachine snapshots

Your Time Machine backup disk might not always be available, so Time Machine also stores some of its backups on your Mac. These backups are called local snapshots. Local TimeMachine snapshots take a large amount of disk space. This space is listed as purgeable in disk info, but cannot be actually used until the system decides to free it up. You can list all local backups with:

tmutil listlocalsnapshots /

Then delete one by one, with follow command:

sudo tmutil deletelocalsnapshots <snapshot date>

You can lists all snaphosts and deletes all of them in a loop:

for d in $(tmutil listlocalsnapshotdates | grep "-"); do sudo tmutil deletelocalsnapshots $d; done

#macOS#TimeMachine#2021

AutoExpand textarea

import autosize from "https://cdn.jsdelivr.net/npm/autosize@5.0.1/dist/autosize.min.js";

/**
 * WebComponent with autosized textarea
 *
 * @see https://blog.jim-nielsen.com/2020/automatically-resize-a-textarea-on-user-input/
 * @see https://codepen.io/jimniels/pen/MWeawPV
 */
class AutoExpand extends HTMLTextAreaElement {
    constructor() {
        super();
        autosize(this);
    }
}


customElements.define("auto-expand", AutoExpand, {extends: "textarea"});

And HTML:

<textarea is="auto-expand"></textarea>

#HTML#Javascript#Snippets#2021

August 2021

Javascript

  • nprogress - nano progress bar
  • heroicons - Beautiful hand-crafted SVG icons for Tailwind
  • pnpm - Fast, disk space efficient package manager

Social

  • Mastodon - Social networking, back in your hands - open source social network

MacOS App

  • Raycast - control your tools with a few keystrokes

Monospaced fonts

Gear

#notes#2021