favalib 0.0.11 → 0.0.13

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.
Files changed (49) hide show
  1. package/build/TwoFALibError.d.mts +1 -1
  2. package/build/TwoFALibError.mjs +1 -1
  3. package/build/TwoFaLib.d.mts +6 -6
  4. package/build/TwoFaLib.mjs +22 -18
  5. package/build/interfaces/CryptoLib.d.mts +11 -11
  6. package/build/interfaces/OpenPgpLib.d.mts +20 -0
  7. package/build/interfaces/PasswordExtraDict.d.ts +2 -0
  8. package/build/interfaces/PasswordExtraDict.js +1 -0
  9. package/build/interfaces/PlatformProviders.d.mts +26 -0
  10. package/build/interfaces/PlatformProviders.mjs +1 -0
  11. package/build/interfaces/QrCodeLib.d.mts +19 -0
  12. package/build/interfaces/QrCodeLib.mjs +1 -0
  13. package/build/main.d.mts +3 -2
  14. package/build/{CryptoProviders/browser/index.d.mts → platformProviders/browser/cryptoLib.d.mts} +6 -6
  15. package/build/{CryptoProviders/browser/index.mjs → platformProviders/browser/cryptoLib.mjs} +21 -21
  16. package/build/platformProviders/browser/index.d.mts +6 -0
  17. package/build/platformProviders/browser/index.mjs +13 -0
  18. package/build/platformProviders/browser/openPgpLib.d.mts +20 -0
  19. package/build/platformProviders/browser/openPgpLib.mjs +44 -0
  20. package/build/platformProviders/browser/qrCodeLib.d.mts +25 -0
  21. package/build/platformProviders/browser/qrCodeLib.mjs +69 -0
  22. package/build/{CryptoProviders/node/index.d.mts → platformProviders/node/cryptoLib.d.mts} +4 -4
  23. package/build/{CryptoProviders/node/index.mjs → platformProviders/node/cryptoLib.mjs} +12 -12
  24. package/build/platformProviders/node/index.d.mts +6 -0
  25. package/build/platformProviders/node/index.mjs +13 -0
  26. package/build/platformProviders/node/openPgpLib.d.mts +20 -0
  27. package/build/platformProviders/node/openPgpLib.mjs +44 -0
  28. package/build/platformProviders/node/qrCodeLib.d.mts +28 -0
  29. package/build/platformProviders/node/qrCodeLib.mjs +54 -0
  30. package/build/subclasses/ExportImportManager.d.mts +8 -8
  31. package/build/subclasses/ExportImportManager.mjs +15 -15
  32. package/build/subclasses/LibraryLoader.d.mts +26 -12
  33. package/build/subclasses/LibraryLoader.mjs +26 -34
  34. package/build/subclasses/PersistentStorageManager.d.mts +16 -16
  35. package/build/subclasses/PersistentStorageManager.mjs +20 -20
  36. package/build/subclasses/StorageOperationsManager.d.mts +7 -7
  37. package/build/subclasses/StorageOperationsManager.mjs +7 -7
  38. package/build/subclasses/SyncManager.mjs +7 -6
  39. package/build/utils/creationUtils.d.mts +20 -20
  40. package/build/utils/creationUtils.mjs +36 -34
  41. package/build/utils/exportImportUtils.d.mts +5 -3
  42. package/build/utils/exportImportUtils.mjs +2 -12
  43. package/build/utils/qrUtils.d.mts +0 -16
  44. package/build/utils/qrUtils.mjs +2 -68
  45. package/build/utils/syncUtils.d.mts +3 -2
  46. package/build/utils/syncUtils.mjs +3 -19
  47. package/package.json +8 -8
  48. package/build/interfaces/PassphraseExtraDict.d.ts +0 -2
  49. /package/build/interfaces/{PassphraseExtraDict.js → OpenPgpLib.mjs} +0 -0
@@ -1,20 +1,4 @@
1
- import type { ImageData } from 'canvas';
2
1
  import LibraryLoader from '../subclasses/LibraryLoader.mjs';
