diff --git a/.gitignore b/.gitignore index 9de57fb..dee4bfa 100644 --- a/.gitignore +++ b/.gitignore @@ -203,5 +203,4 @@ Cargo.lock **/*.rs.bk Cargo.lock bin/ -pkg*/ wasm-pack.log diff --git a/Makefile b/Makefile index 48cff75..cb5818f 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,10 @@ -TARGET := bundler - -all: - make build - make build TARGET=deno +JS := js build: - wasm-pack build --target $(TARGET) --out-dir pkg-$(TARGET) --release --no-pack --weak-refs && cp js-assets/* pkg-$(TARGET)/ + $(MAKE) -C $(JS) build -publish: all - cp js-assets/README.md . && npx jsr publish --allow-slow-types --allow-dirty && rm README.md +test: build + $(MAKE) -C $(JS) test install: - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh - -test: - deno test -A app.test.ts - echo "https://github.com/rustwasm/wasm-pack/pull/1061" - node --experimental-vm-modules --trace-warnings --experimental-wasm-modules app.test.js + $(MAKE) -C $(JS) install diff --git a/js-assets/index.ts b/js-assets/index.ts deleted file mode 100644 index 42b13d5..0000000 --- a/js-assets/index.ts +++ /dev/null @@ -1,155 +0,0 @@ -import * as wasm_module from './pulsebeam_core.js'; -import './pulsebeam_core.d.ts'; - -/** - * @pulsebeam/server: Open-Source Server SDK. Use @pulsebeam/server to generate - * JWT (tokens) for your @pulsebeam/peer clients to use. - * - * For more on @pulsebeam/server: {@link https://jsr.io/@pulsebeam/server} - * - * For more on @pulsebeam/peer: {@link https://jsr.io/@pulsebeam/peer} - * - * For more on PulseBeam: {@link https://pulsebeam.dev/} - * - * For more on JWTs and Claims, see the RFC: {@link https://datatracker.ietf.org/doc/html/rfc7519} - * - * # Example Usage - * - * ```ts - * // Step 1: Initialize app - * const { APP_ID, APP_SECRET } = process.env; - * const app = new App(APP_ID, APP_SECRET); - * - * // Step 2: Listen for JWT requests from your clients' - * router.post('/auth', (req, res) => { - * // Step 3: Generate JWT and respond with JWT - * const claims = new PeerClaims("myGroup1", "myPeer1"); - * const rule = new FirewallClaims("myGroup*", "*"); - * claims.setAllowIncoming0(rule); - * claims.setAllowOutgoing0(rule); - * - * const ttlSeconds = 3600; - * const token = app.createToken(claims, ttlSeconds); - * res.json({ groupId, peerId, token }); - * }); - * ``` - * - * @module - */ - -/** - * Represents the main application instance. - * Get an app_id and app_secret from {@link https://pulsebeam.dev} - * - * @example - * const { APP_ID, APP_SECRET } = process.env; - * const app = new App(APP_ID, APP_SECRET); - * - * router.post('/auth', (req, res) => { - * const claims = new PeerClaims("myGroup1", "myPeer1"); - * const rule = new FirewallClaims("myGroup*", "*"); - * claims.setAllowIncoming0(rule); - * claims.setAllowOutgoing0(rule); - * - * const ttlSeconds = 3600; // 1 hour - * const token = app.createToken(claims, ttlSeconds); - * res.json({ groupId, peerId, token }); - * }); - */ -export class App { - private _internal: wasm_module.App - /** - * Creates a new App instance using your config. Essential for creating - * client tokens. - * Get an app_id and app_secret from {@link https://pulsebeam.dev} - * - * @param {string} app_id - app_id your application ID - * @param {string} app_secret - app_secret your application secret - * @example const app = new App(MY_APP_ID, MY_APP_SECRET); - */ - constructor(app_id: string, app_secret: string) { - this._internal = new wasm_module.App(app_id, app_secret); // Keep the internal WASM instance - } - /** - * Create a JWT. The JWT should be used by your client-side application. - * To learn about JWTs and claims see JWT RFC {@link https://datatracker.ietf.org/doc/html/rfc7519} - * @param {PeerClaims} claims - The peer claims to include in the token. - * @param {number} durationSecs - TTL duration before token expiration - * @return {string} JWT - The generated JWT token as a string. - * @throws {Error} When token creation fails. Likely an issue with your - * AppSecret. - * @example const token = app.createToken(claims, ttlSeconds); - */ - createToken(claims: PeerClaims, durationSecs: number): string { - return this._internal.createToken(claims._internal, durationSecs); - } -} - - -/** - * Represents FirewallClaims for controlling network access. - * To learn about claims see JWT RFC {@link https://datatracker.ietf.org/doc/html/rfc7519} - * - */ -export class FirewallClaims { - public _internal: wasm_module.FirewallClaims - /** - * Creates a new FirewallClaims instance. - * - * @typedef {string} Rule - Must be - * - Is a valid UTF-8 string - * - length(string) >= 1 character - * - Contains no more than 1 wildcard (*) - * - * Regex: /^(?:[^*]*\*[^*]*|[^*]+)$/g - * - * Examples: ["*", "myGroup", "*bob", "my*"] - * - * @param {Rule} group_id_rule - Desired rule for allowed groupIds - * @param {Rule} peer_id_rule - Desired for allowed peerIds - * @example const rule = new FirewallClaims("myGroup*", "*"); - */ - constructor(group_id_rule: string, peer_id_rule: string) { - this._internal = new wasm_module.FirewallClaims(group_id_rule, peer_id_rule); - } -} - -/** - * Represents peer claims for controlling access. - * To learn about claims see JWT RFC {@link https://datatracker.ietf.org/doc/html/rfc7519} - */ -export class PeerClaims { - public _internal: wasm_module.PeerClaims - /** - * Construct a new PeerClaims instance - * - * Strings must be valid UTF-8 and at least 1 character - * - * @param {string} group_id - Identifier for the group which the peer belongs to. - * @param {string} peer_id - Identifier for the peer. - * @example const claims = new PeerClaims("myGroup1", "myPeer1"); - */ - constructor(group_id: string, peer_id: string) { - this._internal = new wasm_module.PeerClaims(group_id, peer_id); - } - - /** - * Configure allowlist for incoming traffic - * @param {FirewallClaims} claims - FirewallClaims instance representing - * the incoming rule. - * @example myClaims.setAllowIncoming0(myRule); - */ - setAllowIncoming(claims: FirewallClaims) { - this._internal.setAllowIncoming0(claims._internal); - } - - /** - * Configure allowlist for outgoing traffic - * @param {FirewallClaims} claims - FirewallClaims instance representing - * the outgoing rule. - * @example myClaims.setAllowOutgoing0(myRule); - */ - setAllowOutgoing(claims: FirewallClaims) { - this._internal.setAllowOutgoing0(claims._internal); - } -} \ No newline at end of file diff --git a/js/.gitignore b/js/.gitignore new file mode 100644 index 0000000..e1e0ec0 --- /dev/null +++ b/js/.gitignore @@ -0,0 +1,2 @@ +pkg*/ +docs diff --git a/js/Makefile b/js/Makefile new file mode 100644 index 0000000..83644a5 --- /dev/null +++ b/js/Makefile @@ -0,0 +1,23 @@ +TARGET := bundler + +build: + $(MAKE) build-target + $(MAKE) build-target TARGET=deno + +build-target: + wasm-pack build --target $(TARGET) --out-dir $(PWD)/pkg-$(TARGET) --release --no-pack --weak-refs + +test: + deno test -A app.test.ts + echo "https://github.com/rustwasm/wasm-pack/pull/1061" + node --experimental-vm-modules --trace-warnings --experimental-wasm-modules app.test.js + +doc: build + deno doc --html ./pkg-bundler/pulsebeam_core.js + python3 -m http.server -d docs + +publish: all + npx jsr publish --allow-slow-types + +install: + curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh diff --git a/js-assets/README.md b/js/README.md similarity index 100% rename from js-assets/README.md rename to js/README.md diff --git a/app.test.js b/js/app.test.js similarity index 100% rename from app.test.js rename to js/app.test.js diff --git a/app.test.ts b/js/app.test.ts similarity index 100% rename from app.test.ts rename to js/app.test.ts diff --git a/js/jsr.json b/js/jsr.json new file mode 100644 index 0000000..39324c5 --- /dev/null +++ b/js/jsr.json @@ -0,0 +1,16 @@ +{ + "name": "@pulsebeam/server", + "version": "0.0.8", + "nodeModulesDir": "auto", + "exports": { + "./node": "./pkg-bundler/pulsebeam_core.js", + "./deno": "./pkg-deno/pulsebeam_core.js" + }, + "exclude": [ + "!pkg-bundler/", + "!pkg-deno/", + "src", + "tests", + "examples" + ] +} diff --git a/jsr.json b/jsr.json deleted file mode 100644 index ed61745..0000000 --- a/jsr.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "@pulsebeam/server", - "version": "0.0.9", - "nodeModulesDir": "auto", - "exports": { - "./node": "./pkg-bundler/index.ts", - "./deno": "./pkg-deno/index.ts" - }, - "publish": { - "exclude": [ - "!pkg-bundler/", - "!pkg-deno/", - "src", - "tests", - "Cargo.toml", - "examples" - ] - } -} diff --git a/src/lib.rs b/src/lib.rs index 37ded1f..4a7194f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,7 @@ impl From for AppError { } } -/// Represents peer claims for controlling access. +/// Represents peer claims for controlling access. Hello /// To learn about claims see JWT RFC {@link https://datatracker.ietf.org/doc/html/rfc7519} /// free() - internal method do not use #[wasm_bindgen] @@ -82,8 +82,8 @@ impl PeerClaims { ..Self::default() } } - - /// Configure allowlist for incoming traffic + + /// Configure allowlist for incoming traffic /// @param {FirewallClaims} claims - FirewallClaims instance representing /// the incoming rule. /// @example myClaims.setAllowIncoming0(myRule); @@ -91,8 +91,8 @@ impl PeerClaims { pub fn set_allow_incoming_0(&mut self, claims: &FirewallClaims) { self.allow_incoming_0 = Some(claims.clone()); } - - /// Configure allowlist for outgoing traffic + + /// Configure allowlist for outgoing traffic /// @param {FirewallClaims} claims - FirewallClaims instance representing /// the outgoing rule. /// @example myClaims.setAllowOutgoing0(myRule); @@ -121,9 +121,9 @@ impl FirewallClaims { /// - Is a valid UTF-8 string /// - length(string) >= 1 character /// - Contains no more than 1 wildcard (*) - /// + /// /// Regex: /^(?:[^*]*\*[^*]*|[^*]+)$/g - /// + /// /// Examples: ["*", "myGroup", "*bob", "my*"] /// /// @param {Rule} group_id_rule - Desired rule for allowed groupIds @@ -180,7 +180,7 @@ impl App { app_secret: app_secret.to_owned(), } } - + /// Create a JWT. The JWT should be used by your client-side application. /// To learn about JWTs and claims see JWT RFC {@link https://datatracker.ietf.org/doc/html/rfc7519} /// @param {PeerClaims} claims - The peer claims to include in the token.