nixamp 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/handles.d.ts +9 -0
- package/dist/handles.js +19 -1
- package/dist/main.js +6 -0
- package/dist/opendirs.d.ts +43 -0
- package/dist/opendirs.js +133 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +179 -105
- package/dist/session.d.ts +8 -0
- package/dist/session.js +86 -0
- package/package.json +1 -1
- package/src/handles.ts +22 -1
- package/src/main.ts +6 -0
- package/src/opendirs.ts +158 -0
- package/src/server.ts +191 -110
- package/src/session.ts +100 -0
- package/web/dist/index.html +1 -0
- package/web/dist/sw.js +1 -1
package/src/session.ts
CHANGED
|
@@ -659,3 +659,103 @@ interface ServerRow {
|
|
|
659
659
|
url: string;
|
|
660
660
|
key: string;
|
|
661
661
|
}
|
|
662
|
+
|
|
663
|
+
/** One row of the public open-directory list, as the API sends it. */
|
|
664
|
+
interface OpenDirRow {
|
|
665
|
+
id: string;
|
|
666
|
+
url: string;
|
|
667
|
+
name: string;
|
|
668
|
+
tracks: number;
|
|
669
|
+
by: string;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* `nixamp opendir list|add|remove`, the public list of folders people found.
|
|
674
|
+
*
|
|
675
|
+
* Reading needs no account, which is why `list` works signed out. Adding needs
|
|
676
|
+
* one, because a public list with nobody accountable for its rows is a list of
|
|
677
|
+
* whatever anybody felt like putting there.
|
|
678
|
+
*/
|
|
679
|
+
export async function opendirs(argv: string[], fetcher: typeof fetch = fetch): Promise<number> {
|
|
680
|
+
const [command = "list", ...rest] = argv;
|
|
681
|
+
const session = readSession();
|
|
682
|
+
const site = session?.site ?? DEFAULT_DIRECTORY;
|
|
683
|
+
const where = `${site}/api/v1/opendirs`;
|
|
684
|
+
const headers: Record<string, string> = {
|
|
685
|
+
"content-type": "application/json",
|
|
686
|
+
...(session ? { authorization: `Bearer ${session.token}` } : {}),
|
|
687
|
+
};
|
|
688
|
+
const at = (flag: string): string | undefined => {
|
|
689
|
+
const index = rest.indexOf(flag);
|
|
690
|
+
return index === -1 ? undefined : rest[index + 1];
|
|
691
|
+
};
|
|
692
|
+
|
|
693
|
+
try {
|
|
694
|
+
if (command === "list" || command === "ls") {
|
|
695
|
+
const answer = await fetcher(where, { headers });
|
|
696
|
+
const body = (await answer.json().catch(() => ({}))) as { opendirs?: OpenDirRow[]; error?: string };
|
|
697
|
+
if (!answer.ok) {
|
|
698
|
+
console.error(`nixamp: ${body.error ?? `could not read the list (${answer.status})`}`);
|
|
699
|
+
return 1;
|
|
700
|
+
}
|
|
701
|
+
const rows = body.opendirs ?? [];
|
|
702
|
+
if (rows.length === 0) {
|
|
703
|
+
console.log("Nothing published yet. `nixamp opendir add <url>` puts one there.");
|
|
704
|
+
return 0;
|
|
705
|
+
}
|
|
706
|
+
const width = Math.max(...rows.map((row) => row.name.length));
|
|
707
|
+
for (const row of rows) {
|
|
708
|
+
const who = row.by ? ` by ${row.by}` : "";
|
|
709
|
+
console.log(`${row.id} ${row.name.padEnd(width)} ${row.tracks} tracks${who}`);
|
|
710
|
+
console.log(` ${row.url}`);
|
|
711
|
+
}
|
|
712
|
+
return 0;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
if (session === null) {
|
|
716
|
+
console.error("nixamp: sign in to publish or remove one. Try `nixamp login`.");
|
|
717
|
+
return 1;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (command === "add" || command === "publish") {
|
|
721
|
+
const target = rest.find((a) => /^https?:\/\//.test(a)) ?? "";
|
|
722
|
+
if (!target) {
|
|
723
|
+
console.error("nixamp: which folder? Give the URL of a directory listing.");
|
|
724
|
+
return 64;
|
|
725
|
+
}
|
|
726
|
+
const answer = await fetcher(where, {
|
|
727
|
+
method: "POST",
|
|
728
|
+
headers,
|
|
729
|
+
body: JSON.stringify({ url: target, name: at("--name") ?? "" }),
|
|
730
|
+
});
|
|
731
|
+
const body = (await answer.json().catch(() => ({}))) as { opendir?: OpenDirRow; error?: string };
|
|
732
|
+
if (!answer.ok || !body.opendir) {
|
|
733
|
+
console.error(`nixamp: ${body.error ?? `could not publish it (${answer.status})`}`);
|
|
734
|
+
return 1;
|
|
735
|
+
}
|
|
736
|
+
console.log(`${body.opendir.id} ${body.opendir.name} ${body.opendir.tracks} tracks`);
|
|
737
|
+
return 0;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (command === "remove" || command === "rm") {
|
|
741
|
+
const id = rest.find((a) => !a.startsWith("-")) ?? "";
|
|
742
|
+
if (!id) {
|
|
743
|
+
console.error("nixamp: which one? `nixamp opendir list` shows their ids.");
|
|
744
|
+
return 64;
|
|
745
|
+
}
|
|
746
|
+
const answer = await fetcher(`${where}/${encodeURIComponent(id)}`, { method: "DELETE", headers });
|
|
747
|
+
if (!answer.ok) {
|
|
748
|
+
console.error(`nixamp: ${answer.status === 404 ? "not yours, or not there" : "could not remove it"}`);
|
|
749
|
+
return 1;
|
|
750
|
+
}
|
|
751
|
+
console.log(`Removed ${id}.`);
|
|
752
|
+
return 0;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
console.error(`nixamp: no such opendir command: ${command}`);
|
|
756
|
+
return 64;
|
|
757
|
+
} catch (error) {
|
|
758
|
+
console.error(`nixamp: could not reach ${site}: ${(error as Error).message}`);
|
|
759
|
+
return 69;
|
|
760
|
+
}
|
|
761
|
+
}
|
package/web/dist/index.html
CHANGED
package/web/dist/sw.js
CHANGED