Skip to content

Commit c98e540

Browse files
authored
Merge pull request #7 from shgysk8zer0/patch/updates
Update `@shgysk8zer0/consts`
2 parents 7534f5c + 02402eb commit c98e540

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [v1.0.3] - 2023-09-29
10+
11+
### Added
12+
- Add `HTTPError.toResponse()` method
13+
- Add functions to get status codes and file/mime types
14+
15+
### Changed
16+
- Update `@shgysk8zer0/consts`
17+
918
## [v1.0.2] - 2023-09-25
1019

1120
### Added

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ A JavaScript library that provides various utilities for working with HTTP
3131

3232
- Exported constants for common HTTP status codes, such as `ok` for 200.
3333
- An extended `HTTPError` class that inherits from Error.
34-
- Form data parsing that mirrors browser behavior, allowing `formData.get('file')` to return a File object.
3534
- Useful polyfills, including an extended `File` object (derived from `Blob`) and `URL.canParse()` for URL validation.
36-
- A set of constants for commonly used Content-Types.
35+
- A set of constants for commonly used Content-Types (from `@shgysk8zer0/consts`).
3736
- A versatile `openLink()` function compatible with various JavaScript environments.
3837
- A `Cookie` class for working with HTTP cookies, enabling easy cookie creation and management.
3938

39+
> [!WARNING]
40+
> Parsing of form data uses regex which has been reported as [vulnerable to ReDoS](https://github.com/shgysk8zer0/node-http/issues/2)
41+
> attacks. `parseMultipartFormData()` is deprecated and will be removed in an
42+
> upcoming release. Instead, in node > 18 you can use `new Request(body, { headers }).formData()`.
43+
4044
## Installation
4145

4246
### NPM Installation

error.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export class HTTPError extends Error {
8282
};
8383
}
8484

85+
toResponse({ headers } = {}) {
86+
return Response.json(this, { status: this.status, headers });
87+
}
88+
8589
static async fromResponse(resp, { cause } = {}) {
8690
if (! (resp instanceof Response)) {
8791
throw new TypeError('resp must be a Response object.');

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shgysk8zer0/http",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A JavaScript library that provides various utilities for working with HTTP",
55
"keywords": [
66
"http",
@@ -71,6 +71,6 @@
7171
"@shgysk8zer0/npm-utils": "^1.1.0"
7272
},
7373
"dependencies": {
74-
"@shgysk8zer0/consts": "^1.0.3"
74+
"@shgysk8zer0/consts": "^1.0.6"
7575
}
7676
}

rollup.config.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { nodeResolve } from '@rollup/plugin-node-resolve';
33
import { listDirByExt } from '@shgysk8zer0/npm-utils/fs';
44

55
export default {
6-
input: await listDirByExt('./', '.js').then(files => files.filter(file => ! file.endsWith('.config.js'))),
6+
input: await listDirByExt('./', '.js')
7+
.then(files => files.filter(file => ! file.endsWith('.config.js'))),
78
external: ['node:fs', 'node:fs/promises', 'node:crypto', 'node:path', 'node:child_process', 'js-yaml'],
89
onwarn: warningHandler,
910
output: {
@@ -14,14 +15,3 @@ export default {
1415
},
1516
plugins: [nodeResolve()],
1617
};
17-
/*import { getConfig } from '@shgysk8zer0/js-utils/rollup';
18-
import { nodeResolve } from '@rollup/plugin-node-resolve';
19-
20-
export default getConfig('./http.js', {
21-
format: 'cjs',
22-
minify: false,
23-
sourcemap: false,
24-
external: ['node:child_process'],
25-
plugins: [nodeResolve()],
26-
globals: {},
27-
});*/

utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import * as EXTS from '@shgysk8zer0/consts/exts.js';
2+
import * as MIMES from '@shgysk8zer0/consts/mimes.js';
3+
import * as STATUS from '@shgysk8zer0/consts/status.js';
4+
import * as STATUS_TEXT from '@shgysk8zer0/consts/status-text.js';
5+
import { extname } from 'node:path';
6+
17
export async function openLink(url, base) {
28
if (typeof url === 'string') {
39
return openLink(new URL(url, base));
@@ -31,3 +37,22 @@ export async function openLink(url, base) {
3137
});
3238
}
3339
}
40+
41+
export function getStatusText(code) {
42+
const [key] = Object.entries(STATUS).find(([,stat]) => stat === code) ?? [];
43+
44+
return STATUS_TEXT[key] ?? '';
45+
}
46+
47+
export function getMimeType(path) {
48+
const ext = extname(path).toLowerCase();
49+
console.log({ path, ext });
50+
51+
if (ext.length !== 0) {
52+
const [key] = Object.entries(EXTS).find(([,exts]) => exts.includes(ext)) ?? [];
53+
54+
return MIMES[key] ?? '';
55+
} else {
56+
return '';
57+
}
58+
}

0 commit comments

Comments
 (0)