@encore.dev/sdk 2.0.0-beta.8 → 2.0.0-beta.9

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 (186) hide show
  1. package/README.md +54 -0
  2. package/app_meta.test.ts +25 -5
  3. package/app_meta.ts +43 -12
  4. package/config/config.test.ts +8 -12
  5. package/config/config.ts +16 -12
  6. package/config/secrets.ts +19 -10
  7. package/config/value.ts +13 -17
  8. package/cron/mod.test.ts +52 -0
  9. package/cron/mod.ts +9 -2
  10. package/dist/app_meta.d.ts +37 -12
  11. package/dist/app_meta.d.ts.map +1 -1
  12. package/dist/app_meta.js +1 -0
  13. package/dist/app_meta.js.map +1 -1
  14. package/dist/config/config.d.ts +16 -12
  15. package/dist/config/config.d.ts.map +1 -1
  16. package/dist/config/config.js +5 -2
  17. package/dist/config/config.js.map +1 -1
  18. package/dist/config/secrets.d.ts +19 -10
  19. package/dist/config/secrets.d.ts.map +1 -1
  20. package/dist/config/secrets.js +19 -10
  21. package/dist/config/secrets.js.map +1 -1
  22. package/dist/config/value.d.ts +7 -5
  23. package/dist/config/value.d.ts.map +1 -1
  24. package/dist/config/value.js +10 -7
  25. package/dist/config/value.js.map +1 -1
  26. package/dist/cron/mod.d.ts +5 -1
  27. package/dist/cron/mod.d.ts.map +1 -1
  28. package/dist/cron/mod.js +8 -1
  29. package/dist/cron/mod.js.map +1 -1
  30. package/dist/frontends/mod.d.ts +39 -0
  31. package/dist/frontends/mod.d.ts.map +1 -0
  32. package/dist/frontends/mod.js +47 -0
  33. package/dist/frontends/mod.js.map +1 -0
  34. package/dist/gateway/mod.d.ts +3 -2
  35. package/dist/gateway/mod.d.ts.map +1 -1
  36. package/dist/gateway/mod.js +3 -2
  37. package/dist/gateway/mod.js.map +1 -1
  38. package/dist/gateways/mod.d.ts +42 -0
  39. package/dist/gateways/mod.d.ts.map +1 -0
  40. package/dist/gateways/mod.js +55 -0
  41. package/dist/gateways/mod.js.map +1 -0
  42. package/dist/internal/fetch.d.ts +4 -0
  43. package/dist/internal/fetch.d.ts.map +1 -1
  44. package/dist/internal/fetch.js +12 -1
  45. package/dist/internal/fetch.js.map +1 -1
  46. package/dist/internal/otel/http.d.ts +11 -0
  47. package/dist/internal/otel/http.d.ts.map +1 -0
  48. package/dist/internal/otel/http.js +64 -0
  49. package/dist/internal/otel/http.js.map +1 -0
  50. package/dist/internal/otel/index.d.ts +1 -1
  51. package/dist/internal/otel/index.d.ts.map +1 -1
  52. package/dist/internal/otel/index.js +1 -1
  53. package/dist/internal/otel/index.js.map +1 -1
  54. package/dist/internal/otel/provider.d.ts +3 -0
  55. package/dist/internal/otel/provider.d.ts.map +1 -1
  56. package/dist/internal/otel/provider.js +3 -0
  57. package/dist/internal/otel/provider.js.map +1 -1
  58. package/dist/internal/otel/span.d.ts +11 -1
  59. package/dist/internal/otel/span.d.ts.map +1 -1
  60. package/dist/internal/otel/span.js +25 -2
  61. package/dist/internal/otel/span.js.map +1 -1
  62. package/dist/internal/reqfields.d.ts +26 -6
  63. package/dist/internal/reqfields.d.ts.map +1 -1
  64. package/dist/internal/reqfields.js +44 -7
  65. package/dist/internal/reqfields.js.map +1 -1
  66. package/dist/internal/runtime/napi/napi.cjs +36 -4
  67. package/dist/internal/runtime/napi/napi.d.cts +99 -4
  68. package/dist/internal/testing/carry.d.ts +49 -0
  69. package/dist/internal/testing/carry.d.ts.map +1 -0
  70. package/dist/internal/testing/carry.js +77 -0
  71. package/dist/internal/testing/carry.js.map +1 -0
  72. package/dist/internal/testing/scope.d.ts +34 -6
  73. package/dist/internal/testing/scope.d.ts.map +1 -1
  74. package/dist/internal/testing/scope.js +103 -5
  75. package/dist/internal/testing/scope.js.map +1 -1
  76. package/dist/internal/testing/steps.d.ts +55 -0
  77. package/dist/internal/testing/steps.d.ts.map +1 -0
  78. package/dist/internal/testing/steps.js +295 -0
  79. package/dist/internal/testing/steps.js.map +1 -0
  80. package/dist/internal/testing/telemetry.d.ts +305 -0
  81. package/dist/internal/testing/telemetry.d.ts.map +1 -0
  82. package/dist/internal/testing/telemetry.js +188 -0
  83. package/dist/internal/testing/telemetry.js.map +1 -0
  84. package/dist/mod.d.ts +1 -1
  85. package/dist/mod.d.ts.map +1 -1
  86. package/dist/mod.js.map +1 -1
  87. package/dist/pubsub/mod.d.ts +1 -1
  88. package/dist/pubsub/mod.d.ts.map +1 -1
  89. package/dist/pubsub/subscription.d.ts +6 -5
  90. package/dist/pubsub/subscription.d.ts.map +1 -1
  91. package/dist/pubsub/subscription.js.map +1 -1
  92. package/dist/pubsub/topic.d.ts +14 -8
  93. package/dist/pubsub/topic.d.ts.map +1 -1
  94. package/dist/pubsub/topic.js +36 -22
  95. package/dist/pubsub/topic.js.map +1 -1
  96. package/dist/reqctx/mod.d.ts +1 -18
  97. package/dist/reqctx/mod.d.ts.map +1 -1
  98. package/dist/reqctx/mod.js +1 -17
  99. package/dist/reqctx/mod.js.map +1 -1
  100. package/dist/services/index.d.ts +2 -1
  101. package/dist/services/index.d.ts.map +1 -1
  102. package/dist/services/index.js +2 -1
  103. package/dist/services/index.js.map +1 -1
  104. package/dist/storage/objects/bucket.d.ts +2 -1
  105. package/dist/storage/objects/bucket.d.ts.map +1 -1
  106. package/dist/storage/objects/bucket.js +2 -1
  107. package/dist/storage/objects/bucket.js.map +1 -1
  108. package/dist/storage/postgres/database.d.ts +5 -1
  109. package/dist/storage/postgres/database.d.ts.map +1 -1
  110. package/dist/storage/postgres/database.js +7 -3
  111. package/dist/storage/postgres/database.js.map +1 -1
  112. package/dist/storage/redis/commands.d.ts +5 -5
  113. package/dist/storage/redis/commands.d.ts.map +1 -1
  114. package/dist/storage/redis/commands.js +5 -5
  115. package/dist/storage/redis/commands.js.map +1 -1
  116. package/dist/testing/mod.d.ts +28 -5
  117. package/dist/testing/mod.d.ts.map +1 -1
  118. package/dist/testing/mod.js +52 -8
  119. package/dist/testing/mod.js.map +1 -1
  120. package/dist/testing/playwright.d.ts +93 -0
  121. package/dist/testing/playwright.d.ts.map +1 -0
  122. package/dist/testing/playwright.js +164 -0
  123. package/dist/testing/playwright.js.map +1 -0
  124. package/dist/testing/puppeteer.d.ts +36 -0
  125. package/dist/testing/puppeteer.d.ts.map +1 -0
  126. package/dist/testing/puppeteer.js +70 -0
  127. package/dist/testing/puppeteer.js.map +1 -0
  128. package/dist/testing/system.d.ts +181 -0
  129. package/dist/testing/system.d.ts.map +1 -0
  130. package/dist/testing/system.js +255 -0
  131. package/dist/testing/system.js.map +1 -0
  132. package/dist/tracing/http.d.ts +11 -0
  133. package/dist/tracing/http.d.ts.map +1 -0
  134. package/dist/tracing/http.js +34 -0
  135. package/dist/tracing/http.js.map +1 -0
  136. package/dist/tracing/mod.d.ts +1 -0
  137. package/dist/tracing/mod.d.ts.map +1 -1
  138. package/dist/tracing/mod.js +1 -0
  139. package/dist/tracing/mod.js.map +1 -1
  140. package/dist/tracing/node.d.ts +10 -0
  141. package/dist/tracing/node.d.ts.map +1 -0
  142. package/dist/tracing/node.js +142 -0
  143. package/dist/tracing/node.js.map +1 -0
  144. package/dist/tsconfig.build.tsbuildinfo +1 -1
  145. package/frontends/mod.test.ts +51 -0
  146. package/frontends/mod.ts +50 -0
  147. package/gateway/mod.ts +3 -2
  148. package/gateways/mod.ts +60 -0
  149. package/internal/fetch.ts +12 -1
  150. package/internal/otel/http.ts +91 -0
  151. package/internal/otel/index.ts +1 -1
  152. package/internal/otel/provider.ts +3 -0
  153. package/internal/otel/span.ts +25 -2
  154. package/internal/reqfields.ts +43 -10
  155. package/internal/runtime/napi/napi.cjs +36 -4
  156. package/internal/runtime/napi/napi.d.cts +99 -4
  157. package/internal/testing/carry.test.ts +242 -0
  158. package/internal/testing/carry.ts +101 -0
  159. package/internal/testing/scope.ts +106 -7
  160. package/internal/testing/steps.test.ts +152 -0
  161. package/internal/testing/steps.ts +342 -0
  162. package/internal/testing/telemetry.test.ts +210 -0
  163. package/internal/testing/telemetry.ts +466 -0
  164. package/mod.ts +1 -0
  165. package/package.json +26 -1
  166. package/pubsub/mod.ts +6 -1
  167. package/pubsub/pubsub.test.ts +1 -0
  168. package/pubsub/subscription.ts +6 -5
  169. package/pubsub/topic.ts +45 -21
  170. package/reqctx/mod.ts +1 -18
  171. package/reqctx/reqctx.test.ts +23 -0
  172. package/scripts/postprocess-docs.test.ts +11 -1
  173. package/services/index.ts +2 -1
  174. package/storage/objects/bucket.ts +2 -1
  175. package/storage/postgres/database.ts +7 -3
  176. package/storage/redis/commands.ts +5 -5
  177. package/testing/mod.ts +63 -7
  178. package/testing/playwright.ts +247 -0
  179. package/testing/puppeteer.ts +101 -0
  180. package/testing/system.test.ts +108 -0
  181. package/testing/system.ts +373 -0
  182. package/tracing/http.ts +49 -0
  183. package/tracing/mod.ts +1 -0
  184. package/tracing/native.test.ts +101 -4
  185. package/tracing/node.ts +176 -0
  186. package/tracing/tracing.test.ts +660 -24
