@max-cloud/functions 0.1.0 → 0.2.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/README.md +26 -0
- package/dist/cli.js +0 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/storage.d.ts +12 -0
- package/dist/storage.js +28 -0
- package/dist/storage.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -72,6 +72,32 @@ Postgres database (`DATABASE_URL`), in a table the SDK creates
|
|
|
72
72
|
`await enqueue("mail", msg, { client: tx })`.
|
|
73
73
|
- **Delay a job** with `{ delaySeconds }`.
|
|
74
74
|
|
|
75
|
+
## Buckets
|
|
76
|
+
|
|
77
|
+
An app's S3 buckets come from the registry:
|
|
78
|
+
`mcloud apps set shop --buckets uploads,assets:public`. The maxcloud
|
|
79
|
+
controller creates them in UpCloud Object Storage as `mc-<app>--<bucket>`,
|
|
80
|
+
and every function gets the app's credentials as environment variables
|
|
81
|
+
(the `AWS_*` ones any S3 client reads, and `MAXCLOUD_APP`). So the AWS SDK
|
|
82
|
+
needs no configuration, only the bucket's full name:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
|
|
86
|
+
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
87
|
+
import { bucketName } from "@max-cloud/functions";
|
|
88
|
+
|
|
89
|
+
const s3 = new S3Client({ forcePathStyle: true });
|
|
90
|
+
const uploads = bucketName("uploads"); // "mc-shop--uploads"
|
|
91
|
+
|
|
92
|
+
// A URL the browser uploads to directly; then enqueue the work.
|
|
93
|
+
const url = await getSignedUrl(s3, new PutObjectCommand({ Bucket: uploads, Key: "a.png" }), { expiresIn: 300 });
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
A `:public` bucket's objects can be read by anyone at
|
|
97
|
+
`<AWS_ENDPOINT_URL_S3>/<bucket>/<key>`; listing and writing still need the
|
|
98
|
+
credentials. Object Storage has no bucket events: uploads tell the app
|
|
99
|
+
themselves (presigned URL, then a queue job).
|
|
100
|
+
|
|
75
101
|
## Running
|
|
76
102
|
|
|
77
103
|
The container runs `maxcloud-functions`, which starts the function named in
|
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { onRequest, onSchedule, onQueue, type CommonOptions, type HttpOptions, type ScheduleOptions, type QueueOptions, type HttpHandler, type HttpContext, type ScheduleHandler, type ScheduleContext, type JobHandler, type JobContext, type Job, type HttpFunction, type ScheduleFunction, type QueueFunction, } from "./define.ts";
|
|
2
2
|
export { enqueue, type EnqueueOptions, type Queryable } from "./queue.ts";
|
|
3
|
+
export { bucketName } from "./storage.ts";
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,UAAU,EACV,OAAO,GAeR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,OAAO,EAAuC,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,UAAU,EACV,OAAO,GAeR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,OAAO,EAAuC,MAAM,YAAY,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The full name of one of the app's buckets: `bucketName("uploads")` is
|
|
3
|
+
* `mc-shop--uploads` in app shop. Use it with any S3 client; the AWS SDK
|
|
4
|
+
* picks up the endpoint and credentials from the environment by itself:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
|
|
8
|
+
* const s3 = new S3Client({ forcePathStyle: true });
|
|
9
|
+
* await s3.send(new PutObjectCommand({ Bucket: bucketName("uploads"), Key: "a.txt", Body: "hi" }));
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function bucketName(name: string, env?: Record<string, string | undefined>): string;
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Buckets an app gets from the registry (`mcloud apps set <app> --buckets
|
|
2
|
+
// uploads`). The maxcloud controller names them mc-<app>--<bucket> and puts
|
|
3
|
+
// the app's credentials in the Secret maxcloud-s3, which every function
|
|
4
|
+
// gets as environment variables: the AWS_* ones any S3 client reads
|
|
5
|
+
// (endpoint, region, keys, UpCloud's checksum settings) and MAXCLOUD_APP.
|
|
6
|
+
/**
|
|
7
|
+
* The full name of one of the app's buckets: `bucketName("uploads")` is
|
|
8
|
+
* `mc-shop--uploads` in app shop. Use it with any S3 client; the AWS SDK
|
|
9
|
+
* picks up the endpoint and credentials from the environment by itself:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
|
|
13
|
+
* const s3 = new S3Client({ forcePathStyle: true });
|
|
14
|
+
* await s3.send(new PutObjectCommand({ Bucket: bucketName("uploads"), Key: "a.txt", Body: "hi" }));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export function bucketName(name, env = process.env) {
|
|
18
|
+
const app = env.MAXCLOUD_APP;
|
|
19
|
+
if (!app) {
|
|
20
|
+
throw new Error(`bucketName(${JSON.stringify(name)}): MAXCLOUD_APP isn't set. The app has no buckets yet ` +
|
|
21
|
+
"(mcloud apps set <app> --buckets ...), or this isn't running on maxcloud.");
|
|
22
|
+
}
|
|
23
|
+
if (!/^[a-z0-9]([a-z0-9]|-[a-z0-9])*$/.test(name)) {
|
|
24
|
+
throw new Error(`bucketName(${JSON.stringify(name)}): lowercase letters, digits and single dashes`);
|
|
25
|
+
}
|
|
26
|
+
return `mc-${app}--${name}`;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,4EAA4E;AAC5E,wEAAwE;AACxE,oEAAoE;AACpE,0EAA0E;AAE1E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,GAAG,GAAuC,OAAO,CAAC,GAAG;IAC5F,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,CAAC;IAC7B,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CACb,cAAc,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,wDAAwD;YACxF,2EAA2E,CAC9E,CAAC;IACJ,CAAC;IACD,IAAI,CAAC,iCAAiC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QAClD,MAAM,IAAI,KAAK,CAAC,cAAc,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,gDAAgD,CAAC,CAAC;IACtG,CAAC;IACD,OAAO,MAAM,GAAG,KAAK,IAAI,EAAE,CAAC;AAC9B,CAAC"}
|
package/package.json
CHANGED