add support for http

This commit is contained in:
zizifn
2022-12-06 00:00:20 +08:00
committed by zizifn
parent be193a1116
commit cc05814c24
25 changed files with 279 additions and 195 deletions

3
libs/test/.babelrc Normal file
View File

@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}

18
libs/test/.eslintrc.json Normal file
View File

@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}

11
libs/test/README.md Normal file
View File

@@ -0,0 +1,11 @@
# test
This library was generated with [Nx](https://nx.dev).
## Running unit tests
Run `nx test test` to execute the unit tests via [Jest](https://jestjs.io).
## Running lint
Run `nx lint test` to execute the lint via [ESLint](https://eslint.org/).

16
libs/test/jest.config.ts Normal file
View File

@@ -0,0 +1,16 @@
/* eslint-disable */
export default {
displayName: 'test',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/test',
};

95
libs/test/node.mjs Normal file
View File

@@ -0,0 +1,95 @@
import { createServer } from 'node:http';
import { connect } from 'node:net';
import { Readable, Duplex, Writable } from 'node:stream';
import { ReadableStream, WritableStream } from 'node:stream/web';
const httpServer = createServer(async (req, resp) => {
// const rawHttp = new ReadableStream({
// start(controller) {
// controller.enqueue(new TextEncoder().encode('GET / HTTP/1.1\r\n'));
// controller.enqueue(new TextEncoder().encode('Host: www.baidu.com\r\n'));
// controller.enqueue(
// new TextEncoder().encode('User-Agent: curl/7.83.1\r\n')
// );
// controller.enqueue(new TextEncoder().encode('Accept: */*\r\n\r\n'));
// controller.close();
// },
// cancel() {},
// });
// async function* resposne() {
// for await (const chunk of [
// 'GET / HTTP/1.1\r\n',
// 'Host: www.baidu.com\r\n',
// 'User-Agent: curl/7.83.1\r\n',
// 'Accept: */*\r\n\r\n',
// ]) {
// yield chunk;
// }
// }
// const buffer = resposne();
// const rawHttp = new Readable({
// async read() {
// console.log('pull----');
// const { value, done } = await buffer.next();
// if (!done) {
// this.push(value);
// }
// },
// });
const rawHttp = Readable.from([
'GET / HTTP/1.1\r\n',
'Host: www.baidu.com\r\n',
'User-Agent: curl/7.83.1\r\n\r\n',
]);
// rawHttp.p;
// rawHttp.push('Accept: */*\r\n\r\n');
rawHttp.on('end', () => {
console.log('rawHttp--end-----');
});
const socket = connect(
{
port: 80,
host: 'www.baidu.com',
},
() => {
console.log('connected');
resp.writeHead(200);
rawHttp.pipe(socket).pipe(resp);
}
);
// .pipe(resp)
// .on('close', () => {
// console.log('--close-----');
// });
});
httpServer.listen('8888', () => {
console.log('Server runnig at http://localhost:8888');
});
// const httpServer = createServer(async (req, resp) => {
// const rawHttp = new ReadableStream({
// start(controller) {
// controller.enqueue(new TextEncoder().encode('GET / HTTP/1.1\r\n'));
// controller.enqueue(new TextEncoder().encode('Host: www.baidu.com\r\n'));
// controller.enqueue(
// new TextEncoder().encode('User-Agent: curl/7.83.1\r\n')
// );
// controller.enqueue(new TextEncoder().encode('Accept: */*\r\n\r\n'));
// controller.close();
// },
// cancel() {},
// });
// const socket = connect({
// port: 80,
// host: 'www.baidu.com',
// });
// resp.writeHead(200);
// const webStreamSocket = rawHttp.pipeThrough(Duplex.toWeb(socket));
// Readable.fromWeb(webStreamSocket).pipe(resp);
// });
// httpServer.listen('8888', () => {
// console.log('Server runnig at http://localhost:8888');
// });

24
libs/test/project.json Normal file
View File

@@ -0,0 +1,24 @@
{
"name": "test",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/test/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/test/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/test/jest.config.ts",
"passWithNoTests": true
}
}
},
"tags": []
}

1
libs/test/src/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './lib/test';

View File

@@ -0,0 +1,7 @@
import { test } from './test';
describe('test', () => {
it('should work', () => {
expect(test()).toEqual('test');
});
});

View File

@@ -0,0 +1,3 @@
export function test(): string {
return 'test';
}

13
libs/test/tsconfig.json Normal file
View File

@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}

View File

@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"],
"include": ["**/*.ts"]
}

View File

@@ -0,0 +1,20 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"**/*.d.ts"
]
}