Skip to content

Commit

Permalink
fix #1030: work around a bug in go's mime types
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 23, 2021
1 parent 03f176c commit c8eb58f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fix incorrect MIME types on Windows ([#1030](https://github.com/evanw/esbuild/issues/1030))

The web server built into esbuild uses the file extension to determine the value of the `Content-Type` header. This was previously done using the `mime.TypeByExtension()` function from Go's standard library. However, this function is apparently broken on Windows because installed programs can change MIME types in the Windows registry: [golang/go#32350](https://github.com/golang/go/issues/32350). This release fixes the problem by using a copy of Go's `mime.TypeByExtension()` function without the part that reads from the Windows registry.

* Using a top-level return inside an ECMAScript module is now forbidden

The CommonJS module format is implemented as an anonymous function wrapper, so technically you can use a top-level `return` statement and it will actually work. Some packages in the wild use this to exit early from module initialization, so esbuild supports this. However, the ECMAScript module format doesn't allow top-level returns. With this release, esbuild no longer allows top-level returns in ECMAScript modules.
Expand Down
31 changes: 29 additions & 2 deletions pkg/api/serve_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package api

import (
"fmt"
"mime"
"net"
"net/http"
"path"
Expand Down Expand Up @@ -111,6 +110,34 @@ func errorsToString(errors []Message) string {
return sb.String()
}

var builtinTypesLower = map[string]string{
".css": "text/css; charset=utf-8",
".gif": "image/gif",
".htm": "text/html; charset=utf-8",
".html": "text/html; charset=utf-8",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".js": "text/javascript; charset=utf-8",
".json": "application/json",
".mjs": "text/javascript; charset=utf-8",
".pdf": "application/pdf",
".png": "image/png",
".svg": "image/svg+xml",
".wasm": "application/wasm",
".webp": "image/webp",
".xml": "text/xml; charset=utf-8",
}

// This is used instead of Go's built-in "mime.TypeByExtension" function because
// this function is broken on Windows: https://github.com/golang/go/issues/32350.
func mimeTypeByExtension(ext string) string {
contentType := builtinTypesLower[ext]
if contentType == "" {
contentType = builtinTypesLower[strings.ToLower(ext)]
}
return contentType
}

func (h *apiHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
start := time.Now()

Expand Down Expand Up @@ -233,7 +260,7 @@ func (h *apiHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {

// Serve a file
if kind == fs.FileEntry {
if contentType := mime.TypeByExtension(path.Ext(queryPath)); contentType != "" {
if contentType := mimeTypeByExtension(path.Ext(queryPath)); contentType != "" {
res.Header().Set("Content-Type", contentType)
}

Expand Down

0 comments on commit c8eb58f

Please sign in to comment.