@fireproof/core 0.7.0-alpha.6 → 0.7.0-alpha.7

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.
@@ -38975,7 +38975,7 @@ class VMemoryBlockstore {
38975
38975
  }
38976
38976
  }
38977
38977
 
38978
- const defaultConfig = {
38978
+ const defaultConfig$1 = {
38979
38979
  headerKeyPrefix: 'fp.'
38980
38980
  };
38981
38981
 
@@ -38983,7 +38983,7 @@ const defaultConfig = {
38983
38983
 
38984
38984
  class Browser extends Base {
38985
38985
  constructor (name, config = {}) {
38986
- super(name, Object.assign({}, defaultConfig, config));
38986
+ super(name, Object.assign({}, defaultConfig$1, config));
38987
38987
  this.isBrowser = false;
38988
38988
  try {
38989
38989
  this.isBrowser = window.localStorage && true;
@@ -39040,7 +39040,628 @@ class Browser extends Base {
39040
39040
  }
39041
39041
  }
39042
39042
 
39043
- // import { Rest } from './storage/rest.js'
39043
+ var browserPonyfillExports = {};
39044
+ var browserPonyfill = {
39045
+ get exports(){ return browserPonyfillExports; },
39046
+ set exports(v){ browserPonyfillExports = v; },
39047
+ };
39048
+
39049
+ (function (module, exports) {
39050
+ var global = typeof self !== 'undefined' ? self : commonjsGlobal;
39051
+ var __self__ = (function () {
39052
+ function F() {
39053
+ this.fetch = false;
39054
+ this.DOMException = global.DOMException;
39055
+ }
39056
+ F.prototype = global;
39057
+ return new F();
39058
+ })();
39059
+ (function(self) {
39060
+
39061
+ ((function (exports) {
39062
+
39063
+ var support = {
39064
+ searchParams: 'URLSearchParams' in self,
39065
+ iterable: 'Symbol' in self && 'iterator' in Symbol,
39066
+ blob:
39067
+ 'FileReader' in self &&
39068
+ 'Blob' in self &&
39069
+ (function() {
39070
+ try {
39071
+ new Blob();
39072
+ return true
39073
+ } catch (e) {
39074
+ return false
39075
+ }
39076
+ })(),
39077
+ formData: 'FormData' in self,
39078
+ arrayBuffer: 'ArrayBuffer' in self
39079
+ };
39080
+
39081
+ function isDataView(obj) {
39082
+ return obj && DataView.prototype.isPrototypeOf(obj)
39083
+ }
39084
+
39085
+ if (support.arrayBuffer) {
39086
+ var viewClasses = [
39087
+ '[object Int8Array]',
39088
+ '[object Uint8Array]',
39089
+ '[object Uint8ClampedArray]',
39090
+ '[object Int16Array]',
39091
+ '[object Uint16Array]',
39092
+ '[object Int32Array]',
39093
+ '[object Uint32Array]',
39094
+ '[object Float32Array]',
39095
+ '[object Float64Array]'
39096
+ ];
39097
+
39098
+ var isArrayBufferView =
39099
+ ArrayBuffer.isView ||
39100
+ function(obj) {
39101
+ return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
39102
+ };
39103
+ }
39104
+
39105
+ function normalizeName(name) {
39106
+ if (typeof name !== 'string') {
39107
+ name = String(name);
39108
+ }
39109
+ if (/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(name)) {
39110
+ throw new TypeError('Invalid character in header field name')
39111
+ }
39112
+ return name.toLowerCase()
39113
+ }
39114
+
39115
+ function normalizeValue(value) {
39116
+ if (typeof value !== 'string') {
39117
+ value = String(value);
39118
+ }
39119
+ return value
39120
+ }
39121
+
39122
+ // Build a destructive iterator for the value list
39123
+ function iteratorFor(items) {
39124
+ var iterator = {
39125
+ next: function() {
39126
+ var value = items.shift();
39127
+ return {done: value === undefined, value: value}
39128
+ }
39129
+ };
39130
+
39131
+ if (support.iterable) {
39132
+ iterator[Symbol.iterator] = function() {
39133
+ return iterator
39134
+ };
39135
+ }
39136
+
39137
+ return iterator
39138
+ }
39139
+
39140
+ function Headers(headers) {
39141
+ this.map = {};
39142
+
39143
+ if (headers instanceof Headers) {
39144
+ headers.forEach(function(value, name) {
39145
+ this.append(name, value);
39146
+ }, this);
39147
+ } else if (Array.isArray(headers)) {
39148
+ headers.forEach(function(header) {
39149
+ this.append(header[0], header[1]);
39150
+ }, this);
39151
+ } else if (headers) {
39152
+ Object.getOwnPropertyNames(headers).forEach(function(name) {
39153
+ this.append(name, headers[name]);
39154
+ }, this);
39155
+ }
39156
+ }
39157
+
39158
+ Headers.prototype.append = function(name, value) {
39159
+ name = normalizeName(name);
39160
+ value = normalizeValue(value);
39161
+ var oldValue = this.map[name];
39162
+ this.map[name] = oldValue ? oldValue + ', ' + value : value;
39163
+ };
39164
+
39165
+ Headers.prototype['delete'] = function(name) {
39166
+ delete this.map[normalizeName(name)];
39167
+ };
39168
+
39169
+ Headers.prototype.get = function(name) {
39170
+ name = normalizeName(name);
39171
+ return this.has(name) ? this.map[name] : null
39172
+ };
39173
+
39174
+ Headers.prototype.has = function(name) {
39175
+ return this.map.hasOwnProperty(normalizeName(name))
39176
+ };
39177
+
39178
+ Headers.prototype.set = function(name, value) {
39179
+ this.map[normalizeName(name)] = normalizeValue(value);
39180
+ };
39181
+
39182
+ Headers.prototype.forEach = function(callback, thisArg) {
39183
+ for (var name in this.map) {
39184
+ if (this.map.hasOwnProperty(name)) {
39185
+ callback.call(thisArg, this.map[name], name, this);
39186
+ }
39187
+ }
39188
+ };
39189
+
39190
+ Headers.prototype.keys = function() {
39191
+ var items = [];
39192
+ this.forEach(function(value, name) {
39193
+ items.push(name);
39194
+ });
39195
+ return iteratorFor(items)
39196
+ };
39197
+
39198
+ Headers.prototype.values = function() {
39199
+ var items = [];
39200
+ this.forEach(function(value) {
39201
+ items.push(value);
39202
+ });
39203
+ return iteratorFor(items)
39204
+ };
39205
+
39206
+ Headers.prototype.entries = function() {
39207
+ var items = [];
39208
+ this.forEach(function(value, name) {
39209
+ items.push([name, value]);
39210
+ });
39211
+ return iteratorFor(items)
39212
+ };
39213
+
39214
+ if (support.iterable) {
39215
+ Headers.prototype[Symbol.iterator] = Headers.prototype.entries;
39216
+ }
39217
+
39218
+ function consumed(body) {
39219
+ if (body.bodyUsed) {
39220
+ return Promise.reject(new TypeError('Already read'))
39221
+ }
39222
+ body.bodyUsed = true;
39223
+ }
39224
+
39225
+ function fileReaderReady(reader) {
39226
+ return new Promise(function(resolve, reject) {
39227
+ reader.onload = function() {
39228
+ resolve(reader.result);
39229
+ };
39230
+ reader.onerror = function() {
39231
+ reject(reader.error);
39232
+ };
39233
+ })
39234
+ }
39235
+
39236
+ function readBlobAsArrayBuffer(blob) {
39237
+ var reader = new FileReader();
39238
+ var promise = fileReaderReady(reader);
39239
+ reader.readAsArrayBuffer(blob);
39240
+ return promise
39241
+ }
39242
+
39243
+ function readBlobAsText(blob) {
39244
+ var reader = new FileReader();
39245
+ var promise = fileReaderReady(reader);
39246
+ reader.readAsText(blob);
39247
+ return promise
39248
+ }
39249
+
39250
+ function readArrayBufferAsText(buf) {
39251
+ var view = new Uint8Array(buf);
39252
+ var chars = new Array(view.length);
39253
+
39254
+ for (var i = 0; i < view.length; i++) {
39255
+ chars[i] = String.fromCharCode(view[i]);
39256
+ }
39257
+ return chars.join('')
39258
+ }
39259
+
39260
+ function bufferClone(buf) {
39261
+ if (buf.slice) {
39262
+ return buf.slice(0)
39263
+ } else {
39264
+ var view = new Uint8Array(buf.byteLength);
39265
+ view.set(new Uint8Array(buf));
39266
+ return view.buffer
39267
+ }
39268
+ }
39269
+
39270
+ function Body() {
39271
+ this.bodyUsed = false;
39272
+
39273
+ this._initBody = function(body) {
39274
+ this._bodyInit = body;
39275
+ if (!body) {
39276
+ this._bodyText = '';
39277
+ } else if (typeof body === 'string') {
39278
+ this._bodyText = body;
39279
+ } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
39280
+ this._bodyBlob = body;
39281
+ } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
39282
+ this._bodyFormData = body;
39283
+ } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
39284
+ this._bodyText = body.toString();
39285
+ } else if (support.arrayBuffer && support.blob && isDataView(body)) {
39286
+ this._bodyArrayBuffer = bufferClone(body.buffer);
39287
+ // IE 10-11 can't handle a DataView body.
39288
+ this._bodyInit = new Blob([this._bodyArrayBuffer]);
39289
+ } else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {
39290
+ this._bodyArrayBuffer = bufferClone(body);
39291
+ } else {
39292
+ this._bodyText = body = Object.prototype.toString.call(body);
39293
+ }
39294
+
39295
+ if (!this.headers.get('content-type')) {
39296
+ if (typeof body === 'string') {
39297
+ this.headers.set('content-type', 'text/plain;charset=UTF-8');
39298
+ } else if (this._bodyBlob && this._bodyBlob.type) {
39299
+ this.headers.set('content-type', this._bodyBlob.type);
39300
+ } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
39301
+ this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
39302
+ }
39303
+ }
39304
+ };
39305
+
39306
+ if (support.blob) {
39307
+ this.blob = function() {
39308
+ var rejected = consumed(this);
39309
+ if (rejected) {
39310
+ return rejected
39311
+ }
39312
+
39313
+ if (this._bodyBlob) {
39314
+ return Promise.resolve(this._bodyBlob)
39315
+ } else if (this._bodyArrayBuffer) {
39316
+ return Promise.resolve(new Blob([this._bodyArrayBuffer]))
39317
+ } else if (this._bodyFormData) {
39318
+ throw new Error('could not read FormData body as blob')
39319
+ } else {
39320
+ return Promise.resolve(new Blob([this._bodyText]))
39321
+ }
39322
+ };
39323
+
39324
+ this.arrayBuffer = function() {
39325
+ if (this._bodyArrayBuffer) {
39326
+ return consumed(this) || Promise.resolve(this._bodyArrayBuffer)
39327
+ } else {
39328
+ return this.blob().then(readBlobAsArrayBuffer)
39329
+ }
39330
+ };
39331
+ }
39332
+
39333
+ this.text = function() {
39334
+ var rejected = consumed(this);
39335
+ if (rejected) {
39336
+ return rejected
39337
+ }
39338
+
39339
+ if (this._bodyBlob) {
39340
+ return readBlobAsText(this._bodyBlob)
39341
+ } else if (this._bodyArrayBuffer) {
39342
+ return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer))
39343
+ } else if (this._bodyFormData) {
39344
+ throw new Error('could not read FormData body as text')
39345
+ } else {
39346
+ return Promise.resolve(this._bodyText)
39347
+ }
39348
+ };
39349
+
39350
+ if (support.formData) {
39351
+ this.formData = function() {
39352
+ return this.text().then(decode)
39353
+ };
39354
+ }
39355
+
39356
+ this.json = function() {
39357
+ return this.text().then(JSON.parse)
39358
+ };
39359
+
39360
+ return this
39361
+ }
39362
+
39363
+ // HTTP methods whose capitalization should be normalized
39364
+ var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'];
39365
+
39366
+ function normalizeMethod(method) {
39367
+ var upcased = method.toUpperCase();
39368
+ return methods.indexOf(upcased) > -1 ? upcased : method
39369
+ }
39370
+
39371
+ function Request(input, options) {
39372
+ options = options || {};
39373
+ var body = options.body;
39374
+
39375
+ if (input instanceof Request) {
39376
+ if (input.bodyUsed) {
39377
+ throw new TypeError('Already read')
39378
+ }
39379
+ this.url = input.url;
39380
+ this.credentials = input.credentials;
39381
+ if (!options.headers) {
39382
+ this.headers = new Headers(input.headers);
39383
+ }
39384
+ this.method = input.method;
39385
+ this.mode = input.mode;
39386
+ this.signal = input.signal;
39387
+ if (!body && input._bodyInit != null) {
39388
+ body = input._bodyInit;
39389
+ input.bodyUsed = true;
39390
+ }
39391
+ } else {
39392
+ this.url = String(input);
39393
+ }
39394
+
39395
+ this.credentials = options.credentials || this.credentials || 'same-origin';
39396
+ if (options.headers || !this.headers) {
39397
+ this.headers = new Headers(options.headers);
39398
+ }
39399
+ this.method = normalizeMethod(options.method || this.method || 'GET');
39400
+ this.mode = options.mode || this.mode || null;
39401
+ this.signal = options.signal || this.signal;
39402
+ this.referrer = null;
39403
+
39404
+ if ((this.method === 'GET' || this.method === 'HEAD') && body) {
39405
+ throw new TypeError('Body not allowed for GET or HEAD requests')
39406
+ }
39407
+ this._initBody(body);
39408
+ }
39409
+
39410
+ Request.prototype.clone = function() {
39411
+ return new Request(this, {body: this._bodyInit})
39412
+ };
39413
+
39414
+ function decode(body) {
39415
+ var form = new FormData();
39416
+ body
39417
+ .trim()
39418
+ .split('&')
39419
+ .forEach(function(bytes) {
39420
+ if (bytes) {
39421
+ var split = bytes.split('=');
39422
+ var name = split.shift().replace(/\+/g, ' ');
39423
+ var value = split.join('=').replace(/\+/g, ' ');
39424
+ form.append(decodeURIComponent(name), decodeURIComponent(value));
39425
+ }
39426
+ });
39427
+ return form
39428
+ }
39429
+
39430
+ function parseHeaders(rawHeaders) {
39431
+ var headers = new Headers();
39432
+ // Replace instances of \r\n and \n followed by at least one space or horizontal tab with a space
39433
+ // https://tools.ietf.org/html/rfc7230#section-3.2
39434
+ var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ');
39435
+ preProcessedHeaders.split(/\r?\n/).forEach(function(line) {
39436
+ var parts = line.split(':');
39437
+ var key = parts.shift().trim();
39438
+ if (key) {
39439
+ var value = parts.join(':').trim();
39440
+ headers.append(key, value);
39441
+ }
39442
+ });
39443
+ return headers
39444
+ }
39445
+
39446
+ Body.call(Request.prototype);
39447
+
39448
+ function Response(bodyInit, options) {
39449
+ if (!options) {
39450
+ options = {};
39451
+ }
39452
+
39453
+ this.type = 'default';
39454
+ this.status = options.status === undefined ? 200 : options.status;
39455
+ this.ok = this.status >= 200 && this.status < 300;
39456
+ this.statusText = 'statusText' in options ? options.statusText : 'OK';
39457
+ this.headers = new Headers(options.headers);
39458
+ this.url = options.url || '';
39459
+ this._initBody(bodyInit);
39460
+ }
39461
+
39462
+ Body.call(Response.prototype);
39463
+
39464
+ Response.prototype.clone = function() {
39465
+ return new Response(this._bodyInit, {
39466
+ status: this.status,
39467
+ statusText: this.statusText,
39468
+ headers: new Headers(this.headers),
39469
+ url: this.url
39470
+ })
39471
+ };
39472
+
39473
+ Response.error = function() {
39474
+ var response = new Response(null, {status: 0, statusText: ''});
39475
+ response.type = 'error';
39476
+ return response
39477
+ };
39478
+
39479
+ var redirectStatuses = [301, 302, 303, 307, 308];
39480
+
39481
+ Response.redirect = function(url, status) {
39482
+ if (redirectStatuses.indexOf(status) === -1) {
39483
+ throw new RangeError('Invalid status code')
39484
+ }
39485
+
39486
+ return new Response(null, {status: status, headers: {location: url}})
39487
+ };
39488
+
39489
+ exports.DOMException = self.DOMException;
39490
+ try {
39491
+ new exports.DOMException();
39492
+ } catch (err) {
39493
+ exports.DOMException = function(message, name) {
39494
+ this.message = message;
39495
+ this.name = name;
39496
+ var error = Error(message);
39497
+ this.stack = error.stack;
39498
+ };
39499
+ exports.DOMException.prototype = Object.create(Error.prototype);
39500
+ exports.DOMException.prototype.constructor = exports.DOMException;
39501
+ }
39502
+
39503
+ function fetch(input, init) {
39504
+ return new Promise(function(resolve, reject) {
39505
+ var request = new Request(input, init);
39506
+
39507
+ if (request.signal && request.signal.aborted) {
39508
+ return reject(new exports.DOMException('Aborted', 'AbortError'))
39509
+ }
39510
+
39511
+ var xhr = new XMLHttpRequest();
39512
+
39513
+ function abortXhr() {
39514
+ xhr.abort();
39515
+ }
39516
+
39517
+ xhr.onload = function() {
39518
+ var options = {
39519
+ status: xhr.status,
39520
+ statusText: xhr.statusText,
39521
+ headers: parseHeaders(xhr.getAllResponseHeaders() || '')
39522
+ };
39523
+ options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL');
39524
+ var body = 'response' in xhr ? xhr.response : xhr.responseText;
39525
+ resolve(new Response(body, options));
39526
+ };
39527
+
39528
+ xhr.onerror = function() {
39529
+ reject(new TypeError('Network request failed'));
39530
+ };
39531
+
39532
+ xhr.ontimeout = function() {
39533
+ reject(new TypeError('Network request failed'));
39534
+ };
39535
+
39536
+ xhr.onabort = function() {
39537
+ reject(new exports.DOMException('Aborted', 'AbortError'));
39538
+ };
39539
+
39540
+ xhr.open(request.method, request.url, true);
39541
+
39542
+ if (request.credentials === 'include') {
39543
+ xhr.withCredentials = true;
39544
+ } else if (request.credentials === 'omit') {
39545
+ xhr.withCredentials = false;
39546
+ }
39547
+
39548
+ if ('responseType' in xhr && support.blob) {
39549
+ xhr.responseType = 'blob';
39550
+ }
39551
+
39552
+ request.headers.forEach(function(value, name) {
39553
+ xhr.setRequestHeader(name, value);
39554
+ });
39555
+
39556
+ if (request.signal) {
39557
+ request.signal.addEventListener('abort', abortXhr);
39558
+
39559
+ xhr.onreadystatechange = function() {
39560
+ // DONE (success or failure)
39561
+ if (xhr.readyState === 4) {
39562
+ request.signal.removeEventListener('abort', abortXhr);
39563
+ }
39564
+ };
39565
+ }
39566
+
39567
+ xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit);
39568
+ })
39569
+ }
39570
+
39571
+ fetch.polyfill = true;
39572
+
39573
+ if (!self.fetch) {
39574
+ self.fetch = fetch;
39575
+ self.Headers = Headers;
39576
+ self.Request = Request;
39577
+ self.Response = Response;
39578
+ }
39579
+
39580
+ exports.Headers = Headers;
39581
+ exports.Request = Request;
39582
+ exports.Response = Response;
39583
+ exports.fetch = fetch;
39584
+
39585
+ Object.defineProperty(exports, '__esModule', { value: true });
39586
+
39587
+ return exports;
39588
+
39589
+ }))({});
39590
+ })(__self__);
39591
+ __self__.fetch.ponyfill = true;
39592
+ // Remove "polyfill" property added by whatwg-fetch
39593
+ delete __self__.fetch.polyfill;
39594
+ // Choose between native implementation (global) or custom implementation (__self__)
39595
+ // var ctx = global.fetch ? global : __self__;
39596
+ var ctx = __self__; // this line disable service worker support temporarily
39597
+ exports = ctx.fetch; // To enable: import fetch from 'cross-fetch'
39598
+ exports.default = ctx.fetch; // For TypeScript consumers without esModuleInterop.
39599
+ exports.fetch = ctx.fetch; // To enable: import {fetch} from 'cross-fetch'
39600
+ exports.Headers = ctx.Headers;
39601
+ exports.Request = ctx.Request;
39602
+ exports.Response = ctx.Response;
39603
+ module.exports = exports;
39604
+ } (browserPonyfill, browserPonyfillExports));
39605
+
39606
+ var fetch = /*@__PURE__*/getDefaultExportFromCjs(browserPonyfillExports);
39607
+
39608
+ const defaultConfig = {
39609
+ url: 'http://localhost:4000'
39610
+ };
39611
+
39612
+ class Rest extends Base {
39613
+ constructor (name, config = {}) {
39614
+ super(name, Object.assign({}, defaultConfig, config));
39615
+ // console.log('Rest', name, config)
39616
+ }
39617
+
39618
+ headerURL (branch = 'main') {
39619
+ return `${this.config.url}/${branch}.json`
39620
+ }
39621
+
39622
+ async writeCars (cars) {
39623
+ if (this.config.readonly) return
39624
+ for (const { cid, bytes } of cars) {
39625
+ const carURL = `${this.config.url}/${cid.toString()}.car`;
39626
+ const response = await fetch(carURL, {
39627
+ method: 'PUT',
39628
+ body: bytes,
39629
+ headers: { 'Content-Type': 'application/car' }
39630
+ });
39631
+ if (!response.ok) throw new Error(`An error occurred: ${response.statusText}`)
39632
+ }
39633
+ }
39634
+
39635
+ async readCar (carCid) {
39636
+ const carURL = `${this.config.url}/${carCid.toString()}.car`;
39637
+ const response = await fetch(carURL);
39638
+ if (!response.ok) throw new Error(`An error occurred: ${response.statusText}`)
39639
+ const got = await response.arrayBuffer();
39640
+ return new Uint8Array(got)
39641
+ }
39642
+
39643
+ async loadHeader (branch = 'main') {
39644
+ const response = await fetch(this.headerURL(branch));
39645
+ // console.log('rest getHeader', response.constructor.name)
39646
+ if (!response.ok) return null
39647
+ const got = await response.json();
39648
+ // console.log('rest getHeader', got)
39649
+ return got
39650
+ }
39651
+
39652
+ async writeHeader (branch, header) {
39653
+ if (this.config.readonly) return
39654
+ const pHeader = this.prepareHeader(header);
39655
+ // console.log('writeHeader rt', branch, pHeader)
39656
+
39657
+ const response = await fetch(this.headerURL(branch), {
39658
+ method: 'PUT',
39659
+ body: pHeader,
39660
+ headers: { 'Content-Type': 'application/json' }
39661
+ });
39662
+ if (!response.ok) throw new Error(`An error occurred: ${response.statusText}`)
39663
+ }
39664
+ }
39044
39665
 
39045
39666
  const Loader = {
39046
39667
  appropriate: (name, config = {}) => {
@@ -39048,9 +39669,9 @@ const Loader = {
39048
39669
  return new config.StorageClass(name, config)
39049
39670
  }
39050
39671
 
39051
- // if (config.type === 'rest') {
39052
- // return new Rest(name, config)
39053
- // }
39672
+ if (config.type === 'rest') {
39673
+ return new Rest(name, config)
39674
+ }
39054
39675
 
39055
39676
  return new Browser(name, config)
39056
39677
  }