-
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!
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.
Approach:
- The file starts with shell code.
- 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.
- Node.js does not currently have CLI flags for achieving what we do in this step. Therefore, we have to pipe to the
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== .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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Transformer Test</title> | |
</head> | |
<body> | |
<script type="module"> | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Readable} from 'node:stream'; | |
import {TextDecoderStream} from 'node:stream/web'; | |
import {spawn} from 'node:child_process'; | |
const childProcess = spawn( | |
'echo', ['hello world'], | |
{ | |
stdio: ['ignore', 'pipe', 'inherit'], | |
} | |
); |
- Problem: We can only transfer callable objects and primitive values between a ShadowRealm and an incubator realm.
- This code demonstrates a simple solution (that’s not completely safe).
- Runs in Safari Technology Preview: https://webkit.org/blog/12522/release-notes-for-safari-technology-preview-142/
- A more complete solution: https://github.com/caridy/irealm
Material:
- Proposal for ShadowRealms: https://github.com/tc39/proposal-shadowrealm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== Solution 1: array.flatMap() and array.map() ========== | |
const prios = (arrayOfArrays) => arrayOfArrays.flatMap( | |
(arr, index) => { | |
return arr.map(elem => ({...elem, priority: index+1})) | |
} | |
); | |
// .flatMap() both maps and flattens | |
// More information: https://exploringjs.com/impatient-js/ch_arrays.html#Array.prototype.flatMap |