-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
129 lines (108 loc) · 4.13 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
const p = require('path')
const fs = require('fs')
const Hyperschema = require('hyperschema')
const generateCode = require('./lib/codegen')
const CODE_FILE_NAME = 'index.js'
const MESSAGES_FILE_NAME = 'messages.js'
const DISPATCH_JSON_FILE_NAME = 'dispatch.json'
class HyperdispatchNamespace {
constructor (hyperdispatch, name) {
this.hyperdispatch = hyperdispatch
this.name = name
}
register (description) {
const fqn = '@' + this.name + '/' + description.name
this.hyperdispatch.register(fqn, description)
}
}
module.exports = class Hyperdispatch {
constructor (schema, dispatchJson, { offset, dispatchDir = null, schemaDir = null } = {}) {
this.schema = schema
this.version = dispatchJson ? dispatchJson.version : 0
this.offset = dispatchJson ? dispatchJson.offset : (offset || 0)
this.dispatchDir = dispatchDir
this.schemaDir = schemaDir
this.namespaces = new Map()
this.handlersByName = new Map()
this.handlersById = new Map()
this.handlers = []
this.currentOffset = this.offset || 0
this.changed = false
this.initializing = true
if (dispatchJson) {
for (let i = 0; i < dispatchJson.schema.length; i++) {
const description = dispatchJson.schema[i]
this.register(description.name, description)
}
}
this.initializing = false
}
namespace (name) {
return new HyperdispatchNamespace(this, name)
}
register (fqn, description) {
const existingByName = this.handlersByName.get(fqn)
const existingById = Number.isInteger(description.id) ? this.handlersById.get(description.id) : null
if (existingByName && existingById) {
if (existingByName !== existingById) throw new Error('ID/Name mismatch for handler: ' + fqn)
if (Number.isInteger(description.id) && (existingByName.id !== description.id)) {
throw new Error('Cannot change the assigned ID for handler: ' + fqn)
}
}
const type = this.schema.resolve(description.requestType)
if (!type) throw new Error('Invalid request type')
if (existingByName && (existingByName.type !== type)) {
throw new Error('Cannot alter the request type for a handler')
}
if (!this.initializing && !existingByName && !this.changed) {
this.changed = true
this.version += 1
}
const id = Number.isInteger(description.id) ? description.id : this.currentOffset++
const handler = {
id,
type,
name: fqn,
requestType: description.requestType,
version: Number.isInteger(description.version) ? description.version : this.version
}
this.handlersById.set(id, handler)
this.handlersByName.set(fqn, handler)
if (!existingByName) {
this.handlers.push(handler)
}
}
toJSON () {
return {
version: this.version,
schema: this.handlers.map(({ type, ...h }) => h)
}
}
static from (schemaJson, dispatchJson, opts) {
const schema = Hyperschema.from(schemaJson)
if (typeof dispatchJson === 'string') {
const jsonFilePath = p.join(p.resolve(dispatchJson), DISPATCH_JSON_FILE_NAME)
let exists = false
try {
fs.statSync(jsonFilePath)
exists = true
} catch (err) {
if (err.code !== 'ENOENT') throw err
}
opts = { ...opts, dispatchDir: dispatchJson, schemaDir: schemaJson }
if (exists) return new this(schema, JSON.parse(fs.readFileSync(jsonFilePath)), opts)
return new this(schema, null, opts)
}
return new this(schema, dispatchJson, opts)
}
static toDisk (hyperdispatch, dispatchDir) {
if (!dispatchDir) dispatchDir = hyperdispatch.dispatchDir
fs.mkdirSync(dispatchDir, { recursive: true })
const messagesPath = p.join(p.resolve(dispatchDir), MESSAGES_FILE_NAME)
const dispatchJsonPath = p.join(p.resolve(dispatchDir), DISPATCH_JSON_FILE_NAME)
const codePath = p.join(p.resolve(dispatchDir), CODE_FILE_NAME)
fs.writeFileSync(dispatchJsonPath, JSON.stringify(hyperdispatch.toJSON(), null, 2), { encoding: 'utf-8' })
fs.writeFileSync(messagesPath, hyperdispatch.schema.toCode(), { encoding: 'utf-8' })
fs.writeFileSync(codePath, generateCode(hyperdispatch), { encoding: 'utf-8' })
}
}