@atproto/syntax 0.4.1 → 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 +6 -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 +53 -57
- 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 +6 -5
- package/dist/nsid.d.ts.map +1 -1
- package/dist/nsid.js +1 -1
- 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 +60 -49
- 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 +8 -6
- package/src/recordkey.ts +40 -17
- package/src/tid.ts +4 -2
- package/tsconfig.build.json +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @atproto/syntax
|
|
2
2
|
|
|
3
|
+
## 0.4.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4389](https://github.com/bluesky-social/atproto/pull/4389) [`bcae2b7`](https://github.com/bluesky-social/atproto/commit/bcae2b77b68da6dc2ec202651c8bf41fd5769f69) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Improve typing of various string formats
|
|
8
|
+
|
|
3
9
|
## 0.4.1
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { DidString } from './did.js';
|
|
2
|
+
import { HandleString } from './handle.js';
|
|
3
|
+
export type AtIdentifierString = DidString | HandleString;
|
|
4
|
+
export declare function ensureValidAtIdentifier(input: string): asserts input is AtIdentifierString;
|
|
5
|
+
//# sourceMappingURL=at-identifier.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"at-identifier.d.ts","sourceRoot":"","sources":["../src/at-identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAuB,MAAM,UAAU,CAAA;AACzD,OAAO,EACL,YAAY,EAGb,MAAM,aAAa,CAAA;AAEpB,MAAM,MAAM,kBAAkB,GAAG,SAAS,GAAG,YAAY,CAAA;AAEzD,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,KAAK,IAAI,kBAAkB,CAUrC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureValidAtIdentifier = ensureValidAtIdentifier;
|
|
4
|
+
const did_js_1 = require("./did.js");
|
|
5
|
+
const handle_js_1 = require("./handle.js");
|
|
6
|
+
function ensureValidAtIdentifier(input) {
|
|
7
|
+
try {
|
|
8
|
+
if (input.startsWith('did:')) {
|
|
9
|
+
(0, did_js_1.ensureValidDidRegex)(input);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
(0, handle_js_1.ensureValidHandleRegex)(input);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
catch (cause) {
|
|
16
|
+
throw new handle_js_1.InvalidHandleError('Invalid DID or handle', { cause });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=at-identifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"at-identifier.js","sourceRoot":"","sources":["../src/at-identifier.ts"],"names":[],"mappings":";;AASA,0DAYC;AArBD,qCAAyD;AACzD,2CAIoB;AAIpB,SAAgB,uBAAuB,CACrC,KAAa;IAEb,IAAI,CAAC;QACH,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YAC7B,IAAA,4BAAmB,EAAC,KAAK,CAAC,CAAA;QAC5B,CAAC;aAAM,CAAC;YACN,IAAA,kCAAsB,EAAC,KAAK,CAAC,CAAA;QAC/B,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,8BAAkB,CAAC,uBAAuB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;IAClE,CAAC;AACH,CAAC","sourcesContent":["import { DidString, ensureValidDidRegex } from './did.js'\nimport {\n HandleString,\n InvalidHandleError,\n ensureValidHandleRegex,\n} from './handle.js'\n\nexport type AtIdentifierString = DidString | HandleString\n\nexport function ensureValidAtIdentifier(\n input: string,\n): asserts input is AtIdentifierString {\n try {\n if (input.startsWith('did:')) {\n ensureValidDidRegex(input)\n } else {\n ensureValidHandleRegex(input)\n }\n } catch (cause) {\n throw new InvalidHandleError('Invalid DID or handle', { cause })\n }\n}\n"]}
|
package/dist/aturi.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
import { AtIdentifierString } from './at-identifier.js';
|
|
2
|
+
import { AtUriString } from './aturi_validation.js';
|
|
3
|
+
export * from './aturi_validation.js';
|
|
2
4
|
export declare const ATP_URI_REGEX: RegExp;
|
|
3
5
|
export declare class AtUri {
|
|
4
6
|
hash: string;
|
|
5
|
-
host:
|
|
7
|
+
host: AtIdentifierString;
|
|
6
8
|
pathname: string;
|
|
7
9
|
searchParams: URLSearchParams;
|
|
8
|
-
constructor(uri: string, base?: string);
|
|
10
|
+
constructor(uri: string, base?: string | AtUri);
|
|
9
11
|
static make(handleOrDid: string, collection?: string, rkey?: string): AtUri;
|
|
10
12
|
get protocol(): string;
|
|
11
|
-
get origin(): string
|
|
13
|
+
get origin(): `at://did:${string}:${string}` | `at://${string}.${string}`;
|
|
12
14
|
get hostname(): string;
|
|
13
15
|
set hostname(v: string);
|
|
14
16
|
get search(): string;
|
|
@@ -17,7 +19,7 @@ export declare class AtUri {
|
|
|
17
19
|
set collection(v: string);
|
|
18
20
|
get rkey(): string;
|
|
19
21
|
set rkey(v: string);
|
|
20
|
-
get href():
|
|
21
|
-
toString():
|
|
22
|
+
get href(): AtUriString;
|
|
23
|
+
toString(): AtUriString;
|
|
22
24
|
}
|
|
23
25
|
//# sourceMappingURL=aturi.d.ts.map
|
package/dist/aturi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aturi.d.ts","sourceRoot":"","sources":["../src/aturi.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"aturi.d.ts","sourceRoot":"","sources":["../src/aturi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA2B,MAAM,oBAAoB,CAAA;AAChF,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAGnD,cAAc,uBAAuB,CAAA;AAErC,eAAO,MAAM,aAAa,QAEyE,CAAA;AAInG,qBAAa,KAAK;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,kBAAkB,CAAA;IACxB,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,EAAE,eAAe,CAAA;gBAEjB,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,KAAK;IAgB9C,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM;IAOnE,IAAI,QAAQ,WAEX;IAED,IAAI,MAAM,gEAET;IAED,IAAI,QAAQ,IAII,MAAM,CAFrB;IAED,IAAI,QAAQ,CAAC,CAAC,EAAE,MAAM,EAGrB;IAED,IAAI,MAAM,IAII,MAAM,CAFnB;IAED,IAAI,MAAM,CAAC,CAAC,EAAE,MAAM,EAEnB;IAED,IAAI,UAAU,IAII,MAAM,CAFvB;IAED,IAAI,UAAU,CAAC,CAAC,EAAE,MAAM,EAKvB;IAED,IAAI,IAAI,IAII,MAAM,CAFjB;IAED,IAAI,IAAI,CAAC,CAAC,EAAE,MAAM,EAKjB;IAED,IAAI,IAAI,gBAEP;IAED,QAAQ,IAAI,WAAW;CAexB"}
|
package/dist/aturi.js
CHANGED
|
@@ -15,7 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.AtUri = exports.ATP_URI_REGEX = void 0;
|
|
18
|
-
|
|
18
|
+
const at_identifier_js_1 = require("./at-identifier.js");
|
|
19
|
+
const nsid_js_1 = require("./nsid.js");
|
|
20
|
+
__exportStar(require("./aturi_validation.js"), exports);
|
|
19
21
|
exports.ATP_URI_REGEX =
|
|
20
22
|
// proto- --did-------------- --name---------------- --path---- --query-- --hash--
|
|
21
23
|
/^(at:\/\/)?((?:did:[a-z0-9:%-]+)|(?:[a-z0-9][a-z0-9.:-]*))(\/[^?#\s]*)?(\?[^#\s]+)?(#[^\s]+)?$/i;
|
|
@@ -47,27 +49,15 @@ class AtUri {
|
|
|
47
49
|
writable: true,
|
|
48
50
|
value: void 0
|
|
49
51
|
});
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (!relativep) {
|
|
58
|
-
throw new Error(`Invalid path: ${uri}`);
|
|
59
|
-
}
|
|
60
|
-
Object.assign(parsed, relativep);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
parsed = parse(uri);
|
|
64
|
-
if (!parsed) {
|
|
65
|
-
throw new Error(`Invalid at uri: ${uri}`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
this.hash = parsed.hash;
|
|
52
|
+
const parsed = base !== undefined
|
|
53
|
+
? typeof base === 'string'
|
|
54
|
+
? Object.assign(parse(base), parseRelative(uri))
|
|
55
|
+
: Object.assign({ host: base.host }, parseRelative(uri))
|
|
56
|
+
: parse(uri);
|
|
57
|
+
(0, at_identifier_js_1.ensureValidAtIdentifier)(parsed.host);
|
|
58
|
+
this.hash = parsed.hash ?? '';
|
|
69
59
|
this.host = parsed.host;
|
|
70
|
-
this.pathname = parsed.pathname;
|
|
60
|
+
this.pathname = parsed.pathname ?? '';
|
|
71
61
|
this.searchParams = parsed.searchParams;
|
|
72
62
|
}
|
|
73
63
|
static make(handleOrDid, collection, rkey) {
|
|
@@ -88,6 +78,7 @@ class AtUri {
|
|
|
88
78
|
return this.host;
|
|
89
79
|
}
|
|
90
80
|
set hostname(v) {
|
|
81
|
+
(0, at_identifier_js_1.ensureValidAtIdentifier)(v);
|
|
91
82
|
this.host = v;
|
|
92
83
|
}
|
|
93
84
|
get search() {
|
|
@@ -100,6 +91,7 @@ class AtUri {
|
|
|
100
91
|
return this.pathname.split('/').filter(Boolean)[0] || '';
|
|
101
92
|
}
|
|
102
93
|
set collection(v) {
|
|
94
|
+
(0, nsid_js_1.ensureValidNsid)(v);
|
|
103
95
|
const parts = this.pathname.split('/').filter(Boolean);
|
|
104
96
|
parts[0] = v;
|
|
105
97
|
this.pathname = parts.join('/');
|
|
@@ -109,8 +101,7 @@ class AtUri {
|
|
|
109
101
|
}
|
|
110
102
|
set rkey(v) {
|
|
111
103
|
const parts = this.pathname.split('/').filter(Boolean);
|
|
112
|
-
|
|
113
|
-
parts[0] = 'undefined';
|
|
104
|
+
parts[0] || (parts[0] = 'undefined');
|
|
114
105
|
parts[1] = v;
|
|
115
106
|
this.pathname = parts.join('/');
|
|
116
107
|
}
|
|
@@ -122,9 +113,9 @@ class AtUri {
|
|
|
122
113
|
if (!path.startsWith('/')) {
|
|
123
114
|
path = `/${path}`;
|
|
124
115
|
}
|
|
125
|
-
let qs =
|
|
126
|
-
if (
|
|
127
|
-
qs = `?${
|
|
116
|
+
let qs = '';
|
|
117
|
+
if (this.searchParams.size) {
|
|
118
|
+
qs = `?${this.searchParams.toString()}`;
|
|
128
119
|
}
|
|
129
120
|
let hash = this.hash;
|
|
130
121
|
if (hash && !hash.startsWith('#')) {
|
|
@@ -135,26 +126,26 @@ class AtUri {
|
|
|
135
126
|
}
|
|
136
127
|
exports.AtUri = AtUri;
|
|
137
128
|
function parse(str) {
|
|
138
|
-
const match = exports.ATP_URI_REGEX
|
|
139
|
-
if (match) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
129
|
+
const match = str.match(exports.ATP_URI_REGEX);
|
|
130
|
+
if (!match) {
|
|
131
|
+
throw new Error(`Invalid AT uri: ${str}`);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
host: match[2],
|
|
135
|
+
hash: match[5],
|
|
136
|
+
pathname: match[3],
|
|
137
|
+
searchParams: new URLSearchParams(match[4]),
|
|
138
|
+
};
|
|
148
139
|
}
|
|
149
140
|
function parseRelative(str) {
|
|
150
|
-
const match =
|
|
151
|
-
if (match) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
141
|
+
const match = str.match(RELATIVE_REGEX);
|
|
142
|
+
if (!match) {
|
|
143
|
+
throw new Error(`Invalid path: ${str}`);
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
hash: match[3],
|
|
147
|
+
pathname: match[1],
|
|
148
|
+
searchParams: new URLSearchParams(match[2]),
|
|
149
|
+
};
|
|
159
150
|
}
|
|
160
151
|
//# sourceMappingURL=aturi.js.map
|
package/dist/aturi.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aturi.js","sourceRoot":"","sources":["../src/aturi.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"aturi.js","sourceRoot":"","sources":["../src/aturi.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,yDAAgF;AAEhF,uCAA2C;AAE3C,wDAAqC;AAExB,QAAA,aAAa;AACxB,6FAA6F;AAC7F,iGAAiG,CAAA;AACnG,0DAA0D;AAC1D,MAAM,cAAc,GAAG,wCAAwC,CAAA;AAE/D,MAAa,KAAK;IAMhB,YAAY,GAAW,EAAE,IAAqB;QAL9C;;;;;WAAY;QACZ;;;;;WAAwB;QACxB;;;;;WAAgB;QAChB;;;;;WAA6B;QAG3B,MAAM,MAAM,GACV,IAAI,KAAK,SAAS;YAChB,CAAC,CAAC,OAAO,IAAI,KAAK,QAAQ;gBACxB,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC;gBAChD,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC;YAC1D,CAAC,CAAC,KAAK,CAAC,GA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ier.js'\nimport { AtUriString } from './aturi_validation.js'\nimport { ensureValidNsid } from './nsid.js'\n\nexport * from './aturi_validation.js'\n\nexport const ATP_URI_REGEX =\n // proto- --did-------------- --name---------------- --path---- --query-- --hash--\n /^(at:\\/\\/)?((?:did:[a-z0-9:%-]+)|(?:[a-z0-9][a-z0-9.:-]*))(\\/[^?#\\s]*)?(\\?[^#\\s]+)?(#[^\\s]+)?$/i\n// --path----- --query-- --hash--\nconst RELATIVE_REGEX = /^(\\/[^?#\\s]*)?(\\?[^#\\s]+)?(#[^\\s]+)?$/i\n\nexport class AtUri {\n hash: string\n host: AtIdentifierString\n pathname: string\n searchParams: URLSearchParams\n\n constructor(uri: string, base?: string | AtUri) {\n const parsed =\n base !== undefined\n ? typeof base === 'string'\n ? Object.assign(parse(base), parseRelative(uri))\n : Object.assign({ host: base.host }, parseRelative(uri))\n : parse(uri)\n\n ensureValidAtIdentifier(parsed.host)\n\n this.hash = parsed.hash ?? ''\n this.host = parsed.host\n this.pathname = parsed.pathname ?? ''\n this.searchParams = parsed.searchParams\n }\n\n static make(handleOrDid: string, collection?: string, rkey?: string) {\n let str = handleOrDid\n if (collection) str += '/' + collection\n if (rkey) str += '/' + rkey\n return new AtUri(str)\n }\n\n get protocol() {\n return 'at:'\n }\n\n get origin() {\n return `at://${this.host}` as const\n }\n\n get hostname() {\n return this.host\n }\n\n set hostname(v: string) {\n ensureValidAtIdentifier(v)\n this.host = v\n }\n\n get search() {\n return this.searchParams.toString()\n }\n\n set search(v: string) {\n this.searchParams = new URLSearchParams(v)\n }\n\n get collection() {\n return this.pathname.split('/').filter(Boolean)[0] || ''\n }\n\n set collection(v: string) {\n ensureValidNsid(v)\n const parts = this.pathname.split('/').filter(Boolean)\n parts[0] = v\n this.pathname = parts.join('/')\n }\n\n get rkey() {\n return this.pathname.split('/').filter(Boolean)[1] || ''\n }\n\n set rkey(v: string) {\n const parts = this.pathname.split('/').filter(Boolean)\n parts[0] ||= 'undefined'\n parts[1] = v\n this.pathname = parts.join('/')\n }\n\n get href() {\n return this.toString()\n }\n\n toString(): AtUriString {\n let path = this.pathname || '/'\n if (!path.startsWith('/')) {\n path = `/${path}`\n }\n let qs = ''\n if (this.searchParams.size) {\n qs = `?${this.searchParams.toString()}`\n }\n let hash = this.hash\n if (hash && !hash.startsWith('#')) {\n hash = `#${hash}`\n }\n return `at://${this.host}${path}${qs}${hash}` as AtUriString\n }\n}\n\nfunction parse(str: string) {\n const match = str.match(ATP_URI_REGEX) as null | {\n 0: string\n 1: string | undefined // proto\n 2: string // host\n 3: string | undefined // path\n 4: string | undefined // query\n 5: string | undefined // hash\n }\n\n if (!match) {\n throw new Error(`Invalid AT uri: ${str}`)\n }\n\n return {\n host: match[2],\n hash: match[5],\n pathname: match[3],\n searchParams: new URLSearchParams(match[4]),\n }\n}\n\nfunction parseRelative(str: string) {\n const match = str.match(RELATIVE_REGEX) as null | {\n 0: string\n 1: string | undefined // path\n 2: string | undefined // query\n 3: string | undefined // hash\n }\n\n if (!match) {\n throw new Error(`Invalid path: ${str}`)\n }\n\n return {\n hash: match[3],\n pathname: match[1],\n searchParams: new URLSearchParams(match[2]),\n }\n}\n"]}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { AtIdentifierString } from './at-identifier.js';
|
|
2
|
+
import { NsidString } from './nsid.js';
|
|
3
|
+
export type AtUriString = `at://${AtIdentifierString}` | `at://${AtIdentifierString}/${NsidString}` | `at://${AtIdentifierString}/${NsidString}/${string}`;
|
|
4
|
+
export declare function ensureValidAtUri(input: string): asserts input is AtUriString;
|
|
5
|
+
export declare function ensureValidAtUriRegex(uri: string): asserts uri is AtUriString;
|
|
3
6
|
//# sourceMappingURL=aturi_validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aturi_validation.d.ts","sourceRoot":"","sources":["../src/aturi_validation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"aturi_validation.d.ts","sourceRoot":"","sources":["../src/aturi_validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA2B,MAAM,oBAAoB,CAAA;AAGhF,OAAO,EAAE,UAAU,EAAe,MAAM,WAAW,CAAA;AAEnD,MAAM,MAAM,WAAW,GACnB,QAAQ,kBAAkB,EAAE,GAC5B,QAAQ,kBAAkB,IAAI,UAAU,EAAE,GAC1C,QAAQ,kBAAkB,IAAI,UAAU,IAAI,MAAM,EAAE,CAAA;AAgBxD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,IAAI,WAAW,CAiF5E;AAED,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,IAAI,WAAW,CA4B7E"}
|
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,71 +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
|
-
if (!(0,
|
|
65
|
+
if (!(0, nsid_js_1.isValidNsid)(collection)) {
|
|
55
66
|
throw new Error('ATURI requires first path segment (if supplied) to be valid NSID');
|
|
56
67
|
}
|
|
57
68
|
}
|
|
58
|
-
|
|
59
|
-
|
|
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) {
|
|
60
73
|
throw new Error('ATURI can not have a slash after collection, unless record key is provided');
|
|
61
74
|
}
|
|
62
75
|
// would validate rkey here, but there are basically no constraints!
|
|
63
76
|
}
|
|
64
|
-
if (
|
|
77
|
+
if (recordKeyEnd !== -1) {
|
|
65
78
|
throw new Error('ATURI path can have at most two parts, and no trailing slash');
|
|
66
79
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
if (fragmentPart != null) {
|
|
71
|
-
if (fragmentPart.length === 0 || fragmentPart[0] !== '/') {
|
|
72
|
-
throw new Error('ATURI fragment must be non-empty and start with slash');
|
|
73
|
-
}
|
|
74
|
-
// NOTE: enforcing *some* checks here for sanity. Eg, at least no whitespace
|
|
75
|
-
if (!/^\/[a-zA-Z0-9._~:@!$&')(*+,;=%[\]/-]*$/.test(fragmentPart)) {
|
|
76
|
-
throw new Error('Disallowed characters in ATURI fragment (ASCII)');
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (uri.length > 8 * 1024) {
|
|
80
|
-
throw new Error('ATURI is far too long');
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
exports.ensureValidAtUri = ensureValidAtUri;
|
|
84
|
-
const ensureValidAtUriRegex = (uri) => {
|
|
80
|
+
}
|
|
81
|
+
function ensureValidAtUriRegex(uri) {
|
|
85
82
|
// simple regex to enforce most constraints via just regex and length.
|
|
86
83
|
// hand wrote this regex based on above constraints. whew!
|
|
87
84
|
const aturiRegex = /^at:\/\/(?<authority>[a-zA-Z0-9._:%-]+)(\/(?<collection>[a-zA-Z0-9-.]+)(\/(?<rkey>[a-zA-Z0-9._~:@!$&%')(*+,;=-]+))?)?(#(?<fragment>\/[a-zA-Z0-9._~:@!$&%')(*+,;=\-[\]/\\]*))?$/;
|
|
@@ -91,22 +88,21 @@ const ensureValidAtUriRegex = (uri) => {
|
|
|
91
88
|
}
|
|
92
89
|
const groups = rm.groups;
|
|
93
90
|
try {
|
|
94
|
-
(0,
|
|
91
|
+
(0, handle_js_1.ensureValidHandleRegex)(groups.authority);
|
|
95
92
|
}
|
|
96
93
|
catch {
|
|
97
94
|
try {
|
|
98
|
-
(0,
|
|
95
|
+
(0, did_js_1.ensureValidDidRegex)(groups.authority);
|
|
99
96
|
}
|
|
100
97
|
catch {
|
|
101
98
|
throw new Error('ATURI authority must be a valid handle or DID');
|
|
102
99
|
}
|
|
103
100
|
}
|
|
104
|
-
if (groups.collection && !(0,
|
|
101
|
+
if (groups.collection && !(0, nsid_js_1.isValidNsid)(groups.collection)) {
|
|
105
102
|
throw new Error('ATURI collection path segment must be a valid NSID');
|
|
106
103
|
}
|
|
107
104
|
if (uri.length > 8 * 1024) {
|
|
108
105
|
throw new Error('ATURI is far too long');
|
|
109
106
|
}
|
|
110
|
-
}
|
|
111
|
-
exports.ensureValidAtUriRegex = ensureValidAtUriRegex;
|
|
107
|
+
}
|
|
112
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,CA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CAAC;IAED,IAAI,YAAY,KAAK,CAAC,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,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,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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
|