@encore.dev/sdk 0.0.0 → 2.0.0-beta.4

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 (250) hide show
  1. package/LICENSE +362 -0
  2. package/README.md +122 -2
  3. package/app_meta.test.ts +134 -0
  4. package/app_meta.ts +186 -0
  5. package/config/config.test.ts +381 -0
  6. package/config/config.ts +178 -0
  7. package/config/mod.ts +14 -0
  8. package/config/secrets.ts +147 -0
  9. package/config/value.ts +243 -0
  10. package/cron/mod.ts +23 -0
  11. package/dist/app_meta.d.ts +93 -0
  12. package/dist/app_meta.js +65 -0
  13. package/dist/app_meta.js.map +1 -0
  14. package/dist/config/config.d.ts +105 -0
  15. package/dist/config/config.js +87 -0
  16. package/dist/config/config.js.map +1 -0
  17. package/dist/config/mod.d.ts +4 -0
  18. package/dist/config/mod.js +3 -0
  19. package/dist/config/mod.js.map +1 -0
  20. package/dist/config/secrets.d.ts +92 -0
  21. package/dist/config/secrets.js +80 -0
  22. package/dist/config/secrets.js.map +1 -0
  23. package/dist/config/value.d.ts +51 -0
  24. package/dist/config/value.js +178 -0
  25. package/dist/config/value.js.map +1 -0
  26. package/dist/cron/mod.d.ts +22 -0
  27. package/dist/cron/mod.js +9 -0
  28. package/dist/cron/mod.js.map +1 -0
  29. package/dist/internal/otel/backend.d.ts +50 -0
  30. package/dist/internal/otel/backend.js +2 -0
  31. package/dist/internal/otel/backend.js.map +1 -0
  32. package/dist/internal/otel/context.d.ts +10 -0
  33. package/dist/internal/otel/context.js +34 -0
  34. package/dist/internal/otel/context.js.map +1 -0
  35. package/dist/internal/otel/context_manager.d.ts +22 -0
  36. package/dist/internal/otel/context_manager.js +120 -0
  37. package/dist/internal/otel/context_manager.js.map +1 -0
  38. package/dist/internal/otel/encode.d.ts +112 -0
  39. package/dist/internal/otel/encode.js +290 -0
  40. package/dist/internal/otel/encode.js.map +1 -0
  41. package/dist/internal/otel/index.d.ts +9 -0
  42. package/dist/internal/otel/index.js +9 -0
  43. package/dist/internal/otel/index.js.map +1 -0
  44. package/dist/internal/otel/meter.d.ts +73 -0
  45. package/dist/internal/otel/meter.js +265 -0
  46. package/dist/internal/otel/meter.js.map +1 -0
  47. package/dist/internal/otel/propagator.d.ts +14 -0
  48. package/dist/internal/otel/propagator.js +61 -0
  49. package/dist/internal/otel/propagator.js.map +1 -0
  50. package/dist/internal/otel/provider.d.ts +9 -0
  51. package/dist/internal/otel/provider.js +19 -0
  52. package/dist/internal/otel/provider.js.map +1 -0
  53. package/dist/internal/otel/request_context.d.ts +11 -0
  54. package/dist/internal/otel/request_context.js +20 -0
  55. package/dist/internal/otel/request_context.js.map +1 -0
  56. package/dist/internal/otel/span.d.ts +61 -0
  57. package/dist/internal/otel/span.js +206 -0
  58. package/dist/internal/otel/span.js.map +1 -0
  59. package/dist/internal/otel/tracer.d.ts +15 -0
  60. package/dist/internal/otel/tracer.js +65 -0
  61. package/dist/internal/otel/tracer.js.map +1 -0
  62. package/dist/internal/reqtrack/mod.d.ts +6 -0
  63. package/dist/internal/reqtrack/mod.js +28 -0
  64. package/dist/internal/reqtrack/mod.js.map +1 -0
  65. package/dist/internal/runtime/meter.d.ts +11 -0
  66. package/dist/internal/runtime/meter.js +22 -0
  67. package/dist/internal/runtime/meter.js.map +1 -0
  68. package/dist/internal/runtime/mod.d.ts +12 -0
  69. package/dist/internal/runtime/mod.js +28 -0
  70. package/dist/internal/runtime/mod.js.map +1 -0
  71. package/dist/internal/runtime/napi/napi.cjs +122 -0
  72. package/dist/internal/runtime/napi/napi.d.cts +601 -0
  73. package/dist/internal/runtime/shutdown.d.ts +13 -0
  74. package/dist/internal/runtime/shutdown.js +56 -0
  75. package/dist/internal/runtime/shutdown.js.map +1 -0
  76. package/dist/internal/utils/constraints.d.ts +9 -0
  77. package/dist/internal/utils/constraints.js +2 -0
  78. package/dist/internal/utils/constraints.js.map +1 -0
  79. package/dist/log/index.d.ts +39 -0
  80. package/dist/log/index.js +44 -0
  81. package/dist/log/index.js.map +1 -0
  82. package/dist/log/logger.d.ts +123 -0
  83. package/dist/log/logger.js +203 -0
  84. package/dist/log/logger.js.map +1 -0
  85. package/dist/log/runtime.d.ts +19 -0
  86. package/dist/log/runtime.js +22 -0
  87. package/dist/log/runtime.js.map +1 -0
  88. package/dist/log/stderr.d.ts +9 -0
  89. package/dist/log/stderr.js +25 -0
  90. package/dist/log/stderr.js.map +1 -0
  91. package/dist/metrics/mod.d.ts +315 -0
  92. package/dist/metrics/mod.js +488 -0
  93. package/dist/metrics/mod.js.map +1 -0
  94. package/dist/mod.d.ts +6 -0
  95. package/dist/mod.js +4 -0
  96. package/dist/mod.js.map +1 -0
  97. package/dist/otel.d.ts +9 -0
  98. package/dist/otel.js +13 -0
  99. package/dist/otel.js.map +1 -0
  100. package/dist/pubsub/encoding.d.ts +3 -0
  101. package/dist/pubsub/encoding.js +10 -0
  102. package/dist/pubsub/encoding.js.map +1 -0
  103. package/dist/pubsub/mod.d.ts +6 -0
  104. package/dist/pubsub/mod.js +3 -0
  105. package/dist/pubsub/mod.js.map +1 -0
  106. package/dist/pubsub/refs.d.ts +12 -0
  107. package/dist/pubsub/refs.js +11 -0
  108. package/dist/pubsub/refs.js.map +1 -0
  109. package/dist/pubsub/subscription.d.ts +128 -0
  110. package/dist/pubsub/subscription.js +39 -0
  111. package/dist/pubsub/subscription.js.map +1 -0
  112. package/dist/pubsub/topic.d.ts +148 -0
  113. package/dist/pubsub/topic.js +164 -0
  114. package/dist/pubsub/topic.js.map +1 -0
  115. package/dist/req_meta.d.ts +47 -0
  116. package/dist/req_meta.js +26 -0
  117. package/dist/req_meta.js.map +1 -0
  118. package/dist/services/index.d.ts +7 -0
  119. package/dist/services/index.js +17 -0
  120. package/dist/services/index.js.map +1 -0
  121. package/dist/shutdown/mod.d.ts +23 -0
  122. package/dist/shutdown/mod.js +24 -0
  123. package/dist/shutdown/mod.js.map +1 -0
  124. package/dist/storage/objects/bucket.d.ts +216 -0
  125. package/dist/storage/objects/bucket.js +123 -0
  126. package/dist/storage/objects/bucket.js.map +1 -0
  127. package/dist/storage/objects/error.d.ts +14 -0
  128. package/dist/storage/objects/error.js +81 -0
  129. package/dist/storage/objects/error.js.map +1 -0
  130. package/dist/storage/objects/mod.d.ts +4 -0
  131. package/dist/storage/objects/mod.js +3 -0
  132. package/dist/storage/objects/mod.js.map +1 -0
  133. package/dist/storage/objects/refs.d.ts +31 -0
  134. package/dist/storage/objects/refs.js +20 -0
  135. package/dist/storage/objects/refs.js.map +1 -0
  136. package/dist/storage/postgres/database.d.ts +295 -0
  137. package/dist/storage/postgres/database.js +378 -0
  138. package/dist/storage/postgres/database.js.map +1 -0
  139. package/dist/storage/postgres/mod.d.ts +2 -0
  140. package/dist/storage/postgres/mod.js +2 -0
  141. package/dist/storage/postgres/mod.js.map +1 -0
  142. package/dist/storage/redis/commands.d.ts +573 -0
  143. package/dist/storage/redis/commands.js +637 -0
  144. package/dist/storage/redis/commands.js.map +1 -0
  145. package/dist/storage/redis/database.d.ts +302 -0
  146. package/dist/storage/redis/database.js +336 -0
  147. package/dist/storage/redis/database.js.map +1 -0
  148. package/dist/storage/redis/mod.d.ts +88 -0
  149. package/dist/storage/redis/mod.js +84 -0
  150. package/dist/storage/redis/mod.js.map +1 -0
  151. package/dist/storage/redis/native.d.ts +8 -0
  152. package/dist/storage/redis/native.js +11 -0
  153. package/dist/storage/redis/native.js.map +1 -0
  154. package/dist/storage/redis/options.d.ts +89 -0
  155. package/dist/storage/redis/options.js +53 -0
  156. package/dist/storage/redis/options.js.map +1 -0
  157. package/dist/storage/redis/pubsub.d.ts +53 -0
  158. package/dist/storage/redis/pubsub.js +88 -0
  159. package/dist/storage/redis/pubsub.js.map +1 -0
  160. package/dist/storage/redis/reply.d.ts +256 -0
  161. package/dist/storage/redis/reply.js +572 -0
  162. package/dist/storage/redis/reply.js.map +1 -0
  163. package/dist/storage/redis/scan.d.ts +11 -0
  164. package/dist/storage/redis/scan.js +76 -0
  165. package/dist/storage/redis/scan.js.map +1 -0
  166. package/dist/storage/redis/script.d.ts +91 -0
  167. package/dist/storage/redis/script.js +143 -0
  168. package/dist/storage/redis/script.js.map +1 -0
  169. package/dist/storage/redis/target.d.ts +47 -0
  170. package/dist/storage/redis/target.js +82 -0
  171. package/dist/storage/redis/target.js.map +1 -0
  172. package/dist/storage/redis/wire.d.ts +105 -0
  173. package/dist/storage/redis/wire.js +333 -0
  174. package/dist/storage/redis/wire.js.map +1 -0
  175. package/dist/tsconfig.build.tsbuildinfo +1 -0
  176. package/dist/types/mod.d.ts +42 -0
  177. package/dist/types/mod.js +65 -0
  178. package/dist/types/mod.js.map +1 -0
  179. package/internal/otel/backend.ts +60 -0
  180. package/internal/otel/context.test.ts +70 -0
  181. package/internal/otel/context.ts +48 -0
  182. package/internal/otel/context_manager.test.ts +154 -0
  183. package/internal/otel/context_manager.ts +141 -0
  184. package/internal/otel/encode.test.ts +266 -0
  185. package/internal/otel/encode.ts +408 -0
  186. package/internal/otel/index.ts +20 -0
  187. package/internal/otel/meter.test.ts +231 -0
  188. package/internal/otel/meter.ts +332 -0
  189. package/internal/otel/propagator.test.ts +105 -0
  190. package/internal/otel/propagator.ts +68 -0
  191. package/internal/otel/provider.ts +23 -0
  192. package/internal/otel/request_context.ts +32 -0
  193. package/internal/otel/span.ts +252 -0
  194. package/internal/otel/tracer.test.ts +352 -0
  195. package/internal/otel/tracer.ts +104 -0
  196. package/internal/reqtrack/mod.ts +39 -0
  197. package/internal/runtime/meter.ts +26 -0
  198. package/internal/runtime/mod.ts +41 -0
  199. package/internal/runtime/napi/napi.cjs +122 -0
  200. package/internal/runtime/napi/napi.d.cts +601 -0
  201. package/internal/runtime/shutdown.test.ts +248 -0
  202. package/internal/runtime/shutdown.ts +64 -0
  203. package/internal/utils/constraints.ts +12 -0
  204. package/log/index.ts +107 -0
  205. package/log/logger.test.ts +194 -0
  206. package/log/logger.ts +308 -0
  207. package/log/runtime.test.ts +65 -0
  208. package/log/runtime.ts +40 -0
  209. package/log/stderr.test.ts +23 -0
  210. package/log/stderr.ts +24 -0
  211. package/metrics/export.test.ts +337 -0
  212. package/metrics/mod.test.ts +180 -0
  213. package/metrics/mod.ts +619 -0
  214. package/mod.ts +22 -0
  215. package/otel.test.ts +84 -0
  216. package/otel.ts +17 -0
  217. package/package.json +121 -5
  218. package/pubsub/encoding.ts +8 -0
  219. package/pubsub/mod.ts +14 -0
  220. package/pubsub/pubsub.test.ts +186 -0
  221. package/pubsub/refs.ts +22 -0
  222. package/pubsub/subscription.ts +180 -0
  223. package/pubsub/topic.test.ts +121 -0
  224. package/pubsub/topic.ts +212 -0
  225. package/req_meta.ts +75 -0
  226. package/services/index.test.ts +30 -0
  227. package/services/index.ts +16 -0
  228. package/shutdown/mod.ts +24 -0
  229. package/storage/objects/bucket.ts +338 -0
  230. package/storage/objects/error.ts +94 -0
  231. package/storage/objects/mod.ts +35 -0
  232. package/storage/objects/refs.ts +69 -0
  233. package/storage/postgres/database.integration.test.ts +175 -0
  234. package/storage/postgres/database.test.ts +241 -0
  235. package/storage/postgres/database.ts +497 -0
  236. package/storage/postgres/mod.ts +15 -0
  237. package/storage/redis/commands.ts +1221 -0
  238. package/storage/redis/database.ts +466 -0
  239. package/storage/redis/mod.ts +135 -0
  240. package/storage/redis/native.ts +11 -0
  241. package/storage/redis/options.ts +119 -0
  242. package/storage/redis/pubsub.ts +92 -0
  243. package/storage/redis/redis.test.ts +706 -0
  244. package/storage/redis/reply.ts +705 -0
  245. package/storage/redis/runtime.test.ts +233 -0
  246. package/storage/redis/scan.ts +125 -0
  247. package/storage/redis/script.ts +162 -0
  248. package/storage/redis/target.ts +131 -0
  249. package/storage/redis/wire.ts +369 -0
  250. package/types/mod.ts +92 -0
