@duplojs/http 0.9.5 → 0.11.0

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.
Files changed (113) hide show
  1. package/dist/client/clientCache.cjs +84 -0
  2. package/dist/client/clientCache.d.ts +4 -0
  3. package/dist/client/clientCache.mjs +80 -0
  4. package/dist/client/getBody.cjs +0 -3
  5. package/dist/client/getBody.mjs +0 -3
  6. package/dist/client/hooks.cjs +36 -0
  7. package/dist/client/hooks.d.ts +5 -1
  8. package/dist/client/hooks.mjs +33 -1
  9. package/dist/client/httpClient.cjs +26 -1
  10. package/dist/client/httpClient.d.ts +8 -1
  11. package/dist/client/httpClient.mjs +26 -1
  12. package/dist/client/index.cjs +12 -0
  13. package/dist/client/index.d.ts +2 -0
  14. package/dist/client/index.mjs +4 -2
  15. package/dist/client/promiseRequest.cjs +33 -8
  16. package/dist/client/promiseRequest.d.ts +2 -1
  17. package/dist/client/promiseRequest.mjs +34 -9
  18. package/dist/client/serverSentEvents.cjs +18 -12
  19. package/dist/client/serverSentEvents.d.ts +2 -1
  20. package/dist/client/serverSentEvents.mjs +18 -13
  21. package/dist/client/stream.cjs +117 -0
  22. package/dist/client/stream.d.ts +3 -0
  23. package/dist/client/stream.mjs +95 -0
  24. package/dist/client/types/clientCache.cjs +2 -0
  25. package/dist/client/types/clientCache.d.ts +27 -0
  26. package/dist/client/types/clientCache.mjs +1 -0
  27. package/dist/client/types/clientRequestParams.d.ts +8 -1
  28. package/dist/client/types/clientResponse.d.ts +24 -6
  29. package/dist/client/types/hooks.d.ts +10 -1
  30. package/dist/client/types/index.cjs +1 -0
  31. package/dist/client/types/index.d.ts +1 -0
  32. package/dist/client/types/index.mjs +1 -0
  33. package/dist/client/types/promiseRequestParams.d.ts +3 -0
  34. package/dist/client/types/serverRoute.d.ts +2 -0
  35. package/dist/core/builders/route/handler.d.ts +5 -5
  36. package/dist/core/defaultHooks/index.cjs +14 -0
  37. package/dist/core/defaultHooks/index.mjs +14 -0
  38. package/dist/core/functionsBuilders/steps/defaults/extractStep.mjs +1 -1
  39. package/dist/core/functionsBuilders/steps/defaults/handlerStep.cjs +33 -0
  40. package/dist/core/functionsBuilders/steps/defaults/handlerStep.mjs +33 -0
  41. package/dist/core/hub/defaultMalformedUrlHandler.d.ts +1 -1
  42. package/dist/core/hub/defaultNotfoundHandler.d.ts +1 -1
  43. package/dist/core/index.cjs +143 -134
  44. package/dist/core/index.d.ts +1 -0
  45. package/dist/core/index.mjs +42 -39
  46. package/dist/core/response/contract.cjs +21 -0
  47. package/dist/core/response/contract.d.ts +33 -5
  48. package/dist/core/response/contract.mjs +21 -0
  49. package/dist/core/response/index.cjs +4 -0
  50. package/dist/core/response/index.d.ts +7 -0
  51. package/dist/core/response/index.mjs +2 -0
  52. package/dist/core/response/streamPredicted.cjs +23 -0
  53. package/dist/core/response/streamPredicted.d.ts +14 -0
  54. package/dist/core/response/streamPredicted.mjs +21 -0
  55. package/dist/core/response/streamTextPredicted.cjs +23 -0
  56. package/dist/core/response/streamTextPredicted.d.ts +14 -0
  57. package/dist/core/response/streamTextPredicted.mjs +21 -0
  58. package/dist/core/serverSentEvents.cjs +25 -77
  59. package/dist/core/serverSentEvents.d.ts +4 -16
  60. package/dist/core/serverSentEvents.mjs +25 -77
  61. package/dist/core/steps/handler.d.ts +15 -5
  62. package/dist/core/stream.cjs +73 -0
  63. package/dist/core/stream.d.ts +19 -0
  64. package/dist/core/stream.mjs +73 -0
  65. package/dist/interfaces/node/bodyReaders/formData/index.cjs +1 -1
  66. package/dist/interfaces/node/bodyReaders/formData/index.mjs +1 -1
  67. package/dist/interfaces/node/hooks/index.cjs +21 -1
  68. package/dist/interfaces/node/hooks/index.mjs +21 -1
  69. package/dist/interfaces/node/index.cjs +6 -6
  70. package/dist/interfaces/node/index.mjs +2 -2
  71. package/dist/plugins/codeGenerator/aggregateStepContract.cjs +12 -0
  72. package/dist/plugins/codeGenerator/aggregateStepContract.mjs +13 -1
  73. package/dist/plugins/cookie/hooks/cookieHooks.cjs +15 -0
  74. package/dist/plugins/cookie/hooks/cookieHooks.d.ts +11 -0
  75. package/dist/plugins/cookie/hooks/cookieHooks.mjs +13 -0
  76. package/dist/plugins/cookie/hooks/index.cjs +11 -0
  77. package/dist/plugins/cookie/hooks/index.d.ts +3 -0
  78. package/dist/plugins/cookie/hooks/index.mjs +3 -0
  79. package/dist/plugins/cookie/hooks/parseRequestCookie.cjs +20 -0
  80. package/dist/plugins/cookie/hooks/parseRequestCookie.d.ts +8 -0
  81. package/dist/plugins/cookie/hooks/parseRequestCookie.mjs +18 -0
  82. package/dist/plugins/cookie/hooks/serializeResponseCookie.cjs +17 -0
  83. package/dist/plugins/cookie/hooks/serializeResponseCookie.d.ts +7 -0
  84. package/dist/plugins/cookie/hooks/serializeResponseCookie.mjs +15 -0
  85. package/dist/plugins/cookie/index.cjs +25 -0
  86. package/dist/plugins/cookie/index.d.ts +6 -0
  87. package/dist/plugins/cookie/index.mjs +9 -0
  88. package/dist/plugins/cookie/kind.cjs +9 -0
  89. package/dist/plugins/cookie/kind.d.ts +6 -0
  90. package/dist/plugins/cookie/kind.mjs +7 -0
  91. package/dist/plugins/cookie/metadata.cjs +8 -0
  92. package/dist/plugins/cookie/metadata.d.ts +1 -0
  93. package/dist/plugins/cookie/metadata.mjs +6 -0
  94. package/dist/plugins/cookie/override.cjs +30 -0
  95. package/dist/plugins/cookie/override.d.ts +16 -0
  96. package/dist/plugins/cookie/override.mjs +28 -0
  97. package/dist/plugins/cookie/parser.cjs +84 -0
  98. package/dist/plugins/cookie/parser.d.ts +2 -0
  99. package/dist/plugins/cookie/parser.mjs +79 -0
  100. package/dist/plugins/cookie/plugin.cjs +30 -0
  101. package/dist/plugins/cookie/plugin.d.ts +8 -0
  102. package/dist/plugins/cookie/plugin.mjs +28 -0
  103. package/dist/plugins/cookie/serialize.cjs +84 -0
  104. package/dist/plugins/cookie/serialize.d.ts +29 -0
  105. package/dist/plugins/cookie/serialize.mjs +81 -0
  106. package/dist/plugins/openApiGenerator/aggregateStepContract.d.ts +1 -1
  107. package/dist/plugins/openApiGenerator/makeOpenApiRoute.d.ts +1 -1
  108. package/dist/plugins/openApiGenerator/routeToOpenApi.cjs +55 -2
  109. package/dist/plugins/openApiGenerator/routeToOpenApi.mjs +55 -2
  110. package/dist/plugins/openApiGenerator/types/endpointResponse.d.ts +4 -1
  111. package/dist/plugins/static/makeRouteFile.d.ts +1 -1
  112. package/dist/plugins/static/makeRouteFolder.d.ts +1 -1
  113. package/package.json +10 -5
