UNPKG

5.04 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2022 Firebase
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.PathPattern = exports.trimParam = void 0;
25const path_1 = require("./path");
26/** https://cloud.google.com/eventarc/docs/path-patterns */
27/** @hidden */
28const WILDCARD_CAPTURE_REGEX = new RegExp("{[^/{}]+}", "g");
29/** @internal */
30function trimParam(param) {
31 const paramNoBraces = param.slice(1, -1);
32 if (paramNoBraces.includes("=")) {
33 return paramNoBraces.slice(0, paramNoBraces.indexOf("="));
34 }
35 return paramNoBraces;
36}
37exports.trimParam = trimParam;
38/** @hidden */
39class Segment {
40 constructor(value) {
41 this.value = value;
42 this.name = "segment";
43 this.trimmed = value;
44 }
45 isSingleSegmentWildcard() {
46 return this.value.includes("*") && !this.isMultiSegmentWildcard();
47 }
48 isMultiSegmentWildcard() {
49 return this.value.includes("**");
50 }
51}
52/** @hidden */
53class SingleCaptureSegment {
54 constructor(value) {
55 this.value = value;
56 this.name = "single-capture";
57 this.trimmed = trimParam(value);
58 }
59 isSingleSegmentWildcard() {
60 return true;
61 }
62 isMultiSegmentWildcard() {
63 return false;
64 }
65}
66/** @hidden */
67class MultiCaptureSegment {
68 constructor(value) {
69 this.value = value;
70 this.name = "multi-capture";
71 this.trimmed = trimParam(value);
72 }
73 isSingleSegmentWildcard() {
74 return false;
75 }
76 isMultiSegmentWildcard() {
77 return true;
78 }
79}
80/**
81 * Implements Eventarc's path pattern from the spec https://cloud.google.com/eventarc/docs/path-patterns
82 * @internal
83 */
84class PathPattern {
85 /** @throws on validation error */
86 // eslint-disable-next-line @typescript-eslint/no-unused-vars
87 static compile(rawPath) {
88 return undefined;
89 }
90 constructor(raw) {
91 this.raw = raw;
92 this.segments = [];
93 this.initPathSegments(raw);
94 }
95 getValue() {
96 return this.raw;
97 }
98 // If false, we don't need to use pathPattern as our eventarc match type.
99 hasWildcards() {
100 return this.segments.some((segment) => segment.isSingleSegmentWildcard() || segment.isMultiSegmentWildcard());
101 }
102 hasCaptures() {
103 return this.segments.some((segment) => segment.name === "single-capture" || segment.name === "multi-capture");
104 }
105 extractMatches(path) {
106 const matches = {};
107 if (!this.hasCaptures()) {
108 return matches;
109 }
110 const pathSegments = (0, path_1.pathParts)(path);
111 let pathNdx = 0;
112 for (let segmentNdx = 0; segmentNdx < this.segments.length && pathNdx < pathSegments.length; segmentNdx++) {
113 const segment = this.segments[segmentNdx];
114 const remainingSegments = this.segments.length - 1 - segmentNdx;
115 const nextPathNdx = pathSegments.length - remainingSegments;
116 if (segment.name === "single-capture") {
117 matches[segment.trimmed] = pathSegments[pathNdx];
118 }
119 else if (segment.name === "multi-capture") {
120 matches[segment.trimmed] = pathSegments.slice(pathNdx, nextPathNdx).join("/");
121 }
122 pathNdx = segment.isMultiSegmentWildcard() ? nextPathNdx : pathNdx + 1;
123 }
124 return matches;
125 }
126 initPathSegments(raw) {
127 const parts = (0, path_1.pathParts)(raw);
128 for (const part of parts) {
129 let segment;
130 const capture = part.match(WILDCARD_CAPTURE_REGEX);
131 if (capture && capture.length === 1) {
132 segment = part.includes("**")
133 ? new MultiCaptureSegment(part)
134 : new SingleCaptureSegment(part);
135 }
136 else {
137 segment = new Segment(part);
138 }
139 this.segments.push(segment);
140 }
141 }
142}
143exports.PathPattern = PathPattern;