@stoprocent/noble 1.9.2-16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/.editorconfig +11 -0
  2. package/.eslintrc.js +25 -0
  3. package/.github/FUNDING.yml +2 -0
  4. package/.github/workflows/fediverse-action.yml +16 -0
  5. package/.github/workflows/nodepackage.yml +77 -0
  6. package/.github/workflows/npm-publish.yml +26 -0
  7. package/.github/workflows/prebuild.yml +65 -0
  8. package/.nycrc.json +4 -0
  9. package/CHANGELOG.md +119 -0
  10. package/LICENSE +20 -0
  11. package/MAINTAINERS.md +1 -0
  12. package/README.md +833 -0
  13. package/assets/noble-logo.png +0 -0
  14. package/assets/noble-logo.svg +13 -0
  15. package/binding.gyp +19 -0
  16. package/codecov.yml +5 -0
  17. package/examples/advertisement-discovery.js +65 -0
  18. package/examples/cache-gatt-discovery.js +198 -0
  19. package/examples/cache-gatt-reconnect.js +164 -0
  20. package/examples/echo.js +104 -0
  21. package/examples/enter-exit.js +78 -0
  22. package/examples/peripheral-explorer-async.js +133 -0
  23. package/examples/peripheral-explorer.js +225 -0
  24. package/examples/pizza/README.md +15 -0
  25. package/examples/pizza/central.js +194 -0
  26. package/examples/pizza/pizza.js +60 -0
  27. package/index.d.ts +203 -0
  28. package/index.js +6 -0
  29. package/lib/characteristic.js +161 -0
  30. package/lib/characteristics.json +449 -0
  31. package/lib/descriptor.js +72 -0
  32. package/lib/descriptors.json +47 -0
  33. package/lib/distributed/bindings.js +326 -0
  34. package/lib/hci-socket/acl-stream.js +60 -0
  35. package/lib/hci-socket/bindings.js +788 -0
  36. package/lib/hci-socket/crypto.js +74 -0
  37. package/lib/hci-socket/gap.js +432 -0
  38. package/lib/hci-socket/gatt.js +809 -0
  39. package/lib/hci-socket/hci-status.json +71 -0
  40. package/lib/hci-socket/hci.js +1264 -0
  41. package/lib/hci-socket/signaling.js +76 -0
  42. package/lib/hci-socket/smp.js +140 -0
  43. package/lib/hci-uart/bindings.js +569 -0
  44. package/lib/hci-uart/hci-serial-parser.js +70 -0
  45. package/lib/hci-uart/hci.js +1336 -0
  46. package/lib/mac/binding.gyp +26 -0
  47. package/lib/mac/bindings.js +11 -0
  48. package/lib/mac/src/ble_manager.h +41 -0
  49. package/lib/mac/src/ble_manager.mm +435 -0
  50. package/lib/mac/src/callbacks.cc +222 -0
  51. package/lib/mac/src/callbacks.h +84 -0
  52. package/lib/mac/src/napi_objc.h +12 -0
  53. package/lib/mac/src/napi_objc.mm +50 -0
  54. package/lib/mac/src/noble_mac.h +34 -0
  55. package/lib/mac/src/noble_mac.mm +264 -0
  56. package/lib/mac/src/objc_cpp.h +26 -0
  57. package/lib/mac/src/objc_cpp.mm +126 -0
  58. package/lib/mac/src/peripheral.h +23 -0
  59. package/lib/manufacture.js +48 -0
  60. package/lib/noble.js +593 -0
  61. package/lib/peripheral.js +219 -0
  62. package/lib/resolve-bindings-web.js +9 -0
  63. package/lib/resolve-bindings.js +44 -0
  64. package/lib/service.js +72 -0
  65. package/lib/services.json +92 -0
  66. package/lib/webbluetooth/bindings.js +368 -0
  67. package/lib/websocket/bindings.js +321 -0
  68. package/lib/win/binding.gyp +23 -0
  69. package/lib/win/bindings.js +11 -0
  70. package/lib/win/src/ble_manager.cc +802 -0
  71. package/lib/win/src/ble_manager.h +77 -0
  72. package/lib/win/src/callbacks.cc +274 -0
  73. package/lib/win/src/callbacks.h +33 -0
  74. package/lib/win/src/napi_winrt.cc +76 -0
  75. package/lib/win/src/napi_winrt.h +12 -0
  76. package/lib/win/src/noble_winrt.cc +308 -0
  77. package/lib/win/src/noble_winrt.h +34 -0
  78. package/lib/win/src/notify_map.cc +62 -0
  79. package/lib/win/src/notify_map.h +50 -0
  80. package/lib/win/src/peripheral.h +23 -0
  81. package/lib/win/src/peripheral_winrt.cc +296 -0
  82. package/lib/win/src/peripheral_winrt.h +82 -0
  83. package/lib/win/src/radio_watcher.cc +125 -0
  84. package/lib/win/src/radio_watcher.h +61 -0
  85. package/lib/win/src/winrt_cpp.cc +82 -0
  86. package/lib/win/src/winrt_cpp.h +11 -0
  87. package/lib/win/src/winrt_guid.cc +12 -0
  88. package/lib/win/src/winrt_guid.h +13 -0
  89. package/misc/nrf52840dk.hex +6921 -0
  90. package/misc/prj.conf +43 -0
  91. package/package.json +96 -0
  92. package/test/lib/characteristic.test.js +791 -0
  93. package/test/lib/descriptor.test.js +249 -0
  94. package/test/lib/distributed/bindings.test.js +918 -0
  95. package/test/lib/hci-socket/acl-stream.test.js +188 -0
  96. package/test/lib/hci-socket/bindings.test.js +1756 -0
  97. package/test/lib/hci-socket/crypto.test.js +55 -0
  98. package/test/lib/hci-socket/gap.test.js +1089 -0
  99. package/test/lib/hci-socket/gatt.test.js +2392 -0
  100. package/test/lib/hci-socket/hci.test.js +1891 -0
  101. package/test/lib/hci-socket/signaling.test.js +94 -0
  102. package/test/lib/hci-socket/smp.test.js +268 -0
  103. package/test/lib/manufacture.test.js +77 -0
  104. package/test/lib/peripheral.test.js +623 -0
  105. package/test/lib/resolve-bindings.test.js +102 -0
  106. package/test/lib/service.test.js +195 -0
  107. package/test/lib/webbluetooth/bindings.test.js +190 -0
  108. package/test/lib/websocket/bindings.test.js +456 -0
  109. package/test/noble.test.js +1565 -0
  110. package/test.js +131 -0
  111. package/with-bindings.js +5 -0
  112. package/ws-slave.js +404 -0
