jsgui3-server 0.0.73 → 0.0.77

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.
Files changed (155) hide show
  1. package/examples/client-side-popup-menu-button.html +93 -93
  2. package/examples/controls/_html-server-color-palette.js +114 -114
  3. package/examples/controls/html-server-combo-box.js +104 -104
  4. package/examples/controls/html-server-list.js +98 -98
  5. package/examples/controls/html-server-popup-menu-button.js +114 -114
  6. package/examples/controls/html-server-start-stop-toggle-button.js +146 -146
  7. package/examples/controls/scs-arrow-button.js +36 -36
  8. package/examples/controls/scs-date-picker.js +157 -157
  9. package/examples/controls/scs-file-browser.js +82 -82
  10. package/examples/controls/scs-item.js +159 -159
  11. package/examples/controls/scs-month-arrow-selector.js +126 -126
  12. package/examples/controls/scs-month-view.js +95 -95
  13. package/examples/controls/scs-start-stop-toggle-button.js +40 -40
  14. package/examples/controls/scs-tree.js +49 -49
  15. package/examples/controls/scs-year-arrow-selector.js +127 -127
  16. package/examples/demos/date-picker.js +119 -119
  17. package/examples/demos/explain-encapsulation.js +9 -9
  18. package/examples/demos/resizing.js +35 -35
  19. package/examples/demos/server_time.js +6 -6
  20. package/examples/demos/square_box.js +324 -324
  21. package/examples/html-rendering.js +20 -20
  22. package/examples/html-server.js +100 -100
  23. package/fs2.js +1836 -1836
  24. package/module.js +21 -21
  25. package/old/single-control-server.js +418 -418
  26. package/package.json +44 -42
  27. package/publishing/function-publisher.js +202 -202
  28. package/publishing/notes.md +5 -5
  29. package/publishing/observable-publisher.js +118 -118
  30. package/publishing/resource-publisher.js +306 -306
  31. package/resources/data-resource.js +104 -104
  32. package/resources/fs-resource.js +148 -148
  33. package/resources/jsbuilder/Abstract_Single_Declaration.js +105 -0
  34. package/resources/jsbuilder/Abstract_Single_Declaration_Sequence.js +43 -0
  35. package/resources/jsbuilder/JS_AST/JS_AST_Abstract_Node.js +62 -0
  36. package/resources/jsbuilder/JS_AST/JS_AST_Abstract_Node_Group.js +42 -0
  37. package/resources/jsbuilder/JS_AST/JS_AST_Group_Shared.js +62 -0
  38. package/resources/jsbuilder/JS_AST/JS_AST_Node.js +94 -0
  39. package/resources/jsbuilder/JS_AST/JS_AST_Node_0-Core.js +228 -0
  40. package/resources/jsbuilder/JS_AST/JS_AST_Node_1-Babel.js +338 -0
  41. package/resources/jsbuilder/JS_AST/JS_AST_Node_10-Changing.js +40 -0
  42. package/resources/jsbuilder/JS_AST/JS_AST_Node_2.1.1-Child.js +97 -0
  43. package/resources/jsbuilder/JS_AST/JS_AST_Node_2.1.2-Parent.js +38 -0
  44. package/resources/jsbuilder/JS_AST/JS_AST_Node_2.1.3-Ancestor.js +62 -0
  45. package/resources/jsbuilder/JS_AST/JS_AST_Node_2.2-Inner.js +44 -0
  46. package/resources/jsbuilder/JS_AST/JS_AST_Node_2.3-All.js +73 -0
  47. package/resources/jsbuilder/JS_AST/JS_AST_Node_2.4-Sibling.js +93 -0
  48. package/resources/jsbuilder/JS_AST/JS_AST_Node_2.5-Available_In_Scope.js +29 -0
  49. package/resources/jsbuilder/JS_AST/JS_AST_Node_2.9-Signature.js +116 -0
  50. package/resources/jsbuilder/JS_AST/JS_AST_Node_3-Basics.js +160 -0
  51. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.0.0-Basics_First.js +179 -0
  52. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.0.1-Basics_Second.js +88 -0
  53. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.0.99-Basics_Last.js +92 -0
  54. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.1-Basics_Each.js +137 -0
  55. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.1.5-Basics_Count.js +74 -0
  56. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.2-Basics_Filter.js +40 -0
  57. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.3-Basics_Collect.js +86 -0
  58. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.4-Basics_Select.js +43 -0
  59. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.5-Basics_Find.js +41 -0
  60. package/resources/jsbuilder/JS_AST/JS_AST_Node_3.6-Basics_Callmap.js +55 -0
  61. package/resources/jsbuilder/JS_AST/JS_AST_Node_4.0-Index_Indexes.js +46 -0
  62. package/resources/jsbuilder/JS_AST/JS_AST_Node_4.1-Index.js +344 -0
  63. package/resources/jsbuilder/JS_AST/JS_AST_Node_5.0-Category.js +39 -0
  64. package/resources/jsbuilder/JS_AST/JS_AST_Node_5.1-Category_Identifier.js +31 -0
  65. package/resources/jsbuilder/JS_AST/JS_AST_Node_5.2-Category_Literal.js +29 -0
  66. package/resources/jsbuilder/JS_AST/JS_AST_Node_5.3-Category_Expression.js +27 -0
  67. package/resources/jsbuilder/JS_AST/JS_AST_Node_5.4-Category_Pattern.js +9 -0
  68. package/resources/jsbuilder/JS_AST/JS_AST_Node_5.5-Category_Declaration.js +44 -0
  69. package/resources/jsbuilder/JS_AST/JS_AST_Node_5.6-Category_Statement.js +22 -0
  70. package/resources/jsbuilder/JS_AST/JS_AST_Node_6.0-Type.js +87 -0
  71. package/resources/jsbuilder/JS_AST/JS_AST_Node_6.1-Type_Class_Declaration.js +9 -0
  72. package/resources/jsbuilder/JS_AST/JS_AST_Node_6.2-Type_Variable_Declaration.js +28 -0
  73. package/resources/jsbuilder/JS_AST/JS_AST_Node_6.3-Type_Variable_Declarator.js +29 -0
  74. package/resources/jsbuilder/JS_AST/JS_AST_Node_7-Query.js +737 -0
  75. package/resources/jsbuilder/JS_AST/JS_AST_Node_8-Features.js +65 -0
  76. package/resources/jsbuilder/JS_AST/JS_AST_Node_9-Planning.js +32 -0
  77. package/resources/jsbuilder/JS_AST/JS_AST_Node_Arrangement.js +15 -0
  78. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Node_Declared_Object.js +306 -0
  79. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Node_Feature.js +78 -0
  80. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Node_Feature_Declaration.js +249 -0
  81. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Node_Feature_Declarator.js +139 -0
  82. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Root_Node_Feature/JS_AST_Root_Node_Feature.js +10 -0
  83. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Root_Node_Feature/JS_AST_Root_Node_Feature_Exported.js +101 -0
  84. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Root_Node_Feature/JS_AST_Root_Node_Feature_Exports.js +61 -0
  85. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Root_Node_Feature/JS_AST_Root_Node_Interpreted.js +180 -0
  86. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Root_Node_Feature/_JSGUI_Root_Node_Interpreted.js +43 -0
  87. package/resources/jsbuilder/JS_AST/JS_AST_Node_Feature/JS_AST_Root_Node_Feature/special_case_objectassign_to_object.js +12 -0
  88. package/resources/jsbuilder/JS_AST/JS_AST_Node_Group.js +35 -0
  89. package/resources/jsbuilder/JS_AST/JS_AST_Operation.js +11 -0
  90. package/resources/jsbuilder/JS_AST/JS_AST_Operation_On_Relationship.js +32 -0
  91. package/resources/jsbuilder/JS_AST/JS_AST_Ordered_Relationship_Node_To_Group.js +38 -0
  92. package/resources/jsbuilder/JS_AST/JS_AST_Ordinal.js +40 -0
  93. package/resources/jsbuilder/JS_AST/JS_AST_Ordinal_Relationship.js +25 -0
  94. package/resources/jsbuilder/JS_AST/JS_AST_Relationship_Node_To_Group.js +201 -0
  95. package/resources/jsbuilder/JS_AST/JS_AST_Relationship_Node_Within_Group_To_Node.js +44 -0
  96. package/resources/jsbuilder/JS_AST/_JS_AST_Node_3.8-Query_Features.js +77 -0
  97. package/resources/jsbuilder/JS_AST/query/all.js +0 -0
  98. package/resources/jsbuilder/JS_AST/query/enable_array_as_queryable.js +228 -0
  99. package/resources/jsbuilder/JS_AST/query/find_object_keys.js +405 -0
  100. package/resources/jsbuilder/JS_AST/query/node_queries.js +9 -0
  101. package/resources/jsbuilder/JS_AST/query/root_queries.js +0 -0
  102. package/resources/jsbuilder/JS_AST/query/root_query_identidy.js +12 -0
  103. package/resources/jsbuilder/JS_AST_Node_Extended/JSGUI_Singular_Declaration.js +86 -0
  104. package/resources/jsbuilder/JS_AST_Node_Extended/JS_AST_Node_Declaration.js +124 -0
  105. package/resources/jsbuilder/JS_AST_Node_Extended/JS_AST_Node_Extended.js +88 -0
  106. package/resources/jsbuilder/JS_AST_Node_Extended/JS_AST_Node_Extended_0-Core.js +11 -0
  107. package/resources/jsbuilder/JS_Builder.js +11 -0
  108. package/resources/jsbuilder/JS_File/Feature/JS_File_Declared_Object.js +32 -0
  109. package/resources/jsbuilder/JS_File/Feature/JS_File_Exported_Object_Info.js +26 -0
  110. package/resources/jsbuilder/JS_File/Feature/JS_File_Exports.js +79 -0
  111. package/resources/jsbuilder/JS_File/Feature/JS_File_Feature.js +18 -0
  112. package/resources/jsbuilder/JS_File/Feature/JS_File_Imported_Object_Info.js +26 -0
  113. package/resources/jsbuilder/JS_File/Feature/JS_File_Imports.js +9 -0
  114. package/resources/jsbuilder/JS_File/JS_File.js +12 -0
  115. package/resources/jsbuilder/JS_File/JS_File_0-Core.js +202 -0
  116. package/resources/jsbuilder/JS_File/JS_File_1-Early_Parse.js +175 -0
  117. package/resources/jsbuilder/JS_File/JS_File_2-Babel.js +81 -0
  118. package/resources/jsbuilder/JS_File/JS_File_3-JS_AST_Node.js +86 -0
  119. package/resources/jsbuilder/JS_File/JS_File_4-Query.js +414 -0
  120. package/resources/jsbuilder/JS_File/JS_File_4.1-Query_Features.js +415 -0
  121. package/resources/jsbuilder/JS_File/JS_File_5-Planning.js +59 -0
  122. package/resources/jsbuilder/JS_File/JS_File_6-Changing.js +24 -0
  123. package/resources/jsbuilder/JS_File/JS_File_Export_Reference.js +12 -0
  124. package/resources/jsbuilder/JS_File/JS_File_Import_Reference.js +24 -0
  125. package/resources/jsbuilder/JS_File/JS_File_Import_References.js +32 -0
  126. package/resources/jsbuilder/JS_File/JS_File_Processor.js +16 -0
  127. package/resources/jsbuilder/JS_File/JS_Files.js +16 -0
  128. package/resources/jsbuilder/Module.js +14 -0
  129. package/resources/jsbuilder/Platform.js +14 -0
  130. package/resources/jsbuilder/Platforms.js +70 -0
  131. package/resources/jsbuilder/Project.js +109 -0
  132. package/resources/jsbuilder/Reference.js +1 -0
  133. package/resources/jsbuilder/Reference_Sequence.js +16 -0
  134. package/resources/jsbuilder/Scope.js +30 -0
  135. package/resources/jsbuilder/Variable_Name_Provider.js +43 -0
  136. package/resources/jsbuilder/_JS_File.js +226 -0
  137. package/resources/jsbuilder/ast_query.js +21 -0
  138. package/resources/jsbuilder/babel/babel_consts.js +150 -0
  139. package/resources/jsbuilder/babel/babel_node_tools.js +542 -0
  140. package/resources/jsbuilder/babel/deep_iterate/deep_iterate_babel.js +604 -0
  141. package/resources/jsbuilder/build.js +16 -0
  142. package/resources/jsbuilder/platform_notes.md +66 -0
  143. package/resources/jsbuilder/test/test_ast_node.js +451 -0
  144. package/resources/jsbuilder/test/test_js_file.js +304 -0
  145. package/resources/jsbuilder/test/test_project.js +157 -0
  146. package/resources/local-server-info-resource.js +78 -78
  147. package/resources/process-js.js +537 -537
  148. package/resources/server-resource-pool.js +84 -84
  149. package/resources/website-css-resource.js +411 -411
  150. package/resources/website-javascript-resource.js +761 -1347
  151. package/resources/website-resource.js +390 -797
  152. package/resources/website-static-html-resource.js +196 -196
  153. package/server.js +170 -170
  154. package/single-control-server.js +398 -398
  155. package/single-page-app.js +129 -129
