@forge/bundler
7.1.1-next.17.1.1-next.1-experimental-0b00ed8
out/wrapper-provider.js~
out/wrapper-provider.jsModified+48−5
Index: package/out/wrapper-provider.js
===================================================================
--- package/out/wrapper-provider.js
+++ package/out/wrapper-provider.js
@@ -2,10 +2,12 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWrapperProvider = exports.NetworkWrapperProvider = exports.LocalWrapperProvider = exports.ParseWrapperCDNIndexError = exports.WrapperNetworkError = exports.LocalWrapperNotFoundError = void 0;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
+const http_1 = tslib_1.__importDefault(require("http"));
+const https_1 = tslib_1.__importDefault(require("https"));
+const url_1 = require("url");
const cheerio_1 = require("cheerio");
-const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
const cli_shared_1 = require("@forge/cli-shared");
var RuntimeCDN;
(function (RuntimeCDN) {
RuntimeCDN["DEV"] = "https://forge-node-runtime.stg-east.frontend.public.atl-paas.net/";
@@ -36,8 +38,32 @@
(function (ScriptType) {
ScriptType["WRAPPER"] = "wrapper";
ScriptType["LOADER"] = "loader";
})(ScriptType || (ScriptType = {}));
+const NODE_REQUEST_TIMEOUT_MS = 30_000;
+const DEFAULT_HTTP_ERROR_STATUS = 500;
+const toHeaders = (rawHeaders) => {
+ const responseHeaders = new Headers();
+ Object.entries(rawHeaders).forEach(([key, value]) => {
+ if (Array.isArray(value)) {
+ value.forEach((headerValue) => responseHeaders.append(key, headerValue));
+ return;
+ }
+ if (typeof value === 'string') {
+ responseHeaders.set(key, value);
+ }
+ });
+ return responseHeaders;
+};
+const toRuntimeFetchResponse = (response, chunks, headers) => {
+ const status = response.statusCode ?? DEFAULT_HTTP_ERROR_STATUS;
+ return {
+ ok: status >= 200 && status < 300,
+ status,
+ headers,
+ text: async () => Buffer.concat(chunks).toString('utf8')
+ };
+};
class LocalWrapperProvider {
filesystemReader;
runtimePath;
constructor(filesystemReader, runtimePath) {
@@ -72,34 +98,51 @@
constructor(statsigService) {
this.statsigService = statsigService;
this.cdnUrl = (0, cli_shared_1.getEnvironmentConfig)(RuntimeCDN);
}
+ getFromNodeRequest = async (url) => {
+ return await new Promise((resolve, reject) => {
+ const parsedUrl = new url_1.URL(url);
+ const requestClient = parsedUrl.protocol === 'https:' ? https_1.default : http_1.default;
+ const request = requestClient.get(parsedUrl, (response) => {
+ const body = [];
+ response.on('data', (chunk) => body.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
+ response.on('end', () => {
+ resolve(toRuntimeFetchResponse(response, body, toHeaders(response.headers)));
+ });
+ });
+ request.setTimeout(NODE_REQUEST_TIMEOUT_MS, () => {
+ request.destroy(new Error(`Request timed out after ${NODE_REQUEST_TIMEOUT_MS}ms`));
+ });
+ request.on('error', reject);
+ });
+ };
async getScriptPathFromIndex(htmlContent, requestId, scriptType) {
const html = (0, cheerio_1.load)(htmlContent, { xml: { xmlMode: false } });
const scriptPath = html('script')
?.get()
?.find((asset) => asset.attribs['src']?.includes(scriptType))?.attribs['src'];
if (typeof scriptPath !== 'string') {
throw new ParseWrapperCDNIndexError(`Unable to parse source of runtime ${scriptType}.`, requestId);
}
- return new URL(scriptPath, this.cdnUrl).toString();
+ return new url_1.URL(scriptPath, this.cdnUrl).toString();
}
getFileFromCDN = async (scriptType) => {
try {
- const indexResponse = await (0, node_fetch_1.default)(this.cdnUrl);
+ const indexResponse = await this.getFromNodeRequest(this.cdnUrl);
if (!indexResponse.ok) {
throw new WrapperNetworkError(`Failed to fetch runtime component: ${this.cdnUrl} ${indexResponse.status}.`, (0, cli_shared_1.getAtlassianTraceId)(indexResponse.headers));
}
const source = await this.getScriptPathFromIndex(await indexResponse.text(), (0, cli_shared_1.getAtlassianTraceId)(indexResponse.headers), scriptType);
- const response = await (0, node_fetch_1.default)(source);
+ const response = await this.getFromNodeRequest(source);
if (!response.ok) {
throw new WrapperNetworkError(`Failed to fetch runtime component: ${source.toString()} ${response.status}.`, (0, cli_shared_1.getAtlassianTraceId)(response.headers));
}
const script = await response.text();
return {
script,
source,
- version: new URL(source).pathname
+ version: new url_1.URL(source).pathname
};
}
catch (e) {
if (e instanceof ParseWrapperCDNIndexError) {