@guren/server 2.6.0 → 2.7.0
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/{chunk-RGGEGXOE.js → chunk-AOPXIURJ.js} +1 -1
- package/dist/{chunk-HWEWL6TE.js → chunk-JTNRAWQR.js} +74 -8
- package/dist/index.js +11 -11
- package/dist/runtime/index.js +1 -1
- package/dist/storage/drivers/LocalDriver.d.ts +2 -0
- package/dist/storage/drivers/S3Driver.d.ts +3 -4
- package/dist/storage/drivers/s3-acl.d.ts +25 -0
- package/dist/storage/index.js +1 -1
- package/dist/storage/types.d.ts +35 -0
- package/dist/support/trim-slashes.d.ts +3 -2
- package/dist/support/warn-once.d.ts +11 -0
- package/package.json +1 -1
|
@@ -16,6 +16,18 @@ import {
|
|
|
16
16
|
} from "fs/promises";
|
|
17
17
|
import { existsSync } from "fs";
|
|
18
18
|
import { join, dirname, resolve, sep } from "path";
|
|
19
|
+
|
|
20
|
+
// src/support/warn-once.ts
|
|
21
|
+
var warned = /* @__PURE__ */ new Set();
|
|
22
|
+
function warnOnce(key, message) {
|
|
23
|
+
if (warned.has(key)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
warned.add(key);
|
|
27
|
+
console.warn(message);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/storage/drivers/LocalDriver.ts
|
|
19
31
|
var LocalDriver = class {
|
|
20
32
|
root;
|
|
21
33
|
baseUrl;
|
|
@@ -47,6 +59,9 @@ var LocalDriver = class {
|
|
|
47
59
|
}
|
|
48
60
|
}
|
|
49
61
|
async put(path, content, options) {
|
|
62
|
+
if (options?.visibility) {
|
|
63
|
+
this.warnUnsupportedVisibility(options.visibility, "put");
|
|
64
|
+
}
|
|
50
65
|
const fullPath = this.fullPath(path);
|
|
51
66
|
await this.ensureDirectory(fullPath);
|
|
52
67
|
const buffer = typeof content === "string" ? Buffer.from(content) : content;
|
|
@@ -181,10 +196,31 @@ var LocalDriver = class {
|
|
|
181
196
|
}
|
|
182
197
|
}
|
|
183
198
|
async setVisibility(path, visibility) {
|
|
199
|
+
await this.warnIfMissing(path, "setVisibility");
|
|
200
|
+
this.warnUnsupportedVisibility(visibility, "setVisibility");
|
|
184
201
|
}
|
|
185
202
|
async getVisibility(path) {
|
|
203
|
+
await this.warnIfMissing(path, "getVisibility");
|
|
186
204
|
return this.defaultVisibility;
|
|
187
205
|
}
|
|
206
|
+
async warnIfMissing(path, operation) {
|
|
207
|
+
if (await this.exists(path)) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
warnOnce(
|
|
211
|
+
`local-visibility-missing:${operation}`,
|
|
212
|
+
`[guren] LocalDriver.${operation}("${path}") was called for a file that does not exist and returned as if it had succeeded. Every other storage driver throws \`File not found\` here, and LocalDriver will too in the next major \u2014 check \`exists()\` first, or handle the error.`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
warnUnsupportedVisibility(requested, operation) {
|
|
216
|
+
if (requested === this.defaultVisibility) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
warnOnce(
|
|
220
|
+
`local-visibility-per-object:${operation}`,
|
|
221
|
+
`[guren] LocalDriver.${operation}() was asked to make an object ${requested} on a ${this.defaultVisibility} disk, and did nothing. A local disk has no per-object visibility \u2014 what makes a file reachable is the disk root and whatever serves it \u2014 so the request was never carried out, it only looked like it was. Declare the disk's "visibility" option to match, or keep restricted files on a disk that is not served. This becomes an error in the next major.`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
188
224
|
/**
|
|
189
225
|
* Get the root directory.
|
|
190
226
|
*/
|
|
@@ -193,6 +229,22 @@ var LocalDriver = class {
|
|
|
193
229
|
}
|
|
194
230
|
};
|
|
195
231
|
|
|
232
|
+
// src/storage/drivers/s3-acl.ts
|
|
233
|
+
function cannedAcl(visibility) {
|
|
234
|
+
return visibility === "public" ? "public-read" : "private";
|
|
235
|
+
}
|
|
236
|
+
function putAclFields(acl, visibility) {
|
|
237
|
+
return acl ? { ACL: cannedAcl(visibility) } : {};
|
|
238
|
+
}
|
|
239
|
+
function assertVisibilitySupported(acl, diskVisibility, requested, operation) {
|
|
240
|
+
if (acl || !requested || requested === diskVisibility) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
throw new Error(
|
|
244
|
+
`S3Driver.${operation}: cannot make an object ${requested} on a ${diskVisibility} disk configured with acl: false. This endpoint does not implement S3 object ACLs, so visibility is a property of the bucket. Declare it with the driver's "visibility" option, or serve restricted files through your own route.`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
196
248
|
// src/storage/drivers/S3Driver.ts
|
|
197
249
|
var S3Driver = class {
|
|
198
250
|
constructor(options) {
|
|
@@ -206,6 +258,7 @@ var S3Driver = class {
|
|
|
206
258
|
this.prefix = options.prefix ?? "";
|
|
207
259
|
this.baseUrl = options.url ?? `https://${this.bucket}.s3.${this.region}.amazonaws.com`;
|
|
208
260
|
this.defaultVisibility = options.visibility ?? "private";
|
|
261
|
+
this.acl = options.acl ?? true;
|
|
209
262
|
}
|
|
210
263
|
options;
|
|
211
264
|
client = null;
|
|
@@ -217,6 +270,7 @@ var S3Driver = class {
|
|
|
217
270
|
prefix;
|
|
218
271
|
baseUrl;
|
|
219
272
|
defaultVisibility;
|
|
273
|
+
acl;
|
|
220
274
|
/**
|
|
221
275
|
* Get or create the S3 client.
|
|
222
276
|
*/
|
|
@@ -247,13 +301,8 @@ var S3Driver = class {
|
|
|
247
301
|
prefixKey(path) {
|
|
248
302
|
return this.prefix ? `${this.prefix}/${path}` : path;
|
|
249
303
|
}
|
|
250
|
-
/**
|
|
251
|
-
* Get ACL from visibility.
|
|
252
|
-
*/
|
|
253
|
-
getAcl(visibility) {
|
|
254
|
-
return visibility === "public" ? "public-read" : "private";
|
|
255
|
-
}
|
|
256
304
|
async put(path, content, options) {
|
|
305
|
+
assertVisibilitySupported(this.acl, this.defaultVisibility, options?.visibility, "put");
|
|
257
306
|
const client = await this.getClient();
|
|
258
307
|
const { PutObjectCommand } = await importAwsModule("@aws-sdk/client-s3");
|
|
259
308
|
const body = typeof content === "string" ? Buffer.from(content) : content;
|
|
@@ -263,7 +312,7 @@ var S3Driver = class {
|
|
|
263
312
|
Key: this.prefixKey(path),
|
|
264
313
|
Body: body,
|
|
265
314
|
ContentType: options?.contentType,
|
|
266
|
-
|
|
315
|
+
...putAclFields(this.acl, visibility),
|
|
267
316
|
Metadata: options?.metadata
|
|
268
317
|
});
|
|
269
318
|
await client.send(command);
|
|
@@ -412,6 +461,14 @@ var S3Driver = class {
|
|
|
412
461
|
throw error;
|
|
413
462
|
}
|
|
414
463
|
}
|
|
464
|
+
/** `metadata()`, but honouring the contract's not-found error. */
|
|
465
|
+
async metadataOrFail(path) {
|
|
466
|
+
const metadata = await this.metadata(path);
|
|
467
|
+
if (!metadata) {
|
|
468
|
+
throw new Error(`File not found: ${path}`);
|
|
469
|
+
}
|
|
470
|
+
return metadata;
|
|
471
|
+
}
|
|
415
472
|
async files(directory) {
|
|
416
473
|
const client = await this.getClient();
|
|
417
474
|
const { ListObjectsV2Command } = await importAwsModule("@aws-sdk/client-s3");
|
|
@@ -459,16 +516,25 @@ var S3Driver = class {
|
|
|
459
516
|
}
|
|
460
517
|
}
|
|
461
518
|
async setVisibility(path, visibility) {
|
|
519
|
+
assertVisibilitySupported(this.acl, this.defaultVisibility, visibility, "setVisibility");
|
|
520
|
+
if (!this.acl) {
|
|
521
|
+
await this.metadataOrFail(path);
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
462
524
|
const client = await this.getClient();
|
|
463
525
|
const { PutObjectAclCommand } = await importAwsModule("@aws-sdk/client-s3");
|
|
464
526
|
const command = new PutObjectAclCommand({
|
|
465
527
|
Bucket: this.bucket,
|
|
466
528
|
Key: this.prefixKey(path),
|
|
467
|
-
ACL:
|
|
529
|
+
ACL: cannedAcl(visibility)
|
|
468
530
|
});
|
|
469
531
|
await client.send(command);
|
|
470
532
|
}
|
|
471
533
|
async getVisibility(path) {
|
|
534
|
+
if (!this.acl) {
|
|
535
|
+
await this.metadataOrFail(path);
|
|
536
|
+
return this.defaultVisibility;
|
|
537
|
+
}
|
|
472
538
|
const client = await this.getClient();
|
|
473
539
|
const { GetObjectAclCommand } = await importAwsModule("@aws-sdk/client-s3");
|
|
474
540
|
const command = new GetObjectAclCommand({
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,17 @@ import {
|
|
|
5
5
|
logDevServerBanner,
|
|
6
6
|
parseImportMap,
|
|
7
7
|
startViteDevServer
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-AOPXIURJ.js";
|
|
9
|
+
import {
|
|
10
|
+
LocalDriver,
|
|
11
|
+
MemoryDriver as MemoryDriver3,
|
|
12
|
+
S3Driver,
|
|
13
|
+
StorageManager,
|
|
14
|
+
createStorageManager
|
|
15
|
+
} from "./chunk-JTNRAWQR.js";
|
|
16
|
+
import {
|
|
17
|
+
trimSlashes
|
|
18
|
+
} from "./chunk-AZOYI43J.js";
|
|
9
19
|
import {
|
|
10
20
|
PendingSchedule,
|
|
11
21
|
Schedule,
|
|
@@ -20,16 +30,6 @@ import {
|
|
|
20
30
|
parseCron,
|
|
21
31
|
toTimezone
|
|
22
32
|
} from "./chunk-EI5GYMAT.js";
|
|
23
|
-
import {
|
|
24
|
-
LocalDriver,
|
|
25
|
-
MemoryDriver as MemoryDriver3,
|
|
26
|
-
S3Driver,
|
|
27
|
-
StorageManager,
|
|
28
|
-
createStorageManager
|
|
29
|
-
} from "./chunk-HWEWL6TE.js";
|
|
30
|
-
import {
|
|
31
|
-
trimSlashes
|
|
32
|
-
} from "./chunk-AZOYI43J.js";
|
|
33
33
|
import {
|
|
34
34
|
isDocsViewerEnabled
|
|
35
35
|
} from "./chunk-A5JJ7XTH.js";
|
package/dist/runtime/index.js
CHANGED
|
@@ -48,6 +48,8 @@ export declare class LocalDriver implements StorageDriver {
|
|
|
48
48
|
deleteDirectory(path: string): Promise<void>;
|
|
49
49
|
setVisibility(path: string, visibility: 'public' | 'private'): Promise<void>;
|
|
50
50
|
getVisibility(path: string): Promise<'public' | 'private'>;
|
|
51
|
+
private warnIfMissing;
|
|
52
|
+
private warnUnsupportedVisibility;
|
|
51
53
|
/**
|
|
52
54
|
* Get the root directory.
|
|
53
55
|
*/
|
|
@@ -27,6 +27,7 @@ export declare class S3Driver implements StorageDriver {
|
|
|
27
27
|
private readonly prefix;
|
|
28
28
|
private readonly baseUrl;
|
|
29
29
|
private readonly defaultVisibility;
|
|
30
|
+
private readonly acl;
|
|
30
31
|
constructor(options: S3DriverOptions);
|
|
31
32
|
/**
|
|
32
33
|
* Get or create the S3 client.
|
|
@@ -36,10 +37,6 @@ export declare class S3Driver implements StorageDriver {
|
|
|
36
37
|
* Get the prefixed key.
|
|
37
38
|
*/
|
|
38
39
|
private prefixKey;
|
|
39
|
-
/**
|
|
40
|
-
* Get ACL from visibility.
|
|
41
|
-
*/
|
|
42
|
-
private getAcl;
|
|
43
40
|
put(path: string, content: Buffer | string, options?: PutOptions): Promise<string>;
|
|
44
41
|
putFile(path: string, localPath: string, options?: PutOptions): Promise<string>;
|
|
45
42
|
get(path: string): Promise<Buffer | null>;
|
|
@@ -54,6 +51,8 @@ export declare class S3Driver implements StorageDriver {
|
|
|
54
51
|
size(path: string): Promise<number>;
|
|
55
52
|
lastModified(path: string): Promise<Date>;
|
|
56
53
|
metadata(path: string): Promise<FileMetadata | null>;
|
|
54
|
+
/** `metadata()`, but honouring the contract's not-found error. */
|
|
55
|
+
private metadataOrFail;
|
|
57
56
|
files(directory: string): Promise<string[]>;
|
|
58
57
|
directories(directory: string): Promise<string[]>;
|
|
59
58
|
allFiles(directory: string): Promise<string[]>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ACL half of the S3 driver, kept out of the driver so it can be tested
|
|
3
|
+
* without the optional `@aws-sdk/client-s3` peer installed: these functions
|
|
4
|
+
* decide what goes into a command, which is exactly the part that was wrong
|
|
5
|
+
* for every S3-compatible endpoint without ACL support.
|
|
6
|
+
*/
|
|
7
|
+
export type Visibility = 'public' | 'private';
|
|
8
|
+
/** The canned ACL S3 uses for each visibility. */
|
|
9
|
+
export declare function cannedAcl(visibility: Visibility): string;
|
|
10
|
+
/**
|
|
11
|
+
* The `ACL` field for a `PutObject` input, or nothing when the endpoint has
|
|
12
|
+
* no ACLs. Returned as a spreadable object so the caller never sends an
|
|
13
|
+
* explicit `ACL: undefined`, which some S3 implementations reject outright
|
|
14
|
+
* rather than treat as absent.
|
|
15
|
+
*/
|
|
16
|
+
export declare function putAclFields(acl: boolean, visibility: Visibility): {
|
|
17
|
+
ACL?: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Guard for a per-object visibility an ACL-less endpoint cannot honour.
|
|
21
|
+
* Throws rather than silently dropping the request: a `setVisibility(path,
|
|
22
|
+
* 'private')` that does nothing on a public bucket is a leak that looks like
|
|
23
|
+
* success.
|
|
24
|
+
*/
|
|
25
|
+
export declare function assertVisibilitySupported(acl: boolean, diskVisibility: Visibility, requested: Visibility | undefined, operation: string): void;
|
package/dist/storage/index.js
CHANGED
package/dist/storage/types.d.ts
CHANGED
|
@@ -160,12 +160,29 @@ export interface StorageDriver {
|
|
|
160
160
|
deleteDirectory(path: string): Promise<void>;
|
|
161
161
|
/**
|
|
162
162
|
* Set file visibility.
|
|
163
|
+
*
|
|
164
|
+
* Contract: throws when the file does not exist, so a caller cannot take
|
|
165
|
+
* success as proof the object is there. A driver whose backend has no
|
|
166
|
+
* per-object visibility (bucket-level access, a plain filesystem) reports
|
|
167
|
+
* the disk's configured visibility and throws when asked for the other
|
|
168
|
+
* value, rather than accepting a request it cannot honour — silently
|
|
169
|
+
* doing nothing is a leak that looks like success.
|
|
170
|
+
*
|
|
171
|
+
* `LocalDriver` is such a backend: what makes a local file reachable is
|
|
172
|
+
* the disk root and whatever serves it, not a flag on one file. It has
|
|
173
|
+
* always accepted per-object requests and done nothing, so it warns
|
|
174
|
+
* instead of throwing and will throw in the next major.
|
|
175
|
+
*
|
|
163
176
|
* @param path File path
|
|
164
177
|
* @param visibility Visibility setting
|
|
165
178
|
*/
|
|
166
179
|
setVisibility(path: string, visibility: 'public' | 'private'): Promise<void>;
|
|
167
180
|
/**
|
|
168
181
|
* Get file visibility.
|
|
182
|
+
*
|
|
183
|
+
* Contract: throws when the file does not exist. Drivers without
|
|
184
|
+
* per-object visibility report the disk's configured value.
|
|
185
|
+
*
|
|
169
186
|
* @param path File path
|
|
170
187
|
*/
|
|
171
188
|
getVisibility(path: string): Promise<'public' | 'private'>;
|
|
@@ -233,6 +250,24 @@ export interface S3DriverOptions {
|
|
|
233
250
|
* @default 'private'
|
|
234
251
|
*/
|
|
235
252
|
visibility?: 'public' | 'private';
|
|
253
|
+
/**
|
|
254
|
+
* Whether the endpoint implements S3 object ACLs.
|
|
255
|
+
*
|
|
256
|
+
* AWS S3 does, so this defaults to `true` and every `put` carries an
|
|
257
|
+
* `x-amz-acl` header derived from the file's visibility. Several
|
|
258
|
+
* S3-compatible endpoints do not: Cloudflare R2 documents `x-amz-acl` and
|
|
259
|
+
* the ACL operations as unsupported (access is decided per bucket), and
|
|
260
|
+
* MinIO/others vary. Set `acl: false` for those.
|
|
261
|
+
*
|
|
262
|
+
* With `acl: false` the driver stops sending the header, and visibility
|
|
263
|
+
* becomes a property of the whole disk: `getVisibility()` reports the
|
|
264
|
+
* configured `visibility`, and `put({ visibility })` / `setVisibility()`
|
|
265
|
+
* throw when asked for the other value rather than silently not applying
|
|
266
|
+
* it. Enforce per-object access in front of the disk instead.
|
|
267
|
+
*
|
|
268
|
+
* @default true
|
|
269
|
+
*/
|
|
270
|
+
acl?: boolean;
|
|
236
271
|
}
|
|
237
272
|
/**
|
|
238
273
|
* Memory driver options.
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
* quadratically on adversarial input (long slash runs mid-string), and
|
|
4
4
|
* several call sites here feed request-derived paths.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Twins live in `@guren/cli` (`src/utils.ts`, `trimSlashes`) and
|
|
7
|
+
* `@guren/plugin-cloudflare` (`src/storage/R2Driver.ts`, both functions);
|
|
8
|
+
* neither package can import this module, so the lines are duplicated by
|
|
8
9
|
* convention.
|
|
9
10
|
*/
|
|
10
11
|
export declare function trimTrailingSlashes(value: string): string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Emit a warning the first time a given key is seen in this process.
|
|
3
|
+
*
|
|
4
|
+
* Deprecation warnings have to be loud enough to be read once and quiet
|
|
5
|
+
* enough not to drown a request log, which is the same shape the OAuth
|
|
6
|
+
* warnings settled on (`auth/oauth/index.ts`) — this is that pattern with a
|
|
7
|
+
* key, so several call sites can share it.
|
|
8
|
+
*/
|
|
9
|
+
export declare function warnOnce(key: string, message: string): void;
|
|
10
|
+
/** Test seam: forget what has already been warned about. */
|
|
11
|
+
export declare function resetWarnOnce(): void;
|