@rangedb/cloudflare 1.0.0
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/dist/index.d.ts +15 -0
- package/index.js +55 -0
- package/package.json +34 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export class RangeDBCloudflare extends RangeDB {
|
|
2
|
+
/**
|
|
3
|
+
* Initialize database by providing Cloudflare R2 bucket and file name.
|
|
4
|
+
*
|
|
5
|
+
* @param {R2Bucket} bucket Cloudflare R2 Bucket binding
|
|
6
|
+
* @param {string} key Name of the file in the bucket
|
|
7
|
+
* @param {import('@rangedb/js').RangeDBOptions} [options]
|
|
8
|
+
*/
|
|
9
|
+
constructor(bucket: R2Bucket, key: string, options?: import("@rangedb/js").RangeDBOptions);
|
|
10
|
+
/** @private @type {R2Bucket} */
|
|
11
|
+
private bucket;
|
|
12
|
+
/** @private @type {string} */
|
|
13
|
+
private key;
|
|
14
|
+
}
|
|
15
|
+
import { RangeDB } from '@rangedb/js';
|
package/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { RangeDB } from '@rangedb/js'
|
|
4
|
+
|
|
5
|
+
export class RangeDBCloudflare extends RangeDB {
|
|
6
|
+
/**
|
|
7
|
+
* Initialize database by providing Cloudflare R2 bucket and file name.
|
|
8
|
+
*
|
|
9
|
+
* @param {R2Bucket} bucket Cloudflare R2 Bucket binding
|
|
10
|
+
* @param {string} key Name of the file in the bucket
|
|
11
|
+
* @param {import('@rangedb/js').RangeDBOptions} [options]
|
|
12
|
+
*/
|
|
13
|
+
constructor(bucket, key, options = {}) {
|
|
14
|
+
super(key, options)
|
|
15
|
+
/** @private @type {R2Bucket} */
|
|
16
|
+
this.bucket = bucket
|
|
17
|
+
/** @private @type {string} */
|
|
18
|
+
this.key = key
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Perform range read on file in Cloudflare R2 bucket.
|
|
23
|
+
*
|
|
24
|
+
* @protected
|
|
25
|
+
* @param {bigint} start
|
|
26
|
+
* @param {bigint} end
|
|
27
|
+
*
|
|
28
|
+
* @returns {Promise<ArrayBuffer>}
|
|
29
|
+
*/
|
|
30
|
+
async readRange(start, end) {
|
|
31
|
+
const length = Number(end - start) + 1
|
|
32
|
+
|
|
33
|
+
const object = await this.bucket.get(this.key, {
|
|
34
|
+
range: {
|
|
35
|
+
offset: Number(start),
|
|
36
|
+
length: length,
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
if (object === null) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Database file not found. Key ${this.key} not found in bucket.`,
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const { etag } = object
|
|
47
|
+
if (this.etag && this.etag !== etag) {
|
|
48
|
+
this.invalidate()
|
|
49
|
+
throw new Error('Database file has changed based on ETag.')
|
|
50
|
+
}
|
|
51
|
+
this.etag = etag
|
|
52
|
+
|
|
53
|
+
return object.arrayBuffer()
|
|
54
|
+
}
|
|
55
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rangedb/cloudflare",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build:types": "npx -p typescript tsc",
|
|
15
|
+
"prepare": "npm run build:types",
|
|
16
|
+
"test": "node --experimental-test-coverage --test-coverage-include=*.js --test",
|
|
17
|
+
"test:watch": "node --test --watch"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"index.js",
|
|
21
|
+
"dist/index.d.ts"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"provenance": false
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@rangedb/js": "*"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@cloudflare/workers-types": "^4.20260307.1",
|
|
32
|
+
"@types/node": "^25.3.3"
|
|
33
|
+
}
|
|
34
|
+
}
|