Skip to content

Commit

Permalink
Polishing nodejs-client and mix-fetch-node
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Martinez <[email protected]>
  • Loading branch information
sebastinez committed Nov 1, 2023
1 parent 7129de4 commit 06a96fa
Show file tree
Hide file tree
Showing 28 changed files with 1,257 additions and 191 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/publish-sdk-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'
registry-url: "https://registry.npmjs.org"

- name: Setup yarn
run: npm install -g yarn
Expand All @@ -28,6 +28,16 @@ jobs:
- name: Install wasm-opt
run: cargo install wasm-opt

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: Install TinyGo
uses: acifani/setup-tinygo@v1
with:
tinygo-version: "0.27.0"

- name: Install dependencies
run: yarn

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"sdk/typescript/packages/mui-theme",
"sdk/typescript/packages/react-components",
"sdk/typescript/packages/validator-client",
"sdk/typescript/packages/mix-fetch-node",
"sdk/typescript/packages/nodejs-client",
"ts-packages/*",
"nym-wallet",
"nym-connect/**",
Expand All @@ -34,7 +36,7 @@
"prebuild:ci": "yarn dev:on && yarn",
"build:ci": "run-s build:types build:packages build:wasm build:ci:sdk",
"postbuild:ci": "yarn dev:off",
"build:ci:sdk": "lerna run --scope '{@nymproject/sdk,@nymproject/node-tester,@nymproject/sdk-react,@nymproject/mix-fetch}' build:dev --stream",
"build:ci:sdk": "lerna run --scope '{@nymproject/sdk,@nymproject/node-tester,@nymproject/sdk-react,@nymproject/mix-fetch,@nymproject/nodejs-client,@nymproject/mix-fetch-node}' build --stream",
"docs:prod:build": "run-s docs:prod:build:ws",
"docs:prod:build:ws": "lerna run docs:prod:build --stream",
"sdk:build": "./sdk/typescript/scripts/build-prod-sdk.sh",
Expand All @@ -53,4 +55,4 @@
"@npmcli/node-gyp": "^3.0.0",
"node-gyp": "^9.3.1"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMixFetch, disconnectMixFetch } from '../dist/cjs/index.js';
const { createMixFetch, disconnectMixFetch } = require('@nymproject/mix-fetch-node-commonjs');

