@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.
package/dist/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
  export const Loader = {
4
5
  appropriate: (name, config = {}) => {
5
6
  if (config.StorageClass) {
@@ -8,6 +9,9 @@ export const Loader = {
8
9
  if (config.type === 'rest') {
9
10
  return new Rest(name, config);
10
11
  }
12
+ if (config.type === 'ucan') {
13
+ return new UCAN(name, config);
14
+ }
11
15
  return new Browser(name, config);
12
16
  }
13
17
  };
@@ -39376,7 +39376,7 @@ const blocksFromEncryptedCarBlock = async (cid, get, keyMaterial) => {
39376
39376
  }
39377
39377
  };
39378
39378
 
39379
- const defaultConfig$1 = {
39379
+ const defaultConfig$2 = {
39380
39380
  headerKeyPrefix: 'fp.'
39381
39381
  };
39382
39382
 
@@ -39384,7 +39384,7 @@ const defaultConfig$1 = {
39384
39384
 
39385
39385
  class Browser extends Base {
39386
39386
  constructor (name, config = {}) {
39387
- super(name, Object.assign({}, defaultConfig$1, config));
39387
+ super(name, Object.assign({}, defaultConfig$2, config));
39388
39388
  }
39389
39389
 
39390
39390
  withDB = async dbWorkFun => {
@@ -40006,13 +40006,13 @@ var browserPonyfill = {
40006
40006
 
40007
40007
  var fetch = /*@__PURE__*/getDefaultExportFromCjs(browserPonyfillExports);
40008
40008
 
40009
- const defaultConfig = {
40009
+ const defaultConfig$1 = {
40010
40010
  url: 'http://localhost:4000'
40011
40011
  };
40012
40012
 
40013
40013
  class Rest extends Base {
40014
40014
  constructor (name, config = {}) {
40015
- super(name, Object.assign({}, defaultConfig, config));
40015
+ super(name, Object.assign({}, defaultConfig$1, config));
40016
40016
  // console.log('Rest', name, config)
40017
40017
  }
40018
40018
 
@@ -40064,6 +40064,49 @@ class Rest extends Base {
40064
40064
  }
40065
40065
  }
40066
40066
 
40067
+ const defaultConfig = {
40068
+ upload: () => {},
40069
+ url: (cid) => `https://${cid}.ipfs.w3s.link/`
40070
+ };
40071
+
40072
+ class UCAN extends Base {
40073
+ constructor (name, config = {}) {
40074
+ super(name, Object.assign({}, defaultConfig, config));
40075
+ }
40076
+
40077
+ async writeCars (cars) {
40078
+ if (this.config.readonly) return
40079
+ for (const { cid, bytes } of cars) {
40080
+ console.log(`write UCAN ${cid}, ${bytes.length} bytes`);
40081
+ const upCid = await this.config.upload(bytes);
40082
+ console.log(`wrote UCAN ${cid}, ${upCid}`);
40083
+ // if (!response.ok) throw new Error(`An error occurred: ${response.statusText}`)
40084
+ }
40085
+ }
40086
+
40087
+ async readCar (carCid) {
40088
+ const carURL = this.config.url(carCid);
40089
+ const response = await fetch(carURL);
40090
+ if (!response.ok) throw new Error(`An error occurred: ${response.statusText}`)
40091
+ const got = await response.arrayBuffer();
40092
+ return new Uint8Array(got)
40093
+ }
40094
+
40095
+ async loadHeader (branch = 'main') {
40096
+ return headerMock.get(branch)
40097
+ }
40098
+
40099
+ async writeHeader (branch, header) {
40100
+ if (this.config.readonly) return
40101
+ const pHeader = this.prepareHeader(header);
40102
+ // console.log('writeHeader rt', branch, pHeader)
40103
+
40104
+ headerMock.set(branch, pHeader);
40105
+ }
40106
+ }
40107
+
40108
+ const headerMock = new Map();
40109
+
40067
40110
  const Loader = {
40068
40111
  appropriate: (name, config = {}) => {
40069
40112
  if (config.StorageClass) {
@@ -40074,6 +40117,10 @@ const Loader = {
40074
40117
  return new Rest(name, config)
40075
40118
  }
40076
40119
 
40120
+ if (config.type === 'ucan') {
40121
+ return new UCAN(name, config)
40122
+ }
40123
+
40077
40124
  return new Browser(name, config)
40078
40125
  }
40079
40126
  };