konserve-r2 0.1.0-alpha.1
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.
- package/LICENSE +183 -0
- package/README.md +161 -0
- package/konserve_r2/bridge.mjs +227 -0
- package/konserve_r2/core.mjs +112 -0
- package/konserve_r2/index.mjs +56 -0
- package/konserve_r2/js_store.mjs +154 -0
- package/konserve_r2/r2_blob.mjs +246 -0
- package/konserve_r2/r2_store.mjs +296 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any patent
|
|
81
|
+
claims licensable by such Contributor constitute direct or contributory
|
|
82
|
+
patent infringement, then any patent licenses granted to You under
|
|
83
|
+
this License for that Work shall terminate as of the date such
|
|
84
|
+
litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the
|
|
105
|
+
attribution notices contained within such NOTICE file, in
|
|
106
|
+
at least one of the following places: within a NOTICE text
|
|
107
|
+
file distributed as part of the Derivative Works; within
|
|
108
|
+
the Source form or documentation, if provided along with the
|
|
109
|
+
Derivative Works; or, within a display generated by the
|
|
110
|
+
Derivative Works, if and wherever such third-party notices
|
|
111
|
+
normally appear. The contents of the NOTICE file are for
|
|
112
|
+
informational purposes only and do not modify the License.
|
|
113
|
+
You may add Your own attribution notices within Derivative
|
|
114
|
+
Works that You distribute, alongside or in addition to the
|
|
115
|
+
NOTICE text from the Work, provided that such additional
|
|
116
|
+
attribution notices cannot be construed as modifying the License.
|
|
117
|
+
|
|
118
|
+
You may add Your own license statement for Your modifications and
|
|
119
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
120
|
+
publish, distribute, sublicense, and/or sell copies of the
|
|
121
|
+
Derivative Works, as separate terms and conditions for use,
|
|
122
|
+
reproduction, or distribution of Your modifications, or for such
|
|
123
|
+
Derivative Works as a whole, if Your use, reproduction, and
|
|
124
|
+
distribution of the Work otherwise complies with the conditions
|
|
125
|
+
stated in this License.
|
|
126
|
+
|
|
127
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
128
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
129
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
130
|
+
this License, without any additional terms or conditions.
|
|
131
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
132
|
+
the terms of any separate license agreement you may have executed
|
|
133
|
+
with Licensor regarding such Contributions.
|
|
134
|
+
|
|
135
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
136
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
137
|
+
except as required for reasonable and customary use in describing the
|
|
138
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
139
|
+
|
|
140
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
141
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
142
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
143
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
144
|
+
implied, including, without limitation, any warranties or conditions
|
|
145
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
146
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
147
|
+
appropriateness of using or reproducing the Work and assume any
|
|
148
|
+
risks associated with Your exercise of permissions under this License.
|
|
149
|
+
|
|
150
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
151
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
152
|
+
unless required by applicable law (such as deliberate and grossly
|
|
153
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
154
|
+
liable to You for damages, including any direct, indirect, special,
|
|
155
|
+
incidental, or exemplary damages of any character arising as a
|
|
156
|
+
result of this License or out of the use or inability to use the
|
|
157
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
158
|
+
work stoppage, computer failure or malfunction, or all other
|
|
159
|
+
commercial damages or losses), even if such Contributor has been
|
|
160
|
+
advised of the possibility of such damages.
|
|
161
|
+
|
|
162
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
163
|
+
the Work or Derivative Works thereof, You may wish to offer, and
|
|
164
|
+
charge a fee for, acceptance of support, warranty, indemnity,
|
|
165
|
+
or other liability obligations and/or rights consistent with this
|
|
166
|
+
License. However, in accepting such obligations, You may offer only
|
|
167
|
+
conditions that are consistent with this License.
|
|
168
|
+
|
|
169
|
+
END OF TERMS AND CONDITIONS
|
|
170
|
+
|
|
171
|
+
Copyright 2026 Alexander Oloo
|
|
172
|
+
|
|
173
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
174
|
+
you may not use this file except in compliance with the License.
|
|
175
|
+
You may obtain a copy of the License at
|
|
176
|
+
|
|
177
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
178
|
+
|
|
179
|
+
Unless required by applicable law or agreed to in writing, software
|
|
180
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
181
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
182
|
+
See the License for the specific language governing permissions and
|
|
183
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# konserve-r2
|
|
2
|
+
|
|
3
|
+
A key-value store for [Cloudflare R2](https://developers.cloudflare.com/r2/), with optional [Konserve](https://github.com/replikativ/konserve)/[Datahike](https://github.com/replikativ/datahike) integration for Datalog on the edge.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
konserve-r2 can be used in two ways:
|
|
8
|
+
|
|
9
|
+
1. **Pure JS** — a simple, Promise-based KV store on R2. No dependencies beyond the R2 bucket binding.
|
|
10
|
+
2. **Konserve/Datahike** — a full `PBackingStore`/`PBackingBlob` implementation that lets Datahike persist to R2 inside a Cloudflare Worker.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- Pure JavaScript KV API — `put`, `get`, `delete`, `keys`, `exists`
|
|
15
|
+
- Binary value support via `putBinary` / `getBinary`
|
|
16
|
+
- Prefix-based namespace isolation
|
|
17
|
+
- Full Konserve `PBackingStore` / `PBackingBlob` protocol implementation
|
|
18
|
+
- Datahike Datalog queries, joins, and aggregates on persistent R2 storage
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install konserve-r2
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### KV Store
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import { connect } from 'konserve-r2';
|
|
32
|
+
|
|
33
|
+
export default {
|
|
34
|
+
async fetch(request, env) {
|
|
35
|
+
const store = connect(env.R2_BUCKET, { prefix: 'my-app/' });
|
|
36
|
+
|
|
37
|
+
await store.put('user:alice', { name: 'Alice', age: 30 });
|
|
38
|
+
const alice = await store.get('user:alice'); // parsed JSON
|
|
39
|
+
|
|
40
|
+
await store.putBinary('avatar', avatarArrayBuffer);
|
|
41
|
+
const avatar = await store.getBinary('avatar'); // ArrayBuffer
|
|
42
|
+
|
|
43
|
+
const allKeys = await store.keys(); // string[]
|
|
44
|
+
const exists = await store.exists('user:alice'); // boolean
|
|
45
|
+
await store.delete('user:alice');
|
|
46
|
+
await store.destroy(); // wipe all prefixed keys
|
|
47
|
+
|
|
48
|
+
return Response.json(alice);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### With Datahike
|
|
54
|
+
|
|
55
|
+
If [datahike](https://www.npmjs.com/package/datahike) is installed and imported **before** konserve-r2, the `:r2` konserve backend registers automatically — no extra setup.
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm install datahike
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import 'datahike';
|
|
63
|
+
import { registerBucket } from 'konserve-r2'; // auto-detects datahike
|
|
64
|
+
|
|
65
|
+
registerBucket(STORE_ID, env.R2_BUCKET);
|
|
66
|
+
|
|
67
|
+
const config = { store: { backend: ':r2', id: STORE_ID } };
|
|
68
|
+
|
|
69
|
+
if (!(await d.databaseExists(config))) {
|
|
70
|
+
await d.createDatabase(config);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const conn = await d.connect(config);
|
|
74
|
+
await d.transact(conn, [{ 'user/name': 'Alice' }]);
|
|
75
|
+
const db = await d.db(conn);
|
|
76
|
+
const results = await d.q('[:find ?name :where [?e :user/name ?name]]', db);
|
|
77
|
+
await d.release(conn);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
If you can't control the import order (e.g. datahike loads after konserve-r2), use `initKonserve()`:
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import { initKonserve, registerBucket } from 'konserve-r2';
|
|
84
|
+
import 'datahike';
|
|
85
|
+
|
|
86
|
+
await initKonserve(); // force-loads the konserve backend
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
To prevent the konserve backend from loading even when datahike is present, call `disableKonserve()`:
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
import { disableKonserve, connect } from 'konserve-r2';
|
|
93
|
+
disableKonserve(); // JS-only mode, skip konserve autodetection
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Development
|
|
97
|
+
|
|
98
|
+
### Prerequisites
|
|
99
|
+
|
|
100
|
+
- Node.js 18+
|
|
101
|
+
- Cloudflare account with an R2 bucket (for deployment)
|
|
102
|
+
|
|
103
|
+
### Install
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
npm install
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Build
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
npm run build
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Compiles ClojureScript via Squint, then bundles everything with esbuild into `dist/worker.js`.
|
|
116
|
+
|
|
117
|
+
### Test
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
npm test
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Runs the full test suite (build + Vitest). Tests use Miniflare's programmatic API — no Cloudflare account needed.
|
|
124
|
+
|
|
125
|
+
### Local dev
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
npm run dev
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Deploy
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
npm run deploy
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Project structure
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
src/
|
|
141
|
+
konserve_r2/
|
|
142
|
+
index.mjs # Unified entry point (autodetects CLJS runtime)
|
|
143
|
+
js_store.mjs # Pure JS KV store API (no CLJS required)
|
|
144
|
+
core.cljs # Konserve backend registration & multimethod impl
|
|
145
|
+
bridge.mjs # Promise ↔ core.async channel bridge
|
|
146
|
+
r2_blob.mjs # PBackingBlob implementation
|
|
147
|
+
r2_store.mjs # PBackingStore implementation
|
|
148
|
+
test_worker.cljs # Example Worker using Datahike + konserve-r2
|
|
149
|
+
test/
|
|
150
|
+
js_store.test.js # Pure JS KV store tests
|
|
151
|
+
bridge.test.js # Bridge layer tests
|
|
152
|
+
r2_blob.test.js # PBackingBlob protocol tests
|
|
153
|
+
r2_store.test.js # PBackingStore protocol tests
|
|
154
|
+
integration.test.js # End-to-end Worker tests via Miniflare
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
Copyright 2026 Alexander Oloo
|
|
160
|
+
|
|
161
|
+
Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// bridge.mjs — Promise ↔ core.async channel bridge
|
|
2
|
+
//
|
|
3
|
+
// Konserve's DefaultStore expects backing operations to return
|
|
4
|
+
// core.async channels (in async mode). R2 binding API returns Promises.
|
|
5
|
+
// This module bridges the two worlds by accessing compiled CLJS globals
|
|
6
|
+
// from the datahike npm package.
|
|
7
|
+
|
|
8
|
+
let _$CLJS = null;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Lazily resolve the $CLJS global.
|
|
12
|
+
* This is set up by datahike or any CLJS runtime that exposes konserve
|
|
13
|
+
* on globalThis.$CLJS (e.g. a standalone konserve npm package).
|
|
14
|
+
*/
|
|
15
|
+
function getCLJS() {
|
|
16
|
+
if (_$CLJS) return _$CLJS;
|
|
17
|
+
if (typeof globalThis !== 'undefined' && globalThis.$CLJS) {
|
|
18
|
+
_$CLJS = globalThis.$CLJS;
|
|
19
|
+
return _$CLJS;
|
|
20
|
+
}
|
|
21
|
+
throw new Error(
|
|
22
|
+
'konserve-r2: $CLJS global not found. ' +
|
|
23
|
+
'Import datahike or a compatible konserve runtime before importing konserve-r2.'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Access a compiled CLJS keyword by name.
|
|
29
|
+
* Uses cljs.core.keyword() to ensure proper hashing and interning.
|
|
30
|
+
*/
|
|
31
|
+
export function keyword(name) {
|
|
32
|
+
const cljs = getCLJS().cljs;
|
|
33
|
+
return cljs.core.keyword.cljs$core$IFn$_invoke$arity$1(name);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Build a CLJS PersistentArrayMap from a JS object.
|
|
38
|
+
*/
|
|
39
|
+
export function toClj(obj) {
|
|
40
|
+
const cljs = getCLJS().cljs;
|
|
41
|
+
const entries = [];
|
|
42
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
43
|
+
entries.push(keyword(k));
|
|
44
|
+
entries.push(v);
|
|
45
|
+
}
|
|
46
|
+
return cljs.core.PersistentArrayMap.createAsIfByAssoc(entries);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Wrap a Promise result in a core.async channel (buffered 1).
|
|
51
|
+
* The channel will receive the resolved value or the rejection error.
|
|
52
|
+
*/
|
|
53
|
+
export function promiseToChan(promise) {
|
|
54
|
+
const cljs = getCLJS().cljs;
|
|
55
|
+
const ch = cljs.core.async.chan.cljs$core$IFn$_invoke$arity$1(1);
|
|
56
|
+
promise.then(
|
|
57
|
+
(val) => {
|
|
58
|
+
// null/undefined → use false as channel sentinel (channels drop nil)
|
|
59
|
+
const v = val == null ? false : val;
|
|
60
|
+
cljs.core.async.put_BANG_.cljs$core$IFn$_invoke$arity$2(ch, v);
|
|
61
|
+
},
|
|
62
|
+
(err) => {
|
|
63
|
+
// Put the error on the channel so superv.async can handle it
|
|
64
|
+
cljs.core.async.put_BANG_.cljs$core$IFn$_invoke$arity$2(ch, err);
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
return ch;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Create a channel that immediately yields a value.
|
|
72
|
+
*/
|
|
73
|
+
export function valueToChan(val) {
|
|
74
|
+
const cljs = getCLJS().cljs;
|
|
75
|
+
const ch = cljs.core.async.chan.cljs$core$IFn$_invoke$arity$1(1);
|
|
76
|
+
const v = val == null ? false : val;
|
|
77
|
+
cljs.core.async.put_BANG_.cljs$core$IFn$_invoke$arity$2(ch, v);
|
|
78
|
+
return ch;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Convert a core.async channel to a Promise.
|
|
83
|
+
* Takes a single value from the channel and resolves with it.
|
|
84
|
+
* This is the inverse of promiseToChan.
|
|
85
|
+
*/
|
|
86
|
+
export function chanToPromise(ch) {
|
|
87
|
+
const cljs = getCLJS().cljs;
|
|
88
|
+
// Check if ch is actually a channel
|
|
89
|
+
if (ch && ch['cljs$core$async$impl$protocols$Channel$']) {
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
cljs.core.async.take_BANG_.cljs$core$IFn$_invoke$arity$2(ch, (val) => {
|
|
92
|
+
if (val instanceof Error) {
|
|
93
|
+
reject(val);
|
|
94
|
+
} else {
|
|
95
|
+
resolve(val);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
// If not a channel, just resolve with the value
|
|
101
|
+
return Promise.resolve(ch);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Access the konserve.store namespace multimethods.
|
|
106
|
+
*/
|
|
107
|
+
export function getStoreMultimethods() {
|
|
108
|
+
const $ = getCLJS();
|
|
109
|
+
const ks = $.konserve.store;
|
|
110
|
+
return {
|
|
111
|
+
connectStore: ks._connect_store,
|
|
112
|
+
createStore: ks._create_store,
|
|
113
|
+
storeExists: ks._store_exists_QMARK_,
|
|
114
|
+
deleteStore: ks._delete_store,
|
|
115
|
+
releaseStore: ks._release_store,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Access konserve.impl.defaults.connect_default_store
|
|
121
|
+
*/
|
|
122
|
+
export function getConnectDefaultStore() {
|
|
123
|
+
return getCLJS().konserve.impl.defaults.connect_default_store;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Access konserve compressor and encryptor defaults.
|
|
128
|
+
*/
|
|
129
|
+
export function getNullCompressor() {
|
|
130
|
+
return getCLJS().konserve.compressor.null_compressor;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function getNullEncryptor() {
|
|
134
|
+
return getCLJS().konserve.encryptor.null_encryptor;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Get cljs.core.PROTOCOL_SENTINEL
|
|
139
|
+
*/
|
|
140
|
+
export function getProtocolSentinel() {
|
|
141
|
+
return getCLJS().cljs.core.PROTOCOL_SENTINEL;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Get cljs.core for general use
|
|
146
|
+
*/
|
|
147
|
+
export function getCljsCore() {
|
|
148
|
+
return getCLJS().cljs.core;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Register a method on a CLJS multimethod.
|
|
153
|
+
*/
|
|
154
|
+
export function addMethod(multimethod, dispatchVal, fn) {
|
|
155
|
+
multimethod.cljs$core$IMultiFn$_add_method$arity$3(null, dispatchVal, fn);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ============================================================================
|
|
159
|
+
// Force async mode for R2 backend
|
|
160
|
+
// ============================================================================
|
|
161
|
+
// Datahike defaults to {:sync? true} for connect/createDatabase/etc.
|
|
162
|
+
// R2 operations are inherently async (Promises). We patch the datahike
|
|
163
|
+
// connector functions to force {:sync? false} when the backend is :r2.
|
|
164
|
+
// This patch is only applied when datahike is present.
|
|
165
|
+
|
|
166
|
+
let _patchApplied = false;
|
|
167
|
+
|
|
168
|
+
export function patchDatahikeForAsync() {
|
|
169
|
+
if (_patchApplied) return;
|
|
170
|
+
_patchApplied = true;
|
|
171
|
+
|
|
172
|
+
const $ = getCLJS();
|
|
173
|
+
const connector = $.datahike?.connector;
|
|
174
|
+
if (!connector) return; // datahike not loaded, nothing to patch
|
|
175
|
+
const cljs = $.cljs;
|
|
176
|
+
const syncKw = new cljs.core.Keyword(null, 'sync?', 'sync?', 630471618);
|
|
177
|
+
const backendKw = new cljs.core.Keyword(null, 'backend', 'backend', -847489124);
|
|
178
|
+
const storeKw = new cljs.core.Keyword(null, 'store', 'store', 1512230022);
|
|
179
|
+
const r2Kw = new cljs.core.Keyword(null, 'r2', 'r2', 0);
|
|
180
|
+
|
|
181
|
+
// Patch connect, create_database, delete_database, database_exists_QMARK_
|
|
182
|
+
const fnsToPath = ['connect', 'create_database', 'delete_database', 'database_exists_QMARK_'];
|
|
183
|
+
|
|
184
|
+
for (const fnName of fnsToPath) {
|
|
185
|
+
const original2 = connector[fnName]?.['cljs$core$IFn$_invoke$arity$2'];
|
|
186
|
+
if (original2) {
|
|
187
|
+
const origFn = connector[fnName];
|
|
188
|
+
connector[fnName]['cljs$core$IFn$_invoke$arity$2'] = function(config, opts) {
|
|
189
|
+
// Check if backend is :r2
|
|
190
|
+
const storeConfig = storeKw.cljs$core$IFn$_invoke$arity$1(config);
|
|
191
|
+
const backend = storeConfig ? backendKw.cljs$core$IFn$_invoke$arity$1(storeConfig) : null;
|
|
192
|
+
if (cljs.core._EQ_.cljs$core$IFn$_invoke$arity$2(backend, r2Kw)) {
|
|
193
|
+
// Force async mode
|
|
194
|
+
opts = cljs.core.assoc.cljs$core$IFn$_invoke$arity$3(opts, syncKw, false);
|
|
195
|
+
}
|
|
196
|
+
return original2.call(origFn, config, opts);
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ============================================================================
|
|
203
|
+
// R2 Bucket Registry
|
|
204
|
+
// ============================================================================
|
|
205
|
+
// The R2 bucket binding is a native Workers object that cannot be serialized
|
|
206
|
+
// by CLJS. We store it in a JS-side registry keyed by store-id string.
|
|
207
|
+
// The CLJS config only contains the store-id string, not the bucket object.
|
|
208
|
+
|
|
209
|
+
const _bucketRegistry = new Map();
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Register an R2 bucket binding for a store-id.
|
|
213
|
+
*/
|
|
214
|
+
export function registerBucket(storeId, bucket) {
|
|
215
|
+
_bucketRegistry.set(storeId, bucket);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Retrieve the R2 bucket binding for a store-id.
|
|
220
|
+
*/
|
|
221
|
+
export function getBucket(storeId) {
|
|
222
|
+
const bucket = _bucketRegistry.get(storeId);
|
|
223
|
+
if (!bucket) {
|
|
224
|
+
throw new Error(`konserve-r2: No R2 bucket registered for store-id: ${storeId}. Call registerBucket first.`);
|
|
225
|
+
}
|
|
226
|
+
return bucket;
|
|
227
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as squint_core from 'squint-cljs/core.js';
|
|
2
|
+
import * as bridge from './bridge.mjs';
|
|
3
|
+
import * as r2_store from './r2_store.mjs';
|
|
4
|
+
var cljs_core = bridge.getCljsCore();
|
|
5
|
+
var kw = function (name) {
|
|
6
|
+
return bridge.keyword(name);
|
|
7
|
+
|
|
8
|
+
};
|
|
9
|
+
var get_config_val = function (config, key_name) {
|
|
10
|
+
const k1 = kw(key_name);
|
|
11
|
+
return k1.cljs$core$IFn$_invoke$arity$1(config);
|
|
12
|
+
|
|
13
|
+
};
|
|
14
|
+
var registerBucket = bridge.registerBucket;
|
|
15
|
+
var make_store_config = function (config) {
|
|
16
|
+
const inner_config1 = get_config_val(config, "config");
|
|
17
|
+
const merged_config2 = cljs_core.merge.cljs$core$IFn$_invoke$arity$variadic(cljs_core.prim_seq.cljs$core$IFn$_invoke$arity$2([(new cljs_core.PersistentArrayMap(null, 3, [kw("sync-blob?"), true, kw("in-place?"), false, kw("lock-blob?"), false, kw("no-backup?"), true], null)), inner_config1], 0));
|
|
18
|
+
return (new cljs_core.PersistentArrayMap(null, 8, [kw("default-serializer"), kw("FressianSerializer"), kw("compressor"), bridge.getNullCompressor(), kw("encryptor"), bridge.getNullEncryptor(), kw("read-handlers"), cljs_core.atom.cljs$core$IFn$_invoke$arity$1(cljs_core.PersistentArrayMap.EMPTY), kw("write-handlers"), cljs_core.atom.cljs$core$IFn$_invoke$arity$1(cljs_core.PersistentArrayMap.EMPTY), kw("buffer-size"), (1024 * 1024), kw("opts"), (new cljs_core.PersistentArrayMap(null, 1, [kw("sync?"), false], null)), kw("config"), merged_config2], null));
|
|
19
|
+
|
|
20
|
+
};
|
|
21
|
+
var connect_r2_store = function (config) {
|
|
22
|
+
const store_id1 = `${get_config_val(config, "id")??''}`;
|
|
23
|
+
const bucket2 = bridge.getBucket(store_id1);
|
|
24
|
+
const backing3 = r2_store.createR2BackingStore(bucket2, store_id1);
|
|
25
|
+
const store_config4 = make_store_config(config);
|
|
26
|
+
const ch5 = bridge.getConnectDefaultStore()(backing3, store_config4);
|
|
27
|
+
return bridge.chanToPromise(ch5);
|
|
28
|
+
|
|
29
|
+
};
|
|
30
|
+
var register_backend_BANG_ = function () {
|
|
31
|
+
const multimethods1 = bridge.getStoreMultimethods();
|
|
32
|
+
const r2_kw2 = kw("r2");
|
|
33
|
+
bridge.addMethod(multimethods1.connectStore, r2_kw2, (function (config, opts) {
|
|
34
|
+
const store_id3 = `${get_config_val(config, "id")??''}`;
|
|
35
|
+
const bucket4 = bridge.getBucket(store_id3);
|
|
36
|
+
const prefix5 = `${store_id3}_`;
|
|
37
|
+
const marker6 = `${prefix5}konserve_store`;
|
|
38
|
+
return bridge.promiseToChan(bucket4.head(marker6).then((function (obj) {
|
|
39
|
+
if ((obj == null)) {
|
|
40
|
+
throw (new Error(`${"R2 store does not exist for id: "}${store_id3}`))} else {
|
|
41
|
+
return connect_r2_store(config)};
|
|
42
|
+
|
|
43
|
+
})));
|
|
44
|
+
|
|
45
|
+
}));
|
|
46
|
+
bridge.addMethod(multimethods1.createStore, r2_kw2, (function (config, opts) {
|
|
47
|
+
const store_id7 = `${get_config_val(config, "id")??''}`;
|
|
48
|
+
const bucket8 = bridge.getBucket(store_id7);
|
|
49
|
+
const prefix9 = `${store_id7}_`;
|
|
50
|
+
const marker10 = `${prefix9}konserve_store`;
|
|
51
|
+
return bridge.promiseToChan(bucket8.head(marker10).then((function (obj) {
|
|
52
|
+
if (!(obj == null)) {
|
|
53
|
+
throw (new Error(`${"R2 store already exists for id: "}${store_id7}`));
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
})).then((function (_) {
|
|
57
|
+
return bucket8.put(marker10, "konserve-r2");
|
|
58
|
+
|
|
59
|
+
})).then((function (_) {
|
|
60
|
+
return connect_r2_store(config);
|
|
61
|
+
|
|
62
|
+
})));
|
|
63
|
+
|
|
64
|
+
}));
|
|
65
|
+
bridge.addMethod(multimethods1.storeExists, r2_kw2, (function (config, opts) {
|
|
66
|
+
const store_id11 = `${get_config_val(config, "id")??''}`;
|
|
67
|
+
const bucket12 = bridge.getBucket(store_id11);
|
|
68
|
+
const prefix13 = `${store_id11}_`;
|
|
69
|
+
const marker14 = `${prefix13}konserve_store`;
|
|
70
|
+
return bridge.promiseToChan(bucket12.head(marker14).then((function (obj) {
|
|
71
|
+
return !(obj == null);
|
|
72
|
+
|
|
73
|
+
})));
|
|
74
|
+
|
|
75
|
+
}));
|
|
76
|
+
bridge.addMethod(multimethods1.deleteStore, r2_kw2, (function (config, opts) {
|
|
77
|
+
const store_id15 = `${get_config_val(config, "id")??''}`;
|
|
78
|
+
const bucket16 = bridge.getBucket(store_id15);
|
|
79
|
+
const prefix17 = `${store_id15}_`;
|
|
80
|
+
return bridge.promiseToChan(Promise.resolve((function delete_all () {
|
|
81
|
+
return bucket16.list(({"prefix": prefix17})).then((function (listing) {
|
|
82
|
+
const keys18 = listing.objects.map((function (o) {
|
|
83
|
+
return o.key;
|
|
84
|
+
|
|
85
|
+
}));
|
|
86
|
+
if ((keys18.length > 0)) {
|
|
87
|
+
return bucket16.delete(keys18).then((function (_) {
|
|
88
|
+
if (squint_core.truth_(listing.truncated)) {
|
|
89
|
+
return delete_all()} else {
|
|
90
|
+
return true};
|
|
91
|
+
|
|
92
|
+
}))} else {
|
|
93
|
+
return true};
|
|
94
|
+
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
})()));
|
|
98
|
+
|
|
99
|
+
}));
|
|
100
|
+
return bridge.addMethod(multimethods1.releaseStore, r2_kw2, (function (_config, _store, opts) {
|
|
101
|
+
const sync_QMARK_19 = get_config_val(opts, "sync?");
|
|
102
|
+
if (squint_core.truth_(sync_QMARK_19)) {
|
|
103
|
+
return null} else {
|
|
104
|
+
return bridge.valueToChan(null)};
|
|
105
|
+
|
|
106
|
+
}));
|
|
107
|
+
|
|
108
|
+
};
|
|
109
|
+
register_backend_BANG_();
|
|
110
|
+
bridge.patchDatahikeForAsync();
|
|
111
|
+
|
|
112
|
+
export { registerBucket }
|