/**
* The main entry point
Expand Down
14 changes: 14 additions & 0 deletions sdk/typescript/examples/mix-fetch/node-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@nymproject/mix-fetch-node-js-example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "node index.js",
"start:server": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@nymproject/mix-fetch-node-commonjs": "^1.2.1-rc.2"
}
}
55 changes: 55 additions & 0 deletions sdk/typescript/examples/mix-fetch/node-js/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const express = require('express');
const { mixFetch } = require('@nymproject/mix-fetch-node-commonjs');

const app = express();
app.use(express.static('public'));

app.get('/nym-fetch', async (req, res) => {
try {
const args = {
mode: 'unsafe-ignore-cors',
headers: {
'Content-Type': 'application/json',
},
};

const url = req.query.url;

if (!url) {
return res.status(400).send('input a valid url');
}

const extra = {
hiddenGateways: [
{
owner: 'n1ns3v70ul9gnl9l9fkyz8cyxfq75vjcmx8el0t3',
host: 'sandbox-gateway1.nymtech.net',
explicitIp: '35.158.238.80',
identityKey: 'HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua',
sphinxKey: 'BoXeUD7ERGmzRauMjJD3itVNnQiH42ncUb6kcVLrb3dy',
},
],
};

const mixFetchOptions = {
nymApiUrl: 'https://sandbox-nym-api1.nymtech.net/api',
preferredGateway: 'HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua',
preferredNetworkRequester:
'AzGdJ4MU78Ex22NEWfeycbN7bt3PFZr1MtKstAdhfELG.GSxnKnvKPjjQm3FdtsgG5KyhP6adGbPHRmFWDH4XfUpP@HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua',
mixFetchOverride: {
requestTimeoutMs: 60_000,
},
forceTls: false,
extra,
};

const response = await mixFetch(url, args, mixFetchOptions);
const json = await response.json();
res.send(json);
} catch (error) {
console.log(error);
res.status(500).send(error.message);
}
});

app.listen(3000, () => console.log('Server running on port 3000'));
3 changes: 0 additions & 3 deletions sdk/typescript/packages/mix-fetch-node/.babelrc

This file was deleted.

2 changes: 1 addition & 1 deletion sdk/typescript/packages/mix-fetch-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This package is a drop-in replacement for `fetch` in NodeJS to send HTTP request
## Usage

```js
const { mixFetch } = require('@nymproject/mix-fetch-node');
const { mixFetch } = require('@nymproject/mix-fetch-node-commonjs');

...

Expand Down
4 changes: 2 additions & 2 deletions sdk/typescript/packages/mix-fetch-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nymproject/mix-fetch-node",
"version": "1.2.0",
"version": "1.2.1-rc.2",
"description": "This package is a drop-in replacement for `fetch` in NodeJS to send HTTP requests over the Nym Mixnet.",
"license": "Apache-2.0",
"author": "Nym Technologies SA",
Expand Down Expand Up @@ -28,7 +28,7 @@
"tsc": "tsc --noEmit true"
},
"dependencies": {
"@nymproject/mix-fetch-wasm-node": ">=1.2.0-rc.10 || ^1",
"@nymproject/mix-fetch-wasm-node": ">=1.2.0 || ^1",
"comlink": "^4.3.1",
"fake-indexeddb": "^5.0.0",
"node-fetch": "^3.3.2",
Expand Down
15 changes: 9 additions & 6 deletions sdk/typescript/packages/mix-fetch-node/rollup-worker.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ export default {
dir: 'dist/cjs',
format: 'cjs',
},
external: ['util', 'fake-indexeddb'],
onwarn,
plugins: [
resolve({
browser: false,
preferBuiltins: true,
extensions: ['.js', '.ts'],
}),
commonjs(),
// TODO: One of the wasm functions calls `new WebSocket` at one point, which we aren't able to polyfill correctly yet.
modify({
find: 'const ret = new WebSocket(getStringFromWasm0(arg0, arg1));',
replace: 'const ws = require("ws"); const ret = new ws.WebSocket(getStringFromWasm0(arg0, arg1));',
}),
// TODO: `getObject(...).require` seems to generate a warning on Webpack but with Rollup we get a panic since it can't require.
// By hard coding the require here, we can workaround that.
// Reference: https://github.com/rust-random/getrandom/issues/224
Expand All @@ -38,3 +33,11 @@ export default {
}),
],
};

function onwarn(warning) {
// fake-indexeddb has a circular dependency that triggers a warning when rolled up
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
// eslint-disable-next-line no-console
console.error(`(!) ${warning.message}`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ set -o errexit
set -o nounset
set -o pipefail

rm -rf ../../../../dist/ts/docs/tsdoc/nymproject/mix-fetch || true
rm -rf ../../../../dist/ts/docs/tsdoc/nymproject/mix-fetch-node || true

# run the build
yarn docs:generate:prod

# move the output outside of the yarn/npm workspaces
mkdir -p ../../../../dist/ts/docs/tsdoc/nymproject
mv docs ../../../../dist/ts/docs/tsdoc/nymproject/mix-fetch
mv docs ../../../../dist/ts/docs/tsdoc/nymproject/mix-fetch-node

echo "Output can be found in:"
realpath ../../../../dist/ts/docs/tsdoc/nymproject/mix-fetch
realpath ../../../../dist/ts/docs/tsdoc/nymproject/mix-fetch-node
6 changes: 3 additions & 3 deletions sdk/typescript/packages/mix-fetch-node/scripts/build-prod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -o nounset
set -o pipefail

rm -rf dist || true
rm -rf ../../../../dist/ts/sdk/mix-fetch || true
rm -rf ../../../../dist/ts/sdk/mix-fetch-node || true

# run the build
scripts/build.sh
Expand All @@ -14,7 +14,7 @@ node scripts/buildPackageJson.mjs
# move the output outside of the yarn/npm workspaces
mkdir -p ../../../../dist/ts/sdk
mv dist ../../../../dist/ts/sdk
mv ../../../../dist/ts/sdk/dist ../../../../dist/ts/sdk/mix-fetch
mv ../../../../dist/ts/sdk/dist ../../../../dist/ts/sdk/mix-fetch-node

echo "Output can be found in:"
realpath ../../../../dist/ts/sdk/mix-fetch
realpath ../../../../dist/ts/sdk/mix-fetch-node
1 change: 0 additions & 1 deletion sdk/typescript/packages/mix-fetch-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-underscore-dangle */

import type { SetupMixFetchOps, IMixFetchFn } from './types';
import { createMixFetch as createMixFetchInternal } from './create-mix-fetch';

Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/packages/mix-fetch-node/src/worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './polyfill';

import { loadWasm } from './wasm-loading';
import { run } from './main';

Expand Down
2 changes: 1 addition & 1 deletion sdk/typescript/packages/mix-fetch-node/src/worker/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-console */
import type { IMixFetchWebWorker, LoadedEvent } from '../types';

import * as Comlink from 'comlink';
import { parentPort } from 'node:worker_threads';
import { setupMixFetch, disconnectMixFetch } from '@nymproject/mix-fetch-wasm-node';

