@trigger.dev/core 3.0.0-beta.5 → 3.0.0-beta.51

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 (201) hide show
  1. package/dist/api-Pl9fxB8v.d.mts +13879 -0
  2. package/dist/api-sNoooMbT.d.ts +13879 -0
  3. package/dist/catalog-H7yFiZ60.d.mts +202 -0
  4. package/dist/catalog-h79CG5Wy.d.ts +202 -0
  5. package/dist/common-CLW82lkt.d.mts +1423 -0
  6. package/dist/common-CLW82lkt.d.ts +1423 -0
  7. package/dist/{eventFilterMatches-2kHImluE.d.mts → eventFilter-66NaBAMi.d.mts} +1 -3
  8. package/dist/{eventFilterMatches-2kHImluE.d.ts → eventFilter-66NaBAMi.d.ts} +1 -3
  9. package/dist/eventFilterMatches.d.mts +6 -0
  10. package/dist/eventFilterMatches.d.ts +6 -0
  11. package/dist/eventFilterMatches.js +165 -0
  12. package/dist/eventFilterMatches.js.map +1 -0
  13. package/dist/eventFilterMatches.mjs +163 -0
  14. package/dist/eventFilterMatches.mjs.map +1 -0
  15. package/dist/index.d.mts +14 -16657
  16. package/dist/index.d.ts +14 -16657
  17. package/dist/index.js +8 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs +8 -2
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/json-DBPEJQRe.d.mts +16 -0
  22. package/dist/json-DBPEJQRe.d.ts +16 -0
  23. package/dist/manager-KvCabW1a.d.mts +18 -0
  24. package/dist/manager-OBA35PzR.d.ts +18 -0
  25. package/dist/messages-JvHJ83wv.d.mts +19428 -0
  26. package/dist/messages-JvHJ83wv.d.ts +19428 -0
  27. package/dist/replacements.d.mts +22 -0
  28. package/dist/replacements.d.ts +22 -0
  29. package/dist/replacements.js +33 -0
  30. package/dist/replacements.js.map +1 -0
  31. package/dist/replacements.mjs +28 -0
  32. package/dist/replacements.mjs.map +1 -0
  33. package/dist/requestFilter-HNZQEpQb.d.ts +180 -0
  34. package/dist/requestFilter-b5W2_1oP.d.mts +180 -0
  35. package/dist/requestFilterMatches.d.mts +13 -0
  36. package/dist/requestFilterMatches.d.ts +13 -0
  37. package/dist/requestFilterMatches.js +238 -0
  38. package/dist/requestFilterMatches.js.map +1 -0
  39. package/dist/requestFilterMatches.mjs +235 -0
  40. package/dist/requestFilterMatches.mjs.map +1 -0
  41. package/dist/retry.d.mts +10 -0
  42. package/dist/retry.d.ts +10 -0
  43. package/dist/retry.js +100 -0
  44. package/dist/retry.js.map +1 -0
  45. package/dist/retry.mjs +97 -0
  46. package/dist/retry.mjs.map +1 -0
  47. package/dist/schemas/index.d.mts +2611 -0
  48. package/dist/schemas/index.d.ts +2611 -0
  49. package/dist/schemas/index.js +1630 -0
  50. package/dist/schemas/index.js.map +1 -0
  51. package/dist/schemas/index.mjs +1463 -0
  52. package/dist/schemas/index.mjs.map +1 -0
  53. package/dist/schemas-C9ssfehv.d.mts +2568 -0
  54. package/dist/schemas-C9ssfehv.d.ts +2568 -0
  55. package/dist/taskLogger-CzADe4je.d.ts +33 -0
  56. package/dist/taskLogger-r_AFTX45.d.mts +33 -0
  57. package/dist/tracer-N0p2Fuuv.d.mts +23 -0
  58. package/dist/tracer-N0p2Fuuv.d.ts +23 -0
  59. package/dist/types.d.mts +11 -0
  60. package/dist/types.d.ts +11 -0
  61. package/dist/types.js +4 -0
  62. package/dist/types.js.map +1 -0
  63. package/dist/types.mjs +3 -0
  64. package/dist/types.mjs.map +1 -0
  65. package/dist/utils.d.mts +7 -0
  66. package/dist/utils.d.ts +7 -0
  67. package/dist/utils.js +33 -0
  68. package/dist/utils.js.map +1 -0
  69. package/dist/utils.mjs +30 -0
  70. package/dist/utils.mjs.map +1 -0
  71. package/dist/v3/dev/index.d.mts +29 -0
  72. package/dist/v3/dev/index.d.ts +29 -0
  73. package/dist/v3/dev/index.js +93 -0
  74. package/dist/v3/dev/index.js.map +1 -0
  75. package/dist/v3/dev/index.mjs +91 -0
  76. package/dist/v3/dev/index.mjs.map +1 -0
  77. package/dist/v3/errors.d.mts +39 -0
  78. package/dist/v3/errors.d.ts +39 -0
  79. package/dist/v3/errors.js +204 -0
  80. package/dist/v3/errors.js.map +1 -0
  81. package/dist/v3/errors.mjs +195 -0
  82. package/dist/v3/errors.mjs.map +1 -0
  83. package/dist/v3/index.d.mts +445 -20508
  84. package/dist/v3/index.d.ts +445 -20508
  85. package/dist/v3/index.js +2843 -2884
  86. package/dist/v3/index.js.map +1 -1
  87. package/dist/v3/index.mjs +2785 -2856
  88. package/dist/v3/index.mjs.map +1 -1
  89. package/dist/v3/logger-api.d.mts +24 -0
  90. package/dist/v3/logger-api.d.ts +24 -0
  91. package/dist/v3/logger-api.js +118 -0
  92. package/dist/v3/logger-api.js.map +1 -0
  93. package/dist/v3/logger-api.mjs +116 -0
  94. package/dist/v3/logger-api.mjs.map +1 -0
  95. package/dist/v3/otel/index.js +119 -53
  96. package/dist/v3/otel/index.js.map +1 -1
  97. package/dist/v3/otel/index.mjs +119 -53
  98. package/dist/v3/otel/index.mjs.map +1 -1
  99. package/dist/v3/prod/index.d.mts +46 -0
  100. package/dist/v3/prod/index.d.ts +46 -0
  101. package/dist/v3/prod/index.js +180 -0
  102. package/dist/v3/prod/index.js.map +1 -0
  103. package/dist/v3/prod/index.mjs +178 -0
  104. package/dist/v3/prod/index.mjs.map +1 -0
  105. package/dist/v3/schemas/index.d.mts +4807 -0
  106. package/dist/v3/schemas/index.d.ts +4807 -0
  107. package/dist/v3/schemas/index.js +2054 -0
  108. package/dist/v3/schemas/index.js.map +1 -0
  109. package/dist/v3/schemas/index.mjs +1913 -0
  110. package/dist/v3/schemas/index.mjs.map +1 -0
  111. package/dist/v3/semanticInternalAttributes.d.mts +56 -0
  112. package/dist/v3/semanticInternalAttributes.d.ts +56 -0
  113. package/dist/v3/semanticInternalAttributes.js +61 -0
  114. package/dist/v3/semanticInternalAttributes.js.map +1 -0
  115. package/dist/v3/semanticInternalAttributes.mjs +59 -0
  116. package/dist/v3/semanticInternalAttributes.mjs.map +1 -0
  117. package/dist/v3/utils/durations.d.mts +15 -0
  118. package/dist/v3/utils/durations.d.ts +15 -0
  119. package/dist/v3/utils/durations.js +93 -0
  120. package/dist/v3/utils/durations.js.map +1 -0
  121. package/dist/v3/utils/durations.mjs +82 -0
  122. package/dist/v3/utils/durations.mjs.map +1 -0
  123. package/dist/v3/utils/flattenAttributes.d.mts +8 -0
  124. package/dist/v3/utils/flattenAttributes.d.ts +8 -0
  125. package/dist/v3/utils/flattenAttributes.js +131 -0
  126. package/dist/v3/utils/flattenAttributes.js.map +1 -0
  127. package/dist/v3/utils/flattenAttributes.mjs +126 -0
  128. package/dist/v3/utils/flattenAttributes.mjs.map +1 -0
  129. package/dist/v3/utils/ioSerialization.d.mts +21 -0
  130. package/dist/v3/utils/ioSerialization.d.ts +21 -0
  131. package/dist/v3/utils/ioSerialization.js +2392 -0
  132. package/dist/v3/utils/ioSerialization.js.map +1 -0
  133. package/dist/v3/utils/ioSerialization.mjs +2383 -0
  134. package/dist/v3/utils/ioSerialization.mjs.map +1 -0
  135. package/dist/v3/utils/omit.d.mts +3 -0
  136. package/dist/v3/utils/omit.d.ts +3 -0
  137. package/dist/v3/utils/omit.js +20 -0
  138. package/dist/v3/utils/omit.js.map +1 -0
  139. package/dist/v3/utils/omit.mjs +18 -0
  140. package/dist/v3/utils/omit.mjs.map +1 -0
  141. package/dist/v3/utils/retries.d.mts +46 -0
  142. package/dist/v3/utils/retries.d.ts +46 -0
  143. package/dist/v3/utils/retries.js +118 -0
  144. package/dist/v3/utils/retries.js.map +1 -0
  145. package/dist/v3/utils/retries.mjs +113 -0
  146. package/dist/v3/utils/retries.mjs.map +1 -0
  147. package/dist/v3/utils/structuredLogger.d.mts +31 -0
  148. package/dist/v3/utils/structuredLogger.d.ts +31 -0
  149. package/dist/v3/utils/structuredLogger.js +88 -0
  150. package/dist/v3/utils/structuredLogger.js.map +1 -0
  151. package/dist/v3/utils/structuredLogger.mjs +86 -0
  152. package/dist/v3/utils/structuredLogger.mjs.map +1 -0
  153. package/dist/v3/utils/timers.d.mts +6 -0
  154. package/dist/v3/utils/timers.d.ts +6 -0
  155. package/dist/v3/utils/timers.js +31 -0
  156. package/dist/v3/utils/timers.js.map +1 -0
  157. package/dist/v3/utils/timers.mjs +28 -0
  158. package/dist/v3/utils/timers.mjs.map +1 -0
  159. package/dist/v3/workers/index.d.mts +144 -0
  160. package/dist/v3/workers/index.d.ts +144 -0
  161. package/dist/v3/workers/index.js +3708 -0
  162. package/dist/v3/workers/index.js.map +1 -0
  163. package/dist/v3/workers/index.mjs +3689 -0
  164. package/dist/v3/workers/index.mjs.map +1 -0
  165. package/dist/v3/zodIpc.d.mts +32 -0
  166. package/dist/v3/zodIpc.d.ts +32 -0
  167. package/dist/v3/zodIpc.js +268 -0
  168. package/dist/v3/zodIpc.js.map +1 -0
  169. package/dist/v3/zodIpc.mjs +266 -0
  170. package/dist/v3/zodIpc.mjs.map +1 -0
  171. package/dist/v3/zodMessageHandler.d.mts +82 -0
  172. package/dist/v3/zodMessageHandler.d.ts +82 -0
  173. package/dist/v3/zodMessageHandler.js +222 -0
  174. package/dist/v3/zodMessageHandler.js.map +1 -0
  175. package/dist/v3/zodMessageHandler.mjs +217 -0
  176. package/dist/v3/zodMessageHandler.mjs.map +1 -0
  177. package/dist/v3/zodNamespace.d.mts +3663 -0
  178. package/dist/v3/zodNamespace.d.ts +3663 -0
  179. package/dist/v3/zodNamespace.js +397 -0
  180. package/dist/v3/zodNamespace.js.map +1 -0
  181. package/dist/v3/zodNamespace.mjs +395 -0
  182. package/dist/v3/zodNamespace.mjs.map +1 -0
  183. package/dist/v3/zodSocket.d.mts +93 -0
  184. package/dist/v3/zodSocket.d.ts +93 -0
  185. package/dist/v3/zodSocket.js +350 -0
  186. package/dist/v3/zodSocket.js.map +1 -0
  187. package/dist/v3/zodSocket.mjs +346 -0
  188. package/dist/v3/zodSocket.mjs.map +1 -0
  189. package/dist/v3/zodfetch.d.mts +219 -0
  190. package/dist/v3/zodfetch.d.ts +219 -0
  191. package/dist/v3/zodfetch.js +834 -0
  192. package/dist/v3/zodfetch.js.map +1 -0
  193. package/dist/v3/zodfetch.mjs +811 -0
  194. package/dist/v3/zodfetch.mjs.map +1 -0
  195. package/dist/versions.d.mts +11 -0
  196. package/dist/versions.d.ts +11 -0
  197. package/dist/versions.js +31 -0
  198. package/dist/versions.js.map +1 -0
  199. package/dist/versions.mjs +27 -0
  200. package/dist/versions.mjs.map +1 -0
  201. package/package.json +223 -5
