socket-function 0.8.15 → 0.8.17

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.
@@ -23,6 +23,10 @@ export type SocketExposedInterfaceClass = {
23
23
  };
24
24
  export interface SocketExposedShape<ExposedType extends SocketExposedInterface = SocketExposedInterface, CallContext extends CallContextType = CallContextType> {
25
25
  [functionName: string]: {
26
+ /** Indicates with the same input, we give the same output, forever,
27
+ * independent of code changes. This only works for data storage.
28
+ */
29
+ dataImmutable?: boolean;
26
30
  hooks?: SocketFunctionHook<ExposedType, CallContext>[];
27
31
  clientHooks?: SocketFunctionClientHook<ExposedType, CallContext>[];
28
32
  };
@@ -39,11 +39,12 @@ export function asyncObservable<Output, Key>(maxCount: number, getValue: (key: K
39
39
  invalidate(key: Key): void;
40
40
  invalidateAll(): void;
41
41
  } {
42
- // NOTE: Not very efficient (invalidates too much), but... makes triggering invalidations much faster/easier,
43
- // making an invalidate depend on the total render time, not the total cached state size.
44
- let invalidateSeqNum = observable({ seqNum: 1 }, undefined, { deep: false, proxy: false });
42
+ let invalidateAllSeqNum = observable({ seqNum: 1 }, undefined, { deep: false, proxy: false });
45
43
  let startingCalculating = new Set<string>();
46
- let values = new Map<string, { value: Output | undefined }>();
44
+ let values = new Map<string, {
45
+ valueObs: { value: Output | undefined };
46
+ seqNum: { value: number };
47
+ }>();
47
48
 
48
49
  get["invalidate"] = (key: Key) => {
49
50
  let hash = JSON.stringify(key);
@@ -51,37 +52,43 @@ export function asyncObservable<Output, Key>(maxCount: number, getValue: (key: K
51
52
  if (!value) return;
52
53
  values.delete(hash);
53
54
  startingCalculating.delete(hash);
54
- invalidateSeqNum.seqNum++;
55
+ value.seqNum.value++;
55
56
  };
56
57
  get["invalidateAll"] = () => {
57
58
  startingCalculating.clear();
58
59
  values.clear();
59
- invalidateSeqNum.seqNum++;
60
+ invalidateAllSeqNum.seqNum++;
60
61
  };
61
62
  function get(key: Key) {
62
- invalidateSeqNum.seqNum;
63
+
63
64
  let hash = JSON.stringify(key);
64
65
  let value = values.get(hash);
65
- if (value) {
66
- return value.value;
67
- }
68
- if (startingCalculating.has(hash)) {
69
- throw new Error(`Cyclic access in cache`);
70
- }
71
- startingCalculating.add(hash);
66
+ if (!value) {
72
67
 
73
- // Not very efficient, but clearing the entire state is a lot easier to do then
74
- // keep track of the order they are accessed (and it does make MOST accesses
75
- // MUCH faster).
76
- if (values.size >= maxCount) {
77
- values.clear();
78
- startingCalculating.clear();
79
- }
68
+ if (startingCalculating.has(hash)) {
69
+ throw new Error(`Cyclic access in cache`);
70
+ }
71
+ startingCalculating.add(hash);
72
+
73
+ // Not very efficient, but clearing the entire state is a lot easier to do then
74
+ // keep track of the order they are accessed (and it does make MOST accesses
75
+ // MUCH faster).
76
+ if (values.size >= maxCount) {
77
+ values.clear();
78
+ startingCalculating.clear();
79
+ }
80
80
 
81
- // We call inside another function so that synchronous errors still get wrapped in the observable
82
- value = promiseToObservable((async () => getValue(key))());
83
- values.set(hash, value);
84
- return value.value;
81
+ // We call inside another function so that synchronous errors still get wrapped in the observable
82
+ let valueObs = promiseToObservable((async () => getValue(key))());
83
+ value = {
84
+ valueObs,
85
+ seqNum: observable({ value: 1 }, undefined, { deep: false, proxy: false }),
86
+ };
87
+ values.set(hash, value);
88
+ }
89
+ invalidateAllSeqNum.seqNum;
90
+ value.seqNum.value;
91
+ return value.valueObs.value;
85
92
  }
86
93
  return get;
87
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.8.15",
3
+ "version": "0.8.17",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -3,7 +3,7 @@ import http from "http";
3
3
  import net from "net";
4
4
  import tls from "tls";
5
5
  import { CallerContext, CallType, NetworkLocation, setCertInfo } from "../SocketFunctionTypes";
6
- import { performLocalCall } from "./callManager";
6
+ import { isDataImmutable, performLocalCall } from "./callManager";
7
7
  import { getNodeIdRaw } from "./nodeAuthentication";
8
8
  import debugbreak from "debugbreak";
9
9
  import * as cookie from "cookie";
@@ -150,6 +150,22 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
150
150
  args,
151
151
  };
152
152
 
153
+ if (isDataImmutable(call)) {
154
+ /** ETag cache, BUT, hashes only the input. Only valid for fully immutable resources
155
+ * (ex data storage, as any endpoints that run code could have that code change).
156
+ * - Shouldn't be needed, but I am seeing chrome fail to cache a lot of requests,
157
+ * which could cost us multiple dollars in server costs from atlas.
158
+ */
159
+ response.setHeader("cache-control", "public, max-age=15206400, immutable");
160
+ let hash = sha256Hash(Buffer.from(JSON.stringify(call)));
161
+ response.setHeader("ETag", hash);
162
+ if (request.headers["if-none-match"] === hash) {
163
+ response.writeHead(304);
164
+ console.log(`CACHED Immutable HTTP response (${request.method}) ${url}`);
165
+ return;
166
+ }
167
+ }
168
+
153
169
  let result = await performLocalCall({
154
170
  caller,
155
171
  call
@@ -174,9 +190,9 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
174
190
 
175
191
 
176
192
  // NOTE: Our ETag caching is only to reduce data sent on the wire, we evaluate the calls
177
- // every time (so it is strictly a wire cache, not computation cache)
193
+ // every time (so it is strictly a wire cache, not a computation cache)
178
194
  if (SocketFunction.httpETagCache) {
179
- response.setHeader("cache-control", "private, s-maxage=0, max-age=0, must-revalidate");
195
+ response.setHeader("cache-control", "private, max-age=0, must-revalidate");
180
196
  let hash = sha256Hash(resultBuffer);
181
197
  response.setHeader("ETag", hash);
182
198
  if (request.headers["if-none-match"] === hash) {
@@ -53,6 +53,10 @@ export async function performLocalCall(
53
53
  return await result;
54
54
  }
55
55
 
56
+ export function isDataImmutable(call: CallType) {
57
+ return !!classes[call.classGuid]?.shape[call.functionName]?.dataImmutable;
58
+ }
59
+
56
60
  export function registerClass(classGuid: string, controller: SocketExposedInterface, shape: SocketExposedShape) {
57
61
  if (classes[classGuid]) {
58
62
  throw new Error(`Class ${classGuid} already registered`);