wirejs-resources 0.1.89 → 0.1.91-async-api
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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/overrides.d.ts +2 -0
- package/dist/resources/background-job.d.ts +31 -0
- package/dist/resources/background-job.js +51 -0
- package/package.json +1 -1
- package/dist/hosting/client.d.ts +0 -1
- package/dist/hosting/client.js +0 -68
- package/dist/internal/client.d.ts +0 -1
- package/dist/internal/client.js +0 -68
- package/dist/setup/index.d.ts +0 -1
- package/dist/setup/index.js +0 -27
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/overrides.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { FileService } from "./services/file";
|
|
|
2
2
|
import type { AuthenticationService } from "./services/authentication";
|
|
3
3
|
import type { Secret } from "./resources/secret";
|
|
4
4
|
import type { RealtimeService } from "./services/realtime";
|
|
5
|
+
import type { BackgroundJob } from "./resources/background-job";
|
|
5
6
|
/**
|
|
6
7
|
* Used by hosting providers to provide service overrides.
|
|
7
8
|
*/
|
|
@@ -11,4 +12,5 @@ export declare const overrides: {
|
|
|
11
12
|
FileService?: typeof FileService;
|
|
12
13
|
Secret?: typeof Secret;
|
|
13
14
|
RealtimeService?: typeof RealtimeService;
|
|
15
|
+
BackgroundJob?: typeof BackgroundJob;
|
|
14
16
|
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Resource } from '../resource.js';
|
|
2
|
+
/**
|
|
3
|
+
* A function that will be executed in the background.
|
|
4
|
+
*
|
|
5
|
+
* Intended for longer-running work that will continue after an initial API response
|
|
6
|
+
* to the client.
|
|
7
|
+
*
|
|
8
|
+
* ## WARNINGS
|
|
9
|
+
*
|
|
10
|
+
* 1. It may execute in a completely different environment that the one that invoked it.
|
|
11
|
+
* DO NOT depend on global variables, closures, or other process state that is not lazily
|
|
12
|
+
* created or constructed in-place by the script.
|
|
13
|
+
* 2. Fire-and-forget behavior is undefined. As soon as the `Promise` returned by your
|
|
14
|
+
* function resolves, the execution environment is free to terminate the process.
|
|
15
|
+
* 3. In local development, **timeouts are not enforced.** (Yet.)
|
|
16
|
+
*/
|
|
17
|
+
export declare class BackgroundJob<T extends (...args: any) => Promise<void>> extends Resource {
|
|
18
|
+
#private;
|
|
19
|
+
constructor(scope: Resource | string, id: string, options: {
|
|
20
|
+
handler: T;
|
|
21
|
+
timeoutSeconds?: number;
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Serializes parameter data and schedules background execution.
|
|
25
|
+
*
|
|
26
|
+
* **NOTE:** This resolves once the job is scheduled; *not when it completes!*
|
|
27
|
+
*
|
|
28
|
+
* @param data
|
|
29
|
+
*/
|
|
30
|
+
start(...args: Parameters<T>): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Resource } from '../resource.js';
|
|
2
|
+
/**
|
|
3
|
+
* A function that will be executed in the background.
|
|
4
|
+
*
|
|
5
|
+
* Intended for longer-running work that will continue after an initial API response
|
|
6
|
+
* to the client.
|
|
7
|
+
*
|
|
8
|
+
* ## WARNINGS
|
|
9
|
+
*
|
|
10
|
+
* 1. It may execute in a completely different environment that the one that invoked it.
|
|
11
|
+
* DO NOT depend on global variables, closures, or other process state that is not lazily
|
|
12
|
+
* created or constructed in-place by the script.
|
|
13
|
+
* 2. Fire-and-forget behavior is undefined. As soon as the `Promise` returned by your
|
|
14
|
+
* function resolves, the execution environment is free to terminate the process.
|
|
15
|
+
* 3. In local development, **timeouts are not enforced.** (Yet.)
|
|
16
|
+
*/
|
|
17
|
+
export class BackgroundJob extends Resource {
|
|
18
|
+
#handler;
|
|
19
|
+
#timeoutSeconds;
|
|
20
|
+
constructor(scope, id, options) {
|
|
21
|
+
super(scope, id);
|
|
22
|
+
this.#handler = options.handler;
|
|
23
|
+
if (options.timeoutSeconds && (options.timeoutSeconds < 1 || options.timeoutSeconds > 900)) {
|
|
24
|
+
throw new Error('timeoutSeconds must be between 1 and 900 seconds.');
|
|
25
|
+
}
|
|
26
|
+
this.#timeoutSeconds = options.timeoutSeconds ?? 900;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Serializes parameter data and schedules background execution.
|
|
30
|
+
*
|
|
31
|
+
* **NOTE:** This resolves once the job is scheduled; *not when it completes!*
|
|
32
|
+
*
|
|
33
|
+
* @param data
|
|
34
|
+
*/
|
|
35
|
+
async start(...args) {
|
|
36
|
+
// Serialize and deserialize is expected to occur in cloud contexts. So, we do
|
|
37
|
+
// the same here to ensure local development behaves similarly to production.
|
|
38
|
+
const serializedArgs = JSON.parse(JSON.stringify(args));
|
|
39
|
+
// Hacky delay for now to give the current request time to complete and to
|
|
40
|
+
// simulate a little bit of the delay that would be expected in a real
|
|
41
|
+
// background job.
|
|
42
|
+
setTimeout(async () => {
|
|
43
|
+
try {
|
|
44
|
+
await this.#handler(...serializedArgs);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.error('Background job failed:', error);
|
|
48
|
+
}
|
|
49
|
+
}, 300);
|
|
50
|
+
}
|
|
51
|
+
}
|
package/package.json
CHANGED
package/dist/hosting/client.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
|
package/dist/hosting/client.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
async function callApi(INTERNAL_API_URL, method, ...args) {
|
|
2
|
-
function isNode() {
|
|
3
|
-
return typeof args[0]?.cookies?.getAll === 'function';
|
|
4
|
-
}
|
|
5
|
-
function apiUrl() {
|
|
6
|
-
if (isNode()) {
|
|
7
|
-
return INTERNAL_API_URL;
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
return "/api";
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
let cookieHeader = {};
|
|
14
|
-
if (isNode()) {
|
|
15
|
-
const context = args[0];
|
|
16
|
-
const cookies = context.cookies.getAll();
|
|
17
|
-
cookieHeader = typeof cookies === 'object'
|
|
18
|
-
? {
|
|
19
|
-
Cookie: Object.entries(cookies).map(kv => kv.join('=')).join('; ')
|
|
20
|
-
}
|
|
21
|
-
: {};
|
|
22
|
-
}
|
|
23
|
-
const response = await fetch(apiUrl(), {
|
|
24
|
-
method: 'POST',
|
|
25
|
-
headers: {
|
|
26
|
-
'Content-Type': 'application/json',
|
|
27
|
-
...cookieHeader
|
|
28
|
-
},
|
|
29
|
-
body: JSON.stringify([{ method, args: [...args] }]),
|
|
30
|
-
});
|
|
31
|
-
const body = await response.json();
|
|
32
|
-
if (isNode()) {
|
|
33
|
-
const context = args[0];
|
|
34
|
-
for (const c of response.headers.getSetCookie()) {
|
|
35
|
-
const parts = c.split(';').map(p => p.trim());
|
|
36
|
-
const flags = parts.slice(1);
|
|
37
|
-
const [name, value] = parts[0].split('=').map(decodeURIComponent);
|
|
38
|
-
const httpOnly = flags.includes('HttpOnly');
|
|
39
|
-
const secure = flags.includes('Secure');
|
|
40
|
-
const maxAgePart = flags.find(f => f.startsWith('Max-Age='))?.split('=')[1];
|
|
41
|
-
context.cookies.set({
|
|
42
|
-
name,
|
|
43
|
-
value,
|
|
44
|
-
httpOnly,
|
|
45
|
-
secure,
|
|
46
|
-
maxAge: maxAgePart ? parseInt(maxAgePart) : undefined
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const error = body[0].error;
|
|
51
|
-
if (error) {
|
|
52
|
-
throw new Error(error);
|
|
53
|
-
}
|
|
54
|
-
const value = body[0].data;
|
|
55
|
-
return value;
|
|
56
|
-
}
|
|
57
|
-
;
|
|
58
|
-
export function apiTree(INTERNAL_API_URL, path = []) {
|
|
59
|
-
return new Proxy(function () { }, {
|
|
60
|
-
apply(_target, _thisArg, args) {
|
|
61
|
-
return callApi(INTERNAL_API_URL, path, ...args);
|
|
62
|
-
},
|
|
63
|
-
get(_target, prop) {
|
|
64
|
-
return apiTree(INTERNAL_API_URL, [...path, prop]);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
|
package/dist/internal/client.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
async function callApi(INTERNAL_API_URL, method, ...args) {
|
|
2
|
-
function isNode() {
|
|
3
|
-
return typeof args[0]?.cookies?.getAll === 'function';
|
|
4
|
-
}
|
|
5
|
-
function apiUrl() {
|
|
6
|
-
if (isNode()) {
|
|
7
|
-
return INTERNAL_API_URL;
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
return "/api";
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
let cookieHeader = {};
|
|
14
|
-
if (isNode()) {
|
|
15
|
-
const context = args[0];
|
|
16
|
-
const cookies = context.cookies.getAll();
|
|
17
|
-
cookieHeader = typeof cookies === 'object'
|
|
18
|
-
? {
|
|
19
|
-
Cookie: Object.entries(cookies).map(kv => kv.join('=')).join('; ')
|
|
20
|
-
}
|
|
21
|
-
: {};
|
|
22
|
-
}
|
|
23
|
-
const response = await fetch(apiUrl(), {
|
|
24
|
-
method: 'POST',
|
|
25
|
-
headers: {
|
|
26
|
-
'Content-Type': 'application/json',
|
|
27
|
-
...cookieHeader
|
|
28
|
-
},
|
|
29
|
-
body: JSON.stringify([{ method, args: [...args] }]),
|
|
30
|
-
});
|
|
31
|
-
const body = await response.json();
|
|
32
|
-
if (isNode()) {
|
|
33
|
-
const context = args[0];
|
|
34
|
-
for (const c of response.headers.getSetCookie()) {
|
|
35
|
-
const parts = c.split(';').map(p => p.trim());
|
|
36
|
-
const flags = parts.slice(1);
|
|
37
|
-
const [name, value] = parts[0].split('=').map(decodeURIComponent);
|
|
38
|
-
const httpOnly = flags.includes('HttpOnly');
|
|
39
|
-
const secure = flags.includes('Secure');
|
|
40
|
-
const maxAgePart = flags.find(f => f.startsWith('Max-Age='))?.split('=')[1];
|
|
41
|
-
context.cookies.set({
|
|
42
|
-
name,
|
|
43
|
-
value,
|
|
44
|
-
httpOnly,
|
|
45
|
-
secure,
|
|
46
|
-
maxAge: maxAgePart ? parseInt(maxAgePart) : undefined
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const error = body[0].error;
|
|
51
|
-
if (error) {
|
|
52
|
-
throw new Error(error);
|
|
53
|
-
}
|
|
54
|
-
const value = body[0].data;
|
|
55
|
-
return value;
|
|
56
|
-
}
|
|
57
|
-
;
|
|
58
|
-
export function apiTree(INTERNAL_API_URL, path = []) {
|
|
59
|
-
return new Proxy(function () { }, {
|
|
60
|
-
apply(_target, _thisArg, args) {
|
|
61
|
-
return callApi(INTERNAL_API_URL, path, ...args);
|
|
62
|
-
},
|
|
63
|
-
get(_target, prop) {
|
|
64
|
-
return apiTree(INTERNAL_API_URL, [...path, prop]);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
;
|
package/dist/setup/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function prebuildApi(): Promise<void>;
|
package/dist/setup/index.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import process from 'process';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
export async function prebuildApi() {
|
|
5
|
-
const CWD = process.cwd();
|
|
6
|
-
let API_URL = '/api';
|
|
7
|
-
const indexModule = await import(path.join(CWD, 'index.js'));
|
|
8
|
-
try {
|
|
9
|
-
const backendConfigModule = await import(path.join(CWD, 'config.js'));
|
|
10
|
-
const backendConfig = backendConfigModule.default;
|
|
11
|
-
console.log("backend config found", backendConfig);
|
|
12
|
-
if (backendConfig.apiUrl) {
|
|
13
|
-
API_URL = backendConfig.apiUrl;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
catch {
|
|
17
|
-
console.log("No backend API config found.");
|
|
18
|
-
}
|
|
19
|
-
const apiCode = Object.keys(indexModule)
|
|
20
|
-
.map(k => `export const ${k} = apiTree(INTERNAL_API_URL, ${JSON.stringify([k])});`)
|
|
21
|
-
.join('\n');
|
|
22
|
-
const baseClient = [
|
|
23
|
-
`import { apiTree } from "wirejs-resources/hosting/client.js";`,
|
|
24
|
-
`const INTERNAL_API_URL = ${JSON.stringify(API_URL)};`,
|
|
25
|
-
].join('\n');
|
|
26
|
-
await fs.promises.writeFile(path.join(CWD, 'index.client.js'), [baseClient, apiCode].join('\n\n'));
|
|
27
|
-
}
|