Skip to main content

StreamingOperationContext

@webda/core


Abstract Class: StreamingOperationContext

Defined in: packages/core/src/contexts/streamingcontext.ts:30

The shared part of a context serving a streaming operation over a transport (gRPC, WebSocket, GraphQL subscription, ...):

  • a non-streamed result is buffered as usual (writeResult())
  • a streamed chunk is sent right away (sendChunk()), write() returning false under backpressure
  • drained() waits for the transport (waitForDrain()) without leaving listeners behind: the signal it gets is aborted once the wait is over
  • once cancelled (the client went away) or once the connection is over (connectionEnded), write() and drained() throw OperationCancelledError

The transport calls cancel() when the client leaves, and also ends the operation's input queue so its pending read returns and its finally blocks run.

Extends​

Extended by​

Properties​

_body​

protected _body: string

Defined in: packages/core/src/contexts/operationcontext.ts:48

Output

Inherited from​

WebContext._body


_cookie: Map<string, Cookie>

Defined in: packages/core/src/contexts/webcontext.ts:48

Cookies to send back

Inherited from​

WebContext._cookie


_ended​

_ended: Promise<any> = undefined

Defined in: packages/core/src/contexts/webcontext.ts:61

Ended

Inherited from​

WebContext._ended


_modes​

protected _modes: string

Defined in: packages/core/src/contexts/operationcontext.ts:43

The sanitization modes (path and mode) the cached body was sanitized with, null when the input was cleared

Inherited from​

WebContext._modes


_outputHeaders​

protected _outputHeaders: OutgoingHttpHeaders

Defined in: packages/core/src/contexts/webcontext.ts:39

Contains the response headers

Inherited from​

WebContext._outputHeaders


_promises​

_promises: Promise<any>[]

Defined in: packages/core/src/contexts/icontext.ts:33

Contain all registered promises to this context

Inherited from​

WebContext._promises


_route​

_route: any

Defined in: packages/core/src/contexts/webcontext.ts:56

Route

Inherited from​

WebContext._route


_sanitized​

protected _sanitized: any

Defined in: packages/core/src/contexts/operationcontext.ts:39

Contain the sanitized request body if computed

Inherited from​

WebContext._sanitized


_sessionSaved​

protected _sessionSaved: boolean = false

Defined in: packages/core/src/contexts/webcontext.ts:65

Whether the session was already saved

Inherited from​

WebContext._sessionSaved


_stream​

_stream: Writable

Defined in: packages/core/src/contexts/operationcontext.ts:87

Output stream

Inherited from​

WebContext._stream


caches​

caches: any

Defined in: packages/core/src/contexts/icontext.ts:19

Get the caches linked to the context

Inherited from​

WebContext.caches


extensions​

protected extensions: object

Defined in: packages/core/src/contexts/icontext.ts:23

Allow extensions

Index Signature​

[key: string]: any

Inherited from​

WebContext.extensions


flushed​

protected flushed: boolean = false

Defined in: packages/core/src/contexts/icontext.ts:37

If the headers were flushed

Inherited from​

WebContext.flushed


headers​

headers: Map<string, string>

Defined in: packages/core/src/contexts/webcontext.ts:52

Headers

Inherited from​

WebContext.headers


headersFlushed​

protected headersFlushed: boolean

Defined in: packages/core/src/contexts/webcontext.ts:70

If headers were flushed

Inherited from​

WebContext.headersFlushed


parameters​

parameters: any

Defined in: packages/core/src/contexts/operationcontext.ts:58

Parameters

Inherited from​

WebContext.parameters


session​

protected session: Session

Defined in: packages/core/src/contexts/operationcontext.ts:35

Session

Inherited from​

WebContext.session


statusCode​

statusCode: number

Defined in: packages/core/src/contexts/webcontext.ts:44

Current status code

Inherited from​

WebContext.statusCode

Accessors​

connectionEnded​

Get Signature​

