Skip to content

Commit

Permalink
Refactor embed FS build (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Dec 29, 2024
1 parent 8deff62 commit 0a754ce
Show file tree
Hide file tree
Showing 182 changed files with 191 additions and 135 deletions.
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package main

import (
"embed"

"github.com/esm-dev/esm.sh/server"
)

//go:embed README.md
//go:embed server/embed
var fs embed.FS

func main() {
server.Serve(&fs)
server.Serve()
}
64 changes: 56 additions & 8 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"strconv"
"strings"

npm_replacements "github.com/esm-dev/esm.sh/server/npm-replacements"
"github.com/esm-dev/esm.sh/server/npm_replacements"
"github.com/esm-dev/esm.sh/server/storage"
esbuild "github.com/evanw/esbuild/pkg/api"
"github.com/ije/gox/set"
Expand Down Expand Up @@ -911,7 +911,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
"Buffer": "__Buffer$",
"process": "__Process$",
"setImmediate": "__setImmediate$",
"clearImmediate": "clearTimeout",
"clearImmediate": "__clearImmediate$",
"require.resolve": "__rResolve$",
"process.env.NODE_ENV": fmt.Sprintf(`"%s"`, nodeEnv),
}
Expand Down Expand Up @@ -1141,18 +1141,66 @@ REBUILD:
header.WriteByte('\n')
}
}
if ids.Has("__global$") {
header.WriteString(`var __global$ = globalThis || (typeof window !== "undefined" ? window : self);`)
header.WriteByte('\n')
}
if ids.Has("__setImmediate$") {
header.WriteString(`var __setImmediate$ = (cb, ...args) => setTimeout(cb, 0, ...args);`)
header.WriteByte('\n')
if ctx.args.external.Has("node:timers") {
header.WriteString(`import { setImmediate as __setImmediate$ } from "node:timers";`)
header.WriteByte('\n')
} else if ctx.isBrowserTarget() {
var excluded bool
if len(ctx.pkgJson.Browser) > 0 {
if name, ok := ctx.pkgJson.Browser["timers"]; ok {
excluded = name == ""
} else if name, ok := ctx.pkgJson.Browser["node:timers"]; ok {
excluded = name == ""
}
}
if !excluded {
header.WriteString(`import { setImmediate as __setImmediate$ } from "/node/timers.mjs";`)
header.WriteByte('\n')
imports.Add("/node/timers.mjs")
}
} else if ctx.target == "denonext" {
header.WriteString(`import { setImmediate as __setImmediate$ } from "node:timers";`)
header.WriteByte('\n')
} else if ctx.target == "deno" {
header.WriteString(`import { setImmediate as __setImmediate$ } from "https://deno.land/[email protected]/node/timers.ts";`)
header.WriteByte('\n')
}
}
if ids.Has("__clearImmediate$") {
if ctx.args.external.Has("node:timers") {
header.WriteString(`import { clearImmediate as __clearImmediate$ } from "node:timers";`)
header.WriteByte('\n')
} else if ctx.isBrowserTarget() {
var excluded bool
if len(ctx.pkgJson.Browser) > 0 {
if name, ok := ctx.pkgJson.Browser["timers"]; ok {
excluded = name == ""
} else if name, ok := ctx.pkgJson.Browser["node:timers"]; ok {
excluded = name == ""
}
}
if !excluded {
header.WriteString(`import { clearImmediate as __clearImmediate$ } from "/node/timers.mjs";`)
header.WriteByte('\n')
imports.Add("/node/timers.mjs")
}
} else if ctx.target == "denonext" {
header.WriteString(`import { clearImmediate as __clearImmediate$ } from "node:timers";`)
header.WriteByte('\n')
} else if ctx.target == "deno" {
header.WriteString(`import { clearImmediate as __clearImmediate$ } from "https://deno.land/[email protected]/node/timers.ts";`)
header.WriteByte('\n')
}
}
if ids.Has("__rResolve$") {
header.WriteString(`var __rResolve$ = p => p;`)
header.WriteByte('\n')
}
if ids.Has("__global$") {
header.WriteString(`var __global$ = globalThis || (typeof window !== "undefined" ? window : self);`)
header.WriteByte('\n')
}
}

// apply cjs requires
Expand Down
30 changes: 18 additions & 12 deletions server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ type cacheItem struct {

func withCache[T any](key string, cacheTtl time.Duration, fetch func() (T, string, error)) (data T, err error) {
// check cache first
if v, ok := cacheStore.Load(key); ok {
item := v.(*cacheItem)
if item.exp.After(time.Now()) {
return item.data.(T), nil
if cacheTtl > time.Second {
if v, ok := cacheStore.Load(key); ok {
item := v.(*cacheItem)
if item.exp.After(time.Now()) {
return item.data.(T), nil
}
}
}

unlock := cacheMutex.Lock(key)
defer unlock()

// check cache again after lock
if v, ok := cacheStore.Load(key); ok {
item := v.(*cacheItem)
if item.exp.After(time.Now()) {
return item.data.(T), nil
if cacheTtl > time.Second {
if v, ok := cacheStore.Load(key); ok {
item := v.(*cacheItem)
if item.exp.After(time.Now()) {
return item.data.(T), nil
}
}
}

Expand All @@ -51,10 +55,12 @@ func withCache[T any](key string, cacheTtl time.Duration, fetch func() (T, strin
return
}

exp := time.Now().Add(cacheTtl)
cacheStore.Store(key, &cacheItem{exp, data})
if aliasKey != "" && aliasKey != key {
cacheStore.Store(aliasKey, &cacheItem{exp, data})
if cacheTtl > time.Second {
exp := time.Now().Add(cacheTtl)
cacheStore.Store(key, &cacheItem{exp, data})
if aliasKey != "" && aliasKey != key {
cacheStore.Store(aliasKey, &cacheItem{exp, data})
}
}
return
}
Expand Down
25 changes: 22 additions & 3 deletions server/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@

package server

const (
DEBUG = true
VERSION = "DEV"
import (
"os"
"path"
)

// debug mode
const DEBUG = true

// always "DEV" in DEBUG mode
const VERSION = "DEV"

// mock embed.FS reads files from the current working directory in DEBUG mode
var embedFS MockEmbedFS

type MockEmbedFS struct{}

func (fs MockEmbedFS) ReadFile(name string) ([]byte, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
return os.ReadFile(path.Join(cwd, "server", name))
}
58 changes: 0 additions & 58 deletions server/embed.go

This file was deleted.

Binary file modified server/embed/node-runtime.tgz
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 8 additions & 2 deletions server/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

package server

const (
DEBUG = false
import (
"embed"
)

// production mode
const DEBUG = false

// may be changed by `-ldflags`
var VERSION = "v136"

//go:embed embed
var embedFS embed.FS
Loading

0 comments on commit 0a754ce

Please sign in to comment.