rsnext/packages/next/server/web/utils.ts
Javi Velasco a815ba9f79
Implement Middleware RFC (#30081)
This PR adds support for [Middleware as per RFC ](https://github.com/vercel/next.js/discussions/29750). 

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
2021-10-20 17:52:11 +00:00

44 lines
1.1 KiB
TypeScript

import type { NodeHeaders } from './types'
export async function* streamToIterator<T>(
readable: ReadableStream<T>
): AsyncIterableIterator<T> {
const reader = readable.getReader()
while (true) {
const { value, done } = await reader.read()
if (done) break
if (value) {
yield value
}
}
reader.releaseLock()
}
export function notImplemented(name: string, method: string): any {
throw new Error(
`Failed to get the '${method}' property on '${name}': the property is not implemented`
)
}
export function fromNodeHeaders(object: NodeHeaders) {
const headers: { [k: string]: string } = {}
for (let headerKey in object) {
const headerValue = object[headerKey]
if (Array.isArray(headerValue)) {
headers[headerKey] = headerValue.join('; ')
} else if (headerValue) {
headers[headerKey] = String(headerValue)
}
}
return headers
}
export function toNodeHeaders(headers?: Headers): NodeHeaders {
const object: NodeHeaders = {}
if (headers) {
for (const [key, value] of headers.entries()) {
object[key] = value.includes(';') ? value.split(';') : value
}
}
return object
}