-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,9 @@ | |
# node | ||
node_modules | ||
node/stroeer | ||
node/dist | ||
node-legacy/stroeer | ||
node-legacy/dist | ||
bin/protoc | ||
*.tgz | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"allowJs": true, | ||
"noEmit": false, | ||
}, | ||
"extends": "./tsconfig.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"allowJs": true, | ||
"noEmit": false, | ||
}, | ||
"extends": "./tsconfig.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters