@venizia/ignis 0.4.8-11 → 0.4.8-12
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/esm/components/mail/common/types/queue.d.ts +8 -4
- package/dist/esm/components/mail/common/types/queue.js.map +1 -1
- package/dist/esm/components/mail/helpers/executors/bull-mq-executor.helper.d.ts +1 -1
- package/dist/esm/components/mail/helpers/executors/bull-mq-executor.helper.js +2 -16
- package/dist/esm/components/mail/helpers/executors/bull-mq-executor.helper.js.map +1 -1
- package/dist/esm/components/static-asset/common/constants.d.ts +18 -11
- package/dist/esm/components/static-asset/common/constants.js +35 -20
- package/dist/esm/components/static-asset/common/constants.js.map +1 -1
- package/dist/esm/components/static-asset/common/errors.d.ts +11 -43
- package/dist/esm/components/static-asset/common/errors.js +11 -40
- package/dist/esm/components/static-asset/common/errors.js.map +1 -1
- package/dist/esm/components/static-asset/common/index.d.ts +1 -0
- package/dist/esm/components/static-asset/common/index.js +1 -0
- package/dist/esm/components/static-asset/common/index.js.map +1 -1
- package/dist/esm/components/static-asset/common/keys.d.ts +1 -1
- package/dist/esm/components/static-asset/common/keys.js +1 -1
- package/dist/esm/components/static-asset/common/keys.js.map +1 -1
- package/dist/esm/components/static-asset/common/types.d.ts +189 -140
- package/dist/esm/components/static-asset/common/types.js.map +1 -1
- package/dist/esm/components/static-asset/common/validation.d.ts +10 -0
- package/dist/esm/components/static-asset/common/validation.js +8 -0
- package/dist/esm/components/static-asset/common/validation.js.map +1 -0
- package/dist/esm/components/static-asset/component.d.ts +1 -1
- package/dist/esm/components/static-asset/component.js +13 -55
- package/dist/esm/components/static-asset/component.js.map +1 -1
- package/dist/esm/components/static-asset/controller/factory.d.ts +185 -67
- package/dist/esm/components/static-asset/controller/factory.js +32 -821
- package/dist/esm/components/static-asset/controller/factory.js.map +1 -1
- package/dist/esm/components/static-asset/controller/routes/context.d.ts +61 -0
- package/dist/esm/components/static-asset/controller/routes/context.js +45 -0
- package/dist/esm/components/static-asset/controller/routes/context.js.map +1 -0
- package/dist/esm/components/static-asset/controller/routes/definition.d.ts +40 -0
- package/dist/esm/components/static-asset/controller/routes/definition.js +74 -0
- package/dist/esm/components/static-asset/controller/routes/definition.js.map +1 -0
- package/dist/esm/components/static-asset/controller/routes/direct-upload.d.ts +3 -0
- package/dist/esm/components/static-asset/controller/routes/direct-upload.js +125 -0
- package/dist/esm/components/static-asset/controller/routes/direct-upload.js.map +1 -0
- package/dist/esm/components/static-asset/controller/routes/labels.d.ts +16 -0
- package/dist/esm/components/static-asset/controller/routes/labels.js +19 -0
- package/dist/esm/components/static-asset/controller/routes/labels.js.map +1 -0
- package/dist/esm/components/static-asset/controller/routes/objects.d.ts +7 -0
- package/dist/esm/components/static-asset/controller/routes/objects.js +193 -0
- package/dist/esm/components/static-asset/controller/routes/objects.js.map +1 -0
- package/dist/esm/components/static-asset/controller/routes/serve.d.ts +17 -0
- package/dist/esm/components/static-asset/controller/routes/serve.js +127 -0
- package/dist/esm/components/static-asset/controller/routes/serve.js.map +1 -0
- package/dist/esm/components/static-asset/controller/routes/table.d.ts +12 -0
- package/dist/esm/components/static-asset/controller/routes/table.js +52 -0
- package/dist/esm/components/static-asset/controller/routes/table.js.map +1 -0
- package/dist/esm/components/static-asset/controller/routes/upload.d.ts +10 -0
- package/dist/esm/components/static-asset/controller/routes/upload.js +174 -0
- package/dist/esm/components/static-asset/controller/routes/upload.js.map +1 -0
- package/dist/esm/components/static-asset/models/base.model.d.ts +4 -4
- package/dist/esm/components/static-asset/models/base.model.js.map +1 -1
- package/dist/esm/components/static-asset/services/commitment.service.d.ts +41 -15
- package/dist/esm/components/static-asset/services/commitment.service.js +59 -29
- package/dist/esm/components/static-asset/services/commitment.service.js.map +1 -1
- package/dist/esm/components/static-asset/services/policy.service.d.ts +7 -3
- package/dist/esm/components/static-asset/services/policy.service.js +27 -6
- package/dist/esm/components/static-asset/services/policy.service.js.map +1 -1
- package/dist/esm/components/static-asset/utilities/commit-token.utility.d.ts +4 -1
- package/dist/esm/components/static-asset/utilities/commit-token.utility.js +33 -2
- package/dist/esm/components/static-asset/utilities/commit-token.utility.js.map +1 -1
- package/dist/esm/components/static-asset/utilities/entry.utility.d.ts +69 -0
- package/dist/esm/components/static-asset/utilities/entry.utility.js +153 -0
- package/dist/esm/components/static-asset/utilities/entry.utility.js.map +1 -0
- package/dist/esm/components/static-asset/utilities/index.d.ts +2 -0
- package/dist/esm/components/static-asset/utilities/index.js +2 -0
- package/dist/esm/components/static-asset/utilities/index.js.map +1 -1
- package/dist/esm/components/static-asset/utilities/ingest.utility.js +2 -4
- package/dist/esm/components/static-asset/utilities/ingest.utility.js.map +1 -1
- package/dist/esm/components/static-asset/utilities/key-scope.utility.d.ts +6 -2
- package/dist/esm/components/static-asset/utilities/key-scope.utility.js +24 -6
- package/dist/esm/components/static-asset/utilities/key-scope.utility.js.map +1 -1
- package/dist/esm/components/static-asset/utilities/meta-link.utility.d.ts +2 -2
- package/dist/esm/components/static-asset/utilities/meta-link.utility.js +6 -6
- package/dist/esm/components/static-asset/utilities/meta-link.utility.js.map +1 -1
- package/dist/esm/components/static-asset/utilities/object-link.utility.d.ts +2 -2
- package/dist/esm/components/static-asset/utilities/object-link.utility.js +4 -5
- package/dist/esm/components/static-asset/utilities/object-link.utility.js.map +1 -1
- package/dist/esm/components/static-asset/utilities/route-options.utility.d.ts +20 -5
- package/dist/esm/components/static-asset/utilities/route-options.utility.js +44 -6
- package/dist/esm/components/static-asset/utilities/route-options.utility.js.map +1 -1
- package/dist/esm/components/static-asset/utilities/toggle.utility.d.ts +13 -0
- package/dist/esm/components/static-asset/utilities/toggle.utility.js +16 -0
- package/dist/esm/components/static-asset/utilities/toggle.utility.js.map +1 -0
- package/dist/presets/app/preset.json +4 -4
- package/dist/presets/lib/preset.json +3 -3
- package/dist/presets/shared/preset.json +1 -1
- package/package.json +6 -6
- package/dist/esm/components/static-asset/controller/base.definition.d.ts +0 -30
- package/dist/esm/components/static-asset/controller/base.definition.js +0 -307
- package/dist/esm/components/static-asset/controller/base.definition.js.map +0 -1
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export declare class StaticAssetBindingKeys {
|
|
3
3
|
/** The options an application registers through `configs.artifacts` land here, because an artifact index carries classes and no options. `application.component(StaticAssetComponent, { options })` passes them directly and wins over this key. */
|
|
4
4
|
static readonly STATIC_ASSET_OPTIONS = "@app/static-asset/options";
|
|
5
|
-
/** The `StaticAssetUploadPolicyService` of one options entry, bound only for an entry that
|
|
5
|
+
/** The `StaticAssetUploadPolicyService` of one options entry, bound only for an entry that turns `directUpload` on. `name` is the entry's KEY in the options object, not the controller name. */
|
|
6
6
|
static policyService(opts: {
|
|
7
7
|
name: string;
|
|
8
8
|
}): string;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export class StaticAssetBindingKeys {
|
|
3
3
|
/** The options an application registers through `configs.artifacts` land here, because an artifact index carries classes and no options. `application.component(StaticAssetComponent, { options })` passes them directly and wins over this key. */
|
|
4
4
|
static { this.STATIC_ASSET_OPTIONS = '@app/static-asset/options'; }
|
|
5
|
-
/** The `StaticAssetUploadPolicyService` of one options entry, bound only for an entry that
|
|
5
|
+
/** The `StaticAssetUploadPolicyService` of one options entry, bound only for an entry that turns `directUpload` on. `name` is the entry's KEY in the options object, not the controller name. */
|
|
6
6
|
static policyService(opts) {
|
|
7
7
|
return `@app/static-asset/policy-service/${opts.name}`;
|
|
8
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../../../../src/components/static-asset/common/keys.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,MAAM,OAAO,sBAAsB;IACjC,oPAAoP;aACpO,yBAAoB,GAAG,2BAA2B,CAAC;IAEnE,
|
|
1
|
+
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../../../../src/components/static-asset/common/keys.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,MAAM,OAAO,sBAAsB;IACjC,oPAAoP;aACpO,yBAAoB,GAAG,2BAA2B,CAAC;IAEnE,iMAAiM;IACjM,MAAM,CAAC,aAAa,CAAC,IAAsB;QACzC,OAAO,oCAAoC,IAAI,CAAC,IAAI,EAAE,CAAC;IACzD,CAAC;IAED,gGAAgG;IAChG,MAAM,CAAC,aAAa,CAAC,IAAsB;QACzC,OAAO,oCAAoC,IAAI,CAAC,IAAI,EAAE,CAAC;IACzD,CAAC","sourcesContent":["/** Container keys of the static asset component. */\nexport class StaticAssetBindingKeys {\n /** The options an application registers through `configs.artifacts` land here, because an artifact index carries classes and no options. `application.component(StaticAssetComponent, { options })` passes them directly and wins over this key. */\n static readonly STATIC_ASSET_OPTIONS = '@app/static-asset/options';\n\n /** The `StaticAssetUploadPolicyService` of one options entry, bound only for an entry that turns `directUpload` on. `name` is the entry's KEY in the options object, not the controller name. */\n static policyService(opts: { name: string }): string {\n return `@app/static-asset/policy-service/${opts.name}`;\n }\n\n /** The `StaticAssetUploadCommitService` of one options entry, bound with its policy service. */\n static commitService(opts: { name: string }): string {\n return `@app/static-asset/commit-service/${opts.name}`;\n }\n}\n"]}
|
|
@@ -1,105 +1,125 @@
|
|
|
1
|
-
import type { BaseRestController, IAuthRouteConfig, TRouteContext } from '../../../base/index.js';
|
|
1
|
+
import type { BaseRestController, IAuthRouteConfig, TRouteAuthConfig, TRouteContext } from '../../../base/index.js';
|
|
2
|
+
import type { z } from '@hono/zod-openapi';
|
|
2
3
|
import type { IDuration } from '@venizia/ignis-helpers/common';
|
|
3
|
-
import type {
|
|
4
|
-
import type { DiskHelper, IBucketRef, IFileStat, IObjectLocation, IObjectRef, IStorageHelper, IUploadResult, TUploadNaming } from '@venizia/ignis-helpers';
|
|
4
|
+
import type { DefaultCRUDRepository, TTableObject } from '@venizia/ignis-connectors/postgres';
|
|
5
|
+
import type { DiskHelper, IBucketRef, IFileStat, IObjectLocation, IObjectRef, IStorageHelper, IUploadResult, StorageTypes, TUploadNaming } from '@venizia/ignis-helpers';
|
|
5
6
|
import type { BunS3Helper } from '@venizia/ignis-helpers/bun-s3';
|
|
6
|
-
import type {
|
|
7
|
-
import type { TMultipartStorage } from '@venizia/ignis-helpers';
|
|
7
|
+
import type { TConstValue, TValueOrAsyncResolver, ValueOrPromise } from '@venizia/ignis-helpers/common';
|
|
8
8
|
import type { TMetaLinkCompatibleSchema, TMetaLinkSchema } from '../models/index.js';
|
|
9
|
-
import type {
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
normalizeNameFn?: (opts: {
|
|
24
|
-
file: TUploadNaming;
|
|
25
|
-
}) => string;
|
|
26
|
-
normalizeLinkFn?: (opts: IObjectLocation) => string;
|
|
27
|
-
/**
|
|
28
|
-
* Maximum folder nesting depth allowed in object paths.
|
|
29
|
-
*
|
|
30
|
-
* @default BaseStorageHelper.DEFAULT_MAX_FOLDER_DEPTH
|
|
31
|
-
*/
|
|
32
|
-
maxFolderDepth?: number;
|
|
33
|
-
/** Largest file the UPLOAD route accepts. Absent means no ceiling. Checked against `content-length` before spooling, then per file after parsing. */
|
|
34
|
-
maxBytes?: number;
|
|
35
|
-
[key: string]: AnyType;
|
|
36
|
-
};
|
|
37
|
-
/** Decides the key one uploaded file is stored under. `defaultKey` is what IGNIS would have written. */
|
|
38
|
-
export type TStaticAssetObjectNameResolver = (opts: {
|
|
9
|
+
import type { StaticAssetRouteKeys } from './constants.js';
|
|
10
|
+
/**
|
|
11
|
+
* A feature that is on or off. `{ use: false }` is the whole off state; `{ use: true, ... }` carries
|
|
12
|
+
* the options. One shape for every switch, so an option is never read from a feature that is off.
|
|
13
|
+
*/
|
|
14
|
+
export type TToggle<Options extends object = Record<never, never>> = {
|
|
15
|
+
use: false;
|
|
16
|
+
} | ({
|
|
17
|
+
use: true;
|
|
18
|
+
} & Options);
|
|
19
|
+
/** The storage backends a static asset entry can serve from: the helpers that declare one. */
|
|
20
|
+
export type TStaticAssetStorageType = typeof StorageTypes.DISK | typeof StorageTypes.BUN_S3;
|
|
21
|
+
/** Decides the key one uploaded file is stored under. `defaultKey` is what IGNIS would have written - the scope and the default naming - so the hook may keep it, extend it or ignore it. */
|
|
22
|
+
export type TStaticAssetObjectKeyResolver = (opts: {
|
|
39
23
|
bucket: IBucketRef;
|
|
40
24
|
file: TUploadNaming;
|
|
41
25
|
defaultKey: string;
|
|
42
26
|
}) => string;
|
|
43
|
-
/** Adds routes of the application's own to a generated asset controller. Runs after every built-in route, so a built-in route always wins a path collision. `basePath` is the mount path with exactly one leading slash. */
|
|
44
27
|
/** Adds routes of the application's own. Registration order decides a path collision, so the same shape serves both the before and the after hook. */
|
|
45
28
|
export type TStaticAssetDefineExtraRoutes = (opts: {
|
|
46
29
|
controller: BaseRestController;
|
|
47
30
|
helper: IStorageHelper;
|
|
48
31
|
basePath: string;
|
|
49
32
|
}) => void;
|
|
50
|
-
/** Path parameters of the bucket routes. */
|
|
51
|
-
export type TStaticAssetBucketParams = {
|
|
52
|
-
bucketName: string;
|
|
53
|
-
};
|
|
54
33
|
/** Path parameters of the object routes. */
|
|
55
34
|
export type TStaticAssetObjectParams = {
|
|
56
|
-
bucketName: string;
|
|
57
35
|
objectName: string;
|
|
58
36
|
};
|
|
59
37
|
/** Labels recorded on the MetaLink row of an uploaded object. */
|
|
60
|
-
export type
|
|
38
|
+
export type TStaticAssetUploadLabels = {
|
|
61
39
|
principalType?: string;
|
|
62
40
|
principalId?: string;
|
|
63
41
|
variant?: string;
|
|
64
42
|
/** Display order within the principal. Caller-supplied: deriving it would be a non-atomic read of `max(sequence)` then a write of `max + 1`. */
|
|
65
43
|
sequence?: number;
|
|
66
|
-
folderPath?: string;
|
|
67
|
-
};
|
|
68
|
-
/** Query of the list objects route. Every value arrives as a string. */
|
|
69
|
-
export type TStaticAssetListQuery = {
|
|
70
|
-
prefix?: string;
|
|
71
|
-
recursive?: string;
|
|
72
|
-
maxKeys?: string;
|
|
73
44
|
};
|
|
74
45
|
/** Enables the signed-policy upload flow, where the client posts bytes straight to the storage and IGNIS only signs the permission and verifies the commit. */
|
|
75
46
|
export type TStaticAssetDirectUploadOptions = {
|
|
76
|
-
/**
|
|
77
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Who may use direct upload. Guards every direct-upload route. There is no safe default for a
|
|
49
|
+
* route that hands out a write credential, so leaving it out is allowed only when the entry's
|
|
50
|
+
* `auth` already authenticates or authorizes: then `auth` is the gate. With neither, the
|
|
51
|
+
* component refuses to start.
|
|
52
|
+
*/
|
|
53
|
+
authorize?: (opts: {
|
|
78
54
|
context: TRouteContext;
|
|
79
55
|
}) => ValueOrPromise<boolean>;
|
|
56
|
+
/**
|
|
57
|
+
* Who the upload belongs to. Called on every direct-upload route: the policy route signs the
|
|
58
|
+
* answer into the commit token, and the other routes refuse a token whose owner differs. Absent,
|
|
59
|
+
* a token commits for whoever holds it.
|
|
60
|
+
*/
|
|
61
|
+
resolveOwner?: (opts: {
|
|
62
|
+
context: TRouteContext;
|
|
63
|
+
}) => ValueOrPromise<string>;
|
|
80
64
|
/** Signs the commit token. Keep it out of the repository and out of the client. */
|
|
81
65
|
secretKey: string;
|
|
82
66
|
/** The ceiling the policy carries as `content-length-range`. A signed PUT cannot express this at all, which is why this is a POST policy. */
|
|
83
67
|
maxBytes: number;
|
|
84
68
|
/**
|
|
85
|
-
*
|
|
69
|
+
* The most files one upload-policy request may name; more is a 422.
|
|
70
|
+
*
|
|
71
|
+
* @default StaticAssetDefaults.MAX_UPLOAD_FILES (20)
|
|
72
|
+
*/
|
|
73
|
+
maxFiles?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Where a policy may write: not empty, ends with `/`, made of valid key segments, and neither
|
|
76
|
+
* inside the entry's `keyPrefix` nor containing it - a pending object must never be readable
|
|
77
|
+
* through the entry's own routes. The final key is NEVER in a policy, so content cannot be
|
|
78
|
+
* replaced after the commit. The lifecycle rule that collects abandoned uploads targets this prefix.
|
|
86
79
|
*
|
|
87
80
|
* @default 'pending/'
|
|
88
81
|
*/
|
|
89
82
|
pendingPrefix?: string;
|
|
83
|
+
expires?: {
|
|
84
|
+
/**
|
|
85
|
+
* How long the browser may post the bytes: the POST policy's lifetime.
|
|
86
|
+
*
|
|
87
|
+
* @default StoragePresignDefaults.POST_EXPIRES_IN
|
|
88
|
+
*/
|
|
89
|
+
upload?: IDuration;
|
|
90
|
+
/**
|
|
91
|
+
* How long the commit token stays good, for validating and committing after the upload. The
|
|
92
|
+
* pending object must outlive it, so the lifecycle rule on `pendingPrefix` has to be longer still.
|
|
93
|
+
*
|
|
94
|
+
* @default `upload`
|
|
95
|
+
*/
|
|
96
|
+
commit?: IDuration;
|
|
97
|
+
};
|
|
90
98
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
99
|
+
* Checks the pending file. On, the component registers `POST {base}/upload-validate`, and
|
|
100
|
+
* `commit` runs the same check first and refuses an invalid file with `UPLOAD_INVALID`.
|
|
101
|
+
* Build it with `StaticAssetValidation.define`.
|
|
94
102
|
*/
|
|
95
|
-
|
|
96
|
-
/** Runs BEFORE the copy to the final key, so a hook that throws leaves the object under the pending prefix for a lifecycle rule to collect, rather than stranding it at the final key. */
|
|
97
|
-
onCommit?: (opts: {
|
|
98
|
-
bucket: IBucketRef;
|
|
99
|
-
pendingObject: IObjectRef;
|
|
100
|
-
object: IObjectRef;
|
|
101
|
-
}) => ValueOrPromise<void>;
|
|
103
|
+
validation: TToggle<IStaticAssetValidation<z.ZodType>>;
|
|
102
104
|
};
|
|
105
|
+
/** What a validation answers: whether the file may be committed, and the problems the user should see. */
|
|
106
|
+
export interface IStaticAssetValidationResult<Issue> {
|
|
107
|
+
isValid: boolean;
|
|
108
|
+
issues: Issue[];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The application's own check of an uploaded file, run on the PENDING object before anything is
|
|
112
|
+
* committed. The component owns the route and the refusal; the application owns only this logic.
|
|
113
|
+
* Build it with `StaticAssetValidation.define` so the handler is typed by `issueSchema`.
|
|
114
|
+
*/
|
|
115
|
+
export interface IStaticAssetValidation<IssueSchema extends z.ZodType> {
|
|
116
|
+
/** The shape of ONE issue. It types the handler's answer and documents the route's response. */
|
|
117
|
+
issueSchema: IssueSchema;
|
|
118
|
+
handler: (opts: IObjectLocation & {
|
|
119
|
+
owner?: string;
|
|
120
|
+
helper: IStorageHelper;
|
|
121
|
+
}) => ValueOrPromise<IStaticAssetValidationResult<z.infer<IssueSchema>>>;
|
|
122
|
+
}
|
|
103
123
|
/** Body of the upload policy route: the files the client intends to upload. */
|
|
104
124
|
export type TStaticAssetUploadPolicyRequest = {
|
|
105
125
|
files: Array<{
|
|
@@ -110,100 +130,129 @@ export type TStaticAssetUploadPolicyRequest = {
|
|
|
110
130
|
};
|
|
111
131
|
/** The MetaLink table belongs to the application. `Schema` defaults to the table IGNIS ships, and widens to any table whose row carries the same fields. */
|
|
112
132
|
export type TMetaLinkConfig<Schema extends TMetaLinkCompatibleSchema = TMetaLinkSchema> = {
|
|
113
|
-
model: TValueOrAsyncResolver<typeof BaseRelationalEntity<Schema>>;
|
|
114
133
|
repository: TValueOrAsyncResolver<DefaultCRUDRepository<Schema>>;
|
|
115
134
|
createMetaLink?: (opts: {
|
|
116
135
|
uploadResult: IUploadResult;
|
|
117
136
|
fileStat: IFileStat;
|
|
118
|
-
/** The caller's labels
|
|
119
|
-
|
|
137
|
+
/** The caller's labels: form fields on an upload, beside the commit token on a direct one. */
|
|
138
|
+
labels: TStaticAssetUploadLabels;
|
|
120
139
|
}) => ValueOrPromise<{
|
|
121
140
|
count: number;
|
|
122
141
|
data: TTableObject<Schema>;
|
|
123
142
|
}>;
|
|
124
143
|
};
|
|
125
|
-
/** The part of a built-in route an application may change. */
|
|
126
|
-
export type TStaticAssetRouteConfig = Partial<Omit<IAuthRouteConfig, 'method' | 'request' | 'responses'>> &
|
|
144
|
+
/** The part of a built-in route an application may change. `authenticate` and `authorize` replace the entry's `auth` for this route, and `{ skip: true }` leaves it open. */
|
|
145
|
+
export type TStaticAssetRouteConfig = Partial<Omit<IAuthRouteConfig, 'method' | 'request' | 'responses' | 'authenticate' | 'authorize'>> & TRouteAuthConfig;
|
|
146
|
+
/** Authentication and authorization for every built-in route of an entry; a route's own `authenticate`/`authorize` replaces it for that route. */
|
|
147
|
+
export type TStaticAssetAuth = TRouteAuthConfig & {
|
|
127
148
|
/**
|
|
128
|
-
*
|
|
149
|
+
* `{ use: true }` leaves `getObjectByName` and `downloadObjectByName` unauthenticated, so a
|
|
150
|
+
* browser can show an image without a token. `listObjects` is NOT public: listing a bucket is not
|
|
151
|
+
* reading an object.
|
|
129
152
|
*
|
|
130
|
-
* @default
|
|
153
|
+
* @default { use: false }
|
|
131
154
|
*/
|
|
132
|
-
|
|
155
|
+
publicRead?: TToggle;
|
|
133
156
|
};
|
|
134
|
-
/**
|
|
135
|
-
export type
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
157
|
+
/** The name of a built-in route, the key of `routes`. */
|
|
158
|
+
export type TStaticAssetRouteKey = TConstValue<typeof StaticAssetRouteKeys>;
|
|
159
|
+
/**
|
|
160
|
+
* What each built-in route does: `{ use: false }` turns it off, `{ use: true, ... }` turns it on
|
|
161
|
+
* with those overrides. A route left out is on as built.
|
|
162
|
+
*/
|
|
163
|
+
export type TStaticAssetRoutes = Partial<Record<TStaticAssetRouteKey, TToggle<TStaticAssetRouteConfig>>>;
|
|
164
|
+
/** Uploading through the application: the browser sends the bytes to `POST /objects` and the application writes them to the storage. */
|
|
165
|
+
export type TStaticAssetServerUploadOptions = {
|
|
166
|
+
/** Largest file the route accepts. Absent means no ceiling. Checked against `content-length` before the body is read, then per file after parsing. */
|
|
167
|
+
maxBytes?: number;
|
|
168
|
+
/**
|
|
169
|
+
* The most file parts one request may carry; more is refused with 413 before anything is written.
|
|
170
|
+
*
|
|
171
|
+
* @default StaticAssetDefaults.MAX_UPLOAD_FILES (20)
|
|
172
|
+
*/
|
|
173
|
+
maxFiles?: number;
|
|
174
|
+
/** The one hook that decides the key an uploaded file is stored under. Absent keeps the default naming. Under a `keyPrefix` a key outside the scope is refused, never rewritten. */
|
|
175
|
+
objectKey?: TStaticAssetObjectKeyResolver;
|
|
176
|
+
};
|
|
177
|
+
/** Direct upload is a POST policy, which only S3 can sign, and its pending objects stay out of the served scope only when the entry has a `keyPrefix`. */
|
|
178
|
+
type TStaticAssetDirectUploadScope = {
|
|
179
|
+
keyPrefix?: string;
|
|
180
|
+
directUpload: {
|
|
181
|
+
use: false;
|
|
182
|
+
};
|
|
183
|
+
} | {
|
|
184
|
+
keyPrefix: string;
|
|
185
|
+
directUpload: {
|
|
186
|
+
use: true;
|
|
187
|
+
} & TStaticAssetDirectUploadOptions;
|
|
148
188
|
};
|
|
149
|
-
/**
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
* @default true
|
|
159
|
-
*/
|
|
160
|
-
isStrict?: boolean;
|
|
161
|
-
/**
|
|
162
|
-
* `true` serves a raw nested object path (`/objects/photos/2024/f.jpg`) - a URL shape change; a percent-encoded path keeps working either way.
|
|
163
|
-
*
|
|
164
|
-
* @default false
|
|
165
|
-
*/
|
|
166
|
-
rawObjectPath?: boolean;
|
|
167
|
-
/** The single bucket every object route uses. It leaves the URL (`/objects/{objectName}`) and the four bucket-management routes are not registered. The function form is read per request, so an environment variable can be read lazily. */
|
|
168
|
-
bucket?: TValueOrAsyncResolver<string>;
|
|
169
|
-
/**
|
|
170
|
-
* The key scope this controller owns, e.g. `inventory/`. Every route refuses a key outside it
|
|
171
|
-
* as a 404 without calling the storage, `listObjects` lists only inside it, and an upload key
|
|
172
|
-
* must land inside it: the default name is placed there, a `resolveObjectName` or
|
|
173
|
-
* `extra.normalizeNameFn` key outside it is refused. `maxFolderDepth` counts the folders below
|
|
174
|
-
* it.
|
|
175
|
-
*
|
|
176
|
-
* @default every key in the bucket
|
|
177
|
-
*/
|
|
178
|
-
keyPrefix?: string;
|
|
179
|
-
routes?: TStaticAssetRoutes;
|
|
180
|
-
/** Direct upload: the browser posts the bytes to the storage and IGNIS only signs the permission. Absent leaves both routes unregistered. Requires `bucket` - a policy names one bucket, and a bucket in the URL is a bucket the caller chooses. */
|
|
181
|
-
directUpload?: TStaticAssetDirectUploadOptions;
|
|
182
|
-
};
|
|
183
|
-
extra?: TStaticAssetExtraOptions;
|
|
184
|
-
/** Decides the stored object name of each uploaded file; absent keeps the storage helper's own naming. */
|
|
185
|
-
resolveObjectName?: TStaticAssetObjectNameResolver;
|
|
186
|
-
/** Registers the application's own routes BEFORE every built-in one, so a literal path wins over the catch-all `rawObjectPath` produces. Hono matches in registration order. */
|
|
187
|
-
defineRoutesBefore?: TStaticAssetDefineExtraRoutes;
|
|
188
|
-
/** Registers the application's own routes on the generated controller, after every built-in one. */
|
|
189
|
-
defineExtraRoutes?: TStaticAssetDefineExtraRoutes;
|
|
190
|
-
} & ({
|
|
191
|
-
storage: typeof StaticAssetStorageTypes.BUN_S3;
|
|
192
|
-
helper: BunS3Helper;
|
|
193
|
-
} | {
|
|
194
|
-
storage: typeof StaticAssetStorageTypes.DISK;
|
|
195
|
-
helper: DiskHelper;
|
|
196
|
-
}) & ({
|
|
197
|
-
useMetaLink?: false | undefined;
|
|
198
|
-
} | {
|
|
199
|
-
useMetaLink: true;
|
|
200
|
-
metaLink: TMetaLinkConfig<Schema>;
|
|
201
|
-
});
|
|
189
|
+
/** The storage helper decides which direct upload shapes are possible. */
|
|
190
|
+
type TStaticAssetStorage = ({
|
|
191
|
+
helper: BunS3Helper;
|
|
192
|
+
} & TStaticAssetDirectUploadScope) | {
|
|
193
|
+
helper: DiskHelper;
|
|
194
|
+
keyPrefix?: string;
|
|
195
|
+
directUpload: {
|
|
196
|
+
use: false;
|
|
197
|
+
};
|
|
202
198
|
};
|
|
203
|
-
/**
|
|
199
|
+
/** One storage backend with its generated controller. Every switch is a toggle; a toggle left out is a type error, so nothing is on or off by accident. */
|
|
200
|
+
export type TStaticAssetEntry<Schema extends TMetaLinkCompatibleSchema = TMetaLinkSchema> = {
|
|
201
|
+
basePath: string;
|
|
202
|
+
/** The controller's name. Absent, it is derived from the entry's key. */
|
|
203
|
+
name?: string;
|
|
204
|
+
/** The single bucket every route uses. The function form is read per request, so an environment variable can be read lazily. */
|
|
205
|
+
bucket: TValueOrAsyncResolver<string>;
|
|
206
|
+
/**
|
|
207
|
+
* The key scope this entry owns, e.g. `inventory/`. Every route refuses a key outside it as a
|
|
208
|
+
* 404 without calling the storage, `listObjects` lists only inside it, and an upload key must
|
|
209
|
+
* land inside it. `maxFolderDepth` counts the folders below it. Required with
|
|
210
|
+
* `directUpload`: it keeps the pending prefix outside everything the routes serve.
|
|
211
|
+
*
|
|
212
|
+
* @default every key in the bucket
|
|
213
|
+
*/
|
|
214
|
+
keyPrefix?: string;
|
|
215
|
+
/**
|
|
216
|
+
* Folders a key may nest below the `keyPrefix`, for every route of the entry: reads and deletes
|
|
217
|
+
* too, so it holds for keys that arrive by direct upload or ingest.
|
|
218
|
+
*
|
|
219
|
+
* @default BaseStorageHelper.DEFAULT_MAX_FOLDER_DEPTH
|
|
220
|
+
*/
|
|
221
|
+
maxFolderDepth?: number;
|
|
222
|
+
/**
|
|
223
|
+
* `true` serves a raw nested object path (`/objects/photos/2024/f.jpg`) - a URL shape change; a percent-encoded path keeps working either way.
|
|
224
|
+
*
|
|
225
|
+
* @default false
|
|
226
|
+
*/
|
|
227
|
+
rawObjectPath?: boolean;
|
|
228
|
+
/** The link stored and returned for an object. Absent builds the URL of this controller's own object route. */
|
|
229
|
+
link?: (opts: IObjectLocation) => string;
|
|
230
|
+
/** Authentication and authorization of every built-in route of this entry. Absent leaves every route open; `publicRead` is the only way a read route is public on purpose. */
|
|
231
|
+
auth?: TStaticAssetAuth;
|
|
232
|
+
serverUpload: TToggle<TStaticAssetServerUploadOptions>;
|
|
233
|
+
/** Every upload also writes a MetaLink row, and the object's deletion removes it. */
|
|
234
|
+
metaLink: TToggle<TMetaLinkConfig<Schema>>;
|
|
235
|
+
routes?: TStaticAssetRoutes;
|
|
236
|
+
extend?: {
|
|
237
|
+
/** Registered BEFORE every built-in route, so a literal path wins over the catch-all `rawObjectPath` produces. Hono matches in registration order. */
|
|
238
|
+
before?: TStaticAssetDefineExtraRoutes;
|
|
239
|
+
/** Registered after every built-in route. */
|
|
240
|
+
after?: TStaticAssetDefineExtraRoutes;
|
|
241
|
+
};
|
|
242
|
+
} & TStaticAssetStorage;
|
|
243
|
+
/** Static asset entries keyed by an application-chosen name; each key becomes one controller. */
|
|
244
|
+
export type TStaticAssetComponentOptions<Schema extends TMetaLinkCompatibleSchema = TMetaLinkSchema> = Record<string, TStaticAssetEntry<Schema>>;
|
|
245
|
+
/**
|
|
246
|
+
* What a commit token binds: the pending object a policy was issued for, when the grant dies and,
|
|
247
|
+
* when the application supplies one, who it was issued to. Other business fields are deliberately
|
|
248
|
+
* absent - which order or tenant an upload belongs to is the application's.
|
|
249
|
+
*/
|
|
204
250
|
export interface IStaticAssetCommitTokenPayload {
|
|
205
|
-
bucket:
|
|
206
|
-
|
|
251
|
+
bucket: IBucketRef;
|
|
252
|
+
object: IObjectRef;
|
|
207
253
|
expiresAt: number;
|
|
254
|
+
/** Absent when `directUpload.resolveOwner` is not set; present, the token commits only for that owner. */
|
|
255
|
+
owner?: string;
|
|
208
256
|
}
|
|
257
|
+
export {};
|
|
209
258
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/components/static-asset/common/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { BaseRestController, IAuthRouteConfig, TRouteContext } from '@/base';\nimport type { IDuration } from '@venizia/ignis-helpers/common';\nimport type {\n BaseRelationalEntity,\n DefaultCRUDRepository,\n TTableObject,\n} from '@venizia/ignis-connectors/postgres';\nimport type {\n DiskHelper,\n IBucketRef,\n IFileStat,\n IObjectLocation,\n IObjectRef,\n IStorageHelper,\n IUploadResult,\n TUploadNaming,\n} from '@venizia/ignis-helpers';\nimport type { BunS3Helper } from '@venizia/ignis-helpers/bun-s3';\nimport type { AnyType, TValueOrAsyncResolver, ValueOrPromise } from '@venizia/ignis-helpers/common';\nimport type { TMultipartStorage } from '@venizia/ignis-helpers';\nimport type { TMetaLinkCompatibleSchema, TMetaLinkSchema } from '../models';\nimport type { StaticAssetStorageTypes } from './constants';\n\n/** Per-controller upload tuning: multipart parsing, name and link normalization, folder depth and size ceiling. */\nexport type TStaticAssetExtraOptions = {\n parseMultipartBody?: {\n storage?: TMultipartStorage;\n uploadDir?: string;\n /**\n * The most file parts one upload request may carry; more is refused with 413 before anything is written. The upload-policy request is bounded by the same number (more is a 422).\n *\n * @default StaticAssetDefaults.MAX_UPLOAD_FILES (20)\n */\n maxFiles?: number;\n };\n\n /** `folderPath` carries the upload query's target folder - dropping it flattens nested uploads. */\n normalizeNameFn?: (opts: { file: TUploadNaming }) => string;\n normalizeLinkFn?: (opts: IObjectLocation) => string;\n\n /**\n * Maximum folder nesting depth allowed in object paths.\n *\n * @default BaseStorageHelper.DEFAULT_MAX_FOLDER_DEPTH\n */\n maxFolderDepth?: number;\n\n /** Largest file the UPLOAD route accepts. Absent means no ceiling. Checked against `content-length` before spooling, then per file after parsing. */\n maxBytes?: number;\n [key: string]: AnyType;\n};\n\n/** Decides the key one uploaded file is stored under. `defaultKey` is what IGNIS would have written. */\nexport type TStaticAssetObjectNameResolver = (opts: {\n bucket: IBucketRef;\n file: TUploadNaming;\n defaultKey: string;\n}) => string;\n\n/** Adds routes of the application's own to a generated asset controller. Runs after every built-in route, so a built-in route always wins a path collision. `basePath` is the mount path with exactly one leading slash. */\n/** Adds routes of the application's own. Registration order decides a path collision, so the same shape serves both the before and the after hook. */\nexport type TStaticAssetDefineExtraRoutes = (opts: {\n controller: BaseRestController;\n helper: IStorageHelper;\n basePath: string;\n}) => void;\n\n// Declared by hand rather than inferred: RouteHandler inference here is heavy.\n/** Path parameters of the bucket routes. */\nexport type TStaticAssetBucketParams = { bucketName: string };\n/** Path parameters of the object routes. */\nexport type TStaticAssetObjectParams = { bucketName: string; objectName: string };\n/** Labels recorded on the MetaLink row of an uploaded object. */\nexport type TStaticAssetUploadQuery = {\n principalType?: string;\n principalId?: string;\n variant?: string;\n /** Display order within the principal. Caller-supplied: deriving it would be a non-atomic read of `max(sequence)` then a write of `max + 1`. */\n sequence?: number;\n folderPath?: string;\n};\n/** Query of the list objects route. Every value arrives as a string. */\nexport type TStaticAssetListQuery = { prefix?: string; recursive?: string; maxKeys?: string };\n\n/** Enables the signed-policy upload flow, where the client posts bytes straight to the storage and IGNIS only signs the permission and verifies the commit. */\nexport type TStaticAssetDirectUploadOptions = {\n /** Who may ask for a policy. REQUIRED, not defaulted: this route hands out a write credential, and there is no safe default for that. */\n authorize: (opts: { context: TRouteContext }) => ValueOrPromise<boolean>;\n\n /** Signs the commit token. Keep it out of the repository and out of the client. */\n secretKey: string;\n\n /** The ceiling the policy carries as `content-length-range`. A signed PUT cannot express this at all, which is why this is a POST policy. */\n maxBytes: number;\n\n /**\n * Where a policy may write. The final key is NEVER in a policy, so content cannot be replaced after the commit.\n *\n * @default 'pending/'\n */\n pendingPrefix?: string;\n\n /**\n * How long a policy and its commit token stay good.\n *\n * @default StoragePresignDefaults.POST_EXPIRES_IN\n */\n expiresIn?: IDuration;\n\n /** Runs BEFORE the copy to the final key, so a hook that throws leaves the object under the pending prefix for a lifecycle rule to collect, rather than stranding it at the final key. */\n onCommit?: (opts: {\n bucket: IBucketRef;\n pendingObject: IObjectRef;\n object: IObjectRef;\n }) => ValueOrPromise<void>;\n};\n\n/** Body of the upload policy route: the files the client intends to upload. */\nexport type TStaticAssetUploadPolicyRequest = {\n files: Array<{ fileName: string; contentType?: string; size?: number }>;\n};\n\n/** The MetaLink table belongs to the application. `Schema` defaults to the table IGNIS ships, and widens to any table whose row carries the same fields. */\nexport type TMetaLinkConfig<Schema extends TMetaLinkCompatibleSchema = TMetaLinkSchema> = {\n model: TValueOrAsyncResolver<typeof BaseRelationalEntity<Schema>>;\n repository: TValueOrAsyncResolver<DefaultCRUDRepository<Schema>>;\n createMetaLink?: (opts: {\n uploadResult: IUploadResult;\n fileStat: IFileStat;\n /** The caller's labels. Named for where they used to travel - they arrive in the request BODY now, as form fields on an upload and beside the commit token on a direct one. */\n query: TStaticAssetUploadQuery;\n }) => ValueOrPromise<{ count: number; data: TTableObject<Schema> }>;\n};\n\n/** The part of a built-in route an application may change. */\nexport type TStaticAssetRouteConfig = Partial<\n Omit<IAuthRouteConfig, 'method' | 'request' | 'responses'>\n> & {\n /**\n * Whether this route is registered.\n *\n * @default true\n */\n enabled?: boolean;\n};\n\n/** Per-route overrides for the built-in routes of a generated asset controller. A key left out keeps that route as built. */\nexport type TStaticAssetRoutes = {\n getBuckets?: TStaticAssetRouteConfig;\n getBucketByName?: TStaticAssetRouteConfig;\n createBucket?: TStaticAssetRouteConfig;\n deleteBucket?: TStaticAssetRouteConfig;\n\n upload?: TStaticAssetRouteConfig;\n listObjects?: TStaticAssetRouteConfig;\n deleteObject?: TStaticAssetRouteConfig;\n getObjectByName?: TStaticAssetRouteConfig;\n downloadObjectByName?: TStaticAssetRouteConfig;\n\n uploadPolicy?: TStaticAssetRouteConfig;\n uploadCommit?: TStaticAssetRouteConfig;\n\n recreateMetaLink?: TStaticAssetRouteConfig;\n};\n\n/** Static asset controllers keyed by an application-chosen name. Each entry pairs a storage type with its matching helper, and `useMetaLink: true` requires a `metaLink` config. */\nexport type TStaticAssetComponentOptions<\n Schema extends TMetaLinkCompatibleSchema = TMetaLinkSchema,\n> = {\n [key: string]: {\n controller: {\n name: string;\n basePath: string;\n\n /**\n * Whether `/path` and `/path/` are different routes on this controller.\n *\n * @default true\n */\n isStrict?: boolean;\n\n /**\n * `true` serves a raw nested object path (`/objects/photos/2024/f.jpg`) - a URL shape change; a percent-encoded path keeps working either way.\n *\n * @default false\n */\n rawObjectPath?: boolean;\n\n /** The single bucket every object route uses. It leaves the URL (`/objects/{objectName}`) and the four bucket-management routes are not registered. The function form is read per request, so an environment variable can be read lazily. */\n bucket?: TValueOrAsyncResolver<string>;\n\n /**\n * The key scope this controller owns, e.g. `inventory/`. Every route refuses a key outside it\n * as a 404 without calling the storage, `listObjects` lists only inside it, and an upload key\n * must land inside it: the default name is placed there, a `resolveObjectName` or\n * `extra.normalizeNameFn` key outside it is refused. `maxFolderDepth` counts the folders below\n * it.\n *\n * @default every key in the bucket\n */\n keyPrefix?: string;\n\n routes?: TStaticAssetRoutes;\n\n /** Direct upload: the browser posts the bytes to the storage and IGNIS only signs the permission. Absent leaves both routes unregistered. Requires `bucket` - a policy names one bucket, and a bucket in the URL is a bucket the caller chooses. */\n directUpload?: TStaticAssetDirectUploadOptions;\n };\n extra?: TStaticAssetExtraOptions;\n\n /** Decides the stored object name of each uploaded file; absent keeps the storage helper's own naming. */\n resolveObjectName?: TStaticAssetObjectNameResolver;\n\n /** Registers the application's own routes BEFORE every built-in one, so a literal path wins over the catch-all `rawObjectPath` produces. Hono matches in registration order. */\n defineRoutesBefore?: TStaticAssetDefineExtraRoutes;\n\n /** Registers the application's own routes on the generated controller, after every built-in one. */\n defineExtraRoutes?: TStaticAssetDefineExtraRoutes;\n } & (\n | { storage: typeof StaticAssetStorageTypes.BUN_S3; helper: BunS3Helper }\n | { storage: typeof StaticAssetStorageTypes.DISK; helper: DiskHelper }\n ) &\n (\n | { useMetaLink?: false | undefined }\n | { useMetaLink: true; metaLink: TMetaLinkConfig<Schema> }\n );\n};\n\n/** What a commit token binds. Business fields are deliberately absent - which order or tenant an upload belongs to is the application's, and a token the framework signs is the wrong place to put its authorization. */\nexport interface IStaticAssetCommitTokenPayload {\n bucket: string;\n key: string;\n expiresAt: number;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/components/static-asset/common/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { BaseRestController, IAuthRouteConfig, TRouteAuthConfig, TRouteContext } from '@/base';\nimport type { z } from '@hono/zod-openapi';\nimport type { IDuration } from '@venizia/ignis-helpers/common';\nimport type { DefaultCRUDRepository, TTableObject } from '@venizia/ignis-connectors/postgres';\nimport type {\n DiskHelper,\n IBucketRef,\n IFileStat,\n IObjectLocation,\n IObjectRef,\n IStorageHelper,\n IUploadResult,\n StorageTypes,\n TUploadNaming,\n} from '@venizia/ignis-helpers';\nimport type { BunS3Helper } from '@venizia/ignis-helpers/bun-s3';\nimport type {\n TConstValue,\n TValueOrAsyncResolver,\n ValueOrPromise,\n} from '@venizia/ignis-helpers/common';\nimport type { TMetaLinkCompatibleSchema, TMetaLinkSchema } from '../models';\nimport type { StaticAssetRouteKeys } from './constants';\n\n/**\n * A feature that is on or off. `{ use: false }` is the whole off state; `{ use: true, ... }` carries\n * the options. One shape for every switch, so an option is never read from a feature that is off.\n */\nexport type TToggle<Options extends object = Record<never, never>> =\n | { use: false }\n | ({ use: true } & Options);\n\n/** The storage backends a static asset entry can serve from: the helpers that declare one. */\nexport type TStaticAssetStorageType = typeof StorageTypes.DISK | typeof StorageTypes.BUN_S3;\n\n/** Decides the key one uploaded file is stored under. `defaultKey` is what IGNIS would have written - the scope and the default naming - so the hook may keep it, extend it or ignore it. */\nexport type TStaticAssetObjectKeyResolver = (opts: {\n bucket: IBucketRef;\n file: TUploadNaming;\n defaultKey: string;\n}) => string;\n\n/** Adds routes of the application's own. Registration order decides a path collision, so the same shape serves both the before and the after hook. */\nexport type TStaticAssetDefineExtraRoutes = (opts: {\n controller: BaseRestController;\n helper: IStorageHelper;\n basePath: string;\n}) => void;\n\n/** Path parameters of the object routes. */\nexport type TStaticAssetObjectParams = { objectName: string };\n\n/** Labels recorded on the MetaLink row of an uploaded object. */\nexport type TStaticAssetUploadLabels = {\n principalType?: string;\n principalId?: string;\n variant?: string;\n /** Display order within the principal. Caller-supplied: deriving it would be a non-atomic read of `max(sequence)` then a write of `max + 1`. */\n sequence?: number;\n};\n\n/** Enables the signed-policy upload flow, where the client posts bytes straight to the storage and IGNIS only signs the permission and verifies the commit. */\nexport type TStaticAssetDirectUploadOptions = {\n /**\n * Who may use direct upload. Guards every direct-upload route. There is no safe default for a\n * route that hands out a write credential, so leaving it out is allowed only when the entry's\n * `auth` already authenticates or authorizes: then `auth` is the gate. With neither, the\n * component refuses to start.\n */\n authorize?: (opts: { context: TRouteContext }) => ValueOrPromise<boolean>;\n\n /**\n * Who the upload belongs to. Called on every direct-upload route: the policy route signs the\n * answer into the commit token, and the other routes refuse a token whose owner differs. Absent,\n * a token commits for whoever holds it.\n */\n resolveOwner?: (opts: { context: TRouteContext }) => ValueOrPromise<string>;\n\n /** Signs the commit token. Keep it out of the repository and out of the client. */\n secretKey: string;\n\n /** The ceiling the policy carries as `content-length-range`. A signed PUT cannot express this at all, which is why this is a POST policy. */\n maxBytes: number;\n\n /**\n * The most files one upload-policy request may name; more is a 422.\n *\n * @default StaticAssetDefaults.MAX_UPLOAD_FILES (20)\n */\n maxFiles?: number;\n\n /**\n * Where a policy may write: not empty, ends with `/`, made of valid key segments, and neither\n * inside the entry's `keyPrefix` nor containing it - a pending object must never be readable\n * through the entry's own routes. The final key is NEVER in a policy, so content cannot be\n * replaced after the commit. The lifecycle rule that collects abandoned uploads targets this prefix.\n *\n * @default 'pending/'\n */\n pendingPrefix?: string;\n\n expires?: {\n /**\n * How long the browser may post the bytes: the POST policy's lifetime.\n *\n * @default StoragePresignDefaults.POST_EXPIRES_IN\n */\n upload?: IDuration;\n\n /**\n * How long the commit token stays good, for validating and committing after the upload. The\n * pending object must outlive it, so the lifecycle rule on `pendingPrefix` has to be longer still.\n *\n * @default `upload`\n */\n commit?: IDuration;\n };\n\n /**\n * Checks the pending file. On, the component registers `POST {base}/upload-validate`, and\n * `commit` runs the same check first and refuses an invalid file with `UPLOAD_INVALID`.\n * Build it with `StaticAssetValidation.define`.\n */\n validation: TToggle<IStaticAssetValidation<z.ZodType>>;\n};\n\n/** What a validation answers: whether the file may be committed, and the problems the user should see. */\nexport interface IStaticAssetValidationResult<Issue> {\n isValid: boolean;\n issues: Issue[];\n}\n\n/**\n * The application's own check of an uploaded file, run on the PENDING object before anything is\n * committed. The component owns the route and the refusal; the application owns only this logic.\n * Build it with `StaticAssetValidation.define` so the handler is typed by `issueSchema`.\n */\nexport interface IStaticAssetValidation<IssueSchema extends z.ZodType> {\n /** The shape of ONE issue. It types the handler's answer and documents the route's response. */\n issueSchema: IssueSchema;\n handler: (\n opts: IObjectLocation & { owner?: string; helper: IStorageHelper },\n ) => ValueOrPromise<IStaticAssetValidationResult<z.infer<IssueSchema>>>;\n}\n\n/** Body of the upload policy route: the files the client intends to upload. */\nexport type TStaticAssetUploadPolicyRequest = {\n files: Array<{ fileName: string; contentType?: string; size?: number }>;\n};\n\n/** The MetaLink table belongs to the application. `Schema` defaults to the table IGNIS ships, and widens to any table whose row carries the same fields. */\nexport type TMetaLinkConfig<Schema extends TMetaLinkCompatibleSchema = TMetaLinkSchema> = {\n repository: TValueOrAsyncResolver<DefaultCRUDRepository<Schema>>;\n createMetaLink?: (opts: {\n uploadResult: IUploadResult;\n fileStat: IFileStat;\n /** The caller's labels: form fields on an upload, beside the commit token on a direct one. */\n labels: TStaticAssetUploadLabels;\n }) => ValueOrPromise<{ count: number; data: TTableObject<Schema> }>;\n};\n\n/** The part of a built-in route an application may change. `authenticate` and `authorize` replace the entry's `auth` for this route, and `{ skip: true }` leaves it open. */\nexport type TStaticAssetRouteConfig = Partial<\n Omit<IAuthRouteConfig, 'method' | 'request' | 'responses' | 'authenticate' | 'authorize'>\n> &\n TRouteAuthConfig;\n\n/** Authentication and authorization for every built-in route of an entry; a route's own `authenticate`/`authorize` replaces it for that route. */\nexport type TStaticAssetAuth = TRouteAuthConfig & {\n /**\n * `{ use: true }` leaves `getObjectByName` and `downloadObjectByName` unauthenticated, so a\n * browser can show an image without a token. `listObjects` is NOT public: listing a bucket is not\n * reading an object.\n *\n * @default { use: false }\n */\n publicRead?: TToggle;\n};\n\n/** The name of a built-in route, the key of `routes`. */\nexport type TStaticAssetRouteKey = TConstValue<typeof StaticAssetRouteKeys>;\n\n/**\n * What each built-in route does: `{ use: false }` turns it off, `{ use: true, ... }` turns it on\n * with those overrides. A route left out is on as built.\n */\nexport type TStaticAssetRoutes = Partial<\n Record<TStaticAssetRouteKey, TToggle<TStaticAssetRouteConfig>>\n>;\n\n/** Uploading through the application: the browser sends the bytes to `POST /objects` and the application writes them to the storage. */\nexport type TStaticAssetServerUploadOptions = {\n /** Largest file the route accepts. Absent means no ceiling. Checked against `content-length` before the body is read, then per file after parsing. */\n maxBytes?: number;\n\n /**\n * The most file parts one request may carry; more is refused with 413 before anything is written.\n *\n * @default StaticAssetDefaults.MAX_UPLOAD_FILES (20)\n */\n maxFiles?: number;\n\n /** The one hook that decides the key an uploaded file is stored under. Absent keeps the default naming. Under a `keyPrefix` a key outside the scope is refused, never rewritten. */\n objectKey?: TStaticAssetObjectKeyResolver;\n};\n\n/** Direct upload is a POST policy, which only S3 can sign, and its pending objects stay out of the served scope only when the entry has a `keyPrefix`. */\ntype TStaticAssetDirectUploadScope =\n | { keyPrefix?: string; directUpload: { use: false } }\n | { keyPrefix: string; directUpload: { use: true } & TStaticAssetDirectUploadOptions };\n\n/** The storage helper decides which direct upload shapes are possible. */\ntype TStaticAssetStorage =\n | ({ helper: BunS3Helper } & TStaticAssetDirectUploadScope)\n | { helper: DiskHelper; keyPrefix?: string; directUpload: { use: false } };\n\n/** One storage backend with its generated controller. Every switch is a toggle; a toggle left out is a type error, so nothing is on or off by accident. */\nexport type TStaticAssetEntry<Schema extends TMetaLinkCompatibleSchema = TMetaLinkSchema> = {\n basePath: string;\n\n /** The controller's name. Absent, it is derived from the entry's key. */\n name?: string;\n\n /** The single bucket every route uses. The function form is read per request, so an environment variable can be read lazily. */\n bucket: TValueOrAsyncResolver<string>;\n\n /**\n * The key scope this entry owns, e.g. `inventory/`. Every route refuses a key outside it as a\n * 404 without calling the storage, `listObjects` lists only inside it, and an upload key must\n * land inside it. `maxFolderDepth` counts the folders below it. Required with\n * `directUpload`: it keeps the pending prefix outside everything the routes serve.\n *\n * @default every key in the bucket\n */\n keyPrefix?: string;\n\n /**\n * Folders a key may nest below the `keyPrefix`, for every route of the entry: reads and deletes\n * too, so it holds for keys that arrive by direct upload or ingest.\n *\n * @default BaseStorageHelper.DEFAULT_MAX_FOLDER_DEPTH\n */\n maxFolderDepth?: number;\n\n /**\n * `true` serves a raw nested object path (`/objects/photos/2024/f.jpg`) - a URL shape change; a percent-encoded path keeps working either way.\n *\n * @default false\n */\n rawObjectPath?: boolean;\n\n /** The link stored and returned for an object. Absent builds the URL of this controller's own object route. */\n link?: (opts: IObjectLocation) => string;\n\n /** Authentication and authorization of every built-in route of this entry. Absent leaves every route open; `publicRead` is the only way a read route is public on purpose. */\n auth?: TStaticAssetAuth;\n\n serverUpload: TToggle<TStaticAssetServerUploadOptions>;\n\n /** Every upload also writes a MetaLink row, and the object's deletion removes it. */\n metaLink: TToggle<TMetaLinkConfig<Schema>>;\n\n routes?: TStaticAssetRoutes;\n\n extend?: {\n /** Registered BEFORE every built-in route, so a literal path wins over the catch-all `rawObjectPath` produces. Hono matches in registration order. */\n before?: TStaticAssetDefineExtraRoutes;\n /** Registered after every built-in route. */\n after?: TStaticAssetDefineExtraRoutes;\n };\n} & TStaticAssetStorage;\n\n/** Static asset entries keyed by an application-chosen name; each key becomes one controller. */\nexport type TStaticAssetComponentOptions<\n Schema extends TMetaLinkCompatibleSchema = TMetaLinkSchema,\n> = Record<string, TStaticAssetEntry<Schema>>;\n\n/**\n * What a commit token binds: the pending object a policy was issued for, when the grant dies and,\n * when the application supplies one, who it was issued to. Other business fields are deliberately\n * absent - which order or tenant an upload belongs to is the application's.\n */\nexport interface IStaticAssetCommitTokenPayload {\n bucket: IBucketRef;\n object: IObjectRef;\n expiresAt: number;\n /** Absent when `directUpload.resolveOwner` is not set; present, the token commits only for that owner. */\n owner?: string;\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { z } from '@hono/zod-openapi';
|
|
2
|
+
import type { IStaticAssetValidation } from './types.js';
|
|
3
|
+
/** Defines the application's check of an uploaded file; see `directUpload.validation`. */
|
|
4
|
+
export declare class StaticAssetValidation {
|
|
5
|
+
/** Returns `opts` switched on. It exists to infer `IssueSchema`, so a handler that answers another shape of issue fails type-checking. */
|
|
6
|
+
static define<IssueSchema extends z.ZodType>(opts: IStaticAssetValidation<IssueSchema>): IStaticAssetValidation<IssueSchema> & {
|
|
7
|
+
use: true;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Defines the application's check of an uploaded file; see `directUpload.validation`. */
|
|
2
|
+
export class StaticAssetValidation {
|
|
3
|
+
/** Returns `opts` switched on. It exists to infer `IssueSchema`, so a handler that answers another shape of issue fails type-checking. */
|
|
4
|
+
static define(opts) {
|
|
5
|
+
return { use: true, ...opts };
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../../../../src/components/static-asset/common/validation.ts"],"names":[],"mappings":"AAGA,0FAA0F;AAC1F,MAAM,OAAO,qBAAqB;IAChC,0IAA0I;IAC1I,MAAM,CAAC,MAAM,CACX,IAAyC;QAEzC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC;IAChC,CAAC;CACF","sourcesContent":["import type { z } from '@hono/zod-openapi';\nimport type { IStaticAssetValidation } from './types';\n\n/** Defines the application's check of an uploaded file; see `directUpload.validation`. */\nexport class StaticAssetValidation {\n /** Returns `opts` switched on. It exists to infer `IssueSchema`, so a handler that answers another shape of issue fails type-checking. */\n static define<IssueSchema extends z.ZodType>(\n opts: IStaticAssetValidation<IssueSchema>,\n ): IStaticAssetValidation<IssueSchema> & { use: true } {\n return { use: true, ...opts };\n }\n}\n"]}
|
|
@@ -8,7 +8,7 @@ export declare class StaticAssetComponent<Schema extends TMetaLinkCompatibleSche
|
|
|
8
8
|
private application;
|
|
9
9
|
constructor(application: BaseApplication);
|
|
10
10
|
binding(): ValueOrPromise<void>;
|
|
11
|
-
/** One pair of services per entry that turns direct upload on. The routes run the very objects an application's service resolves from the keys.
|
|
11
|
+
/** One pair of services per entry that turns direct upload on. The routes run the very objects an application's service resolves from the keys. */
|
|
12
12
|
private bindDirectUploadServices;
|
|
13
13
|
}
|
|
14
14
|
//# sourceMappingURL=component.d.ts.map
|
|
@@ -17,7 +17,7 @@ import { CoreBindings } from '@venizia/ignis-kernel';
|
|
|
17
17
|
import { Binding } from '@venizia/ignis-kernel';
|
|
18
18
|
import { StaticAssetBindingKeys } from './common/index.js';
|
|
19
19
|
import { StaticAssetControllerFactory } from './controller/index.js';
|
|
20
|
-
import {
|
|
20
|
+
import { StaticAssetEntryUtility } from './utilities/entry.utility.js';
|
|
21
21
|
/** `Schema` is the MetaLink table the application binds. It has to live on the CLASS, not only on the options: `component(Ctor, { options })` infers the options from the class, so a class pinned to the shipped table can only carry options for that table. */
|
|
22
22
|
let StaticAssetComponent = StaticAssetComponent_1 = class StaticAssetComponent extends BaseComponent {
|
|
23
23
|
constructor(application) {
|
|
@@ -39,69 +39,27 @@ let StaticAssetComponent = StaticAssetComponent_1 = class StaticAssetComponent e
|
|
|
39
39
|
this.application.get({
|
|
40
40
|
key: StaticAssetBindingKeys.STATIC_ASSET_OPTIONS,
|
|
41
41
|
});
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
this.application.
|
|
47
|
-
controller,
|
|
48
|
-
...directUploadServices,
|
|
49
|
-
storage,
|
|
50
|
-
helper,
|
|
51
|
-
useMetaLink: opt.useMetaLink,
|
|
52
|
-
metaLink: opt.useMetaLink ? opt.metaLink : undefined,
|
|
53
|
-
resolveObjectName,
|
|
54
|
-
defineRoutesBefore,
|
|
55
|
-
defineExtraRoutes,
|
|
56
|
-
options: {
|
|
57
|
-
...extra,
|
|
58
|
-
normalizeLinkFn: extra?.normalizeLinkFn ??
|
|
59
|
-
// The link follows the URL shape the controller serves: `{objectName}` is one Hono path segment unless `rawObjectPath` widens it, and a configured bucket leaves the path.
|
|
60
|
-
((linkOptions) => StaticAssetControllerFactory.buildObjectLink({
|
|
61
|
-
basePath: controller.basePath,
|
|
62
|
-
bucket: linkOptions.bucket,
|
|
63
|
-
object: linkOptions.object,
|
|
64
|
-
hasConfiguredBucket: controller.bucket !== undefined,
|
|
65
|
-
rawObjectPath: controller.rawObjectPath,
|
|
66
|
-
})),
|
|
67
|
-
},
|
|
68
|
-
}));
|
|
69
|
-
this.application.logger.info(`[binding] Asset storage is bound | Key: %s | Storage type: %s | UseMetaLink: %s`, key, storage, Boolean(opt.useMetaLink));
|
|
42
|
+
for (const [key, entry] of Object.entries(componentOptions)) {
|
|
43
|
+
const resolved = StaticAssetEntryUtility.resolve({ key, entry });
|
|
44
|
+
const services = this.bindDirectUploadServices({ key, resolved });
|
|
45
|
+
this.application.controller(StaticAssetControllerFactory.buildResolved({ resolved, ...services }));
|
|
46
|
+
this.application.logger.info(`[binding] Asset storage is bound | Key: %s | Mount: %s | Storage type: %s | Direct upload: %s | MetaLink: %s`, key, resolved.basePath, resolved.storage, resolved.directUpload !== undefined, resolved.metaLink !== undefined);
|
|
70
47
|
}
|
|
71
48
|
}
|
|
72
|
-
/** One pair of services per entry that turns direct upload on. The routes run the very objects an application's service resolves from the keys.
|
|
49
|
+
/** One pair of services per entry that turns direct upload on. The routes run the very objects an application's service resolves from the keys. */
|
|
73
50
|
bindDirectUploadServices(opts) {
|
|
74
|
-
const { key,
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
if (!directUpload || bucket === undefined) {
|
|
51
|
+
const { key, resolved } = opts;
|
|
52
|
+
const services = StaticAssetEntryUtility.buildDirectUploadServices({ resolved });
|
|
53
|
+
if (!services) {
|
|
78
54
|
return undefined;
|
|
79
55
|
}
|
|
80
|
-
const policyService = new StaticAssetUploadPolicyService({
|
|
81
|
-
scope: controller.name,
|
|
82
|
-
helper,
|
|
83
|
-
bucket,
|
|
84
|
-
keyPrefix: controller.keyPrefix,
|
|
85
|
-
directUpload,
|
|
86
|
-
});
|
|
87
|
-
const commitService = new StaticAssetUploadCommitService({
|
|
88
|
-
scope: controller.name,
|
|
89
|
-
helper,
|
|
90
|
-
keyPrefix: controller.keyPrefix,
|
|
91
|
-
directUpload,
|
|
92
|
-
storage,
|
|
93
|
-
useMetaLink: entry.useMetaLink,
|
|
94
|
-
metaLink: entry.useMetaLink ? entry.metaLink : undefined,
|
|
95
|
-
basePath: controller.basePath,
|
|
96
|
-
rawObjectPath: controller.rawObjectPath,
|
|
97
|
-
});
|
|
98
56
|
this.application
|
|
99
57
|
.bind({ key: StaticAssetBindingKeys.policyService({ name: key }) })
|
|
100
|
-
.toValue(policyService);
|
|
58
|
+
.toValue(services.policyService);
|
|
101
59
|
this.application
|
|
102
60
|
.bind({ key: StaticAssetBindingKeys.commitService({ name: key }) })
|
|
103
|
-
.toValue(commitService);
|
|
104
|
-
return
|
|
61
|
+
.toValue(services.commitService);
|
|
62
|
+
return services;
|
|
105
63
|
}
|
|
106
64
|
};
|
|
107
65
|
StaticAssetComponent = StaticAssetComponent_1 = __decorate([
|