@prairielearn/express-list-endpoints 1.0.13 → 1.0.14

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @prairielearn/express-list-endpoints
2
2
 
3
+ ## 1.0.14
4
+
5
+ ### Patch Changes
6
+
7
+ - f571b40: Upgrade all JavaScript dependencies
8
+
3
9
  ## 1.0.13
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ const regExpToReplaceExpressPathRegExpParams = /\(\?:\\?\/?\([^)]+\)\)/;
3
3
  const regexpExpressParamRegexp = /\(\?:\\?\\?\/?\([^)]+\)\)/g;
4
4
  const regexpExpressPathParamRegexp = /(:[^)]+)\([^)]+\)/g;
5
5
  const EXPRESS_ROOT_PATH_REGEXP_VALUE = '/^\\/?(?=\\/|$)/i';
6
- const STACK_ITEM_VALID_NAMES = ['router', 'bound dispatch', 'mounted_app'];
6
+ const STACK_ITEM_VALID_NAMES = new Set(['router', 'bound dispatch', 'mounted_app']);
7
7
  /**
8
8
  * Returns all the verbs detected for the passed route
9
9
  */
@@ -44,7 +44,7 @@ function parseExpressRoute(route, basePath) {
44
44
  const endpoints = paths.map((path) => {
45
45
  const completePath = basePath && path === '/' ? basePath : `${basePath}${path}`;
46
46
  const endpoint = {
47
- path: completePath.replace(regexpExpressPathParamRegexp, '$1'),
47
+ path: completePath.replaceAll(regexpExpressPathParamRegexp, '$1'),
48
48
  methods: getRouteMethods(route),
49
49
  middlewares: getRouteMiddlewares(route),
50
50
  };
@@ -78,7 +78,7 @@ function parseExpressPath(expressPathRegExp, params) {
78
78
  if (!expressPathRegExpExec) {
79
79
  throw new Error('Error parsing express path');
80
80
  }
81
- const parsedPath = expressPathRegExpExec[1].replace(/\\\//g, '/');
81
+ const parsedPath = expressPathRegExpExec[1].replaceAll('\\/', '/');
82
82
  return parsedPath;
83
83
  }
84
84
  function parseEndpoints(app, basePath, endpoints) {
@@ -86,7 +86,7 @@ function parseEndpoints(app, basePath, endpoints) {
86
86
  endpoints = endpoints || [];
87
87
  basePath = basePath || '';
88
88
  if (!stack) {
89
- if (endpoints.length) {
89
+ if (endpoints.length > 0) {
90
90
  endpoints = addEndpoints(endpoints, [
91
91
  {
92
92
  path: basePath,
@@ -129,7 +129,7 @@ function parseStack(stack, basePath, endpoints) {
129
129
  const newEndpoints = parseExpressRoute(stackItem.route, basePath);
130
130
  endpoints = addEndpoints(endpoints, newEndpoints);
131
131
  }
132
- else if (STACK_ITEM_VALID_NAMES.includes(stackItem.name)) {
132
+ else if (STACK_ITEM_VALID_NAMES.has(stackItem.name)) {
133
133
  const isExpressPathRegexp = regExpToParseExpressPathRegExp.test(stackItem.regexp);
134
134
  let newBasePath = basePath;
135
135
  if (isExpressPathRegexp) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,MAAM,8BAA8B,GAClC,iFAAiF,CAAC;AACpF,MAAM,sCAAsC,GAAG,wBAAwB,CAAC;AACxE,MAAM,wBAAwB,GAAG,4BAA4B,CAAC;AAC9D,MAAM,4BAA4B,GAAG,oBAAoB,CAAC;AAE1D,MAAM,8BAA8B,GAAG,mBAAmB,CAAC;AAC3D,MAAM,sBAAsB,GAAG,CAAC,QAAQ,EAAE,gBAAgB,EAAE,aAAa,CAAC,CAAC;AAE3E;;GAEG;AACH,SAAS,eAAe,CAAC,KAAY;IACnC,IAAI,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAEzC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC;IACxD,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,WAAW,EAAE,C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'express';\n\ninterface Route {\n methods: object;\n path: string | string[];\n stack: any[];\n}\n\nexport interface Endpoint {\n path: string;\n methods: string[];\n middlewares: string[];\n}\n\nconst regExpToParseExpressPathRegExp =\n /^\\/\\^\\\\?\\/?(?:(:?[\\w\\\\.-]*(?:\\\\\\/:?[\\w\\\\.-]*)*)|(\\(\\?:\\\\?\\/?\\([^)]+\\)\\)))\\\\\\/.*/;\nconst regExpToReplaceExpressPathRegExpParams = /\\(\\?:\\\\?\\/?\\([^)]+\\)\\)/;\nconst regexpExpressParamRegexp = /\\(\\?:\\\\?\\\\?\\/?\\([^)]+\\)\\)/g;\nconst regexpExpressPathParamRegexp = /(:[^)]+)\\([^)]+\\)/g;\n\nconst EXPRESS_ROOT_PATH_REGEXP_VALUE = '/^\\\\/?(?=\\\\/|$)/i';\nconst STACK_ITEM_VALID_NAMES = ['router', 'bound dispatch', 'mounted_app'];\n\n/**\n * Returns all the verbs detected for the passed route\n */\nfunction getRouteMethods(route: Route) {\n let methods = Object.keys(route.methods);\n\n methods = methods.filter((method) => method !== '_all');\n methods = methods.map((method) => method.toUpperCase());\n\n return methods;\n}\n\n/**\n * Returns the names (or anonymous) of all the middlewares attached to the\n * passed route.\n */\nfunction getRouteMiddlewares(route: Route): string[] {\n return route.stack.map((item) => {\n return item.handle.name || 'anonymous';\n });\n}\n\n/**\n * Returns true if found regexp related with express params.\n */\nfunction hasParams(expressPathRegExp: string): boolean {\n return regexpExpressParamRegexp.test(expressPathRegExp);\n}\n\n/**\n * @param route Express route object to be parsed\n * @param basePath The basePath the route is on\n * @returns Endpoints info\n */\nfunction parseExpressRoute(route: Route, basePath: string): Endpoint[] {\n const paths = [];\n\n if (Array.isArray(route.path)) {\n paths.push(...route.path);\n } else {\n paths.push(route.path);\n }\n\n const endpoints: Endpoint[] = paths.map((path) => {\n const completePath = basePath && path === '/' ? basePath : `${basePath}${path}`;\n\n const endpoint: Endpoint = {\n path: completePath.replace(regexpExpressPathParamRegexp, '$1'),\n methods: getRouteMethods(route),\n middlewares: getRouteMiddlewares(route),\n };\n\n return endpoint;\n });\n\n return endpoints;\n}\n\nfunction parseExpressPath(expressPathRegExp: RegExp, params: any[]): string {\n let parsedRegExp = expressPathRegExp.toString();\n let expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n let paramIndex = 0;\n\n while (hasParams(parsedRegExp)) {\n const paramName = params[paramIndex].name;\n const paramId = `:${paramName}`;\n\n parsedRegExp = parsedRegExp.replace(regExpToReplaceExpressPathRegExpParams, (str) => {\n // Express >= 4.20.0 uses a different RegExp for parameters: it\n // captures the slash as part of the parameter. We need to check\n // for this case and add the slash to the value that will replace\n // the parameter in the path.\n if (str.startsWith('(?:\\\\/')) {\n return `\\\\/${paramId}`;\n }\n\n return paramId;\n });\n\n paramIndex++;\n }\n\n if (parsedRegExp !== expressPathRegExp.toString()) {\n expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n }\n\n // TODO: can we do better than this?\n if (!expressPathRegExpExec) {\n throw new Error('Error parsing express path');\n }\n\n const parsedPath = expressPathRegExpExec[1].replace(/\\\\\\//g, '/');\n\n return parsedPath;\n}\n\nfunction parseEndpoints(\n app: Express | Router | any,\n basePath?: string,\n endpoints?: Endpoint[],\n): Endpoint[] {\n const stack = app.stack || (app._router && app._router.stack);\n\n endpoints = endpoints || [];\n basePath = basePath || '';\n\n if (!stack) {\n if (endpoints.length) {\n endpoints = addEndpoints(endpoints, [\n {\n path: basePath,\n methods: [],\n middlewares: [],\n },\n ]);\n }\n } else {\n endpoints = parseStack(stack, basePath, endpoints);\n }\n\n return endpoints;\n}\n\n/**\n * Ensures the path of the new endpoints isn't yet in the array.\n * If the path is already in the array merges the endpoints with the existing\n * one, if not, it adds them to the array.\n *\n * @param currentEndpoints Array of current endpoints\n * @param endpointsToAdd New endpoints to be added to the array\n * @returns Updated endpoints array\n */\nfunction addEndpoints(currentEndpoints: Endpoint[], endpointsToAdd: Endpoint[]): Endpoint[] {\n endpointsToAdd.forEach((newEndpoint) => {\n const existingEndpoint = currentEndpoints.find(\n (endpoint) => endpoint.path === newEndpoint.path,\n );\n\n if (existingEndpoint !== undefined) {\n const newMethods = newEndpoint.methods.filter(\n (method) => !existingEndpoint.methods.includes(method),\n );\n\n existingEndpoint.methods = existingEndpoint.methods.concat(newMethods);\n } else {\n currentEndpoints.push(newEndpoint);\n }\n });\n\n return currentEndpoints;\n}\n\nfunction parseStack(stack: any[], basePath: string, endpoints: Endpoint[]): Endpoint[] {\n stack.forEach((stackItem) => {\n if (stackItem.route) {\n const newEndpoints = parseExpressRoute(stackItem.route, basePath);\n\n endpoints = addEndpoints(endpoints, newEndpoints);\n } else if (STACK_ITEM_VALID_NAMES.includes(stackItem.name)) {\n const isExpressPathRegexp = regExpToParseExpressPathRegExp.test(stackItem.regexp);\n\n let newBasePath = basePath;\n\n if (isExpressPathRegexp) {\n const parsedPath = parseExpressPath(stackItem.regexp, stackItem.keys);\n\n newBasePath += `/${parsedPath}`;\n } else if (\n !stackItem.path &&\n stackItem.regexp &&\n stackItem.regexp.toString() !== EXPRESS_ROOT_PATH_REGEXP_VALUE\n ) {\n const regExpPath = ` RegExp(${stackItem.regexp}) `;\n\n newBasePath += `/${regExpPath}`;\n }\n\n endpoints = parseEndpoints(stackItem.handle, newBasePath, endpoints);\n }\n });\n\n return endpoints;\n}\n\n/**\n * Returns an array of strings with all the detected endpoints\n * @param app The express/router instance to get the endpoints from\n * @returns The array of endpoints\n */\nfunction expressListEndpoints(app: Express | Router | any): Endpoint[] {\n const endpoints = parseEndpoints(app);\n\n return endpoints;\n}\n\nexport default expressListEndpoints;\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,MAAM,8BAA8B,GAClC,iFAAiF,CAAC;AACpF,MAAM,sCAAsC,GAAG,wBAAwB,CAAC;AACxE,MAAM,wBAAwB,GAAG,4BAA4B,CAAC;AAC9D,MAAM,4BAA4B,GAAG,oBAAoB,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AAK,8BAA8B,EAC9D,CAAC;gBACD,MAAM,UAAU,GAAG,WAAW,SAAS,CAAC,MAAM,IAAI,CAAC;gBAEnD,WAAW,IAAI,IAAI,UAAU,EAAE,CAAC;YAClC,CAAC;YAED,SAAS,GAAG,cAAc,CAAC,SAAS,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC;QACvE,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;GAIG;AACH,SAAS,oBAAoB,CAAC,GAA2B;IACvD,MAAM,SAAS,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;IAEtC,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,eAAe,oBAAoB,CAAC","sourcesContent":["import type { Express, Router } from 'express';\n\ninterface Route {\n methods: object;\n path: string | string[];\n stack: any[];\n}\n\nexport interface Endpoint {\n path: string;\n methods: string[];\n middlewares: string[];\n}\n\nconst regExpToParseExpressPathRegExp =\n /^\\/\\^\\\\?\\/?(?:(:?[\\w\\\\.-]*(?:\\\\\\/:?[\\w\\\\.-]*)*)|(\\(\\?:\\\\?\\/?\\([^)]+\\)\\)))\\\\\\/.*/;\nconst regExpToReplaceExpressPathRegExpParams = /\\(\\?:\\\\?\\/?\\([^)]+\\)\\)/;\nconst regexpExpressParamRegexp = /\\(\\?:\\\\?\\\\?\\/?\\([^)]+\\)\\)/g;\nconst regexpExpressPathParamRegexp = /(:[^)]+)\\([^)]+\\)/g;\n\nconst EXPRESS_ROOT_PATH_REGEXP_VALUE = '/^\\\\/?(?=\\\\/|$)/i';\nconst STACK_ITEM_VALID_NAMES = new Set(['router', 'bound dispatch', 'mounted_app']);\n\n/**\n * Returns all the verbs detected for the passed route\n */\nfunction getRouteMethods(route: Route) {\n let methods = Object.keys(route.methods);\n\n methods = methods.filter((method) => method !== '_all');\n methods = methods.map((method) => method.toUpperCase());\n\n return methods;\n}\n\n/**\n * Returns the names (or anonymous) of all the middlewares attached to the\n * passed route.\n */\nfunction getRouteMiddlewares(route: Route): string[] {\n return route.stack.map((item) => {\n return item.handle.name || 'anonymous';\n });\n}\n\n/**\n * Returns true if found regexp related with express params.\n */\nfunction hasParams(expressPathRegExp: string): boolean {\n return regexpExpressParamRegexp.test(expressPathRegExp);\n}\n\n/**\n * @param route Express route object to be parsed\n * @param basePath The basePath the route is on\n * @returns Endpoints info\n */\nfunction parseExpressRoute(route: Route, basePath: string): Endpoint[] {\n const paths = [];\n\n if (Array.isArray(route.path)) {\n paths.push(...route.path);\n } else {\n paths.push(route.path);\n }\n\n const endpoints: Endpoint[] = paths.map((path) => {\n const completePath = basePath && path === '/' ? basePath : `${basePath}${path}`;\n\n const endpoint: Endpoint = {\n path: completePath.replaceAll(regexpExpressPathParamRegexp, '$1'),\n methods: getRouteMethods(route),\n middlewares: getRouteMiddlewares(route),\n };\n\n return endpoint;\n });\n\n return endpoints;\n}\n\nfunction parseExpressPath(expressPathRegExp: RegExp, params: any[]): string {\n let parsedRegExp = expressPathRegExp.toString();\n let expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n let paramIndex = 0;\n\n while (hasParams(parsedRegExp)) {\n const paramName = params[paramIndex].name;\n const paramId = `:${paramName}`;\n\n parsedRegExp = parsedRegExp.replace(regExpToReplaceExpressPathRegExpParams, (str) => {\n // Express >= 4.20.0 uses a different RegExp for parameters: it\n // captures the slash as part of the parameter. We need to check\n // for this case and add the slash to the value that will replace\n // the parameter in the path.\n if (str.startsWith('(?:\\\\/')) {\n return `\\\\/${paramId}`;\n }\n\n return paramId;\n });\n\n paramIndex++;\n }\n\n if (parsedRegExp !== expressPathRegExp.toString()) {\n expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n }\n\n // TODO: can we do better than this?\n if (!expressPathRegExpExec) {\n throw new Error('Error parsing express path');\n }\n\n const parsedPath = expressPathRegExpExec[1].replaceAll('\\\\/', '/');\n\n return parsedPath;\n}\n\nfunction parseEndpoints(\n app: Express | Router | any,\n basePath?: string,\n endpoints?: Endpoint[],\n): Endpoint[] {\n const stack = app.stack || (app._router && app._router.stack);\n\n endpoints = endpoints || [];\n basePath = basePath || '';\n\n if (!stack) {\n if (endpoints.length > 0) {\n endpoints = addEndpoints(endpoints, [\n {\n path: basePath,\n methods: [],\n middlewares: [],\n },\n ]);\n }\n } else {\n endpoints = parseStack(stack, basePath, endpoints);\n }\n\n return endpoints;\n}\n\n/**\n * Ensures the path of the new endpoints isn't yet in the array.\n * If the path is already in the array merges the endpoints with the existing\n * one, if not, it adds them to the array.\n *\n * @param currentEndpoints Array of current endpoints\n * @param endpointsToAdd New endpoints to be added to the array\n * @returns Updated endpoints array\n */\nfunction addEndpoints(currentEndpoints: Endpoint[], endpointsToAdd: Endpoint[]): Endpoint[] {\n endpointsToAdd.forEach((newEndpoint) => {\n const existingEndpoint = currentEndpoints.find(\n (endpoint) => endpoint.path === newEndpoint.path,\n );\n\n if (existingEndpoint !== undefined) {\n const newMethods = newEndpoint.methods.filter(\n (method) => !existingEndpoint.methods.includes(method),\n );\n\n existingEndpoint.methods = existingEndpoint.methods.concat(newMethods);\n } else {\n currentEndpoints.push(newEndpoint);\n }\n });\n\n return currentEndpoints;\n}\n\nfunction parseStack(stack: any[], basePath: string, endpoints: Endpoint[]): Endpoint[] {\n stack.forEach((stackItem) => {\n if (stackItem.route) {\n const newEndpoints = parseExpressRoute(stackItem.route, basePath);\n\n endpoints = addEndpoints(endpoints, newEndpoints);\n } else if (STACK_ITEM_VALID_NAMES.has(stackItem.name)) {\n const isExpressPathRegexp = regExpToParseExpressPathRegExp.test(stackItem.regexp);\n\n let newBasePath = basePath;\n\n if (isExpressPathRegexp) {\n const parsedPath = parseExpressPath(stackItem.regexp, stackItem.keys);\n\n newBasePath += `/${parsedPath}`;\n } else if (\n !stackItem.path &&\n stackItem.regexp &&\n stackItem.regexp.toString() !== EXPRESS_ROOT_PATH_REGEXP_VALUE\n ) {\n const regExpPath = ` RegExp(${stackItem.regexp}) `;\n\n newBasePath += `/${regExpPath}`;\n }\n\n endpoints = parseEndpoints(stackItem.handle, newBasePath, endpoints);\n }\n });\n\n return endpoints;\n}\n\n/**\n * Returns an array of strings with all the detected endpoints\n * @param app The express/router instance to get the endpoints from\n * @returns The array of endpoints\n */\nfunction expressListEndpoints(app: Express | Router | any): Endpoint[] {\n const endpoints = parseEndpoints(app);\n\n return endpoints;\n}\n\nexport default expressListEndpoints;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prairielearn/express-list-endpoints",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,10 +16,10 @@
16
16
  },
17
17
  "devDependencies": {
18
18
  "@prairielearn/tsconfig": "^0.0.0",
19
- "@types/node": "^22.17.0",
19
+ "@types/node": "^22.18.0",
20
20
  "@vitest/coverage-v8": "^3.2.4",
21
21
  "express": "^4.21.2",
22
- "tsx": "^4.20.3",
22
+ "tsx": "^4.20.5",
23
23
  "typescript": "^5.9.2",
24
24
  "vitest": "^3.2.4"
25
25
  }
package/src/index.ts CHANGED
@@ -19,7 +19,7 @@ const regexpExpressParamRegexp = /\(\?:\\?\\?\/?\([^)]+\)\)/g;
19
19
  const regexpExpressPathParamRegexp = /(:[^)]+)\([^)]+\)/g;
20
20
 
21
21
  const EXPRESS_ROOT_PATH_REGEXP_VALUE = '/^\\/?(?=\\/|$)/i';
22
- const STACK_ITEM_VALID_NAMES = ['router', 'bound dispatch', 'mounted_app'];
22
+ const STACK_ITEM_VALID_NAMES = new Set(['router', 'bound dispatch', 'mounted_app']);
23
23
 
24
24
  /**
25
25
  * Returns all the verbs detected for the passed route
@@ -68,7 +68,7 @@ function parseExpressRoute(route: Route, basePath: string): Endpoint[] {
68
68
  const completePath = basePath && path === '/' ? basePath : `${basePath}${path}`;
69
69
 
70
70
  const endpoint: Endpoint = {
71
- path: completePath.replace(regexpExpressPathParamRegexp, '$1'),
71
+ path: completePath.replaceAll(regexpExpressPathParamRegexp, '$1'),
72
72
  methods: getRouteMethods(route),
73
73
  middlewares: getRouteMiddlewares(route),
74
74
  };
@@ -112,7 +112,7 @@ function parseExpressPath(expressPathRegExp: RegExp, params: any[]): string {
112
112
  throw new Error('Error parsing express path');
113
113
  }
114
114
 
115
- const parsedPath = expressPathRegExpExec[1].replace(/\\\//g, '/');
115
+ const parsedPath = expressPathRegExpExec[1].replaceAll('\\/', '/');
116
116
 
117
117
  return parsedPath;
118
118
  }
@@ -128,7 +128,7 @@ function parseEndpoints(
128
128
  basePath = basePath || '';
129
129
 
130
130
  if (!stack) {
131
- if (endpoints.length) {
131
+ if (endpoints.length > 0) {
132
132
  endpoints = addEndpoints(endpoints, [
133
133
  {
134
134
  path: basePath,
@@ -179,7 +179,7 @@ function parseStack(stack: any[], basePath: string, endpoints: Endpoint[]): Endp
179
179
  const newEndpoints = parseExpressRoute(stackItem.route, basePath);
180
180
 
181
181
  endpoints = addEndpoints(endpoints, newEndpoints);
182
- } else if (STACK_ITEM_VALID_NAMES.includes(stackItem.name)) {
182
+ } else if (STACK_ITEM_VALID_NAMES.has(stackItem.name)) {
183
183
  const isExpressPathRegexp = regExpToParseExpressPathRegExp.test(stackItem.regexp);
184
184
 
185
185
  let newBasePath = basePath;