Giter VIP home page Giter VIP logo

origin-storage_good's Introduction

origin-storage

Node CI npm version

A same-origin storage for cross-domain access, it is based on localForage and supports IndexedDB, WebSQL and localStorage.

origin-storage uses localStorage in browsers with no IndexedDB or WebSQL support. And Safari is not supported.

Table of Contents

Motivation

When different Website domains need a same-origin storage container, we have to use iframe's same-origin policy Web local storage solution. localForage is an excellent storage library, it supports IndexedDB, WebSQL and localStorage, but it can't solve this problem directly.

That's why we have this library for same-origin storage based on localForage.

Installation

yarn add origin-storage

Usage and Example

  • Use OriginStorage on http://localhost:9000/storage.js:
import { OriginStorage } from 'origin-storage';

const originStorage = new OriginStorage();

If you need to set up a more secure origin control, you can set targetOrigin like this.

const originStorage = new OriginStorage({
  targetOrigin: 'http://example.com',
});
  • Create and host a Web page(http://localhost:9000/storage.html) containing JavaScript file storage.js.

  • Use OriginStorageClient on a cross-domain page:

import { OriginStorageClient } from 'origin-storage';

const originStorageClient = new OriginStorageClient({
  uri: 'http://localhost:9000/storage.html',
});

API

OriginStorage

  • new OriginStorage(options)
interface OriginStorageOptions extends IFrameTransportInternalOptions {
  /**
   * Enable read access to OriginStorage.
   */
  read?: boolean;
  /**
   * Enable write access to OriginStorage.
   */
  write?: boolean;
  /**
   * Enable broadcast data changes on OriginStorage.
   */
  broadcastChanges?: boolean;
  /**
   * Specify broadcastChannel name.
   */
  broadcastChannelName?: string;
}

OriginStorageClient

  • new OriginStorageClient(options)
interface OriginStorageClientOptions extends IFrameMainTransportOptions {
  /**
   * Specify the uri of an OriginStorage container.
   */
  uri: string;
  /**
   * Set storage options for localforage.
   */
  storageOptions?: LocalForageOptions;
}
  • OriginStorageClient instance methods.
interface IOriginStorageClient {
  /**
   * The callback will be called when the iframe is connected.
   */
  onConnect(callback: () => void): void;
  /**
   * The callback will be called when the storage is changed.
   */
  onChange(callback: (data: IChangeData) => void): Promise<{
    off: () => void;
    broadcastChanges: boolean;
  }>;
  /**
   * Get the value of the specified key.
   */
  getItem(key: string): Promise<any>;
  /**
   * Set the value of the specified key.
   */
  setItem(key: string, value: any): Promise<void>;
  /**
   * Remove the value of the specified key.
   */
  removeItem(key: string): Promise<void>;
  /**
   * Clear all key/value pairs in the storage.
   */
  clear(): Promise<void>;
  /**
   * Get the number of key/value pairs in the storage.
   */
  length(): Promise<number>;
  /**
   * Get the name of the nth key in the storage.
   */
  key(index: number): Promise<string>;
  /**
   * Get all keys in the storage.
   */
  keys(): Promise<string[]>;
}

origin-storage_good's People

Contributors

unadlib avatar dependabot[bot] avatar caitlinweb avatar

Watchers

 avatar

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.