wirejs-resources 0.1.170 → 0.1.172

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 @@
1
+ export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
@@ -0,0 +1,68 @@
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/index.d.ts CHANGED
@@ -13,6 +13,7 @@ export * from './types/index.js';
13
13
  export * from './derived-types.js';
14
14
  export * from './services/realtime.js';
15
15
  export * from './resources/background-job.js';
16
+ export * from './resources/cron-job.js';
16
17
  export * from './resources/key-value-store.js';
17
18
  export * from './resources/endpoint.js';
18
19
  export * from './resources/system-attribute.js';
package/dist/index.js CHANGED
@@ -12,6 +12,7 @@ export * from './types/index.js';
12
12
  export * from './derived-types.js';
13
13
  export * from './services/realtime.js';
14
14
  export * from './resources/background-job.js';
15
+ export * from './resources/cron-job.js';
15
16
  export * from './resources/key-value-store.js';
16
17
  export * from './resources/endpoint.js';
17
18
  export * from './resources/system-attribute.js';
@@ -0,0 +1 @@
1
+ export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
@@ -0,0 +1,68 @@
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,5 +1,6 @@
1
1
  import type { AuthenticationService } from "./services/authentication";
2
2
  import type { BackgroundJob } from "./resources/background-job";
3
+ import type { CronJob } from "./resources/cron-job";
3
4
  import type { DistributedTable } from "./resources/distributed-table";
4
5
  import type { Endpoint } from "./resources/endpoint";
5
6
  import type { FileService } from "./services/file";
