@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sources":["../store.ts"],"names":[],"mappings":"AAkBA,cAAc,MAAM,sBAAqB;AACzC,cAAc,IAAI,qBAAoB;AACtC,cAAc,MAAM,EAAE,QAAQ,sBAAqB;AAEnD;;;;CAIC,GACD,YAAY;EACV;+EAC6E,GAC7E,gBAAgB,OAAO;EACvB;;;0DAGwD,GACxD,QAAQ,OAAO;EACf,8DAA8D,GAC9D,MAAM,KAAK,MAAM,KAAK,OAAO,GAAG,QAAQ,OAAO;EAC/C,uEAAuE,GACvE,MACE,KAAK,MAAM,KACR,aAAa,SAAS,GAAG,QAAQ,aAAa,SAAS;EAC5D,4DAA4D,GAC5D,QAAQ,KAAK,MAAM,KAAK,QAAQ,SAAS,IAAI;EAC7C;qDACmD,GACnD,MAAM,KAAK,MAAM,EAAE,OAAO,eAAe,IAAI,GAAG,QAAQ,IAAI;EAC5D;;iCAE+B,GAC/B,SAAS,MAAM,UAAU,OAAO,MAAM,KAAK,QAAQ;EACnD,4EAA4E,GAC5E,WAAW,GACT,KAAK,MAAM,EACX,MAAM,UACN,aAAa,QAAQ,OAClB,QAAQ;EACb;;0DAEwD,GACxD,QAAQ,MAAM,MAAM,IAAI,MAAM,WAAW,IAAI,GAAG,QAAQ,IAAI;;AAM9D;;;;;;;;CAQC,GACD,OAAO,YAAI,UAAW,OAAO,MAAM,KAAG,MAAM,CAAiB;AAE7D,6DAA6D,GAC7D,OAAO,YAAI,SAAU,OAAO,MAAM,KAAG,WAAgC;AAErE,sDAAsD,GACtD,OAAO,YAAI,SAAU,OAAO,eAAa,MAAM,CAAsB;AAErE;kDACkD,GAClD,OAAO,YAAI,mBAAkB,MAO5B"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { Blobs } from "./store.js";
|
|
2
|
+
/** Bytes sent as a stream, with how many they are where that is known: a
|
|
3
|
+
* form's part or a zip's entry often does not say. */ export type Streamed = {
|
|
4
|
+
stream: ReadableStream<Uint8Array>;
|
|
5
|
+
size?: number;
|
|
6
|
+
};
|
|
7
|
+
/** Bytes to put: held whole, or a {@link Streamed} stream. */ export type Put = Uint8Array | Streamed;
|
|
8
|
+
/** How many of a file's first bytes are kept as it passes: enough for a
|
|
9
|
+
* picture's header and a file's signature (`sizeOf`, `contentType`). */ export declare let HEAD: any;
|
|
10
|
+
/** The most bytes held whole on their way: a stream of unknown length goes
|
|
11
|
+
* up to a bucket in pieces of this many (at least the 5 MiB R2 takes of every
|
|
12
|
+
* part but the last), and bytes no more than this are put in one trip
|
|
13
|
+
* (`stage`). */ export declare let PIECE: any;
|
|
14
|
+
/** How many bytes `put` is, where that is known before it is read. */ export declare let lengthOf: (put: Put) => number | undefined;
|
|
15
|
+
/** Bytes as a {@link Streamed} stream. */ export declare let streamOf: (bytes: Uint8Array) => Streamed;
|
|
16
|
+
/** Pull one chunk at a time from a parser or another async byte source. */ export declare let fromChunks: (chunks: AsyncIterable<Uint8Array>) => ReadableStream<Uint8Array>;
|
|
17
|
+
/** Bytes held whole: for a store that keeps them in this process anyway. */ export declare let gathered: (put: Put) => Promise<Uint8Array<ArrayBuffer>>;
|
|
18
|
+
/** A stream that ran past the most bytes it may be. */ export declare class TooMany extends Error {
|
|
19
|
+
name: string;
|
|
20
|
+
most: number;
|
|
21
|
+
constructor(most: number);
|
|
22
|
+
}
|
|
23
|
+
/** What passing bytes said: their address, length and first {@link HEAD}. */ export type Seen = {
|
|
24
|
+
sha: string;
|
|
25
|
+
size: number;
|
|
26
|
+
head: Uint8Array<ArrayBuffer>;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* A pass-through that hashes bytes as they go by, counts them and keeps their
|
|
30
|
+
* first {@link HEAD}. `seen` answers once the last byte has passed. A stream
|
|
31
|
+
* running past `size` or `most` ({@link TooMany}), or ending short of `size`,
|
|
32
|
+
* fails.
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { equal } from '@yaks/testing'
|
|
36
|
+
* import { address, hashing } from '@yaks/blob'
|
|
37
|
+
*
|
|
38
|
+
* let { through, seen } = hashing(5)
|
|
39
|
+
* let out = await new Response(new Blob(['hello']).stream().pipeThrough(through)).text()
|
|
40
|
+
* equal([out, (await seen).sha], ['hello', address('hello')])
|
|
41
|
+
* ```
|
|
42
|
+
*/ export declare let hashing: (size?: number, most: number) => {
|
|
43
|
+
through: TransformStream<Uint8Array, Uint8Array>;
|
|
44
|
+
seen: Promise<Seen>;
|
|
45
|
+
};
|
|
46
|
+
/** Bytes landed but not yet kept. `keep` puts them at their address; `drop`
|
|
47
|
+
* lets the staged copy go, kept or not, and is always called. */ export type Staged = Seen & {
|
|
48
|
+
read: () => Promise<ReadableStream<Uint8Array>>;
|
|
49
|
+
keep: () => Promise<void>;
|
|
50
|
+
drop: () => Promise<void>;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Bytes staged in `store`, for the caller to keep at their address or drop,
|
|
54
|
+
* refused past `most` ({@link TooMany}). Bytes no more than {@link PIECE} are
|
|
55
|
+
* held and put in one trip. A larger stream goes to a store that stages
|
|
56
|
+
* streams (a bucket) without being held; any other store holds bytes in this
|
|
57
|
+
* process, and is given them whole.
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { equal } from '@yaks/testing'
|
|
61
|
+
* import { address, encode, memoryBlobs, stage, streamOf } from '@yaks/blob'
|
|
62
|
+
*
|
|
63
|
+
* let store = memoryBlobs()
|
|
64
|
+
* let staged = await stage(store, streamOf(encode('hello')))
|
|
65
|
+
* await staged.keep()
|
|
66
|
+
* await staged.drop()
|
|
67
|
+
* equal(store.has(address('hello')), true)
|
|
68
|
+
* ```
|
|
69
|
+
*/ export declare let stage: (store: Blobs, put: Put, most: number) => Promise<Staged>;
|
|
70
|
+
/**
|
|
71
|
+
* Bytes held whole where they are at most `most`, read off the stream to find
|
|
72
|
+
* out; otherwise a stream that has lost nothing, the bytes read so far ahead
|
|
73
|
+
* of the rest. A caller that does better with small files whole (several put
|
|
74
|
+
* at once) and must not hold a large one asks this first.
|
|
75
|
+
*
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { equal } from '@yaks/testing'
|
|
78
|
+
* import { encode, gathered, holding, streamOf } from '@yaks/blob'
|
|
79
|
+
*
|
|
80
|
+
* let small = await holding(streamOf(encode('small')), 8)
|
|
81
|
+
* let large = await holding(streamOf(encode('larger than eight')), 8)
|
|
82
|
+
* equal(small instanceof Uint8Array, true)
|
|
83
|
+
* equal(large instanceof Uint8Array, false)
|
|
84
|
+
* equal(await gathered(large), encode('larger than eight'))
|
|
85
|
+
* ```
|
|
86
|
+
*/ export declare let holding: (put: Put, most: number) => Promise<Uint8Array<ArrayBuffer> | Streamed>;
|
|
87
|
+
/** Bytes kept at their address in `store`, streamed where they are a stream:
|
|
88
|
+
* {@link stage}, keep and drop, for a caller with nothing to decide between. */ export declare let stored: (store: Blobs, put: Put, most: number) => Promise<Seen>;
|
|
89
|
+
/** A request's or a response's body as it arrives, with the length it
|
|
90
|
+
* declares, if any. */ export declare let bodyOf: (message: Request | Response) => Streamed;
|
|
91
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sources":["../stream.ts"],"names":[],"mappings":"AAeA,cAAc,KAAK,qBAAoB;AAEvC;oDACoD,GACpD,YAAY;EAAa,QAAQ,eAAe;EAAa,OAAO,MAAM;;AAE1E,4DAA4D,GAC5D,YAAY,MAAM,aAAa;AAE/B;sEACsE,GACtE,OAAO,YAAI,UAAiB;AAE5B;;;cAGc,GACd,OAAO,YAAI,WAAuB;AAElC,oEAAoE,GACpE,OAAO,YAAI,WAAY,KAAK,QAAM,MAAM,GAAG,SAAS,CACG;AAEvD,wCAAwC,GACxC,OAAO,YAAI,WAAY,OAAO,eAAa,SAGzC;AAEF,yEAAyE,GACzE,OAAO,YAAI,aACT,QAAQ,cAAc,gBACrB,eAAe,YAUjB;AAED,0EAA0E,GAC1E,OAAO,YAAI,WAAkB,KAAK,QAAM,QAAQ,WAAW,cAGO;AAElE,qDAAqD,GACrD,OAAO,cAAM,gBAAgB;EAClB,aAAgB;EACzB,MAAM,MAAM,CAAA;EACZ,YAAY,MAAM,MAAM;AAI1B;AAEA,2EAA2E,GAC3E,YAAY;EAAS,KAAK,MAAM;EAAE,MAAM,MAAM;EAAE,MAAM,WAAW;;AAEjE;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,UACT,OAAO,MAAM,EACb;EAEA,SAAS,gBAAgB,YAAY;EACrC,MAAM,QAAQ;EA6Bf;AAED;+DAC+D,GAC/D,YAAY,SAAS;EACnB,YAAY,QAAQ,eAAe;EACnC,YAAY,QAAQ,IAAI;EACxB,YAAY,QAAQ,IAAI;;AAG1B;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,YAAI,QACT,OAAO,OACP,KAAK,KACL,iBACC,QAAQ,QAkBV;AAED;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,YAAI,UACT,KAAK,KACL,MAAM,MAAM,KACX,QAAQ,WAAW,eAAe,UA4BpC;AAED;8EAC8E,GAC9E,OAAO,YAAI,SACT,OAAO,OACP,KAAK,KACL,iBACC,QAAQ,MAQV;AAED;qBACqB,GACrB,OAAO,YAAI,SAAU,SAAS,UAAU,aAAW,SAQlD"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { type Built } from "@yaks/query";
|
|
2
|
+
import type { Bundle, Reads } from "@yaks/graph";
|
|
3
|
+
import { type Accepts } from "./accepts.js";
|
|
4
|
+
import { type Size } from "./image.js";
|
|
5
|
+
import { type Representation } from "./representation.js";
|
|
6
|
+
import type { Blobs } from "./store.js";
|
|
7
|
+
import { type Put, type Seen, type Staged } from "./stream.js";
|
|
8
|
+
/** The scope of a representation where a store names none. */ export declare let SCOPE: string;
|
|
9
|
+
/** The eid of the use of the bytes at `sha`: the row saying what they are
|
|
10
|
+
* called and what they are. */ export declare let useOf: (sha: string) => Promise<string>;
|
|
11
|
+
/** Kept bytes, as the rows that name them are made from: their address, the
|
|
12
|
+
* eid of their use, their media type and name, how many there are, and what
|
|
13
|
+
* a picture measures. */ export type Kept = {
|
|
14
|
+
sha: string;
|
|
15
|
+
use: string;
|
|
16
|
+
mime: string;
|
|
17
|
+
name: string;
|
|
18
|
+
bytes: number;
|
|
19
|
+
size?: Size;
|
|
20
|
+
};
|
|
21
|
+
/** What a store's rows say the bytes at `sha` are: their media type and
|
|
22
|
+
* name, and the patch that corrects the type once it is read off them. */ export type Said = {
|
|
23
|
+
mime: string;
|
|
24
|
+
name: string;
|
|
25
|
+
fix: (mime: string) => Bundle[];
|
|
26
|
+
};
|
|
27
|
+
/** How a store's words name kept bytes. */ export type Naming = {
|
|
28
|
+
/** The content row and the use. The use is a patch, so an arrival that
|
|
29
|
+
* names nothing keeps the name the first one gave. */ rows: (kept: Kept) => Bundle[];
|
|
30
|
+
/** What rows read at the use and the content say of the bytes, or null
|
|
31
|
+
* where none names them. */ said: (rows: Bundle[], at: {
|
|
32
|
+
sha: string;
|
|
33
|
+
use: string;
|
|
34
|
+
}) => Said | null;
|
|
35
|
+
/** Find a named use when no canonical use exists at the content address. */ current?: (read: Read, sha: string) => Promise<Said | null>;
|
|
36
|
+
/** The query for the rows still naming the bytes at `sha`. While one
|
|
37
|
+
* answers, the bytes are served. */ names: (sha: string) => Built;
|
|
38
|
+
};
|
|
39
|
+
/** This package's words: an `artifact` at the address, and an `attachment`
|
|
40
|
+
* naming it. */ export declare let artifacts: Naming;
|
|
41
|
+
/** Where bytes are kept and what is taken there. */ export type Upload = {
|
|
42
|
+
/** where the bytes go, under their address */ store: Blobs;
|
|
43
|
+
/** the representations' scope: an app's eid, where one host serves many
|
|
44
|
+
* (default {@link SCOPE}) */ scope?: string;
|
|
45
|
+
/** what is taken: a size, and types read off the bytes */ accepts?: Accepts;
|
|
46
|
+
/** the most bytes one object may be */ limit?: number;
|
|
47
|
+
/** The file a caller committed to sending, checked before it is kept. */ expected?: {
|
|
48
|
+
sha: string;
|
|
49
|
+
bytes: number;
|
|
50
|
+
};
|
|
51
|
+
/** Why the store has no room for `bytes` more at `sha`, its ceiling's
|
|
52
|
+
* refusal, or nothing where they fit. */ room?: (sha: string, bytes: number) => string | Promise<string>;
|
|
53
|
+
/** the words the rows speak (default {@link artifacts}) */ naming?: Naming;
|
|
54
|
+
};
|
|
55
|
+
/** Bytes not taken: too many, of a type not taken, or past the store's room.
|
|
56
|
+
* `status` is the HTTP status a door answers, and `code` its word for it. */ export declare class Untaken extends Error {
|
|
57
|
+
status: number;
|
|
58
|
+
code: string;
|
|
59
|
+
constructor(status: number, code: string, says: string);
|
|
60
|
+
}
|
|
61
|
+
/** Refused, when `bytes` bytes, or bytes read as `type`, are not taken.
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { admit, Untaken } from '@yaks/blob/upload'
|
|
65
|
+
* import { memoryBlobs } from '@yaks/blob'
|
|
66
|
+
* import { assertThrows } from '@std/assert'
|
|
67
|
+
*
|
|
68
|
+
* let at = { store: memoryBlobs(), accepts: { maxLength: 4096, contentMediaType: 'image/*' } }
|
|
69
|
+
* admit(at, 4096, 'image/png')
|
|
70
|
+
* assertThrows(() => admit(at, 4097), Untaken, 'this app takes files of at most 4 KB')
|
|
71
|
+
* assertThrows(() => admit(at, 10, 'text/plain'), Untaken, 'this app takes image/*, and this file is text/plain')
|
|
72
|
+
* assertThrows(() => admit({ ...at, limit: 1024 }, 2048), Untaken, 'a file is at most 1 KB here')
|
|
73
|
+
* ```
|
|
74
|
+
*/ export declare let admit: (at: Upload, bytes: number, type?: string) => void;
|
|
75
|
+
/** Refused where the store has no room for `bytes` more at `sha`. Bytes it
|
|
76
|
+
* holds already count as the store counts them. */ export declare let roomFor: (at: Upload, sha: string, bytes: number) => Promise<void>;
|
|
77
|
+
/** Kept bytes, and the rows that name them. */ export type File = Kept & {
|
|
78
|
+
/** the representation's address beneath a door: `<sha>/<eid>` */ path: string;
|
|
79
|
+
bundles: Bundle[];
|
|
80
|
+
};
|
|
81
|
+
/** How the rows a store holds are read: a query line, its rows. */ export type Read = Reads<Bundle[] | Promise<Bundle[]>, []>;
|
|
82
|
+
/** A name supplied up front or learned as the upload's bytes pass. */ export type UploadName = string | ((seen: Seen) => string);
|
|
83
|
+
/**
|
|
84
|
+
* The rows that name the `bytes` bytes at `sha`, read off `head`: their first
|
|
85
|
+
* bytes, or all of them. A name or type the arrival leaves out is the one the
|
|
86
|
+
* store's rows already say (`read`), since the same file is the same file
|
|
87
|
+
* whatever a page had to call it the second time. The representation is
|
|
88
|
+
* among the rows, fixing the headers the bytes are served with.
|
|
89
|
+
*/ export declare let described: (read: Read, at: Upload, sha: string, head: Uint8Array, mime: string, name: string, bytes?: number) => Promise<File>;
|
|
90
|
+
/**
|
|
91
|
+
* Bytes kept under their address, held to what `at` takes, and the rows that
|
|
92
|
+
* name them, for the caller to write as whoever it decided is writing. A
|
|
93
|
+
* stream ({@link Put}) is kept as it arrives, never held whole (./stream.ts
|
|
94
|
+
* `stage`). Bytes not taken throw {@link Untaken} before any are kept at their
|
|
95
|
+
* address. `check` is asked of the rows before the bytes are kept, so
|
|
96
|
+
* whatever would refuse the write refuses the upload.
|
|
97
|
+
*
|
|
98
|
+
* The bytes land before their rows: a row naming nothing is what a reader
|
|
99
|
+
* sees, while bytes nobody has named yet are invisible until the same file
|
|
100
|
+
* arrives again and names them.
|
|
101
|
+
*/ export declare let filed: (read: Read, at: Upload, bytes: Put, mime: string, name: UploadName, check?: (rows: Bundle[]) => unknown) => Promise<File>;
|
|
102
|
+
/** Bytes staged in `at`'s store as they arrive, refused with {@link Untaken}
|
|
103
|
+
* past the most it takes: before any are read where their length is said,
|
|
104
|
+
* and as they pass where it is not. The caller drops them. */ export declare let admitted: (at: Upload, bytes: Put) => Promise<Staged>;
|
|
105
|
+
/** {@link filed}, for bytes already staged: the caller drops them. */ export declare let settled: (read: Read, at: Upload, staged: Staged, mime: string, name: string, check?: (rows: Bundle[]) => unknown, finish?: (file: File) => File | Promise<File>) => Promise<File>;
|
|
106
|
+
/**
|
|
107
|
+
* The representation `eid` of the bytes at `sha`, read off the store in one
|
|
108
|
+
* query, while it is served: none for one of another scope, one that does
|
|
109
|
+
* not address these bytes, or bytes no row names any more. A representation
|
|
110
|
+
* outlives the rows naming its bytes, and a copy of them may be left in a
|
|
111
|
+
* cache or a bucket, so a deleted upload stops here.
|
|
112
|
+
*/ export declare let servable: (read: Read, at: Pick<Upload, 'scope' | 'naming'>, sha: string, eid: string) => Promise<Representation | null>;
|
|
113
|
+
/** Check a representation already read from the upload's store. */ export declare let servedRepresentation: (scope: string, sha: string, eid: string, rep: Representation | undefined) => Representation | null;
|
|
114
|
+
//# sourceMappingURL=upload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload.d.ts","sources":["../upload.ts"],"names":[],"mappings":"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"}
|
package/_dist/value.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Bundle } from "@yaks/graph";
|
|
2
|
+
export declare const VALUE_LIMIT: 8192;
|
|
3
|
+
/** A provider-neutral tool declaration: JSON Schema arguments in, text out. */ export type ValueTool = {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
parameters: Record<string, unknown>;
|
|
7
|
+
run: (args: Record<string, unknown>) => Promise<string>;
|
|
8
|
+
};
|
|
9
|
+
/** The reader every read goes through: the application's own authorized
|
|
10
|
+
* entity reader. */ export type ValueReader = (entity: string) => Promise<Bundle | undefined>;
|
|
11
|
+
/** A range of a text value: its code points `start` to `end` of `total`, where
|
|
12
|
+
* the next range starts (null at the end), and the `revision` it was read at. */ export type ValueRange = {
|
|
13
|
+
revision: string;
|
|
14
|
+
start: number;
|
|
15
|
+
end: number;
|
|
16
|
+
total: number;
|
|
17
|
+
next: number | null;
|
|
18
|
+
text: string;
|
|
19
|
+
};
|
|
20
|
+
/** What a search of a text value found: each match's offset and the text
|
|
21
|
+
* around it, the `revision` it searched, and where a search goes on from (null
|
|
22
|
+
* once it found fewer matches than it was allowed). */ export type ValueSearch = {
|
|
23
|
+
revision: string;
|
|
24
|
+
matches: {
|
|
25
|
+
offset: number;
|
|
26
|
+
text: string;
|
|
27
|
+
}[];
|
|
28
|
+
next: number | null;
|
|
29
|
+
};
|
|
30
|
+
/** A range of a text value, read through `read`: `count` code points (2048, at
|
|
31
|
+
* most {@link VALUE_LIMIT}) from `start` (0), refused when `revision` is passed
|
|
32
|
+
* and the text is no longer that. */ export declare const valueRange: (read: ValueReader) => any;
|
|
33
|
+
/** The literal, case-sensitive matches of `query` in a text value, read
|
|
34
|
+
* through `read`: from `start` (0), at most `limit` (10, at most 20). */ export declare const valueSearch: (read: ValueReader) => any;
|
|
35
|
+
/** The two tools, built over a reader. Pass the same authorized reader the
|
|
36
|
+
* application's other graph tools use. Each answers a model in text: a line
|
|
37
|
+
* saying where its piece lies, then the piece.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { equal } from '@yaks/testing'
|
|
41
|
+
* import { address } from './store.ts'
|
|
42
|
+
* import { valueTools } from './value.ts'
|
|
43
|
+
*
|
|
44
|
+
* let body = '[\n {\n "title": "Mossvale"\n }\n]'
|
|
45
|
+
* let [read, search] = valueTools(async (eid) =>
|
|
46
|
+
* eid == 'p1' ? { entity: { eid }, post: { body } } : undefined
|
|
47
|
+
* )
|
|
48
|
+
* let at = { entity: 'p1', component: 'post', property: 'body' }
|
|
49
|
+
* let revision = address(body)
|
|
50
|
+
* equal(
|
|
51
|
+
* await read.run({ ...at, start: 4, count: 26 }),
|
|
52
|
+
* `[Code points 4 to 30 of 35, revision ${revision}; next start=30.]\n` +
|
|
53
|
+
* '{\n "title": "Mossvale"\n',
|
|
54
|
+
* )
|
|
55
|
+
* equal(
|
|
56
|
+
* await search.run({ ...at, query: 'Moss', start: 2 }),
|
|
57
|
+
* `[1 match for "Moss" from code point 2, revision ${revision}; no more.]\n` +
|
|
58
|
+
* 'at 20: { "title": "Mossvale" } ]',
|
|
59
|
+
* )
|
|
60
|
+
* ```
|
|
61
|
+
*/ export declare const valueTools: (read: ValueReader) => ValueTool[];
|
|
62
|
+
//# sourceMappingURL=value.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value.d.ts","sources":["../value.ts"],"names":[],"mappings":"AAsBA,cAAc,MAAM,2BAAsC;AAI1D,OAAO,cAAM,aAAc,KAAI;AAyC/B,6EAA6E,GAC7E,YAAY;EACV,MAAM,MAAM;EACZ,aAAa,MAAM;EACnB,YAAY,OAAO,MAAM,EAAE,OAAO;EAClC,MAAM,MAAM,OAAO,MAAM,EAAE,OAAO,MAAM,QAAQ,MAAM;;AAGxD;kBACkB,GAClB,YAAY,eAAe,QAAQ,MAAM,KAAK,QAAQ,SAAS,SAAS;AAExE;+EAC+E,GAC/E,YAAY;EACV,UAAU,MAAM;EAChB,OAAO,MAAM;EACb,KAAK,MAAM;EACX,OAAO,MAAM;EACb,MAAM,MAAM,GAAG,IAAI;EACnB,MAAM,MAAM;;AAGd;;qDAEqD,GACrD,YAAY;EACV,UAAU,MAAM;EAChB;IAAW,QAAQ,MAAM;IAAE,MAAM,MAAM;;EACvC,MAAM,MAAM,GAAG,IAAI;;AAuCrB;;mCAEmC,GACnC,OAAO,cAAM,aACV,MAAM,oBAeN;AAEH;uEACuE,GACvE,OAAO,cAAM,cACV,MAAM,oBAkCN;AAwBH;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC,GACD,OAAO,cAAM,aAAc,MAAM,gBAAc,YAoC9C"}
|
package/_dist/vocab.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Keywords, Vocab, VocabDoc } from "@yaks/vocab";
|
|
2
|
+
import type { Derived } from "@yaks/sql";
|
|
3
|
+
import { artifactDoc } from "./artifact.js";
|
|
4
|
+
import { blobKeywords } from "./keywords.js";
|
|
5
|
+
import { blobRead } from "./sqlite.js";
|
|
6
|
+
export { artifactDoc, blobKeywords, blobRead };
|
|
7
|
+
/** What this package is: its manifest's description, the one place it is
|
|
8
|
+
* written, which a host stamps on each document here. */ export declare let description: string;
|
|
9
|
+
/** Every document this plugin declares. */ export declare let docs: VocabDoc[];
|
|
10
|
+
/** The JSON Schema keywords those documents use. */ export declare let keywords: Keywords[];
|
|
11
|
+
/** Every body property this vocabulary marks `store: blob`, read back as
|
|
12
|
+
* text. */ export declare let derived: (vocab: Vocab) => Derived;
|
|
13
|
+
//# sourceMappingURL=vocab.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAUA,cAAc,QAAQ,EAAE,KAAK,EAAE,QAAQ,2BAAgC;AACvE,cAAc,OAAO,yBAA8B;AACnD,SAAS,WAAW,wBAAuB;AAC3C,SAAS,YAAY,wBAAuB;AAC5C,SAAS,QAAQ,sBAAqB;AAGtC,SAAS,WAAW,EAAE,YAAY,EAAE,QAAQ,GAAE;AAE9C;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAA0B;AAE3C,kDAAkD,GAClD,OAAO,YAAI,UAAU,WAA2B;AAEhD;SACS,GACT,OAAO,YAAI,UAAW,OAAO,UAAQ,QAA0B"}
|
package/accepts.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// What an application takes as a blob, said in JSON Schema's own words for a
|
|
2
|
+
// string of bytes: `maxLength`, the most bytes one may hold, and
|
|
3
|
+
// `contentMediaType`, the media types it may be. A host checks a blob's size
|
|
4
|
+
// against the first and the type read off its bytes (`contentType`) against
|
|
5
|
+
// the second, so a name or a declared type never gets a file past it.
|
|
6
|
+
//
|
|
7
|
+
// `contentMediaType` takes one type or a list, and a type may be a range
|
|
8
|
+
// (`image/*`, `*/*`), the way an `<input accept>` does. Nothing declared takes
|
|
9
|
+
// everything: the limits are the application author's to set.
|
|
10
|
+
/** What an application takes as a blob. */ let RANGE = /^(\*\/\*|[a-z0-9.+-]+\/([a-z0-9.+-]+|\*))$/i;
|
|
11
|
+
let WORDS = [
|
|
12
|
+
'maxLength',
|
|
13
|
+
'contentMediaType'
|
|
14
|
+
];
|
|
15
|
+
let ranges = (a)=>a?.contentMediaType == null ? [] : [
|
|
16
|
+
a.contentMediaType
|
|
17
|
+
].flat().map((r)=>r.toLowerCase());
|
|
18
|
+
/**
|
|
19
|
+
* What is wrong with a declaration, a sentence each; none when it is one.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { assertEquals } from '@std/assert'
|
|
23
|
+
* import { acceptsErrors } from '@yaks/blob'
|
|
24
|
+
*
|
|
25
|
+
* assertEquals(acceptsErrors({ maxLength: 1048576, contentMediaType: 'image/*' }), [])
|
|
26
|
+
* assertEquals(acceptsErrors({ maxLength: -1 }), [
|
|
27
|
+
* 'maxLength is a whole number of bytes, more than none',
|
|
28
|
+
* ])
|
|
29
|
+
* assertEquals(acceptsErrors({ contentMediaType: ['image'] }), [
|
|
30
|
+
* 'contentMediaType "image" is a media type like image/png, or a range like image/*',
|
|
31
|
+
* ])
|
|
32
|
+
* ```
|
|
33
|
+
*/ export let acceptsErrors = (said)=>{
|
|
34
|
+
if (!said || typeof said != 'object' || Array.isArray(said)) {
|
|
35
|
+
return [
|
|
36
|
+
'an object: {"maxLength": bytes, "contentMediaType": "image/*"}'
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
let a = said;
|
|
40
|
+
let types = a.contentMediaType;
|
|
41
|
+
let list = Array.isArray(types) ? types : types == null ? [] : [
|
|
42
|
+
types
|
|
43
|
+
];
|
|
44
|
+
return [
|
|
45
|
+
...Object.keys(a).filter((k)=>!WORDS.includes(k)).map((k)=>`${k} is not one of its words, which are ${WORDS.join(' and ')}`),
|
|
46
|
+
...a.maxLength == null || Number.isSafeInteger(a.maxLength) && Number(a.maxLength) > 0 ? [] : [
|
|
47
|
+
'maxLength is a whole number of bytes, more than none'
|
|
48
|
+
],
|
|
49
|
+
...Array.isArray(types) && !types.length ? [
|
|
50
|
+
'contentMediaType lists at least one type'
|
|
51
|
+
] : [],
|
|
52
|
+
...list.filter((t)=>typeof t != 'string' || !RANGE.test(t)).map((t)=>`contentMediaType ${JSON.stringify(t)} is a media type like image/png, or a range like image/*`)
|
|
53
|
+
];
|
|
54
|
+
};
|
|
55
|
+
/** Whether a blob of `bytes` bytes is not too big. */ export let fits = (a, bytes)=>a?.maxLength == null || bytes <= a.maxLength;
|
|
56
|
+
/**
|
|
57
|
+
* Whether a blob of media type `type` is one of the types taken.
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { assertEquals } from '@std/assert'
|
|
61
|
+
* import { takes } from '@yaks/blob'
|
|
62
|
+
*
|
|
63
|
+
* let photos = { contentMediaType: ['image/*', 'application/pdf'] }
|
|
64
|
+
* assertEquals(takes(photos, 'image/png'), true)
|
|
65
|
+
* assertEquals(takes(photos, 'application/pdf'), true)
|
|
66
|
+
* assertEquals(takes(photos, 'video/mp4'), false)
|
|
67
|
+
* assertEquals(takes({ contentMediaType: '*' + '/*' }, 'video/mp4'), true)
|
|
68
|
+
* assertEquals(takes({}, 'video/mp4'), true)
|
|
69
|
+
* ```
|
|
70
|
+
*/ export let takes = (a, type)=>{
|
|
71
|
+
let said = ranges(a);
|
|
72
|
+
let [top] = type.split('/');
|
|
73
|
+
return !said.length || said.some((r)=>r == type || r == '*/*' || r == `${top}/*`);
|
|
74
|
+
};
|
|
75
|
+
/** The types taken, as a sentence names them: `image/* or application/pdf`. */ export let taken = (a)=>ranges(a).join(' or ') || '*/*';
|
|
76
|
+
//# sourceMappingURL=accepts.js.map
|
package/accepts.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accepts.js","sources":["./accepts.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,iEAAiE;AACjE,6EAA6E;AAC7E,4EAA4E;AAC5E,sEAAsE;AACtE,EAAE;AACF,yEAAyE;AACzE,+EAA+E;AAC/E,8DAA8D;AAE9D,yCAAyC,GAMzC,IAAI,QAAQ;AACZ,IAAI,QAAQ;EAAC;EAAa;CAAmB;AAE7C,IAAI,SAAS,CAAC,IACZ,GAAG,oBAAoB,OACnB,EAAE,GACF;IAAC,EAAE,gBAAgB;GAAC,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,IAAM,EAAE,WAAW;AAE1D;;;;;;;;;;;;;;;CAeC,GACD,OAAO,IAAI,gBAAgB,CAAC;EAC1B,IAAI,CAAC,QAAQ,OAAO,QAAQ,YAAY,MAAM,OAAO,CAAC,OAAO;IAC3D,OAAO;MAAC;KAAiE;EAC3E;EACA,IAAI,IAAI;EACR,IAAI,QAAQ,EAAE,gBAAgB;EAC9B,IAAI,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ,SAAS,OAAO,EAAE,GAAG;IAAC;GAAM;EACtE,OAAO;OACF,OAAO,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,IAAM,CAAC,MAAM,QAAQ,CAAC,IAAI,GAAG,CAAC,CAAC,IACvD,GAAG,EAAE,oCAAoC,EAAE,MAAM,IAAI,CAAC,UAAU;OAE9D,EAAE,SAAS,IAAI,QACd,OAAO,aAAa,CAAC,EAAE,SAAS,KAAK,OAAO,EAAE,SAAS,IAAI,IAC5D,EAAE,GACF;MAAC;KAAuD;OACxD,MAAM,OAAO,CAAC,UAAU,CAAC,MAAM,MAAM,GACrC;MAAC;KAA2C,GAC5C,EAAE;OACH,KAAK,MAAM,CAAC,CAAC,IAAM,OAAO,KAAK,YAAY,CAAC,MAAM,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,IACjE,CAAC,iBAAiB,EAChB,KAAK,SAAS,CAAC,GAChB,wDAAwD,CAAC;GAE7D;AACH,EAAC;AAED,oDAAoD,GACpD,OAAO,IAAI,OAAO,CAAC,GAAwB,QACzC,GAAG,aAAa,QAAQ,SAAS,EAAE,SAAS,CAAA;AAE9C;;;;;;;;;;;;;;CAcC,GACD,OAAO,IAAI,QAAQ,CAAC,GAAwB;EAC1C,IAAI,OAAO,OAAO;EAClB,IAAI,CAAC,IAAI,GAAG,KAAK,KAAK,CAAC;EACvB,OAAO,CAAC,KAAK,MAAM,IACjB,KAAK,IAAI,CAAC,CAAC,IAAM,KAAK,QAAQ,KAAK,SAAS,KAAK,GAAG,IAAI,EAAE,CAAC;AAC/D,EAAC;AAED,6EAA6E,GAC7E,OAAO,IAAI,QAAQ,CAAC,IAClB,OAAO,GAAG,IAAI,CAAC,WAAW,MAAK"}
|
package/accepts.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// What an application takes as a blob, said in JSON Schema's own words for a
|
|
2
|
+
// string of bytes: `maxLength`, the most bytes one may hold, and
|
|
3
|
+
// `contentMediaType`, the media types it may be. A host checks a blob's size
|
|
4
|
+
// against the first and the type read off its bytes (`contentType`) against
|
|
5
|
+
// the second, so a name or a declared type never gets a file past it.
|
|
6
|
+
//
|
|
7
|
+
// `contentMediaType` takes one type or a list, and a type may be a range
|
|
8
|
+
// (`image/*`, `*/*`), the way an `<input accept>` does. Nothing declared takes
|
|
9
|
+
// everything: the limits are the application author's to set.
|
|
10
|
+
|
|
11
|
+
/** What an application takes as a blob. */
|
|
12
|
+
export type Accepts = {
|
|
13
|
+
maxLength?: number
|
|
14
|
+
contentMediaType?: string | string[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let RANGE = /^(\*\/\*|[a-z0-9.+-]+\/([a-z0-9.+-]+|\*))$/i
|
|
18
|
+
let WORDS = ['maxLength', 'contentMediaType']
|
|
19
|
+
|
|
20
|
+
let ranges = (a: Accepts | undefined) =>
|
|
21
|
+
a?.contentMediaType == null
|
|
22
|
+
? []
|
|
23
|
+
: [a.contentMediaType].flat().map((r) => r.toLowerCase())
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What is wrong with a declaration, a sentence each; none when it is one.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { assertEquals } from '@std/assert'
|
|
30
|
+
* import { acceptsErrors } from '@yaks/blob'
|
|
31
|
+
*
|
|
32
|
+
* assertEquals(acceptsErrors({ maxLength: 1048576, contentMediaType: 'image/*' }), [])
|
|
33
|
+
* assertEquals(acceptsErrors({ maxLength: -1 }), [
|
|
34
|
+
* 'maxLength is a whole number of bytes, more than none',
|
|
35
|
+
* ])
|
|
36
|
+
* assertEquals(acceptsErrors({ contentMediaType: ['image'] }), [
|
|
37
|
+
* 'contentMediaType "image" is a media type like image/png, or a range like image/*',
|
|
38
|
+
* ])
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export let acceptsErrors = (said: unknown): string[] => {
|
|
42
|
+
if (!said || typeof said != 'object' || Array.isArray(said)) {
|
|
43
|
+
return ['an object: {"maxLength": bytes, "contentMediaType": "image/*"}']
|
|
44
|
+
}
|
|
45
|
+
let a = said as Record<string, unknown>
|
|
46
|
+
let types = a.contentMediaType
|
|
47
|
+
let list = Array.isArray(types) ? types : types == null ? [] : [types]
|
|
48
|
+
return [
|
|
49
|
+
...Object.keys(a).filter((k) => !WORDS.includes(k)).map((k) =>
|
|
50
|
+
`${k} is not one of its words, which are ${WORDS.join(' and ')}`
|
|
51
|
+
),
|
|
52
|
+
...(a.maxLength == null ||
|
|
53
|
+
(Number.isSafeInteger(a.maxLength) && Number(a.maxLength) > 0)
|
|
54
|
+
? []
|
|
55
|
+
: ['maxLength is a whole number of bytes, more than none']),
|
|
56
|
+
...(Array.isArray(types) && !types.length
|
|
57
|
+
? ['contentMediaType lists at least one type']
|
|
58
|
+
: []),
|
|
59
|
+
...list.filter((t) => typeof t != 'string' || !RANGE.test(t)).map((t) =>
|
|
60
|
+
`contentMediaType ${
|
|
61
|
+
JSON.stringify(t)
|
|
62
|
+
} is a media type like image/png, or a range like image/*`
|
|
63
|
+
),
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Whether a blob of `bytes` bytes is not too big. */
|
|
68
|
+
export let fits = (a: Accepts | undefined, bytes: number): boolean =>
|
|
69
|
+
a?.maxLength == null || bytes <= a.maxLength
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether a blob of media type `type` is one of the types taken.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { assertEquals } from '@std/assert'
|
|
76
|
+
* import { takes } from '@yaks/blob'
|
|
77
|
+
*
|
|
78
|
+
* let photos = { contentMediaType: ['image/*', 'application/pdf'] }
|
|
79
|
+
* assertEquals(takes(photos, 'image/png'), true)
|
|
80
|
+
* assertEquals(takes(photos, 'application/pdf'), true)
|
|
81
|
+
* assertEquals(takes(photos, 'video/mp4'), false)
|
|
82
|
+
* assertEquals(takes({ contentMediaType: '*' + '/*' }, 'video/mp4'), true)
|
|
83
|
+
* assertEquals(takes({}, 'video/mp4'), true)
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export let takes = (a: Accepts | undefined, type: string): boolean => {
|
|
87
|
+
let said = ranges(a)
|
|
88
|
+
let [top] = type.split('/')
|
|
89
|
+
return !said.length ||
|
|
90
|
+
said.some((r) => r == type || r == '*/*' || r == `${top}/*`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The types taken, as a sentence names them: `image/* or application/pdf`. */
|
|
94
|
+
export let taken = (a: Accepts | undefined): string =>
|
|
95
|
+
ranges(a).join(' or ') || '*/*'
|
package/artifact.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Bytes that are not text: a picture, a PDF, anything a property would only get
|
|
2
|
+
// in the way of. They are stored the same way a body property is — under the
|
|
3
|
+
// SHA-256 of the bytes themselves — so an artifact has one identity wherever it
|
|
4
|
+
// came from, and a row that names it names that exact object.
|
|
5
|
+
//
|
|
6
|
+
// The address is computed here rather than taken on trust, and the store is
|
|
7
|
+
// read back and compared before the caller is told the bytes are kept: a store
|
|
8
|
+
// that cannot hold what it was given (a text table handed a PNG) reports it at
|
|
9
|
+
// the write, not at the next read.
|
|
10
|
+
//
|
|
11
|
+
// `crypto.subtle` is the web platform's own, so this loads anywhere the package
|
|
12
|
+
// does — which is why the digest here returns a promise, and why ./store.ts
|
|
13
|
+
// keeps a separate synchronous one for text properties.
|
|
14
|
+
import doc from './vocab.json' with {
|
|
15
|
+
type: 'json'
|
|
16
|
+
};
|
|
17
|
+
/** What these bytes are called: their SHA-256, lowercase hex. It is both the
|
|
18
|
+
* key in the store and the eid of the row, so there is no second name for the
|
|
19
|
+
* two sides to agree on. */ export let addressOf = async (bytes)=>{
|
|
20
|
+
let digest = await crypto.subtle.digest('SHA-256', bytes);
|
|
21
|
+
return Array.from(new Uint8Array(digest), (b)=>b.toString(16).padStart(2, '0')).join('');
|
|
22
|
+
};
|
|
23
|
+
/** Put bytes under their address, once, and verify the store kept them. Writing
|
|
24
|
+
* the same pair twice is a no-op — the second copy is the first one — and a
|
|
25
|
+
* store that hands back anything else has not kept this object, which throws
|
|
26
|
+
* here rather than returning corrupt bytes later. A store that keeps any bytes
|
|
27
|
+
* as given (`exact`) is put to once: the same pair again writes what is
|
|
28
|
+
* already there, and a bucket's look first would be a second round trip. */ export let keep = async (blobs, address, bytes)=>{
|
|
29
|
+
if (blobs.exact) return void await blobs.put(address, bytes);
|
|
30
|
+
let equal = (stored)=>stored?.length == bytes.length && stored.every((b, i)=>b == bytes[i]);
|
|
31
|
+
if (!equal(await blobs.get(address))) await blobs.put(address, bytes);
|
|
32
|
+
if (!equal(await blobs.get(address))) {
|
|
33
|
+
throw new Error(`@yaks/blob: the store did not keep ${address}`);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/** The bytes an {@link Artifact} names, read back from where they are kept:
|
|
37
|
+
* `undefined` when the store holds nothing under its address, and an error
|
|
38
|
+
* when what it holds is not those bytes — another size, or another SHA-256.
|
|
39
|
+
* A row is a promise about the bytes, and this is where the promise is
|
|
40
|
+
* checked, so a reader never hands on bytes the row did not name. Bytes known
|
|
41
|
+
* by their address alone are checked against it alone. */ export let artifactBytes = async (blobs, artifact)=>{
|
|
42
|
+
let bytes = await blobs.get(artifact.address);
|
|
43
|
+
if (!bytes) return;
|
|
44
|
+
if (artifact.size != null && bytes.length != artifact.size || await addressOf(bytes) != artifact.address) {
|
|
45
|
+
throw new Error(`@yaks/blob: the bytes kept under ${artifact.address} are not the ones it names`);
|
|
46
|
+
}
|
|
47
|
+
return bytes;
|
|
48
|
+
};
|
|
49
|
+
export let artifactDoc = doc;
|
|
50
|
+
//# sourceMappingURL=artifact.js.map
|
package/artifact.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"artifact.js","sources":["./artifact.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,6EAA6E;AAC7E,gFAAgF;AAChF,8DAA8D;AAC9D,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAC/E,+EAA+E;AAC/E,mCAAmC;AACnC,EAAE;AACF,gFAAgF;AAChF,4EAA4E;AAC5E,wDAAwD;AAIxD,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AAmBpD;;0BAE0B,GAC1B,OAAO,IAAI,YAAY,OAAO;EAC5B,IAAI,SAAS,MAAM,OAAO,MAAM,CAAC,MAAM,CACrC,WACA;EAEF,OAAO,MAAM,IAAI,CACf,IAAI,WAAW,SACf,CAAC,IAAM,EAAE,QAAQ,CAAC,IAAI,QAAQ,CAAC,GAAG,MAClC,IAAI,CAAC;AACT,EAAC;AAED;;;;;0EAK0E,GAC1E,OAAO,IAAI,OAAO,OAChB,OACA,SACA;EAEA,IAAI,MAAM,KAAK,EAAE,OAAO,KAAK,MAAM,MAAM,GAAG,CAAC,SAAS;EACtD,IAAI,QAAQ,CAAC,SACX,QAAQ,UAAU,MAAM,MAAM,IAAI,OAAO,KAAK,CAAC,CAAC,GAAG,IAAM,KAAK,KAAK,CAAC,EAAE;EACxE,IAAI,CAAC,MAAM,MAAM,MAAM,GAAG,CAAC,WAAW,MAAM,MAAM,GAAG,CAAC,SAAS;EAC/D,IAAI,CAAC,MAAM,MAAM,MAAM,GAAG,CAAC,WAAW;IACpC,MAAM,IAAI,MAAM,CAAC,mCAAmC,EAAE,SAAS;EACjE;AACF,EAAC;AAED;;;;;wDAKwD,GACxD,OAAO,IAAI,gBAAgB,OACzB,OACA;EAEA,IAAI,QAAQ,MAAM,MAAM,GAAG,CAAC,SAAS,OAAO;EAC5C,IAAI,CAAC,OAAO;EACZ,IACE,AAAC,SAAS,IAAI,IAAI,QAAQ,MAAM,MAAM,IAAI,SAAS,IAAI,IACvD,MAAM,UAAU,UAAU,SAAS,OAAO,EAC1C;IACA,MAAM,IAAI,MACR,CAAC,iCAAiC,EAAE,SAAS,OAAO,CAAC,0BAA0B,CAAC;EAEpF;EACA,OAAO;AACT,EAAC;AAED,OAAO,IAAI,cAAwB,IAAG"}
|
package/artifact.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Bytes that are not text: a picture, a PDF, anything a property would only get
|
|
2
|
+
// in the way of. They are stored the same way a body property is — under the
|
|
3
|
+
// SHA-256 of the bytes themselves — so an artifact has one identity wherever it
|
|
4
|
+
// came from, and a row that names it names that exact object.
|
|
5
|
+
//
|
|
6
|
+
// The address is computed here rather than taken on trust, and the store is
|
|
7
|
+
// read back and compared before the caller is told the bytes are kept: a store
|
|
8
|
+
// that cannot hold what it was given (a text table handed a PNG) reports it at
|
|
9
|
+
// the write, not at the next read.
|
|
10
|
+
//
|
|
11
|
+
// `crypto.subtle` is the web platform's own, so this loads anywhere the package
|
|
12
|
+
// does — which is why the digest here returns a promise, and why ./store.ts
|
|
13
|
+
// keeps a separate synchronous one for text properties.
|
|
14
|
+
|
|
15
|
+
import type { Blobs } from './store.ts'
|
|
16
|
+
import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
|
|
17
|
+
import doc from './vocab.json' with { type: 'json' }
|
|
18
|
+
|
|
19
|
+
export type Artifact = {
|
|
20
|
+
address: string
|
|
21
|
+
media_type: string
|
|
22
|
+
size: number
|
|
23
|
+
}
|
|
24
|
+
export type ArtifactStore =
|
|
25
|
+
& ((
|
|
26
|
+
bytes: Uint8Array,
|
|
27
|
+
mediaType: string,
|
|
28
|
+
) => Promise<Artifact>)
|
|
29
|
+
& {
|
|
30
|
+
/** Refuses when the store has no room for more, so a model that makes
|
|
31
|
+
* bytes is asked before anything is paid for them. A store that is never
|
|
32
|
+
* full has none. */
|
|
33
|
+
room?: () => Promise<void>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** What these bytes are called: their SHA-256, lowercase hex. It is both the
|
|
37
|
+
* key in the store and the eid of the row, so there is no second name for the
|
|
38
|
+
* two sides to agree on. */
|
|
39
|
+
export let addressOf = async (bytes: Uint8Array): Promise<string> => {
|
|
40
|
+
let digest = await crypto.subtle.digest(
|
|
41
|
+
'SHA-256',
|
|
42
|
+
bytes as Uint8Array<ArrayBuffer>,
|
|
43
|
+
)
|
|
44
|
+
return Array.from(
|
|
45
|
+
new Uint8Array(digest),
|
|
46
|
+
(b) => b.toString(16).padStart(2, '0'),
|
|
47
|
+
).join('')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Put bytes under their address, once, and verify the store kept them. Writing
|
|
51
|
+
* the same pair twice is a no-op — the second copy is the first one — and a
|
|
52
|
+
* store that hands back anything else has not kept this object, which throws
|
|
53
|
+
* here rather than returning corrupt bytes later. A store that keeps any bytes
|
|
54
|
+
* as given (`exact`) is put to once: the same pair again writes what is
|
|
55
|
+
* already there, and a bucket's look first would be a second round trip. */
|
|
56
|
+
export let keep = async (
|
|
57
|
+
blobs: Blobs,
|
|
58
|
+
address: string,
|
|
59
|
+
bytes: Uint8Array,
|
|
60
|
+
): Promise<void> => {
|
|
61
|
+
if (blobs.exact) return void await blobs.put(address, bytes)
|
|
62
|
+
let equal = (stored: Uint8Array | undefined) =>
|
|
63
|
+
stored?.length == bytes.length && stored.every((b, i) => b == bytes[i])
|
|
64
|
+
if (!equal(await blobs.get(address))) await blobs.put(address, bytes)
|
|
65
|
+
if (!equal(await blobs.get(address))) {
|
|
66
|
+
throw new Error(`@yaks/blob: the store did not keep ${address}`)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The bytes an {@link Artifact} names, read back from where they are kept:
|
|
71
|
+
* `undefined` when the store holds nothing under its address, and an error
|
|
72
|
+
* when what it holds is not those bytes — another size, or another SHA-256.
|
|
73
|
+
* A row is a promise about the bytes, and this is where the promise is
|
|
74
|
+
* checked, so a reader never hands on bytes the row did not name. Bytes known
|
|
75
|
+
* by their address alone are checked against it alone. */
|
|
76
|
+
export let artifactBytes = async (
|
|
77
|
+
blobs: Blobs,
|
|
78
|
+
artifact: Pick<Artifact, 'address'> & Partial<Artifact>,
|
|
79
|
+
): Promise<Uint8Array | undefined> => {
|
|
80
|
+
let bytes = await blobs.get(artifact.address)
|
|
81
|
+
if (!bytes) return
|
|
82
|
+
if (
|
|
83
|
+
(artifact.size != null && bytes.length != artifact.size) ||
|
|
84
|
+
await addressOf(bytes) != artifact.address
|
|
85
|
+
) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`@yaks/blob: the bytes kept under ${artifact.address} are not the ones it names`,
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
return bytes
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export let artifactDoc: VocabDoc = doc
|