-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95d8b7d
commit 4bdfdce
Showing
26 changed files
with
274 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {getDurableRequestStore} from "./store"; | ||
|
||
export function deleteDurableRequest(durableRequestId: string) { | ||
const store = getDurableRequestStore(); | ||
return store.delete(durableRequestId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {DurableRequest, DurableRequestData, DurableResponseData} from "./types"; | ||
|
||
export function fromDurableRequest(durableRequest: DurableRequestData, getOrigin?: () => string) { | ||
const { url, method, headers, body } = durableRequest; | ||
return new Request( | ||
new URL(url, getOrigin?.()), | ||
{ | ||
method, | ||
headers, | ||
body | ||
} | ||
); | ||
} | ||
|
||
export function fromDurableResponse(durableResponse: DurableResponseData) { | ||
const { body, statusText, status, headers } = durableResponse; | ||
return new Response( | ||
body, | ||
{ | ||
status, | ||
statusText, | ||
headers | ||
} | ||
); | ||
} | ||
|
||
export async function fromRequestResponse(request: Request, response: Response) { | ||
const clonedResponse = response.clone(); | ||
|
||
const durableResponse: DurableResponseData = { | ||
headers: getResponseHeadersObject(), | ||
status: response.status, | ||
statusText: response.statusText, | ||
// response.url is empty if it was constructed manually | ||
// Should be same value anyway... | ||
url: response.url || request.url, | ||
// TODO investigate non string responses and storage | ||
// we could just use something like | ||
// await save(`fetch/cache/${durableRequestId}`, Buffer.from(await clonedResponse.arrayBuffer())) | ||
body: await clonedResponse.text() | ||
}; | ||
|
||
const createdAt = new Date().toISOString(); | ||
const { method, url } = request; | ||
|
||
return { | ||
durableRequestId: `${method}:${url}`, | ||
method, | ||
url, | ||
response: durableResponse, | ||
createdAt, | ||
updatedAt: createdAt | ||
}; | ||
|
||
function getResponseHeadersObject() { | ||
const headers = new Headers(response.headers); | ||
// Not sure if we ever get this header in node fetch | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Cache#cookies_and_cache_objects | ||
// Maybe these headers were constructed by a user though | ||
headers.delete("Set-Cookie"); | ||
return getHeadersObject(headers); | ||
} | ||
|
||
} | ||
|
||
function getHeadersObject(headers?: Headers) { | ||
const output: Record<string, string> = {}; | ||
if (!headers) { | ||
return output; | ||
} | ||
headers.forEach((value, key) => { | ||
output[key] = value; | ||
}) | ||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {getDurableRequestStore} from "./store"; | ||
import {DurableEvent} from "../durable-event"; | ||
|
||
export function getDurableRequest(durableRequestId: string) { | ||
const store = getDurableRequestStore(); | ||
return store.get(durableRequestId); | ||
} | ||
|
||
export function getDurableRequestIdForEvent(event: DurableEvent) { | ||
return `${event.type}:request:${event.durableEventId}`; | ||
} | ||
|
||
export function getDurableRequestForEvent(event: DurableEvent) { | ||
return getDurableRequest( | ||
getDurableRequestIdForEvent(event) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from "./types"; | ||
export * from "./store"; | ||
export * from "./set-durable-request"; | ||
export * from "./delete-durable-request"; | ||
export * from "./get-durable-request"; | ||
export * from "./list-durable-requests"; | ||
export * from "./from"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {getDurableRequestStore} from "./store"; | ||
|
||
export function listDurableRequests() { | ||
const store = getDurableRequestStore(); | ||
return store.values(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {v4} from "uuid"; | ||
import {DurableRequest, PartialDurableRequest} from "./types"; | ||
import {getDurableRequestStore} from "./store"; | ||
import {DurableEvent} from "../durable-event"; | ||
import {getDurableRequestIdForEvent} from "./get-durable-request"; | ||
|
||
|
||
export async function setDurableRequest(data: PartialDurableRequest) { | ||
const createdAt = new Date().toISOString(); | ||
const durableRequestId = data.durableRequestId || v4(); | ||
const durableRequest: DurableRequest = { | ||
...data, | ||
createdAt, | ||
updatedAt: createdAt, | ||
durableRequestId, | ||
}; | ||
const store = getDurableRequestStore(); | ||
await store.set(durableRequestId, durableRequest); | ||
return durableRequest; | ||
} | ||
|
||
export function setDurableRequestForEvent(data: PartialDurableRequest, event: DurableEvent) { | ||
return setDurableRequest({ | ||
...data, | ||
durableRequestId: getDurableRequestIdForEvent(event) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {getExpiringStore} from "../expiring-kv"; | ||
import {DurableRequest} from "./types"; | ||
|
||
const STORE_NAME = "request" as const; | ||
|
||
export interface DurableRequestStoreOptions { | ||
name?: string; | ||
prefix?: string; | ||
} | ||
|
||
export function getDurableRequestStore({ name, prefix }: DurableRequestStoreOptions = {}) { | ||
return getExpiringStore<DurableRequest>(name || STORE_NAME, { | ||
counter: false, | ||
prefix | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.