@gjsify/fetch 0.4.0 → 0.4.3

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.
@@ -1,130 +0,0 @@
1
- import { run, describe, it, expect } from '@gjsify/unit';
2
-
3
- run({
4
- async FetchTest() {
5
- await describe('Headers', async () => {
6
- await it('creates empty headers', async () => {
7
- const h = new Headers();
8
- expect(h.get('content-type')).toBeNull();
9
- });
10
-
11
- await it('gets/sets headers case-insensitively', async () => {
12
- const h = new Headers({ 'Content-Type': 'text/plain' });
13
- expect(h.get('content-type')).toBe('text/plain');
14
- expect(h.get('CONTENT-TYPE')).toBe('text/plain');
15
- });
16
-
17
- await it('append combines values with comma', async () => {
18
- const h = new Headers();
19
- h.append('x-custom', 'a');
20
- h.append('x-custom', 'b');
21
- expect(h.get('x-custom')).toBe('a, b');
22
- });
23
-
24
- await it('delete removes a header', async () => {
25
- const h = new Headers({ 'x-token': 'abc' });
26
- h.delete('x-token');
27
- expect(h.get('x-token')).toBeNull();
28
- });
29
-
30
- await it('has() checks existence', async () => {
31
- const h = new Headers({ 'x-foo': 'bar' });
32
- expect(h.has('x-foo')).toBe(true);
33
- expect(h.has('x-missing')).toBe(false);
34
- });
35
- });
36
-
37
- await describe('Request', async () => {
38
- await it('constructs with URL string', async () => {
39
- const r = new Request('https://example.com');
40
- expect(r.url).toBe('https://example.com/');
41
- expect(r.method).toBe('GET');
42
- });
43
-
44
- await it('method is uppercased', async () => {
45
- const r = new Request('https://example.com', { method: 'post' });
46
- expect(r.method).toBe('POST');
47
- });
48
-
49
- await it('constructs with custom headers', async () => {
50
- const r = new Request('https://example.com', {
51
- headers: { 'content-type': 'application/json' },
52
- method: 'POST',
53
- body: '{}',
54
- });
55
- expect(r.headers.get('content-type')).toBe('application/json');
56
- });
57
-
58
- await it('clone() preserves url and method', async () => {
59
- const r = new Request('https://example.com', { method: 'DELETE' });
60
- const c = r.clone();
61
- expect(c.url).toBe(r.url);
62
- expect(c.method).toBe('DELETE');
63
- });
64
-
65
- await it('has signal property', async () => {
66
- const r = new Request('https://example.com');
67
- expect(r.signal).toBeDefined();
68
- expect(r.signal.aborted).toBe(false);
69
- });
70
- });
71
-
72
- await describe('Response', async () => {
73
- await it('constructs with status', async () => {
74
- const r = new Response('body', { status: 201 });
75
- expect(r.status).toBe(201);
76
- expect(r.ok).toBe(true);
77
- });
78
-
79
- await it('ok is false for error status', async () => {
80
- const r = new Response('', { status: 404 });
81
- expect(r.ok).toBe(false);
82
- });
83
-
84
- await it('reads text body', async () => {
85
- const r = new Response('hello world');
86
- expect(await r.text()).toBe('hello world');
87
- });
88
-
89
- await it('reads json body', async () => {
90
- const r = new Response('{"x":42}');
91
- expect(await r.json()).toStrictEqual({ x: 42 });
92
- });
93
-
94
- await it('reads arrayBuffer body', async () => {
95
- const r = new Response(new Uint8Array([1, 2, 3]));
96
- const buf = await r.arrayBuffer();
97
- expect(buf.byteLength).toBe(3);
98
- expect(new Uint8Array(buf)[0]).toBe(1);
99
- });
100
-
101
- await it('bodyUsed prevents double-read', async () => {
102
- const r = new Response('data');
103
- expect(r.bodyUsed).toBe(false);
104
- await r.text();
105
- expect(r.bodyUsed).toBe(true);
106
- });
107
-
108
- await it('Response.error() returns type=error', async () => {
109
- const r = Response.error();
110
- expect(r.ok).toBe(false);
111
- expect(r.status).toBe(0);
112
- });
113
- });
114
-
115
- await describe('fetch', async () => {
116
- await it('fetches a local static file', async () => {
117
- const r = await fetch('/tests/browser/harness/index.html');
118
- expect(r.ok).toBe(true);
119
- const text = await r.text();
120
- expect(text).toContain('<!DOCTYPE html>');
121
- });
122
-
123
- await it('returns 404 for missing file', async () => {
124
- const r = await fetch('/tests/browser/__nonexistent__.html');
125
- expect(r.ok).toBe(false);
126
- expect(r.status).toBe(404);
127
- });
128
- });
129
- },
130
- });
package/src/test.mts DELETED
@@ -1,9 +0,0 @@
1
- import '@gjsify/node-globals/register/process';
2
- import '@gjsify/node-globals/register/buffer';
3
- import '@gjsify/node-globals/register/url';
4
- import 'fetch/register'; // register fetch/Headers/Request/Response globals on GJS (no-op on Node)
5
- import { run } from '@gjsify/unit';
6
-
7
- import testSuite from './index.spec.js';
8
-
9
- run({ testSuite });
@@ -1 +0,0 @@
1
- export * from './system-error.js';
@@ -1,11 +0,0 @@
1
- export interface SystemError {
2
- address?: string;
3
- code: string;
4
- dest?: string;
5
- errno: number;
6
- info?: object;
7
- message: string;
8
- path?: string;
9
- port?: number;
10
- syscall: string
11
- };
@@ -1,8 +0,0 @@
1
- // Re-export Blob/File from buffer (which provides the polyfill on GJS)
2
- // Reference: Node.js buffer.Blob (available since v18)
3
- import { Blob, File } from 'node:buffer';
4
-
5
- export {
6
- Blob,
7
- File,
8
- }
@@ -1,29 +0,0 @@
1
- /**
2
- * Parse a data: URI into its components.
3
- * Replaces the `data-uri-to-buffer` npm package.
4
- *
5
- * Format: data:[<mediatype>][;base64],<data>
6
- */
7
- export function parseDataUri(uri: string): { buffer: Uint8Array; typeFull: string } {
8
- const match = uri.match(/^data:([^,]*?)(;base64)?,(.*)$/s);
9
- if (!match) {
10
- throw new TypeError(`Invalid data URI: ${uri.slice(0, 50)}...`);
11
- }
12
-
13
- const typeFull = match[1] || 'text/plain;charset=US-ASCII';
14
- const isBase64 = !!match[2];
15
- const data = match[3];
16
-
17
- let buffer: Uint8Array;
18
- if (isBase64) {
19
- const binaryString = atob(data);
20
- buffer = new Uint8Array(binaryString.length);
21
- for (let i = 0; i < binaryString.length; i++) {
22
- buffer[i] = binaryString.charCodeAt(i);
23
- }
24
- } else {
25
- buffer = new TextEncoder().encode(decodeURIComponent(data));
26
- }
27
-
28
- return { buffer, typeFull };
29
- }
@@ -1,9 +0,0 @@
1
- export const getSearch = (parsedURL: URL) => {
2
- if (parsedURL.search) {
3
- return parsedURL.search;
4
- }
5
-
6
- const lastOffset = parsedURL.href.length - 1;
7
- const hash = parsedURL.hash || (parsedURL.href[lastOffset] === '#' ? '#' : '');
8
- return parsedURL.href[lastOffset - hash.length] === '?' ? '?' : '';
9
- };
@@ -1,11 +0,0 @@
1
- const redirectStatus = new Set([301, 302, 303, 307, 308]);
2
-
3
- /**
4
- * Redirect code matching
5
- *
6
- * @param {number} code - Status code
7
- * @return {boolean}
8
- */
9
- export const isRedirect = code => {
10
- return redirectStatus.has(code);
11
- };
package/src/utils/is.ts DELETED
@@ -1,88 +0,0 @@
1
- import { URL } from '@gjsify/url';
2
- import { Blob } from './blob-from.js';
3
-
4
- /**
5
- * Is.js
6
- *
7
- * Object type checks.
8
- */
9
-
10
- const NAME = Symbol.toStringTag;
11
-
12
- /**
13
- * Check if `obj` is a URLSearchParams object
14
- * ref: https://github.com/node-fetch/node-fetch/issues/296#issuecomment-307598143
15
- * @param {*} object - Object to check for
16
- * @return {boolean}
17
- */
18
- export const isURLSearchParameters = object => {
19
- return (
20
- typeof object === 'object' &&
21
- typeof object.append === 'function' &&
22
- typeof object.delete === 'function' &&
23
- typeof object.get === 'function' &&
24
- typeof object.getAll === 'function' &&
25
- typeof object.has === 'function' &&
26
- typeof object.set === 'function' &&
27
- typeof object.sort === 'function' &&
28
- object[NAME] === 'URLSearchParams'
29
- );
30
- };
31
-
32
- /**
33
- * Check if `object` is a W3C `Blob` object (which `File` inherits from)
34
- * @param object Object to check for
35
- */
36
- export const isBlob = (value: unknown): value is Blob => {
37
- if (!value || typeof value !== 'object') return false;
38
- const obj = value as Record<string | symbol, unknown>;
39
- return (
40
- typeof obj.arrayBuffer === 'function' &&
41
- typeof obj.type === 'string' &&
42
- typeof obj.stream === 'function' &&
43
- typeof obj.constructor === 'function' &&
44
- /^(Blob|File)$/.test(obj[NAME] as string)
45
- );
46
- };
47
-
48
- /**
49
- * Check if `obj` is an instance of AbortSignal.
50
- * @param object - Object to check for
51
- */
52
- export const isAbortSignal = (object: unknown): object is AbortSignal => {
53
- if (typeof object !== 'object' || object === null) return false;
54
- const obj = object as Record<string | symbol, unknown>;
55
- return (
56
- obj[NAME] === 'AbortSignal' ||
57
- obj[NAME] === 'EventTarget'
58
- );
59
- };
60
-
61
- /**
62
- * isDomainOrSubdomain reports whether sub is a subdomain (or exact match) of
63
- * the parent domain.
64
- *
65
- * Both domains must already be in canonical form.
66
- * @param {string|URL} original
67
- * @param {string|URL} destination
68
- */
69
- export const isDomainOrSubdomain = (destination, original) => {
70
- const orig = new URL(original).hostname;
71
- const dest = new URL(destination).hostname;
72
-
73
- return orig === dest || orig.endsWith(`.${dest}`);
74
- };
75
-
76
- /**
77
- * isSameProtocol reports whether the two provided URLs use the same protocol.
78
- *
79
- * Both domains must already be in canonical form.
80
- * @param {string|URL} original
81
- * @param {string|URL} destination
82
- */
83
- export const isSameProtocol = (destination, original) => {
84
- const orig = new URL(original).protocol;
85
- const dest = new URL(destination).protocol;
86
-
87
- return orig === dest;
88
- };