@yangzhenchen/dsh-balance 0.4.5 → 0.4.7

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/lib/client.js CHANGED
@@ -784,6 +784,18 @@ window.__ModuleLoader__.load({
784
784
  };
785
785
  }
786
786
  /**
787
+ * Read the account state out of a `watch` frame. DSH 0.2.2-rc.3 wraps the
788
+ * update as `{ type, state }`; earlier releases sent the state itself.
789
+ * @param frame - one frame from `remote.account.watch`.
790
+ * @returns `signed-out`, `credential-stored`, or undefined.
791
+ */
792
+ function accountStatus(frame) {
793
+ const update = frame === null || typeof frame !== "object" ? void 0 : frame.value;
794
+ if (update === null || typeof update !== "object") return void 0;
795
+ const state = update.state !== null && typeof update.state === "object" ? update.state : update;
796
+ return typeof state.status === "string" ? state.status : void 0;
797
+ }
798
+ /**
787
799
  * The bottom-pinned balance row.
788
800
  * @param props - slot props plus this plugin's context and translator.
789
801
  * @returns the row, or nothing on the collapsed rail.
@@ -865,7 +877,7 @@ window.__ModuleLoader__.load({
865
877
  try {
866
878
  for await (const frame of stream) {
867
879
  if (disposed) break;
868
- const status = frame?.value?.status;
880
+ const status = accountStatus(frame);
869
881
  setSignedIn(status === "credential-stored");
870
882
  if (status === "credential-stored") read();
871
883
  try {
@@ -1620,6 +1632,7 @@ window.__ModuleLoader__.load({
1620
1632
  formatCountdown,
1621
1633
  switchClock,
1622
1634
  tariffHeadline,
1635
+ accountStatus,
1623
1636
  UsagePanel
1624
1637
  };
1625
1638
  return module.exports;
package/lib/index.js CHANGED
@@ -330,13 +330,32 @@ export function apply(ctx) {
330
330
  return payload;
331
331
  };
332
332
 
333
+ /**
334
+ * Decide whether this request may read usage.
335
+ * Newer DSH admits the route before the handler and passes the admitted
336
+ * Peer; older builds expose `connection.requestRejection`.
337
+ * @param req - HTTP request.
338
+ * @param peer - admitted Peer from the WebServer, when available.
339
+ * @returns undefined when allowed, otherwise the status to answer.
340
+ */
341
+ const rejectionFor = (req, peer) => {
342
+ if (peer !== undefined && peer !== null) {
343
+ const principal = peer.principal;
344
+ return principal === undefined || principal === "owner" ? undefined : 403;
345
+ }
346
+ const check = cap.connection?.requestRejection;
347
+ if (typeof check !== "function") return 401;
348
+ return check(req);
349
+ };
350
+
333
351
  /**
334
352
  * Serve the usage summary for one browser request.
335
353
  * @param req - HTTP request.
336
354
  * @param res - HTTP response.
355
+ * @param peer - admitted Peer (DSH 0.2.2-rc.2 and newer).
337
356
  */
338
- const handler = async (req, res) => {
339
- const rejection = cap.connection.requestRejection(req);
357
+ const handler = async (req, res, peer) => {
358
+ const rejection = rejectionFor(req, peer);
340
359
  if (rejection !== undefined) {
341
360
  res.statusCode = rejection;
342
361
  res.end();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yangzhenchen/dsh-balance",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },