@procedures/media 0.0.0-stage → 0.68.17

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-2026 Mark Russell
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/README.md CHANGED
@@ -1,3 +1,47 @@
1
- # Temporary Holding Version
1
+ # @procedures/media
2
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.
3
+ Media documents: a small JSON record first, and its files after it.
4
+
5
+ A media document has two parts. `$data` is the record of the app. `$media` is the table of its files.
6
+
7
+ ```json
8
+ {
9
+ "$media": {
10
+ "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08": {
11
+ "path": "/photos/0",
12
+ "type": "image/jpeg",
13
+ "size": 48213,
14
+ "sha256": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
15
+ "state": "pending"
16
+ }
17
+ },
18
+ "$data": { "title": "Shelf 3", "photos": ["9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"] }
19
+ }
20
+ ```
21
+
22
+ - The key of each entry is the id of the file: the SHA-256 of its bytes, as 64 lowercase hex characters.
23
+ - `path` is a JSON Pointer (RFC 6901) into `$data`. At that pointer, `$data` holds the id.
24
+ - `state` is `pending`, `partial`, `complete`, `embedded` or `linked`.
25
+ - A `partial` entry has `received`, an `embedded` entry has `base64`, and a `linked` entry has `url`.
26
+
27
+ Because the id is the hash of the bytes, an upload is idempotent. A reader can show the record before its files are complete.
28
+
29
+ ## The steps of an upload
30
+
31
+ 1. The client adds each file with `addMedia`. The function puts the id at the path in `$data`.
32
+ 2. The client sends the small document from `stripMedia`. The server stores it at once.
33
+ 3. The server answers with `missingIds`: the files that its store does not have.
34
+ 4. The client sends each missing file in the chunks of `planChunks`. After a cut, the plan has only the chunks that did not arrive.
35
+ 5. The server sets the count of each file with `withReceived`.
36
+
37
+ A small file can stay in the document (`embedMedia`), or it can be at a URL (`linkMedia`).
38
+
39
+ ## The check
40
+
41
+ `checkMediaDocument` examines an unknown value. It gives `ok(document)`, or `err(issues)` with each problem and its JSON Pointer. `assertMediaDocument` throws a `MediaDocumentError` instead.
42
+
43
+ ## Limits of this version
44
+
45
+ - The package has no hash. The caller gives the SHA-256 of each file.
46
+ - The package has only pure functions. The procedures and the store of the files come in a later version.
47
+ - The root entry has no Node.js import, so a browser can use it. The browser check of the repository holds this (`client.browser` in `package.json`).
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Base64 (RFC 4648, section 4) for the bytes of an embedded file.
3
+ *
4
+ * The decoder accepts only the canonical form: the standard alphabet, the padding, and zero bits after the last
5
+ * character. Thus each list of bytes has one text, and a document has one form. The functions use `btoa` and `atob`,
6
+ * which Node.js and each browser have (through `web.ts`).
7
+ *
8
+ * This copy is temporary. Its home is the entry `/base64` of `@mark1russell7/text`. When that entry is on npm, this
9
+ * package imports it, and this file goes.
10
+ */
11
+ /** This function gives the base64 text of the bytes. */
12
+ export declare function encodeBase64(bytes: Uint8Array): string;
13
+ /** This function gives the bytes of a canonical base64 text, or undefined for a text that is not canonical. */
14
+ export declare function decodeBase64(text: string): Uint8Array | undefined;
15
+ /** This function gives the number of bytes of a canonical base64 text, with no decode. */
16
+ export declare function base64Size(text: string): number;
17
+ //# sourceMappingURL=base64.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AASH,wDAAwD;AACxD,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAMtD;AAED,+GAA+G;AAC/G,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAOjE;AAED,0FAA0F;AAC1F,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG/C"}
package/dist/base64.js ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Base64 (RFC 4648, section 4) for the bytes of an embedded file.
3
+ *
4
+ * The decoder accepts only the canonical form: the standard alphabet, the padding, and zero bits after the last
5
+ * character. Thus each list of bytes has one text, and a document has one form. The functions use `btoa` and `atob`,
6
+ * which Node.js and each browser have (through `web.ts`).
7
+ *
8
+ * This copy is temporary. Its home is the entry `/base64` of `@mark1russell7/text`. When that entry is on npm, this
9
+ * package imports it, and this file goes.
10
+ */
11
+ import { fromBase64, toBase64 } from "./web.js";
12
+ const CANONICAL = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
13
+ /** The number of bytes that one call of `String.fromCharCode` gets. It keeps the argument list small. */
14
+ const BLOCK = 0x8000;
15
+ /** This function gives the base64 text of the bytes. */
16
+ export function encodeBase64(bytes) {
17
+ let binary = "";
18
+ for (let at = 0; at < bytes.length; at += BLOCK) {
19
+ binary += String.fromCharCode(...bytes.subarray(at, at + BLOCK));
20
+ }
21
+ return toBase64(binary);
22
+ }
23
+ /** This function gives the bytes of a canonical base64 text, or undefined for a text that is not canonical. */
24
+ export function decodeBase64(text) {
25
+ if (!CANONICAL.test(text))
26
+ return undefined;
27
+ const binary = fromBase64(text);
28
+ const bytes = new Uint8Array(binary.length);
29
+ for (let at = 0; at < binary.length; at += 1)
30
+ bytes[at] = binary.charCodeAt(at);
31
+ // A last character with bits that are not zero decodes, but it is not the canonical text of the bytes.
32
+ return encodeBase64(bytes) === text ? bytes : undefined;
33
+ }
34
+ /** This function gives the number of bytes of a canonical base64 text, with no decode. */
35
+ export function base64Size(text) {
36
+ const padding = text.endsWith("==") ? 2 : text.endsWith("=") ? 1 : 0;
37
+ return (text.length / 4) * 3 - padding;
38
+ }
39
+ //# sourceMappingURL=base64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.js","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEhD,MAAM,SAAS,GAAG,kEAAkE,CAAC;AAErF,yGAAyG;AACzG,MAAM,KAAK,GAAG,MAAM,CAAC;AAErB,wDAAwD;AACxD,MAAM,UAAU,YAAY,CAAC,KAAiB;IAC5C,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,KAAK,CAAC,MAAM,EAAE,EAAE,IAAI,KAAK,EAAE,CAAC;QAChD,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC;AAC1B,CAAC;AAED,+GAA+G;AAC/G,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,SAAS,CAAC;IAC5C,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5C,KAAK,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,IAAI,CAAC;QAAE,KAAK,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAChF,uGAAuG;IACvG,OAAO,YAAY,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AAC1D,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACrE,OAAO,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC;AACzC,CAAC"}
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Media documents: a small JSON record first, and its files after it.
3
+ *
4
+ * A media document has two parts. `$data` is the record of the app. `$media` is the table of its files: each key is the
5
+ * id of a file, the SHA-256 of its bytes as 64 lowercase hex characters. Each entry tells where the file is in `$data`
6
+ * (a JSON Pointer, RFC 6901), its media type, its size and its state. At that pointer, `$data` holds the id.
7
+ *
8
+ * The states are these:
9
+ *
10
+ * - `pending`: the store has no bytes of the file.
11
+ * - `partial`: the store has `received` bytes, more than 0 and fewer than `size`.
12
+ * - `complete`: the store has all the bytes.
13
+ * - `embedded`: the bytes are in the entry, as the canonical base64 text `base64`.
14
+ * - `linked`: the bytes are at the URL `url` (http or https).
15
+ *
16
+ * Thus a reader can show the record before its files are complete. Because the id is the hash of the bytes, an upload
17
+ * is idempotent, and a store can examine each file that it gets.
18
+ */
19
+ import { type Result } from "@mark1russell7/optional";
20
+ import { type JsonValue } from "@procedures/json";
21
+ /** The states of a file, in the order of an upload. */
22
+ export declare const MEDIA_STATES: readonly ["pending", "partial", "complete", "embedded", "linked"];
23
+ /** The state of a file. */
24
+ export type MediaState = (typeof MEDIA_STATES)[number];
25
+ /** The fields that each entry has. */
26
+ export interface MediaEntryBase {
27
+ /** The JSON Pointer of the file in `$data`. */
28
+ readonly path: string;
29
+ /** The media type (RFC 6838), in lowercase, for example `image/jpeg`. */
30
+ readonly type: string;
31
+ /** The number of bytes of the file. */
32
+ readonly size: number;
33
+ /** The SHA-256 of the bytes, as 64 lowercase hex characters. It is equal to the key of the entry. */
34
+ readonly sha256: string;
35
+ }
36
+ /** An entry of the media table. The state selects the other fields. */
37
+ export type MediaEntry = (MediaEntryBase & {
38
+ readonly state: "pending";
39
+ }) | (MediaEntryBase & {
40
+ readonly state: "partial";
41
+ readonly received: number;
42
+ }) | (MediaEntryBase & {
43
+ readonly state: "complete";
44
+ }) | (MediaEntryBase & {
45
+ readonly state: "embedded";
46
+ readonly base64: string;
47
+ }) | (MediaEntryBase & {
48
+ readonly state: "linked";
49
+ readonly url: string;
50
+ });
51
+ /** The media table: each file by its id. */
52
+ export type MediaTable = Readonly<Record<string, MediaEntry>>;
53
+ /** A media document: the record of the app in `$data`, and the table of its files in `$media`. */
54
+ export interface MediaDocument<D extends JsonValue = JsonValue> {
55
+ readonly $media: MediaTable;
56
+ readonly $data: D;
57
+ }
58
+ /** The kind of a problem in a media document. */
59
+ export type MediaIssueCode = "NOT_OBJECT" | "MISSING_FIELD" | "UNKNOWN_FIELD" | "NOT_JSON" | "BAD_ID" | "ID_MISMATCH" | "BAD_TYPE" | "BAD_SIZE" | "BAD_STATE" | "BAD_PATH" | "PATH_NOT_FOUND" | "PATH_NOT_ID" | "PATH_SHARED" | "BAD_RECEIVED" | "BAD_BASE64" | "SIZE_MISMATCH" | "BAD_URL";
60
+ /** A problem in a media document. `at` is the JSON Pointer of the bad value in the document. */
61
+ export interface MediaIssue {
62
+ readonly at: string;
63
+ readonly code: MediaIssueCode;
64
+ readonly message: string;
65
+ }
66
+ /** This function tells if a text is a media id: 64 lowercase hex characters. */
67
+ export declare function isMediaId(text: unknown): text is string;
68
+ /** This function tells if a text is a media type in lowercase, with optional parameters (`text/plain; charset=utf-8`). */
69
+ export declare function isMediaType(text: unknown): text is string;
70
+ /** This function tells if a text is an absolute http or https URL. */
71
+ export declare function isMediaUrl(text: unknown): text is string;
72
+ /**
73
+ * This function examines a value as a media document. It gives the document, or each problem that it found. A problem
74
+ * in one entry does not stop the examination of the other entries.
75
+ */
76
+ export declare function checkMediaDocument(value: unknown): Result<MediaDocument, readonly MediaIssue[]>;
77
+ /** This function gives the document, or throws a `MediaDocumentError` with each problem. */
78
+ export declare function assertMediaDocument(value: unknown): MediaDocument;
79
+ /** The error of a value that is not a media document. It has the list of the problems. */
80
+ export declare class MediaDocumentError extends Error {
81
+ readonly issues: readonly MediaIssue[];
82
+ constructor(issues: readonly MediaIssue[]);
83
+ }
84
+ //# sourceMappingURL=document.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAmD,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAInG,uDAAuD;AACvD,eAAO,MAAM,YAAY,YAAI,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,CAAU,CAAC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ED,iDAAiD;AACjD,MAAM,MAAM,cAAc,GACtB,YAAY,GACZ,eAAe,GACf,eAAe,GACf,UAAU,GACV,QAAQ,GACR,aAAa,GACb,UAAU,GACV,UAAU,GACV,WAAW,GACX,UAAU,GACV,gBAAgB,GAChB,aAAa,GACb,aAAa,GACb,cAAc,GACd,YAAY,GACZ,eAAe,GACf,SAAS,CAAC;AAEd,gGAAgG;AAChG,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAID,gFAAgF;AAChF,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,MAAM,CAEvD;AAQD,0HAA0H;AAC1H,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,MAAM,CAEzD;AAED,sEAAsE;AACtE,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,MAAM,CAQxD;AA2BD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,aAAa,EAAE,SAAS,UAAU,EAAE,CAAC,CAiC/F;AAoED,4FAA4F;AAC5F,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAIjE;AAED,0FAA0F;AAC1F,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,MAAM,EAAE,SAAS,UAAU,EAAE,CAAC;IACvC,YAAY,MAAM,EAAE,SAAS,UAAU,EAAE,EAMxC;CACF"}
@@ -0,0 +1,212 @@
1
+ /**
2
+ * Media documents: a small JSON record first, and its files after it.
3
+ *
4
+ * A media document has two parts. `$data` is the record of the app. `$media` is the table of its files: each key is the
5
+ * id of a file, the SHA-256 of its bytes as 64 lowercase hex characters. Each entry tells where the file is in `$data`
6
+ * (a JSON Pointer, RFC 6901), its media type, its size and its state. At that pointer, `$data` holds the id.
7
+ *
8
+ * The states are these:
9
+ *
10
+ * - `pending`: the store has no bytes of the file.
11
+ * - `partial`: the store has `received` bytes, more than 0 and fewer than `size`.
12
+ * - `complete`: the store has all the bytes.
13
+ * - `embedded`: the bytes are in the entry, as the canonical base64 text `base64`.
14
+ * - `linked`: the bytes are at the URL `url` (http or https).
15
+ *
16
+ * Thus a reader can show the record before its files are complete. Because the id is the hash of the bytes, an upload
17
+ * is idempotent, and a store can examine each file that it gets.
18
+ */
19
+ import { ok, err } from "@mark1russell7/optional";
20
+ import { assertJson, formatPointer, lookup, parsePointer } from "@procedures/json";
21
+ import { base64Size, decodeBase64 } from "./base64.js";
22
+ import { urlProtocol } from "./web.js";
23
+ /** The states of a file, in the order of an upload. */
24
+ export const MEDIA_STATES = ["pending", "partial", "complete", "embedded", "linked"];
25
+ const ID = /^[0-9a-f]{64}$/;
26
+ /** This function tells if a text is a media id: 64 lowercase hex characters. */
27
+ export function isMediaId(text) {
28
+ return typeof text === "string" && ID.test(text);
29
+ }
30
+ // RFC 6838, section 4.2: a name has 1 to 127 characters, and starts with a letter or a digit.
31
+ const NAME = "[a-z0-9][a-z0-9!#$&^_.+-]{0,126}";
32
+ const TOKEN = "[a-z0-9!#$%&'*+.^_`|~-]+";
33
+ const QUOTED = '"(?:[^"\\\\\\r\\n]|\\\\.)*"';
34
+ const MEDIA_TYPE = new RegExp(`^${NAME}/${NAME}(?:; ?${TOKEN}=(?:[A-Za-z0-9!#$%&'*+.^_\`|~-]+|${QUOTED}))*$`);
35
+ /** This function tells if a text is a media type in lowercase, with optional parameters (`text/plain; charset=utf-8`). */
36
+ export function isMediaType(text) {
37
+ return typeof text === "string" && MEDIA_TYPE.test(text);
38
+ }
39
+ /** This function tells if a text is an absolute http or https URL. */
40
+ export function isMediaUrl(text) {
41
+ if (typeof text !== "string")
42
+ return false;
43
+ try {
44
+ const protocol = urlProtocol(text);
45
+ return protocol === "https:" || protocol === "http:";
46
+ }
47
+ catch {
48
+ return false;
49
+ }
50
+ }
51
+ /** The fields of each state, after the base fields. */
52
+ const STATE_FIELDS = {
53
+ pending: [],
54
+ partial: ["received"],
55
+ complete: [],
56
+ embedded: ["base64"],
57
+ linked: ["url"],
58
+ };
59
+ const BASE_FIELDS = ["path", "type", "size", "sha256", "state"];
60
+ function isObject(value) {
61
+ return value !== null && typeof value === "object" && !Array.isArray(value);
62
+ }
63
+ function isState(value) {
64
+ return typeof value === "string" && MEDIA_STATES.includes(value);
65
+ }
66
+ /** The text of an unknown value in a message. */
67
+ function shown(value) {
68
+ const text = typeof value === "string" ? JSON.stringify(value) : String(value);
69
+ return text.length > 80 ? `${text.slice(0, 77)}...` : text;
70
+ }
71
+ /**
72
+ * This function examines a value as a media document. It gives the document, or each problem that it found. A problem
73
+ * in one entry does not stop the examination of the other entries.
74
+ */
75
+ export function checkMediaDocument(value) {
76
+ const issues = [];
77
+ const add = (tokens, code, message) => {
78
+ issues.push({ at: formatPointer(tokens), code, message });
79
+ };
80
+ if (!isObject(value)) {
81
+ add([], "NOT_OBJECT", "A media document is an object with the fields $media and $data");
82
+ return err(issues);
83
+ }
84
+ for (const key of Object.keys(value)) {
85
+ if (key !== "$media" && key !== "$data")
86
+ add([key], "UNKNOWN_FIELD", `A media document has only $media and $data, not ${shown(key)}`);
87
+ }
88
+ if (!(Object.hasOwn(value, "$data")))
89
+ add(["$data"], "MISSING_FIELD", "A media document needs the field $data");
90
+ if (!(Object.hasOwn(value, "$media")))
91
+ add(["$media"], "MISSING_FIELD", "A media document needs the field $media");
92
+ const data = value["$data"];
93
+ let dataIsJson = Object.hasOwn(value, "$data");
94
+ if (dataIsJson) {
95
+ try {
96
+ assertJson(data, "$data");
97
+ }
98
+ catch (error) {
99
+ dataIsJson = false;
100
+ add(["$data"], "NOT_JSON", error instanceof Error ? error.message : String(error));
101
+ }
102
+ }
103
+ const media = value["$media"];
104
+ if (Object.hasOwn(value, "$media") && !isObject(media))
105
+ add(["$media"], "NOT_OBJECT", "The field $media is an object: each file by its id");
106
+ if (isObject(media)) {
107
+ const owners = new Map();
108
+ for (const [id, entry] of Object.entries(media)) {
109
+ checkEntry(id, entry, dataIsJson ? data : undefined, owners, add);
110
+ }
111
+ }
112
+ return issues.length === 0 ? ok(value) : err(issues);
113
+ }
114
+ function checkEntry(id, entry, data, owners, add) {
115
+ const at = ["$media", id];
116
+ if (!isMediaId(id))
117
+ add(at, "BAD_ID", `The id ${shown(id)} is not a SHA-256 in 64 lowercase hex characters`);
118
+ if (!isObject(entry)) {
119
+ add(at, "NOT_OBJECT", "An entry of $media is an object with path, type, size, sha256 and state");
120
+ return;
121
+ }
122
+ for (const field of BASE_FIELDS) {
123
+ if (!Object.hasOwn(entry, field))
124
+ add([...at, field], "MISSING_FIELD", `An entry of $media needs the field ${field}`);
125
+ }
126
+ const { path, type, size, sha256, state } = entry;
127
+ if (Object.hasOwn(entry, "sha256") && sha256 !== id)
128
+ add([...at, "sha256"], "ID_MISMATCH", "The field sha256 of an entry is equal to its id");
129
+ if (Object.hasOwn(entry, "type") && !isMediaType(type))
130
+ add([...at, "type"], "BAD_TYPE", `The type ${shown(type)} is not a media type in lowercase, for example image/jpeg`);
131
+ const sizeIsGood = Number.isSafeInteger(size) && size >= 0;
132
+ if (Object.hasOwn(entry, "size") && !sizeIsGood)
133
+ add([...at, "size"], "BAD_SIZE", `The size ${shown(size)} is not a safe integer of 0 or more`);
134
+ if (Object.hasOwn(entry, "state") && !isState(state))
135
+ add([...at, "state"], "BAD_STATE", `The state ${shown(state)} is not one of ${MEDIA_STATES.join(", ")}`);
136
+ if (Object.hasOwn(entry, "path"))
137
+ checkPath(id, path, data, owners, [...at, "path"], add);
138
+ if (!isState(state))
139
+ return;
140
+ const allowed = new Set([...BASE_FIELDS, ...STATE_FIELDS[state]]);
141
+ for (const field of Object.keys(entry)) {
142
+ if (!allowed.has(field))
143
+ add([...at, field], "UNKNOWN_FIELD", `An entry in the state ${state} has no field ${shown(field)}`);
144
+ }
145
+ for (const field of STATE_FIELDS[state]) {
146
+ if (!Object.hasOwn(entry, field))
147
+ add([...at, field], "MISSING_FIELD", `An entry in the state ${state} needs the field ${field}`);
148
+ }
149
+ if (state === "partial" && Object.hasOwn(entry, "received")) {
150
+ const received = entry["received"];
151
+ const good = Number.isSafeInteger(received) && received > 0 && (!sizeIsGood || received < size);
152
+ if (!good)
153
+ add([...at, "received"], "BAD_RECEIVED", `The count received ${shown(received)} of a partial file is more than 0 and less than its size`);
154
+ }
155
+ if (state === "embedded" && Object.hasOwn(entry, "base64")) {
156
+ const text = entry["base64"];
157
+ if (typeof text !== "string" || decodeBase64(text) === undefined) {
158
+ add([...at, "base64"], "BAD_BASE64", "The field base64 is the canonical base64 text of the bytes (RFC 4648, with padding)");
159
+ }
160
+ else if (sizeIsGood && base64Size(text) !== size) {
161
+ add([...at, "base64"], "SIZE_MISMATCH", `The base64 text has ${base64Size(text)} bytes, but the size is ${String(size)}`);
162
+ }
163
+ }
164
+ if (state === "linked" && Object.hasOwn(entry, "url") && !isMediaUrl(entry["url"])) {
165
+ add([...at, "url"], "BAD_URL", `The url ${shown(entry["url"])} is not an absolute http or https URL`);
166
+ }
167
+ }
168
+ function checkPath(id, path, data, owners, at, add) {
169
+ if (typeof path !== "string" || path.startsWith("#")) {
170
+ add(at, "BAD_PATH", `The path ${shown(path)} is not a JSON Pointer: it is "" or starts with "/"`);
171
+ return;
172
+ }
173
+ let tokens;
174
+ try {
175
+ tokens = parsePointer(path, "path");
176
+ }
177
+ catch (error) {
178
+ add(at, "BAD_PATH", error instanceof Error ? error.message : String(error));
179
+ return;
180
+ }
181
+ const owner = owners.get(path);
182
+ if (owner !== undefined)
183
+ add(at, "PATH_SHARED", `The path ${shown(path)} is also the path of the file ${owner}`);
184
+ else
185
+ owners.set(path, id);
186
+ if (data === undefined)
187
+ return;
188
+ const found = lookup(data, tokens);
189
+ if (!found.found)
190
+ add(at, "PATH_NOT_FOUND", `The path ${shown(path)} is not in $data`);
191
+ else if (found.value !== id)
192
+ add(at, "PATH_NOT_ID", `At the path ${shown(path)}, $data holds ${shown(found.value)}, not the id of the file`);
193
+ }
194
+ /** This function gives the document, or throws a `MediaDocumentError` with each problem. */
195
+ export function assertMediaDocument(value) {
196
+ const result = checkMediaDocument(value);
197
+ if (result.tag === "err")
198
+ throw new MediaDocumentError(result.error);
199
+ return result.value;
200
+ }
201
+ /** The error of a value that is not a media document. It has the list of the problems. */
202
+ export class MediaDocumentError extends Error {
203
+ issues;
204
+ constructor(issues) {
205
+ const first = issues[0];
206
+ const more = issues.length > 1 ? ` (and ${issues.length - 1} more)` : "";
207
+ super(first === undefined ? "The value is not a media document" : `${first.at || "/"}: ${first.message}${more}`);
208
+ this.name = "MediaDocumentError";
209
+ this.issues = issues;
210
+ }
211
+ }
212
+ //# sourceMappingURL=document.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document.js","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,EAAE,EAAE,GAAG,EAAe,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAkB,MAAM,kBAAkB,CAAC;AACnG,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,uDAAuD;AACvD,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,CAAU,CAAC;AA6D9F,MAAM,EAAE,GAAG,gBAAgB,CAAC;AAE5B,gFAAgF;AAChF,MAAM,UAAU,SAAS,CAAC,IAAa;IACrC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACnD,CAAC;AAED,8FAA8F;AA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AAC,EAAE,IAAI,GAAG,KAAK,KAAK,CAAC,OAAO,GAAG,IAAI,EAAE,CAAC,CAAC;QACjH,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF"}
@@ -0,0 +1,7 @@
1
+ export * from "./module.js";
2
+ export * from "./types.js";
3
+ export { MEDIA_STATES, assertMediaDocument, checkMediaDocument, isMediaId, isMediaType, isMediaUrl, MediaDocumentError, type MediaDocument, type MediaEntry, type MediaEntryBase, type MediaIssue, type MediaIssueCode, type MediaState, type MediaTable, } from "./document.js";
4
+ export { DEFAULT_MAX_EMBEDDED_BYTES, addMedia, embedMedia, isStored, linkMedia, mediaIds, missingIds, stripMedia, withReceived, type EmbedOptions, type NewMedia, type StrippedMedia, } from "./transfer.js";
5
+ export { addRange, isByteRange, missingRanges, normalizeRanges, planChunks, rangeBytes, type ByteRange } from "./ranges.js";
6
+ export { base64Size, decodeBase64, encodeBase64 } from "./base64.js";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAG3B,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,SAAS,EACT,WAAW,EACX,UAAU,EACV,kBAAkB,EAClB,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,UAAU,GAChB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,0BAA0B,EAC1B,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,UAAU,EACV,UAAU,EACV,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,aAAa,GACnB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAC5H,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ // The root registers nothing: import "<package>/register", or give the module to createRegistry()
2
+ export * from "./module.js";
3
+ export * from "./types.js";
4
+ // The pure core of media documents. It has no Node.js import, so it runs in a browser too.
5
+ export { MEDIA_STATES, assertMediaDocument, checkMediaDocument, isMediaId, isMediaType, isMediaUrl, MediaDocumentError, } from "./document.js";
6
+ export { DEFAULT_MAX_EMBEDDED_BYTES, addMedia, embedMedia, isStored, linkMedia, mediaIds, missingIds, stripMedia, withReceived, } from "./transfer.js";
7
+ export { addRange, isByteRange, missingRanges, normalizeRanges, planChunks, rangeBytes } from "./ranges.js";
8
+ export { base64Size, decodeBase64, encodeBase64 } from "./base64.js";
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAE3B,2FAA2F;AAC3F,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,SAAS,EACT,WAAW,EACX,UAAU,EACV,kBAAkB,GAQnB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,0BAA0B,EAC1B,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,UAAU,EACV,UAAU,EACV,YAAY,GAIb,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAkB,MAAM,aAAa,CAAC;AAC5H,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The procedures of @procedures/media as data. Importing this file registers nothing.
3
+ * The package has no procedures yet, so it has no ./register entry. The store and the procedures of the peer come in MD1 PR 2.
4
+ *
5
+ * Scaffold a procedure with: mark procedure new <name> --path packages/media
6
+ */
7
+ import { type ProcedureModule, type ProcedureRegistry } from "@procedures/client";
8
+ export declare const mediaModule: ProcedureModule;
9
+ export default mediaModule;
10
+ /**
11
+ * This function registers the procedures of @procedures/media in a registry. The default is the
12
+ * global registry.
13
+ */
14
+ export declare function registerMediaProcedures(registry?: ProcedureRegistry): void;
15
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAqD,KAAK,eAAe,EAAE,KAAK,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAErI,eAAO,MAAM,WAAW,EAAE,eAAuD,CAAC;eAEnE,WAAW;AAE1B;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,GAAE,iBAAsC,GAAG,IAAI,CAE9F"}
package/dist/module.js ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The procedures of @procedures/media as data. Importing this file registers nothing.
3
+ * The package has no procedures yet, so it has no ./register entry. The store and the procedures of the peer come in MD1 PR 2.
4
+ *
5
+ * Scaffold a procedure with: mark procedure new <name> --path packages/media
6
+ */
7
+ import { defineModule, registerModules, PROCEDURE_REGISTRY } from "@procedures/client";
8
+ export const mediaModule = defineModule("@procedures/media", []);
9
+ export default mediaModule;
10
+ /**
11
+ * This function registers the procedures of @procedures/media in a registry. The default is the
12
+ * global registry.
13
+ */
14
+ export function registerMediaProcedures(registry = PROCEDURE_REGISTRY) {
15
+ registerModules(registry, [mediaModule]);
16
+ }
17
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.js","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAgD,MAAM,oBAAoB,CAAC;AAErI,MAAM,CAAC,MAAM,WAAW,GAAoB,YAAY,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAC;AAElF,eAAe,WAAW,CAAC;AAE3B;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CAAC,QAAQ,GAAsB,kBAAkB;IACtF,eAAe,CAAC,QAAQ,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The byte ranges of a file that a store has, and the chunk plan of the bytes that it does not have.
3
+ *
4
+ * Chunks go in parallel, so they arrive in any order. Thus the store keeps a set of ranges, not one count. A range is
5
+ * half-open: `start` is the first byte, and `end` is the byte after the last. A range set is in its normal form when
6
+ * its ranges are sorted, not empty, and have a gap between each two. Each function here gives the normal form.
7
+ *
8
+ * This copy is temporary. Its home is `RangeSet` of `@mark1russell7/collections`, a set of integer intervals. When
9
+ * that set is on npm, this package imports it, and this file goes.
10
+ */
11
+ /** A half-open range of bytes: from `start` to `end - 1`. */
12
+ export interface ByteRange {
13
+ readonly start: number;
14
+ readonly end: number;
15
+ }
16
+ /** This function tells if a value is a range with two safe integers and `0 <= start <= end`. */
17
+ export declare function isByteRange(value: unknown): value is ByteRange;
18
+ /** This function gives the normal form of a list of ranges. The ranges that touch or overlap become one range. */
19
+ export declare function normalizeRanges(ranges: readonly ByteRange[]): ByteRange[];
20
+ /** This function adds one range to a range set, and gives the normal form. */
21
+ export declare function addRange(ranges: readonly ByteRange[], range: ByteRange): ByteRange[];
22
+ /** This function gives the number of bytes in a range set. An overlap counts once. */
23
+ export declare function rangeBytes(ranges: readonly ByteRange[]): number;
24
+ /** This function gives the ranges of `0` to `size` that are not in the range set. A byte at `size` or after is ignored. */
25
+ export declare function missingRanges(size: number, ranges: readonly ByteRange[]): ByteRange[];
26
+ /**
27
+ * This function gives the chunks that send the missing bytes of a file. Each chunk is in one cell of the grid of
28
+ * `chunkSize`, so a chunk after a cut has the same bounds as before the cut. Each chunk has `chunkSize` bytes or fewer.
29
+ */
30
+ export declare function planChunks(size: number, chunkSize: number, received?: readonly ByteRange[]): ByteRange[];
31
+ //# sourceMappingURL=ranges.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ranges.d.ts","sourceRoot":"","sources":["../src/ranges.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,6DAA6D;AAC7D,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,gGAAgG;AAChG,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAI9D;AAQD,kHAAkH;AAClH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,GAAG,SAAS,EAAE,CAazE;AAED,8EAA8E;AAC9E,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,EAAE,KAAK,EAAE,SAAS,GAAG,SAAS,EAAE,CAEpF;AAED,sFAAsF;AACtF,wBAAgB,UAAU,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,GAAG,MAAM,CAE/D;AAMD,2HAA2H;AAC3H,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,SAAS,EAAE,GAAG,SAAS,EAAE,CAWrF;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,GAAE,SAAS,SAAS,EAAO,GAAG,SAAS,EAAE,CAa5G"}