-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.ts
102 lines (86 loc) · 2.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as deno from "./deno.js";
import * as jspm from "./jspm.js";
import * as skypack from "./skypack.js";
import * as jsdelivr from "./jsdelivr.js";
import * as unpkg from "./unpkg.js";
import * as node from "./node.js";
import * as esmsh from "./esmsh.js";
import {
PackageConfig,
ExactPackage,
LatestPackageTarget,
} from "../install/package.js";
import { Resolver } from "../trace/resolver.js";
import { Install } from "../generator.js";
import { JspmError } from "../common/err.js";
export interface Provider {
parseUrlPkg(
this: Resolver,
url: string
):
| ExactPackage
| { pkg: ExactPackage; subpath: `./${string}` | null; layer: string }
| null;
pkgToUrl(
this: Resolver,
pkg: ExactPackage,
layer: string
): Promise<`${string}/`>;
resolveLatestTarget(
this: Resolver,
target: LatestPackageTarget,
layer: string,
parentUrl: string
): Promise<ExactPackage | null>;
ownsUrl?(this: Resolver, url: string): boolean;
resolveBuiltin?(
this: Resolver,
specifier: string,
env: string[]
): string | Install | null;
getPackageConfig?(
this: Resolver,
pkgUrl: string
): Promise<PackageConfig | null>;
supportedLayers?: string[];
configure?(config: any): void;
}
export const defaultProviders: Record<string, Provider> = {
deno,
jsdelivr,
node,
skypack,
unpkg,
"esm.sh": esmsh,
"jspm.io": jspm,
};
export function getProvider(name: string, providers: Record<string, Provider>) {
const provider = providers[name];
if (provider) return provider;
throw new JspmError(`No provider named "${name}" has been defined.`);
}
// Apply provider configurations
export function configureProviders(
providerConfig: Record<string, any>,
providers: Record<string, Provider>
) {
for (const [providerName, provider] of Object.entries(providers)) {
if (provider.configure) {
provider.configure(providerConfig[providerName] || {});
}
}
}
export function getDefaultProviderStrings() {
let res = [];
for (const [name, provider] of Object.entries(defaultProviders)) {
for (const layer of provider.supportedLayers ?? ["default"])
res.push(`${name}${layer === "default" ? "" : `#${layer}`}`);
}
return res;
}
export const registryProviders: Record<string, string> = {
"denoland:": "deno",
"deno:": "deno",
};
export const mappableSchemes = new Set<String>(["npm", "deno", "node"]);
export const builtinSchemes = new Set<String>(["node", "deno"]);