viem 2.57.15 → 2.57.16

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 (111) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/_cjs/errors/version.js +1 -1
  3. package/_cjs/tempo/Gateway.js +195 -0
  4. package/_cjs/tempo/Gateway.js.map +1 -0
  5. package/_cjs/tempo/GatewayDecorator.js +168 -0
  6. package/_cjs/tempo/GatewayDecorator.js.map +1 -0
  7. package/_cjs/tempo/actions/ai.js +69 -0
  8. package/_cjs/tempo/actions/ai.js.map +1 -0
  9. package/_cjs/tempo/actions/bucket.js +85 -0
  10. package/_cjs/tempo/actions/bucket.js.map +1 -0
  11. package/_cjs/tempo/actions/compute.js +165 -0
  12. package/_cjs/tempo/actions/compute.js.map +1 -0
  13. package/_cjs/tempo/actions/gateway.js +8 -0
  14. package/_cjs/tempo/actions/gateway.js.map +1 -0
  15. package/_cjs/tempo/actions/index.js +6 -1
  16. package/_cjs/tempo/actions/index.js.map +1 -1
  17. package/_cjs/tempo/actions/kv.js +44 -0
  18. package/_cjs/tempo/actions/kv.js.map +1 -0
  19. package/_cjs/tempo/index.js +4 -1
  20. package/_cjs/tempo/index.js.map +1 -1
  21. package/_cjs/tempo/internal/ndjson.js +43 -0
  22. package/_cjs/tempo/internal/ndjson.js.map +1 -0
  23. package/_cjs/utils/buildRequest.js +4 -0
  24. package/_cjs/utils/buildRequest.js.map +1 -1
  25. package/_cjs/utils/hash/toSignatureHash.js +10 -1
  26. package/_cjs/utils/hash/toSignatureHash.js.map +1 -1
  27. package/_cjs/utils/rpc/http.js +6 -3
  28. package/_cjs/utils/rpc/http.js.map +1 -1
  29. package/_cjs/utils/rpc/socket.js +20 -9
  30. package/_cjs/utils/rpc/socket.js.map +1 -1
  31. package/_cjs/zksync/actions/finalizeWithdrawal.js +4 -2
  32. package/_cjs/zksync/actions/finalizeWithdrawal.js.map +1 -1
  33. package/_cjs/zksync/actions/isWithdrawalFinalized.js +4 -5
  34. package/_cjs/zksync/actions/isWithdrawalFinalized.js.map +1 -1
  35. package/_esm/errors/version.js +1 -1
  36. package/_esm/tempo/Gateway.js +294 -0
  37. package/_esm/tempo/Gateway.js.map +1 -0
  38. package/_esm/tempo/GatewayDecorator.js +207 -0
  39. package/_esm/tempo/GatewayDecorator.js.map +1 -0
  40. package/_esm/tempo/actions/ai.js +132 -0
  41. package/_esm/tempo/actions/ai.js.map +1 -0
  42. package/_esm/tempo/actions/bucket.js +181 -0
  43. package/_esm/tempo/actions/bucket.js.map +1 -0
  44. package/_esm/tempo/actions/compute.js +424 -0
  45. package/_esm/tempo/actions/compute.js.map +1 -0
  46. package/_esm/tempo/actions/gateway.js +22 -0
  47. package/_esm/tempo/actions/gateway.js.map +1 -0
  48. package/_esm/tempo/actions/index.js +10 -0
  49. package/_esm/tempo/actions/index.js.map +1 -1
  50. package/_esm/tempo/actions/kv.js +124 -0
  51. package/_esm/tempo/actions/kv.js.map +1 -0
  52. package/_esm/tempo/index.js +9 -0
  53. package/_esm/tempo/index.js.map +1 -1
  54. package/_esm/tempo/internal/ndjson.js +45 -0
  55. package/_esm/tempo/internal/ndjson.js.map +1 -0
  56. package/_esm/utils/buildRequest.js +6 -0
  57. package/_esm/utils/buildRequest.js.map +1 -1
  58. package/_esm/utils/hash/toSignatureHash.js +11 -1
  59. package/_esm/utils/hash/toSignatureHash.js.map +1 -1
  60. package/_esm/utils/rpc/http.js +11 -3
  61. package/_esm/utils/rpc/http.js.map +1 -1
  62. package/_esm/utils/rpc/socket.js +24 -9
  63. package/_esm/utils/rpc/socket.js.map +1 -1
  64. package/_esm/zksync/actions/finalizeWithdrawal.js +5 -2
  65. package/_esm/zksync/actions/finalizeWithdrawal.js.map +1 -1
  66. package/_esm/zksync/actions/isWithdrawalFinalized.js +5 -5
  67. package/_esm/zksync/actions/isWithdrawalFinalized.js.map +1 -1
  68. package/_types/errors/version.d.ts +1 -1
  69. package/_types/tempo/Gateway.d.ts +337 -0
  70. package/_types/tempo/Gateway.d.ts.map +1 -0
  71. package/_types/tempo/GatewayDecorator.d.ts +635 -0
  72. package/_types/tempo/GatewayDecorator.d.ts.map +1 -0
  73. package/_types/tempo/actions/ai.d.ts +145 -0
  74. package/_types/tempo/actions/ai.d.ts.map +1 -0
  75. package/_types/tempo/actions/bucket.d.ts +210 -0
  76. package/_types/tempo/actions/bucket.d.ts.map +1 -0
  77. package/_types/tempo/actions/compute.d.ts +628 -0
  78. package/_types/tempo/actions/compute.d.ts.map +1 -0
  79. package/_types/tempo/actions/gateway.d.ts +25 -0
  80. package/_types/tempo/actions/gateway.d.ts.map +1 -0
  81. package/_types/tempo/actions/index.d.ts +10 -0
  82. package/_types/tempo/actions/index.d.ts.map +1 -1
  83. package/_types/tempo/actions/kv.d.ts +167 -0
  84. package/_types/tempo/actions/kv.d.ts.map +1 -0
  85. package/_types/tempo/index.d.ts +11 -0
  86. package/_types/tempo/index.d.ts.map +1 -1
  87. package/_types/tempo/internal/ndjson.d.ts +9 -0
  88. package/_types/tempo/internal/ndjson.d.ts.map +1 -0
  89. package/_types/utils/buildRequest.d.ts.map +1 -1
  90. package/_types/utils/hash/toSignatureHash.d.ts +1 -1
  91. package/_types/utils/hash/toSignatureHash.d.ts.map +1 -1
  92. package/_types/utils/rpc/http.d.ts.map +1 -1
  93. package/_types/utils/rpc/socket.d.ts.map +1 -1
  94. package/errors/version.ts +1 -1
  95. package/package.json +5 -1
  96. package/tempo/Gateway.ts +477 -0
  97. package/tempo/GatewayDecorator.ts +909 -0
  98. package/tempo/actions/ai.ts +250 -0
  99. package/tempo/actions/bucket.ts +324 -0
  100. package/tempo/actions/compute.ts +894 -0
  101. package/tempo/actions/gateway.ts +33 -0
  102. package/tempo/actions/index.ts +10 -0
  103. package/tempo/actions/kv.ts +229 -0
  104. package/tempo/index.ts +12 -0
  105. package/tempo/internal/ndjson.ts +51 -0
  106. package/utils/buildRequest.ts +4 -0
  107. package/utils/hash/toSignatureHash.ts +12 -2
  108. package/utils/rpc/http.ts +11 -3
  109. package/utils/rpc/socket.ts +28 -15
  110. package/zksync/actions/finalizeWithdrawal.ts +5 -2
  111. package/zksync/actions/isWithdrawalFinalized.ts +5 -5
