@outfoxx/sunday 1.0.9 → 1.1.0-alpha.11

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 (179) hide show
  1. package/LICENSE.txt +203 -0
  2. package/README.md +35 -2
  3. package/dist/any-type.d.ts +2 -0
  4. package/dist/any-type.js +13 -0
  5. package/dist/any-type.js.map +1 -1
  6. package/dist/class-type.d.ts +2 -2
  7. package/dist/class-type.js +13 -0
  8. package/dist/class-type.js.map +1 -1
  9. package/dist/date-time-types.d.ts +2 -7
  10. package/dist/date-time-types.js +15 -4
  11. package/dist/date-time-types.js.map +1 -1
  12. package/dist/event-parser.d.ts +17 -0
  13. package/dist/event-parser.js +153 -0
  14. package/dist/event-parser.js.map +1 -0
  15. package/dist/fetch-event-source.d.ts +12 -10
  16. package/dist/fetch-event-source.js +154 -138
  17. package/dist/fetch-event-source.js.map +1 -1
  18. package/dist/fetch-request-factory.d.ts +10 -5
  19. package/dist/fetch-request-factory.js +100 -71
  20. package/dist/fetch-request-factory.js.map +1 -1
  21. package/dist/fetch.d.ts +7 -1
  22. package/dist/fetch.js +74 -8
  23. package/dist/fetch.js.map +1 -1
  24. package/dist/header-parameters.d.ts +3 -0
  25. package/dist/header-parameters.js +53 -0
  26. package/dist/header-parameters.js.map +1 -0
  27. package/dist/index.d.ts +12 -12
  28. package/dist/index.js +25 -12
  29. package/dist/index.js.map +1 -1
  30. package/dist/logger.js +13 -0
  31. package/dist/logger.js.map +1 -1
  32. package/dist/{binary-decoder.d.ts → media-type-codecs/binary-decoder.d.ts} +2 -2
  33. package/dist/media-type-codecs/binary-decoder.js +29 -0
  34. package/dist/media-type-codecs/binary-decoder.js.map +1 -0
  35. package/dist/{binary-encoder.d.ts → media-type-codecs/binary-encoder.d.ts} +0 -0
  36. package/dist/media-type-codecs/binary-encoder.js +26 -0
  37. package/dist/media-type-codecs/binary-encoder.js.map +1 -0
  38. package/dist/media-type-codecs/cbor-decoder.d.ts +39 -0
  39. package/dist/media-type-codecs/cbor-decoder.js +426 -0
  40. package/dist/media-type-codecs/cbor-decoder.js.map +1 -0
  41. package/dist/media-type-codecs/cbor-encoder.d.ts +43 -0
  42. package/dist/media-type-codecs/cbor-encoder.js +239 -0
  43. package/dist/media-type-codecs/cbor-encoder.js.map +1 -0
  44. package/dist/media-type-codecs/cbor-tags.d.ts +4 -0
  45. package/dist/media-type-codecs/cbor-tags.js +18 -0
  46. package/dist/media-type-codecs/cbor-tags.js.map +1 -0
  47. package/dist/media-type-codecs/json-decoder.d.ts +39 -0
  48. package/dist/media-type-codecs/json-decoder.js +380 -0
  49. package/dist/media-type-codecs/json-decoder.js.map +1 -0
  50. package/dist/media-type-codecs/json-encoder.d.ts +44 -0
  51. package/dist/media-type-codecs/json-encoder.js +276 -0
  52. package/dist/media-type-codecs/json-encoder.js.map +1 -0
  53. package/dist/media-type-codecs/media-type-decoder.d.ts +11 -0
  54. package/dist/media-type-codecs/media-type-decoder.js +19 -0
  55. package/dist/media-type-codecs/media-type-decoder.js.map +1 -0
  56. package/dist/{media-type-decoders.d.ts → media-type-codecs/media-type-decoders.d.ts} +5 -4
  57. package/dist/media-type-codecs/media-type-decoders.js +53 -0
  58. package/dist/media-type-codecs/media-type-decoders.js.map +1 -0
  59. package/dist/{media-type-encoder.d.ts → media-type-codecs/media-type-encoder.d.ts} +5 -1
  60. package/dist/media-type-codecs/media-type-encoder.js +24 -0
  61. package/dist/media-type-codecs/media-type-encoder.js.map +1 -0
  62. package/dist/{media-type-encoders.d.ts → media-type-codecs/media-type-encoders.d.ts} +5 -4
  63. package/dist/media-type-codecs/media-type-encoders.js +55 -0
  64. package/dist/media-type-codecs/media-type-encoders.js.map +1 -0
  65. package/dist/{url-encoder.d.ts → media-type-codecs/www-form-url-encoder.d.ts} +8 -8
  66. package/dist/{url-encoder.js → media-type-codecs/www-form-url-encoder.js} +51 -37
  67. package/dist/media-type-codecs/www-form-url-encoder.js.map +1 -0
  68. package/dist/media-type.d.ts +91 -13
  69. package/dist/media-type.js +286 -15
  70. package/dist/media-type.js.map +1 -1
  71. package/dist/problem.d.ts +24 -9
  72. package/dist/problem.js +153 -6
  73. package/dist/problem.js.map +1 -1
  74. package/dist/request-factory.d.ts +10 -10
  75. package/dist/request-factory.js +13 -0
  76. package/dist/request-factory.js.map +1 -1
  77. package/dist/sunday-error.d.ts +11 -0
  78. package/dist/sunday-error.js +31 -0
  79. package/dist/sunday-error.js.map +1 -0
  80. package/dist/url-template.js +13 -0
  81. package/dist/url-template.js.map +1 -1
  82. package/dist/util/any.d.ts +2 -0
  83. package/dist/util/any.js +24 -0
  84. package/dist/util/any.js.map +1 -0
  85. package/dist/util/base64.js +13 -0
  86. package/dist/util/base64.js.map +1 -1
  87. package/dist/util/error.d.ts +2 -0
  88. package/dist/util/error.js +26 -0
  89. package/dist/util/error.js.map +1 -0
  90. package/dist/util/hex.js +16 -2
  91. package/dist/util/hex.js.map +1 -1
  92. package/dist/util/rxjs.d.ts +3 -0
  93. package/dist/util/rxjs.js +22 -5
  94. package/dist/util/rxjs.js.map +1 -1
  95. package/dist/util/stream-rxjs.js +13 -0
  96. package/dist/util/stream-rxjs.js.map +1 -1
  97. package/dist/util/temporal.d.ts +2 -0
  98. package/dist/util/temporal.js +31 -0
  99. package/dist/util/temporal.js.map +1 -0
  100. package/package.json +29 -22
  101. package/src/any-type.ts +18 -0
  102. package/src/class-type.ts +20 -2
  103. package/src/date-time-types.ts +35 -8
  104. package/src/event-parser.ts +204 -0
  105. package/src/fetch-event-source.ts +163 -159
  106. package/src/fetch-request-factory.ts +142 -100
  107. package/src/fetch.ts +79 -14
  108. package/src/header-parameters.ts +66 -0
  109. package/src/index.ts +26 -12
  110. package/src/logger.ts +14 -0
  111. package/src/media-type-codecs/binary-decoder.ts +38 -0
  112. package/src/media-type-codecs/binary-encoder.ts +33 -0
  113. package/src/media-type-codecs/cbor-decoder.ts +529 -0
  114. package/src/media-type-codecs/cbor-encoder.ts +321 -0
  115. package/src/media-type-codecs/cbor-tags.ts +18 -0
  116. package/src/media-type-codecs/json-decoder.ts +484 -0
  117. package/src/media-type-codecs/json-encoder.ts +342 -0
  118. package/src/media-type-codecs/media-type-decoder.ts +34 -0
  119. package/src/media-type-codecs/media-type-decoders.ts +81 -0
  120. package/src/media-type-codecs/media-type-encoder.ts +45 -0
  121. package/src/media-type-codecs/media-type-encoders.ts +83 -0
  122. package/src/media-type-codecs/www-form-url-encoder.ts +193 -0
  123. package/src/media-type.ts +340 -22
  124. package/src/problem.ts +158 -12
  125. package/src/request-factory.ts +35 -12
  126. package/src/sunday-error.ts +51 -0
  127. package/src/url-template.ts +14 -0
  128. package/src/util/any.ts +24 -0
  129. package/src/util/base64.ts +14 -0
  130. package/src/util/error.ts +28 -0
  131. package/src/util/hex.ts +17 -2
  132. package/src/util/rxjs.ts +30 -5
  133. package/src/util/stream-rxjs.ts +14 -0
  134. package/src/util/temporal.ts +32 -0
  135. package/dist/binary-decoder.js +0 -16
  136. package/dist/binary-decoder.js.map +0 -1
  137. package/dist/binary-encoder.js +0 -13
  138. package/dist/binary-encoder.js.map +0 -1
  139. package/dist/cbor-decoder.d.ts +0 -15
  140. package/dist/cbor-decoder.js +0 -126
  141. package/dist/cbor-decoder.js.map +0 -1
  142. package/dist/cbor-encoder.d.ts +0 -29
  143. package/dist/cbor-encoder.js +0 -81
  144. package/dist/cbor-encoder.js.map +0 -1
  145. package/dist/cbor-tags.d.ts +0 -3
  146. package/dist/cbor-tags.js +0 -4
  147. package/dist/cbor-tags.js.map +0 -1
  148. package/dist/http-error.d.ts +0 -10
  149. package/dist/http-error.js +0 -45
  150. package/dist/http-error.js.map +0 -1
  151. package/dist/json-decoder.d.ts +0 -31
  152. package/dist/json-decoder.js +0 -139
  153. package/dist/json-decoder.js.map +0 -1
  154. package/dist/json-encoder.d.ts +0 -35
  155. package/dist/json-encoder.js +0 -134
  156. package/dist/json-encoder.js.map +0 -1
  157. package/dist/media-type-decoder.d.ts +0 -4
  158. package/dist/media-type-decoder.js +0 -2
  159. package/dist/media-type-decoder.js.map +0 -1
  160. package/dist/media-type-decoders.js +0 -40
  161. package/dist/media-type-decoders.js.map +0 -1
  162. package/dist/media-type-encoder.js +0 -6
  163. package/dist/media-type-encoder.js.map +0 -1
  164. package/dist/media-type-encoders.js +0 -42
  165. package/dist/media-type-encoders.js.map +0 -1
  166. package/dist/url-encoder.js.map +0 -1
  167. package/src/binary-decoder.ts +0 -24
  168. package/src/binary-encoder.ts +0 -19
  169. package/src/cbor-decoder.ts +0 -148
  170. package/src/cbor-encoder.ts +0 -95
  171. package/src/cbor-tags.ts +0 -3
  172. package/src/http-error.ts +0 -55
  173. package/src/json-decoder.ts +0 -164
  174. package/src/json-encoder.ts +0 -162
  175. package/src/media-type-decoder.ts +0 -5
  176. package/src/media-type-decoders.ts +0 -59
  177. package/src/media-type-encoder.ts +0 -16
  178. package/src/media-type-encoders.ts +0 -61
  179. package/src/url-encoder.ts +0 -173
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@outfoxx/sunday",
3
- "version": "1.0.9",
4
- "description": "Sunday - The framework of REST 🙏",
3
+ "version": "v1.1.0-alpha.11",
4
+ "description": "Sunday 🙏 The framework of REST for TypeScript/JavaScript",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "type": "module",
@@ -10,7 +10,7 @@
10
10
  "test:watch": "karma start karma.conf.cjs",
