@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/plugin.ts ADDED
@@ -0,0 +1,184 @@
1
+ // The substitution, as a graph plugin. A writer sends text; the row keeps the
2
+ // text's address and the bytes go to the store; a reader gets text back.
3
+ // External stores keep content in `prepare`, before the graph takes its write
4
+ // lock. Transactional stores keep it after the `$was` guard, so bytes and rows
5
+ // roll back together. Both leave text intact until the guard checks it, then
6
+ // substitute references in `precondition` and restore text in `commit`. `$blob`
7
+ // carries the prepared references and original text through the batch.
8
+ //
9
+ // Bytes outlive edits, removals and expiration: trash and history still read
10
+ // their text. Only an explicit purge may release objects no surviving row or
11
+ // historical copy names.
12
+
13
+ import type { Bundle, Comp } from 'jsr:@yaks/graph@0.2.36/bundle'
14
+ import { hook, type Plugin } from '@yaks/graph/plugin'
15
+ import { after, each } from '@yaks/fp'
16
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
17
+ import { bodies, type Body, holders } from './props.js'
18
+ import { address, type Blobs, encode } from './store.js'
19
+ import { purged } from './purge.js'
20
+
21
+ let STASH = '$blob'
22
+ type Stored = { value: string; ref: string | number }
23
+ type Stash = Record<string, Stored>
24
+
25
+ // The bundle's patch for a component, when it carries one at all. A `null`
26
+ // component is a deletion, not a value, and has no text to move.
27
+ let patch = (b: Bundle, comp: string): Comp | undefined => {
28
+ let c = b[comp]
29
+ return c && typeof c == 'object' && !Array.isArray(c) ? c as Comp : undefined
30
+ }
31
+
32
+ // One bundle's content-addressed properties that carry a string in this write.
33
+ let written = (b: Bundle, props: Body[]): [Body, string][] =>
34
+ props.flatMap(({ comp, prop }) => {
35
+ let value = patch(b, comp)?.[prop]
36
+ return typeof value == 'string' ? [[{ comp, prop }, value] as const] : []
37
+ })
38
+
39
+ // Keep content while leaving the text available to the graph's guard.
40
+ let stage = (
41
+ b: Bundle,
42
+ props: Body[],
43
+ intern: (value: string) => string | number | Promise<string | number>,
44
+ ): Bundle | Promise<Bundle> => {
45
+ let mine = written(b, props)
46
+ if (!mine.length) return b
47
+ let stash: Stash = {}
48
+ let out: Bundle = { ...b }
49
+ return after(
50
+ each(mine, null, (_, [{ comp, prop }, value]) => {
51
+ return after(intern(value), (ref) => {
52
+ stash[`${comp}.${prop}`] = { value, ref }
53
+ return null
54
+ })
55
+ }),
56
+ () => {
57
+ out[STASH] = stash
58
+ return out
59
+ },
60
+ )
61
+ }
62
+
63
+ let swap = (b: Bundle): Bundle => {
64
+ let stash = b[STASH] as Stash | undefined
65
+ if (!stash) return b
66
+ let out = { ...b }
67
+ for (let [key, { ref }] of Object.entries(stash)) {
68
+ let [comp, prop] = key.split('.')
69
+ out[comp] = { ...patch(out, comp)!, [prop]: ref }
70
+ }
71
+ return out
72
+ }
73
+
74
+ // Put the text back where the caller wrote it, and take the stash away.
75
+ let restore = (b: Bundle): Bundle => {
76
+ let stash = b[STASH] as Stash | undefined
77
+ if (!stash) return b
78
+ let out: Bundle = { ...b }
79
+ delete out[STASH]
80
+ for (let [key, { value }] of Object.entries(stash)) {
81
+ let [comp, prop] = key.split('.')
82
+ let held = patch(out, comp)
83
+ if (held) out[comp] = { ...held, [prop]: value }
84
+ }
85
+ return out
86
+ }
87
+
88
+ /** How a store addresses an object from a component row. Usually the hash
89
+ * itself; an existing SQL layout may instead keep an integer foreign key.
90
+ * Called after `put`, in the store's preparation phase. */
91
+ export type Reference = (
92
+ sha: string,
93
+ ) => string | number | Promise<string | number>
94
+
95
+ export type BlobOpts = {
96
+ /** Narrow the vocabulary's blob properties for a partially migrated store. */
97
+ props?: Body[]
98
+ /** Translate the content hash into the reference the row should hold. */
99
+ reference?: Reference
100
+ }
101
+
102
+ /**
103
+ * The blob plugin: content-addressed storage for every property the vocabulary
104
+ * marks `store: "blob"`.
105
+ *
106
+ * ```ts
107
+ * import { loadVocab } from '@yaks/vocab'
108
+ * import { graph } from '@yaks/graph'
109
+ * import { ram } from '@yaks/ram'
110
+ * import { blobKeywords, blobs, memoryBlobs } from '@yaks/blob'
111
+ *
112
+ * let body = { type: 'string', store: 'blob' }
113
+ * let post = { component: true, type: 'object', properties: { body } }
114
+ * let vocab = loadVocab([{ $defs: { post } }], [blobKeywords])
115
+ * let store = memoryBlobs()
116
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [blobs(vocab, store)] })
117
+ * g.apply([{ entity: { eid: 'p1' }, post: { body: 'a long essay…' } }])
118
+ * ```
119
+ *
120
+ * The write side is here. The read side belongs to the storage adapter, which
121
+ * is the half that knows its own layout: over SQL, register {@link blobRead}'s
122
+ * property overrides and a row resolves in the statement itself; over any other
123
+ * store, {@link hydrate} resolves the bundles a read returned.
124
+ *
125
+ * A vocabulary loaded without {@link blobKeywords} declares no body properties,
126
+ * so this plugin is a no-op on it rather than a surprise.
127
+ */
128
+ export let blobs = (
129
+ vocab: Vocab,
130
+ store: Blobs,
131
+ opts: BlobOpts = {},
132
+ ): Plugin => {
133
+ let props = opts.props ?? bodies(vocab)
134
+ let keys = [...new Set(props.map((p) => p.comp))]
135
+ let reference = opts.reference ?? ((sha: string) => sha)
136
+ let prepare = (bundles: Bundle[]) => {
137
+ // Cache only within this batch: a rolled-back transactional reference
138
+ // must never be reused. Equal values need one hash and one store call.
139
+ let refs = new Map<string, string | number>()
140
+ let intern = (value: string) => {
141
+ let held = refs.get(value)
142
+ if (held !== undefined) return held
143
+ let sha = address(value)
144
+ return after(
145
+ store.has(sha),
146
+ (exists) =>
147
+ after(
148
+ exists ? undefined : store.put(sha, encode(value)),
149
+ () =>
150
+ after(reference(sha), (ref) => {
151
+ refs.set(value, ref)
152
+ return ref
153
+ }),
154
+ ),
155
+ )
156
+ }
157
+ return each(
158
+ bundles,
159
+ [] as Bundle[],
160
+ (out, b) => after(stage(b, props, intern), (one) => [...out, one]),
161
+ )
162
+ }
163
+ return {
164
+ name: '@yaks/blob',
165
+ purged: opts.reference ? undefined : purged(store, holders(vocab)),
166
+ // Preparing a body may retain external bytes or mint a local reference.
167
+ admission: (bundles) => bundles.every((b) => !written(b, props).length),
168
+ hooks: {
169
+ prepare: hook(
170
+ keys,
171
+ (bundles) => store.transactional ? bundles : prepare(bundles),
172
+ ),
173
+ precondition: hook(
174
+ [...keys, STASH],
175
+ (bundles) =>
176
+ after(
177
+ store.transactional ? prepare(bundles) : bundles,
178
+ (ready) => ready.map(swap),
179
+ ),
180
+ ),
181
+ commit: hook([...keys, STASH], (bundles) => bundles.map(restore)),
182
+ },
183
+ }
184
+ }
package/png.js ADDED
@@ -0,0 +1,74 @@
1
+ // A PNG from pixels: what a host that draws an image server-side hands a mail
2
+ // client or a browser. Pure apart from the platform's CompressionStream.
3
+ // CRC-32 (ISO 3309), which every PNG chunk ends with.
4
+ let TABLE = Array.from({
5
+ length: 256
6
+ }, (_, n)=>{
7
+ for(let k = 0; k < 8; k++)n = n & 1 ? 0xedb88320 ^ n >>> 1 : n >>> 1;
8
+ return n >>> 0;
9
+ });
10
+ let crc = (bytes)=>(bytes.reduce((c, b)=>TABLE[(c ^ b) & 0xff] ^ c >>> 8, 0xffffffff) ^ 0xffffffff) >>> 0;
11
+ // What every PNG file starts with.
12
+ let SIGNATURE = [
13
+ 137,
14
+ 80,
15
+ 78,
16
+ 71,
17
+ 13,
18
+ 10,
19
+ 26,
20
+ 10
21
+ ];
22
+ let u32 = (n)=>[
23
+ n >>> 24,
24
+ n >>> 16 & 255,
25
+ n >>> 8 & 255,
26
+ n & 255
27
+ ];
28
+ let chunk = (type, data)=>{
29
+ let body = new Uint8Array([
30
+ ...new TextEncoder().encode(type),
31
+ ...data
32
+ ]);
33
+ return [
34
+ ...u32(data.length),
35
+ ...body,
36
+ ...u32(crc(body))
37
+ ];
38
+ };
39
+ let deflate = async (bytes)=>new Uint8Array(await new Response(new Blob([
40
+ bytes
41
+ ]).stream().pipeThrough(new CompressionStream('deflate'))).arrayBuffer());
42
+ /**
43
+ * An RGBA image `w` by `h` pixels as PNG bytes: `rgba` holds four bytes a
44
+ * pixel, row by row from the top left.
45
+ *
46
+ * ```ts
47
+ * import { png } from '@yaks/blob/png'
48
+ * import { sizeOf } from '@yaks/blob'
49
+ * import { equal } from '@yaks/testing'
50
+ *
51
+ * let red = new Uint8Array(3 * 2 * 4).map((_, i) => i % 4 == 1 || i % 4 == 2 ? 0 : 255)
52
+ * equal(sizeOf(await png(3, 2, red)), { w: 3, h: 2 })
53
+ * ```
54
+ */ export let png = async (w, h, rgba)=>{
55
+ let rows = new Uint8Array(h * (w * 4 + 1));
56
+ for(let y = 0; y < h; y++){
57
+ rows.set(rgba.subarray(y * w * 4, (y + 1) * w * 4), y * (w * 4 + 1) + 1);
58
+ }
59
+ return new Uint8Array([
60
+ ...SIGNATURE,
61
+ ...chunk('IHDR', new Uint8Array([
62
+ ...u32(w),
63
+ ...u32(h),
64
+ 8,
65
+ 6,
66
+ 0,
67
+ 0,
68
+ 0
69
+ ])),
70
+ ...chunk('IDAT', await deflate(rows)),
71
+ ...chunk('IEND', new Uint8Array())
72
+ ]);
73
+ };
74
+ //# sourceMappingURL=png.js.map
package/png.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"png.js","sources":["./png.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,yEAAyE;AAEzE,sDAAsD;AACtD,IAAI,QAAQ,MAAM,IAAI,CAAC;EAAE,QAAQ;AAAI,GAAG,CAAC,GAAG;EAC1C,IAAK,IAAI,IAAI,GAAG,IAAI,GAAG,IAAK,IAAI,IAAI,IAAI,aAAc,MAAM,IAAK,MAAM;EACvE,OAAO,MAAM;AACf;AACA,IAAI,MAAM,CAAC,QACT,CAAC,MAAM,MAAM,CAAC,CAAC,GAAG,IAAM,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,GAAI,MAAM,GAAI,cACzD,UAAU,MAAM;AAEpB,mCAAmC;AACnC,IAAI,YAAY;EAAC;EAAK;EAAI;EAAI;EAAI;EAAI;EAAI;EAAI;CAAG;AAEjD,IAAI,MAAM,CAAC,IAAc;IAAC,MAAM;IAAK,MAAM,KAAM;IAAM,MAAM,IAAK;IAAK,IAAI;GAAI;AAE/E,IAAI,QAAQ,CAAC,MAAc;EACzB,IAAI,OAAO,IAAI,WAAW;OAAI,IAAI,cAAc,MAAM,CAAC;OAAU;GAAK;EACtE,OAAO;OAAI,IAAI,KAAK,MAAM;OAAM;OAAS,IAAI,IAAI;GAAO;AAC1D;AAEA,IAAI,UAAU,OAAO,QACnB,IAAI,WACF,MAAM,IAAI,SACR,IAAI,KAAK;IAAC;GAAM,EAAE,MAAM,GAAG,WAAW,CAAC,IAAI,kBAAkB,aAC7D,WAAW;AAGjB;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,MAAM,OACf,GACA,GACA;EAEA,IAAI,OAAO,IAAI,WAAW,IAAI,CAAC,IAAI,IAAI,CAAC;EACxC,IAAK,IAAI,IAAI,GAAG,IAAI,GAAG,IAAK;IAC1B,KAAK,GAAG,CAAC,KAAK,QAAQ,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI;EACxE;EACA,OAAO,IAAI,WAAW;OACjB;OACA,MAAM,QAAQ,IAAI,WAAW;SAAI,IAAI;SAAO,IAAI;MAAI;MAAG;MAAG;MAAG;MAAG;KAAE;OAClE,MAAM,QAAQ,MAAM,QAAQ;OAC5B,MAAM,QAAQ,IAAI;GACtB;AACH,EAAC"}
package/png.ts ADDED
@@ -0,0 +1,58 @@
1
+ // A PNG from pixels: what a host that draws an image server-side hands a mail
2
+ // client or a browser. Pure apart from the platform's CompressionStream.
3
+
4
+ // CRC-32 (ISO 3309), which every PNG chunk ends with.
5
+ let TABLE = Array.from({ length: 256 }, (_, n) => {
6
+ for (let k = 0; k < 8; k++) n = n & 1 ? 0xedb88320 ^ (n >>> 1) : n >>> 1
7
+ return n >>> 0
8
+ })
9
+ let crc = (bytes: Uint8Array) =>
10
+ (bytes.reduce((c, b) => TABLE[(c ^ b) & 0xff] ^ (c >>> 8), 0xffffffff) ^
11
+ 0xffffffff) >>> 0
12
+
13
+ // What every PNG file starts with.
14
+ let SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10]
15
+
16
+ let u32 = (n: number) => [n >>> 24, (n >>> 16) & 255, (n >>> 8) & 255, n & 255]
17
+
18
+ let chunk = (type: string, data: Uint8Array) => {
19
+ let body = new Uint8Array([...new TextEncoder().encode(type), ...data])
20
+ return [...u32(data.length), ...body, ...u32(crc(body))]
21
+ }
22
+
23
+ let deflate = async (bytes: Uint8Array<ArrayBuffer>) =>
24
+ new Uint8Array(
25
+ await new Response(
26
+ new Blob([bytes]).stream().pipeThrough(new CompressionStream('deflate')),
27
+ ).arrayBuffer(),
28
+ )
29
+
30
+ /**
31
+ * An RGBA image `w` by `h` pixels as PNG bytes: `rgba` holds four bytes a
32
+ * pixel, row by row from the top left.
33
+ *
34
+ * ```ts
35
+ * import { png } from '@yaks/blob/png'
36
+ * import { sizeOf } from '@yaks/blob'
37
+ * import { equal } from '@yaks/testing'
38
+ *
39
+ * let red = new Uint8Array(3 * 2 * 4).map((_, i) => i % 4 == 1 || i % 4 == 2 ? 0 : 255)
40
+ * equal(sizeOf(await png(3, 2, red)), { w: 3, h: 2 })
41
+ * ```
42
+ */
43
+ export let png = async (
44
+ w: number,
45
+ h: number,
46
+ rgba: Uint8Array,
47
+ ): Promise<Uint8Array> => {
48
+ let rows = new Uint8Array(h * (w * 4 + 1))
49
+ for (let y = 0; y < h; y++) {
50
+ rows.set(rgba.subarray(y * w * 4, (y + 1) * w * 4), y * (w * 4 + 1) + 1)
51
+ }
52
+ return new Uint8Array([
53
+ ...SIGNATURE,
54
+ ...chunk('IHDR', new Uint8Array([...u32(w), ...u32(h), 8, 6, 0, 0, 0])),
55
+ ...chunk('IDAT', await deflate(rows)),
56
+ ...chunk('IEND', new Uint8Array()),
57
+ ])
58
+ }
package/props.js ADDED
@@ -0,0 +1,32 @@
1
+ // Which properties are content-addressed. The vocabulary carries the `store`
2
+ // keyword; this module is the one place that reads it, so every other module
3
+ // here calls a function ("is this property a body?", "which are they?") instead
4
+ // of digging through schemas itself.
5
+ //
6
+ // A property marked `store: "blob"` is an ordinary string property in every
7
+ // other respect — it is declared, validated, queried and written as text. Only
8
+ // where the value lives differs, and that is this package's whole subject.
9
+ /** Whether a property keeps its value in a content-addressed store. */ export let isBody = (prop)=>prop?.keywords?.store == 'blob';
10
+ /**
11
+ * Every content-addressed property in a vocabulary, by component then
12
+ * declaration order. Requires the vocabulary to have been loaded with
13
+ * {@link blobKeywords} — without that registration the loader does not carry
14
+ * the `store` keyword at all, and this returns an empty list, which is the
15
+ * correct reading of a vocabulary that never declared one.
16
+ */ export let bodies = (vocab)=>vocab.all.flatMap((comp)=>vocab.props(comp).filter((prop)=>isBody(vocab.prop(comp, prop))).map((prop)=>({
17
+ comp,
18
+ prop
19
+ })));
20
+ /**
21
+ * Every stored property whose value names bytes in the store: the bodies, and
22
+ * the properties declaring `address: true`, whose package wrote the bytes and
23
+ * keeps their address itself. While a row of one names an address, its bytes
24
+ * are kept.
25
+ */ export let holders = (vocab)=>vocab.all.flatMap((comp)=>vocab.props(comp).filter((prop)=>{
26
+ let p = vocab.prop(comp, prop);
27
+ return !p?.computed && (isBody(p) || p?.keywords?.address === true);
28
+ }).map((prop)=>({
29
+ comp,
30
+ prop
31
+ })));
32
+ //# sourceMappingURL=props.js.map
package/props.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"props.js","sources":["./props.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,6EAA6E;AAC7E,gFAAgF;AAChF,qCAAqC;AACrC,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAC/E,2EAA2E;AAO3E,qEAAqE,GACrE,OAAO,IAAI,SAAS,CAAC,OACnB,MAAM,UAAU,SAAS,OAAM;AAEjC;;;;;;CAMC,GACD,OAAO,IAAI,SAAS,CAAC,QACnB,MAAM,GAAG,CAAC,OAAO,CAAC,CAAC,OACjB,MAAM,KAAK,CAAC,MACT,MAAM,CAAC,CAAC,OAAS,OAAO,MAAM,IAAI,CAAC,MAAM,QACzC,GAAG,CAAC,CAAC,OAAS,CAAC;QAAE;QAAM;MAAK,CAAC,IACjC;AAEH;;;;;CAKC,GACD,OAAO,IAAI,UAAU,CAAC,QACpB,MAAM,GAAG,CAAC,OAAO,CAAC,CAAC,OACjB,MAAM,KAAK,CAAC,MACT,MAAM,CAAC,CAAC;MACP,IAAI,IAAI,MAAM,IAAI,CAAC,MAAM;MACzB,OAAO,CAAC,GAAG,YAAY,CAAC,OAAO,MAAM,GAAG,UAAU,YAAY,IAAI;IACpE,GACC,GAAG,CAAC,CAAC,OAAS,CAAC;QAAE;QAAM;MAAK,CAAC,IACjC"}
package/props.ts ADDED
@@ -0,0 +1,47 @@
1
+ // Which properties are content-addressed. The vocabulary carries the `store`
2
+ // keyword; this module is the one place that reads it, so every other module
3
+ // here calls a function ("is this property a body?", "which are they?") instead
4
+ // of digging through schemas itself.
5
+ //
6
+ // A property marked `store: "blob"` is an ordinary string property in every
7
+ // other respect — it is declared, validated, queried and written as text. Only
8
+ // where the value lives differs, and that is this package's whole subject.
9
+
10
+ import type { Prop, Vocab } from 'jsr:@yaks/vocab@0.2.36'
11
+
12
+ /** One content-addressed property, named the way a vocabulary names it. */
13
+ export type Body = { comp: string; prop: string }
14
+
15
+ /** Whether a property keeps its value in a content-addressed store. */
16
+ export let isBody = (prop: Prop | undefined): boolean =>
17
+ prop?.keywords?.store == 'blob'
18
+
19
+ /**
20
+ * Every content-addressed property in a vocabulary, by component then
21
+ * declaration order. Requires the vocabulary to have been loaded with
22
+ * {@link blobKeywords} — without that registration the loader does not carry
23
+ * the `store` keyword at all, and this returns an empty list, which is the
24
+ * correct reading of a vocabulary that never declared one.
25
+ */
26
+ export let bodies = (vocab: Vocab): Body[] =>
27
+ vocab.all.flatMap((comp) =>
28
+ vocab.props(comp)
29
+ .filter((prop) => isBody(vocab.prop(comp, prop)))
30
+ .map((prop) => ({ comp, prop }))
31
+ )
32
+
33
+ /**
34
+ * Every stored property whose value names bytes in the store: the bodies, and
35
+ * the properties declaring `address: true`, whose package wrote the bytes and
36
+ * keeps their address itself. While a row of one names an address, its bytes
37
+ * are kept.
38
+ */
39
+ export let holders = (vocab: Vocab): Body[] =>
40
+ vocab.all.flatMap((comp) =>
41
+ vocab.props(comp)
42
+ .filter((prop) => {
43
+ let p = vocab.prop(comp, prop)
44
+ return !p?.computed && (isBody(p) || p?.keywords?.address === true)
45
+ })
46
+ .map((prop) => ({ comp, prop }))
47
+ )
package/purge.js ADDED
@@ -0,0 +1,65 @@
1
+ // Explicit history purges may release pooled bytes, after every surviving
2
+ // graph and plugin copy has had a chance to name the content it still needs.
3
+ import { after, each } from "@yaks/fp";
4
+ import { address, decode } from "./store.js";
5
+ let strings = (value)=>{
6
+ if (typeof value == 'string') return [
7
+ value
8
+ ];
9
+ if (!value || typeof value != 'object') return [];
10
+ return Object.values(value).flatMap(strings);
11
+ };
12
+ let candidates = (removed)=>{
13
+ let found = new Map();
14
+ let keep = (sha, text)=>{
15
+ let values = found.get(sha) ?? new Set([
16
+ sha
17
+ ]);
18
+ values.add(text);
19
+ found.set(sha, values);
20
+ };
21
+ for (let text of strings(removed)){
22
+ keep(address(text), text);
23
+ if (/^[a-f0-9]{64}$/.test(text)) keep(text, text);
24
+ }
25
+ return found;
26
+ };
27
+ let present = (store, removed)=>each([
28
+ ...candidates(removed)
29
+ ], new Map(), (found, [sha, values])=>after(store.get(sha), (bytes)=>{
30
+ if (bytes) {
31
+ values.add(decode(bytes));
32
+ found.set(sha, values);
33
+ }
34
+ return found;
35
+ }));
36
+ let retained = (graph, values)=>after(graph.storage.retained(values), (found)=>each(graph.plugins, new Set(found), (kept, plugin)=>after(plugin.retained?.(graph, values), (more)=>{
37
+ for (let value of more ?? []){
38
+ kept.add(value);
39
+ }
40
+ return kept;
41
+ })));
42
+ /** Release only bytes whose last live, historical and trash reference was
43
+ * removed by an explicit purge. A store without retained-value accounting or
44
+ * byte deletion keeps its pool. */ export let purged = (store, held)=>(graph, removed)=>{
45
+ if (!store.drop || !graph.storage.retained) return;
46
+ return after(present(store, removed), (found)=>{
47
+ if (!found.size) return;
48
+ let values = [
49
+ ...new Set([
50
+ ...found.values()
51
+ ].flatMap((set)=>[
52
+ ...set
53
+ ]))
54
+ ];
55
+ return after(retained(graph, values), (kept)=>{
56
+ let free = [
57
+ ...found
58
+ ].filter(([, values])=>![
59
+ ...values
60
+ ].some((value)=>kept.has(value))).map(([sha])=>sha);
61
+ if (free.length) return store.drop(free, held);
62
+ });
63
+ });
64
+ };
65
+ //# sourceMappingURL=purge.js.map
package/purge.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"purge.js","sources":["./purge.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,6EAA6E;AAE7E,SAAS,KAAK,EAAE,IAAI,wBAA6B;AAEjD,SAAS,OAAO,EAAc,MAAM,qBAAoB;AAExD,IAAI,UAAU,CAAC;EACb,IAAI,OAAO,SAAS,UAAU,OAAO;IAAC;GAAM;EAC5C,IAAI,CAAC,SAAS,OAAO,SAAS,UAAU,OAAO,EAAE;EACjD,OAAO,OAAO,MAAM,CAAC,OAAO,OAAO,CAAC;AACtC;AAEA,IAAI,aAAa,CAAC;EAChB,IAAI,QAAQ,IAAI;EAChB,IAAI,OAAO,CAAC,KAAa;IACvB,IAAI,SAAS,MAAM,GAAG,CAAC,QAAQ,IAAI,IAAY;MAAC;KAAI;IACpD,OAAO,GAAG,CAAC;IACX,MAAM,GAAG,CAAC,KAAK;EACjB;EACA,KAAK,IAAI,QAAQ,QAAQ,SAAU;IACjC,KAAK,QAAQ,OAAO;IACpB,IAAI,iBAAiB,IAAI,CAAC,OAAO,KAAK,MAAM;EAC9C;EACA,OAAO;AACT;AAEA,IAAI,UAAU,CAAC,OAAc,UAC3B,KACE;OAAI,WAAW;GAAS,EACxB,IAAI,OACJ,CAAC,OAAO,CAAC,KAAK,OAAO,GACnB,MAAM,MAAM,GAAG,CAAC,MAAM,CAAC;MACrB,IAAI,OAAO;QACT,OAAO,GAAG,CAAC,OAAO;QAClB,MAAM,GAAG,CAAC,KAAK;MACjB;MACA,OAAO;IACT;AAGN,IAAI,WAAW,CAAC,OAAc,SAC5B,MACE,MAAM,OAAO,CAAC,QAAQ,CAAE,SACxB,CAAC,QACC,KAAK,MAAM,OAAO,EAAE,IAAI,IAAI,QAAQ,CAAC,MAAM,SACzC,MAAM,OAAO,QAAQ,GAAG,OAAO,SAAS,CAAC;QACvC,KAAK,IAAI,SAAS,QAAQ,EAAE,CAAE;UAC5B,KAAK,GAAG,CAAC;QACX;QACA,OAAO;MACT;AAGR;;iCAEiC,GACjC,OAAO,IAAI,SACT,CAAC,OAAc,OACf,CAAC,OAAO;IACN,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,OAAO,CAAC,QAAQ,EAAE;IAC5C,OAAO,MAAM,QAAQ,OAAO,UAAU,CAAC;MACrC,IAAI,CAAC,MAAM,IAAI,EAAE;MACjB,IAAI,SAAS;WAAI,IAAI,IAAI;aAAI,MAAM,MAAM;SAAG,CAAC,OAAO,CAAC,CAAC,MAAQ;eAAI;WAAI;OAAG;MACzE,OAAO,MAAM,SAAS,OAAO,SAAS,CAAC;QACrC,IAAI,OAAO;aAAI;SAAM,CAAC,MAAM,CAAC,CAAC,GAAG,OAAO,GACtC,CAAC;eAAI;WAAO,CAAC,IAAI,CAAC,CAAC,QAAU,KAAK,GAAG,CAAC,SACtC,GAAG,CAAC,CAAC,CAAC,IAAI,GAAK;QACjB,IAAI,KAAK,MAAM,EAAE,OAAO,MAAM,IAAI,CAAE,MAAM;MAC5C;IACF;EACF,EAAC"}
package/purge.ts ADDED
@@ -0,0 +1,72 @@
1
+ // Explicit history purges may release pooled bytes, after every surviving
2
+ // graph and plugin copy has had a chance to name the content it still needs.
3
+ import type { Bundle, Graph, Plugin } from 'jsr:@yaks/graph@0.2.36'
4
+ import { after, each } from '@yaks/fp'
5
+ import type { Body } from './props.ts'
6
+ import { address, type Blobs, decode } from './store.js'
7
+
8
+ let strings = (value: unknown): string[] => {
9
+ if (typeof value == 'string') return [value]
10
+ if (!value || typeof value != 'object') return []
11
+ return Object.values(value).flatMap(strings)
12
+ }
13
+
14
+ let candidates = (removed: Bundle[]): Map<string, Set<string>> => {
15
+ let found = new Map<string, Set<string>>()
16
+ let keep = (sha: string, text: string) => {
17
+ let values = found.get(sha) ?? new Set<string>([sha])
18
+ values.add(text)
19
+ found.set(sha, values)
20
+ }
21
+ for (let text of strings(removed)) {
22
+ keep(address(text), text)
23
+ if (/^[a-f0-9]{64}$/.test(text)) keep(text, text)
24
+ }
25
+ return found
26
+ }
27
+
28
+ let present = (store: Blobs, removed: Bundle[]) =>
29
+ each(
30
+ [...candidates(removed)],
31
+ new Map<string, Set<string>>(),
32
+ (found, [sha, values]) =>
33
+ after(store.get(sha), (bytes) => {
34
+ if (bytes) {
35
+ values.add(decode(bytes))
36
+ found.set(sha, values)
37
+ }
38
+ return found
39
+ }),
40
+ )
41
+
42
+ let retained = (graph: Graph, values: string[]) =>
43
+ after(
44
+ graph.storage.retained!(values),
45
+ (found) =>
46
+ each(graph.plugins, new Set(found), (kept, plugin) =>
47
+ after(plugin.retained?.(graph, values), (more) => {
48
+ for (let value of more ?? []) {
49
+ kept.add(value)
50
+ }
51
+ return kept
52
+ })),
53
+ )
54
+
55
+ /** Release only bytes whose last live, historical and trash reference was
56
+ * removed by an explicit purge. A store without retained-value accounting or
57
+ * byte deletion keeps its pool. */
58
+ export let purged =
59
+ (store: Blobs, held: Body[]): NonNullable<Plugin['purged']> =>
60
+ (graph, removed) => {
61
+ if (!store.drop || !graph.storage.retained) return
62
+ return after(present(store, removed), (found) => {
63
+ if (!found.size) return
64
+ let values = [...new Set([...found.values()].flatMap((set) => [...set]))]
65
+ return after(retained(graph, values), (kept) => {
66
+ let free = [...found].filter(([, values]) =>
67
+ ![...values].some((value) => kept.has(value))
68
+ ).map(([sha]) => sha)
69
+ if (free.length) return store.drop!(free, held)
70
+ })
71
+ })
72
+ }
@@ -0,0 +1,34 @@
1
+ // A byte address says which bytes; a representation says how those bytes are
2
+ // served. Its identity includes the app/store scope, type and filename, so a
3
+ // later edit cannot change the headers at an already-issued URL.
4
+ import { sha256 } from "@yaks/graph/sha256";
5
+ import { mediaType } from "./content_type.js";
6
+ export let representation = (scope, address, mime, name = '')=>{
7
+ let row = {
8
+ scope,
9
+ address,
10
+ media_type: mediaType(mime),
11
+ ...name ? {
12
+ name
13
+ } : {}
14
+ };
15
+ let eid = sha256(JSON.stringify([
16
+ 'representation',
17
+ scope,
18
+ row
19
+ ]));
20
+ return {
21
+ eid,
22
+ row,
23
+ path: `${address}/${eid}`
24
+ };
25
+ };
26
+ export let represents = (eid, row)=>/^[0-9a-f]{64}$/.test(row.address) && !!row.scope && row.media_type == mediaType(row.media_type) && representation(row.scope, row.address, row.media_type, row.name).eid == eid;
27
+ export let addressed = (path)=>{
28
+ let match = /^([0-9a-f]{64})(?:\/([0-9a-f]{64}))?$/.exec(path);
29
+ return match ? {
30
+ sha: match[1],
31
+ eid: match[2] ?? null
32
+ } : null;
33
+ };
34
+ //# sourceMappingURL=representation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"representation.js","sources":["./representation.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,6EAA6E;AAC7E,iEAAiE;AAEjE,SAAS,MAAM,kCAAuC;AACtD,SAAS,SAAS,4BAA2B;AAS7C,OAAO,IAAI,iBAAiB,CAC1B,OACA,SACA,MACA,OAAO,EAAE;EAET,IAAI,MAAsB;IACxB;IACA;IACA,YAAY,UAAU;IACtB,GAAI,OAAO;MAAE;IAAK,IAAI,CAAC,CAAC;EAC1B;EACA,IAAI,MAAM,OAAO,KAAK,SAAS,CAAC;IAAC;IAAkB;IAAO;GAAI;EAC9D,OAAO;IAAE;IAAK;IAAK,MAAM,GAAG,QAAQ,CAAC,EAAE,KAAK;EAAC;AAC/C,EAAC;AAED,OAAO,IAAI,aAAa,CACtB,KACA,MAEA,iBAAiB,IAAI,CAAC,IAAI,OAAO,KACjC,CAAC,CAAC,IAAI,KAAK,IAAI,IAAI,UAAU,IAAI,UAAU,IAAI,UAAU,KACzD,eAAe,IAAI,KAAK,EAAE,IAAI,OAAO,EAAE,IAAI,UAAU,EAAE,IAAI,IAAI,EAAE,GAAG,IAAI,IAAG;AAE7E,OAAO,IAAI,YAAY,CACrB;EAEA,IAAI,QAAQ,wCAAwC,IAAI,CAAC;EACzD,OAAO,QAAQ;IAAE,KAAK,KAAK,CAAC,EAAE;IAAE,KAAK,KAAK,CAAC,EAAE,IAAI;EAAK,IAAI;AAC5D,EAAC"}
@@ -0,0 +1,44 @@
1
+ // A byte address says which bytes; a representation says how those bytes are
2
+ // served. Its identity includes the app/store scope, type and filename, so a
3
+ // later edit cannot change the headers at an already-issued URL.
4
+
5
+ import { sha256 } from '@yaks/graph/sha256'
6
+ import { mediaType } from './content_type.js'
7
+
8
+ export type Representation = {
9
+ scope: string
10
+ address: string
11
+ media_type: string
12
+ name?: string
13
+ }
14
+
15
+ export let representation = (
16
+ scope: string,
17
+ address: string,
18
+ mime: string,
19
+ name = '',
20
+ ): { eid: string; row: Representation; path: string } => {
21
+ let row: Representation = {
22
+ scope,
23
+ address,
24
+ media_type: mediaType(mime),
25
+ ...(name ? { name } : {}),
26
+ }
27
+ let eid = sha256(JSON.stringify(['representation', scope, row]))
28
+ return { eid, row, path: `${address}/${eid}` }
29
+ }
30
+
31
+ export let represents = (
32
+ eid: string,
33
+ row: Representation,
34
+ ): boolean =>
35
+ /^[0-9a-f]{64}$/.test(row.address) &&
36
+ !!row.scope && row.media_type == mediaType(row.media_type) &&
37
+ representation(row.scope, row.address, row.media_type, row.name).eid == eid
38
+
39
+ export let addressed = (
40
+ path: string,
41
+ ): { sha: string; eid: string | null } | null => {
42
+ let match = /^([0-9a-f]{64})(?:\/([0-9a-f]{64}))?$/.exec(path)
43
+ return match ? { sha: match[1], eid: match[2] ?? null } : null
44
+ }
@@ -0,0 +1,44 @@
1
+ // A representation is a snapshot: the URL may be cached for a year, so the
2
+ // graph must never change or delete the row that gives that URL its headers.
3
+ import { after } from "@yaks/fp";
4
+ import { hook } from "@yaks/graph/plugin";
5
+ import { dead } from "@yaks/graph/bundle";
6
+ import { Refused } from "@yaks/graph/errors";
7
+ import { represents } from "./representation.js";
8
+ export let representations = ()=>({
9
+ name: 'representations',
10
+ admission: ()=>true,
11
+ hooks: {
12
+ precondition: hook([
13
+ 'representation',
14
+ '$delete',
15
+ 'tombstone'
16
+ ], (bundles, tx)=>{
17
+ let touched = bundles.filter((b)=>dead(b) || b.representation !== undefined);
18
+ if (!touched.length) return bundles;
19
+ return after(tx.get(touched.map((b)=>b.entity.eid), [
20
+ 'representation'
21
+ ]), (found)=>{
22
+ let at = new Map(found.map((b)=>[
23
+ b.entity.eid,
24
+ b
25
+ ]));
26
+ for (let b of touched){
27
+ let old = at.get(b.entity.eid)?.representation;
28
+ let patch = b.representation;
29
+ if (old && (dead(b) || patch === null)) {
30
+ throw new Refused('a blob representation cannot be deleted');
31
+ }
32
+ if (old && patch && Object.entries(patch).some(([k, v])=>v !== old[k])) {
33
+ throw new Refused('a blob representation cannot change');
34
+ }
35
+ if (!old && patch && !represents(b.entity.eid, patch)) {
36
+ throw new Refused('a blob representation must use its derived id');
37
+ }
38
+ }
39
+ return bundles;
40
+ });
41
+ })
42
+ }
43
+ });
44
+ //# sourceMappingURL=representation_rules.js.map