@standardserver/peer 0.0.0 → 0.0.1

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.
Files changed (2) hide show
  1. package/dist/index.mjs +53 -60
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { flattenStandardHeader, getFilenameFromContentDisposition, generateContentDisposition } from '@standardserver/core';
2
- import { AsyncIteratorClass, isTypescriptObject, SequentialIdGenerator, AsyncIdQueue, isAsyncIteratorObject, AbortError, stringifyJSON } from '@standardserver/shared';
1
+ import { AsyncIteratorClass, isTypescriptObject, isAsyncIteratorObject, SequentialIdGenerator, AsyncIdQueue, AbortError, stringifyJSON } from '@standardserver/shared';
2
+ import { generateContentDisposition, flattenStandardHeader, getFilenameFromContentDisposition } from '@standardserver/core';
3
3
  import { withEventIteratorEventMeta, EventIteratorErrorEvent, resolveEventIteratorEvent } from '@standardserver/core/event-stream';
4
4
 
5
5
  function toEventIterator(pull, cleanup) {
@@ -181,6 +181,37 @@ async function toStandardBody(message, eventStreamMessageQueue, octetStreamMessa
181
181
  await cleanup(true);
182
182
  }
183
183
  }
184
+ async function encodeAtomicStandardBody(body, headers) {
185
+ headers = { ...headers };
186
+ let binary;
187
+ let jsonBody = body;
188
+ headers["standard-server"] = void 0;
189
+ if (body instanceof ReadableStream) {
190
+ jsonBody = void 0;
191
+ headers["standard-server"] = "octet-stream";
192
+ } else if (isAsyncIteratorObject(body)) {
193
+ jsonBody = void 0;
194
+ headers["standard-server"] = "event-stream";
195
+ } else if (body instanceof FormData) {
196
+ const res = new Response(body);
197
+ binary = await res.blob();
198
+ jsonBody = void 0;
199
+ headers["standard-server"] = "form-data";
200
+ headers["content-type"] ??= res.headers.get("content-type");
201
+ } else if (body instanceof Blob) {
202
+ binary = body;
203
+ jsonBody = void 0;
204
+ headers["standard-server"] = "file";
205
+ headers["content-disposition"] ??= generateContentDisposition(
206
+ body instanceof File ? body.name : "blob"
207
+ );
208
+ headers["content-type"] ??= body.type;
209
+ } else if (body instanceof URLSearchParams) {
210
+ jsonBody = body.toString();
211
+ headers["standard-server"] = "url-search-params";
212
+ }
213
+ return [jsonBody, headers, binary];
214
+ }
184
215
 