@@ -0,0 +1,250 @@
1
+ import * as Gateway from '../Gateway.js'
2
+ import * as Ndjson from '../internal/ndjson.js'
3
+
4
+ /**
5
+ * Embeds text with a Gateway model. Paid per request.
6
+ *
7
+ * @experimental
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { Actions, Gateway } from 'viem/tempo'
12
+ *
13
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
14
+ *
15
+ * const { embeddings } = await Actions.ai.embed(gateway, {
16
+ * input: ['hello world'],
17
+ * model: '@cf/baai/bge-m3',
18
+ * })
19
+ * ```
20
+ *
21
+ * @param gateway - Gateway connection.
22
+ * @param parameters - Parameters.
23
+ * @returns The embeddings and their shape.
24
+ */
25
+ export async function embed(
26
+ gateway: Gateway.Gateway,
27
+ parameters: embed.Parameters,
28
+ ): Promise<embed.ReturnValue> {
29
+ const { input, model, signal } = parameters
30
+ const response = await Gateway.request(gateway, {
31
+ body: JSON.stringify({
32
+ input: typeof input === 'string' ? [input] : input,
33
+ model,
34
+ }),
35
+ path: '/ai/embed',
36
+ signal,
37
+ })
38
+ return (await response.json()) as embed.ReturnValue
39
+ }
40
+
41
+ export declare namespace embed {
42
+ type Parameters = {
43
+ /** Text or texts to embed. */
44
+ input: string | readonly string[]
45
+ /** Embedding model. See `Gateway.getCapabilities`. */
46
+ model: string
47
+ /** Signal to abort the request. */
48
+ signal?: AbortSignal | undefined
49
+ }
50
+
51
+ type ReturnValue = {
52
+ /** One embedding per input. */
53
+ embeddings: readonly (readonly number[])[]
54
+ /** Shape of `embeddings`. */
55
+ shape: readonly number[]
56
+ }
57
+
58
+ type ErrorType = Gateway.request.ErrorType
59
+ }
60
+
61
+ /**
62
+ * Generates text with a Gateway model. Paid per request.
63
+ *
64
+ * Pass either `prompt` or `messages`.
65
+ *
66
+ * @experimental
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import { Actions, Gateway } from 'viem/tempo'
71
+ *
72
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
73
+ *
74
+ * const { text } = await Actions.ai.generateText(gateway, {
75
+ * model: '@cf/meta/llama-3.3-70b-instruct-fp8-fast',
76
+ * prompt: 'Describe this app in one sentence.',
77
+ * })
78
+ * ```
79
+ *
80
+ * @param gateway - Gateway connection.
81
+ * @param parameters - Parameters.
82
+ * @returns The generated text and token usage.
83
+ */
84
+ export async function generateText(
85
+ gateway: Gateway.Gateway,
86
+ parameters: generateText.Parameters,
87
+ ): Promise<generateText.ReturnValue> {
88
+ const { maxOutputTokens, messages, model, prompt, signal } = parameters
89
+ const response = await Gateway.request(gateway, {
90
+ body: JSON.stringify({ maxOutputTokens, messages, model, prompt }),
91
+ path: '/ai/generate',
92
+ signal,
93
+ })
94
+ type Body = {
95
+ text: string
96
+ usage: {
97
+ completion_tokens?: number | undefined
98
+ prompt_tokens?: number | undefined
99
+ total_tokens?: number | undefined
100
+ } | null
101
+ }
102
+ const { text, usage } = (await response.json()) as Body
103
+ return {
104
+ text,
105
+ usage: usage
106
+ ? {
107
+ inputTokens: usage.prompt_tokens,
108
+ outputTokens: usage.completion_tokens,
109
+ totalTokens: usage.total_tokens,
110
+ }
111
+ : undefined,
112
+ }
113
+ }
114
+
115
+ export declare namespace generateText {
116
+ type Message = {
117
+ /** Message text. */
118
+ content: string
119
+ /** Message author. */
120
+ role: 'assistant' | 'system' | 'user'
121
+ }
122
+
123
+ type Parameters = {
124
+ /**
125
+ * Maximum generated tokens. The request reserves their cost and refunds
126
+ * what is not generated.
127
+ */
128
+ maxOutputTokens?: number | undefined
129
+ /** Generation model. See `Gateway.getCapabilities`. */
130
+ model: string
131
+ /** Signal to abort the request. */
132
+ signal?: AbortSignal | undefined
133
+ } & (
134
+ | {
135
+ /** Conversation to continue. */
136
+ messages: readonly Message[]
137
+ prompt?: undefined
138
+ }
139
+ | {
140
+ messages?: undefined
141
+ /** Prompt to complete. */
142
+ prompt: string
143
+ }
144
+ )
145
+
146
+ type ReturnValue = {
147
+ /** Generated text. */
148
+ text: string
149
+ /** Token usage, when the model reports it. */
150
+ usage:
151
+ | {
152
+ /** Prompt tokens. */
153
+ inputTokens: number | undefined
154
+ /** Generated tokens. */
155
+ outputTokens: number | undefined
156
+ /** Total tokens. */
157
+ totalTokens: number | undefined
158
+ }
159
+ | undefined
160
+ }
161
+
162
+ type ErrorType = Gateway.request.ErrorType
163
+ }
164
+
165
+ /**
166
+ * Generates text with a Gateway model and yields it as it is produced. Paid
167
+ * per request.
168
+ *
169
+ * @experimental
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * import { Actions, Gateway } from 'viem/tempo'
174
+ *
175
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
176
+ *
177
+ * for await (const event of Actions.ai.streamText(gateway, {
178
+ * model: '@cf/zai-org/glm-5.3',
179
+ * prompt: 'Write a haiku about Tempo.',
180
+ * })) {
181
+ * if (event.type === 'text') process.stdout.write(event.delta)
182
+ * }
183
+ * ```
184
+ *
185
+ * @param gateway - Gateway connection.
186
+ * @param parameters - Parameters.
187
+ * @returns Text and reasoning deltas, then usage.
188
+ */
189
+ export async function* streamText(
190
+ gateway: Gateway.Gateway,
191
+ parameters: streamText.Parameters,
192
+ ): AsyncGenerator<streamText.Event> {
193
+ const { maxOutputTokens, messages, model, prompt, signal } = parameters
194
+ const response = await Gateway.request(gateway, {
195
+ body: JSON.stringify({
196
+ maxOutputTokens,
197
+ messages,
198
+ model,
199
+ prompt,
200
+ stream: true,
201
+ }),
202
+ path: '/ai/generate',
203
+ signal,
204
+ })
205
+ type Raw =
206
+ | { delta: string; type: 'reasoning' | 'text' }
207
+ | {
208
+ type: 'done'
209
+ usage: {
210
+ completion_tokens?: number | undefined
211
+ prompt_tokens?: number | undefined
212
+ total_tokens?: number | undefined
213
+ } | null
214
+ }
215
+ for await (const event of Ndjson.read<Raw>(response)) {
216
+ if (event.type !== 'done') {
217
+ yield event
218
+ continue
219
+ }
220
+ yield {
221
+ type: 'done',
222
+ usage: event.usage
223
+ ? {
224
+ inputTokens: event.usage.prompt_tokens,
225
+ outputTokens: event.usage.completion_tokens,
226
+ totalTokens: event.usage.total_tokens,
227
+ }
228
+ : undefined,
229
+ }
230
+ }
231
+ }
232
+
233
+ export declare namespace streamText {
234
+ type Parameters = generateText.Parameters
235
+
236
+ type Event =
237
+ | {
238
+ /** Text produced since the previous event. */
239
+ delta: string
240
+ /** `text` for the answer, `reasoning` for the model's reasoning. */
241
+ type: 'reasoning' | 'text'
242
+ }
243
+ | {
244
+ type: 'done'
245
+ /** Token usage, when the model reports it. */
246
+ usage: generateText.ReturnValue['usage']
247
+ }
248
+
249
+ type ErrorType = Gateway.request.ErrorType
250
+ }
@@ -0,0 +1,324 @@
1
+ import * as Gateway from '../Gateway.js'
2
+
3
+ /**
4
+ * Metadata of an object in a Gateway bucket.
5
+ *
6
+ * @experimental
7
+ */
8
+ export type Metadata = {
9
+ /** Content type. */
10
+ contentType: string | null
11
+ /** Entity tag. */
12
+ etag: string
13
+ /** Expiry as a Unix timestamp in milliseconds. */
14
+ expiresAt: number
15
+ /** Object key. */
16
+ key: string
17
+ /** Size in bytes. */
18
+ size: number
19
+ /** Upload time as a Unix timestamp in milliseconds. */
20
+ uploadedAt: number
21
+ }
22
+
23
+ /**
24
+ * Deletes an object from the app's bucket. Paid per request.
25
+ *
26
+ * @experimental
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { Actions, Gateway } from 'viem/tempo'
31
+ *
32
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
33
+ *
34
+ * await Actions.bucket.delete(gateway, { key: 'notes/today.txt' })
35
+ * ```
36
+ *
37
+ * @param gateway - Gateway connection.
38
+ * @param parameters - Parameters.
39
+ * @returns Whether the request succeeded.
40
+ */
41
+ // biome-ignore lint/correctness/noUnusedVariables: exported as delete
42
+ async function delete_(
43
+ gateway: Gateway.Gateway,
44
+ parameters: delete_.Parameters,
45
+ ): Promise<delete_.ReturnValue> {
46
+ const { key, signal } = parameters
47
+ const response = await Gateway.request(gateway, {
48
+ body: JSON.stringify({ key }),
49
+ path: '/bucket/delete',
50
+ signal,
51
+ })
52
+ return (await response.json()) as delete_.ReturnValue
53
+ }
54
+
55
+ declare namespace delete_ {
56
+ type Parameters = {
57
+ /** Object key. */
58
+ key: string
59
+ /** Signal to abort the request. */
60
+ signal?: AbortSignal | undefined
61
+ }
62
+
63
+ type ReturnValue = {
64
+ /** Whether the request succeeded. */
65
+ deleted: boolean
66
+ }
67
+
68
+ type ErrorType = Gateway.request.ErrorType
69
+ }
70
+
71
+ export { delete_ as delete }
72
+
73
+ /**
74
+ * Reads an object from the app's bucket as a stream. Returns `null` when the
75
+ * object does not exist or expired. Paid per request.
76
+ *
77
+ * @experimental
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * import { Actions, Gateway } from 'viem/tempo'
82
+ *
83
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
84
+ *
85
+ * const object = await Actions.bucket.get(gateway, { key: 'notes/today.txt' })
86
+ * const text = object ? await new Response(object.body).text() : null
87
+ * ```
88
+ *
89
+ * @param gateway - Gateway connection.
90
+ * @param parameters - Parameters.
91
+ * @returns The object body and metadata, or `null`.
92
+ */
93
+ export async function get(
94
+ gateway: Gateway.Gateway,
95
+ parameters: get.Parameters,
96
+ ): Promise<get.ReturnValue> {
97
+ const { key, signal } = parameters
98
+ const response = await Gateway.request(gateway, {
99
+ body: JSON.stringify({ key }),
100
+ path: '/bucket/get',
101
+ signal,
102
+ }).catch((error) => {
103
+ if (error instanceof Gateway.RequestError && error.code === 'not_found')
104
+ return null
105
+ throw error
106
+ })
107
+ if (!response?.body) return null
108
+
109
+ const { headers } = response
110
+ return {
111
+ body: response.body,
112
+ contentType: headers.get('content-type'),
113
+ etag: headers.get('etag'),
114
+ expiresAt: Number(headers.get('gateway-expires-at')),
115
+ key,
116
+ }
117
+ }
118
+
119
+ export declare namespace get {
120
+ type Parameters = {
121
+ /** Object key. */
122
+ key: string
123
+ /** Signal to abort the request. */
124
+ signal?: AbortSignal | undefined
125
+ }
126
+
127
+ type ReturnValue = {
128
+ /** Object bytes. */
129
+ body: ReadableStream<Uint8Array>
130
+ /** Content type. */
131
+ contentType: string | null
132
+ /** Entity tag. */
133
+ etag: string | null
134
+ /** Expiry as a Unix timestamp in milliseconds. */
135
+ expiresAt: number
136
+ /** Object key. */
137
+ key: string
138
+ } | null
139
+
140
+ type ErrorType = Gateway.request.ErrorType
141
+ }
142
+
143
+ /**
144
+ * Reads the metadata of an object in the app's bucket. Returns `null` when the
145
+ * object does not exist or expired. Paid per request.
146
+ *
147
+ * @experimental
148
+ *
149
+ * @example
150
+ * ```ts
151
+ * import { Actions, Gateway } from 'viem/tempo'
152
+ *
153
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
154
+ *
155
+ * const metadata = await Actions.bucket.head(gateway, { key: 'notes/today.txt' })
156
+ * ```
157
+ *
158
+ * @param gateway - Gateway connection.
159
+ * @param parameters - Parameters.
160
+ * @returns The object metadata, or `null`.
161
+ */
162
+ export async function head(
163
+ gateway: Gateway.Gateway,
164
+ parameters: head.Parameters,
165
+ ): Promise<head.ReturnValue> {
166
+ const { key, signal } = parameters
167
+ const response = await Gateway.request(gateway, {
168
+ body: JSON.stringify({ key }),
169
+ path: '/bucket/head',
170
+ signal,
171
+ })
172
+ return (await response.json()) as head.ReturnValue
173
+ }
174
+
175
+ export declare namespace head {
176
+ type Parameters = {
177
+ /** Object key. */
178
+ key: string
179
+ /** Signal to abort the request. */
180
+ signal?: AbortSignal | undefined
181
+ }
182
+
183
+ type ReturnValue = Metadata | null
184
+
185
+ type ErrorType = Gateway.request.ErrorType
186
+ }
187
+
188
+ /**
189
+ * Lists objects in the app's bucket by key prefix. Paid per request.
190
+ *
191
+ * @experimental
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * import { Actions, Gateway } from 'viem/tempo'
196
+ *
197
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
198
+ *
199
+ * const { cursor, objects } = await Actions.bucket.list(gateway, {
200
+ * prefix: 'notes/',
201
+ * })
202
+ * ```
203
+ *
204
+ * @param gateway - Gateway connection.
205
+ * @param parameters - Parameters.
206
+ * @returns A page of object metadata and the next cursor.
207
+ */
208
+ export async function list(
209
+ gateway: Gateway.Gateway,
210
+ parameters: list.Parameters = {},
211
+ ): Promise<list.ReturnValue> {
212
+ const { cursor, limit, prefix, signal } = parameters
213
+ const response = await Gateway.request(gateway, {
214
+ body: JSON.stringify({ cursor, limit, prefix }),
215
+ path: '/bucket/list',
216
+ signal,
217
+ })
218
+ return (await response.json()) as list.ReturnValue
219
+ }
220
+
221
+ export declare namespace list {
222
+ type Parameters = {
223
+ /** Cursor from a previous page. */
224
+ cursor?: string | undefined
225
+ /** Maximum objects per page. */
226
+ limit?: number | undefined
227
+ /** Key prefix. */
228
+ prefix?: string | undefined
229
+ /** Signal to abort the request. */
230
+ signal?: AbortSignal | undefined
231
+ }
232
+
233
+ type ReturnValue = {
234
+ /** Cursor of the next page, or `null` on the last page. */
235
+ cursor: string | null
236
+ /** Objects on this page. */
237
+ objects: readonly Metadata[]
238
+ }
239
+
240
+ type ErrorType = Gateway.request.ErrorType
241
+ }
242
+
243
+ /**
244
+ * Writes an object to the app's bucket for a retention period. Paid per
245
+ * request.
246
+ *
247
+ * @experimental
248
+ *
249
+ * @example
250
+ * ```ts
251
+ * import { Actions, Gateway } from 'viem/tempo'
252
+ *
253
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
254
+ *
255
+ * const metadata = await Actions.bucket.put(gateway, {
256
+ * key: 'notes/today.txt',
257
+ * retention: 86_400,
258
+ * value: new Blob(['hello'], { type: 'text/plain' }),
259
+ * })
260
+ * ```
261
+ *
262
+ * @param gateway - Gateway connection.
263
+ * @param parameters - Parameters.
264
+ * @returns The object metadata.
265
+ */
266
+ export async function put(
267
+ gateway: Gateway.Gateway,
268
+ parameters: put.Parameters,
269
+ ): Promise<put.ReturnValue> {
270
+ const { key, retention, signal, value } = parameters
271
+ const body =
272
+ typeof value === 'string' ? new TextEncoder().encode(value) : value
273
+ const size = isBlob(body) ? body.size : body.byteLength
274
+ const contentType =
275
+ parameters.contentType ??
276
+ (typeof value === 'string' ? 'text/plain;charset=utf-8' : undefined) ??
277
+ (isBlob(value) && value.type ? value.type : undefined) ??
278
+ 'application/octet-stream'
279
+ const response = await Gateway.request(gateway, {
280
+ body: body as BodyInit,
281
+ headers: {
282
+ 'content-type': contentType,
283
+ 'gateway-key': key,
284
+ 'gateway-retention': String(retention),
285
+ 'gateway-size': String(size),
286
+ },
287
+ path: '/bucket/put',
288
+ signal,
289
+ })
290
+ return (await response.json()) as put.ReturnValue
291
+ }
292
+
293
+ export declare namespace put {
294
+ type Parameters = {
295
+ /**
296
+ * Content type.
297
+ *
298
+ * @default The Blob's type, `text/plain;charset=utf-8` for strings, or `application/octet-stream`.
299
+ */
300
+ contentType?: string | undefined
301
+ /** Object key. */
302
+ key: string
303
+ /** Retention in seconds. The object expires afterwards. */
304
+ retention: number
305
+ /** Signal to abort the request. */
306
+ signal?: AbortSignal | undefined
307
+ /** Object bytes. */
308
+ value: Blob | Uint8Array | ArrayBuffer | string
309
+ }
310
+
311
+ type ReturnValue = Metadata
312
+
313
+ type ErrorType = Gateway.request.ErrorType
314
+ }
315
+
316
+ /** Detects a `Blob` without `instanceof`, so ones from another realm count. */
317
+ function isBlob(value: unknown): value is Blob {
318
+ return (
319
+ typeof value === 'object' &&
320
+ value !== null &&
321
+ typeof (value as Blob).arrayBuffer === 'function' &&
322
+ typeof (value as Blob).size === 'number'
323
+ )
324
+ }