Skip to content

Commit

Permalink
fix: more bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am committed May 28, 2024
1 parent 43e62fd commit ad81ad6
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 92 deletions.
3 changes: 2 additions & 1 deletion dev/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const filteredTestFiles = filtered?.map((res) => testFiles[res.refIndex]) ??

const tmpDir = join(projectDir, "tmp");
const env: Record<string, string> = {
"CLICOLOR_FORCE": "1",
"RUST_LOG": "off,xtask=debug,meta=debug",
"RUST_SPANTRACE": "1",
// "RUST_BACKTRACE": "short",
Expand All @@ -105,7 +106,7 @@ const env: Record<string, string> = {
"TMP_DIR": tmpDir,
"TIMER_MAX_TIMEOUT_MS": "30000",
"NPM_CONFIG_REGISTRY": "http://localhost:4873",
"PATH": `${Deno.env.get("PATH")}:${join(projectDir, "target/debug")}`,
"PATH": `${join(projectDir, "target/debug")}:${Deno.env.get("PATH")}`,
};

await Deno.mkdir(tmpDir, { recursive: true });
Expand Down
30 changes: 23 additions & 7 deletions libs/deno/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ use deno_runtime::deno_core as deno_core; // necessary for re-exported macros to
use std::path::PathBuf;

const DEFAULT_UNSTABLE_FLAGS: &[&str] = &["worker-options", "net"];
const CACHE_BLOCKLIST: &[&str] = &[
"npm:/@typegraph/sdk",
"npm:/@typegraph/sdk/",
"npm:@typegraph/sdk",
"npm:@typegraph/sdk/",
];

/// Ensure that the subcommand runs in a task, rather than being directly executed. Since some of these
/// futures are very large, this prevents the stack from getting blown out from passing them by value up
/// the callchain (especially in debug mode when Rust doesn't have a chance to elide copies!).
#[inline(always)]
fn spawn_subcommand<F: Future<Output = ()> + 'static>(f: F) -> JoinHandle<()> {
// FIXME: find a better location for this as tihs won't work
// if a new thread has already launched by this point
// this is only relevant for WebWorkers
if std::env::var("RUST_MIN_STACK").is_err() {
std::env::set_var("RUST_MIN_STACK", "8388608");
}
// the boxed_local() is important in order to get windows to not blow the stack in debug
deno_core::unsync::spawn(f.boxed_local())
}
Expand Down Expand Up @@ -83,6 +83,12 @@ pub async fn run(
.collect(),
..Default::default()
},
#[cfg(debug_assertions)]
cache_blocklist: CACHE_BLOCKLIST
.iter()
.cloned()
.map(str::to_string)
.collect(),
..Default::default()
};

Expand Down Expand Up @@ -195,7 +201,11 @@ pub async fn test(
config_flag: deno_config::ConfigFlag::Path(config_file.to_string_lossy().into()),
argv,
subcommand: args::DenoSubcommand::Test(test_flags.clone()),
cache_blocklist: vec!["npm:@typegraph/sdk".to_string()],
cache_blocklist: CACHE_BLOCKLIST
.iter()
.cloned()
.map(str::to_string)
.collect(),
..Default::default()
};

