@@ -21,6 +21,7 @@ const APIError = require("../../api/APIError");
21
21
const { DriverError } = require ( "./DriverError" ) ;
22
22
const { TypedValue } = require ( "./meta/Runtime" ) ;
23
23
const BaseService = require ( "../BaseService" ) ;
24
+ const { Driver } = require ( "../../definitions/Driver" ) ;
24
25
25
26
/**
26
27
* DriverService provides the functionality of Puter drivers.
@@ -31,10 +32,30 @@ class DriverService extends BaseService {
31
32
}
32
33
33
34
_construct ( ) {
34
- this . interfaces = require ( './interfaces' ) ;
35
+ this . drivers = { } ;
35
36
this . interface_to_implementation = { } ;
36
37
}
37
38
39
+ async [ '__on_registry.collections' ] ( _ , { svc_registry } ) {
40
+ svc_registry . register_collection ( 'interfaces' ) ;
41
+ svc_registry . register_collection ( 'drivers' ) ;
42
+ }
43
+ async [ '__on_registry.entries' ] ( _ , { svc_registry } ) {
44
+ const services = this . services ;
45
+ const col_interfaces = svc_registry . get ( 'interfaces' ) ;
46
+ const col_drivers = svc_registry . get ( 'drivers' ) ;
47
+ {
48
+ const default_interfaces = require ( './interfaces' ) ;
49
+ for ( const k in default_interfaces ) {
50
+ col_interfaces . set ( k , default_interfaces [ k ] ) ;
51
+ }
52
+ }
53
+ await services . emit ( 'driver.register.interfaces' ,
54
+ { col_interfaces } ) ;
55
+ await services . emit ( 'driver.register.drivers' ,
56
+ { col_drivers } ) ;
57
+ }
58
+
38
59
_init ( ) {
39
60
const svc_registry = this . services . get ( 'registry' ) ;
40
61
svc_registry . register_collection ( '' ) ;
@@ -43,9 +64,27 @@ class DriverService extends BaseService {
43
64
register_driver ( interface_name , implementation ) {
44
65
this . interface_to_implementation [ interface_name ] = implementation ;
45
66
}
46
-
67
+
47
68
get_interface ( interface_name ) {
48
- return this . interfaces [ interface_name ] ;
69
+ const o = { } ;
70
+ const col_interfaces = svc_registry . get ( 'interfaces' ) ;
71
+ const keys = col_interfaces . keys ( ) ;
72
+ for ( const k of keys ) o [ k ] = col_interfaces . get ( k ) ;
73
+ return col_interfaces . get ( interface_name ) ;
74
+ }
75
+
76
+ get_default_implementation ( interface_name ) {
77
+ // If there's a hardcoded implementation, use that
78
+ // (^ temporary, until all are migrated)
79
+ if ( this . interface_to_implementation . hasOwnProperty ( interface_name ) ) {
80
+ return this . interface_to_implementation [ interface_name ] ;
81
+ }
82
+
83
+ this . log . noticeme ( 'HERE IT IS' ) ;
84
+ const options = this . services . get_implementors ( interface_name ) ;
85
+ this . log . info ( 'test' , { options } ) ;
86
+ if ( options . length < 1 ) return ;
87
+ return options [ 0 ] ;
49
88
}
50
89
51
90
async call ( ...a ) {
@@ -76,16 +115,33 @@ class DriverService extends BaseService {
76
115
throw APIError . create ( 'permission_denied' ) ;
77
116
}
78
117
79
- const instance = this . interface_to_implementation [ interface_name ] ;
118
+ const svc_registry = this . services . get ( 'registry' ) ;
119
+ const c_interfaces = svc_registry . get ( 'interfaces' ) ;
120
+
121
+ const instance = this . get_default_implementation ( interface_name ) ;
80
122
if ( ! instance ) {
81
123
throw APIError . create ( 'no_implementation_available' , null , { interface_name } )
82
124
}
83
- const meta = await instance . get_response_meta ( ) ;
84
- const sla_override = await this . maybe_get_sla ( interface_name , method ) ;
125
+ const meta = await ( async ( ) => {
126
+ if ( instance instanceof Driver ) {
127
+ return await instance . get_response_meta ( ) ;
128
+ }
129
+ if ( ! instance . instance . as ( 'driver-metadata' ) ) return ;
130
+ const t = instance . instance . as ( 'driver-metadata' ) ;
131
+ return t . get_response_meta ( ) ;
132
+ } ) ( ) ;
85
133
try {
86
- let result = await instance . call ( method , processed_args , sla_override ) ;
134
+ let result ;
135
+ if ( instance instanceof Driver ) {
136
+ result = await instance . call (
137
+ method , processed_args ) ;
138
+ } else {
139
+ // TODO: SLA and monthly limits do not apply do drivers
140
+ // from service traits (yet)
141
+ result = await instance . impl [ method ] ( processed_args ) ;
142
+ }
87
143
if ( result instanceof TypedValue ) {
88
- const interface_ = this . interfaces [ interface_name ] ;
144
+ const interface_ = c_interfaces . get ( interface_name ) ;
89
145
let desired_type = interface_ . methods [ method ]
90
146
. result_choices [ 0 ] . type ;
91
147
const svc_coercion = services . get ( 'coercion' ) ;
@@ -127,16 +183,12 @@ class DriverService extends BaseService {
127
183
return this . interfaces ;
128
184
}
129
185
130
- async maybe_get_sla ( interface_name , method ) {
131
- const services = this . services ;
132
- const fs = services . get ( 'filesystem' ) ;
133
-
134
- return false ;
135
- }
136
-
137
186
async _process_args ( interface_name , method_name , args ) {
187
+ const svc_registry = this . services . get ( 'registry' ) ;
188
+ const c_interfaces = svc_registry . get ( 'interfaces' ) ;
189
+
138
190
// Note: 'interface' is a strict mode reserved word.
139
- const interface_ = this . interfaces [ interface_name ] ;
191
+ const interface_ = c_interfaces . get ( interface_name ) ;
140
192
if ( ! interface_ ) {
141
193
throw APIError . create ( 'interface_not_found' , null , { interface_name } ) ;
142
194
}
0 commit comments