@geekmidas/storage 10.0.0-alpha.18 → 10.0.0-alpha.181

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.
Files changed (44) hide show
  1. package/README.md +65 -13
  2. package/dist/aws.cjs +88 -10
  3. package/dist/aws.cjs.map +1 -1
  4. package/dist/aws.d.cts +5 -2
  5. package/dist/aws.d.cts.map +1 -1
  6. package/dist/aws.d.mts +5 -2
  7. package/dist/aws.d.mts.map +1 -1
  8. package/dist/aws.mjs +88 -10
  9. package/dist/aws.mjs.map +1 -1
  10. package/dist/{errors-Bexjg-j-.cjs → errors-BB8jdHif.cjs} +52 -3
  11. package/dist/errors-BB8jdHif.cjs.map +1 -0
  12. package/dist/{errors-CpYcpJCY.mjs → errors-COXs1iNg.mjs} +41 -4
  13. package/dist/errors-COXs1iNg.mjs.map +1 -0
  14. package/dist/events.cjs +98 -0
  15. package/dist/events.cjs.map +1 -0
  16. package/dist/events.d.cts +58 -0
  17. package/dist/events.d.cts.map +1 -0
  18. package/dist/events.d.mts +58 -0
  19. package/dist/events.d.mts.map +1 -0
  20. package/dist/events.mjs +94 -0
  21. package/dist/events.mjs.map +1 -0
  22. package/dist/index.cjs +3 -1
  23. package/dist/index.d.cts +27 -3
  24. package/dist/index.d.cts.map +1 -1
  25. package/dist/index.d.mts +27 -3
  26. package/dist/index.d.mts.map +1 -1
  27. package/dist/index.mjs +2 -2
  28. package/dist/{registry-ClEvHGpM.d.cts → registry-DyBOtv6n.d.cts} +44 -2
  29. package/dist/registry-DyBOtv6n.d.cts.map +1 -0
  30. package/dist/{registry-ClEvHGpM.d.mts → registry-DyBOtv6n.d.mts} +44 -2
  31. package/dist/registry-DyBOtv6n.d.mts.map +1 -0
  32. package/dist/s3Url.cjs +61 -9
  33. package/dist/s3Url.cjs.map +1 -1
  34. package/dist/s3Url.d.cts +34 -7
  35. package/dist/s3Url.d.cts.map +1 -1
  36. package/dist/s3Url.d.mts +34 -7
  37. package/dist/s3Url.d.mts.map +1 -1
  38. package/dist/s3Url.mjs +61 -10
  39. package/dist/s3Url.mjs.map +1 -1
  40. package/package.json +20 -5
  41. package/dist/errors-Bexjg-j-.cjs.map +0 -1
  42. package/dist/errors-CpYcpJCY.mjs.map +0 -1
  43. package/dist/registry-ClEvHGpM.d.cts.map +0 -1
  44. package/dist/registry-ClEvHGpM.d.mts.map +0 -1
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { i as registeredStorageSchemes, l as StorageClient, n as createStorageClient, r as registerStorageDriver, t as StorageDriver } from "./registry-ClEvHGpM.mjs";
1
+ import { d as UploadPost, f as UploadPostParams, i as registeredStorageSchemes, l as StorageClient, n as createStorageClient, r as registerStorageDriver, t as StorageDriver } from "./registry-DyBOtv6n.mjs";
2
2
  //#region src/errors.d.ts
3
3
  /**
4
4
  * Storage errors — shared by every implementation, not just S3.
@@ -14,10 +14,23 @@ import { i as registeredStorageSchemes, l as StorageClient, n as createStorageCl
14
14
  */
15
15
  /** Base for anything wrong with a storage URL, so callers may catch broadly. */
