@c9up/archive 0.1.6 → 0.1.8
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/ArchiveProvider.d.ts +7 -1
- package/dist/ArchiveProvider.d.ts.map +1 -1
- package/dist/ArchiveProvider.js +35 -9
- package/dist/ArchiveProvider.js.map +1 -1
- package/dist/DriveManager.d.ts +80 -0
- package/dist/DriveManager.d.ts.map +1 -0
- package/dist/DriveManager.js +102 -0
- package/dist/DriveManager.js.map +1 -0
- package/dist/GcsDriver.d.ts +14 -4
- package/dist/GcsDriver.d.ts.map +1 -1
- package/dist/GcsDriver.js +126 -15
- package/dist/GcsDriver.js.map +1 -1
- package/dist/S3Driver.d.ts +14 -4
- package/dist/S3Driver.d.ts.map +1 -1
- package/dist/S3Driver.js +268 -15
- package/dist/S3Driver.js.map +1 -1
- package/dist/StorageManager.d.ts +124 -18
- package/dist/StorageManager.d.ts.map +1 -1
- package/dist/StorageManager.js +139 -13
- package/dist/StorageManager.js.map +1 -1
- package/dist/expiry.d.ts +7 -0
- package/dist/expiry.d.ts.map +1 -1
- package/dist/expiry.js +47 -0
- package/dist/expiry.js.map +1 -1
- package/dist/index.d.ts +16 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -4
- package/dist/index.js.map +1 -1
- package/dist/objects.d.ts +50 -0
- package/dist/objects.d.ts.map +1 -0
- package/dist/objects.js +77 -0
- package/dist/objects.js.map +1 -0
- package/dist/testing/FakeStorage.d.ts +43 -4
- package/dist/testing/FakeStorage.d.ts.map +1 -1
- package/dist/testing/FakeStorage.js +153 -12
- package/dist/testing/FakeStorage.js.map +1 -1
- package/package.json +2 -1
- package/src/ArchiveProvider.ts +53 -12
- package/src/DriveManager.ts +151 -0
- package/src/GcsDriver.ts +166 -15
- package/src/S3Driver.ts +317 -15
- package/src/StorageManager.ts +284 -25
- package/src/expiry.ts +52 -0
- package/src/index.ts +26 -5
- package/src/objects.ts +92 -0
- package/src/testing/FakeStorage.ts +191 -12
|
@@ -2,7 +2,7 @@ import { type GcsConfig } from "./GcsDriver.js";
|
|
|
2
2
|
import { type S3Config } from "./S3Driver.js";
|
|
3
3
|
interface ArchiveContainer {
|
|
4
4
|
singleton(token: unknown, factory: () => unknown): void;
|
|
5
|
-
resolve<T = unknown>(token: unknown): T
|
|
5
|
+
resolve<T = unknown>(token: unknown): Promise<T>;
|
|
6
6
|
}
|
|
7
7
|
interface ArchiveConfigStore {
|
|
8
8
|
get<T = unknown>(key: string): T | undefined;
|
|
@@ -11,6 +11,12 @@ export interface ArchiveAppContext {
|
|
|
11
11
|
container: ArchiveContainer;
|
|
12
12
|
config: ArchiveConfigStore;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Legacy single-disk config shape (pre-parity). Still accepted: the
|
|
16
|
+
* provider wraps it in a one-service DriveManager under the disk name
|
|
17
|
+
* `default`. New apps should use the AdonisJS-parity {@link DriveConfig}
|
|
18
|
+
* (`{ default, services, fakes }`) built with `defineConfig` + `services.*`.
|
|
19
|
+
*/
|
|
14
20
|
export interface ArchiveConfig {
|
|
15
21
|
driver: "local" | "s3" | "gcs";
|
|
16
22
|
local?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ArchiveProvider.d.ts","sourceRoot":"","sources":["../src/ArchiveProvider.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ArchiveProvider.d.ts","sourceRoot":"","sources":["../src/ArchiveProvider.ts"],"names":[],"mappings":"AA0BA,OAAO,EAAE,KAAK,SAAS,EAAa,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,KAAK,QAAQ,EAAY,MAAM,eAAe,CAAC;AAQxD,UAAU,gBAAgB;IACzB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,GAAG,IAAI,CAAC;IACxD,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CACjD;AACD,UAAU,kBAAkB;IAC3B,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;CAC7C;AACD,MAAM,WAAW,iBAAiB;IACjC,SAAS,EAAE,gBAAgB,CAAC;IAC5B,MAAM,EAAE,kBAAkB,CAAC;CAC3B;AAED;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,OAAO,GAAG,IAAI,GAAG,KAAK,CAAC;IAC/B,KAAK,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACjD,EAAE,CAAC,EAAE,QAAQ,CAAC;IACd,GAAG,CAAC,EAAE,SAAS,CAAC;CAChB;AAOD,MAAM,CAAC,OAAO,OAAO,eAAe;IACvB,SAAS,CAAC,GAAG,EAAE,iBAAiB;gBAAtB,GAAG,EAAE,iBAAiB;IAE5C,QAAQ,IAAI,IAAI;IAkBV,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAkBrB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IACtB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IACtB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAC/B"}
|
package/dist/ArchiveProvider.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* import storage from '@c9up/archive/services/main'
|
|
22
22
|
* await storage.put('uploads/avatar.png', buffer)
|
|
23
23
|
*/
|
|
24
|
+
import { DriveManager } from "./DriveManager.js";
|
|
24
25
|
import { ArchiveError } from "./errors.js";
|
|
25
26
|
import { GcsDriver } from "./GcsDriver.js";
|
|
26
27
|
import { S3Driver } from "./S3Driver.js";
|
|
@@ -36,32 +37,57 @@ export default class ArchiveProvider {
|
|
|
36
37
|
this.app = app;
|
|
37
38
|
}
|
|
38
39
|
register() {
|
|
39
|
-
this.app.container.singleton(
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
return new StorageManager(driver);
|
|
40
|
+
this.app.container.singleton(DriveManager, () => {
|
|
41
|
+
const raw = this.app.config.get("archive");
|
|
42
|
+
return new DriveManager(resolveDriveConfig(raw));
|
|
43
43
|
});
|
|
44
|
-
|
|
44
|
+
// Backward-compatible bindings — the default disk is what apps
|
|
45
|
+
// previously resolved via `StorageManager` / the `storage` token.
|
|
46
|
+
this.app.container.singleton(StorageManager, async () => (await this.app.container.resolve(DriveManager)).use());
|
|
47
|
+
this.app.container.singleton("storage", async () => this.app.container.resolve(StorageManager));
|
|
48
|
+
this.app.container.singleton("drive", async () => this.app.container.resolve(DriveManager));
|
|
45
49
|
}
|
|
46
50
|
async boot() {
|
|
47
|
-
// Skip eager
|
|
51
|
+
// Skip eager resolution when archive is unconfigured: an app that
|
|
48
52
|
// registers this provider but never uses storage should not trigger
|
|
49
53
|
// LocalDriver's mkdirSync of './storage' at boot (breaks on
|
|
50
|
-
// read-only rootfs). Explicit (object-shaped) configs still
|
|
51
|
-
// fast.
|
|
54
|
+
// read-only rootfs). Explicit (object-shaped) configs still resolve.
|
|
52
55
|
const archive = this.app.config.get("archive");
|
|
53
56
|
if (archive === undefined ||
|
|
54
57
|
archive === null ||
|
|
55
58
|
typeof archive !== "object") {
|
|
56
59
|
return;
|
|
57
60
|
}
|
|
58
|
-
const manager = this.app.container.resolve(StorageManager);
|
|
61
|
+
const manager = await this.app.container.resolve(StorageManager);
|
|
59
62
|
setStorage(manager);
|
|
60
63
|
}
|
|
61
64
|
async start() { }
|
|
62
65
|
async ready() { }
|
|
63
66
|
async shutdown() { }
|
|
64
67
|
}
|
|
68
|
+
/** Narrow an unknown config value to the multi-disk {@link DriveConfig} shape. */
|
|
69
|
+
function isDriveConfig(config) {
|
|
70
|
+
return (typeof config === "object" &&
|
|
71
|
+
config !== null &&
|
|
72
|
+
"services" in config &&
|
|
73
|
+
typeof config.services === "object" &&
|
|
74
|
+
config.services !== null);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Reconcile whatever shape the app configured into a {@link DriveConfig}:
|
|
78
|
+
* - `{ default, services, fakes }` → used as-is (parity path).
|
|
79
|
+
* - legacy `{ driver, local, s3, gcs }` → wrapped as one `default` disk.
|
|
80
|
+
* - unset → the built-in local default.
|
|
81
|
+
*/
|
|
82
|
+
function resolveDriveConfig(raw) {
|
|
83
|
+
if (isDriveConfig(raw))
|
|
84
|
+
return raw;
|
|
85
|
+
const legacy = raw ?? DEFAULT_CONFIG;
|
|
86
|
+
return {
|
|
87
|
+
default: "default",
|
|
88
|
+
services: { default: () => buildDriver(legacy) },
|
|
89
|
+
};
|
|
90
|
+
}
|
|
65
91
|
function buildDriver(config) {
|
|
66
92
|
if (config.driver === "s3") {
|
|
67
93
|
if (!config.s3) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ArchiveProvider.js","sourceRoot":"","sources":["../src/ArchiveProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAkB,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAiB,QAAQ,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EACN,WAAW,EAEX,cAAc,GACd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"ArchiveProvider.js","sourceRoot":"","sources":["../src/ArchiveProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAoB,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAkB,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAiB,QAAQ,E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AwB,EACxB,wDAAwD,EACxD;gBACC,IAAI,EAAE,+FAA+F;aACrG,CACD,CAAC;QACH,CAAC;QACD,OAAO,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAChC,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;YACjB,MAAM,IAAI,YAAY,CACrB,wBAAwB,EACxB,0DAA0D,EAC1D;gBACC,IAAI,EAAE,6FAA6F;aACnG,CACD,CAAC;QACH,CAAC;QACD,OAAO,IAAI,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAClC,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;QAC/B,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,EAAE,IAAI,IAAI,WAAW,CAAC;QAC/C,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE;YAC5B,aAAa,EAAE,MAAM,CAAC,KAAK,EAAE,aAAa;SAC1C,CAAC,CAAC;IACJ,CAAC;IACD,yDAAyD;IACzD,8DAA8D;IAC9D,0DAA0D;IAC1D,MAAM,IAAI,YAAY,CACrB,wBAAwB,EACxB,+DAA+D,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EACvF,EAAE,IAAI,EAAE,uDAAuD,EAAE,CACjE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DriveManager — multi-disk registry (AdonisJS Drive / flydrive parity).
|
|
3
|
+
*
|
|
4
|
+
* Holds a set of named services (each a factory that returns a
|
|
5
|
+
* {@link StorageDriver}), resolves them lazily into cached
|
|
6
|
+
* {@link StorageManager} instances via {@link DriveManager.use}, and
|
|
7
|
+
* offers a manager-level {@link DriveManager.fake}/{@link DriveManager.restore}
|
|
8
|
+
* pair for tests.
|
|
9
|
+
*
|
|
10
|
+
* const drive = new DriveManager({
|
|
11
|
+
* default: 'uploads',
|
|
12
|
+
* services: {
|
|
13
|
+
* uploads: services.fs({ location: './storage' }),
|
|
14
|
+
* s3: services.s3({ bucket, region, accessKeyId, secretAccessKey }),
|
|
15
|
+
* },
|
|
16
|
+
* })
|
|
17
|
+
* await drive.use().put('a.txt', 'hi') // default disk
|
|
18
|
+
* await drive.use('s3').put('a.txt', 'hi') // named disk
|
|
19
|
+
*/
|
|
20
|
+
import type { GcsConfig } from "./GcsDriver.js";
|
|
21
|
+
import type { S3Config } from "./S3Driver.js";
|
|
22
|
+
import { type StorageDriver, StorageManager } from "./StorageManager.js";
|
|
23
|
+
import { FakeStorageManager } from "./testing/FakeStorage.js";
|
|
24
|
+
/** A factory that constructs a fresh driver for a named disk. */
|
|
25
|
+
export type DriveServiceFactory = () => StorageDriver;
|
|
26
|
+
/**
|
|
27
|
+
* Multi-disk configuration — AdonisJS Drive `defineConfig` shape:
|
|
28
|
+
* `{ default, services, fakes }`.
|
|
29
|
+
*/
|
|
30
|
+
export interface DriveConfig {
|
|
31
|
+
/** Name of the disk used when {@link DriveManager.use} is called with no argument. */
|
|
32
|
+
default: string;
|
|
33
|
+
/** Named disks, each a factory returning a driver (use the {@link services} helpers). */
|
|
34
|
+
services: Record<string, DriveServiceFactory>;
|
|
35
|
+
/** Optional fakes configuration (reserved; the in-memory fake needs no location). */
|
|
36
|
+
fakes?: {
|
|
37
|
+
location?: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Options accepted by {@link services.fs}. */
|
|
41
|
+
export interface FsServiceOptions {
|
|
42
|
+
/** Storage root. `location` (Adonis name) and `root` are equivalent. */
|
|
43
|
+
location?: string;
|
|
44
|
+
/** Storage root (ream alias of `location`). */
|
|
45
|
+
root?: string;
|
|
46
|
+
/** HMAC secret enabling `getSignedUrl` on the LocalDriver. */
|
|
47
|
+
signingSecret?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Config helpers mirroring AdonisJS Drive's `services.fs/s3/gcs`. Each
|
|
51
|
+
* returns a factory consumed by {@link DriveConfig.services}.
|
|
52
|
+
*
|
|
53
|
+
* DIVERGENCE (documented, deliberate): the ream `services.fs` builds URLs
|
|
54
|
+
* in-package via the LocalDriver + signed-route handler — it does NOT
|
|
55
|
+
* couple to the ream router the way Adonis wires `serveFiles`/`routeBasePath`.
|
|
56
|
+
*/
|
|
57
|
+
export declare const services: {
|
|
58
|
+
fs(options?: FsServiceOptions): DriveServiceFactory;
|
|
59
|
+
s3(config: S3Config): DriveServiceFactory;
|
|
60
|
+
gcs(config: GcsConfig): DriveServiceFactory;
|
|
61
|
+
};
|
|
62
|
+
export declare class DriveManager {
|
|
63
|
+
#private;
|
|
64
|
+
constructor(config: DriveConfig);
|
|
65
|
+
/**
|
|
66
|
+
* Resolve a disk by name (default when omitted). Returns the active
|
|
67
|
+
* fake for the disk when one has been installed via {@link fake}.
|
|
68
|
+
* Instances are cached and reused.
|
|
69
|
+
*/
|
|
70
|
+
use(service?: string): StorageManager;
|
|
71
|
+
/**
|
|
72
|
+
* Swap a disk for an in-memory {@link FakeStorageManager}. Subsequent
|
|
73
|
+
* {@link use} calls for the same disk return the fake until
|
|
74
|
+
* {@link restore} is called. AdonisJS Drive parity.
|
|
75
|
+
*/
|
|
76
|
+
fake(service?: string): FakeStorageManager;
|
|
77
|
+
/** Remove the fake for a disk (default when omitted) and clear its state. */
|
|
78
|
+
restore(service?: string): void;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=DriveManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DriveManager.d.ts","sourceRoot":"","sources":["../src/DriveManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAEN,KAAK,aAA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
3
|
+
*
|
|
4
|
+
* Holds a set of named services (each a factory that returns a
|
|
5
|
+
* {@link StorageDriver}), resolves them lazily into cached
|
|
6
|
+
* {@link StorageManager} instances via {@link DriveManager.use}, and
|
|
7
|
+
* offers a manager-level {@link DriveManager.fake}/{@link DriveManager.restore}
|
|
8
|
+
* pair for tests.
|
|
9
|
+
*
|
|
10
|
+
* const drive = new DriveManager({
|
|
11
|
+
* default: 'uploads',
|
|
12
|
+
* services: {
|
|
13
|
+
* uploads: services.fs({ location: './storage' }),
|
|
14
|
+
* s3: services.s3({ bucket, region, accessKeyId, secretAccessKey }),
|
|
15
|
+
* },
|
|
16
|
+
* })
|
|
17
|
+
* await drive.use().put('a.txt', 'hi') // default disk
|
|
18
|
+
* await drive.use('s3').put('a.txt', 'hi') // named disk
|
|
19
|
+
*/
|
|
20
|
+
import { ArchiveError } from "./errors.js";
|
|
21
|
+
import { GcsDriver } from "./GcsDriver.js";
|
|
22
|
+
import { S3Driver } from "./S3Driver.js";
|
|
23
|
+
import { LocalDriver, StorageManager, } from "./StorageManager.js";
|
|
24
|
+
import { FakeStorageManager } from "./testing/FakeStorage.js";
|
|
25
|
+
/**
|
|
26
|
+
* Config helpers mirroring AdonisJS Drive's `services.fs/s3/gcs`. Each
|
|
27
|
+
* returns a factory consumed by {@link DriveConfig.services}.
|
|
28
|
+
*
|
|
29
|
+
* DIVERGENCE (documented, deliberate): the ream `services.fs` builds URLs
|
|
30
|
+
* in-package via the LocalDriver + signed-route handler — it does NOT
|
|
31
|
+
* couple to the ream router the way Adonis wires `serveFiles`/`routeBasePath`.
|
|
32
|
+
*/
|
|
33
|
+
export const services = {
|
|
34
|
+
fs(options) {
|
|
35
|
+
const root = options?.location ?? options?.root ?? "./storage";
|
|
36
|
+
return () => new LocalDriver(root, { signingSecret: options?.signingSecret });
|
|
37
|
+
},
|
|
38
|
+
s3(config) {
|
|
39
|
+
return () => new S3Driver(config);
|
|
40
|
+
},
|
|
41
|
+
gcs(config) {
|
|
42
|
+
return () => new GcsDriver(config);
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
export class DriveManager {
|
|
46
|
+
#config;
|
|
47
|
+
#cache = new Map();
|
|
48
|
+
#fakes = new Map();
|
|
49
|
+
constructor(config) {
|
|
50
|
+
if (config.services[config.default] === undefined) {
|
|
51
|
+
throw new ArchiveError("ARCHIVE_UNKNOWN_DISK", `Archive default disk '${config.default}' is not present in config.services`, {
|
|
52
|
+
hint: `Add a '${config.default}' entry to config.services, or point config.default at an existing disk.`,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
this.#config = config;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Resolve a disk by name (default when omitted). Returns the active
|
|
59
|
+
* fake for the disk when one has been installed via {@link fake}.
|
|
60
|
+
* Instances are cached and reused.
|
|
61
|
+
*/
|
|
62
|
+
use(service) {
|
|
63
|
+
const name = service ?? this.#config.default;
|
|
64
|
+
const fake = this.#fakes.get(name);
|
|
65
|
+
if (fake)
|
|
66
|
+
return fake;
|
|
67
|
+
const cached = this.#cache.get(name);
|
|
68
|
+
if (cached)
|
|
69
|
+
return cached;
|
|
70
|
+
const factory = this.#config.services[name];
|
|
71
|
+
if (factory === undefined) {
|
|
72
|
+
throw new ArchiveError("ARCHIVE_UNKNOWN_DISK", `Archive disk '${name}' is not configured`, {
|
|
73
|
+
hint: `Known disks: ${Object.keys(this.#config.services).join(", ") || "(none)"}.`,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const manager = new StorageManager(factory());
|
|
77
|
+
this.#cache.set(name, manager);
|
|
78
|
+
return manager;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Swap a disk for an in-memory {@link FakeStorageManager}. Subsequent
|
|
82
|
+
* {@link use} calls for the same disk return the fake until
|
|
83
|
+
* {@link restore} is called. AdonisJS Drive parity.
|
|
84
|
+
*/
|
|
85
|
+
fake(service) {
|
|
86
|
+
const name = service ?? this.#config.default;
|
|
87
|
+
this.restore(name);
|
|
88
|
+
const fake = new FakeStorageManager();
|
|
89
|
+
this.#fakes.set(name, fake);
|
|
90
|
+
return fake;
|
|
91
|
+
}
|
|
92
|
+
/** Remove the fake for a disk (default when omitted) and clear its state. */
|
|
93
|
+
restore(service) {
|
|
94
|
+
const name = service ?? this.#config.default;
|
|
95
|
+
const fake = this.#fakes.get(name);
|
|
96
|
+
if (fake) {
|
|
97
|
+
fake.clear();
|
|
98
|
+
this.#fakes.delete(name);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=DriveManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DriveManager.js","sourceRoot":"","sources":["../src/DriveManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACN,WAAW,EAEX,cAAc,GACd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AA4B9D;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,QAAQ,GAIjB;IACH,EAAE,CAAC,OAAO;QACT,MAAM,IAAI,GAAG,OAAO,EAAE,QAAQ,IAAI,OAAO,EAAE,IAAI,IAAI,WAAW,CAAC;QAC/D,OAAO,GAAG,EAAE,CACX,IAAI,WAAW,CAAC,IAAI,EAAE,EAAE,aAAa,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC,CAAC;IACnE,CAAC;IACD,EAAE,CAAC,MAAM;QACR,OAAO,GAAG,EAAE,CAAC,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IACD,GAAG,CAAC,MAAM;QACT,OAAO,GAAG,EAAE,CAAC,IAAI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AAM,OAAO,GAAG,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC/B,OAAO,OAAO,CAAC;IAChB,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAC,OAAgB;QACpB,MAAM,IAAI,GAAG,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;QAC7C,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACnB,MAAM,IAAI,GAAG,IAAI,kBAAkB,EAAE,CAAC;QACtC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC5B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,6EAA6E;IAC7E,OAAO,CAAC,OAAgB;QACvB,MAAM,IAAI,GAAG,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;QAC7C,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACnC,IAAI,IAAI,EAAE,CAAC;YACV,IAAI,CAAC,KAAK,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC;IACF,CAAC;CACD"}
|
package/dist/GcsDriver.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @implements MISS-24
|
|
10
10
|
*/
|
|
11
|
-
import type { Metadata, PutStreamOptions, SignedUrlOptions, StorageDriver, StorageEntry, Visibility } from "./StorageManager.js";
|
|
11
|
+
import type { ListAllOptions, ListAllResult, Metadata, PutStreamOptions, SignedUrlOptions, StorageDriver, StorageEntry, Visibility, WriteOptions } from "./StorageManager.js";
|
|
12
12
|
export interface GcsServiceAccount {
|
|
13
13
|
client_email: string;
|
|
14
14
|
private_key: string;
|
|
@@ -23,7 +23,8 @@ export interface GcsConfig {
|
|
|
23
23
|
export declare class GcsDriver implements StorageDriver {
|
|
24
24
|
#private;
|
|
25
25
|
constructor(config: GcsConfig);
|
|
26
|
-
put(filePath: string, content: Buffer | string): Promise<void>;
|
|
26
|
+
put(filePath: string, content: Buffer | string, options?: WriteOptions): Promise<void>;
|
|
27
|
+
getBytes(filePath: string): Promise<Uint8Array>;
|
|
27
28
|
putStream(filePath: string, readable: NodeJS.ReadableStream, options?: PutStreamOptions): Promise<void>;
|
|
28
29
|
get(filePath: string): Promise<Buffer | null>;
|
|
29
30
|
getStream(filePath: string): Promise<NodeJS.ReadableStream>;
|
|
@@ -32,10 +33,19 @@ export declare class GcsDriver implements StorageDriver {
|
|
|
32
33
|
publicUrl(filePath: string): string;
|
|
33
34
|
url(filePath: string): Promise<string>;
|
|
34
35
|
getMetadata(filePath: string): Promise<Metadata>;
|
|
36
|
+
getVisibility(filePath: string): Promise<Visibility>;
|
|
35
37
|
setVisibility(filePath: string, visibility: Visibility): Promise<void>;
|
|
36
|
-
copy(from: string, to: string): Promise<void>;
|
|
37
|
-
move(from: string, to: string): Promise<void>;
|
|
38
|
+
copy(from: string, to: string, options?: WriteOptions): Promise<void>;
|
|
39
|
+
move(from: string, to: string, options?: WriteOptions): Promise<void>;
|
|
40
|
+
deleteAll(prefix: string): Promise<void>;
|
|
41
|
+
listAll(prefix: string, options?: ListAllOptions): Promise<ListAllResult>;
|
|
38
42
|
list(prefix: string): AsyncIterable<StorageEntry>;
|
|
39
43
|
getSignedUrl(filePath: string, options?: SignedUrlOptions): string;
|
|
44
|
+
/**
|
|
45
|
+
* Presigned PUT URL for direct-to-GCS uploads. AdonisJS Drive parity.
|
|
46
|
+
* Signed headers are `host` only, so the uploading client is free to
|
|
47
|
+
* set its own `Content-Type`.
|
|
48
|
+
*/
|
|
49
|
+
getSignedUploadUrl(filePath: string, options?: SignedUrlOptions): string;
|
|
40
50
|
}
|
|
41
51
|
//# sourceMappingURL=GcsDriver.d.ts.map
|
package/dist/GcsDriver.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GcsDriver.d.ts","sourceRoot":"","sources":["../src/GcsDriver.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;
|
|
1
|
+
{"version":3,"file":"GcsDriver.d.ts","sourceRoot":"","sources":["../src/GcsDriver.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAaH,OAAO,KAAK,EACX,cAAc,EACd,aAAa,EACb,QAAQ,EACR,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,MAAM,qBAAqB,CAAC;AAqC7B,MAAM,WAAW,iBAAiB;IACjC,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,SAAS;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,iBAAiB,CAAC;IAClC,4EAA4E;IAC5E,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAQD,qBAAa,SAAU,YAAW,aAAa;;gBAIlC,MAAM,EAAE,SAAS;IAevB,GAAG,CACR,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,OAAO,CAAC,EAAE,YAAY,GACpB,OAAO,CAAC,IAAI,CAAC;IA6BV,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAc/C,SAAS,CACd,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,CAAC,cAAc,EAC/B,OAAO,CAAC,EAAE,gBAAgB,GACxB,OAAO,CAAC,IAAI,CAAC;IAwBV,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAY7C,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,cAAc,C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{ createHash, createSign } from "node:crypto";
|
|
12
12
|
import { Readable } from "node:stream";
|
|
13
13
|
import { ArchiveError } from "./errors.js";
|
|
14
|
-
import { assertValidExpiry, DEFAULT_EXPIRES_IN } from "./expiry.js";
|
|
14
|
+
import { assertValidExpiry, DEFAULT_EXPIRES_IN, parseExpiry, } from "./expiry.js";
|
|
15
15
|
import { inferMimeType } from "./mime-types.js";
|
|
16
|
+
import { DriveDirectory, DriveFile } from "./objects.js";
|
|
16
17
|
/** GCS V4 strict URL encoding — identical to AWS's RFC3986-unreserved
|
|
17
18
|
* set. Duplicated from `S3Driver.ts` on purpose: per cerebrum rule,
|
|
18
19
|
* drivers do not share helpers even within the package. */
|
|
@@ -57,22 +58,42 @@ export class GcsDriver {
|
|
|
57
58
|
}
|
|
58
59
|
this.#config = config;
|
|
59
60
|
}
|
|
60
|
-
async put(filePath, content) {
|
|
61
|
+
async put(filePath, content, options) {
|
|
61
62
|
const body = typeof content === "string" ? Buffer.from(content) : content;
|
|
62
63
|
const token = await this.#getAccessToken();
|
|
63
64
|
const url = `${GCS_UPLOAD_ROOT}/b/${this.#config.bucket}/o?uploadType=media&name=${gcsStrictEncode(filePath)}`;
|
|
64
|
-
const mime =
|
|
65
|
+
const mime = options?.contentType ??
|
|
66
|
+
(inferMimeType(extFromPath(filePath)) || "application/octet-stream");
|
|
67
|
+
const headers = {
|
|
68
|
+
authorization: `Bearer ${token}`,
|
|
69
|
+
"content-type": mime,
|
|
70
|
+
};
|
|
71
|
+
if (options?.cacheControl)
|
|
72
|
+
headers["cache-control"] = options.cacheControl;
|
|
73
|
+
if (options?.contentDisposition) {
|
|
74
|
+
headers["content-disposition"] = options.contentDisposition;
|
|
75
|
+
}
|
|
65
76
|
const res = await fetch(url, {
|
|
66
77
|
method: "POST",
|
|
67
|
-
headers
|
|
68
|
-
authorization: `Bearer ${token}`,
|
|
69
|
-
"content-type": mime,
|
|
70
|
-
},
|
|
78
|
+
headers,
|
|
71
79
|
body: body,
|
|
72
80
|
});
|
|
73
81
|
if (!res.ok) {
|
|
74
82
|
throw new Error(`GCS PUT failed (${res.status}): ${await res.text()}`);
|
|
75
83
|
}
|
|
84
|
+
// GCS has no ACL in the media-upload call; apply visibility after.
|
|
85
|
+
if (options?.visibility !== undefined) {
|
|
86
|
+
await this.setVisibility(filePath, options.visibility);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async getBytes(filePath) {
|
|
90
|
+
const buf = await this.get(filePath);
|
|
91
|
+
if (buf === null) {
|
|
92
|
+
throw new ArchiveError("ARCHIVE_NOT_FOUND", `GCS object does not exist at path '${filePath}'`, {
|
|
93
|
+
hint: "Confirm the bucket + key and that the object was put() first.",
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
76
97
|
}
|
|
77
98
|
async putStream(filePath, readable, options) {
|
|
78
99
|
// Single-PUT path only for this story. Resumable upload parity is
|
|
@@ -187,14 +208,21 @@ export class GcsDriver {
|
|
|
187
208
|
? "public"
|
|
188
209
|
: "private"
|
|
189
210
|
: "private";
|
|
211
|
+
const size = Number.parseInt(json.size ?? "0", 10);
|
|
212
|
+
const mimeType = json.contentType ?? "application/octet-stream";
|
|
190
213
|
return {
|
|
191
|
-
size
|
|
192
|
-
|
|
214
|
+
size,
|
|
215
|
+
contentLength: size,
|
|
216
|
+
mimeType,
|
|
217
|
+
contentType: mimeType,
|
|
193
218
|
lastModified: json.updated ? new Date(json.updated) : new Date(0),
|
|
194
219
|
etag: (json.etag ?? "").replace(/^(W\/)?"(.*)"$/, "$1$2"),
|
|
195
220
|
visibility,
|
|
196
221
|
};
|
|
197
222
|
}
|
|
223
|
+
async getVisibility(filePath) {
|
|
224
|
+
return (await this.getMetadata(filePath)).visibility;
|
|
225
|
+
}
|
|
198
226
|
async setVisibility(filePath, visibility) {
|
|
199
227
|
const token = await this.#getAccessToken();
|
|
200
228
|
const base = `${GCS_META_ROOT}/b/${this.#config.bucket}/o/${gcsEncodeKey(filePath)}/acl`;
|
|
@@ -222,7 +250,7 @@ export class GcsDriver {
|
|
|
222
250
|
}
|
|
223
251
|
}
|
|
224
252
|
}
|
|
225
|
-
async copy(from, to) {
|
|
253
|
+
async copy(from, to, options) {
|
|
226
254
|
const token = await this.#getAccessToken();
|
|
227
255
|
const url = `${GCS_META_ROOT}/b/${this.#config.bucket}/o/${gcsEncodeKey(from)}/copyTo/b/${this.#config.bucket}/o/${gcsEncodeKey(to)}`;
|
|
228
256
|
const res = await fetch(url, {
|
|
@@ -235,12 +263,68 @@ export class GcsDriver {
|
|
|
235
263
|
if (!res.ok) {
|
|
236
264
|
throw new Error(`GCS COPY failed (${res.status}): ${await res.text()}`);
|
|
237
265
|
}
|
|
266
|
+
if (options?.visibility !== undefined) {
|
|
267
|
+
await this.setVisibility(to, options.visibility);
|
|
268
|
+
}
|
|
238
269
|
}
|
|
239
|
-
async move(from, to) {
|
|
270
|
+
async move(from, to, options) {
|
|
240
271
|
// Non-atomic: copy then delete. Same as S3.
|
|
241
|
-
await this.copy(from, to);
|
|
272
|
+
await this.copy(from, to, options);
|
|
242
273
|
await this.delete(from);
|
|
243
274
|
}
|
|
275
|
+
async deleteAll(prefix) {
|
|
276
|
+
// GCS has no batch-delete endpoint; drain the listing one object at
|
|
277
|
+
// a time. `list()` already paginates lazily.
|
|
278
|
+
const keys = [];
|
|
279
|
+
for await (const entry of this.list(prefix)) {
|
|
280
|
+
keys.push(entry.path);
|
|
281
|
+
}
|
|
282
|
+
for (const key of keys) {
|
|
283
|
+
await this.delete(key);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
async listAll(prefix, options) {
|
|
287
|
+
const recursive = options?.recursive ?? false;
|
|
288
|
+
const normalizedPrefix = prefix === "" || prefix === "/"
|
|
289
|
+
? ""
|
|
290
|
+
: !recursive && !prefix.endsWith("/")
|
|
291
|
+
? `${prefix}/`
|
|
292
|
+
: prefix;
|
|
293
|
+
const params = new URLSearchParams({ prefix: normalizedPrefix });
|
|
294
|
+
if (!recursive)
|
|
295
|
+
params.set("delimiter", "/");
|
|
296
|
+
if (options?.paginationToken) {
|
|
297
|
+
params.set("pageToken", options.paginationToken);
|
|
298
|
+
}
|
|
299
|
+
const url = `${GCS_META_ROOT}/b/${this.#config.bucket}/o?${params.toString()}`;
|
|
300
|
+
const token = await this.#getAccessToken();
|
|
301
|
+
const res = await fetch(url, {
|
|
302
|
+
method: "GET",
|
|
303
|
+
headers: { authorization: `Bearer ${token}` },
|
|
304
|
+
});
|
|
305
|
+
if (!res.ok) {
|
|
306
|
+
throw new Error(`GCS LIST failed (${res.status}): ${await res.text()}`);
|
|
307
|
+
}
|
|
308
|
+
const json = (await res.json());
|
|
309
|
+
const objects = [];
|
|
310
|
+
for (const dir of json.prefixes ?? []) {
|
|
311
|
+
objects.push(new DriveDirectory(dir.replace(/\/$/, "")));
|
|
312
|
+
}
|
|
313
|
+
for (const item of json.items ?? []) {
|
|
314
|
+
const size = Number.parseInt(item.size ?? "0", 10);
|
|
315
|
+
const mimeType = item.contentType ?? "application/octet-stream";
|
|
316
|
+
objects.push(new DriveFile(item.name ?? "", this, {
|
|
317
|
+
size,
|
|
318
|
+
contentLength: size,
|
|
319
|
+
mimeType,
|
|
320
|
+
contentType: mimeType,
|
|
321
|
+
lastModified: item.updated ? new Date(item.updated) : new Date(0),
|
|
322
|
+
etag: (item.etag ?? "").replace(/^(W\/)?"(.*)"$/, "$1$2"),
|
|
323
|
+
visibility: "private",
|
|
324
|
+
}));
|
|
325
|
+
}
|
|
326
|
+
return { paginationToken: json.nextPageToken, objects };
|
|
327
|
+
}
|
|
244
328
|
async *list(prefix) {
|
|
245
329
|
let pageToken;
|
|
246
330
|
do {
|
|
@@ -268,7 +352,33 @@ export class GcsDriver {
|
|
|
268
352
|
} while (pageToken);
|
|
269
353
|
}
|
|
270
354
|
getSignedUrl(filePath, options) {
|
|
271
|
-
const
|
|
355
|
+
const responseParams = [];
|
|
356
|
+
if (options?.contentType) {
|
|
357
|
+
responseParams.push(["response-content-type", options.contentType]);
|
|
358
|
+
}
|
|
359
|
+
if (options?.contentDisposition) {
|
|
360
|
+
responseParams.push([
|
|
361
|
+
"response-content-disposition",
|
|
362
|
+
options.contentDisposition,
|
|
363
|
+
]);
|
|
364
|
+
}
|
|
365
|
+
return this.#presign("GET", filePath, options, responseParams);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Presigned PUT URL for direct-to-GCS uploads. AdonisJS Drive parity.
|
|
369
|
+
* Signed headers are `host` only, so the uploading client is free to
|
|
370
|
+
* set its own `Content-Type`.
|
|
371
|
+
*/
|
|
372
|
+
getSignedUploadUrl(filePath, options) {
|
|
373
|
+
return this.#presign("PUT", filePath, options, []);
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Shared GOOG4-RSA-SHA256 query-parameter presigner for GET (read)
|
|
377
|
+
* and PUT (upload) URLs. Signs `host` only; extra query params (e.g.
|
|
378
|
+
* `response-content-*`) participate in the signature.
|
|
379
|
+
*/
|
|
380
|
+
#presign(method, filePath, options, extraQueryParams) {
|
|
381
|
+
const expiresIn = parseExpiry(options?.expiresIn ?? DEFAULT_EXPIRES_IN);
|
|
272
382
|
assertValidExpiry(expiresIn);
|
|
273
383
|
const dateStamp = amzDateNow();
|
|
274
384
|
const shortDate = dateStamp.slice(0, 8);
|
|
@@ -280,14 +390,15 @@ export class GcsDriver {
|
|
|
280
390
|
["X-Goog-Date", dateStamp],
|
|
281
391
|
["X-Goog-Expires", String(expiresIn)],
|
|
282
392
|
["X-Goog-SignedHeaders", "host"],
|
|
393
|
+
...extraQueryParams,
|
|
283
394
|
];
|
|
284
395
|
const canonicalQs = queryParams
|
|
285
396
|
.slice()
|
|
286
397
|
.sort((a, b) => (a[0] < b[0] ? -1 : 1))
|
|
287
|
-
.map(([k, v]) => `${k}=${gcsStrictEncode(v)}`)
|
|
398
|
+
.map(([k, v]) => `${gcsStrictEncode(k)}=${gcsStrictEncode(v)}`)
|
|
288
399
|
.join("&");
|
|
289
400
|
const canonicalRequest = [
|
|
290
|
-
|
|
401
|
+
method,
|
|
291
402
|
`/${this.#config.bucket}/${gcsEncodeKey(filePath)}`,
|
|
292
403
|
canonicalQs,
|
|
293
404
|
"host:storage.googleapis.com\n",
|