jsgui3-server 0.0.94 → 0.0.95

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.
@@ -17,38 +17,9 @@ const browserify = require('browserify');
17
17
  const babel = require('@babel/core');
18
18
  const stream_to_array = require('stream-to-array');
19
19
 
20
-
21
- // Bundling maybe would not be proper SSR sometimes.
22
- // SSR is needed when the server needs to generate the specific info to start the page with.
23
- // However, specific info could be quickly updated too.
24
- // In many cases bundling would be more efficient that per-page SSR.
25
-
26
-
27
-
28
-
29
-
30
-
31
- // Maybe some web pages should be unbundlable? Or we note that they are dynamic (somehow).
32
- // Perhaps another fn should do that check. Don't assume all pages will bundle OK. Could raise obs error if needed.
33
-
34
- // Bundling CSS from a JS page.
35
-
36
- // const bundle_js_to_css
37
-
38
-
39
- // Maybe best to move to a JS Bundler.
40
-
41
- // CSS bundler too
42
- // Could consult JS source files. Could use the JS bundler to get css from JS.
43
-
44
- // Creating working bundles from JS (and other) source files seems like an important task.
45
-
46
20
  const {bundle_js} = JS_Bundler;
47
-
48
-
49
21
  // Observable to load the basic CSS, or return it.
50
22
 
