Skip to content

Commit

Permalink
Add support for Deno with an index-deno.ts (#2522)
Browse files Browse the repository at this point in the history
* Add support for Deno with an index-deno.ts

* PR number

* Install Deno in the CI

* Add `zlibInflate` after merge

* Try make it work

* Fix the inflate code

* Comment

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
tomaka and mergify[bot] authored Jul 19, 2022
1 parent e9c804e commit 4e84148
Show file tree
Hide file tree
Showing 8 changed files with 582 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ jobs:
- uses: actions/[email protected]
with:
node-version: '14'
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: cd bin/wasm-node/javascript && RUSTFLAGS=-Dwarnings npm install-ci-test

wasm-node-size-diff:
Expand Down
4 changes: 4 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

- Add support for Deno. Smoldot is now available on the deno.land/x package registry. This doesn't modify anything to the behaviour of the smoldot NPM package. ([#2522](https://github.com/paritytech/smoldot/pull/2522))

### Fixed

- Exceptions thrown in the JSON-RPC callback no longer crash smoldot. ([#2527](https://github.com/paritytech/smoldot/pull/2527))
Expand Down
87 changes: 87 additions & 0 deletions bin/wasm-node/javascript/demo/demo-deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Smoldot
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// This file launches a WebSocket server that exposes JSON-RPC functions.

import * as smoldot from '../dist/mjs/index-deno.js';

// Load the chain spec file.
const chainSpec = new TextDecoder("utf-8").decode(await Deno.readFile("../../westend.json"));

const client = smoldot.start({
maxLogLevel: 3, // Can be increased for more verbosity
forbidTcp: false,
forbidWs: false,
forbidNonLocalWs: false,
forbidWss: false,
cpuRateLimit: 0.5,
logCallback: (_level, target, message) => {
// As incredible as it seems, there is currently no better way to print the current time
// formatted in a certain way.
const now = new Date();
const hours = ("0" + now.getHours()).slice(-2);
const minutes = ("0" + now.getMinutes()).slice(-2);
const seconds = ("0" + now.getSeconds()).slice(-2);
const milliseconds = ("00" + now.getMilliseconds()).slice(-3);
console.log(
"[%s:%s:%s.%s] [%s] %s",
hours, minutes, seconds, milliseconds, target, message
);
}
});

// We add the chain ahead of time in order to preload it.
// Once a client connects, the chain is added again, but smoldot is smart enough to not connect
// a second time.
client.addChain({ chainSpec });

// Now spawn a WebSocket server in order to handle JSON-RPC clients.
console.log('JSON-RPC server now listening on port 9944');
console.log('Please visit: https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944');

const conn = Deno.listen({ port: 9944 });
const httpConn = Deno.serveHttp(await conn.accept());

while(true) {
const event = await httpConn.nextRequest();
if (!event)
continue;

console.log('(demo) New JSON-RPC client connected.');

const { socket, response } = Deno.upgradeWebSocket(event.request);

const chain = await client.addChain({
chainSpec,
jsonRpcCallback: (response) => socket.send(response)
});

socket.onclose = () => {
console.log("(demo) JSON-RPC client disconnected.");
chain.remove();
};

socket.onmessage = (event: Deno.MessageEvent) => {
if (typeof event.data === 'string') {
chain.sendJsonRpc(event.data);
} else {
socket.close(1002); // Protocol error
}
};

event.respondWith(response);
}
2 changes: 1 addition & 1 deletion bin/wasm-node/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"prepublishOnly": "node prepare.mjs --release && rimraf ./dist && npm run buildModules",
"build": "node prepare.mjs --release && rimraf ./dist && npm run buildModules",
"start": "node prepare.mjs --debug && rimraf ./dist && npm run buildModules && node demo/demo.mjs",
"test": "node prepare.mjs --debug && rimraf ./dist && npm run buildModules && ava --timeout=2m --concurrency 2 --no-worker-threads"
"test": "node prepare.mjs --debug && rimraf ./dist && npm run buildModules && deno run ./dist/mjs/index-deno.js && ava --timeout=2m --concurrency 2 --no-worker-threads"
},
"dependencies": {
"pako": "^2.0.4",
Expand Down
2 changes: 1 addition & 1 deletion bin/wasm-node/javascript/src/index-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function start(options?: ClientOptions): Client {

return innerStart(options, {
base64DecodeAndZlibInflate: (input) => {
return pako.inflate(trustedBase64Decode(input))
return Promise.resolve(pako.inflate(trustedBase64Decode(input)))
},
performanceNow: () => {
return performance.now()
Expand Down
Loading

0 comments on commit 4e84148

Please sign in to comment.