-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-schema-ui-server.js
More file actions
118 lines (107 loc) · 3.15 KB
/
Copy pathstart-schema-ui-server.js
File metadata and controls
118 lines (107 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import http from 'node:http';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { createDbRuntime } from '../../../dist/index.js';
import { serializeError } from '../../../dist/errors.js';
import { sendJson } from '../../../dist/rest/handler.js';
import { handleSchemaUiSsrRequest } from './schema-ui-ssr-handler.js';
/**
* Schema UI demo runtime: SSR CMS routes composed ahead of the stock db REST / explorer stack.
*
* @param {{ cwd: string; basePath?: string; skipSync?: boolean }} options
*/
export async function createSchemaUiRuntime(options) {
const {
cwd,
basePath = '',
skipSync = false,
} = options;
const normalizedBasePath = normalizeBasePath(basePath);
const runtime = await createDbRuntime({
cwd,
allowSourceErrors: true,
syncOnOpen: !skipSync,
handler: {
rootRoutes: true,
apiBase: joinPaths(normalizedBasePath, '/__db'),
dataPath: joinPaths(normalizedBasePath, '/db'),
graphqlPath: joinPaths(normalizedBasePath, '/graphql'),
},
});
const db = runtime.db;
const manifestUrl = pathToFileURL(path.join(cwd, 'src/generated/db.schema.json'));
let closed = false;
return {
db,
async handleRequest(request, response) {
try {
const handled = await handleSchemaUiSsrRequest(request, response, {
cwd,
db,
basePath: normalizedBasePath,
manifestUrl,
});
if (handled) {
return;
}
await runtime.handleRequest(request, response);
} catch (error) {
sendJson(response, error.status ?? 500, serializeError(error, 'SERVER_ERROR'));
}
},
async close() {
if (closed) {
return;
}
closed = true;
await runtime.close();
},
};
}
function normalizeBasePath(basePath) {
if (!basePath || basePath === '/') {
return '';
}
return `/${String(basePath).replace(/^\/+|\/+$/gu, '')}`;
}
function joinPaths(basePath, childPath) {
const normalizedBase = normalizeBasePath(basePath);
const normalizedChild = `/${String(childPath).replace(/^\/+/u, '')}`;
return `${normalizedBase}${normalizedChild}`;
}
/**
* Backward-compatible standalone HTTP server wrapper for this example.
*
* @param {{ cwd: string; host?: string; port: number; skipSync?: boolean }} options
*/
export async function startSchemaUiServer(options) {
const {
host = '127.0.0.1',
port,
} = options;
const runtime = await createSchemaUiRuntime(options);
const server = http.createServer((request, response) => {
runtime.handleRequest(request, response).catch((error) => {
sendJson(response, error.status ?? 500, serializeError(error, 'SERVER_ERROR'));
});
});
server.once('close', () => {
void runtime.close();
});
try {
await new Promise((resolve, reject) => {
server.once('error', reject);
server.listen(port, host, resolve);
});
} catch (error) {
await runtime.close();
throw error;
}
const address = server.address();
const boundPort = address && typeof address === 'object' ? address.port : port;
return {
server,
db: runtime.db,
url: `http://${host}:${boundPort}`,
};
}