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

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 +12 -0
  119. package/dist/services/index.js +22 -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 +21 -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,369 @@
1
+ /**
2
+ * Commands as they cross to the native runtime, and replies as they come back:
3
+ * the encoding `encore_sdk_core::cache::wire` documents. A command marks which
4
+ * of its arguments are keys, which is what lets the runtime prefix them with
5
+ * the database's own.
6
+ *
7
+ * @internal
8
+ * @module
9
+ */
10
+
11
+ /** A value sent to Redis as an argument: text, bytes, or a number. */
12
+ export type RedisArg = string | number | bigint | Buffer | Uint8Array;
13
+
14
+ /** A key and its value, or a field and its value, in the order given. */
15
+ export type Pairs<V> = Record<string, V> | ReadonlyMap<string, V>;
16
+
17
+ /** One argument of a command. */
18
+ interface Arg {
19
+ key: boolean;
20
+ value: Buffer;
21
+ }
22
+
23
+ /** A command being built: the arguments in order, the keys among them marked. */
24
+ export class Cmd {
25
+ readonly args: Arg[] = [];
26
+ /**
27
+ * Whether the reply names keys, which then carry the database's prefix: a
28
+ * stream read's, which says which stream each entry came from.
29
+ */
30
+ replyKeys = false;
31
+
32
+ /** A command Redis calls `name`, traced as `operation`. */
33
+ constructor(
34
+ readonly operation: string,
35
+ readonly name: string,
36
+ readonly write: boolean,
37
+ ) {}
38
+
39
+ /** Appends a key: an argument the database's key prefix applies to. */
40
+ key(key: string): this {
41
+ this.args.push({key: true, value: Buffer.from(key, 'utf8')});
42
+ return this;
43
+ }
44
+
45
+ /** Appends several keys. */
46
+ keys(keys: readonly string[]): this {
47
+ for (const key of keys) this.key(key);
48
+ return this;
49
+ }
50
+
51
+ /**
52
+ * Appends keys preceded by how many there are, as the commands that take a
53
+ * variable number of keys before their other arguments want them.
54
+ */
55
+ numkeys(keys: readonly string[]): this {
56
+ return this.arg(keys.length).keys(keys);
57
+ }
58
+
59
+ /** Appends an argument that is not a key. */
60
+ arg(value: RedisArg): this {
61
+ this.args.push({key: false, value: toArg(value, this.operation)});
62
+ return this;
63
+ }
64
+
65
+ /** Appends several arguments that are not keys. */
66
+ argsOf(values: readonly RedisArg[]): this {
67
+ for (const value of values) this.arg(value);
68
+ return this;
69
+ }
70
+
71
+ /** Appends each key and its value: `MSET`'s. */
72
+ pairs(items: Pairs<RedisArg>): this {
73
+ for (const [key, value] of entries(items)) this.key(key).arg(value);
74
+ return this;
75
+ }
76
+
77
+ /** Appends each field and its value, neither a key. */
78
+ fields(items: Pairs<RedisArg>): this {
79
+ for (const [field, value] of entries(items)) this.arg(field).arg(value);
80
+ return this;
81
+ }
82
+
83
+ /** Appends each member's score, then the member: `ZADD`'s. */
84
+ scores(items: Pairs<number>): this {
85
+ for (const [member, score] of entries(items)) this.arg(score).arg(member);
86
+ return this;
87
+ }
88
+
89
+ /** Marks the reply as naming keys. */
90
+ withReplyKeys(): this {
91
+ this.replyKeys = true;
92
+ return this;
93
+ }
94
+
95
+ /** A copy, to append more arguments to. */
96
+ clone(): Cmd {
97
+ const out = new Cmd(this.operation, this.name, this.write);
98
+ out.args.push(...this.args);
99
+ out.replyKeys = this.replyKeys;
100
+ return out;
101
+ }
102
+ }
103
+
104
+ function entries<V>(items: Pairs<V>): Iterable<[string, V]> {
105
+ return items instanceof Map ? items : Object.entries(items);
106
+ }
107
+
108
+ /**
109
+ * `value` as the bytes Redis is sent. Bytes are copied: a queued command is
110
+ * encoded only when its batch runs, and must send what it was given.
111
+ */
112
+ export function toArg(value: unknown, operation: string): Buffer {
113
+ if (typeof value === 'string') return Buffer.from(value, 'utf8');
114
+ if (value instanceof Uint8Array) return Buffer.from(value);
115
+ if (typeof value === 'bigint') return Buffer.from(value.toString(), 'ascii');
116
+ if (typeof value === 'number') {
117
+ // Redis spells the infinities its own way, and has no NaN.
118
+ if (value === Infinity) return Buffer.from('+inf');
119
+ if (value === -Infinity) return Buffer.from('-inf');
120
+ if (Number.isNaN(value)) {
121
+ throw new TypeError(
122
+ `redis: ${operation}: cannot send NaN as an argument`,
123
+ );
124
+ }
125
+ return Buffer.from(String(value), 'ascii');
126
+ }
127
+ throw new TypeError(
128
+ `redis: ${operation}: cannot send ${describe(value)} as an argument: ` +
129
+ `give a string, a number, a bigint, or a Buffer`,
130
+ );
131
+ }
132
+
133
+ function describe(value: unknown): string {
134
+ if (value === null) return 'null';
135
+ if (Array.isArray(value)) return 'an array';
136
+ return `a ${typeof value}`;
137
+ }
138
+
139
+ /** Writes the encodings, sized ahead so that each is one allocation. */
140
+ class Writer {
141
+ private buf: Buffer;
142
+ private at = 0;
143
+
144
+ constructor(size: number) {
145
+ this.buf = Buffer.allocUnsafe(size);
146
+ }
147
+
148
+ u8(n: number) {
149
+ this.buf.writeUInt8(n, this.at);
150
+ this.at += 1;
151
+ }
152
+
153
+ u32(n: number) {
154
+ this.buf.writeUInt32LE(n, this.at);
155
+ this.at += 4;
156
+ }
157
+
158
+ bytes(b: Buffer) {
159
+ this.u32(b.length);
160
+ b.copy(this.buf, this.at);
161
+ this.at += b.length;
162
+ }
163
+
164
+ done(): Buffer {
165
+ return this.buf;
166
+ }
167
+ }
168
+
169
+ function commandSize(cmd: Cmd): number {
170
+ let size =
171
+ 1 +
172
+ 4 +
173
+ Buffer.byteLength(cmd.operation) +
174
+ 4 +
175
+ Buffer.byteLength(cmd.name) +
176
+ 4;
177
+ for (const arg of cmd.args) size += 1 + 4 + arg.value.length;
178
+ return size;
179
+ }
180
+
181
+ function writeCommand(w: Writer, cmd: Cmd) {
182
+ w.u8(cmd.write ? 1 : 0);
183
+ w.bytes(Buffer.from(cmd.operation, 'utf8'));
184
+ w.bytes(Buffer.from(cmd.name, 'utf8'));
185
+ w.u32(cmd.args.length);
186
+ for (const arg of cmd.args) {
187
+ w.u8(arg.key ? 1 : 0);
188
+ w.bytes(arg.value);
189
+ }
190
+ }
191
+
192
+ /** The encoding of one command. */
193
+ export function encodeCommand(cmd: Cmd): Buffer {
194
+ const w = new Writer(commandSize(cmd));
195
+ writeCommand(w, cmd);
196
+ return w.done();
197
+ }
198
+
199
+ /** The encoding of a batch of commands. */
200
+ export function encodeBatch(cmds: readonly Cmd[]): Buffer {
201
+ let size = 4;
202
+ for (const cmd of cmds) size += commandSize(cmd);
203
+ const w = new Writer(size);
204
+ w.u32(cmds.length);
205
+ for (const cmd of cmds) writeCommand(w, cmd);
206
+ return w.done();
207
+ }
208
+
209
+ /** The encoding of a list of keys or channels. */
210
+ export function encodeList(items: readonly string[]): Buffer {
211
+ const bufs = items.map(item => Buffer.from(item, 'utf8'));
212
+ const w = new Writer(4 + bufs.reduce((n, b) => n + 4 + b.length, 0));
213
+ w.u32(bufs.length);
214
+ for (const b of bufs) w.bytes(b);
215
+ return w.done();
216
+ }
217
+
218
+ /**
219
+ * The error a command the server rejected fails with.
220
+ */
221
+ export class ServerError extends Error {
222
+ /**
223
+ * The server's message, as Redis spells it:
224
+ * `WRONGTYPE Operation against a key holding the wrong kind of value`.
225
+ */
226
+ readonly serverMessage: string;
227
+
228
+ constructor(message: string) {
229
+ super(`redis: ${message}`);
230
+ this.serverMessage = message;
231
+ Object.defineProperty(this, 'name', {
232
+ value: 'ServerError',
233
+ enumerable: false,
234
+ configurable: true,
235
+ });
236
+ Object.setPrototypeOf(this, ServerError.prototype);
237
+ }
238
+
239
+ /** The error's code: the first word of its message, `WRONGTYPE`. */
240
+ get code(): string {
241
+ const space = this.serverMessage.indexOf(' ');
242
+ return space < 0 ? this.serverMessage : this.serverMessage.slice(0, space);
243
+ }
244
+ }
245
+
246
+ /** A map reply, its keys and values in the order the server sent them. */
247
+ export class MapReply {
248
+ constructor(readonly entries: [Value, Value][]) {}
249
+ }
250
+
251
+ /** A simple string reply, `OK` among them. */
252
+ export class Status {
253
+ constructor(readonly text: string) {}
254
+ }
255
+
256
+ /**
257
+ * A reply as it was decoded: `null` (nil), a `bigint` (an integer, or a big
258
+ * number), a `number` (a double), a `boolean`, a `Buffer` (a bulk string), a
259
+ * {@link Status}, an array (an array or a set), {@link MapReply} (a map), or a
260
+ * {@link ServerError}.
261
+ */
262
+ export type Value =
263
+ | null
264
+ | bigint
265
+ | number
266
+ | boolean
267
+ | Buffer
268
+ | Status
269
+ | Value[]
270
+ | MapReply
271
+ | ServerError;
272
+
273
+ const malformed = () => new Error('redis: malformed reply');
274
+
275
+ /** Decodes an encoded reply. */
276
+ export function decodeReply(buf: Buffer): Value {
277
+ let at = 0;
278
+ const need = (n: number) => {
279
+ if (at + n > buf.length) throw malformed();
280
+ };
281
+ const u32 = () => {
282
+ need(4);
283
+ const n = buf.readUInt32LE(at);
284
+ at += 4;
285
+ return n;
286
+ };
287
+ const bytes = () => {
288
+ const n = u32();
289
+ need(n);
290
+ // A copy: the reply's buffer is not the caller's to keep alive.
291
+ const out = Buffer.from(buf.subarray(at, at + n));
292
+ at += n;
293
+ return out;
294
+ };
295
+ const value = (): Value => {
296
+ need(1);
297
+ const tag = buf.readUInt8(at);
298
+ at += 1;
299
+ switch (tag) {
300
+ case 0:
301
+ return null;
302
+ case 1: {
303
+ need(8);
304
+ const n = buf.readBigInt64LE(at);
305
+ at += 8;
306
+ return n;
307
+ }
308
+ case 2:
309
+ return bytes();
310
+ case 3:
311
+ return new Status(bytes().toString('utf8'));
312
+ case 4:
313
+ case 6: {
314
+ const n = u32();
315
+ const items: Value[] = [];
316
+ for (let i = 0; i < n; i++) items.push(value());
317
+ return items;
318
+ }
319
+ case 5: {
320
+ const n = u32();
321
+ const pairs: [Value, Value][] = [];
322
+ for (let i = 0; i < n; i++) pairs.push([value(), value()]);
323
+ return new MapReply(pairs);
324
+ }
325
+ case 7: {
326
+ need(8);
327
+ const n = buf.readDoubleLE(at);
328
+ at += 8;
329
+ return n;
330
+ }
331
+ case 8: {
332
+ need(1);
333
+ const b = buf.readUInt8(at) !== 0;
334
+ at += 1;
335
+ return b;
336
+ }
337
+ case 9:
338
+ return new ServerError(bytes().toString('utf8'));
339
+ case 10:
340
+ return BigInt(bytes().toString('ascii'));
341
+ default:
342
+ throw malformed();
343
+ }
344
+ };
345
+ const out = value();
346
+ if (at !== buf.length) throw malformed();
347
+ return out;
348
+ }
349
+
350
+ /**
351
+ * Removes the database's key prefix from the stream names in a stream read's
352
+ * reply, which is a list of (or a map from) stream name to entries.
353
+ */
354
+ export function stripReplyKeys(v: Value, prefix: string | undefined) {
355
+ if (!prefix) return;
356
+ const p = Buffer.from(prefix, 'utf8');
357
+ const strip = (name: Value): Value =>
358
+ Buffer.isBuffer(name) && name.subarray(0, p.length).equals(p)
359
+ ? name.subarray(p.length)
360
+ : name;
361
+ if (Array.isArray(v)) {
362
+ for (const stream of v) {
363
+ if (Array.isArray(stream) && stream.length > 0)
364
+ stream[0] = strip(stream[0]);
365
+ }
366
+ } else if (v instanceof MapReply) {
367
+ for (const pair of v.entries) pair[0] = strip(pair[0]);
368
+ }
369
+ }
package/types/mod.ts ADDED
@@ -0,0 +1,92 @@
1
+ import * as runtime from '../internal/runtime/mod';
2
+
3
+ type durationUnit = 'ns' | 'µs' | 'ms' | 's' | 'm' | 'h';
4
+ type durationComponent = `${number}${durationUnit}`;
5
+
6
+ /**
7
+ * A duration is a string representing a length of time.
8
+ *
9
+ * Examples: `"10s"`, `"500ms"`, `"5m"`, `"1h30m"`, `"1h 30m"`.
10
+ */
11
+ export type DurationString =
12
+ | durationComponent
13
+ | `${durationComponent}${durationComponent}`
14
+ | `${durationComponent} ${durationComponent}`;
15
+
16
+ export type ToDecimal = string | number | bigint;
17
+
18
+ /**
19
+ * A decimal type that can hold values with arbitrary precision.
20
+ * Unlike JavaScript's native number type, this can accurately represent
21
+ * decimal values without floating-point precision errors.
22
+ */
23
+ export class Decimal {
24
+ private impl: runtime.Decimal;
25
+
26
+ constructor(value: ToDecimal) {
27
+ this.impl = new runtime.Decimal(String(value));
28
+ }
29
+
30
+ private static fromImpl(impl: runtime.Decimal): Decimal {
31
+ const d = Object.create(Decimal.prototype);
32
+ d.impl = impl;
33
+ return d;
34
+ }
35
+
36
+ private toImpl(value: Decimal | ToDecimal): runtime.Decimal {
37
+ return value instanceof Decimal
38
+ ? value.impl
39
+ : new runtime.Decimal(String(value));
40
+ }
41
+
42
+ /**
43
+ * Adds this decimal to another decimal value.
44
+ */
45
+ add(d: Decimal | ToDecimal): Decimal {
46
+ return Decimal.fromImpl(this.impl.add(this.toImpl(d)));
47
+ }
48
+
49
+ /**
50
+ * Subtracts another decimal value from this decimal.
51
+ */
52
+ sub(d: Decimal | ToDecimal): Decimal {
53
+ return Decimal.fromImpl(this.impl.sub(this.toImpl(d)));
54
+ }
55
+
56
+ /**
57
+ * Multiplies this decimal by another decimal value.
58
+ */
59
+ mul(d: Decimal | ToDecimal): Decimal {
60
+ return Decimal.fromImpl(this.impl.mul(this.toImpl(d)));
61
+ }
62
+
63
+ /**
64
+ * Divides this decimal by another decimal value.
65
+ */
66
+ div(d: Decimal | ToDecimal): Decimal {
67
+ return Decimal.fromImpl(this.impl.div(this.toImpl(d)));
68
+ }
69
+
70
+ get value(): string {
71
+ return this.impl.toString();
72
+ }
73
+
74
+ toJSON(): string {
75
+ return this.impl.toString();
76
+ }
77
+ toString(): string {
78
+ return this.impl.toString();
79
+ }
80
+
81
+ [Symbol.toPrimitive](hint: string) {
82
+ if (hint === 'number') {
83
+ return +this.value;
84
+ }
85
+
86
+ return this.value;
87
+ }
88
+
89
+ private get __encore_decimal(): boolean {
90
+ return true;
91
+ }
92
+ }