Skip to content

Commit cea8897

Browse files
committed
feat: add docs/tests
1 parent a4a6577 commit cea8897

File tree

4 files changed

+67
-29
lines changed

4 files changed

+67
-29
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ yarn add @jswork/message-bridge
1515
```js
1616
import messageBridge from '@jswork/message-bridge';
1717

18-
messageBridge(1024);
19-
20-
// [1000, 0, 20, 4]
18+
// Register a handler for incoming messages
19+
messageBridge.registerHandler('message:in', (data, responseCallback) => {
20+
console.log('Received data:', data);
21+
// Send response back
22+
responseCallback({ status: 'success' });
23+
});
24+
25+
// Send a message and handle the response
26+
messageBridge.callHandler('message:out', { type: 'request', payload: 'Hello!' }, (response) => {
27+
console.log('Got response:', response);
28+
});
2129
```
2230

2331
## license

__tests__/index.spec.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
1-
import fn from '../src/index.js';
1+
import { Encoder } from '../src/index';
22

3-
describe('Normal test cases', () => {
4-
test('number is equal 0/10/100/1000/10000', () => {
5-
expect(fn(0)).toEqual([0]);
6-
expect(fn(10)).toEqual([10, 0]);
7-
expect(fn(30)).toEqual([30, 0]);
8-
expect(fn(40)).toEqual([40, 0]);
9-
expect(fn(50)).toEqual([50, 0]);
10-
expect(fn(60)).toEqual([60, 0]);
11-
expect(fn(70)).toEqual([70, 0]);
12-
expect(fn(80)).toEqual([80, 0]);
13-
expect(fn(90)).toEqual([90, 0]);
14-
expect(fn(100)).toEqual([100, 0, 0]);
15-
expect(fn(1000)).toEqual([1000, 0, 0, 0]);
16-
expect(fn(10000)).toEqual([10000, 0, 0, 0, 0]);
3+
describe('Encoder', () => {
4+
describe('encode', () => {
5+
test('should encode string correctly', () => {
6+
expect(Encoder.encode('hello')).toBe('aGVsbG8=');
7+
});
8+
9+
test('should encode object correctly', () => {
10+
const obj = { name: 'test', value: 123 };
11+
expect(Encoder.encode(obj)).toBe('eyJuYW1lIjoidGVzdCIsInZhbHVlIjoxMjN9');
12+
});
13+
14+
test('should encode array correctly', () => {
15+
const arr = [1, 'test', true];
16+
expect(Encoder.encode(arr)).toBe('WzEsInRlc3QiLHRydWVd');
17+
});
18+
});
19+
20+
describe('decode', () => {
21+
test('should decode string correctly', () => {
22+
expect(Encoder.decode('aGVsbG8=')).toBe('hello');
23+
});
24+
25+
test('should decode JSON object correctly', () => {
26+
const encoded = 'eyJuYW1lIjoidGVzdCIsInZhbHVlIjoxMjN9';
27+
expect(Encoder.decode(encoded)).toEqual({ name: 'test', value: 123 });
28+
});
29+
30+
test('should decode JSON array correctly', () => {
31+
const encoded = 'WzEsInRlc3QiLHRydWVd';
32+
expect(Encoder.decode(encoded)).toEqual([1, 'test', true]);
33+
});
34+
35+
test('should return decoded string when JSON parse fails', () => {
36+
const encoded = 'aGVsbG8=';
37+
expect(Encoder.decode(encoded)).toBe('hello');
38+
});
1739
});
1840
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jswork/message-bridge",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Web/h5/mp bridge for web/mp.",
55
"type": "module",
66
"npmClient": "pnpm",

src/index.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,24 @@ export interface HandlerCleanup {
1212
destroy: () => void;
1313
}
1414

15+
export class Encoder {
16+
public static encode = (obj: any) => {
17+
if (typeof obj === 'string') return Base64.encode(obj);
18+
return Base64.encode(JSON.stringify(obj));
19+
};
20+
21+
public static decode = (str: string) => {
22+
const decoded = Base64.decode(str);
23+
try {
24+
return JSON.parse(decoded);
25+
} catch (e) {
26+
return decoded;
27+
}
28+
};
29+
}
30+
1531
const callHandler = (inName: string, inPayload: any, inOptions: CallHandlerOptions = {}) => {
16-
const _payload = typeof inPayload === 'string' ? inPayload : JSON.stringify(inPayload);
17-
const payload = Base64.encode(_payload);
32+
const payload = Encoder.encode(inPayload);
1833
const defaultContext = typeof window !== 'undefined' ? window : ({} as any);
1934
const options = { ...inOptions, context: defaultContext };
2035
options.context.postMessage({ name: inName, payload });
@@ -31,14 +46,7 @@ const registerHandler = (
3146
const { name, payload } = event.data;
3247
if (name === inName) {
3348
const decodedPayload = Base64.decode(payload);
34-
let data: string | null = null;
35-
36-
try {
37-
data = JSON.parse(decodedPayload);
38-
} catch (e) {
39-
data = decodedPayload;
40-
}
41-
49+
const data = Encoder.decode(decodedPayload);
4250
inHandler(data, (response) => {
4351
const responseName = `${inName}:response`;
4452
callHandler(responseName, response, options);

0 commit comments

Comments
 (0)