@serwist/routing 8.3.0 → 8.4.1

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.cjs CHANGED
@@ -48,19 +48,18 @@ var internal = require('@serwist/core/internal');
48
48
  });
49
49
  }
50
50
  return handler;
51
- } else {
52
- if (process.env.NODE_ENV !== "production") {
53
- internal.assert.isType(handler, "function", {
54
- moduleName: "@serwist/routing",
55
- className: "Route",
56
- funcName: "constructor",
57
- paramName: "handler"
58
- });
59
- }
60
- return {
61
- handle: handler
62
- };
63
51
  }
52
+ if (process.env.NODE_ENV !== "production") {
53
+ internal.assert.isType(handler, "function", {
54
+ moduleName: "@serwist/routing",
55
+ className: "Route",
56
+ funcName: "constructor",
57
+ paramName: "handler"
58
+ });
59
+ }
60
+ return {
61
+ handle: handler
62
+ };
64
63
  };
65
64
 
66
65
  /**
@@ -175,19 +174,19 @@ var internal = require('@serwist/core/internal');
175
174
  for (const regExp of this._denylist){
176
175
  if (regExp.test(pathnameAndSearch)) {
177
176
  if (process.env.NODE_ENV !== "production") {
178
- internal.logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL matches this denylist pattern: ` + `${regExp.toString()}`);
177
+ internal.logger.log(`The navigation route ${pathnameAndSearch} is not being used, since the URL matches this denylist pattern: ${regExp.toString()}`);
179
178
  }
180
179
  return false;
181
180
  }
182
181
  }
183
182
  if (this._allowlist.some((regExp)=>regExp.test(pathnameAndSearch))) {
184
183
  if (process.env.NODE_ENV !== "production") {
185
- internal.logger.debug(`The navigation route ${pathnameAndSearch} ` + `is being used.`);
184
+ internal.logger.debug(`The navigation route ${pathnameAndSearch} is being used.`);
186
185
  }
187
186
  return true;
188
187
  }
189
188
  if (process.env.NODE_ENV !== "production") {
190
- internal.logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL being navigated to doesn't ` + `match the allowlist.`);
189
+ internal.logger.log(`The navigation route ${pathnameAndSearch} is not being used, since the URL being navigated to doesn't match the allowlist.`);
191
190
  }
192
191
  return false;
193
192
  }
@@ -230,7 +229,7 @@ var internal = require('@serwist/core/internal');
230
229
  // behind this behavior.
231
230
  if (url.origin !== location.origin && result.index !== 0) {
232
231
  if (process.env.NODE_ENV !== "production") {
233
- internal.logger.debug(`The regular expression '${regExp.toString()}' only partially matched ` + `against the cross-origin URL '${url.toString()}'. RegExpRoute's will only ` + `handle cross-origin requests if they match the entire URL.`);
232
+ internal.logger.debug(`The regular expression '${regExp.toString()}' only partially matched against the cross-origin URL '${url.toString()}'. RegExpRoute's will only handle cross-origin requests if they match the entire URL.`);
234
233
  }
235
234
  return;
236
235
  }
@@ -317,7 +316,7 @@ var internal = require('@serwist/core/internal');
317
316
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
318
317
  const { payload } = event.data;
319
318
  if (process.env.NODE_ENV !== "production") {
320
- internal.logger.debug(`Caching URLs from the window`, payload.urlsToCache);
319
+ internal.logger.debug("Caching URLs from the window", payload.urlsToCache);
321
320
  }
322
321
  const requestPromises = Promise.all(payload.urlsToCache.map((entry)=>{
323
322
  if (typeof entry === "string") {
@@ -336,7 +335,7 @@ var internal = require('@serwist/core/internal');
336
335
  })); // TypeScript
337
336
  event.waitUntil(requestPromises);
338
337
  // If a MessageChannel was used, reply to the message on success.
339
- if (event.ports && event.ports[0]) {
338
+ if (event.ports?.[0]) {
340
339
  void requestPromises.then(()=>event.ports[0].postMessage(true));
341
340
  }
342
341
  }
@@ -373,12 +372,12 @@ var internal = require('@serwist/core/internal');
373
372
  sameOrigin,
374
373
  url
375
374
  });
376
- let handler = route && route.handler;
375
+ let handler = route?.handler;
377
376
  const debugMessages = [];
378
377
  if (process.env.NODE_ENV !== "production") {
379
378
  if (handler) {
380
379
  debugMessages.push([
381
- `Found a route to handle this request:`,
380
+ "Found a route to handle this request:",
382
381
  route
383
382
  ]);
384
383
  if (params) {
@@ -394,7 +393,7 @@ var internal = require('@serwist/core/internal');
394
393
  const method = request.method;
395
394
  if (!handler && this._defaultHandlerMap.has(method)) {
396
395
  if (process.env.NODE_ENV !== "production") {
397
- debugMessages.push(`Failed to find a matching route. Falling ` + `back to the default handler for ${method}.`);
396
+ debugMessages.push(`Failed to find a matching route. Falling back to the default handler for ${method}.`);
398
397
  }
399
398
  handler = this._defaultHandlerMap.get(method);
400
399
  }
@@ -410,13 +409,13 @@ var internal = require('@serwist/core/internal');
410
409
  // We have a handler, meaning Serwist is going to handle the route.
411
410
  // print the routing details to the console.
412
411
  internal.logger.groupCollapsed(`Router is responding to: ${internal.getFriendlyURL(url)}`);
413
- debugMessages.forEach((msg)=>{
412
+ for (const msg of debugMessages){
414
413
  if (Array.isArray(msg)) {
415
414
  internal.logger.log(...msg);
416
415
  } else {
417
416
  internal.logger.log(msg);
418
417
  }
419
- });
418
+ }
420
419
  internal.logger.groupEnd();
421
420
  }
422
421
  // Wrap in try and catch in case the handle method throws a synchronous
@@ -433,7 +432,7 @@ var internal = require('@serwist/core/internal');
433
432
  responsePromise = Promise.reject(err);
434
433
  }
435
434
  // Get route's catch handler, if it exists
436
- const catchHandler = route && route.catchHandler;
435
+ const catchHandler = route?.catchHandler;
437
436
  if (responsePromise instanceof Promise && (this._catchHandler || catchHandler)) {
438
437
  responsePromise = responsePromise.catch(async (err)=>{
439
438
  // If there's a route catch handler, process that first
@@ -441,8 +440,8 @@ var internal = require('@serwist/core/internal');
441
440
  if (process.env.NODE_ENV !== "production") {
442
441
  // Still include URL here as it will be async from the console group
443
442
  // and may not make sense without the URL
444
- internal.logger.groupCollapsed(`Error thrown when responding to: ` + ` ${internal.getFriendlyURL(url)}. Falling back to route's Catch Handler.`);
445
- internal.logger.error(`Error thrown by:`, route);
443
+ internal.logger.groupCollapsed(`Error thrown when responding to: ${internal.getFriendlyURL(url)}. Falling back to route's Catch Handler.`);
444
+ internal.logger.error("Error thrown by:", route);
446
445
  internal.logger.error(err);
447
446
  internal.logger.groupEnd();
448
447
  }
@@ -463,8 +462,8 @@ var internal = require('@serwist/core/internal');
463
462
  if (process.env.NODE_ENV !== "production") {
464
463
  // Still include URL here as it will be async from the console group
465
464
  // and may not make sense without the URL
466
- internal.logger.groupCollapsed(`Error thrown when responding to: ` + ` ${internal.getFriendlyURL(url)}. Falling back to global Catch Handler.`);
467
- internal.logger.error(`Error thrown by:`, route);
465
+ internal.logger.groupCollapsed(`Error thrown when responding to: ${internal.getFriendlyURL(url)}. Falling back to global Catch Handler.`);
466
+ internal.logger.error("Error thrown by:", route);
468
467
  internal.logger.error(err);
469
468
  internal.logger.groupEnd();
470
469
  }
@@ -504,7 +503,7 @@ var internal = require('@serwist/core/internal');
504
503
  // Warn developers that using an async matchCallback is almost always
505
504
  // not the right thing to do.
506
505
  if (matchResult instanceof Promise) {
507
- internal.logger.warn(`While routing ${internal.getFriendlyURL(url)}, an async ` + `matchCallback function was used. Please convert the ` + `following route to use a synchronous matchCallback function:`, route);
506
+ internal.logger.warn(`While routing ${internal.getFriendlyURL(url)}, an async matchCallback function was used. Please convert the following route to use a synchronous matchCallback function:`, route);
508
507
  }
509
508
  }
510
509
  // See https://github.com/GoogleChrome/workbox/issues/2079
@@ -664,13 +663,13 @@ let defaultRouter;
664
663
  // See https://github.com/pillarjs/path-to-regexp#parameters
665
664
  const wildcards = "[*:?+]";
666
665
  if (new RegExp(`${wildcards}`).exec(valueToCheck)) {
667
- internal.logger.debug(`The '$capture' parameter contains an Express-style wildcard ` + `character (${wildcards}). Strings are now always interpreted as ` + `exact matches; use a RegExp for partial or wildcard matches.`);
666
+ internal.logger.debug(`The '$capture' parameter contains an Express-style wildcard character (${wildcards}). Strings are now always interpreted as exact matches; use a RegExp for partial or wildcard matches.`);
668
667
  }
669
668
  }
670
669
  const matchCallback = ({ url })=>{
671
670
  if (process.env.NODE_ENV !== "production") {
672
671
  if (url.pathname === captureUrl.pathname && url.origin !== captureUrl.origin) {
673
- internal.logger.debug(`${capture} only partially matches the cross-origin URL ` + `${url.toString()}. This route will only handle cross-origin requests ` + `if they match the entire URL.`);
672
+ internal.logger.debug(`${capture} only partially matches the cross-origin URL ${url.toString()}. This route will only handle cross-origin requests if they match the entire URL.`);
674
673
  }
675
674
  }
676
675
  return url.href === captureUrl.href;
package/dist/index.d.cts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { NavigationRouteMatchOptions } from "./NavigationRoute.js";
2
2
  import { NavigationRoute } from "./NavigationRoute.js";
3
3
  import { RegExpRoute } from "./RegExpRoute.js";
4
- import { registerRoute } from "./registerRoute.js";
5
4
  import { Route } from "./Route.js";
6
5
  import { Router } from "./Router.js";
6
+ import { registerRoute } from "./registerRoute.js";
7
7
  import { setCatchHandler } from "./setCatchHandler.js";
8
8
  import { setDefaultHandler } from "./setDefaultHandler.js";
9
9
  import type { HTTPMethod } from "./utils/constants.js";
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { NavigationRouteMatchOptions } from "./NavigationRoute.js";
2
2
  import { NavigationRoute } from "./NavigationRoute.js";
3
3
  import { RegExpRoute } from "./RegExpRoute.js";
4
- import { registerRoute } from "./registerRoute.js";
5
4
  import { Route } from "./Route.js";
6
5
  import { Router } from "./Router.js";
6
+ import { registerRoute } from "./registerRoute.js";
7
7
  import { setCatchHandler } from "./setCatchHandler.js";
8
8
  import { setDefaultHandler } from "./setDefaultHandler.js";
9
9
  import type { HTTPMethod } from "./utils/constants.js";
package/dist/index.js CHANGED
@@ -46,19 +46,18 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
46
46
  });
47
47
  }
48
48
  return handler;
49
- } else {
50
- if (process.env.NODE_ENV !== "production") {
51
- assert.isType(handler, "function", {
52
- moduleName: "@serwist/routing",
53
- className: "Route",
54
- funcName: "constructor",
55
- paramName: "handler"
56
- });
57
- }
58
- return {
59
- handle: handler
60
- };
61
49
  }
50
+ if (process.env.NODE_ENV !== "production") {
51
+ assert.isType(handler, "function", {
52
+ moduleName: "@serwist/routing",
53
+ className: "Route",
54
+ funcName: "constructor",
55
+ paramName: "handler"
56
+ });
57
+ }
58
+ return {
59
+ handle: handler
60
+ };
62
61
  };
63
62
 
64
63
  /**
@@ -173,19 +172,19 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
173
172
  for (const regExp of this._denylist){
174
173
  if (regExp.test(pathnameAndSearch)) {
175
174
  if (process.env.NODE_ENV !== "production") {
176
- logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL matches this denylist pattern: ` + `${regExp.toString()}`);
175
+ logger.log(`The navigation route ${pathnameAndSearch} is not being used, since the URL matches this denylist pattern: ${regExp.toString()}`);
177
176
  }
178
177
  return false;
179
178
  }
180
179
  }
181
180
  if (this._allowlist.some((regExp)=>regExp.test(pathnameAndSearch))) {
182
181
  if (process.env.NODE_ENV !== "production") {
183
- logger.debug(`The navigation route ${pathnameAndSearch} ` + `is being used.`);
182
+ logger.debug(`The navigation route ${pathnameAndSearch} is being used.`);
184
183
  }
185
184
  return true;
186
185
  }
187
186
  if (process.env.NODE_ENV !== "production") {
188
- logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL being navigated to doesn't ` + `match the allowlist.`);
187
+ logger.log(`The navigation route ${pathnameAndSearch} is not being used, since the URL being navigated to doesn't match the allowlist.`);
189
188
  }
190
189
  return false;
191
190
  }
@@ -228,7 +227,7 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
228
227
  // behind this behavior.
229
228
  if (url.origin !== location.origin && result.index !== 0) {
230
229
  if (process.env.NODE_ENV !== "production") {
231
- logger.debug(`The regular expression '${regExp.toString()}' only partially matched ` + `against the cross-origin URL '${url.toString()}'. RegExpRoute's will only ` + `handle cross-origin requests if they match the entire URL.`);
230
+ logger.debug(`The regular expression '${regExp.toString()}' only partially matched against the cross-origin URL '${url.toString()}'. RegExpRoute's will only handle cross-origin requests if they match the entire URL.`);
232
231
  }
233
232
  return;
234
233
  }
@@ -315,7 +314,7 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
315
314
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
316
315
  const { payload } = event.data;
317
316
  if (process.env.NODE_ENV !== "production") {
318
- logger.debug(`Caching URLs from the window`, payload.urlsToCache);
317
+ logger.debug("Caching URLs from the window", payload.urlsToCache);
319
318
  }
320
319
  const requestPromises = Promise.all(payload.urlsToCache.map((entry)=>{
321
320
  if (typeof entry === "string") {
@@ -334,7 +333,7 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
334
333
  })); // TypeScript
335
334
  event.waitUntil(requestPromises);
336
335
  // If a MessageChannel was used, reply to the message on success.
337
- if (event.ports && event.ports[0]) {
336
+ if (event.ports?.[0]) {
338
337
  void requestPromises.then(()=>event.ports[0].postMessage(true));
339
338
  }
340
339
  }
@@ -371,12 +370,12 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
371
370
  sameOrigin,
372
371
  url
373
372
  });
374
- let handler = route && route.handler;
373
+ let handler = route?.handler;
375
374
  const debugMessages = [];
376
375
  if (process.env.NODE_ENV !== "production") {
377
376
  if (handler) {
378
377
  debugMessages.push([
379
- `Found a route to handle this request:`,
378
+ "Found a route to handle this request:",
380
379
  route
381
380
  ]);
382
381
  if (params) {
@@ -392,7 +391,7 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
392
391
  const method = request.method;
393
392
  if (!handler && this._defaultHandlerMap.has(method)) {
394
393
  if (process.env.NODE_ENV !== "production") {
395
- debugMessages.push(`Failed to find a matching route. Falling ` + `back to the default handler for ${method}.`);
394
+ debugMessages.push(`Failed to find a matching route. Falling back to the default handler for ${method}.`);
396
395
  }
397
396
  handler = this._defaultHandlerMap.get(method);
398
397
  }
@@ -408,13 +407,13 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
408
407
  // We have a handler, meaning Serwist is going to handle the route.
409
408
  // print the routing details to the console.
410
409
  logger.groupCollapsed(`Router is responding to: ${getFriendlyURL(url)}`);
411
- debugMessages.forEach((msg)=>{
410
+ for (const msg of debugMessages){
412
411
  if (Array.isArray(msg)) {
413
412
  logger.log(...msg);
414
413
  } else {
415
414
  logger.log(msg);
416
415
  }
417
- });
416
+ }
418
417
  logger.groupEnd();
419
418
  }
420
419
  // Wrap in try and catch in case the handle method throws a synchronous
@@ -431,7 +430,7 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
431
430
  responsePromise = Promise.reject(err);
432
431
  }
433
432
  // Get route's catch handler, if it exists
434
- const catchHandler = route && route.catchHandler;
433
+ const catchHandler = route?.catchHandler;
435
434
  if (responsePromise instanceof Promise && (this._catchHandler || catchHandler)) {
436
435
  responsePromise = responsePromise.catch(async (err)=>{
437
436
  // If there's a route catch handler, process that first
@@ -439,8 +438,8 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
439
438
  if (process.env.NODE_ENV !== "production") {
440
439
  // Still include URL here as it will be async from the console group
441
440
  // and may not make sense without the URL
442
- logger.groupCollapsed(`Error thrown when responding to: ` + ` ${getFriendlyURL(url)}. Falling back to route's Catch Handler.`);
443
- logger.error(`Error thrown by:`, route);
441
+ logger.groupCollapsed(`Error thrown when responding to: ${getFriendlyURL(url)}. Falling back to route's Catch Handler.`);
442
+ logger.error("Error thrown by:", route);
444
443
  logger.error(err);
445
444
  logger.groupEnd();
446
445
  }
@@ -461,8 +460,8 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
461
460
  if (process.env.NODE_ENV !== "production") {
462
461
  // Still include URL here as it will be async from the console group
463
462
  // and may not make sense without the URL
464
- logger.groupCollapsed(`Error thrown when responding to: ` + ` ${getFriendlyURL(url)}. Falling back to global Catch Handler.`);
465
- logger.error(`Error thrown by:`, route);
463
+ logger.groupCollapsed(`Error thrown when responding to: ${getFriendlyURL(url)}. Falling back to global Catch Handler.`);
464
+ logger.error("Error thrown by:", route);
466
465
  logger.error(err);
467
466
  logger.groupEnd();
468
467
  }
@@ -502,7 +501,7 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
502
501
  // Warn developers that using an async matchCallback is almost always
503
502
  // not the right thing to do.
504
503
  if (matchResult instanceof Promise) {
505
- logger.warn(`While routing ${getFriendlyURL(url)}, an async ` + `matchCallback function was used. Please convert the ` + `following route to use a synchronous matchCallback function:`, route);
504
+ logger.warn(`While routing ${getFriendlyURL(url)}, an async matchCallback function was used. Please convert the following route to use a synchronous matchCallback function:`, route);
506
505
  }
507
506
  }
508
507
  // See https://github.com/GoogleChrome/workbox/issues/2079
@@ -662,13 +661,13 @@ let defaultRouter;
662
661
  // See https://github.com/pillarjs/path-to-regexp#parameters
663
662
  const wildcards = "[*:?+]";
664
663
  if (new RegExp(`${wildcards}`).exec(valueToCheck)) {
665
- logger.debug(`The '$capture' parameter contains an Express-style wildcard ` + `character (${wildcards}). Strings are now always interpreted as ` + `exact matches; use a RegExp for partial or wildcard matches.`);
664
+ logger.debug(`The '$capture' parameter contains an Express-style wildcard character (${wildcards}). Strings are now always interpreted as exact matches; use a RegExp for partial or wildcard matches.`);
666
665
  }
667
666
  }
668
667
  const matchCallback = ({ url })=>{
669
668
  if (process.env.NODE_ENV !== "production") {
670
669
  if (url.pathname === captureUrl.pathname && url.origin !== captureUrl.origin) {
671
- logger.debug(`${capture} only partially matches the cross-origin URL ` + `${url.toString()}. This route will only handle cross-origin requests ` + `if they match the entire URL.`);
670
+ logger.debug(`${capture} only partially matches the cross-origin URL ${url.toString()}. This route will only handle cross-origin requests if they match the entire URL.`);
672
671
  }
673
672
  }
674
673
  return url.href === captureUrl.href;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/routing",
3
- "version": "8.3.0",
3
+ "version": "8.4.1",
4
4
  "type": "module",
5
5
  "description": "A service worker helper library to route request URLs to handlers.",
6
6
  "files": [
@@ -19,7 +19,7 @@
19
19
  "license": "MIT",
20
20
  "repository": "serwist/serwist",
21
21
  "bugs": "https://github.com/serwist/serwist/issues",
22
- "homepage": "https://serwist.vercel.app",
22
+ "homepage": "https://serwist.pages.dev",
23
23
  "module": "./dist/index.js",
24
24
  "main": "./dist/index.cjs",
25
25
  "types": "./dist/index.d.ts",
@@ -37,15 +37,16 @@
37
37
  "./package.json": "./package.json"
38
38
  },
39
39
  "dependencies": {
40
- "@serwist/core": "8.3.0"
40
+ "@serwist/core": "8.4.1"
41
41
  },
42
42
  "devDependencies": {
43
43
  "rollup": "4.9.1",
44
- "@serwist/constants": "8.3.0"
44
+ "@serwist/constants": "8.4.1"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
48
- "lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs",
48
+ "dev": "rollup --config rollup.config.js --watch",
49
+ "lint": "biome lint ./src",
49
50
  "typecheck": "tsc"
50
51
  }
51
52
  }