Commit Graph

1690 Commits

Author SHA1 Message Date
Damien Arrachequesne
ccf7998cc5 feat: add timeout feature
Usage:

```js
socket.timeout(5000).emit("my-event", (err) => {
  if (err) {
    // the server did not acknowledge the event in the given delay
  }
});
```
2021-11-16 19:56:44 +01:00
Damien Arrachequesne
da0b8282f3 chore(release): 4.3.2
Diff: https://github.com/socketio/socket.io-client/compare/4.3.1...4.3.2
2021-10-17 01:13:27 +02:00
Damien Arrachequesne
6780f29624 fix: restore the default export (bis)
The previous commit, while successfully restoring support for:

```js
const socket = require("socket.io-client")(...);
```

breaks for some other cases:

- https://github.com/socketio/socket.io/issues/4128
- https://github.com/socketio/socket.io-client/issues/1509

According to [1], we should use `export = `, but this is not supported
by module "esnext":

> Export assignment cannot be used when targeting ECMAScript modules

So we'll go for this ugly workaround, at least until we remove the
default export in the next major release.

[1]: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
2021-10-17 01:00:28 +02:00
Damien Arrachequesne
ca614b28f8 chore(release): 4.3.1
Diff: https://github.com/socketio/socket.io-client/compare/4.3.0...4.3.1
2021-10-16 01:13:08 +02:00
Damien Arrachequesne
f0aae8457a fix: restore the default export
The default export was accidentally removed in [1].

Note: that being said, users are encouraged to use the named exports,
because the default export has a different meaning for CommonJS and ES
modules users.

```js
// BAD
import io from "socket.io-client";

// GOOD
import { io } from "socket.io-client";
```

Related:

- https://github.com/socketio/socket.io-client/issues/1505
- https://github.com/socketio/socket.io-client/issues/1507
- https://github.com/socketio/socket.io-client/issues/1508

[1]: 16b65698ae
2021-10-16 01:09:43 +02:00
Chris Krycho
8737d0ae6f fix: restore the namespace export
This restores the previous behavior, where the "io" object available in
the browser could be used as a function (`io()`) or as a namespace
(`io.connect()`).

The breaking change was introduced in [1].

Related: https://github.com/socketio/socket.io/issues/4121

[1]: 16b65698ae
2021-10-16 00:52:31 +02:00
Damien Arrachequesne
c76d36778d chore(release): 4.3.0
Diff: https://github.com/socketio/socket.io-client/compare/4.2.0...4.3.0
2021-10-14 14:37:33 +02:00
Damien Arrachequesne
91b948b860 refactor: move the typed events to @socket.io/component-emitter
The typed events have been moved to [1] in order to remove the
intermediary class and reduce the bundle size.

Diff: https://github.com/socketio/emitter/compare/2.0.0...3.0.0