@@ -0,0 +1,176 @@
1
+ import type {Context, TextMapGetter} from '@opentelemetry/api';
2
+ import {errorMonitor, type EventEmitter} from 'node:events';
3
+ import type {
4
+ IncomingHttpHeaders,
5
+ IncomingMessage,
6
+ Server,
7
+ ServerResponse,
8
+ } from 'node:http';
9
+ import {withContext} from '../internal/otel/context';
10
+ import {
11
+ recordHTTPConnectionClosed,
12
+ recordHTTPError,
13
+ recordHTTPStatus,
14
+ startHTTPSpan,
15
+ } from '../internal/otel/http';
16
+
17
+ const headersGetter: TextMapGetter<IncomingHttpHeaders> = {
18
+ keys: headers => Object.keys(headers),
19
+ get: (headers, key) => headers[key.toLowerCase()],
20
+ };
21
+
22
+ /**
23
+ * Traces a native Node HTTP listener through response completion. Preserves
24
+ * the receiver, request and response without consuming bodies. Synchronous
25
+ * results stay synchronous; PromiseLike results become Promises.
26
+ * The application owns error handling and error responses.
27
+ */
28
+ export function instrumentHTTP<This = Server, Result = void>(
29
+ handler: (
30
+ this: This,
31
+ request: IncomingMessage,
32
+ response: ServerResponse,
33
+ ) => Result,
34
+ ): (
35
+ this: This,
36
+ request: IncomingMessage,
37
+ response: ServerResponse,
38
+ ) => Result extends PromiseLike<unknown> ? Promise<Awaited<Result>> : Result {
39
+ return function (this: This, request, response) {
40
+ const url = requestURL(request);
41
+ const {span, context} = startHTTPSpan(
42
+ request.headers,
43
+ headersGetter,
44
+ request.method || '',
45
+ url,
46
+ );
47
+ const restoreRequest = bindEmitter(request, context);
48
+ const restoreResponse = bindEmitter(response, context);
49
+ let ended = false;
50
+ const end = (record: () => void) => {
51
+ if (ended) return;
52
+ ended = true;
53
+ response.removeListener('finish', finish);
54
+ response.removeListener('close', close);
55
+ response.removeListener(errorMonitor, fail);
56
+ restoreRequest();
57
+ restoreResponse();
58
+ try {
59
+ if (response.headersSent) recordHTTPStatus(span, response.statusCode);
60
+ record();
61
+ } finally {
62
+ span.end();
63
+ }
64
+ };
65
+ const finish = () => end(() => recordHTTPStatus(span, response.statusCode));
66
+ const close = () => {
67
+ if (response.writableFinished) finish();
68
+ else end(() => recordHTTPConnectionClosed(span));
69
+ };
70
+ const fail = (error: unknown) => end(() => recordHTTPError(span, error));
71
+ response.once('finish', finish);
72
+ response.once('close', close);
73
+ response.once(errorMonitor, fail);
74
+ if (response.writableFinished) finish();
75
+ else if (response.destroyed) close();
76
+
77
+ return withContext(context, () => {
78
+ try {
79
+ const result = handler.call(this, request, response);
80
+ if (
81
+ result !== null &&
82
+ (typeof result === 'object' || typeof result === 'function')
83
+ ) {
84
+ const then = (result as {then?: unknown}).then;
85
+ if (typeof then === 'function') {
86
+ return new Promise<unknown>((resolve, reject) => {
87
+ then.call(result, resolve, reject);
88
+ }).then(
89
+ value => value,
90
+ error => {
91
+ fail(error);
92
+ throw error;
93
+ },
94
+ );
95
+ }
96
+ }
97
+ return result;
98
+ } catch (error) {
99
+ fail(error);
100
+ throw error;
101
+ }
102
+ }) as Result extends PromiseLike<unknown>
103
+ ? Promise<Awaited<Result>>
104
+ : Result;
105
+ };
106
+ }
107
+
108
+ function requestURL(request: IncomingMessage): URL {
109
+ const scheme =
110
+ 'encrypted' in request.socket && request.socket.encrypted
111
+ ? 'https'
112
+ : 'http';
113
+ let base: URL;
114
+ try {
115
+ base = new URL(`${scheme}://${request.headers.host || 'localhost'}`);
116
+ } catch {
117
+ base = new URL(`${scheme}://localhost`);
118
+ }
119
+ const target = request.url || '/';
120
+ try {
121
+ // An origin-form path starting with // is not a new authority.
122
+ return new URL(
123
+ target.startsWith('/') ? base.origin + target : target,
124
+ base,
125
+ );
126
+ } catch {
127
+ base.pathname = target.split('?')[0]!;
128
+ base.search = '';
129
+ base.hash = '';
130
+ return base;
131
+ }
132
+ }
133
+
134
+ interface EmitterBinding {
135
+ active: boolean;
136
+ original: EventEmitter['emit'];
137
+ descriptor: PropertyDescriptor | undefined;
138
+ }
139
+
140
+ const emitterBindings = new WeakMap<EventEmitter['emit'], EmitterBinding>();
141
+ const dispatching = new WeakSet<EventEmitter>();
142
+
143
+ function bindEmitter(emitter: EventEmitter, context: Context): () => void {
144
+ const binding: EmitterBinding = {
145
+ active: true,
146
+ original: emitter.emit,
147
+ descriptor: Object.getOwnPropertyDescriptor(emitter, 'emit'),
148
+ };
149
+ const bound: typeof emitter.emit = function (this: EventEmitter, ...args) {
150
+ const emit = () => binding.original.apply(this, args);
151
+ if (!binding.active || dispatching.has(this)) return emit();
152
+ return withContext(context, () => {
153
+ dispatching.add(this);
154
+ try {
155
+ return emit();
156
+ } finally {
157
+ dispatching.delete(this);
158
+ }
159
+ });
160
+ };
161
+ emitterBindings.set(bound, binding);
162
+ emitter.emit = bound;
163
+ return () => {
164
+ binding.active = false;
165
+ if (emitter.emit !== bound) return;
166
+ let previous = binding;
167
+ for (;;) {
168
+ const ancestor = emitterBindings.get(previous.original);
169
+ if (!ancestor || ancestor.active) break;
170
+ previous = ancestor;
171
+ }
172
+ if (previous.descriptor)
173
+ Object.defineProperty(emitter, 'emit', previous.descriptor);
174
+ else delete (emitter as Partial<EventEmitter>).emit;
175
+ };
176
+ }