Skip to content

Commit

Permalink
Fix setImmediate polyfill (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Dec 29, 2024
1 parent f5c8f83 commit 577f317
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 72 deletions.
58 changes: 10 additions & 48 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type Ref struct {
importers *set.Set[string]
}

var (
regexpESMInternalIdent = regexp.MustCompile(`__[a-zA-Z]+\$`)
regexpVarDecl = regexp.MustCompile(`var ([\w$]+)\s*=\s*[\w$]+$`)
)

var loaders = map[string]esbuild.Loader{
".js": esbuild.LoaderJS,
".mjs": esbuild.LoaderJS,
Expand Down Expand Up @@ -949,6 +954,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
}
for k, v := range define {
define["global."+k] = v
define["globalThis."+k] = v
}
define["global"] = "globalThis"
}
Expand Down Expand Up @@ -1156,56 +1162,12 @@ REBUILD:
}
}
if ids.Has("__setImmediate$") {
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')
}
header.WriteString(`var __setImmediate$ = (cb, ...args) => ( { $t: setTimeout(cb, 0, ...args), [Symbol.dispose](){ clearTimeout(this.t) } });`)
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')
}
header.WriteString(`var __clearImmediate$ = i => clearTimeout(i.$t);`)
header.WriteByte('\n')
}
if ids.Has("__rResolve$") {
header.WriteString(`var __rResolve$ = p => p;`)
Expand Down
26 changes: 22 additions & 4 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"os"
"path"
"regexp"
"sort"
"strings"
"syscall"
Expand Down Expand Up @@ -44,6 +45,23 @@ const (
RawFile
)

const (
ccMustRevalidate = "public, max-age=0, must-revalidate"
ccOneDay = "public, max-age=86400"
ccImmutable = "public, max-age=31536000, immutable"
ctHTML = "text/html; charset=utf-8"
ctCSS = "text/css; charset=utf-8"
ctJSON = "application/json; charset=utf-8"
ctJavaScript = "application/javascript; charset=utf-8"
ctTypeScript = "application/typescript; charset=utf-8"
)

var (
regexpVersion = regexp.MustCompile(`^[\w\+\-\.]+$`)
regexpVersionStrict = regexp.MustCompile(`^\d+\.\d+\.\d+(-[\w\+\-\.]+)?$`)
regexpJSIdent = regexp.MustCompile(`^[a-zA-Z_$][\w$]*$`)
)

func esmRouter() rex.Handle {
var (
startTime = time.Now()
Expand Down Expand Up @@ -263,7 +281,7 @@ func esmRouter() rex.Handle {
if err != nil {
return rex.Status(500, err.Error())
}
ctx.Header.Set("Content-Type", ctHtml)
ctx.Header.Set("Content-Type", ctHTML)
ctx.Header.Set("Cache-Control", ccMustRevalidate)
ctx.Header.Set("Etag", globalETag)
return indexHTML
Expand Down Expand Up @@ -397,7 +415,7 @@ func esmRouter() rex.Handle {
if DEBUG {
ctx.Header.Set("Cache-Control", ccMustRevalidate)
} else {
ctx.Header.Set("Cache-Control", cc1day)
ctx.Header.Set("Cache-Control", ccOneDay)
}
ctx.Header.Set("Etag", globalETag)
if targetFromUA {
Expand Down Expand Up @@ -448,7 +466,7 @@ func esmRouter() rex.Handle {
if ifNoneMatch == globalETag && !DEBUG {
return rex.Status(http.StatusNotModified, nil)
}
ctx.Header.Set("Cache-Control", cc1day)
ctx.Header.Set("Cache-Control", ccOneDay)
ctx.Header.Set("Etag", globalETag)
}
ctx.Header.Set("Content-Type", ctJavaScript)
Expand All @@ -469,7 +487,7 @@ func esmRouter() rex.Handle {
return rex.Status(http.StatusNotModified, nil)
}
ctx.Header.Set("Etag", etag)
ctx.Header.Set("Cache-Control", cc1day)
ctx.Header.Set("Cache-Control", ccOneDay)
}
contentType := common.ContentType(pathname)
if contentType != "" {
Expand Down
20 changes: 0 additions & 20 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/signal"
"path"
"regexp"
"strings"
"syscall"
"time"
Expand All @@ -19,25 +18,6 @@ import (
"github.com/ije/rex"
)

const (
cc1day = "public, max-age=86400"
ccMustRevalidate = "public, max-age=0, must-revalidate"
ccImmutable = "public, max-age=31536000, immutable"
ctJavaScript = "application/javascript; charset=utf-8"
ctTypeScript = "application/typescript; charset=utf-8"
ctJSON = "application/json; charset=utf-8"
ctCSS = "text/css; charset=utf-8"
ctHtml = "text/html; charset=utf-8"
)

var (
regexpVersion = regexp.MustCompile(`^[\w\+\-\.]+$`)
regexpVersionStrict = regexp.MustCompile(`^\d+\.\d+\.\d+(-[\w\+\-\.]+)?$`)
regexpJSIdent = regexp.MustCompile(`^[a-zA-Z_$][\w$]*$`)
regexpESMInternalIdent = regexp.MustCompile(`__[a-zA-Z]+\$`)
regexpVarDecl = regexp.MustCompile(`var ([\w$]+)\s*=\s*[\w$]+$`)
)

var (
log *logx.Logger
buildQueue *BuildQueue
Expand Down

0 comments on commit 577f317

Please sign in to comment.