@small-web/kitten-types 1.1.5 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/fragments.d.ts +13 -14
- package/global.d.ts +6 -1096
- package/index.d.ts +7 -27
- package/kitten/src/AppDatabase.d.ts +10 -0
- package/kitten/src/AppRepository.d.ts +101 -0
- package/kitten/src/AutomaticUpdates.d.ts +21 -0
- package/kitten/src/Files.d.ts +61 -0
- package/kitten/src/GlobalInternalDatabase.d.ts +22 -0
- package/kitten/src/InternalDatabase.d.ts +7 -0
- package/kitten/src/REPL.d.ts +5 -0
- package/kitten/src/Routes.d.ts +41 -0
- package/kitten/src/Server.d.ts +76 -0
- package/kitten/src/ServerError.d.ts +6 -0
- package/kitten/src/Sessions.d.ts +32 -0
- package/kitten/src/Uploads.d.ts +20 -0
- package/kitten/src/Utils.d.ts +79 -0
- package/kitten/src/cli/commands/db.d.ts +48 -0
- package/kitten/src/cli/commands/deploy.d.ts +16 -0
- package/kitten/src/cli/commands/disable.d.ts +9 -0
- package/kitten/src/cli/commands/enable.d.ts +9 -0
- package/kitten/src/cli/commands/logs.d.ts +15 -0
- package/kitten/src/cli/commands/restart.d.ts +11 -0
- package/kitten/src/cli/commands/run.d.ts +15 -0
- package/kitten/src/cli/commands/serve.d.ts +13 -0
- package/kitten/src/cli/commands/shell.d.ts +1 -0
- package/kitten/src/cli/commands/start.d.ts +9 -0
- package/kitten/src/cli/commands/status.d.ts +15 -0
- package/kitten/src/cli/commands/stop.d.ts +9 -0
- package/kitten/src/cli/commands/uninstall.d.ts +9 -0
- package/kitten/src/cli/commands/update.d.ts +7 -0
- package/kitten/src/cli/index.d.ts +55 -0
- package/kitten/src/cli/lib/WarningBox.d.ts +8 -0
- package/kitten/src/cli/lib/header.d.ts +10 -0
- package/kitten/src/components/SvgSpinner.component.d.ts +9 -0
- package/kitten/src/lib/KittenComponent.d.ts +340 -0
- package/kitten/src/lib/KittenMoji.d.ts +35 -0
- package/kitten/src/lib/KittenPackage.d.ts +56 -0
- package/kitten/src/lib/KittenPage.d.ts +202 -0
- package/kitten/src/lib/Version.d.ts +49 -0
- package/kitten/src/lib/ansi-highlight-html.d.ts +5 -0
- package/kitten/src/lib/components/stateful/CopyButton.component.d.ts +14 -0
- package/kitten/src/lib/components/stateless/copyButton.component.d.ts +21 -0
- package/kitten/src/lib/crypto.d.ts +95 -0
- package/kitten/src/lib/deploy.d.ts +20 -0
- package/kitten/src/lib/ensure.d.ts +28 -0
- package/kitten/src/lib/fragments.d.ts +1 -0
- package/kitten/src/lib/globals.d.ts +10 -0
- package/kitten/src/lib/html.d.ts +61 -0
- package/kitten/src/lib/markdown.d.ts +16 -0
- package/kitten/src/lib/paths.d.ts +25 -0
- package/kitten/src/lib/service-status.d.ts +14 -0
- package/kitten/src/lib/system-exit-codes.d.ts +39 -0
- package/kitten/src/lib/terminal-link.d.ts +24 -0
- package/kitten/src/middleware/authentication.d.ts +8 -0
- package/kitten/src/middleware/index.d.ts +5 -0
- package/kitten/src/middleware/request-and-response-helpers.d.ts +6 -0
- package/kitten/src/middleware/request-log.d.ts +10 -0
- package/kitten/src/middleware/sessions.d.ts +3 -0
- package/kitten/src/middleware/stats.d.ts +8 -0
- package/kitten/src/middleware/trailing-slashes.d.ts +11 -0
- package/kitten/src/middleware/websocket.d.ts +14 -0
- package/kitten/src/routes/HttpRoute.d.ts +16 -0
- package/kitten/src/routes/LazilyLoadedRoute.d.ts +15 -0
- package/kitten/src/routes/PageRoute.d.ts +54 -0
- package/kitten/src/routes/PageSocketRoute.d.ts +42 -0
- package/kitten/src/routes/PostRoute.d.ts +64 -0
- package/kitten/src/routes/StaticRoute.d.ts +17 -0
- package/kitten/src/routes/WebSocketRoute.d.ts +21 -0
- package/kitten/src/routes/lib/validator.d.ts +18 -0
- package/kitten/src/types/fragments.d.ts +14 -0
- package/kitten/src/types/globals.d.ts +1239 -0
- package/kitten/src/types/index.d.ts +12 -0
- package/kitten/src/types/types.d.ts +476 -0
- package/kitten/third-party-libraries-with-missing-type-information/index.d.ts +37 -0
- package/package.json +16 -5
- package/types.d.ts +5 -712
- package/polka/index.d.ts +0 -111
- package/slugify/index.d.ts +0 -246
- /package/{ws → kitten/third-party-libraries-with-missing-type-information/ws}/index.d.ts +0 -0
package/types.d.ts
CHANGED
|
@@ -1,717 +1,10 @@
|
|
|
1
|
-
import EventEmitter from 'node:events'
|
|
2
|
-
import type polka from 'polka'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
Represents a browser session.
|
|
6
|
-
|
|
7
|
-
Sessions are automatically persisted (and expired) in Kitten’s internal database.
|
|
8
|
-
|
|
9
|
-
Used by Kitten to provide automatic authentication for Small Web places.
|
|
10
|
-
|
|
11
|
-
You can also add/update arbitrary session data by adding/updating properties on this object in your routes (this object maps a session object in the `kitten._db.sessions` table in the internal database).
|
|
12
|
-
|
|
13
|
-
You can emit events on a session in order to communicate with other browser tabs and windows that share the same session.
|
|
14
|
-
|
|
15
|
-
@example
|
|
16
|
-
export default function ({ request }) {
|
|
17
|
-
request.session.kittens ??= { count: 1 }
|
|
18
|
-
|
|
19
|
-
return kitten.html`
|
|
20
|
-
<h1>Kitten count</h1>
|
|
21
|
-
<p>${'🐱️'.repeat(request.session.kittens.count++)}</p>
|
|
22
|
-
`
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
@see https://kitten.small-web.org/tutorials/sessions/
|
|
26
|
-
|
|
27
|
-
@remarks
|
|
28
|
-
Do not save custom classes on request.session or JSDB will throw an error when it tries to open the internal `_db` database and cannot find your custom class to instantiate.
|
|
29
|
-
|
|
30
|
-
If you want persisted session-level custom objects, store them in your own database table in `kitten.db` keyed by `session.id`.
|
|
31
|
-
*/
|
|
32
|
-
export class Session extends EventEmitter {
|
|
33
|
-
id: string
|
|
34
|
-
authenticated: boolean
|
|
35
|
-
challenge: string
|
|
36
|
-
createdAt: Date
|
|
37
|
-
redirectToAfterSignIn?: string
|
|
38
|
-
hasExpired (): boolean
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export type { default as WebSocket, BufferLike } from './ws/index.d.ts'
|
|
42
|
-
export type { default as slugify } from './slugify/index.d.ts'
|
|
43
|
-
export type Polka = ReturnType<typeof polka>
|
|
44
|
-
export { default as MarkdownIt } from 'markdown-it'
|
|
45
|
-
|
|
46
|
-
export namespace yaml {
|
|
47
|
-
export function parse(str:string, reviver?:JavaScriptFunction, options?:{}): any
|
|
48
|
-
export function stringify(value:any, replacer?:JavaScriptFunction, options?:{}): string
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
Upload class.
|
|
53
|
-
*/
|
|
54
|
-
|
|
55
|
-
interface UploadConstructorParameters {
|
|
56
|
-
id: string,
|
|
57
|
-
fileName: string,
|
|
58
|
-
filePath: string,
|
|
59
|
-
mimetype: string,
|
|
60
|
-
field: string,
|
|
61
|
-
encoding: string,
|
|
62
|
-
truncated: boolean,
|
|
63
|
-
done: boolean
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
Represents an uploaded file.
|
|
68
|
-
|
|
69
|
-
@see https://kitten.small-web.org/tutorials/multipart-forms-and-file-uploads/
|
|
70
|
-
*/
|
|
71
|
-
export class Upload {
|
|
72
|
-
|
|
73
|
-
// Properties.
|
|
74
|
-
|
|
75
|
-
id: string
|
|
76
|
-
fileName: string
|
|
77
|
-
filePath: string
|
|
78
|
-
mimetype: string
|
|
79
|
-
field: string
|
|
80
|
-
encoding: string
|
|
81
|
-
truncated: boolean
|
|
82
|
-
done: boolean
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
@private
|
|
86
|
-
@remarks For internal use by Kitten’s automatic POST route handler
|
|
87
|
-
*/
|
|
88
|
-
constructor(parameters:UploadConstructorParameters)
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
The absolute resource path for accessing this upload via a GET request (content-disposition: inline).
|
|
92
|
-
*/
|
|
93
|
-
resourcePath: string
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
Returns the absolute download path (content-disposition: attachment).
|
|
97
|
-
*/
|
|
98
|
-
downloadPath: string
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
Deletes this uploaded file from the file system and also from the internal database.
|
|
102
|
-
*/
|
|
103
|
-
delete(): Promise<void>
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
type BoundHandler<T> = (this: T) => void
|
|
107
|
-
type AsyncKittenHandler = (request:KittenRequest, response:KittenResponse, next:AsyncKittenHandler) => Promise<void>
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
Abstract base class for lazily-loaded routes.
|
|
111
|
-
*/
|
|
112
|
-
export class LazilyLoadedRoute {
|
|
113
|
-
filePath:string
|
|
114
|
-
basePath:string
|
|
115
|
-
extension:string
|
|
116
|
-
pattern:string
|
|
117
|
-
method: 'use' | 'get'
|
|
118
|
-
|
|
119
|
-
constructor(filePath:string, basePath:string)
|
|
120
|
-
get handler():BoundHandler<LazilyLoadedRoute>&{filePath?: string}
|
|
121
|
-
lazilyLoadedhandler:AsyncKittenHandler
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
import type {default as WebSocket, BufferLike } from './ws/index.d.ts'
|
|
125
|
-
|
|
126
|
-
type WebSocketWithIsAlive = WebSocket & {isAlive:boolean}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
MessageSender class.
|
|
130
|
-
|
|
131
|
-
Provides a namespaced send() method for use in PageSocket.
|
|
132
|
-
*/
|
|
133
|
-
export class MessageSender {
|
|
134
|
-
socket:WebSocketWithIsAlive
|
|
135
|
-
sockets: [WebSocketWithIsAlive]
|
|
136
|
-
includeSelf: boolean
|
|
137
|
-
|
|
138
|
-
constructor (parameterObject: {
|
|
139
|
-
socket: WebSocketWithIsAlive,
|
|
140
|
-
connections: [WebSocketWithIsAlive],
|
|
141
|
-
includeSelf: boolean
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
Sends message either to all connections or to all connections excluding the current one (depending on value of `this.includeSelf`).
|
|
146
|
-
|
|
147
|
-
You can specify an optional swap target that intelligently wraps what you’re sending with the necessary envelope tag. This is normally rather confusing with htmx’s oob swaps, especially when inserting table rows.
|
|
148
|
-
|
|
149
|
-
See: https://htmx.org/attributes/hx-swap-oob/#using-alternate-swap-strategies
|
|
150
|
-
*/
|
|
151
|
-
send (message: BufferLike, swapTarget?: {
|
|
152
|
-
before?: string,
|
|
153
|
-
after?: string,
|
|
154
|
-
asFirstChildOf?: string,
|
|
155
|
-
asLastChildOf?: string
|
|
156
|
-
}):void
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
Kitten component class.
|
|
161
|
-
*/
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
This type definition required due to following shenanigans:
|
|
165
|
-
https://github.com/Microsoft/TypeScript/issues/20007#issuecomment-2255964704
|
|
166
|
-
*/
|
|
167
|
-
type JavaScriptFunction = (...args: any[]) => any
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
A class constructor.
|
|
171
|
-
*/
|
|
172
|
-
type Constructor<T> = new (...args: any[]) => T
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
Represents the render function of a component that is bound to component (as its `this`).
|
|
176
|
-
*/
|
|
177
|
-
type BoundComponentRenderFunction<T extends KittenComponent> =
|
|
178
|
-
((...args: Parameters<T['html']>) => Promise<Awaited<ReturnType<T['html']>>>)
|
|
179
|
-
& { boundObject: T }
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
An EventEmitter listener with a `remove()` method for cleanup.
|
|
183
|
-
*/
|
|
184
|
-
export class Listener {
|
|
185
|
-
/**
|
|
186
|
-
Create EventEmitter listener.
|
|
187
|
-
*/
|
|
188
|
-
constructor (target:EventEmitter, eventName:string, eventHandler:JavaScriptFunction)
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
Remove this listener from its EventEmitter.
|
|
192
|
-
*/
|
|
193
|
-
remove ():void
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
Kitten Component.
|
|
198
|
-
|
|
199
|
-
A class that makes it easier to author hierarchies of connected components.
|
|
200
|
-
|
|
201
|
-
Handles wiring up and disposal of event listeners for you and gives you an ergonomic, class-based way of writing maintainable applications that take advantage of Kitten’s Streaming HTML workflow while working with well-encapsulated, self-contained components in a clear hierarchy.
|
|
202
|
-
|
|
203
|
-
Extend and instantiate this class to create your own components (components, fragments, layout components, and pages) and provide at least an override for the `html()` method, which is just a function that returns `kitten.html`.
|
|
204
|
-
|
|
205
|
-
The example, below, updates a persisted counter via the + and - buttons (index.page.ts).
|
|
206
|
-
|
|
207
|
-
@example
|
|
208
|
-
// Initialise the database with a persisted counter object.
|
|
209
|
-
kitten.db.counter ??= { count: 0 }
|
|
210
|
-
|
|
211
|
-
export default class CounterPage extends kitten.Page {
|
|
212
|
-
counter
|
|
213
|
-
|
|
214
|
-
constructor () {
|
|
215
|
-
super()
|
|
216
|
-
this.counter = this.addChild(new Counter())
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
override html () {
|
|
220
|
-
return kitten.html`
|
|
221
|
-
<page css>
|
|
222
|
-
<h1>Counter</h1>
|
|
223
|
-
<${this.counter} />
|
|
224
|
-
`
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
class Counter extends kitten.Component {
|
|
229
|
-
override html () {
|
|
230
|
-
return kitten.html`
|
|
231
|
-
<div>
|
|
232
|
-
<div
|
|
233
|
-
id='count'
|
|
234
|
-
aria-live='assertive'
|
|
235
|
-
>
|
|
236
|
-
${kitten.db.counter.count}
|
|
237
|
-
</div>
|
|
238
|
-
<button name='update' connect data='{value: -1}' aria-label='decrement'>-</button>
|
|
239
|
-
<button name='update' connect data='{value: 1}' aria-label='increment'>+</button>
|
|
240
|
-
</div>
|
|
241
|
-
`
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
onUpdate (data: { value: number } ) {
|
|
245
|
-
kitten.db.counter.count += data.value
|
|
246
|
-
this.update()
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
*/
|
|
250
|
-
export class KittenComponent {
|
|
251
|
-
/**
|
|
252
|
-
Unique ID (crypto.uuid) of this component. You can overwrite it with your own if you like.
|
|
253
|
-
*/
|
|
254
|
-
id: string
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
Array of child components, if any. Instead of manipulating this directly, consider using the childrenOfType() method for operating on collections of components and the childOfType() method – or storing child component references as instance variables – for individual child components.
|
|
258
|
-
*/
|
|
259
|
-
_children: Array<KittenComponent>
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
Listeners this component has set up. There’s no reason to manipulate this directly. When you use the addEventHandler() method, Kitten handles the setup and tear-down of listeners for you automatically to avoid memory leaks.
|
|
263
|
-
*/
|
|
264
|
-
_listeners: Array<Listener>
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
Is this component attached to the live component hiearchy?
|
|
268
|
-
*/
|
|
269
|
-
_isAttached: boolean
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
Is the page this component is on connected to its client via WebSocket?
|
|
273
|
-
*/
|
|
274
|
-
_isConnected: boolean
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
Reference to page this component is attached to. Undefined if the page has not yet connected to the client.
|
|
278
|
-
*/
|
|
279
|
-
protected get _page(): KittenPage
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
This component’s parent. Undefined if component has not been added as a child to another component or a page via the addChild() method.
|
|
283
|
-
*/
|
|
284
|
-
protected get _parent(): KittenComponent
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
This pages socket. Not defined as private although you should only need it for low-level stuff.
|
|
288
|
-
*/
|
|
289
|
-
_socket: WebSocketWithIsAlive|undefined
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
All sockets active on this page. You *really* shouldn’t need to use this for anything. Not marked private as the Kitten web app uses it to introspect the page’s socket status.
|
|
293
|
-
*/
|
|
294
|
-
_sockets:Array<WebSocketWithIsAlive>|undefined
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
Constructor. Good place to set initial state, including instantiating child components.
|
|
298
|
-
*/
|
|
299
|
-
constructor ()
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
Returns a bound version of the render function. Use when adding a child component to your `kitten.html`.
|
|
303
|
-
*/
|
|
304
|
-
get component(): BoundComponentRenderFunction<this>
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
Required hook: override this method with your own render function
|
|
308
|
-
that returns `kitten.html`.
|
|
309
|
-
*/
|
|
310
|
-
html(parameterObject?: Record<string, any>):(string|Array<string>|Promise<string|Array<string>>)
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
Optional hook: override this method to run custom logic when the component has been added to the component hierarchy via addChild() (attached to its parent and, thus, to the component hiearchy).
|
|
314
|
-
|
|
315
|
-
At this point it will have a reference to its parent component, the page it’s on, and to any instance data that it might have, (at `this._parent`, `this._page`, and `this.`, respectively.)
|
|
316
|
-
|
|
317
|
-
However, there is no guarantee that the page this component is attached to has connected to the client via its automatic WebSocket. For that, rely on the
|
|
318
|
-
`onConnect()` handler instead.
|
|
319
|
-
*/
|
|
320
|
-
onAddToParent() :void
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
Optional hook: override this method to provide custom logic for your app to be run when the page this component is on connects to the client via its WebSocket.
|
|
324
|
-
|
|
325
|
-
This hook will get called not just for initially-rendered components when the page first connects but also for any components dynamically added to an already-connected page/component hierarchy after the fact.
|
|
326
|
-
|
|
327
|
-
(So you can be sure that this handler will be called once when a component is fully initialised on a connected page. This is a good place to add event handlers or to start streaming updates to the client.)
|
|
328
|
-
*/
|
|
329
|
-
onConnect(parameterObject: {
|
|
330
|
-
page?: KittenComponent,
|
|
331
|
-
request?: KittenRequest,
|
|
332
|
-
response?: KittenResponse
|
|
333
|
-
}):void
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
Optional hook: override this method to run custom logic when the page this component is on disconnects from the client via its WebSocket. (This usually means the page the about to be unloaded, either because the person is nagivating away from it or reloading it.)
|
|
337
|
-
*/
|
|
338
|
-
onDisconnect(parameterObject: {
|
|
339
|
-
page?: KittenComponent,
|
|
340
|
-
request?: KittenRequest,
|
|
341
|
-
response?: KittenResponse
|
|
342
|
-
}):void
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
Adds child component to this one.
|
|
346
|
-
|
|
347
|
-
Child components are entered into the event bubbling hierarchy and contain a reference to the page that they’re on.
|
|
348
|
-
*/
|
|
349
|
-
addChild<T extends KittenComponent> (component: T): T
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
Removes a child and returns a reference to it.
|
|
353
|
-
|
|
354
|
-
If child cannot be found, returns null.
|
|
355
|
-
|
|
356
|
-
Usually called by the child itself when it is being removed.
|
|
357
|
-
*/
|
|
358
|
-
removeChild<T extends KittenComponent>(component: T): T|null
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
Gets all components of a given type (useful when you have collections of child components that you want to render, say, in a list).
|
|
362
|
-
*/
|
|
363
|
-
childrenOfType<T extends KittenComponent>(type: Constructor<T>): T[]
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
Returns the first child component encountered of type T. Use when you know there is only one child of type T and you don’t want to a reference to it in your KittenComponent subclass.
|
|
367
|
-
|
|
368
|
-
Returns undefined if a child of type T cannot be found.
|
|
369
|
-
*/
|
|
370
|
-
childOfType<T extends KittenComponent>(type: Constructor<T>): T|undefined
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
Add event handler for an EventEmitter.
|
|
374
|
-
|
|
375
|
-
Event listening and listener clean-up are automatically handled so the author doesn’t have to worry about implementing this finickety aspect manually.
|
|
376
|
-
*/
|
|
377
|
-
addEventHandler (target: object, eventName: string, eventHandler: JavaScriptFunction):void
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
Helper for sending an updated version of this component to the page.
|
|
381
|
-
*/
|
|
382
|
-
update ():void
|
|
383
|
-
|
|
384
|
-
/**
|
|
385
|
-
Helper for removing this component from the live component hierachy.
|
|
386
|
-
|
|
387
|
-
Also handles removal of event listeners for itself and all its children so we don’t have any leaks.
|
|
388
|
-
*/
|
|
389
|
-
remove ():void
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
Emits an event that is bubbled to all of its children.
|
|
393
|
-
|
|
394
|
-
This is a good way, for example, to set a specific state on a page or component, where each child component knows what it should look like in that state.
|
|
395
|
-
|
|
396
|
-
The event name → event handler naming convention mapping works exactly like it does with events received from the client.
|
|
397
|
-
|
|
398
|
-
@param {string} eventName
|
|
399
|
-
@param {any} data
|
|
400
|
-
@param {any} event
|
|
401
|
-
@param {string} target - if provided, and is prefixed by this component’s Kitten ID, we only bubble to the target and its children.
|
|
402
|
-
@param {any} self - if provided is used to bypass target check for object doing the bubbling.
|
|
403
|
-
*/
|
|
404
|
-
emit (eventName:string, data?:any, event?:any, target?:string, self?:any):void
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
Helper for adding an event handler for a page event and streaming an updated version of the component to the client (a common pattern).
|
|
408
|
-
|
|
409
|
-
@param {string} eventName
|
|
410
|
-
*/
|
|
411
|
-
updateOnEvent (eventName:string):void
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
Helper (handler) for sending an updated version of this component to the page.
|
|
415
|
-
*/
|
|
416
|
-
sendUpdatedComponentToPage ():void
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
KittenPage class.
|
|
421
|
-
|
|
422
|
-
A KittenPage is a specialised KittenComponent that represents a live page in memory (a live page is one that has an automatic WebSocket connection to the page rendered by the PageRoute).
|
|
423
|
-
|
|
424
|
-
A KittenPage constitutes the root of the server-side component hierarchy.
|
|
425
|
-
|
|
426
|
-
Instance data in a `KittenPage` instance sticks around for the lifetime of the page (e.g., until a reload or a navigation event away from it in the browser).
|
|
427
|
-
|
|
428
|
-
If you need greater persistence, use session storage (`request.session`) or the built-in JSDB database (`kitten.db`).
|
|
429
|
-
*/
|
|
430
|
-
export class KittenPage extends KittenComponent {
|
|
431
|
-
request: KittenRequest
|
|
432
|
-
response: KittenResponse
|
|
433
|
-
session: Session
|
|
434
|
-
socket: WebSocketWithIsAlive
|
|
435
|
-
sockets: [WebSocketWithIsAlive]
|
|
436
|
-
data: Record<string, any>
|
|
437
|
-
everyone: MessageSender
|
|
438
|
-
everyoneElse: MessageSender
|
|
439
|
-
|
|
440
|
-
/**
|
|
441
|
-
Construct new KittenPage instance.
|
|
442
|
-
|
|
443
|
-
Note that the `request` and `response` properties are set after construction by Kitten to keep the constructor easy to override in your own subclasses. (The constructor is a good place to initialise known child components, etc.)
|
|
444
|
-
|
|
445
|
-
Every page has a unique ID (inherited from KittenComponent) and can hold ephemeral page-level instance data. e.g., child components. (Page storage is ephemeral is that it only lasts for the lifetime of a page in the browser and is destroyed when the page is closed or reloaded.)
|
|
446
|
-
|
|
447
|
-
If you need greater persistence, please use session storage (request.session) or the built-in JavaScript Database (JSDB) that you can reference at `kitten.db`.
|
|
448
|
-
|
|
449
|
-
A page is known as an “authored page” if the author of the Kitten app/site wrote and exported a KittenPage subclass in the route (as opposed to exporting a simple function and function-based event handlers that then resulted in a generic page being created by Kitten’s `PageRoute` class). Keeping track of this is an optimisation that enables the `PageSocketRoute` to not have to import the source file again if the page was authored as a page instance and thus already contains its event handlers (as opposed to the event handlers being exported as separate functions that have be read in and mixed into the generic `KittenPage` instance by the `PageSocketRoute`).
|
|
450
|
-
*/
|
|
451
|
-
constructor()
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
The page has connected to its WebSocket. The list of sockets and the specific socket for this page are passed for storage on the page and the list of event handlers imported from the page (when using function-based page routes), if any, to be mixed into this instance as methods.
|
|
455
|
-
*/
|
|
456
|
-
connect (socket: WebSocketWithIsAlive, sockets: [WebSocketWithIsAlive], eventHandlers: Record<string, JavaScriptFunction>):void
|
|
457
|
-
|
|
458
|
-
/**
|
|
459
|
-
Dynamically add specified event handler for specified event name.
|
|
460
|
-
|
|
461
|
-
@deprecated Instead of `on(eventName, handler)`, export `onEventName()` from your page (or add `onEventName()` method to your `kitten.Page` subclass if using class-based page routes.)
|
|
462
|
-
*/
|
|
463
|
-
on (eventName: string, eventHandler: (data: any) => void):void
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
Send specified message to just this page’s socket.
|
|
467
|
-
|
|
468
|
-
You can specify an optional swap target that intelligently wraps what you’re sending with the necessary envelope tag. This is normally rather confusing with htmx’s oob swaps, especially when inserting table rows. See: https://htmx.org/attributes/hx-swap-oob/#using-alternate-swap-strategies
|
|
469
|
-
*/
|
|
470
|
-
send (message: BufferLike|Promise<BufferLike>, swapTarget?: {
|
|
471
|
-
before?: string,
|
|
472
|
-
after?: string,
|
|
473
|
-
asFirstChildOf?: string,
|
|
474
|
-
asLastChildOf?: string
|
|
475
|
-
}):void
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
/**
|
|
479
|
-
Request and response types.
|
|
480
|
-
|
|
481
|
-
These vary per route type but are all built on the base of Polka’s request and response objects which are, themselves, based on the incoming message and server response types of Node’s own http module.
|
|
482
|
-
*/
|
|
483
|
-
|
|
484
|
-
import type { IncomingMessage, ServerResponse } from 'http'
|
|
485
|
-
|
|
486
|
-
export interface ParsedURL {
|
|
487
|
-
pathname: string
|
|
488
|
-
search: string
|
|
489
|
-
query: Record<string, string | string[]> | void
|
|
490
|
-
raw: string
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
export type PolkaResponse = ServerResponse
|
|
494
|
-
|
|
495
|
-
export interface PolkaRequest extends IncomingMessage {
|
|
496
|
-
url: string
|
|
497
|
-
method: string
|
|
498
|
-
originalUrl: string
|
|
499
|
-
params: Record<string, string>
|
|
500
|
-
path: string
|
|
501
|
-
search: string
|
|
502
|
-
query: Record<string,string>
|
|
503
|
-
body?: any
|
|
504
|
-
_decoded?: true
|
|
505
|
-
_parsedUrl: ParsedURL
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
/**
|
|
509
|
-
The request objects received by Kitten routes.
|
|
510
|
-
|
|
511
|
-
Based on Polka’s request object with extra Kitten-specific properties and methods.
|
|
512
|
-
*/
|
|
513
|
-
export interface KittenRequest extends PolkaRequest {
|
|
514
|
-
/**
|
|
515
|
-
The raw body of non-multipart requests. Used, for example, for validation webhook signatures.
|
|
516
|
-
*/
|
|
517
|
-
rawBody: Buffer
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
Check if the incoming request contains the `"Content-Type"` header field, and, if so, if it contains the specified mime `type`.
|
|
521
|
-
|
|
522
|
-
Examples:
|
|
523
|
-
|
|
524
|
-
// With Content-Type: text/html; charset=utf-8
|
|
525
|
-
req.is('html');
|
|
526
|
-
req.is('text/html');
|
|
527
|
-
req.is('text/*');
|
|
528
|
-
// => true
|
|
529
|
-
|
|
530
|
-
// When Content-Type is application/json
|
|
531
|
-
req.is('json');
|
|
532
|
-
req.is('application/json');
|
|
533
|
-
req.is('application/*');
|
|
534
|
-
// => true
|
|
535
|
-
req.is('html');
|
|
536
|
-
// => false
|
|
537
|
-
|
|
538
|
-
@link https://github.com/expressjs/express/blob/master/lib/request.js#L231
|
|
539
|
-
*/
|
|
540
|
-
is (types:Array<string>|string):string|false|null
|
|
541
|
-
|
|
542
|
-
/**
|
|
543
|
-
Represents a browser session.
|
|
544
|
-
|
|
545
|
-
Sessions are automatically persisted (and expired) in Kitten’s internal database.
|
|
546
|
-
|
|
547
|
-
Used by Kitten to provide automatic authentication for Small Web places.
|
|
548
|
-
|
|
549
|
-
You can also add/update arbitrary session data by adding/updating properties on this object in your routes (this object maps a session object in the `kitten._db.sessions` table in the internal database).
|
|
550
|
-
|
|
551
|
-
You can emit events on a session in order to communicate with other browser tabs and windows that share the same session.
|
|
552
|
-
|
|
553
|
-
@example
|
|
554
|
-
export default function ({ request }) {
|
|
555
|
-
request.session.kittens ??= { count: 1 }
|
|
556
|
-
|
|
557
|
-
return kitten.html`
|
|
558
|
-
<h1>Kitten count</h1>
|
|
559
|
-
<p>${'🐱️'.repeat(request.session.kittens.count++)}</p>
|
|
560
|
-
`
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
@see https://kitten.small-web.org/tutorials/sessions/
|
|
564
|
-
|
|
565
|
-
@remarks
|
|
566
|
-
Do not save custom classes on request.session or JSDB will throw an error when it tries to open the internal `_db` database and cannot find your custom class to instantiate.
|
|
567
|
-
|
|
568
|
-
If you want persisted session-level custom objects, store them in your own database table in `kitten.db` keyed by `session.id`.
|
|
569
|
-
*/
|
|
570
|
-
session: Session & {
|
|
571
|
-
[key: string]: any
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
type TypedArray = Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|BigInt64Array|BigUint64Array
|
|
576
|
-
|
|
577
1
|
/**
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
Based on Polka’s response object with extra Kitten-specific properties and methods.
|
|
581
|
-
*/
|
|
582
|
-
export interface KittenResponse extends PolkaResponse {
|
|
583
|
-
/**
|
|
584
|
-
JSON.stringifies passed data and ends response with inline JSON using proper headers.
|
|
585
|
-
*/
|
|
586
|
-
json (data:any):void
|
|
587
|
-
|
|
588
|
-
/**
|
|
589
|
-
JSON.stringifies passed data and ends response with JSON attachment using proper headers and requested file name (or data.json as fallback if no file name is provided).
|
|
590
|
-
*/
|
|
591
|
-
jsonFile (data:any, fileName?:string):void
|
|
592
|
-
|
|
593
|
-
/**
|
|
594
|
-
Ends response with a file. Optionally, uses passed file name (or `'download'` as fallback) and passed mime type (or `'application/octet-stream'` as fallback).
|
|
595
|
-
*/
|
|
596
|
-
file (data:string|Buffer|TypedArray|DataView, fileName?:string, mimeType?:string):void
|
|
597
|
-
|
|
598
|
-
/**
|
|
599
|
-
Ends response with 200 OK response code, and the response body, if any (and `''` if not).
|
|
600
|
-
|
|
601
|
-
@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200
|
|
602
|
-
*/
|
|
603
|
-
ok (body?:string):void
|
|
604
|
-
|
|
605
|
-
/**
|
|
606
|
-
Ends response with 201 Created response code, and the response body, if any (and `''` if not).
|
|
2
|
+
Kitten Types (@small-web/kitten-types)
|
|
607
3
|
|
|
608
|
-
|
|
609
|
-
*/
|
|
610
|
-
created (body?:string):void
|
|
4
|
+
Reusable Kitten types for explicit annotations.
|
|
611
5
|
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
Alias: seeOther
|
|
616
|
-
*/
|
|
617
|
-
get (location:string):void
|
|
618
|
-
|
|
619
|
-
/**
|
|
620
|
-
Redirect (temporary; 307) to requested location without changing the request method.
|
|
621
|
-
*/
|
|
622
|
-
redirect (location:string):void
|
|
623
|
-
|
|
624
|
-
/**
|
|
625
|
-
Redirect (permanentl 308) to requested location without changing the request method.
|
|
626
|
-
*/
|
|
627
|
-
permanentRedirect (location:string):void
|
|
628
|
-
|
|
629
|
-
/**
|
|
630
|
-
400 Bad Request
|
|
631
|
-
|
|
632
|
-
Indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
|
|
633
|
-
|
|
634
|
-
@link https://httpwg.org/specs/rfc9110.html#rfc.section.15.5.1
|
|
635
|
-
*/
|
|
636
|
-
badRequest (body?:string):void
|
|
637
|
-
|
|
638
|
-
/**
|
|
639
|
-
“401 Unauthorized” (actually, unauthenticated) response.
|
|
640
|
-
|
|
641
|
-
Aliases: unauthorised, unauthorized.
|
|
642
|
-
*/
|
|
643
|
-
unauthenticated (body?:string):void
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
403 Forbidden response. This should be returned if the request is authenticated but lacks the authorisation (i.e., sufficient rights) to access the resource.
|
|
647
|
-
|
|
648
|
-
If the request requires authentication but has not been authenticated, you should return a “401 Unauthorized” (actually: unauthenticated) response.
|
|
649
|
-
|
|
650
|
-
@see unauthenticated
|
|
651
|
-
*/
|
|
652
|
-
forbidden (body?:string):void
|
|
653
|
-
|
|
654
|
-
/**
|
|
655
|
-
404 Not Found response.
|
|
656
|
-
*/
|
|
657
|
-
notFound (body?:string):void
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
500 Internal Server Error response.
|
|
661
|
-
|
|
662
|
-
Alias: internalServerError.
|
|
663
|
-
*/
|
|
664
|
-
error (body?:string):void
|
|
665
|
-
|
|
666
|
-
/**
|
|
667
|
-
General shorthand helper for setting the status code and ending the response with an optional body.
|
|
668
|
-
*/
|
|
669
|
-
withCode (statusCode:number, body?:string):void
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
Kitten’s POST request object.
|
|
674
|
-
|
|
675
|
-
If the POST route had a multi-part form with file uploads, they will be handled automatically by Kitten and you can find them in the `uploads` property.
|
|
676
|
-
|
|
677
|
-
@see https://kitten.small-web.org/tutorials/multipart-forms-and-file-uploads/
|
|
6
|
+
Generated from Kitten’s TypeScript source code
|
|
7
|
+
(see kitten/ – regenerated via `npm run sync-types` in the Kitten repository).
|
|
678
8
|
*/
|
|
679
|
-
export interface KittenPostRequest extends KittenRequest {
|
|
680
|
-
uploads: Upload[]
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
/* Crypto */
|
|
684
|
-
|
|
685
|
-
type Hex = Uint8Array | string;
|
|
686
|
-
type PrivKey = Hex | bigint | number;
|
|
687
|
-
|
|
688
|
-
export class Point {
|
|
689
|
-
readonly x: bigint;
|
|
690
|
-
readonly y: bigint;
|
|
691
|
-
static BASE: Point;
|
|
692
|
-
static ZERO: Point;
|
|
693
|
-
_WINDOW_SIZE?: number;
|
|
694
|
-
constructor(x: bigint, y: bigint);
|
|
695
|
-
_setWindowSize(windowSize: number): void;
|
|
696
|
-
static fromHex(hex: Hex, strict?: boolean): Point;
|
|
697
|
-
static fromPrivateKey(privateKey: PrivKey): Promise<Point>;
|
|
698
|
-
toRawBytes(): Uint8Array;
|
|
699
|
-
toHex(): string;
|
|
700
|
-
toX25519(): Uint8Array;
|
|
701
|
-
isTorsionFree(): boolean;
|
|
702
|
-
equals(other: Point): boolean;
|
|
703
|
-
negate(): Point;
|
|
704
|
-
add(other: Point): Point;
|
|
705
|
-
subtract(other: Point): Point;
|
|
706
|
-
multiply(scalar: number | bigint): Point;
|
|
707
|
-
}
|
|
708
9
|
|
|
709
|
-
export
|
|
710
|
-
readonly r: Point;
|
|
711
|
-
readonly s: bigint;
|
|
712
|
-
constructor(r: Point, s: bigint);
|
|
713
|
-
static fromHex(hex: Hex): Signature;
|
|
714
|
-
assertValidity(): this;
|
|
715
|
-
toRawBytes(): Uint8Array;
|
|
716
|
-
toHex(): string;
|
|
717
|
-
}
|
|
10
|
+
export type * from './kitten/src/types/types.d.ts'
|