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

fix: definition error #87

Merged
merged 1 commit into from
Apr 25, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/fixtures/ts
24 changes: 24 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->
31 changes: 31 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm i -g npminstall && npminstall
- run: npm run ci
env:
CI: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ node_modules
npm-debug.log
coverage/
.nyc_output/
test/fixtures/ts/*.js
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
sudo: false

language: node_js
node_js:
- '8'
- '10'
- '12'
before_install:
- npm i npminstall -g
install:
- npm i npminstall && npminstall
- npminstall
script:
- npm run ci
after_script:
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '10'
- nodejs_version: '12'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
70 changes: 43 additions & 27 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,62 @@
declare module "agentkeepalive" {
import * as http from 'http';
import * as https from 'https';
import * as http from 'http';
import * as https from 'https';

interface PlainObject {
[key: string]: any;
}

declare class HttpAgent extends http.Agent {
constructor(opts?: AgentKeepAlive.HttpOptions);
readonly statusChanged: boolean;
createSocket(req: http.IncomingMessage, options: http.RequestOptions, cb: Function): void;
getCurrentStatus(): AgentKeepAlive.AgentStatus;
}

interface Constants {
CURRENT_ID: Symbol;
CREATE_ID: Symbol;
INIT_SOCKET: Symbol;
CREATE_HTTPS_CONNECTION: Symbol;
SOCKET_CREATED_TIME: Symbol;
SOCKET_NAME: Symbol;
SOCKET_REQUEST_COUNT: Symbol;
SOCKET_REQUEST_FINISHED_COUNT: Symbol;
}

declare class AgentKeepAlive extends HttpAgent {}

declare namespace AgentKeepAlive {
export interface AgentStatus {
createSocketCount: number,
createSocketErrorCount: number,
closeSocketCount: number,
errorSocketCount: number,
timeoutSocketCount: number,
requestCount: number,
freeSockets: object,
sockets: object,
requests: object,
createSocketCount: number;
createSocketErrorCount: number;
closeSocketCount: number;
errorSocketCount: number;
timeoutSocketCount: number;
requestCount: number;
freeSockets: PlainObject;
sockets: PlainObject;
requests: PlainObject;
}

export interface HttpOptions extends http.AgentOptions {
interface CommonHttpOption {
keepAlive?: boolean;
freeSocketTimeout?: number;
freeSocketKeepAliveTimeout?: number;
timeout?: number;
socketActiveTTL?: number;
}

export interface HttpsOptions extends https.AgentOptions {
keepAlive?: boolean;
freeSocketTimeout?: number;
freeSocketKeepAliveTimeout?: number;
timeout?: number;
socketActiveTTL?: number;
}
export interface HttpOptions extends http.AgentOptions, CommonHttpOption { }
export interface HttpsOptions extends https.AgentOptions, CommonHttpOption { }

export default class HttpAgent extends http.Agent {
export class HttpsAgent extends https.Agent {
constructor(opts?: HttpOptions);
readonly statusChanged: boolean;
createSocket(req: http.IncomingMessage, options: http.RequestOptions, cb: Function): void;
getCurrentStatus(): AgentStatus;
}

export class HttpsAgent extends https.Agent {
constructor(opts?: HttpsOptions);
readonly statusChanged: boolean;
createSocket(req: http.IncomingMessage, options: https.RequestOptions, cb: Function): void;
getCurrentStatus(): AgentStatus;
}
export const constants: Constants;
}

export = AgentKeepAlive;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
},
"devDependencies": {
"autod": "^3.0.1",
"coffee": "^5.3.0",
"egg-bin": "^4.9.0",
"egg-ci": "^1.10.0",
"eslint": "^5.7.0",
"eslint-config-egg": "^7.1.0",
"mm": "^2.4.1",
"pedding": "^1.1.0"
"pedding": "^1.1.0",
"typescript": "^3.8.3"
},
"engines": {
"node": ">= 8.0.0"
Expand Down
58 changes: 58 additions & 0 deletions test/fixtures/ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import http from 'http';
import Agent from '../../..';
import assert from 'assert';

const constants = Agent.constants;
assert(constants.CREATE_ID);
assert(constants.CREATE_HTTPS_CONNECTION);
assert(constants.CURRENT_ID);

const httpOpt: Agent.HttpOptions = {
maxSockets: 100,
maxFreeSockets: 10,
timeout: 60000, // active socket keepalive for 60 seconds
freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
};
const keepaliveAgent = new Agent(httpOpt);

const options = {
host: 'cnodejs.org',
port: 80,
path: '/',
method: 'GET',
agent: keepaliveAgent,
};

const req = http.request(options, res => {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});

req.on('error', e => {
console.log('problem with request: ' + e.message);
});

req.end();

setTimeout(() => {
if (keepaliveAgent.statusChanged) {
const httpAgentStatus: Agent.AgentStatus = keepaliveAgent.getCurrentStatus();
console.log('[%s] agent status changed: %j', Date(), httpAgentStatus);
}
}, 2000);

// https
const HttpsAgent = Agent.HttpsAgent;
const httpsOpt: Agent.HttpsOptions = {
maxSockets: 100,
maxFreeSockets: 10,
timeout: 60000, // active socket keepalive for 60 seconds
freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
};
const keepaliveHttpsAgent = new HttpsAgent(httpsOpt);
const httpsAgentStatus: Agent.AgentStatus = keepaliveHttpsAgent.getCurrentStatus();
assert(httpsAgentStatus);
27 changes: 27 additions & 0 deletions test/fixtures/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"noImplicitAny": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"charset": "utf8",
"allowJs": false,
"pretty": false,
"baseUrl": ".",
"strict": true,
"resolveJsonModule": true,
"noEmitOnError": false,
"noUnusedLocals": false,
"noUnusedParameters": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": false,
"skipDefaultLibCheck": false,
"importHelpers": false,
"inlineSourceMap": true,
"esModuleInterop": true
}
}
2 changes: 2 additions & 0 deletions test/test-https-agent-session-eviction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe('test/test-https-agent-session-eviction.test.js', () => {
const name = Object.keys(httpsAgent.freeSockets);
const socket = httpsAgent.freeSockets[name][0];
socket.on('close', err => {
if (socket.destroyed) return;
assert.equal(err.message, 'mock close error');
socket.destroy();
done();
});
socket.emit('close', new Error('mock close error'));
Expand Down
21 changes: 21 additions & 0 deletions test/ts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const path = require('path');
const coffee = require('coffee');

describe('test/ts.test.js', () => {
it('should compile ts and run without error', async () => {
await coffee.fork(
require.resolve('typescript/bin/tsc'),
[ '-p', path.resolve(__dirname, './fixtures/ts/tsconfig.json') ]
)
.debug()
.expect('code', 0)
.end();

await coffee.fork(path.resolve(__dirname, './fixtures/ts/index.js'))
// .debug()
.expect('code', 0)
.end();
});
});