nish 0.35.0 → 0.37.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.
- package/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/compress/gzip.ts
CHANGED
|
@@ -25,9 +25,11 @@
|
|
|
25
25
|
* flag bit is `GZIP_ERR_HEADER`; `FEXTRA`, `FNAME`, `FCOMMENT` and `FHCRC` are
|
|
26
26
|
* read and skipped, the header CRC checked. A wrong CRC-32 is `GZIP_ERR_CRC`,
|
|
27
27
|
* a wrong length `GZIP_ERR_SIZE`. Members may follow one another (§2.2) and
|
|
28
|
-
* are decompressed one after another into the same output
|
|
29
|
-
* last member
|
|
30
|
-
*
|
|
28
|
+
* are decompressed one after another into the same output. Bytes after the
|
|
29
|
+
* last member are read as another member's header, so trailing garbage is
|
|
30
|
+
* refused, not ignored: two or more bytes without the `1f 8b` magic are
|
|
31
|
+
* `GZIP_ERR_HEADER`, while a single byte, or fewer than 10 that start with
|
|
32
|
+
* `1f 8b`, is `DEFLATE_ERR_TRUNCATED`. The `limit` bounds the whole output.
|
|
31
33
|
*
|
|
32
34
|
* **Streaming.** `GzipWriter` compresses a body that arrives in pieces and
|
|
33
35
|
* `GzipReader` decompresses one: see each class.
|
|
@@ -173,10 +175,16 @@ const gzipReadHeader = (crc: Crc32, src: u8[], p: i32, end: i32): i32 => {
|
|
|
173
175
|
* allocated once and reused by every call. Not shared between threads.
|
|
174
176
|
*/
|
|
175
177
|
export class GzipCompressor {
|
|
176
|
-
readonly deflater: Deflater
|
|
177
|
-
readonly crc: Crc32
|
|
178
|
+
private readonly deflater: Deflater
|
|
179
|
+
private readonly crc: Crc32
|
|
178
180
|
|
|
179
|
-
/**
|
|
181
|
+
/**
|
|
182
|
+
* A compressor at `level`, 0 to 9 (`DEFLATE_FAST`, `DEFLATE_DEFAULT`, ...);
|
|
183
|
+
* another level panics.
|
|
184
|
+
*
|
|
185
|
+
* @param level How hard to compress, `DEFLATE_STORE` (0) to
|
|
186
|
+
* `DEFLATE_BEST` (9); 1 and 9 also set the header's `XFL`.
|
|
187
|
+
*/
|
|
180
188
|
constructor(level: i32) {
|
|
181
189
|
this.deflater = new Deflater(level)
|
|
182
190
|
this.crc = new Crc32()
|
|
@@ -187,6 +195,15 @@ export class GzipCompressor {
|
|
|
187
195
|
* member, and answers where it ends, or `DEFLATE_NO_ROOM` (-1) when `out`
|
|
188
196
|
* cannot hold it; `compressBound(len)` bytes always can. A window outside
|
|
189
197
|
* its array panics. Allocates nothing.
|
|
198
|
+
*
|
|
199
|
+
* @param src The array that holds the data to compress.
|
|
200
|
+
* @param off Where in `src` the data starts.
|
|
201
|
+
* @param len How many bytes of data to compress.
|
|
202
|
+
* @param out The caller's array the member is written into.
|
|
203
|
+
* @param at Where in `out` the member starts.
|
|
204
|
+
* @returns The index in `out` one past the member's last byte, so the
|
|
205
|
+
* member is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
|
|
206
|
+
* bytes of `out` from `at` may have been overwritten and mean nothing.
|
|
190
207
|
*/
|
|
191
208
|
compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
|
|
192
209
|
gzipWindow("GzipCompressor.compress", src, off, len)
|
|
@@ -205,7 +222,14 @@ export class GzipCompressor {
|
|
|
205
222
|
}
|
|
206
223
|
}
|
|
207
224
|
|
|
208
|
-
/**
|
|
225
|
+
/**
|
|
226
|
+
* The most bytes `GzipFixedCompressor.compress` writes for `n` bytes of input.
|
|
227
|
+
*
|
|
228
|
+
* @param n The length of the input, in bytes; a negative `n` answers the
|
|
229
|
+
* bound of 0.
|
|
230
|
+
* @returns A size in bytes, not a position: a `room` of this many always
|
|
231
|
+
* holds the member.
|
|
232
|
+
*/
|
|
209
233
|
export const gzipFixedBound = (n: i32): i32 => fixedDeflateBound(n) + GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE
|
|
210
234
|
|
|
211
235
|
/**
|
|
@@ -214,8 +238,8 @@ export const gzipFixedBound = (n: i32): i32 => fixedDeflateBound(n) + GZIP_HEADE
|
|
|
214
238
|
* depends on its input alone. Not shared between threads.
|
|
215
239
|
*/
|
|
216
240
|
export class GzipFixedCompressor {
|
|
217
|
-
readonly deflater: FixedDeflater
|
|
218
|
-
readonly crc: Crc32
|
|
241
|
+
private readonly deflater: FixedDeflater
|
|
242
|
+
private readonly crc: Crc32
|
|
219
243
|
|
|
220
244
|
/** Builds the tables. */
|
|
221
245
|
constructor() {
|
|
@@ -230,6 +254,18 @@ export class GzipFixedCompressor {
|
|
|
230
254
|
* will take stops early on an input that does not shrink.
|
|
231
255
|
* `gzipFixedBound(len)` always fits. A window outside its array panics.
|
|
232
256
|
* Allocates nothing.
|
|
257
|
+
*
|
|
258
|
+
* @param src The array that holds the data to compress.
|
|
259
|
+
* @param off Where in `src` the data starts.
|
|
260
|
+
* @param len How many bytes of data to compress.
|
|
261
|
+
* @param out The caller's array the member is written into.
|
|
262
|
+
* @param at Where in `out` the member starts.
|
|
263
|
+
* @param room How many bytes of `out` from `at` the member may use; the
|
|
264
|
+
* window `out[at .. at + room)` must lie inside `out`.
|
|
265
|
+
* @returns The index in `out` one past the member's last byte, so the
|
|
266
|
+
* member is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
|
|
267
|
+
* bytes of `out[at .. at + room)` may have been overwritten and mean
|
|
268
|
+
* nothing.
|
|
233
269
|
*/
|
|
234
270
|
compress(src: u8[], off: i32, len: i32, out: u8[], at: i32, room: i32): i32 {
|
|
235
271
|
gzipWindow("GzipFixedCompressor.compress", src, off, len)
|
|
@@ -260,8 +296,8 @@ export class GzipFixedCompressor {
|
|
|
260
296
|
* Not shared between threads.
|
|
261
297
|
*/
|
|
262
298
|
export class GzipDecompressor {
|
|
263
|
-
readonly inflater: Inflater
|
|
264
|
-
readonly crc: Crc32
|
|
299
|
+
private readonly inflater: Inflater
|
|
300
|
+
private readonly crc: Crc32
|
|
265
301
|
|
|
266
302
|
/** Builds the tables. */
|
|
267
303
|
constructor() {
|
|
@@ -277,6 +313,24 @@ export class GzipDecompressor {
|
|
|
277
313
|
* `DEFLATE_ERR_TRUNCATED` or another `DEFLATE_ERR_*` for a malformed
|
|
278
314
|
* stream. An empty input is truncated. A window outside its array, or a
|
|
279
315
|
* negative `limit`, panics. Allocates nothing.
|
|
316
|
+
*
|
|
317
|
+
* @param src The array that holds the gzip data, one member or several.
|
|
318
|
+
* @param off Where in `src` the first member starts.
|
|
319
|
+
* @param len How many bytes the members are; every one must belong to a
|
|
320
|
+
* member. Bytes after the last are read as another member's header:
|
|
321
|
+
* two or more without the `1f 8b` magic are `GZIP_ERR_HEADER`, and a
|
|
322
|
+
* single byte, or fewer than 10 that start with `1f 8b`, is
|
|
323
|
+
* `DEFLATE_ERR_TRUNCATED`; a header that parses is decoded as a member.
|
|
324
|
+
* @param out The caller's array the data is written into, the members'
|
|
325
|
+
* one after another.
|
|
326
|
+
* @param at Where in `out` the data starts.
|
|
327
|
+
* @param limit The most bytes of data to accept over all the members; a
|
|
328
|
+
* longer body is `DEFLATE_ERR_OUTPUT_LIMIT`, as is one that reaches the
|
|
329
|
+
* end of `out`.
|
|
330
|
+
* @returns The index in `out` one past the data's last byte, so the data
|
|
331
|
+
* is `out[at .. result)`; or one of the negative codes above, after
|
|
332
|
+
* which the bytes of `out` from `at` may have been overwritten and mean
|
|
333
|
+
* nothing.
|
|
280
334
|
*/
|
|
281
335
|
decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 {
|
|
282
336
|
gzipWindow("GzipDecompressor.decompress", src, off, len)
|
|
@@ -327,11 +381,32 @@ export class GzipDecompressor {
|
|
|
327
381
|
* `GzipCompressor.compress` at `level`, building the compressor for this call
|
|
328
382
|
* alone (about 330 KB, allocated and filled): keep a `GzipCompressor` for
|
|
329
383
|
* anything repeated.
|
|
384
|
+
*
|
|
385
|
+
* @param src The array that holds the data to compress.
|
|
386
|
+
* @param off Where in `src` the data starts.
|
|
387
|
+
* @param len How many bytes of data to compress.
|
|
388
|
+
* @param out The caller's array the member is written into.
|
|
389
|
+
* @param at Where in `out` the member starts.
|
|
390
|
+
* @param level How hard to compress, 0 to 9; another level panics.
|
|
391
|
+
* @returns The index in `out` one past the member's last byte, or
|
|
392
|
+
* `DEFLATE_NO_ROOM`, as `GzipCompressor.compress` answers.
|
|
330
393
|
*/
|
|
331
394
|
export const gzipCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, level: i32): i32 =>
|
|
332
395
|
new GzipCompressor(level).compress(src, off, len, out, at)
|
|
333
396
|
|
|
334
|
-
/**
|
|
397
|
+
/**
|
|
398
|
+
* `GzipDecompressor.decompress`, building the decompressor for this call
|
|
399
|
+
* alone.
|
|
400
|
+
*
|
|
401
|
+
* @param src The array that holds the gzip data, one member or several.
|
|
402
|
+
* @param off Where in `src` the first member starts.
|
|
403
|
+
* @param len How many bytes the members are.
|
|
404
|
+
* @param out The caller's array the data is written into.
|
|
405
|
+
* @param at Where in `out` the data starts.
|
|
406
|
+
* @param limit The most bytes of data to accept over all the members.
|
|
407
|
+
* @returns The index in `out` one past the data's last byte, or a negative
|
|
408
|
+
* code, as `GzipDecompressor.decompress` answers.
|
|
409
|
+
*/
|
|
335
410
|
export const gzipDecompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 =>
|
|
336
411
|
new GzipDecompressor().decompress(src, off, len, out, at, limit)
|
|
337
412
|
|
|
@@ -346,21 +421,27 @@ export const gzipDecompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32
|
|
|
346
421
|
* once; `start` begins the next body.
|
|
347
422
|
*/
|
|
348
423
|
export class GzipWriter {
|
|
349
|
-
readonly deflater: Deflater
|
|
350
|
-
readonly crc: Crc32
|
|
424
|
+
private readonly deflater: Deflater
|
|
425
|
+
private readonly crc: Crc32
|
|
351
426
|
/** The history and the pending input. */
|
|
352
|
-
readonly window: u8[]
|
|
427
|
+
private readonly window: u8[]
|
|
353
428
|
/** Where the pending input starts in `window`, and where it ends. */
|
|
354
|
-
pending: i32
|
|
355
|
-
fill: i32
|
|
429
|
+
private pending: i32
|
|
430
|
+
private fill: i32
|
|
356
431
|
/** The CRC-32 and length of every byte written so far. */
|
|
357
|
-
checksum: u32
|
|
358
|
-
size: u32
|
|
432
|
+
private checksum: u32
|
|
433
|
+
private size: u32
|
|
359
434
|
/** Whether the header has been written, and whether `finish` has. */
|
|
360
|
-
started: boolean
|
|
361
|
-
finished: boolean
|
|
435
|
+
private started: boolean
|
|
436
|
+
private finished: boolean
|
|
362
437
|
|
|
363
|
-
/**
|
|
438
|
+
/**
|
|
439
|
+
* A writer at `level`, 0 to 9; another level panics. It is ready for a
|
|
440
|
+
* first body without a call to `start`.
|
|
441
|
+
*
|
|
442
|
+
* @param level How hard to compress, `DEFLATE_STORE` (0) to
|
|
443
|
+
* `DEFLATE_BEST` (9).
|
|
444
|
+
*/
|
|
364
445
|
constructor(level: i32) {
|
|
365
446
|
this.deflater = new Deflater(level)
|
|
366
447
|
this.crc = new Crc32()
|
|
@@ -395,6 +476,11 @@ export class GzipWriter {
|
|
|
395
476
|
* call compresses once per 64 KB pending: `deflateBound` of all of it,
|
|
396
477
|
* with the constant part of `deflateBound` again for each compression
|
|
397
478
|
* after the first.
|
|
479
|
+
*
|
|
480
|
+
* @param len The length of the next `write`, in bytes; 0 for a `flush` or
|
|
481
|
+
* `finish`.
|
|
482
|
+
* @returns A size in bytes, not a position: the room `out` must have from
|
|
483
|
+
* `at` for that call not to answer `DEFLATE_NO_ROOM`.
|
|
398
484
|
*/
|
|
399
485
|
bound(len: i32): i32 {
|
|
400
486
|
const drains: i32 = (len >> 16) + 1
|
|
@@ -402,7 +488,7 @@ export class GzipWriter {
|
|
|
402
488
|
}
|
|
403
489
|
|
|
404
490
|
/** Writes the header into `out` at `at` if it has not been; answers the new position. */
|
|
405
|
-
header(out: u8[], at: i32): i32 {
|
|
491
|
+
private header(out: u8[], at: i32): i32 {
|
|
406
492
|
if (this.started) {
|
|
407
493
|
return at
|
|
408
494
|
}
|
|
@@ -416,7 +502,7 @@ export class GzipWriter {
|
|
|
416
502
|
* then slides the window so that the last 32 KB stay as history. Answers
|
|
417
503
|
* the new position, or -1.
|
|
418
504
|
*/
|
|
419
|
-
drain(out: u8[], at: i32, final: boolean): i32 {
|
|
505
|
+
private drain(out: u8[], at: i32, final: boolean): i32 {
|
|
420
506
|
const d: Deflater = this.deflater
|
|
421
507
|
const end: i32 = d.compressRange(
|
|
422
508
|
this.window,
|
|
@@ -448,6 +534,16 @@ export class GzipWriter {
|
|
|
448
534
|
* pending), or `DEFLATE_NO_ROOM` when `out` has less than `bound(len)`
|
|
449
535
|
* bytes from `at`. Writing after `finish` panics, as does a window outside
|
|
450
536
|
* its array.
|
|
537
|
+
*
|
|
538
|
+
* @param src The array that holds the next piece of the body; its bytes
|
|
539
|
+
* are copied, so the caller may reuse it once `write` returns.
|
|
540
|
+
* @param off Where in `src` the piece starts.
|
|
541
|
+
* @param len How many bytes the piece is.
|
|
542
|
+
* @param out The caller's array the output is written into.
|
|
543
|
+
* @param at Where in `out` the output starts.
|
|
544
|
+
* @returns The index in `out` one past the last byte written, so the
|
|
545
|
+
* caller sends `out[at .. result)`, which may be empty; or
|
|
546
|
+
* `DEFLATE_NO_ROOM`, when nothing was taken and nothing written.
|
|
451
547
|
*/
|
|
452
548
|
write(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
|
|
453
549
|
gzipWindow("GzipWriter.write", src, off, len)
|
|
@@ -487,6 +583,13 @@ export class GzipWriter {
|
|
|
487
583
|
* Compresses everything pending and ends with an empty stored block, so a
|
|
488
584
|
* reader can decode every byte written so far (zlib's `Z_SYNC_FLUSH`).
|
|
489
585
|
* Answers the new end of `out`, or `DEFLATE_NO_ROOM` below `bound(0)`.
|
|
586
|
+
* Flushing after `finish` panics, as does a window outside `out`.
|
|
587
|
+
*
|
|
588
|
+
* @param out The caller's array the output is written into.
|
|
589
|
+
* @param at Where in `out` the output starts.
|
|
590
|
+
* @returns The index in `out` one past the last byte written, so the
|
|
591
|
+
* caller sends `out[at .. result)`; or `DEFLATE_NO_ROOM`, when nothing
|
|
592
|
+
* was written.
|
|
490
593
|
*/
|
|
491
594
|
flush(out: u8[], at: i32): i32 {
|
|
492
595
|
gzipWindow("GzipWriter.flush", out, at, 0)
|
|
@@ -510,7 +613,15 @@ export class GzipWriter {
|
|
|
510
613
|
/**
|
|
511
614
|
* Compresses everything pending as the final block and writes the
|
|
512
615
|
* trailer; answers the new end of `out`, or `DEFLATE_NO_ROOM` below
|
|
513
|
-
* `bound(0)`. `start` begins another body
|
|
616
|
+
* `bound(0)`. `start` begins another body; until then `write`, `flush`
|
|
617
|
+
* and `finish` panic, as does a window outside `out`.
|
|
618
|
+
*
|
|
619
|
+
* @param out The caller's array the output is written into.
|
|
620
|
+
* @param at Where in `out` the output starts.
|
|
621
|
+
* @returns The index in `out` one past the trailer's last byte, so the
|
|
622
|
+
* caller sends `out[at .. result)` as the end of the body; or
|
|
623
|
+
* `DEFLATE_NO_ROOM`, when nothing was written and the body is not yet
|
|
624
|
+
* finished.
|
|
514
625
|
*/
|
|
515
626
|
finish(out: u8[], at: i32): i32 {
|
|
516
627
|
gzipWindow("GzipWriter.finish", out, at, 0)
|
|
@@ -549,34 +660,41 @@ export class GzipWriter {
|
|
|
549
660
|
* once; `start` begins the next body.
|
|
550
661
|
*/
|
|
551
662
|
export class GzipReader {
|
|
552
|
-
readonly inflater: Inflater
|
|
553
|
-
readonly crc: Crc32
|
|
663
|
+
private readonly inflater: Inflater
|
|
664
|
+
private readonly crc: Crc32
|
|
554
665
|
/** Input not yet decoded. */
|
|
555
|
-
readonly input: u8[]
|
|
666
|
+
private readonly input: u8[]
|
|
556
667
|
/** The decoded window: history from `windowStart`, unread output from `readPos` to `writePos`. */
|
|
557
|
-
readonly output: u8[]
|
|
558
|
-
inStart: i32
|
|
559
|
-
inEnd: i32
|
|
560
|
-
readPos: i32
|
|
561
|
-
writePos: i32
|
|
668
|
+
private readonly output: u8[]
|
|
669
|
+
private inStart: i32
|
|
670
|
+
private inEnd: i32
|
|
671
|
+
private readPos: i32
|
|
672
|
+
private writePos: i32
|
|
562
673
|
/** Where the current member's output began, for its distances. */
|
|
563
|
-
histStart: i32
|
|
674
|
+
private histStart: i32
|
|
564
675
|
/** Where the bytes not yet checksummed start. */
|
|
565
|
-
crcPos: i32
|
|
676
|
+
private crcPos: i32
|
|
566
677
|
/** 0: a member header next; 1: inside a member's stream; 2: its trailer; 3: done; < 0: failed. */
|
|
567
|
-
phase: i32
|
|
678
|
+
private phase: i32
|
|
568
679
|
/** Whether more input may come. */
|
|
569
|
-
more: boolean
|
|
680
|
+
private more: boolean
|
|
570
681
|
/** The member's running CRC-32 and length, and the body's total length. */
|
|
571
|
-
checksum: u32
|
|
572
|
-
size: u32
|
|
573
|
-
total: i64
|
|
682
|
+
private checksum: u32
|
|
683
|
+
private size: u32
|
|
684
|
+
private total: i64
|
|
574
685
|
/** The most decoded bytes `read` hands out in all. */
|
|
575
|
-
readonly limit: i64
|
|
686
|
+
private readonly limit: i64
|
|
576
687
|
/** Whether a member has been read; another may follow. */
|
|
577
|
-
members: i32
|
|
688
|
+
private members: i32
|
|
578
689
|
|
|
579
|
-
/**
|
|
690
|
+
/**
|
|
691
|
+
* A reader that refuses more than `limit` decoded bytes in all; a negative
|
|
692
|
+
* `limit` panics. It is ready for a first body without a call to `start`.
|
|
693
|
+
*
|
|
694
|
+
* @param limit The most decoded bytes one body may have, over all its
|
|
695
|
+
* members; past it, `read` answers `DEFLATE_ERR_OUTPUT_LIMIT`. `start`
|
|
696
|
+
* keeps it.
|
|
697
|
+
*/
|
|
580
698
|
constructor(limit: i64) {
|
|
581
699
|
if (limit < 0) {
|
|
582
700
|
panic("GzipReader: a negative limit")
|
|
@@ -616,24 +734,51 @@ export class GzipReader {
|
|
|
616
734
|
this.members = 0
|
|
617
735
|
}
|
|
618
736
|
|
|
619
|
-
/**
|
|
737
|
+
/**
|
|
738
|
+
* The buffer to copy input into, from `inputEnd()` for at most
|
|
739
|
+
* `inputRoom()` bytes; then `commit` how many were copied. It may first
|
|
740
|
+
* move the input not yet decoded to the front, so read `inputEnd()` after
|
|
741
|
+
* this call.
|
|
742
|
+
*
|
|
743
|
+
* @returns The reader's own 16 KB input buffer, the same array every call
|
|
744
|
+
* and every body: not the caller's to keep, and only its bytes from
|
|
745
|
+
* `inputEnd()` are the caller's to write.
|
|
746
|
+
*/
|
|
620
747
|
inputBuffer(): u8[] {
|
|
621
748
|
this.compact()
|
|
622
749
|
return this.input
|
|
623
750
|
}
|
|
624
751
|
|
|
625
|
-
/**
|
|
752
|
+
/**
|
|
753
|
+
* Where new input goes in `inputBuffer()`.
|
|
754
|
+
*
|
|
755
|
+
* @returns An index into `inputBuffer()`. `inputBuffer()`, `inputRoom()`,
|
|
756
|
+
* `commit` and `feed` may change it, so read it after them.
|
|
757
|
+
*/
|
|
626
758
|
inputEnd(): i32 {
|
|
627
759
|
return this.inEnd
|
|
628
760
|
}
|
|
629
761
|
|
|
630
|
-
/**
|
|
762
|
+
/**
|
|
763
|
+
* How many bytes `inputBuffer()` has room for, from `inputEnd()`. Like
|
|
764
|
+
* `inputBuffer()`, it may first move the input not yet decoded to the
|
|
765
|
+
* front.
|
|
766
|
+
*
|
|
767
|
+
* @returns A count of bytes; 0 when the buffer is full of input not yet
|
|
768
|
+
* decoded, which `read` must use up before more fits.
|
|
769
|
+
*/
|
|
631
770
|
inputRoom(): i32 {
|
|
632
771
|
this.compact()
|
|
633
772
|
return toI32(this.input.length) - this.inEnd
|
|
634
773
|
}
|
|
635
774
|
|
|
636
|
-
/**
|
|
775
|
+
/**
|
|
776
|
+
* Takes `n` bytes copied into `inputBuffer()` at `inputEnd()`; more than
|
|
777
|
+
* `inputRoom()` panics.
|
|
778
|
+
*
|
|
779
|
+
* @param n How many bytes were copied, from 0 to `inputRoom()`; a
|
|
780
|
+
* negative `n` panics.
|
|
781
|
+
*/
|
|
637
782
|
commit(n: i32): void {
|
|
638
783
|
if (n < 0 || n > toI32(this.input.length) - this.inEnd) {
|
|
639
784
|
panic("GzipReader.commit: more bytes than the buffer has room for")
|
|
@@ -641,7 +786,17 @@ export class GzipReader {
|
|
|
641
786
|
this.inEnd += n
|
|
642
787
|
}
|
|
643
788
|
|
|
644
|
-
/**
|
|
789
|
+
/**
|
|
790
|
+
* Copies as much of `src[off .. off + len)` as fits and answers how much.
|
|
791
|
+
* A window outside `src` panics.
|
|
792
|
+
*
|
|
793
|
+
* @param src The array that holds the next input; its bytes are copied,
|
|
794
|
+
* so the caller may reuse it once `feed` returns.
|
|
795
|
+
* @param off Where in `src` the input starts.
|
|
796
|
+
* @param len How many bytes of input are offered.
|
|
797
|
+
* @returns How many bytes were taken, from 0 to `len`: the caller offers
|
|
798
|
+
* the rest again after `read` has used some input up.
|
|
799
|
+
*/
|
|
645
800
|
feed(src: u8[], off: i32, len: i32): i32 {
|
|
646
801
|
gzipWindow("GzipReader.feed", src, off, len)
|
|
647
802
|
const room: i32 = this.inputRoom()
|
|
@@ -657,7 +812,7 @@ export class GzipReader {
|
|
|
657
812
|
}
|
|
658
813
|
|
|
659
814
|
/** Moves the undecoded input to the front of its buffer. */
|
|
660
|
-
compact(): void {
|
|
815
|
+
private compact(): void {
|
|
661
816
|
if (this.inStart > 0) {
|
|
662
817
|
const n: i32 = this.inEnd - this.inStart
|
|
663
818
|
copyBytes(this.input, 0, this.input, this.inStart, n)
|
|
@@ -667,7 +822,7 @@ export class GzipReader {
|
|
|
667
822
|
}
|
|
668
823
|
|
|
669
824
|
/** Adds the decoded bytes not yet checksummed to the member's CRC-32 and length. */
|
|
670
|
-
sum(): void {
|
|
825
|
+
private sum(): void {
|
|
671
826
|
const n: i32 = this.writePos - this.crcPos
|
|
672
827
|
if (n > 0) {
|
|
673
828
|
this.checksum = this.crc.update(this.checksum, this.output, this.crcPos, n)
|
|
@@ -677,7 +832,7 @@ export class GzipReader {
|
|
|
677
832
|
}
|
|
678
833
|
|
|
679
834
|
/** Slides the decoded window down once the unread output has been read and the buffer is past half. */
|
|
680
|
-
slide(): void {
|
|
835
|
+
private slide(): void {
|
|
681
836
|
if (this.readPos === this.writePos && this.writePos > GZIP_READER_WINDOW) {
|
|
682
837
|
this.sum()
|
|
683
838
|
const keep: i32 = GZIP_READER_WINDOW
|
|
@@ -691,7 +846,7 @@ export class GzipReader {
|
|
|
691
846
|
}
|
|
692
847
|
|
|
693
848
|
/** Decodes as far as the input and the output buffer allow; answers 0 or a final code. */
|
|
694
|
-
advance(): i32 {
|
|
849
|
+
private advance(): i32 {
|
|
695
850
|
while (true) {
|
|
696
851
|
const avail: i32 = this.inEnd - this.inStart
|
|
697
852
|
if (this.phase === 0) {
|
|
@@ -773,7 +928,7 @@ export class GzipReader {
|
|
|
773
928
|
}
|
|
774
929
|
|
|
775
930
|
/** The room `input` has without compacting. */
|
|
776
|
-
inputRoomRaw(): i32 {
|
|
931
|
+
private inputRoomRaw(): i32 {
|
|
777
932
|
return toI32(this.input.length) - this.inEnd
|
|
778
933
|
}
|
|
779
934
|
|
|
@@ -782,6 +937,15 @@ export class GzipReader {
|
|
|
782
937
|
* many (at least 1; `max` is at least 1), or `GZIP_NEED_INPUT` when it needs more input first,
|
|
783
938
|
* `GZIP_END` once the body has ended, or a negative error, after which it
|
|
784
939
|
* answers that error again until `start`. A window outside `out` panics.
|
|
940
|
+
*
|
|
941
|
+
* @param out The caller's array the decoded bytes are copied into.
|
|
942
|
+
* @param at Where in `out` they start.
|
|
943
|
+
* @param max The most bytes to copy, at least 1; `out[at .. at + max)`
|
|
944
|
+
* must lie inside `out`.
|
|
945
|
+
* @returns A count, not a position: the bytes are `out[at .. at + result)`.
|
|
946
|
+
* Or `GZIP_NEED_INPUT` (0), `GZIP_END` (-100), or a negative error code
|
|
947
|
+
* (`GZIP_ERR_*`, `DEFLATE_ERR_TRUNCATED`, `DEFLATE_ERR_OUTPUT_LIMIT` or
|
|
948
|
+
* another `DEFLATE_ERR_*`), which is final.
|
|
785
949
|
*/
|
|
786
950
|
read(out: u8[], at: i32, max: i32): i32 {
|
|
787
951
|
gzipWindow("GzipReader.read", out, at, max)
|
package/std/compress/zlib.ts
CHANGED
|
@@ -11,8 +11,10 @@
|
|
|
11
11
|
* const end: i32 = z.compress(src, 0, n, out, 0); // -1: no room
|
|
12
12
|
* const got: i32 = new ZlibDecompressor().decompress(out, 0, end, back, 0, n); // < 0: an error
|
|
13
13
|
*
|
|
14
|
-
* The objects allocate their tables once and nothing per
|
|
15
|
-
* functions build one for a single call.
|
|
14
|
+
* **Allocated once.** The objects allocate their tables once and nothing per
|
|
15
|
+
* call; the free functions build one for a single call.
|
|
16
|
+
*
|
|
17
|
+
* **What is written and what is refused.** The header written is `0x78` and
|
|
16
18
|
* the `FLEVEL` of the level, with no preset dictionary. Reading refuses a
|
|
17
19
|
* method other than 8, a window over 32 KB, a header whose check fails
|
|
18
20
|
* (`ZLIB_ERR_HEADER`) and a preset dictionary (`ZLIB_ERR_DICTIONARY`, since
|
|
@@ -51,9 +53,14 @@ const zlibWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
|
|
|
51
53
|
|
|
52
54
|
/** A zlib compressor at one level, allocated once. Not shared between threads. */
|
|
53
55
|
export class ZlibCompressor {
|
|
54
|
-
readonly deflater: Deflater
|
|
56
|
+
private readonly deflater: Deflater
|
|
55
57
|
|
|
56
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* A compressor at `level`, 0 to 9; another level panics.
|
|
60
|
+
*
|
|
61
|
+
* @param level How hard to compress, `DEFLATE_STORE` (0) to
|
|
62
|
+
* `DEFLATE_BEST` (9); it also picks the header's `FLEVEL`.
|
|
63
|
+
*/
|
|
57
64
|
constructor(level: i32) {
|
|
58
65
|
this.deflater = new Deflater(level)
|
|
59
66
|
}
|
|
@@ -63,6 +70,15 @@ export class ZlibCompressor {
|
|
|
63
70
|
* stream and answers where it ends, or `DEFLATE_NO_ROOM` (-1) when `out`
|
|
64
71
|
* cannot hold it; `compressBound(len)` bytes always can. A window outside
|
|
65
72
|
* its array panics. Allocates nothing.
|
|
73
|
+
*
|
|
74
|
+
* @param src The array that holds the data to compress.
|
|
75
|
+
* @param off Where in `src` the data starts.
|
|
76
|
+
* @param len How many bytes of data to compress.
|
|
77
|
+
* @param out The caller's array the stream is written into.
|
|
78
|
+
* @param at Where in `out` the stream starts.
|
|
79
|
+
* @returns The index in `out` one past the stream's last byte, so the
|
|
80
|
+
* stream is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
|
|
81
|
+
* bytes of `out` from `at` may have been overwritten and mean nothing.
|
|
66
82
|
*/
|
|
67
83
|
compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
|
|
68
84
|
zlibWindow("ZlibCompressor.compress", src, off, len)
|
|
@@ -100,7 +116,7 @@ export class ZlibCompressor {
|
|
|
100
116
|
|
|
101
117
|
/** A zlib decompressor, allocated once. Not shared between threads. */
|
|
102
118
|
export class ZlibDecompressor {
|
|
103
|
-
readonly inflater: Inflater
|
|
119
|
+
private readonly inflater: Inflater
|
|
104
120
|
|
|
105
121
|
/** Builds the tables. */
|
|
106
122
|
constructor() {
|
|
@@ -113,6 +129,22 @@ export class ZlibDecompressor {
|
|
|
113
129
|
* answers where the output ends, or a negative `ZLIB_ERR_*` or
|
|
114
130
|
* `DEFLATE_ERR_*`. A window outside its array, or a negative `limit`,
|
|
115
131
|
* panics. Allocates nothing.
|
|
132
|
+
*
|
|
133
|
+
* @param src The array that holds the zlib stream.
|
|
134
|
+
* @param off Where in `src` the stream starts.
|
|
135
|
+
* @param len How many bytes the stream is; every one must belong to it,
|
|
136
|
+
* since a byte after the trailer is `DEFLATE_ERR_TRAILING`.
|
|
137
|
+
* @param out The caller's array the data is written into.
|
|
138
|
+
* @param at Where in `out` the data starts.
|
|
139
|
+
* @param limit The most bytes of data to accept; a longer stream is
|
|
140
|
+
* `DEFLATE_ERR_OUTPUT_LIMIT`, as is one that reaches the end of `out`.
|
|
141
|
+
* @returns The index in `out` one past the data's last byte, so the data
|
|
142
|
+
* is `out[at .. result)`; or a negative code: `DEFLATE_ERR_TRUNCATED`
|
|
143
|
+
* (the input ends inside the stream), `ZLIB_ERR_HEADER`,
|
|
144
|
+
* `ZLIB_ERR_DICTIONARY`, `ZLIB_ERR_ADLER`, `DEFLATE_ERR_TRAILING`,
|
|
145
|
+
* `DEFLATE_ERR_OUTPUT_LIMIT` or another `DEFLATE_ERR_*`. After an error
|
|
146
|
+
* the bytes of `out` from `at` may have been overwritten and mean
|
|
147
|
+
* nothing.
|
|
116
148
|
*/
|
|
117
149
|
decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 {
|
|
118
150
|
zlibWindow("ZlibDecompressor.decompress", src, off, len)
|
|
@@ -159,10 +191,34 @@ export class ZlibDecompressor {
|
|
|
159
191
|
}
|
|
160
192
|
}
|
|
161
193
|
|
|
162
|
-
/**
|
|
194
|
+
/**
|
|
195
|
+
* `ZlibCompressor.compress` at `level`, building the compressor for this call
|
|
196
|
+
* alone.
|
|
197
|
+
*
|
|
198
|
+
* @param src The array that holds the data to compress.
|
|
199
|
+
* @param off Where in `src` the data starts.
|
|
200
|
+
* @param len How many bytes of data.
|
|
201
|
+
* @param out The caller's array the stream is written into.
|
|
202
|
+
* @param at Where in `out` the stream starts.
|
|
203
|
+
* @param level How hard to compress, 0 to 9; another level panics.
|
|
204
|
+
* @returns The index in `out` one past the stream's last byte, or
|
|
205
|
+
* `DEFLATE_NO_ROOM`, as `ZlibCompressor.compress` answers.
|
|
206
|
+
*/
|
|
163
207
|
export const zlibCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, level: i32): i32 =>
|
|
164
208
|
new ZlibCompressor(level).compress(src, off, len, out, at)
|
|
165
209
|
|
|
166
|
-
/**
|
|
210
|
+
/**
|
|
211
|
+
* `ZlibDecompressor.decompress`, building the decompressor for this call
|
|
212
|
+
* alone.
|
|
213
|
+
*
|
|
214
|
+
* @param src The array that holds the zlib stream.
|
|
215
|
+
* @param off Where in `src` the stream starts.
|
|
216
|
+
* @param len How many bytes the stream is, trailer included.
|
|
217
|
+
* @param out The caller's array the data is written into.
|
|
218
|
+
* @param at Where in `out` the data starts.
|
|
219
|
+
* @param limit The most bytes of data to accept.
|
|
220
|
+
* @returns The index in `out` one past the data's last byte, or a negative
|
|
221
|
+
* code, as `ZlibDecompressor.decompress` answers.
|
|
222
|
+
*/
|
|
167
223
|
export const zlibDecompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 =>
|
|
168
224
|
new ZlibDecompressor().decompress(src, off, len, out, at, limit)
|