-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevToolsServer.js
More file actions
243 lines (243 loc) · 9.8 KB
/
DevToolsServer.js
File metadata and controls
243 lines (243 loc) · 9.8 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const xdl_1 = require("@expo/xdl");
const subscriptions_transport_ws_1 = require("subscriptions-transport-ws");
// @ts-ignore
const graphql = __importStar(require("graphql"));
const express_1 = __importDefault(require("express"));
const freeport_async_1 = __importDefault(require("freeport-async"));
const path_1 = __importDefault(require("path"));
const http_1 = __importDefault(require("http"));
const crypto_1 = __importDefault(require("crypto"));
const base64url_1 = __importDefault(require("base64url"));
const AsyncIterableRingBuffer_1 = __importDefault(require("./graphql/AsyncIterableRingBuffer"));
const GraphQLSchema_1 = __importDefault(require("./graphql/GraphQLSchema"));
const createContext_1 = __importStar(require("./graphql/createContext"));
const Issues_1 = __importDefault(require("./graphql/Issues"));
const serverStartTimeUTCString = new Date().toUTCString();
function setHeaders(res) {
// Set the Last-Modified header to server start time because otherwise it
// becomes Sat, 26 Oct 1985 08:15:00 GMT for files installed from npm.
res.setHeader('Last-Modified', serverStartTimeUTCString);
}
function generateSecureRandomTokenAsync() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
crypto_1.default.randomBytes(32, (error, buffer) => {
if (error)
reject(error);
else
resolve(base64url_1.default.fromBase64(buffer.toString('base64')));
});
});
});
}
function createAuthenticationContextAsync({ port }) {
return __awaiter(this, void 0, void 0, function* () {
const clientAuthenticationToken = yield generateSecureRandomTokenAsync();
const endpointUrlToken = yield generateSecureRandomTokenAsync();
const graphQLEndpointPath = `/${endpointUrlToken}/graphql`;
const hostname = `${devtoolsGraphQLHost()}:${port}`;
const webSocketGraphQLUrl = `ws://${hostname}${graphQLEndpointPath}`;
const allowedOrigin = `http://${hostname}`;
return {
clientAuthenticationToken,
graphQLEndpointPath,
webSocketGraphQLUrl,
allowedOrigin,
requestHandler: (request, response) => {
response.json({ webSocketGraphQLUrl, clientAuthenticationToken });
},
};
});
}
exports.createAuthenticationContextAsync = createAuthenticationContextAsync;
function startAsync(projectDir) {
return __awaiter(this, void 0, void 0, function* () {
const port = yield freeport_async_1.default(19002, { hostnames: [null, 'localhost'] });
const server = express_1.default();
const authenticationContext = yield createAuthenticationContextAsync({ port });
server.get('/dev-tools-info', authenticationContext.requestHandler);
server.use('/_next', express_1.default.static(path_1.default.join(__dirname, '../client/_next'), {
// All paths in the _next folder include hashes, so they can be cached more aggressively.
immutable: true,
maxAge: '1y',
setHeaders,
}));
server.use(express_1.default.static(path_1.default.join(__dirname, '../client'), { setHeaders }));
const listenHostname = devtoolsHost();
const httpServer = http_1.default.createServer(server);
yield new Promise((resolve, reject) => {
httpServer.once('error', reject);
httpServer.once('listening', resolve);
httpServer.listen(port, listenHostname);
});
startGraphQLServer(projectDir, httpServer, authenticationContext);
yield xdl_1.ProjectSettings.setPackagerInfoAsync(projectDir, { devToolsPort: port });
return `http://${listenHostname}:${port}`;
});
}
exports.startAsync = startAsync;
function startGraphQLServer(projectDir, httpServer, authenticationContext) {
const layout = createLayout();
const issues = new Issues_1.default();
const messageBuffer = createMessageBuffer(projectDir, issues);
subscriptions_transport_ws_1.SubscriptionServer.create({
schema: GraphQLSchema_1.default,
execute: graphql.execute,
subscribe: graphql.subscribe,
onOperation: (operation, params) => (Object.assign(Object.assign({}, params), { context: createContext_1.default({
projectDir,
messageBuffer,
layout,
issues,
}) })),
onConnect: connectionParams => {
if (!connectionParams.clientAuthenticationToken ||
connectionParams.clientAuthenticationToken !==
authenticationContext.clientAuthenticationToken) {
throw new Error('Dev Tools API authentication failed.');
}
return true;
},
}, {
server: httpServer,
path: authenticationContext.graphQLEndpointPath,
verifyClient: info => {
return info.origin === authenticationContext.allowedOrigin;
},
});
}
exports.startGraphQLServer = startGraphQLServer;
function devtoolsHost() {
if (process.env.EXPO_DEVTOOLS_LISTEN_ADDRESS) {
return process.env.EXPO_DEVTOOLS_LISTEN_ADDRESS.trim();
}
return 'localhost';
}
function devtoolsGraphQLHost() {
if (process.env.EXPO_DEVTOOLS_LISTEN_ADDRESS && process.env.REACT_NATIVE_PACKAGER_HOSTNAME) {
return process.env.REACT_NATIVE_PACKAGER_HOSTNAME.trim();
}
else if (process.env.EXPO_DEVTOOLS_LISTEN_ADDRESS) {
return process.env.EXPO_DEVTOOLS_LISTEN_ADDRESS;
}
return 'localhost';
}
function createLayout() {
let layout = {
selected: null,
sources: null,
sourceLastReads: {},
};
return {
get() {
return layout;
},
set(newLayout) {
layout = Object.assign(Object.assign({}, layout), newLayout);
},
setLastRead(sourceId, lastReadCursor) {
layout.sourceLastReads[sourceId] = lastReadCursor;
},
};
}
function createMessageBuffer(projectRoot, issues) {
const buffer = new AsyncIterableRingBuffer_1.default(10000);
// eslint-disable-next-line no-new
new xdl_1.PackagerLogsStream({
projectRoot,
updateLogs: updater => {
const chunks = updater([]);
chunks.forEach(chunk => {
if (chunk.issueId) {
if (chunk.issueCleared) {
issues.clearIssue(chunk.issueId);
}
else {
issues.addIssue(chunk.issueId, chunk);
}
return;
}
buffer.push({
type: 'ADDED',
sourceId: createContext_1.PROCESS_SOURCE.id,
node: chunk,
});
});
},
onStartBuildBundle: chunk => {
buffer.push({
type: 'ADDED',
sourceId: createContext_1.PROCESS_SOURCE.id,
node: Object.assign(Object.assign({}, chunk), { progress: 0, duration: 0 }),
});
},
onProgressBuildBundle: (percentage, start, chunk) => {
buffer.push({
type: 'UPDATED',
sourceId: createContext_1.PROCESS_SOURCE.id,
node: Object.assign(Object.assign({}, chunk), { progress: percentage,
// @ts-ignore
duration: new Date() - (start || new Date()) }),
});
},
onFinishBuildBundle: (error, start, end, chunk) => {
buffer.push({
type: 'UPDATED',
sourceId: createContext_1.PROCESS_SOURCE.id,
node: Object.assign(Object.assign({}, chunk), { error,
// @ts-ignore
duration: end - (start || new Date()) }),
});
},
});
// needed for validation logging to function
xdl_1.ProjectUtils.attachLoggerStream(projectRoot, {
stream: {
write: chunk => {
if (chunk.tag === 'device') {
buffer.push({
type: 'ADDED',
sourceId: chunk.deviceId,
node: chunk,
});
}
},
},
type: 'raw',
});
xdl_1.Logger.global.addStream({
stream: {
write: chunk => {
buffer.push({
type: 'ADDED',
sourceId: createContext_1.PROCESS_SOURCE.id,
node: chunk,
});
},
},
type: 'raw',
});
return buffer;
}
//# sourceMappingURL=DevToolsServer.js.map