Skip to content

Commit 146ab62

Browse files
authored
[protocol] Add x-restate-server header (#319)
1 parent b852280 commit 146ab62

File tree

11 files changed

+113
-14
lines changed

11 files changed

+113
-14
lines changed

package-lock.json

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/restate-sdk/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
"dist"
2121
],
2222
"scripts": {
23+
"gen:version": "node ./scripts/version.js",
2324
"proto": "npx buf mod update && npx buf generate",
25+
"prebuild": "npm run gen:version",
2426
"build": "tsc -b",
27+
"pretest": "npm run gen:version",
2528
"test": "jest --silent --maxWorkers=1",
2629
"lint": "eslint --ignore-path .eslintignore --max-warnings=0 --ext .ts .",
2730
"format": "prettier --ignore-path .eslintignore --write \"**/*.+(js|ts|json)\"",
2831
"format-check": "prettier --ignore-path .eslintignore --check \"**/*.+(js|ts|json)\"",
2932
"verify": "npm run format-check && npm run lint && npm run test && npm run build",
30-
"release": "release-it",
31-
"example": "RESTATE_DEBUG_LOGGING=OFF ts-node-dev --transpile-only ./examples/example.ts",
32-
"workflowexample": "RESTATE_DEBUG_LOGGING=OFF ts-node-dev --transpile-only ./examples/workflow_example.ts",
33-
"ingress": "RESTATE_DEBUG_LOGGING=OFF ts-node-dev --transpile-only ./examples/ingress.ts"
33+
"release": "release-it"
3434
},
3535
"dependencies": {
3636
"@bufbuild/protobuf": "^1.8.0"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate SDK for Node.js/TypeScript,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in file LICENSE in the root
8+
* directory of this repository or package, or at
9+
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10+
*/
11+
12+
// The following script injects the current version
13+
// taken from package.json into a src/generated/version.ts
14+
// file.
15+
16+
const fs = require("node:fs");
17+
const path = require("node:path");
18+
19+
//
20+
// figure out where we are
21+
//
22+
const cwd = path.dirname(__filename);
23+
24+
//
25+
// compute the relative paths to this script
26+
//
27+
const packageJsonPath = `${cwd}/../package.json`;
28+
const targetDir = `${cwd}/../src/generated`;
29+
const targetFile = `${targetDir}/version.ts`;
30+
31+
//
32+
// generate version.ts
33+
//
34+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
35+
const version = packageJson.version;
36+
const versionFileContent = `export const SDK_VERSION = '${version}';\n`;
37+
fs.mkdirSync(targetDir, { recursive: true });
38+
fs.writeFileSync(targetFile, versionFileContent);

packages/restate-sdk/src/endpoint/endpoint_impl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ export class EndpointImpl implements RestateEndpoint {
166166
});
167167
}
168168

