Skip to content

Commit

Permalink
Merge pull request #290 from skyway/staging
Browse files Browse the repository at this point in the history
Release v4.2.0
  • Loading branch information
y-i committed Dec 22, 2020
2 parents deac18b + df273d0 commit bff3a26
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,12 @@

## release note

## [v4.2.0](https://github.com/skyway/skyway-js-sdk/releases/tag/v4.2.0) - 2020-12-22

### Added

- Added `fetchPeerExists` API to fetch whether a peer exists. You can call this API once per second per peer. ([#288](https://github.com/skyway/skyway-js-sdk/pull/288))

## [v4.1.1](https://github.com/skyway/skyway-js-sdk/releases/tag/v4.1.1) - 2020-12-08

### Fixed
Expand Down
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -13,6 +13,18 @@ Add the following script tag to your html file.
You can then use the `Peer` object to start connecting.
For more details, check out the [Tutorial](https://webrtc.ecl.ntt.com/en/js-tutorial.html). ([日本語](https://webrtc.ecl.ntt.com/js-tutorial.html))

#### How to specify version and minified
By changing the file name, you can use each version and minified version.

Example
- Using version 4.2.0: `skyway-4.2.0.js`.
- Using minified version 4.2.0 : `skyway-4.2.0.min.js`.
- Using the latest minified version: `skyway-latest.min.js`.

#### CAUTION
When using `skyway-latest.js` and `skyway-latest.min.js`, when a new version of the SDK is released, the latest version will automatically be applied to your application. This makes it easy to keep up with browser updates, but depending on the changes in the SDK, your application may be affected.


### Installing using npm to use with a bundler (e.g. webpack or browserify)

With [npm](https://npmjs.org/) installed, run
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "skyway-js",
"version": "4.1.1",
"version": "4.2.0",
"description": "The official JavaScript SDK for SkyWay",
"main": "dist/skyway.js",
"module": "src/peer.js",
Expand Down
30 changes: 30 additions & 0 deletions src/peer.js
Expand Up @@ -303,6 +303,36 @@ class Peer extends EventEmitter {
http.send(null);
}

/**
* Fetch whether peer exists from signaling server
* @param {string} peerId - The PeerId that want to fetch.
* @return {Promise<boolean>} A Promise that resolves if fetch is done.
*/
async fetchPeerExists(peerId) {
if (!this.open) {
throw new Error('Peer is not yet connected to signaling server');
}

const currentTimestamp = new Date().getTime();
if (
this._lastFetchPeerExistsTime !== undefined &&
currentTimestamp - this._lastFetchPeerExistsTime < 1000
) {
throw new Error('fetchPeerExists can only be called once per second');
}
this._lastFetchPeerExistsTime = currentTimestamp;

const url = `${this.socket.signalingServerUrl}/api/apikeys/${this.options.key}/peers/${peerId}/exists`;
const response = await fetch(url);
if (response.status === 200) {
const body = await response.json();
return body.exists;
} else {
const body = await response.text();
throw new Error(body);
}
}

/**
* Return socket open status and emit error when it's not open.
* @return {boolean} - The socket status.
Expand Down
132 changes: 132 additions & 0 deletions tests/peer.js
Expand Up @@ -868,6 +868,138 @@ describe('Peer', () => {
});
});

describe('fetchPeerExists', () => {
let peer;

beforeEach(() => {
peer = new Peer({
key: apiKey,
host: signalingHost,
port: signalingPort,
});
const protocol = peer.options.secure ? 'https://' : 'http://';
peer.socket.signalingServerUrl = `${protocol}${signalingHost}:${signalingPort}`;

sinon.stub(window, 'fetch');
});

afterEach(() => {
window.fetch.restore();
peer.destroy();
});

it('should return false if response is false', async () => {
sinon.stub(peer.socket, 'isOpen').get(() => true);
window.fetch.callsFake(() =>
Promise.resolve(
new window.Response(JSON.stringify({ exists: false }), {
status: 200,
headers: { 'Content-type': 'application/json' },
})
)
);

const exists = await peer.fetchPeerExists('peerId');
assert.equal(exists, false);
});

it('should return true if response is true', async () => {
sinon.stub(peer.socket, 'isOpen').get(() => true);
window.fetch.callsFake(() =>
Promise.resolve(
new window.Response(JSON.stringify({ exists: true }), {
status: 200,
headers: { 'Content-type': 'application/json' },
})
)
);

const exists = await peer.fetchPeerExists('peerId');
assert.equal(exists, true);
});

it('should not throw error if it is called at 1 second intervals', async () => {
sinon.stub(peer.socket, 'isOpen').get(() => true);
window.fetch.callsFake(() =>
Promise.resolve(
new window.Response(JSON.stringify({ exists: true }), {
status: 200,
headers: { 'Content-type': 'application/json' },
})
)
);

await peer.fetchPeerExists('peerId1');
const clock = sinon.useFakeTimers(new Date().getTime() + 1000);
const exists = await peer.fetchPeerExists('peerId2');
clock.restore();

assert.equal(exists, true);
});

it('should throw error if it is called twice per second', async () => {
sinon.stub(peer.socket, 'isOpen').get(() => true);
window.fetch.callsFake(() =>
Promise.resolve(
new window.Response(JSON.stringify({ exists: false }), {
status: 200,
headers: { 'Content-type': 'application/json' },
})
)
);

await peer.fetchPeerExists(peerId);
try {
await peer.fetchPeerExists(peerId);
} catch (e) {
assert(e instanceof Error);
assert.equal(
e.message,
'fetchPeerExists can only be called once per second'
);
return;
}
assert.fail("Didn't throw an error");
});

it('should throw error if peer is not open', async () => {
sinon.stub(peer.socket, 'isOpen').get(() => false);

try {
await peer.fetchPeerExists(peerId);
} catch (e) {
assert(e instanceof Error);
assert.equal(
e.message,
'Peer is not yet connected to signaling server'
);
return;
}
assert.fail("Didn't throw an error");
});

it('should throw error if status code is not 200', async () => {
sinon.stub(peer.socket, 'isOpen').get(() => true);
window.fetch.callsFake(() =>
Promise.resolve(
new window.Response('Internal Server Error', {
status: 500,
headers: { 'Content-type': 'application/json' },
})
)
);

try {
await peer.fetchPeerExists(peerId);
} catch (e) {
assert(e instanceof Error);
assert.equal(e.message, 'Internal Server Error');
return;
}
assert.fail("Didn't throw an error");
});
});

describe('_checkOpenStatus', () => {
const peerId = 'testPeerId';
let peer;
Expand Down

0 comments on commit bff3a26

Please sign in to comment.