Live data from Hacker News

Modern Node.js Patterns

kashw1n.com

421–430 of 448 posts

Re: Modern Node.js Patterns

#421

You no longer need to install chalk or picocolors either, you can now style text yourself: `const { styleText } = require('node:util');` Docs: https://nodejs.org/api/util.html#utilstyletextformat-text-op...

I never needed those. I would just have an application wide object property like: text: { angry : "\u001b[1m\u001b[31m", blue : "\u001b[34m", bold : "\u001b[1m", boldLine : "\u001b[1m\u001b[4m", clear : "\u001b[24m\u001b[22m", cyan : "\u001b[36m", green : "\u001b[32m", noColor : "\u001b[39m", none : "\u001b[0m", purple : "\u001b[35m", red : "\u001b[31m", underline: "\u001b[4m", yellow : "\u001b[33m" } And then you ca…

To be fair, I do this when writing shell scripts by hand. But if it’s built in, why would I?

Re: Modern Node.js Patterns

#422
post #303
post #165

Earlier quoted context omitted.

So they did change it! Good. I definitely had a crash like that a long time ago, and you can find multiple articles describing that behavior. It was existing for quite a time, so I didn't think that is something they would fix so I didn't keep track of it.

Perhaps you're confusing it with something else? I tried down to Node 8 (2017) and the behavior is still the same. Maybe a bug in userspace promises like Bluebird? Or an older Node where promises were still experimental? I love a good mystery!

Weird. Also just tried it with v8 and it doesn't behave like I remember and also not like certain descriptions that I can find online. I remember it because I was so extremely flabbergasted when I found out about this behavior and it made me go over all of my code replacing any Promise.all() with Promise.allSettled(). And it's not just me, this blog post talks about that behavior:

https://chrysanthos.xyz/article/dont-ever-use-promise-all/

Maybe my bug was something else back then and I found a source claiming that behavior, so I changed my code and as a side effect my bug happened to go away coincidentally?

Re: Modern Node.js Patterns

#423
post #331

Earlier quoted context omitted.

For years, I heard it's better to use cron, because the problem was already solved the right way(tm). My experience with cron has been about a dozen difficult fixes in production of cron not running / not with the right permission / errors lost without being logged / ... Changing / upgrading OSes became a problem. I since switched to a small node script with a basic scheduler in it, I had ZERO issues in 7 years. My d…

If cron is broken for you, than the logic solution would be to replace it with something that does work for you. But do so at the right place and abstraction. That's hardly ever the runtime or in the app. Do One Thing (and do it well). A special domain specific scheduler microservice? One of the many Cron replacements? One of the many "SaaS cron"? Systemd? This problem has been solved. Corner cases ironed out. Free t…

I rather have my configuration centralized. Instead of configuring two things, this allows me to configure one. I’d take that trade off here.

Re: Modern Node.js Patterns

#424
post #412

Earlier quoted context omitted.

The terminal emulator is not involved in piping to other processes. Grep will search over escape codes if you don’t suppress them in non-interactive environments, so this most definitely is a string output problem. If you want to do it yourself, do it right—or defer to one of the battle-tested libraries that handle this for you, and additional edge cases you didn’t think of (such as NO_COLOR).

How do you define battle tested? Typically JS developers define that as assuming something must be safe if enough people use it. That is a huge rift between the typical JS developer and organizations that actually take security more seriously. There is no safety rating for most software on NPM and more and more highly consumed packages are being identified as malicious or compromised. If you do it yourself and get it…

I mean we’re talking about libraries to format strings here, not rendering engines. If you doubt the quality of such a lib, go read the source on GitHub. That’s what I usually do before deciding if I install something or implement it myself.

Re: Modern Node.js Patterns

#425
post #267

Earlier quoted context omitted.

> Node allows native addons in packages via the N-API so any native module aren't restricted by those permissions. (...) Node permissions (...) just restrict the Node standard library. So what? That's clearly laid out in Node's documentation. https://nodejs.org/api/permissions.html#file-system-permissi... What point do you think you're making?

What is the point of a permissions system that can be trivially bypassed?

> What is the point of a permissions system that can be trivially bypassed?

You seem to be confused. The system is not bypassed. The only argument you can make is that the system covers calls to node:fs, whereas some modules might not use node:fs to access the file system. You control what dependencies you run in your system, and how you design your software. If you choose to design your system in such a way that you absolutely need your Node.js app to have unrestricted access to the file systems, you have the tools to do that. If instead you want to lock down file system access, just use node:fs and flip a switch.

Re: Modern Node.js Patterns

#426
post #304

Earlier quoted context omitted.

> The "proper" place to solve this, is in the OS. This is my thought on using dotenv libraries. The app shouldn’t have to load environment variables, only read them. Using a dotenv function/plugin like in omz is far more preferable.

In a team setting, it can be extremely helpful to have env/config loading logic built into the repo itself. It does not mean it has be loaded by the application process, but it can be part of the surrounding tooling that is part of your codebase.

Yes, that's indeed the right place, IMO: ephemeral tooling that leverages, or simplifies OS features.

Tooling such as xenv, a tiny bash script, a makefile etc. that devs can then replace with their own if they wish (A windows user may need something different from my zsh built-in). That isn't present at all in prod, or when running in k8s or docker compose locally.

A few years ago, I surfaced a security bug in an integrated .env loader that partly leveraged a lib and partly was DIY/NIH code. A dev built something that would traverse up and down file hierarchies to search for .env.* files and merge them runtime and reload the app if it found a new or changed one. Useful for dev. But on prod, uploading a .env.png would end up in in a temp dir that this homebuilt monstrosity would then pick up. Yes, any internet user could inject most configuration into our production app.

Because a developer built a solution to a problem that was long solved, if only he had researched the problem a bit longer.

We "fixed" it by ripping out thousands of LOCs, a dependency (with dependencies) and putting one line back in the READMe: use an env loader like .... Turned out that not only was it a security issue, it was an inotify hogger, memory hog, and io bottleneck on boot. We could downsize some production infra afterwards.

Yes, the dev built bad software. But, again, the problem wasn't that quality, but the fact it was considered to be built in the first place.

Re: Modern Node.js Patterns

#430
post #422
post #303

Earlier quoted context omitted.

Perhaps you're confusing it with something else? I tried down to Node 8 (2017) and the behavior is still the same. Maybe a bug in userspace promises like Bluebird? Or an older Node where promises were still experimental? I love a good mystery!

Weird. Also just tried it with v8 and it doesn't behave like I remember and also not like certain descriptions that I can find online. I remember it because I was so extremely flabbergasted when I found out about this behavior and it made me go over all of my code replacing any Promise.all() with Promise.allSettled(). And it's not just me, this blog post talks about that behavior: https://chrysanthos.xyz/article/dont…

It could be this: https://stackoverflow.com/questions/67789309/why-do-i-get-an...

If you did something like:

    const p1 = makeP1();
    const p2 = makeP2();

    return await Promise.all([p1, p2]);
It's possible that the heuristic didn't trigger?
Post reply on HN