forked from denoland/dotland
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
npm (unpkg) backed registry (denoland#25)
* Add np,m backed registry * Added tests * Fixed worker tests
- Loading branch information
1 parent
04de23d
commit 92bd1e4
Showing
11 changed files
with
230 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { npm, NPMEntry } from "./npm"; | ||
import "isomorphic-unfetch"; | ||
|
||
/* eslint-env jest */ | ||
|
||
const testEntry: NPMEntry = { | ||
type: "npm", | ||
desc: "A entry for testing", | ||
package: "example", | ||
}; | ||
|
||
test("source url", () => { | ||
expect(npm.getSourceURL(testEntry, "/index.js", "1.0.0")).toEqual( | ||
"https://unpkg.com/example@1.0.0/index.js" | ||
); | ||
}); | ||
|
||
test("source url with default version", () => { | ||
expect(npm.getSourceURL(testEntry, "/index.js", undefined)).toEqual( | ||
"https://unpkg.com/example@latest/index.js" | ||
); | ||
}); | ||
|
||
test("source url with empty path", () => { | ||
expect(npm.getSourceURL(testEntry, "", "1.0.0")).toEqual( | ||
"https://unpkg.com/example@1.0.0" | ||
); | ||
}); | ||
|
||
test("source url with subdirectory", () => { | ||
expect( | ||
npm.getSourceURL({ ...testEntry, path: "/test" }, "/index.js", "1.0.0") | ||
).toEqual("https://unpkg.com/example@1.0.0/test/index.js"); | ||
}); | ||
|
||
test("repo url", () => { | ||
expect(npm.getRepositoryURL(testEntry, "/index.js", "1.0.0")).toEqual( | ||
"https://unpkg.com/browse/example@1.0.0/index.js" | ||
); | ||
}); | ||
|
||
test("repo url with default version", () => { | ||
expect(npm.getRepositoryURL(testEntry, "/index.js", undefined)).toEqual( | ||
"https://unpkg.com/browse/example@latest/index.js" | ||
); | ||
}); | ||
|
||
test("repo url with empty path", () => { | ||
expect(npm.getRepositoryURL(testEntry, "", "1.0.0")).toEqual( | ||
"https://unpkg.com/browse/example@1.0.0/" | ||
); | ||
}); | ||
|
||
test("repo url with subdirectory", () => { | ||
expect( | ||
npm.getRepositoryURL({ ...testEntry, path: "/test" }, "/index.js", "1.0.0") | ||
).toEqual("https://unpkg.com/browse/example@1.0.0/test/index.js"); | ||
}); | ||
|
||
test("directory listing", async () => { | ||
expect(await npm.getDirectoryListing(testEntry, "", "0.0.0")).toEqual([ | ||
{ | ||
name: "package.json", | ||
size: 291, | ||
type: "file", | ||
}, | ||
{ | ||
name: "github.js", | ||
size: 886, | ||
type: "file", | ||
}, | ||
]); | ||
}); | ||
|
||
test("version list", async () => { | ||
expect(await npm.getVersionList(testEntry)).toEqual(["0.0.0"]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* Copyright 2020 the Deno authors. All rights reserved. MIT license. */ | ||
|
||
import { Entry, Registry, DirEntry } from "../registries"; | ||
|
||
export interface NPMEntry extends Entry { | ||
type: "npm"; | ||
package: string; | ||
path?: string; | ||
} | ||
|
||
export class NPMRegistry implements Registry<NPMEntry> { | ||
getSourceURL(entry: NPMEntry, path: string, version?: string): string { | ||
return `https://unpkg.com/${entry.package}@${version ?? "latest"}${ | ||
entry.path ?? "" | ||
}${path}`; | ||
} | ||
getRepositoryURL(entry: NPMEntry, path: string, version?: string): string { | ||
return `https://unpkg.com/browse/${entry.package}@${version ?? "latest"}${ | ||
entry.path ?? "" | ||
}${path || "/"}`; | ||
} | ||
async getDirectoryListing( | ||
entry: NPMEntry, | ||
path: string, | ||
version?: string | ||
): Promise<DirEntry[] | null> { | ||
const url = `https://unpkg.com/${entry.package}@${version ?? "latest"}${ | ||
entry.path ?? "" | ||
}${path}/?meta`; | ||
const res = await fetch(url, { | ||
headers: { | ||
accept: "application/json", | ||
}, | ||
}); | ||
if (res.status !== 200) { | ||
throw Error( | ||
`Got an error (${ | ||
res.status | ||
}) while querying unpkg:\n${await res.text()}` | ||
); | ||
} | ||
const data = await res.json(); | ||
if (data.type !== "directory") { | ||
return null; | ||
} | ||
const prefix = (data.path === "/" ? "" : data.path) + "/"; | ||
const files: DirEntry[] = data.files.map((file: any) => { | ||
return { | ||
name: file.path?.substring(prefix.length), | ||
type: file.type === "directory" ? "dir" : file.type, // "file" | "dir" | ||
size: file.size, // file only | ||
}; | ||
}); | ||
return files; | ||
} | ||
async getVersionList(entry: NPMEntry): Promise<string[] | null> { | ||
const url = `https://${ | ||
process ? "" : "cors-anywhere.herokuapp.com/" | ||
}registry.npmjs.org/${entry.package}`; | ||
const res = await fetch(url, { | ||
headers: { | ||
accept: "application/vnd.npm.install-v1+json", | ||
}, | ||
}); | ||
if (res.status !== 200) { | ||
throw Error( | ||
`Got an error (${ | ||
res.status | ||
}) while querying the NPM registry API:\n${await res.text()}` | ||
); | ||
} | ||
const data = await res.json(); | ||
const tags: string[] | undefined = data | ||
? Object.keys(data.versions).reverse() | ||
: []; | ||
return tags ?? null; | ||
} | ||
getDefaultVersion(_entry: NPMEntry): string { | ||
return "latest"; | ||
} | ||
} | ||
|
||
export const npm = new NPMRegistry(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.