feat: enable bundling of encrypted modules with CSS injection and external dependency support
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { useState, useCallback } from "react";
|
||||
import CryptoJS from "crypto-js";
|
||||
import React from "react";
|
||||
import * as React from "react";
|
||||
import * as jsxRuntime from "react/jsx-runtime";
|
||||
import type { CryptoLockerModule } from "./types";
|
||||
|
||||
export type CryptoLockerStatus = "idle" | "loading" | "success" | "error";
|
||||
@@ -8,11 +9,13 @@ export type CryptoLockerStatus = "idle" | "loading" | "success" | "error";
|
||||
/**
|
||||
* Hook that provides logic for AES decryption of dynamic modules.
|
||||
* UI, password handling, and validation are fully delegated to the developer.
|
||||
*
|
||||
* @param dependencies Optional dictionary of external dependencies to provide to the encrypted module.
|
||||
*/
|
||||
export function useCryptoLocker<T = unknown>() {
|
||||
const [status, setStatus] = useState<CryptoLockerStatus>("idle");
|
||||
const [decryptedData, setDecryptedData] = useState<T | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
export function useCryptoLocker<T = unknown>(dependencies: Record<string, any> = {}) {
|
||||
const [status, setStatus] = React.useState<CryptoLockerStatus>("idle");
|
||||
const [decryptedData, setDecryptedData] = React.useState<T | null>(null);
|
||||
const [error, setError] = React.useState<string | null>(null);
|
||||
|
||||
/**
|
||||
* Decrypts the dynamically loaded module and evaluates its code.
|
||||
@@ -53,9 +56,13 @@ export function useCryptoLocker<T = unknown>() {
|
||||
const exportsObj: any = {};
|
||||
const moduleObj = { exports: exportsObj };
|
||||
|
||||
// Provide React dependency to the evaluated module
|
||||
// Provide dependencies to the evaluated module
|
||||
const requireFn = (id: string) => {
|
||||
if (id === "react") return React;
|
||||
if (id === "react/jsx-runtime") return jsxRuntime;
|
||||
if (id === "react-dom") return (window as any).ReactDOM || {};
|
||||
if (/\.(css|scss|less)$/i.test(id)) return {}; // Ignore CSS imports inside secret modules
|
||||
if (id in dependencies) return dependencies[id];
|
||||
throw new Error(`Cannot require '${id}' in encrypted module.`);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user