UNPKG

@codesandbox/api

Version:
46 lines 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ServerApi = void 0; const tslib_1 = require("tslib"); const universal_cookie_1 = tslib_1.__importDefault(require("universal-cookie")); const constants_1 = require("./constants"); class ServerApi { constructor(options) { this.clientType = options.clientType; } /** * On server side we need to produce headers passed to any REST/GQL request based * on client headers and cookies. As we do not want to create a CodeSandboxApi instance * for every request, we rather allow to create these headers and pass them to each request */ createServerRequestHeaders(clientHeaders, clientCookies) { const headers = {}; if (clientCookies.devJwt) { headers.Authorization = `Bearer ${clientCookies.devJwt}`; } if (clientHeaders.cookie) { headers.Cookie = clientHeaders.cookie; } // Pass the country header in the request injected by Cloudflare to the API server // This is required by global scheduler to decide the cluster for scheduling const countryFromIp = clientHeaders["x-forwarded-cf-ipcountry"] || clientHeaders["cf-ipcountry"]; if (countryFromIp) { headers["x-forwarded-cf-ipcountry"] = countryFromIp; } headers["x-codesandbox-client"] = this.clientType; return headers; } /** * Expect to get the headers created by "createServerRequestHeaders" */ hasSignedIn(serverRequestHeaders) { if (serverRequestHeaders.Cookie) { const cookies = new universal_cookie_1.default(serverRequestHeaders.Cookie); return Boolean(cookies.get(constants_1.COOKIES.SIGNED_IN)); } return false; } } exports.ServerApi = ServerApi; //# sourceMappingURL=ServerApi.js.map