@webqit/webflo 0.8.45 → 0.8.49

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 (38) hide show
  1. package/docker/Dockerfile +25 -0
  2. package/docker/README.md +69 -0
  3. package/package.json +9 -5
  4. package/src/cmd/client.js +68 -97
  5. package/src/cmd/origins.js +2 -2
  6. package/src/modules/Router.js +130 -0
  7. package/src/modules/_FormData.js +60 -0
  8. package/src/modules/_Headers.js +88 -0
  9. package/src/modules/_MessageStream.js +191 -0
  10. package/src/modules/_NavigationEvent.js +89 -0
  11. package/src/modules/_Request.js +61 -0
  12. package/src/modules/_RequestHeaders.js +72 -0
  13. package/src/modules/_Response.js +56 -0
  14. package/src/modules/_ResponseHeaders.js +81 -0
  15. package/src/modules/_URL.js +111 -0
  16. package/src/modules/client/Cache.js +38 -0
  17. package/src/modules/client/Client.js +51 -22
  18. package/src/modules/client/Http.js +26 -11
  19. package/src/modules/client/NavigationEvent.js +20 -0
  20. package/src/modules/client/Router.js +30 -104
  21. package/src/modules/client/StdRequest.js +34 -33
  22. package/src/modules/client/Storage.js +56 -0
  23. package/src/modules/client/Url.js +1 -1
  24. package/src/modules/client/Worker.js +74 -68
  25. package/src/modules/client/WorkerClient.js +102 -0
  26. package/src/modules/client/WorkerComm.js +183 -0
  27. package/src/modules/client/effects/sounds.js +64 -0
  28. package/src/modules/server/NavigationEvent.js +38 -0
  29. package/src/modules/server/Router.js +53 -124
  30. package/src/modules/server/Server.js +195 -87
  31. package/src/modules/util.js +7 -7
  32. package/src/modules/NavigationEvent.js +0 -32
  33. package/src/modules/Response.js +0 -98
  34. package/src/modules/XURL.js +0 -125
  35. package/src/modules/client/ClientNavigationEvent.js +0 -22
  36. package/src/modules/client/Push.js +0 -84
  37. package/src/modules/server/ServerNavigationEvent.js +0 -23
  38. package/src/modules/server/StdIncomingMessage.js +0 -70
