fix: ensure proxy-data is cached correctly

This commit is contained in:
Alexis Tyler
2020-06-06 14:33:02 +09:30
parent ae6f858360
commit c28282df76
7 changed files with 113 additions and 18 deletions

View File

@@ -1,3 +0,0 @@
import NodeCache from 'node-cache';
export const cache = new NodeCache();

1
app/cache/index.ts vendored Normal file
View File

@@ -0,0 +1 @@
export * from './user';

25
app/cache/user.ts vendored Normal file
View File

@@ -0,0 +1,25 @@
import NodeCache from 'node-cache';
export const userCache = new NodeCache();
type URL = string;
type IpAddress = string;
type Status = 'online' | 'offline';
export interface CachedUser {
profile: {
username: string;
url: URL;
avatar: URL;
},
servers: [{
guid: string;
apikey: string;
name: string;
status: Status;
wanip: IpAddress;
lanip: IpAddress;
localurl: URL;
remoteurl: string
}]
};

View File

@@ -10,7 +10,7 @@ import GraphQLJSON from 'graphql-type-json';
import GraphQLLong from 'graphql-type-long';
import GraphQLUUID from 'graphql-type-uuid';
import { run, publish } from '../../run';
import { cache } from '../../cache';
import { userCache } from '../../cache';
import { hasSubscribedToChannel, canPublishToChannel } from '../../ws';
const { ensurePermission } = utils;
@@ -97,8 +97,62 @@ export const resolvers = {
// return cache.get();
},
servers() {
const mockData = [{
online: true,
info: {
machineId: '38ed954556087cd0921d3ce69adaa57f',
apps: {
installed: 100,
started: 1
},
versions: {
unraid: '6.9.0-beta12'
},
os: {
hostname: 'tower'
}
},
array: {
state: 'started',
capacity: {
bytes: {
free: 1222900940800,
total: 1347826987008,
used: 124926046208
}
}
},
services: [
{
name: 'emhttpd',
online: true,
version: null
},
{
name: 'rest-api',
online: false,
version: null
},
{
name: 'graphql-api',
online: true,
version: '2.4.9'
},
{
name: 'plugins',
online: null,
version: '1.1.5'
}
],
my_servers: {
url: 'case-model.png',
serverCase: 'custom'
}
}];
// All servers
return cache.data;
return userCache.get('mine');
// return cache.data;
}
},
Subscription: {

View File

@@ -1,6 +1,6 @@
type Query {
server(name: String!): Server @func(module: "servers/name/get-server")
servers: [Server] @func(module: "getServers")
server(name: String!): Server
servers: [JSON]
}
type ServerSubscription {
@@ -19,10 +19,10 @@ type Subscription {
# type Status = 'Online' | 'Offline';
type Server {
name: String!
status: String!
wanIp: String!
localUrl: String!
remoteUrl: String!
}
# type Server {
# name: String!
# status: String!
# wanIp: String!
# localUrl: String!
# remoteUrl: String!
# }

View File

@@ -17,6 +17,7 @@ import { log, config, utils, paths, states } from '@unraid/core';
import { DynamixConfig } from '@unraid/core/dist/lib/types';
import { createServer } from './patched-install-subscription-handlers';
import { graphql } from './graphql';
import { userCache, CachedUser } from './cache';
const { getEndpoints, globalErrorHandler, exitApp, loadState } = utils;
const { varState } = states;
@@ -260,11 +261,12 @@ const connectToMothership = async (currentRetryAttempt: number = 0) => {
mothership.on('ping', heartbeat);
interface Message {
type: 'query' | 'mutation' | 'start' | 'stop';
type: 'query' | 'mutation' | 'start' | 'stop' | 'proxy-data';
payload: {
operationName: any;
variables: {};
query: string;
data: any;
}
};
@@ -305,6 +307,22 @@ const connectToMothership = async (currentRetryAttempt: number = 0) => {
}
});
}
const isUserObject = (x): x is CachedUser => {
const keys = Object.keys(data);
return keys.includes('profile') && keys.includes('servers') && keys.length === 2
};
if (message.type === 'proxy-data') {
const { data } = message.payload;
// Cache the response
if (isUserObject(data)) {
userCache.set('mine', data);
return;
}
}
} catch (error) {
// Something weird happened while processing the message
// This is likely a malformed message

6
package-lock.json generated
View File

@@ -926,7 +926,7 @@
"from": "github:unraid/core",
"requires": {
"accesscontrol": "^2.2.1",
"apollo-server": "^2.14.1",
"apollo-server": "^2.14.2",
"async-exit-hook": "^2.0.1",
"better-stack-traces": "^1.1.0",
"bycontract": "2.0.9",
@@ -948,7 +948,7 @@
"fromentries": "^1.2.0",
"get-server-address": "^1.0.1",
"glob": "^7.1.6",
"globby": "^11.0.0",
"globby": "^11.0.1",
"graphql": "14.6.0",
"htpasswd-js": "^1.0.2",
"ini": "^1.3.5",
@@ -979,7 +979,7 @@
"sendmail": "1.6.1",
"spread-the-word": "0.8.4",
"stoppable": "1.1.0",
"systeminformation": "^4.26.5",
"systeminformation": "^4.26.6",
"tracer": "^1.1.2",
"upcast": "^4.0.0",
"uuid": "8.1.0",