Skip to content

Commit

Permalink
Add support for configuring zstdCompressorCallback (#811)
Browse files Browse the repository at this point in the history
* add support for ZstdCompressorCallback

* Update .generator/src/generator/templates/configuration.j2

Co-authored-by: NouemanKHAL <[email protected]>

* include zstdCompressorCallback in test config

* update licenses

* pre-commit fixes

* update readme

* code review suggestion

Co-authored-by: NouemanKHAL <[email protected]>
Co-authored-by: ci.datadog-api-spec <[email protected]>
  • Loading branch information
3 people authored Aug 26, 2022
1 parent 93c7c87 commit b8bfbf2
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpLibrary, HttpConfiguration, RequestContext } from "./http/http";
import { HttpLibrary, HttpConfiguration, RequestContext, ZstdCompressorCallback } from "./http/http";
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
import { BaseServerConfiguration, server1, servers, operationServers } from "./servers";
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth";
Expand Down Expand Up @@ -47,6 +47,10 @@ export interface ConfigurationParameters {
* Flag to enable requests tracing
*/
debug?: boolean
/**
* Callback method to compress string body with zstd
*/
zstdCompressorCallback?: ZstdCompressorCallback
}

/**
Expand Down Expand Up @@ -101,6 +105,7 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
httpConfig: conf.httpConfig || {},
debug: conf.debug
};
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback
configuration.httpApi.debug = configuration.debug;
return configuration;
}
Expand Down
3 changes: 3 additions & 0 deletions .generator/src/generator/templates/http/http.j2
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ export class ResponseContext {
}
}

export type ZstdCompressorCallback = (body: string) => Buffer;

export interface HttpLibrary {
debug?: boolean;
zstdCompressorCallback?: ZstdCompressorCallback;
send(request: RequestContext): Promise<ResponseContext>;
}
9 changes: 8 additions & 1 deletion .generator/src/generator/templates/http/isomorphic-fetch.j2
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { HttpLibrary, RequestContext, ResponseContext } from "./http";
import { HttpLibrary, RequestContext, ResponseContext, ZstdCompressorCallback } from "./http";
import fetch from "cross-fetch";
import pako from "pako";
import bufferFrom from "buffer-from";

export class IsomorphicFetchHttpLibrary implements HttpLibrary {
public debug = false;
public zstdCompressorCallback: ZstdCompressorCallback | undefined;

public send(request: RequestContext): Promise<ResponseContext> {
if (this.debug) {
Expand All @@ -24,6 +25,12 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
body = bufferFrom(pako.gzip(body).buffer);
} else if (headers["Content-Encoding"] == "deflate") {
body = bufferFrom(pako.deflate(body).buffer);
} else if (headers["Content-Encoding"] == "zstd1") {
if (this.zstdCompressorCallback) {
body = this.zstdCompressorCallback(body);
} else {
throw new Error("zstdCompressorCallback method missing")
}
}
}

Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ typedoc,Apache-2.0,Copyright (c) 2015 Sebastian Lenz Copyright (c) 2016-2021 Typ
typescript,Apache-2.0,Copyright (c) Microsoft Corporation.
url-parse,MIT,Copyright (c) 2015 Unshift.io, Arnout Kazemier, the Contributors.
loglevel,MIT,Copyright (c) 2013 Tim Perry
zstd.ts,BSD-2-Clause,Copyright (c) [2021] [Beeno Tung (Tung Cheung Leong)]
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,54 @@ async function main() {
main();
```

### Zstd compression

Zstd compression support requires users to supply their own zstd compressor callback function.
The callback should accept string arg and return compressed Buffer data.
Callback signature `(body: string) => Buffer`.
For example, using `zstd.ts` package:

```typescript
import { compressSync } from 'zstd.ts'
import { client, v2 } from "@datadog/datadog-api-client";

async function main() {
const configurationOpts = {
zstdCompressorCallback: (body: string) => compressSync({input: Buffer.from(body, "utf8")})
}
const configuration = client.createConfiguration(configurationOpts);
const apiInstance = new v2.MetricsApi(configuration);
const params: v2.MetricsApiSubmitMetricsRequest = {
body: {
series: [
{
metric: "system.load.1",
type: 0,
points: [
{
timestamp: Math.round(new Date().getTime() / 1000),
value: 0.7,
},
],
},
],
},
contentEncoding: "zstd1",
};

apiInstance.submitMetrics(params).then((data: v2.IntakePayloadAccepted) => {
console.log(
"API called successfully. Returned data: " + JSON.stringify(data)
);
}).catch((error: any) => console.error(error));
}

main();
```

## Documentation

Documentation for API endpoints can be found in in [Github pages][github pages].
Documentation for API endpoints can be found in [GitHub pages][github pages].

## Contributing

Expand Down
3 changes: 3 additions & 0 deletions features/support/given.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Given } from "@cucumber/cucumber";
import { compressSync } from 'zstd.ts'

import fs from "fs";
import path from "path";
Expand Down Expand Up @@ -43,7 +44,9 @@ for (const apiVersion of Versions) {
appKeyAuth: process.env.DD_TEST_CLIENT_APP_KEY,
},
httpConfig: { compress: false },
zstdCompressorCallback: (body: string) => compressSync({input: Buffer.from(body, "utf8")}),
};

if (process.env.DD_TEST_SITE) {
const server = datadogApiClient.client.servers[2];
const serverConf = server.getConfiguration();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
"ts-jest": "^27.0.4",
"ts-node": "^9.1.1",
"typedoc": "^0.22.11",
"typescript": "4.5.5"
"typescript": "4.5.5",
"zstd.ts": "^1.1.3"
},
"engines": {
"node": ">=12.0.0"
Expand Down
12 changes: 11 additions & 1 deletion packages/datadog-api-client-common/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { HttpLibrary, HttpConfiguration, RequestContext } from "./http/http";
import {
HttpLibrary,
HttpConfiguration,
RequestContext,
ZstdCompressorCallback,
} from "./http/http";
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
import {
BaseServerConfiguration,
Expand Down Expand Up @@ -55,6 +60,10 @@ export interface ConfigurationParameters {
* Flag to enable requests tracing
*/
debug?: boolean;
/**
* Callback method to compress string body with zstd
*/
zstdCompressorCallback?: ZstdCompressorCallback;
}

