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:49

Output

Inherited from​

WebContext._body


_cookie: Map<string, Cookie>

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

Cookies to send back

Inherited from​

WebContext._cookie


_ended​

_ended: Promise<any> = undefined

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

Inherited from​

WebContext._ended


_outputHeaders​

protected _outputHeaders: OutgoingHttpHeaders

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

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:43

Inherited from​

WebContext._route


_sanitized​

protected _sanitized: any

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

Contain the sanitized request body if computed

Inherited from​

WebContext._sanitized


_sessionSaved​

protected _sessionSaved: boolean = false

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

Whether the session was already saved

Inherited from​

WebContext._sessionSaved


_stream​

_stream: Writable

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

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:42

Inherited from​

WebContext.headers


headersFlushed​

protected headersFlushed: boolean

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

If headers were flushed

Inherited from​

WebContext.headersFlushed


parameters​

parameters: any

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

Inherited from​

WebContext.parameters


session​

protected session: Session

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

Session

Inherited from​

WebContext.session


statusCode​

statusCode: number

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

Current status code

Inherited from​

WebContext.statusCode

Accessors​

connectionEnded​

Get Signature​

get abstract protected connectionEnded(): boolean

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

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:36

Returns​

boolean

whether the client went away

Methods​

addAsyncRequest()​

addAsyncRequest(promise): void

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

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:78

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:41

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

Returns​

void


clearInput()​

clearInput(): void

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

Remove the input

Returns​

void

Inherited from​

WebContext.clearInput


cookie(param, value, options?): void

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

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:101

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:277

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:406

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:91

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:71

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:97

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

Returns​

Promise<any>

Inherited from​

WebContext.getInput


getLocale()​

getLocale(): string

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

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:144

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:251

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:109

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:107

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:83

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:114

Returns​

Readable

Inherited from​

WebContext.getRawStream


getRequest()​

getRequest(): IncomingMessage

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

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:307

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:320

Get request body

Returns​

any

the result

Inherited from​

WebContext.getResponseBody


getResponseCode()​

getResponseCode(): number

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

Returns​

number

the result

Inherited from​

WebContext.getResponseCode


getResponseCookies()​

getResponseCookies(): Map<string, Cookie>

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

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:399

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:248

Return the response size

Returns​

number

the result

Inherited from​

WebContext.getResponseSize


getSession()​

getSession<K>(): K

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

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:205

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:373

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:454

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:213

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:239

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:268

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:101

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:176

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:276

Remove sanitized body

Returns​

void

Inherited from​

WebContext.reinit


resetResponse()​

resetResponse(): void

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

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:257

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:55

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:127

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:381

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:62

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:117

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:78

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:228

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:62

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:90

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:154

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:71

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