webhoster 0.3.4 → 0.3.6

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 (127) hide show
  1. package/data/custom-types.js +14 -14
  2. package/package.json +12 -1
  3. package/types/data/CookieObject.d.ts +34 -0
  4. package/types/data/CookieObject.d.ts.map +1 -0
  5. package/types/data/custom-types.d.ts +112 -0
  6. package/types/data/custom-types.d.ts.map +1 -0
  7. package/types/errata/socketio.d.ts +11 -0
  8. package/types/errata/socketio.d.ts.map +1 -0
  9. package/types/examples/starter.d.ts +2 -0
  10. package/types/examples/starter.d.ts.map +1 -0
  11. package/types/helpers/HeadersParser.d.ts +27 -0
  12. package/types/helpers/HeadersParser.d.ts.map +1 -0
  13. package/types/helpers/HttpListener.d.ts +84 -0
  14. package/types/helpers/HttpListener.d.ts.map +1 -0
  15. package/types/helpers/RequestHeaders.d.ts +25 -0
  16. package/types/helpers/RequestHeaders.d.ts.map +1 -0
  17. package/types/helpers/RequestReader.d.ts +52 -0
  18. package/types/helpers/RequestReader.d.ts.map +1 -0
  19. package/types/helpers/ResponseHeaders.d.ts +126 -0
  20. package/types/helpers/ResponseHeaders.d.ts.map +1 -0
  21. package/types/helpers/index.d.ts +6 -0
  22. package/types/helpers/index.d.ts.map +1 -0
  23. package/types/lib/HttpHandler.d.ts +79 -0
  24. package/types/lib/HttpHandler.d.ts.map +1 -0
  25. package/types/lib/HttpRequest.d.ts +175 -0
  26. package/types/lib/HttpRequest.d.ts.map +1 -0
  27. package/types/lib/HttpResponse.d.ts +117 -0
  28. package/types/lib/HttpResponse.d.ts.map +1 -0
  29. package/types/lib/HttpTransaction.d.ts +114 -0
  30. package/types/lib/HttpTransaction.d.ts.map +1 -0
  31. package/types/middleware/AutoHeadersMiddleware.d.ts +27 -0
  32. package/types/middleware/AutoHeadersMiddleware.d.ts.map +1 -0
  33. package/types/middleware/CORSMiddleware.d.ts +65 -0
  34. package/types/middleware/CORSMiddleware.d.ts.map +1 -0
  35. package/types/middleware/CaseInsensitiveHeadersMiddleware.d.ts +27 -0
  36. package/types/middleware/CaseInsensitiveHeadersMiddleware.d.ts.map +1 -0
  37. package/types/middleware/ContentDecoderMiddleware.d.ts +17 -0
  38. package/types/middleware/ContentDecoderMiddleware.d.ts.map +1 -0
  39. package/types/middleware/ContentEncoderMiddleware.d.ts +48 -0
  40. package/types/middleware/ContentEncoderMiddleware.d.ts.map +1 -0
  41. package/types/middleware/ContentLengthMiddleware.d.ts +38 -0
  42. package/types/middleware/ContentLengthMiddleware.d.ts.map +1 -0
  43. package/types/middleware/HashMiddleware.d.ts +28 -0
  44. package/types/middleware/HashMiddleware.d.ts.map +1 -0
  45. package/types/middleware/HeadMethodMiddleware.d.ts +12 -0
  46. package/types/middleware/HeadMethodMiddleware.d.ts.map +1 -0
  47. package/types/middleware/MethodMiddleware.d.ts +40 -0
  48. package/types/middleware/MethodMiddleware.d.ts.map +1 -0
  49. package/types/middleware/PathMiddleware.d.ts +81 -0
  50. package/types/middleware/PathMiddleware.d.ts.map +1 -0
  51. package/types/middleware/ReadFormData.d.ts +7 -0
  52. package/types/middleware/ReadFormData.d.ts.map +1 -0
  53. package/types/middleware/SendJsonMiddleware.d.ts +51 -0
  54. package/types/middleware/SendJsonMiddleware.d.ts.map +1 -0
  55. package/types/middleware/SendStringMiddleware.d.ts +35 -0
  56. package/types/middleware/SendStringMiddleware.d.ts.map +1 -0
  57. package/types/templates/starter.d.ts +15 -0
  58. package/types/templates/starter.d.ts.map +1 -0
  59. package/types/utils/AsyncObject.d.ts +38 -0
  60. package/types/utils/AsyncObject.d.ts.map +1 -0
  61. package/types/utils/CaseInsensitiveObject.d.ts +9 -0
  62. package/types/utils/CaseInsensitiveObject.d.ts.map +1 -0
  63. package/types/utils/function.d.ts +2 -0
  64. package/types/utils/function.d.ts.map +1 -0
  65. package/types/utils/headers.d.ts +7 -0
  66. package/types/utils/headers.d.ts.map +1 -0
  67. package/types/utils/qualityValues.d.ts +12 -0
  68. package/types/utils/qualityValues.d.ts.map +1 -0
  69. package/types/utils/stream.d.ts +6 -0
  70. package/types/utils/stream.d.ts.map +1 -0
  71. package/.eslintrc.json +0 -96
  72. package/.github/FUNDING.yml +0 -4
  73. package/.github/copilot-instructions.md +0 -100
  74. package/.github/workflows/publish.yml +0 -29
  75. package/.github/workflows/test-matrix.yml +0 -37
  76. package/.test/benchmark.js +0 -28
  77. package/.test/constants.js +0 -4
  78. package/.test/cookietester.js +0 -19
  79. package/.test/http2server.js +0 -35
  80. package/.test/httpserver.js +0 -32
  81. package/.test/index.js +0 -498
  82. package/.test/multipromise.js +0 -32
  83. package/.test/tls.js +0 -75
  84. package/.test/urlencoded.js +0 -54
  85. package/.vscode/launch.json +0 -35
  86. package/.vscode/settings.json +0 -5
  87. package/CHANGELOG.md +0 -10
  88. package/CODE_OF_CONDUCT.md +0 -76
  89. package/data/middleware.d.ts +0 -14
  90. package/examples/starter.js +0 -11
  91. package/jsconfig.json +0 -12
  92. package/polyfill/FormData.js +0 -164
  93. package/rollup.config.js +0 -8
  94. package/scripts/check-teapot.mjs +0 -40
  95. package/scripts/test-all-sync.sh +0 -6
  96. package/scripts/test-all.sh +0 -7
  97. package/test/fixtures/stream.js +0 -68
  98. package/test/helpers/HttpListener/construct.js +0 -18
  99. package/test/helpers/HttpListener/customOptions.js +0 -22
  100. package/test/helpers/HttpListener/doubleCreate.js +0 -40
  101. package/test/helpers/HttpListener/events.js +0 -77
  102. package/test/helpers/HttpListener/http.js +0 -31
  103. package/test/helpers/HttpListener/http2.js +0 -41
  104. package/test/helpers/HttpListener/https.js +0 -38
  105. package/test/helpers/HttpListener/startAll.js +0 -31
  106. package/test/helpers/HttpListener/stopNotStarted.js +0 -23
  107. package/test/lib/HttpHandler/class.js +0 -8
  108. package/test/lib/HttpHandler/handleRequest.js +0 -11
  109. package/test/lib/HttpHandler/middleware.js +0 -941
  110. package/test/lib/HttpHandler/parse.js +0 -41
  111. package/test/lib/HttpRequest/class.js +0 -8
  112. package/test/lib/HttpRequest/downstream.js +0 -171
  113. package/test/lib/HttpRequest/properties.js +0 -101
  114. package/test/lib/HttpRequest/read.js +0 -518
  115. package/test/lib/HttpResponse/async-iterable-middleware.js +0 -37
  116. package/test/lib/HttpResponse/async-iterable.js +0 -31
  117. package/test/lib/HttpResponse/class.js +0 -8
  118. package/test/lib/HttpResponse/properties.js +0 -59
  119. package/test/lib/HttpResponse/send.js +0 -275
  120. package/test/lib/HttpTransaction/class.js +0 -8
  121. package/test/lib/HttpTransaction/ping.js +0 -50
  122. package/test/lib/HttpTransaction/push.js +0 -89
  123. package/test/middleware/SendJsonMiddleware.js +0 -222
  124. package/test/sanity.js +0 -10
  125. package/test/templates/error-teapot.js +0 -47
  126. package/test/templates/starter.js +0 -93
  127. package/tsconfig.json +0 -29
