1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// @ts-check js
import { API_URL } from "https://esm.town/v/std/API_URL";
const VALTOWN_API_URL = API_URL.concat("/v1");
import { getMyValTownUserUUID } from "https://esm.town/v/zarutian/getMyValTownUserUUID";
import { getAllValsOfUser } from "https://esm.town/v/zarutian/getAllValsOfUser";
import { blob } from "https://esm.town/v/std/blob";
const add = (a, b) => (a + b);
/**
* @param {string=} prefix - prefix of blob keys
* @returns {Promise<number>} - how many bytes used by those blobs
**/
export const getAllUsedByBlobs = async (prefix = "") => {
return (await blob.list(prefix)).map((item) => item.size).reduce(add, 0);
};
/**
* @callback privacyCallbackFilterFn
* @param {"private" | "unlisted" | "public"} kind
* @returns {boolean}
**/
/**
* @param {string=} prefix - prefix of val names
* @param {privacyCallbackFilterFn=} privacyCallbackFn
* @returns {Promise<number>} - how many bytes used by those vals
**/
export const getAllUsedByVals = async (prefix = "", privacyCallbackFn = (_) => true) => {
const myUserUUID = await getMyValTownUserUUID();
const allMyVals = await getAllValsOfUser(myUserUUID);
return allMyVals.filter(
(item) => (privacyCallbackFn(item.privacy) && item.name.startsWith(prefix))
).map((item) => (item.code.length)).reduce(add, 0);
};
/**
* @returns {Promise<number>}
**/
export const getAllUsed = async () => {
return (await Promise.all([
getAllUsedByBlobs(),
getAllUsedByVals(),
])).reduce(add, 0);
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
January 19, 2024