185
216
  class ClientPeer {
186
217
  constructor(send) {
@@ -245,39 +276,20 @@ class ClientPeer {
245
276
  ];
246
277
  this.cleanupFns.set(id, cleanupFns);
247
278
  try {
279
+ const [jsonBody, headers, binary] = await encodeAtomicStandardBody(request.body, request.headers);
280
+ signal?.throwIfAborted();
248
281
  const requestMessage = {
249
282
  id,
250
283
  kind: "request",
251
284
  json: {
252
- ...{ ...request, signal: void 0 },
253
- // clone and remove signal from request
254
- headers: { ...request.headers }
255
- // clone headers
256
- }
285
+ ...request,
286
+ headers,
287
+ body: jsonBody,
288
+ ...{ signal: void 0 }
289
+ // remove signal from request
290
+ },
291
+ binary
257
292
  };
258
- if (request.body instanceof ReadableStream) {
259
- requestMessage.json.body = void 0;
260
- requestMessage.json.headers["standard-server"] = "octet-stream";
261
- } else if (isAsyncIteratorObject(request.body)) {
262
- requestMessage.json.body = void 0;
263
- requestMessage.json.headers["standard-server"] = "event-stream";
264
- } else if (request.body instanceof FormData) {
265
- const res = new Response(request.body);
266
- requestMessage.binary = await res.blob();
267
- requestMessage.json.body = void 0;
268
- requestMessage.json.headers["standard-server"] = "form-data";
269
- requestMessage.json.headers["content-type"] = res.headers.get("content-type") ?? void 0;
270
- } else if (request.body instanceof Blob) {
271
- requestMessage.binary = request.body;
272
- requestMessage.json.body = void 0;
273
- requestMessage.json.headers["standard-server"] = "file";
274
- requestMessage.json.headers["content-disposition"] = generateContentDisposition(request.body instanceof File ? request.body.name : "blob");
275
- requestMessage.json.headers["content-type"] = request.body.type;
276
- } else if (request.body instanceof URLSearchParams) {
277
- requestMessage.json.body = request.body.toString();
278
- requestMessage.json.headers["standard-server"] = "url-search-params";
279
- }
280
- signal?.throwIfAborted();
281
293
  await this.send(requestMessage);
282
294
  signal?.throwIfAborted();
283
295
  if (isAsyncIteratorObject(request.body)) {
@@ -509,41 +521,22 @@ class ServerPeer {
509
521
  if (signal.aborted) {
510
522
  return;
511
523
  }
524
+ const [jsonBody, headers, binary] = await encodeAtomicStandardBody(response.body, response.headers);
525
+ if (signal.aborted) {
526
+ return;
527
+ }
512
528
  const responseMessage = {
513
529
  id: message.id,
514
530
  kind: "response",
515
531
  json: {
516
- ...{ ...response, signal: void 0 },
517
- // clone and remove signal from request
518
- headers: { ...response.headers }
519
- // clone headers
520
- }
532
+ ...response,
533
+ headers,
534
+ body: jsonBody,
535
+ ...{ signal: void 0 }
536
+ // remove signal from request
537
+ },
538
+ binary
521
539
  };
522
- if (response.body instanceof ReadableStream) {
523
- responseMessage.json.body = void 0;
524
- responseMessage.json.headers["standard-server"] = "octet-stream";
525
- } else if (isAsyncIteratorObject(response.body)) {
526
- responseMessage.json.body = void 0;
527
- responseMessage.json.headers["standard-server"] = "event-stream";
528
- } else if (response.body instanceof FormData) {
529
- const res = new Response(response.body);
530
- responseMessage.binary = await res.blob();
531
- responseMessage.json.body = void 0;
532
- responseMessage.json.headers["standard-server"] = "form-data";
533
- responseMessage.json.headers["content-type"] = res.headers.get("content-type") ?? void 0;
534
- } else if (response.body instanceof Blob) {
535
- responseMessage.binary = response.body;
536
- responseMessage.json.body = void 0;
537
- responseMessage.json.headers["standard-server"] = "file";
538
- responseMessage.json.headers["content-disposition"] = generateContentDisposition(response.body instanceof File ? response.body.name : "blob");
539
- responseMessage.json.headers["content-type"] = response.body.type;
540
- } else if (response.body instanceof URLSearchParams) {
541
- responseMessage.json.body = response.body.toString();
542
- responseMessage.json.headers["standard-server"] = "url-search-params";
543
- }
544
- if (signal.aborted) {
545
- return;
546
- }
547
540
  await this.send(responseMessage);
548
541
  if (signal.aborted) {
549
542
  return;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@standardserver/peer",
3
3
  "type": "module",
4
- "version": "0.0.0",
4
+ "version": "0.0.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://standardserver.dev",
7
7
  "repository": {
@@ -20,8 +20,8 @@
20
20
  "dist"
21
21
  ],
22
22
  "dependencies": {
23
- "@standardserver/core": "0.0.0",
24
- "@standardserver/shared": "0.0.0"
23
+ "@standardserver/core": "0.0.1",
24
+ "@standardserver/shared": "0.0.1"
25
25
  },
26
26
  "scripts": {
27
27
  "build": "unbuild",