GeezerVison is showing github.com — src: support diagnostics channel in the snapshot · nodejs/node@fddc701
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/mksnapshot.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,7 +48,7 @@ const supportedModules = new SafeSet(new SafeArrayIterator([ | 48 | 48 | 'constants', | 49 | 49 | 'crypto', | 50 | 50 | // 'dgram', | 51 | | - // 'diagnostics_channel', | | 51 | + 'diagnostics_channel', | 52 | 52 | // 'dns', | 53 | 53 | // 'dns/promises', | 54 | 54 | // 'domain', | @@ -60,7 +60,7 @@ const supportedModules = new SafeSet(new SafeArrayIterator([ | 60 | 60 | // 'https', | 61 | 61 | // 'inspector', | 62 | 62 | // 'module', | 63 | | - // 'net', | | 63 | + 'net', | 64 | 64 | 'os', | 65 | 65 | 'path', | 66 | 66 | 'path/posix', | |
Collapse file Copy file name to clipboardExpand all lines: lib/net.js +4-16Lines changed: 4 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -131,20 +131,9 @@ const noop = () => {}; | 131 | 131 |
| 132 | 132 | const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext'); | 133 | 133 |
| 134 | | -let netClientSocketChannel; | 135 | | -let netServerSocketChannel; | 136 | | -function lazyChannels() { | 137 | | - // TODO(joyeecheung): support diagnostics channels in the snapshot. | 138 | | - // For now it is fine to create them lazily when there isn't a snapshot to | 139 | | - // build. If users need the channels they would have to create them first | 140 | | - // before invoking any built-ins that would publish to these channels | 141 | | - // anyway. | 142 | | - if (netClientSocketChannel === undefined) { | 143 | | - const dc = require('diagnostics_channel'); | 144 | | - netClientSocketChannel = dc.channel('net.client.socket'); | 145 | | - netServerSocketChannel = dc.channel('net.server.socket'); | 146 | | - } | 147 | | -} | | 134 | +const dc = require('diagnostics_channel'); | | 135 | +const netClientSocketChannel = dc.channel('net.client.socket'); | | 136 | +const netServerSocketChannel = dc.channel('net.server.socket'); | 148 | 137 |
| 149 | 138 | const { | 150 | 139 | hasObserver, | @@ -217,7 +206,7 @@ function connect(...args) { | 217 | 206 | const options = normalized[0]; | 218 | 207 | debug('createConnection', normalized); | 219 | 208 | const socket = new Socket(options); | 220 | | - lazyChannels(); | | 209 | + | 221 | 210 | if (netClientSocketChannel.hasSubscribers) { | 222 | 211 | netClientSocketChannel.publish({ | 223 | 212 | socket, | @@ -1761,7 +1750,6 @@ function onconnection(err, clientHandle) { | 1761 | 1750 | socket.server = self; | 1762 | 1751 | socket._server = self; | 1763 | 1752 | self.emit('connection', socket); | 1764 | | - lazyChannels(); | 1765 | 1753 | if (netServerSocketChannel.hasSubscribers) { | 1766 | 1754 | netServerSocketChannel.publish({ | 1767 | 1755 | socket, | |
Collapse file Copy file name to clipboard +60Lines changed: 60 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const net = require('net'); | | 4 | +const { | | 5 | + setDeserializeMainFunction | | 6 | +} = require('v8').startupSnapshot; | | 7 | +const dc = require('diagnostics_channel'); | | 8 | + | | 9 | +const echoServer = net.Server(function(connection) { | | 10 | + connection.on('data', function(chunk) { | | 11 | + connection.write(chunk); | | 12 | + }); | | 13 | + connection.on('end', function() { | | 14 | + connection.end(); | | 15 | + }); | | 16 | +}); | | 17 | + | | 18 | +const kNumChars = 256; | | 19 | +const buffer = new Uint8Array(kNumChars); | | 20 | +for (let i = 0; i < kNumChars; ++i) { | | 21 | + buffer[i] = i; | | 22 | +} | | 23 | + | | 24 | +let recv = ''; | | 25 | + | | 26 | +echoServer.on('listening', function() { | | 27 | + const port = this.address().port; | | 28 | + console.log(`server port`, port); | | 29 | + const c = net.createConnection({ host: '127.0.0.1', port }); | | 30 | + | | 31 | + c.on('data', function(chunk) { | | 32 | + recv += chunk.toString('latin1'); | | 33 | + | | 34 | + if (recv.length === buffer.length) { | | 35 | + c.end(); | | 36 | + } | | 37 | + }); | | 38 | + | | 39 | + c.on('connect', function() { | | 40 | + c.write(buffer); | | 41 | + }); | | 42 | + | | 43 | + c.on('close', function() { | | 44 | + console.log(`recv.length: ${recv.length}`); | | 45 | + echoServer.close(); | | 46 | + }); | | 47 | + | | 48 | +}); | | 49 | + | | 50 | +dc.subscribe('net.server.socket', (({ socket }) => { | | 51 | + console.log(`From server diagnostics channel:`, socket.localPort); | | 52 | +})); | | 53 | + | | 54 | +dc.subscribe('net.client.socket', (({ socket }) => { | | 55 | + console.log(`From client diagnostics channel`); | | 56 | +})); | | 57 | + | | 58 | +setDeserializeMainFunction(() => { | | 59 | + echoServer.listen(0); | | 60 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-bootstrap-modules.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,7 @@ const expectedModules = new Set([ | 46 | 46 | 'Internal Binding wasm_web_api', | 47 | 47 | 'Internal Binding worker', | 48 | 48 | 'NativeModule buffer', | | 49 | + 'NativeModule diagnostics_channel', | 49 | 50 | 'NativeModule events', | 50 | 51 | 'NativeModule fs', | 51 | 52 | 'NativeModule internal/abort_controller', | |
Collapse file Copy file name to clipboard +65Lines changed: 65 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// This tests that a local TCP server can be snapshotted and the | | 4 | +// diagnostics channels work across serialization. | | 5 | + | | 6 | +require('../common'); | | 7 | +const assert = require('assert'); | | 8 | +const { spawnSync } = require('child_process'); | | 9 | +const tmpdir = require('../common/tmpdir'); | | 10 | +const fixtures = require('../common/fixtures'); | | 11 | +const path = require('path'); | | 12 | +const fs = require('fs'); | | 13 | + | | 14 | +tmpdir.refresh(); | | 15 | +const blobPath = path.join(tmpdir.path, 'snapshot.blob'); | | 16 | +const entry = fixtures.path('snapshot', 'server.js'); | | 17 | +{ | | 18 | + const child = spawnSync(process.execPath, [ | | 19 | + '--snapshot-blob', | | 20 | + blobPath, | | 21 | + '--build-snapshot', | | 22 | + entry, | | 23 | + ], { | | 24 | + cwd: tmpdir.path | | 25 | + }); | | 26 | + if (child.status !== 0) { | | 27 | + console.log(child.stderr.toString()); | | 28 | + console.log(child.stdout.toString()); | | 29 | + assert.strictEqual(child.status, 0); | | 30 | + } | | 31 | + const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); | | 32 | + assert(stats.isFile()); | | 33 | +} | | 34 | + | | 35 | +{ | | 36 | + const child = spawnSync(process.execPath, [ | | 37 | + '--snapshot-blob', | | 38 | + blobPath, | | 39 | + ], { | | 40 | + cwd: tmpdir.path, | | 41 | + env: { | | 42 | + ...process.env, | | 43 | + } | | 44 | + }); | | 45 | + | | 46 | + const stdout = child.stdout.toString().trim(); | | 47 | + console.log(`[stdout]:\n${stdout}\n`); | | 48 | + const stderr = child.stderr.toString().trim(); | | 49 | + console.log(`[stderr]:\n${stderr}\n`); | | 50 | + assert.strictEqual(child.status, 0); | | 51 | + | | 52 | + const lines = stdout.split('\n'); | | 53 | + assert.strictEqual(lines.length, 4); | | 54 | + | | 55 | + // The log should look like this: | | 56 | + // server port ${port} | | 57 | + // From client diagnostics channel | | 58 | + // From server diagnostics channel: ${port} | | 59 | + // recv.length: 256 | | 60 | + assert.match(lines[0], /server port (\d+)/); | | 61 | + const port = lines[0].match(/server port (\d+)/)[1]; | | 62 | + assert.match(lines[1], /From client diagnostics channel/); | | 63 | + assert.match(lines[2], new RegExp(`From server diagnostics channel: ${port}`)); | | 64 | + assert.match(lines[3], /recv\.length: 256/); | | 65 | +} |
|