document-model 6.0.0-dev.105 → 6.0.0-dev.106
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/index.d.ts +81 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +216 -0
- package/dist/index.js.map +1 -0
- package/dist/{src/core/node.d.ts → node.d.mts} +25 -23
- package/dist/node.d.mts.map +1 -0
- package/dist/node.mjs +151 -0
- package/dist/node.mjs.map +1 -0
- package/package.json +11 -15
- package/dist/src/core/actions.d.ts +0 -87
- package/dist/src/core/actions.d.ts.map +0 -1
- package/dist/src/core/actions.js +0 -187
- package/dist/src/core/actions.js.map +0 -1
- package/dist/src/core/controller.d.ts +0 -30
- package/dist/src/core/controller.d.ts.map +0 -1
- package/dist/src/core/controller.js +0 -60
- package/dist/src/core/controller.js.map +0 -1
- package/dist/src/core/crypto.d.ts +0 -8
- package/dist/src/core/crypto.d.ts.map +0 -1
- package/dist/src/core/crypto.js +0 -68
- package/dist/src/core/crypto.js.map +0 -1
- package/dist/src/core/documents.d.ts +0 -147
- package/dist/src/core/documents.d.ts.map +0 -1
- package/dist/src/core/documents.js +0 -845
- package/dist/src/core/documents.js.map +0 -1
- package/dist/src/core/errors.d.ts +0 -21
- package/dist/src/core/errors.d.ts.map +0 -1
- package/dist/src/core/errors.js +0 -46
- package/dist/src/core/errors.js.map +0 -1
- package/dist/src/core/files.d.ts +0 -27
- package/dist/src/core/files.d.ts.map +0 -1
- package/dist/src/core/files.js +0 -90
- package/dist/src/core/files.js.map +0 -1
- package/dist/src/core/header.d.ts +0 -63
- package/dist/src/core/header.d.ts.map +0 -1
- package/dist/src/core/header.js +0 -173
- package/dist/src/core/header.js.map +0 -1
- package/dist/src/core/index.d.ts +0 -16
- package/dist/src/core/index.d.ts.map +0 -1
- package/dist/src/core/index.js +0 -16
- package/dist/src/core/index.js.map +0 -1
- package/dist/src/core/logger-types.d.ts +0 -12
- package/dist/src/core/logger-types.d.ts.map +0 -1
- package/dist/src/core/logger-types.js +0 -2
- package/dist/src/core/logger-types.js.map +0 -1
- package/dist/src/core/logger.d.ts +0 -27
- package/dist/src/core/logger.d.ts.map +0 -1
- package/dist/src/core/logger.js +0 -127
- package/dist/src/core/logger.js.map +0 -1
- package/dist/src/core/node.d.ts.map +0 -1
- package/dist/src/core/node.js +0 -155
- package/dist/src/core/node.js.map +0 -1
- package/dist/src/core/operations.d.ts +0 -50
- package/dist/src/core/operations.d.ts.map +0 -1
- package/dist/src/core/operations.js +0 -126
- package/dist/src/core/operations.js.map +0 -1
- package/dist/src/core/ph-types.d.ts +0 -2
- package/dist/src/core/ph-types.d.ts.map +0 -1
- package/dist/src/core/ph-types.js +0 -2
- package/dist/src/core/ph-types.js.map +0 -1
- package/dist/src/core/reducer.d.ts +0 -63
- package/dist/src/core/reducer.d.ts.map +0 -1
- package/dist/src/core/reducer.js +0 -446
- package/dist/src/core/reducer.js.map +0 -1
- package/dist/src/core/schemas.d.ts +0 -75
- package/dist/src/core/schemas.d.ts.map +0 -1
- package/dist/src/core/schemas.js +0 -116
- package/dist/src/core/schemas.js.map +0 -1
- package/dist/src/core/state.d.ts +0 -26
- package/dist/src/core/state.d.ts.map +0 -1
- package/dist/src/core/state.js +0 -56
- package/dist/src/core/state.js.map +0 -1
- package/dist/src/core/types.d.ts +0 -2
- package/dist/src/core/types.d.ts.map +0 -1
- package/dist/src/core/types.js +0 -2
- package/dist/src/core/types.js.map +0 -1
- package/dist/src/core/utils.d.ts +0 -6
- package/dist/src/core/utils.d.ts.map +0 -1
- package/dist/src/core/utils.js +0 -15
- package/dist/src/core/utils.js.map +0 -1
- package/dist/src/core/validation.d.ts +0 -4
- package/dist/src/core/validation.d.ts.map +0 -1
- package/dist/src/core/validation.js +0 -27
- package/dist/src/core/validation.js.map +0 -1
- package/dist/src/document-model/actions.d.ts +0 -164
- package/dist/src/document-model/actions.d.ts.map +0 -1
- package/dist/src/document-model/actions.js +0 -111
- package/dist/src/document-model/actions.js.map +0 -1
- package/dist/src/document-model/constants.d.ts +0 -6
- package/dist/src/document-model/constants.d.ts.map +0 -1
- package/dist/src/document-model/constants.js +0 -584
- package/dist/src/document-model/constants.js.map +0 -1
- package/dist/src/document-model/controller.d.ts +0 -5
- package/dist/src/document-model/controller.d.ts.map +0 -1
- package/dist/src/document-model/controller.js +0 -5
- package/dist/src/document-model/controller.js.map +0 -1
- package/dist/src/document-model/document-schema.d.ts +0 -69
- package/dist/src/document-model/document-schema.d.ts.map +0 -1
- package/dist/src/document-model/document-schema.js +0 -43
- package/dist/src/document-model/document-schema.js.map +0 -1
- package/dist/src/document-model/document-type.d.ts +0 -2
- package/dist/src/document-model/document-type.d.ts.map +0 -1
- package/dist/src/document-model/document-type.js +0 -2
- package/dist/src/document-model/document-type.js.map +0 -1
- package/dist/src/document-model/files.d.ts +0 -5
- package/dist/src/document-model/files.d.ts.map +0 -1
- package/dist/src/document-model/files.js +0 -9
- package/dist/src/document-model/files.js.map +0 -1
- package/dist/src/document-model/index.d.ts +0 -12
- package/dist/src/document-model/index.d.ts.map +0 -1
- package/dist/src/document-model/index.js +0 -12
- package/dist/src/document-model/index.js.map +0 -1
- package/dist/src/document-model/module.d.ts +0 -3
- package/dist/src/document-model/module.d.ts.map +0 -1
- package/dist/src/document-model/module.js +0 -24
- package/dist/src/document-model/module.js.map +0 -1
- package/dist/src/document-model/reducers.d.ts +0 -11
- package/dist/src/document-model/reducers.d.ts.map +0 -1
- package/dist/src/document-model/reducers.js +0 -618
- package/dist/src/document-model/reducers.js.map +0 -1
- package/dist/src/document-model/schemas.d.ts +0 -189
- package/dist/src/document-model/schemas.d.ts.map +0 -1
- package/dist/src/document-model/schemas.js +0 -388
- package/dist/src/document-model/schemas.js.map +0 -1
- package/dist/src/document-model/state.d.ts +0 -15
- package/dist/src/document-model/state.d.ts.map +0 -1
- package/dist/src/document-model/state.js +0 -75
- package/dist/src/document-model/state.js.map +0 -1
- package/dist/src/document-model/types.d.ts +0 -584
- package/dist/src/document-model/types.d.ts.map +0 -1
- package/dist/src/document-model/types.js +0 -2
- package/dist/src/document-model/types.js.map +0 -1
- package/dist/src/document-model/validation.d.ts +0 -32
- package/dist/src/document-model/validation.d.ts.map +0 -1
- package/dist/src/document-model/validation.js +0 -166
- package/dist/src/document-model/validation.js.map +0 -1
- package/dist/src/index.d.ts +0 -6
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -3
- package/dist/src/index.js.map +0 -1
- package/dist/test/document/crypto.test.d.ts +0 -2
- package/dist/test/document/crypto.test.d.ts.map +0 -1
- package/dist/test/document/crypto.test.js +0 -192
- package/dist/test/document/crypto.test.js.map +0 -1
- package/dist/test/document/event-vs-command.test.d.ts +0 -2
- package/dist/test/document/event-vs-command.test.d.ts.map +0 -1
- package/dist/test/document/event-vs-command.test.js +0 -202
- package/dist/test/document/event-vs-command.test.js.map +0 -1
- package/dist/test/document/local.test.d.ts +0 -2
- package/dist/test/document/local.test.d.ts.map +0 -1
- package/dist/test/document/local.test.js +0 -226
- package/dist/test/document/local.test.js.map +0 -1
- package/dist/test/document/operation-id.test.d.ts +0 -2
- package/dist/test/document/operation-id.test.d.ts.map +0 -1
- package/dist/test/document/operation-id.test.js +0 -118
- package/dist/test/document/operation-id.test.js.map +0 -1
- package/dist/test/document/prune.test.d.ts +0 -2
- package/dist/test/document/prune.test.d.ts.map +0 -1
- package/dist/test/document/prune.test.js +0 -159
- package/dist/test/document/prune.test.js.map +0 -1
- package/dist/test/document/reducer.test.d.ts +0 -2
- package/dist/test/document/reducer.test.d.ts.map +0 -1
- package/dist/test/document/reducer.test.js +0 -284
- package/dist/test/document/reducer.test.js.map +0 -1
- package/dist/test/document/skip-operations.test.d.ts +0 -2
- package/dist/test/document/skip-operations.test.d.ts.map +0 -1
- package/dist/test/document/skip-operations.test.js +0 -495
- package/dist/test/document/skip-operations.test.js.map +0 -1
- package/dist/test/document/undo-redo-v2.test.d.ts +0 -2
- package/dist/test/document/undo-redo-v2.test.d.ts.map +0 -1
- package/dist/test/document/undo-redo-v2.test.js +0 -207
- package/dist/test/document/undo-redo-v2.test.js.map +0 -1
- package/dist/test/document/undo-redo.test.d.ts +0 -2
- package/dist/test/document/undo-redo.test.d.ts.map +0 -1
- package/dist/test/document/undo-redo.test.js +0 -413
- package/dist/test/document/undo-redo.test.js.map +0 -1
- package/dist/test/document/utils.test.d.ts +0 -2
- package/dist/test/document/utils.test.d.ts.map +0 -1
- package/dist/test/document/utils.test.js +0 -172
- package/dist/test/document/utils.test.js.map +0 -1
- package/dist/test/document-helpers/addUndo.test.d.ts +0 -2
- package/dist/test/document-helpers/addUndo.test.d.ts.map +0 -1
- package/dist/test/document-helpers/addUndo.test.js +0 -120
- package/dist/test/document-helpers/addUndo.test.js.map +0 -1
- package/dist/test/document-helpers/attachBranch.test.d.ts +0 -2
- package/dist/test/document-helpers/attachBranch.test.d.ts.map +0 -1
- package/dist/test/document-helpers/attachBranch.test.js +0 -364
- package/dist/test/document-helpers/attachBranch.test.js.map +0 -1
- package/dist/test/document-helpers/checkCleanedOperationsIntegrity.test.d.ts +0 -2
- package/dist/test/document-helpers/checkCleanedOperationsIntegrity.test.d.ts.map +0 -1
- package/dist/test/document-helpers/checkCleanedOperationsIntegrity.test.js +0 -252
- package/dist/test/document-helpers/checkCleanedOperationsIntegrity.test.js.map +0 -1
- package/dist/test/document-helpers/conflictResolution.test.d.ts +0 -2
- package/dist/test/document-helpers/conflictResolution.test.d.ts.map +0 -1
- package/dist/test/document-helpers/conflictResolution.test.js +0 -109
- package/dist/test/document-helpers/conflictResolution.test.js.map +0 -1
- package/dist/test/document-helpers/filterDuplicatedOperations.test.d.ts +0 -2
- package/dist/test/document-helpers/filterDuplicatedOperations.test.d.ts.map +0 -1
- package/dist/test/document-helpers/filterDuplicatedOperations.test.js +0 -126
- package/dist/test/document-helpers/filterDuplicatedOperations.test.js.map +0 -1
- package/dist/test/document-helpers/garbageCollect.test.d.ts +0 -2
- package/dist/test/document-helpers/garbageCollect.test.d.ts.map +0 -1
- package/dist/test/document-helpers/garbageCollect.test.js +0 -136
- package/dist/test/document-helpers/garbageCollect.test.js.map +0 -1
- package/dist/test/document-helpers/groupOperationsByScope.test.d.ts +0 -2
- package/dist/test/document-helpers/groupOperationsByScope.test.d.ts.map +0 -1
- package/dist/test/document-helpers/groupOperationsByScope.test.js +0 -102
- package/dist/test/document-helpers/groupOperationsByScope.test.js.map +0 -1
- package/dist/test/document-helpers/headerRevision.test.d.ts +0 -2
- package/dist/test/document-helpers/headerRevision.test.d.ts.map +0 -1
- package/dist/test/document-helpers/headerRevision.test.js +0 -203
- package/dist/test/document-helpers/headerRevision.test.js.map +0 -1
- package/dist/test/document-helpers/merge.test.d.ts +0 -2
- package/dist/test/document-helpers/merge.test.d.ts.map +0 -1
- package/dist/test/document-helpers/merge.test.js +0 -906
- package/dist/test/document-helpers/merge.test.js.map +0 -1
- package/dist/test/document-helpers/nextSkipNumber.test.d.ts +0 -2
- package/dist/test/document-helpers/nextSkipNumber.test.d.ts.map +0 -1
- package/dist/test/document-helpers/nextSkipNumber.test.js +0 -135
- package/dist/test/document-helpers/nextSkipNumber.test.js.map +0 -1
- package/dist/test/document-helpers/prepareOperations.test.d.ts +0 -2
- package/dist/test/document-helpers/prepareOperations.test.d.ts.map +0 -1
- package/dist/test/document-helpers/prepareOperations.test.js +0 -304
- package/dist/test/document-helpers/prepareOperations.test.js.map +0 -1
- package/dist/test/document-helpers/removeExistingOperations.test.d.ts +0 -2
- package/dist/test/document-helpers/removeExistingOperations.test.d.ts.map +0 -1
- package/dist/test/document-helpers/removeExistingOperations.test.js +0 -177
- package/dist/test/document-helpers/removeExistingOperations.test.js.map +0 -1
- package/dist/test/document-helpers/reshuffleByTimestamp.test.d.ts +0 -2
- package/dist/test/document-helpers/reshuffleByTimestamp.test.d.ts.map +0 -1
- package/dist/test/document-helpers/reshuffleByTimestamp.test.js +0 -148
- package/dist/test/document-helpers/reshuffleByTimestamp.test.js.map +0 -1
- package/dist/test/document-helpers/reshuffleByTimestampAndIndex.test.d.ts +0 -2
- package/dist/test/document-helpers/reshuffleByTimestampAndIndex.test.d.ts.map +0 -1
- package/dist/test/document-helpers/reshuffleByTimestampAndIndex.test.js +0 -200
- package/dist/test/document-helpers/reshuffleByTimestampAndIndex.test.js.map +0 -1
- package/dist/test/document-helpers/skipHeaderOperations.test.d.ts +0 -2
- package/dist/test/document-helpers/skipHeaderOperations.test.d.ts.map +0 -1
- package/dist/test/document-helpers/skipHeaderOperations.test.js +0 -72
- package/dist/test/document-helpers/skipHeaderOperations.test.js.map +0 -1
- package/dist/test/document-helpers/sortOperations.test.d.ts +0 -2
- package/dist/test/document-helpers/sortOperations.test.d.ts.map +0 -1
- package/dist/test/document-helpers/sortOperations.test.js +0 -101
- package/dist/test/document-helpers/sortOperations.test.js.map +0 -1
- package/dist/test/document-helpers/split.test.d.ts +0 -2
- package/dist/test/document-helpers/split.test.d.ts.map +0 -1
- package/dist/test/document-helpers/split.test.js +0 -121
- package/dist/test/document-helpers/split.test.js.map +0 -1
- package/dist/test/document-helpers/utils.d.ts +0 -15
- package/dist/test/document-helpers/utils.d.ts.map +0 -1
- package/dist/test/document-helpers/utils.js +0 -61
- package/dist/test/document-helpers/utils.js.map +0 -1
- package/dist/test/document-model/replay.test.d.ts +0 -2
- package/dist/test/document-model/replay.test.d.ts.map +0 -1
- package/dist/test/document-model/replay.test.js +0 -139
- package/dist/test/document-model/replay.test.js.map +0 -1
- package/dist/test/document-model/skip-operations.test.d.ts +0 -2
- package/dist/test/document-model/skip-operations.test.d.ts.map +0 -1
- package/dist/test/document-model/skip-operations.test.js +0 -214
- package/dist/test/document-model/skip-operations.test.js.map +0 -1
- package/dist/test/document-model/validation.test.d.ts +0 -2
- package/dist/test/document-model/validation.test.d.ts.map +0 -1
- package/dist/test/document-model/validation.test.js +0 -451
- package/dist/test/document-model/validation.test.js.map +0 -1
- package/dist/test/document-model/versioning.test.d.ts +0 -2
- package/dist/test/document-model/versioning.test.d.ts.map +0 -1
- package/dist/test/document-model/versioning.test.js +0 -93
- package/dist/test/document-model/versioning.test.js.map +0 -1
- package/dist/test/document-model/zip.test.d.ts +0 -2
- package/dist/test/document-model/zip.test.d.ts.map +0 -1
- package/dist/test/document-model/zip.test.js +0 -79
- package/dist/test/document-model/zip.test.js.map +0 -1
- package/dist/test/helpers.d.ts +0 -79
- package/dist/test/helpers.d.ts.map +0 -1
- package/dist/test/helpers.js +0 -121
- package/dist/test/helpers.js.map +0 -1
- package/dist/test/index.d.ts +0 -4
- package/dist/test/index.d.ts.map +0 -1
- package/dist/test/index.js +0 -3
- package/dist/test/index.js.map +0 -1
- package/dist/test/types.d.ts +0 -6
- package/dist/test/types.d.ts.map +0 -1
- package/dist/test/types.js +0 -2
- package/dist/test/types.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/vitest.config.d.ts +0 -3
- package/dist/vitest.config.d.ts.map +0 -1
- package/dist/vitest.config.js +0 -7
- package/dist/vitest.config.js.map +0 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Action, AssertIsDocumentOfType, AssertIsStateOfType, CreateState, DocumentAction, DocumentModelAction, DocumentModelDocument, DocumentModelDocumentModelModule, DocumentModelModule, DocumentModelPHState, DocumentOperations, IsDocumentOfType, IsStateOfType, Operation, PHBaseState, PHDocument, PHDocumentHeader } from "@powerhousedao/shared/document-model";
|
|
2
|
+
export * from "@powerhousedao/shared/document-model";
|
|
3
|
+
|
|
4
|
+
//#region src/controller.d.ts
|
|
5
|
+
type ScreamingSnakeToCamel<S extends string> = S extends `${infer Head}_${infer Tail}` ? `${Lowercase<Head>}${Capitalize<ScreamingSnakeToCamel<Tail>>}` : Lowercase<S>;
|
|
6
|
+
type DocumentChangeEvent<TState extends PHBaseState = PHBaseState> = {
|
|
7
|
+
document: PHDocument<TState>;
|
|
8
|
+
};
|
|
9
|
+
type DocumentChangeListener<TState extends PHBaseState = PHBaseState> = (event: DocumentChangeEvent<TState>) => void;
|
|
10
|
+
type ActionMap<TAction extends Action, TReturn = void> = { [K in (TAction | DocumentAction)["type"] as ScreamingSnakeToCamel<K>]: (input: Extract<TAction | DocumentAction, {
|
|
11
|
+
type: K;
|
|
12
|
+
}>["input"]) => TReturn & ActionMap<TAction, TReturn> };
|
|
13
|
+
declare class PHDocumentController<TState extends PHBaseState> implements PHDocument<TState> {
|
|
14
|
+
readonly module: DocumentModelModule<TState>;
|
|
15
|
+
protected _document: PHDocument<TState>;
|
|
16
|
+
private listeners;
|
|
17
|
+
constructor(module: DocumentModelModule<TState>, initialDocument?: PHDocument<TState>);
|
|
18
|
+
get document(): PHDocument<TState>;
|
|
19
|
+
get header(): PHDocumentHeader;
|
|
20
|
+
get state(): TState;
|
|
21
|
+
get initialState(): TState;
|
|
22
|
+
get operations(): DocumentOperations;
|
|
23
|
+
get clipboard(): Operation[];
|
|
24
|
+
onChange(listener: DocumentChangeListener<TState>): () => void;
|
|
25
|
+
private notifyListeners;
|
|
26
|
+
static forDocumentModel<TState extends PHBaseState, TAction extends Action>(module: DocumentModelModule<TState>): new (initialDocument?: PHDocument<TState>) => PHDocumentController<TState> & ActionMap<TAction, PHDocumentController<TState>>;
|
|
27
|
+
}
|
|
28
|
+
declare const DocumentModelController: new (initialDocument?: PHDocument<DocumentModelPHState> | undefined) => PHDocumentController<DocumentModelPHState> & ActionMap<DocumentModelAction, PHDocumentController<DocumentModelPHState>>;
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/logger-types.d.ts
|
|
31
|
+
type LoggerErrorHandler = (...data: any[]) => void;
|
|
32
|
+
type ILogger = {
|
|
33
|
+
level: "verbose" | "debug" | "info" | "warn" | "error";
|
|
34
|
+
verbose: (message: string, ...replacements: any[]) => void;
|
|
35
|
+
debug: (message: string, ...replacements: any[]) => void;
|
|
36
|
+
info: (message: string, ...replacements: any[]) => void;
|
|
37
|
+
warn: (message: string, ...replacements: any[]) => void;
|
|
38
|
+
error: (message: string, ...replacements: any[]) => void;
|
|
39
|
+
errorHandler: LoggerErrorHandler;
|
|
40
|
+
child: (tags: string[]) => ILogger;
|
|
41
|
+
};
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/logger.d.ts
|
|
44
|
+
declare const LOG_LEVELS: {
|
|
45
|
+
readonly verbose: 0;
|
|
46
|
+
readonly debug: 1;
|
|
47
|
+
readonly info: 2;
|
|
48
|
+
readonly warn: 3;
|
|
49
|
+
readonly error: 4;
|
|
50
|
+
};
|
|
51
|
+
declare class ConsoleLogger implements ILogger {
|
|
52
|
+
#private;
|
|
53
|
+
errorHandler: LoggerErrorHandler;
|
|
54
|
+
constructor(tags?: string[], handler?: LoggerErrorHandler);
|
|
55
|
+
get level(): keyof typeof LOG_LEVELS;
|
|
56
|
+
set level(value: keyof typeof LOG_LEVELS);
|
|
57
|
+
child(tags: string[]): ILogger;
|
|
58
|
+
verbose(message: string, ...replacements: any[]): void;
|
|
59
|
+
debug(message: string, ...replacements: any[]): void;
|
|
60
|
+
info(message: string, ...replacements: any[]): void;
|
|
61
|
+
warn(message: string, ...replacements: any[]): void;
|
|
62
|
+
error(message: string, ...replacements: any[]): void;
|
|
63
|
+
}
|
|
64
|
+
declare const logger: ILogger;
|
|
65
|
+
declare const setLogLevel: (level: ILogger["level"]) => void;
|
|
66
|
+
declare const setErrorHandler: (handler: LoggerErrorHandler) => void;
|
|
67
|
+
declare const childLogger: (tags: string[]) => ILogger;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/module.d.ts
|
|
70
|
+
declare const documentModelDocumentModelModule: DocumentModelDocumentModelModule;
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/state.d.ts
|
|
73
|
+
declare const isStateOfType: IsStateOfType<DocumentModelPHState>;
|
|
74
|
+
declare const assertIsStateOfType: AssertIsStateOfType<DocumentModelPHState>;
|
|
75
|
+
declare const isDocumentOfType: IsDocumentOfType<DocumentModelPHState>;
|
|
76
|
+
declare const assertIsDocumentOfType: AssertIsDocumentOfType<DocumentModelPHState>;
|
|
77
|
+
declare const documentModelCreateState: CreateState<DocumentModelPHState>;
|
|
78
|
+
declare function documentModelCreateDocument(state?: Partial<DocumentModelPHState>): DocumentModelDocument;
|
|
79
|
+
//#endregion
|
|
80
|
+
export { ActionMap, ConsoleLogger, DocumentChangeEvent, DocumentChangeListener, DocumentModelController, ILogger, LoggerErrorHandler, PHDocumentController, assertIsDocumentOfType, assertIsStateOfType, childLogger, documentModelCreateDocument, documentModelCreateState, documentModelDocumentModelModule, isDocumentOfType, isStateOfType, logger, setErrorHandler, setLogLevel };
|
|
81
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/controller.ts","../src/logger-types.ts","../src/logger.ts","../src/module.ts","../src/state.ts"],"mappings":";;;;KAcK,qBAAA,qBACH,CAAA,4CACO,SAAA,CAAU,IAAA,IAAQ,UAAA,CAAW,qBAAA,CAAsB,IAAA,OACtD,SAAA,CAAU,CAAA;AAAA,KAEJ,mBAAA,gBAAmC,WAAA,GAAc,WAAA;EAC3D,QAAA,EAAU,UAAA,CAAW,MAAA;AAAA;AAAA,KAGX,sBAAA,gBAAsC,WAAA,GAAc,WAAA,KAC9D,KAAA,EAAO,mBAAA,CAAoB,MAAA;AAAA,KAGjB,SAAA,iBAA0B,MAAA,6BAC7B,OAAA,GAAU,cAAA,aAA2B,qBAAA,CAAsB,CAAA,KAChE,KAAA,EAAO,OAAA,CAAQ,OAAA,GAAU,cAAA;EAAkB,IAAA,EAAM,CAAA;AAAA,gBAC9C,OAAA,GAAU,SAAA,CAAU,OAAA,EAAS,OAAA;AAAA,cAGvB,oBAAA,gBACI,WAAA,aACJ,UAAA,CAAW,MAAA;EAAA,SACb,MAAA,EAAQ,mBAAA,CAAoB,MAAA;EAAA,UAC3B,SAAA,EAAW,UAAA,CAAW,MAAA;EAAA,QACxB,SAAA;cAGN,MAAA,EAAQ,mBAAA,CAAoB,MAAA,GAC5B,eAA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,KAAA,CAAM,IAAA,aAAiB,OAAA;EASvB,OAAA,CAAQ,OAAA,aAAoB,YAAA;EAY5B,KAAA,CAAM,OAAA,aAAoB,YAAA;EAY1B,IAAA,CAAK,OAAA,aAAoB,YAAA;EAYzB,IAAA,CAAK,OAAA,aAAoB,YAAA;EAYzB,KAAA,CAAM,OAAA,aAAoB,YAAA;AAAA;AAAA,cAcf,MAAA,EAAQ,OAAA;AAAA,cAGR,WAAA,GAAe,KAAA,EAAO,OAAA;AAAA,cAItB,eAAA,GAAmB,OAAA,EAAS,kBAAA;AAAA,cAK5B,WAAA,GAAe,IAAA,eAAiB,OAAA;;;cCpJhC,gCAAA,EAAkC,gCAAA;;;cCVlC,aAAA,EAAe,aAAA,CAAc,oBAAA;AAAA,cAI7B,mBAAA,EAAqB,mBAAA,CAAoB,oBAAA;AAAA,cAMzC,gBAAA,EAAkB,gBAAA,CAAiB,oBAAA;AAAA,cAMnC,sBAAA,EAAwB,sBAAA,CACnC,oBAAA;AAAA,cAIW,wBAAA,EAA0B,WAAA,CAAY,oBAAA;AAAA,iBAanC,2BAAA,CACd,KAAA,GAAQ,OAAA,CAAQ,oBAAA,IACf,qBAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { actions, assertIsDocumentModelDocument, assertIsDocumentModelState, baseCreateDocument, createState, defaultBaseState, documentModelDocumentType, documentModelFileExtension, documentModelGlobalState, documentModelInitialGlobalState, documentModelInitialLocalState, documentModelLoadFromInput, documentModelReducer, documentModelSaveToFileHandle, isDocumentModelDocument, isDocumentModelState } from "@powerhousedao/shared/document-model";
|
|
2
|
+
export * from "@powerhousedao/shared/document-model";
|
|
3
|
+
//#region src/state.ts
|
|
4
|
+
const isStateOfType = (state) => {
|
|
5
|
+
return isDocumentModelState(state);
|
|
6
|
+
};
|
|
7
|
+
const assertIsStateOfType = (state) => {
|
|
8
|
+
assertIsDocumentModelState(state);
|
|
9
|
+
};
|
|
10
|
+
const isDocumentOfType = (document) => {
|
|
11
|
+
return isDocumentModelDocument(document);
|
|
12
|
+
};
|
|
13
|
+
const assertIsDocumentOfType = (document) => {
|
|
14
|
+
assertIsDocumentModelDocument(document);
|
|
15
|
+
};
|
|
16
|
+
const documentModelCreateState = (state) => {
|
|
17
|
+
return {
|
|
18
|
+
...defaultBaseState(),
|
|
19
|
+
global: {
|
|
20
|
+
...documentModelInitialGlobalState,
|
|
21
|
+
...state?.global ?? {}
|
|
22
|
+
},
|
|
23
|
+
local: {
|
|
24
|
+
...documentModelInitialLocalState,
|
|
25
|
+
...state?.local ?? {}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
function documentModelCreateDocument(state) {
|
|
30
|
+
const document = baseCreateDocument(documentModelCreateState, state);
|
|
31
|
+
document.header.documentType = documentModelDocumentType;
|
|
32
|
+
return document;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/module.ts
|
|
36
|
+
const utils = {
|
|
37
|
+
fileExtension: documentModelFileExtension,
|
|
38
|
+
createState: documentModelCreateState,
|
|
39
|
+
createDocument: documentModelCreateDocument,
|
|
40
|
+
loadFromInput: documentModelLoadFromInput,
|
|
41
|
+
saveToFileHandle: documentModelSaveToFileHandle,
|
|
42
|
+
isStateOfType,
|
|
43
|
+
assertIsStateOfType,
|
|
44
|
+
isDocumentOfType,
|
|
45
|
+
assertIsDocumentOfType
|
|
46
|
+
};
|
|
47
|
+
const documentModelDocumentModelModule = {
|
|
48
|
+
reducer: documentModelReducer,
|
|
49
|
+
documentModel: createState(defaultBaseState(), documentModelGlobalState),
|
|
50
|
+
actions,
|
|
51
|
+
utils
|
|
52
|
+
};
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/controller.ts
|
|
55
|
+
var PHDocumentController = class PHDocumentController {
|
|
56
|
+
module;
|
|
57
|
+
_document;
|
|
58
|
+
listeners = [];
|
|
59
|
+
constructor(module, initialDocument) {
|
|
60
|
+
this.module = module;
|
|
61
|
+
this._document = initialDocument ?? module.utils.createDocument();
|
|
62
|
+
for (const actionType in this.module.actions) Object.defineProperty(this, actionType, { value: (input) => {
|
|
63
|
+
const action = this.module.actions[actionType](input);
|
|
64
|
+
this._document = this.module.reducer(this._document, action);
|
|
65
|
+
this.notifyListeners();
|
|
66
|
+
return this;
|
|
67
|
+
} });
|
|
68
|
+
}
|
|
69
|
+
get document() {
|
|
70
|
+
return this._document;
|
|
71
|
+
}
|
|
72
|
+
get header() {
|
|
73
|
+
return this.document.header;
|
|
74
|
+
}
|
|
75
|
+
get state() {
|
|
76
|
+
return this.document.state;
|
|
77
|
+
}
|
|
78
|
+
get initialState() {
|
|
79
|
+
return this.document.initialState;
|
|
80
|
+
}
|
|
81
|
+
get operations() {
|
|
82
|
+
return this.document.operations;
|
|
83
|
+
}
|
|
84
|
+
get clipboard() {
|
|
85
|
+
return this.document.clipboard;
|
|
86
|
+
}
|
|
87
|
+
onChange(listener) {
|
|
88
|
+
this.listeners.push(listener);
|
|
89
|
+
return () => {
|
|
90
|
+
this.listeners = this.listeners.filter((l) => l !== listener);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
notifyListeners() {
|
|
94
|
+
if (this.listeners.length === 0) return;
|
|
95
|
+
const event = { document: this._document };
|
|
96
|
+
for (const listener of this.listeners) listener(event);
|
|
97
|
+
}
|
|
98
|
+
static forDocumentModel(module) {
|
|
99
|
+
return class extends PHDocumentController {
|
|
100
|
+
constructor(initialDocument) {
|
|
101
|
+
super(module, initialDocument);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const DocumentModelController = PHDocumentController.forDocumentModel(documentModelDocumentModelModule);
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/logger.ts
|
|
109
|
+
const tokenSub = /@([a-zA-Z0-9_]+)/g;
|
|
110
|
+
const dtf = new Intl.DateTimeFormat(void 0, {
|
|
111
|
+
hour12: false,
|
|
112
|
+
hour: "2-digit",
|
|
113
|
+
minute: "2-digit",
|
|
114
|
+
second: "2-digit",
|
|
115
|
+
fractionalSecondDigits: 2
|
|
116
|
+
});
|
|
117
|
+
const formatMessage = (tagString, message, replacements) => {
|
|
118
|
+
const meta = {};
|
|
119
|
+
const uniqueTokens = [];
|
|
120
|
+
let results;
|
|
121
|
+
while ((results = tokenSub.exec(message)) !== null) {
|
|
122
|
+
const tokenName = results[1];
|
|
123
|
+
if (uniqueTokens.indexOf(tokenName) === -1) {
|
|
124
|
+
uniqueTokens.push(tokenName);
|
|
125
|
+
meta[tokenName] = replacements[uniqueTokens.length - 1];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
for (const [key, value] of Object.entries(meta)) {
|
|
129
|
+
let stringValue;
|
|
130
|
+
if (!value) stringValue = "null";
|
|
131
|
+
else if (typeof value === "string") stringValue = value;
|
|
132
|
+
else if (typeof value === "object") stringValue = JSON.stringify(value);
|
|
133
|
+
else if (typeof value === "function") {
|
|
134
|
+
const name = value.name;
|
|
135
|
+
stringValue = name ? `${name}()` : "anonymous()";
|
|
136
|
+
} else stringValue = String(value);
|
|
137
|
+
message = message.replaceAll(`@${key}`, stringValue);
|
|
138
|
+
}
|
|
139
|
+
if (tagString.length > 0) message = `${tagString} ${message}`;
|
|
140
|
+
const now = /* @__PURE__ */ new Date();
|
|
141
|
+
meta["timestamp"] = dtf.format(now);
|
|
142
|
+
return [message, meta];
|
|
143
|
+
};
|
|
144
|
+
const LOG_LEVELS = {
|
|
145
|
+
verbose: 0,
|
|
146
|
+
debug: 1,
|
|
147
|
+
info: 2,
|
|
148
|
+
warn: 3,
|
|
149
|
+
error: 4
|
|
150
|
+
};
|
|
151
|
+
var ConsoleLogger = class ConsoleLogger {
|
|
152
|
+
#tags;
|
|
153
|
+
#tagString;
|
|
154
|
+
#level = LOG_LEVELS.info;
|
|
155
|
+
errorHandler;
|
|
156
|
+
constructor(tags, handler) {
|
|
157
|
+
this.#tags = tags || [];
|
|
158
|
+
this.#tagString = this.#tags.map((tag) => `[${tag}]`).join("");
|
|
159
|
+
this.errorHandler = handler ?? (() => {});
|
|
160
|
+
}
|
|
161
|
+
get level() {
|
|
162
|
+
return Object.keys(LOG_LEVELS).find((key) => LOG_LEVELS[key] === this.#level);
|
|
163
|
+
}
|
|
164
|
+
set level(value) {
|
|
165
|
+
this.#level = LOG_LEVELS[value];
|
|
166
|
+
}
|
|
167
|
+
child(tags) {
|
|
168
|
+
const logger = new ConsoleLogger([...this.#tags, ...tags], this.errorHandler);
|
|
169
|
+
logger.level = this.level;
|
|
170
|
+
return logger;
|
|
171
|
+
}
|
|
172
|
+
verbose(message, ...replacements) {
|
|
173
|
+
if (this.#level <= LOG_LEVELS.verbose) {
|
|
174
|
+
const [formattedMessage, meta] = formatMessage(this.#tagString, message, replacements);
|
|
175
|
+
console.debug(`[${meta["timestamp"]}] ${formattedMessage}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
debug(message, ...replacements) {
|
|
179
|
+
if (this.#level <= LOG_LEVELS.debug) {
|
|
180
|
+
const [formattedMessage, meta] = formatMessage(this.#tagString, message, replacements);
|
|
181
|
+
console.debug(`[${meta["timestamp"]}] ${formattedMessage}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
info(message, ...replacements) {
|
|
185
|
+
if (this.#level <= LOG_LEVELS.info) {
|
|
186
|
+
const [formattedMessage, meta] = formatMessage(this.#tagString, message, replacements);
|
|
187
|
+
console.info(`[${meta["timestamp"]}] ${formattedMessage}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
warn(message, ...replacements) {
|
|
191
|
+
if (this.#level <= LOG_LEVELS.warn) {
|
|
192
|
+
const [formattedMessage, meta] = formatMessage(this.#tagString, message, replacements);
|
|
193
|
+
console.warn(`[${meta["timestamp"]}] ${formattedMessage}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
error(message, ...replacements) {
|
|
197
|
+
if (this.#level <= LOG_LEVELS.error) {
|
|
198
|
+
const [formattedMessage, meta] = formatMessage(this.#tagString, message, replacements);
|
|
199
|
+
console.error(`[${meta["timestamp"]}] ${formattedMessage}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const logger = new ConsoleLogger();
|
|
204
|
+
const setLogLevel = (level) => {
|
|
205
|
+
logger.level = level;
|
|
206
|
+
};
|
|
207
|
+
const setErrorHandler = (handler) => {
|
|
208
|
+
logger.errorHandler = handler;
|
|
209
|
+
};
|
|
210
|
+
const childLogger = (tags) => {
|
|
211
|
+
return logger.child(tags);
|
|
212
|
+
};
|
|
213
|
+
//#endregion
|
|
214
|
+
export { ConsoleLogger, DocumentModelController, PHDocumentController, assertIsDocumentOfType, assertIsStateOfType, childLogger, documentModelCreateDocument, documentModelCreateState, documentModelDocumentModelModule, isDocumentOfType, isStateOfType, logger, setErrorHandler, setLogLevel };
|
|
215
|
+
|
|
216
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["#tags","#tagString","#level"],"sources":["../src/state.ts","../src/module.ts","../src/controller.ts","../src/logger.ts"],"sourcesContent":["import type {\n AssertIsDocumentOfType,\n AssertIsStateOfType,\n CreateState,\n DocumentModelDocument,\n DocumentModelGlobalState,\n DocumentModelPHState,\n IsDocumentOfType,\n IsStateOfType,\n} from \"@powerhousedao/shared/document-model\";\nimport {\n assertIsDocumentModelDocument,\n assertIsDocumentModelState,\n baseCreateDocument,\n defaultBaseState,\n documentModelDocumentType,\n documentModelInitialGlobalState,\n documentModelInitialLocalState,\n isDocumentModelDocument,\n isDocumentModelState,\n} from \"@powerhousedao/shared/document-model\";\n\nexport const isStateOfType: IsStateOfType<DocumentModelPHState> = (state) => {\n return isDocumentModelState(state);\n};\n\nexport const assertIsStateOfType: AssertIsStateOfType<DocumentModelPHState> = (\n state,\n) => {\n assertIsDocumentModelState(state);\n};\n\nexport const isDocumentOfType: IsDocumentOfType<DocumentModelPHState> = (\n document,\n) => {\n return isDocumentModelDocument(document);\n};\n\nexport const assertIsDocumentOfType: AssertIsDocumentOfType<\n DocumentModelPHState\n> = (document) => {\n assertIsDocumentModelDocument(document);\n};\nexport const documentModelCreateState: CreateState<DocumentModelPHState> = (\n state,\n) => {\n return {\n ...defaultBaseState(),\n global: {\n ...documentModelInitialGlobalState,\n ...((state?.global ?? {}) as DocumentModelGlobalState),\n },\n local: { ...documentModelInitialLocalState, ...(state?.local ?? {}) },\n };\n};\n\nexport function documentModelCreateDocument(\n state?: Partial<DocumentModelPHState>,\n): DocumentModelDocument {\n const document = baseCreateDocument(documentModelCreateState, state);\n document.header.documentType = documentModelDocumentType;\n\n return document;\n}\n","import type { DocumentModelDocumentModelModule } from \"@powerhousedao/shared/document-model\";\nimport {\n actions,\n createState,\n defaultBaseState,\n documentModelFileExtension,\n documentModelGlobalState,\n documentModelLoadFromInput,\n documentModelReducer,\n documentModelSaveToFileHandle,\n} from \"@powerhousedao/shared/document-model\";\nimport {\n assertIsDocumentOfType,\n assertIsStateOfType,\n documentModelCreateDocument,\n documentModelCreateState,\n isDocumentOfType,\n isStateOfType,\n} from \"./state.js\";\n\nconst utils = {\n fileExtension: documentModelFileExtension,\n createState: documentModelCreateState,\n createDocument: documentModelCreateDocument,\n loadFromInput: documentModelLoadFromInput,\n saveToFileHandle: documentModelSaveToFileHandle,\n isStateOfType,\n assertIsStateOfType,\n isDocumentOfType,\n assertIsDocumentOfType,\n};\n\nexport const documentModelDocumentModelModule: DocumentModelDocumentModelModule =\n {\n reducer: documentModelReducer,\n documentModel: createState(defaultBaseState(), documentModelGlobalState),\n actions,\n utils,\n };\n","import {\n type Action,\n type DocumentAction,\n type DocumentModelAction,\n type DocumentModelModule,\n type DocumentModelPHState,\n type DocumentOperations,\n type Operation,\n type PHBaseState,\n type PHDocument,\n type PHDocumentHeader,\n} from \"@powerhousedao/shared/document-model\";\nimport { documentModelDocumentModelModule } from \"./module.js\";\n\ntype ScreamingSnakeToCamel<S extends string> =\n S extends `${infer Head}_${infer Tail}`\n ? `${Lowercase<Head>}${Capitalize<ScreamingSnakeToCamel<Tail>>}`\n : Lowercase<S>;\n\nexport type DocumentChangeEvent<TState extends PHBaseState = PHBaseState> = {\n document: PHDocument<TState>;\n};\n\nexport type DocumentChangeListener<TState extends PHBaseState = PHBaseState> = (\n event: DocumentChangeEvent<TState>,\n) => void;\n\nexport type ActionMap<TAction extends Action, TReturn = void> = {\n [K in (TAction | DocumentAction)[\"type\"] as ScreamingSnakeToCamel<K>]: (\n input: Extract<TAction | DocumentAction, { type: K }>[\"input\"],\n ) => TReturn & ActionMap<TAction, TReturn>;\n};\n\nexport class PHDocumentController<\n TState extends PHBaseState,\n> implements PHDocument<TState> {\n readonly module: DocumentModelModule<TState>;\n protected _document: PHDocument<TState>;\n private listeners: DocumentChangeListener<TState>[] = [];\n\n constructor(\n module: DocumentModelModule<TState>,\n initialDocument?: PHDocument<TState>,\n ) {\n this.module = module;\n this._document = initialDocument ?? module.utils.createDocument();\n\n // dynamically add action methods to the controller\n for (const actionType in this.module.actions) {\n Object.defineProperty(this, actionType, {\n value: (input: unknown) => {\n const action = this.module.actions[actionType](input);\n this._document = this.module.reducer(this._document, action);\n this.notifyListeners();\n return this;\n },\n });\n }\n }\n\n get document(): PHDocument<TState> {\n return this._document;\n }\n\n get header(): PHDocumentHeader {\n return this.document.header;\n }\n get state(): TState {\n return this.document.state;\n }\n get initialState(): TState {\n return this.document.initialState;\n }\n get operations(): DocumentOperations {\n return this.document.operations;\n }\n get clipboard(): Operation[] {\n return this.document.clipboard;\n }\n\n onChange(listener: DocumentChangeListener<TState>): () => void {\n this.listeners.push(listener);\n return () => {\n this.listeners = this.listeners.filter((l) => l !== listener);\n };\n }\n\n private notifyListeners(): void {\n if (this.listeners.length === 0) return;\n const event: DocumentChangeEvent<TState> = { document: this._document };\n for (const listener of this.listeners) {\n listener(event);\n }\n }\n\n static forDocumentModel<TState extends PHBaseState, TAction extends Action>(\n module: DocumentModelModule<TState>,\n ) {\n return class extends PHDocumentController<TState> {\n constructor(initialDocument?: PHDocument<TState>) {\n super(module, initialDocument);\n }\n } as unknown as new (\n initialDocument?: PHDocument<TState>,\n ) => PHDocumentController<TState> &\n ActionMap<TAction, PHDocumentController<TState>>;\n }\n}\n\nexport const DocumentModelController = PHDocumentController.forDocumentModel<\n DocumentModelPHState,\n DocumentModelAction\n>(documentModelDocumentModelModule);\n","import type { ILogger, LoggerErrorHandler } from \"./logger-types.js\";\n\nconst tokenSub = /@([a-zA-Z0-9_]+)/g;\nconst dtf = new Intl.DateTimeFormat(undefined, {\n hour12: false,\n hour: \"2-digit\",\n minute: \"2-digit\",\n second: \"2-digit\",\n fractionalSecondDigits: 2,\n});\n\nconst formatMessage = (\n tagString: string,\n message: string,\n replacements: any[],\n): [string, Record<string, any>] => {\n const meta: Record<string, any> = {};\n const uniqueTokens: string[] = [];\n\n let results;\n while ((results = tokenSub.exec(message)) !== null) {\n const tokenName = results[1];\n const index = uniqueTokens.indexOf(tokenName);\n if (index === -1) {\n uniqueTokens.push(tokenName);\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n const replacement = replacements[uniqueTokens.length - 1];\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n meta[tokenName] = replacement;\n }\n }\n\n // replace\n for (const [key, value] of Object.entries(meta)) {\n let stringValue;\n if (!value) {\n stringValue = \"null\";\n } else if (typeof value === \"string\") {\n stringValue = value;\n } else if (typeof value === \"object\") {\n stringValue = JSON.stringify(value);\n } else if (typeof value === \"function\") {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n const name = (value as Function).name;\n stringValue = name ? `${name}()` : \"anonymous()\";\n } else {\n stringValue = String(value);\n }\n\n message = message.replaceAll(`@${key}`, stringValue);\n }\n\n if (tagString.length > 0) {\n message = `${tagString} ${message}`;\n }\n\n // timestamp\n const now = new Date();\n const timestamp = dtf.format(now);\n meta[\"timestamp\"] = timestamp;\n\n return [message, meta];\n};\n\nconst LOG_LEVELS = {\n verbose: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n} as const;\n\nexport class ConsoleLogger implements ILogger {\n #tags: string[];\n #tagString: string;\n #level: number = LOG_LEVELS.info;\n\n errorHandler: LoggerErrorHandler;\n\n constructor(tags?: string[], handler?: LoggerErrorHandler) {\n this.#tags = tags || [];\n this.#tagString = this.#tags.map((tag) => `[${tag}]`).join(\"\");\n\n this.errorHandler = handler ?? (() => {});\n }\n\n get level(): keyof typeof LOG_LEVELS {\n return Object.keys(LOG_LEVELS).find(\n (key) => LOG_LEVELS[key as keyof typeof LOG_LEVELS] === this.#level,\n ) as keyof typeof LOG_LEVELS;\n }\n\n set level(value: keyof typeof LOG_LEVELS) {\n this.#level = LOG_LEVELS[value];\n }\n\n child(tags: string[]): ILogger {\n const logger = new ConsoleLogger(\n [...this.#tags, ...tags],\n this.errorHandler,\n );\n logger.level = this.level;\n return logger;\n }\n\n verbose(message: string, ...replacements: any[]): void {\n if (this.#level <= LOG_LEVELS.verbose) {\n const [formattedMessage, meta] = formatMessage(\n this.#tagString,\n message,\n replacements,\n );\n\n console.debug(`[${meta[\"timestamp\"]}] ${formattedMessage}`);\n }\n }\n\n debug(message: string, ...replacements: any[]): void {\n if (this.#level <= LOG_LEVELS.debug) {\n const [formattedMessage, meta] = formatMessage(\n this.#tagString,\n message,\n replacements,\n );\n\n console.debug(`[${meta[\"timestamp\"]}] ${formattedMessage}`);\n }\n }\n\n info(message: string, ...replacements: any[]): void {\n if (this.#level <= LOG_LEVELS.info) {\n const [formattedMessage, meta] = formatMessage(\n this.#tagString,\n message,\n replacements,\n );\n\n console.info(`[${meta[\"timestamp\"]}] ${formattedMessage}`);\n }\n }\n\n warn(message: string, ...replacements: any[]): void {\n if (this.#level <= LOG_LEVELS.warn) {\n const [formattedMessage, meta] = formatMessage(\n this.#tagString,\n message,\n replacements,\n );\n\n console.warn(`[${meta[\"timestamp\"]}] ${formattedMessage}`);\n }\n }\n\n error(message: string, ...replacements: any[]): void {\n if (this.#level <= LOG_LEVELS.error) {\n const [formattedMessage, meta] = formatMessage(\n this.#tagString,\n message,\n replacements,\n );\n\n console.error(`[${meta[\"timestamp\"]}] ${formattedMessage}`);\n }\n }\n}\n\n// Singleton instance\nexport const logger: ILogger = new ConsoleLogger();\n\n// Global configuration\nexport const setLogLevel = (level: ILogger[\"level\"]) => {\n logger.level = level;\n};\n\nexport const setErrorHandler = (handler: LoggerErrorHandler) => {\n logger.errorHandler = handler;\n};\n\n// Backwards-compatible factory function\nexport const childLogger = (tags: string[]): ILogger => {\n return logger.child(tags);\n};\n"],"mappings":";;;AAsBA,MAAa,iBAAsD,UAAU;AAC3E,QAAO,qBAAqB,MAAM;;AAGpC,MAAa,uBACX,UACG;AACH,4BAA2B,MAAM;;AAGnC,MAAa,oBACX,aACG;AACH,QAAO,wBAAwB,SAAS;;AAG1C,MAAa,0BAER,aAAa;AAChB,+BAA8B,SAAS;;AAEzC,MAAa,4BACX,UACG;AACH,QAAO;EACL,GAAG,kBAAkB;EACrB,QAAQ;GACN,GAAG;GACH,GAAK,OAAO,UAAU,EAAE;GACzB;EACD,OAAO;GAAE,GAAG;GAAgC,GAAI,OAAO,SAAS,EAAE;GAAG;EACtE;;AAGH,SAAgB,4BACd,OACuB;CACvB,MAAM,WAAW,mBAAmB,0BAA0B,MAAM;AACpE,UAAS,OAAO,eAAe;AAE/B,QAAO;;;;AC1CT,MAAM,QAAQ;CACZ,eAAe;CACf,aAAa;CACb,gBAAgB;CAChB,eAAe;CACf,kBAAkB;CAClB;CACA;CACA;CACA;CACD;AAED,MAAa,mCACX;CACE,SAAS;CACT,eAAe,YAAY,kBAAkB,EAAE,yBAAyB;CACxE;CACA;CACD;;;ACLH,IAAa,uBAAb,MAAa,qBAEmB;CAC9B;CACA;CACA,YAAsD,EAAE;CAExD,YACE,QACA,iBACA;AACA,OAAK,SAAS;AACd,OAAK,YAAY,mBAAmB,OAAO,MAAM,gBAAgB;AAGjE,OAAK,MAAM,cA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/shared/document-model";
|
|
2
|
+
import { BinaryLike } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
//#region node.d.mts
|
|
4
5
|
/**
|
|
5
6
|
* Reads an attachment from a file and returns its base64-encoded data and MIME type.
|
|
6
7
|
* @param path - The path of the attachment file to read.
|
|
7
8
|
* @returns A Promise that resolves to an object containing the base64-encoded data and MIME type of the attachment.
|
|
8
9
|
*/
|
|
9
|
-
|
|
10
|
+
declare function getLocalFile(path: string): Promise<AttachmentInput>;
|
|
10
11
|
/**
|
|
11
12
|
* Fetches an attachment from a URL and returns its base64-encoded data and MIME type.
|
|
12
13
|
* @param url - The URL of the attachment to fetch.
|
|
13
14
|
* @returns A Promise that resolves to an object containing the base64-encoded data and MIME type of the attachment.
|
|
14
15
|
*/
|
|
15
|
-
|
|
16
|
+
declare function getRemoteFile(url: string): Promise<AttachmentInput>;
|
|
17
|
+
declare function writeFileNode(path: string, name: string, data: Uint8Array): Promise<string>;
|
|
18
|
+
declare function readFileNode(path: string): NonSharedBuffer;
|
|
19
|
+
declare function fetchFileNode(url: string): Promise<{
|
|
20
|
+
buffer: Buffer;
|
|
21
|
+
mimeType?: string;
|
|
22
|
+
}>;
|
|
23
|
+
declare const getFileNode: (file: string) => Promise<NonSharedBuffer>;
|
|
24
|
+
declare const hashNode: (data: BinaryLike, algorithm?: string, encoding?: "base64" | "hex", _params?: Record<string, unknown>) => string;
|
|
16
25
|
/**
|
|
17
26
|
* Loads a document from a ZIP file.
|
|
18
27
|
*
|
|
@@ -28,7 +37,13 @@ export declare function getRemoteFile(url: string): Promise<AttachmentInput>;
|
|
|
28
37
|
* @returns A promise that resolves to the document state after applying all the operations.
|
|
29
38
|
* @throws An error if the initial state or the operations history is not found in the ZIP file.
|
|
30
39
|
*/
|
|
31
|
-
|
|
40
|
+
declare function baseLoadFromFile<TState extends PHBaseState = PHBaseState>(path: string, reducer: Reducer<TState>, options?: ReplayDocumentOptions): Promise<PHDocument<TState>>;
|
|
41
|
+
/**
|
|
42
|
+
* Saves a minimal document backup to a .phd file.
|
|
43
|
+
* Used when the full document is not available (e.g., in onOperations handler).
|
|
44
|
+
* Creates a file with minimal header and empty operations.
|
|
45
|
+
*/
|
|
46
|
+
declare function baseMinimalSaveToFile(data: MinimalBackupData, path: string, extension: string): Promise<string>;
|
|
32
47
|
/**
|
|
33
48
|
* Saves a document to a ZIP file.
|
|
34
49
|
*
|
|
@@ -41,20 +56,7 @@ export declare function baseLoadFromFile<TState extends PHBaseState = PHBaseStat
|
|
|
41
56
|
* @param extension - The extension to use for the file.
|
|
42
57
|
* @returns A promise that resolves to the path of the saved file.
|
|
43
58
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* Creates a file with minimal header and empty operations.
|
|
49
|
-
*/
|
|
50
|
-
export declare function baseMinimalSaveToFile(data: MinimalBackupData, path: string, extension: string): Promise<string>;
|
|
51
|
-
export type { MinimalBackupData };
|
|
52
|
-
export declare function writeFileNode(path: string, name: string, data: Uint8Array): Promise<string>;
|
|
53
|
-
export declare function readFileNode(path: string): NonSharedBuffer;
|
|
54
|
-
export declare function fetchFileNode(url: string): Promise<{
|
|
55
|
-
buffer: Buffer;
|
|
56
|
-
mimeType?: string;
|
|
57
|
-
}>;
|
|
58
|
-
export declare const getFileNode: (file: string) => Promise<NonSharedBuffer>;
|
|
59
|
-
export declare const hashNode: (data: BinaryLike, algorithm?: string, encoding?: "base64" | "hex", _params?: Record<string, unknown>) => string;
|
|
60
|
-
//# sourceMappingURL=node.d.ts.map
|
|
59
|
+
declare function baseSaveToFile(document: PHDocument, path: string, extension: string, name?: string): Promise<string>;
|
|
60
|
+
//#endregion
|
|
61
|
+
export { baseLoadFromFile, baseMinimalSaveToFile, baseSaveToFile, fetchFileNode, getFileNode, getLocalFile, getRemoteFile, hashNode, readFileNode, writeFileNode };
|
|
62
|
+
//# sourceMappingURL=node.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.d.mts","names":[],"sources":["../node.mts"],"mappings":";;;;;;AAgCA;;;iBAAsB,YAAA,CAAa,IAAA,WAAe,OAAA,CAAQ,eAAA;;;;;;iBAapC,aAAA,CAAc,GAAA,WAAc,OAAA,CAAQ,eAAA;AAAA,iBAa1C,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,GAAqB,IAAA,aAAY,OAAA,CAAA,eAAA;AAAA,cAIjC,QAAA,GACX,IAAA,EAAM,UAAA,EACN,SAAA,WACA,QAAA,qBACA,OAAA,GAAU,MAAA;AA7DZ;;;;;;;;;;;AA2BA;;;;AA3BA,iBA6FsB,gBAAA,gBACL,WAAA,GAAc,WAAA,CAAA,CAE7B,IAAA,UACA,OAAA,EAAS,OAAA,CAAQ,MAAA,GACjB,OAAA,GAAU,qBAAA,GACT,OAAA,CAAQ,UAAA,CAAW,MAAA;AApEtB;;;;;AAAA,iBA8EsB,qBAAA,CACpB,IAAA,EAAM,iBAAA,EACN,IAAA,UACA,SAAA,WAAiB,OAAA;;;;;;AA3DnB;;;;;;;iBAyFsB,cAAA,CACpB,QAAA,EAAU,UAAA,EACV,IAAA,UACA,SAAA,UACA,IAAA,YAAa,OAAA"}
|
package/dist/node.mjs
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { baseLoadFromInput, createMinimalZip, createZip } from "@powerhousedao/shared/document-model";
|
|
2
|
+
import mime from "mime/lite";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import https from "node:https";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
//#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
|
+
function writeFileNode(path, name, data) {
|
|
48
|
+
const filePath = join(path, name);
|
|
49
|
+
fs.mkdirSync(path, { recursive: true });
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
try {
|
|
52
|
+
fs.writeFile(filePath, data, {}, (err) => {
|
|
53
|
+
if (err) reject(err);
|
|
54
|
+
else resolve(filePath);
|
|
55
|
+
});
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error instanceof Error) reject(error);
|
|
58
|
+
else reject(new Error(String(error)));
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function readFileNode(path) {
|
|
63
|
+
return fs.readFileSync(path);
|
|
64
|
+
}
|
|
65
|
+
function fetchFileNode(url) {
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
https.get(url, (resp) => {
|
|
68
|
+
const data = [];
|
|
69
|
+
const mimeType = resp.headers["content-type"];
|
|
70
|
+
resp.on("data", (chunk) => {
|
|
71
|
+
data.push(chunk);
|
|
72
|
+
});
|
|
73
|
+
resp.on("end", () => {
|
|
74
|
+
resolve({
|
|
75
|
+
buffer: Buffer.concat(data),
|
|
76
|
+
mimeType
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}).on("error", (err) => {
|
|
80
|
+
reject(err);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const getFileNode = async (file) => {
|
|
85
|
+
return readFileNode(file);
|
|
86
|
+
};
|
|
87
|
+
const hashNode = (data, algorithm = "sha1", encoding = "base64", _params) => {
|
|
88
|
+
if (![
|
|
89
|
+
"sha1",
|
|
90
|
+
"sha256",
|
|
91
|
+
"sha512"
|
|
92
|
+
].includes(algorithm)) throw new Error(`Hashing algorithm not supported: "${algorithm}". Available: sha1, sha256, sha512`);
|
|
93
|
+
if (!["base64", "hex"].includes(encoding)) throw new Error(`Hash encoding not supported: "${encoding}". Available: base64, hex`);
|
|
94
|
+
return createHash(algorithm).update(data).digest(encoding);
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Loads a document from a ZIP file.
|
|
98
|
+
*
|
|
99
|
+
* @remarks
|
|
100
|
+
* This function reads a ZIP file and returns the document state after
|
|
101
|
+
* applying all the operations. The reducer is used to apply the operations.
|
|
102
|
+
*
|
|
103
|
+
* @typeParam S - The type of the state object.
|
|
104
|
+
* @typeParam A - The type of the actions that can be applied to the state object.
|
|
105
|
+
*
|
|
106
|
+
* @param path - The path to the ZIP file.
|
|
107
|
+
* @param reducer - The reducer to apply the operations to the state object.
|
|
108
|
+
* @returns A promise that resolves to the document state after applying all the operations.
|
|
109
|
+
* @throws An error if the initial state or the operations history is not found in the ZIP file.
|
|
110
|
+
*/
|
|
111
|
+
async function baseLoadFromFile(path, reducer, options) {
|
|
112
|
+
return baseLoadFromInput(readFileNode(path), reducer, options);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Saves a minimal document backup to a .phd file.
|
|
116
|
+
* Used when the full document is not available (e.g., in onOperations handler).
|
|
117
|
+
* Creates a file with minimal header and empty operations.
|
|
118
|
+
*/
|
|
119
|
+
async function baseMinimalSaveToFile(data, path, extension) {
|
|
120
|
+
const file = await createMinimalZip(data).generateAsync({
|
|
121
|
+
type: "uint8array",
|
|
122
|
+
streamFiles: true
|
|
123
|
+
});
|
|
124
|
+
const fileExtension = extension ? `.${extension}.phd` : ".phd";
|
|
125
|
+
return writeFileNode(path, data.name.endsWith(fileExtension) ? data.name : `${data.name}${fileExtension}`, file);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Saves a document to a ZIP file.
|
|
129
|
+
*
|
|
130
|
+
* @remarks
|
|
131
|
+
* This function creates a ZIP file containing the document's state, operations,
|
|
132
|
+
* and file attachments. The file is saved to the specified path.
|
|
133
|
+
*
|
|
134
|
+
* @param document - The document to save to the file.
|
|
135
|
+
* @param path - The path to save the file to.
|
|
136
|
+
* @param extension - The extension to use for the file.
|
|
137
|
+
* @returns A promise that resolves to the path of the saved file.
|
|
138
|
+
*/
|
|
139
|
+
async function baseSaveToFile(document, path, extension, name) {
|
|
140
|
+
const file = await createZip(document).generateAsync({
|
|
141
|
+
type: "uint8array",
|
|
142
|
+
streamFiles: true
|
|
143
|
+
});
|
|
144
|
+
const fileName = name ?? document.header.name;
|
|
145
|
+
const fileExtension = extension ? `.${extension}.phd` : ".phd";
|
|
146
|
+
return writeFileNode(path, fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`, file);
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
export { baseLoadFromFile, baseMinimalSaveToFile, baseSaveToFile, fetchFileNode, getFileNode, getLocalFile, getRemoteFile, hashNode, readFileNode, writeFileNode };
|
|
150
|
+
|
|
151
|
+
//# sourceMappingURL=node.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.mjs","names":[],"sources":["../node.mts"],"sourcesContent":["import {\n baseLoadFromInput,\n createMinimalZip,\n createZip,\n type Attachment,\n type AttachmentInput,\n type MinimalBackupData,\n type PHBaseState,\n type PHDocument,\n type Reducer,\n type ReplayDocumentOptions,\n} from \"@powerhousedao/shared/document-model\";\nimport mime from \"mime/lite\";\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\nfunction getFileAttributes(\n file: string,\n): Omit<Attachment, \"data\" | \"mimeType\"> {\n const extension = file.replace(/^.*\\./, \"\") || undefined;\n const fileName = file.replace(/^.*[/\\\\]/, \"\") || undefined;\n return { extension, fileName };\n}\n\n/**\n * Reads an attachment from a file and returns its base64-encoded data and MIME type.\n * @param path - The path of the attachment file to read.\n * @returns A Promise that resolves to an object containing the base64-encoded data and MIME type of the attachment.\n */\nexport async function getLocalFile(path: string): Promise<AttachmentInput> {\n const buffer = await getFileNode(path);\n const mimeType = mime.getType(path) || \"application/octet-stream\";\n const attributes = getFileAttributes(path);\n const data = buffer.toString(\"base64\");\n return { data, hash: hashNode(data), mimeType, ...attributes };\n}\n\n/**\n * Fetches an attachment from a URL and returns its base64-encoded data and MIME type.\n * @param url - The URL of the attachment to fetch.\n * @returns A Promise that resolves to an object containing the base64-encoded data and MIME type of the attachment.\n */\nexport async function getRemoteFile(url: string): Promise<AttachmentInput> {\n const { buffer, mimeType = \"application/octet-stream\" } =\n await fetchFileNode(url);\n const attributes = getFileAttributes(url);\n const data = buffer.toString(\"base64\");\n return {\n data,\n hash: hashNode(data),\n mimeType,\n ...attributes,\n };\n}\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 = async (file: string) => {\n return 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 * 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 zip = createMinimalZip(data);\n const file = await zip.generateAsync({\n type: \"uint8array\",\n streamFiles: true,\n });\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, operations,\n * and file attachments. 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 // create zip file\n const zip = createZip(document);\n const file = await zip.generateAsync({\n type: \"uint8array\",\n streamFiles: true,\n });\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":";;;;;;;AAmBA,SAAS,kBACP,MACuC;AAGvC,QAAO;EAAE,WAFS,KAAK,QAAQ,SAAS,GAAG,IAAI,KAAA;EAE3B,UADH,KAAK,QAAQ,YAAY,GAAG,IAAI,KAAA;EACnB;;;;;;;AAQhC,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pB,UAAO,IAAI;IACX;GACJ;;AAGJ,MAAa,cAAc,OAAO,SAAiB;AACjD,QAAO,aAAa,KAAK;;AAG3B,MAAa,YACX,MACA,YAAY,QACZ,WAA6B,UAC7B,YACG;AACH,KAAI,CAAC;EAAC;EAAQ;EAAU;EAAS,CAAC,SAAS,UAAU,CACnD,OAAM,IAAI,MACR,qCAAqC,UAAU,oCAChD;AAGH,KAAI,CAAC,CAAC,UAAU,MAAM,CAAC,SAAS,SAAS,CACvC,OAAM,IAAI,MACR,iCAAiC,SAAS,2BAC3C;AAGH,QAAO,WAAW,UAAU,CAAC,OAAO,KAAK,CAAC,OAAO,SAAS;;;;;;;;;;;;;;;;;AAkB5D,eAAsB,iBAGpB,MACA,SACA,SAC6B;AAE7B,QAAO,kBADM,aAAa,KAAK,EACA,SAAS,QAAQ;;;;;;;AAQlD,eAAsB,sBACpB,MACA,MACA,WACA;CAEA,MAAM,OAAO,MADD,iBAAiB,KAAK,CACX,cAAc;EACnC,MAAM;EACN,aAAa;EACd,CAAC;CACF,MAAM,gBAAgB,YAAY,IAAI,UAAU,QAAQ;AAExD,QAAO,cACL,MACA,KAAK,KAAK,SAAS,cAAc,GAC7B,KAAK,OACL,GAAG,KAAK,OAAO,iBACnB,KACD;;;;;;;;;;;;;;AAeH,eAAsB,eACpB,UACA,MACA,WACA,MACA;CAGA,MAAM,OAAO,MADD,UAAU,SAAS,CACR,cAAc;EACnC,MAAM;EACN,aAAa;EACd,CAAC;CACF,MAAM,WAAW,QAAQ,SAAS,OAAO;CACzC,MAAM,gBAAgB,YAAY,IAAI,UAAU,QAAQ;AAExD,QAAO,cACL,MACA,SAAS,SAAS,cAAc,GAAG,WAAW,GAAG,WAAW,iBAC5D,KACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "document-model",
|
|
3
|
-
"version": "6.0.0-dev.
|
|
3
|
+
"version": "6.0.0-dev.106",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"private": false,
|
|
6
6
|
"files": [
|
|
@@ -13,23 +13,18 @@
|
|
|
13
13
|
"type": "module",
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
|
-
"import": "./dist/
|
|
17
|
-
"types": "./dist/
|
|
18
|
-
},
|
|
19
|
-
"./core": {
|
|
20
|
-
"import": "./dist/src/core/index.js",
|
|
21
|
-
"types": "./dist/src/core/index.d.ts"
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts"
|
|
22
18
|
},
|
|
23
19
|
"./test": {
|
|
24
20
|
"import": "./dist/test/index.js",
|
|
25
21
|
"types": "./dist/test/index.d.ts"
|
|
26
22
|
},
|
|
27
23
|
"./node": {
|
|
28
|
-
"
|
|
29
|
-
"types": "./dist/
|
|
24
|
+
"node": "./dist/node.mjs",
|
|
25
|
+
"types": "./dist/node.d.mts"
|
|
30
26
|
}
|
|
31
27
|
},
|
|
32
|
-
"types": "./dist/src/index.d.ts",
|
|
33
28
|
"sideEffects": false,
|
|
34
29
|
"devDependencies": {
|
|
35
30
|
"@types/jest": "^29.5.14",
|
|
@@ -40,18 +35,19 @@
|
|
|
40
35
|
},
|
|
41
36
|
"dependencies": {
|
|
42
37
|
"change-case": "5.4.4",
|
|
43
|
-
"jszip": "^3.10.1",
|
|
44
38
|
"mime": "^4.0.4",
|
|
45
39
|
"mutative": "^1.0.5",
|
|
46
|
-
"safe-stable-stringify": "^2.4.3",
|
|
47
|
-
"sha1-uint8array": "^0.10.7",
|
|
48
40
|
"zod": "4.3.6",
|
|
49
|
-
"
|
|
41
|
+
"tsdown": "0.21.0",
|
|
42
|
+
"jszip": "^3.10.1",
|
|
43
|
+
"tsx": "4.21.0",
|
|
44
|
+
"@powerhousedao/shared": "6.0.0-dev.106"
|
|
50
45
|
},
|
|
51
46
|
"scripts": {
|
|
52
47
|
"tsc": "tsc",
|
|
53
48
|
"lint": "eslint",
|
|
54
49
|
"test": "vitest --run",
|
|
55
|
-
"test:watch": "vitest watch"
|
|
50
|
+
"test:watch": "vitest watch",
|
|
51
|
+
"build:bundle": "tsx ./build.ts"
|
|
56
52
|
}
|
|
57
53
|
}
|