-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
507 additions
and
22 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,20 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "anonymous", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": [ | ||
"get", | ||
"post" | ||
] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"scriptFile": "../dist/httpTriggerRandomDelay/index.js" | ||
} |
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,19 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { AzureFunction, Context, HttpRequest } from '@azure/functions'; | ||
import { addRandomAsyncOrSyncDelay } from '../utils/getRandomTestData'; | ||
|
||
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> { | ||
context.log(`Http function processed request for url "${req.url}"`); | ||
|
||
await addRandomAsyncOrSyncDelay(); | ||
|
||
const name = req.query.name || req.body || 'world'; | ||
|
||
context.res = { | ||
body: `Hello, ${name}!`, | ||
}; | ||
}; | ||
|
||
export default httpTrigger; |
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,13 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
export async function delay(ms: number): Promise<void> { | ||
await new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
export function delaySync(ms: number): void { | ||
const endTime = Date.now() + ms; | ||
while (Date.now() < endTime) { | ||
// wait | ||
} | ||
} |
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,32 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as crypto from 'crypto'; | ||
import { delay, delaySync } from './delay'; | ||
|
||
export function getRandomTestData(): string { | ||
// This should start with non-numeric data to prevent this bug from causing a test failure | ||
// https://github.com/Azure/azure-functions-nodejs-library/issues/90 | ||
return `testData${getRandomHexString()}`; | ||
} | ||
|
||
export function getRandomHexString(length = 10): string { | ||
const buffer: Buffer = crypto.randomBytes(Math.ceil(length / 2)); | ||
return buffer.toString('hex').slice(0, length); | ||
} | ||
|
||
export function getRandomInt(min: number, max: number): number { | ||
return Math.floor(Math.random() * (max - min) + min); | ||
} | ||
|
||
export function getRandomBoolean(percentTrue: number): boolean { | ||
return Math.random() * 100 > percentTrue; | ||
} | ||
|
||
export async function addRandomAsyncOrSyncDelay(): Promise<void> { | ||
if (getRandomBoolean(95)) { | ||
await delay(getRandomInt(0, 250)); | ||
} else { | ||
delaySync(getRandomInt(0, 10)); | ||
} | ||
} |
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,6 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { app } from '@azure/functions'; | ||
|
||
app.setup({ enableHttpStream: false }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'; | ||
import { addRandomAsyncOrSyncDelay } from '../utils/getRandomTestData'; | ||
|
||
export async function httpTriggerRandomDelay( | ||
request: HttpRequest, | ||
context: InvocationContext | ||
): Promise<HttpResponseInit> { | ||
context.log(`Http function processed request for url "${request.url}"`); | ||
|
||
await addRandomAsyncOrSyncDelay(); | ||
|
||
const name = request.query.get('name') || (await request.text()) || 'world'; | ||
|
||
return { body: `Hello, ${name}!` }; | ||
} | ||
|
||
app.http('httpTriggerRandomDelay', { | ||
methods: ['GET', 'POST'], | ||
authLevel: 'anonymous', | ||
handler: httpTriggerRandomDelay, | ||
}); |
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,22 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'; | ||
import { receiveStreamWithProgress } from '../utils/streamHttp'; | ||
|
||
export async function httpTriggerReceiveStream( | ||
request: HttpRequest, | ||
context: InvocationContext | ||
): Promise<HttpResponseInit> { | ||
context.log(`Http function processed request for url "${request.url}"`); | ||
|
||
const bytesReceived = await receiveStreamWithProgress(request.body); | ||
|
||
return { body: `Bytes received: ${bytesReceived}` }; | ||
} | ||
|
||
app.http('httpTriggerReceiveStream', { | ||
methods: ['GET', 'POST'], | ||
authLevel: 'anonymous', | ||
handler: httpTriggerReceiveStream, | ||
}); |
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,22 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'; | ||
import { createRandomStream } from '../utils/streamHttp'; | ||
|
||
export async function httpTriggerSendStream( | ||
request: HttpRequest, | ||
context: InvocationContext | ||
): Promise<HttpResponseInit> { | ||
context.log(`Http function processed request for url "${request.url}"`); | ||
|
||
const lengthInMb = request.query.get('lengthInMb'); | ||
const stream = createRandomStream(Number(lengthInMb)); | ||
return { body: stream }; | ||
} | ||
|
||
app.http('httpTriggerSendStream', { | ||
methods: ['GET', 'POST'], | ||
authLevel: 'anonymous', | ||
handler: httpTriggerSendStream, | ||
}); |
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,6 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { app } from '@azure/functions'; | ||
|
||
app.setup({ enableHttpStream: true }); |
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,13 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
export async function delay(ms: number): Promise<void> { | ||
await new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
export function delaySync(ms: number): void { | ||
const endTime = Date.now() + ms; | ||
while (Date.now() < endTime) { | ||
// wait | ||
} | ||
} |
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,32 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as crypto from 'crypto'; | ||
import { delay, delaySync } from './delay'; | ||
|
||
export function getRandomTestData(): string { | ||
// This should start with non-numeric data to prevent this bug from causing a test failure | ||
// https://github.com/Azure/azure-functions-nodejs-library/issues/90 | ||
return `testData${getRandomHexString()}`; | ||
} | ||
|
||
export function getRandomHexString(length = 10): string { | ||
const buffer: Buffer = crypto.randomBytes(Math.ceil(length / 2)); | ||
return buffer.toString('hex').slice(0, length); | ||
} | ||
|
||
export function getRandomInt(min: number, max: number): number { | ||
return Math.floor(Math.random() * (max - min) + min); | ||
} | ||
|
||
export function getRandomBoolean(percentTrue: number): boolean { | ||
return Math.random() * 100 > percentTrue; | ||
} | ||
|
||
export async function addRandomAsyncOrSyncDelay(): Promise<void> { | ||
if (getRandomBoolean(95)) { | ||
await delay(getRandomInt(0, 250)); | ||
} else { | ||
delaySync(getRandomInt(0, 10)); | ||
} | ||
} |
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,57 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as crypto from 'crypto'; | ||
import { Readable } from 'stream'; | ||
import { delay } from './delay'; | ||
|
||
const oneMb = 1024 * 1024; | ||
|
||
export function createRandomStream(lengthInMb: number): Readable { | ||
const stream = new Readable(); | ||
stream._read = () => {}; | ||
setTimeout(() => { | ||
void sendRandomData(stream, lengthInMb); | ||
}, 5); | ||
return stream; | ||
} | ||
|
||
async function sendRandomData(stream: Readable, lengthInMb: number): Promise<void> { | ||
const maxChunkSize = oneMb; | ||
let remainingBytes = convertMbToB(lengthInMb); | ||
do { | ||
if (stream.readableLength > maxChunkSize) { | ||
await delay(5); | ||
} else { | ||
const chunkSize = Math.min(maxChunkSize, remainingBytes); | ||
stream.push(crypto.randomBytes(chunkSize)); | ||
remainingBytes -= chunkSize; | ||
} | ||
} while (remainingBytes > 0); | ||
stream.push(null); | ||
} | ||
|
||
export async function receiveStreamWithProgress(stream: { | ||
[Symbol.asyncIterator](): AsyncIterableIterator<string | Buffer>; | ||
}): Promise<number> { | ||
let bytesReceived = 0; | ||
const logInterval = 500; | ||
let nextLogTime = Date.now(); | ||
for await (const chunk of stream) { | ||
if (Date.now() > nextLogTime) { | ||
nextLogTime = Date.now() + logInterval; | ||
console.log(`Progress: ${convertBToMb(bytesReceived)}mb`); | ||
} | ||
|
||
bytesReceived += chunk.length; | ||
} | ||
return bytesReceived; | ||
} | ||
|
||
export function convertMbToB(mb: number): number { | ||
return mb * oneMb; | ||
} | ||
|
||
function convertBToMb(bytes: number) { | ||
return Math.round(bytes / oneMb); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.