@fireproof/core 0.6.4 → 0.6.5

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.
@@ -0,0 +1,58 @@
1
+ import fetch from 'node-fetch';
2
+ import { Base } from './base.js';
3
+ const defaultConfig = {
4
+ url: 'http://localhost:4000'
5
+ };
6
+ export class Rest extends Base {
7
+ constructor(name, config = {}) {
8
+ super(name, Object.assign({}, defaultConfig, config));
9
+ // console.log('Rest', name, config)
10
+ }
11
+ headerURL(branch = 'main') {
12
+ return `${this.config.url}/${branch}.json`;
13
+ }
14
+ async writeCars(cars) {
15
+ if (this.config.readonly)
16
+ return;
17
+ for (const { cid, bytes } of cars) {
18
+ const carURL = `${this.config.url}/${cid.toString()}.car`;
19
+ const response = await fetch(carURL, {
20
+ method: 'PUT',
21
+ body: bytes,
22
+ headers: { 'Content-Type': 'application/car' }
23
+ });
24
+ if (!response.ok)
25
+ throw new Error(`An error occurred: ${response.statusText}`);
26
+ }
27
+ }
28
+ async readCar(carCid) {
29
+ const carURL = `${this.config.url}/${carCid.toString()}.car`;
30
+ const response = await fetch(carURL);
31
+ if (!response.ok)
32
+ throw new Error(`An error occurred: ${response.statusText}`);
33
+ const got = await response.arrayBuffer();
34
+ return new Uint8Array(got);
35
+ }
36
+ async loadHeader(branch = 'main') {
37
+ const response = await fetch(this.headerURL(branch));
38
+ // console.log('rest getHeader', response.constructor.name)
39
+ if (!response.ok)
40
+ return null;
41
+ const got = await response.json();
42
+ // console.log('rest getHeader', got)
43
+ return got;
44
+ }
45
+ async writeHeader(branch, header) {
46
+ if (this.config.readonly)
47
+ return;
48
+ const pHeader = this.prepareHeader(header);
49
+ // console.log('writeHeader rt', branch, pHeader)
50
+ const response = await fetch(this.headerURL(branch), {
51
+ method: 'PUT',
52
+ body: pHeader,
53
+ headers: { 'Content-Type': 'application/json' }
54
+ });
55
+ if (!response.ok)
56
+ throw new Error(`An error occurred: ${response.statusText}`);
57
+ }
58
+ }
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fireproof/core",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
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",
package/src/db-index.js CHANGED
@@ -134,12 +134,21 @@ export class DbIndex {
134
134
  applyMapFn (mapFn, name) {
135
135
  if (typeof mapFn === 'string') {
136
136
  this.mapFnString = mapFn
137
+ // make a regex that matches strings that only have letters, numbers, and spaces
138
+ const regex = /^[a-zA-Z0-9 ]+$/
139
+ // if the string matches the regex, make a function that returns the value at that key
140
+ if (regex.test(mapFn)) {
141
+ this.mapFn = (doc, emit) => {
142
+ if (doc[mapFn]) emit(doc[mapFn])
143
+ }
144
+ this.includeDocsDefault = true
145
+ }
137
146
  } else {
138
147
  this.mapFn = mapFn
139
148
  this.mapFnString = mapFn.toString()
140
149
  }
141
150
  const matches = /=>\s*(.*)/.exec(this.mapFnString)
142
- this.includeDocsDefault = matches && matches.length > 0
151
+ this.includeDocsDefault = this.includeDocsDefault || (matches && matches.length > 0)
143
152
  this.name = name || this.makeName()
144
153
  }
145
154