llm-large-files 0.8.2 → 0.9.0

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 (201) hide show
  1. package/CHANGELOG.md +535 -0
  2. package/README.md +9 -10
  3. package/dist/access.d.ts +86 -0
  4. package/dist/access.d.ts.map +1 -0
  5. package/dist/access.js +131 -0
  6. package/dist/access.js.map +1 -0
  7. package/dist/ask.d.ts +45 -1
  8. package/dist/ask.d.ts.map +1 -1
  9. package/dist/ask.js +181 -20
  10. package/dist/ask.js.map +1 -1
  11. package/dist/cache.d.ts +12 -3
  12. package/dist/cache.d.ts.map +1 -1
  13. package/dist/cache.js +18 -5
  14. package/dist/cache.js.map +1 -1
  15. package/dist/encoding.js +5 -3
  16. package/dist/encoding.js.map +1 -1
  17. package/dist/error-info.d.ts +57 -0
  18. package/dist/error-info.d.ts.map +1 -0
  19. package/dist/error-info.js +217 -0
  20. package/dist/error-info.js.map +1 -0
  21. package/dist/errors.d.ts +72 -8
  22. package/dist/errors.d.ts.map +1 -1
  23. package/dist/errors.js +102 -15
  24. package/dist/errors.js.map +1 -1
  25. package/dist/events.d.ts +35 -9
  26. package/dist/events.d.ts.map +1 -1
  27. package/dist/events.js +42 -11
  28. package/dist/events.js.map +1 -1
  29. package/dist/evidence.d.ts +4 -2
  30. package/dist/evidence.d.ts.map +1 -1
  31. package/dist/evidence.js +14 -3
  32. package/dist/evidence.js.map +1 -1
  33. package/dist/extract.d.ts +32 -8
  34. package/dist/extract.d.ts.map +1 -1
  35. package/dist/extract.js +46 -35
  36. package/dist/extract.js.map +1 -1
  37. package/dist/gemini/adapter.d.ts +19 -3
  38. package/dist/gemini/adapter.d.ts.map +1 -1
  39. package/dist/gemini/adapter.js +84 -12
  40. package/dist/gemini/adapter.js.map +1 -1
  41. package/dist/gemini/chat.d.ts +18 -6
  42. package/dist/gemini/chat.d.ts.map +1 -1
  43. package/dist/gemini/chat.js +2 -0
  44. package/dist/gemini/chat.js.map +1 -1
  45. package/dist/gemini/index.d.ts +1 -1
  46. package/dist/gemini/index.d.ts.map +1 -1
  47. package/dist/index.d.ts +9 -6
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +4 -3
  50. package/dist/index.js.map +1 -1
  51. package/dist/ingest.d.ts +70 -0
  52. package/dist/ingest.d.ts.map +1 -0
  53. package/dist/ingest.js +868 -0
  54. package/dist/ingest.js.map +1 -0
  55. package/dist/library.d.ts +169 -34
  56. package/dist/library.d.ts.map +1 -1
  57. package/dist/library.js +324 -591
  58. package/dist/library.js.map +1 -1
  59. package/dist/map-reduce.js +3 -3
  60. package/dist/map-reduce.js.map +1 -1
  61. package/dist/model.d.ts +43 -2
  62. package/dist/model.d.ts.map +1 -1
  63. package/dist/model.js +63 -3
  64. package/dist/model.js.map +1 -1
  65. package/dist/node/handlers.d.ts +226 -14
  66. package/dist/node/handlers.d.ts.map +1 -1
  67. package/dist/node/handlers.js +423 -181
  68. package/dist/node/handlers.js.map +1 -1
  69. package/dist/node/index.d.ts +3 -2
  70. package/dist/node/index.d.ts.map +1 -1
  71. package/dist/node/index.js +2 -1
  72. package/dist/node/index.js.map +1 -1
  73. package/dist/node/ingest-child.d.ts +7 -1
  74. package/dist/node/ingest-child.d.ts.map +1 -1
  75. package/dist/node/ingest-child.js +18 -3
  76. package/dist/node/ingest-child.js.map +1 -1
  77. package/dist/node/isolated.d.ts +3 -1
  78. package/dist/node/isolated.d.ts.map +1 -1
  79. package/dist/node/isolated.js +11 -4
  80. package/dist/node/isolated.js.map +1 -1
  81. package/dist/node/originals.d.ts +19 -0
  82. package/dist/node/originals.d.ts.map +1 -0
  83. package/dist/node/originals.js +82 -0
  84. package/dist/node/originals.js.map +1 -0
  85. package/dist/node/spool.d.ts +102 -0
  86. package/dist/node/spool.d.ts.map +1 -0
  87. package/dist/node/spool.js +365 -0
  88. package/dist/node/spool.js.map +1 -0
  89. package/dist/node/sse.d.ts +4 -3
  90. package/dist/node/sse.d.ts.map +1 -1
  91. package/dist/node/sse.js +7 -6
  92. package/dist/node/sse.js.map +1 -1
  93. package/dist/pdf-layout.d.ts.map +1 -1
  94. package/dist/pdf-layout.js +71 -12
  95. package/dist/pdf-layout.js.map +1 -1
  96. package/dist/ranged.d.ts +15 -11
  97. package/dist/ranged.d.ts.map +1 -1
  98. package/dist/ranged.js +42 -11
  99. package/dist/ranged.js.map +1 -1
  100. package/dist/retry.d.ts +3 -1
  101. package/dist/retry.d.ts.map +1 -1
  102. package/dist/retry.js +6 -2
  103. package/dist/retry.js.map +1 -1
  104. package/dist/scope.d.ts +25 -0
  105. package/dist/scope.d.ts.map +1 -0
  106. package/dist/scope.js +60 -0
  107. package/dist/scope.js.map +1 -0
  108. package/dist/source.d.ts +60 -0
  109. package/dist/source.d.ts.map +1 -0
  110. package/dist/source.js +170 -0
  111. package/dist/source.js.map +1 -0
  112. package/dist/stores.d.ts +120 -31
  113. package/dist/stores.d.ts.map +1 -1
  114. package/dist/stores.js +582 -139
  115. package/dist/stores.js.map +1 -1
  116. package/dist/testing/fake-fetch.d.ts +31 -0
  117. package/dist/testing/fake-fetch.d.ts.map +1 -0
  118. package/dist/testing/fake-fetch.js +66 -0
  119. package/dist/testing/fake-fetch.js.map +1 -0
  120. package/dist/testing/fake-model.d.ts +38 -0
  121. package/dist/testing/fake-model.d.ts.map +1 -0
  122. package/dist/testing/fake-model.js +62 -0
  123. package/dist/testing/fake-model.js.map +1 -0
  124. package/dist/testing/index.d.ts +7 -0
  125. package/dist/testing/index.d.ts.map +1 -0
  126. package/dist/testing/index.js +9 -0
  127. package/dist/testing/index.js.map +1 -0
  128. package/dist/testing/store-checks.d.ts +72 -0
  129. package/dist/testing/store-checks.d.ts.map +1 -0
  130. package/dist/testing/store-checks.js +619 -0
  131. package/dist/testing/store-checks.js.map +1 -0
  132. package/dist/tools.d.ts +25 -0
  133. package/dist/tools.d.ts.map +1 -1
  134. package/dist/tools.js +135 -13
  135. package/dist/tools.js.map +1 -1
  136. package/dist/types.d.ts +116 -13
  137. package/dist/types.d.ts.map +1 -1
  138. package/docs/README.md +17 -8
  139. package/docs/getting-started.md +18 -13
  140. package/docs/guides/anthropic.md +12 -8
  141. package/docs/guides/conversations.md +112 -47
  142. package/docs/guides/costs-and-limits.md +16 -7
  143. package/docs/guides/deployment.md +284 -83
  144. package/docs/guides/errors-and-reliability.md +161 -23
  145. package/docs/guides/own-adapter-and-loop.md +27 -10
  146. package/docs/guides/pdfs.md +123 -29
  147. package/docs/guides/storage.md +633 -145
  148. package/docs/guides/testing.md +78 -129
  149. package/docs/guides/vertex-and-gemini.md +10 -7
  150. package/docs/guides/web-app.md +312 -78
  151. package/docs/how-it-works.md +35 -8
  152. package/docs/measurements.md +43 -8
  153. package/docs/publishing.md +112 -36
  154. package/docs/reference/ask.md +154 -13
  155. package/docs/reference/errors-and-retries.md +166 -14
  156. package/docs/reference/events.md +53 -20
  157. package/docs/reference/gemini.md +70 -21
  158. package/docs/reference/library.md +706 -165
  159. package/docs/reference/node.md +624 -110
  160. package/docs/reference/testing.md +311 -0
  161. package/docs/reference/tools.md +15 -5
  162. package/docs/security.md +304 -67
  163. package/docs/troubleshooting.md +144 -65
  164. package/docs/upgrading-0.9.md +890 -0
  165. package/package.json +12 -3
  166. package/src/access.ts +144 -0
  167. package/src/ask.ts +230 -19
  168. package/src/cache.ts +20 -5
  169. package/src/encoding.ts +6 -4
  170. package/src/error-info.ts +255 -0
  171. package/src/errors.ts +129 -15
  172. package/src/events.ts +86 -21
  173. package/src/evidence.ts +14 -3
  174. package/src/extract.ts +50 -33
  175. package/src/gemini/adapter.ts +97 -11
  176. package/src/gemini/chat.ts +20 -6
  177. package/src/gemini/index.ts +1 -1
  178. package/src/index.ts +9 -6
  179. package/src/ingest.ts +927 -0
  180. package/src/library.ts +467 -601
  181. package/src/map-reduce.ts +3 -3
  182. package/src/model.ts +69 -5
  183. package/src/node/handlers.ts +630 -183
  184. package/src/node/index.ts +13 -2
  185. package/src/node/ingest-child.ts +23 -5
  186. package/src/node/isolated.ts +11 -4
  187. package/src/node/originals.ts +78 -0
  188. package/src/node/spool.ts +365 -0
  189. package/src/node/sse.ts +7 -6
  190. package/src/pdf-layout.ts +69 -11
  191. package/src/ranged.ts +34 -13
  192. package/src/retry.ts +6 -2
  193. package/src/scope.ts +63 -0
  194. package/src/source.ts +178 -0
  195. package/src/stores.ts +635 -140
  196. package/src/testing/fake-fetch.ts +80 -0
  197. package/src/testing/fake-model.ts +78 -0
  198. package/src/testing/index.ts +11 -0
  199. package/src/testing/store-checks.ts +660 -0
  200. package/src/tools.ts +150 -14
  201. package/src/types.ts +106 -13
