Skip to content

Commit

Permalink
Fixing type only packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed May 3, 2023
1 parent b158ef4 commit bf5fa05
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
4 changes: 2 additions & 2 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,14 @@ func (task *BuildTask) build() (esm *ESMBuild, err error) {
if task.Target == "types" {
if npm.Types != "" {
dts := npm.Name + "@" + npm.Version + path.Join("/", npm.Types)
task.stage = "transform-dts"
task.buildDTS(dts)
}
return
}

if esm.TypesOnly {
dts := npm.Name + "@" + npm.Version + path.Join("/", npm.Types)
task.stage = "transform-dts"
esm.Dts = fmt.Sprintf("/v%d%s/%s", task.BuildVersion, task.ghPrefix(), dts)
task.buildDTS(dts)
task.storeToDB(esm)
return
Expand Down Expand Up @@ -1095,6 +1094,7 @@ func (task *BuildTask) checkDTS(esm *ESMBuild, npm NpmPackage) {

func (task *BuildTask) buildDTS(dts string) {
start := time.Now()
task.stage = "transform-dts"
n, err := task.TransformDTS(dts)
if err != nil && os.IsExist(err) {
log.Errorf("TransformDTS(%s): %v", dts, err)
Expand Down
7 changes: 4 additions & 3 deletions server/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,13 @@ func queryESMBuild(id string) (*ESMBuild, bool) {
if strings.HasPrefix(id, "stable/") {
id = fmt.Sprintf("v%d/", STABLE_VERSION) + strings.TrimPrefix(id, "stable/")
}
_, err = fs.Stat(path.Join("builds", id))
if err == nil {
if !esm.TypesOnly {
_, err = fs.Stat(path.Join("builds", id))
}
if err == nil || os.IsExist(err) {
return &esm, true
}
}

// delete the invalid db entry
db.Delete(id)
}
Expand Down
26 changes: 16 additions & 10 deletions server/dts_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *
}
marker.Add(aliasDepsPrefix + dts)

var taskPkgInfo NpmPackage
taskPkgJsonPath := path.Join(task.wd, "node_modules", task.Pkg.Name, "package.json")
err = utils.ParseJSONFile(taskPkgJsonPath, &taskPkgInfo)
var pkgInfo NpmPackage
pkgJsonPath := path.Join(task.wd, "node_modules", task.Pkg.Name, "package.json")
err = utils.ParseJSONFile(pkgJsonPath, &pkgInfo)
if err != nil {
return
}
Expand Down Expand Up @@ -67,26 +67,32 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *
}
defer dtsFile.Close()

internalDeclModules := newStringSet()
allDeclModules := newStringSet()
pass1Buf := bytes.NewBuffer(nil)
err = walkDts(dtsFile, pass1Buf, func(name string, kind string, position int) string {
if kind == "declareModule" {
internalDeclModules.Add(name)
allDeclModules.Add(name)
}
return name
})
if err != nil {
return
}

for _, path := range internalDeclModules.Values() {
internalDeclModules := newStringSet()
for _, path := range allDeclModules.Values() {
if pkgName == "@types/node" {
if strings.HasPrefix(path, "node:") {
internalDeclModules.Remove(path)
continue
}
} else if _, ok := pkgInfo.Dependencies[path]; ok {
continue
} else if _, ok := pkgInfo.PeerDependencies[path]; ok {
continue
} else if path == pkgName || strings.HasPrefix(path, pkgName+"/") {
internalDeclModules.Remove(path)
continue
}
internalDeclModules.Add(path)
}

wd := task.getRealWD()
Expand Down Expand Up @@ -181,9 +187,9 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *

depTypePkgName, _ := splitPkgPath(importPath)
maybeVersion := []string{"latest"}
if v, ok := taskPkgInfo.Dependencies[depTypePkgName]; ok {
if v, ok := pkgInfo.Dependencies[depTypePkgName]; ok {
maybeVersion = []string{v, "latest"}
} else if v, ok := taskPkgInfo.PeerDependencies[depTypePkgName]; ok {
} else if v, ok := pkgInfo.PeerDependencies[depTypePkgName]; ok {
maybeVersion = []string{v, "latest"}
}

Expand Down
26 changes: 16 additions & 10 deletions server/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,17 +1020,23 @@ func getHandler() rex.Handle {

// should redirect to `*.d.ts` file
if esm.TypesOnly {
if esm.Dts != "" && !noCheck {
value := fmt.Sprintf(
"%s%s/%s",
cdnOrigin,
cfg.BasePath,
strings.TrimPrefix(esm.Dts, "/"),
)
ctx.SetHeader("X-TypeScript-Types", value)
}
ctx.SetHeader("Cache-Control", "private, no-store, no-cache, must-revalidate")
dtsUrl := fmt.Sprintf(
"%s%s/%s",
cdnOrigin,
cfg.BasePath,
strings.TrimPrefix(esm.Dts, "/"),
)
ctx.SetHeader("X-TypeScript-Types", dtsUrl)
ctx.SetHeader("Content-Type", "application/javascript; charset=utf-8")
if fallback {
ctx.SetHeader("Cache-Control", "private, no-store, no-cache, must-revalidate")
} else {
if isPined {
ctx.SetHeader("Cache-Control", "public, max-age=31536000, immutable")
} else {
ctx.SetHeader("Cache-Control", fmt.Sprintf("public, max-age=%d", 24*3600)) // cache for 24 hours
}
}
return []byte("export default null;\n")
}

Expand Down
13 changes: 13 additions & 0 deletions test/_issue-433/issue-433.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";

import { Octokit } from "http://localhost:8080/@octokit-next/[email protected]";
import "http://localhost:8080/@octokit-next/[email protected]";

Deno.test("issue #433", async () => {
const octokit = new Octokit();

const { data } = await octokit.request("GET /");

// should be typed as string
data.current_user_url;
});

0 comments on commit bf5fa05

Please sign in to comment.