@gjsify/http 0.0.2

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.
package/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # @gjsify/http
2
+
3
+ ## Inspirations
4
+ - https://github.com/node-fetch/node-fetch/blob/main/src/headers.js
@@ -0,0 +1,37 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var src_exports = {};
19
+ __export(src_exports, {
20
+ validateHeaderName: () => validateHeaderName,
21
+ validateHeaderValue: () => validateHeaderValue
22
+ });
23
+ module.exports = __toCommonJS(src_exports);
24
+ function validateHeaderName(name) {
25
+ if (!/^[\^`\-\w!#$%&'*+.|~]+$/.test(name)) {
26
+ const error = new TypeError(`Header name must be a valid HTTP token ["${name}"]`);
27
+ Object.defineProperty(error, "code", { value: "ERR_INVALID_HTTP_TOKEN" });
28
+ throw error;
29
+ }
30
+ }
31
+ function validateHeaderValue(name, value) {
32
+ if (/[^\t\u0020-\u007E\u0080-\u00FF]/.test(value)) {
33
+ const error = new TypeError(`Invalid character in header content ["${name}"]`);
34
+ Object.defineProperty(error, "code", { value: "ERR_INVALID_CHAR" });
35
+ throw error;
36
+ }
37
+ }
@@ -0,0 +1,18 @@
1
+ function validateHeaderName(name) {
2
+ if (!/^[\^`\-\w!#$%&'*+.|~]+$/.test(name)) {
3
+ const error = new TypeError(`Header name must be a valid HTTP token ["${name}"]`);
4
+ Object.defineProperty(error, "code", { value: "ERR_INVALID_HTTP_TOKEN" });
5
+ throw error;
6
+ }
7
+ }
8
+ function validateHeaderValue(name, value) {
9
+ if (/[^\t\u0020-\u007E\u0080-\u00FF]/.test(value)) {
10
+ const error = new TypeError(`Invalid character in header content ["${name}"]`);
11
+ Object.defineProperty(error, "code", { value: "ERR_INVALID_CHAR" });
12
+ throw error;
13
+ }
14
+ }
15
+ export {
16
+ validateHeaderName,
17
+ validateHeaderValue
18
+ };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@gjsify/http",
3
+ "version": "0.0.2",
4
+ "description": "Node.js http module for Gjs",
5
+ "main": "lib/cjs/index.js",
6
+ "module": "lib/esm/index.js",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./lib/types/index.d.ts",
12
+ "default": "./lib/esm/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./lib/types/index.d.ts",
16
+ "default": "./lib/cjs/index.js"
17
+ }
18
+ }
19
+ },
20
+ "scripts": {
21
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
22
+ "print:name": "echo '@gjsify/http'",
23
+ "build": "yarn print:name && yarn build:gjsify",
24
+ "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
25
+ "build:types": "tsc --project tsconfig.types.json || exit 0",
26
+ "build:test": "yarn build:test:gjs && yarn build:test:node",
27
+ "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
28
+ "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
29
+ "test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
30
+ "test:gjs": "gjs -m test.gjs.mjs",
31
+ "test:node": "node test.node.mjs"
32
+ },
33
+ "keywords": [
34
+ "gjs",
35
+ "node",
36
+ "http"
37
+ ],
38
+ "devDependencies": {
39
+ "@gjsify/cli": "^0.0.2",
40
+ "@gjsify/unit": "^0.0.2",
41
+ "@types/node": "^20.3.1"
42
+ }
43
+ }
@@ -0,0 +1,74 @@
1
+ import { describe, it, expect } from '@gjsify/unit';
2
+
3
+ import * as http from 'http';
4
+ import type { validateHeaderName as gjsifyValidateHeaderName, validateHeaderValue as gjsifyValidateHeaderValue } from './index.js';
5
+
6
+ // TODO: Add types to @types/node
7
+ const validateHeaderName: typeof gjsifyValidateHeaderName = (http as any).validateHeaderName;
8
+ const validateHeaderValue: typeof gjsifyValidateHeaderValue = (http as any).validateHeaderValue;
9
+
10
+ export default async () => {
11
+
12
+ const validNames = ['Content-Type', 'set-cookie', 'alfa-beta'];
13
+ const invalidNames = ['@@wdjhgw'];
14
+ const expectedErrorMessage = (name: any) => `Header name must be a valid HTTP token ["${name}"]`;
15
+
16
+ await describe('http.validateHeaderName', async () => {
17
+ await it('should be a function', async () => {
18
+ expect(typeof validateHeaderName).toBe("function");
19
+ });
20
+
21
+ await it('an empty string should be not valid and throw an error', async () => {
22
+ expect(() => {
23
+ validateHeaderName('');
24
+ }).toThrow();
25
+ });
26
+
27
+ await it('an empty string should be throw an error which is an instance of TypeError', async () => {
28
+ try {
29
+ validateHeaderName('');
30
+ } catch (error) {
31
+ expect(error instanceof TypeError).toBeTruthy();
32
+ }
33
+ });
34
+
35
+ await it(`an empty string should be throw an error with the message of "${expectedErrorMessage('')}"`, async () => {
36
+ try {
37
+ validateHeaderName('');
38
+ } catch (error) {
39
+ expect(error.message).toBe(expectedErrorMessage(''));
40
+ }
41
+ });
42
+
43
+ await it(`a number should be throw an error with the message of "${expectedErrorMessage(100)}"`, async () => {
44
+ try {
45
+ validateHeaderName(100 as any);
46
+ } catch (error) {
47
+ expect(error.message).toBe(expectedErrorMessage(100));
48
+ }
49
+ });
50
+
51
+ for (const validName of validNames) {
52
+ await it(`"${validName}" should be valid and not throw any error`, async () => {
53
+ expect(() => {
54
+ validateHeaderName(validName);
55
+ }).not?.toThrow();
56
+ });
57
+ }
58
+
59
+ for (const invalidName of invalidNames) {
60
+ await it(`"${invalidName}" should be not valid and throw an error`, async () => {
61
+ expect(() => {
62
+ validateHeaderName(invalidName);
63
+ }).toThrow();
64
+ });
65
+ }
66
+ });
67
+
68
+ await describe('http.validateHeaderValue', async () => {
69
+ await it('should be a function', async () => {
70
+ expect(typeof validateHeaderValue).toBe("function");
71
+ });
72
+ });
73
+
74
+ }
package/src/index.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Performs the low-level validations on the provided `name` that are done when `res.setHeader(name, value)` is called.
3
+ * Passing illegal value as `name` will result in a `TypeError` being thrown, identified by `code: 'ERR_INVALID_HTTP_TOKEN'`.
4
+ * It is not necessary to use this method before passing headers to an HTTP request or response. The HTTP module will automatically validate such headers.
5
+ * @param name
6
+ * @since v14.3.0
7
+ */
8
+ export function validateHeaderName(name: string) {
9
+ if (!/^[\^`\-\w!#$%&'*+.|~]+$/.test(name)) {
10
+ const error = new TypeError(`Header name must be a valid HTTP token ["${name}"]`);
11
+ Object.defineProperty(error, 'code', { value: 'ERR_INVALID_HTTP_TOKEN' });
12
+ throw error;
13
+ }
14
+ }
15
+
16
+ /**
17
+ * Performs the low-level validations on the provided `value` that are done when `res.setHeader(name, value)` is called.
18
+ * Passing illegal value as `value` will result in a `TypeError` being thrown.
19
+ * * Undefined value error is identified by `code: 'ERR_HTTP_INVALID_HEADER_VALUE'`.
20
+ * * Invalid value character error is identified by `code: 'ERR_INVALID_CHAR'`.
21
+ * It is not necessary to use this method before passing headers to an HTTP request or response. The HTTP module will automatically validate such headers.
22
+ * @param name
23
+ * @param value
24
+ */
25
+ export function validateHeaderValue(name: string, value: any) {
26
+ if (/[^\t\u0020-\u007E\u0080-\u00FF]/.test(value)) {
27
+ const error = new TypeError(`Invalid character in header content ["${name}"]`);
28
+ Object.defineProperty(error, 'code', {value: 'ERR_INVALID_CHAR'});
29
+ throw error;
30
+ }
31
+ }
package/src/test.mts ADDED
@@ -0,0 +1,6 @@
1
+
2
+ import { run } from '@gjsify/unit';
3
+
4
+ import testSuite from './index.spec.js';
5
+
6
+ run({testSuite});