Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,18 @@ class Replicate {
* @returns {Promise<object>} - Resolves with the output of running the model
*/
async run(identifier, options) {
const pattern =
/^(?<owner>[a-zA-Z0-9-_]+?)\/(?<name>[a-zA-Z0-9-_]+?):(?<version>[0-9a-fA-F]+)$/;
const match = identifier.match(pattern);
// Define a pattern for owner and model names that allows
// letters, digits, and certain special characters.
// Example: "user123", "abc__123", "user.name"
const namePattern = /[a-zA-Z0-9]+(?:(?:[._]|__|[-]*)[a-zA-Z0-9]+)*/;

// Define a pattern for "owner/name:version" format with named capturing groups.
// Example: "user123/repo_a:1a2b3c"
const pattern = new RegExp(
`^(?<owner>${namePattern.source})/(?<name>${namePattern.source}):(?<version>[0-9a-fA-F]+)$`
);

const match = identifier.match(pattern);
if (!match || !match.groups) {
throw new Error(
'Invalid version. It must be in the format "owner/name:version"'
Expand Down
31 changes: 31 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,37 @@ describe('Replicate client', () => {
);
expect(output).toBe('foobar');
});

test('Does not throw an error for identifier containing hyphen and full stop', async () => {
nock(BASE_URL)
.post('/predictions')
.reply(200, {
id: 'ufawqhfynnddngldkgtslldrkq',
status: 'processing',
})
.get('/predictions/ufawqhfynnddngldkgtslldrkq')
.reply(200, {
id: 'ufawqhfynnddngldkgtslldrkq',
status: 'succeeded',
output: 'foobar',
});

await expect(client.run('a/b-1.0:abc123', { input: { text: 'Hello, world!' } })).resolves.not.toThrow();
});

test('Throws an error for invalid identifiers', async () => {
const options = { input: { text: 'Hello, world!' } }

await expect(client.run('owner/model:invalid', options)).rejects.toThrow();

// @ts-expect-error
await expect(client.run('owner:abc123', options)).rejects.toThrow();

await expect(client.run('/model:abc123', options)).rejects.toThrow();

// @ts-expect-error
await expect(client.run(':abc123', options)).rejects.toThrow();
});
});

// Continue with tests for other methods
Expand Down