UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

87 lines (86 loc) 3.77 kB
"use strict"; /** * Static Files Abstraction Layer * Dual compatibility: Node.js (Express) + Cloudflare Workers (Assets Fetcher) */ 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.serveStatic = serveStatic; exports.serveFile = serveFile; const express_1 = __importDefault(require("express")); const path_1 = __importDefault(require("path")); /** * Proxy Workers Assets response to Express response */ function proxyAssetsResponse(fetcher, assetPath, baseUrl, req, res, next) { return __awaiter(this, void 0, void 0, function* () { try { if (assetPath === '/') { return next(); } const assetUrl = new URL(assetPath, baseUrl); const response = yield fetcher.fetch(assetUrl.toString()); if (!response.ok) { return next(); } const body = yield response.arrayBuffer(); const contentType = response.headers.get('content-type'); if (contentType) { res.setHeader('Content-Type', contentType); } res.send(Buffer.from(body)); } catch (err) { next(err); } }); } /** * Serve static directory with dual compatibility * @param config - App configuration * @param staticPath - Path to static directory */ function serveStatic(config, staticPath) { var _a; if ((_a = config.assetsConfig) === null || _a === void 0 ? void 0 : _a.fetcher) { const assetsFetcher = config.assetsConfig.fetcher; return (req, res, next) => __awaiter(this, void 0, void 0, function* () { const assetPath = `/${staticPath}/${req.path}`.replace(/\/+/g, '/'); yield proxyAssetsResponse(assetsFetcher, assetPath, config.baseUrl, req, res, next); }); } const rootDir = path_1.default.dirname(__dirname); const absolutePath = path_1.default.isAbsolute(staticPath) ? staticPath : path_1.default.join(rootDir, staticPath); return express_1.default.static(absolutePath); } /** * Serve single file (e.g., logo.png, icons/star.svg) with dual compatibility * @param config - App configuration * @param filePath - Path to file */ function serveFile(config, filePath) { return (req, res, next) => __awaiter(this, void 0, void 0, function* () { var _a; if ((_a = config.assetsConfig) === null || _a === void 0 ? void 0 : _a.fetcher) { const assetsFetcher = config.assetsConfig.fetcher; const assetPath = filePath.startsWith('/') ? filePath : `/${filePath}`; yield proxyAssetsResponse(assetsFetcher, assetPath, config.baseUrl, req, res, next); } else { const rootDir = path_1.default.dirname(__dirname); const absolutePath = path_1.default.isAbsolute(filePath) ? filePath : path_1.default.join(rootDir, filePath); res.sendFile(absolutePath); } }); }