169-
computeDiscovery(): discovery.Deployment {
169+
computeDiscovery(protocolMode: discovery.ProtocolMode): discovery.Deployment {
170170
const components = [...this.components.values()].map((c) => c.discovery());
171171

172172
const deployment: discovery.Deployment = {
173-
protocolMode: discovery.ProtocolMode.BIDI_STREAM,
173+
protocolMode,
174174
minProtocolVersion: 1,
175175
maxProtocolVersion: 2,
176176
components,

packages/restate-sdk/src/endpoint/http2_handler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
import { Deployment, ProtocolMode } from "../types/discovery";
2929
import { validateRequestSignature } from "./request_signing/validate";
3030
import { ServerHttp2Stream } from "node:http2";
31+
import { X_RESTATE_SERVER } from "../user_agent";
3132

3233
export class Http2Handler {
3334
constructor(private readonly endpoint: EndpointImpl) {}
@@ -82,6 +83,7 @@ export class Http2Handler {
8283
);
8384
stream.respond({
8485
"content-type": "application/restate",
86+
"x-restate-server": X_RESTATE_SERVER,
8587
":status": 401,
8688
});
8789
stream.end();
@@ -101,7 +103,10 @@ export class Http2Handler {
101103
return respondNotFound(stream);
102104
}
103105
if (route === "discovery") {
104-
return respondDiscovery(this.endpoint.computeDiscovery(), stream);
106+
const discovery = this.endpoint.computeDiscovery(
107+
ProtocolMode.BIDI_STREAM
108+
);
109+
return respondDiscovery(discovery, stream);
105110
}
106111
const urlComponents = route as UrlPathComponents;
107112
const component = this.endpoint.componentByName(
@@ -117,6 +122,7 @@ export class Http2Handler {
117122
// valid connection, let's dispatch the invocation
118123
stream.respond({
119124
"content-type": "application/restate",
125+
"x-restate-server": X_RESTATE_SERVER,
120126
":status": 200,
121127
});
122128
const restateStream = RestateHttp2Connection.from(stream);
@@ -133,6 +139,7 @@ function respondDiscovery(
133139
http2Stream.respond({
134140
":status": 200,
135141
"content-type": "application/json",
142+
"x-restate-server": X_RESTATE_SERVER,
136143
});
137144

138145
return pipeline(stream.Readable.from(responseData), http2Stream, {
@@ -143,6 +150,7 @@ function respondDiscovery(
143150
function respondNotFound(stream: http2.ServerHttp2Stream) {
144151
stream.respond({
145152
"content-type": "application/json",
153+
"x-restate-server": X_RESTATE_SERVER,
146154
":status": 404,
147155
});
148156
stream.end();

packages/restate-sdk/src/endpoint/lambda_handler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
parseUrlComponents,
3434
} from "../types/components";
3535
import { validateRequestSignature } from "./request_signing/validate";
36+
import { X_RESTATE_SERVER } from "../user_agent";
3637

3738
export class LambdaHandler {
3839
constructor(private readonly endpoint: EndpointImpl) {}
@@ -149,6 +150,7 @@ export class LambdaHandler {
149150
return {
150151
headers: {
151152
"content-type": "application/restate",
153+
"x-restate-server": X_RESTATE_SERVER,
152154
},
153155
statusCode: 200,
154156
isBase64Encoded: true,
@@ -163,13 +165,16 @@ export class LambdaHandler {
163165
}
164166

165167
private handleDiscovery(): APIGatewayProxyResult | APIGatewayProxyResultV2 {
166-
const disocvery = this.endpoint.computeDiscovery();
168+
const disocvery = this.endpoint.computeDiscovery(
169+
ProtocolMode.REQUEST_RESPONSE
170+
);
167171
const discoveryJson = JSON.stringify(disocvery);
168172
const body = Buffer.from(discoveryJson).toString("base64");
169173

170174
return {
171175
headers: {
172176
"content-type": "application/json",
177+
"x-restate-server": X_RESTATE_SERVER,
173178
},
174179
statusCode: 200,
175180
isBase64Encoded: true,
@@ -181,6 +186,7 @@ export class LambdaHandler {
181186
return {
182187
headers: {
183188
"content-type": "application/restate",
189+
"x-restate-server": X_RESTATE_SERVER,
184190
},
185191
statusCode: code,
186192
isBase64Encoded: true,

packages/restate-sdk/src/endpoint/request_signing/ed25519.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
* Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate SDK for Node.js/TypeScript,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in file LICENSE in the root
8+
* directory of this repository or package, or at
9+
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10+
*/
11+
112
import { Buffer } from "node:buffer";
213
import { webcrypto } from "node:crypto";
314
import * as crypto from "node:crypto";

packages/restate-sdk/src/endpoint/request_signing/v1.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
* Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate SDK for Node.js/TypeScript,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in file LICENSE in the root
8+
* directory of this repository or package, or at
9+
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10+
*/
11+
112
import { Buffer } from "node:buffer";
213
import { headerValue, ValidateResponse, ValidateSuccess } from "./validate";
314
import { importKey, Key, verify } from "./ed25519";

packages/restate-sdk/src/endpoint/request_signing/validate.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
* Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate SDK for Node.js/TypeScript,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in file LICENSE in the root
8+
* directory of this repository or package, or at
9+
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10+
*/
11+
112
import { KeySetV1, SCHEME_V1, validateV1 } from "./v1";
213

314
const SIGNATURE_SCHEME_HEADER = "x-restate-signature-scheme";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate SDK for Node.js/TypeScript,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in file LICENSE in the root
8+
* directory of this repository or package, or at
9+
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10+
*/
11+
12+
import { SDK_VERSION } from "./generated/version";
13+
14+
export const X_RESTATE_SERVER = `restate-sdk-typescript/${SDK_VERSION}`;

packages/restate-sdk/test/lambda.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
import { decodeLambdaBody } from "../src/io/decoder";
2828
import { TestGreeter, TestResponse } from "./testdriver";
2929
import { ComponentType, Deployment } from "../src/types/discovery";
30+
import { X_RESTATE_SERVER } from "../src/user_agent";
3031

3132
class LambdaGreeter implements TestGreeter {
3233
async greet(
@@ -128,6 +129,7 @@ describe("LambdaGreeter", () => {
128129
expect(result.statusCode).toStrictEqual(200);
129130
expect(result.headers).toStrictEqual({
130131
"content-type": "application/restate",
132+
"x-restate-server": X_RESTATE_SERVER,
131133
});
132134
expect(result.isBase64Encoded).toStrictEqual(true);
133135
expect(deserializeMessages(result.body)).toStrictEqual([
@@ -152,6 +154,7 @@ describe("LambdaGreeter", () => {
152154
expect(result.statusCode).toStrictEqual(404);
153155
expect(result.headers).toStrictEqual({
154156
"content-type": "application/restate",
157+
"x-restate-server": X_RESTATE_SERVER,
155158
});
156159
expect(result.isBase64Encoded).toStrictEqual(true);
157160
});
@@ -172,6 +175,7 @@ describe("LambdaGreeter", () => {
172175
expect(result.statusCode).toStrictEqual(404);
173176
expect(result.headers).toStrictEqual({
174177
"content-type": "application/restate",
178+
"x-restate-server": X_RESTATE_SERVER,
175179
});
176180
});
177181

@@ -191,6 +195,7 @@ describe("LambdaGreeter", () => {
191195
expect(result.statusCode).toStrictEqual(404);
192196
expect(result.headers).toStrictEqual({
193197
"content-type": "application/restate",
198+
"x-restate-server": X_RESTATE_SERVER,
194199
});
195200
});
196201

@@ -208,6 +213,7 @@ describe("LambdaGreeter", () => {
208213
expect(result.statusCode).toStrictEqual(200);
209214
expect(result.headers).toStrictEqual({
210215
"content-type": "application/json",
216+
"x-restate-server": X_RESTATE_SERVER,
211217
});
212218
expect(result.isBase64Encoded).toStrictEqual(true);
213219

0 commit comments

Comments
 (0)