seroval-plugins 1.0.0

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.
Files changed (51) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +44 -0
  3. package/dist/cjs/development/web.cjs +684 -0
  4. package/dist/cjs/development/web.cjs.map +7 -0
  5. package/dist/cjs/production/web.cjs +1 -0
  6. package/dist/esm/development/web.mjs +661 -0
  7. package/dist/esm/development/web.mjs.map +7 -0
  8. package/dist/esm/production/web.mjs +1 -0
  9. package/dist/types/web/blob.d.ts +8 -0
  10. package/dist/types/web/blob.d.ts.map +1 -0
  11. package/dist/types/web/custom-event.d.ts +8 -0
  12. package/dist/types/web/custom-event.d.ts.map +1 -0
  13. package/dist/types/web/dom-exception.d.ts +8 -0
  14. package/dist/types/web/dom-exception.d.ts.map +1 -0
  15. package/dist/types/web/event.d.ts +8 -0
  16. package/dist/types/web/event.d.ts.map +1 -0
  17. package/dist/types/web/file.d.ts +9 -0
  18. package/dist/types/web/file.d.ts.map +1 -0
  19. package/dist/types/web/form-data.d.ts +8 -0
  20. package/dist/types/web/form-data.d.ts.map +1 -0
  21. package/dist/types/web/headers.d.ts +4 -0
  22. package/dist/types/web/headers.d.ts.map +1 -0
  23. package/dist/types/web/image-data.d.ts +10 -0
  24. package/dist/types/web/image-data.d.ts.map +1 -0
  25. package/dist/types/web/index.d.ts +14 -0
  26. package/dist/types/web/index.d.ts.map +1 -0
  27. package/dist/types/web/readable-stream.d.ts +8 -0
  28. package/dist/types/web/readable-stream.d.ts.map +1 -0
  29. package/dist/types/web/request.d.ts +8 -0
  30. package/dist/types/web/request.d.ts.map +1 -0
  31. package/dist/types/web/response.d.ts +8 -0
  32. package/dist/types/web/response.d.ts.map +1 -0
  33. package/dist/types/web/url-search-params.d.ts +4 -0
  34. package/dist/types/web/url-search-params.d.ts.map +1 -0
  35. package/dist/types/web/url.d.ts +4 -0
  36. package/dist/types/web/url.d.ts.map +1 -0
  37. package/package.json +73 -0
  38. package/web/blob.ts +41 -0
  39. package/web/custom-event.ts +66 -0
  40. package/web/dom-exception.ts +57 -0
  41. package/web/event.ts +62 -0
  42. package/web/file.ts +50 -0
  43. package/web/form-data.ts +99 -0
  44. package/web/headers.ts +40 -0
  45. package/web/image-data.ts +74 -0
  46. package/web/index.ts +13 -0
  47. package/web/readable-stream.ts +139 -0
  48. package/web/request.ts +75 -0
  49. package/web/response.ts +61 -0
  50. package/web/url-search-params.ts +34 -0
  51. package/web/url.ts +31 -0
