document-model 6.2.0-dev.1 → 6.2.0-dev.11
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/dist/node.d.mts +14 -16
- package/dist/node.d.mts.map +1 -1
- package/dist/node.mjs +16 -44
- package/dist/node.mjs.map +1 -1
- package/package.json +2 -3
package/dist/node.d.mts
CHANGED
|
@@ -1,19 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MinimalBackupData, PHBaseState, PHDocument, Reducer, ReplayDocumentOptions, VersionedReplayConfig } from "@powerhousedao/shared/document-model";
|
|
2
2
|
import { BinaryLike } from "node:crypto";
|
|
3
3
|
|
|
4
4
|
//#region node.d.mts
|
|
5
|
-
/**
|
|
6
|
-
* Reads an attachment from a file and returns its base64-encoded data and MIME type.
|
|
7
|
-
* @param path - The path of the attachment file to read.
|
|
8
|
-
* @returns A Promise that resolves to an object containing the base64-encoded data and MIME type of the attachment.
|
|
9
|
-
*/
|
|
10
|
-
declare function getLocalFile(path: string): Promise<AttachmentInput>;
|
|
11
|
-
/**
|
|
12
|
-
* Fetches an attachment from a URL and returns its base64-encoded data and MIME type.
|
|
13
|
-
* @param url - The URL of the attachment to fetch.
|
|
14
|
-
* @returns A Promise that resolves to an object containing the base64-encoded data and MIME type of the attachment.
|
|
15
|
-
*/
|
|
16
|
-
declare function getRemoteFile(url: string): Promise<AttachmentInput>;
|
|
17
5
|
declare function writeFileNode(path: string, name: string, data: Uint8Array): Promise<string>;
|
|
18
6
|
declare function readFileNode(path: string): NonSharedBuffer;
|
|
19
7
|
declare function fetchFileNode(url: string): Promise<{
|
|
@@ -38,6 +26,16 @@ declare const hashNode: (data: BinaryLike, algorithm?: string, encoding?: "base6
|
|
|
38
26
|
* @throws An error if the initial state or the operations history is not found in the ZIP file.
|
|
39
27
|
*/
|
|
40
28
|
declare function baseLoadFromFile<TState extends PHBaseState = PHBaseState>(path: string, reducer: Reducer<TState>, options?: ReplayDocumentOptions): Promise<PHDocument<TState>>;
|
|
29
|
+
/**
|
|
30
|
+
* Loads a version-aware document from a ZIP file.
|
|
31
|
+
*
|
|
32
|
+
* @typeParam TState - The type of the state object.
|
|
33
|
+
* @param path - The path to the ZIP file.
|
|
34
|
+
* @param config - Versioned replay config with per-version reducers and optional upgrade manifest.
|
|
35
|
+
* @param options - Optional replay options.
|
|
36
|
+
* @returns A promise that resolves to the document state after versioned replay.
|
|
37
|
+
*/
|
|
38
|
+
declare function baseLoadFromFileVersioned<TState extends PHBaseState = PHBaseState>(path: string, config: VersionedReplayConfig, options?: ReplayDocumentOptions): Promise<PHDocument<TState>>;
|
|
41
39
|
/**
|
|
42
40
|
* Saves a minimal document backup to a .phd file.
|
|
43
41
|
* Used when the full document is not available (e.g., in onOperations handler).
|
|
@@ -48,8 +46,8 @@ declare function baseMinimalSaveToFile(data: MinimalBackupData, path: string, ex
|
|
|
48
46
|
* Saves a document to a ZIP file.
|
|
49
47
|
*
|
|
50
48
|
* @remarks
|
|
51
|
-
* This function creates a ZIP file containing the document's state
|
|
52
|
-
*
|
|
49
|
+
* This function creates a ZIP file containing the document's state and
|
|
50
|
+
* operations. The file is saved to the specified path.
|
|
53
51
|
*
|
|
54
52
|
* @param document - The document to save to the file.
|
|
55
53
|
* @param path - The path to save the file to.
|
|
@@ -58,5 +56,5 @@ declare function baseMinimalSaveToFile(data: MinimalBackupData, path: string, ex
|
|
|
58
56
|
*/
|
|
59
57
|
declare function baseSaveToFile(document: PHDocument, path: string, extension: string, name?: string): Promise<string>;
|
|
60
58
|
//#endregion
|
|
61
|
-
export { baseLoadFromFile, baseMinimalSaveToFile, baseSaveToFile, fetchFileNode, getFileNode,
|
|
59
|
+
export { baseLoadFromFile, baseLoadFromFileVersioned, baseMinimalSaveToFile, baseSaveToFile, fetchFileNode, getFileNode, hashNode, readFileNode, writeFileNode };
|
|
62
60
|
//# sourceMappingURL=node.d.mts.map
|
package/dist/node.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.d.mts","names":[],"sources":["../node.mts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"node.d.mts","names":[],"sources":["../node.mts"],"mappings":";;;;iBAkBgB,aAAA,CACd,IAAA,UACA,IAAA,UACA,IAAA,EAAM,UAAA,GACL,OAAA;AAAA,iBAuBa,YAAA,CAAa,IAAA,WAAY,eAAA;AAAA,iBAIzB,aAAA,CACd,GAAA,WACC,OAAA;EAAU,MAAA,EAAQ,MAAA;EAAQ,QAAA;AAAA;AAAA,cAoBhB,WAAA,GAAe,IAAA,aAAY,OAAA,CAAA,eAAA;AAAA,cAI3B,QAAA,GACX,IAAA,EAAM,UAAA,EACN,SAAA,WACA,QAAA,qBACA,OAAA,GAAU,MAAA;;;;;;AAlCZ;;;;;AAIA;;;;;iBA8DsB,gBAAA,gBACL,WAAA,GAAc,WAAA,CAAA,CAE7B,IAAA,UACA,OAAA,EAAS,OAAA,CAAQ,MAAA,GACjB,OAAA,GAAU,qBAAA,GACT,OAAA,CAAQ,UAAA,CAAW,MAAA;;;;;;AA9CtB;;;;iBA4DsB,yBAAA,gBACL,WAAA,GAAc,WAAA,CAAA,CAE7B,IAAA,UACA,MAAA,EAAQ,qBAAA,EACR,OAAA,GAAU,qBAAA,GACT,OAAA,CAAQ,UAAA,CAAW,MAAA;;;;;AA9DtB;iBAwEsB,qBAAA,CACpB,IAAA,EAAM,iBAAA,EACN,IAAA,UACA,SAAA,WAAiB,OAAA;;;;;;;;;;;AAvCnB;;iBAiEsB,cAAA,CACpB,QAAA,EAAU,UAAA,EACV,IAAA,UACA,SAAA,UACA,IAAA,YAAa,OAAA"}
|
package/dist/node.mjs
CHANGED
|
@@ -1,49 +1,9 @@
|
|
|
1
|
-
import { baseLoadFromInput, createMinimalZip, createZip } from "@powerhousedao/shared/document-model";
|
|
2
|
-
import mime from "mime/lite";
|
|
1
|
+
import { baseLoadFromInput, baseLoadFromInputVersioned, createMinimalZip, createZip } from "@powerhousedao/shared/document-model";
|
|
3
2
|
import { createHash } from "node:crypto";
|
|
4
3
|
import fs from "node:fs";
|
|
5
4
|
import https from "node:https";
|
|
6
5
|
import { join } from "node:path";
|
|
7
6
|
//#region node.mts
|
|
8
|
-
function getFileAttributes(file) {
|
|
9
|
-
return {
|
|
10
|
-
extension: file.replace(/^.*\./, "") || void 0,
|
|
11
|
-
fileName: file.replace(/^.*[/\\]/, "") || void 0
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Reads an attachment from a file and returns its base64-encoded data and MIME type.
|
|
16
|
-
* @param path - The path of the attachment file to read.
|
|
17
|
-
* @returns A Promise that resolves to an object containing the base64-encoded data and MIME type of the attachment.
|
|
18
|
-
*/
|
|
19
|
-
async function getLocalFile(path) {
|
|
20
|
-
const buffer = await getFileNode(path);
|
|
21
|
-
const mimeType = mime.getType(path) || "application/octet-stream";
|
|
22
|
-
const attributes = getFileAttributes(path);
|
|
23
|
-
const data = buffer.toString("base64");
|
|
24
|
-
return {
|
|
25
|
-
data,
|
|
26
|
-
hash: hashNode(data),
|
|
27
|
-
mimeType,
|
|
28
|
-
...attributes
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Fetches an attachment from a URL and returns its base64-encoded data and MIME type.
|
|
33
|
-
* @param url - The URL of the attachment to fetch.
|
|
34
|
-
* @returns A Promise that resolves to an object containing the base64-encoded data and MIME type of the attachment.
|
|
35
|
-
*/
|
|
36
|
-
async function getRemoteFile(url) {
|
|
37
|
-
const { buffer, mimeType = "application/octet-stream" } = await fetchFileNode(url);
|
|
38
|
-
const attributes = getFileAttributes(url);
|
|
39
|
-
const data = buffer.toString("base64");
|
|
40
|
-
return {
|
|
41
|
-
data,
|
|
42
|
-
hash: hashNode(data),
|
|
43
|
-
mimeType,
|
|
44
|
-
...attributes
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
7
|
function writeFileNode(path, name, data) {
|
|
48
8
|
const filePath = join(path, name);
|
|
49
9
|
fs.mkdirSync(path, { recursive: true });
|
|
@@ -112,6 +72,18 @@ async function baseLoadFromFile(path, reducer, options) {
|
|
|
112
72
|
return baseLoadFromInput(readFileNode(path), reducer, options);
|
|
113
73
|
}
|
|
114
74
|
/**
|
|
75
|
+
* Loads a version-aware document from a ZIP file.
|
|
76
|
+
*
|
|
77
|
+
* @typeParam TState - The type of the state object.
|
|
78
|
+
* @param path - The path to the ZIP file.
|
|
79
|
+
* @param config - Versioned replay config with per-version reducers and optional upgrade manifest.
|
|
80
|
+
* @param options - Optional replay options.
|
|
81
|
+
* @returns A promise that resolves to the document state after versioned replay.
|
|
82
|
+
*/
|
|
83
|
+
async function baseLoadFromFileVersioned(path, config, options) {
|
|
84
|
+
return baseLoadFromInputVersioned(readFileNode(path), config, options);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
115
87
|
* Saves a minimal document backup to a .phd file.
|
|
116
88
|
* Used when the full document is not available (e.g., in onOperations handler).
|
|
117
89
|
* Creates a file with minimal header and empty operations.
|
|
@@ -125,8 +97,8 @@ async function baseMinimalSaveToFile(data, path, extension) {
|
|
|
125
97
|
* Saves a document to a ZIP file.
|
|
126
98
|
*
|
|
127
99
|
* @remarks
|
|
128
|
-
* This function creates a ZIP file containing the document's state
|
|
129
|
-
*
|
|
100
|
+
* This function creates a ZIP file containing the document's state and
|
|
101
|
+
* operations. The file is saved to the specified path.
|
|
130
102
|
*
|
|
131
103
|
* @param document - The document to save to the file.
|
|
132
104
|
* @param path - The path to save the file to.
|
|
@@ -140,6 +112,6 @@ async function baseSaveToFile(document, path, extension, name) {
|
|
|
140
112
|
return writeFileNode(path, fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`, file);
|
|
141
113
|
}
|
|
142
114
|
//#endregion
|
|
143
|
-
export { baseLoadFromFile, baseMinimalSaveToFile, baseSaveToFile, fetchFileNode, getFileNode,
|
|
115
|
+
export { baseLoadFromFile, baseLoadFromFileVersioned, baseMinimalSaveToFile, baseSaveToFile, fetchFileNode, getFileNode, hashNode, readFileNode, writeFileNode };
|
|
144
116
|
|
|
145
117
|
//# sourceMappingURL=node.mjs.map
|
package/dist/node.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.mjs","names":[],"sources":["../node.mts"],"sourcesContent":["import {\n baseLoadFromInput,\n
|
|
1
|
+
{"version":3,"file":"node.mjs","names":[],"sources":["../node.mts"],"sourcesContent":["import {\n baseLoadFromInput,\n baseLoadFromInputVersioned,\n createMinimalZip,\n createZip,\n type MinimalBackupData,\n type PHBaseState,\n type PHDocument,\n type Reducer,\n type ReplayDocumentOptions,\n type VersionedReplayConfig,\n} from \"@powerhousedao/shared/document-model\";\nimport type { BinaryLike } from \"node:crypto\";\nimport { createHash } from \"node:crypto\";\nimport fs from \"node:fs\";\nimport https from \"node:https\";\nimport { join } from \"node:path\";\n\nexport function writeFileNode(\n path: string,\n name: string,\n data: Uint8Array,\n): Promise<string> {\n const filePath = join(path, name);\n fs.mkdirSync(path, { recursive: true });\n\n return new Promise((resolve, reject) => {\n try {\n fs.writeFile(filePath, data, {}, (err) => {\n if (err) {\n reject(err);\n } else {\n resolve(filePath);\n }\n });\n } catch (error: unknown) {\n if (error instanceof Error) {\n reject(error);\n } else {\n reject(new Error(String(error)));\n }\n }\n });\n}\n\nexport function readFileNode(path: string) {\n return fs.readFileSync(path);\n}\n\nexport function fetchFileNode(\n url: string,\n): Promise<{ buffer: Buffer; mimeType?: string }> {\n return new Promise((resolve, reject) => {\n https\n .get(url, (resp) => {\n const data: Uint8Array[] = [];\n const mimeType = resp.headers[\"content-type\"];\n resp.on(\"data\", (chunk: Uint8Array) => {\n data.push(chunk);\n });\n\n resp.on(\"end\", () => {\n resolve({ buffer: Buffer.concat(data), mimeType });\n });\n })\n .on(\"error\", (err) => {\n reject(err);\n });\n });\n}\n\nexport const getFileNode = (file: string) => {\n return Promise.resolve(readFileNode(file));\n};\n\nexport const hashNode = (\n data: BinaryLike,\n algorithm = \"sha1\",\n encoding: \"base64\" | \"hex\" = \"base64\",\n _params?: Record<string, unknown>,\n) => {\n if (![\"sha1\", \"sha256\", \"sha512\"].includes(algorithm)) {\n throw new Error(\n `Hashing algorithm not supported: \"${algorithm}\". Available: sha1, sha256, sha512`,\n );\n }\n\n if (![\"base64\", \"hex\"].includes(encoding)) {\n throw new Error(\n `Hash encoding not supported: \"${encoding}\". Available: base64, hex`,\n );\n }\n\n return createHash(algorithm).update(data).digest(encoding);\n};\n\n/**\n * Loads a document from a ZIP file.\n *\n * @remarks\n * This function reads a ZIP file and returns the document state after\n * applying all the operations. The reducer is used to apply the operations.\n *\n * @typeParam S - The type of the state object.\n * @typeParam A - The type of the actions that can be applied to the state object.\n *\n * @param path - The path to the ZIP file.\n * @param reducer - The reducer to apply the operations to the state object.\n * @returns A promise that resolves to the document state after applying all the operations.\n * @throws An error if the initial state or the operations history is not found in the ZIP file.\n */\nexport async function baseLoadFromFile<\n TState extends PHBaseState = PHBaseState,\n>(\n path: string,\n reducer: Reducer<TState>,\n options?: ReplayDocumentOptions,\n): Promise<PHDocument<TState>> {\n const file = readFileNode(path);\n return baseLoadFromInput(file, reducer, options);\n}\n\n/**\n * Loads a version-aware document from a ZIP file.\n *\n * @typeParam TState - The type of the state object.\n * @param path - The path to the ZIP file.\n * @param config - Versioned replay config with per-version reducers and optional upgrade manifest.\n * @param options - Optional replay options.\n * @returns A promise that resolves to the document state after versioned replay.\n */\nexport async function baseLoadFromFileVersioned<\n TState extends PHBaseState = PHBaseState,\n>(\n path: string,\n config: VersionedReplayConfig,\n options?: ReplayDocumentOptions,\n): Promise<PHDocument<TState>> {\n const file = readFileNode(path);\n return baseLoadFromInputVersioned<TState>(file, config, options);\n}\n\n/**\n * Saves a minimal document backup to a .phd file.\n * Used when the full document is not available (e.g., in onOperations handler).\n * Creates a file with minimal header and empty operations.\n */\nexport async function baseMinimalSaveToFile(\n data: MinimalBackupData,\n path: string,\n extension: string,\n) {\n const file = await createMinimalZip(data);\n const fileExtension = extension ? `.${extension}.phd` : \".phd\";\n\n return writeFileNode(\n path,\n data.name.endsWith(fileExtension)\n ? data.name\n : `${data.name}${fileExtension}`,\n file,\n );\n}\n\n/**\n * Saves a document to a ZIP file.\n *\n * @remarks\n * This function creates a ZIP file containing the document's state and\n * operations. The file is saved to the specified path.\n *\n * @param document - The document to save to the file.\n * @param path - The path to save the file to.\n * @param extension - The extension to use for the file.\n * @returns A promise that resolves to the path of the saved file.\n */\nexport async function baseSaveToFile(\n document: PHDocument,\n path: string,\n extension: string,\n name?: string,\n) {\n const file = await createZip(document);\n const fileName = name ?? document.header.name;\n const fileExtension = extension ? `.${extension}.phd` : \".phd\";\n\n return writeFileNode(\n path,\n fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`,\n file,\n );\n}\n"],"mappings":";;;;;;AAkBA,SAAgB,cACd,MACA,MACA,MACiB;CACjB,MAAM,WAAW,KAAK,MAAM,KAAK;AACjC,IAAG,UAAU,MAAM,EAAE,WAAW,MAAM,CAAC;AAEvC,QAAO,IAAI,SAAS,SAAS,WAAW;AACtC,MAAI;AACF,MAAG,UAAU,UAAU,MAAM,EAAE,GAAG,QAAQ;AACxC,QAAI,IACF,QAAO,IAAI;QAEX,SAAQ,SAAS;KAEnB;WACK,OAAgB;AACvB,OAAI,iBAAiB,MACnB,QAAO,MAAM;OAEb,QAAO,IAAI,MAAM,OAAO,MAAM,CAAC,CAAC;;GAGpC;;AAGJ,SAAgB,aAAa,MAAc;AACzC,QAAO,GAAG,aAAa,KAAK;;AAG9B,SAAgB,cACd,KACgD;AAChD,QAAO,IAAI,SAAS,SAAS,WAAW;AACtC,QACG,IAAI,MAAM,SAAS;GAClB,MAAM,OAAqB,EAAE;GAC7B,MAAM,WAAW,KAAK,QAAQ;AAC9B,QAAK,GAAG,SAAS,UAAsB;AACrC,SAAK,KAAK,MAAM;KAChB;AAEF,QAAK,GAAG,aAAa;AACnB,YAAQ;KAAE,QAAQ,OAAO,OAAO,KAAK;KAA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",
|
|
3
|
-
"version": "6.2.0-dev.
|
|
3
|
+
"version": "6.2.0-dev.11",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"private": false,
|
|
6
6
|
"files": [
|
|
@@ -48,8 +48,7 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"jszip": "^3.10.1",
|
|
51
|
-
"
|
|
52
|
-
"@powerhousedao/shared": "6.2.0-dev.1"
|
|
51
|
+
"@powerhousedao/shared": "6.2.0-dev.11"
|
|
53
52
|
},
|
|
54
53
|
"scripts": {
|
|
55
54
|
"tsc": "tsc",
|