@secrecy/lib 1.83.3 → 1.84.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.
|
@@ -28,15 +28,17 @@ export const downloadDataLinkSchema = z.union([
|
|
|
28
28
|
downloadDataLinkS3Schema,
|
|
29
29
|
downloadDataLinkLiteSchema,
|
|
30
30
|
]);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
export async function fetchDataLinkMetadata(dataLinkSlug, dataUrl) {
|
|
32
|
+
let baseUrl = dataUrl ?? 'https://data.secrecy.tech/';
|
|
33
|
+
baseUrl = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
|
|
34
|
+
const rawDataLinkRes = await fetch(`${baseUrl}${dataLinkSlug}/json`, {
|
|
35
|
+
// ! disable cookies for now
|
|
36
|
+
// credentials: 'include',
|
|
37
37
|
}).catch((err) => {
|
|
38
38
|
console.error(err);
|
|
39
|
-
throw new Error(`Unable to fetch "${`${
|
|
39
|
+
throw new Error(`Unable to fetch "${`${baseUrl}${dataLinkSlug}/json"`}`, {
|
|
40
|
+
cause: err,
|
|
41
|
+
});
|
|
40
42
|
});
|
|
41
43
|
const rawDataLink = await rawDataLinkRes.json().catch((err) => {
|
|
42
44
|
console.error(err);
|
|
@@ -46,28 +48,33 @@ export async function downloadDataFromLink(opts) {
|
|
|
46
48
|
if (dataLink.error) {
|
|
47
49
|
throw new Error(`Should not happen!`, { cause: dataLink.error });
|
|
48
50
|
}
|
|
49
|
-
|
|
51
|
+
return dataLink.data;
|
|
52
|
+
}
|
|
53
|
+
// TODO: Add an opt header for authed users!
|
|
54
|
+
export async function downloadDataFromLink(opts) {
|
|
55
|
+
const dataLink = await fetchDataLinkMetadata(opts.dataLinkSlug, opts.dataUrl);
|
|
56
|
+
if (dataLink.isEncrypted && !opts.crypto) {
|
|
50
57
|
throw new Error('Unable to read encrypted data without password!');
|
|
51
58
|
}
|
|
52
59
|
const bytes = await buildBytesFromDataLink({
|
|
53
|
-
dataLink: dataLink
|
|
54
|
-
totalBytes: Number(dataLink.
|
|
60
|
+
dataLink: dataLink,
|
|
61
|
+
totalBytes: Number(dataLink.size),
|
|
55
62
|
signal: opts.signal,
|
|
56
63
|
onDownloadProgress: opts.downloadProgress,
|
|
57
64
|
});
|
|
58
|
-
if (!dataLink.
|
|
65
|
+
if (!dataLink.isEncrypted) {
|
|
59
66
|
return bytes;
|
|
60
67
|
}
|
|
61
68
|
if (!opts.crypto) {
|
|
62
69
|
throw new Error('Unable to read encrypted data without password!');
|
|
63
70
|
}
|
|
64
71
|
try {
|
|
65
|
-
const salt = sodium.crypto_generichash(sodium.crypto_pwhash_SALTBYTES, opts.crypto.password + dataLink.
|
|
72
|
+
const salt = sodium.crypto_generichash(sodium.crypto_pwhash_SALTBYTES, opts.crypto.password + dataLink.md5);
|
|
66
73
|
const derivedPassword = derivePassword(opts.crypto.password, salt);
|
|
67
74
|
const decryptedSharingDataKey = decryptSecretBox(sodium.from_hex(opts.crypto.key), derivedPassword);
|
|
68
75
|
const decryptedBytes = await decrypt(decryptedSharingDataKey, bytes, opts.decryptProgress, opts.signal);
|
|
69
76
|
const md5Content = await md5(decryptedBytes);
|
|
70
|
-
if (md5Content !== dataLink.
|
|
77
|
+
if (md5Content !== dataLink.md5) {
|
|
71
78
|
throw new Error(`Content does not match`);
|
|
72
79
|
}
|
|
73
80
|
return decompress(decryptedBytes);
|
|
@@ -32,6 +32,27 @@ export type DownloadDataFromLinkOptions = {
|
|
|
32
32
|
downloadProgress?: (progress: Progress) => void;
|
|
33
33
|
signal?: AbortSignal;
|
|
34
34
|
};
|
|
35
|
+
export declare function fetchDataLinkMetadata(dataLinkSlug: string, dataUrl?: string): Promise<{
|
|
36
|
+
name: string;
|
|
37
|
+
md5: string;
|
|
38
|
+
md5Encrypted: string | null;
|
|
39
|
+
size: bigint;
|
|
40
|
+
parts: {
|
|
41
|
+
order: number;
|
|
42
|
+
md5: string;
|
|
43
|
+
contentUrl: string;
|
|
44
|
+
}[];
|
|
45
|
+
mime: string;
|
|
46
|
+
isEncrypted: boolean;
|
|
47
|
+
} | {
|
|
48
|
+
name: string;
|
|
49
|
+
md5: string;
|
|
50
|
+
md5Encrypted: string | null;
|
|
51
|
+
size: bigint;
|
|
52
|
+
bytes: string;
|
|
53
|
+
mime: string;
|
|
54
|
+
isEncrypted: boolean;
|
|
55
|
+
}>;
|
|
35
56
|
export declare function downloadDataFromLink(opts: DownloadDataFromLinkOptions & {
|
|
36
57
|
dataUrl?: string;
|
|
37
58
|
}): Promise<Uint8Array<ArrayBuffer>>;
|
package/package.json
CHANGED