]>
Lady’s Gitweb - Pisces/blob - iri.js
a3e454689346b998df91fb413316af89219ef033
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 { bind
} from "./function.js";
17 const sub
·delims
= String
.raw
`[!\$&'()*+,;=]`;
18 const gen
·delims
= String
.raw
`[:/?#\[\]@]`;
19 //deno-lint-ignore no-unused-vars
20 const reserved
= String
.raw
`${gen·delims}|${sub·delims}`;
21 const unreserved
= String
.raw
`[A-Za-z0-9\-\._~]`;
22 const pct
·encoded
= String
.raw
`%[0-9A-Fa-f][0-9A-Fa-f]`;
23 const dec
·octet
= String
.raw
24 `[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]`;
25 const IPv4address
= String
.raw
26 `(?:${dec·octet})\.(?:${dec·octet})\.(?:${dec·octet})\.(?:${dec·octet})`;
27 const h16
= String
.raw
`[0-9A-Fa-f]{1,4}`;
28 const ls32
= String
.raw
`(?:${h16}):(?:${h16})|${IPv4address}`;
29 const IPv6address
= String
.raw
30 `(?:(?:${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}))?::`;
31 const IPvFuture
= String
.raw
32 `v[0-9A-Fa-f]{1,}\.(?:${unreserved}|${sub·delims}|:)`;
33 const IP
·literal
= String
.raw
`\[(?:${IPv6address}|${IPvFuture})\]`;
34 const port
= String
.raw
`[0-9]*`;
35 const scheme
= String
.raw
`[A-Za-z][A-Za-z0-9+\-\.]*`;
36 const pchar
= String
.raw
37 `${unreserved}|${pct·encoded}|${sub·delims}|[:@]`;
38 const fragment
= String
.raw
`(?:${pchar}|[/?])*`;
39 const query
= String
.raw
`(?:${pchar}|[/?])*`;
40 const segment
·nz
·nc
= String
.raw
41 `(?:${unreserved}|${pct·encoded}|${sub·delims}|@)+`;
42 const segment
·nz
= String
.raw
`(?:${pchar})+`;
43 const segment
= String
.raw
`(?:${pchar})*`;
44 const path
·empty
= String
.raw
``;
45 const path
·rootless
= String
.raw
46 `(?:${segment·nz})(?:/(?:${segment}))*`;
47 const path
·noscheme
= String
.raw
48 `(?:${segment·nz·nc})(?:/(?:${segment}))*`;
49 const path
·absolute
= String
.raw
50 `/(?:(?:${segment·nz})(?:/(?:${segment}))*)?`;
51 const path
·abempty
= String
.raw
`(?:/(?:${segment}))*`;
52 const path
= String
.raw
53 `${path·abempty}|${path·absolute}|${path·noscheme}|${path·rootless}|${path·empty}`;
54 const reg
·name
= String
.raw
55 `(?:${unreserved}|${pct·encoded}|${sub·delims})*`;
56 const host
= String
.raw
`${IP·literal}|${IPv4address}|${reg·name}`;
57 const userinfo
= String
.raw
58 `(?:${unreserved}|${pct·encoded}|${sub·delims}|:)*`;
59 const authority
= String
.raw
60 `(?:(?:${userinfo})@)?(?:${host})(?::(?:${port}))?`;
61 const relative
·part
= String
.raw
62 `//(?:${authority})(?:${path·abempty})|(?:${path·absolute})|(?:${path·noscheme})|(?:${path·empty})`;
63 const relative
·ref
= String
.raw
64 `(?:${relative·part})(?:\?(?:${query}))?(?:#(?:${fragment}))?`;
65 const hier
·part
= String
.raw
66 `//(?:${authority})(?:${path·abempty})|(?:${path·absolute})|(?:${path·rootless})|(?:${path·empty})`;
67 const absolute
·URI
= String
.raw
68 `(?:${scheme}):(?:${hier·part})(?:\?(?:${query}))?`;
69 const URI
= String
.raw
70 `(?:${scheme}):(?:${hier·part})(?:\?(?:${query}))?(?:#(?:${fragment}))?`;
71 const URI
·reference
= String
.raw
`(?:${URI})|(?:${relative·ref})`;
73 const iprivate
= String
.raw
74 `[\u{E000}-\u{F8FF}\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}]`;
75 const ucschar
= String
.raw
76 `[\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}]`;
77 const iunreserved
= String
.raw
`[A-Za-z0-9\-\._~]|${ucschar}`;
78 const ipchar
= String
.raw
79 `${iunreserved}|${pct·encoded}|${sub·delims}|[:@]`;
80 const ifragment
= String
.raw
`(?:${ipchar}|[/?])*`;
81 const iquery
= String
.raw
`(?:${ipchar}|${iprivate}|[/?])*`;
82 const isegment
·nz
·nc
= String
.raw
83 `(?:${iunreserved}|${pct·encoded}|${sub·delims}|@)+`;
84 const isegment
·nz
= String
.raw
`(?:${ipchar})+`;
85 const isegment
= String
.raw
`(?:${ipchar})*`;
86 const ipath
·empty
= String
.raw
``;
87 const ipath
·rootless
= String
.raw
88 `(?:${isegment·nz})(?:/(?:${isegment}))*`;
89 const ipath
·noscheme
= String
.raw
90 `(?:${isegment·nz·nc})(?:/(?:${isegment}))*`;
91 const ipath
·absolute
= String
.raw
92 `/(?:(?:${isegment·nz})(?:/(?:${isegment}))*)?`;
93 const ipath
·abempty
= String
.raw
`(?:/(?:${isegment}))*`;
94 const ipath
= String
.raw
95 `${ipath·abempty}|${ipath·absolute}|${ipath·noscheme}|${ipath·rootless}|${ipath·empty}`;
96 const ireg
·name
= String
.raw
97 `(?:${iunreserved}|${pct·encoded}|${sub·delims})*`;
98 const ihost
= String
.raw
`${IP·literal}|${IPv4address}|${ireg·name}`;
99 const iuserinfo
= String
.raw
100 `(?:${iunreserved}|${pct·encoded}|${sub·delims}|:)*`;
101 const iauthority
= String
.raw
102 `(?:(?:${iuserinfo})@)?(?:${ihost})(?::(?:${port}))?`;
103 const irelative
·part
= String
.raw
104 `//(?:${iauthority})(?:${ipath·abempty})|(?:${ipath·absolute})|(?:${ipath·noscheme})|(?:${ipath·empty})`;
105 const irelative
·ref
= String
.raw
106 `(?:${irelative·part})(?:\?(?:${iquery}))?(?:#(?:${ifragment}))?`;
107 const ihier
·part
= String
.raw
108 `//(?:${iauthority})(?:${ipath·abempty})|(?:${ipath·absolute})|(?:${ipath·rootless})|(?:${ipath·empty})`;
109 const absolute
·IRI
= String
.raw
110 `(?:${scheme}):(?:${ihier·part})(?:\?(?:${iquery}))?`;
111 const IRI
= String
.raw
112 `(?:${scheme}):(?:${ihier·part})(?:\?(?:${iquery}))?(?:#(?:${ifragment}))?`;
113 const IRI
·reference
= String
.raw
`(?:${IRI})|(?:${irelative·ref})`;
115 const leiri
·iprivate
= String
.raw
116 `[\u{E000}-\u{F8FF}\u{E0000}-\u{E0FFF}\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}]`;
117 const leiri
·ucschar
= String
.raw
118 `[ <>"{}|\\^${"`"}\u{0}-\u{1F}\u{7F}-\u{D7FF}\u{E000}-\u{FFFD}\u{10000}-\u{10FFFF}]`;
119 const leiri
·iunreserved
= String
.raw
120 `[A-Za-z0-9\-\._~]|${leiri·ucschar}`;
121 const leiri
·ipchar
= String
.raw
122 `${leiri·iunreserved}|${pct·encoded}|${sub·delims}|[:@]`;
123 const leiri
·ifragment
= String
.raw
`(?:${leiri·ipchar}|[/?])*`;
124 const leiri
·iquery
= String
.raw
125 `(?:${leiri·ipchar}|${leiri·iprivate}|[/?])*`;
126 const leiri
·isegment
·nz
·nc
= String
.raw
127 `(?:${leiri·iunreserved}|${pct·encoded}|${sub·delims}|@)+`;
128 const leiri
·isegment
·nz
= String
.raw
`(?:${leiri·ipchar})+`;
129 const leiri
·isegment
= String
.raw
`(?:${leiri·ipchar})*`;
130 const leiri
·ipath
·empty
= String
.raw
``;
131 const leiri
·ipath
·rootless
= String
.raw
132 `(?:${leiri·isegment·nz})(?:/(?:${leiri·isegment}))*`;
133 const leiri
·ipath
·noscheme
= String
.raw
134 `(?:${leiri·isegment·nz·nc})(?:/(?:${leiri·isegment}))*`;
135 const leiri
·ipath
·absolute
= String
.raw
136 `/(?:(?:${leiri·isegment·nz})(?:/(?:${leiri·isegment}))*)?`;
137 const leiri
·ipath
·abempty
= String
.raw
`(?:/(?:${leiri·isegment}))*`;
138 const leiri
·ipath
= String
.raw
139 `${leiri·ipath·abempty}|${leiri·ipath·absolute}|${leiri·ipath·noscheme}|${leiri·ipath·rootless}|${leiri·ipath·empty}`;
140 const leiri
·ireg
·name
= String
.raw
141 `(?:${leiri·iunreserved}|${pct·encoded}|${sub·delims})*`;
142 const leiri
·ihost
= String
.raw
143 `${IP·literal}|${IPv4address}|${leiri·ireg·name}`;
144 const leiri
·iuserinfo
= String
.raw
145 `(?:${leiri·iunreserved}|${pct·encoded}|${sub·delims}|:)*`;
146 const leiri
·iauthority
= String
.raw
147 `(?:(?:${leiri·iuserinfo})@)?(?:${leiri·ihost})(?::(?:${port}))?`;
148 const leiri
·irelative
·part
= String
.raw
149 `//(?:${leiri·iauthority})(?:${leiri·ipath·abempty})|(?:${leiri·ipath·absolute})|(?:${leiri·ipath·noscheme})|(?:${leiri·ipath·empty})`;
150 const leiri
·irelative
·ref
= String
.raw
151 `(?:${leiri·irelative·part})(?:\?(?:${leiri·iquery}))?(?:#(?:${leiri·ifragment}))?`;
152 const leiri
·ihier
·part
= String
.raw
153 `//(?:${leiri·iauthority})(?:${leiri·ipath·abempty})|(?:${leiri·ipath·absolute})|(?:${leiri·ipath·rootless})|(?:${leiri·ipath·empty})`;
154 const absolute
·LEIRI
= String
.raw
155 `(?:${scheme}):(?:${leiri·ihier·part})(?:\?(?:${leiri·iquery}))?`;
156 const LEIRI
= String
.raw
157 `(?:${scheme}):(?:${leiri·ihier·part})(?:\?(?:${leiri·iquery}))?(?:#(?:${leiri·ifragment}))?`;
158 const LEIRI
·reference
= String
.raw
159 `(?:${LEIRI})|(?:${leiri·irelative·ref})`;
162 isAbsoluteURI
, // U·R·I with no fragment
166 isURISuffix
, // only authority, path, query, fragment
167 isAbsoluteIRI
, // I·R·I with no fragment
171 isIRISuffix
, // only authority, path, query, fragment
172 isAbsoluteLEIRI
, // L·E·I·R·I with no fragment
176 isLEIRISuffix
, // only authority, path, query, fragment
177 } = objectFromEntries(
179 isAbsoluteLEIRI
: absolute
·LEIRI
,
180 isAbsoluteIRI
: absolute
·IRI
,
181 isAbsoluteURI
: absolute
·URI
,
183 isLEIRIPath
: leiri
·ipath
,
184 isLEIRIReference
: LEIRI
·reference
,
185 isLEIRISuffix
: String
.raw
186 `(?:${leiri·iauthority})(?:${leiri·ipath·abempty})(?:\?(?:${leiri·iquery}))?(?:#(?:${leiri·ifragment}))?`,
189 isIRIReference
: IRI
·reference
,
190 isIRISuffix
: String
.raw
191 `(?:${iauthority})(?:${ipath·abempty})(?:\?(?:${iquery}))?(?:#(?:${ifragment}))?`,
194 isURIReference
: URI
·reference
,
195 isURISuffix
: String
.raw
196 `(?:${authority})(?:${path·abempty})(?:\?(?:${query}))?(?:#(?:${fragment}))?`,
197 }).map(([key
, value
]) => {
198 const regExp
= new RegExp(`^(?:${value})$`, "u");
202 ($) => typeof $ == "string" && regExp
.test($),
204 name
: { value
: key
},
208 get: () => bind(regExp
[Symbol
.match
], regExp
, []),
218 * Recomposes an (L·E·)I·R·I reference from its component parts.
220 * See §5.3 of R·F·C 3986.
222 export const composeReference
= ($) => {
224 const { scheme
, authority
, path
, query
, fragment
} = $;
225 if (scheme
!= null) {
226 // A scheme is present.
227 result
.push(scheme
, ":");
229 // No scheme is present.
232 if (authority
!= null) {
233 // An authority is present.
234 result
.push("//", authority
);
236 // No authority is present.
239 result
.push(path
?? "");
241 // A query is present.
242 result
.push("?", query
);
244 // No query is present.
247 if (fragment
!= null) {
248 // A fragment is present.
249 result
.push("#", fragment
);
251 // No fragment is present.
254 return result
.join("");
258 * Converts an L·E·I·R·I to the corresponding I·R·I by percent‐encoding
259 * unsupported characters.
261 * This function is somewhat complex because the I·R·I syntax allows
262 * private·use characters *only* in the query.
264 export const escapeForIRI
= ($) => {
265 const components
= parseReference($);
266 const encoder
= new TextEncoder();
268 // The path will always be present (although perhaps empty) on a
269 // successful parse. If it isn’t (and parsing failed), treat the
270 // entire input as the path.
271 components
.path
??= `${$}`;
273 // Escape disallowed codepoints in each component and compose an
274 // I·R·I from the result.
275 return composeReference(
277 namedEntries(components
).map(
278 ([componentName
, componentValue
]) => [
280 componentValue
== null ? undefined : [...function* () {
281 for (const character
of componentValue
) {
283 new RegExp(`${leiri·ucschar}|${leiri·iprivate}`, "u")
289 componentName == "query" ? `|${iprivate}
` : ""
294 // This codepoint needs to be escaped.
295 for (const byte of encoder
.encode(character
)) {
296 yield `%${byte < 0x10 ? "0" : ""}${
297 byte.toString(0x10).toUpperCase()
301 // This codepoint does not need escaping.
313 * Converts an (L·E·)I·R·I to the corresponding U·R·I by
314 * percent‐encoding unsupported characters.
316 * This does not punycode the authority.
318 export const escapeForURI
= ($) =>
320 const encoder
= new TextEncoder();
321 for (const character
of `${$}`) {
323 new RegExp(`${leiri·ucschar}|${leiri·iprivate}`, "u").test(
327 // This codepoint needs to be escaped.
328 for (const byte of encoder
.encode(character
)) {
329 yield `%${byte < 0x10 ? "0" : ""}${
330 byte.toString(0x10).toUpperCase()
334 // This codepoint doesn’t need escaping.
341 * Merges a reference path with a base path.
343 * See §5.2.3 of R·F·C 3986.
345 export const mergePaths
= (base
, reference
) => {
346 const baseStr
= `${base}`;
348 baseStr.substring(0, baseStr.lastIndexOf("/") + 1)
353 * Returns the `scheme`, `authority`, `path`, `query`, and `fragment`
354 * of the provided (L·E·)I·R·I reference.
356 * `path` will always be defined for valid references, and will be
357 * undefined for values which are not valid L·E·I·R·Is.
359 export const parseReference
= ($) => {
360 const regExp
= new RegExp(
362 `^(?:(?<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}))?)$`,
377 } = regExp
.exec($)?.groups
?? {};
379 scheme
: absolute
·scheme
,
380 authority
: absolute
·authority
?? relative
·authority
,
381 path
: absolute
·patha
?? absolute
·pathb
?? relative
·patha
??
383 query
: absolute
·query
?? relative
·query
,
384 fragment
: absolute
·fragment
?? relative
·fragment
,
389 * Removes all dot segments ("." or "..") from the provided (L·E·)I·R·I.
391 * See §5.2.4 of R·F·C 3986.
393 export const removeDotSegments
= ($) => {
394 const input
= `${$}`;
396 const { length
} = input
;
398 while (index
< length
) {
399 if (input
.startsWith("../", index
)) {
400 // The input starts with a double leader; drop it. This can only
401 // occur at the beginning of the input.
403 } else if (input
.startsWith("./", index
)) {
404 // The input starts with a single leader; drop it. This can only
405 // occur at the beginning of the input.
407 } else if (input
.startsWith("/./", index
)) {
408 // The input starts with a slash, single leader, and another
409 // slash. Ignore it, and move the input to just before the second
412 } else if (input
.startsWith("/.", index
) && index
+ 2 == length
) {
413 // The input starts with a slash and single leader, and this
414 // exhausts the string. Push an empty segment and move the index
415 // to the end of the string.
418 } else if (input
.startsWith("/../", index
)) {
419 // The input starts with a slash, double leader, and another
420 // slash. Drop a segment from the output, and move the input to
421 // just before the second slash.
423 output
.splice(-1, 1);
424 } else if (input
.startsWith("/..", index
) && index
+ 3 == length
) {
425 // The input starts with a slash and single leader, and this
426 // exhausts the string. Drop a segment from the output, push an
427 // empty segment, and move the index to the end of the string.
428 output
.splice(-1, 1, "/");
431 input
.startsWith(".", index
) && index
+ 1 == length
||
432 input
.startsWith("..", index
) && index
+ 2 == length
434 // The input starts with a single or double leader, and this
435 // exhausts the string. Do nothing (this can only occur at the
436 // beginning of input) and move the index to the end of the
440 // The input does not start with a leader. Advance the index to
441 // the position before the next slash and push the segment
442 // between the old and new positions.
443 const nextIndex
= input
.indexOf("/", index
+ 1);
444 if (nextIndex
== -1) {
445 // No slash remains; set index to the end of the string.
446 output
.push(input
.substring(index
));
449 // There are further path segments.
450 output
.push(input
.substring(index
, nextIndex
));
455 return output
.join("");
459 * Resolves the provided reference relative to the provided base
462 * See §5.2 of R·F·C 3986.
464 export const resolveReference
= (R
, Base
= location
?? "") => {
467 authority
: Base
·authority
,
470 } = parseReference(Base
);
471 if (Base
·scheme
== null) {
472 // Base I·R·I’s must be valid I·R·I’s, meaning they must have a
475 `Piscēs: Base did not have a scheme: ${Base}.`,
478 // The provided Base I·R·I is valid.
481 authority
: R
·authority
,
484 fragment
: R
·fragment
,
485 } = parseReference(R
);
486 return composeReference(
490 authority
: R
·authority
,
491 path
: removeDotSegments(R
·path
),
493 fragment
: R
·fragment
,
495 : R
·authority
!= null
498 authority
: R
·authority
,
499 path
: removeDotSegments(R
·path
),
501 fragment
: R
·fragment
,
506 authority
: Base
·authority
,
508 query
: R
·query
?? Base
·query
,
509 fragment
: R
·fragment
,
513 authority
: Base
·authority
,
514 path
: R
·path
[0] == "/"
515 ? removeDotSegments(R
·path
)
516 : removeDotSegments(mergePaths(Base
·path
|| "/", R
·path
)),
518 fragment
: R
·fragment
,
This page took 0.118605 seconds and 3 git commands to generate.