From bf5fa0520b5471a61eb2d6d4e4b0541acf8da393 Mon Sep 17 00:00:00 2001 From: X Date: Wed, 3 May 2023 17:44:56 +0800 Subject: [PATCH] Fixing type only packages --- server/build.go | 4 ++-- server/build_helpers.go | 7 ++++--- server/dts_transform.go | 26 ++++++++++++++++---------- server/server_handler.go | 26 ++++++++++++++++---------- test/_issue-433/issue-433.test.ts | 13 +++++++++++++ 5 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 test/_issue-433/issue-433.test.ts diff --git a/server/build.go b/server/build.go index a9a29e697..8e6af9738 100644 --- a/server/build.go +++ b/server/build.go @@ -115,7 +115,6 @@ 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 @@ -123,7 +122,7 @@ func (task *BuildTask) build() (esm *ESMBuild, err error) { 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 @@ -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) diff --git a/server/build_helpers.go b/server/build_helpers.go index c8c89ecca..c0297b65b 100644 --- a/server/build_helpers.go +++ b/server/build_helpers.go @@ -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) } diff --git a/server/dts_transform.go b/server/dts_transform.go index 54f430f19..ac17e9b84 100644 --- a/server/dts_transform.go +++ b/server/dts_transform.go @@ -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 } @@ -67,11 +67,11 @@ 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 }) @@ -79,14 +79,20 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker * 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() @@ -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"} } diff --git a/server/server_handler.go b/server/server_handler.go index 5688a9a5a..1dcdf01d6 100644 --- a/server/server_handler.go +++ b/server/server_handler.go @@ -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") } diff --git a/test/_issue-433/issue-433.test.ts b/test/_issue-433/issue-433.test.ts new file mode 100644 index 000000000..bb1d60685 --- /dev/null +++ b/test/_issue-433/issue-433.test.ts @@ -0,0 +1,13 @@ +import { assert } from "https://deno.land/std@0.180.0/testing/asserts.ts"; + +import { Octokit } from "http://localhost:8080/@octokit-next/core@2.5.0"; +import "http://localhost:8080/@octokit-next/types-rest-api@2.5.0"; + +Deno.test("issue #433", async () => { + const octokit = new Octokit(); + + const { data } = await octokit.request("GET /"); + + // should be typed as string + data.current_user_url; +});