@standardserver/peer
Advanced tools
+1
-0
@@ -208,2 +208,3 @@ import { StandardMethod, StandardUrl, StandardHeaders, StandardBody, EventMeta, StandardRequest, StandardLazyResponse, StandardLazyRequest, StandardResponse } from '@standardserver/core'; | ||
| request(request: StandardRequest): Promise<StandardLazyResponse>; | ||
| private transmitRequest; | ||
| /** | ||
@@ -210,0 +211,0 @@ * Handle a message from server |
+1
-0
@@ -208,2 +208,3 @@ import { StandardMethod, StandardUrl, StandardHeaders, StandardBody, EventMeta, StandardRequest, StandardLazyResponse, StandardLazyRequest, StandardResponse } from '@standardserver/core'; | ||
| request(request: StandardRequest): Promise<StandardLazyResponse>; | ||
| private transmitRequest; | ||
| /** | ||
@@ -210,0 +211,0 @@ * Handle a message from server |
+74
-55
@@ -1,2 +0,2 @@ | ||
| import { AsyncIteratorClass, isTypescriptObject, isAsyncIteratorObject, Queue, SequentialIdGenerator, AbortError, stringifyJSON } from '@standardserver/shared'; | ||
| import { AsyncIteratorClass, isTypescriptObject, isAsyncIteratorObject, Queue, SequentialIdGenerator, hasAnyDefinedValue, AbortError, stringifyJSON } from '@standardserver/shared'; | ||
| import { withEventMeta, ErrorEvent, unwrapEvent, generateContentDisposition, flattenStandardHeader, getFilenameFromContentDisposition, isStandardMethod, isStandardHeaders, isStandardUrl, isStandardStatus } from '@standardserver/core'; | ||
@@ -195,4 +195,4 @@ | ||
| 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" | ||
| const file = new File([message.binary], filename ?? "blob", { | ||
| type: contentType ?? "application/octet-stream" | ||
| }); | ||
@@ -226,3 +226,3 @@ return file; | ||
| headers["content-disposition"] ??= generateContentDisposition( | ||
| body instanceof File ? body.name : "blob" | ||
| body instanceof File ? body.name ?? "" : "blob" | ||
| ); | ||
@@ -244,3 +244,3 @@ if (Number.isFinite(body.size)) { | ||
| headers["standard-server"] = "form-data"; | ||
| headers["content-type"] = res.headers.get("content-type"); | ||
| headers["content-type"] = blob.type; | ||
| headers["content-length"] = blob.size.toString(); | ||
@@ -265,17 +265,27 @@ return { jsonBody: void 0, headers, binary: blob }; | ||
| */ | ||
| async request(request) { | ||
| const signal = request.signal; | ||
| signal?.throwIfAborted(); | ||
| const id = this.idGenerator.generate(); | ||
| const state = {}; | ||
| this.requests.set(id, state); | ||
| let abortListener; | ||
| signal?.addEventListener("abort", abortListener = () => this.abortById(id, signal.reason)); | ||
| state.cleanupFns ??= []; | ||
| state.cleanupFns.push(() => { | ||
| signal?.removeEventListener("abort", abortListener); | ||
| request(request) { | ||
| return new Promise((resolve, reject) => { | ||
| const signal = request.signal; | ||
| signal?.throwIfAborted(); | ||
| const id = this.idGenerator.generate(); | ||
| const state = { resolve, reject }; | ||
| this.requests.set(id, state); | ||
| if (signal) { | ||
| const abortListener = () => { | ||
| void this.abortById(id, signal.reason).catch(() => { | ||
| }); | ||
| }; | ||
| signal.addEventListener("abort", abortListener); | ||
| state.removeAbortListener = () => signal.removeEventListener("abort", abortListener); | ||
| } | ||
| void this.transmitRequest(id, state, request); | ||
| }); | ||
| } | ||
| async transmitRequest(id, state, request) { | ||
| try { | ||
| const encodedAtomicBody = await encodeAtomicStandardBody(request.body, request.headers); | ||
| signal?.throwIfAborted(); | ||
| request.signal?.throwIfAborted(); | ||
| if (this.requests.get(id) !== state) { | ||
| return; | ||
| } | ||
| await this.send({ | ||
@@ -287,3 +297,3 @@ id, | ||
| url: request.url, | ||
| headers: Object.entries(encodedAtomicBody.headers).every(([, v]) => v === void 0) ? void 0 : encodedAtomicBody.headers, | ||
| headers: hasAnyDefinedValue(encodedAtomicBody.headers) ? encodedAtomicBody.headers : void 0, | ||
| body: encodedAtomicBody.jsonBody | ||
@@ -293,28 +303,30 @@ }, | ||
| }); | ||
| signal?.throwIfAborted(); | ||
| if (isAsyncIteratorObject(request.body)) { | ||
| const transmitter = new EventStreamTransmitter(request.body, id, this.send); | ||
| state.eventStreamTransmitter = transmitter; | ||
| void transmitter.transmit().catch(async (error) => { | ||
| if (state.eventStreamTransmitter) { | ||
| await this.abortById(id, error); | ||
| } | ||
| }); | ||
| if (this.requests.get(id) !== state) { | ||
| await transmitter.cancel(); | ||
| } else { | ||
| state.eventStreamTransmitter = transmitter; | ||
| await transmitter.transmit().catch((error) => { | ||
| if (state.eventStreamTransmitter) { | ||
| return this.abortById(id, error); | ||
| } | ||
| }); | ||
| } | ||
| } else if (request.body instanceof ReadableStream) { | ||
| const transmitter = new OctetStreamTransmitter(request.body, id, this.send); | ||
| state.octetStreamTransmitter = transmitter; | ||
| void transmitter.transmit().catch(async (error) => { | ||
| if (state.octetStreamTransmitter) { | ||
| await this.abortById(id, error); | ||
| } | ||
| }); | ||
| if (this.requests.get(id) !== state) { | ||
| await transmitter.cancel(); | ||
| } else { | ||
| state.octetStreamTransmitter = transmitter; | ||
| await transmitter.transmit().catch((error) => { | ||
| if (state.octetStreamTransmitter) { | ||
| return this.abortById(id, error); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } catch (reason) { | ||
| await this.closeById(id, reason); | ||
| throw reason; | ||
| } | ||
| return new Promise((resolve, reject) => { | ||
| state.resolve = resolve; | ||
| state.reject = reject; | ||
| }); | ||
| } | ||
@@ -372,2 +384,3 @@ /** | ||
| }); | ||
| state.reject = void 0; | ||
| if (!state.eventStreamMessageQueue && !state.octetStreamMessageQueue) { | ||
@@ -392,3 +405,5 @@ await this.closeById(id); | ||
| this.requests.delete(id); | ||
| reason ??= new AbortError("Request was closed"); | ||
| if (state.reject || state.eventStreamMessageQueue || state.octetStreamMessageQueue) { | ||
| reason ??= new AbortError("Request was closed"); | ||
| } | ||
| state.reject?.(reason); | ||
@@ -407,4 +422,4 @@ state.resolve = void 0; | ||
| state.octetStreamTransmitter = void 0; | ||
| state.cleanupFns?.forEach((fn) => fn()); | ||
| state.cleanupFns = void 0; | ||
| state.removeAbortListener?.(); | ||
| state.removeAbortListener = void 0; | ||
| await Promise.all(promises); | ||
@@ -433,4 +448,4 @@ } | ||
| state.octetStreamTransmitter = void 0; | ||
| state.cleanupFns?.forEach((fn) => fn()); | ||
| state.cleanupFns = void 0; | ||
| state.removeAbortListener?.(); | ||
| state.removeAbortListener = void 0; | ||
| await Promise.all(promises); | ||
@@ -526,6 +541,7 @@ } | ||
| async function encodePeerMessage(message, options = {}) { | ||
| const jsonPart = stringifyJSON({ ...message, binary: void 0 }); | ||
| if (message.binary === void 0) { | ||
| return options.prefix ? options.prefix + jsonPart : jsonPart; | ||
| const jsonPart2 = stringifyJSON(message); | ||
| return options.prefix ? options.prefix + jsonPart2 : jsonPart2; | ||
| } | ||
| const jsonPart = stringifyJSON({ ...message, binary: void 0 }); | ||
| const textBytes = textEncoder.encode( | ||
@@ -637,5 +653,6 @@ options.prefix ? options.prefix + jsonPart : jsonPart | ||
| const decoded = toStandardBody(message, async ({ kind }) => { | ||
| if (kind === "cancelled" && (state.eventStreamMessageQueue || state.octetStreamMessageQueue)) { | ||
| state.eventStreamMessageQueue = void 0; | ||
| state.octetStreamMessageQueue = void 0; | ||
| const streamActive = state.eventStreamMessageQueue !== void 0 || state.octetStreamMessageQueue !== void 0; | ||
| state.eventStreamMessageQueue = void 0; | ||
| state.octetStreamMessageQueue = void 0; | ||
| if (kind === "cancelled" && streamActive) { | ||
| await this.send({ id, kind: "stream/cancel" }); | ||
@@ -661,7 +678,7 @@ } | ||
| const responseMessage = { | ||
| id: message.id, | ||
| id, | ||
| kind: "response", | ||
| json: { | ||
| status: response.status === 200 ? void 0 : response.status, | ||
| headers: Object.entries(encodedAtomicBody.headers).every(([, v]) => v === void 0) ? void 0 : encodedAtomicBody.headers, | ||
| headers: hasAnyDefinedValue(encodedAtomicBody.headers) ? encodedAtomicBody.headers : void 0, | ||
| body: encodedAtomicBody.jsonBody | ||
@@ -677,9 +694,9 @@ }, | ||
| if (response.body instanceof HibernationAsyncIteratorClass) { | ||
| await response.body["~callback"]?.(message.id); | ||
| await response.body["~callback"]?.(id); | ||
| } else { | ||
| const transmitter = new EventStreamTransmitter(response.body, message.id, this.send); | ||
| const transmitter = new EventStreamTransmitter(response.body, id, this.send); | ||
| state.eventStreamTransmitter = transmitter; | ||
| await transmitter.transmit().catch(async (reason) => { | ||
| if (state.eventStreamTransmitter) { | ||
| await this.cancelById(message.id, reason); | ||
| await this.cancelById(id, reason); | ||
| } | ||
@@ -689,7 +706,7 @@ }); | ||
| } else if (response.body instanceof ReadableStream) { | ||
| const transmitter = new OctetStreamTransmitter(response.body, message.id, this.send); | ||
| const transmitter = new OctetStreamTransmitter(response.body, id, this.send); | ||
| state.octetStreamTransmitter = transmitter; | ||
| await transmitter.transmit().catch(async (reason) => { | ||
| if (state.octetStreamTransmitter) { | ||
| await this.cancelById(message.id, reason); | ||
| await this.cancelById(id, reason); | ||
| } | ||
@@ -701,3 +718,3 @@ }); | ||
| } catch (reason) { | ||
| await this.cancelById(message.id, reason); | ||
| await this.cancelById(id, reason); | ||
| throw reason; | ||
@@ -718,3 +735,5 @@ } | ||
| this.requests.delete(id); | ||
| reason ??= new AbortError("Request was closed"); | ||
| if (state.controller || state.eventStreamMessageQueue || state.octetStreamMessageQueue) { | ||
| reason ??= new AbortError("Request was closed"); | ||
| } | ||
| state.controller?.abort(reason); | ||
@@ -721,0 +740,0 @@ state.controller = void 0; |
+3
-3
| { | ||
| "name": "@standardserver/peer", | ||
| "type": "module", | ||
| "version": "0.5.0", | ||
| "version": "0.6.0", | ||
| "license": "MIT", | ||
@@ -25,4 +25,4 @@ "homepage": "https://standardserver.dev", | ||
| "dependencies": { | ||
| "@standardserver/core": "0.5.0", | ||
| "@standardserver/shared": "0.5.0" | ||
| "@standardserver/core": "0.6.0", | ||
| "@standardserver/shared": "0.6.0" | ||
| }, | ||
@@ -29,0 +29,0 @@ "scripts": { |
+10
-7
@@ -10,2 +10,5 @@ # @standardserver/peer | ||
| </a> | ||
| <a href="https://app.codspeed.io/middleapi/standardserver?utm_source=badge"> | ||
| <img src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" alt="CodSpeed" /> | ||
| </a> | ||
| <a href="https://github.com/middleapi/standardserver/blob/main/LICENSE"> | ||
@@ -198,8 +201,6 @@ <img alt="MIT License" src="https://img.shields.io/github/license/middleapi/standardserver?logo=open-source-initiative" /> | ||
| <td align="center"><a href="https://github.com/K-Mistele?ref=orpc" target="_blank" rel="noopener" title="Kyle Mistele"><img src="https://avatars.githubusercontent.com/u/18430555?u=3afebeb81de666e35aaac3ed46f14159d7603ffb&v=4" width="139" alt="Kyle Mistele"/><br />Kyle Mistele</a></td> | ||
| <td align="center"><a href="https://github.com/andrewpeters9?ref=orpc" target="_blank" rel="noopener" title="Andrew Peters"><img src="https://avatars.githubusercontent.com/u/36251325?v=4" width="139" alt="Andrew Peters"/><br />Andrew Peters</a></td> | ||
| <td align="center"><a href="https://github.com/R44VC0RP?ref=orpc" target="_blank" rel="noopener" title="Ryan Vogel"><img src="https://avatars.githubusercontent.com/u/89211796?u=1857347b9787d8d8a7ea5bfc333f96be92d5a683&v=4" width="139" alt="Ryan Vogel"/><br />Ryan Vogel</a></td> | ||
| <td align="center"><a href="https://github.com/christ12938?ref=orpc" target="_blank" rel="noopener" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="139" alt="christ12938"/><br />christ12938</a></td> | ||
| <td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td> | ||
| </tr> | ||
| <tr> | ||
| <td align="center"><a href="https://github.com/christ12938?ref=orpc" target="_blank" rel="noopener" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="139" alt="christ12938"/><br />christ12938</a></td> | ||
| <td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td> | ||
| <td align="center"><a href="https://github.com/itigoore01?ref=orpc" target="_blank" rel="noopener" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&v=4" width="139" alt="shota"/><br />shota</a></td> | ||
@@ -214,3 +215,2 @@ </tr> | ||
| <td align="center"><a href="https://github.com/rhinodavid?ref=orpc" target="_blank" rel="noopener" title="David Walsh"><img src="https://avatars.githubusercontent.com/u/5778036?u=b5521f07d2f88c3db2a0dae62b5f2f8357214af0&v=4" width="119" alt="David Walsh"/><br />David Walsh</a></td> | ||
| <td align="center"><a href="https://github.com/Nic13Gamer?ref=orpc" target="_blank" rel="noopener" title="Nicholas"><img src="https://avatars.githubusercontent.com/u/54724556?u=56a7ab430ce7a80d648ab6eba051d454a818ed0b&v=4" width="119" alt="Nicholas"/><br />Nicholas</a></td> | ||
| <td align="center"><a href="https://github.com/Robbe95?ref=orpc" target="_blank" rel="noopener" title="Robbe Vaes"><img src="https://avatars.githubusercontent.com/u/44748019?u=e0232402c045ad4eac7cbd217f1f47e083103b89&v=4" width="119" alt="Robbe Vaes"/><br />Robbe Vaes</a></td> | ||
@@ -221,5 +221,5 @@ <td align="center"><a href="https://github.com/aidansunbury?ref=orpc" target="_blank" rel="noopener" title="Aidan Sunbury"><img src="https://avatars.githubusercontent.com/u/64103161?v=4" width="119" alt="Aidan Sunbury"/><br />Aidan Sunbury</a></td> | ||
| <td align="center"><a href="https://github.com/pumpkinlink?ref=orpc" target="_blank" rel="noopener" title="Denis"><img src="https://avatars.githubusercontent.com/u/11864620?u=5f47bbe6c65d0f6f5cf011021490238e4b0593d0&v=4" width="119" alt="Denis"/><br />Denis</a></td> | ||
| <td align="center"><a href="https://github.com/christopher-kapic?ref=orpc" target="_blank" rel="noopener" title="Christopher Kapic"><img src="https://avatars.githubusercontent.com/u/59740769?u=e7ad4b72b5bf6c9eb1644c26dbf3332a8f987377&v=4" width="119" alt="Christopher Kapic"/><br />Christopher Kapic</a></td> | ||
| </tr> | ||
| <tr> | ||
| <td align="center"><a href="https://github.com/christopher-kapic?ref=orpc" target="_blank" rel="noopener" title="Christopher Kapic"><img src="https://avatars.githubusercontent.com/u/59740769?u=e7ad4b72b5bf6c9eb1644c26dbf3332a8f987377&v=4" width="119" alt="Christopher Kapic"/><br />Christopher Kapic</a></td> | ||
| <td align="center"><a href="https://github.com/thomasballinger?ref=orpc" target="_blank" rel="noopener" title="Tom Ballinger"><img src="https://avatars.githubusercontent.com/u/458879?u=4b045ac75d721b6ac2b42a74d7d37f61f0414031&v=4" width="119" alt="Tom Ballinger"/><br />Tom Ballinger</a></td> | ||
@@ -231,5 +231,5 @@ <td align="center"><a href="https://github.com/SSam0419?ref=orpc" target="_blank" rel="noopener" title="Sam"><img src="https://avatars.githubusercontent.com/u/102863520?u=3c89611f549d5070be232eb4532f690c8f2e7a65&v=4" width="119" alt="Sam"/><br />Sam</a></td> | ||
| <td align="center"><a href="https://github.com/ldub?ref=orpc" target="_blank" rel="noopener" title="Lev Dubinets"><img src="https://avatars.githubusercontent.com/u/3114081?u=f547f5d5012cab54851f1b1ad72d10e537f78fc2&v=4" width="119" alt="Lev Dubinets"/><br />Lev Dubinets</a></td> | ||
| <td align="center"><a href="https://github.com/mr-kelly?ref=orpc" target="_blank" rel="noopener" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="119" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td> | ||
| </tr> | ||
| <tr> | ||
| <td align="center"><a href="https://github.com/mr-kelly?ref=orpc" target="_blank" rel="noopener" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="119" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td> | ||
| <td align="center"><a href="https://github.com/piscis?ref=orpc" target="_blank" rel="noopener" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="119" alt="Alex"/><br />Alex</a></td> | ||
@@ -266,2 +266,4 @@ <td align="center"><a href="https://github.com/finom?ref=orpc" target="_blank" rel="noopener" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=29e91400dbd4a9c217048a8f59562c4f740498e6&v=4" width="119" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td> | ||
| <a href="https://github.com/wobsoriano?ref=orpc" target="_blank" rel="noopener" title="Robert Soriano"><img src="https://avatars.githubusercontent.com/u/13049130?u=6d72104182e7c9ed25934815313fb69107332111&v=4" width="32" height="32" alt="Robert Soriano" /></a> | ||
| <a href="https://github.com/andrewpeters9?ref=orpc" target="_blank" rel="noopener" title="Andrew Peters"><img src="https://avatars.githubusercontent.com/u/36251325?v=4" width="32" height="32" alt="Andrew Peters" /></a> | ||
| <a href="https://github.com/R44VC0RP?ref=orpc" target="_blank" rel="noopener" title="Ryan Vogel"><img src="https://avatars.githubusercontent.com/u/89211796?u=1857347b9787d8d8a7ea5bfc333f96be92d5a683&v=4" width="32" height="32" alt="Ryan Vogel" /></a> | ||
| <a href="https://github.com/SKostyukovich?ref=orpc" target="_blank" rel="noopener" title="SKostyukovich"><img src="https://avatars.githubusercontent.com/u/10700067?v=4" width="32" height="32" alt="SKostyukovich" /></a> | ||
@@ -279,2 +281,3 @@ <a href="https://github.com/peter-adam-dy?ref=orpc" target="_blank" rel="noopener" title="Peter Adam"><img src="https://avatars.githubusercontent.com/u/132129459?u=4f3dbbb3b443990b56acb7d6a5d11ed2c555f6db&v=4" width="32" height="32" alt="Peter Adam" /></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> | ||
| <a href="https://github.com/Nic13Gamer?ref=orpc" target="_blank" rel="noopener" title="Nicholas"><img src="https://avatars.githubusercontent.com/u/54724556?u=56a7ab430ce7a80d648ab6eba051d454a818ed0b&v=4" width="32" height="32" alt="Nicholas" /></a> | ||
| </p> |
75333
0.93%1053
1.94%277
1.09%+ Added
+ Added
- Removed
- Removed
Updated
Updated