get abstract protected connectionEnded(): boolean

Defined in: packages/core/src/contexts/streamingcontext.ts:53

Get true when nothing can be sent anymore although the context was not cancelled (response or socket over)

Returns​

boolean

true when nothing can be sent anymore although the context was not cancelled (response or socket over)


isCancelled​

Get Signature​

get isCancelled(): boolean

Defined in: packages/core/src/contexts/streamingcontext.ts:40

Get whether the client went away

Returns​

boolean

whether the client went away

Methods​

addAsyncRequest()​

addAsyncRequest(promise): void

Defined in: packages/core/src/contexts/operationcontext.ts:138

Register a promise with the context

Parameters​

promise​

any

the promise to add

Returns​

void

Inherited from​

WebContext.addAsyncRequest


assertAlive()​

protected assertAlive(): void

Defined in: packages/core/src/contexts/streamingcontext.ts:84

Assert alive

Returns​

void

Throws​

OperationCancelledError when the client went away or the connection is over


cancel()​

cancel(): void

Defined in: packages/core/src/contexts/streamingcontext.ts:45

The client went away: the next write or drain throws.

Returns​

void


clearInput()​

clearInput(): void

Defined in: packages/core/src/contexts/operationcontext.ts:163

Remove the input

Returns​

void

Inherited from​

WebContext.clearInput


cookie(param, value, options?): void

Defined in: packages/core/src/contexts/webcontext.ts:204

For compatibility reason

Parameters​

param​

any

the parameter name

value​

any

the value to set

options?​

any = undefined

the options

Returns​

void

Inherited from​

WebContext.cookie


createStream()​

createStream(): void

Defined in: packages/core/src/contexts/operationcontext.ts:286

Create a buffer stream

Returns​

void

Inherited from​

WebContext.createStream


drained()​

drained(): Promise<void>

Defined in: packages/core/src/contexts/streamingcontext.ts:107

Wait for the transport to take more output, without leaving listeners behind

Returns​

Promise<void>

Throws​

OperationCancelledError when the client went away, the connection is over or the transport failed

Overrides​

WebContext.drained


end()​

end(): Promise<any>

Defined in: packages/core/src/contexts/webcontext.ts:298

Flush the request

Returns​

Promise<any>

the result

Emits​

'finish' event

Throws​

Error if the request was already ended

Inherited from​

WebContext.end


flushHeaders()​

flushHeaders(): Promise<void>

Defined in: packages/core/src/contexts/webcontext.ts:427

Flush headers

Returns​

Promise<void>

Inherited from​

WebContext.flushHeaders


getCurrentUser()​

getCurrentUser<K>(refresh?): Promise<K>

Defined in: packages/core/src/contexts/operationcontext.ts:351

Get the current user from session

Type Parameters​

K​

K extends IUser

Parameters​

refresh?​

boolean = false

whether to refresh

Returns​

Promise<K>

the result

Inherited from​

WebContext.getCurrentUser


getCurrentUserId()​

getCurrentUserId(): string

Defined in: packages/core/src/contexts/operationcontext.ts:371

Get the current user id from session

A session waiting for its second factor (mfa: "pending") is not logged: it has no current user. Code that needs the pending user reads getSession().userId.

Returns​

string

the result

Inherited from​

WebContext.getCurrentUserId


getExtension()​

getExtension<K>(name): K

Defined in: packages/core/src/contexts/operationcontext.ts:93

Get an extension of the context

Type Parameters​

K​

K = any

Parameters​

name​

string

of the extension

Returns​

K

extension object

Inherited from​

WebContext.getExtension


getHttpContext()​

getHttpContext(): HttpContext

Defined in: packages/core/src/contexts/webcontext.ts:90

Get current http context

Returns​

HttpContext

the result

Inherited from​

WebContext.getHttpContext


getInput()​

getInput(sanitizedOptions?): Promise<any>

Defined in: packages/core/src/contexts/webcontext.ts:116

