@geekmidas/storage 10.0.0-alpha.99 → 10.0.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/CHANGELOG.md +79 -0
- package/README.md +13 -49
- package/dist/AmazonStorageClient-BHpWawGD.d.mts +45 -0
- package/dist/AmazonStorageClient-BHpWawGD.d.mts.map +1 -0
- package/dist/AmazonStorageClient-BNoY2PL6.d.cts +45 -0
- package/dist/AmazonStorageClient-BNoY2PL6.d.cts.map +1 -0
- package/dist/AmazonStorageClient-BvoZepKJ.cjs +161 -0
- package/dist/AmazonStorageClient-BvoZepKJ.cjs.map +1 -0
- package/dist/AmazonStorageClient-Dz-KCJ_4.mjs +127 -0
- package/dist/AmazonStorageClient-Dz-KCJ_4.mjs.map +1 -0
- package/dist/AmazonStorageClient.cjs +5 -0
- package/dist/AmazonStorageClient.d.cts +3 -0
- package/dist/AmazonStorageClient.d.mts +3 -0
- package/dist/AmazonStorageClient.mjs +4 -0
- package/dist/StorageClient-4ADnTPnJ.mjs +11 -0
- package/dist/StorageClient-4ADnTPnJ.mjs.map +1 -0
- package/dist/StorageClient-BDRVrMJj.cjs +17 -0
- package/dist/StorageClient-BDRVrMJj.cjs.map +1 -0
- package/dist/{registry-ClEvHGpM.d.cts → StorageClient-COs9SOsU.d.cts} +5 -27
- package/dist/StorageClient-COs9SOsU.d.cts.map +1 -0
- package/dist/{registry-ClEvHGpM.d.mts → StorageClient-DH4tP-Mf.d.mts} +5 -27
- package/dist/StorageClient-DH4tP-Mf.d.mts.map +1 -0
- package/dist/StorageClient.cjs +3 -0
- package/dist/StorageClient.d.cts +2 -0
- package/dist/StorageClient.d.mts +2 -0
- package/dist/StorageClient.mjs +3 -0
- package/dist/aws.cjs +4 -198
- package/dist/aws.d.cts +3 -50
- package/dist/aws.d.mts +3 -50
- package/dist/aws.mjs +3 -189
- package/dist/index.cjs +0 -55
- package/dist/index.d.cts +2 -87
- package/dist/index.d.mts +2 -87
- package/dist/index.mjs +0 -45
- package/package.json +9 -27
- package/src/AmazonStorageClient.ts +207 -0
- package/src/StorageClient.ts +108 -0
- package/src/__tests__/AmazonStorageClient.spec.ts +606 -0
- package/src/__tests__/StorageClient.spec.ts +98 -0
- package/src/aws.ts +11 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +9 -0
- package/tsdown.config.ts +9 -0
- package/dist/aws.cjs.map +0 -1
- package/dist/aws.d.cts.map +0 -1
- package/dist/aws.d.mts.map +0 -1
- package/dist/aws.mjs.map +0 -1
- package/dist/errors-BB8jdHif.cjs +0 -157
- package/dist/errors-BB8jdHif.cjs.map +0 -1
- package/dist/errors-COXs1iNg.mjs +0 -116
- package/dist/errors-COXs1iNg.mjs.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/registry-ClEvHGpM.d.cts.map +0 -1
- package/dist/registry-ClEvHGpM.d.mts.map +0 -1
- package/dist/rolldown-runtime-D7D4PA-g.mjs +0 -13
- package/dist/s3Url.cjs +0 -109
- package/dist/s3Url.cjs.map +0 -1
- package/dist/s3Url.d.cts +0 -52
- package/dist/s3Url.d.cts.map +0 -1
- package/dist/s3Url.d.mts +0 -52
- package/dist/s3Url.d.mts.map +0 -1
- package/dist/s3Url.mjs +0 -90
- package/dist/s3Url.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_errors = require("./errors-BB8jdHif.cjs");
|
|
3
|
-
//#region src/registry.ts
|
|
4
|
-
/**
|
|
5
|
-
* Driver registry — how a storage URL becomes a client without the caller
|
|
6
|
-
* naming a cloud.
|
|
7
|
-
*
|
|
8
|
-
* A construct declares that it needs object storage and receives one URL. It
|
|
9
|
-
* must not import `AmazonStorageClient` to use it: that would name a provider in
|
|
10
|
-
* the neutral layer and drag the AWS SDK into an app that only ever talks to
|
|
11
|
-
* GCS. So the protocol selects the driver, and whoever assembles the
|
|
12
|
-
* application decides which drivers exist — a generated handler registering the
|
|
13
|
-
* one its target needs, or a dev entry registering several.
|
|
14
|
-
*
|
|
15
|
-
* Registration is explicit rather than an import side effect, because a
|
|
16
|
-
* side-effecting module is exactly what a bundler is entitled to drop.
|
|
17
|
-
*/
|
|
18
|
-
const drivers = /* @__PURE__ */ new Map();
|
|
19
|
-
/** Make a driver available to {@link createStorageClient}. Idempotent. */
|
|
20
|
-
function registerStorageDriver(driver) {
|
|
21
|
-
drivers.set(driver.scheme, driver);
|
|
22
|
-
}
|
|
23
|
-
/** Which schemes are currently registered — the useful half of a failure. */
|
|
24
|
-
function registeredStorageSchemes() {
|
|
25
|
-
return [...drivers.keys()].sort();
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Build a client for a URL.
|
|
29
|
-
*
|
|
30
|
-
* @throws {UnregisteredStorageScheme} when no driver handles the scheme, which
|
|
31
|
-
* in practice means the build pinned a different one, or nothing registered.
|
|
32
|
-
*/
|
|
33
|
-
function createStorageClient(url) {
|
|
34
|
-
const scheme = schemeOf(url);
|
|
35
|
-
const driver = drivers.get(scheme);
|
|
36
|
-
if (!driver) throw new require_errors.UnregisteredStorageScheme(url, scheme, registeredStorageSchemes());
|
|
37
|
-
return driver.create(url);
|
|
38
|
-
}
|
|
39
|
-
function schemeOf(url) {
|
|
40
|
-
const separator = url.indexOf(":");
|
|
41
|
-
return separator === -1 ? "" : url.slice(0, separator + 1);
|
|
42
|
-
}
|
|
43
|
-
//#endregion
|
|
44
|
-
exports.IncompleteStorageCredentials = require_errors.IncompleteStorageCredentials;
|
|
45
|
-
exports.MalformedStorageUrl = require_errors.MalformedStorageUrl;
|
|
46
|
-
exports.MissingStorageBucket = require_errors.MissingStorageBucket;
|
|
47
|
-
exports.StorageUrlError = require_errors.StorageUrlError;
|
|
48
|
-
exports.UnexpectedStorageScheme = require_errors.UnexpectedStorageScheme;
|
|
49
|
-
exports.UnregisteredStorageScheme = require_errors.UnregisteredStorageScheme;
|
|
50
|
-
exports.createStorageClient = createStorageClient;
|
|
51
|
-
exports.redactStorageUrl = require_errors.redactStorageUrl;
|
|
52
|
-
exports.registerStorageDriver = registerStorageDriver;
|
|
53
|
-
exports.registeredStorageSchemes = registeredStorageSchemes;
|
|
54
|
-
|
|
55
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.d.cts
CHANGED
|
@@ -1,87 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Storage errors — shared by every implementation, not just S3.
|
|
5
|
-
*
|
|
6
|
-
* A `gs://` or Azure client hits the same three problems as the S3 one: the
|
|
7
|
-
* string is not a URL, it is a URL for a different provider, or it addresses no
|
|
8
|
-
* bucket. Each carries the provider-specific detail as a field, so the type is
|
|
9
|
-
* common while the specifics stay accurate.
|
|
10
|
-
*
|
|
11
|
-
* Messages state the rule, which is constant, and never interpolate the value.
|
|
12
|
-
* An interpolated message cannot be matched on, reads differently every time it
|
|
13
|
-
* is thrown, and carries user input into every log line that touches it.
|
|
14
|
-
*/
|
|
15
|
-
/** Base for anything wrong with a storage URL, so callers may catch broadly. */
|
|
16
|
-
export declare abstract class StorageUrlError extends Error {
|
|
17
|
-
/**
|
|
18
|
-
* The URL that could not be used, with any credentials it carried replaced
|
|
19
|
-
* by `REDACTED` — an error is logged, and a key in its userinfo would be
|
|
20
|
-
* logged with it.
|
|
21
|
-
*/
|
|
22
|
-
readonly url: string;
|
|
23
|
-
constructor(url: string, message: string);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Replace a URL's userinfo with `REDACTED`, so `s3://KEY:SECRET@uploads` can be
|
|
27
|
-
* logged as `s3://REDACTED@uploads`. A URL without userinfo comes back as is.
|
|
28
|
-
*
|
|
29
|
-
* A string the URL parser rejects is still redacted, up to its last `@`: an
|
|
30
|
-
* unencoded `/` in a secret is the commonest way to write a malformed one, and
|
|
31
|
-
* that string is exactly the one that ends up in an error.
|
|
32
|
-
*/
|
|
33
|
-
export declare function redactStorageUrl(url: string): string;
|
|
34
|
-
/** The string is not a URL at all. */
|
|
35
|
-
export declare class MalformedStorageUrl extends StorageUrlError {
|
|
36
|
-
constructor(url: string);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* A URL, but for a different provider — a `gs://` URL reaching the S3 client,
|
|
40
|
-
* or the reverse. The usual cause is a construct resolving against the wrong
|
|
41
|
-
* target, which is worth distinguishing from a malformed string.
|
|
42
|
-
*/
|
|
43
|
-
export declare class UnexpectedStorageScheme extends StorageUrlError {
|
|
44
|
-
/** The scheme this client handles, e.g. `'s3:'`. */
|
|
45
|
-
readonly expected: string;
|
|
46
|
-
/** The scheme the URL actually carried, e.g. `'gs:'`. */
|
|
47
|
-
readonly actual: string;
|
|
48
|
-
constructor(url: string, expected: string, actual: string);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* A well-formed URL that addresses no bucket.
|
|
52
|
-
*
|
|
53
|
-
* "Bucket" is the term S3 and GCS share; Azure calls the same thing a
|
|
54
|
-
* container. One error covers all three — the word in the message is the
|
|
55
|
-
* common one rather than a new abstraction nobody uses.
|
|
56
|
-
*/
|
|
57
|
-
export declare class MissingStorageBucket extends StorageUrlError {
|
|
58
|
-
constructor(url: string);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* The URL's userinfo carries one half of a key pair: an access key with no
|
|
62
|
-
* secret, or a secret with no access key. Either both are in the URL, or
|
|
63
|
-
* neither is and the provider's default credential chain applies — half a pair
|
|
64
|
-
* is never silently completed from the environment.
|
|
65
|
-
*/
|
|
66
|
-
export declare class IncompleteStorageCredentials extends StorageUrlError {
|
|
67
|
-
/** Which half is missing: `'accessKeyId'` or `'secretAccessKey'`. */
|
|
68
|
-
readonly missing: 'accessKeyId' | 'secretAccessKey';
|
|
69
|
-
constructor(url: string, missing: 'accessKeyId' | 'secretAccessKey');
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* No driver is registered for the URL's scheme.
|
|
73
|
-
*
|
|
74
|
-
* Usually means the build pinned a driver for a different target, or an entry
|
|
75
|
-
* point registered nothing at all — so the registered schemes are carried
|
|
76
|
-
* alongside, since "what *is* available" is the actionable half.
|
|
77
|
-
*/
|
|
78
|
-
export declare class UnregisteredStorageScheme extends StorageUrlError {
|
|
79
|
-
/** The scheme that had no driver, e.g. `'gs:'`. */
|
|
80
|
-
readonly scheme: string;
|
|
81
|
-
/** What is registered, so the caller can see what was pinned instead. */
|
|
82
|
-
readonly registered: readonly string[];
|
|
83
|
-
constructor(url: string, scheme: string, registered: readonly string[]);
|
|
84
|
-
}
|
|
85
|
-
//#endregion
|
|
86
|
-
export { type StorageClient, type StorageDriver, createStorageClient, registerStorageDriver, registeredStorageSchemes };
|
|
87
|
-
//# sourceMappingURL=index.d.cts.map
|
|
1
|
+
import { StorageClient } from "./StorageClient-COs9SOsU.cjs";
|
|
2
|
+
export { StorageClient };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,87 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Storage errors — shared by every implementation, not just S3.
|
|
5
|
-
*
|
|
6
|
-
* A `gs://` or Azure client hits the same three problems as the S3 one: the
|
|
7
|
-
* string is not a URL, it is a URL for a different provider, or it addresses no
|
|
8
|
-
* bucket. Each carries the provider-specific detail as a field, so the type is
|
|
9
|
-
* common while the specifics stay accurate.
|
|
10
|
-
*
|
|
11
|
-
* Messages state the rule, which is constant, and never interpolate the value.
|
|
12
|
-
* An interpolated message cannot be matched on, reads differently every time it
|
|
13
|
-
* is thrown, and carries user input into every log line that touches it.
|
|
14
|
-
*/
|
|
15
|
-
/** Base for anything wrong with a storage URL, so callers may catch broadly. */
|
|
16
|
-
export declare abstract class StorageUrlError extends Error {
|
|
17
|
-
/**
|
|
18
|
-
* The URL that could not be used, with any credentials it carried replaced
|
|
19
|
-
* by `REDACTED` — an error is logged, and a key in its userinfo would be
|
|
20
|
-
* logged with it.
|
|
21
|
-
*/
|
|
22
|
-
readonly url: string;
|
|
23
|
-
constructor(url: string, message: string);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Replace a URL's userinfo with `REDACTED`, so `s3://KEY:SECRET@uploads` can be
|
|
27
|
-
* logged as `s3://REDACTED@uploads`. A URL without userinfo comes back as is.
|
|
28
|
-
*
|
|
29
|
-
* A string the URL parser rejects is still redacted, up to its last `@`: an
|
|
30
|
-
* unencoded `/` in a secret is the commonest way to write a malformed one, and
|
|
31
|
-
* that string is exactly the one that ends up in an error.
|
|
32
|
-
*/
|
|
33
|
-
export declare function redactStorageUrl(url: string): string;
|
|
34
|
-
/** The string is not a URL at all. */
|
|
35
|
-
export declare class MalformedStorageUrl extends StorageUrlError {
|
|
36
|
-
constructor(url: string);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* A URL, but for a different provider — a `gs://` URL reaching the S3 client,
|
|
40
|
-
* or the reverse. The usual cause is a construct resolving against the wrong
|
|
41
|
-
* target, which is worth distinguishing from a malformed string.
|
|
42
|
-
*/
|
|
43
|
-
export declare class UnexpectedStorageScheme extends StorageUrlError {
|
|
44
|
-
/** The scheme this client handles, e.g. `'s3:'`. */
|
|
45
|
-
readonly expected: string;
|
|
46
|
-
/** The scheme the URL actually carried, e.g. `'gs:'`. */
|
|
47
|
-
readonly actual: string;
|
|
48
|
-
constructor(url: string, expected: string, actual: string);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* A well-formed URL that addresses no bucket.
|
|
52
|
-
*
|
|
53
|
-
* "Bucket" is the term S3 and GCS share; Azure calls the same thing a
|
|
54
|
-
* container. One error covers all three — the word in the message is the
|
|
55
|
-
* common one rather than a new abstraction nobody uses.
|
|
56
|
-
*/
|
|
57
|
-
export declare class MissingStorageBucket extends StorageUrlError {
|
|
58
|
-
constructor(url: string);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* The URL's userinfo carries one half of a key pair: an access key with no
|
|
62
|
-
* secret, or a secret with no access key. Either both are in the URL, or
|
|
63
|
-
* neither is and the provider's default credential chain applies — half a pair
|
|
64
|
-
* is never silently completed from the environment.
|
|
65
|
-
*/
|
|
66
|
-
export declare class IncompleteStorageCredentials extends StorageUrlError {
|
|
67
|
-
/** Which half is missing: `'accessKeyId'` or `'secretAccessKey'`. */
|
|
68
|
-
readonly missing: 'accessKeyId' | 'secretAccessKey';
|
|
69
|
-
constructor(url: string, missing: 'accessKeyId' | 'secretAccessKey');
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* No driver is registered for the URL's scheme.
|
|
73
|
-
*
|
|
74
|
-
* Usually means the build pinned a driver for a different target, or an entry
|
|
75
|
-
* point registered nothing at all — so the registered schemes are carried
|
|
76
|
-
* alongside, since "what *is* available" is the actionable half.
|
|
77
|
-
*/
|
|
78
|
-
export declare class UnregisteredStorageScheme extends StorageUrlError {
|
|
79
|
-
/** The scheme that had no driver, e.g. `'gs:'`. */
|
|
80
|
-
readonly scheme: string;
|
|
81
|
-
/** What is registered, so the caller can see what was pinned instead. */
|
|
82
|
-
readonly registered: readonly string[];
|
|
83
|
-
constructor(url: string, scheme: string, registered: readonly string[]);
|
|
84
|
-
}
|
|
85
|
-
//#endregion
|
|
86
|
-
export { type StorageClient, type StorageDriver, createStorageClient, registerStorageDriver, registeredStorageSchemes };
|
|
87
|
-
//# sourceMappingURL=index.d.mts.map
|
|
1
|
+
import { StorageClient } from "./StorageClient-DH4tP-Mf.mjs";
|
|
2
|
+
export { StorageClient };
|
package/dist/index.mjs
CHANGED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { a as UnexpectedStorageScheme, i as StorageUrlError, n as MalformedStorageUrl, o as UnregisteredStorageScheme, r as MissingStorageBucket, s as redactStorageUrl, t as IncompleteStorageCredentials } from "./errors-COXs1iNg.mjs";
|
|
2
|
-
//#region src/registry.ts
|
|
3
|
-
/**
|
|
4
|
-
* Driver registry — how a storage URL becomes a client without the caller
|
|
5
|
-
* naming a cloud.
|
|
6
|
-
*
|
|
7
|
-
* A construct declares that it needs object storage and receives one URL. It
|
|
8
|
-
* must not import `AmazonStorageClient` to use it: that would name a provider in
|
|
9
|
-
* the neutral layer and drag the AWS SDK into an app that only ever talks to
|
|
10
|
-
* GCS. So the protocol selects the driver, and whoever assembles the
|
|
11
|
-
* application decides which drivers exist — a generated handler registering the
|
|
12
|
-
* one its target needs, or a dev entry registering several.
|
|
13
|
-
*
|
|
14
|
-
* Registration is explicit rather than an import side effect, because a
|
|
15
|
-
* side-effecting module is exactly what a bundler is entitled to drop.
|
|
16
|
-
*/
|
|
17
|
-
const drivers = /* @__PURE__ */ new Map();
|
|
18
|
-
/** Make a driver available to {@link createStorageClient}. Idempotent. */
|
|
19
|
-
function registerStorageDriver(driver) {
|
|
20
|
-
drivers.set(driver.scheme, driver);
|
|
21
|
-
}
|
|
22
|
-
/** Which schemes are currently registered — the useful half of a failure. */
|
|
23
|
-
function registeredStorageSchemes() {
|
|
24
|
-
return [...drivers.keys()].sort();
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Build a client for a URL.
|
|
28
|
-
*
|
|
29
|
-
* @throws {UnregisteredStorageScheme} when no driver handles the scheme, which
|
|
30
|
-
* in practice means the build pinned a different one, or nothing registered.
|
|
31
|
-
*/
|
|
32
|
-
function createStorageClient(url) {
|
|
33
|
-
const scheme = schemeOf(url);
|
|
34
|
-
const driver = drivers.get(scheme);
|
|
35
|
-
if (!driver) throw new UnregisteredStorageScheme(url, scheme, registeredStorageSchemes());
|
|
36
|
-
return driver.create(url);
|
|
37
|
-
}
|
|
38
|
-
function schemeOf(url) {
|
|
39
|
-
const separator = url.indexOf(":");
|
|
40
|
-
return separator === -1 ? "" : url.slice(0, separator + 1);
|
|
41
|
-
}
|
|
42
|
-
//#endregion
|
|
43
|
-
export { IncompleteStorageCredentials, MalformedStorageUrl, MissingStorageBucket, StorageUrlError, UnexpectedStorageScheme, UnregisteredStorageScheme, createStorageClient, redactStorageUrl, registerStorageDriver, registeredStorageSchemes };
|
|
44
|
-
|
|
45
|
-
//# sourceMappingURL=index.mjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/storage",
|
|
3
|
-
"version": "10.0.0
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -31,30 +31,15 @@
|
|
|
31
31
|
"types": "./dist/index.d.cts",
|
|
32
32
|
"default": "./dist/index.cjs"
|
|
33
33
|
}
|
|
34
|
-
},
|
|
35
|
-
"./s3-url": {
|
|
36
|
-
"import": {
|
|
37
|
-
"types": "./dist/s3Url.d.mts",
|
|
38
|
-
"default": "./dist/s3Url.mjs"
|
|
39
|
-
},
|
|
40
|
-
"require": {
|
|
41
|
-
"types": "./dist/s3Url.d.cts",
|
|
42
|
-
"default": "./dist/s3Url.cjs"
|
|
43
|
-
}
|
|
44
34
|
}
|
|
45
35
|
},
|
|
46
|
-
"
|
|
47
|
-
"dist"
|
|
48
|
-
],
|
|
49
|
-
"dependencies": {
|
|
50
|
-
"@opentelemetry/api": "~1.9.1"
|
|
51
|
-
},
|
|
36
|
+
"dependencies": {},
|
|
52
37
|
"peerDependencies": {
|
|
53
|
-
"zod": "
|
|
54
|
-
"@aws-sdk/client-s3": "
|
|
55
|
-
"@aws-sdk/s3-presigned-post": "
|
|
56
|
-
"@aws-sdk/s3-request-presigner": "
|
|
57
|
-
"@geekmidas/cache": "^10.0.0
|
|
38
|
+
"zod": "~4.1.13",
|
|
39
|
+
"@aws-sdk/client-s3": "~3.844.0",
|
|
40
|
+
"@aws-sdk/s3-presigned-post": "^3.844.0",
|
|
41
|
+
"@aws-sdk/s3-request-presigner": "~3.844.0",
|
|
42
|
+
"@geekmidas/cache": "^10.0.0"
|
|
58
43
|
},
|
|
59
44
|
"peerDependenciesMeta": {
|
|
60
45
|
"@geekmidas/cache": {
|
|
@@ -74,10 +59,7 @@
|
|
|
74
59
|
}
|
|
75
60
|
},
|
|
76
61
|
"devDependencies": {
|
|
77
|
-
"@
|
|
78
|
-
"@geekmidas/cache": "^10.0.0-alpha.99"
|
|
62
|
+
"@geekmidas/cache": "^10.0.0"
|
|
79
63
|
},
|
|
80
|
-
"scripts": {
|
|
81
|
-
"build": "tsdown --config ../../tsdown.config.ts --filter ."
|
|
82
|
-
}
|
|
64
|
+
"scripts": {}
|
|
83
65
|
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeleteObjectCommand,
|
|
3
|
+
GetObjectCommand,
|
|
4
|
+
ListObjectVersionsCommand,
|
|
5
|
+
PutObjectCommand,
|
|
6
|
+
S3Client,
|
|
7
|
+
} from '@aws-sdk/client-s3';
|
|
8
|
+
import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
|
|
9
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
10
|
+
|
|
11
|
+
import type { Cache } from '@geekmidas/cache';
|
|
12
|
+
import {
|
|
13
|
+
type DocumentVersion,
|
|
14
|
+
type File,
|
|
15
|
+
type GetUploadParams,
|
|
16
|
+
type GetUploadResponse,
|
|
17
|
+
type StorageClient,
|
|
18
|
+
StorageProvider,
|
|
19
|
+
} from './StorageClient';
|
|
20
|
+
|
|
21
|
+
export class AmazonStorageClient implements StorageClient {
|
|
22
|
+
readonly provider = StorageProvider.AWSS3;
|
|
23
|
+
|
|
24
|
+
static create(
|
|
25
|
+
options: AmazonStorageClientCreateOptions,
|
|
26
|
+
): AmazonStorageClient {
|
|
27
|
+
const {
|
|
28
|
+
bucket,
|
|
29
|
+
region,
|
|
30
|
+
accessKeyId,
|
|
31
|
+
acl,
|
|
32
|
+
endpoint,
|
|
33
|
+
secretAccessKey,
|
|
34
|
+
forcePathStyle = false,
|
|
35
|
+
cache,
|
|
36
|
+
} = options;
|
|
37
|
+
const hasCredentials = accessKeyId && secretAccessKey;
|
|
38
|
+
const credentials = hasCredentials
|
|
39
|
+
? { accessKeyId, secretAccessKey }
|
|
40
|
+
: undefined;
|
|
41
|
+
|
|
42
|
+
const client = new S3Client({
|
|
43
|
+
region,
|
|
44
|
+
credentials,
|
|
45
|
+
endpoint,
|
|
46
|
+
forcePathStyle,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return new AmazonStorageClient(client, bucket, acl, cache);
|
|
50
|
+
}
|
|
51
|
+
constructor(
|
|
52
|
+
private readonly client: S3Client,
|
|
53
|
+
private readonly bucket: string,
|
|
54
|
+
private readonly acl = AmazonCannedAccessControlList.AuthenticatedRead,
|
|
55
|
+
readonly cache?: Cache,
|
|
56
|
+
) {}
|
|
57
|
+
|
|
58
|
+
private createGetObjectCommand(
|
|
59
|
+
file: File,
|
|
60
|
+
overrides?: { VersionId?: string },
|
|
61
|
+
): GetObjectCommand {
|
|
62
|
+
const disposition =
|
|
63
|
+
file.disposition ?? (file.name ? 'attachment' : undefined);
|
|
64
|
+
const ResponseContentDisposition = disposition
|
|
65
|
+
? file.name
|
|
66
|
+
? `${disposition}; filename=${encodeURIComponent(file.name)}`
|
|
67
|
+
: disposition
|
|
68
|
+
: undefined;
|
|
69
|
+
|
|
70
|
+
return new GetObjectCommand({
|
|
71
|
+
Bucket: this.bucket,
|
|
72
|
+
Key: file.path,
|
|
73
|
+
ResponseContentDisposition,
|
|
74
|
+
ResponseContentType: file.responseContentType,
|
|
75
|
+
...overrides,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getVersionDownloadURL(file: File, versionId: string): Promise<string> {
|
|
80
|
+
const command = this.createGetObjectCommand(file, { VersionId: versionId });
|
|
81
|
+
return getSignedUrl(this.client, command, { expiresIn: 60 * 60 * 24 });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async getVersions(key: string): Promise<DocumentVersion[]> {
|
|
85
|
+
const command = new ListObjectVersionsCommand({
|
|
86
|
+
Bucket: this.bucket,
|
|
87
|
+
Prefix: key,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const { Versions = [] } = await this.client.send(command);
|
|
91
|
+
|
|
92
|
+
return Versions.map((version) => ({
|
|
93
|
+
id: version.VersionId || '',
|
|
94
|
+
createdAt: version.LastModified || new Date(),
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async getDownloadURL(file: File, expiresIn = 60 * 60): Promise<string> {
|
|
99
|
+
const cacheKey = `download-url:${file.path}`;
|
|
100
|
+
const cachedURL = await this.cache?.get<string>(cacheKey);
|
|
101
|
+
|
|
102
|
+
if (cachedURL) {
|
|
103
|
+
return cachedURL;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const command = this.createGetObjectCommand(file);
|
|
107
|
+
const url = await getSignedUrl(this.client, command, { expiresIn });
|
|
108
|
+
const cacheExpiration = Math.max(expiresIn - 60, 0);
|
|
109
|
+
|
|
110
|
+
if (cacheExpiration) {
|
|
111
|
+
await this.cache?.set(cacheKey, url, cacheExpiration);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return url;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async getUploadURL(
|
|
118
|
+
params: GetUploadParams,
|
|
119
|
+
expiresIn = 60 * 60,
|
|
120
|
+
): Promise<string> {
|
|
121
|
+
const command = new PutObjectCommand({
|
|
122
|
+
Bucket: this.bucket,
|
|
123
|
+
Key: params.path,
|
|
124
|
+
ContentType: params.contentType,
|
|
125
|
+
ContentLength: params.contentLength,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return getSignedUrl(this.client, command, { expiresIn });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async getUpload(
|
|
132
|
+
params: GetUploadParams,
|
|
133
|
+
expiresIn = 5,
|
|
134
|
+
): Promise<GetUploadResponse> {
|
|
135
|
+
const { path } = params;
|
|
136
|
+
const { fields: values, url } = await createPresignedPost(this.client, {
|
|
137
|
+
Expires: expiresIn * 60,
|
|
138
|
+
Bucket: this.bucket,
|
|
139
|
+
Fields: {
|
|
140
|
+
acl: this.acl,
|
|
141
|
+
},
|
|
142
|
+
Conditions: [
|
|
143
|
+
// content length restrictions: 0-1MB]
|
|
144
|
+
// ['content-length-range', 0, contentLength],
|
|
145
|
+
// specify content-type to be more generic- images only
|
|
146
|
+
// ['starts-with', '$Content-Type', 'image/'],
|
|
147
|
+
// ['starts-with', '$Content-Type', contentType],
|
|
148
|
+
],
|
|
149
|
+
Key: path,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const keys = Object.keys(values);
|
|
153
|
+
const fields = keys.map((key) => ({ key, value: values[key] || '' }));
|
|
154
|
+
|
|
155
|
+
return { url, fields };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async upload(
|
|
159
|
+
key: string,
|
|
160
|
+
data: string | Buffer,
|
|
161
|
+
contentType: string,
|
|
162
|
+
): Promise<void> {
|
|
163
|
+
const Body = typeof data === 'string' ? Buffer.from(data, 'base64') : data;
|
|
164
|
+
|
|
165
|
+
const params = {
|
|
166
|
+
Bucket: this.bucket,
|
|
167
|
+
Key: key,
|
|
168
|
+
Body,
|
|
169
|
+
ContentType: contentType,
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const command = new PutObjectCommand(params);
|
|
173
|
+
|
|
174
|
+
await this.client.send(command);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async delete(key: string): Promise<void> {
|
|
178
|
+
const command = new DeleteObjectCommand({
|
|
179
|
+
Bucket: this.bucket,
|
|
180
|
+
Key: key,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
await this.client.send(command);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export enum AmazonCannedAccessControlList {
|
|
188
|
+
AuthenticatedRead = 'authenticated-read',
|
|
189
|
+
Private = 'private',
|
|
190
|
+
PublicRead = 'public-read',
|
|
191
|
+
PublicReadWrite = 'public-read-write',
|
|
192
|
+
AwsExecRead = 'aws-exec-read',
|
|
193
|
+
BucketOwnerRead = 'bucket-owner-read',
|
|
194
|
+
BucketOwnerFullControl = 'bucket-owner-full-control',
|
|
195
|
+
LogDeliveryWrite = 'log-delivery-write',
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
interface AmazonStorageClientCreateOptions {
|
|
199
|
+
bucket: string;
|
|
200
|
+
region?: string;
|
|
201
|
+
acl?: AmazonCannedAccessControlList;
|
|
202
|
+
accessKeyId?: string;
|
|
203
|
+
secretAccessKey?: string;
|
|
204
|
+
endpoint?: string;
|
|
205
|
+
forcePathStyle?: boolean;
|
|
206
|
+
cache?: Cache;
|
|
207
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { Cache } from '@geekmidas/cache';
|
|
2
|
+
export interface DocumentVersion {
|
|
3
|
+
id: string;
|
|
4
|
+
createdAt: Date;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export enum StorageProvider {
|
|
8
|
+
AWSS3 = 'geekimdas.toolbox.storage.aws.s3',
|
|
9
|
+
GCP = 'geekimdas.toolbox.storage.gcp',
|
|
10
|
+
AZURE = 'geekimdas.toolbox.storage.azure',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface StorageClient {
|
|
14
|
+
readonly provider: StorageProvider;
|
|
15
|
+
readonly cache?: Cache;
|
|
16
|
+
/**
|
|
17
|
+
* Get a URL to upload a file to.
|
|
18
|
+
*
|
|
19
|
+
* @param params - The parameters to get the upload URL for.
|
|
20
|
+
* @param expiresIn - The number of minutes the URL should be valid for.
|
|
21
|
+
* Defaults to 60 * 60 (1 hour).
|
|
22
|
+
* @returns A promise that resolves to the upload URL.
|
|
23
|
+
*/
|
|
24
|
+
getUploadURL(params: GetUploadParams, expiresIn?: number): Promise<string>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get a URL to download a file from.
|
|
28
|
+
*
|
|
29
|
+
* @param file - The file to get the download URL for.
|
|
30
|
+
* @param expiresIn - The number of seconds the URL should be valid for.
|
|
31
|
+
* Defaults to 60 * 60 (1 hour).
|
|
32
|
+
* @returns A promise that resolves to the download URL.
|
|
33
|
+
*/
|
|
34
|
+
getDownloadURL(file: File, expiresIn?: number): Promise<string>;
|
|
35
|
+
/**
|
|
36
|
+
* Get the versions for a key.
|
|
37
|
+
*
|
|
38
|
+
* @param key - The key to get the versions for.
|
|
39
|
+
*/
|
|
40
|
+
getVersions(key: string): Promise<DocumentVersion[]>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get a URL to download a version of a file from.
|
|
44
|
+
*
|
|
45
|
+
* @param file - The file to get the download URL for.
|
|
46
|
+
* @param versionId - The version ID to get the download URL for.
|
|
47
|
+
*/
|
|
48
|
+
getVersionDownloadURL(file: File, versionId: string): Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
*
|
|
51
|
+
* @param key - The key to upload the file to.
|
|
52
|
+
* @param data - The data to upload.
|
|
53
|
+
* @param contentType - The content type of the data.
|
|
54
|
+
*/
|
|
55
|
+
upload(
|
|
56
|
+
key: string,
|
|
57
|
+
data: string | Buffer,
|
|
58
|
+
contentType: string,
|
|
59
|
+
): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Get a URL to upload a file to.
|
|
62
|
+
*
|
|
63
|
+
* @param params - The parameters to get the upload URL for.
|
|
64
|
+
* @param expiresIn - The number of minutes the URL should be valid for.
|
|
65
|
+
*/
|
|
66
|
+
getUpload(
|
|
67
|
+
params: GetUploadParams,
|
|
68
|
+
expiresIn?: number,
|
|
69
|
+
): Promise<GetUploadResponse>;
|
|
70
|
+
/**
|
|
71
|
+
* Delete a file from storage.
|
|
72
|
+
*
|
|
73
|
+
* @param key - The key (path) of the file to delete.
|
|
74
|
+
*/
|
|
75
|
+
delete(key: string): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface GetUploadParams {
|
|
79
|
+
path: string;
|
|
80
|
+
contentType: string;
|
|
81
|
+
contentLength: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type UploadField = {
|
|
85
|
+
key: string;
|
|
86
|
+
value: string;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export type GetUploadResponse = {
|
|
90
|
+
url: string;
|
|
91
|
+
fields: UploadField[];
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export interface File {
|
|
95
|
+
name?: string;
|
|
96
|
+
path: string;
|
|
97
|
+
/**
|
|
98
|
+
* Override the Content-Type of the response.
|
|
99
|
+
* Useful when the stored object's content type differs from what the browser should receive.
|
|
100
|
+
*/
|
|
101
|
+
responseContentType?: string;
|
|
102
|
+
/**
|
|
103
|
+
* Control how the browser handles the file.
|
|
104
|
+
* - `'attachment'` — prompts download (default when `name` is set)
|
|
105
|
+
* - `'inline'` — displays in the browser
|
|
106
|
+
*/
|
|
107
|
+
disposition?: 'inline' | 'attachment';
|
|
108
|
+
}
|