@ref0/git 0.27.0 → 0.30.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/dist/client/git-http.mjs +29 -12
- package/dist/client/push-upload.mjs +13 -12
- package/dist/client/remote-helper.mjs +1 -1
- package/dist/client/remote.mjs +1 -1
- package/dist/client/run-remote-helper.mjs +7 -14
- package/dist/client/slice-push.d.mts +13 -9
- package/dist/client/slice-push.mjs +16 -5
- package/dist/client/slice-report.mjs +6 -8
- package/dist/client/transport.mjs +27 -5
- package/dist/contracts/git.d.mts +17 -2
- package/dist/contracts/store.d.mts +1 -1
- package/dist/core/forks.mjs +1 -1
- package/dist/core/purge.mjs +1 -1
- package/dist/http/push-uploads.mjs +10 -15
- package/dist/lfs/copy.d.mts +4 -1
- package/dist/lfs/copy.mjs +7 -6
- package/dist/lfs/layer.mjs +1 -1
- package/dist/lfs/objects.mjs +127 -8
- package/dist/lfs/upload-most.mjs +19 -3
- package/dist/merges/merges.d.mts +3 -2
- package/dist/protocol/bytes/buffers.mjs +5 -5
- package/dist/protocol/bytes/crc32.mjs +39 -6
- package/dist/protocol/bytes/deflate.d.mts +11 -31
- package/dist/protocol/bytes/deflate.mjs +133 -72
- package/dist/protocol/bytes/inflate.mjs +2 -2
- package/dist/protocol/bytes/sha1-hasher.mjs +12 -12
- package/dist/protocol/bytes/zlib-ng/README.md +65 -38
- package/dist/protocol/bytes/zlib-ng/compile.d.mts +5 -4
- package/dist/protocol/bytes/zlib-ng/compile.mjs +5 -4
- package/dist/protocol/bytes/zlib-ng/embedded.mjs +1 -1
- package/dist/protocol/bytes/zlib-ng/instance.mjs +14 -8
- package/dist/protocol/bytes/zlib-ng/zlib-ng.wasm +0 -0
- package/dist/protocol/objects/fsck-names.mjs +27 -7
- package/dist/protocol/objects/fsck.mjs +100 -88
- package/dist/protocol/objects/object-rules.d.mts +1 -0
- package/dist/protocol/objects/tree.d.mts +1 -0
- package/dist/protocol/objects/tree.mjs +55 -6
- package/dist/protocol/wire/push-uploads.mjs +12 -1
- package/dist/server/registry.mjs +12 -0
- package/dist/server/server.mjs +7 -1
- package/dist/storage/budget.d.mts +2 -1
- package/dist/storage/budget.mjs +25 -9
- package/dist/storage/calls.d.mts +1 -1
- package/dist/storage/calls.mjs +4 -2
- package/dist/storage/host-bucket.mjs +15 -5
- package/dist/storage/index-bitmaps.d.mts +1 -0
- package/dist/storage/index-bitmaps.mjs +15 -14
- package/dist/storage/index-build.d.mts +2 -1
- package/dist/storage/index-build.mjs +14 -16
- package/dist/storage/index-files.mjs +3 -3
- package/dist/storage/index-graph.mjs +3 -3
- package/dist/storage/index-remap.mjs +6 -7
- package/dist/storage/links.mjs +34 -2
- package/dist/storage/memory-budget.mjs +12 -3
- package/dist/storage/merge-stream.mjs +4 -3
- package/dist/storage/merge-write.mjs +2 -2
- package/dist/storage/pack-receive.mjs +24 -8
- package/dist/storage/packs.d.mts +3 -1
- package/dist/storage/packs.mjs +33 -11
- package/dist/storage/put-holds.d.mts +1 -0
- package/dist/storage/put-holds.mjs +20 -0
- package/dist/storage/ready.mjs +4 -2
- package/dist/storage/receive-bounds.d.mts +1 -1
- package/dist/storage/receive-bounds.mjs +4 -4
- package/dist/storage/receive-links.mjs +31 -15
- package/dist/storage/receive-steps.mjs +21 -1
- package/dist/storage/runs.d.mts +1 -1
- package/dist/storage/runs.mjs +7 -5
- package/dist/store/door.mjs +9 -1
- package/dist/store/job-try.mjs +5 -5
- package/dist/store/repo-record.mjs +1 -1
- package/dist/sync/step.mjs +3 -4
- package/package.json +3 -9
- package/dist/http/node.d.mts +0 -62
- package/dist/http/node.mjs +0 -212
- package/dist/node.d.mts +0 -3
- package/dist/node.mjs +0 -1
- package/dist/server/edge-node.d.mts +0 -19
- package/dist/server/edge-node.mjs +0 -20
package/dist/storage/runs.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { bucketDeleteMany, bucketDo, sizedPutCalls } from "./calls.mjs";
|
|
|
3
3
|
import { runKey, runsPrefix } from "./layout.mjs";
|
|
4
4
|
import { startPut } from "./merge.mjs";
|
|
5
5
|
import { readExactly } from "./pack-ranges.mjs";
|
|
6
|
+
import { putHeldBytes } from "./put-holds.mjs";
|
|
6
7
|
import { receiveDropPage, receiveMergeReadBytes, receiveRunBytes } from "./receive-bounds.mjs";
|
|
7
8
|
import * as Schema from "effect/Schema";
|
|
8
9
|
import * as Effect from "effect/Effect";
|
|
@@ -697,9 +698,9 @@ const eachRow = /* @__PURE__ */ Effect.fnUntraced(function* (cursor, places, eac
|
|
|
697
698
|
const mergeFew = (bucket, runs, each, bounds, room, starts) => Effect.flatMap(fewCursor(bucket, runs, bounds, room, starts), ({ next, places }) => eachRow(next, places, each));
|
|
698
699
|
/**
|
|
699
700
|
* `runs` merged `bounds.ways` chains at a time into fewer, longer runs under `pack`, until no more
|
|
700
|
-
* than that many chains are left: each pass reads and puts every row once, its reads
|
|
701
|
-
* taken from `room` as it merges. Each run it puts is a chain of its own. A
|
|
702
|
-
* same runs more than once narrows them once first.
|
|
701
|
+
* than that many chains are left: each pass reads and puts every row once, its reads, its chunk
|
|
702
|
+
* and what its put holds taken from `room` as it merges. Each run it puts is a chain of its own. A
|
|
703
|
+
* caller merging the same runs more than once narrows them once first.
|
|
703
704
|
*/
|
|
704
705
|
const narrowed = /* @__PURE__ */ Effect.fnUntraced(function* (bucket, pack, runs, bounds, room) {
|
|
705
706
|
const { ways } = bounds;
|
|
@@ -720,7 +721,8 @@ const narrowed = /* @__PURE__ */ Effect.fnUntraced(function* (bucket, pack, runs
|
|
|
720
721
|
const length = group.lengths.reduce((sum, each) => sum + each, 0);
|
|
721
722
|
const key = runKey(pack, `${runs.kind}.${pass}`, keys.length);
|
|
722
723
|
const chunkBytes = Math.max(1, Math.floor(writeBytes / runs.width)) * runs.width;
|
|
723
|
-
const
|
|
724
|
+
const held = chunkBytes + putHeldBytes(bucket.multipart, { size: length });
|
|
725
|
+
const taking = room?.take(held);
|
|
724
726
|
if (taking !== void 0) yield* taking;
|
|
725
727
|
yield* Effect.scoped(Effect.gen(function* () {
|
|
726
728
|
const put = yield* startPut(bucket, key, { size: length });
|
|
@@ -741,7 +743,7 @@ const narrowed = /* @__PURE__ */ Effect.fnUntraced(function* (bucket, pack, runs
|
|
|
741
743
|
yield* put.finish;
|
|
742
744
|
}).pipe(Effect.onError(() => put.abort));
|
|
743
745
|
}));
|
|
744
|
-
room?.give(
|
|
746
|
+
room?.give(held);
|
|
745
747
|
keys.push(key);
|
|
746
748
|
lengths.push(length);
|
|
747
749
|
}
|
package/dist/store/door.mjs
CHANGED
|
@@ -127,11 +127,19 @@ const untilClosed = (body, door) => bodyUntilAborted(body, door.closing, closedE
|
|
|
127
127
|
/** `links`, built on `context`, each call through `door` on the caller's context, as `doorBucket`'s. */
|
|
128
128
|
const doorLinks = (links, context, door) => {
|
|
129
129
|
const { call } = doorOf(context, door);
|
|
130
|
-
|
|
130
|
+
const calls = {
|
|
131
131
|
url: (key, options) => call(links.url(key, options)),
|
|
132
132
|
putUrl: (key, options) => call(links.putUrl(key, options)),
|
|
133
133
|
head: (key) => call(links.head(key))
|
|
134
134
|
};
|
|
135
|
+
return links.puts === "checked" ? {
|
|
136
|
+
puts: "checked",
|
|
137
|
+
...calls
|
|
138
|
+
} : {
|
|
139
|
+
puts: "unchecked",
|
|
140
|
+
...calls,
|
|
141
|
+
copy: (from, to) => call(links.copy(from, to))
|
|
142
|
+
};
|
|
135
143
|
};
|
|
136
144
|
/** `uploads`, built on `context`, each call through `door` on the caller's context, as `doorBucket`'s. */
|
|
137
145
|
const doorUploads = (uploads, context, door) => {
|
package/dist/store/job-try.mjs
CHANGED
|
@@ -13,18 +13,18 @@ import * as Scheduler from "effect/Scheduler";
|
|
|
13
13
|
* The longest a try of Git's long jobs runs: `git.index`'s, `git.repack`'s, `git.ref-index`'s and
|
|
14
14
|
* a push upload's, 3.5 minutes. An import's and a mirror's is `importTryTimeout`, longer until
|
|
15
15
|
* their transfers resume across tries. With the minute a host's `onFailure` may
|
|
16
|
-
* take, that's 270 s
|
|
17
|
-
*
|
|
16
|
+
* take, that's 270 s: a try cut short, as a Vercel Daemon's is when a deploy replaces it 30 s after
|
|
17
|
+
* `SIGTERM`, runs again once its lease lapses, so a short try bounds what a cut loses (#2900). A step is cut at `jobBucketCallsDefault`'s 1,000 bucket calls, about
|
|
18
18
|
* 100 s at 100 ms a call, and no step is cut by CPU: s1m's heaviest, `git.index`'s bitmaps, took
|
|
19
19
|
* 35,138 ms of CPU in one try (s1m-upkeep run 46099393357), so its try fits with 75 s to spare. A
|
|
20
20
|
* Worker's alarm, 15 minutes, holds it on every platform, so it's one number everywhere.
|
|
21
21
|
*/
|
|
22
22
|
const jobTryTimeout = "210 seconds";
|
|
23
23
|
/**
|
|
24
|
-
* The bucket calls a try may make where the adapter bounds none, as
|
|
25
|
-
*
|
|
24
|
+
* The bucket calls a try may make where the adapter bounds none, as Postgres's store doesn't:
|
|
25
|
+
* Workers' 1,000, so a step that stops at a Worker's bound and goes on in its next try stops
|
|
26
26
|
* there on every platform. Without it, on Vercel, a step such as `git.index`'s `bitmaps`, 999
|
|
27
|
-
* calls a try on Workers for a million objects (#1132), ran on past
|
|
27
|
+
* calls a try on Workers for a million objects (#1132), ran on past its try's timeout on calls
|
|
28
28
|
* alone (#2900). A request's calls stay the adapter's, unbounded where it gives none.
|
|
29
29
|
*/
|
|
30
30
|
const jobBucketCallsDefault = 1e3;
|
|
@@ -145,7 +145,7 @@ const heldStore = (store) => ({
|
|
|
145
145
|
/**
|
|
146
146
|
* `addition` to `repo`'s scope, an add at no version, only while the repo is live: it reads the
|
|
147
147
|
* record first and fails `RepoGone` when there's none or it's deleted, so a late write, a wake or
|
|
148
|
-
* a job after the delete never makes a purged scope again
|
|
148
|
+
* a job after the delete never makes a purged scope again. One whose
|
|
149
149
|
* read stalled past `heldUntil` is `UNAVAILABLE`, for a try that reads again, and so is one whose
|
|
150
150
|
* add the store found past it as it landed, `heldUntil` being the add's `by`. A wake, a job, an
|
|
151
151
|
* upload or a sync's cover made apart from the repo's own read goes through here, and a push's
|
package/dist/sync/step.mjs
CHANGED
|
@@ -13,7 +13,7 @@ import { discover, fetchTerms, remoteRefsAndHeadOf } from "../client/remote.mjs"
|
|
|
13
13
|
import { heldOnce, oneShotReads, scopedReader, scopedReceiveStep } from "../storage/calls.mjs";
|
|
14
14
|
import { isPackId } from "../storage/layout.mjs";
|
|
15
15
|
import { receiveCutParentsMost } from "../storage/receive-bounds.mjs";
|
|
16
|
-
import { ReceiveRow, receiveKey
|
|
16
|
+
import { ReceiveRow, receiveKey } from "../storage/receive-steps.mjs";
|
|
17
17
|
import { decodeHeld } from "../storage/rows.mjs";
|
|
18
18
|
import { infiniteDepth } from "../storage/shallow.mjs";
|
|
19
19
|
import { decodeRow, storeCall, storeCommit } from "../store/errors.mjs";
|
|
@@ -740,17 +740,16 @@ const resumedIn = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, row, rec
|
|
|
740
740
|
edits: [],
|
|
741
741
|
atomic: false,
|
|
742
742
|
pack: { received: answer.received },
|
|
743
|
-
also: (
|
|
743
|
+
also: (_at, received) => {
|
|
744
744
|
const written = cutAfter(lands.roots, kept, received);
|
|
745
745
|
const paged = withLfsPages(landed(row, lands), received?.lfsPointers, copying);
|
|
746
746
|
return {
|
|
747
747
|
put: [
|
|
748
748
|
importRow(paged.row),
|
|
749
|
-
stagedRow(pack, at, true),
|
|
750
749
|
...written.put,
|
|
751
750
|
...paged.put
|
|
752
751
|
],
|
|
753
|
-
delete:
|
|
752
|
+
delete: written.delete,
|
|
754
753
|
jobs: []
|
|
755
754
|
};
|
|
756
755
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ref0/git",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Git hosting as a library: a server, a client and their merges",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -34,11 +34,6 @@
|
|
|
34
34
|
"import": "./dist/extend.mjs",
|
|
35
35
|
"default": "./dist/extend.mjs"
|
|
36
36
|
},
|
|
37
|
-
"./node": {
|
|
38
|
-
"types": "./dist/node.d.mts",
|
|
39
|
-
"import": "./dist/node.mjs",
|
|
40
|
-
"default": "./dist/node.mjs"
|
|
41
|
-
},
|
|
42
37
|
"./protocol": {
|
|
43
38
|
"types": "./dist/protocol.d.mts",
|
|
44
39
|
"import": "./dist/protocol.mjs",
|
|
@@ -56,12 +51,11 @@
|
|
|
56
51
|
}
|
|
57
52
|
},
|
|
58
53
|
"dependencies": {
|
|
59
|
-
"@ref0/infra": "0.
|
|
60
|
-
"pako": "2.1.0"
|
|
54
|
+
"@ref0/infra": "0.30.0"
|
|
61
55
|
},
|
|
62
56
|
"devDependencies": {
|
|
63
57
|
"@effect/vitest": "4.1.7",
|
|
64
|
-
"@ref0/xdiff-wasm": "0.
|
|
58
|
+
"@ref0/xdiff-wasm": "0.30.0",
|
|
65
59
|
"effect": "4.1.7",
|
|
66
60
|
"vite-plus": "1.8.0",
|
|
67
61
|
"vitest": "5.1.1",
|
package/dist/http/node.d.mts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What `toNodeHandler` reads of `node:http`'s request, which Express's extends: this package has
|
|
3
|
-
* no Node types, so the few members used are typed here.
|
|
4
|
-
*/
|
|
5
|
-
export interface NodeRequest extends AsyncIterable<Uint8Array> {
|
|
6
|
-
readonly method?: string | undefined;
|
|
7
|
-
readonly url?: string | undefined;
|
|
8
|
-
/** Express's: the path before a router took its mount off `url`. */
|
|
9
|
-
readonly originalUrl?: string | undefined;
|
|
10
|
-
readonly rawHeaders: ReadonlyArray<string>;
|
|
11
|
-
/** `host`, or over HTTP/2, `node:http2`'s `:authority`, which stands in for it. */
|
|
12
|
-
readonly headers: {
|
|
13
|
-
readonly host?: string | undefined;
|
|
14
|
-
readonly ':authority'?: string | undefined;
|
|
15
|
-
};
|
|
16
|
-
/** A `Socket`, whose `remoteAddress` every one has; a TLS one's `encrypted` is `true`. */
|
|
17
|
-
readonly socket: {
|
|
18
|
-
readonly remoteAddress?: string | undefined;
|
|
19
|
-
readonly encrypted?: boolean | undefined;
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* What `toNodeHandler` calls of `node:http`'s response, or `node:http2`'s compatibility one. Both
|
|
24
|
-
* set `writableEnded` once `end` is called, and not for a client that left first; `node:http2`'s
|
|
25
|
-
* has no `destroyed` of its own, and its stream's says whether it's gone, and its `close` that
|
|
26
|
-
* the client left.
|
|
27
|
-
*/
|
|
28
|
-
export interface NodeResponse {
|
|
29
|
-
readonly headersSent: boolean;
|
|
30
|
-
readonly writableEnded: boolean;
|
|
31
|
-
readonly destroyed?: boolean | undefined;
|
|
32
|
-
readonly stream?: {
|
|
33
|
-
readonly destroyed: boolean;
|
|
34
|
-
once(event: 'close', listener: () => void): void;
|
|
35
|
-
} | undefined;
|
|
36
|
-
writeHead(status: number, headers: string[]): void;
|
|
37
|
-
write(chunk: Uint8Array): boolean;
|
|
38
|
-
end(): void;
|
|
39
|
-
once(event: 'drain' | 'close', listener: () => void): void;
|
|
40
|
-
removeListener(event: 'drain' | 'close', listener: () => void): void;
|
|
41
|
-
destroy(): void;
|
|
42
|
-
}
|
|
43
|
-
/** A listener for `node:http`'s `createServer`, or an Express route. */
|
|
44
|
-
export type NodeHandler = (request: NodeRequest, response: NodeResponse) => void;
|
|
45
|
-
/**
|
|
46
|
-
* What a Node host serves under, its public origin, when a proxy in front of it serves one its
|
|
47
|
-
* socket can't tell, as one that ends TLS does: every link Git gives out, a clone's, a redirect's
|
|
48
|
-
* and LFS's, is built from it. A request's own `Host`, `X-Forwarded-Host` and `X-Forwarded-Proto`
|
|
49
|
-
* never are: a client can send any of them.
|
|
50
|
-
*/
|
|
51
|
-
export interface NodeServeOptions {
|
|
52
|
-
/**
|
|
53
|
-
* The origin the host is reached at, such as `'https://git.example.com'`: a scheme, a host and a
|
|
54
|
-
* port, with no path, query or trailing slash. A `basePath` stays the runtime's. Unset, each
|
|
55
|
-
* request's is the connection's, its scheme from the socket and its host from its `Host`.
|
|
56
|
-
*/
|
|
57
|
-
readonly origin?: string | undefined;
|
|
58
|
-
}
|
|
59
|
-
/** Express's `next`: the request goes on to the app's own middleware and routes. */
|
|
60
|
-
export type NodeNext = () => void;
|
|
61
|
-
/** An Express middleware, or `next` for a request it doesn't answer. */
|
|
62
|
-
export type NodeMiddleware = (request: NodeRequest, response: NodeResponse, next: NodeNext) => void;
|
package/dist/http/node.mjs
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import { ownedReader } from "../protocol/bytes/reader.mjs";
|
|
2
|
-
// src/http/node.ts
|
|
3
|
-
/**
|
|
4
|
-
* A request's body as a stream, read from Node only as the handler pulls it, with nothing read
|
|
5
|
-
* ahead: over HTTP/2, `node:http2` closes a stream whose upload no one reads once its answer has
|
|
6
|
-
* ended, so an upload Git refused unread doesn't hold the stream open till the client ends it.
|
|
7
|
-
*/
|
|
8
|
-
const streamOf = (incoming) => {
|
|
9
|
-
const chunks = incoming[Symbol.asyncIterator]();
|
|
10
|
-
return new ReadableStream({
|
|
11
|
-
pull: async (controller) => {
|
|
12
|
-
const next = await chunks.next();
|
|
13
|
-
if (next.done === true) return controller.close();
|
|
14
|
-
controller.enqueue(next.value);
|
|
15
|
-
},
|
|
16
|
-
cancel: async () => {
|
|
17
|
-
await chunks.return?.();
|
|
18
|
-
}
|
|
19
|
-
}, { highWaterMark: 0 });
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* The origin of the connection Node received `incoming` on: its scheme from the socket, `https`
|
|
23
|
-
* for TLS, and its `Host`. Over HTTP/2, `:authority` is the `Host`, and a request whose `host`
|
|
24
|
-
* names another, which RFC 9113 calls malformed, has none. It throws for a `Host` that is empty or
|
|
25
|
-
* holds more than a host and port, as `mo.dev#`, `mo.dev/git` or `@evil.example` do, since the
|
|
26
|
-
* target would be read as part of the origin.
|
|
27
|
-
*/
|
|
28
|
-
const socketOrigin = (incoming) => {
|
|
29
|
-
const authority = incoming.headers[":authority"];
|
|
30
|
-
const given = incoming.headers.host;
|
|
31
|
-
if (authority !== void 0 && given !== void 0 && given.toLowerCase() !== authority.toLowerCase()) throw new TypeError(`A Host, ${given}, that isn't the :authority, ${authority}`);
|
|
32
|
-
const host = authority ?? given ?? "localhost";
|
|
33
|
-
if (host === "" || /[/\\?#@]/.test(host)) throw new TypeError(`No origin in the Host ${host}`);
|
|
34
|
-
return `${incoming.socket.encrypted === true ? "https" : "http"}://${host}`;
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* The URL Node received, as `Request` reads it, under `origin`, the connection's when the host
|
|
38
|
-
* names none: dot segments, `%2e%2e` and `\` resolved. A path is read after the origin, so one
|
|
39
|
-
* that starts `//`, as `//evil.example/git/…` does, stays a path, and an absolute target keeps
|
|
40
|
-
* only its path and query: neither names the origin Git builds its links from. It throws for one
|
|
41
|
-
* that can't be read, such as one whose `Host` `socketOrigin` refuses, when the host names none.
|
|
42
|
-
*/
|
|
43
|
-
const urlOf = (incoming, origin = socketOrigin(incoming)) => {
|
|
44
|
-
const target = incoming.originalUrl ?? incoming.url ?? "/";
|
|
45
|
-
if (target.startsWith("/")) return new URL(`${origin}${target}`);
|
|
46
|
-
const named = new URL(target);
|
|
47
|
-
return new URL(`${origin}${named.pathname}${named.search}`);
|
|
48
|
-
};
|
|
49
|
-
/** A bare `http://` or `https://` host and port, as a URL's `origin` reads it. */
|
|
50
|
-
const bareOrigin = /^https?:\/\/[^/\\?#@\s]+$/i;
|
|
51
|
-
/**
|
|
52
|
-
* `options.origin`, checked as the handler is made, so a host that names a wrong one fails as it
|
|
53
|
-
* starts, not on its first request: one that isn't a bare `http(s)://host[:port]` throws.
|
|
54
|
-
* `undefined` when it's unset.
|
|
55
|
-
*/
|
|
56
|
-
const servedOrigin = ({ origin }) => {
|
|
57
|
-
if (origin === void 0) return void 0;
|
|
58
|
-
const url = URL.parse(origin);
|
|
59
|
-
if (url === null || !bareOrigin.test(origin) || url.pathname !== "/") throw new TypeError(`A Node host's origin is a bare http(s)://host[:port], with no path, query or trailing slash, not ${origin}`);
|
|
60
|
-
return url.origin;
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* The request Node received, at `url`, as a `Request` on `signal`, its body streamed. HTTP/2's
|
|
64
|
-
* pseudo-headers, such as `:method` and `:authority`, are left out: `Headers` refuses their names.
|
|
65
|
-
*/
|
|
66
|
-
const requestOf = (incoming, signal, url = urlOf(incoming)) => {
|
|
67
|
-
const headers = new Headers();
|
|
68
|
-
for (let at = 0; at + 1 < incoming.rawHeaders.length; at += 2) {
|
|
69
|
-
const name = incoming.rawHeaders[at] ?? "";
|
|
70
|
-
if (!name.startsWith(":")) headers.append(name, incoming.rawHeaders[at + 1] ?? "");
|
|
71
|
-
}
|
|
72
|
-
const method = incoming.method ?? "GET";
|
|
73
|
-
const init = {
|
|
74
|
-
method,
|
|
75
|
-
headers
|
|
76
|
-
};
|
|
77
|
-
if (signal !== void 0) init.signal = signal;
|
|
78
|
-
if (method !== "GET" && method !== "HEAD") {
|
|
79
|
-
init.body = streamOf(incoming);
|
|
80
|
-
init.duplex = "half";
|
|
81
|
-
}
|
|
82
|
-
return new Request(url, init);
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* What emits `close` when the client leaves: over HTTP/2, the response's stream, since `node:http2`'s
|
|
86
|
-
* compatibility response emits none for a HEAD whose stream closes before `end`; else the response.
|
|
87
|
-
*/
|
|
88
|
-
const closer = (response) => response.stream ?? response;
|
|
89
|
-
/**
|
|
90
|
-
* Whether Node's response is gone, its connection closed or reset: `node:http`'s says so itself,
|
|
91
|
-
* and `node:http2`'s compatibility response only through its stream.
|
|
92
|
-
*/
|
|
93
|
-
const gone = (response) => response.destroyed === true || response.stream?.destroyed === true;
|
|
94
|
-
/**
|
|
95
|
-
* Resolves once Node can take more of the response, or once its connection has closed, and takes
|
|
96
|
-
* its other listener off: a long answer waits for `drain` many times, and each wait would leave a
|
|
97
|
-
* `close` listener on the response till it closed.
|
|
98
|
-
*/
|
|
99
|
-
const writable = (response) => new Promise((ready) => {
|
|
100
|
-
const settle = () => {
|
|
101
|
-
response.removeListener("drain", settle);
|
|
102
|
-
response.removeListener("close", settle);
|
|
103
|
-
ready();
|
|
104
|
-
};
|
|
105
|
-
response.once("drain", settle);
|
|
106
|
-
response.once("close", settle);
|
|
107
|
-
});
|
|
108
|
-
/**
|
|
109
|
-
* Writes `answer` to Node's response, each header as it is, its body as it streams: a write Node
|
|
110
|
-
* can't take yet waits for `drain`, so a slow client holds the stream back, not memory. A
|
|
111
|
-
* connection that closes first cancels the body, a read it waits on included. The answer to a
|
|
112
|
-
* `HEAD` goes without its body, which is cancelled, as RFC 9110 says: `node:http2` ends a HEAD's
|
|
113
|
-
* stream with its head, and would take no write after it.
|
|
114
|
-
*/
|
|
115
|
-
const respond = async (answer, response, head = false) => {
|
|
116
|
-
const headers = [...answer.headers].flatMap(([name, value]) => [name, value]);
|
|
117
|
-
response.writeHead(answer.status, headers);
|
|
118
|
-
if (head) answer.body?.cancel().catch(() => {});
|
|
119
|
-
else if (answer.body !== null) {
|
|
120
|
-
const { reader, close: cancel } = ownedReader(answer.body);
|
|
121
|
-
if (gone(response)) return cancel();
|
|
122
|
-
closer(response).once("close", () => {
|
|
123
|
-
if (!response.writableEnded) cancel();
|
|
124
|
-
});
|
|
125
|
-
try {
|
|
126
|
-
for (;;) {
|
|
127
|
-
const next = await reader.read();
|
|
128
|
-
if (next.done || gone(response)) break;
|
|
129
|
-
if (!response.write(next.value)) await writable(response);
|
|
130
|
-
}
|
|
131
|
-
} finally {
|
|
132
|
-
cancel();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (!gone(response)) response.end();
|
|
136
|
-
};
|
|
137
|
-
/** No answer to a client that went away: `answer`'s body is cancelled and the response destroyed. */
|
|
138
|
-
const leave = (answer, response) => {
|
|
139
|
-
answer.body?.cancel().catch(() => {});
|
|
140
|
-
response.destroy();
|
|
141
|
-
};
|
|
142
|
-
/**
|
|
143
|
-
* `handler`'s answer to `incoming`, at `url` or the URL Node received under `origin`, written to
|
|
144
|
-
* `response`. A response already gone, as when the client left while the app's earlier middleware
|
|
145
|
-
* ran, gets no call to `handler`: its `close` came before this could hear it.
|
|
146
|
-
*/
|
|
147
|
-
const answerAt = (handler, incoming, response, url, origin) => {
|
|
148
|
-
if (gone(response)) return;
|
|
149
|
-
const ended = new AbortController();
|
|
150
|
-
closer(response).once("close", () => {
|
|
151
|
-
if (!response.writableEnded) ended.abort();
|
|
152
|
-
});
|
|
153
|
-
let request;
|
|
154
|
-
try {
|
|
155
|
-
request = requestOf(incoming, ended.signal, url ?? urlOf(incoming, origin));
|
|
156
|
-
} catch {
|
|
157
|
-
response.writeHead(400, ["content-type", "text/plain"]);
|
|
158
|
-
response.end();
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
Promise.resolve().then(() => handler.fetch(request)).then((answer) => ended.signal.aborted ? leave(answer, response) : respond(answer, response, request.method === "HEAD")).catch(() => {
|
|
162
|
-
if (ended.signal.aborted || response.headersSent || gone(response)) {
|
|
163
|
-
response.destroy();
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
response.writeHead(500, ["content-type", "text/plain"]);
|
|
167
|
-
response.end();
|
|
168
|
-
});
|
|
169
|
-
};
|
|
170
|
-
/**
|
|
171
|
-
* A `fetch` as a `node:http` or `node:http2` listener or an Express route, which
|
|
172
|
-
* `@ref0/git/node`'s `toNodeHandler` hands a runtime's `Git.toFetch`; beside an app's own routes,
|
|
173
|
-
* `toNodeMiddleware`. The body
|
|
174
|
-
* streams to Git unread. The response streams back as Node can take it, and a client that
|
|
175
|
-
* disconnects or resets its stream before the response ends, which a `close` of the HTTP/2
|
|
176
|
-
* stream, or the HTTP/1 response, before `writableEnded` says, aborts the request's `signal`, which ends Git's reads and leaves it
|
|
177
|
-
* unanswered. A request whose URL can't be read, such as one with a malformed `Host`, is answered
|
|
178
|
-
* 400. Over HTTP/2, `node:http2` keeps a stream open while its client's end is, as for a `HEAD`
|
|
179
|
-
* whose client never ends it: that holds only the client's own stream. With `options.origin`, every
|
|
180
|
-
* request is read under it, its `Host` unread, and forwarded headers are never trusted: it throws
|
|
181
|
-
* as it's made for an origin that isn't a bare `http(s)://host[:port]`.
|
|
182
|
-
*/
|
|
183
|
-
const toNodeHandler = (handler, options = {}) => {
|
|
184
|
-
const origin = servedOrigin(options);
|
|
185
|
-
return (incoming, response) => answerAt(handler, incoming, response, void 0, origin);
|
|
186
|
-
};
|
|
187
|
-
/**
|
|
188
|
-
* A `fetch` beside an app's own routes, as an Express middleware, which `@ref0/git/node`'s
|
|
189
|
-
* `toNodeMiddleware` hands a runtime's `Git.toFetch`, before any body parser. A
|
|
190
|
-
* request `pathOf` names is answered at the path it gives, as `toNodeHandler` answers it, the
|
|
191
|
-
* query kept; every other goes on to `next`, its body unread. `pathOf` is handed the URL `fetch`
|
|
192
|
-
* gets, with its dot segments resolved, so the path Express routed by never decides. A URL that
|
|
193
|
-
* can't be read goes on to `next` too. It calls `next` synchronously, before it returns, or not
|
|
194
|
-
* at all, so a framework's hook, as Fastify's `onRequest` is, can tell by then whether it answers.
|
|
195
|
-
* `options.origin` is read as `toNodeHandler` reads it.
|
|
196
|
-
*/
|
|
197
|
-
const toNodeMiddleware = (handler, pathOf, options = {}) => {
|
|
198
|
-
const origin = servedOrigin(options);
|
|
199
|
-
return (incoming, response, next) => {
|
|
200
|
-
let url;
|
|
201
|
-
try {
|
|
202
|
-
url = urlOf(incoming, origin);
|
|
203
|
-
} catch {
|
|
204
|
-
return next();
|
|
205
|
-
}
|
|
206
|
-
const path = pathOf(url);
|
|
207
|
-
if (path === void 0) return next();
|
|
208
|
-
url.pathname = path;
|
|
209
|
-
answerAt(handler, incoming, response, url, origin);
|
|
210
|
-
};
|
|
211
|
-
};
|
|
212
|
-
export { requestOf, respond, toNodeHandler, toNodeMiddleware };
|
package/dist/node.d.mts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { toNodeHandler, toNodeMiddleware } from "./server/edge-node.mjs";
|
|
2
|
-
import { NodeHandler, NodeMiddleware, NodeNext, NodeRequest, NodeResponse, NodeServeOptions } from "./http/node.mjs";
|
|
3
|
-
export { type NodeHandler, type NodeMiddleware, type NodeNext, type NodeRequest, type NodeResponse, type NodeServeOptions, toNodeHandler, toNodeMiddleware };
|
package/dist/node.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { toNodeHandler, toNodeMiddleware } from "./server/edge-node.mjs";
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Git } from "../contracts/git.mjs";
|
|
2
|
-
import { NodeHandler, NodeMiddleware, NodeServeOptions } from "../http/node.mjs";
|
|
3
|
-
import { Runtime } from "./edge.mjs";
|
|
4
|
-
/**
|
|
5
|
-
* A runtime over `Git.layer` as a `node:http` or `node:http2` listener or an Express route:
|
|
6
|
-
* `createServer(toNodeHandler(runtime))`. The runtime is the host's, made once a process with
|
|
7
|
-
* `Git.runtime(Git.layer(options, features).pipe(Layer.provide(adapter)))`, as docs/deploy/node.md's
|
|
8
|
-
* "Stores" builds it. Each request is answered through `Git.toFetch`, so a failed build answers
|
|
9
|
-
* `UNAVAILABLE` here too, and `starting` while it waits to build again. Behind a proxy that ends
|
|
10
|
-
* TLS, `options.origin` names the origin it's reached at, which every link Git gives out is built
|
|
11
|
-
* from; forwarded headers are never trusted, as a client can send any.
|
|
12
|
-
*/
|
|
13
|
-
export declare const toNodeHandler: (runtime: Runtime<Git, unknown>, options?: NodeServeOptions) => NodeHandler;
|
|
14
|
-
/**
|
|
15
|
-
* A runtime over `Git.layer` beside an app's own routes, as an Express middleware: it answers the
|
|
16
|
-
* paths `pathOf` names through `Git.toFetch`, and calls `next` for every other. `options.origin`
|
|
17
|
-
* is read as `toNodeHandler` reads it.
|
|
18
|
-
*/
|
|
19
|
-
export declare const toNodeMiddleware: (runtime: Runtime<Git, unknown>, pathOf: (url: URL) => string | undefined, options?: NodeServeOptions) => NodeMiddleware;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { toNodeHandler as toNodeHandler$1, toNodeMiddleware as toNodeMiddleware$1 } from "../http/node.mjs";
|
|
2
|
-
import { toFetch } from "./edge.mjs";
|
|
3
|
-
// src/server/edge-node.ts
|
|
4
|
-
/**
|
|
5
|
-
* A runtime over `Git.layer` as a `node:http` or `node:http2` listener or an Express route:
|
|
6
|
-
* `createServer(toNodeHandler(runtime))`. The runtime is the host's, made once a process with
|
|
7
|
-
* `Git.runtime(Git.layer(options, features).pipe(Layer.provide(adapter)))`, as docs/deploy/node.md's
|
|
8
|
-
* "Stores" builds it. Each request is answered through `Git.toFetch`, so a failed build answers
|
|
9
|
-
* `UNAVAILABLE` here too, and `starting` while it waits to build again. Behind a proxy that ends
|
|
10
|
-
* TLS, `options.origin` names the origin it's reached at, which every link Git gives out is built
|
|
11
|
-
* from; forwarded headers are never trusted, as a client can send any.
|
|
12
|
-
*/
|
|
13
|
-
const toNodeHandler = (runtime, options) => toNodeHandler$1({ fetch: toFetch(runtime) }, options);
|
|
14
|
-
/**
|
|
15
|
-
* A runtime over `Git.layer` beside an app's own routes, as an Express middleware: it answers the
|
|
16
|
-
* paths `pathOf` names through `Git.toFetch`, and calls `next` for every other. `options.origin`
|
|
17
|
-
* is read as `toNodeHandler` reads it.
|
|
18
|
-
*/
|
|
19
|
-
const toNodeMiddleware = (runtime, pathOf, options) => toNodeMiddleware$1({ fetch: toFetch(runtime) }, pathOf, options);
|
|
20
|
-
export { toNodeHandler, toNodeMiddleware };
|