Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update babel monorepo to v7.19.0 #198

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
},
"devDependencies": {
"@babel/cli": "7.18.10",
"@babel/core": "7.18.10",
"@babel/core": "7.19.0",
"@babel/node": "7.18.10",
"@babel/preset-env": "7.18.10",
"@babel/preset-env": "7.19.0",
"@types/node": "16.11.52",
"babel-loader": "8.2.5",
"chalk": "5.0.1",
Expand Down
13 changes: 11 additions & 2 deletions src/aegis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { badUserRoute, reload } = DomainEvents
const { StorageService } = services
const { StorageAdapter } = adapters
const { pathToRegexp, match } = require('path-to-regexp')
const { nanoid } = require('nanoid')
const { AsyncLocalStorage } = require('async_hooks')
const { find, save } = StorageAdapter
const overrides = { find, save, ...StorageService }
const broker = EventBrokerFactory.getInstance()
Expand Down Expand Up @@ -168,12 +170,19 @@ async function handle (path, method, req, res) {
}
}

const newStore = () => nanoid()

const asyncLocalStorage = new AsyncLocalStorage()

const handler = (path, method, req, res) =>
asyncLocalStorage.run(newStore(), handle, path, method, req, res)

/**
* Tell components to clean up any system resources
*/
exports.dispose = async function () {
console.log('system hot-reloading')
await broker.notify(reload, 'system reload')
broker.notify(reload, 'system reload')
}