16
16
  export declare abstract class StorageUrlError extends Error {
17
- /** The URL that could not be used. */
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
+ */
18
22
  readonly url: string;
19
23
  constructor(url: string, message: string);
20
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;
21
34
  /** The string is not a URL at all. */
22
35
  export declare class MalformedStorageUrl extends StorageUrlError {
23
36
  constructor(url: string);
@@ -44,6 +57,17 @@ export declare class UnexpectedStorageScheme extends StorageUrlError {
44
57
  export declare class MissingStorageBucket extends StorageUrlError {
45
58
  constructor(url: string);
46
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
+ }
47
71
  /**
48
72
  * No driver is registered for the URL's scheme.
49
73
  *
@@ -59,5 +83,5 @@ export declare class UnregisteredStorageScheme extends StorageUrlError {
59
83
  constructor(url: string, scheme: string, registered: readonly string[]);
60
84
  }
61
85
  //#endregion
62
- export { type StorageClient, type StorageDriver, createStorageClient, registerStorageDriver, registeredStorageSchemes };
86
+ export { type StorageClient, type StorageDriver, type UploadPost, type UploadPostParams, createStorageClient, registerStorageDriver, registeredStorageSchemes };
63
87
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":";;;;;;;;;;;;;;;8BAcsB,wBAAwB;;WAEpC;EAET,YAAY,aAAa;;;qBAQb,4BAA4B;EACxC,YAAY;;;;;;;qBAUA,gCAAgC;;WAEnC;;WAEA;EAET,YAAY,aAAa,kBAAkB;;;;;;;;;qBAc/B,6BAA6B;EACzC,YAAY;;;;;;;;;qBAYA,kCAAkC;;WAErC;;WAEA;EAET,YAAY,aAAa,gBAAgB"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":";;;;;;;;;;;;;;;8BAcsB,wBAAwB;;;;;;WAMpC;EAET,YAAY,aAAa;;;;;;;;;;wBAeV,iBAAiB;;qBAapB,4BAA4B;EACxC,YAAY;;;;;;;qBAUA,gCAAgC;;WAEnC;;WAEA;EAET,YAAY,aAAa,kBAAkB;;;;;;;;;qBAc/B,6BAA6B;EACzC,YAAY;;;;;;;;qBAWA,qCAAqC;;WAExC;EAET,YAAY,aAAa;;;;;;;;;qBAgBb,kCAAkC;;WAErC;;WAEA;EAET,YAAY,aAAa,gBAAgB"}
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as UnregisteredStorageScheme, i as UnexpectedStorageScheme, n as MissingStorageBucket, r as StorageUrlError, t as MalformedStorageUrl } from "./errors-CpYcpJCY.mjs";
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
2
  //#region src/registry.ts
3
3
  /**
4
4
  * Driver registry — how a storage URL becomes a client without the caller
@@ -40,6 +40,6 @@ function schemeOf(url) {
40
40
  return separator === -1 ? "" : url.slice(0, separator + 1);
41
41
  }
42
42
  //#endregion
43
- export { MalformedStorageUrl, MissingStorageBucket, StorageUrlError, UnexpectedStorageScheme, UnregisteredStorageScheme, createStorageClient, registerStorageDriver, registeredStorageSchemes };
43
+ export { IncompleteStorageCredentials, MalformedStorageUrl, MissingStorageBucket, StorageUrlError, UnexpectedStorageScheme, UnregisteredStorageScheme, createStorageClient, redactStorageUrl, registerStorageDriver, registeredStorageSchemes };
44
44
 
45
45
  //# sourceMappingURL=index.mjs.map
@@ -57,6 +57,17 @@ interface StorageClient {
57
57
  * @param expiresIn - The number of minutes the URL should be valid for.
58
58
  */
59
59
  getUpload(params: GetUploadParams, expiresIn?: number): Promise<GetUploadResponse>;
60
+ /**
61
+ * A signed form a browser posts one file to, straight into the bucket.
62
+ *
63
+ * The policy is the guard: the storage refuses a file larger than
64
+ * `maxSize`, or of a type the policy does not allow, so nothing the client
65
+ * sends past this call is trusted to the server that issued it.
66
+ *
67
+ * @param params - What may be uploaded, and where.
68
+ * @returns The form's action and fields, the key, and when it expires.
69
+ */
70
+ getUploadPost(params: UploadPostParams): Promise<UploadPost>;
60
71
  /**
61
72
  * Delete a file from storage.
62
73
  *
@@ -64,6 +75,37 @@ interface StorageClient {
64
75
  */
65
76
  delete(key: string): Promise<void>;
66
77
  }
78
+ /** What a signed upload form allows. */
79
+ interface UploadPostParams {
80
+ /** Where the object lands. */
81
+ key: string;
82
+ /** The largest file the storage accepts, in bytes. */
83
+ maxSize: number;
84
+ /**
85
+ * The type the file must be sent as: an exact `Content-Type`, fixed in the
86
+ * form's fields — or `{ startsWith }`, which the client sets itself
87
+ * (`image/` for any image). Absent, any type is accepted.
88
+ */
89
+ contentType?: string | {
90
+ startsWith: string;
91
+ };
92
+ /** How long the form is valid, in seconds. Defaults to 300. */
93
+ expiresIn?: number;
94
+ }
95
+ /**
96
+ * A signed upload form. Post it as `multipart/form-data`: every field, then
97
+ * the file last, as `file`.
98
+ */
99
+ interface UploadPost {
100
+ /** The form's action — where the browser posts. */
101
+ url: string;
102
+ /** Every field the post must carry, as signed. */
103
+ fields: Record<string, string>;
104
+ /** Where the object lands. */
105
+ key: string;
106
+ /** When the signature stops being accepted, as an ISO timestamp. */
107
+ expiresAt: string;
108
+ }
67
109
  interface GetUploadParams {
68
110
  path: string;
69
111
  contentType: string;
@@ -117,5 +159,5 @@ declare function registeredStorageSchemes(): string[];
117
159
  */
118
160
  declare function createStorageClient(url: string): StorageClient;
119
161
  //#endregion
120
- export { DocumentVersion as a, GetUploadResponse as c, registeredStorageSchemes as i, StorageClient as l, createStorageClient as n, File as o, registerStorageDriver as r, GetUploadParams as s, StorageDriver as t, StorageProvider as u };
121
- //# sourceMappingURL=registry-ClEvHGpM.d.cts.map
162
+ export { DocumentVersion as a, GetUploadResponse as c, UploadPost as d, UploadPostParams as f, registeredStorageSchemes as i, StorageClient as l, createStorageClient as n, File as o, registerStorageDriver as r, GetUploadParams as s, StorageDriver as t, StorageProvider as u };
163
+ //# sourceMappingURL=registry-DyBOtv6n.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry-DyBOtv6n.d.cts","names":[],"sources":["../src/StorageClient.ts","../src/registry.ts"],"mappings":";;UACiB;EAChB;EACA,WAAW;;aAGA;EACX;EACA;EACA;;UAGgB;WACP,UAAU;WACV,QAAQ;;;;;;;;;EASjB,aAAa,QAAQ,iBAAiB,qBAAqB;;;;;;;;;EAU3D,eAAe,MAAM,MAAM,qBAAqB;;;;;;EAMhD,YAAY,cAAc,QAAQ;;;;;;;EAQlC,sBAAsB,MAAM,MAAM,oBAAoB;;;;;;;EAOtD,OACC,aACA,eAAe,QACf,sBACE;;;;;;;EAOH,UACC,QAAQ,iBACR,qBACE,QAAQ;;;;;;;;;;;EAWX,cAAc,QAAQ,mBAAmB,QAAQ;;;;;;EAMjD,OAAO,cAAc;;;UAIL;;EAEhB;;EAEA;;;;;;EAMA;IAAyB;;;EAEzB;;;;;;UAOgB;;EAEhB;;EAEA,QAAQ;;EAER;;EAEA;;UAGgB;EAChB;EACA;EACA;;KAGW;EACX;EACA;;KAGW;EACX;EACA,QAAQ;;UAGQ;EAChB;EACA;;;;;EAKA;;;;;;EAMA;;;;;;;;;;UC5HgB;;WAEP;EACT,OAAO,cAAc;;;iBAMN,sBAAsB,QAAQ;;iBAK9B;;;;;;;iBAUA,oBAAoB,cAAc"}
@@ -57,6 +57,17 @@ interface StorageClient {
57
57
  * @param expiresIn - The number of minutes the URL should be valid for.
58
58
  */
59
59
  getUpload(params: GetUploadParams, expiresIn?: number): Promise<GetUploadResponse>;
60
+ /**
61
+ * A signed form a browser posts one file to, straight into the bucket.
62
+ *
63
+ * The policy is the guard: the storage refuses a file larger than
64
+ * `maxSize`, or of a type the policy does not allow, so nothing the client
65
+ * sends past this call is trusted to the server that issued it.
66
+ *
67
+ * @param params - What may be uploaded, and where.
68
+ * @returns The form's action and fields, the key, and when it expires.
69
+ */
70
+ getUploadPost(params: UploadPostParams): Promise<UploadPost>;
60
71
  /**
61
72
  * Delete a file from storage.
62
73
  *
@@ -64,6 +75,37 @@ interface StorageClient {
64
75
  */
65
76
  delete(key: string): Promise<void>;
66
77
  }
78
+ /** What a signed upload form allows. */
79
+ interface UploadPostParams {
80
+ /** Where the object lands. */
81
+ key: string;
82
+ /** The largest file the storage accepts, in bytes. */
83
+ maxSize: number;
84
+ /**
85
+ * The type the file must be sent as: an exact `Content-Type`, fixed in the
86
+ * form's fields — or `{ startsWith }`, which the client sets itself
87
+ * (`image/` for any image). Absent, any type is accepted.
88
+ */
89
+ contentType?: string | {
90
+ startsWith: string;
91
+ };
92
+ /** How long the form is valid, in seconds. Defaults to 300. */
93
+ expiresIn?: number;
94
+ }
95
+ /**
96
+ * A signed upload form. Post it as `multipart/form-data`: every field, then
97
+ * the file last, as `file`.
98
+ */
99
+ interface UploadPost {
100
+ /** The form's action — where the browser posts. */
101
+ url: string;
102
+ /** Every field the post must carry, as signed. */
103
+ fields: Record<string, string>;
104
+ /** Where the object lands. */
105
+ key: string;
106
+ /** When the signature stops being accepted, as an ISO timestamp. */
107
+ expiresAt: string;
108
+ }
67
109
  interface GetUploadParams {
68
110
  path: string;
69
111
  contentType: string;
@@ -117,5 +159,5 @@ declare function registeredStorageSchemes(): string[];
117
159
  */
118
160
  declare function createStorageClient(url: string): StorageClient;
119
161
  //#endregion
120
- export { DocumentVersion as a, GetUploadResponse as c, registeredStorageSchemes as i, StorageClient as l, createStorageClient as n, File as o, registerStorageDriver as r, GetUploadParams as s, StorageDriver as t, StorageProvider as u };
121
- //# sourceMappingURL=registry-ClEvHGpM.d.mts.map
162
+ export { DocumentVersion as a, GetUploadResponse as c, UploadPost as d, UploadPostParams as f, registeredStorageSchemes as i, StorageClient as l, createStorageClient as n, File as o, registerStorageDriver as r, GetUploadParams as s, StorageDriver as t, StorageProvider as u };
163
+ //# sourceMappingURL=registry-DyBOtv6n.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry-DyBOtv6n.d.mts","names":[],"sources":["../src/StorageClient.ts","../src/registry.ts"],"mappings":";;UACiB;EAChB;EACA,WAAW;;aAGA;EACX;EACA;EACA;;UAGgB;WACP,UAAU;WACV,QAAQ;;;;;;;;;EASjB,aAAa,QAAQ,iBAAiB,qBAAqB;;;;;;;;;EAU3D,eAAe,MAAM,MAAM,qBAAqB;;;;;;EAMhD,YAAY,cAAc,QAAQ;;;;;;;EAQlC,sBAAsB,MAAM,MAAM,oBAAoB;;;;;;;EAOtD,OACC,aACA,eAAe,QACf,sBACE;;;;;;;EAOH,UACC,QAAQ,iBACR,qBACE,QAAQ;;;;;;;;;;;EAWX,cAAc,QAAQ,mBAAmB,QAAQ;;;;;;EAMjD,OAAO,cAAc;;;UAIL;;EAEhB;;EAEA;;;;;;EAMA;IAAyB;;;EAEzB;;;;;;UAOgB;;EAEhB;;EAEA,QAAQ;;EAER;;EAEA;;UAGgB;EAChB;EACA;EACA;;KAGW;EACX;EACA;;KAGW;EACX;EACA,QAAQ;;UAGQ;EAChB;EACA;;;;;EAKA;;;;;;EAMA;;;;;;;;;;UC5HgB;;WAEP;EACT,OAAO,cAAc;;;iBAMN,sBAAsB,QAAQ;;iBAK9B;;;;;;;iBAUA,oBAAoB,cAAc"}
package/dist/s3Url.cjs CHANGED
@@ -11,7 +11,7 @@ var __exportAll = (all, no_symbols) => {
11
11
  return target;
12
12
  };
13
13
  //#endregion
14
- const require_errors = require("./errors-Bexjg-j-.cjs");
14
+ const require_errors = require("./errors-BB8jdHif.cjs");
15
15
  //#region src/s3Url.ts
16
16
  /**
17
17
  * The `s3://` URL codec — how an S3 bucket is addressed as a single string.
@@ -26,27 +26,36 @@ const require_errors = require("./errors-Bexjg-j-.cjs");
26
26
  */
27
27
  var s3Url_exports = /* @__PURE__ */ __exportAll({
28
28
  build: () => build,
29
- parse: () => parse
29
+ parse: () => parse,
30
+ signedOrigin: () => signedOrigin
30
31
  });
31
32
  const SCHEME = "s3:";
32
33
  /**
33
34
  * Compose an address into a URL.
34
35
  *
35
- * Credentials never appear. The AWS SDK resolves them from its own chain — an
36
- * execution role when deployed, `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`
37
- * locally — so the URL stays safe to log and identical across environments that
38
- * differ only in who is calling.
36
+ * Credentials appear only when the address carries them, percent-encoded —
37
+ * AWS secrets routinely contain `/` and `+`. An address without them builds a
38
+ * URL with no userinfo, which leaves the caller to the SDK's own chain.
39
39
  */
40
40
  function build(address) {
41
- const { bucket, region, endpoint, forcePathStyle } = address;
41
+ const { bucket, region, endpoint, forcePathStyle, accessKeyId, secretAccessKey } = address;
42
42
  if (!bucket) throw new require_errors.MissingStorageBucket("");
43
43
  const url = new URL(`${SCHEME}//${bucket}`);
44
+ if (accessKeyId || secretAccessKey) {
45
+ if (!accessKeyId) throw new require_errors.IncompleteStorageCredentials(url.toString(), "accessKeyId");
46
+ if (!secretAccessKey) throw new require_errors.IncompleteStorageCredentials(url.toString(), "secretAccessKey");
47
+ url.username = encodeURIComponent(accessKeyId);
48
+ url.password = encodeURIComponent(secretAccessKey);
49
+ }
44
50
  if (region) url.searchParams.set("region", region);
45
51
  if (endpoint) url.searchParams.set("endpoint", endpoint);
46
52
  if (forcePathStyle) url.searchParams.set("forcePathStyle", "true");
47
53
  return url.toString();
48
54
  }
49
- /** Parse a URL back into an address. Throws if it is not an `s3://` URL. */
55
+ /**
56
+ * Parse a URL back into an address. Throws if it is not an `s3://` URL, or if
57
+ * its userinfo carries only one half of a key pair.
58
+ */
50
59
  function parse(url) {
51
60
  let parsed;
52
61
  try {
@@ -60,13 +69,55 @@ function parse(url) {
60
69
  const region = parsed.searchParams.get("region") ?? void 0;
61
70
  const endpoint = parsed.searchParams.get("endpoint") ?? void 0;
62
71
  const forcePathStyle = parsed.searchParams.get("forcePathStyle") === "true" ? true : void 0;
72
+ const credentials = credentialsOf(url, parsed);
63
73
  return {
64
74
  bucket,
65
75
  ...region ? { region } : {},
66
76
  ...endpoint ? { endpoint } : {},
67
- ...forcePathStyle ? { forcePathStyle } : {}
77
+ ...forcePathStyle ? { forcePathStyle } : {},
78
+ ...credentials
79
+ };
80
+ }
81
+ /** The URL's userinfo as a key pair: both halves, or nothing. */
82
+ function credentialsOf(url, parsed) {
83
+ const accessKeyId = decode(url, parsed.username);
84
+ const secretAccessKey = decode(url, parsed.password);
85
+ if (!accessKeyId && !secretAccessKey) return {};
86
+ if (!accessKeyId) throw new require_errors.IncompleteStorageCredentials(url, "accessKeyId");
87
+ if (!secretAccessKey) throw new require_errors.IncompleteStorageCredentials(url, "secretAccessKey");
88
+ return {
89
+ accessKeyId,
90
+ secretAccessKey
68
91
  };
69
92
  }
93
+ function decode(url, value) {
94
+ try {
95
+ return decodeURIComponent(value);
96
+ } catch {
97
+ throw new require_errors.MalformedStorageUrl(url);
98
+ }
99
+ }
100
+ /**
101
+ * The scheme and host a presigned link to one of the bucket's objects is on —
102
+ * what a page showing private objects through signed links has to allow
103
+ * (`next/image`'s `remotePatterns`, a CSP's `img-src`).
104
+ *
105
+ * The SDK's own rule: path-style against an endpoint (MinIO) is the endpoint;
106
+ * virtual-host style puts the bucket in front of the endpoint's host, or of
107
+ * S3's regional one. Carries no credentials, so it is public.
108
+ *
109
+ * `undefined` for an S3 bucket whose URL names no region: the SDK takes it
110
+ * from the process, so the host is not knowable from the URL alone.
111
+ */
112
+ function signedOrigin(address) {
113
+ const { bucket, region, endpoint, forcePathStyle } = address;
114
+ if (endpoint) {
115
+ const url = new URL(endpoint);
116
+ return forcePathStyle ? url.origin : `${url.protocol}//${bucket}.${url.host}`;
117
+ }
118
+ if (!region) return void 0;
119
+ return `https://${bucket}.s3.${region}.amazonaws.com`;
120
+ }
70
121
  //#endregion
71
122
  exports.build = build;
72
123
  exports.parse = parse;
@@ -76,5 +127,6 @@ Object.defineProperty(exports, "s3Url_exports", {
76
127
  return s3Url_exports;
77
128
  }
78
129
  });
130
+ exports.signedOrigin = signedOrigin;
79
131
 
80
132
  //# sourceMappingURL=s3Url.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"s3Url.cjs","names":["MissingStorageBucket","MalformedStorageUrl","UnexpectedStorageScheme"],"sources":["../src/s3Url.ts"],"sourcesContent":["/**\n * The `s3://` URL codec — how an S3 bucket is addressed as a single string.\n *\n * It lives beside the client rather than in a neutral package because `bucket`,\n * `region`, and `forcePathStyle` are S3's vocabulary. The neutral layer only\n * knows that a construct provides *one URL*; what that URL says is between the\n * component that composes it and the client that parses it. A `gs://` codec\n * would sit beside its own client and share nothing with this one.\n *\n * Both directions live here so they cannot drift: `parse(build(x))` is `x`.\n */\n\nimport {\n\tMalformedStorageUrl,\n\tMissingStorageBucket,\n\tUnexpectedStorageScheme,\n} from './errors';\n\n/** What an S3 URL addresses. Credentials are deliberately absent — see below. */\nexport interface S3Address {\n\tbucket: string;\n\t/** Read off the bucket, never inherited: a bucket may live in another region. */\n\tregion?: string;\n\t/** Set for S3-compatible backends such as MinIO. */\n\tendpoint?: string;\n\t/** MinIO and most S3-compatible servers need path-style addressing. */\n\tforcePathStyle?: boolean;\n}\n\nconst SCHEME = 's3:';\n\n/**\n * Compose an address into a URL.\n *\n * Credentials never appear. The AWS SDK resolves them from its own chain — an\n * execution role when deployed, `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`\n * locally — so the URL stays safe to log and identical across environments that\n * differ only in who is calling.\n */\nexport function build(address: S3Address): string {\n\tconst { bucket, region, endpoint, forcePathStyle } = address;\n\tif (!bucket) throw new MissingStorageBucket('');\n\n\tconst url = new URL(`${SCHEME}//${bucket}`);\n\tif (region) url.searchParams.set('region', region);\n\tif (endpoint) url.searchParams.set('endpoint', endpoint);\n\tif (forcePathStyle) url.searchParams.set('forcePathStyle', 'true');\n\treturn url.toString();\n}\n\n/** Parse a URL back into an address. Throws if it is not an `s3://` URL. */\nexport function parse(url: string): S3Address {\n\tlet parsed: URL;\n\ttry {\n\t\tparsed = new URL(url);\n\t} catch {\n\t\tthrow new MalformedStorageUrl(url);\n\t}\n\n\tif (parsed.protocol !== SCHEME) {\n\t\tthrow new UnexpectedStorageScheme(url, SCHEME, parsed.protocol);\n\t}\n\n\tconst bucket = parsed.hostname;\n\tif (!bucket) throw new MissingStorageBucket(url);\n\n\tconst region = parsed.searchParams.get('region') ?? undefined;\n\tconst endpoint = parsed.searchParams.get('endpoint') ?? undefined;\n\tconst forcePathStyle =\n\t\tparsed.searchParams.get('forcePathStyle') === 'true' ? true : undefined;\n\n\treturn {\n\t\tbucket,\n\t\t...(region ? { region } : {}),\n\t\t...(endpoint ? { endpoint } : {}),\n\t\t...(forcePathStyle ? { forcePathStyle } : {}),\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAM,SAAS;;;;;;;;;AAUf,SAAgB,MAAM,SAA4B;CACjD,MAAM,EAAE,QAAQ,QAAQ,UAAU,mBAAmB;CACrD,IAAI,CAAC,QAAQ,MAAM,IAAIA,eAAAA,qBAAqB,EAAE;CAE9C,MAAM,MAAM,IAAI,IAAI,GAAG,OAAO,IAAI,QAAQ;CAC1C,IAAI,QAAQ,IAAI,aAAa,IAAI,UAAU,MAAM;CACjD,IAAI,UAAU,IAAI,aAAa,IAAI,YAAY,QAAQ;CACvD,IAAI,gBAAgB,IAAI,aAAa,IAAI,kBAAkB,MAAM;CACjE,OAAO,IAAI,SAAS;AACrB;;AAGA,SAAgB,MAAM,KAAwB;CAC7C,IAAI;CACJ,IAAI;EACH,SAAS,IAAI,IAAI,GAAG;CACrB,QAAQ;EACP,MAAM,IAAIC,eAAAA,oBAAoB,GAAG;CAClC;CAEA,IAAI,OAAO,aAAa,QACvB,MAAM,IAAIC,eAAAA,wBAAwB,KAAK,QAAQ,OAAO,QAAQ;CAG/D,MAAM,SAAS,OAAO;CACtB,IAAI,CAAC,QAAQ,MAAM,IAAIF,eAAAA,qBAAqB,GAAG;CAE/C,MAAM,SAAS,OAAO,aAAa,IAAI,QAAQ,KAAK,KAAA;CACpD,MAAM,WAAW,OAAO,aAAa,IAAI,UAAU,KAAK,KAAA;CACxD,MAAM,iBACL,OAAO,aAAa,IAAI,gBAAgB,MAAM,SAAS,OAAO,KAAA;CAE/D,OAAO;EACN;EACA,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;EAC3B,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;EAC/B,GAAI,iBAAiB,EAAE,eAAe,IAAI,CAAC;CAC5C;AACD"}
1
+ {"version":3,"file":"s3Url.cjs","names":["MissingStorageBucket","IncompleteStorageCredentials","MalformedStorageUrl","UnexpectedStorageScheme"],"sources":["../src/s3Url.ts"],"sourcesContent":["/**\n * The `s3://` URL codec — how an S3 bucket is addressed as a single string.\n *\n * It lives beside the client rather than in a neutral package because `bucket`,\n * `region`, and `forcePathStyle` are S3's vocabulary. The neutral layer only\n * knows that a construct provides *one URL*; what that URL says is between the\n * component that composes it and the client that parses it. A `gs://` codec\n * would sit beside its own client and share nothing with this one.\n *\n * Both directions live here so they cannot drift: `parse(build(x))` is `x`.\n */\n\nimport {\n\tIncompleteStorageCredentials,\n\tMalformedStorageUrl,\n\tMissingStorageBucket,\n\tUnexpectedStorageScheme,\n} from './errors';\n\n/**\n * What an S3 URL addresses, and optionally who it calls as.\n *\n * Credentials are optional and come as a pair. With them, the URL is a key\n * scoped to this one bucket (`s3://KEY:SECRET@uploads`); without them the AWS\n * SDK resolves its own chain — an execution role when deployed,\n * `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` otherwise. The URL's pair wins\n * over that chain.\n */\nexport interface S3Address {\n\tbucket: string;\n\t/** Read off the bucket, never inherited: a bucket may live in another region. */\n\tregion?: string;\n\t/** Set for S3-compatible backends such as MinIO. */\n\tendpoint?: string;\n\t/** MinIO and most S3-compatible servers need path-style addressing. */\n\tforcePathStyle?: boolean;\n\t/** The URL's own access key; always paired with `secretAccessKey`. */\n\taccessKeyId?: string;\n\t/** The URL's own secret; always paired with `accessKeyId`. */\n\tsecretAccessKey?: string;\n}\n\nconst SCHEME = 's3:';\n\n/**\n * Compose an address into a URL.\n *\n * Credentials appear only when the address carries them, percent-encoded —\n * AWS secrets routinely contain `/` and `+`. An address without them builds a\n * URL with no userinfo, which leaves the caller to the SDK's own chain.\n */\nexport function build(address: S3Address): string {\n\tconst {\n\t\tbucket,\n\t\tregion,\n\t\tendpoint,\n\t\tforcePathStyle,\n\t\taccessKeyId,\n\t\tsecretAccessKey,\n\t} = address;\n\tif (!bucket) throw new MissingStorageBucket('');\n\n\tconst url = new URL(`${SCHEME}//${bucket}`);\n\tif (accessKeyId || secretAccessKey) {\n\t\tif (!accessKeyId) {\n\t\t\tthrow new IncompleteStorageCredentials(url.toString(), 'accessKeyId');\n\t\t}\n\t\tif (!secretAccessKey) {\n\t\t\tthrow new IncompleteStorageCredentials(url.toString(), 'secretAccessKey');\n\t\t}\n\t\turl.username = encodeURIComponent(accessKeyId);\n\t\turl.password = encodeURIComponent(secretAccessKey);\n\t}\n\tif (region) url.searchParams.set('region', region);\n\tif (endpoint) url.searchParams.set('endpoint', endpoint);\n\tif (forcePathStyle) url.searchParams.set('forcePathStyle', 'true');\n\treturn url.toString();\n}\n\n/**\n * Parse a URL back into an address. Throws if it is not an `s3://` URL, or if\n * its userinfo carries only one half of a key pair.\n */\nexport function parse(url: string): S3Address {\n\tlet parsed: URL;\n\ttry {\n\t\tparsed = new URL(url);\n\t} catch {\n\t\tthrow new MalformedStorageUrl(url);\n\t}\n\n\tif (parsed.protocol !== SCHEME) {\n\t\tthrow new UnexpectedStorageScheme(url, SCHEME, parsed.protocol);\n\t}\n\n\tconst bucket = parsed.hostname;\n\tif (!bucket) throw new MissingStorageBucket(url);\n\n\tconst region = parsed.searchParams.get('region') ?? undefined;\n\tconst endpoint = parsed.searchParams.get('endpoint') ?? undefined;\n\tconst forcePathStyle =\n\t\tparsed.searchParams.get('forcePathStyle') === 'true' ? true : undefined;\n\tconst credentials = credentialsOf(url, parsed);\n\n\treturn {\n\t\tbucket,\n\t\t...(region ? { region } : {}),\n\t\t...(endpoint ? { endpoint } : {}),\n\t\t...(forcePathStyle ? { forcePathStyle } : {}),\n\t\t...credentials,\n\t};\n}\n\n/** The URL's userinfo as a key pair: both halves, or nothing. */\nfunction credentialsOf(\n\turl: string,\n\tparsed: URL,\n): Pick<S3Address, 'accessKeyId' | 'secretAccessKey'> {\n\tconst accessKeyId = decode(url, parsed.username);\n\tconst secretAccessKey = decode(url, parsed.password);\n\tif (!accessKeyId && !secretAccessKey) return {};\n\tif (!accessKeyId) {\n\t\tthrow new IncompleteStorageCredentials(url, 'accessKeyId');\n\t}\n\tif (!secretAccessKey) {\n\t\tthrow new IncompleteStorageCredentials(url, 'secretAccessKey');\n\t}\n\treturn { accessKeyId, secretAccessKey };\n}\n\nfunction decode(url: string, value: string): string {\n\ttry {\n\t\treturn decodeURIComponent(value);\n\t} catch {\n\t\tthrow new MalformedStorageUrl(url);\n\t}\n}\n\n/**\n * The scheme and host a presigned link to one of the bucket's objects is on —\n * what a page showing private objects through signed links has to allow\n * (`next/image`'s `remotePatterns`, a CSP's `img-src`).\n *\n * The SDK's own rule: path-style against an endpoint (MinIO) is the endpoint;\n * virtual-host style puts the bucket in front of the endpoint's host, or of\n * S3's regional one. Carries no credentials, so it is public.\n *\n * `undefined` for an S3 bucket whose URL names no region: the SDK takes it\n * from the process, so the host is not knowable from the URL alone.\n */\nexport function signedOrigin(address: S3Address): string | undefined {\n\tconst { bucket, region, endpoint, forcePathStyle } = address;\n\tif (endpoint) {\n\t\tconst url = new URL(endpoint);\n\t\treturn forcePathStyle\n\t\t\t? url.origin\n\t\t\t: `${url.protocol}//${bucket}.${url.host}`;\n\t}\n\tif (!region) return undefined;\n\treturn `https://${bucket}.s3.${region}.amazonaws.com`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,MAAM,SAAS;;;;;;;;AASf,SAAgB,MAAM,SAA4B;CACjD,MAAM,EACL,QACA,QACA,UACA,gBACA,aACA,oBACG;CACJ,IAAI,CAAC,QAAQ,MAAM,IAAIA,eAAAA,qBAAqB,EAAE;CAE9C,MAAM,MAAM,IAAI,IAAI,GAAG,OAAO,IAAI,QAAQ;CAC1C,IAAI,eAAe,iBAAiB;EACnC,IAAI,CAAC,aACJ,MAAM,IAAIC,eAAAA,6BAA6B,IAAI,SAAS,GAAG,aAAa;EAErE,IAAI,CAAC,iBACJ,MAAM,IAAIA,eAAAA,6BAA6B,IAAI,SAAS,GAAG,iBAAiB;EAEzE,IAAI,WAAW,mBAAmB,WAAW;EAC7C,IAAI,WAAW,mBAAmB,eAAe;CAClD;CACA,IAAI,QAAQ,IAAI,aAAa,IAAI,UAAU,MAAM;CACjD,IAAI,UAAU,IAAI,aAAa,IAAI,YAAY,QAAQ;CACvD,IAAI,gBAAgB,IAAI,aAAa,IAAI,kBAAkB,MAAM;CACjE,OAAO,IAAI,SAAS;AACrB;;;;;AAMA,SAAgB,MAAM,KAAwB;CAC7C,IAAI;CACJ,IAAI;EACH,SAAS,IAAI,IAAI,GAAG;CACrB,QAAQ;EACP,MAAM,IAAIC,eAAAA,oBAAoB,GAAG;CAClC;CAEA,IAAI,OAAO,aAAa,QACvB,MAAM,IAAIC,eAAAA,wBAAwB,KAAK,QAAQ,OAAO,QAAQ;CAG/D,MAAM,SAAS,OAAO;CACtB,IAAI,CAAC,QAAQ,MAAM,IAAIH,eAAAA,qBAAqB,GAAG;CAE/C,MAAM,SAAS,OAAO,aAAa,IAAI,QAAQ,KAAK,KAAA;CACpD,MAAM,WAAW,OAAO,aAAa,IAAI,UAAU,KAAK,KAAA;CACxD,MAAM,iBACL,OAAO,aAAa,IAAI,gBAAgB,MAAM,SAAS,OAAO,KAAA;CAC/D,MAAM,cAAc,cAAc,KAAK,MAAM;CAE7C,OAAO;EACN;EACA,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;EAC3B,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;EAC/B,GAAI,iBAAiB,EAAE,eAAe,IAAI,CAAC;EAC3C,GAAG;CACJ;AACD;;AAGA,SAAS,cACR,KACA,QACqD;CACrD,MAAM,cAAc,OAAO,KAAK,OAAO,QAAQ;CAC/C,MAAM,kBAAkB,OAAO,KAAK,OAAO,QAAQ;CACnD,IAAI,CAAC,eAAe,CAAC,iBAAiB,OAAO,CAAC;CAC9C,IAAI,CAAC,aACJ,MAAM,IAAIC,eAAAA,6BAA6B,KAAK,aAAa;CAE1D,IAAI,CAAC,iBACJ,MAAM,IAAIA,eAAAA,6BAA6B,KAAK,iBAAiB;CAE9D,OAAO;EAAE;EAAa;CAAgB;AACvC;AAEA,SAAS,OAAO,KAAa,OAAuB;CACnD,IAAI;EACH,OAAO,mBAAmB,KAAK;CAChC,QAAQ;EACP,MAAM,IAAIC,eAAAA,oBAAoB,GAAG;CAClC;AACD;;;;;;;;;;;;;AAcA,SAAgB,aAAa,SAAwC;CACpE,MAAM,EAAE,QAAQ,QAAQ,UAAU,mBAAmB;CACrD,IAAI,UAAU;EACb,MAAM,MAAM,IAAI,IAAI,QAAQ;EAC5B,OAAO,iBACJ,IAAI,SACJ,GAAG,IAAI,SAAS,IAAI,OAAO,GAAG,IAAI;CACtC;CACA,IAAI,CAAC,QAAQ,OAAO,KAAA;CACpB,OAAO,WAAW,OAAO,MAAM,OAAO;AACvC"}
package/dist/s3Url.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  declare namespace s3Url_d_exports {
2
- export { S3Address, build, parse };
2
+ export { S3Address, build, parse, signedOrigin };
3
3
  }
4
4
  /**
5
5
  * The `s3://` URL codec — how an S3 bucket is addressed as a single string.
@@ -12,7 +12,15 @@ declare namespace s3Url_d_exports {
12
12
  *
13
13
  * Both directions live here so they cannot drift: `parse(build(x))` is `x`.
14
14
  */
15
- /** What an S3 URL addresses. Credentials are deliberately absent — see below. */
15
+ /**
16
+ * What an S3 URL addresses, and optionally who it calls as.
17
+ *
18
+ * Credentials are optional and come as a pair. With them, the URL is a key
19
+ * scoped to this one bucket (`s3://KEY:SECRET@uploads`); without them the AWS
20
+ * SDK resolves its own chain — an execution role when deployed,
21
+ * `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` otherwise. The URL's pair wins
22
+ * over that chain.
23
+ */
16
24
  export interface S3Address {
17
25
  bucket: string;
18
26
  /** Read off the bucket, never inherited: a bucket may live in another region. */
@@ -21,18 +29,37 @@ export interface S3Address {
21
29
  endpoint?: string;
22
30
  /** MinIO and most S3-compatible servers need path-style addressing. */
23
31
  forcePathStyle?: boolean;
32
+ /** The URL's own access key; always paired with `secretAccessKey`. */
33
+ accessKeyId?: string;
34
+ /** The URL's own secret; always paired with `accessKeyId`. */
35
+ secretAccessKey?: string;
24
36
  }
25
37
  /**
26
38
  * Compose an address into a URL.
27
39
  *
28
- * Credentials never appear. The AWS SDK resolves them from its own chain — an
29
- * execution role when deployed, `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`
30
- * locally — so the URL stays safe to log and identical across environments that
31
- * differ only in who is calling.
40
+ * Credentials appear only when the address carries them, percent-encoded —
41
+ * AWS secrets routinely contain `/` and `+`. An address without them builds a
42
+ * URL with no userinfo, which leaves the caller to the SDK's own chain.
32
43
  */
33
44
  export declare function build(address: S3Address): string;
34
- /** Parse a URL back into an address. Throws if it is not an `s3://` URL. */
45
+ /**
46
+ * Parse a URL back into an address. Throws if it is not an `s3://` URL, or if
47
+ * its userinfo carries only one half of a key pair.
48
+ */
35
49
  export declare function parse(url: string): S3Address;
50
+ /**
51
+ * The scheme and host a presigned link to one of the bucket's objects is on —
52
+ * what a page showing private objects through signed links has to allow
53
+ * (`next/image`'s `remotePatterns`, a CSP's `img-src`).
54
+ *
55
+ * The SDK's own rule: path-style against an endpoint (MinIO) is the endpoint;
56
+ * virtual-host style puts the bucket in front of the endpoint's host, or of
57
+ * S3's regional one. Carries no credentials, so it is public.
58
+ *
59
+ * `undefined` for an S3 bucket whose URL names no region: the SDK takes it
60
+ * from the process, so the host is not knowable from the URL alone.
61
+ */
62
+ export declare function signedOrigin(address: S3Address): string | undefined;
36
63
  //#endregion
37
64
  export { s3Url_d_exports as t };
38
65
  //# sourceMappingURL=s3Url.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"s3Url.d.cts","names":[],"sources":["../src/s3Url.ts"],"mappings":";;;;;;;;;;;;;;;iBAmBiB;EAChB;;EAEA;;EAEA;;EAEA;;;;;;;;;;wBAae,MAAM,SAAS;;wBAYf,MAAM,cAAc"}
1
+ {"version":3,"file":"s3Url.d.cts","names":[],"sources":["../src/s3Url.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;iBA4BiB;EAChB;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;;;;;;;;wBAYe,MAAM,SAAS;;;;;wBAgCf,MAAM,cAAc;;;;;;;;;;;;;wBAmEpB,aAAa,SAAS"}
package/dist/s3Url.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  declare namespace s3Url_d_exports {
2
- export { S3Address, build, parse };
2
+ export { S3Address, build, parse, signedOrigin };
3
3
  }
4
4
  /**
5
5
  * The `s3://` URL codec — how an S3 bucket is addressed as a single string.
@@ -12,7 +12,15 @@ declare namespace s3Url_d_exports {
12
12
  *
13
13
  * Both directions live here so they cannot drift: `parse(build(x))` is `x`.
14
14
  */
15
- /** What an S3 URL addresses. Credentials are deliberately absent — see below. */
15
+ /**
16
+ * What an S3 URL addresses, and optionally who it calls as.
17
+ *
18
+ * Credentials are optional and come as a pair. With them, the URL is a key
19
+ * scoped to this one bucket (`s3://KEY:SECRET@uploads`); without them the AWS
20
+ * SDK resolves its own chain — an execution role when deployed,
21
+ * `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` otherwise. The URL's pair wins
22
+ * over that chain.
23
+ */
16
24
  export interface S3Address {
17
25
  bucket: string;
18
26
  /** Read off the bucket, never inherited: a bucket may live in another region. */
@@ -21,18 +29,37 @@ export interface S3Address {
21
29
  endpoint?: string;
22
30
  /** MinIO and most S3-compatible servers need path-style addressing. */
23
31
  forcePathStyle?: boolean;
32
+ /** The URL's own access key; always paired with `secretAccessKey`. */
33
+ accessKeyId?: string;
34
+ /** The URL's own secret; always paired with `accessKeyId`. */
35
+ secretAccessKey?: string;
24
36
  }
25
37
  /**
26
38
  * Compose an address into a URL.
27
39
  *
28
- * Credentials never appear. The AWS SDK resolves them from its own chain — an
29
- * execution role when deployed, `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`
30
- * locally — so the URL stays safe to log and identical across environments that
31
- * differ only in who is calling.
40
+ * Credentials appear only when the address carries them, percent-encoded —
41
+ * AWS secrets routinely contain `/` and `+`. An address without them builds a
42
+ * URL with no userinfo, which leaves the caller to the SDK's own chain.
32
43
  */
33
44
  export declare function build(address: S3Address): string;
34
- /** Parse a URL back into an address. Throws if it is not an `s3://` URL. */
45
+ /**
46
+ * Parse a URL back into an address. Throws if it is not an `s3://` URL, or if
47
+ * its userinfo carries only one half of a key pair.
48
+ */
35
49
  export declare function parse(url: string): S3Address;
50
+ /**
51
+ * The scheme and host a presigned link to one of the bucket's objects is on —
52
+ * what a page showing private objects through signed links has to allow
53
+ * (`next/image`'s `remotePatterns`, a CSP's `img-src`).
54
+ *
55
+ * The SDK's own rule: path-style against an endpoint (MinIO) is the endpoint;
56
+ * virtual-host style puts the bucket in front of the endpoint's host, or of
57
+ * S3's regional one. Carries no credentials, so it is public.
58
+ *
59
+ * `undefined` for an S3 bucket whose URL names no region: the SDK takes it
60
+ * from the process, so the host is not knowable from the URL alone.
61
+ */
62
+ export declare function signedOrigin(address: S3Address): string | undefined;
36
63
  //#endregion
37
64
  export { s3Url_d_exports as t };
38
65
  //# sourceMappingURL=s3Url.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"s3Url.d.mts","names":[],"sources":["../src/s3Url.ts"],"mappings":";;;;;;;;;;;;;;;iBAmBiB;EAChB;;EAEA;;EAEA;;EAEA;;;;;;;;;;wBAae,MAAM,SAAS;;wBAYf,MAAM,cAAc"}
1
+ {"version":3,"file":"s3Url.d.mts","names":[],"sources":["../src/s3Url.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;iBA4BiB;EAChB;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;;;;;;;;wBAYe,MAAM,SAAS;;;;;wBAgCf,MAAM,cAAc;;;;;;;;;;;;;wBAmEpB,aAAa,SAAS"}
package/dist/s3Url.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
- import { i as UnexpectedStorageScheme, n as MissingStorageBucket, t as MalformedStorageUrl } from "./errors-CpYcpJCY.mjs";
2
+ import { a as UnexpectedStorageScheme, n as MalformedStorageUrl, r as MissingStorageBucket, t as IncompleteStorageCredentials } from "./errors-COXs1iNg.mjs";
3
3
  //#region src/s3Url.ts
4
4
  /**
5
5
  * The `s3://` URL codec — how an S3 bucket is addressed as a single string.
@@ -14,27 +14,36 @@ import { i as UnexpectedStorageScheme, n as MissingStorageBucket, t as Malformed
14
14
  */
15
15
  var s3Url_exports = /* @__PURE__ */ __exportAll({
16
16
  build: () => build,
17
- parse: () => parse
17
+ parse: () => parse,
18
+ signedOrigin: () => signedOrigin
18
19
  });
19
20
  const SCHEME = "s3:";
20
21
  /**
21
22
  * Compose an address into a URL.
22
23
  *
23
- * Credentials never appear. The AWS SDK resolves them from its own chain — an
24
- * execution role when deployed, `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`
25
- * locally — so the URL stays safe to log and identical across environments that
26
- * differ only in who is calling.
24
+ * Credentials appear only when the address carries them, percent-encoded —
25
+ * AWS secrets routinely contain `/` and `+`. An address without them builds a
26
+ * URL with no userinfo, which leaves the caller to the SDK's own chain.
27
27
  */
28
28
  function build(address) {
29
- const { bucket, region, endpoint, forcePathStyle } = address;
29
+ const { bucket, region, endpoint, forcePathStyle, accessKeyId, secretAccessKey } = address;
30
30
  if (!bucket) throw new MissingStorageBucket("");
31
31
  const url = new URL(`${SCHEME}//${bucket}`);
32
+ if (accessKeyId || secretAccessKey) {
33
+ if (!accessKeyId) throw new IncompleteStorageCredentials(url.toString(), "accessKeyId");
34
+ if (!secretAccessKey) throw new IncompleteStorageCredentials(url.toString(), "secretAccessKey");
35
+ url.username = encodeURIComponent(accessKeyId);
36
+ url.password = encodeURIComponent(secretAccessKey);
37
+ }
32
38
  if (region) url.searchParams.set("region", region);
33
39
  if (endpoint) url.searchParams.set("endpoint", endpoint);
34
40
  if (forcePathStyle) url.searchParams.set("forcePathStyle", "true");
35
41
  return url.toString();
36
42
  }
37
- /** Parse a URL back into an address. Throws if it is not an `s3://` URL. */
43
+ /**
44
+ * Parse a URL back into an address. Throws if it is not an `s3://` URL, or if
45
+ * its userinfo carries only one half of a key pair.
46
+ */
38
47
  function parse(url) {
39
48
  let parsed;
40
49
  try {
@@ -48,14 +57,56 @@ function parse(url) {
48
57
  const region = parsed.searchParams.get("region") ?? void 0;
49
58
  const endpoint = parsed.searchParams.get("endpoint") ?? void 0;
50
59
  const forcePathStyle = parsed.searchParams.get("forcePathStyle") === "true" ? true : void 0;
60
+ const credentials = credentialsOf(url, parsed);
51
61
  return {
52
62
  bucket,
53
63
  ...region ? { region } : {},
54
64
  ...endpoint ? { endpoint } : {},
55
- ...forcePathStyle ? { forcePathStyle } : {}
65
+ ...forcePathStyle ? { forcePathStyle } : {},
66
+ ...credentials
67
+ };
68
+ }
69
+ /** The URL's userinfo as a key pair: both halves, or nothing. */
70
+ function credentialsOf(url, parsed) {
71
+ const accessKeyId = decode(url, parsed.username);
72
+ const secretAccessKey = decode(url, parsed.password);
73
+ if (!accessKeyId && !secretAccessKey) return {};
74
+ if (!accessKeyId) throw new IncompleteStorageCredentials(url, "accessKeyId");
75
+ if (!secretAccessKey) throw new IncompleteStorageCredentials(url, "secretAccessKey");
76
+ return {
77
+ accessKeyId,
78
+ secretAccessKey
56
79
  };
57
80
  }
81
+ function decode(url, value) {
82
+ try {
83
+ return decodeURIComponent(value);
84
+ } catch {
85
+ throw new MalformedStorageUrl(url);
86
+ }
87
+ }
88
+ /**
89
+ * The scheme and host a presigned link to one of the bucket's objects is on —
90
+ * what a page showing private objects through signed links has to allow
91
+ * (`next/image`'s `remotePatterns`, a CSP's `img-src`).
92
+ *
93
+ * The SDK's own rule: path-style against an endpoint (MinIO) is the endpoint;
94
+ * virtual-host style puts the bucket in front of the endpoint's host, or of
95
+ * S3's regional one. Carries no credentials, so it is public.
96
+ *
97
+ * `undefined` for an S3 bucket whose URL names no region: the SDK takes it
98
+ * from the process, so the host is not knowable from the URL alone.
99
+ */
100
+ function signedOrigin(address) {
101
+ const { bucket, region, endpoint, forcePathStyle } = address;
102
+ if (endpoint) {
103
+ const url = new URL(endpoint);
104
+ return forcePathStyle ? url.origin : `${url.protocol}//${bucket}.${url.host}`;
105
+ }
106
+ if (!region) return void 0;
107
+ return `https://${bucket}.s3.${region}.amazonaws.com`;
108
+ }
58
109
  //#endregion
59
- export { build, parse, s3Url_exports as t };
110
+ export { build, parse, signedOrigin, s3Url_exports as t };
60
111
 
61
112
  //# sourceMappingURL=s3Url.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"s3Url.mjs","names":[],"sources":["../src/s3Url.ts"],"sourcesContent":["/**\n * The `s3://` URL codec — how an S3 bucket is addressed as a single string.\n *\n * It lives beside the client rather than in a neutral package because `bucket`,\n * `region`, and `forcePathStyle` are S3's vocabulary. The neutral layer only\n * knows that a construct provides *one URL*; what that URL says is between the\n * component that composes it and the client that parses it. A `gs://` codec\n * would sit beside its own client and share nothing with this one.\n *\n * Both directions live here so they cannot drift: `parse(build(x))` is `x`.\n */\n\nimport {\n\tMalformedStorageUrl,\n\tMissingStorageBucket,\n\tUnexpectedStorageScheme,\n} from './errors';\n\n/** What an S3 URL addresses. Credentials are deliberately absent — see below. */\nexport interface S3Address {\n\tbucket: string;\n\t/** Read off the bucket, never inherited: a bucket may live in another region. */\n\tregion?: string;\n\t/** Set for S3-compatible backends such as MinIO. */\n\tendpoint?: string;\n\t/** MinIO and most S3-compatible servers need path-style addressing. */\n\tforcePathStyle?: boolean;\n}\n\nconst SCHEME = 's3:';\n\n/**\n * Compose an address into a URL.\n *\n * Credentials never appear. The AWS SDK resolves them from its own chain — an\n * execution role when deployed, `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`\n * locally — so the URL stays safe to log and identical across environments that\n * differ only in who is calling.\n */\nexport function build(address: S3Address): string {\n\tconst { bucket, region, endpoint, forcePathStyle } = address;\n\tif (!bucket) throw new MissingStorageBucket('');\n\n\tconst url = new URL(`${SCHEME}//${bucket}`);\n\tif (region) url.searchParams.set('region', region);\n\tif (endpoint) url.searchParams.set('endpoint', endpoint);\n\tif (forcePathStyle) url.searchParams.set('forcePathStyle', 'true');\n\treturn url.toString();\n}\n\n/** Parse a URL back into an address. Throws if it is not an `s3://` URL. */\nexport function parse(url: string): S3Address {\n\tlet parsed: URL;\n\ttry {\n\t\tparsed = new URL(url);\n\t} catch {\n\t\tthrow new MalformedStorageUrl(url);\n\t}\n\n\tif (parsed.protocol !== SCHEME) {\n\t\tthrow new UnexpectedStorageScheme(url, SCHEME, parsed.protocol);\n\t}\n\n\tconst bucket = parsed.hostname;\n\tif (!bucket) throw new MissingStorageBucket(url);\n\n\tconst region = parsed.searchParams.get('region') ?? undefined;\n\tconst endpoint = parsed.searchParams.get('endpoint') ?? undefined;\n\tconst forcePathStyle =\n\t\tparsed.searchParams.get('forcePathStyle') === 'true' ? true : undefined;\n\n\treturn {\n\t\tbucket,\n\t\t...(region ? { region } : {}),\n\t\t...(endpoint ? { endpoint } : {}),\n\t\t...(forcePathStyle ? { forcePathStyle } : {}),\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA6BA,MAAM,SAAS;;;;;;;;;AAUf,SAAgB,MAAM,SAA4B;CACjD,MAAM,EAAE,QAAQ,QAAQ,UAAU,mBAAmB;CACrD,IAAI,CAAC,QAAQ,MAAM,IAAI,qBAAqB,EAAE;CAE9C,MAAM,MAAM,IAAI,IAAI,GAAG,OAAO,IAAI,QAAQ;CAC1C,IAAI,QAAQ,IAAI,aAAa,IAAI,UAAU,MAAM;CACjD,IAAI,UAAU,IAAI,aAAa,IAAI,YAAY,QAAQ;CACvD,IAAI,gBAAgB,IAAI,aAAa,IAAI,kBAAkB,MAAM;CACjE,OAAO,IAAI,SAAS;AACrB;;AAGA,SAAgB,MAAM,KAAwB;CAC7C,IAAI;CACJ,IAAI;EACH,SAAS,IAAI,IAAI,GAAG;CACrB,QAAQ;EACP,MAAM,IAAI,oBAAoB,GAAG;CAClC;CAEA,IAAI,OAAO,aAAa,QACvB,MAAM,IAAI,wBAAwB,KAAK,QAAQ,OAAO,QAAQ;CAG/D,MAAM,SAAS,OAAO;CACtB,IAAI,CAAC,QAAQ,MAAM,IAAI,qBAAqB,GAAG;CAE/C,MAAM,SAAS,OAAO,aAAa,IAAI,QAAQ,KAAK,KAAA;CACpD,MAAM,WAAW,OAAO,aAAa,IAAI,UAAU,KAAK,KAAA;CACxD,MAAM,iBACL,OAAO,aAAa,IAAI,gBAAgB,MAAM,SAAS,OAAO,KAAA;CAE/D,OAAO;EACN;EACA,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;EAC3B,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;EAC/B,GAAI,iBAAiB,EAAE,eAAe,IAAI,CAAC;CAC5C;AACD"}
1
+ {"version":3,"file":"s3Url.mjs","names":[],"sources":["../src/s3Url.ts"],"sourcesContent":["/**\n * The `s3://` URL codec — how an S3 bucket is addressed as a single string.\n *\n * It lives beside the client rather than in a neutral package because `bucket`,\n * `region`, and `forcePathStyle` are S3's vocabulary. The neutral layer only\n * knows that a construct provides *one URL*; what that URL says is between the\n * component that composes it and the client that parses it. A `gs://` codec\n * would sit beside its own client and share nothing with this one.\n *\n * Both directions live here so they cannot drift: `parse(build(x))` is `x`.\n */\n\nimport {\n\tIncompleteStorageCredentials,\n\tMalformedStorageUrl,\n\tMissingStorageBucket,\n\tUnexpectedStorageScheme,\n} from './errors';\n\n/**\n * What an S3 URL addresses, and optionally who it calls as.\n *\n * Credentials are optional and come as a pair. With them, the URL is a key\n * scoped to this one bucket (`s3://KEY:SECRET@uploads`); without them the AWS\n * SDK resolves its own chain — an execution role when deployed,\n * `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` otherwise. The URL's pair wins\n * over that chain.\n */\nexport interface S3Address {\n\tbucket: string;\n\t/** Read off the bucket, never inherited: a bucket may live in another region. */\n\tregion?: string;\n\t/** Set for S3-compatible backends such as MinIO. */\n\tendpoint?: string;\n\t/** MinIO and most S3-compatible servers need path-style addressing. */\n\tforcePathStyle?: boolean;\n\t/** The URL's own access key; always paired with `secretAccessKey`. */\n\taccessKeyId?: string;\n\t/** The URL's own secret; always paired with `accessKeyId`. */\n\tsecretAccessKey?: string;\n}\n\nconst SCHEME = 's3:';\n\n/**\n * Compose an address into a URL.\n *\n * Credentials appear only when the address carries them, percent-encoded —\n * AWS secrets routinely contain `/` and `+`. An address without them builds a\n * URL with no userinfo, which leaves the caller to the SDK's own chain.\n */\nexport function build(address: S3Address): string {\n\tconst {\n\t\tbucket,\n\t\tregion,\n\t\tendpoint,\n\t\tforcePathStyle,\n\t\taccessKeyId,\n\t\tsecretAccessKey,\n\t} = address;\n\tif (!bucket) throw new MissingStorageBucket('');\n\n\tconst url = new URL(`${SCHEME}//${bucket}`);\n\tif (accessKeyId || secretAccessKey) {\n\t\tif (!accessKeyId) {\n\t\t\tthrow new IncompleteStorageCredentials(url.toString(), 'accessKeyId');\n\t\t}\n\t\tif (!secretAccessKey) {\n\t\t\tthrow new IncompleteStorageCredentials(url.toString(), 'secretAccessKey');\n\t\t}\n\t\turl.username = encodeURIComponent(accessKeyId);\n\t\turl.password = encodeURIComponent(secretAccessKey);\n\t}\n\tif (region) url.searchParams.set('region', region);\n\tif (endpoint) url.searchParams.set('endpoint', endpoint);\n\tif (forcePathStyle) url.searchParams.set('forcePathStyle', 'true');\n\treturn url.toString();\n}\n\n/**\n * Parse a URL back into an address. Throws if it is not an `s3://` URL, or if\n * its userinfo carries only one half of a key pair.\n */\nexport function parse(url: string): S3Address {\n\tlet parsed: URL;\n\ttry {\n\t\tparsed = new URL(url);\n\t} catch {\n\t\tthrow new MalformedStorageUrl(url);\n\t}\n\n\tif (parsed.protocol !== SCHEME) {\n\t\tthrow new UnexpectedStorageScheme(url, SCHEME, parsed.protocol);\n\t}\n\n\tconst bucket = parsed.hostname;\n\tif (!bucket) throw new MissingStorageBucket(url);\n\n\tconst region = parsed.searchParams.get('region') ?? undefined;\n\tconst endpoint = parsed.searchParams.get('endpoint') ?? undefined;\n\tconst forcePathStyle =\n\t\tparsed.searchParams.get('forcePathStyle') === 'true' ? true : undefined;\n\tconst credentials = credentialsOf(url, parsed);\n\n\treturn {\n\t\tbucket,\n\t\t...(region ? { region } : {}),\n\t\t...(endpoint ? { endpoint } : {}),\n\t\t...(forcePathStyle ? { forcePathStyle } : {}),\n\t\t...credentials,\n\t};\n}\n\n/** The URL's userinfo as a key pair: both halves, or nothing. */\nfunction credentialsOf(\n\turl: string,\n\tparsed: URL,\n): Pick<S3Address, 'accessKeyId' | 'secretAccessKey'> {\n\tconst accessKeyId = decode(url, parsed.username);\n\tconst secretAccessKey = decode(url, parsed.password);\n\tif (!accessKeyId && !secretAccessKey) return {};\n\tif (!accessKeyId) {\n\t\tthrow new IncompleteStorageCredentials(url, 'accessKeyId');\n\t}\n\tif (!secretAccessKey) {\n\t\tthrow new IncompleteStorageCredentials(url, 'secretAccessKey');\n\t}\n\treturn { accessKeyId, secretAccessKey };\n}\n\nfunction decode(url: string, value: string): string {\n\ttry {\n\t\treturn decodeURIComponent(value);\n\t} catch {\n\t\tthrow new MalformedStorageUrl(url);\n\t}\n}\n\n/**\n * The scheme and host a presigned link to one of the bucket's objects is on —\n * what a page showing private objects through signed links has to allow\n * (`next/image`'s `remotePatterns`, a CSP's `img-src`).\n *\n * The SDK's own rule: path-style against an endpoint (MinIO) is the endpoint;\n * virtual-host style puts the bucket in front of the endpoint's host, or of\n * S3's regional one. Carries no credentials, so it is public.\n *\n * `undefined` for an S3 bucket whose URL names no region: the SDK takes it\n * from the process, so the host is not knowable from the URL alone.\n */\nexport function signedOrigin(address: S3Address): string | undefined {\n\tconst { bucket, region, endpoint, forcePathStyle } = address;\n\tif (endpoint) {\n\t\tconst url = new URL(endpoint);\n\t\treturn forcePathStyle\n\t\t\t? url.origin\n\t\t\t: `${url.protocol}//${bucket}.${url.host}`;\n\t}\n\tif (!region) return undefined;\n\treturn `https://${bucket}.s3.${region}.amazonaws.com`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA0CA,MAAM,SAAS;;;;;;;;AASf,SAAgB,MAAM,SAA4B;CACjD,MAAM,EACL,QACA,QACA,UACA,gBACA,aACA,oBACG;CACJ,IAAI,CAAC,QAAQ,MAAM,IAAI,qBAAqB,EAAE;CAE9C,MAAM,MAAM,IAAI,IAAI,GAAG,OAAO,IAAI,QAAQ;CAC1C,IAAI,eAAe,iBAAiB;EACnC,IAAI,CAAC,aACJ,MAAM,IAAI,6BAA6B,IAAI,SAAS,GAAG,aAAa;EAErE,IAAI,CAAC,iBACJ,MAAM,IAAI,6BAA6B,IAAI,SAAS,GAAG,iBAAiB;EAEzE,IAAI,WAAW,mBAAmB,WAAW;EAC7C,IAAI,WAAW,mBAAmB,eAAe;CAClD;CACA,IAAI,QAAQ,IAAI,aAAa,IAAI,UAAU,MAAM;CACjD,IAAI,UAAU,IAAI,aAAa,IAAI,YAAY,QAAQ;CACvD,IAAI,gBAAgB,IAAI,aAAa,IAAI,kBAAkB,MAAM;CACjE,OAAO,IAAI,SAAS;AACrB;;;;;AAMA,SAAgB,MAAM,KAAwB;CAC7C,IAAI;CACJ,IAAI;EACH,SAAS,IAAI,IAAI,GAAG;CACrB,QAAQ;EACP,MAAM,IAAI,oBAAoB,GAAG;CAClC;CAEA,IAAI,OAAO,aAAa,QACvB,MAAM,IAAI,wBAAwB,KAAK,QAAQ,OAAO,QAAQ;CAG/D,MAAM,SAAS,OAAO;CACtB,IAAI,CAAC,QAAQ,MAAM,IAAI,qBAAqB,GAAG;CAE/C,MAAM,SAAS,OAAO,aAAa,IAAI,QAAQ,KAAK,KAAA;CACpD,MAAM,WAAW,OAAO,aAAa,IAAI,UAAU,KAAK,KAAA;CACxD,MAAM,iBACL,OAAO,aAAa,IAAI,gBAAgB,MAAM,SAAS,OAAO,KAAA;CAC/D,MAAM,cAAc,cAAc,KAAK,MAAM;CAE7C,OAAO;EACN;EACA,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;EAC3B,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;EAC/B,GAAI,iBAAiB,EAAE,eAAe,IAAI,CAAC;EAC3C,GAAG;CACJ;AACD;;AAGA,SAAS,cACR,KACA,QACqD;CACrD,MAAM,cAAc,OAAO,KAAK,OAAO,QAAQ;CAC/C,MAAM,kBAAkB,OAAO,KAAK,OAAO,QAAQ;CACnD,IAAI,CAAC,eAAe,CAAC,iBAAiB,OAAO,CAAC;CAC9C,IAAI,CAAC,aACJ,MAAM,IAAI,6BAA6B,KAAK,aAAa;CAE1D,IAAI,CAAC,iBACJ,MAAM,IAAI,6BAA6B,KAAK,iBAAiB;CAE9D,OAAO;EAAE;EAAa;CAAgB;AACvC;AAEA,SAAS,OAAO,KAAa,OAAuB;CACnD,IAAI;EACH,OAAO,mBAAmB,KAAK;CAChC,QAAQ;EACP,MAAM,IAAI,oBAAoB,GAAG;CAClC;AACD;;;;;;;;;;;;;AAcA,SAAgB,aAAa,SAAwC;CACpE,MAAM,EAAE,QAAQ,QAAQ,UAAU,mBAAmB;CACrD,IAAI,UAAU;EACb,MAAM,MAAM,IAAI,IAAI,QAAQ;EAC5B,OAAO,iBACJ,IAAI,SACJ,GAAG,IAAI,SAAS,IAAI,OAAO,GAAG,IAAI;CACtC;CACA,IAAI,CAAC,QAAQ,OAAO,KAAA;CACpB,OAAO,WAAW,OAAO,MAAM,OAAO;AACvC"}