@johnhenry/packfile 0.0.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 +21 -0
- package/README.md +486 -0
- package/browser.mjs +93 -0
- package/cache.mjs +61 -0
- package/compat.mjs +25 -0
- package/index.mjs +7 -0
- package/lib/blob-preview.mjs +447 -0
- package/lib/compression.browser.mjs +13 -0
- package/lib/compression.mjs +16 -0
- package/lib/create-router.mjs +79 -0
- package/lib/from-archive.mjs +23 -0
- package/lib/from-directory-lazy.mjs +61 -0
- package/lib/from-directory.mjs +79 -0
- package/lib/hash.mjs +14 -0
- package/lib/lazy-file-map.mjs +62 -0
- package/lib/mime.mjs +74 -0
- package/lib/response.mjs +41 -0
- package/lib/safe-symlink.mjs +15 -0
- package/lib/to-archive.mjs +27 -0
- package/lib/web-bundle.mjs +195 -0
- package/package.json +68 -0
- package/packfile.mjs +89 -0
- package/types.d.ts +84 -0
- package/types.ts +65 -0
package/types.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FileEntry,
|
|
3
|
+
FilesMap,
|
|
4
|
+
FromDirectoryOptions,
|
|
5
|
+
FromDirectoryLazyOptions,
|
|
6
|
+
ToArchiveOptions,
|
|
7
|
+
FromArchiveOptions,
|
|
8
|
+
RouterOptions,
|
|
9
|
+
RouteHandler,
|
|
10
|
+
RouteContext,
|
|
11
|
+
FallbackHandler,
|
|
12
|
+
CompileOptions,
|
|
13
|
+
} from "./types";
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
FileEntry,
|
|
17
|
+
FilesMap,
|
|
18
|
+
FromDirectoryOptions,
|
|
19
|
+
FromDirectoryLazyOptions,
|
|
20
|
+
ToArchiveOptions,
|
|
21
|
+
FromArchiveOptions,
|
|
22
|
+
RouterOptions,
|
|
23
|
+
RouteHandler,
|
|
24
|
+
RouteContext,
|
|
25
|
+
FallbackHandler,
|
|
26
|
+
CompileOptions,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Note: `types.d.ts` describes the main `.` entry point (`index.mjs`).
|
|
30
|
+
// `withCache` (from the `@johnhenry/packfile/cache` subpath) is documented in the README
|
|
31
|
+
// but not re-declared here, since it is not part of `index.mjs`'s exports.
|
|
32
|
+
|
|
33
|
+
/** Reads all files from a directory into a `Map<string, FileEntry>`. */
|
|
34
|
+
export function fromDirectory(
|
|
35
|
+
directoryPath: string,
|
|
36
|
+
options?: FromDirectoryOptions
|
|
37
|
+
): Promise<Map<string, FileEntry>>;
|
|
38
|
+
|
|
39
|
+
/** Returns a `LazyFileMap` that reads files from disk on demand. */
|
|
40
|
+
export function fromDirectoryLazy(
|
|
41
|
+
directoryPath: string,
|
|
42
|
+
options?: FromDirectoryLazyOptions
|
|
43
|
+
): Promise<FilesMap>;
|
|
44
|
+
|
|
45
|
+
/** Serializes a file Map to a (by default gzip-compressed) CBOR archive buffer. */
|
|
46
|
+
export function toArchive(
|
|
47
|
+
map: Map<string, FileEntry>,
|
|
48
|
+
options?: ToArchiveOptions
|
|
49
|
+
): Promise<Buffer>;
|
|
50
|
+
|
|
51
|
+
/** Deserializes a CBOR archive back into a `Map<string, FileEntry>`. */
|
|
52
|
+
export function fromArchive(
|
|
53
|
+
buffer: Buffer | ArrayBuffer | Uint8Array,
|
|
54
|
+
options?: FromArchiveOptions
|
|
55
|
+
): Promise<Map<string, FileEntry>>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns a `(input, ctx?) => Promise<Response>` handler that serves files
|
|
59
|
+
* from a `Map`/`LazyFileMap`. `input` may be a path string or a `Request`.
|
|
60
|
+
* The returned function also has itself assigned to `.fetch`.
|
|
61
|
+
*/
|
|
62
|
+
export function createRouter(
|
|
63
|
+
files: FilesMap,
|
|
64
|
+
options?: RouterOptions
|
|
65
|
+
): RouteHandler;
|
|
66
|
+
|
|
67
|
+
/** SHA-256 hex digest of a Buffer/Uint8Array. */
|
|
68
|
+
export function hashBuffer(buffer: Buffer | Uint8Array): string;
|
|
69
|
+
|
|
70
|
+
/** SHA-256 hex digest of a Node.js Readable stream. */
|
|
71
|
+
export function hashStream(stream: NodeJS.ReadableStream): Promise<string>;
|
|
72
|
+
|
|
73
|
+
/** Convenience wrapper: `fromDirectory` -> `toArchive`. */
|
|
74
|
+
export function compileDirectory(
|
|
75
|
+
directoryPath: string,
|
|
76
|
+
options?: CompileOptions
|
|
77
|
+
): Promise<Buffer>;
|
|
78
|
+
|
|
79
|
+
/** Convenience wrapper: `fromArchive` -> write files to `outputPath`. */
|
|
80
|
+
export function decompileDirectory(
|
|
81
|
+
compiledData: Buffer | ArrayBuffer | Uint8Array,
|
|
82
|
+
outputPath: string,
|
|
83
|
+
compressed?: boolean
|
|
84
|
+
): Promise<void>;
|
package/types.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export type FileEntry = {
|
|
2
|
+
data: Uint8Array;
|
|
3
|
+
size: number;
|
|
4
|
+
hash: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/** A `Map`-like collection of file entries. `LazyFileMap.get()` may return a Promise. */
|
|
8
|
+
export interface FilesMap {
|
|
9
|
+
has(key: string): boolean;
|
|
10
|
+
get(key: string): FileEntry | undefined | Promise<FileEntry | undefined>;
|
|
11
|
+
keys(): IterableIterator<string>;
|
|
12
|
+
readonly size: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type FromDirectoryOptions = {
|
|
16
|
+
ignorePatterns?: string[];
|
|
17
|
+
maxFileSize?: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type FromDirectoryLazyOptions = {
|
|
21
|
+
ignorePatterns?: string[];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type ToArchiveOptions = {
|
|
25
|
+
compress?: boolean;
|
|
26
|
+
compressionLevel?: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type FromArchiveOptions = {
|
|
30
|
+
compressed?: boolean;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type RouteContext = unknown;
|
|
34
|
+
|
|
35
|
+
export type FallbackHandler = (
|
|
36
|
+
input: string | Request,
|
|
37
|
+
ctx?: RouteContext
|
|
38
|
+
) => Response | Promise<Response>;
|
|
39
|
+
|
|
40
|
+
export type RouterOptions = {
|
|
41
|
+
alias?: { [path: string]: string };
|
|
42
|
+
cacheControl?: string;
|
|
43
|
+
mimeTypes?: { [extension: string]: string };
|
|
44
|
+
tryExtensions?: string[];
|
|
45
|
+
fallback?: FallbackHandler;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type RouteHandler = ((
|
|
49
|
+
input: string | Request,
|
|
50
|
+
ctx?: RouteContext
|
|
51
|
+
) => Promise<Response>) & {
|
|
52
|
+
fetch: RouteHandler;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type CompileOptions = {
|
|
56
|
+
compress?: boolean;
|
|
57
|
+
compressionLevel?: number;
|
|
58
|
+
ignorePatterns?: string[];
|
|
59
|
+
maxFileSize?: number;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type CacheOptions = {
|
|
63
|
+
cacheControl?: string;
|
|
64
|
+
weak?: boolean;
|
|
65
|
+
};
|