UNPKG

1.61 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4export { HttpRequest } from './http/HttpRequest';
5export { HttpResponse } from './http/HttpResponse';
6export { InvocationContext } from './InvocationContext';
7
8const bindingCounts: Record<string, number> = {};
9/**
10 * If the host spawns multiple workers, it expects the metadata (including binding name) to be the same across workers.
11 * That means we need to generate binding names in a deterministic fashion, so we'll do that using a count
12 * There's a tiny risk users register bindings in a non-deterministic order (i.e. async race conditions), but it's okay considering the following:
13 * 1. We will track the count individually for each binding type. This makes the names more readable and reduces the chances a race condition will matter
14 * 2. Users can manually specify the name themselves (aka if they're doing weird async stuff) and we will respect that
15 * More info here: https://github.com/Azure/azure-functions-nodejs-worker/issues/638
16 */
17export function addBindingName<T extends { type: string; name?: string }>(
18 binding: T,
19 suffix: string
20): T & { name: string } {
21 if (!binding.name) {
22 let bindingType = binding.type;
23 if (!bindingType.toLowerCase().endsWith(suffix.toLowerCase())) {
24 bindingType += suffix;
25 }
26 let count = bindingCounts[bindingType] || 0;
27 count += 1;
28 bindingCounts[bindingType] = count;
29 binding.name = bindingType + count.toString();
30 }
31 return <T & { name: string }>binding;
32}