@@ -0,0 +1,241 @@
1
+ import {beforeEach, expect, mock, test} from 'bun:test';
2
+
3
+ let rows: Record<string, unknown>[];
4
+ let affected: bigint;
5
+ let reads: number;
6
+ let closes: number;
7
+ let failAt: number;
8
+ let conversionFails: boolean;
9
+ let calls: {sql: string; params: unknown[]}[];
10
+ let committed: boolean;
11
+ let rolledBack: boolean;
12
+ let transactionClosed: boolean;
13
+ let transactionBusy: boolean;
14
+ let connectionClosed: boolean;
15
+
16
+ const transaction = {
17
+ query,
18
+ isClosed: () => transactionClosed,
19
+ async commit() {
20
+ if (transactionBusy) throw new Error('busy');
21
+ committed = true;
22
+ transactionClosed = true;
23
+ },
24
+ async rollback() {
25
+ if (transactionBusy) throw new Error('busy');
26
+ rolledBack = true;
27
+ transactionClosed = true;
28
+ },
29
+ };
30
+ const connection = {
31
+ query,
32
+ async begin() {
33
+ return transaction;
34
+ },
35
+ async close() {
36
+ connectionClosed = true;
37
+ },
38
+ };
39
+ let accesses: number[];
40
+ const database = {
41
+ query,
42
+ connString: () => 'postgres://test',
43
+ async acquire() {
44
+ return connection;
45
+ },
46
+ async begin() {
47
+ return transaction;
48
+ },
49
+ withAccess(access: number) {
50
+ accesses.push(access);
51
+ return {...database, connString: () => `postgres://access-${access}`};
52
+ },
53
+ };
54
+ const SQLAccess = {ReadWrite: 0, Read: 1, ReadPrimary: 2};
55
+ async function query(sql: string, args: {params: unknown[]}) {
56
+ calls.push({sql, params: args.params});
57
+ let index = 0;
58
+ return {
59
+ async next() {
60
+ reads++;
61
+ if (index === failAt) throw new Error('query failed');
62
+ if (index === rows.length) return null;
63
+ const row = rows[index++];
64
+ return {
65
+ values() {
66
+ if (conversionFails) throw new Error('invalid column');
67
+ return row;
68
+ },
69
+ };
70
+ },
71
+ async rowsAffected() {
72
+ if (reads <= rows.length) throw new Error('query not drained');
73
+ return affected;
74
+ },
75
+ async close() {
76
+ closes++;
77
+ },
78
+ };
79
+ }
80
+ mock.module('../../internal/runtime/mod', () => ({
81
+ RT: {sqlDatabase: () => database},
82
+ QueryArgs: class {
83
+ constructor(public params: unknown[]) {}
84
+ },
85
+ SQLAccess,
86
+ }));
87
+ mock.module('../../internal/otel/index', () => ({
88
+ activeCtxBytes: () => undefined,
89
+ }));
90
+ const {Database, DatabaseRef, Queryable} = await import('./mod');
91
+
92
+ beforeEach(() => {
93
+ rows = [];
94
+ affected = 0n;
95
+ reads = 0;
96
+ closes = 0;
97
+ failAt = -1;
98
+ conversionFails = false;
99
+ calls = [];
100
+ committed = false;
101
+ rolledBack = false;
102
+ transactionClosed = false;
103
+ transactionBusy = false;
104
+ connectionClosed = false;
105
+ accesses = [];
106
+ });
107
+
108
+ test('access handles are queryable; declarations are not', async () => {
109
+ const db = new Database('orders');
110
+ expect(db instanceof Queryable).toBe(false);
111
+ for (const handle of [
112
+ db.read(),
113
+ db.read({primary: true}),
114
+ db.write(),
115
+ db.admin(),
116
+ ]) {
117
+ expect(handle instanceof DatabaseRef).toBe(true);
118
+ expect(handle instanceof Queryable).toBe(true);
119
+ await handle.rawQueryAll('SELECT 1');
120
+ }
121
+ expect(calls).toHaveLength(4);
122
+ });
123
+
124
+ test("a handle's connection string is its access's", () => {
125
+ const db = new Database('orders');
126
+ expect(db.connectionString).toBe('postgres://test');
127
+ expect(db.write().connectionString).toBe('postgres://test');
128
+ expect(db.admin().connectionString).toBe('postgres://test');
129
+ expect(db.read().connectionString).toBe(
130
+ `postgres://access-${SQLAccess.Read}`,
131
+ );
132
+ expect(db.read({primary: true}).connectionString).toBe(
133
+ `postgres://access-${SQLAccess.ReadPrimary}`,
134
+ );
135
+ });
136
+
137
+ test("read handles run with read access; write and admin handles with the declaration's", () => {
138
+ const db = new Database('orders');
139
+ db.write();
140
+ db.admin();
141
+ expect(accesses).toEqual([]);
142
+ const reader = db.read();
143
+ const primaryReader = db.read({primary: true});
144
+ expect(accesses).toEqual([SQLAccess.Read, SQLAccess.ReadPrimary]);
145
+ // Made once each.
146
+ expect(db.read()).toBe(reader);
147
+ expect(db.read({primary: true})).toBe(primaryReader);
148
+ expect(accesses).toHaveLength(2);
149
+ });
150
+
151
+ test('tagged queries bind values and normalize undefined', async () => {
152
+ const db = new Database('orders');
153
+ rows = [{id: 1}];
154
+ expect(
155
+ await db.read().queryAll`SELECT ${'user input'}, ${undefined}`,
156
+ ).toEqual(rows);
157
+ expect(calls).toEqual([{sql: 'SELECT $1, $2', params: ['user input', null]}]);
158
+ expect(closes).toBe(1);
159
+ });
160
+
161
+ test('single-row queries accept zero or one and reject multiple rows', async () => {
162
+ const db = new Database('orders');
163
+ expect(await db.queryRow`SELECT 1`).toBeNull();
164
+ rows = [{id: 1}];
165
+ expect(await db.rawQueryRow('SELECT 1')).toEqual({id: 1});
166
+ rows = [{id: 1}, {id: 2}];
167
+ await expect(db.queryRow`SELECT id`).rejects.toThrow('more than one row');
168
+ expect(closes).toBe(3);
169
+ });
170
+
171
+ test('exec drains RETURNING rows and preserves exact affected counts', async () => {
172
+ const db = new Database('orders');
173
+ rows = [{id: 1}, {id: 2}];
174
+ affected = 9007199254740993n;
175
+ expect(
176
+ await db.write().exec`UPDATE orders SET paid = ${true} RETURNING id`,
177
+ ).toEqual({rowsAffected: affected});
178
+ expect(reads).toBe(3);
179
+ expect(closes).toBe(1);
180
+ });
181
+
182
+ test('exec propagates errors after returned rows and closes the cursor', async () => {
183
+ rows = [{id: 1}];
184
+ failAt = 1;
185
+ await expect(
186
+ new Database('orders').rawExec('UPDATE orders RETURNING id'),
187
+ ).rejects.toThrow('query failed');
188
+ expect(closes).toBe(1);
189
+ });
190
+
191
+ test('early iteration exit and column conversion failures close cursors', async () => {
192
+ const db = new Database('orders');
193
+ rows = [{id: 1}, {id: 2}];
194
+ for await (const _row of db.query`SELECT id`) break;
195
+ expect(closes).toBe(1);
196
+ conversionFails = true;
197
+ await expect(db.rawQueryAll('SELECT id')).rejects.toThrow('invalid column');
198
+ expect(closes).toBe(2);
199
+ });
200
+
201
+ test('connections begin queryable transactions and dispose explicitly', async () => {
202
+ const conn = await new Database('orders').write().acquire();
203
+ const tx = await conn.begin();
204
+ expect(conn instanceof Queryable).toBe(true);
205
+ expect(tx instanceof Queryable).toBe(true);
206
+ await tx.rawExec('UPDATE orders');
207
+ await tx.commit();
208
+ await tx[Symbol.asyncDispose]();
209
+ expect(committed).toBe(true);
210
+ expect(rolledBack).toBe(false);
211
+ await conn[Symbol.asyncDispose]();
212
+ expect(connectionClosed).toBe(true);
213
+ });
214
+
215
+ test('a failed busy commit does not disable automatic rollback', async () => {
216
+ const tx = await new Database('orders').begin();
217
+ transactionBusy = true;
218
+ await expect(tx.commit()).rejects.toThrow('busy');
219
+ transactionBusy = false;
220
+ await tx[Symbol.asyncDispose]();
221
+ expect(rolledBack).toBe(true);
222
+ });
223
+
224
+ test('await using rolls back on normal exit and exceptions, then closes the connection', async () => {
225
+ async function scope(fail: boolean) {
226
+ await using conn = await new Database('orders').acquire();
227
+ await using tx = await conn.begin();
228
+ await tx.rawExec('UPDATE orders');
229
+ if (fail) throw new Error('application error');
230
+ }
231
+ await scope(false);
232
+ expect(rolledBack).toBe(true);
233
+ expect(connectionClosed).toBe(true);
234
+ expect(committed).toBe(false);
235
+ rolledBack = false;
236
+ connectionClosed = false;
237
+ transactionClosed = false;
238
+ await expect(scope(true)).rejects.toThrow('application error');
239
+ expect(rolledBack).toBe(true);
240
+ expect(connectionClosed).toBe(true);
241
+ });