A multipart body (file upload) is never JSON: return the default value instead of letting the parent try to JSON.parse it and log an error. The raw body stays available to getRawInput()/getRawStream().

Parameters​

sanitizedOptions?​

IOptions & object & object

Returns​

Promise<any>

Inherited from​

WebContext.getInput


getLocale()​

getLocale(): string

Defined in: packages/core/src/contexts/webcontext.ts:380

Get the request locale if found

Returns​

string

the result

Inherited from​

WebContext.getLocale


getOutput()​

getOutput(): string

Defined in: packages/core/src/contexts/operationcontext.ts:146

Get output as string, if a OutputStream is provided it will returned null

Returns​

string

the result string

Inherited from​

WebContext.getOutput


getOutputStream()​

getOutputStream(): Promise<Writable>

Defined in: packages/core/src/contexts/operationcontext.ts:250

Get the HTTP stream to output raw data

Returns​

Promise<Writable>

the result

Inherited from​

WebContext.getOutputStream


getParameters()​

getParameters(): any

Defined in: packages/core/src/contexts/operationcontext.ts:111

Get the parameters

Returns​

any

the result

Inherited from​

WebContext.getParameters


getRawInput()​

getRawInput(limit?, timeout?): Promise<Buffer<ArrayBufferLike>>

Defined in: packages/core/src/contexts/webcontext.ts:126

Parameters​

limit?​

number = ...

timeout?​

number = 60000

Returns​

Promise<Buffer<ArrayBufferLike>>

Inherited from​

WebContext.getRawInput


getRawInputAsString()​

getRawInputAsString(limit?, timeout?, encoding?): Promise<string>

Defined in: packages/core/src/contexts/webcontext.ts:102

Get the raw input as string

Any WHATWG Encoding Standard label is accepted as encoding (utf-8, iso-8859-1, shift_jis, ...) If no encoding is given, the charset of the Content-Type header is used, falling back to UTF-8

Parameters​

limit?​

number = ...

timeout?​

number = 60000

encoding?​

string

Returns​

Promise<string>

Inherited from​

WebContext.getRawInputAsString


getRawStream()​

getRawStream(): Readable

Defined in: packages/core/src/contexts/webcontext.ts:133

Returns​

Readable

Inherited from​

WebContext.getRawStream


getRequest()​

getRequest(): IncomingMessage

Defined in: packages/core/src/contexts/webcontext.ts:354

Retrieve a http.IncomingMessage valid from Context

Need more testing

Returns​

IncomingMessage

the result

Inherited from​

WebContext.getRequest


getRequestBody()​

getRequestBody(sanitizedOptions?): Promise<any>

Defined in: packages/core/src/contexts/webcontext.ts:328

Alias to keep compatibility with WebContext

Parameters​

sanitizedOptions?​

any = ...

the sanitized options

Returns​

Promise<any>

the result

Inherited from​

WebContext.getRequestBody


getResponseBody()​

getResponseBody(): any

Defined in: packages/core/src/contexts/webcontext.ts:341

Get request body

Returns​

any

the result

Inherited from​

WebContext.getResponseBody


getResponseCode()​

getResponseCode(): number

Defined in: packages/core/src/contexts/webcontext.ts:187

Get the response code

Returns​

number

the result

Inherited from​

WebContext.getResponseCode


getResponseCookies()​

getResponseCookies(): Map<string, Cookie>

Defined in: packages/core/src/contexts/webcontext.ts:216

Get all cookies set on the response

Returns​

Map<string, Cookie>

the result map

Inherited from​

WebContext.getResponseCookies


getResponseHeaders()​

getResponseHeaders(): OutgoingHttpHeaders

Defined in: packages/core/src/contexts/webcontext.ts:420

Return the merged response headers — defaults set by the constructor (_outputHeaders) overlaid with any headers added via setHeader() / writeHead(). Same merge order as flushHeaders writes to the wire, so callers (logging, capture, transports) see exactly what the client will receive.

