27 March 2024

AI is tasting your beer for you

In a study in Nature  the taste of a potential recipe for a Belgian beer is being predicted by Machine Learning based on its chemical components at KUL (Catholic University Leuven).

One of the primary goals of the study is brewing better tasting alcohol free beers.

The techniques used are also interesting for being tested on other types of food.

Replacing Jest for TypeScript testing

 In an earlier post we explained how we moved from ts-node to tsx.

For testing we were using Jest. Unfortunatly Jest relies on ts-node for reading typescript configuration files. We used ts-jest for the configuration and even then we needed quite some configuration in jest.config.ts to work with ESM:

import type { JestConfigWithTsJest } from 'ts-jest'

const jestConfig: JestConfigWithTsJest = {
// [...]
preset: 'ts-jest/presets/js-with-ts-esm', // or other ESM presets
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
}

export default jestConfig
With the compatibility problem with Node.js 20 we looked for some alternatives:
  • vitest
  • node builtin testing 

vitest 

Vitest is built by the Vue community, who also made the vite build tool. As the name suggest, vitest's natural habitat is vite (which we are not currently using), but it runs without vite.

Like tsx it is a modern tool, built with support for typescript and ES modules builtin, and setup was a breeze. 

Just like tsx, it has a builtin watch mode. This mode is even the default: you run your test, then you correct our code and the test automatically reruns.

We adapted the test scripts to our package.json:

"scripts": {
"start": "tsx watch server.ts",
"debug": "tsx watch --inspect-brk server.ts",
"build": "tsc",
"test": "vitest",
"coverage" :
"vitest run --coverage "
}

Vitest's testing API is heavily inspired on, and compatible with Jest, easing migration. The only things we had to change, were some imports.

Node.js builtin Test Runner

The new Node builtin TestRrunner (node:test module), that is present in the Node.js 20 LTS, does not require you to add testing packages to your project. 
It does not have the same level of testing support as a specialised testing package like vitest, but it's feature set is surprisingly complete, but test coverage is still under an experimental flag. Here's a comparison.
Unfortunatly the test runner does not find *.ts files when looking for tests. You can work around this and they are also working on easier support for this in Node.js 21.

Conclusion: replace Jest with vitest

We'll certainly gives this another look when the next Node LTS is released, but as for now vitest is the way to go and we are happy with our tsx/vitest setup.

Firefox (124) adds in-browser PDF editing

 

22 March 2024

Java -> Kotlin equivalents

Sometimes I'm searching for equivalents of some Java constructs in Kotlin. So I bundled them in the correspondence table below:

JavaKotlin
String [] anArray;anArray:Array<String>
(castType)smart casting after is
as
enum.values()enum.entries
final String aConstantval aConstant
instanceOfis
int, double... (primitives)Int,Double...
public static void main(String[] args){}fun main(args:Array<String>{}
ObjectAny?
new Random()Random()
staticcompanion
top level function
const
switchwhen
ternaryVar==null?0:ternaryVarternaryVar?:0
throw UnsupportedOperationExceptionTODO()


1 March 2024

Stackoverflow to charge for AI usage of its Q&A data

Stackoverflow will charge AI companies for usage of its data and will require attribution back when its answers are used.

More info...

23 February 2024

Switching from ts-node to tsx

 Running server side typescript with ts-node

JavaScript is a fast changing ecosystem. With so many new and old kids on the block there  are so many options that having them play nicely together is a challenge.

We're working with node.js 18 at the server side and have moved to using TypeScript (instead of JavaScript) and ES modules (instead of CommonJS modules).

We're using ts-node to feed node TypeScript, and added extra options to our tsconfig.json for this:

{
"extends": "@tsconfig/node-lts/tsconfig.json",
"compilerOptions": {
"module": "ESNext",
},
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
}
}

We're using nodemon to restart node upon changes.  

This was giving us some problems when launching in debug mode, so we had to explictly pass the ES module loaders of node to nodemon. These are our commands in package.json

"scripts": {
"start": "nodemon index.ts",
"ts": "ts-node ",
"debug": "nodemon -x \"node --inspect --loader ts-node/esm \" "
}

For both npm run ts and npm run debug we're appending the ts file to be run.

We're testing with Jest and  again we needed some extra configuration to play nicely with Typescript and ES modules.

ts-node does not play nicely with Node.js 20

And then we moved to the Node.js 20 LTS release and things broke. It cannot even grok @tsconfig/node-lts/tsconfig.json (es2023 not supported).  You can get things going by also passing the esm loader when running the code normally, just as you do when you're debugging.

But really, not running with the LTS seemed a bit lame to me for a tool like ts-node. The 10.9 release dates from juli 2022, with only two bug fix releases since, and no sign of updates for new releases of underlying packages.

 Running server side typescript with tsx

So, we took a stab at tsx as an alternative for ts-node. If you look at the comparison between the two (by tsx, but still informational), you see that tsx has less downloads (6M/month vs 94M/month), has a larger footprint (10 MB vs 2MB), has less github stars (7k vs 12k).

Trying out tsx we could reduce tsconfig.json to sensible defaults

{
"extends": "@tsconfig/node-lts/tsconfig.json",
"compilerOptions": {
"outDir": "dist"
}
}

With this setup we are importing files with a .js extension, even if we are developing with .ts extensions. We need to avoid picking up previously generated .js files is compiling with tsc, so we are directing compiled files to a dist directory.

Annoying downside of tsx is that its name overlaps with the .tsx file extension (React typescript file), which gives you false positives when googling.

Monitoring changes

Also, we did not need nodemon anymore, as tsx comes with a watcher. Our scripts commands in package.json have simplified to

"scripts": {
"start": "tsx watch index.ts",
"ts": "tsx ",
"debug": "tsx --inspect-brk "
},

I have not tried out (Jest) testing yet, but as for now, tsx looks like the way to go!

22 January 2024

Nightshade: protecting your images against usage for AI trraining

Nightshade is a tool built at the University of Chicago that tries to pick the part of the image that is described in the associated text and then blurs its boundaries. In this way AI training software has a hard time to find the subject in the downloaded image.