@@ -1,5 +1,112 @@
1
1
  import type { IncomingMessage, ServerResponse } from "node:http";
2
+ import { type IngestErrorCode } from "../errors.js";
2
3
  import { type LargeFiles } from "../library.js";
4
+ import type { DocumentMeta, DocumentScope } from "../types.js";
5
+ /** What `onUploaded` is told besides the document. New in 0.9.0. */
6
+ export type UploadedContext = {
7
+ /**
8
+ * The upload's request, for its headers and URL. Its body has been read: the bytes are not here,
9
+ * unless a framework left them in `request.body` (`express.raw()`), which keeps them until the
10
+ * request ends.
11
+ */
12
+ request: IncomingMessage;
13
+ /** What `getOwner` returned, when it returned an owner. */
14
+ owner?: string;
15
+ /** What `getScope` returned, when it returned a scope (a frozen copy). */
16
+ scope?: DocumentScope;
17
+ /**
18
+ * True when the client went away before the hook was called: the document is stored all the
19
+ * same, and nobody gets the answer. Record it, or remove it, as your app needs. With the
20
+ * library's own `ingest`, a client that leaves before the ingest ends leaves nothing stored, and
21
+ * the hook is not called: this is for a client that left after the ingest ended, or for an
22
+ * `ingest` of your own that does not stop on its `signal`.
23
+ */
24
+ clientGone: boolean;
25
+ /**
26
+ * The path the body was saved to: the `path` that `prepareUpload` returned. Only for such an
27
+ * upload: the file is yours, and stays (unless this hook throws). A file of the spool
28
+ * (`uploadSpool`) is gone by now, and is not told. New in 0.9.0.
29
+ */
30
+ path?: string;
31
+ };
32
+ /** What `prepareUpload` is told of an upload, before its body is read. New in 0.9.0. */
33
+ export type PrepareUploadInfo = {
34
+ /**
35
+ * The file's name, as the document will have it: the last part of the name sent, without
36
+ * control characters (`cleanFileName`). It is the client's: never build a path from it.
37
+ */
38
+ name: string;
39
+ /** What `getOwner` returned, when it returned an owner. */
40
+ owner?: string;
41
+ /** What `getScope` returned, when it returned a scope (a frozen copy). */
42
+ scope?: DocumentScope;
43
+ };
44
+ /**
45
+ * What `prepareUpload` may return for an upload: the document's ID, where to save its body, and
46
+ * the ref of that file for `LargeFilesOptions.originals`. Each is optional. New in 0.9.0.
47
+ */
48
+ export type PreparedUpload = {
49
+ /**
50
+ * The document's ID (`IngestInput.id`), such as the ID of the upload in your database. A
51
+ * document of that ID that is already stored makes the upload fail with a 409 ("exists").
52
+ */
53
+ id?: string;
54
+ /**
55
+ * An absolute path where the body is saved as it arrives, then ingested from (`source`). The
56
+ * file must not be there: it is made new (`"wx"`, mode `uploadFileMode`), and never written
57
+ * through a file or a link already at that name (a 409). Its folder must exist: the handler makes
58
+ * no folder. A link in a folder above it is followed, except below the root of a ref (see
59
+ * `original`). The file is yours once the document is stored: the library never removes it, even
60
+ * when the document is deleted or expires. When the upload fails, it is removed.
61
+ */
62
+ path?: string;
63
+ /**
64
+ * The ref of the saved file for `LargeFilesOptions.originals` (`IngestInput.original`), such as
65
+ * its path under the folder of `fileOriginals`: the store then keeps no copy of it. Give it with
66
+ * `path`: the handler does not make a ref of a path, nor check that the ref names that file.
67
+ * When `path` ends with the ref (`<root>/<ref>`, as `fileOriginals(root)` reads it), a link in a
68
+ * folder below that root, which `fileOriginals` would refuse to read through, fails the upload
69
+ * before its body is read: a 500 "Upload failed (ELOOP).".
70
+ */
71
+ original?: {
72
+ ref: string;
73
+ };
74
+ };
75
+ /** Where `uploadSpool` writes the bodies of uploads, and which ones. New in 0.9.0. */
76
+ export type UploadSpoolOptions = {
77
+ /** The folder of the temporary files. It must exist. Defaults to `os.tmpdir()`. */
78
+ directory?: string;
79
+ /**
80
+ * Bodies whose `Content-Length` is this many bytes or fewer are held in memory, as without a
81
+ * spool; larger ones, and those without a `Content-Length`, are written to a temporary file.
82
+ * Defaults to 1 MiB (1,048,576). 0 writes every body to a file.
83
+ */
84
+ minBytes?: number;
85
+ };
86
+ /**
87
+ * What `onUploaded` may return: nothing, or `{ answer }`, whose fields are added to the upload's
88
+ * answer (the `201` JSON, or `ingest.finish.app`). New in 0.9.0.
89
+ */
90
+ export type UploadedResult = void | {
91
+ answer?: Record<string, unknown>;
92
+ };
93
+ /** What `onRemoved` is told besides the document's ID. New in 0.9.0. */
94
+ export type RemovedContext = {
95
+ request: IncomingMessage;
96
+ owner?: string;
97
+ scope?: DocumentScope;
98
+ };
99
+ /**
100
+ * An error as the upload handler answers it (`uploadAnswerOf`): the HTTP status, the message of
101
+ * the JSON body `{ "error": … }`, the `IngestErrorCode` of a file that could not be read, and how
102
+ * long to wait before trying again (the `Retry-After` header of a 503). New in 0.9.0.
103
+ */
104
+ export type UploadAnswer = {
105
+ status: number;
106
+ error: string;
107
+ code?: IngestErrorCode;
108
+ retryAfterMs?: number;
109
+ };
3
110
  export type DocumentHandlerOptions = {
4
111
  /**
5
112
  * The signed-in user for this request. When set, uploads are owned by that user and other
@@ -9,6 +116,13 @@ export type DocumentHandlerOptions = {
9
116
  * JavaScript that is neither a string nor a finite number (`req.user` in place of `req.user.id`,
10
117
  * say): `{"error":"Upload failed (owner is not a string)."}`.
11
118
  *
119
+ * When the library requires an owner (`requireOwner`), it is required here: the handlers cannot
120
+ * be made without it, and an undefined owner (or null, from JavaScript) is answered 401
121
+ * `{"error":"Sign in first."}` before the body is read, on every route (new in 0.9.0). An empty
122
+ * owner is then a 500 on every route too, without calling the library: `get` answers
123
+ * `{"error":"The file could not be looked up (empty owner)."}`, and `remove`
124
+ * `{"error":"The file could not be deleted (empty owner)."}`.
125
+ *
12
126
  * To refuse a request, throw an error with a `status` (or `statusCode`) between 400 and 599, as
13
127
  * the `http-errors` package makes: the handler answers with that status and the error's message.
14
128
  * Any other error is answered with a 500 that does not repeat its message. It is called before
@@ -16,18 +130,91 @@ export type DocumentHandlerOptions = {
16
130
  */
17
131
  getOwner?: (request: IncomingMessage) => string | undefined | Promise<string | undefined>;
18
132
  /**
19
- * The most bytes of uploads these handlers hold at once: an upload is held whole in memory from
20
- * the moment its body is read until its ingest ends. An upload that would pass it is answered 503
21
- * with `Retry-After` before its body is read, while another upload is held: one upload alone is
22
- * always taken. An upload counts its `Content-Length` (or `maxUploadBytes` without one, then its
23
- * size once read). `Infinity` for no limit (as before 0.8.2). Defaults to twice `maxUploadBytes`.
24
- * New in 0.8.2.
133
+ * The scope of this request, such as [userId, chatId] (`DocumentScope`): uploads are stored in
134
+ * it, and `get` and `remove` find only the documents of the same scope. Called after `getOwner`,
135
+ * before the body is read, on every route. Return undefined for no scope. A scope that is not
136
+ * valid is the server's mistake: a 500 `{"error":"Upload failed (invalid scope)."}`. Throw an
137
+ * error with a status to refuse a request, as for `getOwner`. Required when the library requires
138
+ * a scope (`requireScope`); an undefined scope is then a 500 `{"error":"Upload failed (no scope)."}`.
139
+ * New in 0.9.0.
140
+ */
141
+ getScope?: (request: IncomingMessage) => DocumentScope | undefined | Promise<DocumentScope | undefined>;
142
+ /**
143
+ * Called once an upload is stored, before it is answered: to record the document in your
144
+ * database, say. The `201` is sent, or `ingest.finish` on the event-stream path, once it
145
+ * resolves, with no time limit: bound your own calls. The upload's room
146
+ * (`maxPendingUploadBytes`) is given back, and the body the handler read is freed, before it is
147
+ * called. It is called even when the client went away after the ingest ended (`clientGone`):
148
+ * with the library's own `ingest`, a client that leaves before it ends leaves nothing stored,
149
+ * and no call. Return `{ answer }` to add its fields to the answer: to the `201` JSON (the
150
+ * summary's own fields win), or as `app` in `ingest.finish`. `answer` must be a plain object that
151
+ * JSON can hold.
152
+ *
153
+ * When it throws, or returns an `answer` that is not such an object, the document is deleted
154
+ * (best effort) and the upload is answered as for an error of `ingest`: with the error's status
155
+ * (400 to 599) and message, or a 500 `{"error":"Upload failed."}`. New in 0.9.0.
156
+ */
157
+ onUploaded?: (document: DocumentMeta, context: UploadedContext) => UploadedResult | Promise<UploadedResult>;
158
+ /**
159
+ * Called after `remove` deleted a document (`{ deleted: true }`), before it answers. When it
160
+ * throws, the answer is the error's status and message, or a 500; the document is deleted all
161
+ * the same, and a second `remove` answers `{ deleted: false }` without calling it. New in 0.9.0.
162
+ */
163
+ onRemoved?: (id: string, context: RemovedContext) => void | Promise<void>;
164
+ /**
165
+ * Called for each upload after `getOwner` and `getScope`, before its body is read: returns the
166
+ * document's `id`, a `path` to save the body to, and the `original` ref of that file (see
167
+ * `PreparedUpload`), or nothing. With a `path`, the body is written there as it arrives (under
168
+ * the same limits: `maxUploadBytes`, `uploadIdleTimeoutMs`), then ingested from the file, which
169
+ * stays once the document is stored and is removed when the upload fails, whatever the reason
170
+ * (a process that is killed cannot remove it).
171
+ * Such an upload is not held in memory, and takes no room of `maxPendingUploadBytes` or
172
+ * `maxSpooledUploadBytes`. Throw an error with a status to refuse the upload, as for `getOwner`.
173
+ * The upload can still fail after it (no room, too large…): choose here, record in `onUploaded`.
174
+ * New in 0.9.0.
175
+ */
176
+ prepareUpload?: (request: IncomingMessage, info: PrepareUploadInfo) => PreparedUpload | void | Promise<PreparedUpload | void>;
177
+ /**
178
+ * Writes the bodies of uploads to temporary files, instead of holding them in memory while they
179
+ * are ingested: those over `minBytes` (1 MiB), and those without a `Content-Length`. Each file is
180
+ * made new in `directory` (`os.tmpdir()`), with a random name and mode 0o600, ingested from its
181
+ * path, and removed once the ingest ends, whatever the end. `true` for the defaults. Defaults to
182
+ * false. New in 0.9.0.
183
+ */
184
+ uploadSpool?: boolean | UploadSpoolOptions;
185
+ /**
186
+ * The most bytes of the spool's files (`uploadSpool`) at once. An upload that would pass it is
187
+ * answered 503 with `Retry-After`, before its body is read, as for `maxPendingUploadBytes`. A
188
+ * disk that is full is answered 503 with `Retry-After` too. `Infinity` for no limit. Defaults to
189
+ * ten times `maxUploadBytes`. New in 0.9.0.
190
+ */
191
+ maxSpooledUploadBytes?: number;
192
+ /**
193
+ * The mode of the files saved to the `path` of `prepareUpload`, minus the umask of the process.
194
+ * Defaults to 0o600: only the server's user reads them. 0o640 lets a group read them (pods of
195
+ * another user that share the volume through a group). The spool's files are always 0o600. New
196
+ * in 0.9.0.
197
+ */
198
+ uploadFileMode?: number;
199
+ /**
200
+ * The most bytes of uploads these handlers hold in memory at once: an upload is held whole in
201
+ * memory from the moment its body is read until its ingest ends. An upload that would pass it is
202
+ * answered 503 with `Retry-After` before its body is read, while another upload is held: one
203
+ * upload alone is always taken. An upload counts its `Content-Length` (or `maxUploadBytes`
204
+ * without one, then its size once read). Bodies written to a file (`prepareUpload`'s `path`,
205
+ * `uploadSpool`) do not count, since 0.9.0. `Infinity` for no limit (as before 0.8.2). Defaults
206
+ * to twice `maxUploadBytes`. New in 0.8.2.
25
207
  */
26
208
  maxPendingUploadBytes?: number;
27
209
  /**
28
210
  * An upload whose body stops arriving for this many milliseconds is answered 408, its connection
29
211
  * closed and its room given back: a client that sends its headers and then nothing, or stalls,
30
- * holds its room no longer. 0 or `Infinity` for no limit. Defaults to 30,000. New in 0.8.2.
212
+ * holds its room no longer. Since 0.9.0 it also bounds each call to the disk for a body written
213
+ * to a file (the check of its folders for a link, its opening, each write, its closing): a volume
214
+ * that stops answering fails the upload with a 500 "Upload failed (ETIMEDOUT).", and its room is
215
+ * given back. The removal of the file is waited for no longer either: past it, the removal goes
216
+ * on alone, and the upload is answered all the same. 0 or `Infinity` for no limit. Defaults to
217
+ * 30,000. New in 0.8.2.
31
218
  */
32
219
  uploadIdleTimeoutMs?: number;
33
220
  };