@@ -0,0 +1,191 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import { _isArray, _isEmpty, _isNumber, _isObject, _isPlainArray, _isPlainObject, _isString } from '@webqit/util/js/index.js';
6
+ import { formDataType } from './_FormData.js';
7
+
8
+ /**
9
+ * The _Request Mixin
10
+ */
11
+ const _MessageStream = (NativeMessageStream, Headers, FormData) => {
12
+ const MessageStream = class extends NativeMessageStream {
13
+
14
+ constructor(input, init = {}) {
15
+ var _proxy = {}, _meta = {};
16
+ if ((('headers' in init) && !(init instanceof Headers)) || ('_proxy' in init) || ('meta' in init)) {
17
+ init = { ...init };
18
+ if (('headers' in init) && !(init instanceof Headers)) {
19
+ init.headers = new Headers(init.headers);
20
+ }
21
+ if (('_proxy' in init)) {
22
+ _proxy = init._proxy;
23
+ delete init._proxy;
24
+ }
25
+ if (('meta' in init)) {
26
+ _meta = init.meta;
27
+ delete init.meta;
28
+ }
29
+ arguments[1] = init;
30
+ }
31
+ super(...arguments);
32
+ this._proxy = _proxy;
33
+ this._meta = _meta;
34
+ }
35
+
36
+ clone() {
37
+ const clone = new this.constructor(super.clone());
38
+ clone._proxy = this._proxy;
39
+ clone._headers = this._headers;
40
+ clone._typedDataCache = this._typedDataCache;
41
+ clone._meta = this._meta;
42
+ return clone;
43
+ }
44
+
45
+ get url() {
46
+ return 'url' in this._proxy ? this._proxy.url : super.url;
47
+ }
48
+
49
+ get headers() {
50
+ if (!this._headers) {
51
+ this._headers = new Headers(super.headers);
52
+ }
53
+ return this._headers;
54
+ }
55
+
56
+ get original() {
57
+ return this._typedDataCache.original;
58
+ }
59
+
60
+ get originalType() {
61
+ return this._typedDataCache.originalType;
62
+ }
63
+
64
+ get meta() {
65
+ return this._meta || {};
66
+ }
67
+
68
+ async arrayBuffer() {
69
+ if (this._typedDataCache.arrayBuffer) {
70
+ return this._typedDataCache.arrayBuffer;
71
+ }
72
+ return super.arrayBuffer();
73
+ }
74
+
75
+ async blob() {
76
+ if (this._typedDataCache.blob) {
77
+ return this._typedDataCache.blob;
78
+ }
79
+ return super.blob();
80
+ }
81
+
82
+ async formData() {
83
+ if (this._typedDataCache.formData) {
84
+ return this._typedDataCache.formData;
85
+ }
86
+ const formData = await super.formData();
87
+ formData.tee = FormData.prototype.tee.bind(formData);
88
+ formData.json = FormData.prototype.json.bind(formData);
89
+ return formData;
90
+ }
91
+
92
+ async json() {
93
+ if (this._typedDataCache.json) {
94
+ return this._typedDataCache.json;
95
+ }
96
+ return super.json();
97
+ }
98
+
99
+ async text() {
100
+ if (this._typedDataCache.text) {
101
+ return this._typedDataCache.text;
102
+ }
103
+ return super.text();
104
+ }
105
+
106
+ // Payload
107
+ jsonBuild(force = false) {
108
+ if (!this._typedDataCache.jsonBuild || force) {
109
+ this._typedDataCache.jsonBuild = new Promise(async resolve => {
110
+ var request = this, jsonBuild, contentType = request.headers.get('content-type') || '';
111
+ var type = contentType === 'application/json' || this._typedDataCache.json ? 'json' : (
112
+ contentType === 'application/x-www-form-urlencoded' || contentType.startsWith('multipart/') || this._typedDataCache.formData || (!contentType && !['get'].includes((request.method || '').toLowerCase())) ? 'formData' : (
113
+ contentType === 'text/plain' ? 'plain' : 'other'
114
+ )
115
+ );
116
+ if (type === 'formData') {
117
+ jsonBuild = (await request.formData()).json();
118
+ } else {
119
+ jsonBuild = type === 'json' ? await request.json() : (
120
+ type === 'plain' ? await request.text() : request.body
121
+ );
122
+ }
123
+ resolve(jsonBuild);
124
+ });
125
+ }
126
+ return this._typedDataCache.jsonBuild;
127
+ }
128
+
129
+ };
130
+ // ----------
131
+ MessageStream.Headers = Headers;
132
+ // ----------
133
+ return MessageStream;
134
+ }
135
+
136
+ export default _MessageStream;
137
+
138
+ export function encodeBody(body, globals) {
139
+ const detailsObj = { body, original: body };
140
+ if (_isString(body) || _isNumber(body)) {
141
+ detailsObj.originalType = 'text';
142
+ detailsObj.text = body;
143
+ detailsObj.headers = {
144
+ contentLength: (body + '').length,
145
+ };
146
+ return detailsObj;
147
+ }
148
+ detailsObj.originalType = formDataType(body);
149
+ if ([ 'Blob', 'File' ].includes(detailsObj.originalType)) {
150
+ detailsObj.blob = body;
151
+ detailsObj.headers = {
152
+ contentType: body.type,
153
+ contentLength: body.size,
154
+ };
155
+ } else if ([ 'Uint8Array', 'Uint16Array', 'Uint32Array', 'ArrayBuffer' ].includes(detailsObj.originalType)) {
156
+ detailsObj.arrayBuffer = body;
157
+ detailsObj.headers = {
158
+ contentLength: body.byteLength,
159
+ };
160
+ } else if (detailsObj.originalType === 'FormData') {
161
+ detailsObj.formData = body;
162
+ encodeFormData(detailsObj, body, globals);
163
+ } else if ((_isObject(body) && _isPlainObject(body)) || (_isArray(body) && _isPlainArray(body))) {
164
+ // Deserialize object while detecting if multipart
165
+ var hasBlobs, formData = new globals.FormData;
166
+ formData.json(body, (path, value, objectType) => {
167
+ hasBlobs = hasBlobs || objectType;
168
+ return true;
169
+ });
170
+ if (hasBlobs) {
171
+ detailsObj.formData = formData;
172
+ encodeFormData(detailsObj, formData, globals);
173
+ } else {
174
+ detailsObj.json = body;
175
+ detailsObj.body = JSON.stringify(body);
176
+ detailsObj.headers = {
177
+ contentType: 'application/json',
178
+ contentLength: Buffer.byteLength(detailsObj.body, 'utf8'), // Buffer.from(string).length
179
+ };
180
+ }
181
+ detailsObj.jsonBuild = body;
182
+ }
183
+ return detailsObj;
184
+ }
185
+
186
+ const encodeFormData = (detailsObj, formData, globals) => {
187
+ if (!globals.FormDataEncoder || !globals.ReadableStream) return;
188
+ const encoder = new globals.FormDataEncoder(formData);
189
+ detailsObj.body = globals.ReadableStream.from(encoder.encode());
190
+ detailsObj.headers = encoder.headers;
191
+ };
@@ -0,0 +1,89 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import _URL from './_URL.js';
6
+ import _Request from "./_Request.js";
7
+ import _Response from "./_Response.js";
8
+ import _FormData from "./_FormData.js";
9
+ import { _isEmpty } from '@webqit/util/js/index.js';
10
+
11
+ /**
12
+ * The _NavigationEvent Mixin
13
+ */
14
+ const _NavigationEvent = globals => {
15
+ // ----------
16
+ const URL = _URL(globals.URL);
17
+ const Request = _Request(globals);
18
+ const Response = _Response(globals);
19
+ // ----------
20
+ const NavigationEvent = class {
21
+
22
+ /**
23
+ * Initializes a new NavigationEvent instance.
24
+ *
25
+ * @param Request request
26
+ * @param Object sessionStore
27
+ */
28
+ constructor(_request, _session = null) {
29
+ this._request = _request;
30
+ this._session = _session;
31
+ // -------
32
+ this.URL = URL;
33
+ // -------
34
+ this.Request = Request;
35
+ this.Request.sourceEvent = this;
36
+ // -------
37
+ this.Response = Response;
38
+ this.Response.sourceEvent = this;
39
+ // -------
40
+ this.globals = globals;
41
+ }
42
+
43
+ // url
44
+ get url() {
45
+ if (!this._url) {
46
+ this._url = new URL(this._request.url);
47
+ }
48
+ return this._url;
49
+ }
50
+
51
+ // request
52
+ get request() {
53
+ return this._request;
54
+ }
55
+
56
+ // session
57
+ get session() {
58
+ return this._session;
59
+ }
60
+
61
+ // RDR
62
+ retarget(url, init = {}) {
63
+ var request;
64
+ if (url instanceof NavigationEvent.Request) {
65
+ if (!_isEmpty(init)) {
66
+ request = new NavigationEvent.Request(url, init);
67
+ } else {
68
+ request = url;
69
+ }
70
+ } else {
71
+ init = { _proxy: {}, ...init };
72
+ init._proxy.url = `${this.url.origin}${url}`;
73
+ init._proxy.referrer = this.request.url;
74
+ request = new NavigationEvent.Request(this._request, init);
75
+ }
76
+ return new NavigationEvent(request, this._session);
77
+ }
78
+
79
+ }
80
+ // ----------
81
+ NavigationEvent.URL = URL;
82
+ NavigationEvent.Request = Request;
83
+ NavigationEvent.Response = Response;
84
+ NavigationEvent.globals = globals;
85
+ // ----------
86
+ return NavigationEvent;
87
+ }
88
+
89
+ export default _NavigationEvent;
@@ -0,0 +1,61 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import { _isEmpty } from '@webqit/util/js/index.js';
6
+ import _MessageStream, { encodeBody } from './_MessageStream.js';
7
+ import _RequestHeaders from './_RequestHeaders.js';
8
+
9
+ /**
10
+ * The _Request Mixin
11
+ */
12
+ const _Request = globals => class extends _MessageStream(globals.Request, _RequestHeaders(globals.Headers), globals.FormData) {
13
+
14
+ constructor(input, init = {}) {
15
+ var _typedDataCache = {};
16
+ if (input instanceof globals.Request) {
17
+ init = { ...init };
18
+ init._proxy = { ...(input._proxy || {}), ...(init._proxy || {}) };
19
+ if (input._typedDataCache) {
20
+ _typedDataCache = input._typedDataCache;
21
+ }
22
+ if (init.method && input.method !== init.method.toUpperCase() && [ 'GET', 'HEAD' ].includes(init.method.toUpperCase())) {
23
+ // Body must not inherited. We should now simply copy attributes
24
+ input = input.url;
25
+ [ 'headers', 'mode', 'credentials', 'cache', 'redirect', 'referrer', 'integrity' ].forEach(attr => {
26
+ if (!(attr in init)) {
27
+ init[attr] = input[attr];
28
+ }
29
+ });
30
+ }
31
+ }
32
+ // Init can contain "already-parsed request content"
33
+ if (('body' in init)) {
34
+ init = { ...init };
35
+ if (('body' in init)) {
36
+ _typedDataCache = encodeBody(init.body, globals);
37
+ init.body = _typedDataCache.body;
38
+ }
39
+ }
40
+ if (!_isEmpty(init)) {
41
+ super(input, init);
42
+ } else {
43
+ super(input);
44
+ }
45
+ this._typedDataCache = _typedDataCache;
46
+ if (this._typedDataCache.headers) {
47
+ this.headers.json(this._typedDataCache.headers);
48
+ }
49
+ }
50
+
51
+ get destination() {
52
+ return 'destination' in this._proxy ? this._proxy.destination : super.destination;
53
+ }
54
+
55
+ get referrer() {
56
+ return 'referrer' in this._proxy ? this._proxy.referrer : super.referrer;
57
+ }
58
+
59
+ };
60
+
61
+ export default _Request;
@@ -0,0 +1,72 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import { _after } from "@webqit/util/str/index.js";
6
+ import _Headers from './_Headers.js';
7
+ import { wwwFormUnserialize } from './util.js';
8
+
9
+ /**
10
+ * The _Headers Mixin
11
+ */
12
+ const _RequestHeaders = NativeHeaders => class extends _Headers(NativeHeaders) {
13
+
14
+ set accept(value) {
15
+ return this.set('Accept', value);
16
+ }
17
+
18
+ get accept() {
19
+ const list = (this.get('Accept') || '')
20
+ .split(',').map(a => (a = a.trim().split(';').map(a => a.trim()), [a.shift(), parseFloat((a.pop() || '1').replace('q=', ''))]))
21
+ .sort((a, b) => a[1] > b[1] ? -1 : 1);
22
+ return {
23
+ match(mime) {
24
+ mime = (mime + '').split('/');
25
+ return list.reduce((prev, entry) => prev || (
26
+ (entry = entry[0].split('/')) && [0, 1].every(i => ((mime[i] === entry[i]) || mime[i] === '*' || entry[i] === '*'))
27
+ ), false);
28
+ }
29
+ };
30
+ }
31
+
32
+ set cookies(cookies) {
33
+ this.set('Cookie', cookies);
34
+ return true;
35
+ }
36
+
37
+ get cookies() {
38
+ if (!this._cookies) {
39
+ this._cookies = wwwFormUnserialize(this.get('cookie'), {}, ';');
40
+ }
41
+ return this._cookies;
42
+ }
43
+
44
+ set cors(value) {
45
+ return this.set('Access-Control-Allow-Origin', value === true ? '*' : (value === false ? '' : value));
46
+ }
47
+
48
+ get cors() {
49
+ return this.get('Access-Control-Allow-Origin');
50
+ }
51
+
52
+ set range(value) {
53
+ return this.set('Range', Array.isArray(value) ? `bytes=${value.join('-')}` : value);
54
+ }
55
+
56
+ get range() {
57
+ const value = this.get('Range');
58
+ if (!value) return;
59
+ const range = _after(value, 'bytes=').split('-');
60
+ range[0] = range[0] ? parseInt(range[0], 10) : 0;
61
+ range[1] = range[1] ? parseInt(range[1], 10) : 0;
62
+ range.clamp = max => {
63
+ if (range[1] > max - 1) {
64
+ range[1] = max - 1;
65
+ }
66
+ };
67
+ return range;
68
+ }
69
+
70
+ }
71
+
72
+ export default _RequestHeaders;
@@ -0,0 +1,56 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import { _isEmpty, _isUndefined } from '@webqit/util/js/index.js';
6
+ import _MessageStream, { encodeBody } from './_MessageStream.js';
7
+ import _ResponseHeaders from './_ResponseHeaders.js';
8
+
9
+ /**
10
+ * The _Response Mixin
11
+ */
12
+ const _Response = globals => class extends _MessageStream(globals.Response, _ResponseHeaders(globals.Headers), globals.FormData) {
13
+
14
+ // construct
15
+ constructor(body = null, init = {}) {
16
+ var _proxy = {}, _meta = {}, _typedDataCache = {};
17
+ if (arguments.length) {
18
+ _typedDataCache = encodeBody(body, globals);
19
+ body = _typedDataCache.body;
20
+ }
21
+ // Init can contain "already-parsed request content"
22
+ if (('_proxy' in init) || ('meta' in init)) {
23
+ init = { ...init };
24
+ if (('_proxy' in init)) {
25
+ _proxy = init._proxy;
26
+ delete init._proxy;
27
+ }
28
+ if (('meta' in init)) {
29
+ _meta = init.meta;
30
+ delete init.meta;
31
+ }
32
+ }
33
+ if (!_isEmpty(init)) {
34
+ super(body, init);
35
+ } else {
36
+ super(body);
37
+ }
38
+ this._proxy = _proxy;
39
+ this._meta = _meta;
40
+ this._typedDataCache = _typedDataCache;
41
+ if (this._typedDataCache.headers) {
42
+ this.headers.json(this._typedDataCache.headers);
43
+ }
44
+ }
45
+
46
+ get ok() {
47
+ return 'ok' in this._proxy ? this._proxy.ok : super.ok;
48
+ }
49
+
50
+ get redirected() {
51
+ return 'redirected' in this._proxy ? this._proxy.redirected : super.redirected;
52
+ }
53
+
54
+ };
55
+
56
+ export default _Response;
@@ -0,0 +1,81 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import { _after, _beforeLast } from "@webqit/util/str/index.js";
6
+ import { _isString, _getType, _isObject } from "@webqit/util/js/index.js";
7
+ import _Headers from './_Headers.js';
8
+
9
+ /**
10
+ * The _Headers Mixin
11
+ */
12
+ const _ResponseHeaders = NativeHeaders => class extends _Headers(NativeHeaders) {
13
+
14
+ set contentRange(value) {
15
+ if (Array.isArray(value)) {
16
+ if ((value.length === 2 && !value[0].includes('-')) || value.length < 2) {
17
+ throw new Error(`A Content-Range array must be in the format: [ 'start-end', 'total' ]`);
18
+ }
19
+ return this.set('Content-Range', `bytes ${value.join('/')}`);
20
+ }
21
+ if (!this.has('Accept-Ranges')) {
22
+ this.set('Accept-Ranges', 'bytes');
23
+ }
24
+ return this.set('Content-Range', value);
25
+ }
26
+
27
+ get contentRange() {
28
+ const value = this.get('Content-Range');
29
+ if (!value) return;
30
+ return _after(value, 'bytes ').split('/');
31
+ }
32
+
33
+ set cookies(cookieStr) {
34
+ if (!_isObject(cookieJar)) {
35
+ throw new Error(`The "cookies" response directive does not support the type: ${_getType(cookieStr)}`);
36
+ }
37
+ this.set('Set-Cookie', cookieStr);
38
+ return true;
39
+ }
40
+
41
+ get cookies() {
42
+ return this.get('Set-Cookie');
43
+ }
44
+
45
+ set cors(value) {
46
+ return this.set('Access-Control-Allow-Origin', value === true ? '*' : (value === false ? '' : value));
47
+ }
48
+
49
+ get cors() {
50
+ return this.get('Access-Control-Allow-Origin');
51
+ }
52
+
53
+ set attachment(value) {
54
+ value = value === true ? 'attachment' : (value === false ? 'inline' : value);
55
+ if (!_isString(value)) {
56
+ throw new Error(`The "download" response directive does not support the type: ${_getType(value)}`);
57
+ }
58
+ if (![ 'attachment', 'inline' ].includes(value)) {
59
+ value = `attachment; filename="${value}"`;
60
+ }
61
+ return this.set('Content-Disposition', value);
62
+ }
63
+
64
+ get attachment() {
65
+ var value = (this.get('Content-Disposition') || '').trim();
66
+ value = value === 'attachment' ? true : (
67
+ value === 'inline' ? false : _after(_beforeLast(value, '"'), 'filename="')
68
+ );
69
+ return value;
70
+ }
71
+
72
+ get redirect() {
73
+ return this.get('Location');
74
+ }
75
+
76
+ set redirect(value) {
77
+ return this.set('Location', value);
78
+ }
79
+ }
80
+
81
+ export default _ResponseHeaders;
@@ -0,0 +1,111 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import { Observer } from '@webqit/pseudo-browser/index2.js';
6
+ import _isArray from '@webqit/util/js/isArray.js';
7
+ import _isObject from '@webqit/util/js/isObject.js';
8
+ import { wwwFormUnserialize, wwwFormSerialize } from './util.js';
9
+
10
+ /**
11
+ * ---------------------------
12
+ * The _URL Mixin
13
+ * ---------------------------
14
+ */
15
+ const _URL = NativeURL => {
16
+ const URL = class extends NativeURL {
17
+
18
+ // constructor
19
+ constructor(...args) {
20
+ super(...args);
21
+ var query = wwwFormUnserialize(this.search);
22
+ const updateSearch = query => {
23
+ // "query" was updated. So we update "search"
24
+ var search = wwwFormSerialize(query);
25
+ search = search ? '?' + search : '';
26
+ if (search !== this.search) {
27
+ this.search = search;
28
+ }
29
+ };
30
+ this.__query = {
31
+ value: query,
32
+ proxy: new Proxy(query, {
33
+ set(t, n, v) {
34
+ t[n] = v;
35
+ updateSearch(t);
36
+ return true;
37
+ },
38
+ deleteProperty(t, n) {
39
+ delete t[n];
40
+ updateSearch(t);
41
+ return true;
42
+ }
43
+ })
44
+ };
45
+ }
46
+
47
+ // Set search
48
+ set search(value) {
49
+ super.search = value;
50
+ // "search" was updated. So we update "query"
51
+ var query = wwwFormUnserialize(value);
52
+ if (!_strictEven(query, this.query)) {
53
+ this.query = query;
54
+ }
55
+ }
56
+
57
+ // Get search
58
+ get search() {
59
+ return super.search;
60
+ }
61
+
62
+ // Get query
63
+ get query() {
64
+ return this.__query.proxy;
65
+ }
66
+
67
+ };
68
+ // ----------
69
+ URL.Observable = class extends URL {
70
+
71
+ constructor() {
72
+ super(...arguments);
73
+ Observer.accessorize(this, [
74
+ 'protocol',
75
+ 'username',
76
+ 'password',
77
+ 'host',
78
+ 'hostname',
79
+ 'port',
80
+ 'origin',
81
+ 'pathname',
82
+ 'search',
83
+ 'query',
84
+ 'hash',
85
+ 'href',
86
+ ]);
87
+ }
88
+
89
+ };
90
+ // ----------
91
+ return URL;
92
+ }
93
+
94
+ /**
95
+ * ---------------------------
96
+ * Helpers
97
+ * ---------------------------
98
+ */
99
+ var _strictEven = (a, b) => {
100
+ if (_isObject(a) && _isObject(b)) {
101
+ return _strictEven(Object.keys(a), Object.keys(b))
102
+ && _strictEven(Object.values(a), Object.values(b));
103
+ }
104
+ if (_isArray(a) && _isArray(b)) {
105
+ return a.length === b.length
106
+ && a.reduce((recieved, item, i) => recieved && item === b[i], true);
107
+ }
108
+ return a === b;
109
+ };
110
+
111
+ export default _URL;
@@ -0,0 +1,38 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+
6
+ export function syncToCache(name, data) {
7
+ const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
8
+ return caches.open(name)
9
+ .then(cache => cache.put('http://session.temp', new Response(blob)));
10
+ }
11
+
12
+ export function syncFromCache(name, del = false) {
13
+ return caches.open(name)
14
+ .then(cache => cache.match('http://session.temp')
15
+ .then(response => response ? response.json() : null))
16
+ .then(async data => (del ? await caches.delete(name) : null, data)).catch(e => null);
17
+ }
18
+
19
+ /**
20
+ const sessionData = Object.keys(clientNavigationEvent.session)
21
+ .reduce((obj, key) => (obj[key] = clientNavigationEvent.session[key], obj), {});
22
+ await syncToCache('$session', sessionData);
23
+ */
24
+ /**
25
+ response.finally(async () => {
26
+ // Sync session data from cache that may have been exposed by service-worker routers
27
+ const sessionData = await syncFromCache('$session', true);
28
+ if (!sessionData) return;
29
+ const keysInCache = Object.keys(sessionData);
30
+ _unique(Object.keys(clientNavigationEvent.session).concat(keysInCache)).forEach(key => {
31
+ if (!keysInCache.includes(key)) {
32
+ delete clientNavigationEvent.session[key];
33
+ } else {
34
+ clientNavigationEvent.session[key] = sessionData[key];
35
+ }
36
+ });
37
+ });
38
+ */