Coder Social home page Coder Social logo

nullixat / browstorjs Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 1.0 120 KB

Persistent key/value data storage for your Browser and/or PWA, promisified, including file support and service worker support, all with IndexedDB. Perfectly suitable for your next (PWA) app.

Home Page: https://nullixat.github.io/browstorjs/

License: MIT License

TypeScript 97.08% JavaScript 2.92%
browser indexeddb pwa storage app async-await javascript promisified service-worker typescript

browstorjs's Introduction

Logo

BrowstorJS ๐Ÿš€ ๐Ÿ’พ ๐Ÿ”’ Tests

Persistent key/value data storage for your Browser and/or PWA, promisified, including file support and service worker support, all with IndexedDB. Perfectly suitable for your next (PWA) app.

Features ๐Ÿ“ฃ

  • Simple Key/Value Data Storage in IndexedDB
  • Serve any storage value as a real URL (No Data URI) for Images, Files, etc...
  • Promisified for async/await support
  • Cross-Browser
    • Chrome (Mobile/Desktop incl. incognito mode)
    • Firefox (Mobile/Desktop but not in private mode)
    • Safari (Mobile/Desktop incl. partially in InPrivate Mode)
    • Edge New (Chromium incl. private mode)
    • Edge Old v17+
    • WebKit
    • and every other from the last years
    • No Internet Explorer :trollface:
  • Super Lightweight (~400 byte when gzipped, ~4kb uncompressed)
  • Notice: A word about persistence in current browsers...

Usage โšก

const db = await BrowstorJS.open() // get instance
await db.set('mykey', 'myvalue') // set a value
await db.get('mykey') // get a value
await db.getUrl('mykey') // get a URL that serves the value from this key (eg.: for images)
await db.getDataUri('mykey') // get a data uri (to use as image src for example) for the value of this key
await db.search((key, value) => { return key.startsWith('mykey') }) // search entries with condition
await db.remove('mykey') // remove a single key
await db.reset() // clear the database, delete all entries
await db.getKeys() // ['mykey', ...]
const db = await BrowstorJS.open('myotherdb') // get instance to a separate db
const isPersistent = await BrowstorJS.requestPersistentStorage() // request persistent storage
const info = await BrowstorJS.getStorageSpaceInfo() // {available:number, used:number, free:number}

Jump to Event registration inside service worker to make the function db.getUrl() to actually work.

Demo ๐Ÿ‘พ

Head to our demo page to see and test the library in action.

Install ๐Ÿ’ฟ

Variant Self-Hosted Self-Download

Download the latest release, unpack the dist/browstorjs.js and load it into your website <script src="browstorjs.js"></script>.

Variant using TypeScript

Download the latest release (Or use NPM to install the library) and include src/browstorjs.ts wherever you need it.

Variant using NPM

npm install browstorjs

You know how to pack the library into your website when you opt in for using npm. It depends on your environment how you integrate the dist file.

Variant using CDN (Not recommended but handy for quick tests)

Load it into your website <script src="https://cdn.jsdelivr.net/npm/browstorjs/dist/browstorjs.js"></script>.

Event registration inside service worker ๐ŸŽท

To make the generation of getUrl work, you need to handle some service worker events. If you don't need getUrl you also don't necessarily need a service worker.

This is the bare minimum inside a service worker, you can add your custom code after the BrowstorJS handlers.

importScripts('scripts/browstorjs.js')

self.addEventListener('activate', event => {
  if (BrowstorJS.handleServiceWorkerEvents(event)) return
  // place your additional app code here
})

self.addEventListener('fetch', event => {
  if (BrowstorJS.handleServiceWorkerEvents(event)) return
  // place your additional app code here
})

self.addEventListener('message', event => {
  if (BrowstorJS.handleServiceWorkerEvents(event)) return
  // place your additional app code here
})

Persistence - How browsers handle it :shipit:

One thing you must have definitely in mind is that, to date, persistence in browser is wanky. IndexedDB Storage is persistence over time and after browser is closed, yes, but it can be wiped easily. Even when your app is installed as a PWA. By cleanup jobs, by long inactivity, by history cleanup, etc...

For PWA (as of July 2022), unfortunetely, there is still no real 100% bullet-proof way to store data forever until the app is deleted, like you can do in native apps. We all hope that browser devs will fix this as soon as possible.

Here a few links to show how browser engines handle IndexedDB Storage, which BrowstorJS internally uses:

Development in this library ๐Ÿ’Œ

  1. Create an issue for features and bugs
  2. Checkout master
  3. Run npm install && npm ci && npx playwright install --with-deps
  4. After changing src/browserjs.ts, run npm run dist
  5. Check tests and add new tests to docs/test.html when adding new features

browstorjs's People

Contributors

brainfoolong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

pwfoo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.