@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/browser.ts ADDED
@@ -0,0 +1,179 @@
1
+ import { hasher } from './sha256.js'
2
+
3
+ // A file is read a MiB at a time, a multiple of 64, so only the last read
4
+ // leaves part of a block over. Every part the door asks for is a whole number
5
+ // of MiB, so the state after each MiB is the state some part starts from.
6
+ let MIB = 1024 * 1024
7
+
8
+ /** The digest and the hash state at each whole MiB, for resuming parts. */
9
+ export type FileHash = { sha: string; states: string[] }
10
+
11
+ let hashed = async (blob: Blob, signal?: AbortSignal): Promise<FileHash> => {
12
+ let hash = hasher()
13
+ let states = [hash.end()]
14
+ for (let at = 0; at < blob.size; at += MIB) {
15
+ signal?.throwIfAborted()
16
+ let bytes = new Uint8Array(await blob.slice(at, at + MIB).arrayBuffer())
17
+ hash.add(bytes)
18
+ if (bytes.length == MIB) states.push(hash.end())
19
+ }
20
+ return { sha: hash.end(blob.size), states }
21
+ }
22
+
23
+ /** The SHA-256 address of a file, read in bounded pieces. */
24
+ export let digest = async (blob: Blob): Promise<string> =>
25
+ (await hashed(blob)).sha
26
+
27
+ /** The blob door's upload receipt. */
28
+ export type Uploaded = {
29
+ eid: string
30
+ attachment: string
31
+ url: string
32
+ mime: string
33
+ bytes: number
34
+ w?: number
35
+ h?: number
36
+ }
37
+
38
+ /** Call an upload route relative to the app's API and decode its answer. */
39
+ export type Ask = (path: string, init: RequestInit) => Promise<unknown>
40
+ export type Again = <T>(call: () => Promise<T>) => Promise<T>
41
+
42
+ /** Options for one file sent through the blob door. */
43
+ export type Sending = {
44
+ name?: string
45
+ signal?: AbortSignal
46
+ /** Each checkpoint lists the parts the door has received. */
47
+ progress?: (part: number, sent: number[]) => void | Promise<void>
48
+ /** Reuse a checkpoint for the same bytes after reopening the queue. */
49
+ hash?: FileHash
50
+ prepared?: (hash: FileHash) => void | Promise<void>
51
+ /** Number of requests sending parts of this file concurrently. */
52
+ lanes?: number
53
+ }
54
+
55
+ let SPLIT = 32 * MIB
56
+
57
+ let sendParts = async (
58
+ items: number[],
59
+ lanes: number,
60
+ go: (item: number) => Promise<unknown>,
61
+ ) => {
62
+ let next = 0
63
+ let failure: { error: unknown } | undefined
64
+ let lane = async () => {
65
+ while (!failure && next < items.length) {
66
+ try {
67
+ await go(items[next++])
68
+ } catch (error) {
69
+ failure ??= { error }
70
+ }
71
+ }
72
+ }
73
+ await Promise.all(Array.from({ length: Math.min(lanes, items.length) }, lane))
74
+ if (failure) throw failure.error
75
+ }
76
+
77
+ let receipt = (value: unknown, size: number): Uploaded => {
78
+ let result = value as Uploaded | null
79
+ if (
80
+ !result || !/^[a-f0-9]{64}$/.test(result.eid) ||
81
+ typeof result.attachment != 'string' || !result.attachment ||
82
+ typeof result.url != 'string' || typeof result.mime != 'string' ||
83
+ result.bytes !== size
84
+ ) {
85
+ throw new Error('The server did not confirm the uploaded file')
86
+ }
87
+ return result
88
+ }
89
+
90
+ /** Send a file whole or through the blob door's resumable multipart protocol.
91
+ * Each multipart attempt asks the door which parts still need to arrive. */
92
+ export let uploading = (
93
+ ask: Ask,
94
+ again: Again = (call) => call(),
95
+ ): (file: Blob, opts?: Sending) => Promise<Uploaded> =>
96
+ async (
97
+ file,
98
+ {
99
+ name: named,
100
+ signal,
101
+ progress,
102
+ hash,
103
+ prepared,
104
+ lanes = 4,
105
+ } = {},
106
+ ) => {
107
+ let name = named ?? (file as File).name ?? ''
108
+ let mime = file.type || 'application/octet-stream'
109
+ let send = (path: string, init: RequestInit) =>
110
+ again(() => {
111
+ signal?.throwIfAborted()
112
+ return ask(path, { ...init, signal })
113
+ })
114
+ if (file.size < SPLIT) {
115
+ return receipt(
116
+ await send('blob', {
117
+ method: 'POST',
118
+ headers: {
119
+ 'content-type': mime,
120
+ ...(name ? { 'x-yak-name': encodeURIComponent(name) } : {}),
121
+ },
122
+ body: file,
123
+ }),
124
+ file.size,
125
+ )
126
+ }
127
+ let found = hash ?? await hashed(file, signal)
128
+ if (!hash) await prepared?.(found)
129
+ let { sha, states } = found
130
+ let at = `blob/${sha}`
131
+ let stand = () =>
132
+ send(at, {
133
+ method: 'POST',
134
+ headers: { 'content-type': 'application/json' },
135
+ body: JSON.stringify({
136
+ bytes: file.size,
137
+ name: encodeURIComponent(name),
138
+ mime,
139
+ }),
140
+ }) as Promise<Uploaded | { part: number; need: number[] }>
141
+
142
+ // The bucket may have expired the upload since the last POST.
143
+ for (let round = 0; round < 4; round++) {
144
+ let held = await stand()
145
+ if (!held || typeof held != 'object' || !('need' in held)) {
146
+ return receipt(held, file.size)
147
+ }
148
+ if (
149
+ !Number.isInteger(held.part / MIB) || held.part <= 0 ||
150
+ !Array.isArray(held.need)
151
+ ) {
152
+ throw new Error('The server did not say which parts it needs')
153
+ }
154
+ let { part, need } = held
155
+ let missing = new Set(need)
156
+ let sent = new Set(
157
+ Array.from(
158
+ { length: Math.ceil(file.size / part) },
159
+ (_, i) => i + 1,
160
+ ).filter((n) => !missing.has(n)),
161
+ )
162
+ let checked = () => progress?.(part, [...sent].sort((a, b) => a - b))
163
+ await checked()
164
+ try {
165
+ await sendParts(need, lanes, async (n) => {
166
+ await send(`${at}/${n}`, {
167
+ method: 'PUT',
168
+ headers: { 'x-yak-state': states[(n - 1) * part / MIB] },
169
+ body: file.slice((n - 1) * part, n * part),
170
+ })
171
+ sent.add(n)
172
+ await checked()
173
+ })
174
+ } catch (error) {
175
+ if ((error as { status?: number })?.status != 409) throw error
176
+ }
177
+ }
178
+ throw new Error(`The upload of ${name || 'a file'} did not finish`)
179
+ }
package/compare.js ADDED
@@ -0,0 +1,82 @@
1
+ // Replay the shared prefix from storage, followed by the still-unread bytes
2
+ // of the response. The comparison only holds one chunk from each stream.
3
+ let replay = async (original, length, chunk, rest)=>{
4
+ let prefix = length ? (await original.read({
5
+ from: 0,
6
+ to: length - 1
7
+ })).getReader() : null;
8
+ return new ReadableStream({
9
+ pull: async (out)=>{
10
+ if (prefix) {
11
+ let got = await prefix.read();
12
+ if (!got.done) return out.enqueue(got.value);
13
+ prefix.releaseLock();
14
+ prefix = null;
15
+ }
16
+ if (chunk) {
17
+ out.enqueue(chunk);
18
+ chunk = undefined;
19
+ return;
20
+ }
21
+ let got = await rest.read();
22
+ if (got.done) {
23
+ rest.releaseLock();
24
+ out.close();
25
+ } else out.enqueue(got.value);
26
+ },
27
+ cancel: async (why)=>{
28
+ await Promise.all([
29
+ prefix?.cancel(why),
30
+ rest.cancel(why)
31
+ ]);
32
+ }
33
+ });
34
+ };
35
+ /** Null when `sent` is the stored object's exact bytes. Otherwise return
36
+ * the original response stream, including the bytes read to compare it. */ export let differing = async (sent, original)=>{
37
+ let source = (await original.read()).getReader();
38
+ let reader = sent.getReader();
39
+ let prefix = 0;
40
+ let expected = new Uint8Array(0);
41
+ let offset = 0;
42
+ let more = async ()=>{
43
+ while(offset == expected.length){
44
+ let got = await source.read();
45
+ if (got.done) return false;
46
+ expected = got.value;
47
+ offset = 0;
48
+ }
49
+ return true;
50
+ };
51
+ let matches = async (chunk)=>{
52
+ for(let at = 0; at < chunk.length;){
53
+ if (!await more()) return false;
54
+ let length = Math.min(chunk.length - at, expected.length - offset);
55
+ for(let i = 0; i < length; i++){
56
+ if (chunk[at + i] != expected[offset + i]) return false;
57
+ }
58
+ at += length;
59
+ offset += length;
60
+ }
61
+ return true;
62
+ };
63
+ try {
64
+ while(true){
65
+ let got = await reader.read();
66
+ if (got.done && !await more()) {
67
+ reader.releaseLock();
68
+ return null;
69
+ }
70
+ if (got.done || !await matches(got.value)) {
71
+ return await replay(original, prefix, got.value, reader);
72
+ }
73
+ prefix += got.value.length;
74
+ }
75
+ } catch (error) {
76
+ await reader.cancel().catch(()=>{});
77
+ throw error;
78
+ } finally{
79
+ await source.cancel();
80
+ }
81
+ };
82
+ //# sourceMappingURL=compare.js.map
package/compare.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compare.js","sources":["./compare.ts"],"names":[],"mappings":"AAEA,4EAA4E;AAC5E,yEAAyE;AACzE,IAAI,SAAS,OACX,UACA,QACA,OACA;EAEA,IAAI,SAAS,SACT,CAAC,MAAM,SAAS,IAAI,CAAC;IAAE,MAAM;IAAG,IAAI,SAAS;EAAE,EAAE,EAChD,SAAS,KACV;EACJ,OAAO,IAAI,eAA2B;IACpC,MAAM,OAAO;MACX,IAAI,QAAQ;QACV,IAAI,MAAM,MAAM,OAAO,IAAI;QAC3B,IAAI,CAAC,IAAI,IAAI,EAAE,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK;QAC3C,OAAO,WAAW;QAClB,SAAS;MACX;MACA,IAAI,OAAO;QACT,IAAI,OAAO,CAAC;QACZ,QAAQ;QACR;MACF;MACA,IAAI,MAAM,MAAM,KAAK,IAAI;MACzB,IAAI,IAAI,IAAI,EAAE;QACZ,KAAK,WAAW;QAChB,IAAI,KAAK;MACX,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK;IAC9B;IACA,QAAQ,OAAO;MACb,MAAM,QAAQ,GAAG,CAAC;QAAC,QAAQ,OAAO;QAAM,KAAK,MAAM,CAAC;OAAK;IAC3D;EACF;AACF;AAEA;yEACyE,GACzE,OAAO,IAAI,YAAY,OACrB,MACA;EAEA,IAAI,SAAS,CAAC,MAAM,SAAS,IAAI,EAAE,EAAE,SAAS;EAC9C,IAAI,SAAS,KAAK,SAAS;EAC3B,IAAI,SAAS;EACb,IAAI,WAAuB,IAAI,WAAW;EAC1C,IAAI,SAAS;EACb,IAAI,OAAO;IACT,MAAO,UAAU,SAAS,MAAM,CAAE;MAChC,IAAI,MAAM,MAAM,OAAO,IAAI;MAC3B,IAAI,IAAI,IAAI,EAAE,OAAO;MACrB,WAAW,IAAI,KAAK;MACpB,SAAS;IACX;IACA,OAAO;EACT;EACA,IAAI,UAAU,OAAO;IACnB,IAAK,IAAI,KAAK,GAAG,KAAK,MAAM,MAAM,EAAG;MACnC,IAAI,CAAC,MAAM,QAAQ,OAAO;MAC1B,IAAI,SAAS,KAAK,GAAG,CAAC,MAAM,MAAM,GAAG,IAAI,SAAS,MAAM,GAAG;MAC3D,IAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,IAAK;QAC/B,IAAI,KAAK,CAAC,KAAK,EAAE,IAAI,QAAQ,CAAC,SAAS,EAAE,EAAE,OAAO;MACpD;MACA,MAAM;MACN,UAAU;IACZ;IACA,OAAO;EACT;EACA,IAAI;IACF,MAAO,KAAM;MACX,IAAI,MAAM,MAAM,OAAO,IAAI;MAC3B,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,QAAQ;QAC7B,OAAO,WAAW;QAClB,OAAO;MACT;MACA,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,QAAQ,IAAI,KAAK,GAAG;QACzC,OAAO,MAAM,OAAO,UAAU,QAAQ,IAAI,KAAK,EAAE;MACnD;MACA,UAAU,IAAI,KAAK,CAAC,MAAM;IAC5B;EACF,EAAE,OAAO,OAAO;IACd,MAAM,OAAO,MAAM,GAAG,KAAK,CAAC,KAAO;IACnC,MAAM;EACR,SAAU;IACR,MAAM,OAAO,MAAM;EACrB;AACF,EAAC"}
package/compare.ts ADDED
@@ -0,0 +1,90 @@
1
+ import type { Opened } from './object.ts'
2
+
3
+ // Replay the shared prefix from storage, followed by the still-unread bytes
4
+ // of the response. The comparison only holds one chunk from each stream.
5
+ let replay = async (
6
+ original: Opened,
7
+ length: number,
8
+ chunk: Uint8Array | undefined,
9
+ rest: ReadableStreamDefaultReader<Uint8Array>,
10
+ ) => {
11
+ let prefix = length
12
+ ? (await original.read({ from: 0, to: length - 1 }))
13
+ .getReader()
14
+ : null
15
+ return new ReadableStream<Uint8Array>({
16
+ pull: async (out) => {
17
+ if (prefix) {
18
+ let got = await prefix.read()
19
+ if (!got.done) return out.enqueue(got.value)
20
+ prefix.releaseLock()
21
+ prefix = null
22
+ }
23
+ if (chunk) {
24
+ out.enqueue(chunk)
25
+ chunk = undefined
26
+ return
27
+ }
28
+ let got = await rest.read()
29
+ if (got.done) {
30
+ rest.releaseLock()
31
+ out.close()
32
+ } else out.enqueue(got.value)
33
+ },
34
+ cancel: async (why) => {
35
+ await Promise.all([prefix?.cancel(why), rest.cancel(why)])
36
+ },
37
+ })
38
+ }
39
+
40
+ /** Null when `sent` is the stored object's exact bytes. Otherwise return
41
+ * the original response stream, including the bytes read to compare it. */
42
+ export let differing = async (
43
+ sent: ReadableStream<Uint8Array>,
44
+ original: Opened,
45
+ ): Promise<ReadableStream<Uint8Array> | null> => {
46
+ let source = (await original.read()).getReader()
47
+ let reader = sent.getReader()
48
+ let prefix = 0
49
+ let expected: Uint8Array = new Uint8Array(0)
50
+ let offset = 0
51
+ let more = async () => {
52
+ while (offset == expected.length) {
53
+ let got = await source.read()
54
+ if (got.done) return false
55
+ expected = got.value
56
+ offset = 0
57
+ }
58
+ return true
59
+ }
60
+ let matches = async (chunk: Uint8Array) => {
61
+ for (let at = 0; at < chunk.length;) {
62
+ if (!await more()) return false
63
+ let length = Math.min(chunk.length - at, expected.length - offset)
64
+ for (let i = 0; i < length; i++) {
65
+ if (chunk[at + i] != expected[offset + i]) return false
66
+ }
67
+ at += length
68
+ offset += length
69
+ }
70
+ return true
71
+ }
72
+ try {
73
+ while (true) {
74
+ let got = await reader.read()
75
+ if (got.done && !await more()) {
76
+ reader.releaseLock()
77
+ return null
78
+ }
79
+ if (got.done || !await matches(got.value)) {
80
+ return await replay(original, prefix, got.value, reader)
81
+ }
82
+ prefix += got.value.length
83
+ }
84
+ } catch (error) {
85
+ await reader.cancel().catch(() => {})
86
+ throw error
87
+ } finally {
88
+ await source.cancel()
89
+ }
90
+ }
@@ -0,0 +1,43 @@
1
+ // The media type of content-addressed bytes. Binary signatures win over an
2
+ // upload's declared type; text formats cannot be identified from their bytes,
3
+ // so their first validated declaration is kept beside the object.
4
+ import { mediaTypeOf } from "./image.js";
5
+ const UNKNOWN = 'application/octet-stream';
6
+ /** A declared media type with no parameters or header control characters. */ export let mediaType = (declared = '')=>{
7
+ let mime = declared.split(';')[0].trim().toLowerCase();
8
+ return /^[a-z0-9.+-]+\/[a-z0-9.+-]+$/.test(mime) ? mime : UNKNOWN;
9
+ };
10
+ /**
11
+ * Whether bytes are text: UTF-8 with no control character but tab, line feed
12
+ * and carriage return. For the first bytes of a file (`whole` false), a
13
+ * character the cut split in two is not held against them.
14
+ *
15
+ * ```ts
16
+ * import { assertEquals } from '@std/assert'
17
+ * import { utf8 } from '@yaks/blob'
18
+ *
19
+ * let bytes = (s: string) => new TextEncoder().encode(s)
20
+ * assertEquals(utf8(bytes('<h1>Café</h1>\n')), true)
21
+ * assertEquals(utf8(new Uint8Array([0x89, 0x50, 0x4e, 0x47])), false)
22
+ * assertEquals(utf8(bytes('a\x00b')), false)
23
+ * assertEquals(utf8(bytes('Café').subarray(0, 4)), false)
24
+ * assertEquals(utf8(bytes('Café').subarray(0, 4), false), true)
25
+ * ```
26
+ */ export let utf8 = (bytes, whole = true)=>{
27
+ try {
28
+ let text = new TextDecoder('utf-8', {
29
+ fatal: true
30
+ }).decode(bytes, {
31
+ stream: !whole
32
+ });
33
+ // deno-lint-ignore no-control-regex
34
+ return !/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(text);
35
+ } catch {
36
+ return false;
37
+ }
38
+ };
39
+ let at = (bytes, offset, value)=>bytes.length >= offset + value.length && [
40
+ ...value
41
+ ].every((c, i)=>bytes[offset + i] == c.charCodeAt(0));
42
+ /** Formats whose declaration can be checked without a file-type guess. */ export let matchesMediaType = (bytes, mime)=>mediaTypeOf(bytes) == mime || mime == 'audio/mpeg' && (at(bytes, 0, 'ID3') || bytes.length >= 3 && bytes[0] == 255 && (bytes[1] & 224) == 224) || mime == 'audio/wav' && at(bytes, 0, 'RIFF') && at(bytes, 8, 'WAVE') || mime == 'audio/flac' && at(bytes, 0, 'fLaC') || mime == 'audio/opus' && (at(bytes, 0, 'OggS') || at(bytes, 0, 'OpusHead')) || mime == 'audio/pcm' && bytes.length > 0 && bytes.length % 2 == 0;
43
+ //# sourceMappingURL=content_type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content_type.js","sources":["./content_type.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,8EAA8E;AAC9E,kEAAkE;AAElE,SAAS,WAAW,qBAAoB;AAExC,MAAM,UAAU;AAEhB,2EAA2E,GAC3E,OAAO,IAAI,YAAY,CAAC,WAAW,EAAE;EACnC,IAAI,OAAO,SAAS,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,GAAG,WAAW;EACpD,OAAO,+BAA+B,IAAI,CAAC,QAAQ,OAAO;AAC5D,EAAC;AAED;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,IAAI,OAAO,CAAC,OAAmB,QAAQ,IAAI;EAChD,IAAI;IACF,IAAI,OAAO,IAAI,YAAY,SAAS;MAAE,OAAO;IAAK,GAC/C,MAAM,CAAC,OAAO;MAAE,QAAQ,CAAC;IAAM;IAClC,oCAAoC;IACpC,OAAO,CAAC,mCAAmC,IAAI,CAAC;EAClD,EAAE,OAAM;IACN,OAAO;EACT;AACF,EAAC;AAED,IAAI,KAAK,CAAC,OAAmB,QAAgB,QAC3C,MAAM,MAAM,IAAI,SAAS,MAAM,MAAM,IACrC;OAAI;GAAM,CAAC,KAAK,CAAC,CAAC,GAAG,IAAM,KAAK,CAAC,SAAS,EAAE,IAAI,EAAE,UAAU,CAAC;AAE/D,wEAAwE,GACxE,OAAO,IAAI,mBAAmB,CAAC,OAAmB,OAChD,YAAY,UAAU,QACrB,QAAQ,gBACP,CAAC,GAAG,OAAO,GAAG,UACX,MAAM,MAAM,IAAI,KAAK,KAAK,CAAC,EAAE,IAAI,OAChC,CAAC,KAAK,CAAC,EAAE,GAAG,GAAG,KAAK,GAAI,KAC7B,QAAQ,eAAe,GAAG,OAAO,GAAG,WAAW,GAAG,OAAO,GAAG,WAC5D,QAAQ,gBAAgB,GAAG,OAAO,GAAG,WACrC,QAAQ,gBACP,CAAC,GAAG,OAAO,GAAG,WAAW,GAAG,OAAO,GAAG,WAAW,KAClD,QAAQ,eAAe,MAAM,MAAM,GAAG,KAAK,MAAM,MAAM,GAAG,KAAK,EAAE"}
@@ -0,0 +1,58 @@
1
+ // The media type of content-addressed bytes. Binary signatures win over an
2
+ // upload's declared type; text formats cannot be identified from their bytes,
3
+ // so their first validated declaration is kept beside the object.
4
+
5
+ import { mediaTypeOf } from './image.js'
6
+
7
+ const UNKNOWN = 'application/octet-stream'
8
+
9
+ /** A declared media type with no parameters or header control characters. */
10
+ export let mediaType = (declared = ''): string => {
11
+ let mime = declared.split(';')[0].trim().toLowerCase()
12
+ return /^[a-z0-9.+-]+\/[a-z0-9.+-]+$/.test(mime) ? mime : UNKNOWN
13
+ }
14
+
15
+ /**
16
+ * Whether bytes are text: UTF-8 with no control character but tab, line feed
17
+ * and carriage return. For the first bytes of a file (`whole` false), a
18
+ * character the cut split in two is not held against them.
19
+ *
20
+ * ```ts
21
+ * import { assertEquals } from '@std/assert'
22
+ * import { utf8 } from '@yaks/blob'
23
+ *
24
+ * let bytes = (s: string) => new TextEncoder().encode(s)
25
+ * assertEquals(utf8(bytes('<h1>Café</h1>\n')), true)
26
+ * assertEquals(utf8(new Uint8Array([0x89, 0x50, 0x4e, 0x47])), false)
27
+ * assertEquals(utf8(bytes('a\x00b')), false)
28
+ * assertEquals(utf8(bytes('Café').subarray(0, 4)), false)
29
+ * assertEquals(utf8(bytes('Café').subarray(0, 4), false), true)
30
+ * ```
31
+ */
32
+ export let utf8 = (bytes: Uint8Array, whole = true): boolean => {
33
+ try {
34
+ let text = new TextDecoder('utf-8', { fatal: true })
35
+ .decode(bytes, { stream: !whole })
36
+ // deno-lint-ignore no-control-regex
37
+ return !/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(text)
38
+ } catch {
39
+ return false
40
+ }
41
+ }
42
+
43
+ let at = (bytes: Uint8Array, offset: number, value: string) =>
44
+ bytes.length >= offset + value.length &&
45
+ [...value].every((c, i) => bytes[offset + i] == c.charCodeAt(0))
46
+
47
+ /** Formats whose declaration can be checked without a file-type guess. */
48
+ export let matchesMediaType = (bytes: Uint8Array, mime: string): boolean =>
49
+ mediaTypeOf(bytes) == mime ||
50
+ (mime == 'audio/mpeg' &&
51
+ (at(bytes, 0, 'ID3') ||
52
+ (bytes.length >= 3 && bytes[0] == 255 &&
53
+ (bytes[1] & 224) == 224))) ||
54
+ (mime == 'audio/wav' && at(bytes, 0, 'RIFF') && at(bytes, 8, 'WAVE')) ||
55
+ (mime == 'audio/flac' && at(bytes, 0, 'fLaC')) ||
56
+ (mime == 'audio/opus' &&
57
+ (at(bytes, 0, 'OggS') || at(bytes, 0, 'OpusHead'))) ||
58
+ (mime == 'audio/pcm' && bytes.length > 0 && bytes.length % 2 == 0)
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts","./testing.ts","./workers.json","./conform.ts"]},"name":"@yaks/blob","version":"0.2.36","license":"Apache-2.0","description":"Stores text and binary content under SHA-256 addresses, with transparent graph text reads and writes, verified artifacts and HTTP responses.","exports":{".":"./mod.ts","./attachment":"./attachment.ts","./identify":"./identify.ts","./object":"./object.ts","./backend":"./backend.ts","./store":"./store.ts","./stream":"./stream.ts","./compare":"./compare.ts","./sqlite":"./sqlite.ts","./vocab":"./vocab.ts","./graph":"./graph.ts","./routes":"./routes.ts","./upload":"./upload.ts","./serve":"./serve.ts","./png":"./png.ts","./browser":"./browser.ts"},"imports":{"@yaks/blob":"./mod.ts","@yaks/blob/attachment":"./attachment.ts","@yaks/blob/identify":"./identify.ts","@yaks/blob/object":"./object.ts","@yaks/blob/backend":"./backend.ts","@yaks/blob/store":"./store.ts","@yaks/blob/stream":"./stream.ts","@yaks/blob/compare":"./compare.ts","@yaks/blob/sqlite":"./sqlite.ts","@yaks/blob/vocab":"./vocab.ts","@yaks/blob/graph":"./graph.ts","@yaks/blob/routes":"./routes.ts","@yaks/blob/upload":"./upload.ts","@yaks/blob/serve":"./serve.ts","@yaks/blob/png":"./png.ts","@yaks/blob/browser":"./browser.ts"}}
package/fixed.js ADDED
@@ -0,0 +1,53 @@
1
+ // Own the reader behind the stream given to R2. The binding may still have
2
+ // that stream locked when it rejects, so cleanup acts on our reader directly.
3
+ let owned = (source)=>{
4
+ let reader = source.getReader();
5
+ let cancelled = false;
6
+ let cancel = (reason)=>reader.cancel(reason);
7
+ return {
8
+ body: new ReadableStream({
9
+ pull: async (controller)=>{
10
+ let got = await reader.read();
11
+ if (cancelled) return;
12
+ if (got.done) controller.close();
13
+ else controller.enqueue(got.value);
14
+ },
15
+ cancel: (reason)=>{
16
+ cancelled = true;
17
+ return cancel(reason);
18
+ }
19
+ }),
20
+ cancel
21
+ };
22
+ };
23
+ let exact = (length)=>{
24
+ let Fixed = globalThis.FixedLengthStream;
25
+ return Fixed && new Fixed(length);
26
+ };
27
+ export let fixed = (stream, length, make = exact)=>{
28
+ let input = owned(stream);
29
+ let exact1 = make(length);
30
+ if (!exact1) return {
31
+ ...input,
32
+ done: Promise.resolve()
33
+ };
34
+ let abort = new AbortController();
35
+ let done = input.body.pipeTo(exact1.writable, {
36
+ signal: abort.signal
37
+ });
38
+ done.catch(()=>{});
39
+ let output = exact1.readable;
40
+ return {
41
+ body: output,
42
+ done,
43
+ cancel: async (reason)=>{
44
+ abort.abort(reason);
45
+ await Promise.allSettled([
46
+ input.cancel(reason),
47
+ output.cancel(reason)
48
+ ]);
49
+ await done.catch(()=>{});
50
+ }
51
+ };
52
+ };
53
+ //# sourceMappingURL=fixed.js.map
package/fixed.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fixed.js","sources":["./fixed.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,8EAA8E;AAC9E,IAAI,QAAQ,CAAC;EACX,IAAI,SAAS,OAAO,SAAS;EAC7B,IAAI,YAAY;EAChB,IAAI,SAAS,CAAC,SAAqB,OAAO,MAAM,CAAC;EACjD,OAAO;IACL,MAAM,IAAI,eAA2B;MACnC,MAAM,OAAO;QACX,IAAI,MAAM,MAAM,OAAO,IAAI;QAC3B,IAAI,WAAW;QACf,IAAI,IAAI,IAAI,EAAE,WAAW,KAAK;aACzB,WAAW,OAAO,CAAC,IAAI,KAAK;MACnC;MACA,QAAQ,CAAC;QACP,YAAY;QACZ,OAAO,OAAO;MAChB;IACF;IACA;EACF;AACF;AASA,IAAI,QAAe,CAAC;EAClB,IAAI,QAAQ,AAAC,WAIV,iBAAiB;EACpB,OAAO,SAAS,IAAI,MAAM;AAC5B;AAEA,OAAO,IAAI,QAAQ,CACjB,QACA,QACA,OAAO,KAAK;EAEZ,IAAI,QAAQ,MAAM;EAClB,IAAI,SAAQ,KAAK;EACjB,IAAI,CAAC,QAAO,OAAO;IAAE,GAAG,KAAK;IAAE,MAAM,QAAQ,OAAO;EAAG;EACvD,IAAI,QAAQ,IAAI;EAChB,IAAI,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,OAAM,QAAQ,EAAE;IAAE,QAAQ,MAAM,MAAM;EAAC;EACpE,KAAK,KAAK,CAAC,KAAO;EAClB,IAAI,SAAS,OAAM,QAAQ;EAC3B,OAAO;IACL,MAAM;IACN;IACA,QAAQ,OAAO;MACb,MAAM,KAAK,CAAC;MACZ,MAAM,QAAQ,UAAU,CAAC;QAAC,MAAM,MAAM,CAAC;QAAS,OAAO,MAAM,CAAC;OAAQ;MACtE,MAAM,KAAK,KAAK,CAAC,KAAO;IAC1B;EACF;AACF,EAAC"}
package/fixed.ts ADDED
@@ -0,0 +1,61 @@
1
+ // Own the reader behind the stream given to R2. The binding may still have
2
+ // that stream locked when it rejects, so cleanup acts on our reader directly.
3
+ let owned = (source: ReadableStream<Uint8Array>) => {
4
+ let reader = source.getReader()
5
+ let cancelled = false
6
+ let cancel = (reason?: unknown) => reader.cancel(reason)
7
+ return {
8
+ body: new ReadableStream<Uint8Array>({
9
+ pull: async (controller) => {
10
+ let got = await reader.read()
11
+ if (cancelled) return
12
+ if (got.done) controller.close()
13
+ else controller.enqueue(got.value)
14
+ },
15
+ cancel: (reason) => {
16
+ cancelled = true
17
+ return cancel(reason)
18
+ },
19
+ }),
20
+ cancel,
21
+ }
22
+ }
23
+
24
+ // R2 uploads and HTTP responses both require the Worker's native stream
25
+ // length. Retain both ends and the pipe, so a rejected upload can stop it
26
+ // even before the binding consumes its body.
27
+ type Exact = (
28
+ length: number,
29
+ ) => TransformStream<Uint8Array, Uint8Array> | undefined
30
+
31
+ let exact: Exact = (length) => {
32
+ let Fixed = (globalThis as {
33
+ FixedLengthStream?: new (
34
+ length: number,
35
+ ) => TransformStream<Uint8Array, Uint8Array>
36
+ }).FixedLengthStream
37
+ return Fixed && new Fixed(length)
38
+ }
39
+
40
+ export let fixed = (
41
+ stream: ReadableStream<Uint8Array>,
42
+ length: number,
43
+ make = exact,
44
+ ) => {
45
+ let input = owned(stream)
46
+ let exact = make(length)
47
+ if (!exact) return { ...input, done: Promise.resolve() }
48
+ let abort = new AbortController()
49
+ let done = input.body.pipeTo(exact.writable, { signal: abort.signal })
50
+ done.catch(() => {})
51
+ let output = exact.readable
52
+ return {
53
+ body: output,
54
+ done,
55
+ cancel: async (reason?: unknown) => {
56
+ abort.abort(reason)
57
+ await Promise.allSettled([input.cancel(reason), output.cancel(reason)])
58
+ await done.catch(() => {})
59
+ },
60
+ }
61
+ }
package/graph.js ADDED
@@ -0,0 +1,15 @@
1
+ // Where a stored value lives: the module a server imports from
2
+ // `@yaks/blob/graph`. The store is the host's own (`host.blobs`); the schema declarations are in ./vocab.ts,
3
+ // where a browser can load them without a store coming with them.
4
+ import { blobs } from "./plugin.js";
5
+ import { representations } from "./representation_rules.js";
6
+ /** The plugin for every body property this vocabulary marks `store: blob`,
7
+ * storing each distinct value once in the host's blob store however many rows
8
+ * hold the same text. */ export let plugins = (host)=>[
9
+ blobs(host.vocab, host.blobs),
10
+ representations()
11
+ ];
12
+ /** A store this plugin wrote keeps an address where each body's text was,
13
+ * which only this plugin reads back as text and writes in its place: from
14
+ * then on the store requires it (@yaks/host `GraphFacet.required`). */ export let required = ()=>true;
15
+ //# sourceMappingURL=graph.js.map
package/graph.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph.js","sources":["./graph.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,6GAA6G;AAC7G,kEAAkE;AAIlE,SAAS,KAAK,sBAAqB;AACnC,SAAS,eAAe,oCAAmC;AAG3D;;uBAEuB,GACvB,OAAO,IAAI,UAAU,CAAC,OAAmD;IACvE,MAAM,KAAK,KAAK,EAAE,KAAK,KAAK;IAC5B;GACD,CAAA;AAED;;qEAEqE,GACrE,OAAO,IAAI,WAAW,IAAe,KAAI"}
package/graph.ts ADDED
@@ -0,0 +1,22 @@
1
+ // Where a stored value lives: the module a server imports from
2
+ // `@yaks/blob/graph`. The store is the host's own (`host.blobs`); the schema declarations are in ./vocab.ts,
3
+ // where a browser can load them without a store coming with them.
4
+
5
+ import type { Plugin } from 'jsr:@yaks/graph@0.2.36'
6
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
7
+ import { blobs } from './plugin.js'
8
+ import { representations } from './representation_rules.js'
9
+ import type { Blobs } from './store.ts'
10
+
11
+ /** The plugin for every body property this vocabulary marks `store: blob`,
12
+ * storing each distinct value once in the host's blob store however many rows
13
+ * hold the same text. */
14
+ export let plugins = (host: { vocab: Vocab; blobs: Blobs }): Plugin[] => [
15
+ blobs(host.vocab, host.blobs),
16
+ representations(),
17
+ ]
18
+
19
+ /** A store this plugin wrote keeps an address where each body's text was,
20
+ * which only this plugin reads back as text and writes in its place: from
21
+ * then on the store requires it (@yaks/host `GraphFacet.required`). */
22
+ export let required = (): boolean => true