@tdacorp/hyle-sdk 0.23.0 → 0.24.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.
Files changed (36) hide show
  1. package/README.md +36 -18
  2. package/dist/check-routes-bin.js +34 -16
  3. package/dist/{chunk-UFMKLCXS.js → chunk-5TKJH2XC.js} +2 -2
  4. package/dist/{chunk-VXARM37R.js → chunk-BDFMNY76.js} +11 -5
  5. package/dist/{chunk-VUML2UPD.js → chunk-O4QWHXVH.js} +1 -1
  6. package/dist/{chunk-YHXE3TGS.js → chunk-RCK4NCYC.js} +33 -16
  7. package/dist/{chunk-JHZJOJDC.js → chunk-Y3BACEWA.js} +1 -1
  8. package/dist/{client-DK-S2580.d.cts → client-CI-3cHtc.d.cts} +13 -5
  9. package/dist/{client--ighAWaj.d.ts → client-DN38IP63.d.ts} +13 -5
  10. package/dist/comments.cjs +3 -2
  11. package/dist/comments.js +3 -2
  12. package/dist/{embed-g7NT1CyB.d.ts → embed-BaLQbE_n.d.ts} +1 -1
  13. package/dist/{embed-XT0x_Jaa.d.cts → embed-BrrGgkgQ.d.cts} +1 -1
  14. package/dist/html.cjs +3 -2
  15. package/dist/html.js +2 -2
  16. package/dist/index.cjs +34 -16
  17. package/dist/index.d.cts +3 -3
  18. package/dist/index.d.ts +3 -3
  19. package/dist/index.js +2 -2
  20. package/dist/next.cjs +316 -184
  21. package/dist/next.d.cts +46 -31
  22. package/dist/next.d.ts +46 -31
  23. package/dist/next.js +265 -154
  24. package/dist/react.cjs +3 -2
  25. package/dist/react.d.cts +3 -3
  26. package/dist/react.d.ts +3 -3
  27. package/dist/react.js +3 -3
  28. package/dist/{resolve-CiZCPysX.d.cts → resolve-Ce_yz8gC.d.cts} +1 -1
  29. package/dist/{resolve-BqC9l5RH.d.ts → resolve-DMHbYyTS.d.ts} +1 -1
  30. package/dist/{types-b9QbmEDd.d.cts → types-CxE8kLeo.d.cts} +30 -5
  31. package/dist/{types-b9QbmEDd.d.ts → types-CxE8kLeo.d.ts} +30 -5
  32. package/dist/use-cache.cjs +34 -16
  33. package/dist/use-cache.d.cts +3 -3
  34. package/dist/use-cache.d.ts +3 -3
  35. package/dist/use-cache.js +3 -3
  36. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -61,9 +61,11 @@ var PREVIEW_PARAM = "hyle_preview";
61
61
  var PREVIEW_QUERY_SOURCE = `\\?${PREVIEW_PARAM}=${PREVIEW_TOKEN_SOURCE}`;
62
62
  var PROTECTED_PARAM = "hyle_protected";
63
63
  var PROTECTED_HEADER = "cp-protected";
64
- var PROTECTED_ID = new RegExp(`^${UUID}$`);
65
- var PROTECTED_PASSWORD = /^[A-Za-z0-9]{16}$/;
66
- var PROTECTED_QUERY_SOURCE = `\\?${PROTECTED_PARAM}=${UUID}`;
64
+ var SHARE_SLUG = "[a-z0-9-]+(?:/[a-z0-9-]+){0,3}";
65
+ var PROTECTED_SLUG = new RegExp(`^${SHARE_SLUG}$`);
66
+ var PROTECTED_DOC_SLUG = /^[a-z0-9-]+$/;
67
+ var PROTECTED_PASSWORD = /^[\x21\x23-\x24\x26-\x2B\x2D-\x3A\x3C-\x5B\x5D-\x7E]{12,128}$/;
68
+ var PROTECTED_QUERY_SOURCE = `\\?${PROTECTED_PARAM}=${SHARE_SLUG}`;
67
69
  var HYLE_ASSET_URL = new RegExp(`^/assets/(${UUID}(?:\\.(?:${EXTENSIONS}))?)(${PREVIEW_QUERY_SOURCE}|${PROTECTED_QUERY_SOURCE})?$`, "i");