Expand Down Expand Up @@ -280,6 +290,12 @@ pub async fn test(
fn new_thread_builder() -> std::thread::Builder {
let builder = std::thread::Builder::new();
let builder = if cfg!(debug_assertions) {
// this is only relevant for WebWorkers
// FIXME: find a better location for this as tihs won't work
// if a new thread has already launched by this point
if std::env::var("RUST_MIN_STACK").is_err() {
std::env::set_var("RUST_MIN_STACK", "8388608");
}
// deno & swc need 8 MiB with dev profile (release is ok)
// https://github.com/swc-project/swc/blob/main/CONTRIBUTING.md
builder.stack_size(8 * 1024 * 1024)
Expand Down
19 changes: 7 additions & 12 deletions libs/metagen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ impl GeneratorRunner {
pub fn exec(&self, workspace_path: &Path, value: serde_json::Value) -> PluginOutputResult {
(self.op)(workspace_path, value)
}

pub fn get(name: &str) -> Option<GeneratorRunner> {
GENERATORS.with(|m| {
let out = m.get(name).cloned();
out
})
}
}

thread_local! {
Expand Down Expand Up @@ -130,18 +137,6 @@ thread_local! {
]);
}

impl GeneratorRunner {
pub fn get(name: &str) -> Option<GeneratorRunner> {
GENERATORS.with(|m| {
let out = m.get(name).cloned();
if out.is_none() {
warn!("it really aint here boss: {name}");
}
out
})
}
}

/// This function makes use of a JoinSet to process
/// items in parallel. This makes using actix workers in InputResolver
/// is a no no.
Expand Down
273 changes: 273 additions & 0 deletions typegate/deno.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions typegate/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ deno_core::extension!(
tg_metatype_ext,
ops = [
crate::op_get_version,
// #[cfg(test)]
// tests::op_obj_go_round,
typescript::op_typescript_format_code,
typegraph::op_typegraph_validate,
typegraph::op_validate_prisma_runtime_data,
Expand All @@ -44,6 +42,9 @@ deno_core::extension!(
wit_wire::op_wit_wire_init,
wit_wire::op_wit_wire_handle,
wit_wire::op_wit_wire_destroy,
// FIXME(yohe): this test broke and has proven difficult to fix
// #[cfg(test)]
// tests::op_obj_go_round,
],
// esm_entry_point = "ext:tg_metatype_ext/00_runtime.js",
// esm = ["00_runtime.js"],
Expand Down Expand Up @@ -92,6 +93,7 @@ pub mod tests {
b: String,
}

// FIXME: this is also broken for some reason
#[deno_core::op2]
#[serde]
pub fn op_obj_go_round(#[state] ctx: &TestCtx, #[serde] incoming: In) -> Result<Out> {
Expand Down
2 changes: 1 addition & 1 deletion typegate/import_map.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"imports": {
"native": "./engine/bindings.ts",
"std/": "https://deno.land/std@0.221.0/",
"std/": "https://deno.land/std@0.224.0/",
"compress/": "https://deno.land/x/[email protected]/",
"graphql": "npm:[email protected]",
"graphql/ast": "npm:[email protected]/language/ast.js",
Expand Down
13 changes: 6 additions & 7 deletions typegate/src/engine/typecheck/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ import {
Validator,
ValidatorFn,
} from "./common.ts";
import { typescript_format_code } from "native";

export function generateValidator(tg: TypeGraph, typeIdx: number): Validator {
const validatorName = (typeIdx: number) => `validate_${typeIdx}`;
const validatorCode = new InputValidationCompiler(tg, validatorName)
.generate(typeIdx);

console.log("validatorCode ----- START");
const formattedCode = typescript_format_code({
source: validatorCode,
});
console.log(formattedCode.Ok?.formatted_code);
console.log("validatorCode ----- END");
// console.log("validatorCode ----- START");
// const formattedCode = typescript_format_code({
// source: validatorCode,
// });
// console.log(formattedCode.Ok?.formatted_code);
// console.log("validatorCode ----- END");

const validator = new Function(validatorCode)() as ValidatorFn;
return (value: unknown) => {
Expand Down
70 changes: 10 additions & 60 deletions typegate/tests/typecheck/__snapshots__/typecheck_test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const snapshot = {};

snapshot[`typecheck 1`] = `
"function validate_66_1(value, path, errors, context) {
"function validate_54_1(value, path, errors, context) {
if (typeof value !== \\"object\\") {
errors.push([path, \`expected an object, got \${typeof value}\`]);
} else if (value == null) {
Expand All @@ -16,39 +16,39 @@ snapshot[`typecheck 1`] = `
}
}
function validate_8_2(value, path, errors, context) {
validate_27_3(value, path, errors, context);
validate_24_3(value, path, errors, context);
}
function validate_27_3(value, path, errors, context) {
function validate_24_3(value, path, errors, context) {
if (!Array.isArray(value)) {
errors.push([path, \`expected an array, got \${typeof value}\`]);
} else if (value.length > 20) {
errors.push([path, \`expected maximum items: 20, got \${value.length}\`]);
} else {
for (let i = 0; i < value.length; ++i) {
const item = value[i];
validate_28_4(value[i], path + \`[\${i}]\`, errors, context);
validate_25_4(value[i], path + \`[\${i}]\`, errors, context);
}
}
}
function validate_28_4(value, path, errors, context) {
function validate_25_4(value, path, errors, context) {
if (typeof value !== \\"object\\") {
errors.push([path, \`expected an object, got \${typeof value}\`]);
} else if (value == null) {
errors.push([path, \\"exptected a non-null object, got null\\"]);
} else {
const keys = new Set(Object.keys(value));
keys.delete(\\"id\\");
validate_29(value[\\"id\\"], path + \\".id\\", errors, context);
validate_3(value[\\"id\\"], path + \\".id\\", errors, context);
keys.delete(\\"title\\");
validate_30(value[\\"title\\"], path + \\".title\\", errors, context);
validate_26(value[\\"title\\"], path + \\".title\\", errors, context);
keys.delete(\\"author\\");
validate_2_5(value[\\"author\\"], path + \\".author\\", errors, context);
if (keys.size > 0) {
errors.push([path, \`unexpected fields: \${[...keys].join(\\", \\")}\`]);
}
}
}
function validate_29(value, path, errors, context) {
function validate_3(value, path, errors, context) {
if (typeof value !== \\"string\\") {
errors.push([path, \`expected a string, got \${typeof value}\`]);
} else {
Expand All @@ -61,7 +61,7 @@ function validate_29(value, path, errors, context) {
}
}
}
function validate_30(value, path, errors, context) {
function validate_26(value, path, errors, context) {
if (typeof value !== \\"string\\") {
errors.push([path, \`expected a string, got \${typeof value}\`]);
} else if (value.length < 10) {
Expand All @@ -86,19 +86,6 @@ function validate_2_5(value, path, errors, context) {
}
}
}
function validate_3(value, path, errors, context) {
if (typeof value !== \\"string\\") {
errors.push([path, \`expected a string, got \${typeof value}\`]);
} else {
const formatValidator = context.formatValidators[\\"uuid\\"];
if (formatValidator == null) {
errors.push([path, \\"unknown format 'uuid'\\"]);
} else if (!formatValidator(value)) {
errors.push([path,
\\"string does not statisfy the required format 'uuid'\\"]);
}
}
}
function validate_4(value, path, errors, context) {
if (typeof value !== \\"string\\") {
errors.push([path, \`expected a string, got \${typeof value}\`]);
Expand All @@ -112,43 +99,6 @@ function validate_4(value, path, errors, context) {
}
}
}
return validate_66_1;
"
`;
snapshot[`typecheck 2`] = `
"Validation errors:
- at <value>.posts[0].author: expected an object, got undefined
"
`;
snapshot[`typecheck 3`] = `
"Validation errors:
- at <value>.posts[0].author.username: expected a string, got undefined
"
`;
snapshot[`typecheck 4`] = `
"Validation errors:
- at <value>.posts[1].author.username: expected a string, got undefined
"
`;
snapshot[`typecheck 5`] = `
"Validation errors:
- at <value>.posts[0].author.id: string does not statisfy the required format 'uuid'
- at <value>.posts[0].author.email: string does not statisfy the required format 'email'
"
`;
snapshot[`typecheck 6`] = `
"Validation errors:
- at <value>.posts[0].author.email: string does not statisfy the required format 'email'
"
`;
snapshot[`typecheck 7`] = `
"Validation errors:
- at <value>.posts[0].author.website: string does not statisfy the required format 'uri'
return validate_54_1;
"
`;
3 changes: 1 addition & 2 deletions typegate/tests/typecheck/typecheck_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Meta.test("typecheck", async (t) => {

const getValidationCode = (query: string) => {
const [operation, fragments] = findOperation(parse(query), None);
console.log({ query, operation, fragments });
if (operation.isNone()) {
throw new Error("No operation found in the query");
}
Expand All @@ -34,7 +33,6 @@ Meta.test("typecheck", async (t) => {

const getValidator = (query: string) => {
const [operation, fragments] = findOperation(parse(query), None);
console.log({ query, operation, fragments });
if (operation.isNone()) {
throw new Error("No operation found in the query");
}
Expand Down Expand Up @@ -92,6 +90,7 @@ Meta.test("typecheck", async (t) => {
source: code,
}))!.formatted_code;

console.log("asserting snapshot", { formattedCode });
t.assertSnapshot(formattedCode);
});

Expand Down

0 comments on commit ad81ad6

Please sign in to comment.