@coffre/client 0.8.0 → 0.8.1

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.
@@ -17,6 +17,21 @@ export type ProxiedCredential = {
17
17
  url: string;
18
18
  /** `fetch`, at a path under the credential's upstream, with the handle the proxy takes. */
19
19
  fetch(path: string, init?: RequestInit): Promise<Response>;
20
+ /**
21
+ * A presigned GET (or HEAD) of `path` under an AWS-signed credential's
22
+ * upstream, `/bucket/key` with each segment encoded as in a URL: coffre
23
+ * signs it with the real key pair, for `expiresIn` seconds (15 minutes
24
+ * unless set, an hour at most), and the URL is the upstream's own, for a
25
+ * browser or anyone to use until then.
26
+ */
27
+ presign(path: string, options?: {
28
+ method?: 'GET' | 'HEAD';
29
+ expiresIn?: number;
30
+ query?: Record<string, string>;
31
+ }): Promise<{
32
+ url: string;
33
+ expiresAt: string;
34
+ }>;
20
35
  };
21
36
  type Env = Record<string, string | undefined>;
22
37
  /** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
@@ -19,6 +19,26 @@ function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input,
19
19
  ...init,
20
20
  headers
21
21
  });
22
+ },
23
+ presign: async (path, options = {}) => {
24
+ const answer = await fetcher(url.slice(0, url.lastIndexOf("/")), {
25
+ method: "POST",
26
+ headers: {
27
+ authorization: `Bearer ${handle}`,
28
+ "content-type": "application/json"
29
+ },
30
+ body: JSON.stringify({ presign: {
31
+ credential: name,
32
+ path,
33
+ ...options
34
+ } })
35
+ });
36
+ const body = await answer.json();
37
+ if (!answer.ok || body.url === void 0) throw new Error(`coffre presigned nothing: ${body.message ?? answer.status}`);
38
+ return {
39
+ url: body.url,
40
+ expiresAt: body.expiresAt
41
+ };
22
42
  }
23
43
  };
24
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/client",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "The coffre API as typed function calls.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/credential.ts CHANGED
@@ -16,6 +16,14 @@ export type ProxiedCredential = {
16
16
  url: string;
17
17
  /** `fetch`, at a path under the credential's upstream, with the handle the proxy takes. */
18
18
  fetch(path: string, init?: RequestInit): Promise<Response>;
19
+ /**
20
+ * A presigned GET (or HEAD) of `path` under an AWS-signed credential's
21
+ * upstream, `/bucket/key` with each segment encoded as in a URL: coffre
22
+ * signs it with the real key pair, for `expiresIn` seconds (15 minutes
23
+ * unless set, an hour at most), and the URL is the upstream's own, for a
24
+ * browser or anyone to use until then.
25
+ */
26
+ presign(path: string, options?: { method?: 'GET' | 'HEAD'; expiresIn?: number; query?: Record<string, string> }): Promise<{ url: string; expiresAt: string }>;
19
27
  };
20
28
 
21
29
  type Env = Record<string, string | undefined>;
@@ -44,5 +52,16 @@ export function credential(
44
52
  headers.set('x-coffre-handle', handle);
45
53
  return fetcher(`${url}${path.startsWith('/') ? '' : '/'}${path}`, { ...init, headers });
46
54
  },
55
+ presign: async (path, options = {}) => {
56
+ // The environment's own URL on the proxy, which the credential's is under.
57
+ const answer = await fetcher(url.slice(0, url.lastIndexOf('/')), {
58
+ method: 'POST',
59
+ headers: { authorization: `Bearer ${handle}`, 'content-type': 'application/json' },
60
+ body: JSON.stringify({ presign: { credential: name, path, ...options } }),
61
+ });
62
+ const body = (await answer.json()) as { url?: string; expiresAt?: string; message?: string };
63
+ if (!answer.ok || body.url === undefined) throw new Error(`coffre presigned nothing: ${body.message ?? answer.status}`);
64
+ return { url: body.url, expiresAt: body.expiresAt! };
65
+ },
47
66
  };
48
67
  }