@whatwg-node/server 0.8.1 → 0.8.2-alpha-20230605124052-0f7729b

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.
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createServerAdapter = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ /* eslint-disable @typescript-eslint/ban-types */
6
+ const repeater_1 = require("@repeaterjs/repeater");
5
7
  const DefaultFetchAPI = tslib_1.__importStar(require("@whatwg-node/fetch"));
6
8
  const utils_js_1 = require("./utils.js");
7
9
  const uwebsockets_js_1 = require("./uwebsockets.js");
@@ -128,26 +130,24 @@ function createServerAdapter(serverAdapterBaseObject, options) {
128
130
  let resAborted = false;
129
131
  res.onAborted(function () {
130
132
  resAborted = true;
131
- body?.readable.push(null);
132
133
  });
133
134
  if (method !== 'get' && method !== 'head') {
134
- body = new fetchAPI.ReadableStream({});
135
- res.onData(function (chunk, isLast) {
136
- body?.readable.push(Buffer.from(chunk, 0, chunk.byteLength));
137
- if (isLast) {
138
- body?.readable.push(null);
139
- }
135
+ body = new repeater_1.Repeater(function (push, stop) {
136
+ res.onAborted(stop);
137
+ res.onData(function (chunk, isLast) {
138
+ push(Buffer.from(chunk));
139
+ if (isLast) {
140
+ stop();
141
+ }
142
+ });
140
143
  });
141
144
  }
142
- const headers = {};
143
- req.forEach((key, value) => {
144
- headers[key] = value;
145
- });
145
+ const headers = (0, uwebsockets_js_1.getHeadersFromUWSRequest)(req);
146
146
  const url = `http://localhost${req.getUrl()}`;
147
147
  const request = new fetchAPI.Request(url, {
148
148
  method,
149
149
  headers,
150
- body,
150
+ body: body,
151
151
  });
152
152
  const response = await handleRequest(request, serverContext);
153
153
  if (resAborted) {
@@ -1,7 +1,61 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isUWSResponse = void 0;
3
+ exports.getHeadersFromUWSRequest = exports.isUWSResponse = void 0;
4
4
  function isUWSResponse(res) {
5
5
  return typeof res === 'object' && typeof res.onData === 'function';
6
6
  }
7
7
  exports.isUWSResponse = isUWSResponse;
8
+ function throwReadOnlyHeadersError() {
9
+ throw new Error('You cannot modify headers for a read-only request');
10
+ }
11
+ function getHeadersFromUWSRequest(req) {
12
+ return {
13
+ append() {
14
+ throwReadOnlyHeadersError();
15
+ },
16
+ delete() {
17
+ throwReadOnlyHeadersError();
18
+ },
19
+ get(key) {
20
+ return req.getHeader(key) || null;
21
+ },
22
+ has(key) {
23
+ return req.getHeader(key) != null;
24
+ },
25
+ set() {
26
+ throwReadOnlyHeadersError();
27
+ },
28
+ forEach(callback) {
29
+ req.forEach((key, value) => callback(value, key, this));
30
+ },
31
+ entries() {
32
+ const entries = [];
33
+ this.forEach((value, key) => {
34
+ entries.push([key, value]);
35
+ });
36
+ return entries[Symbol.iterator]();
37
+ },
38
+ keys() {
39
+ const keys = [];
40
+ this.forEach((_, key) => {
41
+ keys.push(key);
42
+ });
43
+ return keys[Symbol.iterator]();
44
+ },
45
+ values() {
46
+ const values = [];
47
+ this.forEach(value => {
48
+ values.push(value);
49
+ });
50
+ return values[Symbol.iterator]();
51
+ },
52
+ [Symbol.iterator]() {
53
+ const entries = [];
54
+ this.forEach((value, key) => {
55
+ entries.push([key, value]);
56
+ });
57
+ return entries[Symbol.iterator]();
58
+ },
59
+ };
60
+ }
61
+ exports.getHeadersFromUWSRequest = getHeadersFromUWSRequest;
@@ -1,6 +1,8 @@
1
+ /* eslint-disable @typescript-eslint/ban-types */
2
+ import { Repeater } from '@repeaterjs/repeater';
1
3
  import * as DefaultFetchAPI from '@whatwg-node/fetch';
2
4
  import { isFetchEvent, isNodeRequest, isRequestInit, isServerResponse, normalizeNodeRequest, sendNodeResponse, } from './utils.js';
3
- import { isUWSResponse } from './uwebsockets.js';
5
+ import { getHeadersFromUWSRequest, isUWSResponse } from './uwebsockets.js';
4
6
  async function handleWaitUntils(waitUntilPromises) {
5
7
  const waitUntils = await Promise.allSettled(waitUntilPromises);
6
8
  waitUntils.forEach(waitUntil => {
@@ -124,26 +126,24 @@ function createServerAdapter(serverAdapterBaseObject, options) {
124
126
  let resAborted = false;
125
127
  res.onAborted(function () {
126
128
  resAborted = true;
127
- body?.readable.push(null);
128
129
  });
129
130
  if (method !== 'get' && method !== 'head') {
130
- body = new fetchAPI.ReadableStream({});
131
- res.onData(function (chunk, isLast) {
132
- body?.readable.push(Buffer.from(chunk, 0, chunk.byteLength));
133
- if (isLast) {
134
- body?.readable.push(null);
135
- }
131
+ body = new Repeater(function (push, stop) {
132
+ res.onAborted(stop);
133
+ res.onData(function (chunk, isLast) {
134
+ push(Buffer.from(chunk));
135
+ if (isLast) {
136
+ stop();
137
+ }
138
+ });
136
139
  });
137
140
  }
138
- const headers = {};
139
- req.forEach((key, value) => {
140
- headers[key] = value;
141
- });
141
+ const headers = getHeadersFromUWSRequest(req);
142
142
  const url = `http://localhost${req.getUrl()}`;
143
143
  const request = new fetchAPI.Request(url, {
144
144
  method,
145
145
  headers,
146
- body,
146
+ body: body,
147
147
  });
148
148
  const response = await handleRequest(request, serverContext);
149
149
  if (resAborted) {
@@ -1,3 +1,56 @@
1
1
  export function isUWSResponse(res) {
2
2
  return typeof res === 'object' && typeof res.onData === 'function';
3
3
  }
4
+ function throwReadOnlyHeadersError() {
5
+ throw new Error('You cannot modify headers for a read-only request');
6
+ }
7
+ export function getHeadersFromUWSRequest(req) {
8
+ return {
9
+ append() {
10
+ throwReadOnlyHeadersError();
11
+ },
12
+ delete() {
13
+ throwReadOnlyHeadersError();
14
+ },
15
+ get(key) {
16
+ return req.getHeader(key) || null;
17
+ },
18
+ has(key) {
19
+ return req.getHeader(key) != null;
20
+ },
21
+ set() {
22
+ throwReadOnlyHeadersError();
23
+ },
24
+ forEach(callback) {
25
+ req.forEach((key, value) => callback(value, key, this));
26
+ },
27
+ entries() {
28
+ const entries = [];
29
+ this.forEach((value, key) => {
30
+ entries.push([key, value]);
31
+ });
32
+ return entries[Symbol.iterator]();
33
+ },
34
+ keys() {
35
+ const keys = [];
36
+ this.forEach((_, key) => {
37
+ keys.push(key);
38
+ });
39
+ return keys[Symbol.iterator]();
40
+ },
41
+ values() {
42
+ const values = [];
43
+ this.forEach(value => {
44
+ values.push(value);
45
+ });
46
+ return values[Symbol.iterator]();
47
+ },
48
+ [Symbol.iterator]() {
49
+ const entries = [];
50
+ this.forEach((value, key) => {
51
+ entries.push([key, value]);
52
+ });
53
+ return entries[Symbol.iterator]();
54
+ },
55
+ };
56
+ }
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@whatwg-node/server",
3
- "version": "0.8.1",
3
+ "version": "0.8.2-alpha-20230605124052-0f7729b",
4
4
  "description": "Fetch API compliant HTTP Server adapter",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
7
+ "@repeaterjs/repeater": "^3.0.4",
7
8
  "@whatwg-node/fetch": "^0.9.0",
8
9
  "tslib": "^2.3.1"
9
10
  },
@@ -2,6 +2,7 @@ export interface UWSRequest {
2
2
  getMethod(): string;
3
3
  forEach(callback: (key: string, value: string) => void): void;
4
4
  getUrl(): string;
5
+ getHeader(key: string): string | undefined;
5
6
  }
6
7
  export interface UWSResponse {
7
8
  onData(callback: (chunk: ArrayBuffer, isLast: boolean) => void): void;
@@ -14,3 +15,4 @@ export interface UWSResponse {
14
15
  }
15
16
  export type UWSHandler = (res: UWSResponse, req: UWSRequest) => void | Promise<void>;
16
17
  export declare function isUWSResponse(res: any): res is UWSResponse;
18
+ export declare function getHeadersFromUWSRequest(req: UWSRequest): Headers;
@@ -2,6 +2,7 @@ export interface UWSRequest {
2
2
  getMethod(): string;
3
3
  forEach(callback: (key: string, value: string) => void): void;
4
4
  getUrl(): string;
5
+ getHeader(key: string): string | undefined;
5
6
  }
6
7
  export interface UWSResponse {
7
8
  onData(callback: (chunk: ArrayBuffer, isLast: boolean) => void): void;
@@ -14,3 +15,4 @@ export interface UWSResponse {
14
15
  }
15
16
  export type UWSHandler = (res: UWSResponse, req: UWSRequest) => void | Promise<void>;
16
17
  export declare function isUWSResponse(res: any): res is UWSResponse;
18
+ export declare function getHeadersFromUWSRequest(req: UWSRequest): Headers;