项目作者: MatthewZito

项目描述 :
Cross-domain storage for the frontend
高级语言: JavaScript
项目地址: git://github.com/MatthewZito/seance.git
创建时间: 2021-04-16T06:22:04Z
项目社区:https://github.com/MatthewZito/seance

开源协议:MIT License

下载


Séance | Cross-origin State Sharing

Build Status
Coverage Status
npm version
License: MIT

Séance enables cross-domain state sharing via the browser’s local storage.

A Séance can be agreed upon by any number of domains. A Seance instance is initialized at the domain you want to serve as the provider. Each domain that subscribes to this shared state registers a Medium, allowing it to observe the shared state, and perform read / write transactions with it.

Mediums use iframes to proxy the Seance provider. When initialized, a Seance will listen for any Medium in its list of registered domains (this can be explicit strings or a group of regular expressions). Once a Medium registers itself with the Seance, it can begin a sequence. A sequence is a Promise that when fulfilled provides an API exposing get, set, delete, and enumerate operations that read from and write to the shared state.

Mediums poll the Seance connection in a handshake exchange series of SYN/ACK messages. If at any point the connection is lost, interrupted, or ended, the sequence will reject.

Table of Contents

Supported Environments" class="reference-link"> Supported Environments

seance is a frontend system that currently supports UMD and ESM build-targets. Is your preferred build not supported? Open an issue!

Installation and Usage" class="reference-link"> Installation and Usage

Install:

  1. npm install seance-js

or

  1. yarn add seance-js

Initializing a Seance:

  1. // at https://domain.com
  2. import { Seance } from 'seance-js';
  3. Seance(['https://otherdomain.com']);

Registering Mediums:

  1. // at https://otherdomain.com
  2. import { Medium } from 'seance-js';
  3. function handleConnErr (ex) { ... }
  4. const medium = Medium({
  5. // the origin of the `Seance`
  6. seanceOrigin: 'https://domain.com',
  7. // callback to invoke when `Medium` is initialized, receives the `Medium`'s uuid
  8. created: (id) => console.log({ CREATED: id }),
  9. // callback to invoke when `Medium` is unmounted, receives the `Medium`'s uuid
  10. destroyed: (id) => console.log({ DESTROYED: id })
  11. });
  12. const keysToSet = [{ key1: 'value1'}, { key2: 'value2' }];
  13. // begin sequence
  14. medium.sequence()
  15. // connection OK, return api
  16. .then(api => {
  17. // set key/val pairs in shared state
  18. api.set(
  19. keysToSet,
  20. // each api method accepts a callback that is invoked when the operation succeeds (or fails)
  21. function (error, response) {
  22. if (error) // handle err
  23. else if (response) // handle response
  24. });
  25. })
  26. .catch(handleConnErr);
  27. const keysToGet = ['key1'];
  28. medium.sequence()
  29. .then(api => {
  30. api.get(keysToGet, (error, response) => ...)
  31. })
  32. .catch(handleConnErr);
  33. medium.sequence()
  34. .then(api => {
  35. api.delete(['key2'], (error, response) => ...);
  36. })
  37. .catch(handleConnErr);

Documentation and API" class="reference-link"> Documentation and API

Upcoming Features" class="reference-link"> Upcoming Features

  • permissions
  • enumerate and clear APIs
  • adapters for different browser storage types
  • IE support

Testing" class="reference-link"> Testing

This package is tested with Jest and JSDOM. Test suites must be run serially with --runInBand to avoid cross-pool propagation of events.