Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
14 changes: 7 additions & 7 deletions .github/workflows/test-node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,8 @@ dist

# example.js spec folder
/spec

package-lock.json

test/test-storage
.vscode
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prettier-config-holepunch
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# hyperdispatch

Generate operations/endpoints using Hyperschema
32 changes: 18 additions & 14 deletions builder.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ const MESSAGES_FILE_NAME = 'messages.js'
const DISPATCH_JSON_FILE_NAME = 'dispatch.json'

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

register (description) {
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 } = {}) {
constructor(schema, dispatchJson, { offset, dispatchDir = null, schemaDir = null } = {}) {
if (dispatchJson && offset && dispatchJson.offset !== offset) {
throw new Error('Cannot change the hyperdispatch offset once it has been defined once')
}
this.schema = schema
this.version = dispatchJson ? dispatchJson.version : 0
this.offset = dispatchJson ? dispatchJson.offset : (offset || 0)
this.offset = dispatchJson ? dispatchJson.offset : offset || 0
this.dispatchDir = dispatchDir
this.schemaDir = schemaDir

Expand All @@ -51,24 +51,26 @@ module.exports = class Hyperdispatch {

static esm = false

namespace (name) {
namespace(name) {
return new HyperdispatchNamespace(this, name)
}

register (fqn, description) {
register(fqn, description) {
const existingByName = this.handlersByName.get(fqn)
const existingById = Number.isInteger(description.id) ? this.handlersById.get(description.id) : null
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)) {
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)) {
if (existingByName && existingByName.type !== type) {
throw new Error('Cannot alter the request type for a handler')
}

Expand All @@ -94,15 +96,15 @@ module.exports = class Hyperdispatch {
}
}

toJSON () {
toJSON() {
return {
version: this.version,
offset: this.offset,
schema: this.handlers.map(({ type, ...h }) => h)
}
}

static from (schemaJson, dispatchJson, opts) {
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)
Expand All @@ -120,11 +122,11 @@ module.exports = class Hyperdispatch {
return new this(schema, dispatchJson, opts)
}

toCode ({ esm = this.constructor.esm, filename } = {}) {
toCode({ esm = this.constructor.esm, filename } = {}) {
return generateCode(this, { esm, filename })
}

static toDisk (hyperdispatch, dispatchDir, opts = {}) {
static toDisk(hyperdispatch, dispatchDir, opts = {}) {
if (typeof dispatchDir === 'object' && dispatchDir) {
opts = dispatchDir
dispatchDir = null
Expand All @@ -139,7 +141,9 @@ module.exports = class Hyperdispatch {
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(dispatchJsonPath, JSON.stringify(hyperdispatch.toJSON(), null, 2), {
encoding: 'utf-8'
})
fs.writeFileSync(messagesPath, hyperdispatch.schema.toCode(opts), { encoding: 'utf-8' })
fs.writeFileSync(codePath, generateCode(hyperdispatch, opts), { encoding: 'utf-8' })
}
Expand Down
18 changes: 9 additions & 9 deletions lib/codegen.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const s = require('generate-string')

module.exports = function generateCode (hyperdispatch, { esm = false }) {
module.exports = function generateCode(hyperdispatch, { esm = false }) {
let str = ''
str += '// This file is autogenerated by the hyperdispatch compiler\n'
str += '/* eslint-disable camelcase */\n'
str += '\n'

if (esm) {
str += 'import { c, b4a, assert } from \'hyperdispatch/runtime\'\n'
str += 'import { version, getEncoding, setVersion } from \'./messages.js\'\n'
str += "import { c, b4a, assert } from 'hyperdispatch/runtime'\n"
str += "import { version, getEncoding, setVersion } from './messages.js'\n"
} else {
str += 'const { c, b4a, assert } = require(\'hyperdispatch/runtime\')\n'
str += 'const { version, getEncoding, setVersion } = require(\'./messages.js\')\n'
str += "const { c, b4a, assert } = require('hyperdispatch/runtime')\n"
str += "const { version, getEncoding, setVersion } = require('./messages.js')\n"
}

str += '\n'
Expand All @@ -35,7 +35,7 @@ module.exports = function generateCode (hyperdispatch, { esm = false }) {
str += ' break\n'
}
str += ' default:\n'
str += ' throw new Error(\'Cannot register a handler for a nonexistent route: \' + name)\n'
str += " throw new Error('Cannot register a handler for a nonexistent route: ' + name)\n"
str += ' }\n'
str += ' this._missing--\n'
str += ' }\n'
Expand All @@ -61,7 +61,7 @@ module.exports = function generateCode (hyperdispatch, { esm = false }) {
str += ` return this._handler${handler.id}(op.value, context)\n`
}
str += ' default:\n'
str += ' throw new Error(\'Handler not found for ID:\' + op.id)\n'
str += " throw new Error('Handler not found for ID:' + op.id)\n"
str += ' }\n'
str += ' }\n'
str += '}\n'
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports = function generateCode (hyperdispatch, { esm = false }) {
str += ` return route${handler.id}\n`
}
str += ' default:\n'
str += ' throw new Error(\'Handler not found for name: \' + name)\n'
str += " throw new Error('Handler not found for name: ' + name)\n"
str += ' }\n'
str += '}\n'
str += '\n'
Expand All @@ -126,7 +126,7 @@ module.exports = function generateCode (hyperdispatch, { esm = false }) {
str += ` return route${handler.id}\n`
}
str += ' default:\n'
str += ' throw new Error(\'Handler not found for ID: \' + id)\n'
str += " throw new Error('Handler not found for ID: ' + id)\n"
str += ' }\n'
str += '}\n'
str += '\n'
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
}
},
"scripts": {
"test": "standard && brittle test/index.js && npm run example:rebuild",
"format": "prettier . --write",
"test": "prettier . --check && brittle test/index.js && npm run example:rebuild",
"example:rebuild": "node example.js && node example.js"
},
"repository": {
Expand All @@ -37,7 +38,8 @@
"homepage": "https://github.com/holepunchto/hyperdispatch#readme",
"devDependencies": {
"brittle": "^3.7.0",
"standard": "^17.1.0",
"prettier": "^3.6.2",
"prettier-config-holepunch": "^2.0.0",
"test-tmp": "^1.3.0",
"which-runtime": "^1.3.0"
},
Expand Down
Loading