@pluv/platform-cloudflare 0.2.1 → 0.3.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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @pluv/platform-cloudflare@0.2.1 build /home/runner/work/pluv/pluv/packages/platform-cloudflare
2
+ > @pluv/platform-cloudflare@0.3.1 build /home/runner/work/pluv/pluv/packages/platform-cloudflare
3
3
  > tsup src/index.ts --format esm,cjs --dts
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,10 +8,10 @@
8
8
  CLI Target: es6
9
9
  ESM Build start
10
10
  CJS Build start
11
- ESM dist/index.mjs 5.53 KB
12
- ESM ⚡️ Build success in 98ms
13
- CJS dist/index.js 6.66 KB
14
- CJS ⚡️ Build success in 100ms
11
+ CJS dist/index.js 6.76 KB
12
+ CJS ⚡️ Build success in 101ms
13
+ ESM dist/index.mjs 5.63 KB
14
+ ESM ⚡️ Build success in 102ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 5163ms
17
- DTS dist/index.d.ts 2.57 KB
16
+ DTS ⚡️ Build success in 5082ms
17
+ DTS dist/index.d.ts 2.90 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @pluv/platform-cloudflare
2
2
 
3
+ ## 0.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [2e7cbfa]
8
+ - @pluv/io@0.6.0
9
+
10
+ ## 0.3.0
11
+
12
+ ### Minor Changes
13
+
14
+ - ae4e1f1: added platform-specific contexts to expose env in cloudflare and req in node.js
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [ae4e1f1]
19
+ - @pluv/io@0.5.0
20
+
3
21
  ## 0.2.1
4
22
 
5
23
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPlatform, PluvIO } from '@pluv/io';
2
- import { MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired } from '@pluv/types';
2
+ import { MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired, Id } from '@pluv/types';
3
3
 
4
4
  type CloudflareWebSocketConfig = AbstractWebSocketConfig;
