@yaks/blob 0.0.0-stage → 0.2.36

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.
Files changed (172) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +946 -2
  3. package/_dist/accepts.d.ts +38 -0
  4. package/_dist/accepts.d.ts.map +1 -0
  5. package/_dist/artifact.d.ts +29 -0
  6. package/_dist/artifact.d.ts.map +1 -0
  7. package/_dist/attachment.d.ts +53 -0
  8. package/_dist/attachment.d.ts.map +1 -0
  9. package/_dist/backend.d.ts +20 -0
  10. package/_dist/backend.d.ts.map +1 -0
  11. package/_dist/broken.d.ts +24 -0
  12. package/_dist/broken.d.ts.map +1 -0
  13. package/_dist/browser.d.ts +27 -0
  14. package/_dist/browser.d.ts.map +1 -0
  15. package/_dist/compare.d.ts +4 -0
  16. package/_dist/compare.d.ts.map +1 -0
  17. package/_dist/content_type.d.ts +20 -0
  18. package/_dist/content_type.d.ts.map +1 -0
  19. package/_dist/graph.d.ts +13 -0
  20. package/_dist/graph.d.ts.map +1 -0
  21. package/_dist/hydrate.d.ts +36 -0
  22. package/_dist/hydrate.d.ts.map +1 -0
  23. package/_dist/identify.d.ts +7 -0
  24. package/_dist/identify.d.ts.map +1 -0
  25. package/_dist/image.d.ts +37 -0
  26. package/_dist/image.d.ts.map +1 -0
  27. package/_dist/keywords.d.ts +10 -0
  28. package/_dist/keywords.d.ts.map +1 -0
  29. package/_dist/mime.d.ts +2 -0
  30. package/_dist/mime.d.ts.map +1 -0
  31. package/_dist/mod.d.ts +97 -0
  32. package/_dist/mod.d.ts.map +1 -0
  33. package/_dist/object-put.d.ts +2 -0
  34. package/_dist/object-put.d.ts.map +1 -0
  35. package/_dist/object.d.ts +175 -0
  36. package/_dist/object.d.ts.map +1 -0
  37. package/_dist/parts.d.ts +91 -0
  38. package/_dist/parts.d.ts.map +1 -0
  39. package/_dist/plugin.d.ts +38 -0
  40. package/_dist/plugin.d.ts.map +1 -0
  41. package/_dist/png.d.ts +14 -0
  42. package/_dist/png.d.ts.map +1 -0
  43. package/_dist/props.d.ts +20 -0
  44. package/_dist/props.d.ts.map +1 -0
  45. package/_dist/representation.d.ts +17 -0
  46. package/_dist/representation.d.ts.map +1 -0
  47. package/_dist/representation_rules.d.ts +3 -0
  48. package/_dist/representation_rules.d.ts.map +1 -0
  49. package/_dist/routes.d.ts +65 -0
  50. package/_dist/routes.d.ts.map +1 -0
  51. package/_dist/serve.d.ts +29 -0
  52. package/_dist/serve.d.ts.map +1 -0
  53. package/_dist/sha256.d.ts +26 -0
  54. package/_dist/sha256.d.ts.map +1 -0
  55. package/_dist/sqlite.d.ts +57 -0
  56. package/_dist/sqlite.d.ts.map +1 -0
  57. package/_dist/store.d.ts +41 -0
  58. package/_dist/store.d.ts.map +1 -0
  59. package/_dist/stream.d.ts +91 -0
  60. package/_dist/stream.d.ts.map +1 -0
  61. package/_dist/upload.d.ts +114 -0
  62. package/_dist/upload.d.ts.map +1 -0
  63. package/_dist/value.d.ts +62 -0
  64. package/_dist/value.d.ts.map +1 -0
  65. package/_dist/vocab.d.ts +13 -0
  66. package/_dist/vocab.d.ts.map +1 -0
  67. package/accepts.js +76 -0
  68. package/accepts.js.map +1 -0
  69. package/accepts.ts +95 -0
  70. package/artifact.js +50 -0
  71. package/artifact.js.map +1 -0
  72. package/artifact.ts +93 -0
  73. package/attachment.js +190 -0
  74. package/attachment.js.map +1 -0
  75. package/attachment.ts +201 -0
  76. package/backend.js +37 -0
  77. package/backend.js.map +1 -0
  78. package/backend.ts +44 -0
  79. package/broken.js +278 -0
  80. package/broken.js.map +1 -0
  81. package/broken.ts +217 -0
  82. package/browser.js +124 -0
  83. package/browser.js.map +1 -0
  84. package/browser.ts +179 -0
  85. package/compare.js +82 -0
  86. package/compare.js.map +1 -0
  87. package/compare.ts +90 -0
  88. package/content_type.js +43 -0
  89. package/content_type.js.map +1 -0
  90. package/content_type.ts +58 -0
  91. package/deno.json +1 -0
  92. package/fixed.js +53 -0
  93. package/fixed.js.map +1 -0
  94. package/fixed.ts +61 -0
  95. package/graph.js +15 -0
  96. package/graph.js.map +1 -0
  97. package/graph.ts +22 -0
  98. package/hydrate.js +69 -0
  99. package/hydrate.js.map +1 -0
  100. package/hydrate.ts +72 -0
  101. package/identify.js +38 -0
  102. package/identify.js.map +1 -0
  103. package/identify.ts +50 -0
  104. package/image.js +111 -0
  105. package/image.js.map +1 -0
  106. package/image.ts +120 -0
  107. package/keywords.js +28 -0
  108. package/keywords.js.map +1 -0
  109. package/keywords.ts +28 -0
  110. package/meta/blob.vocab.json +17 -0
  111. package/mime.js +72 -0
  112. package/mime.js.map +1 -0
  113. package/mime.ts +73 -0
  114. package/mod.js +97 -0
  115. package/mod.js.map +1 -0
  116. package/mod.ts +115 -0
  117. package/object-put.js +90 -0
  118. package/object-put.js.map +1 -0
  119. package/object-put.ts +114 -0
  120. package/object.js +369 -0
  121. package/object.js.map +1 -0
  122. package/object.ts +592 -0
  123. package/package.json +84 -4
  124. package/parts.js +354 -0
  125. package/parts.js.map +1 -0
  126. package/parts.ts +423 -0
  127. package/plugin.js +158 -0
  128. package/plugin.js.map +1 -0
  129. package/plugin.ts +184 -0
  130. package/png.js +74 -0
  131. package/png.js.map +1 -0
  132. package/png.ts +58 -0
  133. package/props.js +32 -0
  134. package/props.js.map +1 -0
  135. package/props.ts +47 -0
  136. package/purge.js +65 -0
  137. package/purge.js.map +1 -0
  138. package/purge.ts +72 -0
  139. package/representation.js +34 -0
  140. package/representation.js.map +1 -0
  141. package/representation.ts +44 -0
  142. package/representation_rules.js +44 -0
  143. package/representation_rules.js.map +1 -0
  144. package/representation_rules.ts +57 -0
  145. package/routes.js +454 -0
  146. package/routes.js.map +1 -0
  147. package/routes.ts +575 -0
  148. package/serve.js +196 -0
  149. package/serve.js.map +1 -0
  150. package/serve.ts +242 -0
  151. package/sha256.js +185 -0
  152. package/sha256.js.map +1 -0
  153. package/sha256.ts +210 -0
  154. package/sqlite.js +201 -0
  155. package/sqlite.js.map +1 -0
  156. package/sqlite.ts +250 -0
  157. package/store.js +40 -0
  158. package/store.js.map +1 -0
  159. package/store.ts +93 -0
  160. package/stream.js +203 -0
  161. package/stream.js.map +1 -0
  162. package/stream.ts +256 -0
  163. package/upload.js +256 -0
  164. package/upload.js.map +1 -0
  165. package/upload.ts +404 -0
  166. package/value.js +234 -0
  167. package/value.js.map +1 -0
  168. package/value.ts +281 -0
  169. package/vocab.js +27 -0
  170. package/vocab.js.map +1 -0
  171. package/vocab.json +123 -0
  172. package/vocab.ts +32 -0
