Enables shared cross domain localStorage and sessionStorage.
- Server
Listens for a window messages from allowed domains. If the client domain, method or storage type is not allowed the message will send back error message. - Client
On Connections creates an invisible iframe with specified domain assrc
attribute and appends it todocument.body
. Client communicates with that iframe through posting and listing to messages.
All communications are handled with iframe.contentWindow.postMessage
, window.top.postMessage
and window.addEventListener
npm i @repugraf/cross-domain-storage
import { getServer, getClient } from "https://esm.sh/@repugraf/cross-domain-storage";
import { getServer } from "@repugraf/cross-domain-storage";
const server = getServer({
allowedDomains: [
{
origin: /sub1.example.com$/,
allowedMethods: ["get", "set", "remove"]
},
{
origin: /sub2.example.com$/,
allowedMethods: ["get"]
}
]
});
await server.listen();
import { getClient } from "@repugraf/cross-domain-storage";
const client = getClient({
domain: "https://www.example.com"
});
await client.connect();
await client.set("key", "val");
await client.get("key"); // "val"
The library is documented with JSDoc and TypeScript definitions. All details and types should be highlighted in most commonly used IDEs (VSCode, WebStorm)
The library includes build in typescript definitions.