@teamkeel/functions-runtime 0.402.0 → 0.403.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/File.js +34 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamkeel/functions-runtime",
3
- "version": "0.402.0",
3
+ "version": "0.403.0",
4
4
  "description": "Internal package used by @teamkeel/sdk",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/File.js CHANGED
@@ -9,6 +9,37 @@ const { useDatabase } = require("./database");
9
9
  const { DatabaseError } = require("./errors");
10
10
  const KSUID = require("ksuid");
11
11
 
12
+ const s3Client = (() => {
13
+ if (!process.env.KEEL_FILES_BUCKET_NAME) {
14
+ return null;
15
+ }
16
+
17
+ // Set in integration tests to send all AWS API calls to a test server
18
+ // for mocking
19
+ const endpoint = process.env.TEST_AWS_ENDPOINT;
20
+
21
+ return new S3Client({
22
+ region: process.env.KEEL_REGION,
23
+
24
+ // If a test endpoint is provided then use some test credentials rather than fromEnv()
25
+ credentials: endpoint
26
+ ? {
27
+ accessKeyId: "test",
28
+ secretAccessKey: "test",
29
+ }
30
+ : fromEnv(),
31
+
32
+ // If a custom endpoint is set we need to use a custom resolver. Just settng the base endpoint isn't enough for S3 as it
33
+ // as the default resolver uses the bucket name as a sub-domain, which likely won't work with the custom endpoint.
34
+ // By impleenting a full resolver we can force it to be the endpoint we want.
35
+ endpointProvider: () => {
36
+ return {
37
+ url: URL.parse(endpoint),
38
+ };
39
+ },
40
+ });
41
+ })();
42
+
12
43
  class InlineFile {
13
44
  constructor({ filename, contentType }) {
14
45
  this._filename = filename;
@@ -109,12 +140,7 @@ class File extends InlineFile {
109
140
  return Buffer.from(arrayBuffer);
110
141
  }
111
142
 
112
- if (isS3Storage()) {
113
- const s3Client = new S3Client({
114
- credentials: fromEnv(),
115
- region: process.env.KEEL_REGION,
116
- });
117
-
143
+ if (s3Client) {
118
144
  const params = {
119
145
  Bucket: process.env.KEEL_FILES_BUCKET_NAME,
120
146
  Key: "files/" + this.key,
@@ -157,12 +183,7 @@ class File extends InlineFile {
157
183
  }
158
184
 
159
185
  async getPresignedUrl() {
160
- if (isS3Storage()) {
161
- const s3Client = new S3Client({
162
- credentials: fromEnv(),
163
- region: process.env.KEEL_REGION,
164
- });
165
-
186
+ if (s3Client) {
166
187
  const command = new GetObjectCommand({
167
188
  Bucket: process.env.KEEL_FILES_BUCKET_NAME,
168
189
  Key: "files/" + this.key,
@@ -203,12 +224,7 @@ class File extends InlineFile {
203
224
  }
204
225
 
205
226
  async function storeFile(contents, key, filename, contentType, expires) {
206
- if (isS3Storage()) {
207
- const s3Client = new S3Client({
208
- credentials: fromEnv(),
209
- region: process.env.KEEL_REGION,
210
- });
211
-
227
+ if (s3Client) {
212
228
  const params = {
213
229
  Bucket: process.env.KEEL_FILES_BUCKET_NAME,
214
230
  Key: "files/" + key,
@@ -269,10 +285,6 @@ async function storeFile(contents, key, filename, contentType, expires) {
269
285
  }
270
286
  }
271
287
 
272
- function isS3Storage() {
273
- return "KEEL_FILES_BUCKET_NAME" in process.env;
274
- }
275
-
276
288
  module.exports = {
277
289
  InlineFile,
278
290
  File,