Returns​

OutgoingHttpHeaders

the merged response headers

Inherited from​

WebContext.getResponseHeaders


getResponseSize()​

getResponseSize(): number

Defined in: packages/core/src/contexts/webcontext.ts:269

Return the response size

Returns​

number

the result

Inherited from​

WebContext.getResponseSize


getSession()​

getSession<K>(): K

Defined in: packages/core/src/contexts/operationcontext.ts:259

Get linked session

Type Parameters​

K​

K = Session

Returns​

K

the result

Inherited from​

WebContext.getSession


getSetCookieHeaders()​

getSetCookieHeaders(): string[]

Defined in: packages/core/src/contexts/webcontext.ts:224

The Set-Cookie header values of the response, one per cookie

Returns​

string[]

the serialized cookies

Inherited from​

WebContext.getSetCookieHeaders


hasFlushedHeaders()​

hasFlushedHeaders(): boolean

Defined in: packages/core/src/contexts/webcontext.ts:394

Return true if Headers got flushed already

Returns​

boolean

the result

Inherited from​

WebContext.hasFlushedHeaders


init()​

init(force?): Promise<StreamingOperationContext>

Defined in: packages/core/src/contexts/webcontext.ts:475

Initialize the web context, loading session and setting up stream listeners

Parameters​

force?​

boolean = false

whether to force the operation

Returns​

Promise<StreamingOperationContext>

this for chaining

Inherited from​

WebContext.init


isEnded()​

isEnded(): Promise<any>

Defined in: packages/core/src/contexts/webcontext.ts:234

Whether the response has been ended

Returns​

Promise<any>

the result

Inherited from​

WebContext.isEnded


isGlobalContext()​

isGlobalContext(): boolean

Defined in: packages/core/src/contexts/icontext.ts:43

Whether this is the global (non-request-scoped) context

Returns​

boolean

true if the condition is met

Inherited from​

WebContext.isGlobalContext


json()​

json(obj): this

Defined in: packages/core/src/contexts/webcontext.ts:260

Express response allow answer to be sent this way

Parameters​

obj​

any

the target object

Returns​

this

this for chaining

Inherited from​

WebContext.json


newSession()​

newSession(): Session

Defined in: packages/core/src/contexts/operationcontext.ts:267

Create and assign a new empty session

Returns​

Session

the result

Inherited from​

WebContext.newSession


parameter()​

parameter(name, defaultValue?): any

Defined in: packages/core/src/contexts/operationcontext.ts:103

Get one parameter

Parameters​

name​

string

the name to use

defaultValue?​

any = undefined

the default value

Returns​

any

the result

Inherited from​

WebContext.parameter


pipeline()​

pipeline(stream1, ...streams): Promise<void>

Defined in: packages/core/src/contexts/icontext.ts:149

Pipeline streams into the output stream

Parameters​

stream1​

ReadableStream

the first stream

streams​

...(ReadWriteStream | PipelineOptions)[]

additional streams

Returns​

Promise<void>

See​

https://nodejs.org/api/stream.html#streampipelinestreams-options

Inherited from​

WebContext.pipeline


redirect()​

redirect(url): void

Defined in: packages/core/src/contexts/webcontext.ts:195

Redirect to another url

Parameters​

url​

string

the URL

Returns​

void

Inherited from​

WebContext.redirect


registerPromise()​

registerPromise(promise): void

Defined in: packages/core/src/contexts/icontext.ts:56

Register a promise with the context

Parameters​

promise​

any

the promise to add

Returns​

void

Inherited from​

WebContext.registerPromise


reinit()​

reinit(): void

Defined in: packages/core/src/contexts/operationcontext.ts:275

Remove sanitized body

Returns​

void

Inherited from​

WebContext.reinit


resetResponse()​

resetResponse(): void

Defined in: packages/core/src/contexts/webcontext.ts:140