51
-
52
23
  const get_basic_css_content_obj = () => {
53
24
  return obs((next, complete, error) => {
54
25
 
@@ -82,143 +53,45 @@ const get_basic_css_content_obj = () => {
82
53
 
83
54
  const bundle_web_page = (webpage, options = {}) => {
84
55
  const {content} = webpage;
85
- //console.log('bundle web page');
86
- //console.log('webpage', webpage);
87
- //console.trace();
88
-
89
-
90
- // What if there is no content in the webpage?
91
-
92
56
  let {disk_path_client_js} = options;
93
- //console.log('disk_path_client_js', disk_path_client_js);
94
-
95
-
96
- // Could use the CSS bundler to extract the css from the JS.
97
-
98
-
99
-
100
- // Could look for the css within disk_path_client_js.
101
-
102
-
103
- //if (options.js_client) js_client_disk_path =
104
-
105
- // Then depending on the content type
106
-
107
57
 
108
58
  const t_content = tof(content);
109
-
110
- //console.log('content', content);
111
- //console.log('t_content', t_content);
112
-
113
- //console.log('');
114
-
115
- //console.log('pre return obs');
116
- //console.trace();
59
+ console.log('t_content', t_content);
117
60
 
118
61
  return obs((next, complete, error) => {
119
62
  const res = new Bundle();
120
-
121
-
122
-
123
-
124
-
125
63
  (async () => {
126
-
127
64
  const o_basic_css = await get_basic_css_content_obj();
128
- //console.log('o_css', o_css);
129
- //console.log('t_content', t_content);
130
- //throw 'stop';
131
-
132
65
  if (t_content === 'string') {
133
- // Hardly anything to bundle. No JS required, so it seems.
134
- // Maybe put it inside a basic JSGUI page control...?
135
-
136
- // Page may still have a title.
137
- const html = `<html><head><title>${webpage.title}</title></head><body>${content}</body></html>`;
138
- const buff = Buffer.from(html, "utf-8");
139
-
140
- // and value with different types of compression....
141
- // worth having them ready.
66
+ const buff = Buffer.from(`<html><head><title>${webpage.title}</title></head><body>${content}</body></html>`, "utf-8");
142
67
  res.push({
143
68
  'path': webpage.path,
144
69
  'value': buff,
145
70
  'content-type': 'text/html'
146
71
  });
147
72
  complete(res);
148
-
149
73
  } else if (t_content === 'control') {
150
- //console.log ('content.context', content.context);
151
- //console.log('content', content);
152
-
153
- // May need to clone this control, putting it into new contexts.
154
- // Or render it with a temporary context?
155
-
156
- // Controls with temportary contexts could be useful.
157
74
  if (content instanceof Control) {
158
75
  //console.log('content is control');
159
76
  //console.log('content.constructor.name', content.constructor.name);
160
77
  if (content instanceof HTML_Document) {
161
78
  console.log('content is an html document');
79
+ // May in fact be much easier.
80
+ // Assume already bundled?
81
+ // Probably best not to.
82
+
83
+ // Want to have nicely named functions that get called to do things.
84
+
85
+
162
86
 
163
87
  throw 'NYI';
164
88
  } else {
165
-
166
- // create an HTML document
167
- // then put this control (or a clone of it) inside that HTML document.
168
-
169
- // We may be changing the construction / rendering order here.
170
- // Seem to be doing more construction of controls without a Page_Context.
171
- // That page_context may only be important at a later stage.
172
-
173
- // Some pages will render differently depending on when they are rendered.
174
- // That may be why a server would need to individually build each page.
175
- // Eg if there is SSR of latest news items.
176
-
177
- // Question of pre-rendering or rendering on each page request.
178
- // Maybe we don't bundle it if it includes dynamic content.
179
- // Though we would need to bundle the JS.
180
-
181
- // Could create a new page context.
182
-
183
- //const cloned_content = content.clone();
184
-
185
- // Should be able to clone a control?
186
-
187
- //console.log('cloned_content', cloned_content);
188
-
189
- // The page context may not have a request and response.
190
- // For bundling, won't have one (unless one were made).
191
-
192
- // Creating the JS bundle...
193
-
194
- // Make it require jsgui-html for the moment.
195
- // Requiring specific JS of the client control may make more sense....
196
-
197
- // First let's bundle the JS.
198
- // Or get the JS bundle for 'jsgui3-html'.
199
-
200
- // js.serve_package('/js/app.js', js_client, o_serve_package, (err, served) => {
201
- // maybe should make a JS_Bundler class.
202
-
203
- // May need to be told earlier what file path we are using for the client js bundle.
204
-
205
89
  const diskpath_js_client = disk_path_client_js || require.resolve('jsgui3-html');
206
- //const diskpath_js_client = require.resolve('./../controls/page/admin.js');
207
-
208
- // Bundle js could be an observable
209
- // So when it finds CSS, it can output that.
210
- // A Control's CSS property, within the JS definition.
211
- // That can be output to a CSS file, copied or removed from the JS file.
212
-
213
- // js_mode debug
214
-
215
90
  res_bundle_js = await bundle_js(diskpath_js_client, {
216
91
  'js_mode': 'mini',
217
92
  'babel': 'mini'
218
93
  });
219
-
220
94
  console.log('res_bundle_js', res_bundle_js);
221
-
222
95
  throw 'NYI';
223
96
 
224
97
  /*
@@ -273,64 +146,54 @@ const bundle_web_page = (webpage, options = {}) => {
273
146
  }
274
147
  });
275
148
  */
276
-
277
149
  // Need to create a new control
278
-
279
150
  //throw 'NYI';
280
151
  }
281
152
  } else {
282
153
  throw 'NYI';
283
154
  }
284
155
  } else if (t_content === 'undefined') {
285
- // Hardly anything to bundle. No JS required, so it seems.
286
- // Maybe put it inside a basic JSGUI page control...?
287
-
288
- // Page may still have a title.
289
156
  const html = `<html><head></head><body></body></html>`;
290
157
  const buff = Buffer.from(html, "utf-8");
291
-
292
- // and value with different types of compression....
293
- // worth having them ready.
294
158
  res.push({
295
159
  'path': webpage.path,
296
160
  'value': buff,
297
161
  'content-type': 'text/html'
298
162
  });
299
163
  complete(res);
300
-
301
164
  } else {
302
165
  //console.log('t_content', t_content);
303
166
  if (t_content === 'function') {
304
-
305
167
  const spc = new Server_Page_Context({
306
168
  //request:
307
169
  });
308
-
309
170
  const Ctrl = content;
310
171
  const ctrl = new Ctrl({
311
172
  'context': spc
312
173
  });
313
-
314
-
315
- // Or from a different chain maybe? Seems unorganised that way.
316
-
317
174
  if (ctrl instanceof HTML_Document) {
318
- console.trace();
319
- throw 'NYI';
320
- } else {
321
175
 
322
- console.log('Bundle path A');
176
+ // Need way to fix this - it had been working OK before in some cases.
323
177
 
178
+ /*
324
179
 
325
- // is ctrl a Control instance?
180
+ console.log('ctrl', ctrl);
181
+ console.trace();
182
+ throw 'NYI - HTML_Document processing NYI';
183
+ */
326
184
 
185
+ const buff_html = Buffer.from(ctrl.render(), "utf-8");
186
+ res.push({
187
+ 'path': webpage.path,
188
+ 'value': buff_html,
189
+ 'content-type': 'text/html'
190
+ });
191
+ } else {
192
+ console.log('Bundle path A');
193
+ // is ctrl a Control instance?
327
194
  // Is it a control in some other way?
328
195
 
329
-
330
-
331
196
  if (ctrl instanceof Control || tof(ctrl) === 'control') {
332
-
333
-
334
197
  const doc = new Client_HTML_Document({
335
198
  context: spc
336
199
  });
@@ -338,45 +201,21 @@ const bundle_web_page = (webpage, options = {}) => {
338
201
  doc.include_css('/css/app.css');
339
202
  ctrl.active();
340
203
  doc.body.add(ctrl);
341
-
342
- // Getting it to load the JS would be nice...
343
- // But maybe only do it automatically if it's an active document.
344
- // If it's just generated JS then best not to bundle JS to load.
345
-
346
- // Could also make smaller bundles for some specific parts of the app. Could improve speed.
347
- //console.log('doc.html', doc.html);
348
204
  const buff_html = Buffer.from(doc.html, "utf-8");
349
-
350
205
  res.push({
351
206
  'path': webpage.path,
352
207
  'value': buff_html,
353
208
  'content-type': 'text/html'
354
209
  });
355
-
356
-
357
210
  const diskpath_js_client = disk_path_client_js || require.resolve('jsgui3-html');
358
-
359
211
  let waiting_for_css_extraction = false, handle_css_extraction_complete = undefined;
360
-
361
-
362
- //const diskpath_js_client = require.resolve('./../controls/page/admin.js');
363
-
364
- // debug mode
365
-
366
-
367
212
  const obs_bundle = bundle_js(diskpath_js_client, {
368
213
  'js_mode': 'debug',
369
214
  'babel': 'debug'
370
215
  });
371
-
372
216
  obs_bundle.on('next', data => {
373
- //console.log('next data', data);
374
217
  console.log('next Object.keys(data)', Object.keys(data));
375
-
376
- // at this point, could go through the js to extract any CSS.
377
-
378
218
  const {lang, operation, compress, type, value} = data;
379
-
380
219
  if (lang === 'JavaScript') {
381
220
  if (type === 'single-string') {
382
221
 
@@ -385,66 +224,37 @@ const bundle_web_page = (webpage, options = {}) => {
385
224
  // Or maybe better they get returned as a 'Bundle' object.
386
225
 
387
226
  const obs_css_from_js = bundle_css_from_js_str(value);
388
- console.log('post obs_css_from_js = bundle_css_from_js_str(value) call');
389
-
227
+ //console.log('post obs_css_from_js = bundle_css_from_js_str(value) call');
390
228
  waiting_for_css_extraction = true;
391
-
392
229
  obs_css_from_js.on('next', data => {
393
230
  console.log('obs_css_from_js next data', data);
394
231
  });
395
232
  obs_css_from_js.on('complete', obs_css_from_js_res => {
396
-
397
-
398
233
  console.log('obs_css_from_js complete obs_css_from_js_res', obs_css_from_js_res);
399
234
 
400
235
  if (tof(obs_css_from_js_res) === 'string') {
401
- // should be added to the page bundle.
402
-
403
236
  const basic_and_app_css = Buffer.concat([o_basic_css.value, Buffer.from(obs_css_from_js_res)]);
404
-
405
237
  res.push({
406
238
  'path': webpage.path + 'css/app.css',
407
239
  'value': basic_and_app_css,
408
240
  'content-type': 'text/css'
409
241
  });
410
-
411
242
  waiting_for_css_extraction = false;
412
- //console.log('obs_css_from_js complete');
413
-
414
243
  if (handle_css_extraction_complete) {
415
244
  handle_css_extraction_complete();
416
245
  } else {
417
246
  console.trace();
418
247
  throw 'stop';
419
248
  }
420
-
421
249
  }
422
-
423
- //console.log('tof(res)', tof(res));
424
- //throw 'stop';
425
-
426
-
427
250
  });
428
-
429
-
430
-
431
-
432
-
433
-
434
251
  }
435
252
  }
436
-
437
253
  })
438
254
 
439
255
  obs_bundle.on('complete', res_bundle_js => {
440
- // the result object is a buffer.
441
- // maybe it should be a Bundle.
442
-
443
- // Should only be considered complete once css extraction from js is complete (incl if nothing gets extracted).
444
-
445
256
  const do_when_complete = () => {
446
257
  console.log('js-bundler bundle is complete');
447
-
448
258
  console.trace();
449
259
  console.log('res_bundle_js', res_bundle_js);
450
260
  console.log('res', res);
@@ -455,7 +265,6 @@ const bundle_web_page = (webpage, options = {}) => {
455
265
  });
456
266
  //throw 'stop';
457
267
  complete(res);
458
-
459
268
  }
460
269
 
461
270
  if (waiting_for_css_extraction) {
@@ -463,72 +272,12 @@ const bundle_web_page = (webpage, options = {}) => {
463
272
  } else {
464
273
  do_when_complete();
465
274
  }
466
-
467
-
468
- //console.log('complete res', res);
469
- //console.trace();
470
- //throw 'stop';
471
-
472
275
  })
473
-
474
-
475
276
  } else {
476
277
  console.trace();
477
278
  console.log('ctrl', ctrl);
478
279
  throw 'NYI';
479
280
  }
480
-
481
- // better to render the HTML document (control) first?
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
- const old = () => {
490
- bundle_js(diskpath_js_client, {
491
- 'js_mode': 'mini',
492
- 'babel': 'mini'
493
- }, (err, res_bundle_js) => {
494
- if (err) {
495
- console.trace();
496
- throw err;
497
- } else {
498
- //console.log('res_bundle_js', res_bundle_js);
499
-
500
- res.push({
501
- 'path': webpage.path + 'js/app.js',
502
- 'value': res_bundle_js,
503
- 'content-type': 'text/javascript'
504
- });
505
-
506
- const doc = new Client_HTML_Document({
507
- context: spc
508
- });
509
- doc.include_js('/js/app.js');
510
- doc.body.add(ctrl);
511
-
512
- // Getting it to load the JS would be nice...
513
- // But maybe only do it automatically if it's an active document.
514
- // If it's just generated JS then best not to bundle JS to load.
515
-
516
- // Could also make smaller bundles for some specific parts of the app. Could improve speed.
517
- //console.log('doc.html', doc.html);
518
- const buff_html = Buffer.from(doc.html, "utf-8");
519
-
520
- res.push({
521
- 'path': webpage.path,
522
- 'value': buff_html,
523
- 'content-type': 'text/html'
524
- });
525
- //console.log('pre complete bundlejs');
526
- //console.log('res.length()', res.length());
527
- complete(res);
528
- }
529
- });
530
- }
531
-
532
281
  }
533
282
 
534
283
  } else if (false) {
@@ -550,11 +299,6 @@ const bundle_web_page = (webpage, options = {}) => {
550
299
  });
551
300
 
552
301
  // The observable could / should return updates along the way, things that contribute to the full result.
553
-
554
-
555
-
556
-
557
-
558
302
  const stop = () => {throw 'NYI'};
559
303
  const pause = () => {throw 'NYI'};
560
304
  const resume = () => {throw 'NYI'};
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "cookies": "^0.8.0",
14
14
  "fnl": "^0.0.23",
15
15
  "fnlfs": "^0.0.27",
16
- "jsgui3-client": "^0.0.73",
16
+ "jsgui3-client": "^0.0.75",
17
17
 
18
18
  "multiparty": "^4.2.3",
19
19
  "ncp": "^2.0.0",
@@ -41,5 +41,5 @@
41
41
  "type": "git",
42
42
  "url": "https://github.com/metabench/jsgui3-server.git"
43
43
  },
44
- "version": "0.0.94"
44
+ "version": "0.0.95"
45
45
  }
package/page-context.js CHANGED
@@ -1,12 +1,9 @@
1
1
  var jsgui = require('jsgui3-html');
2
-
3
2
  // This should be running in node.js
4
-
5
3
  var stringify = jsgui.stringify, each = jsgui.each, arrayify = jsgui.arrayify, tof = jsgui.tof;
6
4
  var filter_map_by_regex = jsgui.filter_map_by_regex;
7
5
  var Class = jsgui.Class, Data_Object = jsgui.Data_Object;
8
6
  var fp = jsgui.fp, is_defined = jsgui.is_defined;
9
-
10
7
  var get_a_sig = jsgui.get_a_sig;
11
8
 
12
9
  // Need to find out what this one requires to put it in its own module.
@@ -35,24 +32,17 @@ class Server_Page_Context extends jsgui.Page_Context {
35
32
  if (spec.server) {
36
33
  this.server = spec.server;
37
34
  }
38
-
39
-
40
35
  this.selection_scope_count = 0;
41
36
  // Perhaps things could be more sandboxed, so that controls don't get access to the resource pool by default.
42
37
  // Maybe only a small number of controls should have access to this.
43
38
  if (spec.pool) {
44
39
  this.pool = spec.pool;
45
40
  }
46
- //if (spec.rendering_mode) {
47
- // this.rendering_mode = spec.rendering_mode;
48
- //}
49
41
  this.rendering_mode = spec.rendering_mode || 'html5';
50
42
  // The item IDs could be handled here... use the local variable closure here.
51
43
  var map_new_ids = {};
52
44
  // and have the objects registered within the context too.
53
-
54
45
  var map_objects = {};
55
-
56
46
  var _get_new_typed_object_id = function(type_name) {
57
47
  if (!is_defined(map_new_ids[type_name])) {
58
48
  map_new_ids[type_name] = 0;
@@ -39,6 +39,15 @@ obs((next, complete, error) => {
39
39
  // Perhaps HTTP_Webpage_Bundler would be an important class to have here.
40
40
  // Considering how it could be interchangable from the publisher if it's a different class.
41
41
 
42
+ // Possibly a Publisher should be called to provide something over HTTP.
43
+ // So the call is routed to a Webpage object, through the Publisher.
44
+ // Publisher could maintain a cache.
45
+ // Maybe the publisher should read the bundle?
46
+ // Maybe the publisher should do the bundling?
47
+ // Perhaps there should be a bit more coordination of the bundling process, possibly accessed once needed.
48
+ //
49
+
50
+
42
51
 
43
52
 
44
53
  class HTTP_Webpage_Publisher extends HTTP_Publisher {
@@ -72,10 +81,19 @@ class HTTP_Webpage_Publisher extends HTTP_Publisher {
72
81
 
73
82
  // Bundling and compiling web content seems like a better thing to get working before serving (or attempting to serve) it.
74
83
  // .build?
84
+ setTimeout(() => {
85
+ this.raise('ready');
86
+ }, 0);
75
87
 
76
88
  }
77
89
  handle_http(req, res) {
78
90
 
91
+ // Is the webpage rendered?
92
+ // Does the webpage require rendering?
93
+ // Seems like the main question to ask here.
94
+
95
+
96
+
79
97
 
80
98
  // returning an observable would make sense.
81
99
  // so other parts of the server could observe the request being processed.
@@ -97,15 +115,38 @@ class HTTP_Webpage_Publisher extends HTTP_Publisher {
97
115
 
98
116
 
99
117
 
100
- console.log('HTTP_Webpage_Publisher handle_http');
118
+ //console.log('HTTP_Webpage_Publisher handle_http');
101
119
  //console.log('req', req);
102
120
 
121
+ const {webpage} = this;
122
+ //console.log('webpage', webpage);
123
+ //console.log('webpage.render()', webpage.render());
124
+ //console.log('Object.keys (webpage)', Object.keys (webpage));
125
+
126
+ //console.log('webpage.content', webpage.content);
127
+
128
+ const Ctrl = webpage.content;
129
+ const ctrl = new Ctrl();
130
+
131
+ //console.log('webpage.content.render()', webpage.content.render());
132
+ //console.log('webpage.content[0]', webpage.content[0]);
133
+
134
+ //res.statusCode = 200;
135
+ //response.setHeader('Content-Type', 'text/html');
136
+ res.writeHead(200, {
137
+ 'Content-Type': 'text/html'
138
+ });
139
+
140
+ res.end(ctrl.all_html_render());
141
+
142
+
143
+
103
144
 
104
145
  // May have bundle already prepared anyway.
105
146
  // Possibly the Website or the Website_Resource could do the bundling / building.
106
147
  // Could even bundle into a ZIP file :)
107
148
 
108
- throw 'NYI';
149
+ //throw 'NYI';
109
150
  }
110
151
  }
111
152