Просмотр исходного кода

refactor: renames cookie utils

e22m4u 1 месяц назад
Родитель
Сommit
105aa769d3

+ 39 - 39
dist/cjs/index.cjs

@@ -51,7 +51,7 @@ __export(index_exports, {
   TrieRouter: () => TrieRouter,
   UNPARSABLE_MEDIA_TYPES: () => UNPARSABLE_MEDIA_TYPES,
   cloneDeep: () => cloneDeep,
-  createCookiesString: () => createCookiesString,
+  createCookieString: () => createCookieString,
   createDebugger: () => createDebugger,
   createError: () => createError,
   createRequestMock: () => createRequestMock,
@@ -64,7 +64,7 @@ __export(index_exports, {
   isResponseSent: () => isResponseSent,
   isWritableStream: () => isWritableStream,
   parseContentType: () => parseContentType,
-  parseCookies: () => parseCookies,
+  parseCookieString: () => parseCookieString,
   parseJsonBody: () => parseJsonBody,
   toCamelCase: () => toCamelCase
 });
@@ -148,28 +148,11 @@ function createError(errorCtor, message, ...args) {
 }
 __name(createError, "createError");
 
-// src/utils/parse-cookies.js
-var import_js_format2 = require("@e22m4u/js-format");
-function parseCookies(input) {
-  if (typeof input !== "string")
-    throw new import_js_format2.Errorf(
-      'The first parameter of "parseCookies" should be a String, but %v was given.',
-      input
-    );
-  return input.split(";").filter((v) => v !== "").map((v) => v.split("=")).reduce((cookies, tuple) => {
-    const key = decodeURIComponent(tuple[0]).trim();
-    const value = tuple[1] !== void 0 ? decodeURIComponent(tuple[1]).trim() : "";
-    cookies[key] = value;
-    return cookies;
-  }, {});
-}
-__name(parseCookies, "parseCookies");
-
 // src/utils/to-camel-case.js
-var import_js_format3 = require("@e22m4u/js-format");
+var import_js_format2 = require("@e22m4u/js-format");
 function toCamelCase(input) {
   if (typeof input !== "string")
-    throw new import_js_format3.Errorf(
+    throw new import_js_format2.Errorf(
       'The first argument of "toCamelCase" should be a String, but %v was given.',
       input
     );
@@ -179,26 +162,26 @@ __name(toCamelCase, "toCamelCase");
 
 // src/utils/create-debugger.js
 var import_debug = __toESM(require("debug"), 1);
-var import_js_format4 = require("@e22m4u/js-format");
+var import_js_format3 = require("@e22m4u/js-format");
 function createDebugger(name) {
   if (typeof name !== "string")
-    throw new import_js_format4.Errorf(
+    throw new import_js_format3.Errorf(
       'The first argument of "createDebugger" should be a String, but %v was given.',
       name
     );
   const debug = (0, import_debug.default)(`jsTrieRouter:${name}`);
   return function(message, ...args) {
-    const interpolatedMessage = (0, import_js_format4.format)(message, ...args);
+    const interpolatedMessage = (0, import_js_format3.format)(message, ...args);
     return debug(interpolatedMessage);
   };
 }
 __name(createDebugger, "createDebugger");
 
 // src/utils/is-response-sent.js
-var import_js_format5 = require("@e22m4u/js-format");
+var import_js_format4 = require("@e22m4u/js-format");
 function isResponseSent(response) {
   if (!response || typeof response !== "object" || Array.isArray(response) || typeof response.headersSent !== "boolean") {
-    throw new import_js_format5.Errorf(
+    throw new import_js_format4.Errorf(
       'The first argument of "isResponseSent" should be an instance of ServerResponse, but %v was given.',
       response
     );
@@ -225,10 +208,10 @@ function isReadableStream(value) {
 __name(isReadableStream, "isReadableStream");
 
 // src/utils/parse-content-type.js
-var import_js_format6 = require("@e22m4u/js-format");
+var import_js_format5 = require("@e22m4u/js-format");
 function parseContentType(input) {
   if (typeof input !== "string")
-    throw new import_js_format6.Errorf(
+    throw new import_js_format5.Errorf(
       'The parameter "input" of "parseContentType" should be a String, but %v was given.',
       input
     );
@@ -254,7 +237,7 @@ __name(isWritableStream, "isWritableStream");
 // src/utils/fetch-request-body.js
 var import_http_errors = __toESM(require("http-errors"), 1);
 var import_http = require("http");
-var import_js_format7 = require("@e22m4u/js-format");
+var import_js_format6 = require("@e22m4u/js-format");
 var CHARACTER_ENCODING_LIST = [
   "ascii",
   "utf8",
@@ -267,12 +250,12 @@ var CHARACTER_ENCODING_LIST = [
 ];
 function fetchRequestBody(request, bodyBytesLimit = 0) {
   if (!(request instanceof import_http.IncomingMessage))
-    throw new import_js_format7.Errorf(
+    throw new import_js_format6.Errorf(
       'The first parameter of "fetchRequestBody" should be an IncomingMessage instance, but %v was given.',
       request
     );
   if (typeof bodyBytesLimit !== "number")
-    throw new import_js_format7.Errorf(
+    throw new import_js_format6.Errorf(
       'The parameter "bodyBytesLimit" of "fetchRequestBody" should be a number, but %v was given.',
       bodyBytesLimit
     );
@@ -345,6 +328,23 @@ function fetchRequestBody(request, bodyBytesLimit = 0) {
 }
 __name(fetchRequestBody, "fetchRequestBody");
 
+// src/utils/parse-cookie-string.js
+var import_js_format7 = require("@e22m4u/js-format");
+function parseCookieString(input) {
+  if (typeof input !== "string")
+    throw new import_js_format7.Errorf(
+      "The first parameter of `parseCookieString` must be a String, but %v was given.",
+      input
+    );
+  return input.split(";").filter((v) => v !== "").map((v) => v.split("=")).reduce((cookies, tuple) => {
+    const key = decodeURIComponent(tuple[0]).trim();
+    const value = tuple[1] !== void 0 ? decodeURIComponent(tuple[1]).trim() : "";
+    cookies[key] = value;
+    return cookies;
+  }, {});
+}
+__name(parseCookieString, "parseCookieString");
+
 // src/utils/create-request-mock.js
 var import_net = require("net");
 var import_tls = require("tls");
@@ -352,12 +352,12 @@ var import_http2 = require("http");
 var import_querystring = __toESM(require("querystring"), 1);
 var import_js_format9 = require("@e22m4u/js-format");
 
-// src/utils/create-cookies-string.js
+// src/utils/create-cookie-string.js
 var import_js_format8 = require("@e22m4u/js-format");
-function createCookiesString(data) {
+function createCookieString(data) {
   if (!data || typeof data !== "object" || Array.isArray(data))
     throw new import_js_format8.Errorf(
-      'The first parameter of "createCookiesString" should be an Object, but %v was given.',
+      "The first parameter of `createCookieString` should be an Object, but %v was given.",
       data
     );
   let cookies = "";
@@ -369,7 +369,7 @@ function createCookiesString(data) {
   }
   return cookies.trim();
 }
-__name(createCookiesString, "createCookiesString");
+__name(createCookieString, "createCookieString");
 
 // src/utils/create-request-mock.js
 function createRequestMock(patch) {
@@ -549,7 +549,7 @@ function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
       obj["cookie"] += obj["cookie"] ? `; ${cookies}` : cookies;
     } else if (typeof cookies === "object") {
       obj["cookie"] = obj["cookie"] ? obj["cookie"] : "";
-      const newCookies = createCookiesString(cookies);
+      const newCookies = createCookieString(cookies);
       obj["cookie"] += obj["cookie"] ? `; ${newCookies}` : newCookies;
     }
   }
@@ -1216,7 +1216,7 @@ var _CookiesParser = class _CookiesParser extends DebuggableService {
   parse(request) {
     const debug = this.getDebuggerFor(this.parse);
     const cookiesString = request.headers["cookie"] || "";
-    const cookies = parseCookies(cookiesString);
+    const cookies = parseCookieString(cookiesString);
     const cookiesKeys = Object.keys(cookies);
     if (cookiesKeys.length) {
       cookiesKeys.forEach((key) => {
@@ -1882,7 +1882,7 @@ var TrieRouter = _TrieRouter;
   TrieRouter,
   UNPARSABLE_MEDIA_TYPES,
   cloneDeep,
-  createCookiesString,
+  createCookieString,
   createDebugger,
   createError,
   createRequestMock,
@@ -1895,7 +1895,7 @@ var TrieRouter = _TrieRouter;
   isResponseSent,
   isWritableStream,
   parseContentType,
-  parseCookies,
+  parseCookieString,
   parseJsonBody,
   toCamelCase
 });

+ 2 - 2
src/parsers/cookies-parser.js

@@ -1,5 +1,5 @@
 import {DebuggableService} from '../debuggable-service.js';
-import {parseCookies, getRequestPathname} from '../utils/index.js';
+import {parseCookieString, getRequestPathname} from '../utils/index.js';
 
 /**
  * Cookies parser.
@@ -14,7 +14,7 @@ export class CookiesParser extends DebuggableService {
   parse(request) {
     const debug = this.getDebuggerFor(this.parse);
     const cookiesString = request.headers['cookie'] || '';
-    const cookies = parseCookies(cookiesString);
+    const cookies = parseCookieString(cookiesString);
     const cookiesKeys = Object.keys(cookies);
     if (cookiesKeys.length) {
       cookiesKeys.forEach(key => {

+ 6 - 0
src/utils/create-cookie-string.d.ts

@@ -0,0 +1,6 @@
+/**
+ * Create cookie string.
+ *
+ * @param data
+ */
+export declare function createCookieString(data: object): string;

+ 3 - 3
src/utils/create-cookies-string.js → src/utils/create-cookie-string.js

@@ -1,15 +1,15 @@
 import {Errorf} from '@e22m4u/js-format';
 
 /**
- * Create cookies string.
+ * Create cookie string.
  *
  * @param {object} data
  * @returns {string}
  */
-export function createCookiesString(data) {
+export function createCookieString(data) {
   if (!data || typeof data !== 'object' || Array.isArray(data))
     throw new Errorf(
-      'The first parameter of "createCookiesString" should be ' +
+      'The first parameter of `createCookieString` should be ' +
         'an Object, but %v was given.',
       data,
     );

+ 6 - 6
src/utils/create-cookies-string.spec.js → src/utils/create-cookie-string.spec.js

@@ -1,13 +1,13 @@
 import {expect} from 'chai';
 import {format} from '@e22m4u/js-format';
-import {createCookiesString} from './create-cookies-string.js';
+import {createCookieString} from './create-cookie-string.js';
 
-describe('createCookiesString', function () {
+describe('createCookieString', function () {
   it('requires the first argument to be an object', function () {
-    const throwable = v => () => createCookiesString(v);
+    const throwable = v => () => createCookieString(v);
     const error = v =>
       format(
-        'The first parameter of "createCookiesString" should be ' +
+        'The first parameter of `createCookieString` should be ' +
           'an Object, but %s was given.',
         v,
       );
@@ -25,12 +25,12 @@ describe('createCookiesString', function () {
   });
 
   it('returns an empty string if no keys', function () {
-    expect(createCookiesString({})).to.be.eq('');
+    expect(createCookieString({})).to.be.eq('');
   });
 
   it('returns a cookies string from a given object', function () {
     const data = {foo: 'bar', baz: 'quz'};
-    const result = createCookiesString(data);
+    const result = createCookieString(data);
     expect(result).to.be.eq('foo=bar; baz=quz;');
   });
 });

+ 0 - 6
src/utils/create-cookies-string.d.ts

@@ -1,6 +0,0 @@
-/**
- * Create cookies string.
- *
- * @param data
- */
-export declare function createCookiesString(data: object): string;

+ 2 - 2
src/utils/create-request-mock.js

@@ -4,7 +4,7 @@ import {IncomingMessage} from 'http';
 import queryString from 'querystring';
 import {Errorf} from '@e22m4u/js-format';
 import {isReadableStream} from './is-readable-stream.js';
-import {createCookiesString} from './create-cookies-string.js';
+import {createCookieString} from './create-cookie-string.js';
 import {CHARACTER_ENCODING_LIST} from './fetch-request-body.js';
 
 /**
@@ -291,7 +291,7 @@ function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
       obj['cookie'] += obj['cookie'] ? `; ${cookies}` : cookies;
     } else if (typeof cookies === 'object') {
       obj['cookie'] = obj['cookie'] ? obj['cookie'] : '';
-      const newCookies = createCookiesString(cookies);
+      const newCookies = createCookieString(cookies);
       obj['cookie'] += obj['cookie'] ? `; ${newCookies}` : newCookies;
     }
   }

+ 2 - 2
src/utils/index.d.ts

@@ -1,7 +1,6 @@
 export * from './clone-deep.js';
 export * from './is-promise.js';
 export * from './create-error.js';
-export * from './parse-cookies.js';
 export * from './to-camel-case.js';
 export * from './create-debugger.js';
 export * from './is-response-sent.js';
@@ -10,7 +9,8 @@ export * from './is-readable-stream.js';
 export * from './parse-content-type.js';
 export * from './is-writable-stream.js';
 export * from './fetch-request-body.js';
+export * from './parse-cookie-string.js';
 export * from './create-request-mock.js';
 export * from './create-response-mock.js';
-export * from './create-cookies-string.js';
 export * from './get-request-pathname.js';
+export * from './create-cookie-string.js';

+ 2 - 2
src/utils/index.js

@@ -1,7 +1,6 @@
 export * from './clone-deep.js';
 export * from './is-promise.js';
 export * from './create-error.js';
-export * from './parse-cookies.js';
 export * from './to-camel-case.js';
 export * from './create-debugger.js';
 export * from './is-response-sent.js';
@@ -10,7 +9,8 @@ export * from './is-readable-stream.js';
 export * from './parse-content-type.js';
 export * from './is-writable-stream.js';
 export * from './fetch-request-body.js';
+export * from './parse-cookie-string.js';
 export * from './create-request-mock.js';
 export * from './create-response-mock.js';
-export * from './create-cookies-string.js';
 export * from './get-request-pathname.js';
+export * from './create-cookie-string.js';

+ 3 - 3
src/utils/parse-cookies.d.ts → src/utils/parse-cookie-string.d.ts

@@ -6,14 +6,14 @@ type ParsedCookies = {
 };
 
 /**
- * Parse cookies.
+ * Parse cookie string.
  *
  * @example
  * ```ts
- * parseCookies('pkg=math; equation=E%3Dmc%5E2');
+ * parseCookieString('pkg=math; equation=E%3Dmc%5E2');
  * // {pkg: 'math', equation: 'E=mc^2'}
  * ```
  *
  * @param input
  */
-export declare function parseCookies(input: string): ParsedCookies;
+export declare function parseCookieString(input: string): ParsedCookies;

+ 4 - 4
src/utils/parse-cookies.js → src/utils/parse-cookie-string.js

@@ -1,21 +1,21 @@
 import {Errorf} from '@e22m4u/js-format';
 
 /**
- * Parse cookies.
+ * Parse cookie string.
  *
  * @example
  * ```ts
- * parseCookies('pkg=math; equation=E%3Dmc%5E2');
+ * parseCookieString('pkg=math; equation=E%3Dmc%5E2');
  * // {pkg: 'math', equation: 'E=mc^2'}
  * ```
  *
  * @param {string} input
  * @returns {object}
  */
-export function parseCookies(input) {
+export function parseCookieString(input) {
   if (typeof input !== 'string')
     throw new Errorf(
-      'The first parameter of "parseCookies" should be a String, ' +
+      'The first parameter of `parseCookieString` must be a String, ' +
         'but %v was given.',
       input,
     );

+ 8 - 8
src/utils/parse-cookies.spec.js → src/utils/parse-cookie-string.spec.js

@@ -1,14 +1,14 @@
 import {expect} from 'chai';
 import {format} from '@e22m4u/js-format';
-import {parseCookies} from './parse-cookies.js';
+import {parseCookieString} from './parse-cookie-string.js';
 
-describe('parseCookies', function () {
+describe('parseCookieString', function () {
   it('requires the first parameter to be an IncomingMessage instance', function () {
-    const throwable = v => () => parseCookies(v);
+    const throwable = v => () => parseCookieString(v);
     const error = v =>
       format(
-        'The first parameter of "parseCookies" should be ' +
-          'a String, but %s was given.',
+        'The first parameter of `parseCookieString` must be a String, ' +
+          'but %s was given.',
         v,
       );
     expect(throwable(10)).to.throw(error('10'));
@@ -25,18 +25,18 @@ describe('parseCookies', function () {
 
   it('returns cookies as a plain object', function () {
     const value = 'pkg=math; equation=E%3Dmc%5E2';
-    const result = parseCookies(value);
+    const result = parseCookieString(value);
     expect(result).to.have.property('pkg', 'math');
     expect(result).to.have.property('equation', 'E=mc^2');
   });
 
   it('returns an empty object for an empty string', function () {
-    const result = parseCookies('');
+    const result = parseCookieString('');
     expect(result).to.be.eql({});
   });
 
   it('parses an empty cookie as an empty string', function () {
-    const result = parseCookies('foo=bar; baz');
+    const result = parseCookieString('foo=bar; baz');
     expect(result).to.be.eql({foo: 'bar', baz: ''});
   });
 });