-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core-http] Add NDJSON support (#11325)
- Loading branch information
Showing
10 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 | ||
if (typeof request.body === "string" && request.body.startsWith("[")) { | ||
const body = JSON.parse(request.body); | ||
if (Array.isArray(body)) { | ||
request.body = body.map((item) => JSON.stringify(item) + "\n").join(""); | ||
} | ||
} | ||
return this._nextPolicy.sendRequest(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { PipelineResponse, PipelineRequest, SendRequest } from "../interfaces"; | ||
import { PipelinePolicy } from "../pipeline"; | ||
|
||
/** | ||
* The programmatic identifier of the keepAlivePolicy. | ||
*/ | ||
export const ndJsonPolicyName = "ndJsonPolicy"; | ||
|
||
/** | ||
* ndJsonPolicy is a policy used to control keep alive settings for every request. | ||
*/ | ||
export function ndJsonPolicy(): PipelinePolicy { | ||
return { | ||
name: ndJsonPolicyName, | ||
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> { | ||
// There currently isn't a good way to bypass the serializer | ||
if (typeof request.body === "string" && request.body.startsWith("[")) { | ||
const body = JSON.parse(request.body); | ||
if (Array.isArray(body)) { | ||
request.body = body.map((item) => JSON.stringify(item) + "\n").join(""); | ||
} | ||
} | ||
return next(request); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { assert } from "chai"; | ||
import * as sinon from "sinon"; | ||
import { | ||
createPipelineRequest, | ||
SendRequest, | ||
PipelineResponse, | ||
createHttpHeaders, | ||
ndJsonPolicy | ||
} from "../src"; | ||
|
||
describe("NdJsonPolicy", function() { | ||
afterEach(function() { | ||
sinon.restore(); | ||
}); | ||
|
||
it("Formats arrays correctly", async function() { | ||
const request = createPipelineRequest({ | ||
url: "https://bing.com" | ||
}); | ||
request.body = JSON.stringify([{ a: 1 }, { b: 2 }, { c: 3 }]); | ||
const successResponse: PipelineResponse = { | ||
headers: createHttpHeaders(), | ||
request, | ||
status: 200 | ||
}; | ||
const next = sinon.stub<Parameters<SendRequest>, ReturnType<SendRequest>>(); | ||
next.resolves(successResponse); | ||
|
||
const policy = ndJsonPolicy(); | ||
|
||
const result = await policy.sendRequest(request, next); | ||
assert.strictEqual(result.request.body, `{"a":1}\n{"b":2}\n{"c":3}\n`); | ||
}); | ||
}); |