@nubbin/store-fs 0.1.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +109 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jesse Wheeler
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ArtifactStore } from '@nubbin/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One pointer file per route, one file per artifact, and nothing else. No aggregate document
|
|
5
|
+
* exists, so two publishes to different routes cannot lose each other's write.
|
|
6
|
+
*/
|
|
7
|
+
declare function createFsArtifactStore(root: string): ArtifactStore;
|
|
8
|
+
|
|
9
|
+
export { createFsArtifactStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/artifactPath.ts
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
function artifactPath(root, hash) {
|
|
4
|
+
return join(root, "artifacts", `${hash}.json`);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// src/fsManifest.ts
|
|
8
|
+
import { readdir } from "fs/promises";
|
|
9
|
+
import { join as join2 } from "path";
|
|
10
|
+
|
|
11
|
+
// src/readJsonOrNull.ts
|
|
12
|
+
import { readFile } from "fs/promises";
|
|
13
|
+
async function readJsonOrNull(filePath) {
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(await readFile(filePath, "utf8"));
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (error.code === "ENOENT") {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/fsManifest.ts
|
|
25
|
+
async function fsManifest(root) {
|
|
26
|
+
const directory = join2(root, "routes");
|
|
27
|
+
let entries;
|
|
28
|
+
try {
|
|
29
|
+
entries = await readdir(directory);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error.code !== "ENOENT") {
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
entries = [];
|
|
35
|
+
}
|
|
36
|
+
const read = entries.map((entry) => readJsonOrNull(join2(directory, entry)));
|
|
37
|
+
const routes = (await Promise.all(read)).filter((pointer) => pointer !== null);
|
|
38
|
+
return { routes, generatedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/fsPublish.ts
|
|
42
|
+
import { parseMatchKind } from "@nubbin/core";
|
|
43
|
+
|
|
44
|
+
// src/pointerPath.ts
|
|
45
|
+
import { join as join3 } from "path";
|
|
46
|
+
|
|
47
|
+
// src/encodeRouteKey.ts
|
|
48
|
+
function encodeRouteKey(route) {
|
|
49
|
+
return encodeURIComponent(route);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/pointerPath.ts
|
|
53
|
+
function pointerPath(root, route) {
|
|
54
|
+
return join3(root, "routes", `${encodeRouteKey(route)}.json`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/writeJsonAtomic.ts
|
|
58
|
+
import { mkdir, rename, writeFile } from "fs/promises";
|
|
59
|
+
import { dirname } from "path";
|
|
60
|
+
async function writeJsonAtomic(filePath, value) {
|
|
61
|
+
await mkdir(dirname(filePath), { recursive: true });
|
|
62
|
+
const temp = `${filePath}.${process.pid}.tmp`;
|
|
63
|
+
await writeFile(temp, JSON.stringify(value, null, 2));
|
|
64
|
+
await rename(temp, filePath);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/fsPublish.ts
|
|
68
|
+
async function fsPublish(root, route, hash) {
|
|
69
|
+
if (!await readJsonOrNull(artifactPath(root, hash))) {
|
|
70
|
+
throw new Error(`cannot publish ${route}: artifact ${hash} is not in the store`);
|
|
71
|
+
}
|
|
72
|
+
const pointer = {
|
|
73
|
+
route,
|
|
74
|
+
matchKind: parseMatchKind(route),
|
|
75
|
+
hash,
|
|
76
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
77
|
+
};
|
|
78
|
+
await writeJsonAtomic(pointerPath(root, route), pointer);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/fsUnpublish.ts
|
|
82
|
+
import { rm } from "fs/promises";
|
|
83
|
+
async function fsUnpublish(root, route) {
|
|
84
|
+
await rm(pointerPath(root, route), { force: true });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/fsWriteArtifact.ts
|
|
88
|
+
async function fsWriteArtifact(root, artifact) {
|
|
89
|
+
const path = artifactPath(root, artifact.hash);
|
|
90
|
+
if (await readJsonOrNull(path)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
await writeJsonAtomic(path, artifact);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/createFsArtifactStore.ts
|
|
97
|
+
function createFsArtifactStore(root) {
|
|
98
|
+
return {
|
|
99
|
+
read: (hash) => readJsonOrNull(artifactPath(root, hash)),
|
|
100
|
+
write: (artifact) => fsWriteArtifact(root, artifact),
|
|
101
|
+
pointer: (route) => readJsonOrNull(pointerPath(root, route)),
|
|
102
|
+
manifest: () => fsManifest(root),
|
|
103
|
+
publish: (route, hash) => fsPublish(root, route, hash),
|
|
104
|
+
unpublish: (route) => fsUnpublish(root, route)
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export {
|
|
108
|
+
createFsArtifactStore
|
|
109
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nubbin/store-fs",
|
|
3
|
+
"version": "0.1.0-rc.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@nubbin/core": "0.1.0-rc.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "25.9.2",
|
|
23
|
+
"tsup": "8.5.1",
|
|
24
|
+
"typescript": "6.0.3",
|
|
25
|
+
"vitest": "4.1.10"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"typecheck": "tsc --noEmit"
|
|
31
|
+
}
|
|
32
|
+
}
|