@@ -13,6 +14,7 @@ import type { Setting } from "./resources/setting";
13
14
  export declare const overrides: {
14
15
  AuthenticationService?: typeof AuthenticationService;
15
16
  BackgroundJob?: typeof BackgroundJob;
17
+ CronJob?: typeof CronJob;
16
18
  DistributedTable?: typeof DistributedTable;
17
19
  Endpoint?: typeof Endpoint;
18
20
  FileService?: typeof FileService;
@@ -0,0 +1,28 @@
1
+ import { Resource } from '../resource.js';
2
+ /**
3
+ * A function that will be executed in the background on a recurring schedule
4
+ * defined by a cron expression.
5
+ *
6
+ * These MUST be created at the top-level of your application. I.e., they cannot be created
7
+ * dynamically in response to an API request or other event.
8
+ *
9
+ * A `CronJob` can also invoke an existing {@link BackgroundJob} from its handler.
10
+ *
11
+ * ## WARNINGS
12
+ *
13
+ * 1. These jobs execute in a completely different environment that the one that defines it.
14
+ * DO NOT depend on global variables, closures, or other process state that is not lazily
15
+ * created or constructed in-place by the script.
16
+ * 2. In local development, **timeouts are not enforced.** (Yet.)
17
+ */
18
+ export declare class CronJob extends Resource {
19
+ static registeredJobs: Map<string, {
20
+ schedule: string;
21
+ handler: () => Promise<void>;
22
+ }>;
23
+ readonly schedule: string;
24
+ constructor(scope: Resource | string, id: string, options: {
25
+ schedule: string;
26
+ handler: () => Promise<void>;
27
+ });
28
+ }
@@ -0,0 +1,29 @@
1
+ import { Resource } from '../resource.js';
2
+ /**
3
+ * A function that will be executed in the background on a recurring schedule
4
+ * defined by a cron expression.
5
+ *
6
+ * These MUST be created at the top-level of your application. I.e., they cannot be created
7
+ * dynamically in response to an API request or other event.
8
+ *
9
+ * A `CronJob` can also invoke an existing {@link BackgroundJob} from its handler.
10
+ *
11
+ * ## WARNINGS
12
+ *
13
+ * 1. These jobs execute in a completely different environment that the one that defines it.
14
+ * DO NOT depend on global variables, closures, or other process state that is not lazily
15
+ * created or constructed in-place by the script.
16
+ * 2. In local development, **timeouts are not enforced.** (Yet.)
17
+ */
18
+ export class CronJob extends Resource {
19
+ static registeredJobs = new Map();
20
+ schedule;
21
+ constructor(scope, id, options) {
22
+ super(scope, id);
23
+ this.schedule = options.schedule;
24
+ CronJob.registeredJobs.set(this.absoluteId, {
25
+ schedule: options.schedule,
26
+ handler: options.handler,
27
+ });
28
+ }
29
+ }
@@ -1,7 +1,13 @@
1
1
  import { Resource } from '../resource.js';
2
2
  export declare class Secret extends Resource {
3
- #private;
4
3
  constructor(scope: Resource | string, id: string);
5
- read(): Promise<any>;
6
- write(data: any): Promise<void>;
4
+ read(): Promise<string>;
5
+ write(value: string, options?: {
6
+ onlyIfNotExists?: boolean;
7
+ }): Promise<void | undefined>;
8
+ scan(): AsyncGenerator<never, AsyncGenerator<{
9
+ key: string;
10
+ value: string;
11
+ }, any, any> | undefined, unknown>;
12
+ isAlreadyExistsError(error: any): boolean | undefined;
7
13
  }
@@ -1,28 +1,38 @@
1
- import crypto from 'crypto';
1
+ import * as crypto from 'crypto';
2
2
  import { Resource } from '../resource.js';
3
- import { FileService } from '../services/file.js';
3
+ import { KeyValueStore } from './key-value-store.js';
4
4
  import { overrides } from '../overrides.js';
5
- const FILENAME = 'secret';
5
+ let secrets;
6
6
  export class Secret extends Resource {
7
- #fileService;
8
- #initPromise;
9
7
  constructor(scope, id) {
10
8
  super(scope, id);
11
- this.#fileService = new (overrides.FileService || FileService)(this, 'files');
12
- }
13
- #initialize() {
14
- this.#initPromise = this.#initPromise || this.#fileService.write(FILENAME, JSON.stringify(crypto.randomBytes(64).toString('base64url')), { onlyIfNotExists: true }).catch(error => {
15
- if (!this.#fileService.isAlreadyExistsError(error))
16
- throw error;
17
- });
18
- return this.#initPromise;
9
+ secrets = new (overrides.KeyValueStore || KeyValueStore)('wirejs', 'secrets');
19
10
  }
20
11
  async read() {
21
- await this.#initialize();
22
- return JSON.parse(await this.#fileService.read(FILENAME));
12
+ const existingValue = await secrets?.get(this.absoluteId);
13
+ if (!existingValue) {
14
+ try {
15
+ const initValue = crypto.randomBytes(64).toString('base64url');
16
+ await this.write(initValue, { onlyIfNotExists: true });
17
+ return initValue;
18
+ }
19
+ catch (error) {
20
+ if (!this.isAlreadyExistsError(error))
21
+ throw error;
22
+ return this.read();
23
+ }
24
+ }
25
+ else {
26
+ return existingValue;
27
+ }
28
+ }
29
+ async write(value, options) {
30
+ return secrets?.set(this.absoluteId, value, { onlyIfNotExists: options?.onlyIfNotExists });
31
+ }
32
+ async *scan() {
33
+ return secrets?.scan();
23
34
  }
24
- async write(data) {
25
- await this.#initialize();
26
- await this.#fileService.write(FILENAME, JSON.stringify(data));
35
+ isAlreadyExistsError(error) {
36
+ return secrets?.isAlreadyExistsError(error);
27
37
  }
28
38
  }
@@ -0,0 +1 @@
1
+ export declare function prebuildApi(): Promise<void>;
@@ -0,0 +1,27 @@
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-resources",
3
- "version": "0.1.170",
3
+ "version": "0.1.172",
4
4
  "description": "Basic services and server-side resources for wirejs apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",