@@ -0,0 +1,133 @@
1
+ const noble = require('../')({ extended: false });
2
+
3
+ const peripheralIdOrAddress = process.argv[2].toLowerCase();
4
+
5
+ noble.on('stateChange', async (state) => {
6
+ if (state === 'poweredOn') {
7
+ await noble.startScanningAsync([], false);
8
+ }
9
+ });
10
+
11
+ noble.on('discover', async (peripheral) => {
12
+ if ([peripheral.id, peripheral.address].includes(peripheralIdOrAddress)) {
13
+ await noble.stopScanningAsync();
14
+
15
+ console.log(`Peripheral with ID ${peripheral.id} found`);
16
+ const advertisement = peripheral.advertisement;
17
+
18
+ const localName = advertisement.localName;
19
+ const txPowerLevel = advertisement.txPowerLevel;
20
+ const manufacturerData = advertisement.manufacturerData;
21
+ const serviceData = advertisement.serviceData;
22
+ const serviceUuids = advertisement.serviceUuids;
23
+
24
+ if (localName) {
25
+ console.log(` Local Name = ${localName}`);
26
+ }
27
+
28
+ if (txPowerLevel) {
29
+ console.log(` TX Power Level = ${txPowerLevel}`);
30
+ }
31
+
32
+ if (manufacturerData) {
33
+ console.log(` Manufacturer Data = ${manufacturerData.toString('hex')}`);
34
+ }
35
+
36
+ if (serviceData) {
37
+ console.log(
38
+ ` Service Data = ${JSON.stringify(serviceData, null, 2)}`
39
+ );
40
+ }
41
+
42
+ if (serviceUuids) {
43
+ console.log(` Service UUIDs = ${serviceUuids}`);
44
+ }
45
+
46
+ console.log();
47
+
48
+ await explore(peripheral);
49
+ }
50
+ });
51
+
52
+ /**
53
+ * @param {import('../').Peripheral} peripheral
54
+ */
55
+ const explore = async (peripheral) => {
56
+ console.log('Services and characteristics:');
57
+
58
+ peripheral.on('disconnect', () => {
59
+ process.exit(0);
60
+ });
61
+
62
+ await peripheral.connectAsync();
63
+
64
+ const services = await peripheral.discoverServicesAsync([]);
65
+
66
+ for (const service of services) {
67
+ let serviceInfo = service.uuid;
68
+
69
+ if (service.name) {
70
+ serviceInfo += ` (${service.name})`;
71
+ }
72
+
73
+ console.log(serviceInfo);
74
+
75
+ const characteristics = await service.discoverCharacteristicsAsync([]);
76
+
77
+ for (const characteristic of characteristics) {
78
+ let characteristicInfo = ` ${characteristic.uuid}`;
79
+
80
+ if (characteristic.name) {
81
+ characteristicInfo += ` (${characteristic.name})`;
82
+ }
83
+
84
+ const descriptors = await characteristic.discoverDescriptorsAsync();
85
+
86
+ const userDescriptionDescriptor = descriptors.find(
87
+ (descriptor) => descriptor.uuid === '2901'
88
+ );
89
+
90
+ if (userDescriptionDescriptor) {
91
+ const data = await userDescriptionDescriptor.readValueAsync();
92
+ if (data) {
93
+ characteristicInfo += ` (${data.toString()})`;
94
+ }
95
+ }
96
+
97
+ characteristicInfo += `\n properties ${characteristic.properties.join(
98
+ ', '
99
+ )}`;
100
+
101
+ if (characteristic.properties.includes('read')) {
102
+ const data = await characteristic.readAsync();
103
+
104
+ if (data) {
105
+ const string = data.toString('ascii');
106
+
107
+ characteristicInfo += `\n value ${data.toString(
108
+ 'hex'
109
+ )} | '${string}'`;
110
+ }
111
+ }
112
+
113
+ console.log(characteristicInfo);
114
+ }
115
+ }
116
+
117
+ await peripheral.disconnectAsync();
118
+ };
119
+
120
+ process.on('SIGINT', function () {
121
+ console.log('Caught interrupt signal');
122
+ noble.stopScanning(() => process.exit());
123
+ });
124
+
125
+ process.on('SIGQUIT', function () {
126
+ console.log('Caught interrupt signal');
127
+ noble.stopScanning(() => process.exit());
128
+ });
129
+
130
+ process.on('SIGTERM', function () {
131
+ console.log('Caught interrupt signal');
132
+ noble.stopScanning(() => process.exit());
133
+ });
@@ -0,0 +1,225 @@
1
+ /* eslint-disable handle-callback-err */
2
+ const async = require('async');
3
+ const noble = require('../index')({ extended: false });
4
+
5
+ const peripheralIdOrAddress = process.argv[2].toLowerCase();
6
+
7
+ noble.on('stateChange', function (state) {
8
+ if (state === 'poweredOn') {
9
+ noble.startScanning([], false);
10
+ } else {
11
+ noble.stopScanning();
12
+ }
13
+ });
14
+
15
+ noble.on('discover', function (peripheral) {
16
+ if (
17
+ peripheral.id === peripheralIdOrAddress ||
18
+ peripheral.address === peripheralIdOrAddress
19
+ ) {
20
+ noble.stopScanning();
21
+
22
+ console.log(`peripheral with ID ${peripheral.id} found`);
23
+ const advertisement = peripheral.advertisement;
24
+
25
+ const localName = advertisement.localName;
26
+ const txPowerLevel = advertisement.txPowerLevel;
27
+ const manufacturerData = advertisement.manufacturerData;
28
+ const serviceData = advertisement.serviceData;
29
+ const serviceUuids = advertisement.serviceUuids;
30
+
31
+ if (localName) {
32
+ console.log(` Local Name = ${localName}`);
33
+ }
34
+
35
+ if (txPowerLevel) {
36
+ console.log(` TX Power Level = ${txPowerLevel}`);
37
+ }
38
+
39
+ if (manufacturerData) {
40
+ console.log(` Manufacturer Data = ${manufacturerData.toString('hex')}`);
41
+ }
42
+
43
+ if (serviceData) {
44
+ console.log(
45
+ ` Service Data = ${JSON.stringify(serviceData, null, 2)}`
46
+ );
47
+ }
48
+
49
+ if (serviceUuids) {
50
+ console.log(` Service UUIDs = ${serviceUuids}`);
51
+ }
52
+
53
+ console.log();
54
+
55
+ explore(peripheral);
56
+ }
57
+ });
58
+
59
+ function explore (peripheral) {
60
+ console.log('services and characteristics:');
61
+
62
+ peripheral.on('disconnect', function () {
63
+ process.exit(0);
64
+ });
65
+
66
+ peripheral.connect(function (error) {
67
+ if (error) {
68
+ console.error(error);
69
+ return;
70
+ }
71
+
72
+ peripheral.discoverServices([], function (error, services) {
73
+ if (error) {
74
+ console.error(error);
75
+ return;
76
+ }
77
+
78
+ let serviceIndex = 0;
79
+
80
+ async.whilst(
81
+ function () {
82
+ return serviceIndex < services.length;
83
+ },
84
+ function (callback) {
85
+ const service = services[serviceIndex];
86
+ let serviceInfo = service.uuid;
87
+
88
+ if (service.name) {
89
+ serviceInfo += ` (${service.name})`;
90
+ }
91
+ console.log(serviceInfo);
92
+
93
+ service.discoverCharacteristics(
94
+ [],
95
+ function (error, characteristics) {
96
+ if (error) {
97
+ console.error(error);
98
+ return;
99
+ }
100
+
101
+ let characteristicIndex = 0;
102
+
103
+ async.whilst(
104
+ function () {
105
+ return characteristicIndex < characteristics.length;
106
+ },
107
+ function (callback) {
108
+ const characteristic = characteristics[characteristicIndex];
109
+ let characteristicInfo = ` ${characteristic.uuid}`;
110
+
111
+ if (characteristic.name) {
112
+ characteristicInfo += ` (${characteristic.name})`;
113
+ }
114
+
115
+ async.series([
116
+ function (callback) {
117
+ characteristic.discoverDescriptors(function (
118
+ error,
119
+ descriptors
120
+ ) {
121
+ if (error) {
122
+ console.error(error);
123
+ return;
124
+ }
125
+
126
+ async.detect(
127
+ descriptors,
128
+ function (descriptor, callback) {
129
+ if (descriptor.uuid === '2901') {
130
+ return callback(descriptor);
131
+ } else {
132
+ return callback();
133
+ }
134
+ },
135
+ function (userDescriptionDescriptor) {
136
+ if (userDescriptionDescriptor) {
137
+ userDescriptionDescriptor.readValue(function (
138
+ error,
139
+ data
140
+ ) {
141
+ if (error) {
142
+ console.error(error);
143
+ }
144
+
145
+ if (data) {
146
+ characteristicInfo += ` (${data.toString()})`;
147
+ }
148
+ callback();
149
+ });
150
+ } else {
151
+ callback();
152
+ }
153
+ }
154
+ );
155
+ });
156
+ },
157
+ function (callback) {
158
+ characteristicInfo += `\n properties ${characteristic.properties.join(
159
+ ', '
160
+ )}`;
161
+
162
+ if (characteristic.properties.indexOf('read') !== -1) {
163
+ characteristic.read(function (error, data) {
164
+ if (error) {
165
+ console.error(error);
166
+ }
167
+
168
+ if (data) {
169
+ const string = data.toString('ascii');
170
+
171
+ characteristicInfo += `\n value ${data.toString(
172
+ 'hex'
173
+ )} | '${string}'`;
174
+ }
175
+ callback();
176
+ });
177
+ } else {
178
+ callback();
179
+ }
180
+ },
181
+ function () {
182
+ console.log(characteristicInfo);
183
+ characteristicIndex++;
184
+ callback();
185
+ }
186
+ ]);
187
+ },
188
+ function (error) {
189
+ if (error) {
190
+ console.error(error);
191
+ }
192
+
193
+ serviceIndex++;
194
+ callback();
195
+ }
196
+ );
197
+ }
198
+ );
199
+ },
200
+ function (error) {
201
+ if (error) {
202
+ console.error(error);
203
+ }
204
+
205
+ peripheral.disconnect();
206
+ }
207
+ );
208
+ });
209
+ });
210
+ }
211
+
212
+ process.on('SIGINT', function () {
213
+ console.log('Caught interrupt signal');
214
+ noble.stopScanning(() => process.exit());
215
+ });
216
+
217
+ process.on('SIGQUIT', function () {
218
+ console.log('Caught interrupt signal');
219
+ noble.stopScanning(() => process.exit());
220
+ });
221
+
222
+ process.on('SIGTERM', function () {
223
+ console.log('Caught interrupt signal');
224
+ noble.stopScanning(() => process.exit());
225
+ });
@@ -0,0 +1,15 @@
1
+ # BLE Pizza Service
2
+
3
+ This is an example program demonstrating BLE connectivity between a peripheral running bleno, and a central running noble.
4
+
5
+ This central connects to a robotic pizza oven service, with the following characteristics:
6
+
7
+ * crust - read / write. A value representing the type of pizza crust (normal, thin, or deep dish)
8
+ * toppings - read / write. A value representing which toppings to include (pepperoni, mushrooms, extra cheese, etc.)
9
+ * bake - write / notify. The value written is the temperature at which to bake the pizza. When baking is finished, the central is notified with a bake result (half baked, crispy, burnt, etc.)
10
+
11
+ To run the central example:
12
+
13
+ node central
14
+
15
+ And on another computer, start advertising a peripheral with [bleno](https://github.com/sandeepmistry/bleno/tree/master/examples/pizza).
@@ -0,0 +1,194 @@
1
+ /* eslint-disable handle-callback-err */
2
+ const noble = require('../..')({ extended: false });
3
+ const pizza = require('./pizza');
4
+
5
+ const pizzaServiceUuid = '13333333333333333333333333333337';
6
+ const pizzaCrustCharacteristicUuid = '13333333333333333333333333330001';
7
+ const pizzaToppingsCharacteristicUuid = '13333333333333333333333333330002';
8
+ const pizzaBakeCharacteristicUuid = '13333333333333333333333333330003';
9
+
10
+ noble.on('stateChange', function (state) {
11
+ if (state === 'poweredOn') {
12
+ //
13
+ // Once the BLE radio has been powered on, it is possible
14
+ // to begin scanning for services. Pass an empty array to
15
+ // scan for all services (uses more time and power).
16
+ //
17
+ console.log('scanning...');
18
+ noble.startScanning([pizzaServiceUuid], false);
19
+ } else {
20
+ noble.stopScanning();
21
+ }
22
+ });
23
+
24
+ let pizzaCrustCharacteristic = null;
25
+ let pizzaToppingsCharacteristic = null;
26
+ let pizzaBakeCharacteristic = null;
27
+
28
+ noble.on('discover', function (peripheral) {
29
+ // we found a peripheral, stop scanning
30
+ noble.stopScanning();
31
+
32
+ //
33
+ // The advertisment data contains a name, power level (if available),
34
+ // certain advertised service uuids, as well as manufacturer data,
35
+ // which could be formatted as an iBeacon.
36
+ //
37
+ console.log('found peripheral:', peripheral.advertisement);
38
+ //
39
+ // Once the peripheral has been discovered, then connect to it.
40
+ //
41
+ peripheral.connect(function (error) {
42
+ if (error) {
43
+ console.error(error);
44
+ }
45
+
46
+ //
47
+ // Once the peripheral has been connected, then discover the
48
+ // services and characteristics of interest.
49
+ //
50
+ peripheral.discoverServices([pizzaServiceUuid], function (error, services) {
51
+ if (error) {
52
+ console.error(error);
53
+ return;
54
+ }
55
+
56
+ services.forEach(function (service) {
57
+ //
58
+ // This must be the service we were looking for.
59
+ //
60
+ console.log('found service:', service.uuid);
61
+
62
+ //
63
+ // So, discover its characteristics.
64
+ //
65
+ service.discoverCharacteristics([], function (error, characteristics) {
66
+ if (error) {
67
+ console.error(error);
68
+ return;
69
+ }
70
+
71
+ characteristics.forEach(function (characteristic) {
72
+ //
73
+ // Loop through each characteristic and match them to the
74
+ // UUIDs that we know about.
75
+ //
76
+ console.log('found characteristic:', characteristic.uuid);
77
+
78
+ if (pizzaCrustCharacteristicUuid === characteristic.uuid) {
79
+ pizzaCrustCharacteristic = characteristic;
80
+ } else if (
81
+ pizzaToppingsCharacteristicUuid === characteristic.uuid
82
+ ) {
83
+ pizzaToppingsCharacteristic = characteristic;
84
+ } else if (pizzaBakeCharacteristicUuid === characteristic.uuid) {
85
+ pizzaBakeCharacteristic = characteristic;
86
+ }
87
+ });
88
+
89
+ //
90
+ // Check to see if we found all of our characteristics.
91
+ //
92
+ if (
93
+ pizzaCrustCharacteristic &&
94
+ pizzaToppingsCharacteristic &&
95
+ pizzaBakeCharacteristic
96
+ ) {
97
+ //
98
+ // We did, so bake a pizza!
99
+ //
100
+ bakePizza();
101
+ } else {
102
+ console.log('missing characteristics');
103
+ }
104
+ });
105
+ });
106
+ });
107
+ });
108
+ });
109
+
110
+ function bakePizza () {
111
+ //
112
+ // Pick the crust.
113
+ //
114
+ const crust = Buffer.alloc(1);
115
+ crust.writeUInt8(pizza.PizzaCrust.THIN, 0);
116
+ pizzaCrustCharacteristic.write(crust, false, function (err) {
117
+ if (!err) {
118
+ //
119
+ // Pick the toppings.
120
+ //
121
+ const toppings = Buffer.alloc(2);
122
+ toppings.writeUInt16BE(
123
+ pizza.PizzaToppings.EXTRA_CHEESE |
124
+ pizza.PizzaToppings.CANADIAN_BACON |
125
+ pizza.PizzaToppings.PINEAPPLE,
126
+ 0
127
+ );
128
+ pizzaToppingsCharacteristic.write(toppings, false, function (err) {
129
+ if (!err) {
130
+ //
131
+ // Subscribe to the bake notification, so we know when
132
+ // our pizza will be ready.
133
+ //
134
+ pizzaBakeCharacteristic.on('read', function (data, isNotification) {
135
+ console.log('Our pizza is ready!');
136
+ if (data.length === 1) {
137
+ const result = data.readUInt8(0);
138
+ console.log('The result is',
139
+ result === pizza.PizzaBakeResult.HALF_BAKED
140
+ ? 'half baked.'
141
+ : result === pizza.PizzaBakeResult.BAKED
142
+ ? 'baked.'
143
+ : result === pizza.PizzaBakeResult.CRISPY
144
+ ? 'crispy.'
145
+ : result === pizza.PizzaBakeResult.BURNT
146
+ ? 'burnt.'
147
+ : result === pizza.PizzaBakeResult.ON_FIRE
148
+ ? 'on fire!'
149
+ : 'unknown?');
150
+ } else {
151
+ console.log('result length incorrect');
152
+ }
153
+ });
154
+ pizzaBakeCharacteristic.subscribe(function (error) {
155
+ if (error) {
156
+ console.error(error);
157
+ return;
158
+ }
159
+
160
+ //
161
+ // Bake at 450 degrees!
162
+ //
163
+ const temperature = Buffer.alloc(2);
164
+ temperature.writeUInt16BE(450, 0);
165
+ pizzaBakeCharacteristic.write(temperature, false, function (err) {
166
+ if (err) {
167
+ console.log('bake error');
168
+ }
169
+ });
170
+ });
171
+ } else {
172
+ console.log('toppings error');
173
+ }
174
+ });
175
+ } else {
176
+ console.log('crust error');
177
+ }
178
+ });
179
+ }
180
+
181
+ process.on('SIGINT', function () {
182
+ console.log('Caught interrupt signal');
183
+ noble.stopScanning(() => process.exit());
184
+ });
185
+
186
+ process.on('SIGQUIT', function () {
187
+ console.log('Caught interrupt signal');
188
+ noble.stopScanning(() => process.exit());
189
+ });
190
+
191
+ process.on('SIGTERM', function () {
192
+ console.log('Caught interrupt signal');
193
+ noble.stopScanning(() => process.exit());
194
+ });
@@ -0,0 +1,60 @@
1
+ const util = require('util');
2
+ const events = require('events');
3
+
4
+ const PizzaCrust = {
5
+ NORMAL: 0,
6
+ DEEP_DISH: 1,
7
+ THIN: 2
8
+ };
9
+
10
+ const PizzaToppings = {
11
+ NONE: 0,
12
+ PEPPERONI: 1 << 0,
13
+ MUSHROOMS: 1 << 1,
14
+ EXTRA_CHEESE: 1 << 2,
15
+ BLACK_OLIVES: 1 << 3,
16
+ CANADIAN_BACON: 1 << 4,
17
+ PINEAPPLE: 1 << 5,
18
+ BELL_PEPPERS: 1 << 6,
19
+ SAUSAGE: 1 << 7
20
+ };
21
+
22
+ const PizzaBakeResult = {
23
+ HALF_BAKED: 0,
24
+ BAKED: 1,
25
+ CRISPY: 2,
26
+ BURNT: 3,
27
+ ON_FIRE: 4
28
+ };
29
+
30
+ function Pizza () {
31
+ events.EventEmitter.call(this);
32
+ this.toppings = PizzaToppings.NONE;
33
+ this.crust = PizzaCrust.NORMAL;
34
+ }
35
+
36
+ util.inherits(Pizza, events.EventEmitter);
37
+
38
+ Pizza.prototype.bake = function (temperature) {
39
+ const time = temperature * 10;
40
+ const self = this;
41
+ console.log('baking pizza at', temperature, 'degrees for', time, 'milliseconds');
42
+ setTimeout(function () {
43
+ const result =
44
+ (temperature < 350)
45
+ ? PizzaBakeResult.HALF_BAKED
46
+ : (temperature < 450)
47
+ ? PizzaBakeResult.BAKED
48
+ : (temperature < 500)
49
+ ? PizzaBakeResult.CRISPY
50
+ : (temperature < 600)
51
+ ? PizzaBakeResult.BURNT
52
+ : PizzaBakeResult.ON_FIRE;
53
+ self.emit('ready', result);
54
+ }, time);
55
+ };
56
+
57
+ module.exports.Pizza = Pizza;
58
+ module.exports.PizzaToppings = PizzaToppings;
59
+ module.exports.PizzaCrust = PizzaCrust;
60
+ module.exports.PizzaBakeResult = PizzaBakeResult;