Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core-http] Add NDJSON support #11325

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/core/core-http/review/core-http.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ export interface InternalPipelineOptions extends PipelineOptions {
decompressResponse?: boolean;
deserializationOptions?: DeserializationOptions;
loggingOptions?: LogPolicyOptions;
sendStreamingJson?: boolean;
}

// @public
Expand Down
5 changes: 5 additions & 0 deletions sdk/core/core-http/src/pipelineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ export interface InternalPipelineOptions extends PipelineOptions {
* Configure whether to decompress response according to Accept-Encoding header (node-fetch only)
*/
decompressResponse?: boolean;

/**
* Send JSON Array payloads as NDJSON.
*/
sendStreamingJson?: boolean;
}
51 changes: 51 additions & 0 deletions sdk/core/core-http/src/policies/ndJsonPolicy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

// BaseRequestPolicy has a protected constructor.
/* eslint-disable @typescript-eslint/no-useless-constructor */

import {
BaseRequestPolicy,
RequestPolicy,
RequestPolicyOptions,
RequestPolicyFactory
} from "./requestPolicy";
import { WebResourceLike } from "../webResource";
import { HttpOperationResponse } from "../httpOperationResponse";

export function ndJsonPolicy(): RequestPolicyFactory {
return {
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new NdJsonPolicy(nextPolicy, options);
}
};
}

/**
* NdJsonPolicy that formats a JSON array as newline-delimited JSON
*/
class NdJsonPolicy extends BaseRequestPolicy {
/**
* Creates an instance of KeepAlivePolicy.
*
* @param nextPolicy
* @param options
*/
constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions) {
super(nextPolicy, options);
}

/**
* Sends a request.
*
* @param request
*/
public async sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> {
// There currently isn't a good way to bypass the serializer
const body = JSON.parse(request.body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be too unreliable to check if the first character is [ before parsing the body?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a guarantee, but it's way faster than always parsing, so added!

if (Array.isArray(body)) {
request.body = body.map((item) => JSON.stringify(item) + "\n").join("");
}
return this._nextPolicy.sendRequest(request);
}
}
5 changes: 5 additions & 0 deletions sdk/core/core-http/src/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import { InternalPipelineOptions } from "./pipelineOptions";
import { DefaultKeepAliveOptions, keepAlivePolicy } from "./policies/keepAlivePolicy";
import { tracingPolicy } from "./policies/tracingPolicy";
import { disableResponseDecompressionPolicy } from "./policies/disableResponseDecompressionPolicy";
import { ndJsonPolicy } from "./policies/ndJsonPolicy";

/**
* Options to configure a proxy for outgoing requests (Node.js only).
Expand Down Expand Up @@ -677,6 +678,10 @@ export function createPipelineFromOptions(
): ServiceClientOptions {
const requestPolicyFactories: RequestPolicyFactory[] = [];

if (pipelineOptions.sendStreamingJson) {
requestPolicyFactories.push(ndJsonPolicy());
}

let userAgentValue = undefined;
if (pipelineOptions.userAgentOptions && pipelineOptions.userAgentOptions.userAgentPrefix) {
const userAgentInfo: string[] = [];
Expand Down
31 changes: 31 additions & 0 deletions sdk/core/core-http/test/policies/ndJsonPolicyTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { assert } from "chai";
import { RequestPolicyOptions } from "../../src/policies/requestPolicy";
import { WebResource } from "../../src/webResource";
import { HttpHeaders } from "../../src/httpHeaders";
import { ndJsonPolicy } from "../../src/policies/ndJsonPolicy";

describe("NdJsonPolicy", function() {
const returnOk = {
sendRequest: async (request: WebResource) => {
return {
request,
status: 200,
headers: new HttpHeaders()
};
}
};

const emptyPolicyOptions = new RequestPolicyOptions();

it("Formats arrays correctly", async function() {
const factory = ndJsonPolicy();
const policy = factory.create(returnOk, emptyPolicyOptions);
const request = new WebResource();
request.body = JSON.stringify([{ a: 1 }, { b: 2 }, { c: 3 }]);
const result = await policy.sendRequest(request);
assert.strictEqual(result.request.body, `{"a":1}\n{"b":2}\n{"c":3}\n`);
});
});