storyblok 4.23.4 → 4.23.5

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/index.mjs CHANGED
@@ -158,12 +158,32 @@ const directories = {
158
158
  * Splits an iterable into batches of at most `size` items, preserving order.
159
159
  * Returns `[]` for empty input. If `size <= 0`, returns the full input as a single batch.
160
160
  */
161
- const chunk = (items, size) => {
162
- const all = Array.from(items);
163
- if (all.length === 0) return [];
164
- if (size <= 0) return [all];
161
+ const chunk = (items, size) => chunkByWeight(items, {
162
+ maxSize: size,
163
+ maxWeight: Infinity,
164
+ weightOf: () => 0
165
+ });
166
+ /**
167
+ * Splits an iterable into batches, preserving order, so that each batch holds at most `maxSize`
168
+ * items and their summed `weightOf` stays within `maxWeight`. An item heavier than `maxWeight`
169
+ * gets a batch of its own rather than being dropped. A `maxSize <= 0` means no size limit.
170
+ */
171
+ const chunkByWeight = (items, { maxSize, maxWeight, weightOf }) => {
172
+ const sizeLimit = maxSize > 0 ? maxSize : Infinity;
165
173
  const chunks = [];
166
- for (let i = 0; i < all.length; i += size) chunks.push(all.slice(i, i + size));
174
+ let current = [];
175
+ let currentWeight = 0;
176
+ for (const item of items) {
177
+ const weight = weightOf(item);
178
+ if (current.length > 0 && (current.length >= sizeLimit || currentWeight + weight > maxWeight)) {
179
+ chunks.push(current);
180
+ current = [];
181
+ currentWeight = 0;
182
+ }
183
+ current.push(item);
184
+ currentWeight += weight;
185
+ }
186
+ if (current.length > 0) chunks.push(current);
167
187
  return chunks;
168
188
  };
169
189
  //#endregion
@@ -4168,6 +4188,9 @@ const updateStory = async (spaceId, storyId, payload) => {
4168
4188
  }
4169
4189
  };
4170
4190
  const PREFETCH_CHUNK_SIZE = 100;
4191
+ const PREFETCH_MAX_ENCODED_SLUGS_LENGTH = 6e3;
4192
+ const ENCODED_SEPARATOR_LENGTH = 3;
4193
+ const MAX_REPORTED_SLUG_LENGTH = 100;
4171
4194
  const PREFETCH_PER_PAGE = 100;
4172
4195
  const addRef = (result, story) => {
4173
4196
  const ref = {
@@ -4212,6 +4235,8 @@ const fetchChunkAllPages = async (spaceId, params, onPageStories) => {
4212
4235
  *
4213
4236
  * Slug and id batches are dispatched concurrently through the MAPI client's
4214
4237
  * existing throttle (default 6 requests per second, auto-retries 429).
4238
+ *
4239
+ * Throws before any request if a slug is too long to fit into a lookup URL.
4215
4240
  */
4216
4241
  const prefetchTargetStoriesByKeys = async (spaceId, keys, options) => {
4217
4242
  const result = {
@@ -4223,8 +4248,20 @@ const prefetchTargetStoriesByKeys = async (spaceId, keys, options) => {
4223
4248
  const idSet = /* @__PURE__ */ new Set();
4224
4249
  for (const id of keys.ids) if (typeof id === "number" && Number.isFinite(id)) idSet.add(id);
4225
4250
  options?.onTotal?.(slugSet.size + idSet.size);
4251
+ const encodedSlugLength = (slug) => encodeURIComponent(slug).length + ENCODED_SEPARATOR_LENGTH;
4252
+ const unmatchableSlugs = [...slugSet].filter((slug) => encodedSlugLength(slug) > PREFETCH_MAX_ENCODED_SLUGS_LENGTH);
4253
+ if (unmatchableSlugs.length > 0) {
4254
+ const reportedSlugs = unmatchableSlugs.map((slug) => {
4255
+ return ` ${slug.length > MAX_REPORTED_SLUG_LENGTH ? `…${slug.slice(-100)}` : slug} (${encodedSlugLength(slug)} characters)`;
4256
+ });
4257
+ throw new Error([`Full slugs longer than ${PREFETCH_MAX_ENCODED_SLUGS_LENGTH} URL-encoded characters can't be matched with existing stories in the target space. Shorten the slugs or nest the stories in fewer folders. Affected stories (${unmatchableSlugs.length}):`, ...reportedSlugs].join("\n"));
4258
+ }
4226
4259
  if (slugSet.size === 0 && idSet.size === 0) return result;
4227
- const slugChunks = chunk(slugSet, PREFETCH_CHUNK_SIZE);
4260
+ const slugChunks = chunkByWeight(slugSet, {
4261
+ maxSize: PREFETCH_CHUNK_SIZE,
4262
+ maxWeight: PREFETCH_MAX_ENCODED_SLUGS_LENGTH,
4263
+ weightOf: encodedSlugLength
4264
+ });
4228
4265
  const idChunks = chunk(idSet, PREFETCH_CHUNK_SIZE);
4229
4266
  const requests = [];
4230
4267
  for (const slugs of slugChunks) requests.push((async () => {