forked from microsoft/TypeScript-Website
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
442 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ATABootstrapConfig } from "." | ||
|
||
// https://github.com/jsdelivr/data.jsdelivr.com | ||
|
||
export const getNPMVersionsForModule = (config: ATABootstrapConfig, moduleName: string) => { | ||
const url = `https://data.jsdelivr.com/v1/package/npm/${moduleName}` | ||
return api<{ tags: Record<string, string>; versions: string[] }>(config, url) | ||
} | ||
|
||
export const getNPMVersionForModuleReference = (config: ATABootstrapConfig, moduleName: string, reference: string) => { | ||
const url = `https://data.jsdelivr.com/v1/package/resolve/npm/${moduleName}@${reference}` | ||
return api<{ version: string | null }>(config, url) | ||
} | ||
|
||
export type NPMTreeMeta = { default: string; files: Array<{ name: string }>; moduleName: string; version: string } | ||
|
||
export const getFiletreeForModuleWithVersion = async ( | ||
config: ATABootstrapConfig, | ||
moduleName: string, | ||
version: string | ||
) => { | ||
const url = `https://data.jsdelivr.com/v1/package/npm/${moduleName}@${version}/flat` | ||
const res = await api<NPMTreeMeta>(config, url) | ||
if (res instanceof Error) { | ||
return res | ||
} else { | ||
return { | ||
...res, | ||
moduleName, | ||
version, | ||
} | ||
} | ||
} | ||
|
||
export const getDTSFileForModuleWithVersion = async ( | ||
config: ATABootstrapConfig, | ||
moduleName: string, | ||
version: string, | ||
file: string | ||
) => { | ||
// file has a prefix / in falr mode | ||
const url = `https://cdn.jsdelivr.net/npm/${moduleName}@${version}${file}` | ||
const f = config.fetcher || fetch | ||
const res = await f(url, { headers: { "User-Agent": `Type Acquisition ${config.projectName}` } }) | ||
if (res.ok) { | ||
return res.text() | ||
} else { | ||
return new Error("OK") | ||
} | ||
} | ||
|
||
function api<T>(config: ATABootstrapConfig, url: string): Promise<T | Error> { | ||
const f = config.fetcher || fetch | ||
return f(url, { headers: { "User-Agent": `Type Acquisition ${config.projectName}` } }).then(res => { | ||
if (res.ok) { | ||
return res.json().then(f => f as T) | ||
} else { | ||
return new Error("OK") | ||
} | ||
}) | ||
} |
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,65 @@ | ||
/** Converts some of the known global imports to node so that we grab the right info */ | ||
export const mapModuleNameToModule = (name: string) => { | ||
// in node repl: | ||
// > require("module").builtinModules | ||
const builtInNodeMods = [ | ||
"assert", | ||
"assert/strict", | ||
"async_hooks", | ||
"buffer", | ||
"child_process", | ||
"cluster", | ||
"console", | ||
"constants", | ||
"crypto", | ||
"dgram", | ||
"diagnostics_channel", | ||
"dns", | ||
"dns/promises", | ||
"domain", | ||
"events", | ||
"fs", | ||
"fs/promises", | ||
"http", | ||
"http2", | ||
"https", | ||
"inspector", | ||
"module", | ||
"net", | ||
"os", | ||
"path", | ||
"path/posix", | ||
"path/win32", | ||
"perf_hooks", | ||
"process", | ||
"punycode", | ||
"querystring", | ||
"readline", | ||
"repl", | ||
"stream", | ||
"stream/promises", | ||
"stream/consumers", | ||
"stream/web", | ||
"string_decoder", | ||
"sys", | ||
"timers", | ||
"timers/promises", | ||
"tls", | ||
"trace_events", | ||
"tty", | ||
"url", | ||
"util", | ||
"util/types", | ||
"v8", | ||
"vm", | ||
"wasi", | ||
"worker_threads", | ||
"zlib", | ||
] | ||
|
||
if (builtInNodeMods.includes(name.replace("node:", ""))) { | ||
return "node" | ||
} | ||
|
||
return name | ||
} |
Oops, something went wrong.