What problem are you trying to solve?
Hey folks! I wonder whether you've considered a narrower use case of the URLPattern object which would make it more easily possible for folks to match against a pattern representing an origin as opposed to a URL?
I'm thinking about this in the context of handling MessageEvent objects, where we'd like developers to take a close look at the origin attribute before handling the message. URLPattern can certainly support this use case, as in:
function isReasonableOrigin(origin) {
const validOrigin = new URLPattern("https://*.trusted.site");
try {
const url = new URL(origin);
return validOrigin.test(url);
} catch(e) {
return false; // Or `true`, I suppose, if your application needs to
// work with opaque origins.
}
}
window.addEventListener('message', e => {
if (isReasonableOrigin(e.origin)) {
// Exciting activities go here...
}
});
This works, as long as developers remember to only populate protocol, hostname, and port in a URLPatternInit dictionary, or to ensure that they don't include a trailing slash in the URL pattern string (new URLPattern("https://*.trusted.site/") is fairly restrictive, for instance).
What solutions exist today?
Developers can hold the URLPattern object correctly.
How would you solve it?
Two possibilities come to mind:
-
Create an origin-matching variant of test() (and exec(), I suppose) which would only take protocol, hostname, and port into account, returning a match iff those three aspects of the pattern matched while ignoring extraneous attributes.
-
Create an OriginPattern object (and OriginPatternInit) which only accepts the relevant attributes.
The latter seems clearer to me, though I'm not sure how it would be related to the URLPattern object (subclass? parent class? completely separate thing? It would be nice if it could be used wherever URLPattern was used, so subclassing makes the most sense, but that means it would have a bunch of properties that are somewhat meaningless. Meh.)
Anything else?
No response
What problem are you trying to solve?
Hey folks! I wonder whether you've considered a narrower use case of the
URLPatternobject which would make it more easily possible for folks to match against a pattern representing an origin as opposed to a URL?I'm thinking about this in the context of handling
MessageEventobjects, where we'd like developers to take a close look at theoriginattribute before handling the message.URLPatterncan certainly support this use case, as in:This works, as long as developers remember to only populate
protocol,hostname, andportin aURLPatternInitdictionary, or to ensure that they don't include a trailing slash in the URL pattern string (new URLPattern("https://*.trusted.site/")is fairly restrictive, for instance).What solutions exist today?
Developers can hold the
URLPatternobject correctly.How would you solve it?
Two possibilities come to mind:
Create an origin-matching variant of
test()(andexec(), I suppose) which would only takeprotocol,hostname, andportinto account, returning a match iff those three aspects of the pattern matched while ignoring extraneous attributes.Create an
OriginPatternobject (andOriginPatternInit) which only accepts the relevant attributes.The latter seems clearer to me, though I'm not sure how it would be related to the
URLPatternobject (subclass? parent class? completely separate thing? It would be nice if it could be used whereverURLPatternwas used, so subclassing makes the most sense, but that means it would have a bunch of properties that are somewhat meaningless. Meh.)Anything else?
No response