sst 3.1.12 → 3.1.13

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.
@@ -1,6 +1,12 @@
1
- import { LambdaClient, InvokeCommand, } from "@aws-sdk/client-lambda";
1
+ import { AwsClient } from "aws4fetch";
2
2
  import { Resource } from "../resource.js";
3
- const lambda = new LambdaClient();
3
+ const client = new AwsClient({
4
+ accessKeyId: process.env.AWS_ACCESS_KEY_ID,
5
+ secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
6
+ sessionToken: process.env.AWS_SESSION_TOKEN,
7
+ region: process.env.AWS_REGION,
8
+ });
9
+ const endpoint = `https://lambda.${process.env.AWS_REGION}.amazonaws.com/2015-03-31`;
4
10
  /**
5
11
  * Create a client to interact with the Vector database.
6
12
  * @example
@@ -31,38 +37,76 @@ const lambda = new LambdaClient();
31
37
  export function VectorClient(name) {
32
38
  return {
33
39
  put: async (event) => {
34
- const ret = await lambda.send(new InvokeCommand({
35
- // @ts-expect-error
36
- FunctionName: Resource[name].putFunction,
37
- Payload: JSON.stringify(event),
38
- }));
39
- parsePayload(ret, "Failed to store into the vector db");
40
+ await invokeFunction(
41
+ // @ts-expect-error
42
+ Resource[name].putFunction, JSON.stringify(event), "Failed to store into the vector db");
40
43
  },
41
44
  query: async (event) => {
42
- const ret = await lambda.send(new InvokeCommand({
43
- // @ts-expect-error
44
- FunctionName: Resource[name].queryFunction,
45
- Payload: JSON.stringify(event),
46
- }));
47
- return parsePayload(ret, "Failed to query the vector db");
45
+ return await invokeFunction(
46
+ // @ts-expect-error
47
+ Resource[name].queryFunction, JSON.stringify(event), "Failed to query the vector db");
48
48
  },
49
49
  remove: async (event) => {
50
- const ret = await lambda.send(new InvokeCommand({
51
- // @ts-expect-error
52
- FunctionName: Resource[name].removeFunction,
53
- Payload: JSON.stringify(event),
54
- }));
55
- parsePayload(ret, "Failed to remove from the vector db");
50
+ await invokeFunction(
51
+ // @ts-expect-error
52
+ Resource[name].removeFunction, JSON.stringify(event), "Failed to remove from the vector db");
56
53
  },
57
54
  };
58
55
  }
59
- function parsePayload(output, message) {
60
- const payload = JSON.parse(Buffer.from(output.Payload).toString());
61
- // Set cause to the payload so that it can be logged in CloudWatch
62
- if (output.FunctionError) {
63
- const e = new Error(message);
64
- e.cause = payload;
65
- throw e;
56
+ async function invokeFunction(functionName, body, errorMessage, attempts = 0) {
57
+ try {
58
+ const response = await client.fetch(`${endpoint}/functions/${functionName}/invocations`, {
59
+ method: "POST",
60
+ headers: { Accept: "application/json" },
61
+ body,
62
+ });
63
+ // success
64
+ if (response.status === 200 || response.status === 201) {
65
+ if (response.headers.get("content-length") === "0")
66
+ return undefined;
67
+ const text = await response.text();
68
+ try {
69
+ return JSON.parse(text);
70
+ }
71
+ catch (e) {
72
+ throw new Error(`Failed to parse JSON response: ${text}`);
73
+ }
74
+ }
75
+ // error
76
+ const error = new Error();
77
+ const text = await response.text();
78
+ try {
79
+ const json = JSON.parse(text);
80
+ error.name = json.Error?.Code;
81
+ error.message = json.Error?.Message ?? json.message ?? text;
82
+ }
83
+ catch (e) {
84
+ error.message = text;
85
+ }
86
+ error.name = error.name ?? response.headers.get("x-amzn-ErrorType");
87
+ // @ts-expect-error
88
+ error.requestID = response.headers.get("x-amzn-RequestId");
89
+ // @ts-expect-error
90
+ error.statusCode = response.status;
91
+ throw error;
92
+ }
93
+ catch (e) {
94
+ let isRetryable = false;
95
+ // AWS throttling errors => retry
96
+ if ([
97
+ "ThrottlingException",
98
+ "Throttling",
99
+ "TooManyRequestsException",
100
+ "OperationAbortedException",
101
+ "TimeoutError",
102
+ "NetworkingError",
103
+ ].includes(e.name)) {
104
+ isRetryable = true;
105
+ }
106
+ if (!isRetryable)
107
+ throw e;
108
+ // retry
109
+ await new Promise((resolve) => setTimeout(resolve, 1.5 ** attempts * 100 * Math.random()));
110
+ return await invokeFunction(functionName, body, errorMessage, attempts + 1);
66
111
  }
67
- return payload;
68
112
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "name": "sst",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
- "version": "3.1.12",
6
+ "version": "3.1.13",
7
7
  "main": "./dist/index.js",
8
8
  "exports": {
9
9
  ".": "./dist/index.js",
@@ -47,14 +47,13 @@
47
47
  }
48
48
  },
49
49
  "optionalDependencies": {
50
- "sst-linux-x64": "3.1.12",
51
- "sst-linux-x86": "3.1.12",
52
- "sst-darwin-x64": "3.1.12",
53
- "sst-linux-arm64": "3.1.12",
54
- "sst-darwin-arm64": "3.1.12"
50
+ "sst-linux-x64": "3.1.13",
51
+ "sst-linux-x86": "3.1.13",
52
+ "sst-linux-arm64": "3.1.13",
53
+ "sst-darwin-x64": "3.1.13",
54
+ "sst-darwin-arm64": "3.1.13"
55
55
  },
56
56
  "dependencies": {
57
- "@aws-sdk/client-lambda": "3.478.0",
58
57
  "aws4fetch": "^1.0.18",
59
58
  "jose": "5.2.3",
60
59
  "openid-client": "5.6.4"