@remix-run/file-storage 0.10.0 → 0.12.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 Michael Jackson
3
+ Copyright (c) 2025 Shopify Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,13 +1,17 @@
1
1
  # file-storage
2
2
 
3
- `file-storage` is a key/value interface for storing [`File` objects](https://developer.mozilla.org/en-US/docs/Web/API/File) in JavaScript. Similar to how `localStorage` allows you to store key/value pairs of strings in the browser, `file-storage` allows you to store key/value pairs of files on the server.
3
+ `file-storage` is a key/value interface for storing [`File` objects](https://developer.mozilla.org/en-US/docs/Web/API/File) in JavaScript.
4
+
5
+ Handling file uploads and storage is a common requirement in web applications, but each storage backend (local disk, AWS S3, Cloudflare R2, etc.) has its own API and conventions. This fragmentation makes it difficult to write portable code that can easily switch between storage providers or support multiple backends simultaneously.
6
+
7
+ Similar to how `localStorage` allows you to store key/value pairs of strings in the browser, `file-storage` allows you to store key/value pairs of files on the server with a consistent interface regardless of the underlying storage mechanism.
4
8
 
5
9
  ## Features
6
10
 
7
- - Simple, intuitive key/value API (like [Web Storage](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API), but for `File`s instead of strings)
8
- - A generic `FileStorage` interface that works for various large object storage backends (can be adapted to AWS S3, Cloudflare R2, etc.)
9
- - Support streaming file content to and from storage
10
- - Preserves all `File` metadata including `file.name`, `file.type`, and `file.lastModified`
11
+ - **Simple API** - Intuitive key/value API (like [Web Storage](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API), but for `File`s instead of strings)
12
+ - **Generic Interface** - `FileStorage` interface that works for various large object storage backends (can be adapted to AWS S3, Cloudflare R2, etc.)
13
+ - **Streaming Support** - Stream file content to and from storage
14
+ - **Metadata Preservation** - Preserves all `File` metadata including `file.name`, `file.type`, and `file.lastModified`
11
15
 
12
16
  ## Installation
13
17
 
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- //# sourceMappingURL=index.js.map
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,158 @@
1
+ import * as fs from 'node:fs';
2
+ import * as fsp from 'node:fs/promises';
3
+ import * as path from 'node:path';
4
+ import { openFile, writeFile } from '@remix-run/fs';
5
+ /**
6
+ * A `FileStorage` that is backed by a directory on the local filesystem.
7
+ *
8
+ * Important: No attempt is made to avoid overwriting existing files, so the directory used should
9
+ * be a new directory solely dedicated to this storage object.
10
+ *
11
+ * Note: Keys have no correlation to file names on disk, so they may be any string including
12
+ * characters that are not valid in file names. Additionally, individual `File` names have no
13
+ * correlation to names of files on disk, so multiple files with the same name may be stored in the
14
+ * same storage object.
15
+ */
16
+ export class LocalFileStorage {
17
+ #dirname;
18
+ /**
19
+ * @param directory The directory where files are stored
20
+ */
21
+ constructor(directory) {
22
+ this.#dirname = path.resolve(directory);
23
+ try {
24
+ let stats = fs.statSync(this.#dirname);
25
+ if (!stats.isDirectory()) {
26
+ throw new Error(`Path "${this.#dirname}" is not a directory`);
27
+ }
28
+ }
29
+ catch (error) {
30
+ if (!isNoEntityError(error)) {
31
+ throw error;
32
+ }
33
+ fs.mkdirSync(this.#dirname, { recursive: true });
34
+ }
35
+ }
36
+ async get(key) {
37
+ let { filePath, metaPath } = await this.#getPaths(key);
38
+ try {
39
+ let meta = await readMetadata(metaPath);
40
+ return openFile(filePath, {
41
+ lastModified: meta.lastModified,
42
+ name: meta.name,
43
+ type: meta.type,
44
+ });
45
+ }
46
+ catch (error) {
47
+ if (!isNoEntityError(error)) {
48
+ throw error;
49
+ }
50
+ return null;
51
+ }
52
+ }
53
+ async has(key) {
54
+ let { metaPath } = await this.#getPaths(key);
55
+ try {
56
+ await fsp.access(metaPath);
57
+ return true;
58
+ }
59
+ catch {
60
+ return false;
61
+ }
62
+ }
63
+ async list(options) {
64
+ let { cursor, includeMetadata = false, limit = 32, prefix } = options ?? {};
65
+ let files = [];
66
+ let foundCursor = cursor === undefined;
67
+ let nextCursor;
68
+ let lastHash;
69
+ outerLoop: for await (let subdir of await fsp.opendir(this.#dirname)) {
70
+ if (!subdir.isDirectory())
71
+ continue;
72
+ for await (let file of await fsp.opendir(path.join(this.#dirname, subdir.name))) {
73
+ if (!file.isFile() || !file.name.endsWith('.meta.json'))
74
+ continue;
75
+ let hash = file.name.slice(0, -10); // Remove ".meta.json"
76
+ if (foundCursor) {
77
+ let meta = await readMetadata(path.join(this.#dirname, subdir.name, file.name));
78
+ if (prefix != null && !meta.key.startsWith(prefix)) {
79
+ continue;
80
+ }
81
+ if (files.length >= limit) {
82
+ nextCursor = lastHash;
83
+ break outerLoop;
84
+ }
85
+ if (includeMetadata) {
86
+ let size = (await fsp.stat(path.join(this.#dirname, subdir.name, `${hash}.dat`))).size;
87
+ files.push({ ...meta, size });
88
+ }
89
+ else {
90
+ files.push({ key: meta.key });
91
+ }
92
+ }
93
+ else if (hash === cursor) {
94
+ foundCursor = true;
95
+ }
96
+ lastHash = hash;
97
+ }
98
+ }
99
+ return {
100
+ cursor: nextCursor,
101
+ files,
102
+ };
103
+ }
104
+ async put(key, file) {
105
+ await this.set(key, file);
106
+ return (await this.get(key));
107
+ }
108
+ async remove(key) {
109
+ let { directory, filePath, metaPath } = await this.#getPaths(key);
110
+ try {
111
+ await Promise.all([fsp.unlink(filePath), fsp.unlink(metaPath)]);
112
+ // Check if directory is empty and remove it if so
113
+ let files = await fsp.readdir(directory);
114
+ if (files.length === 0) {
115
+ await fsp.rmdir(directory);
116
+ }
117
+ }
118
+ catch (error) {
119
+ if (!isNoEntityError(error)) {
120
+ throw error;
121
+ }
122
+ }
123
+ }
124
+ async set(key, file) {
125
+ let { directory, filePath, metaPath } = await this.#getPaths(key);
126
+ // Ensure directory exists
127
+ await fsp.mkdir(directory, { recursive: true });
128
+ await writeFile(filePath, file);
129
+ let meta = {
130
+ key,
131
+ lastModified: file.lastModified,
132
+ name: file.name,
133
+ type: file.type,
134
+ };
135
+ await fsp.writeFile(metaPath, JSON.stringify(meta));
136
+ }
137
+ async #getPaths(key) {
138
+ let hash = await computeHash(key);
139
+ let directory = path.join(this.#dirname, hash.slice(0, 2));
140
+ return {
141
+ directory,
142
+ filePath: path.join(directory, `${hash}.dat`),
143
+ metaPath: path.join(directory, `${hash}.meta.json`),
144
+ };
145
+ }
146
+ }
147
+ async function readMetadata(metaPath) {
148
+ return JSON.parse(await fsp.readFile(metaPath, 'utf-8'));
149
+ }
150
+ async function computeHash(key, algorithm = 'SHA-256') {
151
+ let digest = await crypto.subtle.digest(algorithm, new TextEncoder().encode(key));
152
+ return Array.from(new Uint8Array(digest))
153
+ .map((b) => b.toString(16).padStart(2, '0'))
154
+ .join('');
155
+ }
156
+ function isNoEntityError(obj) {
157
+ return obj instanceof Error && 'code' in obj && obj.code === 'ENOENT';
158
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * A simple, in-memory implementation of the `FileStorage` interface.
3
+ */
4
+ export class MemoryFileStorage {
5
+ #map = new Map();
6
+ get(key) {
7
+ return this.#map.get(key) ?? null;
8
+ }
9
+ has(key) {
10
+ return this.#map.has(key);
11
+ }
12
+ list(options) {
13
+ let { cursor, includeMetadata = false, limit = Infinity, prefix } = options ?? {};
14
+ let files = [];
15
+ let foundCursor = cursor === undefined;
16
+ let nextCursor;
17
+ for (let [key, file] of this.#map.entries()) {
18
+ if (foundCursor) {
19
+ if (prefix != null && !key.startsWith(prefix)) {
20
+ continue;
21
+ }
22
+ if (files.length >= limit) {
23
+ nextCursor = files[files.length - 1]?.key;
24
+ break;
25
+ }
26
+ if (includeMetadata) {
27
+ files.push({
28
+ key,
29
+ lastModified: file.lastModified,
30
+ name: file.name,
31
+ size: file.size,
32
+ type: file.type,
33
+ });
34
+ }
35
+ else {
36
+ files.push({ key });
37
+ }
38
+ }
39
+ else if (key === cursor) {
40
+ foundCursor = true;
41
+ }
42
+ }
43
+ return {
44
+ cursor: nextCursor,
45
+ files,
46
+ };
47
+ }
48
+ async put(key, file) {
49
+ await this.set(key, file);
50
+ return this.get(key);
51
+ }
52
+ remove(key) {
53
+ this.#map.delete(key);
54
+ }
55
+ async set(key, file) {
56
+ let buffer = await file.arrayBuffer();
57
+ let newFile = new File([buffer], file.name, {
58
+ lastModified: file.lastModified,
59
+ type: file.type,
60
+ });
61
+ this.#map.set(key, newFile);
62
+ }
63
+ }