@standardserver/peer
Advanced tools
+7
-3
@@ -1,2 +0,2 @@ | ||
| import { StandardRequest, EventStreamMessage, StandardResponse, StandardLazyResponse, StandardLazyRequest } from '@standardserver/core'; | ||
| import { StandardRequest, StandardBodyHint, EventStreamMessage, StandardResponse, StandardLazyResponse, StandardLazyRequest } from '@standardserver/core'; | ||
| import { Queue, AsyncCleanupFn, AsyncIteratorClass } from '@standardserver/shared'; | ||
@@ -40,3 +40,5 @@ | ||
| */ | ||
| json: Omit<StandardRequest, 'signal'>; | ||
| json: Omit<StandardRequest, 'signal'> & { | ||
| bodyHint: StandardBodyHint; | ||
| }; | ||
| } | ||
@@ -56,3 +58,5 @@ /** | ||
| */ | ||
| json: StandardResponse; | ||
| json: StandardResponse & { | ||
| bodyHint: StandardBodyHint; | ||
| }; | ||
| } | ||
@@ -59,0 +63,0 @@ /** |
+7
-3
@@ -1,2 +0,2 @@ | ||
| import { StandardRequest, EventStreamMessage, StandardResponse, StandardLazyResponse, StandardLazyRequest } from '@standardserver/core'; | ||
| import { StandardRequest, StandardBodyHint, EventStreamMessage, StandardResponse, StandardLazyResponse, StandardLazyRequest } from '@standardserver/core'; | ||
| import { Queue, AsyncCleanupFn, AsyncIteratorClass } from '@standardserver/shared'; | ||
@@ -40,3 +40,5 @@ | ||
| */ | ||
| json: Omit<StandardRequest, 'signal'>; | ||
| json: Omit<StandardRequest, 'signal'> & { | ||
| bodyHint: StandardBodyHint; | ||
| }; | ||
| } | ||
@@ -56,3 +58,5 @@ /** | ||
| */ | ||
| json: StandardResponse; | ||
| json: StandardResponse & { | ||
| bodyHint: StandardBodyHint; | ||
| }; | ||
| } | ||
@@ -59,0 +63,0 @@ /** |
+83
-71
@@ -1,2 +0,2 @@ | ||
| import { AsyncIteratorClass, isTypescriptObject, isAsyncIteratorObject, Queue, SequentialIdGenerator, omit, AbortError, stringifyJSON } from '@standardserver/shared'; | ||
| import { AsyncIteratorClass, isTypescriptObject, isAsyncIteratorObject, Queue, SequentialIdGenerator, AbortError, stringifyJSON } from '@standardserver/shared'; | ||
| import { withEventMeta, ErrorEvent, unwrapEvent, generateContentDisposition, flattenStandardHeader, getFilenameFromContentDisposition, isStandardRequest, isStandardResponse } from '@standardserver/core'; | ||
@@ -159,33 +159,27 @@ | ||
| function toStandardBody(message, cleanup) { | ||
| const bodyHint = flattenStandardHeader(message.json.headers["standard-server"]); | ||
| const mimeType = flattenStandardHeader(message.json.headers["content-type"])?.split(";")[0]?.trim(); | ||
| if (message.json.body === void 0 && message.binary === void 0) { | ||
| if (bodyHint === "event-stream" || bodyHint === void 0 && mimeType === "text/event-stream") { | ||
| const eventStreamMessageQueue = new Queue(); | ||
| return { | ||
| resolveBody: async () => toAsyncIteratorObject(eventStreamMessageQueue, cleanup), | ||
| eventStreamMessageQueue | ||
| }; | ||
| } | ||
| if (bodyHint === "octet-stream" || bodyHint === void 0 && mimeType !== void 0 && flattenStandardHeader(message.json.headers["content-length"]) === void 0) { | ||
| const octetStreamMessageQueue = new Queue(); | ||
| return { | ||
| resolveBody: async () => toOctetStream(octetStreamMessageQueue, cleanup), | ||
| octetStreamMessageQueue | ||
| }; | ||
| } | ||
| const bodyHint = message.json.bodyHint; | ||
| if (bodyHint === "event-stream") { | ||
| const eventStreamMessageQueue = new Queue(); | ||
| return { | ||
| resolveBody: async () => toAsyncIteratorObject(eventStreamMessageQueue, cleanup), | ||
| eventStreamMessageQueue | ||
| }; | ||
| } | ||
| if (bodyHint === "octet-stream") { | ||
| const octetStreamMessageQueue = new Queue(); | ||
| return { | ||
| resolveBody: async () => toOctetStream(octetStreamMessageQueue, cleanup), | ||
| octetStreamMessageQueue | ||
| }; | ||
| } | ||
| const resolveBody = async () => { | ||
| let errorRef; | ||
| try { | ||
| if ((bodyHint === "url-search-params" || bodyHint === void 0 && mimeType === "application/x-www-form-urlencoded") && typeof message.json.body === "string") { | ||
| if (bodyHint === "url-search-params") { | ||
| if (typeof message.json.body !== "string") { | ||
| throw new TypeError("Expected body to be a string for url-search-params bodyHint"); | ||
| } | ||
| return new URLSearchParams(message.json.body); | ||
| } | ||
| if (bodyHint === "none" || bodyHint === void 0 && mimeType === void 0 && message.json.body === void 0 && message.binary === void 0) { | ||
| return void 0; | ||
| } | ||
| if (message.json.body !== void 0) { | ||
| return message.json.body; | ||
| } | ||
| if (bodyHint === "form-data" || bodyHint === void 0 && mimeType === "multipart/form-data") { | ||
| if (bodyHint === "form-data") { | ||
| const res = new Response(message.binary, { | ||
@@ -196,11 +190,18 @@ headers: { | ||
| }); | ||
| const body2 = await res.formData(); | ||
| return body2; | ||
| const form = await res.formData(); | ||
| return form; | ||
| } | ||
| const contentDisposition = flattenStandardHeader(message.json.headers["content-disposition"]); | ||
| const filename = contentDisposition !== void 0 ? getFilenameFromContentDisposition(contentDisposition) : void 0; | ||
| const body = new File(message.binary ? [message.binary] : [], filename ?? "blob", { | ||
| type: flattenStandardHeader(message.json.headers["content-type"]) ?? "application/octet-stream" | ||
| }); | ||
| return body; | ||
| if (bodyHint === "file") { | ||
| const contentDisposition = flattenStandardHeader(message.json.headers["content-disposition"]); | ||
| const filename = contentDisposition !== void 0 ? getFilenameFromContentDisposition(contentDisposition) : void 0; | ||
| const file = new File(message.binary ? [message.binary] : [], filename ?? "blob", { | ||
| type: flattenStandardHeader(message.json.headers["content-type"]) ?? "application/octet-stream" | ||
| }); | ||
| return file; | ||
| } | ||
| if (bodyHint === "none") { | ||
| return void 0; | ||
| } | ||
| const _expect = bodyHint; | ||
| return message.json.body; | ||
| } catch (error) { | ||
@@ -217,10 +218,6 @@ errorRef = { value: error }; | ||
| headers = { ...headers }; | ||
| let binary; | ||
| let json; | ||
| if (body instanceof ReadableStream) { | ||
| headers["standard-server"] = "octet-stream"; | ||
| headers["content-type"] ??= "application/octet-stream"; | ||
| } else if (body instanceof Blob) { | ||
| binary = body; | ||
| headers["standard-server"] = "file"; | ||
| return { bodyHint: "octet-stream", jsonBody: void 0, headers, binary: void 0 }; | ||
| } | ||
| if (body instanceof Blob) { | ||
| headers["content-type"] = body.type; | ||
@@ -233,23 +230,21 @@ headers["content-disposition"] ??= generateContentDisposition( | ||
| } | ||
| } else { | ||
| headers["standard-server"] = void 0; | ||
| headers["content-length"] = void 0; | ||
| if (isAsyncIteratorObject(body)) { | ||
| headers["content-type"] = "text/event-stream"; | ||
| } else if (body instanceof FormData) { | ||
| const res = new Response(body); | ||
| binary = await res.blob(); | ||
| headers["content-type"] = res.headers.get("content-type"); | ||
| headers["content-length"] = binary.size.toString(); | ||
| } else if (body instanceof URLSearchParams) { | ||
| json = body.toString(); | ||
| headers["content-type"] = "application/x-www-form-urlencoded"; | ||
| } else if (body === void 0) { | ||
| headers["content-type"] = void 0; | ||
| } else { | ||
| json = body; | ||
| headers["content-type"] = "application/json"; | ||
| } | ||
| return { bodyHint: "file", jsonBody: void 0, headers, binary: body }; | ||
| } | ||
| return [json, headers, binary]; | ||
| if (isAsyncIteratorObject(body)) { | ||
| return { bodyHint: "event-stream", jsonBody: void 0, headers, binary: void 0 }; | ||
| } | ||
| if (body instanceof FormData) { | ||
| const res = new Response(body); | ||
| const blob = await res.blob(); | ||
| headers["content-type"] = res.headers.get("content-type"); | ||
| headers["content-length"] = blob.size.toString(); | ||
| return { bodyHint: "form-data", jsonBody: void 0, headers, binary: blob }; | ||
| } | ||
| if (body instanceof URLSearchParams) { | ||
| return { bodyHint: "url-search-params", jsonBody: body.toString(), headers, binary: void 0 }; | ||
| } | ||
| if (body === void 0) { | ||
| return { bodyHint: "none", jsonBody: void 0, headers, binary: void 0 }; | ||
| } | ||
| return { bodyHint: "json", jsonBody: body, headers, binary: void 0 }; | ||
| } | ||
@@ -279,3 +274,3 @@ | ||
| try { | ||
| const [jsonBody, headers, binary] = await encodeAtomicStandardBody(request.body, request.headers); | ||
| const encodedAtomicBody = await encodeAtomicStandardBody(request.body, request.headers); | ||
| signal?.throwIfAborted(); | ||
@@ -286,7 +281,9 @@ await this.send({ | ||
| json: { | ||
| ...omit(request, ["signal"]), | ||
| headers, | ||
| body: jsonBody | ||
| method: request.method, | ||
| url: request.url, | ||
| headers: encodedAtomicBody.headers, | ||
| bodyHint: encodedAtomicBody.bodyHint, | ||
| body: encodedAtomicBody.jsonBody | ||
| }, | ||
| binary | ||
| binary: encodedAtomicBody.binary | ||
| }); | ||
@@ -366,3 +363,7 @@ signal?.throwIfAborted(); | ||
| state.octetStreamMessageQueue = decoded.octetStreamMessageQueue; | ||
| resolve({ ...message.json, resolveBody: decoded.resolveBody }); | ||
| resolve({ | ||
| headers: message.json.headers, | ||
| status: message.json.status, | ||
| resolveBody: decoded.resolveBody | ||
| }); | ||
| if (!state.eventStreamMessageQueue && !state.octetStreamMessageQueue) { | ||
@@ -612,7 +613,13 @@ await this.closeById(id); | ||
| state.octetStreamMessageQueue = decoded.octetStreamMessageQueue; | ||
| const response = await handleRequest({ ...message.json, signal, resolveBody: decoded.resolveBody }); | ||
| const response = await handleRequest({ | ||
| url: message.json.url, | ||
| method: message.json.method, | ||
| headers: message.json.headers, | ||
| resolveBody: decoded.resolveBody, | ||
| signal | ||
| }); | ||
| if (signal.aborted) { | ||
| return; | ||
| } | ||
| const [jsonBody, headers, binary] = await encodeAtomicStandardBody(response.body, response.headers); | ||
| const encodedAtomicBody = await encodeAtomicStandardBody(response.body, response.headers); | ||
| if (signal.aborted) { | ||
@@ -624,4 +631,9 @@ return; | ||
| kind: "response", | ||
| json: { ...response, headers, body: jsonBody }, | ||
| binary | ||
| json: { | ||
| status: response.status, | ||
| headers: encodedAtomicBody.headers, | ||
| bodyHint: encodedAtomicBody.bodyHint, | ||
| body: encodedAtomicBody.jsonBody | ||
| }, | ||
| binary: encodedAtomicBody.binary | ||
| }; | ||
@@ -628,0 +640,0 @@ await this.send(responseMessage); |
+3
-3
| { | ||
| "name": "@standardserver/peer", | ||
| "type": "module", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "license": "MIT", | ||
@@ -25,4 +25,4 @@ "homepage": "https://standardserver.dev", | ||
| "dependencies": { | ||
| "@standardserver/core": "0.1.0", | ||
| "@standardserver/shared": "0.1.0" | ||
| "@standardserver/shared": "0.2.0", | ||
| "@standardserver/core": "0.2.0" | ||
| }, | ||
@@ -29,0 +29,0 @@ "scripts": { |
+1
-1
@@ -270,5 +270,5 @@ # @standardserver/peer | ||
| <a href="https://github.com/Scrumplex?ref=orpc" target="_blank" rel="noopener" title="Sefa Eyeoglu"><img src="https://avatars.githubusercontent.com/u/11587657?u=ab503582165c0bbff0cca47ce31c9450bb1553c9&v=4" width="32" height="32" alt="Sefa Eyeoglu" /></a> | ||
| <a href="https://github.com/nattstack?ref=orpc" target="_blank" rel="noopener" title="Natt"><img src="https://avatars.githubusercontent.com/u/31426677?u=fa9dbb8b3e66eb0ea3c88db5dc07f31c8c5418fe&v=4" width="32" height="32" alt="Natt" /></a> | ||
| <a href="https://github.com/nattstack?ref=orpc" target="_blank" rel="noopener" title="NΛTT"><img src="https://avatars.githubusercontent.com/u/31426677?u=fa9dbb8b3e66eb0ea3c88db5dc07f31c8c5418fe&v=4" width="32" height="32" alt="NΛTT" /></a> | ||
| <a href="https://github.com/ChromeGG?ref=orpc" target="_blank" rel="noopener" title="Adam Tkaczyk"><img src="https://avatars.githubusercontent.com/u/39050595?u=a58ca6042a6950e94e6e92442db76ef584279bc0&v=4" width="32" height="32" alt="Adam Tkaczyk" /></a> | ||
| <a href="https://github.com/plancraft?ref=orpc" target="_blank" rel="noopener" title="plancraft"><img src="https://avatars.githubusercontent.com/u/46482287?v=4" width="32" height="32" alt="plancraft" /></a> | ||
| </p> |
71624
0.16%970
1.68%+ Added
+ Added
- Removed
- Removed
Updated
Updated