@dynlabs/react-native-immutable-file-cache 1.0.0-alpha.1

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 (113) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +415 -0
  3. package/lib/commonjs/adapters/memoryAdapter.js +266 -0
  4. package/lib/commonjs/adapters/memoryAdapter.js.map +1 -0
  5. package/lib/commonjs/adapters/rnfsAdapter.js +259 -0
  6. package/lib/commonjs/adapters/rnfsAdapter.js.map +1 -0
  7. package/lib/commonjs/adapters/webAdapter.js +432 -0
  8. package/lib/commonjs/adapters/webAdapter.js.map +1 -0
  9. package/lib/commonjs/core/adapter.js +2 -0
  10. package/lib/commonjs/core/adapter.js.map +1 -0
  11. package/lib/commonjs/core/cacheEngine.js +578 -0
  12. package/lib/commonjs/core/cacheEngine.js.map +1 -0
  13. package/lib/commonjs/core/errors.js +83 -0
  14. package/lib/commonjs/core/errors.js.map +1 -0
  15. package/lib/commonjs/core/hash.js +83 -0
  16. package/lib/commonjs/core/hash.js.map +1 -0
  17. package/lib/commonjs/core/indexStore.js +175 -0
  18. package/lib/commonjs/core/indexStore.js.map +1 -0
  19. package/lib/commonjs/core/mutex.js +143 -0
  20. package/lib/commonjs/core/mutex.js.map +1 -0
  21. package/lib/commonjs/core/prune.js +127 -0
  22. package/lib/commonjs/core/prune.js.map +1 -0
  23. package/lib/commonjs/core/types.js +6 -0
  24. package/lib/commonjs/core/types.js.map +1 -0
  25. package/lib/commonjs/factory.js +56 -0
  26. package/lib/commonjs/factory.js.map +1 -0
  27. package/lib/commonjs/index.js +110 -0
  28. package/lib/commonjs/index.js.map +1 -0
  29. package/lib/commonjs/index.native.js +74 -0
  30. package/lib/commonjs/index.native.js.map +1 -0
  31. package/lib/commonjs/index.web.js +75 -0
  32. package/lib/commonjs/index.web.js.map +1 -0
  33. package/lib/commonjs/types/react-native-fs.d.js +2 -0
  34. package/lib/commonjs/types/react-native-fs.d.js.map +1 -0
  35. package/lib/module/adapters/memoryAdapter.js +261 -0
  36. package/lib/module/adapters/memoryAdapter.js.map +1 -0
  37. package/lib/module/adapters/rnfsAdapter.js +251 -0
  38. package/lib/module/adapters/rnfsAdapter.js.map +1 -0
  39. package/lib/module/adapters/webAdapter.js +426 -0
  40. package/lib/module/adapters/webAdapter.js.map +1 -0
  41. package/lib/module/core/adapter.js +2 -0
  42. package/lib/module/core/adapter.js.map +1 -0
  43. package/lib/module/core/cacheEngine.js +571 -0
  44. package/lib/module/core/cacheEngine.js.map +1 -0
  45. package/lib/module/core/errors.js +71 -0
  46. package/lib/module/core/errors.js.map +1 -0
  47. package/lib/module/core/hash.js +76 -0
  48. package/lib/module/core/hash.js.map +1 -0
  49. package/lib/module/core/indexStore.js +168 -0
  50. package/lib/module/core/indexStore.js.map +1 -0
  51. package/lib/module/core/mutex.js +135 -0
  52. package/lib/module/core/mutex.js.map +1 -0
  53. package/lib/module/core/prune.js +116 -0
  54. package/lib/module/core/prune.js.map +1 -0
  55. package/lib/module/core/types.js +2 -0
  56. package/lib/module/core/types.js.map +1 -0
  57. package/lib/module/factory.js +49 -0
  58. package/lib/module/factory.js.map +1 -0
  59. package/lib/module/index.js +41 -0
  60. package/lib/module/index.js.map +1 -0
  61. package/lib/module/index.native.js +54 -0
  62. package/lib/module/index.native.js.map +1 -0
  63. package/lib/module/index.web.js +55 -0
  64. package/lib/module/index.web.js.map +1 -0
  65. package/lib/module/types/react-native-fs.d.js +2 -0
  66. package/lib/module/types/react-native-fs.d.js.map +1 -0
  67. package/lib/typescript/src/adapters/memoryAdapter.d.ts +23 -0
  68. package/lib/typescript/src/adapters/memoryAdapter.d.ts.map +1 -0
  69. package/lib/typescript/src/adapters/rnfsAdapter.d.ts +18 -0
  70. package/lib/typescript/src/adapters/rnfsAdapter.d.ts.map +1 -0
  71. package/lib/typescript/src/adapters/webAdapter.d.ts +30 -0
  72. package/lib/typescript/src/adapters/webAdapter.d.ts.map +1 -0
  73. package/lib/typescript/src/core/adapter.d.ts +105 -0
  74. package/lib/typescript/src/core/adapter.d.ts.map +1 -0
  75. package/lib/typescript/src/core/cacheEngine.d.ts +99 -0
  76. package/lib/typescript/src/core/cacheEngine.d.ts.map +1 -0
  77. package/lib/typescript/src/core/errors.d.ts +54 -0
  78. package/lib/typescript/src/core/errors.d.ts.map +1 -0
  79. package/lib/typescript/src/core/hash.d.ts +20 -0
  80. package/lib/typescript/src/core/hash.d.ts.map +1 -0
  81. package/lib/typescript/src/core/indexStore.d.ts +34 -0
  82. package/lib/typescript/src/core/indexStore.d.ts.map +1 -0
  83. package/lib/typescript/src/core/mutex.d.ts +49 -0
  84. package/lib/typescript/src/core/mutex.d.ts.map +1 -0
  85. package/lib/typescript/src/core/prune.d.ts +39 -0
  86. package/lib/typescript/src/core/prune.d.ts.map +1 -0
  87. package/lib/typescript/src/core/types.d.ts +109 -0
  88. package/lib/typescript/src/core/types.d.ts.map +1 -0
  89. package/lib/typescript/src/factory.d.ts +46 -0
  90. package/lib/typescript/src/factory.d.ts.map +1 -0
  91. package/lib/typescript/src/index.d.ts +20 -0
  92. package/lib/typescript/src/index.d.ts.map +1 -0
  93. package/lib/typescript/src/index.native.d.ts +37 -0
  94. package/lib/typescript/src/index.native.d.ts.map +1 -0
  95. package/lib/typescript/src/index.web.d.ts +38 -0
  96. package/lib/typescript/src/index.web.d.ts.map +1 -0
  97. package/package.json +125 -0
  98. package/src/adapters/memoryAdapter.ts +307 -0
  99. package/src/adapters/rnfsAdapter.ts +283 -0
  100. package/src/adapters/webAdapter.ts +480 -0
  101. package/src/core/adapter.ts +128 -0
  102. package/src/core/cacheEngine.ts +634 -0
  103. package/src/core/errors.ts +82 -0
  104. package/src/core/hash.ts +78 -0
  105. package/src/core/indexStore.ts +184 -0
  106. package/src/core/mutex.ts +134 -0
  107. package/src/core/prune.ts +145 -0
  108. package/src/core/types.ts +165 -0
  109. package/src/factory.ts +60 -0
  110. package/src/index.native.ts +58 -0
  111. package/src/index.ts +82 -0
  112. package/src/index.web.ts +59 -0
  113. package/src/types/react-native-fs.d.ts +75 -0
