Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CJS projects #2487

Merged
merged 26 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bee1764
Add support for CJS projects
wirednkod Jul 8, 2022
68ad7dd
Add Changelog entry
wirednkod Jul 8, 2022
644933e
Add Changelog entry
wirednkod Jul 8, 2022
f70a2c4
Merge branch 'main' of github.com:wirednkod/smoldot into nik-add-cjs-…
wirednkod Jul 8, 2022
b8e6f6b
minor fixes to package.json
wirednkod Jul 8, 2022
029884d
replace yarn install v1.22.15
wirednkod Jul 9, 2022
7aa4c29
replace yarn install v1.22.15
wirednkod Jul 9, 2022
9912cf1
replace yarn install v1.22.15
wirednkod Jul 9, 2022
b2c2ce5
Merge branch 'main' into nik-add-cjs-support
wirednkod Jul 9, 2022
096f04c
remove erroneous files
wirednkod Jul 11, 2022
f738271
remove 'extra' demo and fix pacakge.json accordingly
wirednkod Jul 11, 2022
97f994d
Merge branch 'main' into nik-add-cjs-support
wirednkod Jul 11, 2022
4a31e0a
merge
wirednkod Jul 13, 2022
8f5f692
merge
wirednkod Jul 13, 2022
3434146
remove sed
wirednkod Jul 13, 2022
c56c4a2
Merge branch 'main' into nik-add-cjs-support
wirednkod Jul 13, 2022
b7d7d5e
Update bin/wasm-node/javascript/package.json
wirednkod Jul 13, 2022
7147c37
Update bin/wasm-node/javascript/package.json
wirednkod Jul 13, 2022
4e68b6e
Update bin/wasm-node/javascript/tsconfig-cjs.json
wirednkod Jul 13, 2022
8d5637e
Address PR comments
wirednkod Jul 13, 2022
c1d762b
Fix Changelog
wirednkod Jul 13, 2022
4fd9541
Fix Changelog
wirednkod Jul 13, 2022
5679d22
remove from gitignore fix-package-type file
wirednkod Jul 13, 2022
de87cce
Fix tests; add module in package json in order to avoid renaming to m…
wirednkod Jul 13, 2022
42577b8
remove type module and rename files to mjs
wirednkod Jul 13, 2022
1aff5e7
Remove yarn lock
wirednkod Jul 13, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 CommonJS projects [#2487](https://github.com/paritytech/smoldot/pull/2487)
wirednkod marked this conversation as resolved.
Show resolved Hide resolved

## Changed

- Block headers with an unknown consensus engine now parse successfully. This adds support for parachains using consensus engines that smoldot doesn't recognize. As smoldot cannot verify the validity of their blocks, standalone/relay chains using an unrecognized consensus engine remain unsupported. ([#2481](https://github.com/paritytech/smoldot/pull/2481))
Expand Down
179 changes: 179 additions & 0 deletions bin/wasm-node/javascript/demo/demo.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Smoldot
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
// 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.

const smoldot = require('../dist/cjs/index.js');
const websocket = require('websocket');
const http = require('node:http');
const process = require('node:process');
const fs = require('node:fs');

// List of files containing chains available to the user.
// The first item has a specific role in that we always connect to it at initialization.
const chainSpecsFiles = [
'../../westend.json',
'../../westend-westmint.json',
'../../polkadot.json',
'../../polkadot-acala.json',
'../../kusama.json',
'../../kusama-statemine.json',
'../../kusama-karura.json',
'../../rococo.json',
'../../rococo-adz.json',
'../../rococo-canvas.json',
];

// Load all the files in a single map.
const chainSpecsById = {};
let firstChainSpecId = null;
for (const file of chainSpecsFiles) {
const content = fs.readFileSync(file, 'utf8');
const decoded = JSON.parse(content);
if (!firstChainSpecId)
firstChainSpecId = decoded.id;
chainSpecsById[decoded.id] = {
chainSpec: content,
relayChain: decoded.relay_chain,
};
}

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
);
}
});

// Note that We call `addChain` again with the same chain spec again every time a new WebSocket
// connection is established, but smoldot will de-duplicate them and only connect to the chain
// once. By calling it now, we let smoldot start syncing that chain in the background even before
// a WebSocket connection has been established.
client
.addChain({ chainSpec: chainSpecsById[firstChainSpecId].chainSpec })
.catch((error) => {
console.error("Error while adding chain: " + error);
process.exit(1);
});

// Start the WebSocket server listening on port 9944.
let server = http.createServer(function (_request, response) {
response.writeHead(404);
response.end();
});
server.listen(9944, function () {
console.log('JSON-RPC server now listening on port 9944');
console.log('Please visit one of:');
for (const chainId in chainSpecsById) {
console.log('- ' + chainId + ': https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944%2F' + chainId);
}
console.log('');
});
let wsServer = new websocket.server({
httpServer: server,
autoAcceptConnections: false,
});

