Skip to content

Commit

Permalink
feat: add ws provider package (#2578)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanleecode authored Nov 27, 2024
1 parent 15c5a58 commit e6825af
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 3 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
"overrides": {
"@substrate/connect": "workspace:^",
"smoldot": "^2.0.34"
},
"patchedDependencies": {
"@polkadot-api/ws-provider": "patches/@polkadot-api__ws-provider.patch"
}
}
}
1 change: 1 addition & 0 deletions packages/ws-provider/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
1 change: 1 addition & 0 deletions packages/ws-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @substrate/ws-provider
53 changes: 53 additions & 0 deletions packages/ws-provider/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@substrate/ws-provider",
"version": "0.3.6",
"author": "Parity Technologies (https://github.com/paritytech)",
"repository": {
"type": "git",
"url": "git+https://github.com/paritytech/substrate-connect.git"
},
"license": "MIT",
"type": "module",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"@substrate-connect/source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"module": "./dist/esm/index.js",
"files": [
"dist"
],
"scripts": {
"deep-clean": "rimraf dist node_modules",
"build": "tsup",
"typecheck": "tsc --noEmit",
"dev": "pnpm build --watch",
"lint": "prettier --check README.md \"src/**/*.{js,jsx,ts,tsx,json,md}\""
},
"prettier": {
"printWidth": 80,
"semi": false,
"trailingComma": "all"
},
"dependencies": {
"@polkadot-api/ws-provider": "0.3.6",
"@polkadot-api/json-rpc-provider-proxy": "0.2.4",
"@polkadot-api/json-rpc-provider": "0.0.4"
},
"devDependencies": {
"typescript": "5.6.2",
"vitest": "^2.1.4"
},
"peerDependencies": {}
}
41 changes: 41 additions & 0 deletions packages/ws-provider/src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
getInternalWsProvider,
type StatusChange,
type WsJsonRpcProvider,
} from "@polkadot-api/ws-provider/web"

export namespace GetWsProvider {
export type Options = {
endpoints: Array<
| string
| {
uri: string
protocol: string[]
}
>
onStatusChanged?: (status: StatusChange) => void
timeout?: number
websocketConstructor?: typeof globalThis.WebSocket
}

export type GetWsProvider = (options: Options) => WsJsonRpcProvider
}

export const getWsProvider: GetWsProvider.GetWsProvider = (options) => {
const wsProvider = getInternalWsProvider(
options.websocketConstructor ?? globalThis.WebSocket,
)

return wsProvider(options)
}

export {
WsEvent,
type WsConnecting,
type WsConnected,
type WsError,
type WsClose,
type StatusChange,
type JsonRpcProvider,
type WsJsonRpcProvider,
} from "@polkadot-api/ws-provider/web"
8 changes: 8 additions & 0 deletions packages/ws-provider/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.build.json",
"include": ["src"],
"compilerOptions": {
"incremental": false,
"composite": false
}
}
4 changes: 4 additions & 0 deletions packages/ws-provider/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src", "tests"]
}
36 changes: 36 additions & 0 deletions packages/ws-provider/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { defineConfig } from "tsup"

export default defineConfig([
{
entry: {
index: "src/mod.ts",
},
outDir: "dist/esm",
format: ["esm"],
dts: true,
sourcemap: true,
clean: true,
tsconfig: "./tsconfig.build.json",
noExternal: ["@polkadot-api/ws-provider"],
external: [
"@polkadot-api/json-rpc-provider-proxy",
"@polkadot-api/json-rpc-provider",
],
},
{
entry: {
index: "src/mod.ts",
},
outDir: "dist/commonjs",
format: ["cjs"],
dts: true,
sourcemap: true,
clean: true,
tsconfig: "./tsconfig.build.json",
noExternal: ["@polkadot-api/ws-provider"],
external: [
"@polkadot-api/json-rpc-provider-proxy",
"@polkadot-api/json-rpc-provider",
],
},
])
33 changes: 33 additions & 0 deletions patches/@polkadot-api__ws-provider.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
diff --git a/dist/web/esm/web.mjs b/dist/web/esm/web.mjs
index 241ec3bf8ebec5d7cfee0dc98fd95309b1298135..6aa173d56ed6702387325974b4ef912a5168abe9 100644
--- a/dist/web/esm/web.mjs
+++ b/dist/web/esm/web.mjs
@@ -3,5 +3,5 @@ export { WsEvent } from './types.mjs';

const getWsProvider = getInternalWsProvider(WebSocket);

-export { getWsProvider };
+export { getWsProvider, getInternalWsProvider };
//# sourceMappingURL=web.mjs.map
diff --git a/dist/web/web.d.ts b/dist/web/web.d.ts
index d426010c36aa1ef4696409cc81036597d15f43b3..dc525d2d35ee5011eaeb9c0519ebe3f91dbbd2fa 100644
--- a/dist/web/web.d.ts
+++ b/dist/web/web.d.ts
@@ -49,5 +49,6 @@ interface GetWsProviderInput {
}

declare const getWsProvider: GetWsProviderInput;
+declare const getInternalWsProvider: (wsCtor: typeof globalThis.WebSocket) => GetWsProviderInput;

-export { type GetWsProviderInput, type StatusChange, type WsClose, type WsConnected, type WsConnecting, type WsError, WsEvent, type WsJsonRpcProvider, type WsProviderConfig, getWsProvider };
+export { type GetWsProviderInput, type StatusChange, type WsClose, type WsConnected, type WsConnecting, type WsError, WsEvent, type WsJsonRpcProvider, type WsProviderConfig, getWsProvider, getInternalWsProvider };
diff --git a/dist/web/web.js b/dist/web/web.js
index 0fa7cd8079fb1834bda11013eca4af81b3c12106..d533a1bd65deb63e7e3d1c5f690cacdf7ffbda15 100644
--- a/dist/web/web.js
+++ b/dist/web/web.js
@@ -227,4 +227,5 @@ const getWsProvider = getInternalWsProvider(WebSocket);

exports.WsEvent = WsEvent;
exports.getWsProvider = getWsProvider;
+exports.getInternalWsProvider = getInternalWsProvider;
//# sourceMappingURL=web.js.map
30 changes: 27 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit e6825af

Please sign in to comment.