-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathFire.R
More file actions
1492 lines (1471 loc) · 50 KB
/
Fire.R
File metadata and controls
1492 lines (1471 loc) · 50 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' @include aaa.R
#' @include HandlerStack.R
#' @include loggers.R
NULL
#' Generate a New App Object
#'
#' @description
#' The Fire generator creates a new `Fire`-object, which is the class containing
#' all the app logic. The class is based on the [R6][R6::R6Class] OO-system and
#' is thus reference-based with methods and data attached to each object, in
#' contrast to the more well known S3 and S4 systems. A `fiery` server is event
#' driven, which means that it is build up and manipulated by adding event
#' handlers and triggering events. To learn more about the `fiery` event model,
#' read the [event vignette](https://fiery.data-imaginist.com/articles/events.html).
#' `fiery` servers can be modified directly or by attaching plugins. As with
#' events, [plugins has its own vignette](https://fiery.data-imaginist.com/articles/plugins.html).
#'
#' ## Initialization
#' A new 'Fire'-object is initialized using the `new()` method on the generator:
#'
#' \tabular{l}{
#' `app <- Fire$new(host = '127.0.0.1', port = 8080L)`
#' }
#'
#' ## Copying
#'
#' As `Fire` objects are using reference semantics new copies of an app cannot
#' be made simply be assigning it to a new variable. If a true copy of a `Fire`
#' object is desired, use the `clone()` method.
#'
#' @importFrom R6 R6Class
#' @importFrom httpuv startServer service startDaemonizedServer stopDaemonizedServer stopServer
#' @importFrom utils browseURL
#' @importFrom later later
#' @importFrom stats setNames
#' @importFrom reqres Request
#' @importFrom stringi stri_pad_left
#' @importFrom sodium hex2bin
#' @importFrom promises is.promising
#'
#' @export
#'
#' @examples
#' # Create a New App
#' app <- Fire$new(port = 4689)
#'
#' # Setup the data every time it starts
#' app$on('start', function(server, ...) {
#' server$set_data('visits', 0)
#' server$set_data('cycles', 0)
#' })
#'
#' # Count the number of cycles
#' app$on('cycle-start', function(server, ...) {
#' server$set_data('cycles', server$get_data('cycles') + 1)
#' })
#'
#' # Count the number of requests
#' app$on('before-request', function(server, ...) {
#' server$set_data('visits', server$get_data('visits') + 1)
#' })
#'
#' # Handle requests
#' app$on('request', function(server, ...) {
#' list(
#' status = 200L,
#' headers = list('Content-Type' = 'text/html'),
#' body = paste('This is indeed a test. You are number', server$get_data('visits'))
#' )
#' })
#'
#' # Show number of requests in the console
#' app$on('after-request', function(server, ...) {
#' message(server$get_data('visits'))
#' flush.console()
#' })
#'
#' # Terminate the server after 300 cycles
#' app$on('cycle-end', function(server, ...) {
#' if (server$get_data('cycles') > 300) {
#' message('Ending...')
#' flush.console()
#' server$extinguish()
#' }
#' })
#'
#' # Be polite
#' app$on('end', function(server) {
#' message('Goodbye')
#' flush.console()
#' })
#'
#' \dontrun{
#' app$ignite(showcase = TRUE)
#' }
#'
Fire <- R6Class(
'Fire',
public = list(
# Methods
#' @description Create a new `Fire` app
#' @param host A string overriding the default host
#' @param port An port number overriding the default port
#' @return A `Fire` object
initialize = function(host = '127.0.0.1', port = 8080) {
self$host <- host
self$port <- port
private$data <- new.env(parent = emptyenv())
private$handlers <- list()
private$websockets <- list()
private$client_id <- session_id_cookie()
private$DELAY <- DelayStack(self)
private$TIME <- TimeStack(self)
private$ASYNC <- AsyncStack$new(self)
private$LOG_QUEUE <- DelayStack()
private$SESSION_NAME <- gsub(" ", "_", cli::hash_animal(runif(1))$hash)
private$SESSION_FRAMEWORK_VERSION <- utils::packageVersion("fiery")
self$set_logger(logger_void)
private$ACCESS_LOG_FORMAT <- common_log_formatter
},
#' @description Human readable description of the app
#' @param ... ignored
#' @return A character vector
format = function(...) {
text <- c(
'\U0001f525 A fiery webserver',
'\U0001f525 \U0001f4a5 \U0001f4a5 \U0001f4a5'
)
mat <- matrix(
c('Running on', ': ', paste0(self$host, ':', self$port, self$root)),
ncol = 3
)
plugins <- names(private$pluginList)
if (is.null(plugins)) {
plugins <- 'none'
}
mat <- rbind(mat, c('Plugins attached', ': ', plugins[1]))
mat <- rbind(
mat,
matrix(c(rep(' ', (length(plugins) - 1) * 2), plugins[-1]), ncol = 3)
)
handlers <- lapply(private$handlers, function(x) x$length())
if (length(handlers) == 0) {
mat <- rbind(mat, c('Event handlers added', ': ', 'none'))
} else {
mat <- rbind(mat, c('Event handlers added', '', ''))
order <- match(names(handlers), private$privateTriggers)
order[is.na(order)] <- seq_len(sum(is.na(order))) +
max(order, na.rm = TRUE)
handlers <- handlers[order(order)]
mat <- rbind(
mat,
matrix(
c(
names(handlers),
rep(': ', length(handlers)),
as.character(unlist(handlers))
),
ncol = 3
)
)
}
mat[, 1] <- stri_pad_left(mat[, 1], max(nchar(mat[, 1])))
c(text, paste0('\U0001f525 ', apply(mat, 1, paste, collapse = '')))
},
#' @description Begin running the server. Will trigger the `start` event
#' @param block Should the console be blocked while running (alternative is to run in the background)
#' @param showcase Should the default browser open up at the server address.
#' If `TRUE` then a browser opens at the root of the app. If a string the
#' string is used as a path to add to the root before opening
#' @param ... Arguments passed on to the `start` handler
#' @param silent Should startup messaging by silenced
ignite = function(block = TRUE, showcase = FALSE, ..., silent = FALSE) {
private$run(block = block, showcase = showcase, ..., silent = silent)
invisible(NULL)
},
#' @description Synonymous method to `ignite()`
#' @param ... passed on to `ignite()`
start = function(...) {
self$ignite(...)
},
#' @description Resume a session. This is equivalent to `ignite()` but will also trigger the `resume` event
#' @param ... passed on to `ignite()`
reignite = function(...) {
private$run(..., resume = TRUE)
invisible(NULL)
},
#' @description Synonymous method to `reignite()`
#' @param ... passed on to `ignite()`
resume = function(...) {
self$reignite(...)
},
#' @description Stop the server. Will trigger the `end` event
extinguish = function() {
if (private$running) {
if (!is.null(private$server)) {
private$running <- FALSE
private$p_trigger('end', server = self)
stopServer(private$server)
private$server <- NULL
private$p_log('stop', paste0(self$host, ':', self$port, self$root))
} else {
private$quitting <- TRUE
}
}
invisible(NULL)
},
#' @description Synonymous method to `extinguish()`
stop = function() {
self$extinguish()
},
#' @description Add a handler to an event. See the [*The event cycle in fiery* vignette](https://fiery.data-imaginist.com/articles/events.html) for more information.
#' @param event The name of the event that should trigger the handler
#' @param handler The handler function that should be triggered
#' @param pos The position in the handler stack to place it at. `NULL` will place it at the end.
#' @param id An optional id to use to identify the handler
#' @return A unique string identifying the handler (either `id` or generated for you)
on = function(event, handler, pos = NULL, id = NULL) {
check_string(event)
check_function(handler)
# We do this to ensure that any set of arguments is valid to the handler
if (!"..." %in% fn_fmls_names(handler)) {
fn_fmls(handler) <- c(fn_fmls(handler), "..." = missing_arg())
}
check_string(id, allow_null = TRUE)
if (!is.null(id) && id %in% names(private$handlerMap)) {
cli::cli_abort(
"{.arg id} must be unique. A handler with this id has already been added"
)
}
handlerId <- id %||% reqres::random_key()
private$handlerMap[[handlerId]] <- event
private$add_handler(event, handler, pos, handlerId)
invisible(handlerId)
},
#' @description Remove an event handler from the app.
#' @param handlerId The unique id identifying the handler
off = function(handlerId) {
check_string(handlerId)
private$remove_handler(handlerId)
private$handlerMap[[handlerId]] <- NULL
invisible(NULL)
},
#' @description Trigger an event in the app. This will cause any handler attached to the event to be called. See the [*The event cycle in fiery* vignette](https://fiery.data-imaginist.com/articles/events.html) for more information.
#' @param event The name of the event
#' @param ... Arguments passed on to the handlers
#' @return A named list containing the return values of all handlers attached to the event
trigger = function(event, ...) {
check_string(event)
if (event %in% private$privateTriggers) {
cli::cli_abort(
'{.val {event}} and other protected events cannot be triggered manually'
)
} else {
private$p_trigger(event, server = self, ...)
}
},
#' @description Send a Websocket message to a client. Will trigger the `send` event.
#' @param message The message to send
#' @param id The id of the client to send to. If missing, the message will be send to all clients
send = function(message, id) {
private$send_ws(message, id)
private$p_trigger('send', server = self, id = id, message = message)
invisible(NULL)
},
#' @description Close a Websocket connection. Will trigger the `websocket-closed` event
#' @param id The id of the client to close the websocket connection to
close_ws_con = function(id) {
check_string(id)
ws <- private$websockets[[id]]
if (!is.null(ws)) {
private$close_ws(id)
}
},
#' @description Serve a file or directory of files at a specified url path. Requests matching a file on the system never enters into the request loop but are served directly (and fast). Due to this, logging for these requests are also turned off
#' @param at The url path to listen to requests on
#' @param path The path to the file or directory on the file system
#' @param use_index Should an `index.html` file be served if present when a client requests the folder
#' @param fallthrough Should requests that doesn't match a file enter the request loop or have a 404 response send directly
#' @param html_charset The charset to report for serving html files
#' @param headers A list of headers to add to the response. Will be combined with the global headers of the app
#' @param validation An optional validation pattern. Presently, the only type of validation supported is an exact string match of a header. For example, if validation is `"abc" = "xyz"`, then HTTP requests must have a header named `abc` (case-insensitive) with the value `"xyz"` (case-sensitive). If a request does not have a matching header, than httpuv will give a 403 Forbidden response. If `character(0)` (the default), then no validation check will be performed.
serve_static = function(
at,
path,
use_index = TRUE,
fallthrough = FALSE,
html_charset = "utf-8",
headers = list(),
validation = NULL
) {
check_string(at)
check_string(path)
if (!file.exists(path) && !dir.exists(path)) {
cli::cli_abort(
"{.arg {path}} does not point to an existing file or directory"
)
}
check_bool(use_index)
check_bool(fallthrough)
check_string(html_charset)
if (!(rlang::is_bare_list(headers) && rlang::is_named2(headers))) {
stop_input_type(headers, "a named list")
}
for (i in names(headers)) {
check_string(headers[[i]], arg = paste0("headers$", i))
}
check_string(validation, allow_null = TRUE)
if (at %in% names(private$staticList)) {
cli::cli_inform("Overwriting static url path {.field {at}}")
}
private$staticList[[at]] <- httpuv::staticPath(
path = path,
indexhtml = use_index,
fallthrough = fallthrough,
html_charset = html_charset,
headers = headers,
validation = validation %||% character(0L)
)
invisible(NULL)
},
#' @description Exclude a url path from serving static content. Only meaningful to exclude sub paths of path that are set to serve static content
#' @param at The url path to exclude from static serving. Request to this path will enter the normal request loop
exclude_static = function(at) {
check_string(at)
private$staticList[[at]] <- httpuv::excludeStaticPath()
invisible(NULL)
},
#' @description Attach a plugin to the app. See the [*Creating and using fiery plugins* vignette](https://fiery.data-imaginist.com/articles/plugins.html) for more information
#' @param plugin The plugin to attach
#' @param name Optional name for the plugin. If omitted `plugin$name` will be used instead
#' @param ... Arguments to pass into the plugins `on_attach()` method
#' @param force If the plugin has already been attached an error is thrown, unless `force = TRUE` which tells the app to reattach it
attach = function(plugin, ..., name = NULL, force = FALSE) {
name <- name %||% plugin$name
check_string(name)
if (!force && self$has_plugin(name)) {
cli::cli_abort(c(
'The {.arg {name}} plugin is already loaded.',
i = 'Use {.code force = TRUE} to reapply it.'
))
}
requires <- plugin$require
if (!is.null(requires)) {
check_character(requires)
exists <- vapply(requires, self$has_plugin, logical(1))
if (!all(exists)) {
cli::cli_abort(
'The {.arg {name}} plugin requires the following {cli::qty(requires[!exists])} plugin{?s}: {requires[!exists]}'
)
}
}
try_fetch(
plugin$on_attach(self, ...),
error = function(cnd) {
cli::cli_abort(
'The {.arg {name}} plugin failed to attach to the app',
parent = cnd
)
}
)
private$add_plugin(plugin, name)
invisible(NULL)
},
#' @description Check if the app has a plugin attached
#' @param name The name of the plugin
#' @return A boolean indicating if the given plugin is already attached
has_plugin = function(name) {
name %in% names(private$pluginList)
},
#' @description Add a global http header that will be applied to all responses
#' @param name The name of the header
#' @param value The value of the header. Use `NULL` to remove the global header
header = function(name, value) {
check_string(name)
if (missing(value)) {
return(private$headers[[tolower(name)]])
}
check_string(value, allow_null = TRUE)
private$headers[[tolower(name)]] <- value
invisible(NULL)
},
#' @description Add data to the global data store
#' @param name The name identifying the data
#' @param value The data to add
set_data = function(name, value) {
check_string(name)
assign(name, value, envir = private$data)
invisible(NULL)
},
#' @description Retrieve data from the global data store
#' @param name The name identifying the data
#' @return The data requested. Returns `NULL` if the store does not contain the requested data
get_data = function(name) {
check_string(name)
private$data[[name]]
},
#' @description Remove data from the global data store
#' @param name The name identifying the data to be removed
remove_data = function(name) {
check_string(name)
rm(list = name, envir = private$data)
invisible(NULL)
},
#' @description Add a timed evaluation that will be evaluated after the given number of seconds.
#' @param expr The expression to evaluate when the time has passed
#' @param then A handler to call once `expr` has been evaluated
#' @param after The time in second to wait before evaluating `expr`
#' @param loop Should `expr` be called repeatedly with the interval given by `after`
#' @return A unique id identifying the handler
time = function(expr, then, after, loop = FALSE) {
private$TIME$add({{ expr }}, then, after, loop)
},
#' @description Remove a timed evaluation
#' @param id The unique id identifying the handler
remove_time = function(id) {
private$TIME$remove(id)
},
#' @description Add a delayed evaluation to be evaluated immediately at the end of the loop cycle.
#' @param expr The expression to evaluate at the end of the cycle
#' @param then A handler to call once `expr` has been evaluated
#' @return A unique id identifying the handler
delay = function(expr, then) {
private$DELAY$add({{ expr }}, then)
},
#' @description Remove a delayed evaluation
#' @param id The unique id identifying the handler
remove_delay = function(id) {
private$DELAY$remove(id)
},
#' @description `r lifecycle::badge('deprecated')` Add an asynchronous evaluation to be evaluated in another process without blocking the server. This function has been deprecated in favor of using your own async framework of choice, e.g. [mirai](https://mirai.r-lib.org) or [promises](https://rstudio.github.io/promises/)
#' @param expr The expression to evaluate at the end of the cycle
#' @param then A handler to call once `expr` has been evaluated
#' @return A unique id identifying the handler
async = function(expr, then) {
lifecycle::deprecate_soft(
"2.0.0",
what = "Fire$async()",
details = "Use an async evaluation framework of your own choice, such as promises or mirai"
)
private$ASYNC$add(substitute(expr), then)
},
#' @description Remove an async evaluation
#' @param id The unique id identifying the handler
remove_async = function(id) {
private$ASYNC$remove(id)
},
#' @description Sets the function that converts an HTTP request into a specific client id
#' @param converter A function with the argument `request`
set_client_id_converter = function(converter) {
check_args(converter, 'request')
private$client_id <- converter
invisible(NULL)
},
#' @description Sets the logging function to use
#' @param logger A function with the arguments `event`, `message`, `request`, and `...`
set_logger = function(logger) {
check_function(logger)
if (identical(logger, logger_void)) {
private$logger <- NULL
} else {
check_args(logger, c('event', 'message', 'request', '...'))
private$logger <- logger
}
invisible(NULL)
},
#' @description Log a message with the logger attached to the app. See [loggers] for build in functionality
#' @param event The event associated with the message
#' @param message The message to log
#' @param request The `Request` object associated with the message, if any.
#' @param ... Additional arguments passed on to the logger.
#' @param .logcall The call that send the log request
#' @param .topcall The call in which `.logcall` is called from
#' @param .topenv The environment associated with `.topcall`
log = function(
event,
message,
request = NULL,
...,
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()
) {
if (is.null(private$logger)) {
return(invisible(NULL))
}
time <- Sys.time()
force(message)
force(.logcall)
force(.topcall)
force(.topenv)
log_fun <- function(...) {
private$logger(
event,
message,
request,
time,
.logcall = .logcall,
.topcall = .topcall,
.topenv = .topenv,
.session_name = private$SESSION_NAME,
...
)
}
if (private$running) {
private$LOG_QUEUE$add(
NULL,
log_fun
)
} else {
log_fun()
}
invisible(NULL)
},
#' @description Test if an app is running
is_running = function() {
private$running
},
#' @description Evaluate an expression safely, logging any errors, warnings,
#' or messages that bubbles up
#' @param expr An expression to evaluate
#' @param request The request under evaluation, if any. Used in logging
#'
#' @return The value of the expression. If an error is caught, the condition
#' object is returned instead
#'
safe_call = function(expr, request = NULL) {
private$p_safe_call(expr, request = request)
},
#' @description Send a request directly to the request logic of a non-running app. Only intended for testing the request logic
#' @param request The request to send
test_request = function(request) {
res <- private$header_logic(request)
res %||% private$request_logic(request)
},
#' @description Send a request directly to the header logic of a non-running app. Only intended for testing the request logic
#' @param request The request to send
test_header = function(request) {
on.exit(
if (!is.null(request$._REQRES_OBJ)) put_request(request$._REQRES_OBJ)
)
private$header_logic(request)
},
#' @description Send a message directly **to** the message logic of a non-running app. Only intended for testing the websocket logic
#' @param request The request to use to establish the connection
#' @param binary Is the message send in binary or character format
#' @param message The message to send. If `binary = FALSE` a character vector, if `binary = TRUE` a raw vector
#' @param withClose Should the websocket connection be closed at the end by the client
test_message = function(request, binary, message, withClose = TRUE) {
req <- private$new_req(request)
id <- private$client_id(req)
message_fun <- private$message_logic(id, req)
message_fun(binary, message)
if (withClose) {
close_fun <- private$close_ws_logic(id, req)
close_fun()
}
},
#' @description Send a message directly **from** a non-running app. Only intended for testing the websocket logic
#' @param request The request to use to establish the connection
#' @param message The message to send from the app
#' @param close Should the websocket connection be closed at the end by the server
test_websocket = function(request, message, close = TRUE) {
ws <- list(
request = request,
onMessage = function(func) {},
onClose = function(func) {},
send = function(message) {
message(message)
},
close = function() {
message('closing')
}
)
private$websocket_logic(ws)
self$send(message, private$client_id(private$new_req(request)))
if (close) private$close_ws(private$client_id(private$new_req(request)))
}
),
active = list(
#' @field host A string giving a valid IPv4 address owned by the server, or `'0.0.0.0'` to listen on all addresses. The default is `'127.0.0.1'`
host = function(address) {
if (missing(address)) {
return(private$HOST)
}
check_string(address)
private$HOST <- address
},
#' @field port An integer giving the port number the server should listen on (defaults to `8080L`)
port = function(n) {
if (missing(n)) {
return(private$PORT)
}
check_number_whole(n, min = 1)
private$PORT <- n
},
#' @field refresh_rate `r lifecycle::badge('deprecated')` The interval in seconds between run cycles when running a blocking server (defaults to `0.001`)
refresh_rate = function(rate) {
lifecycle::deprecate_soft(
"1.5.0",
I("Refresh rate for blocking servers"),
details = "The loop now runs without any pause"
)
if (missing(rate)) {
return(private$REFRESHRATE)
}
check_number_decimal(rate)
private$REFRESHRATE <- rate
},
#' @field refresh_rate_nb The interval in seconds between run cycles when running a non-blocking server (defaults to `1`)
refresh_rate_nb = function(rate) {
if (missing(rate)) {
return(private$REFRESHRATENB)
}
check_number_decimal(rate)
private$REFRESHRATENB <- rate
},
#' @field trigger_dir A valid folder where trigger files can be put when running a blocking server (defaults to `NULL`). See the [*The event cycle in fiery* vignette](https://fiery.data-imaginist.com/articles/events.html) for more information.
trigger_dir = function(dir) {
if (missing(dir)) {
return(private$TRIGGERDIR)
}
check_dir(dir, allow_null = TRUE)
private$TRIGGERDIR <- dir
},
#' @field plugins A named list of the already attached plugins. **Read Only** - can only be modified using the `attach()` method.
plugins = function(plugin) {
if (!missing(plugin)) {
cli::cli_abort('Use the {.fn attach} method to add plugins')
}
private$pluginList
},
#' @field data_store Access the environment that holds the global data store
data_store = function(value) {
if (missing(value)) {
return(private$data)
}
if (!identical(private$data, value)) {
cli::cli_abort("It is not allowed to replace the data store")
}
private$data <- value
},
#' @field root The location of the app. Setting this will remove the root value from requests (or decline them with `400` if the request does not match the root). E.g. the path of a request will be changed from `/demo/test` to `/test` if `root == '/demo'`
root = function(path) {
if (missing(path)) {
return(private$ROOT)
}
check_string(path)
path <- sub('/$', '', path)
if (path != '') {
path <- paste0('/', sub('^/+', '', path))
}
private$ROOT <- path
},
#' @field access_log_format A [glue][glue::glue] string defining how requests will be logged. For standard formats see [common_log_format] and [combined_log_format]. Defaults to the *Common Log Format*
access_log_format = function(format) {
if (missing(format)) {
return(private$ACCESS_LOG_FORMAT)
}
check_string(format)
if (format == common_log_format) {
format <- common_log_formatter
} else if (format == combined_log_format) {
format <- combined_log_formatter
}
private$ACCESS_LOG_FORMAT <- format
},
#' @field key The encryption key to use for request/response encryption
#'
key = function(value) {
if (missing(value)) {
cli::cli_abort("{.arg key} can only be set, not retrieved")
}
if (!is.null(value)) {
if (is_string(value)) {
value <- hex2bin(value)
if (length(value) == 0) {
cli::cli_abort(
"Malformed key. If given as a string it must be hexadecimal encoded"
)
}
}
if (!is.raw(value)) {
cli::cli_abort(
"Malformed key. It must be provided as either a string, a raw vector or NULL"
)
}
if (length(value) != 32) {
cli::cli_abort("Malformed key. The key must be 32 bit")
}
}
private$KEY <- value
},
#' @field session_cookie_settings Get or set the session cookie settings
#'
session_cookie_settings = function(value) {
if (missing(value)) {
return(private$SESSION_COOKIE)
}
if (!reqres::is_session_cookie_settings(value)) {
cli::cli_abort(c(
"{.arg session_cookie_settings} can only be set to a valid settings object",
"i" = "Construct one using {.fun reqres::session_cookie}"
))
}
private$SESSION_COOKIE <- value
},
#' @field trust A logical indicating whether incoming requests are trusted.
#'
trust = function(value) {
if (missing(value)) {
return(private$TRUST)
}
check_bool(value)
private$TRUST <- value
},
#' @field compression_limit The size threshold in bytes for trying to
#' compress the response body (it is still dependant on content negotiation)
compression_limit = function(value) {
if (missing(value)) {
return(private$COMPRESSION_LIMIT)
}
check_number_decimal(value, min = 0, allow_infinite = TRUE)
private$COMPRESSION_LIMIT <- value
},
#' @field query_delim The delimeter used to split array-type query arguments
#' when parsing the query string
query_delim = function(value) {
if (missing(value)) {
return(private$QUERY_DELIM)
}
check_string(value, allow_null = TRUE)
private$QUERY_DELIM <- value
}
),
private = list(
# Data
HOST = '127.0.0.1',
PORT = 8080,
REFRESHRATE = 0.001,
REFRESHRATENB = 1,
TRIGGERDIR = NULL,
ROOT = '',
ACCESS_LOG_FORMAT = NULL,
KEY = NULL,
SESSION_COOKIE = NULL,
TRUST = FALSE,
COMPRESSION_LIMIT = 0,
QUERY_DELIM = NULL,
SESSION_NAME = "",
SESSION_FRAMEWORK = "fiery",
SESSION_FRAMEWORK_VERSION = "",
running = FALSE,
quitting = FALSE,
privateTriggers = c(
'start',
'resume',
'cycle-start',
'header',
'before-request',
'request',
'after-request',
'before-message',
'message',
'after-message',
'websocket-opened',
'websocket-closed',
'send',
'cycle-end',
'end'
),
data = NULL,
headers = list(),
handlers = NULL,
handlerMap = list(),
pluginList = list(),
staticList = list(),
websockets = NULL,
server = NULL,
client_id = NULL,
logger = NULL,
DELAY = NULL,
TIME = NULL,
ASYNC = NULL,
LOG_QUEUE = NULL,
# Methods
run = function(
block = TRUE,
resume = FALSE,
showcase = FALSE,
...,
silent = FALSE
) {
check_bool(block)
check_bool(resume)
if (!is_bool(showcase)) {
check_string(showcase, what = "`TRUE` or `FALSE` or a single string")
}
check_bool(silent)
if (!private$running) {
private$running <- TRUE
private$TIME$reset()
private$p_trigger('start', server = self, ...)
if (resume) {
private$p_trigger('resume', server = self, ...)
if (!silent) {
cli::cli_inform(
'Fire restarted at {.url {self$host}:{self$port}{self$root}} ({private$SESSION_NAME})'
)
}
private$p_log('resume', paste0(self$host, ':', self$port, self$root))
} else {
if (!silent) {
cli::cli_inform(
'Fire started at {.url {self$host}:{self$port}{self$root}} ({private$SESSION_NAME})'
)
}
private$p_log('start', paste0(self$host, ':', self$port, self$root))
}
if (block) {
on.exit({
private$running <- FALSE
private$p_trigger('end', server = self)
private$p_log('stop', paste0(self$host, ':', self$port, self$root))
})
private$run_blocking_server(showcase = showcase)
} else {
private$run_allowing_server(showcase = showcase)
}
} else {
private$p_log(
'warning',
'Server is already running and cannot be started'
)
}
},
run_blocking_server = function(showcase = FALSE) {
server <- startServer(
self$host,
self$port,
list(
call = private$request_logic,
onHeaders = private$header_logic,
onWSOpen = private$websocket_logic,
staticPaths = lapply(private$staticList, function(p) {
attr(p, "headers") <- c(attr(p, "headers"), private$headers)
p
})
)
)
on.exit(stopServer(server))
if (!isFALSE(showcase)) {
private$open_browser(if (is.character(showcase)) showcase else "")
}
repeat {
private$p_trigger('cycle-start', server = self)
service()
private$external_triggers()
private$DELAY$eval(server = self)
private$TIME$eval(server = self)
private$ASYNC$eval(server = self)
private$LOG_QUEUE$eval(server = self)
private$p_trigger('cycle-end', server = self)
if (private$quitting) {
private$quitting <- FALSE
break
}
}
},
run_allowing_server = function(showcase = FALSE) {
private$server <- startServer(
self$host,
self$port,
list(
call = private$request_logic,
onHeaders = private$header_logic,
onWSOpen = private$websocket_logic,
staticPaths = lapply(private$staticList, function(p) {
attr(p, "headers") <- c(attr(p, "headers"), private$headers)
p
})
)
)
if (!isFALSE(showcase)) {
private$open_browser(if (is.character(showcase)) showcase else "")
}
private$allowing_cycle()
},
allowing_cycle = function() {
if (private$running) {
private$p_trigger('cycle-start', server = self)
private$external_triggers()
private$DELAY$eval(server = self)
private$TIME$eval(server = self)
private$ASYNC$eval(server = self)
private$LOG_QUEUE$eval(server = self)
private$p_trigger('cycle-end', server = self)
later(
function() {
private$allowing_cycle()
},
private$REFRESHRATENB
)
}
},
mount_request = function(req) {
if (req$SCRIPT_NAME == self$root) {
req
} else if (grepl(paste0('^', self$root, '(/|$)'), req$PATH_INFO)) {
req$SCRIPT_NAME <- self$root
req$PATH_INFO <- sub(paste0('^', self$root, ''), '', req$PATH_INFO)
req
} else {
cli::cli_abort(
'URL ({req$PATH_INFO}) not matching mount point ({self$root})'
)
}
},
request_logic = function(req) {
req <- req$._REQRES_OBJ
start_time <- req$start_time %||% Sys.time()
id <- private$client_id(req)
args <- unlist(
unname(private$p_trigger(
'before-request',
server = self,
id = id,
request = req,
.request = req
)),
recursive = FALSE
)
res <- private$p_trigger(
'request',
server = self,
id = id,
request = req,
arg_list = args,
.request = req
)
any_promising <- any(vapply(res, promises::is.promising, logical(1)))
if (any_promising) {
promises::then(promises::promise_map(res, identity), function(res) {
private$finish_request(res, req, start_time, id)
})
} else {
private$finish_request(res, req, start_time, id)
}
},
finish_request = function(res, request, start_time, id) {
on.exit({
request$locked <- FALSE
put_request(request)
})
response <- request$respond()
problems <- vapply(res, reqres::is_reqres_problem, logical(1))
if (any(problems)) {
reqres::handle_problem(response, res[[which(problems)[1]]])
} else if (any(vapply(res, is_condition, logical(1)))) {
response$status_with_text(500L)
}
response <- private$p_safe_call(response$as_list(), request)
# On the off-chance that reqres throws an error during conversion of response
if (is_condition(response)) {
request$response$status_with_text(500L) # Update the real response first so it gets logged correctly
response <- list(
status = 500L,
headers = list("Content-Type" = "text/plain"),
body = "Internal Server Error"
)
}
private$p_trigger(
'after-request',
server = self,
id = id,
request = request,
response = request$response,
.request = request
)
private$log_request(start_time, request, id)
response
},
log_request = function(start_time, req, id) {
end_time <- Sys.time()
log_format <- private$ACCESS_LOG_FORMAT
if (is.character(log_format)) {
private$p_log(