-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzip.ts
More file actions
73 lines (68 loc) · 1.94 KB
/
zip.ts
File metadata and controls
73 lines (68 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as z from "zod";
import * as fs from "node:fs/promises";
import { resource } from "@notation/core";
import { zip } from "src/utils/zip";
import { getSourceSha256 } from "src/utils/hash";
export type ZipSchema = {
Key: { sourceFilePath: string };
CreateParams: { sourceFilePath: string };
UpdateParams: { sourceFilePath: string };
ReadResult: void;
};
const zipResource = resource<ZipSchema>({
type: "std/fs/Zip",
});
export const zipSchema = zipResource.defineSchema({
sourceFilePath: {
valueType: z.string(),
propertyType: "param",
presence: "required",
primaryKey: true,
},
filePath: {
valueType: z.string(),
propertyType: "param",
presence: "required",
secondaryKey: true,
},
sourceSha256: {
valueType: z.string(),
propertyType: "param",
presence: "required",
},
file: {
valueType: z.instanceof(Buffer),
propertyType: "computed",
presence: "required",
hidden: true,
},
} as const);
export const Zip = zipSchema.defineOperations({
setIntrinsicConfig: async ({ config }) => {
const sourceSha256 = await getSourceSha256(config.sourceFilePath!);
const filePath = `${config.sourceFilePath}.zip`;
return { sourceSha256, filePath };
},
read: async (params) => {
try {
const file = await fs.readFile(params.filePath);
return { ...params, file };
} catch (error: any) {
if (error.code !== "ENOENT") throw error;
await zip.package(params.sourceFilePath, params.filePath);
const file = await fs.readFile(params.filePath);
return { ...params, file };
}
},
create: async (params) => {
await zip.package(params.sourceFilePath, params.filePath);
},
update: async (config) => {
await fs.unlink(config.filePath);
await zip.package(config.sourceFilePath, config.filePath);
},
delete: async (config) => {
await fs.unlink(config.filePath);
},
});
export type ZipFileInstance = InstanceType<typeof Zip>;