Skip to content

Commit

Permalink
test: add a custom registry test case (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Nov 19, 2023
1 parent 020f0d2 commit 2376b38
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 32 deletions.
8 changes: 1 addition & 7 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export const defaultProviders: Record<string, Provider> = {
skypack,
unpkg,
"esm.sh": esmsh,
"jspm.io": jspm,

// TODO: remove at some point, alias for backwards compatibility:
jspm,
"jspm.io": jspm
};

export function getProvider(name: string, providers: Record<string, Provider>) {
Expand All @@ -74,9 +71,6 @@ export function getProvider(name: string, providers: Record<string, Provider>) {
export function getDefaultProviderStrings() {
let res = [];
for (const [name, provider] of Object.entries(defaultProviders)) {
// TODO: remove the jspm alias at some point along with this hack:
if (name === "jspm") continue;

for (const layer of provider.supportedLayers ?? ["default"])
res.push(`${name}${layer === "default" ? "" : `#${layer}`}`);
}
Expand Down
6 changes: 3 additions & 3 deletions src/trace/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Resolver {
}

providerNameForUrl(url: string): string | null {
for (const name of Object.keys(this.providers)) {
for (const name of Object.keys(this.providers).reverse()) {
const provider = this.providers[name];
if (
(provider.ownsUrl && provider.ownsUrl.call(this, url)) ||
Expand All @@ -111,7 +111,7 @@ export class Resolver {
}

async parseUrlPkg(url: string): Promise<ExactModule | null> {
for (const provider of Object.keys(this.providers)) {
for (const provider of Object.keys(this.providers).reverse()) {
const providerInstance = this.providers[provider];
const result = providerInstance.parseUrlPkg.call(this, url);
if (result)
Expand Down Expand Up @@ -139,7 +139,7 @@ export class Resolver {
}

resolveBuiltin(specifier: string): string | Install | undefined {
for (const provider of Object.values(this.providers)) {
for (const provider of Object.values(this.providers).reverse()) {
if (!provider.resolveBuiltin) continue;
const builtin = provider.resolveBuiltin.call(this, specifier, this.env);
if (builtin) return builtin;
Expand Down
63 changes: 63 additions & 0 deletions test/providers/customregistry.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Generator, lookup, fetch } from "@jspm/generator";
import assert from "assert";

const myorgUrl = "https://unpkg.com/";
const exactPkgRegEx = /^((?:@[^/\\%@]+\/)?[^./\\%@][^/\\%@]*)@([^\/]+)(\/.*)?$/;

const generator = new Generator({
defaultProvider: "myorg",
customProviders: {
myorg: {
ownsUrl(url) {
return url.startsWith(myorgUrl);
},
pkgToUrl({ registry, name, version }) {
return `${myorgUrl}${name}@${version}/`;
},
async getPackageConfig(pkgUrl) {
// hook package.json lookup to insert explicit registry identifiers in package.json dependencies
// the alternative to this approach is to set a global "defaultRegistry" generator option
// but that option is limited in that the defaultRegistry is global instead of being per-provider /
// per-service.
// This is thus the recommended way to support multi-registry workflows, keeping npm as the default
// (per ecosystem semantics), and instead overriding package.json dependency schemas to point to
// any new registries.
const pcfg = await (await fetch(`${pkgUrl}package.json`)).json();
// strictly, this should also be for optionalDependencies + peerDependencies
if (pcfg.dependencies) {
let dependencies = {};
for (let [name, target] of Object.entries(pcfg.dependencies)) {
if (target.indexOf(':') === -1)
target = 'myorg:' + name + '@' + target;
dependencies[name] = target;
}
pcfg.dependencies = dependencies;
}
return pcfg;
},
parseUrlPkg(url) {
if (url.startsWith(myorgUrl)) {
const [, name, version] = url.slice(myorgUrl.length).match(exactPkgRegEx) || [];
return { registry: "myorg", name, version };
}
},
async resolveLatestTarget(
{ registry, name, range, unstable },
layer,
parentUrl
) {
assert.ok(registry === 'myorg');
const { resolved: { name: resolvedName, version: resolvedVersion } } = await lookup(`${name}@${range.toString()}`);
return { registry: 'myorg', name: resolvedName, version: resolvedVersion };
},
},
},
});

await generator.install("myorg:lit");

const json = generator.getMap();

assert.ok(
json.imports.lit.startsWith('https://unpkg.com/lit@')
);
44 changes: 22 additions & 22 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<!-- Generated by @jspm/generator - https://github.com/jspm/generator -->
<script async src="https://ga.jspm.io/npm:es-module-shims@1.8.0/dist/es-module-shims.js" crossorigin="anonymous"></script>
<script async src="https://ga.jspm.io/npm:es-module-shims@1.8.2/dist/es-module-shims.js" crossorigin="anonymous"></script>
<script type="importmap">
{
"imports": {
Expand All @@ -10,29 +10,29 @@
"scopes": {
"../": {
"#fetch": "../dist/fetch-native.js",
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.23.2/lib/index.js",
"@babel/plugin-syntax-import-assertions": "https://ga.jspm.io/npm:@babel/plugin-syntax-import-assertions@7.22.5/lib/index.js",
"@babel/preset-typescript": "https://ga.jspm.io/npm:@babel/preset-typescript@7.23.2/lib/index.js",
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.23.3/lib/index.js",
"@babel/plugin-syntax-import-assertions": "https://ga.jspm.io/npm:@babel/plugin-syntax-import-assertions@7.23.3/lib/index.js",
"@babel/preset-typescript": "https://ga.jspm.io/npm:@babel/preset-typescript@7.23.3/lib/index.js",
"@jspm/import-map": "https://ga.jspm.io/npm:@jspm/import-map@1.0.7/dist/map.js",
"crypto": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/crypto.js",
"es-module-lexer/js": "https://ga.jspm.io/npm:es-module-lexer@1.3.1/dist/lexer.asm.js",
"es-module-lexer/js": "https://ga.jspm.io/npm:es-module-lexer@1.4.1/dist/lexer.asm.js",
"fs": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/fs.js",
"process": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/process-production.js",
"sver": "https://ga.jspm.io/npm:sver@1.8.4/sver.js",
"sver/convert-range.js": "https://ga.jspm.io/npm:sver@1.8.4/convert-range.js",
"url": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/url.js"
},
"https://ga.jspm.io/": {
"#lib/config/files/index.js": "https://ga.jspm.io/npm:@babel/core@7.23.2/lib/config/files/index-browser.js",
"#lib/config/resolve-targets.js": "https://ga.jspm.io/npm:@babel/core@7.23.2/lib/config/resolve-targets-browser.js",
"#lib/transform-file.js": "https://ga.jspm.io/npm:@babel/core@7.23.2/lib/transform-file-browser.js",
"#lib/config/files/index.js": "https://ga.jspm.io/npm:@babel/core@7.23.3/lib/config/files/index-browser.js",
"#lib/config/resolve-targets.js": "https://ga.jspm.io/npm:@babel/core@7.23.3/lib/config/resolve-targets-browser.js",
"#lib/transform-file.js": "https://ga.jspm.io/npm:@babel/core@7.23.3/lib/transform-file-browser.js",
"#node.js": "https://ga.jspm.io/npm:browserslist@4.22.1/browser.js",
"@ampproject/remapping": "https://ga.jspm.io/npm:@ampproject/remapping@2.2.1/dist/remapping.umd.js",
"@babel/code-frame": "https://ga.jspm.io/npm:@babel/code-frame@7.22.13/lib/index.js",
"@babel/compat-data/native-modules": "https://ga.jspm.io/npm:@babel/compat-data@7.23.2/native-modules.js",
"@babel/compat-data/plugins": "https://ga.jspm.io/npm:@babel/compat-data@7.23.2/plugins.js",
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.23.2/lib/index.js",
"@babel/generator": "https://ga.jspm.io/npm:@babel/generator@7.23.0/lib/index.js",
"@babel/compat-data/native-modules": "https://ga.jspm.io/npm:@babel/compat-data@7.23.3/native-modules.js",
"@babel/compat-data/plugins": "https://ga.jspm.io/npm:@babel/compat-data@7.23.3/plugins.js",
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.23.3/lib/index.js",
"@babel/generator": "https://ga.jspm.io/npm:@babel/generator@7.23.3/lib/index.js",
"@babel/helper-annotate-as-pure": "https://ga.jspm.io/npm:@babel/helper-annotate-as-pure@7.22.5/lib/index.js",
"@babel/helper-compilation-targets": "https://ga.jspm.io/npm:@babel/helper-compilation-targets@7.22.15/lib/index.js",
"@babel/helper-create-class-features-plugin": "https://ga.jspm.io/npm:@babel/helper-create-class-features-plugin@7.22.15/lib/index.js",
Expand All @@ -41,7 +41,7 @@
"@babel/helper-hoist-variables": "https://ga.jspm.io/npm:@babel/helper-hoist-variables@7.22.5/lib/index.js",
"@babel/helper-member-expression-to-functions": "https://ga.jspm.io/npm:@babel/helper-member-expression-to-functions@7.23.0/lib/index.js",
"@babel/helper-module-imports": "https://ga.jspm.io/npm:@babel/helper-module-imports@7.22.15/lib/index.js",
"@babel/helper-module-transforms": "https://ga.jspm.io/npm:@babel/helper-module-transforms@7.23.0/lib/index.js",
"@babel/helper-module-transforms": "https://ga.jspm.io/npm:@babel/helper-module-transforms@7.23.3/lib/index.js",
"@babel/helper-optimise-call-expression": "https://ga.jspm.io/npm:@babel/helper-optimise-call-expression@7.22.5/lib/index.js",
"@babel/helper-plugin-utils": "https://ga.jspm.io/npm:@babel/helper-plugin-utils@7.22.5/lib/index.js",
"@babel/helper-replace-supers": "https://ga.jspm.io/npm:@babel/helper-replace-supers@7.22.20/lib/index.js",
Expand All @@ -53,14 +53,14 @@
"@babel/helper-validator-option": "https://ga.jspm.io/npm:@babel/helper-validator-option@7.22.15/lib/index.js",
"@babel/helpers": "https://ga.jspm.io/npm:@babel/helpers@7.23.2/lib/index.js",
"@babel/highlight": "https://ga.jspm.io/npm:@babel/highlight@7.22.20/lib/index.js",
"@babel/parser": "https://ga.jspm.io/npm:@babel/parser@7.23.0/lib/index.js",
"@babel/plugin-syntax-jsx": "https://ga.jspm.io/npm:@babel/plugin-syntax-jsx@7.22.5/lib/index.js",
"@babel/plugin-syntax-typescript": "https://ga.jspm.io/npm:@babel/plugin-syntax-typescript@7.22.5/lib/index.js",
"@babel/plugin-transform-modules-commonjs": "https://ga.jspm.io/npm:@babel/plugin-transform-modules-commonjs@7.23.0/lib/index.js",
"@babel/plugin-transform-typescript": "https://ga.jspm.io/npm:@babel/plugin-transform-typescript@7.22.15/lib/index.js",
"@babel/parser": "https://ga.jspm.io/npm:@babel/parser@7.23.3/lib/index.js",
"@babel/plugin-syntax-jsx": "https://ga.jspm.io/npm:@babel/plugin-syntax-jsx@7.23.3/lib/index.js",
"@babel/plugin-syntax-typescript": "https://ga.jspm.io/npm:@babel/plugin-syntax-typescript@7.23.3/lib/index.js",
"@babel/plugin-transform-modules-commonjs": "https://ga.jspm.io/npm:@babel/plugin-transform-modules-commonjs@7.23.3/lib/index.js",
"@babel/plugin-transform-typescript": "https://ga.jspm.io/npm:@babel/plugin-transform-typescript@7.23.3/lib/index.js",
"@babel/template": "https://ga.jspm.io/npm:@babel/template@7.22.15/lib/index.js",
"@babel/traverse": "https://ga.jspm.io/npm:@babel/traverse@7.23.2/lib/index.js",
"@babel/types": "https://ga.jspm.io/npm:@babel/types@7.23.0/lib/index.js",
"@babel/traverse": "https://ga.jspm.io/npm:@babel/traverse@7.23.3/lib/index.js",
"@babel/types": "https://ga.jspm.io/npm:@babel/types@7.23.3/lib/index.js",
"@jridgewell/gen-mapping": "https://ga.jspm.io/npm:@jridgewell/gen-mapping@0.3.3/dist/gen-mapping.umd.js",
"@jridgewell/resolve-uri": "https://ga.jspm.io/npm:@jridgewell/resolve-uri@3.1.1/dist/resolve-uri.umd.js",
"@jridgewell/set-array": "https://ga.jspm.io/npm:@jridgewell/set-array@1.1.2/dist/set-array.umd.js",
Expand All @@ -69,13 +69,13 @@
"ansi-styles": "https://ga.jspm.io/npm:ansi-styles@3.2.1/index.js",
"browserslist": "https://ga.jspm.io/npm:browserslist@4.22.1/index.js",
"buffer": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/buffer.js",
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:caniuse-lite@1.0.30001554/dist/unpacker/agents.js",
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:caniuse-lite@1.0.30001563/dist/unpacker/agents.js",
"chalk": "https://ga.jspm.io/npm:chalk@2.4.2/index.js",
"color-convert": "https://ga.jspm.io/npm:color-convert@1.9.3/index.js",
"color-name": "https://ga.jspm.io/npm:color-name@1.1.3/index.js",
"convert-source-map": "https://ga.jspm.io/npm:convert-source-map@2.0.0/index.js",
"debug": "https://ga.jspm.io/npm:debug@4.3.4/src/browser.js",
"electron-to-chromium/versions": "https://ga.jspm.io/npm:electron-to-chromium@1.4.566/versions.js",
"electron-to-chromium/versions": "https://ga.jspm.io/npm:electron-to-chromium@1.4.585/versions.js",
"escape-string-regexp": "https://ga.jspm.io/npm:escape-string-regexp@1.0.5/index.js",
"fs": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/fs.js",
"gensync": "https://ga.jspm.io/npm:gensync@1.0.0-beta.2/index.js",
Expand Down

0 comments on commit 2376b38

Please sign in to comment.