Repository: Microsoft/node-uwp
Branch: master
Commit: cbb40f319374
Files: 11
Total size: 17.2 KB
Directory structure:
gitextract_m2z66kne/
├── .gitignore
├── LICENSE
├── README.md
├── SECURITY.md
├── binding.gyp
├── index.js
├── package.json
├── src/
│ ├── node-async.h
│ ├── uwp.cc
│ └── uwp.h
└── test/
└── index.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
build
node_modules
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2015 Microsoft
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.
================================================
FILE: README.md
================================================
node-uwp
==========
Enables Universal Windows Platform (UWP) API access for Node.js (Chakra build)
on Windows 10.
Example
-------
```javascript
const uwp = require('uwp');
const Windows = uwp.projectNamespace('Windows');
Windows.Storage.KnownFolders.documentsLibrary.createFileAsync(
'sample.dat', Windows.Storage.CreationCollisionOption.replaceExisting)
.done(
function (file) {
console.log('ok');
uwp.close(); // all async operations are completed, release uwp
},
function (error) {
console.error('error', error);
uwp.close(); // all async operations are completed, release uwp
}
);
```
Installation
------------
### Prerequisites
* Windows 10 [1511 or above](http://windows.microsoft.com/en-us/windows-10/windows-update-faq)
* [Visual Studio](https://www.visualstudio.com/vs-2015-product-editions)
* [Node.js (Chakra)](http://aka.ms/node-chakra-installer)
Run under Node.js (Chakra) command prompt:
```sh
npm install uwp
```
APIs
----
This package exports 2 functions.
### projectNamespace(name)
Project a UWP namespace of given name.
* **Note**: This function will keep Node process alive so that your app can
continue to run and handle UWP async callbacks. You need to call
[close()](#close) when UWP usage is completed.
### close()
Close all UWP handles used by this package. Call this when all UWP usage is
completed.
---------------------------------------------------------------------------
Checkout our OSS effort with
[Node-ChakraCore](https://github.com/nodejs/node-chakracore). It supports the
most recent version of node.js and will also be useful if you are on Windows 7
or Windows 8.1. Note: It does not support UWP.
================================================
FILE: SECURITY.md
================================================
## Security
Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/).
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below.
## Reporting Security Issues
**Please do not report security vulnerabilities through public GitHub issues.**
Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report).
If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey).
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc).
Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
* Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
* Full paths of source file(s) related to the manifestation of the issue
* The location of the affected source code (tag/branch/commit or direct URL)
* Any special configuration required to reproduce the issue
* Step-by-step instructions to reproduce the issue
* Proof-of-concept or exploit code (if possible)
* Impact of the issue, including how an attacker might exploit the issue
This information will help us triage your report more quickly.
If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs.
## Preferred Languages
We prefer all communications to be in English.
## Policy
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd).
================================================
FILE: binding.gyp
================================================
{
'targets': [{
'target_name': 'uwp',
"include_dirs" : [
"
namespace NodeUtils {
extern std::thread::id g_mainThreadId;
class Async {
private:
class TokenData {
private:
std::function func;
friend Async;
public:
static uv_async_t* NewAsyncToken() {
uv_async_t* asyncHandle = new uv_async_t;
uv_async_init(uv_default_loop(), asyncHandle, AsyncCb);
asyncHandle->data = new TokenData();
return asyncHandle;
}
};
public:
static uv_async_t* GetAsyncToken() {
return TokenData::NewAsyncToken();
}
static void ReleaseAsyncToken(uv_async_t* handle) {
TokenData* tokenData = static_cast(handle->data);
uv_close(reinterpret_cast(handle), AyncCloseCb);
delete tokenData;
}
static void RunOnMain(uv_async_t* async, std::function func) {
TokenData* tokenData = static_cast(async->data);
tokenData->func = func;
uv_async_send(async);
}
static void RunOnMain(std::function func) {
if (std::this_thread::get_id() == g_mainThreadId) {
func();
} else {
uv_async_t *async = GetAsyncToken();
RunOnMain(async, func);
}
}
private:
// called after the async handle is closed in order to free it's memory
static void AyncCloseCb(uv_handle_t* handle) {
if (handle != nullptr) {
uv_async_t* async = reinterpret_cast(handle);
delete async;
}
}
// Called by run on main in case we are not running on the main thread
static void AsyncCb(uv_async_t* handle) {
TokenData* tokenData = static_cast(handle->data);
tokenData->func();
ReleaseAsyncToken(handle);
}
};
} // namespace NodeUtils
================================================
FILE: src/uwp.cc
================================================
// Copyright Microsoft. All rights reserved.
//
// 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 "uwp.h"
std::thread::id NodeUtils::g_mainThreadId;
UWPAddOn UWPAddOn::s_instance;
bool UWPAddOn::EnsureCoInitialized() {
if (!_coInitialized) {
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
_coInitialized = SUCCEEDED(hr);
if (!_coInitialized) {
Nan::ThrowError("CoInitializeEx failed");
}
}
return _coInitialized;
}
void UWPAddOn::EnsureCoUninitialized() {
if (_coInitialized) {
CoUninitialize();
_coInitialized = false;
}
}
bool UWPAddOn::EnsureKeepAlive() {
if (_keepAliveToken == nullptr) {
// Open an active async handler to keep Node alive
_keepAliveToken = Async::GetAsyncToken();
}
return _keepAliveToken != nullptr;
}
void UWPAddOn::ReleaseKeepAlive() {
if (_keepAliveToken != nullptr) {
auto token = _keepAliveToken;
_keepAliveToken = nullptr;
Async::ReleaseAsyncToken(token);
}
}
void UWPAddOn::Init(Handle