@functional-systems/memdrive 0.1.1-dev.4 → 0.1.1-dev.5
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/README.md +18 -0
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +5 -2
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/list.d.ts +5 -0
- package/dist/commonjs/list.d.ts.map +1 -0
- package/dist/commonjs/list.js +150 -0
- package/dist/commonjs/list.js.map +1 -0
- package/dist/commonjs/records.d.ts +2 -1
- package/dist/commonjs/records.d.ts.map +1 -1
- package/dist/commonjs/records.js +10 -3
- package/dist/commonjs/records.js.map +1 -1
- package/dist/commonjs/types.d.ts +24 -2
- package/dist/commonjs/types.d.ts.map +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/list.d.ts +5 -0
- package/dist/esm/list.d.ts.map +1 -0
- package/dist/esm/list.js +147 -0
- package/dist/esm/list.js.map +1 -0
- package/dist/esm/records.d.ts +2 -1
- package/dist/esm/records.d.ts.map +1 -1
- package/dist/esm/records.js +9 -3
- package/dist/esm/records.js.map +1 -1
- package/dist/esm/types.d.ts +24 -2
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +5 -2
- package/src/list.ts +152 -0
- package/src/records.ts +11 -4
- package/src/types.ts +27 -2
package/src/records.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MemDriveError, requireInput } from './errors.js';
|
|
2
2
|
import { isObject, keysOnly, metadataValues, validDate } from './config.js';
|
|
3
3
|
import { chunkSpans } from './chunking.js';
|
|
4
|
-
import type { CreateFileRequest, DriveFile, MetadataSchema } from './types.js';
|
|
4
|
+
import type { CreateFileRequest, DriveFile, FileListEntry, MetadataSchema } from './types.js';
|
|
5
5
|
|
|
6
6
|
const encoder = new TextEncoder();
|
|
7
7
|
export const bytes = (value: unknown) => encoder.encode(JSON.stringify(value)).length;
|
|
@@ -26,19 +26,26 @@ export function resultDocs(response: unknown, collection: string, maximum: numbe
|
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
export function
|
|
29
|
+
export function decodeFileEntry(doc: Record<string, unknown>, schema: MetadataSchema): FileListEntry {
|
|
30
30
|
if (doc.kind !== 'file' || !validFileId(doc.fileId) || doc.id !== originalId(doc.fileId)
|
|
31
|
-
|| !validPath(doc.path) ||
|
|
31
|
+
|| !validPath(doc.path) || !validDate(doc.modifiedAt))
|
|
32
32
|
throw new MemDriveError('INVALID_RESULT', 'Read returned an invalid file original.');
|
|
33
33
|
try {
|
|
34
34
|
const metadata = metadataValues(doc.metadata, schema);
|
|
35
|
-
return { fileId: doc.fileId, path: doc.path, modifiedAt: doc.modifiedAt,
|
|
35
|
+
return { fileId: doc.fileId, path: doc.path, modifiedAt: doc.modifiedAt,
|
|
36
36
|
...(metadata === undefined ? {} : { metadata }) };
|
|
37
37
|
} catch (cause) {
|
|
38
38
|
throw new MemDriveError('INVALID_RESULT', 'Read returned invalid file metadata.', { cause });
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
export function decodeFile(doc: Record<string, unknown>, schema: MetadataSchema): DriveFile {
|
|
43
|
+
if (typeof doc.body !== 'string' || /\p{Surrogate}/u.test(doc.body))
|
|
44
|
+
throw new MemDriveError('INVALID_RESULT', 'Read returned an invalid file original.');
|
|
45
|
+
const { metadata, ...entry } = decodeFileEntry(doc, schema);
|
|
46
|
+
return { ...entry, content: doc.body, ...(metadata === undefined ? {} : { metadata }) };
|
|
47
|
+
}
|
|
48
|
+
|
|
42
49
|
async function contentHash(content: string): Promise<string> {
|
|
43
50
|
const hash = new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', encoder.encode(content)));
|
|
44
51
|
return [...hash].map(byte => byte.toString(16).padStart(2, '0')).join('');
|
package/src/types.ts
CHANGED
|
@@ -13,14 +13,38 @@ export interface OpenFileStoreOptions extends ConnectionOptions {
|
|
|
13
13
|
at?: { fork: string } | undefined;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export interface
|
|
16
|
+
export interface FileListEntry {
|
|
17
17
|
fileId: string;
|
|
18
18
|
modifiedAt: string;
|
|
19
19
|
path: string;
|
|
20
|
-
content: string;
|
|
21
20
|
metadata?: Metadata | undefined;
|
|
22
21
|
}
|
|
23
22
|
|
|
23
|
+
export interface DriveFile extends FileListEntry {
|
|
24
|
+
content: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type ListFilesRequest = {
|
|
28
|
+
prefix?: string | undefined;
|
|
29
|
+
nameContains?: string | undefined;
|
|
30
|
+
delimiter?: '/' | undefined;
|
|
31
|
+
limit?: number | undefined;
|
|
32
|
+
cursor?: never;
|
|
33
|
+
} | {
|
|
34
|
+
cursor: string;
|
|
35
|
+
prefix?: never;
|
|
36
|
+
nameContains?: never;
|
|
37
|
+
delimiter?: never;
|
|
38
|
+
limit?: never;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export interface ListFilesResult {
|
|
42
|
+
files: FileListEntry[];
|
|
43
|
+
prefixes: string[];
|
|
44
|
+
consistency: 'eventual';
|
|
45
|
+
cursor?: string | undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
24
48
|
export type ReadFileRequest = { fileId: string; path?: never } | { path: string; fileId?: never };
|
|
25
49
|
export interface CreateFileRequest {
|
|
26
50
|
op: 'create';
|
|
@@ -31,6 +55,7 @@ export interface CreateFileRequest {
|
|
|
31
55
|
|
|
32
56
|
interface FileView {
|
|
33
57
|
readonly name: string;
|
|
58
|
+
list(request?: ListFilesRequest, options?: RequestOptions): Promise<ListFilesResult>;
|
|
34
59
|
describe(options?: RequestOptions): Promise<FileStoreDescription>;
|
|
35
60
|
search(request: SearchFilesRequest, options?: RequestOptions): Promise<SearchFilesResult>;
|
|
36
61
|
read(request: ReadFileRequest, options?: RequestOptions): Promise<DriveFile | null>;
|