weifuwu 0.17.23 → 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
  }
@@ -3282,7 +3295,7 @@ h2{color:#dc2626}.desc{color:#555}</style>
3282
3295
  <body><h2>${error}</h2>${description ? `<p class="desc">${description}</p>` : ""}</body>
3283
3296
  </html>`, { status: 400, headers: { "Content-Type": "text/html; charset=utf-8" } });
3284
3297
  }
3285
- async function authorizeHandler(req, _ctx2) {
3298
+ async function authorizeHandler(req, _ctx) {
3286
3299
  const url = new URL(req.url);
3287
3300
  const clientId = url.searchParams.get("client_id") || "";
3288
3301
  const redirectUri = url.searchParams.get("redirect_uri") || "";
@@ -4690,23 +4703,23 @@ function buildGraphQLHandler(sql2) {
4690
4703
  });
4691
4704
  return Response.json(result, { status: result.errors ? 400 : 200 });
4692
4705
  });
4693
- r.get("/", async (req, _ctx2) => {
4706
+ r.get("/", async (req, _ctx) => {
4694
4707
  const url = new URL(req.url);
4695
4708
  if (url.searchParams.has("query")) {
4696
- return handleGET(req, _ctx2);
4709
+ return handleGET(req, _ctx);
4697
4710
  }
4698
4711
  return new Response("GraphQL endpoint. Send POST /graphql with { query, variables }", {
4699
4712
  status: 200,
4700
4713
  headers: { "Content-Type": "text/plain" }
4701
4714
  });
4702
4715
  });
4703
- async function handleGET(req, _ctx2) {
4716
+ async function handleGET(req, _ctx) {
4704
4717
  const tables = await sql2`
4705
4718
  SELECT * FROM "_user_tables"
4706
- WHERE tenant_id = ${_ctx2.tenant.id}
4719
+ WHERE tenant_id = ${_ctx.tenant.id}
4707
4720
  ORDER BY created_at ASC
4708
4721
  `;
4709
- 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() };
4710
4723
  const schema = new GraphQLSchema({
4711
4724
  query: new GraphQLObjectType({
4712
4725
  name: "Query",
@@ -4726,7 +4739,7 @@ function buildGraphQLHandler(sql2) {
4726
4739
  source: query,
4727
4740
  variableValues: variables,
4728
4741
  operationName: url.searchParams.get("operationName") || void 0,
4729
- contextValue: _ctx2
4742
+ contextValue: _ctx
4730
4743
  });
4731
4744
  return Response.json(result, { status: result.errors ? 400 : 200 });
4732
4745
  }
@@ -6744,7 +6757,7 @@ function createWSHandler2(deps) {
6744
6757
  clients.delete(ws);
6745
6758
  }
6746
6759
  },
6747
- error(ws, _ctx2, _err) {
6760
+ error(ws, _ctx, _err) {
6748
6761
  const client = clients.get(ws);
6749
6762
  if (client) {
6750
6763
  client.abortController?.abort();
@@ -7086,7 +7099,7 @@ ${referrers.length ? `<div class="section"><h2>Referrers</h2><table><thead><tr><
7086
7099
  function analytics(options) {
7087
7100
  const excluded = options?.excluded ?? DEFAULT_EXCLUDED;
7088
7101
  const pg = options?.pg;
7089
- const store = pg ? null : new MemStore();
7102
+ const store2 = pg ? null : new MemStore();
7090
7103
  const middleware = () => {
7091
7104
  const m = async (req, ctx, next) => {
7092
7105
  const path2 = new URL(req.url).pathname;
@@ -7099,7 +7112,7 @@ function analytics(options) {
7099
7112
  } else {
7100
7113
  const ref = req.headers.get("referer") || "";
7101
7114
  const refDomain = ref ? new URL(ref).hostname.replace(/^www\./, "") : "";
7102
- store.record(path2, date, refDomain, mobile);
7115
+ store2.record(path2, date, refDomain, mobile);
7103
7116
  }
7104
7117
  return next(req, ctx);
7105
7118
  };
@@ -7108,7 +7121,7 @@ function analytics(options) {
7108
7121
  const handler = async (req) => {
7109
7122
  const url = new URL(req.url);
7110
7123
  const days = Math.min(Math.max(Number(url.searchParams.get("days")) || 7, 1), 365);
7111
- const data = pg ? await queryPg(pg.sql, days) : store.query(days);
7124
+ const data = pg ? await queryPg(pg.sql, days) : store2.query(days);
7112
7125
  if (url.pathname === "/__analytics/data") return Response.json(data);
7113
7126
  return new Response(renderDashboard(days, data), {
7114
7127
  headers: { "content-type": "text/html; charset=utf-8" }
@@ -7700,33 +7713,33 @@ function applyOps(value, ops) {
7700
7713
  return current;
7701
7714
  }
7702
7715
  function createMemoryStore(channels) {
7703
- const store = /* @__PURE__ */ new Map();
7716
+ const store2 = /* @__PURE__ */ new Map();
7704
7717
  function key(stream, group, item) {
7705
7718
  return `${stream}:${group}:${item}`;
7706
7719
  }
7707
7720
  return {
7708
7721
  async set(stream, group, item, data) {
7709
7722
  const k = key(stream, group, item);
7710
- const old = store.get(k) ?? null;
7711
- store.set(k, deepClone(data));
7723
+ const old = store2.get(k) ?? null;
7724
+ store2.set(k, deepClone(data));
7712
7725
  notify(channels, stream, group, item, "set", data);
7713
7726
  return { old_value: old, new_value: deepClone(data) };
7714
7727
  },
7715
7728
  async get(stream, group, item) {
7716
- const v = store.get(key(stream, group, item)) ?? null;
7729
+ const v = store2.get(key(stream, group, item)) ?? null;
7717
7730
  return { value: deepClone(v) };
7718
7731
  },
7719
7732
  async delete(stream, group, item) {
7720
7733
  const k = key(stream, group, item);
7721
- const old = store.get(k) ?? null;
7722
- store.delete(k);
7734
+ const old = store2.get(k) ?? null;
7735
+ store2.delete(k);
7723
7736
  notify(channels, stream, group, item, "delete", null);
7724
7737
  return { old_value: old };
7725
7738
  },
7726
7739
  async list(stream, group) {
7727
7740
  const items = [];
7728
7741
  const prefix = `${stream}:${group}:`;
7729
- for (const [k, v] of store) {
7742
+ for (const [k, v] of store2) {
7730
7743
  if (k.startsWith(prefix) && !k.slice(prefix.length).includes(":")) {
7731
7744
  items.push({ item_id: k.slice(prefix.length), data: deepClone(v) });
7732
7745
  }
@@ -7736,7 +7749,7 @@ function createMemoryStore(channels) {
7736
7749
  async list_groups(stream) {
7737
7750
  const groups = /* @__PURE__ */ new Set();
7738
7751
  const prefix = `${stream}:`;
7739
- for (const k of store.keys()) {
7752
+ for (const k of store2.keys()) {
7740
7753
  if (k.startsWith(prefix)) {
7741
7754
  const rest = k.slice(prefix.length);
7742
7755
  const g = rest.split(":")[0];
@@ -7747,7 +7760,7 @@ function createMemoryStore(channels) {
7747
7760
  },
7748
7761
  async list_all() {
7749
7762
  const streamMap = /* @__PURE__ */ new Map();
7750
- for (const k of store.keys()) {
7763
+ for (const k of store2.keys()) {
7751
7764
  const parts = k.split(":");
7752
7765
  const s = parts[0];
7753
7766
  const g = parts[1];
@@ -7768,9 +7781,9 @@ function createMemoryStore(channels) {
7768
7781
  },
7769
7782
  async update(stream, group, item, ops) {
7770
7783
  const k = key(stream, group, item);
7771
- const old = deepClone(store.get(k) ?? null);
7784
+ const old = deepClone(store2.get(k) ?? null);
7772
7785
  const newVal = applyOps(old, ops);
7773
- store.set(k, deepClone(newVal));
7786
+ store2.set(k, deepClone(newVal));
7774
7787
  notify(channels, stream, group, item, "update", newVal);
7775
7788
  return { old_value: old, new_value: deepClone(newVal) };
7776
7789
  }
@@ -7959,7 +7972,7 @@ function createRedisStore(channels, redis2, ttl) {
7959
7972
  }
7960
7973
  function createStream(opts) {
7961
7974
  const channels = /* @__PURE__ */ new Map();
7962
- 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);
7963
7976
  let redisSub = null;
7964
7977
  if (opts?.redis) {
7965
7978
  redisSub = opts.redis.duplicate();
@@ -7978,7 +7991,7 @@ function createStream(opts) {
7978
7991
  });
7979
7992
  }
7980
7993
  return {
7981
- ...store,
7994
+ ...store2,
7982
7995
  subscribe(ws, sub) {
7983
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;
7984
7997
  if (!channels.has(key)) channels.set(key, /* @__PURE__ */ new Set());
@@ -8019,7 +8032,7 @@ function createWsHandler(deps) {
8019
8032
  return wsToWorkerId.get(ws) || "";
8020
8033
  }
8021
8034
  return {
8022
- open(_ws, _ctx2) {
8035
+ open(_ws, _ctx) {
8023
8036
  },
8024
8037
  async message(ws, ctx, data) {
8025
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.23",
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",