@usethrottle/builder 0.7.0 → 0.7.1

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.
@@ -110,6 +110,21 @@ function currentDocument(answer, where) {
110
110
  return upgraded.doc;
111
111
  }
112
112
 
113
+ // src/media.ts
114
+ var MEDIA_HEADER = "x-builder-media";
115
+ function mediaHeaders(mediaBase) {
116
+ return mediaBase === void 0 ? {} : { [MEDIA_HEADER]: "direct" };
117
+ }
118
+ function mediaBaseOf(base, media) {
119
+ if (media === "proxy") return void 0;
120
+ try {
121
+ const url = new URL(base);
122
+ return url.protocol === "https:" ? url.origin : void 0;
123
+ } catch {
124
+ return void 0;
125
+ }
126
+ }
127
+
113
128
  // src/release.ts
114
129
  var DEFAULT_BASE_URL = "https://builder.usethrottle.dev";
115
130
  var DEFAULT_BUNDLE_TIMEOUT_MS = 6e4;
@@ -163,8 +178,8 @@ function readBundle(body) {
163
178
  } : {}
164
179
  };
165
180
  }
166
- async function fetchBundle({ base, key, ref, timeoutMs, doFetch }) {
167
- const headers = { accept: "application/json", [SCHEMA_MIN_HEADER]: BUILDER_SCHEMA_MIN };
181
+ async function fetchBundle({ base, key, ref, timeoutMs, doFetch, media }) {
182
+ const headers = { accept: "application/json", [SCHEMA_MIN_HEADER]: BUILDER_SCHEMA_MIN, ...mediaHeaders(mediaBaseOf(base, media)) };
168
183
  if (key.startsWith("tb_sk_")) headers.authorization = `Bearer ${key}`;
169
184
  else headers["X-Builder-Key"] = key;
170
185
  const controller = new AbortController();
@@ -219,7 +234,7 @@ var stored = () => {
219
234
  };
220
235
  function loadOnce(load) {
221
236
  const memo = stored();
222
- const id = `${load.base}|${load.key}|${load.ref}`;
237
+ const id = `${load.base}|${load.key}|${load.ref}|${load.media ?? "direct"}`;
223
238
  let held = memo.get(id);
224
239
  if (held === void 0) {
225
240
  held = fetchBundle(load);
@@ -309,10 +324,11 @@ function createBuildClient(options) {
309
324
  const clock = options.now ?? Date.now;
310
325
  const getReleaseBundle = async ({ release } = {}) => {
311
326
  if (options.releaseBundle !== void 0) return options.releaseBundle;
312
- return loadOnce({ base, key: builderKey, ref: releaseRef(release, options.release), timeoutMs, doFetch: options.fetch });
327
+ return loadOnce({ base, key: builderKey, ref: releaseRef(release, options.release), timeoutMs, doFetch: options.fetch, media: options.media });
313
328
  };
314
329
  return {
315
330
  mode: "build",
331
+ mediaBase: mediaBaseOf(base, options.media),
316
332
  getReleaseBundle,
317
333
  site: async ({ locale } = {}) => siteFor(await getReleaseBundle(), locale, clock()),
318
334
  pages: {
@@ -565,7 +581,8 @@ function createLiveClient(options) {
565
581
  const headers = {
566
582
  accept: "application/json",
567
583
  "X-Builder-Key": builderKey,
568
- [SCHEMA_MIN_HEADER]: BUILDER_SCHEMA_MIN
584
+ [SCHEMA_MIN_HEADER]: BUILDER_SCHEMA_MIN,
585
+ ...mediaHeaders(mediaBaseOf(base, options.media))
569
586
  };
570
587
  if (previewHeader !== void 0) headers["X-Builder-Preview"] = previewHeader;
571
588
  const url = `${base}${path}${queryString(read2.query ?? {})}`;
@@ -599,6 +616,7 @@ function createLiveClient(options) {
599
616
  }
600
617
  }
601
618
  return {
619
+ mediaBase: mediaBaseOf(base, options.media),
602
620
  site: ({ locale } = {}) => read("/v1/site", { query: { locale }, tags: [BUILDER_TAGS.all, BUILDER_TAGS.site] }, readSite),
603
621
  pages: {
604
622
  list: () => read(
@@ -110,6 +110,21 @@ function currentDocument(answer, where) {
110
110
  return upgraded.doc;
111
111
  }
112
112
 
113
+ // src/media.ts
114
+ var MEDIA_HEADER = "x-builder-media";
115
+ function mediaHeaders(mediaBase) {
116
+ return mediaBase === void 0 ? {} : { [MEDIA_HEADER]: "direct" };
117
+ }
118
+ function mediaBaseOf(base, media) {
119
+ if (media === "proxy") return void 0;
120
+ try {
121
+ const url = new URL(base);
122
+ return url.protocol === "https:" ? url.origin : void 0;
123
+ } catch (e) {
124
+ return void 0;
125
+ }
126
+ }
127
+
113
128
  // src/release.ts
114
129
  var DEFAULT_BASE_URL = "https://builder.usethrottle.dev";
115
130
  var DEFAULT_BUNDLE_TIMEOUT_MS = 6e4;
@@ -163,8 +178,8 @@ function readBundle(body) {
163
178
  } : {}
164
179
  };
165
180
  }
166
- async function fetchBundle({ base, key, ref, timeoutMs, doFetch }) {
167
- const headers = { accept: "application/json", [SCHEMA_MIN_HEADER]: BUILDER_SCHEMA_MIN };
181
+ async function fetchBundle({ base, key, ref, timeoutMs, doFetch, media }) {
182
+ const headers = { accept: "application/json", [SCHEMA_MIN_HEADER]: BUILDER_SCHEMA_MIN, ...mediaHeaders(mediaBaseOf(base, media)) };
168
183
  if (key.startsWith("tb_sk_")) headers.authorization = `Bearer ${key}`;
169
184
  else headers["X-Builder-Key"] = key;
170
185
  const controller = new AbortController();
@@ -219,7 +234,7 @@ var stored = () => {
219
234
  };
220
235
  function loadOnce(load) {
221
236
  const memo = stored();
222
- const id = `${load.base}|${load.key}|${load.ref}`;
237
+ const id = `${load.base}|${load.key}|${load.ref}|${_nullishCoalesce(load.media, () => ( "direct"))}`;
223
238
  let held = memo.get(id);
224
239
  if (held === void 0) {
225
240
  held = fetchBundle(load);
@@ -309,10 +324,11 @@ function createBuildClient(options) {
309
324
  const clock = _nullishCoalesce(options.now, () => ( Date.now));
310
325
  const getReleaseBundle = async ({ release } = {}) => {
311
326
  if (options.releaseBundle !== void 0) return options.releaseBundle;
312
- return loadOnce({ base, key: builderKey, ref: releaseRef(release, options.release), timeoutMs, doFetch: options.fetch });
327
+ return loadOnce({ base, key: builderKey, ref: releaseRef(release, options.release), timeoutMs, doFetch: options.fetch, media: options.media });
313
328
  };
314
329
  return {
315
330
  mode: "build",
331
+ mediaBase: mediaBaseOf(base, options.media),
316
332
  getReleaseBundle,
317
333
  site: async ({ locale } = {}) => siteFor(await getReleaseBundle(), locale, clock()),
318
334
  pages: {
@@ -565,7 +581,8 @@ function createLiveClient(options) {
565
581
  const headers = {
566
582
  accept: "application/json",
567
583
  "X-Builder-Key": builderKey,
568
- [SCHEMA_MIN_HEADER]: BUILDER_SCHEMA_MIN
584
+ [SCHEMA_MIN_HEADER]: BUILDER_SCHEMA_MIN,
585
+ ...mediaHeaders(mediaBaseOf(base, options.media))
569
586
  };
570
587
  if (previewHeader !== void 0) headers["X-Builder-Preview"] = previewHeader;
571
588
  const url = `${base}${path}${queryString(_nullishCoalesce(read2.query, () => ( {})))}`;
@@ -599,6 +616,7 @@ function createLiveClient(options) {
599
616
  }
600
617
  }
601
618
  return {
619
+ mediaBase: mediaBaseOf(base, options.media),
602
620
  site: ({ locale } = {}) => read("/v1/site", { query: { locale }, tags: [BUILDER_TAGS.all, BUILDER_TAGS.site] }, readSite),
603
621
  pages: {
604
622
  list: () => read(
@@ -728,7 +746,7 @@ async function verifyBuilderNotice(input) {
728
746
  let matched = false;
729
747
  for (const digest of digests) matched = constantTimeEqual(expected, fromHex(digest)) || matched;
730
748
  return matched;
731
- } catch (e) {
749
+ } catch (e2) {
732
750
  return false;
733
751
  }
734
752
  }
package/dist/index.cjs CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- var _chunkKNA3T4M6cjs = require('./chunk-KNA3T4M6.cjs');
17
+ var _chunkYLRXAP4Jcjs = require('./chunk-YLRXAP4J.cjs');
18
18
 
19
19
 
20
20
 
@@ -31,4 +31,4 @@ var _chunkKNA3T4M6cjs = require('./chunk-KNA3T4M6.cjs');
31
31
 
32
32
 
33
33
 
34
- exports.BUILDER_SCHEMA_MIN = _chunkKNA3T4M6cjs.BUILDER_SCHEMA_MIN; exports.BUILDER_TAGS = _chunkKNA3T4M6cjs.BUILDER_TAGS; exports.BuilderNotFoundError = _chunkKNA3T4M6cjs.BuilderNotFoundError; exports.BuilderReleaseNotFoundError = _chunkKNA3T4M6cjs.BuilderReleaseNotFoundError; exports.BuilderUnavailableError = _chunkKNA3T4M6cjs.BuilderUnavailableError; exports.BuilderValidationError = _chunkKNA3T4M6cjs.BuilderValidationError; exports.DEFAULT_BASE_URL = _chunkKNA3T4M6cjs.DEFAULT_BASE_URL; exports.DEFAULT_TIMEOUT_MS = _chunkKNA3T4M6cjs.DEFAULT_TIMEOUT_MS; exports.PreviewExpiredError = _chunkKNA3T4M6cjs.PreviewExpiredError; exports.SUPPORTED_BUNDLE_VERSION = _chunkKNA3T4M6cjs.SUPPORTED_BUNDLE_VERSION; exports.UNTIL_CHANGED_REVALIDATE_S = _chunkKNA3T4M6cjs.UNTIL_CHANGED_REVALIDATE_S; exports.createBuilderClient = _chunkKNA3T4M6cjs.createBuilderClient; exports.scopesToTags = _chunkKNA3T4M6cjs.scopesToTags; exports.templateTag = _chunkKNA3T4M6cjs.templateTag; exports.verifyBuilderNotice = _chunkKNA3T4M6cjs.verifyBuilderNotice;
34
+ exports.BUILDER_SCHEMA_MIN = _chunkYLRXAP4Jcjs.BUILDER_SCHEMA_MIN; exports.BUILDER_TAGS = _chunkYLRXAP4Jcjs.BUILDER_TAGS; exports.BuilderNotFoundError = _chunkYLRXAP4Jcjs.BuilderNotFoundError; exports.BuilderReleaseNotFoundError = _chunkYLRXAP4Jcjs.BuilderReleaseNotFoundError; exports.BuilderUnavailableError = _chunkYLRXAP4Jcjs.BuilderUnavailableError; exports.BuilderValidationError = _chunkYLRXAP4Jcjs.BuilderValidationError; exports.DEFAULT_BASE_URL = _chunkYLRXAP4Jcjs.DEFAULT_BASE_URL; exports.DEFAULT_TIMEOUT_MS = _chunkYLRXAP4Jcjs.DEFAULT_TIMEOUT_MS; exports.PreviewExpiredError = _chunkYLRXAP4Jcjs.PreviewExpiredError; exports.SUPPORTED_BUNDLE_VERSION = _chunkYLRXAP4Jcjs.SUPPORTED_BUNDLE_VERSION; exports.UNTIL_CHANGED_REVALIDATE_S = _chunkYLRXAP4Jcjs.UNTIL_CHANGED_REVALIDATE_S; exports.createBuilderClient = _chunkYLRXAP4Jcjs.createBuilderClient; exports.scopesToTags = _chunkYLRXAP4Jcjs.scopesToTags; exports.templateTag = _chunkYLRXAP4Jcjs.templateTag; exports.verifyBuilderNotice = _chunkYLRXAP4Jcjs.verifyBuilderNotice;
package/dist/index.d.cts CHANGED
@@ -128,6 +128,11 @@ type BuilderClientOptions = {
128
128
  /** Defaults to `https://builder.usethrottle.dev`. */
129
129
  baseUrl?: string;
130
130
  fetch?: typeof fetch;
131
+ /**
132
+ * `direct` (default) sends `X-Builder-Media: direct`, so images come as cacheable URLs on the Builder's own
133
+ * origin. `proxy` sends nothing and gets the API's redirecting asset URLs.
134
+ */
135
+ media?: 'direct' | 'proxy';
131
136
  /** Per read, body included. Default 3000. */
132
137
  timeoutMs?: number;
133
138
  /**
@@ -144,6 +149,11 @@ type LocaleOptions = {
144
149
  type BuilderClient = {
145
150
  /** `build` for a client made with `mode: 'build'`: it reads a frozen release and never the live API. */
146
151
  readonly mode?: 'live' | 'build';
152
+ /**
153
+ * The origin direct media URLs are on (`https://builder.usethrottle.dev`), when this client asks for them.
154
+ * The renderer accepts images there, for `/v1/media/` paths only.
155
+ */
156
+ readonly mediaBase?: string;
147
157
  site(options?: LocaleOptions): Promise<BuilderSite>;
148
158
  pages: {
149
159
  list(): Promise<BuilderPageSummary[]>;
@@ -249,6 +259,8 @@ type BuildClientCommon = {
249
259
  /** Defaults to `https://builder.usethrottle.dev`. */
250
260
  baseUrl?: string;
251
261
  fetch?: typeof fetch;
262
+ /** As for `createBuilderClient({ media })`: `direct` (default) asks for direct, cacheable media URLs. */
263
+ media?: 'direct' | 'proxy';
252
264
  /** The bundle read, body included. Default 60000 (a bundle is the whole site). */
253
265
  timeoutMs?: number;
254
266
  /**
package/dist/index.d.ts CHANGED
@@ -128,6 +128,11 @@ type BuilderClientOptions = {
128
128
  /** Defaults to `https://builder.usethrottle.dev`. */
129
129
  baseUrl?: string;
130
130
  fetch?: typeof fetch;
131
+ /**
132
+ * `direct` (default) sends `X-Builder-Media: direct`, so images come as cacheable URLs on the Builder's own
133
+ * origin. `proxy` sends nothing and gets the API's redirecting asset URLs.
134
+ */
135
+ media?: 'direct' | 'proxy';
131
136
  /** Per read, body included. Default 3000. */
132
137
  timeoutMs?: number;
133
138
  /**
@@ -144,6 +149,11 @@ type LocaleOptions = {
144
149
  type BuilderClient = {
145
150
  /** `build` for a client made with `mode: 'build'`: it reads a frozen release and never the live API. */
146
151
  readonly mode?: 'live' | 'build';
152
+ /**
153
+ * The origin direct media URLs are on (`https://builder.usethrottle.dev`), when this client asks for them.
154
+ * The renderer accepts images there, for `/v1/media/` paths only.
155
+ */
156
+ readonly mediaBase?: string;
147
157
  site(options?: LocaleOptions): Promise<BuilderSite>;
148
158
  pages: {
149
159
  list(): Promise<BuilderPageSummary[]>;
@@ -249,6 +259,8 @@ type BuildClientCommon = {
249
259
  /** Defaults to `https://builder.usethrottle.dev`. */
250
260
  baseUrl?: string;
251
261
  fetch?: typeof fetch;
262
+ /** As for `createBuilderClient({ media })`: `direct` (default) asks for direct, cacheable media URLs. */
263
+ media?: 'direct' | 'proxy';
252
264
  /** The bundle read, body included. Default 60000 (a bundle is the whole site). */
253
265
  timeoutMs?: number;
254
266
  /**
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  scopesToTags,
15
15
  templateTag,
16
16
  verifyBuilderNotice
17
- } from "./chunk-3PRNCLZO.js";
17
+ } from "./chunk-SRTHMABZ.js";
18
18
  export {
19
19
  BUILDER_SCHEMA_MIN,
20
20
  BUILDER_TAGS,
package/dist/next.cjs CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- var _chunkKNA3T4M6cjs = require('./chunk-KNA3T4M6.cjs');
9
+ var _chunkYLRXAP4Jcjs = require('./chunk-YLRXAP4J.cjs');
10
10
 
11
11
  // src/next/index.ts
12
12
  require('server-only');
@@ -49,7 +49,7 @@ function parseNotice(rawBody) {
49
49
  if (typeof value !== "object" || value === null) return null;
50
50
  const { type, generation, scopes } = value;
51
51
  if (type !== "builder.changed" || typeof generation !== "number" || !Number.isSafeInteger(generation) || generation < 0 || !Array.isArray(scopes) || scopes.length === 0 || scopes.length > MAX_SCOPES || !scopes.every(
52
- (scope) => typeof scope === "string" && scope.length > 0 && scope.length <= _chunkKNA3T4M6cjs.TAG_MAX_CHARS
52
+ (scope) => typeof scope === "string" && scope.length > 0 && scope.length <= _chunkYLRXAP4Jcjs.TAG_MAX_CHARS
53
53
  ))
54
54
  return null;
55
55
  return { generation, scopes };
@@ -74,12 +74,12 @@ function createRevalidateHandler(options) {
74
74
  const now = Math.floor(Date.now() / 1e3);
75
75
  let verified = false;
76
76
  for (const secret of secrets)
77
- verified = await _chunkKNA3T4M6cjs.verifyBuilderNotice.call(void 0, { rawBody, header, secret, now }) || verified;
77
+ verified = await _chunkYLRXAP4Jcjs.verifyBuilderNotice.call(void 0, { rawBody, header, secret, now }) || verified;
78
78
  if (!verified) return json(401, { error: "invalid signature" });
79
79
  const notice = parseNotice(rawBody);
80
80
  if (notice === null) return json(400, { error: "invalid body" });
81
- const mapped = _chunkKNA3T4M6cjs.scopesToTags.call(void 0, notice.scopes);
82
- const tags = mapped.length > MAX_TAGS_PER_NOTICE ? [_chunkKNA3T4M6cjs.BUILDER_TAGS.all] : mapped;
81
+ const mapped = _chunkYLRXAP4Jcjs.scopesToTags.call(void 0, notice.scopes);
82
+ const tags = mapped.length > MAX_TAGS_PER_NOTICE ? [_chunkYLRXAP4Jcjs.BUILDER_TAGS.all] : mapped;
83
83
  const revalidate = await _asyncNullishCoalesce(options.revalidateTag, async () => ( await defaultRevalidateTag()));
84
84
  for (const tag of tags) revalidate(tag);
85
85
  return json(200, { revalidated: tags });
@@ -104,10 +104,10 @@ function createWatchSyncHandler(options) {
104
104
  let running;
105
105
  const run = async (key) => {
106
106
  const doFetch = _nullishCoalesce(options.fetch, () => ( fetch));
107
- const base = (_nullishCoalesce(options.baseUrl, () => ( _chunkKNA3T4M6cjs.DEFAULT_BASE_URL))).replace(/\/+$/u, "");
107
+ const base = (_nullishCoalesce(options.baseUrl, () => ( _chunkYLRXAP4Jcjs.DEFAULT_BASE_URL))).replace(/\/+$/u, "");
108
108
  const read = async (init) => {
109
109
  const response = await doFetch(`${base}/v1/site`, {
110
- headers: { "X-Builder-Key": key, [_chunkKNA3T4M6cjs.SCHEMA_MIN_HEADER]: _chunkKNA3T4M6cjs.BUILDER_SCHEMA_MIN },
110
+ headers: { "X-Builder-Key": key, [_chunkYLRXAP4Jcjs.SCHEMA_MIN_HEADER]: _chunkYLRXAP4Jcjs.BUILDER_SCHEMA_MIN },
111
111
  signal: AbortSignal.timeout(READ_TIMEOUT_MS),
112
112
  ...init
113
113
  });
@@ -120,14 +120,14 @@ function createWatchSyncHandler(options) {
120
120
  let generation;
121
121
  let recorded;
122
122
  try {
123
- recorded = await read({ next: { tags: [_chunkKNA3T4M6cjs.BUILDER_TAGS.all], revalidate: false } });
123
+ recorded = await read({ next: { tags: [_chunkYLRXAP4Jcjs.BUILDER_TAGS.all], revalidate: false } });
124
124
  generation = await read({ cache: "no-store" });
125
125
  } catch (e2) {
126
126
  return json2(502, { error: "The Builder could not be read." });
127
127
  }
128
128
  if (generation === recorded) return json2(200, { changed: false, generation });
129
129
  const revalidate = await _asyncNullishCoalesce(options.revalidateTag, async () => ( await defaultRevalidateTag2()));
130
- revalidate(_chunkKNA3T4M6cjs.BUILDER_TAGS.all);
130
+ revalidate(_chunkYLRXAP4Jcjs.BUILDER_TAGS.all);
131
131
  return json2(200, { changed: true, generation });
132
132
  };
133
133
  return () => {
@@ -184,4 +184,4 @@ async function getPreviewToken() {
184
184
 
185
185
 
186
186
 
187
- exports.PREVIEW_COOKIE = PREVIEW_COOKIE; exports.createRevalidateHandler = createRevalidateHandler; exports.createWatchSyncHandler = createWatchSyncHandler; exports.getPreviewToken = getPreviewToken; exports.previewMiddleware = previewMiddleware; exports.verifyBuilderNotice = _chunkKNA3T4M6cjs.verifyBuilderNotice;
187
+ exports.PREVIEW_COOKIE = PREVIEW_COOKIE; exports.createRevalidateHandler = createRevalidateHandler; exports.createWatchSyncHandler = createWatchSyncHandler; exports.getPreviewToken = getPreviewToken; exports.previewMiddleware = previewMiddleware; exports.verifyBuilderNotice = _chunkYLRXAP4Jcjs.verifyBuilderNotice;
package/dist/next.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  TAG_MAX_CHARS,
7
7
  scopesToTags,
8
8
  verifyBuilderNotice
9
- } from "./chunk-3PRNCLZO.js";
9
+ } from "./chunk-SRTHMABZ.js";
10
10
 
11
11
  // src/next/index.ts
12
12
  import "server-only";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usethrottle/builder",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Headless client for Throttle Builder: read published pages, parts, slots and site settings from a storefront, with Next.js cache, revalidation and preview helpers.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -59,7 +59,7 @@
59
59
  ],
60
60
  "dependencies": {
61
61
  "server-only": "0.0.1",
62
- "@usethrottle/builder-schema": "^0.4.1"
62
+ "@usethrottle/builder-schema": "^0.4.5"
63
63
  },
64
64
  "peerDependencies": {
65
65
  "next": ">=14",