You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.8 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.LoadingEndpoint = exports.DeepLinkEndpoint = void 0;
exports.getLoadingPageHandler = getLoadingPageHandler;
exports.noCacheMiddleware = noCacheMiddleware;
exports.setOnDeepLink = setOnDeepLink;
function _config() {
const data = require("@expo/config");
_config = function () {
return data;
};
return data;
}
function _Updates() {
const data = require("@expo/config-plugins/build/utils/Updates");
_Updates = function () {
return data;
};
return data;
}
function _fsExtra() {
const data = require("fs-extra");
_fsExtra = function () {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function () {
return data;
};
return data;
}
function _url() {
const data = require("url");
_url = function () {
return data;
};
return data;
}
function _internal() {
const data = require("./../internal");
_internal = function () {
return data;
};
return data;
}
const LoadingEndpoint = '/_expo/loading';
exports.LoadingEndpoint = LoadingEndpoint;
const DeepLinkEndpoint = '/_expo/link';
exports.DeepLinkEndpoint = DeepLinkEndpoint;
let onDeepLink = async () => {};
function setOnDeepLink(listener) {
onDeepLink = listener;
}
function getPlatform(query, userAgent = null) {
if (query['platform'] === 'android' || query['platform'] === 'ios') {
return query['platform'];
}
if (userAgent !== null && userAgent !== void 0 && userAgent.match(/Android/i)) {
return 'android';
}
if (userAgent !== null && userAgent !== void 0 && userAgent.match(/iPhone|iPad/i)) {
return 'ios';
}
return null;
}
function getRuntimeVersion(exp, platform) {
if (!platform) {
return null;
}
return (0, _Updates().getRuntimeVersionNullable)(exp, platform);
}
function noCacheMiddleware(res) {
res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.setHeader('Expires', '-1');
res.setHeader('Pragma', 'no-cache');
return res;
}
async function loadingEndpointHandler(projectRoot, req, res) {
var _getSDKVersion;
res.setHeader('Content-Type', 'text/html');
let content = (await (0, _fsExtra().readFile)((0, _path().resolve)(__dirname, './../../static/loading-page/index.html'))).toString('utf-8');
const {
exp
} = (0, _config().getConfig)(projectRoot, {
skipSDKVersionRequirement: true
});
const {
scheme
} = await _internal().ProjectSettings.readAsync(projectRoot);
const {
appName
} = (0, _config().getNameFromConfig)(exp);
const {
query
} = (0, _url().parse)(req.url, true);
const platform = getPlatform(query, req.headers['user-agent']);
const runtimeVersion = getRuntimeVersion(exp, platform);
content = content.replace(/{{\s*AppName\s*}}/, appName !== null && appName !== void 0 ? appName : 'App');
content = content.replace(/{{\s*ProjectVersionType\s*}}/, runtimeVersion ? 'Runtime version' : 'SDK version');
content = content.replace(/{{\s*ProjectVersion\s*}}/, runtimeVersion ? runtimeVersion : (_getSDKVersion = (0, _Updates().getSDKVersion)(exp)) !== null && _getSDKVersion !== void 0 ? _getSDKVersion : 'Undetected');
content = content.replace(/{{\s*Path\s*}}/, projectRoot);
content = content.replace(/{{\s*Scheme\s*}}/, scheme !== null && scheme !== void 0 ? scheme : 'Unknown');
res.end(content);
}
async function deeplinkEndpointHandler(projectRoot, req, res) {
const {
query
} = (0, _url().parse)(req.url, true);
const isDevClient = query['choice'] === 'expo-dev-client';
const projectUrl = isDevClient ? await _internal().UrlUtils.constructDevClientUrlAsync(projectRoot) : await _internal().UrlUtils.constructManifestUrlAsync(projectRoot);
res.setHeader('Location', projectUrl);
onDeepLink(projectRoot, isDevClient, getPlatform(query, req.headers['user-agent']));
res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.setHeader('Expires', '-1');
res.setHeader('Pragma', 'no-cache');
res.statusCode = 307;
res.end();
}
function getLoadingPageHandler(projectRoot) {
return async (req, res, next) => {
if (!req.url) {
next();
return;
}
try {
const url = (0, _url().parse)(req.url).pathname || req.url;
switch (url) {
case LoadingEndpoint:
await loadingEndpointHandler(projectRoot, req, noCacheMiddleware(res));
break;
case DeepLinkEndpoint:
await deeplinkEndpointHandler(projectRoot, req, noCacheMiddleware(res));
break;
default:
next();
}
} catch (exception) {
res.statusCode = 520;
if (typeof exception == 'object' && exception != null) {
res.end(JSON.stringify({
error: exception.toString()
}));
} else {
res.end(`Unexpected error: ${exception}`);
}
}
};
}
//# sourceMappingURL=LoadingPageHandler.js.map