MCPcopy
hub / github.com/aspen-cloud/triplit / createServer

Function createServer

packages/server/src/legacy-node-server.ts:89–590  ·  view source on GitHub ↗
(options?: ServerOptions)

Source from the content-addressed store, hash-verified

87}
88
89export function createServer(options?: ServerOptions) {
90 const dbSource = !!options?.storage
91 ? typeof options.storage === 'string'
92 ? createTriplitStorageProvider(
93 // @ts-ignore TODO: check why this is not working...might be module resolution issue?
94 options.storage
95 )
96 : typeof options.storage === 'function'
97 ? options.storage()
98 : options.storage
99 : undefined;
100 if (options?.verboseLogs) logger.verbose = true;
101 const triplitServers = new Map<string, TriplitServer>();
102
103 function getServer(projectId: string, upstream?: ServerOptions['upstream']) {
104 if (triplitServers.has(projectId)) return triplitServers.get(projectId)!;
105 const dbOptions: Partial<DBConfig> = {
106 experimental: {},
107 };
108 if (process.env.ENTITY_CACHE_ENABLED) {
109 dbOptions.experimental!.entityCache = {
110 capacity: process.env.ENTITY_CACHE_CAPACITY
111 ? parseInt(process.env.ENTITY_CACHE_CAPACITY)
112 : 100000,
113 };
114 }
115 Object.assign(dbOptions, options?.dbOptions);
116 const db = upstream
117 ? new TriplitClient({
118 clientId: projectId,
119 serverUrl: upstream.url,
120 token: upstream.token,
121 syncSchema: true,
122 skipRules: false,
123 })
124 : new DB({
125 source: dbSource,
126 tenantId: projectId,
127 clock: new DurableClock(),
128 ...dbOptions,
129 });
130 // @ts-expect-error
131 const server = new TriplitServer(db, captureException);
132 triplitServers.set(projectId, server);
133 return server;
134 }
135 initSentry();
136 const app = express();
137 app.use(express.json());
138 app.use((req, res, next) => {
139 const start = new Date();
140 let send = res.send;
141 res.send = (c) => {
142 const end = new Date();
143 let body = c;
144 const resWithBody = {
145 ...res,
146 body,

Callers

nothing calls this directly

Calls 15

parseAndValidateTokenFunction · 0.90
getRolesFromSessionFunction · 0.90
normalizeSessionVarsFunction · 0.90
hasAdminAccessFunction · 0.90
nextFunction · 0.85
getServerFunction · 0.85
readWSTokenFunction · 0.85
logRequestAndResponseMethod · 0.80
addListenerMethod · 0.80
logMessageMethod · 0.80

Tested by

no test coverage detected