@azure/functions
Version:
Microsoft Azure Functions NodeJS Framework
78 lines (69 loc) • 2.49 kB
text/typescript
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.
import * as types from '@azure/functions';
import { MediaType } from '../constants';
import { AzFuncSystemError } from '../errors';
import { parseContentType } from './parseHeader';
import { parseMultipartForm } from './parseMultipartForm';
/**
* See ./test/parseForm.test.ts for examples
*/
export function parseForm(data: Buffer | string, contentType: string): Form {
const [mediaType, parameters] = parseContentType(contentType);
switch (mediaType.toLowerCase()) {
case MediaType.multipartForm: {
const boundary = parameters.get('boundary');
const parts = parseMultipartForm(typeof data === 'string' ? Buffer.from(data) : data, boundary);
return new Form(parts);
}
case MediaType.urlEncodedForm: {
const parsed = new URLSearchParams(data.toString());
const parts: [string, types.FormPart][] = [];
for (const [key, value] of parsed) {
parts.push([key, { value: Buffer.from(value) }]);
}
return new Form(parts);
}
default:
throw new AzFuncSystemError(
`Media type "${mediaType}" does not match types supported for form parsing: "${MediaType.multipartForm}", "${MediaType.urlEncodedForm}".`
);
}
}
export class Form implements types.Form {
constructor(parts: [string, types.FormPart][]) {
this.
}
get(name: string): types.FormPart | null {
for (const [key, value] of this.
if (key === name) {
return value;
}
}
return null;
}
getAll(name: string): types.FormPart[] {
const result: types.FormPart[] = [];
for (const [key, value] of this.
if (key === name) {
result.push(value);
}
}
return result;
}
has(name: string): boolean {
for (const [key] of this.
if (key === name) {
return true;
}
}
return false;
}
[](): Iterator<[string, types.FormPart]> {
return this.
}
get length(): number {
return this.
}
}