@@ -40,20 +227,45 @@ export type DocumentHandlers = {
40
227
  /**
41
228
  * POST: the body is the raw file. Name it with an `X-File-Name` header (URI-encoded) or `?name=`.
42
229
  * A file that cannot be read is answered with a 422 and the reason. A store that fails while it
43
- * writes is answered with a 500 that does not repeat its message, unless its error has a status.
230
+ * writes is answered with a 503 and `Retry-After`. Any other error is answered as
231
+ * `uploadAnswerOf` says. The body is held in memory, or, with the `path` of `prepareUpload` or
232
+ * with `uploadSpool`, written to a file as it arrives and ingested from its path (new in 0.9.0).
44
233
  *
45
234
  * When the request's `Accept` header names `text/event-stream` (not with `q=0`, nor only through a
46
- * wildcard), a file that passes the checks of the name, the owner and the size (which still
47
- * answer with an HTTP error) is answered with a 200 and Server-Sent Events: the ingest's events (`ingest.start`, `ingest.progress`…), ending
48
- * with `ingest.finish`, whose data is the summary a 201 would hold, or `ingest.error`, whose data
49
- * is `{ error: { message, status } }` with the message and status a JSON answer would have.
235
+ * wildcard), a file that passes the checks of the name, the owner, the scope and the size (which
236
+ * still answer with an HTTP error) is answered with a 200 and Server-Sent Events: the ingest's
237
+ * events (`ingest.start`, `ingest.progress`…), ending with `ingest.finish`, whose data is the
238
+ * summary a 201 would hold (and `onUploaded`'s answer in `app`), or `ingest.error`, whose data is
239
+ * `{ error: { message, status } }` with the message and status a JSON answer would have, the
240
+ * `code` of a refused file, the `name` of a store that failed or of a scope error (with its
241
+ * `code`), and the `kind` of a model's error, for `errorInfoOf`.
50
242
  */
51
243
  upload(request: IncomingMessage, response: ServerResponse): Promise<void>;
52
- /** GET: the document's summary, or 404. */
244
+ /** GET: the document's summary, or 404. With `getScope`, only a document of the request's scope. */
53
245
  get(request: IncomingMessage, response: ServerResponse, id: string): Promise<void>;
54
- /** DELETE: removes the document. Responds `{ deleted: boolean }`. */
246
+ /** DELETE: removes the document, then calls `onRemoved`. Responds `{ deleted: boolean }`. */
55
247
  remove(request: IncomingMessage, response: ServerResponse, id: string): Promise<void>;
56
248
  };
