Skip to content

Commit 09a02bb

Browse files
authored
Use pako for compressing payloads (#487)
* Use pako for compressing payloads zlib is a node specific library, using pako offers some chances to be more cross platform. * Add licenses
1 parent 195f547 commit 09a02bb

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

.generator/templates/http/isomorphic-fetch.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import fetch from "node-fetch";
88
import "whatwg-fetch";
99
{{/browser}}
1010
{{/platforms}}
11-
import * as zlib from "zlib";
11+
import pako from "pako";
12+
import bufferFrom from "buffer-from";
1213

1314
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1415
@@ -24,9 +25,9 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
2425
let headers = request.getHeaders();
2526
if (typeof body === "string") {
2627
if (headers["Content-Encoding"] == "gzip") {
27-
body = zlib.gzipSync(body);
28+
body = bufferFrom(pako.gzip(body).buffer);
2829
} else if (headers["Content-Encoding"] == "deflate") {
29-
body = zlib.deflateSync(body);
30+
body = bufferFrom(pako.deflate(body).buffer);
3031
}
3132
}
3233

LICENSE-3rdparty.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ Component,Licence,Copyright
44
@pollyjs/adapter-node-http,Apache-2.0,
55
@pollyjs/core,Apache-2.0,
66
@pollyjs/persister-fs,Apache-2.0,
7+
@types/buffer-from,MIT,Copyright Nat Burns
78
@types/chai,MIT,Copyright Microsoft Corporation
89
@types/jest,MIT,Copyright Microsoft Corporation
910
@types/lodash,MIT,Copyright Microsoft Corporation
1011
@types/node-fetch,MIT,Copyright Microsoft Corporation
1112
@types/node,MIT,Copyright Microsoft Corporation
13+
@types/pako,MIT,Copyright Caleb Eggensperger Muhammet Öztürk
1214
@types/pollyjs__adapter-node-http,MIT,Copyright Microsoft Corporation
1315
@types/pollyjs__core,MIT,Copyright Microsoft Corporation
1416
@types/pollyjs__persister-fs,MIT,Copyright Microsoft Corporation
@@ -18,6 +20,7 @@ Component,Licence,Copyright
1820
@typescript-eslint/eslint-plugin,MIT,Copyright (c) 2019 TypeScript ESLint and other contributors
1921
@typescript-eslint/parser,BSD-2-Clause,
2022
btoa,(MIT OR Apache-2.0),
23+
buffer-from,MIT,Copyright (c) 2016, 2018 Linus Unnebäck
2124
chai,MIT,Copyright (c) 2017 Chai.js Assertion Library
2225
jest,MIT,Copyright (c) Facebook, Inc. and its affiliates.
2326
ts-jest,MIT,Copyright (c) 2016-2018
@@ -30,6 +33,7 @@ eslint-plugin-unused-imports,MIT,Copyright (c) 2021 Mikkel Holmer
3033
eslint,MIT,Copyright JS Foundation and other contributors, https://js.foundation
3134
form-data,MIT,Copyright (c) 2012 Felix Geisendörfer ([email protected]) and contributors
3235
node-fetch,MIT,Copyright (c) 2016 David Frank
36+
pako,MIT,Copyright (C) 2014-2017 Vitaly Puzrin and Andrei Tuputcyn
3337
prettier,MIT,Copyright © James Long and contributors
3438
ts-node,MIT,Copyright (c) 2014 Blake Embrey ([email protected])
3539
typescript,Apache-2.0,Copyright (c) Microsoft Corporation.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@
6060
"jest-coverage": "jest --no-cache --coverage"
6161
},
6262
"dependencies": {
63+
"@types/buffer-from": "^1.1.0",
6364
"@types/node": "*",
6465
"@types/node-fetch": "^2.5.7",
66+
"@types/pako": "^1.0.3",
6567
"btoa": "^1.2.1",
68+
"buffer-from": "^1.1.2",
6669
"durations": "^3.4.2",
6770
"es6-promise": "^4.2.4",
6871
"form-data": "^3.0.0",
6972
"loglevel": "^1.7.1",
7073
"node-fetch": "^2.6.0",
74+
"pako": "^2.0.4",
7175
"url-parse": "^1.4.3"
7276
},
7377
"resolutions": {

packages/datadog-api-client-v1/http/isomorphic-fetch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { HttpLibrary, RequestContext, ResponseContext } from "./http";
22
import { from, Observable } from "../rxjsStub";
33
import fetch from "node-fetch";
4-
import * as zlib from "zlib";
4+
import pako from "pako";
5+
import bufferFrom from "buffer-from";
56

67
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
78
public send(request: RequestContext): Observable<ResponseContext> {
@@ -16,9 +17,9 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1617
const headers = request.getHeaders();
1718
if (typeof body === "string") {
1819
if (headers["Content-Encoding"] == "gzip") {
19-
body = zlib.gzipSync(body);
20+
body = bufferFrom(pako.gzip(body).buffer);
2021
} else if (headers["Content-Encoding"] == "deflate") {
21-
body = zlib.deflateSync(body);
22+
body = bufferFrom(pako.deflate(body).buffer);
2223
}
2324
}
2425

packages/datadog-api-client-v2/http/isomorphic-fetch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { HttpLibrary, RequestContext, ResponseContext } from "./http";
22
import { from, Observable } from "../rxjsStub";
33
import fetch from "node-fetch";
4-
import * as zlib from "zlib";
4+
import pako from "pako";
5+
import bufferFrom from "buffer-from";
56

67
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
78
public send(request: RequestContext): Observable<ResponseContext> {
@@ -16,9 +17,9 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1617
const headers = request.getHeaders();
1718
if (typeof body === "string") {
1819
if (headers["Content-Encoding"] == "gzip") {
19-
body = zlib.gzipSync(body);
20+
body = bufferFrom(pako.gzip(body).buffer);
2021
} else if (headers["Content-Encoding"] == "deflate") {
21-
body = zlib.deflateSync(body);
22+
body = bufferFrom(pako.deflate(body).buffer);
2223
}
2324
}
2425

yarn.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,13 @@
868868
dependencies:
869869
"@babel/types" "^7.3.0"
870870

871+
"@types/buffer-from@^1.1.0":
872+
version "1.1.0"
873+
resolved "https://registry.yarnpkg.com/@types/buffer-from/-/buffer-from-1.1.0.tgz#fed6287e90fe524dc2b412e0fbc2222c1889c21f"
874+
integrity sha512-BLFpLBcN+RPKUsFxqRkMiwqTOOdi+TrKr5OpLJ9qCnUdSxS6S80+QRX/mIhfR66u0Ykc4QTkReaejOM2ILh+9Q==
875+
dependencies:
876+
"@types/node" "*"
877+
871878
"@types/chai@^4.2.14":
872879
version "4.2.15"
873880
resolved "https://registry.npmjs.org/@types/chai/-/chai-4.2.15.tgz"
@@ -945,6 +952,11 @@
945952
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.0.tgz#62797cee3b8b497f6547503b2312254d4fe3c2bb"
946953
integrity sha512-eMhwJXc931Ihh4tkU+Y7GiLzT/y/DBNpNtr4yU9O2w3SYBsr9NaOPhQlLKRmoWtI54uNwuo0IOUFQjVOTZYRvw==
947954

955+
"@types/pako@^1.0.3":
956+
version "1.0.3"
957+
resolved "https://registry.yarnpkg.com/@types/pako/-/pako-1.0.3.tgz#2e61c2b02020b5f44e2e5e946dfac74f4ec33c58"
958+
integrity sha512-EDxOsHAD5dqjbjEUM1xwa7rpKPFb8ECBE5irONTQU7/OsO3thI5YrNEWSPNMvYmvFM0l/OLQJ6Mgw7PEdXSjhg==
959+
948960
"@types/pollyjs__adapter-node-http@^2.0.1":
949961
version "2.0.1"
950962
resolved "https://registry.npmjs.org/@types/pollyjs__adapter-node-http/-/pollyjs__adapter-node-http-2.0.1.tgz"
@@ -1468,6 +1480,11 @@ buffer-from@^1.0.0:
14681480
resolved "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz"
14691481
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
14701482

1483+
buffer-from@^1.1.2:
1484+
version "1.1.2"
1485+
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5"
1486+
integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==
1487+
14711488
busboy@^0.2.11:
14721489
version "0.2.14"
14731490
resolved "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz"
@@ -3986,6 +4003,11 @@ pad-right@^0.2.2:
39864003
dependencies:
39874004
repeat-string "^1.5.2"
39884005

4006+
pako@^2.0.4:
4007+
version "2.0.4"
4008+
resolved "https://registry.yarnpkg.com/pako/-/pako-2.0.4.tgz#6cebc4bbb0b6c73b0d5b8d7e8476e2b2fbea576d"
4009+
integrity sha512-v8tweI900AUkZN6heMU/4Uy4cXRc2AYNRggVmTR+dEncawDJgCdLMximOVA2p4qO57WMynangsfGRb5WD6L1Bg==
4010+
39894011
parent-module@^1.0.0:
39904012
version "1.0.1"
39914013
resolved "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz"

0 commit comments

Comments
 (0)