import type { IMixFetchWebWorker, LoadedEvent } from '../types';
import nodeEndpoint from '../node-adapter';
import { EventKinds, ResponseBodyConfigMap, ResponseBodyConfigMapDefaults } from '../types';
import { handleResponseMimeTypes } from './handle-response-mime-types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TextDecoder, TextEncoder } from 'node:util';
import * as crypto from 'node:crypto';
import * as fs from 'node:fs';
import WebSocket from 'ws';
import fetch, { Headers, Request, Response } from 'node-fetch';
import { TextDecoder, TextEncoder } from 'node:util';
import { Worker } from 'node:worker_threads';
import { indexedDB } from 'fake-indexeddb';

Expand All @@ -18,7 +18,6 @@ import { indexedDB } from 'fake-indexeddb';
(globalThis as any).Headers = Headers;
(globalThis as any).Request = Request;
(globalThis as any).Response = Response;
(globalThis as any).Headers = Headers;
(globalThis as any).fs = fs;
(globalThis as any).crypto = crypto;
(globalThis as any).WebSocket = WebSocket;
Expand Down
6 changes: 2 additions & 4 deletions sdk/typescript/packages/nodejs-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ This package is a NodeJS client that uses the wasm from the [Sphinx webassembly
## Usage

```js
const { createNymMixnetClient } = require('../dist/cjs/index.js');
const { createNymMixnetClient } = require('@nymproject/nodejs-client-commonjs');

async () => {
const nym = await createNymMixnetClient();

nym.events.subscribeToTextMessageReceivedEvent(async (e) => {
if (e.args.payload === 'Hello') {
await nym.client.stop();
}
console.log("message received", e.args.payload);
});

const nymApiUrl = 'https://validator.nymtech.net/api/';
Expand Down
25 changes: 25 additions & 0 deletions sdk/typescript/packages/nodejs-client/internal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { createNymMixnetClient } = require('../dist/cjs/index.js');

(async () => {
const nym = await createNymMixnetClient();

nym.events.subscribeToTextMessageReceivedEvent(async ({ args: { payload, mimeType } }) => {
console.log(`received message: ${payload}`);
console.log(`with mimeType: ${mimeType}`);
});

// start the client and connect to a gateway
await nym.client.start({
nymApiUrl: 'https://validator.nymtech.net/api/',
clientId: 'my-client',
});

nym.events.subscribeToConnected(async (e) => {
// send a message to yourself
const message = 'Hello';
const recipient = await nym.client.selfAddress();
console.log('main thread address: ', recipient);
console.log(`sending "${message}" to ourselves...`);
await nym.client.send({ payload: { message, mimeType: 'text/plain' }, recipient });
});
})();
16 changes: 2 additions & 14 deletions sdk/typescript/packages/nodejs-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nymproject/nodejs-client",
"version": "1.2.0-rc.10",
"version": "1.2.1-rc.3",
"license": "Apache-2.0",
"author": "Nym Technologies SA",
"files": [
Expand All @@ -25,29 +25,20 @@
"tsc": "tsc --noEmit true"
},
"dependencies": {
"@nymproject/nym-client-wasm-node": "^1.2.0",
"@nymproject/nym-client-wasm-node": ">=1.2.0 || ^1",
"comlink": "^4.3.1",
"fake-indexeddb": "^4.0.2",
"rollup-plugin-polyfill": "^4.2.0",
"ws": "^8.14.2"
},
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/plugin-transform-async-to-generator": "^7.14.5",
"@babel/preset-env": "^7.15.0",
"@babel/preset-react": "^7.14.5",
"@babel/preset-typescript": "^7.15.0",
"@nymproject/eslint-config-react-typescript": "^1.0.0",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-inject": "^5.0.3",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-terser": "^0.2.1",
"@rollup/plugin-typescript": "^10.0.1",
"@rollup/plugin-url": "^8.0.1",
"@rollup/plugin-wasm": "^6.1.1",
"@types/jest": "^27.0.1",
"@types/node": "^16.7.13",
"@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.13.0",
Expand All @@ -62,16 +53,13 @@
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.29.2",
"eslint-plugin-react-hooks": "^4.3.0",
"handlebars": "^4.7.8",
"jest": "^29.5.0",
"nodemon": "3.0.1",
"reload": "^3.2.1",
"rimraf": "^3.0.2",
"rollup": "^3.9.1",
"rollup-plugin-base64": "^1.0.1",
"rollup-plugin-modify": "^3.0.0",
"rollup-plugin-web-worker-loader": "^1.6.1",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.2",
"typedoc": "^0.24.8",
"typescript": "^4.8.4"
Expand Down
6 changes: 3 additions & 3 deletions sdk/typescript/packages/nodejs-client/rollup-cjs.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable import/no-extraneous-dependencies */
import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import { wasm } from '@rollup/plugin-wasm';
import typescript from '@rollup/plugin-typescript';
import webWorkerLoader from 'rollup-plugin-web-worker-loader';
import replace from '@rollup/plugin-replace';
import { wasm } from '@rollup/plugin-wasm';

export default {
input: 'src/index.ts',
Expand Down
Loading

0 comments on commit 06a96fa

Please sign in to comment.