/**
Expand Down Expand Up @@ -129,6 +138,7 @@ export function createConfiguration(
httpConfig: conf.httpConfig || {},
debug: conf.debug,
};
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback;
configuration.httpApi.debug = configuration.debug;
return configuration;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/datadog-api-client-common/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ export class ResponseContext {
}
}

export type ZstdCompressorCallback = (body: string) => Buffer;

export interface HttpLibrary {
debug?: boolean;
zstdCompressorCallback?: ZstdCompressorCallback;
send(request: RequestContext): Promise<ResponseContext>;
}
14 changes: 13 additions & 1 deletion packages/datadog-api-client-common/http/isomorphic-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { HttpLibrary, RequestContext, ResponseContext } from "./http";
import {
HttpLibrary,
RequestContext,
ResponseContext,
ZstdCompressorCallback,
} from "./http";
import fetch from "cross-fetch";
import pako from "pako";
import bufferFrom from "buffer-from";

export class IsomorphicFetchHttpLibrary implements HttpLibrary {
public debug = false;
public zstdCompressorCallback: ZstdCompressorCallback | undefined;

public send(request: RequestContext): Promise<ResponseContext> {
if (this.debug) {
Expand All @@ -24,6 +30,12 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
body = bufferFrom(pako.gzip(body).buffer);
} else if (headers["Content-Encoding"] == "deflate") {
body = bufferFrom(pako.deflate(body).buffer);
} else if (headers["Content-Encoding"] == "zstd1") {
if (this.zstdCompressorCallback) {
body = this.zstdCompressorCallback(body);
} else {
throw new Error("zstdCompressorCallback method missing");
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5302,3 +5302,8 @@ yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zstd.ts@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/zstd.ts/-/zstd.ts-1.1.3.tgz#3ddbc625ef499429aa69d423a0fb5892e5af99f5"
integrity sha512-1xlKTD4O/4H2MEQ/McteFAdujc4cHxNZYDRzjxtWfLAimYbPzV7mIa+ZzzJqpN830AuNklVDA6RgHwcop6IZKw==

0 comments on commit b8bfbf2

Please sign in to comment.