diff --git a/README.md b/README.md index a12cd5e..d37bb95 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,26 @@ Steps: * Set node_dir to the path of your Node.js clone * Set release_dir if desired (optional if copyrelease is not used) * Run "build.bat [x86|x64|arm] [copyrelease]" + +##To test: +Follow the steps below to run [tests](https://github.com/joyent/node/tree/master/test) included with Node.js. + + * Clone Node.js from https://github.com/microsoft/node + * Install the NTVS IoT Extension using the steps [here](http://ms-iot.github.io/content/en-US/win10/samples/NodejsWU.htm) and create a new Node.js (Windows Universal) project + * Copy <Node.js clone path>\tests to <Node.js UWP project path (location of .njsproj file)>\tests + * Right click on the test you want to run and select "Set as Node.js Startup File". The file text will be made bold (see test-assert.js example below) + ![Set test as Startup File]({{site.baseurl}}/images/test-startup-file.png) + * Press F5 (or click on Debug->Start Debugging menu) to run the test + * Console output can be redirected to file. You can view the logs on the device in c:\Users\DefaultAccount\AppData\Local\Packages\<Your app ID (get it from VS build logs)>\LocalState\nodeuwp.log + +##Node.js compatibility with UWP +The following API's are not supported in Node.js UWP: + + * Child Processes + * Cluster + * Debugger + * TTY + +There are a few other limitations in other modules. For example, the [fs](https://nodejs.org/api/fs.html) module can only access files within its the UWP package or within paths declared in the [package capabilities](https://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx). + +Detailed documentation on which API's are supported in Node.js UWP can be found [here]({{site.baseurl}}/compatibility.xlsx). diff --git a/build.bat b/build.bat index 2d1616a..da156d3 100644 --- a/build.bat +++ b/build.bat @@ -41,7 +41,7 @@ set buildall=1 :arm @rem build node.dll -call "%node_dir%\vcbuild.bat" arm chakra uwp-dll withoutssl +call "%node_dir%\vcbuild.bat" arm chakra uwp-dll openssl-no-asm pushd %batch_dir% @rem build nodeuwp.dll set WindowsSdkDir=%programfiles(x86)%\Windows Kits\10\ @@ -56,7 +56,7 @@ if not defined buildall goto end :x86 @rem build node.dll -call "%node_dir%\vcbuild.bat" x86 chakra uwp-dll withoutssl +call "%node_dir%\vcbuild.bat" x86 chakra uwp-dll pushd %batch_dir% @rem build nodeuwp.dll set WindowsSdkDir=%programfiles(x86)%\Windows Kits\10\ @@ -71,7 +71,7 @@ if not defined buildall goto end :x64 @rem build node.dll -call "%node_dir%\vcbuild.bat" x64 chakra uwp-dll withoutssl +call "%node_dir%\vcbuild.bat" x64 chakra uwp-dll pushd %batch_dir% @rem build nodeuwp.dll set WindowsSdkDir=%programfiles(x86)%\Windows Kits\10\ diff --git a/compatibility.xlsx b/compatibility.xlsx new file mode 100644 index 0000000..f895d7d Binary files /dev/null and b/compatibility.xlsx differ diff --git a/images/test-startup-file.png b/images/test-startup-file.png new file mode 100644 index 0000000..08dc010 Binary files /dev/null and b/images/test-startup-file.png differ diff --git a/nodeuwp/Logger.cpp b/nodeuwp/Logger.cpp new file mode 100644 index 0000000..d988ff4 --- /dev/null +++ b/nodeuwp/Logger.cpp @@ -0,0 +1,75 @@ +/* + Copyright(c) Microsoft Open Technologies, Inc. All rights reserved. + + The MIT License(MIT) + + 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. +*/ + +#include "pch.h" +#include "Logger.h" +#include +#include + +using namespace Windows::Storage; +using namespace Windows::Foundation; +using namespace Platform; +using namespace concurrency; + +namespace nodeuwp +{ + std::unique_ptr Logger::s_pInstance; + + const Logger& Logger::GetInstance() + { + if (s_pInstance == nullptr) + { + if (s_pInstance == nullptr) + { + s_pInstance.reset(new (std::nothrow) Logger()); + } + } + return *s_pInstance.get(); + } + + Logger::Logger() + { + // Create a new file in the local folder if it doesn't exist + StorageFolder^ localFolder = ApplicationData::Current->LocalFolder; + String^ logFile = "nodeuwp.log"; + auto createFileTask = create_task(localFolder->CreateFileAsync(logFile, Windows::Storage::CreationCollisionOption::OpenIfExists)); + createFileTask.then([this](StorageFile^ file) { + m_file = file; + }); + } + + void Logger::Log(ILogger::LogLevel logLevel, const char* str) const + { + // Convert char* to Platform::String + size_t newsize = strlen(str) + 1; + wchar_t * wcstring = new wchar_t[newsize]; + size_t convertedChars = 0; + mbstowcs_s(&convertedChars, wcstring, newsize, str, _TRUNCATE); + String^ pstr = ref new String(wcstring); + + // Append console logs to file + IAsyncAction^ Action = FileIO::AppendTextAsync(m_file, pstr + "\r\n"); + create_task(Action).then([this](){}).wait(); // wait is required to log messages sequentially. + } +} \ No newline at end of file diff --git a/nodeuwp/Logger.h b/nodeuwp/Logger.h new file mode 100644 index 0000000..7b62975 --- /dev/null +++ b/nodeuwp/Logger.h @@ -0,0 +1,48 @@ +/* + Copyright(c) Microsoft Open Technologies, Inc. All rights reserved. + + The MIT License(MIT) + + 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. +*/ + +#pragma once + +#include +#include "ILogger.h" +#include + +using namespace Windows::Storage; + +namespace nodeuwp +{ + // Logger class routes log messages to file + class Logger : public node::logger::ILogger + { + public: + static const Logger &GetInstance(); + ~Logger() {} + virtual void Log(ILogger::LogLevel logLevel, const char* str) const override; + + private: + Logger(); + StorageFile^ m_file; + static std::unique_ptr s_pInstance; + }; +} \ No newline at end of file diff --git a/nodeuwp/StartupTask.cpp b/nodeuwp/StartupTask.cpp index cfab40c..874e739 100644 --- a/nodeuwp/StartupTask.cpp +++ b/nodeuwp/StartupTask.cpp @@ -27,6 +27,7 @@ #include #include "v8.h" #include "node.h" +#include "Logger.h" using namespace nodeuwp; @@ -115,6 +116,7 @@ void StartupTask::Run(IBackgroundTaskInstance^ taskInstance) getStartupInfoXml.then([=](XmlDocument^ startupInfoXml) { std::vector> argumentVector; + bool useLogger = true; //TODO: set value from a property in Visual Studio std::shared_ptr argChar = PlatformStringToChar(L" ", 1); argumentVector.push_back(argChar); @@ -138,7 +140,14 @@ void StartupTask::Run(IBackgroundTaskInstance^ taskInstance) argv.get()[i] = (argumentVector[i]).get(); } - node::Start(argc, argv.get()); + if (!useLogger) + { + node::Start(argc, argv.get()); + } + else + { + node::Start(argc, argv.get(), &Logger::GetInstance()); + } deferral->Complete(); }); }); diff --git a/nodeuwp/nodeuwp.vcxproj b/nodeuwp/nodeuwp.vcxproj index 59fbc79..ce62b6a 100644 --- a/nodeuwp/nodeuwp.vcxproj +++ b/nodeuwp/nodeuwp.vcxproj @@ -118,13 +118,13 @@ Use - _WINRT_DLL;%(PreprocessorDefinitions) + _WINRT_DLL;UWP_DLL;%(PreprocessorDefinitions) pch.h $(IntDir)pch.pch $(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories) /bigobj %(AdditionalOptions) 28204 - $(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;%(AdditionalIncludeDirectories) + $(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;$(NODE_DIR)\deps\logger\include;%(AdditionalIncludeDirectories) Console @@ -135,13 +135,13 @@ Use - _WINRT_DLL;NDEBUG;%(PreprocessorDefinitions) + _WINRT_DLL;NDEBUG;UWP_DLL;%(PreprocessorDefinitions) pch.h $(IntDir)pch.pch $(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories) /bigobj %(AdditionalOptions) 28204 - $(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;%(AdditionalIncludeDirectories) + $(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;$(NODE_DIR)\deps\logger\include;%(AdditionalIncludeDirectories) Console @@ -153,7 +153,8 @@ - setx NODE_DIR E:\Repos\my_ntvsiot + + @@ -175,13 +176,13 @@ Use - _WINRT_DLL;NDEBUG;%(PreprocessorDefinitions) + _WINRT_DLL;NDEBUG;UWP_DLL;%(PreprocessorDefinitions) pch.h $(IntDir)pch.pch $(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories) /bigobj %(AdditionalOptions) 28204 - $(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;%(AdditionalIncludeDirectories) + $(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;$(NODE_DIR)\deps\logger\include;%(AdditionalIncludeDirectories) Console @@ -209,13 +210,13 @@ Use - _WINRT_DLL;NDEBUG;%(PreprocessorDefinitions) + _WINRT_DLL;NDEBUG;UWP_DLL;%(PreprocessorDefinitions) pch.h $(IntDir)pch.pch $(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories) /bigobj %(AdditionalOptions) 28204 - $(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;%(AdditionalIncludeDirectories) + $(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;$(NODE_DIR)\deps\logger\include;%(AdditionalIncludeDirectories) Console @@ -237,10 +238,12 @@ + + Create Create