11
11
  "build": "tsc --build tsconfig.dist.json",
12
12
  "clean": "rimraf build dist",
13
- "prepublishOnly": "npm run clean && npm run pretty -- -c && npm run lint && npm run build && npm run test",
13
+ "prepublishOnly": "npm run clean && npm run lint && npm run build && npm run test",
14
14
  "lint": "eslint src test",
15
15
  "pretty": "prettier src test"
16
16
  },
@@ -26,39 +26,46 @@
26
26
  },
27
27
  "keywords": [],
28
28
  "author": "kdubb",
29
- "license": "outfox",
29
+ "license": "Apache-2.0",
30
30
  "dependencies": {
31
+ "@js-joda/core": "^5.2.0",
31
32
  "@outfoxx/cbor-redux": "^0.3.1",
32
- "@outfoxx/jackson-js": "^1.2.20",
33
- "luxon": "^1.25.0",
34
- "rxjs": "^6.6.3",
35
- "uri-template-lite": "^20.5.0"
33
+ "@outfoxx/jackson-js": "^1.2.22",
34
+ "@types/jasmine": "^4.0.3",
35
+ "common-tags": "^1.8.2",
36
+ "tslib": "^2.4.0",
37
+ "uri-template-lite": "^22.1.0"
38
+ },
39
+ "peerDependencies": {
40
+ "rxjs": "^7.5.5"
36
41
  },
