@seed-hypermedia/client 0.0.68 → 0.0.70
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 +201 -0
- package/dist/auth.d.ts +79 -0
- package/dist/auth.mjs +266 -0
- package/dist/auto-link.d.ts +3 -2
- package/dist/auto-link.mjs +21 -0
- package/dist/base64.mjs +9 -0
- package/dist/blobs.d.ts +170 -0
- package/dist/blobs.mjs +51 -0
- package/dist/block-diff.mjs +17 -0
- package/dist/blocks-to-markdown.mjs +22 -0
- package/dist/capability.d.ts +7 -2
- package/dist/capability.mjs +14 -0
- package/dist/cbor.d.ts +13 -0
- package/dist/cbor.mjs +13 -0
- package/dist/change.d.ts +8 -7
- package/dist/change.mjs +24 -0
- package/dist/chunk-2MD3FXKB.mjs +14 -0
- package/dist/chunk-37WRGUK4.mjs +125 -0
- package/dist/chunk-3KESF5XJ.mjs +297 -0
- package/dist/chunk-3TPCJCVO.mjs +24 -0
- package/dist/chunk-4Q3SAL5R.mjs +75 -0
- package/dist/chunk-52P3E6H6.mjs +459 -0
- package/dist/chunk-BUPMCRR4.mjs +48 -0
- package/dist/chunk-CBELM7KX.mjs +29 -0
- package/dist/{chunk-KCR3277A.mjs → chunk-DBHL3BYB.mjs} +9 -14
- package/dist/chunk-DR3HJ6BV.mjs +25 -0
- package/dist/chunk-EU36RFR3.mjs +159 -0
- package/dist/chunk-EY6RQVAP.mjs +138 -0
- package/dist/chunk-H7L4TI6X.mjs +705 -0
- package/dist/chunk-HBIVZA6C.mjs +102 -0
- package/dist/chunk-IT5LLTMP.mjs +209 -0
- package/dist/chunk-JT57MV7Y.mjs +136 -0
- package/dist/chunk-KO4XH5L2.mjs +475 -0
- package/dist/chunk-LVAHHQIK.mjs +41 -0
- package/dist/chunk-LYECQKZE.mjs +49 -0
- package/dist/chunk-N3P7S5YZ.mjs +118 -0
- package/dist/chunk-NPG76WUS.mjs +348 -0
- package/dist/chunk-NYWCMQTH.mjs +412 -0
- package/dist/chunk-OZKQOI2X.mjs +334 -0
- package/dist/chunk-PJ2RFT5A.mjs +113 -0
- package/dist/chunk-PPAY4WZX.mjs +40 -0
- package/dist/chunk-S2PVJK2P.mjs +517 -0
- package/dist/chunk-SHOAYNJY.mjs +289 -0
- package/dist/chunk-T5BTAJPW.mjs +725 -0
- package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
- package/dist/chunk-VN7F33SS.mjs +108 -0
- package/dist/chunk-YNVFRCUH.mjs +171 -0
- package/dist/client.d.ts +3 -2
- package/dist/client.mjs +15 -0
- package/dist/comment.d.ts +5 -4
- package/dist/comment.mjs +19 -0
- package/dist/contact.d.ts +5 -4
- package/dist/contact.mjs +15 -0
- package/dist/document-state.mjs +8 -0
- package/dist/editor-types.mjs +0 -0
- package/dist/editorblock-to-hmblock.mjs +11 -0
- package/dist/encryption.mjs +13 -0
- package/dist/errors.mjs +11 -0
- package/dist/file-to-ipfs.mjs +13 -0
- package/dist/grobid.mjs +11 -0
- package/dist/hm-resolver.mjs +10 -0
- package/dist/hmauth.d.ts +169 -0
- package/dist/hmauth.mjs +53 -0
- package/dist/hmblock-to-editorblock.mjs +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +132 -5453
- package/dist/keyfile.mjs +19 -0
- package/dist/markdown-to-blocks.mjs +15 -0
- package/dist/pdf-to-blocks-embedded.mjs +7 -0
- package/dist/pdf-to-blocks.mjs +11 -0
- package/dist/ref.d.ts +5 -4
- package/dist/ref.mjs +13 -0
- package/dist/resource-read.mjs +15 -0
- package/dist/signer.d.ts +42 -0
- package/dist/signer.mjs +9 -0
- package/dist/signing.d.ts +10 -2
- package/dist/signing.mjs +16 -0
- package/dist/tei-to-blocks.mjs +8 -0
- package/dist/unicode.mjs +19 -0
- package/dist/vault-local.mjs +10 -6
- package/dist/vault.d.ts +14 -4
- package/dist/vault.mjs +6 -2
- package/package.json +18 -28
- package/src/index.ts +0 -144
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createChange,
|
|
3
|
+
createChangeOps
|
|
4
|
+
} from "./chunk-JT57MV7Y.mjs";
|
|
5
|
+
import {
|
|
6
|
+
createVersionRef
|
|
7
|
+
} from "./chunk-PJ2RFT5A.mjs";
|
|
8
|
+
import {
|
|
9
|
+
resolveDocumentState
|
|
10
|
+
} from "./chunk-HBIVZA6C.mjs";
|
|
11
|
+
import {
|
|
12
|
+
entityQueryPathToHmIdPath,
|
|
13
|
+
packHmId,
|
|
14
|
+
unpackHmId
|
|
15
|
+
} from "./chunk-SQEUO7FO.mjs";
|
|
16
|
+
|
|
17
|
+
// src/auto-link.ts
|
|
18
|
+
import { CID } from "multiformats/cid";
|
|
19
|
+
function documentContainsLinkToChild(document, childId) {
|
|
20
|
+
const childUrlNormalized = packHmId({ ...childId, version: null, latest: null });
|
|
21
|
+
function searchBlocks(nodes) {
|
|
22
|
+
for (const node of nodes) {
|
|
23
|
+
if (node.block.type === "Embed") {
|
|
24
|
+
const linkId = unpackHmId(node.block.link);
|
|
25
|
+
if (linkId) {
|
|
26
|
+
const linkUrl = packHmId({ ...linkId, version: null, latest: null });
|
|
27
|
+
if (linkUrl === childUrlNormalized) return true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const annotations = "annotations" in node.block ? node.block.annotations : void 0;
|
|
31
|
+
if (annotations) {
|
|
32
|
+
for (const ann of annotations) {
|
|
33
|
+
if (ann.type === "Link" || ann.type === "Embed") {
|
|
34
|
+
const linkId = unpackHmId(ann.link);
|
|
35
|
+
if (linkId) {
|
|
36
|
+
const linkUrl = packHmId({ ...linkId, version: null, latest: null });
|
|
37
|
+
if (linkUrl === childUrlNormalized) return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (node.children && searchBlocks(node.children)) return true;
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return searchBlocks(document.content || []);
|
|
47
|
+
}
|
|
48
|
+
function documentHasSelfQuery(document, documentId) {
|
|
49
|
+
const parentPathStr = (documentId.path || []).join("/");
|
|
50
|
+
function isSelfMatch(include) {
|
|
51
|
+
if (!include) return false;
|
|
52
|
+
if (include.space !== documentId.uid) return false;
|
|
53
|
+
const includePath = entityQueryPathToHmIdPath(include.path);
|
|
54
|
+
return (includePath || []).join("/") === parentPathStr;
|
|
55
|
+
}
|
|
56
|
+
function searchBlocks(nodes) {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
for (const node of nodes) {
|
|
59
|
+
if (node.block.type === "Query") {
|
|
60
|
+
const includes = (_b = (_a = node.block.attributes) == null ? void 0 : _a.query) == null ? void 0 : _b.includes;
|
|
61
|
+
if (includes == null ? void 0 : includes.some(isSelfMatch)) return true;
|
|
62
|
+
}
|
|
63
|
+
if (node.children && searchBlocks(node.children)) return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return searchBlocks(document.content || []);
|
|
68
|
+
}
|
|
69
|
+
function shouldAutoLinkParent(isPrivate, parentDocument, editableLocation, parentId) {
|
|
70
|
+
if (isPrivate) return false;
|
|
71
|
+
if (parentDocument) {
|
|
72
|
+
if (documentContainsLinkToChild(parentDocument, editableLocation)) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (documentHasSelfQuery(parentDocument, parentId)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
function createAutoLinkOps(parentDocument, childHmUrl, newBlockId) {
|
|
82
|
+
const existingRootBlockIds = (parentDocument.content || []).map((node) => node.block.id).filter(Boolean);
|
|
83
|
+
const embedBlock = {
|
|
84
|
+
id: newBlockId,
|
|
85
|
+
type: "Embed",
|
|
86
|
+
link: childHmUrl,
|
|
87
|
+
attributes: { view: "Card" }
|
|
88
|
+
};
|
|
89
|
+
return [
|
|
90
|
+
{ type: "ReplaceBlock", block: embedBlock },
|
|
91
|
+
{ type: "MoveBlocks", blocks: [...existingRootBlockIds, newBlockId], parent: "" }
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
async function autoLinkChildToParent(opts) {
|
|
95
|
+
const { client, account, path, childHmUrl, signer } = opts;
|
|
96
|
+
const segments = path.replace(/^\//, "").split("/");
|
|
97
|
+
if (segments.length <= 1) return false;
|
|
98
|
+
const parentSegments = segments.slice(0, -1);
|
|
99
|
+
const parentPath = "/" + parentSegments.join("/");
|
|
100
|
+
const parentId = unpackHmId(`hm://${account}${parentPath}`);
|
|
101
|
+
if (!parentId) return false;
|
|
102
|
+
const childId = unpackHmId(childHmUrl);
|
|
103
|
+
if (!childId) return false;
|
|
104
|
+
let parentDocument = null;
|
|
105
|
+
try {
|
|
106
|
+
const resource = await client.request("Resource", { ...parentId, latest: true });
|
|
107
|
+
if (resource.type === "document") {
|
|
108
|
+
parentDocument = resource.document;
|
|
109
|
+
}
|
|
110
|
+
} catch {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
if (!parentDocument) return false;
|
|
114
|
+
if (!shouldAutoLinkParent(false, parentDocument, childId, parentId)) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const newBlockId = generateBlockId();
|
|
118
|
+
const ops = createAutoLinkOps(parentDocument, childHmUrl, newBlockId);
|
|
119
|
+
const hmUrl = `hm://${account}${parentPath}`;
|
|
120
|
+
const state = await resolveDocumentState(client, hmUrl);
|
|
121
|
+
const genesisCid = CID.parse(state.genesis);
|
|
122
|
+
const depCids = state.heads.map((h) => CID.parse(h));
|
|
123
|
+
const newDepth = state.headDepth + 1;
|
|
124
|
+
const { unsignedBytes, ts } = createChangeOps({ ops, genesisCid, deps: depCids, depth: newDepth });
|
|
125
|
+
const changeBlock = await createChange(unsignedBytes, signer);
|
|
126
|
+
const generation = Number(ts);
|
|
127
|
+
const refInput = await createVersionRef(
|
|
128
|
+
{
|
|
129
|
+
space: account,
|
|
130
|
+
path: parentPath,
|
|
131
|
+
genesis: state.genesis,
|
|
132
|
+
version: changeBlock.cid.toString(),
|
|
133
|
+
generation
|
|
134
|
+
},
|
|
135
|
+
signer
|
|
136
|
+
);
|
|
137
|
+
await client.publish({
|
|
138
|
+
blobs: [{ data: new Uint8Array(changeBlock.bytes), cid: changeBlock.cid.toString() }, ...refInput.blobs]
|
|
139
|
+
});
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
function generateBlockId() {
|
|
143
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
144
|
+
let id = "";
|
|
145
|
+
const bytes = new Uint8Array(10);
|
|
146
|
+
crypto.getRandomValues(bytes);
|
|
147
|
+
for (let i = 0; i < 10; i++) {
|
|
148
|
+
id += chars[bytes[i] % chars.length];
|
|
149
|
+
}
|
|
150
|
+
return id;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export {
|
|
154
|
+
documentContainsLinkToChild,
|
|
155
|
+
documentHasSelfQuery,
|
|
156
|
+
shouldAutoLinkParent,
|
|
157
|
+
createAutoLinkOps,
|
|
158
|
+
autoLinkChildToParent
|
|
159
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_PARAMS,
|
|
3
|
+
decrypt,
|
|
4
|
+
deriveKeyFromPassword,
|
|
5
|
+
encrypt
|
|
6
|
+
} from "./chunk-UOL3F6XQ.mjs";
|
|
7
|
+
import {
|
|
8
|
+
decode,
|
|
9
|
+
encode
|
|
10
|
+
} from "./chunk-2MD3FXKB.mjs";
|
|
11
|
+
import {
|
|
12
|
+
__export
|
|
13
|
+
} from "./chunk-7P6ASYW6.mjs";
|
|
14
|
+
|
|
15
|
+
// src/keyfile.ts
|
|
16
|
+
var keyfile_exports = {};
|
|
17
|
+
__export(keyfile_exports, {
|
|
18
|
+
create: () => create,
|
|
19
|
+
decrypt: () => decrypt2,
|
|
20
|
+
load: () => load,
|
|
21
|
+
parse: () => parse,
|
|
22
|
+
principalStringFromSeed: () => principalStringFromSeed,
|
|
23
|
+
stringify: () => stringify
|
|
24
|
+
});
|
|
25
|
+
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
26
|
+
import { base58btc } from "multiformats/bases/base58";
|
|
27
|
+
var ED25519_VARINT_PREFIX = new Uint8Array([237, 1]);
|
|
28
|
+
function principalStringFromSeed(seed) {
|
|
29
|
+
const pub = ed25519.getPublicKey(seed);
|
|
30
|
+
const principal = new Uint8Array(ED25519_VARINT_PREFIX.length + pub.length);
|
|
31
|
+
principal.set(ED25519_VARINT_PREFIX, 0);
|
|
32
|
+
principal.set(pub, ED25519_VARINT_PREFIX.length);
|
|
33
|
+
return base58btc.encode(principal);
|
|
34
|
+
}
|
|
35
|
+
async function create(input) {
|
|
36
|
+
const createTime = input.createTime ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
37
|
+
const includedProfile = input.profile && (input.profile.name || input.profile.description) ? input.profile : void 0;
|
|
38
|
+
if (!input.password) {
|
|
39
|
+
return {
|
|
40
|
+
createTime,
|
|
41
|
+
publicKey: input.publicKey,
|
|
42
|
+
keyB64: encode(input.key),
|
|
43
|
+
...includedProfile ? { profile: includedProfile } : {}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const salt = crypto.getRandomValues(new Uint8Array(16));
|
|
47
|
+
const derivedKey = await deriveKeyFromPassword(input.password, salt, DEFAULT_PARAMS);
|
|
48
|
+
const encrypted = await encrypt(input.key, derivedKey);
|
|
49
|
+
return {
|
|
50
|
+
createTime,
|
|
51
|
+
publicKey: input.publicKey,
|
|
52
|
+
keyB64: encode(encrypted),
|
|
53
|
+
encryption: {
|
|
54
|
+
kdf: "argon2id",
|
|
55
|
+
argon2: {
|
|
56
|
+
memoryCost: DEFAULT_PARAMS.memoryCost,
|
|
57
|
+
timeCost: DEFAULT_PARAMS.timeCost,
|
|
58
|
+
parallelism: DEFAULT_PARAMS.parallelism,
|
|
59
|
+
saltB64: encode(salt)
|
|
60
|
+
},
|
|
61
|
+
cipher: "xchacha20poly1305"
|
|
62
|
+
},
|
|
63
|
+
...includedProfile ? { profile: includedProfile } : {}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function parse(json) {
|
|
67
|
+
const value = JSON.parse(json);
|
|
68
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
69
|
+
throw new Error("Invalid key file payload");
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
function stringify(payload) {
|
|
74
|
+
return `${JSON.stringify(payload, null, 2)}
|
|
75
|
+
`;
|
|
76
|
+
}
|
|
77
|
+
async function decrypt2(payload, password) {
|
|
78
|
+
if (!payload.keyB64) {
|
|
79
|
+
throw new Error("keyB64 is required");
|
|
80
|
+
}
|
|
81
|
+
if (!payload.encryption) {
|
|
82
|
+
return decode(payload.keyB64);
|
|
83
|
+
}
|
|
84
|
+
if (!password) {
|
|
85
|
+
throw new Error("password is required for encrypted key files");
|
|
86
|
+
}
|
|
87
|
+
if (payload.encryption.kdf !== "argon2id") {
|
|
88
|
+
throw new Error(`unsupported key derivation function "${payload.encryption.kdf}"`);
|
|
89
|
+
}
|
|
90
|
+
if (payload.encryption.cipher !== "xchacha20poly1305") {
|
|
91
|
+
throw new Error(`unsupported cipher "${payload.encryption.cipher}"`);
|
|
92
|
+
}
|
|
93
|
+
if (!payload.encryption.argon2) {
|
|
94
|
+
throw new Error("argon2 parameters are required");
|
|
95
|
+
}
|
|
96
|
+
if (!payload.encryption.argon2.memoryCost || !payload.encryption.argon2.timeCost || !payload.encryption.argon2.parallelism) {
|
|
97
|
+
throw new Error("argon2 parameters must be greater than zero");
|
|
98
|
+
}
|
|
99
|
+
if (!payload.encryption.argon2.saltB64) {
|
|
100
|
+
throw new Error("saltB64 is required for encrypted key files");
|
|
101
|
+
}
|
|
102
|
+
const derivedKey = await deriveKeyFromPassword(
|
|
103
|
+
password,
|
|
104
|
+
decode(payload.encryption.argon2.saltB64),
|
|
105
|
+
{
|
|
106
|
+
memoryCost: payload.encryption.argon2.memoryCost,
|
|
107
|
+
timeCost: payload.encryption.argon2.timeCost,
|
|
108
|
+
parallelism: payload.encryption.argon2.parallelism
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
return decrypt(decode(payload.keyB64), derivedKey);
|
|
112
|
+
}
|
|
113
|
+
async function load(json, password) {
|
|
114
|
+
const payload = parse(json);
|
|
115
|
+
const seed = await decrypt2(payload, password);
|
|
116
|
+
if (seed.length !== 32) {
|
|
117
|
+
throw new Error(`invalid private key length: expected 32 bytes, got ${seed.length}`);
|
|
118
|
+
}
|
|
119
|
+
const publicKey = principalStringFromSeed(seed);
|
|
120
|
+
if (payload.publicKey && payload.publicKey !== publicKey) {
|
|
121
|
+
throw new Error("publicKey does not match private key");
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
payload,
|
|
125
|
+
seed,
|
|
126
|
+
publicKey
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export {
|
|
131
|
+
principalStringFromSeed,
|
|
132
|
+
create,
|
|
133
|
+
parse,
|
|
134
|
+
stringify,
|
|
135
|
+
decrypt2 as decrypt,
|
|
136
|
+
load,
|
|
137
|
+
keyfile_exports
|
|
138
|
+
};
|