@x-otto/workspace-artifacts 0.0.0-stage → 0.4.0-beta.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.
@@ -0,0 +1,4 @@
1
+ {
2
+ "version": 1,
3
+ "revision": "bed4cfe38f2d03a555879ae295aea757"
4
+ }
@@ -0,0 +1,53 @@
1
+ //#region src/types.d.ts
2
+ type ArtifactKind = 'app' | 'module';
3
+ interface ArtifactRef {
4
+ kind: ArtifactKind;
5
+ id: string;
6
+ scope: string;
7
+ digest: string;
8
+ generation: number;
9
+ }
10
+ interface ArtifactFile {
11
+ path: string;
12
+ content: string;
13
+ }
14
+ interface StagedArtifact {
15
+ kind: ArtifactKind;
16
+ id: string;
17
+ scope: string;
18
+ capabilities: readonly string[];
19
+ files: readonly ArtifactFile[];
20
+ parent?: string;
21
+ }
22
+ interface ArtifactRevision {
23
+ digest: string;
24
+ kind: ArtifactKind;
25
+ id: string;
26
+ scope: string;
27
+ capabilities: readonly string[];
28
+ files: readonly string[];
29
+ createdAt: string;
30
+ parent?: string;
31
+ generation: number;
32
+ }
33
+ interface ArtifactSummary {
34
+ ref: ArtifactRef;
35
+ createdAt: string;
36
+ capabilities: readonly string[];
37
+ }
38
+ //#endregion
39
+ //#region src/store.d.ts
40
+ declare class ArtifactRevisionStore {
41
+ readonly root: string;
42
+ constructor(root: string);
43
+ directoryOf(kind: string, id: string, digest: string): string;
44
+ write(staged: StagedArtifact): Promise<ArtifactRevision>;
45
+ read(ref: Pick<ArtifactRef, 'kind' | 'id' | 'digest'>): Promise<StagedArtifact | undefined>;
46
+ list(kind: string, id: string): Promise<ArtifactSummary[]>;
47
+ private readRevision;
48
+ }
49
+ //#endregion
50
+ //#region src/canonical.d.ts
51
+ declare function digestOf(input: StagedArtifact): string;
52
+ //#endregion
53
+ export { type ArtifactFile, type ArtifactKind, type ArtifactRef, type ArtifactRevision, ArtifactRevisionStore, type ArtifactSummary, type StagedArtifact, digestOf };
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ import { withStoreMutex, writeFileAtomic } from "@x-otto/atomic-fs";
2
+ import { readFile, readdir, rename, rm } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { PLUGIN_MANIFEST_FILENAME, canonicalJson } from "@x-otto/kernel";
5
+ import { createHash } from "node:crypto";
6
+
7
+ //#region src/canonical.ts
8
+ function digestContentOf(file) {
9
+ if (file.path !== PLUGIN_MANIFEST_FILENAME) return file.content;
10
+ return canonicalJson(JSON.parse(file.content));
11
+ }
12
+ function sortedFiles(files) {
13
+ return [...files].sort((left, right) => left.path.localeCompare(right.path));
14
+ }
15
+ function digestOf(input) {
16
+ const payload = {
17
+ kind: input.kind,
18
+ id: input.id,
19
+ scope: input.scope,
20
+ capabilities: [...input.capabilities].sort(),
21
+ files: sortedFiles(input.files).map((file) => ({
22
+ path: file.path,
23
+ content: digestContentOf(file)
24
+ }))
25
+ };
26
+ return createHash("sha256").update(canonicalJson(payload)).digest("hex");
27
+ }
28
+
29
+ //#endregion
30
+ //#region src/store.ts
31
+ const DURABLE_WRITE = {
32
+ durable: true,
33
+ syncDirectory: true
34
+ };
35
+ const DIGEST_DIRECTORY_PATTERN = /^[0-9a-f]{64}$/u;
36
+ const LOCK_DEADLINE_MS = 1e4;
37
+ async function withStoreLock(mutexRoot, action) {
38
+ return withStoreMutex(mutexRoot, action, { deadlineMs: LOCK_DEADLINE_MS });
39
+ }
40
+ var ArtifactRevisionStore = class {
41
+ root;
42
+ constructor(root) {
43
+ this.root = join(root, ".otto", "space", ".revisions");
44
+ }
45
+ directoryOf(kind, id, digest) {
46
+ return join(this.root, kind, id, digest);
47
+ }
48
+ async write(staged) {
49
+ const digest = digestOf(staged);
50
+ const directory = this.directoryOf(staged.kind, staged.id, digest);
51
+ const recordPath = join(directory, "revision.json");
52
+ const existing = await this.readRevision(recordPath);
53
+ if (existing !== void 0) return existing;
54
+ return withStoreLock(join(this.root, staged.kind, `${staged.id}.lock`), async () => {
55
+ const raced = await this.readRevision(recordPath);
56
+ if (raced !== void 0) return raced;
57
+ const files = [...staged.files].sort((left, right) => left.path.localeCompare(right.path));
58
+ const stagingDirectory = `${directory}.${process.pid}.staging`;
59
+ await rm(stagingDirectory, {
60
+ recursive: true,
61
+ force: true
62
+ });
63
+ try {
64
+ for (const file of files) await writeFileAtomic(join(stagingDirectory, "files", file.path), file.content, DURABLE_WRITE);
65
+ const existingRevisions = await this.list(staged.kind, staged.id);
66
+ const record = {
67
+ digest,
68
+ kind: staged.kind,
69
+ id: staged.id,
70
+ scope: staged.scope,
71
+ capabilities: [...staged.capabilities].sort(),
72
+ files: files.map((file) => file.path),
73
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
74
+ ...staged.parent === void 0 ? {} : { parent: staged.parent },
75
+ generation: nextGenerationOf(existingRevisions)
76
+ };
77
+ await writeFileAtomic(join(stagingDirectory, "revision.json"), `${JSON.stringify(record, null, 2)}\n`, DURABLE_WRITE);
78
+ await rm(directory, {
79
+ recursive: true,
80
+ force: true
81
+ });
82
+ await rename(stagingDirectory, directory);
83
+ return revisionOf(record);
84
+ } catch (error) {
85
+ await rm(stagingDirectory, {
86
+ recursive: true,
87
+ force: true
88
+ });
89
+ throw error;
90
+ }
91
+ });
92
+ }
93
+ async read(ref) {
94
+ const directory = this.directoryOf(ref.kind, ref.id, ref.digest);
95
+ const record = await this.readRevision(join(directory, "revision.json"));
96
+ if (record === void 0) return;
97
+ const files = [];
98
+ for (const path of record.files) {
99
+ const content = await readFile(join(directory, "files", path), "utf8");
100
+ files.push({
101
+ path,
102
+ content
103
+ });
104
+ }
105
+ return {
106
+ kind: record.kind,
107
+ id: record.id,
108
+ scope: record.scope,
109
+ capabilities: record.capabilities,
110
+ files,
111
+ ...record.parent === void 0 ? {} : { parent: record.parent }
112
+ };
113
+ }
114
+ async list(kind, id) {
115
+ const directory = join(this.root, kind, id);
116
+ let entries;
117
+ try {
118
+ entries = await readdir(directory);
119
+ } catch {
120
+ return [];
121
+ }
122
+ const summaries = [];
123
+ for (const digest of entries) {
124
+ if (!DIGEST_DIRECTORY_PATTERN.test(digest)) continue;
125
+ const record = await this.readRevision(join(directory, digest, "revision.json"));
126
+ if (record !== void 0) summaries.push({
127
+ ref: {
128
+ kind: record.kind,
129
+ id: record.id,
130
+ scope: record.scope,
131
+ digest: record.digest,
132
+ generation: record.generation
133
+ },
134
+ createdAt: record.createdAt,
135
+ capabilities: record.capabilities
136
+ });
137
+ }
138
+ return summaries.sort((left, right) => right.createdAt.localeCompare(left.createdAt));
139
+ }
140
+ async readRevision(path) {
141
+ try {
142
+ const content = await readFile(path, "utf8");
143
+ return revisionOf(JSON.parse(content));
144
+ } catch {
145
+ return;
146
+ }
147
+ }
148
+ };
149
+ function nextGenerationOf(summaries) {
150
+ return summaries.reduce((highest, summary) => Math.max(highest, summary.ref.generation), 0) + 1;
151
+ }
152
+ function revisionOf(record) {
153
+ return {
154
+ digest: record.digest,
155
+ kind: record.kind,
156
+ id: record.id,
157
+ scope: record.scope,
158
+ capabilities: record.capabilities,
159
+ files: record.files,
160
+ createdAt: record.createdAt,
161
+ ...record.parent === void 0 ? {} : { parent: record.parent },
162
+ generation: record.generation
163
+ };
164
+ }
165
+
166
+ //#endregion
167
+ export { ArtifactRevisionStore, digestOf };
package/package.json CHANGED
@@ -1,6 +1,25 @@
1
1
  {
2
2
  "name": "@x-otto/workspace-artifacts",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.0-beta.0",
4
+ "description": "Workspace artifact revision store:digest、immutable revisions、active pointer",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "type": "module",
9
+ "main": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ }
16
+ },
17
+ "dependencies": {
18
+ "@x-otto/atomic-fs": "^0.4.0-beta.0",
19
+ "@x-otto/kernel": "^0.4.0-beta.0"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public",
23
+ "tag": "beta"
24
+ }
25
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.