@basaltkit/files 1.1.0 → 1.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.
- package/README.md +58 -11
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -81,17 +81,30 @@ await files.markScanned(id, { clean: true }, tenantId) // emits file:scanned
|
|
|
81
81
|
|
|
82
82
|
## API reference
|
|
83
83
|
|
|
84
|
-
###
|
|
84
|
+
### Options reference
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
|
89
|
-
|
|
90
|
-
| `
|
|
91
|
-
| `
|
|
92
|
-
| `
|
|
86
|
+
`filesPlugin(options)` — registers a `Files` singleton under the `FILES` token:
|
|
87
|
+
|
|
88
|
+
| Option | Type | Default | Purpose |
|
|
89
|
+
|---|---|---|---|
|
|
90
|
+
| `disk` | `Disk \| string` | — (required) | A `Disk` instance, or the name of a disk declared in `storagePlugin`. A string is resolved from the `STORAGE` token at first use. |
|
|
91
|
+
| `validate` | `FileValidation` | `{ maxSize: DEFAULT_MAX_FILE_SIZE }` | Size limit and content-type allowlist. See below — the size cap applies **even if you pass nothing**. |
|
|
92
|
+
| `maxTotalBytes` | `number` | — (no quota) | Built-in per-tenant quota: rejects an upload when the tenant's stored bytes plus this file would exceed it. Costs one `store.totalSize()` read per upload. |
|
|
93
|
+
| `checkQuota` | `(tenantId, size) => Promise<void> \| void` | — | Custom quota check, run after the built-in one. Throw to reject — this is where you wire `@basaltkit/subscriptions` plan limits. |
|
|
94
|
+
| `store` | `FileStore` | `MemoryFileStore` | Where file metadata lives. In-memory means records vanish on restart while the bytes stay in storage — implement `FileStore` over your database in production. |
|
|
95
|
+
|
|
96
|
+
`FileValidation`:
|
|
97
|
+
|
|
98
|
+
| Option | Type | Default | Purpose |
|
|
99
|
+
|---|---|---|---|
|
|
100
|
+
| `maxSize` | `number` | `DEFAULT_MAX_FILE_SIZE` = **25 MiB** (`26214400`) | Per-file byte cap. Secure by default: uploads are capped even when you configure nothing. Raise it, or pass `Infinity` to disable. |
|
|
101
|
+
| `allowedTypes` | `string[]` | — (anything) | Content-type allowlist. Supports trailing wildcards: `'image/*'` matches `image/png`. |
|
|
93
102
|
|
|
94
|
-
|
|
103
|
+
`DEFAULT_MAX_FILE_SIZE` is exported, so you can express a limit relative to it.
|
|
104
|
+
|
|
105
|
+
> The cap applies to the buffer you hand to `upload()`. Your HTTP adapter's own
|
|
106
|
+
> body limit still applies first, and `@basaltkit/storage` itself caps nothing
|
|
107
|
+
> unless you pass `maxBytes` per `put()`.
|
|
95
108
|
|
|
96
109
|
### `class Files`
|
|
97
110
|
|
|
@@ -99,14 +112,46 @@ Registers the `FILES` token.
|
|
|
99
112
|
|---|---|
|
|
100
113
|
| `upload(content, input)` | Validates, enforces quota, stores, records metadata, emits `file:uploaded`. |
|
|
101
114
|
| `download(id, tenantId?)` | `{ record, content }`. |
|
|
102
|
-
| `temporaryUrl(id, expiresIn, tenantId?)` | Signed URL. |
|
|
115
|
+
| `temporaryUrl(id, expiresIn, tenantId?, options?)` | Signed URL. Served `Content-Disposition: attachment` by default; pass `{ disposition: 'inline' }` only when top-level rendering is deliberate — an uploaded HTML/SVG file served inline is stored XSS on the storage origin. Embedded `<img>`/`<video>` uses render regardless. |
|
|
103
116
|
| `get(id, tenantId?)` · `list(tenantId?)` | Metadata. |
|
|
104
117
|
| `delete(id, tenantId?)` | Deletes bytes + metadata; emits `file:deleted`. |
|
|
105
118
|
| `markScanned(id, result, tenantId?)` | Marks as scanned; emits `file:scanned`. |
|
|
106
119
|
|
|
107
120
|
Without an explicit `tenantId`, it uses `ctx().tenant.id`; without a tenant, it throws `FileTenantRequiredError`. Storage access runs in the resolved tenant's context, so files stay isolated even from a background job.
|
|
108
121
|
|
|
109
|
-
|
|
122
|
+
### Failure modes
|
|
123
|
+
|
|
124
|
+
| Error | Code | HTTP | When |
|
|
125
|
+
|---|---|---|---|
|
|
126
|
+
| `FileTooLargeError` | `FILE_TOO_LARGE` | 413 | The buffer exceeds `validate.maxSize` — 25 MiB when you configured nothing. |
|
|
127
|
+
| `FileTypeNotAllowedError` | `FILE_TYPE_NOT_ALLOWED` | 415 | `contentType` doesn't match `validate.allowedTypes`. |
|
|
128
|
+
| `StorageQuotaExceededError` | `FILE_QUOTA_EXCEEDED` | 402 | The tenant's total stored bytes plus this upload would pass `maxTotalBytes`. |
|
|
129
|
+
| `FileNotFoundError` | `FILE_NOT_FOUND` | 404 | `download` / `temporaryUrl` / `markScanned` for an id absent from this tenant's metadata store. |
|
|
130
|
+
| `FileTenantRequiredError` | `FILE_TENANT_REQUIRED` | 400 | No `tenantId` argument and no `ctx().tenant` — every operation is tenant-scoped and fails closed rather than querying unscoped. |
|
|
131
|
+
|
|
132
|
+
All extend `BasaltError` and declare a `status`, so the adapters map them to the
|
|
133
|
+
HTTP code above with the real error `code` in the body. Errors thrown by the
|
|
134
|
+
underlying disk (`STORAGE_*`) do **not** — they surface as 500 `INTERNAL_ERROR`.
|
|
135
|
+
|
|
136
|
+
- **`FILE_NOT_FOUND` for a file that exists in the bucket** — the metadata
|
|
137
|
+
record is gone, not the bytes. `MemoryFileStore` loses everything on restart;
|
|
138
|
+
wire a durable `FileStore`.
|
|
139
|
+
- **`FILE_TOO_LARGE` at exactly 25 MiB** — that's the default, not your adapter.
|
|
140
|
+
Set `validate: { maxSize: … }`.
|
|
141
|
+
- **`FILE_TENANT_REQUIRED` inside a queue job** — jobs don't inherit the request
|
|
142
|
+
context. Pass `tenantId` explicitly, or run the job body inside
|
|
143
|
+
`tenancy.run(tenantId, …)`.
|
|
144
|
+
|
|
145
|
+
### Hooks & events
|
|
146
|
+
|
|
147
|
+
| Hook | Payload | When |
|
|
148
|
+
|---|---|---|
|
|
149
|
+
| `file:uploaded` | `{ file: FileRecord }` | After the bytes are written and the metadata recorded. |
|
|
150
|
+
| `file:deleted` | `{ tenantId: string; id: string }` | After the bytes and the record are removed. |
|
|
151
|
+
| `file:scanned` | `{ file: FileRecord }` | After `markScanned()` records an out-of-band scan result. |
|
|
152
|
+
|
|
153
|
+
They are declared on `BasaltHooks`, so `hooks.on('file:uploaded', …)` is fully
|
|
154
|
+
typed.
|
|
110
155
|
|
|
111
156
|
## How it connects to other modules
|
|
112
157
|
|
|
@@ -114,3 +159,5 @@ Errors: `FileTooLargeError` (413), `FileTypeNotAllowedError` (415), `StorageQuot
|
|
|
114
159
|
- **`@basaltkit/subscriptions`** — hook `checkQuota` into `features(tenant).consume(...)` for plan-based quotas.
|
|
115
160
|
- **`@basaltkit/queue`** — processes `file:uploaded` outside the request (antivirus, thumbnails).
|
|
116
161
|
- **`@basaltkit/tenancy`** — supplies the tenant from the context.
|
|
162
|
+
|
|
163
|
+
Guides: [Files & uploads](/guide/files) · [Storage](/guide/storage) · [Queues](/guide/queues).
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/files",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=22.5.0"
|
|
6
|
+
},
|
|
4
7
|
"description": "File uploads for Basalt: validation (type/size), per-tenant storage quota, metadata records, signed URLs, and hooks (antivirus/thumbnails) over @basaltkit/storage.",
|
|
5
8
|
"license": "MIT",
|
|
6
9
|
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
7
11
|
"exports": {
|
|
8
12
|
".": {
|
|
9
13
|
"types": "./dist/index.d.ts",
|
|
@@ -14,9 +18,9 @@
|
|
|
14
18
|
"dist"
|
|
15
19
|
],
|
|
16
20
|
"dependencies": {
|
|
17
|
-
"@basaltkit/core": "^1.3.
|
|
18
|
-
"@basaltkit/http": "^1.
|
|
19
|
-
"@basaltkit/storage": "^1.3.
|
|
21
|
+
"@basaltkit/core": "^1.3.1",
|
|
22
|
+
"@basaltkit/http": "^1.14.0",
|
|
23
|
+
"@basaltkit/storage": "^1.3.1"
|
|
20
24
|
},
|
|
21
25
|
"peerDependencies": {
|
|
22
26
|
"zod": "^3.24.0 || ^4.0.0"
|
|
@@ -26,7 +30,7 @@
|
|
|
26
30
|
"typescript": "^7.0.2",
|
|
27
31
|
"vitest": "^4.1.11",
|
|
28
32
|
"zod": "^3.24.0 || ^4.0.0",
|
|
29
|
-
"@basaltkit/fastify": "^1.
|
|
33
|
+
"@basaltkit/fastify": "^1.8.1",
|
|
30
34
|
"@basaltkit/tsconfig": "^0.24.0"
|
|
31
35
|
},
|
|
32
36
|
"publishConfig": {
|