Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial work to parse package metadata from cdn url 🤘🏾 #17

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,528 changes: 173 additions & 2,355 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
"dir-size": "chomp dir-size"
},
"dependencies": {
"@jspm/generator": "^1.1.10",
"@jspm/import-map": "^1.0.7",
"node-fetch": "^3.3.2"
"@jspm/import-map": "^1.0.7"
},
"devDependencies": {
"@commitlint/cli": "^17.6.7",
Expand Down
14 changes: 2 additions & 12 deletions src/__tests__/loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
jest.mock("node-fetch", () =>
jest.fn().mockResolvedValue({
ok: true,
text: jest.fn().mockResolvedValue("module code"),
})
);

jest.mock("@jspm/generator", () => ({
parseUrlPkg: jest.fn(),
}));


jest.mock("@jspm/import-map", () => ({
ImportMap: jest.fn(() => ({
resolve: jest.fn(),
Expand Down Expand Up @@ -47,9 +35,11 @@ describe('loader', () => {
})

test("resolved with basic config", async () => {
const resolveModulePathSpy = jest.spyOn(utils, 'resolveModulePath').mockReturnValue('modulePath');
const checkIfNodeOrFileProtocolSpy = jest.spyOn(utils, 'checkIfNodeOrFileProtocol').mockReturnValue(true);
const context = { parentURL: "parentURL" };
await resolve(specifier, context, nextResolve);
expect(resolveModulePathSpy).toHaveBeenCalledWith(specifier, context.parentURL);
expect(checkIfNodeOrFileProtocolSpy).toHaveBeenCalled();
expect(nextResolve).toHaveBeenCalledWith('specifier');
});
Expand Down
15 changes: 10 additions & 5 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseNodeModuleCachePath } from '../parser';
import { parseNodeModuleCachePath, getPackageNameVersionFromUrl } from '../parser';

import * as fs from "node:fs";

Expand All @@ -17,10 +17,6 @@ jest.mock("@jspm/import-map", () => ({
})),
}));

jest.mock('@jspm/generator', () => ({
parseUrlPkg: jest.fn(),
}))

jest.mock("../utils", () => {
const actual = jest.requireActual("../utils");
return {
Expand Down Expand Up @@ -71,3 +67,12 @@ describe("parseNodeModuleCachePath", () => {
expect(errorSpy).toHaveBeenCalled();
});
});

test('getPackageNameVersionFromUrl', () => {
const result = getPackageNameVersionFromUrl('https://ga.jspm.io/npm:[email protected]/index.js')
expect(result).toStrictEqual({
file: 'index.js',
name: 'morgan',
version: '1.10.0',
});
});
36 changes: 22 additions & 14 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
ensureFileSync,
resolveModulePath,
resolveNodeModuleCachePath,
resolveParsedModulePath
resolveParsedModulePath,
getLastPart,
getVersion,
} from '../utils';

jest.mock('node:fs');
Expand All @@ -18,17 +20,12 @@ jest.mock("@jspm/import-map", () => ({
})),
}));

jest.mock('@jspm/generator', () => ({
parseUrlPkg: jest.fn(),
}))

import * as generator from '@jspm/generator';

jest.mock('../config')
import * as config from '../config';

jest.mock('../parser', () => ({
parseNodeModuleCachePath: jest.fn(),
getPackageNameVersionFromUrl: jest.fn(),
}))
import * as parser from '../parser';

Expand Down Expand Up @@ -96,17 +93,16 @@ test('resolveModulePath', () => {
})

