-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds websocket support to the node SDK.
- Loading branch information
Showing
12 changed files
with
596 additions
and
3 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 |
---|---|---|
|
@@ -22,7 +22,8 @@ | |
"license:header:check": "license-check-and-add check -f ./licenseconfig.json", | ||
"license:check": "licensee --production", | ||
"download:contracts": "curl -L https://github.com/nitrictech/nitric/releases/download/${npm_package_nitric}/contracts.tgz -o contracts.tgz && tar xvzf contracts.tgz && rm contracts.tgz", | ||
"gen:proto": "yarn run download:contracts && mkdir -p ./src/gen && grpc_tools_node_protoc --ts_out=service=grpc-node,mode=grpc-js:./src/gen --js_out=import_style=commonjs,binary:./src/gen --grpc_out=grpc_js:./src/gen -I ./contracts ./contracts/**/*.proto ./contracts/proto/**/*/*.proto" | ||
"gen:proto": "yarn run download:contracts && yarn run gen:sources", | ||
"gen:sources": "mkdir -p ./src/gen && grpc_tools_node_protoc --ts_out=service=grpc-node,mode=grpc-js:./src/gen --js_out=import_style=commonjs,binary:./src/gen --grpc_out=grpc_js:./src/gen -I ./contracts ./contracts/**/*.proto ./contracts/proto/**/*/*.proto" | ||
}, | ||
"contributors": [ | ||
"Jye Cusch <[email protected]>", | ||
|
@@ -72,6 +73,7 @@ | |
"ts-jest": "^26.4.3", | ||
"ts-node": "^10.9.1", | ||
"ts-protoc-gen": "^0.15.0", | ||
"tsconfig-paths": "^4.2.0", | ||
"tsup": "^6.5.0", | ||
"typescript": "^4.4" | ||
}, | ||
|
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,13 @@ | ||
// Copyright 2021, Nitric Technologies Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. |
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,14 @@ | ||
// Copyright 2021, Nitric Technologies Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
export * from './websocket'; |
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,101 @@ | ||
// Copyright 2021, Nitric Technologies Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
import { SERVICE_BIND } from '../../../constants'; | ||
import { WebsocketServiceClient } from '@nitric/api/proto/websocket/v1/websocket_grpc_pb'; | ||
import { | ||
WebsocketSendRequest, | ||
WebsocketCloseRequest, | ||
} from '@nitric/api/proto/websocket/v1/websocket_pb'; | ||
import * as grpc from '@grpc/grpc-js'; | ||
import { fromGrpcError } from '../../errors'; | ||
|
||
/** | ||
* Nitric websocket client, facilitates sending messages to connections on this websocket. | ||
*/ | ||
export class Websocket { | ||
client: WebsocketServiceClient; | ||
|
||
constructor() { | ||
this.client = new WebsocketServiceClient( | ||
SERVICE_BIND, | ||
grpc.ChannelCredentials.createInsecure() | ||
); | ||
} | ||
|
||
async send( | ||
socket: string, | ||
connectionId: string, | ||
message: string | Uint8Array | Record<string, any> | ||
): Promise<void> { | ||
let payload: Uint8Array; | ||
|
||
// handle all message types | ||
if (typeof message === 'string') { | ||
payload = new TextEncoder().encode(message); | ||
} else if (message instanceof Uint8Array) { | ||
payload = message; | ||
} else { | ||
payload = new TextEncoder().encode(JSON.stringify(message)); | ||
} | ||
|
||
const sendRequest = new WebsocketSendRequest(); | ||
|
||
sendRequest.setSocket(socket); | ||
sendRequest.setConnectionId(connectionId); | ||
sendRequest.setData(payload); | ||
|
||
return new Promise((res, rej) => { | ||
this.client.send(sendRequest, (error, data) => { | ||
if (error) { | ||
rej(fromGrpcError(error)); | ||
} | ||
|
||
res(); | ||
}); | ||
}); | ||
} | ||
|
||
async close(socket: string, connectionId: string): Promise<void> { | ||
const closeRequest = new WebsocketCloseRequest(); | ||
|
||
closeRequest.setSocket(socket); | ||
closeRequest.setConnectionId(connectionId); | ||
|
||
return new Promise((res, rej) => { | ||
this.client.close(closeRequest, (error) => { | ||
if (error) { | ||
rej(fromGrpcError(error)); | ||
} | ||
|
||
res(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
// Websocket client singleton | ||
let WEBSOCKET = undefined; | ||
|
||
/** | ||
* Websocket API client. | ||
* | ||
* @returns a Websocket API client. | ||
*/ | ||
export const websocket = (): Websocket => { | ||
if (!WEBSOCKET) { | ||
WEBSOCKET = new Websocket(); | ||
} | ||
|
||
return WEBSOCKET; | ||
}; |
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
Oops, something went wrong.