-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
652 lines (641 loc) · 18.7 KB
/
index.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
/* auto-generated by NAPI-RS */
const { existsSync, readFileSync } = require('fs')
const { join } = require('path')
const { inspect } = require('util')
const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom')
const { platform, arch } = process
let nativeBinding = null
let localFileExisted = false
let loadError = null
function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
}
} else {
const { glibcVersionRuntime } = process.report.getReport().header
return !glibcVersionRuntime
}
}
switch (platform) {
case 'android':
switch (arch) {
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'teo.android-arm64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./teo.android-arm64.node')
} else {
nativeBinding = require('@teodevgroup/teo-android-arm64')
}
} catch (e) {
loadError = e
}
break
case 'arm':
localFileExisted = existsSync(join(__dirname, 'teo.android-arm-eabi.node'))
try {
if (localFileExisted) {
nativeBinding = require('./teo.android-arm-eabi.node')
} else {
nativeBinding = require('@teodevgroup/teo-android-arm-eabi')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Android ${arch}`)
}
break
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(
join(__dirname, 'teo.win32-x64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.win32-x64-msvc.node')
} else {
nativeBinding = require('@teodevgroup/teo-win32-x64-msvc')
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(
join(__dirname, 'teo.win32-ia32-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.win32-ia32-msvc.node')
} else {
nativeBinding = require('@teodevgroup/teo-win32-ia32-msvc')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'teo.win32-arm64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.win32-arm64-msvc.node')
} else {
nativeBinding = require('@teodevgroup/teo-win32-arm64-msvc')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Windows: ${arch}`)
}
break
case 'darwin':
localFileExisted = existsSync(join(__dirname, 'teo.darwin-universal.node'))
try {
if (localFileExisted) {
nativeBinding = require('./teo.darwin-universal.node')
} else {
nativeBinding = require('@teodevgroup/teo-darwin-universal')
}
break
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'teo.darwin-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./teo.darwin-x64.node')
} else {
nativeBinding = require('@teodevgroup/teo-darwin-x64')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'teo.darwin-arm64.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.darwin-arm64.node')
} else {
nativeBinding = require('@teodevgroup/teo-darwin-arm64')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on macOS: ${arch}`)
}
break
case 'freebsd':
if (arch !== 'x64') {
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
}
localFileExisted = existsSync(join(__dirname, 'teo.freebsd-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./teo.freebsd-x64.node')
} else {
nativeBinding = require('@teodevgroup/teo-freebsd-x64')
}
} catch (e) {
loadError = e
}
break
case 'linux':
switch (arch) {
case 'x64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'teo.linux-x64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-x64-musl.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-x64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'teo.linux-x64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-x64-gnu.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-x64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'teo.linux-arm64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-arm64-musl.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-arm64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'teo.linux-arm64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-arm64-gnu.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-arm64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'teo.linux-arm-musleabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-arm-musleabihf.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-arm-musleabihf')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'teo.linux-arm-gnueabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
}
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'teo.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-riscv64-musl.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'teo.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-riscv64-gnu.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
localFileExisted = existsSync(
join(__dirname, 'teo.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./teo.linux-s390x-gnu.node')
} else {
nativeBinding = require('@teodevgroup/teo-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
break
default:
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}
if (!nativeBinding) {
if (loadError) {
throw loadError
}
throw new Error(`Failed to load native binding`)
}
const { HandlerGroup, Model, Field, Property, Relation, Namespace, DateOnly, ObjectId, File, Range, OptionVariant, InterfaceEnumVariant, App, Expiration, Cookie, Cookies, Headers, HandlerMatch, Request, LocalObjects, LocalValues, Response, EnumMember, Enum, Pipeline, PipelineCtx, TestServer, TestRequest, TestResponse } = nativeBinding
module.exports.HandlerGroup = HandlerGroup
module.exports.Model = Model
module.exports.Field = Field
module.exports.Property = Property
module.exports.Relation = Relation
module.exports.Namespace = Namespace
module.exports.DateOnly = DateOnly
module.exports.ObjectId = ObjectId
module.exports.File = File
module.exports.Range = Range
module.exports.OptionVariant = OptionVariant
module.exports.InterfaceEnumVariant = InterfaceEnumVariant
module.exports.App = App
module.exports.Expiration = Expiration
module.exports.Cookie = Cookie
module.exports.Cookies = Cookies
module.exports.Headers = Headers
module.exports.HandlerMatch = HandlerMatch
module.exports.Request = Request
module.exports.LocalObjects = LocalObjects
module.exports.LocalValues = LocalValues
module.exports.Response = Response
module.exports.EnumMember = EnumMember
module.exports.Enum = Enum
module.exports.Pipeline = Pipeline
module.exports.PipelineCtx = PipelineCtx
module.exports.TestServer = TestServer
module.exports.TestRequest = TestRequest
module.exports.TestResponse = TestResponse
App.prototype.run = async function() {
await this._prepare();
return this._run();
}
HandlerMatch.prototype[customInspectSymbol] = function(_, inspectOptions) {
return "HandlerMatch " + inspect({
"path": this.path,
"handlerName": this.handlerName,
"captures": this.captures,
}, inspectOptions)
}
Request.prototype[customInspectSymbol] = function(_, inspectOptions) {
return "Request " + inspect({
"method": this.method,
"path": this.path,
"query": this.query,
"contentType": this.contentType,
"headers": this.headers,
"bodyObject": this.bodyObject,
"teo": this.teo,
"handlerMatch": this.handlerMatch,
}, inspectOptions)
}
Response.prototype[customInspectSymbol] = function(_, inspectOptions) {
return "Response " + inspect({
"code": this.code(),
"headers": this.headers(),
}, inspectOptions)
}
Headers.prototype[customInspectSymbol] = function(_, inspectOptions) {
let object = {}
for (let k of this.keys()) {
object[k] = this.get(k)
}
return "Headers " + inspect(object, inspectOptions)
}
DateOnly.prototype[customInspectSymbol] = function(_, inspectOptions) {
return this.toString()
}
ObjectId.prototype[customInspectSymbol] = function(_, inspectOptions) {
return "ObjectId(\"" + this.toString() + "\")"
}
Namespace.prototype.definePipelineItem = function(name, creator) {
this._definePipelineItem(name, function(e, args) {
if (e != null) {
throw e
}
const item = creator(args)
return function (e, ctx) {
if (e != null) {
throw e
}
return item(ctx)
}
})
}
Namespace.prototype.defineTransformPipelineItem = Namespace.prototype.definePipelineItem
Namespace.prototype.defineValidatorPipelineItem = function(name, creator) {
this._defineValidatorPipelineItem(name, function(e, args) {
if (e != null) {
throw e
}
const item = creator(args)
return function (e, ctx) {
if (e != null) {
throw e
}
return item(ctx)
}
})
}
Namespace.prototype.defineCallbackPipelineItem = function(name, creator) {
this._defineCallbackPipelineItem(name, function(e, args) {
if (e != null) {
throw e
}
const item = creator(args)
return function (e, ctx) {
if (e != null) {
throw e
}
return item(ctx)
}
})
}
Namespace.prototype.defineComparePipelineItem = function(name, creator) {
this._defineComparePipelineItem(name, function(e, args) {
if (e != null) {
throw e
}
const item = creator(args)
return function (e, oldValue, newValue, ctx) {
if (e != null) {
throw e
}
return item(oldValue, newValue, ctx)
}
})
}
Namespace.prototype.definePipelineItemFunction = function(name, item) {
this.definePipelineItem(name, () => item)
}
Namespace.prototype.defineTransformPipelineItemFunction = Namespace.prototype.definePipelineItemFunction
Namespace.prototype.defineValidatorPipelineItemFunction = function(name, item) {
this.defineValidatorPipelineItem(name, () => item)
}
Namespace.prototype.defineCallbackPipelineItemFunction = function(name, item) {
this.defineCallbackPipelineItem(name, () => item)
}
Namespace.prototype.defineComparePipelineItemFunction = function(name, item) {
this.defineComparePipelineItem(name, () => item)
}
Namespace.prototype.defineHandler = function(name, callback) {
this._defineHandler(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
HandlerGroup.prototype.defineHandler = function(name, callback) {
this._defineHandler(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
TestServer.prototype.setup = async function() {
await this._setup_0()
this._setup_1()
}
Object.defineProperty(Response.prototype, 'text', {
get() {
return this.getText()
}
})
Object.defineProperty(Response.prototype, 'teon', {
get() {
return this.getTeon()
}
})
Object.defineProperty(Response.prototype, 'file', {
get() {
return this.getFile()
}
})
App.prototype.setup = function(callback) {
this._setup(function(e, teo) {
if (e != null) {
throw e
}
return callback(teo)
})
}
App.prototype.program = function(callback) {
this._program(function(e, teo) {
if (e != null) {
throw e
}
return callback(teo)
})
}
Namespace.prototype.defineModelDecorator = function(name, callback) {
this._defineModelDecorator(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
Namespace.prototype.defineModelFieldDecorator = function(name, callback) {
this._defineModelFieldDecorator(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
Namespace.prototype.defineModelRelationDecorator = function(name, callback) {
this._defineModelRelationDecorator(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
Namespace.prototype.defineModelPropertyDecorator = function(name, callback) {
this._defineModelPropertyDecorator(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
Namespace.prototype.defineEnumDecorator = function(name, callback) {
this._defineEnumDecorator(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
Namespace.prototype.defineEnumMemberDecorator = function(name, callback) {
this._defineEnumMemberDecorator(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
Namespace.prototype.defineHandlerGroup = function(name, callback) {
this._defineHandlerGroup(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
Namespace.prototype.defineModelHandlerGroup = function(name, callback) {
this._defineModelHandlerGroup(name, function(e, arg) {
if (e != null) {
throw e
}
return callback(arg)
})
}
Namespace.prototype.defineRequestMiddleware = function(name, creator) {
this._defineRequestMiddleware(name, function(e, args) {
if (e != null) {
throw e
}
const middleware = creator(args)
return async function(e, req, next) {
if (e != null) {
throw e
}
return await middleware(req, next)
}
})
}
Namespace.prototype.defineHandlerMiddleware = function(name, creator) {
this._defineHandlerMiddleware(name, function(e, args) {
if (e != null) {
throw e
}
const middleware = creator(args)
return async function(e, req, next) {
if (e != null) {
throw e
}
return await middleware(req, next)
}
})
}
class TeoError extends Error {
constructor(message, code = 500, errors = null) {
super("")
this.name = "TeoError"
this._code = code
this._errorMessage = message
this._errors = errors
this.message = this.buildMessage()
}
buildMessage() {
return JSON.stringify({code: this.code, message: this.errorMessage, errors: this.errors })
}
set code(newValue) {
this._code = newValue
this.message = this.buildMessage()
}
get code() { return this._code }
set errorMessage(newValue) {
this._errorMessage = newValue
this.message = this.buildMessage()
}
get errorMessage() { return this._errorMessage }
set errors(newValue) {
this._errors = newValue
this.message = this.buildMessage()
}
get errors() { return this._errors }
messagePrefixed(prefix) {
return new TeoError(this.errors ? this.errorMessage : prefix + ': ' + this.errorMessage, this.code, this.errors ? Object.fromEntries(
Object.entries(this.errors).map(([key, value]) => [key, prefix + ": " + value])
) : null)
}
pathPrefixed(prefix) {
return new TeoError(this.errorMessage, this.code, this.errors ? Object.fromEntries(
Object.entries(this.errors).map(([key, value]) => [prefix + "." + key, value])
) : null)
}
mapPath(mapper) {
return new TeoError(this.errorMessage, this.code, this.errors ? Object.fromEntries(
Object.entries(this.errors).map(([key, value]) => [mapper(key), value])
) : null)
}
}
TeoError.notFound = (message = "not found") => new TeoError(message, 404)
TeoError.invalidRequest = (message = "value is invalid") => new TeoError(message, 400)
TeoError.internalServerError = (message = "internal server error") => new TeoError(message, 500)
TeoError.unauthorized = (message = "unauthorized") => new TeoError(message, 401)
module.exports.TeoError = TeoError
globalThis.require = require
process.on('SIGINT', function() { process.exit(0) })