@atproto/lex-password-session 0.0.3 → 0.0.4
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/dist/error.d.ts +4 -4
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +7 -4
- package/dist/error.js.map +1 -1
- package/dist/password-session.d.ts +10 -6
- package/dist/password-session.d.ts.map +1 -1
- package/dist/password-session.js +24 -14
- package/dist/password-session.js.map +1 -1
- package/dist/util.d.ts +0 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +1 -4
- package/dist/util.js.map +1 -1
- package/package.json +5 -5
- package/src/error.ts +7 -8
- package/src/password-session.test.ts +17 -16
- package/src/password-session.ts +46 -19
- package/src/util.ts +2 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @atproto/lex-password-session
|
|
2
2
|
|
|
3
|
+
## 0.0.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4589](https://github.com/bluesky-social/atproto/pull/4589) [`369bb02`](https://github.com/bluesky-social/atproto/commit/369bb02b9f80f0e15e5242e54f09bd4e01117f3a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add `PasswordSession.createAccount` static method
|
|
8
|
+
|
|
9
|
+
- [#4589](https://github.com/bluesky-social/atproto/pull/4589) [`369bb02`](https://github.com/bluesky-social/atproto/commit/369bb02b9f80f0e15e5242e54f09bd4e01117f3a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Rename `PasswordSession.create` to `PasswordSession.login`
|
|
10
|
+
|
|
11
|
+
- [#4589](https://github.com/bluesky-social/atproto/pull/4589) [`369bb02`](https://github.com/bluesky-social/atproto/commit/369bb02b9f80f0e15e5242e54f09bd4e01117f3a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Make all `PasswordSessionOptions` optional
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [[`369bb02`](https://github.com/bluesky-social/atproto/commit/369bb02b9f80f0e15e5242e54f09bd4e01117f3a), [`369bb02`](https://github.com/bluesky-social/atproto/commit/369bb02b9f80f0e15e5242e54f09bd4e01117f3a), [`369bb02`](https://github.com/bluesky-social/atproto/commit/369bb02b9f80f0e15e5242e54f09bd4e01117f3a)]:
|
|
14
|
+
- @atproto/lex-client@0.0.11
|
|
15
|
+
- @atproto/lex-schema@0.0.11
|
|
16
|
+
|
|
3
17
|
## 0.0.3
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/error.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { LexError,
|
|
2
|
-
import { com } from './lexicons';
|
|
1
|
+
import { LexError, XrpcFailure } from '@atproto/lex-client';
|
|
3
2
|
export declare class LexAuthFactorError extends LexError {
|
|
4
|
-
readonly
|
|
3
|
+
readonly cause: XrpcFailure;
|
|
5
4
|
name: string;
|
|
6
|
-
constructor(
|
|
5
|
+
constructor(cause: XrpcFailure);
|
|
6
|
+
toResponse(): Response;
|
|
7
7
|
}
|
|
8
8
|
//# sourceMappingURL=error.d.ts.map
|
package/dist/error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAE3D,qBAAa,kBAAmB,SAAQ,QAAQ;IAGlC,QAAQ,CAAC,KAAK,EAAE,WAAW;IAFvC,IAAI,SAAuB;gBAEN,KAAK,EAAE,WAAW;IAI9B,UAAU,IAAI,QAAQ;CAGhC"}
|
package/dist/error.js
CHANGED
|
@@ -3,11 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.LexAuthFactorError = void 0;
|
|
4
4
|
const lex_client_1 = require("@atproto/lex-client");
|
|
5
5
|
class LexAuthFactorError extends lex_client_1.LexError {
|
|
6
|
-
|
|
6
|
+
cause;
|
|
7
7
|
name = 'LexAuthFactorError';
|
|
8
|
-
constructor(
|
|
9
|
-
super(
|
|
10
|
-
this.
|
|
8
|
+
constructor(cause) {
|
|
9
|
+
super(cause.error, cause.message ?? 'Auth factor token required', { cause });
|
|
10
|
+
this.cause = cause;
|
|
11
|
+
}
|
|
12
|
+
toResponse() {
|
|
13
|
+
return Response.json({ error: 'InternalServerError' }, { status: 500 });
|
|
11
14
|
}
|
|
12
15
|
}
|
|
13
16
|
exports.LexAuthFactorError = LexAuthFactorError;
|
package/dist/error.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;AAAA,oDAA2D;AAE3D,MAAa,kBAAmB,SAAQ,qBAAQ;IAGzB;IAFrB,IAAI,GAAG,oBAAoB,CAAA;IAE3B,YAAqB,KAAkB;QACrC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,IAAI,4BAA4B,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;QADzD,UAAK,GAAL,KAAK,CAAa;IAEvC,CAAC;IAEQ,UAAU;QACjB,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,qBAAqB,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAA;IACzE,CAAC;CACF;AAVD,gDAUC","sourcesContent":["import { LexError, XrpcFailure } from '@atproto/lex-client'\n\nexport class LexAuthFactorError extends LexError {\n name = 'LexAuthFactorError'\n\n constructor(readonly cause: XrpcFailure) {\n super(cause.error, cause.message ?? 'Auth factor token required', { cause })\n }\n\n override toResponse(): Response {\n return Response.json({ error: 'InternalServerError' }, { status: 500 })\n }\n}\n"]}
|
|
@@ -20,7 +20,7 @@ export type PasswordSessionOptions = {
|
|
|
20
20
|
*
|
|
21
21
|
* @note this function **must** not throw
|
|
22
22
|
*/
|
|
23
|
-
onUpdated
|
|
23
|
+
onUpdated?: (this: PasswordSession, data: SessionData) => void | Promise<void>;
|
|
24
24
|
/**
|
|
25
25
|
* Called whenever the session update fails due to an expected error, such as
|
|
26
26
|
* a network issue or server unavailability. This function can be used to log
|
|
@@ -38,7 +38,7 @@ export type PasswordSessionOptions = {
|
|
|
38
38
|
*
|
|
39
39
|
* @note this function **must** not throw
|
|
40
40
|
*/
|
|
41
|
-
onDeleted
|
|
41
|
+
onDeleted?: (this: PasswordSession, data: SessionData) => void | Promise<void>;
|
|
42
42
|
/**
|
|
43
43
|
* Called whenever a session deletion fails due to an unexpected error, such
|
|
44
44
|
* as a network issue or server unavailability. This function can be used to
|
|
@@ -58,7 +58,7 @@ export type PasswordSessionOptions = {
|
|
|
58
58
|
export declare class PasswordSession implements Agent {
|
|
59
59
|
#private;
|
|
60
60
|
protected readonly options: PasswordSessionOptions;
|
|
61
|
-
constructor(sessionData: SessionData, options
|
|
61
|
+
constructor(sessionData: SessionData, options?: PasswordSessionOptions);
|
|
62
62
|
get did(): `did:${string}:${string}`;
|
|
63
63
|
get handle(): `${string}.${string}`;
|
|
64
64
|
get session(): SessionData;
|
|
@@ -66,6 +66,10 @@ export declare class PasswordSession implements Agent {
|
|
|
66
66
|
fetchHandler(path: string, init: RequestInit): Promise<Response>;
|
|
67
67
|
refresh(): Promise<SessionData>;
|
|
68
68
|
logout(): Promise<void>;
|
|
69
|
+
static createAccount(body: com.atproto.server.createAccount.InputBody, { service, headers, ...options }: PasswordSessionOptions & {
|
|
70
|
+
headers?: HeadersInit;
|
|
71
|
+
service: string | URL;
|
|
72
|
+
}): Promise<PasswordSession>;
|
|
69
73
|
/**
|
|
70
74
|
* @note It is **not** recommended to use {@link PasswordSession} with main
|
|
71
75
|
* account credentials. Instead, it is strongly advised to use OAuth based
|
|
@@ -82,7 +86,7 @@ export declare class PasswordSession implements Agent {
|
|
|
82
86
|
*
|
|
83
87
|
* ```ts
|
|
84
88
|
* try {
|
|
85
|
-
* const session = await PasswordSession.
|
|
89
|
+
* const session = await PasswordSession.login({
|
|
86
90
|
* service: 'https://example.com',
|
|
87
91
|
* identifier: 'alice',
|
|
88
92
|
* password: 'correct horse battery staple',
|
|
@@ -94,7 +98,7 @@ export declare class PasswordSession implements Agent {
|
|
|
94
98
|
* }
|
|
95
99
|
* ```
|
|
96
100
|
*/
|
|
97
|
-
static
|
|
101
|
+
static login({ service, identifier, password, allowTakendown, authFactorToken, ...options }: PasswordSessionOptions & {
|
|
98
102
|
service: string | URL;
|
|
99
103
|
identifier: string;
|
|
100
104
|
password: string;
|
|
@@ -122,6 +126,6 @@ export declare class PasswordSession implements Agent {
|
|
|
122
126
|
* @throws In case of unexpected error (network issue, server down, etc)
|
|
123
127
|
* meaning that the session may still be valid.
|
|
124
128
|
*/
|
|
125
|
-
static delete(data: SessionData, options?:
|
|
129
|
+
static delete(data: SessionData, options?: PasswordSessionOptions): Promise<void>;
|
|
126
130
|
}
|
|
127
131
|
//# sourceMappingURL=password-session.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"password-session.d.ts","sourceRoot":"","sources":["../src/password-session.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,
|
|
1
|
+
{"version":3,"file":"password-session.d.ts","sourceRoot":"","sources":["../src/password-session.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,WAAW,EAIZ,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAA;AAGzC,MAAM,MAAM,cAAc,GAAG,WAAW,CACtC,OAAO,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAC9C,CAAA;AAED,MAAM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AE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC;IAkEhE,OAAO,IAAI,OAAO,CAAC,WAAW,CAAC;IA4D/B,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;WAwChB,aAAa,CACxB,IAAI,EAAE,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,SAAS,EAChD,EACE,OAAO,EACP,OAAO,EACP,GAAG,OAAO,EACX,EAAE,sBAAsB,GAAG;QAC1B,OAAO,CAAC,EAAE,WAAW,CAAA;QACrB,OAAO,EAAE,MAAM,GAAG,GAAG,CAAA;KACtB,GACA,OAAO,CAAC,eAAe,CAAC;IAiB3B;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;WACU,KAAK,CAAC,EACjB,OAAO,EACP,UAAU,EACV,QAAQ,EACR,cAAc,EACd,eAAe,EACf,GAAG,OAAO,EACX,EAAE,sBAAsB,GAAG;QAC1B,OAAO,EAAE,MAAM,GAAG,GAAG,CAAA;QACrB,UAAU,EAAE,MAAM,CAAA;QAClB,QAAQ,EAAE,MAAM,CAAA;QAChB,cAAc,CAAC,EAAE,OAAO,CAAA;QACxB,eAAe,CAAC,EAAE,MAAM,CAAA;KACzB,GAAG,OAAO,CAAC,eAAe,CAAC;IA6B5B;;;;;;;;;;;;OAYG;WACU,MAAM,CACjB,IAAI,EAAE,WAAW,EACjB,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC;IAM3B;;;;;;OAMG;WACU,MAAM,CACjB,IAAI,EAAE,WAAW,EACjB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CAAC,IAAI,CAAC;CAIjB"}
|
package/dist/password-session.js
CHANGED
|
@@ -14,7 +14,7 @@ class PasswordSession {
|
|
|
14
14
|
#serviceAgent;
|
|
15
15
|
#sessionData;
|
|
16
16
|
#sessionPromise;
|
|
17
|
-
constructor(sessionData, options) {
|
|
17
|
+
constructor(sessionData, options = {}) {
|
|
18
18
|
this.options = options;
|
|
19
19
|
this.#serviceAgent = (0, lex_client_1.buildAgent)({
|
|
20
20
|
service: sessionData.service,
|
|
@@ -32,7 +32,7 @@ class PasswordSession {
|
|
|
32
32
|
get session() {
|
|
33
33
|
if (this.#sessionData)
|
|
34
34
|
return this.#sessionData;
|
|
35
|
-
throw new
|
|
35
|
+
throw new Error('Logged out');
|
|
36
36
|
}
|
|
37
37
|
get destroyed() {
|
|
38
38
|
return this.#sessionData === null;
|
|
@@ -93,12 +93,16 @@ class PasswordSession {
|
|
|
93
93
|
const response = await (0, lex_client_1.xrpcSafe)(this.#serviceAgent, index_js_1.com.atproto.server.refreshSession.main, { headers: { Authorization: `Bearer ${sessionData.refreshJwt}` } });
|
|
94
94
|
if (!response.success && response.matchesSchema()) {
|
|
95
95
|
// Expected errors that indicate the session is no longer valid
|
|
96
|
-
await this.options.onDeleted
|
|
96
|
+
await this.options.onDeleted?.call(this, sessionData);
|
|
97
97
|
// Update the session promise to a rejected state
|
|
98
98
|
this.#sessionData = null;
|
|
99
99
|
throw response;
|
|
100
100
|
}
|
|
101
101
|
if (!response.success) {
|
|
102
|
+
response.error;
|
|
103
|
+
if (response.matchesSchema()) {
|
|
104
|
+
response.error;
|
|
105
|
+
}
|
|
102
106
|
// We failed to refresh the token, assume the session might still be
|
|
103
107
|
// valid by returning the existing session.
|
|
104
108
|
await this.options.onUpdateFailure?.call(this, sessionData, response);
|
|
@@ -120,7 +124,7 @@ class PasswordSession {
|
|
|
120
124
|
...data,
|
|
121
125
|
service: sessionData.service,
|
|
122
126
|
};
|
|
123
|
-
await this.options.onUpdated
|
|
127
|
+
await this.options.onUpdated?.call(this, newSession);
|
|
124
128
|
return (this.#sessionData = newSession);
|
|
125
129
|
});
|
|
126
130
|
return this.#sessionPromise;
|
|
@@ -130,10 +134,10 @@ class PasswordSession {
|
|
|
130
134
|
this.#sessionPromise = this.#sessionPromise.then(async (sessionData) => {
|
|
131
135
|
const result = await (0, lex_client_1.xrpcSafe)(this.#serviceAgent, index_js_1.com.atproto.server.deleteSession.main, { headers: { Authorization: `Bearer ${sessionData.refreshJwt}` } });
|
|
132
136
|
if (result.success || result.matchesSchema()) {
|
|
133
|
-
await this.options.onDeleted
|
|
137
|
+
await this.options.onDeleted?.call(this, sessionData);
|
|
134
138
|
// Update the session promise to a rejected state
|
|
135
139
|
this.#sessionData = null;
|
|
136
|
-
throw new
|
|
140
|
+
throw new Error('Logged out');
|
|
137
141
|
}
|
|
138
142
|
else {
|
|
139
143
|
// Capture the reason for the failure to re-throw in the outer promise
|
|
@@ -152,6 +156,16 @@ class PasswordSession {
|
|
|
152
156
|
// Successful logout
|
|
153
157
|
});
|
|
154
158
|
}
|
|
159
|
+
static async createAccount(body, { service, headers, ...options }) {
|
|
160
|
+
const response = await (0, lex_client_1.xrpc)((0, lex_client_1.buildAgent)({ service, headers, fetch: options.fetch }), index_js_1.com.atproto.server.createAccount.main, { body });
|
|
161
|
+
const data = {
|
|
162
|
+
...response.body,
|
|
163
|
+
service: String(service),
|
|
164
|
+
};
|
|
165
|
+
const agent = new PasswordSession(data, options);
|
|
166
|
+
await options.onUpdated?.call(agent, data);
|
|
167
|
+
return agent;
|
|
168
|
+
}
|
|
155
169
|
/**
|
|
156
170
|
* @note It is **not** recommended to use {@link PasswordSession} with main
|
|
157
171
|
* account credentials. Instead, it is strongly advised to use OAuth based
|
|
@@ -168,7 +182,7 @@ class PasswordSession {
|
|
|
168
182
|
*
|
|
169
183
|
* ```ts
|
|
170
184
|
* try {
|
|
171
|
-
* const session = await PasswordSession.
|
|
185
|
+
* const session = await PasswordSession.login({
|
|
172
186
|
* service: 'https://example.com',
|
|
173
187
|
* identifier: 'alice',
|
|
174
188
|
* password: 'correct horse battery staple',
|
|
@@ -180,7 +194,7 @@ class PasswordSession {
|
|
|
180
194
|
* }
|
|
181
195
|
* ```
|
|
182
196
|
*/
|
|
183
|
-
static async
|
|
197
|
+
static async login({ service, identifier, password, allowTakendown, authFactorToken, ...options }) {
|
|
184
198
|
const xrpcAgent = (0, lex_client_1.buildAgent)({
|
|
185
199
|
service,
|
|
186
200
|
fetch: options.fetch,
|
|
@@ -197,7 +211,7 @@ class PasswordSession {
|
|
|
197
211
|
service: String(service),
|
|
198
212
|
};
|
|
199
213
|
const agent = new PasswordSession(data, options);
|
|
200
|
-
await options.onUpdated
|
|
214
|
+
await options.onUpdated?.call(agent, data);
|
|
201
215
|
return agent;
|
|
202
216
|
}
|
|
203
217
|
/**
|
|
@@ -226,11 +240,7 @@ class PasswordSession {
|
|
|
226
240
|
* meaning that the session may still be valid.
|
|
227
241
|
*/
|
|
228
242
|
static async delete(data, options) {
|
|
229
|
-
const agent = new PasswordSession(data,
|
|
230
|
-
...options,
|
|
231
|
-
onUpdated: options?.onUpdated ?? util_js_1.noop,
|
|
232
|
-
onDeleted: options?.onDeleted ?? util_js_1.noop,
|
|
233
|
-
});
|
|
243
|
+
const agent = new PasswordSession(data, options);
|
|
234
244
|
await agent.logout();
|
|
235
245
|
}
|
|
236
246
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"password-session.js","sourceRoot":"","sources":["../src/password-session.ts"],"names":[],"mappings":";;;AAAA,oDAM4B;AAC5B,yCAA+C;AAC/C,kDAAyC;AACzC,uCAAqE;AA6ErE,MAAa,eAAe;IAYL;IAXrB;;;OAGG;IACH,aAAa,CAAO;IAEpB,YAAY,CAAoB;IAChC,eAAe,CAAsB;IAErC,YACE,WAAwB,EACL,OAA+B;QAA/B,YAAO,GAAP,OAAO,CAAwB;QAElD,IAAI,CAAC,aAAa,GAAG,IAAA,uBAAU,EAAC;YAC9B,OAAO,EAAE,WAAW,CAAC,OAAO;YAC5B,KAAK,EAAE,OAAO,CAAC,KAAK;SACrB,CAAC,CAAA;QAEF,IAAI,CAAC,YAAY,GAAG,WAAW,CAAA;QAC/B,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;IAC3D,CA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K,GAAG,IAAI,eAAe,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAChD,MAAM,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QACzC,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,CACjB,IAAiB,EACjB,OAA+B;QAE/B,MAAM,KAAK,GAAG,IAAI,eAAe,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAChD,MAAM,KAAK,CAAC,OAAO,EAAE,CAAA;QACrB,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,CACjB,IAAiB,EACjB,OAAyC;QAEzC,MAAM,KAAK,GAAG,IAAI,eAAe,CAAC,IAAI,EAAE;YACtC,GAAG,OAAO;YACV,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,cAAI;YACrC,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,cAAI;SACtC,CAAC,CAAA;QACF,MAAM,KAAK,CAAC,MAAM,EAAE,CAAA;IACtB,CAAC;CACF;AAxTD,0CAwTC;AAED,SAAS,QAAQ,CAAC,WAAwB,EAAE,IAAY;IACtD,MAAM,MAAM,GAAG,IAAA,uBAAa,EAAC,WAAW,CAAC,MAAM,CAAC,CAAA;IAChD,OAAO,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,IAAI,WAAW,CAAC,OAAO,CAAC,CAAA;AACrD,CAAC","sourcesContent":["import {\n Agent,\n XrpcError,\n XrpcFailure,\n buildAgent,\n xrpcSafe,\n} from '@atproto/lex-client'\nimport { LexAuthFactorError } from './error.js'\nimport { com } from './lexicons/index.js'\nimport { extractPdsUrl, extractXrpcErrorCode, noop } from './util.js'\n\nexport type RefreshFailure = XrpcFailure<\n typeof com.atproto.server.refreshSession.main\n>\n\nexport type DeleteFailure = XrpcFailure<\n typeof com.atproto.server.deleteSession.main\n>\n\nexport type SessionData = com.atproto.server.createSession.OutputBody & {\n service: string\n}\n\nexport type PasswordSessionOptions = {\n /**\n * Custom fetch implementation to use for network requests\n */\n fetch?: typeof globalThis.fetch\n\n /**\n * Called whenever the session is successfully created/refreshed, and new\n * credentials have been obtained. Use this hook to persist the updated\n * session information.\n *\n * If this callback returns a promise, this function will never be called\n * again (on the same process) until the promise resolves.\n *\n * @note this function **must** not throw\n */\n onUpdated: (this: PasswordSession, data: SessionData) => void | Promise<void>\n\n /**\n * Called whenever the session update fails due to an expected error, such as\n * a network issue or server unavailability. This function can be used to log\n * the error or notify the user, but should not assume that the session is\n * invalid.\n *\n * @note this function **must** not throw\n */\n onUpdateFailure?: (\n this: PasswordSession,\n data: SessionData,\n err: RefreshFailure,\n ) => void | Promise<void>\n\n /**\n * Called whenever the session is deleted, either due to an explicit logout or\n * because the refresh operation indicated that the session is no longer\n * valid. Use this hook to clean up any persisted session information and\n * update the application state accordingly.\n *\n * @note this function **must** not throw\n */\n onDeleted: (this: PasswordSession, data: SessionData) => void | Promise<void>\n\n /**\n * Called whenever a session deletion fails due to an unexpected error, such\n * as a network issue or server unavailability. This function can be used to\n * log the error or notify the user. When this function is called, the session\n * might still be valid on the server. It is up to the implementation to\n * decide whether to retry the deletion or keep the session active. Ignoring\n * these errors is not recommended as it can lead to orphaned sessions on the\n * server, or security issues if the user believes they have logged out when a\n * bad actor is still using the session. The implementation should consider\n * keeping track of failed deletions and retrying them later, until they\n * succeed.\n *\n * @note this function **must** not throw\n */\n onDeleteFailure?: (\n this: PasswordSession,\n data: SessionData,\n err: DeleteFailure,\n ) => void | Promise<void>\n}\n\nexport class PasswordSession implements Agent {\n /**\n * Internal {@link Agent} used for session management towards the\n * authentication service only.\n */\n #serviceAgent: Agent\n\n #sessionData: null | SessionData\n #sessionPromise: Promise<SessionData>\n\n constructor(\n sessionData: SessionData,\n protected readonly options: PasswordSessionOptions,\n ) {\n this.#serviceAgent = buildAgent({\n service: sessionData.service,\n fetch: options.fetch,\n })\n\n this.#sessionData = sessionData\n this.#sessionPromise = Promise.resolve(this.#sessionData)\n }\n\n get did() {\n return this.session.did\n }\n\n get handle() {\n return this.session.handle\n }\n\n get session() {\n if (this.#sessionData) return this.#sessionData\n throw new XrpcError('AuthenticationRequired', 'Logged out')\n }\n\n get destroyed(): boolean {\n return this.#sessionData === null\n }\n\n async fetchHandler(path: string, init: RequestInit): Promise<Response> {\n const headers = new Headers(init.headers)\n if (headers.has('authorization')) {\n throw new TypeError(\"Unexpected 'authorization' header set\")\n }\n\n const sessionPromise = this.#sessionPromise\n const sessionData = await sessionPromise\n\n const fetch = this.options.fetch ?? globalThis.fetch\n\n headers.set('authorization', `Bearer ${sessionData.accessJwt}`)\n const initialRes = await fetch(fetchUrl(sessionData, path), {\n ...init,\n headers,\n })\n\n const refreshNeeded =\n initialRes.status === 401 ||\n (initialRes.status === 400 &&\n (await extractXrpcErrorCode(initialRes)) === 'ExpiredToken')\n\n if (!refreshNeeded) {\n return initialRes\n }\n\n // Refresh session (unless it was already refreshed in the meantime)\n const newSessionPromise =\n this.#sessionPromise === sessionPromise\n ? this.refresh()\n : this.#sessionPromise\n\n // Error should have been propagated through hooks\n const newSessionData = await newSessionPromise.catch((_err) => null)\n if (!newSessionData) {\n return initialRes\n }\n\n // refresh silently failed, no point in retrying.\n if (newSessionData.accessJwt === sessionData.accessJwt) {\n return initialRes\n }\n\n if (init?.signal?.aborted) {\n return initialRes\n }\n\n // The stream was already consumed. We cannot retry the request. A solution\n // would be to tee() the input stream but that would bufferize the entire\n // stream in memory which can lead to memory starvation. Instead, we will\n // return the original response and let the calling code handle retries.\n if (ReadableStream && init?.body instanceof ReadableStream) {\n return initialRes\n }\n\n // Make sure the initial request is cancelled to avoid leaking resources\n // (NodeJS 👀): https://undici.nodejs.org/#/?id=garbage-collection\n if (!initialRes.bodyUsed) {\n await initialRes.body?.cancel()\n }\n\n // Finally, retry the request with the new access token\n headers.set('authorization', `Bearer ${newSessionData.accessJwt}`)\n return fetch(fetchUrl(newSessionData, path), { ...init, headers })\n }\n\n async refresh(): Promise<SessionData> {\n this.#sessionPromise = this.#sessionPromise.then(async (sessionData) => {\n const response = await xrpcSafe(\n this.#serviceAgent,\n com.atproto.server.refreshSession.main,\n { headers: { Authorization: `Bearer ${sessionData.refreshJwt}` } },\n )\n\n if (!response.success && response.matchesSchema()) {\n // Expected errors that indicate the session is no longer valid\n await this.options.onDeleted.call(this, sessionData)\n\n // Update the session promise to a rejected state\n this.#sessionData = null\n throw response\n }\n\n if (!response.success) {\n // We failed to refresh the token, assume the session might still be\n // valid by returning the existing session.\n await this.options.onUpdateFailure?.call(this, sessionData, response)\n\n return sessionData\n }\n\n const data = response.body\n\n // Historically, refreshSession did not return all the fields from\n // getSession. In particular, emailConfirmed and didDoc were missing.\n // Similarly, some servers might not return the didDoc in refreshSession.\n // We fetch them via getSession if missing, allowing to ensure that we are\n // always talking with the right PDS.\n if (data.emailConfirmed == null || data.didDoc == null) {\n const extraData = await xrpcSafe(\n this.#serviceAgent,\n com.atproto.server.getSession.main,\n { headers: { Authorization: `Bearer ${data.accessJwt}` } },\n )\n if (extraData.success && extraData.body.did === data.did) {\n Object.assign(data, extraData.body)\n }\n }\n\n const newSession: SessionData = {\n ...data,\n service: sessionData.service,\n }\n\n await this.options.onUpdated.call(this, newSession)\n\n return (this.#sessionData = newSession)\n })\n\n return this.#sessionPromise\n }\n\n async logout(): Promise<void> {\n let reason: DeleteFailure | null = null\n\n this.#sessionPromise = this.#sessionPromise.then(async (sessionData) => {\n const result = await xrpcSafe(\n this.#serviceAgent,\n com.atproto.server.deleteSession.main,\n { headers: { Authorization: `Bearer ${sessionData.refreshJwt}` } },\n )\n\n if (result.success || result.matchesSchema()) {\n await this.options.onDeleted.call(this, sessionData)\n\n // Update the session promise to a rejected state\n this.#sessionData = null\n throw new XrpcError('AuthenticationRequired', 'Logged out')\n } else {\n // Capture the reason for the failure to re-throw in the outer promise\n reason = result\n\n // An unknown/unexpected error occurred (network, server down, etc)\n await this.options.onDeleteFailure?.call(this, sessionData, result)\n\n // Keep the session in an active state\n return sessionData\n }\n })\n\n return this.#sessionPromise.then(\n (_session) => {\n // If the promise above resolved, then logout failed. Re-throw the\n // reason captured earlier.\n throw reason!\n },\n (_err) => {\n // Successful logout\n },\n )\n }\n\n /**\n * @note It is **not** recommended to use {@link PasswordSession} with main\n * account credentials. Instead, it is strongly advised to use OAuth based\n * authentication for main username/password credentials and use\n * {@link PasswordSession} with an app-password, for bots, scripts, or similar\n * use-cases.\n *\n * @throws If unable to create a session. In particular, if the server\n * requires a 2FA token, a {@link XrpcResponseError} with the\n * `AuthFactorTokenRequired` error code will be thrown.\n *\n *\n * @example Handling 2FA errors\n *\n * ```ts\n * try {\n * const session = await PasswordSession.create({\n * service: 'https://example.com',\n * identifier: 'alice',\n * password: 'correct horse battery staple',\n * })\n * } catch (err) {\n * if (err instanceof XrpcResponseError && err.error === 'AuthFactorTokenRequired') {\n * // Prompt user for 2FA token and re-attempt session creation\n * }\n * }\n * ```\n */\n static async create({\n service,\n identifier,\n password,\n allowTakendown,\n authFactorToken,\n ...options\n }: PasswordSessionOptions & {\n service: string | URL\n identifier: string\n password: string\n allowTakendown?: boolean\n authFactorToken?: string\n }): Promise<PasswordSession> {\n const xrpcAgent = buildAgent({\n service,\n fetch: options.fetch,\n })\n\n const response = await xrpcSafe(\n xrpcAgent,\n com.atproto.server.createSession.main,\n { body: { identifier, password, allowTakendown, authFactorToken } },\n )\n\n if (!response.success) {\n if (response.error === 'AuthFactorTokenRequired') {\n throw new LexAuthFactorError(response)\n }\n throw response.reason\n }\n\n const data: SessionData = {\n ...response.body,\n service: String(service),\n }\n\n const agent = new PasswordSession(data, options)\n await options.onUpdated.call(agent, data)\n return agent\n }\n\n /**\n * Resume an existing session, ensuring it is still valid by refreshing it.\n * Any error thrown here indicates that the session is definitely no longer\n * valid. Network errors will be propagated through the\n * {@link PasswordSessionOptions.onUpdateFailure} hook, and not re-thrown\n * here. This means that a resolved promise does not necessarily indicate a\n * valid session, only that it's refresh did not definitively fail.\n *\n * This is the same as calling {@link PasswordSession.refresh} after\n * constructing the {@link PasswordSession} manually.\n *\n * @throws If, and only if, the session is definitely no longer valid.\n */\n static async resume(\n data: SessionData,\n options: PasswordSessionOptions,\n ): Promise<PasswordSession> {\n const agent = new PasswordSession(data, options)\n await agent.refresh()\n return agent\n }\n\n /**\n * Delete a session without having to {@link resume resume()} it first, or\n * provide hooks.\n *\n * @throws In case of unexpected error (network issue, server down, etc)\n * meaning that the session may still be valid.\n */\n static async delete(\n data: SessionData,\n options?: Partial<PasswordSessionOptions>,\n ): Promise<void> {\n const agent = new PasswordSession(data, {\n ...options,\n onUpdated: options?.onUpdated ?? noop,\n onDeleted: options?.onDeleted ?? noop,\n })\n await agent.logout()\n }\n}\n\nfunction fetchUrl(sessionData: SessionData, path: string): URL {\n const pdsUrl = extractPdsUrl(sessionData.didDoc)\n return new URL(path, pdsUrl ?? sessionData.service)\n}\n"]}
|
|
1
|
+
{"version":3,"file":"password-session.js","sourceRoot":"","sources":["../src/password-session.ts"],"names":[],"mappings":";;;AAAA,oDAM4B;AAC5B,yCAA+C;AAC/C,kDAAyC;AACzC,uCAA+D;AA6E/D,MAAa,eAAe;IAYL;IAXrB;;;OAGG;IACH,aAAa,CAAO;IAEpB,YAAY,CAAoB;IAChC,eAAe,CAAsB;IAErC,YACE,WAAwB,EACL,UAAkC,EAAE;QAApC,YAAO,GAAP,OAAO,CAA6B;QAEvD,IAAI,CAAC,aAAa,GAAG,IAAA,uBAAU,EAAC;YAC9B,OAAO,EAAE,WAAW,CAAC,OAAO;YAC5B,KAAK,EAAE,OAAO,CAAC,KAAK;SACrB,CAAC,CAAA;QAEF,IAAI,CAAC,YAAY,GAAG,WAAW,CAAA;QAC/B,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;IAC3D,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CA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AC,CAAA;QAChD,MAAM,KAAK,CAAC,MAAM,EAAE,CAAA;IACtB,CAAC;CACF;AAnVD,0CAmVC;AAED,SAAS,QAAQ,CAAC,WAAwB,EAAE,IAAY;IACtD,MAAM,MAAM,GAAG,IAAA,uBAAa,EAAC,WAAW,CAAC,MAAM,CAAC,CAAA;IAChD,OAAO,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,IAAI,WAAW,CAAC,OAAO,CAAC,CAAA;AACrD,CAAC","sourcesContent":["import {\n Agent,\n XrpcFailure,\n buildAgent,\n xrpc,\n xrpcSafe,\n} from '@atproto/lex-client'\nimport { LexAuthFactorError } from './error.js'\nimport { com } from './lexicons/index.js'\nimport { extractPdsUrl, extractXrpcErrorCode } from './util.js'\n\nexport type RefreshFailure = XrpcFailure<\n typeof com.atproto.server.refreshSession.main\n>\n\nexport type DeleteFailure = XrpcFailure<\n typeof com.atproto.server.deleteSession.main\n>\n\nexport type SessionData = com.atproto.server.createSession.OutputBody & {\n service: string\n}\n\nexport type PasswordSessionOptions = {\n /**\n * Custom fetch implementation to use for network requests\n */\n fetch?: typeof globalThis.fetch\n\n /**\n * Called whenever the session is successfully created/refreshed, and new\n * credentials have been obtained. Use this hook to persist the updated\n * session information.\n *\n * If this callback returns a promise, this function will never be called\n * again (on the same process) until the promise resolves.\n *\n * @note this function **must** not throw\n */\n onUpdated?: (this: PasswordSession, data: SessionData) => void | Promise<void>\n\n /**\n * Called whenever the session update fails due to an expected error, such as\n * a network issue or server unavailability. This function can be used to log\n * the error or notify the user, but should not assume that the session is\n * invalid.\n *\n * @note this function **must** not throw\n */\n onUpdateFailure?: (\n this: PasswordSession,\n data: SessionData,\n err: RefreshFailure,\n ) => void | Promise<void>\n\n /**\n * Called whenever the session is deleted, either due to an explicit logout or\n * because the refresh operation indicated that the session is no longer\n * valid. Use this hook to clean up any persisted session information and\n * update the application state accordingly.\n *\n * @note this function **must** not throw\n */\n onDeleted?: (this: PasswordSession, data: SessionData) => void | Promise<void>\n\n /**\n * Called whenever a session deletion fails due to an unexpected error, such\n * as a network issue or server unavailability. This function can be used to\n * log the error or notify the user. When this function is called, the session\n * might still be valid on the server. It is up to the implementation to\n * decide whether to retry the deletion or keep the session active. Ignoring\n * these errors is not recommended as it can lead to orphaned sessions on the\n * server, or security issues if the user believes they have logged out when a\n * bad actor is still using the session. The implementation should consider\n * keeping track of failed deletions and retrying them later, until they\n * succeed.\n *\n * @note this function **must** not throw\n */\n onDeleteFailure?: (\n this: PasswordSession,\n data: SessionData,\n err: DeleteFailure,\n ) => void | Promise<void>\n}\n\nexport class PasswordSession implements Agent {\n /**\n * Internal {@link Agent} used for session management towards the\n * authentication service only.\n */\n #serviceAgent: Agent\n\n #sessionData: null | SessionData\n #sessionPromise: Promise<SessionData>\n\n constructor(\n sessionData: SessionData,\n protected readonly options: PasswordSessionOptions = {},\n ) {\n this.#serviceAgent = buildAgent({\n service: sessionData.service,\n fetch: options.fetch,\n })\n\n this.#sessionData = sessionData\n this.#sessionPromise = Promise.resolve(this.#sessionData)\n }\n\n get did() {\n return this.session.did\n }\n\n get handle() {\n return this.session.handle\n }\n\n get session() {\n if (this.#sessionData) return this.#sessionData\n throw new Error('Logged out')\n }\n\n get destroyed(): boolean {\n return this.#sessionData === null\n }\n\n async fetchHandler(path: string, init: RequestInit): Promise<Response> {\n const headers = new Headers(init.headers)\n if (headers.has('authorization')) {\n throw new TypeError(\"Unexpected 'authorization' header set\")\n }\n\n const sessionPromise = this.#sessionPromise\n const sessionData = await sessionPromise\n\n const fetch = this.options.fetch ?? globalThis.fetch\n\n headers.set('authorization', `Bearer ${sessionData.accessJwt}`)\n const initialRes = await fetch(fetchUrl(sessionData, path), {\n ...init,\n headers,\n })\n\n const refreshNeeded =\n initialRes.status === 401 ||\n (initialRes.status === 400 &&\n (await extractXrpcErrorCode(initialRes)) === 'ExpiredToken')\n\n if (!refreshNeeded) {\n return initialRes\n }\n\n // Refresh session (unless it was already refreshed in the meantime)\n const newSessionPromise =\n this.#sessionPromise === sessionPromise\n ? this.refresh()\n : this.#sessionPromise\n\n // Error should have been propagated through hooks\n const newSessionData = await newSessionPromise.catch((_err) => null)\n if (!newSessionData) {\n return initialRes\n }\n\n // refresh silently failed, no point in retrying.\n if (newSessionData.accessJwt === sessionData.accessJwt) {\n return initialRes\n }\n\n if (init?.signal?.aborted) {\n return initialRes\n }\n\n // The stream was already consumed. We cannot retry the request. A solution\n // would be to tee() the input stream but that would bufferize the entire\n // stream in memory which can lead to memory starvation. Instead, we will\n // return the original response and let the calling code handle retries.\n if (ReadableStream && init?.body instanceof ReadableStream) {\n return initialRes\n }\n\n // Make sure the initial request is cancelled to avoid leaking resources\n // (NodeJS 👀): https://undici.nodejs.org/#/?id=garbage-collection\n if (!initialRes.bodyUsed) {\n await initialRes.body?.cancel()\n }\n\n // Finally, retry the request with the new access token\n headers.set('authorization', `Bearer ${newSessionData.accessJwt}`)\n return fetch(fetchUrl(newSessionData, path), { ...init, headers })\n }\n\n async refresh(): Promise<SessionData> {\n this.#sessionPromise = this.#sessionPromise.then(async (sessionData) => {\n const response = await xrpcSafe(\n this.#serviceAgent,\n com.atproto.server.refreshSession.main,\n { headers: { Authorization: `Bearer ${sessionData.refreshJwt}` } },\n )\n\n if (!response.success && response.matchesSchema()) {\n // Expected errors that indicate the session is no longer valid\n await this.options.onDeleted?.call(this, sessionData)\n\n // Update the session promise to a rejected state\n this.#sessionData = null\n throw response\n }\n\n if (!response.success) {\n response.error\n if (response.matchesSchema()) {\n response.error\n }\n // We failed to refresh the token, assume the session might still be\n // valid by returning the existing session.\n await this.options.onUpdateFailure?.call(this, sessionData, response)\n\n return sessionData\n }\n\n const data = response.body\n\n // Historically, refreshSession did not return all the fields from\n // getSession. In particular, emailConfirmed and didDoc were missing.\n // Similarly, some servers might not return the didDoc in refreshSession.\n // We fetch them via getSession if missing, allowing to ensure that we are\n // always talking with the right PDS.\n if (data.emailConfirmed == null || data.didDoc == null) {\n const extraData = await xrpcSafe(\n this.#serviceAgent,\n com.atproto.server.getSession.main,\n { headers: { Authorization: `Bearer ${data.accessJwt}` } },\n )\n if (extraData.success && extraData.body.did === data.did) {\n Object.assign(data, extraData.body)\n }\n }\n\n const newSession: SessionData = {\n ...data,\n service: sessionData.service,\n }\n\n await this.options.onUpdated?.call(this, newSession)\n\n return (this.#sessionData = newSession)\n })\n\n return this.#sessionPromise\n }\n\n async logout(): Promise<void> {\n let reason: DeleteFailure | null = null\n\n this.#sessionPromise = this.#sessionPromise.then(async (sessionData) => {\n const result = await xrpcSafe(\n this.#serviceAgent,\n com.atproto.server.deleteSession.main,\n { headers: { Authorization: `Bearer ${sessionData.refreshJwt}` } },\n )\n\n if (result.success || result.matchesSchema()) {\n await this.options.onDeleted?.call(this, sessionData)\n\n // Update the session promise to a rejected state\n this.#sessionData = null\n throw new Error('Logged out')\n } else {\n // Capture the reason for the failure to re-throw in the outer promise\n reason = result\n\n // An unknown/unexpected error occurred (network, server down, etc)\n await this.options.onDeleteFailure?.call(this, sessionData, result)\n\n // Keep the session in an active state\n return sessionData\n }\n })\n\n return this.#sessionPromise.then(\n (_session) => {\n // If the promise above resolved, then logout failed. Re-throw the\n // reason captured earlier.\n throw reason!\n },\n (_err) => {\n // Successful logout\n },\n )\n }\n\n static async createAccount(\n body: com.atproto.server.createAccount.InputBody,\n {\n service,\n headers,\n ...options\n }: PasswordSessionOptions & {\n headers?: HeadersInit\n service: string | URL\n },\n ): Promise<PasswordSession> {\n const response = await xrpc(\n buildAgent({ service, headers, fetch: options.fetch }),\n com.atproto.server.createAccount.main,\n { body },\n )\n\n const data: SessionData = {\n ...response.body,\n service: String(service),\n }\n\n const agent = new PasswordSession(data, options)\n await options.onUpdated?.call(agent, data)\n return agent\n }\n\n /**\n * @note It is **not** recommended to use {@link PasswordSession} with main\n * account credentials. Instead, it is strongly advised to use OAuth based\n * authentication for main username/password credentials and use\n * {@link PasswordSession} with an app-password, for bots, scripts, or similar\n * use-cases.\n *\n * @throws If unable to create a session. In particular, if the server\n * requires a 2FA token, a {@link XrpcResponseError} with the\n * `AuthFactorTokenRequired` error code will be thrown.\n *\n *\n * @example Handling 2FA errors\n *\n * ```ts\n * try {\n * const session = await PasswordSession.login({\n * service: 'https://example.com',\n * identifier: 'alice',\n * password: 'correct horse battery staple',\n * })\n * } catch (err) {\n * if (err instanceof XrpcResponseError && err.error === 'AuthFactorTokenRequired') {\n * // Prompt user for 2FA token and re-attempt session creation\n * }\n * }\n * ```\n */\n static async login({\n service,\n identifier,\n password,\n allowTakendown,\n authFactorToken,\n ...options\n }: PasswordSessionOptions & {\n service: string | URL\n identifier: string\n password: string\n allowTakendown?: boolean\n authFactorToken?: string\n }): Promise<PasswordSession> {\n const xrpcAgent = buildAgent({\n service,\n fetch: options.fetch,\n })\n\n const response = await xrpcSafe(\n xrpcAgent,\n com.atproto.server.createSession.main,\n { body: { identifier, password, allowTakendown, authFactorToken } },\n )\n\n if (!response.success) {\n if (response.error === 'AuthFactorTokenRequired') {\n throw new LexAuthFactorError(response)\n }\n throw response.reason\n }\n\n const data: SessionData = {\n ...response.body,\n service: String(service),\n }\n\n const agent = new PasswordSession(data, options)\n await options.onUpdated?.call(agent, data)\n return agent\n }\n\n /**\n * Resume an existing session, ensuring it is still valid by refreshing it.\n * Any error thrown here indicates that the session is definitely no longer\n * valid. Network errors will be propagated through the\n * {@link PasswordSessionOptions.onUpdateFailure} hook, and not re-thrown\n * here. This means that a resolved promise does not necessarily indicate a\n * valid session, only that it's refresh did not definitively fail.\n *\n * This is the same as calling {@link PasswordSession.refresh} after\n * constructing the {@link PasswordSession} manually.\n *\n * @throws If, and only if, the session is definitely no longer valid.\n */\n static async resume(\n data: SessionData,\n options: PasswordSessionOptions,\n ): Promise<PasswordSession> {\n const agent = new PasswordSession(data, options)\n await agent.refresh()\n return agent\n }\n\n /**\n * Delete a session without having to {@link resume resume()} it first, or\n * provide hooks.\n *\n * @throws In case of unexpected error (network issue, server down, etc)\n * meaning that the session may still be valid.\n */\n static async delete(\n data: SessionData,\n options?: PasswordSessionOptions,\n ): Promise<void> {\n const agent = new PasswordSession(data, options)\n await agent.logout()\n }\n}\n\nfunction fetchUrl(sessionData: SessionData, path: string): URL {\n const pdsUrl = extractPdsUrl(sessionData.didDoc)\n return new URL(path, pdsUrl ?? sessionData.service)\n}\n"]}
|
package/dist/util.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { LexMap } from '@atproto/lex-client';
|
|
2
|
-
export declare const noop: () => void;
|
|
3
2
|
export declare function extractXrpcErrorCode(response: Response): Promise<string | null>;
|
|
4
3
|
export declare function extractPdsUrl(didDoc?: LexMap): string | null;
|
|
5
4
|
//# sourceMappingURL=util.d.ts.map
|
package/dist/util.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAY,MAAM,qBAAqB,CAAA;AAGtD,
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAY,MAAM,qBAAqB,CAAA;AAGtD,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,QAAQ,GACjB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAKxB;AA4BD,wBAAgB,aAAa,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAM5D"}
|
package/dist/util.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noop = void 0;
|
|
4
3
|
exports.extractXrpcErrorCode = extractXrpcErrorCode;
|
|
5
4
|
exports.extractPdsUrl = extractPdsUrl;
|
|
6
5
|
const lex_schema_1 = require("@atproto/lex-schema");
|
|
7
|
-
const noop = () => { };
|
|
8
|
-
exports.noop = noop;
|
|
9
6
|
async function extractXrpcErrorCode(response) {
|
|
10
7
|
const json = await peekJson(response, 10 * 1024); // Avoid reading large bodies
|
|
11
8
|
if (json === undefined)
|
|
12
9
|
return null;
|
|
13
|
-
if (!lex_schema_1.
|
|
10
|
+
if (!lex_schema_1.lexErrorDataSchema.matches(json))
|
|
14
11
|
return null;
|
|
15
12
|
return json.error;
|
|
16
13
|
}
|
package/dist/util.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;AAGA,oDAOC;AA4BD,sCAMC;AA3CD,oDAAwD;AAEjD,KAAK,UAAU,oBAAoB,CACxC,QAAkB;IAElB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,QAAQ,EAAE,EAAE,GAAG,IAAI,CAAC,CAAA,CAAC,6BAA6B;IAC9E,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,IAAI,CAAA;IACnC,IAAI,CAAC,+BAAkB,CAAC,OAAO,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAA;IAClD,OAAO,IAAI,CAAC,KAAK,CAAA;AACnB,CAAC;AAED,KAAK,UAAU,QAAQ,CACrB,QAAkB,EAClB,OAAO,GAAG,QAAQ;IAElB,MAAM,IAAI,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAA;IAClC,IAAI,IAAI,KAAK,kBAAkB;QAAE,OAAO,SAAS,CAAA;IACjD,MAAM,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAA;IACtC,IAAI,MAAM,IAAI,IAAI,IAAI,MAAM,GAAG,OAAO;QAAE,OAAO,SAAS,CAAA;IAExD,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAsB,CAAA;IAC7D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAA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ractXrpcErrorCode(\n response: Response,\n): Promise<string | null> {\n const json = await peekJson(response, 10 * 1024) // Avoid reading large bodies\n if (json === undefined) return null\n if (!lexErrorDataSchema.matches(json)) return null\n return json.error\n}\n\nasync function peekJson(\n response: Response,\n maxSize = Infinity,\n): Promise<undefined | LexValue> {\n const type = extractType(response)\n if (type !== 'application/json') return undefined\n const length = extractLength(response)\n if (length != null && length > maxSize) return undefined\n\n try {\n return (await response.clone().json()) as Promise<LexValue>\n } catch {\n return undefined\n }\n}\n\nfunction extractLength({ headers }: Response) {\n return headers.get('Content-Length')\n ? Number(headers.get('Content-Length'))\n : undefined\n}\n\nfunction extractType({ headers }: Response) {\n return headers.get('Content-Type')?.split(';')[0]?.trim().toLowerCase()\n}\n\nexport function extractPdsUrl(didDoc?: LexMap): string | null {\n const pdsService = ifArray(didDoc?.service)?.find((service) =>\n ifString((service as any)?.id)?.endsWith('#atproto_pds'),\n )\n const pdsEndpoint = ifString((pdsService as any)?.serviceEndpoint)\n return pdsEndpoint && URL.canParse(pdsEndpoint) ? pdsEndpoint : null\n}\n\nconst ifString = <T>(v: T) =>\n (typeof v === 'string' ? v : undefined) as unknown extends T\n ? undefined | string\n : T extends string\n ? string\n : undefined\n\nconst ifArray = <T>(v: T) =>\n (Array.isArray(v) ? v : undefined) as unknown extends T\n ? undefined | unknown[]\n : T extends unknown[]\n ? Extract<T, unknown[]>\n : undefined\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/lex-password-session",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Password based client authentication for AT Lexicons",
|
|
6
6
|
"keywords": [
|
|
@@ -36,13 +36,13 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"tslib": "^2.8.1",
|
|
39
|
-
"@atproto/lex-client": "0.0.
|
|
40
|
-
"@atproto/lex-schema": "0.0.
|
|
39
|
+
"@atproto/lex-client": "^0.0.11",
|
|
40
|
+
"@atproto/lex-schema": "^0.0.11"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"vitest": "^4.0.16",
|
|
44
|
-
"@atproto/lex-builder": "0.0.
|
|
45
|
-
"@atproto/lex-server": "0.0.
|
|
44
|
+
"@atproto/lex-builder": "^0.0.13",
|
|
45
|
+
"@atproto/lex-server": "^0.0.8"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"prebuild": "node ./scripts/lex-build.mjs",
|
package/src/error.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { LexError,
|
|
2
|
-
import { com } from './lexicons'
|
|
1
|
+
import { LexError, XrpcFailure } from '@atproto/lex-client'
|
|
3
2
|
|
|
4
3
|
export class LexAuthFactorError extends LexError {
|
|
5
4
|
name = 'LexAuthFactorError'
|
|
6
5
|
|
|
7
|
-
constructor(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
) {
|
|
12
|
-
|
|
6
|
+
constructor(readonly cause: XrpcFailure) {
|
|
7
|
+
super(cause.error, cause.message ?? 'Auth factor token required', { cause })
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
override toResponse(): Response {
|
|
11
|
+
return Response.json({ error: 'InternalServerError' }, { status: 500 })
|
|
13
12
|
}
|
|
14
13
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
2
2
|
|
|
3
3
|
import { afterAll, assert, beforeAll, describe, expect, it, vi } from 'vitest'
|
|
4
|
-
import { Client,
|
|
4
|
+
import { Client, XrpcAuthenticationError } from '@atproto/lex-client'
|
|
5
5
|
import { l } from '@atproto/lex-schema'
|
|
6
6
|
import { LexRouter, LexServerAuthError } from '@atproto/lex-server'
|
|
7
7
|
import { Server, serve } from '@atproto/lex-server/nodejs'
|
|
@@ -171,7 +171,7 @@ describe(PasswordSession, () => {
|
|
|
171
171
|
const onUpdated: PasswordSessionOptions['onUpdated'] = vi.fn()
|
|
172
172
|
|
|
173
173
|
await expect(
|
|
174
|
-
PasswordSession.
|
|
174
|
+
PasswordSession.login({
|
|
175
175
|
...defaultOptions,
|
|
176
176
|
service: entrywayOrigin,
|
|
177
177
|
identifier: 'alice',
|
|
@@ -181,7 +181,6 @@ describe(PasswordSession, () => {
|
|
|
181
181
|
}),
|
|
182
182
|
).rejects.toMatchObject({
|
|
183
183
|
success: false,
|
|
184
|
-
status: 401,
|
|
185
184
|
error: 'AuthenticationRequired',
|
|
186
185
|
})
|
|
187
186
|
|
|
@@ -193,7 +192,7 @@ describe(PasswordSession, () => {
|
|
|
193
192
|
const onDeleted: PasswordSessionOptions['onDeleted'] = vi.fn()
|
|
194
193
|
const onUpdated: PasswordSessionOptions['onUpdated'] = vi.fn()
|
|
195
194
|
|
|
196
|
-
const result = await PasswordSession.
|
|
195
|
+
const result = await PasswordSession.login({
|
|
197
196
|
...defaultOptions,
|
|
198
197
|
service: entrywayOrigin,
|
|
199
198
|
identifier: 'alice',
|
|
@@ -217,7 +216,7 @@ describe(PasswordSession, () => {
|
|
|
217
216
|
const onDeleted: PasswordSessionOptions['onDeleted'] = vi.fn()
|
|
218
217
|
const onUpdated: PasswordSessionOptions['onUpdated'] = vi.fn()
|
|
219
218
|
|
|
220
|
-
const session = await PasswordSession.
|
|
219
|
+
const session = await PasswordSession.login({
|
|
221
220
|
...defaultOptions,
|
|
222
221
|
service: entrywayOrigin,
|
|
223
222
|
identifier: 'alice',
|
|
@@ -253,7 +252,12 @@ describe(PasswordSession, () => {
|
|
|
253
252
|
|
|
254
253
|
await expect(
|
|
255
254
|
client.call(app.example.customMethod, { message: 'hello' }),
|
|
256
|
-
).rejects.
|
|
255
|
+
).rejects.toMatchObject({
|
|
256
|
+
message: 'Unable to fulfill XRPC request',
|
|
257
|
+
cause: expect.objectContaining({
|
|
258
|
+
message: 'Logged out',
|
|
259
|
+
}),
|
|
260
|
+
})
|
|
257
261
|
})
|
|
258
262
|
|
|
259
263
|
it('fails to perform unauthenticated call', async () => {
|
|
@@ -263,22 +267,21 @@ describe(PasswordSession, () => {
|
|
|
263
267
|
})
|
|
264
268
|
|
|
265
269
|
assert(result.success === false)
|
|
266
|
-
assert(result instanceof
|
|
270
|
+
assert(result instanceof XrpcAuthenticationError)
|
|
267
271
|
expect(result).toMatchObject({
|
|
268
272
|
success: false,
|
|
269
|
-
status: 401,
|
|
270
273
|
error: 'AuthenticationRequired',
|
|
271
274
|
})
|
|
272
|
-
expect(result.
|
|
273
|
-
|
|
274
|
-
)
|
|
275
|
+
expect(result.wwwAuthenticate).toEqual({
|
|
276
|
+
Bearer: { realm: 'access token' },
|
|
277
|
+
})
|
|
275
278
|
})
|
|
276
279
|
|
|
277
280
|
it('refreshes expired token', async () => {
|
|
278
281
|
const onDeleted: PasswordSessionOptions['onDeleted'] = vi.fn()
|
|
279
282
|
const onUpdated: PasswordSessionOptions['onUpdated'] = vi.fn()
|
|
280
283
|
|
|
281
|
-
const session = await PasswordSession.
|
|
284
|
+
const session = await PasswordSession.login({
|
|
282
285
|
...defaultOptions,
|
|
283
286
|
service: entrywayOrigin,
|
|
284
287
|
identifier: 'bob',
|
|
@@ -332,7 +335,7 @@ describe(PasswordSession, () => {
|
|
|
332
335
|
const onDeleted: PasswordSessionOptions['onDeleted'] = vi.fn()
|
|
333
336
|
const onUpdated: PasswordSessionOptions['onUpdated'] = vi.fn()
|
|
334
337
|
|
|
335
|
-
const initialAgent = await PasswordSession.
|
|
338
|
+
const initialAgent = await PasswordSession.login({
|
|
336
339
|
...defaultOptions,
|
|
337
340
|
service: entrywayOrigin,
|
|
338
341
|
identifier: 'carla',
|
|
@@ -368,7 +371,6 @@ describe(PasswordSession, () => {
|
|
|
368
371
|
await expect(initialAgent.refresh()).rejects.toMatchObject({
|
|
369
372
|
success: false,
|
|
370
373
|
error: 'ExpiredToken',
|
|
371
|
-
status: 401,
|
|
372
374
|
})
|
|
373
375
|
|
|
374
376
|
expect(onDeleted).toHaveBeenCalledTimes(1)
|
|
@@ -393,7 +395,7 @@ describe(PasswordSession, () => {
|
|
|
393
395
|
it('silently ignores expected logout errors', async () => {
|
|
394
396
|
let sessionData: SessionData | null = null
|
|
395
397
|
|
|
396
|
-
const session = await PasswordSession.
|
|
398
|
+
const session = await PasswordSession.login({
|
|
397
399
|
...defaultOptions,
|
|
398
400
|
service: entrywayOrigin,
|
|
399
401
|
identifier: 'dave',
|
|
@@ -402,7 +404,6 @@ describe(PasswordSession, () => {
|
|
|
402
404
|
onUpdated: (data) => {
|
|
403
405
|
sessionData = structuredClone(data)
|
|
404
406
|
},
|
|
405
|
-
onDeleted: () => {},
|
|
406
407
|
})
|
|
407
408
|
|
|
408
409
|
assert(sessionData)
|
package/src/password-session.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent,
|
|
3
|
-
XrpcError,
|
|
4
3
|
XrpcFailure,
|
|
5
4
|
buildAgent,
|
|
5
|
+
xrpc,
|
|
6
6
|
xrpcSafe,
|
|
7
7
|
} from '@atproto/lex-client'
|
|
8
8
|
import { LexAuthFactorError } from './error.js'
|
|
9
9
|
import { com } from './lexicons/index.js'
|
|
10
|
-
import { extractPdsUrl, extractXrpcErrorCode
|
|
10
|
+
import { extractPdsUrl, extractXrpcErrorCode } from './util.js'
|
|
11
11
|
|
|
12
12
|
export type RefreshFailure = XrpcFailure<
|
|
13
13
|
typeof com.atproto.server.refreshSession.main
|
|
@@ -37,7 +37,7 @@ export type PasswordSessionOptions = {
|
|
|
37
37
|
*
|
|
38
38
|
* @note this function **must** not throw
|
|
39
39
|
*/
|
|
40
|
-
onUpdated
|
|
40
|
+
onUpdated?: (this: PasswordSession, data: SessionData) => void | Promise<void>
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* Called whenever the session update fails due to an expected error, such as
|
|
@@ -61,7 +61,7 @@ export type PasswordSessionOptions = {
|
|
|
61
61
|
*
|
|
62
62
|
* @note this function **must** not throw
|
|
63
63
|
*/
|
|
64
|
-
onDeleted
|
|
64
|
+
onDeleted?: (this: PasswordSession, data: SessionData) => void | Promise<void>
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Called whenever a session deletion fails due to an unexpected error, such
|
|
@@ -96,7 +96,7 @@ export class PasswordSession implements Agent {
|
|
|
96
96
|
|
|
97
97
|
constructor(
|
|
98
98
|
sessionData: SessionData,
|
|
99
|
-
protected readonly options: PasswordSessionOptions,
|
|
99
|
+
protected readonly options: PasswordSessionOptions = {},
|
|
100
100
|
) {
|
|
101
101
|
this.#serviceAgent = buildAgent({
|
|
102
102
|
service: sessionData.service,
|
|
@@ -117,7 +117,7 @@ export class PasswordSession implements Agent {
|
|
|
117
117
|
|
|
118
118
|
get session() {
|
|
119
119
|
if (this.#sessionData) return this.#sessionData
|
|
120
|
-
throw new
|
|
120
|
+
throw new Error('Logged out')
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
get destroyed(): boolean {
|
|
@@ -200,7 +200,7 @@ export class PasswordSession implements Agent {
|
|
|
200
200
|
|
|
201
201
|
if (!response.success && response.matchesSchema()) {
|
|
202
202
|
// Expected errors that indicate the session is no longer valid
|
|
203
|
-
await this.options.onDeleted
|
|
203
|
+
await this.options.onDeleted?.call(this, sessionData)
|
|
204
204
|
|
|
205
205
|
// Update the session promise to a rejected state
|
|
206
206
|
this.#sessionData = null
|
|
@@ -208,6 +208,10 @@ export class PasswordSession implements Agent {
|
|
|
208
208
|
}
|
|
209
209
|
|
|
210
210
|
if (!response.success) {
|
|
211
|
+
response.error
|
|
212
|
+
if (response.matchesSchema()) {
|
|
213
|
+
response.error
|
|
214
|
+
}
|
|
211
215
|
// We failed to refresh the token, assume the session might still be
|
|
212
216
|
// valid by returning the existing session.
|
|
213
217
|
await this.options.onUpdateFailure?.call(this, sessionData, response)
|
|
@@ -238,7 +242,7 @@ export class PasswordSession implements Agent {
|
|
|
238
242
|
service: sessionData.service,
|
|
239
243
|
}
|
|
240
244
|
|
|
241
|
-
await this.options.onUpdated
|
|
245
|
+
await this.options.onUpdated?.call(this, newSession)
|
|
242
246
|
|
|
243
247
|
return (this.#sessionData = newSession)
|
|
244
248
|
})
|
|
@@ -257,11 +261,11 @@ export class PasswordSession implements Agent {
|
|
|
257
261
|
)
|
|
258
262
|
|
|
259
263
|
if (result.success || result.matchesSchema()) {
|
|
260
|
-
await this.options.onDeleted
|
|
264
|
+
await this.options.onDeleted?.call(this, sessionData)
|
|
261
265
|
|
|
262
266
|
// Update the session promise to a rejected state
|
|
263
267
|
this.#sessionData = null
|
|
264
|
-
throw new
|
|
268
|
+
throw new Error('Logged out')
|
|
265
269
|
} else {
|
|
266
270
|
// Capture the reason for the failure to re-throw in the outer promise
|
|
267
271
|
reason = result
|
|
@@ -286,6 +290,33 @@ export class PasswordSession implements Agent {
|
|
|
286
290
|
)
|
|
287
291
|
}
|
|
288
292
|
|
|
293
|
+
static async createAccount(
|
|
294
|
+
body: com.atproto.server.createAccount.InputBody,
|
|
295
|
+
{
|
|
296
|
+
service,
|
|
297
|
+
headers,
|
|
298
|
+
...options
|
|
299
|
+
}: PasswordSessionOptions & {
|
|
300
|
+
headers?: HeadersInit
|
|
301
|
+
service: string | URL
|
|
302
|
+
},
|
|
303
|
+
): Promise<PasswordSession> {
|
|
304
|
+
const response = await xrpc(
|
|
305
|
+
buildAgent({ service, headers, fetch: options.fetch }),
|
|
306
|
+
com.atproto.server.createAccount.main,
|
|
307
|
+
{ body },
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
const data: SessionData = {
|
|
311
|
+
...response.body,
|
|
312
|
+
service: String(service),
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const agent = new PasswordSession(data, options)
|
|
316
|
+
await options.onUpdated?.call(agent, data)
|
|
317
|
+
return agent
|
|
318
|
+
}
|
|
319
|
+
|
|
289
320
|
/**
|
|
290
321
|
* @note It is **not** recommended to use {@link PasswordSession} with main
|
|
291
322
|
* account credentials. Instead, it is strongly advised to use OAuth based
|
|
@@ -302,7 +333,7 @@ export class PasswordSession implements Agent {
|
|
|
302
333
|
*
|
|
303
334
|
* ```ts
|
|
304
335
|
* try {
|
|
305
|
-
* const session = await PasswordSession.
|
|
336
|
+
* const session = await PasswordSession.login({
|
|
306
337
|
* service: 'https://example.com',
|
|
307
338
|
* identifier: 'alice',
|
|
308
339
|
* password: 'correct horse battery staple',
|
|
@@ -314,7 +345,7 @@ export class PasswordSession implements Agent {
|
|
|
314
345
|
* }
|
|
315
346
|
* ```
|
|
316
347
|
*/
|
|
317
|
-
static async
|
|
348
|
+
static async login({
|
|
318
349
|
service,
|
|
319
350
|
identifier,
|
|
320
351
|
password,
|
|
@@ -352,7 +383,7 @@ export class PasswordSession implements Agent {
|
|
|
352
383
|
}
|
|
353
384
|
|
|
354
385
|
const agent = new PasswordSession(data, options)
|
|
355
|
-
await options.onUpdated
|
|
386
|
+
await options.onUpdated?.call(agent, data)
|
|
356
387
|
return agent
|
|
357
388
|
}
|
|
358
389
|
|
|
@@ -387,13 +418,9 @@ export class PasswordSession implements Agent {
|
|
|
387
418
|
*/
|
|
388
419
|
static async delete(
|
|
389
420
|
data: SessionData,
|
|
390
|
-
options?:
|
|
421
|
+
options?: PasswordSessionOptions,
|
|
391
422
|
): Promise<void> {
|
|
392
|
-
const agent = new PasswordSession(data,
|
|
393
|
-
...options,
|
|
394
|
-
onUpdated: options?.onUpdated ?? noop,
|
|
395
|
-
onDeleted: options?.onDeleted ?? noop,
|
|
396
|
-
})
|
|
423
|
+
const agent = new PasswordSession(data, options)
|
|
397
424
|
await agent.logout()
|
|
398
425
|
}
|
|
399
426
|
}
|
package/src/util.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { LexMap, LexValue } from '@atproto/lex-client'
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export const noop = () => {}
|
|
2
|
+
import { lexErrorDataSchema } from '@atproto/lex-schema'
|
|
5
3
|
|
|
6
4
|
export async function extractXrpcErrorCode(
|
|
7
5
|
response: Response,
|
|
8
6
|
): Promise<string | null> {
|
|
9
7
|
const json = await peekJson(response, 10 * 1024) // Avoid reading large bodies
|
|
10
8
|
if (json === undefined) return null
|
|
11
|
-
if (!
|
|
9
|
+
if (!lexErrorDataSchema.matches(json)) return null
|
|
12
10
|
return json.error
|
|
13
11
|
}
|
|
14
12
|
|