test('resolveModulePath with modulePath', async () => {
const parseUrlPkgSpy = await jest.spyOn(generator, 'parseUrlPkg').mockResolvedValue({
pkg: {
name: 'foo',
version: '1.0.0',
}
} as any);
const getPackageNameVersionFromUrlSpy = jest.spyOn(parser, 'getPackageNameVersionFromUrl').mockReturnValue({
fie: 'bar/index.js',
name: 'foo',
version: '1.0.0',
} as unknown as Record<string, string>);
(jest.mocked(config).cache as unknown) = 'test/.cache'
const modulePath = 'file:///bar/index.js';
const joinSpy = jest.spyOn(path, 'join').mockReturnValue('test/.cache/[email protected]/bar/index.js');
const result = await resolveNodeModuleCachePath(modulePath);
expect(parseUrlPkgSpy).toBeCalledWith(modulePath);
expect(getPackageNameVersionFromUrlSpy).toBeCalledWith(modulePath);
expect(joinSpy).toBeCalled();
expect(result).toBe('test/.cache/[email protected]/bar/index.js');
});
Expand All @@ -117,3 +113,15 @@ test('resolveParsedModulePath', async () => {
expect(parseNodeModuleCachePathSpy).toBeCalled();
expect(result).toBe('file:///foo/bar');
})


test('getVersion', () => {
const urlParts = ['1.10.0/index.js']
const result = getVersion(urlParts)(0)
expect(result).toStrictEqual('1.10.0')
});

test('getLastPart', () => {
const result = getLastPart('1.10.0/index.js', '/')
expect(result).toStrictEqual('index.js')
});
28 changes: 20 additions & 8 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, writeFileSync } from "node:fs";
import { ensureFileSync } from "src/utils";
import { ensureFileSync, getLastPart, getVersion } from "src/utils";
import { logger } from "src/logger";
import { isDebuggingEnabled } from "./config";

Expand All @@ -14,13 +14,25 @@ import { isDebuggingEnabled } from "./config";

const log = logger({ file: "parser", isLogging: isDebuggingEnabled() });

/**
* parseNodeModuleCachePath
* @description a convenience function to parse modules
* @param {string} modulePath
* @param {string} cachePath
* @returns {string}
*/
export const getPackageNameVersionFromUrl = (url: string) => {
try {
const file = getLastPart(url, "/");
const urlParts = url?.split("@");
const urlPartsCount = urlParts.length;
if (urlPartsCount === 3) {
const name = `@${urlParts[1]}`;
const version = getVersion(urlParts)(2);
return { file, name, version };
}
const name = getLastPart(getLastPart(urlParts[0], "/"), ":");
const version = getVersion(urlParts)(1);
return { file, name, version };
} catch (err) {
log.error(`getPackageNameVersionFromUrl: ${err}`, {});
return {};
}
};

export const parseNodeModuleCachePath = async (modulePath: string, cachePath: string) => {
log.debug("parseNodeModuleCachePath", cachePath, modulePath);
if (existsSync(cachePath)) return cachePath;
Expand Down
15 changes: 8 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { parseUrlPkg } from "@jspm/generator";
import { parseNodeModuleCachePath } from "./parser";
import { parseNodeModuleCachePath, getPackageNameVersionFromUrl } from "./parser";
import { cache, importmap, isDebuggingEnabled } from "./config";
import { logger } from "./logger";

Expand Down Expand Up @@ -61,11 +60,8 @@ export const resolveModulePath = (specifier: string, cacheMapPath: string): stri

export const resolveNodeModuleCachePath = async (modulePath: string) => {
try {
const moduleMetadata = await parseUrlPkg(modulePath);
const name = moduleMetadata?.pkg?.name;
const version = moduleMetadata?.pkg?.version;
const moduleFile = modulePath.split("/").reverse()[0] || "";
const nodeModuleCachePath = join(cache, `${name}@${version}`, moduleFile);
const { name, version, file = "" } = getPackageNameVersionFromUrl(modulePath);
const nodeModuleCachePath = join(cache, `${name}@${version}`, file);
log.debug("resolveNodeModuleCachePath:", { nodeModuleCachePath });
return nodeModuleCachePath;
} catch (err) {
Expand All @@ -84,3 +80,8 @@ export const resolveParsedModulePath = async (modulePath: string, nodeModuleCach
return;
}
};

export const getVersion = (urlParts: string[]) => (index: number) => urlParts?.[index]?.split("/")?.[0] || "";

export const getLastPart = (part: string, char: string) =>
(part?.length && char && part?.split(char)?.reverse()[0]) || "";