@@ -1,797 +1,390 @@
1
- /**
2
- * Created by James on 29/07/2014.
3
- */
4
-
5
- // Want to get the core resources working and tested.
6
- // Want to run a clock website / service to start with.
7
- // The server could have a clock, while clients could connect to it and share the information.
8
- // Could also experiment with P2P distribution of the data.
9
- // A clock is also useful because it contains time signals so we can see how long it takes for data to reach various machines.
10
-
11
- // A web resource in particular?
12
- // Any need for an HTML resource?
13
- // Probably not - have web resource handle HTML and websockets.
14
-
15
- // Resources could also have events which objects can listen to.
16
- // Programmatic objects can listen.
17
- // The resources may broadcast to whatever is listening.
18
-
19
- // Also, maintaining one connection stream, communicating with multiple resources - could connect through a Resource Pool, or maybe there
20
- // could be a Multi Resource Publisher that publishes a bunch of resources.
21
-
22
- // The Application router at the moment sending requests to resources.
23
- // I am thinking that rather than doing that, the requests should be handled by a resource publisher that interacts with the resource and publishes it over HTTP.
24
-
25
- // Resources in general won't handle HTTP requests, though they will have the capability.
26
- // More likely that a resource, when it is served online, will be served using a Resource Publisher (which is itself a resource), which handles HTTP implementation details that would otherwise have
27
- // to be repeated between resources.
28
-
29
- //define(["./jsgui-lang-util", './abstract-resource'], function(jsgui, AR) {
30
-
31
- // Do this not with AMD?
32
-
33
- // May need Website_Data_Resource
34
- // Would be configured within the node app, client has maximum autoconfig from connecting to the server, and integration with client-side controls.
35
- // With the DB admin, it's about returining queries (maybe from RAM) about table data.
36
- // /data/then the api?
37
-
38
- // Respond to both data HTTP requests and websocket connections.
39
-
40
- // HTTP data request -> response seems like the most important step now.
41
-
42
- // An fs Resource would be of use for file system browser.
43
- // Could have a client-side fs resource that connects to a veriety of services, could have one that connects to the server-side fs resource.
44
-
45
- // Need to connect a data-providing resource to the UI component in cases where it's not secure to do so automatically.
46
- // It would be much more secure to bring up an NTP provision service automatically than a file system one. Don't want to take that risk now.
47
- //
48
-
49
- var Site_Images = require("./website-image-resource");
50
-
51
- //console.log('1) Site_Images', Site_Images);
52
-
53
- var jsgui = require("jsgui3-html");
54
- const { each, get_a_sig, is_defined, tof } = jsgui;
55
-
56
- //var Web_Resource = require('./website-resource');
57
- const Resource = jsgui.Resource;
58
- const Router = jsgui.Router;
59
- const Evented_Class = jsgui.Evented_Class;
60
-
61
- var Resource_Pool = require("./server-resource-pool");
62
- //var Resource_Web_Admin = require('../web-admin');
63
-
64
- var Site_JavaScript = require("./website-javascript-resource");
65
- //console.log('1) Site_JavaScript', Site_JavaScript);
66
- var Site_CSS = require("./website-css-resource");
67
- var Site_Static_HTML = require("./website-static-html-resource");
68
- //var DB_Web_Resource = require('../../web/db-resource-postgres');
69
- //var database_resource_factory = require('../../db/resource/factory');
70
-
71
- const Resource_Publisher = require("../publishing/resource-publisher");
72
- const Observable_Publisher = require("../publishing/observable-publisher");
73
- const Function_Publisher = require("../publishing/function-publisher");
74
-
75
- const Data_Resource = require("./data-resource");
76
- /*
77
- var is_defined = jsgui.is_defined,
78
- fp = jsgui.fp,
79
- stringify = jsgui.stringify,
80
- tof = jsgui.tof;
81
- */
82
-
83
- /*
84
- var fs2 = require('../../fs/jsgui-node-fs2-core');
85
- var jsgui_jpeg = require('../../image/node/jsgui-node-jpeg');
86
- var jsgui_png = require('../../image/node/jsgui-node-png');
87
- var Worker = require('webworker-threads');
88
- */
89
-
90
- // Could maybe make a website resource as well?
91
-
92
- //console.log('2) Site_Images', Site_Images);
93
-
94
- // It would publish a web db, I think.
95
- // A website resource would contain a variety of requests.
96
-
97
- // Want to be able to run a website resource serving static files.
98
- // Don't want too complicated configuration here.
99
-
100
- // Does not have the FS resource by default.
101
- // Can add the FS resource to the website's resource pool.
102
- // Can just have the resource there, not in the pool, and publish it.
103
- // Set the resource's security?
104
- // Resource has security built-in?
105
-
106
-
107
- class Website_Resource extends Resource {
108
- constructor(spec = {}) {
109
- super(spec);
110
-
111
- //console.log('Init website resource');
112
- //console.log('Website_Resource spec', spec);
113
-
114
- // speck could be a string, such as 'static'
115
-
116
- //var t_spec = tof(spec);
117
- //console.log('t_spec', t_spec);
118
- // A website has a resource pool as well.
119
- // That means nested resource pools.
120
- // Server resource pool
121
- // Server router
122
- // Website resource
123
- // Website resource pool
124
- // Website router
125
- // And don't need local server info there?
126
- // Connect the local server info back up the resource and resource pool chain?
127
-
128
-
129
- // A bit of a special resource here because it has its own resource_pool.
130
-
131
- var resource_pool = new Resource_Pool({
132
- name: "Website Resource Pool"
133
- });
134
-
135
- // within a pool?
136
- // its own pool?
137
-
138
- this.resource_pool = resource_pool;
139
- //console.log('this._.resource_pool', this._.resource_pool);
140
- //throw 'stop';
141
- // maybe there is not a database.
142
- var database_spec = spec.database;
143
- var web_database_resource;
144
-
145
- // Maybe better to specifically add this???
146
- // DB resource factory according to spec is cool though.
147
-
148
- // Could use CMS-Resource?
149
-
150
-
151
- if (database_spec) {
152
- database_spec.name = database_spec.name || database_spec.database_name;
153
- var database_resource = database_resource_factory(database_spec);
154
- database_resource.start();
155
- // should start automatically when in the pool?
156
- // does the pool need to be told to start?
157
- // Though probably don't want to start the resource on initialization.
158
- resource_pool.add(database_resource);
159
- web_database_resource = new DB_Web_Resource({
160
- database: database_resource,
161
- meta: {
162
- name: "Web DB",
163
- pool: resource_pool
164
- }
165
- });
166
- }
167
- // should set the name of meta when we set this up.
168
- // That should be part of the general resource code.
169
- //console.log('web_database_resource', web_database_resource);
170
- //console.log('web_database_resource.meta._.name', web_database_resource.meta._.name);
171
- //console.log('web_database_resource.meta._.name.value()', web_database_resource.meta._.name);
172
- // So why is the resource pool not indexing it by name
173
- //throw 'stop';
174
- if (web_database_resource) {
175
- resource_pool.add(web_database_resource);
176
- }
177
-
178
- // use the Database Resource Factory.
179
- //
180
-
181
- // This needs to have a bunch of other resources inside it.
182
-
183
- // It will also need to handle requests based on the stored content.
184
- // Need to be careful about how this is routed, so that the correct resources handle the responses.
185
-
186
- // The website db resource could have a specific entry for a given URL / path.
187
- // In that case, we serve that page.
188
-
189
- // Then we check for other situations. Eg /admin, or image directories that get served from disk. Perhaps not images.
190
- // Serve the jsgui CSS and JavaScript from disk... it could maybe check to serve particular JSGUI files first.
191
-
192
- // The whole routing will get more complicated, because we will still keep what was the Application_Router, but use it as a Server_Router, or Server_Process_Router perhaps.
193
- // When it has routed to a particular application (perhaps the only application), that application's router has a chance to match pages.
194
- // Will check the DB, and may get info about dynamically generating a page.
195
- // May also just get content to serve.
196
-
197
- // PUT, POST requests - also sent by the Server Router to the Application Router.
198
- // jsgui server could start with a single website resource?
199
- // Or, the website resource is in the resource pool.
200
- // I think giving each website resource its own resource pool makes the most sense.
201
- // Also, encapsulating more in jsgui server would be good. Not needing much code at all in the app itself would be best.
202
-
203
- // Needs to have a router inside it.
204
-
205
- var router = new Router({
206
- name: "Site Router"
207
- });
208
-
209
- this.router = router;
210
- // termorary fix to get/set problem.
211
-
212
- //var router_2 = this.get('router');
213
- //console.log('router_2', router_2);
214
- //throw 'stop';
215
-
216
- // May start an admin web resource without a database connection.
217
-
218
- // Maybe there is no web database resource?
219
-
220
- var spec_web_admin = {
221
- //'web_database': web_database_resource,
222
- meta: {
223
- name: "Web Admin"
224
- }
225
- };
226
-
227
- if (web_database_resource) {
228
- spec_web_admin.web_database = web_database_resource;
229
- }
230
-
231
- //var admin_web_resource = new Resource_Web_Admin(spec_web_admin);
232
-
233
- // Site images resource as well.
234
- // The site images will interact with the web db resource, providing an API that deals with image metadata, possibly serving them too.
235
- // This is a way of keeping non-db functionality out of the web db module.
236
-
237
- // Images, JavaScript, CSS.
238
- // Need a Static_HTML resource.
239
-
240
- //console.log('Site_Images', Site_Images);
241
-
242
- // The images resource may need more work.
243
- // Need to be able to simply load images / icons into this resource.
244
-
245
-
246
-
247
-
248
- var img_resource = new Site_Images({
249
- //'meta': {
250
- name: "Site Images",
251
- pool: resource_pool
252
- // }
253
- });
254
-
255
- var js_resource = new Site_JavaScript({
256
- //'meta': {
257
- name: "Site JavaScript",
258
- pool: resource_pool
259
- //}
260
- });
261
-
262
-
263
-
264
-
265
-
266
- // Also want a static HTML server.
267
- // Would serve index.html by default I think???
268
- // Probably with the static or simplest settings.
269
-
270
- var static_html_resource;
271
-
272
- if (spec == "static") {
273
- // Will need to serve the JavaScript and CSS directories anyway.
274
- // They will generally have static content on a dynamic-html website.
275
-
276
- // The static setting means we set up serving HTML from the app's directory.
277
- // Only using JSGUI to serve what is there (for the moment)
278
- // Potentially jsgui could be used to edit a static site.
279
-
280
- // Set up and use the static HTML resource.
281
-
282
- // Maybe should be set up anyway?
283
- // Not always needed!
284
-
285
- static_html_resource = new Site_Static_HTML({
286
- //'meta': {
287
- name: "Static HTML",
288
- pool: resource_pool
289
- //}
290
- });
291
-
292
- resource_pool.push(static_html_resource);
293
-
294
- // Perhaps set it up with the specific files (automatically)?
295
- // Probably with the index.html
296
- }
297
- // Want to maybe set up the js_resource so that it serves static files from a directory.
298
- // has it set this?
299
- // Better way of setting custom paths in the future.
300
- //js_resource.meta.set('custom_paths.js/app☺js', './client/js/app.js');
301
- //js_resource.meta.set('custom_paths.js/app_bundle☺js', './client/js/app_bundle.js');
302
-
303
- var css_resource = new Site_CSS({
304
- //'meta': {
305
- name: "Site CSS",
306
- pool: resource_pool
307
- //}
308
- });
309
-
310
-
311
- js_resource.on('extracted-controls-css', str_extracted_css => {
312
-
313
- //console.log('website resource has got the extracted controls css from the js: ' + str_extracted_css);
314
-
315
- // serve this as /css/controls.css
316
-
317
- // Give it to the css resource to serve.
318
-
319
- //css_resource.ser
320
-
321
- css_resource.serve_str_css('controls.css', str_extracted_css);
322
-
323
-
324
-
325
-
326
-
327
-
328
- // Will serve this as controls.css
329
- // Separate HTTP request, will get more CSS for the moment.
330
-
331
-
332
-
333
-
334
-
335
- })
336
-
337
-
338
- // Not so sure about this data resource.
339
- // Don't use it for the moment - it's not functional.
340
-
341
- /*
342
- var data_resource = new Data_Resource({
343
- //'meta': {
344
- name: "Site Data",
345
- pool: resource_pool
346
- //}
347
- });
348
- */
349
- //resource_pool.push(admin_web_resource);
350
-
351
- // javascript and css resources.
352
- resource_pool.push(router);
353
- resource_pool.push(img_resource);
354
- resource_pool.push(js_resource);
355
- resource_pool.push(css_resource);
356
- //resource_pool.push(data_resource);
357
-
358
- // anything ending in .css as well.
359
- // Routing maybe wouldn't work like that.
360
- //router.set_route('*.css', css_resource, css_resource.process);
361
-
362
- router.set_route("css/*", css_resource, css_resource.process);
363
- router.set_route("js/*", js_resource, js_resource.process);
364
- // As well as this, it could get the JavaScript resource to serve the JavaScript from the app's js directory.
365
- js_resource.serve_directory("js");
366
-
367
- router.set_route("img/*", img_resource, img_resource.process);
368
- router.set_route("images/*", img_resource, img_resource.process);
369
- //router.set_route("data/*", data_resource, data_resource.process);
370
-
371
- // removing /data default path to data resource (which doesn't appear to do much right now)
372
-
373
-
374
- //router.set_route('resources/*', data_resource, data_resource.process);
375
-
376
- //let server_pool = this.pool;
377
-
378
- this.map_resource_publishers = this.map_resource_publishers || {};
379
-
380
- //let that = this;
381
-
382
- // always under a /reseources/ url path?
383
- // Maybe just want it under the resource name.
384
-
385
-
386
- // It could add the path when it publishes a resource.
387
- // A particular path could have its route set when its published.
388
-
389
- // Or change the client-side wiring up?
390
- // Could include the path of the resource in the def that's sent to the client.
391
-
392
- // Publishing functions / being able to do so, not under the /resources/ path makes a lot of sense.
393
- // Intuitively a website arch may not need the /resources/ path.
394
- // Be able to set a custom path / route.
395
-
396
- // A publisher could create a request handler function?
397
-
398
- // resources maybe will not be set under /resources/.
399
- // worth keeping as a default?
400
- // does make sense unless a 'path' is specified in the options.
401
-
402
-
403
-
404
-
405
-
406
-
407
- // keep this for the moment...
408
-
409
-
410
- router.set_route("resources/:resource_name/*", this, (req, res) => {
411
- //console.log('website router routing resource request');
412
- //console.log('route_data.params', req.params);
413
- //let resource_name = req.params.resource_name;
414
- //console.log('resource_name', resource_name);
415
- //console.log('req', req);
416
- let { url, method } = req;
417
- let s_url = url.split("/");
418
- //console.log('s_url', s_url);
419
- let resource_short_name = s_url[2];
420
- //console.log('resource_short_name', resource_short_name);
421
- //console.log('Object.keys(req)', Object.keys(req));
422
- //console.log('req.params', req.params);
423
- //console.log('url, method', url, method);
424
- //console.log('resource_name', resource_name);
425
- // consult the map of published resources
426
- // or supposed to be encapsulated
427
- // That is the server resource pool.
428
- //console.log('this.pool', this.pool);
429
- // Route it to a server resource?
430
-
431
- //console.log('Object.keys(this.map_resource_publishers)', Object.keys(this.map_resource_publishers));
432
- //console.log('this', this);
433
-
434
- let resource_publisher = this.map_resource_publishers[resource_short_name];
435
- //console.log('1) this', this);
436
- //console.log('1) that', that);
437
- // that
438
- //console.log('this.map_resource_publishers', that.map_resource_publishers);
439
- //console.log('!!resource_publisher', !!resource_publisher);
440
- //console.trace();
441
-
442
- if (resource_publisher) {
443
- resource_publisher.handle_http(req, res);
444
- }
445
- });
446
- // The website (admin) resource will make use of the images resource where necessary.
447
- // The website (admin) resource will be able to get the images resource from the resource pool.
448
- //router.set_route('admin/*', admin_web_resource, admin_web_resource.process);
449
- /*
450
- resource_pool.push(new Site_Images({
451
- 'meta': {
452
- 'name': 'Site Images'
453
- }
454
- }));
455
- */
456
- // set up the routes.
457
- if (!is_defined(spec)) spec = {};
458
- //console.log('pre super');
459
- //Web_Resource.prototype.init.call(this, spec);
460
- //this.router = router;
461
- this.resource_pool = resource_pool;
462
- //this.set('router', router);
463
- //this.set('resource_pool', resource_pool);
464
- // Super call was not working for some reason.
465
- //this._super(spec);
466
- }
467
-
468
- publishing_get_pub(item) {
469
- // don't need to name it?
470
- // name could already be included in item.
471
-
472
- let pub;
473
- if (item instanceof jsgui.Resource) {
474
- pub = new Resource_Publisher({
475
- resource: item
476
- });
477
- //this.map_resource_publishers[published_name] = resource_publisher;
478
-
479
- //item.name = item.name || published_name;
480
- // add that resource!
481
- // (to the pool?)
482
- //console.log('item', item);
483
- //this.resource_pool.add(item);
484
-
485
- //console.log('Object.keys(this.map_resource_publishers)', Object.keys(this.map_resource_publishers));
486
- } else {
487
- // if its a function
488
- // return that function call to the response.
489
- let t_item = typeof item;
490
- if (t_item === "function") {
491
- // Function_Call_Publisher
492
- // could respec this.
493
- // And the Function_Publisher operates through the Publisher API. Not sure what that is right now though.
494
-
495
- pub = new Function_Publisher({
496
- fn: item
497
- });
498
- //this.map_resource_publishers[published_name] = pub;
499
- //
500
- } else {
501
- if (item.next && item.complete && item.error) {
502
- // assuming observable
503
- // Observable publisher
504
- // One way sending...
505
- //console.log('using Observable_Publisher');
506
- pub = new Observable_Publisher({
507
- obs: item
508
- });
509
- // or not a resource publisher, an observable publisher.
510
- //this.map_resource_publishers = this.map_resource_publishers || {};
511
- //this.map_resource_publishers[published_name] = obs_pub;
512
-
513
- //console.log('2) this', this);
514
- //console.log('this.map_resource_publishers', this.map_resource_publishers);
515
- //console.trace();
516
- } else {
517
- console.log("item", item);
518
- throw "Unrecognised item type. Possibly node module versions are wrong / have not been linked fully.";
519
- }
520
- }
521
- //if (item instanceof Evented_Class) {
522
- }
523
- return pub;
524
- }
525
-
526
-
527
-
528
-
529
- // publish within resources?
530
- publish(published_name, item, schema) {
531
-
532
- // more sig matching where we check for a path?
533
- // use of an options object?
534
-
535
- // worth parsing the signature, looking for specific things.
536
- // likely could save some space and make the function clearer.
537
-
538
- // and automatically publishing the function schema / api, if the function is made with mfp etc?
539
- // not for the moment...?
540
-
541
- // or assign its full params.
542
-
543
- // an options object?
544
- // path, (name?), item
545
-
546
- // ofp applying as plural using multiple keys and values from the options object
547
- // not yet. that would be a way of calling the single function.
548
-
549
- // allowing it to take an options object would be very useful.
550
- // specifying the name as well as the path?
551
-
552
-
553
-
554
- // Want to be able to (optionally) choose the full path its published under.
555
- // Not (only) published under /resources/name.
556
- // More flexibility in creation of the website / service API / structure.
557
-
558
- // single and multi
559
- // effectively like arrayify or mapify
560
- // 1 or multiple args
561
- // string, whatever
562
- // Including published info within the HTML document?
563
- // Could go there as a comment.
564
- // A hidden control may work well.
565
- // Just as a jsgui property would be fine.
566
- // Automatic wiring of the services / publishers so that they are available on the client without requiring further plumbing.
567
-
568
- let sig = get_a_sig(arguments),
569
- a = arguments,
570
- l = a.length;
571
-
572
- //console.log("a", a);
573
- //console.log("sig", sig);
574
- //console.log("l", l);
575
- //throw 'stop';
576
-
577
- // could use mfp, ofp etc.
578
-
579
- const single = (published_name, item) => {
580
-
581
- // option of
582
-
583
- //console.log("Website_Resource publish", published_name);
584
- //console.log('published_name, item', published_name, item);
585
- //this.map_resource_publishers = this.map_resource_publishers || {};
586
- // .__resource ?
587
- // instanceof means same version?
588
- if (item instanceof jsgui.Resource) {
589
- let resource_publisher = new Resource_Publisher({
590
- resource: item,
591
- name: published_name
592
- });
593
- this.map_resource_publishers[published_name] = resource_publisher;
594
-
595
- item.name = item.name || published_name;
596
- // add that resource!
597
- // (to the pool?)
598
- //console.log('item', item);
599
- this.resource_pool.add(item);
600
-
601
- //console.log('Object.keys(this.map_resource_publishers)', Object.keys(this.map_resource_publishers));
602
- } else {
603
- // if its a function
604
- // return that function call to the response.
605
- let t_item = typeof item;
606
- if (t_item === "function") {
607
- // Function_Call_Publisher
608
- // could respec this.
609
-
610
- // And the Function_Publisher operates through the Publisher API. Not sure what that is right now though.
611
- // directly attaching the resource publishers?
612
- let pub = new Function_Publisher({
613
- fn: item,
614
- schema: schema
615
- });
616
- this.map_resource_publishers[published_name] = pub;
617
- //
618
-
619
- } else {
620
- if (item.next && item.complete && item.error) {
621
- // assuming observable
622
- // Observable publisher
623
- // One way sending...
624
- //console.log('using Observable_Publisher');
625
- let obs_pub = new Observable_Publisher({
626
- obs: item,
627
- schema: schema
628
- });
629
- // or not a resource publisher, an observable publisher.
630
- //this.map_resource_publishers = this.map_resource_publishers || {};
631
- this.map_resource_publishers[published_name] = obs_pub;
632
-
633
- //console.log('2) this', this);
634
- //console.log('this.map_resource_publishers', this.map_resource_publishers);
635
- //console.trace();
636
- } else {
637
- console.log("item", item);
638
- throw "Unrecognised item type. Possibly node module versions are wrong / have not been linked fully.";
639
- }
640
- }
641
- //if (item instanceof Evented_Class) {
642
- }
643
-
644
- // Need to give it a name to publish it as
645
-
646
- // server needs a Resource_Publisher.
647
- // Some resources include their own publishing.
648
- // (existing things like javascript-resource)
649
-
650
- // needs a name
651
-
652
- //this.resource_publisher = this.resource_publisher || new Resource_Publisher({
653
-
654
- //this.resource_pool.map_resource_publishers = resource_publisher;
655
-
656
- // website resource needs the map of resource publishers.
657
-
658
- // Should actually publish within a Website_Resource...
659
- // Server holds this.
660
- };
661
-
662
- if (sig === "[o]") {
663
- each(a[0], (v, i) => {
664
- single(i, v);
665
- })
666
- } else {
667
-
668
- single(published_name, item);
669
- }
670
- }
671
-
672
- get resource_names() {
673
- //console.log('this.resource_pool', this.resource_pool);
674
- return this.resource_pool.resource_names;
675
- }
676
-
677
- get_resource(resource_name) {
678
- var resource_pool = this.resource_pool;
679
- //console.log('resource_pool', resource_pool);
680
-
681
- //console.log('this._.resource_pool', this._.resource_pool);
682
-
683
- //throw 'stop';
684
- return resource_pool.get_resource(resource_name);
685
- }
686
-
687
- get def_resource_publishers() {
688
- const res = {};
689
- each(this.map_resource_publishers, (rp, name) => {
690
-
691
-
692
-
693
- //console.log('name', name);
694
- //console.log('rp', rp);
695
- let def = {
696
- name: name,
697
- type: rp.type
698
- }
699
- res[name] = def;
700
- if (rp.type === 'function') {
701
- if (rp.schema) def.schema = rp.schema;
702
- }
703
-
704
- /*
705
- if (rp.type === 'function') {
706
-
707
- }
708
- if (rp.type === 'observable') {
709
-
710
- }
711
- */
712
-
713
- // look up the def of each of the resource publishers.
714
- // could include a little schema with each of the functions to be published.
715
- // the observable publisher responds to the observable events.
716
-
717
-
718
-
719
-
720
- })
721
-
722
- return res;
723
- }
724
-
725
- start(callback) {
726
- // Need to wait until the database has started.
727
- //console.log('Website Resource start');
728
- // start the db / web db resources?
729
- // start the resource pool?
730
- var resource_pool = this.resource_pool;
731
- resource_pool.start(callback);
732
- //callback(null, true);
733
-
734
- // needs various other resources to have started.
735
-
736
- // check the requirements
737
-
738
- // check requirements recursive - checks the requirements of everything required, and if they have the check_requirements function, it uses that.
739
- // I think using the system of names APIs will help here.
740
-
741
- // The web db resource needs to have been started.
742
-
743
- //throw 'no start function defined for web resource (subclass)'
744
- }
745
-
746
- meets_requirements() {
747
- // Likely will be part of Status
748
-
749
- //return false;
750
-
751
- return true;
752
- }
753
-
754
- // Needs to be able to process HTTP requests. A bit like the Router in that way.
755
- process(req, res) {
756
- //console.log('website process request req.url', req.url);
757
- //throw 'stop';
758
-
759
- var remoteAddress = req.connection.remoteAddress;
760
- var router = this.router;
761
- var res_process = router.process(req, res);
762
- if (res_process === false) {
763
- // Perhaps it's one of the static HTML files?
764
- // Could try to process it using static HTML?
765
- // Or an internal change / proxy from / to /index.html
766
-
767
- // These will possibly be base level page requests, just looking for the file on disk and serving it.
768
-
769
- // At this point we hand it off to the static HTML processor.
770
- // Need some more root directory level handling, but the main processing system is about setting up paths and dealing with parameters.
771
- //
772
- // Special case of '/'
773
-
774
- if (req.url === "/") {
775
- // Send this to the static HTML processing system.
776
-
777
- var static_html_resource = this.resource_pool.get_resource(
778
- "Static HTML"
779
- );
780
- //console.log('static_html_resource', static_html_resource);
781
- // And lets get the static resource to process it
782
- if (static_html_resource) {
783
- static_html_resource.process(req, res);
784
- }
785
- } else {
786
- // show a 404
787
- res.writeHead(404, {
788
- "Content-Type": "text/plain"
789
- });
790
- res.write("404 Not Found\n");
791
- res.end();
792
- }
793
- }
794
- }
795
- }
796
-
797
- module.exports = Website_Resource;
1
+ /**
2
+ * Created by James on 29/07/2014.
3
+ */
4
+
5
+ // Want to get the core resources working and tested.
6
+ // Want to run a clock website / service to start with.
7
+ // The server could have a clock, while clients could connect to it and share the information.
8
+ // Could also experiment with P2P distribution of the data.
9
+ // A clock is also useful because it contains time signals so we can see how long it takes for data to reach various machines.
10
+
11
+ // A web resource in particular?
12
+ // Any need for an HTML resource?
13
+ // Probably not - have web resource handle HTML and websockets.
14
+
15
+ // Resources could also have events which objects can listen to.
16
+ // Programmatic objects can listen.
17
+ // The resources may broadcast to whatever is listening.
18
+
19
+ // Also, maintaining one connection stream, communicating with multiple resources - could connect through a Resource Pool, or maybe there
20
+ // could be a Multi Resource Publisher that publishes a bunch of resources.
21
+
22
+ // The Application router at the moment sending requests to resources.
23
+ // I am thinking that rather than doing that, the requests should be handled by a resource publisher that interacts with the resource and publishes it over HTTP.
24
+
25
+ // Resources in general won't handle HTTP requests, though they will have the capability.
26
+ // More likely that a resource, when it is served online, will be served using a Resource Publisher (which is itself a resource), which handles HTTP implementation details that would otherwise have
27
+ // to be repeated between resources.
28
+
29
+ //define(["./jsgui-lang-util", './abstract-resource'], function(jsgui, AR) {
30
+
31
+ // Do this not with AMD?
32
+
33
+ // May need Website_Data_Resource
34
+ // Would be configured within the node app, client has maximum autoconfig from connecting to the server, and integration with client-side controls.
35
+ // With the DB admin, it's about returining queries (maybe from RAM) about table data.
36
+ // /data/then the api?
37
+
38
+ // Respond to both data HTTP requests and websocket connections.
39
+
40
+ // HTTP data request -> response seems like the most important step now.
41
+
42
+ // An fs Resource would be of use for file system browser.
43
+ // Could have a client-side fs resource that connects to a veriety of services, could have one that connects to the server-side fs resource.
44
+
45
+ // Need to connect a data-providing resource to the UI component in cases where it's not secure to do so automatically.
46
+ // It would be much more secure to bring up an NTP provision service automatically than a file system one. Don't want to take that risk now.
47
+ //
48
+
49
+ var Site_Images = require("./website-image-resource");
50
+
51
+ //console.log('1) Site_Images', Site_Images);
52
+
53
+ var jsgui = require("jsgui3-html");
54
+ const { each, get_a_sig, is_defined, tof } = jsgui;
55
+
56
+ //var Web_Resource = require('./website-resource');
57
+ const Resource = jsgui.Resource;
58
+ const Router = jsgui.Router;
59
+ //const Evented_Class = jsgui.Evented_Class;
60
+
61
+ var Resource_Pool = require("./server-resource-pool");
62
+ //var Resource_Web_Admin = require('../web-admin');
63
+
64
+ var Site_JavaScript = require("./website-javascript-resource");
65
+ //console.log('1) Site_JavaScript', Site_JavaScript);
66
+ var Site_CSS = require("./website-css-resource");
67
+ var Site_Static_HTML = require("./website-static-html-resource");
68
+ //var DB_Web_Resource = require('../../web/db-resource-postgres');
69
+ //var database_resource_factory = require('../../db/resource/factory');
70
+
71
+ const Resource_Publisher = require("../publishing/resource-publisher");
72
+ const Observable_Publisher = require("../publishing/observable-publisher");
73
+ const Function_Publisher = require("../publishing/function-publisher");
74
+ //const Data_Resource = require("./data-resource");
75
+
76
+ class Website_Resource extends Resource {
77
+ constructor(spec = {}) {
78
+ super(spec);
79
+ // A bit of a special resource here because it has its own resource_pool.
80
+ var resource_pool = new Resource_Pool({
81
+ name: "Website Resource Pool"
82
+ });
83
+ this.resource_pool = resource_pool;
84
+ var database_spec = spec.database;
85
+ var web_database_resource;
86
+
87
+ if (database_spec) {
88
+ database_spec.name = database_spec.name || database_spec.database_name;
89
+ var database_resource = database_resource_factory(database_spec);
90
+ database_resource.start();
91
+ // should start automatically when in the pool?
92
+ // does the pool need to be told to start?
93
+ // Though probably don't want to start the resource on initialization.
94
+ resource_pool.add(database_resource);
95
+ web_database_resource = new DB_Web_Resource({
96
+ database: database_resource,
97
+ meta: {
98
+ name: "Web DB",
99
+ pool: resource_pool
100
+ }
101
+ });
102
+ }
103
+
104
+ if (web_database_resource) {
105
+ resource_pool.add(web_database_resource);
106
+ }
107
+
108
+ var router = new Router({
109
+ name: "Site Router"
110
+ });
111
+
112
+ this.router = router;
113
+
114
+ var spec_web_admin = {
115
+ //'web_database': web_database_resource,
116
+ meta: {
117
+ name: "Web Admin"
118
+ }
119
+ };
120
+
121
+ if (web_database_resource) {
122
+ spec_web_admin.web_database = web_database_resource;
123
+ }
124
+
125
+ var img_resource = new Site_Images({
126
+ //'meta': {
127
+ name: "Site Images",
128
+ pool: resource_pool
129
+ // }
130
+ });
131
+
132
+ var js_resource = new Site_JavaScript({
133
+ //'meta': {
134
+ name: "Site JavaScript",
135
+ pool: resource_pool
136
+ //}
137
+ });
138
+
139
+ // Also want a static HTML server.
140
+ // Would serve index.html by default I think???
141
+ // Probably with the static or simplest settings.
142
+
143
+ var static_html_resource;
144
+
145
+ if (spec == "static") {
146
+ static_html_resource = new Site_Static_HTML({
147
+ //'meta': {
148
+ name: "Static HTML",
149
+ pool: resource_pool
150
+ //}
151
+ });
152
+
153
+ resource_pool.push(static_html_resource);
154
+
155
+ // Perhaps set it up with the specific files (automatically)?
156
+ // Probably with the index.html
157
+ }
158
+ var css_resource = new Site_CSS({
159
+ //'meta': {
160
+ name: "Site CSS",
161
+ pool: resource_pool
162
+ //}
163
+ });
164
+
165
+
166
+ js_resource.on('extracted-controls-css', str_extracted_css => {
167
+ css_resource.serve_str_css('controls.css', str_extracted_css);
168
+ // Will serve this as controls.css
169
+ // Separate HTTP request, will get more CSS for the moment.
170
+
171
+ });
172
+ // javascript and css resources.
173
+ resource_pool.push(router);
174
+ resource_pool.push(img_resource);
175
+ resource_pool.push(js_resource);
176
+ resource_pool.push(css_resource);
177
+ //resource_pool.push(data_resource);
178
+
179
+ // anything ending in .css as well.
180
+ // Routing maybe wouldn't work like that.
181
+ //router.set_route('*.css', css_resource, css_resource.process);
182
+
183
+ router.set_route("css/*", css_resource, css_resource.process);
184
+ router.set_route("js/*", js_resource, js_resource.process);
185
+ // As well as this, it could get the JavaScript resource to serve the JavaScript from the app's js directory.
186
+ js_resource.serve_directory("js");
187
+ router.set_route("img/*", img_resource, img_resource.process);
188
+ router.set_route("images/*", img_resource, img_resource.process);
189
+ this.map_resource_publishers = this.map_resource_publishers || {};
190
+ router.set_route("resources/:resource_name/*", this, (req, res) => {
191
+ let { url, method } = req;
192
+ let s_url = url.split("/");
193
+ let resource_short_name = s_url[2];
194
+ let resource_publisher = this.map_resource_publishers[resource_short_name];
195
+ if (resource_publisher) {
196
+ resource_publisher.handle_http(req, res);
197
+ }
198
+ });
199
+ if (!is_defined(spec)) spec = {};
200
+ this.resource_pool = resource_pool;
201
+ }
202
+
203
+ publishing_get_pub(item) {
204
+ let pub;
205
+ if (item instanceof jsgui.Resource) {
206
+ pub = new Resource_Publisher({
207
+ resource: item
208
+ });
209
+ } else {
210
+ // if its a function
211
+ // return that function call to the response.
212
+ let t_item = typeof item;
213
+ if (t_item === "function") {
214
+ // Function_Call_Publisher
215
+ // could respec this.
216
+ // And the Function_Publisher operates through the Publisher API. Not sure what that is right now though.
217
+
218
+ pub = new Function_Publisher({
219
+ fn: item
220
+ });
221
+ //this.map_resource_publishers[published_name] = pub;
222
+ //
223
+ } else {
224
+ if (item.next && item.complete && item.error) {
225
+ // assuming observable
226
+ // Observable publisher
227
+ // One way sending...
228
+ //console.log('using Observable_Publisher');
229
+ pub = new Observable_Publisher({
230
+ obs: item
231
+ });
232
+ // or not a resource publisher, an observable publisher.
233
+ //this.map_resource_publishers = this.map_resource_publishers || {};
234
+ //this.map_resource_publishers[published_name] = obs_pub;
235
+
236
+ //console.log('2) this', this);
237
+ //console.log('this.map_resource_publishers', this.map_resource_publishers);
238
+ //console.trace();
239
+ } else {
240
+ console.log("item", item);
241
+ throw "Unrecognised item type. Possibly node module versions are wrong / have not been linked fully.";
242
+ }
243
+ }
244
+ //if (item instanceof Evented_Class) {
245
+ }
246
+ return pub;
247
+ }
248
+
249
+ // publish within resources?
250
+ publish(published_name, item, schema) {
251
+ let sig = get_a_sig(arguments),
252
+ a = arguments,
253
+ l = a.length;
254
+ const single = (published_name, item) => {
255
+ if (item instanceof jsgui.Resource) {
256
+ let resource_publisher = new Resource_Publisher({
257
+ resource: item,
258
+ name: published_name
259
+ });
260
+ this.map_resource_publishers[published_name] = resource_publisher;
261
+
262
+ item.name = item.name || published_name;
263
+ // add that resource!
264
+ // (to the pool?)
265
+ //console.log('item', item);
266
+ this.resource_pool.add(item);
267
+
268
+ //console.log('Object.keys(this.map_resource_publishers)', Object.keys(this.map_resource_publishers));
269
+ } else {
270
+ // if its a function
271
+ // return that function call to the response.
272
+ let t_item = typeof item;
273
+ if (t_item === "function") {
274
+ // Function_Call_Publisher
275
+ // could respec this.
276
+
277
+ // And the Function_Publisher operates through the Publisher API. Not sure what that is right now though.
278
+ // directly attaching the resource publishers?
279
+ let pub = new Function_Publisher({
280
+ fn: item,
281
+ schema: schema
282
+ });
283
+ this.map_resource_publishers[published_name] = pub;
284
+ //
285
+
286
+ } else {
287
+ if (item.next && item.complete && item.error) {
288
+ let obs_pub = new Observable_Publisher({
289
+ obs: item,
290
+ schema: schema
291
+ });
292
+ this.map_resource_publishers[published_name] = obs_pub;
293
+ } else {
294
+ console.log("item", item);
295
+ throw "Unrecognised item type. Possibly node module versions are wrong / have not been linked fully.";
296
+ }
297
+ }
298
+ //if (item instanceof Evented_Class) {
299
+ }
300
+ };
301
+
302
+ if (sig === "[o]") {
303
+ each(a[0], (v, i) => {
304
+ single(i, v);
305
+ })
306
+ } else {
307
+
308
+ single(published_name, item);
309
+ }
310
+ }
311
+
312
+ get resource_names() {
313
+ //console.log('this.resource_pool', this.resource_pool);
314
+ return this.resource_pool.resource_names;
315
+ }
316
+
317
+ get_resource(resource_name) {
318
+ var resource_pool = this.resource_pool;
319
+ //console.log('resource_pool', resource_pool);
320
+
321
+ //console.log('this._.resource_pool', this._.resource_pool);
322
+
323
+ //throw 'stop';
324
+ return resource_pool.get_resource(resource_name);
325
+ }
326
+
327
+ get def_resource_publishers() {
328
+ const res = {};
329
+ each(this.map_resource_publishers, (rp, name) => {
330
+
331
+ let def = {
332
+ name: name,
333
+ type: rp.type
334
+ }
335
+ res[name] = def;
336
+ if (rp.type === 'function') {
337
+ if (rp.schema) def.schema = rp.schema;
338
+ }
339
+
340
+ })
341
+
342
+ return res;
343
+ }
344
+
345
+ start(callback) {
346
+ var resource_pool = this.resource_pool;
347
+ resource_pool.start(callback);
348
+ }
349
+
350
+ meets_requirements() {
351
+ // Likely will be part of Status
352
+
353
+ //return false;
354
+
355
+ return true;
356
+ }
357
+
358
+ // Needs to be able to process HTTP requests. A bit like the Router in that way.
359
+ process(req, res) {
360
+ //console.log('website process request req.url', req.url);
361
+ //throw 'stop';
362
+
363
+ var remoteAddress = req.connection.remoteAddress;
364
+ var router = this.router;
365
+ var res_process = router.process(req, res);
366
+ if (res_process === false) {
367
+ if (req.url === "/") {
368
+ // Send this to the static HTML processing system.
369
+
370
+ var static_html_resource = this.resource_pool.get_resource(
371
+ "Static HTML"
372
+ );
373
+ //console.log('static_html_resource', static_html_resource);
374
+ // And lets get the static resource to process it
375
+ if (static_html_resource) {
376
+ static_html_resource.process(req, res);
377
+ }
378
+ } else {
379
+ // show a 404
380
+ res.writeHead(404, {
381
+ "Content-Type": "text/plain"
382
+ });
383
+ res.write("404 Not Found\n");
384
+ res.end();
385
+ }
386
+ }
387
+ }
388
+ }
389
+
390
+ module.exports = Website_Resource;