@teamkeel/functions-runtime 0.395.0 → 0.395.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamkeel/functions-runtime",
3
- "version": "0.395.0",
3
+ "version": "0.395.2",
4
4
  "description": "Internal package used by @teamkeel/sdk",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -21,6 +21,7 @@
21
21
  "dependencies": {
22
22
  "@aws-sdk/client-s3": "^3.637.0",
23
23
  "@aws-sdk/credential-providers": "^3.637.0",
24
+ "@aws-sdk/s3-request-presigner": "^3.637.0",
24
25
  "@neondatabase/serverless": "^0.9.4",
25
26
  "@opentelemetry/api": "^1.7.0",
26
27
  "@opentelemetry/exporter-trace-otlp-proto": "^0.46.0",
package/src/File.js CHANGED
@@ -4,7 +4,8 @@ const {
4
4
  GetObjectCommand,
5
5
  } = require("@aws-sdk/client-s3");
6
6
  const { fromEnv } = require("@aws-sdk/credential-providers");
7
- const { useDatabase } = require("./database");
7
+ const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
8
+ const { createDatabaseClient } = require("./database");
8
9
  const { DatabaseError } = require("./errors");
9
10
  const KSUID = require("ksuid");
10
11
 
@@ -105,9 +106,7 @@ class File extends InlineFile {
105
106
  async read() {
106
107
  if (this._contents) {
107
108
  const arrayBuffer = await this._contents.arrayBuffer();
108
- const buffer = Buffer.from(arrayBuffer);
109
-
110
- return buffer;
109
+ return Buffer.from(arrayBuffer);
111
110
  }
112
111
 
113
112
  if (isS3Storage()) {
@@ -122,12 +121,12 @@ class File extends InlineFile {
122
121
  };
123
122
  const command = new GetObjectCommand(params);
124
123
  const response = await s3Client.send(command);
125
- const blob = response.Body.transformToByteArray();
124
+ const blob = await response.Body.transformToByteArray();
126
125
  return Buffer.from(blob);
127
126
  }
128
127
 
129
128
  // default to db storage
130
- const db = useDatabase();
129
+ const db = createDatabaseClient();
131
130
 
132
131
  try {
133
132
  let query = db
@@ -139,6 +138,8 @@ class File extends InlineFile {
139
138
  return row.data;
140
139
  } catch (e) {
141
140
  throw new DatabaseError(e);
141
+ } finally {
142
+ await db.destroy();
142
143
  }
143
144
  }
144
145
 
@@ -157,6 +158,30 @@ class File extends InlineFile {
157
158
  return this;
158
159
  }
159
160
 
161
+ async getPresignedUrl() {
162
+ if (isS3Storage()) {
163
+ const s3Client = new S3Client({
164
+ credentials: fromEnv(),
165
+ region: process.env.KEEL_REGION,
166
+ });
167
+
168
+ const command = new GetObjectCommand({
169
+ Bucket: process.env.KEEL_FILES_BUCKET_NAME,
170
+ Key: "files/" + this.key(),
171
+ });
172
+
173
+ const url = await getSignedUrl(s3Client, command, { expiresIn: 60 * 24 });
174
+
175
+ return new URL(url);
176
+ } else {
177
+ const contents = await this.read();
178
+ const dataurl = `data:${this.contentType};name=${
179
+ this.filename
180
+ };base64,${contents.toString("base64")}`;
181
+ return new URL(dataurl);
182
+ }
183
+ }
184
+
160
185
  toDbRecord() {
161
186
  return {
162
187
  key: this.key,
@@ -210,7 +235,7 @@ async function storeFile(contents, key, filename, contentType, expires) {
210
235
  throw error;
211
236
  }
212
237
  } else {
213
- const db = useDatabase();
238
+ const db = createDatabaseClient();
214
239
 
215
240
  try {
216
241
  let query = db
@@ -236,6 +261,8 @@ async function storeFile(contents, key, filename, contentType, expires) {
236
261
  await query.execute();
237
262
  } catch (e) {
238
263
  throw new DatabaseError(e);
264
+ } finally {
265
+ await db.destroy();
239
266
  }
240
267
  }
241
268
  }
package/src/database.js CHANGED
@@ -70,6 +70,7 @@ function useDatabase() {
70
70
 
71
71
  // If we've gotten to this point, then we know that we are in a custom function runtime server
72
72
  // context and we haven't been able to retrieve the in-context instance of Kysely, which means we should throw an error.
73
+ console.trace();
73
74
  throw new Error("useDatabase must be called within a function");
74
75
  }
75
76
 
@@ -80,6 +80,7 @@ async function handleRequest(request, config) {
80
80
  return customFunction(ctx, inputs);
81
81
  }
82
82
  );
83
+
83
84
  if (result instanceof Error) {
84
85
  span.recordException(result);
85
86
  span.setStatus({
package/src/index.d.ts CHANGED
@@ -176,6 +176,9 @@ export declare class File extends InlineFile {
176
176
  get key(): string;
177
177
  // Gets size of the file's contents in bytes
178
178
  get isPublic(): boolean;
179
+ // Generates a presigned download URL
180
+ getPresignedUrl(): Promise<URL>;
181
+ // Creates a new instance from the database record
179
182
  static fromDbRecord(input: FileDbRecord): File;
180
183
  // Persists the file
181
184
  toDbRecord(): FileDbRecord;