@thexjs/core 1.0.0 → 1.1.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/data.d.ts CHANGED
@@ -7,9 +7,20 @@ interface SQLiteOptions {
7
7
  }
8
8
  declare function connectSQLite(options?: SQLiteOptions): Database;
9
9
 
10
+ /** A Postgres client scoped to a single transaction (auto-commit/rollback). */
11
+ interface PostgresTransactionClient {
12
+ unsafe(query: string, params?: unknown[]): Promise<unknown>;
13
+ (strings: TemplateStringsArray, ...values: unknown[]): Promise<unknown>;
14
+ }
10
15
  interface PostgresClient {
11
- unsafe(query: string): Promise<unknown>;
16
+ unsafe(query: string, params?: unknown[]): Promise<unknown>;
12
17
  (strings: TemplateStringsArray, ...values: unknown[]): Promise<unknown>;
18
+ /**
19
+ * Runs `fn` inside a transaction. The callback receives a transaction-scoped
20
+ * client; when it resolves the transaction commits, when it throws it rolls
21
+ * back — so a group of statements either all apply or none do.
22
+ */
23
+ begin<T>(fn: (tx: PostgresTransactionClient) => Promise<T>): Promise<T>;
13
24
  }
14
25
  type PostgresSslMode = "disable" | "prefer" | "require" | "verify-ca" | "verify-full";
