Skip to content

Commit

Permalink
fix: some errors and warning coming from rollup build output (#365)
Browse files Browse the repository at this point in the history
* fix: some errors and warning coming from rollup build output

* fix: do not set FILES_DIR if not already set

* fix: error reporting on data validation

* chores: more minor fixes
  • Loading branch information
matteo-cristino authored Oct 7, 2024
1 parent 3519e6b commit 207e9db
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 150 deletions.
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import { Command, Option } from 'commander';
import 'dotenv/config';
import { statSync } from 'node:fs';
import { Logger } from 'tslog';
import { Logger, type ILogObj } from 'tslog';
import p from '../package.json' with { type: 'json' };
import { Config } from './types';
export const program = new Command();
const L = new Logger({
const L: Logger<ILogObj> = new Logger({
name: p.name,
type: 'pretty',
prettyLogTemplate:
Expand Down
57 changes: 34 additions & 23 deletions src/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export class Directory {
path.endsWith('.metadata.json') ||
path.endsWith('.chain.js')
);
},
ignore: {
// extensions: ['keys']
}
},
ignore: {
// extensions: ['keys']
}
});
}
Expand All @@ -53,31 +53,42 @@ export class Directory {
return this.files.map(({ path }) => path);
}

private getEndpoint (name: string) {
const [path, ext, json] = name.split('.');
if (ext === 'zen') {
return {
path: path,
contract: formatContract(this.getContent(name) || ''),
keys: this.getJSON(path, 'keys'),
conf: this.getContent(path + '.conf') || '',
schema: this.getJSONSchema(path),
metadata: newMetadata(this.getJSON(path, 'metadata') || {})
};
} else if (ext == 'chain' && json == 'js') {
return {
path: path,
chain: this.getContent(name) || '',
schema: this.getJSONSchema(path),
metadata: newMetadata(this.getJSON(path, 'metadata') || {}),
conf: ''
};
}
return;
}

get files() {
const result: Endpoints[] = [];
this.liveDirectory.files.forEach((c, f) => {
const [path, ext, json] = f.split('.');
if (ext === 'zen') {
result.push({
path: path,
contract: formatContract(this.getContent(f)),
keys: this.getJSON(path, 'keys'),
conf: this.getContent(path + '.conf') || '',
schema: this.getJSONSchema(path),
metadata: newMetadata(this.getJSON(path, 'metadata') || {})
});
} else if (ext == 'chain' && json == 'js') {
result.push({
path: path,
chain: this.getContent(f),
schema: this.getJSONSchema(path),
metadata: newMetadata(this.getJSON(path, 'metadata') || {})
});
}
const res = this.getEndpoint(f);
if (res) result.push(res);
});
return result;
}

public endpoint(path: string): Endpoints | undefined {
return this.getEndpoint(path);
}