68
70
 
69
71
  // src/errors.ts
@@ -119,6 +121,8 @@ function applyCredential(headers, credential, method, url) {
119
121
  headers.set("cp-workspace", workspaceId);
120
122
  });
121
123
  }
124
+ var IP_LITERAL = /^[0-9a-fA-F:.]{2,45}$/;
125
+ var visitorHeaders = ({ clientIp }) => clientIp !== void 0 && IP_LITERAL.test(clientIp) ? { "x-forwarded-for": clientIp } : {};
122
126
  var DEFAULT_TIMEOUT_MS = 1e4;
123
127
  var DEFAULT_MAX_RESPONSE_BYTES = 5 * 1024 * 1024;
124
128
  var LOOPBACK = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]"]);
@@ -145,7 +149,7 @@ var KINDS = /* @__PURE__ */ new Set(["article", "list", "redirect", "notfound",
145
149
  var isStrings = (value) => Array.isArray(value) && value.every((v) => typeof v === "string");
146
150
  function isMounts(data) {
147
151
  const mounts = data.mounts;
148
- return typeof data.workspace_id === "string" && isStrings(data.reserved) && isStrings(data.supported_locales) && Array.isArray(mounts) && mounts.every((m) => typeof m?.prefix === "string" && typeof m.default_section === "string");
152
+ return typeof data.workspace_id === "string" && isStrings(data.reserved) && isStrings(data.supported_locales) && Array.isArray(mounts) && mounts.every((m) => typeof m?.prefix === "string" && typeof m.default_section === "string") && (data.protected === void 0 || isStrings(data.protected));
149
153
  }
150
154
  function checkBaseUrl(baseUrl, allowInsecureHttp) {
151
155
  let origin;
@@ -180,7 +184,7 @@ function createHyleClient(options) {
180
184
  } catch {
181
185
  parsed = void 0;
182
186
  }
183
- return { parsed, status: res.status };
187
+ return { parsed, status: res.status, retryAfter: res.headers.get("retry-after") };
184
188
  }
185
189
  const failure = ({ parsed, status }) => {
186
190
  const err = parsed?.error;
@@ -228,22 +232,38 @@ function createHyleClient(options) {
228
232
  if (data?.cp !== 1 || data.kind !== "preview" || typeof data.expires_at !== "string" || article?.kind !== "article" || article.content?.format !== "hast" || !article.publication) throw failure(reply);
229
233
  return { kind: "preview", expires_at: data.expires_at, article: withAssetQuery(article, `${PREVIEW_PARAM}=${token}`) };
230
234
  }
231
- async function protectedPage(pageId, password) {
232
- if (!PROTECTED_ID.test(pageId) || !PROTECTED_PASSWORD.test(password)) return { kind: "notfound" };
233
- const reply = await get("/cp1/read/_protected", new URLSearchParams(), { headers: { [PROTECTED_HEADER]: `${pageId}.${password}` }, cache: "no-store" });
235
+ const limited = ({ retryAfter }) => ({ kind: "limited", retryAfterSeconds: Math.min(3600, Math.max(1, Math.trunc(Number(retryAfter)) || 60)) });
236
+ async function protectedGate(slug, opts = {}) {
237
+ if (!PROTECTED_SLUG.test(slug)) return { kind: "notfound" };
238
+ const reply = await get("/cp1/read/_protected/gate", new URLSearchParams({ path: slug }), { headers: visitorHeaders(opts), cache: "no-store" });
239
+ if (reply.status === 429) return limited(reply);
240
+ if (reply.status === 404 && reply.parsed?.error?.code === "NOT_FOUND") return { kind: "notfound" };
241
+ const data = reply.parsed?.data;
242
+ if (data?.cp === 1 && data.kind === "disabled") return { kind: "disabled" };
243
+ if (data?.cp === 1 && data.kind === "gate") return { kind: "gate" };
244
+ throw failure(reply);
245
+ }
246
+ async function protectedShare(slug, password, docSlug, opts = {}) {
247
+ if (!PROTECTED_SLUG.test(slug) || docSlug !== void 0 && !PROTECTED_DOC_SLUG.test(docSlug) || !PROTECTED_PASSWORD.test(password)) return { kind: "notfound" };
248
+ const path = docSlug === void 0 ? slug : `${slug}/${docSlug}`;
249
+ const reply = await get("/cp1/read/_protected", new URLSearchParams({ path }), { headers: { ...visitorHeaders(opts), [PROTECTED_HEADER]: password }, cache: "no-store" });
250
+ if (reply.status === 429) return limited(reply);
234
251
  if (reply.status === 404 && reply.parsed?.error?.code === "NOT_FOUND") return { kind: "notfound" };
235
252
  const data = reply.parsed?.data;
236
253
  if (data?.cp === 1 && data.kind === "disabled") return { kind: "disabled" };
237
254
  const article = data?.article;
238
- const expires = data?.expires_at;
239
- if (data?.cp !== 1 || data.kind !== "protected" || typeof expires !== "string" && expires !== null || article?.kind !== "article" || article.content?.format !== "hast" || !article.publication) throw failure(reply);
240
- return { kind: "protected", expires_at: expires, article: withAssetQuery(article, `${PROTECTED_PARAM}=${pageId}`) };
255
+ const { name, expires_at: expires, documents, doc_slug: current } = data ?? {};
256
+ if (data?.cp !== 1 || data.kind !== "protected" || typeof name !== "string" || typeof expires !== "string" && expires !== null || !Array.isArray(documents) || typeof current !== "string" || article?.kind !== "article" || article.content?.format !== "hast" || !article.publication) throw failure(reply);
257
+ return { cp: 1, kind: "protected", name, expires_at: expires, documents, doc_slug: current, article: withAssetQuery(article, `${PROTECTED_PARAM}=${slug}`) };
241
258
  }
242
259
  async function asset(file, opts = {}) {
243
260
  if (!ASSET_FILE.test(file)) throw new TypeError("asset file must be <uuid>[.<ext>]");
244
261
  const { previewToken } = opts;
245
262
  if (previewToken !== void 0 && !PREVIEW_TOKEN.test(previewToken)) throw new TypeError("previewToken is not a preview token");
246
- const url = `${base}/cp1/read/assets/${file}`;
263
+ const { protectedAccess } = opts;
264
+ if (protectedAccess !== void 0 && !(PROTECTED_SLUG.test(protectedAccess.slug) && PROTECTED_PASSWORD.test(protectedAccess.password))) throw new TypeError("protectedAccess is not a share slug and password");
265
+ const query = protectedAccess === void 0 ? "" : `?protected=${encodeURIComponent(protectedAccess.slug)}`;
266
+ const url = `${base}/cp1/read/assets/${file}${query}`;
247
267
  const headers = new Headers();
248
268
  const applied = applyCredential(headers, options, "GET", url);
249
269
  if (applied) await applied;
@@ -251,10 +271,8 @@ function createHyleClient(options) {
251
271
  const value = opts.headers?.[name];
252
272
  if (value) headers.set(name, value);
253
273
  }
254
- const { protectedAccess } = opts;
255
- if (protectedAccess !== void 0 && !(PROTECTED_ID.test(protectedAccess.pageId) && PROTECTED_PASSWORD.test(protectedAccess.password))) throw new TypeError("protectedAccess is not a page id and password");
256
274
  if (previewToken !== void 0) headers.set(PREVIEW_HEADER, previewToken);
257
- if (protectedAccess !== void 0) headers.set(PROTECTED_HEADER, `${protectedAccess.pageId}.${protectedAccess.password}`);
275
+ if (protectedAccess !== void 0) headers.set(PROTECTED_HEADER, protectedAccess.password);
258
276
  const uncached = previewToken !== void 0 || protectedAccess !== void 0;
259
277
  return fetchImpl(url, { method: "GET", headers, redirect: "error", signal: AbortSignal.timeout(opts.timeoutMs ?? options.timeoutMs ?? DEFAULT_TIMEOUT_MS), ...uncached ? { cache: "no-store" } : {} });
260
278
  }
@@ -265,7 +283,7 @@ function createHyleClient(options) {
265
283
  if (applied) await applied;
266
284
  return fetchImpl(url, { method: "GET", headers, redirect: "error", signal: AbortSignal.timeout(opts.timeoutMs ?? options.timeoutMs ?? DEFAULT_TIMEOUT_MS) });
267
285
  }
268
- return { resolve, mounts, pushPosts, preview, protected: protectedPage, asset, theme };
286
+ return { resolve, mounts, pushPosts, preview, protectedGate, protected: protectedShare, asset, theme };
269
287
  }
270
288
 
271
289
  // src/host-sync.ts
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
- import { H as HyleAuth, a as HyleClient } from './client-DK-S2580.cjs';
2
- export { A as AssetOptions, b as HyleClientOptions, c as HyleCredential, d as HyleRequestInit, M as MAX_PUSH_BODY_BYTES, e as MountsOptions, P as PushEvent, f as PushEventType, g as PushPage, h as PushPost, i as PushRoute, j as PushRouteOptions, k as PushStore, R as ResolveOptions, l as ResyncOptions, m as ResyncResult, n as createHyleClient, o as createPushRoute, r as resyncPath, s as staleAfterResync } from './client-DK-S2580.cjs';
3
- export { A as Alternate, a as ArticleContent, B as Breadcrumb, C as CacheInfo, D as DocumentMetaFields, E as EngageComment, b as EngageHeldComment, c as EngageListing, F as Fallback, H as HylePreview, d as HyleProtected, L as ListItem, M as Mount, e as Mounts, f as MountsResult, N as NavItem, g as NavNode, h as NavTree, R as ReactionCounts, i as ReactionKind, j as Resolved, k as ResolvedArticle, l as ResolvedDisabled, m as ResolvedList, n as ResolvedNotFound, o as ResolvedRedirect, T as ThemeRef, p as TocEntry, V as VoteValue } from './types-b9QbmEDd.cjs';
1
+ import { H as HyleAuth, a as HyleClient } from './client-CI-3cHtc.cjs';
2
+ export { A as AssetOptions, b as HyleClientOptions, c as HyleCredential, d as HyleRequestInit, M as MAX_PUSH_BODY_BYTES, e as MountsOptions, P as ProtectedReadOptions, f as PushEvent, g as PushEventType, h as PushPage, i as PushPost, j as PushRoute, k as PushRouteOptions, l as PushStore, R as ResolveOptions, m as ResyncOptions, n as ResyncResult, o as createHyleClient, p as createPushRoute, r as resyncPath, s as staleAfterResync } from './client-CI-3cHtc.cjs';
3
+ export { A as Alternate, a as ArticleContent, B as Breadcrumb, C as CacheInfo, D as DocumentMetaFields, E as EngageComment, b as EngageHeldComment, c as EngageListing, F as Fallback, H as HylePreview, d as HyleProtected, e as HyleProtectedGate, f as HyleProtectedLimited, g as HyleProtectedShare, L as ListItem, M as Mount, h as Mounts, i as MountsResult, N as NavItem, j as NavNode, k as NavTree, R as ReactionCounts, l as ReactionKind, m as Resolved, n as ResolvedArticle, o as ResolvedDisabled, p as ResolvedList, q as ResolvedNotFound, r as ResolvedRedirect, T as ThemeRef, s as TocEntry, V as VoteValue } from './types-CxE8kLeo.cjs';
4
4
  export { C as CommentEvent, a as CommentEventType, R as RevalidationCallbackOptions, b as RevalidationCallbackRoute, c as RevalidationPayload, d as createRevalidationCallbackRoute, p as parseCommentEvent } from './revalidation-callback-BQl77tQR.cjs';
5
5
 
6
6
  declare class Cp1ClientError extends Error {
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { H as HyleAuth, a as HyleClient } from './client--ighAWaj.js';
2
- export { A as AssetOptions, b as HyleClientOptions, c as HyleCredential, d as HyleRequestInit, M as MAX_PUSH_BODY_BYTES, e as MountsOptions, P as PushEvent, f as PushEventType, g as PushPage, h as PushPost, i as PushRoute, j as PushRouteOptions, k as PushStore, R as ResolveOptions, l as ResyncOptions, m as ResyncResult, n as createHyleClient, o as createPushRoute, r as resyncPath, s as staleAfterResync } from './client--ighAWaj.js';
3
- export { A as Alternate, a as ArticleContent, B as Breadcrumb, C as CacheInfo, D as DocumentMetaFields, E as EngageComment, b as EngageHeldComment, c as EngageListing, F as Fallback, H as HylePreview, d as HyleProtected, L as ListItem, M as Mount, e as Mounts, f as MountsResult, N as NavItem, g as NavNode, h as NavTree, R as ReactionCounts, i as ReactionKind, j as Resolved, k as ResolvedArticle, l as ResolvedDisabled, m as ResolvedList, n as ResolvedNotFound, o as ResolvedRedirect, T as ThemeRef, p as TocEntry, V as VoteValue } from './types-b9QbmEDd.js';
1
+ import { H as HyleAuth, a as HyleClient } from './client-DN38IP63.js';
2
+ export { A as AssetOptions, b as HyleClientOptions, c as HyleCredential, d as HyleRequestInit, M as MAX_PUSH_BODY_BYTES, e as MountsOptions, P as ProtectedReadOptions, f as PushEvent, g as PushEventType, h as PushPage, i as PushPost, j as PushRoute, k as PushRouteOptions, l as PushStore, R as ResolveOptions, m as ResyncOptions, n as ResyncResult, o as createHyleClient, p as createPushRoute, r as resyncPath, s as staleAfterResync } from './client-DN38IP63.js';
3
+ export { A as Alternate, a as ArticleContent, B as Breadcrumb, C as CacheInfo, D as DocumentMetaFields, E as EngageComment, b as EngageHeldComment, c as EngageListing, F as Fallback, H as HylePreview, d as HyleProtected, e as HyleProtectedGate, f as HyleProtectedLimited, g as HyleProtectedShare, L as ListItem, M as Mount, h as Mounts, i as MountsResult, N as NavItem, j as NavNode, k as NavTree, R as ReactionCounts, l as ReactionKind, m as Resolved, n as ResolvedArticle, o as ResolvedDisabled, p as ResolvedList, q as ResolvedNotFound, r as ResolvedRedirect, T as ThemeRef, s as TocEntry, V as VoteValue } from './types-CxE8kLeo.js';
4
4
  export { C as CommentEvent, a as CommentEventType, R as RevalidationCallbackOptions, b as RevalidationCallbackRoute, c as RevalidationPayload, d as createRevalidationCallbackRoute, p as parseCommentEvent } from './revalidation-callback-BQl77tQR.js';
5
5
 
6
6
  declare class Cp1ClientError extends Error {
package/dist/index.js CHANGED
@@ -23,8 +23,8 @@ import {
23
23
  staleAfterResync,
24
24
  syncHostReserved,
25
25
  verifyHyleSignature
26
- } from "./chunk-YHXE3TGS.js";
27
- import "./chunk-VXARM37R.js";
26
+ } from "./chunk-RCK4NCYC.js";
27
+ import "./chunk-BDFMNY76.js";
28
28
  export {
29
29
  CACHE_TAG_PATTERN,
30
30
  Cp1ClientError,