cogsbox-sync 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,101 @@
1
+ import { SyncEngineShadowMetadata, SyncEngineShadowNode, TypeInfo } from '.';
2
+
3
+ function getTypeFromZodSchema(schema: any): TypeInfo | null {
4
+ if (!schema || !schema._def) return null;
5
+
6
+ let baseSchema = schema;
7
+ let isNullable = false;
8
+ let isOptional = false;
9
+ let defaultValue: any = undefined;
10
+ let hasDefault = false;
11
+
12
+ while (baseSchema._def) {
13
+ const typeName = baseSchema._def.typeName;
14
+ if (typeName === 'ZodOptional') {
15
+ isOptional = true;
16
+ baseSchema = baseSchema._def.innerType;
17
+ } else if (typeName === 'ZodNullable') {
18
+ isNullable = true;
19
+ baseSchema = baseSchema._def.innerType;
20
+ } else if (typeName === 'ZodDefault') {
21
+ hasDefault = true;
22
+ defaultValue = baseSchema._def.defaultValue();
23
+ baseSchema = baseSchema._def.innerType;
24
+ } else if (typeName === 'ZodEffects') {
25
+ baseSchema = baseSchema._def.schema;
26
+ } else {
27
+ break;
28
+ }
29
+ }
30
+
31
+ const typeName = baseSchema._def?.typeName;
32
+ const createTypeInfo = (type: TypeInfo['type'], defaultVal: any): TypeInfo => ({
33
+ type,
34
+ schema, // Store original schema
35
+ source: 'zod',
36
+ default: hasDefault ? defaultValue : defaultVal,
37
+ nullable: isNullable,
38
+ optional: isOptional,
39
+ });
40
+
41
+ switch (typeName) {
42
+ case 'ZodNumber':
43
+ return createTypeInfo('number', 0);
44
+ case 'ZodString':
45
+ return createTypeInfo('string', '');
46
+ case 'ZodBoolean':
47
+ return createTypeInfo('boolean', false);
48
+ case 'ZodArray':
49
+ return createTypeInfo('array', []);
50
+ case 'ZodObject':
51
+ return createTypeInfo('object', {});
52
+ case 'ZodDate':
53
+ return createTypeInfo('date', new Date());
54
+ default:
55
+ return null;
56
+ }
57
+ }
58
+
59
+ export function buildShadowNode(value: any, schema?: any): SyncEngineShadowNode {
60
+ const meta: SyncEngineShadowMetadata = {
61
+ validation: { status: 'NOT_VALIDATED', errors: [] },
62
+ };
63
+
64
+ if (schema) {
65
+ meta.typeInfo = getTypeFromZodSchema(schema) || undefined;
66
+ // If value is missing and there's a default in the schema, use it
67
+ if ((value === undefined || value === null) && meta.typeInfo?.default !== undefined) {
68
+ value = meta.typeInfo.default;
69
+ }
70
+ }
71
+
72
+ if (value === null || typeof value !== 'object') {
73
+ meta.value = value;
74
+ return { _meta: meta };
75
+ }
76
+
77
+ if (Array.isArray(value)) {
78
+ const node: SyncEngineShadowNode = { _meta: { ...meta, arrayKeys: [] } };
79
+ const keys: string[] = [];
80
+ const elementSchema = schema?._def.type; // Get schema for array elements
81
+
82
+ value.forEach((item, index) => {
83
+ const itemId = `id:sync_${Date.now()}_${index}`;
84
+ node[itemId] = buildShadowNode(item, elementSchema);
85
+ keys.push(itemId);
86
+ });
87
+ node._meta!.arrayKeys = keys;
88
+ return node;
89
+ }
90
+
91
+ const node: SyncEngineShadowNode = { _meta: meta };
92
+ const shape = schema?._def.shape();
93
+
94
+ for (const key in value) {
95
+ if (Object.prototype.hasOwnProperty.call(value, key)) {
96
+ const fieldSchema = shape ? shape[key] : undefined;
97
+ node[key] = buildShadowNode(value[key], fieldSchema);
98
+ }
99
+ }
100
+ return node;
101
+ }
@@ -0,0 +1,25 @@
1
+ // test/index.spec.ts
2
+ import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
3
+ import { describe, it, expect } from 'vitest';
4
+ import worker from '../src/index';
5
+
6
+ // For now, you'll need to do something like this to get a correctly-typed
7
+ // `Request` to pass to `worker.fetch()`.
8
+ const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
9
+
10
+ describe('Hello World worker', () => {
11
+ it('responds with Hello World! (unit style)', async () => {
12
+ const request = new IncomingRequest('http://example.com');
13
+ // Create an empty context to pass to `worker.fetch()`.
14
+ const ctx = createExecutionContext();
15
+ const response = await worker.fetch(request, env, ctx);
16
+ // Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
17
+ await waitOnExecutionContext(ctx);
18
+ expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
19
+ });
20
+
21
+ it('responds with Hello World! (integration style)', async () => {
22
+ const response = await SELF.fetch('https://example.com');
23
+ expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
24
+ });
25
+ });
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "types": ["@cloudflare/workers-types/experimental", "@cloudflare/vitest-pool-workers"]
5
+ },
6
+ "include": ["./**/*.ts", "../worker-configuration.d.ts"],
7
+ "exclude": []
8
+ }
@@ -0,0 +1,46 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
+
5
+ /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
6
+ "target": "es2021",
7
+ /* Specify a set of bundled library declaration files that describe the target runtime environment. */
8
+ "lib": ["es2021"],
9
+ /* Specify what JSX code is generated. */
10
+ "jsx": "react-jsx",
11
+
12
+ /* Specify what module code is generated. */
13
+ "module": "es2022",
14
+ /* Specify how TypeScript looks up a file from a given module specifier. */
15
+ "moduleResolution": "Bundler",
16
+ /* Specify type package names to be included without being referenced in a source file. */
17
+ "types": [
18
+ "@cloudflare/workers-types/2023-07-01"
19
+ ],
20
+ /* Enable importing .json files */
21
+ "resolveJsonModule": true,
22
+
23
+ /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
24
+ "allowJs": true,
25
+ /* Enable error reporting in type-checked JavaScript files. */
26
+ "checkJs": false,
27
+
28
+ /* Disable emitting files from a compilation. */
29
+ "noEmit": true,
30
+
31
+ /* Ensure that each file can be safely transpiled without relying on other imports. */
32
+ "isolatedModules": true,
33
+ /* Allow 'import x from y' when a module doesn't have a default export. */
34
+ "allowSyntheticDefaultImports": true,
35
+ /* Ensure that casing is correct in imports. */
36
+ "forceConsistentCasingInFileNames": true,
37
+
38
+ /* Enable all strict type-checking options. */
39
+ "strict": true,
40
+
41
+ /* Skip type checking all .d.ts files. */
42
+ "skipLibCheck": true
43
+ },
44
+ "exclude": ["test"],
45
+ "include": ["worker-configuration.d.ts", "src/**/*.ts"]
46
+ }
@@ -0,0 +1,11 @@
1
+ import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';
2
+
3
+ export default defineWorkersConfig({
4
+ test: {
5
+ poolOptions: {
6
+ workers: {
7
+ wrangler: { configPath: './wrangler.jsonc' },
8
+ },
9
+ },
10
+ },
11
+ });