@based/functions 3.0.2 → 3.1.1-alpha

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/src/functions.ts DELETED
@@ -1,367 +0,0 @@
1
- import type { Required } from 'utility-types'
2
- import { Context, HttpSession } from './context.js'
3
- import { BasedFunctionClient } from './client.js'
4
- import { BasedDataStream } from './stream.js'
5
- import { Authorize } from './auth.js'
6
-
7
- export type ObservableUpdateFunction<K = any> = (
8
- data: K,
9
- checksum?: number,
10
- err?: any,
11
- cache?: Uint8Array,
12
- diff?: any,
13
- fromChecksum?: number,
14
- isDeflate?: boolean
15
- ) => void
16
-
17
- export type ObserveErrorListener = (err: any) => void
18
-
19
- export type HttpHeaders = {
20
- [header: string]: number | string | (string | number)[]
21
- }
22
-
23
- export type SendHttpResponse = (
24
- responseData: any,
25
- headers?: HttpHeaders,
26
- status?: string | number
27
- ) => void
28
-
29
- export type HttpResponse<P = any, K = any> = (
30
- based: BasedFunctionClient,
31
- payload: P,
32
- responseData: K,
33
- send: SendHttpResponse,
34
- ctx: Context<HttpSession>
35
- ) => Promise<void>
36
-
37
- export type BasedFunction<P = any, K = any> = (
38
- based: BasedFunctionClient,
39
- payload: P,
40
- ctx: Context
41
- ) => Promise<K>
42
-
43
- export type BasedAppFunction = (
44
- based: BasedFunctionClient,
45
- assets: {
46
- css: {
47
- text: Promise<string>
48
- url: string
49
- }
50
- js: {
51
- text: Promise<string>
52
- url: string
53
- }
54
- favicon: {
55
- url: string
56
- content: string
57
- path: string
58
- }
59
- },
60
- ctx: Context
61
- ) => Promise<string>
62
-
63
- export type StreamPayload<P = any> = {
64
- payload?: P
65
- mimeType: string
66
- size: number
67
- stream: BasedDataStream
68
- fileName?: string
69
- extension?: string
70
- fn?: string
71
- seqId?: number
72
- }
73
-
74
- export type BasedStreamFunction<P = any, K = any> = BasedFunction<
75
- StreamPayload<P>,
76
- K
77
- >
78
-
79
- export type BasedQueryFunction<P = any, K = any> =
80
- | ((
81
- based: BasedFunctionClient,
82
- payload: P,
83
- update: ObservableUpdateFunction<K>,
84
- error: ObserveErrorListener
85
- ) => Promise<() => void>)
86
- | ((
87
- based: BasedFunctionClient,
88
- payload: P,
89
- update: ObservableUpdateFunction<K>,
90
- error: ObserveErrorListener
91
- ) => () => void)
92
-
93
- export type BasedChannelFunction<P = any, K = any> = (
94
- based: BasedFunctionClient,
95
- payload: P,
96
- id: number,
97
- update: ChannelMessageFunction<K>,
98
- error: (err?: any) => void
99
- ) => () => void
100
-
101
- export type BasedChannelPublishFunction<P = any, M = any> = (
102
- based: BasedFunctionClient,
103
- payload: P,
104
- message: M,
105
- id: number,
106
- ctx: Context
107
- ) => void
108
-
109
- export type ChannelMessageFunction<M = any> = (message: M) => void
110
-
111
- export type ChannelMessageFunctionInternal<K = any> = (
112
- message: K,
113
- err?: any
114
- ) => void
115
-
116
- export type BasedJobFunction =
117
- | ((based: BasedFunctionClient) => Promise<() => void>)
118
- | ((based: BasedFunctionClient) => () => void)
119
-
120
- export type UninstallFunction = () => Promise<void>
121
-
122
- // ------------ Config -------------------
123
- type FunctionConfigShared = {
124
- /** Function name */
125
- name?: string
126
- /** In addition to the name, a function can have a custom path for HTTP requests.
127
- * For example: `path: 'my/custom/path'` will result in the function being
128
- * available with a request to `env.based.io/my/custom/path`
129
- */
130
- path?: string
131
- /** In bytes. `-1` indicates no size limit */
132
- maxPayloadSize?: number
133
- /** Cost in tokens for this function call. */
134
- rateLimitTokens?: number
135
- /** A function marked as `public` will skip the call to authorize. */
136
- public?: boolean
137
- /** Array of headers that this function expects to receive. */
138
- headers?: string[]
139
- /** A function marked as `internalOnly` will only be accessible from other server side functions,
140
- * and not through the public internet.
141
- */
142
- internalOnly?: boolean
143
- /** Can hold extra information about a spec */
144
- data?: any
145
- /** Unistall after idle, in ms -1 indicates endless */
146
- uninstallAfterIdleTime?: number
147
- /** Hook that fires on uninstall of the function e.g. to clean up database connections */
148
- uninstall?: UninstallFunction
149
- /** Specific authorize for this function */
150
- authorize?: Authorize
151
- /** Relay allows functions to relay traffic to another `@based/server`
152
- `Currently not supported for `stream`
153
-
154
- ```js
155
- new BasedServer({
156
- clients: { events: BasedClient },
157
- functions: {
158
- specs:
159
- somethingToRelay: {
160
- relay: { client: 'events', target: 'hello' },
161
- type: 'function'
162
- })
163
- }
164
- }
165
- })
166
- ```
167
- */
168
- relay?: {
169
- client: string
170
- target?: string
171
- }
172
- /** Function version */
173
- version?: number
174
- /** Used inernaly to check if a function is ready to uninstall */
175
- timeoutCounter?: number
176
- }
177
-
178
- type FunctionConfigSharedComplete = Required<
179
- FunctionConfigShared,
180
- 'maxPayloadSize' | 'rateLimitTokens' | 'version' | 'name'
181
- >
182
-
183
- export type BasedFunctionTypes =
184
- | 'channel'
185
- | 'query'
186
- | 'function'
187
- | 'stream'
188
- | 'app'
189
- | 'job'
190
-
191
- type BasedChannelFunctionConfig = {
192
- /** Function type `channel, function, query, stream, authorize` */
193
- type: 'channel'
194
- /** Channel subscriber
195
-
196
- ```js
197
- const subscribe = (based, payload, id, update) => {
198
- let cnt = 0
199
- const interval = setInterval(() => {
200
- update(++cnt)
201
- })
202
- return () => clearInterval(cnt)
203
- }
204
- ```
205
- */
206
- subscriber?: BasedChannelFunction
207
- /** Channel publisher
208
-
209
- ```js
210
- const publisher = (based, payload, msg, id) => {
211
- publishToChannel(id, msg)
212
- }
213
- ```
214
- */
215
- publisher?: BasedChannelPublishFunction
216
- /** Makes only the publisher public */
217
- publicPublisher?: boolean
218
- /** How long should the channel subscriber remain active after all subscribers are gone, in ms */
219
- closeAfterIdleTime?: number
220
- /** Only for Publisher */
221
- httpResponse?: HttpResponse
222
- /** Throttle amount of outgoing messages, in milliseconds */
223
- throttle?: number
224
- }
225
-
226
- type BasedCallFunctionConfig = {
227
- /** Function type `channel, function, query, stream` */
228
- type: 'function'
229
- fn?: BasedFunction
230
- httpResponse?: HttpResponse
231
- }
232
-
233
- type BasedQueryFunctionConfig = {
234
- /** Function type `channel, function, query, stream` */
235
- type: 'query'
236
- fn?: BasedQueryFunction
237
- httpResponse?: HttpResponse
238
- /** How long should the query function remain active after all subscribers are gone, in ms */
239
- closeAfterIdleTime?: number
240
- /** Throttle amount of outgoing messages, in milliseconds */
241
- throttle?: number
242
- }
243
-
244
- type BasedStreamFunctionConfig = {
245
- /** Function type `channel, function, query, stream` */
246
- type: 'stream'
247
- fn: BasedStreamFunction
248
- }
249
-
250
- type BasedAppFunctionConfig = {
251
- type: 'app'
252
- main: string
253
- path?: string
254
- favicon?: string
255
- }
256
-
257
- type BasedJobFunctionConfig = {
258
- type: 'job'
259
- fn?: BasedFunction
260
- }
261
-
262
- export type BasedFunctionConfig<
263
- T extends BasedFunctionTypes = BasedFunctionTypes
264
- > = T extends 'channel'
265
- ? BasedChannelFunctionConfig & FunctionConfigShared
266
- : T extends 'function'
267
- ? BasedCallFunctionConfig & FunctionConfigShared
268
- : T extends 'query'
269
- ? BasedQueryFunctionConfig & FunctionConfigShared
270
- : T extends 'stream'
271
- ? BasedStreamFunctionConfig & FunctionConfigShared
272
- : T extends 'job'
273
- ? BasedJobFunctionConfig & FunctionConfigShared
274
- : T extends 'app'
275
- ? BasedAppFunctionConfig & FunctionConfigShared
276
- :
277
- | (BasedChannelFunctionConfig & FunctionConfigShared)
278
- | (BasedCallFunctionConfig & FunctionConfigShared)
279
- | (BasedQueryFunctionConfig & FunctionConfigShared)
280
- | (BasedStreamFunctionConfig & FunctionConfigShared)
281
- | (BasedJobFunctionConfig & FunctionConfigShared)
282
- | (BasedAppFunctionConfig & FunctionConfigShared)
283
-
284
- export type BasedFunctionConfigComplete<
285
- T extends BasedFunctionTypes = BasedFunctionTypes
286
- > = T extends 'channel'
287
- ? BasedChannelFunctionConfig & FunctionConfigSharedComplete
288
- : T extends 'function'
289
- ? BasedCallFunctionConfig & FunctionConfigSharedComplete
290
- : T extends 'query'
291
- ? BasedQueryFunctionConfig & FunctionConfigSharedComplete
292
- : T extends 'stream'
293
- ? BasedStreamFunctionConfig & FunctionConfigSharedComplete
294
- : T extends 'job'
295
- ? BasedJobFunctionConfig & FunctionConfigSharedComplete
296
- : T extends 'app'
297
- ? BasedAppFunctionConfig & FunctionConfigSharedComplete
298
- :
299
- | (BasedChannelFunctionConfig & FunctionConfigSharedComplete)
300
- | (BasedCallFunctionConfig & FunctionConfigSharedComplete)
301
- | (BasedQueryFunctionConfig & FunctionConfigSharedComplete)
302
- | (BasedStreamFunctionConfig & FunctionConfigSharedComplete)
303
- | (BasedJobFunctionConfig & FunctionConfigSharedComplete)
304
- | (BasedAppFunctionConfig & FunctionConfigSharedComplete)
305
-
306
- export type BasedAuthorizeFunctionConfig = {
307
- /** Function type `authorize` */
308
- type: 'authorize'
309
- fn?: Authorize
310
- }
311
-
312
- export type BasedRoute<
313
- T extends BasedFunctionTypes = BasedFunctionTypes,
314
- R extends keyof BasedFunctionConfig = 'type' | 'name'
315
- > = Required<Partial<BasedFunctionConfig<T>>, R>
316
-
317
- export type BasedRouteComplete<
318
- T extends BasedFunctionTypes = BasedFunctionTypes
319
- > = Required<
320
- Partial<BasedFunctionConfig<T>>,
321
- 'type' | 'name' | 'maxPayloadSize' | 'rateLimitTokens'
322
- >
323
-
324
- export function isBasedRoute<T extends BasedFunctionTypes>(
325
- type: T,
326
- route: any
327
- ): route is BasedRoute<T> {
328
- return (
329
- route &&
330
- typeof route === 'object' &&
331
- route.type === type &&
332
- typeof route.name === 'string'
333
- )
334
- }
335
-
336
- export function isAnyBasedRoute(route: any): route is BasedRoute {
337
- return (
338
- route &&
339
- typeof route === 'object' &&
340
- (route.type === 'channel' ||
341
- route.type === 'query' ||
342
- route.type === 'function' ||
343
- route.type === 'stream') &&
344
- typeof route.name === 'string'
345
- )
346
- }
347
-
348
- export function isBasedFunctionConfig<T extends BasedFunctionTypes>(
349
- type: T,
350
- config: any
351
- ): config is BasedFunctionConfig<T> {
352
- return isBasedRoute(type, config)
353
- }
354
-
355
- export function isAnyBasedFunctionConfig(
356
- config: any
357
- ): config is BasedFunctionConfig {
358
- return isAnyBasedRoute(config)
359
- }
360
-
361
- export type BasedRoutes = {
362
- [name: string]: BasedRoute<BasedFunctionTypes, 'type'>
363
- }
364
-
365
- export type BasedFunctionConfigs = {
366
- [name: string]: BasedFunctionConfig<BasedFunctionTypes>
367
- }
package/src/geo.ts DELETED
@@ -1,322 +0,0 @@
1
- export type CountryCode =
2
- | 'AF'
3
- | 'AL'
4
- | 'DZ'
5
- | 'AS'
6
- | 'AD'
7
- | 'AO'
8
- | 'AI'
9
- | 'AQ'
10
- | 'AG'
11
- | 'AR'
12
- | 'AM'
13
- | 'AW'
14
- | 'AU'
15
- | 'AT'
16
- | 'AZ'
17
- | 'BS'
18
- | 'BH'
19
- | 'BD'
20
- | 'BB'
21
- | 'BY'
22
- | 'BE'
23
- | 'BZ'
24
- | 'BJ'
25
- | 'BM'
26
- | 'BT'
27
- | 'BO'
28
- | 'BQ'
29
- | 'BA'
30
- | 'BW'
31
- | 'BV'
32
- | 'BR'
33
- | 'IO'
34
- | 'BN'
35
- | 'BG'
36
- | 'BF'
37
- | 'BI'
38
- | 'KH'
39
- | 'CM'
40
- | 'CA'
41
- | 'CV'
42
- | 'KY'
43
- | 'CF'
44
- | 'TD'
45
- | 'CL'
46
- | 'CN'
47
- | 'CX'
48
- | 'CC'
49
- | 'CO'
50
- | 'KM'
51
- | 'CG'
52
- | 'CD'
53
- | 'CK'
54
- | 'CR'
55
- | 'HR'
56
- | 'CU'
57
- | 'CW'
58
- | 'CY'
59
- | 'CZ'
60
- | 'CI'
61
- | 'DK'
62
- | 'DJ'
63
- | 'DM'
64
- | 'DO'
65
- | 'EC'
66
- | 'EG'
67
- | 'SV'
68
- | 'GQ'
69
- | 'ER'
70
- | 'EE'
71
- | 'ET'
72
- | 'FK'
73
- | 'FO'
74
- | 'FJ'
75
- | 'FI'
76
- | 'FR'
77
- | 'GF'
78
- | 'PF'
79
- | 'TF'
80
- | 'GA'
81
- | 'GM'
82
- | 'GE'
83
- | 'DE'
84
- | 'GH'
85
- | 'GI'
86
- | 'GR'
87
- | 'GL'
88
- | 'GD'
89
- | 'GP'
90
- | 'GU'
91
- | 'GT'
92
- | 'GG'
93
- | 'GN'
94
- | 'GW'
95
- | 'GY'
96
- | 'HT'
97
- | 'HM'
98
- | 'VA'
99
- | 'HN'
100
- | 'HK'
101
- | 'HU'
102
- | 'IS'
103
- | 'IN'
104
- | 'ID'
105
- | 'IR'
106
- | 'IQ'
107
- | 'IE'
108
- | 'IM'
109
- | 'IL'
110
- | 'IT'
111
- | 'JM'
112
- | 'JP'
113
- | 'JE'
114
- | 'JO'
115
- | 'KZ'
116
- | 'KE'
117
- | 'KI'
118
- | 'KP'
119
- | 'KR'
120
- | 'KW'
121
- | 'KG'
122
- | 'LA'
123
- | 'LV'
124
- | 'LB'
125
- | 'LS'
126
- | 'LR'
127
- | 'LY'
128
- | 'LI'
129
- | 'LT'
130
- | 'LU'
131
- | 'MO'
132
- | 'MK'
133
- | 'MG'
134
- | 'MW'
135
- | 'MY'
136
- | 'MV'
137
- | 'ML'
138
- | 'MT'
139
- | 'MH'
140
- | 'MQ'
141
- | 'MR'
142
- | 'MU'
143
- | 'YT'
144
- | 'MX'
145
- | 'FM'
146
- | 'MD'
147
- | 'MC'
148
- | 'MN'
149
- | 'ME'
150
- | 'MS'
151
- | 'MA'
152
- | 'MZ'
153
- | 'MM'
154
- | 'NA'
155
- | 'NR'
156
- | 'NP'
157
- | 'NL'
158
- | 'NC'
159
- | 'NZ'
160
- | 'NI'
161
- | 'NE'
162
- | 'NG'
163
- | 'NU'
164
- | 'NF'
165
- | 'MP'
166
- | 'NO'
167
- | 'OM'
168
- | 'PK'
169
- | 'PW'
170
- | 'PS'
171
- | 'PA'
172
- | 'PG'
173
- | 'PY'
174
- | 'PE'
175
- | 'PH'
176
- | 'PN'
177
- | 'PL'
178
- | 'PT'
179
- | 'PR'
180
- | 'QA'
181
- | 'RO'
182
- | 'RU'
183
- | 'RW'
184
- | 'RE'
185
- | 'BL'
186
- | 'SH'
187
- | 'KN'
188
- | 'LC'
189
- | 'MF'
190
- | 'PM'
191
- | 'VC'
192
- | 'WS'
193
- | 'SM'
194
- | 'ST'
195
- | 'SA'
196
- | 'SN'
197
- | 'RS'
198
- | 'SC'
199
- | 'SL'
200
- | 'SG'
201
- | 'SX'
202
- | 'SK'
203
- | 'SI'
204
- | 'SB'
205
- | 'SO'
206
- | 'ZA'
207
- | 'GS'
208
- | 'SS'
209
- | 'ES'
210
- | 'LK'
211
- | 'SD'
212
- | 'SR'
213
- | 'SJ'
214
- | 'SZ'
215
- | 'SE'
216
- | 'CH'
217
- | 'SY'
218
- | 'TW'
219
- | 'TJ'
220
- | 'TZ'
221
- | 'TH'
222
- | 'TL'
223
- | 'TG'
224
- | 'TK'
225
- | 'TO'
226
- | 'TT'
227
- | 'TN'
228
- | 'TR'
229
- | 'TM'
230
- | 'TC'
231
- | 'TV'
232
- | 'UG'
233
- | 'UA'
234
- | 'AE'
235
- | 'GB'
236
- | 'US'
237
- | 'UM'
238
- | 'UY'
239
- | 'UZ'
240
- | 'VU'
241
- | 'VE'
242
- | 'VN'
243
- | 'VG'
244
- | 'VI'
245
- | 'WF'
246
- | 'EH'
247
- | 'YE'
248
- | 'ZM'
249
- | 'ZW'
250
- | 'AX'
251
-
252
- type Char =
253
- | 'A'
254
- | 'B'
255
- | 'C'
256
- | 'D'
257
- | 'E'
258
- | 'F'
259
- | 'G'
260
- | 'H'
261
- | 'I'
262
- | 'J'
263
- | 'K'
264
- | 'L'
265
- | 'M'
266
- | 'N'
267
- | 'O'
268
- | 'P'
269
- | 'Q'
270
- | 'R'
271
- | 'S'
272
- | 'T'
273
- | 'U'
274
- | 'V'
275
- | 'W'
276
- | 'X'
277
- | 'Y'
278
- | 'Z'
279
- | '0'
280
- | '1'
281
- | '2'
282
- | '3'
283
- | '4'
284
- | '5'
285
- | '6'
286
- | '7'
287
- | '8'
288
- | '9'
289
-
290
- export type GeoRegionISO = `${Char}${Char}` | `${Char}${Char}${Char}`
291
-
292
- export type Geo = {
293
- country: CountryCode | 'unknown'
294
- /**
295
- Ip formatted as ipv6
296
-
297
- e.g. `2001:0db8:85a3:0000:0000:8a2e:0370:7334`
298
- */
299
- ip: string
300
- /**
301
- Region subdivisions
302
-
303
- e.g. `[{ iso: 'NH', name: 'north-holland' }]`
304
- */
305
- regions: {
306
- iso: GeoRegionISO
307
- name?: string
308
- }[]
309
- lat: number
310
- long: number
311
- /**
312
- Accuracy Radius (km)
313
- */
314
- accuracy: number
315
- /**
316
- City name
317
-
318
- e.g. `Amsterdam`
319
- */
320
- city?: string
321
- postalCode?: string
322
- }
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
1
- export * from './context.js'
2
- export * from './auth.js'
3
- export * from './client.js'
4
- export * from './functions.js'
5
- export * from './query.js'
6
- export * from './stream.js'
7
- export * from './channel.js'
8
- export * from './geo.js'
package/src/query.ts DELETED
@@ -1,14 +0,0 @@
1
- import { ObservableUpdateFunction, ObserveErrorListener } from './functions.js'
2
-
3
- export abstract class BasedQuery<K = any> {
4
- abstract subscribe(
5
- onData: ObservableUpdateFunction<K>,
6
- onError?: ObserveErrorListener
7
- ): () => void
8
-
9
- abstract getWhen(
10
- condition: (data: K, checksum: number) => boolean
11
- ): Promise<any>
12
-
13
- abstract get(): Promise<K>
14
- }