@syntay/fastay 0.2.4 → 0.2.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.
@@ -1,457 +0,0 @@
1
- import * as http from 'http';
2
- import * as net from 'net';
3
-
4
- export interface CookieItem {
5
- value: string;
6
- }
7
-
8
- export interface RequestCookies {
9
- /**
10
- * Retrieves a cookie by its name.
11
- * @param name - The name of the cookie to retrieve.
12
- * @returns An object containing the cookie's value, or undefined if not found.
13
- */
14
- get(name: string): CookieItem | undefined;
15
-
16
- /**
17
- * Checks if a cookie with the given name exists.
18
- * @param name - The name of the cookie to check.
19
- * @returns True if the cookie exists, false otherwise.
20
- */
21
- has(name: string): boolean;
22
-
23
- /**
24
- * Returns all cookies as a key-value object.
25
- * @returns An object where keys are cookie names and values are cookie values.
26
- */
27
- all(): Record<string, string>;
28
- }
29
-
30
- /**
31
- * Interface representing the Fastay Request object
32
- * @interface Request
33
- * @extends http.IncomingMessage
34
- */
35
- export interface Request extends http.IncomingMessage {
36
- // ==================== CORE EXPRESS PROPERTIES ====================
37
-
38
- /**
39
- * Contains a reference to the instance of the Express application that is using the middleware.
40
- * @type {Express}
41
- */
42
- app: Express;
43
-
44
- /**
45
- * The URL path on which a router instance was mounted.
46
- * @type {string}
47
- * @example
48
- * // app.use('/admin', router)
49
- * // req.baseUrl would be '/admin'
50
- */
51
- baseUrl: string;
52
-
53
- /**
54
- * Contains key-value pairs of data submitted in the request body.
55
- * By default, it is undefined and is populated when you use body-parsing middleware such as express.json() or express.urlencoded().
56
- * @type {any}
57
- */
58
- body: any;
59
-
60
- /**
61
- * When using cookie-parser middleware, this property is an object that contains cookies sent by the request.
62
- * If the request contains no cookies, it defaults to {}.
63
- * @type {object}
64
- */
65
- cookies: RequestCookies;
66
-
67
- /**
68
- * Indicates whether the request is "fresh". It is the opposite of req.stale.
69
- * It is true if the cache-control request header doesn't have a no-cache directive
70
- * and any of the following are true:
71
- * - The if-modified-since request header is specified and last-modified request header is equal to or earlier than the modified response header.
72
- * - The if-none-match request header is *.
73
- * - The if-none-match request header, after being parsed into its directives, does not match the etag response header.
74
- * @type {boolean}
75
- */
76
- fresh: boolean;
77
-
78
- /**
79
- * Contains the hostname derived from the Host HTTP header.
80
- * @type {string}
81
- */
82
- hostname: string;
83
-
84
- /**
85
- * Contains the remote IP address of the request.
86
- * When the trust proxy setting does not evaluate to false, the value of this property
87
- * is derived from the left-most entry in the X-Forwarded-For header.
88
- * @type {string}
89
- */
90
- ip: string;
91
-
92
- /**
93
- * When the trust proxy setting does not evaluate to false, this property contains an array of IP addresses
94
- * specified in the X-Forwarded-For request header. Otherwise, it contains an empty array.
95
- * @type {string[]}
96
- */
97
- ips: string[];
98
-
99
- /**
100
- * Contains a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on.
101
- * @type { 'GET'
102
- | 'POST'
103
- | 'PUT'
104
- | 'DELETE'
105
- | 'PATCH'
106
- | 'OPTIONS'
107
- | 'HEAD'
108
- | string}
109
- */
110
- method:
111
- | 'GET'
112
- | 'POST'
113
- | 'PUT'
114
- | 'DELETE'
115
- | 'PATCH'
116
- | 'OPTIONS'
117
- | 'HEAD'
118
- | string;
119
-
120
- /**
121
- * This property is much like req.url; however, it retains the original request URL, allowing you to rewrite req.url freely for internal routing purposes.
122
- * @type {string}
123
- * @example
124
- * // GET /search?q=something
125
- * console.log(req.originalUrl); // '/search?q=something'
126
- */
127
- originalUrl: string;
128
-
129
- /**
130
- * This property is an object containing properties mapped to the named route "parameters".
131
- * For example, if you have the route /user/:name, then the "name" property is available as req.params.name.
132
- * This object defaults to {}.
133
- * @type {object}
134
- */
135
- params: any;
136
-
137
- /**
138
- * Contains the path part of the request URL.
139
- * @type {string}
140
- * @example
141
- * // example.com/users?sort=desc
142
- * console.log(req.path); // '/users'
143
- */
144
- path: string;
145
-
146
- /**
147
- * Contains the request protocol string: either http or (for TLS requests) https.
148
- * When the trust proxy setting does not evaluate to false, this property will use the value of the X-Forwarded-Proto header field if present.
149
- * @type {string}
150
- */
151
- protocol: string;
152
-
153
- /**
154
- * This property is an object containing a property for each query string parameter in the route.
155
- * When query parser is set to disabled, it is an empty object {}.
156
- * @type {object}
157
- */
158
- query: any;
159
-
160
- /**
161
- * Contains the currently-matched route, a string. For example:
162
- * @type {string}
163
- * @example
164
- * app.get('/user/:id?', function userIdHandler(req, res) {
165
- * console.log(req.route); // { path: '/user/:id?', ... }
166
- * });
167
- */
168
- route: any;
169
-
170
- /**
171
- * A Boolean property that is true if a TLS connection is established.
172
- * Equivalent to: req.protocol === 'https'
173
- * @type {boolean}
174
- */
175
- secure: boolean;
176
-
177
- /**
178
- * When using cookie-parser middleware, this property contains signed cookies sent by the request, unsigned and ready for use.
179
- * Signed cookies reside in a different object to show developer intent; otherwise, a malicious attack could be placed on req.cookie values.
180
- * @type {object}
181
- */
182
- signedCookies: any;
183
-
184
- /**
185
- * Indicates whether the request is "stale", and is the opposite of req.fresh.
186
- * For more information, see req.fresh.
187
- * @type {boolean}
188
- */
189
- stale: boolean;
190
-
191
- /**
192
- * Contains an array of subdomains in the domain name of the request.
193
- * @type {string[]}
194
- * @example
195
- * // Host: "tobi.ferrets.example.com"
196
- * console.log(req.subdomains); // ['ferrets', 'tobi']
197
- */
198
- subdomains: string[];
199
-
200
- /**
201
- * True if the request's X-Requested-With header field is "XMLHttpRequest", indicating that the request was issued by a client library such as jQuery.
202
- * @type {boolean}
203
- */
204
- xhr: boolean;
205
-
206
- // ==================== EXPRESS METHODS ====================
207
-
208
- /**
209
- * Checks if the specified content types are acceptable, based on the request's Accept HTTP header field.
210
- * The method returns the best match, or if none of the specified content types is acceptable, returns false.
211
- * @param {string|string[]} types - The content types to check
212
- * @returns {string|false} The best matching content type, or false if none are acceptable
213
- * @example
214
- * // Accept: text/html
215
- * req.accepts('html'); // => 'html'
216
- * req.accepts('text/html'); // => 'text/html'
217
- */
218
- accepts(types: string | string[]): string | false;
219
-
220
- /**
221
- * Returns the first accepted charset of the specified character sets, based on the request's Accept-Charset HTTP header field.
222
- * @param {string|string[]} charsets - The charsets to check
223
- * @returns {string|false} The best matching charset, or false if none are acceptable
224
- */
225
- acceptsCharsets(charsets: string | string[]): string | false;
226
-
227
- /**
228
- * Returns the first accepted encoding of the specified encodings, based on the request's Accept-Encoding HTTP header field.
229
- * @param {string|string[]} encodings - The encodings to check
230
- * @returns {string|false} The best matching encoding, or false if none are acceptable
231
- */
232
- acceptsEncodings(encodings: string | string[]): string | false;
233
-
234
- /**
235
- * Returns the first accepted language of the specified languages, based on the request's Accept-Language HTTP header field.
236
- * @param {string|string[]} langs - The languages to check
237
- * @returns {string|false} The best matching language, or false if none are acceptable
238
- */
239
- acceptsLanguages(langs: string | string[]): string | false;
240
-
241
- /**
242
- * Returns the specified HTTP request header field (case-insensitive match).
243
- * The Referrer and Referer fields are interchangeable.
244
- * @param {string} field - The header field name
245
- * @returns {string} The header value
246
- * @example
247
- * req.get('Content-Type'); // => "text/plain"
248
- * req.get('content-type'); // => "text/plain"
249
- */
250
- get(field: string): string;
251
-
252
- /**
253
- * Alias for req.get().
254
- * @param {string} name - The header field name
255
- * @returns {string} The header value
256
- */
257
- header(name: string): string;
258
-
259
- /**
260
- * Checks if the incoming request contains the "Content-Type" header field and if it matches the given type string.
261
- * @param {string|string[]} types - The content type(s) to check for
262
- * @returns {string|false} The matching content type, or false if no match
263
- * @example
264
- * // With Content-Type: text/html; charset=utf-8
265
- * req.is('html'); // => 'html'
266
- * req.is('text/html'); // => 'text/html'
267
- */
268
- is(types: string | string[]): string | false;
269
-
270
- /**
271
- * Parse Range header field, capping to the given size.
272
- * The ranges property is an array of ranges, or undefined if there are no ranges.
273
- * @param {number} size - The maximum size of the range
274
- * @returns {any} An array of ranges or undefined
275
- */
276
- range(size: number, options?: any): any;
277
-
278
- // ==================== NODE.JS HTTP INHERITED PROPERTIES ====================
279
-
280
- /**
281
- * @deprecated Since v13.4.0, v12.16.0 - Use request.destroyed instead
282
- * Is true if the request has been aborted.
283
- * @type {boolean}
284
- */
285
- aborted: boolean;
286
-
287
- /**
288
- * The request/response headers object. Key-value pairs of header names and values.
289
- * @type {http.IncomingHttpHeaders}
290
- */
291
- headers: http.IncomingHttpHeaders;
292
-
293
- /**
294
- * The raw request/response headers list exactly as they were received.
295
- * @type {string[]}
296
- */
297
- rawHeaders: string[];
298
-
299
- /**
300
- * The raw request/response trailer keys and values exactly as they were received.
301
- * Only populated at the 'end' event.
302
- * @type {string[]}
303
- */
304
- rawTrailers: string[];
305
-
306
- /**
307
- * The net.Socket object associated with the connection.
308
- * @type {net.Socket}
309
- */
310
- socket: net.Socket;
311
-
312
- /**
313
- * The request/response trailers object. Only populated at the 'end' event.
314
- * @type {NodeJS.Dict<string>}
315
- */
316
- trailers: NodeJS.Dict<string>;
317
-
318
- /**
319
- * The request method as a string. Read only. Example: 'GET', 'DELETE'.
320
- * @type {string}
321
- */
322
-
323
- /**
324
- * Request URL string. This contains only the URL that is present in the actual HTTP request.
325
- * @type {string}
326
- */
327
- url: string;
328
-
329
- /**
330
- * In case of server request, the HTTP version sent by the client.
331
- * @type {string}
332
- */
333
- httpVersion: string;
334
-
335
- /**
336
- * The 3-digit HTTP response status code. E.G. 404.
337
- * @type {number}
338
- */
339
- statusCode: number;
340
-
341
- /**
342
- * The HTTP response status message (reason phrase). E.G. OK or Internal Server Error.
343
- * @type {string}
344
- */
345
- statusMessage: string;
346
-
347
- /**
348
- * Calls destroy() on the socket that received the IncomingMessage.
349
- * @returns {this}
350
- */
351
- destroy(error?: Error): this;
352
-
353
- // ==================== ADDITIONAL PROPERTIES (Common Middlewares) ====================
354
-
355
- /**
356
- * File object(s) uploaded via multer middleware
357
- * @type {Express.Multer.File|Express.Multer.File[]}
358
- */
359
- file?: Express.Multer.File;
360
- files?:
361
- | Express.Multer.File[]
362
- | { [fieldname: string]: Express.Multer.File[] };
363
-
364
- /**
365
- * Authenticated user object when using Passport.js
366
- * @type {any}
367
- */
368
- user?: any;
369
-
370
- /**
371
- * Session object when using express-session
372
- * @type {Express.Session}
373
- */
374
- session?: any;
375
-
376
- /**
377
- * Session ID when using express-session
378
- * @type {string}
379
- */
380
- sessionID?: string;
381
-
382
- /**
383
- * Authentication context when using multiple strategies
384
- * @type {any}
385
- */
386
- authInfo?: any;
387
-
388
- /**
389
- * Nonce for Content Security Policy when using helmet
390
- * @type {string}
391
- */
392
- cspNonce?: string;
393
- }
394
-
395
- // ==================== SUPPORTING INTERFACES ====================
396
-
397
- /**
398
- * Interface for Express application
399
- */
400
- interface Express {
401
- /**
402
- * Settings for the application
403
- */
404
- settings: any;
405
-
406
- /**
407
- * Middleware stack
408
- */
409
- _router: any;
410
-
411
- /**
412
- * Event emitter methods
413
- */
414
- on: (event: string, listener: Function) => Express;
415
- emit: (event: string, ...args: any[]) => boolean;
416
- }
417
-
418
- /**
419
- * Interface for Multer file object
420
- */
421
- declare namespace Express {
422
- namespace Multer {
423
- interface File {
424
- /** Field name specified in the form */
425
- fieldname: string;
426
- /** Name of the file on the user's computer */
427
- originalname: string;
428
- /** Encoding type of the file */
429
- encoding: string;
430
- /** Mime type of the file */
431
- mimetype: string;
432
- /** Size of the file in bytes */
433
- size: number;
434
- /** The folder to which the file has been saved */
435
- destination: string;
436
- /** The name of the file within the destination */
437
- filename: string;
438
- /** Location of the uploaded file */
439
- path: string;
440
- /** A Buffer of the entire file */
441
- buffer: Buffer;
442
- }
443
- }
444
- }
445
-
446
- /**
447
- * Interface for Express Session
448
- */
449
- declare namespace Express {
450
- interface Session {
451
- id: string;
452
- cookie: any;
453
- [key: string]: any;
454
- }
455
- }
456
-
457
- export type { Response } from 'express';
@@ -1,34 +0,0 @@
1
- // utils/cookies.ts
2
- import { IncomingMessage } from 'http';
3
-
4
- export class RequestCookies {
5
- private cookies: Map<string, string> = new Map();
6
-
7
- constructor(cookieHeader: string | undefined) {
8
- if (!cookieHeader) return;
9
-
10
- cookieHeader.split(';').forEach((cookie) => {
11
- const [name, ...rest] = cookie.trim().split('=');
12
- if (!name) return;
13
- this.cookies.set(name, decodeURIComponent(rest.join('=')));
14
- });
15
- }
16
-
17
- public get(name: string) {
18
- const value = this.cookies.get(name);
19
- if (!value) return undefined;
20
- return { value };
21
- }
22
-
23
- public has(name: string) {
24
- return this.cookies.has(name);
25
- }
26
-
27
- public all() {
28
- const obj: Record<string, string> = {};
29
- this.cookies.forEach((v, k) => (obj[k] = v));
30
- return obj;
31
- }
32
- }
33
-
34
- export const cookies = RequestCookies;
@@ -1,155 +0,0 @@
1
- import Busboy from 'busboy';
2
-
3
- type FieldMap = Record<string, string | string[]>;
4
- type FileInfo = {
5
- filename: string;
6
- encoding: string;
7
- mimeType: string;
8
- size: number;
9
- buffer: Buffer;
10
- };
11
- type FileMap = Record<string, File[]>;
12
-
13
- // Classe File compatível com a do browser
14
- export class File {
15
- name: string;
16
- type: string;
17
- size: number;
18
- buffer: Buffer;
19
-
20
- constructor(file: FileInfo) {
21
- this.name = file.filename;
22
- this.type = file.mimeType;
23
- this.size = file.size;
24
- this.buffer = file.buffer;
25
- }
26
-
27
- arrayBuffer() {
28
- return this.buffer.buffer.slice(
29
- this.buffer.byteOffset,
30
- this.buffer.byteOffset + this.buffer.byteLength
31
- );
32
- }
33
- }
34
-
35
- export function formDataMiddleware() {
36
- return function (req: any, res: any, next: any) {
37
- if (typeof req.formData === 'function') return next();
38
-
39
- req.formData = () =>
40
- new Promise((resolve, reject) => {
41
- const bb = Busboy({ headers: req.headers });
42
-
43
- const fields: FieldMap = {};
44
- const files: FileMap = {};
45
-
46
- bb.on('field', (name, value) => {
47
- if (name.endsWith('[]')) {
48
- const key = name.slice(0, -2);
49
- if (!fields[key]) fields[key] = [];
50
- (fields[key] as string[]).push(value);
51
- } else {
52
- fields[name] = value;
53
- }
54
- });
55
-
56
- bb.on('file', (name, file, info) => {
57
- const chunks: Buffer[] = [];
58
-
59
- file.on('data', (chunk) => chunks.push(chunk));
60
-
61
- file.on('end', () => {
62
- const buffer = Buffer.concat(chunks);
63
-
64
- const fileObj = new File({
65
- filename: info.filename,
66
- encoding: info.encoding,
67
- mimeType: info.mimeType,
68
- size: buffer.length,
69
- buffer,
70
- });
71
-
72
- if (!files[name]) files[name] = [];
73
- files[name].push(fileObj);
74
- });
75
- });
76
-
77
- bb.on('finish', () => {
78
- resolve(createFormDataLike(fields, files));
79
- });
80
-
81
- bb.on('error', reject);
82
-
83
- req.pipe(bb);
84
- });
85
-
86
- next();
87
- };
88
- }
89
-
90
- function createFormDataLike(fields: FieldMap, files: FileMap) {
91
- return {
92
- get(key: string) {
93
- if (files[key]) return files[key][0];
94
- return fields[key] ?? null;
95
- },
96
-
97
- getAll(key: string) {
98
- if (files[key]) return files[key];
99
- const val = fields[key];
100
- return Array.isArray(val) ? val : val ? [val] : [];
101
- },
102
-
103
- has(key: string) {
104
- return !!fields[key] || !!files[key];
105
- },
106
-
107
- append(key: string, value: any) {
108
- if (value instanceof File) {
109
- if (!files[key]) files[key] = [];
110
- files[key].push(value);
111
- return;
112
- }
113
-
114
- if (!fields[key]) {
115
- fields[key] = value;
116
- } else if (Array.isArray(fields[key])) {
117
- (fields[key] as string[]).push(value);
118
- } else {
119
- fields[key] = [fields[key] as string, value];
120
- }
121
- },
122
-
123
- set(key: string, value: any) {
124
- if (value instanceof File) {
125
- files[key] = [value];
126
- delete fields[key];
127
- return;
128
- }
129
-
130
- fields[key] = value;
131
- delete files[key];
132
- },
133
-
134
- delete(key: string) {
135
- delete fields[key];
136
- delete files[key];
137
- },
138
-
139
- *entries() {
140
- for (const [k, v] of Object.entries(fields)) {
141
- if (Array.isArray(v)) {
142
- for (const item of v) yield [k, item];
143
- } else {
144
- yield [k, v];
145
- }
146
- }
147
-
148
- for (const [k, arr] of Object.entries(files)) {
149
- for (const file of arr) yield [k, file];
150
- }
151
- },
152
-
153
- raw: { fields, files },
154
- };
155
- }