@lynkow/astro 0.0.0-stage → 0.1.0
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/LICENSE +29 -0
- package/README.md +390 -2
- package/dist/chunk-67VKUBOP.js +40 -0
- package/dist/chunk-67VKUBOP.js.map +1 -0
- package/dist/chunk-7ZL7TFBK.js +28 -0
- package/dist/chunk-7ZL7TFBK.js.map +1 -0
- package/dist/chunk-DQXJDZWU.js +9 -0
- package/dist/chunk-DQXJDZWU.js.map +1 -0
- package/dist/chunk-OGQ25G7G.js +329 -0
- package/dist/chunk-OGQ25G7G.js.map +1 -0
- package/dist/chunk-USZSRPT5.js +119 -0
- package/dist/chunk-USZSRPT5.js.map +1 -0
- package/dist/client/index.d.ts +60 -0
- package/dist/client/index.js +59 -0
- package/dist/client/index.js.map +1 -0
- package/dist/clients-DLlMBN4c.d.ts +10 -0
- package/dist/config-BVCvQ8Wx.d.ts +66 -0
- package/dist/index.d.ts +353 -0
- package/dist/index.js +395 -0
- package/dist/index.js.map +1 -0
- package/dist/integration.d.ts +37 -0
- package/dist/integration.js +51 -0
- package/dist/integration.js.map +1 -0
- package/dist/lynkow-cache-BKZFJRTI.d.ts +150 -0
- package/dist/middleware/index.d.ts +187 -0
- package/dist/middleware/index.js +582 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/next-headers-3VG6LM4M.js +3 -0
- package/dist/next-headers-3VG6LM4M.js.map +1 -0
- package/dist/next-navigation-VNM5XXCX.js +7 -0
- package/dist/next-navigation-VNM5XXCX.js.map +1 -0
- package/dist/react/index.d.ts +134 -0
- package/dist/react/index.js +128 -0
- package/dist/react/index.js.map +1 -0
- package/dist/server/index.d.ts +493 -0
- package/dist/server/index.js +989 -0
- package/dist/server/index.js.map +1 -0
- package/dist/site-locales-tmcu43n2.d.ts +80 -0
- package/package.json +91 -4
|
@@ -0,0 +1,989 @@
|
|
|
1
|
+
import { REVALIDATE, TAGS, lynkowCache, uncached, baseClientConfig, cached, TAGS2 } from '../chunk-OGQ25G7G.js';
|
|
2
|
+
import { createLocalePaths } from '../chunk-USZSRPT5.js';
|
|
3
|
+
export { currentRequest, runWithRequest } from '../chunk-7ZL7TFBK.js';
|
|
4
|
+
import { createClient, isLynkowError } from 'lynkow';
|
|
5
|
+
import { isIP } from 'node:net';
|
|
6
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
7
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
8
|
+
|
|
9
|
+
var HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
10
|
+
function resolveBuyerForwarding(headers) {
|
|
11
|
+
try {
|
|
12
|
+
const serverKey = readEnv("LYNKOW_SERVER_KEY");
|
|
13
|
+
if (!serverKey) return {};
|
|
14
|
+
const clientIp = resolveClientIp(headers, readEnv("LYNKOW_CLIENT_IP_HEADER"));
|
|
15
|
+
return clientIp ? { serverKey, clientIp } : {};
|
|
16
|
+
} catch {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function hasServerKey() {
|
|
21
|
+
return readEnv("LYNKOW_SERVER_KEY") !== void 0;
|
|
22
|
+
}
|
|
23
|
+
async function requestForwardingHeaders() {
|
|
24
|
+
let incoming;
|
|
25
|
+
try {
|
|
26
|
+
const { headers } = await import('../next-headers-3VG6LM4M.js');
|
|
27
|
+
incoming = await headers();
|
|
28
|
+
} catch (error) {
|
|
29
|
+
await rethrowNextSignal(error);
|
|
30
|
+
return void 0;
|
|
31
|
+
}
|
|
32
|
+
const { serverKey, clientIp } = resolveBuyerForwarding(incoming);
|
|
33
|
+
return serverKey && clientIp ? { "X-Lynkow-Server-Key": serverKey, "X-Lynkow-Client-Ip": clientIp } : void 0;
|
|
34
|
+
}
|
|
35
|
+
async function rethrowNextSignal(error) {
|
|
36
|
+
let rethrow;
|
|
37
|
+
try {
|
|
38
|
+
rethrow = (await import('../next-navigation-VNM5XXCX.js')).unstable_rethrow;
|
|
39
|
+
} catch {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
rethrow?.(error);
|
|
43
|
+
}
|
|
44
|
+
function readEnv(name) {
|
|
45
|
+
const value = typeof process === "undefined" ? void 0 : process.env[name];
|
|
46
|
+
return typeof value === "string" && value.trim() ? value.trim() : void 0;
|
|
47
|
+
}
|
|
48
|
+
function resolveClientIp(headers, overrideHeader) {
|
|
49
|
+
if (overrideHeader) return singleIp(headers, overrideHeader);
|
|
50
|
+
const entries = headers.get("x-forwarded-for")?.split(",") ?? [];
|
|
51
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
52
|
+
const entry = entries[i]?.trim();
|
|
53
|
+
if (!entry) continue;
|
|
54
|
+
if (isIP(entry) === 0) return void 0;
|
|
55
|
+
if (!isPublicIp(entry)) continue;
|
|
56
|
+
return entry;
|
|
57
|
+
}
|
|
58
|
+
return void 0;
|
|
59
|
+
}
|
|
60
|
+
function singleIp(headers, name) {
|
|
61
|
+
if (!HEADER_NAME.test(name)) return void 0;
|
|
62
|
+
const value = headers.get(name)?.trim();
|
|
63
|
+
return value && !value.includes(",") && isIP(value) !== 0 ? value : void 0;
|
|
64
|
+
}
|
|
65
|
+
function isPublicIp(ip) {
|
|
66
|
+
const v4 = ipv4Octets(ip);
|
|
67
|
+
if (v4) return isPublicIpv4(v4);
|
|
68
|
+
const groups = ipv6Groups(ip);
|
|
69
|
+
if (!groups) return false;
|
|
70
|
+
if (groups.every((group) => group === 0)) return false;
|
|
71
|
+
if (groups.slice(0, 7).every((group) => group === 0) && groups[7] === 1) return false;
|
|
72
|
+
const first = groups[0] ?? 0;
|
|
73
|
+
return (first & 65024) !== 64512 && (first & 65472) !== 65152 && (first & 65280) !== 65280;
|
|
74
|
+
}
|
|
75
|
+
function isPublicIpv4([a, b]) {
|
|
76
|
+
return !(a === 0 || a === 10 || a === 127 || a === 100 && b !== void 0 && b >= 64 && b <= 127 || a === 169 && b === 254 || a === 172 && b !== void 0 && b >= 16 && b <= 31 || a === 192 && b === 168 || a !== void 0 && a >= 224);
|
|
77
|
+
}
|
|
78
|
+
function ipv4Octets(ip) {
|
|
79
|
+
if (isIP(ip) === 4) return ip.split(".").map(Number);
|
|
80
|
+
const groups = ipv6Groups(ip);
|
|
81
|
+
if (!groups || !groups.slice(0, 5).every((group) => group === 0) || groups[5] !== 65535) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const high = groups[6] ?? 0;
|
|
85
|
+
const low = groups[7] ?? 0;
|
|
86
|
+
return [high >> 8, high & 255, low >> 8, low & 255];
|
|
87
|
+
}
|
|
88
|
+
function ipv6Groups(ip) {
|
|
89
|
+
if (ip.includes("%") || isIP(ip) !== 6) return null;
|
|
90
|
+
let address = ip.toLowerCase();
|
|
91
|
+
const dotted = address.match(/(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
|
|
92
|
+
if (dotted) {
|
|
93
|
+
const [a = 0, b = 0, c = 0, d = 0] = dotted.slice(1).map(Number);
|
|
94
|
+
address = `${address.slice(0, dotted.index)}${(a << 8 | b).toString(16)}:${(c << 8 | d).toString(16)}`;
|
|
95
|
+
}
|
|
96
|
+
const [head, tail] = address.split("::");
|
|
97
|
+
const left = head ? head.split(":") : [];
|
|
98
|
+
const right = tail ? tail.split(":") : [];
|
|
99
|
+
const missing = address.includes("::") ? 8 - left.length - right.length : 0;
|
|
100
|
+
return [...left, ...Array(missing).fill("0"), ...right].map((group) => parseInt(group, 16));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ../lynkow-next/src/server/buyer-session.ts
|
|
104
|
+
var BUYER_COOKIE_NAMES = {
|
|
105
|
+
/** The guest cart token. */
|
|
106
|
+
cart: "__Host-lynkow-cart",
|
|
107
|
+
/** The signed-in customer's session token. */
|
|
108
|
+
customer: "__Host-lynkow-customer"
|
|
109
|
+
};
|
|
110
|
+
var THIRTY_DAYS_S = 30 * 24 * 60 * 60;
|
|
111
|
+
var CUSTOMER_MAX_AGE_S = THIRTY_DAYS_S;
|
|
112
|
+
var CART_MAX_AGE_S = THIRTY_DAYS_S;
|
|
113
|
+
var MAX_TOKEN_LENGTH = 4096;
|
|
114
|
+
var TOKEN_VALUE = /^[\x21-\x2b\x2d-\x3a\x3c-\x7e]+$/;
|
|
115
|
+
function readBuyerTokens(source) {
|
|
116
|
+
const read = isRequest(source) ? (name) => cookieFromHeader(source.headers.get("cookie"), name) : (name) => source.get(name)?.value;
|
|
117
|
+
return {
|
|
118
|
+
cartToken: usableToken(read(BUYER_COOKIE_NAMES.cart)),
|
|
119
|
+
customerToken: usableToken(read(BUYER_COOKIE_NAMES.customer))
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function createBuyerClient(config, source, options = {}) {
|
|
123
|
+
const { cartToken, customerToken } = readBuyerTokens(source);
|
|
124
|
+
const explicit = options.serverKey !== void 0 || options.clientIp !== void 0;
|
|
125
|
+
const serverKey = options.serverKey?.trim();
|
|
126
|
+
const clientIp = options.clientIp?.trim();
|
|
127
|
+
const client = createClient({
|
|
128
|
+
...baseClientConfig(config, options.locale),
|
|
129
|
+
...customerToken ? { customerToken } : {},
|
|
130
|
+
// The secret stays on this per-request client and never enters the
|
|
131
|
+
// resolved config, which reaches client bundles. Both or neither: the
|
|
132
|
+
// SDK sends the pair only when both are present.
|
|
133
|
+
...serverKey && clientIp ? { serverKey, clientIp } : {},
|
|
134
|
+
// Resolved per write and only then, so a read never calls `headers()`.
|
|
135
|
+
...!explicit && hasServerKey() ? { requestHeaders: forwardOnWrites } : {},
|
|
136
|
+
// A client-wide option, so it also covers the reads that take no request
|
|
137
|
+
// options, such as `customers.me()`.
|
|
138
|
+
fetchOptions: uncached().fetchOptions
|
|
139
|
+
});
|
|
140
|
+
if (cartToken) client.cart.setCartToken(cartToken);
|
|
141
|
+
return client;
|
|
142
|
+
}
|
|
143
|
+
var WRITE_METHODS = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
144
|
+
var FORWARDED_READS = [
|
|
145
|
+
// GET /storefront/:siteId/orders/guest, on the form-submission bucket.
|
|
146
|
+
/\/storefront\/[^/]+\/orders\/guest$/
|
|
147
|
+
];
|
|
148
|
+
function forwardOnWrites({
|
|
149
|
+
method,
|
|
150
|
+
url
|
|
151
|
+
}) {
|
|
152
|
+
return WRITE_METHODS.has(method) || method === "GET" && isForwardedRead(url) ? requestForwardingHeaders() : void 0;
|
|
153
|
+
}
|
|
154
|
+
function isForwardedRead(url) {
|
|
155
|
+
let pathname;
|
|
156
|
+
try {
|
|
157
|
+
pathname = new URL(url).pathname;
|
|
158
|
+
} catch {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return FORWARDED_READS.some((pattern) => pattern.test(pathname));
|
|
162
|
+
}
|
|
163
|
+
function setCartCookie(store, token, options = {}) {
|
|
164
|
+
writeToken(store, BUYER_COOKIE_NAMES.cart, token, CART_MAX_AGE_S, options.expiresAt);
|
|
165
|
+
}
|
|
166
|
+
function setCustomerCookie(store, token, options = {}) {
|
|
167
|
+
writeToken(store, BUYER_COOKIE_NAMES.customer, token, CUSTOMER_MAX_AGE_S, options.expiresAt);
|
|
168
|
+
}
|
|
169
|
+
function clearBuyerCookies(store) {
|
|
170
|
+
store.set(BUYER_COOKIE_NAMES.cart, "", attributes(0));
|
|
171
|
+
store.set(BUYER_COOKIE_NAMES.customer, "", attributes(0));
|
|
172
|
+
}
|
|
173
|
+
async function signOut(client, store, options = {}) {
|
|
174
|
+
try {
|
|
175
|
+
await client.customers.logout(options);
|
|
176
|
+
} finally {
|
|
177
|
+
clearBuyerCookies(store);
|
|
178
|
+
client.cart.setCartToken(null);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function writeToken(store, name, token, ceiling, expiresAt) {
|
|
182
|
+
if (!isTokenValue(token)) {
|
|
183
|
+
throw new TypeError(
|
|
184
|
+
`${name} takes 1 to ${MAX_TOKEN_LENGTH} visible ASCII characters, without "," or ";"`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
const maxAge = lifetime(ceiling, expiresAt);
|
|
188
|
+
store.set(name, maxAge > 0 ? token : "", attributes(maxAge));
|
|
189
|
+
}
|
|
190
|
+
function lifetime(ceiling, expiresAt) {
|
|
191
|
+
if (expiresAt === void 0) return ceiling;
|
|
192
|
+
const at = expiresAt instanceof Date ? expiresAt.getTime() : expiresAt;
|
|
193
|
+
if (typeof at !== "number" || Number.isNaN(at)) {
|
|
194
|
+
throw new TypeError("expiresAt is not a valid date");
|
|
195
|
+
}
|
|
196
|
+
return Math.min(ceiling, Math.max(0, Math.floor((at - Date.now()) / 1e3)));
|
|
197
|
+
}
|
|
198
|
+
function attributes(maxAge) {
|
|
199
|
+
return { httpOnly: true, secure: true, sameSite: "lax", path: "/", maxAge };
|
|
200
|
+
}
|
|
201
|
+
function isTokenValue(value) {
|
|
202
|
+
return typeof value === "string" && value.length <= MAX_TOKEN_LENGTH && TOKEN_VALUE.test(value);
|
|
203
|
+
}
|
|
204
|
+
function usableToken(value) {
|
|
205
|
+
return isTokenValue(value) ? value : null;
|
|
206
|
+
}
|
|
207
|
+
function isRequest(source) {
|
|
208
|
+
const headers = source.headers;
|
|
209
|
+
return typeof headers === "object" && headers !== null && typeof headers.get === "function";
|
|
210
|
+
}
|
|
211
|
+
function cookieFromHeader(header, name) {
|
|
212
|
+
if (!header) return void 0;
|
|
213
|
+
let value;
|
|
214
|
+
for (const pair of header.split(";")) {
|
|
215
|
+
const at = pair.indexOf("=");
|
|
216
|
+
if (at === -1 || pair.slice(0, at).trim() !== name) continue;
|
|
217
|
+
try {
|
|
218
|
+
value = decodeURIComponent(pair.slice(at + 1).trim());
|
|
219
|
+
} catch {
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return value;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// src/server/buyer.ts
|
|
226
|
+
function readBuyerTokens2(source) {
|
|
227
|
+
return readBuyerTokens(source);
|
|
228
|
+
}
|
|
229
|
+
function createBuyerClient2(config, source, options = {}) {
|
|
230
|
+
return createBuyerClient(config, source, options);
|
|
231
|
+
}
|
|
232
|
+
function setCartCookie2(cookies, token, options = {}) {
|
|
233
|
+
setCartCookie(cookies, token, options);
|
|
234
|
+
}
|
|
235
|
+
function setCustomerCookie2(cookies, token, options = {}) {
|
|
236
|
+
setCustomerCookie(cookies, token, options);
|
|
237
|
+
}
|
|
238
|
+
function clearBuyerCookies2(cookies) {
|
|
239
|
+
clearBuyerCookies(cookies);
|
|
240
|
+
}
|
|
241
|
+
async function signOut2(client, cookies, options = {}) {
|
|
242
|
+
await signOut(client, cookies, options);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ../lynkow-next/src/server/same-origin.ts
|
|
246
|
+
function assertSameOrigin(request) {
|
|
247
|
+
const origin = originHost(request.headers.get("origin"));
|
|
248
|
+
const host = requestHost(request);
|
|
249
|
+
if (origin === null || host === null || origin !== host) {
|
|
250
|
+
return Response.json({ error: "Forbidden" }, { status: 403 });
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
function originHost(value) {
|
|
255
|
+
const trimmed = value?.trim();
|
|
256
|
+
if (!trimmed || trimmed === "null") return null;
|
|
257
|
+
let url;
|
|
258
|
+
try {
|
|
259
|
+
url = new URL(trimmed);
|
|
260
|
+
} catch {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
|
|
264
|
+
return url.host.toLowerCase() || null;
|
|
265
|
+
}
|
|
266
|
+
function requestHost(request) {
|
|
267
|
+
const forwarded = request.headers.get("x-forwarded-host")?.split(",")[0]?.trim();
|
|
268
|
+
const host = forwarded || request.headers.get("host")?.trim() || urlHost(request.url);
|
|
269
|
+
return host ? host.toLowerCase() : null;
|
|
270
|
+
}
|
|
271
|
+
function urlHost(url) {
|
|
272
|
+
try {
|
|
273
|
+
return new URL(url).host;
|
|
274
|
+
} catch {
|
|
275
|
+
return "";
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// src/server/request.ts
|
|
280
|
+
function resolveBuyerForwarding2(headers) {
|
|
281
|
+
return resolveBuyerForwarding(headers);
|
|
282
|
+
}
|
|
283
|
+
function assertSameOrigin2(request) {
|
|
284
|
+
return assertSameOrigin(request);
|
|
285
|
+
}
|
|
286
|
+
function jsonLdPayload(nodes, siteDomain, rewrite) {
|
|
287
|
+
const stripped = nodes.map((node) => {
|
|
288
|
+
const copy = { ...node };
|
|
289
|
+
delete copy["@context"];
|
|
290
|
+
return copy;
|
|
291
|
+
});
|
|
292
|
+
const payload = JSON.stringify({ "@context": "https://schema.org", "@graph": stripped });
|
|
293
|
+
return rewrite(payload, siteDomain).replace(/</g, "\\u003c");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ../lynkow-next/src/server/metadata.ts
|
|
297
|
+
function ogLocale(tag) {
|
|
298
|
+
try {
|
|
299
|
+
const parsed = new Intl.Locale(tag);
|
|
300
|
+
const region = parsed.region ?? parsed.maximize().region;
|
|
301
|
+
return region ? `${parsed.language.toLowerCase()}_${region.toUpperCase()}` : tag;
|
|
302
|
+
} catch {
|
|
303
|
+
return tag;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function ogAlternateLocales(served, locales) {
|
|
307
|
+
const own = ogLocale(served);
|
|
308
|
+
const list = [...new Set(locales.map(ogLocale))].filter((code) => code !== own);
|
|
309
|
+
return list.length > 0 ? list : void 0;
|
|
310
|
+
}
|
|
311
|
+
function twitterCard(card) {
|
|
312
|
+
switch (card) {
|
|
313
|
+
case "app":
|
|
314
|
+
return { card, app: { id: {} } };
|
|
315
|
+
case "player":
|
|
316
|
+
return { card, players: [] };
|
|
317
|
+
default:
|
|
318
|
+
return { card };
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function createMetadataBuilders(paths) {
|
|
322
|
+
function apiUrl(path, locale) {
|
|
323
|
+
return paths.absoluteUrl(paths.fromApiPath(path, locale));
|
|
324
|
+
}
|
|
325
|
+
function routeUrl(locale, path) {
|
|
326
|
+
return paths.absoluteUrl(paths.localePath(locale, path));
|
|
327
|
+
}
|
|
328
|
+
function alternateUrls(alternates) {
|
|
329
|
+
const urls = {};
|
|
330
|
+
for (const alternate of alternates ?? []) {
|
|
331
|
+
const code = paths.toLocale(alternate.locale);
|
|
332
|
+
if (code) urls[code] = apiUrl(alternate.path, code);
|
|
333
|
+
}
|
|
334
|
+
return urls;
|
|
335
|
+
}
|
|
336
|
+
function hreflang(urls) {
|
|
337
|
+
if (Object.keys(urls).length === 0) return void 0;
|
|
338
|
+
const byDefault = urls[paths.defaultLocale];
|
|
339
|
+
return byDefault ? { ...urls, "x-default": byDefault } : urls;
|
|
340
|
+
}
|
|
341
|
+
function pageMetadata(page, locale, fallbackPath) {
|
|
342
|
+
const seo = page.seo;
|
|
343
|
+
const title = seo?.metaTitle || page.name || void 0;
|
|
344
|
+
const description = seo?.metaDescription || void 0;
|
|
345
|
+
const fallback = page.isFallback === true;
|
|
346
|
+
const urls = alternateUrls(page.alternates);
|
|
347
|
+
let canonical;
|
|
348
|
+
if (fallback) {
|
|
349
|
+
canonical = seo?.canonicalUrl || page.canonicalUrl || (page.canonicalPath ? apiUrl(page.canonicalPath, paths.defaultLocale) : void 0) || urls[paths.defaultLocale] || routeUrl(paths.defaultLocale, fallbackPath);
|
|
350
|
+
} else {
|
|
351
|
+
const wanted = paths.toLocale(locale);
|
|
352
|
+
const current = wanted ? page.alternates?.find((alt) => paths.toLocale(alt.locale) === wanted) : void 0;
|
|
353
|
+
canonical = seo?.canonicalUrl || (current ? apiUrl(current.path, locale) : routeUrl(locale, fallbackPath));
|
|
354
|
+
}
|
|
355
|
+
const served = fallback && page.locale ? page.locale : locale;
|
|
356
|
+
return {
|
|
357
|
+
title: seo?.metaTitle ? { absolute: seo.metaTitle } : title,
|
|
358
|
+
description,
|
|
359
|
+
keywords: seo?.keywords?.length ? seo.keywords : void 0,
|
|
360
|
+
alternates: {
|
|
361
|
+
canonical,
|
|
362
|
+
languages: fallback ? void 0 : hreflang(urls)
|
|
363
|
+
},
|
|
364
|
+
robots: seo?.noIndex ? { index: false, follow: false } : void 0,
|
|
365
|
+
openGraph: {
|
|
366
|
+
type: "website",
|
|
367
|
+
locale: ogLocale(served),
|
|
368
|
+
alternateLocale: fallback ? void 0 : ogAlternateLocales(served, Object.keys(urls)),
|
|
369
|
+
url: seo?.ogUrl || canonical,
|
|
370
|
+
siteName: seo?.ogSiteName || void 0,
|
|
371
|
+
title: seo?.ogTitle || title,
|
|
372
|
+
description: seo?.ogDescription || description,
|
|
373
|
+
images: seo?.ogImage?.url ? [{ url: seo.ogImage.url, alt: seo.ogImage.alt || void 0 }] : void 0
|
|
374
|
+
},
|
|
375
|
+
twitter: {
|
|
376
|
+
...twitterCard(seo?.twitterCard || "summary_large_image"),
|
|
377
|
+
title: seo?.twitterTitle || seo?.ogTitle || title,
|
|
378
|
+
description: seo?.twitterDescription || seo?.ogDescription || description,
|
|
379
|
+
images: seo?.twitterImage?.url ? [seo.twitterImage.url] : void 0
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
function routeMetadata(locale, path, title, description) {
|
|
384
|
+
const url = routeUrl(locale, path);
|
|
385
|
+
const multilingual = paths.locales.length > 1;
|
|
386
|
+
const languages = multilingual ? {
|
|
387
|
+
...Object.fromEntries(paths.locales.map((code) => [code, routeUrl(code, path)])),
|
|
388
|
+
"x-default": routeUrl(paths.defaultLocale, path)
|
|
389
|
+
} : void 0;
|
|
390
|
+
return {
|
|
391
|
+
title,
|
|
392
|
+
description,
|
|
393
|
+
alternates: { canonical: url, languages },
|
|
394
|
+
openGraph: {
|
|
395
|
+
type: "website",
|
|
396
|
+
locale: ogLocale(locale),
|
|
397
|
+
alternateLocale: multilingual ? ogAlternateLocales(locale, paths.locales) : void 0,
|
|
398
|
+
url,
|
|
399
|
+
title,
|
|
400
|
+
description
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
function recordMetadata(record, locale, type) {
|
|
405
|
+
const title = record.seoTitle || record.name;
|
|
406
|
+
const description = record.description || void 0;
|
|
407
|
+
const fallback = record.isFallback === true;
|
|
408
|
+
const urls = paths.locales.length > 1 ? alternateUrls(record.alternates) : {};
|
|
409
|
+
const languages = fallback ? void 0 : hreflang(urls);
|
|
410
|
+
let canonical;
|
|
411
|
+
if (fallback) {
|
|
412
|
+
const path = record.canonicalPath || record.path;
|
|
413
|
+
canonical = record.canonicalUrl || alternateUrls(record.alternates)[paths.defaultLocale] || (path ? apiUrl(path, paths.defaultLocale) : void 0);
|
|
414
|
+
} else {
|
|
415
|
+
canonical = record.canonicalUrl || (record.path ? apiUrl(record.path, locale) : void 0);
|
|
416
|
+
}
|
|
417
|
+
const image = record.ogImage ? { url: record.ogImage, alt: record.ogImageAlt } : record.image;
|
|
418
|
+
return {
|
|
419
|
+
title: record.seoTitle ? { absolute: record.seoTitle } : record.name,
|
|
420
|
+
description,
|
|
421
|
+
keywords: record.keywords || void 0,
|
|
422
|
+
alternates: canonical || languages ? { ...canonical ? { canonical } : {}, ...languages ? { languages } : {} } : void 0,
|
|
423
|
+
// Independent, unlike a page's: a record can stay out of the index while
|
|
424
|
+
// its links are still followed, or the reverse.
|
|
425
|
+
robots: record.noIndex || record.noFollow ? { index: !record.noIndex, follow: !record.noFollow } : void 0,
|
|
426
|
+
openGraph: {
|
|
427
|
+
type,
|
|
428
|
+
locale: ogLocale(locale),
|
|
429
|
+
alternateLocale: languages ? ogAlternateLocales(locale, Object.keys(urls)) : void 0,
|
|
430
|
+
url: canonical,
|
|
431
|
+
title: record.ogTitle || title,
|
|
432
|
+
description: record.ogDescription || description,
|
|
433
|
+
images: image ? [{ url: image.url, alt: image.alt || void 0 }] : void 0
|
|
434
|
+
},
|
|
435
|
+
twitter: record.twitterCard ? twitterCard(record.twitterCard) : void 0
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
function articleMetadata(content) {
|
|
439
|
+
const locale = paths.toLocale(content.locale) ?? paths.defaultLocale;
|
|
440
|
+
return recordMetadata(
|
|
441
|
+
{
|
|
442
|
+
seoTitle: content.metaTitle,
|
|
443
|
+
name: content.title,
|
|
444
|
+
description: content.metaDescription || content.excerpt,
|
|
445
|
+
keywords: content.keywords,
|
|
446
|
+
canonicalUrl: content.canonicalUrl,
|
|
447
|
+
path: content.path,
|
|
448
|
+
canonicalPath: content.canonicalPath,
|
|
449
|
+
alternates: content.alternates,
|
|
450
|
+
isFallback: content.isFallback,
|
|
451
|
+
ogTitle: content.ogTitle,
|
|
452
|
+
ogDescription: content.ogDescription,
|
|
453
|
+
ogImage: content.ogImage,
|
|
454
|
+
ogImageAlt: null,
|
|
455
|
+
image: content.featuredImage ? { url: content.featuredImage, alt: null } : null,
|
|
456
|
+
twitterCard: content.twitterCard,
|
|
457
|
+
noIndex: content.noIndex,
|
|
458
|
+
noFollow: content.noFollow
|
|
459
|
+
},
|
|
460
|
+
locale,
|
|
461
|
+
"article"
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
function categoryMetadata(category) {
|
|
465
|
+
const locale = paths.toLocale(category.locale) ?? paths.defaultLocale;
|
|
466
|
+
return recordMetadata(
|
|
467
|
+
{
|
|
468
|
+
seoTitle: null,
|
|
469
|
+
name: category.name,
|
|
470
|
+
description: category.description,
|
|
471
|
+
keywords: null,
|
|
472
|
+
canonicalUrl: category.canonicalUrl ?? null,
|
|
473
|
+
path: category.path,
|
|
474
|
+
canonicalPath: category.canonicalPath,
|
|
475
|
+
alternates: category.alternates,
|
|
476
|
+
isFallback: category.isFallback,
|
|
477
|
+
ogTitle: null,
|
|
478
|
+
ogDescription: null,
|
|
479
|
+
ogImage: null,
|
|
480
|
+
ogImageAlt: null,
|
|
481
|
+
image: category.image ? { url: category.image, alt: null } : null,
|
|
482
|
+
twitterCard: null,
|
|
483
|
+
noIndex: false,
|
|
484
|
+
noFollow: false
|
|
485
|
+
},
|
|
486
|
+
locale,
|
|
487
|
+
"website"
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
function commerceSeo(record) {
|
|
491
|
+
return {
|
|
492
|
+
seoTitle: record.seoTitle,
|
|
493
|
+
keywords: record.keywords,
|
|
494
|
+
canonicalUrl: record.canonicalUrl,
|
|
495
|
+
canonicalPath: record.canonicalPath,
|
|
496
|
+
alternates: record.alternates,
|
|
497
|
+
isFallback: record.isFallback,
|
|
498
|
+
ogTitle: record.ogTitle,
|
|
499
|
+
ogDescription: record.ogDescription,
|
|
500
|
+
ogImage: record.ogImage,
|
|
501
|
+
ogImageAlt: record.ogImageAlt,
|
|
502
|
+
twitterCard: record.twitterCard,
|
|
503
|
+
noIndex: record.noIndex,
|
|
504
|
+
noFollow: record.noFollow
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
function servedLocale(record) {
|
|
508
|
+
return paths.toLocale(record.locale) ?? paths.defaultLocale;
|
|
509
|
+
}
|
|
510
|
+
function productMetadata(product) {
|
|
511
|
+
const first = product.gallery[0];
|
|
512
|
+
return recordMetadata(
|
|
513
|
+
{
|
|
514
|
+
...commerceSeo(product),
|
|
515
|
+
name: product.title,
|
|
516
|
+
description: product.seoDescription || product.description,
|
|
517
|
+
path: product.path,
|
|
518
|
+
image: first ? { url: first.url, alt: first.alt } : null
|
|
519
|
+
},
|
|
520
|
+
servedLocale(product),
|
|
521
|
+
"website"
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
function productCollectionMetadata(collection) {
|
|
525
|
+
return recordMetadata(
|
|
526
|
+
{
|
|
527
|
+
...commerceSeo(collection),
|
|
528
|
+
name: collection.title,
|
|
529
|
+
description: collection.seoDescription || collection.description,
|
|
530
|
+
path: collection.path,
|
|
531
|
+
image: collection.image ? { url: collection.image, alt: collection.imageAlt } : null
|
|
532
|
+
},
|
|
533
|
+
servedLocale(collection),
|
|
534
|
+
"website"
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
function productCategoryMetadata(category) {
|
|
538
|
+
return recordMetadata(
|
|
539
|
+
{
|
|
540
|
+
...commerceSeo(category),
|
|
541
|
+
name: category.name,
|
|
542
|
+
description: category.seoDescription || category.description,
|
|
543
|
+
path: category.path,
|
|
544
|
+
image: category.image ? { url: category.image, alt: category.imageAlt } : null
|
|
545
|
+
},
|
|
546
|
+
servedLocale(category),
|
|
547
|
+
"website"
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
return {
|
|
551
|
+
pageMetadata,
|
|
552
|
+
routeMetadata,
|
|
553
|
+
articleMetadata,
|
|
554
|
+
categoryMetadata,
|
|
555
|
+
productMetadata,
|
|
556
|
+
productCollectionMetadata,
|
|
557
|
+
productCategoryMetadata
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// ../lynkow-next/src/server/origin.ts
|
|
562
|
+
function rewriteOrigin(body, siteDomain, siteUrl) {
|
|
563
|
+
if (!siteDomain) return body;
|
|
564
|
+
const sources = [`https://${siteDomain}`, `http://${siteDomain}`];
|
|
565
|
+
return sources.reduce(
|
|
566
|
+
(acc, source) => source === siteUrl ? acc : acc.split(source).join(siteUrl),
|
|
567
|
+
body
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
function unprefixDefaultLocale(body, siteUrl, defaultLocale) {
|
|
571
|
+
const pattern = new RegExp(
|
|
572
|
+
`${escapeRegExp(siteUrl)}/${escapeRegExp(defaultLocale.toLowerCase())}(?=[/?#"'<>\\s)\\]\\\\]|$)`,
|
|
573
|
+
"g"
|
|
574
|
+
);
|
|
575
|
+
return body.replace(
|
|
576
|
+
pattern,
|
|
577
|
+
(match, offset) => body[offset + match.length] === "/" ? siteUrl : `${siteUrl}/`
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
function createBodyRewriter(siteUrl, paths) {
|
|
581
|
+
return (body, siteDomain) => unprefixDefaultLocale(rewriteOrigin(body, siteDomain, siteUrl), siteUrl, paths.defaultLocale);
|
|
582
|
+
}
|
|
583
|
+
function escapeRegExp(value) {
|
|
584
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// src/server/head.ts
|
|
588
|
+
function text(value) {
|
|
589
|
+
return typeof value === "string" && value !== "" ? value : void 0;
|
|
590
|
+
}
|
|
591
|
+
function assign(target, fields) {
|
|
592
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
593
|
+
if (value !== void 0) target[key] = value;
|
|
594
|
+
}
|
|
595
|
+
return target;
|
|
596
|
+
}
|
|
597
|
+
function toHeadData(metadata) {
|
|
598
|
+
const title = metadata.title;
|
|
599
|
+
const absolute = typeof title === "object" && title !== null;
|
|
600
|
+
const head = assign(
|
|
601
|
+
{ titleAbsolute: absolute, alternates: [] },
|
|
602
|
+
{
|
|
603
|
+
title: absolute ? text(title.absolute) : text(title),
|
|
604
|
+
description: text(metadata.description),
|
|
605
|
+
// Joined as Next's renderer joins them, with a bare comma.
|
|
606
|
+
keywords: Array.isArray(metadata.keywords) ? text(metadata.keywords.join(",")) : text(metadata.keywords),
|
|
607
|
+
canonical: text(metadata.alternates?.canonical)
|
|
608
|
+
}
|
|
609
|
+
);
|
|
610
|
+
for (const [hreflang, href] of Object.entries(metadata.alternates?.languages ?? {})) {
|
|
611
|
+
head.alternates.push({ hreflang, href });
|
|
612
|
+
}
|
|
613
|
+
if (metadata.robots) {
|
|
614
|
+
const { index = true, follow = true } = metadata.robots;
|
|
615
|
+
head.robots = `${index ? "index" : "noindex"}, ${follow ? "follow" : "nofollow"}`;
|
|
616
|
+
}
|
|
617
|
+
const og = metadata.openGraph;
|
|
618
|
+
if (og) {
|
|
619
|
+
head.openGraph = assign(
|
|
620
|
+
{
|
|
621
|
+
alternateLocales: [...og.alternateLocale ?? []],
|
|
622
|
+
images: (og.images ?? []).map(
|
|
623
|
+
(image) => assign({ url: image.url }, { alt: text(image.alt) })
|
|
624
|
+
)
|
|
625
|
+
},
|
|
626
|
+
{
|
|
627
|
+
type: text(og.type),
|
|
628
|
+
locale: text(og.locale),
|
|
629
|
+
url: text(og.url),
|
|
630
|
+
siteName: text(og.siteName),
|
|
631
|
+
// Next fills an Open Graph title and description left unset from the page's own.
|
|
632
|
+
title: text(og.title) ?? head.title,
|
|
633
|
+
description: text(og.description) ?? head.description
|
|
634
|
+
}
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
const twitter = metadata.twitter;
|
|
638
|
+
const openGraph = head.openGraph;
|
|
639
|
+
if (twitter || openGraph) {
|
|
640
|
+
const images = twitter?.images ? twitter.images.map((url) => ({ url })) : (openGraph?.images ?? []).map((image) => ({ ...image }));
|
|
641
|
+
head.twitter = assign(
|
|
642
|
+
{
|
|
643
|
+
card: twitter?.card || (images.length > 0 ? "summary_large_image" : "summary"),
|
|
644
|
+
images
|
|
645
|
+
},
|
|
646
|
+
{
|
|
647
|
+
title: text(twitter?.title) ?? openGraph?.title ?? head.title,
|
|
648
|
+
description: text(twitter?.description) ?? openGraph?.description ?? head.description
|
|
649
|
+
}
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
return head;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// src/server/create-server.ts
|
|
656
|
+
var XML = "application/xml; charset=utf-8";
|
|
657
|
+
var TEXT = "text/plain; charset=utf-8";
|
|
658
|
+
var TEXT_CACHE_CONTROL = `public, max-age=0, s-maxage=${REVALIDATE.seo}, stale-while-revalidate=86400`;
|
|
659
|
+
function createLynkowServer({
|
|
660
|
+
config,
|
|
661
|
+
client,
|
|
662
|
+
getSiteDomain,
|
|
663
|
+
siteLocales
|
|
664
|
+
}) {
|
|
665
|
+
const paths = createLocalePaths(config, siteLocales);
|
|
666
|
+
const rewrite = createBodyRewriter(config.siteUrl, paths);
|
|
667
|
+
const builders = createMetadataBuilders(paths);
|
|
668
|
+
const live = config.locales.length > 0 ? void 0 : siteLocales;
|
|
669
|
+
const head = (build) => (...args) => toHeadData(build(...args));
|
|
670
|
+
function textRoute(read, contentType) {
|
|
671
|
+
async function readText(locale) {
|
|
672
|
+
try {
|
|
673
|
+
return await read(client(locale), cached([TAGS2.seo], REVALIDATE.seo), locale);
|
|
674
|
+
} catch (error) {
|
|
675
|
+
if (isLynkowError(error) && error.code === "NOT_FOUND") return null;
|
|
676
|
+
throw error;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return async function GET({ params }) {
|
|
680
|
+
await live?.get();
|
|
681
|
+
const locale = paths.toLocale(params["locale"]) ?? paths.defaultLocale;
|
|
682
|
+
const [domain, body] = await Promise.all([getSiteDomain(locale), readText(locale)]);
|
|
683
|
+
if (body === null) {
|
|
684
|
+
return new Response("Not found", {
|
|
685
|
+
status: 404,
|
|
686
|
+
headers: { "content-type": TEXT, "cache-control": TEXT_CACHE_CONTROL }
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
return new Response(rewrite(body, domain), {
|
|
690
|
+
headers: { "content-type": contentType, "cache-control": TEXT_CACHE_CONTROL }
|
|
691
|
+
});
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
async function jsonLd(nodes, locale) {
|
|
695
|
+
if (!nodes || nodes.length === 0) return null;
|
|
696
|
+
await live?.get();
|
|
697
|
+
return jsonLdPayload(nodes, await getSiteDomain(locale), rewrite);
|
|
698
|
+
}
|
|
699
|
+
const buildArticleHead = head(builders.articleMetadata);
|
|
700
|
+
function articleHead(content) {
|
|
701
|
+
const data = buildArticleHead(content);
|
|
702
|
+
const article = {
|
|
703
|
+
authors: content.author?.fullName ? [content.author.fullName] : [],
|
|
704
|
+
tags: (content.tags ?? []).map((tag) => tag.name)
|
|
705
|
+
};
|
|
706
|
+
if (content.publishedAt) article.publishedTime = content.publishedAt;
|
|
707
|
+
if (content.updatedAt) article.modifiedTime = content.updatedAt;
|
|
708
|
+
if (data.openGraph) data.openGraph.article = article;
|
|
709
|
+
return data;
|
|
710
|
+
}
|
|
711
|
+
return {
|
|
712
|
+
textRoute,
|
|
713
|
+
jsonLd,
|
|
714
|
+
pageHead: head(builders.pageMetadata),
|
|
715
|
+
routeHead: head(builders.routeMetadata),
|
|
716
|
+
articleHead,
|
|
717
|
+
categoryHead: head(builders.categoryMetadata),
|
|
718
|
+
productHead: head(builders.productMetadata),
|
|
719
|
+
productCollectionHead: head(builders.productCollectionMetadata),
|
|
720
|
+
productCategoryHead: head(builders.productCategoryMetadata),
|
|
721
|
+
rewriteOrigin: rewrite
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
var STORAGE = /* @__PURE__ */ Symbol.for("@lynkow/astro/invalidation");
|
|
725
|
+
var storage = globalThis[STORAGE] ??= new AsyncLocalStorage();
|
|
726
|
+
async function runInvalidation(cache, fn) {
|
|
727
|
+
const invalidation = { cache, pending: [] };
|
|
728
|
+
const result = await storage.run(invalidation, fn);
|
|
729
|
+
await Promise.all(invalidation.pending);
|
|
730
|
+
return result;
|
|
731
|
+
}
|
|
732
|
+
function revalidateTag(tag, _profile) {
|
|
733
|
+
const invalidation = storage.getStore();
|
|
734
|
+
const done = (invalidation?.cache ?? lynkowCache).invalidateTags([tag]);
|
|
735
|
+
if (invalidation) {
|
|
736
|
+
done.catch(() => {
|
|
737
|
+
});
|
|
738
|
+
invalidation.pending.push(done);
|
|
739
|
+
} else {
|
|
740
|
+
done.catch((error) => {
|
|
741
|
+
console.error("[lynkow] a cache tag could not be evicted", error);
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
return void 0;
|
|
745
|
+
}
|
|
746
|
+
var PREFIX = "sha256=";
|
|
747
|
+
var HEX_DIGEST = /^[0-9a-f]{64}$/i;
|
|
748
|
+
function verifySignature(rawBody, header, secret) {
|
|
749
|
+
if (typeof secret !== "string" || secret.length === 0) return false;
|
|
750
|
+
if (typeof rawBody !== "string") return false;
|
|
751
|
+
if (!hasSignatureShape(header)) return false;
|
|
752
|
+
const received = header.slice(PREFIX.length);
|
|
753
|
+
const expected = createHmac("sha256", secret).update(rawBody).digest();
|
|
754
|
+
if (!timingSafeEqual(Buffer.from(received, "hex"), expected)) return false;
|
|
755
|
+
return isFresh(rawBody, Date.now());
|
|
756
|
+
}
|
|
757
|
+
function hasSignatureShape(header) {
|
|
758
|
+
return typeof header === "string" && header.startsWith(PREFIX) && HEX_DIGEST.test(header.slice(PREFIX.length));
|
|
759
|
+
}
|
|
760
|
+
var FRESHNESS_WINDOW_MS = 5 * 60 * 1e3;
|
|
761
|
+
function isFresh(rawBody, now) {
|
|
762
|
+
let envelope;
|
|
763
|
+
try {
|
|
764
|
+
envelope = JSON.parse(rawBody);
|
|
765
|
+
} catch {
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
if (typeof envelope !== "object" || envelope === null) return false;
|
|
769
|
+
const timestamp = envelope.timestamp;
|
|
770
|
+
if (typeof timestamp !== "string") return false;
|
|
771
|
+
const signedAt = Date.parse(timestamp);
|
|
772
|
+
if (!Number.isFinite(signedAt)) return false;
|
|
773
|
+
return Math.abs(now - signedAt) <= FRESHNESS_WINDOW_MS;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
// ../lynkow-next/src/server/revalidation.ts
|
|
777
|
+
var MAX_BODY_BYTES = 5 * 1024 * 1024;
|
|
778
|
+
async function readBody(request) {
|
|
779
|
+
if (Number(request.headers.get("content-length")) > MAX_BODY_BYTES) return null;
|
|
780
|
+
if (!request.body) return "";
|
|
781
|
+
const reader = request.body.getReader();
|
|
782
|
+
const decoder = new TextDecoder();
|
|
783
|
+
let text2 = "";
|
|
784
|
+
let size = 0;
|
|
785
|
+
for (; ; ) {
|
|
786
|
+
const { done, value } = await reader.read();
|
|
787
|
+
if (done) return text2 + decoder.decode();
|
|
788
|
+
size += value.byteLength;
|
|
789
|
+
if (size > MAX_BODY_BYTES) {
|
|
790
|
+
await reader.cancel();
|
|
791
|
+
return null;
|
|
792
|
+
}
|
|
793
|
+
text2 += decoder.decode(value, { stream: true });
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
function tagsForResource(resource, slug, data) {
|
|
797
|
+
switch (resource) {
|
|
798
|
+
case "content":
|
|
799
|
+
return [
|
|
800
|
+
TAGS.contents,
|
|
801
|
+
TAGS.paths,
|
|
802
|
+
TAGS.seo,
|
|
803
|
+
TAGS.search,
|
|
804
|
+
...slug ? [TAGS.content(slug)] : []
|
|
805
|
+
];
|
|
806
|
+
case "category":
|
|
807
|
+
return [
|
|
808
|
+
TAGS.categories,
|
|
809
|
+
TAGS.contents,
|
|
810
|
+
TAGS.paths,
|
|
811
|
+
TAGS.seo,
|
|
812
|
+
TAGS.search,
|
|
813
|
+
...slug ? [TAGS.content(slug)] : []
|
|
814
|
+
];
|
|
815
|
+
case "tag":
|
|
816
|
+
return [TAGS.tags, TAGS.contents, TAGS.search];
|
|
817
|
+
case "site_block":
|
|
818
|
+
return [
|
|
819
|
+
TAGS.pages,
|
|
820
|
+
TAGS.paths,
|
|
821
|
+
TAGS.seo,
|
|
822
|
+
...data["type"] === "page" ? [] : [TAGS.site],
|
|
823
|
+
...slug ? [TAGS.page(slug)] : []
|
|
824
|
+
];
|
|
825
|
+
case "variable":
|
|
826
|
+
return [TAGS.pages, TAGS.site];
|
|
827
|
+
case "form":
|
|
828
|
+
return [TAGS.forms, ...slug ? [TAGS.form(slug)] : []];
|
|
829
|
+
case "review":
|
|
830
|
+
return [TAGS.reviews, TAGS.paths];
|
|
831
|
+
case "media":
|
|
832
|
+
return [TAGS.pages, TAGS.contents, TAGS.search];
|
|
833
|
+
case "redirect":
|
|
834
|
+
return [TAGS.paths];
|
|
835
|
+
case "sitemap_entry":
|
|
836
|
+
case "sitemap_rule":
|
|
837
|
+
return [TAGS.seo];
|
|
838
|
+
case "seo":
|
|
839
|
+
case "product":
|
|
840
|
+
case "product_category":
|
|
841
|
+
case "product_collection":
|
|
842
|
+
case "product_variant":
|
|
843
|
+
case "product_option":
|
|
844
|
+
return [TAGS.seo, TAGS.paths, TAGS.search];
|
|
845
|
+
case "site":
|
|
846
|
+
return [TAGS.site, TAGS.seo, TAGS.consent];
|
|
847
|
+
case "search_profile":
|
|
848
|
+
case "search_config":
|
|
849
|
+
return [TAGS.search];
|
|
850
|
+
default:
|
|
851
|
+
return [];
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
function onlyConsentChanged(data) {
|
|
855
|
+
if (typeof data !== "object" || data === null) return false;
|
|
856
|
+
const fields = data["fields"];
|
|
857
|
+
return Array.isArray(fields) && fields.length > 0 && fields.every((field) => field === "cookie_consent");
|
|
858
|
+
}
|
|
859
|
+
function linksNoProduct(event, data) {
|
|
860
|
+
if (typeof data !== "object" || data === null) return false;
|
|
861
|
+
const payload = data;
|
|
862
|
+
switch (event) {
|
|
863
|
+
case "review.created":
|
|
864
|
+
case "review.deleted":
|
|
865
|
+
return payload["productId"] === null;
|
|
866
|
+
case "review.updated":
|
|
867
|
+
return payload["productId"] === null && payload["previousProductId"] === null;
|
|
868
|
+
case "review.bulk_updated":
|
|
869
|
+
case "review.bulk_deleted": {
|
|
870
|
+
const productIds = payload["productIds"];
|
|
871
|
+
return Array.isArray(productIds) && productIds.length === 0;
|
|
872
|
+
}
|
|
873
|
+
default:
|
|
874
|
+
return false;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
var BATCH_SUFFIX = ".batch";
|
|
878
|
+
function tagsForEvent(envelope) {
|
|
879
|
+
const batched = envelope.event.endsWith(BATCH_SUFFIX);
|
|
880
|
+
const event = batched ? envelope.event.slice(0, -BATCH_SUFFIX.length) : envelope.event;
|
|
881
|
+
if (event === "form.submitted") return [];
|
|
882
|
+
if (batched) return tagsForBatch(event, asRecord(envelope.data));
|
|
883
|
+
if (envelope.event === "review.created") {
|
|
884
|
+
const status = envelope.data?.["status"];
|
|
885
|
+
if (status === "pending" || status === "rejected") return [TAGS.reviews];
|
|
886
|
+
}
|
|
887
|
+
if (linksNoProduct(envelope.event, envelope.data)) return [TAGS.reviews];
|
|
888
|
+
if (envelope.event === "site.updated" && onlyConsentChanged(envelope.data)) {
|
|
889
|
+
return [TAGS.consent];
|
|
890
|
+
}
|
|
891
|
+
const data = asRecord(envelope.data);
|
|
892
|
+
const slug = data["slug"];
|
|
893
|
+
return [
|
|
894
|
+
...new Set(tagsForResource(resourceOf(event), typeof slug === "string" ? slug : null, data))
|
|
895
|
+
];
|
|
896
|
+
}
|
|
897
|
+
function asRecord(value) {
|
|
898
|
+
return typeof value === "object" && value !== null ? value : {};
|
|
899
|
+
}
|
|
900
|
+
function resourceOf(event) {
|
|
901
|
+
return event.split(".")[0] ?? "";
|
|
902
|
+
}
|
|
903
|
+
function tagsForBatch(event, data) {
|
|
904
|
+
const resource = resourceOf(event);
|
|
905
|
+
const items = Array.isArray(data["items"]) ? data["items"] : [];
|
|
906
|
+
if (event === "review.created" && data["truncated"] === false && items.length > 0 && items.every((item) => {
|
|
907
|
+
const status = asRecord(item)["status"];
|
|
908
|
+
return status === "pending" || status === "rejected";
|
|
909
|
+
})) {
|
|
910
|
+
return [TAGS.reviews];
|
|
911
|
+
}
|
|
912
|
+
const tags = new Set(tagsForResource(resource, null, {}));
|
|
913
|
+
for (const item of items) {
|
|
914
|
+
const slug = asRecord(item)["slug"];
|
|
915
|
+
if (typeof slug !== "string" || slug === "") continue;
|
|
916
|
+
for (const tag of tagsForResource(resource, slug, {})) tags.add(tag);
|
|
917
|
+
}
|
|
918
|
+
return [...tags];
|
|
919
|
+
}
|
|
920
|
+
function createRevalidateHandler({ secret, siteLocales }) {
|
|
921
|
+
return async function POST(request) {
|
|
922
|
+
if (!secret) {
|
|
923
|
+
return Response.json({ error: "Revalidation webhook is not configured" }, { status: 503 });
|
|
924
|
+
}
|
|
925
|
+
const signature = request.headers.get("x-webhook-signature");
|
|
926
|
+
if (!hasSignatureShape(signature)) {
|
|
927
|
+
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
928
|
+
}
|
|
929
|
+
const rawBody = await readBody(request);
|
|
930
|
+
if (rawBody === null) {
|
|
931
|
+
return Response.json({ error: "Body too large" }, { status: 413 });
|
|
932
|
+
}
|
|
933
|
+
if (!verifySignature(rawBody, signature, secret)) {
|
|
934
|
+
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
935
|
+
}
|
|
936
|
+
let envelope;
|
|
937
|
+
try {
|
|
938
|
+
envelope = JSON.parse(rawBody);
|
|
939
|
+
} catch {
|
|
940
|
+
return Response.json({ error: "Body is not JSON" }, { status: 400 });
|
|
941
|
+
}
|
|
942
|
+
if (typeof envelope?.event !== "string") {
|
|
943
|
+
return Response.json({ error: "Body carries no event name" }, { status: 400 });
|
|
944
|
+
}
|
|
945
|
+
const siteChanged = envelope.event === `site.updated${BATCH_SUFFIX}` || envelope.event === "site.updated" && !onlyConsentChanged(envelope.data);
|
|
946
|
+
if (siteChanged && siteLocales) {
|
|
947
|
+
try {
|
|
948
|
+
siteLocales.invalidate();
|
|
949
|
+
} catch (error) {
|
|
950
|
+
console.error("[lynkow] the site languages could not be marked stale", error);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
const tags = tagsForEvent(envelope);
|
|
954
|
+
for (const tag of tags) {
|
|
955
|
+
revalidateTag(tag);
|
|
956
|
+
}
|
|
957
|
+
return Response.json({ event: envelope.event, revalidated: tags });
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
// src/server/revalidate.ts
|
|
962
|
+
function createRevalidateEndpoint({
|
|
963
|
+
secret,
|
|
964
|
+
siteLocales,
|
|
965
|
+
cache = lynkowCache
|
|
966
|
+
}) {
|
|
967
|
+
const handler = createRevalidateHandler(siteLocales ? { secret, siteLocales } : { secret });
|
|
968
|
+
return async function POST({ request }) {
|
|
969
|
+
let handled = false;
|
|
970
|
+
try {
|
|
971
|
+
return await runInvalidation(cache, async () => {
|
|
972
|
+
const response = await handler(request);
|
|
973
|
+
handled = true;
|
|
974
|
+
return response;
|
|
975
|
+
});
|
|
976
|
+
} catch (error) {
|
|
977
|
+
if (!handled) {
|
|
978
|
+
console.error("[lynkow] the revalidation webhook failed", error);
|
|
979
|
+
return Response.json({ error: "Revalidation failed" }, { status: 500 });
|
|
980
|
+
}
|
|
981
|
+
console.error("[lynkow] the revalidation webhook could not evict the cache", error);
|
|
982
|
+
return Response.json({ error: "Cache eviction failed" }, { status: 500 });
|
|
983
|
+
}
|
|
984
|
+
};
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
export { BUYER_COOKIE_NAMES, TEXT, XML, assertSameOrigin2 as assertSameOrigin, clearBuyerCookies2 as clearBuyerCookies, createBuyerClient2 as createBuyerClient, createLynkowServer, createRevalidateEndpoint, readBuyerTokens2 as readBuyerTokens, resolveBuyerForwarding2 as resolveBuyerForwarding, setCartCookie2 as setCartCookie, setCustomerCookie2 as setCustomerCookie, signOut2 as signOut, tagsForEvent, verifySignature };
|
|
988
|
+
//# sourceMappingURL=index.js.map
|
|
989
|
+
//# sourceMappingURL=index.js.map
|