From cb35604ef5ee9fda5a3d46e3068c815e33d83f01 Mon Sep 17 00:00:00 2001 From: Jeremy Barlow Date: Wed, 19 Sep 2018 08:44:26 -0700 Subject: [PATCH] Resolve path when sending example file for Windows support Previously, when trying to import an example into the flow editor on Windows, the load attempt would fail with an HTTP 404 error in the browser client, with a `TypeError: path must be absolute or specify root to res.sendFile` error being written to the Node-RED log. This was due to the path being passed to the `res.sendFile` function not being fully-qualified (for example, `\Users\myuser\...\example.json`). With the changes in this commit, the path to the example file is resolved to a fully-qualified path before being passed into the `res.sendFile` call. For example, a path on Windows of `\Users\myuser\...\example.json` would be transformed to `C:\\Users\\myuser\\...\\example.json` before being passed along to the `sendFile` function. This change allows the file to be loaded and sent properly to the browser client and for the embedded flows in the example to be loaded in the flow editor. --- red/api/editor/library.js | 5 +++-- test/red/api/editor/library_spec.js | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/red/api/editor/library.js b/red/api/editor/library.js index d5cbb020f..fd9c2d085 100644 --- a/red/api/editor/library.js +++ b/red/api/editor/library.js @@ -102,9 +102,10 @@ module.exports = { var fullPath = redNodes.getNodeExampleFlowPath(module,path); if (fullPath) { try { - fs.statSync(fullPath); + var resolvedPath = fspath.resolve(fullPath); + fs.statSync(resolvedPath); log.audit({event: "library.get",type:"flow",path:req.params[0]},req); - return res.sendFile(fullPath,{ + return res.sendFile(resolvedPath,{ headers:{ 'Content-Type': 'application/json' } diff --git a/test/red/api/editor/library_spec.js b/test/red/api/editor/library_spec.js index 65e53e459..f921596a3 100644 --- a/test/red/api/editor/library_spec.js +++ b/test/red/api/editor/library_spec.js @@ -225,7 +225,7 @@ describe("api/editor/library", function() { throw err; } res.body.should.have.property('sendFile', - 'node-module:example-one'); + fspath.resolve('node-module') + ':example-one'); done(); }); }); @@ -243,7 +243,8 @@ describe("api/editor/library", function() { throw err; } res.body.should.have.property('sendFile', - '@org_scope/node_package:example-one'); + fspath.resolve('@org_scope/node_package') + + ':example-one'); done(); }); });