@yangzhenchen/dsh-balance 0.4.6 → 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.
Files changed (2) hide show
  1. package/lib/index.js +21 -2
  2. package/package.json +1 -1
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.6",
3
+ "version": "0.4.7",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },