@tdacorp/hyle-sdk 0.3.0 → 0.8.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.
- package/README.md +135 -5
- package/bin/hyle-sdk-check-routes.mjs +3 -0
- package/dist/check-routes-bin.js +537 -0
- package/dist/chunk-67IOIAVW.js +35 -0
- package/dist/chunk-BUJFMR3M.js +38 -0
- package/dist/chunk-EBNIKRQH.js +536 -0
- package/dist/{chunk-EARQBSMF.js → chunk-LGW2KP4X.js} +109 -5
- package/dist/{chunk-7VCMJUPC.js → chunk-N4QYPO7S.js} +1 -1
- package/dist/diagram-viewer-DYX6R63Y.js +1132 -0
- package/dist/index.cjs +279 -23
- package/dist/index.d.cts +147 -3
- package/dist/index.d.ts +147 -3
- package/dist/index.js +18 -1
- package/dist/mermaid-diagram.cjs +1267 -16
- package/dist/mermaid-diagram.d.cts +39 -1
- package/dist/mermaid-diagram.d.ts +39 -1
- package/dist/mermaid-diagram.js +35 -5
- package/dist/next.cjs +385 -30
- package/dist/next.d.cts +93 -5
- package/dist/next.d.ts +93 -5
- package/dist/next.js +210 -5
- package/dist/react.cjs +118 -5
- package/dist/react.d.cts +44 -4
- package/dist/react.d.ts +44 -4
- package/dist/react.js +2 -1
- package/dist/{resolve-fMx2W8RR.d.ts → resolve-1rQntRBh.d.ts} +1 -1
- package/dist/{resolve-B_p0p4Zn.d.cts → resolve-36-Ng1H0.d.cts} +1 -1
- package/dist/styles.css +76 -0
- package/dist/{types-CBvLhUPr.d.ts → types-BoyBWF2F.d.ts} +1 -1
- package/dist/{types-DjXf6iPo.d.cts → types-CwBCPh6K.d.cts} +20 -1
- package/dist/{types-DjXf6iPo.d.ts → types-CwBCPh6K.d.ts} +20 -1
- package/dist/{types-D0qoLh4E.d.cts → types-DnPzhrFx.d.cts} +1 -1
- package/dist/use-cache.cjs +55 -23
- package/dist/use-cache.d.cts +2 -2
- package/dist/use-cache.d.ts +2 -2
- package/dist/use-cache.js +3 -2
- package/package.json +5 -1
- package/dist/chunk-HFUJRNFP.js +0 -290
package/dist/chunk-HFUJRNFP.js
DELETED
|
@@ -1,290 +0,0 @@
|
|
|
1
|
-
// src/errors.ts
|
|
2
|
-
var Cp1ClientError = class extends Error {
|
|
3
|
-
code;
|
|
4
|
-
status;
|
|
5
|
-
requestId;
|
|
6
|
-
details;
|
|
7
|
-
constructor(code, message, status, requestId, details) {
|
|
8
|
-
super(message);
|
|
9
|
-
this.name = "Cp1ClientError";
|
|
10
|
-
this.code = code;
|
|
11
|
-
this.status = status;
|
|
12
|
-
this.requestId = requestId;
|
|
13
|
-
this.details = details;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
// src/client.ts
|
|
18
|
-
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
19
|
-
var DEFAULT_MAX_RESPONSE_BYTES = 5 * 1024 * 1024;
|
|
20
|
-
var LOOPBACK = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
21
|
-
async function readCapped(res, max) {
|
|
22
|
-
const tooLarge = () => new Cp1ClientError("RESPONSE_TOO_LARGE", `hyle response exceeds ${max} bytes`, res.status, "");
|
|
23
|
-
if (Number(res.headers.get("content-length")) > max) throw tooLarge();
|
|
24
|
-
if (!res.body) return "";
|
|
25
|
-
const reader = res.body.getReader();
|
|
26
|
-
const chunks = [];
|
|
27
|
-
let total = 0;
|
|
28
|
-
for (; ; ) {
|
|
29
|
-
const { done, value } = await reader.read();
|
|
30
|
-
if (done) break;
|
|
31
|
-
total += value.byteLength;
|
|
32
|
-
if (total > max) {
|
|
33
|
-
await reader.cancel();
|
|
34
|
-
throw tooLarge();
|
|
35
|
-
}
|
|
36
|
-
chunks.push(value);
|
|
37
|
-
}
|
|
38
|
-
return Buffer.concat(chunks).toString("utf8");
|
|
39
|
-
}
|
|
40
|
-
var KINDS = /* @__PURE__ */ new Set(["article", "list", "redirect", "notfound", "disabled"]);
|
|
41
|
-
function createHyleClient(options) {
|
|
42
|
-
let origin;
|
|
43
|
-
try {
|
|
44
|
-
origin = new URL(options.baseUrl);
|
|
45
|
-
} catch {
|
|
46
|
-
throw new TypeError("baseUrl must be an https origin");
|
|
47
|
-
}
|
|
48
|
-
if (origin.protocol !== "https:" && !(origin.protocol === "http:" && (options.allowInsecureHttp || LOOPBACK.has(origin.hostname)))) {
|
|
49
|
-
throw new TypeError("baseUrl must be https (http is allowed only for localhost, or with allowInsecureHttp)");
|
|
50
|
-
}
|
|
51
|
-
if (!options.token) throw new TypeError("token is required");
|
|
52
|
-
const base = options.baseUrl.replace(/\/+$/, "");
|
|
53
|
-
const fetchImpl = options.fetch ?? fetch;
|
|
54
|
-
async function resolve(opts) {
|
|
55
|
-
const url = new URL(`${base}/cp1/read/resolve`);
|
|
56
|
-
url.searchParams.set("path", opts.path);
|
|
57
|
-
if (opts.locale !== void 0) url.searchParams.set("locale", opts.locale);
|
|
58
|
-
if (opts.format !== void 0) url.searchParams.set("format", opts.format);
|
|
59
|
-
if (opts.cursor !== void 0) url.searchParams.set("cursor", opts.cursor);
|
|
60
|
-
if (opts.limit !== void 0) url.searchParams.set("limit", String(opts.limit));
|
|
61
|
-
const headers = new Headers(opts.init?.headers);
|
|
62
|
-
headers.set("authorization", `Bearer ${options.token}`);
|
|
63
|
-
const timeout = AbortSignal.timeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
64
|
-
const signal = opts.init?.signal ? AbortSignal.any([opts.init.signal, timeout]) : timeout;
|
|
65
|
-
const res = await fetchImpl(url.toString(), { ...opts.init, method: "GET", headers, redirect: "error", signal });
|
|
66
|
-
const text = await readCapped(res, options.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES);
|
|
67
|
-
let parsed;
|
|
68
|
-
try {
|
|
69
|
-
parsed = JSON.parse(text);
|
|
70
|
-
} catch {
|
|
71
|
-
parsed = void 0;
|
|
72
|
-
}
|
|
73
|
-
const data = parsed?.data;
|
|
74
|
-
if (data && data.cp === 1 && typeof data.kind === "string" && KINDS.has(data.kind)) return data;
|
|
75
|
-
const err = parsed?.error;
|
|
76
|
-
throw new Cp1ClientError(
|
|
77
|
-
err?.code ?? "INVALID_RESPONSE",
|
|
78
|
-
err?.message ?? `Unexpected response from hyle (HTTP ${res.status})`,
|
|
79
|
-
res.status,
|
|
80
|
-
err?.request_id ?? "",
|
|
81
|
-
err?.details
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
return { resolve };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// src/delivery-dedupe.ts
|
|
88
|
-
var DeliveryDedupe = class {
|
|
89
|
-
constructor(capacity = 1e3) {
|
|
90
|
-
this.capacity = capacity;
|
|
91
|
-
}
|
|
92
|
-
capacity;
|
|
93
|
-
seen = /* @__PURE__ */ new Map();
|
|
94
|
-
/** True if this id was already recorded (a duplicate delivery). */
|
|
95
|
-
has(deliveryId) {
|
|
96
|
-
return this.seen.has(deliveryId);
|
|
97
|
-
}
|
|
98
|
-
add(deliveryId) {
|
|
99
|
-
this.seen.delete(deliveryId);
|
|
100
|
-
this.seen.set(deliveryId, true);
|
|
101
|
-
if (this.seen.size > this.capacity) {
|
|
102
|
-
const oldest = this.seen.keys().next().value;
|
|
103
|
-
if (oldest !== void 0) this.seen.delete(oldest);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// src/signature.ts
|
|
109
|
-
import { createHmac, timingSafeEqual } from "crypto";
|
|
110
|
-
var SIGNATURE_HEX = /^[0-9a-f]{64}$/;
|
|
111
|
-
function parseSignatureHeader(header) {
|
|
112
|
-
let timestamp;
|
|
113
|
-
const signatures = [];
|
|
114
|
-
for (const part of header.split(",")) {
|
|
115
|
-
const [key, value] = part.split("=", 2);
|
|
116
|
-
if (key === "t" && value) {
|
|
117
|
-
const parsed = Number(value);
|
|
118
|
-
if (Number.isFinite(parsed)) timestamp = parsed;
|
|
119
|
-
} else if (key === "v1" && value && SIGNATURE_HEX.test(value)) {
|
|
120
|
-
signatures.push(value);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (timestamp === void 0 || signatures.length === 0) return void 0;
|
|
124
|
-
return { timestamp, signatures };
|
|
125
|
-
}
|
|
126
|
-
function computeSignature(secret, timestamp, rawBody) {
|
|
127
|
-
return createHmac("sha256", secret).update(`${timestamp}.${rawBody}`).digest("hex");
|
|
128
|
-
}
|
|
129
|
-
function signaturesEqual(a, b) {
|
|
130
|
-
const bufA = Buffer.from(a, "hex");
|
|
131
|
-
const bufB = Buffer.from(b, "hex");
|
|
132
|
-
if (bufA.length !== bufB.length) return false;
|
|
133
|
-
return timingSafeEqual(bufA, bufB);
|
|
134
|
-
}
|
|
135
|
-
function verifyHyleSignature(params) {
|
|
136
|
-
const secrets = params.secrets.filter((s) => s !== "");
|
|
137
|
-
if (!params.header || secrets.length === 0) return false;
|
|
138
|
-
const parsed = parseSignatureHeader(params.header);
|
|
139
|
-
if (!parsed) return false;
|
|
140
|
-
const tolerance = params.toleranceSeconds ?? 300;
|
|
141
|
-
const now = (params.now ?? Date.now)() / 1e3;
|
|
142
|
-
if (Math.abs(now - parsed.timestamp) > tolerance) return false;
|
|
143
|
-
for (const secret of secrets) {
|
|
144
|
-
const expected = computeSignature(secret, parsed.timestamp, params.rawBody);
|
|
145
|
-
for (const candidate of parsed.signatures) {
|
|
146
|
-
if (signaturesEqual(candidate, expected)) return true;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return false;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// src/tag-validation.ts
|
|
153
|
-
var CACHE_TAG_PATTERN = /^hyle:1:(pub|doc|sec|link|ws):[0-9a-f-]{36}(:[\x21-\x7e]{1,200})?$/;
|
|
154
|
-
function isValidCacheTag(tag) {
|
|
155
|
-
return CACHE_TAG_PATTERN.test(tag);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// src/revalidation-callback.ts
|
|
159
|
-
var MAX_BODY_BYTES = 65536;
|
|
160
|
-
var MAX_TAGS = 1e3;
|
|
161
|
-
function createRevalidationCallbackRoute(options) {
|
|
162
|
-
const dedupe = new DeliveryDedupe(options.dedupeCapacity);
|
|
163
|
-
return {
|
|
164
|
-
async POST(req) {
|
|
165
|
-
const rawBody = await readCapped2(req);
|
|
166
|
-
if (rawBody === void 0) return new Response(null, { status: 413 });
|
|
167
|
-
const verified = verifyHyleSignature({
|
|
168
|
-
header: req.headers.get("hyle-signature"),
|
|
169
|
-
rawBody,
|
|
170
|
-
secrets: options.secrets,
|
|
171
|
-
...options.toleranceSeconds !== void 0 ? { toleranceSeconds: options.toleranceSeconds } : {},
|
|
172
|
-
...options.now !== void 0 ? { now: options.now } : {}
|
|
173
|
-
});
|
|
174
|
-
if (!verified) {
|
|
175
|
-
return new Response(null, { status: 401 });
|
|
176
|
-
}
|
|
177
|
-
let payload;
|
|
178
|
-
try {
|
|
179
|
-
payload = JSON.parse(rawBody);
|
|
180
|
-
} catch {
|
|
181
|
-
return new Response(null, { status: 400 });
|
|
182
|
-
}
|
|
183
|
-
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return new Response(null, { status: 400 });
|
|
184
|
-
const body = payload;
|
|
185
|
-
if (body.cp !== 1) {
|
|
186
|
-
return okResponse(0);
|
|
187
|
-
}
|
|
188
|
-
const tags = body.tags ?? [];
|
|
189
|
-
if (typeof body.workspace_id !== "string" || !Array.isArray(tags) || tags.length > MAX_TAGS || !tags.every((t) => typeof t === "string")) {
|
|
190
|
-
return new Response(null, { status: 400 });
|
|
191
|
-
}
|
|
192
|
-
if (body.workspace_id !== options.workspaceId) {
|
|
193
|
-
return okResponse(0);
|
|
194
|
-
}
|
|
195
|
-
const deliveryId = req.headers.get("hyle-delivery-id");
|
|
196
|
-
if (deliveryId && dedupe.has(deliveryId)) {
|
|
197
|
-
return okResponse(0);
|
|
198
|
-
}
|
|
199
|
-
let purged = 0;
|
|
200
|
-
try {
|
|
201
|
-
for (const tag of tags) {
|
|
202
|
-
if (!isValidCacheTag(tag)) continue;
|
|
203
|
-
options.revalidateTag(tag);
|
|
204
|
-
purged += 1;
|
|
205
|
-
}
|
|
206
|
-
} catch {
|
|
207
|
-
return new Response(null, { status: 500 });
|
|
208
|
-
}
|
|
209
|
-
if (deliveryId) dedupe.add(deliveryId);
|
|
210
|
-
return okResponse(purged);
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
async function readCapped2(req) {
|
|
215
|
-
const declared = Number(req.headers.get("content-length"));
|
|
216
|
-
if (Number.isFinite(declared) && declared > MAX_BODY_BYTES) return void 0;
|
|
217
|
-
if (!req.body) return "";
|
|
218
|
-
const reader = req.body.getReader();
|
|
219
|
-
const chunks = [];
|
|
220
|
-
let total = 0;
|
|
221
|
-
for (; ; ) {
|
|
222
|
-
const { done, value } = await reader.read();
|
|
223
|
-
if (done) break;
|
|
224
|
-
total += value.byteLength;
|
|
225
|
-
if (total > MAX_BODY_BYTES) {
|
|
226
|
-
await reader.cancel();
|
|
227
|
-
return void 0;
|
|
228
|
-
}
|
|
229
|
-
chunks.push(value);
|
|
230
|
-
}
|
|
231
|
-
return Buffer.concat(chunks).toString("utf8");
|
|
232
|
-
}
|
|
233
|
-
function okResponse(purged) {
|
|
234
|
-
return new Response(JSON.stringify({ ok: true, purged }), {
|
|
235
|
-
status: 200,
|
|
236
|
-
headers: { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" }
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// src/sitemap.ts
|
|
241
|
-
var PAGE_LIMIT = 100;
|
|
242
|
-
var DEFAULT_MAX_PAGES = 20;
|
|
243
|
-
var SECTION_SLUG = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
|
|
244
|
-
var UNSAFE_PATH = /[\\?#<>&"'\u0000- \u007f]/;
|
|
245
|
-
async function listPublicationPaths(options) {
|
|
246
|
-
const maxPages = options.maxPages ?? DEFAULT_MAX_PAGES;
|
|
247
|
-
const prefix = `${options.basePath}/`;
|
|
248
|
-
const indexes = [options.basePath || "/"];
|
|
249
|
-
for (const slug of options.sections ?? []) if (slug !== "posts" && SECTION_SLUG.test(slug)) indexes.push(`${options.basePath}/${slug}`);
|
|
250
|
-
const found = /* @__PURE__ */ new Map();
|
|
251
|
-
let complete = true;
|
|
252
|
-
try {
|
|
253
|
-
for (const index of new Set(indexes)) {
|
|
254
|
-
let cursor;
|
|
255
|
-
for (let page = 0; page < maxPages; page++) {
|
|
256
|
-
const result = await options.client.resolve({
|
|
257
|
-
path: index,
|
|
258
|
-
limit: PAGE_LIMIT,
|
|
259
|
-
...options.locale !== void 0 ? { locale: options.locale } : {},
|
|
260
|
-
...cursor !== void 0 ? { cursor } : {}
|
|
261
|
-
});
|
|
262
|
-
if (result.kind !== "list") {
|
|
263
|
-
complete = false;
|
|
264
|
-
break;
|
|
265
|
-
}
|
|
266
|
-
for (const item of result.items) {
|
|
267
|
-
if (item.canonical_path.startsWith(prefix) && !UNSAFE_PATH.test(item.canonical_path)) found.set(item.canonical_path, item.published_at);
|
|
268
|
-
}
|
|
269
|
-
if (!result.next_cursor) break;
|
|
270
|
-
if (page === maxPages - 1) complete = false;
|
|
271
|
-
cursor = result.next_cursor;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
} catch {
|
|
275
|
-
complete = false;
|
|
276
|
-
}
|
|
277
|
-
return { paths: [...found].map(([path, publishedAt]) => ({ path, publishedAt })), complete };
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
export {
|
|
281
|
-
Cp1ClientError,
|
|
282
|
-
createHyleClient,
|
|
283
|
-
DeliveryDedupe,
|
|
284
|
-
parseSignatureHeader,
|
|
285
|
-
verifyHyleSignature,
|
|
286
|
-
CACHE_TAG_PATTERN,
|
|
287
|
-
isValidCacheTag,
|
|
288
|
-
createRevalidationCallbackRoute,
|
|
289
|
-
listPublicationPaths
|
|
290
|
-
};
|