2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2012-04-06 16:26:18 -07:00
|
|
|
// Tests of multiple domains happening at once.
|
|
|
|
|
2017-01-14 19:05:04 -05:00
|
|
|
const common = require('../common');
|
2016-12-30 18:38:06 -05:00
|
|
|
const domain = require('domain');
|
2017-01-14 19:05:04 -05:00
|
|
|
const http = require('http');
|
2012-04-06 16:26:18 -07:00
|
|
|
|
2020-07-07 18:05:33 +02:00
|
|
|
process.on('warning', common.mustNotCall());
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const a = domain.create();
|
2019-03-07 01:03:53 +01:00
|
|
|
a.enter(); // This will be our "root" domain
|
2012-04-06 16:26:18 -07:00
|
|
|
|
2017-02-03 14:54:19 -05:00
|
|
|
a.on('error', common.mustNotCall());
|
2012-04-06 16:26:18 -07:00
|
|
|
|
2017-01-14 19:05:04 -05:00
|
|
|
const server = http.createServer((req, res) => {
|
2012-05-01 11:32:20 +09:00
|
|
|
// child domain of a.
|
2017-01-08 13:19:00 +00:00
|
|
|
const b = domain.create();
|
2012-05-01 11:32:20 +09:00
|
|
|
a.add(b);
|
2012-04-06 16:26:18 -07:00
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Treat these EE objects as if they are a part of the b domain
|
2012-04-06 16:26:18 -07:00
|
|
|
// so, an 'error' event on them propagates to the domain, rather
|
|
|
|
// than being thrown.
|
|
|
|
b.add(req);
|
|
|
|
b.add(res);
|
|
|
|
|
2017-01-14 19:05:04 -05:00
|
|
|
b.on('error', common.mustCall((er) => {
|
2012-04-06 16:26:18 -07:00
|
|
|
if (res) {
|
2022-11-24 09:29:07 +01:00
|
|
|
// Introduce an error on the client by writing unexpected data.
|
|
|
|
// The client is now expecting a chunk header so any letter will have the parser throw an error.
|
|
|
|
res.socket.write('H');
|
2012-04-06 16:26:18 -07:00
|
|
|
}
|
|
|
|
// res.writeHead(500), res.destroy, etc.
|
|
|
|
server.close();
|
2017-01-14 19:05:04 -05:00
|
|
|
}));
|
2012-04-06 16:26:18 -07:00
|
|
|
|
|
|
|
// XXX this bind should not be necessary.
|
|
|
|
// the write cb behavior in http/net should use an
|
|
|
|
// event so that it picks up the domain handling.
|
2017-01-14 19:05:04 -05:00
|
|
|
res.write('HELLO\n', b.bind(() => {
|
2012-04-06 16:26:18 -07:00
|
|
|
throw new Error('this kills domain B, not A');
|
|
|
|
}));
|
|
|
|
|
2017-01-14 19:05:04 -05:00
|
|
|
}).listen(0, () => {
|
2017-01-08 13:19:00 +00:00
|
|
|
const c = domain.create();
|
2017-01-14 19:05:04 -05:00
|
|
|
const req = http.get({ host: 'localhost', port: server.address().port });
|
2012-04-06 16:26:18 -07:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Add the request to the C domain
|
2016-05-29 03:06:56 -04:00
|
|
|
c.add(req);
|
2012-04-06 16:26:18 -07:00
|
|
|
|
2017-01-14 19:05:04 -05:00
|
|
|
req.on('response', (res) => {
|
2019-03-07 01:03:53 +01:00
|
|
|
// Add the response object to the C domain
|
2016-05-29 03:06:56 -04:00
|
|
|
c.add(res);
|
|
|
|
res.pipe(process.stdout);
|
|
|
|
});
|
2012-04-06 16:26:18 -07:00
|
|
|
|
2022-10-20 08:13:32 -05:00
|
|
|
c.on('error', common.mustCall());
|
2012-04-06 16:26:18 -07:00
|
|
|
});
|