Skip to content

Commit

Permalink
Rename module to hyperdispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewosh committed Sep 11, 2024
1 parent 23eab9f commit 3e6c561
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# hyperswitch
# hyperdispatch
Generate operations/endpoints using Hyperschema
56 changes: 28 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ const generateCode = require('./lib/codegen')

const CODE_FILE_NAME = 'index.js'
const MESSAGES_FILE_NAME = 'messages.js'
const SWITCH_JSON_FILE_NAME = 'switch.json'
const DISPATCH_JSON_FILE_NAME = 'dispatch.json'

class HyperswitchNamespace {
constructor (hyperswitch, name) {
this.hyperswitch = hyperswitch
class HyperdispatchNamespace {
constructor (hyperdispatch, name) {
this.hyperdispatch = hyperdispatch
this.name = name
}

register (description) {
const fqn = '@' + this.name + '/' + description.name
this.hyperswitch.register(fqn, description)
this.hyperdispatch.register(fqn, description)
}
}

module.exports = class Hyperswitch {
constructor (schema, switchJson, { offset, switchDir = null, schemaDir = null } = {}) {
module.exports = class Hyperdispatch {
constructor (schema, dispatchJson, { offset, dispatchDir = null, schemaDir = null } = {}) {
this.schema = schema
this.version = switchJson ? switchJson.version : 0
this.offset = switchJson ? switchJson.offset : (offset || 0)
this.switchDir = switchDir
this.version = dispatchJson ? dispatchJson.version : 0
this.offset = dispatchJson ? dispatchJson.offset : (offset || 0)
this.dispatchDir = dispatchDir
this.schemaDir = schemaDir

this.namespaces = new Map()
Expand All @@ -37,17 +37,17 @@ module.exports = class Hyperswitch {

this.changed = false
this.initializing = true
if (switchJson) {
for (let i = 0; i < switchJson.schema.length; i++) {
const description = switchJson.schema[i]
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 HyperswitchNamespace(this, name)
return new HyperdispatchNamespace(this, name)
}

register (fqn, description) {
Expand Down Expand Up @@ -96,34 +96,34 @@ module.exports = class Hyperswitch {
}
}

static from (schemaJson, switchJson) {
static from (schemaJson, dispatchJson) {
const schema = Hyperschema.from(schemaJson)
if (typeof switchJson === 'string') {
const jsonFilePath = p.join(p.resolve(switchJson), SWITCH_JSON_FILE_NAME)
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
}
const opts = { switchDir: switchJson, schemaDir: schemaJson }
const 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, switchJson)
return new this(schema, dispatchJson)
}

static toDisk (hyperswitch, switchDir) {
if (!switchDir) switchDir = hyperswitch.switchDir
fs.mkdirSync(switchDir, { recursive: true })
static toDisk (hyperdispatch, dispatchDir) {
if (!dispatchDir) dispatchDir = hyperdispatch.dispatchDir
fs.mkdirSync(dispatchDir, { recursive: true })

const messagesPath = p.join(p.resolve(switchDir), MESSAGES_FILE_NAME)
const switchJsonPath = p.join(p.resolve(switchDir), SWITCH_JSON_FILE_NAME)
const codePath = p.join(p.resolve(switchDir), CODE_FILE_NAME)
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(switchJsonPath, JSON.stringify(hyperswitch.toJSON(), null, 2), { encoding: 'utf-8' })
fs.writeFileSync(messagesPath, hyperswitch.schema.toCode(), { encoding: 'utf-8' })
fs.writeFileSync(codePath, generateCode(hyperswitch), { encoding: 'utf-8' })
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' })
}
}
18 changes: 9 additions & 9 deletions lib/codegen.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
const s = require('generate-string')

module.exports = function generateCode (hyperswitch) {
module.exports = function generateCode (hyperdispatch) {
let str = ''
str += '// This file is autogenerated by the hyperswitch compiler\n'
str += '// This file is autogenerated by the hyperdispatch compiler\n'
str += '/* eslint-disable camelcase */\n'
str += '\n'
str += 'const { c, b4a, assert } = require(\'hyperswitch/runtime\')\n'
str += 'const { c, b4a, assert } = require(\'hyperdispatch/runtime\')\n'
str += 'const { version, resolveStruct } = require(\'./messages.js\')\n'
str += '\n'

str += 'class Router {\n'
str += ' constructor () {\n'
for (const handler of hyperswitch.handlers) {
for (const handler of hyperdispatch.handlers) {
str += ` this._handler${handler.id} = null\n`
}
str += '\n'
str += ` this._missing = ${hyperswitch.handlers.length}\n`
str += ` this._missing = ${hyperdispatch.handlers.length}\n`
str += ' }\n'
str += '\n'
str += ' add (name, handler) {\n'
str += ' switch (name) {\n'
for (const handler of hyperswitch.handlers) {
for (const handler of hyperdispatch.handlers) {
str += ` case ${s(handler.name)}:\n`
str += ` this._handler${handler.id} = handler\n`
str += ' break\n'
Expand All @@ -32,7 +32,7 @@ module.exports = function generateCode (hyperswitch) {
str += ' }\n'
str += '\n'
str += ' _checkAll () {\n'
for (const handler of hyperswitch.handlers) {
for (const handler of hyperdispatch.handlers) {
str += ` assert(this._handler${handler.id} !== null, 'Missing handler for ${JSON.stringify(handler.name)}')\n`
}
str += ' }\n'
Expand All @@ -46,7 +46,7 @@ module.exports = function generateCode (hyperswitch) {
str += ' const id = c.uint.decode(state)\n'
str += '\n'
str += ' switch (id) {\n'
for (const handler of hyperswitch.handlers) {
for (const handler of hyperdispatch.handlers) {
str += ` case ${handler.id}:\n`
str += ` return this._handler${handler.id}(resolveStruct(${s(handler.requestType)}).decode(state), context)\n`
}
Expand Down Expand Up @@ -76,7 +76,7 @@ module.exports = function generateCode (hyperswitch) {

str += 'function getEncoderAndId (name, opts) {\n'
str += ' switch (name) {\n'
for (const handler of hyperswitch.handlers) {
for (const handler of hyperdispatch.handlers) {
str += ` case ${s(handler.name)}:\n`
str += ' return {\n'
str += ` id: ${handler.id},\n`
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@holepunchto/hyperswitch",
"name": "hyperdispatch",
"version": "0.0.0",
"description": "Generate operations/endpoints using Hyperschema",
"main": "index.js",
Expand All @@ -12,14 +12,14 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/holepunchto/hyperswitch.git"
"url": "git+https://github.com/holepunchto/hyperdispatch.git"
},
"author": "Holepunch",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/holepunchto/hyperswitch/issues"
"url": "https://github.com/holepunchto/hyperdispatch/issues"
},
"homepage": "https://github.com/holepunchto/hyperswitch#readme",
"homepage": "https://github.com/holepunchto/hyperdispatch#readme",
"devDependencies": {
"brittle": "^3.7.0",
"standard": "^17.1.0",
Expand Down
10 changes: 5 additions & 5 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { createTestSchema } = require('./helpers')
test('basic sync switch', async t => {
t.plan(6)

const hs = await createTestSchema(t)
hs.rebuild({
const hd = await createTestSchema(t)
hd.rebuild({
schema: schema => {
const ns = schema.namespace('test')
ns.register({
Expand All @@ -23,8 +23,8 @@ test('basic sync switch', async t => {
]
})
},
switch: hyperswitch => {
const ns = hyperswitch.namespace('test')
dispatch: hyperdispatch => {
const ns = hyperdispatch.namespace('test')
ns.register({
name: 'test-request-1',
requestType: '@test/request'
Expand All @@ -35,7 +35,7 @@ test('basic sync switch', async t => {
})
}
})
const { dispatch, Router } = hs.module
const { dispatch, Router } = hd.module

const r = new Router()
r.add('@test/test-request-1', (req, ctx) => {
Expand Down
20 changes: 10 additions & 10 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const fs = require('fs')
const tmp = require('test-tmp')

const Hyperschema = require('hyperschema')
const Hyperswitch = require('../..')
const Hyperdispatch = require('../..')

class TestBuilder {
constructor (dir) {
this.dir = dir
this.schemaDir = p.join(dir, 'hyperschema')
this.switchDir = p.join(dir, 'hyperswitch')
this.dispatchDir = p.join(dir, 'hyperdispatch')
this.module = null
this.version = 0
}
Expand All @@ -18,17 +18,17 @@ class TestBuilder {
const schema = Hyperschema.from(this.schemaDir)
builder.schema(schema)
Hyperschema.toDisk(schema)
const hyperswitch = Hyperswitch.from(this.schemaDir, this.switchDir)
builder.switch(hyperswitch)
Hyperswitch.toDisk(hyperswitch)
const hyperdispatch = Hyperdispatch.from(this.schemaDir, this.dispatchDir)
builder.dispatch(hyperdispatch)
Hyperdispatch.toDisk(hyperdispatch)

if (this.module) {
delete require.cache[require.resolve(this.switchDir)]
delete require.cache[require.resolve(p.join(this.switchDir, 'switch.json'))]
delete require.cache[require.resolve(this.dispatchDir)]
delete require.cache[require.resolve(p.join(this.dispatchDir, 'dispatch.json'))]
}

this.module = require(this.switchDir)
this.json = require(p.join(this.switchDir, 'switch.json'))
this.module = require(this.dispatchDir)
this.json = require(p.join(this.dispatchDir, 'dispatch.json'))

return schema
}
Expand All @@ -38,7 +38,7 @@ async function createTestSchema (t) {
const dir = await tmp(t, { dir: p.join(__dirname, '../test-storage') })

// Copy the runtime into the tmp dir so that we don't need to override it in the codegen
const runtimePath = p.join(dir, 'node_modules', 'hyperswitch', 'runtime.js')
const runtimePath = p.join(dir, 'node_modules', 'hyperdispatch', 'runtime.js')
await fs.promises.mkdir(p.dirname(runtimePath), { recursive: true })
await fs.promises.copyFile(p.resolve(dir, '../../../runtime.js'), runtimePath)

Expand Down

0 comments on commit 3e6c561

Please sign in to comment.