GithubHelp home page GithubHelp logo

googlechromelabs / kv-storage-polyfill Goto Github PK

View Code? Open in Web Editor NEW
147.0 7.0 6.0 26 KB

A polyfill for the kv-storage built-in module.

Home Page: https://developers.google.com/web/updates/2019/03/kv-storage

License: Apache License 2.0

JavaScript 100.00%
polyfill import-maps

kv-storage-polyfill's Introduction

kv-storage polyfill

A polyfill for the kv-storage built-in module.

Usage

First, ensure you have an Import Map defined to enable the browser version where supported:

<script type="importmap">
{
  "imports": {
    "/node_modules/kv-storage/dist/kv-storage-polyfill.mjs": [
      "std:kv-storage",
      "/node_modules/kv-storage/dist/kv-storage-polyfill.mjs"
    ]
  }
}
</script>

Then import the polyfill. If the browser supports std:kv-storage natively, it will use that instead:

import { storage } from '/node_modules/kv-storage/dist/kv-storage-polyfill.mjs';

(async () => {
  await storage.set("mycat", "Tom");
  console.assert(await storage.get("mycat") === "Tom");

  console.log(await storage.entries());
  // Logs [["mycat", "Tom"]]

  await storage.delete("mycat");
  console.assert(await storage.get("mycat") === undefined);
})();

Available Module Formats

ES Modules: (for everything)

import storage from 'kv-storage-polyfill';  // default storage namespace
import { StorageArea } from 'kv-storage-polyfill';  // instantiable StorageArea class
import storage, { StorageArea } from 'kv-storage-polyfill';  // you can combine

CommonJS: (for Node/browserify)

const storage = require('kv-storage-polyfill');  // default storage namespace
const storage = require('kv-storage-polyfill').default  // also works, just an alias
const { StorageArea } = require('kv-storage-polyfill');  // instantiable StorageArea class

UMD/AMD: (for compatibility)

define(['/web/kv-storage-polyfill.umd.js'], function(storage) {
  storage === storage.default // default storage area
  storage.StorageArea  // instantiable StorageArea class
});

Browser Globals: (for demos)

<script src="/web/kv-storage/polyfill.umd.js"></script>
<script>
  const { StorageArea } = kvStoragePolyfill;
  const storage = kvStoragePolyfill;  // optional `.default` if you want
</script>

License

Apache 2

kv-storage-polyfill's People

Contributors

bperson avatar developit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

kv-storage-polyfill's Issues

ReferenceError: self is not defined

I tried to use this polyfill with Node.js, but trying to perform a .get causes an exception. Here's a repro:

const storage = require('kv-storage-polyfill').default
storage.get("a")

This happens on Node.js v12.5.0 on macOS.

Update example for storage.entries()

In the example code in the usage section of the readme, it has:

console.log(await storage.entries());
// Logs [["mycat", "Tom"]]

but this will produce an empty object because storage.entries() is async iterable. The console output looks like this currently:

d {}
__proto__:
next: ƒ ()
return: ƒ ()
Symbol(Symbol.asyncIterator): ƒ ()
constructor: ƒ ()
__proto__: Object

Perhaps the example can include something like this instead:

for await (const [k, v] of storage.entries()) {
  console.log( [k, v] );
  // Logs ["mycat", "Tom"]
}

File location confusion

The repository has a src/ directory with .js files

The distributed package has a dist/ directory with .js and .mjs files

The README suggests using /node_modules/kv-storage/index.mjs which exists in neither case.

What's the plan?

In Chrome Canary, export is not found in kv-storage-polyfill.

Wasn't able to submitt a Chromium bug (got 400 error trying to submit the form), so pinging here instead.

On https://podd.app/ in Chrome Canary I'm getting:

When I open the file I see a S as storage export though... 🤔

Chrome 79.0.3915.0 canary (64 bit)
bcb279bcb4aab2d9bfa914d87584a506da473e1a-refs/branch-heads/3915@{#1}

Works fine in Chrome stable though.

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.