webext-patterns 1.3.0 → 1.4.0

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 (4) hide show
  1. package/index.d.ts +3 -0
  2. package/index.js +38 -13
  3. package/package.json +13 -10
  4. package/readme.md +41 -1
package/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export declare const patternValidationRegex: RegExp;
2
2
  export declare const allStarsRegex: RegExp;
3
3
  export declare const allUrlsRegex: RegExp;
4
+ export declare function assertValidPattern(matchPattern: string): void;
5
+ export declare function isValidPattern(matchPattern: string): boolean;
6
+ export declare function doesUrlMatchPatterns(url: string, ...matchPattern: string[]): boolean;
4
7
  export declare function patternToRegex(...matchPatterns: readonly string[]): RegExp;
5
8
  export declare function globToRegex(...globs: readonly string[]): RegExp;
6
9
  export declare function excludeDuplicatePatterns(matchPatterns: readonly string[]): string[];
package/index.js CHANGED
@@ -1,28 +1,53 @@
1
1
  import escapeStringRegexp from 'escape-string-regexp';
2
2
  // Copied from https://github.com/mozilla/gecko-dev/blob/073cc24f53d0cf31403121d768812146e597cc9d/toolkit/components/extensions/schemas/manifest.json#L487-L491
3
3
  export const patternValidationRegex = /^(https?|wss?|file|ftp|\*):\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^file:\/\/\/.*$|^resource:\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^about:/;
4
- const isFirefox = typeof navigator === 'object' && navigator.userAgent.includes('Firefox/');
4
+ const isFirefox = globalThis.navigator?.userAgent.includes('Firefox/');
5
5
  export const allStarsRegex = isFirefox
6
6
  ? /^(https?|wss?):[/][/][^/]+([/].*)?$/
7
7
  : /^https?:[/][/][^/]+([/].*)?$/;
8
8
  export const allUrlsRegex = /^(https?|file|ftp):[/]+/;