5
5
  declare class CloudflareWebSocket extends AbstractWebSocket {
@@ -13,7 +13,11 @@ declare class CloudflareWebSocket extends AbstractWebSocket {
13
13
  terminate(): void;
14
14
  }
15
15
 
16
- declare class CloudflarePlatform extends AbstractPlatform<WebSocket> {
16
+ declare class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends AbstractPlatform<WebSocket, {
17
+ env: TEnv;
18
+ }, {
19
+ request: Request;
20
+ }> {
17
21
  convertWebSocket(webSocket: WebSocket, config: AbstractWebSocketConfig): CloudflareWebSocket;
18
22
  parseData(data: string | ArrayBuffer): Record<string, any>;
19
23
  randomUUID(): string;
@@ -24,8 +28,8 @@ interface AuthorizeFunctionContext {
24
28
  roomId: string;
25
29
  }
26
30
  type AuthorizeFunction<TPluv extends PluvIO<CloudflarePlatform>> = (ctx: AuthorizeFunctionContext) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluv>>>>;
27
- type CreatePluvHandlerConfig<TPluv extends PluvIO<CloudflarePlatform>, TBinding extends string, TEnv extends Record<string, any>> = {
28
- binding: TBinding;
31
+ type CreatePluvHandlerConfig<TPluv extends PluvIO<CloudflarePlatform>, TEnv extends Record<string, any>> = {
32
+ binding: string;
29
33
  endpoint?: string;
30
34
  modify?: (request: Request, response: Response, env: TEnv) => MaybePromise<Response>;
31
35
  io: TPluv;
@@ -37,13 +41,14 @@ type CreatePluvHandlerConfig<TPluv extends PluvIO<CloudflarePlatform>, TBinding
37
41
  type PluvHandlerFetch<TEnv extends Record<string, any> = {}> = (request: Request, env: TEnv) => Promise<Response | null>;
38
42
  interface CreatePluvHandlerResult<TEnv extends Record<string, any> = {}> {
39
43
  DurableObject: {
40
- new (state: DurableObjectState): DurableObject;
44
+ new (state: DurableObjectState, env: TEnv): DurableObject;
41
45
  };
42
46
  fetch: PluvHandlerFetch<TEnv>;
43
47
  handler: ExportedHandler<TEnv>;
44
48
  }
45
- declare const createPluvHandler: <TPluv extends PluvIO<CloudflarePlatform, any, any, any, any, any, any>, TBinding extends string, TEnv extends Record<string, any>>(config: CreatePluvHandlerConfig<TPluv, TBinding, TEnv>) => CreatePluvHandlerResult<TEnv>;
49
+ type InferCloudflarePluvHandlerEnv<TPluv extends PluvIO<CloudflarePlatform, any, any, any, any, any, any>> = TPluv extends PluvIO<CloudflarePlatform<infer IEnv>, any, any, any, any, any, any> ? IEnv : {};
50
+ declare const createPluvHandler: <TPluv extends PluvIO<CloudflarePlatform<{}>, any, any, any, any, any, any>>(config: CreatePluvHandlerConfig<TPluv, Id<InferCloudflarePluvHandlerEnv<TPluv>>>) => CreatePluvHandlerResult<Id<InferCloudflarePluvHandlerEnv<TPluv>>>;
46
51
 
47
- declare const platformCloudflare: () => CloudflarePlatform;
52
+ declare const platformCloudflare: <TEnv extends Record<string, any> = {}>() => CloudflarePlatform<TEnv>;
48
53
 
49
54
  export { AuthorizeFunction, AuthorizeFunctionContext, CreatePluvHandlerConfig, CreatePluvHandlerResult, PluvHandlerFetch, createPluvHandler, platformCloudflare };
package/dist/index.js CHANGED
@@ -50,8 +50,9 @@ var import_path_to_regexp = require("path-to-regexp");
50
50
  var createPluvHandler = (config) => {
51
51
  const { authorize, binding, endpoint = "/api/pluv", modify, io } = config;
52
52
  const DurableObject = class {
53
- constructor(state) {
54
- this._io = io.getRoom(state.id.toString());
53
+ constructor(state, env) {
54
+ this._env = env;
55
+ this._io = io.getRoom(state.id.toString(), { env });
55
56
  }
56
57
  fetch(request) {
57
58
  return __async(this, null, function* () {
@@ -61,7 +62,11 @@ var createPluvHandler = (config) => {
61
62
  }
62
63
  const { 0: client, 1: server } = new WebSocketPair();
63
64
  const token = new URL(request.url).searchParams.get("token");
64
- yield this._io.register(server, { token });
65
+ yield this._io.register(server, {
66
+ env: this._env,
67
+ request,
68
+ token
69
+ });
65
70
  return new Response(null, { status: 101, webSocket: client });
66
71
  });
67
72
  }
package/dist/index.mjs CHANGED
@@ -24,8 +24,9 @@ import { match } from "path-to-regexp";
24
24
  var createPluvHandler = (config) => {
25
25
  const { authorize, binding, endpoint = "/api/pluv", modify, io } = config;
26
26
  const DurableObject = class {
27
- constructor(state) {
28
- this._io = io.getRoom(state.id.toString());
27
+ constructor(state, env) {
28
+ this._env = env;
29
+ this._io = io.getRoom(state.id.toString(), { env });
29
30
  }
30
31
  fetch(request) {
31
32
  return __async(this, null, function* () {
@@ -35,7 +36,11 @@ var createPluvHandler = (config) => {
35
36
  }
36
37
  const { 0: client, 1: server } = new WebSocketPair();
37
38
  const token = new URL(request.url).searchParams.get("token");
38
- yield this._io.register(server, { token });
39
+ yield this._io.register(server, {
40
+ env: this._env,
41
+ request,
42
+ token
43
+ });
39
44
  return new Response(null, { status: 101, webSocket: client });
40
45
  });
41
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/platform-cloudflare",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "@pluv/io adapter for cloudflare workers",
5
5
  "author": "leedavidcs",
6
6
  "license": "MIT",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "path-to-regexp": "^6.2.1",
21
- "@pluv/io": "^0.4.2",
21
+ "@pluv/io": "^0.6.0",
22
22
  "@pluv/types": "^0.2.0"
23
23
  },
24
24
  "devDependencies": {
@@ -2,7 +2,9 @@ import type { AbstractWebSocketConfig } from "@pluv/io";
2
2
  import { AbstractPlatform } from "@pluv/io";
3
3
  import { CloudflareWebSocket } from "./CloudflareWebSocket";
4
4
 
5
- export class CloudflarePlatform extends AbstractPlatform<WebSocket> {
5
+ export class CloudflarePlatform<
6
+ TEnv extends Record<string, any> = {}
7
+ > extends AbstractPlatform<WebSocket, { env: TEnv }, { request: Request }> {
6
8
  public convertWebSocket(
7
9
  webSocket: WebSocket,
8
10
  config: AbstractWebSocketConfig
@@ -1,5 +1,6 @@
1
1
  import { IORoom, PluvIO } from "@pluv/io";
2
2
  import {
3
+ Id,
3
4
  InferIOAuthorize,
4
5
  InferIOAuthorizeRequired,
5
6
  InferIOAuthorizeUser,
@@ -19,10 +20,9 @@ export type AuthorizeFunction<TPluv extends PluvIO<CloudflarePlatform>> = (
19
20
 
20
21
  export type CreatePluvHandlerConfig<
21
22
  TPluv extends PluvIO<CloudflarePlatform>,
22
- TBinding extends string,
23
23
  TEnv extends Record<string, any>
24
24
  > = {
25
- binding: TBinding;
25
+ binding: string;
26
26
  endpoint?: string;
27
27
  modify?: (
28
28
  request: Request,
@@ -42,25 +42,47 @@ export type PluvHandlerFetch<TEnv extends Record<string, any> = {}> = (
42
42
  export interface CreatePluvHandlerResult<
43
43
  TEnv extends Record<string, any> = {}
44
44
  > {
45
- DurableObject: { new (state: DurableObjectState): DurableObject };
45
+ DurableObject: {
46
+ new (state: DurableObjectState, env: TEnv): DurableObject;
47
+ };
46
48
  fetch: PluvHandlerFetch<TEnv>;
47
49
  handler: ExportedHandler<TEnv>;
48
50
  }
49
51
 
52
+ type InferCloudflarePluvHandlerEnv<
53
+ TPluv extends PluvIO<CloudflarePlatform, any, any, any, any, any, any>
54
+ > = TPluv extends PluvIO<
55
+ CloudflarePlatform<infer IEnv>,
56
+ any,
57
+ any,
58
+ any,
59
+ any,
60
+ any,
61
+ any
62
+ >
63
+ ? IEnv
64
+ : {};
65
+
50
66
  export const createPluvHandler = <
51
- TPluv extends PluvIO<CloudflarePlatform, any, any, any, any, any, any>,
52
- TBinding extends string,
53
- TEnv extends Record<string, any>
67
+ TPluv extends PluvIO<CloudflarePlatform, any, any, any, any, any, any>
54
68
  >(
55
- config: CreatePluvHandlerConfig<TPluv, TBinding, TEnv>
56
- ): CreatePluvHandlerResult<TEnv> => {
69
+ config: CreatePluvHandlerConfig<
70
+ TPluv,
71
+ Id<InferCloudflarePluvHandlerEnv<TPluv>>
72
+ >
73
+ ): CreatePluvHandlerResult<Id<InferCloudflarePluvHandlerEnv<TPluv>>> => {
57
74
  const { authorize, binding, endpoint = "/api/pluv", modify, io } = config;
58
75
 
59
76
  const DurableObject = class implements DurableObject {
77
+ private _env: Id<InferCloudflarePluvHandlerEnv<TPluv>>;
60
78
  private _io: IORoom<CloudflarePlatform>;
61
79
 
62
- constructor(state: DurableObjectState) {
63
- this._io = io.getRoom(state.id.toString());
80
+ constructor(
81
+ state: DurableObjectState,
82
+ env: Id<InferCloudflarePluvHandlerEnv<TPluv>>
83
+ ) {
84
+ this._env = env;
85
+ this._io = io.getRoom(state.id.toString(), { env });
64
86
  }
65
87
 
66
88
  async fetch(
@@ -76,14 +98,22 @@ export const createPluvHandler = <
76
98
 
77
99
  const token = new URL(request.url).searchParams.get("token");
78
100
 
79
- await this._io.register(server, { token });
101
+ await this._io.register(server, {
102
+ env: this._env,
103
+ request,
104
+ token,
105
+ });
80
106
 
81
107
  return new Response(null, { status: 101, webSocket: client });
82
108
  }
83
109
  };
84
110
 
85
- const getDurableObjectNamespace = (env: TEnv): DurableObjectNamespace => {
86
- const namespace = env[binding];
111
+ const getDurableObjectNamespace = (
112
+ env: Id<InferCloudflarePluvHandlerEnv<TPluv>>
113
+ ): DurableObjectNamespace => {
114
+ const namespace = env[
115
+ binding as keyof typeof env
116
+ ] as DurableObjectNamespace;
87
117
 
88
118
  if (!namespace) {
89
119
  throw new Error(`Could not find DurableObject binding: ${binding}`);
@@ -92,7 +122,9 @@ export const createPluvHandler = <
92
122
  return namespace;
93
123
  };
94
124
 
95
- const authHandler: PluvHandlerFetch<TEnv> = async (request, env) => {
125
+ const authHandler: PluvHandlerFetch<
126
+ Id<InferCloudflarePluvHandlerEnv<TPluv>>
127
+ > = async (request, env) => {
96
128
  if (!authorize) return null;
97
129
 
98
130
  const { pathname, searchParams } = new URL(request.url);
@@ -138,7 +170,9 @@ export const createPluvHandler = <
138
170
  }
139
171
  };
140
172
 
141
- const roomHandler: PluvHandlerFetch<TEnv> = async (request, env) => {
173
+ const roomHandler: PluvHandlerFetch<
174
+ Id<InferCloudflarePluvHandlerEnv<TPluv>>
175
+ > = async (request, env) => {
142
176
  const { pathname } = new URL(request.url);
143
177
  const matcher = match<{ roomId: string }>(`${endpoint}/room/:roomId`);
144
178
  const matched = matcher(pathname);
@@ -161,13 +195,15 @@ export const createPluvHandler = <
161
195
  return room.fetch(request);
162
196
  };
163
197
 
164
- const fetch: PluvHandlerFetch<TEnv> = async (request, env) => {
198
+ const fetch: PluvHandlerFetch<
199
+ Id<InferCloudflarePluvHandlerEnv<TPluv>>
200
+ > = async (request, env) => {
165
201
  return [authHandler, roomHandler].reduce((promise, current) => {
166
202
  return promise.then((value) => value ?? current(request, env));
167
203
  }, Promise.resolve<Response | null>(null));
168
204
  };
169
205
 
170
- const handler: ExportedHandler<TEnv> = {
206
+ const handler: ExportedHandler<Id<InferCloudflarePluvHandlerEnv<TPluv>>> = {
171
207
  fetch: async (request, env) => {
172
208
  const response =
173
209
  (await fetch(request, env)) ??
@@ -1,5 +1,7 @@
1
1
  import { CloudflarePlatform } from "./CloudflarePlatform";
2
2
 
3
- export const platformCloudflare = (): CloudflarePlatform => {
4
- return new CloudflarePlatform();
3
+ export const platformCloudflare = <
4
+ TEnv extends Record<string, any> = {}
5
+ >(): CloudflarePlatform<TEnv> => {
6
+ return new CloudflarePlatform<TEnv>();
5
7
  };