54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import { ABC } from "../deps.ts";
|
|
|
|
import { sortVersions, extractPackagePath } from "../utils.ts";
|
|
|
|
import db, { IPackage } from "../db.ts";
|
|
|
|
import bucket from "../s3.ts";
|
|
|
|
export default function raw(g: ABC.Group) {
|
|
g.get("/:package/*path", async (ctx) => {
|
|
console.log(ctx.params, ctx.path);
|
|
let [packageName, packageVersion] = extractPackagePath(
|
|
ctx.params.package
|
|
);
|
|
|
|
const meta = await db.findOne({ name: packageName });
|
|
|
|
console.log(packageName, await db.findOne({ name: packageName }));
|
|
|
|
const E404 = () => {
|
|
ctx.response.status = 404;
|
|
ctx.response.body = "Not found!";
|
|
throw new Error("Not found!");
|
|
};
|
|
|
|
if (!meta || meta.versions.length < 1) return E404();
|
|
|
|
const versions = meta.versions.sort(sortVersions).reverse();
|
|
|
|
if (!packageVersion) {
|
|
packageVersion = versions[0];
|
|
} else {
|
|
const v = versions.filter((e) =>
|
|
e.startsWith(packageVersion as string)
|
|
);
|
|
if (v.length < 1) return E404();
|
|
packageVersion = v[0];
|
|
}
|
|
|
|
const bucketPath = (
|
|
"packages/" +
|
|
packageName +
|
|
"/" +
|
|
packageVersion +
|
|
"/" +
|
|
ctx.params.path
|
|
).replace(/@/g, "§");
|
|
|
|
console.log("Getting file from:", bucketPath);
|
|
|
|
return (await bucket.getObject(bucketPath))?.body;
|
|
});
|
|
}
|