@shop-on/theme-server 0.7.0 → 0.8.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/dist/analytics-proxy.js +3 -1
- package/dist/graphql.js +21 -14
- package/package.json +3 -3
package/dist/analytics-proxy.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { requestTraceHeaders } from "./trace.js";
|
|
2
2
|
import { getStorefrontToken, storefrontApiUrl } from "./graphql.js";
|
|
3
|
+
import { shardBuyerHeaders } from "@shop-on/customer-runtime/relay";
|
|
3
4
|
import { isIP } from "node:net";
|
|
4
5
|
//#region src/analytics-proxy.ts
|
|
5
6
|
const PATHS = {
|
|
@@ -10,7 +11,8 @@ async function proxyAnalytics({ endpoint, body, visitToken, visitorToken, userAg
|
|
|
10
11
|
const headers = {
|
|
11
12
|
Authorization: `Bearer ${await getStorefrontToken()}`,
|
|
12
13
|
"Content-Type": "application/json",
|
|
13
|
-
...requestTraceHeaders()
|
|
14
|
+
...requestTraceHeaders(),
|
|
15
|
+
...shardBuyerHeaders()
|
|
14
16
|
};
|
|
15
17
|
if (visitToken) headers["Ahoy-Visit"] = visitToken;
|
|
16
18
|
if (visitorToken) headers["Ahoy-Visitor"] = visitorToken;
|
package/dist/graphql.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createLease } from "./lease.js";
|
|
2
2
|
import { requestSlot } from "./request-scope.js";
|
|
3
3
|
import { requestTraceHeaders } from "./trace.js";
|
|
4
|
+
import { shardBuyerHeaders } from "@shop-on/customer-runtime/relay";
|
|
4
5
|
import { initGraphQLTada } from "gql.tada";
|
|
5
6
|
import { Kind, print } from "graphql";
|
|
6
7
|
import { ClientError, GraphQLClient } from "graphql-request";
|
|
@@ -147,18 +148,24 @@ function extraHeaders(options) {
|
|
|
147
148
|
if (options?.acceptLanguage) extra["Accept-Language"] = options.acceptLanguage;
|
|
148
149
|
return extra;
|
|
149
150
|
}
|
|
150
|
-
function
|
|
151
|
+
function requestScopedHeaders() {
|
|
152
|
+
return {
|
|
153
|
+
...requestTraceHeaders(),
|
|
154
|
+
...shardBuyerHeaders()
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function bearerHeaders(token, options, scoped) {
|
|
151
158
|
return {
|
|
152
159
|
Authorization: `Bearer ${token}`,
|
|
153
160
|
...extraHeaders(options),
|
|
154
|
-
...
|
|
161
|
+
...scoped
|
|
155
162
|
};
|
|
156
163
|
}
|
|
157
164
|
function invalidateToken(used) {
|
|
158
165
|
if (cached?.accessToken === used) cached = null;
|
|
159
166
|
}
|
|
160
|
-
async function executeSingle(document, variables, options,
|
|
161
|
-
const send = async (token) => await buildClient(bearerHeaders(token, options,
|
|
167
|
+
async function executeSingle(document, variables, options, scoped) {
|
|
168
|
+
const send = async (token) => await buildClient(bearerHeaders(token, options, scoped)).request({
|
|
162
169
|
document,
|
|
163
170
|
variables,
|
|
164
171
|
signal: AbortSignal.timeout(GRAPHQL_TIMEOUT_MS)
|
|
@@ -186,12 +193,12 @@ function enqueueBatched(queues, query, variables, options) {
|
|
|
186
193
|
let queue = queues.get(key);
|
|
187
194
|
if (!queue) {
|
|
188
195
|
const scheduled = [];
|
|
189
|
-
const
|
|
196
|
+
const scoped = requestScopedHeaders();
|
|
190
197
|
queue = scheduled;
|
|
191
198
|
queues.set(key, scheduled);
|
|
192
199
|
setTimeout(() => {
|
|
193
200
|
queues.delete(key);
|
|
194
|
-
flushBatch(scheduled, options,
|
|
201
|
+
flushBatch(scheduled, options, scoped);
|
|
195
202
|
}, 0);
|
|
196
203
|
}
|
|
197
204
|
queue.push({
|
|
@@ -202,15 +209,15 @@ function enqueueBatched(queues, query, variables, options) {
|
|
|
202
209
|
});
|
|
203
210
|
});
|
|
204
211
|
}
|
|
205
|
-
async function flushBatch(entries, options,
|
|
212
|
+
async function flushBatch(entries, options, scoped) {
|
|
206
213
|
const chunks = [];
|
|
207
214
|
for (let i = 0; i < entries.length; i += BATCH_MAX_QUERIES) chunks.push(entries.slice(i, i + BATCH_MAX_QUERIES));
|
|
208
215
|
await Promise.all(chunks.map(async (chunk) => {
|
|
209
216
|
try {
|
|
210
217
|
if (chunk.length === 1) {
|
|
211
218
|
const only = chunk[0];
|
|
212
|
-
only.resolve(await executeSingle(only.query, only.variables, options,
|
|
213
|
-
} else await executeBatchChunk(chunk, options,
|
|
219
|
+
only.resolve(await executeSingle(only.query, only.variables, options, scoped));
|
|
220
|
+
} else await executeBatchChunk(chunk, options, scoped);
|
|
214
221
|
} catch (err) {
|
|
215
222
|
for (const entry of chunk) entry.reject(err);
|
|
216
223
|
}
|
|
@@ -231,7 +238,7 @@ async function postWithAuthRetry(buildInit) {
|
|
|
231
238
|
invalidateToken(token);
|
|
232
239
|
return await post(await getToken());
|
|
233
240
|
}
|
|
234
|
-
async function executeBatchChunk(chunk, options,
|
|
241
|
+
async function executeBatchChunk(chunk, options, scoped) {
|
|
235
242
|
const body = JSON.stringify(chunk.map(({ query, variables }) => ({
|
|
236
243
|
query,
|
|
237
244
|
variables
|
|
@@ -239,7 +246,7 @@ async function executeBatchChunk(chunk, options, trace) {
|
|
|
239
246
|
const res = await postWithAuthRetry((token) => ({
|
|
240
247
|
headers: {
|
|
241
248
|
"Content-Type": "application/json",
|
|
242
|
-
...bearerHeaders(token, options,
|
|
249
|
+
...bearerHeaders(token, options, scoped)
|
|
243
250
|
},
|
|
244
251
|
body,
|
|
245
252
|
signal: AbortSignal.timeout(GRAPHQL_TIMEOUT_MS)
|
|
@@ -275,7 +282,7 @@ async function executeBatchChunk(chunk, options, trace) {
|
|
|
275
282
|
async function request(document, variables, options) {
|
|
276
283
|
assertServerOnly();
|
|
277
284
|
const queues = isBatchableQuery(document) ? requestSlot(BATCH_QUEUES, () => /* @__PURE__ */ new Map()) : void 0;
|
|
278
|
-
if (!queues) return await executeSingle(document, variables, options,
|
|
285
|
+
if (!queues) return await executeSingle(document, variables, options, requestScopedHeaders());
|
|
279
286
|
return await enqueueBatched(queues, print(document), variables, options);
|
|
280
287
|
}
|
|
281
288
|
/**
|
|
@@ -294,7 +301,7 @@ async function request(document, variables, options) {
|
|
|
294
301
|
*/
|
|
295
302
|
async function requestUpload(document, variables, files, filesPath, options) {
|
|
296
303
|
assertServerOnly();
|
|
297
|
-
const
|
|
304
|
+
const scoped = requestScopedHeaders();
|
|
298
305
|
const query = print(document);
|
|
299
306
|
const map = {};
|
|
300
307
|
files.forEach((_, i) => {
|
|
@@ -315,7 +322,7 @@ async function requestUpload(document, variables, files, filesPath, options) {
|
|
|
315
322
|
return form;
|
|
316
323
|
};
|
|
317
324
|
const res = await postWithAuthRetry((token) => ({
|
|
318
|
-
headers: bearerHeaders(token, options,
|
|
325
|
+
headers: bearerHeaders(token, options, scoped),
|
|
319
326
|
body: buildBody()
|
|
320
327
|
}));
|
|
321
328
|
if (!res.ok) throw batchClientError({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shop-on/theme-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"license": "SEE LICENSE IN dist/LICENSE.md",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -60,8 +60,8 @@
|
|
|
60
60
|
"graphql": "17.0.2",
|
|
61
61
|
"graphql-request": "7.4.0",
|
|
62
62
|
"liquidjs": "10.30.0",
|
|
63
|
-
"@shop-on/
|
|
64
|
-
"@shop-on/
|
|
63
|
+
"@shop-on/customer-runtime": "0.8.0",
|
|
64
|
+
"@shop-on/theme-runtime": "0.8.0"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@tanstack/react-start": "1.169.3",
|