package/src/factory.ts ADDED
@@ -0,0 +1,60 @@
1
+ import type { ICacheConfig } from "./core/types";
2
+ import type { IStorageAdapter } from "./core/adapter";
3
+ import { CacheEngine } from "./core/cacheEngine";
4
+
5
+ export interface ICreateCacheOptions extends ICacheConfig {
6
+ /**
7
+ * If true, auto-initializes the cache before returning.
8
+ * @default true
9
+ */
10
+ readonly autoInit?: boolean;
11
+ }
12
+
13
+ /**
14
+ * Creates an immutable file cache instance.
15
+ *
16
+ * This is the base factory function. Platform-specific entrypoints
17
+ * (index.native.ts, index.web.ts) wrap this to auto-select the appropriate adapter.
18
+ *
19
+ * @param options - Cache configuration options
20
+ * @param adapter - Storage adapter (required in base factory)
21
+ * @returns Initialized CacheEngine instance
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * import { createImmutableFileCache } from "react-native-immutable-file-cache";
26
+ *
27
+ * const cache = await createImmutableFileCache({
28
+ * namespace: "images",
29
+ * defaultTtlMs: 7 * 24 * 60 * 60 * 1000, // 7 days
30
+ * });
31
+ * ```
32
+ */
33
+ export async function createImmutableFileCache(
34
+ options: ICreateCacheOptions & { adapter: IStorageAdapter }
35
+ ): Promise<CacheEngine> {
36
+ const { autoInit = true, adapter, ...config } = options;
37
+
38
+ const engine = new CacheEngine(config, adapter);
39
+
40
+ if (autoInit) {
41
+ await engine.init();
42
+ }
43
+
44
+ return engine;
45
+ }
46
+
47
+ /**
48
+ * Creates a cache engine without auto-initialization.
49
+ * Useful when you need to control initialization timing.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const engine = createCacheEngineSync({ namespace: "images" }, adapter);
54
+ * // Later...
55
+ * await engine.init();
56
+ * ```
57
+ */
58
+ export function createCacheEngineSync(config: ICacheConfig, adapter: IStorageAdapter): CacheEngine {
59
+ return new CacheEngine(config, adapter);
60
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @dynlabs/react-native-immutable-file-cache - Native entrypoint
3
+ * Auto-selects RNFS adapter for React Native (iOS/Android).
4
+ */
5
+
6
+ // Re-export everything from main index
7
+ export * from "./index";
8
+
9
+ import { createRnfsAdapter } from "./adapters/rnfsAdapter";
10
+ import type { ICreateCacheOptions } from "./factory";
11
+ import { CacheEngine } from "./core/cacheEngine";
12
+
13
+ /**
14
+ * Creates an immutable file cache instance for React Native.
15
+ * Automatically uses the RNFS adapter.
16
+ *
17
+ * @param options - Cache configuration options
18
+ * @returns Initialized CacheEngine instance
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { createImmutableFileCache } from "@dynlabs/react-native-immutable-file-cache";
23
+ *
24
+ * const cache = await createImmutableFileCache({
25
+ * namespace: "images",
26
+ * defaultTtlMs: 7 * 24 * 60 * 60 * 1000,
27
+ * });
28
+ *
29
+ * // Cache an image
30
+ * const result = await cache.putFromUrl("avatar", "https://example.com/avatar.jpg");
31
+ *
32
+ * // Get cached image URI
33
+ * const entry = await cache.get("avatar");
34
+ * if (entry) {
35
+ * console.log("Image URI:", entry.uri);
36
+ * }
37
+ * ```
38
+ */
39
+ export async function createImmutableFileCache(
40
+ options?: Omit<ICreateCacheOptions, "adapter"> & { adapter?: ICreateCacheOptions["adapter"] }
41
+ ): Promise<CacheEngine> {
42
+ const { autoInit = true, adapter, ...config } = options ?? {};
43
+
44
+ // Use provided adapter or create RNFS adapter
45
+ const resolvedAdapter =
46
+ adapter ??
47
+ createRnfsAdapter({
48
+ namespace: config.namespace,
49
+ });
50
+
51
+ const engine = new CacheEngine(config, resolvedAdapter);
52
+
53
+ if (autoInit) {
54
+ await engine.init();
55
+ }
56
+
57
+ return engine;
58
+ }
package/src/index.ts ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * @dynlabs/react-native-immutable-file-cache
3
+ *
4
+ * Cross-platform immutable file cache with pluggable storage adapters.
5
+ */
6
+
7
+ // ─────────────────────────────────────────────────────────────────
8
+ // Core Types
9
+ // ─────────────────────────────────────────────────────────────────
10
+
11
+ export type {
12
+ IStorageAdapter,
13
+ TAdapterPath,
14
+ TBinarySource,
15
+ IBinaryWriteResult,
16
+ IBinaryWriteOptions,
17
+ IFileStat,
18
+ TProgressCallback,
19
+ } from "./core/adapter";
20
+
21
+ export type {
22
+ ICacheConfig,
23
+ ICacheEntry,
24
+ ICacheEntryMeta,
25
+ ICacheIndex,
26
+ IPutOptions,
27
+ IPutResult,
28
+ IGetResult,
29
+ IListOptions,
30
+ IPruneResult,
31
+ ICacheStats,
32
+ TPutStatus,
33
+ TSortField,
34
+ TSortOrder,
35
+ } from "./core/types";
36
+
37
+ // ─────────────────────────────────────────────────────────────────
38
+ // Errors
39
+ // ─────────────────────────────────────────────────────────────────
40
+
41
+ export {
42
+ CacheError,
43
+ UnsupportedSourceError,
44
+ AdapterIOError,
45
+ CorruptIndexError,
46
+ ImmutableConflictError,
47
+ EntryNotFoundError,
48
+ } from "./core/errors";
49
+
50
+ // ─────────────────────────────────────────────────────────────────
51
+ // Core Engine
52
+ // ─────────────────────────────────────────────────────────────────
53
+
54
+ export { CacheEngine } from "./core/cacheEngine";
55
+
56
+ // ─────────────────────────────────────────────────────────────────
57
+ // Factory
58
+ // ─────────────────────────────────────────────────────────────────
59
+
60
+ export { createImmutableFileCache, createCacheEngineSync } from "./factory";
61
+
62
+ export type { ICreateCacheOptions } from "./factory";
63
+
64
+ // ─────────────────────────────────────────────────────────────────
65
+ // Adapters
66
+ // ─────────────────────────────────────────────────────────────────
67
+
68
+ export { createRnfsAdapter } from "./adapters/rnfsAdapter";
69
+ export type { IRnfsAdapterOptions } from "./adapters/rnfsAdapter";
70
+
71
+ export { createWebAdapter } from "./adapters/webAdapter";
72
+ export type { IWebAdapterOptions, IWebAdapterWithCleanup } from "./adapters/webAdapter";
73
+
74
+ export { createMemoryAdapter } from "./adapters/memoryAdapter";
75
+ export type { IMemoryAdapter } from "./adapters/memoryAdapter";
76
+
77
+ // ─────────────────────────────────────────────────────────────────
78
+ // Utilities
79
+ // ─────────────────────────────────────────────────────────────────
80
+
81
+ export { hash, hashSync } from "./core/hash";
82
+ export { Mutex, KeyedMutex } from "./core/mutex";
@@ -0,0 +1,59 @@
1
+ /**
2
+ * @dynlabs/react-native-immutable-file-cache - Web entrypoint
3
+ * Auto-selects Web adapter for browser environments.
4
+ */
5
+
6
+ // Re-export everything from main index
7
+ export * from "./index";
8
+
9
+ import { createWebAdapter } from "./adapters/webAdapter";
10
+ import type { ICreateCacheOptions } from "./factory";
11
+ import { CacheEngine } from "./core/cacheEngine";
12
+
13
+ /**
14
+ * Creates an immutable file cache instance for web browsers.
15
+ * Automatically uses the Web adapter (Cache Storage + IndexedDB).
16
+ *
17
+ * @param options - Cache configuration options
18
+ * @returns Initialized CacheEngine instance
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { createImmutableFileCache } from "@dynlabs/react-native-immutable-file-cache/web";
23
+ *
24
+ * const cache = await createImmutableFileCache({
25
+ * namespace: "images",
26
+ * defaultTtlMs: 7 * 24 * 60 * 60 * 1000,
27
+ * });
28
+ *
29
+ * // Cache an image
30
+ * const result = await cache.putFromUrl("avatar", "https://example.com/avatar.jpg");
31
+ *
32
+ * // Get cached image URI (blob: URL)
33
+ * const entry = await cache.get("avatar");
34
+ * if (entry) {
35
+ * // Use entry.uri as image source
36
+ * // Remember to revoke blob URL when done to free memory
37
+ * }
38
+ * ```
39
+ */
40
+ export async function createImmutableFileCache(
41
+ options?: Omit<ICreateCacheOptions, "adapter"> & { adapter?: ICreateCacheOptions["adapter"] }
42
+ ): Promise<CacheEngine> {
43
+ const { autoInit = true, adapter, ...config } = options ?? {};
44
+
45
+ // Use provided adapter or create Web adapter
46
+ const resolvedAdapter =
47
+ adapter ??
48
+ createWebAdapter({
49
+ namespace: config.namespace,
50
+ });
51
+
52
+ const engine = new CacheEngine(config, resolvedAdapter);
53
+
54
+ if (autoInit) {
55
+ await engine.init();
56
+ }
57
+
58
+ return engine;
59
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Type declarations for react-native-fs
3
+ * This is a minimal stub to satisfy TypeScript when the actual package is not installed.
4
+ */
5
+ declare module "react-native-fs" {
6
+ export const CachesDirectoryPath: string;
7
+ export const DocumentDirectoryPath: string;
8
+
9
+ export interface StatResult {
10
+ size: number;
11
+ mtime: string | number | Date;
12
+ ctime: string | number | Date;
13
+ isFile: () => boolean;
14
+ isDirectory: () => boolean;
15
+ }
16
+
17
+ export interface ReadDirItem {
18
+ name: string;
19
+ path: string;
20
+ size: number;
21
+ isFile: () => boolean;
22
+ isDirectory: () => boolean;
23
+ }
24
+
25
+ export interface DownloadFileOptions {
26
+ fromUrl: string;
27
+ toFile: string;
28
+ headers?: Record<string, string>;
29
+ progress?: (res: { contentLength: number; bytesWritten: number }) => void;
30
+ progressDivider?: number;
31
+ }
32
+
33
+ export interface DownloadResult {
34
+ jobId: number;
35
+ statusCode: number;
36
+ bytesWritten: number;
37
+ }
38
+
39
+ export interface DownloadBeginCallbackResult {
40
+ jobId: number;
41
+ contentLength: number;
42
+ headers: Record<string, string>;
43
+ }
44
+
45
+ export function mkdir(path: string): Promise<void>;
46
+ export function exists(path: string): Promise<boolean>;
47
+ export function unlink(path: string): Promise<void>;
48
+ export function readDir(path: string): Promise<ReadDirItem[]>;
49
+ export function readFile(path: string, encoding?: string): Promise<string>;
50
+ export function writeFile(path: string, content: string, encoding?: string): Promise<void>;
51
+ export function moveFile(from: string, to: string): Promise<void>;
52
+ export function copyFile(from: string, to: string): Promise<void>;
53
+ export function stat(path: string): Promise<StatResult>;
54
+ export function downloadFile(options: DownloadFileOptions): {
55
+ jobId: number;
56
+ promise: Promise<DownloadResult>;
57
+ };
58
+
59
+ const RNFS: {
60
+ CachesDirectoryPath: string;
61
+ DocumentDirectoryPath: string;
62
+ mkdir: typeof mkdir;
63
+ exists: typeof exists;
64
+ unlink: typeof unlink;
65
+ readDir: typeof readDir;
66
+ readFile: typeof readFile;
67
+ writeFile: typeof writeFile;
68
+ moveFile: typeof moveFile;
69
+ copyFile: typeof copyFile;
70
+ stat: typeof stat;
71
+ downloadFile: typeof downloadFile;
72
+ };
73
+
74
+ export default RNFS;
75
+ }