249
+ /**
250
+ * How the upload handler answers an error, for a route of your own that ingests files and should
251
+ * answer as it does:
252
+ * - an `IngestError` (a file that could not be read): its status (422; 413, 409, or 503 for "busy"
253
+ * with its `retryAfterMs`), its message and its `code`. Only an `IngestError` is a 422. An
254
+ * "unreadable" file is told in fixed words, as `errorInfoOf` tells it: its own message is a
255
+ * parser's, or that of an extractor of your own;
256
+ * - a `DocumentStoreError`: 503, its message (which names no path or host), and 5 s to wait;
257
+ * - a `DocumentScopeError` "scope_missing": 404 "This chat no longer exists."; the other codes
258
+ * are the server's mistake: 500 "Upload failed.";
259
+ * - a model's error (`ModelRequestError`): the status and words of `errorInfoOf`, never the
260
+ * provider's own;
261
+ * - any other error with a `status` (or `statusCode`) from 400 to 599: that status and its message;
262
+ * - a Node system error (a `code` and a `syscall`): 500 "Upload failed (ENOSPC).", or "Upload
263
+ * failed." when its code does not read as one (capitals, digits and "_"), as a host does;
264
+ * - anything else: 500 "Upload failed.", without its message, which may name a host.
265
+ *
266
+ * It reads the errors of every copy of the package, and never throws. New in 0.9.0.
267
+ */
268
+ export declare function uploadAnswerOf(error: unknown): UploadAnswer;
57
269
  /**
58
270
  * Upload, get, and delete handlers for any Node HTTP server (node:http, Express, Fastify via
59
271
  * `request.raw`/`reply.raw`, Next.js API routes on the Node runtime). Responses are JSON, except an upload that asks for
@@ -1 +1 @@
1
- {"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../src/node/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGjE,OAAO,EAAmD,KAAK,UAAU,EAAE,MAAM,eAAe,CAAC;AAKjG,MAAM,MAAM,sBAAsB,GAAG;IACnC;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAC1F;;;;;;;OAOG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;;;;;;;;OAUG;IACH,MAAM,CAAC,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,cAAc,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