@amritk/nish-x86_64-linux 0.47.0 → 0.49.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.
@@ -141,6 +141,7 @@ import {
141
141
  } from "nish/net/http-compress"
142
142
  import { netClose, netMss, netRead, netShutdown, netWrite, netWrite2, tcpAccept } from "nish:net"
143
143
  import {
144
+ HTTP1_BAD_WINDOW,
144
145
  HTTP1_BODY,
145
146
  HTTP1_CHUNKED,
146
147
  HTTP1_CLOSED,
@@ -154,7 +155,7 @@ import {
154
155
  HTTP1_UPGRADE,
155
156
  Http1Parser,
156
157
  http1BytesAre,
157
- http1WriteChunk,
158
+ http1TryWriteChunk,
158
159
  http1WriteLastChunk,
159
160
  http1WriteResponseHead,
160
161
  } from "nish/net/http1-codec"
@@ -2090,7 +2091,12 @@ export class Http1Connection {
2090
2091
  return H1_AGAIN
2091
2092
  }
2092
2093
  if (chunked) {
2093
- this.outputEnd = http1WriteChunk(this.output, this.outputEnd, buf, off, take)
2094
+ // The window was checked on entry, and `take` only shortens it.
2095
+ const end: i32 = http1TryWriteChunk(this.output, this.outputEnd, buf, off, take)
2096
+ if (end === HTTP1_BAD_WINDOW) {
2097
+ return H1_INVALID
2098
+ }
2099
+ this.outputEnd = end
2094
2100
  } else {
2095
2101
  this.outputEnd = http1ServerCopy(this.output, this.outputEnd, buf, off, take)
2096
2102
  if (this.responseFraming >= 0) {
@@ -3345,6 +3351,27 @@ export class Http1Server {
3345
3351
  return true
3346
3352
  }
3347
3353
 
3354
+ /**
3355
+ * Frees `slot` without closing its socket, which is the caller's from now
3356
+ * on: for a program that moves a connection this pool has accepted but
3357
+ * not yet read to another carrier. A slot that has read, and so holds a
3358
+ * connection, is not released.
3359
+ *
3360
+ * @param slot the slot to free, as `accept` answered it
3361
+ * @returns the slot's socket, now the caller's to close; -1 for a free or unknown slot, or one that has read
3362
+ */
3363
+ release(slot: i32): i32 {
3364
+ const fds: i32[] = this.fds
3365
+ const at: i32[] = this.at
3366
+ if (slot < 0 || slot >= toI32(fds.length) || slot >= toI32(at.length) || fds[slot] < 0 || at[slot] >= 0) {
3367
+ return -1
3368
+ }
3369
+ const fd: i32 = fds[slot]
3370
+ fds[slot] = H1_FREE
3371
+ this.free.push(slot)
3372
+ return fd
3373
+ }
3374
+
3348
3375
  /**
3349
3376
  * Closes `slot`'s socket, whatever is left unsent, and frees the slot. Its
3350
3377
  * connection, when it holds one, is aborted and restarted, and is spare.
@@ -3,7 +3,9 @@
3
3
  * (h2c, RFC 9113 §3.3): a pool of `Http2Connection`s, one per accepted
4
4
  * socket, for a program that owns its readiness loop. The client starts with
5
5
  * the 24-byte connection preface; there is no `Upgrade: h2c` (RFC 9113 §3.1
6
- * deprecates it) and no fallback to HTTP/1.1 on the same port.
6
+ * deprecates it), and the pool serves no HTTP/1.1 on its own port. One port
7
+ * for both is `nish/net/http-server`'s `listenHttp1` with `h2c`: the façade
8
+ * reads the preface itself and hands a matching socket over with `adopt`.
7
9
  *
8
10
  * **No confidentiality, no integrity.** Anyone on the path reads and edits
9
11
  * the traffic. Serve h2c only upstream of a TLS-terminating proxy, on
@@ -339,6 +341,33 @@ export class Http2CleartextServer {
339
341
  return s.at
340
342
  }
341
343
 
344
+ /**
345
+ * Takes `fd`, a connection the program accepted and has read the whole
346
+ * 24-byte preface from, into a free slot, as `accept` does, and feeds
347
+ * `preface` to its connection: the slot starts past its preface, so
348
+ * `expire` never closes it for one. The bytes are the connection's to
349
+ * check, which refuses any other 24 as a bad preface (RFC 9113 §3.4).
350
+ *
351
+ * @param fd The socket, which is this pool's from now on, whatever it answers.
352
+ * @param preface The 24 bytes read from `fd`, in order; a shorter array panics, as `feed` does.
353
+ * @returns The slot, 0 or more, that now holds the connection; or `H2C_POOL_FULL` when every slot is busy
354
+ * (the socket is closed with nothing sent).
355
+ */
356
+ adopt(fd: i32, preface: u8[]): i32 {
357
+ const s: H2cSlot | null = this.take()
358
+ if (s === null) {
359
+ netClose(fd)
360
+ return H2C_POOL_FULL
361
+ }
362
+ s.conn.restart()
363
+ s.fd = fd
364
+ s.got = H2C_PREFACE_SIZE
365
+ s.since = monotonicNanos()
366
+ s.ended = false
367
+ s.conn.feed(preface, H2C_ZERO, H2C_PREFACE_SIZE)
368
+ return s.at
369
+ }
370
+
342
371
  /** A free slot for `accept`: a closed one first, then the next never used; null when every slot is busy. */
343
372
  private take(): H2cSlot | null {
344
373
  const free: H2cSlot[] = this.free
package/std/raster.ts CHANGED
@@ -730,7 +730,9 @@ export const rasterPushTrail = (trail: RasterTrail, x: f64, y: f64): void => {
730
730
  /**
731
731
  * A trail as a line of discs from oldest to newest, each larger and more
732
732
  * opaque than the one before: a streak that fades out behind what made it.
733
- * A point at `v` is drawn at pixel `offset + v * scale`.
733
+ * A point at `v` is drawn at pixel `offset + v * scale`. A `head` set
734
+ * outside the ring still draws the points `count` slots behind it that land
735
+ * in the ring, as an in-range head does, and no others.
734
736
  *
735
737
  * @param pixels - the frame, row-major
736
738
  * @param width - the frame's width in pixels
@@ -760,29 +762,41 @@ export const rasterTrail = (
760
762
  const n: i32 = toI32(xs.length)
761
763
  const count: i32 = Math.min(trail.count, n)
762
764
  const head: i32 = trail.head
763
- if (count <= 0 || head < 0 || head >= n) {
765
+ // The oldest point is `count` slots behind the head, wrapping once. A head
766
+ // out of the ring (a caller can set the field) still has the slots that land
767
+ // in it drawn. Point i sits at `head - count + i`, counted in f64, where it
768
+ // is exact for any head, so no sum can overflow. The points before slot 0
769
+ // wrap to the ring's end and are older than the rest, so they are drawn
770
+ // first, as one run of slots, and the others as a second.
771
+ if (count <= 0) {
764
772
  return
765
773
  }
766
- // The oldest point is `count` slots behind the head, wrapping; `slot` walks
767
- // forward from it.
768
- let slot: i32 = head - count
769
- if (slot < 0) {
770
- slot = slot + n
771
- }
772
- for (let i: i32 = 0; i < count; i++) {
773
- // `slot` is always in the ring, so the first test never ends the walk; it
774
- // is what proves the index.
775
- if (slot < 0 || slot >= n) {
776
- return
777
- }
778
- if (slot < toI32(ys.length)) {
779
- const f: f64 = (toF64(i) + 1.0) / (toF64(count) + 1.0)
780
- const radius: f64 = r * (f * 0.6 + 0.4)
781
- const cx: f64 = offsetX + xs[slot] * scale
782
- const cy: f64 = offsetY + ys[slot] * scale
783
- rasterDisc(pixels, width, height, cx, cy, radius * scale, colour, alpha * f)
774
+ const size: f64 = toF64(n)
775
+ const ysLength: i32 = toI32(ys.length)
776
+ const first: f64 = toF64(head) - toF64(count)
777
+ const steps: f64 = toF64(count) + 1.0
778
+ for (let pass: i32 = 0; pass < 2; pass++) {
779
+ // Point i of this run sits at slot `base + i`.
780
+ const base: f64 = pass === 0 ? first + size : first
781
+ const lo: f64 = Math.max(base, 0.0)
782
+ const hi: f64 = Math.min(base + toF64(count), size)
783
+ if (lo < hi) {
784
+ const end: i32 = toI32(hi)
785
+ // How far along the trail the point is, from 1 for the oldest.
786
+ let step: f64 = lo - base + 1.0
787
+ for (let slot: i32 = toI32(lo); slot < end; slot++) {
788
+ // The first two are always true in the run and prove the indexes; the
789
+ // third skips a slot a shorter `ys` does not hold.
790
+ if (slot >= 0 && slot < n && slot < ysLength) {
791
+ const f: f64 = step / steps
792
+ const radius: f64 = r * (f * 0.6 + 0.4)
793
+ const cx: f64 = offsetX + xs[slot] * scale
794
+ const cy: f64 = offsetY + ys[slot] * scale
795
+ rasterDisc(pixels, width, height, cx, cy, radius * scale, colour, alpha * f)
796
+ }
797
+ step = step + 1.0
798
+ }
784
799
  }
785
- slot = slot + 1 >= n ? 0 : slot + 1
786
800
  }
787
801
  }
788
802
 
package/std/sync/shm.ts CHANGED
@@ -175,6 +175,9 @@ const shmDone = (rc: i32): Result<void, ShmError> => {
175
175
  * Whether `name` keeps `createRegion`'s rules: 1 to 30 of
176
176
  * `A-Z a-z 0-9 . _ -`, not starting with `.`. The runtime checks them too;
177
177
  * checking here answers a NUL byte as `SHM_INVALID` before C would read it.
178
+ * Each caller also asks `name.indexOf("\u0000") < 0` itself, which is what
179
+ * proves the name NUL-free at the `declare function` call, so no `ffi-nul`
180
+ * panic is left there.
178
181
  */
179
182
  const shmNameOk = (name: string): boolean => {
180
183
  const n: i32 = toI32(name.length)
@@ -211,7 +214,7 @@ const shmNameOk = (name: string): boolean => {
211
214
  * system gave
212
215
  */
213
216
  export const createRegion = (name: string, bytes: i32): Result<SharedRegion, ShmError> => {
214
- if (!shmNameOk(name)) {
217
+ if (name.indexOf("\u0000") >= 0 || !shmNameOk(name)) {
215
218
  return Err(new ShmError(SHM_INVALID))
216
219
  }
217
220
  const h: i32 = nishshm_create(name, bytes)
@@ -235,7 +238,7 @@ export const createRegion = (name: string, bytes: i32): Result<SharedRegion, Shm
235
238
  * system gave
236
239
  */
237
240
  export const openRegion = (name: string): Result<SharedRegion, ShmError> => {
238
- if (!shmNameOk(name)) {
241
+ if (name.indexOf("\u0000") >= 0 || !shmNameOk(name)) {
239
242
  return Err(new ShmError(SHM_INVALID))
240
243
  }
241
244
  const h: i32 = nishshm_open(name)
@@ -254,7 +257,7 @@ export const openRegion = (name: string): Result<SharedRegion, ShmError> => {
254
257
  * `SHM_INVALID`, code 95 on wasi, or the errno the system gave
255
258
  */
256
259
  export const unlinkRegion = (name: string): Result<void, ShmError> => {
257
- if (!shmNameOk(name)) {
260
+ if (name.indexOf("\u0000") >= 0 || !shmNameOk(name)) {
258
261
  return Err(new ShmError(SHM_INVALID))
259
262
  }
260
263
  return shmDone(nishshm_unlink(name))
package/std/synth.ts CHANGED
@@ -47,7 +47,15 @@ const SYNTH_SAMPLE_LIMIT: f64 = 2147483647.0
47
47
  */
48
48
  export const synthSilence = (seconds: f64, sampleRate: i32): f64[] => {
49
49
  const want: f64 = seconds * toF64(sampleRate)
50
- const n: i32 = want >= 1.0 && want < SYNTH_SAMPLE_LIMIT ? toI32(want) : 1
50
+ let n: i32 = 1
51
+ if (want >= 1.0 && want < SYNTH_SAMPLE_LIMIT) {
52
+ const k: i32 = toI32(want)
53
+ // Never false: `want` is at least 1 here. The test is what proves the
54
+ // array length is not negative.
55
+ if (k >= 1) {
56
+ n = k
57
+ }
58
+ }
51
59
  const out: f64[] = new Array<f64>(n)
52
60
  out.fill(0.0)
53
61
  return out