|
3 | 3 | * Uses OCI distribution-style regexes to parse name, registry, tag, and digest. |
4 | 4 | */ |
5 | 5 |
|
| 6 | +import RE2 = require("re2"); |
| 7 | + |
6 | 8 | // Full reference: optional registry, repository path, optional tag, optional digest. |
7 | 9 | // Capture groups: 1 = name (repo path including optional registry), 2 = tag, 3 = digest. |
8 | | -const imageReferencePattern = String.raw`^((?:(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))*|\[(?:[a-fA-F0-9:]+)\])(?::[0-9]+)?/)?[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*)*)(?::([a-zA-Z0-9_][a-zA-Z0-9._-]{0,127}))?(?:@([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][a-fA-F0-9]{32,}))?$`; |
9 | | -const imageReferenceRegex = new RegExp(imageReferencePattern); |
| 10 | +const imageReferencePattern = String.raw`^((?:(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))*|\[(?:[a-fA-F0-9:]+)\])(?::[0-9]+)?/)?[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*)*)(?::([\w][\w.-]{0,127}))?(?:@([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}))?$`; |
| 11 | +const imageReferenceRegex = new RE2(imageReferencePattern); |
10 | 12 |
|
11 | 13 | // Registry prefix only. Requires '.' or localhost to distinguish registry from repository. |
12 | 14 | // Capture group 1 = registry hostname (no trailing '/' or '@'). |
13 | 15 | const imageRegistryPattern = String.raw`^((?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))+|\[(?:[a-fA-F0-9:]+)\]|localhost)(?::[0-9]+)?)(?:/|@)`; |
14 | | -const imageRegistryRegex = new RegExp(imageRegistryPattern); |
| 16 | +const imageRegistryRegex = new RE2(imageRegistryPattern); |
15 | 17 |
|
16 | 18 | // Digest regex. Anchored to the start and end of the string. |
17 | 19 | const digestPattern = String.raw`^[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][a-fA-F0-9]{32,}$`; |
18 | | -const digestRegex = new RegExp(digestPattern); |
| 20 | +const digestRegex = new RE2(digestPattern); |
19 | 21 |
|
20 | 22 | export class ParsedImageReference { |
21 | 23 | /** Repository path (e.g. nginx, library/nginx) */ |
|
0 commit comments