sst 3.0.14 → 3.0.16

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.
@@ -6,7 +6,7 @@ export function createSessionBuilder() {
6
6
  async verify(token) {
7
7
  const auth = Object.values(Resource).find((value) => value.publicKey);
8
8
  if (!auth) {
9
- throw new Error("No auth resource found");
9
+ throw new Error("No auth resource found. Make sure to link the auth resource to this function.");
10
10
  }
11
11
  const publicKey = auth.publicKey;
12
12
  const result = await jwtVerify(token, await importSPKI(publicKey, "RS512"));
@@ -0,0 +1,74 @@
1
+ import { ZodObject, ZodSchema, z } from "zod";
2
+ type Event = {
3
+ type: string;
4
+ $output: any;
5
+ $metadata: any;
6
+ $payload: any;
7
+ };
8
+ export declare function EventClient<MetadataFunction extends () => any, Validator extends (schema: any) => (input: any) => any, MetadataSchema extends Parameters<Validator>[0] | undefined>(input: {
9
+ metadata?: MetadataSchema;
10
+ metadataFn?: MetadataFunction;
11
+ validator: Validator;
12
+ }): {
13
+ <Type extends string, Schema extends Parameters<Validator>[0]>(type: Type, schema: Schema): {
14
+ create: undefined extends MetadataSchema ? (properties: inferParser<Schema>["in"]) => {
15
+ type: Type;
16
+ properties: inferParser<Schema>["out"];
17
+ metadata: ReturnType<MetadataFunction>;
18
+ } : (properties: inferParser<Schema>["in"], metadata: inferParser<MetadataSchema>["in"]) => {
19
+ type: Type;
20
+ properties: inferParser<Schema>["out"];
21
+ metadata: ReturnType<MetadataFunction>;
22
+ };
23
+ type: Type;
24
+ $input: inferParser<Schema>["in"];
25
+ $output: inferParser<Schema>["out"];
26
+ $payload: {
27
+ type: Type;
28
+ properties: inferParser<Schema>["out"];
29
+ metadata: ReturnType<MetadataFunction>;
30
+ };
31
+ $metadata: ReturnType<MetadataFunction>;
32
+ };
33
+ coerce<Events extends Event>(_events: Events | Events[], raw: any): { [K in Events["type"]]: Extract<Events, {
34
+ type: K;
35
+ }>["$payload"]; }[Events["type"]];
36
+ };
37
+ export declare function ZodValidator<Schema extends ZodSchema>(schema: Schema): (input: z.input<Schema>) => z.output<Schema>;
38
+ export type ParserZodEsque<TInput, TParsedInput> = {
39
+ _input: TInput;
40
+ _output: TParsedInput;
41
+ };
42
+ export type ParserValibotEsque<TInput, TParsedInput> = {
43
+ _types?: {
44
+ input: TInput;
45
+ output: TParsedInput;
46
+ };
47
+ };
48
+ export type ParserMyZodEsque<TInput> = {
49
+ parse: (input: any) => TInput;
50
+ };
51
+ export type ParserSuperstructEsque<TInput> = {
52
+ create: (input: unknown) => TInput;
53
+ };
54
+ export type ParserCustomValidatorEsque<TInput> = (input: unknown) => Promise<TInput> | TInput;
55
+ export type ParserYupEsque<TInput> = {
56
+ validateSync: (input: unknown) => TInput;
57
+ };
58
+ export type ParserScaleEsque<TInput> = {
59
+ assert(value: unknown): asserts value is TInput;
60
+ };
61
+ export type ParserWithoutInput<TInput> = ParserCustomValidatorEsque<TInput> | ParserMyZodEsque<TInput> | ParserScaleEsque<TInput> | ParserSuperstructEsque<TInput> | ParserYupEsque<TInput>;
62
+ export type ParserWithInputOutput<TInput, TParsedInput> = ParserZodEsque<TInput, TParsedInput> | ParserValibotEsque<TInput, TParsedInput>;
63
+ export type Parser = ParserWithInputOutput<any, any> | ParserWithoutInput<any>;
64
+ export type inferParser<TParser extends Parser> = TParser extends ParserWithInputOutput<infer $TIn, infer $TOut> ? {
65
+ in: $TIn;
66
+ out: $TOut;
67
+ } : TParser extends ParserWithoutInput<infer $InOut> ? {
68
+ in: $InOut;
69
+ out: $InOut;
70
+ } : never;
71
+ export type inferEvent<T extends {
72
+ shape: ZodObject<any>;
73
+ }> = z.infer<T["shape"]>;
74
+ export {};
@@ -0,0 +1,38 @@
1
+ export function EventClient(input) {
2
+ const validator = input.validator;
3
+ const metadataValidator = input.metadata ? validator(input.metadata) : null;
4
+ const fn = function event(type, schema) {
5
+ const validate = validator(schema);
6
+ async function create(properties, metadata) {
7
+ if (metadataValidator) {
8
+ metadata = metadataValidator(metadata);
9
+ }
10
+ if (input.metadataFn) {
11
+ metadata = input.metadataFn();
12
+ }
13
+ properties = validate(properties);
14
+ return {
15
+ type,
16
+ properties,
17
+ metadata,
18
+ };
19
+ }
20
+ return {
21
+ create: create,
22
+ type,
23
+ $input: {},
24
+ $output: {},
25
+ $payload: {},
26
+ $metadata: {},
27
+ };
28
+ };
29
+ fn.coerce = (_events, raw) => {
30
+ return raw;
31
+ };
32
+ return fn;
33
+ }
34
+ export function ZodValidator(schema) {
35
+ return (input) => {
36
+ return schema.parse(input);
37
+ };
38
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export * from "./realtime/index.js";
1
2
  export * from "./resource.js";
2
- export * from "./vector-client.js";
3
+ export * from "./vector/index.js";
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
+ export * from "./realtime/index.js";
1
2
  export * from "./resource.js";
2
- export * from "./vector-client.js";
3
+ export * from "./vector/index.js";
@@ -0,0 +1,59 @@
1
+ import type { IoTCustomAuthorizerHandler } from "aws-lambda";
2
+ export interface RealtimeAuthResult {
3
+ /**
4
+ * The topics the client can subscribe to.
5
+ * @example
6
+ * For example, this subscribes to specific topics.
7
+ * ```js
8
+ * {
9
+ * subscribe: ["chat/room1", "chat/room2"]
10
+ * }
11
+ * ```
12
+ *
13
+ * And to subscribe to all topics under a specific prefix.
14
+ * ```js
15
+ * {
16
+ * subscribe: ["chat/*"]
17
+ * }
18
+ * ```
19
+ */
20
+ subscribe?: string[];
21
+ /**
22
+ * The topics the client can publish to.
23
+ * @example
24
+ * For example, this publishes to specific topics.
25
+ * ```js
26
+ * {
27
+ * publish: ["chat/room1", "chat/room2"]
28
+ * }
29
+ * ```
30
+ * And to publish to all topics under a specific prefix.
31
+ * ```js
32
+ * {
33
+ * publish: ["chat/*"]
34
+ * }
35
+ * ```
36
+ */
37
+ publish?: string[];
38
+ }
39
+ /**
40
+ * Creates an authorization handler for the `Realtime` component, that validates
41
+ * the token and grants permissions for the topics the client can subscribe and publish to.
42
+ *
43
+ * @example
44
+ * ```js
45
+ * import { RealtimeAuthHandler, Resource } from "sst";
46
+ *
47
+ * export const handler = RealtimeAuthHandler(async (token) => {
48
+ * // Validate the token
49
+ * console.log(token);
50
+ *
51
+ * // Return the topics to subscribe and publish
52
+ * return {
53
+ * subscribe: [`${Resource.App.name}/${Resource.App.stage}/chat/room1`],
54
+ * publish: [`${Resource.App.name}/${Resource.App.stage}/chat/room1`],
55
+ * };
56
+ * });
57
+ * ```
58
+ */
59
+ export declare function RealtimeAuthHandler(input: (token: string) => Promise<RealtimeAuthResult>): IoTCustomAuthorizerHandler;
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Creates an authorization handler for the `Realtime` component, that validates
3
+ * the token and grants permissions for the topics the client can subscribe and publish to.
4
+ *
5
+ * @example
6
+ * ```js
7
+ * import { RealtimeAuthHandler, Resource } from "sst";
8
+ *
9
+ * export const handler = RealtimeAuthHandler(async (token) => {
10
+ * // Validate the token
11
+ * console.log(token);
12
+ *
13
+ * // Return the topics to subscribe and publish
14
+ * return {
15
+ * subscribe: [`${Resource.App.name}/${Resource.App.stage}/chat/room1`],
16
+ * publish: [`${Resource.App.name}/${Resource.App.stage}/chat/room1`],
17
+ * };
18
+ * });
19
+ * ```
20
+ */
21
+ export function RealtimeAuthHandler(input) {
22
+ return async (evt, context) => {
23
+ const [, , , region, accountId] = context.invokedFunctionArn.split(":");
24
+ const token = Buffer.from(evt.protocolData.mqtt?.password ?? "", "base64").toString();
25
+ const ret = await input(token);
26
+ return {
27
+ isAuthenticated: true,
28
+ principalId: Date.now().toString(),
29
+ disconnectAfterInSeconds: 86400,
30
+ refreshAfterInSeconds: 300,
31
+ policyDocuments: [
32
+ {
33
+ Version: "2012-10-17",
34
+ Statement: [
35
+ {
36
+ Action: "iot:Connect",
37
+ Effect: "Allow",
38
+ Resource: "*",
39
+ },
40
+ ...(ret.subscribe
41
+ ? [
42
+ {
43
+ Action: "iot:Receive",
44
+ Effect: "Allow",
45
+ Resource: ret.subscribe.map((t) => `arn:aws:iot:${region}:${accountId}:topic/${t}`),
46
+ },
47
+ ]
48
+ : []),
49
+ ...(ret.subscribe
50
+ ? [
51
+ {
52
+ Action: "iot:Subscribe",
53
+ Effect: "Allow",
54
+ Resource: ret.subscribe.map((t) => `arn:aws:iot:${region}:${accountId}:topicfilter/${t}`),
55
+ },
56
+ ]
57
+ : []),
58
+ ...(ret.publish
59
+ ? [
60
+ {
61
+ Action: "iot:Publish",
62
+ Effect: "Allow",
63
+ Resource: ret.publish.map((t) => `arn:aws:iot:${region}:${accountId}:topic/${t}`),
64
+ },
65
+ ]
66
+ : []),
67
+ ],
68
+ },
69
+ ],
70
+ };
71
+ };
72
+ }
@@ -0,0 +1,3 @@
1
+ export type Prettify<T> = {
2
+ [K in keyof T]: T[K];
3
+ } & {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,193 @@
1
+ import { Resource } from "../resource.js";
2
+ export interface PutEvent {
3
+ /**
4
+ * The vector to store in the database.
5
+ * @example
6
+ * ```js
7
+ * {
8
+ * vector: [32.4, 6.55, 11.2, 10.3, 87.9],
9
+ * }
10
+ * ```
11
+ */
12
+ vector: number[];
13
+ /**
14
+ * Metadata for the event in JSON format.
15
+ * This metadata will be used to filter when quering and removing vectors.
16
+ * @example
17
+ * ```js
18
+ * {
19
+ * metadata: {
20
+ * type: "movie",
21
+ * id: "movie-123",
22
+ * name: "Spiderman",
23
+ * }
24
+ * }
25
+ * ```
26
+ */
27
+ metadata: Record<string, any>;
28
+ }
29
+ export interface QueryEvent {
30
+ /**
31
+ * The vector used to query the database.
32
+ * @example
33
+ * ```js
34
+ * {
35
+ * vector: [32.4, 6.55, 11.2, 10.3, 87.9],
36
+ * }
37
+ * ```
38
+ */
39
+ vector: number[];
40
+ /**
41
+ * The metadata used to filter the vectors.
42
+ * Only vectors that match the provided fields will be returned.
43
+ * @example
44
+ * ```js
45
+ * {
46
+ * include: {
47
+ * type: "movie",
48
+ * release: "2001",
49
+ * }
50
+ * }
51
+ * ```
52
+ * This will match the vector with metadata:
53
+ * ```js
54
+ * {
55
+ * type: "movie",
56
+ * name: "Spiderman",
57
+ * release: "2001",
58
+ * }
59
+ * ```
60
+ *
61
+ * But not the vector with metadata:
62
+ * ```js
63
+ * {
64
+ * type: "book",
65
+ * name: "Spiderman",
66
+ * release: "2001",
67
+ * }
68
+ * ```
69
+ */
70
+ include: Record<string, any>;
71
+ /**
72
+ * Exclude vectors with metadata that match the provided fields.
73
+ * @example
74
+ * ```js
75
+ * {
76
+ * include: {
77
+ * type: "movie",
78
+ * release: "2001",
79
+ * },
80
+ * exclude: {
81
+ * name: "Spiderman",
82
+ * }
83
+ * }
84
+ * ```
85
+ * This will match the vector with metadata:
86
+ * ```js
87
+ * {
88
+ * type: "movie",
89
+ * name: "A Beautiful Mind",
90
+ * release: "2001",
91
+ * }
92
+ * ```
93
+ *
94
+ * But not the vector with metadata:
95
+ * ```js
96
+ * {
97
+ * type: "book",
98
+ * name: "Spiderman",
99
+ * release: "2001",
100
+ * }
101
+ * ```
102
+ */
103
+ exclude?: Record<string, any>;
104
+ /**
105
+ * The threshold of similarity between the prompt and the queried vectors.
106
+ * Only vectors with a similarity score higher than the threshold will be returned.
107
+ * Expected value is between 0 and 1.
108
+ * - 0 means the prompt and the queried vectors are completely different.
109
+ * - 1 means the prompt and the queried vectors are identical.
110
+ * @default `0`
111
+ * @example
112
+ * ```js
113
+ * {
114
+ * threshold: 0.5,
115
+ * }
116
+ * ```
117
+ */
118
+ threshold?: number;
119
+ /**
120
+ * The number of results to return.
121
+ * @default `10`
122
+ * @example
123
+ * ```js
124
+ * {
125
+ * count: 10,
126
+ * }
127
+ * ```
128
+ */
129
+ count?: number;
130
+ }
131
+ export interface RemoveEvent {
132
+ /**
133
+ * The metadata used to filter the removal of vectors.
134
+ * Only vectors with metadata that match the provided fields will be removed.
135
+ * @example
136
+ * To remove vectors for movie with id "movie-123":
137
+ * ```js
138
+ * {
139
+ * include: {
140
+ * id: "movie-123",
141
+ * }
142
+ * }
143
+ * ```
144
+ * To remove vectors for all movies:
145
+ * ```js
146
+ * {
147
+ * include: {
148
+ * type: "movie",
149
+ * }
150
+ * }
151
+ * ```
152
+ */
153
+ include: Record<string, any>;
154
+ }
155
+ export interface QueryResponse {
156
+ /**
157
+ * Metadata for the event in JSON format that was provided when storing the vector.
158
+ */
159
+ metadata: Record<string, any>;
160
+ /**
161
+ * The similarity score between the prompt and the queried vector.
162
+ */
163
+ score: number;
164
+ }
165
+ export interface VectorClientResponse {
166
+ put: (event: PutEvent) => Promise<void>;
167
+ query: (event: QueryEvent) => Promise<QueryResponse>;
168
+ remove: (event: RemoveEvent) => Promise<void>;
169
+ }
170
+ /**
171
+ * Create a client to interact with the Vector database.
172
+ * @example
173
+ * ```js
174
+ * import { VectorClient } from "sst";
175
+ * const client = VectorClient("MyVectorDB");
176
+ *
177
+ * // Store a vector into the db
178
+ * await client.put({
179
+ * vector: [32.4, 6.55, 11.2, 10.3, 87.9],
180
+ * metadata: { type: "movie", genre: "comedy" },
181
+ * });
182
+ *
183
+ * // Query vectors similar to the provided vector
184
+ * const result = await client.query({
185
+ * vector: [32.4, 6.55, 11.2, 10.3, 87.9],
186
+ * include: { type: "movie" },
187
+ * exclude: { genre: "thriller" },
188
+ * });
189
+ * ```
190
+ */
191
+ export declare function VectorClient<T extends keyof {
192
+ [key in keyof Resource as "sst.aws.Vector" extends Resource[key]["type"] ? string extends key ? never : key : never]: Resource[key];
193
+ }>(name: T): VectorClientResponse;
@@ -0,0 +1,62 @@
1
+ import { LambdaClient, InvokeCommand, } from "@aws-sdk/client-lambda";
2
+ import { Resource } from "../resource.js";
3
+ const lambda = new LambdaClient();
4
+ /**
5
+ * Create a client to interact with the Vector database.
6
+ * @example
7
+ * ```js
8
+ * import { VectorClient } from "sst";
9
+ * const client = VectorClient("MyVectorDB");
10
+ *
11
+ * // Store a vector into the db
12
+ * await client.put({
13
+ * vector: [32.4, 6.55, 11.2, 10.3, 87.9],
14
+ * metadata: { type: "movie", genre: "comedy" },
15
+ * });
16
+ *
17
+ * // Query vectors similar to the provided vector
18
+ * const result = await client.query({
19
+ * vector: [32.4, 6.55, 11.2, 10.3, 87.9],
20
+ * include: { type: "movie" },
21
+ * exclude: { genre: "thriller" },
22
+ * });
23
+ * ```
24
+ */
25
+ export function VectorClient(name) {
26
+ return {
27
+ put: async (event) => {
28
+ const ret = await lambda.send(new InvokeCommand({
29
+ // @ts-expect-error
30
+ FunctionName: Resource[name].putFunction,
31
+ Payload: JSON.stringify(event),
32
+ }));
33
+ parsePayload(ret, "Failed to store into the vector db");
34
+ },
35
+ query: async (event) => {
36
+ const ret = await lambda.send(new InvokeCommand({
37
+ // @ts-expect-error
38
+ FunctionName: Resource[name].queryFunction,
39
+ Payload: JSON.stringify(event),
40
+ }));
41
+ return parsePayload(ret, "Failed to query the vector db");
42
+ },
43
+ remove: async (event) => {
44
+ const ret = await lambda.send(new InvokeCommand({
45
+ // @ts-expect-error
46
+ FunctionName: Resource[name].removeFunction,
47
+ Payload: JSON.stringify(event),
48
+ }));
49
+ parsePayload(ret, "Failed to remove from the vector db");
50
+ },
51
+ };
52
+ }
53
+ function parsePayload(output, message) {
54
+ const payload = JSON.parse(Buffer.from(output.Payload).toString());
55
+ // Set cause to the payload so that it can be logged in CloudWatch
56
+ if (output.FunctionError) {
57
+ const e = new Error(message);
58
+ e.cause = payload;
59
+ throw e;
60
+ }
61
+ return payload;
62
+ }
package/package.json CHANGED
@@ -3,26 +3,28 @@
3
3
  "name": "sst",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
- "version": "3.0.14",
6
+ "version": "3.0.16",
7
7
  "main": "./dist/index.js",
8
8
  "exports": {
9
9
  ".": "./dist/index.js",
10
- "./*": [
11
- "./dist/*/index.js",
12
- "./dist/*.js"
13
- ]
10
+ "./auth": "./dist/auth/index.js",
11
+ "./auth/adapter": "./dist/auth/adapter.js",
12
+ "./*": "./dist/*.js"
14
13
  },
15
14
  "devDependencies": {
16
15
  "@tsconfig/node18": "^18.2.2",
17
16
  "@types/node": "^20.11.0",
18
- "typescript": "^5.3.3"
17
+ "typescript": "^5.3.3",
18
+ "hono": "4.3.9"
19
19
  },
20
20
  "files": [
21
21
  "dist"
22
22
  ],
23
+ "peerDependencies": {
24
+ "hono": "4.x"
25
+ },
23
26
  "dependencies": {
24
27
  "@aws-sdk/client-lambda": "3.478.0",
25
- "hono": "4.0.1",
26
28
  "jose": "5.2.3",
27
29
  "openid-client": "5.6.4"
28
30
  },