UNPKG

4.95 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.LoadingEndpoint = exports.DeepLinkEndpoint = void 0;
7exports.getLoadingPageHandler = getLoadingPageHandler;
8exports.noCacheMiddleware = noCacheMiddleware;
9exports.setOnDeepLink = setOnDeepLink;
10function _config() {
11 const data = require("@expo/config");
12 _config = function () {
13 return data;
14 };
15 return data;
16}
17function _Updates() {
18 const data = require("@expo/config-plugins/build/utils/Updates");
19 _Updates = function () {
20 return data;
21 };
22 return data;
23}
24function _fsExtra() {
25 const data = require("fs-extra");
26 _fsExtra = function () {
27 return data;
28 };
29 return data;
30}
31function _path() {
32 const data = require("path");
33 _path = function () {
34 return data;
35 };
36 return data;
37}
38function _url() {
39 const data = require("url");
40 _url = function () {
41 return data;
42 };
43 return data;
44}
45function _internal() {
46 const data = require("./../internal");
47 _internal = function () {
48 return data;
49 };
50 return data;
51}
52const LoadingEndpoint = '/_expo/loading';
53exports.LoadingEndpoint = LoadingEndpoint;
54const DeepLinkEndpoint = '/_expo/link';
55exports.DeepLinkEndpoint = DeepLinkEndpoint;
56let onDeepLink = async () => {};
57function setOnDeepLink(listener) {
58 onDeepLink = listener;
59}
60function getPlatform(query, userAgent = null) {
61 if (query['platform'] === 'android' || query['platform'] === 'ios') {
62 return query['platform'];
63 }
64 if (userAgent !== null && userAgent !== void 0 && userAgent.match(/Android/i)) {
65 return 'android';
66 }
67 if (userAgent !== null && userAgent !== void 0 && userAgent.match(/iPhone|iPad/i)) {
68 return 'ios';
69 }
70 return null;
71}
72function getRuntimeVersion(exp, platform) {
73 if (!platform) {
74 return null;
75 }
76 return (0, _Updates().getRuntimeVersionNullable)(exp, platform);
77}
78function noCacheMiddleware(res) {
79 res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
80 res.setHeader('Expires', '-1');
81 res.setHeader('Pragma', 'no-cache');
82 return res;
83}
84async function loadingEndpointHandler(projectRoot, req, res) {
85 var _getSDKVersion;
86 res.setHeader('Content-Type', 'text/html');
87 let content = (await (0, _fsExtra().readFile)((0, _path().resolve)(__dirname, './../../static/loading-page/index.html'))).toString('utf-8');
88 const {
89 exp
90 } = (0, _config().getConfig)(projectRoot, {
91 skipSDKVersionRequirement: true
92 });
93 const {
94 scheme
95 } = await _internal().ProjectSettings.readAsync(projectRoot);
96 const {
97 appName
98 } = (0, _config().getNameFromConfig)(exp);
99 const {
100 query
101 } = (0, _url().parse)(req.url, true);
102 const platform = getPlatform(query, req.headers['user-agent']);
103 const runtimeVersion = getRuntimeVersion(exp, platform);
104 content = content.replace(/{{\s*AppName\s*}}/, appName !== null && appName !== void 0 ? appName : 'App');
105 content = content.replace(/{{\s*ProjectVersionType\s*}}/, runtimeVersion ? 'Runtime version' : 'SDK version');
106 content = content.replace(/{{\s*ProjectVersion\s*}}/, runtimeVersion ? runtimeVersion : (_getSDKVersion = (0, _Updates().getSDKVersion)(exp)) !== null && _getSDKVersion !== void 0 ? _getSDKVersion : 'Undetected');
107 content = content.replace(/{{\s*Path\s*}}/, projectRoot);
108 content = content.replace(/{{\s*Scheme\s*}}/, scheme !== null && scheme !== void 0 ? scheme : 'Unknown');
109 res.end(content);
110}
111async function deeplinkEndpointHandler(projectRoot, req, res) {
112 const {
113 query
114 } = (0, _url().parse)(req.url, true);
115 const isDevClient = query['choice'] === 'expo-dev-client';
116 const projectUrl = isDevClient ? await _internal().UrlUtils.constructDevClientUrlAsync(projectRoot) : await _internal().UrlUtils.constructManifestUrlAsync(projectRoot);
117 res.setHeader('Location', projectUrl);
118 onDeepLink(projectRoot, isDevClient, getPlatform(query, req.headers['user-agent']));
119 res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
120 res.setHeader('Expires', '-1');
121 res.setHeader('Pragma', 'no-cache');
122 res.statusCode = 307;
123 res.end();
124}
125function getLoadingPageHandler(projectRoot) {
126 return async (req, res, next) => {
127 if (!req.url) {
128 next();
129 return;
130 }
131 try {
132 const url = (0, _url().parse)(req.url).pathname || req.url;
133 switch (url) {
134 case LoadingEndpoint:
135 await loadingEndpointHandler(projectRoot, req, noCacheMiddleware(res));
136 break;
137 case DeepLinkEndpoint:
138 await deeplinkEndpointHandler(projectRoot, req, noCacheMiddleware(res));
139 break;
140 default:
141 next();
142 }
143 } catch (exception) {
144 res.statusCode = 520;
145 if (typeof exception == 'object' && exception != null) {
146 res.end(JSON.stringify({
147 error: exception.toString()
148 }));
149 } else {
150 res.end(`Unexpected error: ${exception}`);
151 }
152 }
153 };
154}
155//# sourceMappingURL=LoadingPageHandler.js.map
\No newline at end of file