Skip to content

Commit

Permalink
Use nodejs 22
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed May 4, 2024
1 parent d5e8998 commit 0a4b859
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20
node-version: 22

- name: Install pnpm
uses: pnpm/action-setup@v3
with:
version: 8
version: 9
run_install: false

- name: Get pnpm store directory
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/esm-dev/esm.sh:latest,ghcr.io/esm-dev/esm.sh:v135_1
tags: ghcr.io/esm-dev/esm.sh:latest,ghcr.io/esm-dev/esm.sh:v136
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Build Stage
FROM golang:1.22-alpine AS build-stage

ENV ESM_SH_VERSION v135_1
ENV ESM_SH_VERSION v136
ENV ESM_SH_GIT_URL https://github.com/esm-dev/esm.sh

RUN apk update && apk add --no-cache git
Expand All @@ -11,7 +11,7 @@ WORKDIR /tmp/esm.sh
RUN CGO_ENABLED=0 GOOS=linux go build -o esmd main.go

# Release Stage
FROM node:20-alpine AS release-stage
FROM node:22-alpine AS release-stage

RUN apk update && apk add --no-cache git libcap-utils
RUN npm i -g pnpm
Expand Down
5 changes: 5 additions & 0 deletions server/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ var nativeNodePackages = []string{
"zlib-sync",
}

// unsupported node modules for `denonext` target
var denoNextUnspportedNodeModules = map[string]bool{
"inspector": true,
}

// force to use `npm:` specifier for `denonext` target to fix `createRequire` issue
var forceNpmSpecifiers = map[string]bool{
"css-tree": true,
Expand Down
45 changes: 33 additions & 12 deletions server/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (
)

const (
nodejsMinVersion = 20
nodejsLatestLTS = "20.11.1"
nodeTypesVersion = "20.11.20"
nodejsMinVersion = 22
nodeTypesVersion = "20.12.8"
denoStdVersion = "0.177.1"
)

Expand Down Expand Up @@ -76,27 +75,28 @@ var nodejsInternalModules = map[string]bool{
"zlib": true,
}

var denoNextUnspportedNodeModules = map[string]bool{
"inspector": true,
}

func checkNodejs(installDir string) (nodeVersion string, pnpmVersion string, err error) {
nodeVersion, major, err := getNodejsVersion()
usingSystemNodejs := err == nil && major >= nodejsMinVersion
useSystemNodejs := err == nil && major >= nodejsMinVersion

if !usingSystemNodejs {
if !useSystemNodejs {
PATH := os.Getenv("PATH")
nodeBinDir := path.Join(installDir, "bin")
if !strings.Contains(PATH, nodeBinDir) {
os.Setenv("PATH", fmt.Sprintf("%s%c%s", nodeBinDir, os.PathListSeparator, PATH))
}
nodeVersion, major, err = getNodejsVersion()
if err != nil || major < nodejsMinVersion {
err = installNodejs(installDir, nodejsLatestLTS)
var latestVersion string
latestVersion, err = getNodejsLatestVersion()
if err != nil {
return
}
err = installNodejs(installDir, latestVersion)
if err != nil {
return
}
log.Infof("nodejs %s installed", nodejsLatestLTS)
log.Infof("nodejs %s installed", latestVersion)
}
nodeVersion, major, err = getNodejsVersion()
}
Expand Down Expand Up @@ -134,6 +134,27 @@ func getNodejsVersion() (version string, major int, err error) {
return
}

func getNodejsLatestVersion() (verison string, err error) {
var res *http.Response
res, err = http.Get(fmt.Sprintf("https://nodejs.org/download/release/latest-v%d.x/", nodejsMinVersion))
if err != nil {
return
}
defer res.Body.Close()
var body []byte
body, err = io.ReadAll(res.Body)
if err != nil {
return
}
i := strings.Index(string(body), fmt.Sprintf("node-v%d.", nodejsMinVersion))
if i < 0 {
err = fmt.Errorf("no nodejs version found")
return
}
verison, _ = utils.SplitByFirstByte(string(body[i+5:]), '-')
return
}

func installNodejs(installDir string, version string) (err error) {
arch := runtime.GOARCH
switch arch {
Expand All @@ -142,7 +163,7 @@ func installNodejs(installDir string, version string) (err error) {
case "386":
arch = "x86"
}
dlURL := fmt.Sprintf("https://nodejs.org/dist/v%s/node-v%s-%s-%s.tar.xz", version, version, runtime.GOOS, arch)
dlURL := fmt.Sprintf("https://nodejs.org/dist/%s/node-%s-%s-%s.tar.xz", version, version, runtime.GOOS, arch)
resp, err := http.Get(dlURL)
if err != nil {
err = fmt.Errorf("download nodejs: %v", err)
Expand Down
17 changes: 17 additions & 0 deletions server/nodejs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package server

import (
"fmt"
"strings"
"testing"
)

func TestGetNodejsLatestVersion(t *testing.T) {
version, err := getNodejsLatestVersion()
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(version, fmt.Sprintf("v%d.", nodejsMinVersion)) {
t.Fatalf("bad version %s", version)
}
}

0 comments on commit 0a4b859

Please sign in to comment.