nuxt-filer 0.0.12 → 0.0.14
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 +34 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +4 -0
- package/dist/runtime/server/providers/s3.d.ts +40 -0
- package/dist/runtime/server/providers/s3.js +216 -0
- package/package.json +6 -1
- package/src/runtime/server/providers/s3.ts +318 -0
package/README.md
CHANGED
|
@@ -193,9 +193,42 @@ filer: {
|
|
|
193
193
|
|
|
194
194
|
`@nuxt/image` and `ipx` are declared as optional peer dependencies — they only need to be installed if you want to use this integration.
|
|
195
195
|
|
|
196
|
+
## S3 storage
|
|
197
|
+
|
|
198
|
+
For durable object storage (AWS S3, Cloudflare R2, MinIO, …) use the built-in
|
|
199
|
+
`createS3Provider`. It stores each file as a data object plus a JSON metadata
|
|
200
|
+
sidecar, and — unlike unstorage's generic `s3` driver — `list()`/`findByMeta()`
|
|
201
|
+
use real S3 prefix listing with continuation pagination, so a group stays
|
|
202
|
+
correctly scoped even inside a large or shared bucket.
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
// nuxt.config.ts
|
|
206
|
+
export default defineNuxtConfig({
|
|
207
|
+
modules: ['nuxt-filer'],
|
|
208
|
+
filer: { provider: 'custom' },
|
|
209
|
+
})
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
// server/plugins/file-provider.ts
|
|
214
|
+
export default defineNitroPlugin(() => {
|
|
215
|
+
const { s3 } = useRuntimeConfig()
|
|
216
|
+
setFileStorageProvider(createS3Provider({
|
|
217
|
+
accessKeyId: s3.accessKeyId,
|
|
218
|
+
secretAccessKey: s3.secretAccessKey,
|
|
219
|
+
endpoint: s3.endpoint, // e.g. https://<acct>.r2.cloudflarestorage.com
|
|
220
|
+
region: s3.region, // R2: 'auto'
|
|
221
|
+
bucket: s3.bucket,
|
|
222
|
+
// prefix: 'media/', // optional: namespace within a shared bucket
|
|
223
|
+
}))
|
|
224
|
+
})
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
> `createS3Provider` requires the optional [`aws4fetch`](https://github.com/mhart/aws4fetch) peer dependency (`npm i aws4fetch`). Pass a custom `client` to use a different transport or to unit-test without network.
|
|
228
|
+
|
|
196
229
|
## Custom Provider
|
|
197
230
|
|
|
198
|
-
For advanced use cases (database-backed metadata,
|
|
231
|
+
For advanced use cases (database-backed metadata, external file sync), implement the `FileStorageProvider` interface and register it in a Nitro plugin:
|
|
199
232
|
|
|
200
233
|
```ts
|
|
201
234
|
// nuxt.config.ts
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -37,6 +37,10 @@ const module = defineNuxtModule({
|
|
|
37
37
|
{
|
|
38
38
|
name: "createUnstorageProvider",
|
|
39
39
|
from: resolver.resolve("./runtime/server/providers/unstorage")
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "createS3Provider",
|
|
43
|
+
from: resolver.resolve("./runtime/server/providers/s3")
|
|
40
44
|
}
|
|
41
45
|
]);
|
|
42
46
|
const typesSpecifier = "nuxt-filer/runtime/types";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { FileStorageProvider } from '../../../runtime/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Minimal S3 object-store surface the provider needs. Abstracted so the
|
|
4
|
+
* provider can be unit-tested with an in-memory fake (pass `client`), and so
|
|
5
|
+
* the real implementation (aws4fetch) stays isolated.
|
|
6
|
+
*
|
|
7
|
+
* `listKeys` MUST list with a server-side prefix and paginate through every
|
|
8
|
+
* page — that's the whole point of this provider over unstorage's generic s3
|
|
9
|
+
* driver, which lists the bucket root and caps at 1000 keys.
|
|
10
|
+
*/
|
|
11
|
+
export interface S3Client {
|
|
12
|
+
put(key: string, body: Buffer | Uint8Array, contentType?: string): Promise<void>;
|
|
13
|
+
get(key: string): Promise<Buffer | null>;
|
|
14
|
+
head(key: string): Promise<boolean>;
|
|
15
|
+
delete(key: string): Promise<void>;
|
|
16
|
+
listKeys(prefix: string): AsyncGenerator<string, void, unknown>;
|
|
17
|
+
}
|
|
18
|
+
export interface S3ProviderOptions {
|
|
19
|
+
accessKeyId?: string;
|
|
20
|
+
secretAccessKey?: string;
|
|
21
|
+
/** S3 API endpoint, e.g. https://<acct>.r2.cloudflarestorage.com */
|
|
22
|
+
endpoint?: string;
|
|
23
|
+
/** SigV4 region. R2 uses 'auto' (the default). */
|
|
24
|
+
region?: string;
|
|
25
|
+
bucket?: string;
|
|
26
|
+
/** Optional key prefix to namespace files within a shared bucket. */
|
|
27
|
+
prefix?: string;
|
|
28
|
+
/** Inject a custom S3 client (testing or alternative transport). */
|
|
29
|
+
client?: S3Client;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* S3-backed {@link FileStorageProvider}. Binary data and a JSON metadata
|
|
33
|
+
* sidecar are stored as separate objects per file, mirroring the built-in
|
|
34
|
+
* unstorage provider's semantics — but `list`/`findByMeta` use real S3 prefix
|
|
35
|
+
* listing with continuation pagination, so a group is correctly scoped even
|
|
36
|
+
* inside a large shared bucket.
|
|
37
|
+
*
|
|
38
|
+
* Key layout: `${prefix}${groupId}/data/${id}` and `${prefix}${groupId}/meta/${id}`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createS3Provider(options: S3ProviderOptions): FileStorageProvider;
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
export function createS3Provider(options) {
|
|
3
|
+
const prefix = options.prefix ? options.prefix.replace(/\/+$/, "") + "/" : "";
|
|
4
|
+
let clientPromise;
|
|
5
|
+
const getClient = () => clientPromise ??= options.client ? Promise.resolve(options.client) : createAwsS3Client(options);
|
|
6
|
+
const seg = (value) => value.replace(/^\/+|\/+$/g, "");
|
|
7
|
+
const dataKey = (groupId, id) => `${prefix}${seg(groupId)}/data/${seg(id)}`;
|
|
8
|
+
const metaKey = (groupId, id) => `${prefix}${seg(groupId)}/meta/${seg(id)}`;
|
|
9
|
+
const metaPrefix = (groupId) => `${prefix}${seg(groupId)}/meta/`;
|
|
10
|
+
const groupIdFromMetaKey = (key) => {
|
|
11
|
+
const rel = key.slice(prefix.length);
|
|
12
|
+
return rel.slice(0, rel.lastIndexOf("/meta/"));
|
|
13
|
+
};
|
|
14
|
+
const idFromKey = (key) => key.slice(key.lastIndexOf("/") + 1);
|
|
15
|
+
const readMeta = async (client, key) => {
|
|
16
|
+
const raw = await client.get(key);
|
|
17
|
+
if (!raw) return null;
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(raw.toString("utf8"));
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const toStoredFile = (id, groupId, meta, data) => ({
|
|
25
|
+
id,
|
|
26
|
+
groupId,
|
|
27
|
+
data: data ?? void 0,
|
|
28
|
+
meta: stripInternal(meta),
|
|
29
|
+
createdAt: meta?._createdAt ? new Date(meta._createdAt) : void 0,
|
|
30
|
+
updatedAt: meta?._updatedAt ? new Date(meta._updatedAt) : void 0
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
async create(groupId, data, meta) {
|
|
34
|
+
const client = await getClient();
|
|
35
|
+
const id = randomUUID();
|
|
36
|
+
await client.put(dataKey(groupId, id), data, meta?.mime);
|
|
37
|
+
if (meta) {
|
|
38
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
39
|
+
await client.put(
|
|
40
|
+
metaKey(groupId, id),
|
|
41
|
+
Buffer.from(JSON.stringify({ ...meta, _createdAt: now, _updatedAt: now })),
|
|
42
|
+
"application/json"
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return { id };
|
|
46
|
+
},
|
|
47
|
+
async get(groupId, id) {
|
|
48
|
+
const client = await getClient();
|
|
49
|
+
const [data, meta] = await Promise.all([
|
|
50
|
+
client.get(dataKey(groupId, id)),
|
|
51
|
+
readMeta(client, metaKey(groupId, id))
|
|
52
|
+
]);
|
|
53
|
+
if (!data && !meta) return null;
|
|
54
|
+
return toStoredFile(id, groupId, meta, data ?? void 0);
|
|
55
|
+
},
|
|
56
|
+
async getData(groupId, id) {
|
|
57
|
+
return (await getClient()).get(dataKey(groupId, id));
|
|
58
|
+
},
|
|
59
|
+
async getMeta(id) {
|
|
60
|
+
const client = await getClient();
|
|
61
|
+
for await (const key of client.listKeys(prefix)) {
|
|
62
|
+
if (key.endsWith(`/meta/${id}`)) {
|
|
63
|
+
return stripInternal(await readMeta(client, key));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
},
|
|
68
|
+
async list(groupId) {
|
|
69
|
+
const client = await getClient();
|
|
70
|
+
const files = [];
|
|
71
|
+
for await (const key of client.listKeys(metaPrefix(groupId))) {
|
|
72
|
+
const meta = await readMeta(client, key);
|
|
73
|
+
if (!meta) continue;
|
|
74
|
+
files.push(toStoredFile(idFromKey(key), groupId, meta));
|
|
75
|
+
}
|
|
76
|
+
return files;
|
|
77
|
+
},
|
|
78
|
+
async update(id, meta) {
|
|
79
|
+
const client = await getClient();
|
|
80
|
+
for await (const key of client.listKeys(prefix)) {
|
|
81
|
+
if (!key.endsWith(`/meta/${id}`)) continue;
|
|
82
|
+
const existing = await readMeta(client, key) ?? {};
|
|
83
|
+
await client.put(
|
|
84
|
+
key,
|
|
85
|
+
Buffer.from(
|
|
86
|
+
JSON.stringify({ ...existing, ...meta, _updatedAt: (/* @__PURE__ */ new Date()).toISOString() })
|
|
87
|
+
),
|
|
88
|
+
"application/json"
|
|
89
|
+
);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
throw new Error(`File metadata not found: ${id}`);
|
|
93
|
+
},
|
|
94
|
+
async remove(groupId, id) {
|
|
95
|
+
const client = await getClient();
|
|
96
|
+
await Promise.all([
|
|
97
|
+
client.delete(dataKey(groupId, id)),
|
|
98
|
+
client.delete(metaKey(groupId, id))
|
|
99
|
+
]);
|
|
100
|
+
},
|
|
101
|
+
async clear(groupId) {
|
|
102
|
+
const client = await getClient();
|
|
103
|
+
for await (const key of client.listKeys(`${prefix}${seg(groupId)}/`)) {
|
|
104
|
+
await client.delete(key);
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
async has(groupId, id) {
|
|
108
|
+
return (await getClient()).head(dataKey(groupId, id));
|
|
109
|
+
},
|
|
110
|
+
async findByMeta(filter) {
|
|
111
|
+
const client = await getClient();
|
|
112
|
+
const scanPrefix = filter.groupId ? metaPrefix(filter.groupId) : prefix;
|
|
113
|
+
for await (const key of client.listKeys(scanPrefix)) {
|
|
114
|
+
if (!key.includes("/meta/")) continue;
|
|
115
|
+
const meta = await readMeta(client, key);
|
|
116
|
+
if (!meta) continue;
|
|
117
|
+
if (meta[filter.key] === filter.value) {
|
|
118
|
+
return toStoredFile(idFromKey(key), groupIdFromMetaKey(key), meta);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function stripInternal(meta) {
|
|
126
|
+
if (!meta) return { name: "", mime: "", type: "", version: 0 };
|
|
127
|
+
const { _createdAt, _updatedAt, ...rest } = meta;
|
|
128
|
+
return rest;
|
|
129
|
+
}
|
|
130
|
+
async function createAwsS3Client(options) {
|
|
131
|
+
for (const key of ["accessKeyId", "secretAccessKey", "endpoint", "bucket"]) {
|
|
132
|
+
if (!options[key]) {
|
|
133
|
+
throw new Error(`[nuxt-filer] createS3Provider: missing required option "${key}"`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
let AwsClient;
|
|
137
|
+
try {
|
|
138
|
+
({ AwsClient } = await import("aws4fetch"));
|
|
139
|
+
} catch {
|
|
140
|
+
throw new Error(
|
|
141
|
+
'[nuxt-filer] createS3Provider needs the optional "aws4fetch" dependency. Install it: npm i aws4fetch'
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const aws = new AwsClient({
|
|
145
|
+
service: "s3",
|
|
146
|
+
accessKeyId: options.accessKeyId,
|
|
147
|
+
secretAccessKey: options.secretAccessKey,
|
|
148
|
+
region: options.region || "auto"
|
|
149
|
+
});
|
|
150
|
+
const base = `${options.endpoint.replace(/\/+$/, "")}/${options.bucket}`;
|
|
151
|
+
const objectUrl = (key) => `${base}/${key.split("/").map(encodeURIComponent).join("/")}`;
|
|
152
|
+
const signedFetch = (url, init) => aws.sign(url, init).then((req) => fetch(req));
|
|
153
|
+
return {
|
|
154
|
+
async put(key, body, contentType) {
|
|
155
|
+
const res = await signedFetch(objectUrl(key), {
|
|
156
|
+
method: "PUT",
|
|
157
|
+
body,
|
|
158
|
+
headers: contentType ? { "content-type": contentType } : void 0
|
|
159
|
+
});
|
|
160
|
+
if (!res.ok) {
|
|
161
|
+
throw new Error(`[nuxt-filer] S3 PUT ${key}: ${res.status} ${res.statusText}`);
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
async get(key) {
|
|
165
|
+
const res = await signedFetch(objectUrl(key));
|
|
166
|
+
if (res.status === 404) return null;
|
|
167
|
+
if (!res.ok) {
|
|
168
|
+
throw new Error(`[nuxt-filer] S3 GET ${key}: ${res.status} ${res.statusText}`);
|
|
169
|
+
}
|
|
170
|
+
return Buffer.from(await res.arrayBuffer());
|
|
171
|
+
},
|
|
172
|
+
async head(key) {
|
|
173
|
+
const res = await signedFetch(objectUrl(key), { method: "HEAD" });
|
|
174
|
+
if (res.status === 404) return false;
|
|
175
|
+
if (!res.ok) {
|
|
176
|
+
throw new Error(`[nuxt-filer] S3 HEAD ${key}: ${res.status} ${res.statusText}`);
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
},
|
|
180
|
+
async delete(key) {
|
|
181
|
+
const res = await signedFetch(objectUrl(key), { method: "DELETE" });
|
|
182
|
+
if (!res.ok && res.status !== 404) {
|
|
183
|
+
throw new Error(`[nuxt-filer] S3 DELETE ${key}: ${res.status} ${res.statusText}`);
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
async *listKeys(keyPrefix) {
|
|
187
|
+
let token;
|
|
188
|
+
do {
|
|
189
|
+
const url = new URL(base);
|
|
190
|
+
url.searchParams.set("list-type", "2");
|
|
191
|
+
if (keyPrefix) url.searchParams.set("prefix", keyPrefix);
|
|
192
|
+
if (token) url.searchParams.set("continuation-token", token);
|
|
193
|
+
const res = await signedFetch(url.toString());
|
|
194
|
+
if (!res.ok) {
|
|
195
|
+
throw new Error(`[nuxt-filer] S3 LIST ${keyPrefix}: ${res.status} ${res.statusText}`);
|
|
196
|
+
}
|
|
197
|
+
const xml = await res.text();
|
|
198
|
+
for (const key of parseListKeys(xml)) yield key;
|
|
199
|
+
token = matchTag(xml, "IsTruncated") === "true" ? matchTag(xml, "NextContinuationToken") : void 0;
|
|
200
|
+
} while (token);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function parseListKeys(xml) {
|
|
205
|
+
const keys = [];
|
|
206
|
+
const re = /<Key>([\s\S]*?)<\/Key>/g;
|
|
207
|
+
let match;
|
|
208
|
+
while (match = re.exec(xml)) keys.push(decodeXml(match[1]));
|
|
209
|
+
return keys;
|
|
210
|
+
}
|
|
211
|
+
function matchTag(xml, tag) {
|
|
212
|
+
return xml.match(new RegExp(`<${tag}>([\\s\\S]*?)</${tag}>`))?.[1];
|
|
213
|
+
}
|
|
214
|
+
function decodeXml(value) {
|
|
215
|
+
return value.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&");
|
|
216
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-filer",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "File storage module for Nuxt",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"@nuxt/devtools": "^3.2.4",
|
|
46
46
|
"@nuxt/eslint-config": "^1.15.2",
|
|
47
47
|
"@nuxt/image": "^2.0.0",
|
|
48
|
+
"aws4fetch": "^1.0.20",
|
|
48
49
|
"@nuxt/module-builder": "^1.0.2",
|
|
49
50
|
"@nuxt/schema": "^4.4.2",
|
|
50
51
|
"@nuxt/test-utils": "^4.0.0",
|
|
@@ -60,6 +61,7 @@
|
|
|
60
61
|
},
|
|
61
62
|
"peerDependencies": {
|
|
62
63
|
"@nuxt/image": "^2.0.0",
|
|
64
|
+
"aws4fetch": "^1.0.20",
|
|
63
65
|
"ipx": "^3.0.0",
|
|
64
66
|
"sharp": "^0.33.0 || ^0.34.0"
|
|
65
67
|
},
|
|
@@ -67,6 +69,9 @@
|
|
|
67
69
|
"@nuxt/image": {
|
|
68
70
|
"optional": true
|
|
69
71
|
},
|
|
72
|
+
"aws4fetch": {
|
|
73
|
+
"optional": true
|
|
74
|
+
},
|
|
70
75
|
"ipx": {
|
|
71
76
|
"optional": true
|
|
72
77
|
},
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import type {
|
|
3
|
+
FileStorageProvider,
|
|
4
|
+
FileMeta,
|
|
5
|
+
StoredFile,
|
|
6
|
+
} from '../../../runtime/types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Minimal S3 object-store surface the provider needs. Abstracted so the
|
|
10
|
+
* provider can be unit-tested with an in-memory fake (pass `client`), and so
|
|
11
|
+
* the real implementation (aws4fetch) stays isolated.
|
|
12
|
+
*
|
|
13
|
+
* `listKeys` MUST list with a server-side prefix and paginate through every
|
|
14
|
+
* page — that's the whole point of this provider over unstorage's generic s3
|
|
15
|
+
* driver, which lists the bucket root and caps at 1000 keys.
|
|
16
|
+
*/
|
|
17
|
+
export interface S3Client {
|
|
18
|
+
put(key: string, body: Buffer | Uint8Array, contentType?: string): Promise<void>;
|
|
19
|
+
get(key: string): Promise<Buffer | null>;
|
|
20
|
+
head(key: string): Promise<boolean>;
|
|
21
|
+
delete(key: string): Promise<void>;
|
|
22
|
+
listKeys(prefix: string): AsyncGenerator<string, void, unknown>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface S3ProviderOptions {
|
|
26
|
+
accessKeyId?: string;
|
|
27
|
+
secretAccessKey?: string;
|
|
28
|
+
/** S3 API endpoint, e.g. https://<acct>.r2.cloudflarestorage.com */
|
|
29
|
+
endpoint?: string;
|
|
30
|
+
/** SigV4 region. R2 uses 'auto' (the default). */
|
|
31
|
+
region?: string;
|
|
32
|
+
bucket?: string;
|
|
33
|
+
/** Optional key prefix to namespace files within a shared bucket. */
|
|
34
|
+
prefix?: string;
|
|
35
|
+
/** Inject a custom S3 client (testing or alternative transport). */
|
|
36
|
+
client?: S3Client;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type InternalMeta = FileMeta & { _createdAt?: string; _updatedAt?: string };
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* S3-backed {@link FileStorageProvider}. Binary data and a JSON metadata
|
|
43
|
+
* sidecar are stored as separate objects per file, mirroring the built-in
|
|
44
|
+
* unstorage provider's semantics — but `list`/`findByMeta` use real S3 prefix
|
|
45
|
+
* listing with continuation pagination, so a group is correctly scoped even
|
|
46
|
+
* inside a large shared bucket.
|
|
47
|
+
*
|
|
48
|
+
* Key layout: `${prefix}${groupId}/data/${id}` and `${prefix}${groupId}/meta/${id}`.
|
|
49
|
+
*/
|
|
50
|
+
export function createS3Provider(options: S3ProviderOptions): FileStorageProvider {
|
|
51
|
+
const prefix = options.prefix ? options.prefix.replace(/\/+$/, '') + '/' : '';
|
|
52
|
+
|
|
53
|
+
let clientPromise: Promise<S3Client> | undefined;
|
|
54
|
+
const getClient = () =>
|
|
55
|
+
(clientPromise ??= options.client
|
|
56
|
+
? Promise.resolve(options.client)
|
|
57
|
+
: createAwsS3Client(options));
|
|
58
|
+
|
|
59
|
+
// Strip leading/trailing slashes so callers can pass `studio` or `/studio`
|
|
60
|
+
// interchangeably (the IPX integration hands group ids through with a leading
|
|
61
|
+
// slash). Without this, keys like `/studio/data/x` miss the stored object.
|
|
62
|
+
const seg = (value: string) => value.replace(/^\/+|\/+$/g, '');
|
|
63
|
+
const dataKey = (groupId: string, id: string) => `${prefix}${seg(groupId)}/data/${seg(id)}`;
|
|
64
|
+
const metaKey = (groupId: string, id: string) => `${prefix}${seg(groupId)}/meta/${seg(id)}`;
|
|
65
|
+
const metaPrefix = (groupId: string) => `${prefix}${seg(groupId)}/meta/`;
|
|
66
|
+
|
|
67
|
+
/** `${prefix}${groupId}/meta/${id}` → groupId (groups may contain '/'). */
|
|
68
|
+
const groupIdFromMetaKey = (key: string) => {
|
|
69
|
+
const rel = key.slice(prefix.length);
|
|
70
|
+
return rel.slice(0, rel.lastIndexOf('/meta/'));
|
|
71
|
+
};
|
|
72
|
+
const idFromKey = (key: string) => key.slice(key.lastIndexOf('/') + 1);
|
|
73
|
+
|
|
74
|
+
const readMeta = async (client: S3Client, key: string): Promise<InternalMeta | null> => {
|
|
75
|
+
const raw = await client.get(key);
|
|
76
|
+
if (!raw) return null;
|
|
77
|
+
try {
|
|
78
|
+
return JSON.parse(raw.toString('utf8')) as InternalMeta;
|
|
79
|
+
} catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const toStoredFile = (
|
|
85
|
+
id: string,
|
|
86
|
+
groupId: string,
|
|
87
|
+
meta: InternalMeta | null,
|
|
88
|
+
data?: Buffer,
|
|
89
|
+
): StoredFile => ({
|
|
90
|
+
id,
|
|
91
|
+
groupId,
|
|
92
|
+
data: data ?? undefined,
|
|
93
|
+
meta: stripInternal(meta),
|
|
94
|
+
createdAt: meta?._createdAt ? new Date(meta._createdAt) : undefined,
|
|
95
|
+
updatedAt: meta?._updatedAt ? new Date(meta._updatedAt) : undefined,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
async create(groupId, data, meta) {
|
|
100
|
+
const client = await getClient();
|
|
101
|
+
const id = randomUUID();
|
|
102
|
+
|
|
103
|
+
await client.put(dataKey(groupId, id), data, meta?.mime);
|
|
104
|
+
|
|
105
|
+
if (meta) {
|
|
106
|
+
const now = new Date().toISOString();
|
|
107
|
+
await client.put(
|
|
108
|
+
metaKey(groupId, id),
|
|
109
|
+
Buffer.from(JSON.stringify({ ...meta, _createdAt: now, _updatedAt: now })),
|
|
110
|
+
'application/json',
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return { id };
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
async get(groupId, id) {
|
|
118
|
+
const client = await getClient();
|
|
119
|
+
const [data, meta] = await Promise.all([
|
|
120
|
+
client.get(dataKey(groupId, id)),
|
|
121
|
+
readMeta(client, metaKey(groupId, id)),
|
|
122
|
+
]);
|
|
123
|
+
if (!data && !meta) return null;
|
|
124
|
+
return toStoredFile(id, groupId, meta, data ?? undefined);
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
async getData(groupId, id) {
|
|
128
|
+
return (await getClient()).get(dataKey(groupId, id));
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
async getMeta(id) {
|
|
132
|
+
// No groupId: scan meta objects (bounded by `prefix` when set). The
|
|
133
|
+
// serving path uses get/getData (which carry a groupId), so this is the
|
|
134
|
+
// only non-group-scoped lookup.
|
|
135
|
+
const client = await getClient();
|
|
136
|
+
for await (const key of client.listKeys(prefix)) {
|
|
137
|
+
if (key.endsWith(`/meta/${id}`)) {
|
|
138
|
+
return stripInternal(await readMeta(client, key));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
async list(groupId) {
|
|
145
|
+
const client = await getClient();
|
|
146
|
+
const files: StoredFile[] = [];
|
|
147
|
+
for await (const key of client.listKeys(metaPrefix(groupId))) {
|
|
148
|
+
const meta = await readMeta(client, key);
|
|
149
|
+
if (!meta) continue;
|
|
150
|
+
files.push(toStoredFile(idFromKey(key), groupId, meta));
|
|
151
|
+
}
|
|
152
|
+
return files;
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
async update(id, meta) {
|
|
156
|
+
const client = await getClient();
|
|
157
|
+
for await (const key of client.listKeys(prefix)) {
|
|
158
|
+
if (!key.endsWith(`/meta/${id}`)) continue;
|
|
159
|
+
const existing = (await readMeta(client, key)) ?? ({} as InternalMeta);
|
|
160
|
+
await client.put(
|
|
161
|
+
key,
|
|
162
|
+
Buffer.from(
|
|
163
|
+
JSON.stringify({ ...existing, ...meta, _updatedAt: new Date().toISOString() }),
|
|
164
|
+
),
|
|
165
|
+
'application/json',
|
|
166
|
+
);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
throw new Error(`File metadata not found: ${id}`);
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
async remove(groupId, id) {
|
|
173
|
+
const client = await getClient();
|
|
174
|
+
await Promise.all([
|
|
175
|
+
client.delete(dataKey(groupId, id)),
|
|
176
|
+
client.delete(metaKey(groupId, id)),
|
|
177
|
+
]);
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
async clear(groupId) {
|
|
181
|
+
const client = await getClient();
|
|
182
|
+
for await (const key of client.listKeys(`${prefix}${seg(groupId)}/`)) {
|
|
183
|
+
await client.delete(key);
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
async has(groupId, id) {
|
|
188
|
+
return (await getClient()).head(dataKey(groupId, id));
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
async findByMeta(filter) {
|
|
192
|
+
const client = await getClient();
|
|
193
|
+
const scanPrefix = filter.groupId ? metaPrefix(filter.groupId) : prefix;
|
|
194
|
+
for await (const key of client.listKeys(scanPrefix)) {
|
|
195
|
+
if (!key.includes('/meta/')) continue;
|
|
196
|
+
const meta = await readMeta(client, key);
|
|
197
|
+
if (!meta) continue;
|
|
198
|
+
if (meta[filter.key] === filter.value) {
|
|
199
|
+
return toStoredFile(idFromKey(key), groupIdFromMetaKey(key), meta);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return null;
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function stripInternal(meta: InternalMeta | null): FileMeta {
|
|
208
|
+
if (!meta) return { name: '', mime: '', type: '', version: 0 };
|
|
209
|
+
const { _createdAt, _updatedAt, ...rest } = meta;
|
|
210
|
+
return rest as FileMeta;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Default {@link S3Client} backed by aws4fetch (SigV4 over fetch). */
|
|
214
|
+
async function createAwsS3Client(options: S3ProviderOptions): Promise<S3Client> {
|
|
215
|
+
for (const key of ['accessKeyId', 'secretAccessKey', 'endpoint', 'bucket'] as const) {
|
|
216
|
+
if (!options[key]) {
|
|
217
|
+
throw new Error(`[nuxt-filer] createS3Provider: missing required option "${key}"`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
let AwsClient: typeof import('aws4fetch').AwsClient;
|
|
222
|
+
try {
|
|
223
|
+
({ AwsClient } = await import('aws4fetch'));
|
|
224
|
+
} catch {
|
|
225
|
+
throw new Error(
|
|
226
|
+
'[nuxt-filer] createS3Provider needs the optional "aws4fetch" dependency. Install it: npm i aws4fetch',
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const aws = new AwsClient({
|
|
231
|
+
service: 's3',
|
|
232
|
+
accessKeyId: options.accessKeyId!,
|
|
233
|
+
secretAccessKey: options.secretAccessKey!,
|
|
234
|
+
region: options.region || 'auto',
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const base = `${options.endpoint!.replace(/\/+$/, '')}/${options.bucket}`;
|
|
238
|
+
const objectUrl = (key: string) =>
|
|
239
|
+
`${base}/${key.split('/').map(encodeURIComponent).join('/')}`;
|
|
240
|
+
|
|
241
|
+
const signedFetch = (url: string, init?: RequestInit) =>
|
|
242
|
+
aws.sign(url, init).then((req) => fetch(req));
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
async put(key, body, contentType) {
|
|
246
|
+
const res = await signedFetch(objectUrl(key), {
|
|
247
|
+
method: 'PUT',
|
|
248
|
+
body: body as BodyInit,
|
|
249
|
+
headers: contentType ? { 'content-type': contentType } : undefined,
|
|
250
|
+
});
|
|
251
|
+
if (!res.ok) {
|
|
252
|
+
throw new Error(`[nuxt-filer] S3 PUT ${key}: ${res.status} ${res.statusText}`);
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
async get(key) {
|
|
256
|
+
const res = await signedFetch(objectUrl(key));
|
|
257
|
+
if (res.status === 404) return null;
|
|
258
|
+
if (!res.ok) {
|
|
259
|
+
throw new Error(`[nuxt-filer] S3 GET ${key}: ${res.status} ${res.statusText}`);
|
|
260
|
+
}
|
|
261
|
+
return Buffer.from(await res.arrayBuffer());
|
|
262
|
+
},
|
|
263
|
+
async head(key) {
|
|
264
|
+
const res = await signedFetch(objectUrl(key), { method: 'HEAD' });
|
|
265
|
+
if (res.status === 404) return false;
|
|
266
|
+
if (!res.ok) {
|
|
267
|
+
throw new Error(`[nuxt-filer] S3 HEAD ${key}: ${res.status} ${res.statusText}`);
|
|
268
|
+
}
|
|
269
|
+
return true;
|
|
270
|
+
},
|
|
271
|
+
async delete(key) {
|
|
272
|
+
const res = await signedFetch(objectUrl(key), { method: 'DELETE' });
|
|
273
|
+
if (!res.ok && res.status !== 404) {
|
|
274
|
+
throw new Error(`[nuxt-filer] S3 DELETE ${key}: ${res.status} ${res.statusText}`);
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
async *listKeys(keyPrefix) {
|
|
278
|
+
let token: string | undefined;
|
|
279
|
+
do {
|
|
280
|
+
const url = new URL(base);
|
|
281
|
+
url.searchParams.set('list-type', '2');
|
|
282
|
+
if (keyPrefix) url.searchParams.set('prefix', keyPrefix);
|
|
283
|
+
if (token) url.searchParams.set('continuation-token', token);
|
|
284
|
+
|
|
285
|
+
const res = await signedFetch(url.toString());
|
|
286
|
+
if (!res.ok) {
|
|
287
|
+
throw new Error(`[nuxt-filer] S3 LIST ${keyPrefix}: ${res.status} ${res.statusText}`);
|
|
288
|
+
}
|
|
289
|
+
const xml = await res.text();
|
|
290
|
+
for (const key of parseListKeys(xml)) yield key;
|
|
291
|
+
|
|
292
|
+
token =
|
|
293
|
+
matchTag(xml, 'IsTruncated') === 'true' ? matchTag(xml, 'NextContinuationToken') : undefined;
|
|
294
|
+
} while (token);
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function parseListKeys(xml: string): string[] {
|
|
300
|
+
const keys: string[] = [];
|
|
301
|
+
const re = /<Key>([\s\S]*?)<\/Key>/g;
|
|
302
|
+
let match: RegExpExecArray | null;
|
|
303
|
+
while ((match = re.exec(xml))) keys.push(decodeXml(match[1]!));
|
|
304
|
+
return keys;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function matchTag(xml: string, tag: string): string | undefined {
|
|
308
|
+
return xml.match(new RegExp(`<${tag}>([\\s\\S]*?)</${tag}>`))?.[1];
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function decodeXml(value: string): string {
|
|
312
|
+
return value
|
|
313
|
+
.replace(/</g, '<')
|
|
314
|
+
.replace(/>/g, '>')
|
|
315
|
+
.replace(/"/g, '"')
|
|
316
|
+
.replace(/'/g, "'")
|
|
317
|
+
.replace(/&/g, '&');
|
|
318
|
+
}
|