3
- /**
4
- * Gets the ImageData from an image, for browser environments.
5
- * This ImageData is then further processed to get QR codes.
6
- * @param input - The image to get the ImageData from.
7
- * @returns A promise that resolves to the ImageData.
8
- */
9
- export declare const getImageDataBrowser: (input: string | File) => Promise<ImageData>;
10
- /**
11
- * Gets the ImageData from an image, for Node.js environments.
12
- * This ImageData is then further processed to get QR codes.
13
- * @param canvasLib - The Canvas library.
14
- * @param inputImage - The image to get the ImageData from.
15
- * @returns A promise that resolves to the ImageData.
16
- */
17
- export declare const getImageDataNode: (canvasLib: typeof import("canvas"), inputImage: Uint8Array | string) => Promise<ImageData>;
18
2
  /**
19
3
  * Import an entry from a QR code image.
20
4
  * @param libraryLoader - An instance of LibraryLoader.
@@ -1,59 +1,4 @@
1
- import { isUint8Array } from 'uint8array-extras';
2
1
  import { TwoFALibError } from '../TwoFALibError.mjs';
3
- /**
4
- * Gets the ImageData from an image, for browser environments.
5
- * This ImageData is then further processed to get QR codes.
6
- * @param input - The image to get the ImageData from.
7
- * @returns A promise that resolves to the ImageData.
8
- */
9
- export const getImageDataBrowser = (input) => {
10
- return new Promise((resolve, reject) => {
11
- const img = new Image();
12
- img.onload = () => {
13
- const canvas = document.createElement('canvas');
14
- canvas.width = img.width;
15
- canvas.height = img.height;
16
- const ctx = canvas.getContext('2d');
17
- if (!ctx) {
18
- reject(new TwoFALibError('Could not create canvas context'));
19
- return;
20
- }
21
- ctx.drawImage(img, 0, 0);
22
- resolve(ctx.getImageData(0, 0, img.width, img.height));
23
- };
24
- img.onerror = () => reject(new TwoFALibError('Failed to load image'));
25
- if (typeof input === 'string') {
26
- // URL or Data URL
27
- img.src = input;
28
- }
29
- else {
30
- // File object
31
- const reader = new FileReader();
32
- reader.onload = (e) => {
33
- img.src = e.target?.result;
34
- };
35
- reader.onerror = () => reject(new TwoFALibError('Failed to read file'));
36
- reader.readAsDataURL(input);
37
- }
38
- });
39
- };
40
- /**
41
- * Gets the ImageData from an image, for Node.js environments.
42
- * This ImageData is then further processed to get QR codes.
43
- * @param canvasLib - The Canvas library.
44
- * @param inputImage - The image to get the ImageData from.
45
- * @returns A promise that resolves to the ImageData.
46
- */
47
- export const getImageDataNode = async (canvasLib, inputImage) => {
48
- const { createCanvas, loadImage } = canvasLib;
49
- // eslint-disable-next-line no-restricted-globals
50
- const input = isUint8Array(inputImage) ? Buffer.from(inputImage) : inputImage;
51
- const image = await loadImage(input); // canvaslib expects a buffer
52
- const canvas = createCanvas(image.width, image.height);
53
- const ctx = canvas.getContext('2d');
54
- ctx.drawImage(image, 0, 0);
55
- return ctx.getImageData(0, 0, image.width, image.height);
56
- };
57
2
  /**
58
3
  * Import an entry from a QR code image.
59
4
  * @param libraryLoader - An instance of LibraryLoader.
@@ -63,19 +8,8 @@ export const getImageDataNode = async (canvasLib, inputImage) => {
63
8
  */
