-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,67 @@ | ||
import * as Crypto from 'expo-crypto'; | ||
|
||
// Polyfill for global.crypto if it doesn't exist. | ||
if (typeof global.crypto === 'undefined') { | ||
global.crypto = { | ||
getRandomValues: <T extends ArrayBufferView | null>(buffer: T): T => { | ||
if (!buffer) return buffer; | ||
const ints = new Uint8Array(buffer.byteLength); | ||
Crypto.getRandomValues(ints); | ||
new Uint8Array(buffer.buffer).set(ints); | ||
return buffer; | ||
}, | ||
} as Crypto; | ||
} | ||
global.crypto = { | ||
// Polyfill getRandomValues | ||
getRandomValues: <T extends ArrayBufferView | null>(buffer: T): T => { | ||
if (!buffer) return buffer; | ||
const ints = new Uint8Array(buffer.byteLength); | ||
Crypto.getRandomValues(ints); | ||
new Uint8Array(buffer.buffer).set(ints); | ||
return buffer; | ||
}, | ||
// Polyfill subtle if missing | ||
subtle: { | ||
async digest(algorithm: string, data: BufferSource): Promise<ArrayBuffer> { | ||
// Convert BufferSource to ArrayBuffer | ||
let dataBuffer: ArrayBuffer; | ||
if (data instanceof ArrayBuffer) { | ||
dataBuffer = data; | ||
} else if (ArrayBuffer.isView(data)) { | ||
dataBuffer = data.buffer; | ||
} else { | ||
throw new Error('Unsupported data type for digest'); | ||
} | ||
|
||
// Convert ArrayBuffer to a UTF-8 string. | ||
// WARNING: This assumes that the input is valid UTF-8. | ||
const decoded = new TextDecoder().decode(dataBuffer); | ||
|
||
// Calculate digest as a hex string using Expo Crypto. | ||
const digestHex = await Crypto.digestStringAsync(algorithm, decoded); | ||
|
||
// Convert hex string back to ArrayBuffer | ||
const buffer = new ArrayBuffer(digestHex.length / 2); | ||
const view = new Uint8Array(buffer); | ||
for (let i = 0; i < digestHex.length; i += 2) { | ||
view[i / 2] = parseInt(digestHex.substr(i, 2), 16); | ||
} | ||
return buffer; | ||
}, | ||
}, | ||
}; | ||
} else if (typeof global.crypto.subtle === 'undefined') { | ||
// If crypto exists but crypto.subtle is missing, add the subtle polyfill. | ||
global.crypto.subtle = { | ||
async digest(algorithm: string, data: BufferSource): Promise<ArrayBuffer> { | ||
let dataBuffer: ArrayBuffer; | ||
if (data instanceof ArrayBuffer) { | ||
dataBuffer = data; | ||
} else if (ArrayBuffer.isView(data)) { | ||
dataBuffer = data.buffer; | ||
} else { | ||
throw new Error('Unsupported data type for digest'); | ||
} | ||
|
||
const decoded = new TextDecoder().decode(dataBuffer); | ||
const digestHex = await Crypto.digestStringAsync(algorithm, decoded); | ||
const buffer = new ArrayBuffer(digestHex.length / 2); | ||
const view = new Uint8Array(buffer); | ||
for (let i = 0; i < digestHex.length; i += 2) { | ||
view[i / 2] = parseInt(digestHex.substr(i, 2), 16); | ||
} | ||
return buffer; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
import { getRxStorageWorker } from 'rxdb-premium/plugins/storage-worker'; | ||
import { wrappedValidateZSchemaStorage } from 'rxdb/plugins/validate-z-schema'; | ||
|
||
export const fastConfig = { | ||
storage: getRxStorageWorker({ | ||
/** | ||
* Contains any value that can be used as parameter | ||
* to the Worker constructor of thread.js | ||
* Most likely you want to put the path to the worker.js file in here. | ||
* | ||
* @link https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker | ||
*/ | ||
workerInput: globalThis.idbWorker, | ||
/** | ||
* (Optional) options | ||
* for the worker. | ||
*/ | ||
// workerOptions: { | ||
// type: 'module', | ||
// credentials: 'omit', | ||
// }, | ||
}), | ||
}; | ||
const storage = getRxStorageWorker({ | ||
/** | ||
* Contains any value that can be used as parameter | ||
* to the Worker constructor of thread.js | ||
* Most likely you want to put the path to the worker.js file in here. | ||
* | ||
* @link https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker | ||
*/ | ||
workerInput: globalThis.idbWorker, | ||
/** | ||
* (Optional) options | ||
* for the worker. | ||
*/ | ||
// workerOptions: { | ||
// type: 'module', | ||
// credentials: 'omit', | ||
// }, | ||
}); | ||
|
||
const devStorage = wrappedValidateZSchemaStorage({ | ||
storage, | ||
}); | ||
|
||
export const fastConfig = { storage: __DEV__ ? devStorage : storage }; |