@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/mime.ts ADDED
@@ -0,0 +1,73 @@
1
+ // The media type of a named file. App assets and named uploads use the same
2
+ // vocabulary; a caller's specific Content-Type still takes precedence.
3
+
4
+ let MIME: Record<string, string> = {
5
+ html: 'text/html; charset=utf-8',
6
+ htm: 'text/html; charset=utf-8',
7
+ css: 'text/css; charset=utf-8',
8
+ js: 'text/javascript; charset=utf-8',
9
+ mjs: 'text/javascript; charset=utf-8',
10
+ cjs: 'text/javascript; charset=utf-8',
11
+ json: 'application/json',
12
+ map: 'application/json',
13
+ webmanifest: 'application/manifest+json',
14
+ xml: 'application/xml',
15
+ rss: 'application/rss+xml',
16
+ atom: 'application/atom+xml',
17
+ svg: 'image/svg+xml',
18
+ png: 'image/png',
19
+ apng: 'image/apng',
20
+ jpg: 'image/jpeg',
21
+ jpeg: 'image/jpeg',
22
+ gif: 'image/gif',
23
+ webp: 'image/webp',
24
+ avif: 'image/avif',
25
+ ico: 'image/x-icon',
26
+ txt: 'text/plain; charset=utf-8',
27
+ // TypeScript source, which every browser shows as text; the extension is
28
+ // also MPEG-TS's, but an app's .ts is far more often its source.
29
+ ts: 'text/plain; charset=utf-8',
30
+ mts: 'text/plain; charset=utf-8',
31
+ cts: 'text/plain; charset=utf-8',
32
+ md: 'text/markdown; charset=utf-8',
33
+ csv: 'text/csv; charset=utf-8',
34
+ vtt: 'text/vtt; charset=utf-8',
35
+ ics: 'text/calendar; charset=utf-8',
36
+ woff: 'font/woff',
37
+ woff2: 'font/woff2',
38
+ ttf: 'font/ttf',
39
+ otf: 'font/otf',
40
+ wasm: 'application/wasm',
41
+ glb: 'model/gltf-binary',
42
+ gltf: 'model/gltf+json',
43
+ zip: 'application/zip',
44
+ pdf: 'application/pdf',
45
+ mp4: 'video/mp4',
46
+ m4v: 'video/x-m4v',
47
+ mov: 'video/quicktime',
48
+ webm: 'video/webm',
49
+ ogv: 'video/ogg',
50
+ mp3: 'audio/mpeg',
51
+ m4a: 'audio/mp4',
52
+ aac: 'audio/aac',
53
+ wav: 'audio/wav',
54
+ oga: 'audio/ogg',
55
+ ogg: 'audio/ogg',
56
+ opus: 'audio/ogg',
57
+ flac: 'audio/flac',
58
+ m3u8: 'application/vnd.apple.mpegurl',
59
+ mpd: 'application/dash+xml',
60
+ }
61
+
62
+ ///
63
+ /// mimeOf('dist/_astro/hero.avif') -> 'image/avif'
64
+ /// mimeOf('feed.xml') -> 'application/xml'
65
+ /// mimeOf('fonts/Inter.ttf') -> 'font/ttf'
66
+ /// mimeOf('src/setup.ts') -> 'text/plain; charset=utf-8'
67
+ /// mimeOf('LICENSE') -> 'application/octet-stream'
68
+ export let mimeOf = (name: string): string => {
69
+ let base = name.split('/').pop() ?? ''
70
+ let dot = base.lastIndexOf('.')
71
+ let ext = dot < 0 ? '' : base.slice(dot + 1).toLowerCase()
72
+ return MIME[ext] ?? 'application/octet-stream'
73
+ }
package/mod.js ADDED
@@ -0,0 +1,97 @@
1
+ /**
2
+ * @yaks/blob — content-addressed storage for a text property, applied without
3
+ * any caller having to know.
4
+ *
5
+ * A blog post's body, a product description, a page of notes: values that are
6
+ * long, often repeated, and awkward in a row. Mark the property and they move:
7
+ *
8
+ * ```ts
9
+ * import { assertObjectMatch } from '@std/assert'
10
+ * import { loadVocab } from '@yaks/vocab'
11
+ * import { graph } from '@yaks/graph'
12
+ * import { storage } from '@yaks/sqlite'
13
+ * import { open } from '@yaks/sqlite/db'
14
+ * import {
15
+ * blobKeywords,
16
+ * blobRead,
17
+ * blobs,
18
+ * blobSchema,
19
+ * sqliteBlobs,
20
+ * } from '@yaks/blob'
21
+ *
22
+ * let blog = {
23
+ * $defs: {
24
+ * post: {
25
+ * component: true,
26
+ * type: 'object',
27
+ * properties: {
28
+ * title: { type: 'string' },
29
+ * // the one declaration this package reads
30
+ * body: { type: 'string', store: 'blob' },
31
+ * },
32
+ * },
33
+ * },
34
+ * }
35
+ * let vocab = loadVocab([blog], [blobKeywords])
36
+ * let driver = open(':memory:')
37
+ * let db = storage(driver, vocab, { derived: blobRead(vocab) })
38
+ * for (let s of [...db.ddl(), ...blobSchema()]) driver.query(s)
39
+ * let store = sqliteBlobs(db.statements)
40
+ * let g = graph({ storage: db, vocab, plugins: [blobs(vocab, store)] })
41
+ *
42
+ * g.apply([{ entity: { eid: 'p1' }, post: { body: 'a long essay…' } }])
43
+ * assertObjectMatch(db.read('.post')[0], { post: { body: 'a long essay…' } })
44
+ * ```
45
+ *
46
+ * Nothing between those two lines mentions blobs. The write went in as text and
47
+ * came back as text; in between, the row kept the SHA-256 of the essay and the
48
+ * essay itself went to the store — once, however many posts quote it.
49
+ *
50
+ * ## What it is made of
51
+ *
52
+ * - **One keyword.** {@link blobKeywords} registers `store` with @yaks/vocab,
53
+ * so a schema can carry the word and this package supplies its meaning. To
54
+ * the schema a body property is a plain string property, and it stays one for
55
+ * validation, routing and queries.
56
+ * - **One plugin.** {@link blobs} replaces the text with its address on the way
57
+ * in and puts it back before `apply()` returns. It runs inside the
58
+ * transaction, so the bytes and the row that addresses them commit together.
59
+ * - **One byte-store interface.** {@link Blobs} is `has`, `get` and `put` over
60
+ * `Uint8Array`, keyed by {@link address}. Three implementations ship with the
61
+ * package: {@link sqliteBlobs} (a table beside your rows — synchronous, and
62
+ * the only one SQL can read through), `fileBlobs` (a directory, from `@yaks/file/blobs`), and
63
+ * {@link objectBlobs} (an S3-shaped bucket, R2 included).
64
+ * - **Two read paths.** Over SQL, {@link blobRead} resolves the address in the
65
+ * statement itself; over any other store, {@link hydrate} resolves the
66
+ * bundles a read returned.
67
+ *
68
+ * Removing the plugin does not strand your data: a body property is a text
69
+ * property holding a hash, and the store is a table of hashes and text.
70
+ *
71
+ * The core — the keyword, the plugin, the interface, and the SQLite store —
72
+ * imports no platform API, so the same code runs on a server, in a worker, and
73
+ * in a browser tab. `fileBlobs` is the one store that needs a filesystem, so it
74
+ * lives in @yaks/file (`@yaks/file/blobs`), which those runtimes never load.
75
+ *
76
+ * @module
77
+ */ export * from "./keywords.js";
78
+ export * from "./props.js";
79
+ export * from "./store.js";
80
+ export * from "./plugin.js";
81
+ export * from "./sqlite.js";
82
+ export * from "./hydrate.js";
83
+ export * from "./object.js";
84
+ export * from "./accepts.js";
85
+ export * from "./parts.js";
86
+ export * from "./sha256.js";
87
+ export * from "./stream.js";
88
+ export * from "./image.js";
89
+ export { broken } from "./broken.js";
90
+ export * from "./serve.js";
91
+ export * from "./mime.js";
92
+ export * from "./content_type.js";
93
+ export * from "./representation.js";
94
+ export * from "./representation_rules.js";
95
+ export { VALUE_LIMIT, valueRange, valueSearch, valueTools } from "./value.js";
96
+ export { addressOf, artifactBytes, artifactDoc, keep } from "./artifact.js";
97
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4EC,GAED,8BAA6B;AAC7B,2BAA0B;AAC1B,2BAA0B;AAC1B,4BAA2B;AAC3B,4BAA2B;AAC3B,6BAA4B;AAC5B,4BAA2B;AAC3B,6BAA4B;AAC5B,2BAA0B;AAC1B,4BAA2B;AAC3B,4BAA2B;AAC3B,2BAA0B;AAC1B,SAAS,MAAM,sBAAqB;AACpC,2BAA0B;AAC1B,0BAAyB;AACzB,kCAAiC;AACjC,oCAAmC;AACnC,0CAAyC;AAEzC,SACE,WAAW,EAEX,UAAU,EAGV,WAAW,EAEX,UAAU,qBACO;AACnB,SACE,SAAS,EAET,aAAa,EACb,WAAW,EAEX,IAAI,wBACgB"}
package/mod.ts ADDED
@@ -0,0 +1,115 @@
1
+ /**
2
+ * @yaks/blob — content-addressed storage for a text property, applied without
3
+ * any caller having to know.
4
+ *
5
+ * A blog post's body, a product description, a page of notes: values that are
6
+ * long, often repeated, and awkward in a row. Mark the property and they move:
7
+ *
8
+ * ```ts
9
+ * import { assertObjectMatch } from '@std/assert'
10
+ * import { loadVocab } from '@yaks/vocab'
11
+ * import { graph } from '@yaks/graph'
12
+ * import { storage } from '@yaks/sqlite'
13
+ * import { open } from '@yaks/sqlite/db'
14
+ * import {
15
+ * blobKeywords,
16
+ * blobRead,
17
+ * blobs,
18
+ * blobSchema,
19
+ * sqliteBlobs,
20
+ * } from '@yaks/blob'
21
+ *
22
+ * let blog = {
23
+ * $defs: {
24
+ * post: {
25
+ * component: true,
26
+ * type: 'object',
27
+ * properties: {
28
+ * title: { type: 'string' },
29
+ * // the one declaration this package reads
30
+ * body: { type: 'string', store: 'blob' },
31
+ * },
32
+ * },
33
+ * },
34
+ * }
35
+ * let vocab = loadVocab([blog], [blobKeywords])
36
+ * let driver = open(':memory:')
37
+ * let db = storage(driver, vocab, { derived: blobRead(vocab) })
38
+ * for (let s of [...db.ddl(), ...blobSchema()]) driver.query(s)
39
+ * let store = sqliteBlobs(db.statements)
40
+ * let g = graph({ storage: db, vocab, plugins: [blobs(vocab, store)] })
41
+ *
42
+ * g.apply([{ entity: { eid: 'p1' }, post: { body: 'a long essay…' } }])
43
+ * assertObjectMatch(db.read('.post')[0], { post: { body: 'a long essay…' } })
44
+ * ```
45
+ *
46
+ * Nothing between those two lines mentions blobs. The write went in as text and
47
+ * came back as text; in between, the row kept the SHA-256 of the essay and the
48
+ * essay itself went to the store — once, however many posts quote it.
49
+ *
50
+ * ## What it is made of
51
+ *
52
+ * - **One keyword.** {@link blobKeywords} registers `store` with @yaks/vocab,
53
+ * so a schema can carry the word and this package supplies its meaning. To
54
+ * the schema a body property is a plain string property, and it stays one for
55
+ * validation, routing and queries.
56
+ * - **One plugin.** {@link blobs} replaces the text with its address on the way
57
+ * in and puts it back before `apply()` returns. It runs inside the
58
+ * transaction, so the bytes and the row that addresses them commit together.
59
+ * - **One byte-store interface.** {@link Blobs} is `has`, `get` and `put` over
60
+ * `Uint8Array`, keyed by {@link address}. Three implementations ship with the
61
+ * package: {@link sqliteBlobs} (a table beside your rows — synchronous, and
62
+ * the only one SQL can read through), `fileBlobs` (a directory, from `@yaks/file/blobs`), and
63
+ * {@link objectBlobs} (an S3-shaped bucket, R2 included).
64
+ * - **Two read paths.** Over SQL, {@link blobRead} resolves the address in the
65
+ * statement itself; over any other store, {@link hydrate} resolves the
66
+ * bundles a read returned.
67
+ *
68
+ * Removing the plugin does not strand your data: a body property is a text
69
+ * property holding a hash, and the store is a table of hashes and text.
70
+ *
71
+ * The core — the keyword, the plugin, the interface, and the SQLite store —
72
+ * imports no platform API, so the same code runs on a server, in a worker, and
73
+ * in a browser tab. `fileBlobs` is the one store that needs a filesystem, so it
74
+ * lives in @yaks/file (`@yaks/file/blobs`), which those runtimes never load.
75
+ *
76
+ * @module
77
+ */
78
+
79
+ export * from './keywords.js'
80
+ export * from './props.js'
81
+ export * from './store.js'
82
+ export * from './plugin.js'
83
+ export * from './sqlite.js'
84
+ export * from './hydrate.js'
85
+ export * from './object.js'
86
+ export * from './accepts.js'
87
+ export * from './parts.js'
88
+ export * from './sha256.js'
89
+ export * from './stream.js'
90
+ export * from './image.js'
91
+ export { broken } from './broken.js'
92
+ export * from './serve.js'
93
+ export * from './mime.js'
94
+ export * from './content_type.js'
95
+ export * from './representation.js'
96
+ export * from './representation_rules.js'
97
+
98
+ export {
99
+ VALUE_LIMIT,
100
+ type ValueRange,
101
+ valueRange,
102
+ type ValueReader,
103
+ type ValueSearch,
104
+ valueSearch,
105
+ type ValueTool,
106
+ valueTools,
107
+ } from './value.js'
108
+ export {
109
+ addressOf,
110
+ type Artifact,
111
+ artifactBytes,
112
+ artifactDoc,
113
+ type ArtifactStore,
114
+ keep,
115
+ } from './artifact.js'
package/object-put.js ADDED
@@ -0,0 +1,90 @@
1
+ import { fixed } from "./fixed.js";
2
+ import { patient } from "./object.js";
3
+ import { sized } from "./parts.js";
4
+ import { gathered, PIECE } from "./stream.js";
5
+ // A fourth piece can be filled while three are in flight.
6
+ let PARALLEL = 3;
7
+ async function* pieces(stream, size) {
8
+ let piece = new Uint8Array(size), at = 0;
9
+ for await (let chunk of stream){
10
+ for(let from = 0; from < chunk.byteLength;){
11
+ let n = Math.min(size - at, chunk.byteLength - from);
12
+ piece.set(chunk.subarray(from, from + n), at);
13
+ at += n;
14
+ from += n;
15
+ if (at == size) {
16
+ yield piece;
17
+ piece = new Uint8Array(size);
18
+ at = 0;
19
+ }
20
+ }
21
+ }
22
+ if (at) yield piece.subarray(0, at);
23
+ }
24
+ export let discardNow = (remove)=>remove();
25
+ /** Write the stream, letting `check` inspect what arrived before the object
26
+ * becomes visible. With multipart, only `complete` publishes the bytes. */ export let publishing = (bucket, pause, discard)=>async (key, sent, check)=>{
27
+ let { createMultipartUpload, resumeMultipartUpload } = bucket;
28
+ if (!createMultipartUpload || !resumeMultipartUpload || sent.size != null && sent.size <= PIECE) {
29
+ let bytes = await gathered(sent);
30
+ let checked = await check();
31
+ await patient(()=>bucket.put(key, bytes), pause);
32
+ return checked;
33
+ }
34
+ let size = sized(sent.size ?? 0, PIECE);
35
+ let next = pieces(sent.stream, size);
36
+ let first = (await next.next()).value ?? new Uint8Array();
37
+ if (first.byteLength < size) {
38
+ let checked = await check();
39
+ await patient(()=>bucket.put(key, first), pause);
40
+ return checked;
41
+ }
42
+ let up = await patient(()=>createMultipartUpload.call(bucket, key), pause).then(({ uploadId })=>resumeMultipartUpload.call(bucket, key, uploadId)).catch(async (error)=>{
43
+ await next.return(undefined);
44
+ throw error;
45
+ });
46
+ let pending = [];
47
+ let complete = [];
48
+ let part = 0;
49
+ let send = (bytes)=>{
50
+ let n = ++part;
51
+ let sent = patient(()=>up.uploadPart(n, bytes), pause);
52
+ sent.catch(()=>{});
53
+ pending.push(sent);
54
+ };
55
+ try {
56
+ send(first);
57
+ for await (let piece of next){
58
+ if (pending.length == PARALLEL) complete.push(await pending.shift());
59
+ send(piece);
60
+ }
61
+ let [checked, last] = await Promise.all([
62
+ check(),
63
+ Promise.all(pending)
64
+ ]);
65
+ await patient(()=>up.complete([
66
+ ...complete,
67
+ ...last
68
+ ]), pause);
69
+ return checked;
70
+ } catch (error) {
71
+ await discard(async ()=>{
72
+ await Promise.allSettled(pending);
73
+ await up.abort();
74
+ }).catch(()=>{});
75
+ throw error;
76
+ }
77
+ };
78
+ /** An unchecked write. A small stream of known length can go straight into
79
+ * one PUT. Larger streams and streams of unknown length use `publishing`. */ export let streaming = (bucket, pause, discard)=>async (key, sent)=>{
80
+ if (sent.size == null || sent.size > PIECE && bucket.createMultipartUpload && bucket.resumeMultipartUpload) return await publishing(bucket, pause, discard)(key, sent, async ()=>{});
81
+ let upload = fixed(sent.stream, sent.size);
82
+ try {
83
+ await bucket.put(key, upload.body);
84
+ await upload.done;
85
+ } catch (error) {
86
+ await upload.cancel(error);
87
+ throw error;
88
+ }
89
+ };
90
+ //# sourceMappingURL=object-put.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object-put.js","sources":["./object-put.ts"],"names":[],"mappings":"AAAA,SAAS,KAAK,qBAAoB;AAClC,SAAsB,OAAO,sBAAqB;AAClD,SAAyB,KAAK,qBAAoB;AAClD,SAAS,QAAQ,EAAE,KAAK,sBAAoC;AAM5D,0DAA0D;AAC1D,IAAI,WAAW;AAEf,gBAAgB,OAAO,MAAkC,EAAE,IAAY;EACrE,IAAI,QAAQ,IAAI,WAAW,OAAO,KAAK;EACvC,WAAW,IAAI,SAAS,OAAQ;IAC9B,IAAK,IAAI,OAAO,GAAG,OAAO,MAAM,UAAU,EAAG;MAC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+ import { type Multipart, sized } from './parts.js'
4
+ import { gathered, PIECE, type Streamed } from './stream.js'
5
+
6
+ type Writer = Pick<Bucket, 'put'> & Partial<Multipart>
7
+ type Pause = Parameters<typeof patient>[1]
8
+ export type Discard = (remove: () => Promise<void>) => Promise<void>
9
+
10
+ // A fourth piece can be filled while three are in flight.
11
+ let PARALLEL = 3
12
+
13
+ async function* pieces(stream: ReadableStream<Uint8Array>, size: number) {
14
+ let piece = new Uint8Array(size), at = 0
15
+ for await (let chunk of stream) {
16
+ for (let from = 0; from < chunk.byteLength;) {
17
+ let n = Math.min(size - at, chunk.byteLength - from)
18
+ piece.set(chunk.subarray(from, from + n), at)
19
+ at += n
20
+ from += n
21
+ if (at == size) {
22
+ yield piece
23
+ piece = new Uint8Array(size)
24
+ at = 0
25
+ }
26
+ }
27
+ }
28
+ if (at) yield piece.subarray(0, at)
29
+ }
30
+
31
+ export let discardNow: Discard = (remove) => remove()
32
+
33
+ /** Write the stream, letting `check` inspect what arrived before the object
34
+ * becomes visible. With multipart, only `complete` publishes the bytes. */
35
+ export let publishing =
36
+ (bucket: Writer, pause: Pause, discard: Discard) =>
37
+ async <T>(
38
+ key: string,
39
+ sent: Streamed,
40
+ check: () => Promise<T>,
41
+ ): Promise<T> => {
42
+ let { createMultipartUpload, resumeMultipartUpload } = bucket
43
+ if (
44
+ !createMultipartUpload || !resumeMultipartUpload ||
45
+ (sent.size != null && sent.size <= PIECE)
46
+ ) {
47
+ let bytes = await gathered(sent)
48
+ let checked = await check()
49
+ await patient(() => bucket.put(key, bytes), pause)
50
+ return checked
51
+ }
52
+
53
+ let size = sized(sent.size ?? 0, PIECE)
54
+ let next = pieces(sent.stream, size)
55
+ let first = (await next.next()).value ?? new Uint8Array()
56
+ if (first.byteLength < size) {
57
+ let checked = await check()
58
+ await patient(() => bucket.put(key, first), pause)
59
+ return checked
60
+ }
61
+
62
+ let up = await patient(() => createMultipartUpload.call(bucket, key), pause)
63
+ .then(({ uploadId }) => resumeMultipartUpload.call(bucket, key, uploadId))
64
+ .catch(async (error) => {
65
+ await next.return(undefined)
66
+ throw error
67
+ })
68
+ let pending: ReturnType<typeof up.uploadPart>[] = []
69
+ let complete: Awaited<ReturnType<typeof up.uploadPart>>[] = []
70
+ let part = 0
71
+ let send = (bytes: Uint8Array<ArrayBuffer>) => {
72
+ let n = ++part
73
+ let sent = patient(() => up.uploadPart(n, bytes), pause)
74
+ sent.catch(() => {})
75
+ pending.push(sent)
76
+ }
77
+ try {
78
+ send(first)
79
+ for await (let piece of next) {
80
+ if (pending.length == PARALLEL) complete.push(await pending.shift()!)
81
+ send(piece)
82
+ }
83
+ let [checked, last] = await Promise.all([check(), Promise.all(pending)])
84
+ await patient(() => up.complete([...complete, ...last]), pause)
85
+ return checked
86
+ } catch (error) {
87
+ await discard(async () => {
88
+ await Promise.allSettled(pending)
89
+ await up.abort()
90
+ }).catch(() => {})
91
+ throw error
92
+ }
93
+ }
94
+
95
+ /** An unchecked write. A small stream of known length can go straight into
96
+ * one PUT. Larger streams and streams of unknown length use `publishing`. */
97
+ export let streaming =
98
+ (bucket: Writer, pause: Pause, discard: Discard) =>
99
+ async (key: string, sent: Streamed) => {
100
+ if (
101
+ sent.size == null ||
102
+ (sent.size > PIECE && bucket.createMultipartUpload &&
103
+ bucket.resumeMultipartUpload)
104
+ ) return await publishing(bucket, pause, discard)(key, sent, async () => {})
105
+
106
+ let upload = fixed(sent.stream, sent.size)
107
+ try {
108
+ await bucket.put(key, upload.body)
109
+ await upload.done
110
+ } catch (error) {
111
+ await upload.cancel(error)
112
+ throw error
113
+ }
114
+ }