Skip to content

Commit

Permalink
Merge pull request #149 from jasonraimondi/fix/support-readable-streams
Browse files Browse the repository at this point in the history
fix(break): support readable streams in response to vanilla
  • Loading branch information
jasonraimondi authored Aug 5, 2024
2 parents a3a88f7 + 11108e3 commit 962241c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/docs/adapters/vanilla.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ responseFromVanilla(res: Response): OAuthResponse
```

```ts
requestFromVanilla(req: Request): OAuthRequest
requestFromVanilla(req: Request): Promise<OAuthRequest>
```

```ts
Expand Down
61 changes: 43 additions & 18 deletions src/adapters/vanilla.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OAuthRequest } from "../requests/request.js";
import { OAuthResponse } from "../responses/response.js";
import { ErrorType, OAuthException } from "../exceptions/oauth.exception.js";
import type { ReadableStream } from "stream/web";

export function responseFromVanilla(res: Response): OAuthResponse {
const headers: Record<string, unknown> = {};
Expand All @@ -13,16 +14,38 @@ export function responseFromVanilla(res: Response): OAuthResponse {
});
}

export function requestFromVanilla(req: Request): OAuthRequest {
export function responseToVanilla(oauthResponse: OAuthResponse): Response {
if (oauthResponse.status === 302) {
if (!oauthResponse.headers.location) {
throw new OAuthException(`missing redirect location`, ErrorType.InvalidRequest);
}
return new Response(null, {
status: 302,
headers: {
Location: oauthResponse.headers.location,
},
});
}

return new Response(JSON.stringify(oauthResponse.body), {
status: oauthResponse.status,
headers: oauthResponse.headers,
});
}

export async function requestFromVanilla(req: Request): Promise<OAuthRequest> {
const url = new URL(req.url);
const query: Record<string, unknown> = {};
url.searchParams.forEach((value, key) => {
query[key] = value;
});

let body: Record<string, unknown> = {};
if (req.body != null) {
body = JSON.parse(req.body.toString());

if (isReadableStream(req.body)) {
body = JSON.parse(await streamToString(req.body));
} else if (req.body != null) {
body = JSON.parse(req.body);
}

const headers: Record<string, unknown> = {};
Expand All @@ -37,21 +60,23 @@ export function requestFromVanilla(req: Request): OAuthRequest {
});
}

export function responseToVanilla(oauthResponse: OAuthResponse): Response {
if (oauthResponse.status === 302) {
if (!oauthResponse.headers.location) {
throw new OAuthException(`missing redirect location`, ErrorType.InvalidRequest);
}
return new Response(null, {
status: 302,
headers: {
Location: oauthResponse.headers.location,
},
});
async function streamToString(stream: ReadableStream): Promise<string> {
const reader = stream.getReader();
let result = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
result += new TextDecoder().decode(value);
}
return result;
}

return new Response(JSON.stringify(oauthResponse.body), {
status: oauthResponse.status,
headers: oauthResponse.headers,
});
function isReadableStream(value: unknown): value is ReadableStream {
return (
value !== null &&
typeof value === "object" &&
typeof (value as ReadableStream).getReader === "function" &&
typeof (value as ReadableStream).tee === "function" &&
typeof (value as ReadableStream).locked !== "undefined"
);
}

0 comments on commit 962241c

Please sign in to comment.