Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
);
const rawName = isScoped ? pkgName.replace(/^tunnckocore\//, '') : pkgName;
let pkgLoc = isScoped ? `@${pkgName}` : `packages/${pkgName}`;
const loc = /preset|config/.test(pkgName) ? `configs/${rawName}` : pkgLoc;
pkgLoc = `${loc}${restPath.length > 0 ? `/${restPath.join('/')}` : ''}`;
const pathname =
version === 'master' ? `@master/${pkgLoc}` : `@${encodedName}/${pkgLoc}`;
// try request `src/index.ts`, `src/index.js`, `index.ts`, `index.js`
if (!/\.(jsx?|tsx?)$/.test(pathname)) {
if (field) {
const resp = await ky
.get(`${JSDELIVR}${pathname}/package.json`)
.then((x) => x.text())
.catch(() => {});
// if `package.json` is found, try getting the source of file given `field`
// which field by default is the "module" one.
// Pass `field=main` query param to get the main field.
if (resp) {
let srcPath = null;
try {
srcPath = JSON.parse(resp)[field];
} catch (err) {
res.send('Found `package.json` but failed to parse.', 500);
return;
}
export async function packageJson(packageName, endpoint) {
const { name, version } = parsePkgName(packageName);
const tag = version === '' ? 'latest' : version;
const uri =
typeof endpoint === 'function'
? endpoint(name, tag)
: `https://cdn.jsdelivr.net/npm/${name}@${tag}/package.json`;
let pkg = null;
try {
pkg = await ky
.get(uri)
.then((resp) => resp.text())
.then(JSON.parse);
} catch (err) {
// ! jsDelivr can response with 403 Forbidden, if over 50MB
if (err.response && err.response.status === 403) {
try {
// ! so, try through UNPKG.com
pkg = await packageJson(
packageName,
(x, t) => `https://unpkg.com/${x}@${t}/package.json`,
);
} catch (error) {
throw new ky.HTTPError(
`Package "${name}" not found, even through UNPKG.com!`,
);
async function packageJson(packageName, endpoint) {
const { name, version } = parsePkgName(packageName);
const tag = version === '' ? 'latest' : version;
const uri =
typeof endpoint === 'function'
? endpoint(name, tag)
: `https://cdn.jsdelivr.net/npm/${name}@${tag}/package.json`;
let pkg = null;
try {
pkg = await ky
.get(uri)
.then((resp) => resp.text())
.then(JSON.parse);
} catch (err) {
// ! jsDelivr can response with 403 Forbidden, if over 50MB
if (err.response && err.response.status === 403) {
try {
// ! so, try through UNPKG.com
pkg = await packageJson(
packageName,
(x, t) => `https://unpkg.com/${x}@${t}/package.json`,
);
} catch (error) {
throw new ky.HTTPError(
`Package "${name}" not found, even through UNPKG.com!`,
);
.then((x) => x.text())
.catch(() => {});
// if `package.json` is found, try getting the source of file given `field`
// which field by default is the "module" one.
// Pass `field=main` query param to get the main field.
if (resp) {
let srcPath = null;
try {
srcPath = JSON.parse(resp)[field];
} catch (err) {
res.send('Found `package.json` but failed to parse.', 500);
return;
}
await ky
.get(`${JSDELIVR}${pathname}/${srcPath}`)
.then(() => {
res.send(`${JSDELIVR}${pathname}/${srcPath}`, 301);
})
.catch(() => {
res.send(
`Not found source path given in the "${field}" of package.json`,
404,
);
});
return;
}
}
const result = await pLocate(
['src/index.ts', 'src/index.js', 'index.ts', 'index.js'].map((fp) =>
['src/index.ts', 'src/index.js', 'index.ts', 'index.js'].map((fp) =>
ky
.get(`${JSDELIVR}${pathname}/${fp}`)
.then(async (x) => ({ file: fp, value: await x.text() }))
.catch(() => {}),
),
public async get(url: string): Promise {
return ky.get(url, { prefixUrl }).json();
}