@atproto/common-web 0.2.1 → 0.2.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atproto/common-web
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [`35d108ce`](https://github.com/bluesky-social/atproto/commit/35d108ce94866ce1b3d147cd0620a0ba1c4ebcd7) Thanks [@devinivy](https://github.com/devinivy)! - Allow pds to serve did doc with credentials, API client to respect PDS listed in the did doc.
8
+
3
9
  ## 0.2.1
4
10
 
5
11
  ### Patch Changes
package/dist/async.d.ts CHANGED
@@ -22,3 +22,4 @@ export declare class AsyncBuffer<T> {
22
22
  export declare class AsyncBufferFullError extends Error {
23
23
  constructor(maxSize: number);
24
24
  }
25
+ export declare const handleAllSettledErrors: (results: PromiseSettledResult<unknown>[]) => void;
@@ -0,0 +1,91 @@
1
+ import { z } from 'zod';
2
+ export declare const isValidDidDoc: (doc: unknown) => doc is {
3
+ id: string;
4
+ alsoKnownAs?: string[] | undefined;
5
+ verificationMethod?: {
6
+ id: string;
7
+ type: string;
8
+ controller: string;
9
+ publicKeyMultibase?: string | undefined;
10
+ }[] | undefined;
11
+ service?: {
12
+ id: string;
13
+ type: string;
14
+ serviceEndpoint: (string | Record<string, unknown>) & (string | Record<string, unknown> | undefined);
15
+ }[] | undefined;
16
+ };
17
+ export declare const getDid: (doc: DidDocument) => string;
18
+ export declare const getHandle: (doc: DidDocument) => string | undefined;
19
+ export declare const getSigningKey: (doc: DidDocument) => {
20
+ type: string;
21
+ publicKeyMultibase: string;
22
+ } | undefined;
23
+ export declare const getPdsEndpoint: (doc: DidDocument) => string | undefined;
24
+ export declare const getFeedGenEndpoint: (doc: DidDocument) => string | undefined;
25
+ export declare const getNotifEndpoint: (doc: DidDocument) => string | undefined;
26
+ export declare const getServiceEndpoint: (doc: DidDocument, opts: {
27
+ id: string;
28
+ type: string;
29
+ }) => string | undefined;
30
+ export declare const didDocument: z.ZodObject<{
31
+ id: z.ZodString;
32
+ alsoKnownAs: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
33
+ verificationMethod: z.ZodOptional<z.ZodArray<z.ZodObject<{
34
+ id: z.ZodString;
35
+ type: z.ZodString;
36
+ controller: z.ZodString;
37
+ publicKeyMultibase: z.ZodOptional<z.ZodString>;
38
+ }, "strip", z.ZodTypeAny, {
39
+ id: string;
40
+ type: string;
41
+ controller: string;
42
+ publicKeyMultibase?: string | undefined;
43
+ }, {
44
+ id: string;
45
+ type: string;
46
+ controller: string;
47
+ publicKeyMultibase?: string | undefined;
48
+ }>, "many">>;
49
+ service: z.ZodOptional<z.ZodArray<z.ZodObject<{
50
+ id: z.ZodString;
51
+ type: z.ZodString;
52
+ serviceEndpoint: z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
53
+ }, "strip", z.ZodTypeAny, {
54
+ id: string;
55
+ type: string;
56
+ serviceEndpoint: (string | Record<string, unknown>) & (string | Record<string, unknown> | undefined);
57
+ }, {
58
+ id: string;
59
+ type: string;
60
+ serviceEndpoint: (string | Record<string, unknown>) & (string | Record<string, unknown> | undefined);
61
+ }>, "many">>;
62
+ }, "strip", z.ZodTypeAny, {
63
+ id: string;
64
+ alsoKnownAs?: string[] | undefined;
65
+ verificationMethod?: {
66
+ id: string;
67
+ type: string;
68
+ controller: string;
69
+ publicKeyMultibase?: string | undefined;
70
+ }[] | undefined;
71
+ service?: {
72
+ id: string;
73
+ type: string;
74
+ serviceEndpoint: (string | Record<string, unknown>) & (string | Record<string, unknown> | undefined);
75
+ }[] | undefined;
76
+ }, {
77
+ id: string;
78
+ alsoKnownAs?: string[] | undefined;
79
+ verificationMethod?: {
80
+ id: string;
81
+ type: string;
82
+ controller: string;
83
+ publicKeyMultibase?: string | undefined;
84
+ }[] | undefined;
85
+ service?: {
86
+ id: string;
87
+ type: string;
88
+ serviceEndpoint: (string | Record<string, unknown>) & (string | Record<string, unknown> | undefined);
89
+ }[] | undefined;
90
+ }>;
91
+ export declare type DidDocument = z.infer<typeof didDocument>;
package/dist/index.d.ts CHANGED
@@ -8,3 +8,4 @@ export * from './ipld';
8
8
  export * from './types';
9
9
  export * from './times';
10
10
  export * from './strings';
11
+ export * from './did-doc';
package/dist/index.js CHANGED
@@ -8837,12 +8837,22 @@ __export(src_exports2, {
8837
8837
  createDeferrables: () => createDeferrables,
8838
8838
  dedupeStrs: () => dedupeStrs,
8839
8839
  def: () => def,
8840
+ didDocument: () => didDocument,
8840
8841
  errHasMsg: () => errHasMsg,
8841
8842
  flattenUint8Arrays: () => flattenUint8Arrays,
8843
+ getDid: () => getDid,
8844
+ getFeedGenEndpoint: () => getFeedGenEndpoint,
8845
+ getHandle: () => getHandle,
8846
+ getNotifEndpoint: () => getNotifEndpoint,
8847
+ getPdsEndpoint: () => getPdsEndpoint,
8848
+ getServiceEndpoint: () => getServiceEndpoint,
8849
+ getSigningKey: () => getSigningKey,
8842
8850
  graphemeLen: () => graphemeLen,
8851
+ handleAllSettledErrors: () => handleAllSettledErrors,
8843
8852
  ipldEquals: () => ipldEquals,
8844
8853
  ipldToJson: () => ipldToJson,
8845
8854
  isErrnoException: () => isErrnoException,
8855
+ isValidDidDoc: () => isValidDidDoc,
8846
8856
  jitter: () => jitter,
8847
8857
  jsonToIpld: () => jsonToIpld,
8848
8858
  lessThanAgoMs: () => lessThanAgoMs,
@@ -9108,6 +9118,19 @@ var AsyncBufferFullError = class extends Error {
9108
9118
  super(`ReachedMaxBufferSize: ${maxSize}`);
9109
9119
  }
9110
9120
  };
9121
+ var handleAllSettledErrors = (results) => {
9122
+ const errors = results.filter(isRejected).map((res) => res.reason);
9123
+ if (errors.length === 0) {
9124
+ return;
9125
+ }
9126
+ if (errors.length === 1) {
9127
+ throw errors[0];
9128
+ }
9129
+ throw new AggregateError(errors, "Multiple errors: " + errors.map((err) => err?.message).join("\n"));
9130
+ };
9131
+ var isRejected = (result) => {
9132
+ return result.status === "rejected";
9133
+ };
9111
9134
 
9112
9135
  // src/tid.ts
9113
9136
  var TID_LEN = 13;
@@ -14055,4 +14078,114 @@ var validateLanguage = (langTag) => {
14055
14078
  return bcp47Regexp.test(langTag);
14056
14079
  };
14057
14080
  var bcp47Regexp = /^((?<grandfathered>(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?<language>([A-Za-z]{2,3}(-(?<extlang>[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})(-(?<script>[A-Za-z]{4}))?(-(?<region>[A-Za-z]{2}|[0-9]{3}))?(-(?<variant>[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(-(?<extension>[0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*(-(?<privateUseA>x(-[A-Za-z0-9]{1,8})+))?)|(?<privateUseB>x(-[A-Za-z0-9]{1,8})+))$/;
14081
+
14082
+ // src/did-doc.ts
14083
+ var isValidDidDoc = (doc) => {
14084
+ return didDocument.safeParse(doc).success;
14085
+ };
14086
+ var getDid = (doc) => {
14087
+ const id = doc.id;
14088
+ if (typeof id !== "string") {
14089
+ throw new Error("No `id` on document");
14090
+ }
14091
+ return id;
14092
+ };
14093
+ var getHandle = (doc) => {
14094
+ const aka = doc.alsoKnownAs;
14095
+ if (!aka)
14096
+ return void 0;
14097
+ const found = aka.find((name2) => name2.startsWith("at://"));
14098
+ if (!found)
14099
+ return void 0;
14100
+ return found.slice(5);
14101
+ };
14102
+ var getSigningKey = (doc) => {
14103
+ const did = getDid(doc);
14104
+ let keys = doc.verificationMethod;
14105
+ if (!keys)
14106
+ return void 0;
14107
+ if (typeof keys !== "object")
14108
+ return void 0;
14109
+ if (!Array.isArray(keys)) {
14110
+ keys = [keys];
14111
+ }
14112
+ const found = keys.find((key) => key.id === "#atproto" || key.id === `${did}#atproto`);
14113
+ if (!found?.publicKeyMultibase)
14114
+ return void 0;
14115
+ return {
14116
+ type: found.type,
14117
+ publicKeyMultibase: found.publicKeyMultibase
14118
+ };
14119
+ };
14120
+ var getPdsEndpoint = (doc) => {
14121
+ return getServiceEndpoint(doc, {
14122
+ id: "#atproto_pds",
14123
+ type: "AtprotoPersonalDataServer"
14124
+ });
14125
+ };
14126
+ var getFeedGenEndpoint = (doc) => {
14127
+ return getServiceEndpoint(doc, {
14128
+ id: "#bsky_fg",
14129
+ type: "BskyFeedGenerator"
14130
+ });
14131
+ };
14132
+ var getNotifEndpoint = (doc) => {
14133
+ return getServiceEndpoint(doc, {
14134
+ id: "#bsky_notif",
14135
+ type: "BskyNotificationService"
14136
+ });
14137
+ };
14138
+ var getServiceEndpoint = (doc, opts) => {
14139
+ const did = getDid(doc);
14140
+ let services = doc.service;
14141
+ if (!services)
14142
+ return void 0;
14143
+ if (typeof services !== "object")
14144
+ return void 0;
14145
+ if (!Array.isArray(services)) {
14146
+ services = [services];
14147
+ }
14148
+ const found = services.find((service2) => service2.id === opts.id || service2.id === `${did}${opts.id}`);
14149
+ if (!found)
14150
+ return void 0;
14151
+ if (found.type !== opts.type) {
14152
+ return void 0;
14153
+ }
14154
+ if (typeof found.serviceEndpoint !== "string") {
14155
+ return void 0;
14156
+ }
14157
+ return validateUrl(found.serviceEndpoint);
14158
+ };
14159
+ var validateUrl = (urlStr) => {
14160
+ let url;
14161
+ try {
14162
+ url = new URL(urlStr);
14163
+ } catch {
14164
+ return void 0;
14165
+ }
14166
+ if (!["http:", "https:"].includes(url.protocol)) {
14167
+ return void 0;
14168
+ } else if (!url.hostname) {
14169
+ return void 0;
14170
+ } else {
14171
+ return urlStr;
14172
+ }
14173
+ };
14174
+ var verificationMethod = z.object({
14175
+ id: z.string(),
14176
+ type: z.string(),
14177
+ controller: z.string(),
14178
+ publicKeyMultibase: z.string().optional()
14179
+ });
14180
+ var service = z.object({
14181
+ id: z.string(),
14182
+ type: z.string(),
14183
+ serviceEndpoint: z.union([z.string(), z.record(z.unknown())])
14184
+ });
14185
+ var didDocument = z.object({
14186
+ id: z.string(),
14187
+ alsoKnownAs: z.array(z.string()).optional(),
14188
+ verificationMethod: z.array(verificationMethod).optional(),
14189
+ service: z.array(service).optional()
14190
+ });
14058
14191
  //# sourceMappingURL=index.js.map