[1]: https://github.com/socketio/emitter/
2021-10-14 14:09:23 +02:00
Abd Ul-Hameed Maree
a9e5b85580 feat(typings): add missing types for some emitter methods (#1502)
Co-authored-by: Abd UlHameed Maree <abd.maree@scandinaviatech.com>
2021-10-14 10:58:32 +02:00
Damien Arrachequesne
0661564dc2 chore: migrate to rollup
This change allows us to:

- reduce the size of the bundle
- provide an ESM bundle (for usage in <script type="module">)

Related: https://github.com/socketio/socket.io-client/issues/1198
2021-10-13 18:09:41 +02:00
Damien Arrachequesne
16b65698ae feat: provide an ESM build with and without debug
See also: 00d7e7d7ee

Related:

- https://github.com/socketio/socket.io-client/issues/1188
- https://github.com/socketio/socket.io-client/issues/1378
2021-10-13 18:09:41 +02:00
Damien Arrachequesne
718745305f chore: bump socket.io-parser to version 4.1.0
Diff: https://github.com/socketio/socket.io-parser/compare/4.0.4...4.1.0
2021-10-11 23:19:51 +02:00
Damien Arrachequesne
91fbd47e1e chore: bump engine.io-client to version 6.0.0
Release notes: https://github.com/socketio/engine.io-client/releases/6.0.0
Diff: https://github.com/socketio/engine.io-client/compare/5.2.0...6.0.0
2021-10-11 23:19:51 +02:00
Damien Arrachequesne
0a7efc8217 chore(release): 4.2.0
Diff: https://github.com/socketio/socket.io-client/compare/4.1.3...4.2.0
2021-08-30 09:15:30 +02:00
Damien Arrachequesne
ec3a784fa6 chore: bump dependencies 2021-08-30 08:21:47 +02:00
Damien Arrachequesne
66e00b7dd7 fix(typings): allow async listener in typed events
So that:

```ts
socket.on("my-event", async () => {
  // ...
});
```

is valid under the @typescript-eslint/no-misused-promises rule.

Related: https://github.com/socketio/socket.io-client/issues/1486
2021-08-30 08:21:47 +02:00
Michael Vartan
4e1b65699d feat: add an option to use native timer functions (#1479)
This allows to control the behavior of mocked timers (@sinonjs/fake-timers),
depending on the value of the "useNativeTimers" option:

- true: use native setTimeout function
- false (default): use classic timers, that may be mocked

Related: 5d1d5bea11
2021-08-29 09:31:25 +02:00
anshul singh
f3acddf997 refactor: remove duplicate initilializations (#1489)
The attributes were already initialized, resulting in duplicate lines
in the final bundle.

Related: https://github.com/socketio/socket.io/issues/4063
2021-08-28 09:25:36 +02:00
vks-jrobertson
dfb46b55a5 fix: allow to set randomizationFactor to 0 (#1447)
Documentation: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing

Related: https://github.com/socketio/socket.io-client/issues/1231
2021-07-29 09:59:03 +02:00
Damien Arrachequesne
7326bd50df chore(release): 4.1.3
Diff: https://github.com/socketio/socket.io-client/compare/4.1.2...4.1.3
2021-07-10 12:03:09 +02:00
Damien Arrachequesne
cef471bb93 chore: bump dependencies
`engine.io-client` diff: https://github.com/socketio/engine.io-client/compare/5.1.1...5.1.2
2021-07-04 00:53:16 +02:00
Damien Arrachequesne
b466c6f0b1 chore(release): 4.1.2
Diff: https://github.com/socketio/socket.io-client/compare/4.1.1...4.1.2
2021-05-17 23:08:31 +02:00
javiergonzalezGenially
35d27df2ae fix(typings): add missing closeOnBeforeunload option (#1469) 2021-05-17 22:01:39 +02:00
Poyan Nabati
c8dfbb1c1d fix(typings): add missing requestTimeout option (#1467) 2021-05-17 00:39:29 +02:00
Damien Arrachequesne
7d6a71c91d chore(release): 4.1.1
Diff: https://github.com/socketio/socket.io-client/compare/4.1.0...4.1.1
2021-05-11 23:54:24 +02:00
Damien Arrachequesne
411499ebcf chore(release): 4.1.0
Diff: https://github.com/socketio/socket.io-client/compare/4.0.2...4.1.0
2021-05-11 09:16:28 +02:00
Damien Arrachequesne
70220b5333 chore: bump engine.io-client dependency
Diff: https://github.com/socketio/engine.io-client/compare/5.0.1...5.1.0
2021-05-11 09:12:12 +02:00
Damien Arrachequesne
6279065209 chore(release): 4.0.2
Diff: https://github.com/socketio/socket.io-client/compare/4.0.1...4.0.2
2021-05-06 14:31:00 +02:00
Damien Arrachequesne
5394669488 fix(typings): add fallback to untyped event listener
See also: a11152f42b

Related:

- https://github.com/socketio/socket.io/issues/3885
- https://github.com/socketio/socket.io/issues/3872
- https://github.com/socketio/socket.io/issues/3833
2021-05-06 14:24:48 +02:00
Damien Arrachequesne
e20d487ac0 fix: properly export the Socket class
Before this change, `require("socket.io-client").Socket` would return
"undefined".

Note: having access to the Socket class allows users to modify its
prototype.

Related: https://github.com/socketio/socket.io/issues/3726
2021-05-06 14:24:08 +02:00
Damien Arrachequesne
34f822f783 fix: ensure buffered events are sent in order
Before this commit, an event sent in the "connect" handler could be
sent before the events that were buffered while disconnected.

```js
socket.on("connect", () => {
  socket.emit("bar");
});

socket.emit("foo"); // buffered while disconnected
```

In the example above, the "bar" event was sent first, which is not
correct.

Related: https://github.com/socketio/socket.io-client/issues/1458
2021-05-06 14:23:25 +02:00
Damien Arrachequesne
dd2a8fce00 fix: ensure connections are properly multiplexed
When passing the same `options` argument for two distinct Socket
instances, a new Manager was created:

```js
const opts = {};

const socket1 = io("/foo", opts);
const socket2 = io("/bar", opts);

console.log(socket1.io === socket2.io); // false
```

This bug was introduced by [1].

Note: the `options` argument is modified at the `socket.io-client`
level (path, query) and at the `engine.io-client` level (host, port),
which may not be optimal.

[1]: 7a0c2b504f

Related: https://github.com/socketio/socket.io/issues/3898
2021-05-06 14:22:54 +02:00
YuLe
7e81e66b2f docs: update README links (#1459) 2021-04-07 10:01:43 +02:00
Damien Arrachequesne
2edf0f308c chore(release): 4.0.1
Diff: https://github.com/socketio/socket.io-client/compare/4.0.0...4.0.1
2021-04-01 01:07:04 +02:00
Damien Arrachequesne
86f28c3204 chore: bump engine.io-client version
Diff: https://github.com/socketio/engine.io-client/compare/5.0.0...5.0.1
2021-04-01 01:02:25 +02:00
divlo
c15022347c fix(typings): make auth property public (#1455)
Related: https://github.com/socketio/socket.io-client/issues/1453
2021-04-01 00:32:21 +02:00
alex.zeng
48f573f6f6 fix(typings): update definition to match wrapper.mjs (#1456) 2021-04-01 00:31:43 +02:00
Damien Arrachequesne
d28cde7afc chore(release): 4.0.0
The major bump is due to some breaking changes on the server side.

Diff: https://github.com/socketio/socket.io-client/compare/3.1.2...4.0.0
2021-03-10 12:25:27 +01:00
Damien Arrachequesne
43613d1b2c fix(bundle): restore support for JS modules
This change is needed so the bundle can be used with:

```
<script type="module" src="xxxx/socket.io.js"></script>
```

Related:

- https://github.com/socketio/socket.io/discussions/3828
- 13b32b39a4
- 8c08c5d5c3
2021-03-10 12:17:52 +01:00
KC Erb
6abfa1fa4c feat: add autoUnref option
With autoUnref set to true (default: false), the Socket.IO client will
allow the program to exit if there is no other active timer/socket in
the event system.

```js
const socket = io({
  autoUnref: true
});
```

Note: this option only applies to Node.js clients.

Related: https://github.com/socketio/socket.io-client/issues/1446
2021-03-10 12:16:12 +01:00
Damien Arrachequesne
59023657a0 feat: add support for typed events
Syntax:

```ts
interface ServerToClientEvents {
  "my-event": (a: number, b: string, c: number[]) => void;
}

interface ClientToServerEvents {
  hello: (message: string) => void;
}

const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io();

socket.emit("hello", "world");

socket.on("my-event", (a, b, c) => {
  // ...
});
```

The events are not typed by default (inferred as any), so this change
is backward compatible.

Related: https://github.com/socketio/socket.io/issues/3742
2021-03-10 01:24:56 +01:00
Damien Arrachequesne
78ec5a6e43 chore(release): 3.1.2
Diff: https://github.com/socketio/socket.io-client/compare/3.1.1...3.1.2
2021-02-26 01:10:30 +01:00
Damien Arrachequesne
83a65be78a chore: bump engine.io-client version
Diff: https://github.com/socketio/engine.io-client/compare/4.1.0...4.1.2
2021-02-26 01:08:39 +01:00
Damien Arrachequesne
13b32b39a4 fix: restore support for web workers
A previous change ([1], included in 3.0.5) broke support for web
workers, which threw "window is not defined" when importing the bundle
in a web worker.

Related: https://github.com/socketio/socket.io/issues/3809

Reference: https://webpack.js.org/configuration/output/#outputglobalobject

[1]: 8c08c5d5c3
2021-02-26 00:43:26 +01:00
Damien Arrachequesne
311c5d14c3 chore(release): 3.1.1
Diff: https://github.com/socketio/socket.io-client/compare/3.1.0...3.1.1
2021-02-03 22:36:17 +01:00
Damien Arrachequesne
7a0c2b504f fix: include the path in the manager ID
Previously, the following code:

```js
const socket1 = io({
  path: "/test1"
});
const socket2 = io({
  path: "/test2"
});
```

would result in one single Manager, with the "/test2" path being
silently ignored.

Two distinct Manager instances will now be created.

Related: https://github.com/socketio/socket.io-client/issues/1225
2021-02-03 22:31:01 +01:00
Damien Arrachequesne
61afc5d8cb fix: remove polyfill for process in the bundle
A polyfill for Node.js "process" was included in the final bundle.

Reference: https://webpack.js.org/configuration/node/
2021-02-02 12:08:26 +01:00
david-fong
47f917afdd fix(typings): add return types and general-case overload signatures (#1440) 2021-02-02 11:29:08 +01:00
Mick Lawitzke
f02ab3bc96 fix(typings): fix the type of the "query" option (#1439)
Having type `Object` it was not possible to set values, e.g.:

```ts
if (!this.socket.io.opts.query) {
  this.socket.io.opts.query = {};
}
this.socket.io.opts.query.token = 'abc123';
```

Results in error:

> Element implicitly has an 'any' type because expression of type '"token"' can't be used to index type 'Object'.
2021-02-02 11:26:33 +01:00
Damien Arrachequesne
be81a2ce6f chore: point towards the master branch for the CI badge 2021-01-28 14:17:28 +01:00