# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Jose-Luis Rivas <ghostbar@riseup.net>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
#
# secfixes:
#   21.7.2-r0:
#     - CVE-2024-27982
#     - CVE-2024-27983
#   20.8.1-r0:
#     - CVE-2023-45143
#     - CVE-2023-39332
#     - CVE-2023-39331
#     - CVE-2023-38552
#     - CVE-2023-39333
#   18.9.1-r0:
#     - CVE-2022-32213
#     - CVE-2022-32215
#     - CVE-2022-32222
#     - CVE-2022-35255
#     - CVE-2022-35256
#   18.6.0-r0:
#     - CVE-2022-32212
#     - CVE-2022-32214
#     - CVE-2022-32222
#   17.3.1-r0:
#     - CVE-2021-44531
#     - CVE-2021-44532
#     - CVE-2021-44533
#     - CVE-2022-21824
#   16.11.1-r0:
#     - CVE-2021-22959
#     - CVE-2021-22960
#   16.6.2-r0:
#     - CVE-2021-3672
#     - CVE-2021-22931
#     - CVE-2021-22939
#   16.6.0-r0:
#     - CVE-2021-22930
#   15.10.0-r0:
#     - CVE-2021-22883
#     - CVE-2021-22884
#   15.5.1-r0:
#     - CVE-2020-8265
#     - CVE-2020-8287
#   15.3.0-r0:
#     - CVE-2020-8277
#   14.11.0-r0:
#     - CVE-2020-8201
#     - CVE-2020-8251
#   14.4.0-r0:
#     - CVE-2020-8172
#     - CVE-2020-11080
#     - CVE-2020-8174
#   13.11.0-r0:
#     - CVE-2019-15604
#     - CVE-2019-15605
#     - CVE-2019-15606
#   11.10.1-r0:
#     - CVE-2019-5737
#   11.3.0-r0:
#     - CVE-2018-12121
#     - CVE-2018-12122
#     - CVE-2018-12123
#     - CVE-2018-0735
#     - CVE-2018-0734
#   9.10.0-r0:
#     - CVE-2018-7158
#     - CVE-2018-7159
#     - CVE-2018-7160
#   9.2.1-r0:
#     - CVE-2017-15896
#     - CVE-2017-15897
#   0:
#     - CVE-2023-44487

pkgname=nodejs-current
# The current stable version, i.e. non-LTS.
pkgver=22.8.0
pkgrel=0
pkgdesc="JavaScript runtime built on V8 engine - current stable version"
url="https://nodejs.org/"
arch="all"
license="MIT"
depends="ca-certificates"
depends_dev="
	libuv-dev
	"
makedepends="
	$depends_dev
	ada-dev
	brotli-dev
	c-ares-dev
	icu-dev
	linux-headers
	nghttp2-dev
	openssl-dev>3
	python3
	samurai
	zlib-dev
	"
subpackages="$pkgname-dev $pkgname-doc"
provides="nodejs"
provider_priority=10
replaces="nodejs nodejs-lts"  # nodejs-lts for backward compatibility
source="https://nodejs.org/dist/v$pkgver/node-v$pkgver.tar.xz
	dont-run-gyp-files-for-bundled-deps.patch
	elf.patch
	unbundle-uv.patch
	39739.patch
	google-sucks.patch
	riscv-trap-handler.patch
	"
builddir="$srcdir/node-v$pkgver"

build() {
	# Os overwrites the optimizations in BUILDTYPE=Release
	# increases binary size by ~10% (53.1 MiB -> 58.6 MiB),
	# but also increases performance by ~20%  according to
	# v8/web-tooling-benchmark. Node.js is quite huge anyway;
	# there are better options for size constrained environments.
	export CFLAGS="${CFLAGS/-Os/}"
	export CXXFLAGS="${CXXFLAGS/-Os/} -Wno-deprecated-declarations"

	./configure --prefix=/usr \
		--shared-brotli \
		--shared-zlib \
		--shared-libuv \
		--shared-openssl \
		--shared-cares \
		--shared-nghttp2 \
		--ninja \
		--with-intl=system-icu \
		--without-npm \
		--openssl-use-def-ca-store

	make BUILDTYPE=Release
}

# TODO Run provided test suite.
check() {
	cd "$builddir"/out/Release

	./node -e 'console.log("Hello, world!")'
	./node -e "require('assert').equal(process.versions.node, '$pkgver')"
	./node -e 'console.log("Openssl:", process.versions.openssl)'
}

package() {
	make DESTDIR="$pkgdir" install
}

sha512sums="
c2942c65dc9909b1f412489a54d16afae7be46a5c2a8bd292cde75e4c56ec763ccee6572fd72774c3f9d94796cfff6edbd9e93976893873cc182548225ac162a  node-v22.8.0.tar.xz
7ad1119ca0788c4a7b3312c8f46ae4bfb661fd38ece8894f5e3117bc30eb3ad99ab9ac3b63d78313bbb79b5973d5bcf7337e5830563e56314b599e4ed1a24457  dont-run-gyp-files-for-bundled-deps.patch
e6f2907bdf2082097841f44ac9380d5b1bc65eca39be7153da081266940c6a10d2932e16a2e15bdcf12fec61c45549d009d1a07e5e1633603172968b5f6fdb77  elf.patch
d8f1092a92f7a168755020b3e40fe72b743f9150de6d18f7664074dadaef866ecb4fa8252fa891fd7cf34b3dc183455af8d5570d7b3f1883464c2b6e3a9f2789  unbundle-uv.patch
080ede859262f5507a913b1f9995092ed075777e94c73c23ee74d74c739e9b2e6b4cc3afad189a333692f10485e7e3e12a15662286df2c946f73bdd6083baee8  39739.patch
31c8cd4a14f98ed7935f7ad44270cbbcd37a3ed871e03a2ca97b6a478cf9de3a44e57e75ab7605cf987c0eea71f8d62841a6ba71df62ddf5a682b801e041e401  google-sucks.patch
177082592adaf3c92267afa1db6535b9fc8d7fdd31dc5932086f3249d3213f3af91d350bb290d682efff5a41a985a0b09c0f49084aca7a30e8b28cc2b368b59c  riscv-trap-handler.patch
"
