-
Notifications
You must be signed in to change notification settings - Fork 5
/
seneca-browser-src.js
181 lines (153 loc) · 5.16 KB
/
seneca-browser-src.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Copyright (c) Richard Rodger 2019-2024, MIT license. */
require('util.promisify/shim')()
let Timers = require('timers')
let SenecaModule = require('seneca4')
// let SenecaPromisify = require('seneca-promisify')
global.setImmediate = global.setImmediate || Timers.setImmediate
let SenecaExport = function (options, more_options) {
options = options || {}
options.legacy = options.legacy || false
let seneca = SenecaModule(options, more_options)
// seneca.use(SenecaPromisify)
seneca.use({
name: 'browser',
init: function browser(options) {
// endpoint:
// - string: endpoint string
// - function(msg, config, meta): returns endpoint string, can modify config
options.endpoint = options.endpoint || '/seneca'
options.fetch = options.fetch || {}
options.headers = options.headers || {}
this.add('role:transport,hook:client,type:browser', hook_client_browser)
let tu = this.export('transport/utils')
let pathMapper
// { 'a:1,b:2': { endpoint, suffix, prefix } }
if ('object' === typeof options.pathmap) {
pathMapper = seneca.util.Patrun({ gex: true })
Object.entries(options.pathmap).forEach((entry) => {
pathMapper.add(seneca.util.Jsonic(entry[0]), entry[1])
})
if (options.debug) {
console.log('SENECA', 'pathmap', '' + pathMapper)
}
}
function hook_client_browser(msg, reply) {
let seneca = this
reply({
send: async function (msg, reply, meta) {
let config = {
// credentials: 'same-origin',
method: 'post',
...options.fetch,
mode: 'cors',
cache: 'no-cache',
headers: await resolveHeaders({
'Content-Type': 'application/json',
...options.fetch.headers,
...options.headers,
}),
body: tu.stringifyJSON(tu.externalize_msg(seneca, msg, meta)),
}
let endpoint = options.endpoint
if ('function' === typeof endpoint) {
endpoint = endpoint.call(seneca, msg, config, meta)
} else if (pathMapper) {
let spec = pathMapper.find(msg)
if (spec) {
endpoint =
null != spec.endpoint
? spec.endpoint
: (null == spec.prefix
? ''
: 'function' === typeof spec.prefix
? spec.prefix.call(seneca, msg, config, meta)
: spec.prefix) +
endpoint +
(null == spec.suffix
? ''
: 'function' === typeof spec.suffix
? spec.suffix.call(seneca, msg, config, meta)
: spec.suffix)
}
}
fetch(endpoint, config)
.then(function (response) {
try {
let json = response.json()
return json
} catch (e) {
e.message =
response.status +
': ' +
response.statusText +
': ' +
e.message
return reply(e)
}
})
.then(function (json) {
// FIX: seneca.reply broken in browser
// TODO: transport should handle this
if (Array.isArray(json)) {
json.meta$ = { id: 'ID' }
}
let rep = tu.internalize_reply(seneca, json)
reply(rep.err, rep.out, rep.meta)
})
},
})
}
},
})
async function resolveHeaders(headers) {
let names = Object.keys(headers)
for (let h of names) {
let v = headers[h]
if ('function' === typeof v) {
headers[h] = await v()
} else {
headers[h] = v
}
}
return headers
}
seneca.root.order.inward.add({
name: 'debounce',
before: 'inward_msg_modify',
exec: function (spec) {
let msg = spec.data.msg
if (msg.debounce$) {
let log = spec.ctx.seneca.status().history.log
// TODO: just using pattern is not sufficient - conflates
// unrelated messages - need to check params too...
let actdef = spec.ctx.seneca.find(msg)
if (!actdef) return null
for (let i = log.length - 1; -1 < i; i--) {
if (
log[i].meta.pattern === actdef.pattern &&
// Only drop if there's a previous inflight
0 === log[i].result.length
) {
return {
// TODO: need a `drop` operation
op: 'stop',
out: {
kind: 'result',
result: {},
},
}
}
}
}
return null
},
})
return seneca
}
SenecaExport.util = SenecaModule.util
SenecaExport.valid = SenecaModule.valid
SenecaExport.prototype = SenecaModule.prototype
SenecaExport.browser = {
version: '7.1.0',
}
module.exports = SenecaExport