@atproto/syntax 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/LICENSE.txt +1 -1
- package/benchmark.js +208 -0
- package/dist/at-identifier.d.ts +5 -0
- package/dist/at-identifier.d.ts.map +1 -0
- package/dist/at-identifier.js +19 -0
- package/dist/at-identifier.js.map +1 -0
- package/dist/aturi.d.ts +8 -6
- package/dist/aturi.d.ts.map +1 -1
- package/dist/aturi.js +36 -45
- package/dist/aturi.js.map +1 -1
- package/dist/aturi_validation.d.ts +5 -2
- package/dist/aturi_validation.d.ts.map +1 -1
- package/dist/aturi_validation.js +54 -66
- package/dist/aturi_validation.js.map +1 -1
- package/dist/datetime.d.ts +11 -4
- package/dist/datetime.d.ts.map +1 -1
- package/dist/datetime.js +16 -16
- package/dist/datetime.js.map +1 -1
- package/dist/did.d.ts +3 -2
- package/dist/did.d.ts.map +1 -1
- package/dist/did.js +8 -8
- package/dist/did.js.map +1 -1
- package/dist/handle.d.ts +7 -6
- package/dist/handle.d.ts.map +1 -1
- package/dist/handle.js +28 -31
- package/dist/handle.js.map +1 -1
- package/dist/index.d.ts +8 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -7
- package/dist/index.js.map +1 -1
- package/dist/nsid.d.ts +30 -5
- package/dist/nsid.d.ts.map +1 -1
- package/dist/nsid.js +137 -47
- package/dist/nsid.js.map +1 -1
- package/dist/recordkey.d.ts +3 -2
- package/dist/recordkey.d.ts.map +1 -1
- package/dist/recordkey.js +33 -22
- package/dist/recordkey.js.map +1 -1
- package/dist/tid.d.ts +3 -2
- package/dist/tid.d.ts.map +1 -1
- package/dist/tid.js +7 -7
- package/dist/tid.js.map +1 -1
- package/package.json +1 -1
- package/src/at-identifier.ts +22 -0
- package/src/aturi.ts +59 -46
- package/src/aturi_validation.ts +62 -57
- package/src/datetime.ts +17 -4
- package/src/did.ts +5 -2
- package/src/handle.ts +23 -22
- package/src/index.ts +8 -7
- package/src/nsid.ts +146 -47
- package/src/recordkey.ts +40 -17
- package/src/tid.ts +4 -2
- package/tests/nsid.test.ts +117 -77
- package/tsconfig.build.json +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.tests.tsbuildinfo +1 -1
package/dist/aturi_validation.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
3
|
+
exports.ensureValidAtUri = ensureValidAtUri;
|
|
4
|
+
exports.ensureValidAtUriRegex = ensureValidAtUriRegex;
|
|
5
|
+
const at_identifier_js_1 = require("./at-identifier.js");
|
|
6
|
+
const did_js_1 = require("./did.js");
|
|
7
|
+
const handle_js_1 = require("./handle.js");
|
|
8
|
+
const nsid_js_1 = require("./nsid.js");
|
|
7
9
|
// Human-readable constraints on ATURI:
|
|
8
10
|
// - following regular URLs, a 8KByte hard total length limit
|
|
9
11
|
// - follows ATURI docs on website
|
|
@@ -17,74 +19,66 @@ const nsid_1 = require("./nsid");
|
|
|
17
19
|
// [a-zA-Z0-9._~:@!$&'\(\)*+,;=-]
|
|
18
20
|
// - rkey must have at least one char
|
|
19
21
|
// - regardless of path component, a fragment can follow as "#" and then a JSON pointer (RFC-6901)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
function ensureValidAtUri(input) {
|
|
23
|
+
const fragmentIndex = input.indexOf('#');
|
|
24
|
+
if (fragmentIndex !== -1) {
|
|
25
|
+
if (input.charCodeAt(fragmentIndex + 1) !== 47) {
|
|
26
|
+
throw new Error('ATURI fragment must be non-empty and start with slash');
|
|
27
|
+
}
|
|
28
|
+
if (input.includes('#', fragmentIndex + 1)) {
|
|
29
|
+
throw new Error('ATURI can have at most one "#", separating fragment out');
|
|
30
|
+
}
|
|
31
|
+
// NOTE: enforcing *some* checks here for sanity. Eg, at least no whitespace
|
|
32
|
+
const fragment = input.slice(fragmentIndex + 1);
|
|
33
|
+
if (!/^\/[a-zA-Z0-9._~:@!$&')(*+,;=%[\]/-]*$/.test(fragment)) {
|
|
34
|
+
throw new Error('Disallowed characters in ATURI fragment (ASCII)');
|
|
35
|
+
}
|
|
25
36
|
}
|
|
26
|
-
const
|
|
27
|
-
uri
|
|
28
|
-
|
|
29
|
-
if (!/^[a-zA-Z0-9._~:@!$&')(*+,;=%/-]*$/.test(uri)) {
|
|
30
|
-
throw new Error('Disallowed characters in ATURI (ASCII)');
|
|
37
|
+
const uri = fragmentIndex === -1 ? input : input.slice(0, fragmentIndex);
|
|
38
|
+
if (uri.length > 8 * 1024) {
|
|
39
|
+
throw new Error('ATURI is far too long');
|
|
31
40
|
}
|
|
32
|
-
|
|
33
|
-
if (parts.length >= 3 && (parts[0] !== 'at:' || parts[1].length !== 0)) {
|
|
41
|
+
if (!uri.startsWith('at://')) {
|
|
34
42
|
throw new Error('ATURI must start with "at://"');
|
|
35
43
|
}
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
// check that all chars are boring ASCII
|
|
45
|
+
if (!/^[a-zA-Z0-9._~:@!$&')(*+,;=%/-]*$/.test(uri)) {
|
|
46
|
+
throw new Error('Disallowed characters in ATURI (ASCII)');
|
|
38
47
|
}
|
|
48
|
+
const authorityEnd = uri.indexOf('/', 5);
|
|
49
|
+
const authority = authorityEnd === -1 ? uri.slice(5) : uri.slice(5, authorityEnd);
|
|
39
50
|
try {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (
|
|
51
|
+
(0, at_identifier_js_1.ensureValidAtIdentifier)(authority);
|
|
52
|
+
}
|
|
53
|
+
catch (cause) {
|
|
54
|
+
throw new Error('ATURI authority must be a valid handle or DID', { cause });
|
|
55
|
+
}
|
|
56
|
+
const collectionStart = authorityEnd === -1 ? -1 : authorityEnd + 1;
|
|
57
|
+
const collectionEnd = collectionStart === -1 ? -1 : uri.indexOf('/', collectionStart);
|
|
58
|
+
if (collectionStart !== -1) {
|
|
59
|
+
const collection = collectionEnd === -1
|
|
60
|
+
? uri.slice(collectionStart)
|
|
61
|
+
: uri.slice(collectionStart, collectionEnd);
|
|
62
|
+
if (collection.length === 0) {
|
|
52
63
|
throw new Error('ATURI can not have a slash after authority without a path segment');
|
|
53
64
|
}
|
|
54
|
-
|
|
55
|
-
(0, nsid_1.ensureValidNsid)(parts[3]);
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
65
|
+
if (!(0, nsid_js_1.isValidNsid)(collection)) {
|
|
58
66
|
throw new Error('ATURI requires first path segment (if supplied) to be valid NSID');
|
|
59
67
|
}
|
|
60
68
|
}
|
|
61
|
-
|
|
62
|
-
|
|
69
|
+
const recordKeyStart = collectionEnd === -1 ? -1 : collectionEnd + 1;
|
|
70
|
+
const recordKeyEnd = recordKeyStart === -1 ? -1 : uri.indexOf('/', recordKeyStart);
|
|
71
|
+
if (recordKeyStart !== -1) {
|
|
72
|
+
if (recordKeyStart === uri.length) {
|
|
63
73
|
throw new Error('ATURI can not have a slash after collection, unless record key is provided');
|
|
64
74
|
}
|
|
65
75
|
// would validate rkey here, but there are basically no constraints!
|
|
66
76
|
}
|
|
67
|
-
if (
|
|
77
|
+
if (recordKeyEnd !== -1) {
|
|
68
78
|
throw new Error('ATURI path can have at most two parts, and no trailing slash');
|
|
69
79
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
if (fragmentPart != null) {
|
|
74
|
-
if (fragmentPart.length === 0 || fragmentPart[0] !== '/') {
|
|
75
|
-
throw new Error('ATURI fragment must be non-empty and start with slash');
|
|
76
|
-
}
|
|
77
|
-
// NOTE: enforcing *some* checks here for sanity. Eg, at least no whitespace
|
|
78
|
-
if (!/^\/[a-zA-Z0-9._~:@!$&')(*+,;=%[\]/-]*$/.test(fragmentPart)) {
|
|
79
|
-
throw new Error('Disallowed characters in ATURI fragment (ASCII)');
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (uri.length > 8 * 1024) {
|
|
83
|
-
throw new Error('ATURI is far too long');
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
exports.ensureValidAtUri = ensureValidAtUri;
|
|
87
|
-
const ensureValidAtUriRegex = (uri) => {
|
|
80
|
+
}
|
|
81
|
+
function ensureValidAtUriRegex(uri) {
|
|
88
82
|
// simple regex to enforce most constraints via just regex and length.
|
|
89
83
|
// hand wrote this regex based on above constraints. whew!
|
|
90
84
|
const aturiRegex = /^at:\/\/(?<authority>[a-zA-Z0-9._:%-]+)(\/(?<collection>[a-zA-Z0-9-.]+)(\/(?<rkey>[a-zA-Z0-9._~:@!$&%')(*+,;=-]+))?)?(#(?<fragment>\/[a-zA-Z0-9._~:@!$&%')(*+,;=\-[\]/\\]*))?$/;
|
|
@@ -94,27 +88,21 @@ const ensureValidAtUriRegex = (uri) => {
|
|
|
94
88
|
}
|
|
95
89
|
const groups = rm.groups;
|
|
96
90
|
try {
|
|
97
|
-
(0,
|
|
91
|
+
(0, handle_js_1.ensureValidHandleRegex)(groups.authority);
|
|
98
92
|
}
|
|
99
93
|
catch {
|
|
100
94
|
try {
|
|
101
|
-
(0,
|
|
95
|
+
(0, did_js_1.ensureValidDidRegex)(groups.authority);
|
|
102
96
|
}
|
|
103
97
|
catch {
|
|
104
98
|
throw new Error('ATURI authority must be a valid handle or DID');
|
|
105
99
|
}
|
|
106
100
|
}
|
|
107
|
-
if (groups.collection) {
|
|
108
|
-
|
|
109
|
-
(0, nsid_1.ensureValidNsidRegex)(groups.collection);
|
|
110
|
-
}
|
|
111
|
-
catch {
|
|
112
|
-
throw new Error('ATURI collection path segment must be a valid NSID');
|
|
113
|
-
}
|
|
101
|
+
if (groups.collection && !(0, nsid_js_1.isValidNsid)(groups.collection)) {
|
|
102
|
+
throw new Error('ATURI collection path segment must be a valid NSID');
|
|
114
103
|
}
|
|
115
104
|
if (uri.length > 8 * 1024) {
|
|
116
105
|
throw new Error('ATURI is far too long');
|
|
117
106
|
}
|
|
118
|
-
}
|
|
119
|
-
exports.ensureValidAtUriRegex = ensureValidAtUriRegex;
|
|
107
|
+
}
|
|
120
108
|
//# sourceMappingURL=aturi_validation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aturi_validation.js","sourceRoot":"","sources":["../src/aturi_validation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"aturi_validation.js","sourceRoot":"","sources":["../src/aturi_validation.ts"],"names":[],"mappings":";;AAwBA,4CAiFC;AAED,sDA4BC;AAvID,yDAAgF;AAChF,qCAA8C;AAC9C,2CAAoD;AACpD,uCAAmD;AAOnD,uCAAuC;AACvC,+DAA+D;AAC/D,oCAAoC;AACpC,6EAA6E;AAC7E,wBAAwB;AACxB,sDAAsD;AACtD,iFAAiF;AACjF,kEAAkE;AAClE,8EAA8E;AAC9E,+HAA+H;AAC/H,0CAA0C;AAC1C,0CAA0C;AAC1C,wGAAwG;AAExG,SAAgB,gBAAgB,CAAC,KAAa;IAC5C,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IACxC,IAAI,aAAa,KAAK,CAAC,CAAC,EAAE,CAAC;QACzB,IAAI,KAAK,CAAC,UAAU,CAAC,aAAa,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC;YAC/C,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAA;QAC1E,CAAC;QACD,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,aAAa,GAAG,CAAC,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAA;QAC5E,CAAC;QAED,4EAA4E;QAC5E,MAAM,QAAQ,GAAG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,CACb,8DAA8D,CAC/D,CAAA;IACH,CAAC;AACH,CAAC;AAED,SAAgB,qBAAqB,CAAC,GAAW;IAC/C,sEAAsE;IACtE,0DAA0D;IAC1D,MAAM,UAAU,GACd,gLAAgL,CAAA;IAClL,MAAM,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;IAChC,IAAI,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAA;IACpD,CAAC;IACD,MAAM,MAAM,GAAG,EAAE,CAAC,MAAM,CAAA;IAExB,IAAI,CAAC;QACH,IAAA,kCAAsB,EAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IAC1C,CAAC;IAAC,MAAM,CAAC;QACP,IAAI,CAAC;YACH,IAAA,4BAAmB,EAAC,MAAM,CAAC,SAAS,CAAC,CAAA;QACvC,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAA;QAClE,CAAC;IACH,CAAC;IAED,IAAI,MAAM,CAAC,UAAU,IAAI,CAAC,IAAA,qBAAW,EAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;QACzD,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;IACvE,CAAC;IAED,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;IAC1C,CAAC;AACH,CAAC","sourcesContent":["import { AtIdentifierString, ensureValidAtIdentifier } from './at-identifier.js'\nimport { ensureValidDidRegex } from './did.js'\nimport { ensureValidHandleRegex } from './handle.js'\nimport { NsidString, isValidNsid } from './nsid.js'\n\nexport type AtUriString =\n | `at://${AtIdentifierString}`\n | `at://${AtIdentifierString}/${NsidString}`\n | `at://${AtIdentifierString}/${NsidString}/${string}`\n\n// Human-readable constraints on ATURI:\n// - following regular URLs, a 8KByte hard total length limit\n// - follows ATURI docs on website\n// - all ASCII characters, no whitespace. non-ASCII could be URL-encoded\n// - starts \"at://\"\n// - \"authority\" is a valid DID or a valid handle\n// - optionally, follow \"authority\" with \"/\" and valid NSID as start of path\n// - optionally, if NSID given, follow that with \"/\" and rkey\n// - rkey path component can include URL-encoded (\"percent encoded\"), or:\n// ALPHA / DIGIT / \"-\" / \".\" / \"_\" / \"~\" / \":\" / \"@\" / \"!\" / \"$\" / \"&\" / \"'\" / \"(\" / \")\" / \"*\" / \"+\" / \",\" / \";\" / \"=\"\n// [a-zA-Z0-9._~:@!$&'\\(\\)*+,;=-]\n// - rkey must have at least one char\n// - regardless of path component, a fragment can follow as \"#\" and then a JSON pointer (RFC-6901)\n\nexport function ensureValidAtUri(input: string): asserts input is AtUriString {\n const fragmentIndex = input.indexOf('#')\n if (fragmentIndex !== -1) {\n if (input.charCodeAt(fragmentIndex + 1) !== 47) {\n throw new Error('ATURI fragment must be non-empty and start with slash')\n }\n if (input.includes('#', fragmentIndex + 1)) {\n throw new Error('ATURI can have at most one \"#\", separating fragment out')\n }\n\n // NOTE: enforcing *some* checks here for sanity. Eg, at least no whitespace\n const fragment = input.slice(fragmentIndex + 1)\n if (!/^\\/[a-zA-Z0-9._~:@!$&')(*+,;=%[\\]/-]*$/.test(fragment)) {\n throw new Error('Disallowed characters in ATURI fragment (ASCII)')\n }\n }\n\n const uri = fragmentIndex === -1 ? input : input.slice(0, fragmentIndex)\n\n if (uri.length > 8 * 1024) {\n throw new Error('ATURI is far too long')\n }\n\n if (!uri.startsWith('at://')) {\n throw new Error('ATURI must start with \"at://\"')\n }\n\n // check that all chars are boring ASCII\n if (!/^[a-zA-Z0-9._~:@!$&')(*+,;=%/-]*$/.test(uri)) {\n throw new Error('Disallowed characters in ATURI (ASCII)')\n }\n\n const authorityEnd = uri.indexOf('/', 5)\n const authority =\n authorityEnd === -1 ? uri.slice(5) : uri.slice(5, authorityEnd)\n try {\n ensureValidAtIdentifier(authority)\n } catch (cause) {\n throw new Error('ATURI authority must be a valid handle or DID', { cause })\n }\n\n const collectionStart = authorityEnd === -1 ? -1 : authorityEnd + 1\n const collectionEnd =\n collectionStart === -1 ? -1 : uri.indexOf('/', collectionStart)\n\n if (collectionStart !== -1) {\n const collection =\n collectionEnd === -1\n ? uri.slice(collectionStart)\n : uri.slice(collectionStart, collectionEnd)\n\n if (collection.length === 0) {\n throw new Error(\n 'ATURI can not have a slash after authority without a path segment',\n )\n }\n if (!isValidNsid(collection)) {\n throw new Error(\n 'ATURI requires first path segment (if supplied) to be valid NSID',\n )\n }\n }\n\n const recordKeyStart = collectionEnd === -1 ? -1 : collectionEnd + 1\n const recordKeyEnd =\n recordKeyStart === -1 ? -1 : uri.indexOf('/', recordKeyStart)\n\n if (recordKeyStart !== -1) {\n if (recordKeyStart === uri.length) {\n throw new Error(\n 'ATURI can not have a slash after collection, unless record key is provided',\n )\n }\n // would validate rkey here, but there are basically no constraints!\n }\n\n if (recordKeyEnd !== -1) {\n throw new Error(\n 'ATURI path can have at most two parts, and no trailing slash',\n )\n }\n}\n\nexport function ensureValidAtUriRegex(uri: string): asserts uri is AtUriString {\n // simple regex to enforce most constraints via just regex and length.\n // hand wrote this regex based on above constraints. whew!\n const aturiRegex =\n /^at:\\/\\/(?<authority>[a-zA-Z0-9._:%-]+)(\\/(?<collection>[a-zA-Z0-9-.]+)(\\/(?<rkey>[a-zA-Z0-9._~:@!$&%')(*+,;=-]+))?)?(#(?<fragment>\\/[a-zA-Z0-9._~:@!$&%')(*+,;=\\-[\\]/\\\\]*))?$/\n const rm = uri.match(aturiRegex)\n if (!rm || !rm.groups) {\n throw new Error(\"ATURI didn't validate via regex\")\n }\n const groups = rm.groups\n\n try {\n ensureValidHandleRegex(groups.authority)\n } catch {\n try {\n ensureValidDidRegex(groups.authority)\n } catch {\n throw new Error('ATURI authority must be a valid handle or DID')\n }\n }\n\n if (groups.collection && !isValidNsid(groups.collection)) {\n throw new Error('ATURI collection path segment must be a valid NSID')\n }\n\n if (uri.length > 8 * 1024) {\n throw new Error('ATURI is far too long')\n }\n}\n"]}
|
package/dist/datetime.d.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/** An ISO 8601 formatted datetime string (YYYY-MM-DDTHH:mm:ss.sssZ) */
|
|
2
|
+
export type DatetimeString = `${string}-${string}-${string}T${string}:${string}:${string}${'Z' | `+${string}` | `-${string}`}`;
|
|
3
|
+
declare global {
|
|
4
|
+
interface Date {
|
|
5
|
+
toISOString(): `${string}-${string}-${string}T${string}:${string}:${string}Z`;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare function ensureValidDatetime(dtStr: string): asserts dtStr is DatetimeString;
|
|
9
|
+
export declare function isValidDatetime(dtStr: string): dtStr is DatetimeString;
|
|
10
|
+
export declare function normalizeDatetime(dtStr: string): DatetimeString;
|
|
11
|
+
export declare const normalizeDatetimeAlways: (dtStr: string) => DatetimeString;
|
|
5
12
|
export declare class InvalidDatetimeError extends Error {
|
|
6
13
|
}
|
|
7
14
|
//# sourceMappingURL=datetime.d.ts.map
|
package/dist/datetime.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,MAAM,MAAM,cAAc,GACxB,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,GAAG,GAAG,GAAG,IAAI,MAAM,EAAE,GAAG,IAAI,MAAM,EAAE,EAAE,CAAA;AAGnG,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,IAAI;QACZ,WAAW,IAAI,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,GAAG,CAAA;KAC9E;CACF;AAKD,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,KAAK,IAAI,cAAc,CA4BjC;AAID,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,cAAc,CAWtE;AAYD,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAkC/D;AAMD,eAAO,MAAM,uBAAuB,GAAI,OAAO,MAAM,KAAG,cASvD,CAAA;AAID,qBAAa,oBAAqB,SAAQ,KAAK;CAAG"}
|
package/dist/datetime.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InvalidDatetimeError = exports.normalizeDatetimeAlways =
|
|
3
|
+
exports.InvalidDatetimeError = exports.normalizeDatetimeAlways = void 0;
|
|
4
|
+
exports.ensureValidDatetime = ensureValidDatetime;
|
|
5
|
+
exports.isValidDatetime = isValidDatetime;
|
|
6
|
+
exports.normalizeDatetime = normalizeDatetime;
|
|
4
7
|
/* Validates datetime string against atproto Lexicon 'datetime' format.
|
|
5
8
|
* Syntax is described at: https://atproto.com/specs/lexicon#datetime
|
|
6
9
|
*/
|
|
7
|
-
|
|
10
|
+
function ensureValidDatetime(dtStr) {
|
|
8
11
|
const date = new Date(dtStr);
|
|
9
12
|
// must parse as ISO 8601; this also verifies semantics like month is not 13 or 00
|
|
10
13
|
if (isNaN(date.getTime())) {
|
|
@@ -26,13 +29,12 @@ const ensureValidDatetime = (dtStr) => {
|
|
|
26
29
|
if (dtStr.startsWith('000')) {
|
|
27
30
|
throw new InvalidDatetimeError('datetime so close to year zero not allowed');
|
|
28
31
|
}
|
|
29
|
-
}
|
|
30
|
-
exports.ensureValidDatetime = ensureValidDatetime;
|
|
32
|
+
}
|
|
31
33
|
/* Same logic as ensureValidDatetime(), but returns a boolean instead of throwing an exception.
|
|
32
34
|
*/
|
|
33
|
-
|
|
35
|
+
function isValidDatetime(dtStr) {
|
|
34
36
|
try {
|
|
35
|
-
|
|
37
|
+
ensureValidDatetime(dtStr);
|
|
36
38
|
}
|
|
37
39
|
catch (err) {
|
|
38
40
|
if (err instanceof InvalidDatetimeError) {
|
|
@@ -41,8 +43,7 @@ const isValidDatetime = (dtStr) => {
|
|
|
41
43
|
throw err;
|
|
42
44
|
}
|
|
43
45
|
return true;
|
|
44
|
-
}
|
|
45
|
-
exports.isValidDatetime = isValidDatetime;
|
|
46
|
+
}
|
|
46
47
|
/* Takes a flexible datetime string and normalizes representation.
|
|
47
48
|
*
|
|
48
49
|
* This function will work with any valid atproto datetime (eg, anything which isValidDatetime() is true for). It *additionally* is more flexible about accepting datetimes that don't comply to RFC 3339, or are missing timezone information, and normalizing them to a valid datetime.
|
|
@@ -53,10 +54,10 @@ exports.isValidDatetime = isValidDatetime;
|
|
|
53
54
|
*
|
|
54
55
|
* Expected output format: YYYY-MM-DDTHH:mm:ss.sssZ
|
|
55
56
|
*/
|
|
56
|
-
|
|
57
|
-
if (
|
|
57
|
+
function normalizeDatetime(dtStr) {
|
|
58
|
+
if (isValidDatetime(dtStr)) {
|
|
58
59
|
const outStr = new Date(dtStr).toISOString();
|
|
59
|
-
if (
|
|
60
|
+
if (isValidDatetime(outStr)) {
|
|
60
61
|
return outStr;
|
|
61
62
|
}
|
|
62
63
|
}
|
|
@@ -65,7 +66,7 @@ const normalizeDatetime = (dtStr) => {
|
|
|
65
66
|
const date = new Date(dtStr + 'Z');
|
|
66
67
|
if (!isNaN(date.getTime())) {
|
|
67
68
|
const tzStr = date.toISOString();
|
|
68
|
-
if (
|
|
69
|
+
if (isValidDatetime(tzStr)) {
|
|
69
70
|
return tzStr;
|
|
70
71
|
}
|
|
71
72
|
}
|
|
@@ -76,21 +77,20 @@ const normalizeDatetime = (dtStr) => {
|
|
|
76
77
|
throw new InvalidDatetimeError('datetime did not parse as any timestamp format');
|
|
77
78
|
}
|
|
78
79
|
const isoStr = date.toISOString();
|
|
79
|
-
if (
|
|
80
|
+
if (isValidDatetime(isoStr)) {
|
|
80
81
|
return isoStr;
|
|
81
82
|
}
|
|
82
83
|
else {
|
|
83
84
|
throw new InvalidDatetimeError('datetime normalized to invalid timestamp string');
|
|
84
85
|
}
|
|
85
|
-
}
|
|
86
|
-
exports.normalizeDatetime = normalizeDatetime;
|
|
86
|
+
}
|
|
87
87
|
/* Variant of normalizeDatetime() which always returns a valid datetime strings.
|
|
88
88
|
*
|
|
89
89
|
* If a InvalidDatetimeError is encountered, returns the UNIX epoch time as a UTC datetime (1970-01-01T00:00:00.000Z).
|
|
90
90
|
*/
|
|
91
91
|
const normalizeDatetimeAlways = (dtStr) => {
|
|
92
92
|
try {
|
|
93
|
-
return
|
|
93
|
+
return normalizeDatetime(dtStr);
|
|
94
94
|
}
|
|
95
95
|
catch (err) {
|
|
96
96
|
if (err instanceof InvalidDatetimeError) {
|
package/dist/datetime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datetime.js","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"datetime.js","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":";;;AAcA,kDA8BC;AAID,0CAWC;AAYD,8CAkCC;AA9FD;;GAEG;AACH,SAAgB,mBAAmB,CACjC,KAAa;IAEb,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAA;IAC5B,kFAAkF;IAClF,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,oBAAoB,CAAC,oCAAoC,CAAC,CAAA;IACtE,CAAC;IACD,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACvC,MAAM,IAAI,oBAAoB,CAAC,wCAAwC,CAAC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KAAK,GAAG,GAAG,CAAC,CAAA;QAClC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;YAC3B,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,EAAE,CAAA;YAChC,IAAI,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC3B,OAAO,KAAK,CAAA;YACd,CAAC;QACH,CAAC;IACH,CAAC;IAED,yCAAyC;IACzC,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAA;IAC5B,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,oBAAoB,CAC5B,gDAAgD,CACjD,CAAA;IACH,CAAC;IACD,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,CAAA;IACjC,IAAI,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,OAAO,MAAM,CAAA;IACf,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,oBAAoB,CAC5B,iDAAiD,CAClD,CAAA;IACH,CAAC;AACH,CAAC;AAED;;;GAGG;AACI,MAAM,uBAAuB,GAAG,CAAC,KAAa,EAAkB,EAAE;IACvE,IAAI,CAAC;QACH,OAAO,iBAAiB,CAAC,KAAK,CAAC,CAAA;IACjC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,oBAAoB,EAAE,CAAC;YACxC,OAAO,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAA;QAClC,CAAC;QACD,MAAM,GAAG,CAAA;IACX,CAAC;AACH,CAAC,CAAA;AATY,QAAA,uBAAuB,2BASnC;AAED;GACG;AACH,MAAa,oBAAqB,SAAQ,KAAK;CAAG;AAAlD,oDAAkD","sourcesContent":["/** An ISO 8601 formatted datetime string (YYYY-MM-DDTHH:mm:ss.sssZ) */\nexport type DatetimeString =\n `${string}-${string}-${string}T${string}:${string}:${string}${'Z' | `+${string}` | `-${string}`}`\n\n// Allow date.toISOString() to be used where datetime format is expected\ndeclare global {\n interface Date {\n toISOString(): `${string}-${string}-${string}T${string}:${string}:${string}Z`\n }\n}\n\n/* Validates datetime string against atproto Lexicon 'datetime' format.\n * Syntax is described at: https://atproto.com/specs/lexicon#datetime\n */\nexport function ensureValidDatetime(\n dtStr: string,\n): asserts dtStr is DatetimeString {\n const date = new Date(dtStr)\n // must parse as ISO 8601; this also verifies semantics like month is not 13 or 00\n if (isNaN(date.getTime())) {\n throw new InvalidDatetimeError('datetime did not parse as ISO 8601')\n }\n if (date.toISOString().startsWith('-')) {\n throw new InvalidDatetimeError('datetime normalized to a negative time')\n }\n // regex and other checks for RFC-3339\n if (\n !/^[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9](.[0-9]{1,20})?(Z|([+-][0-2][0-9]:[0-5][0-9]))$/.test(\n dtStr,\n )\n ) {\n throw new InvalidDatetimeError(\"datetime didn't validate via regex\")\n }\n if (dtStr.length > 64) {\n throw new InvalidDatetimeError('datetime is too long (64 chars max)')\n }\n if (dtStr.endsWith('-00:00')) {\n throw new InvalidDatetimeError(\n 'datetime can not use \"-00:00\" for UTC timezone',\n )\n }\n if (dtStr.startsWith('000')) {\n throw new InvalidDatetimeError('datetime so close to year zero not allowed')\n }\n}\n\n/* Same logic as ensureValidDatetime(), but returns a boolean instead of throwing an exception.\n */\nexport function isValidDatetime(dtStr: string): dtStr is DatetimeString {\n try {\n ensureValidDatetime(dtStr)\n } catch (err) {\n if (err instanceof InvalidDatetimeError) {\n return false\n }\n throw err\n }\n\n return true\n}\n\n/* Takes a flexible datetime string and normalizes representation.\n *\n * This function will work with any valid atproto datetime (eg, anything which isValidDatetime() is true for). It *additionally* is more flexible about accepting datetimes that don't comply to RFC 3339, or are missing timezone information, and normalizing them to a valid datetime.\n *\n * One use-case is a consistent, sortable string. Another is to work with older invalid createdAt datetimes.\n *\n * Successful output will be a valid atproto datetime with millisecond precision (3 sub-second digits) and UTC timezone with trailing 'Z' syntax. Throws `InvalidDatetimeError` if the input string could not be parsed as a datetime, even with permissive parsing.\n *\n * Expected output format: YYYY-MM-DDTHH:mm:ss.sssZ\n */\nexport function normalizeDatetime(dtStr: string): DatetimeString {\n if (isValidDatetime(dtStr)) {\n const outStr = new Date(dtStr).toISOString()\n if (isValidDatetime(outStr)) {\n return outStr\n }\n }\n\n // check if this permissive datetime is missing a timezone\n if (!/.*(([+-]\\d\\d:?\\d\\d)|[a-zA-Z])$/.test(dtStr)) {\n const date = new Date(dtStr + 'Z')\n if (!isNaN(date.getTime())) {\n const tzStr = date.toISOString()\n if (isValidDatetime(tzStr)) {\n return tzStr\n }\n }\n }\n\n // finally try parsing as simple datetime\n const date = new Date(dtStr)\n if (isNaN(date.getTime())) {\n throw new InvalidDatetimeError(\n 'datetime did not parse as any timestamp format',\n )\n }\n const isoStr = date.toISOString()\n if (isValidDatetime(isoStr)) {\n return isoStr\n } else {\n throw new InvalidDatetimeError(\n 'datetime normalized to invalid timestamp string',\n )\n }\n}\n\n/* Variant of normalizeDatetime() which always returns a valid datetime strings.\n *\n * If a InvalidDatetimeError is encountered, returns the UNIX epoch time as a UTC datetime (1970-01-01T00:00:00.000Z).\n */\nexport const normalizeDatetimeAlways = (dtStr: string): DatetimeString => {\n try {\n return normalizeDatetime(dtStr)\n } catch (err) {\n if (err instanceof InvalidDatetimeError) {\n return new Date(0).toISOString()\n }\n throw err\n }\n}\n\n/* Indicates a datetime string did not pass full atproto Lexicon datetime string format checks.\n */\nexport class InvalidDatetimeError extends Error {}\n"]}
|
package/dist/did.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export declare
|
|
1
|
+
export type DidString<M extends string = string> = `did:${M}:${string}`;
|
|
2
|
+
export declare function ensureValidDid(did: string): asserts did is DidString;
|
|
3
|
+
export declare function ensureValidDidRegex(did: string): asserts did is DidString;
|
|
3
4
|
export declare class InvalidDidError extends Error {
|
|
4
5
|
}
|
|
5
6
|
//# sourceMappingURL=did.d.ts.map
|
package/dist/did.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"did.d.ts","sourceRoot":"","sources":["../src/did.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"did.d.ts","sourceRoot":"","sources":["../src/did.ts"],"names":[],"mappings":"AAcA,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,OAAO,CAAC,IAAI,MAAM,EAAE,CAAA;AAEvE,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,IAAI,SAAS,CA8BpE;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,IAAI,SAAS,CAUzE;AAED,qBAAa,eAAgB,SAAQ,KAAK;CAAG"}
|
package/dist/did.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InvalidDidError = exports.ensureValidDidRegex = exports.ensureValidDid = void 0;
|
|
4
2
|
// Human-readable constraints:
|
|
5
3
|
// - valid W3C DID (https://www.w3.org/TR/did-core/#did-syntax)
|
|
6
4
|
// - entire URI is ASCII: [a-zA-Z0-9._:%-]
|
|
@@ -14,7 +12,11 @@ exports.InvalidDidError = exports.ensureValidDidRegex = exports.ensureValidDid =
|
|
|
14
12
|
// - in current atproto, only allowing did:plc and did:web. But not *forcing* this at lexicon layer
|
|
15
13
|
// - hard length limit of 8KBytes
|
|
16
14
|
// - not going to validate "percent encoding" here
|
|
17
|
-
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.InvalidDidError = void 0;
|
|
17
|
+
exports.ensureValidDid = ensureValidDid;
|
|
18
|
+
exports.ensureValidDidRegex = ensureValidDidRegex;
|
|
19
|
+
function ensureValidDid(did) {
|
|
18
20
|
if (!did.startsWith('did:')) {
|
|
19
21
|
throw new InvalidDidError('DID requires "did:" prefix');
|
|
20
22
|
}
|
|
@@ -35,9 +37,8 @@ const ensureValidDid = (did) => {
|
|
|
35
37
|
if (did.length > 2 * 1024) {
|
|
36
38
|
throw new InvalidDidError('DID is too long (2048 chars max)');
|
|
37
39
|
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const ensureValidDidRegex = (did) => {
|
|
40
|
+
}
|
|
41
|
+
function ensureValidDidRegex(did) {
|
|
41
42
|
// simple regex to enforce most constraints via just regex and length.
|
|
42
43
|
// hand wrote this regex based on above constraints
|
|
43
44
|
if (!/^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$/.test(did)) {
|
|
@@ -46,8 +47,7 @@ const ensureValidDidRegex = (did) => {
|
|
|
46
47
|
if (did.length > 2 * 1024) {
|
|
47
48
|
throw new InvalidDidError('DID is too long (2048 chars max)');
|
|
48
49
|
}
|
|
49
|
-
}
|
|
50
|
-
exports.ensureValidDidRegex = ensureValidDidRegex;
|
|
50
|
+
}
|
|
51
51
|
class InvalidDidError extends Error {
|
|
52
52
|
}
|
|
53
53
|
exports.InvalidDidError = InvalidDidError;
|
package/dist/did.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"did.js","sourceRoot":"","sources":["../src/did.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"did.js","sourceRoot":"","sources":["../src/did.ts"],"names":[],"mappings":";AAAA,8BAA8B;AAC9B,iEAAiE;AACjE,+CAA+C;AAC/C,2CAA2C;AAC3C,wEAAwE;AACxE,sFAAsF;AACtF,qFAAqF;AACrF,wHAAwH;AACxH,8GAA8G;AAC9G,6FAA6F;AAC7F,qGAAqG;AACrG,mCAAmC;AACnC,oDAAoD;;;AAIpD,wCA8BC;AAED,kDAUC;AA1CD,SAAgB,cAAc,CAAC,GAAW;IACxC,IAAI,C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ACH,CAAC;AAED,MAAa,eAAgB,SAAQ,KAAK;CAAG;AAA7C,0CAA6C","sourcesContent":["// Human-readable constraints:\n// - valid W3C DID (https://www.w3.org/TR/did-core/#did-syntax)\n// - entire URI is ASCII: [a-zA-Z0-9._:%-]\n// - always starts \"did:\" (lower-case)\n// - method name is one or more lower-case letters, followed by \":\"\n// - remaining identifier can have any of the above chars, but can not end in \":\"\n// - it seems that a bunch of \":\" can be included, and don't need spaces between\n// - \"%\" is used only for \"percent encoding\" and must be followed by two hex characters (and thus can't end in \"%\")\n// - query (\"?\") and fragment (\"#\") stuff is defined for \"DID URIs\", but not as part of identifier itself\n// - \"The current specification does not take a position on the maximum length of a DID\"\n// - in current atproto, only allowing did:plc and did:web. But not *forcing* this at lexicon layer\n// - hard length limit of 8KBytes\n// - not going to validate \"percent encoding\" here\n\nexport type DidString<M extends string = string> = `did:${M}:${string}`\n\nexport function ensureValidDid(did: string): asserts did is DidString {\n if (!did.startsWith('did:')) {\n throw new InvalidDidError('DID requires \"did:\" prefix')\n }\n\n // check that all chars are boring ASCII\n if (!/^[a-zA-Z0-9._:%-]*$/.test(did)) {\n throw new InvalidDidError(\n 'Disallowed characters in DID (ASCII letters, digits, and a couple other characters only)',\n )\n }\n\n const { length, 1: method } = did.split(':')\n if (length < 3) {\n throw new InvalidDidError(\n 'DID requires prefix, method, and method-specific content',\n )\n }\n\n if (!/^[a-z]+$/.test(method)) {\n throw new InvalidDidError('DID method must be lower-case letters')\n }\n\n if (did.endsWith(':') || did.endsWith('%')) {\n throw new InvalidDidError('DID can not end with \":\" or \"%\"')\n }\n\n if (did.length > 2 * 1024) {\n throw new InvalidDidError('DID is too long (2048 chars max)')\n }\n}\n\nexport function ensureValidDidRegex(did: string): asserts did is DidString {\n // simple regex to enforce most constraints via just regex and length.\n // hand wrote this regex based on above constraints\n if (!/^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$/.test(did)) {\n throw new InvalidDidError(\"DID didn't validate via regex\")\n }\n\n if (did.length > 2 * 1024) {\n throw new InvalidDidError('DID is too long (2048 chars max)')\n }\n}\n\nexport class InvalidDidError extends Error {}\n"]}
|
package/dist/handle.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export declare const INVALID_HANDLE = "handle.invalid";
|
|
2
|
+
export type HandleString = `${string}.${string}`;
|
|
2
3
|
export declare const DISALLOWED_TLDS: string[];
|
|
3
|
-
export declare
|
|
4
|
-
export declare
|
|
5
|
-
export declare
|
|
6
|
-
export declare
|
|
7
|
-
export declare
|
|
8
|
-
export declare
|
|
4
|
+
export declare function ensureValidHandle(handle: string): asserts handle is HandleString;
|
|
5
|
+
export declare function ensureValidHandleRegex(handle: string): asserts handle is HandleString;
|
|
6
|
+
export declare function normalizeHandle(handle: string): string;
|
|
7
|
+
export declare function normalizeAndEnsureValidHandle(handle: string): HandleString;
|
|
8
|
+
export declare function isValidHandle(handle: string): handle is HandleString;
|
|
9
|
+
export declare function isValidTld(handle: string): boolean;
|
|
9
10
|
export declare class InvalidHandleError extends Error {
|
|
10
11
|
}
|
|
11
12
|
/** @deprecated Never used */
|
package/dist/handle.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handle.d.ts","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"handle.d.ts","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,mBAAmB,CAAA;AAE9C,MAAM,MAAM,YAAY,GAAG,GAAG,MAAM,IAAI,MAAM,EAAE,CAAA;AAMhD,eAAO,MAAM,eAAe,UAY3B,CAAA;AAqBD,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,IAAI,YAAY,CAkChC;AAMD,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,IAAI,YAAY,CAOhC;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CAI1E;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,IAAI,YAAY,CAEpE;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAOlD;AAED,qBAAa,kBAAmB,SAAQ,KAAK;CAAG;AAChD,6BAA6B;AAC7B,qBAAa,mBAAoB,SAAQ,KAAK;CAAG;AACjD,6BAA6B;AAC7B,qBAAa,sBAAuB,SAAQ,KAAK;CAAG;AACpD,6BAA6B;AAC7B,qBAAa,qBAAsB,SAAQ,KAAK;CAAG"}
|
package/dist/handle.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DisallowedDomainError = exports.UnsupportedDomainError = exports.ReservedHandleError = exports.InvalidHandleError = exports.
|
|
3
|
+
exports.DisallowedDomainError = exports.UnsupportedDomainError = exports.ReservedHandleError = exports.InvalidHandleError = exports.DISALLOWED_TLDS = exports.INVALID_HANDLE = void 0;
|
|
4
|
+
exports.ensureValidHandle = ensureValidHandle;
|
|
5
|
+
exports.ensureValidHandleRegex = ensureValidHandleRegex;
|
|
6
|
+
exports.normalizeHandle = normalizeHandle;
|
|
7
|
+
exports.normalizeAndEnsureValidHandle = normalizeAndEnsureValidHandle;
|
|
8
|
+
exports.isValidHandle = isValidHandle;
|
|
9
|
+
exports.isValidTld = isValidTld;
|
|
4
10
|
exports.INVALID_HANDLE = 'handle.invalid';
|
|
5
11
|
// Currently these are registration-time restrictions, not protocol-level
|
|
6
12
|
// restrictions. We have a couple accounts in the wild that we need to clean up
|
|
@@ -38,7 +44,7 @@ exports.DISALLOWED_TLDS = [
|
|
|
38
44
|
// - does not validate whether domain or TLD exists, or is a reserved or
|
|
39
45
|
// special TLD (eg, .onion or .local)
|
|
40
46
|
// - does not validate punycode
|
|
41
|
-
|
|
47
|
+
function ensureValidHandle(handle) {
|
|
42
48
|
// check that all chars are boring ASCII
|
|
43
49
|
if (!/^[a-zA-Z0-9.-]*$/.test(handle)) {
|
|
44
50
|
throw new InvalidHandleError('Disallowed characters in handle (ASCII letters, digits, dashes, periods only)');
|
|
@@ -65,45 +71,36 @@ const ensureValidHandle = (handle) => {
|
|
|
65
71
|
throw new InvalidHandleError('Handle final component (TLD) must start with ASCII letter');
|
|
66
72
|
}
|
|
67
73
|
}
|
|
68
|
-
}
|
|
69
|
-
exports.ensureValidHandle = ensureValidHandle;
|
|
74
|
+
}
|
|
70
75
|
// simple regex translation of above constraints
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
throw new InvalidHandleError("Handle didn't validate via regex");
|
|
74
|
-
}
|
|
76
|
+
const HANDLE_REGEX = /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/;
|
|
77
|
+
function ensureValidHandleRegex(handle) {
|
|
75
78
|
if (handle.length > 253) {
|
|
76
79
|
throw new InvalidHandleError('Handle is too long (253 chars max)');
|
|
77
80
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
if (!HANDLE_REGEX.test(handle)) {
|
|
82
|
+
throw new InvalidHandleError("Handle didn't validate via regex");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function normalizeHandle(handle) {
|
|
81
86
|
return handle.toLowerCase();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
(0, exports.ensureValidHandle)(normalized);
|
|
87
|
+
}
|
|
88
|
+
function normalizeAndEnsureValidHandle(handle) {
|
|
89
|
+
const normalized = normalizeHandle(handle);
|
|
90
|
+
ensureValidHandle(normalized);
|
|
87
91
|
return normalized;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (err instanceof InvalidHandleError) {
|
|
92
|
+
}
|
|
93
|
+
function isValidHandle(handle) {
|
|
94
|
+
return handle.length <= 253 && HANDLE_REGEX.test(handle);
|
|
95
|
+
}
|
|
96
|
+
function isValidTld(handle) {
|
|
97
|
+
for (const tld of exports.DISALLOWED_TLDS) {
|
|
98
|
+
if (handle.endsWith(tld)) {
|
|
96
99
|
return false;
|
|
97
100
|
}
|
|
98
|
-
throw err;
|
|
99
101
|
}
|
|
100
102
|
return true;
|
|
101
|
-
}
|
|
102
|
-
exports.isValidHandle = isValidHandle;
|
|
103
|
-
const isValidTld = (handle) => {
|
|
104
|
-
return !exports.DISALLOWED_TLDS.some((domain) => handle.endsWith(domain));
|
|
105
|
-
};
|
|
106
|
-
exports.isValidTld = isValidTld;
|
|
103
|
+
}
|
|
107
104
|
class InvalidHandleError extends Error {
|
|
108
105
|
}
|
|
109
106
|
exports.InvalidHandleError = InvalidHandleError;
|
package/dist/handle.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handle.js","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"handle.js","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":";;;AAyCA,8CAoCC;AAMD,wDASC;AAED,0CAEC;AAED,sEAIC;AAED,sCAEC;AAED,gCAOC;AAnHY,QAAA,cAAc,GAAG,gBAAgB,CAAA;AAI9C,yEAAyE;AACzE,+EAA+E;AAC/E,wBAAwB;AACxB,4EAA4E;AAC/D,QAAA,eAAe,GAAG;IAC7B,QAAQ;IACR,OAAO;IACP,UAAU;IACV,YAAY;IACZ,WAAW;IACX,UAAU;IACV,MAAM;IACN,uDAAuD;IACvD,QAAQ;IACR,sEAAsE;IACtE,qEAAqE;CACtE,C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I,CAAA;AACb,CAAC;AAED,MAAa,kBAAmB,SAAQ,KAAK;CAAG;AAAhD,gDAAgD;AAChD,6BAA6B;AAC7B,MAAa,mBAAoB,SAAQ,KAAK;CAAG;AAAjD,kDAAiD;AACjD,6BAA6B;AAC7B,MAAa,sBAAuB,SAAQ,KAAK;CAAG;AAApD,wDAAoD;AACpD,6BAA6B;AAC7B,MAAa,qBAAsB,SAAQ,KAAK;CAAG;AAAnD,sDAAmD","sourcesContent":["export const INVALID_HANDLE = 'handle.invalid'\n\nexport type HandleString = `${string}.${string}`\n\n// Currently these are registration-time restrictions, not protocol-level\n// restrictions. We have a couple accounts in the wild that we need to clean up\n// before hard-disallow.\n// See also: https://en.wikipedia.org/wiki/Top-level_domain#Reserved_domains\nexport const DISALLOWED_TLDS = [\n '.local',\n '.arpa',\n '.invalid',\n '.localhost',\n '.internal',\n '.example',\n '.alt',\n // policy could concievably change on \".onion\" some day\n '.onion',\n // NOTE: .test is allowed in testing and devopment. In practical terms\n // \"should\" \"never\" actually resolve and get registered in production\n]\n\n// Handle constraints, in English:\n// - must be a possible domain name\n// - RFC-1035 is commonly referenced, but has been updated. eg, RFC-3696,\n// section 2. and RFC-3986, section 3. can now have leading numbers (eg,\n// 4chan.org)\n// - \"labels\" (sub-names) are made of ASCII letters, digits, hyphens\n// - can not start or end with a hyphen\n// - TLD (last component) should not start with a digit\n// - can't end with a hyphen (can end with digit)\n// - each segment must be between 1 and 63 characters (not including any periods)\n// - overall length can't be more than 253 characters\n// - separated by (ASCII) periods; does not start or end with period\n// - case insensitive\n// - domains (handles) are equal if they are the same lower-case\n// - punycode allowed for internationalization\n// - no whitespace, null bytes, joining chars, etc\n// - does not validate whether domain or TLD exists, or is a reserved or\n// special TLD (eg, .onion or .local)\n// - does not validate punycode\nexport function ensureValidHandle(\n handle: string,\n): asserts handle is HandleString {\n // check that all chars are boring ASCII\n if (!/^[a-zA-Z0-9.-]*$/.test(handle)) {\n throw new InvalidHandleError(\n 'Disallowed characters in handle (ASCII letters, digits, dashes, periods only)',\n )\n }\n\n if (handle.length > 253) {\n throw new InvalidHandleError('Handle is too long (253 chars max)')\n }\n const labels = handle.split('.')\n if (labels.length < 2) {\n throw new InvalidHandleError('Handle domain needs at least two parts')\n }\n for (let i = 0; i < labels.length; i++) {\n const l = labels[i]\n if (l.length < 1) {\n throw new InvalidHandleError('Handle parts can not be empty')\n }\n if (l.length > 63) {\n throw new InvalidHandleError('Handle part too long (max 63 chars)')\n }\n if (l.endsWith('-') || l.startsWith('-')) {\n throw new InvalidHandleError(\n 'Handle parts can not start or end with hyphens',\n )\n }\n if (i + 1 === labels.length && !/^[a-zA-Z]/.test(l)) {\n throw new InvalidHandleError(\n 'Handle final component (TLD) must start with ASCII letter',\n )\n }\n }\n}\n\n// simple regex translation of above constraints\nconst HANDLE_REGEX =\n /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/\n\nexport function ensureValidHandleRegex(\n handle: string,\n): asserts handle is HandleString {\n if (handle.length > 253) {\n throw new InvalidHandleError('Handle is too long (253 chars max)')\n }\n if (!HANDLE_REGEX.test(handle)) {\n throw new InvalidHandleError(\"Handle didn't validate via regex\")\n }\n}\n\nexport function normalizeHandle(handle: string): string {\n return handle.toLowerCase()\n}\n\nexport function normalizeAndEnsureValidHandle(handle: string): HandleString {\n const normalized = normalizeHandle(handle)\n ensureValidHandle(normalized)\n return normalized\n}\n\nexport function isValidHandle(handle: string): handle is HandleString {\n return handle.length <= 253 && HANDLE_REGEX.test(handle)\n}\n\nexport function isValidTld(handle: string): boolean {\n for (const tld of DISALLOWED_TLDS) {\n if (handle.endsWith(tld)) {\n return false\n }\n }\n return true\n}\n\nexport class InvalidHandleError extends Error {}\n/** @deprecated Never used */\nexport class ReservedHandleError extends Error {}\n/** @deprecated Never used */\nexport class UnsupportedDomainError extends Error {}\n/** @deprecated Never used */\nexport class DisallowedDomainError extends Error {}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export * from './handle';
|
|
2
|
-
export * from './did';
|
|
3
|
-
export * from './nsid';
|
|
4
|
-
export * from './aturi';
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
1
|
+
export * from './handle.js';
|
|
2
|
+
export * from './did.js';
|
|
3
|
+
export * from './nsid.js';
|
|
4
|
+
export * from './aturi.js';
|
|
5
|
+
export * from './at-identifier.js';
|
|
6
|
+
export * from './tid.js';
|
|
7
|
+
export * from './recordkey.js';
|
|
8
|
+
export * from './datetime.js';
|
|
8
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,oBAAoB,CAAA;AAClC,cAAc,UAAU,CAAA;AACxB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA"}
|