@wervt/platform 0.0.0-stage → 0.0.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/README.md +7 -2
- package/dist/dev.d.ts +62 -0
- package/dist/dev.js +158 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/provision.d.ts +75 -0
- package/dist/provision.js +237 -0
- package/dist/seed.d.ts +30 -0
- package/dist/seed.js +51 -0
- package/dist/storage.d.ts +98 -0
- package/dist/storage.js +362 -0
- package/package.json +45 -3
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @wervt/platform
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Provisioning shared by the wervt control plane and local development (`nuxt dev` through
|
|
4
|
+
`@wervt/nuxt`, later `wervt dev`): each app's Postgres role and schema with a password derived from
|
|
5
|
+
`WERVT_DB_SECRET`, migrations as that role, PgBouncer's login role, and the app's Garage bucket and
|
|
6
|
+
S3 key.
|
|
7
|
+
|
|
8
|
+
Node only. Apps don't use this package directly.
|
package/dist/dev.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { SEED_TASK, SeedOutcome, runDevSeed, unscheduleSeed } from "./seed.js";
|
|
2
|
+
import { WervtRole } from "@wervt/core";
|
|
3
|
+
/** What the dev setup needs from the dev server it runs in. */
|
|
4
|
+
export interface DevHost {
|
|
5
|
+
/** The app's directory, with `drizzle/` and `.env`. */
|
|
6
|
+
rootDir: string;
|
|
7
|
+
/** Runs `fn` when the dev server stops. */
|
|
8
|
+
onClose(fn: () => void): void;
|
|
9
|
+
/** Restarts the dev server, which runs the setup again. */
|
|
10
|
+
restart(): void;
|
|
11
|
+
/** Runs `fn` with the dev server's URL once it listens. Without it, nothing is seeded. */
|
|
12
|
+
onListen?(fn: (url: string) => void): void;
|
|
13
|
+
logger: {
|
|
14
|
+
info(message: string): void;
|
|
15
|
+
success(message: string): void;
|
|
16
|
+
warn(message: string, ...args: unknown[]): void;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface DevOptions {
|
|
20
|
+
/** Superuser URL of the local wervt Postgres. */
|
|
21
|
+
adminUrl?: string;
|
|
22
|
+
/** WERVT_DB_SECRET of the local setup. */
|
|
23
|
+
dbSecret?: string;
|
|
24
|
+
electricUrl?: string;
|
|
25
|
+
electricSecret?: string;
|
|
26
|
+
/** The local Garage, for apps with ablage: S3 API and admin API. */
|
|
27
|
+
s3Url?: string;
|
|
28
|
+
garageAdminUrl?: string;
|
|
29
|
+
garageAdminToken?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Runs the app's `db:seed` task (`server/tasks/db/seed.ts`) once its database was just created
|
|
32
|
+
* and migrated. Defaults to true; `false` leaves seeding to `nuxt task run db:seed` (or nitro's).
|
|
33
|
+
*/
|
|
34
|
+
seed?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Who `/_wervt/login` signs in as in local dev (there's no wervt auth locally).
|
|
37
|
+
* Defaults to a dev user with a Twitch account linked. Without `role`, they're the app's owner.
|
|
38
|
+
*/
|
|
39
|
+
user?: {
|
|
40
|
+
id: string;
|
|
41
|
+
email: string;
|
|
42
|
+
name?: string;
|
|
43
|
+
image?: string;
|
|
44
|
+
accounts?: Record<string, string>;
|
|
45
|
+
role?: WervtRole;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The dev server against the local compose stack: provisions the app's role and schema,
|
|
50
|
+
* runs its migrations (again whenever `drizzle/` changes), seeds a new database and
|
|
51
|
+
* points the dev server at the app's database and Electric.
|
|
52
|
+
*/
|
|
53
|
+
export declare function setupDev(host: DevHost, app: string, options: DevOptions, features?: {
|
|
54
|
+
storage?: boolean;
|
|
55
|
+
}): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* App name, normalised to `[a-z0-9-]+`: `WERVT_APP` first, which `wervt deploy` sets for its
|
|
58
|
+
* build, so one codebase deployed as several apps (`--name`) builds each under its own name.
|
|
59
|
+
* Then the module option, then package.json.
|
|
60
|
+
*/
|
|
61
|
+
export declare function resolveAppName(rootDir: string, name?: string): Promise<string>;
|
|
62
|
+
export { SEED_TASK, type SeedOutcome, runDevSeed, unscheduleSeed };
|
package/dist/dev.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { appDatabaseUrl, dbRole, migrateAs, provisionDatabase, storageCredentials } from "./provision.js";
|
|
2
|
+
import { createStorage } from "./storage.js";
|
|
3
|
+
import { SEED_TASK, runDevSeed, unscheduleSeed } from "./seed.js";
|
|
4
|
+
import { existsSync, watch } from "node:fs";
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
// src/dev.ts
|
|
8
|
+
const DEFAULTS = {
|
|
9
|
+
adminUrl: "postgres://postgres:postgres@localhost:5432/wervt",
|
|
10
|
+
dbSecret: "dev-secret",
|
|
11
|
+
electricUrl: "http://localhost:3030",
|
|
12
|
+
electricSecret: "dev-electric-secret",
|
|
13
|
+
s3Url: "http://localhost:3900",
|
|
14
|
+
garageAdminUrl: "http://localhost:3903",
|
|
15
|
+
garageAdminToken: "dev-garage-admin"
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The dev server against the local compose stack: provisions the app's role and schema,
|
|
19
|
+
* runs its migrations (again whenever `drizzle/` changes), seeds a new database and
|
|
20
|
+
* points the dev server at the app's database and Electric.
|
|
21
|
+
*/
|
|
22
|
+
async function setupDev(host, app, options, features = {}) {
|
|
23
|
+
const { logger } = host;
|
|
24
|
+
forgetOwnEnv(host.rootDir);
|
|
25
|
+
const config = {
|
|
26
|
+
adminUrl: process.env.WERVT_PG_ADMIN_URL ?? options.adminUrl ?? DEFAULTS.adminUrl,
|
|
27
|
+
dbSecret: process.env.WERVT_DB_SECRET ?? options.dbSecret ?? DEFAULTS.dbSecret,
|
|
28
|
+
electricUrl: process.env.ELECTRIC_URL ?? options.electricUrl ?? DEFAULTS.electricUrl,
|
|
29
|
+
electricSecret: process.env.ELECTRIC_SECRET ?? options.electricSecret ?? DEFAULTS.electricSecret
|
|
30
|
+
};
|
|
31
|
+
process.env.WERVT_DEV_USER ??= JSON.stringify(options.user ?? {
|
|
32
|
+
id: "dev",
|
|
33
|
+
email: "dev@wervt.localhost",
|
|
34
|
+
name: "Dev",
|
|
35
|
+
accounts: { twitch: "dev" }
|
|
36
|
+
});
|
|
37
|
+
if (features.storage) await setupStorage(host, app, config.dbSecret, options);
|
|
38
|
+
const migrationsFolder = join(host.rootDir, "drizzle");
|
|
39
|
+
if (!existsSync(migrationsFolder)) {
|
|
40
|
+
const watcher = watch(host.rootDir, (_event, file) => {
|
|
41
|
+
if (file !== "drizzle" || !existsSync(migrationsFolder)) return;
|
|
42
|
+
watcher.close();
|
|
43
|
+
logger.info("Migrations added, restarting…");
|
|
44
|
+
host.restart();
|
|
45
|
+
});
|
|
46
|
+
host.onClose(() => watcher.close());
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const provision = {
|
|
50
|
+
app,
|
|
51
|
+
adminUrl: config.adminUrl,
|
|
52
|
+
secret: config.dbSecret,
|
|
53
|
+
migrationsFolder
|
|
54
|
+
};
|
|
55
|
+
const given = process.env.DATABASE_URL && process.env.WERVT_DB_SCHEMA ? {
|
|
56
|
+
url: process.env.DATABASE_URL,
|
|
57
|
+
schema: process.env.WERVT_DB_SCHEMA
|
|
58
|
+
} : void 0;
|
|
59
|
+
const database = given?.schema ?? dbRole(app);
|
|
60
|
+
let serverUrl;
|
|
61
|
+
let seedPending = false;
|
|
62
|
+
const seed = async () => {
|
|
63
|
+
if (!seedPending || !serverUrl) return;
|
|
64
|
+
seedPending = false;
|
|
65
|
+
try {
|
|
66
|
+
if ((await runDevSeed(serverUrl)).status === "seeded") logger.success(`Seeded \`${database}\` (${SEED_TASK})`);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
logger.warn(`\`${SEED_TASK}\` failed:`, e instanceof Error ? e.message : e);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
if (options.seed !== false) host.onListen?.((url) => {
|
|
72
|
+
serverUrl = url;
|
|
73
|
+
seed();
|
|
74
|
+
});
|
|
75
|
+
const migrate = async () => {
|
|
76
|
+
try {
|
|
77
|
+
const { fresh } = given ? await migrateAs({
|
|
78
|
+
...given,
|
|
79
|
+
migrationsFolder
|
|
80
|
+
}) : await provisionDatabase(provision);
|
|
81
|
+
logger.success(`Database \`${database}\` is up to date`);
|
|
82
|
+
if (fresh && options.seed !== false) {
|
|
83
|
+
seedPending = true;
|
|
84
|
+
seed();
|
|
85
|
+
}
|
|
86
|
+
} catch (e) {
|
|
87
|
+
logger.warn(given ? `Could not migrate \`${database}\`.` : `Could not migrate \`${database}\`. Is the wervt stack running (docker compose up -d)?`, e instanceof Error ? e.message : e);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
await migrate();
|
|
91
|
+
const own = {
|
|
92
|
+
DATABASE_URL: await appDatabaseUrl(provision),
|
|
93
|
+
WERVT_DB_SCHEMA: dbRole(app),
|
|
94
|
+
ELECTRIC_URL: config.electricUrl,
|
|
95
|
+
ELECTRIC_SECRET: config.electricSecret
|
|
96
|
+
};
|
|
97
|
+
const set = Object.fromEntries(Object.entries(own).filter(([key]) => process.env[key] === void 0));
|
|
98
|
+
Object.assign(process.env, set);
|
|
99
|
+
process.env[OWN_ENV] = JSON.stringify(set);
|
|
100
|
+
let timer;
|
|
101
|
+
const watcher = watch(migrationsFolder, { recursive: true }, (_event, file) => {
|
|
102
|
+
if (!file?.endsWith(".sql")) return;
|
|
103
|
+
clearTimeout(timer);
|
|
104
|
+
timer = setTimeout(() => {
|
|
105
|
+
logger.info("Migrations changed, migrating…");
|
|
106
|
+
migrate();
|
|
107
|
+
}, 300);
|
|
108
|
+
});
|
|
109
|
+
host.onClose(() => watcher.close());
|
|
110
|
+
}
|
|
111
|
+
const OWN_ENV = "WERVT_DEV_OWN_ENV";
|
|
112
|
+
function forgetOwnEnv(rootDir) {
|
|
113
|
+
const own = process.env[OWN_ENV];
|
|
114
|
+
if (!own) return;
|
|
115
|
+
for (const [key, value] of Object.entries(JSON.parse(own))) if (process.env[key] === value) delete process.env[key];
|
|
116
|
+
delete process.env[OWN_ENV];
|
|
117
|
+
const file = join(rootDir, ".env");
|
|
118
|
+
if (existsSync(file)) process.loadEnvFile(file);
|
|
119
|
+
}
|
|
120
|
+
/** The app's bucket in the local Garage, and WERVT_S3_* for ablage, like in production. */
|
|
121
|
+
async function setupStorage(host, app, secret, options) {
|
|
122
|
+
const { logger } = host;
|
|
123
|
+
const s3Url = process.env.WERVT_S3_URL ?? options.s3Url ?? DEFAULTS.s3Url;
|
|
124
|
+
const storage = createStorage({
|
|
125
|
+
adminUrl: options.garageAdminUrl ?? DEFAULTS.garageAdminUrl,
|
|
126
|
+
adminToken: process.env.WERVT_GARAGE_ADMIN_TOKEN ?? options.garageAdminToken ?? DEFAULTS.garageAdminToken,
|
|
127
|
+
s3Url,
|
|
128
|
+
secret
|
|
129
|
+
});
|
|
130
|
+
try {
|
|
131
|
+
await storage.ensureLayout();
|
|
132
|
+
const { bucket } = await storage.provision(app, "*");
|
|
133
|
+
logger.success(`Storage bucket \`${bucket}\` is ready`);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
return logger.warn("Could not set up storage. Is the wervt stack running (docker compose up -d)?", e instanceof Error ? e.message : e);
|
|
136
|
+
}
|
|
137
|
+
const creds = await storageCredentials(app, secret);
|
|
138
|
+
process.env.WERVT_S3_URL ??= s3Url;
|
|
139
|
+
process.env.WERVT_S3_PUBLIC_URL ??= s3Url;
|
|
140
|
+
process.env.WERVT_S3_REGION ??= "garage";
|
|
141
|
+
process.env.WERVT_S3_BUCKET ??= creds.bucket;
|
|
142
|
+
process.env.WERVT_S3_ACCESS_KEY_ID ??= creds.accessKeyId;
|
|
143
|
+
process.env.WERVT_S3_SECRET_ACCESS_KEY ??= creds.secretAccessKey;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* App name, normalised to `[a-z0-9-]+`: `WERVT_APP` first, which `wervt deploy` sets for its
|
|
147
|
+
* build, so one codebase deployed as several apps (`--name`) builds each under its own name.
|
|
148
|
+
* Then the module option, then package.json.
|
|
149
|
+
*/
|
|
150
|
+
async function resolveAppName(rootDir, name) {
|
|
151
|
+
name = process.env.WERVT_APP || name;
|
|
152
|
+
if (!name) {
|
|
153
|
+
const pkg = JSON.parse(await readFile(join(rootDir, "package.json"), "utf8").catch(() => "{}"));
|
|
154
|
+
name = String(pkg.name ?? "app").replace(/^@[^/]+\//, "");
|
|
155
|
+
}
|
|
156
|
+
return name.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
157
|
+
}
|
|
158
|
+
export { SEED_TASK, resolveAppName, runDevSeed, setupDev, unscheduleSeed };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { DEFAULT_ROLE_TIMEOUTS, POOLER_ROLE, ProvisionOptions, RoleTimeouts, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials } from "./provision.js";
|
|
2
|
+
import { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, ObjectDetails, ObjectListing, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, companionKeyOf, createStorage, metaKeyOf, parseObjectList } from "./storage.js";
|
|
3
|
+
export { ABANDONED_UPLOAD_GRACE_MS, DEFAULT_ROLE_TIMEOUTS, MAX_KEY_BYTES, ObjectDetails, ObjectListing, POOLER_ROLE, ProvisionOptions, RoleTimeouts, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, appDatabaseUrl, companionKeyOf, createStorage, dbPassword, dbRole, dropDatabase, hmac, metaKeyOf, migrateAs, parseDuration, parseObjectList, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { DEFAULT_ROLE_TIMEOUTS, POOLER_ROLE, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials } from "./provision.js";
|
|
2
|
+
import { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, StorageError, companionKeyOf, createStorage, metaKeyOf, parseObjectList } from "./storage.js";
|
|
3
|
+
export { ABANDONED_UPLOAD_GRACE_MS, DEFAULT_ROLE_TIMEOUTS, MAX_KEY_BYTES, POOLER_ROLE, StorageError, appDatabaseUrl, companionKeyOf, createStorage, dbPassword, dbRole, dropDatabase, hmac, metaKeyOf, migrateAs, parseDuration, parseObjectList, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export interface ProvisionOptions {
|
|
2
|
+
/** App name, `[a-z0-9-]+`. */
|
|
3
|
+
app: string;
|
|
4
|
+
/** Superuser connection URL to the `wervt` database. */
|
|
5
|
+
adminUrl: string;
|
|
6
|
+
/** Secret the role password is derived from (WERVT_DB_SECRET). */
|
|
7
|
+
secret: string;
|
|
8
|
+
/** Drizzle migrations folder. Skipped when undefined. */
|
|
9
|
+
migrationsFolder?: string;
|
|
10
|
+
/** Timeouts for the app's role; DEFAULT_ROLE_TIMEOUTS where not given. */
|
|
11
|
+
timeouts?: Partial<RoleTimeouts>;
|
|
12
|
+
}
|
|
13
|
+
/** Postgres durations (`30s`, `500ms`, `2min`) for the app's role; `0` turns one off. */
|
|
14
|
+
export interface RoleTimeouts {
|
|
15
|
+
/** Longest a single statement may run. */
|
|
16
|
+
statement: string;
|
|
17
|
+
/** Longest a transaction may sit idle, holding its locks and its pooled connection. */
|
|
18
|
+
idleInTransaction: string;
|
|
19
|
+
}
|
|
20
|
+
export declare const DEFAULT_ROLE_TIMEOUTS: RoleTimeouts;
|
|
21
|
+
/** A Postgres duration as the env gives it, e.g. `WERVT_STATEMENT_TIMEOUT`; the fallback when empty. */
|
|
22
|
+
export declare function parseDuration(value: string | undefined, fallback: string, name?: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Sets the timeouts on an existing app role, for apps that aren't redeployed. New sessions get
|
|
25
|
+
* them; open ones keep theirs. Returns false when the app has no role.
|
|
26
|
+
*/
|
|
27
|
+
export declare function setRoleTimeouts({ app, adminUrl, timeouts }: Pick<ProvisionOptions, 'app' | 'adminUrl' | 'timeouts'>): Promise<boolean>;
|
|
28
|
+
export declare function dbRole(app: string): string;
|
|
29
|
+
export declare function hmac(secret: string, message: string): Promise<string>;
|
|
30
|
+
/** The app's Garage bucket and S3 key, derived from WERVT_DB_SECRET like role passwords. */
|
|
31
|
+
export declare function storageCredentials(app: string, secret: string): Promise<{
|
|
32
|
+
bucket: string;
|
|
33
|
+
accessKeyId: string;
|
|
34
|
+
secretAccessKey: string;
|
|
35
|
+
}>;
|
|
36
|
+
export declare function dbPassword(app: string, secret: string): Promise<string>;
|
|
37
|
+
/** Connection URL for the app's own role, on the same host and database as `adminUrl`. */
|
|
38
|
+
export declare function appDatabaseUrl({ app, adminUrl, secret }: ProvisionOptions): Promise<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Creates (or updates) the app's role and schema and runs its Drizzle migrations as
|
|
41
|
+
* that role. Each app gets a role and schema named `app_<name>`; the role's
|
|
42
|
+
* search_path is its own schema, it can't create schemas or touch `public`, and it can
|
|
43
|
+
* only connect to the wervt database.
|
|
44
|
+
*
|
|
45
|
+
* `fresh` is true when no migration had been applied before, so `nuxt dev` knows when to seed.
|
|
46
|
+
*/
|
|
47
|
+
export declare function provisionDatabase(options: ProvisionOptions): Promise<{
|
|
48
|
+
fresh: boolean;
|
|
49
|
+
}>;
|
|
50
|
+
/**
|
|
51
|
+
* Runs the Drizzle migrations as the app's own role, into its schema. The role needs CREATE on
|
|
52
|
+
* the database while this runs (see `provisionDatabase`). `fresh` is true when no migration had
|
|
53
|
+
* been applied before.
|
|
54
|
+
*/
|
|
55
|
+
export declare function migrateAs({ url, schema, migrationsFolder }: {
|
|
56
|
+
url: string;
|
|
57
|
+
schema: string;
|
|
58
|
+
migrationsFolder: string;
|
|
59
|
+
}): Promise<{
|
|
60
|
+
fresh: boolean;
|
|
61
|
+
}>;
|
|
62
|
+
/** The role PgBouncer signs in with to look up app roles' password hashes (`auth_query`). */
|
|
63
|
+
export declare const POOLER_ROLE = "pgbouncer";
|
|
64
|
+
/**
|
|
65
|
+
* Prepares Postgres for PgBouncer in front of the app isolates: a login role for PgBouncer and
|
|
66
|
+
* `pgbouncer.get_auth()`, which hands out the SCRAM hashes of app roles (`app_*`) only, never
|
|
67
|
+
* those of the superuser or other roles. App passwords are derived, not stored anywhere
|
|
68
|
+
* PgBouncer could read them, so it has to ask Postgres.
|
|
69
|
+
*/
|
|
70
|
+
export declare function setupPooler({ adminUrl, password }: {
|
|
71
|
+
adminUrl: string;
|
|
72
|
+
password: string;
|
|
73
|
+
}): Promise<void>;
|
|
74
|
+
/** Drops the app's schema (all its data) and role. */
|
|
75
|
+
export declare function dropDatabase({ app, adminUrl }: Pick<ProvisionOptions, 'app' | 'adminUrl'>): Promise<void>;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { drizzle } from "drizzle-orm/postgres-js";
|
|
2
|
+
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
3
|
+
import postgres from "postgres";
|
|
4
|
+
// src/provision.ts
|
|
5
|
+
const DEFAULT_ROLE_TIMEOUTS = {
|
|
6
|
+
statement: "30s",
|
|
7
|
+
idleInTransaction: "60s"
|
|
8
|
+
};
|
|
9
|
+
const DURATION_RE = /^\d+(?:ms|s|min|h)?$/;
|
|
10
|
+
/** A Postgres duration as the env gives it, e.g. `WERVT_STATEMENT_TIMEOUT`; the fallback when empty. */
|
|
11
|
+
function parseDuration(value, fallback, name = "timeout") {
|
|
12
|
+
const trimmed = value?.trim();
|
|
13
|
+
if (!trimmed) return fallback;
|
|
14
|
+
if (!DURATION_RE.test(trimmed)) throw new Error(`${name} must be a duration like 30s, 500ms or 2min (0 for none)`);
|
|
15
|
+
return trimmed;
|
|
16
|
+
}
|
|
17
|
+
function roleTimeouts(timeouts) {
|
|
18
|
+
const merged = {
|
|
19
|
+
...DEFAULT_ROLE_TIMEOUTS,
|
|
20
|
+
...timeouts
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
statement: parseDuration(merged.statement, DEFAULT_ROLE_TIMEOUTS.statement),
|
|
24
|
+
idleInTransaction: parseDuration(merged.idleInTransaction, DEFAULT_ROLE_TIMEOUTS.idleInTransaction)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
async function applyRoleTimeouts(admin, role, timeouts) {
|
|
28
|
+
await admin.unsafe(`alter role "${role}" set statement_timeout = '${timeouts.statement}'`);
|
|
29
|
+
await admin.unsafe(`alter role "${role}" set idle_in_transaction_session_timeout = '${timeouts.idleInTransaction}'`);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Sets the timeouts on an existing app role, for apps that aren't redeployed. New sessions get
|
|
33
|
+
* them; open ones keep theirs. Returns false when the app has no role.
|
|
34
|
+
*/
|
|
35
|
+
async function setRoleTimeouts({ app, adminUrl, timeouts }) {
|
|
36
|
+
const role = dbRole(app);
|
|
37
|
+
const admin = postgres(adminUrl, {
|
|
38
|
+
max: 1,
|
|
39
|
+
onnotice: () => {}
|
|
40
|
+
});
|
|
41
|
+
try {
|
|
42
|
+
const [row] = await admin`select exists(select 1 from pg_roles where rolname = ${role})`;
|
|
43
|
+
if (!row?.exists) return false;
|
|
44
|
+
await applyRoleTimeouts(admin, role, roleTimeouts(timeouts));
|
|
45
|
+
return true;
|
|
46
|
+
} finally {
|
|
47
|
+
await admin.end();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function dbRole(app) {
|
|
51
|
+
return `app_${app.replaceAll("-", "_")}`;
|
|
52
|
+
}
|
|
53
|
+
async function hmac(secret, message) {
|
|
54
|
+
const key = await crypto.subtle.importKey("raw", new TextEncoder().encode(secret), {
|
|
55
|
+
name: "HMAC",
|
|
56
|
+
hash: "SHA-256"
|
|
57
|
+
}, false, ["sign"]);
|
|
58
|
+
const sig = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(message));
|
|
59
|
+
return Array.from(new Uint8Array(sig), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
60
|
+
}
|
|
61
|
+
/** The app's Garage bucket and S3 key, derived from WERVT_DB_SECRET like role passwords. */
|
|
62
|
+
async function storageCredentials(app, secret) {
|
|
63
|
+
return {
|
|
64
|
+
bucket: `app-${app}`,
|
|
65
|
+
accessKeyId: `GK${(await hmac(secret, `s3-id:${app}`)).slice(0, 24)}`,
|
|
66
|
+
secretAccessKey: await hmac(secret, `s3-secret:${app}`)
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function dbPassword(app, secret) {
|
|
70
|
+
return hmac(secret, app);
|
|
71
|
+
}
|
|
72
|
+
/** Connection URL for the app's own role, on the same host and database as `adminUrl`. */
|
|
73
|
+
async function appDatabaseUrl({ app, adminUrl, secret }) {
|
|
74
|
+
const url = new URL(adminUrl);
|
|
75
|
+
url.username = dbRole(app);
|
|
76
|
+
url.password = await dbPassword(app, secret);
|
|
77
|
+
return url.href;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates (or updates) the app's role and schema and runs its Drizzle migrations as
|
|
81
|
+
* that role. Each app gets a role and schema named `app_<name>`; the role's
|
|
82
|
+
* search_path is its own schema, it can't create schemas or touch `public`, and it can
|
|
83
|
+
* only connect to the wervt database.
|
|
84
|
+
*
|
|
85
|
+
* `fresh` is true when no migration had been applied before, so `nuxt dev` knows when to seed.
|
|
86
|
+
*/
|
|
87
|
+
async function provisionDatabase(options) {
|
|
88
|
+
const { app, adminUrl, secret, migrationsFolder } = options;
|
|
89
|
+
const role = dbRole(app);
|
|
90
|
+
const password = await dbPassword(app, secret);
|
|
91
|
+
const database = new URL(adminUrl).pathname.slice(1);
|
|
92
|
+
const admin = postgres(adminUrl, {
|
|
93
|
+
max: 1,
|
|
94
|
+
onnotice: () => {}
|
|
95
|
+
});
|
|
96
|
+
try {
|
|
97
|
+
const [row] = await admin`select exists(select 1 from pg_roles where rolname = ${role})`;
|
|
98
|
+
const exists = Boolean(row?.exists);
|
|
99
|
+
await admin.unsafe(`${exists ? "alter" : "create"} role "${role}" login password '${password}' connection limit 10`);
|
|
100
|
+
await admin.unsafe(`create schema if not exists "${role}" authorization "${role}"`);
|
|
101
|
+
await claimSchema(admin, role);
|
|
102
|
+
await admin.unsafe(`alter role "${role}" set search_path = "${role}"`);
|
|
103
|
+
await applyRoleTimeouts(admin, role, roleTimeouts(options.timeouts));
|
|
104
|
+
for (const db of [
|
|
105
|
+
database,
|
|
106
|
+
"postgres",
|
|
107
|
+
"template1"
|
|
108
|
+
]) await admin.unsafe(`revoke all on database "${db}" from public`);
|
|
109
|
+
await admin.unsafe(`grant connect on database "${database}" to "${role}"`);
|
|
110
|
+
await admin.unsafe(`revoke all on schema public from public`);
|
|
111
|
+
if (!migrationsFolder) return { fresh: false };
|
|
112
|
+
await admin.unsafe(`grant create on database "${database}" to "${role}"`);
|
|
113
|
+
try {
|
|
114
|
+
return await migrateAs({
|
|
115
|
+
url: await appDatabaseUrl(options),
|
|
116
|
+
schema: role,
|
|
117
|
+
migrationsFolder
|
|
118
|
+
});
|
|
119
|
+
} finally {
|
|
120
|
+
await admin.unsafe(`revoke create on database "${database}" from "${role}"`);
|
|
121
|
+
}
|
|
122
|
+
} finally {
|
|
123
|
+
await admin.end();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Runs the Drizzle migrations as the app's own role, into its schema. The role needs CREATE on
|
|
128
|
+
* the database while this runs (see `provisionDatabase`). `fresh` is true when no migration had
|
|
129
|
+
* been applied before.
|
|
130
|
+
*/
|
|
131
|
+
async function migrateAs({ url, schema, migrationsFolder }) {
|
|
132
|
+
const client = postgres(url, {
|
|
133
|
+
max: 1,
|
|
134
|
+
onnotice: () => {}
|
|
135
|
+
});
|
|
136
|
+
try {
|
|
137
|
+
const table = `"${schema}"."__drizzle_migrations"`;
|
|
138
|
+
const [state] = await client`
|
|
139
|
+
select to_regclass(${table}) is not null as exists`;
|
|
140
|
+
let fresh = !state?.exists;
|
|
141
|
+
if (!fresh) {
|
|
142
|
+
const [row] = await client.unsafe(`select exists(select 1 from ${table}) as applied`);
|
|
143
|
+
fresh = !row?.applied;
|
|
144
|
+
}
|
|
145
|
+
await client.unsafe("set statement_timeout = 0; set idle_in_transaction_session_timeout = 0");
|
|
146
|
+
await migrate(drizzle({ client }), {
|
|
147
|
+
migrationsFolder,
|
|
148
|
+
migrationsSchema: schema
|
|
149
|
+
});
|
|
150
|
+
return { fresh };
|
|
151
|
+
} finally {
|
|
152
|
+
await client.end();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Gives the role its schema and everything in it back. A dump restored with `--no-owner`
|
|
157
|
+
* (Coolify's backups) leaves all of it owned by the restoring superuser.
|
|
158
|
+
*/
|
|
159
|
+
async function claimSchema(admin, role) {
|
|
160
|
+
await admin.unsafe(`alter schema "${role}" owner to "${role}"`);
|
|
161
|
+
const objects = await admin`
|
|
162
|
+
select case c.relkind when 'S' then 'sequence' when 'v' then 'view'
|
|
163
|
+
when 'm' then 'materialized view' when 'f' then 'foreign table' else 'table' end as kind,
|
|
164
|
+
format('%I.%I', n.nspname, c.relname) as name
|
|
165
|
+
from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
|
166
|
+
where n.nspname = ${role} and c.relkind in ('r', 'p', 'v', 'm', 'f', 'S')
|
|
167
|
+
and c.relowner <> ${role}::regrole
|
|
168
|
+
-- identity and serial sequences follow their table
|
|
169
|
+
and not exists (select 1 from pg_depend d where d.classid = 'pg_class'::regclass
|
|
170
|
+
and d.objid = c.oid and d.deptype in ('a', 'i'))
|
|
171
|
+
union all
|
|
172
|
+
select case t.typtype when 'd' then 'domain' else 'type' end, format('%I.%I', n.nspname, t.typname)
|
|
173
|
+
from pg_type t join pg_namespace n on n.oid = t.typnamespace
|
|
174
|
+
where n.nspname = ${role} and t.typtype in ('e', 'd')
|
|
175
|
+
and t.typowner <> ${role}::regrole
|
|
176
|
+
union all
|
|
177
|
+
select case p.prokind when 'p' then 'procedure' when 'a' then 'aggregate' else 'function' end,
|
|
178
|
+
p.oid::regprocedure::text
|
|
179
|
+
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
|
180
|
+
where n.nspname = ${role} and p.proowner <> ${role}::regrole`;
|
|
181
|
+
for (const { kind, name } of objects) await admin.unsafe(`alter ${kind} ${name} owner to "${role}"`);
|
|
182
|
+
}
|
|
183
|
+
/** The role PgBouncer signs in with to look up app roles' password hashes (`auth_query`). */
|
|
184
|
+
const POOLER_ROLE = "pgbouncer";
|
|
185
|
+
/**
|
|
186
|
+
* Prepares Postgres for PgBouncer in front of the app isolates: a login role for PgBouncer and
|
|
187
|
+
* `pgbouncer.get_auth()`, which hands out the SCRAM hashes of app roles (`app_*`) only, never
|
|
188
|
+
* those of the superuser or other roles. App passwords are derived, not stored anywhere
|
|
189
|
+
* PgBouncer could read them, so it has to ask Postgres.
|
|
190
|
+
*/
|
|
191
|
+
async function setupPooler({ adminUrl, password }) {
|
|
192
|
+
const database = new URL(adminUrl).pathname.slice(1);
|
|
193
|
+
const admin = postgres(adminUrl, {
|
|
194
|
+
max: 1,
|
|
195
|
+
onnotice: () => {}
|
|
196
|
+
});
|
|
197
|
+
try {
|
|
198
|
+
const [row] = await admin`select exists(select 1 from pg_roles where rolname = ${POOLER_ROLE})`;
|
|
199
|
+
await admin.unsafe(`${row?.exists ? "alter" : "create"} role "${POOLER_ROLE}" login password '${password.replaceAll("'", "''")}'`);
|
|
200
|
+
await admin.unsafe(`grant connect on database "${database}" to "${POOLER_ROLE}"`);
|
|
201
|
+
await admin.unsafe(`create schema if not exists "${POOLER_ROLE}"`);
|
|
202
|
+
await admin.unsafe(`revoke all on schema "${POOLER_ROLE}" from public`);
|
|
203
|
+
await admin.unsafe(`grant usage on schema "${POOLER_ROLE}" to "${POOLER_ROLE}"`);
|
|
204
|
+
await admin.unsafe(`
|
|
205
|
+
create or replace function "${POOLER_ROLE}".get_auth(p_usename text)
|
|
206
|
+
returns table(usename name, passwd text)
|
|
207
|
+
language sql security definer set search_path = pg_catalog as $$
|
|
208
|
+
select usename, passwd from pg_shadow
|
|
209
|
+
where usename = p_usename and usename like 'app_%' and not usesuper
|
|
210
|
+
$$`);
|
|
211
|
+
await admin.unsafe(`revoke all on function "${POOLER_ROLE}".get_auth(text) from public`);
|
|
212
|
+
await admin.unsafe(`grant execute on function "${POOLER_ROLE}".get_auth(text) to "${POOLER_ROLE}"`);
|
|
213
|
+
} finally {
|
|
214
|
+
await admin.end();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** Drops the app's schema (all its data) and role. */
|
|
218
|
+
async function dropDatabase({ app, adminUrl }) {
|
|
219
|
+
const role = dbRole(app);
|
|
220
|
+
const database = new URL(adminUrl).pathname.slice(1);
|
|
221
|
+
const admin = postgres(adminUrl, {
|
|
222
|
+
max: 1,
|
|
223
|
+
onnotice: () => {}
|
|
224
|
+
});
|
|
225
|
+
try {
|
|
226
|
+
await admin`select pg_terminate_backend(pid) from pg_stat_activity where usename = ${role}`;
|
|
227
|
+
await admin.unsafe(`drop schema if exists "${role}" cascade`);
|
|
228
|
+
const [row] = await admin`select exists(select 1 from pg_roles where rolname = ${role})`;
|
|
229
|
+
if (Boolean(row?.exists)) {
|
|
230
|
+
await admin.unsafe(`revoke all on database "${database}" from "${role}"`);
|
|
231
|
+
await admin.unsafe(`drop role "${role}"`);
|
|
232
|
+
}
|
|
233
|
+
} finally {
|
|
234
|
+
await admin.end();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
export { DEFAULT_ROLE_TIMEOUTS, POOLER_ROLE, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
package/dist/seed.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** The conventional seed task: `server/tasks/db/seed.ts`. */
|
|
2
|
+
export declare const SEED_TASK = "db:seed";
|
|
3
|
+
type Schedules = Record<string, string | string[]>;
|
|
4
|
+
/**
|
|
5
|
+
* `nitro.scheduledTasks` without the seed task, and the crons it was listed under. Seeding fills
|
|
6
|
+
* an empty database on request; on a schedule it would run against production data.
|
|
7
|
+
*/
|
|
8
|
+
export declare function unscheduleSeed(schedules: Schedules): {
|
|
9
|
+
schedules: Schedules;
|
|
10
|
+
removed: string[];
|
|
11
|
+
};
|
|
12
|
+
export type SeedOutcome = {
|
|
13
|
+
status: 'seeded';
|
|
14
|
+
result: unknown;
|
|
15
|
+
} | {
|
|
16
|
+
status: 'missing';
|
|
17
|
+
};
|
|
18
|
+
export interface DevSeedOptions {
|
|
19
|
+
fetch?: typeof fetch;
|
|
20
|
+
/** How long to wait for the dev server to finish its first build. */
|
|
21
|
+
timeout?: number;
|
|
22
|
+
interval?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Runs `db:seed` through the Nitro dev server's task route, once the server answers. The dev
|
|
26
|
+
* server is still building when it starts listening, so its task list is polled first; that
|
|
27
|
+
* also tells whether the app has a seed task at all.
|
|
28
|
+
*/
|
|
29
|
+
export declare function runDevSeed(baseUrl: string, { fetch: request, timeout, interval }?: DevSeedOptions): Promise<SeedOutcome>;
|
|
30
|
+
export {};
|
package/dist/seed.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/seed.ts
|
|
2
|
+
/** The conventional seed task: `server/tasks/db/seed.ts`. */
|
|
3
|
+
const SEED_TASK = "db:seed";
|
|
4
|
+
/**
|
|
5
|
+
* `nitro.scheduledTasks` without the seed task, and the crons it was listed under. Seeding fills
|
|
6
|
+
* an empty database on request; on a schedule it would run against production data.
|
|
7
|
+
*/
|
|
8
|
+
function unscheduleSeed(schedules) {
|
|
9
|
+
const kept = {};
|
|
10
|
+
const removed = [];
|
|
11
|
+
for (const [cron, entry] of Object.entries(schedules)) {
|
|
12
|
+
const tasks = [entry].flat();
|
|
13
|
+
const rest = tasks.filter((task) => task !== SEED_TASK);
|
|
14
|
+
if (rest.length < tasks.length) removed.push(cron);
|
|
15
|
+
if (rest.length > 0) kept[cron] = Array.isArray(entry) ? rest : rest[0];
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
schedules: kept,
|
|
19
|
+
removed
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Runs `db:seed` through the Nitro dev server's task route, once the server answers. The dev
|
|
24
|
+
* server is still building when it starts listening, so its task list is polled first; that
|
|
25
|
+
* also tells whether the app has a seed task at all.
|
|
26
|
+
*/
|
|
27
|
+
async function runDevSeed(baseUrl, { fetch: request = fetch, timeout = 12e4, interval = 500 } = {}) {
|
|
28
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
29
|
+
const deadline = Date.now() + timeout;
|
|
30
|
+
let tasks;
|
|
31
|
+
while (!tasks) {
|
|
32
|
+
const response = await request(`${base}/_nitro/tasks`).catch(() => void 0);
|
|
33
|
+
const body = response?.ok ? await response.json().catch(() => void 0) : void 0;
|
|
34
|
+
if (body && typeof body === "object" && "tasks" in body) tasks = body.tasks;
|
|
35
|
+
else if (Date.now() >= deadline) throw new Error("the dev server did not answer in time");
|
|
36
|
+
else await new Promise((resolve) => setTimeout(resolve, interval));
|
|
37
|
+
}
|
|
38
|
+
if (!("db:seed" in tasks)) return { status: "missing" };
|
|
39
|
+
const response = await request(`${base}/_nitro/tasks/${SEED_TASK}`, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: { "content-type": "application/json" },
|
|
42
|
+
body: JSON.stringify({ payload: {} })
|
|
43
|
+
});
|
|
44
|
+
const body = await response.json().catch(() => ({}));
|
|
45
|
+
if (!response.ok) throw new Error(body.message ?? `${response.status} ${response.statusText}`);
|
|
46
|
+
return {
|
|
47
|
+
status: "seeded",
|
|
48
|
+
result: body.result
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export { SEED_TASK, runDevSeed, unscheduleSeed };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export interface StorageOptions {
|
|
2
|
+
/** Garage's admin API, e.g. http://garage:3903 */
|
|
3
|
+
adminUrl: string;
|
|
4
|
+
adminToken: string;
|
|
5
|
+
/** Garage's S3 API as the control plane reaches it, e.g. http://garage:3900 */
|
|
6
|
+
s3Url: string;
|
|
7
|
+
secret: string;
|
|
8
|
+
}
|
|
9
|
+
export interface StorageUsage {
|
|
10
|
+
bucket: string;
|
|
11
|
+
objects: number;
|
|
12
|
+
bytes: number;
|
|
13
|
+
/** Multipart uploads started but never completed or aborted. */
|
|
14
|
+
unfinishedUploads: number;
|
|
15
|
+
/** The bucket's size limit in bytes, null for none. */
|
|
16
|
+
maxBytes: number | null;
|
|
17
|
+
}
|
|
18
|
+
export interface StorageQuota {
|
|
19
|
+
/** Bytes the bucket may hold; null for no limit. Garage refuses uploads past it. */
|
|
20
|
+
maxBytes: number | null;
|
|
21
|
+
}
|
|
22
|
+
export declare class StorageError extends Error {}
|
|
23
|
+
/**
|
|
24
|
+
* How old abandoned direct-upload bytes must be before the sweep deletes them. The browser calls
|
|
25
|
+
* `completeUpload()` right after its `PUT` finishes, so a day leaves any upload in flight alone;
|
|
26
|
+
* it matches the lifecycle rule for multipart uploads.
|
|
27
|
+
*/
|
|
28
|
+
export declare const ABANDONED_UPLOAD_GRACE_MS: number;
|
|
29
|
+
/** ablage's S3 layout: `<group>/data/<id>` holds the bytes, `<group>/meta/<id>` the metadata. */
|
|
30
|
+
export declare function metaKeyOf(key: string): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Objects of a ListObjectsV2 page, its folders (listed with a delimiter), and the token for the
|
|
33
|
+
* next page.
|
|
34
|
+
*/
|
|
35
|
+
export declare function parseObjectList(xml: string): {
|
|
36
|
+
objects: {
|
|
37
|
+
key: string;
|
|
38
|
+
lastModified: number;
|
|
39
|
+
size: number;
|
|
40
|
+
}[];
|
|
41
|
+
prefixes: string[];
|
|
42
|
+
next: string | undefined;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* ablage keeps a file as bytes and metadata, which only make sense together: the metadata key
|
|
46
|
+
* for bytes, the bytes for metadata, undefined for anything else.
|
|
47
|
+
*/
|
|
48
|
+
export declare function companionKeyOf(key: string): string | undefined;
|
|
49
|
+
export interface StoredObject {
|
|
50
|
+
key: string;
|
|
51
|
+
size: number;
|
|
52
|
+
/** ISO time. */
|
|
53
|
+
lastModified: string;
|
|
54
|
+
}
|
|
55
|
+
export interface ObjectListing {
|
|
56
|
+
prefix: string;
|
|
57
|
+
/** Prefixes one level below `prefix`, ending in `/`. */
|
|
58
|
+
folders: string[];
|
|
59
|
+
files: StoredObject[];
|
|
60
|
+
/** Pass as `token` for the next page. */
|
|
61
|
+
next?: string;
|
|
62
|
+
}
|
|
63
|
+
export interface ObjectDetails extends StoredObject {
|
|
64
|
+
contentType: string | null;
|
|
65
|
+
/** `x-amz-meta-*` headers, without the prefix. */
|
|
66
|
+
metadata: Record<string, string>;
|
|
67
|
+
}
|
|
68
|
+
/** S3's limit for object keys, in bytes. */
|
|
69
|
+
export declare const MAX_KEY_BYTES = 1024;
|
|
70
|
+
export declare function createStorage(options: StorageOptions): {
|
|
71
|
+
ensureLayout: () => Promise<void>;
|
|
72
|
+
provision: (app: string, appOrigins: string | string[], quota?: StorageQuota) => Promise<{
|
|
73
|
+
bucket: string;
|
|
74
|
+
}>;
|
|
75
|
+
setQuota: (app: string, quota: StorageQuota) => Promise<boolean>;
|
|
76
|
+
usage: (app: string) => Promise<StorageUsage | undefined>;
|
|
77
|
+
list: (app: string, { prefix, token, limit }?: {
|
|
78
|
+
prefix?: string;
|
|
79
|
+
token?: string;
|
|
80
|
+
limit?: number;
|
|
81
|
+
}) => Promise<ObjectListing | undefined>;
|
|
82
|
+
describe: (app: string, key: string) => Promise<ObjectDetails | undefined>;
|
|
83
|
+
downloadUrl: (app: string, key: string, { publicUrl, expiresIn }: {
|
|
84
|
+
publicUrl: string;
|
|
85
|
+
expiresIn?: number;
|
|
86
|
+
}) => Promise<string>;
|
|
87
|
+
remove: (app: string, key: string) => Promise<{
|
|
88
|
+
deleted: string[];
|
|
89
|
+
} | undefined>;
|
|
90
|
+
sweepAbandonedUploads: (app: string, { graceMs, now }?: {
|
|
91
|
+
graceMs?: number | undefined;
|
|
92
|
+
now?: number | undefined;
|
|
93
|
+
}) => Promise<{
|
|
94
|
+
deleted: string[];
|
|
95
|
+
}>;
|
|
96
|
+
hasBucket: (app: string) => Promise<boolean>;
|
|
97
|
+
drop: (app: string) => Promise<void>;
|
|
98
|
+
};
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { storageCredentials } from "./provision.js";
|
|
2
|
+
import { AwsClient } from "aws4fetch";
|
|
3
|
+
// src/storage.ts
|
|
4
|
+
var StorageError = class extends Error {};
|
|
5
|
+
/**
|
|
6
|
+
* ablage marks bytes uploaded straight from browsers (`x-amz-meta-ablage-direct-upload`). Its
|
|
7
|
+
* metadata object (`<group>/meta/<id>`) is only written once `completeUpload()` has checked them,
|
|
8
|
+
* so marked bytes without one belong to an upload that was never completed.
|
|
9
|
+
*/
|
|
10
|
+
const DIRECT_UPLOAD_MARKER = "x-amz-meta-ablage-direct-upload";
|
|
11
|
+
/**
|
|
12
|
+
* How old abandoned direct-upload bytes must be before the sweep deletes them. The browser calls
|
|
13
|
+
* `completeUpload()` right after its `PUT` finishes, so a day leaves any upload in flight alone;
|
|
14
|
+
* it matches the lifecycle rule for multipart uploads.
|
|
15
|
+
*/
|
|
16
|
+
const ABANDONED_UPLOAD_GRACE_MS = 864e5;
|
|
17
|
+
/** ablage's S3 layout: `<group>/data/<id>` holds the bytes, `<group>/meta/<id>` the metadata. */
|
|
18
|
+
function metaKeyOf(key) {
|
|
19
|
+
const parts = key.split("/");
|
|
20
|
+
if (parts.length < 3 || parts.at(-2) !== "data" || !parts.at(-1)) return void 0;
|
|
21
|
+
return [
|
|
22
|
+
...parts.slice(0, -2),
|
|
23
|
+
"meta",
|
|
24
|
+
parts.at(-1)
|
|
25
|
+
].join("/");
|
|
26
|
+
}
|
|
27
|
+
const unescapeXml = (text) => text.replace(/&(amp|lt|gt|quot|apos|#x?[0-9a-f]+);/gi, (_, entity) => {
|
|
28
|
+
const named = {
|
|
29
|
+
amp: "&",
|
|
30
|
+
lt: "<",
|
|
31
|
+
gt: ">",
|
|
32
|
+
quot: "\"",
|
|
33
|
+
apos: "'"
|
|
34
|
+
};
|
|
35
|
+
if (entity in named) return named[entity];
|
|
36
|
+
const hex = entity[1] === "x" || entity[1] === "X";
|
|
37
|
+
return String.fromCodePoint(parseInt(entity.slice(hex ? 2 : 1), hex ? 16 : 10));
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* Objects of a ListObjectsV2 page, its folders (listed with a delimiter), and the token for the
|
|
41
|
+
* next page.
|
|
42
|
+
*/
|
|
43
|
+
function parseObjectList(xml) {
|
|
44
|
+
const objects = [...xml.matchAll(/<Contents>([\s\S]*?)<\/Contents>/g)].map(([, entry]) => ({
|
|
45
|
+
key: unescapeXml(/<Key>([^<]*)<\/Key>/.exec(entry)?.[1] ?? ""),
|
|
46
|
+
lastModified: Date.parse(/<LastModified>([^<]*)<\/LastModified>/.exec(entry)?.[1] ?? ""),
|
|
47
|
+
size: Number(/<Size>([^<]*)<\/Size>/.exec(entry)?.[1] ?? 0)
|
|
48
|
+
}));
|
|
49
|
+
const prefixes = [...xml.matchAll(/<CommonPrefixes>([\s\S]*?)<\/CommonPrefixes>/g)].map(([, entry]) => unescapeXml(/<Prefix>([^<]*)<\/Prefix>/.exec(entry)?.[1] ?? ""));
|
|
50
|
+
const next = /<IsTruncated>true<\/IsTruncated>/.test(xml) ? /<NextContinuationToken>([^<]*)<\/NextContinuationToken>/.exec(xml)?.[1] : void 0;
|
|
51
|
+
return {
|
|
52
|
+
objects,
|
|
53
|
+
prefixes,
|
|
54
|
+
next: next === void 0 ? void 0 : unescapeXml(next)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* ablage keeps a file as bytes and metadata, which only make sense together: the metadata key
|
|
59
|
+
* for bytes, the bytes for metadata, undefined for anything else.
|
|
60
|
+
*/
|
|
61
|
+
function companionKeyOf(key) {
|
|
62
|
+
const meta = metaKeyOf(key);
|
|
63
|
+
if (meta !== void 0) return meta;
|
|
64
|
+
const parts = key.split("/");
|
|
65
|
+
if (parts.length < 3 || parts.at(-2) !== "meta" || !parts.at(-1)) return void 0;
|
|
66
|
+
return [
|
|
67
|
+
...parts.slice(0, -2),
|
|
68
|
+
"data",
|
|
69
|
+
parts.at(-1)
|
|
70
|
+
].join("/");
|
|
71
|
+
}
|
|
72
|
+
/** S3's limit for object keys, in bytes. */
|
|
73
|
+
const MAX_KEY_BYTES = 1024;
|
|
74
|
+
function createStorage(options) {
|
|
75
|
+
async function admin(path, body) {
|
|
76
|
+
const res = await fetch(`${options.adminUrl}/v2/${path}`, {
|
|
77
|
+
method: body === void 0 ? "GET" : "POST",
|
|
78
|
+
headers: {
|
|
79
|
+
authorization: `Bearer ${options.adminToken}`,
|
|
80
|
+
"content-type": "application/json"
|
|
81
|
+
},
|
|
82
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
83
|
+
});
|
|
84
|
+
if (!res.ok) throw new StorageError(`garage ${path}: ${res.status} ${await res.text()}`);
|
|
85
|
+
return await res.json();
|
|
86
|
+
}
|
|
87
|
+
async function exists(path) {
|
|
88
|
+
const res = await fetch(`${options.adminUrl}/v2/${path}`, { headers: { authorization: `Bearer ${options.adminToken}` } });
|
|
89
|
+
if (res.status === 404 || res.status === 400) return void 0;
|
|
90
|
+
if (!res.ok) throw new StorageError(`garage ${path}: ${res.status} ${await res.text()}`);
|
|
91
|
+
return await res.json();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A fresh single-node Garage has no layout and stores nothing until it gets one. Gives this
|
|
95
|
+
* node all of its disk, once; later starts find the layout in place.
|
|
96
|
+
*/
|
|
97
|
+
async function ensureLayout() {
|
|
98
|
+
const node = (await admin("GetClusterStatus")).nodes[0];
|
|
99
|
+
if (!node || node.role) return;
|
|
100
|
+
await admin("UpdateClusterLayout", { roles: [{
|
|
101
|
+
id: node.id,
|
|
102
|
+
zone: "wervt",
|
|
103
|
+
capacity: node.dataPartition?.available ?? 0xe8d4a51000,
|
|
104
|
+
tags: []
|
|
105
|
+
}] });
|
|
106
|
+
await admin("ApplyClusterLayout", { version: (await admin("GetClusterLayout")).version + 1 });
|
|
107
|
+
}
|
|
108
|
+
const s3 = async (app) => {
|
|
109
|
+
const creds = await storageCredentials(app, options.secret);
|
|
110
|
+
return {
|
|
111
|
+
creds,
|
|
112
|
+
client: new AwsClient({
|
|
113
|
+
accessKeyId: creds.accessKeyId,
|
|
114
|
+
secretAccessKey: creds.secretAccessKey,
|
|
115
|
+
region: "garage",
|
|
116
|
+
service: "s3"
|
|
117
|
+
}),
|
|
118
|
+
base: `${options.s3Url}/${creds.bucket}`
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Creates the app's bucket and key if they're missing, lets the app's origins upload and
|
|
123
|
+
* download from browsers (CORS), and cleans up abandoned uploads. Safe to run on every deploy.
|
|
124
|
+
* With `quota`, sets the bucket's size limit too; without, leaves it as it is.
|
|
125
|
+
*/
|
|
126
|
+
async function provision(app, appOrigins, quota) {
|
|
127
|
+
const { creds, client, base } = await s3(app);
|
|
128
|
+
const bucket = await exists(`GetBucketInfo?globalAlias=${creds.bucket}`) ?? await admin("CreateBucket", { globalAlias: creds.bucket });
|
|
129
|
+
if (!await exists(`GetKeyInfo?id=${creds.accessKeyId}`)) await admin("ImportKey", {
|
|
130
|
+
accessKeyId: creds.accessKeyId,
|
|
131
|
+
secretAccessKey: creds.secretAccessKey,
|
|
132
|
+
name: creds.bucket
|
|
133
|
+
});
|
|
134
|
+
await admin("AllowBucketKey", {
|
|
135
|
+
bucketId: bucket.id,
|
|
136
|
+
accessKeyId: creds.accessKeyId,
|
|
137
|
+
permissions: {
|
|
138
|
+
read: true,
|
|
139
|
+
write: true,
|
|
140
|
+
owner: true
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
if (quota) await applyQuota(bucket.id, quota);
|
|
144
|
+
const cors = `<CORSConfiguration><CORSRule>
|
|
145
|
+
${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\n`).join("")}<AllowedMethod>GET</AllowedMethod><AllowedMethod>HEAD</AllowedMethod>
|
|
146
|
+
<AllowedMethod>PUT</AllowedMethod><AllowedMethod>POST</AllowedMethod>
|
|
147
|
+
<AllowedHeader>*</AllowedHeader><ExposeHeader>ETag</ExposeHeader>
|
|
148
|
+
<MaxAgeSeconds>3600</MaxAgeSeconds>
|
|
149
|
+
</CORSRule></CORSConfiguration>`;
|
|
150
|
+
const res = await client.fetch(`${base}?cors`, {
|
|
151
|
+
method: "PUT",
|
|
152
|
+
body: cors
|
|
153
|
+
});
|
|
154
|
+
if (!res.ok) throw new StorageError(`setting CORS for ${creds.bucket}: ${await res.text()}`);
|
|
155
|
+
const rules = await client.fetch(`${base}?lifecycle`, {
|
|
156
|
+
method: "PUT",
|
|
157
|
+
body: `<LifecycleConfiguration><Rule>
|
|
158
|
+
<ID>abort-incomplete-uploads</ID><Status>Enabled</Status><Filter></Filter>
|
|
159
|
+
<AbortIncompleteMultipartUpload><DaysAfterInitiation>1</DaysAfterInitiation></AbortIncompleteMultipartUpload>
|
|
160
|
+
</Rule></LifecycleConfiguration>`
|
|
161
|
+
});
|
|
162
|
+
if (!rules.ok) throw new StorageError(`setting the lifecycle for ${creds.bucket}: ${await rules.text()}`);
|
|
163
|
+
return { bucket: creds.bucket };
|
|
164
|
+
}
|
|
165
|
+
function applyQuota(bucketId, quota) {
|
|
166
|
+
return admin(`UpdateBucket?id=${bucketId}`, { quotas: {
|
|
167
|
+
maxSize: quota.maxBytes,
|
|
168
|
+
maxObjects: null
|
|
169
|
+
} });
|
|
170
|
+
}
|
|
171
|
+
/** Changes the size limit of an existing bucket. False when the app has no bucket yet. */
|
|
172
|
+
async function setQuota(app, quota) {
|
|
173
|
+
const { creds } = await s3(app);
|
|
174
|
+
const bucket = await exists(`GetBucketInfo?globalAlias=${creds.bucket}`);
|
|
175
|
+
if (!bucket) return false;
|
|
176
|
+
await applyQuota(bucket.id, quota);
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
async function usage(app) {
|
|
180
|
+
const { creds } = await s3(app);
|
|
181
|
+
const info = await exists(`GetBucketInfo?globalAlias=${creds.bucket}`);
|
|
182
|
+
if (!info) return void 0;
|
|
183
|
+
return {
|
|
184
|
+
bucket: creds.bucket,
|
|
185
|
+
objects: info.objects,
|
|
186
|
+
bytes: info.bytes,
|
|
187
|
+
unfinishedUploads: info.unfinishedMultipartUploads,
|
|
188
|
+
maxBytes: info.quotas?.maxSize ?? null
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Deletes the bytes of direct uploads that were never completed: marked by ablage, older than
|
|
193
|
+
* `graceMs` and without a metadata object. ablage never lists them, but they count against the
|
|
194
|
+
* bucket quota. Safe to run any time and as often as wanted.
|
|
195
|
+
*/
|
|
196
|
+
async function sweepAbandonedUploads(app, { graceMs = ABANDONED_UPLOAD_GRACE_MS, now = Date.now() } = {}) {
|
|
197
|
+
const { creds, client, base } = await s3(app);
|
|
198
|
+
if (!await exists(`GetBucketInfo?globalAlias=${creds.bucket}`)) return { deleted: [] };
|
|
199
|
+
const url = (key) => `${base}/${key.split("/").map(encodeURIComponent).join("/")}`;
|
|
200
|
+
const candidates = /* @__PURE__ */ new Map();
|
|
201
|
+
let token;
|
|
202
|
+
do {
|
|
203
|
+
const query = `list-type=2&max-keys=1000${token ? `&continuation-token=${encodeURIComponent(token)}` : ""}`;
|
|
204
|
+
const res = await client.fetch(`${base}?${query}`);
|
|
205
|
+
if (!res.ok) throw new StorageError(`listing ${creds.bucket}: ${await res.text()}`);
|
|
206
|
+
const page = parseObjectList(await res.text());
|
|
207
|
+
for (const object of page.objects) {
|
|
208
|
+
candidates.delete(object.key);
|
|
209
|
+
const metaKey = metaKeyOf(object.key);
|
|
210
|
+
if (metaKey !== void 0 && now - object.lastModified >= graceMs) candidates.set(metaKey, object.key);
|
|
211
|
+
}
|
|
212
|
+
token = page.next;
|
|
213
|
+
} while (token);
|
|
214
|
+
const deleted = [];
|
|
215
|
+
for (const [metaKey, key] of candidates) {
|
|
216
|
+
const head = await client.fetch(url(key), { method: "HEAD" });
|
|
217
|
+
if (head.status === 404) continue;
|
|
218
|
+
if (!head.ok) throw new StorageError(`checking ${key} in ${creds.bucket}: ${head.status}`);
|
|
219
|
+
if (!head.headers.get(DIRECT_UPLOAD_MARKER)) continue;
|
|
220
|
+
const meta = await client.fetch(url(metaKey), { method: "HEAD" });
|
|
221
|
+
if (meta.ok) continue;
|
|
222
|
+
if (meta.status !== 404) throw new StorageError(`checking ${metaKey} in ${creds.bucket}: ${meta.status}`);
|
|
223
|
+
const res = await client.fetch(url(key), { method: "DELETE" });
|
|
224
|
+
if (!res.ok && res.status !== 404) throw new StorageError(`deleting ${key} in ${creds.bucket}: ${await res.text()}`);
|
|
225
|
+
deleted.push(key);
|
|
226
|
+
}
|
|
227
|
+
return { deleted };
|
|
228
|
+
}
|
|
229
|
+
/** Whether the app's bucket exists, e.g. left over from a deleted app that kept its data. */
|
|
230
|
+
async function hasBucket(app) {
|
|
231
|
+
const creds = await storageCredentials(app, options.secret);
|
|
232
|
+
return Boolean(await exists(`GetBucketInfo?globalAlias=${creds.bucket}`));
|
|
233
|
+
}
|
|
234
|
+
/** Deletes every object and unfinished upload, then the bucket. Only with "drop data". */
|
|
235
|
+
async function drop(app) {
|
|
236
|
+
const { creds, client, base } = await s3(app);
|
|
237
|
+
const bucket = await exists(`GetBucketInfo?globalAlias=${creds.bucket}`);
|
|
238
|
+
if (bucket) {
|
|
239
|
+
for (;;) {
|
|
240
|
+
const keys = [...(await (await client.fetch(`${base}?list-type=2&max-keys=1000`)).text()).matchAll(/<Key>([^<]*)<\/Key>/g)].map((m) => m[1]);
|
|
241
|
+
if (!keys.length) break;
|
|
242
|
+
const body = `<Delete><Quiet>true</Quiet>${keys.map((key) => `<Object><Key>${key}</Key></Object>`).join("")}</Delete>`;
|
|
243
|
+
const res = await client.fetch(`${base}?delete`, {
|
|
244
|
+
method: "POST",
|
|
245
|
+
body
|
|
246
|
+
});
|
|
247
|
+
if (!res.ok) throw new StorageError(`emptying ${creds.bucket}: ${await res.text()}`);
|
|
248
|
+
}
|
|
249
|
+
for (;;) {
|
|
250
|
+
const uploads = [...(await (await client.fetch(`${base}?uploads&max-uploads=1000`)).text()).matchAll(/<Key>([^<]*)<\/Key>\s*<UploadId>([^<]*)<\/UploadId>/g)].map((m) => ({
|
|
251
|
+
key: m[1],
|
|
252
|
+
id: m[2]
|
|
253
|
+
}));
|
|
254
|
+
if (!uploads.length) break;
|
|
255
|
+
for (const upload of uploads) {
|
|
256
|
+
const url = `${base}/${upload.key.replaceAll("&", "&").split("/").map(encodeURIComponent).join("/")}?uploadId=${encodeURIComponent(upload.id)}`;
|
|
257
|
+
const res = await client.fetch(url, { method: "DELETE" });
|
|
258
|
+
if (!res.ok && res.status !== 404) throw new StorageError(`aborting an upload in ${creds.bucket}: ${await res.text()}`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
await admin(`DeleteBucket?id=${String(bucket.id)}`, {});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const objectUrl = (base, key) => `${base}/${key.split("/").map(encodeURIComponent).join("/")}`;
|
|
265
|
+
/**
|
|
266
|
+
* One page of the bucket, one folder level at a time like a file browser: the files directly
|
|
267
|
+
* in `prefix` and the folders below it. Undefined when the app has no bucket.
|
|
268
|
+
*/
|
|
269
|
+
async function list(app, { prefix = "", token, limit = 200 } = {}) {
|
|
270
|
+
const { creds, client, base } = await s3(app);
|
|
271
|
+
const query = new URLSearchParams({
|
|
272
|
+
"list-type": "2",
|
|
273
|
+
delimiter: "/",
|
|
274
|
+
prefix,
|
|
275
|
+
"max-keys": String(Math.min(Math.max(limit, 1), 1e3))
|
|
276
|
+
});
|
|
277
|
+
if (token) query.set("continuation-token", token);
|
|
278
|
+
const res = await client.fetch(`${base}?${query.toString()}`);
|
|
279
|
+
if (res.status === 404) return void 0;
|
|
280
|
+
if (!res.ok) throw new StorageError(`listing ${creds.bucket}: ${await res.text()}`);
|
|
281
|
+
const page = parseObjectList(await res.text());
|
|
282
|
+
return {
|
|
283
|
+
prefix,
|
|
284
|
+
folders: page.prefixes,
|
|
285
|
+
files: page.objects.map((object) => ({
|
|
286
|
+
key: object.key,
|
|
287
|
+
size: object.size,
|
|
288
|
+
lastModified: new Date(object.lastModified).toISOString()
|
|
289
|
+
})),
|
|
290
|
+
next: page.next
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/** Size, type and metadata of one object; undefined when it doesn't exist. */
|
|
294
|
+
async function describe(app, key) {
|
|
295
|
+
const { creds, client, base } = await s3(app);
|
|
296
|
+
const res = await client.fetch(objectUrl(base, key), { method: "HEAD" });
|
|
297
|
+
if (res.status === 404) return void 0;
|
|
298
|
+
if (!res.ok) throw new StorageError(`reading ${key} in ${creds.bucket}: ${res.status}`);
|
|
299
|
+
const metadata = {};
|
|
300
|
+
for (const [name, value] of res.headers) if (name.startsWith("x-amz-meta-")) metadata[name.slice(11)] = value;
|
|
301
|
+
const modified = res.headers.get("last-modified");
|
|
302
|
+
return {
|
|
303
|
+
key,
|
|
304
|
+
size: Number(res.headers.get("content-length") ?? 0),
|
|
305
|
+
lastModified: modified ? new Date(modified).toISOString() : "",
|
|
306
|
+
contentType: res.headers.get("content-type"),
|
|
307
|
+
metadata
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* A presigned download link on `publicUrl`, the S3 API as browsers reach it, so files go from
|
|
312
|
+
* Garage straight to the browser. Always an attachment, never shown inline: the file is the
|
|
313
|
+
* app's users' content and may well be HTML.
|
|
314
|
+
*/
|
|
315
|
+
async function downloadUrl(app, key, { publicUrl, expiresIn = 300 }) {
|
|
316
|
+
const { creds, client } = await s3(app);
|
|
317
|
+
const url = new URL(objectUrl(`${publicUrl.replace(/\/$/, "")}/${creds.bucket}`, key));
|
|
318
|
+
const name = key.split("/").at(-1) || "download";
|
|
319
|
+
const ascii = name.replace(/[^\x20-\x7e]|["\\]/g, "_");
|
|
320
|
+
url.searchParams.set("response-content-disposition", `attachment; filename="${ascii}"; filename*=UTF-8''${encodeURIComponent(name)}`);
|
|
321
|
+
url.searchParams.set("X-Amz-Expires", String(expiresIn));
|
|
322
|
+
return (await client.sign(url.toString(), {
|
|
323
|
+
method: "GET",
|
|
324
|
+
aws: { signQuery: true }
|
|
325
|
+
})).url;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Deletes one object, and its ablage companion (see companionKeyOf) so ablage never lists a
|
|
329
|
+
* file whose bytes are gone. Undefined when the object doesn't exist.
|
|
330
|
+
*/
|
|
331
|
+
async function remove(app, key) {
|
|
332
|
+
const { creds, client, base } = await s3(app);
|
|
333
|
+
const present = async (k) => {
|
|
334
|
+
const res = await client.fetch(objectUrl(base, k), { method: "HEAD" });
|
|
335
|
+
if (res.status === 404) return false;
|
|
336
|
+
if (!res.ok) throw new StorageError(`checking ${k} in ${creds.bucket}: ${res.status}`);
|
|
337
|
+
return true;
|
|
338
|
+
};
|
|
339
|
+
if (!await present(key)) return void 0;
|
|
340
|
+
const companion = companionKeyOf(key);
|
|
341
|
+
const keys = companion !== void 0 && await present(companion) ? [key, companion] : [key];
|
|
342
|
+
for (const k of keys) {
|
|
343
|
+
const res = await client.fetch(objectUrl(base, k), { method: "DELETE" });
|
|
344
|
+
if (!res.ok && res.status !== 404) throw new StorageError(`deleting ${k} in ${creds.bucket}: ${await res.text()}`);
|
|
345
|
+
}
|
|
346
|
+
return { deleted: keys };
|
|
347
|
+
}
|
|
348
|
+
return {
|
|
349
|
+
ensureLayout,
|
|
350
|
+
provision,
|
|
351
|
+
setQuota,
|
|
352
|
+
usage,
|
|
353
|
+
list,
|
|
354
|
+
describe,
|
|
355
|
+
downloadUrl,
|
|
356
|
+
remove,
|
|
357
|
+
sweepAbandonedUploads,
|
|
358
|
+
hasBucket,
|
|
359
|
+
drop
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
export { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, StorageError, companionKeyOf, createStorage, metaKeyOf, parseObjectList };
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wervt/platform",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Provisioning for wervt apps: database roles, migrations, PgBouncer and object storage. Used by the control plane and local development.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"garage",
|
|
7
|
+
"pgbouncer",
|
|
8
|
+
"postgres",
|
|
9
|
+
"wervt"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://wervt.app",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Niki2k1",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/wervt-app/wervt.git",
|
|
17
|
+
"directory": "packages/platform"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./dev": {
|
|
30
|
+
"types": "./dist/dev.d.ts",
|
|
31
|
+
"default": "./dist/dev.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@wervt/core": "^0.0.2",
|
|
36
|
+
"aws4fetch": "^1.0.20",
|
|
37
|
+
"postgres": "^3.4.9"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"drizzle-orm": "1.0.0-rc.5"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"drizzle-orm": ">=1.0.0-rc.5 <2"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "vp pack"
|
|
47
|
+
}
|
|
6
48
|
}
|