@camstack/addon-import-alexa 0.2.157 → 0.2.158

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/CHANGELOG.md CHANGED
@@ -7,11 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.158] - 2026-10-27
11
+
12
+ ### Fixed
13
+
14
+ - Alexa sign-in follows Amazon's current login pages
15
+
10
16
  ## [0.2.157] - 2026-10-27
11
17
 
12
18
  ### Fixed
13
19
 
14
20
  - Amazon sign-in for the Alexa broker can be retried after a cancelled attempt, and the sign-in link now uses the hub's LAN address from the Network page instead of the first network interface
21
+ - Alexa sign-in follows Amazon's current login pages (library update)
15
22
 
16
23
  ## [0.2.156] - 2026-10-27
17
24
 
package/dist/addon.js CHANGED
@@ -128426,7 +128426,7 @@ var require_fix_request_body = /* @__PURE__ */ __commonJSMin(((exports) => {
128426
128426
  * Fix proxied body if bodyParser is involved.
128427
128427
  */
128428
128428
  function fixRequestBody(proxyReq, req) {
128429
- if (req.readableLength !== 0) return;
128429
+ if (!req.readableEnded) return;
128430
128430
  const requestBody = req.body;
128431
128431
  if (!requestBody) return;
128432
128432
  const contentType = proxyReq.getHeader("Content-Type");
@@ -128521,6 +128521,12 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128521
128521
  var path$2 = require("path");
128522
128522
  var crypto$1 = require("crypto");
128523
128523
  var FORMERDATA_STORE_VERSION = 4;
128524
+ var ALEXA_APP_VERSION = "2.2.651540.0";
128525
+ var EXTRA_AMAZON_PROXY_HOSTS = [
128526
+ "amazon.com",
128527
+ "www.amazon.com",
128528
+ "alexa.amazon.com"
128529
+ ];
128524
128530
  function addCookies(Cookie, headers) {
128525
128531
  if (!headers || !headers["set-cookie"]) return Cookie;
128526
128532
  const cookies = cookieTools.parse(Cookie);
@@ -128536,6 +128542,15 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128536
128542
  Cookie = Cookie.replace(/[; ]*$/, "");
128537
128543
  return Cookie;
128538
128544
  }
128545
+ function isAmazonCookieName(name) {
128546
+ return /^(session-id|session-id-time|session-token|ubid-.+|lc-.+|x-.+|at-.+|sess-at-.+|frc|map-md|csrf|sid|csm-hit|i18n-prefs|sp-cdn|skin)$/.test(name);
128547
+ }
128548
+ function sanitizeAmazonCookie(cookie) {
128549
+ const cookies = cookieTools.parse(cookie || "");
128550
+ let sanitizedCookie = "";
128551
+ for (const name of Object.keys(cookies)) if (isAmazonCookieName(name)) sanitizedCookie += `${name}=${cookies[name]}; `;
128552
+ return sanitizedCookie.replace(/[; ]*$/, "");
128553
+ }
128539
128554
  function customStringify(v, func, intent) {
128540
128555
  const cache = /* @__PURE__ */ new Map();
128541
128556
  return JSON.stringify(v, function(key, value) {
@@ -128549,6 +128564,29 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128549
128564
  }
128550
128565
  function initAmazonProxy(_options, callbackCookie, callbackListening) {
128551
128566
  const initialCookies = {};
128567
+ const proxyBase = () => `http://${_options.proxyOwnIp}:${_options.proxyPort}/`;
128568
+ const defaultAmazonHost = `www.${_options.baseAmazonPage}`;
128569
+ const amazonProxyHosts = [];
128570
+ function escapeRegex(data) {
128571
+ return data.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
128572
+ }
128573
+ function addAmazonProxyHost(host) {
128574
+ if (host && !amazonProxyHosts.includes(host)) amazonProxyHosts.push(host);
128575
+ }
128576
+ addAmazonProxyHost(defaultAmazonHost);
128577
+ addAmazonProxyHost(`alexa.${_options.baseAmazonPage}`);
128578
+ addAmazonProxyHost(_options.baseAmazonPage);
128579
+ EXTRA_AMAZON_PROXY_HOSTS.forEach(addAmazonProxyHost);
128580
+ function amazonHostFromProxyPath(url) {
128581
+ const match = (url || "").match(/^\/([^/?#]+)(?:[/?#]|$)/);
128582
+ if (match && amazonProxyHosts.includes(match[1])) return match[1];
128583
+ return null;
128584
+ }
128585
+ function amazonHostFromProxyUrl(url) {
128586
+ if (!url) return null;
128587
+ for (const host of amazonProxyHosts) if (url === `${proxyBase()}${host}` || url.startsWith(`${proxyBase()}${host}/`)) return host;
128588
+ return null;
128589
+ }
128552
128590
  const formerDataStorePath = _options.formerDataStorePath || path$2.join(__dirname, "formerDataStore.json");
128553
128591
  let formerDataStoreValid = false;
128554
128592
  if (!_options.formerRegistrationData) try {
@@ -128572,7 +128610,7 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128572
128610
  _options.logger && _options.logger("Proxy Init: reuse frc from former data");
128573
128611
  initialCookies.frc = _options.formerRegistrationData.frc;
128574
128612
  }
128575
- if (!_options.formerRegistrationData || !_options.formerRegistrationData["map-md"]) initialCookies["map-md"] = Buffer.from("{\"device_user_dictionary\":[],\"device_registration_data\":{\"software_version\":\"1\"},\"app_identifier\":{\"app_version\":\"2.2.485407\",\"bundle_id\":\"com.amazon.echo\"}}").toString("base64");
128613
+ if (!_options.formerRegistrationData || !_options.formerRegistrationData["map-md"]) initialCookies["map-md"] = Buffer.from(`{"device_user_dictionary":[],"device_registration_data":{"software_version":"1"},"app_identifier":{"app_version":"${ALEXA_APP_VERSION}","bundle_id":"com.amazon.echo"}}`).toString("base64");
128576
128614
  else {
128577
128615
  _options.logger && _options.logger("Proxy Init: reuse map-md from former data");
128578
128616
  initialCookies["map-md"] = _options.formerRegistrationData["map-md"];
@@ -128605,11 +128643,49 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128605
128643
  const code_verifier = base64URLEncode(crypto$1.randomBytes(32));
128606
128644
  const code_challenge = base64URLEncode(sha256(code_verifier));
128607
128645
  let proxyCookies = "";
128646
+ let returnedInitUrl;
128647
+ function buildInitialUrl() {
128648
+ return `https://www.${_options.baseAmazonPage}/ap/signin?openid.return_to=https%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fmaplanding&openid.assoc_handle=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&accountStatusPolicy=P1&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns.oa2=http%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fext%2Foauth%2F2&openid.oa2.client_id=device%3A${deviceId}&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.oa2.response_type=code&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.pape.max_auth_age=0&openid.oa2.scope=device_auth_access&openid.oa2.code_challenge_method=S256&openid.oa2.code_challenge=${code_challenge}&language=${_options.amazonPageProxyLanguage}`;
128649
+ }
128650
+ function fixEmbeddedProxyUrls(pathAndQuery) {
128651
+ const qIndex = pathAndQuery.indexOf("?");
128652
+ if (qIndex === -1) return pathAndQuery;
128653
+ const p = pathAndQuery.slice(0, qIndex);
128654
+ const qs = pathAndQuery.slice(qIndex + 1);
128655
+ let params;
128656
+ try {
128657
+ params = new URLSearchParams(qs);
128658
+ } catch (_e) {
128659
+ return pathAndQuery;
128660
+ }
128661
+ let changed = false;
128662
+ for (const key of params.keys()) {
128663
+ const value = params.get(key);
128664
+ const fixed = replaceHostsBack(value);
128665
+ if (fixed !== value) {
128666
+ params.set(key, fixed);
128667
+ changed = true;
128668
+ }
128669
+ }
128670
+ if (changed) _options.logger && _options.logger(`Alexa-Cookie: Fixed embedded proxy URL(s) in outgoing request query`);
128671
+ return changed ? `${p}?${params.toString()}` : pathAndQuery;
128672
+ }
128673
+ function rewriteProxyPath(url, req) {
128674
+ let result;
128675
+ const urlHost = amazonHostFromProxyPath(url);
128676
+ if (urlHost) result = url.replace(new RegExp(`^/${escapeRegex(urlHost)}`), "") || "/";
128677
+ else if (req && req.headers && req.headers.host === `${_options.proxyOwnIp}:${_options.proxyPort}` && url === "/") {
128678
+ const initialUrl = returnedInitUrl || buildInitialUrl();
128679
+ const parsed = new URL(initialUrl);
128680
+ result = `${parsed.pathname}${parsed.search}`;
128681
+ } else result = url;
128682
+ return fixEmbeddedProxyUrls(result);
128683
+ }
128608
128684
  const optionsAlexa = {
128609
128685
  target: `https://alexa.${_options.baseAmazonPage}`,
128610
128686
  changeOrigin: true,
128611
128687
  ws: false,
128612
- pathRewrite: {},
128688
+ pathRewrite: rewriteProxyPath,
128613
128689
  router,
128614
128690
  hostRewrite: true,
128615
128691
  followRedirects: false,
@@ -128617,17 +128693,10 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128617
128693
  onError,
128618
128694
  onProxyRes,
128619
128695
  onProxyReq,
128620
- headers: {
128621
- "user-agent": "AppleWebKit PitanguiBridge/2.2.485407.0-[HARDWARE=iPhone10_4][SOFTWARE=15.5][DEVICE=iPhone]",
128622
- "accept-language": _options.acceptLanguage,
128623
- "authority": `www.${_options.baseAmazonPage}`
128624
- },
128625
- cookieDomainRewrite: { "*": "" }
128696
+ headers: { "accept-language": _options.acceptLanguage },
128697
+ cookieDomainRewrite: { "*": "" },
128698
+ cookiePathRewrite: { "*": "/" }
128626
128699
  };
128627
- optionsAlexa.pathRewrite[`^/www.${_options.baseAmazonPage}`] = "";
128628
- optionsAlexa.pathRewrite[`^/alexa.${_options.baseAmazonPage}`] = "";
128629
- optionsAlexa.cookieDomainRewrite[`.${_options.baseAmazonPage}`] = _options.proxyOwnIp;
128630
- optionsAlexa.cookieDomainRewrite[_options.baseAmazonPage] = _options.proxyOwnIp;
128631
128700
  if (_options.logger) optionsAlexa.logProvider = function logProvider() {
128632
128701
  return {
128633
128702
  log: _options.logger.log || _options.logger,
@@ -128637,22 +128706,20 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128637
128706
  error: _options.logger.error || _options.logger
128638
128707
  };
128639
128708
  };
128640
- let returnedInitUrl;
128641
128709
  function router(req) {
128642
128710
  const url = req.originalUrl || req.url;
128643
128711
  _options.logger && _options.logger(`Router: ${url} / ${req.method} / ${JSON.stringify(req.headers)}`);
128644
128712
  if (req.headers.host === `${_options.proxyOwnIp}:${_options.proxyPort}`) {
128645
- if (url.startsWith(`/www.${_options.baseAmazonPage}/`)) return `https://www.${_options.baseAmazonPage}`;
128646
- else if (url.startsWith(`/alexa.${_options.baseAmazonPage}/`)) return `https://alexa.${_options.baseAmazonPage}`;
128647
- else if (req.headers.referer) {
128648
- if (req.headers.referer.startsWith(`http://${_options.proxyOwnIp}:${_options.proxyPort}/www.${_options.baseAmazonPage}/`)) return `https://www.${_options.baseAmazonPage}`;
128649
- else if (req.headers.referer.startsWith(`http://${_options.proxyOwnIp}:${_options.proxyPort}/alexa.${_options.baseAmazonPage}/`)) return `https://alexa.${_options.baseAmazonPage}`;
128650
- }
128713
+ const urlHost = amazonHostFromProxyPath(url);
128714
+ if (urlHost) return `https://${urlHost}`;
128715
+ const refererHost = amazonHostFromProxyUrl(req.headers.referer);
128716
+ if (refererHost) return `https://${refererHost}`;
128651
128717
  if (url === "/") {
128652
- returnedInitUrl = `https://www.${_options.baseAmazonPage}/ap/signin?openid.return_to=https%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fmaplanding&openid.assoc_handle=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&accountStatusPolicy=P1&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns.oa2=http%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fext%2Foauth%2F2&openid.oa2.client_id=device%3A${deviceId}&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.oa2.response_type=code&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.pape.max_auth_age=0&openid.oa2.scope=device_auth_access&openid.oa2.code_challenge_method=S256&openid.oa2.code_challenge=${code_challenge}&language=${_options.amazonPageProxyLanguage}`;
128718
+ returnedInitUrl = buildInitialUrl();
128653
128719
  _options.logger && _options.logger(`Alexa-Cookie: Initial Page Request: ${returnedInitUrl}`);
128654
- return returnedInitUrl;
128655
- } else return `https://www.${_options.baseAmazonPage}`;
128720
+ const parsedInitUrl = new URL(returnedInitUrl);
128721
+ return `${parsedInitUrl.protocol}//${parsedInitUrl.host}`;
128722
+ } else return `https://${defaultAmazonHost}`;
128656
128723
  }
128657
128724
  return `https://alexa.${_options.baseAmazonPage}`;
128658
128725
  }
@@ -128664,21 +128731,34 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128664
128731
  } catch (err) {}
128665
128732
  }
128666
128733
  function replaceHosts(data) {
128667
- const amazonRegex = new RegExp(`https?://www.${_options.baseAmazonPage}:?[0-9]*/`.replace(/\./g, "\\."), "g");
128668
- const alexaRegex = new RegExp(`https?://alexa.${_options.baseAmazonPage}:?[0-9]*/`.replace(/\./g, "\\."), "g");
128669
128734
  data = data.replace(///g, "/");
128670
- data = data.replace(amazonRegex, `http://${_options.proxyOwnIp}:${_options.proxyPort}/www.${_options.baseAmazonPage}/`);
128671
- data = data.replace(alexaRegex, `http://${_options.proxyOwnIp}:${_options.proxyPort}/alexa.${_options.baseAmazonPage}/`);
128735
+ for (const host of amazonProxyHosts) {
128736
+ const hostRegex = new RegExp(`https?://${escapeRegex(host)}:?[0-9]*/`, "g");
128737
+ data = data.replace(hostRegex, `${proxyBase()}${host}/`);
128738
+ }
128672
128739
  return data;
128673
128740
  }
128674
128741
  function replaceHostsBack(data) {
128675
- const amazonRegex = new RegExp(`http://${_options.proxyOwnIp}:${_options.proxyPort}/www.${_options.baseAmazonPage}/`.replace(/\./g, "\\."), "g");
128676
- const alexaRegex = new RegExp(`http://${_options.proxyOwnIp}:${_options.proxyPort}/alexa.${_options.baseAmazonPage}/`.replace(/\./g, "\\."), "g");
128677
- data = data.replace(amazonRegex, `https://www.${_options.baseAmazonPage}/`);
128678
- data = data.replace(alexaRegex, `https://alexa.${_options.baseAmazonPage}/`);
128679
- if (data === `http://${_options.proxyOwnIp}:${_options.proxyPort}/`) data = returnedInitUrl;
128742
+ const base = proxyBase();
128743
+ for (const host of amazonProxyHosts) {
128744
+ const hostRegex = new RegExp(`${escapeRegex(base)}${escapeRegex(host)}/`, "g");
128745
+ data = data.replace(hostRegex, `https://${host}/`);
128746
+ }
128747
+ if (data === base) data = returnedInitUrl;
128748
+ else if (data.startsWith(base)) data = `https://${defaultAmazonHost}/${data.slice(base.length)}`;
128680
128749
  return data;
128681
128750
  }
128751
+ function parseQueryParams(data) {
128752
+ const paramStart = data && data.indexOf("?");
128753
+ if (paramStart === -1 || paramStart === void 0) return {};
128754
+ return querystring$2.parse(data.substr(paramStart + 1));
128755
+ }
128756
+ function isProxySuccessUrl(data) {
128757
+ if (!data) return false;
128758
+ if (data.includes("/spa/index.html")) return true;
128759
+ if (!data.includes("/ap/maplanding?")) return false;
128760
+ return !!parseQueryParams(data)["openid.oa2.authorization_code"];
128761
+ }
128682
128762
  function onProxyReq(proxyReq, req) {
128683
128763
  const url = req.originalUrl || req.url;
128684
128764
  if (url.endsWith(".ico") || url.endsWith(".js") || url.endsWith(".ttf") || url.endsWith(".svg") || url.endsWith(".png") || url.endsWith(".appcache")) return;
@@ -128705,14 +128785,26 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128705
128785
  }
128706
128786
  }
128707
128787
  if (typeof proxyReq.getHeader === "function" && proxyReq.getHeader("origin") !== `https://${proxyReq.getHeader("host")}`) {
128708
- proxyReq.setHeader("origin", `https://www.${_options.baseAmazonPage}`);
128709
- _options.logger && _options.logger("Alexa-Cookie: Modify headers: Delete Origin");
128788
+ proxyReq.setHeader("origin", `https://${proxyReq.getHeader("host") || defaultAmazonHost}`);
128789
+ _options.logger && _options.logger("Alexa-Cookie: Modify headers: Changed Origin");
128710
128790
  modified = true;
128711
128791
  }
128712
- let postBody = "";
128713
- req.on("data", (chunk) => {
128714
- postBody += chunk.toString();
128715
- });
128792
+ if (req.body && typeof req.body === "object") {
128793
+ let bodyChanged = false;
128794
+ for (const key of Object.keys(req.body)) {
128795
+ const value = req.body[key];
128796
+ if (typeof value !== "string") continue;
128797
+ const fixed = replaceHostsBack(value);
128798
+ if (fixed !== value) {
128799
+ req.body[key] = fixed;
128800
+ bodyChanged = true;
128801
+ }
128802
+ }
128803
+ if (bodyChanged) _options.logger && _options.logger("Alexa-Cookie: Fixed embedded proxy URL(s) in outgoing POST body");
128804
+ const bodyData = querystring$2.stringify(req.body);
128805
+ proxyReq.setHeader("Content-Length", Buffer.byteLength(bodyData));
128806
+ proxyReq.write(bodyData);
128807
+ }
128716
128808
  }
128717
128809
  _options.proxyLogLevel === "debug" && _options.logger && _options.logger(`Alexa-Cookie: Proxy-Request: (modified:${modified})${customStringify(proxyReq, null, 2)}`);
128718
128810
  }
@@ -128730,17 +128822,22 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128730
128822
  proxyCookies = addCookies(proxyCookies, proxyRes.headers);
128731
128823
  }
128732
128824
  _options.logger && _options.logger(`Alexa-Cookie: Cookies handled: ${JSON.stringify(proxyCookies)}`);
128733
- if (proxyRes.socket && proxyRes.socket._host === `www.${_options.baseAmazonPage}` && proxyRes.socket.parser.outgoing && proxyRes.socket.parser.outgoing.method === "GET" && proxyRes.socket.parser.outgoing.path.startsWith("/ap/maplanding") || proxyRes.socket && proxyRes.socket.parser.outgoing && proxyRes.socket.parser.outgoing.getHeader("location") && proxyRes.socket.parser.outgoing.getHeader("location").includes("/ap/maplanding?") || proxyRes.headers.location && (proxyRes.headers.location.includes("/ap/maplanding?") || proxyRes.headers.location.includes("/spa/index.html"))) {
128825
+ const outgoing = proxyRes.socket && proxyRes.socket.parser && proxyRes.socket.parser.outgoing;
128826
+ const successUrl = [
128827
+ proxyRes.headers.location,
128828
+ outgoing && outgoing.method === "GET" && outgoing.path,
128829
+ outgoing && outgoing.getHeader && outgoing.getHeader("location")
128830
+ ].find(isProxySuccessUrl);
128831
+ if (successUrl) {
128734
128832
  _options.logger && _options.logger("Alexa-Cookie: Proxy detected SUCCESS!!");
128735
- const paramStart = proxyRes.headers.location.indexOf("?");
128736
- const queryParams = querystring$2.parse(proxyRes.headers.location.substr(paramStart + 1));
128833
+ const queryParams = parseQueryParams(successUrl);
128737
128834
  proxyRes.statusCode = 302;
128738
128835
  proxyRes.headers.location = `http://${_options.proxyOwnIp}:${_options.proxyPort}/cookie-success`;
128739
128836
  delete proxyRes.headers.referer;
128740
128837
  _options.logger && _options.logger(`Alexa-Cookie: Proxy catched cookie: ${proxyCookies}`);
128741
128838
  _options.logger && _options.logger(`Alexa-Cookie: Proxy catched parameters: ${JSON.stringify(queryParams)}`);
128742
128839
  callbackCookie && callbackCookie(null, {
128743
- "loginCookie": proxyCookies,
128840
+ "loginCookie": sanitizeAmazonCookie(proxyCookies),
128744
128841
  "authorization_code": queryParams["openid.oa2.authorization_code"],
128745
128842
  "frc": initialCookies.frc,
128746
128843
  "map-md": initialCookies["map-md"],
@@ -128752,7 +128849,7 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128752
128849
  if (proxyRes.headers.location) {
128753
128850
  _options.logger && _options.logger(`Redirect: Original Location ----> ${proxyRes.headers.location}`);
128754
128851
  proxyRes.headers.location = replaceHosts(proxyRes.headers.location);
128755
- if (reqestHost && proxyRes.headers.location.startsWith("/")) proxyRes.headers.location = `http://${_options.proxyOwnIp}:${_options.proxyPort}/${reqestHost}${proxyRes.headers.location}`;
128852
+ if (reqestHost && amazonProxyHosts.includes(reqestHost) && proxyRes.headers.location.startsWith("/")) proxyRes.headers.location = `http://${_options.proxyOwnIp}:${_options.proxyPort}/${reqestHost}${proxyRes.headers.location}`;
128756
128853
  _options.logger && _options.logger(`Redirect: Final Location ----> ${proxyRes.headers.location}`);
128757
128854
  return;
128758
128855
  }
@@ -128770,8 +128867,21 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128770
128867
  return body;
128771
128868
  });
128772
128869
  }
128870
+ function bufferUrlencodedBody(req, res, next) {
128871
+ const contentType = req.headers["content-type"] || "";
128872
+ if (req.method !== "POST" || contentType.indexOf("application/x-www-form-urlencoded") === -1) return next();
128873
+ let raw = "";
128874
+ req.on("data", (chunk) => {
128875
+ raw += chunk;
128876
+ });
128877
+ req.on("end", () => {
128878
+ req.body = querystring$2.parse(raw);
128879
+ next();
128880
+ });
128881
+ }
128773
128882
  const myProxy = proxy("!/cookie-success", optionsAlexa);
128774
128883
  const app = express();
128884
+ app.use(bufferUrlencodedBody);
128775
128885
  app.use(myProxy);
128776
128886
  app.get("/cookie-success", function(req, res) {
128777
128887
  res.send(_options.proxyCloseWindowHTML);
@@ -128781,7 +128891,8 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128781
128891
  _options.proxyPort = void 0;
128782
128892
  }
128783
128893
  const server = app.listen(_options.proxyPort, _options.proxyListenBind, function() {
128784
- _options.logger && _options.logger(`Alexa-Cookie: Proxy-Server listening on port ${server.address().port}`);
128894
+ _options.proxyPort = this.address().port;
128895
+ _options.logger && _options.logger(`Alexa-Cookie: Proxy-Server listening on port ${_options.proxyPort}`);
128785
128896
  callbackListening && callbackListening(server);
128786
128897
  callbackListening = null;
128787
128898
  }).on("error", (err) => {
@@ -128827,6 +128938,13 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128827
128938
  let proxyServer;
128828
128939
  let _options;
128829
128940
  let Cookie = "";
128941
+ const isAmazonCookieName = (name) => /^(session-id|session-id-time|session-token|ubid-.+|lc-.+|x-.+|at-.+|sess-at-.+|frc|map-md|csrf|sid|csm-hit|i18n-prefs|sp-cdn|skin)$/.test(name);
128942
+ const sanitizeAmazonCookie = (cookie) => {
128943
+ const cookies = cookieTools.parse(cookie || "");
128944
+ let sanitizedCookie = "";
128945
+ for (const name of Object.keys(cookies)) if (isAmazonCookieName(name)) sanitizedCookie += `${name}=${cookies[name]}; `;
128946
+ return sanitizedCookie.replace(/[; ]*$/, "");
128947
+ };
128830
128948
  const addCookies = (Cookie, headers) => {
128831
128949
  if (!headers || !headers["set-cookie"]) return Cookie;
128832
128950
  const cookies = cookieTools.parse(Cookie || "");
@@ -128914,7 +129032,7 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128914
129032
  if (!_options.baseAmazonPageHandle && _options.baseAmazonPageHandle !== "") {
128915
129033
  const amazonDomain = _options.baseAmazonPage.substr(_options.baseAmazonPage.lastIndexOf(".") + 1);
128916
129034
  if (amazonDomain === "jp") _options.baseAmazonPageHandle = `_${amazonDomain}`;
128917
- else if (amazonDomain !== "com") _options.baseAmazonPageHandle = "";
129035
+ else if (amazonDomain !== "com") _options.baseAmazonPageHandle = `_${amazonDomain}`;
128918
129036
  else _options.baseAmazonPageHandle = "";
128919
129037
  }
128920
129038
  if (!_options.userAgent) if (os$1.platform() === "win32") _options.userAgent = defaultUserAgent;
@@ -128941,7 +129059,7 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128941
129059
  if (_options.formerRegistrationData) _options.proxyOnly = true;
128942
129060
  };
128943
129061
  const getCSRFFromCookies = (cookie, _options, callback) => {
128944
- const csrfUrls = csrfOptions;
129062
+ const csrfUrls = [...csrfOptions];
128945
129063
  function csrfTry() {
128946
129064
  const path = csrfUrls.shift();
128947
129065
  const options = {
@@ -129104,6 +129222,7 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129104
129222
  const handleTokenRegistration = (_options, loginData, callback) => {
129105
129223
  _options.logger && _options.logger(`Handle token registration Start: ${JSON.stringify(loginData)}`);
129106
129224
  loginData.deviceAppName = _options.deviceAppName;
129225
+ loginData.loginCookie = sanitizeAmazonCookie(loginData.loginCookie);
129107
129226
  let deviceSerial;
129108
129227
  if (!_options.formerRegistrationData || !_options.formerRegistrationData.deviceSerial) {
129109
129228
  const deviceSerialBuffer = Buffer.alloc(16);
@@ -129191,6 +129310,7 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129191
129310
  Cookie = addCookies(Cookie, response.headers);
129192
129311
  loginData.refreshToken = body.response.success.tokens.bearer.refresh_token;
129193
129312
  const accessToken = body.response.success.tokens.bearer.access_token;
129313
+ loginData.accessToken = accessToken;
129194
129314
  loginData.tokenDate = Date.now();
129195
129315
  loginData.macDms = body.response.success.tokens.mac_dms;
129196
129316
  if (body.response.success.tokens.website_cookies && Array.isArray(body.response.success.tokens.website_cookies)) {
@@ -129217,29 +129337,43 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129217
129337
  _options.logger && _options.logger("Alexa-Cookie: Get User data");
129218
129338
  _options.logger && _options.logger(JSON.stringify(options));
129219
129339
  request(options, (error, response, body) => {
129220
- if (!error) {
129340
+ if (!error) if (!response || response.statusCode < 200 || response.statusCode >= 300 || !body) {
129341
+ _options.logger && _options.logger(`Get User data Response: status=${response && response.statusCode}, body=${JSON.stringify(body)}`);
129342
+ if (!_options.amazonPage) {
129343
+ callback && callback(/* @__PURE__ */ new Error(`Could not get user data from Amazon: HTTP ${response && response.statusCode}`), null);
129344
+ return;
129345
+ }
129346
+ loginData.amazonPage = _options.amazonPage;
129347
+ } else {
129221
129348
  try {
129222
129349
  if (typeof body !== "object") body = JSON.parse(body);
129223
129350
  } catch (err) {
129224
129351
  _options.logger && _options.logger(`Get User data Response: ${JSON.stringify(body)}`);
129225
- callback && callback(err, null);
129226
- return;
129352
+ if (!_options.amazonPage) {
129353
+ callback && callback(err, null);
129354
+ return;
129355
+ }
129356
+ loginData.amazonPage = _options.amazonPage;
129227
129357
  }
129228
129358
  _options.logger && _options.logger(`Get User data Response: ${JSON.stringify(body)}`);
129229
129359
  Cookie = addCookies(Cookie, response.headers);
129230
- if (body.marketPlaceDomainName) {
129360
+ if (body && body.marketPlaceDomainName) {
129231
129361
  const pos = body.marketPlaceDomainName.indexOf(".");
129232
129362
  if (pos !== -1) _options.amazonPage = body.marketPlaceDomainName.substr(pos + 1);
129233
129363
  }
129234
129364
  loginData.amazonPage = _options.amazonPage;
129235
- } else if (error && (!_options || !_options.amazonPage)) {
129365
+ }
129366
+ else if (error && (!_options || !_options.amazonPage)) {
129236
129367
  callback && callback(error, null);
129237
129368
  return;
129238
129369
  } else if (error && (!_options.formerRegistrationData || !_options.formerRegistrationData.amazonPage) && _options.amazonPage) _options.logger && _options.logger(`Continue with externally set amazonPage: ${_options.amazonPage}`);
129239
129370
  else if (error) _options.logger && _options.logger("Ignore error while getting user data and amazonPage because previously set amazonPage is available");
129240
129371
  loginData.loginCookie = Cookie;
129241
129372
  getLocalCookies(loginData.amazonPage, loginData.refreshToken, (err, localCookie) => {
129242
- if (err) callback && callback(err, null);
129373
+ if (err) {
129374
+ callback && callback(err, null);
129375
+ return;
129376
+ }
129243
129377
  loginData.localCookie = localCookie;
129244
129378
  getCSRFFromCookies(loginData.localCookie, _options, (err, resData) => {
129245
129379
  if (err) {
@@ -129248,7 +129382,6 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129248
129382
  }
129249
129383
  loginData.localCookie = resData.cookie;
129250
129384
  loginData.csrf = resData.csrf;
129251
- delete loginData.accessToken;
129252
129385
  delete loginData.authorization_code;
129253
129386
  delete loginData.verifier;
129254
129387
  loginData.dataVersion = 2;
@@ -129351,6 +129484,39 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129351
129484
  callback && callback(null, localCookie);
129352
129485
  });
129353
129486
  };
129487
+ const finishCookieRefresh = (loginData, callback) => {
129488
+ _options.logger && _options.logger("Alexa-Cookie: Skip App registration during refresh and update local cookies");
129489
+ const updateLocalCookies = () => {
129490
+ const amazonPage = loginData.amazonPage || _options.amazonPage;
129491
+ getLocalCookies(amazonPage, loginData.refreshToken, (err, localCookie) => {
129492
+ if (err) {
129493
+ callback && callback(err, null);
129494
+ return;
129495
+ }
129496
+ loginData.localCookie = localCookie;
129497
+ getCSRFFromCookies(loginData.localCookie, _options, (err, resData) => {
129498
+ if (err) {
129499
+ callback && callback(/* @__PURE__ */ new Error(`Error getting csrf for ${amazonPage}`), null);
129500
+ return;
129501
+ }
129502
+ loginData.localCookie = resData.cookie;
129503
+ loginData.csrf = resData.csrf;
129504
+ loginData.amazonPage = amazonPage;
129505
+ loginData.tokenDate = Date.now();
129506
+ delete loginData.authorization_code;
129507
+ delete loginData.verifier;
129508
+ loginData.dataVersion = 2;
129509
+ _options.logger && _options.logger("Alexa-Cookie: Refresh finished with updated cookies and csrf");
129510
+ callback && callback(null, loginData);
129511
+ });
129512
+ });
129513
+ };
129514
+ if (loginData.accessToken) {
129515
+ registerTokenCapabilities(loginData.accessToken, updateLocalCookies);
129516
+ return;
129517
+ }
129518
+ updateLocalCookies();
129519
+ };
129354
129520
  this.refreshAlexaCookie = (__options, callback) => {
129355
129521
  if (!__options || !__options.formerRegistrationData || !__options.formerRegistrationData.loginCookie || !__options.formerRegistrationData.refreshToken) {
129356
129522
  callback && callback(/* @__PURE__ */ new Error("No former registration data provided for Cookie Refresh"), null);
@@ -129417,13 +129583,16 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129417
129583
  _options.formerRegistrationData.loginCookie = addCookies(Cookie, response.headers);
129418
129584
  _options.formerRegistrationData.accessToken = body.access_token;
129419
129585
  getLocalCookies(_options.baseAmazonPage, _options.formerRegistrationData.refreshToken, (err, comCookie) => {
129420
- if (err) callback && callback(err, null);
129586
+ if (err) {
129587
+ callback && callback(err, null);
129588
+ return;
129589
+ }
129421
129590
  const initCookies = cookieTools.parse(_options.formerRegistrationData.loginCookie);
129422
129591
  let newCookie = `frc=${initCookies.frc}; `;
129423
129592
  newCookie += `map-md=${initCookies["map-md"]}; `;
129424
129593
  newCookie += comCookie;
129425
- _options.formerRegistrationData.loginCookie = newCookie;
129426
- handleTokenRegistration(_options, _options.formerRegistrationData, callback);
129594
+ _options.formerRegistrationData.loginCookie = sanitizeAmazonCookie(newCookie);
129595
+ finishCookieRefresh(_options.formerRegistrationData, callback);
129427
129596
  });
129428
129597
  });
129429
129598
  };
@@ -130018,10 +130187,20 @@ var import_alexa_remote = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin
130018
130187
  listItemId: payload.listItemId
130019
130188
  });
130020
130189
  return;
130190
+ case "NotifyNowPlayingUpdated":
130191
+ const nowPlaying = payload?.update?.update?.nowPlayingData ?? null;
130192
+ this.emit("ws-now-playing-updated", {
130193
+ customerId: payload?.customerId ?? null,
130194
+ taskSessionId: payload?.update?.taskSessionId ?? null,
130195
+ cause: payload?.update?.update?.cause ?? null,
130196
+ mediaId: nowPlaying?.mediaId ?? null,
130197
+ playerState: nowPlaying?.playerState ?? null,
130198
+ nowPlayingData: nowPlaying
130199
+ });
130200
+ return;
130021
130201
  case "PUSH_MICROPHONE_STATE":
130022
130202
  case "PUSH_DELETE_DOPPLER_ACTIVITIES":
130023
130203
  case "PUSH_DEVICE_SETUP_STATE_CHANGE":
130024
- case "NotifyNowPlayingUpdated":
130025
130204
  case "NotifyMediaSessionsUpdated": return;
130026
130205
  }
130027
130206
  this.emit("ws-unknown-command", command, payload);
@@ -132068,7 +132247,7 @@ var import_alexa_remote = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin
132068
132247
  */
132069
132248
  var ALEXA_API_USER_AGENT_POSTFIX = `AlexaRemote/${{
132070
132249
  name: "alexa-remote2",
132071
- version: "8.0.4",
132250
+ version: "8.1.1",
132072
132251
  description: "Remote Control for amazon echo devices",
132073
132252
  author: {
132074
132253
  "name": "Apollon77",
@@ -132097,16 +132276,16 @@ var ALEXA_API_USER_AGENT_POSTFIX = `AlexaRemote/${{
132097
132276
  "layla.amazon.de"
132098
132277
  ],
132099
132278
  dependencies: {
132100
- "alexa-cookie2": "^5.0.3",
132279
+ "alexa-cookie2": "^5.0.6",
132101
132280
  "https": "^1.0.0",
132102
132281
  "querystring": "^0.2.1",
132103
- "ws": "^8.18.3",
132282
+ "ws": "^8.21.0",
132104
132283
  "extend": "^3.0.2",
132105
- "uuid": "^11.1.0"
132284
+ "uuid": "^11.1.1"
132106
132285
  },
132107
132286
  devDependencies: {
132108
- "@alcalzone/release-script": "^5.0.0",
132109
- "@alcalzone/release-script-plugin-license": "^4.0.0",
132287
+ "@alcalzone/release-script": "^5.2.1",
132288
+ "@alcalzone/release-script-plugin-license": "^5.2.0",
132110
132289
  "eslint": "^8.57.1"
132111
132290
  },
132112
132291
  scripts: {
package/dist/addon.mjs CHANGED
@@ -128425,7 +128425,7 @@ var require_fix_request_body = /* @__PURE__ */ __commonJSMin(((exports) => {
128425
128425
  * Fix proxied body if bodyParser is involved.
128426
128426
  */
128427
128427
  function fixRequestBody(proxyReq, req) {
128428
- if (req.readableLength !== 0) return;
128428
+ if (!req.readableEnded) return;
128429
128429
  const requestBody = req.body;
128430
128430
  if (!requestBody) return;
128431
128431
  const contentType = proxyReq.getHeader("Content-Type");
@@ -128520,6 +128520,12 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128520
128520
  var path$2 = __require("path");
128521
128521
  var crypto$1 = __require("crypto");
128522
128522
  var FORMERDATA_STORE_VERSION = 4;
128523
+ var ALEXA_APP_VERSION = "2.2.651540.0";
128524
+ var EXTRA_AMAZON_PROXY_HOSTS = [
128525
+ "amazon.com",
128526
+ "www.amazon.com",
128527
+ "alexa.amazon.com"
128528
+ ];
128523
128529
  function addCookies(Cookie, headers) {
128524
128530
  if (!headers || !headers["set-cookie"]) return Cookie;
128525
128531
  const cookies = cookieTools.parse(Cookie);
@@ -128535,6 +128541,15 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128535
128541
  Cookie = Cookie.replace(/[; ]*$/, "");
128536
128542
  return Cookie;
128537
128543
  }
128544
+ function isAmazonCookieName(name) {
128545
+ return /^(session-id|session-id-time|session-token|ubid-.+|lc-.+|x-.+|at-.+|sess-at-.+|frc|map-md|csrf|sid|csm-hit|i18n-prefs|sp-cdn|skin)$/.test(name);
128546
+ }
128547
+ function sanitizeAmazonCookie(cookie) {
128548
+ const cookies = cookieTools.parse(cookie || "");
128549
+ let sanitizedCookie = "";
128550
+ for (const name of Object.keys(cookies)) if (isAmazonCookieName(name)) sanitizedCookie += `${name}=${cookies[name]}; `;
128551
+ return sanitizedCookie.replace(/[; ]*$/, "");
128552
+ }
128538
128553
  function customStringify(v, func, intent) {
128539
128554
  const cache = /* @__PURE__ */ new Map();
128540
128555
  return JSON.stringify(v, function(key, value) {
@@ -128548,6 +128563,29 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128548
128563
  }
128549
128564
  function initAmazonProxy(_options, callbackCookie, callbackListening) {
128550
128565
  const initialCookies = {};
128566
+ const proxyBase = () => `http://${_options.proxyOwnIp}:${_options.proxyPort}/`;
128567
+ const defaultAmazonHost = `www.${_options.baseAmazonPage}`;
128568
+ const amazonProxyHosts = [];
128569
+ function escapeRegex(data) {
128570
+ return data.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
128571
+ }
128572
+ function addAmazonProxyHost(host) {
128573
+ if (host && !amazonProxyHosts.includes(host)) amazonProxyHosts.push(host);
128574
+ }
128575
+ addAmazonProxyHost(defaultAmazonHost);
128576
+ addAmazonProxyHost(`alexa.${_options.baseAmazonPage}`);
128577
+ addAmazonProxyHost(_options.baseAmazonPage);
128578
+ EXTRA_AMAZON_PROXY_HOSTS.forEach(addAmazonProxyHost);
128579
+ function amazonHostFromProxyPath(url) {
128580
+ const match = (url || "").match(/^\/([^/?#]+)(?:[/?#]|$)/);
128581
+ if (match && amazonProxyHosts.includes(match[1])) return match[1];
128582
+ return null;
128583
+ }
128584
+ function amazonHostFromProxyUrl(url) {
128585
+ if (!url) return null;
128586
+ for (const host of amazonProxyHosts) if (url === `${proxyBase()}${host}` || url.startsWith(`${proxyBase()}${host}/`)) return host;
128587
+ return null;
128588
+ }
128551
128589
  const formerDataStorePath = _options.formerDataStorePath || path$2.join(__dirname, "formerDataStore.json");
128552
128590
  let formerDataStoreValid = false;
128553
128591
  if (!_options.formerRegistrationData) try {
@@ -128571,7 +128609,7 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128571
128609
  _options.logger && _options.logger("Proxy Init: reuse frc from former data");
128572
128610
  initialCookies.frc = _options.formerRegistrationData.frc;
128573
128611
  }
128574
- if (!_options.formerRegistrationData || !_options.formerRegistrationData["map-md"]) initialCookies["map-md"] = Buffer.from("{\"device_user_dictionary\":[],\"device_registration_data\":{\"software_version\":\"1\"},\"app_identifier\":{\"app_version\":\"2.2.485407\",\"bundle_id\":\"com.amazon.echo\"}}").toString("base64");
128612
+ if (!_options.formerRegistrationData || !_options.formerRegistrationData["map-md"]) initialCookies["map-md"] = Buffer.from(`{"device_user_dictionary":[],"device_registration_data":{"software_version":"1"},"app_identifier":{"app_version":"${ALEXA_APP_VERSION}","bundle_id":"com.amazon.echo"}}`).toString("base64");
128575
128613
  else {
128576
128614
  _options.logger && _options.logger("Proxy Init: reuse map-md from former data");
128577
128615
  initialCookies["map-md"] = _options.formerRegistrationData["map-md"];
@@ -128604,11 +128642,49 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128604
128642
  const code_verifier = base64URLEncode(crypto$1.randomBytes(32));
128605
128643
  const code_challenge = base64URLEncode(sha256(code_verifier));
128606
128644
  let proxyCookies = "";
128645
+ let returnedInitUrl;
128646
+ function buildInitialUrl() {
128647
+ return `https://www.${_options.baseAmazonPage}/ap/signin?openid.return_to=https%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fmaplanding&openid.assoc_handle=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&accountStatusPolicy=P1&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns.oa2=http%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fext%2Foauth%2F2&openid.oa2.client_id=device%3A${deviceId}&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.oa2.response_type=code&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.pape.max_auth_age=0&openid.oa2.scope=device_auth_access&openid.oa2.code_challenge_method=S256&openid.oa2.code_challenge=${code_challenge}&language=${_options.amazonPageProxyLanguage}`;
128648
+ }
128649
+ function fixEmbeddedProxyUrls(pathAndQuery) {
128650
+ const qIndex = pathAndQuery.indexOf("?");
128651
+ if (qIndex === -1) return pathAndQuery;
128652
+ const p = pathAndQuery.slice(0, qIndex);
128653
+ const qs = pathAndQuery.slice(qIndex + 1);
128654
+ let params;
128655
+ try {
128656
+ params = new URLSearchParams(qs);
128657
+ } catch (_e) {
128658
+ return pathAndQuery;
128659
+ }
128660
+ let changed = false;
128661
+ for (const key of params.keys()) {
128662
+ const value = params.get(key);
128663
+ const fixed = replaceHostsBack(value);
128664
+ if (fixed !== value) {
128665
+ params.set(key, fixed);
128666
+ changed = true;
128667
+ }
128668
+ }
128669
+ if (changed) _options.logger && _options.logger(`Alexa-Cookie: Fixed embedded proxy URL(s) in outgoing request query`);
128670
+ return changed ? `${p}?${params.toString()}` : pathAndQuery;
128671
+ }
128672
+ function rewriteProxyPath(url, req) {
128673
+ let result;
128674
+ const urlHost = amazonHostFromProxyPath(url);
128675
+ if (urlHost) result = url.replace(new RegExp(`^/${escapeRegex(urlHost)}`), "") || "/";
128676
+ else if (req && req.headers && req.headers.host === `${_options.proxyOwnIp}:${_options.proxyPort}` && url === "/") {
128677
+ const initialUrl = returnedInitUrl || buildInitialUrl();
128678
+ const parsed = new URL(initialUrl);
128679
+ result = `${parsed.pathname}${parsed.search}`;
128680
+ } else result = url;
128681
+ return fixEmbeddedProxyUrls(result);
128682
+ }
128607
128683
  const optionsAlexa = {
128608
128684
  target: `https://alexa.${_options.baseAmazonPage}`,
128609
128685
  changeOrigin: true,
128610
128686
  ws: false,
128611
- pathRewrite: {},
128687
+ pathRewrite: rewriteProxyPath,
128612
128688
  router,
128613
128689
  hostRewrite: true,
128614
128690
  followRedirects: false,
@@ -128616,17 +128692,10 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128616
128692
  onError,
128617
128693
  onProxyRes,
128618
128694
  onProxyReq,
128619
- headers: {
128620
- "user-agent": "AppleWebKit PitanguiBridge/2.2.485407.0-[HARDWARE=iPhone10_4][SOFTWARE=15.5][DEVICE=iPhone]",
128621
- "accept-language": _options.acceptLanguage,
128622
- "authority": `www.${_options.baseAmazonPage}`
128623
- },
128624
- cookieDomainRewrite: { "*": "" }
128695
+ headers: { "accept-language": _options.acceptLanguage },
128696
+ cookieDomainRewrite: { "*": "" },
128697
+ cookiePathRewrite: { "*": "/" }
128625
128698
  };
128626
- optionsAlexa.pathRewrite[`^/www.${_options.baseAmazonPage}`] = "";
128627
- optionsAlexa.pathRewrite[`^/alexa.${_options.baseAmazonPage}`] = "";
128628
- optionsAlexa.cookieDomainRewrite[`.${_options.baseAmazonPage}`] = _options.proxyOwnIp;
128629
- optionsAlexa.cookieDomainRewrite[_options.baseAmazonPage] = _options.proxyOwnIp;
128630
128699
  if (_options.logger) optionsAlexa.logProvider = function logProvider() {
128631
128700
  return {
128632
128701
  log: _options.logger.log || _options.logger,
@@ -128636,22 +128705,20 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128636
128705
  error: _options.logger.error || _options.logger
128637
128706
  };
128638
128707
  };
128639
- let returnedInitUrl;
128640
128708
  function router(req) {
128641
128709
  const url = req.originalUrl || req.url;
128642
128710
  _options.logger && _options.logger(`Router: ${url} / ${req.method} / ${JSON.stringify(req.headers)}`);
128643
128711
  if (req.headers.host === `${_options.proxyOwnIp}:${_options.proxyPort}`) {
128644
- if (url.startsWith(`/www.${_options.baseAmazonPage}/`)) return `https://www.${_options.baseAmazonPage}`;
128645
- else if (url.startsWith(`/alexa.${_options.baseAmazonPage}/`)) return `https://alexa.${_options.baseAmazonPage}`;
128646
- else if (req.headers.referer) {
128647
- if (req.headers.referer.startsWith(`http://${_options.proxyOwnIp}:${_options.proxyPort}/www.${_options.baseAmazonPage}/`)) return `https://www.${_options.baseAmazonPage}`;
128648
- else if (req.headers.referer.startsWith(`http://${_options.proxyOwnIp}:${_options.proxyPort}/alexa.${_options.baseAmazonPage}/`)) return `https://alexa.${_options.baseAmazonPage}`;
128649
- }
128712
+ const urlHost = amazonHostFromProxyPath(url);
128713
+ if (urlHost) return `https://${urlHost}`;
128714
+ const refererHost = amazonHostFromProxyUrl(req.headers.referer);
128715
+ if (refererHost) return `https://${refererHost}`;
128650
128716
  if (url === "/") {
128651
- returnedInitUrl = `https://www.${_options.baseAmazonPage}/ap/signin?openid.return_to=https%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fmaplanding&openid.assoc_handle=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&accountStatusPolicy=P1&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns.oa2=http%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fext%2Foauth%2F2&openid.oa2.client_id=device%3A${deviceId}&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.oa2.response_type=code&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.pape.max_auth_age=0&openid.oa2.scope=device_auth_access&openid.oa2.code_challenge_method=S256&openid.oa2.code_challenge=${code_challenge}&language=${_options.amazonPageProxyLanguage}`;
128717
+ returnedInitUrl = buildInitialUrl();
128652
128718
  _options.logger && _options.logger(`Alexa-Cookie: Initial Page Request: ${returnedInitUrl}`);
128653
- return returnedInitUrl;
128654
- } else return `https://www.${_options.baseAmazonPage}`;
128719
+ const parsedInitUrl = new URL(returnedInitUrl);
128720
+ return `${parsedInitUrl.protocol}//${parsedInitUrl.host}`;
128721
+ } else return `https://${defaultAmazonHost}`;
128655
128722
  }
128656
128723
  return `https://alexa.${_options.baseAmazonPage}`;
128657
128724
  }
@@ -128663,21 +128730,34 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128663
128730
  } catch (err) {}
128664
128731
  }
128665
128732
  function replaceHosts(data) {
128666
- const amazonRegex = new RegExp(`https?://www.${_options.baseAmazonPage}:?[0-9]*/`.replace(/\./g, "\\."), "g");
128667
- const alexaRegex = new RegExp(`https?://alexa.${_options.baseAmazonPage}:?[0-9]*/`.replace(/\./g, "\\."), "g");
128668
128733
  data = data.replace(/&#x2F;/g, "/");
128669
- data = data.replace(amazonRegex, `http://${_options.proxyOwnIp}:${_options.proxyPort}/www.${_options.baseAmazonPage}/`);
128670
- data = data.replace(alexaRegex, `http://${_options.proxyOwnIp}:${_options.proxyPort}/alexa.${_options.baseAmazonPage}/`);
128734
+ for (const host of amazonProxyHosts) {
128735
+ const hostRegex = new RegExp(`https?://${escapeRegex(host)}:?[0-9]*/`, "g");
128736
+ data = data.replace(hostRegex, `${proxyBase()}${host}/`);
128737
+ }
128671
128738
  return data;
128672
128739
  }
128673
128740
  function replaceHostsBack(data) {
128674
- const amazonRegex = new RegExp(`http://${_options.proxyOwnIp}:${_options.proxyPort}/www.${_options.baseAmazonPage}/`.replace(/\./g, "\\."), "g");
128675
- const alexaRegex = new RegExp(`http://${_options.proxyOwnIp}:${_options.proxyPort}/alexa.${_options.baseAmazonPage}/`.replace(/\./g, "\\."), "g");
128676
- data = data.replace(amazonRegex, `https://www.${_options.baseAmazonPage}/`);
128677
- data = data.replace(alexaRegex, `https://alexa.${_options.baseAmazonPage}/`);
128678
- if (data === `http://${_options.proxyOwnIp}:${_options.proxyPort}/`) data = returnedInitUrl;
128741
+ const base = proxyBase();
128742
+ for (const host of amazonProxyHosts) {
128743
+ const hostRegex = new RegExp(`${escapeRegex(base)}${escapeRegex(host)}/`, "g");
128744
+ data = data.replace(hostRegex, `https://${host}/`);
128745
+ }
128746
+ if (data === base) data = returnedInitUrl;
128747
+ else if (data.startsWith(base)) data = `https://${defaultAmazonHost}/${data.slice(base.length)}`;
128679
128748
  return data;
128680
128749
  }
128750
+ function parseQueryParams(data) {
128751
+ const paramStart = data && data.indexOf("?");
128752
+ if (paramStart === -1 || paramStart === void 0) return {};
128753
+ return querystring$2.parse(data.substr(paramStart + 1));
128754
+ }
128755
+ function isProxySuccessUrl(data) {
128756
+ if (!data) return false;
128757
+ if (data.includes("/spa/index.html")) return true;
128758
+ if (!data.includes("/ap/maplanding?")) return false;
128759
+ return !!parseQueryParams(data)["openid.oa2.authorization_code"];
128760
+ }
128681
128761
  function onProxyReq(proxyReq, req) {
128682
128762
  const url = req.originalUrl || req.url;
128683
128763
  if (url.endsWith(".ico") || url.endsWith(".js") || url.endsWith(".ttf") || url.endsWith(".svg") || url.endsWith(".png") || url.endsWith(".appcache")) return;
@@ -128704,14 +128784,26 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128704
128784
  }
128705
128785
  }
128706
128786
  if (typeof proxyReq.getHeader === "function" && proxyReq.getHeader("origin") !== `https://${proxyReq.getHeader("host")}`) {
128707
- proxyReq.setHeader("origin", `https://www.${_options.baseAmazonPage}`);
128708
- _options.logger && _options.logger("Alexa-Cookie: Modify headers: Delete Origin");
128787
+ proxyReq.setHeader("origin", `https://${proxyReq.getHeader("host") || defaultAmazonHost}`);
128788
+ _options.logger && _options.logger("Alexa-Cookie: Modify headers: Changed Origin");
128709
128789
  modified = true;
128710
128790
  }
128711
- let postBody = "";
128712
- req.on("data", (chunk) => {
128713
- postBody += chunk.toString();
128714
- });
128791
+ if (req.body && typeof req.body === "object") {
128792
+ let bodyChanged = false;
128793
+ for (const key of Object.keys(req.body)) {
128794
+ const value = req.body[key];
128795
+ if (typeof value !== "string") continue;
128796
+ const fixed = replaceHostsBack(value);
128797
+ if (fixed !== value) {
128798
+ req.body[key] = fixed;
128799
+ bodyChanged = true;
128800
+ }
128801
+ }
128802
+ if (bodyChanged) _options.logger && _options.logger("Alexa-Cookie: Fixed embedded proxy URL(s) in outgoing POST body");
128803
+ const bodyData = querystring$2.stringify(req.body);
128804
+ proxyReq.setHeader("Content-Length", Buffer.byteLength(bodyData));
128805
+ proxyReq.write(bodyData);
128806
+ }
128715
128807
  }
128716
128808
  _options.proxyLogLevel === "debug" && _options.logger && _options.logger(`Alexa-Cookie: Proxy-Request: (modified:${modified})${customStringify(proxyReq, null, 2)}`);
128717
128809
  }
@@ -128729,17 +128821,22 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128729
128821
  proxyCookies = addCookies(proxyCookies, proxyRes.headers);
128730
128822
  }
128731
128823
  _options.logger && _options.logger(`Alexa-Cookie: Cookies handled: ${JSON.stringify(proxyCookies)}`);
128732
- if (proxyRes.socket && proxyRes.socket._host === `www.${_options.baseAmazonPage}` && proxyRes.socket.parser.outgoing && proxyRes.socket.parser.outgoing.method === "GET" && proxyRes.socket.parser.outgoing.path.startsWith("/ap/maplanding") || proxyRes.socket && proxyRes.socket.parser.outgoing && proxyRes.socket.parser.outgoing.getHeader("location") && proxyRes.socket.parser.outgoing.getHeader("location").includes("/ap/maplanding?") || proxyRes.headers.location && (proxyRes.headers.location.includes("/ap/maplanding?") || proxyRes.headers.location.includes("/spa/index.html"))) {
128824
+ const outgoing = proxyRes.socket && proxyRes.socket.parser && proxyRes.socket.parser.outgoing;
128825
+ const successUrl = [
128826
+ proxyRes.headers.location,
128827
+ outgoing && outgoing.method === "GET" && outgoing.path,
128828
+ outgoing && outgoing.getHeader && outgoing.getHeader("location")
128829
+ ].find(isProxySuccessUrl);
128830
+ if (successUrl) {
128733
128831
  _options.logger && _options.logger("Alexa-Cookie: Proxy detected SUCCESS!!");
128734
- const paramStart = proxyRes.headers.location.indexOf("?");
128735
- const queryParams = querystring$2.parse(proxyRes.headers.location.substr(paramStart + 1));
128832
+ const queryParams = parseQueryParams(successUrl);
128736
128833
  proxyRes.statusCode = 302;
128737
128834
  proxyRes.headers.location = `http://${_options.proxyOwnIp}:${_options.proxyPort}/cookie-success`;
128738
128835
  delete proxyRes.headers.referer;
128739
128836
  _options.logger && _options.logger(`Alexa-Cookie: Proxy catched cookie: ${proxyCookies}`);
128740
128837
  _options.logger && _options.logger(`Alexa-Cookie: Proxy catched parameters: ${JSON.stringify(queryParams)}`);
128741
128838
  callbackCookie && callbackCookie(null, {
128742
- "loginCookie": proxyCookies,
128839
+ "loginCookie": sanitizeAmazonCookie(proxyCookies),
128743
128840
  "authorization_code": queryParams["openid.oa2.authorization_code"],
128744
128841
  "frc": initialCookies.frc,
128745
128842
  "map-md": initialCookies["map-md"],
@@ -128751,7 +128848,7 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128751
128848
  if (proxyRes.headers.location) {
128752
128849
  _options.logger && _options.logger(`Redirect: Original Location ----> ${proxyRes.headers.location}`);
128753
128850
  proxyRes.headers.location = replaceHosts(proxyRes.headers.location);
128754
- if (reqestHost && proxyRes.headers.location.startsWith("/")) proxyRes.headers.location = `http://${_options.proxyOwnIp}:${_options.proxyPort}/${reqestHost}${proxyRes.headers.location}`;
128851
+ if (reqestHost && amazonProxyHosts.includes(reqestHost) && proxyRes.headers.location.startsWith("/")) proxyRes.headers.location = `http://${_options.proxyOwnIp}:${_options.proxyPort}/${reqestHost}${proxyRes.headers.location}`;
128755
128852
  _options.logger && _options.logger(`Redirect: Final Location ----> ${proxyRes.headers.location}`);
128756
128853
  return;
128757
128854
  }
@@ -128769,8 +128866,21 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128769
128866
  return body;
128770
128867
  });
128771
128868
  }
128869
+ function bufferUrlencodedBody(req, res, next) {
128870
+ const contentType = req.headers["content-type"] || "";
128871
+ if (req.method !== "POST" || contentType.indexOf("application/x-www-form-urlencoded") === -1) return next();
128872
+ let raw = "";
128873
+ req.on("data", (chunk) => {
128874
+ raw += chunk;
128875
+ });
128876
+ req.on("end", () => {
128877
+ req.body = querystring$2.parse(raw);
128878
+ next();
128879
+ });
128880
+ }
128772
128881
  const myProxy = proxy("!/cookie-success", optionsAlexa);
128773
128882
  const app = express();
128883
+ app.use(bufferUrlencodedBody);
128774
128884
  app.use(myProxy);
128775
128885
  app.get("/cookie-success", function(req, res) {
128776
128886
  res.send(_options.proxyCloseWindowHTML);
@@ -128780,7 +128890,8 @@ var require_proxy = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128780
128890
  _options.proxyPort = void 0;
128781
128891
  }
128782
128892
  const server = app.listen(_options.proxyPort, _options.proxyListenBind, function() {
128783
- _options.logger && _options.logger(`Alexa-Cookie: Proxy-Server listening on port ${server.address().port}`);
128893
+ _options.proxyPort = this.address().port;
128894
+ _options.logger && _options.logger(`Alexa-Cookie: Proxy-Server listening on port ${_options.proxyPort}`);
128784
128895
  callbackListening && callbackListening(server);
128785
128896
  callbackListening = null;
128786
128897
  }).on("error", (err) => {
@@ -128826,6 +128937,13 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128826
128937
  let proxyServer;
128827
128938
  let _options;
128828
128939
  let Cookie = "";
128940
+ const isAmazonCookieName = (name) => /^(session-id|session-id-time|session-token|ubid-.+|lc-.+|x-.+|at-.+|sess-at-.+|frc|map-md|csrf|sid|csm-hit|i18n-prefs|sp-cdn|skin)$/.test(name);
128941
+ const sanitizeAmazonCookie = (cookie) => {
128942
+ const cookies = cookieTools.parse(cookie || "");
128943
+ let sanitizedCookie = "";
128944
+ for (const name of Object.keys(cookies)) if (isAmazonCookieName(name)) sanitizedCookie += `${name}=${cookies[name]}; `;
128945
+ return sanitizedCookie.replace(/[; ]*$/, "");
128946
+ };
128829
128947
  const addCookies = (Cookie, headers) => {
128830
128948
  if (!headers || !headers["set-cookie"]) return Cookie;
128831
128949
  const cookies = cookieTools.parse(Cookie || "");
@@ -128913,7 +129031,7 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128913
129031
  if (!_options.baseAmazonPageHandle && _options.baseAmazonPageHandle !== "") {
128914
129032
  const amazonDomain = _options.baseAmazonPage.substr(_options.baseAmazonPage.lastIndexOf(".") + 1);
128915
129033
  if (amazonDomain === "jp") _options.baseAmazonPageHandle = `_${amazonDomain}`;
128916
- else if (amazonDomain !== "com") _options.baseAmazonPageHandle = "";
129034
+ else if (amazonDomain !== "com") _options.baseAmazonPageHandle = `_${amazonDomain}`;
128917
129035
  else _options.baseAmazonPageHandle = "";
128918
129036
  }
128919
129037
  if (!_options.userAgent) if (os$2.platform() === "win32") _options.userAgent = defaultUserAgent;
@@ -128940,7 +129058,7 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
128940
129058
  if (_options.formerRegistrationData) _options.proxyOnly = true;
128941
129059
  };
128942
129060
  const getCSRFFromCookies = (cookie, _options, callback) => {
128943
- const csrfUrls = csrfOptions;
129061
+ const csrfUrls = [...csrfOptions];
128944
129062
  function csrfTry() {
128945
129063
  const path = csrfUrls.shift();
128946
129064
  const options = {
@@ -129103,6 +129221,7 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129103
129221
  const handleTokenRegistration = (_options, loginData, callback) => {
129104
129222
  _options.logger && _options.logger(`Handle token registration Start: ${JSON.stringify(loginData)}`);
129105
129223
  loginData.deviceAppName = _options.deviceAppName;
129224
+ loginData.loginCookie = sanitizeAmazonCookie(loginData.loginCookie);
129106
129225
  let deviceSerial;
129107
129226
  if (!_options.formerRegistrationData || !_options.formerRegistrationData.deviceSerial) {
129108
129227
  const deviceSerialBuffer = Buffer.alloc(16);
@@ -129190,6 +129309,7 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129190
129309
  Cookie = addCookies(Cookie, response.headers);
129191
129310
  loginData.refreshToken = body.response.success.tokens.bearer.refresh_token;
129192
129311
  const accessToken = body.response.success.tokens.bearer.access_token;
129312
+ loginData.accessToken = accessToken;
129193
129313
  loginData.tokenDate = Date.now();
129194
129314
  loginData.macDms = body.response.success.tokens.mac_dms;
129195
129315
  if (body.response.success.tokens.website_cookies && Array.isArray(body.response.success.tokens.website_cookies)) {
@@ -129216,29 +129336,43 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129216
129336
  _options.logger && _options.logger("Alexa-Cookie: Get User data");
129217
129337
  _options.logger && _options.logger(JSON.stringify(options));
129218
129338
  request(options, (error, response, body) => {
129219
- if (!error) {
129339
+ if (!error) if (!response || response.statusCode < 200 || response.statusCode >= 300 || !body) {
129340
+ _options.logger && _options.logger(`Get User data Response: status=${response && response.statusCode}, body=${JSON.stringify(body)}`);
129341
+ if (!_options.amazonPage) {
129342
+ callback && callback(/* @__PURE__ */ new Error(`Could not get user data from Amazon: HTTP ${response && response.statusCode}`), null);
129343
+ return;
129344
+ }
129345
+ loginData.amazonPage = _options.amazonPage;
129346
+ } else {
129220
129347
  try {
129221
129348
  if (typeof body !== "object") body = JSON.parse(body);
129222
129349
  } catch (err) {
129223
129350
  _options.logger && _options.logger(`Get User data Response: ${JSON.stringify(body)}`);
129224
- callback && callback(err, null);
129225
- return;
129351
+ if (!_options.amazonPage) {
129352
+ callback && callback(err, null);
129353
+ return;
129354
+ }
129355
+ loginData.amazonPage = _options.amazonPage;
129226
129356
  }
129227
129357
  _options.logger && _options.logger(`Get User data Response: ${JSON.stringify(body)}`);
129228
129358
  Cookie = addCookies(Cookie, response.headers);
129229
- if (body.marketPlaceDomainName) {
129359
+ if (body && body.marketPlaceDomainName) {
129230
129360
  const pos = body.marketPlaceDomainName.indexOf(".");
129231
129361
  if (pos !== -1) _options.amazonPage = body.marketPlaceDomainName.substr(pos + 1);
129232
129362
  }
129233
129363
  loginData.amazonPage = _options.amazonPage;
129234
- } else if (error && (!_options || !_options.amazonPage)) {
129364
+ }
129365
+ else if (error && (!_options || !_options.amazonPage)) {
129235
129366
  callback && callback(error, null);
129236
129367
  return;
129237
129368
  } else if (error && (!_options.formerRegistrationData || !_options.formerRegistrationData.amazonPage) && _options.amazonPage) _options.logger && _options.logger(`Continue with externally set amazonPage: ${_options.amazonPage}`);
129238
129369
  else if (error) _options.logger && _options.logger("Ignore error while getting user data and amazonPage because previously set amazonPage is available");
129239
129370
  loginData.loginCookie = Cookie;
129240
129371
  getLocalCookies(loginData.amazonPage, loginData.refreshToken, (err, localCookie) => {
129241
- if (err) callback && callback(err, null);
129372
+ if (err) {
129373
+ callback && callback(err, null);
129374
+ return;
129375
+ }
129242
129376
  loginData.localCookie = localCookie;
129243
129377
  getCSRFFromCookies(loginData.localCookie, _options, (err, resData) => {
129244
129378
  if (err) {
@@ -129247,7 +129381,6 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129247
129381
  }
129248
129382
  loginData.localCookie = resData.cookie;
129249
129383
  loginData.csrf = resData.csrf;
129250
- delete loginData.accessToken;
129251
129384
  delete loginData.authorization_code;
129252
129385
  delete loginData.verifier;
129253
129386
  loginData.dataVersion = 2;
@@ -129350,6 +129483,39 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129350
129483
  callback && callback(null, localCookie);
129351
129484
  });
129352
129485
  };
129486
+ const finishCookieRefresh = (loginData, callback) => {
129487
+ _options.logger && _options.logger("Alexa-Cookie: Skip App registration during refresh and update local cookies");
129488
+ const updateLocalCookies = () => {
129489
+ const amazonPage = loginData.amazonPage || _options.amazonPage;
129490
+ getLocalCookies(amazonPage, loginData.refreshToken, (err, localCookie) => {
129491
+ if (err) {
129492
+ callback && callback(err, null);
129493
+ return;
129494
+ }
129495
+ loginData.localCookie = localCookie;
129496
+ getCSRFFromCookies(loginData.localCookie, _options, (err, resData) => {
129497
+ if (err) {
129498
+ callback && callback(/* @__PURE__ */ new Error(`Error getting csrf for ${amazonPage}`), null);
129499
+ return;
129500
+ }
129501
+ loginData.localCookie = resData.cookie;
129502
+ loginData.csrf = resData.csrf;
129503
+ loginData.amazonPage = amazonPage;
129504
+ loginData.tokenDate = Date.now();
129505
+ delete loginData.authorization_code;
129506
+ delete loginData.verifier;
129507
+ loginData.dataVersion = 2;
129508
+ _options.logger && _options.logger("Alexa-Cookie: Refresh finished with updated cookies and csrf");
129509
+ callback && callback(null, loginData);
129510
+ });
129511
+ });
129512
+ };
129513
+ if (loginData.accessToken) {
129514
+ registerTokenCapabilities(loginData.accessToken, updateLocalCookies);
129515
+ return;
129516
+ }
129517
+ updateLocalCookies();
129518
+ };
129353
129519
  this.refreshAlexaCookie = (__options, callback) => {
129354
129520
  if (!__options || !__options.formerRegistrationData || !__options.formerRegistrationData.loginCookie || !__options.formerRegistrationData.refreshToken) {
129355
129521
  callback && callback(/* @__PURE__ */ new Error("No former registration data provided for Cookie Refresh"), null);
@@ -129416,13 +129582,16 @@ var require_alexa_cookie = /* @__PURE__ */ __commonJSMin(((exports, module) => {
129416
129582
  _options.formerRegistrationData.loginCookie = addCookies(Cookie, response.headers);
129417
129583
  _options.formerRegistrationData.accessToken = body.access_token;
129418
129584
  getLocalCookies(_options.baseAmazonPage, _options.formerRegistrationData.refreshToken, (err, comCookie) => {
129419
- if (err) callback && callback(err, null);
129585
+ if (err) {
129586
+ callback && callback(err, null);
129587
+ return;
129588
+ }
129420
129589
  const initCookies = cookieTools.parse(_options.formerRegistrationData.loginCookie);
129421
129590
  let newCookie = `frc=${initCookies.frc}; `;
129422
129591
  newCookie += `map-md=${initCookies["map-md"]}; `;
129423
129592
  newCookie += comCookie;
129424
- _options.formerRegistrationData.loginCookie = newCookie;
129425
- handleTokenRegistration(_options, _options.formerRegistrationData, callback);
129593
+ _options.formerRegistrationData.loginCookie = sanitizeAmazonCookie(newCookie);
129594
+ finishCookieRefresh(_options.formerRegistrationData, callback);
129426
129595
  });
129427
129596
  });
129428
129597
  };
@@ -130017,10 +130186,20 @@ var import_alexa_remote = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin
130017
130186
  listItemId: payload.listItemId
130018
130187
  });
130019
130188
  return;
130189
+ case "NotifyNowPlayingUpdated":
130190
+ const nowPlaying = payload?.update?.update?.nowPlayingData ?? null;
130191
+ this.emit("ws-now-playing-updated", {
130192
+ customerId: payload?.customerId ?? null,
130193
+ taskSessionId: payload?.update?.taskSessionId ?? null,
130194
+ cause: payload?.update?.update?.cause ?? null,
130195
+ mediaId: nowPlaying?.mediaId ?? null,
130196
+ playerState: nowPlaying?.playerState ?? null,
130197
+ nowPlayingData: nowPlaying
130198
+ });
130199
+ return;
130020
130200
  case "PUSH_MICROPHONE_STATE":
130021
130201
  case "PUSH_DELETE_DOPPLER_ACTIVITIES":
130022
130202
  case "PUSH_DEVICE_SETUP_STATE_CHANGE":
130023
- case "NotifyNowPlayingUpdated":
130024
130203
  case "NotifyMediaSessionsUpdated": return;
130025
130204
  }
130026
130205
  this.emit("ws-unknown-command", command, payload);
@@ -132067,7 +132246,7 @@ var import_alexa_remote = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin
132067
132246
  */
132068
132247
  var ALEXA_API_USER_AGENT_POSTFIX = `AlexaRemote/${{
132069
132248
  name: "alexa-remote2",
132070
- version: "8.0.4",
132249
+ version: "8.1.1",
132071
132250
  description: "Remote Control for amazon echo devices",
132072
132251
  author: {
132073
132252
  "name": "Apollon77",
@@ -132096,16 +132275,16 @@ var ALEXA_API_USER_AGENT_POSTFIX = `AlexaRemote/${{
132096
132275
  "layla.amazon.de"
132097
132276
  ],
132098
132277
  dependencies: {
132099
- "alexa-cookie2": "^5.0.3",
132278
+ "alexa-cookie2": "^5.0.6",
132100
132279
  "https": "^1.0.0",
132101
132280
  "querystring": "^0.2.1",
132102
- "ws": "^8.18.3",
132281
+ "ws": "^8.21.0",
132103
132282
  "extend": "^3.0.2",
132104
- "uuid": "^11.1.0"
132283
+ "uuid": "^11.1.1"
132105
132284
  },
132106
132285
  devDependencies: {
132107
- "@alcalzone/release-script": "^5.0.0",
132108
- "@alcalzone/release-script-plugin-license": "^4.0.0",
132286
+ "@alcalzone/release-script": "^5.2.1",
132287
+ "@alcalzone/release-script-plugin-license": "^5.2.0",
132109
132288
  "eslint": "^8.57.1"
132110
132289
  },
132111
132290
  scripts: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-import-alexa",
3
- "version": "0.2.157",
3
+ "version": "0.2.158",
4
4
  "description": "Alexa device-import provider for CamStack — imports the smart-home devices in a user's Alexa account via the unofficial alexa-remote2 cookie/token client (the inverse of the Alexa exporter)",
5
5
  "keywords": [
6
6
  "camstack",
@@ -100,7 +100,7 @@
100
100
  },
101
101
  "devDependencies": {
102
102
  "@camstack/types": "*",
103
- "alexa-remote2": "^8.0.4",
103
+ "alexa-remote2": "^8.1.1",
104
104
  "typescript": "~6.0.3",
105
105
  "vite": "^8.0.11",
106
106
  "vitest": "^2.0.0",