/**
Expand All @@ -194,5 +203,5 @@ exports.init = async function (remotes) {
// load from storage
await cache.load()
// controllers
return handle
return handler
}
3 changes: 2 additions & 1 deletion src/domain/model-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ const ModelFactory = {
* @param {import('./datasource').default} datasource - persistence/cache
* @returns {Promise<Readonly<Model>>} the model instance
*/
createModel: async function (broker, datasource, modelName, ...args) {
// createModel: async function (broker, datasource, modelName, ...args) {
createModel: function (broker, datasource, modelName, ...args) {
const name = checkModelName(modelName)
const spec = modelFactories.get(name)

Expand Down
72 changes: 37 additions & 35 deletions src/domain/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ const Model = (() => {
return datasource.find(id)
},

/**
* Returns writable stream for upserting the datasource.
* Use in combination with {@link Model}.list(), which will pipe
* its output to it.
*
* @returns {WritableStream}
*/
createWriteStream () {
return datasource.createWriteStream()
},
Expand All @@ -357,32 +364,19 @@ const Model = (() => {
},

/**
* Search existing model instances (asynchronously).
* Searches cache first, then persistent storage if not found.
*
* @param {{key1, keyN}} filter
* @param {{
* writable:WritableStream,
* transform:import('stream').Transform,
* filter:{k:'v'},
* sort:'asc'|'dsc'|*,
* limit:number
* }} options
* @returns {Model[]}
*/
async list ({
filter,
writable = null,
transform = null,
cache = false,
serialize = true,
sort,
limit,
aggregate
}) {
return datasource.list({
filter,
writable,
transform,
cache,
serialize,
sort,
limit,
aggregate
})
async list (options) {
return datasource.list(options)
},

/**
Expand Down Expand Up @@ -483,24 +477,31 @@ const Model = (() => {
* spec: import('./index').ModelSpecification
* }} modelInfo Contains model specification and user input to build a model instance
*/
const Model = async modelInfo =>
Promise.resolve(
// Call factory with data from request payload
modelInfo.spec.factory(...modelInfo.args)
).then(model =>
make({
model,
args: modelInfo.args,
spec: modelInfo.spec
})
)
// const Model = async modelInfo =>
// Promise.resolve(
// // Call factory with data from request payload
// modelInfo.spec.factory(...modelInfo.args)
// ).then(model =>
// make({
// model,
// args: modelInfo.args,
// spec: modelInfo.spec
// })
// )

const Model = modelInfo =>
make({
model: modelInfo.spec.factory(...modelInfo.args),
args: modelInfo.args,
spec: modelInfo.spec
})

const validate = event => model => model[VALIDATE]({}, event)

/**
* Create model instance
*/
const makeModel = asyncPipe(
const makeModel = pipe(
Model,
withTimestamp(CREATETIME),
withSerializers(
Expand Down Expand Up @@ -535,7 +536,8 @@ const Model = (() => {
* }} modelInfo
* @returns {Promise<Readonly<Model>>}
*/
create: async modelInfo => makeModel(modelInfo),
// create: async modelInfo => makeModel(modelInfo),
create: modelInfo => makeModel(modelInfo),

/**
* Load a saved model
Expand Down
6 changes: 4 additions & 2 deletions src/domain/orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export async function generateWorkflow (options) {
ModelFactory.registerModel(workflow)
}

export async function runWorkflow ({ wfName }) {
const model = await ModelFactory.createModel(
export function runWorkflow ({ wfName }) {
// export async function runWorkflow ({ wfName }) {
// const model = await ModelFactory.createModel(
const model = ModelFactory.createModel(
EventBrokerFactory.getInstance(),
DataSourceFactory.getSharedDataSource(wfName),
wfName
Expand Down
7 changes: 5 additions & 2 deletions src/domain/thread-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { Worker, BroadcastChannel } from 'worker_threads'
import domainEvents from './domain-events'
import ModelFactory from '.'
import { performance as perf } from 'perf_hooks'
import { AsyncResource } from 'async_hooks'
import { AsyncResource, AsyncLocalStorage } from 'async_hooks'
import os from 'os'

const { poolOpen, poolClose, poolDrain, poolAbort } = domainEvents
const broker = EventBrokerFactory.getInstance()
const asyncLocalStorage = new AsyncLocalStorage()
const NOJOBS = 'noJobsRunning'
const MAINCHANNEL = 'mainChannel'
const EVENTCHANNEL = 'eventChannel'
Expand Down Expand Up @@ -54,7 +55,6 @@ class Job extends AsyncResource {
this.options = options
this.callback = (error, result) =>
this.runInAsyncScope(options.callback, this, error, result)
//options.callback(error, result)
}
}

Expand Down Expand Up @@ -177,6 +177,7 @@ export class ThreadPool extends EventEmitter {
transfer = []
} = job.options
const startTime = perf.now()
console.log({ kAsyncStore: asyncLocalStorage.getStore() })

this[channel].once('message', result => {
pool.jobTime(perf.now() - startTime)
Expand Down Expand Up @@ -264,6 +265,7 @@ export class ThreadPool extends EventEmitter {
* @returns {Promise<*>} anything that can be cloned
*/
runJob (jobName, jobData, options = {}) {
console.log({ asyncLocalStorage: asyncLocalStorage.getStore() })
return new Promise((resolve, reject) => {
this.jobsRequested++

Expand All @@ -276,6 +278,7 @@ export class ThreadPool extends EventEmitter {
jobName,
jobData,
...options,
asyncStore: asyncLocalStorage.getStore(),
callback: (error, result) => {
if (error) return reject(error)
if (result) return resolve(result)
Expand Down
33 changes: 17 additions & 16 deletions src/domain/use-cases/add-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,26 @@ export default function makeAddModel ({

return result
} else {
const model = await models.createModel(
broker,
repository,
modelName,
input
)
await repository.save(model.getId(), model)

try {
const event = models.createEvent(eventType, modelName, model)
await broker.notify(eventName, event)
// const model = await models.createModel(
const model = models.createModel(broker, repository, modelName, input)
await repository.save(model.getId(), model)

try {
const event = models.createEvent(eventType, modelName, model)
broker.notify(eventName, event)
} catch (error) {
// remote the object if not processed
await repository.delete(model.getId())
broker.emitError(error)
}

// Return the latest changes
return repository.find(model.getId())
} catch (error) {
// remote the object if not processed
await repository.delete(model.getId())
throw error
console.log({ fn: addModel.name, error })
broker.notify('error', error)
}

// Return the latest changes
return repository.find(model.getId())
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/domain/use-cases/create-service-mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ const requiredMethods = ['connect', 'publish', 'subscribe', 'close']
const pluginName = process.env.SERVICE_MESH_PLUGIN || 'webswitch'

export default function makeServiceMesh ({ broker, models, repository }) {
return async function (options) {
const plugin = await models.createModel(
broker,
repository,
pluginName,
options
)
return function (options) {
const plugin = models.createModel(broker, repository, pluginName, options)
const missingMethods = requiredMethods.filter(method => !plugin[method])
const msg = `ServiceMesh plug-in is missing required methods ${missingMethods}`

if (missingMethods.length > 0)
throw new Error(
`ServiceMesh plug-in is missing required methods ${missingMethods}`
)
if (missingMethods.length > 0) {
console.error({ fn: makeServiceMesh.n, msg })
//throw new Error(msg)
}

return plugin
}
Expand Down
9 changes: 5 additions & 4 deletions src/domain/use-cases/invoke-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ export default function makeInvokePort ({
} else {
try {
const { id = null, port, args } = input
const model = typeof(id) === undefined
? await repository.find(id)
: await models.createModel(broker, repository, modelName, args)
const model =
typeof id === undefined
? await repository.find(id)
: models.createModel(broker, repository, modelName, args)

if (!model) throw new Error('no such id')

const callback = model.getPorts()[port].callback || (x => x)
return await model[port](input,callback)
return await model[port](input, callback)
} catch (error) {
return new Error(error)
}
Expand Down
15 changes: 10 additions & 5 deletions src/domain/util/app-error.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict'

class AppError {
class AppError extends Error {
constructor (error) {
this.message = error?.message
this.stack = error?.stack
this.name = error?.name
this.hasError = true
super(error.message)
this.name = error.name
this.stack = error.stack
this.cause = error.cause || 'unknown'
this.time = date.now()
}

toJSON () {
return { ...this, time: new Date(this.time).toUTCString() }
}
}