Skip to content

Commit

Permalink
Merge pull request #1993 from aeternity/readonly-host
Browse files Browse the repository at this point in the history
fix(node,compiler,middleware)!: mark $host as readonly
  • Loading branch information
davidyuk committed Jun 18, 2024
2 parents 399a70e + 9e47d5c commit 8620701
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
"build:assets": "node tooling/fetch-aesophia-cli.mjs",
"build:types": "tsc && node tooling/downlevel/run.mjs",
"build:es": "babel src --config-file ./babel.esm.config.js --out-dir es --extensions .js,.ts --out-file-extension .mjs --source-maps true",
"build:api:node": "autorest tooling/autorest/node.yaml",
"build:api:compiler": "node tooling/autorest/compiler-prepare.mjs && autorest tooling/autorest/compiler.yaml",
"build:api:middleware": "node tooling/autorest/middleware-prepare.mjs && autorest tooling/autorest/middleware.yaml",
"build:api:node": "autorest tooling/autorest/node.yaml && node tooling/autorest/postprocessing.mjs node",
"build:api:compiler": "node tooling/autorest/compiler-prepare.mjs && autorest tooling/autorest/compiler.yaml && node tooling/autorest/postprocessing.mjs compiler",
"build:api:middleware": "node tooling/autorest/middleware-prepare.mjs && autorest tooling/autorest/middleware.yaml && node tooling/autorest/postprocessing.mjs middleware",
"build:api": "npm run build:api:node && npm run build:api:compiler && npm run build:api:middleware",
"build:generate": "ts-node --transpileOnly tooling/generate-schema.ts",
"build": "npm run build:api && npm run build:generate && webpack && npm run build:types && npm run build:es && npm run build:assets",
Expand Down
23 changes: 20 additions & 3 deletions test/integration/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, before } from 'mocha';
import { expect } from 'chai';
import { stub } from 'sinon';
import { RestError } from '@azure/core-rest-pipeline';
import { FullOperationResponse } from '@azure/core-client';
import { FullOperationResponse, OperationArguments, OperationSpec } from '@azure/core-client';
import { url } from '.';
import {
AeSdkBase, Node, NodeNotFoundError, MemoryAccount, buildTx, Tag,
Expand Down Expand Up @@ -84,6 +84,12 @@ describe('Node client', () => {
.to.be.rejectedWith(RestError, 'v3/transactions error: Invalid tx (nonce_too_high)');
});

it('can\'t change $host', async () => {
const n = new Node(url);
// @ts-expect-error $host should be readonly
n.$host = 'http://example.com';
});

it('returns recent gas prices', async () => {
const example: Awaited<ReturnType<typeof node.getRecentGasPrices>> = [
{ minGasPrice: 0n, minutes: 5, utilization: 0 },
Expand All @@ -98,9 +104,20 @@ describe('Node client', () => {
});

it('doesn\'t remember failed version request', async () => {
const n = new Node('https://test.stg.aepps.com');
let shouldFail = true;
class CustomNode extends Node {
override sendOperationRequest = async <T>(
args: OperationArguments,
spec: OperationSpec,
): Promise<T> => {
if (shouldFail) spec = { ...spec, path: `https://test.stg.aepps.com${spec.path}` };
return super.sendOperationRequest(args, spec);
};
}

const n = new CustomNode(url);
await expect(n.getTopHeader()).to.be.rejectedWith('v3/status error: 404 status code');
n.$host = url;
shouldFail = false;
expect(await n.getTopHeader()).to.be.an('object');
});

Expand Down
2 changes: 0 additions & 2 deletions tooling/autorest/middleware-prepare.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

import { spawnSync } from 'child_process';
// eslint-disable-next-line import/extensions
import restoreFile from '../restore-file.mjs';
Expand Down
7 changes: 7 additions & 0 deletions tooling/autorest/postprocessing.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from 'fs';

const name = process.argv.at(-1);
const path = `./src/apis/${name}/${name}.ts`;
let content = await fs.promises.readFile(path, { encoding: 'utf-8' });
content = content.replace(/ {2}\$host: string;/, ' readonly $host: string;');
await fs.promises.writeFile(path, content);

0 comments on commit 8620701

Please sign in to comment.