sst 2.38.6 → 2.38.7

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.
@@ -0,0 +1,13 @@
1
+ export declare const Context: {
2
+ create: typeof create;
3
+ reset: typeof reset;
4
+ memo: typeof memo;
5
+ };
6
+ declare function create<C>(cb?: (() => C) | string, name?: string): {
7
+ use(): C;
8
+ reset(): void;
9
+ provide(value: C): void;
10
+ };
11
+ declare function reset(): void;
12
+ export declare function memo<C>(cb: () => C, name?: string): () => C;
13
+ export {};
@@ -0,0 +1,69 @@
1
+ export const Context = {
2
+ create,
3
+ reset,
4
+ memo,
5
+ };
6
+ const state = {
7
+ requestID: "",
8
+ contexts: new Map(),
9
+ tracking: [],
10
+ };
11
+ function create(cb, name) {
12
+ const id = typeof cb === "string" ? cb : name || Symbol(cb?.toString());
13
+ return {
14
+ use() {
15
+ let result = state.contexts.get(id);
16
+ if (!result) {
17
+ if (!cb || typeof cb === "string")
18
+ throw new Error(`"${String(id)}" context was not provided.`);
19
+ state.tracking.push(id);
20
+ const value = cb();
21
+ state.tracking.pop();
22
+ result = {
23
+ value,
24
+ dependants: new Set(),
25
+ };
26
+ state.contexts.set(id, result);
27
+ }
28
+ const last = state.tracking[state.tracking.length - 1];
29
+ // Use is being called within another context booting up so mark it as a dependent
30
+ if (last)
31
+ result.dependants.add(last);
32
+ return result.value;
33
+ },
34
+ reset() {
35
+ resetDependencies(id);
36
+ state.contexts.delete(id);
37
+ },
38
+ provide(value) {
39
+ // If a new request has started, automatically clear all contexts
40
+ const requestID = global[Symbol.for("aws.lambda.runtime.requestId")];
41
+ if (state.requestID !== requestID) {
42
+ state.requestID = requestID;
43
+ reset();
44
+ }
45
+ // If the context is already set, we need to reset its dependants
46
+ resetDependencies(id);
47
+ state.contexts.set(id, {
48
+ value,
49
+ dependants: new Set(),
50
+ });
51
+ },
52
+ };
53
+ }
54
+ function reset() {
55
+ state.contexts.clear();
56
+ }
57
+ function resetDependencies(id) {
58
+ const info = state.contexts.get(id);
59
+ if (!info)
60
+ return;
61
+ for (const dependantID of info.dependants) {
62
+ state.contexts.delete(dependantID);
63
+ resetDependencies(dependantID);
64
+ }
65
+ }
66
+ export function memo(cb, name) {
67
+ const ctx = create(cb, name);
68
+ return ctx.use;
69
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "sideEffects": false,
3
3
  "name": "sst",
4
- "version": "2.38.6",
4
+ "version": "2.38.7",
5
5
  "bin": {
6
6
  "sst": "cli/sst.js"
7
7
  },
@@ -120,7 +120,7 @@
120
120
  "@types/ws": "^8.5.3",
121
121
  "@types/yargs": "^17.0.13",
122
122
  "archiver": "^5.3.1",
123
- "astro-sst": "2.38.6",
123
+ "astro-sst": "2.38.7",
124
124
  "async": "^3.2.4",
125
125
  "tsx": "^3.12.1",
126
126
  "typescript": "^5.2.2",