Remove everything that was about to be sent

Returns​

void

Inherited from​

WebContext.resetResponse


saveSession()​

saveSession(): Promise<void>

Defined in: packages/core/src/contexts/webcontext.ts:278

Save the session through the SessionManager, once

The SessionManager writes the session cookie: call it before the headers are flushed

Returns​

Promise<void>

Inherited from​

WebContext.saveSession


sendChunk()​

abstract protected sendChunk(chunk): boolean

Defined in: packages/core/src/contexts/streamingcontext.ts:60

Send one streamed chunk now

Parameters​

chunk​

any

what the operation wrote

Returns​

boolean

false when the transport asks the operation to wait for drained()


setExtension()​

setExtension(name, extension): this

Defined in: packages/core/src/contexts/operationcontext.ts:129

Set the extension

Parameters​

name​

string

to add

extension​

any

object to store

Returns​

this

this for chaining

Inherited from​

WebContext.setExtension


setFlushedHeaders()​

setFlushedHeaders(status?): void

Defined in: packages/core/src/contexts/webcontext.ts:402

Set flushed header status

Parameters​

status?​

boolean = true

the HTTP status code

Returns​

void

Inherited from​

WebContext.setFlushedHeaders


setHeader()​

setHeader(name, value): void

Defined in: packages/core/src/contexts/icontext.ts:104

Set a header in the response

Parameters​

name​

string

the name to use

value​

string | number | string[]

the value to set

Returns​

void

Inherited from​

WebContext.setHeader


setHttpContext()​

setHttpContext(httpContext): void

Defined in: packages/core/src/contexts/webcontext.ts:81

Set current http context

Parameters​

httpContext​

HttpContext

current http context

Returns​

void

Inherited from​

WebContext.setHttpContext


setParameters()​

setParameters(params): void

Defined in: packages/core/src/contexts/operationcontext.ts:119

Set the parameters

Parameters​

params​

any

the service parameters

Returns​

void

Inherited from​

WebContext.setParameters


setSession()​

setSession(session): void

Defined in: packages/core/src/contexts/operationcontext.ts:80

Set the session

Parameters​

session​

Session

the session object

Returns​

void

Inherited from​

WebContext.setSession


status()​

status(code): this

Defined in: packages/core/src/contexts/webcontext.ts:249

Express response allow statusCode to be defined this way

Parameters​

code​

number

to return

Returns​

this

this for chaining

Inherited from​

WebContext.status


waitForDrain()​

abstract protected waitForDrain(signal): Promise<void>

Defined in: packages/core/src/contexts/streamingcontext.ts:67

Wait until the transport can take more output

Parameters​

signal​

AbortSignal

aborted once the wait is over: listeners registered with it are removed

Returns​

Promise<void>

Throws​

any error when the transport failed or closed (reported as OperationCancelledError)


write()​

write(output, encoding?, cb?): boolean

Defined in: packages/core/src/contexts/streamingcontext.ts:96

Streamed chunks go out immediately; a non-streamed result is buffered

Parameters​

output​

any

encoding?​

string

cb?​

(error) => void

Returns​

boolean

Throws​

OperationCancelledError when streaming and the client went away or the connection is over

Overrides​

WebContext.write


writeHead()​

writeHead(statusCode, headers?): this

Defined in: packages/core/src/contexts/webcontext.ts:173

Write the http return code and some headers Those headers are not flushed yet so can still be overwritten

Parameters​

statusCode​

number

to return to the client

headers?​

OutgoingHttpHeaders = undefined

to add to the response

Returns​

this

this for chaining

Inherited from​

WebContext.writeHead


writeResult()​

protected writeResult(output, encoding?, cb?): boolean

Defined in: packages/core/src/contexts/streamingcontext.ts:76

Keep a non-streamed result

Parameters​

output​

any

the result

encoding?​

string

the encoding to use

cb?​

(error) => void

the callback function

Returns​

boolean

true