private getJSON(path: string, type: 'schema' | 'keys' | 'metadata' | 'chain') {
try {
const k = this.getContent(`${path}.${type}.json`);
Expand All @@ -103,11 +114,11 @@ export class Directory {
this.liveDirectory.on(event, cb);
}

public onAdd(cb: (path: string, file: LiveFile) => void) {
public onAdd(cb: (path: string) => void) {
this.onChange('add', cb);
}

public onUpdate(cb: (path: string, file: LiveFile) => void) {
public onUpdate(cb: (path: string) => void) {
this.onChange('update', cb);
}

Expand Down
107 changes: 42 additions & 65 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import dotenv from 'dotenv';
import fs from 'fs';
import mime from 'mime';
import path from 'path';
import {
TemplatedApp,
us_socket_local_port,
LIBUS_LISTEN_EXCLUSIVE_PORT
} from 'uWebSockets.js';
import { TemplatedApp, us_socket_local_port } from 'uWebSockets.js';
import { autorunContracts } from './autorun.js';
import { config } from './cli.js';
import { Directory } from './directory.js';
Expand All @@ -24,10 +20,10 @@ import {
openapiTemplate
} from './openapi.js';
import { SlangroomManager } from './slangroom.js';
import { formatContract } from './fileUtils.js';
import { getSchema, getQueryParams, prettyChain, newMetadata } from './utils.js';
import { forbidden, notFound, unprocessableEntity, internalServerError } from './responseUtils.js';
import { createAppWithBasePath, generateRoute, runPrecondition } from './routeUtils.js';
import { Endpoints, Events } from './types.js';

dotenv.config();

Expand All @@ -36,10 +32,6 @@ const Dir = Directory.getInstance();

const PROM = process.env.PROM == 'true';

if (typeof process.env.FILES_DIR == 'undefined') {
process.env.FILES_DIR = config.zencodeDir;
}

const setupProm = async (app: TemplatedApp) => {
const client = await import('prom-client');
const register = new client.Registry();
Expand All @@ -56,7 +48,8 @@ const setupProm = async (app: TemplatedApp) => {
help: 'Emissions for 1GB',
collect() {
const emissions = swd.perByte(1000000000);
this.set(emissions);
const emissionValue = typeof emissions === 'number' ? emissions : emissions.total;
this.set(emissionValue);
}
});

Expand All @@ -79,7 +72,7 @@ const ncrApp = async () => {
if (!metadata.hidden && !metadata.hideFromOpenapi)
acc.push(`http://${req.getHeader('host')}${config.basepath}${path}`);
return acc;
}, []);
}, [] as string[]);
res
.writeStatus('200 OK')
.writeHeader('Content-Type', 'application/json')
Expand All @@ -90,17 +83,17 @@ const ncrApp = async () => {
})
.get('/oas.json', async (res) => {
definition.paths = {};
const tags = [];
const tags: string[] = [];
await Promise.all(
Dir.files.map(async (endpoints) => {
const { path, metadata } = endpoints;
if (metadata.tags) tags.push(...metadata.tags);
if (definition.paths && !metadata.hidden && !metadata.hideFromOpenapi) {
const prefixedPath = config.basepath + path;
if (metadata.tags) tags.push(...metadata.tags);
if (definition.paths && !metadata.hidden && !metadata.hideFromOpenapi) {
const prefixedPath = config.basepath + path;
const schema = await getSchema(endpoints);
if (schema)
definition.paths[prefixedPath] = generatePath(
endpoints.contract ?? prettyChain(endpoints.chain),
'contract' in endpoints ? endpoints.contract : prettyChain(endpoints.chain),
schema,
metadata
);
Expand All @@ -109,12 +102,15 @@ const ncrApp = async () => {
}
})
);
const customTags = tags.reduce((acc, tag) => {
if (tag === defaultTagsName.zen) return acc;
const t = { name: tag };
if (!acc.includes(t)) acc.push(t);
return acc;
}, []);
const customTags = tags.reduce(
(acc, tag) => {
if (tag === defaultTagsName.zen) return acc;
const t = { name: tag };
if (!acc.includes(t)) acc.push(t);
return acc;
},
[] as { name: string }[]
);
definition.tags = [...customTags, ...defaultTags];
res.cork(() => {
res
Expand Down Expand Up @@ -167,7 +163,7 @@ const generatePublicDirectory = (app: TemplatedApp) => {
res.writeStatus('500').end('Aborted');
});
let url = req.getUrl();
if (url.split('/').pop().startsWith('.')) {
if (url.split('/').pop()?.startsWith('.')) {
notFound(res, L, new Error('Try to access hidden file'));
return;
}
Expand All @@ -182,7 +178,7 @@ const generatePublicDirectory = (app: TemplatedApp) => {
if (fs.existsSync(file + '.metadata.json')) {
let publicMetadata;
try {
publicMetadata = JSON.parse(fs.readFileSync(file + '.metadata.json'));
publicMetadata = JSON.parse(fs.readFileSync(file + '.metadata.json').toString('utf-8'));
} catch (e) {
unprocessableEntity(
res,
Expand All @@ -207,7 +203,7 @@ const generatePublicDirectory = (app: TemplatedApp) => {
.writeStatus('200 OK')
.writeHeader('Access-Control-Allow-Origin', '*')
.writeHeader('Content-Type', contentType)
.end(fs.readFileSync(file));
.end(fs.readFileSync(file).toString('utf-8'));
});
} else {
notFound(res, L, new Error(`File not found: ${file}`));
Expand All @@ -216,44 +212,25 @@ const generatePublicDirectory = (app: TemplatedApp) => {
}
};

const generateEndpoint = (basePath: string): Endpoints | undefined => {
if (Dir.getContent(basePath + '.zen') !== undefined) {
return {
path: basePath,
contract: formatContract(Dir.getContent(basePath + '.zen')),
keys: Dir.getJSON(basePath, 'keys'),
conf: Dir.getContent(basePath + '.conf') || '',
schema: Dir.getJSONSchema(basePath),
metadata: newMetadata(Dir.getJSON(basePath, 'metadata') || {})
};
} else if (Dir.getContent(basePath + '.chain.js') !== undefined) {
return {
path: basePath,
chain: Dir.getContent(basePath + '.chain.js'),
schema: Dir.getJSONSchema(basePath),
metadata: newMetadata(Dir.getJSON(basePath, 'metadata') || {})
};
}
return;
};

Dir.ready(async () => {
const app = await ncrApp();

await autorunContracts();

await Promise.all(
Dir.files.map(async (endpoint) => {
await generateRoute(app, endpoint, 'add');
await generateRoute(app, endpoint, Events.Add);
})
);

generatePublicDirectory(app);

app.listen(config.port, LIBUS_LISTEN_EXCLUSIVE_PORT, (socket) => {
const listenOption = 1;
app.listen(config.port, listenOption, (socket) => {
if (socket) {
const port = us_socket_local_port(socket);
L.info(`Swagger UI is running on http://${config.hostname}:${port}${config.basepath}${config.openapiPath}`);
L.info(
`Swagger UI is running on http://${config.hostname}:${port}${config.basepath}${config.openapiPath}`
);
} else {
L.error('Port already in use ' + config.port);
throw new Error('Port already in use ' + config.port);
Expand All @@ -262,39 +239,39 @@ Dir.ready(async () => {

Dir.onAdd(async (path: string) => {
const [baseName, ext, json] = path.split('.');
const endpoint = generateEndpoint(baseName);
const endpoint = Dir.endpoint(baseName);
if (!endpoint) return;
let event: string;
let event: Events;
if (ext === 'zen' || (ext === 'chain' && json === 'js')) {
event = 'add';
event = Events.Add;
} else {
event = 'update';
event = Events.Update;
}
await generateRoute(app, endpoint, event);
});

Dir.onUpdate(async (path: string) => {
const endpoint = generateEndpoint(path.split('.')[0]);
const endpoint = Dir.endpoint(path.split('.')[0]);
if (!endpoint) return;
await generateRoute(app, endpoint, 'update');
await generateRoute(app, endpoint, Events.Update);
});

Dir.onDelete(async (path: string) => {
const [baseName, ext, json] = path.split('.');
let endpoint: Endpoints;
let event: string;
let endpoint: Endpoints | undefined;
let event: Events;
if (ext === 'zen' || (ext === 'chain' && json === 'js')) {
endpoint = {
path: baseName,
contract: null,
chain: null,
contract: '',
chain: '',
conf: '',
metadata: {}
metadata: newMetadata({})
};
event = 'delete';
event = Events.Delete;
} else {
endpoint = generateEndpoint(baseName);
event = 'update';
endpoint = Dir.endpoint(baseName);
event = Events.Update;
}
if (!endpoint) return;
await generateRoute(app, endpoint, event);
Expand Down
Loading

0 comments on commit 207e9db

Please sign in to comment.