@rsbuild/core 2.0.0-alpha.4 → 2.0.0-beta.1

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,5 +1,4 @@
1
1
  /// <reference types="node" />
2
-
3
2
  import * as http from 'http';
4
3
  import events from 'events';
5
4
  import * as net from 'net';
@@ -253,52 +252,125 @@ declare namespace Server {
253
252
  * https://github.com/DefinitelyTyped/DefinitelyTyped/blob/6f529c6c67a447190f86bfbf894d1061e41e07b7/types/http-proxy-middleware/index.d.ts
254
253
  */
255
254
 
256
- interface Request extends http.IncomingMessage {
255
+ type NextFunction<T = (err?: any) => void> = T;
256
+ interface RequestHandler<TReq = http.IncomingMessage, TRes = http.ServerResponse, TNext = NextFunction> {
257
+ (req: TReq, res: TRes, next?: TNext): Promise<void>;
258
+ upgrade: (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => void;
257
259
  }
258
- interface Response extends http.ServerResponse {
260
+ type Filter<TReq = http.IncomingMessage> = string | string[] | ((pathname: string, req: TReq) => boolean);
261
+ interface Plugin<TReq = http.IncomingMessage, TRes = http.ServerResponse> {
262
+ (proxyServer: Server<TReq, TRes>, options: Options<TReq, TRes>): void;
259
263
  }
260
- interface RequestHandler {
261
- (req: Request, res: Response, next?: (err?: any) => void): void | Promise<void>;
262
- upgrade?: (req: Request, socket: net.Socket, head: any) => void;
264
+ interface OnProxyEvent<TReq = http.IncomingMessage, TRes = http.ServerResponse> {
265
+ error?: Server.ErrorCallback<Error, TReq, TRes>;
266
+ proxyReq?: Server.ProxyReqCallback<http.ClientRequest, TReq, TRes>;
267
+ proxyReqWs?: Server.ProxyReqWsCallback<http.ClientRequest, TReq>;
268
+ proxyRes?: Server.ProxyResCallback<TReq, TRes>;
269
+ open?: Server.OpenCallback;
270
+ close?: Server.CloseCallback<TReq>;
271
+ start?: Server.StartCallback<TReq, TRes>;
272
+ end?: Server.EndCallback<TReq, TRes>;
273
+ econnreset?: Server.EconnresetCallback<Error, TReq, TRes>;
263
274
  }
264
- declare type Filter = string | string[] | ((pathname: string, req: Request) => boolean);
265
- interface Options extends Server.ServerOptions {
275
+ type Logger = Pick<Console, 'info' | 'warn' | 'error'>;
276
+ interface Options<TReq = http.IncomingMessage, TRes = http.ServerResponse> extends Server.ServerOptions {
277
+ /**
278
+ * Narrow down requests to proxy or not.
279
+ * Filter on {@link http.IncomingMessage.url `pathname`} which is relative to the proxy's "mounting" point in the server.
280
+ * Or use the {@link http.IncomingMessage `req`} object for more complex filtering.
281
+ * @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathFilter.md
282
+ * @since v3.0.0
283
+ */
284
+ pathFilter?: Filter<TReq>;
285
+ /**
286
+ * Modify request paths before requests are send to the target.
287
+ * @example
288
+ * ```js
289
+ * createProxyMiddleware({
290
+ * pathRewrite: {
291
+ * '^/api/old-path': '/api/new-path', // rewrite path
292
+ * }
293
+ * });
294
+ * ```
295
+ * @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathRewrite.md
296
+ */
266
297
  pathRewrite?: {
267
298
  [regexp: string]: string;
268
- } | ((path: string, req: Request) => string) | ((path: string, req: Request) => Promise<string>);
299
+ } | ((path: string, req: TReq) => string | undefined) | ((path: string, req: TReq) => Promise<string>);
300
+ /**
301
+ * Access the internal http-proxy server instance to customize behavior
302
+ *
303
+ * @example
304
+ * ```js
305
+ * createProxyMiddleware({
306
+ * plugins: [(proxyServer, options) => {
307
+ * proxyServer.on('error', (error, req, res) => {
308
+ * console.error(error);
309
+ * });
310
+ * }]
311
+ * });
312
+ * ```
313
+ * @link https://github.com/chimurai/http-proxy-middleware#plugins-array
314
+ * @since v3.0.0
315
+ */
316
+ plugins?: Plugin<TReq, TRes>[];
317
+ /**
318
+ * Eject pre-configured plugins.
319
+ * NOTE: register your own error handlers to prevent server from crashing.
320
+ *
321
+ * @link https://github.com/chimurai/http-proxy-middleware#ejectplugins-boolean-default-false
322
+ * @since v3.0.0
323
+ */
324
+ ejectPlugins?: boolean;
325
+ /**
326
+ * Listen to http-proxy events
327
+ * @see {@link OnProxyEvent} for available events
328
+ * @example
329
+ * ```js
330
+ * createProxyMiddleware({
331
+ * on: {
332
+ * error: (error, req, res, target) => {
333
+ * console.error(error);
334
+ * }
335
+ * }
336
+ * });
337
+ * ```
338
+ * @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/proxy-events.md
339
+ * @since v3.0.0
340
+ */
341
+ on?: OnProxyEvent<TReq, TRes>;
342
+ /**
343
+ * Dynamically set the {@link Options.target `options.target`}.
344
+ * @example
345
+ * ```js
346
+ * createProxyMiddleware({
347
+ * router: async (req) => {
348
+ * return 'http://127:0.0.1:3000';
349
+ * }
350
+ * });
351
+ * ```
352
+ * @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/router.md
353
+ */
269
354
  router?: {
270
355
  [hostOrPath: string]: Server.ServerOptions['target'];
271
- } | ((req: Request) => Server.ServerOptions['target']) | ((req: Request) => Promise<Server.ServerOptions['target']>);
272
- logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent';
273
- logProvider?: LogProviderCallback;
274
- onError?: OnErrorCallback;
275
- onProxyRes?: OnProxyResCallback;
276
- onProxyReq?: OnProxyReqCallback;
277
- onProxyReqWs?: OnProxyReqWsCallback;
278
- onOpen?: OnOpenCallback;
279
- onClose?: OnCloseCallback;
280
- }
281
- interface LogProvider {
282
- log: Logger;
283
- debug?: Logger;
284
- info?: Logger;
285
- warn?: Logger;
286
- error?: Logger;
356
+ } | ((req: TReq) => Server.ServerOptions['target']) | ((req: TReq) => Promise<Server.ServerOptions['target']>);
357
+ /**
358
+ * Log information from http-proxy-middleware
359
+ * @example
360
+ * ```js
361
+ * createProxyMiddleware({
362
+ * logger: console
363
+ * });
364
+ * ```
365
+ * @link https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/logger.md
366
+ * @since v3.0.0
367
+ */
368
+ logger?: Logger | any;
287
369
  }
288
- declare type Logger = (...args: any[]) => void;
289
- declare type LogProviderCallback = (provider: LogProvider) => LogProvider;
290
- /**
291
- * Use types based on the events listeners from http-proxy
292
- * https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51504fd999031b7f025220fab279f1b2155cbaff/types/http-proxy/index.d.ts
293
- */
294
- declare type OnErrorCallback = (err: Error, req: Request, res: Response, target?: string | Partial<url.Url>) => void;
295
- declare type OnProxyResCallback = (proxyRes: http.IncomingMessage, req: Request, res: Response) => void;
296
- declare type OnProxyReqCallback = (proxyReq: http.ClientRequest, req: Request, res: Response, options: Server.ServerOptions) => void;
297
- declare type OnProxyReqWsCallback = (proxyReq: http.ClientRequest, req: Request, socket: net.Socket, options: Server.ServerOptions, head: any) => void;
298
- declare type OnCloseCallback = (proxyRes: Response, proxySocket: net.Socket, proxyHead: any) => void;
299
- declare type OnOpenCallback = (proxySocket: net.Socket) => void;
300
370
 
301
- declare type Interceptor = (buffer: Buffer, proxyRes: http.IncomingMessage, req: http.IncomingMessage, res: http.ServerResponse) => Promise<Buffer | string>;
371
+ declare function createProxyMiddleware<TReq = http.IncomingMessage, TRes = http.ServerResponse, TNext = NextFunction>(options: Options<TReq, TRes>): RequestHandler<TReq, TRes, TNext>;
372
+
373
+ type Interceptor<TReq = http.IncomingMessage, TRes = http.ServerResponse> = (buffer: Buffer, proxyRes: TReq, req: TReq, res: TRes) => Promise<Buffer | string>;
302
374
  /**
303
375
  * Intercept responses from upstream.
304
376
  * Automatically decompress (deflate, gzip, brotli).
@@ -306,14 +378,167 @@ declare type Interceptor = (buffer: Buffer, proxyRes: http.IncomingMessage, req:
306
378
  *
307
379
  * NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())
308
380
  */
309
- declare function responseInterceptor(interceptor: Interceptor): (proxyRes: http.IncomingMessage, req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
381
+ declare function responseInterceptor<TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse>(interceptor: Interceptor<TReq, TRes>): (proxyRes: TReq, req: TReq, res: TRes) => Promise<void>;
310
382
 
383
+ type BodyParserLikeRequest = http.IncomingMessage & {
384
+ body?: any;
385
+ };
311
386
  /**
312
387
  * Fix proxied body if bodyParser is involved.
313
388
  */
314
- declare function fixRequestBody(proxyReq: http.ClientRequest, req: http.IncomingMessage): void;
389
+ declare function fixRequestBody<TReq extends BodyParserLikeRequest = BodyParserLikeRequest>(proxyReq: http.ClientRequest, req: TReq): void;
390
+
391
+ /**
392
+ * Subscribe to {@link https://www.npmjs.com/package/http-proxy#listening-for-proxy-events http-proxy error events} to prevent server from crashing.
393
+ * Errors are logged with {@link https://www.npmjs.com/package/debug debug} library.
394
+ */
395
+ declare const debugProxyErrorsPlugin: Plugin;
396
+
397
+ declare const errorResponsePlugin: Plugin;
398
+
399
+ declare const loggerPlugin: Plugin;
400
+
401
+ /**
402
+ * Implements option.on object to subscribe to http-proxy events.
403
+ *
404
+ * @example
405
+ * ```js
406
+ * createProxyMiddleware({
407
+ * on: {
408
+ * error: (error, req, res, target) => {},
409
+ * proxyReq: (proxyReq, req, res, options) => {},
410
+ * proxyReqWs: (proxyReq, req, socket, options) => {},
411
+ * proxyRes: (proxyRes, req, res) => {},
412
+ * open: (proxySocket) => {},
413
+ * close: (proxyRes, proxySocket, proxyHead) => {},
414
+ * start: (req, res, target) => {},
415
+ * end: (req, res, proxyRes) => {},
416
+ * econnreset: (error, req, res, target) => {},
417
+ * }
418
+ * });
419
+ * ```
420
+ */
421
+ declare const proxyEventsPlugin: Plugin;
422
+
423
+ /**
424
+ * @deprecated
425
+ *
426
+ * Will be removed in a future version.
427
+ */
428
+ interface LegacyOptions<TReq = http.IncomingMessage, TRes = http.ServerResponse> extends Options<TReq, TRes> {
429
+ /**
430
+ * @deprecated
431
+ * Use `on.error` instead.
432
+ *
433
+ * @example
434
+ * ```js
435
+ * {
436
+ * on: {
437
+ * error: () => {}
438
+ * }
439
+ * ```
440
+ */
441
+ onError?: (...args: any[]) => void;
442
+ /**
443
+ * @deprecated
444
+ * Use `on.proxyRes` instead.
445
+ *
446
+ * @example
447
+ * ```js
448
+ * {
449
+ * on: {
450
+ * proxyRes: () => {}
451
+ * }
452
+ * ```
453
+ */
454
+ onProxyRes?: (...args: any[]) => void;
455
+ /**
456
+ * @deprecated
457
+ * Use `on.proxyReq` instead.
458
+ *
459
+ * @example
460
+ * ```js
461
+ * {
462
+ * on: {
463
+ * proxyReq: () => {}
464
+ * }
465
+ * ```
466
+ */
467
+ onProxyReq?: (...args: any[]) => void;
468
+ /**
469
+ * @deprecated
470
+ * Use `on.proxyReqWs` instead.
471
+ *
472
+ * @example
473
+ * ```js
474
+ * {
475
+ * on: {
476
+ * proxyReqWs: () => {}
477
+ * }
478
+ * ```
479
+ */
480
+ onProxyReqWs?: (...args: any[]) => void;
481
+ /**
482
+ * @deprecated
483
+ * Use `on.open` instead.
484
+ *
485
+ * @example
486
+ * ```js
487
+ * {
488
+ * on: {
489
+ * open: () => {}
490
+ * }
491
+ * ```
492
+ */
493
+ onOpen?: (...args: any[]) => void;
494
+ /**
495
+ * @deprecated
496
+ * Use `on.close` instead.
497
+ *
498
+ * @example
499
+ * ```js
500
+ * {
501
+ * on: {
502
+ * close: () => {}
503
+ * }
504
+ * ```
505
+ */
506
+ onClose?: (...args: any[]) => void;
507
+ /**
508
+ * @deprecated
509
+ * Use `logger` instead.
510
+ *
511
+ * @example
512
+ * ```js
513
+ * {
514
+ * logger: console
515
+ * }
516
+ * ```
517
+ */
518
+ logProvider?: any;
519
+ /**
520
+ * @deprecated
521
+ * Use `logger` instead.
522
+ *
523
+ * @example
524
+ * ```js
525
+ * {
526
+ * logger: console
527
+ * }
528
+ * ```
529
+ */
530
+ logLevel?: any;
531
+ }
315
532
 
316
- declare function createProxyMiddleware(context: Filter | Options, options?: Options): RequestHandler;
533
+ /**
534
+ * @deprecated
535
+ * This function is deprecated and will be removed in a future version.
536
+ *
537
+ * Use {@link createProxyMiddleware} instead.
538
+ */
539
+ declare function legacyCreateProxyMiddleware<TReq = http.IncomingMessage, TRes = http.ServerResponse>(shortHand: string): RequestHandler<TReq, TRes>;
540
+ declare function legacyCreateProxyMiddleware<TReq = http.IncomingMessage, TRes = http.ServerResponse>(legacyOptions: LegacyOptions<TReq, TRes>): RequestHandler<TReq, TRes>;
541
+ declare function legacyCreateProxyMiddleware<TReq = http.IncomingMessage, TRes = http.ServerResponse>(legacyContext: Filter<TReq>, legacyOptions: LegacyOptions<TReq, TRes>): RequestHandler<TReq, TRes>;
317
542
 
318
- export { createProxyMiddleware, fixRequestBody, responseInterceptor };
319
- export type { Filter, Options, RequestHandler };
543
+ export { createProxyMiddleware, debugProxyErrorsPlugin, errorResponsePlugin, fixRequestBody, legacyCreateProxyMiddleware, loggerPlugin, proxyEventsPlugin, responseInterceptor };
544
+ export type { Filter, LegacyOptions, Options, Plugin, RequestHandler };