@crosslink/sdk-browser 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,163 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by the
13
+ copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other
16
+ entities that control, are controlled by, or are under common control
17
+ with that entity. For the purposes of this definition, "control" means
18
+ (i) the power, direct or indirect, to cause the direction or management
19
+ of such entity, whether by contract or otherwise, or (ii) ownership of
20
+ fifty percent (50%) or more of the outstanding shares, or (iii) beneficial
21
+ ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
24
+ permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation source,
28
+ and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical transformation
31
+ or translation of a Source form, including but not limited to compiled
32
+ object code, generated documentation, and conversions to other media types.
33
+
34
+ "Work" shall mean the work of authorship, whether in Source or Object
35
+ form, made available under the License, as indicated by a copyright notice
36
+ that is included in or attached to the work.
37
+
38
+ "Derivative Works" shall mean any work, whether in Source or Object form,
39
+ that is based on (or derived from) the Work and for which the editorial
40
+ revisions, annotations, elaborations, or other modifications represent, as
41
+ a whole, an original work of authorship. For the purposes of this License,
42
+ Derivative Works shall not include works that remain separable from, or
43
+ merely link (or bind by name) to the interfaces of, the Work and Derivative
44
+ Works thereof.
45
+
46
+ "Contribution" shall mean any work of authorship, including the original
47
+ version of the Work and any modifications or additions to that Work or
48
+ Derivative Works thereof, that is intentionally submitted to the Licensor
49
+ for inclusion in the Work by the copyright owner or by an individual or
50
+ Legal Entity authorized to submit on behalf of the copyright owner. For the
51
+ purposes of this definition, "submitted" means any form of electronic,
52
+ verbal, or written communication sent to the Licensor or its representatives,
53
+ including but not limited to communication on electronic mailing lists,
54
+ source code control systems, and issue tracking systems that are managed by,
55
+ or on behalf of, the Licensor for the purpose of discussing and improving
56
+ the Work, but excluding communication that is conspicuously marked or
57
+ otherwise designated in writing by the copyright owner as "Not a Contribution."
58
+
59
+ "Contributor" shall mean Licensor and any individual or Legal Entity on
60
+ behalf of whom a Contribution has been received by the Licensor and
61
+ subsequently incorporated within the Work.
62
+
63
+ 2. Grant of Copyright License. Subject to the terms and conditions of this
64
+ License, each Contributor hereby grants to You a perpetual, worldwide,
65
+ non-exclusive, no-charge, royalty-free, irrevocable copyright license to
66
+ reproduce, prepare Derivative Works of, publicly display, publicly perform,
67
+ sublicense, and distribute the Work and such Derivative Works in Source or
68
+ Object form.
69
+
70
+ 3. Grant of Patent License. Subject to the terms and conditions of this
71
+ License, each Contributor hereby grants to You a perpetual, worldwide,
72
+ non-exclusive, no-charge, royalty-free, irrevocable (except as stated in
73
+ this section) patent license to make, have made, use, offer to sell, sell,
74
+ import, and otherwise transfer the Work, where such license applies only to
75
+ those patent claims licensable by such Contributor that are necessarily
76
+ infringed by their Contribution(s) alone or by combination of their
77
+ Contribution(s) with the Work to which such Contribution(s) was submitted.
78
+ If You institute patent litigation against any entity (including a
79
+ cross-claim or counterclaim in a lawsuit) alleging that the Work or a
80
+ Contribution incorporated within the Work constitutes direct or contributory
81
+ patent infringement, then any patent licenses granted to You under this
82
+ License for that Work shall terminate as of the date such litigation is filed.
83
+
84
+ 4. Redistribution. You may reproduce and distribute copies of the Work or
85
+ Derivative Works thereof in any medium, with or without modifications, and
86
+ in Source or Object form, provided that You meet the following conditions:
87
+
88
+ (a) You must give any other recipients of the Work or Derivative Works
89
+ a copy of this License; and
90
+
91
+ (b) You must cause any modified files to carry prominent notices stating
92
+ that You changed the files; and
93
+
94
+ (c) You must retain, in the Source form of any Derivative Works that You
95
+ distribute, all copyright, patent, trademark, and attribution notices
96
+ from the Source form of the Work, excluding those notices that do not
97
+ pertain to any part of the Derivative Works; and
98
+
99
+ (d) If the Work includes a "NOTICE" text file as part of its distribution,
100
+ then any Derivative Works that You distribute must include a readable
101
+ copy of the attribution notices contained within such NOTICE file,
102
+ excluding any notices that do not pertain to any part of the Derivative
103
+ Works, in at least one of the following places: within a NOTICE text
104
+ file distributed as part of the Derivative Works; within the Source
105
+ form or documentation, if provided along with the Derivative Works; or,
106
+ within a display generated by the Derivative Works, if and wherever
107
+ such third-party notices normally appear. The contents of the NOTICE
108
+ file are for informational purposes only and do not modify the License.
109
+ You may add Your own attribution notices within Derivative Works that
110
+ You distribute, alongside or as an addendum to the NOTICE text from
111
+ the Work, provided that such additional attribution notices cannot be
112
+ construed as modifying the License.
113
+
114
+ You may add Your own copyright statement to Your modifications and may
115
+ provide additional or different license terms and conditions for use,
116
+ reproduction, or distribution of Your modifications, or for any such
117
+ Derivative Works as a whole, provided Your use, reproduction, and
118
+ distribution of the Work otherwise complies with the conditions stated
119
+ in this License.
120
+
121
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any
122
+ Contribution intentionally submitted for inclusion in the Work by You to
123
+ the Licensor shall be under the terms and conditions of this License,
124
+ without any additional terms or conditions. Notwithstanding the above,
125
+ nothing herein shall supersede or modify the terms of any separate license
126
+ agreement you may have executed with Licensor regarding such Contributions.
127
+
128
+ 6. Trademarks. This License does not grant permission to use the trade names,
129
+ trademarks, service marks, or product names of the Licensor, except as
130
+ required for reasonable and customary use in describing the origin of the
131
+ Work and reproducing the content of the NOTICE file.
132
+
133
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
134
+ writing, Licensor provides the Work (and each Contributor provides its
135
+ Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
136
+ KIND, either express or implied, including, without limitation, any warranties
137
+ or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
138
+ PARTICULAR PURPOSE. You are solely responsible for determining the
139
+ appropriateness of using or redistributing the Work and assume any risks
140
+ associated with Your exercise of permissions under this License.
141
+
142
+ 8. Limitation of Liability. In no event and under no legal theory, whether in
143
+ tort (including negligence), contract, or otherwise, unless required by
144
+ applicable law (such as deliberate and grossly negligent acts) or agreed to
145
+ in writing, shall any Contributor be liable to You for damages, including
146
+ any direct, indirect, special, incidental, or consequential damages of any
147
+ character arising as a result of this License or out of the use or inability
148
+ to use the Work (including but not limited to damages for loss of goodwill,
149
+ work stoppage, computer failure or malfunction, or any and all other
150
+ commercial damages or losses), even if such Contributor has been advised of
151
+ the possibility of such damages.
152
+
153
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work
154
+ or Derivative Works thereof, You may choose to offer, and charge a fee for,
155
+ acceptance of support, warranty, indemnity, or other liability obligations
156
+ and/or rights consistent with this License. However, in accepting such
157
+ obligations, You may act only on Your own behalf and on Your sole
158
+ responsibility, not on behalf of any other Contributor, and only if You
159
+ agree to indemnify, defend, and hold each Contributor harmless for any
160
+ liability incurred by, or claims asserted against, such Contributor by reason
161
+ of your accepting any such warranty or additional liability.
162
+
163
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @crosslink/sdk-browser
2
+
3
+ <p align="center"><img src="https://crosslink.mintlify.site/crosslink.svg" alt="Crosslink" width="320" /></p>
4
+
5
+ The Crosslink SDK for browsers and installable PWAs, including the canonical pairing widget.
6
+
7
+ ```bash
8
+ npm install @crosslink/sdk-browser
9
+ ```
10
+
11
+ See the [Crosslink quickstart](https://crosslink.mintlify.site/quickstart) and [repository](https://github.com/jacobpowaza/crosslink).
@@ -0,0 +1,54 @@
1
+ // src/storage.ts
2
+ var MemorySecureStorage = class {
3
+ map = /* @__PURE__ */ new Map();
4
+ get(key) {
5
+ return this.map.get(key) ?? null;
6
+ }
7
+ set(key, value) {
8
+ this.map.set(key, value);
9
+ }
10
+ delete(key) {
11
+ this.map.delete(key);
12
+ }
13
+ };
14
+ var LocalStorageSecureStorage = class {
15
+ constructor(ls) {
16
+ this.ls = ls;
17
+ }
18
+ ls;
19
+ get(key) {
20
+ return this.ls.getItem(key);
21
+ }
22
+ set(key, value) {
23
+ this.ls.setItem(key, value);
24
+ }
25
+ delete(key) {
26
+ this.ls.removeItem(key);
27
+ }
28
+ };
29
+ var JsonStore = class {
30
+ constructor(storage, key) {
31
+ this.storage = storage;
32
+ this.key = key;
33
+ }
34
+ storage;
35
+ key;
36
+ load(defaults) {
37
+ const raw = this.storage.get(this.key);
38
+ if (!raw) return defaults;
39
+ try {
40
+ return { ...defaults, ...JSON.parse(raw) };
41
+ } catch {
42
+ return defaults;
43
+ }
44
+ }
45
+ save(value) {
46
+ this.storage.set(this.key, JSON.stringify(value));
47
+ }
48
+ };
49
+
50
+ export {
51
+ MemorySecureStorage,
52
+ LocalStorageSecureStorage,
53
+ JsonStore
54
+ };
@@ -0,0 +1,221 @@
1
+ // src/secure-storage.ts
2
+ var DB_NAME = "crosslink-secure";
3
+ var DB_VERSION = 1;
4
+ var KEY_STORE = "keys";
5
+ var VALUE_STORE = "values";
6
+ var MASTER_KEY_ID = "master";
7
+ var IV_BYTES = 12;
8
+ function openDb() {
9
+ return new Promise((resolve, reject) => {
10
+ const request2 = indexedDB.open(DB_NAME, DB_VERSION);
11
+ request2.onupgradeneeded = () => {
12
+ const db = request2.result;
13
+ if (!db.objectStoreNames.contains(KEY_STORE)) db.createObjectStore(KEY_STORE);
14
+ if (!db.objectStoreNames.contains(VALUE_STORE)) db.createObjectStore(VALUE_STORE);
15
+ };
16
+ request2.onsuccess = () => resolve(request2.result);
17
+ request2.onerror = () => reject(request2.error ?? new Error("indexedDB open failed"));
18
+ request2.onblocked = () => reject(new Error("indexedDB open blocked by another tab"));
19
+ });
20
+ }
21
+ function txDone(tx) {
22
+ return new Promise((resolve, reject) => {
23
+ tx.oncomplete = () => resolve();
24
+ tx.onerror = () => reject(tx.error ?? new Error("indexedDB transaction failed"));
25
+ tx.onabort = () => reject(tx.error ?? new Error("indexedDB transaction aborted"));
26
+ });
27
+ }
28
+ function request(req) {
29
+ return new Promise((resolve, reject) => {
30
+ req.onsuccess = () => resolve(req.result);
31
+ req.onerror = () => reject(req.error ?? new Error("indexedDB request failed"));
32
+ });
33
+ }
34
+ var IndexedDbSecureStorage = class _IndexedDbSecureStorage {
35
+ constructor(db, key) {
36
+ this.db = db;
37
+ this.key = key;
38
+ }
39
+ db;
40
+ key;
41
+ kind = "indexeddb-aes-gcm";
42
+ encrypted = true;
43
+ /**
44
+ * Opens the store, generating the non-extractable master key on first use.
45
+ * Rejects when IndexedDB or WebCrypto is unavailable (private-mode Safari,
46
+ * insecure origins) so the caller can choose its own fallback rather than
47
+ * being silently downgraded to plaintext.
48
+ */
49
+ static async open() {
50
+ if (typeof indexedDB === "undefined") {
51
+ throw new Error("IndexedDB is not available in this environment");
52
+ }
53
+ const subtle = globalThis.crypto?.subtle;
54
+ if (!subtle) {
55
+ throw new Error("WebCrypto subtle is not available (requires a secure origin)");
56
+ }
57
+ const db = await openDb();
58
+ let key = await request(
59
+ db.transaction(KEY_STORE, "readonly").objectStore(KEY_STORE).get(MASTER_KEY_ID)
60
+ );
61
+ if (!key) {
62
+ key = await subtle.generateKey({ name: "AES-GCM", length: 256 }, false, [
63
+ "encrypt",
64
+ "decrypt"
65
+ ]);
66
+ const tx = db.transaction(KEY_STORE, "readwrite");
67
+ tx.objectStore(KEY_STORE).put(key, MASTER_KEY_ID);
68
+ await txDone(tx);
69
+ }
70
+ return new _IndexedDbSecureStorage(db, key);
71
+ }
72
+ async get(name) {
73
+ const stored = await request(
74
+ this.db.transaction(VALUE_STORE, "readonly").objectStore(VALUE_STORE).get(name)
75
+ );
76
+ if (!stored) return null;
77
+ try {
78
+ const plain = await crypto.subtle.decrypt(
79
+ { name: "AES-GCM", iv: new Uint8Array(stored.iv) },
80
+ this.key,
81
+ stored.data
82
+ );
83
+ return new TextDecoder().decode(plain);
84
+ } catch {
85
+ return null;
86
+ }
87
+ }
88
+ async set(name, value) {
89
+ const iv = crypto.getRandomValues(new Uint8Array(IV_BYTES));
90
+ const data = await crypto.subtle.encrypt(
91
+ { name: "AES-GCM", iv },
92
+ this.key,
93
+ new TextEncoder().encode(value)
94
+ );
95
+ const tx = this.db.transaction(VALUE_STORE, "readwrite");
96
+ tx.objectStore(VALUE_STORE).put({ iv: iv.buffer, data }, name);
97
+ await txDone(tx);
98
+ }
99
+ async delete(name) {
100
+ const tx = this.db.transaction(VALUE_STORE, "readwrite");
101
+ tx.objectStore(VALUE_STORE).delete(name);
102
+ await txDone(tx);
103
+ }
104
+ async keys() {
105
+ const all = await request(
106
+ this.db.transaction(VALUE_STORE, "readonly").objectStore(VALUE_STORE).getAllKeys()
107
+ );
108
+ return all.map(String);
109
+ }
110
+ /** Destroys every stored value and the master key. */
111
+ async wipe() {
112
+ const tx = this.db.transaction([VALUE_STORE, KEY_STORE], "readwrite");
113
+ tx.objectStore(VALUE_STORE).clear();
114
+ tx.objectStore(KEY_STORE).clear();
115
+ await txDone(tx);
116
+ }
117
+ };
118
+ var AsyncStorageAdapter = class {
119
+ constructor(inner, kind = "sync-adapter", knownKeys = /* @__PURE__ */ new Set()) {
120
+ this.inner = inner;
121
+ this.knownKeys = knownKeys;
122
+ this.kind = kind;
123
+ }
124
+ inner;
125
+ knownKeys;
126
+ kind;
127
+ encrypted = false;
128
+ async get(key) {
129
+ return this.inner.get(key);
130
+ }
131
+ async set(key, value) {
132
+ this.knownKeys.add(key);
133
+ this.inner.set(key, value);
134
+ }
135
+ async delete(key) {
136
+ this.knownKeys.delete(key);
137
+ this.inner.delete(key);
138
+ }
139
+ async keys() {
140
+ return [...this.knownKeys];
141
+ }
142
+ };
143
+ var HydratedSecureStorage = class _HydratedSecureStorage {
144
+ constructor(backend, onWriteError) {
145
+ this.backend = backend;
146
+ this.onWriteError = onWriteError;
147
+ }
148
+ backend;
149
+ onWriteError;
150
+ cache = /* @__PURE__ */ new Map();
151
+ /** serializes writes so a rapid set/delete pair cannot land out of order */
152
+ flushChain = Promise.resolve();
153
+ pendingWrites = 0;
154
+ static async hydrate(backend, options = {}) {
155
+ const storage = new _HydratedSecureStorage(backend, options.onWriteError);
156
+ for (const key of await backend.keys()) {
157
+ const value = await backend.get(key);
158
+ if (value !== null) storage.cache.set(key, value);
159
+ }
160
+ return storage;
161
+ }
162
+ get kind() {
163
+ return this.backend.kind;
164
+ }
165
+ get encrypted() {
166
+ return this.backend.encrypted;
167
+ }
168
+ get(key) {
169
+ return this.cache.get(key) ?? null;
170
+ }
171
+ set(key, value) {
172
+ this.cache.set(key, value);
173
+ this.enqueue(key, () => this.backend.set(key, value));
174
+ }
175
+ delete(key) {
176
+ this.cache.delete(key);
177
+ this.enqueue(key, () => this.backend.delete(key));
178
+ }
179
+ /** Resolves once every queued write has reached the backend. */
180
+ flushed() {
181
+ return this.flushChain;
182
+ }
183
+ get pending() {
184
+ return this.pendingWrites;
185
+ }
186
+ enqueue(key, op) {
187
+ this.pendingWrites += 1;
188
+ this.flushChain = this.flushChain.then(op).catch((err) => {
189
+ this.onWriteError?.(err, key);
190
+ }).finally(() => {
191
+ this.pendingWrites -= 1;
192
+ });
193
+ }
194
+ };
195
+ async function createSecureStorage(options = {}) {
196
+ try {
197
+ const backend = await IndexedDbSecureStorage.open();
198
+ const storage = await HydratedSecureStorage.hydrate(backend, {
199
+ ...options.onWriteError ? { onWriteError: options.onWriteError } : {}
200
+ });
201
+ return { storage, kind: backend.kind, encrypted: true };
202
+ } catch (err) {
203
+ if (options.allowPlaintextFallback === false) throw err;
204
+ }
205
+ const { LocalStorageSecureStorage, MemorySecureStorage } = await import("./storage-FHFZA2HW.js");
206
+ if (typeof localStorage !== "undefined") {
207
+ return {
208
+ storage: new LocalStorageSecureStorage(localStorage),
209
+ kind: "localstorage",
210
+ encrypted: false
211
+ };
212
+ }
213
+ return { storage: new MemorySecureStorage(), kind: "memory", encrypted: false };
214
+ }
215
+
216
+ export {
217
+ IndexedDbSecureStorage,
218
+ AsyncStorageAdapter,
219
+ HydratedSecureStorage,
220
+ createSecureStorage
221
+ };