37
42
  "devDependencies": {
38
- "@types/eslint": "^7.2.2",
39
- "@types/eslint-plugin-prettier": "^3.1.0",
40
- "@types/jasmine": "^3.5.14",
41
- "@types/luxon": "^1.24.4",
43
+ "@types/common-tags": "^1.8.0",
42
44
  "@types/uri-template-lite": "^19.12.0",
43
- "@typescript-eslint/eslint-plugin": "^4.1.1",
44
- "@typescript-eslint/parser": "^4.1.1",
45
- "eslint": "^7.9.0",
46
- "eslint-config-prettier": "^6.11.0",
47
- "eslint-plugin-prettier": "^3.1.4",
48
- "fetch-mock": "^9.10.7",
45
+ "@typescript-eslint/eslint-plugin": "^5.25.0",
46
+ "@typescript-eslint/parser": "^5.25.0",
47
+ "eslint": "^8.15.0",
48
+ "eslint-config-prettier": "^8.5.0",
49
+ "eslint-plugin-header": "^3.1.1",
50
+ "eslint-plugin-prettier": "^4.0.0",
51
+ "fetch-mock": "^9.11.0",
49
52
  "fsevents": "^2.1.3",
50
53
  "husky": "^4.3.0",
51
- "karma": "^5.2.2",
54
+ "install": "^0.13.0",
55
+ "karma": "^6.3.2",
52
56
  "karma-chrome-launcher": "^3.1.0",
53
57
  "karma-cli": "^2.0.0",
58
+ "karma-coverage": "^2.0.3",
54
59
  "karma-jasmine": "^4.0.1",
55
- "karma-typescript": "^5.2.0",
56
- "karma-typescript-es6-transform": "^5.2.0",
60
+ "karma-source-map-support": "^1.4.0",
61
+ "karma-sourcemap-loader": "^0.3.8",
62
+ "karma-typescript": "^5.5.1",
63
+ "karma-typescript-es6-transform": "^5.5.1",
57
64
  "lint-staged": "^10.4.0",
58
65
  "prettier": "^2.1.2",
59
66
  "pretty-quick": "^3.0.2",
60
67
  "rimraf": "^3.0.2",
61
- "typescript": "^4.0.2",
68
+ "typescript": "~4.4",
62
69
  "web-streams-polyfill": "^3.0.0"
63
70
  },
