monoidentity 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Monoidentity.svelte +16 -0
- package/dist/Monoidentity.svelte.d.ts +10 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/storage-private-local.d.ts +1 -0
- package/dist/storage-private-local.js +63 -0
- package/dist/storage.d.ts +2 -0
- package/dist/storage.js +27 -0
- package/dist/trackready.d.ts +2 -0
- package/dist/trackready.js +39 -0
- package/dist/utils.d.ts +31 -0
- package/dist/utils.js +26 -0
- package/package.json +48 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from "svelte";
|
|
3
|
+
import type { Scope } from "../../../src/lib.js";
|
|
4
|
+
import { trackReady } from "./trackready.js";
|
|
5
|
+
|
|
6
|
+
let { app, scopes, children }: { app: string; scopes: Scope[]; children: Snippet } = $props();
|
|
7
|
+
|
|
8
|
+
let ready = $state(false);
|
|
9
|
+
trackReady(app, scopes, () => (ready = true));
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
{#if ready}
|
|
13
|
+
{@render children()}
|
|
14
|
+
{:else}
|
|
15
|
+
<p style:margin="auto">Setting up</p>
|
|
16
|
+
{/if}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Snippet } from "svelte";
|
|
2
|
+
import type { Scope } from "../../../src/lib.js";
|
|
3
|
+
type $$ComponentProps = {
|
|
4
|
+
app: string;
|
|
5
|
+
scopes: Scope[];
|
|
6
|
+
children: Snippet;
|
|
7
|
+
};
|
|
8
|
+
declare const Monoidentity: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
9
|
+
type Monoidentity = ReturnType<typeof Monoidentity>;
|
|
10
|
+
export default Monoidentity;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const init: () => Record<string, string>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const prefix = "monoidentity";
|
|
2
|
+
const prefixed = (key) => `${prefix}/${key}`;
|
|
3
|
+
const unprefixed = (key) => {
|
|
4
|
+
if (!key.startsWith(`${prefix}/`))
|
|
5
|
+
throw new Error("Key is not prefixed");
|
|
6
|
+
return key.slice(prefix.length + 1);
|
|
7
|
+
};
|
|
8
|
+
const target = {};
|
|
9
|
+
const get = (_, key) => {
|
|
10
|
+
if (typeof key == "string") {
|
|
11
|
+
const value = localStorage.getItem(prefixed(key));
|
|
12
|
+
if (value == null)
|
|
13
|
+
return undefined;
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(value);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
};
|
|
23
|
+
export const init = () => new Proxy(target, {
|
|
24
|
+
get,
|
|
25
|
+
set(_, key, value) {
|
|
26
|
+
if (typeof key == "string") {
|
|
27
|
+
const serialized = typeof value == "string" ? value : JSON.stringify(value);
|
|
28
|
+
localStorage.setItem(prefixed(key), serialized);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
},
|
|
33
|
+
deleteProperty(_, key) {
|
|
34
|
+
if (typeof key == "string") {
|
|
35
|
+
localStorage.removeItem(prefixed(key));
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
},
|
|
40
|
+
has(_, key) {
|
|
41
|
+
return typeof key == "string" && localStorage.getItem(prefixed(key)) !== null;
|
|
42
|
+
},
|
|
43
|
+
ownKeys(_) {
|
|
44
|
+
const keys = [];
|
|
45
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
46
|
+
const key = localStorage.key(i);
|
|
47
|
+
if (key && key.startsWith(`${prefix}_`)) {
|
|
48
|
+
keys.push(unprefixed(key));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return keys;
|
|
52
|
+
},
|
|
53
|
+
getOwnPropertyDescriptor(_, key) {
|
|
54
|
+
if (typeof key == "string" && localStorage.getItem(prefixed(key)) !== null) {
|
|
55
|
+
return {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
value: get(target, key),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
},
|
|
63
|
+
});
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { stringify, parse } from "devalue";
|
|
2
|
+
let implementation;
|
|
3
|
+
let app = "";
|
|
4
|
+
export const setup = (i, a) => {
|
|
5
|
+
implementation = i;
|
|
6
|
+
app = a;
|
|
7
|
+
};
|
|
8
|
+
export const getStorage = (realm) => {
|
|
9
|
+
const prefix = (text) => `.${realm}/${app}/${text}`;
|
|
10
|
+
const storage = implementation;
|
|
11
|
+
if (!app)
|
|
12
|
+
throw new Error("No app set");
|
|
13
|
+
if (!storage)
|
|
14
|
+
throw new Error("No implementation set");
|
|
15
|
+
return new Proxy({}, {
|
|
16
|
+
get(_, key) {
|
|
17
|
+
const item = storage[prefix(key)];
|
|
18
|
+
if (!item)
|
|
19
|
+
return undefined;
|
|
20
|
+
return parse(item);
|
|
21
|
+
},
|
|
22
|
+
set(_, key, value) {
|
|
23
|
+
storage[prefix(key)] = stringify(value);
|
|
24
|
+
return true;
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { rememberCallback } from "../../../src/lib.js";
|
|
2
|
+
import { init as initLocal } from "./storage-private-local.js";
|
|
3
|
+
import { setup } from "./storage.js";
|
|
4
|
+
export const trackReady = (app, scopes, callback) => {
|
|
5
|
+
const params = new URLSearchParams(location.hash.slice(1));
|
|
6
|
+
let memory = localStorage.monoidentityMemory
|
|
7
|
+
? JSON.parse(localStorage.monoidentityMemory)
|
|
8
|
+
: undefined;
|
|
9
|
+
let createNew = false;
|
|
10
|
+
const paramCB = params.get("monoidentitycallback");
|
|
11
|
+
if (paramCB) {
|
|
12
|
+
history.replaceState(null, "", location.pathname);
|
|
13
|
+
const cb = JSON.parse(paramCB);
|
|
14
|
+
console.log("got callback", cb); // todo remove
|
|
15
|
+
if (cb.connect.method == "file" && cb.connect.createNew) {
|
|
16
|
+
createNew = true;
|
|
17
|
+
}
|
|
18
|
+
memory = rememberCallback(cb, memory);
|
|
19
|
+
localStorage.monoidentityMemory = JSON.stringify(memory);
|
|
20
|
+
}
|
|
21
|
+
if (!memory) {
|
|
22
|
+
const params = new URLSearchParams();
|
|
23
|
+
params.set("app", app);
|
|
24
|
+
params.set("scopes", scopes.join(","));
|
|
25
|
+
params.set("redirectURI", location.origin);
|
|
26
|
+
location.href = `https://usemonoidentity.web.app/#${params.toString()}`;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (memory.method == "cloud") {
|
|
30
|
+
// TODO
|
|
31
|
+
}
|
|
32
|
+
else if (memory.method == "file") {
|
|
33
|
+
// TODO (use createNew here)
|
|
34
|
+
}
|
|
35
|
+
else if (memory.method == "localStorage") {
|
|
36
|
+
setup(initLocal(), app);
|
|
37
|
+
}
|
|
38
|
+
callback();
|
|
39
|
+
};
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const scopeDefs: {
|
|
2
|
+
"login-recognized": {
|
|
3
|
+
files: string[];
|
|
4
|
+
};
|
|
5
|
+
storage: {
|
|
6
|
+
files: never[];
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export type Scope = keyof typeof scopeDefs;
|
|
10
|
+
export declare const supportsFile: boolean;
|
|
11
|
+
type Setup = {
|
|
12
|
+
method: "cloud";
|
|
13
|
+
jwt: string;
|
|
14
|
+
} | {
|
|
15
|
+
method: "file";
|
|
16
|
+
createNew?: never;
|
|
17
|
+
} | {
|
|
18
|
+
method: "localStorage";
|
|
19
|
+
};
|
|
20
|
+
export type Memory = Setup & {
|
|
21
|
+
knownFiles: string[];
|
|
22
|
+
};
|
|
23
|
+
export type Callback = {
|
|
24
|
+
connect: Setup | {
|
|
25
|
+
method: "file";
|
|
26
|
+
createNew: boolean;
|
|
27
|
+
};
|
|
28
|
+
fileTasks: Record<string, string> | undefined;
|
|
29
|
+
};
|
|
30
|
+
export declare const rememberCallback: (data: Callback, pastMemory?: Memory) => Memory;
|
|
31
|
+
export {};
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const scopeDefs = {
|
|
2
|
+
"login-recognized": {
|
|
3
|
+
files: [".config/login.encjson"],
|
|
4
|
+
},
|
|
5
|
+
storage: {
|
|
6
|
+
files: [],
|
|
7
|
+
},
|
|
8
|
+
};
|
|
9
|
+
export const supportsFile = "showSaveFilePicker" in window;
|
|
10
|
+
export const rememberCallback = (data, pastMemory) => {
|
|
11
|
+
const { connect, fileTasks } = data;
|
|
12
|
+
const setup = connect.method == "cloud" ? { method: "cloud", jwt: connect.jwt } : { method: connect.method };
|
|
13
|
+
let knownFilesSet = new Set();
|
|
14
|
+
if (pastMemory) {
|
|
15
|
+
for (const file of pastMemory.knownFiles) {
|
|
16
|
+
knownFilesSet.add(file);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (fileTasks) {
|
|
20
|
+
for (const file of Object.keys(fileTasks)) {
|
|
21
|
+
knownFilesSet.add(file);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const knownFiles = Array.from(knownFilesSet);
|
|
25
|
+
return { ...setup, knownFiles };
|
|
26
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "monoidentity",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist",
|
|
6
|
+
"!dist/**/*.test.*",
|
|
7
|
+
"!dist/**/*.spec.*"
|
|
8
|
+
],
|
|
9
|
+
"sideEffects": [
|
|
10
|
+
"**/*.css"
|
|
11
|
+
],
|
|
12
|
+
"svelte": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"svelte": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"svelte": "^5.0.0"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"devalue": "^5.3.2"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@sveltejs/adapter-static": "^3.0.9",
|
|
29
|
+
"@sveltejs/kit": "^2.22.0",
|
|
30
|
+
"@sveltejs/package": "^2.0.0",
|
|
31
|
+
"@sveltejs/vite-plugin-svelte": "^6.0.0",
|
|
32
|
+
"publint": "^0.3.2",
|
|
33
|
+
"svelte": "^5.0.0",
|
|
34
|
+
"svelte-check": "^4.0.0",
|
|
35
|
+
"typescript": "^5.0.0",
|
|
36
|
+
"vite": "^7.0.4"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"svelte"
|
|
40
|
+
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"dev": "vite dev",
|
|
43
|
+
"build": "vite build && pnpm run prepack",
|
|
44
|
+
"preview": "vite preview",
|
|
45
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
46
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
|
|
47
|
+
}
|
|
48
|
+
}
|