[API reference](https://jsprismarine.dev/api/index.md) / [@jsprismarine/script-host](https://jsprismarine.dev/api/@jsprismarine/script-host/index.md) / [debugger/DebugSession](https://jsprismarine.dev/api/@jsprismarine/script-host/debugger/DebugSession/index.md) / ScriptDebugSession

# Class: ScriptDebugSession

Defined in: [script-host/src/debugger/DebugSession.ts:121](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L121)

One debugger connection over a [DebuggerChannel](https://jsprismarine.dev/api/@jsprismarine/script-host/debugger/DebugSession/index.md#debuggerchannel). The world host runs [handshake](#handshake),
then [attach](#attach) with the engine and context of the pack the extension picked, then calls [poll](#poll)
between ticks until the connection closes or [detach](#detach) ends it. Every method blocks the
calling thread for as long as the extension keeps a script stopped, which is how a stopped
script stops its world's tick.

## Constructors

### Constructor

> **new ScriptDebugSession**(`channel`, `options`): `ScriptDebugSession`

Defined in: [script-host/src/debugger/DebugSession.ts:147](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L147)

#### Parameters

##### channel

[`DebuggerChannel`](https://jsprismarine.dev/api/@jsprismarine/script-host/debugger/DebugSession/index.md#debuggerchannel)

##### options

[`DebugSessionOptions`](https://jsprismarine.dev/api/@jsprismarine/script-host/debugger/DebugSession/index.md#debugsessionoptions)

#### Returns

`ScriptDebugSession`

## Accessors

### attached

#### Get Signature

> **get** **attached**(): `boolean`

Defined in: [script-host/src/debugger/DebugSession.ts:264](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L264)

Whether the session is attached to an engine.

##### Returns

`boolean`

***

### closed

#### Get Signature

> **get** **closed**(): `boolean`

Defined in: [script-host/src/debugger/DebugSession.ts:154](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L154)

Whether the connection has closed or the session has detached.

##### Returns

`boolean`

## Methods

### attach()

> **attach**(`engine`, `context?`): `void`

Defined in: [script-host/src/debugger/DebugSession.ts:222](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L222)

Attaches to the picked pack's context, and handles the extension's configuration, its
breakpoints and exception setting, until it sends `resume`. The engine runs no script until
then. Only that context's scripts stop: another pack's file of the same name never does.

After [detachEngine](#detachengine), an attach to the new engine sets the breakpoints and the
exception setting the extension sent before, and returns at once: the extension configures
a session only once.

#### Parameters

##### engine

[`ScriptEngine`](https://jsprismarine.dev/api/@jsprismarine/script-host/ScriptEngine/classes/ScriptEngine.md)

The engine the pack runs in.

##### context?

`number` = `DEFAULT_CONTEXT`

The pack's context in the engine.

#### Returns

`void`

***

### detach()

> **detach**(): `void`

Defined in: [script-host/src/debugger/DebugSession.ts:305](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L305)

Ends the session and detaches from the engine. A session whose handshake passed tells the
extension it has terminated, as BDS does just before it closes the connection; after a
refused handshake BDS sends its error line and closes.

#### Returns

`void`

***

### detachEngine()

> **detachEngine**(): `void`

Defined in: [script-host/src/debugger/DebugSession.ts:254](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L254)

Detaches from the engine and tells the extension the scripts' thread has exited, while the
session stays open, as BDS does around `/reload`. [attach](#attach) with the new engine starts
the thread again.

#### Returns

`void`

***

### handshake()

> **handshake**(): `string` \| `undefined`

Defined in: [script-host/src/debugger/DebugSession.ts:181](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L181)

Offers the protocol and the plugins, unless [offer](#offer) already did, and checks the
extension's answer as Bedrock Dedicated Server does: the same version, the passcode when one
is set, and a target among the plugins.

#### Returns

`string` \| `undefined`

The script module UUID the extension picked, lowercase, or undefined when it picked none.

#### Throws

With Bedrock Dedicated Server's message when the answer is refused or never comes.

***

### offer()

> **offer**(): `void`

Defined in: [script-host/src/debugger/DebugSession.ts:163](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L163)

Sends the protocol event, with the plugins and whether a passcode is needed, without waiting
for the answer. A host that must not block until the extension answers offers first and
calls [handshake](#handshake) once a message has arrived. Only the first call sends anything.

#### Returns

`void`

***

### poll()

> **poll**(): `void`

Defined in: [script-host/src/debugger/DebugSession.ts:281](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L281)

Handles what the extension has sent while no script ran, without waiting. A pause request
stops the next script the engine runs at its first line. Once the connection has closed and
its last messages are handled, the session closes and detaches.

#### Returns

`void`

***

### print()

> **print**(`message`, `logLevel?`): `void`

Defined in: [script-host/src/debugger/DebugSession.ts:296](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L296)

Shows script output in the extension's debug console.

#### Parameters

##### message

`string`

##### logLevel?

`number` = `LogLevel.log`

#### Returns

`void`

***

### stats()

> **stats**(`tick`, `stats`): `void`

Defined in: [script-host/src/debugger/DebugSession.ts:272](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/debugger/DebugSession.ts#L272)

Sends one tick's statistics, as BDS streams a `StatEvent2` each tick to an attached
extension. Nothing is sent while no engine is attached.

#### Parameters

##### tick

`number`

##### stats

readonly [`DebuggerStat`](https://jsprismarine.dev/api/@jsprismarine/script-host/debugger/protocol/index.md#debuggerstat)[]

#### Returns

`void`
