@devvit/server 0.11.20-next-2025-08-08-16-39-07-0861787df.0 → 0.11.20-next-2025-08-11-14-42-10-6b8f5c219.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.
- package/context.d.ts +4 -4
- package/context.d.ts.map +1 -1
- package/context.js +3 -3
- package/create-server.js +2 -2
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js +1 -1
- package/package.json +8 -8
- package/{request-context.d.ts → server-context.d.ts} +7 -6
- package/server-context.d.ts.map +1 -0
- package/server-context.js +30 -0
- package/request-context.d.ts.map +0 -1
- package/request-context.js +0 -22
package/context.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Context } from './server-context.js';
|
|
2
2
|
/**
|
|
3
|
-
* Runs an async callback with the given
|
|
3
|
+
* Runs an async callback with the given Context. Code in the callback can use
|
|
4
4
|
* `getContext()` to get the context. For testing.
|
|
5
5
|
* @experimental
|
|
6
6
|
* @param context The context to run the callback with
|
|
7
7
|
* @param callback The callback to run
|
|
8
8
|
*/
|
|
9
|
-
export declare function runWithContext<T>(context:
|
|
10
|
-
export declare const context:
|
|
9
|
+
export declare function runWithContext<T>(context: Context, callback: () => Promise<T>): Promise<T>;
|
|
10
|
+
export declare const context: Context;
|
|
11
11
|
/** For testing. @experimental */
|
|
12
12
|
export declare function setContextCache(key: string, value: unknown): void;
|
|
13
13
|
/** For testing. @experimental */
|
package/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAyBnD;;;;;;GAMG;AACH,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAEhG;AAED,eAAO,MAAM,OAAO,SAalB,CAAC;AAEH,iCAAiC;AACjC,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAMjE;AAED,iCAAiC;AACjC,wBAAgB,eAAe,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAM7D"}
|
package/context.js
CHANGED
|
@@ -7,9 +7,9 @@ catch {
|
|
|
7
7
|
// Hack: workaround inclusion in Blocks client builds.
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
|
-
* Gets the current
|
|
10
|
+
* Gets the current Context. This is set by the server when handling a request. If there is no
|
|
11
11
|
* context, this will throw an error.
|
|
12
|
-
* @returns The current
|
|
12
|
+
* @returns The current Context.
|
|
13
13
|
* @throws Error Will throw an error if there is no context.
|
|
14
14
|
*/
|
|
15
15
|
function getContext() {
|
|
@@ -20,7 +20,7 @@ function getContext() {
|
|
|
20
20
|
return ctx;
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
|
-
* Runs an async callback with the given
|
|
23
|
+
* Runs an async callback with the given Context. Code in the callback can use
|
|
24
24
|
* `getContext()` to get the context. For testing.
|
|
25
25
|
* @experimental
|
|
26
26
|
* @param context The context to run the callback with
|
package/create-server.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { createServer as nodeCreateServer } from 'node:http';
|
|
3
3
|
import { runWithContext } from './context.js';
|
|
4
|
-
import {
|
|
4
|
+
import { Context } from './server-context.js';
|
|
5
5
|
/**
|
|
6
6
|
* Creates a new Devvit server. This implements the same API as Node.js's `createServer` function,
|
|
7
7
|
* but we do not guarantee that this actually creates an HTTP server of any kind - it may be any
|
|
@@ -15,7 +15,7 @@ export const createServer = (optionsOrRequestListener, requestListener) => {
|
|
|
15
15
|
};
|
|
16
16
|
function _createServer(options, requestListener) {
|
|
17
17
|
const server = nodeCreateServer(options, async (req, res) => {
|
|
18
|
-
const context =
|
|
18
|
+
const context = Context(req.headers);
|
|
19
19
|
return runWithContext(context, async () => {
|
|
20
20
|
return requestListener?.(req, res);
|
|
21
21
|
});
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { context, getContextCache, runWithContext, setContextCache } from './context.js';
|
|
2
2
|
export { createServer } from './create-server.js';
|
|
3
3
|
export { getServerPort } from './get-server-port.js';
|
|
4
|
-
export {
|
|
4
|
+
export { Context, setContext } from './server-context.js';
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC"}
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { context, getContextCache, runWithContext, setContextCache } from './context.js';
|
|
2
2
|
export { createServer } from './create-server.js';
|
|
3
3
|
export { getServerPort } from './get-server-port.js';
|
|
4
|
-
export {
|
|
4
|
+
export { Context, setContext } from './server-context.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devvit/server",
|
|
3
|
-
"version": "0.11.20-next-2025-08-
|
|
3
|
+
"version": "0.11.20-next-2025-08-11-14-42-10-6b8f5c219.0",
|
|
4
4
|
"license": "BSD-3-Clause",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -30,18 +30,18 @@
|
|
|
30
30
|
},
|
|
31
31
|
"types": "./dist/index.d.ts",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@devvit/protos": "0.11.20-next-2025-08-
|
|
34
|
-
"@devvit/public-api": "0.11.20-next-2025-08-
|
|
35
|
-
"@devvit/shared": "0.11.20-next-2025-08-
|
|
36
|
-
"@devvit/shared-types": "0.11.20-next-2025-08-
|
|
33
|
+
"@devvit/protos": "0.11.20-next-2025-08-11-14-42-10-6b8f5c219.0",
|
|
34
|
+
"@devvit/public-api": "0.11.20-next-2025-08-11-14-42-10-6b8f5c219.0",
|
|
35
|
+
"@devvit/shared": "0.11.20-next-2025-08-11-14-42-10-6b8f5c219.0",
|
|
36
|
+
"@devvit/shared-types": "0.11.20-next-2025-08-11-14-42-10-6b8f5c219.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@devvit/repo-tools": "0.11.20-next-2025-08-
|
|
40
|
-
"@devvit/tsconfig": "0.11.20-next-2025-08-
|
|
39
|
+
"@devvit/repo-tools": "0.11.20-next-2025-08-11-14-42-10-6b8f5c219.0",
|
|
40
|
+
"@devvit/tsconfig": "0.11.20-next-2025-08-11-14-42-10-6b8f5c219.0",
|
|
41
41
|
"eslint": "9.11.1",
|
|
42
42
|
"typescript": "5.8.3",
|
|
43
43
|
"vitest": "1.6.1"
|
|
44
44
|
},
|
|
45
45
|
"source": "./src/index.ts",
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "a07e98369a1df038a1cb8a2263673d14938364cd"
|
|
47
47
|
}
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import type { Metadata } from '@devvit/protos';
|
|
2
|
-
import type
|
|
2
|
+
import { type BaseContext } from '@devvit/shared';
|
|
3
3
|
/** Devvit server context for the lifetime of a request. */
|
|
4
|
-
export type
|
|
4
|
+
export type Context = BaseContext & {
|
|
5
5
|
cache?: {
|
|
6
6
|
[key: string]: unknown;
|
|
7
7
|
};
|
|
8
|
+
metadata: Metadata;
|
|
8
9
|
};
|
|
9
10
|
/** Designed to be compatible with IncomingHttpHeaders and any KV. */
|
|
10
11
|
type Headers = {
|
|
11
12
|
[header: string]: string | string[] | undefined;
|
|
12
13
|
};
|
|
13
|
-
/** Constructs a new
|
|
14
|
-
export declare let
|
|
14
|
+
/** Constructs a new Context. */
|
|
15
|
+
export declare let Context: (headers: Readonly<Headers>) => Context;
|
|
15
16
|
/**
|
|
16
17
|
* Overwrite the context provider for test.
|
|
17
18
|
* @experimental
|
|
18
19
|
*/
|
|
19
|
-
export declare function
|
|
20
|
+
export declare function setContext(fn: typeof Context): void;
|
|
20
21
|
/** @internal */
|
|
21
22
|
export declare function metaFromIncomingMessage(headers: Readonly<Headers>): Metadata;
|
|
22
23
|
export {};
|
|
23
|
-
//# sourceMappingURL=
|
|
24
|
+
//# sourceMappingURL=server-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-context.d.ts","sourceRoot":"","sources":["../src/server-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE/C,OAAO,EAAE,KAAK,WAAW,EAAc,MAAM,gBAAgB,CAAC;AAG9D,2DAA2D;AAC3D,MAAM,MAAM,OAAO,GAAG,WAAW,GAAG;IAClC,KAAK,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IACnC,QAAQ,EAAE,QAAQ,CAAC;CACpB,CAAC;AAEF,qEAAqE;AACrE,KAAK,OAAO,GAAG;IAAE,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAA;CAAE,CAAC;AAEnE,gCAAgC;AAChC,eAAO,IAAI,OAAO,GAAI,SAAS,QAAQ,CAAC,OAAO,CAAC,KAAG,OAUlD,CAAC;AAEF;;;GAGG;AACH,wBAAgB,UAAU,CAAC,EAAE,EAAE,OAAO,OAAO,GAAG,IAAI,CAEnD;AAED,gBAAgB;AAChB,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,QAAQ,CAM5E"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { getContextFromMetadata } from '@devvit/public-api/devvit/internals/context.js';
|
|
2
|
+
import { T2, T3, T5 } from '@devvit/shared';
|
|
3
|
+
import { Header, headerPrefix } from '@devvit/shared-types/Header.js';
|
|
4
|
+
/** Constructs a new Context. */
|
|
5
|
+
export let Context = (headers) => {
|
|
6
|
+
const meta = metaFromIncomingMessage(headers);
|
|
7
|
+
const publicApiContext = getContextFromMetadata(meta, meta[Header.Post]?.values[0]);
|
|
8
|
+
return {
|
|
9
|
+
...publicApiContext,
|
|
10
|
+
subredditId: T5(publicApiContext.subredditId),
|
|
11
|
+
userId: publicApiContext.userId ? T2(publicApiContext.userId) : undefined,
|
|
12
|
+
postId: publicApiContext.postId ? T3(publicApiContext.postId) : undefined,
|
|
13
|
+
subredditName: publicApiContext.subredditName, // This is guaranteed to be defined
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Overwrite the context provider for test.
|
|
18
|
+
* @experimental
|
|
19
|
+
*/
|
|
20
|
+
export function setContext(fn) {
|
|
21
|
+
Context = fn;
|
|
22
|
+
}
|
|
23
|
+
/** @internal */
|
|
24
|
+
export function metaFromIncomingMessage(headers) {
|
|
25
|
+
const meta = {};
|
|
26
|
+
for (const [key, val] of Object.entries(headers))
|
|
27
|
+
if (key.startsWith(headerPrefix))
|
|
28
|
+
meta[key] = { values: typeof val === 'object' ? val : val == null ? [] : [val] };
|
|
29
|
+
return meta;
|
|
30
|
+
}
|
package/request-context.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.d.ts","sourceRoot":"","sources":["../src/request-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAItD,2DAA2D;AAC3D,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,QAAQ,CAAC,GAAG;IACzD,KAAK,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;CACpC,CAAC;AAEF,qEAAqE;AACrE,KAAK,OAAO,GAAG;IAAE,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAA;CAAE,CAAC;AAEnE,uCAAuC;AACvC,eAAO,IAAI,cAAc,GAAI,SAAS,QAAQ,CAAC,OAAO,CAAC,KAAG,cAGzD,CAAC;AAEF;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,OAAO,cAAc,GAAG,IAAI,CAEjE;AAED,gBAAgB;AAChB,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,QAAQ,CAM5E"}
|
package/request-context.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { getContextFromMetadata } from '@devvit/public-api/devvit/internals/context.js';
|
|
2
|
-
import { Header, headerPrefix } from '@devvit/shared-types/Header.js';
|
|
3
|
-
/** Constructs a new RequestContext. */
|
|
4
|
-
export let RequestContext = (headers) => {
|
|
5
|
-
const meta = metaFromIncomingMessage(headers);
|
|
6
|
-
return getContextFromMetadata(meta, meta[Header.Post]?.values[0]);
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Overwrite the context provider for test.
|
|
10
|
-
* @experimental
|
|
11
|
-
*/
|
|
12
|
-
export function setRequestContext(fn) {
|
|
13
|
-
RequestContext = fn;
|
|
14
|
-
}
|
|
15
|
-
/** @internal */
|
|
16
|
-
export function metaFromIncomingMessage(headers) {
|
|
17
|
-
const meta = {};
|
|
18
|
-
for (const [key, val] of Object.entries(headers))
|
|
19
|
-
if (key.startsWith(headerPrefix))
|
|
20
|
-
meta[key] = { values: typeof val === 'object' ? val : val == null ? [] : [val] };
|
|
21
|
-
return meta;
|
|
22
|
-
}
|