From 3e6c561f11acd6bc59bcd2fdba8702034827ef65 Mon Sep 17 00:00:00 2001 From: Andrew Osheroff Date: Wed, 11 Sep 2024 16:50:18 +0200 Subject: [PATCH] Rename module to hyperdispatch --- README.md | 2 +- index.js | 56 +++++++++++++++++++++---------------------- lib/codegen.js | 18 +++++++------- package.json | 8 +++---- test/basic.js | 10 ++++---- test/helpers/index.js | 20 ++++++++-------- 6 files changed, 57 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index dcb3cfb..a7c6c27 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# hyperswitch +# hyperdispatch Generate operations/endpoints using Hyperschema diff --git a/index.js b/index.js index 5abb354..1136a3a 100644 --- a/index.js +++ b/index.js @@ -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() @@ -37,9 +37,9 @@ 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) } } @@ -47,7 +47,7 @@ module.exports = class Hyperswitch { } namespace (name) { - return new HyperswitchNamespace(this, name) + return new HyperdispatchNamespace(this, name) } register (fqn, description) { @@ -96,10 +96,10 @@ 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) @@ -107,23 +107,23 @@ module.exports = class Hyperswitch { } 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' }) } } diff --git a/lib/codegen.js b/lib/codegen.js index d3f0573..02999de 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -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' @@ -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' @@ -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` } @@ -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` diff --git a/package.json b/package.json index 42fc2d0..c63e04d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@holepunchto/hyperswitch", + "name": "hyperdispatch", "version": "0.0.0", "description": "Generate operations/endpoints using Hyperschema", "main": "index.js", @@ -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", diff --git a/test/basic.js b/test/basic.js index 1798240..6466dfe 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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({ @@ -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' @@ -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) => { diff --git a/test/helpers/index.js b/test/helpers/index.js index 3db0db3..4810342 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -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 } @@ -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 } @@ -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)