64
9
  export const getDataFromQRImage = async (libraryLoader, imageInput) => {
65
10
  const jsQr = await libraryLoader.getJsQrLib();
66
- let imageData;
67
- if (typeof window !== 'undefined') {
68
- // Browser environment
69
- imageData = await getImageDataBrowser(imageInput);
70
- }
71
- else {
72
- if (imageInput instanceof File) {
73
- throw new TwoFALibError('Getting data from QR where image type is "File" is not supported in the node environment');
74
- }
75
- // Node.js environment
76
- const canvasLib = await libraryLoader.getCanvasLib();
77
- imageData = await getImageDataNode(canvasLib, imageInput);
78
- }
11
+ const qrLib = libraryLoader.getQrGeneratorLib();
12
+ const imageData = await qrLib.getImageDataFromInput(imageInput);
79
13
  const qrCodeResult = jsQr(imageData.data, imageData.width, imageData.height);
80
14
  if (!qrCodeResult) {
81
15
  throw new TwoFALibError("Couldn't read QR code data from image");
@@ -1,13 +1,14 @@
1
1
  import { InitiateAddDeviceFlowResult } from '../interfaces/SyncTypes.mjs';
2
+ import type { QrCodeLib } from '../interfaces/QrCodeLib.mjs';
2
3
  /**
3
4
  * Decodes the initiator data from a string or QR code.
4
5
  * @param initiatorData - The initiator data to decode.
5
6
  * @param initiatorDataType The type of the initiatorData, determines how it should be decoded
6
7
  * @param jsQr - The QR code decoder.
7
- * @param getCanvasLib - A function to get the Canvas library.
8
+ * @param qrCodeLib - The extended QR code library with platform-specific image processing.
8
9
  * @returns A promise that resolves to the decoded initiator data.
9
10
  */
10
- export declare const decodeInitiatorData: (initiatorData: string | Uint8Array | File, initiatorDataType: "text" | "qr", jsQr: typeof import("jsqr").default, getCanvasLib: () => Promise<typeof import("canvas")>) => Promise<InitiateAddDeviceFlowResult>;
11
+ export declare const decodeInitiatorData: (initiatorData: string | Uint8Array | File, initiatorDataType: "text" | "qr", jsQr: typeof import("jsqr").default, qrCodeLib: QrCodeLib) => Promise<InitiateAddDeviceFlowResult>;
11
12
  /**
12
13
  * Converts a JSONified Uint8Array to a Uint8Array.
13
14
  * A JSONified Uint8Array is the output of JSON.stringify on a Uint8Array.
@@ -1,33 +1,17 @@
1
1
  import { base64ToString } from 'uint8array-extras';
2
- import { getImageDataBrowser, getImageDataNode } from './qrUtils.mjs';
3
2
  import { SyncError } from '../TwoFALibError.mjs';
4
3
  /**
5
4
  * Decodes the initiator data from a string or QR code.
6
5
  * @param initiatorData - The initiator data to decode.
7
6
  * @param initiatorDataType The type of the initiatorData, determines how it should be decoded
8
7
  * @param jsQr - The QR code decoder.
9
- * @param getCanvasLib - A function to get the Canvas library.
8
+ * @param qrCodeLib - The extended QR code library with platform-specific image processing.
10
9
  * @returns A promise that resolves to the decoded initiator data.
11
10
  */
12
- export const decodeInitiatorData = async (initiatorData, initiatorDataType, jsQr, getCanvasLib) => {
11
+ export const decodeInitiatorData = async (initiatorData, initiatorDataType, jsQr, qrCodeLib) => {
13
12
  if (initiatorDataType === 'qr') {
14
13
  try {
15
- let imageData;
16
- if (typeof window !== 'undefined') {
17
- if (initiatorData instanceof Uint8Array) {
18
- throw new SyncError('Invalid initiator data type, should be a string or File in browser environment');
19
- }
20
- // Browser environment
21
- imageData = await getImageDataBrowser(initiatorData);
22
- }
23
- else {
24
- if (!(initiatorData instanceof Uint8Array)) {
25
- throw new SyncError('Invalid initiator data type, should be a Uint8Array in Node.js environment');
26
- }
27
- // Node.js environment
28
- const canvasLib = await getCanvasLib();
29
- imageData = await getImageDataNode(canvasLib, initiatorData);
30
- }
14
+ const imageData = await qrCodeLib.getImageDataFromInput(initiatorData);
31
15
  const qrCodeResult = jsQr(imageData.data, imageData.width, imageData.height);
32
16
  if (!qrCodeResult) {
33
17
  throw new SyncError('Invalid QR code');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "favalib",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -8,13 +8,13 @@
8
8
  "import": "./build/main.mjs",
9
9
  "types": "./build/main.d.mts"
10
10
  },
11
- "./cryptoProviders/browser": {
12
- "import": "./build/CryptoProviders/browser/index.mjs",
13
- "types": "./build/CryptoProviders/browser/index.d.mts"
11
+ "./platformProviders/browser": {
12
+ "import": "./build/platformProviders/browser/index.mjs",
13
+ "types": "./build/platformProviders/browser/index.d.mts"
14
14
  },
15
- "./cryptoProviders/node": {
16
- "import": "./build/CryptoProviders/node/index.mjs",
17
- "types": "./build/CryptoProviders/node/index.d.mts"
15
+ "./platformProviders/node": {
16
+ "import": "./build/platformProviders/node/index.mjs",
17
+ "types": "./build/platformProviders/node/index.d.mts"
18
18
  }
19
19
  },
20
20
  "types": "./build/main.d.mts",
@@ -39,6 +39,7 @@
39
39
  "@zxcvbn-ts/language-common": "^3.0.4",
40
40
  "@zxcvbn-ts/language-en": "^3.0.2",
41
41
  "canvas": "^3.1.0",
42
+ "esbuild": "^0.25.8",
42
43
  "hash-wasm": "^4.12.0",
43
44
  "jpake-ts": "^1.0.1",
44
45
  "jsqr": "^1.4.0",
@@ -48,7 +49,6 @@
48
49
  "totp-generator": "^1.0.0",
49
50
  "typescript-event-target": "^1.1.1",
50
51
  "uint8array-extras": "^1.4.0",
51
- "unws": "^0.3.2",
52
52
  "uuid": "^11.1.0",
53
53
  "whatwg-url": "^14.2.0",
54
54
  "ws": "^8.18.2"
@@ -1,2 +0,0 @@
1
- import type { NonEmptyTuple } from 'type-fest';
2
- export type PassphraseExtraDict = NonEmptyTuple<string>;