@qooal/vault 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,160 @@
1
+ import type { VaultAccess, VaultAsset, VaultAssetPage, VaultAssetQuery, VaultAssetVersion, VaultBulkAction, VaultCompletedPart, VaultCreatedDownloadLink, VaultDownloadLink, VaultDownloadLinkInput, VaultFileClass, VaultFolder, VaultLibrary, VaultPage, VaultSignedUrl, VaultSignUrlInput, VaultTransformPreset, VaultUploadFromUrlInput, VaultUploadInput, VaultUploadInstructions, VaultUploadPart, VaultUploadPreset, VaultUploadStatus, VaultUploadTicket, VaultUploadTicketInput, VaultUrlImportInstructions, VaultUsage } from "../types.js";
2
+ import type { VaultResourceType } from "../url.js";
3
+ export interface VaultClientOptions {
4
+ /** `qk_live_…` or `qk_test_…` */
5
+ apiKey: string;
6
+ /** default https://api.qooal.com/v1/vault */
7
+ baseUrl?: string;
8
+ fetch?: typeof fetch;
9
+ /** retries per request; default 2 */
10
+ retries?: number;
11
+ /** ms before retry `attempt`; default full-jitter exponential, honouring Retry-After */
12
+ retryDelay?: (attempt: number, retryAfterSeconds: number | null) => number;
13
+ /** the client refuses to run in a browser, where the API key would leak; set only for trusted desktop shells */
14
+ dangerouslyAllowBrowser?: boolean;
15
+ }
16
+ export interface RequestOptions {
17
+ signal?: AbortSignal;
18
+ /** overrides the generated `Idempotency-Key` on creates and keyed actions */
19
+ idempotencyKey?: string;
20
+ }
21
+ /** Omitted fields stay as they are; `null` would too, so there is none: `""` clears (or, for `folderId`, moves to the root). */
22
+ export interface VaultAssetPatch {
23
+ publicId?: string;
24
+ /** a folder (`vfld_…`), or `""` for the root */
25
+ folderId?: string;
26
+ tags?: string[];
27
+ /** `""` clears it */
28
+ title?: string;
29
+ /** `""` clears it */
30
+ altText?: string;
31
+ fields?: {
32
+ key: string;
33
+ value: string;
34
+ }[];
35
+ access?: VaultAccess;
36
+ }
37
+ export interface VaultTransformPresetInput {
38
+ name: string;
39
+ transformation: string;
40
+ /** render it ahead for new uploads (required on create) */
41
+ eager: boolean;
42
+ description?: string | null;
43
+ }
44
+ /** `PATCH …/transform-presets/{preset}`: the name cannot change; `description: ""` clears it. */
45
+ export type VaultTransformPresetPatch = Partial<Pick<VaultTransformPresetInput, "transformation" | "eager">> & {
46
+ description?: string;
47
+ };
48
+ /** Signed upload presets only on the public surface; unsigned ones are console-only. */
49
+ export interface VaultUploadPresetInput {
50
+ name: string;
51
+ folderId?: string | null;
52
+ publicIdMode?: "random" | "filename" | "filename_unique";
53
+ overwrite?: boolean;
54
+ access?: VaultAccess;
55
+ tags?: string[];
56
+ allowedClasses?: VaultFileClass[];
57
+ maxBytes?: number | null;
58
+ eagerPresets?: string[];
59
+ /** browser origins allowed */
60
+ allowedOrigins?: string[];
61
+ /** uploads per minute, 1-600 */
62
+ ratePerMinute?: number;
63
+ /** uploads per cycle; on update `0` removes the cap */
64
+ monthlyCap?: number | null;
65
+ turnstile?: boolean;
66
+ /** hold every upload for approval */
67
+ moderation?: boolean;
68
+ disabled?: boolean;
69
+ }
70
+ /** One strict-mode allowlist entry. */
71
+ export interface VaultAllowedTransform {
72
+ hash: string;
73
+ transformation: string;
74
+ source: "eager" | "console" | "api";
75
+ createdAt: string;
76
+ }
77
+ /** The query string for `GET …/assets`: lists are comma-separated, `tag` and `field` repeat. */
78
+ export declare function assetQueryString(q?: VaultAssetQuery): string;
79
+ /** Creates a client for the public API. */
80
+ export declare function createVaultClient(options: VaultClientOptions): {
81
+ listLibraries: (o?: RequestOptions) => Promise<{
82
+ items: VaultLibrary[];
83
+ }>;
84
+ getLibrary: (lib: string, o?: RequestOptions) => Promise<VaultLibrary>;
85
+ getUsage: (lib: string, range?: VaultUsage["range"], o?: RequestOptions) => Promise<VaultUsage>;
86
+ listFolders: (lib: string, o?: RequestOptions) => Promise<{
87
+ items: VaultFolder[];
88
+ }>;
89
+ createFolder: (lib: string, path: string, o?: RequestOptions) => Promise<VaultFolder>;
90
+ updateFolder: (lib: string, folder: string, path: string, o?: RequestOptions) => Promise<VaultFolder>;
91
+ deleteFolder: (lib: string, folder: string, o?: RequestOptions) => Promise<void>;
92
+ listAssets: (lib: string, query?: VaultAssetQuery, o?: RequestOptions) => Promise<VaultAssetPage>;
93
+ getAsset: (lib: string, asset: string, o?: RequestOptions) => Promise<VaultAsset>;
94
+ lookupAsset: (lib: string, resourceType: VaultResourceType, publicId: string, o?: RequestOptions) => Promise<VaultAsset>;
95
+ updateAsset: (lib: string, asset: string, patch: VaultAssetPatch, o?: RequestOptions & {
96
+ ifMatch?: string;
97
+ }) => Promise<VaultAsset>;
98
+ deleteAsset: (lib: string, asset: string, o?: RequestOptions) => Promise<void>;
99
+ restoreAsset: (lib: string, asset: string, o?: RequestOptions) => Promise<VaultAsset>;
100
+ regenerateAsset: (lib: string, asset: string, o?: RequestOptions) => Promise<VaultAsset>;
101
+ bulkAssets: (lib: string, ids: string[], action: VaultBulkAction, o?: RequestOptions) => Promise<{
102
+ updated: number;
103
+ failed: {
104
+ id: string;
105
+ code: string;
106
+ }[];
107
+ }>;
108
+ listVersions: (lib: string, asset: string, o?: RequestOptions) => Promise<{
109
+ items: VaultAssetVersion[];
110
+ }>;
111
+ restoreVersion: (lib: string, asset: string, version: number, o?: RequestOptions) => Promise<VaultAsset>;
112
+ signUrl: (lib: string, asset: string, input?: VaultSignUrlInput, o?: RequestOptions) => Promise<VaultSignedUrl>;
113
+ createDownloadLink: (lib: string, asset: string, input: VaultDownloadLinkInput, o?: RequestOptions) => Promise<VaultCreatedDownloadLink>;
114
+ listDownloadLinks: (lib: string, query?: {
115
+ asset?: string;
116
+ cursor?: string;
117
+ limit?: number;
118
+ }, o?: RequestOptions) => Promise<VaultPage<VaultDownloadLink>>;
119
+ revokeDownloadLink: (lib: string, link: string, o?: RequestOptions) => Promise<void>;
120
+ createUpload: (lib: string, input: VaultUploadInput, o?: RequestOptions) => Promise<VaultUploadInstructions>;
121
+ importFromUrl: (lib: string, input: VaultUploadFromUrlInput, o?: RequestOptions) => Promise<VaultUrlImportInstructions>;
122
+ requestUploadParts: (lib: string, upload: string, partNumbers: number[], o?: RequestOptions) => Promise<VaultUploadPart[]>;
123
+ getUpload: (lib: string, upload: string, o?: RequestOptions) => Promise<VaultUploadStatus>;
124
+ completeUpload: (lib: string, upload: string, parts?: VaultCompletedPart[], o?: RequestOptions) => Promise<VaultAsset>;
125
+ abortUpload: (lib: string, upload: string, o?: RequestOptions) => Promise<void>;
126
+ createUploadTicket: (lib: string, input?: VaultUploadTicketInput, o?: RequestOptions) => Promise<VaultUploadTicket>;
127
+ revokeUploadTicket: (lib: string, ticket: string, o?: RequestOptions) => Promise<void>;
128
+ listTransformPresets: (lib: string, o?: RequestOptions) => Promise<{
129
+ items: VaultTransformPreset[];
130
+ }>;
131
+ getTransformPreset: (lib: string, preset: string, o?: RequestOptions) => Promise<VaultTransformPreset>;
132
+ createTransformPreset: (lib: string, input: VaultTransformPresetInput, o?: RequestOptions) => Promise<VaultTransformPreset>;
133
+ updateTransformPreset: (lib: string, preset: string, patch: VaultTransformPresetPatch, o?: RequestOptions) => Promise<VaultTransformPreset>;
134
+ deleteTransformPreset: (lib: string, preset: string, o?: RequestOptions) => Promise<void>;
135
+ listUploadPresets: (lib: string, o?: RequestOptions) => Promise<{
136
+ items: VaultUploadPreset[];
137
+ }>;
138
+ getUploadPreset: (lib: string, preset: string, o?: RequestOptions) => Promise<VaultUploadPreset>;
139
+ createUploadPreset: (lib: string, input: VaultUploadPresetInput, o?: RequestOptions) => Promise<VaultUploadPreset>;
140
+ updateUploadPreset: (lib: string, preset: string, patch: Partial<VaultUploadPresetInput>, o?: RequestOptions) => Promise<VaultUploadPreset>;
141
+ deleteUploadPreset: (lib: string, preset: string, o?: RequestOptions) => Promise<void>;
142
+ listAllowedTransforms: (lib: string, o?: RequestOptions) => Promise<{
143
+ items: VaultAllowedTransform[];
144
+ }>;
145
+ allowTransform: (lib: string, transformation: string, o?: RequestOptions) => Promise<VaultAllowedTransform>;
146
+ removeAllowedTransform: (lib: string, hash: string, o?: RequestOptions) => Promise<void>;
147
+ };
148
+ export type VaultClient = ReturnType<typeof createVaultClient>;
149
+ /** Client options other than the key, for the one-call helpers below. */
150
+ export type VaultCallOptions = Omit<VaultClientOptions, "apiKey"> & RequestOptions;
151
+ /**
152
+ * Mints an upload ticket for a browser: call it from your backend for a signed-in shopper or admin, then give
153
+ * `ticket` to `<qooal-vault-uploader ticket="…">` or `createUploader({ticket})`. Default lifetime 15 minutes.
154
+ */
155
+ export declare function createUploadTicket(apiKey: string, library: string, input?: VaultUploadTicketInput, options?: VaultCallOptions): Promise<VaultUploadTicket>;
156
+ /**
157
+ * Creates a download link for a paid digital product: an expiring URL on the `dl` host, optionally limited to
158
+ * `maxDownloads`, revocable. The answer's `url` is shown once; email it to the buyer.
159
+ */
160
+ export declare function createDownloadLink(apiKey: string, library: string, asset: string, input: VaultDownloadLinkInput, options?: VaultCallOptions): Promise<VaultCreatedDownloadLink>;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `@qooal/vault/server` (Node 20+, edge runtimes; WebCrypto and `fetch` only): what runs on the customer's backend.
3
+ *
4
+ * - Signing: `signVaultUrl` / `signedVaultUrl` (private and authenticated delivery), over the signer
5
+ * `signPath` and friends, re-exported as they are.
6
+ * - Uploads from browsers: `createUploadTicket`.
7
+ * - Paid downloads: `createDownloadLink`.
8
+ * - Webhooks: `verifyWebhook` (Standard Webhooks).
9
+ * - Everything else in the public API: `createVaultClient`.
10
+ */
11
+ export { VAULT_API_BASE, VaultApiError, type VaultFieldError } from "../http.js";
12
+ export { base64Url, keyVersionChar, pathSigningInput, signDownload, signHlsPrefix, signPath, type VaultSigningKeyBytes, verifyPath, verifyToken, } from "../sign.js";
13
+ export type * from "../types.js";
14
+ export { assetQueryString, createDownloadLink, createUploadTicket, createVaultClient, type RequestOptions, type VaultAllowedTransform, type VaultAssetPatch, type VaultCallOptions, type VaultClient, type VaultClientOptions, type VaultTransformPresetInput, type VaultTransformPresetPatch, type VaultUploadPresetInput, } from "./client.js";
15
+ export { decodeBase64, type SignVaultUrlOptions, signedVaultUrl, signVaultUrl, type VaultUrlSigningKey, } from "./sign-url.js";
16
+ export { VaultWebhookError, type VaultWebhookErrorCode, type VerifyWebhookOptions, verifyWebhook, type WebhookHeaders, webhookSignature, } from "./webhook.js";
@@ -0,0 +1,28 @@
1
+ import { type VaultUrlLibrary, type VaultUrlOptions } from "../url.js";
2
+ /** A library signing key: its version (from the console) and the 32 secret bytes, or their base64url text. */
3
+ export interface VaultUrlSigningKey {
4
+ version: number;
5
+ key: Uint8Array | string;
6
+ }
7
+ export interface SignVaultUrlOptions {
8
+ /**
9
+ * when the URL stops working: Unix seconds or a Date; required for `authenticated` delivery (the edge refuses an
10
+ * expiry too far ahead)
11
+ */
12
+ expiresAt?: number | Date;
13
+ /** the library slug, for URLs on a custom domain (their paths carry no slug) */
14
+ library?: string;
15
+ }
16
+ /** Decodes base64 or base64url (padding optional). */
17
+ export declare function decodeBase64(text: string): Uint8Array;
18
+ /**
19
+ * Signs a delivery URL or path (`https://media.qooalcdn.com/acme-shop/image/private/w_400/v3/shoes/red.jpg`, or the
20
+ * path alone). Answers the canonical, signed URL with `?exp=` when `expiresAt` is given. Refuses URLs that are
21
+ * already signed, carry a query, or point inside an HLS stream (those use `tok`, minted by the edge).
22
+ */
23
+ export declare function signVaultUrl(url: string, key: VaultUrlSigningKey, opts?: SignVaultUrlOptions): Promise<string>;
24
+ /**
25
+ * Builds and signs in one step: `vaultUrl`, then `signVaultUrl`. Use it for `private` and `authenticated` assets
26
+ * (`delivery` in `opts`); give `library` (the slug) when `lib` is a custom domain.
27
+ */
28
+ export declare function signedVaultUrl(lib: VaultUrlLibrary, publicId: string, key: VaultUrlSigningKey, opts?: Omit<VaultUrlOptions, "signature" | "expiresAt"> & SignVaultUrlOptions): Promise<string>;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Verifying Vault webhooks (the Standard Webhooks scheme):
3
+ * - `webhook-id`: the event ID (`vevt_…`), the same on every retry, so use it to drop duplicates;
4
+ * - `webhook-timestamp`: Unix seconds;
5
+ * - `webhook-signature`: one or more space-separated `v1,<base64 HMAC-SHA-256(key, id + "." + timestamp + "." + body)>`
6
+ * (two while a secret rotates, for 24 h).
7
+ * The key is the secret's bytes: `whsec_` + base64(32 bytes), as the console shows it once.
8
+ *
9
+ * Verify the raw body exactly as received (before any JSON parsing), in constant time, within a timestamp tolerance.
10
+ */
11
+ import type { VaultWebhookEvent } from "../types.js";
12
+ export type VaultWebhookErrorCode = "missing_headers" | "bad_timestamp" | "timestamp_out_of_range" | "bad_secret" | "no_valid_signature" | "bad_body";
13
+ export declare class VaultWebhookError extends Error {
14
+ readonly code: VaultWebhookErrorCode;
15
+ constructor(code: VaultWebhookErrorCode, detail?: string);
16
+ }
17
+ /** Request headers: a `Headers` object, or a plain record (Node's `req.headers`, any letter case). */
18
+ export type WebhookHeaders = Headers | Record<string, string | string[] | undefined>;
19
+ export interface VerifyWebhookOptions {
20
+ /** allowed clock skew either way, seconds; default 300 */
21
+ toleranceSeconds?: number;
22
+ /** "now" in Unix seconds (tests) */
23
+ now?: number;
24
+ }
25
+ /** HMAC-SHA-256 over `<id>.<timestamp>.<body>`: the `v1` signature's bytes. */
26
+ export declare function webhookSignature(secret: string, id: string, timestamp: string | number, body: string | Uint8Array): Promise<Uint8Array>;
27
+ /**
28
+ * Verifies a webhook and answers its parsed body. Throws VaultWebhookError when a header is missing, the timestamp is
29
+ * outside the tolerance, or no `v1` signature matches any of the given secrets (pass two during your own rotation).
30
+ */
31
+ export declare function verifyWebhook(body: string | Uint8Array, headers: WebhookHeaders, secret: string | readonly string[], opts?: VerifyWebhookOptions): Promise<VaultWebhookEvent>;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Qooal Vault delivery signatures.
3
+ *
4
+ * - Key: 32 bytes per library and key version. Version character: `version mod 36` in base 36 (`0-9a-z`).
5
+ * - Path signature: `s--` + version char + base64url(HMAC-SHA-256(key, input)[0..16]) + `--`, where the input is the
6
+ * UTF-8 of `qv1`, the library slug, the signed part (`<resource>/<delivery>/<rest>`) and the `exp` value or "",
7
+ * joined with `\n`.
8
+ * - HLS `tok`: version char + base64url(HMAC(key, `qv1-prefix\n<slug>\n<prefix>\n<exp>`)[0..16]).
9
+ * - Download `sig`: version char + base64url(HMAC(key, `qv1-dl\n<slug>\n<link id>`)[0..16]).
10
+ *
11
+ * WebCrypto only (`globalThis.crypto.subtle`): Node 20+, browsers, edge runtimes. Never ship a signing key to a
12
+ * browser; sign on the server (`@qooal/vault/server`) and hand out URLs.
13
+ */
14
+ export interface VaultSigningKeyBytes {
15
+ version: number;
16
+ key: Uint8Array;
17
+ }
18
+ /** The key-version character: `version mod 36` in base 36. */
19
+ export declare function keyVersionChar(version: number): string;
20
+ /** base64url without padding. */
21
+ export declare function base64Url(bytes: Uint8Array): string;
22
+ /** The exact bytes a path signature covers. */
23
+ export declare function pathSigningInput(library: string, signedPart: string, exp?: number | null): string;
24
+ /**
25
+ * Signs a delivery path: returns the `s--<c><22>--` component that goes right after `<resource>/<delivery>/`.
26
+ * `signedPart` is `<resource>/<delivery>/<rest>` (url.ts `vaultSignedPart`); `exp` is Unix seconds.
27
+ */
28
+ export declare function signPath(keyBytes: Uint8Array, version: number, library: string, signedPart: string, exp?: number): Promise<string>;
29
+ /**
30
+ * Verifies an `s--…--` component against every key whose version character matches, comparing in constant time.
31
+ * False when the format is wrong or `exp` is in the past (`now` > `exp`, both Unix seconds).
32
+ */
33
+ export declare function verifyPath(keys: readonly VaultSigningKeyBytes[], library: string, signedPart: string, exp: number | null | undefined, sig: string, now: number): Promise<boolean>;
34
+ /** The `tok` for the HLS children of an `authenticated` video; `prefix` is `video/authenticated/v<n>/<id>.hls/`. */
35
+ export declare function signHlsPrefix(keyBytes: Uint8Array, version: number, library: string, prefix: string, exp: number): Promise<string>;
36
+ /** The `sig` of a download link: covers the library slug and the link ID; expiry and counts live in the row. */
37
+ export declare function signDownload(keyBytes: Uint8Array, version: number, library: string, linkId: string): Promise<string>;
38
+ /** Verifies a `tok` or download `sig` (version char + 22 characters) against the matching keys, in constant time. */
39
+ export declare function verifyToken(keys: readonly VaultSigningKeyBytes[], input: {
40
+ kind: "hls";
41
+ library: string;
42
+ prefix: string;
43
+ exp: number;
44
+ } | {
45
+ kind: "download";
46
+ library: string;
47
+ linkId: string;
48
+ }, token: string): Promise<boolean>;