@effect-vfs/nfs 0.1.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 +22 -0
- package/README.md +93 -0
- package/dist/NfsServer.d.ts +693 -0
- package/dist/NfsServer.d.ts.map +1 -0
- package/dist/NfsServer.js +439 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/internal/export.d.ts +77 -0
- package/dist/internal/export.d.ts.map +1 -0
- package/dist/internal/export.js +224 -0
- package/dist/internal/lockRanges.d.ts +16 -0
- package/dist/internal/lockRanges.d.ts.map +1 -0
- package/dist/internal/lockRanges.js +12 -0
- package/dist/internal/nfs4.d.ts +194 -0
- package/dist/internal/nfs4.d.ts.map +1 -0
- package/dist/internal/nfs4.js +3561 -0
- package/dist/internal/recordMarking.d.ts +29 -0
- package/dist/internal/recordMarking.d.ts.map +1 -0
- package/dist/internal/recordMarking.js +127 -0
- package/dist/internal/rpc.d.ts +73 -0
- package/dist/internal/rpc.d.ts.map +1 -0
- package/dist/internal/rpc.js +161 -0
- package/dist/internal/server.d.ts +17 -0
- package/dist/internal/server.d.ts.map +1 -0
- package/dist/internal/server.js +78 -0
- package/dist/internal/xdr.d.ts +51 -0
- package/dist/internal/xdr.d.ts.map +1 -0
- package/dist/internal/xdr.js +178 -0
- package/package.json +54 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export interface RecordLimits {
|
|
3
|
+
readonly maxFragmentBytes: ByteSize.ByteSize;
|
|
4
|
+
readonly maxRecordBytes: ByteSize.ByteSize;
|
|
5
|
+
readonly maxFragmentsPerRecord: number;
|
|
6
|
+
}
|
|
7
|
+
declare const RecordMarkingError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
8
|
+
readonly _tag: "RecordMarkingError";
|
|
9
|
+
} & Readonly<A>;
|
|
10
|
+
/** @internal */
|
|
11
|
+
export declare class RecordMarkingError extends RecordMarkingError_base<{
|
|
12
|
+
readonly detail: string;
|
|
13
|
+
}> {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
get message(): string;
|
|
16
|
+
}
|
|
17
|
+
/** @internal */
|
|
18
|
+
export declare class RecordDecoder {
|
|
19
|
+
#private;
|
|
20
|
+
constructor(limits: RecordLimits);
|
|
21
|
+
get bufferedByteLength(): number;
|
|
22
|
+
reset(): void;
|
|
23
|
+
push(input: Uint8Array): ReadonlyArray<Uint8Array>;
|
|
24
|
+
}
|
|
25
|
+
/** @internal */
|
|
26
|
+
export declare const encodeRecord: (record: Uint8Array) => Uint8Array;
|
|
27
|
+
import * as ByteSize from "effect/ByteSize";
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=recordMarking.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recordMarking.d.ts","sourceRoot":"","sources":["../../src/internal/recordMarking.ts"],"names":[],"mappings":"AAAA,gBAAgB;AAChB,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,gBAAgB,EAAE,QAAQ,CAAC,QAAQ,CAAA;IAC5C,QAAQ,CAAC,cAAc,EAAE,QAAQ,CAAC,QAAQ,CAAA;IAC1C,QAAQ,CAAC,qBAAqB,EAAE,MAAM,CAAA;CACvC;;;;AAED,gBAAgB;AAChB,qBAAa,kBAAmB,SAAQ,wBAAuC;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;IACzG,YAAY,OAAO,EAAE,MAAM,EAE1B;IAED,IAAa,OAAO,IAAI,MAAM,CAE7B;CACF;AAkBD,gBAAgB;AAChB,qBAAa,aAAa;;IAYxB,YAAY,MAAM,EAAE,YAAY,EAK/B;IAED,IAAI,kBAAkB,IAAI,MAAM,CAE/B;IACD,KAAK,IAAI,IAAI,CASZ;IAED,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,aAAa,CAAC,UAAU,CAAC,CAkDjD;CAmBF;AAED,gBAAgB;AAChB,eAAO,MAAM,YAAY,WAAY,UAAU,KAAG,UAOjD,CAAA;AAED,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export class RecordMarkingError extends Data.TaggedError("RecordMarkingError") {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super({ detail: message });
|
|
5
|
+
}
|
|
6
|
+
get message() {
|
|
7
|
+
return this.detail;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const validate = (name, value) => {
|
|
11
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
12
|
+
throw new RangeError(`${name} must be a non-negative safe integer`);
|
|
13
|
+
};
|
|
14
|
+
const join = (chunks, size) => {
|
|
15
|
+
const result = new Uint8Array(size);
|
|
16
|
+
let offset = 0;
|
|
17
|
+
for (const chunk of chunks) {
|
|
18
|
+
result.set(chunk, offset);
|
|
19
|
+
offset += chunk.length;
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
/** @internal */
|
|
24
|
+
export class RecordDecoder {
|
|
25
|
+
#limits;
|
|
26
|
+
#header = new Uint8Array(4);
|
|
27
|
+
#headerLength = 0;
|
|
28
|
+
#fragmentLength = -1;
|
|
29
|
+
#fragmentLast = false;
|
|
30
|
+
#fragmentChunks = [];
|
|
31
|
+
#fragmentBytes = 0;
|
|
32
|
+
#recordFragments = [];
|
|
33
|
+
#recordBytes = 0;
|
|
34
|
+
#recordFragmentCount = 0;
|
|
35
|
+
constructor(limits) {
|
|
36
|
+
validate("maxFragmentBytes", ByteSize.toNumberUnsafe(limits.maxFragmentBytes));
|
|
37
|
+
validate("maxRecordBytes", ByteSize.toNumberUnsafe(limits.maxRecordBytes));
|
|
38
|
+
validate("maxFragmentsPerRecord", limits.maxFragmentsPerRecord);
|
|
39
|
+
this.#limits = limits;
|
|
40
|
+
}
|
|
41
|
+
get bufferedByteLength() {
|
|
42
|
+
return this.#headerLength + this.#fragmentBytes + this.#recordBytes;
|
|
43
|
+
}
|
|
44
|
+
reset() {
|
|
45
|
+
this.#headerLength = 0;
|
|
46
|
+
this.#fragmentLength = -1;
|
|
47
|
+
this.#fragmentLast = false;
|
|
48
|
+
this.#fragmentChunks = [];
|
|
49
|
+
this.#fragmentBytes = 0;
|
|
50
|
+
this.#recordFragments = [];
|
|
51
|
+
this.#recordBytes = 0;
|
|
52
|
+
this.#recordFragmentCount = 0;
|
|
53
|
+
}
|
|
54
|
+
push(input) {
|
|
55
|
+
const records = [];
|
|
56
|
+
let offset = 0;
|
|
57
|
+
try {
|
|
58
|
+
while (offset < input.length) {
|
|
59
|
+
if (this.#fragmentLength < 0) {
|
|
60
|
+
const count = Math.min(4 - this.#headerLength, input.length - offset);
|
|
61
|
+
this.#header.set(input.subarray(offset, offset + count), this.#headerLength);
|
|
62
|
+
this.#headerLength += count;
|
|
63
|
+
offset += count;
|
|
64
|
+
if (this.#headerLength < 4)
|
|
65
|
+
continue;
|
|
66
|
+
const marker = new DataView(this.#header.buffer).getUint32(0);
|
|
67
|
+
this.#fragmentLast = (marker & 0x8000_0000) !== 0;
|
|
68
|
+
this.#fragmentLength = marker & 0x7fff_ffff;
|
|
69
|
+
this.#headerLength = 0;
|
|
70
|
+
if (BigInt(this.#fragmentLength) > this.#limits.maxFragmentBytes) {
|
|
71
|
+
throw new RecordMarkingError("RPC fragment exceeds its byte limit");
|
|
72
|
+
}
|
|
73
|
+
if (BigInt(this.#recordBytes + this.#fragmentLength) > this.#limits.maxRecordBytes) {
|
|
74
|
+
throw new RecordMarkingError("RPC record exceeds its byte limit");
|
|
75
|
+
}
|
|
76
|
+
if (this.#recordFragmentCount >= this.#limits.maxFragmentsPerRecord) {
|
|
77
|
+
throw new RecordMarkingError("RPC record exceeds its fragment limit");
|
|
78
|
+
}
|
|
79
|
+
if (this.#fragmentLength === 0)
|
|
80
|
+
this.#finishFragment(records);
|
|
81
|
+
}
|
|
82
|
+
if (this.#fragmentLength < 0)
|
|
83
|
+
continue;
|
|
84
|
+
const count = Math.min(this.#fragmentLength - this.#fragmentBytes, input.length - offset);
|
|
85
|
+
if (count > 0) {
|
|
86
|
+
this.#fragmentChunks.push(input.slice(offset, offset + count));
|
|
87
|
+
this.#fragmentBytes += count;
|
|
88
|
+
offset += count;
|
|
89
|
+
}
|
|
90
|
+
if (this.#fragmentBytes === this.#fragmentLength)
|
|
91
|
+
this.#finishFragment(records);
|
|
92
|
+
}
|
|
93
|
+
return records;
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
this.reset();
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
#finishFragment(records) {
|
|
101
|
+
const fragment = join(this.#fragmentChunks, this.#fragmentBytes);
|
|
102
|
+
this.#recordFragments.push(fragment);
|
|
103
|
+
this.#recordFragmentCount += 1;
|
|
104
|
+
this.#recordBytes += fragment.length;
|
|
105
|
+
const last = this.#fragmentLast;
|
|
106
|
+
this.#fragmentLength = -1;
|
|
107
|
+
this.#fragmentChunks = [];
|
|
108
|
+
this.#fragmentBytes = 0;
|
|
109
|
+
if (last) {
|
|
110
|
+
records.push(join(this.#recordFragments, this.#recordBytes));
|
|
111
|
+
this.#recordFragments = [];
|
|
112
|
+
this.#recordBytes = 0;
|
|
113
|
+
this.#recordFragmentCount = 0;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** @internal */
|
|
118
|
+
export const encodeRecord = (record) => {
|
|
119
|
+
if (record.length > 0x7fff_ffff)
|
|
120
|
+
throw new RangeError("RPC record is too large for one fragment");
|
|
121
|
+
const result = new Uint8Array(record.length + 4);
|
|
122
|
+
new DataView(result.buffer).setUint32(0, 0x8000_0000 | record.length);
|
|
123
|
+
result.set(record, 4);
|
|
124
|
+
return result;
|
|
125
|
+
};
|
|
126
|
+
import * as ByteSize from "effect/ByteSize";
|
|
127
|
+
import * as Data from "effect/Data";
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as ByteSize from "effect/ByteSize";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import type { NfsPeer } from "../NfsServer.js";
|
|
4
|
+
import { type DecodeLimits } from "./xdr.js";
|
|
5
|
+
/** @internal */
|
|
6
|
+
export interface RpcLimits extends DecodeLimits {
|
|
7
|
+
readonly maxAuthBytes: ByteSize.ByteSize;
|
|
8
|
+
readonly maxMachineNameBytes: ByteSize.ByteSize;
|
|
9
|
+
readonly maxSupplementaryGroups: number;
|
|
10
|
+
}
|
|
11
|
+
/** @internal */
|
|
12
|
+
export type Credentials = {
|
|
13
|
+
readonly _tag: "None";
|
|
14
|
+
} | {
|
|
15
|
+
readonly _tag: "Sys";
|
|
16
|
+
readonly stamp: number;
|
|
17
|
+
readonly machineName: string;
|
|
18
|
+
readonly uid: number;
|
|
19
|
+
readonly gid: number;
|
|
20
|
+
readonly supplementaryGroups: ReadonlyArray<number>;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* One transport connection, identified by object identity. A session records the connections
|
|
24
|
+
* associated with its channels (RFC 8881 Section 2.10.5), so the identity must outlive a single
|
|
25
|
+
* call and end when the connection does.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export interface Connection {
|
|
30
|
+
readonly id: number;
|
|
31
|
+
/** Address verified by the application's transport resolver, when networked mode is active. */
|
|
32
|
+
readonly peer?: NfsPeer;
|
|
33
|
+
/**
|
|
34
|
+
* Writes one RPC message to the peer, answering false when the connection can no longer carry
|
|
35
|
+
* it. The server uses this to send callbacks down a session's backchannel. The message is an
|
|
36
|
+
* unframed RPC call; applying record marking is the transport implementation's job, so callers
|
|
37
|
+
* must not frame it themselves.
|
|
38
|
+
*/
|
|
39
|
+
readonly send: (message: Uint8Array) => Effect.Effect<boolean>;
|
|
40
|
+
}
|
|
41
|
+
/** @internal */
|
|
42
|
+
export interface CompoundCall {
|
|
43
|
+
/** The connection the call arrived on. */
|
|
44
|
+
readonly connection: Connection;
|
|
45
|
+
/** Untrusted identity claims. Only the caller configured on `NfsServer` supplies VFS authority. */
|
|
46
|
+
readonly credentials: Credentials;
|
|
47
|
+
readonly arguments: Uint8Array;
|
|
48
|
+
readonly requestBytes?: number;
|
|
49
|
+
}
|
|
50
|
+
/** @internal */
|
|
51
|
+
export interface RpcHandlers {
|
|
52
|
+
readonly compound: (call: CompoundCall) => Effect.Effect<Uint8Array, RpcPolicyDenied>;
|
|
53
|
+
/** Called once when a connection ends, however it ended. */
|
|
54
|
+
readonly disconnect: (connection: Connection) => Effect.Effect<void>;
|
|
55
|
+
/** Called with an RPC REPLY, which on a backchannel answers a callback the server sent. */
|
|
56
|
+
readonly callbackReply: (connection: Connection, message: Uint8Array) => Effect.Effect<void>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* True when a record is an RPC REPLY rather than a CALL. A backchannel shares its connection with
|
|
60
|
+
* the fore channel, so callback replies arrive interleaved with ordinary requests and must be
|
|
61
|
+
* routed to the callback that is waiting for them instead of being answered as a call.
|
|
62
|
+
*/
|
|
63
|
+
export declare const isReply: (message: Uint8Array) => boolean;
|
|
64
|
+
declare const RpcPolicyDenied_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
65
|
+
readonly _tag: "RpcPolicyDenied";
|
|
66
|
+
} & Readonly<A>;
|
|
67
|
+
/** A policy refusal is an RPC authentication failure and dispatches no NFS compound. */
|
|
68
|
+
export declare class RpcPolicyDenied extends RpcPolicyDenied_base<{}> {
|
|
69
|
+
}
|
|
70
|
+
/** @internal */
|
|
71
|
+
export declare const handleCall: (connection: Connection, message: Uint8Array, limits: RpcLimits, handlers: RpcHandlers) => Effect.Effect<U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MAAM,CAAC,OAAO,CAAC,CAAA;CAC/D;AAED,gBAAgB;AAChB,MAAM,WAAW,YAAY;IAC3B,0CAA0C;IAC1C,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAA;IAC/B,mGAAmG;IACnG,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAA;IACjC,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAA;IAC9B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAC/B;AAED,gBAAgB;AAChB,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,YAAY,KAAK,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,eAAe,CAAC,CAAA;IACrF,4DAA4D;IAC5D,QAAQ,CAAC,UAAU,EAAE,CAAC,UAAU,EAAE,UAAU,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACpE,2FAA2F;IAC3F,QAAQ,CAAC,aAAa,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;CAC7F;AAMD;;;;GAIG;AACH,eAAO,MAAM,OAAO,YAAa,UAAU,KAAG,OAE+C,CAAA;;;;AA2B7F,wFAAwF;AACxF,qBAAa,eAAgB,SAAQ,qBAAoC,EAAE,CAAC;CAAG;AA+F/E,gBAAgB;AAChB,eAAO,MAAM,UAAU,eACT,UAAU,WACb,UAAU,UACX,SAAS,YACP,WAAW,KACpB,MAAM,CAAC,MAAM,CAAC,UAAU,GAAG,SAAS,CAyDnC,CAAA"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import * as ByteSize from "effect/ByteSize";
|
|
2
|
+
import * as Data from "effect/Data";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Predicate from "effect/Predicate";
|
|
5
|
+
import { Reader, Writer, XdrDecodeError } from "./xdr.js";
|
|
6
|
+
const Credentials = Data.taggedEnum();
|
|
7
|
+
const CALL = 0;
|
|
8
|
+
const REPLY = 1;
|
|
9
|
+
/**
|
|
10
|
+
* True when a record is an RPC REPLY rather than a CALL. A backchannel shares its connection with
|
|
11
|
+
* the fore channel, so callback replies arrive interleaved with ordinary requests and must be
|
|
12
|
+
* routed to the callback that is waiting for them instead of being answered as a call.
|
|
13
|
+
*/
|
|
14
|
+
export const isReply = (message) => message.length >= 8 &&
|
|
15
|
+
new DataView(message.buffer, message.byteOffset, message.byteLength).getUint32(4) === REPLY;
|
|
16
|
+
const MSG_ACCEPTED = 0;
|
|
17
|
+
const MSG_DENIED = 1;
|
|
18
|
+
const RPC_VERSION = 2;
|
|
19
|
+
const NFS_PROGRAM = 100003;
|
|
20
|
+
const NFS_VERSION = 4;
|
|
21
|
+
const AUTH_NONE = 0;
|
|
22
|
+
const AUTH_SYS = 1;
|
|
23
|
+
const RPCSEC_GSS = 6;
|
|
24
|
+
/** RFC 5531 Section 9 `auth_stat` values this server emits. */
|
|
25
|
+
const AUTH_BADCRED = 1;
|
|
26
|
+
const AUTH_BADVERF = 3;
|
|
27
|
+
const AUTH_TOOWEAK = 5;
|
|
28
|
+
const AUTH_FAILED = 7;
|
|
29
|
+
/** A policy refusal is an RPC authentication failure and dispatches no NFS compound. */
|
|
30
|
+
export class RpcPolicyDenied extends Data.TaggedError("RpcPolicyDenied") {
|
|
31
|
+
}
|
|
32
|
+
class CredentialError extends Data.TaggedError("CredentialError") {
|
|
33
|
+
constructor(message, status = AUTH_BADCRED) {
|
|
34
|
+
super({ detail: message, status });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
class VerifierError extends Data.TaggedError("VerifierError") {
|
|
38
|
+
constructor(message) {
|
|
39
|
+
super({ detail: message });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const decodeAuth = (reader, limits) => {
|
|
43
|
+
const flavor = reader.uint32();
|
|
44
|
+
const body = reader.opaque(ByteSize.min(limits.maxAuthBytes, ByteSize.bytes(400)));
|
|
45
|
+
const auth = new Reader(body, limits);
|
|
46
|
+
if (flavor === AUTH_NONE) {
|
|
47
|
+
auth.finish();
|
|
48
|
+
return Credentials.None();
|
|
49
|
+
}
|
|
50
|
+
// RPCSEC_GSS is a flavor this server understands but does not implement, so the reply says
|
|
51
|
+
// "server requires different authentication" (AUTH_TOOWEAK) rather than "credential is
|
|
52
|
+
// malformed". Nothing ever falls back to a broader identity: the compound is never dispatched.
|
|
53
|
+
if (flavor === RPCSEC_GSS)
|
|
54
|
+
throw new CredentialError("RPCSEC_GSS is not implemented", AUTH_TOOWEAK);
|
|
55
|
+
if (flavor !== AUTH_SYS)
|
|
56
|
+
throw new CredentialError("Unsupported RPC authentication flavor");
|
|
57
|
+
try {
|
|
58
|
+
const value = Credentials.Sys({
|
|
59
|
+
stamp: auth.uint32(),
|
|
60
|
+
machineName: auth.string(limits.maxMachineNameBytes),
|
|
61
|
+
uid: auth.uint32(),
|
|
62
|
+
gid: auth.uint32(),
|
|
63
|
+
supplementaryGroups: auth.array((item) => item.uint32(), limits.maxSupplementaryGroups)
|
|
64
|
+
});
|
|
65
|
+
auth.finish();
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error instanceof XdrDecodeError)
|
|
70
|
+
throw new CredentialError(error.message);
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const decodeVerifier = (reader, limits) => {
|
|
75
|
+
try {
|
|
76
|
+
const flavor = reader.uint32();
|
|
77
|
+
const body = reader.opaque(ByteSize.min(limits.maxAuthBytes, ByteSize.bytes(400)));
|
|
78
|
+
if (flavor !== AUTH_NONE || body.length !== 0) {
|
|
79
|
+
throw new VerifierError("Only an empty AUTH_NONE verifier is accepted");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error instanceof XdrDecodeError)
|
|
84
|
+
throw new VerifierError(error.message);
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const accepted = (xid, status, payload, mismatch) => {
|
|
89
|
+
const writer = new Writer().uint32(xid).uint32(REPLY).uint32(MSG_ACCEPTED).uint32(0).uint32(0).uint32(status);
|
|
90
|
+
if (mismatch !== undefined)
|
|
91
|
+
writer.uint32(mismatch[0]).uint32(mismatch[1]);
|
|
92
|
+
const prefix = writer.bytes();
|
|
93
|
+
if (payload === undefined)
|
|
94
|
+
return prefix;
|
|
95
|
+
const result = new Uint8Array(prefix.length + payload.length);
|
|
96
|
+
result.set(prefix);
|
|
97
|
+
result.set(payload, prefix.length);
|
|
98
|
+
return result;
|
|
99
|
+
};
|
|
100
|
+
const denied = (xid, status, detail) => {
|
|
101
|
+
const writer = new Writer().uint32(xid).uint32(REPLY).uint32(MSG_DENIED).uint32(status);
|
|
102
|
+
if (Predicate.isNumber(detail))
|
|
103
|
+
writer.uint32(detail);
|
|
104
|
+
else
|
|
105
|
+
writer.uint32(detail[0]).uint32(detail[1]);
|
|
106
|
+
return writer.bytes();
|
|
107
|
+
};
|
|
108
|
+
/** @internal */
|
|
109
|
+
export const handleCall = (connection, message, limits, handlers) => Effect.suspend(() => {
|
|
110
|
+
if (message.length < 4)
|
|
111
|
+
return Effect.as(Effect.void, undefined);
|
|
112
|
+
const xid = new DataView(message.buffer, message.byteOffset, message.byteLength).getUint32(0);
|
|
113
|
+
const reader = new Reader(message, limits);
|
|
114
|
+
try {
|
|
115
|
+
reader.uint32();
|
|
116
|
+
if (reader.uint32() !== CALL)
|
|
117
|
+
return Effect.succeed(accepted(xid, 4));
|
|
118
|
+
const rpcVersion = reader.uint32();
|
|
119
|
+
if (rpcVersion !== RPC_VERSION)
|
|
120
|
+
return Effect.succeed(denied(xid, 0, [RPC_VERSION, RPC_VERSION]));
|
|
121
|
+
const program = reader.uint32();
|
|
122
|
+
const version = reader.uint32();
|
|
123
|
+
const procedure = reader.uint32();
|
|
124
|
+
let credentials;
|
|
125
|
+
try {
|
|
126
|
+
credentials = decodeAuth(reader, limits);
|
|
127
|
+
decodeVerifier(reader, limits);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
if (error instanceof VerifierError)
|
|
131
|
+
return Effect.succeed(denied(xid, 1, AUTH_BADVERF));
|
|
132
|
+
if (error instanceof CredentialError)
|
|
133
|
+
return Effect.succeed(denied(xid, 1, error.status));
|
|
134
|
+
if (error instanceof XdrDecodeError)
|
|
135
|
+
return Effect.succeed(denied(xid, 1, AUTH_BADCRED));
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
if (program !== NFS_PROGRAM)
|
|
139
|
+
return Effect.succeed(accepted(xid, 1));
|
|
140
|
+
if (version !== NFS_VERSION)
|
|
141
|
+
return Effect.succeed(accepted(xid, 2, undefined, [NFS_VERSION, NFS_VERSION]));
|
|
142
|
+
if (procedure === 0) {
|
|
143
|
+
reader.finish();
|
|
144
|
+
return Effect.succeed(accepted(xid, 0));
|
|
145
|
+
}
|
|
146
|
+
if (procedure !== 1)
|
|
147
|
+
return Effect.succeed(accepted(xid, 3));
|
|
148
|
+
const arguments_ = message.slice(message.length - reader.remaining);
|
|
149
|
+
return handlers.compound({
|
|
150
|
+
connection,
|
|
151
|
+
credentials,
|
|
152
|
+
arguments: arguments_,
|
|
153
|
+
requestBytes: message.length
|
|
154
|
+
}).pipe(Effect.map((payload) => accepted(xid, 0, payload)), Effect.catchTag("RpcPolicyDenied", () => Effect.succeed(denied(xid, 1, AUTH_FAILED))));
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
if (error instanceof XdrDecodeError)
|
|
158
|
+
return Effect.succeed(accepted(xid, 4));
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import type * as Scope from "effect/Scope";
|
|
3
|
+
import type * as SocketServer from "effect/unstable/socket/SocketServer";
|
|
4
|
+
import { type RecordLimits } from "./recordMarking.js";
|
|
5
|
+
import { type Connection, type RpcHandlers, type RpcLimits } from "./rpc.js";
|
|
6
|
+
/** @internal */
|
|
7
|
+
export interface ServerLimits extends RecordLimits, RpcLimits {
|
|
8
|
+
readonly maxConnections: number;
|
|
9
|
+
}
|
|
10
|
+
/** @internal */
|
|
11
|
+
export interface ServerOptions {
|
|
12
|
+
readonly limits: ServerLimits;
|
|
13
|
+
readonly peer?: Effect.Effect<Connection["peer"] | null>;
|
|
14
|
+
}
|
|
15
|
+
/** @internal */
|
|
16
|
+
export declare const startServer: (server: SocketServer.SocketServer["Service"], options: ServerOptions, handlers: RpcHandlers) => Effect.Effect<void, SocketServer.SocketServerError, Scope.Scope>;
|
|
17
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/internal/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,KAAK,KAAK,MAAM,cAAc,CAAA;AAG1C,OAAO,KAAK,KAAK,YAAY,MAAM,qCAAqC,CAAA;AACxE,OAAO,EAA+B,KAAK,YAAY,EAAsB,MAAM,oBAAoB,CAAA;AACvG,OAAO,EAAE,KAAK,UAAU,EAAuB,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,MAAM,UAAU,CAAA;AAEjG,gBAAgB;AAChB,MAAM,WAAW,YAAa,SAAQ,YAAY,EAAE,SAAS;IAC3D,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;CAChC;AAED,gBAAgB;AAChB,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAA;IAC7B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAA;CACzD;AAuFD,gBAAgB;AAChB,eAAO,MAAM,WAAW,WACd,YAAY,CAAC,YAAY,CAAC,SAAS,CAAC,WACnC,aAAa,YACZ,WAAW,KACpB,MAAM,CAAC,MAAM,CACd,IAAI,EACJ,YAAY,CAAC,iBAAiB,EAC9B,KAAK,CAAC,KAAK,CAoBT,CAAA"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Option from "effect/Option";
|
|
3
|
+
import * as Semaphore from "effect/Semaphore";
|
|
4
|
+
import * as Socket from "effect/unstable/socket/Socket";
|
|
5
|
+
import { encodeRecord, RecordDecoder, RecordMarkingError } from "./recordMarking.js";
|
|
6
|
+
import { handleCall, isReply } from "./rpc.js";
|
|
7
|
+
/**
|
|
8
|
+
* Opens and immediately closes a connection the server has no permit for. Acquiring the reader is
|
|
9
|
+
* what opens the underlying socket; a refusal that never acquires one leaves the connection
|
|
10
|
+
* accepted by the platform but abandoned, holding a file descriptor until the process exits.
|
|
11
|
+
*/
|
|
12
|
+
const refuseConnection = (socket) => Effect.ignore(Effect.scoped(Effect.asVoid(socket.reader)));
|
|
13
|
+
const handleConnection = (id, socket, options, handlers) => Effect.suspend(() => {
|
|
14
|
+
// Only a connection that reached the read loop was ever handed to the NFSv4 layer, so only
|
|
15
|
+
// that one needs disassociating when it ends.
|
|
16
|
+
let opened;
|
|
17
|
+
return Effect.scoped(Effect.gen(function* () {
|
|
18
|
+
const peer = options.peer === undefined ? undefined : yield* options.peer;
|
|
19
|
+
if (peer === null) {
|
|
20
|
+
yield* refuseConnection(socket);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const pull = yield* Socket.readerBytes(socket);
|
|
24
|
+
const writer = yield* socket.writer;
|
|
25
|
+
const decoder = new RecordDecoder(options.limits);
|
|
26
|
+
// The connection can only carry callbacks once its writer exists, so it is built here
|
|
27
|
+
// rather than at accept time.
|
|
28
|
+
const send = (message) => writer.write(encodeRecord(message)).pipe(Effect.as(true),
|
|
29
|
+
// A peer that has gone away is reported, not raised: the caller decides whether a
|
|
30
|
+
// dead backchannel matters.
|
|
31
|
+
Effect.catchTag("SocketError", () => Effect.succeed(false)));
|
|
32
|
+
const connection = peer === undefined ? { id, send } : { id, peer, send };
|
|
33
|
+
opened = connection;
|
|
34
|
+
while (true) {
|
|
35
|
+
const chunks = yield* pull;
|
|
36
|
+
for (const chunk of chunks) {
|
|
37
|
+
const records = yield* Effect.suspend(() => {
|
|
38
|
+
try {
|
|
39
|
+
return Effect.succeed(decoder.push(chunk));
|
|
40
|
+
}
|
|
41
|
+
catch (cause) {
|
|
42
|
+
if (cause instanceof RecordMarkingError)
|
|
43
|
+
return Effect.fail(cause);
|
|
44
|
+
throw cause;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
for (const record of records) {
|
|
48
|
+
// A REPLY on this connection answers a callback the server sent down the
|
|
49
|
+
// backchannel; only a CALL is a request to be served.
|
|
50
|
+
if (isReply(record)) {
|
|
51
|
+
yield* handlers.callbackReply(connection, record);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const response = yield* handleCall(connection, record, options.limits, handlers);
|
|
55
|
+
if (response !== undefined)
|
|
56
|
+
yield* writer.write(encodeRecord(response));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
})).pipe(Effect.catchTag("RecordMarkingError", () => Effect.void), Effect.catchReason("SocketError", "SocketCloseError", () => Effect.void),
|
|
61
|
+
// A connection that ends for any reason releases the session state it was associated with.
|
|
62
|
+
// `Effect.ensuring` runs this uninterruptibly, so `disconnect` must never block; see the
|
|
63
|
+
// note on the handler's `disconnect`, which is deliberately gate-free for that reason.
|
|
64
|
+
Effect.ensuring(Effect.suspend(() => opened === undefined ? Effect.void : handlers.disconnect(opened))));
|
|
65
|
+
});
|
|
66
|
+
/** @internal */
|
|
67
|
+
export const startServer = (server, options, handlers) => Effect.gen(function* () {
|
|
68
|
+
const connections = yield* Semaphore.make(options.limits.maxConnections);
|
|
69
|
+
let connectionSerial = 0;
|
|
70
|
+
yield* server.run((socket) => Semaphore.withPermitsIfAvailable(connections, 1, handleConnection(connectionSerial++, socket, options, handlers)).pipe(
|
|
71
|
+
// A refusal never runs `handleConnection`, so the socket is still unopened here and must
|
|
72
|
+
// be closed explicitly rather than left for the server finalizer, which only tracks
|
|
73
|
+
// connections that arrived before `run` started.
|
|
74
|
+
Effect.flatMap(Option.match({
|
|
75
|
+
onNone: () => refuseConnection(socket),
|
|
76
|
+
onSome: () => Effect.void
|
|
77
|
+
})))).pipe(Effect.forkScoped);
|
|
78
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export interface DecodeLimits {
|
|
3
|
+
readonly maxOpaqueBytes: ByteSize.ByteSize;
|
|
4
|
+
readonly maxStringBytes: ByteSize.ByteSize;
|
|
5
|
+
readonly maxArrayElements: number;
|
|
6
|
+
}
|
|
7
|
+
declare const XdrDecodeError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
8
|
+
readonly _tag: "XdrDecodeError";
|
|
9
|
+
} & Readonly<A>;
|
|
10
|
+
/** @internal */
|
|
11
|
+
export declare class XdrDecodeError extends XdrDecodeError_base<{
|
|
12
|
+
readonly detail: string;
|
|
13
|
+
}> {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
get message(): string;
|
|
16
|
+
}
|
|
17
|
+
/** @internal */
|
|
18
|
+
export declare class Reader {
|
|
19
|
+
#private;
|
|
20
|
+
readonly bytes: Uint8Array;
|
|
21
|
+
readonly limits: DecodeLimits;
|
|
22
|
+
constructor(bytes: Uint8Array, limits: DecodeLimits);
|
|
23
|
+
get remaining(): number;
|
|
24
|
+
uint32(): number;
|
|
25
|
+
int32(): number;
|
|
26
|
+
uint64(): bigint;
|
|
27
|
+
boolean(): boolean;
|
|
28
|
+
fixedOpaque(length: number): Uint8Array;
|
|
29
|
+
opaque(maxBytes?: ByteSize.ByteSize): Uint8Array;
|
|
30
|
+
string(maxBytes?: ByteSize.ByteSize): string;
|
|
31
|
+
array<A>(decode: (reader: Reader) => A, maxElements?: number): ReadonlyArray<A>;
|
|
32
|
+
discriminant<const A extends Record<number, unknown>>(cases: A): A[keyof A];
|
|
33
|
+
finish(): void;
|
|
34
|
+
}
|
|
35
|
+
/** @internal */
|
|
36
|
+
export declare class Writer {
|
|
37
|
+
#private;
|
|
38
|
+
get length(): number;
|
|
39
|
+
uint32(value: number): this;
|
|
40
|
+
int32(value: number): this;
|
|
41
|
+
uint64(value: bigint): this;
|
|
42
|
+
boolean(value: boolean): this;
|
|
43
|
+
fixedOpaque(value: Uint8Array): this;
|
|
44
|
+
opaque(value: Uint8Array): this;
|
|
45
|
+
string(value: string): this;
|
|
46
|
+
array<A>(values: ReadonlyArray<A>, encode: (writer: Writer, value: A) => void): this;
|
|
47
|
+
bytes(): Uint8Array;
|
|
48
|
+
}
|
|
49
|
+
import * as ByteSize from "effect/ByteSize";
|
|
50
|
+
export {};
|
|
51
|
+
//# sourceMappingURL=xdr.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xdr.d.ts","sourceRoot":"","sources":["../../src/internal/xdr.ts"],"names":[],"mappings":"AAAA,gBAAgB;AAChB,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,cAAc,EAAE,QAAQ,CAAC,QAAQ,CAAA;IAC1C,QAAQ,CAAC,cAAc,EAAE,QAAQ,CAAC,QAAQ,CAAA;IAC1C,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAA;CAClC;;;;AAED,gBAAgB;AAChB,qBAAa,cAAe,SAAQ,oBAAmC;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;IACjG,YAAY,OAAO,EAAE,MAAM,EAE1B;IAED,IAAa,OAAO,IAAI,MAAM,CAE7B;CACF;AAQD,gBAAgB;AAChB,qBAAa,M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