just returns no
This commit is contained in:
parent
9151402408
commit
6b6e4638d8
1 changed files with 38 additions and 28 deletions
58
lib.ts
58
lib.ts
|
|
@ -15,16 +15,13 @@ export const thIgnore = [
|
||||||
"bpmkeytracker",
|
"bpmkeytracker",
|
||||||
]
|
]
|
||||||
|
|
||||||
export function jsonToCsv(data: unknown|unknown[]) {
|
export function jsonToCsv(data: unknown | unknown[]) {
|
||||||
const arr = Array.isArray(data) ? data : [data];
|
const arr = Array.isArray(data) ? data : [data];
|
||||||
const headers = [...new Set(arr.flatMap(o => Object.keys(o)))];
|
const headers = [...new Set(arr.flatMap((o) => Object.keys(o)))];
|
||||||
|
|
||||||
const escape = (v: string )=>
|
const escape = (v: string) => `"${String(v ?? "").replace(/"/g, '""')}"`;
|
||||||
`"${String(v ?? "").replace(/"/g, '""')}"`;
|
|
||||||
|
|
||||||
const rows = arr.map(o =>
|
const rows = arr.map((o) => headers.map((h) => escape(o[h])).join(","));
|
||||||
headers.map(h => escape(o[h])).join(",")
|
|
||||||
);
|
|
||||||
|
|
||||||
return [headers.join(","), ...rows].join("\n");
|
return [headers.join(","), ...rows].join("\n");
|
||||||
}
|
}
|
||||||
|
|
@ -32,9 +29,16 @@ export function jsonToCsv(data: unknown|unknown[]) {
|
||||||
export enum TripleBool {
|
export enum TripleBool {
|
||||||
MOSTLY = 2,
|
MOSTLY = 2,
|
||||||
YES = 1,
|
YES = 1,
|
||||||
NO = 0
|
NO = 0,
|
||||||
|
}
|
||||||
|
export interface Entry {
|
||||||
|
name: string;
|
||||||
|
url: string;
|
||||||
|
credit: string;
|
||||||
|
updated: TripleBool;
|
||||||
|
links_work: TripleBool;
|
||||||
|
best: boolean;
|
||||||
}
|
}
|
||||||
export interface Entry { name: string, url: string, credit: string, updated: TripleBool, links_work: TripleBool, best: boolean };
|
|
||||||
|
|
||||||
export type Change =
|
export type Change =
|
||||||
| { op: "delete"; name: string }
|
| { op: "delete"; name: string }
|
||||||
|
|
@ -51,19 +55,25 @@ export function tripleBoolToString(v: TripleBool): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function tripleBool(bool: string): TripleBool {
|
export function tripleBool(bool: string): TripleBool {
|
||||||
if(bool.toLowerCase() == "mostly") {
|
if (bool.toLowerCase() == "mostly") {
|
||||||
return TripleBool.MOSTLY;
|
return TripleBool.MOSTLY;
|
||||||
} else if(bool.toLowerCase() == "yes") {
|
} else if (bool.toLowerCase() == "yes") {
|
||||||
return TripleBool.YES
|
return TripleBool.YES;
|
||||||
} else if(bool.toLowerCase() == "no") {
|
} else if (bool.toLowerCase() == "no") {
|
||||||
return TripleBool.NO
|
return TripleBool.NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error("tripleBool conversion function errored, mysteriously! Passed in: " + bool)
|
console.error(
|
||||||
|
"tripleBool conversion function errored, mysteriously! Passed in: " + bool,
|
||||||
|
);
|
||||||
|
|
||||||
|
return TripleBool.NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prettyStringify(z: unknown) {
|
export function prettyStringify(z: unknown) {
|
||||||
return JSON.stringify(z, null, 2).replace(/\n {2}"/g, ' "').replace(/\n}/, " }")
|
return JSON.stringify(z, null, 2)
|
||||||
|
.replace(/\n {2}"/g, ' "')
|
||||||
|
.replace(/\n}/, " }");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function requireBasicAuth(req: Request): Response | null {
|
export function requireBasicAuth(req: Request): Response | null {
|
||||||
|
|
@ -71,28 +81,28 @@ export function requireBasicAuth(req: Request): Response | null {
|
||||||
if (!auth || !auth.startsWith("Basic ")) {
|
if (!auth || !auth.startsWith("Basic ")) {
|
||||||
return new Response("Auth required", {
|
return new Response("Auth required", {
|
||||||
status: 401,
|
status: 401,
|
||||||
headers: { "WWW-Authenticate": 'Basic realm="acx-sheets"' }
|
headers: { "WWW-Authenticate": 'Basic realm="acx-sheets"' },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const decoded = Buffer.from(auth.slice(6), "base64").toString("utf8");
|
const decoded = Buffer.from(auth.slice(6), "base64").toString("utf8");
|
||||||
const [user, pass] = decoded.split(":");
|
const [user, pass] = decoded.split(":");
|
||||||
|
|
||||||
if (
|
if (user !== process.env.MERGE_USER || pass !== process.env.MERGE_PASS) {
|
||||||
user !== process.env.MERGE_USER ||
|
|
||||||
pass !== process.env.MERGE_PASS
|
|
||||||
) {
|
|
||||||
return new Response("Forbidden", { status: 403 });
|
return new Response("Forbidden", { status: 403 });
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
export function ndjsonToJson(ndjson: string): unknown[] {
|
export function ndjsonToJson(ndjson: string): unknown[] {
|
||||||
return ndjson.split("\n").map(z => {
|
return ndjson
|
||||||
|
.split("\n")
|
||||||
|
.map((z) => {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(z)
|
return JSON.parse(z);
|
||||||
} catch {
|
} catch {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}).filter(Boolean);
|
})
|
||||||
|
.filter(Boolean);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue