weifuwu 0.17.22 → 0.17.24

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.js CHANGED
@@ -578,25 +578,38 @@ import chokidar from "chokidar";
578
578
  import { useSyncExternalStore, createContext } from "react";
579
579
  var fallbackT = (key, _params, fallback) => fallback ?? key;
580
580
  var DEFAULT_CTX = { params: {}, query: {}, parsed: {}, prefs: {}, env: {}, t: fallbackT, user: {} };
581
- var _ctx = DEFAULT_CTX;
582
- var _snapshot = { params: _ctx.params, query: _ctx.query, user: _ctx.user, parsed: _ctx.parsed, prefs: _ctx.prefs, env: _ctx.env };
583
- var _listeners = /* @__PURE__ */ new Set();
581
+ var KEY = "__WEIFUWU_CTX";
582
+ function getStore() {
583
+ if (typeof globalThis !== "undefined" && globalThis[KEY]) {
584
+ return globalThis[KEY];
585
+ }
586
+ const s = {
587
+ _ctx: DEFAULT_CTX,
588
+ _snapshot: { params: DEFAULT_CTX.params, query: DEFAULT_CTX.query, user: DEFAULT_CTX.user, parsed: DEFAULT_CTX.parsed, prefs: DEFAULT_CTX.prefs, env: DEFAULT_CTX.env },
589
+ _listeners: /* @__PURE__ */ new Set(),
590
+ _alsGetStore: null
591
+ };
592
+ if (typeof globalThis !== "undefined") {
593
+ globalThis[KEY] = s;
594
+ }
595
+ return s;
596
+ }
597
+ var store = getStore();
584
598
  var subscribe = (cb) => {
585
- _listeners.add(cb);
599
+ store._listeners.add(cb);
586
600
  return () => {
587
- _listeners.delete(cb);
601
+ store._listeners.delete(cb);
588
602
  };
589
603
  };
590
- var getSnapshot = () => _snapshot;
604
+ var getSnapshot = () => store._snapshot;
591
605
  var getServerSnapshot = getSnapshot;
592
- var _alsGetStore = null;
593
- function __registerAls(getStore) {
594
- _alsGetStore = getStore;
606
+ function __registerAls(getStore2) {
607
+ store._alsGetStore = getStore2;
595
608
  }
596
609
  function setCtx(value) {
597
- _ctx = { ..._ctx, ...value };
598
- _snapshot = { params: _ctx.params, query: _ctx.query, user: _ctx.user, parsed: _ctx.parsed, prefs: _ctx.prefs, env: _ctx.env };
599
- _listeners.forEach((fn) => fn());
610
+ store._ctx = { ...store._ctx, ...value };
611
+ store._snapshot = { params: store._ctx.params, query: store._ctx.query, user: store._ctx.user, parsed: store._ctx.parsed, prefs: store._ctx.prefs, env: store._ctx.env };
612
+ store._listeners.forEach((fn) => fn());
600
613
  }
601
614
  function _buildT() {
602
615
  const messages2 = typeof window !== "undefined" ? window.__LOCALE_DATA__ : globalThis.__LOCALE_DATA__;
@@ -611,8 +624,8 @@ function _buildT() {
611
624
  };
612
625
  }
613
626
  function _readCtx() {
614
- const alsStore = _alsGetStore?.();
615
- const base = alsStore ?? _ctx;
627
+ const alsStore = store._alsGetStore?.();
628
+ const base = alsStore ?? store._ctx;
616
629
  const data = typeof window !== "undefined" ? window.__WEIFUWU_CTX : null;
617
630
  return { ...base, ...data, t: _buildT() };
618
631
  }
@@ -1018,7 +1031,9 @@ ${src}`;
1018
1031
  // ── client bundle ─────────────────────────────────────────────────────────
1019
1032
  async buildClientBundle(entryPath, layoutPaths, pagesDir) {
1020
1033
  try {
1034
+ const layoutImports = layoutPaths.map((p) => `import${JSON.stringify(p)};`).join("");
1021
1035
  const code = [
1036
+ layoutImports,
1022
1037
  `import{hydrateRoot}from'react-dom/client';`,
1023
1038
  `import{createElement,useState,useEffect}from'react';`,
1024
1039
  `import{TsxContext}from'weifuwu/react';`,
@@ -3280,7 +3295,7 @@ h2{color:#dc2626}.desc{color:#555}</style>
3280
3295
  <body><h2>${error}</h2>${description ? `<p class="desc">${description}</p>` : ""}</body>
3281
3296
  </html>`, { status: 400, headers: { "Content-Type": "text/html; charset=utf-8" } });
3282
3297
  }
3283
- async function authorizeHandler(req, _ctx2) {
3298
+ async function authorizeHandler(req, _ctx) {
3284
3299
  const url = new URL(req.url);
3285
3300
  const clientId = url.searchParams.get("client_id") || "";
3286
3301
  const redirectUri = url.searchParams.get("redirect_uri") || "";
@@ -4688,23 +4703,23 @@ function buildGraphQLHandler(sql2) {
4688
4703
  });
4689
4704
  return Response.json(result, { status: result.errors ? 400 : 200 });
4690
4705
  });
4691
- r.get("/", async (req, _ctx2) => {
4706
+ r.get("/", async (req, _ctx) => {
4692
4707
  const url = new URL(req.url);
4693
4708
  if (url.searchParams.has("query")) {
4694
- return handleGET(req, _ctx2);
4709
+ return handleGET(req, _ctx);
4695
4710
  }
4696
4711
  return new Response("GraphQL endpoint. Send POST /graphql with { query, variables }", {
4697
4712
  status: 200,
4698
4713
  headers: { "Content-Type": "text/plain" }
4699
4714
  });
4700
4715
  });
4701
- async function handleGET(req, _ctx2) {
4716
+ async function handleGET(req, _ctx) {
4702
4717
  const tables = await sql2`
4703
4718
  SELECT * FROM "_user_tables"
4704
- WHERE tenant_id = ${_ctx2.tenant.id}
4719
+ WHERE tenant_id = ${_ctx.tenant.id}
4705
4720
  ORDER BY created_at ASC
4706
4721
  `;
4707
- const buildCtx = { sql: sql2, tenantId: _ctx2.tenant.id, tables, typeCache: /* @__PURE__ */ new Map() };
4722
+ const buildCtx = { sql: sql2, tenantId: _ctx.tenant.id, tables, typeCache: /* @__PURE__ */ new Map() };
4708
4723
  const schema = new GraphQLSchema({
4709
4724
  query: new GraphQLObjectType({
4710
4725
  name: "Query",
@@ -4724,7 +4739,7 @@ function buildGraphQLHandler(sql2) {
4724
4739
  source: query,
4725
4740
  variableValues: variables,
4726
4741
  operationName: url.searchParams.get("operationName") || void 0,
4727
- contextValue: _ctx2
4742
+ contextValue: _ctx
4728
4743
  });
4729
4744
  return Response.json(result, { status: result.errors ? 400 : 200 });
4730
4745
  }
@@ -6742,7 +6757,7 @@ function createWSHandler2(deps) {
6742
6757
  clients.delete(ws);
6743
6758
  }
6744
6759
  },
6745
- error(ws, _ctx2, _err) {
6760
+ error(ws, _ctx, _err) {
6746
6761
  const client = clients.get(ws);
6747
6762
  if (client) {
6748
6763
  client.abortController?.abort();
@@ -7084,7 +7099,7 @@ ${referrers.length ? `<div class="section"><h2>Referrers</h2><table><thead><tr><
7084
7099
  function analytics(options) {
7085
7100
  const excluded = options?.excluded ?? DEFAULT_EXCLUDED;
7086
7101
  const pg = options?.pg;
7087
- const store = pg ? null : new MemStore();
7102
+ const store2 = pg ? null : new MemStore();
7088
7103
  const middleware = () => {
7089
7104
  const m = async (req, ctx, next) => {
7090
7105
  const path2 = new URL(req.url).pathname;
@@ -7097,7 +7112,7 @@ function analytics(options) {
7097
7112
  } else {
7098
7113
  const ref = req.headers.get("referer") || "";
7099
7114
  const refDomain = ref ? new URL(ref).hostname.replace(/^www\./, "") : "";
7100
- store.record(path2, date, refDomain, mobile);
7115
+ store2.record(path2, date, refDomain, mobile);
7101
7116
  }
7102
7117
  return next(req, ctx);
7103
7118
  };
@@ -7106,7 +7121,7 @@ function analytics(options) {
7106
7121
  const handler = async (req) => {
7107
7122
  const url = new URL(req.url);
7108
7123
  const days = Math.min(Math.max(Number(url.searchParams.get("days")) || 7, 1), 365);
7109
- const data = pg ? await queryPg(pg.sql, days) : store.query(days);
7124
+ const data = pg ? await queryPg(pg.sql, days) : store2.query(days);
7110
7125
  if (url.pathname === "/__analytics/data") return Response.json(data);
7111
7126
  return new Response(renderDashboard(days, data), {
7112
7127
  headers: { "content-type": "text/html; charset=utf-8" }
@@ -7698,33 +7713,33 @@ function applyOps(value, ops) {
7698
7713
  return current;
7699
7714
  }
7700
7715
  function createMemoryStore(channels) {
7701
- const store = /* @__PURE__ */ new Map();
7716
+ const store2 = /* @__PURE__ */ new Map();
7702
7717
  function key(stream, group, item) {
7703
7718
  return `${stream}:${group}:${item}`;
7704
7719
  }
7705
7720
  return {
7706
7721
  async set(stream, group, item, data) {
7707
7722
  const k = key(stream, group, item);
7708
- const old = store.get(k) ?? null;
7709
- store.set(k, deepClone(data));
7723
+ const old = store2.get(k) ?? null;
7724
+ store2.set(k, deepClone(data));
7710
7725
  notify(channels, stream, group, item, "set", data);
7711
7726
  return { old_value: old, new_value: deepClone(data) };
7712
7727
  },
7713
7728
  async get(stream, group, item) {
7714
- const v = store.get(key(stream, group, item)) ?? null;
7729
+ const v = store2.get(key(stream, group, item)) ?? null;
7715
7730
  return { value: deepClone(v) };
7716
7731
  },
7717
7732
  async delete(stream, group, item) {
7718
7733
  const k = key(stream, group, item);
7719
- const old = store.get(k) ?? null;
7720
- store.delete(k);
7734
+ const old = store2.get(k) ?? null;
7735
+ store2.delete(k);
7721
7736
  notify(channels, stream, group, item, "delete", null);
7722
7737
  return { old_value: old };
7723
7738
  },
7724
7739
  async list(stream, group) {
7725
7740
  const items = [];
7726
7741
  const prefix = `${stream}:${group}:`;
7727
- for (const [k, v] of store) {
7742
+ for (const [k, v] of store2) {
7728
7743
  if (k.startsWith(prefix) && !k.slice(prefix.length).includes(":")) {
7729
7744
  items.push({ item_id: k.slice(prefix.length), data: deepClone(v) });
7730
7745
  }
@@ -7734,7 +7749,7 @@ function createMemoryStore(channels) {
7734
7749
  async list_groups(stream) {
7735
7750
  const groups = /* @__PURE__ */ new Set();
7736
7751
  const prefix = `${stream}:`;
7737
- for (const k of store.keys()) {
7752
+ for (const k of store2.keys()) {
7738
7753
  if (k.startsWith(prefix)) {
7739
7754
  const rest = k.slice(prefix.length);
7740
7755
  const g = rest.split(":")[0];
@@ -7745,7 +7760,7 @@ function createMemoryStore(channels) {
7745
7760
  },
7746
7761
  async list_all() {
7747
7762
  const streamMap = /* @__PURE__ */ new Map();
7748
- for (const k of store.keys()) {
7763
+ for (const k of store2.keys()) {
7749
7764
  const parts = k.split(":");
7750
7765
  const s = parts[0];
7751
7766
  const g = parts[1];
@@ -7766,9 +7781,9 @@ function createMemoryStore(channels) {
7766
7781
  },
7767
7782
  async update(stream, group, item, ops) {
7768
7783
  const k = key(stream, group, item);
7769
- const old = deepClone(store.get(k) ?? null);
7784
+ const old = deepClone(store2.get(k) ?? null);
7770
7785
  const newVal = applyOps(old, ops);
7771
- store.set(k, deepClone(newVal));
7786
+ store2.set(k, deepClone(newVal));
7772
7787
  notify(channels, stream, group, item, "update", newVal);
7773
7788
  return { old_value: old, new_value: deepClone(newVal) };
7774
7789
  }
@@ -7957,7 +7972,7 @@ function createRedisStore(channels, redis2, ttl) {
7957
7972
  }
7958
7973
  function createStream(opts) {
7959
7974
  const channels = /* @__PURE__ */ new Map();
7960
- const store = opts?.pg ? createPgStore(channels, opts.pg) : opts?.redis ? createRedisStore(channels, opts.redis, opts.streamTTL ?? 3600) : createMemoryStore(channels);
7975
+ const store2 = opts?.pg ? createPgStore(channels, opts.pg) : opts?.redis ? createRedisStore(channels, opts.redis, opts.streamTTL ?? 3600) : createMemoryStore(channels);
7961
7976
  let redisSub = null;
7962
7977
  if (opts?.redis) {
7963
7978
  redisSub = opts.redis.duplicate();
@@ -7976,7 +7991,7 @@ function createStream(opts) {
7976
7991
  });
7977
7992
  }
7978
7993
  return {
7979
- ...store,
7994
+ ...store2,
7980
7995
  subscribe(ws, sub) {
7981
7996
  const key = sub.item_id ? `${sub.stream_name}:${sub.group_id}:${sub.item_id}` : sub.group_id ? `${sub.stream_name}:${sub.group_id}` : sub.stream_name;
7982
7997
  if (!channels.has(key)) channels.set(key, /* @__PURE__ */ new Set());
@@ -8017,7 +8032,7 @@ function createWsHandler(deps) {
8017
8032
  return wsToWorkerId.get(ws) || "";
8018
8033
  }
8019
8034
  return {
8020
- open(_ws, _ctx2) {
8035
+ open(_ws, _ctx) {
8021
8036
  },
8022
8037
  async message(ws, ctx, data) {
8023
8038
  let msg;
package/dist/react.js CHANGED
@@ -334,22 +334,35 @@ async function prefetchPage(href) {
334
334
  import { useSyncExternalStore, createContext } from "react";
335
335
  var fallbackT = (key, _params, fallback) => fallback ?? key;
336
336
  var DEFAULT_CTX = { params: {}, query: {}, parsed: {}, prefs: {}, env: {}, t: fallbackT, user: {} };
337
- var _ctx = DEFAULT_CTX;
338
- var _snapshot = { params: _ctx.params, query: _ctx.query, user: _ctx.user, parsed: _ctx.parsed, prefs: _ctx.prefs, env: _ctx.env };
339
- var _listeners2 = /* @__PURE__ */ new Set();
337
+ var KEY = "__WEIFUWU_CTX";
338
+ function getStore() {
339
+ if (typeof globalThis !== "undefined" && globalThis[KEY]) {
340
+ return globalThis[KEY];
341
+ }
342
+ const s = {
343
+ _ctx: DEFAULT_CTX,
344
+ _snapshot: { params: DEFAULT_CTX.params, query: DEFAULT_CTX.query, user: DEFAULT_CTX.user, parsed: DEFAULT_CTX.parsed, prefs: DEFAULT_CTX.prefs, env: DEFAULT_CTX.env },
345
+ _listeners: /* @__PURE__ */ new Set(),
346
+ _alsGetStore: null
347
+ };
348
+ if (typeof globalThis !== "undefined") {
349
+ globalThis[KEY] = s;
350
+ }
351
+ return s;
352
+ }
353
+ var store = getStore();
340
354
  var subscribe = (cb) => {
341
- _listeners2.add(cb);
355
+ store._listeners.add(cb);
342
356
  return () => {
343
- _listeners2.delete(cb);
357
+ store._listeners.delete(cb);
344
358
  };
345
359
  };
346
- var getSnapshot = () => _snapshot;
360
+ var getSnapshot = () => store._snapshot;
347
361
  var getServerSnapshot = getSnapshot;
348
- var _alsGetStore = null;
349
362
  function setCtx(value) {
350
- _ctx = { ..._ctx, ...value };
351
- _snapshot = { params: _ctx.params, query: _ctx.query, user: _ctx.user, parsed: _ctx.parsed, prefs: _ctx.prefs, env: _ctx.env };
352
- _listeners2.forEach((fn) => fn());
363
+ store._ctx = { ...store._ctx, ...value };
364
+ store._snapshot = { params: store._ctx.params, query: store._ctx.query, user: store._ctx.user, parsed: store._ctx.parsed, prefs: store._ctx.prefs, env: store._ctx.env };
365
+ store._listeners.forEach((fn) => fn());
353
366
  }
354
367
  function _buildT() {
355
368
  const messages = typeof window !== "undefined" ? window.__LOCALE_DATA__ : globalThis.__LOCALE_DATA__;
@@ -364,8 +377,8 @@ function _buildT() {
364
377
  };
365
378
  }
366
379
  function _readCtx() {
367
- const alsStore = _alsGetStore?.();
368
- const base = alsStore ?? _ctx;
380
+ const alsStore = store._alsGetStore?.();
381
+ const base = alsStore ?? store._ctx;
369
382
  const data = typeof window !== "undefined" ? window.__WEIFUWU_CTX : null;
370
383
  return { ...base, ...data, t: _buildT() };
371
384
  }
@@ -544,7 +557,8 @@ function useLocale() {
544
557
 
545
558
  // client-theme.ts
546
559
  function resolveTheme(theme) {
547
- if (theme === "system" && typeof window !== "undefined") {
560
+ if (theme === "system") {
561
+ if (typeof window === "undefined") return "light";
548
562
  return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
549
563
  }
550
564
  return theme;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "weifuwu",
3
- "version": "0.17.22",
3
+ "version": "0.17.24",
4
4
  "description": "Web-standard HTTP framework for Node.js — (req, ctx) => Response",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",