-
Notifications
You must be signed in to change notification settings - Fork 0
/
pico.js
288 lines (253 loc) · 7.31 KB
/
pico.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require('isomorphic-fetch')
require('es6-promise').polyfill()
var EventSource = require('eventsource')
var FormData = require('form-data')
var fetch = global.fetch
var Request = global.Request
var modules = {}
var json = {
dumps: function (obj) {
return JSON.stringify(obj, function (k, v) {
return v && typeof v === 'object' && typeof v.json === 'string' ? JSON.parse(v.json) : v
})
},
loads: JSON.parse
}
function urlencode (params) {
return Object.keys(params).map(function (k) {
return k + '=' + encodeURIComponent(params[k])
}).join('&')
}
/**
* Returns true of the object is a File or FileList
* else false
*/
function isFileOrFilelist (obj) {
return global.File && global.FileList && (obj instanceof global.File || obj instanceof global.FileList)
}
/**
* Creates a namespace from a point-seperated string.
*/
function createNamespace (ns, root) {
var m = root || global
var parts = ns.split('.')
var level
for (var i = 0, l = parts.length; i < l; i++) {
level = parts[i]
m = (m[level] = m[level] || {})
}
return m
}
function createFunctionProxy (definition, functionName, obj) {
var args = definition.args.map(function (arg) { return arg.name })
var proxy = function () {
var argsDict = {}
var argValues = arguments
args.forEach(function (arg, i) {
argsDict[arg] = argValues[i]
})
var f = definition.stream ? stream : callFunctionWithArgs
return f(obj, functionName, argsDict)
}
proxy.submitFormData = function (form) {
return callFunction(obj, functionName, form)
}
proxy.submitJSON = function (data) {
return callFunction(obj, functionName, json.dumps(data))
}
proxy.url = definition.url
// generate doc string
proxy.__doc__ = 'function(' + args.join(', ') + ')'
if (definition.doc) {
proxy.__doc__ = [proxy.__doc__, definition.doc].join('\n')
}
proxy.toString = function () {
return this.__doc__
}
return proxy
}
function createModuleProxy (definition, moduleName, alias) {
if (alias === undefined) {
alias = moduleName
}
var ns = createNamespace(alias, {})
for (var k in ns) {
delete ns[k]
}
ns.__name__ = definition.name
ns.__alias__ = alias
ns.__url__ = definition.url
ns.__doc__ = definition.doc
ns._request_hooks = {}
for (var i = 0; i < definition.functions.length; i++) {
var funcDef = definition.functions[i]
var funcName = funcDef.name
ns[funcName] = createFunctionProxy(funcDef, funcName, ns)
}
return ns
};
function callFunctionWithArgs (obj, functionName, argsDict) {
var data = new FormData()
Object.keys(argsDict).forEach(function (k) {
var v = isFileOrFilelist(argsDict[k]) ? argsDict[k] : json.dumps(argsDict[k])
data.append(k, v)
})
return callFunction(obj, functionName, data)
}
function callFunction (obj, functionName, data) {
var promise = new Promise(function (resolve, reject) {
var url = obj[functionName].url
var request = new Request(url, {
method: 'POST',
body: data,
headers: {
'Accept': 'application/json'
},
credentials: 'include'
})
if (!(data instanceof FormData)) {
request.headers.set('Content-Type', 'application/json')
}
for (var k in obj._request_hooks) {
var hook = obj._request_hooks[k]
hook(request)
}
fetch(request)
.then(function (response) {
if (response.ok) {
return response
} else {
if (response.headers.get('Content-Type') === 'application/json') {
return response.json().then(function (r) {
var error = new Error(r.message)
error.code = r.code
error.name = r.name
throw error
})
} else {
var error = new Error(response.statusText)
error.code = response.status
throw error
}
}
})
.then(function (response) { return response.json() })
.then(resolve)
.catch(reject)
})
return promise
}
function setRequestHook (obj, name, hook) {
obj._request_hooks[name] = hook
}
function clearRequestHook (obj, name) {
delete obj._request_hooks[name]
}
function stream (obj, functionName, argsDict) {
var url = obj[functionName].url
var stream = {}
stream.buffer = []
stream.max_buffer_size = 100
stream.open = function () {
url = url + '?' + urlencode(argsDict)
stream.socket = new EventSource(url)
stream.socket.addEventListener('message', function (e) {
stream.handler(JSON.parse(e.data))
})
stream.socket.addEventListener('close', function (e) {
stream.closehandler(e.data)
stream.socket.close()
})
stream.socket.addEventListener('error', function (e) {
stream.errorhandler(e.data)
stream.socket.close()
})
return stream
}
stream.close = function () {
stream.socket.close()
stream.closehandler()
}
stream.status = function () {
var states = ['Connecting', 'Open', 'Closed']
return states[stream.socket.readyState]
}
stream.handler = function (message) {
if (stream.buffer.length > stream.max_buffer_size) {
console.warn('Max stream buffer length exceeded. Discarding oldest message.')
stream.buffer.shift()
}
stream.buffer.push(message)
}
stream.errorhandler = function (e) {
console.error(e)
}
stream.closehandler = function () {
}
stream.each = function (handler) {
stream.handler = handler
while (stream.buffer.length) {
handler(stream.buffer.shift())
}
return stream
}
stream.catch = function (handler) {
stream.errorhandler = handler
return stream
}
stream.then = function (handler) {
stream.closehandler = handler
return stream
}
stream.open()
return stream
}
function loadModuleDefinition (definition) {
var module = createModuleProxy(definition, definition.name)
// for imports by absolute url: example.com/blog/api
modules[module.__url__.split('//')[1]] = module
// for imports by dotted name: blog.api
modules[module.__name__.replace('/', '.')] = module
return module
}
function importModule (moduleName) {
if (moduleName.indexOf('/') > -1) {
var parts = moduleName.split('//')
moduleName = parts[parts.length - 1]
}
return modules[moduleName]
}
exports.importModule = importModule
exports.loadAsync = function (moduleUrl) {
var promise = new Promise(function (resolve, reject) {
fetch(moduleUrl)
.then(function (response) { return response.json() })
.then(function (definition) {
resolve(loadModuleDefinition(definition, definition.name))
})
.catch(reject)
})
return promise
}
exports.loadModuleDefinition = loadModuleDefinition
exports.loadAppDefinition = function (definition) {
var app = createNamespace('app')
for (var i = 0; i < definition.modules.length; i++) {
var moduleDef = definition.modules[i]
app[moduleDef.name] = createModuleProxy(moduleDef, moduleDef.name)
}
return app
}
exports.setRequestHook = setRequestHook
exports.clearRequestHook = clearRequestHook
exports.setAuthentication = function (object, username, password) {
var authString = username + ':' + password
var buf = Buffer.from(authString)
var b64 = buf.toString('base64')
setRequestHook(object, 'auth', function (req) {
req.headers.set('Authorization', 'Basic ' + b64)
})
}
exports.clearAuthentication = function (object) {
clearRequestHook(object, 'auth')
}