@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.
- package/LICENSE +202 -0
- package/README.md +946 -2
- package/_dist/accepts.d.ts +38 -0
- package/_dist/accepts.d.ts.map +1 -0
- package/_dist/artifact.d.ts +29 -0
- package/_dist/artifact.d.ts.map +1 -0
- package/_dist/attachment.d.ts +53 -0
- package/_dist/attachment.d.ts.map +1 -0
- package/_dist/backend.d.ts +20 -0
- package/_dist/backend.d.ts.map +1 -0
- package/_dist/broken.d.ts +24 -0
- package/_dist/broken.d.ts.map +1 -0
- package/_dist/browser.d.ts +27 -0
- package/_dist/browser.d.ts.map +1 -0
- package/_dist/compare.d.ts +4 -0
- package/_dist/compare.d.ts.map +1 -0
- package/_dist/content_type.d.ts +20 -0
- package/_dist/content_type.d.ts.map +1 -0
- package/_dist/graph.d.ts +13 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/hydrate.d.ts +36 -0
- package/_dist/hydrate.d.ts.map +1 -0
- package/_dist/identify.d.ts +7 -0
- package/_dist/identify.d.ts.map +1 -0
- package/_dist/image.d.ts +37 -0
- package/_dist/image.d.ts.map +1 -0
- package/_dist/keywords.d.ts +10 -0
- package/_dist/keywords.d.ts.map +1 -0
- package/_dist/mime.d.ts +2 -0
- package/_dist/mime.d.ts.map +1 -0
- package/_dist/mod.d.ts +97 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/object-put.d.ts +2 -0
- package/_dist/object-put.d.ts.map +1 -0
- package/_dist/object.d.ts +175 -0
- package/_dist/object.d.ts.map +1 -0
- package/_dist/parts.d.ts +91 -0
- package/_dist/parts.d.ts.map +1 -0
- package/_dist/plugin.d.ts +38 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/png.d.ts +14 -0
- package/_dist/png.d.ts.map +1 -0
- package/_dist/props.d.ts +20 -0
- package/_dist/props.d.ts.map +1 -0
- package/_dist/representation.d.ts +17 -0
- package/_dist/representation.d.ts.map +1 -0
- package/_dist/representation_rules.d.ts +3 -0
- package/_dist/representation_rules.d.ts.map +1 -0
- package/_dist/routes.d.ts +65 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/serve.d.ts +29 -0
- package/_dist/serve.d.ts.map +1 -0
- package/_dist/sha256.d.ts +26 -0
- package/_dist/sha256.d.ts.map +1 -0
- package/_dist/sqlite.d.ts +57 -0
- package/_dist/sqlite.d.ts.map +1 -0
- package/_dist/store.d.ts +41 -0
- package/_dist/store.d.ts.map +1 -0
- package/_dist/stream.d.ts +91 -0
- package/_dist/stream.d.ts.map +1 -0
- package/_dist/upload.d.ts +114 -0
- package/_dist/upload.d.ts.map +1 -0
- package/_dist/value.d.ts +62 -0
- package/_dist/value.d.ts.map +1 -0
- package/_dist/vocab.d.ts +13 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/accepts.js +76 -0
- package/accepts.js.map +1 -0
- package/accepts.ts +95 -0
- package/artifact.js +50 -0
- package/artifact.js.map +1 -0
- package/artifact.ts +93 -0
- package/attachment.js +190 -0
- package/attachment.js.map +1 -0
- package/attachment.ts +201 -0
- package/backend.js +37 -0
- package/backend.js.map +1 -0
- package/backend.ts +44 -0
- package/broken.js +278 -0
- package/broken.js.map +1 -0
- package/broken.ts +217 -0
- package/browser.js +124 -0
- package/browser.js.map +1 -0
- package/browser.ts +179 -0
- package/compare.js +82 -0
- package/compare.js.map +1 -0
- package/compare.ts +90 -0
- package/content_type.js +43 -0
- package/content_type.js.map +1 -0
- package/content_type.ts +58 -0
- package/deno.json +1 -0
- package/fixed.js +53 -0
- package/fixed.js.map +1 -0
- package/fixed.ts +61 -0
- package/graph.js +15 -0
- package/graph.js.map +1 -0
- package/graph.ts +22 -0
- package/hydrate.js +69 -0
- package/hydrate.js.map +1 -0
- package/hydrate.ts +72 -0
- package/identify.js +38 -0
- package/identify.js.map +1 -0
- package/identify.ts +50 -0
- package/image.js +111 -0
- package/image.js.map +1 -0
- package/image.ts +120 -0
- package/keywords.js +28 -0
- package/keywords.js.map +1 -0
- package/keywords.ts +28 -0
- package/meta/blob.vocab.json +17 -0
- package/mime.js +72 -0
- package/mime.js.map +1 -0
- package/mime.ts +73 -0
- package/mod.js +97 -0
- package/mod.js.map +1 -0
- package/mod.ts +115 -0
- package/object-put.js +90 -0
- package/object-put.js.map +1 -0
- package/object-put.ts +114 -0
- package/object.js +369 -0
- package/object.js.map +1 -0
- package/object.ts +592 -0
- package/package.json +84 -4
- package/parts.js +354 -0
- package/parts.js.map +1 -0
- package/parts.ts +423 -0
- package/plugin.js +158 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +184 -0
- package/png.js +74 -0
- package/png.js.map +1 -0
- package/png.ts +58 -0
- package/props.js +32 -0
- package/props.js.map +1 -0
- package/props.ts +47 -0
- package/purge.js +65 -0
- package/purge.js.map +1 -0
- package/purge.ts +72 -0
- package/representation.js +34 -0
- package/representation.js.map +1 -0
- package/representation.ts +44 -0
- package/representation_rules.js +44 -0
- package/representation_rules.js.map +1 -0
- package/representation_rules.ts +57 -0
- package/routes.js +454 -0
- package/routes.js.map +1 -0
- package/routes.ts +575 -0
- package/serve.js +196 -0
- package/serve.js.map +1 -0
- package/serve.ts +242 -0
- package/sha256.js +185 -0
- package/sha256.js.map +1 -0
- package/sha256.ts +210 -0
- package/sqlite.js +201 -0
- package/sqlite.js.map +1 -0
- package/sqlite.ts +250 -0
- package/store.js +40 -0
- package/store.js.map +1 -0
- package/store.ts +93 -0
- package/stream.js +203 -0
- package/stream.js.map +1 -0
- package/stream.ts +256 -0
- package/upload.js +256 -0
- package/upload.js.map +1 -0
- package/upload.ts +404 -0
- package/value.js +234 -0
- package/value.js.map +1 -0
- package/value.ts +281 -0
- package/vocab.js +27 -0
- package/vocab.js.map +1 -0
- package/vocab.json +123 -0
- package/vocab.ts +32 -0
package/store.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// The byte-store interface: where the bytes go. Three methods over one key —
|
|
2
|
+
// and the key is not the caller's to choose, it is the SHA-256 of the bytes
|
|
3
|
+
// themselves, which is what makes the store content-addressed: the same value
|
|
4
|
+
// written twice is one stored object, and a stored object can never be the
|
|
5
|
+
// wrong one for its key.
|
|
6
|
+
//
|
|
7
|
+
// Everything above this interface deals in bytes and hashes; nothing above it
|
|
8
|
+
// knows whether they landed in a table, a directory or a bucket. That is why a
|
|
9
|
+
// store is three functions and not a class: `sqliteBlobs`, `fileBlobs` and
|
|
10
|
+
// `objectBlobs` in this package are each a small object of this shape, and so
|
|
11
|
+
// is anything you write yourself.
|
|
12
|
+
//
|
|
13
|
+
// Every method may return a value or a promise, the same rule @yaks/graph's
|
|
14
|
+
// `Storage` follows: a table in the database you are already writing answers
|
|
15
|
+
// immediately and keeps `apply()` synchronous, while a bucket over the network
|
|
16
|
+
// answers with a promise and makes it asynchronous.
|
|
17
|
+
import { sha256 } from "@yaks/graph/sha256";
|
|
18
|
+
let utf8 = new TextEncoder();
|
|
19
|
+
let text = new TextDecoder();
|
|
20
|
+
/**
|
|
21
|
+
* The address of a string: the SHA-256 of its UTF-8 bytes, lowercase hex. This
|
|
22
|
+
* is the value a content-addressed property holds in place of its text, and the
|
|
23
|
+
* key the bytes are stored under.
|
|
24
|
+
*
|
|
25
|
+
* It reuses @yaks/graph's synchronous digest rather than `crypto.subtle`, whose
|
|
26
|
+
* promise would make every write to a body property asynchronous — including
|
|
27
|
+
* one over an embedded database that is otherwise synchronous end to end.
|
|
28
|
+
*/ export let address = (value)=>sha256(value);
|
|
29
|
+
/** A string as the bytes a store holds: its UTF-8 encoding. */ export let encode = (value)=>utf8.encode(value);
|
|
30
|
+
/** Stored bytes read back as the string they encode. */ export let decode = (bytes)=>text.decode(bytes);
|
|
31
|
+
/** A {@link Blobs} held in this process's memory, for exactly as long as it
|
|
32
|
+
* runs: where a graph in memory keeps its bytes. */ export let memoryBlobs = ()=>{
|
|
33
|
+
let held = new Map();
|
|
34
|
+
return {
|
|
35
|
+
has: (sha)=>held.has(sha),
|
|
36
|
+
get: (sha)=>held.get(sha),
|
|
37
|
+
put: (sha, bytes)=>void held.set(sha, bytes)
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=store.js.map
|
package/store.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sources":["./store.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,4EAA4E;AAC5E,8EAA8E;AAC9E,2EAA2E;AAC3E,yBAAyB;AACzB,EAAE;AACF,8EAA8E;AAC9E,+EAA+E;AAC/E,2EAA2E;AAC3E,8EAA8E;AAC9E,kCAAkC;AAClC,EAAE;AACF,4EAA4E;AAC5E,6EAA6E;AAC7E,+EAA+E;AAC/E,oDAAoD;AAEpD,SAAS,MAAM,kCAAuC;AA8CtD,IAAI,OAAO,IAAI;AACf,IAAI,OAAO,IAAI;AAEf;;;;;;;;CAQC,GACD,OAAO,IAAI,UAAU,CAAC,QAA0B,OAAO,OAAM;AAE7D,6DAA6D,GAC7D,OAAO,IAAI,SAAS,CAAC,QAA8B,KAAK,MAAM,CAAC,OAAM;AAErE,sDAAsD,GACtD,OAAO,IAAI,SAAS,CAAC,QAA8B,KAAK,MAAM,CAAC,OAAM;AAErE;kDACkD,GAClD,OAAO,IAAI,cAAc;EACvB,IAAI,OAAO,IAAI;EACf,OAAO;IACL,KAAK,CAAC,MAAQ,KAAK,GAAG,CAAC;IACvB,KAAK,CAAC,MAAQ,KAAK,GAAG,CAAC;IACvB,KAAK,CAAC,KAAK,QAAU,KAAK,KAAK,GAAG,CAAC,KAAK;EAC1C;AACF,EAAC"}
|
package/store.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// The byte-store interface: where the bytes go. Three methods over one key —
|
|
2
|
+
// and the key is not the caller's to choose, it is the SHA-256 of the bytes
|
|
3
|
+
// themselves, which is what makes the store content-addressed: the same value
|
|
4
|
+
// written twice is one stored object, and a stored object can never be the
|
|
5
|
+
// wrong one for its key.
|
|
6
|
+
//
|
|
7
|
+
// Everything above this interface deals in bytes and hashes; nothing above it
|
|
8
|
+
// knows whether they landed in a table, a directory or a bucket. That is why a
|
|
9
|
+
// store is three functions and not a class: `sqliteBlobs`, `fileBlobs` and
|
|
10
|
+
// `objectBlobs` in this package are each a small object of this shape, and so
|
|
11
|
+
// is anything you write yourself.
|
|
12
|
+
//
|
|
13
|
+
// Every method may return a value or a promise, the same rule @yaks/graph's
|
|
14
|
+
// `Storage` follows: a table in the database you are already writing answers
|
|
15
|
+
// immediately and keeps `apply()` synchronous, while a bucket over the network
|
|
16
|
+
// answers with a promise and makes it asynchronous.
|
|
17
|
+
|
|
18
|
+
import { sha256 } from '@yaks/graph/sha256'
|
|
19
|
+
import type { Opened } from './object.ts'
|
|
20
|
+
import type { Body } from './props.ts'
|
|
21
|
+
import type { Staged, Streamed } from './stream.ts'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A content-addressed byte store. `sha` is always the lowercase-hex SHA-256 of
|
|
25
|
+
* the bytes — {@link address} computes it, and an implementation may take it on
|
|
26
|
+
* trust rather than re-hashing.
|
|
27
|
+
*/
|
|
28
|
+
export type Blobs = {
|
|
29
|
+
/** Writes share the graph's transaction and must roll back with its rows.
|
|
30
|
+
* Other stores prepare their content before the graph takes its write lock. */
|
|
31
|
+
transactional?: boolean
|
|
32
|
+
/** The store holds any bytes exactly as it was given them, so keeping an
|
|
33
|
+
* object is one `put`, neither asked about first nor read back
|
|
34
|
+
* (./artifact.ts `keep`): a bucket, where each look is a round trip. A text
|
|
35
|
+
* table is not: handed a PNG, it keeps something else. */
|
|
36
|
+
exact?: boolean
|
|
37
|
+
/** whether the store already holds an object under this hash */
|
|
38
|
+
has: (sha: string) => boolean | Promise<boolean>
|
|
39
|
+
/** the bytes stored under this hash, or `undefined` if there are none */
|
|
40
|
+
get: (
|
|
41
|
+
sha: string,
|
|
42
|
+
) => Uint8Array | undefined | Promise<Uint8Array | undefined>
|
|
43
|
+
/** Stores that can stream a requested span expose it here. */
|
|
44
|
+
open?: (sha: string) => Promise<Opened | null>
|
|
45
|
+
/** store these bytes under this hash; storing the same pair twice is a
|
|
46
|
+
* no-op, because the second copy is the first one */
|
|
47
|
+
put: (sha: string, bytes: Uint8Array) => void | Promise<void>
|
|
48
|
+
/** Stores that take a stream without holding it land it here, hashed as it
|
|
49
|
+
* passes and refused past `most`, for the caller to keep at its address or
|
|
50
|
+
* drop (./stream.ts `stage`). */
|
|
51
|
+
stage?: (sent: Streamed, most?: number) => Promise<Staged>
|
|
52
|
+
/** Stream to a known address, making it visible only after `check` passes. */
|
|
53
|
+
publish?: <T>(
|
|
54
|
+
sha: string,
|
|
55
|
+
sent: Streamed,
|
|
56
|
+
check: () => Promise<T>,
|
|
57
|
+
) => Promise<T>
|
|
58
|
+
/** Let go of these objects, keeping each one a row of `held` still names:
|
|
59
|
+
* what an expired value leaves behind. A store that cannot tell which rows
|
|
60
|
+
* name an object leaves this out and keeps everything. */
|
|
61
|
+
drop?: (shas: string[], held: Body[]) => void | Promise<void>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let utf8 = new TextEncoder()
|
|
65
|
+
let text = new TextDecoder()
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The address of a string: the SHA-256 of its UTF-8 bytes, lowercase hex. This
|
|
69
|
+
* is the value a content-addressed property holds in place of its text, and the
|
|
70
|
+
* key the bytes are stored under.
|
|
71
|
+
*
|
|
72
|
+
* It reuses @yaks/graph's synchronous digest rather than `crypto.subtle`, whose
|
|
73
|
+
* promise would make every write to a body property asynchronous — including
|
|
74
|
+
* one over an embedded database that is otherwise synchronous end to end.
|
|
75
|
+
*/
|
|
76
|
+
export let address = (value: string): string => sha256(value)
|
|
77
|
+
|
|
78
|
+
/** A string as the bytes a store holds: its UTF-8 encoding. */
|
|
79
|
+
export let encode = (value: string): Uint8Array => utf8.encode(value)
|
|
80
|
+
|
|
81
|
+
/** Stored bytes read back as the string they encode. */
|
|
82
|
+
export let decode = (bytes: Uint8Array): string => text.decode(bytes)
|
|
83
|
+
|
|
84
|
+
/** A {@link Blobs} held in this process's memory, for exactly as long as it
|
|
85
|
+
* runs: where a graph in memory keeps its bytes. */
|
|
86
|
+
export let memoryBlobs = (): Blobs => {
|
|
87
|
+
let held = new Map<string, Uint8Array>()
|
|
88
|
+
return {
|
|
89
|
+
has: (sha) => held.has(sha),
|
|
90
|
+
get: (sha) => held.get(sha),
|
|
91
|
+
put: (sha, bytes) => void held.set(sha, bytes),
|
|
92
|
+
}
|
|
93
|
+
}
|
package/stream.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// Bytes as they flow. A file goes from the request that sent it to the bucket
|
|
2
|
+
// that keeps it, or from the bucket to the response that serves it, as a
|
|
3
|
+
// stream: a chunk held at a time, so a file is never bounded by the memory of
|
|
4
|
+
// the process it passes through. What has to be learned from the bytes on the
|
|
5
|
+
// way, their address, their length and their first bytes (a picture's header,
|
|
6
|
+
// a file's signature), is read off them as they pass (`hashing`).
|
|
7
|
+
//
|
|
8
|
+
// A content-addressed store names bytes by their hash, which is known only
|
|
9
|
+
// once the last byte has passed. So a stream is kept in two steps (`stage`):
|
|
10
|
+
// it lands under a staging key, hashed as it goes, and once the caller has
|
|
11
|
+
// decided to keep it, it is moved to its address. A store that holds its bytes
|
|
12
|
+
// in this process anyway (memory, a SQLite table) takes them whole.
|
|
13
|
+
import { addressOf, keep } from "./artifact.js";
|
|
14
|
+
import { hasher } from "./sha256.js";
|
|
15
|
+
/** How many of a file's first bytes are kept as it passes: enough for a
|
|
16
|
+
* picture's header and a file's signature (`sizeOf`, `contentType`). */ export let HEAD = 256 * 1024;
|
|
17
|
+
/** The most bytes held whole on their way: a stream of unknown length goes
|
|
18
|
+
* up to a bucket in pieces of this many (at least the 5 MiB R2 takes of every
|
|
19
|
+
* part but the last), and bytes no more than this are put in one trip
|
|
20
|
+
* (`stage`). */ export let PIECE = 8 * 1024 * 1024;
|
|
21
|
+
/** How many bytes `put` is, where that is known before it is read. */ export let lengthOf = (put)=>put instanceof Uint8Array ? put.byteLength : put.size;
|
|
22
|
+
/** Bytes as a {@link Streamed} stream. */ export let streamOf = (bytes)=>({
|
|
23
|
+
stream: new Blob([
|
|
24
|
+
bytes
|
|
25
|
+
]).stream(),
|
|
26
|
+
size: bytes.byteLength
|
|
27
|
+
});
|
|
28
|
+
/** Pull one chunk at a time from a parser or another async byte source. */ export let fromChunks = (chunks)=>{
|
|
29
|
+
let next = chunks[Symbol.asyncIterator]();
|
|
30
|
+
return new ReadableStream({
|
|
31
|
+
pull: async (out)=>{
|
|
32
|
+
let part = await next.next();
|
|
33
|
+
if (part.done) out.close();
|
|
34
|
+
else out.enqueue(part.value);
|
|
35
|
+
},
|
|
36
|
+
cancel: async ()=>void await next.return?.()
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
/** Bytes held whole: for a store that keeps them in this process anyway. */ export let gathered = async (put)=>put instanceof Uint8Array ? new Uint8Array(put) : new Uint8Array(await new Response(put.stream).arrayBuffer());
|
|
40
|
+
/** A stream that ran past the most bytes it may be. */ export class TooMany extends Error {
|
|
41
|
+
name = 'TooMany';
|
|
42
|
+
most;
|
|
43
|
+
constructor(most){
|
|
44
|
+
super(`more than ${most} bytes`);
|
|
45
|
+
this.most = most;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A pass-through that hashes bytes as they go by, counts them and keeps their
|
|
50
|
+
* first {@link HEAD}. `seen` answers once the last byte has passed. A stream
|
|
51
|
+
* running past `size` or `most` ({@link TooMany}), or ending short of `size`,
|
|
52
|
+
* fails.
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { equal } from '@yaks/testing'
|
|
56
|
+
* import { address, hashing } from '@yaks/blob'
|
|
57
|
+
*
|
|
58
|
+
* let { through, seen } = hashing(5)
|
|
59
|
+
* let out = await new Response(new Blob(['hello']).stream().pipeThrough(through)).text()
|
|
60
|
+
* equal([out, (await seen).sha], ['hello', address('hello')])
|
|
61
|
+
* ```
|
|
62
|
+
*/ export let hashing = (size, most = Infinity)=>{
|
|
63
|
+
let hash = hasher();
|
|
64
|
+
let head = new Uint8Array(Math.min(HEAD, size ?? HEAD, most));
|
|
65
|
+
let at = 0;
|
|
66
|
+
let resolve, reject;
|
|
67
|
+
let seen = new Promise((yes, no)=>(resolve = yes, reject = no));
|
|
68
|
+
seen.catch(()=>{});
|
|
69
|
+
let failed = (error)=>(reject(error), error);
|
|
70
|
+
let through = new TransformStream({
|
|
71
|
+
transform: (chunk, controller)=>{
|
|
72
|
+
let next = at + chunk.byteLength;
|
|
73
|
+
if (next > most) throw failed(new TooMany(most));
|
|
74
|
+
if (size != null && next > size) {
|
|
75
|
+
throw failed(new Error(`more bytes arrived than the ${size} said`));
|
|
76
|
+
}
|
|
77
|
+
hash.add(chunk);
|
|
78
|
+
if (at < head.length) head.set(chunk.subarray(0, head.length - at), at);
|
|
79
|
+
at = next;
|
|
80
|
+
controller.enqueue(chunk);
|
|
81
|
+
},
|
|
82
|
+
flush: ()=>{
|
|
83
|
+
if (size != null && at != size) {
|
|
84
|
+
throw failed(new Error(`${at} bytes arrived, not the ${size} said`));
|
|
85
|
+
}
|
|
86
|
+
resolve({
|
|
87
|
+
sha: hash.end(at),
|
|
88
|
+
size: at,
|
|
89
|
+
head: head.slice(0, at)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return {
|
|
94
|
+
through,
|
|
95
|
+
seen
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Bytes staged in `store`, for the caller to keep at their address or drop,
|
|
100
|
+
* refused past `most` ({@link TooMany}). Bytes no more than {@link PIECE} are
|
|
101
|
+
* held and put in one trip. A larger stream goes to a store that stages
|
|
102
|
+
* streams (a bucket) without being held; any other store holds bytes in this
|
|
103
|
+
* process, and is given them whole.
|
|
104
|
+
*
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { equal } from '@yaks/testing'
|
|
107
|
+
* import { address, encode, memoryBlobs, stage, streamOf } from '@yaks/blob'
|
|
108
|
+
*
|
|
109
|
+
* let store = memoryBlobs()
|
|
110
|
+
* let staged = await stage(store, streamOf(encode('hello')))
|
|
111
|
+
* await staged.keep()
|
|
112
|
+
* await staged.drop()
|
|
113
|
+
* equal(store.has(address('hello')), true)
|
|
114
|
+
* ```
|
|
115
|
+
*/ export let stage = async (store, put, most = Infinity)=>{
|
|
116
|
+
let held = await holding(put, Math.min(PIECE, most));
|
|
117
|
+
if (!(held instanceof Uint8Array) && store.stage) {
|
|
118
|
+
return store.stage(held, most);
|
|
119
|
+
}
|
|
120
|
+
let bytes = held instanceof Uint8Array ? held : await gathered({
|
|
121
|
+
stream: held.stream.pipeThrough(hashing(held.size, most).through)
|
|
122
|
+
});
|
|
123
|
+
if (bytes.byteLength > most) throw new TooMany(most);
|
|
124
|
+
let sha = await addressOf(bytes);
|
|
125
|
+
return {
|
|
126
|
+
sha,
|
|
127
|
+
size: bytes.byteLength,
|
|
128
|
+
head: new Uint8Array(bytes.subarray(0, HEAD)),
|
|
129
|
+
read: ()=>Promise.resolve(streamOf(bytes).stream),
|
|
130
|
+
keep: ()=>keep(store, sha, bytes),
|
|
131
|
+
drop: ()=>Promise.resolve()
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Bytes held whole where they are at most `most`, read off the stream to find
|
|
136
|
+
* out; otherwise a stream that has lost nothing, the bytes read so far ahead
|
|
137
|
+
* of the rest. A caller that does better with small files whole (several put
|
|
138
|
+
* at once) and must not hold a large one asks this first.
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* import { equal } from '@yaks/testing'
|
|
142
|
+
* import { encode, gathered, holding, streamOf } from '@yaks/blob'
|
|
143
|
+
*
|
|
144
|
+
* let small = await holding(streamOf(encode('small')), 8)
|
|
145
|
+
* let large = await holding(streamOf(encode('larger than eight')), 8)
|
|
146
|
+
* equal(small instanceof Uint8Array, true)
|
|
147
|
+
* equal(large instanceof Uint8Array, false)
|
|
148
|
+
* equal(await gathered(large), encode('larger than eight'))
|
|
149
|
+
* ```
|
|
150
|
+
*/ export let holding = async (put, most)=>{
|
|
151
|
+
if (put instanceof Uint8Array) return new Uint8Array(put);
|
|
152
|
+
if (put.size != null && put.size > most) return put;
|
|
153
|
+
let reader = put.stream.getReader();
|
|
154
|
+
let read = [], held = 0;
|
|
155
|
+
while(held <= most){
|
|
156
|
+
let got = await reader.read();
|
|
157
|
+
if (got.done) {
|
|
158
|
+
let bytes = new Uint8Array(held), at = 0;
|
|
159
|
+
for (let chunk of read)bytes.set(chunk, at), at += chunk.byteLength;
|
|
160
|
+
return bytes;
|
|
161
|
+
}
|
|
162
|
+
read.push(got.value);
|
|
163
|
+
held += got.value.byteLength;
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
size: put.size,
|
|
167
|
+
stream: new ReadableStream({
|
|
168
|
+
pull: async (c)=>{
|
|
169
|
+
let ahead = read.shift();
|
|
170
|
+
if (ahead) return c.enqueue(ahead);
|
|
171
|
+
let got = await reader.read();
|
|
172
|
+
if (got.done) c.close();
|
|
173
|
+
else c.enqueue(got.value);
|
|
174
|
+
},
|
|
175
|
+
cancel: (why)=>reader.cancel(why)
|
|
176
|
+
})
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
/** Bytes kept at their address in `store`, streamed where they are a stream:
|
|
180
|
+
* {@link stage}, keep and drop, for a caller with nothing to decide between. */ export let stored = async (store, put, most = Infinity)=>{
|
|
181
|
+
let { keep, drop, sha, size, head } = await stage(store, put, most);
|
|
182
|
+
try {
|
|
183
|
+
await keep();
|
|
184
|
+
} finally{
|
|
185
|
+
await drop();
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
sha,
|
|
189
|
+
size,
|
|
190
|
+
head
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
/** A request's or a response's body as it arrives, with the length it
|
|
194
|
+
* declares, if any. */ export let bodyOf = (message)=>{
|
|
195
|
+
let declared = Number(message.headers.get('content-length') ?? NaN);
|
|
196
|
+
return {
|
|
197
|
+
stream: message.body ?? new Response('').body,
|
|
198
|
+
...Number.isSafeInteger(declared) && declared >= 0 ? {
|
|
199
|
+
size: declared
|
|
200
|
+
} : {}
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
//# sourceMappingURL=stream.js.map
|
package/stream.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sources":["./stream.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,yEAAyE;AACzE,8EAA8E;AAC9E,8EAA8E;AAC9E,8EAA8E;AAC9E,kEAAkE;AAClE,EAAE;AACF,2EAA2E;AAC3E,6EAA6E;AAC7E,2EAA2E;AAC3E,+EAA+E;AAC/E,oEAAoE;AAEpE,SAAS,SAAS,EAAE,IAAI,wBAAuB;AAC/C,SAAS,MAAM,sBAAqB;AAUpC;sEACsE,GACtE,OAAO,IAAI,OAAO,MAAM,KAAI;AAE5B;;;cAGc,GACd,OAAO,IAAI,QAAQ,IAAI,OAAO,KAAI;AAElC,oEAAoE,GACpE,OAAO,IAAI,WAAW,CAAC,MACrB,eAAe,aAAa,IAAI,UAAU,GAAG,IAAI,IAAI,CAAA;AAEvD,wCAAwC,GACxC,OAAO,IAAI,WAAW,CAAC,QAAgC,CAAC;IACtD,QAAQ,IAAI,KAAK;MAAC;KAAiC,EAAE,MAAM;IAC3D,MAAM,MAAM,UAAU;EACxB,CAAC,EAAC;AAEF,yEAAyE,GACzE,OAAO,IAAI,aAAa,CACtB;EAEA,IAAI,OAAO,MAAM,CAAC,OAAO,aAAa,CAAC;EACvC,OAAO,IAAI,eAA2B;IACpC,MAAM,OAAO;MACX,IAAI,OAAO,MAAM,KAAK,IAAI;MAC1B,IAAI,KAAK,IAAI,EAAE,IAAI,KAAK;WACnB,IAAI,OAAO,CAAC,KAAK,KAAK;IAC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from the request that sent it to the bucket
|
|
2
|
+
// that keeps it, or from the bucket to the response that serves it, as a
|
|
3
|
+
// stream: a chunk held at a time, so a file is never bounded by the memory of
|
|
4
|
+
// the process it passes through. What has to be learned from the bytes on the
|
|
5
|
+
// way, their address, their length and their first bytes (a picture's header,
|
|
6
|
+
// a file's signature), is read off them as they pass (`hashing`).
|
|
7
|
+
//
|
|
8
|
+
// A content-addressed store names bytes by their hash, which is known only
|
|
9
|
+
// once the last byte has passed. So a stream is kept in two steps (`stage`):
|
|
10
|
+
// it lands under a staging key, hashed as it goes, and once the caller has
|
|
11
|
+
// decided to keep it, it is moved to its address. A store that holds its bytes
|
|
12
|
+
// in this process anyway (memory, a SQLite table) takes them whole.
|
|
13
|
+
|
|
14
|
+
import { addressOf, keep } from './artifact.js'
|
|
15
|
+
import { hasher } from './sha256.js'
|
|
16
|
+
import type { Blobs } from './store.ts'
|
|
17
|
+
|
|
18
|
+
/** Bytes sent as a stream, with how many they are where that is known: a
|
|
19
|
+
* form's part or a zip's entry often does not say. */
|
|
20
|
+
export type Streamed = { stream: ReadableStream<Uint8Array>; size?: number }
|
|
21
|
+
|
|
22
|
+
/** Bytes to put: held whole, or a {@link Streamed} stream. */
|
|
23
|
+
export type Put = Uint8Array | Streamed
|
|
24
|
+
|
|
25
|
+
/** How many of a file's first bytes are kept as it passes: enough for a
|
|
26
|
+
* picture's header and a file's signature (`sizeOf`, `contentType`). */
|
|
27
|
+
export let HEAD = 256 * 1024
|
|
28
|
+
|
|
29
|
+
/** The most bytes held whole on their way: a stream of unknown length goes
|
|
30
|
+
* up to a bucket in pieces of this many (at least the 5 MiB R2 takes of every
|
|
31
|
+
* part but the last), and bytes no more than this are put in one trip
|
|
32
|
+
* (`stage`). */
|
|
33
|
+
export let PIECE = 8 * 1024 * 1024
|
|
34
|
+
|
|
35
|
+
/** How many bytes `put` is, where that is known before it is read. */
|
|
36
|
+
export let lengthOf = (put: Put): number | undefined =>
|
|
37
|
+
put instanceof Uint8Array ? put.byteLength : put.size
|
|
38
|
+
|
|
39
|
+
/** Bytes as a {@link Streamed} stream. */
|
|
40
|
+
export let streamOf = (bytes: Uint8Array): Streamed => ({
|
|
41
|
+
stream: new Blob([bytes as Uint8Array<ArrayBuffer>]).stream(),
|
|
42
|
+
size: bytes.byteLength,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
/** Pull one chunk at a time from a parser or another async byte source. */
|
|
46
|
+
export let fromChunks = (
|
|
47
|
+
chunks: AsyncIterable<Uint8Array>,
|
|
48
|
+
): ReadableStream<Uint8Array> => {
|
|
49
|
+
let next = chunks[Symbol.asyncIterator]()
|
|
50
|
+
return new ReadableStream<Uint8Array>({
|
|
51
|
+
pull: async (out) => {
|
|
52
|
+
let part = await next.next()
|
|
53
|
+
if (part.done) out.close()
|
|
54
|
+
else out.enqueue(part.value)
|
|
55
|
+
},
|
|
56
|
+
cancel: async () => void await next.return?.(),
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Bytes held whole: for a store that keeps them in this process anyway. */
|
|
61
|
+
export let gathered = async (put: Put): Promise<Uint8Array<ArrayBuffer>> =>
|
|
62
|
+
put instanceof Uint8Array
|
|
63
|
+
? new Uint8Array(put)
|
|
64
|
+
: new Uint8Array(await new Response(put.stream).arrayBuffer())
|
|
65
|
+
|
|
66
|
+
/** A stream that ran past the most bytes it may be. */
|
|
67
|
+
export class TooMany extends Error {
|
|
68
|
+
override name = 'TooMany'
|
|
69
|
+
most: number
|
|
70
|
+
constructor(most: number) {
|
|
71
|
+
super(`more than ${most} bytes`)
|
|
72
|
+
this.most = most
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** What passing bytes said: their address, length and first {@link HEAD}. */
|
|
77
|
+
export type Seen = { sha: string; size: number; head: Uint8Array<ArrayBuffer> }
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A pass-through that hashes bytes as they go by, counts them and keeps their
|
|
81
|
+
* first {@link HEAD}. `seen` answers once the last byte has passed. A stream
|
|
82
|
+
* running past `size` or `most` ({@link TooMany}), or ending short of `size`,
|
|
83
|
+
* fails.
|
|
84
|
+
*
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { equal } from '@yaks/testing'
|
|
87
|
+
* import { address, hashing } from '@yaks/blob'
|
|
88
|
+
*
|
|
89
|
+
* let { through, seen } = hashing(5)
|
|
90
|
+
* let out = await new Response(new Blob(['hello']).stream().pipeThrough(through)).text()
|
|
91
|
+
* equal([out, (await seen).sha], ['hello', address('hello')])
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export let hashing = (
|
|
95
|
+
size?: number,
|
|
96
|
+
most = Infinity,
|
|
97
|
+
): {
|
|
98
|
+
through: TransformStream<Uint8Array, Uint8Array>
|
|
99
|
+
seen: Promise<Seen>
|
|
100
|
+
} => {
|
|
101
|
+
let hash = hasher()
|
|
102
|
+
let head = new Uint8Array(Math.min(HEAD, size ?? HEAD, most))
|
|
103
|
+
let at = 0
|
|
104
|
+
let resolve!: (seen: Seen) => void, reject!: (error: Error) => void
|
|
105
|
+
let seen = new Promise<Seen>((yes, no) => ((resolve = yes), (reject = no)))
|
|
106
|
+
seen.catch(() => {})
|
|
107
|
+
let failed = (error: Error) => (reject(error), error)
|
|
108
|
+
let through = new TransformStream<Uint8Array, Uint8Array>({
|
|
109
|
+
transform: (chunk, controller) => {
|
|
110
|
+
let next = at + chunk.byteLength
|
|
111
|
+
if (next > most) throw failed(new TooMany(most))
|
|
112
|
+
if (size != null && next > size) {
|
|
113
|
+
throw failed(new Error(`more bytes arrived than the ${size} said`))
|
|
114
|
+
}
|
|
115
|
+
hash.add(chunk)
|
|
116
|
+
if (at < head.length) head.set(chunk.subarray(0, head.length - at), at)
|
|
117
|
+
at = next
|
|
118
|
+
controller.enqueue(chunk)
|
|
119
|
+
},
|
|
120
|
+
flush: () => {
|
|
121
|
+
if (size != null && at != size) {
|
|
122
|
+
throw failed(new Error(`${at} bytes arrived, not the ${size} said`))
|
|
123
|
+
}
|
|
124
|
+
resolve({ sha: hash.end(at), size: at, head: head.slice(0, at) })
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
return { through, seen }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Bytes landed but not yet kept. `keep` puts them at their address; `drop`
|
|
131
|
+
* lets the staged copy go, kept or not, and is always called. */
|
|
132
|
+
export type Staged = Seen & {
|
|
133
|
+
read: () => Promise<ReadableStream<Uint8Array>>
|
|
134
|
+
keep: () => Promise<void>
|
|
135
|
+
drop: () => Promise<void>
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Bytes staged in `store`, for the caller to keep at their address or drop,
|
|
140
|
+
* refused past `most` ({@link TooMany}). Bytes no more than {@link PIECE} are
|
|
141
|
+
* held and put in one trip. A larger stream goes to a store that stages
|
|
142
|
+
* streams (a bucket) without being held; any other store holds bytes in this
|
|
143
|
+
* process, and is given them whole.
|
|
144
|
+
*
|
|
145
|
+
* ```ts
|
|
146
|
+
* import { equal } from '@yaks/testing'
|
|
147
|
+
* import { address, encode, memoryBlobs, stage, streamOf } from '@yaks/blob'
|
|
148
|
+
*
|
|
149
|
+
* let store = memoryBlobs()
|
|
150
|
+
* let staged = await stage(store, streamOf(encode('hello')))
|
|
151
|
+
* await staged.keep()
|
|
152
|
+
* await staged.drop()
|
|
153
|
+
* equal(store.has(address('hello')), true)
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export let stage = async (
|
|
157
|
+
store: Blobs,
|
|
158
|
+
put: Put,
|
|
159
|
+
most = Infinity,
|
|
160
|
+
): Promise<Staged> => {
|
|
161
|
+
let held = await holding(put, Math.min(PIECE, most))
|
|
162
|
+
if (!(held instanceof Uint8Array) && store.stage) {
|
|
163
|
+
return store.stage(held, most)
|
|
164
|
+
}
|
|
165
|
+
let bytes = held instanceof Uint8Array ? held : await gathered({
|
|
166
|
+
stream: held.stream.pipeThrough(hashing(held.size, most).through),
|
|
167
|
+
})
|
|
168
|
+
if (bytes.byteLength > most) throw new TooMany(most)
|
|
169
|
+
let sha = await addressOf(bytes)
|
|
170
|
+
return {
|
|
171
|
+
sha,
|
|
172
|
+
size: bytes.byteLength,
|
|
173
|
+
head: new Uint8Array(bytes.subarray(0, HEAD)),
|
|
174
|
+
read: () => Promise.resolve(streamOf(bytes).stream),
|
|
175
|
+
keep: () => keep(store, sha, bytes),
|
|
176
|
+
drop: () => Promise.resolve(),
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Bytes held whole where they are at most `most`, read off the stream to find
|
|
182
|
+
* out; otherwise a stream that has lost nothing, the bytes read so far ahead
|
|
183
|
+
* of the rest. A caller that does better with small files whole (several put
|
|
184
|
+
* at once) and must not hold a large one asks this first.
|
|
185
|
+
*
|
|
186
|
+
* ```ts
|
|
187
|
+
* import { equal } from '@yaks/testing'
|
|
188
|
+
* import { encode, gathered, holding, streamOf } from '@yaks/blob'
|
|
189
|
+
*
|
|
190
|
+
* let small = await holding(streamOf(encode('small')), 8)
|
|
191
|
+
* let large = await holding(streamOf(encode('larger than eight')), 8)
|
|
192
|
+
* equal(small instanceof Uint8Array, true)
|
|
193
|
+
* equal(large instanceof Uint8Array, false)
|
|
194
|
+
* equal(await gathered(large), encode('larger than eight'))
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
export let holding = async (
|
|
198
|
+
put: Put,
|
|
199
|
+
most: number,
|
|
200
|
+
): Promise<Uint8Array<ArrayBuffer> | Streamed> => {
|
|
201
|
+
if (put instanceof Uint8Array) return new Uint8Array(put)
|
|
202
|
+
if (put.size != null && put.size > most) return put
|
|
203
|
+
let reader = put.stream.getReader()
|
|
204
|
+
let read: Uint8Array[] = [], held = 0
|
|
205
|
+
while (held <= most) {
|
|
206
|
+
let got = await reader.read()
|
|
207
|
+
if (got.done) {
|
|
208
|
+
let bytes = new Uint8Array(held), at = 0
|
|
209
|
+
for (let chunk of read) bytes.set(chunk, at), at += chunk.byteLength
|
|
210
|
+
return bytes
|
|
211
|
+
}
|
|
212
|
+
read.push(got.value)
|
|
213
|
+
held += got.value.byteLength
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
size: put.size,
|
|
217
|
+
stream: new ReadableStream<Uint8Array>({
|
|
218
|
+
pull: async (c) => {
|
|
219
|
+
let ahead = read.shift()
|
|
220
|
+
if (ahead) return c.enqueue(ahead)
|
|
221
|
+
let got = await reader.read()
|
|
222
|
+
if (got.done) c.close()
|
|
223
|
+
else c.enqueue(got.value)
|
|
224
|
+
},
|
|
225
|
+
cancel: (why) => reader.cancel(why),
|
|
226
|
+
}),
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Bytes kept at their address in `store`, streamed where they are a stream:
|
|
231
|
+
* {@link stage}, keep and drop, for a caller with nothing to decide between. */
|
|
232
|
+
export let stored = async (
|
|
233
|
+
store: Blobs,
|
|
234
|
+
put: Put,
|
|
235
|
+
most = Infinity,
|
|
236
|
+
): Promise<Seen> => {
|
|
237
|
+
let { keep, drop, sha, size, head } = await stage(store, put, most)
|
|
238
|
+
try {
|
|
239
|
+
await keep()
|
|
240
|
+
} finally {
|
|
241
|
+
await drop()
|
|
242
|
+
}
|
|
243
|
+
return { sha, size, head }
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** A request's or a response's body as it arrives, with the length it
|
|
247
|
+
* declares, if any. */
|
|
248
|
+
export let bodyOf = (message: Request | Response): Streamed => {
|
|
249
|
+
let declared = Number(message.headers.get('content-length') ?? NaN)
|
|
250
|
+
return {
|
|
251
|
+
stream: message.body ?? new Response('').body!,
|
|
252
|
+
...Number.isSafeInteger(declared) && declared >= 0
|
|
253
|
+
? { size: declared }
|
|
254
|
+
: {},
|
|
255
|
+
}
|
|
256
|
+
}
|