@atproto/syntax 0.5.0 → 0.5.1
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 +49 -2
- package/dist/at-identifier.d.ts.map +1 -1
- package/dist/at-identifier.js +68 -6
- package/dist/at-identifier.js.map +1 -1
- package/dist/aturi.d.ts +2 -1
- package/dist/aturi.d.ts.map +1 -1
- package/dist/aturi.js +4 -3
- package/dist/aturi.js.map +1 -1
- package/package.json +1 -1
- package/src/at-identifier.ts +77 -6
- package/src/aturi.ts +19 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @atproto/syntax
|
|
2
2
|
|
|
3
|
+
## 0.5.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`67eb0c1`](https://github.com/bluesky-social/atproto/commit/67eb0c19ac415e762e221b2ccda9f0bcf7b3dd6f) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Improve efficiency of `AtUri` `did` getter and typing of `hostname` property
|
|
8
|
+
|
|
3
9
|
## 0.5.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/dist/at-identifier.d.ts
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
import { DidString } from './did.js';
|
|
2
2
|
import { HandleString } from './handle.js';
|
|
3
|
+
/**
|
|
4
|
+
* An "at-identifier" string - either a {@link DidString} or a {@link HandleString}
|
|
5
|
+
*
|
|
6
|
+
* @example `"did:plc:1234..."`, `"did:web:example.com"` or `"alice.bsky.social"`
|
|
7
|
+
*/
|
|
3
8
|
export type AtIdentifierString = DidString | HandleString;
|
|
4
|
-
|
|
5
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Discriminates {@link HandleString} from a valid {@link AtIdentifierString}.
|
|
11
|
+
*
|
|
12
|
+
* @return `true` if the identifier is a handle, `false` otherwise
|
|
13
|
+
*/
|
|
14
|
+
export declare function isHandleIdentifier(id: AtIdentifierString): id is HandleString;
|
|
15
|
+
/**
|
|
16
|
+
* Discriminates {@link DidString} from a valid {@link AtIdentifierString}.
|
|
17
|
+
*
|
|
18
|
+
* @return `true` if the identifier is a DID, `false` otherwise
|
|
19
|
+
*/
|
|
20
|
+
export declare function isDidIdentifier(id: AtIdentifierString): id is DidString;
|
|
21
|
+
/**
|
|
22
|
+
* Validates that a string is a valid {@link AtIdentifierString} format string,
|
|
23
|
+
* throwing an error if it is not.
|
|
24
|
+
*
|
|
25
|
+
* @throws InvalidHandleError if the input string does not meet the atproto 'datetime' format requirements.
|
|
26
|
+
* @see {@link AtIdentifierString}
|
|
27
|
+
*/
|
|
28
|
+
export declare function assertAtIdentifierString<I>(input: I): asserts input is I & AtIdentifierString;
|
|
29
|
+
/**
|
|
30
|
+
* Casts a string to a {@link AtIdentifierString} if it is a valid at-identifier
|
|
31
|
+
* string, throwing an error if it is not.
|
|
32
|
+
*
|
|
33
|
+
* @throws InvalidHandleError if the input string does not meet the atproto 'at-identifier' format requirements.
|
|
34
|
+
* @see {@link AtIdentifierString}
|
|
35
|
+
*/
|
|
36
|
+
export declare function asAtIdentifierString<I>(input: I): I & AtIdentifierString;
|
|
37
|
+
/**
|
|
38
|
+
* Type guard that checks if a value is a valid AT identifier (DID or handle).
|
|
39
|
+
*
|
|
40
|
+
* @param value - The value to check
|
|
41
|
+
* @returns `true` if the value is a valid AT identifier
|
|
42
|
+
* @see {@link AtIdentifierString}
|
|
43
|
+
*/
|
|
44
|
+
export declare function isAtIdentifierString<I>(input: I): input is I & AtIdentifierString;
|
|
45
|
+
/**
|
|
46
|
+
* Returns the input if it is a valid {@link AtIdentifierString} format string, or
|
|
47
|
+
* `undefined` if it is not.
|
|
48
|
+
*
|
|
49
|
+
* @see {@link AtIdentifierString}
|
|
50
|
+
*/
|
|
51
|
+
export declare function ifAtIdentifierString<I>(input: I): undefined | (I & AtIdentifierString);
|
|
52
|
+
export { assertAtIdentifierString as ensureValidAtIdentifier, isAtIdentifierString as isValidAtIdentifier, };
|
|
6
53
|
//# sourceMappingURL=at-identifier.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"at-identifier.d.ts","sourceRoot":"","sources":["../src/at-identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAmC,MAAM,UAAU,CAAA;AACrE,OAAO,EACL,YAAY,EAIb,MAAM,aAAa,CAAA;AAEpB,MAAM,MAAM,kBAAkB,GAAG,SAAS,GAAG,YAAY,CAAA;AAEzD,wBAAgB,
|
|
1
|
+
{"version":3,"file":"at-identifier.d.ts","sourceRoot":"","sources":["../src/at-identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAmC,MAAM,UAAU,CAAA;AACrE,OAAO,EACL,YAAY,EAIb,MAAM,aAAa,CAAA;AAEpB;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,SAAS,GAAG,YAAY,CAAA;AAEzD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,kBAAkB,GAAG,EAAE,IAAI,YAAY,CAE7E;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,kBAAkB,GAAG,EAAE,IAAI,SAAS,CAEvE;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EACxC,KAAK,EAAE,CAAC,GACP,OAAO,CAAC,KAAK,IAAI,CAAC,GAAG,kBAAkB,CAYzC;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,GAAG,kBAAkB,CAGxE;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,KAAK,EAAE,CAAC,GACP,KAAK,IAAI,CAAC,GAAG,kBAAkB,CAQjC;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,KAAK,EAAE,CAAC,GACP,SAAS,GAAG,CAAC,CAAC,GAAG,kBAAkB,CAAC,CAEtC;AAGD,OAAO,EACL,wBAAwB,IAAI,uBAAuB,EACnD,oBAAoB,IAAI,mBAAmB,GAC5C,CAAA"}
|
package/dist/at-identifier.js
CHANGED
|
@@ -1,12 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
3
|
+
exports.isHandleIdentifier = isHandleIdentifier;
|
|
4
|
+
exports.isDidIdentifier = isDidIdentifier;
|
|
5
|
+
exports.assertAtIdentifierString = assertAtIdentifierString;
|
|
6
|
+
exports.ensureValidAtIdentifier = assertAtIdentifierString;
|
|
7
|
+
exports.asAtIdentifierString = asAtIdentifierString;
|
|
8
|
+
exports.isAtIdentifierString = isAtIdentifierString;
|
|
9
|
+
exports.isValidAtIdentifier = isAtIdentifierString;
|
|
10
|
+
exports.ifAtIdentifierString = ifAtIdentifierString;
|
|
5
11
|
const did_js_1 = require("./did.js");
|
|
6
12
|
const handle_js_1 = require("./handle.js");
|
|
7
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Discriminates {@link HandleString} from a valid {@link AtIdentifierString}.
|
|
15
|
+
*
|
|
16
|
+
* @return `true` if the identifier is a handle, `false` otherwise
|
|
17
|
+
*/
|
|
18
|
+
function isHandleIdentifier(id) {
|
|
19
|
+
return !isDidIdentifier(id);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Discriminates {@link DidString} from a valid {@link AtIdentifierString}.
|
|
23
|
+
*
|
|
24
|
+
* @return `true` if the identifier is a DID, `false` otherwise
|
|
25
|
+
*/
|
|
26
|
+
function isDidIdentifier(id) {
|
|
27
|
+
return id.startsWith('did:');
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Validates that a string is a valid {@link AtIdentifierString} format string,
|
|
31
|
+
* throwing an error if it is not.
|
|
32
|
+
*
|
|
33
|
+
* @throws InvalidHandleError if the input string does not meet the atproto 'datetime' format requirements.
|
|
34
|
+
* @see {@link AtIdentifierString}
|
|
35
|
+
*/
|
|
36
|
+
function assertAtIdentifierString(input) {
|
|
8
37
|
try {
|
|
9
|
-
if (input
|
|
38
|
+
if (!input || typeof input !== 'string') {
|
|
39
|
+
throw new TypeError('Identifier must be a non-empty string');
|
|
40
|
+
}
|
|
41
|
+
else if (input.startsWith('did:')) {
|
|
10
42
|
(0, did_js_1.ensureValidDidRegex)(input);
|
|
11
43
|
}
|
|
12
44
|
else {
|
|
@@ -17,12 +49,42 @@ function ensureValidAtIdentifier(input) {
|
|
|
17
49
|
throw new handle_js_1.InvalidHandleError('Invalid DID or handle', { cause });
|
|
18
50
|
}
|
|
19
51
|
}
|
|
20
|
-
|
|
21
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Casts a string to a {@link AtIdentifierString} if it is a valid at-identifier
|
|
54
|
+
* string, throwing an error if it is not.
|
|
55
|
+
*
|
|
56
|
+
* @throws InvalidHandleError if the input string does not meet the atproto 'at-identifier' format requirements.
|
|
57
|
+
* @see {@link AtIdentifierString}
|
|
58
|
+
*/
|
|
59
|
+
function asAtIdentifierString(input) {
|
|
60
|
+
assertAtIdentifierString(input);
|
|
61
|
+
return input;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Type guard that checks if a value is a valid AT identifier (DID or handle).
|
|
65
|
+
*
|
|
66
|
+
* @param value - The value to check
|
|
67
|
+
* @returns `true` if the value is a valid AT identifier
|
|
68
|
+
* @see {@link AtIdentifierString}
|
|
69
|
+
*/
|
|
70
|
+
function isAtIdentifierString(input) {
|
|
71
|
+
if (!input || typeof input !== 'string') {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
else if (input.startsWith('did:')) {
|
|
22
75
|
return (0, did_js_1.isValidDid)(input);
|
|
23
76
|
}
|
|
24
77
|
else {
|
|
25
78
|
return (0, handle_js_1.isValidHandle)(input);
|
|
26
79
|
}
|
|
27
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Returns the input if it is a valid {@link AtIdentifierString} format string, or
|
|
83
|
+
* `undefined` if it is not.
|
|
84
|
+
*
|
|
85
|
+
* @see {@link AtIdentifierString}
|
|
86
|
+
*/
|
|
87
|
+
function ifAtIdentifierString(input) {
|
|
88
|
+
return isAtIdentifierString(input) ? input : undefined;
|
|
89
|
+
}
|
|
28
90
|
//# sourceMappingURL=at-identifier.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"at-identifier.js","sourceRoot":"","sources":["../src/at-identifier.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"at-identifier.js","sourceRoot":"","sources":["../src/at-identifier.ts"],"names":[],"mappings":";;AAoBA,gDAEC;AAOD,0CAEC;AASD,4DAcC;AA+C6B,2DAAuB;AAtCrD,oDAGC;AASD,oDAUC;AAiByB,mDAAmB;AAT7C,oDAIC;AAjGD,qCAAqE;AACrE,2CAKoB;AASpB;;;;GAIG;AACH,SAAgB,kBAAkB,CAAC,EAAsB;IACvD,OAAO,CAAC,eAAe,CAAC,EAAE,CAAC,CAAA;AAC7B,CAAC;AAED;;;;GAIG;AACH,SAAgB,eAAe,CAAC,EAAsB;IACpD,OAAO,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;AAC9B,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,wBAAwB,CACtC,KAAQ;IAER,IAAI,CAAC;QACH,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YACxC,MAAM,IAAI,SAAS,CAAC,uCAAuC,CAAC,CAAA;QAC9D,CAAC;aAAM,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YACpC,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;AAED;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAAI,KAAQ;IAC9C,wBAAwB,CAAC,KAAK,CAAC,CAAA;IAC/B,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAClC,KAAQ;IAER,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,OAAO,KAAK,CAAA;IACd,CAAC;SAAM,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QACpC,OAAO,IAAA,mBAAU,EAAC,KAAK,CAAC,CAAA;IAC1B,CAAC;SAAM,CAAC;QACN,OAAO,IAAA,yBAAa,EAAC,KAAK,CAAC,CAAA;IAC7B,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAQ;IAER,OAAO,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AACxD,CAAC","sourcesContent":["import { DidString, ensureValidDidRegex, isValidDid } from './did.js'\nimport {\n HandleString,\n InvalidHandleError,\n ensureValidHandleRegex,\n isValidHandle,\n} from './handle.js'\n\n/**\n * An \"at-identifier\" string - either a {@link DidString} or a {@link HandleString}\n *\n * @example `\"did:plc:1234...\"`, `\"did:web:example.com\"` or `\"alice.bsky.social\"`\n */\nexport type AtIdentifierString = DidString | HandleString\n\n/**\n * Discriminates {@link HandleString} from a valid {@link AtIdentifierString}.\n *\n * @return `true` if the identifier is a handle, `false` otherwise\n */\nexport function isHandleIdentifier(id: AtIdentifierString): id is HandleString {\n return !isDidIdentifier(id)\n}\n\n/**\n * Discriminates {@link DidString} from a valid {@link AtIdentifierString}.\n *\n * @return `true` if the identifier is a DID, `false` otherwise\n */\nexport function isDidIdentifier(id: AtIdentifierString): id is DidString {\n return id.startsWith('did:')\n}\n\n/**\n * Validates that a string is a valid {@link AtIdentifierString} format string,\n * throwing an error if it is not.\n *\n * @throws InvalidHandleError if the input string does not meet the atproto 'datetime' format requirements.\n * @see {@link AtIdentifierString}\n */\nexport function assertAtIdentifierString<I>(\n input: I,\n): asserts input is I & AtIdentifierString {\n try {\n if (!input || typeof input !== 'string') {\n throw new TypeError('Identifier must be a non-empty string')\n } else 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\n/**\n * Casts a string to a {@link AtIdentifierString} if it is a valid at-identifier\n * string, throwing an error if it is not.\n *\n * @throws InvalidHandleError if the input string does not meet the atproto 'at-identifier' format requirements.\n * @see {@link AtIdentifierString}\n */\nexport function asAtIdentifierString<I>(input: I): I & AtIdentifierString {\n assertAtIdentifierString(input)\n return input\n}\n\n/**\n * Type guard that checks if a value is a valid AT identifier (DID or handle).\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid AT identifier\n * @see {@link AtIdentifierString}\n */\nexport function isAtIdentifierString<I>(\n input: I,\n): input is I & AtIdentifierString {\n if (!input || typeof input !== 'string') {\n return false\n } else if (input.startsWith('did:')) {\n return isValidDid(input)\n } else {\n return isValidHandle(input)\n }\n}\n\n/**\n * Returns the input if it is a valid {@link AtIdentifierString} format string, or\n * `undefined` if it is not.\n *\n * @see {@link AtIdentifierString}\n */\nexport function ifAtIdentifierString<I>(\n input: I,\n): undefined | (I & AtIdentifierString) {\n return isAtIdentifierString(input) ? input : undefined\n}\n\n// Legacy exports (should we deprecate these ?)\nexport {\n assertAtIdentifierString as ensureValidAtIdentifier,\n isAtIdentifierString as isValidAtIdentifier,\n}\n"]}
|
package/dist/aturi.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { DidString } from './did.js';
|
|
|
4
4
|
import { NsidString } from './nsid.js';
|
|
5
5
|
import { RecordKeyString } from './recordkey.js';
|
|
6
6
|
export * from './aturi_validation.js';
|
|
7
|
+
export type { AtIdentifierString, AtUriString, DidString, NsidString, RecordKeyString, };
|
|
7
8
|
export declare const ATP_URI_REGEX: RegExp;
|
|
8
9
|
export declare class AtUri {
|
|
9
10
|
hash: string;
|
|
@@ -15,7 +16,7 @@ export declare class AtUri {
|
|
|
15
16
|
get protocol(): string;
|
|
16
17
|
get origin(): `at://did:${string}:${string}` | `at://${string}.${string}`;
|
|
17
18
|
get did(): DidString;
|
|
18
|
-
get hostname():
|
|
19
|
+
get hostname(): AtIdentifierString;
|
|
19
20
|
set hostname(v: string);
|
|
20
21
|
get search(): string;
|
|
21
22
|
set search(v: string);
|
package/dist/aturi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aturi.d.ts","sourceRoot":"","sources":["../src/aturi.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"aturi.d.ts","sourceRoot":"","sources":["../src/aturi.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAGnB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAmB,MAAM,UAAU,CAAA;AACrD,OAAO,EAAE,UAAU,EAAmB,MAAM,WAAW,CAAA;AACvD,OAAO,EAAE,eAAe,EAAwB,MAAM,gBAAgB,CAAA;AAEtE,cAAc,uBAAuB,CAAA;AAGrC,YAAY,EACV,kBAAkB,EAClB,WAAW,EACX,SAAS,EACT,UAAU,EACV,eAAe,GAChB,CAAA;AAED,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,GAAG,IAAI,SAAS,CAInB;IAED,IAAI,QAAQ,IAAI,kBAAkB,CAEjC;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,IAUI,MAAM,CARvB;IAED,IAAI,cAAc,IAAI,UAAU,CAI/B;IAED,IAAI,UAAU,CAAC,CAAC,EAAE,MAAM,EAGvB;IAED,qBAAqB,CAAC,CAAC,EAAE,MAAM;IAM/B,IAAI,IAAI,IAUI,MAAM,CARjB;IAED,IAAI,QAAQ,IAAI,eAAe,CAI9B;IAED,IAAI,IAAI,CAAC,CAAC,EAAE,MAAM,EAGjB;IAED,eAAe,CAAC,CAAC,EAAE,MAAM;IAOzB,IAAI,IAAI,gBAEP;IAED,QAAQ,IAAI,WAAW;CAexB"}
|
package/dist/aturi.js
CHANGED
|
@@ -44,9 +44,10 @@ class AtUri {
|
|
|
44
44
|
return `at://${this.host}`;
|
|
45
45
|
}
|
|
46
46
|
get did() {
|
|
47
|
-
const {
|
|
48
|
-
(0,
|
|
49
|
-
|
|
47
|
+
const { host } = this;
|
|
48
|
+
if ((0, at_identifier_js_1.isDidIdentifier)(host))
|
|
49
|
+
return host;
|
|
50
|
+
throw new did_js_1.InvalidDidError(`AtUri "${this}" does not have a DID hostname`);
|
|
50
51
|
}
|
|
51
52
|
get hostname() {
|
|
52
53
|
return this.host;
|
package/dist/aturi.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aturi.js","source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AAC,CAAA;IACzC,CAAC;IAED,OAAO;QACL,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;QACd,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC;QAClB,YAAY,EAAE,IAAI,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5C,CAAA;AACH,CAAC","sourcesContent":["import {\n AtIdentifierString,\n ensureValidAtIdentifier,\n isDidIdentifier,\n} from './at-identifier.js'\nimport { AtUriString } from './aturi_validation.js'\nimport { DidString, InvalidDidError } from './did.js'\nimport { NsidString, ensureValidNsid } from './nsid.js'\nimport { RecordKeyString, ensureValidRecordKey } from './recordkey.js'\n\nexport * from './aturi_validation.js'\n\n// Re-export types used in public interface\nexport type {\n AtIdentifierString,\n AtUriString,\n DidString,\n NsidString,\n RecordKeyString,\n}\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 did(): DidString {\n const { host } = this\n if (isDidIdentifier(host)) return host\n throw new InvalidDidError(`AtUri \"${this}\" does not have a DID hostname`)\n }\n\n get hostname(): AtIdentifierString {\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 get collectionSafe(): NsidString {\n const { collection } = this\n ensureValidNsid(collection)\n return collection\n }\n\n set collection(v: string) {\n ensureValidNsid(v)\n this.unsafelySetCollection(v)\n }\n\n unsafelySetCollection(v: string) {\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 get rkeySafe(): RecordKeyString {\n const { rkey } = this\n ensureValidRecordKey(rkey)\n return rkey\n }\n\n set rkey(v: string) {\n ensureValidRecordKey(v)\n this.unsafelySetRkey(v)\n }\n\n unsafelySetRkey(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"]}
|
package/package.json
CHANGED
package/src/at-identifier.ts
CHANGED
|
@@ -6,13 +6,45 @@ import {
|
|
|
6
6
|
isValidHandle,
|
|
7
7
|
} from './handle.js'
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* An "at-identifier" string - either a {@link DidString} or a {@link HandleString}
|
|
11
|
+
*
|
|
12
|
+
* @example `"did:plc:1234..."`, `"did:web:example.com"` or `"alice.bsky.social"`
|
|
13
|
+
*/
|
|
9
14
|
export type AtIdentifierString = DidString | HandleString
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Discriminates {@link HandleString} from a valid {@link AtIdentifierString}.
|
|
18
|
+
*
|
|
19
|
+
* @return `true` if the identifier is a handle, `false` otherwise
|
|
20
|
+
*/
|
|
21
|
+
export function isHandleIdentifier(id: AtIdentifierString): id is HandleString {
|
|
22
|
+
return !isDidIdentifier(id)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Discriminates {@link DidString} from a valid {@link AtIdentifierString}.
|
|
27
|
+
*
|
|
28
|
+
* @return `true` if the identifier is a DID, `false` otherwise
|
|
29
|
+
*/
|
|
30
|
+
export function isDidIdentifier(id: AtIdentifierString): id is DidString {
|
|
31
|
+
return id.startsWith('did:')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Validates that a string is a valid {@link AtIdentifierString} format string,
|
|
36
|
+
* throwing an error if it is not.
|
|
37
|
+
*
|
|
38
|
+
* @throws InvalidHandleError if the input string does not meet the atproto 'datetime' format requirements.
|
|
39
|
+
* @see {@link AtIdentifierString}
|
|
40
|
+
*/
|
|
41
|
+
export function assertAtIdentifierString<I>(
|
|
42
|
+
input: I,
|
|
43
|
+
): asserts input is I & AtIdentifierString {
|
|
14
44
|
try {
|
|
15
|
-
if (input
|
|
45
|
+
if (!input || typeof input !== 'string') {
|
|
46
|
+
throw new TypeError('Identifier must be a non-empty string')
|
|
47
|
+
} else if (input.startsWith('did:')) {
|
|
16
48
|
ensureValidDidRegex(input)
|
|
17
49
|
} else {
|
|
18
50
|
ensureValidHandleRegex(input)
|
|
@@ -22,12 +54,51 @@ export function ensureValidAtIdentifier(
|
|
|
22
54
|
}
|
|
23
55
|
}
|
|
24
56
|
|
|
25
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Casts a string to a {@link AtIdentifierString} if it is a valid at-identifier
|
|
59
|
+
* string, throwing an error if it is not.
|
|
60
|
+
*
|
|
61
|
+
* @throws InvalidHandleError if the input string does not meet the atproto 'at-identifier' format requirements.
|
|
62
|
+
* @see {@link AtIdentifierString}
|
|
63
|
+
*/
|
|
64
|
+
export function asAtIdentifierString<I>(input: I): I & AtIdentifierString {
|
|
65
|
+
assertAtIdentifierString(input)
|
|
66
|
+
return input
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Type guard that checks if a value is a valid AT identifier (DID or handle).
|
|
71
|
+
*
|
|
72
|
+
* @param value - The value to check
|
|
73
|
+
* @returns `true` if the value is a valid AT identifier
|
|
74
|
+
* @see {@link AtIdentifierString}
|
|
75
|
+
*/
|
|
76
|
+
export function isAtIdentifierString<I>(
|
|
26
77
|
input: I,
|
|
27
78
|
): input is I & AtIdentifierString {
|
|
28
|
-
if (input
|
|
79
|
+
if (!input || typeof input !== 'string') {
|
|
80
|
+
return false
|
|
81
|
+
} else if (input.startsWith('did:')) {
|
|
29
82
|
return isValidDid(input)
|
|
30
83
|
} else {
|
|
31
84
|
return isValidHandle(input)
|
|
32
85
|
}
|
|
33
86
|
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns the input if it is a valid {@link AtIdentifierString} format string, or
|
|
90
|
+
* `undefined` if it is not.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link AtIdentifierString}
|
|
93
|
+
*/
|
|
94
|
+
export function ifAtIdentifierString<I>(
|
|
95
|
+
input: I,
|
|
96
|
+
): undefined | (I & AtIdentifierString) {
|
|
97
|
+
return isAtIdentifierString(input) ? input : undefined
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Legacy exports (should we deprecate these ?)
|
|
101
|
+
export {
|
|
102
|
+
assertAtIdentifierString as ensureValidAtIdentifier,
|
|
103
|
+
isAtIdentifierString as isValidAtIdentifier,
|
|
104
|
+
}
|
package/src/aturi.ts
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AtIdentifierString,
|
|
3
|
+
ensureValidAtIdentifier,
|
|
4
|
+
isDidIdentifier,
|
|
5
|
+
} from './at-identifier.js'
|
|
2
6
|
import { AtUriString } from './aturi_validation.js'
|
|
3
|
-
import { DidString,
|
|
7
|
+
import { DidString, InvalidDidError } from './did.js'
|
|
4
8
|
import { NsidString, ensureValidNsid } from './nsid.js'
|
|
5
9
|
import { RecordKeyString, ensureValidRecordKey } from './recordkey.js'
|
|
6
10
|
|
|
7
11
|
export * from './aturi_validation.js'
|
|
8
12
|
|
|
13
|
+
// Re-export types used in public interface
|
|
14
|
+
export type {
|
|
15
|
+
AtIdentifierString,
|
|
16
|
+
AtUriString,
|
|
17
|
+
DidString,
|
|
18
|
+
NsidString,
|
|
19
|
+
RecordKeyString,
|
|
20
|
+
}
|
|
21
|
+
|
|
9
22
|
export const ATP_URI_REGEX =
|
|
10
23
|
// proto- --did-------------- --name---------------- --path---- --query-- --hash--
|
|
11
24
|
/^(at:\/\/)?((?:did:[a-z0-9:%-]+)|(?:[a-z0-9][a-z0-9.:-]*))(\/[^?#\s]*)?(\?[^#\s]+)?(#[^\s]+)?$/i
|
|
@@ -50,12 +63,12 @@ export class AtUri {
|
|
|
50
63
|
}
|
|
51
64
|
|
|
52
65
|
get did(): DidString {
|
|
53
|
-
const {
|
|
54
|
-
|
|
55
|
-
|
|
66
|
+
const { host } = this
|
|
67
|
+
if (isDidIdentifier(host)) return host
|
|
68
|
+
throw new InvalidDidError(`AtUri "${this}" does not have a DID hostname`)
|
|
56
69
|
}
|
|
57
70
|
|
|
58
|
-
get hostname() {
|
|
71
|
+
get hostname(): AtIdentifierString {
|
|
59
72
|
return this.host
|
|
60
73
|
}
|
|
61
74
|
|