15
26
  interface PostgresOptions {
package/dist/data.js CHANGED
@@ -84,7 +84,10 @@ function connectPostgres(options = {}) {
84
84
  return new Proxy(sql, {
85
85
  get(target, prop, receiver) {
86
86
  if (prop === "unsafe") {
87
- return (query) => run(() => client.unsafe(query));
87
+ return (query, params) => run(() => client.unsafe(query, params));
88
+ }
89
+ if (prop === "begin") {
90
+ return (fn) => run(() => client.begin(async (tx) => fn(tx)));
88
91
  }
89
92
  const value = Reflect.get(target, prop, receiver);
90
93
  return typeof value === "function" ? value.bind(target) : value;
@@ -119,8 +122,10 @@ function runSQLiteMigrations(db, migrationsDir) {
119
122
  continue;
120
123
  }
121
124
  const sql = readFileSync(join(migrationsDir, file), "utf-8");
122
- db.run(sql);
123
- db.run("INSERT INTO _x_migrations (name) VALUES (?1)", [file]);
125
+ db.transaction(() => {
126
+ db.run(sql);
127
+ db.run("INSERT INTO _x_migrations (name) VALUES (?1)", [file]);
128
+ })();
124
129
  console.log(`[x] migration applied: ${file}`);
125
130
  result.applied.push(file);
126
131
  }
@@ -146,8 +151,10 @@ async function runPostgresMigrations(client, migrationsDir) {
146
151
  continue;
147
152
  }
148
153
  const raw = readFileSync(join(migrationsDir, file), "utf-8");
149
- await client.unsafe(raw);
150
- await client.unsafe(`INSERT INTO _x_migrations (name) VALUES ('${file}')`);
154
+ await client.begin(async (tx) => {
155
+ await tx.unsafe(raw);
156
+ await tx.unsafe("INSERT INTO _x_migrations (name) VALUES ($1)", [file]);
157
+ });
151
158
  console.log(`[x] migration applied: ${file}`);
152
159
  result.applied.push(file);
153
160
  }
package/dist/index.d.ts CHANGED
@@ -289,9 +289,9 @@ declare function createRateLimiter(options?: RateLimitOptions): {
289
289
  declare function rateLimitMiddleware(limiter: ReturnType<typeof createRateLimiter>, req: Request, server?: RateLimitServer): Promise<Response | null>;
290
290
  /**
291
291
  * Shared rate-limit store backed by Redis, so limits are enforced across
292
- * multiple server instances. Uses Bun's built-in `bun:redis` (no npm
293
- * dependency) and connects lazily on first use. Pass the result as
294
- * `store` in `createRateLimiter`/`security.rateLimit`.
292
+ * multiple server instances. Connects lazily on first use and keeps retrying
293
+ * after a failed connection attempt. Pass the result as `store` in
294
+ * `createRateLimiter`/`security.rateLimit`.
295
295
  */
296
296
  declare function createRedisRateLimitStore(options?: {
297
297
  url?: string;
package/dist/index.js CHANGED
@@ -611,19 +611,49 @@ function parseYamlLines(raw) {
611
611
  function escapeHtml2(s) {
612
612
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
613
613
  }
614
+ function renderListBlock(block) {
615
+ const lines = block.split("\n");
616
+ const isUl = lines.every((l) => /^\s*[-*]\s+/.test(l));
617
+ const isOl = lines.every((l) => /^\s*\d+\.\s+/.test(l));
618
+ if (!isUl && !isOl) return block;
619
+ const tag = isUl ? "ul" : "ol";
620
+ const items = lines.map((l) => `<li>${l.replace(/^\s*[-*]\s+/, "").replace(/^\s*\d+\.\s+/, "")}</li>`).join("");
621
+ return `<${tag}>${items}</${tag}>`;
622
+ }
623
+ var ALLOWED_LINK_SCHEMES = /* @__PURE__ */ new Set(["http", "https", "mailto"]);
624
+ var LINK_SCHEME_RE = /^([a-z][a-z0-9+.-]*):/i;
625
+ function decodeLinkUrl(url) {
626
+ try {
627
+ return decodeURIComponent(url);
628
+ } catch {
629
+ return url;
630
+ }
631
+ }
632
+ function isSafeLinkUrl(url) {
633
+ const scheme = LINK_SCHEME_RE.exec(decodeLinkUrl(url.trim()))?.[1]?.toLowerCase();
634
+ return scheme === void 0 || ALLOWED_LINK_SCHEMES.has(scheme);
635
+ }
614
636
  function renderMarkdown(md) {
615
637
  const inlineCodes = [];
616
- let html = md.replace(/^### (.+)$/gm, "<h3>$1</h3>").replace(/^## (.+)$/gm, "<h2>$1</h2>").replace(/^# (.+)$/gm, "<h1>$1</h1>").replace(
617
- /`{3}(\w*)\n([\s\S]*?)`{3}/gm,
618
- (_m, _lang, code) => `<pre><code>${escapeHtml2(code.trim())}</code></pre>`
619
- ).replace(/`([^`]+)`/g, (_m, code) => {
638
+ const fences = [];
639
+ let html = escapeHtml2(md).replace(/`{3}(\w*)\n([\s\S]*?)`{3}/gm, (_m, _lang, code) => {
640
+ fences.push(`<pre><code>${code.trim()}</code></pre>`);
641
+ return `__X_FENCE_${fences.length - 1}__`;
642
+ });
643
+ html = html.replace(/^### (.+)$/gm, "<h3>$1</h3>").replace(/^## (.+)$/gm, "<h2>$1</h2>").replace(/^# (.+)$/gm, "<h1>$1</h1>").replace(/`([^`]+)`/g, (_m, code) => {
620
644
  inlineCodes.push(code);
621
645
  return `__X_CODE_${inlineCodes.length - 1}__`;
622
- }).replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>").replace(/\*([^*]+)\*/g, "<em>$1</em>").replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>').replace(/__X_CODE_(\d+)__/g, (_m, i) => `<code>${inlineCodes[Number.parseInt(i)]}</code>`);
646
+ }).replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>").replace(/\*([^*]+)\*/g, "<em>$1</em>").replace(
647
+ /\[([^\]]+)\]\(([^)]+)\)/g,
648
+ (_m, text, href) => isSafeLinkUrl(href) ? `<a href="${href}">${text}</a>` : text
649
+ ).replace(/__X_CODE_(\d+)__/g, (_m, i) => `<code>${inlineCodes[Number.parseInt(i)]}</code>`);
650
+ html = html.replace(/__X_FENCE_(\d+)__/g, (_m, i) => fences[Number.parseInt(i)] ?? "");
623
651
  const blocks = html.split(/\n\n+/);
624
652
  html = blocks.map((b) => {
625
653
  const t = b.trim();
626
654
  if (!t) return "";
655
+ const listBlock = renderListBlock(t);
656
+ if (listBlock !== t) return listBlock;
627
657
  if (t.startsWith("<h") || t.startsWith("<pre") || t.startsWith("<ul") || t.startsWith("<ol"))
628
658
  return t;
629
659
  return `<p>${t}</p>`;
@@ -1426,20 +1456,25 @@ async function rateLimitMiddleware(limiter, req, server) {
1426
1456
  }
1427
1457
  });
1428
1458
  }
1429
- function createRedisRateLimitStore(options = {}) {
1430
- let client = null;
1431
- let connecting = null;
1432
- async function connect() {
1459
+ function createBunRedisClient(options) {
1460
+ return async () => {
1433
1461
  const require2 = import.meta.require;
1434
1462
  const mod = require2("bun:redis");
1435
1463
  const instance = options.url ? new mod.RedisClient({ url: options.url }) : new mod.RedisClient();
1436
1464
  await instance.connect();
1437
1465
  return instance;
1438
- }
1466
+ };
1467
+ }
1468
+ function createRedisRateLimitStore(options = {}) {
1469
+ return createRedisRateLimitStoreFromClient(createBunRedisClient(options));
1470
+ }
1471
+ function createRedisRateLimitStoreFromClient(clientFactory) {
1472
+ let client = null;
1473
+ let connecting = null;
1439
1474
  function getClient() {
1440
1475
  if (client) return Promise.resolve(client);
1441
1476
  if (!connecting) {
1442
- connecting = connect().then(
1477
+ connecting = clientFactory().then(
1443
1478
  (c) => {
1444
1479
  client = c;
1445
1480
  return c;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thexjs/core",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Full-stack React framework core (router, SSR/SSG renderer, islands, server functions) for the x framework, built on Bun.",
5
5
  "keywords": [
6
6
  "react",