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

Exploratory: Alternative Node.js target implementation #32

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.hx]
indent_style = tab
indent_size = 4
31 changes: 18 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
name: CI

on: [push,pull_request]
on: [push, pull_request]

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: true
os:
- ubuntu-latest
- windows-latest
- macos-13
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1 #nodejs
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '14'
- uses: pxshadow/setup-hashlink@v1.0.1 #hashlink

- name: install lix
node-version: "lts/*"
- uses: pxshadow/setup-hashlink@v1.0.4
- name: Install lix
run: npm i lix -g
- name: run lix
- name: Download Haxe and library dependencies
run: npx lix download

- name: build test
- name: Build test suite
run: npx haxe tests.hxml
- name: run test
- name: Run test suite for Hashlink target
run: hl test.hl
- name: Install Node.js dependencies
run: npm add deasync
- name: Run test suite for Node.js target
run: node test.js
20 changes: 17 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
*.hl
/*.hx
*.n
*.js
bin
*.cppia
*.py
*.txt
*.txt

# Hashlink
*.hl

# JS
package.json
package-lock.json
node_modules/
*.js
*.js.map

# Misc. tools
.mise.toml
flamegraph.html
*.cpuprofile
*.log
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"[haxe]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.sortImports": true
"source.sortImports": "explicit"
}
},
"testExplorer.codeLens": false
Expand Down
7 changes: 7 additions & 0 deletions haxe_libraries/hxnodejs.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @install: lix --silent download "haxelib:/hxnodejs#12.2.0" into hxnodejs/12.2.0/haxelib
-cp ${HAXE_LIBCACHE}/hxnodejs/12.2.0/haxelib/src
-D hxnodejs=12.2.0
--macro allowPackage('sys')
# should behave like other target defines and not be defined in macro context
--macro define('nodejs')
--macro _internal.SuppressDeprecated.run()
3 changes: 3 additions & 0 deletions tests-common.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-cp tests
-main Test
--macro nullSafety("weblink._internal.ds", StrictThreaded)
5 changes: 5 additions & 0 deletions tests-hashlink.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tests-common.hxml

--macro nullSafety("weblink._internal.hashlink", StrictThreaded)

-hl test.hl
9 changes: 9 additions & 0 deletions tests-nodejs.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests-common.hxml

--library hxnodejs
--define haxeJSON
--define js-es=6
--define source-map-content
--macro nullSafety("weblink._internal.nodejs", StrictThreaded)

-js test.js
9 changes: 5 additions & 4 deletions tests.hxml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-cp tests
-main Test
--macro nullSafety("weblink._internal.ds", StrictThreaded)
-hl test.hl
--each

tests-hashlink.hxml
--next
tests-nodejs.hxml
8 changes: 0 additions & 8 deletions tests.sh

This file was deleted.

29 changes: 11 additions & 18 deletions tests/Request.hx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import haxe.Http;
using TestingTools;

class Request {
public static function main() {
Sys.println("start test");
trace("Starting Request Test");
var app = new weblink.Weblink();
var data = haxe.io.Bytes.ofString(Std.string(Std.random(10 * 1000))).toHex();
app.get("/", function(request, response) {
Expand All @@ -14,24 +14,17 @@ class Request {
app.put("/", function(request, response) {
response.send(data + request.data);
});
app.listen(2000, false);
app.listenBackground(2000);

sys.thread.Thread.create(() -> {
var response = Http.requestUrl("http://localhost:2000");
if (response != data)
throw "post response data does not match: " + response + " data: " + data;
var http = new Http("http://localhost:2000");
http.setPostData(data);
http.request(false);
if (http.responseData != data + data)
throw "post response data does not match: " + http.responseData + " data: " + data + data;
app.close();
});
var responseGet = "http://localhost:2000".GET();
if (responseGet != data)
throw "post response data does not match: " + responseGet + " data: " + data;

var responsePost = "http://localhost:2000".POST(data);
if (responsePost != data + data)
throw "post response data does not match: " + responsePost + " data: " + data + data;

while (app.server.running) {
app.server.update(false);
Sys.sleep(0.2);
}
app.close();
trace("done");
}
}
31 changes: 14 additions & 17 deletions tests/TestCompression.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import haxe.zip.Compress;
import weblink.Compression;
import weblink.Weblink;

using TestingTools;

class TestCompression {
public static function main() {
trace("Starting Content-Encoding Test");
Expand All @@ -14,27 +16,22 @@ class TestCompression {
app.get("/", function(request, response) {
response.sendBytes(bytes);
}, Compression.deflateCompressionMiddleware);
app.listen(2000, false);
app.listenBackground(2000);

sys.thread.Thread.create(() -> {
var http = new Http("http://localhost:2000");
var response:Bytes = null;
http.onBytes = function(bytes) {
response = bytes;
}
http.onError = function(e) {
throw e;
}
http.request(false);
var done = false;
var http = new Http("http://localhost:2000");
http.onBytes = function(response) {
if (response.compare(compressedData) != 0)
throw "get response compressed data does not match: " + response + " compressedData: " + compressedData;
app.close();
});

while (app.server.running) {
app.server.update(false);
Sys.sleep(0.2);
done = true;
}
http.onError = e -> throw e;
http.request(false);
#if nodejs
sys.NodeSync.wait(() -> done);
#end

app.close();
trace("done");
}
}
44 changes: 25 additions & 19 deletions tests/TestCookie.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import haxe.Http;
import weblink.Cookie;
import weblink.Weblink;

using TestingTools;

class TestCookie {
public static function main() {
Sys.println("Starting cookie Response Test");
trace("Starting cookie Response Test");
var app:Weblink;
var data:String;

Expand All @@ -15,27 +17,31 @@ class TestCookie {
response.cookies.add(new Cookie("foo", "bar"));
response.send(data);
});
app.listen(2000, false);
app.listenBackground(2000);

sys.thread.Thread.create(() -> {
var http = new Http("http://localhost:2000");
http.onStatus = function(status) {
if (status == 200) {
var headers = http.responseHeaders;
if (headers.get("Set-Cookie") != "foo=bar") {
throw 'Set-Cookie not foo=bar. got ${headers.get("Set-Cookie")}';
}
}
};
http.request(false);
var done = false;
var http = new Http("http://localhost:2000");
http.onStatus = status -> {
if (status != 200) {
throw "status not OK";
}
};
http.onData = _ -> {
#if (!nodejs || haxe >= version("4.3.0")) // see #10809
final headers = http.responseHeaders;
if (headers.get("Set-Cookie") != "foo=bar") {
throw 'Set-Cookie not foo=bar. got ${headers.get("Set-Cookie")}';
}
#end
done = true;
};
http.request(false);

app.close();
});
#if nodejs
sys.NodeSync.wait(() -> done);
#end

while (app.server.running) {
app.server.update(false);
Sys.sleep(0.2);
}
app.close();
trace("done");
}
}
25 changes: 8 additions & 17 deletions tests/TestMiddlewareCorrectOrder.hx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import haxe.Http;
import haxe.io.Bytes;
import sys.thread.Thread;
import weblink.Weblink;

using TestingTools;

class TestMiddlewareCorrectOrder {
static var v1:String = "";
static var v2:String = "";
Expand All @@ -29,23 +30,13 @@ class TestMiddlewareCorrectOrder {
});

app.get("/", (_, res) -> res.send('$v1$v2$v3'));
app.listen(2000, false);

Thread.create(() -> {
final http = new Http("http://localhost:2000");
var response:Null<Bytes> = null;
http.onBytes = bytes -> response = bytes;
http.onError = e -> throw e;
http.request(false);
if (response.toString() != "bazbarfoo")
throw "not the response we expected";
app.close();
});
app.listenBackground(2000);

final response = "http://localhost:2000".GET();
if (response != "bazbarfoo")
throw "not the response we expected";
app.close();

while (app.server.running) {
app.server.update(false);
Sys.sleep(0.2);
}
trace("done");
}
}
25 changes: 7 additions & 18 deletions tests/TestMiddlewareShortCircuit.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// For a middleware that does not cut the request short, see TestCompression.
import haxe.Http;
import haxe.io.Bytes;
import sys.thread.Thread;
import weblink.Weblink;

using TestingTools;

class TestMiddlewareShortCircuit {
public static function main() {
trace("Starting Middleware Short Circuit Test");
Expand All @@ -12,23 +11,13 @@ class TestMiddlewareShortCircuit {
app.get("/", (_, _) -> throw "should not be called", next -> {
return (_, res) -> res.send("foo");
});
app.listen(2000, false);
app.listenBackground(2000);

Thread.create(() -> {
final http = new Http("http://localhost:2000");
var response:Null<Bytes> = null;
http.onBytes = bytes -> response = bytes;
http.onError = e -> throw e;
http.request(false);
if (response.toString() != "foo")
throw "not the response we expected";
app.close();
});
final response = "http://localhost:2000".GET();
if (response != "foo")
throw "not the response we expected";

while (app.server.running) {
app.server.update(false);
Sys.sleep(0.2);
}
app.close();
trace("done");
}
}
Loading
Loading