node-wpapi icon indicating copy to clipboard operation
node-wpapi copied to clipboard

Regex error when using WPAPI.discover

Open githubuser441 opened this issue 3 years ago • 3 comments

SyntaxError: Invalid regular expression: /^[a-zA-Z0-9-_]+)$/: Unmatched ')' at new RegExp (<anonymous>) at reduceRouteComponents (C:\project\node_modules\wpapi\lib\route-tree.js:83:3) at Array.reduce (<anonymous>) at reduceRouteTree (C:\project\node_modules\wpapi\lib\route-tree.js:182:18) at C:\project\node_modules\wpapi\lib\util\object-reduce.js:25:20 at Array.reduce (<anonymous>) at module.exports (C:\project\node_modules\wpapi\lib\util\object-reduce.js:24:3) at buildRouteTree (C:\project\node_modules\wpapi\lib\route-tree.js:200:9) at WPAPI.bootstrap (C:\project\node_modules\wpapi\wpapi.js:349:23) at new WPAPI (C:\project\node_modules\wpapi\wpapi.js:88:4) Endpoint detected, proceeding despite error...

I'm using the default route, so I don't understand why it's giving me a error.

githubuser441 avatar Apr 13 '22 13:04 githubuser441

After updating from WP5.9.3 to WP6.0 I experience a similar problem.

SyntaxError: unterminated character class

Error is in reduceRouteComponents (route-gree.js:83)

	// A level's validate method is called to check whether a value being set
	// on the request URL is of the proper type for the location in which it
	// is specified. If a group pattern was found, the validator checks whether
	// the input string exactly matches the group pattern.
	const groupPatternRE = groupPattern === '' ?
		// If groupPattern is an empty string, accept any input without validation
		/.*/ :
		// Otherwise, validate against the group pattern or the component string
		new RegExp( groupPattern ? '^' + groupPattern + '$' : component, 'i' ); # <= line 83

Related to #503 ?

joostdekeijzer avatar Jun 13 '22 08:06 joostdekeijzer

In my case, the code seems to break on the WP WP_REST_Global_Styles_Controller regex:

PHP code:

'/' . $this->rest_base . '/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations'

In above reduceRouteCompontens javascript the groupPattern is [\\/\\s%\\w\\.\\(\\ and the component is (?P<stylesheet>[\\/\\s%\\w\\.\\(\\)\\[\\]\\@_\\-]+).

I guess the groupPattern is unterminated...

joostdekeijzer avatar Jun 13 '22 08:06 joostdekeijzer

Hi everyone, has same issue, using wpapi-node, solve it by patch, and try-catch for now, will wait for new updates,

	let groupPatternRE;
	try {
		groupPatternRE = groupPattern === '' ?
		// If groupPattern is an empty string, accept any input without validation
		/.*/ :
		// Otherwise, validate against the group pattern or the component string
		new RegExp( groupPattern ? '^' + groupPattern + '$' : component, 'i' );
	} catch (e) {
		if (groupPattern !== null && groupPattern.includes('+)?')) {
			groupPatternRE = groupPattern === '' ?
			// If groupPattern is an empty string, accept any input without validation
			/.*/ :
			// Otherwise, validate against the group pattern or the component string
			new RegExp( groupPattern ? '^' + groupPattern.replace('+)?', '+))?') + '$' : component, 'i' );
		}
	}

Not the best solution of course.

braniubojni avatar Jun 29 '22 12:06 braniubojni