Make it modern
This commit is contained in:
@ -1,15 +1,13 @@
|
||||
import { ABC, Base64, Path } from "./deps.ts";
|
||||
import config from "./config.ts";
|
||||
import * as Storage from "./storage.ts";
|
||||
|
||||
const packageNameRegex = /^[@]?[a-zA-Z][\d\w\-\_]*$/g.compile();
|
||||
const packageVersionRegex = /^\d(\.\d)?(\.\d)?$/g.compile();
|
||||
const packageFullVersionRegex = /^\d(\.\d)(\.\d)$/g.compile();
|
||||
|
||||
export const isValidPackageName = (name: string) => packageNameRegex.test(name);
|
||||
export const isValidPackageName = (name: string) => /^[@]?[a-zA-Z][\d\w\-\_]*$/g.test(name);
|
||||
export const isValidVersion = (version: string) =>
|
||||
packageVersionRegex.test(version);
|
||||
/^\d+(\.\d+)?(\.\d+)?$/g.test(version);
|
||||
export const isValidFullVersion = (version: string) =>
|
||||
packageFullVersionRegex.test(version);
|
||||
/^\d+(\.\d+)(\.\d+)$/g.test(version);
|
||||
|
||||
const ALg = 1;
|
||||
const ASm = -ALg;
|
||||
@ -70,16 +68,18 @@ export function extractPackagePath(path: string): [string, string | undefined] {
|
||||
path = path.slice(1);
|
||||
}
|
||||
|
||||
let parts = path.split("@");
|
||||
|
||||
const parts = path.split("@");
|
||||
if (parts.length > 2) throw new Error("Invalid package name!");
|
||||
|
||||
packageName += parts[0];
|
||||
let packageVersion: string | undefined = parts[1];
|
||||
const packageVersion: string | undefined = parts[1];
|
||||
console.log({ path, parts, packageName, packageVersion });
|
||||
|
||||
if (!isValidPackageName(packageName))
|
||||
throw new Error("Invalid package name!");
|
||||
|
||||
if (packageVersion !== "") {
|
||||
if (packageVersion && packageVersion !== "") {
|
||||
if (!isValidVersion(packageVersion))
|
||||
throw new Error("Invalid package version!");
|
||||
}
|
||||
@ -89,8 +89,6 @@ export function extractPackagePath(path: string): [string, string | undefined] {
|
||||
|
||||
import type { IPackage } from "./db.ts";
|
||||
|
||||
import bucket from "./s3.ts";
|
||||
|
||||
export function getAbsolutePackageVersion(
|
||||
pkg?: IPackage | null,
|
||||
version?: string
|
||||
@ -110,7 +108,7 @@ export function getAbsolutePackageVersion(
|
||||
return version;
|
||||
}
|
||||
|
||||
export async function getBucketFilePath(
|
||||
export function getFilePath(
|
||||
pkgName: string,
|
||||
version: string,
|
||||
file: string
|
||||
@ -120,7 +118,6 @@ export async function getBucketFilePath(
|
||||
}
|
||||
|
||||
const bucketPath = (
|
||||
"packages/" +
|
||||
pkgName +
|
||||
"/" +
|
||||
version +
|
||||
@ -131,24 +128,33 @@ export async function getBucketFilePath(
|
||||
return bucketPath;
|
||||
}
|
||||
|
||||
export async function getOneOf(pkgName: string, version: string, files: (string | undefined)[]) {
|
||||
for (const file of files) {
|
||||
if (!file) continue;
|
||||
const res = await getFile(pkgName, version, file);
|
||||
if (res) return res;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export async function getFile(
|
||||
pkgName: string,
|
||||
version: string | undefined,
|
||||
file: string
|
||||
): Promise<{ etag: string; data: Uint8Array } | null | undefined> {
|
||||
if (!version) return undefined;
|
||||
const bucketPath = await getBucketFilePath(pkgName, version, file);
|
||||
const bucketPath = await getFilePath(pkgName, version, file);
|
||||
if (!bucketPath) return null;
|
||||
|
||||
console.log("Getting file from:", bucketPath);
|
||||
|
||||
try {
|
||||
const res = await bucket.getObject(bucketPath);
|
||||
if (!res || res.body.byteLength === 0) return undefined;
|
||||
const res = await Storage.readFile(bucketPath);
|
||||
if (!res) return undefined;
|
||||
|
||||
return {
|
||||
etag: res.etag,
|
||||
data: res.body,
|
||||
etag: Base64.encodeBase64(await crypto.subtle.digest("sha-1", res)),
|
||||
data: res,
|
||||
};
|
||||
} catch (err) {
|
||||
const msg = err.message as string;
|
||||
|
Reference in New Issue
Block a user