-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathkv.h
More file actions
156 lines (124 loc) · 6.58 KB
/
Copy pathkv.h
File metadata and controls
156 lines (124 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
#pragma once
#include <workerd/jsg/jsg.h>
#include "http.h"
namespace workerd::api {
class KvNamespace: public jsg::Object {
// A capability to a KV namespace.
public:
explicit KvNamespace(uint subrequestChannel): subrequestChannel(subrequestChannel) {}
// `subrequestChannel` is what to pass to IoContext::getHttpClient() to get an HttpClient
// representing this namespace.
struct GetOptions {
jsg::Optional<kj::String> type;
jsg::Optional<int> cacheTtl;
JSG_STRUCT(type, cacheTtl);
JSG_STRUCT_TS_OVERRIDE(KVNamespaceGetOptions<Type> {
type: Type;
});
};
using GetResult = kj::Maybe<
kj::OneOf<jsg::Ref<ReadableStream>, kj::Array<byte>, kj::String, jsg::Value>>;
jsg::Promise<GetResult> get(
jsg::Lock& js,
kj::String name,
jsg::Optional<kj::OneOf<kj::String, GetOptions>> options);
struct GetWithMetadataResult {
GetResult value;
kj::Maybe<jsg::Value> metadata;
JSG_STRUCT(value, metadata);
JSG_STRUCT_TS_OVERRIDE(KVNamespaceGetWithMetadataResult<Value, Metadata> {
value: Value | null;
metadata: Metadata | null;
});
};
jsg::Promise<GetWithMetadataResult> getWithMetadata(
jsg::Lock& js,
kj::String name,
jsg::Optional<kj::OneOf<kj::String, GetOptions>> options);
struct ListOptions {
jsg::Optional<int> limit;
jsg::Optional<kj::Maybe<kj::String>> prefix;
jsg::Optional<kj::Maybe<kj::String>> cursor;
JSG_STRUCT(limit, prefix, cursor);
JSG_STRUCT_TS_OVERRIDE(KVNamespaceListOptions);
};
jsg::Promise<jsg::Value> list(jsg::Lock& js, jsg::Optional<ListOptions> options);
struct PutOptions {
// Optional parameter for passing options into a Fetcher::put. Initially
// intended for supporting expiration times in KV bindings.
jsg::Optional<int> expiration;
jsg::Optional<int> expirationTtl;
jsg::Optional<kj::Maybe<jsg::Value>> metadata;
JSG_STRUCT(expiration, expirationTtl, metadata);
JSG_STRUCT_TS_OVERRIDE(KVNamespacePutOptions);
};
using PutBody = kj::OneOf<kj::String, v8::Local<v8::Object>>;
// We can't just list the supported types in this OneOf because if we did then arbitrary objects
// would get coerced into meaningless strings like "[object Object]". Instead we first use this
// OneOf to differentiate between primitives and objects, and check the object for the types that
// we specifically support later.
using PutSupportedTypes = kj::OneOf<kj::String, kj::Array<byte>, jsg::Ref<ReadableStream>>;
jsg::Promise<void> put(
jsg::Lock& js,
kj::String name,
PutBody body,
jsg::Optional<PutOptions> options,
const jsg::TypeHandler<PutSupportedTypes>& putTypeHandler);
jsg::Promise<void> delete_(jsg::Lock& js, kj::String name);
JSG_RESOURCE_TYPE(KvNamespace) {
JSG_METHOD(get);
JSG_METHOD(list);
JSG_METHOD(put);
JSG_METHOD(getWithMetadata);
JSG_METHOD_NAMED(delete, delete_);
JSG_TS_ROOT();
JSG_TS_DEFINE(
interface KVNamespaceListKey<Metadata, Key extends string = string> {
name: Key;
expiration?: number;
metadata?: Metadata;
}
type KVNamespaceListResult<Metadata, Key extends string = string> =
| { list_complete: false; keys: KVNamespaceListKey<Metadata, Key>[]; cursor: string; }
| { list_complete: true; keys: KVNamespaceListKey<Metadata, Key>[]; };
);
// `Metadata` before `Key` type parameter for backwards-compatibility with `workers-types@3`.
// `Key` is also an optional type parameter, which must come after required parameters.
JSG_TS_OVERRIDE(KVNamespace<Key extends string = string> {
get(key: Key, options?: Partial<KVNamespaceGetOptions<undefined>>): Promise<string | null>;
get(key: Key, type: "text"): Promise<string | null>;
get<ExpectedValue = unknown>(key: Key, type: "json"): Promise<ExpectedValue | null>;
get(key: Key, type: "arrayBuffer"): Promise<ArrayBuffer | null>;
get(key: Key, type: "stream"): Promise<ReadableStream | null>;
get(key: Key, options?: KVNamespaceGetOptions<"text">): Promise<string | null>;
get<ExpectedValue = unknown>(key: Key, options?: KVNamespaceGetOptions<"json">): Promise<ExpectedValue | null>;
get(key: Key, options?: KVNamespaceGetOptions<"arrayBuffer">): Promise<string | null>;
get(key: Key, options?: KVNamespaceGetOptions<"stream">): Promise<string | null>;
list<Metadata = unknown>(options?: KVNamespaceListOptions): Promise<KVNamespaceListResult<Metadata, Key>>;
put(key: Key, value: string | ArrayBuffer | ArrayBufferView | ReadableStream, options?: KVNamespacePutOptions): Promise<void>;
getWithMetadata<Metadata = unknown>(key: Key, options?: Partial<KVNamespaceGetOptions<undefined>>): Promise<KVNamespaceGetWithMetadataResult<string, Metadata>>;
getWithMetadata<Metadata = unknown>(key: Key, type: "text"): Promise<KVNamespaceGetWithMetadataResult<string, Metadata>>;
getWithMetadata<ExpectedValue = unknown, Metadata = unknown>(key: Key, type: "json"): Promise<KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>;
getWithMetadata<Metadata = unknown>(key: Key, type: "arrayBuffer"): Promise<KVNamespaceGetWithMetadataResult<ArrayBuffer, Metadata>>;
getWithMetadata<Metadata = unknown>(key: Key, type: "stream"): Promise<KVNamespaceGetWithMetadataResult<ReadableStream, Metadata>>;
getWithMetadata<Metadata = unknown>(key: Key, options: KVNamespaceGetOptions<"text">): Promise<KVNamespaceGetWithMetadataResult<string, Metadata>>;
getWithMetadata<ExpectedValue = unknown, Metadata = unknown>(key: Key, options: KVNamespaceGetOptions<"json">): Promise<KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>;
getWithMetadata<Metadata = unknown>(key: Key, options: KVNamespaceGetOptions<"arrayBuffer">): Promise<KVNamespaceGetWithMetadataResult<ArrayBuffer, Metadata>>;
getWithMetadata<Metadata = unknown>(key: Key, options: KVNamespaceGetOptions<"stream">): Promise<KVNamespaceGetWithMetadataResult<ReadableStream, Metadata>>;
delete(key: Key): Promise<void>;
});
}
private:
uint subrequestChannel;
};
#define EW_KV_ISOLATE_TYPES \
api::KvNamespace, \
api::KvNamespace::ListOptions, \
api::KvNamespace::GetOptions, \
api::KvNamespace::PutOptions, \
api::KvNamespace::GetWithMetadataResult
// The list of kv.h types that are added to worker.c++'s JSG_DECLARE_ISOLATE_TYPE
} // namespace workerd::api