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

chore(api): upgrade to hapi 21 #276

Merged
merged 7 commits into from
Oct 11, 2023
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
16 changes: 6 additions & 10 deletions packages/api-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@
"test:file": "uvu -r tsm source"
},
"dependencies": {
"@hapi/boom": "9.1.4",
"@hapi/hapi": "20.1.5",
"@hapi/hoek": "9.2.0",
"@hapi/boom": "10.0.1",
"@hapi/hapi": "21.3.2",
"@hapi/hoek": "11.0.2",
"@mainsail/api-database": "workspace:*",
"@mainsail/container": "workspace:*",
"@mainsail/contracts": "workspace:*",
"@mainsail/kernel": "workspace:*",
"@mainsail/utils": "workspace:*",
"joi": "17.9.2",
"joi": "17.11.0",
"nanomatch": "1.2.13",
"rate-limiter-flexible": "1.3.2",
"semver": "6.3.0"
"rate-limiter-flexible": "1.3.2"
},
"devDependencies": {
"@types/hapi__boom": "7.4.1",
"@types/hapi__hapi": "21.0.0",
"@types/hapi__joi": "17.1.7",
"@types/ip": "1.1.0",
oXtxNt9U marked this conversation as resolved.
Show resolved Hide resolved
"@types/semver": "6.2.3",
"@types/semver": "7.5.3",
"uvu": "^0.5.6"
},
"engines": {
Expand Down
15 changes: 15 additions & 0 deletions packages/api-common/source/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import Hapi from "@hapi/hapi";
import { Contracts } from "@mainsail/contracts";

export type ApiServer = Hapi.Server<ServerState>;

export enum ServerType {
Http = "HTTP",
Https = "HTTPS",
}

export interface ServerState {
app: Contracts.Kernel.Application;
schemas: any;
}

export type Sorting = {
property: string;
direction: "asc" | "desc";
Expand Down
4 changes: 3 additions & 1 deletion packages/api-common/source/plugins/comma-separated-query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Hapi from "@hapi/hapi";
import { set } from "@mainsail/utils";

export const commaArrayQuery = {
name: "comma-array-query",
Expand All @@ -10,7 +11,8 @@ export const commaArrayQuery = {
query[key] = value.includes(separator) ? value.split(separator) : value;
}

request.query = query;
set(request, "query", query);

return h.continue;
},

Expand Down
2 changes: 1 addition & 1 deletion packages/api-common/source/plugins/dot-separated-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const dotSeparatedQuery = {
for (const [key, value] of Object.entries(request.query)) {
set(query, key, value);
}
request.query = query;
set(request, "query", query);
return h.continue;
},

Expand Down
2 changes: 1 addition & 1 deletion packages/api-common/source/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { rateLimit } from "./rate-limit";
import { responseHeaders } from "./response-headers";
import { whitelist } from "./whitelist";

export const preparePlugins = (config) => [
export const preparePlugins: any = (config) => [
{
options: {
trustProxy: config.trustProxy,
Expand Down
2 changes: 1 addition & 1 deletion packages/api-common/source/plugins/rate-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
server.ext({
async method(request, h) {
try {
const rateLimitRes: RateLimiterRes = await rateLimiter.consume(

Check warning on line 57 in packages/api-common/source/plugins/rate-limit.ts

View workflow job for this annotation

GitHub Actions / source (20.x)

Please rename the variable `rateLimitRes`. Suggested names are: `rateLimitResponse`, `rateLimitResult`. A more descriptive name will do too
getIp(request, options.trustProxy),
1,
);
Expand Down Expand Up @@ -86,7 +86,7 @@
if (request.plugins["rate-limit"]) {
const data = request.plugins["rate-limit"] as RateLimitPluginData;

if (request.response.isBoom) {
if ("isBoom" in request.response) {
request.response.output.headers["X-RateLimit-Limit"] = String(options.points);
request.response.output.headers["X-RateLimit-Remaining"] = String(data.remaining);
request.response.output.headers["X-RateLimit-Reset"] = new Date(data.reset).toUTCString();
Expand Down
9 changes: 5 additions & 4 deletions packages/api-common/source/plugins/response-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ import Hapi from "@hapi/hapi";
import { Contracts as ApiDatabaseContracts, Identifiers as ApiDatabaseIdentifiers } from "@mainsail/api-database";
import { Contracts } from "@mainsail/contracts";

import { ApiServer } from "../contracts";

export const responseHeaders = {
getOnPreResponseHandler(app: Contracts.Kernel.Application) {
const blockRepositoryFactory = app.get<ApiDatabaseContracts.IBlockRepositoryFactory>(
ApiDatabaseIdentifiers.BlockRepositoryFactory,
);

return async (request: Hapi.Request, h: Hapi.ResponseToolkit): Hapi.Lifecycle.ReturnValue => {
return async (request: Hapi.Request, h: Hapi.ResponseToolkit): Promise<Hapi.Lifecycle.ReturnValue> => {
const blockHeight = await blockRepositoryFactory().getLatestHeight();

const responsePropertyToUpdate = request.response.isBoom ? request.response.output : request.response;
responsePropertyToUpdate.headers = responsePropertyToUpdate.headers ?? {};
const responsePropertyToUpdate = "isBoom" in request.response ? request.response.output : request.response;
responsePropertyToUpdate.headers["x-block-height"] = blockHeight;

return h.continue;
};
},
name: "response-headers",

register(server: Hapi.Server): void {
register(server: ApiServer): void {
server.ext("onPreResponse", this.getOnPreResponseHandler(server.app.app));
},

Expand Down
10 changes: 4 additions & 6 deletions packages/api-common/source/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { Contracts, Identifiers } from "@mainsail/contracts";
import { Providers, Utils } from "@mainsail/kernel";
import { readFileSync } from "fs";

export enum ServerType {
Http = "HTTP",
Https = "HTTPS",
}
import { ApiServer, ServerType } from "./contracts";

@injectable()
export abstract class AbstractServer {
Expand All @@ -17,7 +14,7 @@ export abstract class AbstractServer {
@inject(Identifiers.LogService)
protected readonly logger!: Contracts.Kernel.Logger;

private server: HapiServer;
private server!: ApiServer;

protected abstract baseName(): string;
private serverType!: ServerType;
Expand Down Expand Up @@ -49,7 +46,8 @@ export abstract class AbstractServer {
});

this.server.ext("onPreResponse", (request, h) => {
if (request.response.isBoom && request.response.isServer) {
if ("isBoom" in request.response && request.response.isBoom && request.response.isServer) {
// @ts-ignore
this.logger.error(request.response.stack);
}
return h.continue;
Expand Down
4 changes: 2 additions & 2 deletions packages/api-common/source/service-provider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Providers } from "@mainsail/kernel";
import Joi from "joi";

import { ServerType } from "./contracts";
import { preparePlugins } from "./plugins";
// import { preparePlugins } from "./plugins";
import { AbstractServer, ServerType } from "./server";
import { AbstractServer } from "./server";

export type ServerConstructor<T extends AbstractServer> = new (...arguments_: any[]) => T;
export abstract class AbstractServiceProvider<T extends AbstractServer> extends Providers.ServiceProvider {
Expand Down
13 changes: 5 additions & 8 deletions packages/api-development/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@
"test:file": "uvu -r tsm source"
},
"dependencies": {
"@hapi/boom": "9.1.4",
"@hapi/hapi": "20.1.5",
"@hapi/hoek": "9.2.0",
"@hapi/boom": "10.0.1",
"@hapi/hapi": "21.3.2",
"@hapi/hoek": "11.0.2",
"@mainsail/container": "workspace:*",
"@mainsail/contracts": "workspace:*",
"@mainsail/kernel": "workspace:*",
"@mainsail/transactions": "workspace:*",
"@mainsail/utils": "workspace:*",
"joi": "17.9.2",
"joi": "17.11.0",
"nanomatch": "1.2.13",
"rate-limiter-flexible": "1.3.2",
"semver": "6.3.0"
"semver": "7.5.4"
},
"devDependencies": {
"@types/hapi__boom": "7.4.1",
"@types/hapi__hapi": "21.0.0",
"@types/hapi__joi": "17.1.7",
"@types/ip": "1.1.0",
"@types/semver": "6.2.3",
"uvu": "^0.5.6"
Expand Down
10 changes: 5 additions & 5 deletions packages/api-development/source/controllers/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class BlocksController extends Controller {
@inject(Identifiers.Database.Service)
private readonly database!: Contracts.Database.IDatabaseService;

public async index(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async index(request: Hapi.Request) {
const lastBlock = this.stateService.getStateStore().getLastBlock();

const pagination = this.getQueryPagination(request.query);
Expand Down Expand Up @@ -45,7 +45,7 @@ export class BlocksController extends Controller {
}
}

public async first(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async first(request: Hapi.Request) {
const block = this.stateService.getStateStore().getGenesisBlock();

if (request.query.transform) {
Expand All @@ -55,7 +55,7 @@ export class BlocksController extends Controller {
}
}

public async last(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async last(request: Hapi.Request) {
const block = this.stateService.getStateStore().getLastBlock();

if (request.query.transform) {
Expand All @@ -65,7 +65,7 @@ export class BlocksController extends Controller {
}
}

public async show(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async show(request: Hapi.Request) {
const block = await this.getBlock(request.params.id);

if (!block) {
Expand All @@ -79,7 +79,7 @@ export class BlocksController extends Controller {
}
}

public async transactions(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async transactions(request: Hapi.Request) {
const block = await this.getBlock(request.params.id);

if (!block) {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-development/source/controllers/peers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class PeersController extends Controller {
@inject(Identifiers.PeerRepository)
private readonly peerRepository!: Contracts.P2P.PeerRepository;

public async index(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async index(request: Hapi.Request) {
const allPeers: Contracts.P2P.Peer[] = [...this.peerRepository.getPeers()];

let results = allPeers;
Expand Down Expand Up @@ -86,7 +86,7 @@ export class PeersController extends Controller {
return super.toPagination(resultsPage, PeerResource);
}

public async show(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async show(request: Hapi.Request) {
if (!this.peerRepository.hasPeer(request.params.ip)) {
return Boom.notFound("Peer not found");
}
Expand Down
13 changes: 7 additions & 6 deletions packages/api-development/source/controllers/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class TransactionsController extends Controller {
@inject(Identifiers.TransactionPoolProcessor)
private readonly processor!: Contracts.TransactionPool.Processor;

public async store(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async store(request: Hapi.Request) {
// @ts-ignore
const result = await this.processor.process(request.payload.transactions);
return {
data: {
Expand All @@ -37,7 +38,7 @@ export class TransactionsController extends Controller {
};
}

public async show(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async show(request: Hapi.Request) {
console.log("ID:", request.params.id);

const transaction = await this.database.getTransaction(request.params.id);
Expand All @@ -61,7 +62,7 @@ export class TransactionsController extends Controller {
return this.respondWithResource(transaction.data, TransactionResource, false);
}

public async unconfirmed(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async unconfirmed(request: Hapi.Request) {
const pagination: Pagination = super.getListingPage(request);
const all: Contracts.Crypto.ITransaction[] = await this.poolQuery.getFromHighestPriority().all();
const transactions: Contracts.Crypto.ITransaction[] = all.slice(
Expand All @@ -78,7 +79,7 @@ export class TransactionsController extends Controller {
return super.toPagination(resultsPage, TransactionResource, !!request.query.transform);
}

public async showUnconfirmed(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async showUnconfirmed(request: Hapi.Request) {
const transactionQuery: Contracts.TransactionPool.QueryIterable = this.poolQuery
.getFromHighestPriority()
.whereId(request.params.id);
Expand All @@ -92,7 +93,7 @@ export class TransactionsController extends Controller {
return super.respondWithResource(transaction.data, TransactionResource, !!request.query.transform);
}

public async types(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async types(request: Hapi.Request) {
const activatedTransactionHandlers = await this.nullHandlerRegistry.getActivatedHandlers();
const typeGroups: Record<string | number, Record<string, number>> = {};

Expand All @@ -117,7 +118,7 @@ export class TransactionsController extends Controller {
return { data: typeGroups };
}

public async schemas(request: Hapi.Request, h: Hapi.ResponseToolkit) {
public async schemas(request: Hapi.Request) {
const activatedTransactionHandlers = await this.nullHandlerRegistry.getActivatedHandlers();
const schemasByType: Record<string, Record<string, any>> = {};

Expand Down
2 changes: 1 addition & 1 deletion packages/api-development/source/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { rateLimit } from "./rate-limit";
import { responseHeaders } from "./response-headers";
import { whitelist } from "./whitelist";

export const preparePlugins = (config) => [
export const preparePlugins: any = (config) => [
{
options: {
trustProxy: config.trustProxy,
Expand Down
2 changes: 1 addition & 1 deletion packages/api-development/source/plugins/rate-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const rateLimit = {
if (request.plugins["rate-limit"]) {
const data = request.plugins["rate-limit"] as RateLimitPluginData;

if (request.response.isBoom) {
if ("isBoom" in request.response) {
request.response.output.headers["X-RateLimit-Limit"] = String(options.points);
request.response.output.headers["X-RateLimit-Remaining"] = String(data.remaining);
request.response.output.headers["X-RateLimit-Reset"] = new Date(data.reset).toUTCString();
Expand Down
5 changes: 2 additions & 3 deletions packages/api-development/source/plugins/response-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ export const responseHeaders = {
.getStateStore()
.getLastHeight();

const responsePropertyToUpdate = request.response.isBoom ? request.response.output : request.response;
responsePropertyToUpdate.headers = responsePropertyToUpdate.headers ?? {};
const responsePropertyToUpdate = "isBoom" in request.response ? request.response.output : request.response;
responsePropertyToUpdate.headers["X-Block-Height"] = blockHeight;

return h.continue;
};
},
name: "response-headers",

register(server: Hapi.Server): void {
register(server: Hapi.Server<any>): void {
server.ext("onPreResponse", this.getOnPreResponseHandler(server.app.app));
},

Expand Down
2 changes: 1 addition & 1 deletion packages/api-development/source/routes/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Hapi from "@hapi/hapi";

import { BlockchainController } from "../controllers/blockchain";

export const register = (server: Hapi.Server): void => {
export const register = (server: Hapi.Server<any>): void => {
const controller = server.app.app.resolve(BlockchainController);
server.bind(controller);

Expand Down
2 changes: 1 addition & 1 deletion packages/api-development/source/routes/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Joi from "joi";
import { BlocksController } from "../controllers/blocks";
import { blockId, pagination } from "../schemas";

export const register = (server: Hapi.Server): void => {
export const register = (server: Hapi.Server<any>): void => {
const controller = server.app.app.resolve(BlocksController);
server.bind(controller);

Expand Down
2 changes: 1 addition & 1 deletion packages/api-development/source/routes/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Hapi from "@hapi/hapi";

import { ConsensusController } from "../controllers/consensus";

export const register = (server: Hapi.Server): void => {
export const register = (server: Hapi.Server<any>): void => {
const controller = server.app.app.resolve(ConsensusController);
server.bind(controller);

Expand Down
2 changes: 1 addition & 1 deletion packages/api-development/source/routes/delegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Hapi from "@hapi/hapi";
import { DelegatesController } from "../controllers/delegates";
import { pagination } from "../schemas";

export const register = (server: Hapi.Server): void => {
export const register = (server: Hapi.Server<any>): void => {
const controller = server.app.app.resolve(DelegatesController);
server.bind(controller);

Expand Down
2 changes: 1 addition & 1 deletion packages/api-development/source/routes/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Hapi from "@hapi/hapi";

import { NodeController } from "../controllers/node";

export const register = (server: Hapi.Server): void => {
export const register = (server: Hapi.Server<any>): void => {
const controller = server.app.app.resolve(NodeController);
server.bind(controller);

Expand Down
Loading
Loading