basklee-svelte 0.9.1 → 0.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basklee-svelte",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "SvelteKit 2 storefront for your Basklee marketplace: setup CLI, server API gateway, typed store and ready-made catalog, cart, checkout and account components in English, Russian and Spanish, plus an Android app build.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -1,6 +1,7 @@
1
1
  import { chown, mkdir, readFile, readdir, rename, stat, unlink, writeFile } from 'node:fs/promises';
2
2
  import { dirname, relative, resolve } from 'node:path';
3
3
  import {
4
+ BlockList,
4
5
  getDefaultAutoSelectFamilyAttemptTimeout,
5
6
  isIP,
6
7
  setDefaultAutoSelectFamilyAttemptTimeout,
@@ -24,6 +25,62 @@ if (getDefaultAutoSelectFamilyAttemptTimeout() < connectTimeout) {
24
25
  const requestTimeout = 30_000;
25
26
  const uploadTimeout = 300_000;
26
27
 
28
+ // Cloudflare's edge addresses (https://www.cloudflare.com/ips-v4 and /ips-v6,
29
+ // checked 2026-11-18). A storefront behind Cloudflare is reached from one of
30
+ // them, so the address a proxy in front of it passes on is Cloudflare's, not
31
+ // the visitor's. Cloudflare puts the visitor's address into CF-Connecting-IP
32
+ // and overwrites any value a visitor sends; the header is taken only when the
33
+ // address it came with is a Cloudflare one. Nothing to set up: a storefront
34
+ // that is not behind Cloudflare keeps the address it gets.
35
+ const cloudflareRanges = [
36
+ '173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', '103.31.4.0/22',
37
+ '141.101.64.0/18', '108.162.192.0/18', '190.93.240.0/20', '188.114.96.0/20',
38
+ '197.234.240.0/22', '198.41.128.0/17', '162.158.0.0/15', '104.16.0.0/13',
39
+ '104.24.0.0/14', '172.64.0.0/13', '131.0.72.0/22',
40
+ '2400:cb00::/32', '2606:4700::/32', '2803:f800::/32', '2405:b500::/32',
41
+ '2405:8100::/32', '2a06:98c0::/29', '2c0f:f248::/32',
42
+ ];
43
+ const cloudflare = new BlockList();
44
+ for (const range of cloudflareRanges) {
45
+ const [address, prefix] = range.split('/');
46
+ cloudflare.addSubnet(address, Number(prefix), address.includes(':') ? 'ipv6' : 'ipv4');
47
+ }
48
+
49
+ /** One address written plainly ('::ffff:1.2.3.4' as 1.2.3.4), or ''. */
50
+ function plainAddress(value) {
51
+ let address = String(value ?? '').trim();
52
+ const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i.exec(address);
53
+ if (mapped) {
54
+ address = mapped[1];
55
+ }
56
+ return isIP(address) ? address : '';
57
+ }
58
+
59
+ /** True for an address of Cloudflare's edge. */
60
+ export function isCloudflareAddress(value) {
61
+ const address = plainAddress(value);
62
+ if (!address) {
63
+ return false;
64
+ }
65
+ return cloudflare.check(address, isIP(address) === 6 ? 'ipv6' : 'ipv4');
66
+ }
67
+
68
+ /**
69
+ * The visitor's address for the backend: X-Init-Ip, X-Real-IP or the first
70
+ * X-Forwarded-For entry, and CF-Connecting-IP instead when that address is a
71
+ * Cloudflare edge (or there is no proxy address at all) and Cloudflare sent one.
72
+ * @param {Headers} headers
73
+ */
74
+ export function marketplaceClientIp(headers) {
75
+ const proxied = headers.get('x-init-ip') || headers.get('x-real-ip');
76
+ const forwarded = headers.get('x-forwarded-for')?.split(',')[0]?.trim();
77
+ const visitor = plainAddress(headers.get('cf-connecting-ip'));
78
+ if (visitor && (!proxied || isCloudflareAddress(proxied))) {
79
+ return visitor;
80
+ }
81
+ return proxied || forwarded || '';
82
+ }
83
+
27
84
  /** @param {Request} request */
28
85
  export function marketplaceRequestTimeout(request) {
29
86
  const contentType = request.headers.get('content-type') ?? '';
@@ -320,10 +377,7 @@ export async function forwardMarketplace(request, configuration = {}) {
320
377
  headers.set('origin', `https://${tenant.domain}`);
321
378
  }
322
379
  headers.set('Accept-Encoding', 'identity');
323
- const clientIp =
324
- request.headers.get('x-init-ip') ||
325
- request.headers.get('x-real-ip') ||
326
- request.headers.get('x-forwarded-for')?.split(',')[0]?.trim();
380
+ const clientIp = marketplaceClientIp(request.headers);
327
381
  if (clientIp) {
328
382
  headers.set('X-Init-Ip', clientIp);
329
383
  }
@@ -382,6 +382,9 @@ location / {
382
382
 
383
383
  Here localhost is your frontend process on your VPS. That process selects the
384
384
  remote personal API from the domain file. The proxy must overwrite those headers.
385
+ Behind Cloudflare, \`$remote_addr\` is a Cloudflare address: the storefront
386
+ notices it and sends the customer's own address from \`CF-Connecting-IP\`
387
+ instead, with nothing to set up.
385
388
  An unknown domain never falls back to another tenant. \`/robots.txt\` points
386
389
  crawlers to \`/sitemap.xml\`, which lists the public pages, categories, products
387
390
  on sale and journal publications in every storefront language (each publication
@@ -21,6 +21,7 @@ const FORWARDED = [
21
21
  'x-init-ip',
22
22
  'x-real-ip',
23
23
  'x-forwarded-for',
24
+ 'cf-connecting-ip',
24
25
  'x-tz',
25
26
  ];
26
27