64
71
  "files": [
package/src/any-type.ts CHANGED
@@ -1,4 +1,22 @@
1
+ // Copyright 2020 Outfox, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
1
15
  import { ClassList, ClassType } from '@outfoxx/jackson-js/dist/@types';
16
+ import { ConstructableClassType } from './class-type';
2
17
 
3
18
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
19
  export type AnyType = ClassList<ClassType<any>>;
20
+
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ export type AnyConstructableType = ClassList<ConstructableClassType<any>>;
package/src/class-type.ts CHANGED
@@ -1,3 +1,21 @@
1
- import { ClassType as CT } from '@outfoxx/jackson-js/dist/@types';
1
+ // Copyright 2020 Outfox, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
2
14
 
3
- export type ClassType<T> = CT<T>;
15
+ export type ClassType<T> =
16
+ | (abstract new () => T) // eslint-disable-line prettier/prettier
17
+ | (abstract new (...args: any[]) => T); // eslint-disable-line @typescript-eslint/no-explicit-any
18
+
19
+ export declare type ConstructableClassType<T> =
20
+ | (new () => T) // eslint-disable-line prettier/prettier
21
+ | (new (...args: any[]) => T); // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -1,10 +1,37 @@
1
- import { DateTime as luxonDateTime } from 'luxon';
1
+ // Copyright 2020 Outfox, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
2
14
 
3
- export type Date = luxonDateTime;
4
- export const Date = luxonDateTime;
15
+ import {
16
+ Duration,
17
+ Instant,
18
+ LocalDate,
19
+ LocalDateTime,
20
+ LocalTime,
21
+ OffsetDateTime,
22
+ OffsetTime,
23
+ Period,
24
+ ZonedDateTime,
25
+ } from '@js-joda/core';
5
26
 
6
- export type Time = luxonDateTime;
7
- export const Time = luxonDateTime;
8
-
9
- export type DateTime = luxonDateTime;
10
- export const DateTime = luxonDateTime;
27
+ export {
28
+ Duration,
29
+ Instant,
30
+ LocalDate,
31
+ LocalTime,
32
+ LocalDateTime,
33
+ OffsetDateTime,
34
+ OffsetTime,
35
+ Period,
36
+ ZonedDateTime,
37
+ };
@@ -0,0 +1,204 @@
1
+ // Copyright 2020 Outfox, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ export interface EventInfo {
16
+ id?: string;
17
+ event?: string;
18
+ data?: string;
19
+ retry?: string;
20
+ }
21
+
22
+ export class EventParser {
23
+ private decoder: TextDecoder = new TextDecoder('utf-8');
24
+ private unprocessedBuffer?: ArrayBuffer;
25
+
26
+ process(
27
+ buffer: ArrayBuffer,
28
+ dispatcher: (eventInfo: EventInfo) => void
29
+ ): void {
30
+ const availableBuffer = this.buildAvailableBuffer(buffer);
31
+ if (!availableBuffer) {
32
+ return;
33
+ }
34
+
35
+ const eventStrings = this.extractEventStringsFromBuffer(availableBuffer);
36
+
37
+ if (!eventStrings.length) {
38
+ return;
39
+ }
40
+
41
+ EventParser.parseAndDispatchEvents(eventStrings, dispatcher);
42
+ }
43
+
44
+ private buildAvailableBuffer(buffer: ArrayBuffer): ArrayBuffer | undefined {
45
+ const unprocessedBuffer = this.unprocessedBuffer;
46
+ this.unprocessedBuffer = undefined;
47
+
48
+ if (!buffer.byteLength) {
49
+ return unprocessedBuffer;
50
+ } else if (!unprocessedBuffer?.byteLength) {
51
+ return buffer;
52
+ }
53
+
54
+ const newBuffer = new Uint8Array(
55
+ unprocessedBuffer.byteLength + buffer.byteLength
56
+ );
57
+ newBuffer.set(new Uint8Array(unprocessedBuffer), 0);
58
+ newBuffer.set(new Uint8Array(buffer), unprocessedBuffer.byteLength);
59
+
60
+ return newBuffer.buffer;
61
+ }
62
+
63
+ private extractEventStringsFromBuffer(buffer: ArrayBuffer): string[] {
64
+ const eventStrings: string[] = [];
65
+
66
+ while (buffer.byteLength) {
67
+ // Find end of next event separator in buffer, exiting if none found.
68
+ const eventSeparator = EventParser.findEventSeparator(buffer);
69
+ if (!eventSeparator) {
70
+ // Save unprocessed data
71
+ this.unprocessedBuffer = buffer;
72
+ break;
73
+ }
74
+
75
+ const [endOfCurrentEventIdx, startOfNextEventIdx] = eventSeparator;
76
+
77
+ const eventBuffer = buffer.slice(0, endOfCurrentEventIdx);
78
+ buffer = buffer.slice(startOfNextEventIdx);
79
+
80
+ const eventString = this.decoder.decode(eventBuffer, {
81
+ stream: true,
82
+ });
83
+
84
+ eventStrings.push(eventString);
85
+ }
86
+
87
+ return eventStrings;
88
+ }
89
+
90
+ private static findEventSeparator(
91
+ buffer: ArrayBuffer
92
+ ): [number, number] | undefined {
93
+ const bytes = new Uint8Array(buffer);
94
+
95
+ for (let idx = 0; idx < bytes.length; ++idx) {
96
+ const byte = bytes[idx];
97
+
98
+ switch (byte) {
99
+ // line-feed
100
+ case 0xa: {
101
+ // if next byte is same,
102
+ // we found a separator
103
+ if (bytes[idx + 1] == 0xa) {
104
+ return [idx, idx + 2];
105
+ }
106
+ break;
107
+ }
108
+
109
+ // carriage-return
110
+ case 0xd: {
111
+ // if next byte is same,
112
+ // we found a separator
113
+ if (bytes[idx + 1] == 0xd) {
114
+ return [idx, idx + 2];
115
+ }
116
+
117
+ // if next is line-feed, and pattern
118
+ // repeats, we found a separator.
119
+ if (
120
+ bytes[idx + 1] == 0xa &&
121
+ bytes[idx + 2] == 0xd &&
122
+ bytes[idx + 3] == 0xa
123
+ ) {
124
+ return [idx, idx + 4];
125
+ }
126
+
127
+ break;
128
+ }
129
+
130
+ default:
131
+ }
132
+ }
133
+ return undefined;
134
+ }
135
+
136
+ private static parseAndDispatchEvents(
137
+ eventStrings: string[],
138
+ dispatcher: (eventInfo: EventInfo) => void
139
+ ) {
140
+ for (const eventString of eventStrings) {
141
+ if (!eventString.length) {
142
+ continue;
143
+ }
144
+
145
+ const parsedEvent = EventParser.parseEvent(eventString);
146
+
147
+ dispatcher(parsedEvent);
148
+ }
149
+ }
150
+
151
+ private static parseEvent(eventString: string): EventInfo {
152
+ const event: EventInfo = {};
153
+
154
+ for (const line of eventString.split(lineSeparatorsRegEx)) {
155
+ const keyValueSeparatorIdx = line.indexOf(':');
156
+
157
+ let key: string;
158
+ let value: string;
159
+ if (keyValueSeparatorIdx != -1) {
160
+ key = line.slice(0, keyValueSeparatorIdx);
161
+ value = line.slice(keyValueSeparatorIdx + 1);
162
+ } else {
163
+ key = line;
164
+ value = '';
165
+ }
166
+
167
+ switch (key) {
168
+ case 'retry':
169
+ event.retry = EventParser.trimFieldValue(value);
170
+ break;
171
+
172
+ case 'data': {
173
+ const data = event.data ?? '';
174
+ event.data = `${data}${EventParser.trimFieldValue(value)}\n`;
175
+ break;
176
+ }
177
+
178
+ case '':
179
+ // comment do nothing
180
+ break;
181
+
182
+ default: {
183
+ (event as Record<string, string>)[key] =
184
+ EventParser.trimFieldValue(value);
185
+ }
186
+ }
187
+ }
188
+
189
+ if (event.data?.[event.data?.length - 1] == '\n') {
190
+ event.data = event.data.slice(0, -1);
191
+ }
192
+
193
+ return event;
194
+ }
195
+
196
+ private static trimFieldValue(value: string): string {
197
+ if (value[0] != ' ') {
198
+ return value;
199
+ }
200
+ return value.slice(1);
201
+ }
202
+ }
203
+
204
+ const lineSeparatorsRegEx = /\r\n|\r|\n/;