@@ -1,94 +1,42 @@
1
1
  import { stringToMillisecond } from '@duplojs/utils';
2
+ import { Stream } from './stream.mjs';
2
3
 
3
4
  /* eslint-disable @typescript-eslint/prefer-for-of */
4
5
  var ServerSentEvents;
5
6
  (function (ServerSentEvents) {
6
7
  const regexServerSentEventSplitStringData = /\r\n|\r|\n/;
7
8
  const nullIdRegexp = /\0|\n|\r/;
8
- function init(response, initParams) {
9
- const abortSubscribers = [];
10
- let isAbort = false;
11
- const handler = {
12
- async start(send, close) {
13
- if (isAbort) {
14
- return;
9
+ function init(startSendingEvents, initParams) {
10
+ return Stream.init((streamParams) => startSendingEvents({
11
+ ...streamParams,
12
+ lastId: initParams.lastId,
13
+ send: (event, data, params) => {
14
+ if (streamParams.isClose()) {
15
+ return Promise.resolve();
15
16
  }
16
- let isClose = false;
17
- const closeSubscribers = [];
18
- const errorSubscribers = [];
19
- const params = {
20
- send: (event, data, params) => {
21
- if (isClose) {
22
- return Promise.resolve();
23
- }
24
- else if (isAbort) {
25
- return Promise.resolve();
26
- }
27
- let content = `event: ${event || "message"}\n`;
28
- if (typeof data === "string") {
29
- const splitData = data.split(regexServerSentEventSplitStringData);
30
- for (let index = 0; index < splitData.length; index++) {
31
- content += `data: ${splitData[index]}\n`;
32
- }
33
- }
34
- else if (data !== undefined) {
35
- content += `content-type: application/json\ndata: ${JSON.stringify(data)}\n`;
36
- }
37
- if (typeof params?.id === "string" && !nullIdRegexp.test(params.id)) {
38
- content += `id: ${params.id}\n`;
39
- }
40
- if (params?.retry !== undefined) {
41
- content += `retry: ${stringToMillisecond(params.retry)}\n`;
42
- }
43
- content += "\n";
44
- return send(content);
45
- },
46
- abort: handler.abort,
47
- isAbort: () => isAbort,
48
- onAbort: (theFunction) => void abortSubscribers.push(theFunction),
49
- close: () => {
50
- if (isClose === true) {
51
- return;
52
- }
53
- isClose = true;
54
- close();
55
- for (let index = 0; index < closeSubscribers.length; index++) {
56
- closeSubscribers[index]();
57
- }
58
- },
59
- isClose: () => isClose,
60
- onClose: (theFunction) => void closeSubscribers.push(theFunction),
61
- error: (error) => {
62
- for (let index = 0; index < errorSubscribers.length; index++) {
63
- errorSubscribers[index](error);
64
- }
65
- },
66
- onError: (theFunction) => void errorSubscribers.push(theFunction),
67
- lastId: initParams.lastId && !nullIdRegexp.test(initParams.lastId)
68
- ? initParams.lastId
69
- : null,
70
- };
71
- try {
72
- await response.startSendingEvents(params);
17
+ else if (streamParams.isAbort()) {
18
+ return Promise.resolve();
73
19
  }
74
- catch (error) {
75
- params.error(error);
20
+ let content = `event: ${event || "message"}\n`;
21
+ if (typeof data === "string") {
22
+ const splitData = data.split(regexServerSentEventSplitStringData);
23
+ for (let index = 0; index < splitData.length; index++) {
24
+ content += `data: ${splitData[index]}\n`;
25
+ }
76
26
  }
77
- finally {
78
- params.close();
27
+ else if (data !== undefined) {
28
+ content += `content-type: application/json\ndata: ${JSON.stringify(data)}\n`;
79
29
  }
80
- },
81
- abort() {
82
- if (isAbort === true) {
83
- return;
30
+ if (typeof params?.id === "string" && !nullIdRegexp.test(params.id)) {
31
+ content += `id: ${params.id}\n`;
84
32
  }
85
- isAbort = true;
86
- for (let index = 0; index < abortSubscribers.length; index++) {
87
- abortSubscribers[index]();
33
+ if (params?.retry !== undefined) {
34
+ content += `retry: ${stringToMillisecond(params.retry)}\n`;
88
35
  }
36
+ content += "\n";
37
+ return streamParams.send(content);
89
38
  },
90
- };
91
- return handler;
39
+ }));
92
40
  }
93
41
  ServerSentEvents.init = init;
94
42
  })(ServerSentEvents || (ServerSentEvents = {}));
@@ -1,19 +1,29 @@
1
1
  import { type MaybePromise, type Kind, type MaybeArray } from "@duplojs/utils";
2
2
  import { type StepKind } from "./kind";
3
3
  import { type Floor } from "../floor";
4
- import { type ServerSentEventsPredictedResponse, type PredictedResponse, type ResponseContract } from "../response";
4
+ import { type ServerSentEventsPredictedResponse, type PredictedResponse, type ResponseContract, type PredictedResponses, type StreamPredictedResponse, type StreamTextPredictedResponse } from "../response";
5
5
  import { type StepFunctionParams } from "./types";
6
6
  import { type Metadata } from "../metadata";
7
- interface HandlerStepFunctionParamsServerSentEventsResponse<GenericResponse extends ServerSentEventsPredictedResponse> {
7
+ export interface HandlerStepFunctionParamsServerSentEventsResponse<GenericResponse extends ServerSentEventsPredictedResponse> {
8
8
  serverSentEventsResponse<GenericInformation extends GenericResponse["information"], GenericFilteredResponse extends Extract<GenericResponse, {
9
9
  information: GenericInformation;
10
10
  }>>(information: GenericInformation, startSendingEvents: GenericFilteredResponse["startSendingEvents"]): GenericFilteredResponse;
11
11
  }
12
- export interface HandlerStepFunctionParams<GenericResponse extends PredictedResponse | ServerSentEventsPredictedResponse = PredictedResponse | ServerSentEventsPredictedResponse> extends StepFunctionParams<Extract<GenericResponse, PredictedResponse>>, HandlerStepFunctionParamsServerSentEventsResponse<Extract<GenericResponse, ServerSentEventsPredictedResponse>> {
12
+ interface HandlerStepFunctionParamsStreamResponse<GenericResponse extends StreamPredictedResponse> {
13
+ streamResponse<GenericInformation extends GenericResponse["information"], GenericFilteredResponse extends Extract<GenericResponse, {
14
+ information: GenericInformation;
15
+ }>>(information: GenericInformation, startStream: GenericFilteredResponse["startStream"]): GenericFilteredResponse;
16
+ }
17
+ interface HandlerStepFunctionParamsStreamTextResponse<GenericResponse extends StreamTextPredictedResponse> {
18
+ streamTextResponse<GenericInformation extends GenericResponse["information"], GenericFilteredResponse extends Extract<GenericResponse, {
19
+ information: GenericInformation;
20
+ }>>(information: GenericInformation, startStream: GenericFilteredResponse["startStream"]): GenericFilteredResponse;
21
+ }
22
+ export interface HandlerStepFunctionParams<GenericResponse extends PredictedResponses = PredictedResponses> extends StepFunctionParams<Extract<GenericResponse, PredictedResponse>>, HandlerStepFunctionParamsServerSentEventsResponse<Extract<GenericResponse, ServerSentEventsPredictedResponse>>, HandlerStepFunctionParamsStreamResponse<Extract<GenericResponse, StreamPredictedResponse>>, HandlerStepFunctionParamsStreamTextResponse<Extract<GenericResponse, StreamTextPredictedResponse>> {
13
23
  }
14
24
  export interface HandlerStepDefinition {
15
- theFunction(floor: Floor, params: HandlerStepFunctionParams): MaybePromise<PredictedResponse | ServerSentEventsPredictedResponse>;
16
- readonly responseContract: MaybeArray<ResponseContract.Contract | ResponseContract.ServerSentEventsContract>;
25
+ theFunction(floor: Floor, params: HandlerStepFunctionParams): MaybePromise<PredictedResponses>;
26
+ readonly responseContract: MaybeArray<ResponseContract.Contracts>;
17
27
  readonly metadata: readonly Metadata[];
18
28
  }
19
29
  export declare const handlerStepKind: import("@duplojs/utils").KindHandler<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/handler-step", unknown>>;
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ exports.Stream = void 0;
4
+ (function (Stream) {
5
+ function init(startStream) {
6
+ const abortSubscribers = [];
7
+ let isAbort = false;
8
+ const handler = {
9
+ async start(send, close) {
10
+ if (isAbort) {
11
+ return;
12
+ }
13
+ let isClose = false;
14
+ const closeSubscribers = [];
15
+ const errorSubscribers = [];
16
+ const params = {
17
+ send: async (...args) => {
18
+ if (isClose) {
19
+ return Promise.resolve();
20
+ }
21
+ else if (isAbort) {
22
+ return Promise.resolve();
23
+ }
24
+ for (let index = 0; index < args.length; index++) {
25
+ await send(args[index]);
26
+ }
27
+ },
28
+ abort: handler.abort,
29
+ isAbort: () => isAbort,
30
+ onAbort: (theFunction) => void abortSubscribers.push(theFunction),
31
+ close: () => {
32
+ if (isClose === true) {
33
+ return;
34
+ }
35
+ isClose = true;
36
+ close();
37
+ for (let index = 0; index < closeSubscribers.length; index++) {
38
+ closeSubscribers[index]();
39
+ }
40
+ },
41
+ isClose: () => isClose,
42
+ onClose: (theFunction) => void closeSubscribers.push(theFunction),
43
+ error: (error) => {
44
+ for (let index = 0; index < errorSubscribers.length; index++) {
45
+ errorSubscribers[index](error);
46
+ }
47
+ },
48
+ onError: (theFunction) => void errorSubscribers.push(theFunction),
49
+ };
50
+ try {
51
+ await startStream(params);
52
+ }
53
+ catch (error) {
54
+ params.error(error);
55
+ }
56
+ finally {
57
+ params.close();
58
+ }
59
+ },
60
+ abort() {
61
+ if (isAbort === true) {
62
+ return;
63
+ }
64
+ isAbort = true;
65
+ for (let index = 0; index < abortSubscribers.length; index++) {
66
+ abortSubscribers[index]();
67
+ }
68
+ },
69
+ };
70
+ return handler;
71
+ }
72
+ Stream.init = init;
73
+ })(exports.Stream || (exports.Stream = {}));
@@ -0,0 +1,19 @@
1
+ import { type MaybePromise, type AnyTuple } from "@duplojs/utils";
2
+ export declare namespace Stream {
3
+ interface StartSendingParams<GenericFlux extends unknown = unknown> {
4
+ send(...args: AnyTuple<GenericFlux>): Promise<void>;
5
+ abort(): void;
6
+ onAbort(theFunction: () => void): void;
7
+ isAbort(): boolean;
8
+ close(): void;
9
+ onClose(theFunction: () => void): void;
10
+ isClose(): boolean;
11
+ error(error: unknown): void;
12
+ onError(theFunction: (error: unknown) => void): void;
13
+ }
14
+ interface Handler {
15
+ start(send: (value: unknown) => Promise<void>, close: () => void): Promise<void>;
16
+ abort(): void;
17
+ }
18
+ function init(startStream: (params: StartSendingParams) => MaybePromise<void>): Handler;
19
+ }
@@ -0,0 +1,73 @@
1
+ var Stream;
2
+ (function (Stream) {
3
+ function init(startStream) {
4
+ const abortSubscribers = [];
5
+ let isAbort = false;
6
+ const handler = {
7
+ async start(send, close) {
8
+ if (isAbort) {
9
+ return;
10
+ }
11
+ let isClose = false;
12
+ const closeSubscribers = [];
13
+ const errorSubscribers = [];
14
+ const params = {
15
+ send: async (...args) => {
16
+ if (isClose) {
17
+ return Promise.resolve();
18
+ }
19
+ else if (isAbort) {
20
+ return Promise.resolve();
21
+ }
22
+ for (let index = 0; index < args.length; index++) {
23
+ await send(args[index]);
24
+ }
25
+ },
26
+ abort: handler.abort,
27
+ isAbort: () => isAbort,
28
+ onAbort: (theFunction) => void abortSubscribers.push(theFunction),
29
+ close: () => {
30
+ if (isClose === true) {
31
+ return;
32
+ }
33
+ isClose = true;
34
+ close();
35
+ for (let index = 0; index < closeSubscribers.length; index++) {
36
+ closeSubscribers[index]();
37
+ }
38
+ },
39
+ isClose: () => isClose,
40
+ onClose: (theFunction) => void closeSubscribers.push(theFunction),
41
+ error: (error) => {
42
+ for (let index = 0; index < errorSubscribers.length; index++) {
43
+ errorSubscribers[index](error);
44
+ }
45
+ },
46
+ onError: (theFunction) => void errorSubscribers.push(theFunction),
47
+ };
48
+ try {
49
+ await startStream(params);
50
+ }
51
+ catch (error) {
52
+ params.error(error);
53
+ }
54
+ finally {
55
+ params.close();
56
+ }
57
+ },
58
+ abort() {
59
+ if (isAbort === true) {
60
+ return;
61
+ }
62
+ isAbort = true;
63
+ for (let index = 0; index < abortSubscribers.length; index++) {
64
+ abortSubscribers[index]();
65
+ }
66
+ },
67
+ };
68
+ return handler;
69
+ }
70
+ Stream.init = init;
71
+ })(Stream || (Stream = {}));
72
+
73
+ export { Stream };
@@ -102,7 +102,7 @@ function createFormDataBodyReaderImplementation(serverParams) {
102
102
  }
103
103
  return result;
104
104
  }
105
- if (request.headers["content-type-options"]?.includes("advanced")) {
105
+ if (request.headers["x-duplojs-body-options"]?.includes("advanced")) {
106
106
  return utils.E.success(utils.TheFormData.fromEntries(result.entries(), params.maxIndexArray));
107
107
  }
108
108
  return utils.E.success(utils.O.fromEntries(result.entries()));
@@ -100,7 +100,7 @@ function createFormDataBodyReaderImplementation(serverParams) {
100
100
  }
101
101
  return result;
102
102
  }
103
- if (request.headers["content-type-options"]?.includes("advanced")) {
103
+ if (request.headers["x-duplojs-body-options"]?.includes("advanced")) {
104
104
  return E.success(TheFormData.fromEntries(result.entries(), params.maxIndexArray));
105
105
  }
106
106
  return E.success(O.fromEntries(result.entries()));
@@ -3,11 +3,14 @@
3
3
  require('../../../core/response/index.cjs');
4
4
  require('../../../core/route/index.cjs');
5
5
  var serverSentEvents = require('../../../core/serverSentEvents.cjs');
6
+ var stream = require('../../../core/stream.cjs');
6
7
  var serverUtils = require('@duplojs/server-utils');
7
8
  var utils = require('@duplojs/utils');
8
9
  var node_fs = require('node:fs');
9
10
  var hooks = require('../../../core/route/hooks.cjs');
10
11
  var serverSentEventsPredicted = require('../../../core/response/serverSentEventsPredicted.cjs');
12
+ var streamPredicted = require('../../../core/response/streamPredicted.cjs');
13
+ var streamTextPredicted = require('../../../core/response/streamTextPredicted.cjs');
11
14
 
12
15
  function initNodeHook(hub, serverParams) {
13
16
  const isDev = hub.config.environment === "DEV";
@@ -19,7 +22,7 @@ function initNodeHook(hub, serverParams) {
19
22
  async sendResponse({ request, currentResponse, exit }) {
20
23
  const { response: rawResponse, request: rawRequest } = request.raw;
21
24
  if (currentResponse instanceof serverSentEventsPredicted.ServerSentEventsPredictedResponse) {
22
- const handler = serverSentEvents.ServerSentEvents.init(currentResponse, {
25
+ const handler = serverSentEvents.ServerSentEvents.init(currentResponse.startSendingEvents, {
23
26
  lastId: typeof request.headers["last-event-id"] === "string"
24
27
  ? request.headers["last-event-id"]
25
28
  : null,
@@ -35,6 +38,23 @@ function initNodeHook(hub, serverParams) {
35
38
  }), () => void rawResponse.end());
36
39
  return exit();
37
40
  }
41
+ else if (currentResponse instanceof streamPredicted.StreamPredictedResponse
42
+ || currentResponse instanceof streamTextPredicted.StreamTextPredictedResponse) {
43
+ const handler = stream.Stream.init(currentResponse.startStream);
44
+ rawRequest.on("close", handler.abort);
45
+ void handler.start((value) => new Promise((resolve) => {
46
+ if (!rawResponse.write((value instanceof Buffer
47
+ || value instanceof Uint8Array)
48
+ ? value
49
+ : String(value))) {
50
+ rawResponse.once("drain", resolve);
51
+ }
52
+ else {
53
+ resolve();
54
+ }
55
+ }), () => void rawResponse.end());
56
+ return exit();
57
+ }
38
58
  const body = currentResponse.body;
39
59
  if (body instanceof Error) {
40
60
  rawResponse.write(body.toString());
@@ -1,11 +1,14 @@
1
1
  import '../../../core/response/index.mjs';
2
2
  import '../../../core/route/index.mjs';
3
3
  import { ServerSentEvents } from '../../../core/serverSentEvents.mjs';
4
+ import { Stream } from '../../../core/stream.mjs';
4
5
  import { SF } from '@duplojs/server-utils';
5
6
  import { A } from '@duplojs/utils';
6
7
  import { createReadStream } from 'node:fs';
7
8
  import { createHookRouteLifeCycle } from '../../../core/route/hooks.mjs';
8
9
  import { ServerSentEventsPredictedResponse } from '../../../core/response/serverSentEventsPredicted.mjs';
10
+ import { StreamPredictedResponse } from '../../../core/response/streamPredicted.mjs';
11
+ import { StreamTextPredictedResponse } from '../../../core/response/streamTextPredicted.mjs';
9
12
 
10
13
  function initNodeHook(hub, serverParams) {
11
14
  const isDev = hub.config.environment === "DEV";
@@ -17,7 +20,7 @@ function initNodeHook(hub, serverParams) {
17
20
  async sendResponse({ request, currentResponse, exit }) {
18
21
  const { response: rawResponse, request: rawRequest } = request.raw;
19
22
  if (currentResponse instanceof ServerSentEventsPredictedResponse) {
20
- const handler = ServerSentEvents.init(currentResponse, {
23
+ const handler = ServerSentEvents.init(currentResponse.startSendingEvents, {
21
24
  lastId: typeof request.headers["last-event-id"] === "string"
22
25
  ? request.headers["last-event-id"]
23
26
  : null,
@@ -33,6 +36,23 @@ function initNodeHook(hub, serverParams) {
33
36
  }), () => void rawResponse.end());
34
37
  return exit();
35
38
  }
39
+ else if (currentResponse instanceof StreamPredictedResponse
40
+ || currentResponse instanceof StreamTextPredictedResponse) {
41
+ const handler = Stream.init(currentResponse.startStream);
42
+ rawRequest.on("close", handler.abort);
43
+ void handler.start((value) => new Promise((resolve) => {
44
+ if (!rawResponse.write((value instanceof Buffer
45
+ || value instanceof Uint8Array)
46
+ ? value
47
+ : String(value))) {
48
+ rawResponse.once("drain", resolve);
49
+ }
50
+ else {
51
+ resolve();
52
+ }
53
+ }), () => void rawResponse.end());
54
+ return exit();
55
+ }
36
56
  const body = currentResponse.body;
37
57
  if (body instanceof Error) {
38
58
  rawResponse.write(body.toString());
@@ -3,21 +3,21 @@
3
3
  require('./types/index.cjs');
4
4
  var kind = require('./kind.cjs');
5
5
  var createHttpServer = require('./createHttpServer.cjs');
6
- var index = require('./hooks/index.cjs');
6
+ var index$2 = require('./hooks/index.cjs');
7
7
  require('./bodyReaders/index.cjs');
8
8
  var error = require('./bodyReaders/formData/error.cjs');
9
+ var index = require('./bodyReaders/formData/index.cjs');
10
+ var index$1 = require('./bodyReaders/text/index.cjs');
9
11
  var readRequestFormData = require('./bodyReaders/formData/readRequestFormData.cjs');
10
- var index$1 = require('./bodyReaders/formData/index.cjs');
11
12
  var readRequestText = require('./bodyReaders/text/readRequestText.cjs');
12
- var index$2 = require('./bodyReaders/text/index.cjs');
13
13
 
14
14
 
15
15
 
16
16
  exports.createInterfacesNodeLibKind = kind.createInterfacesNodeLibKind;
17
17
  exports.createHttpServer = createHttpServer.createHttpServer;
18
- exports.initNodeHook = index.initNodeHook;
18
+ exports.initNodeHook = index$2.initNodeHook;
19
19
  exports.BodyParseFormDataError = error.BodyParseFormDataError;
20
+ exports.createFormDataBodyReaderImplementation = index.createFormDataBodyReaderImplementation;
21
+ exports.createTextBodyReaderImplementation = index$1.createTextBodyReaderImplementation;
20
22
  exports.readRequestFormData = readRequestFormData.readRequestFormData;
21
- exports.createFormDataBodyReaderImplementation = index$1.createFormDataBodyReaderImplementation;
22
23
  exports.readRequestText = readRequestText.readRequestText;
23
- exports.createTextBodyReaderImplementation = index$2.createTextBodyReaderImplementation;
@@ -4,7 +4,7 @@ export { createHttpServer } from './createHttpServer.mjs';
4
4
  export { initNodeHook } from './hooks/index.mjs';
5
5
  import './bodyReaders/index.mjs';
6
6
  export { BodyParseFormDataError } from './bodyReaders/formData/error.mjs';
7
- export { readRequestFormData } from './bodyReaders/formData/readRequestFormData.mjs';
8
7
  export { createFormDataBodyReaderImplementation } from './bodyReaders/formData/index.mjs';
9
- export { readRequestText } from './bodyReaders/text/readRequestText.mjs';
10
8
  export { createTextBodyReaderImplementation } from './bodyReaders/text/index.mjs';
9
+ export { readRequestFormData } from './bodyReaders/formData/readRequestFormData.mjs';
10
+ export { readRequestText } from './bodyReaders/text/readRequestText.mjs';
@@ -4,6 +4,7 @@ require('../../core/steps/index.cjs');
4
4
  var utils = require('@duplojs/utils');
5
5
  require('../../core/response/index.cjs');
6
6
  var metadata = require('./metadata.cjs');
7
+ var typescript = require('typescript');
7
8
  var identifier = require('../../core/steps/identifier.cjs');
8
9
  var process = require('../../core/steps/process.cjs');
9
10
  var extract = require('../../core/steps/extract.cjs');
@@ -13,6 +14,7 @@ var cut = require('../../core/steps/cut.cjs');
13
14
  var handler = require('../../core/steps/handler.cjs');
14
15
  var contract = require('../../core/response/contract.cjs');
15
16
 
17
+ const defaultFluxStreamSchema = utils.DP.unknown().setOverrideTypescriptTransformer(typescript.factory.createTypeReferenceNode("Uint8Array", [typescript.factory.createTypeReferenceNode("ArrayBuffer")]));
16
18
  function aggregateStepContract(steps, params) {
17
19
  const filteredStep = utils.A.filter(steps, (step) => utils.A.find(step.definition.metadata, metadata.IgnoreByCodeGeneratorMetadata.is) === undefined);
18
20
  const processContracts = utils.pipe(filteredStep, utils.A.filter(identifier.stepIdentifier(process.processStepKind)), utils.A.filter((step) => utils.A.find(step.definition.process.definition.metadata, metadata.IgnoreByCodeGeneratorMetadata.is) === undefined), utils.A.map((element) => aggregateStepContract(element.definition.process.definition.steps, params)), utils.O.to({
@@ -66,6 +68,16 @@ function aggregateStepContract(steps, params) {
66
68
  information: utils.DP.literal(information),
67
69
  body,
68
70
  events: utils.DP.object(events),
71
+ })), utils.P.when(contract.ResponseContract.streamContractKind.has, ({ code, information, body }) => utils.DP.object({
72
+ code: utils.DP.literal(code),
73
+ information: utils.DP.literal(information),
74
+ body,
75
+ flux: defaultFluxStreamSchema,
76
+ })), utils.P.when(contract.ResponseContract.streamTextContractKind.has, ({ code, information, body, flux }) => utils.DP.object({
77
+ code: utils.DP.literal(code),
78
+ information: utils.DP.literal(information),
79
+ body,
80
+ flux,
69
81
  })), utils.P.exhaustive)), utils.A.concat(processContracts.endpointContract));
70
82
  return {
71
83
  entrypointContract,
@@ -1,7 +1,8 @@
1
1
  import '../../core/steps/index.mjs';
2
- import { A, pipe, O, DP, P, hasSomeKinds, innerPipe } from '@duplojs/utils';
2
+ import { DP, A, pipe, O, P, hasSomeKinds, innerPipe } from '@duplojs/utils';
3
3
  import '../../core/response/index.mjs';
4
4
  import { IgnoreByCodeGeneratorMetadata } from './metadata.mjs';
5
+ import { factory } from 'typescript';
5
6
  import { stepIdentifier } from '../../core/steps/identifier.mjs';
6
7
  import { processStepKind } from '../../core/steps/process.mjs';
7
8
  import { extractStepKind } from '../../core/steps/extract.mjs';
@@ -11,6 +12,7 @@ import { cutStepKind } from '../../core/steps/cut.mjs';
11
12
  import { handlerStepKind } from '../../core/steps/handler.mjs';
12
13
  import { ResponseContract } from '../../core/response/contract.mjs';
13
14
 
15
+ const defaultFluxStreamSchema = DP.unknown().setOverrideTypescriptTransformer(factory.createTypeReferenceNode("Uint8Array", [factory.createTypeReferenceNode("ArrayBuffer")]));
14
16
  function aggregateStepContract(steps, params) {
15
17
  const filteredStep = A.filter(steps, (step) => A.find(step.definition.metadata, IgnoreByCodeGeneratorMetadata.is) === undefined);
16
18
  const processContracts = pipe(filteredStep, A.filter(stepIdentifier(processStepKind)), A.filter((step) => A.find(step.definition.process.definition.metadata, IgnoreByCodeGeneratorMetadata.is) === undefined), A.map((element) => aggregateStepContract(element.definition.process.definition.steps, params)), O.to({
@@ -64,6 +66,16 @@ function aggregateStepContract(steps, params) {
64
66
  information: DP.literal(information),
65
67
  body,
66
68
  events: DP.object(events),
69
+ })), P.when(ResponseContract.streamContractKind.has, ({ code, information, body }) => DP.object({
70
+ code: DP.literal(code),
71
+ information: DP.literal(information),
72
+ body,
73
+ flux: defaultFluxStreamSchema,
74
+ })), P.when(ResponseContract.streamTextContractKind.has, ({ code, information, body, flux }) => DP.object({
75
+ code: DP.literal(code),
76
+ information: DP.literal(information),
77
+ body,
78
+ flux,
67
79
  })), P.exhaustive)), A.concat(processContracts.endpointContract));
68
80
  return {
69
81
  entrypointContract,
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ var parser = require('../parser.cjs');
4
+ var serialize = require('../serialize.cjs');
5
+ var parseRequestCookie = require('./parseRequestCookie.cjs');
6
+ var serializeResponseCookie = require('./serializeResponseCookie.cjs');
7
+
8
+ function cookieHooks({ parser: parser$1 = parser.defaultParser, serializer = serialize.defaultSerializer, } = {}) {
9
+ return {
10
+ ...parseRequestCookie.parseRequestCookieHook({ parser: parser$1 }),
11
+ ...serializeResponseCookie.serializeResponseCookieHook({ serializer }),
12
+ };
13
+ }
14
+
15
+ exports.cookieHooks = cookieHooks;
@@ -0,0 +1,11 @@
1
+ import { type Parser } from "../parser";
2
+ import { type Serializer } from "../serialize";
3
+ interface CookieHooksParams {
4
+ parser?: Parser;
5
+ serializer?: Serializer;
6
+ }
7
+ export declare function cookieHooks({ parser, serializer, }?: CookieHooksParams): {
8
+ beforeSendResponse: ({ currentResponse, next }: import("../../../core/route").RouteHookParamsAfter) => import("../../../core/route").RouteHookNext;
9
+ beforeRouteExecution: ({ request, next }: import("../../../core/route").RouteHookParams) => import("../../../core/route").RouteHookNext;
10
+ };
11
+ export {};
@@ -0,0 +1,13 @@
1
+ import { defaultParser } from '../parser.mjs';
2
+ import { defaultSerializer } from '../serialize.mjs';
3
+ import { parseRequestCookieHook } from './parseRequestCookie.mjs';
4
+ import { serializeResponseCookieHook } from './serializeResponseCookie.mjs';
5
+
6
+ function cookieHooks({ parser = defaultParser, serializer = defaultSerializer, } = {}) {
7
+ return {
8
+ ...parseRequestCookieHook({ parser }),
9
+ ...serializeResponseCookieHook({ serializer }),
10
+ };
11
+ }
12
+
13
+ export { cookieHooks };
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ var parseRequestCookie = require('./parseRequestCookie.cjs');
4
+ var serializeResponseCookie = require('./serializeResponseCookie.cjs');
5
+ var cookieHooks = require('./cookieHooks.cjs');
6
+
7
+
8
+
9
+ exports.parseRequestCookieHook = parseRequestCookie.parseRequestCookieHook;
10
+ exports.serializeResponseCookieHook = serializeResponseCookie.serializeResponseCookieHook;
11
+ exports.cookieHooks = cookieHooks.cookieHooks;
@@ -0,0 +1,3 @@
1
+ export * from "./parseRequestCookie";
2
+ export * from "./serializeResponseCookie";
3
+ export * from "./cookieHooks";
@@ -0,0 +1,3 @@
1
+ export { parseRequestCookieHook } from './parseRequestCookie.mjs';
2
+ export { serializeResponseCookieHook } from './serializeResponseCookie.mjs';
3
+ export { cookieHooks } from './cookieHooks.mjs';
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ require('../../../core/route/index.cjs');
4
+ var hooks = require('../../../core/route/hooks.cjs');
5
+
6
+ function parseRequestCookieHook(params) {
7
+ return hooks.createHookRouteLifeCycle({
8
+ beforeRouteExecution: ({ request, next }) => {
9
+ if (request.headers.cookie) {
10
+ const cookieValue = Array.isArray(request.headers.cookie)
11
+ ? request.headers.cookie.join("; ")
12
+ : request.headers.cookie;
13
+ request.cookies = params.parser(cookieValue);
14
+ }
15
+ return next();
16
+ },
17
+ });
18
+ }
19
+
20
+ exports.parseRequestCookieHook = parseRequestCookieHook;
@@ -0,0 +1,8 @@
1
+ import { type Parser } from "../parser";
2
+ interface ParseRequestCookieHookParams {
3
+ parser: Parser;
4
+ }
5
+ export declare function parseRequestCookieHook(params: ParseRequestCookieHookParams): {
6
+ readonly beforeRouteExecution: ({ request, next }: import("../../../core/route").RouteHookParams) => import("../../../core/route").RouteHookNext;
7
+ };
8
+ export {};