@fireproof/core 0.7.2 → 0.7.3-dev.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ import fetch from 'cross-fetch';
2
+ import { Base } from './base.js';
3
+ const defaultConfig = {
4
+ upload: () => { },
5
+ url: (cid) => `https://${cid}.ipfs.w3s.link/`
6
+ };
7
+ export class UCAN extends Base {
8
+ constructor(name, config = {}) {
9
+ super(name, Object.assign({}, defaultConfig, config));
10
+ }
11
+ async writeCars(cars) {
12
+ if (this.config.readonly)
13
+ return;
14
+ for (const { cid, bytes } of cars) {
15
+ console.log(`write UCAN ${cid}, ${bytes.length} bytes`);
16
+ const upCid = await this.config.upload(bytes);
17
+ console.log(`wrote UCAN ${cid}, ${upCid}`);
18
+ // if (!response.ok) throw new Error(`An error occurred: ${response.statusText}`)
19
+ }
20
+ }
21
+ async readCar(carCid) {
22
+ const carURL = this.config.url(carCid);
23
+ const response = await fetch(carURL);
24
+ if (!response.ok)
25
+ throw new Error(`An error occurred: ${response.statusText}`);
26
+ const got = await response.arrayBuffer();
27
+ return new Uint8Array(got);
28
+ }
29
+ async loadHeader(branch = 'main') {
30
+ return headerMock.get(branch);
31
+ }
32
+ async writeHeader(branch, header) {
33
+ if (this.config.readonly)
34
+ return;
35
+ const pHeader = this.prepareHeader(header);
36
+ // console.log('writeHeader rt', branch, pHeader)
37
+ headerMock.set(branch, pHeader);
38
+ }
39
+ }
40
+ const headerMock = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fireproof/core",
3
- "version": "0.7.2",
3
+ "version": "0.7.3-dev.2",
4
4
  "description": "Live data for React, accelerated by proofs, powered by IPFS",
5
5
  "main": "dist/src/fireproof.js",
6
6
  "module": "dist/src/fireproof.mjs",
@@ -42,6 +42,10 @@
42
42
  "@ipld/car": "^5.1.0",
43
43
  "@ipld/dag-cbor": "^9.0.0",
44
44
  "@jsonlines/core": "^1.0.2",
45
+ "@ucanto/principal": "^8.0.0",
46
+ "@ucanto/server": "^8.0.1",
47
+ "@ucanto/transport": "^8.0.0",
48
+ "@ucanto/validator": "^8.0.0",
45
49
  "async": "^3.2.4",
46
50
  "charwise": "^3.0.1",
47
51
  "cross-fetch": "^3.1.6",
package/src/loader.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Browser } from './storage/browser.js'
2
2
  import { Rest } from './storage/rest.js'
3
+ import { UCAN } from './storage/ucan.js'
3
4
 
4
5
  export const Loader = {
5
6
  appropriate: (name, config = {}) => {
@@ -11,6 +12,10 @@ export const Loader = {
11
12
  return new Rest(name, config)
12
13
  }
13
14
 
15
+ if (config.type === 'ucan') {
16
+ return new UCAN(name, config)
17
+ }
18
+
14
19
  return new Browser(name, config)
15
20
  }
16
21
  }
@@ -0,0 +1,45 @@
1
+ import fetch from 'cross-fetch'
2
+ import { Base } from './base.js'
3
+
4
+ const defaultConfig = {
5
+ upload: () => {},
6
+ url: (cid) => `https://${cid}.ipfs.w3s.link/`
7
+ }
8
+
9
+ export class UCAN extends Base {
10
+ constructor (name, config = {}) {
11
+ super(name, Object.assign({}, defaultConfig, config))
12
+ }
13
+
14
+ async writeCars (cars) {
15
+ if (this.config.readonly) return
16
+ for (const { cid, bytes } of cars) {
17
+ console.log(`write UCAN ${cid}, ${bytes.length} bytes`)
18
+ const upCid = await this.config.upload(bytes)
19
+ console.log(`wrote UCAN ${cid}, ${upCid}`)
20
+ // if (!response.ok) throw new Error(`An error occurred: ${response.statusText}`)
21
+ }
22
+ }
23
+
24
+ async readCar (carCid) {
25
+ const carURL = this.config.url(carCid)
26
+ const response = await fetch(carURL)
27
+ if (!response.ok) throw new Error(`An error occurred: ${response.statusText}`)
28
+ const got = await response.arrayBuffer()
29
+ return new Uint8Array(got)
30
+ }
31
+
32
+ async loadHeader (branch = 'main') {
33
+ return headerMock.get(branch)
34
+ }
35
+
36
+ async writeHeader (branch, header) {
37
+ if (this.config.readonly) return
38
+ const pHeader = this.prepareHeader(header)
39
+ // console.log('writeHeader rt', branch, pHeader)
40
+
41
+ headerMock.set(branch, pHeader)
42
+ }
43
+ }
44
+
45
+ const headerMock = new Map()