socket-function 0.6.0 → 0.7.0

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/SocketFunction.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { SocketExposedInterface, CallContextType, SocketFunctionHook, SocketFunctionClientHook, SocketExposedShape, SocketRegistered, NetworkLocation, CallerContext, SocketExposedInterfaceClass, CallType } from "./SocketFunctionTypes";
2
2
  import { exposeClass, registerClass, registerGlobalClientHook, registerGlobalHook, runClientHooks } from "./src/callManager";
3
3
  import { SocketServerConfig, startSocketServer } from "./src/socketServer";
4
- import { getCallFactoryNodeId, getCreateCallFactoryLocation } from "./src/nodeCache";
4
+ import { getCallFactoryNodeId, getCreateCallFactoryLocation, getNetworkLocationHash } from "./src/nodeCache";
5
5
  import { getCallProxy } from "./src/nodeProxy";
6
6
  import { Args } from "./src/types";
7
7
  import { setDefaultHTTPCall } from "./src/callHTTPHandler";
@@ -46,9 +46,9 @@ export class SocketFunction {
46
46
  registerClass(classGuid, instance as SocketExposedInterface, shape as any as SocketExposedShape);
47
47
 
48
48
  let nodeProxy = getCallProxy(classGuid, async (nodeId, functionName, args) => {
49
- let callFactory = getCallFactoryNodeId(nodeId);
49
+ let callFactory = await getCallFactoryNodeId(nodeId);
50
50
  if (!callFactory) {
51
- throw new Error(`Cannot reach node ${nodeId}. Either it was established via an HTTP call, or was incorrect provided to us via another node, which should have provided us a NetworkLocation instead.`);
51
+ throw new Error(`Cannot reach node ${nodeId}. It might have been incorrect provided to us via another node, which should have provided us a NetworkLocation instead.`);
52
52
  }
53
53
 
54
54
  let shapeObj = shape[functionName];
@@ -123,6 +123,20 @@ export class SocketFunction {
123
123
  return await getCreateCallFactoryLocation(location);
124
124
  }
125
125
 
126
+ public static connectSync(location: NetworkLocation | { address: string; port: number }): string {
127
+ if (!("localPort" in location)) {
128
+ location = {
129
+ address: location.address,
130
+ listeningPorts: [location.port],
131
+ localPort: 0,
132
+ };
133
+ }
134
+ let tempNodeId = "syncTempNodeId_" + getNetworkLocationHash(location);
135
+
136
+ void getCreateCallFactoryLocation(location, tempNodeId);
137
+
138
+ return tempNodeId;
139
+ }
126
140
 
127
141
  public static addGlobalHook<CallContext extends CallContextType>(hook: SocketFunctionHook<SocketExposedInterface, CallContext>) {
128
142
  registerGlobalHook(hook as SocketFunctionHook);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -144,7 +144,6 @@ class RequireControllerBase {
144
144
  let moduleObj = modules[module.filename];
145
145
  if (moduleObj.allowclient) {
146
146
  moduleObj.source = module.moduleContents;
147
- // TODO: Check timestamps on .json file and get most recent value
148
147
  if (module.filename.endsWith(".json") && !moduleObj.source) {
149
148
  moduleObj.source = module.moduleContents = fs.readFileSync(module.filename).toString();
150
149
  }
package/spec.txt CHANGED
@@ -11,6 +11,15 @@ spec.txt
11
11
  certs very easy.
12
12
  - ALTHOUGH, maybe this should just be a downstream user of socket-function thing, and not
13
13
  a requirement of socket-function?
14
+ - This will be something where machines ask a server for an identity, and then the owner
15
+ allows it (giving them a subdomain), verifying their ip, etc.
16
+ - MAYBE we just automatically allow it? This works if we never use the root domain for anything,
17
+ and only use subdomains?
18
+ - Although, of course, sometimes we WILL want to group by domain, so it is more than just
19
+ identity, so having a way for the owner to authenticate it might be nice as well...
20
+ - Once they are given an identity, they are allowed to request their certs are updated,
21
+ and to get the new certs (it probably won't happen automatically, so that we can create
22
+ many nodes and let them die, without having to try to track which still need certs to be updated)
14
23
  - ALSO, remember, for domains, use the domain as the nodeId, not the public key, that way it
15
24
  is more consistent.
16
25
 
package/src/nodeCache.ts CHANGED
@@ -14,11 +14,11 @@ import { MaybePromise } from "./types";
14
14
 
15
15
  // nodeId =>
16
16
  const nodeCache = new Map<string, {
17
- callFactory: CallFactory;
17
+ callFactory: MaybePromise<CallFactory>;
18
18
  }>();
19
19
  const locationLookup = new Map<string, MaybePromise<string>>();
20
20
 
21
- function getNetworkLocationHash(location: NetworkLocation): string {
21
+ export function getNetworkLocationHash(location: NetworkLocation): string {
22
22
  return location.address + ":" + location.localPort + "=" + location.listeningPorts.join("|");
23
23
  }
24
24
 
@@ -37,9 +37,12 @@ export function registerNodeClient(callFactory: CallFactory) {
37
37
  // Never go from listening ports to no listening ports. Worst case the listening ports are old
38
38
  // and won't work, in which case... we won't be able to reconnect, which basically what
39
39
  // we would do if there were no listening ports.
40
- let prevListeningPorts = nodeCache.get(nodeId)?.callFactory.location.listeningPorts;
41
- if (prevListeningPorts && !callFactory.location.listeningPorts.length) {
42
- callFactory.location.listeningPorts = prevListeningPorts;
40
+ let prevFactory = nodeCache.get(nodeId)?.callFactory;
41
+ if (prevFactory && !(prevFactory instanceof Promise)) {
42
+ let prevListeningPorts = prevFactory.location.listeningPorts;
43
+ if (prevListeningPorts && !callFactory.location.listeningPorts.length) {
44
+ callFactory.location.listeningPorts = prevListeningPorts;
45
+ }
43
46
  }
44
47
  // TODO: Maybe even preserve the address in some cases, such as if it was a domain, and is now an ip?
45
48
  nodeCache.set(nodeId, {
@@ -47,7 +50,7 @@ export function registerNodeClient(callFactory: CallFactory) {
47
50
  });
48
51
  }
49
52
 
50
- export function getCreateCallFactoryLocation(location: NetworkLocation): MaybePromise<string> {
53
+ export function getCreateCallFactoryLocation(location: NetworkLocation, tempNodeId?: string): MaybePromise<string> {
51
54
  let locationHash = getNetworkLocationHash(location);
52
55
  let nodeId = locationLookup.get(locationHash);
53
56
  if (nodeId !== undefined) {
@@ -58,12 +61,22 @@ export function getCreateCallFactoryLocation(location: NetworkLocation): MaybePr
58
61
  let nodeIdPromise = callFactoryPromise.then(x => x.nodeId);
59
62
  locationLookup.set(locationHash, nodeIdPromise);
60
63
 
64
+ if (tempNodeId !== undefined) {
65
+ nodeCache.set(tempNodeId, {
66
+ callFactory: callFactoryPromise,
67
+ });
68
+ }
69
+
61
70
  return callFactoryPromise.then(callFactory => {
62
71
  let nodeId = callFactory.nodeId;
63
72
  // TODO: Maybe warn if we just clobbered a nodeId?
64
73
  let prevEntry = nodeCache.get(nodeId);
65
74
  if (prevEntry) {
66
- console.warn(`Clobbering nodeId ${nodeId}, with a new location ${locationHash}, was ${getNetworkLocationHash(prevEntry.callFactory.location)}. (This might indiciate multiple locations with the same nodeId, which could cause an issue. If this happens repeatedly it will cause stability issues).`);
75
+ if (prevEntry.callFactory instanceof Promise) {
76
+ console.warn(`Clobbering nodeId ${nodeId}, with a new location ${locationHash}, which was still resolving. (This might indiciate multiple locations with the same nodeId, which could cause an issue. If this happens repeatedly it will cause stability issues).`);
77
+ } else {
78
+ console.warn(`Clobbering nodeId ${nodeId}, with a new location ${locationHash}, was ${getNetworkLocationHash(prevEntry.callFactory.location)}. (This might indiciate multiple locations with the same nodeId, which could cause an issue. If this happens repeatedly it will cause stability issues).`);
79
+ }
67
80
  }
68
81
  nodeCache.set(nodeId, {
69
82
  callFactory,
@@ -74,6 +87,6 @@ export function getCreateCallFactoryLocation(location: NetworkLocation): MaybePr
74
87
 
75
88
 
76
89
  // TODO: Give a special error if the nodeId has been seen, but is only one-way (from HTTP requests).
77
- export function getCallFactoryNodeId(nodeId: string): CallFactory | undefined {
78
- return nodeCache.get(nodeId)?.callFactory;
90
+ export async function getCallFactoryNodeId(nodeId: string): Promise<CallFactory | undefined> {
91
+ return await nodeCache.get(nodeId)?.callFactory;
79
92
  }
package/src/nodeProxy.ts CHANGED
@@ -1,10 +1,6 @@
1
1
  import { lazy } from "./caching";
2
2
  import { SocketExposedInterface } from "../SocketFunctionTypes";
3
3
 
4
- //todonext
5
- // We need some sort of cache, but... maybe not here?
6
- // Proxy => callback?
7
-
8
4
  type CallProxyType = {
9
5
  [controllerName: string]: SocketExposedInterface;
10
6
  };