Skip to content

Commit

Permalink
Adds the prod URL
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Sep 24, 2021
1 parent 1f00600 commit cbfcdd3
Show file tree
Hide file tree
Showing 5 changed files with 442 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/playground/src/navigation.ts
Expand Up @@ -83,7 +83,9 @@ export const gistPoweredNavBar = (sandbox: Sandbox, ui: UI, showNav: () => void)

const gistHash = location.hash.split("#gist/")[1]
const [gistID, gistStoryIndex] = gistHash.split("-")
fetch(`http://localhost:7071/api/API?gistID=${gistID}`)
// const relay = "http://localhost:7071/api/API"
const relay = "https://typescriptplaygroundgistproxyapi.azurewebsites.net/api/API"
fetch(`${relay}?gistID=${gistID}`)
.then(async res => {
playground.style.opacity = "1"
sandbox.editor.updateOptions({ readOnly: false })
Expand Down
61 changes: 61 additions & 0 deletions packages/sandbox/src/vendor/ata/apis.ts
@@ -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")
}
})
}
65 changes: 65 additions & 0 deletions packages/sandbox/src/vendor/ata/edgeCases.ts
@@ -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
}

0 comments on commit cbfcdd3

Please sign in to comment.