package/web/file.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { SerovalNode } from 'seroval';
2
+ import { createPlugin } from 'seroval';
3
+
4
+ interface FileNode {
5
+ name: SerovalNode;
6
+ options: SerovalNode;
7
+ buffer: SerovalNode;
8
+ }
9
+
10
+ const FilePlugin = /* @__PURE__ */ createPlugin<File, FileNode>({
11
+ tag: 'seroval-plugins/web/File',
12
+ test(value) {
13
+ if (typeof File === 'undefined') {
14
+ return false;
15
+ }
16
+ return value instanceof File;
17
+ },
18
+ parse: {
19
+ async async(value, ctx) {
20
+ return {
21
+ name: await ctx.parse(value.name),
22
+ options: await ctx.parse({
23
+ type: value.type,
24
+ lastModified: value.lastModified,
25
+ }),
26
+ buffer: await ctx.parse(await value.arrayBuffer()),
27
+ };
28
+ },
29
+ },
30
+ serialize(node, ctx) {
31
+ return (
32
+ 'new File([' +
33
+ ctx.serialize(node.buffer) +
34
+ '],' +
35
+ ctx.serialize(node.name) +
36
+ ',' +
37
+ ctx.serialize(node.options) +
38
+ ')'
39
+ );
40
+ },
41
+ deserialize(node, ctx) {
42
+ return new File(
43
+ [ctx.deserialize(node.buffer) as ArrayBuffer],
44
+ ctx.deserialize(node.name) as string,
45
+ ctx.deserialize(node.options) as FilePropertyBag,
46
+ );
47
+ },
48
+ });
49
+
50
+ export default FilePlugin;
@@ -0,0 +1,99 @@
1
+ import type { SerovalNode } from 'seroval';
2
+ import { createPlugin } from 'seroval';
3
+ import FilePlugin from './file';
4
+
5
+ type FormDataInit = [key: string, value: FormDataEntryValue][];
6
+
7
+ function convertFormData(instance: FormData): FormDataInit {
8
+ const items: FormDataInit = [];
9
+ // biome-ignore lint/complexity/noForEach: <explanation>
10
+ instance.forEach((value, key) => {
11
+ items.push([key, value]);
12
+ });
13
+ return items;
14
+ }
15
+
16
+ const FORM_DATA_FACTORY = {};
17
+
18
+ const FormDataFactoryPlugin = /* @__PURE__ */ createPlugin<object, undefined>({
19
+ tag: 'seroval-plugins/web/FormDataFactory',
20
+ test(value) {
21
+ return value === FORM_DATA_FACTORY;
22
+ },
23
+ parse: {
24
+ sync() {
25
+ return undefined;
26
+ },
27
+ async async() {
28
+ return await Promise.resolve(undefined);
29
+ },
30
+ stream() {
31
+ return undefined;
32
+ },
33
+ },
34
+ serialize(_node, ctx) {
35
+ return ctx.createEffectfulFunction(
36
+ ['e', 'f', 'i', 's', 't'],
37
+ 'f=new FormData;for(i=0,s=e.length;i<s;i++)f.append((t=e[i])[0],t[1]);return f',
38
+ );
39
+ },
40
+ deserialize() {
41
+ return FORM_DATA_FACTORY;
42
+ },
43
+ });
44
+
45
+ interface FormDataNode {
46
+ factory: SerovalNode;
47
+ entries: SerovalNode;
48
+ }
49
+
50
+ const FormDataPlugin = /* @__PURE__ */ createPlugin<FormData, FormDataNode>({
51
+ tag: 'seroval-plugins/web/FormData',
52
+ extends: [FilePlugin, FormDataFactoryPlugin],
53
+ test(value) {
54
+ if (typeof FormData === 'undefined') {
55
+ return false;
56
+ }
57
+ return value instanceof FormData;
58
+ },
59
+ parse: {
60
+ sync(value, ctx) {
61
+ return {
62
+ factory: ctx.parse(FORM_DATA_FACTORY),
63
+ entries: ctx.parse(convertFormData(value)),
64
+ };
65
+ },
66
+ async async(value, ctx) {
67
+ return {
68
+ factory: await ctx.parse(FORM_DATA_FACTORY),
69
+ entries: await ctx.parse(convertFormData(value)),
70
+ };
71
+ },
72
+ stream(value, ctx) {
73
+ return {
74
+ factory: ctx.parse(FORM_DATA_FACTORY),
75
+ entries: ctx.parse(convertFormData(value)),
76
+ };
77
+ },
78
+ },
79
+ serialize(node, ctx) {
80
+ return (
81
+ '(' +
82
+ ctx.serialize(node.factory) +
83
+ ')(' +
84
+ ctx.serialize(node.entries) +
85
+ ')'
86
+ );
87
+ },
88
+ deserialize(node, ctx) {
89
+ const instance = new FormData();
90
+ const entries = ctx.deserialize(node.entries) as FormDataInit;
91
+ for (let i = 0, len = entries.length; i < len; i++) {
92
+ const entry = entries[i];
93
+ instance.append(entry[0], entry[1]);
94
+ }
95
+ return instance;
96
+ },
97
+ });
98
+
99
+ export default FormDataPlugin;
package/web/headers.ts ADDED
@@ -0,0 +1,40 @@
1
+ import type { SerovalNode } from 'seroval';
2
+ import { createPlugin } from 'seroval';
3
+
4
+ function convertHeaders(instance: Headers): HeadersInit {
5
+ const items: HeadersInit = [];
6
+ // biome-ignore lint/complexity/noForEach: <explanation>
7
+ instance.forEach((value, key) => {
8
+ items.push([key, value]);
9
+ });
10
+ return items;
11
+ }
12
+
13
+ const HeadersPlugin = /* @__PURE__ */ createPlugin<Headers, SerovalNode>({
14
+ tag: 'seroval-plugins/web/Headers',
15
+ test(value) {
16
+ if (typeof Headers === 'undefined') {
17
+ return false;
18
+ }
19
+ return value instanceof Headers;
20
+ },
21
+ parse: {
22
+ sync(value, ctx) {
23
+ return ctx.parse(convertHeaders(value));
24
+ },
25
+ async async(value, ctx) {
26
+ return await ctx.parse(convertHeaders(value));
27
+ },
28
+ stream(value, ctx) {
29
+ return ctx.parse(convertHeaders(value));
30
+ },
31
+ },
32
+ serialize(node, ctx) {
33
+ return 'new Headers(' + ctx.serialize(node) + ')';
34
+ },
35
+ deserialize(node, ctx) {
36
+ return new Headers(ctx.deserialize(node) as HeadersInit);
37
+ },
38
+ });
39
+
40
+ export default HeadersPlugin;
@@ -0,0 +1,74 @@
1
+ import type { SerovalNode } from 'seroval';
2
+ import { createPlugin } from 'seroval';
3
+
4
+ interface ImageDataNode {
5
+ data: SerovalNode;
6
+ width: SerovalNode;
7
+ height: SerovalNode;
8
+ options: SerovalNode;
9
+ }
10
+
11
+ const ImageDataPlugin = /* @__PURE__ */ createPlugin<ImageData, ImageDataNode>({
12
+ tag: 'seroval-plugins/web/ImageData',
13
+ test(value) {
14
+ if (typeof ImageData === 'undefined') {
15
+ return false;
16
+ }
17
+ return value instanceof ImageData;
18
+ },
19
+ parse: {
20
+ sync(value, ctx) {
21
+ return {
22
+ data: ctx.parse(value.data),
23
+ width: ctx.parse(value.width),
24
+ height: ctx.parse(value.height),
25
+ options: ctx.parse({
26
+ colorSpace: value.colorSpace,
27
+ }),
28
+ };
29
+ },
30
+ async async(value, ctx) {
31
+ return {
32
+ data: await ctx.parse(value.data),
33
+ width: await ctx.parse(value.width),
34
+ height: await ctx.parse(value.height),
35
+ options: await ctx.parse({
36
+ colorSpace: value.colorSpace,
37
+ }),
38
+ };
39
+ },
40
+ stream(value, ctx) {
41
+ return {
42
+ data: ctx.parse(value.data),
43
+ width: ctx.parse(value.width),
44
+ height: ctx.parse(value.height),
45
+ options: ctx.parse({
46
+ colorSpace: value.colorSpace,
47
+ }),
48
+ };
49
+ },
50
+ },
51
+ serialize(node, ctx) {
52
+ return (
53
+ 'new ImageData(' +
54
+ ctx.serialize(node.data) +
55
+ ',' +
56
+ ctx.serialize(node.width) +
57
+ ',' +
58
+ ctx.serialize(node.height) +
59
+ ',' +
60
+ ctx.serialize(node.options) +
61
+ ')'
62
+ );
63
+ },
64
+ deserialize(node, ctx) {
65
+ return new ImageData(
66
+ ctx.deserialize(node.data) as Uint8ClampedArray,
67
+ ctx.deserialize(node.width) as number,
68
+ ctx.deserialize(node.height) as number,
69
+ ctx.deserialize(node.options) as ImageDataSettings,
70
+ );
71
+ },
72
+ });
73
+
74
+ export default ImageDataPlugin;
package/web/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ export { default as BlobPlugin } from './blob';
2
+ export { default as CustomEventPlugin } from './custom-event';
3
+ export { default as DOMExceptionPlugin } from './dom-exception';
4
+ export { default as EventPlugin } from './event';
5
+ export { default as FilePlugin } from './file';
6
+ export { default as FormDataPlugin } from './form-data';
7
+ export { default as HeadersPlugin } from './headers';
8
+ export { default as ImageDataPlugin } from './image-data';
9
+ export { default as ReadableStreamPlugin } from './readable-stream';
10
+ export { default as Request } from './request';
11
+ export { default as Response } from './response';
12
+ export { default as URLSearchParamsPlugin } from './url-search-params';
13
+ export { default as URL } from './url';
@@ -0,0 +1,139 @@
1
+ import type { SerovalNode, Stream } from 'seroval';
2
+ import { createPlugin, createStream } from 'seroval';
3
+
4
+ const READABLE_STREAM_FACTORY = {};
5
+
6
+ const ReadableStreamFactoryPlugin = /* @__PURE__ */ createPlugin<
7
+ object,
8
+ undefined
9
+ >({
10
+ tag: 'seroval-plugins/web/ReadableStreamFactory',
11
+ test(value) {
12
+ return value === READABLE_STREAM_FACTORY;
13
+ },
14
+ parse: {
15
+ sync() {
16
+ return undefined;
17
+ },
18
+ async async() {
19
+ return await Promise.resolve(undefined);
20
+ },
21
+ stream() {
22
+ return undefined;
23
+ },
24
+ },
25
+ serialize(_node, ctx) {
26
+ return ctx.createFunction(
27
+ ['d'],
28
+ 'new ReadableStream({start:' +
29
+ ctx.createEffectfulFunction(
30
+ ['c'],
31
+ 'd.on({next:' +
32
+ ctx.createEffectfulFunction(['v'], 'c.enqueue(v)') +
33
+ ',throw:' +
34
+ ctx.createEffectfulFunction(['v'], 'c.error(v)') +
35
+ ',return:' +
36
+ ctx.createEffectfulFunction([], 'c.close()') +
37
+ '})',
38
+ ) +
39
+ '})',
40
+ );
41
+ },
42
+ deserialize() {
43
+ return READABLE_STREAM_FACTORY;
44
+ },
45
+ });
46
+
47
+ function toStream<T>(value: ReadableStream<T>): Stream<T | undefined> {
48
+ const stream = createStream<T | undefined>();
49
+
50
+ const reader = value.getReader();
51
+
52
+ async function push(): Promise<void> {
53
+ try {
54
+ const result = await reader.read();
55
+ if (result.done) {
56
+ stream.return(result.value);
57
+ } else {
58
+ stream.next(result.value);
59
+ await push();
60
+ }
61
+ } catch (error) {
62
+ stream.throw(error);
63
+ }
64
+ }
65
+
66
+ push().catch(() => {
67
+ //
68
+ });
69
+
70
+ return stream;
71
+ }
72
+
73
+ interface ReadableStreamNode {
74
+ factory: SerovalNode;
75
+ stream: SerovalNode;
76
+ }
77
+
78
+ const ReadableStreamPlugin = /* @__PURE__ */ createPlugin<
79
+ ReadableStream,
80
+ ReadableStreamNode
81
+ >({
82
+ tag: 'seroval/plugins/web/ReadableStream',
83
+ extends: [ReadableStreamFactoryPlugin],
84
+ test(value) {
85
+ if (typeof ReadableStream === 'undefined') {
86
+ return false;
87
+ }
88
+ return value instanceof ReadableStream;
89
+ },
90
+ parse: {
91
+ sync(_value, ctx) {
92
+ return {
93
+ factory: ctx.parse(READABLE_STREAM_FACTORY),
94
+ stream: ctx.parse(createStream()),
95
+ };
96
+ },
97
+ async async(value, ctx) {
98
+ return {
99
+ factory: await ctx.parse(READABLE_STREAM_FACTORY),
100
+ stream: await ctx.parse(toStream(value)),
101
+ };
102
+ },
103
+ stream(value, ctx) {
104
+ return {
105
+ factory: ctx.parse(READABLE_STREAM_FACTORY),
106
+ stream: ctx.parse(toStream(value)),
107
+ };
108
+ },
109
+ },
110
+ serialize(node, ctx) {
111
+ return (
112
+ '(' +
113
+ ctx.serialize(node.factory) +
114
+ ')(' +
115
+ ctx.serialize(node.stream) +
116
+ ')'
117
+ );
118
+ },
119
+ deserialize(node, ctx) {
120
+ const stream = ctx.deserialize(node.stream) as Stream<any>;
121
+ return new ReadableStream({
122
+ start(controller): void {
123
+ stream.on({
124
+ next(value) {
125
+ controller.enqueue(value);
126
+ },
127
+ throw(value) {
128
+ controller.error(value);
129
+ },
130
+ return() {
131
+ controller.close();
132
+ },
133
+ });
134
+ },
135
+ });
136
+ },
137
+ });
138
+
139
+ export default ReadableStreamPlugin;
package/web/request.ts ADDED
@@ -0,0 +1,75 @@
1
+ import type { SerovalNode } from 'seroval';
2
+ import { createPlugin } from 'seroval';
3
+ import ReadableStreamPlugin from './readable-stream';
4
+ import HeadersPlugin from './headers';
5
+
6
+ function createRequestOptions(
7
+ current: Request,
8
+ body: ArrayBuffer | ReadableStream | null,
9
+ ): RequestInit {
10
+ return {
11
+ body,
12
+ cache: current.cache,
13
+ credentials: current.credentials,
14
+ headers: current.headers,
15
+ integrity: current.integrity,
16
+ keepalive: current.keepalive,
17
+ method: current.method,
18
+ mode: current.mode,
19
+ redirect: current.redirect,
20
+ referrer: current.referrer,
21
+ referrerPolicy: current.referrerPolicy,
22
+ };
23
+ }
24
+
25
+ interface RequestNode {
26
+ url: SerovalNode;
27
+ options: SerovalNode;
28
+ }
29
+
30
+ const RequestPlugin = /* @__PURE__ */ createPlugin<Request, RequestNode>({
31
+ tag: 'seroval-plugins/web/Request',
32
+ extends: [ReadableStreamPlugin, HeadersPlugin],
33
+ test(value) {
34
+ if (typeof Request === 'undefined') {
35
+ return false;
36
+ }
37
+ return value instanceof Request;
38
+ },
39
+ parse: {
40
+ async async(value, ctx) {
41
+ return {
42
+ url: await ctx.parse(value.url),
43
+ options: await ctx.parse(
44
+ createRequestOptions(
45
+ value,
46
+ value.body ? await value.clone().arrayBuffer() : null,
47
+ ),
48
+ ),
49
+ };
50
+ },
51
+ stream(value, ctx) {
52
+ return {
53
+ url: ctx.parse(value.url),
54
+ options: ctx.parse(createRequestOptions(value, value.clone().body)),
55
+ };
56
+ },
57
+ },
58
+ serialize(node, ctx) {
59
+ return (
60
+ 'new Request(' +
61
+ ctx.serialize(node.url) +
62
+ ',' +
63
+ ctx.serialize(node.options) +
64
+ ')'
65
+ );
66
+ },
67
+ deserialize(node, ctx) {
68
+ return new Request(
69
+ ctx.deserialize(node.url) as string,
70
+ ctx.deserialize(node.options) as RequestInit,
71
+ );
72
+ },
73
+ });
74
+
75
+ export default RequestPlugin;
@@ -0,0 +1,61 @@
1
+ import type { SerovalNode } from 'seroval';
2
+ import { createPlugin } from 'seroval';
3
+ import ReadableStreamPlugin from './readable-stream';
4
+ import HeadersPlugin from './headers';
5
+
6
+ function createResponseOptions(current: Response): ResponseInit {
7
+ return {
8
+ headers: current.headers,
9
+ status: current.status,
10
+ statusText: current.statusText,
11
+ };
12
+ }
13
+
14
+ interface ResponseNode {
15
+ body: SerovalNode;
16
+ options: SerovalNode;
17
+ }
18
+
19
+ const ResponsePlugin = /* @__PURE__ */ createPlugin<Response, ResponseNode>({
20
+ tag: 'seroval-plugins/web/Response',
21
+ extends: [ReadableStreamPlugin, HeadersPlugin],
22
+ test(value) {
23
+ if (typeof Response === 'undefined') {
24
+ return false;
25
+ }
26
+ return value instanceof Response;
27
+ },
28
+ parse: {
29
+ async async(value, ctx) {
30
+ return {
31
+ body: await ctx.parse(
32
+ value.body ? await value.clone().arrayBuffer() : null,
33
+ ),
34
+ options: await ctx.parse(createResponseOptions(value)),
35
+ };
36
+ },
37
+ stream(value, ctx) {
38
+ return {
39
+ body: ctx.parse(value.clone().body),
40
+ options: ctx.parse(createResponseOptions(value)),
41
+ };
42
+ },
43
+ },
44
+ serialize(node, ctx) {
45
+ return (
46
+ 'new Response(' +
47
+ ctx.serialize(node.body) +
48
+ ',' +
49
+ ctx.serialize(node.options) +
50
+ ')'
51
+ );
52
+ },
53
+ deserialize(node, ctx) {
54
+ return new Response(
55
+ ctx.deserialize(node.body) as BodyInit,
56
+ ctx.deserialize(node.options) as ResponseInit,
57
+ );
58
+ },
59
+ });
60
+
61
+ export default ResponsePlugin;
@@ -0,0 +1,34 @@
1
+ import type { SerovalNode } from 'seroval';
2
+ import { createPlugin } from 'seroval';
3
+
4
+ const URLSearchParamsPlugin = /* @__PURE__ */ createPlugin<
5
+ URLSearchParams,
6
+ SerovalNode
7
+ >({
8
+ tag: 'seroval-plugins/web/URLSearchParams',
9
+ test(value) {
10
+ if (typeof URLSearchParams === 'undefined') {
11
+ return false;
12
+ }
13
+ return value instanceof URLSearchParams;
14
+ },
15
+ parse: {
16
+ sync(value, ctx) {
17
+ return ctx.parse(value.toString());
18
+ },
19
+ async async(value, ctx) {
20
+ return await ctx.parse(value.toString());
21
+ },
22
+ stream(value, ctx) {
23
+ return ctx.parse(value.toString());
24
+ },
25
+ },
26
+ serialize(node, ctx) {
27
+ return 'new URLSearchParams(' + ctx.serialize(node) + ')';
28
+ },
29
+ deserialize(node, ctx) {
30
+ return new URLSearchParams(ctx.deserialize(node) as string);
31
+ },
32
+ });
33
+
34
+ export default URLSearchParamsPlugin;
package/web/url.ts ADDED
@@ -0,0 +1,31 @@
1
+ import type { SerovalNode } from 'seroval';
2
+ import { createPlugin } from 'seroval';
3
+
4
+ const URLPlugin = /* @__PURE__ */ createPlugin<URL, SerovalNode>({
5
+ tag: 'seroval-plugins/web/URL',
6
+ test(value) {
7
+ if (typeof URL === 'undefined') {
8
+ return false;
9
+ }
10
+ return value instanceof URL;
11
+ },
12
+ parse: {
13
+ sync(value, ctx) {
14
+ return ctx.parse(value.href);
15
+ },
16
+ async async(value, ctx) {
17
+ return await ctx.parse(value.href);
18
+ },
19
+ stream(value, ctx) {
20
+ return ctx.parse(value.href);
21
+ },
22
+ },
23
+ serialize(node, ctx) {
24
+ return 'new URL(' + ctx.serialize(node) + ')';
25
+ },
26
+ deserialize(node, ctx) {
27
+ return new URL(ctx.deserialize(node) as string);
28
+ },
29
+ });
30
+
31
+ export default URLPlugin;