2 // ====================================================================
4 // Copyright © 2020, 2022 Lady [@ Lady’s Computer].
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
10 import { push
, splice
} from "./collection.js";
11 import { bind
, call
} from "./function.js";
12 import { objectCreate
} from "./object.js";
15 getFirstSubstringIndex
,
16 getLastSubstringIndex
,
24 const sub
·delims
= rawString
`[!\$&'()*+,;=]`;
25 const gen
·delims
= rawString
`[:/?#\[\]@]`;
26 //deno-lint-ignore no-unused-vars
27 const reserved
= rawString
`${gen·delims}|${sub·delims}`;
28 const unreserved
= rawString
`[A-Za-z0-9\-\._~]`;
29 const pct
·encoded
= rawString
`%[0-9A-Fa-f][0-9A-Fa-f]`;
31 rawString
`[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]`;
33 rawString
`(?:${dec·octet})\.(?:${dec·octet})\.(?:${dec·octet})\.(?:${dec·octet})`;
34 const h16
= rawString
`[0-9A-Fa-f]{1,4}`;
35 const ls32
= rawString
`(?:${h16}):(?:${h16})|${IPv4address}`;
37 rawString
`(?:(?:${h16}):){6}(?:${ls32})|::(?:(?:${h16}):){5}(?:${ls32})|(?:${h16})?::(?:(?:${h16}):){4}(?:${ls32})|(?:(?:(?:${h16}):){0,1}(?:${h16}))?::(?:(?:${h16}):){3}(?:${ls32})|(?:(?:(?:${h16}):){0,2}(?:${h16}))?::(?:(?:${h16}):){2}(?:${ls32})|(?:(?:(?:${h16}):){0,3}(?:${h16}))?::(?:${h16}):(?:${ls32})|(?:(?:(?:${h16}):){0,4}(?:${h16}))?::(?:${ls32})|(?:(?:(?:${h16}):){0,5}(?:${h16}))?::(?:${h16})|(?:(?:(?:${h16}):){0,6}(?:${h16}))?::`;
39 rawString
`v[0-9A-Fa-f]{1,}\.(?:${unreserved}|${sub·delims}|:)`;
40 const IP
·literal
= rawString
`\[(?:${IPv6address}|${IPvFuture})\]`;
41 const port
= rawString
`[0-9]*`;
42 const scheme
= rawString
`[A-Za-z][A-Za-z0-9+\-\.]*`;
44 rawString
`${unreserved}|${pct·encoded}|${sub·delims}|[:@]`;
45 const fragment
= rawString
`(?:${pchar}|[/?])*`;
46 const query
= rawString
`(?:${pchar}|[/?])*`;
48 rawString
`(?:${unreserved}|${pct·encoded}|${sub·delims}|@)+`;
49 const segment
·nz
= rawString
`(?:${pchar})+`;
50 const segment
= rawString
`(?:${pchar})*`;
51 const path
·empty
= rawString
``;
52 const path
·rootless
= rawString
`(?:${segment·nz})(?:/(?:${segment}))*`;
54 rawString
`(?:${segment·nz·nc})(?:/(?:${segment}))*`;
56 rawString
`/(?:(?:${segment·nz})(?:/(?:${segment}))*)?`;
57 const path
·abempty
= rawString
`(?:/(?:${segment}))*`;
59 rawString
`${path·abempty}|${path·absolute}|${path·noscheme}|${path·rootless}|${path·empty}`;
61 rawString
`(?:${unreserved}|${pct·encoded}|${sub·delims})*`;
62 const host
= rawString
`${IP·literal}|${IPv4address}|${reg·name}`;
64 rawString
`(?:${unreserved}|${pct·encoded}|${sub·delims}|:)*`;
66 rawString
`(?:(?:${userinfo})@)?(?:${host})(?::(?:${port}))?`;
68 rawString
`//(?:${authority})(?:${path·abempty})|(?:${path·absolute})|(?:${path·noscheme})|(?:${path·empty})`;
70 rawString
`(?:${relative·part})(?:\?(?:${query}))?(?:#(?:${fragment}))?`;
72 rawString
`//(?:${authority})(?:${path·abempty})|(?:${path·absolute})|(?:${path·rootless})|(?:${path·empty})`;
74 rawString
`(?:${scheme}):(?:${hier·part})(?:\?(?:${query}))?`;
76 rawString
`(?:${scheme}):(?:${hier·part})(?:\?(?:${query}))?(?:#(?:${fragment}))?`;
77 const URI
·reference
= rawString
`(?:${URI})|(?:${relative·ref})`;
80 rawString
`[\u{E000}-\u{F8FF}\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}]`;
82 rawString
`[\u{A0}-\u{D7FF}\u{F900}-\u{FDCF}\u{FDF0}-\u{FFEF}\u{10000}-\u{1FFFD}\u{20000}-\u{2FFFD}\u{30000}-\u{3FFFD}\u{40000}-\u{4FFFD}\u{50000}-\u{5FFFD}\u{60000}-\u{6FFFD}\u{70000}-\u{7FFFD}\u{80000}-\u{8FFFD}\u{90000}-\u{9FFFD}\u{A0000}-\u{AFFFD}\u{B0000}-\u{BFFFD}\u{C0000}-\u{CFFFD}\u{D0000}-\u{DFFFD}\u{E0000}-\u{EFFFD}]`;
83 const iunreserved
= rawString
`[A-Za-z0-9\-\._~]|${ucschar}`;
85 rawString
`${iunreserved}|${pct·encoded}|${sub·delims}|[:@]`;
86 const ifragment
= rawString
`(?:${ipchar}|[/?])*`;
87 const iquery
= rawString
`(?:${ipchar}|${iprivate}|[/?])*`;
88 const isegment
·nz
·nc
=
89 rawString
`(?:${iunreserved}|${pct·encoded}|${sub·delims}|@)+`;
90 const isegment
·nz
= rawString
`(?:${ipchar})+`;
91 const isegment
= rawString
`(?:${ipchar})*`;
92 const ipath
·empty
= rawString
``;
93 const ipath
·rootless
=
94 rawString
`(?:${isegment·nz})(?:/(?:${isegment}))*`;
95 const ipath
·noscheme
=
96 rawString
`(?:${isegment·nz·nc})(?:/(?:${isegment}))*`;
97 const ipath
·absolute
=
98 rawString
`/(?:(?:${isegment·nz})(?:/(?:${isegment}))*)?`;
99 const ipath
·abempty
= rawString
`(?:/(?:${isegment}))*`;
101 rawString
`${ipath·abempty}|${ipath·absolute}|${ipath·noscheme}|${ipath·rootless}|${ipath·empty}`;
103 rawString
`(?:${iunreserved}|${pct·encoded}|${sub·delims})*`;
104 const ihost
= rawString
`${IP·literal}|${IPv4address}|${ireg·name}`;
106 rawString
`(?:${iunreserved}|${pct·encoded}|${sub·delims}|:)*`;
108 rawString
`(?:(?:${iuserinfo})@)?(?:${ihost})(?::(?:${port}))?`;
109 const irelative
·part
=
110 rawString
`//(?:${iauthority})(?:${ipath·abempty})|(?:${ipath·absolute})|(?:${ipath·noscheme})|(?:${ipath·empty})`;
111 const irelative
·ref
=
112 rawString
`(?:${irelative·part})(?:\?(?:${iquery}))?(?:#(?:${ifragment}))?`;
114 rawString
`//(?:${iauthority})(?:${ipath·abempty})|(?:${ipath·absolute})|(?:${ipath·rootless})|(?:${ipath·empty})`;
116 rawString
`(?:${scheme}):(?:${ihier·part})(?:\?(?:${iquery}))?`;
118 rawString
`(?:${scheme}):(?:${ihier·part})(?:\?(?:${iquery}))?(?:#(?:${ifragment}))?`;
119 const IRI
·reference
= rawString
`(?:${IRI})|(?:${irelative·ref})`;
121 const leiri
·iprivate
=
122 rawString
`[\u{E000}-\u{F8FF}\u{E0000}-\u{E0FFF}\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}]`;
123 const leiri
·ucschar
=
124 rawString
`[ <>"{}|\\^${"\x60"}\u{0}-\u{1F}\u{7F}-\u{D7FF}\u{E000}-\u{FFFD}\u{10000}-\u{10FFFF}]`;
125 const leiri
·iunreserved
=
126 rawString
`[A-Za-z0-9\-\._~]|${leiri·ucschar}`;
128 rawString
`${leiri·iunreserved}|${pct·encoded}|${sub·delims}|[:@]`;
129 const leiri
·ifragment
= rawString
`(?:${leiri·ipchar}|[/?])*`;
131 rawString
`(?:${leiri·ipchar}|${leiri·iprivate}|[/?])*`;
132 const leiri
·isegment
·nz
·nc
=
133 rawString
`(?:${leiri·iunreserved}|${pct·encoded}|${sub·delims}|@)+`;
134 const leiri
·isegment
·nz
= rawString
`(?:${leiri·ipchar})+`;
135 const leiri
·isegment
= rawString
`(?:${leiri·ipchar})*`;
136 const leiri
·ipath
·empty
= rawString
``;
137 const leiri
·ipath
·rootless
=
138 rawString
`(?:${leiri·isegment·nz})(?:/(?:${leiri·isegment}))*`;
139 const leiri
·ipath
·noscheme
=
140 rawString
`(?:${leiri·isegment·nz·nc})(?:/(?:${leiri·isegment}))*`;
141 const leiri
·ipath
·absolute
=
142 rawString
`/(?:(?:${leiri·isegment·nz})(?:/(?:${leiri·isegment}))*)?`;
143 const leiri
·ipath
·abempty
= rawString
`(?:/(?:${leiri·isegment}))*`;
145 rawString
`${leiri·ipath·abempty}|${leiri·ipath·absolute}|${leiri·ipath·noscheme}|${leiri·ipath·rootless}|${leiri·ipath·empty}`;
146 const leiri
·ireg
·name
=
147 rawString
`(?:${leiri·iunreserved}|${pct·encoded}|${sub·delims})*`;
149 rawString
`${IP·literal}|${IPv4address}|${leiri·ireg·name}`;
150 const leiri
·iuserinfo
=
151 rawString
`(?:${leiri·iunreserved}|${pct·encoded}|${sub·delims}|:)*`;
152 const leiri
·iauthority
=
153 rawString
`(?:(?:${leiri·iuserinfo})@)?(?:${leiri·ihost})(?::(?:${port}))?`;
154 const leiri
·irelative
·part
=
155 rawString
`//(?:${leiri·iauthority})(?:${leiri·ipath·abempty})|(?:${leiri·ipath·absolute})|(?:${leiri·ipath·noscheme})|(?:${leiri·ipath·empty})`;
156 const leiri
·irelative
·ref
=
157 rawString
`(?:${leiri·irelative·part})(?:\?(?:${leiri·iquery}))?(?:#(?:${leiri·ifragment}))?`;
158 const leiri
·ihier
·part
=
159 rawString
`//(?:${leiri·iauthority})(?:${leiri·ipath·abempty})|(?:${leiri·ipath·absolute})|(?:${leiri·ipath·rootless})|(?:${leiri·ipath·empty})`;
160 const absolute
·LEIRI
=
161 rawString
`(?:${scheme}):(?:${leiri·ihier·part})(?:\?(?:${leiri·iquery}))?`;
163 rawString
`(?:${scheme}):(?:${leiri·ihier·part})(?:\?(?:${leiri·iquery}))?(?:#(?:${leiri·ifragment}))?`;
164 const LEIRI
·reference
=
165 rawString
`(?:${LEIRI})|(?:${leiri·irelative·ref})`;
169 * Recomposes an (L·E·)I·R·I reference from its component parts.
171 * See §5.3 of R·F·C 3986.
176 * Converts an L·E·I·R·I to the corresponding I·R·I by
177 * percent‐encoding unsupported characters.
179 * This function is somewhat complex because the I·R·I syntax allows
180 * private·use characters *only* in the query.
185 * Converts an (L·E·)I·R·I to the corresponding U·R·I by
186 * percent‐encoding unsupported characters.
188 * This does not punycode the authority.
193 * Removes all dot segments ("." or "..") from the provided
196 * See §5.2.4 of R·F·C 3986.
200 const TE
= TextEncoder
;
201 const { iterator
: iteratorSymbol
} = Symbol
;
202 const { toString
: numberToString
} = Number
.prototype;
203 const { encode
: teEncode
} = TE
.prototype;
205 const { [iteratorSymbol
]: arrayIterator
} = Array
.prototype;
207 next
: arrayIteratorNext
,
208 } = Object
.getPrototypeOf([][iteratorSymbol
]());
210 next
: generatorIteratorNext
,
211 } = Object
.getPrototypeOf(function* () {}.prototype);
212 const { [iteratorSymbol
]: stringIterator
} = String
.prototype;
214 next
: stringIteratorNext
,
215 } = Object
.getPrototypeOf(""[iteratorSymbol
]());
217 const iriCharacterIterablePrototype
= {
222 call(stringIterator
, this.source
, []),
228 const iriGeneratorIterablePrototype
= {
231 next
: bind(generatorIteratorNext
, this.generator(), []),
235 const iriSegmentIterablePrototype
= {
240 call(arrayIterator
, this.segments
, []),
248 composeReference
: ($) =>
251 iriGeneratorIterablePrototype
,
254 value
: function* () {
255 const { scheme
, authority
, path
, query
, fragment
} = $;
256 if (scheme
!= null) {
257 // A scheme is present.
261 // No scheme is present.
264 if (authority
!= null) {
265 // An authority is present.
269 // No authority is present.
274 // A query is present.
278 // No query is present.
281 if (fragment
!= null) {
282 // A fragment is present.
286 // No fragment is present.
295 escapeForIRI
: ($) => {
296 const components
= parseReference($);
298 // The path will always be present (although perhaps empty) on a
299 // successful parse. If it isn’t (and parsing failed), treat the
300 // entire input as the path.
301 components
.path
??= `${$}`;
303 // Escape disallowed codepoints in each component and compose an
304 // I·R·I from the result.
305 const reference
= objectCreate(null);
306 for (const componentName
in components
) {
307 const componentValue
= components
[componentName
];
308 reference
[componentName
] = componentValue
== null
312 iriGeneratorIterablePrototype
,
315 value
: function* () {
316 const encoder
= new TE();
318 const character
of objectCreate(
319 iriCharacterIterablePrototype
,
320 { source
: { value
: componentValue
} },
325 `${leiri·ucschar}|${leiri·iprivate}`,
329 componentName == "query"
335 // This codepoint needs to be escaped.
336 const encoded
= call(teEncode
, encoder
, [
341 index
< encoded
.length
;
344 const byte = encoded
[index
];
345 yield `%${byte < 0x10 ? "0" : ""}${
347 call(numberToString, byte, [0x10]),
352 // This codepoint does not need escaping.
363 return composeReference(reference
);
368 iriGeneratorIterablePrototype
,
371 value
: function* () {
372 const encoder
= new TE();
374 const character
of objectCreate(
375 iriCharacterIterablePrototype
,
376 { source
: { value
: `${$}` } },
381 `${leiri·ucschar}|${leiri·iprivate}`,
384 // This codepoint needs to be escaped.
385 const encoded
= call(teEncode
, encoder
, [
390 index
< encoded
.length
;
393 const byte = encoded
[index
];
394 yield `%${byte < 0x10 ? "0" : ""}${
396 call(numberToString, byte, [0x10]),
401 // This codepoint does not need escaping.
411 removeDotSegments
: ($) => {
412 const input
= `${$}`;
414 const { length
} = input
;
416 while (index
< length
) {
417 if (stringStartsWith(input
, "../", index
)) {
418 // The input starts with a double leader; drop it. This can
419 // only occur at the beginning of the input.
421 } else if (stringStartsWith(input
, "./", index
)) {
422 // The input starts with a single leader; drop it. This can
423 // only occur at the beginning of the input.
425 } else if (stringStartsWith(input
, "/./", index
)) {
426 // The input starts with a slash, single leader, and another
427 // slash. Ignore it, and move the input to just before the
431 stringStartsWith(input
, "/.", index
) && index
+ 2 == length
433 // The input starts with a slash and single leader, and this
434 // exhausts the string. Push an empty segment and move the
435 // index to the end of the string.
438 } else if (stringStartsWith(input
, "/../", index
)) {
439 // The input starts with a slash, double leader, and another
440 // slash. Drop a segment from the output, and move the input
441 // to just before the second slash.
443 splice(output
, -1, 1);
445 stringStartsWith(input
, "/..", index
) && index
+ 3 == length
447 // The input starts with a slash and single leader, and this
448 // exhausts the string. Drop a segment from the output, push
449 // an empty segment, and move the index to the end of the
451 splice(output
, -1, 1, "/");
454 stringStartsWith(input
, ".", index
) && index
+ 1 == length
||
455 stringStartsWith(input
, "..", index
) && index
+ 2 == length
457 // The input starts with a single or double leader, and this
458 // exhausts the string. Do nothing (this can only occur at
459 // the beginning of input) and move the index to the end of
463 // The input does not start with a leader. Advance the index
464 // to the position before the next slash and push the segment
465 // between the old and new positions.
466 const nextIndex
= getFirstSubstringIndex(
471 if (nextIndex
== -1) {
472 // No slash remains; set index to the end of the string.
473 push(output
, substring(input
, index
));
476 // There are further path segments.
477 push(output
, substring(input
, index
, nextIndex
));
484 iriSegmentIterablePrototype
,
485 { segments
: { value
: output
} },
494 isAbsoluteIRI
, // I·R·I with no fragment
495 isAbsoluteLEIRI
, // L·E·I·R·I with no fragment
496 isAbsoluteURI
, // U·R·I with no fragment
500 isIRISuffix
, // only authority, path, query, fragment
504 isLEIRISuffix
, // only authority, path, query, fragment
508 isURISuffix
, // only authority, path, query, fragment
509 } = Object
.fromEntries(
511 isAbsoluteIRI
: absolute
·IRI
,
512 isAbsoluteLEIRI
: absolute
·LEIRI
,
513 isAbsoluteURI
: absolute
·URI
,
516 isIRIReference
: IRI
·reference
,
518 rawString
`(?:${iauthority})(?:${ipath·abempty})(?:\?(?:${iquery}))?(?:#(?:${ifragment}))?`,
520 isLEIRIPath
: leiri
·ipath
,
521 isLEIRIReference
: LEIRI
·reference
,
523 rawString
`(?:${leiri·iauthority})(?:${leiri·ipath·abempty})(?:\?(?:${leiri·iquery}))?(?:#(?:${leiri·ifragment}))?`,
526 isURIReference
: URI
·reference
,
528 rawString
`(?:${authority})(?:${path·abempty})(?:\?(?:${query}))?(?:#(?:${fragment}))?`,
530 ([key
, value
]) => [key
, new Matcher(rawString
`^(?:${value})$`)],
535 * Returns the result of merging the provided reference path with the
536 * provided base path.
538 * See §5.2.3 of R·F·C 3986.
540 export const mergePaths
= (base
, reference
) => {
541 const baseStr
= `${base}` || "/";
543 substring(baseStr, 0, getLastSubstringIndex(baseStr, "/") + 1)
549 * Returns the `scheme`, `authority`, `path`, `query`, and `fragment`
550 * of the provided (L·E·)I·R·I reference.
552 * `path` will always be defined for valid references, and will be
553 * undefined for values which are not valid L·E·I·R·Is.
558 const { prototype: rePrototype
} = RE
;
559 const { exec
: reExec
} = rePrototype
;
561 parseReference
: ($) => {
563 rawString
`^(?:(?<absolute·scheme>${scheme}):(?://(?<absolute·authority>${leiri·iauthority})(?<absolute·patha>${leiri·ipath·abempty})|(?<absolute·pathb>(?:${leiri·ipath·absolute})|(?:${leiri·ipath·rootless})|(?:${leiri·ipath·empty})))(?:\?(?<absolute·query>${leiri·iquery}))?(?:#(?<absolute·fragment>${leiri·ifragment}))?|(?://(?<relative·authority>${leiri·iauthority})(?<relative·patha>${leiri·ipath·abempty})|(?<relative·pathb>(?:${leiri·ipath·absolute})|(?:${leiri·ipath·noscheme})|(?:${leiri·ipath·empty})))(?:\?(?<relative·query>${leiri·iquery}))?(?:#(?<relative·fragment>${leiri·ifragment}))?)$`,
578 } = call(reExec
, re
, [$])?.groups
?? {};
580 scheme
: absolute
·scheme
,
581 authority
: absolute
·authority
?? relative
·authority
,
582 path
: absolute
·patha
?? absolute
·pathb
?? relative
·patha
??
584 query
: absolute
·query
?? relative
·query
,
585 fragment
: absolute
·fragment
?? relative
·fragment
,
592 * Resolves the provided reference relative to the provided base
595 * See §5.2 of R·F·C 3986.
597 export const resolveReference
= (R
, Base
= location
?? "") => {
600 authority
: Base
·authority
,
603 } = parseReference(Base
);
604 if (Base
·scheme
== null) {
605 // Base I·R·I’s must be valid I·R·I’s, meaning they must have a
608 `Piscēs: Base did not have a scheme: ${Base}.`,
611 // The provided Base I·R·I is valid.
614 authority
: R
·authority
,
617 fragment
: R
·fragment
,
618 } = parseReference(R
);
619 return composeReference(
623 authority
: R
·authority
,
624 path
: removeDotSegments(R
·path
),
626 fragment
: R
·fragment
,
628 : R
·authority
!= null
631 authority
: R
·authority
,
632 path
: removeDotSegments(R
·path
),
634 fragment
: R
·fragment
,
639 authority
: Base
·authority
,
641 query
: R
·query
?? Base
·query
,
642 fragment
: R
·fragment
,
646 authority
: Base
·authority
,
647 path
: R
·path
[0] == "/"
648 ? removeDotSegments(R
·path
)
649 : removeDotSegments(mergePaths(Base
·path
, R
·path
)),
651 fragment
: R
·fragment
,