Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No source provided #2

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
806edf8
Update deps
TinyPandas Feb 17, 2021
69dbe81
Update info
TinyPandas Feb 17, 2021
f1cef66
Update readme
TinyPandas Feb 17, 2021
ded76c0
Final info update
TinyPandas Feb 17, 2021
633db5d
Updated API url
TinyPandas Feb 19, 2021
7ec45d4
Updated to v0.1.3
TinyPandas Feb 19, 2021
00f1eeb
Actually updated api url
TinyPandas Feb 19, 2021
a4bc767
Fix typo in readme.md
TinyPandas Feb 19, 2021
a460fdf
Replaced request with https
TinyPandas Feb 22, 2021
0b59c41
Fixed res.on(data) callback spam
TinyPandas Mar 14, 2021
ad7b9db
Fixed testing conditions to follow changes from wandbox updates over …
TinyPandas Mar 22, 2021
a18b6ff
Update README.md
TinyPandas Mar 22, 2021
bf49a9f
Fixed duplicate variable declaration in file.js
TinyPandas Mar 22, 2021
6d3c6ee
Update file tests
TinyPandas Mar 22, 2021
590e60c
Update file.js error result
TinyPandas Mar 22, 2021
809d234
Fixed tests failing on request timeout
TinyPandas Mar 22, 2021
b63a5ac
Forgot to define start in file.js
TinyPandas Mar 22, 2021
cb69a8d
Removed stable from travis builds
TinyPandas Mar 22, 2021
2171e81
Implement fromStringV2.
TinyPandas Apr 26, 2021
0bb430b
Implement fileV2
TinyPandas Apr 26, 2021
c909d12
Updated docs for v2
TinyPandas Apr 26, 2021
9f93df2
Merge pull request #1 from TinyPandas/develop
TinyPandas Apr 26, 2021
cdded8c
Implement interface Opts
TinyPandas Apr 26, 2021
dff313c
Updated to v0.2.1 to incorporate `Opts` typing
TinyPandas Apr 26, 2021
91c681c
Merge pull request #2 from TinyPandas/develop
TinyPandas Apr 26, 2021
99526c5
Update typings
TinyPandas May 2, 2021
ede07ef
update typings for res
TinyPandas May 2, 2021
131b247
Fixed package.json
TinyPandas May 2, 2021
7fcc0b7
Update version fix
TinyPandas May 2, 2021
7232982
Merge pull request #3 from TinyPandas/develop
TinyPandas May 2, 2021
1d0254f
Fixed JSON.parse SyntaxError
TinyPandas May 4, 2021
8486f27
Update to v0.2.4
TinyPandas May 4, 2021
1dee046
Implemented API message/error fields on Result
TinyPandas May 7, 2021
2092a27
Began implementation of v0.3.0 promise update
TinyPandas May 8, 2021
685d792
Deprecate v1 methods.
TinyPandas May 8, 2021
fa6aa18
Minor cleanup
TinyPandas May 8, 2021
e4e06d2
Complete rewrite of api
TinyPandas May 18, 2021
8e04c4e
Complete rewrite of api
TinyPandas May 18, 2021
585db3b
Update README.md
TinyPandas May 18, 2021
7c2c1c3
Merge pull request #4 from TinyPandas/develop
TinyPandas May 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
language: node_js
node_js:
- 'stable'
- '5'
- '4'
- '3'
- '2'
- '1'
- '0.12'
- '0.10'
- '14'
- '13'
- '12'
before_install:
- npm update -g npm
after_script:
Expand Down
82 changes: 61 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,70 @@
Node Wandbox API
===
[![NPM version][npm-image]][npm-url] [![Build Status][build-image]][build-url] [![Coverage Status][coverage-image]][coverage-url] [![Dependencies][dependencies-image]][dependencies-url]
[![NPM version][npm-image]][npm-url] [![Build Status][build-image]][build-url] [![Dependencies][dependencies-image]][dependencies-url]

> Access Social Compilation Service [Wandbox][wandbox] via API from Node.js.


## Installation

``` bash
$ npm install wandbox-api
$ npm install wandbox-api-updated
```

# > v1.0.0 Usage
``` javascript
var { fromString, fromFile, getCompilers } = require('../lib/index');

fromString({
code: "print(\"Hello World!\")",
compiler: "lua-5.4.0",
}).then(console.log).catch(console.error);

fromFile("./test/fixtures/gamma.cpp",
{
compiler: "gcc-head"
}
).then(console.log).catch(console.error);

getCompilers("Lua").then(console.log).catch(console.error);
```

# < v1.0.0 Usage

## V2 Usage
``` javascript
var { fromFileV2, fromStringV2 } = require( 'wandbox-api-updated' );
```

#### fromFileV2( srcFile, opts, clbk [, dest] )
#### fromStringV2( opts, clbk [, dest])

