@sitecore-jss/sitecore-jss-proxy 13.2.5-canary.1 → 13.2.5-canary.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -55,7 +55,6 @@ var set_cookie_parser_1 = __importDefault(require("set-cookie-parser"));
55
55
  var http_status_codes_1 = __importDefault(require("http-status-codes"));
56
56
  var zlib_1 = __importDefault(require("zlib")); // node.js standard lib
57
57
  var util_1 = require("./util");
58
- // tslint:disable:max-line-length
59
58
  // For some reason, every other response returned by Sitecore contains the 'set-cookie' header with the SC_ANALYTICS_GLOBAL_COOKIE value as an empty string.
60
59
  // This effectively sets the cookie to empty on the client as well, so if a user were to close their browser
61
60
  // after one of these 'empty value' responses, they would not be tracked as a returning visitor after re-opening their browser.
@@ -83,7 +82,8 @@ function renderAppToResponse(proxyResponse, request, serverResponse, renderer, c
83
82
  return __awaiter(this, void 0, void 0, function () {
84
83
  var responseString;
85
84
  return __generator(this, function (_a) {
86
- if (proxyResponse.statusCode === http_status_codes_1.default.OK || proxyResponse.statusCode === http_status_codes_1.default.NOT_FOUND) {
85
+ if (proxyResponse.statusCode === http_status_codes_1.default.OK ||
86
+ proxyResponse.statusCode === http_status_codes_1.default.NOT_FOUND) {
87
87
  responseString = void 0;
88
88
  if (contentEncoding &&
89
89
  (contentEncoding.indexOf('gzip') !== -1 || contentEncoding.indexOf('deflate') !== -1)) {
@@ -400,13 +400,26 @@ function isUrlIgnored(originalUrl, config, noDebug) {
400
400
  return false;
401
401
  }
402
402
  function handleProxyRequest(proxyReq, req, res, config, customOnProxyReq) {
403
+ var _a;
403
404
  // if a HEAD request, we still need to issue a GET so we can return accurate headers
404
- // proxyReq defined as 'any' to allow us to mutate this
405
- if (proxyReq.method === 'HEAD' && !isUrlIgnored(req.originalUrl, config, true)) {
406
- if (config.debug) {
407
- console.log('DEBUG: Rewriting HEAD request to GET to create accurate headers');
405
+ if (!isUrlIgnored(req.originalUrl, config, true)) {
406
+ // In case 'followRedirects' is enabled, and before the proxy was initialized we had set 'originalMethod'
407
+ // now we need to set req.method back to original one, since proxyReq is already initialized.
408
+ // See more info in 'preProxyHandler'
409
+ if (((_a = config.proxyOptions) === null || _a === void 0 ? void 0 : _a.followRedirects) && req.originalMethod === 'HEAD') {
410
+ req.method = req.originalMethod;
411
+ delete req.originalMethod;
412
+ if (config.debug) {
413
+ console.log('DEBUG: Rewriting HEAD request to GET to create accurate headers');
414
+ }
415
+ }
416
+ else if (proxyReq.method === 'HEAD') {
417
+ if (config.debug) {
418
+ console.log('DEBUG: Rewriting HEAD request to GET to create accurate headers');
419
+ }
420
+ // if a HEAD request, we still need to issue a GET so we can return accurate headers
421
+ proxyReq.method = 'GET';
408
422
  }
409
- proxyReq.method = 'GET';
410
423
  }
411
424
  // invoke custom onProxyReq
412
425
  if (customOnProxyReq) {
@@ -428,10 +441,26 @@ function createOptions(renderer, config, parseRouteUrl) {
428
441
  console.log('DEBUG: Final proxy config', config);
429
442
  }
430
443
  var customOnProxyReq = (_a = config.proxyOptions) === null || _a === void 0 ? void 0 : _a.onProxyReq;
431
- return __assign(__assign({}, config.proxyOptions), { target: config.apiHost, changeOrigin: true, ws: true, pathRewrite: function (reqPath, req) { return rewriteRequestPath(reqPath, req, config, parseRouteUrl); }, logLevel: config.debug ? 'debug' : 'info', onProxyReq: function (proxyReq, req, res) { return handleProxyRequest(proxyReq, req, res, config, customOnProxyReq); }, onProxyRes: function (proxyRes, req, res) { return handleProxyResponse(proxyRes, req, res, renderer, config); } });
444
+ return __assign(__assign({}, config.proxyOptions), { target: config.apiHost, changeOrigin: true, ws: true, pathRewrite: function (reqPath, req) { return rewriteRequestPath(reqPath, req, config, parseRouteUrl); }, logLevel: config.debug ? 'debug' : 'info', onProxyReq: function (proxyReq, req, res) {
445
+ return handleProxyRequest(proxyReq, req, res, config, customOnProxyReq);
446
+ }, onProxyRes: function (proxyRes, req, res) { return handleProxyResponse(proxyRes, req, res, renderer, config); } });
432
447
  }
433
448
  function scProxy(renderer, config, parseRouteUrl) {
434
449
  var options = createOptions(renderer, config, parseRouteUrl);
435
- return http_proxy_middleware_1.default(options);
450
+ var preProxyHandler = function (req, _res, next) {
451
+ // When 'followRedirects' is enabled, 'onProxyReq' is executed after 'proxyReq' is initialized based on original 'req'
452
+ // and there are no public properties/methods to modify Redirectable 'proxyReq'.
453
+ // so, we need to set 'HEAD' req as 'GET' before the proxy is initialized.
454
+ // During the 'onProxyReq' event we will set 'req.method' back as 'HEAD'.
455
+ // if a HEAD request, we need to issue a GET so we can return accurate headers
456
+ if (req.method === 'HEAD' &&
457
+ options.followRedirects &&
458
+ !isUrlIgnored(req.originalUrl, config, true)) {
459
+ req.method = 'GET';
460
+ req.originalMethod = 'HEAD';
461
+ }
462
+ next();
463
+ };
464
+ return [preProxyHandler, http_proxy_middleware_1.default(options)];
436
465
  }
437
466
  exports.default = scProxy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-jss/sitecore-jss-proxy",
3
- "version": "13.2.5-canary.1",
3
+ "version": "13.2.5-canary.2",
4
4
  "description": "Proxy middleware for express.js server.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -42,5 +42,5 @@
42
42
  "typescript": "^3.1.3"
43
43
  },
44
44
  "types": "types/index.d.ts",
45
- "gitHead": "9a90cd19b4ee128d60d6664b2f2dbabdc9a5e77f"
45
+ "gitHead": "e55d15ad7d6b3498d9d1c285a7d3640cc2f9a5f3"
46
46
  }
package/types/index.d.ts CHANGED
@@ -1,7 +1,11 @@
1
- /// <reference types="connect" />
1
+ /// <reference types="node" />
2
+ import { IncomingMessage } from 'http';
2
3
  import { AppRenderer } from './AppRenderer';
3
4
  import { ProxyConfig } from './ProxyConfig';
4
5
  import { RouteUrlParser } from './RouteUrlParser';
6
+ export interface ProxyIncomingMessage extends IncomingMessage {
7
+ originalUrl: string;
8
+ }
5
9
  export declare const removeEmptyAnalyticsCookie: (proxyResponse: any) => void;
6
10
  export declare function rewriteRequestPath(reqPath: string, req: any, config: ProxyConfig, parseRouteUrl?: RouteUrlParser): string;
7
- export default function scProxy(renderer: AppRenderer, config: ProxyConfig, parseRouteUrl: RouteUrlParser): import("connect").NextHandleFunction;
11
+ export default function scProxy(renderer: AppRenderer, config: ProxyConfig, parseRouteUrl: RouteUrlParser): any[];