@flowscripter/io-gateway-http 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/src/HttpGateway.d.ts +39 -0
- package/dist/src/HttpGateway.d.ts.map +1 -0
- package/dist/src/HttpGateway.js +106 -0
- package/dist/src/http/parseRange.d.ts +18 -0
- package/dist/src/http/parseRange.d.ts.map +1 -0
- package/dist/src/http/parseRange.js +41 -0
- package/dist/src/http/resolveTokenPath.d.ts +16 -0
- package/dist/src/http/resolveTokenPath.d.ts.map +1 -0
- package/dist/src/http/resolveTokenPath.js +30 -0
- package/dist/src/http/statusForError.d.ts +7 -0
- package/dist/src/http/statusForError.d.ts.map +1 -0
- package/dist/src/http/statusForError.js +15 -0
- package/dist/src/stream/createResumingReadStream.d.ts +19 -0
- package/dist/src/stream/createResumingReadStream.d.ts.map +1 -0
- package/dist/src/stream/createResumingReadStream.js +46 -0
- package/dist/src/token/GatewayGrant.d.ts +36 -0
- package/dist/src/token/GatewayGrant.d.ts.map +1 -0
- package/dist/src/token/GatewayGrant.js +234 -0
- package/dist/src/token/WriteTracker.d.ts +19 -0
- package/dist/src/token/WriteTracker.d.ts.map +1 -0
- package/dist/src/token/WriteTracker.js +40 -0
- package/dist/src/token/keys.d.ts +6 -0
- package/dist/src/token/keys.d.ts.map +1 -0
- package/dist/src/token/keys.js +16 -0
- package/index.ts +3 -0
- package/package.json +64 -4
- package/src/HttpGateway.ts +142 -0
- package/src/http/parseRange.ts +47 -0
- package/src/http/resolveTokenPath.ts +34 -0
- package/src/http/statusForError.ts +16 -0
- package/src/stream/createResumingReadStream.ts +61 -0
- package/src/token/GatewayGrant.ts +294 -0
- package/src/token/WriteTracker.ts +47 -0
- package/src/token/keys.ts +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Flowscripter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,84 @@
|
|
|
1
|
-
#
|
|
1
|
+
# io-gateway-http
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/flowscripter/io-gateway-http/releases)
|
|
4
|
+
[](https://github.com/flowscripter/io-gateway-http/actions/workflows/release-bun-library.yml)
|
|
5
|
+
[](https://flowscripter.github.io/io-gateway-http/index.html)
|
|
6
|
+
[](https://github.com/flowscripter/io-gateway-http/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
> A local HTTP gateway serving
|
|
9
|
+
> [pluggable-io-framework](https://github.com/flowscripter/pluggable-io-framework)
|
|
10
|
+
> locations to tools that only speak HTTP, such as ffmpeg, ffprobe and mediainfo
|
|
11
|
+
|
|
12
|
+
## Key Features
|
|
13
|
+
|
|
14
|
+
- Serves any `js` IOProvider location, so a tool can read from and write to
|
|
15
|
+
`s3:`, `http(s):`, `file:` or a composite provider without knowing about
|
|
16
|
+
them.
|
|
17
|
+
- Listens on a loopback ephemeral port. Access is by grant: each grant has a
|
|
18
|
+
random token as the first path segment (`http://127.0.0.1:<port>/<token>/<path>`),
|
|
19
|
+
so relative references such as HLS playlists, image sequences and segment
|
|
20
|
+
outputs resolve against the grant root with no client configuration.
|
|
21
|
+
- A grant is a root (a container or a single entry) plus a scope, `read` or
|
|
22
|
+
`read-write`. Paths are decoded and normalised, and anything that would
|
|
23
|
+
escape the root is rejected.
|
|
24
|
+
- Request mapping:
|
|
25
|
+
- `HEAD` reports size, last modified, content type and etag. `Accept-Ranges: bytes` is only
|
|
26
|
+
sent when the handle is range readable.
|
|
27
|
+
- `GET` with a single range (`a-b`, `a-`, `-n`) answers `206` or `416`. A multi-range or
|
|
28
|
+
malformed `bytes` range, or one starting past the end, answers `416` with
|
|
29
|
+
`Content-Range: bytes */<size>`. Other units, and entries that are not range
|
|
30
|
+
readable, serve the whole entry.
|
|
31
|
+
- `PUT` and `POST` stream the request body to the provider, chunked or not.
|
|
32
|
+
- `DELETE` calls the provider's `delete`.
|
|
33
|
+
- Unbounded sources are streamed as a chunked `200` with `Accept-Ranges: none`.
|
|
34
|
+
- Reads are lazy with backpressure, and a client disconnect cancels the
|
|
35
|
+
provider read, so a seek (dropping an open-ended range request and opening a
|
|
36
|
+
new one) does not cost the data it skipped.
|
|
37
|
+
- Responses are never capped below the requested range, because curl-based
|
|
38
|
+
clients treat a short `206` as the end of the file.
|
|
39
|
+
- `Connection: close` is echoed when the client sends it, and keep-alive is
|
|
40
|
+
supported.
|
|
41
|
+
- Transient provider failures are retried by the gateway with `withRetry`,
|
|
42
|
+
including resuming a read part way through, so the client does not see them.
|
|
43
|
+
- Every write and delete is recorded per grant. A tool's exit code says
|
|
44
|
+
nothing about whether its uploads committed, so wait for `settled()` and take
|
|
45
|
+
success or failure from the results.
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { HttpGateway } from "@flowscripter/io-gateway-http";
|
|
51
|
+
|
|
52
|
+
const gateway = HttpGateway.start();
|
|
53
|
+
|
|
54
|
+
// `registry` is a pluggable-io-framework ProviderResolver
|
|
55
|
+
const grant = await gateway.grantLocation(registry, "s3://bucket/media/", {
|
|
56
|
+
scope: "read-write",
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// pass grant.urlFor("input.mkv") and grant.urlFor("out/playlist.m3u8") to the tool
|
|
60
|
+
|
|
61
|
+
const results = await grant.settled();
|
|
62
|
+
const failed = results.filter((result) => result.error !== undefined);
|
|
63
|
+
|
|
64
|
+
await grant.revoke();
|
|
65
|
+
await gateway.close();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`grant(provider, target, options)` serves a provider the caller already owns.
|
|
69
|
+
`grantLocation(resolver, location, options)` resolves a `js` provider for the
|
|
70
|
+
location and disposes it when the grant is revoked.
|
|
71
|
+
|
|
72
|
+
## Future Work
|
|
73
|
+
|
|
74
|
+
- Serve `native` payload providers by converting their payloads to bytes. The
|
|
75
|
+
gateway only serves `js` providers today.
|
|
76
|
+
|
|
77
|
+
## Further Details
|
|
78
|
+
|
|
79
|
+
- [Development](./README/development.md)
|
|
80
|
+
- [API Documentation](https://flowscripter.github.io/io-gateway-http/index.html)
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT © Flowscripter
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { HttpGateway, type GrantOptions, type HttpGatewayOptions } from "./src/HttpGateway.ts";
|
|
2
|
+
export { GatewayGrant, type GrantScope } from "./src/token/GatewayGrant.ts";
|
|
3
|
+
export { type WriteResult } from "./src/token/WriteTracker.ts";
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,YAAY,EAAE,KAAK,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/F,OAAO,EAAE,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,6BAA6B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type IOProvider, type LocationTarget, PayloadKind, type ProviderResolver } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
import { type RetryOptions } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
import { GatewayGrant, type GrantScope } from "./token/GatewayGrant.ts";
|
|
4
|
+
export interface HttpGatewayOptions {
|
|
5
|
+
/** Retries for transient provider failures, applied by the gateway so the HTTP client never sees them. */
|
|
6
|
+
readonly retry?: RetryOptions;
|
|
7
|
+
}
|
|
8
|
+
export interface GrantOptions {
|
|
9
|
+
readonly scope: GrantScope;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A local HTTP server on a loopback ephemeral port that serves
|
|
13
|
+
* IOProvider locations. Access is by grant: each grant has a random token as
|
|
14
|
+
* the first path segment, so relative references below the grant URL resolve
|
|
15
|
+
* against its root and no client configuration is needed.
|
|
16
|
+
*/
|
|
17
|
+
export declare class HttpGateway {
|
|
18
|
+
#private;
|
|
19
|
+
private constructor();
|
|
20
|
+
static start(options?: HttpGatewayOptions): HttpGateway;
|
|
21
|
+
get origin(): string;
|
|
22
|
+
/**
|
|
23
|
+
* Serves a container or single entry of `provider`. The provider must be
|
|
24
|
+
* of the `js` payload kind and stays owned by the caller.
|
|
25
|
+
*/
|
|
26
|
+
grant(provider: IOProvider<PayloadKind.Js>, target: LocationTarget, options: GrantOptions): GatewayGrant;
|
|
27
|
+
/**
|
|
28
|
+
* Resolves `location` to a `js` provider and serves its target. The grant
|
|
29
|
+
* owns the provider and disposes it when revoked.
|
|
30
|
+
*/
|
|
31
|
+
grantLocation(resolver: ProviderResolver, location: string, options: GrantOptions): Promise<GatewayGrant>;
|
|
32
|
+
/**
|
|
33
|
+
* Revokes every grant, waits for in-flight writes to settle, then stops the
|
|
34
|
+
* server: responses in progress get `graceMs` to finish before their
|
|
35
|
+
* connections are closed.
|
|
36
|
+
*/
|
|
37
|
+
close(graceMs?: number): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=HttpGateway.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpGateway.d.ts","sourceRoot":"","sources":["../../src/HttpGateway.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,WAAW,EACX,KAAK,gBAAgB,EACtB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAiB,KAAK,YAAY,EAAE,MAAM,sCAAsC,CAAC;AAExF,OAAO,EAAE,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAExE,MAAM,WAAW,kBAAkB;IACjC,0GAA0G;IAC1G,QAAQ,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC;CAC/B;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;CAC5B;AAMD;;;;;GAKG;AACH,qBAAa,WAAW;;IAKtB,OAAO,eASN;IAED,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,WAAW,CAEtD;IAED,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;OAGG;IACH,KAAK,CACH,QAAQ,EAAE,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC,EACpC,MAAM,EAAE,cAAc,EACtB,OAAO,EAAE,YAAY,GACpB,YAAY,CAEd;IAED;;;OAGG;IACG,aAAa,CACjB,QAAQ,EAAE,gBAAgB,EAC1B,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,YAAY,GACpB,OAAO,CAAC,YAAY,CAAC,CAUvB;IAED;;;;OAIG;IACG,KAAK,CAAC,OAAO,SAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CASzC;CA0CF"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { PayloadKind, } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
import { DEFAULT_RETRY } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
import { resolveTokenPath } from "./http/resolveTokenPath.js";
|
|
4
|
+
import { GatewayGrant } from "./token/GatewayGrant.js";
|
|
5
|
+
function randomToken() {
|
|
6
|
+
return Buffer.from(crypto.getRandomValues(new Uint8Array(24))).toString("base64url");
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A local HTTP server on a loopback ephemeral port that serves
|
|
10
|
+
* IOProvider locations. Access is by grant: each grant has a random token as
|
|
11
|
+
* the first path segment, so relative references below the grant URL resolve
|
|
12
|
+
* against its root and no client configuration is needed.
|
|
13
|
+
*/
|
|
14
|
+
export class HttpGateway {
|
|
15
|
+
#server;
|
|
16
|
+
#retry;
|
|
17
|
+
#grants = new Map();
|
|
18
|
+
constructor(retry) {
|
|
19
|
+
this.#retry = retry;
|
|
20
|
+
this.#server = Bun.serve({
|
|
21
|
+
hostname: "127.0.0.1",
|
|
22
|
+
port: 0,
|
|
23
|
+
idleTimeout: 0,
|
|
24
|
+
maxRequestBodySize: Number.MAX_SAFE_INTEGER,
|
|
25
|
+
fetch: (request) => this.#handle(request),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
static start(options) {
|
|
29
|
+
return new HttpGateway(options?.retry ?? DEFAULT_RETRY);
|
|
30
|
+
}
|
|
31
|
+
get origin() {
|
|
32
|
+
return `http://127.0.0.1:${this.#server.port}`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Serves a container or single entry of `provider`. The provider must be
|
|
36
|
+
* of the `js` payload kind and stays owned by the caller.
|
|
37
|
+
*/
|
|
38
|
+
grant(provider, target, options) {
|
|
39
|
+
return this.#addGrant(provider, target, options, false);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Resolves `location` to a `js` provider and serves its target. The grant
|
|
43
|
+
* owns the provider and disposes it when revoked.
|
|
44
|
+
*/
|
|
45
|
+
async grantLocation(resolver, location, options) {
|
|
46
|
+
const { provider, target } = await resolver.createProviderForLocation(location, {
|
|
47
|
+
kind: PayloadKind.Js,
|
|
48
|
+
});
|
|
49
|
+
try {
|
|
50
|
+
return this.#addGrant(provider, target, options, true);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
await provider[Symbol.asyncDispose]();
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Revokes every grant, waits for in-flight writes to settle, then stops the
|
|
59
|
+
* server: responses in progress get `graceMs` to finish before their
|
|
60
|
+
* connections are closed.
|
|
61
|
+
*/
|
|
62
|
+
async close(graceMs = 1000) {
|
|
63
|
+
const grants = [...this.#grants.values()];
|
|
64
|
+
await Promise.all(grants.map((grant) => grant.revoke()));
|
|
65
|
+
await Promise.all(grants.map((grant) => grant.settled()));
|
|
66
|
+
this.#grants.clear();
|
|
67
|
+
const stopping = this.#server.stop();
|
|
68
|
+
const timer = setTimeout(() => void this.#server.stop(true), graceMs);
|
|
69
|
+
await stopping;
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
}
|
|
72
|
+
#addGrant(provider, target, options, ownsProvider) {
|
|
73
|
+
if (provider.kind !== PayloadKind.Js) {
|
|
74
|
+
throw new TypeError(`The gateway serves js providers, not "${provider.kind}"`);
|
|
75
|
+
}
|
|
76
|
+
if (target.kind === "pattern") {
|
|
77
|
+
throw new TypeError("A pattern location cannot be served; use its container");
|
|
78
|
+
}
|
|
79
|
+
const token = randomToken();
|
|
80
|
+
const grant = new GatewayGrant({
|
|
81
|
+
provider: provider,
|
|
82
|
+
target,
|
|
83
|
+
scope: options.scope,
|
|
84
|
+
retry: this.#retry,
|
|
85
|
+
origin: this.origin,
|
|
86
|
+
token,
|
|
87
|
+
ownsProvider,
|
|
88
|
+
});
|
|
89
|
+
this.#grants.set(token, grant);
|
|
90
|
+
return grant;
|
|
91
|
+
}
|
|
92
|
+
async #handle(request) {
|
|
93
|
+
const resolved = resolveTokenPath(new URL(request.url).pathname);
|
|
94
|
+
if (resolved.kind === "malformed") {
|
|
95
|
+
return new Response("Bad Request", { status: 400 });
|
|
96
|
+
}
|
|
97
|
+
const grant = resolved.kind === "ok" ? this.#grants.get(resolved.token) : undefined;
|
|
98
|
+
if (resolved.kind === "escape") {
|
|
99
|
+
return new Response("Forbidden", { status: 403 });
|
|
100
|
+
}
|
|
101
|
+
if (grant === undefined || grant.revoked) {
|
|
102
|
+
return new Response("Not Found", { status: 404 });
|
|
103
|
+
}
|
|
104
|
+
return grant.handle(request, resolved.segments);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type RangeRequest = {
|
|
2
|
+
readonly kind: "none";
|
|
3
|
+
} | {
|
|
4
|
+
readonly kind: "range";
|
|
5
|
+
readonly start: number;
|
|
6
|
+
readonly end: number;
|
|
7
|
+
} | {
|
|
8
|
+
readonly kind: "unsatisfiable";
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Parses a `Range` header against an entry of `size` bytes into a byte range
|
|
12
|
+
* with an exclusive `end`. A missing header or a malformed or multi-range
|
|
13
|
+
* header, or one in a unit other than bytes, is `none` (the whole entry is
|
|
14
|
+
* served). A bytes header that is malformed, holds several ranges or starts at
|
|
15
|
+
* or past `size` is `unsatisfiable`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseRange(header: string | null, size: number): RangeRequest;
|
|
18
|
+
//# sourceMappingURL=parseRange.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseRange.d.ts","sourceRoot":"","sources":["../../../src/http/parseRange.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GACpB;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACzB;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GACxE;IAAE,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAA;CAAE,CAAC;AAIvC;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAgC5E"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const SINGLE_RANGE = /^bytes=(\d*)-(\d*)$/;
|
|
2
|
+
/**
|
|
3
|
+
* Parses a `Range` header against an entry of `size` bytes into a byte range
|
|
4
|
+
* with an exclusive `end`. A missing header or a malformed or multi-range
|
|
5
|
+
* header, or one in a unit other than bytes, is `none` (the whole entry is
|
|
6
|
+
* served). A bytes header that is malformed, holds several ranges or starts at
|
|
7
|
+
* or past `size` is `unsatisfiable`.
|
|
8
|
+
*/
|
|
9
|
+
export function parseRange(header, size) {
|
|
10
|
+
if (header === null) {
|
|
11
|
+
return { kind: "none" };
|
|
12
|
+
}
|
|
13
|
+
const value = header.trim();
|
|
14
|
+
if (!/^bytes\s*=/i.test(value)) {
|
|
15
|
+
return { kind: "none" };
|
|
16
|
+
}
|
|
17
|
+
const match = SINGLE_RANGE.exec(value);
|
|
18
|
+
if (match === null || (match[1] === "" && match[2] === "")) {
|
|
19
|
+
return { kind: "unsatisfiable" };
|
|
20
|
+
}
|
|
21
|
+
const [, first, last] = match;
|
|
22
|
+
if (first === "") {
|
|
23
|
+
const suffix = Number(last);
|
|
24
|
+
if (suffix === 0) {
|
|
25
|
+
return { kind: "unsatisfiable" };
|
|
26
|
+
}
|
|
27
|
+
return { kind: "range", start: Math.max(0, size - suffix), end: size };
|
|
28
|
+
}
|
|
29
|
+
const start = Number(first);
|
|
30
|
+
if (start >= size) {
|
|
31
|
+
return { kind: "unsatisfiable" };
|
|
32
|
+
}
|
|
33
|
+
if (last === "") {
|
|
34
|
+
return { kind: "range", start, end: size };
|
|
35
|
+
}
|
|
36
|
+
const inclusiveEnd = Number(last);
|
|
37
|
+
if (inclusiveEnd < start) {
|
|
38
|
+
return { kind: "unsatisfiable" };
|
|
39
|
+
}
|
|
40
|
+
return { kind: "range", start, end: Math.min(inclusiveEnd + 1, size) };
|
|
41
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type TokenPath = {
|
|
2
|
+
readonly kind: "ok";
|
|
3
|
+
readonly token: string;
|
|
4
|
+
readonly segments: readonly string[];
|
|
5
|
+
} | {
|
|
6
|
+
readonly kind: "malformed";
|
|
7
|
+
} | {
|
|
8
|
+
readonly kind: "escape";
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Splits a request pathname into its token and the decoded path segments
|
|
12
|
+
* below it. Empty and `.` segments are dropped. A `..` segment, or a
|
|
13
|
+
* decoded segment containing a path separator or NUL, is an `escape`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveTokenPath(pathname: string): TokenPath;
|
|
16
|
+
//# sourceMappingURL=resolveTokenPath.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveTokenPath.d.ts","sourceRoot":"","sources":["../../../src/http/resolveTokenPath.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GACjB;IAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,GACrF;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE,GAC9B;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC;AAEhC;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAuB5D"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits a request pathname into its token and the decoded path segments
|
|
3
|
+
* below it. Empty and `.` segments are dropped. A `..` segment, or a
|
|
4
|
+
* decoded segment containing a path separator or NUL, is an `escape`.
|
|
5
|
+
*/
|
|
6
|
+
export function resolveTokenPath(pathname) {
|
|
7
|
+
const raw = pathname.split("/").filter((segment) => segment !== "");
|
|
8
|
+
const token = raw.shift();
|
|
9
|
+
if (token === undefined) {
|
|
10
|
+
return { kind: "malformed" };
|
|
11
|
+
}
|
|
12
|
+
const segments = [];
|
|
13
|
+
for (const part of raw) {
|
|
14
|
+
let segment;
|
|
15
|
+
try {
|
|
16
|
+
segment = decodeURIComponent(part);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return { kind: "malformed" };
|
|
20
|
+
}
|
|
21
|
+
if (segment === ".") {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (segment === ".." || /[/\\\0]/.test(segment)) {
|
|
25
|
+
return { kind: "escape" };
|
|
26
|
+
}
|
|
27
|
+
segments.push(segment);
|
|
28
|
+
}
|
|
29
|
+
return { kind: "ok", token, segments };
|
|
30
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTTP status for a provider failure. A permanent failure reading or
|
|
3
|
+
* deleting is reported as not found; a permanent write failure and a
|
|
4
|
+
* transient failure that outlived its retries are an upstream error.
|
|
5
|
+
*/
|
|
6
|
+
export declare function statusForError(error: unknown, operation: "read" | "write"): number;
|
|
7
|
+
//# sourceMappingURL=statusForError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"statusForError.d.ts","sourceRoot":"","sources":["../../../src/http/statusForError.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,CAQlF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PermanentIOError, TransientIOError } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
/**
|
|
3
|
+
* The HTTP status for a provider failure. A permanent failure reading or
|
|
4
|
+
* deleting is reported as not found; a permanent write failure and a
|
|
5
|
+
* transient failure that outlived its retries are an upstream error.
|
|
6
|
+
*/
|
|
7
|
+
export function statusForError(error, operation) {
|
|
8
|
+
if (error instanceof PermanentIOError) {
|
|
9
|
+
return operation === "read" ? 404 : 502;
|
|
10
|
+
}
|
|
11
|
+
if (error instanceof TransientIOError) {
|
|
12
|
+
return 502;
|
|
13
|
+
}
|
|
14
|
+
return 500;
|
|
15
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Item, PayloadKind } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
import { type RetryOptions } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
export interface ResumingReadOptions {
|
|
4
|
+
readonly initial: ReadableStream<Item<PayloadKind.Js>>;
|
|
5
|
+
/** Byte offset in the entry at which `initial` starts. */
|
|
6
|
+
readonly startOffset: number;
|
|
7
|
+
/** Opens a stream from the given byte offset, or omit to disable resuming. */
|
|
8
|
+
readonly reopen?: (offset: number) => Promise<ReadableStream<Item<PayloadKind.Js>>>;
|
|
9
|
+
readonly retry: RetryOptions;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A pull-based byte stream over a provider read. When the provider fails
|
|
13
|
+
* transiently part way through, the read is reopened at the first byte not
|
|
14
|
+
* yet delivered, so the HTTP client never sees the failure. Consecutive
|
|
15
|
+
* failures are counted against `retry.maxRetries` and reset once data arrives.
|
|
16
|
+
* Cancelling the returned stream cancels the provider stream.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createResumingReadStream(options: ResumingReadOptions): ReadableStream<Uint8Array>;
|
|
19
|
+
//# sourceMappingURL=createResumingReadStream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createResumingReadStream.d.ts","sourceRoot":"","sources":["../../../src/stream/createResumingReadStream.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,IAAI,EACT,WAAW,EAGZ,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAW,KAAK,YAAY,EAAE,MAAM,sCAAsC,CAAC;AAElF,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACvD,0DAA0D;IAC1D,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,8EAA8E;IAC9E,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IACpF,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;CAC9B;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,mBAAmB,GAAG,cAAc,CAAC,UAAU,CAAC,CAoCjG"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { PayloadKind, toWebReadableStream, TransientIOError, } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
import { backoff } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
/**
|
|
4
|
+
* A pull-based byte stream over a provider read. When the provider fails
|
|
5
|
+
* transiently part way through, the read is reopened at the first byte not
|
|
6
|
+
* yet delivered, so the HTTP client never sees the failure. Consecutive
|
|
7
|
+
* failures are counted against `retry.maxRetries` and reset once data arrives.
|
|
8
|
+
* Cancelling the returned stream cancels the provider stream.
|
|
9
|
+
*/
|
|
10
|
+
export function createResumingReadStream(options) {
|
|
11
|
+
const { reopen, retry } = options;
|
|
12
|
+
let offset = options.startOffset;
|
|
13
|
+
let failures = 0;
|
|
14
|
+
let reader = toWebReadableStream(options.initial).getReader();
|
|
15
|
+
return new ReadableStream({
|
|
16
|
+
async pull(controller) {
|
|
17
|
+
for (;;) {
|
|
18
|
+
try {
|
|
19
|
+
const { done, value } = await reader.read();
|
|
20
|
+
if (done) {
|
|
21
|
+
controller.close();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
failures = 0;
|
|
25
|
+
offset += value.byteLength;
|
|
26
|
+
controller.enqueue(value);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (reopen === undefined || !(error instanceof TransientIOError)) {
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
failures += 1;
|
|
34
|
+
if (failures > retry.maxRetries) {
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
await backoff(retry, failures);
|
|
38
|
+
reader = toWebReadableStream(await reopen(offset)).getReader();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
cancel(reason) {
|
|
43
|
+
return reader.cancel(reason);
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type IOProvider, type LocationTarget, PayloadKind } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
import { type RetryOptions } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
import { type WriteResult } from "./WriteTracker.ts";
|
|
4
|
+
export type GrantScope = "read" | "read-write";
|
|
5
|
+
export interface GrantInit {
|
|
6
|
+
readonly provider: IOProvider<PayloadKind.Js>;
|
|
7
|
+
readonly target: Extract<LocationTarget, {
|
|
8
|
+
kind: "entry" | "container";
|
|
9
|
+
}>;
|
|
10
|
+
readonly scope: GrantScope;
|
|
11
|
+
readonly retry: RetryOptions;
|
|
12
|
+
readonly origin: string;
|
|
13
|
+
readonly token: string;
|
|
14
|
+
/** Whether revoking the grant disposes the provider. */
|
|
15
|
+
readonly ownsProvider: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Serves one root location (a container or a single entry) over HTTP,
|
|
19
|
+
* restricted to a scope. Created by `HttpGateway`; revoking it makes
|
|
20
|
+
* every later request a 404.
|
|
21
|
+
*/
|
|
22
|
+
export declare class GatewayGrant {
|
|
23
|
+
#private;
|
|
24
|
+
constructor(init: GrantInit);
|
|
25
|
+
/** The URL of the root: the container with a trailing slash, or the single entry. */
|
|
26
|
+
get url(): string;
|
|
27
|
+
/** The URL of a path below a container root. */
|
|
28
|
+
urlFor(path: string): string;
|
|
29
|
+
get revoked(): boolean;
|
|
30
|
+
/** Resolves with every write and delete result once none is in flight. */
|
|
31
|
+
settled(): Promise<readonly WriteResult[]>;
|
|
32
|
+
/** Stops serving requests. Writes already in flight still run to completion. */
|
|
33
|
+
revoke(): Promise<void>;
|
|
34
|
+
handle(request: Request, segments: readonly string[]): Promise<Response>;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=GatewayGrant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GatewayGrant.d.ts","sourceRoot":"","sources":["../../../src/token/GatewayGrant.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EAGf,KAAK,cAAc,EACnB,WAAW,EAIZ,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAE,KAAK,YAAY,EAAa,MAAM,sCAAsC,CAAC;AAKpF,OAAO,EAAgB,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEnE,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,YAAY,CAAC;AAE/C,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;IAC9C,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,cAAc,EAAE;QAAE,IAAI,EAAE,OAAO,GAAG,WAAW,CAAA;KAAE,CAAC,CAAC;IAC1E,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,wDAAwD;IACxD,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;CAChC;AAUD;;;;GAIG;AACH,qBAAa,YAAY;;IAMvB,YAAY,IAAI,EAAE,SAAS,EAE1B;IAED,qFAAqF;IACrF,IAAI,GAAG,IAAI,MAAM,CAIhB;IAED,gDAAgD;IAChD,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAU3B;IAED,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,0EAA0E;IAC1E,OAAO,IAAI,OAAO,CAAC,SAAS,WAAW,EAAE,CAAC,CAEzC;IAED,gFAAgF;IAC1E,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAU5B;IAEK,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAkB7E;CAoLF"}
|