Skip to content

Commit

Permalink
feat(node): test clients (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobaaaaa authored Jan 31, 2022
1 parent b5ee6e5 commit 099ae85
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
# node
node_modules
node/stroeer
node/dist
node-legacy/stroeer
node-legacy/dist
bin/protoc
*.tgz

Expand Down
5 changes: 4 additions & 1 deletion node-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"test": "jest",
"lint": "tsc -b",
"update": "npx npm-check --update",
"format": "prettier --write '__tests__/**/*.ts'"
"format": "prettier --write '__tests__/**/*.ts'",
"client": "tsc -w -p tsconfig.client.json",
"server": "npx nodemon ./dist/src/server.js",
"start": "node ./dist/src/server.js"
},
"dependencies": {
"@types/google-protobuf": "3.15.5",
Expand Down
47 changes: 47 additions & 0 deletions node-legacy/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { GetSectionPageRequest } from '../stroeer/page/section/v1/section_page_service_pb';
import { SectionPageServiceClient } from '../stroeer/page/section/v1/section_page_service_grpc_pb';
import { credentials, Metadata } from 'grpc';

import type { ServiceError, CallOptions } from 'grpc';
import type { SectionPage } from '../stroeer/page/section/v1/section_page_pb';

const host: string = process.env.GRPC_HOST ?? '';
const apiToken: string = process.env.GRPC_API_TOKEN ?? '';
const ssl = credentials.createSsl();

const client = new SectionPageServiceClient(host, ssl, {
'grpc.primary_user_agent': 'tapir/node-legacy test-client',
});

const createRequest = (sectionPath: string) => {
const request = new GetSectionPageRequest();
request.setPage(0);
request.setSectionPath(sectionPath);
return request;
};

const callOptions = (): CallOptions => {
return {
deadline: Date.now() + 6000,
};
};

const metaData = new Metadata();
metaData.set('authorization', apiToken);

export const getSectionPage = (
sectionPath: string,
cb: (err: ServiceError | null, sectionPage?: SectionPage | undefined) => void
) => {
const request = createRequest(sectionPath);
console.log({ sectionPath });
console.time('request');
client.getSectionPage(request, metaData, callOptions(), (err, value) => {
if (err) {
cb(err);
} else {
cb(null, value?.getSectionPage());
}
console.timeEnd('request');
});
};
43 changes: 43 additions & 0 deletions node-legacy/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createServer } from 'http';
import { getSectionPage } from './client';

const port = 3000;

const ignorePath = ['/favicon.ico', '/service-worker.js'];

const toResult = (data: unknown) => {
return JSON.stringify(data, null, 2);
};

const server = createServer((req, res) => {
if (ignorePath.includes(req.url ?? '')) {
res.end();
} else {
getSectionPage(req.url ?? '/', (err, sectionPage) => {
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
if (err) {
res.end(
toResult({
status: 'error',
error: err,
})
);
} else {
res.end(
toResult({
status: 'success',
path: sectionPage?.getSection()?.getFieldsMap().get('ref_path'),
})
);
}
});
}
});

server.addListener('error', (err) => {
console.error(err);
});

server.listen(port, () => {
console.log(`server is listening on http://localhost:${port}`);
});
8 changes: 8 additions & 0 deletions node-legacy/tsconfig.client.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"outDir": "./dist",
"allowJs": true,
"noEmit": false,
},
"extends": "./tsconfig.json"
}
2 changes: 1 addition & 1 deletion node-legacy/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"isolatedModules": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["node_modules"]
"exclude": ["node_modules", "dist"]
}
5 changes: 4 additions & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"test": "jest",
"lint": "tsc -b",
"update": "npx npm-check --update",
"format": "prettier --write '__tests__/**/*.ts'"
"format": "prettier --write '__tests__/**/*.ts'",
"client": "tsc -w -p tsconfig.client.json",
"server": "npx nodemon ./dist/src/server.js",
"start": "node ./dist/src/server.js"
},
"dependencies": {
"@grpc/grpc-js": "1.5.3",
Expand Down
47 changes: 47 additions & 0 deletions node/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { GetSectionPageRequest } from '../stroeer/page/section/v1/section_page_service_pb';
import { SectionPageServiceClient } from '../stroeer/page/section/v1/section_page_service_grpc_pb';
import { credentials, Metadata } from '@grpc/grpc-js';

import type { ServiceError, CallOptions } from '@grpc/grpc-js';
import type { SectionPage } from '../stroeer/page/section/v1/section_page_pb';

const host: string = process.env.GRPC_HOST ?? '';
const apiToken: string = process.env.GRPC_API_TOKEN ?? '';
const ssl = credentials.createSsl();

const client = new SectionPageServiceClient(host, ssl, {
'grpc.primary_user_agent': 'tapir/node test-client',
});

const createRequest = (sectionPath: string) => {
const request = new GetSectionPageRequest();
request.setPage(0);
request.setSectionPath(sectionPath);
return request;
};

const callOptions = (): CallOptions => {
return {
deadline: Date.now() + 6000,
};
};

const metaData = new Metadata();
metaData.set('authorization', apiToken);

export const getSectionPage = (
sectionPath: string,
cb: (err: ServiceError | null, sectionPage?: SectionPage | undefined) => void
) => {
const request = createRequest(sectionPath);
console.log({ sectionPath });
console.time('request');
client.getSectionPage(request, metaData, callOptions(), (err, value) => {
if (err) {
cb(err);
} else {
cb(null, value?.getSectionPage());
}
console.timeEnd('request');
});
};
43 changes: 43 additions & 0 deletions node/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createServer } from 'http';
import { getSectionPage } from './client';

const port = 3000;

const ignorePath = ['/favicon.ico', '/service-worker.js'];

const toResult = (data: unknown) => {
return JSON.stringify(data, null, 2);
};

const server = createServer((req, res) => {
if (ignorePath.includes(req.url ?? '')) {
res.end();
} else {
getSectionPage(req.url ?? '/', (err, sectionPage) => {
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
if (err) {
res.end(
toResult({
status: 'error',
error: err,
})
);
} else {
res.end(
toResult({
status: 'success',
path: sectionPage?.getSection()?.getFieldsMap().get('ref_path'),
})
);
}
});
}
});

server.addListener('error', (err) => {
console.error(err);
});

server.listen(port, () => {
console.log(`server is listening on http://localhost:${port}`);
});
8 changes: 8 additions & 0 deletions node/tsconfig.client.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"outDir": "./dist",
"allowJs": true,
"noEmit": false,
},
"extends": "./tsconfig.json"
}
2 changes: 1 addition & 1 deletion node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"isolatedModules": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["node_modules"]
"exclude": ["node_modules", "dist"]
}

0 comments on commit 099ae85

Please sign in to comment.