With opts now being required for `fromStringV2`, setting `opts.code` to the `src` is replacing the `code` parameter of `runWandbox`.

## fromStringV2 Example
``` javascript
const runWandbox = require('../lib/index');

let res = runWandbox.fromStringv2(
{
compiler: "lua-5.4.0",
codes: [],
options: "",
code: "print 'Hello, Wandbox!'",
save: false,
timeout: 30000
},
function done(error, res) {
console.log(error);
console.log(res);
}
);
```

## Usage

``` javascript
var runWandbox = require( 'wandbox-api' );
var runWandbox = require( 'wandbox-api-updated' );
```

#### runWandbox( [dest,] src[, opts], clbk )
Expand Down Expand Up @@ -152,7 +201,7 @@ runWandbox.fromString( code, {
## Examples

``` javascript
var runWandbox = require( 'wandbox-api' );
var runWandbox = require( 'wandbox-api-updated' );

// String:

Expand Down Expand Up @@ -190,7 +239,7 @@ $ DEBUG=* node ./examples/index.js
To use the module as a general utility, install the module globally

``` bash
$ npm install -g wandbox-api
$ npm install -g wandbox-api-updated
```


Expand Down Expand Up @@ -293,26 +342,17 @@ $ make view-browser-tests

## Copyright

Copyright &copy; 2016. Philipp Burckhardt.


[npm-image]: http://img.shields.io/npm/v/wandbox-api.svg
[npm-url]: https://npmjs.org/package/wandbox-api

[build-image]: http://img.shields.io/travis/Planeshifter/node-wandbox-api/master.svg
[build-url]: https://travis-ci.org/Planeshifter/node-wandbox-api
Copyright &copy; 2021. Chris Barwick.

[coverage-image]: https://img.shields.io/codecov/c/github/Planeshifter/node-wandbox-api/master.svg
[coverage-url]: https://codecov.io/github/Planeshifter/node-wandbox-api?branch=master

[dependencies-image]: http://img.shields.io/david/Planeshifter/node-wandbox-api.svg
[dependencies-url]: https://david-dm.org/Planeshifter/node-wandbox-api
[npm-image]: http://img.shields.io/npm/v/wandbox-api-updated.svg
[npm-url]: https://npmjs.org/package/wandbox-api-updated

[dev-dependencies-image]: http://img.shields.io/david/dev/Planeshifter/node-wandbox-api.svg
[dev-dependencies-url]: https://david-dm.org/dev/Planeshifter/node-wandbox-api
[build-image]: http://img.shields.io/travis/TinyPandas/node-wandbox-api/master.svg
[build-url]: https://travis-ci.org/github/TinyPandas/node-wandbox-api

[github-issues-image]: http://img.shields.io/github/issues/Planeshifter/node-wandbox-api.svg
[github-issues-url]: https://github.com/Planeshifter/node-wandbox-api/issues
[dependencies-image]: http://img.shields.io/david/TinyPandas/node-wandbox-api.svg
[dependencies-url]: https://david-dm.org/TinyPandas/node-wandbox-api

[tape]: https://github.com/substack/tape
[istanbul]: https://github.com/gotwarlost/istanbul
Expand Down
5 changes: 3 additions & 2 deletions lib/defaults.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"compiler": "gcc-head",
"codes": [],
"options": "boost-1.60,warning,gnu++1y",
"options": "boost-1.73.0-gcc-head,warning,gnu++14",
"stdin": "",
"compiler-option-raw": "",
"runtime-option-raw": "",
"save": false
"save": false,
"timeout": 30000
}
228 changes: 89 additions & 139 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -1,147 +1,97 @@
'use strict';

// MODULES //

var debug = require( 'debug' )( 'wandbox-api:file' );
var copy = require( 'utils-copy' );
const { readFile } = require('fs');
var https = require( 'https' );
var cwd = require( 'utils-cwd' );
var readFile = require( 'utils-fs-read-file' );
var writeFile = require( 'fs' ).writeFile;
var request = require( 'request' );
var resolve = require( 'path' ).resolve;
var typeName = require( 'type-name' );
var defaults = require( './defaults.json' );
var validate = require( './validate.js' );


// VARIABLES //

var interfaces = {
'string,string,Object,function': [ 0, 1, 2, 3 ],
'string,string,function': [ 0, 1, -1, 2 ],
'string,Object,function': [ -1, 0, 1, 2 ],
'string,function': [ -1, 0, -1, 1 ]
};


// FILE //
var resolveFile = require( 'path' ).resolve;

/**
* FUNCTION: file( [dest,] src[, opts], clbk )
* Execute source file on Wandbox and fetch results.
*
* @param {String} [dest] - output file path
* @param {String} src - file to run on Wandbox
* @param {Object} [opts] - function options
* @param {String} [opts.compiler="gcc-head"] - name of used compiler
* @param {Array} [opts.codes=[]] - additional codes, objects with `file` and `code` keys
* @param {String} [opts.options="boost-1.60,warning,gnu++1y"] - used options for a compiler joined by comma.
* @param {String} [opts.stdin=""] - standard input
* @param {String} [opts.compiler-option-raw=""] - additional compile-time options joined by line-break
* @param {String} [opts.runtime-option-raw=""] - additional run-time options joined by line-break
* @param {Boolean} [opts.save=false] - boolean indicating whether permanent link should be generated
* @param {Function} clbk - callback to invoke after receiving results from Wandbox
* @returns {Void}
*/
function file() {
var outFile;
var options;
var inputFile;
var types;
var dest;
var opts;
var clbk;
var src;
var err;
var dir;
var idx;
var len;
var i;

len = arguments.length;
types = new Array( len );
for ( i = 0; i < len; i++ ) {
types[ i ] = typeName( arguments[ i ] );
}
types = types.join( ',' );
idx = interfaces[ types ];
if ( idx === void 0 ) {
throw new Error( 'invalid input argument(s). No implementation matching `f('+types+')`.' );
}
if ( idx[ 0 ] >= 0 ) {
dest = arguments[ idx[0] ];
}
src = arguments[ idx[1] ];
if ( idx[ 2 ] >= 0 ) {
options = arguments[ idx[2] ];
}
clbk = arguments[ idx[3] ];

opts = copy( defaults );
if ( options ) {
err = validate( opts, options );
if ( err ) {
throw err;
*
* @param {String} srcFile - file to run on Wandbox
* @param {Array} opts - function options
* @returns JSON Result or String
*/
module.exports = function string(srcFile, opts) {
return new Promise((resolve, reject) => {
if (!srcFile) {
reject("No source provided.");
}
}
dir = cwd();
debug( 'Current working directory: %s', dir );

inputFile = resolve( dir, src );
debug( 'Input file: %s', inputFile );

readFile( inputFile, {'encoding':'utf8'}, onFile );
/**
* FUNCTION: onFile( error, file )
* Callback invoked upon reading a file.
*
* @private
* @param {Error|Null} error - error object
* @param {String} data - file contents
* @returns {Void}
*/
function onFile( error, data ) {
if ( error ) {
debug( 'Error encountered while attempting to read a input file %s: %s', inputFile, error.message );
return done( error );
}
debug( 'Successfully read input file: %s', inputFile );

// Add `code` key to options holding the source code to run on Wandbox...
opts.code = data;

request.post( 'http://melpon.org/wandbox/api/compile.json', {
'body': opts,
'json': true
}, done );
}
/**
* FUNCTION: done( error, message, body )
* Callback invoked after resolving POST request to Wandbox.
*
* @private
* @param {Error|Null} error - error object
* @param {Object} message - an http.IncomingMessage
* @param {Object} body - response body
* @returns {Void}
*/
function done( error, message, body ) {
error = error || null;
body = body || null;

// Save response body if `dest` is supplied:
if ( dest !== void 0 ) {
outFile = resolve( dir, dest );
writeFile( outFile, JSON.stringify( body ), function( err ) {
clbk( err, body );
});
} else {
clbk( error, body );
let dir = cwd();
let inputFile = resolveFile( dir, srcFile);

readFile( inputFile, {'encoding':'utf-8'}, onFile);

function onFile( error, data ) {
if ( error ) {
reject(`[${error.name}]: ${error.message}`);
}

opts.code = data;

require('./getCompilerList')().then(list => {
let found = false;
list.forEach(compiler => {
if (compiler.name.toLowerCase() === opts.compiler.toLowerCase()) {
found = true;
}
});

if (!found) {
reject("Invalid compiler supplied.");
} else {
const post_data = JSON.stringify(opts);

const params = {
hostname: 'wandbox.org',
port: 443,
path: '/api/compile.json',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data)
}
}

let e = null;

let data = "";
let start = Date.now();

const req = https.request(params, (res) => {
res.setEncoding('utf8')
res.on('data', function(chunk) {
if ((Date.now() - start) > opts.timeout) {
e = new Error('Request Timed out.');
req.destroy();
}
data += chunk;
})

res.on('close', function() {
if (e) {
data = '{ "response":"Request timed out." }';
reject(`[${e.name}]: ${e.message}`)
} else {
try {
resolve(JSON.parse(data))
} catch (err) {
reject(`[${err.name}]: ${err.message}`)
}
}
});
})

req.on('error', error => {
e = error;
})

req.write(post_data);
req.end();
}
}).catch(e => {
reject("Failed to gather list of compilers.");
})
}
} // end FUNCTION done()
} // end FUNCTION file()


// EXPORTS //

module.exports = file;
});
}
Loading