9
- function getRawPatternRegex(matchPattern) {
10
- if (!patternValidationRegex.test(matchPattern)) {
9
+ export function assertValidPattern(matchPattern) {
10
+ if (!isValidPattern(matchPattern)) {
11
11
  throw new Error(matchPattern + ' is an invalid pattern, it must match ' + String(patternValidationRegex));
12
12
  }
13
- let [, protocol, host, pathname] = matchPattern.split(/(^[^:]+:[/][/])([^/]+)?/);
13
+ }
14
+ export function isValidPattern(matchPattern) {
15
+ return matchPattern === '<all_urls>' || patternValidationRegex.test(matchPattern);
16
+ }
17
+ export function doesUrlMatchPatterns(url, ...matchPattern) {
18
+ if (matchPattern.includes('<all_urls>') && allUrlsRegex.test(url)) {
19
+ return true;
20
+ }
21
+ if (matchPattern.includes('*://*/*') && allStarsRegex.test(url)) {
22
+ return true;
23
+ }
24
+ for (const pattern of matchPattern) {
25
+ if (patternToRegex(pattern).test(url)) {
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ }
31
+ function getRawPatternRegex(matchPattern) {
32
+ assertValidPattern(matchPattern);
33
+ // Host undefined for file:///
34
+ let [, protocol, host = '', pathname] = matchPattern.split(/(^[^:]+:[/][/])([^/]+)?/);
14
35
  protocol = protocol
15
36
  .replace('*', isFirefox ? '(https?|wss?)' : 'https?') // Protocol wildcard
16
- .replace(/[/]/g, '[/]'); // Escape slashes
17
- host = (host !== null && host !== void 0 ? host : '') // Undefined for file:///
18
- .replace(/^[*][.]/, '([^/]+.)*') // Initial wildcard
19
- .replace(/^[*]$/, '[^/]+') // Only wildcard
20
- .replace(/[.]/g, '[.]') // Escape dots
21
- .replace(/[*]$/g, '[^.]+'); // Last wildcard
37
+ .replaceAll(/[/]/g, '[/]'); // Escape slashes
38
+ if (host === '*') {
39
+ host = '[^/]+';
40
+ }
41
+ else if (host) {
42
+ host = host
43
+ .replace(/^[*][.]/, '([^/]+.)*') // Initial wildcard
44
+ .replaceAll(/[.]/g, '[.]') // Escape dots
45
+ .replace(/[*]$/, '[^.]+'); // Last wildcard
46
+ }
22
47
  pathname = pathname
23
- .replace(/[/]/g, '[/]') // Escape slashes
24
- .replace(/[.]/g, '[.]') // Escape dots
25
- .replace(/[*]/g, '.*'); // Any wildcard
48
+ .replaceAll(/[/]/g, '[/]') // Escape slashes
49
+ .replaceAll(/[.]/g, '[.]') // Escape dots
50
+ .replaceAll(/[*]/g, '.*'); // Any wildcard
26
51
  return '^' + protocol + host + '(' + pathname + ')?$';
27
52
  }
28
53
  export function patternToRegex(...matchPatterns) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-patterns",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Tool to convert the patterns and globs of your WebExtension manifest to regex",
5
5
  "keywords": [
6
6
  "browser",
@@ -21,8 +21,8 @@
21
21
  "license": "MIT",
22
22
  "author": "Federico Brigante <me@fregante.com> (https://fregante.com)",
23
23
  "type": "module",
24
- "main": "index.js",
25
- "module": "index.js",
24
+ "exports": "./index.js",
25
+ "types": "./index.d.ts",
26
26
  "files": [
27
27
  "index.js",
28
28
  "index.d.ts"
@@ -48,13 +48,16 @@
48
48
  "escape-string-regexp": "^5.0.0"
49
49
  },
50
50
  "devDependencies": {
51
- "@sindresorhus/tsconfig": "^3.0.1",
52
- "@types/chrome": "0.0.210",
53
- "ava": "^5.1.1",
54
- "sinon": "^15.0.1",
55
- "type-fest": "^3.5.3",
56
- "typescript": "^4.9.4",
57
- "xo": "^0.53.1"
51
+ "@sindresorhus/tsconfig": "^5.0.0",
52
+ "@types/chrome": "0.0.256",
53
+ "ava": "^6.0.1",
54
+ "sinon": "^17.0.1",
55
+ "type-fest": "^4.9.0",
56
+ "typescript": "^5.3.3",
57
+ "xo": "^0.56.0"
58
+ },
59
+ "engines": {
60
+ "node": ">=18"
58
61
  },
59
62
  "webExt": {
60
63
  "sourceDir": "demo-extension",
package/readme.md CHANGED
@@ -19,7 +19,14 @@ npm install webext-patterns
19
19
 
20
20
  ```js
21
21
  // This module is only offered as a ES Module
22
- import {patternToRegex} from 'webext-patterns';
22
+ import {
23
+ patternToRegex,
24
+ globToRegex,
25
+ excludeDuplicatePatterns
26
+ doesUrlMatchPatterns,
27
+ assertValidPattern,
28
+ isValidPattern,
29
+ } from 'webext-patterns';
23
30
  ```
24
31
 
25
32
  ## Usage
@@ -33,6 +40,12 @@ globToRegex('*.example.com');
33
40
 
34
41
  excludeDuplicatePatterns(['https://*.google.com/*', 'https://google.com/*']);
35
42
  // Returns ['https://*.google.com/*']
43
+
44
+ assertValidPattern('https://google.*/*');
45
+ // Throws an error because the pattern is invalid
46
+
47
+ isValidPattern('https://*.google.com/*');
48
+ // Returns true
36
49
  ```
37
50
 
38
51
  > **Note**
@@ -100,6 +113,33 @@ excludeDuplicatePatterns([
100
113
  // Returns ["https://*/*"]
101
114
  ```
102
115
 
116
+ #### doesUrlMatchPatterns(url, ...patterns)
117
+
118
+ Accepts a URL and any number of patterns and returns `true` if the URL matches any of the patterns. This is a convenience method that wraps `patternToRegex` for single use. If you plan on testing multiple URLs to the same pattern, it's better to convert the patterns to a regex once and reuse that.
119
+
120
+ ```js
121
+ doesUrlMatchPatterns('https://google.com/', ['https://*.google.com/*', '*://example.com/*']);
122
+ // Returns true
123
+ ```
124
+
125
+ #### assertValidPattern(pattern)
126
+
127
+ Accepts a pattern and throws an error if it's invalid.
128
+
129
+ ```js
130
+ assertValidPattern('https://google.*/*');
131
+ // Throws an error because the pattern is invalid
132
+ ```
133
+
134
+ #### isValidPattern(pattern)
135
+
136
+ Accepts a pattern and returns `true` if it's valid.
137
+
138
+ ```js
139
+ isValidPattern('https://google.*/*');
140
+ // Returns false
141
+ ```
142
+
103
143
  ## Related
104
144
 
105
145
  ### Permissions