@@ -0,0 +1,238 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/eventFilterMatches.ts
7
+ function eventFilterMatches(payload, filter) {
8
+ if (payload === void 0 || payload === null) {
9
+ if (Object.entries(filter).length === 0) {
10
+ return true;
11
+ } else {
12
+ return false;
13
+ }
14
+ }
15
+ for (const [patternKey, patternValue] of Object.entries(filter)) {
16
+ const payloadValue = payload[patternKey];
17
+ if (Array.isArray(patternValue)) {
18
+ if (patternValue.length === 0) {
19
+ continue;
20
+ }
21
+ if (patternValue.every((item) => typeof item === "string")) {
22
+ if (patternValue.includes(payloadValue)) {
23
+ continue;
24
+ }
25
+ return false;
26
+ }
27
+ if (patternValue.every((item) => typeof item === "number")) {
28
+ if (patternValue.includes(payloadValue)) {
29
+ continue;
30
+ }
31
+ return false;
32
+ }
33
+ if (patternValue.every((item) => typeof item === "boolean")) {
34
+ if (patternValue.includes(payloadValue)) {
35
+ continue;
36
+ }
37
+ return false;
38
+ }
39
+ const objectArray = patternValue;
40
+ if (!contentFiltersMatches(payloadValue, objectArray)) {
41
+ return false;
42
+ }
43
+ continue;
44
+ } else if (typeof patternValue === "object") {
45
+ if (Array.isArray(payloadValue)) {
46
+ if (!payloadValue.some((item) => eventFilterMatches(item, patternValue))) {
47
+ return false;
48
+ }
49
+ } else {
50
+ if (!eventFilterMatches(payloadValue, patternValue)) {
51
+ return false;
52
+ }
53
+ }
54
+ }
55
+ }
56
+ return true;
57
+ }
58
+ __name(eventFilterMatches, "eventFilterMatches");
59
+ function contentFiltersMatches(actualValue, contentFilters) {
60
+ for (const contentFilter of contentFilters) {
61
+ if (typeof contentFilter === "object") {
62
+ Object.entries(contentFilter)[0];
63
+ if (!contentFilterMatches(actualValue, contentFilter)) {
64
+ return false;
65
+ }
66
+ }
67
+ }
68
+ return true;
69
+ }
70
+ __name(contentFiltersMatches, "contentFiltersMatches");
71
+ function contentFilterMatches(actualValue, contentFilter) {
72
+ if ("$endsWith" in contentFilter) {
73
+ if (typeof actualValue !== "string") {
74
+ return false;
75
+ }
76
+ return actualValue.endsWith(contentFilter.$endsWith);
77
+ }
78
+ if ("$startsWith" in contentFilter) {
79
+ if (typeof actualValue !== "string") {
80
+ return false;
81
+ }
82
+ return actualValue.startsWith(contentFilter.$startsWith);
83
+ }
84
+ if ("$anythingBut" in contentFilter) {
85
+ if (Array.isArray(contentFilter.$anythingBut)) {
86
+ if (contentFilter.$anythingBut.includes(actualValue)) {
87
+ return false;
88
+ }
89
+ }
90
+ if (contentFilter.$anythingBut === actualValue) {
91
+ return false;
92
+ }
93
+ return true;
94
+ }
95
+ if ("$exists" in contentFilter) {
96
+ if (contentFilter.$exists) {
97
+ return actualValue !== void 0;
98
+ }
99
+ return actualValue === void 0;
100
+ }
101
+ if ("$gt" in contentFilter) {
102
+ if (typeof actualValue !== "number") {
103
+ return false;
104
+ }
105
+ return actualValue > contentFilter.$gt;
106
+ }
107
+ if ("$lt" in contentFilter) {
108
+ if (typeof actualValue !== "number") {
109
+ return false;
110
+ }
111
+ return actualValue < contentFilter.$lt;
112
+ }
113
+ if ("$gte" in contentFilter) {
114
+ if (typeof actualValue !== "number") {
115
+ return false;
116
+ }
117
+ return actualValue >= contentFilter.$gte;
118
+ }
119
+ if ("$lte" in contentFilter) {
120
+ if (typeof actualValue !== "number") {
121
+ return false;
122
+ }
123
+ return actualValue <= contentFilter.$lte;
124
+ }
125
+ if ("$between" in contentFilter) {
126
+ if (typeof actualValue !== "number") {
127
+ return false;
128
+ }
129
+ return actualValue >= contentFilter.$between[0] && actualValue <= contentFilter.$between[1];
130
+ }
131
+ if ("$includes" in contentFilter) {
132
+ if (Array.isArray(actualValue)) {
133
+ return actualValue.includes(contentFilter.$includes);
134
+ }
135
+ return false;
136
+ }
137
+ if ("$ignoreCaseEquals" in contentFilter) {
138
+ if (typeof actualValue !== "string") {
139
+ return false;
140
+ }
141
+ return actualValue.localeCompare(contentFilter.$ignoreCaseEquals, void 0, {
142
+ sensitivity: "accent"
143
+ }) === 0;
144
+ }
145
+ if ("$isNull" in contentFilter) {
146
+ if (contentFilter.$isNull) {
147
+ return actualValue === null;
148
+ }
149
+ return actualValue !== null;
150
+ }
151
+ if ("$not" in contentFilter) {
152
+ if (Array.isArray(actualValue)) {
153
+ return !actualValue.includes(contentFilter.$not);
154
+ } else if (typeof actualValue === "number" || typeof actualValue === "boolean" || typeof actualValue === "string") {
155
+ return actualValue !== contentFilter.$not;
156
+ }
157
+ return false;
158
+ }
159
+ return true;
160
+ }
161
+ __name(contentFilterMatches, "contentFilterMatches");
162
+
163
+ // src/requestFilterMatches.ts
164
+ async function requestFilterMatches(request, filter) {
165
+ const clonedRequest = request.clone();
166
+ if (!requestMethodMatches(clonedRequest.method, filter.method)) {
167
+ return false;
168
+ }
169
+ const headersObj = Object.fromEntries(clonedRequest.headers.entries());
170
+ if (filter.headers && !eventFilterMatches(headersObj, filter.headers)) {
171
+ return false;
172
+ }
173
+ const searchParams = new URL(clonedRequest.url).searchParams;
174
+ const searchParamsObject = {};
175
+ for (const [key, value] of searchParams.entries()) {
176
+ searchParamsObject[key] = value;
177
+ }
178
+ if (filter.query && !eventFilterMatches(searchParamsObject, filter.query)) {
179
+ return false;
180
+ }
181
+ try {
182
+ const json = await clonedRequest.json();
183
+ if (filter.body && !eventFilterMatches(json, filter.body)) {
184
+ return false;
185
+ }
186
+ } catch (e) {
187
+ if (filter.body) {
188
+ return false;
189
+ }
190
+ }
191
+ return true;
192
+ }
193
+ __name(requestFilterMatches, "requestFilterMatches");
194
+ async function responseFilterMatches(response, filter) {
195
+ if (filter.headers && !eventFilterMatches(response.headers, filter.headers)) {
196
+ return {
197
+ match: false
198
+ };
199
+ }
200
+ try {
201
+ const json = await response.json();
202
+ if (filter.body && !eventFilterMatches(json, filter.body)) {
203
+ return {
204
+ match: false,
205
+ body: json
206
+ };
207
+ } else {
208
+ return {
209
+ match: true,
210
+ body: json
211
+ };
212
+ }
213
+ } catch (e) {
214
+ if (filter.body) {
215
+ return {
216
+ match: false,
217
+ body: void 0
218
+ };
219
+ }
220
+ }
221
+ return {
222
+ match: true,
223
+ body: void 0
224
+ };
225
+ }
226
+ __name(responseFilterMatches, "responseFilterMatches");
227
+ function requestMethodMatches(method, filter) {
228
+ if (!filter) {
229
+ return true;
230
+ }
231
+ return filter.includes(method);
232
+ }
233
+ __name(requestMethodMatches, "requestMethodMatches");
234
+
235
+ exports.requestFilterMatches = requestFilterMatches;
236
+ exports.responseFilterMatches = responseFilterMatches;
237
+ //# sourceMappingURL=out.js.map
238
+ //# sourceMappingURL=requestFilterMatches.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/eventFilterMatches.ts","../src/requestFilterMatches.ts"],"names":["eventFilterMatches","payload","filter","undefined","Object","entries","length","patternKey","patternValue","payloadValue","Array","isArray","every","item","includes","objectArray","contentFiltersMatches","some","actualValue","contentFilters","contentFilter","key","value","contentFilterMatches","endsWith","$endsWith","startsWith","$startsWith","$anythingBut","$exists","$gt","$lt","$gte","$lte","$between","$includes","localeCompare","$ignoreCaseEquals","sensitivity","$isNull","$not","requestFilterMatches","request","clonedRequest","clone","requestMethodMatches","method","headersObj","fromEntries","headers","searchParams","URL","url","searchParamsObject","query","json","body","e","responseFilterMatches","response","match"],"mappings":";;;;AAMO,SAASA,mBAAmBC,SAAcC,QAA8B;AAC7E,MAAID,YAAYE,UAAaF,YAAY,MAAM;AAC7C,QAAIG,O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7C,QAAO,GAAI;AACjDgD,uBAAmBhC,GAAAA,IAAOC;EAC5B;AAEA,MAAIpB,OAAOoD,SAAS,CAACtD,mBAAmBqD,oBAAoBnD,OAAOoD,KAAK,GAAG;AACzE,WAAO;EACT;AAEA,MAAI;AACF,UAAMC,OAAO,MAAMZ,cAAcY,KAAI;AACrC,QAAIrD,OAAOsD,QAAQ,CAACxD,mBAAmBuD,MAAMrD,OAAOsD,IAAI,GAAG;AACzD,aAAO;IACT;EACF,SAASC,GAAG;AACV,QAAIvD,OAAOsD,MAAM;AACf,aAAO;IACT;EACF;AAEA,SAAO;AACT;AApCsBf;AA2CtB,eAAsBiB,sBACpBC,UACAzD,QACoC;AACpC,MAAIA,OAAO+C,WAAW,CAACjD,mBAAmB2D,SAASV,SAAS/C,OAAO+C,OAAO,GAAG;AAC3E,WAAO;MAAEW,OAAO;IAAM;EACxB;AAEA,MAAI;AACF,UAAML,OAAO,MAAMI,SAASJ,KAAI;AAChC,QAAIrD,OAAOsD,QAAQ,CAACxD,mBAAmBuD,MAAMrD,OAAOsD,IAAI,GAAG;AACzD,aAAO;QAAEI,OAAO;QAAOJ,MAAMD;MAAK;IACpC,OAAO;AACL,aAAO;QAAEK,OAAO;QAAMJ,MAAMD;MAAK;IACnC;EACF,SAASE,GAAG;AACV,QAAIvD,OAAOsD,MAAM;AACf,aAAO;QAAEI,OAAO;QAAOJ,MAAMrD;MAAU;IACzC;EACF;AAEA,SAAO;IAAEyD,OAAO;IAAMJ,MAAMrD;EAAU;AACxC;AAtBsBuD;AAwBtB,SAASb,qBAAqBC,QAAoB5C,QAA0C;AAC1F,MAAI,CAACA,QAAQ;AACX,WAAO;EACT;AAEA,SAAOA,OAAOY,SAASgC,MAAAA;AACzB;AANSD","sourcesContent":["import { EventFilter } from \"./schemas/eventFilter\";\n\n// EventFilter is a recursive type, where the keys are strings and the values are an array of strings, numbers, booleans, or objects.\n// If the values of the array are strings, numbers, or booleans, than we are matching against the value of the payload.\n// If the values of the array are objects, then we are doing content filtering\n// An example would be [{ $endsWith: \".png\" }, { $startsWith: \"images/\" } ]\nexport function eventFilterMatches(payload: any, filter: EventFilter): boolean {\n if (payload === undefined || payload === null) {\n if (Object.entries(filter).length === 0) {\n return true;\n } else {\n return false;\n }\n }\n\n for (const [patternKey, patternValue] of Object.entries(filter)) {\n const payloadValue = payload[patternKey];\n\n if (Array.isArray(patternValue)) {\n if (patternValue.length === 0) {\n continue;\n }\n\n // Check to see if all the items in the array are a string\n if ((patternValue as unknown[]).every((item) => typeof item === \"string\")) {\n if ((patternValue as string[]).includes(payloadValue)) {\n continue;\n }\n\n return false;\n }\n\n // Check to see if all the items in the array are a number\n if ((patternValue as unknown[]).every((item) => typeof item === \"number\")) {\n if ((patternValue as number[]).includes(payloadValue)) {\n continue;\n }\n\n return false;\n }\n\n // Check to see if all the items in the array are a boolean\n if ((patternValue as unknown[]).every((item) => typeof item === \"boolean\")) {\n if ((patternValue as boolean[]).includes(payloadValue)) {\n continue;\n }\n\n return false;\n }\n\n // Now we know that all the items in the array are objects\n const objectArray = patternValue as Exclude<\n typeof patternValue,\n number[] | string[] | boolean[]\n >;\n\n if (!contentFiltersMatches(payloadValue, objectArray)) {\n return false;\n }\n\n continue;\n } else if (typeof patternValue === \"object\") {\n if (Array.isArray(payloadValue)) {\n if (!payloadValue.some((item) => eventFilterMatches(item, patternValue))) {\n return false;\n }\n } else {\n if (!eventFilterMatches(payloadValue, patternValue)) {\n return false;\n }\n }\n }\n }\n return true;\n}\n\ntype ContentFilters = Exclude<EventFilter[string], EventFilter | string[] | number[] | boolean[]>;\n\nfunction contentFiltersMatches(actualValue: any, contentFilters: ContentFilters): boolean {\n for (const contentFilter of contentFilters) {\n if (typeof contentFilter === \"object\") {\n const [key, value] = Object.entries(contentFilter)[0];\n\n if (!contentFilterMatches(actualValue, contentFilter)) {\n return false;\n }\n }\n }\n\n return true;\n}\n\nfunction contentFilterMatches(actualValue: any, contentFilter: ContentFilters[number]): boolean {\n if (\"$endsWith\" in contentFilter) {\n if (typeof actualValue !== \"string\") {\n return false;\n }\n\n return actualValue.endsWith(contentFilter.$endsWith);\n }\n\n if (\"$startsWith\" in contentFilter) {\n if (typeof actualValue !== \"string\") {\n return false;\n }\n\n return actualValue.startsWith(contentFilter.$startsWith);\n }\n\n if (\"$anythingBut\" in contentFilter) {\n if (Array.isArray(contentFilter.$anythingBut)) {\n if ((contentFilter.$anythingBut as any[]).includes(actualValue)) {\n return false;\n }\n }\n\n if (contentFilter.$anythingBut === actualValue) {\n return false;\n }\n\n return true;\n }\n\n if (\"$exists\" in contentFilter) {\n if (contentFilter.$exists) {\n return actualValue !== undefined;\n }\n\n return actualValue === undefined;\n }\n\n if (\"$gt\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue > contentFilter.$gt;\n }\n\n if (\"$lt\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue < contentFilter.$lt;\n }\n\n if (\"$gte\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue >= contentFilter.$gte;\n }\n\n if (\"$lte\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue <= contentFilter.$lte;\n }\n\n if (\"$between\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue >= contentFilter.$between[0] && actualValue <= contentFilter.$between[1];\n }\n\n if (\"$includes\" in contentFilter) {\n if (Array.isArray(actualValue)) {\n return actualValue.includes(contentFilter.$includes);\n }\n\n return false;\n }\n\n // Use localCompare\n if (\"$ignoreCaseEquals\" in contentFilter) {\n if (typeof actualValue !== \"string\") {\n return false;\n }\n\n return (\n actualValue.localeCompare(contentFilter.$ignoreCaseEquals, undefined, {\n sensitivity: \"accent\",\n }) === 0\n );\n }\n\n if (\"$isNull\" in contentFilter) {\n if (contentFilter.$isNull) {\n return actualValue === null;\n }\n\n return actualValue !== null;\n }\n\n if (\"$not\" in contentFilter) {\n if (Array.isArray(actualValue)) {\n return !actualValue.includes(contentFilter.$not);\n } else if (typeof actualValue === 'number' || typeof actualValue === 'boolean' || typeof actualValue === 'string') {\n return actualValue !== contentFilter.$not;\n }\n\n return false;\n }\n\n return true;\n}\n","import { eventFilterMatches } from \"./eventFilterMatches\";\nimport { HttpMethod, RequestFilter, ResponseFilter, StringMatch } from \"./schemas/requestFilter\";\n\nexport async function requestFilterMatches(\n request: Request,\n filter: RequestFilter\n): Promise<boolean> {\n const clonedRequest = request.clone();\n if (!requestMethodMatches(clonedRequest.method as HttpMethod, filter.method)) {\n return false;\n }\n\n const headersObj = Object.fromEntries(clonedRequest.headers.entries());\n if (filter.headers && !eventFilterMatches(headersObj, filter.headers)) {\n return false;\n }\n\n const searchParams = new URL(clonedRequest.url).searchParams;\n const searchParamsObject: Record<string, string> = {};\n for (const [key, value] of searchParams.entries()) {\n searchParamsObject[key] = value;\n }\n\n if (filter.query && !eventFilterMatches(searchParamsObject, filter.query)) {\n return false;\n }\n\n try {\n const json = await clonedRequest.json();\n if (filter.body && !eventFilterMatches(json, filter.body)) {\n return false;\n }\n } catch (e) {\n if (filter.body) {\n return false;\n }\n }\n\n return true;\n}\n\nexport type ResponseFilterMatchResult = {\n match: boolean;\n body?: unknown;\n};\n\nexport async function responseFilterMatches(\n response: Response,\n filter: ResponseFilter\n): Promise<ResponseFilterMatchResult> {\n if (filter.headers && !eventFilterMatches(response.headers, filter.headers)) {\n return { match: false };\n }\n\n try {\n const json = await response.json();\n if (filter.body && !eventFilterMatches(json, filter.body)) {\n return { match: false, body: json };\n } else {\n return { match: true, body: json };\n }\n } catch (e) {\n if (filter.body) {\n return { match: false, body: undefined };\n }\n }\n\n return { match: true, body: undefined };\n}\n\nfunction requestMethodMatches(method: HttpMethod, filter: RequestFilter[\"method\"]): boolean {\n if (!filter) {\n return true;\n }\n\n return filter.includes(method);\n}\n"]}
@@ -0,0 +1,235 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/eventFilterMatches.ts
5
+ function eventFilterMatches(payload, filter) {
6
+ if (payload === void 0 || payload === null) {
7
+ if (Object.entries(filter).length === 0) {
8
+ return true;
9
+ } else {
10
+ return false;
11
+ }
12
+ }
13
+ for (const [patternKey, patternValue] of Object.entries(filter)) {
14
+ const payloadValue = payload[patternKey];
15
+ if (Array.isArray(patternValue)) {
16
+ if (patternValue.length === 0) {
17
+ continue;
18
+ }
19
+ if (patternValue.every((item) => typeof item === "string")) {
20
+ if (patternValue.includes(payloadValue)) {
21
+ continue;
22
+ }
23
+ return false;
24
+ }
25
+ if (patternValue.every((item) => typeof item === "number")) {
26
+ if (patternValue.includes(payloadValue)) {
27
+ continue;
28
+ }
29
+ return false;
30
+ }
31
+ if (patternValue.every((item) => typeof item === "boolean")) {
32
+ if (patternValue.includes(payloadValue)) {
33
+ continue;
34
+ }
35
+ return false;
36
+ }
37
+ const objectArray = patternValue;
38
+ if (!contentFiltersMatches(payloadValue, objectArray)) {
39
+ return false;
40
+ }
41
+ continue;
42
+ } else if (typeof patternValue === "object") {
43
+ if (Array.isArray(payloadValue)) {
44
+ if (!payloadValue.some((item) => eventFilterMatches(item, patternValue))) {
45
+ return false;
46
+ }
47
+ } else {
48
+ if (!eventFilterMatches(payloadValue, patternValue)) {
49
+ return false;
50
+ }
51
+ }
52
+ }
53
+ }
54
+ return true;
55
+ }
56
+ __name(eventFilterMatches, "eventFilterMatches");
57
+ function contentFiltersMatches(actualValue, contentFilters) {
58
+ for (const contentFilter of contentFilters) {
59
+ if (typeof contentFilter === "object") {
60
+ Object.entries(contentFilter)[0];
61
+ if (!contentFilterMatches(actualValue, contentFilter)) {
62
+ return false;
63
+ }
64
+ }
65
+ }
66
+ return true;
67
+ }
68
+ __name(contentFiltersMatches, "contentFiltersMatches");
69
+ function contentFilterMatches(actualValue, contentFilter) {
70
+ if ("$endsWith" in contentFilter) {
71
+ if (typeof actualValue !== "string") {
72
+ return false;
73
+ }
74
+ return actualValue.endsWith(contentFilter.$endsWith);
75
+ }
76
+ if ("$startsWith" in contentFilter) {
77
+ if (typeof actualValue !== "string") {
78
+ return false;
79
+ }
80
+ return actualValue.startsWith(contentFilter.$startsWith);
81
+ }
82
+ if ("$anythingBut" in contentFilter) {
83
+ if (Array.isArray(contentFilter.$anythingBut)) {
84
+ if (contentFilter.$anythingBut.includes(actualValue)) {
85
+ return false;
86
+ }
87
+ }
88
+ if (contentFilter.$anythingBut === actualValue) {
89
+ return false;
90
+ }
91
+ return true;
92
+ }
93
+ if ("$exists" in contentFilter) {
94
+ if (contentFilter.$exists) {
95
+ return actualValue !== void 0;
96
+ }
97
+ return actualValue === void 0;
98
+ }
99
+ if ("$gt" in contentFilter) {
100
+ if (typeof actualValue !== "number") {
101
+ return false;
102
+ }
103
+ return actualValue > contentFilter.$gt;
104
+ }
105
+ if ("$lt" in contentFilter) {
106
+ if (typeof actualValue !== "number") {
107
+ return false;
108
+ }
109
+ return actualValue < contentFilter.$lt;
110
+ }
111
+ if ("$gte" in contentFilter) {
112
+ if (typeof actualValue !== "number") {
113
+ return false;
114
+ }
115
+ return actualValue >= contentFilter.$gte;
116
+ }
117
+ if ("$lte" in contentFilter) {
118
+ if (typeof actualValue !== "number") {
119
+ return false;
120
+ }
121
+ return actualValue <= contentFilter.$lte;
122
+ }
123
+ if ("$between" in contentFilter) {
124
+ if (typeof actualValue !== "number") {
125
+ return false;
126
+ }
127
+ return actualValue >= contentFilter.$between[0] && actualValue <= contentFilter.$between[1];
128
+ }
129
+ if ("$includes" in contentFilter) {
130
+ if (Array.isArray(actualValue)) {
131
+ return actualValue.includes(contentFilter.$includes);
132
+ }
133
+ return false;
134
+ }
135
+ if ("$ignoreCaseEquals" in contentFilter) {
136
+ if (typeof actualValue !== "string") {
137
+ return false;
138
+ }
139
+ return actualValue.localeCompare(contentFilter.$ignoreCaseEquals, void 0, {
140
+ sensitivity: "accent"
141
+ }) === 0;
142
+ }
143
+ if ("$isNull" in contentFilter) {
144
+ if (contentFilter.$isNull) {
145
+ return actualValue === null;
146
+ }
147
+ return actualValue !== null;
148
+ }
149
+ if ("$not" in contentFilter) {
150
+ if (Array.isArray(actualValue)) {
151
+ return !actualValue.includes(contentFilter.$not);
152
+ } else if (typeof actualValue === "number" || typeof actualValue === "boolean" || typeof actualValue === "string") {
153
+ return actualValue !== contentFilter.$not;
154
+ }
155
+ return false;
156
+ }
157
+ return true;
158
+ }
159
+ __name(contentFilterMatches, "contentFilterMatches");
160
+
161
+ // src/requestFilterMatches.ts
162
+ async function requestFilterMatches(request, filter) {
163
+ const clonedRequest = request.clone();
164
+ if (!requestMethodMatches(clonedRequest.method, filter.method)) {
165
+ return false;
166
+ }
167
+ const headersObj = Object.fromEntries(clonedRequest.headers.entries());
168
+ if (filter.headers && !eventFilterMatches(headersObj, filter.headers)) {
169
+ return false;
170
+ }
171
+ const searchParams = new URL(clonedRequest.url).searchParams;
172
+ const searchParamsObject = {};
173
+ for (const [key, value] of searchParams.entries()) {
174
+ searchParamsObject[key] = value;
175
+ }
176
+ if (filter.query && !eventFilterMatches(searchParamsObject, filter.query)) {
177
+ return false;
178
+ }
179
+ try {
180
+ const json = await clonedRequest.json();
181
+ if (filter.body && !eventFilterMatches(json, filter.body)) {
182
+ return false;
183
+ }
184
+ } catch (e) {
185
+ if (filter.body) {
186
+ return false;
187
+ }
188
+ }
189
+ return true;
190
+ }
191
+ __name(requestFilterMatches, "requestFilterMatches");
192
+ async function responseFilterMatches(response, filter) {
193
+ if (filter.headers && !eventFilterMatches(response.headers, filter.headers)) {
194
+ return {
195
+ match: false
196
+ };
197
+ }
198
+ try {
199
+ const json = await response.json();
200
+ if (filter.body && !eventFilterMatches(json, filter.body)) {
201
+ return {
202
+ match: false,
203
+ body: json
204
+ };
205
+ } else {
206
+ return {
207
+ match: true,
208
+ body: json
209
+ };
210
+ }
211
+ } catch (e) {
212
+ if (filter.body) {
213
+ return {
214
+ match: false,
215
+ body: void 0
216
+ };
217
+ }
218
+ }
219
+ return {
220
+ match: true,
221
+ body: void 0
222
+ };
223
+ }
224
+ __name(responseFilterMatches, "responseFilterMatches");
225
+ function requestMethodMatches(method, filter) {
226
+ if (!filter) {
227
+ return true;
228
+ }
229
+ return filter.includes(method);
230
+ }
231
+ __name(requestMethodMatches, "requestMethodMatches");
232
+
233
+ export { requestFilterMatches, responseFilterMatches };
234
+ //# sourceMappingURL=out.js.map
235
+ //# sourceMappingURL=requestFilterMatches.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/eventFilterMatches.ts","../src/requestFilterMatches.ts"],"names":["eventFilterMatches","payload","filter","undefined","Object","entries","length","patternKey","patternValue","payloadValue","Array","isArray","every","item","includes","objectArray","contentFiltersMatches","some","actualValue","contentFilters","contentFilter","key","value","contentFilterMatches","endsWith","$endsWith","startsWith","$startsWith","$anythingBut","$exists","$gt","$lt","$gte","$lte","$between","$includes","localeCompare","$ignoreCaseEquals","sensitivity","$isNull","$not","requestFilterMatches","request","clonedRequest","clone","requestMethodMatches","method","headersObj","fromEntries","headers","searchParams","URL","url","searchParamsObject","query","json","body","e","responseFilterMatches","response","match"],"mappings":";;;;AAMO,SAASA,mBAAmBC,SAAcC,QAA8B;AAC7E,MAAID,YAAYE,UAAaF,YAAY,MAAM;AAC7C,QAAIG,OAAOC,QAAQH,MAAAA,EAAQI,WAAW,GAAG;AACvC,aAAO;IACT,OAAO;AACL,aAAO;IACT;EACF;AAEA,aAAW,CAACC,YAAYC,YAAAA,KAAiBJ,OAAOC,QAAQH,MAAAA,GAAS;AAC/D,UAAMO,eAAeR,QAAQM,UAAAA;AAE7B,QAAIG,MAAMC,QAAQH,YAAAA,GAAe;AAC/B,UAAIA,aAAaF,WAAW,GAAG;AAC7B;MACF;AAGA,UAAKE,aAA2BI,MAAM,CAACC,SAAS,OAAOA,SAAS,QAAA,GAAW;AACzE,YAAKL,aAA0BM,SAASL,YAAAA,GAAe;AACrD;QACF;AAEA,eAAO;MACT;AAGA,UAAKD,aAA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AACpC,MAAIA,OAAO+C,WAAW,CAACjD,mBAAmB2D,SAASV,SAAS/C,OAAO+C,OAAO,GAAG;AAC3E,WAAO;MAAEW,OAAO;IAAM;EACxB;AAEA,MAAI;AACF,UAAML,OAAO,MAAMI,SAASJ,KAAI;AAChC,QAAIrD,OAAOsD,QAAQ,CAACxD,mBAAmBuD,MAAMrD,OAAOsD,IAAI,GAAG;AACzD,aAAO;QAAEI,OAAO;QAAOJ,MAAMD;MAAK;IACpC,OAAO;AACL,aAAO;QAAEK,OAAO;QAAMJ,MAAMD;MAAK;IACnC;EACF,SAASE,GAAG;AACV,QAAIvD,OAAOsD,MAAM;AACf,aAAO;QAAEI,OAAO;QAAOJ,MAAMrD;MAAU;IACzC;EACF;AAEA,SAAO;IAAEyD,OAAO;IAAMJ,MAAMrD;EAAU;AACxC;AAtBsBuD;AAwBtB,SAASb,qBAAqBC,QAAoB5C,QAA0C;AAC1F,MAAI,CAACA,QAAQ;AACX,WAAO;EACT;AAEA,SAAOA,OAAOY,SAASgC,MAAAA;AACzB;AANSD","sourcesContent":["import { EventFilter } from \"./schemas/eventFilter\";\n\n// EventFilter is a recursive type, where the keys are strings and the values are an array of strings, numbers, booleans, or objects.\n// If the values of the array are strings, numbers, or booleans, than we are matching against the value of the payload.\n// If the values of the array are objects, then we are doing content filtering\n// An example would be [{ $endsWith: \".png\" }, { $startsWith: \"images/\" } ]\nexport function eventFilterMatches(payload: any, filter: EventFilter): boolean {\n if (payload === undefined || payload === null) {\n if (Object.entries(filter).length === 0) {\n return true;\n } else {\n return false;\n }\n }\n\n for (const [patternKey, patternValue] of Object.entries(filter)) {\n const payloadValue = payload[patternKey];\n\n if (Array.isArray(patternValue)) {\n if (patternValue.length === 0) {\n continue;\n }\n\n // Check to see if all the items in the array are a string\n if ((patternValue as unknown[]).every((item) => typeof item === \"string\")) {\n if ((patternValue as string[]).includes(payloadValue)) {\n continue;\n }\n\n return false;\n }\n\n // Check to see if all the items in the array are a number\n if ((patternValue as unknown[]).every((item) => typeof item === \"number\")) {\n if ((patternValue as number[]).includes(payloadValue)) {\n continue;\n }\n\n return false;\n }\n\n // Check to see if all the items in the array are a boolean\n if ((patternValue as unknown[]).every((item) => typeof item === \"boolean\")) {\n if ((patternValue as boolean[]).includes(payloadValue)) {\n continue;\n }\n\n return false;\n }\n\n // Now we know that all the items in the array are objects\n const objectArray = patternValue as Exclude<\n typeof patternValue,\n number[] | string[] | boolean[]\n >;\n\n if (!contentFiltersMatches(payloadValue, objectArray)) {\n return false;\n }\n\n continue;\n } else if (typeof patternValue === \"object\") {\n if (Array.isArray(payloadValue)) {\n if (!payloadValue.some((item) => eventFilterMatches(item, patternValue))) {\n return false;\n }\n } else {\n if (!eventFilterMatches(payloadValue, patternValue)) {\n return false;\n }\n }\n }\n }\n return true;\n}\n\ntype ContentFilters = Exclude<EventFilter[string], EventFilter | string[] | number[] | boolean[]>;\n\nfunction contentFiltersMatches(actualValue: any, contentFilters: ContentFilters): boolean {\n for (const contentFilter of contentFilters) {\n if (typeof contentFilter === \"object\") {\n const [key, value] = Object.entries(contentFilter)[0];\n\n if (!contentFilterMatches(actualValue, contentFilter)) {\n return false;\n }\n }\n }\n\n return true;\n}\n\nfunction contentFilterMatches(actualValue: any, contentFilter: ContentFilters[number]): boolean {\n if (\"$endsWith\" in contentFilter) {\n if (typeof actualValue !== \"string\") {\n return false;\n }\n\n return actualValue.endsWith(contentFilter.$endsWith);\n }\n\n if (\"$startsWith\" in contentFilter) {\n if (typeof actualValue !== \"string\") {\n return false;\n }\n\n return actualValue.startsWith(contentFilter.$startsWith);\n }\n\n if (\"$anythingBut\" in contentFilter) {\n if (Array.isArray(contentFilter.$anythingBut)) {\n if ((contentFilter.$anythingBut as any[]).includes(actualValue)) {\n return false;\n }\n }\n\n if (contentFilter.$anythingBut === actualValue) {\n return false;\n }\n\n return true;\n }\n\n if (\"$exists\" in contentFilter) {\n if (contentFilter.$exists) {\n return actualValue !== undefined;\n }\n\n return actualValue === undefined;\n }\n\n if (\"$gt\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue > contentFilter.$gt;\n }\n\n if (\"$lt\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue < contentFilter.$lt;\n }\n\n if (\"$gte\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue >= contentFilter.$gte;\n }\n\n if (\"$lte\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue <= contentFilter.$lte;\n }\n\n if (\"$between\" in contentFilter) {\n if (typeof actualValue !== \"number\") {\n return false;\n }\n\n return actualValue >= contentFilter.$between[0] && actualValue <= contentFilter.$between[1];\n }\n\n if (\"$includes\" in contentFilter) {\n if (Array.isArray(actualValue)) {\n return actualValue.includes(contentFilter.$includes);\n }\n\n return false;\n }\n\n // Use localCompare\n if (\"$ignoreCaseEquals\" in contentFilter) {\n if (typeof actualValue !== \"string\") {\n return false;\n }\n\n return (\n actualValue.localeCompare(contentFilter.$ignoreCaseEquals, undefined, {\n sensitivity: \"accent\",\n }) === 0\n );\n }\n\n if (\"$isNull\" in contentFilter) {\n if (contentFilter.$isNull) {\n return actualValue === null;\n }\n\n return actualValue !== null;\n }\n\n if (\"$not\" in contentFilter) {\n if (Array.isArray(actualValue)) {\n return !actualValue.includes(contentFilter.$not);\n } else if (typeof actualValue === 'number' || typeof actualValue === 'boolean' || typeof actualValue === 'string') {\n return actualValue !== contentFilter.$not;\n }\n\n return false;\n }\n\n return true;\n}\n","import { eventFilterMatches } from \"./eventFilterMatches\";\nimport { HttpMethod, RequestFilter, ResponseFilter, StringMatch } from \"./schemas/requestFilter\";\n\nexport async function requestFilterMatches(\n request: Request,\n filter: RequestFilter\n): Promise<boolean> {\n const clonedRequest = request.clone();\n if (!requestMethodMatches(clonedRequest.method as HttpMethod, filter.method)) {\n return false;\n }\n\n const headersObj = Object.fromEntries(clonedRequest.headers.entries());\n if (filter.headers && !eventFilterMatches(headersObj, filter.headers)) {\n return false;\n }\n\n const searchParams = new URL(clonedRequest.url).searchParams;\n const searchParamsObject: Record<string, string> = {};\n for (const [key, value] of searchParams.entries()) {\n searchParamsObject[key] = value;\n }\n\n if (filter.query && !eventFilterMatches(searchParamsObject, filter.query)) {\n return false;\n }\n\n try {\n const json = await clonedRequest.json();\n if (filter.body && !eventFilterMatches(json, filter.body)) {\n return false;\n }\n } catch (e) {\n if (filter.body) {\n return false;\n }\n }\n\n return true;\n}\n\nexport type ResponseFilterMatchResult = {\n match: boolean;\n body?: unknown;\n};\n\nexport async function responseFilterMatches(\n response: Response,\n filter: ResponseFilter\n): Promise<ResponseFilterMatchResult> {\n if (filter.headers && !eventFilterMatches(response.headers, filter.headers)) {\n return { match: false };\n }\n\n try {\n const json = await response.json();\n if (filter.body && !eventFilterMatches(json, filter.body)) {\n return { match: false, body: json };\n } else {\n return { match: true, body: json };\n }\n } catch (e) {\n if (filter.body) {\n return { match: false, body: undefined };\n }\n }\n\n return { match: true, body: undefined };\n}\n\nfunction requestMethodMatches(method: HttpMethod, filter: RequestFilter[\"method\"]): boolean {\n if (!filter) {\n return true;\n }\n\n return filter.includes(method);\n}\n"]}
@@ -0,0 +1,10 @@
1
+ import { bh as RetryOptions } from './api-Pl9fxB8v.mjs';
2
+ import './eventFilter-66NaBAMi.mjs';
3
+ import 'zod';
4
+ import './json-DBPEJQRe.mjs';
5
+ import './types.mjs';
6
+
7
+ declare function calculateRetryAt(retryOptions: RetryOptions, attempts: number): Date | undefined;
8
+ declare function calculateResetAt(resets: string | undefined | null, format: "unix_timestamp" | "iso_8601" | "iso_8601_duration_openai_variant" | "unix_timestamp_in_ms", now?: Date): Date | undefined;
9
+
10
+ export { calculateResetAt, calculateRetryAt };
@@ -0,0 +1,10 @@
1
+ import { bh as RetryOptions } from './api-sNoooMbT.js';
2
+ import './eventFilter-66NaBAMi.js';
3
+ import 'zod';
4
+ import './json-DBPEJQRe.js';
5
+ import './types.js';
6
+
7
+ declare function calculateRetryAt(retryOptions: RetryOptions, attempts: number): Date | undefined;
8
+ declare function calculateResetAt(resets: string | undefined | null, format: "unix_timestamp" | "iso_8601" | "iso_8601_duration_openai_variant" | "unix_timestamp_in_ms", now?: Date): Date | undefined;
9
+
10
+ export { calculateResetAt, calculateRetryAt };
package/dist/retry.js ADDED
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/retry.ts
7
+ var DEFAULT_RETRY_OPTIONS = {
8
+ limit: 5,
9
+ factor: 1.8,
10
+ minTimeoutInMs: 1e3,
11
+ maxTimeoutInMs: 6e4,
12
+ randomize: true
13
+ };
14
+ function calculateRetryAt(retryOptions, attempts) {
15
+ const options = {
16
+ ...DEFAULT_RETRY_OPTIONS,
17
+ ...retryOptions
18
+ };
19
+ const retryCount = attempts + 1;
20
+ if (retryCount >= options.limit) {
21
+ return;
22
+ }
23
+ const random = options.randomize ? Math.random() + 1 : 1;
24
+ let timeoutInMs = Math.round(random * Math.max(options.minTimeoutInMs, 1) * Math.pow(options.factor, Math.max(attempts - 1, 0)));
25
+ timeoutInMs = Math.min(timeoutInMs, options.maxTimeoutInMs);
26
+ return new Date(Date.now() + timeoutInMs);
27
+ }
28
+ __name(calculateRetryAt, "calculateRetryAt");
29
+ function calculateResetAt(resets, format, now = /* @__PURE__ */ new Date()) {
30
+ if (!resets)
31
+ return;
32
+ switch (format) {
33
+ case "iso_8601_duration_openai_variant": {
34
+ return calculateISO8601DurationOpenAIVariantResetAt(resets, now);
35
+ }
36
+ case "iso_8601": {
37
+ return calculateISO8601ResetAt(resets, now);
38
+ }
39
+ case "unix_timestamp": {
40
+ return calculateUnixTimestampResetAt(resets, now);
41
+ }
42
+ case "unix_timestamp_in_ms": {
43
+ return calculateUnixTimestampInMsResetAt(resets, now);
44
+ }
45
+ }
46
+ }
47
+ __name(calculateResetAt, "calculateResetAt");
48
+ function calculateUnixTimestampResetAt(resets, now = /* @__PURE__ */ new Date()) {
49
+ if (!resets)
50
+ return void 0;
51
+ const resetAt = parseInt(resets, 10);
52
+ if (isNaN(resetAt))
53
+ return void 0;
54
+ return new Date(resetAt * 1e3);
55
+ }
56
+ __name(calculateUnixTimestampResetAt, "calculateUnixTimestampResetAt");
57
+ function calculateUnixTimestampInMsResetAt(resets, now = /* @__PURE__ */ new Date()) {
58
+ if (!resets)
59
+ return void 0;
60
+ const resetAt = parseInt(resets, 10);
61
+ if (isNaN(resetAt))
62
+ return void 0;
63
+ return new Date(resetAt);
64
+ }
65
+ __name(calculateUnixTimestampInMsResetAt, "calculateUnixTimestampInMsResetAt");
66
+ function calculateISO8601ResetAt(resets, now = /* @__PURE__ */ new Date()) {
67
+ if (!resets)
68
+ return void 0;
69
+ const resetAt = new Date(resets);
70
+ if (isNaN(resetAt.getTime()))
71
+ return void 0;
72
+ return resetAt;
73
+ }
74
+ __name(calculateISO8601ResetAt, "calculateISO8601ResetAt");
75
+ function calculateISO8601DurationOpenAIVariantResetAt(resets, now = /* @__PURE__ */ new Date()) {
76
+ if (!resets)
77
+ return void 0;
78
+ const pattern = /^(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+(?:\.\d+)?)s)?(?:(\d+)ms)?$/;
79
+ const match = resets.match(pattern);
80
+ if (!match)
81
+ return void 0;
82
+ const days = parseInt(match[1], 10) || 0;
83
+ const hours = parseInt(match[2], 10) || 0;
84
+ const minutes = parseInt(match[3], 10) || 0;
85
+ const seconds = parseFloat(match[4]) || 0;
86
+ const milliseconds = parseInt(match[5], 10) || 0;
87
+ const resetAt = new Date(now);
88
+ resetAt.setDate(resetAt.getDate() + days);
89
+ resetAt.setHours(resetAt.getHours() + hours);
90
+ resetAt.setMinutes(resetAt.getMinutes() + minutes);
91
+ resetAt.setSeconds(resetAt.getSeconds() + Math.floor(seconds));
92
+ resetAt.setMilliseconds(resetAt.getMilliseconds() + (seconds - Math.floor(seconds)) * 1e3 + milliseconds);
93
+ return resetAt;
94
+ }
95
+ __name(calculateISO8601DurationOpenAIVariantResetAt, "calculateISO8601DurationOpenAIVariantResetAt");
96
+
97
+ exports.calculateResetAt = calculateResetAt;
98
+ exports.calculateRetryAt = calculateRetryAt;
99
+ //# sourceMappingURL=out.js.map
100
+ //# sourceMappingURL=retry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/retry.ts"],"names":["DEFAULT_RETRY_OPTIONS","limit","factor","minTimeoutInMs","maxTimeoutInMs","randomize","calculateRetryAt","retryOptions","attempts","options","retryCount","random","Math","timeoutInMs","round","max","pow","min","Date","now","calculateResetAt","resets","format","calculateISO8601DurationOpenAIVariantResetAt","calculateISO8601ResetAt","calculateUnixTimestampResetAt","calculateUnixTimestampInMsResetAt","undefined","resetAt","parseInt","isNaN","getTime","pattern","match","days","hours","minutes","seconds","parseFloat","milliseconds","setDate","getDate","setHours","getHours","setMinutes","getMinutes","setSeconds","getSeconds","floor","setMilliseconds","getMilliseconds"],"mappings":";;;;AAEA,IAAMA,wBAAwB;EAC5BC,OAAO;EACPC,QAAQ;EACRC,gBAAgB;EAChBC,gBAAgB;EAChBC,WAAW;AACb;AAEO,SAASC,iBAAiBC,cAA4BC,UAAoC;AAC/F,QAAMC,UAAU;IACd,GAAGT;IACH,GAAGO;EACL;AAEA,QAAMG,aAAaF,WAAW;AAE9B,MAAIE,cAAcD,QAAQR,OAAO;AAC/B;EACF;AAEA,QAAMU,SAASF,QAAQJ,YAAYO,KAAKD,OAAM,IAAK,IAAI;AAEvD,MAAIE,cAAcD,KAAKE,MACrBH,SACEC,KAAKG,IAAIN,QAAQN,gBAAgB,CAAA,IACjCS,KAAKI,IAAIP,QAAQP,QAAQU,KAAKG,IAAIP,WAAW,GAAG,CAAA,CAAA,CAAA;AAGpDK,gBAAcD,KAAKK,IAAIJ,aAAaJ,QAAQL,cAAc;AAE1D,SAAO,IAAIc,KAAKA,KAAKC,IAAG,IAAKN,WAAAA;AAC/B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B,QAAMO,OAAOL,SAASI,MAAM,CAAA,GAAI,EAAA,KAAO;AACvC,QAAME,QAAQN,SAASI,MAAM,CAAA,GAAI,EAAA,KAAO;AACxC,QAAMG,UAAUP,SAASI,MAAM,CAAA,GAAI,EAAA,KAAO;AAC1C,QAAMI,UAAUC,WAAWL,MAAM,CAAA,CAAE,KAAK;AACxC,QAAMM,eAAeV,SAASI,MAAM,CAAA,GAAI,EAAA,KAAO;AAG/C,QAAML,UAAU,IAAIV,KAAKC,GAAAA;AACzBS,UAAQY,QAAQZ,QAAQa,QAAO,IAAKP,IAAAA;AACpCN,UAAQc,SAASd,QAAQe,SAAQ,IAAKR,KAAAA;AACtCP,UAAQgB,WAAWhB,QAAQiB,WAAU,IAAKT,OAAAA;AAC1CR,UAAQkB,WAAWlB,QAAQmB,WAAU,IAAKnC,KAAKoC,MAAMX,OAAAA,CAAAA;AACrDT,UAAQqB,gBACNrB,QAAQsB,gBAAe,KAAMb,UAAUzB,KAAKoC,MAAMX,OAAAA,KAAY,MAAOE,YAAAA;AAGvE,SAAOX;AACT;AAhCSL","sourcesContent":["import { RetryOptions } from \"./schemas\";\n\nconst DEFAULT_RETRY_OPTIONS = {\n limit: 5,\n factor: 1.8,\n minTimeoutInMs: 1000,\n maxTimeoutInMs: 60000,\n randomize: true,\n} satisfies RetryOptions;\n\nexport function calculateRetryAt(retryOptions: RetryOptions, attempts: number): Date | undefined {\n const options = {\n ...DEFAULT_RETRY_OPTIONS,\n ...retryOptions,\n };\n\n const retryCount = attempts + 1;\n\n if (retryCount >= options.limit) {\n return;\n }\n\n const random = options.randomize ? Math.random() + 1 : 1;\n\n let timeoutInMs = Math.round(\n random *\n Math.max(options.minTimeoutInMs, 1) *\n Math.pow(options.factor, Math.max(attempts - 1, 0))\n );\n\n timeoutInMs = Math.min(timeoutInMs, options.maxTimeoutInMs);\n\n return new Date(Date.now() + timeoutInMs);\n}\n\nexport function calculateResetAt(\n resets: string | undefined | null,\n format:\n | \"unix_timestamp\"\n | \"iso_8601\"\n | \"iso_8601_duration_openai_variant\"\n | \"unix_timestamp_in_ms\",\n now: Date = new Date()\n): Date | undefined {\n if (!resets) return;\n\n switch (format) {\n case \"iso_8601_duration_openai_variant\": {\n return calculateISO8601DurationOpenAIVariantResetAt(resets, now);\n }\n case \"iso_8601\": {\n return calculateISO8601ResetAt(resets, now);\n }\n case \"unix_timestamp\": {\n return calculateUnixTimestampResetAt(resets, now);\n }\n case \"unix_timestamp_in_ms\": {\n return calculateUnixTimestampInMsResetAt(resets, now);\n }\n }\n}\n\nfunction calculateUnixTimestampResetAt(resets: string, now: Date = new Date()): Date | undefined {\n // Check if the input is null or undefined\n if (!resets) return undefined;\n\n // Convert the string to a number\n const resetAt = parseInt(resets, 10);\n\n // If the string doesn't match the expected format, return undefined\n if (isNaN(resetAt)) return undefined;\n\n // Return the date\n return new Date(resetAt * 1000);\n}\n\nfunction calculateUnixTimestampInMsResetAt(\n resets: string,\n now: Date = new Date()\n): Date | undefined {\n // Check if the input is null or undefined\n if (!resets) return undefined;\n\n // Convert the string to a number\n const resetAt = parseInt(resets, 10);\n\n // If the string doesn't match the expected format, return undefined\n if (isNaN(resetAt)) return undefined;\n\n // Return the date\n return new Date(resetAt);\n}\n\nfunction calculateISO8601ResetAt(resets: string, now: Date = new Date()): Date | undefined {\n // Check if the input is null or undefined\n if (!resets) return undefined;\n\n // Parse the date\n const resetAt = new Date(resets);\n\n // If the string doesn't match the expected format, return undefined\n if (isNaN(resetAt.getTime())) return undefined;\n\n return resetAt;\n}\n\nfunction calculateISO8601DurationOpenAIVariantResetAt(\n resets: string,\n now: Date = new Date()\n): Date | undefined {\n // Check if the input is null or undefined\n if (!resets) return undefined;\n\n // Regular expression to match the duration string pattern\n const pattern = /^(?:(\\d+)d)?(?:(\\d+)h)?(?:(\\d+)m)?(?:(\\d+(?:\\.\\d+)?)s)?(?:(\\d+)ms)?$/;\n const match = resets.match(pattern);\n\n // If the string doesn't match the expected format, return undefined\n if (!match) return undefined;\n\n // Extract days, hours, minutes, seconds, and milliseconds from the string\n const days = parseInt(match[1], 10) || 0;\n const hours = parseInt(match[2], 10) || 0;\n const minutes = parseInt(match[3], 10) || 0;\n const seconds = parseFloat(match[4]) || 0;\n const milliseconds = parseInt(match[5], 10) || 0;\n\n // Calculate the future date based on the current date plus the extracted time\n const resetAt = new Date(now);\n resetAt.setDate(resetAt.getDate() + days);\n resetAt.setHours(resetAt.getHours() + hours);\n resetAt.setMinutes(resetAt.getMinutes() + minutes);\n resetAt.setSeconds(resetAt.getSeconds() + Math.floor(seconds));\n resetAt.setMilliseconds(\n resetAt.getMilliseconds() + (seconds - Math.floor(seconds)) * 1000 + milliseconds\n );\n\n return resetAt;\n}\n"]}