package/attachment.js ADDED
@@ -0,0 +1,190 @@
1
+ /**
2
+ * An attachment is one use of immutable bytes, beside the message they came
3
+ * with: the bytes' `artifact` row, named by their address, and an
4
+ * `attachment` row for each message that carried them, named by that message
5
+ * and its place there. Sending the same bytes again with another message
6
+ * keeps both names; writing one message's files again patches the same rows.
7
+ *
8
+ * ```ts
9
+ * import { equal } from '@yaks/testing'
10
+ * import { attachmentOf, attachmentRows } from '@yaks/blob/attachment'
11
+ *
12
+ * let [bytes, use] = attachmentRows('m1', [
13
+ * { name: 'notes.pdf', media_type: 'application/pdf', address: 'c0ffee' },
14
+ * ])
15
+ * equal(bytes, {
16
+ * entity: { eid: 'c0ffee' },
17
+ * artifact: { address: 'c0ffee', media_type: 'application/pdf' },
18
+ * })
19
+ * equal(use.entity.eid, attachmentOf('m1', 0))
20
+ * ```
21
+ *
22
+ * What a reader is shown of them is {@link attachmentsSeen}: the attachment
23
+ * rows a clause admits and the artifacts they name, and nothing else in the
24
+ * graph, even where a query walks a reference.
25
+ *
26
+ * @module
27
+ */ import { graph } from "@yaks/graph";
28
+ import { derivedEid } from "@yaks/graph/identity";
29
+ import { tier } from "@yaks/graph/tools";
30
+ import { ram } from "@yaks/ram";
31
+ import { and, eq, fields, list, or, parse, present } from "@yaks/query/core";
32
+ import { mediaTypeOf } from "./image.js";
33
+ /** The attachment's stable identity within the message that carried it. The
34
+ * words it is derived from are the ones its first rows were named by. */ export let attachmentOf = (message, index)=>derivedEid(`operator-attachment|${message}|${index}`);
35
+ /** The bytes of each file, and each one's use beside `message`. */ export let attachmentRows = (message, files)=>files.flatMap((file, i)=>[
36
+ {
37
+ entity: {
38
+ eid: file.address
39
+ },
40
+ artifact: {
41
+ address: file.address,
42
+ media_type: file.media_type,
43
+ ...file.size == null ? {} : {
44
+ size: file.size
45
+ }
46
+ }
47
+ },
48
+ {
49
+ entity: {
50
+ eid: attachmentOf(message, i)
51
+ },
52
+ attachment: {
53
+ name: file.name,
54
+ media_type: file.media_type,
55
+ artifact: file.address,
56
+ message,
57
+ ...file.text == null ? {} : {
58
+ text: file.text
59
+ }
60
+ }
61
+ }
62
+ ]);
63
+ /** The files each of these messages came with, in the order they came: each
64
+ * attachment beside it (`attachmentRows`) and the size its bytes are kept at.
65
+ * A message with none is left out. */ export let attachedTo = async (g, messages)=>{
66
+ let asked = [
67
+ ...new Set(messages)
68
+ ].filter(Boolean);
69
+ let uses = asked.length ? await g.read(and(eq('attachment.message', list(...asked)), fields('attachment.name', 'attachment.media_type', 'attachment.artifact', 'attachment.text'))) : [];
70
+ let by = new Map(uses.map((b)=>[
71
+ b.entity.eid,
72
+ b
73
+ ]));
74
+ let sizes = new Map((await g.get([
75
+ ...new Set(uses.map((b)=>String(use(b).artifact)))
76
+ ], [
77
+ 'artifact'
78
+ ])).map((b)=>[
79
+ b.entity.eid,
80
+ b.artifact.size
81
+ ]));
82
+ let out = new Map();
83
+ for (let message of asked){
84
+ let files = [];
85
+ for(let i = 0, b; b = by.get(attachmentOf(message, i)); i++){
86
+ let { name, media_type, artifact, text } = use(b);
87
+ let size = sizes.get(String(artifact));
88
+ files.push({
89
+ name: String(name ?? artifact),
90
+ media_type: String(media_type ?? 'application/octet-stream'),
91
+ address: String(artifact),
92
+ ...size == null ? {} : {
93
+ size: Number(size)
94
+ },
95
+ ...text == null ? {} : {
96
+ text: String(text)
97
+ }
98
+ });
99
+ }
100
+ if (files.length) out.set(message, files);
101
+ }
102
+ return out;
103
+ };
104
+ let use = (b)=>b.attachment ?? {};
105
+ /** The artifact row of each address `head` finds kept, its media type read
106
+ * from the first bytes `head` gives back. An address with nothing kept is
107
+ * left out, for the graph to hold as it is. */ export let artifactsOf = async (addresses, head)=>{
108
+ let rows = await Promise.all([
109
+ ...new Set(addresses)
110
+ ].map(async (address)=>{
111
+ let first = await head(address);
112
+ if (!first) return null;
113
+ let type = mediaTypeOf(first);
114
+ return {
115
+ entity: {
116
+ eid: address
117
+ },
118
+ artifact: {
119
+ address,
120
+ ...type ? {
121
+ media_type: type
122
+ } : {}
123
+ }
124
+ };
125
+ }));
126
+ return rows.filter((b)=>!!b);
127
+ };
128
+ // A clause that names one entity or one artifact, which the read of
129
+ // attachments narrows by before the query is asked of what it found.
130
+ let focusOf = (q)=>{
131
+ let focus = parse(q).clauses.find((clause)=>clause.kind == 'pred' && clause.op == '=' && [
132
+ 'entity.eid',
133
+ 'attachment.artifact'
134
+ ].includes(clause.path.join('.')) && !!clause.value && [
135
+ 'scalar',
136
+ 'list'
137
+ ].includes(clause.value.kind));
138
+ // An id may be the attachment's own, or its bytes' address.
139
+ return focus?.path.join('.') == 'entity.eid' ? or(focus, {
140
+ ...focus,
141
+ path: [
142
+ 'attachment',
143
+ 'artifact'
144
+ ]
145
+ }) : focus;
146
+ };
147
+ /**
148
+ * `q` asked as graph_query asks it, of only the attachment rows `admits` lets
149
+ * a reader see and the artifact rows they name: their names, media types and
150
+ * sizes, never the messages they came with or anything else, even where `q`
151
+ * walks a reference. `call` is the query's call, which the answer names.
152
+ */ export let attachmentsSeen = async (g, admits, q, call = crypto.randomUUID())=>{
153
+ let focus = focusOf(q);
154
+ let seen = (await g.read(and(present('attachment'), admits, ...focus ? [
155
+ focus
156
+ ] : []))).map((row)=>({
157
+ entity: row.entity,
158
+ attachment: row.attachment
159
+ }));
160
+ let addresses = seen.map((row)=>String(row.attachment.artifact));
161
+ let artifacts = (await g.get([
162
+ ...new Set(addresses)
163
+ ])).filter((row)=>row.artifact).map((row)=>({
164
+ entity: row.entity,
165
+ artifact: row.artifact
166
+ }));
167
+ let subset = graph({
168
+ vocab: g.vocab,
169
+ storage: ram(g.vocab)
170
+ });
171
+ await subset.apply([
172
+ ...seen,
173
+ ...artifacts
174
+ ], {
175
+ trusted: true,
176
+ stamp: false
177
+ });
178
+ let query = tier().find((tool)=>tool.name == 'graph_query');
179
+ return await query.run({
180
+ entity: {
181
+ eid: call
182
+ },
183
+ call: {
184
+ args: {
185
+ q
186
+ }
187
+ }
188
+ }, subset);
189
+ };
190
+ //# sourceMappingURL=attachment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment.js","sources":["./attachment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC,GACD,SAAkC,KAAK,2BAAgC;AACvE,SAAS,UAAU,oCAAyC;AAC5D,SAAS,IAAI,iCAAsC;AACnD,SAAS,GAAG,yBAA8B;AAC1C,SACE,GAAG,EAEH,EAAE,EACF,MAAM,EACN,IAAI,EACJ,EAAE,EACF,KAAK,EAEL,OAAO,gCAC2B;AACpC,SAAS,WAAW,qBAAoB;AAaxC;uEACuE,GACvE,OAAO,IAAI,eAAe,CAAC,SAAiB,QAC1C,WAAW,CAAC,oBAAoB,EAAE,QAAQ,CAAC,EAAE,OAAO,EAAC;AAEvD,iEAAiE,GACjE,OAAO,IAAI,iBAAiB,CAAC,SAAiB,QAC5C,MAAM,OAAO,CAAC,CAAC,MAAM,IAAM;MAAC;QAC1B,QAAQ;UAAE,KAAK,KAAK,OAAO;QAAC;QAC5B,UAAU;UACR,SAAS,KAAK,OAAO;UACrB,YAAY,KAAK,UAAU;UAC3B,GAAG,KAAK,IAAI,IAAI,OAAO,CAAC,IAAI;YAAE,MAAM,KAAK,IAAI;UAAC,CAAC;QACjD;MACF;MAAG;QACD,QAAQ;UAAE,KAAK,aAAa,SAAS;QAAG;QACxC,YAAY;UACV,MAAM,KAAK,IAAI;UACf,YAAY,KAAK,UAAU;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IACvB,QAAQ,eACR,WACG,QAAQ;IAAC;GAAM,GAAG,EAAE,EACvB,EAAE,GAAG,CAAC,CAAC,MAAQ,CAAC;MAAE,QAAQ,IAAI,MAAM;MAAE,YAAY,IAAI,UAAU;IAAC,CAAC;EACpE,IAAI,YAAY,KAAK,GAAG,CAAC,CAAC,MACxB,OAAO,AAAC,IAAI,UAAU,CAA0B,QAAQ;EAE1D,IAAI,YAAY,CAAC,MAAM,EAAE,GAAG,CAAC;OAAI,IAAI,IAAI;GAAW,CAAC,EAClD,MAAM,CAAC,CAAC,MAAQ,IAAI,QAAQ,EAC5B,GAAG,CAAC,CAAC,MAAQ,CAAC;MAAE,QAAQ,IAAI,MAAM;MAAE,UAAU,IAAI,QAAQ;IAAC,CAAC;EAC/D,IAAI,SAAS,MAAM;IAAE,OAAO,EAAE,KAAK;IAAE,SAAS,IAAI,EAAE,KAAK;EAAE;EAC3D,MAAM,OAAO,KAAK,CAAC;OAAI;OAAS;GAAU,EAAc;IACtD,SAAS;IACT,OAAO;EACT;EACA,IAAI,QAAQ,OAAO,IAAI,CAAC,CAAC,OAAS,KAAK,IAAI,IAAI;EAC/C,OAAO,MAAM,MAAM,GAAG,CACpB;IAAE,QAAQ;MAAE,KAAK;IAAK;IAAG,MAAM;MAAE,MAAM;QAAE;MAAE;IAAE;EAAE,GAC/C;AAEJ,EAAC"}
package/attachment.ts ADDED
@@ -0,0 +1,201 @@
1
+ /**
2
+ * An attachment is one use of immutable bytes, beside the message they came
3
+ * with: the bytes' `artifact` row, named by their address, and an
4
+ * `attachment` row for each message that carried them, named by that message
5
+ * and its place there. Sending the same bytes again with another message
6
+ * keeps both names; writing one message's files again patches the same rows.
7
+ *
8
+ * ```ts
9
+ * import { equal } from '@yaks/testing'
10
+ * import { attachmentOf, attachmentRows } from '@yaks/blob/attachment'
11
+ *
12
+ * let [bytes, use] = attachmentRows('m1', [
13
+ * { name: 'notes.pdf', media_type: 'application/pdf', address: 'c0ffee' },
14
+ * ])
15
+ * equal(bytes, {
16
+ * entity: { eid: 'c0ffee' },
17
+ * artifact: { address: 'c0ffee', media_type: 'application/pdf' },
18
+ * })
19
+ * equal(use.entity.eid, attachmentOf('m1', 0))
20
+ * ```
21
+ *
22
+ * What a reader is shown of them is {@link attachmentsSeen}: the attachment
23
+ * rows a clause admits and the artifacts they name, and nothing else in the
24
+ * graph, even where a query walks a reference.
25
+ *
26
+ * @module
27
+ */
28
+ import { type Bundle, type Graph, graph } from '@yaks/graph'
29
+ import { derivedEid } from '@yaks/graph/identity'
30
+ import { tier } from '@yaks/graph/tools'
31
+ import { ram } from '@yaks/ram'
32
+ import {
33
+ and,
34
+ type Clause,
35
+ eq,
36
+ fields,
37
+ list,
38
+ or,
39
+ parse,
40
+ type Pred,
41
+ present,
42
+ } from '@yaks/query/core'
43
+ import { mediaTypeOf } from './image.js'
44
+
45
+ /** A file as a message carries it: its name, media type and the address its
46
+ * bytes are kept by, with their size where it is known, and the words read
47
+ * from it for a model that cannot read it as it is, once something has. */
48
+ export type Attached = {
49
+ name: string
50
+ media_type: string
51
+ address: string
52
+ size?: number
53
+ text?: string
54
+ }
55
+
56
+ /** The attachment's stable identity within the message that carried it. The
57
+ * words it is derived from are the ones its first rows were named by. */
58
+ export let attachmentOf = (message: string, index: number): string =>
59
+ derivedEid(`operator-attachment|${message}|${index}`)
60
+
61
+ /** The bytes of each file, and each one's use beside `message`. */
62
+ export let attachmentRows = (message: string, files: Attached[]): Bundle[] =>
63
+ files.flatMap((file, i) => [{
64
+ entity: { eid: file.address },
65
+ artifact: {
66
+ address: file.address,
67
+ media_type: file.media_type,
68
+ ...file.size == null ? {} : { size: file.size },
69
+ },
70
+ }, {
71
+ entity: { eid: attachmentOf(message, i) },
72
+ attachment: {
73
+ name: file.name,
74
+ media_type: file.media_type,
75
+ artifact: file.address,
76
+ message,
77
+ ...file.text == null ? {} : { text: file.text },
78
+ },
79
+ }])
80
+
81
+ /** The files each of these messages came with, in the order they came: each
82
+ * attachment beside it (`attachmentRows`) and the size its bytes are kept at.
83
+ * A message with none is left out. */
84
+ export let attachedTo = async (
85
+ g: Pick<Graph, 'read' | 'get'>,
86
+ messages: string[],
87
+ ): Promise<Map<string, Attached[]>> => {
88
+ let asked = [...new Set(messages)].filter(Boolean)
89
+ let uses = asked.length
90
+ ? await g.read(and(
91
+ eq('attachment.message', list(...asked)),
92
+ fields(
93
+ 'attachment.name',
94
+ 'attachment.media_type',
95
+ 'attachment.artifact',
96
+ 'attachment.text',
97
+ ),
98
+ ))
99
+ : []
100
+ let by = new Map(uses.map((b) => [b.entity.eid, b]))
101
+ let sizes = new Map(
102
+ (await g.get(
103
+ [...new Set(uses.map((b) => String(use(b).artifact)))],
104
+ ['artifact'],
105
+ )).map((b) => [b.entity.eid, (b.artifact as { size?: number }).size]),
106
+ )
107
+ let out = new Map<string, Attached[]>()
108
+ for (let message of asked) {
109
+ let files: Attached[] = []
110
+ for (let i = 0, b; (b = by.get(attachmentOf(message, i))); i++) {
111
+ let { name, media_type, artifact, text } = use(b)
112
+ let size = sizes.get(String(artifact))
113
+ files.push({
114
+ name: String(name ?? artifact),
115
+ media_type: String(media_type ?? 'application/octet-stream'),
116
+ address: String(artifact),
117
+ ...size == null ? {} : { size: Number(size) },
118
+ ...text == null ? {} : { text: String(text) },
119
+ })
120
+ }
121
+ if (files.length) out.set(message, files)
122
+ }
123
+ return out
124
+ }
125
+
126
+ let use = (b: Bundle) =>
127
+ (b.attachment ?? {}) as {
128
+ name?: string
129
+ media_type?: string
130
+ artifact?: string
131
+ text?: string
132
+ }
133
+
134
+ /** The artifact row of each address `head` finds kept, its media type read
135
+ * from the first bytes `head` gives back. An address with nothing kept is
136
+ * left out, for the graph to hold as it is. */
137
+ export let artifactsOf = async (
138
+ addresses: string[],
139
+ head: (address: string) => Promise<Uint8Array | null>,
140
+ ): Promise<Bundle[]> => {
141
+ let rows = await Promise.all([...new Set(addresses)].map(async (address) => {
142
+ let first = await head(address)
143
+ if (!first) return null
144
+ let type = mediaTypeOf(first)
145
+ return {
146
+ entity: { eid: address },
147
+ artifact: { address, ...type ? { media_type: type } : {} },
148
+ }
149
+ }))
150
+ return rows.filter((b) => !!b)
151
+ }
152
+
153
+ // A clause that names one entity or one artifact, which the read of
154
+ // attachments narrows by before the query is asked of what it found.
155
+ let focusOf = (q: string): Clause | undefined => {
156
+ let focus = parse(q).clauses.find((clause): clause is Pred =>
157
+ clause.kind == 'pred' && clause.op == '=' &&
158
+ ['entity.eid', 'attachment.artifact'].includes(clause.path.join('.')) &&
159
+ !!clause.value && ['scalar', 'list'].includes(clause.value.kind)
160
+ )
161
+ // An id may be the attachment's own, or its bytes' address.
162
+ return focus?.path.join('.') == 'entity.eid'
163
+ ? or(focus, { ...focus, path: ['attachment', 'artifact'] })
164
+ : focus
165
+ }
166
+
167
+ /**
168
+ * `q` asked as graph_query asks it, of only the attachment rows `admits` lets
169
+ * a reader see and the artifact rows they name: their names, media types and
170
+ * sizes, never the messages they came with or anything else, even where `q`
171
+ * walks a reference. `call` is the query's call, which the answer names.
172
+ */
173
+ export let attachmentsSeen = async (
174
+ g: Pick<Graph, 'read' | 'get' | 'vocab'>,
175
+ admits: Clause,
176
+ q: string,
177
+ call: string = crypto.randomUUID(),
178
+ ): Promise<Bundle[]> => {
179
+ let focus = focusOf(q)
180
+ let seen = (await g.read(and(
181
+ present('attachment'),
182
+ admits,
183
+ ...focus ? [focus] : [],
184
+ ))).map((row) => ({ entity: row.entity, attachment: row.attachment }))
185
+ let addresses = seen.map((row) =>
186
+ String((row.attachment as { artifact: string }).artifact)
187
+ )
188
+ let artifacts = (await g.get([...new Set(addresses)]))
189
+ .filter((row) => row.artifact)
190
+ .map((row) => ({ entity: row.entity, artifact: row.artifact }))
191
+ let subset = graph({ vocab: g.vocab, storage: ram(g.vocab) })
192
+ await subset.apply([...seen, ...artifacts] as Bundle[], {
193
+ trusted: true,
194
+ stamp: false,
195
+ })
196
+ let query = tier().find((tool) => tool.name == 'graph_query')!
197
+ return await query.run(
198
+ { entity: { eid: call }, call: { args: { q } } },
199
+ subset,
200
+ ) as Bundle[]
201
+ }
package/backend.js ADDED
@@ -0,0 +1,37 @@
1
+ // One backend declaration yields the byte store shared by artifact writers
2
+ // and readers. Text properties keep their separate SQL store.
3
+ //
4
+ // The stores a runtime may lack come from its host: a directory store is
5
+ // `host.files` (@yaks/file/blobs `fileBlobs`, given by a box), so a Worker or a
6
+ // browser tab that loads this module never loads the filesystem.
7
+ import { objectBlobs } from "./object.js";
8
+ /** Build the backend named by a config, or explain why it cannot serve. */ export let backend = (said, host)=>{
9
+ if (said.via == 'sqlite') {
10
+ return host.blobs ? {
11
+ store: host.blobs
12
+ } : {
13
+ waiting: 'a SQLite store needs a host that keeps one'
14
+ };
15
+ }
16
+ if (said.via == 'file') {
17
+ if (!said.dir) return {
18
+ waiting: 'a file store needs `dir`'
19
+ };
20
+ return host.files ? {
21
+ store: host.files(said.dir)
22
+ } : {
23
+ waiting: 'a file store needs a host with files'
24
+ };
25
+ }
26
+ if (said.via == 'object') {
27
+ return said.bucket ? {
28
+ store: objectBlobs(said.bucket, said.prefix)
29
+ } : {
30
+ waiting: 'an object store needs `bucket`'
31
+ };
32
+ }
33
+ return {
34
+ waiting: `no store called ${JSON.stringify(said.via)}`
35
+ };
36
+ };
37
+ //# sourceMappingURL=backend.js.map
package/backend.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backend.js","sources":["./backend.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,8DAA8D;AAC9D,EAAE;AACF,yEAAyE;AACzE,gFAAgF;AAChF,iEAAiE;AACjE,SAAsB,WAAW,sBAAqB;AAatD,yEAAyE,GACzE,OAAO,IAAI,UAAU,CACnB,MACA;EAEA,IAAI,KAAK,GAAG,IAAI,UAAU;IACxB,OAAO,KAAK,KAAK,GACb;MAAE,OAAO,KAAK,KAAK;IAAC,IACpB;MAAE,SAAS;IAA6C;EAC9D;EACA,IAAI,KAAK,GAAG,IAAI,QAAQ;IACtB,IAAI,CAAC,KAAK,GAAG,EAAE,OAAO;MAAE,SAAS;IAA2B;IAC5D,OAAO,KAAK,KAAK,GACb;MAAE,OAAO,KAAK,KAAK,CAAC,KAAK,GAAG;IAAE,IAC9B;MAAE,SAAS;IAAuC;EACxD;EACA,IAAI,KAAK,GAAG,IAAI,UAAU;IACxB,OAAO,KAAK,MAAM,GACd;MAAE,OAAO,YAAY,KAAK,MAAM,EAAE,KAAK,MAAM;IAAE,IAC/C;MAAE,SAAS;IAAiC;EAClD;EACA,OAAO;IACL,SAAS,CAAC,gBAAgB,EAAE,KAAK,SAAS,CAAC,AAAC,KAAiB,GAAG,GAAG;EACrE;AACF,EAAC"}
package/backend.ts ADDED
@@ -0,0 +1,44 @@
1
+ // One backend declaration yields the byte store shared by artifact writers
2
+ // and readers. Text properties keep their separate SQL store.
3
+ //
4
+ // The stores a runtime may lack come from its host: a directory store is
5
+ // `host.files` (@yaks/file/blobs `fileBlobs`, given by a box), so a Worker or a
6
+ // browser tab that loads this module never loads the filesystem.
7
+ import { type Bucket, objectBlobs } from './object.js'
8
+ import type { Blobs } from './store.ts'
9
+
10
+ /** A byte store, as a graph's configuration names one. */
11
+ export type Backend =
12
+ | { via: 'sqlite' }
13
+ | { via: 'file'; dir: string }
14
+ | {
15
+ via: 'object'
16
+ bucket: Pick<Bucket, 'head' | 'get' | 'put' | 'delete'>
17
+ prefix?: string
18
+ }
19
+
20
+ /** Build the backend named by a config, or explain why it cannot serve. */
21
+ export let backend = (
22
+ said: Backend,
23
+ host: { blobs?: Blobs; files?: (dir: string) => Blobs },
24
+ ): { store?: Blobs; waiting?: string } => {
25
+ if (said.via == 'sqlite') {
26
+ return host.blobs
27
+ ? { store: host.blobs }
28
+ : { waiting: 'a SQLite store needs a host that keeps one' }
29
+ }
30
+ if (said.via == 'file') {
31
+ if (!said.dir) return { waiting: 'a file store needs `dir`' }
32
+ return host.files
33
+ ? { store: host.files(said.dir) }
34
+ : { waiting: 'a file store needs a host with files' }
35
+ }
36
+ if (said.via == 'object') {
37
+ return said.bucket
38
+ ? { store: objectBlobs(said.bucket, said.prefix) }
39
+ : { waiting: 'an object store needs `bucket`' }
40
+ }
41
+ return {
42
+ waiting: `no store called ${JSON.stringify((said as Backend).via)}`,
43
+ }
44
+ }