-
Notifications
You must be signed in to change notification settings - Fork 6
/
runner.ts
executable file
·206 lines (182 loc) · 6.51 KB
/
runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env -S npx tsx
// Copyright 2023-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {
ConformanceRequest,
ConformanceResponse,
FailureSet,
WireFormat,
} from "./gen/conformance/conformance.pb.js";
import {
TestAllTypesProto2,
TestAllTypesProto2JSON,
} from "./gen/google/protobuf/test_messages_proto2.pb.js";
import {
TestAllTypesProto3,
TestAllTypesProto3JSON,
} from "./gen/google/protobuf/test_messages_proto3.pb.js";
import { readSync, writeSync } from "fs";
const proto2serializer = {
decodeProtobuf: TestAllTypesProto2.decode.bind(TestAllTypesProto2),
decodeJson: TestAllTypesProto2JSON.decode.bind(TestAllTypesProto2JSON),
encodeProtobuf: TestAllTypesProto2.encode.bind(TestAllTypesProto2),
encodeJson: TestAllTypesProto2JSON.encode.bind(TestAllTypesProto2JSON),
};
const proto3serializer = {
decodeProtobuf: TestAllTypesProto3.decode.bind(TestAllTypesProto3),
decodeJson: TestAllTypesProto3JSON.decode.bind(TestAllTypesProto3JSON),
encodeProtobuf: TestAllTypesProto3.encode.bind(TestAllTypesProto3),
encodeJson: TestAllTypesProto3JSON.encode.bind(TestAllTypesProto3JSON),
};
function main() {
let testCount = 0;
try {
while (testIo(test)) {
testCount += 1;
}
} catch (e) {
process.stderr.write(
`conformance.ts: exiting after ${testCount} tests: ${String(e)}`,
);
process.exit(1);
}
}
function test(request: ConformanceRequest): ConformanceResponse {
if (request.messageType === "conformance.FailureSet") {
// > The conformance runner will request a list of failures as the first request.
// > This will be known by message_type == "conformance.FailureSet", a conformance
// > test should return a serialized FailureSet in protobuf_payload.
return {
protobufPayload: FailureSet.encode({}),
};
}
const serializer =
request.messageType === "protobuf_test_messages.proto3.TestAllTypesProto3"
? proto3serializer
: request.messageType ===
"protobuf_test_messages.proto2.TestAllTypesProto2"
? proto2serializer
: undefined;
if (!serializer) {
return {
runtimeError: `unknown request message type ${request.messageType}`,
};
}
let testMessage: object;
try {
if (request.protobufPayload) {
testMessage = serializer.decodeProtobuf(request.protobufPayload);
} else if (request.jsonPayload) {
testMessage = serializer.decodeJson(request.jsonPayload);
} else {
// We use a failure list instead of skipping, because that is more transparent.
return { runtimeError: `${request} not supported` };
}
} catch (err) {
// > This string should be set to indicate parsing failed. The string can
// > provide more information about the parse error if it is available.
// >
// > Setting this string does not necessarily mean the testee failed the
// > test. Some of the test cases are intentionally invalid input.
return { parseError: String(err) };
}
try {
switch (request.requestedOutputFormat) {
case WireFormat.PROTOBUF:
return { protobufPayload: serializer.encodeProtobuf(testMessage) };
case WireFormat.JSON:
return { jsonPayload: serializer.encodeJson(testMessage) };
case WireFormat.JSPB:
return { skipped: "JSPB not supported." };
case WireFormat.TEXT_FORMAT:
return { skipped: "Text format not supported." };
default:
return {
runtimeError: `unknown requested output format ${request.requestedOutputFormat}`,
};
}
} catch (err) {
// > If the input was successfully parsed but errors occurred when
// > serializing it to the requested output format, set the error message in
// > this field.
return { serializeError: String(err) };
}
}
// Returns true if the test ran successfully, false on legitimate EOF.
// If EOF is encountered in an unexpected place, raises IOError.
function testIo(
test: (request: ConformanceRequest) => ConformanceResponse,
): boolean {
setBlockingStdout();
const requestLengthBuf = readBuffer(4);
if (requestLengthBuf === "EOF") {
return false;
}
const requestLength = requestLengthBuf.readInt32LE(0);
const serializedRequest = readBuffer(requestLength);
if (serializedRequest === "EOF") {
throw "Failed to read request.";
}
const request = ConformanceRequest.decode(serializedRequest);
const response = test(request);
const serializedResponse = ConformanceResponse.encode(response);
const responseLengthBuf = Buffer.alloc(4);
responseLengthBuf.writeInt32LE(serializedResponse.length, 0);
writeBuffer(responseLengthBuf);
writeBuffer(Buffer.from(serializedResponse));
return true;
}
// Read a buffer of N bytes from stdin.
function readBuffer(bytes: number): Buffer | "EOF" {
const buf = Buffer.alloc(bytes);
let read = 0;
try {
read = readSync(0, buf, 0, bytes, null);
} catch (e) {
throw `failed to read from stdin: ${String(e)}`;
}
if (read !== bytes) {
if (read === 0) {
return "EOF";
}
throw "premature EOF on stdin.";
}
return buf;
}
// Write a buffer to stdout.
function writeBuffer(buffer: Buffer): void {
let totalWritten = 0;
while (totalWritten < buffer.length) {
totalWritten += writeSync(
1,
buffer,
totalWritten,
buffer.length - totalWritten,
);
}
}
// Fixes https://github.com/timostamm/protobuf-ts/issues/134
// Node is buffering chunks to stdout, meaning that for big generated
// files the CodeGeneratorResponse will not reach protoc completely.
// To fix this, we set stdout to block using the internal private
// method setBlocking(true)
function setBlockingStdout(): void {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment
const stdoutHandle = (process.stdout as any)._handle;
if (stdoutHandle !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
stdoutHandle.setBlocking(true);
}
}
main();