wsServer.on('request', function (request) {
// Received a new incoming WebSocket connection.

// Note that we don't care too much about sanitizing input as this is just a demo.
const chainCfg = chainSpecsById[request.resource.substring(1)];

if (!chainCfg) {
request.reject(404);
return;
}

// Start loading the chain.
let chain = (async () => {
if (chainCfg.relayChain) {
const relay = await client.addChain({
chainSpec: chainSpecsById[chainCfg.relayChain].chainSpec,
});

const para = await client.addChain({
chainSpec: chainCfg.chainSpec,
jsonRpcCallback: (resp) => {
connection.sendUTF(resp);
},
potentialRelayChains: [relay]
});

return { relay, para };
} else {
return {
relay: await client.addChain({
chainSpec: chainCfg.chainSpec,
jsonRpcCallback: (resp) => {
connection.sendUTF(resp);
},
})
};
}
})().catch((error) => {
console.error("(demo) Error while adding chain: " + error);
connection.close(400);
});

const connection = request.accept(request.requestedProtocols[0], request.origin);
console.log('(demo) New JSON-RPC client connected: ' + request.remoteAddress + '.');

// Receiving a message from the connection. This is a JSON-RPC request.
connection.on('message', function (message) {
if (message.type === 'utf8') {
chain
.then(chain => {
if (chain.para)
chain.para.sendJsonRpc(message.utf8Data);
else
chain.relay.sendJsonRpc(message.utf8Data);
})
.catch((error) => {
console.error("(demo) Error during JSON-RPC request: " + error);
process.exit(1);
});
} else {
connection.close(400);
}
});

// When the connection closes, remove the chains that have been added.
connection.on('close', function (reasonCode, description) {
console.log("(demo) JSON-RPC client " + connection.remoteAddress + ' disconnected.');
chain.then(chain => {
chain.relay.remove();
if (chain.para)
chain.para.remove();
}).catch(() => { });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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

import * as smoldot from '../dist/index.js';
import { start } from '../dist/mjs/index.js';
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
import { default as websocket } from 'websocket';
import * as http from 'node:http';
import * as process from 'node:process';
Expand Down Expand Up @@ -52,7 +52,7 @@ for (const file of chainSpecsFiles) {
};
}

const client = smoldot.start({
const client = start({
maxLogLevel: 3, // Can be increased for more verbosity
forbidTcp: false,
forbidWs: false,
Expand Down
25 changes: 25 additions & 0 deletions bin/wasm-node/javascript/fix-package-type
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
# Adds package.json files to cjs/mjs subtrees with the respective type needed for
# the target environment.
#

cat >dist/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >dist/mjs/package.json <<!EOF
{
"type": "module"
}
!EOF

# This is a workaround for supporting Worker in both ESM and CJS with typescript build and without
# falling into solutions like webpack, babel etc.
# It is a simple `sed` script that replaces
# the `Worker(new URL('./worker.js', import.meta.url), { type: "module" })`
# with `Worker(__dirname.concat('/worker.js'))`
# which allows the worker to work for Common JS.
sed -i -e s/new\ URL\(\'\./__dirname\.concat\(\'/g "dist/cjs/worker/spawn.js"
sed -i -e s/,\ import\.meta\.url\),\ {\ type:\ \"module\"\ }/\)/g "dist/cjs/worker/spawn.js"
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions bin/wasm-node/javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions bin/wasm-node/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@
"url": "https://github.com/paritytech/smoldot/issues"
},
"files": ["dist"],
"type": "module",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"main": "dist/cjs/index.js",
"types": "dist/mjs/index.d.ts",
"module": "dist/mjs/index.js",
"exports": {
".": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js"
}
},
"scripts": {
"prepublishOnly": "node prepare.js --release && rimraf ./dist && tsc",
"build": "node prepare.js --release && rimraf ./dist && tsc",
"start": "node prepare.js --debug && rimraf ./dist && tsc && node demo/demo.js",
"test": "node prepare.js --debug && rimraf ./dist && tsc && ava --timeout=2m --concurrency 2 --no-worker-threads"
"buildAll": "tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && bash fix-package-type",
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
"prepublishOnly": "node prepare.mjs --release && rimraf ./dist && npm run buildAll",
"build": "node prepare.mjs --release && rimraf ./dist && npm run buildAll",
"demo:cjs": "node prepare.mjs --debug && rimraf ./dist && npm run buildAll && node demo/demo.cjs",
"demo:mjs": "node prepare.mjs --debug && rimraf ./dist && npm run buildAll && node demo/demo.mjs",
"test": "node prepare.mjs --debug && rimraf ./dist && npm run buildAll && ava --timeout=2m --concurrency 2 --no-worker-threads"
},
"browser": {
"./dist/compat/index.js": "./dist/compat/index-browser-overwrite.js",
"./dist/worker/spawn.js": "./dist/worker/spawn-browser-overwrite.js"
"./dist/mjs/compat/index.js": "./dist/mjs/compat/index-browser-overwrite.js",
"./dist/mjs/worker/spawn.js": "./dist/mjs/worker/spawn-browser-overwrite.js"
},
"dependencies": {
"buffer": "^6.0.1",
Expand Down
11 changes: 11 additions & 0 deletions bin/wasm-node/javascript/tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": [
"ES2015"
],
"target": "ES2015",
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
"module": "commonjs",
"outDir": "./dist/cjs",
},
}
11 changes: 11 additions & 0 deletions bin/wasm-node/javascript/tsconfig-mjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": [
"es2020"
],
"target": "es2020",
"module": "es2020",
"outDir": "./dist/mjs",
}
}
8 changes: 1 addition & 7 deletions bin/wasm-node/javascript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
{
"compilerOptions": {
"lib": [
"es2020"
],
"target": "es2020",
"module": "es2020",
"outDir": "./dist",
"declaration": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,

"esModuleInterop": true,
"strict": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
Expand Down
15 changes: 15 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
# yarn lockfile v1