p2party 0.7.8 → 0.7.9
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/lib/cryptography/utils.d.ts +2 -1
- package/lib/db.worker.js +1 -1
- package/lib/handlers/handleOpenChannel.d.ts +0 -4
- package/lib/index.js +1 -1
- package/lib/index.min.js +1 -1
- package/lib/index.mjs +1 -1
- package/lib/utils/constants.d.ts +4 -1
- package/lib/utils/uint8array.d.ts +0 -4
- package/package.json +5 -5
- package/lib/handlers/handleReceiveMessageOld.d.ts +0 -16
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export declare const randomNumberInRange: (min: number, max: number) => Promise<number>;
|
|
2
2
|
export declare const randomBytesToString: (len: number, withAlphabetSmall?: boolean, withAlphabetCapital?: boolean, withNumbers?: boolean) => Promise<string>;
|
|
3
3
|
export declare const generateRandomRoomUrl: (lenMin: number, lenMax?: number, withAlphabetSmall?: boolean, withAlphabetCapital?: boolean, withNumbers?: boolean) => Promise<string>;
|
|
4
|
+
export declare function uniformInt(min: number, max: number): number;
|
|
5
|
+
export declare function fisherYatesShuffle<T>(arr: T[]): T[];
|
|
4
6
|
/**
|
|
5
7
|
* @function
|
|
6
8
|
* Fisher-Yates shuffle of array.
|
|
@@ -9,5 +11,4 @@ export declare const generateRandomRoomUrl: (lenMin: number, lenMax?: number, wi
|
|
|
9
11
|
*
|
|
10
12
|
* @returns Promise<T[]>
|
|
11
13
|
*/
|
|
12
|
-
export declare const fisherYatesShuffle: <T>(array: T[]) => Promise<T[]>;
|
|
13
14
|
//# sourceMappingURL=utils.d.ts.map
|
package/lib/db.worker.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=(e,t)=>t.some(t=>e instanceof t);let t,a;const n=new WeakMap,o=new WeakMap,r=new WeakMap;let s={get(e,t,a){if(e instanceof IDBTransaction){if("done"===t)return n.get(e);if("store"===t)return a.objectStoreNames[1]?void 0:a.objectStore(a.objectStoreNames[0])}return d(e[t])},set:(e,t,a)=>(e[t]=a,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function i(e){s=e(s)}function c(e){return(a||(a=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(l(this),t),d(this.request)}:function(...t){return d(e.apply(l(this),t))}}function u(a){return"function"==typeof a?c(a):(a instanceof IDBTransaction&&function(e){if(n.has(e))return;const t=new Promise((t,a)=>{const n=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",r),e.removeEventListener("abort",r)},o=()=>{t(),n()},r=()=>{a(e.error||new DOMException("AbortError","AbortError")),n()};e.addEventListener("complete",o),e.addEventListener("error",r),e.addEventListener("abort",r)});n.set(e,t)}(a),e(a,t||(t=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(a,s):a)}function d(e){if(e instanceof IDBRequest)return function(e){const t=new Promise((t,a)=>{const n=()=>{e.removeEventListener("success",o),e.removeEventListener("error",r)},o=()=>{t(d(e.result)),n()},r=()=>{a(e.error),n()};e.addEventListener("success",o),e.addEventListener("error",r)});return r.set(t,e),t}(e);if(o.has(e))return o.get(e);const t=u(e);return t!==e&&(o.set(e,t),r.set(t,e)),t}const l=e=>r.get(e);const w=["get","getKey","getAll","getAllKeys","count"],h=["put","add","delete","clear"],m=new Map;function g(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(m.get(t))return m.get(t);const a=t.replace(/FromIndex$/,""),n=t!==a,o=h.includes(a);if(!(a in(n?IDBIndex:IDBObjectStore).prototype)||!o&&!w.includes(a))return;const r=async function(e,...t){const r=this.transaction(e,o?"readwrite":"readonly");let s=r.store;return n&&(s=s.index(t.shift())),(await Promise.all([s[a](...t),o&&r.done]))[0]};return m.set(t,r),r}i(e=>({...e,get:(t,a,n)=>g(t,a)||e.get(t,a,n),has:(t,a)=>!!g(t,a)||e.has(t,a)}));const k=["continue","continuePrimaryKey","advance"],y={},b=new WeakMap,f=new WeakMap,I={get(e,t){if(!k.includes(t))return e[t];let a=y[t];return a||(a=y[t]=function(...e){b.set(this,f.get(this)[t](...e))}),a}};async function*x(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;const a=new Proxy(t,I);for(f.set(a,t),r.set(a,l(t));t;)yield a,t=await(b.get(a)||t.continue()),b.delete(a)}function p(t,a){return a===Symbol.asyncIterator&&e(t,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===a&&e(t,[IDBIndex,IDBObjectStore])}i(e=>({...e,get:(t,a,n)=>p(t,a)?x:e.get(t,a,n),has:(t,a)=>p(t,a)||e.has(t,a)}));const D=64*Uint8Array.BYTES_PER_ELEMENT,B="p2party";async function S(){return function(e,t,{blocked:a,upgrade:n,blocking:o,terminated:r}={}){const s=indexedDB.open(e,t),i=d(s);return n&&s.addEventListener("upgradeneeded",e=>{n(d(s.result),e.oldVersion,e.newVersion,d(s.transaction),e)}),a&&s.addEventListener("blocked",e=>a(e.oldVersion,e.newVersion,e)),i.then(e=>{r&&e.addEventListener("close",()=>r()),o&&e.addEventListener("versionchange",e=>o(e.oldVersion,e.newVersion,e))}).catch(()=>{}),i}(B,16,{upgrade(e,t,a,n){if(e.objectStoreNames.contains("addressBook")){const e=n.objectStore("addressBook");e.indexNames.contains("username")||e.createIndex("username","username",{unique:!1}),e.indexNames.contains("peerId")||e.createIndex("peerId","peerId",{unique:!0}),e.indexNames.contains("peerPublicKey")||e.createIndex("peerPublicKey","peerPublicKey",{unique:!0})}else{const t=e.createObjectStore("addressBook",{keyPath:["peerId"]});t.createIndex("username","username",{unique:!1}),t.createIndex("peerId","peerId",{unique:!0}),t.createIndex("peerPublicKey","peerPublicKey",{unique:!0})}if(e.objectStoreNames.contains("blacklist")){const e=n.objectStore("blacklist");e.indexNames.contains("username")||e.createIndex("username","username",{unique:!1}),e.indexNames.contains("peerId")||e.createIndex("peerId","peerId",{unique:!0}),e.indexNames.contains("peerPublicKey")||e.createIndex("peerPublicKey","peerPublicKey",{unique:!0})}else{const t=e.createObjectStore("blacklist",{keyPath:["peerId"]});t.createIndex("username","username",{unique:!1}),t.createIndex("peerId","peerId",{unique:!0}),t.createIndex("peerPublicKey","peerPublicKey",{unique:!0})}if(e.objectStoreNames.contains("uniqueRoom")){const e=n.objectStore("uniqueRoom");e.indexNames.contains("roomUrl")||e.createIndex("roomUrl","roomUrl",{unique:!0}),e.indexNames.contains("roomId")||e.createIndex("roomId","roomId",{unique:!0})}else{const t=e.createObjectStore("uniqueRoom",{keyPath:["roomId"]});t.createIndex("roomUrl","roomUrl",{unique:!0}),t.createIndex("roomId","roomId",{unique:!0})}if(e.objectStoreNames.contains("messageData")){const e=n.objectStore("messageData");e.indexNames.contains("roomId")||e.createIndex("roomId","roomId",{unique:!1}),e.indexNames.contains("hash")||e.createIndex("hash","hash",{unique:!1}),e.indexNames.contains("merkleRoot")||e.createIndex("merkleRoot","merkleRoot",{unique:!0}),e.indexNames.contains("fromPeerId")||e.createIndex("fromPeerId","fromPeerId",{unique:!1})}else{const t=e.createObjectStore("messageData",{keyPath:["timestamp","roomId","merkleRoot"]});t.createIndex("roomId","roomId",{unique:!1}),t.createIndex("hash","hash",{unique:!1}),t.createIndex("merkleRoot","merkleRoot",{unique:!0}),t.createIndex("fromPeerId","fromPeerId",{unique:!1})}if(e.objectStoreNames.contains("chunks")){const e=n.objectStore("chunks");e.indexNames.contains("merkleRoot")||e.createIndex("merkleRoot","merkleRoot",{unique:!1}),e.indexNames.contains("hash")||e.createIndex("hash","hash",{unique:!1})}else{const t=e.createObjectStore("chunks",{keyPath:["merkleRoot","chunkIndex"]});t.createIndex("merkleRoot","merkleRoot",{unique:!1}),t.createIndex("hash","hash",{unique:!1})}if(e.objectStoreNames.contains("newChunks")){const e=n.objectStore("newChunks");e.indexNames.contains("hash")||e.createIndex("hash","hash",{unique:!1}),e.indexNames.contains("merkleRoot")||e.createIndex("merkleRoot","merkleRoot",{unique:!1}),e.indexNames.contains("realChunkHash")||e.createIndex("realChunkHash","realChunkHash",{unique:!0})}else{const t=e.createObjectStore("newChunks",{keyPath:["hash","chunkIndex"]});t.createIndex("hash","hash",{unique:!1}),t.createIndex("merkleRoot","merkleRoot",{unique:!1}),t.createIndex("realChunkHash","realChunkHash",{unique:!0})}if(e.objectStoreNames.contains("sendQueue")){const e=n.objectStore("sendQueue");e.indexNames.contains("labelPeer")||e.createIndex("labelPeer",["label","toPeerId"],{unique:!1})}else{e.createObjectStore("sendQueue",{keyPath:["position","label","toPeerId"]}).createIndex("labelPeer",["label","toPeerId"],{unique:!1})}}})}async function R(){await function(e,{blocked:t}={}){const a=indexedDB.deleteDatabase(e);return t&&a.addEventListener("blocked",e=>t(e.oldVersion,e)),d(a).then(()=>{})}(B,{blocked(){console.error("DB deletion BLOCKED")}})}onmessage=async e=>{const t=e.data,{id:a,method:n}=t;try{let e;switch(n){case"getDBAddressBookEntry":e=await async function(e,t){if(!e&&!t)return;if(e&&e.length<10&&!t)return;if(t&&64!==t.length&&!e)return;const a=await S();try{const n=a.transaction("addressBook","readonly"),o=e?n.objectStore("addressBook").index("peerId"):n.objectStore("addressBook").index("peerPublicKey"),r=e?await o.get(e):await o.get(t);return await n.done,a.close(),r?{username:r.username,peerId:r.peerId,peerPublicKey:r.peerPublicKey}:void 0}catch(e){return void a.close()}}(...t.args);break;case"getAllDBAddressBookEntries":e=await async function(){try{const e=await S(),t=await e.getAll("addressBook");return e.close(),t}catch(e){return[]}}(...t.args);break;case"setDBAddressBookEntry":e=await async function(e,t,a){try{const n=await S(),o=n.transaction(["addressBook"],"readwrite"),r=o.objectStore("addressBook"),s=r.index("peerId"),i=r.index("peerPublicKey"),c=await s.get(t),u=await i.get(a);(!c&&!u||c&&!u||!c&&u)&&await r.put({username:e,peerId:t,peerPublicKey:a,dateAdded:Date.now()}),await o.done,n.close()}catch(e){console.error(e)}}(...t.args);break;case"deleteDBAddressBookEntry":e=await async function(e,t,a){const n=!e||0===e.length,o=!t||t.length<10,r=!a||64!==a.length;if(n&&o&&r)throw new Error("Cannot delete address book with no data");let s=t??"";try{const n=await S(),i=n.transaction("addressBook","readwrite"),c=i.objectStore("addressBook");if(o)if(r){const t=c.index("username"),a=await t.getKey(e);if(a){const n=await t.get(e);s=n?.peerId??"",await c.delete(a)}}else{const e=c.index("peerPublicKey"),t=await e.getKey(a);if(t){const n=await e.get(a);s=n?.peerId??"",await c.delete(t)}}else{const e=c.index("peerId"),a=await e.getKey(t);a&&await c.delete(a)}await i.done,n.close()}catch{}return s}(...t.args);break;case"getDBPeerIsBlacklisted":e=await async function(e,t){if(!e&&!t)return!1;if(e&&e.length<10&&!t)return!1;if(t&&64!==t.length&&!e)return!1;try{const a=await S(),n=a.transaction("blacklist","readonly"),o=e?n.objectStore("blacklist").index("peerId"):n.objectStore("blacklist").index("peerPublicKey"),r=e?await o.get(e):await o.get(t);return await n.done,a.close(),!!r}catch(e){return!1}}(...t.args);break;case"getAllDBBlacklisted":e=await async function(){const e=await S();try{const t=await e.getAll("blacklist");return e.close(),t}catch(t){return e.close(),[]}}(...t.args);break;case"setDBPeerInBlacklist":e=await async function(e,t){try{const a=await S(),n=a.transaction(["blacklist"],"readwrite"),o=n.objectStore("blacklist"),r=o.index("peerId"),s=o.index("peerPublicKey"),i=await r.get(e),c=await s.get(t);(!i&&!c||i&&!c||!i&&c)&&await o.put({peerId:e,peerPublicKey:t,dateAdded:Date.now()}),await n.done,a.close()}catch(e){console.error(e)}}(...t.args);break;case"deleteDBPeerFromBlacklist":e=await async function(e,t){const a=!e||e.length<10,n=!t||64!==t.length;if(a&&n)throw new Error("Cannot delete blacklisted with no data");try{const n=await S(),o=n.transaction("blacklist","readwrite"),r=o.objectStore("blacklist");if(a){const e=r.index("peerPublicKey"),a=await e.getKey(t);a&&await r.delete(a)}else{const t=r.index("peerId"),a=await t.getKey(e);a&&await r.delete(a)}await o.done,n.close()}catch(e){console.error(e)}}(...t.args);break;case"getAllDBUniqueRooms":e=await async function(){const e=await S();try{const t=await e.getAll("uniqueRoom");return e.close(),t}catch(t){return e.close(),[]}}(...t.args);break;case"setDBUniqueRoom":e=await async function(e,t){try{const a=await S(),n=a.transaction(["uniqueRoom"],"readwrite"),o=n.objectStore("uniqueRoom"),r=o.index("roomUrl"),s=o.index("roomId"),i=await r.get(e),c=await s.get(t);if(!i&&!c){const a=Date.now();await o.put({roomId:t,roomUrl:e,messageCount:0,lastMessageMerkleRoot:"",createdAt:a,updatedAt:a})}await n.done,a.close()}catch(e){console.error(e)}}(...t.args);break;case"getDBMessageData":e=await async function(e,t){try{const a=await S(),n=a.transaction(["messageData"],"readonly"),o=n.objectStore("messageData"),r=o.index("merkleRoot"),s=o.index("hash");if(e&&e.length===2*D){const o=await r.get(e);if(o)return await n.done,a.close(),o;if(t&&t.length===2*D){const e=await s.get(t);return await n.done,a.close(),e}return await n.done,void a.close()}if(t&&t.length===2*D){const e=await s.get(t);return await n.done,a.close(),e}return await n.done,void a.close()}catch(e){return void console.error(e)}}(...t.args);break;case"getDBRoomMessageData":e=await async function(e){const t=await S(),a=await t.getAllFromIndex("messageData","roomId",e);t.close();const n=a.length,o=[];for(let t=0;t<n;t++)o.push({roomId:e,merkleRoot:a[t].merkleRoot,hash:a[t].hash,fromPeerId:a[t].fromPeerId,filename:a[t].filename,messageType:a[t].messageType,savedSize:a[t].savedSize,totalSize:a[t].totalSize,channelLabel:a[t].channelLabel,timestamp:a[t].timestamp});return o}(...t.args);break;case"setDBRoomMessageData":e=await async function(e,t,a,n,o,r,s,i,c,u){try{const d=await S(),l=d.transaction(["messageData","uniqueRoom"],"readwrite"),w=l.objectStore("messageData"),h=await w.index("merkleRoot").get(t),m=h?.savedSize??0;try{await w.put({roomId:e,timestamp:u,merkleRoot:t,hash:a,fromPeerId:n,filename:i,messageType:s,savedSize:m<r?m+o:r,totalSize:r,channelLabel:c})}catch(e){throw await l.done,d.close(),e}const g=l.objectStore("uniqueRoom"),k=await g.index("roomId").get(e);if(k&&k.lastMessageMerkleRoot!==t)try{await g.put({...k,lastMessageMerkleRoot:t,messageCount:k.messageCount+1,updatedAt:u})}catch(e){throw await l.done,d.close(),e}await l.done,d.close()}catch(e){throw console.error(e),e}}(...t.args);break;case"getDBChunk":e=await async function(e,t){const a=await S(),n=await a.get("chunks",[e,t]);return a.close(),n?.data}(...t.args);break;case"existsDBChunk":e=await async function(e,t){const a=await S(),n=await a.count("chunks",[e,t]);return a.close(),n>0}(...t.args);break;case"getDBNewChunk":e=await async function(e,t){try{const a=await S(),n=t??-1;if(n>-1){const t=await a.get("newChunks",[e,n]);return a.close(),t}{const t=a.transaction("newChunks"),n=t.objectStore("newChunks").index("realChunkHash"),o=await n.get(e);return await t.done,a.close(),o}}catch(e){return void console.error(e)}}(...t.args);break;case"existsDBNewChunk":e=await async function(e,t){const a=await S(),n=await a.count("newChunks",[e,t]);return a.close(),n>0}(...t.args);break;case"getDBSendQueue":e=await async function(e,t,a){const n=await S();if(a){const o=await n.get("sendQueue",[a,e,t]);return n.close(),o?[o]:[]}const o=n.transaction("sendQueue","readonly").objectStore("sendQueue").index("labelPeer"),r=IDBKeyRange.only([e,t]),s=await o.getAll(r);return n.close(),s}(...t.args);break;case"getDBAllChunks":e=await async function(e,t){try{const a=await S(),n=a.transaction("chunks","readonly"),o=n.objectStore("chunks"),r=o.index("hash"),s=o.index("merkleRoot");if(e&&e.length===2*D){const t=await s.getAll(e);return 0===t.length?(await n.done,a.close(),[]):(await n.done,a.close(),t)}if(t&&t.length===2*D){const e=await r.getAll(t);return await n.done,a.close(),e.length>0?e.filter(t=>t.merkleRoot===e[0].merkleRoot):e}return await n.done,a.close(),[]}catch(e){return console.error(e),[]}}(...t.args);break;case"getDBAllChunksCount":e=await async function(e,t){try{const a=await S(),n=a.transaction(["chunks"],"readonly"),o=n.objectStore("chunks"),r=o.index("hash"),s=o.index("merkleRoot");if(t&&t.length===2*D){const o=await r.count(t);if(0===o){if(e&&e.length===2*D){const t=await s.count(e);return await n.done,a.close(),t}return await n.done,a.close(),0}return await n.done,a.close(),o}if(e&&e.length===2*D){const t=await s.count(e);return await n.done,a.close(),t}return await n.done,a.close(),0}catch(e){return 0}}(...t.args);break;case"setDBChunk":e=await async function(e){const t=await S();try{await t.add("chunks",e)}catch(e){}t.close()}(...t.args);break;case"getDBAllNewChunks":e=await async function(e,t){if(!e&&!t)return[];if(e?.length!==2*D&&t?.length!==2*D)return[];const a=await S();if((e?await a.countFromIndex("newChunks","hash",e):await a.countFromIndex("newChunks","merkleRoot",t))>0){const n=e?await a.getAllFromIndex("newChunks","hash",e):await a.getAllFromIndex("newChunks","merkleRoot",t);return a.close(),n}{const n=a.transaction("newChunks","readonly").objectStore("newChunks"),o=e?n.index("hash"):n.index("merkleRoot"),r=e?IDBKeyRange.only(e):IDBKeyRange.only(t),s=await o.getAll(r);return a.close(),s}}(...t.args);break;case"getDBAllNewChunksCount":e=await async function(e){const t=await S(),a=await t.countFromIndex("newChunks","hash",e);return t.close(),a}(...t.args);break;case"setDBNewChunk":e=await async function(e){const t=await S();await t.put("newChunks",e),t.close()}(...t.args);break;case"setDBSendQueue":e=await async function(e){const t=await S();await t.put("sendQueue",e),t.close()}(...t.args);break;case"countDBSendQueue":e=await async function(e,t){const a=await S(),n=a.transaction("sendQueue","readonly").objectStore("sendQueue").index("labelPeer"),o=IDBKeyRange.only([e,t]),r=await n.count(o);return a.close(),r}(...t.args);break;case"deleteDBChunk":e=await async function(e,t){try{const a=await S(),n=a.transaction("chunks","readwrite"),o=n.objectStore("chunks");if(t){const a=IDBKeyRange.only([e,t]);await o.delete(a)}else{const t=o.index("hash"),a=await t.getAllKeys(e),n=a.length;if(0===n){const t=o.index("merkleRoot"),a=await t.getAllKeys(e),n=a.length;for(let e=0;e<n;e++)await o.delete(a[e])}else for(let e=0;e<n;e++)await o.delete(a[e])}await n.done,a.close()}catch(e){}}(...t.args);break;case"deleteDBNewChunk":e=await async function(e,t,a,n){try{const o=await S(),r=o.transaction("newChunks","readwrite"),s=r.objectStore("newChunks");if(e){const t=s.index("merkleRoot"),a=await t.getAllKeys(e),n=a.length;for(let e=0;e<n;e++)await s.delete(a[e])}else if(a&&n)await s.delete([a,n]);else if(a){const e=s.index("hash"),t=await e.getAllKeys(a),n=t.length;for(let e=0;e<n;e++)await s.delete(t[e])}else if(t){const e=s.index("realChunkHash"),a=await e.getKey(t);a&&await s.delete(a)}await r.done,o.close()}catch(e){console.error(e)}}(...t.args);break;case"deleteDBMessageData":e=await async function(e){try{const t=await S(),a=t.transaction(["messageData","uniqueRoom"],"readwrite"),n=a.objectStore("messageData"),o=a.objectStore("uniqueRoom"),r=n.index("merkleRoot"),s=await r.getAllKeys(e),i=s.length;let c="";for(let e=0;e<i;e++){if(0===e||0===c.length){const t=await n.get(s[e]);c=t?.roomId??""}await n.delete(s[e])}if(c.length>0){const e=o.index("roomId"),t=await e.get(c);if(t){const e=n.index("roomId"),a=await e.getAllKeys(c),r=a.length,s=r>0?await n.get(a[r-1]):void 0;await o.put({...t,messageCount:t.messageCount>0?t.messageCount-1:0,updatedAt:s?.timestamp??Date.now(),lastMessageMerkleRoot:s?.merkleRoot??""})}}await a.done,t.close()}catch(e){}}(...t.args);break;case"deleteDBUniqueRoom":e=await async function(e){try{const t=await S(),a=t.transaction("uniqueRoom","readwrite"),n=a.objectStore("uniqueRoom"),o=n.index("roomId"),r=await o.getKey(e);r&&await n.delete(r),await a.done,t.close()}catch(e){}}(...t.args);break;case"deleteDBSendQueue":e=await async function(e,t,a){try{const n=await S();if(a)await n.delete("sendQueue",[a,e,t]);else{const a=IDBKeyRange.only([e,t]);await n.delete("sendQueue",a)}n.close()}catch(e){}}(...t.args);break;case"deleteDB":e=await R();break;default:return void postMessage({id:a,error:"Method not found"})}postMessage({id:a,result:e})}catch(e){postMessage({id:a,error:String(e)})}};
|
|
1
|
+
const e=(e,t)=>t.some(t=>e instanceof t);let t,a;const n=new WeakMap,o=new WeakMap,r=new WeakMap;let s={get(e,t,a){if(e instanceof IDBTransaction){if("done"===t)return n.get(e);if("store"===t)return a.objectStoreNames[1]?void 0:a.objectStore(a.objectStoreNames[0])}return d(e[t])},set:(e,t,a)=>(e[t]=a,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function i(e){s=e(s)}function c(e){return(a||(a=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(l(this),t),d(this.request)}:function(...t){return d(e.apply(l(this),t))}}function u(a){return"function"==typeof a?c(a):(a instanceof IDBTransaction&&function(e){if(n.has(e))return;const t=new Promise((t,a)=>{const n=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",r),e.removeEventListener("abort",r)},o=()=>{t(),n()},r=()=>{a(e.error||new DOMException("AbortError","AbortError")),n()};e.addEventListener("complete",o),e.addEventListener("error",r),e.addEventListener("abort",r)});n.set(e,t)}(a),e(a,t||(t=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(a,s):a)}function d(e){if(e instanceof IDBRequest)return function(e){const t=new Promise((t,a)=>{const n=()=>{e.removeEventListener("success",o),e.removeEventListener("error",r)},o=()=>{t(d(e.result)),n()},r=()=>{a(e.error),n()};e.addEventListener("success",o),e.addEventListener("error",r)});return r.set(t,e),t}(e);if(o.has(e))return o.get(e);const t=u(e);return t!==e&&(o.set(e,t),r.set(t,e)),t}const l=e=>r.get(e);const w=["get","getKey","getAll","getAllKeys","count"],h=["put","add","delete","clear"],m=new Map;function g(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(m.get(t))return m.get(t);const a=t.replace(/FromIndex$/,""),n=t!==a,o=h.includes(a);if(!(a in(n?IDBIndex:IDBObjectStore).prototype)||!o&&!w.includes(a))return;const r=async function(e,...t){const r=this.transaction(e,o?"readwrite":"readonly");let s=r.store;return n&&(s=s.index(t.shift())),(await Promise.all([s[a](...t),o&&r.done]))[0]};return m.set(t,r),r}i(e=>({...e,get:(t,a,n)=>g(t,a)||e.get(t,a,n),has:(t,a)=>!!g(t,a)||e.has(t,a)}));const k=["continue","continuePrimaryKey","advance"],y={},b=new WeakMap,f=new WeakMap,I={get(e,t){if(!k.includes(t))return e[t];let a=y[t];return a||(a=y[t]=function(...e){b.set(this,f.get(this)[t](...e))}),a}};async function*x(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;const a=new Proxy(t,I);for(f.set(a,t),r.set(a,l(t));t;)yield a,t=await(b.get(a)||t.continue()),b.delete(a)}function p(t,a){return a===Symbol.asyncIterator&&e(t,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===a&&e(t,[IDBIndex,IDBObjectStore])}i(e=>({...e,get:(t,a,n)=>p(t,a)?x:e.get(t,a,n),has:(t,a)=>p(t,a)||e.has(t,a)}));const D=64*Uint8Array.BYTES_PER_ELEMENT,B="p2party";async function S(){return function(e,t,{blocked:a,upgrade:n,blocking:o,terminated:r}={}){const s=indexedDB.open(e,t),i=d(s);return n&&s.addEventListener("upgradeneeded",e=>{n(d(s.result),e.oldVersion,e.newVersion,d(s.transaction),e)}),a&&s.addEventListener("blocked",e=>a(e.oldVersion,e.newVersion,e)),i.then(e=>{r&&e.addEventListener("close",()=>r()),o&&e.addEventListener("versionchange",e=>o(e.oldVersion,e.newVersion,e))}).catch(()=>{}),i}(B,16,{upgrade(e,t,a,n){if(e.objectStoreNames.contains("addressBook")){const e=n.objectStore("addressBook");e.indexNames.contains("username")||e.createIndex("username","username",{unique:!1}),e.indexNames.contains("peerId")||e.createIndex("peerId","peerId",{unique:!0}),e.indexNames.contains("peerPublicKey")||e.createIndex("peerPublicKey","peerPublicKey",{unique:!0})}else{const t=e.createObjectStore("addressBook",{keyPath:["peerId"]});t.createIndex("username","username",{unique:!1}),t.createIndex("peerId","peerId",{unique:!0}),t.createIndex("peerPublicKey","peerPublicKey",{unique:!0})}if(e.objectStoreNames.contains("blacklist")){const e=n.objectStore("blacklist");e.indexNames.contains("username")||e.createIndex("username","username",{unique:!1}),e.indexNames.contains("peerId")||e.createIndex("peerId","peerId",{unique:!0}),e.indexNames.contains("peerPublicKey")||e.createIndex("peerPublicKey","peerPublicKey",{unique:!0})}else{const t=e.createObjectStore("blacklist",{keyPath:["peerId"]});t.createIndex("username","username",{unique:!1}),t.createIndex("peerId","peerId",{unique:!0}),t.createIndex("peerPublicKey","peerPublicKey",{unique:!0})}if(e.objectStoreNames.contains("uniqueRoom")){const e=n.objectStore("uniqueRoom");e.indexNames.contains("roomUrl")||e.createIndex("roomUrl","roomUrl",{unique:!0}),e.indexNames.contains("roomId")||e.createIndex("roomId","roomId",{unique:!0})}else{const t=e.createObjectStore("uniqueRoom",{keyPath:["roomId"]});t.createIndex("roomUrl","roomUrl",{unique:!0}),t.createIndex("roomId","roomId",{unique:!0})}if(e.objectStoreNames.contains("messageData")){const e=n.objectStore("messageData");e.indexNames.contains("roomId")||e.createIndex("roomId","roomId",{unique:!1}),e.indexNames.contains("hash")||e.createIndex("hash","hash",{unique:!1}),e.indexNames.contains("merkleRoot")||e.createIndex("merkleRoot","merkleRoot",{unique:!0}),e.indexNames.contains("fromPeerId")||e.createIndex("fromPeerId","fromPeerId",{unique:!1})}else{const t=e.createObjectStore("messageData",{keyPath:["timestamp","roomId","merkleRoot"]});t.createIndex("roomId","roomId",{unique:!1}),t.createIndex("hash","hash",{unique:!1}),t.createIndex("merkleRoot","merkleRoot",{unique:!0}),t.createIndex("fromPeerId","fromPeerId",{unique:!1})}if(e.objectStoreNames.contains("chunks")){const e=n.objectStore("chunks");e.indexNames.contains("merkleRoot")||e.createIndex("merkleRoot","merkleRoot",{unique:!1}),e.indexNames.contains("hash")||e.createIndex("hash","hash",{unique:!1})}else{const t=e.createObjectStore("chunks",{keyPath:["merkleRoot","chunkIndex"]});t.createIndex("merkleRoot","merkleRoot",{unique:!1}),t.createIndex("hash","hash",{unique:!1})}if(e.objectStoreNames.contains("newChunks")){const e=n.objectStore("newChunks");e.indexNames.contains("hash")||e.createIndex("hash","hash",{unique:!1}),e.indexNames.contains("merkleRoot")||e.createIndex("merkleRoot","merkleRoot",{unique:!1}),e.indexNames.contains("realChunkHash")||e.createIndex("realChunkHash","realChunkHash",{unique:!0})}else{const t=e.createObjectStore("newChunks",{keyPath:["hash","chunkIndex"]});t.createIndex("hash","hash",{unique:!1}),t.createIndex("merkleRoot","merkleRoot",{unique:!1}),t.createIndex("realChunkHash","realChunkHash",{unique:!0})}if(e.objectStoreNames.contains("sendQueue")){const e=n.objectStore("sendQueue");e.indexNames.contains("labelPeer")||e.createIndex("labelPeer",["label","toPeerId"],{unique:!1})}else{e.createObjectStore("sendQueue",{keyPath:["position","label","toPeerId"]}).createIndex("labelPeer",["label","toPeerId"],{unique:!1})}}})}async function R(){await function(e,{blocked:t}={}){const a=indexedDB.deleteDatabase(e);return t&&a.addEventListener("blocked",e=>t(e.oldVersion,e)),d(a).then(()=>{})}(B,{blocked(){console.error("DB deletion BLOCKED")}})}onmessage=async e=>{const t=e.data,{id:a,method:n}=t;try{let e;switch(n){case"getDBAddressBookEntry":e=await async function(e,t){if(!e&&!t)return;if(e&&e.length<10&&!t)return;if(t&&64!==t.length&&!e)return;const a=await S();try{const n=a.transaction("addressBook","readonly"),o=e?n.objectStore("addressBook").index("peerId"):n.objectStore("addressBook").index("peerPublicKey"),r=e?await o.get(e):await o.get(t);return await n.done,a.close(),r?{username:r.username,peerId:r.peerId,peerPublicKey:r.peerPublicKey}:void 0}catch(e){return void a.close()}}(...t.args);break;case"getAllDBAddressBookEntries":e=await async function(){try{const e=await S(),t=await e.getAll("addressBook");return e.close(),t}catch(e){return[]}}(...t.args);break;case"setDBAddressBookEntry":e=await async function(e,t,a){try{const n=await S(),o=n.transaction(["addressBook"],"readwrite"),r=o.objectStore("addressBook"),s=r.index("peerId"),i=r.index("peerPublicKey"),c=await s.get(t),u=await i.get(a);(!c&&!u||c&&!u||!c&&u)&&await r.put({username:e,peerId:t,peerPublicKey:a,dateAdded:Date.now()}),await o.done,n.close()}catch(e){console.error(e)}}(...t.args);break;case"deleteDBAddressBookEntry":e=await async function(e,t,a){const n=!e||0===e.length,o=!t||t.length<10,r=!a||64!==a.length;if(n&&o&&r)throw new Error("Cannot delete address book with no data");let s=t??"";try{const n=await S(),i=n.transaction("addressBook","readwrite"),c=i.objectStore("addressBook");if(o)if(r){const t=c.index("username"),a=await t.getKey(e);if(a){const n=await t.get(e);s=n?.peerId??"",await c.delete(a)}}else{const e=c.index("peerPublicKey"),t=await e.getKey(a);if(t){const n=await e.get(a);s=n?.peerId??"",await c.delete(t)}}else{const e=c.index("peerId"),a=await e.getKey(t);a&&await c.delete(a)}await i.done,n.close()}catch{}return s}(...t.args);break;case"getDBPeerIsBlacklisted":e=await async function(e,t){if(!e&&!t)return!1;if(e&&e.length<10&&!t)return!1;if(t&&64!==t.length&&!e)return!1;try{const a=await S(),n=a.transaction("blacklist","readonly"),o=e?n.objectStore("blacklist").index("peerId"):n.objectStore("blacklist").index("peerPublicKey"),r=e?await o.get(e):await o.get(t);return await n.done,a.close(),!!r}catch(e){return!1}}(...t.args);break;case"getAllDBBlacklisted":e=await async function(){const e=await S();try{const t=await e.getAll("blacklist");return e.close(),t}catch(t){return e.close(),[]}}(...t.args);break;case"setDBPeerInBlacklist":e=await async function(e,t){try{const a=await S(),n=a.transaction(["blacklist"],"readwrite"),o=n.objectStore("blacklist"),r=o.index("peerId"),s=o.index("peerPublicKey"),i=await r.get(e),c=await s.get(t);(!i&&!c||i&&!c||!i&&c)&&await o.put({peerId:e,peerPublicKey:t,dateAdded:Date.now()}),await n.done,a.close()}catch(e){console.error(e)}}(...t.args);break;case"deleteDBPeerFromBlacklist":e=await async function(e,t){const a=!e||e.length<10,n=!t||64!==t.length;if(a&&n)throw new Error("Cannot delete blacklisted with no data");try{const n=await S(),o=n.transaction("blacklist","readwrite"),r=o.objectStore("blacklist");if(a){const e=r.index("peerPublicKey"),a=await e.getKey(t);a&&await r.delete(a)}else{const t=r.index("peerId"),a=await t.getKey(e);a&&await r.delete(a)}await o.done,n.close()}catch(e){console.error(e)}}(...t.args);break;case"getAllDBUniqueRooms":e=await async function(){const e=await S();try{const t=await e.getAll("uniqueRoom");return e.close(),t}catch(t){return e.close(),[]}}(...t.args);break;case"setDBUniqueRoom":e=await async function(e,t){try{const a=await S(),n=a.transaction(["uniqueRoom"],"readwrite"),o=n.objectStore("uniqueRoom"),r=o.index("roomUrl"),s=o.index("roomId"),i=await r.get(e),c=await s.get(t);if(!i&&!c){const a=Date.now();await o.put({roomId:t,roomUrl:e,messageCount:0,lastMessageMerkleRoot:"",createdAt:a,updatedAt:a})}await n.done,a.close()}catch(e){console.error(e)}}(...t.args);break;case"getDBMessageData":e=await async function(e,t){try{const a=await S(),n=a.transaction(["messageData"],"readonly"),o=n.objectStore("messageData"),r=o.index("merkleRoot"),s=o.index("hash");if(e&&e.length===2*D){const o=await r.get(e);if(o)return await n.done,a.close(),o;if(t&&t.length===2*D){const e=await s.get(t);return await n.done,a.close(),e}return await n.done,void a.close()}if(t&&t.length===2*D){const e=await s.get(t);return await n.done,a.close(),e}return await n.done,void a.close()}catch(e){return void console.error(e)}}(...t.args);break;case"getDBRoomMessageData":e=await async function(e){const t=await S(),a=await t.getAllFromIndex("messageData","roomId",e);t.close();const n=a.length,o=[];for(let t=0;t<n;t++)o.push({roomId:e,merkleRoot:a[t].merkleRoot,hash:a[t].hash,fromPeerId:a[t].fromPeerId,filename:a[t].filename,messageType:a[t].messageType,savedSize:a[t].savedSize,totalSize:a[t].totalSize,channelLabel:a[t].channelLabel,timestamp:a[t].timestamp});return o}(...t.args);break;case"setDBRoomMessageData":e=await async function(e,t,a,n,o,r,s,i,c,u){try{const d=await S(),l=d.transaction(["messageData","uniqueRoom"],"readwrite"),w=l.objectStore("messageData"),h=await w.index("merkleRoot").get(t),m=h?.savedSize??0;try{await w.put({roomId:e,timestamp:u,merkleRoot:t,hash:a,fromPeerId:n,filename:i,messageType:s,savedSize:m+o<=r?m+o:r,totalSize:r,channelLabel:c})}catch(e){throw await l.done,d.close(),e}const g=l.objectStore("uniqueRoom"),k=await g.index("roomId").get(e);if(k&&k.lastMessageMerkleRoot!==t)try{await g.put({...k,lastMessageMerkleRoot:t,messageCount:k.messageCount+1,updatedAt:u})}catch(e){throw await l.done,d.close(),e}await l.done,d.close()}catch(e){throw console.error(e),e}}(...t.args);break;case"getDBChunk":e=await async function(e,t){const a=await S(),n=await a.get("chunks",[e,t]);return a.close(),n?.data}(...t.args);break;case"existsDBChunk":e=await async function(e,t){const a=await S(),n=await a.count("chunks",[e,t]);return a.close(),n>0}(...t.args);break;case"getDBNewChunk":e=await async function(e,t){try{const a=await S(),n=t??-1;if(n>-1){const t=await a.get("newChunks",[e,n]);return a.close(),t}{const t=a.transaction("newChunks"),n=t.objectStore("newChunks").index("realChunkHash"),o=await n.get(e);return await t.done,a.close(),o}}catch(e){return void console.error(e)}}(...t.args);break;case"existsDBNewChunk":e=await async function(e,t){const a=await S(),n=await a.count("newChunks",[e,t]);return a.close(),n>0}(...t.args);break;case"getDBSendQueue":e=await async function(e,t,a){const n=await S();if(a){const o=await n.get("sendQueue",[a,e,t]);return n.close(),o?[o]:[]}const o=n.transaction("sendQueue","readonly").objectStore("sendQueue").index("labelPeer"),r=IDBKeyRange.only([e,t]),s=await o.getAll(r);return n.close(),s}(...t.args);break;case"getDBAllChunks":e=await async function(e,t){try{const a=await S(),n=a.transaction("chunks","readonly"),o=n.objectStore("chunks"),r=o.index("hash"),s=o.index("merkleRoot");if(e&&e.length===2*D){const t=await s.getAll(e);return 0===t.length?(await n.done,a.close(),[]):(await n.done,a.close(),t)}if(t&&t.length===2*D){const e=await r.getAll(t);return await n.done,a.close(),e.length>0?e.filter(t=>t.merkleRoot===e[0].merkleRoot):e}return await n.done,a.close(),[]}catch(e){return console.error(e),[]}}(...t.args);break;case"getDBAllChunksCount":e=await async function(e,t){try{const a=await S(),n=a.transaction(["chunks"],"readonly"),o=n.objectStore("chunks"),r=o.index("hash"),s=o.index("merkleRoot");if(t&&t.length===2*D){const o=await r.count(t);if(0===o){if(e&&e.length===2*D){const t=await s.count(e);return await n.done,a.close(),t}return await n.done,a.close(),0}return await n.done,a.close(),o}if(e&&e.length===2*D){const t=await s.count(e);return await n.done,a.close(),t}return await n.done,a.close(),0}catch(e){return 0}}(...t.args);break;case"setDBChunk":e=await async function(e){const t=await S();try{await t.add("chunks",e)}catch(e){}t.close()}(...t.args);break;case"getDBAllNewChunks":e=await async function(e,t){if(!e&&!t)return[];if(e?.length!==2*D&&t?.length!==2*D)return[];const a=await S();if((e?await a.countFromIndex("newChunks","hash",e):await a.countFromIndex("newChunks","merkleRoot",t))>0){const n=e?await a.getAllFromIndex("newChunks","hash",e):await a.getAllFromIndex("newChunks","merkleRoot",t);return a.close(),n}{const n=a.transaction("newChunks","readonly").objectStore("newChunks"),o=e?n.index("hash"):n.index("merkleRoot"),r=e?IDBKeyRange.only(e):IDBKeyRange.only(t),s=await o.getAll(r);return a.close(),s}}(...t.args);break;case"getDBAllNewChunksCount":e=await async function(e){const t=await S(),a=await t.countFromIndex("newChunks","hash",e);return t.close(),a}(...t.args);break;case"setDBNewChunk":e=await async function(e){const t=await S();await t.put("newChunks",e),t.close()}(...t.args);break;case"setDBSendQueue":e=await async function(e){const t=await S();await t.put("sendQueue",e),t.close()}(...t.args);break;case"countDBSendQueue":e=await async function(e,t){const a=await S(),n=a.transaction("sendQueue","readonly").objectStore("sendQueue").index("labelPeer"),o=IDBKeyRange.only([e,t]),r=await n.count(o);return a.close(),r}(...t.args);break;case"deleteDBChunk":e=await async function(e,t){try{const a=await S(),n=a.transaction("chunks","readwrite"),o=n.objectStore("chunks");if(t){const a=IDBKeyRange.only([e,t]);await o.delete(a)}else{const t=o.index("hash"),a=await t.getAllKeys(e),n=a.length;if(0===n){const t=o.index("merkleRoot"),a=await t.getAllKeys(e),n=a.length;for(let e=0;e<n;e++)await o.delete(a[e])}else for(let e=0;e<n;e++)await o.delete(a[e])}await n.done,a.close()}catch(e){}}(...t.args);break;case"deleteDBNewChunk":e=await async function(e,t,a,n){try{const o=await S(),r=o.transaction("newChunks","readwrite"),s=r.objectStore("newChunks");if(e){const t=s.index("merkleRoot"),a=await t.getAllKeys(e),n=a.length;for(let e=0;e<n;e++)await s.delete(a[e])}else if(a&&n)await s.delete([a,n]);else if(a){const e=s.index("hash"),t=await e.getAllKeys(a),n=t.length;for(let e=0;e<n;e++)await s.delete(t[e])}else if(t){const e=s.index("realChunkHash"),a=await e.getKey(t);a&&await s.delete(a)}await r.done,o.close()}catch(e){console.error(e)}}(...t.args);break;case"deleteDBMessageData":e=await async function(e){try{const t=await S(),a=t.transaction(["messageData","uniqueRoom"],"readwrite"),n=a.objectStore("messageData"),o=a.objectStore("uniqueRoom"),r=n.index("merkleRoot"),s=await r.getAllKeys(e),i=s.length;let c="";for(let e=0;e<i;e++){if(0===e||0===c.length){const t=await n.get(s[e]);c=t?.roomId??""}await n.delete(s[e])}if(c.length>0){const e=o.index("roomId"),t=await e.get(c);if(t){const e=n.index("roomId"),a=await e.getAllKeys(c),r=a.length,s=r>0?await n.get(a[r-1]):void 0;await o.put({...t,messageCount:t.messageCount>0?t.messageCount-1:0,updatedAt:s?.timestamp??Date.now(),lastMessageMerkleRoot:s?.merkleRoot??""})}}await a.done,t.close()}catch(e){}}(...t.args);break;case"deleteDBUniqueRoom":e=await async function(e){try{const t=await S(),a=t.transaction("uniqueRoom","readwrite"),n=a.objectStore("uniqueRoom"),o=n.index("roomId"),r=await o.getKey(e);r&&await n.delete(r),await a.done,t.close()}catch(e){}}(...t.args);break;case"deleteDBSendQueue":e=await async function(e,t,a){try{const n=await S();if(a)await n.delete("sendQueue",[a,e,t]);else{const a=IDBKeyRange.only([e,t]);await n.delete("sendQueue",a)}n.close()}catch(e){}}(...t.args);break;case"deleteDB":e=await R();break;default:return void postMessage({id:a,error:"Method not found"})}postMessage({id:a,result:e})}catch(e){postMessage({id:a,error:String(e)})}};
|
|
2
2
|
//# sourceMappingURL=db.worker.js.map
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import type { BaseQueryApi } from "@reduxjs/toolkit/query";
|
|
2
2
|
import type { IRTCPeerConnection, IRTCDataChannel } from "../api/webrtc/interfaces";
|
|
3
|
-
export declare const observableBool: (init: boolean, onChange: (newValue: boolean) => void) => {
|
|
4
|
-
get: () => boolean;
|
|
5
|
-
set: (v: boolean) => void;
|
|
6
|
-
};
|
|
7
3
|
export interface OpenChannelHelperParams {
|
|
8
4
|
channel: string | RTCDataChannel;
|
|
9
5
|
epc: IRTCPeerConnection;
|