@@ -1,5 +0,0 @@
1
- {
2
- "javascript.preferences.importModuleSpecifier": "relative",
3
- "javascript.preferences.importModuleSpecifierEnding": "js",
4
- "javascript.preferences.quoteStyle": "single"
5
- }
package/CHANGELOG.md DELETED
@@ -1,10 +0,0 @@
1
- ## v0.0.5
2
- * Fix SocketIO errata
3
-
4
- ## v0.0.4
5
- * Add CommonJS support
6
- * Add `HttpListener` helper
7
- * Add SocketIO errata
8
-
9
- ## v0.0.3
10
- * Rename RequestHandler to HttpHandler
@@ -1,76 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, sex characteristics, gender identity and expression,
9
- level of experience, education, socio-economic status, nationality, personal
10
- appearance, race, religion, or sexual identity and orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at clshortfuse@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
-
73
- [homepage]: https://www.contributor-covenant.org
74
-
75
- For answers to common questions about this code of conduct, see
76
- https://www.contributor-covenant.org/faq
@@ -1,14 +0,0 @@
1
- import {
2
- IMiddleware,
3
- MiddlewareContent,
4
- MiddlewareFunction,
5
- MiddlewareFunctionResultType,
6
- } from './custom-types.js';
7
-
8
- export type Middleware =
9
- | IMiddleware
10
- | MiddlewareFunction
11
- | Middleware[]
12
- | MiddlewareContent
13
- | Set<Middleware>
14
- | MiddlewareFunctionResultType
@@ -1,11 +0,0 @@
1
- import * as starter from '../templates/starter.js';
2
-
3
- const middleware = [
4
- () => 'Hello world!',
5
- ];
6
-
7
- // Start the singleton HTTP server using the starter convenience API.
8
- await starter.start({ middleware });
9
-
10
- // Export nothing — this example file is intended as a tiny runnable demo.
11
- export {};
package/jsconfig.json DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "checkJs": true,
4
- "noImplicitAny": true,
5
- "target": "es2022",
6
- "module": "esnext",
7
- "moduleResolution": "node"
8
- },
9
- "exclude": [
10
- "**/node_modules/*"
11
- ]
12
- }
@@ -1,164 +0,0 @@
1
- /* @see https://xhr.spec.whatwg.org/#dom-formdata */
2
-
3
- /** @implements {FormData} */
4
- export default class FormData {
5
- /** @type {Map<string, (string|File)[]>} */
6
- #list = new Map();
7
-
8
- /** @param {HTMLFormElement} [form] */
9
- constructor(form) {
10
- if (form) {
11
- throw new Error('InvalidStateError');
12
- }
13
- }
14
-
15
- /**
16
- * @param {string} s
17
- * @return {string}
18
- */
19
- static #scalarValue = String;
20
-
21
- /**
22
- * @param {string} name
23
- * @param {string|Blob} value
24
- * @param {string} [filename]
25
- * @return {{name:string, value:string|File}}
26
- */
27
- static #createEntry(name, value, filename) {
28
- if (typeof value === 'string') {
29
- return {
30
- name: FormData.#scalarValue(name),
31
- value: FormData.#scalarValue(value),
32
- };
33
- }
34
-
35
- if (typeof File === 'undefined') {
36
- if (!('name' in value) && !('lastModified' in value)) {
37
- /** @type {File} */
38
- // eslint-disable-next-line unicorn/prefer-spread
39
- const file = Object.defineProperties(value.slice(), {
40
- name: {
41
- value: filename === undefined ? 'blob' : filename,
42
- },
43
- lastModified: {
44
- value: Date.now(),
45
- },
46
- toString: { value: () => '[object File]' },
47
- });
48
- return {
49
- name: FormData.#scalarValue(name),
50
- value: file,
51
- };
52
- }
53
- return {
54
- name: FormData.#scalarValue(name),
55
- value,
56
- };
57
- }
58
- if (value instanceof File) {
59
- return {
60
- name: FormData.#scalarValue(name),
61
- value,
62
- };
63
- }
64
- return {
65
- name: FormData.#scalarValue(name),
66
- value: new File([value], filename === undefined ? 'blob' : filename, {}),
67
- };
68
- }
69
-
70
- /**
71
- * @param {string} name
72
- * @param {string|Blob} value
73
- * @param {string} [filename]
74
- * @return {void}
75
- */
76
- append(name, value, filename) {
77
- const entry = FormData.#createEntry(name, value, filename);
78
- console.log(entry);
79
- if (this.#list.has(entry.name)) {
80
- this.#list.get(entry.name).push(entry.value);
81
- } else {
82
- this.#list.set(name, [entry.value]);
83
- }
84
- }
85
-
86
- /**
87
- * @param {string} name
88
- * @return {void}
89
- */
90
- delete(name) {
91
- this.#list.delete(name);
92
- }
93
-
94
- /**
95
- * @param {string} name
96
- * @return {null|File|string}
97
- */
98
- get(name) {
99
- const entry = this.#list.get(name);
100
- if (!entry) return null;
101
- return entry[0];
102
- }
103
-
104
- /**
105
- * @param {string} name
106
- * @return {(File|string)[]}
107
- */
108
- getAll(name) {
109
- return this.#list.get(name) ?? [];
110
- }
111
-
112
- /**
113
- * @param {string} name
114
- * @return {boolean}
115
- */
116
- has(name) {
117
- return this.#list.has(name);
118
- }
119
-
120
- /**
121
- * @param {string} name
122
- * @param {string|Blob} value
123
- * @param {string} [filename]
124
- * @return {void}
125
- */
126
- set(name, value, filename) {
127
- const entry = FormData.#createEntry(name, value, filename);
128
- if (this.#list.has(entry.name)) {
129
- const entries = this.#list.get(name);
130
- entries.splice(0, entries.length, entry.value);
131
- } else {
132
- this.#list.set(name, [entry.value]);
133
- }
134
- }
135
-
136
- /**
137
- * @param {(value:(string|File), key:string, parent:this) => void} callback
138
- * @return {void}
139
- */
140
- forEach(callback) {
141
- for (const [key, value] of this) {
142
- callback(value, key, this);
143
- }
144
- }
145
-
146
- get keys() {
147
- return this.#list.keys;
148
- }
149
-
150
- * values() {
151
- for (const value of this.#list.values()) {
152
- yield value[0];
153
- }
154
- }
155
-
156
- * [Symbol.iterator]() {
157
- for (const entry of this.#list.entries()) {
158
- yield /** @type {[string, string|File]} */ ([entry[0], entry[1][0]]);
159
- }
160
- }
161
- }
162
-
163
- FormData.prototype.entries = FormData.prototype[Symbol.iterator];
164
- FormData.prototype.toString = () => '[Object FormData]';
package/rollup.config.js DELETED
@@ -1,8 +0,0 @@
1
- /** @typedef {import('rollup')} */
2
- export default {
3
- input: 'index.js',
4
- output: {
5
- file: 'index.cjs',
6
- format: 'cjs',
7
- },
8
- };
@@ -1,40 +0,0 @@
1
- import http from 'node:http';
2
- import * as starter from '../templates/starter.js';
3
-
4
- async function run() {
5
- const throwingMiddleware = [() => { throw new Error('brew failed'); }];
6
- const teapotHandler = {
7
- onError(transaction) {
8
- transaction.response.status = 418;
9
- return "I'm a teapot";
10
- },
11
- };
12
-
13
- const listener = await starter.start({ middleware: throwingMiddleware, errorHandlers: [teapotHandler], host: '127.0.0.1', port: 0 });
14
- try {
15
- const addr = listener.httpServer.address();
16
- const port = typeof addr === 'object' ? addr.port : addr;
17
- const result = await new Promise((resolve, reject) => {
18
- const req = http.get({ port, path: '/' }, (res) => {
19
- let data = '';
20
- res.setEncoding('utf8');
21
- res.on('data', (c) => { data += c; });
22
- res.on('end', () => resolve({ status: res.statusCode, body: data }));
23
- });
24
- req.on('error', reject);
25
- });
26
-
27
- // Print a concise confirmation
28
- // (CI will show this output in the terminal)
29
- // eslint-disable-next-line no-console
30
- console.log('TEAPOT_CHECK_RESULT', JSON.stringify(result));
31
- } finally {
32
- await listener.stopHttpServer();
33
- }
34
- }
35
-
36
- run().catch((err) => {
37
- // eslint-disable-next-line no-console
38
- console.error('check-teapot failed', err);
39
- process.exit(1);
40
- });
@@ -1,6 +0,0 @@
1
- . ~/.nvm/nvm.sh;
2
- nvm install 16.13; nvm use 16.13; npx c8 --clean false -r none ava;
3
- nvm install 16 ; nvm use 16 ; npx c8 --clean false -r none ava;
4
- nvm install 18 ; nvm use 18 ; npx c8 --clean false -r none ava;
5
- nvm install 20 ; nvm use 20 ; npx c8 --clean false -r none ava;
6
- nvm install 22 ; nvm use 22 ; npx c8 --clean false -r none ava;
@@ -1,7 +0,0 @@
1
- echo . \
2
- & sh -c ". ~/.nvm/nvm.sh; nvm install 16.13; nvm use 16.13; npx c8 --clean false -r none ava;" \
3
- & sh -c ". ~/.nvm/nvm.sh; nvm install 16; nvm use 16; npx c8 --clean false -r none ava;" \
4
- & sh -c ". ~/.nvm/nvm.sh; nvm install 18; nvm use 18; npx c8 --clean false -r none ava;" \
5
- & sh -c ". ~/.nvm/nvm.sh; nvm install 20; nvm use 20; npx c8 --clean false -r none ava;" \
6
- & sh -c ". ~/.nvm/nvm.sh; nvm install 22; nvm use 22; npx c8 --clean false -r none ava;" \
7
-
@@ -1,68 +0,0 @@
1
- import { createHash } from 'node:crypto';
2
- import {
3
- Readable, pipeline as legacyPipeline,
4
- } from 'node:stream';
5
- import { promisify } from 'node:util';
6
-
7
- /** @type {import('node:stream/promises').pipeline} */
8
- let pipeline;
9
- try {
10
- ({ pipeline } = (await import('node:stream/promises')));
11
- } catch {
12
- pipeline = promisify(legacyPipeline);
13
- }
14
-
15
- const BUFFER_SIZE = 1024 * 16;
16
- const CHUNK_SIZE = 256 / 8;
17
- const CHUNK_COUNT = BUFFER_SIZE / CHUNK_SIZE;
18
-
19
- const SEED = Math.floor(Math.random() * (2 ** 4));
20
-
21
- /** @yields {Buffer} */
22
- function* binaryGenerator() {
23
- for (let index = 0; index < CHUNK_COUNT; index++) {
24
- const number_ = (SEED + index);
25
- /* eslint-disable no-bitwise */
26
- yield createHash('sha256').update(Buffer.from([
27
- (number_ >> 24) & 255,
28
- (number_ >> 16) & 255,
29
- (number_ >> 8) & 255,
30
- number_ & 255,
31
- ])).digest();
32
- /* eslint-enable no-bitwise */
33
- }
34
- }
35
-
36
- /** @yields {Buffer} */
37
- function* textGenerator() {
38
- for (const chunk of binaryGenerator()) {
39
- const hexString = chunk.toString('hex');
40
- yield Buffer.from(hexString, 'utf-8');
41
- }
42
- }
43
-
44
- /** @return {Readable} */
45
- export function getTestBinaryStream() {
46
- return Readable.from(binaryGenerator());
47
- }
48
-
49
- /** @return {Readable} */
50
- export function getTestTextStream() {
51
- return Readable.from(textGenerator());
52
- }
53
-
54
- /** @return {Promise<string>} */
55
- export async function getTestHash() {
56
- const hash = createHash('sha256');
57
- await pipeline(binaryGenerator(), hash);
58
- return hash.digest().toString('hex');
59
- }
60
-
61
- /** @return {string} */
62
- export function getTestString() {
63
- let data = '';
64
- for (const chunk of textGenerator()) {
65
- data += chunk;
66
- }
67
- return data;
68
- }
@@ -1,18 +0,0 @@
1
- import test from 'ava';
2
-
3
- import HttpListener from '../../../helpers/HttpListener.js';
4
-
5
- test('HttpListener can be constructed with defaults', (t) => {
6
- const listener = new HttpListener();
7
- t.truthy(listener);
8
- t.is(typeof listener.startHttpServer, 'function');
9
- t.is(typeof listener.stopHttpServer, 'function');
10
- });
11
-
12
- test('HttpListener.defaultInstance returns a singleton', (t) => {
13
- const inst1 = HttpListener.defaultInstance;
14
- const inst2 = HttpListener.defaultInstance;
15
- t.truthy(inst1);
16
- t.is(inst1, inst2);
17
- t.true(inst1 instanceof HttpListener);
18
- });
@@ -1,22 +0,0 @@
1
- import test from 'ava';
2
- import HttpListener from '../../../helpers/HttpListener.js';
3
-
4
- // Test custom port/host and handler injection
5
-
6
- test('custom insecurePort and insecureHost are set', t => {
7
- const listener = new HttpListener({ insecurePort: 1234, insecureHost: '127.0.0.1' });
8
- t.is(listener.insecurePort, 1234);
9
- t.is(listener.insecureHost, '127.0.0.1');
10
- });
11
-
12
- test('custom securePort and secureHost are set', t => {
13
- const listener = new HttpListener({ securePort: 4321, secureHost: 'localhost' });
14
- t.is(listener.securePort, 4321);
15
- t.is(listener.secureHost, 'localhost');
16
- });
17
-
18
- test('custom httpHandler is set', t => {
19
- const fakeHandler = { handleHttp1Request() {}, handleHttp2Stream() {} };
20
- const listener = new HttpListener({ httpHandler: fakeHandler });
21
- t.is(listener.httpHandler, fakeHandler);
22
- });
@@ -1,40 +0,0 @@
1
- import test from 'ava';
2
-
3
- import HttpListener, { SERVER_ALREADY_CREATED } from '../../../helpers/HttpListener.js';
4
-
5
- // Test double server creation throws error
6
- test('createHttpServer throws if called twice with custom options', (t) => {
7
- const listener = new HttpListener();
8
- listener.createHttpServer();
9
- t.notThrows(() => listener.createHttpServer());
10
- t.notThrows(() => listener.createHttpServer({}));
11
- const err = t.throws(() => listener.createHttpServer({ maxHeaderSize: 8192 }));
12
- t.truthy(err);
13
- t.is(err.message, SERVER_ALREADY_CREATED);
14
- });
15
-
16
- // Use environment variables for PEM string values
17
- const key = process.env.TEST_SSL_KEY;
18
- const cert = process.env.TEST_SSL_CERT;
19
- const haveCerts = key && cert;
20
- const testFn = (haveCerts ? test : test.skip);
21
-
22
- testFn('createHttpsServer throws if called twice with custom options', (t) => {
23
- const listener = new HttpListener({ useHttps: true, tlsOptions: { key, cert } });
24
- listener.createHttpsServer();
25
- t.notThrows(() => listener.createHttpsServer());
26
- t.notThrows(() => listener.createHttpsServer({}));
27
- const err = t.throws(() => listener.createHttpsServer({ maxHeaderSize: 8192 }));
28
- t.truthy(err);
29
- t.is(err.message, SERVER_ALREADY_CREATED);
30
- });
31
-
32
- testFn('createHttp2Server throws if called twice with custom options', (t) => {
33
- const listener = new HttpListener({ useHttp2: true, tlsOptions: { key, cert } });
34
- listener.createHttp2Server();
35
- t.notThrows(() => listener.createHttp2Server());
36
- t.notThrows(() => listener.createHttp2Server({}));
37
- const err = t.throws(() => listener.createHttp2Server({ settings: { maxConcurrentStreams: 100 } }));
38
- t.truthy(err);
39
- t.is(err.message, SERVER_ALREADY_CREATED);
40
- });
@@ -1,77 +0,0 @@
1
- import * as http from 'node:http';
2
- import { Socket } from 'node:net';
3
-
4
- import test from 'ava';
5
-
6
- import HttpListener from '../../../helpers/HttpListener.js';
7
-
8
- // Event listener coverage: simulate error/clientError/request events
9
-
10
- test('httpServer emits error and clientError', (t) => {
11
- const listener = new HttpListener();
12
- const server = listener.createHttpServer();
13
- let errorCalled = false;
14
- let clientErrorCalled = false;
15
- server.on('error', () => { errorCalled = true; });
16
- server.on('clientError', () => { clientErrorCalled = true; });
17
- server.emit('error', new Error('test error'));
18
- server.emit('clientError', new Error('test client error'), new EventTarget());
19
- t.true(errorCalled);
20
- t.true(clientErrorCalled);
21
- });
22
-
23
- // Cannot easily test request/stream/session events without real network, but can check listeners are attached
24
-
25
- test('listeners are attached for request and error events', async (t) => {
26
- const listener = new HttpListener({ insecurePort: 0 });
27
- await listener.startHttpServer();
28
- const server = listener.httpServer;
29
- t.true(server.listenerCount('request') > 0);
30
- t.true(server.listenerCount('error') > 0);
31
- t.true(server.listenerCount('clientError') > 0);
32
- await listener.stopHttpServer();
33
- });
34
-
35
- // Test that 'clientError' event handler is called for bad request
36
- test('httpServer clientError event is called for bad request', async (t) => {
37
- const listener = new HttpListener({ insecurePort: 0 });
38
- await listener.startHttpServer();
39
- const server = listener.httpServer;
40
- let called = false;
41
- server.on('clientError', () => { called = true; });
42
- const address = server.address();
43
- const port = typeof address === 'object' && address ? address.port : address;
44
- const socket = new Socket();
45
- socket.connect(Number(port), '127.0.0.1', () => {
46
- socket.write('BAD REQUEST\r\n\r\n');
47
- setTimeout(() => {
48
- socket.destroy();
49
- }, 50);
50
- });
51
- await new Promise((resolve) => setTimeout(resolve, 100));
52
- t.true(called);
53
- await listener.stopHttpServer();
54
- });
55
-
56
- // Test that 'error' event handler is called for server error
57
- test('httpServer error event is called', async (t) => {
58
- const listener = new HttpListener({ insecurePort: 0 });
59
- await listener.startHttpServer();
60
- const server = listener.httpServer;
61
- let called = false;
62
- server.on('error', () => { called = true; });
63
- server.emit('error', new Error('test error')); // Simulate error
64
- t.true(called);
65
- await listener.stopHttpServer();
66
- });
67
-
68
- // Test that listeners are removed after stop
69
- test('listeners are removed after stop', async (t) => {
70
- const listener = new HttpListener({ insecurePort: 0 });
71
- await listener.startHttpServer();
72
- const server = listener.httpServer;
73
- await listener.stopHttpServer();
74
- t.is(server.listenerCount('request'), 0);
75
- t.is(server.listenerCount('error'), 0);
76
- t.is(server.listenerCount('clientError'), 0);
77
- });
@@ -1,31 +0,0 @@
1
- import { Socket } from 'node:net';
2
-
3
- import test from 'ava';
4
-
5
- import HttpListener from '../../../helpers/HttpListener.js';
6
-
7
- test('HttpListener.startHttpServer() and stopHttpServer() work', async (t) => {
8
- const listener = new HttpListener({ insecurePort: 0 }); // Use ephemeral port
9
- const server = await listener.startHttpServer();
10
- t.truthy(server);
11
- await listener.stopHttpServer();
12
- t.pass();
13
- });
14
-
15
- test('HttpListener HTTP server closes idle socket after keepAliveTimeout', async (t) => {
16
- const listener = new HttpListener({ insecurePort: 0 });
17
- await listener.startHttpServer();
18
- const server = listener.httpServer;
19
- server.setTimeout(1000); // Set keepAliveTimeout to 1 seconds
20
- const address = server.address();
21
- const port = typeof address === 'object' && address ? address.port : address;
22
- const socket = new Socket();
23
- let destroyed = false;
24
- socket.on('close', () => { destroyed = true; });
25
- socket.connect(Number(port), '127.0.0.1', () => {
26
- // Do not send any data, just keep connection open
27
- });
28
- await new Promise((resolve) => setTimeout(resolve, 2000));
29
- t.true(destroyed, 'Socket should be destroyed after keepAliveTimeout');
30
- await listener.stopHttpServer();
31
- });
@@ -1,41 +0,0 @@
1
- import { connect } from 'node:http2';
2
-
3
- import test from 'ava';
4
-
5
- import HttpListener from '../../../helpers/HttpListener.js';
6
-
7
- const key = process.env.TEST_SSL_KEY;
8
- const cert = process.env.TEST_SSL_CERT;
9
- const haveCerts = key && cert;
10
-
11
- const testFn = haveCerts ? test : test.skip;
12
-
13
- testFn('HttpListener.startHttp2Server() and stopHttp2Server() work', async (t) => {
14
- const tlsOptions = { key, cert };
15
- const listener = new HttpListener({ securePort: 0, useHttp2: true, tlsOptions });
16
- const server = await listener.startHttp2Server();
17
- t.truthy(server);
18
- await listener.stopHttp2Server();
19
- t.pass();
20
- });
21
-
22
- testFn('HttpListener HTTP2 server closes idle socket after setTimeout', async (t) => {
23
- const tlsOptions = { key, cert };
24
- const listener = new HttpListener({ securePort: 0, useHttp2: true, tlsOptions });
25
- await listener.startHttp2Server();
26
- const server = listener.http2Server;
27
- server.setTimeout(1000); // Set keepAliveTimeout to 1 seconds
28
- const address = server.address();
29
- const port = typeof address === 'object' && address ? address.port : address;
30
- const client = connect(`https://127.0.0.1:${port}`, {
31
- ca: cert,
32
- rejectUnauthorized: false,
33
- });
34
- let closed = false;
35
- client.on('close', () => { closed = true; });
36
- // Do not send any requests, just keep connection open
37
- await new Promise((resolve) => setTimeout(resolve, 2000));
38
- t.true(closed, 'HTTP2 client should be closed after setTimeout');
39
- client.destroy();
40
- await listener.stopHttp2Server();
41
- });